manual.html 347 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Lua 5.4 Reference Manual</TITLE>
  5. <LINK REL="stylesheet" TYPE="text/css" HREF="lua.css">
  6. <LINK REL="stylesheet" TYPE="text/css" HREF="manual.css">
  7. <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
  8. </HEAD>
  9. <BODY>
  10. <H1>
  11. <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A>
  12. Lua 5.4 Reference Manual
  13. </H1>
  14. <P>
  15. by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
  16. <P>
  17. <SMALL>
  18. Copyright &copy; 2020 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. <DIV CLASS="menubar">
  23. <A HREF="contents.html#contents">contents</A>
  24. &middot;
  25. <A HREF="contents.html#index">index</A>
  26. &middot;
  27. <A HREF="http://www.lua.org/manual/">other versions</A>
  28. </DIV>
  29. <!-- ====================================================================== -->
  30. <p>
  31. <!-- $Id: manual.of $ -->
  32. <h1>1 &ndash; <a name="1">Introduction</a></h1>
  33. <p>
  34. Lua is a powerful, efficient, lightweight, embeddable scripting language.
  35. It supports procedural programming,
  36. object-oriented programming, functional programming,
  37. data-driven programming, and data description.
  38. <p>
  39. Lua combines simple procedural syntax with powerful data description
  40. constructs based on associative arrays and extensible semantics.
  41. Lua is dynamically typed,
  42. runs by interpreting bytecode with a register-based
  43. virtual machine,
  44. and has automatic memory management with
  45. a generational garbage collection,
  46. making it ideal for configuration, scripting,
  47. and rapid prototyping.
  48. <p>
  49. Lua is implemented as a library, written in <em>clean C</em>,
  50. the common subset of Standard&nbsp;C and C++.
  51. The Lua distribution includes a host program called <code>lua</code>,
  52. which uses the Lua library to offer a complete,
  53. standalone Lua interpreter,
  54. for interactive or batch use.
  55. Lua is intended to be used both as a powerful, lightweight,
  56. embeddable scripting language for any program that needs one,
  57. and as a powerful but lightweight and efficient stand-alone language.
  58. <p>
  59. As an extension language, Lua has no notion of a "main" program:
  60. it works <em>embedded</em> in a host client,
  61. called the <em>embedding program</em> or simply the <em>host</em>.
  62. (Frequently, this host is the stand-alone <code>lua</code> program.)
  63. The host program can invoke functions to execute a piece of Lua code,
  64. can write and read Lua variables,
  65. and can register C&nbsp;functions to be called by Lua code.
  66. Through the use of C&nbsp;functions, Lua can be augmented to cope with
  67. a wide range of different domains,
  68. thus creating customized programming languages sharing a syntactical framework.
  69. <p>
  70. Lua is free software,
  71. and is provided as usual with no guarantees,
  72. as stated in its license.
  73. The implementation described in this manual is available
  74. at Lua's official web site, <code>www.lua.org</code>.
  75. <p>
  76. Like any other reference manual,
  77. this document is dry in places.
  78. For a discussion of the decisions behind the design of Lua,
  79. see the technical papers available at Lua's web site.
  80. For a detailed introduction to programming in Lua,
  81. see Roberto's book, <em>Programming in Lua</em>.
  82. <h1>2 &ndash; <a name="2">Basic Concepts</a></h1>
  83. <p>
  84. This section describes the basic concepts of the language.
  85. <h2>2.1 &ndash; <a name="2.1">Values and Types</a></h2>
  86. <p>
  87. Lua is a dynamically typed language.
  88. This means that
  89. variables do not have types; only values do.
  90. There are no type definitions in the language.
  91. All values carry their own type.
  92. <p>
  93. All values in Lua are first-class values.
  94. This means that all values can be stored in variables,
  95. passed as arguments to other functions, and returned as results.
  96. <p>
  97. There are eight basic types in Lua:
  98. <em>nil</em>, <em>boolean</em>, <em>number</em>,
  99. <em>string</em>, <em>function</em>, <em>userdata</em>,
  100. <em>thread</em>, and <em>table</em>.
  101. The type <em>nil</em> has one single value, <b>nil</b>,
  102. whose main property is to be different from any other value;
  103. it often represents the absence of a useful value.
  104. The type <em>boolean</em> has two values, <b>false</b> and <b>true</b>.
  105. Both <b>nil</b> and <b>false</b> make a condition false;
  106. they are collectively called <em>false values</em>.
  107. Any other value makes a condition true.
  108. <p>
  109. The type <em>number</em> represents both
  110. integer numbers and real (floating-point) numbers,
  111. using two subtypes: <em>integer</em> and <em>float</em>.
  112. Standard Lua uses 64-bit integers and double-precision (64-bit) floats,
  113. but you can also compile Lua so that it
  114. uses 32-bit integers and/or single-precision (32-bit) floats.
  115. The option with 32 bits for both integers and floats
  116. is particularly attractive
  117. for small machines and embedded systems.
  118. (See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.)
  119. <p>
  120. Unless stated otherwise,
  121. any overflow when manipulating integer values <em>wrap around</em>,
  122. according to the usual rules of two-complement arithmetic.
  123. (In other words,
  124. the actual result is the unique representable integer
  125. that is equal modulo <em>2<sup>n</sup></em> to the mathematical result,
  126. where <em>n</em> is the number of bits of the integer type.)
  127. <p>
  128. Lua has explicit rules about when each subtype is used,
  129. but it also converts between them automatically as needed (see <a href="#3.4.3">&sect;3.4.3</a>).
  130. Therefore,
  131. the programmer may choose to mostly ignore the difference
  132. between integers and floats
  133. or to assume complete control over the representation of each number.
  134. <p>
  135. The type <em>string</em> represents immutable sequences of bytes.
  136. Lua is 8-bit clean:
  137. strings can contain any 8-bit value,
  138. including embedded zeros ('<code>\0</code>').
  139. Lua is also encoding-agnostic;
  140. it makes no assumptions about the contents of a string.
  141. The length of any string in Lua must fit in a Lua integer.
  142. <p>
  143. Lua can call (and manipulate) functions written in Lua and
  144. functions written in C (see <a href="#3.4.10">&sect;3.4.10</a>).
  145. Both are represented by the type <em>function</em>.
  146. <p>
  147. The type <em>userdata</em> is provided to allow arbitrary C&nbsp;data to
  148. be stored in Lua variables.
  149. A userdata value represents a block of raw memory.
  150. There are two kinds of userdata:
  151. <em>full userdata</em>,
  152. which is an object with a block of memory managed by Lua,
  153. and <em>light userdata</em>,
  154. which is simply a C&nbsp;pointer value.
  155. Userdata has no predefined operations in Lua,
  156. except assignment and identity test.
  157. By using <em>metatables</em>,
  158. the programmer can define operations for full userdata values
  159. (see <a href="#2.4">&sect;2.4</a>).
  160. Userdata values cannot be created or modified in Lua,
  161. only through the C&nbsp;API.
  162. This guarantees the integrity of data owned by
  163. the host program and C&nbsp;libraries.
  164. <p>
  165. The type <em>thread</em> represents independent threads of execution
  166. and it is used to implement coroutines (see <a href="#2.6">&sect;2.6</a>).
  167. Lua threads are not related to operating-system threads.
  168. Lua supports coroutines on all systems,
  169. even those that do not support threads natively.
  170. <p>
  171. The type <em>table</em> implements associative arrays,
  172. that is, arrays that can have as indices not only numbers,
  173. but any Lua value except <b>nil</b> and NaN.
  174. (<em>Not a Number</em> is a special floating-point value
  175. used by the IEEE 754 standard to represent
  176. undefined numerical results, such as <code>0/0</code>.)
  177. Tables can be <em>heterogeneous</em>;
  178. that is, they can contain values of all types (except <b>nil</b>).
  179. Any key associated to the value <b>nil</b> is not considered part of the table.
  180. Conversely, any key that is not part of a table has
  181. an associated value <b>nil</b>.
  182. <p>
  183. Tables are the sole data-structuring mechanism in Lua;
  184. they can be used to represent ordinary arrays, lists,
  185. symbol tables, sets, records, graphs, trees, etc.
  186. To represent records, Lua uses the field name as an index.
  187. The language supports this representation by
  188. providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
  189. There are several convenient ways to create tables in Lua
  190. (see <a href="#3.4.9">&sect;3.4.9</a>).
  191. <p>
  192. Like indices,
  193. the values of table fields can be of any type.
  194. In particular,
  195. because functions are first-class values,
  196. table fields can contain functions.
  197. Thus tables can also carry <em>methods</em> (see <a href="#3.4.11">&sect;3.4.11</a>).
  198. <p>
  199. The indexing of tables follows
  200. the definition of raw equality in the language.
  201. The expressions <code>a[i]</code> and <code>a[j]</code>
  202. denote the same table element
  203. if and only if <code>i</code> and <code>j</code> are raw equal
  204. (that is, equal without metamethods).
  205. In particular, floats with integral values
  206. are equal to their respective integers
  207. (e.g., <code>1.0 == 1</code>).
  208. To avoid ambiguities,
  209. any float used as a key that is equal to an integer
  210. is converted to that integer.
  211. For instance, if you write <code>a[2.0] = true</code>,
  212. the actual key inserted into the table will be the integer <code>2</code>.
  213. <p>
  214. Tables, functions, threads, and (full) userdata values are <em>objects</em>:
  215. variables do not actually <em>contain</em> these values,
  216. only <em>references</em> to them.
  217. Assignment, parameter passing, and function returns
  218. always manipulate references to such values;
  219. these operations do not imply any kind of copy.
  220. <p>
  221. The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type
  222. of a given value (see <a href="#pdf-type"><code>type</code></a>).
  223. <h2>2.2 &ndash; <a name="2.2">Environments and the Global Environment</a></h2>
  224. <p>
  225. As we will discuss further in <a href="#3.2">&sect;3.2</a> and <a href="#3.3.3">&sect;3.3.3</a>,
  226. any reference to a free name
  227. (that is, a name not bound to any declaration) <code>var</code>
  228. is syntactically translated to <code>_ENV.var</code>.
  229. Moreover, every chunk is compiled in the scope of
  230. an external local variable named <code>_ENV</code> (see <a href="#3.3.2">&sect;3.3.2</a>),
  231. so <code>_ENV</code> itself is never a free name in a chunk.
  232. <p>
  233. Despite the existence of this external <code>_ENV</code> variable and
  234. the translation of free names,
  235. <code>_ENV</code> is a completely regular name.
  236. In particular,
  237. you can define new variables and parameters with that name.
  238. Each reference to a free name uses the <code>_ENV</code> that is
  239. visible at that point in the program,
  240. following the usual visibility rules of Lua (see <a href="#3.5">&sect;3.5</a>).
  241. <p>
  242. Any table used as the value of <code>_ENV</code> is called an <em>environment</em>.
  243. <p>
  244. Lua keeps a distinguished environment called the <em>global environment</em>.
  245. This value is kept at a special index in the C registry (see <a href="#4.3">&sect;4.3</a>).
  246. In Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value.
  247. (<a href="#pdf-_G"><code>_G</code></a> is never used internally,
  248. so changing its value will affect only your own code.)
  249. <p>
  250. When Lua loads a chunk,
  251. the default value for its <code>_ENV</code> variable
  252. is the global environment (see <a href="#pdf-load"><code>load</code></a>).
  253. Therefore, by default,
  254. free names in Lua code refer to entries in the global environment
  255. and, therefore, they are also called <em>global variables</em>.
  256. Moreover, all standard libraries are loaded in the global environment
  257. and some functions there operate on that environment.
  258. You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>)
  259. to load a chunk with a different environment.
  260. (In C, you have to load the chunk and then change the value
  261. of its first upvalue; see <a href="#lua_setupvalue"><code>lua_setupvalue</code></a>.)
  262. <h2>2.3 &ndash; <a name="2.3">Error Handling</a></h2>
  263. <p>
  264. Several operations in Lua can <em>raise</em> an error.
  265. An error interrupts the normal flow of the program,
  266. which can continue by <em>catching</em> the error.
  267. <p>
  268. Lua code can explicitly raise an error by calling the
  269. <a href="#pdf-error"><code>error</code></a> function.
  270. (This function never returns.)
  271. <p>
  272. To catch errors in Lua,
  273. you can do a <em>protected call</em>,
  274. using <a href="#pdf-pcall"><code>pcall</code></a> (or <a href="#pdf-xpcall"><code>xpcall</code></a>).
  275. The function <a href="#pdf-pcall"><code>pcall</code></a> calls a given function in <em>protected mode</em>.
  276. Any error while running the function stops its execution,
  277. and control returns immediately to <code>pcall</code>,
  278. which returns a status code.
  279. <p>
  280. Because Lua is an embedded extension language,
  281. Lua code starts running by a call
  282. from C&nbsp;code in the host program.
  283. (When you use Lua standalone,
  284. the <code>lua</code> application is the host program.)
  285. Usually, this call is protected;
  286. so, when an otherwise unprotected error occurs during
  287. the compilation or execution of a Lua chunk,
  288. control returns to the host,
  289. which can take appropriate measures,
  290. such as printing an error message.
  291. <p>
  292. Whenever there is an error,
  293. an <em>error object</em>
  294. is propagated with information about the error.
  295. Lua itself only generates errors whose error object is a string,
  296. but programs may generate errors with
  297. any value as the error object.
  298. It is up to the Lua program or its host to handle such error objects.
  299. For historical reasons,
  300. an error object is often called an <em>error message</em>,
  301. even though it does not have to be a string.
  302. <p>
  303. When you use <a href="#pdf-xpcall"><code>xpcall</code></a> (or <a href="#lua_pcall"><code>lua_pcall</code></a>, in C)
  304. you may give a <em>message handler</em>
  305. to be called in case of errors.
  306. This function is called with the original error object
  307. and returns a new error object.
  308. It is called before the error unwinds the stack,
  309. so that it can gather more information about the error,
  310. for instance by inspecting the stack and creating a stack traceback.
  311. This message handler is still protected by the protected call;
  312. so, an error inside the message handler
  313. will call the message handler again.
  314. If this loop goes on for too long,
  315. Lua breaks it and returns an appropriate message.
  316. The message handler is called only for regular runtime errors.
  317. It is not called for memory-allocation errors
  318. nor for errors while running finalizers or other message handlers.
  319. <p>
  320. Lua also offers a system of <em>warnings</em> (see <a href="#pdf-warn"><code>warn</code></a>).
  321. Unlike errors, warnings do not interfere
  322. in any way with program execution.
  323. They typically only generate a message to the user,
  324. although this behavior can be adapted from C (see <a href="#lua_setwarnf"><code>lua_setwarnf</code></a>).
  325. <h2>2.4 &ndash; <a name="2.4">Metatables and Metamethods</a></h2>
  326. <p>
  327. Every value in Lua can have a <em>metatable</em>.
  328. This <em>metatable</em> is an ordinary Lua table
  329. that defines the behavior of the original value
  330. under certain events.
  331. You can change several aspects of the behavior
  332. of a value by setting specific fields in its metatable.
  333. For instance, when a non-numeric value is the operand of an addition,
  334. Lua checks for a function in the field "<code>__add</code>" of the value's metatable.
  335. If it finds one,
  336. Lua calls this function to perform the addition.
  337. <p>
  338. The key for each event in a metatable is a string
  339. with the event name prefixed by two underscores;
  340. the corresponding value is called a <em>metavalue</em>.
  341. For most events, the metavalue must be a function,
  342. which is then called a <em>metamethod</em>.
  343. In the previous example, the key is the string "<code>__add</code>"
  344. and the metamethod is the function that performs the addition.
  345. Unless stated otherwise,
  346. a metamethod may in fact be any callable value,
  347. which is either a function or a value with a <code>__call</code> metamethod.
  348. <p>
  349. You can query the metatable of any value
  350. using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function.
  351. Lua queries metamethods in metatables using a raw access (see <a href="#pdf-rawget"><code>rawget</code></a>).
  352. <p>
  353. You can replace the metatable of tables
  354. using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function.
  355. You cannot change the metatable of other types from Lua code,
  356. except by using the debug library (<a href="#6.10">&sect;6.10</a>).
  357. <p>
  358. Tables and full userdata have individual metatables,
  359. although multiple tables and userdata can share their metatables.
  360. Values of all other types share one single metatable per type;
  361. that is, there is one single metatable for all numbers,
  362. one for all strings, etc.
  363. By default, a value has no metatable,
  364. but the string library sets a metatable for the string type (see <a href="#6.4">&sect;6.4</a>).
  365. <p>
  366. A detailed list of operations controlled by metatables is given next.
  367. Each event is identified by its corresponding key.
  368. By convention, all metatable keys used by Lua are composed by
  369. two underscores followed by lowercase Latin letters.
  370. <ul>
  371. <li><b><code>__add</code>: </b>
  372. the addition (<code>+</code>) operation.
  373. If any operand for an addition is not a number,
  374. Lua will try to call a metamethod.
  375. It starts by checking the first operand (even if it is a number);
  376. if that operand does not define a metamethod for <code>__add</code>,
  377. then Lua will check the second operand.
  378. If Lua can find a metamethod,
  379. it calls the metamethod with the two operands as arguments,
  380. and the result of the call
  381. (adjusted to one value)
  382. is the result of the operation.
  383. Otherwise, if no metamethod is found,
  384. Lua raises an error.
  385. </li>
  386. <li><b><code>__sub</code>: </b>
  387. the subtraction (<code>-</code>) operation.
  388. Behavior similar to the addition operation.
  389. </li>
  390. <li><b><code>__mul</code>: </b>
  391. the multiplication (<code>*</code>) operation.
  392. Behavior similar to the addition operation.
  393. </li>
  394. <li><b><code>__div</code>: </b>
  395. the division (<code>/</code>) operation.
  396. Behavior similar to the addition operation.
  397. </li>
  398. <li><b><code>__mod</code>: </b>
  399. the modulo (<code>%</code>) operation.
  400. Behavior similar to the addition operation.
  401. </li>
  402. <li><b><code>__pow</code>: </b>
  403. the exponentiation (<code>^</code>) operation.
  404. Behavior similar to the addition operation.
  405. </li>
  406. <li><b><code>__unm</code>: </b>
  407. the negation (unary <code>-</code>) operation.
  408. Behavior similar to the addition operation.
  409. </li>
  410. <li><b><code>__idiv</code>: </b>
  411. the floor division (<code>//</code>) operation.
  412. Behavior similar to the addition operation.
  413. </li>
  414. <li><b><code>__band</code>: </b>
  415. the bitwise AND (<code>&amp;</code>) operation.
  416. Behavior similar to the addition operation,
  417. except that Lua will try a metamethod
  418. if any operand is neither an integer
  419. nor a float coercible to an integer (see <a href="#3.4.3">&sect;3.4.3</a>).
  420. </li>
  421. <li><b><code>__bor</code>: </b>
  422. the bitwise OR (<code>|</code>) operation.
  423. Behavior similar to the bitwise AND operation.
  424. </li>
  425. <li><b><code>__bxor</code>: </b>
  426. the bitwise exclusive OR (binary <code>~</code>) operation.
  427. Behavior similar to the bitwise AND operation.
  428. </li>
  429. <li><b><code>__bnot</code>: </b>
  430. the bitwise NOT (unary <code>~</code>) operation.
  431. Behavior similar to the bitwise AND operation.
  432. </li>
  433. <li><b><code>__shl</code>: </b>
  434. the bitwise left shift (<code>&lt;&lt;</code>) operation.
  435. Behavior similar to the bitwise AND operation.
  436. </li>
  437. <li><b><code>__shr</code>: </b>
  438. the bitwise right shift (<code>&gt;&gt;</code>) operation.
  439. Behavior similar to the bitwise AND operation.
  440. </li>
  441. <li><b><code>__concat</code>: </b>
  442. the concatenation (<code>..</code>) operation.
  443. Behavior similar to the addition operation,
  444. except that Lua will try a metamethod
  445. if any operand is neither a string nor a number
  446. (which is always coercible to a string).
  447. </li>
  448. <li><b><code>__len</code>: </b>
  449. the length (<code>#</code>) operation.
  450. If the object is not a string,
  451. Lua will try its metamethod.
  452. If there is a metamethod,
  453. Lua calls it with the object as argument,
  454. and the result of the call
  455. (always adjusted to one value)
  456. is the result of the operation.
  457. If there is no metamethod but the object is a table,
  458. then Lua uses the table length operation (see <a href="#3.4.7">&sect;3.4.7</a>).
  459. Otherwise, Lua raises an error.
  460. </li>
  461. <li><b><code>__eq</code>: </b>
  462. the equal (<code>==</code>) operation.
  463. Behavior similar to the addition operation,
  464. except that Lua will try a metamethod only when the values
  465. being compared are either both tables or both full userdata
  466. and they are not primitively equal.
  467. The result of the call is always converted to a boolean.
  468. </li>
  469. <li><b><code>__lt</code>: </b>
  470. the less than (<code>&lt;</code>) operation.
  471. Behavior similar to the addition operation,
  472. except that Lua will try a metamethod only when the values
  473. being compared are neither both numbers nor both strings.
  474. Moreover, the result of the call is always converted to a boolean.
  475. </li>
  476. <li><b><code>__le</code>: </b>
  477. the less equal (<code>&lt;=</code>) operation.
  478. Behavior similar to the less than operation.
  479. </li>
  480. <li><b><code>__index</code>: </b>
  481. The indexing access operation <code>table[key]</code>.
  482. This event happens when <code>table</code> is not a table or
  483. when <code>key</code> is not present in <code>table</code>.
  484. The metavalue is looked up in the metatable of <code>table</code>.
  485. <p>
  486. The metavalue for this event can be either a function, a table,
  487. or any value with an <code>__index</code> metavalue.
  488. If it is a function,
  489. it is called with <code>table</code> and <code>key</code> as arguments,
  490. and the result of the call
  491. (adjusted to one value)
  492. is the result of the operation.
  493. Otherwise,
  494. the final result is the result of indexing this metavalue with <code>key</code>.
  495. This indexing is regular, not raw,
  496. and therefore can trigger another <code>__index</code> metavalue.
  497. </li>
  498. <li><b><code>__newindex</code>: </b>
  499. The indexing assignment <code>table[key] = value</code>.
  500. Like the index event,
  501. this event happens when <code>table</code> is not a table or
  502. when <code>key</code> is not present in <code>table</code>.
  503. The metavalue is looked up in the metatable of <code>table</code>.
  504. <p>
  505. Like with indexing,
  506. the metavalue for this event can be either a function, a table,
  507. or any value with an <code>__newindex</code> metavalue.
  508. If it is a function,
  509. it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments.
  510. Otherwise,
  511. Lua repeats the indexing assignment over this metavalue
  512. with the same key and value.
  513. This assignment is regular, not raw,
  514. and therefore can trigger another <code>__newindex</code> metavalue.
  515. <p>
  516. Whenever a <code>__newindex</code> metavalue is invoked,
  517. Lua does not perform the primitive assignment.
  518. If needed,
  519. the metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a>
  520. to do the assignment.
  521. </li>
  522. <li><b><code>__call</code>: </b>
  523. The call operation <code>func(args)</code>.
  524. This event happens when Lua tries to call a non-function value
  525. (that is, <code>func</code> is not a function).
  526. The metamethod is looked up in <code>func</code>.
  527. If present,
  528. the metamethod is called with <code>func</code> as its first argument,
  529. followed by the arguments of the original call (<code>args</code>).
  530. All results of the call
  531. are the results of the operation.
  532. This is the only metamethod that allows multiple results.
  533. </li>
  534. </ul>
  535. <p>
  536. In addition to the previous list,
  537. the interpreter also respects the following keys in metatables:
  538. <code>__gc</code> (see <a href="#2.5.3">&sect;2.5.3</a>),
  539. <code>__close</code> (see <a href="#3.3.8">&sect;3.3.8</a>),
  540. <code>__mode</code> (see <a href="#2.5.4">&sect;2.5.4</a>),
  541. and <code>__name</code>.
  542. (The entry <code>__name</code>,
  543. when it contains a string,
  544. may be used by <a href="#pdf-tostring"><code>tostring</code></a> and in error messages.)
  545. <p>
  546. For the unary operators (negation, length, and bitwise NOT),
  547. the metamethod is computed and called with a dummy second operand,
  548. equal to the first one.
  549. This extra operand is only to simplify Lua's internals
  550. (by making these operators behave like a binary operation)
  551. and may be removed in future versions.
  552. For most uses this extra operand is irrelevant.
  553. <p>
  554. Because metatables are regular tables,
  555. they can contain arbitrary fields,
  556. not only the event names defined above.
  557. Some functions in the standard library
  558. (e.g., <a href="#pdf-tostring"><code>tostring</code></a>)
  559. use other fields in metatables for their own purposes.
  560. <p>
  561. It is a good practice to add all needed metamethods to a table
  562. before setting it as a metatable of some object.
  563. In particular, the <code>__gc</code> metamethod works only when this order
  564. is followed (see <a href="#2.5.3">&sect;2.5.3</a>).
  565. It is also a good practice to set the metatable of an object
  566. right after its creation.
  567. <h2>2.5 &ndash; <a name="2.5">Garbage Collection</a></h2>
  568. <p>
  569. Lua performs automatic memory management.
  570. This means that
  571. you do not have to worry about allocating memory for new objects
  572. or freeing it when the objects are no longer needed.
  573. Lua manages memory automatically by running
  574. a <em>garbage collector</em> to collect all <em>dead</em> objects.
  575. All memory used by Lua is subject to automatic management:
  576. strings, tables, userdata, functions, threads, internal structures, etc.
  577. <p>
  578. An object is considered <em>dead</em>
  579. as soon as the collector can be sure the object
  580. will not be accessed again in the normal execution of the program.
  581. ("Normal execution" here excludes finalizers,
  582. which can resurrect dead objects (see <a href="#2.5.3">&sect;2.5.3</a>),
  583. and excludes also operations using the debug library.)
  584. Note that the time when the collector can be sure that an object
  585. is dead may not coincide with the programmer's expectations.
  586. The only guarantees are that Lua will not collect an object
  587. that may still be accessed in the normal execution of the program,
  588. and it will eventually collect an object
  589. that is inaccessible from Lua.
  590. (Here,
  591. <em>inaccessible from Lua</em> means that neither a variable nor
  592. another live object refer to the object.)
  593. Because Lua has no knowledge about C&nbsp;code,
  594. it never collects objects accessible through the registry (see <a href="#4.3">&sect;4.3</a>),
  595. which includes the global environment (see <a href="#2.2">&sect;2.2</a>).
  596. <p>
  597. The garbage collector (GC) in Lua can work in two modes:
  598. incremental and generational.
  599. <p>
  600. The default GC mode with the default parameters
  601. are adequate for most uses.
  602. However, programs that waste a large proportion of their time
  603. allocating and freeing memory can benefit from other settings.
  604. Keep in mind that the GC behavior is non-portable
  605. both across platforms and across different Lua releases;
  606. therefore, optimal settings are also non-portable.
  607. <p>
  608. You can change the GC mode and parameters by calling
  609. <a href="#lua_gc"><code>lua_gc</code></a> in&nbsp;C
  610. or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
  611. You can also use these functions to control
  612. the collector directly (e.g., to stop and restart it).
  613. <h3>2.5.1 &ndash; <a name="2.5.1">Incremental Garbage Collection</a></h3>
  614. <p>
  615. In incremental mode,
  616. each GC cycle performs a mark-and-sweep collection in small steps
  617. interleaved with the program's execution.
  618. In this mode,
  619. the collector uses three numbers to control its garbage-collection cycles:
  620. the <em>garbage-collector pause</em>,
  621. the <em>garbage-collector step multiplier</em>,
  622. and the <em>garbage-collector step size</em>.
  623. <p>
  624. The garbage-collector pause
  625. controls how long the collector waits before starting a new cycle.
  626. The collector starts a new cycle when the use of memory
  627. hits <em>n%</em> of the use after the previous collection.
  628. Larger values make the collector less aggressive.
  629. Values equal to or less than 100 mean the collector will not wait to
  630. start a new cycle.
  631. A value of 200 means that the collector waits for the total memory in use
  632. to double before starting a new cycle.
  633. The default value is 200; the maximum value is 1000.
  634. <p>
  635. The garbage-collector step multiplier
  636. controls the speed of the collector relative to
  637. memory allocation,
  638. that is,
  639. how many elements it marks or sweeps for each
  640. kilobyte of memory allocated.
  641. Larger values make the collector more aggressive but also increase
  642. the size of each incremental step.
  643. You should not use values less than 100,
  644. because they make the collector too slow and
  645. can result in the collector never finishing a cycle.
  646. The default value is 100; the maximum value is 1000.
  647. <p>
  648. The garbage-collector step size controls the
  649. size of each incremental step,
  650. specifically how many bytes the interpreter allocates
  651. before performing a step.
  652. This parameter is logarithmic:
  653. A value of <em>n</em> means the interpreter will allocate <em>2<sup>n</sup></em>
  654. bytes between steps and perform equivalent work during the step.
  655. A large value (e.g., 60) makes the collector a stop-the-world
  656. (non-incremental) collector.
  657. The default value is 13,
  658. which means steps of approximately 8&nbsp;Kbytes.
  659. <h3>2.5.2 &ndash; <a name="2.5.2">Generational Garbage Collection</a></h3>
  660. <p>
  661. In generational mode,
  662. the collector does frequent <em>minor</em> collections,
  663. which traverses only objects recently created.
  664. If after a minor collection the use of memory is still above a limit,
  665. the collector does a stop-the-world <em>major</em> collection,
  666. which traverses all objects.
  667. The generational mode uses two parameters:
  668. the <em>minor multiplier</em> and the <em>the major multiplier</em>.
  669. <p>
  670. The minor multiplier controls the frequency of minor collections.
  671. For a minor multiplier <em>x</em>,
  672. a new minor collection will be done when memory
  673. grows <em>x%</em> larger than the memory in use after the previous major
  674. collection.
  675. For instance, for a multiplier of 20,
  676. the collector will do a minor collection when the use of memory
  677. gets 20% larger than the use after the previous major collection.
  678. The default value is 20; the maximum value is 200.
  679. <p>
  680. The major multiplier controls the frequency of major collections.
  681. For a major multiplier <em>x</em>,
  682. a new major collection will be done when memory
  683. grows <em>x%</em> larger than the memory in use after the previous major
  684. collection.
  685. For instance, for a multiplier of 100,
  686. the collector will do a major collection when the use of memory
  687. gets larger than twice the use after the previous collection.
  688. The default value is 100; the maximum value is 1000.
  689. <h3>2.5.3 &ndash; <a name="2.5.3">Garbage-Collection Metamethods</a></h3>
  690. <p>
  691. You can set garbage-collector metamethods for tables
  692. and, using the C&nbsp;API,
  693. for full userdata (see <a href="#2.4">&sect;2.4</a>).
  694. These metamethods, called <em>finalizers</em>,
  695. are called when the garbage collector detects that the
  696. corresponding table or userdata is dead.
  697. Finalizers allow you to coordinate Lua's garbage collection
  698. with external resource management such as closing files,
  699. network or database connections,
  700. or freeing your own memory.
  701. <p>
  702. For an object (table or userdata) to be finalized when collected,
  703. you must <em>mark</em> it for finalization.
  704. You mark an object for finalization when you set its metatable
  705. and the metatable has a field indexed by the string "<code>__gc</code>".
  706. Note that if you set a metatable without a <code>__gc</code> field
  707. and later create that field in the metatable,
  708. the object will not be marked for finalization.
  709. <p>
  710. When a marked object becomes dead,
  711. it is not collected immediately by the garbage collector.
  712. Instead, Lua puts it in a list.
  713. After the collection,
  714. Lua goes through that list.
  715. For each object in the list,
  716. it checks the object's <code>__gc</code> metamethod:
  717. If it is present,
  718. Lua calls it with the object as its single argument.
  719. <p>
  720. At the end of each garbage-collection cycle,
  721. the finalizers are called in
  722. the reverse order that the objects were marked for finalization,
  723. among those collected in that cycle;
  724. that is, the first finalizer to be called is the one associated
  725. with the object marked last in the program.
  726. The execution of each finalizer may occur at any point during
  727. the execution of the regular code.
  728. <p>
  729. Because the object being collected must still be used by the finalizer,
  730. that object (and other objects accessible only through it)
  731. must be <em>resurrected</em> by Lua.
  732. Usually, this resurrection is transient,
  733. and the object memory is freed in the next garbage-collection cycle.
  734. However, if the finalizer stores the object in some global place
  735. (e.g., a global variable),
  736. then the resurrection is permanent.
  737. Moreover, if the finalizer marks a finalizing object for finalization again,
  738. its finalizer will be called again in the next cycle where the
  739. object is dead.
  740. In any case,
  741. the object memory is freed only in a GC cycle where
  742. the object is dead and not marked for finalization.
  743. <p>
  744. When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
  745. Lua calls the finalizers of all objects marked for finalization,
  746. following the reverse order that they were marked.
  747. If any finalizer marks objects for collection during that phase,
  748. these marks have no effect.
  749. <p>
  750. Finalizers cannot yield.
  751. Except for that, they can do anything,
  752. such as raise errors, create new objects,
  753. or even run the garbage collector.
  754. However, because they can run in unpredictable times,
  755. it is good practice to restrict each finalizer
  756. to the minimum necessary to properly release
  757. its associated resource.
  758. <p>
  759. Any error while running a finalizer generates a warning;
  760. the error is not propagated.
  761. <h3>2.5.4 &ndash; <a name="2.5.4">Weak Tables</a></h3>
  762. <p>
  763. A <em>weak table</em> is a table whose elements are
  764. <em>weak references</em>.
  765. A weak reference is ignored by the garbage collector.
  766. In other words,
  767. if the only references to an object are weak references,
  768. then the garbage collector will collect that object.
  769. <p>
  770. A weak table can have weak keys, weak values, or both.
  771. A table with weak values allows the collection of its values,
  772. but prevents the collection of its keys.
  773. A table with both weak keys and weak values allows the collection of
  774. both keys and values.
  775. In any case, if either the key or the value is collected,
  776. the whole pair is removed from the table.
  777. The weakness of a table is controlled by the
  778. <code>__mode</code> field of its metatable.
  779. This metavalue, if present, must be one of the following strings:
  780. "<code>k</code>", for a table with weak keys;
  781. "<code>v</code>", for a table with weak values;
  782. or "<code>kv</code>", for a table with both weak keys and values.
  783. <p>
  784. A table with weak keys and strong values
  785. is also called an <em>ephemeron table</em>.
  786. In an ephemeron table,
  787. a value is considered reachable only if its key is reachable.
  788. In particular,
  789. if the only reference to a key comes through its value,
  790. the pair is removed.
  791. <p>
  792. Any change in the weakness of a table may take effect only
  793. at the next collect cycle.
  794. In particular, if you change the weakness to a stronger mode,
  795. Lua may still collect some items from that table
  796. before the change takes effect.
  797. <p>
  798. Only objects that have an explicit construction
  799. are removed from weak tables.
  800. Values, such as numbers and light C&nbsp;functions,
  801. are not subject to garbage collection,
  802. and therefore are not removed from weak tables
  803. (unless their associated values are collected).
  804. Although strings are subject to garbage collection,
  805. they do not have an explicit construction and
  806. their equality is by value;
  807. they behave more like values than like objects.
  808. Therefore, they are not removed from weak tables.
  809. <p>
  810. Resurrected objects
  811. (that is, objects being finalized
  812. and objects accessible only through objects being finalized)
  813. have a special behavior in weak tables.
  814. They are removed from weak values before running their finalizers,
  815. but are removed from weak keys only in the next collection
  816. after running their finalizers, when such objects are actually freed.
  817. This behavior allows the finalizer to access properties
  818. associated with the object through weak tables.
  819. <p>
  820. If a weak table is among the resurrected objects in a collection cycle,
  821. it may not be properly cleared until the next cycle.
  822. <h2>2.6 &ndash; <a name="2.6">Coroutines</a></h2>
  823. <p>
  824. Lua supports coroutines,
  825. also called <em>collaborative multithreading</em>.
  826. A coroutine in Lua represents an independent thread of execution.
  827. Unlike threads in multithread systems, however,
  828. a coroutine only suspends its execution by explicitly calling
  829. a yield function.
  830. <p>
  831. You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>.
  832. Its sole argument is a function
  833. that is the main function of the coroutine.
  834. The <code>create</code> function only creates a new coroutine and
  835. returns a handle to it (an object of type <em>thread</em>);
  836. it does not start the coroutine.
  837. <p>
  838. You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  839. When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
  840. passing as its first argument
  841. a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
  842. the coroutine starts its execution by
  843. calling its main function.
  844. Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed
  845. as arguments to that function.
  846. After the coroutine starts running,
  847. it runs until it terminates or <em>yields</em>.
  848. <p>
  849. A coroutine can terminate its execution in two ways:
  850. normally, when its main function returns
  851. (explicitly or implicitly, after the last instruction);
  852. and abnormally, if there is an unprotected error.
  853. In case of normal termination,
  854. <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>,
  855. plus any values returned by the coroutine main function.
  856. In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b>
  857. plus the error object.
  858. In this case, the coroutine does not unwind its stack,
  859. so that it is possible to inspect it after the error
  860. with the debug API.
  861. <p>
  862. A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
  863. When a coroutine yields,
  864. the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately,
  865. even if the yield happens inside nested function calls
  866. (that is, not in the main function,
  867. but in a function directly or indirectly called by the main function).
  868. In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>,
  869. plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
  870. The next time you resume the same coroutine,
  871. it continues its execution from the point where it yielded,
  872. with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra
  873. arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  874. <p>
  875. Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
  876. the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine,
  877. but instead of returning the coroutine itself,
  878. it returns a function that, when called, resumes the coroutine.
  879. Any arguments passed to this function
  880. go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  881. <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>,
  882. except the first one (the boolean error code).
  883. Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
  884. the function created by <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a>
  885. propagates any error to the caller.
  886. In this case,
  887. the function also closes the coroutine (see <a href="#pdf-coroutine.close"><code>coroutine.close</code></a>).
  888. <p>
  889. As an example of how coroutines work,
  890. consider the following code:
  891. <pre>
  892. function foo (a)
  893. print("foo", a)
  894. return coroutine.yield(2*a)
  895. end
  896. co = coroutine.create(function (a,b)
  897. print("co-body", a, b)
  898. local r = foo(a+1)
  899. print("co-body", r)
  900. local r, s = coroutine.yield(a+b, a-b)
  901. print("co-body", r, s)
  902. return b, "end"
  903. end)
  904. print("main", coroutine.resume(co, 1, 10))
  905. print("main", coroutine.resume(co, "r"))
  906. print("main", coroutine.resume(co, "x", "y"))
  907. print("main", coroutine.resume(co, "x", "y"))
  908. </pre><p>
  909. When you run it, it produces the following output:
  910. <pre>
  911. co-body 1 10
  912. foo 2
  913. main true 4
  914. co-body r
  915. main true 11 -9
  916. co-body x y
  917. main true 10 end
  918. main false cannot resume dead coroutine
  919. </pre>
  920. <p>
  921. You can also create and manipulate coroutines through the C API:
  922. see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>,
  923. and <a href="#lua_yield"><code>lua_yield</code></a>.
  924. <h1>3 &ndash; <a name="3">The Language</a></h1>
  925. <p>
  926. This section describes the lexis, the syntax, and the semantics of Lua.
  927. In other words,
  928. this section describes
  929. which tokens are valid,
  930. how they can be combined,
  931. and what their combinations mean.
  932. <p>
  933. Language constructs will be explained using the usual extended BNF notation,
  934. in which
  935. {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and
  936. [<em>a</em>]&nbsp;means an optional <em>a</em>.
  937. Non-terminals are shown like non-terminal,
  938. keywords are shown like <b>kword</b>,
  939. and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;.
  940. The complete syntax of Lua can be found in <a href="#9">&sect;9</a>
  941. at the end of this manual.
  942. <h2>3.1 &ndash; <a name="3.1">Lexical Conventions</a></h2>
  943. <p>
  944. Lua is a free-form language.
  945. It ignores spaces and comments between lexical elements (tokens),
  946. except as delimiters between two tokens.
  947. In source code,
  948. Lua recognizes as spaces the standard ASCII whitespace
  949. characters space, form feed, newline,
  950. carriage return, horizontal tab, and vertical tab.
  951. <p>
  952. <em>Names</em>
  953. (also called <em>identifiers</em>)
  954. in Lua can be any string of Latin letters,
  955. Arabic-Indic digits, and underscores,
  956. not beginning with a digit and
  957. not being a reserved word.
  958. Identifiers are used to name variables, table fields, and labels.
  959. <p>
  960. The following <em>keywords</em> are reserved
  961. and cannot be used as names:
  962. <pre>
  963. and break do else elseif end
  964. false for function goto if in
  965. local nil not or repeat return
  966. then true until while
  967. </pre>
  968. <p>
  969. Lua is a case-sensitive language:
  970. <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
  971. are two different, valid names.
  972. As a convention,
  973. programs should avoid creating
  974. names that start with an underscore followed by
  975. one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>).
  976. <p>
  977. The following strings denote other tokens:
  978. <pre>
  979. + - * / % ^ #
  980. &amp; ~ | &lt;&lt; &gt;&gt; //
  981. == ~= &lt;= &gt;= &lt; &gt; =
  982. ( ) { } [ ] ::
  983. ; : , . .. ...
  984. </pre>
  985. <p>
  986. A <em>short literal string</em>
  987. can be delimited by matching single or double quotes,
  988. and can contain the following C-like escape sequences:
  989. '<code>\a</code>' (bell),
  990. '<code>\b</code>' (backspace),
  991. '<code>\f</code>' (form feed),
  992. '<code>\n</code>' (newline),
  993. '<code>\r</code>' (carriage return),
  994. '<code>\t</code>' (horizontal tab),
  995. '<code>\v</code>' (vertical tab),
  996. '<code>\\</code>' (backslash),
  997. '<code>\"</code>' (quotation mark [double quote]),
  998. and '<code>\'</code>' (apostrophe [single quote]).
  999. A backslash followed by a line break
  1000. results in a newline in the string.
  1001. The escape sequence '<code>\z</code>' skips the following span
  1002. of whitespace characters,
  1003. including line breaks;
  1004. it is particularly useful to break and indent a long literal string
  1005. into multiple lines without adding the newlines and spaces
  1006. into the string contents.
  1007. A short literal string cannot contain unescaped line breaks
  1008. nor escapes not forming a valid escape sequence.
  1009. <p>
  1010. We can specify any byte in a short literal string,
  1011. including embedded zeros,
  1012. by its numeric value.
  1013. This can be done
  1014. with the escape sequence <code>\x<em>XX</em></code>,
  1015. where <em>XX</em> is a sequence of exactly two hexadecimal digits,
  1016. or with the escape sequence <code>\<em>ddd</em></code>,
  1017. where <em>ddd</em> is a sequence of up to three decimal digits.
  1018. (Note that if a decimal escape sequence is to be followed by a digit,
  1019. it must be expressed using exactly three digits.)
  1020. <p>
  1021. The UTF-8 encoding of a Unicode character
  1022. can be inserted in a literal string with
  1023. the escape sequence <code>\u{<em>XXX</em>}</code>
  1024. (with mandatory enclosing braces),
  1025. where <em>XXX</em> is a sequence of one or more hexadecimal digits
  1026. representing the character code point.
  1027. This code point can be any value less than <em>2<sup>31</sup></em>.
  1028. (Lua uses the original UTF-8 specification here,
  1029. which is not restricted to valid Unicode code points.)
  1030. <p>
  1031. Literal strings can also be defined using a long format
  1032. enclosed by <em>long brackets</em>.
  1033. We define an <em>opening long bracket of level <em>n</em></em> as an opening
  1034. square bracket followed by <em>n</em> equal signs followed by another
  1035. opening square bracket.
  1036. So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,
  1037. an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,
  1038. and so on.
  1039. A <em>closing long bracket</em> is defined similarly;
  1040. for instance,
  1041. a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.
  1042. A <em>long literal</em> starts with an opening long bracket of any level and
  1043. ends at the first closing long bracket of the same level.
  1044. It can contain any text except a closing bracket of the same level.
  1045. Literals in this bracketed form can run for several lines,
  1046. do not interpret any escape sequences,
  1047. and ignore long brackets of any other level.
  1048. Any kind of end-of-line sequence
  1049. (carriage return, newline, carriage return followed by newline,
  1050. or newline followed by carriage return)
  1051. is converted to a simple newline.
  1052. When the opening long bracket is immediately followed by a newline,
  1053. the newline is not included in the string.
  1054. <p>
  1055. As an example, in a system using ASCII
  1056. (in which '<code>a</code>' is coded as&nbsp;97,
  1057. newline is coded as&nbsp;10, and '<code>1</code>' is coded as&nbsp;49),
  1058. the five literal strings below denote the same string:
  1059. <pre>
  1060. a = 'alo\n123"'
  1061. a = "alo\n123\""
  1062. a = '\97lo\10\04923"'
  1063. a = [[alo
  1064. 123"]]
  1065. a = [==[
  1066. alo
  1067. 123"]==]
  1068. </pre>
  1069. <p>
  1070. Any byte in a literal string not
  1071. explicitly affected by the previous rules represents itself.
  1072. However, Lua opens files for parsing in text mode,
  1073. and the system's file functions may have problems with
  1074. some control characters.
  1075. So, it is safer to represent
  1076. binary data as a quoted literal with
  1077. explicit escape sequences for the non-text characters.
  1078. <p>
  1079. A <em>numeric constant</em> (or <em>numeral</em>)
  1080. can be written with an optional fractional part
  1081. and an optional decimal exponent,
  1082. marked by a letter '<code>e</code>' or '<code>E</code>'.
  1083. Lua also accepts hexadecimal constants,
  1084. which start with <code>0x</code> or <code>0X</code>.
  1085. Hexadecimal constants also accept an optional fractional part
  1086. plus an optional binary exponent,
  1087. marked by a letter '<code>p</code>' or '<code>P</code>'.
  1088. <p>
  1089. A numeric constant with a radix point or an exponent
  1090. denotes a float;
  1091. otherwise,
  1092. if its value fits in an integer or it is a hexadecimal constant,
  1093. it denotes an integer;
  1094. otherwise (that is, a decimal integer numeral that overflows),
  1095. it denotes a float.
  1096. Hexadecimal numerals with neither a radix point nor an exponent
  1097. always denote an integer value;
  1098. if the value overflows, it <em>wraps around</em>
  1099. to fit into a valid integer.
  1100. <p>
  1101. Examples of valid integer constants are
  1102. <pre>
  1103. 3 345 0xff 0xBEBADA
  1104. </pre><p>
  1105. Examples of valid float constants are
  1106. <pre>
  1107. 3.0 3.1416 314.16e-2 0.31416E1 34e1
  1108. 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
  1109. </pre>
  1110. <p>
  1111. A <em>comment</em> starts with a double hyphen (<code>--</code>)
  1112. anywhere outside a string.
  1113. If the text immediately after <code>--</code> is not an opening long bracket,
  1114. the comment is a <em>short comment</em>,
  1115. which runs until the end of the line.
  1116. Otherwise, it is a <em>long comment</em>,
  1117. which runs until the corresponding closing long bracket.
  1118. <h2>3.2 &ndash; <a name="3.2">Variables</a></h2>
  1119. <p>
  1120. Variables are places that store values.
  1121. There are three kinds of variables in Lua:
  1122. global variables, local variables, and table fields.
  1123. <p>
  1124. A single name can denote a global variable or a local variable
  1125. (or a function's formal parameter,
  1126. which is a particular kind of local variable):
  1127. <pre>
  1128. var ::= Name
  1129. </pre><p>
  1130. Name denotes identifiers (see <a href="#3.1">&sect;3.1</a>).
  1131. <p>
  1132. Any variable name is assumed to be global unless explicitly declared
  1133. as a local (see <a href="#3.3.7">&sect;3.3.7</a>).
  1134. Local variables are <em>lexically scoped</em>:
  1135. local variables can be freely accessed by functions
  1136. defined inside their scope (see <a href="#3.5">&sect;3.5</a>).
  1137. <p>
  1138. Before the first assignment to a variable, its value is <b>nil</b>.
  1139. <p>
  1140. Square brackets are used to index a table:
  1141. <pre>
  1142. var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo;
  1143. </pre><p>
  1144. The meaning of accesses to table fields can be changed via metatables
  1145. (see <a href="#2.4">&sect;2.4</a>).
  1146. <p>
  1147. The syntax <code>var.Name</code> is just syntactic sugar for
  1148. <code>var["Name"]</code>:
  1149. <pre>
  1150. var ::= prefixexp &lsquo;<b>.</b>&rsquo; Name
  1151. </pre>
  1152. <p>
  1153. An access to a global variable <code>x</code>
  1154. is equivalent to <code>_ENV.x</code>.
  1155. Due to the way that chunks are compiled,
  1156. the variable <code>_ENV</code> itself is never global (see <a href="#2.2">&sect;2.2</a>).
  1157. <h2>3.3 &ndash; <a name="3.3">Statements</a></h2>
  1158. <p>
  1159. Lua supports an almost conventional set of statements,
  1160. similar to those in other conventional languages.
  1161. This set includes
  1162. blocks, assignments, control structures, function calls,
  1163. and variable declarations.
  1164. <h3>3.3.1 &ndash; <a name="3.3.1">Blocks</a></h3>
  1165. <p>
  1166. A block is a list of statements,
  1167. which are executed sequentially:
  1168. <pre>
  1169. block ::= {stat}
  1170. </pre><p>
  1171. Lua has <em>empty statements</em>
  1172. that allow you to separate statements with semicolons,
  1173. start a block with a semicolon
  1174. or write two semicolons in sequence:
  1175. <pre>
  1176. stat ::= &lsquo;<b>;</b>&rsquo;
  1177. </pre>
  1178. <p>
  1179. Both function calls and assignments
  1180. can start with an open parenthesis.
  1181. This possibility leads to an ambiguity in Lua's grammar.
  1182. Consider the following fragment:
  1183. <pre>
  1184. a = b + c
  1185. (print or io.write)('done')
  1186. </pre><p>
  1187. The grammar could see this fragment in two ways:
  1188. <pre>
  1189. a = b + c(print or io.write)('done')
  1190. a = b + c; (print or io.write)('done')
  1191. </pre><p>
  1192. The current parser always sees such constructions
  1193. in the first way,
  1194. interpreting the open parenthesis
  1195. as the start of the arguments to a call.
  1196. To avoid this ambiguity,
  1197. it is a good practice to always precede with a semicolon
  1198. statements that start with a parenthesis:
  1199. <pre>
  1200. ;(print or io.write)('done')
  1201. </pre>
  1202. <p>
  1203. A block can be explicitly delimited to produce a single statement:
  1204. <pre>
  1205. stat ::= <b>do</b> block <b>end</b>
  1206. </pre><p>
  1207. Explicit blocks are useful
  1208. to control the scope of variable declarations.
  1209. Explicit blocks are also sometimes used to
  1210. add a <b>return</b> statement in the middle
  1211. of another block (see <a href="#3.3.4">&sect;3.3.4</a>).
  1212. <h3>3.3.2 &ndash; <a name="3.3.2">Chunks</a></h3>
  1213. <p>
  1214. The unit of compilation of Lua is called a <em>chunk</em>.
  1215. Syntactically,
  1216. a chunk is simply a block:
  1217. <pre>
  1218. chunk ::= block
  1219. </pre>
  1220. <p>
  1221. Lua handles a chunk as the body of an anonymous function
  1222. with a variable number of arguments
  1223. (see <a href="#3.4.11">&sect;3.4.11</a>).
  1224. As such, chunks can define local variables,
  1225. receive arguments, and return values.
  1226. Moreover, such anonymous function is compiled as in the
  1227. scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">&sect;2.2</a>).
  1228. The resulting function always has <code>_ENV</code> as its only external variable,
  1229. even if it does not use that variable.
  1230. <p>
  1231. A chunk can be stored in a file or in a string inside the host program.
  1232. To execute a chunk,
  1233. Lua first <em>loads</em> it,
  1234. precompiling the chunk's code into instructions for a virtual machine,
  1235. and then Lua executes the compiled code
  1236. with an interpreter for the virtual machine.
  1237. <p>
  1238. Chunks can also be precompiled into binary form;
  1239. see the program <code>luac</code> and the function <a href="#pdf-string.dump"><code>string.dump</code></a> for details.
  1240. Programs in source and compiled forms are interchangeable;
  1241. Lua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>).
  1242. <h3>3.3.3 &ndash; <a name="3.3.3">Assignment</a></h3>
  1243. <p>
  1244. Lua allows multiple assignments.
  1245. Therefore, the syntax for assignment
  1246. defines a list of variables on the left side
  1247. and a list of expressions on the right side.
  1248. The elements in both lists are separated by commas:
  1249. <pre>
  1250. stat ::= varlist &lsquo;<b>=</b>&rsquo; explist
  1251. varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
  1252. explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
  1253. </pre><p>
  1254. Expressions are discussed in <a href="#3.4">&sect;3.4</a>.
  1255. <p>
  1256. Before the assignment,
  1257. the list of values is <em>adjusted</em> to the length of
  1258. the list of variables.
  1259. If there are more values than needed,
  1260. the excess values are thrown away.
  1261. If there are fewer values than needed,
  1262. the list is extended with <b>nil</b>'s.
  1263. If the list of expressions ends with a function call,
  1264. then all values returned by that call enter the list of values,
  1265. before the adjustment
  1266. (except when the call is enclosed in parentheses; see <a href="#3.4">&sect;3.4</a>).
  1267. <p>
  1268. The assignment statement first evaluates all its expressions
  1269. and only then the assignments are performed.
  1270. Thus the code
  1271. <pre>
  1272. i = 3
  1273. i, a[i] = i+1, 20
  1274. </pre><p>
  1275. sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
  1276. because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
  1277. before it is assigned&nbsp;4.
  1278. Similarly, the line
  1279. <pre>
  1280. x, y = y, x
  1281. </pre><p>
  1282. exchanges the values of <code>x</code> and <code>y</code>,
  1283. and
  1284. <pre>
  1285. x, y, z = y, z, x
  1286. </pre><p>
  1287. cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
  1288. <p>
  1289. An assignment to a global name <code>x = val</code>
  1290. is equivalent to the assignment
  1291. <code>_ENV.x = val</code> (see <a href="#2.2">&sect;2.2</a>).
  1292. <p>
  1293. The meaning of assignments to table fields and
  1294. global variables (which are actually table fields, too)
  1295. can be changed via metatables (see <a href="#2.4">&sect;2.4</a>).
  1296. <h3>3.3.4 &ndash; <a name="3.3.4">Control Structures</a></h3><p>
  1297. The control structures
  1298. <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
  1299. familiar syntax:
  1300. <pre>
  1301. stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
  1302. stat ::= <b>repeat</b> block <b>until</b> exp
  1303. stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
  1304. </pre><p>
  1305. Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">&sect;3.3.5</a>).
  1306. <p>
  1307. The condition expression of a
  1308. control structure can return any value.
  1309. Both <b>false</b> and <b>nil</b> test false.
  1310. All values different from <b>nil</b> and <b>false</b> test true.
  1311. In particular, the number 0 and the empty string also test true.
  1312. <p>
  1313. In the <b>repeat</b>&ndash;<b>until</b> loop,
  1314. the inner block does not end at the <b>until</b> keyword,
  1315. but only after the condition.
  1316. So, the condition can refer to local variables
  1317. declared inside the loop block.
  1318. <p>
  1319. The <b>goto</b> statement transfers the program control to a label.
  1320. For syntactical reasons,
  1321. labels in Lua are considered statements too:
  1322. <pre>
  1323. stat ::= <b>goto</b> Name
  1324. stat ::= label
  1325. label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
  1326. </pre>
  1327. <p>
  1328. A label is visible in the entire block where it is defined,
  1329. except inside nested functions.
  1330. A goto may jump to any visible label as long as it does not
  1331. enter into the scope of a local variable.
  1332. A label should not be declared
  1333. where a label with the same name is visible,
  1334. even if this other label has been declared in an enclosing block.
  1335. <p>
  1336. Labels and empty statements are called <em>void statements</em>,
  1337. as they perform no actions.
  1338. <p>
  1339. The <b>break</b> statement terminates the execution of a
  1340. <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
  1341. skipping to the next statement after the loop:
  1342. <pre>
  1343. stat ::= <b>break</b>
  1344. </pre><p>
  1345. A <b>break</b> ends the innermost enclosing loop.
  1346. <p>
  1347. The <b>return</b> statement is used to return values
  1348. from a function or a chunk
  1349. (which is handled as an anonymous function).
  1350. Functions can return more than one value,
  1351. so the syntax for the <b>return</b> statement is
  1352. <pre>
  1353. stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
  1354. </pre>
  1355. <p>
  1356. The <b>return</b> statement can only be written
  1357. as the last statement of a block.
  1358. If it is necessary to <b>return</b> in the middle of a block,
  1359. then an explicit inner block can be used,
  1360. as in the idiom <code>do return end</code>,
  1361. because now <b>return</b> is the last statement in its (inner) block.
  1362. <h3>3.3.5 &ndash; <a name="3.3.5">For Statement</a></h3>
  1363. <p>
  1364. The <b>for</b> statement has two forms:
  1365. one numerical and one generic.
  1366. <h4>The numerical <b>for</b> loop</h4>
  1367. <p>
  1368. The numerical <b>for</b> loop repeats a block of code while a
  1369. control variable goes through an arithmetic progression.
  1370. It has the following syntax:
  1371. <pre>
  1372. stat ::= <b>for</b> Name &lsquo;<b>=</b>&rsquo; exp &lsquo;<b>,</b>&rsquo; exp [&lsquo;<b>,</b>&rsquo; exp] <b>do</b> block <b>end</b>
  1373. </pre><p>
  1374. The given identifier (Name) defines the control variable,
  1375. which is a new variable local to the loop body (<em>block</em>).
  1376. <p>
  1377. The loop starts by evaluating once the three control expressions.
  1378. Their values are called respectively
  1379. the <em>initial value</em>, the <em>limit</em>, and the <em>step</em>.
  1380. If the step is absent, it defaults to&nbsp;1.
  1381. <p>
  1382. If both the initial value and the step are integers,
  1383. the loop is done with integers;
  1384. note that the limit may not be an integer.
  1385. Otherwise, the three values are converted to
  1386. floats and the loop is done with floats.
  1387. Beware of floating-point accuracy in this case.
  1388. <p>
  1389. After that initialization,
  1390. the loop body is repeated with the value of the control variable
  1391. going through an arithmetic progression,
  1392. starting at the initial value,
  1393. with a common difference given by the step.
  1394. A negative step makes a decreasing sequence;
  1395. a step equal to zero raises an error.
  1396. The loop continues while the value is less than
  1397. or equal to the limit
  1398. (greater than or equal to for a negative step).
  1399. If the initial value is already greater than the limit
  1400. (or less than, if the step is negative),
  1401. the body is not executed.
  1402. <p>
  1403. For integer loops,
  1404. the control variable never wraps around;
  1405. instead, the loop ends in case of an overflow.
  1406. <p>
  1407. You should not change the value of the control variable
  1408. during the loop.
  1409. If you need its value after the loop,
  1410. assign it to another variable before exiting the loop.
  1411. <h4>The generic <b>for</b> loop</h4>
  1412. <p>
  1413. The generic <b>for</b> statement works over functions,
  1414. called <em>iterators</em>.
  1415. On each iteration, the iterator function is called to produce a new value,
  1416. stopping when this new value is <b>nil</b>.
  1417. The generic <b>for</b> loop has the following syntax:
  1418. <pre>
  1419. stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
  1420. namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
  1421. </pre><p>
  1422. A <b>for</b> statement like
  1423. <pre>
  1424. for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>explist</em> do <em>body</em> end
  1425. </pre><p>
  1426. works as follows.
  1427. <p>
  1428. The names <em>var_i</em> declare loop variables local to the loop body.
  1429. The first of these variables is the <em>control variable</em>.
  1430. <p>
  1431. The loop starts by evaluating <em>explist</em>
  1432. to produce four values:
  1433. an <em>iterator function</em>,
  1434. a <em>state</em>,
  1435. an initial value for the control variable,
  1436. and a <em>closing value</em>.
  1437. <p>
  1438. Then, at each iteration,
  1439. Lua calls the iterator function with two arguments:
  1440. the state and the control variable.
  1441. The results from this call are then assigned to the loop variables,
  1442. following the rules of multiple assignments (see <a href="#3.3.3">&sect;3.3.3</a>).
  1443. If the control variable becomes <b>nil</b>,
  1444. the loop terminates.
  1445. Otherwise, the body is executed and the loop goes
  1446. to the next iteration.
  1447. <p>
  1448. The closing value behaves like a
  1449. to-be-closed variable (see <a href="#3.3.8">&sect;3.3.8</a>),
  1450. which can be used to release resources when the loop ends.
  1451. Otherwise, it does not interfere with the loop.
  1452. <p>
  1453. You should not change the value of the control variable
  1454. during the loop.
  1455. <h3>3.3.6 &ndash; <a name="3.3.6">Function Calls as Statements</a></h3><p>
  1456. To allow possible side-effects,
  1457. function calls can be executed as statements:
  1458. <pre>
  1459. stat ::= functioncall
  1460. </pre><p>
  1461. In this case, all returned values are thrown away.
  1462. Function calls are explained in <a href="#3.4.10">&sect;3.4.10</a>.
  1463. <h3>3.3.7 &ndash; <a name="3.3.7">Local Declarations</a></h3><p>
  1464. Local variables can be declared anywhere inside a block.
  1465. The declaration can include an initialization:
  1466. <pre>
  1467. stat ::= <b>local</b> attnamelist [&lsquo;<b>=</b>&rsquo; explist]
  1468. attnamelist ::= Name attrib {&lsquo;<b>,</b>&rsquo; Name attrib}
  1469. </pre><p>
  1470. If present, an initial assignment has the same semantics
  1471. of a multiple assignment (see <a href="#3.3.3">&sect;3.3.3</a>).
  1472. Otherwise, all variables are initialized with <b>nil</b>.
  1473. <p>
  1474. Each variable name may be postfixed by an attribute
  1475. (a name between angle brackets):
  1476. <pre>
  1477. attrib ::= [&lsquo;<b>&lt;</b>&rsquo; Name &lsquo;<b>&gt;</b>&rsquo;]
  1478. </pre><p>
  1479. There are two possible attributes:
  1480. <code>const</code>, which declares a constant variable,
  1481. that is, a variable that cannot be assigned to
  1482. after its initialization;
  1483. and <code>close</code>, which declares a to-be-closed variable (see <a href="#3.3.8">&sect;3.3.8</a>).
  1484. A list of variables can contain at most one to-be-closed variable.
  1485. <p>
  1486. A chunk is also a block (see <a href="#3.3.2">&sect;3.3.2</a>),
  1487. and so local variables can be declared in a chunk outside any explicit block.
  1488. <p>
  1489. The visibility rules for local variables are explained in <a href="#3.5">&sect;3.5</a>.
  1490. <h3>3.3.8 &ndash; <a name="3.3.8">To-be-closed Variables</a></h3>
  1491. <p>
  1492. A to-be-closed variable behaves like a constant local variable,
  1493. except that its value is <em>closed</em> whenever the variable
  1494. goes out of scope, including normal block termination,
  1495. exiting its block by <b>break</b>/<b>goto</b>/<b>return</b>,
  1496. or exiting by an error.
  1497. <p>
  1498. Here, to <em>close</em> a value means
  1499. to call its <code>__close</code> metamethod.
  1500. When calling the metamethod,
  1501. the value itself is passed as the first argument
  1502. and the error object that caused the exit (if any)
  1503. is passed as a second argument;
  1504. if there was no error, the second argument is <b>nil</b>.
  1505. <p>
  1506. The value assigned to a to-be-closed variable
  1507. must have a <code>__close</code> metamethod
  1508. or be a false value.
  1509. (<b>nil</b> and <b>false</b> are ignored as to-be-closed values.)
  1510. <p>
  1511. If several to-be-closed variables go out of scope at the same event,
  1512. they are closed in the reverse order that they were declared.
  1513. <p>
  1514. If there is any error while running a closing method,
  1515. that error is handled like an error in the regular code
  1516. where the variable was defined.
  1517. However, Lua may call the method one more time.
  1518. <p>
  1519. After an error,
  1520. the other pending closing methods will still be called.
  1521. Errors in these methods
  1522. interrupt the respective method and generate a warning,
  1523. but are otherwise ignored;
  1524. the error reported is only the original one.
  1525. <p>
  1526. If a coroutine yields and is never resumed again,
  1527. some variables may never go out of scope,
  1528. and therefore they will never be closed.
  1529. (These variables are the ones created inside the coroutine
  1530. and in scope at the point where the coroutine yielded.)
  1531. Similarly, if a coroutine ends with an error,
  1532. it does not unwind its stack,
  1533. so it does not close any variable.
  1534. In both cases,
  1535. you can either use finalizers
  1536. or call <a href="#pdf-coroutine.close"><code>coroutine.close</code></a> to close the variables.
  1537. However, if the coroutine was created
  1538. through <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a>,
  1539. then its corresponding function will close the coroutine
  1540. in case of errors.
  1541. <h2>3.4 &ndash; <a name="3.4">Expressions</a></h2>
  1542. <p>
  1543. The basic expressions in Lua are the following:
  1544. <pre>
  1545. exp ::= prefixexp
  1546. exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
  1547. exp ::= Numeral
  1548. exp ::= LiteralString
  1549. exp ::= functiondef
  1550. exp ::= tableconstructor
  1551. exp ::= &lsquo;<b>...</b>&rsquo;
  1552. exp ::= exp binop exp
  1553. exp ::= unop exp
  1554. prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
  1555. </pre>
  1556. <p>
  1557. Numerals and literal strings are explained in <a href="#3.1">&sect;3.1</a>;
  1558. variables are explained in <a href="#3.2">&sect;3.2</a>;
  1559. function definitions are explained in <a href="#3.4.11">&sect;3.4.11</a>;
  1560. function calls are explained in <a href="#3.4.10">&sect;3.4.10</a>;
  1561. table constructors are explained in <a href="#3.4.9">&sect;3.4.9</a>.
  1562. Vararg expressions,
  1563. denoted by three dots ('<code>...</code>'), can only be used when
  1564. directly inside a vararg function;
  1565. they are explained in <a href="#3.4.11">&sect;3.4.11</a>.
  1566. <p>
  1567. Binary operators comprise arithmetic operators (see <a href="#3.4.1">&sect;3.4.1</a>),
  1568. bitwise operators (see <a href="#3.4.2">&sect;3.4.2</a>),
  1569. relational operators (see <a href="#3.4.4">&sect;3.4.4</a>), logical operators (see <a href="#3.4.5">&sect;3.4.5</a>),
  1570. and the concatenation operator (see <a href="#3.4.6">&sect;3.4.6</a>).
  1571. Unary operators comprise the unary minus (see <a href="#3.4.1">&sect;3.4.1</a>),
  1572. the unary bitwise NOT (see <a href="#3.4.2">&sect;3.4.2</a>),
  1573. the unary logical <b>not</b> (see <a href="#3.4.5">&sect;3.4.5</a>),
  1574. and the unary <em>length operator</em> (see <a href="#3.4.7">&sect;3.4.7</a>).
  1575. <p>
  1576. Both function calls and vararg expressions can result in multiple values.
  1577. If a function call is used as a statement (see <a href="#3.3.6">&sect;3.3.6</a>),
  1578. then its return list is adjusted to zero elements,
  1579. thus discarding all returned values.
  1580. If an expression is used as the last (or the only) element
  1581. of a list of expressions,
  1582. then no adjustment is made
  1583. (unless the expression is enclosed in parentheses).
  1584. In all other contexts,
  1585. Lua adjusts the result list to one element,
  1586. either discarding all values except the first one
  1587. or adding a single <b>nil</b> if there are no values.
  1588. <p>
  1589. Here are some examples:
  1590. <pre>
  1591. f() -- adjusted to 0 results
  1592. g(f(), x) -- f() is adjusted to 1 result
  1593. g(x, f()) -- g gets x plus all results from f()
  1594. a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
  1595. a,b = ... -- a gets the first vararg argument, b gets
  1596. -- the second (both a and b can get nil if there
  1597. -- is no corresponding vararg argument)
  1598. a,b,c = x, f() -- f() is adjusted to 2 results
  1599. a,b,c = f() -- f() is adjusted to 3 results
  1600. return f() -- returns all results from f()
  1601. return ... -- returns all received vararg arguments
  1602. return x,y,f() -- returns x, y, and all results from f()
  1603. {f()} -- creates a list with all results from f()
  1604. {...} -- creates a list with all vararg arguments
  1605. {f(), nil} -- f() is adjusted to 1 result
  1606. </pre>
  1607. <p>
  1608. Any expression enclosed in parentheses always results in only one value.
  1609. Thus,
  1610. <code>(f(x,y,z))</code> is always a single value,
  1611. even if <code>f</code> returns several values.
  1612. (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
  1613. or <b>nil</b> if <code>f</code> does not return any values.)
  1614. <h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>
  1615. Lua supports the following arithmetic operators:
  1616. <ul>
  1617. <li><b><code>+</code>: </b>addition</li>
  1618. <li><b><code>-</code>: </b>subtraction</li>
  1619. <li><b><code>*</code>: </b>multiplication</li>
  1620. <li><b><code>/</code>: </b>float division</li>
  1621. <li><b><code>//</code>: </b>floor division</li>
  1622. <li><b><code>%</code>: </b>modulo</li>
  1623. <li><b><code>^</code>: </b>exponentiation</li>
  1624. <li><b><code>-</code>: </b>unary minus</li>
  1625. </ul>
  1626. <p>
  1627. With the exception of exponentiation and float division,
  1628. the arithmetic operators work as follows:
  1629. If both operands are integers,
  1630. the operation is performed over integers and the result is an integer.
  1631. Otherwise, if both operands are numbers,
  1632. then they are converted to floats,
  1633. the operation is performed following the machine's rules
  1634. for floating-point arithmetic
  1635. (usually the IEEE 754 standard),
  1636. and the result is a float.
  1637. (The string library coerces strings to numbers in
  1638. arithmetic operations; see <a href="#3.4.3">&sect;3.4.3</a> for details.)
  1639. <p>
  1640. Exponentiation and float division (<code>/</code>)
  1641. always convert their operands to floats
  1642. and the result is always a float.
  1643. Exponentiation uses the ISO&nbsp;C function <code>pow</code>,
  1644. so that it works for non-integer exponents too.
  1645. <p>
  1646. Floor division (<code>//</code>) is a division
  1647. that rounds the quotient towards minus infinity,
  1648. resulting in the floor of the division of its operands.
  1649. <p>
  1650. Modulo is defined as the remainder of a division
  1651. that rounds the quotient towards minus infinity (floor division).
  1652. <p>
  1653. In case of overflows in integer arithmetic,
  1654. all operations <em>wrap around</em>.
  1655. <h3>3.4.2 &ndash; <a name="3.4.2">Bitwise Operators</a></h3><p>
  1656. Lua supports the following bitwise operators:
  1657. <ul>
  1658. <li><b><code>&amp;</code>: </b>bitwise AND</li>
  1659. <li><b><code>&#124;</code>: </b>bitwise OR</li>
  1660. <li><b><code>~</code>: </b>bitwise exclusive OR</li>
  1661. <li><b><code>&gt;&gt;</code>: </b>right shift</li>
  1662. <li><b><code>&lt;&lt;</code>: </b>left shift</li>
  1663. <li><b><code>~</code>: </b>unary bitwise NOT</li>
  1664. </ul>
  1665. <p>
  1666. All bitwise operations convert its operands to integers
  1667. (see <a href="#3.4.3">&sect;3.4.3</a>),
  1668. operate on all bits of those integers,
  1669. and result in an integer.
  1670. <p>
  1671. Both right and left shifts fill the vacant bits with zeros.
  1672. Negative displacements shift to the other direction;
  1673. displacements with absolute values equal to or higher than
  1674. the number of bits in an integer
  1675. result in zero (as all bits are shifted out).
  1676. <h3>3.4.3 &ndash; <a name="3.4.3">Coercions and Conversions</a></h3><p>
  1677. Lua provides some automatic conversions between some
  1678. types and representations at run time.
  1679. Bitwise operators always convert float operands to integers.
  1680. Exponentiation and float division
  1681. always convert integer operands to floats.
  1682. All other arithmetic operations applied to mixed numbers
  1683. (integers and floats) convert the integer operand to a float.
  1684. The C API also converts both integers to floats and
  1685. floats to integers, as needed.
  1686. Moreover, string concatenation accepts numbers as arguments,
  1687. besides strings.
  1688. <p>
  1689. In a conversion from integer to float,
  1690. if the integer value has an exact representation as a float,
  1691. that is the result.
  1692. Otherwise,
  1693. the conversion gets the nearest higher or
  1694. the nearest lower representable value.
  1695. This kind of conversion never fails.
  1696. <p>
  1697. The conversion from float to integer
  1698. checks whether the float has an exact representation as an integer
  1699. (that is, the float has an integral value and
  1700. it is in the range of integer representation).
  1701. If it does, that representation is the result.
  1702. Otherwise, the conversion fails.
  1703. <p>
  1704. Several places in Lua coerce strings to numbers when necessary.
  1705. In particular,
  1706. the string library sets metamethods that try to coerce
  1707. strings to numbers in all arithmetic operations.
  1708. If the conversion fails,
  1709. the library calls the metamethod of the other operand
  1710. (if present) or it raises an error.
  1711. Note that bitwise operators do not do this coercion.
  1712. <p>
  1713. Nonetheless, it is always a good practice not to rely on these
  1714. implicit coercions, as they are not always applied;
  1715. in particular, <code>"1"==1</code> is false and <code>"1"&lt;1</code> raises an error
  1716. (see <a href="#3.4.4">&sect;3.4.4</a>).
  1717. These coercions exist mainly for compatibility and may be removed
  1718. in future versions of the language.
  1719. <p>
  1720. A string is converted to an integer or a float
  1721. following its syntax and the rules of the Lua lexer.
  1722. The string may have also leading and trailing whitespaces and a sign.
  1723. All conversions from strings to numbers
  1724. accept both a dot and the current locale mark
  1725. as the radix character.
  1726. (The Lua lexer, however, accepts only a dot.)
  1727. If the string is not a valid numeral,
  1728. the conversion fails.
  1729. If necessary, the result of this first step is then converted
  1730. to a specific number subtype following the previous rules
  1731. for conversions between floats and integers.
  1732. <p>
  1733. The conversion from numbers to strings uses a
  1734. non-specified human-readable format.
  1735. To convert numbers to strings in any specific way,
  1736. use the function <a href="#pdf-string.format"><code>string.format</code></a>.
  1737. <h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
  1738. Lua supports the following relational operators:
  1739. <ul>
  1740. <li><b><code>==</code>: </b>equality</li>
  1741. <li><b><code>~=</code>: </b>inequality</li>
  1742. <li><b><code>&lt;</code>: </b>less than</li>
  1743. <li><b><code>&gt;</code>: </b>greater than</li>
  1744. <li><b><code>&lt;=</code>: </b>less or equal</li>
  1745. <li><b><code>&gt;=</code>: </b>greater or equal</li>
  1746. </ul><p>
  1747. These operators always result in <b>false</b> or <b>true</b>.
  1748. <p>
  1749. Equality (<code>==</code>) first compares the type of its operands.
  1750. If the types are different, then the result is <b>false</b>.
  1751. Otherwise, the values of the operands are compared.
  1752. Strings are equal if they have the same byte content.
  1753. Numbers are equal if they denote the same mathematical value.
  1754. <p>
  1755. Tables, userdata, and threads
  1756. are compared by reference:
  1757. two objects are considered equal only if they are the same object.
  1758. Every time you create a new object
  1759. (a table, a userdata, or a thread),
  1760. this new object is different from any previously existing object.
  1761. A function is always equal to itself.
  1762. Functions with any detectable difference
  1763. (different behavior, different definition) are always different.
  1764. Functions created at different times but with no detectable differences
  1765. may be classified as equal or not
  1766. (depending on internal caching details).
  1767. <p>
  1768. You can change the way that Lua compares tables and userdata
  1769. by using the <code>__eq</code> metamethod (see <a href="#2.4">&sect;2.4</a>).
  1770. <p>
  1771. Equality comparisons do not convert strings to numbers
  1772. or vice versa.
  1773. Thus, <code>"0"==0</code> evaluates to <b>false</b>,
  1774. and <code>t[0]</code> and <code>t["0"]</code> denote different
  1775. entries in a table.
  1776. <p>
  1777. The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
  1778. <p>
  1779. The order operators work as follows.
  1780. If both arguments are numbers,
  1781. then they are compared according to their mathematical values,
  1782. regardless of their subtypes.
  1783. Otherwise, if both arguments are strings,
  1784. then their values are compared according to the current locale.
  1785. Otherwise, Lua tries to call the <code>__lt</code> or the <code>__le</code>
  1786. metamethod (see <a href="#2.4">&sect;2.4</a>).
  1787. A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
  1788. and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
  1789. <p>
  1790. Following the IEEE 754 standard,
  1791. the special value NaN is considered neither less than,
  1792. nor equal to, nor greater than any value, including itself.
  1793. <h3>3.4.5 &ndash; <a name="3.4.5">Logical Operators</a></h3><p>
  1794. The logical operators in Lua are
  1795. <b>and</b>, <b>or</b>, and <b>not</b>.
  1796. Like the control structures (see <a href="#3.3.4">&sect;3.3.4</a>),
  1797. all logical operators consider both <b>false</b> and <b>nil</b> as false
  1798. and anything else as true.
  1799. <p>
  1800. The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
  1801. The conjunction operator <b>and</b> returns its first argument
  1802. if this value is <b>false</b> or <b>nil</b>;
  1803. otherwise, <b>and</b> returns its second argument.
  1804. The disjunction operator <b>or</b> returns its first argument
  1805. if this value is different from <b>nil</b> and <b>false</b>;
  1806. otherwise, <b>or</b> returns its second argument.
  1807. Both <b>and</b> and <b>or</b> use short-circuit evaluation;
  1808. that is,
  1809. the second operand is evaluated only if necessary.
  1810. Here are some examples:
  1811. <pre>
  1812. 10 or 20 --&gt; 10
  1813. 10 or error() --&gt; 10
  1814. nil or "a" --&gt; "a"
  1815. nil and 10 --&gt; nil
  1816. false and error() --&gt; false
  1817. false and nil --&gt; false
  1818. false or nil --&gt; nil
  1819. 10 and 20 --&gt; 20
  1820. </pre>
  1821. <h3>3.4.6 &ndash; <a name="3.4.6">Concatenation</a></h3><p>
  1822. The string concatenation operator in Lua is
  1823. denoted by two dots ('<code>..</code>').
  1824. If both operands are strings or numbers,
  1825. then the numbers are converted to strings
  1826. in a non-specified format (see <a href="#3.4.3">&sect;3.4.3</a>).
  1827. Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>).
  1828. <h3>3.4.7 &ndash; <a name="3.4.7">The Length Operator</a></h3>
  1829. <p>
  1830. The length operator is denoted by the unary prefix operator <code>#</code>.
  1831. <p>
  1832. The length of a string is its number of bytes.
  1833. (That is the usual meaning of string length when each
  1834. character is one byte.)
  1835. <p>
  1836. The length operator applied on a table
  1837. returns a border in that table.
  1838. A <em>border</em> in a table <code>t</code> is any natural number
  1839. that satisfies the following condition:
  1840. <pre>
  1841. (border == 0 or t[border] ~= nil) and t[border + 1] == nil
  1842. </pre><p>
  1843. In words,
  1844. a border is any (natural) index present in the table
  1845. that is followed by an absent index
  1846. (or zero, when index 1 is absent).
  1847. <p>
  1848. A table with exactly one border is called a <em>sequence</em>.
  1849. For instance, the table <code>{10, 20, 30, 40, 50}</code> is a sequence,
  1850. as it has only one border (5).
  1851. The table <code>{10, 20, 30, nil, 50}</code> has two borders (3 and 5),
  1852. and therefore it is not a sequence.
  1853. (The <b>nil</b> at index 4 is called a <em>hole</em>.)
  1854. The table <code>{nil, 20, 30, nil, nil, 60, nil}</code>
  1855. has three borders (0, 3, and 6) and three holes
  1856. (at indices 1, 4, and 5),
  1857. so it is not a sequence, too.
  1858. The table <code>{}</code> is a sequence with border 0.
  1859. Note that non-natural keys do not interfere
  1860. with whether a table is a sequence.
  1861. <p>
  1862. When <code>t</code> is a sequence,
  1863. <code>#t</code> returns its only border,
  1864. which corresponds to the intuitive notion of the length of the sequence.
  1865. When <code>t</code> is not a sequence,
  1866. <code>#t</code> can return any of its borders.
  1867. (The exact one depends on details of
  1868. the internal representation of the table,
  1869. which in turn can depend on how the table was populated and
  1870. the memory addresses of its non-numeric keys.)
  1871. <p>
  1872. The computation of the length of a table
  1873. has a guaranteed worst time of <em>O(log n)</em>,
  1874. where <em>n</em> is the largest natural key in the table.
  1875. <p>
  1876. A program can modify the behavior of the length operator for
  1877. any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">&sect;2.4</a>).
  1878. <h3>3.4.8 &ndash; <a name="3.4.8">Precedence</a></h3><p>
  1879. Operator precedence in Lua follows the table below,
  1880. from lower to higher priority:
  1881. <pre>
  1882. or
  1883. and
  1884. &lt; &gt; &lt;= &gt;= ~= ==
  1885. |
  1886. ~
  1887. &amp;
  1888. &lt;&lt; &gt;&gt;
  1889. ..
  1890. + -
  1891. * / // %
  1892. unary operators (not # - ~)
  1893. ^
  1894. </pre><p>
  1895. As usual,
  1896. you can use parentheses to change the precedences of an expression.
  1897. The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
  1898. operators are right associative.
  1899. All other binary operators are left associative.
  1900. <h3>3.4.9 &ndash; <a name="3.4.9">Table Constructors</a></h3><p>
  1901. Table constructors are expressions that create tables.
  1902. Every time a constructor is evaluated, a new table is created.
  1903. A constructor can be used to create an empty table
  1904. or to create a table and initialize some of its fields.
  1905. The general syntax for constructors is
  1906. <pre>
  1907. tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
  1908. fieldlist ::= field {fieldsep field} [fieldsep]
  1909. field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
  1910. fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
  1911. </pre>
  1912. <p>
  1913. Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
  1914. with key <code>exp1</code> and value <code>exp2</code>.
  1915. A field of the form <code>name = exp</code> is equivalent to
  1916. <code>["name"] = exp</code>.
  1917. Fields of the form <code>exp</code> are equivalent to
  1918. <code>[i] = exp</code>, where <code>i</code> are consecutive integers
  1919. starting with 1;
  1920. fields in the other formats do not affect this counting.
  1921. For example,
  1922. <pre>
  1923. a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
  1924. </pre><p>
  1925. is equivalent to
  1926. <pre>
  1927. do
  1928. local t = {}
  1929. t[f(1)] = g
  1930. t[1] = "x" -- 1st exp
  1931. t[2] = "y" -- 2nd exp
  1932. t.x = 1 -- t["x"] = 1
  1933. t[3] = f(x) -- 3rd exp
  1934. t[30] = 23
  1935. t[4] = 45 -- 4th exp
  1936. a = t
  1937. end
  1938. </pre>
  1939. <p>
  1940. The order of the assignments in a constructor is undefined.
  1941. (This order would be relevant only when there are repeated keys.)
  1942. <p>
  1943. If the last field in the list has the form <code>exp</code>
  1944. and the expression is a function call or a vararg expression,
  1945. then all values returned by this expression enter the list consecutively
  1946. (see <a href="#3.4.10">&sect;3.4.10</a>).
  1947. <p>
  1948. The field list can have an optional trailing separator,
  1949. as a convenience for machine-generated code.
  1950. <h3>3.4.10 &ndash; <a name="3.4.10">Function Calls</a></h3><p>
  1951. A function call in Lua has the following syntax:
  1952. <pre>
  1953. functioncall ::= prefixexp args
  1954. </pre><p>
  1955. In a function call,
  1956. first prefixexp and args are evaluated.
  1957. If the value of prefixexp has type <em>function</em>,
  1958. then this function is called
  1959. with the given arguments.
  1960. Otherwise, if present,
  1961. the prefixexp <code>__call</code> metamethod is called:
  1962. its first argument is the value of prefixexp,
  1963. followed by the original call arguments
  1964. (see <a href="#2.4">&sect;2.4</a>).
  1965. <p>
  1966. The form
  1967. <pre>
  1968. functioncall ::= prefixexp &lsquo;<b>:</b>&rsquo; Name args
  1969. </pre><p>
  1970. can be used to emulate methods.
  1971. A call <code>v:name(<em>args</em>)</code>
  1972. is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
  1973. except that <code>v</code> is evaluated only once.
  1974. <p>
  1975. Arguments have the following syntax:
  1976. <pre>
  1977. args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo;
  1978. args ::= tableconstructor
  1979. args ::= LiteralString
  1980. </pre><p>
  1981. All argument expressions are evaluated before the call.
  1982. A call of the form <code>f{<em>fields</em>}</code> is
  1983. syntactic sugar for <code>f({<em>fields</em>})</code>;
  1984. that is, the argument list is a single new table.
  1985. A call of the form <code>f'<em>string</em>'</code>
  1986. (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)
  1987. is syntactic sugar for <code>f('<em>string</em>')</code>;
  1988. that is, the argument list is a single literal string.
  1989. <p>
  1990. A call of the form <code>return <em>functioncall</em></code> not in the
  1991. scope of a to-be-closed variable is called a <em>tail call</em>.
  1992. Lua implements <em>proper tail calls</em>
  1993. (or <em>proper tail recursion</em>):
  1994. in a tail call,
  1995. the called function reuses the stack entry of the calling function.
  1996. Therefore, there is no limit on the number of nested tail calls that
  1997. a program can execute.
  1998. However, a tail call erases any debug information about the
  1999. calling function.
  2000. Note that a tail call only happens with a particular syntax,
  2001. where the <b>return</b> has one single function call as argument,
  2002. and it is outside the scope of any to-be-closed variable.
  2003. This syntax makes the calling function return exactly
  2004. the returns of the called function,
  2005. without any intervening action.
  2006. So, none of the following examples are tail calls:
  2007. <pre>
  2008. return (f(x)) -- results adjusted to 1
  2009. return 2 * f(x) -- result multiplied by 2
  2010. return x, f(x) -- additional results
  2011. f(x); return -- results discarded
  2012. return x or f(x) -- results adjusted to 1
  2013. </pre>
  2014. <h3>3.4.11 &ndash; <a name="3.4.11">Function Definitions</a></h3>
  2015. <p>
  2016. The syntax for function definition is
  2017. <pre>
  2018. functiondef ::= <b>function</b> funcbody
  2019. funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
  2020. </pre>
  2021. <p>
  2022. The following syntactic sugar simplifies function definitions:
  2023. <pre>
  2024. stat ::= <b>function</b> funcname funcbody
  2025. stat ::= <b>local</b> <b>function</b> Name funcbody
  2026. funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
  2027. </pre><p>
  2028. The statement
  2029. <pre>
  2030. function f () <em>body</em> end
  2031. </pre><p>
  2032. translates to
  2033. <pre>
  2034. f = function () <em>body</em> end
  2035. </pre><p>
  2036. The statement
  2037. <pre>
  2038. function t.a.b.c.f () <em>body</em> end
  2039. </pre><p>
  2040. translates to
  2041. <pre>
  2042. t.a.b.c.f = function () <em>body</em> end
  2043. </pre><p>
  2044. The statement
  2045. <pre>
  2046. local function f () <em>body</em> end
  2047. </pre><p>
  2048. translates to
  2049. <pre>
  2050. local f; f = function () <em>body</em> end
  2051. </pre><p>
  2052. not to
  2053. <pre>
  2054. local f = function () <em>body</em> end
  2055. </pre><p>
  2056. (This only makes a difference when the body of the function
  2057. contains references to <code>f</code>.)
  2058. <p>
  2059. A function definition is an executable expression,
  2060. whose value has type <em>function</em>.
  2061. When Lua precompiles a chunk,
  2062. all its function bodies are precompiled too,
  2063. but they are not created yet.
  2064. Then, whenever Lua executes the function definition,
  2065. the function is <em>instantiated</em> (or <em>closed</em>).
  2066. This function instance, or <em>closure</em>,
  2067. is the final value of the expression.
  2068. <p>
  2069. Parameters act as local variables that are
  2070. initialized with the argument values:
  2071. <pre>
  2072. parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
  2073. </pre><p>
  2074. When a Lua function is called,
  2075. it adjusts its list of arguments to
  2076. the length of its list of parameters,
  2077. unless the function is a <em>vararg function</em>,
  2078. which is indicated by three dots ('<code>...</code>')
  2079. at the end of its parameter list.
  2080. A vararg function does not adjust its argument list;
  2081. instead, it collects all extra arguments and supplies them
  2082. to the function through a <em>vararg expression</em>,
  2083. which is also written as three dots.
  2084. The value of this expression is a list of all actual extra arguments,
  2085. similar to a function with multiple results.
  2086. If a vararg expression is used inside another expression
  2087. or in the middle of a list of expressions,
  2088. then its return list is adjusted to one element.
  2089. If the expression is used as the last element of a list of expressions,
  2090. then no adjustment is made
  2091. (unless that last expression is enclosed in parentheses).
  2092. <p>
  2093. As an example, consider the following definitions:
  2094. <pre>
  2095. function f(a, b) end
  2096. function g(a, b, ...) end
  2097. function r() return 1,2,3 end
  2098. </pre><p>
  2099. Then, we have the following mapping from arguments to parameters and
  2100. to the vararg expression:
  2101. <pre>
  2102. CALL PARAMETERS
  2103. f(3) a=3, b=nil
  2104. f(3, 4) a=3, b=4
  2105. f(3, 4, 5) a=3, b=4
  2106. f(r(), 10) a=1, b=10
  2107. f(r()) a=1, b=2
  2108. g(3) a=3, b=nil, ... --&gt; (nothing)
  2109. g(3, 4) a=3, b=4, ... --&gt; (nothing)
  2110. g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8
  2111. g(5, r()) a=5, b=1, ... --&gt; 2 3
  2112. </pre>
  2113. <p>
  2114. Results are returned using the <b>return</b> statement (see <a href="#3.3.4">&sect;3.3.4</a>).
  2115. If control reaches the end of a function
  2116. without encountering a <b>return</b> statement,
  2117. then the function returns with no results.
  2118. <p>
  2119. There is a system-dependent limit on the number of values
  2120. that a function may return.
  2121. This limit is guaranteed to be greater than 1000.
  2122. <p>
  2123. The <em>colon</em> syntax
  2124. is used to emulate <em>methods</em>,
  2125. adding an implicit extra parameter <code>self</code> to the function.
  2126. Thus, the statement
  2127. <pre>
  2128. function t.a.b.c:f (<em>params</em>) <em>body</em> end
  2129. </pre><p>
  2130. is syntactic sugar for
  2131. <pre>
  2132. t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
  2133. </pre>
  2134. <h2>3.5 &ndash; <a name="3.5">Visibility Rules</a></h2>
  2135. <p>
  2136. Lua is a lexically scoped language.
  2137. The scope of a local variable begins at the first statement after
  2138. its declaration and lasts until the last non-void statement
  2139. of the innermost block that includes the declaration.
  2140. Consider the following example:
  2141. <pre>
  2142. x = 10 -- global variable
  2143. do -- new block
  2144. local x = x -- new 'x', with value 10
  2145. print(x) --&gt; 10
  2146. x = x+1
  2147. do -- another block
  2148. local x = x+1 -- another 'x'
  2149. print(x) --&gt; 12
  2150. end
  2151. print(x) --&gt; 11
  2152. end
  2153. print(x) --&gt; 10 (the global one)
  2154. </pre>
  2155. <p>
  2156. Notice that, in a declaration like <code>local x = x</code>,
  2157. the new <code>x</code> being declared is not in scope yet,
  2158. and so the second <code>x</code> refers to the outside variable.
  2159. <p>
  2160. Because of the lexical scoping rules,
  2161. local variables can be freely accessed by functions
  2162. defined inside their scope.
  2163. A local variable used by an inner function is called an <em>upvalue</em>
  2164. (or <em>external local variable</em>, or simply <em>external variable</em>)
  2165. inside the inner function.
  2166. <p>
  2167. Notice that each execution of a <b>local</b> statement
  2168. defines new local variables.
  2169. Consider the following example:
  2170. <pre>
  2171. a = {}
  2172. local x = 20
  2173. for i = 1, 10 do
  2174. local y = 0
  2175. a[i] = function () y = y + 1; return x + y end
  2176. end
  2177. </pre><p>
  2178. The loop creates ten closures
  2179. (that is, ten instances of the anonymous function).
  2180. Each of these closures uses a different <code>y</code> variable,
  2181. while all of them share the same <code>x</code>.
  2182. <h1>4 &ndash; <a name="4">The Application Program Interface</a></h1>
  2183. <p>
  2184. This section describes the C&nbsp;API for Lua, that is,
  2185. the set of C&nbsp;functions available to the host program to communicate
  2186. with Lua.
  2187. All API functions and related types and constants
  2188. are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
  2189. <p>
  2190. Even when we use the term "function",
  2191. any facility in the API may be provided as a macro instead.
  2192. Except where stated otherwise,
  2193. all such macros use each of their arguments exactly once
  2194. (except for the first argument, which is always a Lua state),
  2195. and so do not generate any hidden side-effects.
  2196. <p>
  2197. As in most C&nbsp;libraries,
  2198. the Lua API functions do not check their arguments
  2199. for validity or consistency.
  2200. However, you can change this behavior by compiling Lua
  2201. with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
  2202. <p>
  2203. The Lua library is fully reentrant:
  2204. it has no global variables.
  2205. It keeps all information it needs in a dynamic structure,
  2206. called the <em>Lua state</em>.
  2207. <p>
  2208. Each Lua state has one or more threads,
  2209. which correspond to independent, cooperative lines of execution.
  2210. The type <a href="#lua_State"><code>lua_State</code></a> (despite its name) refers to a thread.
  2211. (Indirectly, through the thread, it also refers to the
  2212. Lua state associated to the thread.)
  2213. <p>
  2214. A pointer to a thread must be passed as the first argument to
  2215. every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
  2216. which creates a Lua state from scratch and returns a pointer
  2217. to the <em>main thread</em> in the new state.
  2218. <h2>4.1 &ndash; <a name="4.1">The Stack</a></h2>
  2219. <p>
  2220. Lua uses a <em>virtual stack</em> to pass values to and from C.
  2221. Each element in this stack represents a Lua value
  2222. (<b>nil</b>, number, string, etc.).
  2223. Functions in the API can access this stack through the
  2224. Lua state parameter that they receive.
  2225. <p>
  2226. Whenever Lua calls C, the called function gets a new stack,
  2227. which is independent of previous stacks and of stacks of
  2228. C&nbsp;functions that are still active.
  2229. This stack initially contains any arguments to the C&nbsp;function
  2230. and it is where the C&nbsp;function can store temporary
  2231. Lua values and must push its results
  2232. to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  2233. <p>
  2234. For convenience,
  2235. most query operations in the API do not follow a strict stack discipline.
  2236. Instead, they can refer to any element in the stack
  2237. by using an <em>index</em>:
  2238. A positive index represents an absolute stack position,
  2239. starting at&nbsp;1 as the bottom of the stack;
  2240. a negative index represents an offset relative to the top of the stack.
  2241. More specifically, if the stack has <em>n</em> elements,
  2242. then index&nbsp;1 represents the first element
  2243. (that is, the element that was pushed onto the stack first)
  2244. and
  2245. index&nbsp;<em>n</em> represents the last element;
  2246. index&nbsp;-1 also represents the last element
  2247. (that is, the element at the&nbsp;top)
  2248. and index <em>-n</em> represents the first element.
  2249. <h3>4.1.1 &ndash; <a name="4.1.1">Stack Size</a></h3>
  2250. <p>
  2251. When you interact with the Lua API,
  2252. you are responsible for ensuring consistency.
  2253. In particular,
  2254. <em>you are responsible for controlling stack overflow</em>.
  2255. You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
  2256. to ensure that the stack has enough space for pushing new elements.
  2257. <p>
  2258. Whenever Lua calls C,
  2259. it ensures that the stack has space for
  2260. at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
  2261. <code>LUA_MINSTACK</code> is defined as 20,
  2262. so that usually you do not have to worry about stack space
  2263. unless your code has loops pushing elements onto the stack.
  2264. <p>
  2265. When you call a Lua function
  2266. without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
  2267. Lua ensures that the stack has enough space for all results,
  2268. but it does not ensure any extra space.
  2269. So, before pushing anything in the stack after such a call
  2270. you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
  2271. <h3>4.1.2 &ndash; <a name="4.1.2">Valid and Acceptable Indices</a></h3>
  2272. <p>
  2273. Any function in the API that receives stack indices
  2274. works only with <em>valid indices</em> or <em>acceptable indices</em>.
  2275. <p>
  2276. A <em>valid index</em> is an index that refers to a
  2277. position that stores a modifiable Lua value.
  2278. It comprises stack indices between&nbsp;1 and the stack top
  2279. (<code>1 &le; abs(index) &le; top</code>)
  2280. plus <em>pseudo-indices</em>,
  2281. which represent some positions that are accessible to C&nbsp;code
  2282. but that are not in the stack.
  2283. Pseudo-indices are used to access the registry (see <a href="#4.3">&sect;4.3</a>)
  2284. and the upvalues of a C&nbsp;function (see <a href="#4.2">&sect;4.2</a>).
  2285. <p>
  2286. Functions that do not need a specific mutable position,
  2287. but only a value (e.g., query functions),
  2288. can be called with acceptable indices.
  2289. An <em>acceptable index</em> can be any valid index,
  2290. but it also can be any positive index after the stack top
  2291. within the space allocated for the stack,
  2292. that is, indices up to the stack size.
  2293. (Note that 0 is never an acceptable index.)
  2294. Indices to upvalues (see <a href="#4.2">&sect;4.2</a>) greater than the real number
  2295. of upvalues in the current C&nbsp;function are also acceptable (but invalid).
  2296. Except when noted otherwise,
  2297. functions in the API work with acceptable indices.
  2298. <p>
  2299. Acceptable indices serve to avoid extra tests
  2300. against the stack top when querying the stack.
  2301. For instance, a C&nbsp;function can query its third argument
  2302. without the need to check whether there is a third argument,
  2303. that is, without the need to check whether 3 is a valid index.
  2304. <p>
  2305. For functions that can be called with acceptable indices,
  2306. any non-valid index is treated as if it
  2307. contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
  2308. which behaves like a nil value.
  2309. <h2>4.2 &ndash; <a name="4.2">C Closures</a></h2>
  2310. <p>
  2311. When a C&nbsp;function is created,
  2312. it is possible to associate some values with it,
  2313. thus creating a <em>C&nbsp;closure</em>
  2314. (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
  2315. these values are called <em>upvalues</em> and are
  2316. accessible to the function whenever it is called.
  2317. <p>
  2318. Whenever a C&nbsp;function is called,
  2319. its upvalues are located at specific pseudo-indices.
  2320. These pseudo-indices are produced by the macro
  2321. <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
  2322. The first upvalue associated with a function is at index
  2323. <code>lua_upvalueindex(1)</code>, and so on.
  2324. Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
  2325. where <em>n</em> is greater than the number of upvalues of the
  2326. current function
  2327. (but not greater than 256,
  2328. which is one plus the maximum number of upvalues in a closure),
  2329. produces an acceptable but invalid index.
  2330. <p>
  2331. A C&nbsp;closure can also change the values
  2332. of its corresponding upvalues.
  2333. <h2>4.3 &ndash; <a name="4.3">Registry</a></h2>
  2334. <p>
  2335. Lua provides a <em>registry</em>,
  2336. a predefined table that can be used by any C&nbsp;code to
  2337. store whatever Lua values it needs to store.
  2338. The registry table is always accessible at pseudo-index
  2339. <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>.
  2340. Any C&nbsp;library can store data into this table,
  2341. but it must take care to choose keys
  2342. that are different from those used
  2343. by other libraries, to avoid collisions.
  2344. Typically, you should use as key a string containing your library name,
  2345. or a light userdata with the address of a C&nbsp;object in your code,
  2346. or any Lua object created by your code.
  2347. As with variable names,
  2348. string keys starting with an underscore followed by
  2349. uppercase letters are reserved for Lua.
  2350. <p>
  2351. The integer keys in the registry are used
  2352. by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>)
  2353. and by some predefined values.
  2354. Therefore, integer keys in the registry
  2355. must not be used for other purposes.
  2356. <p>
  2357. When you create a new Lua state,
  2358. its registry comes with some predefined values.
  2359. These predefined values are indexed with integer keys
  2360. defined as constants in <code>lua.h</code>.
  2361. The following constants are defined:
  2362. <ul>
  2363. <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has
  2364. the main thread of the state.
  2365. (The main thread is the one created together with the state.)
  2366. </li>
  2367. <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has
  2368. the global environment.
  2369. </li>
  2370. </ul>
  2371. <h2>4.4 &ndash; <a name="4.4">Error Handling in C</a></h2>
  2372. <p>
  2373. Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
  2374. (Lua will use exceptions if you compile it as C++;
  2375. search for <code>LUAI_THROW</code> in the source code for details.)
  2376. When Lua faces any error,
  2377. such as a memory allocation error or a type error,
  2378. it <em>raises</em> an error;
  2379. that is, it does a long jump.
  2380. A <em>protected environment</em> uses <code>setjmp</code>
  2381. to set a recovery point;
  2382. any error jumps to the most recent active recovery point.
  2383. <p>
  2384. Inside a C&nbsp;function you can raise an error explicitly
  2385. by calling <a href="#lua_error"><code>lua_error</code></a>.
  2386. <p>
  2387. Most functions in the API can raise an error,
  2388. for instance due to a memory allocation error.
  2389. The documentation for each function indicates whether
  2390. it can raise errors.
  2391. <p>
  2392. If an error happens outside any protected environment,
  2393. Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
  2394. and then calls <code>abort</code>,
  2395. thus exiting the host application.
  2396. Your panic function can avoid this exit by
  2397. never returning
  2398. (e.g., doing a long jump to your own recovery point outside Lua).
  2399. <p>
  2400. The panic function,
  2401. as its name implies,
  2402. is a mechanism of last resort.
  2403. Programs should avoid it.
  2404. As a general rule,
  2405. when a C&nbsp;function is called by Lua with a Lua state,
  2406. it can do whatever it wants on that Lua state,
  2407. as it should be already protected.
  2408. However,
  2409. when C code operates on other Lua states
  2410. (e.g., a Lua-state argument to the function,
  2411. a Lua state stored in the registry, or
  2412. the result of <a href="#lua_newthread"><code>lua_newthread</code></a>),
  2413. it should use them only in API calls that cannot raise errors.
  2414. <p>
  2415. The panic function runs as if it were a message handler (see <a href="#2.3">&sect;2.3</a>);
  2416. in particular, the error object is on the top of the stack.
  2417. However, there is no guarantee about stack space.
  2418. To push anything on the stack,
  2419. the panic function must first check the available space (see <a href="#4.1.1">&sect;4.1.1</a>).
  2420. <h3>4.4.1 &ndash; <a name="4.4.1">Status Codes</a></h3>
  2421. <p>
  2422. Several functions that report errors in the API use the following
  2423. status codes to indicate different kinds of errors or other conditions:
  2424. <ul>
  2425. <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b> no errors.</li>
  2426. <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b> a runtime error.</li>
  2427. <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
  2428. memory allocation error.
  2429. For such errors, Lua does not call the message handler.
  2430. </li>
  2431. <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b> error while running the message handler.</li>
  2432. <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b> syntax error during precompilation.</li>
  2433. <li><b><a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a>: </b> the thread (coroutine) yields.</li>
  2434. <li><b><a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>: </b> a file-related error;
  2435. e.g., it cannot open or read the file.</li>
  2436. </ul><p>
  2437. These constants are defined in the header file <code>lua.h</code>.
  2438. <h2>4.5 &ndash; <a name="4.5">Handling Yields in C</a></h2>
  2439. <p>
  2440. Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
  2441. Therefore, if a C&nbsp;function <code>foo</code> calls an API function
  2442. and this API function yields
  2443. (directly or indirectly by calling another function that yields),
  2444. Lua cannot return to <code>foo</code> any more,
  2445. because the <code>longjmp</code> removes its frame from the C stack.
  2446. <p>
  2447. To avoid this kind of problem,
  2448. Lua raises an error whenever it tries to yield across an API call,
  2449. except for three functions:
  2450. <a href="#lua_yieldk"><code>lua_yieldk</code></a>, <a href="#lua_callk"><code>lua_callk</code></a>, and <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
  2451. All those functions receive a <em>continuation function</em>
  2452. (as a parameter named <code>k</code>) to continue execution after a yield.
  2453. <p>
  2454. We need to set some terminology to explain continuations.
  2455. We have a C&nbsp;function called from Lua which we will call
  2456. the <em>original function</em>.
  2457. This original function then calls one of those three functions in the C API,
  2458. which we will call the <em>callee function</em>,
  2459. that then yields the current thread.
  2460. This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  2461. or when the callee function is either <a href="#lua_callk"><code>lua_callk</code></a> or <a href="#lua_pcallk"><code>lua_pcallk</code></a>
  2462. and the function called by them yields.
  2463. <p>
  2464. Suppose the running thread yields while executing the callee function.
  2465. After the thread resumes,
  2466. it eventually will finish running the callee function.
  2467. However,
  2468. the callee function cannot return to the original function,
  2469. because its frame in the C stack was destroyed by the yield.
  2470. Instead, Lua calls a <em>continuation function</em>,
  2471. which was given as an argument to the callee function.
  2472. As the name implies,
  2473. the continuation function should continue the task
  2474. of the original function.
  2475. <p>
  2476. As an illustration, consider the following function:
  2477. <pre>
  2478. int original_function (lua_State *L) {
  2479. ... /* code 1 */
  2480. status = lua_pcall(L, n, m, h); /* calls Lua */
  2481. ... /* code 2 */
  2482. }
  2483. </pre><p>
  2484. Now we want to allow
  2485. the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
  2486. First, we can rewrite our function like here:
  2487. <pre>
  2488. int k (lua_State *L, int status, lua_KContext ctx) {
  2489. ... /* code 2 */
  2490. }
  2491. int original_function (lua_State *L) {
  2492. ... /* code 1 */
  2493. return k(L, lua_pcall(L, n, m, h), ctx);
  2494. }
  2495. </pre><p>
  2496. In the above code,
  2497. the new function <code>k</code> is a
  2498. <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
  2499. which should do all the work that the original function
  2500. was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
  2501. Now, we must inform Lua that it must call <code>k</code> if the Lua code
  2502. being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
  2503. (errors or yielding),
  2504. so we rewrite the code as here,
  2505. replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>:
  2506. <pre>
  2507. int original_function (lua_State *L) {
  2508. ... /* code 1 */
  2509. return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
  2510. }
  2511. </pre><p>
  2512. Note the external, explicit call to the continuation:
  2513. Lua will call the continuation only if needed, that is,
  2514. in case of errors or resuming after a yield.
  2515. If the called function returns normally without ever yielding,
  2516. <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally.
  2517. (Of course, instead of calling the continuation in that case,
  2518. you can do the equivalent work directly inside the original function.)
  2519. <p>
  2520. Besides the Lua state,
  2521. the continuation function has two other parameters:
  2522. the final status of the call and the context value (<code>ctx</code>) that
  2523. was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
  2524. Lua does not use this context value;
  2525. it only passes this value from the original function to the
  2526. continuation function.
  2527. For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
  2528. the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
  2529. except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield
  2530. (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
  2531. For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
  2532. the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
  2533. (For these two functions,
  2534. Lua will not call the continuation in case of errors,
  2535. because they do not handle errors.)
  2536. Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
  2537. you should call the continuation function
  2538. with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
  2539. (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
  2540. directly the continuation function,
  2541. because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
  2542. <p>
  2543. Lua treats the continuation function as if it were the original function.
  2544. The continuation function receives the same Lua stack
  2545. from the original function,
  2546. in the same state it would be if the callee function had returned.
  2547. (For instance,
  2548. after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
  2549. removed from the stack and replaced by the results from the call.)
  2550. It also has the same upvalues.
  2551. Whatever it returns is handled by Lua as if it were the return
  2552. of the original function.
  2553. <h2>4.6 &ndash; <a name="4.6">Functions and Types</a></h2>
  2554. <p>
  2555. Here we list all functions and types from the C&nbsp;API in
  2556. alphabetical order.
  2557. Each function has an indicator like this:
  2558. <span class="apii">[-o, +p, <em>x</em>]</span>
  2559. <p>
  2560. The first field, <code>o</code>,
  2561. is how many elements the function pops from the stack.
  2562. The second field, <code>p</code>,
  2563. is how many elements the function pushes onto the stack.
  2564. (Any function always pushes its results after popping its arguments.)
  2565. A field in the form <code>x|y</code> means the function can push (or pop)
  2566. <code>x</code> or <code>y</code> elements,
  2567. depending on the situation;
  2568. an interrogation mark '<code>?</code>' means that
  2569. we cannot know how many elements the function pops/pushes
  2570. by looking only at its arguments.
  2571. (For instance, they may depend on what is on the stack.)
  2572. The third field, <code>x</code>,
  2573. tells whether the function may raise errors:
  2574. '<code>-</code>' means the function never raises any error;
  2575. '<code>m</code>' means the function may raise only out-of-memory errors;
  2576. '<code>v</code>' means the function may raise the errors explained in the text;
  2577. '<code>e</code>' means the function can run arbitrary Lua code,
  2578. either directly or through metamethods,
  2579. and therefore may raise any errors.
  2580. <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
  2581. <span class="apii">[-0, +0, &ndash;]</span>
  2582. <pre>int lua_absindex (lua_State *L, int idx);</pre>
  2583. <p>
  2584. Converts the acceptable index <code>idx</code>
  2585. into an equivalent absolute index
  2586. (that is, one that does not depend on the stack top).
  2587. <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
  2588. <pre>typedef void * (*lua_Alloc) (void *ud,
  2589. void *ptr,
  2590. size_t osize,
  2591. size_t nsize);</pre>
  2592. <p>
  2593. The type of the memory-allocation function used by Lua states.
  2594. The allocator function must provide a
  2595. functionality similar to <code>realloc</code>,
  2596. but not exactly the same.
  2597. Its arguments are
  2598. <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
  2599. <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
  2600. <code>osize</code>, the original size of the block or some code about what
  2601. is being allocated;
  2602. and <code>nsize</code>, the new size of the block.
  2603. <p>
  2604. When <code>ptr</code> is not <code>NULL</code>,
  2605. <code>osize</code> is the size of the block pointed by <code>ptr</code>,
  2606. that is, the size given when it was allocated or reallocated.
  2607. <p>
  2608. When <code>ptr</code> is <code>NULL</code>,
  2609. <code>osize</code> encodes the kind of object that Lua is allocating.
  2610. <code>osize</code> is any of
  2611. <a href="#pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, <a href="#pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, <a href="#pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
  2612. <a href="#pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, or <a href="#pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a> when (and only when)
  2613. Lua is creating a new object of that type.
  2614. When <code>osize</code> is some other value,
  2615. Lua is allocating memory for something else.
  2616. <p>
  2617. Lua assumes the following behavior from the allocator function:
  2618. <p>
  2619. When <code>nsize</code> is zero,
  2620. the allocator must behave like <code>free</code>
  2621. and then return <code>NULL</code>.
  2622. <p>
  2623. When <code>nsize</code> is not zero,
  2624. the allocator must behave like <code>realloc</code>.
  2625. In particular, the allocator returns <code>NULL</code>
  2626. if and only if it cannot fulfill the request.
  2627. <p>
  2628. Here is a simple implementation for the allocator function.
  2629. It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
  2630. <pre>
  2631. static void *l_alloc (void *ud, void *ptr, size_t osize,
  2632. size_t nsize) {
  2633. (void)ud; (void)osize; /* not used */
  2634. if (nsize == 0) {
  2635. free(ptr);
  2636. return NULL;
  2637. }
  2638. else
  2639. return realloc(ptr, nsize);
  2640. }
  2641. </pre><p>
  2642. Note that Standard&nbsp;C ensures
  2643. that <code>free(NULL)</code> has no effect and that
  2644. <code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>.
  2645. <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
  2646. <span class="apii">[-(2|1), +1, <em>e</em>]</span>
  2647. <pre>void lua_arith (lua_State *L, int op);</pre>
  2648. <p>
  2649. Performs an arithmetic or bitwise operation over the two values
  2650. (or one, in the case of negations)
  2651. at the top of the stack,
  2652. with the value on the top being the second operand,
  2653. pops these values, and pushes the result of the operation.
  2654. The function follows the semantics of the corresponding Lua operator
  2655. (that is, it may call metamethods).
  2656. <p>
  2657. The value of <code>op</code> must be one of the following constants:
  2658. <ul>
  2659. <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
  2660. <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
  2661. <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
  2662. <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li>
  2663. <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li>
  2664. <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
  2665. <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
  2666. <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li>
  2667. <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise NOT (<code>~</code>)</li>
  2668. <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise AND (<code>&amp;</code>)</li>
  2669. <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise OR (<code>|</code>)</li>
  2670. <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive OR (<code>~</code>)</li>
  2671. <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code>&lt;&lt;</code>)</li>
  2672. <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>&gt;&gt;</code>)</li>
  2673. </ul>
  2674. <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
  2675. <span class="apii">[-0, +0, &ndash;]</span>
  2676. <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
  2677. <p>
  2678. Sets a new panic function and returns the old one (see <a href="#4.4">&sect;4.4</a>).
  2679. <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
  2680. <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
  2681. <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
  2682. <p>
  2683. Calls a function.
  2684. Like regular Lua calls,
  2685. <code>lua_call</code> respects the <code>__call</code> metamethod.
  2686. So, here the word "function"
  2687. means any callable value.
  2688. <p>
  2689. To do a call you must use the following protocol:
  2690. first, the function to be called is pushed onto the stack;
  2691. then, the arguments to the call are pushed
  2692. in direct order;
  2693. that is, the first argument is pushed first.
  2694. Finally you call <a href="#lua_call"><code>lua_call</code></a>;
  2695. <code>nargs</code> is the number of arguments that you pushed onto the stack.
  2696. When the function returns,
  2697. all arguments and the function value are popped
  2698. and the call results are pushed onto the stack.
  2699. The number of results is adjusted to <code>nresults</code>,
  2700. unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
  2701. In this case, all results from the function are pushed;
  2702. Lua takes care that the returned values fit into the stack space,
  2703. but it does not ensure any extra space in the stack.
  2704. The function results are pushed onto the stack in direct order
  2705. (the first result is pushed first),
  2706. so that after the call the last result is on the top of the stack.
  2707. <p>
  2708. Any error while calling and running the function is propagated upwards
  2709. (with a <code>longjmp</code>).
  2710. <p>
  2711. The following example shows how the host program can do the
  2712. equivalent to this Lua code:
  2713. <pre>
  2714. a = f("how", t.x, 14)
  2715. </pre><p>
  2716. Here it is in&nbsp;C:
  2717. <pre>
  2718. lua_getglobal(L, "f"); /* function to be called */
  2719. lua_pushliteral(L, "how"); /* 1st argument */
  2720. lua_getglobal(L, "t"); /* table to be indexed */
  2721. lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
  2722. lua_remove(L, -2); /* remove 't' from the stack */
  2723. lua_pushinteger(L, 14); /* 3rd argument */
  2724. lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
  2725. lua_setglobal(L, "a"); /* set global 'a' */
  2726. </pre><p>
  2727. Note that the code above is <em>balanced</em>:
  2728. at its end, the stack is back to its original configuration.
  2729. This is considered good programming practice.
  2730. <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
  2731. <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
  2732. <pre>void lua_callk (lua_State *L,
  2733. int nargs,
  2734. int nresults,
  2735. lua_KContext ctx,
  2736. lua_KFunction k);</pre>
  2737. <p>
  2738. This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
  2739. but allows the called function to yield (see <a href="#4.5">&sect;4.5</a>).
  2740. <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
  2741. <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
  2742. <p>
  2743. Type for C&nbsp;functions.
  2744. <p>
  2745. In order to communicate properly with Lua,
  2746. a C&nbsp;function must use the following protocol,
  2747. which defines the way parameters and results are passed:
  2748. a C&nbsp;function receives its arguments from Lua in its stack
  2749. in direct order (the first argument is pushed first).
  2750. So, when the function starts,
  2751. <code>lua_gettop(L)</code> returns the number of arguments received by the function.
  2752. The first argument (if any) is at index 1
  2753. and its last argument is at index <code>lua_gettop(L)</code>.
  2754. To return values to Lua, a C&nbsp;function just pushes them onto the stack,
  2755. in direct order (the first result is pushed first),
  2756. and returns in C the number of results.
  2757. Any other value in the stack below the results will be properly
  2758. discarded by Lua.
  2759. Like a Lua function, a C&nbsp;function called by Lua can also return
  2760. many results.
  2761. <p>
  2762. As an example, the following function receives a variable number
  2763. of numeric arguments and returns their average and their sum:
  2764. <pre>
  2765. static int foo (lua_State *L) {
  2766. int n = lua_gettop(L); /* number of arguments */
  2767. lua_Number sum = 0.0;
  2768. int i;
  2769. for (i = 1; i &lt;= n; i++) {
  2770. if (!lua_isnumber(L, i)) {
  2771. lua_pushliteral(L, "incorrect argument");
  2772. lua_error(L);
  2773. }
  2774. sum += lua_tonumber(L, i);
  2775. }
  2776. lua_pushnumber(L, sum/n); /* first result */
  2777. lua_pushnumber(L, sum); /* second result */
  2778. return 2; /* number of results */
  2779. }
  2780. </pre>
  2781. <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
  2782. <span class="apii">[-0, +0, &ndash;]</span>
  2783. <pre>int lua_checkstack (lua_State *L, int n);</pre>
  2784. <p>
  2785. Ensures that the stack has space for at least <code>n</code> extra slots,
  2786. that is, that you can safely push up to <code>n</code> values into it.
  2787. It returns false if it cannot fulfill the request,
  2788. either because it would cause the stack
  2789. to be greater than a fixed maximum size
  2790. (typically at least several thousand elements) or
  2791. because it cannot allocate memory for the extra space.
  2792. This function never shrinks the stack;
  2793. if the stack already has space for the extra slots,
  2794. it is left unchanged.
  2795. <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
  2796. <span class="apii">[-0, +0, &ndash;]</span>
  2797. <pre>void lua_close (lua_State *L);</pre>
  2798. <p>
  2799. Close all active to-be-closed variables in the main thread,
  2800. release all objects in the given Lua state
  2801. (calling the corresponding garbage-collection metamethods, if any),
  2802. and frees all dynamic memory used by this state.
  2803. <p>
  2804. On several platforms, you may not need to call this function,
  2805. because all resources are naturally released when the host program ends.
  2806. On the other hand, long-running programs that create multiple states,
  2807. such as daemons or web servers,
  2808. will probably need to close states as soon as they are not needed.
  2809. <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
  2810. <span class="apii">[-0, +0, <em>e</em>]</span>
  2811. <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre>
  2812. <p>
  2813. Compares two Lua values.
  2814. Returns 1 if the value at index <code>index1</code> satisfies <code>op</code>
  2815. when compared with the value at index <code>index2</code>,
  2816. following the semantics of the corresponding Lua operator
  2817. (that is, it may call metamethods).
  2818. Otherwise returns&nbsp;0.
  2819. Also returns&nbsp;0 if any of the indices is not valid.
  2820. <p>
  2821. The value of <code>op</code> must be one of the following constants:
  2822. <ul>
  2823. <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li>
  2824. <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code>&lt;</code>)</li>
  2825. <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code>&lt;=</code>)</li>
  2826. </ul>
  2827. <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
  2828. <span class="apii">[-n, +1, <em>e</em>]</span>
  2829. <pre>void lua_concat (lua_State *L, int n);</pre>
  2830. <p>
  2831. Concatenates the <code>n</code> values at the top of the stack,
  2832. pops them, and leaves the result on the top.
  2833. If <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack
  2834. (that is, the function does nothing);
  2835. if <code>n</code> is 0, the result is the empty string.
  2836. Concatenation is performed following the usual semantics of Lua
  2837. (see <a href="#3.4.6">&sect;3.4.6</a>).
  2838. <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
  2839. <span class="apii">[-0, +0, &ndash;]</span>
  2840. <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
  2841. <p>
  2842. Copies the element at index <code>fromidx</code>
  2843. into the valid index <code>toidx</code>,
  2844. replacing the value at that position.
  2845. Values at other positions are not affected.
  2846. <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
  2847. <span class="apii">[-0, +1, <em>m</em>]</span>
  2848. <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
  2849. <p>
  2850. Creates a new empty table and pushes it onto the stack.
  2851. Parameter <code>narr</code> is a hint for how many elements the table
  2852. will have as a sequence;
  2853. parameter <code>nrec</code> is a hint for how many other elements
  2854. the table will have.
  2855. Lua may use these hints to preallocate memory for the new table.
  2856. This preallocation may help performance when you know in advance
  2857. how many elements the table will have.
  2858. Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
  2859. <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
  2860. <span class="apii">[-0, +0, &ndash;]</span>
  2861. <pre>int lua_dump (lua_State *L,
  2862. lua_Writer writer,
  2863. void *data,
  2864. int strip);</pre>
  2865. <p>
  2866. Dumps a function as a binary chunk.
  2867. Receives a Lua function on the top of the stack
  2868. and produces a binary chunk that,
  2869. if loaded again,
  2870. results in a function equivalent to the one dumped.
  2871. As it produces parts of the chunk,
  2872. <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
  2873. with the given <code>data</code>
  2874. to write them.
  2875. <p>
  2876. If <code>strip</code> is true,
  2877. the binary representation may not include all debug information
  2878. about the function,
  2879. to save space.
  2880. <p>
  2881. The value returned is the error code returned by the last
  2882. call to the writer;
  2883. 0&nbsp;means no errors.
  2884. <p>
  2885. This function does not pop the Lua function from the stack.
  2886. <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
  2887. <span class="apii">[-1, +0, <em>v</em>]</span>
  2888. <pre>int lua_error (lua_State *L);</pre>
  2889. <p>
  2890. Raises a Lua error,
  2891. using the value on the top of the stack as the error object.
  2892. This function does a long jump,
  2893. and therefore never returns
  2894. (see <a href="#luaL_error"><code>luaL_error</code></a>).
  2895. <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
  2896. <span class="apii">[-0, +0, &ndash;]</span>
  2897. <pre>int lua_gc (lua_State *L, int what, ...);</pre>
  2898. <p>
  2899. Controls the garbage collector.
  2900. <p>
  2901. This function performs several tasks,
  2902. according to the value of the parameter <code>what</code>.
  2903. For options that need extra arguments,
  2904. they are listed after the option.
  2905. <ul>
  2906. <li><b><code>LUA_GCCOLLECT</code>: </b>
  2907. Performs a full garbage-collection cycle.
  2908. </li>
  2909. <li><b><code>LUA_GCSTOP</code>: </b>
  2910. Stops the garbage collector.
  2911. </li>
  2912. <li><b><code>LUA_GCRESTART</code>: </b>
  2913. Restarts the garbage collector.
  2914. </li>
  2915. <li><b><code>LUA_GCCOUNT</code>: </b>
  2916. Returns the current amount of memory (in Kbytes) in use by Lua.
  2917. </li>
  2918. <li><b><code>LUA_GCCOUNTB</code>: </b>
  2919. Returns the remainder of dividing the current amount of bytes of
  2920. memory in use by Lua by 1024.
  2921. </li>
  2922. <li><b><code>LUA_GCSTEP</code> <code>(int stepsize)</code>: </b>
  2923. Performs an incremental step of garbage collection,
  2924. corresponding to the allocation of <code>stepsize</code> Kbytes.
  2925. </li>
  2926. <li><b><code>LUA_GCISRUNNING</code>: </b>
  2927. Returns a boolean that tells whether the collector is running
  2928. (i.e., not stopped).
  2929. </li>
  2930. <li><b><code>LUA_GCINC</code> (int pause, int stepmul, stepsize): </b>
  2931. Changes the collector to incremental mode
  2932. with the given parameters (see <a href="#2.5.1">&sect;2.5.1</a>).
  2933. Returns the previous mode (<code>LUA_GCGEN</code> or <code>LUA_GCINC</code>).
  2934. </li>
  2935. <li><b><code>LUA_GCGEN</code> (int minormul, int majormul): </b>
  2936. Changes the collector to generational mode
  2937. with the given parameters (see <a href="#2.5.2">&sect;2.5.2</a>).
  2938. Returns the previous mode (<code>LUA_GCGEN</code> or <code>LUA_GCINC</code>).
  2939. </li>
  2940. </ul><p>
  2941. For more details about these options,
  2942. see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
  2943. <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
  2944. <span class="apii">[-0, +0, &ndash;]</span>
  2945. <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
  2946. <p>
  2947. Returns the memory-allocation function of a given state.
  2948. If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
  2949. opaque pointer given when the memory-allocator function was set.
  2950. <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
  2951. <span class="apii">[-0, +1, <em>e</em>]</span>
  2952. <pre>int lua_getfield (lua_State *L, int index, const char *k);</pre>
  2953. <p>
  2954. Pushes onto the stack the value <code>t[k]</code>,
  2955. where <code>t</code> is the value at the given index.
  2956. As in Lua, this function may trigger a metamethod
  2957. for the "index" event (see <a href="#2.4">&sect;2.4</a>).
  2958. <p>
  2959. Returns the type of the pushed value.
  2960. <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
  2961. <span class="apii">[-0, +0, &ndash;]</span>
  2962. <pre>void *lua_getextraspace (lua_State *L);</pre>
  2963. <p>
  2964. Returns a pointer to a raw memory area associated with the
  2965. given Lua state.
  2966. The application can use this area for any purpose;
  2967. Lua does not use it for anything.
  2968. <p>
  2969. Each new thread has this area initialized with a copy
  2970. of the area of the main thread.
  2971. <p>
  2972. By default, this area has the size of a pointer to void,
  2973. but you can recompile Lua with a different size for this area.
  2974. (See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.)
  2975. <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
  2976. <span class="apii">[-0, +1, <em>e</em>]</span>
  2977. <pre>int lua_getglobal (lua_State *L, const char *name);</pre>
  2978. <p>
  2979. Pushes onto the stack the value of the global <code>name</code>.
  2980. Returns the type of that value.
  2981. <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p>
  2982. <span class="apii">[-0, +1, <em>e</em>]</span>
  2983. <pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre>
  2984. <p>
  2985. Pushes onto the stack the value <code>t[i]</code>,
  2986. where <code>t</code> is the value at the given index.
  2987. As in Lua, this function may trigger a metamethod
  2988. for the "index" event (see <a href="#2.4">&sect;2.4</a>).
  2989. <p>
  2990. Returns the type of the pushed value.
  2991. <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
  2992. <span class="apii">[-0, +(0|1), &ndash;]</span>
  2993. <pre>int lua_getmetatable (lua_State *L, int index);</pre>
  2994. <p>
  2995. If the value at the given index has a metatable,
  2996. the function pushes that metatable onto the stack and returns&nbsp;1.
  2997. Otherwise,
  2998. the function returns&nbsp;0 and pushes nothing on the stack.
  2999. <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
  3000. <span class="apii">[-1, +1, <em>e</em>]</span>
  3001. <pre>int lua_gettable (lua_State *L, int index);</pre>
  3002. <p>
  3003. Pushes onto the stack the value <code>t[k]</code>,
  3004. where <code>t</code> is the value at the given index
  3005. and <code>k</code> is the value on the top of the stack.
  3006. <p>
  3007. This function pops the key from the stack,
  3008. pushing the resulting value in its place.
  3009. As in Lua, this function may trigger a metamethod
  3010. for the "index" event (see <a href="#2.4">&sect;2.4</a>).
  3011. <p>
  3012. Returns the type of the pushed value.
  3013. <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
  3014. <span class="apii">[-0, +0, &ndash;]</span>
  3015. <pre>int lua_gettop (lua_State *L);</pre>
  3016. <p>
  3017. Returns the index of the top element in the stack.
  3018. Because indices start at&nbsp;1,
  3019. this result is equal to the number of elements in the stack;
  3020. in particular, 0&nbsp;means an empty stack.
  3021. <hr><h3><a name="lua_getiuservalue"><code>lua_getiuservalue</code></a></h3><p>
  3022. <span class="apii">[-0, +1, &ndash;]</span>
  3023. <pre>int lua_getiuservalue (lua_State *L, int index, int n);</pre>
  3024. <p>
  3025. Pushes onto the stack the <code>n</code>-th user value associated with the
  3026. full userdata at the given index and
  3027. returns the type of the pushed value.
  3028. <p>
  3029. If the userdata does not have that value,
  3030. pushes <b>nil</b> and returns <a href="#pdf-LUA_TNONE"><code>LUA_TNONE</code></a>.
  3031. <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
  3032. <span class="apii">[-1, +1, &ndash;]</span>
  3033. <pre>void lua_insert (lua_State *L, int index);</pre>
  3034. <p>
  3035. Moves the top element into the given valid index,
  3036. shifting up the elements above this index to open space.
  3037. This function cannot be called with a pseudo-index,
  3038. because a pseudo-index is not an actual stack position.
  3039. <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
  3040. <pre>typedef ... lua_Integer;</pre>
  3041. <p>
  3042. The type of integers in Lua.
  3043. <p>
  3044. By default this type is <code>long long</code>,
  3045. (usually a 64-bit two-complement integer),
  3046. but that can be changed to <code>long</code> or <code>int</code>
  3047. (usually a 32-bit two-complement integer).
  3048. (See <code>LUA_INT_TYPE</code> in <code>luaconf.h</code>.)
  3049. <p>
  3050. Lua also defines the constants
  3051. <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>,
  3052. with the minimum and the maximum values that fit in this type.
  3053. <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
  3054. <span class="apii">[-0, +0, &ndash;]</span>
  3055. <pre>int lua_isboolean (lua_State *L, int index);</pre>
  3056. <p>
  3057. Returns 1 if the value at the given index is a boolean,
  3058. and 0&nbsp;otherwise.
  3059. <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
  3060. <span class="apii">[-0, +0, &ndash;]</span>
  3061. <pre>int lua_iscfunction (lua_State *L, int index);</pre>
  3062. <p>
  3063. Returns 1 if the value at the given index is a C&nbsp;function,
  3064. and 0&nbsp;otherwise.
  3065. <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
  3066. <span class="apii">[-0, +0, &ndash;]</span>
  3067. <pre>int lua_isfunction (lua_State *L, int index);</pre>
  3068. <p>
  3069. Returns 1 if the value at the given index is a function
  3070. (either C or Lua), and 0&nbsp;otherwise.
  3071. <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p>
  3072. <span class="apii">[-0, +0, &ndash;]</span>
  3073. <pre>int lua_isinteger (lua_State *L, int index);</pre>
  3074. <p>
  3075. Returns 1 if the value at the given index is an integer
  3076. (that is, the value is a number and is represented as an integer),
  3077. and 0&nbsp;otherwise.
  3078. <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
  3079. <span class="apii">[-0, +0, &ndash;]</span>
  3080. <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
  3081. <p>
  3082. Returns 1 if the value at the given index is a light userdata,
  3083. and 0&nbsp;otherwise.
  3084. <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
  3085. <span class="apii">[-0, +0, &ndash;]</span>
  3086. <pre>int lua_isnil (lua_State *L, int index);</pre>
  3087. <p>
  3088. Returns 1 if the value at the given index is <b>nil</b>,
  3089. and 0&nbsp;otherwise.
  3090. <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
  3091. <span class="apii">[-0, +0, &ndash;]</span>
  3092. <pre>int lua_isnone (lua_State *L, int index);</pre>
  3093. <p>
  3094. Returns 1 if the given index is not valid,
  3095. and 0&nbsp;otherwise.
  3096. <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
  3097. <span class="apii">[-0, +0, &ndash;]</span>
  3098. <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
  3099. <p>
  3100. Returns 1 if the given index is not valid
  3101. or if the value at this index is <b>nil</b>,
  3102. and 0&nbsp;otherwise.
  3103. <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
  3104. <span class="apii">[-0, +0, &ndash;]</span>
  3105. <pre>int lua_isnumber (lua_State *L, int index);</pre>
  3106. <p>
  3107. Returns 1 if the value at the given index is a number
  3108. or a string convertible to a number,
  3109. and 0&nbsp;otherwise.
  3110. <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
  3111. <span class="apii">[-0, +0, &ndash;]</span>
  3112. <pre>int lua_isstring (lua_State *L, int index);</pre>
  3113. <p>
  3114. Returns 1 if the value at the given index is a string
  3115. or a number (which is always convertible to a string),
  3116. and 0&nbsp;otherwise.
  3117. <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
  3118. <span class="apii">[-0, +0, &ndash;]</span>
  3119. <pre>int lua_istable (lua_State *L, int index);</pre>
  3120. <p>
  3121. Returns 1 if the value at the given index is a table,
  3122. and 0&nbsp;otherwise.
  3123. <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
  3124. <span class="apii">[-0, +0, &ndash;]</span>
  3125. <pre>int lua_isthread (lua_State *L, int index);</pre>
  3126. <p>
  3127. Returns 1 if the value at the given index is a thread,
  3128. and 0&nbsp;otherwise.
  3129. <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
  3130. <span class="apii">[-0, +0, &ndash;]</span>
  3131. <pre>int lua_isuserdata (lua_State *L, int index);</pre>
  3132. <p>
  3133. Returns 1 if the value at the given index is a userdata
  3134. (either full or light), and 0&nbsp;otherwise.
  3135. <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
  3136. <span class="apii">[-0, +0, &ndash;]</span>
  3137. <pre>int lua_isyieldable (lua_State *L);</pre>
  3138. <p>
  3139. Returns 1 if the given coroutine can yield,
  3140. and 0&nbsp;otherwise.
  3141. <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3>
  3142. <pre>typedef ... lua_KContext;</pre>
  3143. <p>
  3144. The type for continuation-function contexts.
  3145. It must be a numeric type.
  3146. This type is defined as <code>intptr_t</code>
  3147. when <code>intptr_t</code> is available,
  3148. so that it can store pointers too.
  3149. Otherwise, it is defined as <code>ptrdiff_t</code>.
  3150. <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
  3151. <pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre>
  3152. <p>
  3153. Type for continuation functions (see <a href="#4.5">&sect;4.5</a>).
  3154. <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
  3155. <span class="apii">[-0, +1, <em>e</em>]</span>
  3156. <pre>void lua_len (lua_State *L, int index);</pre>
  3157. <p>
  3158. Returns the length of the value at the given index.
  3159. It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>) and
  3160. may trigger a metamethod for the "length" event (see <a href="#2.4">&sect;2.4</a>).
  3161. The result is pushed on the stack.
  3162. <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
  3163. <span class="apii">[-0, +1, &ndash;]</span>
  3164. <pre>int lua_load (lua_State *L,
  3165. lua_Reader reader,
  3166. void *data,
  3167. const char *chunkname,
  3168. const char *mode);</pre>
  3169. <p>
  3170. Loads a Lua chunk without running it.
  3171. If there are no errors,
  3172. <code>lua_load</code> pushes the compiled chunk as a Lua
  3173. function on top of the stack.
  3174. Otherwise, it pushes an error message.
  3175. <p>
  3176. The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
  3177. to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
  3178. The <code>data</code> argument is an opaque value passed to the reader function.
  3179. <p>
  3180. The <code>chunkname</code> argument gives a name to the chunk,
  3181. which is used for error messages and in debug information (see <a href="#4.7">&sect;4.7</a>).
  3182. <p>
  3183. <code>lua_load</code> automatically detects whether the chunk is text or binary
  3184. and loads it accordingly (see program <code>luac</code>).
  3185. The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
  3186. with the addition that
  3187. a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
  3188. <p>
  3189. <code>lua_load</code> uses the stack internally,
  3190. so the reader function must always leave the stack
  3191. unmodified when returning.
  3192. <p>
  3193. <code>lua_load</code> can return
  3194. <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>, <a href="#pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>, or <a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>.
  3195. The function may also return other values corresponding to
  3196. errors raised by the read function (see <a href="#4.4.1">&sect;4.4.1</a>).
  3197. <p>
  3198. If the resulting function has upvalues,
  3199. its first upvalue is set to the value of the global environment
  3200. stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.3">&sect;4.3</a>).
  3201. When loading main chunks,
  3202. this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
  3203. Other upvalues are initialized with <b>nil</b>.
  3204. <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
  3205. <span class="apii">[-0, +0, &ndash;]</span>
  3206. <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
  3207. <p>
  3208. Creates a new independent state and returns its main thread.
  3209. Returns <code>NULL</code> if it cannot create the state
  3210. (due to lack of memory).
  3211. The argument <code>f</code> is the allocator function;
  3212. Lua will do all memory allocation for this state
  3213. through this function (see <a href="#lua_Alloc"><code>lua_Alloc</code></a>).
  3214. The second argument, <code>ud</code>, is an opaque pointer that Lua
  3215. passes to the allocator in every call.
  3216. <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
  3217. <span class="apii">[-0, +1, <em>m</em>]</span>
  3218. <pre>void lua_newtable (lua_State *L);</pre>
  3219. <p>
  3220. Creates a new empty table and pushes it onto the stack.
  3221. It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
  3222. <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
  3223. <span class="apii">[-0, +1, <em>m</em>]</span>
  3224. <pre>lua_State *lua_newthread (lua_State *L);</pre>
  3225. <p>
  3226. Creates a new thread, pushes it on the stack,
  3227. and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
  3228. The new thread returned by this function shares with the original thread
  3229. its global environment,
  3230. but has an independent execution stack.
  3231. <p>
  3232. Threads are subject to garbage collection,
  3233. like any Lua object.
  3234. <hr><h3><a name="lua_newuserdatauv"><code>lua_newuserdatauv</code></a></h3><p>
  3235. <span class="apii">[-0, +1, <em>m</em>]</span>
  3236. <pre>void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue);</pre>
  3237. <p>
  3238. This function creates and pushes on the stack a new full userdata,
  3239. with <code>nuvalue</code> associated Lua values, called <code>user values</code>,
  3240. plus an associated block of raw memory with <code>size</code> bytes.
  3241. (The user values can be set and read with the functions
  3242. <a href="#lua_setiuservalue"><code>lua_setiuservalue</code></a> and <a href="#lua_getiuservalue"><code>lua_getiuservalue</code></a>.)
  3243. <p>
  3244. The function returns the address of the block of memory.
  3245. <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
  3246. <span class="apii">[-1, +(2|0), <em>v</em>]</span>
  3247. <pre>int lua_next (lua_State *L, int index);</pre>
  3248. <p>
  3249. Pops a key from the stack,
  3250. and pushes a key&ndash;value pair from the table at the given index,
  3251. the "next" pair after the given key.
  3252. If there are no more elements in the table,
  3253. then <a href="#lua_next"><code>lua_next</code></a> returns 0 and pushes nothing.
  3254. <p>
  3255. A typical table traversal looks like this:
  3256. <pre>
  3257. /* table is in the stack at index 't' */
  3258. lua_pushnil(L); /* first key */
  3259. while (lua_next(L, t) != 0) {
  3260. /* uses 'key' (at index -2) and 'value' (at index -1) */
  3261. printf("%s - %s\n",
  3262. lua_typename(L, lua_type(L, -2)),
  3263. lua_typename(L, lua_type(L, -1)));
  3264. /* removes 'value'; keeps 'key' for next iteration */
  3265. lua_pop(L, 1);
  3266. }
  3267. </pre>
  3268. <p>
  3269. While traversing a table,
  3270. avoid calling <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
  3271. unless you know that the key is actually a string.
  3272. Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
  3273. the value at the given index;
  3274. this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
  3275. <p>
  3276. This function may raise an error if the given key
  3277. is neither <b>nil</b> nor present in the table.
  3278. See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
  3279. the table during its traversal.
  3280. <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
  3281. <pre>typedef ... lua_Number;</pre>
  3282. <p>
  3283. The type of floats in Lua.
  3284. <p>
  3285. By default this type is double,
  3286. but that can be changed to a single float or a long double.
  3287. (See <code>LUA_FLOAT_TYPE</code> in <code>luaconf.h</code>.)
  3288. <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3>
  3289. <pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre>
  3290. <p>
  3291. Tries to convert a Lua float to a Lua integer;
  3292. the float <code>n</code> must have an integral value.
  3293. If that value is within the range of Lua integers,
  3294. it is converted to an integer and assigned to <code>*p</code>.
  3295. The macro results in a boolean indicating whether the
  3296. conversion was successful.
  3297. (Note that this range test can be tricky to do
  3298. correctly without this macro, due to rounding.)
  3299. <p>
  3300. This macro may evaluate its arguments more than once.
  3301. <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
  3302. <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
  3303. <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
  3304. <p>
  3305. Calls a function (or a callable object) in protected mode.
  3306. <p>
  3307. Both <code>nargs</code> and <code>nresults</code> have the same meaning as
  3308. in <a href="#lua_call"><code>lua_call</code></a>.
  3309. If there are no errors during the call,
  3310. <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
  3311. However, if there is any error,
  3312. <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
  3313. pushes a single value on the stack (the error object),
  3314. and returns an error code.
  3315. Like <a href="#lua_call"><code>lua_call</code></a>,
  3316. <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
  3317. and its arguments from the stack.
  3318. <p>
  3319. If <code>msgh</code> is 0,
  3320. then the error object returned on the stack
  3321. is exactly the original error object.
  3322. Otherwise, <code>msgh</code> is the stack index of a
  3323. <em>message handler</em>.
  3324. (This index cannot be a pseudo-index.)
  3325. In case of runtime errors,
  3326. this handler will be called with the error object
  3327. and its return value will be the object
  3328. returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
  3329. <p>
  3330. Typically, the message handler is used to add more debug
  3331. information to the error object, such as a stack traceback.
  3332. Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
  3333. since by then the stack has unwound.
  3334. <p>
  3335. The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following status codes:
  3336. <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>, <a href="#pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>, <a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>, or <a href="#pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>.
  3337. <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
  3338. <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
  3339. <pre>int lua_pcallk (lua_State *L,
  3340. int nargs,
  3341. int nresults,
  3342. int msgh,
  3343. lua_KContext ctx,
  3344. lua_KFunction k);</pre>
  3345. <p>
  3346. This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
  3347. except that it allows the called function to yield (see <a href="#4.5">&sect;4.5</a>).
  3348. <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
  3349. <span class="apii">[-n, +0, &ndash;]</span>
  3350. <pre>void lua_pop (lua_State *L, int n);</pre>
  3351. <p>
  3352. Pops <code>n</code> elements from the stack.
  3353. <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
  3354. <span class="apii">[-0, +1, &ndash;]</span>
  3355. <pre>void lua_pushboolean (lua_State *L, int b);</pre>
  3356. <p>
  3357. Pushes a boolean value with value <code>b</code> onto the stack.
  3358. <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
  3359. <span class="apii">[-n, +1, <em>m</em>]</span>
  3360. <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
  3361. <p>
  3362. Pushes a new C&nbsp;closure onto the stack.
  3363. This function receives a pointer to a C&nbsp;function
  3364. and pushes onto the stack a Lua value of type <code>function</code> that,
  3365. when called, invokes the corresponding C&nbsp;function.
  3366. The parameter <code>n</code> tells how many upvalues this function will have
  3367. (see <a href="#4.2">&sect;4.2</a>).
  3368. <p>
  3369. Any function to be callable by Lua must
  3370. follow the correct protocol to receive its parameters
  3371. and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  3372. <p>
  3373. When a C&nbsp;function is created,
  3374. it is possible to associate some values with it,
  3375. the so called upvalues;
  3376. these upvalues are then accessible to the function whenever it is called.
  3377. This association is called a C&nbsp;closure (see <a href="#4.2">&sect;4.2</a>).
  3378. To create a C&nbsp;closure,
  3379. first the initial values for its upvalues must be pushed onto the stack.
  3380. (When there are multiple upvalues, the first value is pushed first.)
  3381. Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
  3382. is called to create and push the C&nbsp;function onto the stack,
  3383. with the argument <code>n</code> telling how many values will be
  3384. associated with the function.
  3385. <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
  3386. <p>
  3387. The maximum value for <code>n</code> is 255.
  3388. <p>
  3389. When <code>n</code> is zero,
  3390. this function creates a <em>light C&nbsp;function</em>,
  3391. which is just a pointer to the C&nbsp;function.
  3392. In that case, it never raises a memory error.
  3393. <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
  3394. <span class="apii">[-0, +1, &ndash;]</span>
  3395. <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
  3396. <p>
  3397. Pushes a C&nbsp;function onto the stack.
  3398. This function is equivalent to <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> with no upvalues.
  3399. <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
  3400. <span class="apii">[-0, +1, <em>v</em>]</span>
  3401. <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
  3402. <p>
  3403. Pushes onto the stack a formatted string
  3404. and returns a pointer to this string.
  3405. It is similar to the ISO&nbsp;C function <code>sprintf</code>,
  3406. but has two important differences.
  3407. First,
  3408. you do not have to allocate space for the result;
  3409. the result is a Lua string and Lua takes care of memory allocation
  3410. (and deallocation, through garbage collection).
  3411. Second,
  3412. the conversion specifiers are quite restricted.
  3413. There are no flags, widths, or precisions.
  3414. The conversion specifiers can only be
  3415. '<code>%%</code>' (inserts the character '<code>%</code>'),
  3416. '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
  3417. '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
  3418. '<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>),
  3419. '<code>%p</code>' (inserts a pointer),
  3420. '<code>%d</code>' (inserts an <code>int</code>),
  3421. '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and
  3422. '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence).
  3423. <p>
  3424. This function may raise errors due to memory overflow
  3425. or an invalid conversion specifier.
  3426. <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
  3427. <span class="apii">[-0, +1, &ndash;]</span>
  3428. <pre>void lua_pushglobaltable (lua_State *L);</pre>
  3429. <p>
  3430. Pushes the global environment onto the stack.
  3431. <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
  3432. <span class="apii">[-0, +1, &ndash;]</span>
  3433. <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
  3434. <p>
  3435. Pushes an integer with value <code>n</code> onto the stack.
  3436. <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
  3437. <span class="apii">[-0, +1, &ndash;]</span>
  3438. <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
  3439. <p>
  3440. Pushes a light userdata onto the stack.
  3441. <p>
  3442. Userdata represent C&nbsp;values in Lua.
  3443. A <em>light userdata</em> represents a pointer, a <code>void*</code>.
  3444. It is a value (like a number):
  3445. you do not create it, it has no individual metatable,
  3446. and it is not collected (as it was never created).
  3447. A light userdata is equal to "any"
  3448. light userdata with the same C&nbsp;address.
  3449. <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
  3450. <span class="apii">[-0, +1, <em>m</em>]</span>
  3451. <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre>
  3452. <p>
  3453. This macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>,
  3454. but should be used only when <code>s</code> is a literal string.
  3455. (Lua may optimize this case.)
  3456. <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
  3457. <span class="apii">[-0, +1, <em>m</em>]</span>
  3458. <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
  3459. <p>
  3460. Pushes the string pointed to by <code>s</code> with size <code>len</code>
  3461. onto the stack.
  3462. Lua will make or reuse an internal copy of the given string,
  3463. so the memory at <code>s</code> can be freed or reused immediately after
  3464. the function returns.
  3465. The string can contain any binary data,
  3466. including embedded zeros.
  3467. <p>
  3468. Returns a pointer to the internal copy of the string.
  3469. <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
  3470. <span class="apii">[-0, +1, &ndash;]</span>
  3471. <pre>void lua_pushnil (lua_State *L);</pre>
  3472. <p>
  3473. Pushes a nil value onto the stack.
  3474. <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
  3475. <span class="apii">[-0, +1, &ndash;]</span>
  3476. <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
  3477. <p>
  3478. Pushes a float with value <code>n</code> onto the stack.
  3479. <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
  3480. <span class="apii">[-0, +1, <em>m</em>]</span>
  3481. <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre>
  3482. <p>
  3483. Pushes the zero-terminated string pointed to by <code>s</code>
  3484. onto the stack.
  3485. Lua will make or reuse an internal copy of the given string,
  3486. so the memory at <code>s</code> can be freed or reused immediately after
  3487. the function returns.
  3488. <p>
  3489. Returns a pointer to the internal copy of the string.
  3490. <p>
  3491. If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>.
  3492. <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
  3493. <span class="apii">[-0, +1, &ndash;]</span>
  3494. <pre>int lua_pushthread (lua_State *L);</pre>
  3495. <p>
  3496. Pushes the thread represented by <code>L</code> onto the stack.
  3497. Returns 1 if this thread is the main thread of its state.
  3498. <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
  3499. <span class="apii">[-0, +1, &ndash;]</span>
  3500. <pre>void lua_pushvalue (lua_State *L, int index);</pre>
  3501. <p>
  3502. Pushes a copy of the element at the given index
  3503. onto the stack.
  3504. <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
  3505. <span class="apii">[-0, +1, <em>v</em>]</span>
  3506. <pre>const char *lua_pushvfstring (lua_State *L,
  3507. const char *fmt,
  3508. va_list argp);</pre>
  3509. <p>
  3510. Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
  3511. instead of a variable number of arguments.
  3512. <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
  3513. <span class="apii">[-0, +0, &ndash;]</span>
  3514. <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
  3515. <p>
  3516. Returns 1 if the two values in indices <code>index1</code> and
  3517. <code>index2</code> are primitively equal
  3518. (that is, equal without calling the <code>__eq</code> metamethod).
  3519. Otherwise returns&nbsp;0.
  3520. Also returns&nbsp;0 if any of the indices are not valid.
  3521. <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
  3522. <span class="apii">[-1, +1, &ndash;]</span>
  3523. <pre>int lua_rawget (lua_State *L, int index);</pre>
  3524. <p>
  3525. Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
  3526. (i.e., without metamethods).
  3527. <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
  3528. <span class="apii">[-0, +1, &ndash;]</span>
  3529. <pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre>
  3530. <p>
  3531. Pushes onto the stack the value <code>t[n]</code>,
  3532. where <code>t</code> is the table at the given index.
  3533. The access is raw,
  3534. that is, it does not use the <code>__index</code> metavalue.
  3535. <p>
  3536. Returns the type of the pushed value.
  3537. <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
  3538. <span class="apii">[-0, +1, &ndash;]</span>
  3539. <pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre>
  3540. <p>
  3541. Pushes onto the stack the value <code>t[k]</code>,
  3542. where <code>t</code> is the table at the given index and
  3543. <code>k</code> is the pointer <code>p</code> represented as a light userdata.
  3544. The access is raw;
  3545. that is, it does not use the <code>__index</code> metavalue.
  3546. <p>
  3547. Returns the type of the pushed value.
  3548. <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
  3549. <span class="apii">[-0, +0, &ndash;]</span>
  3550. <pre>lua_Unsigned lua_rawlen (lua_State *L, int index);</pre>
  3551. <p>
  3552. Returns the raw "length" of the value at the given index:
  3553. for strings, this is the string length;
  3554. for tables, this is the result of the length operator ('<code>#</code>')
  3555. with no metamethods;
  3556. for userdata, this is the size of the block of memory allocated
  3557. for the userdata.
  3558. For other values, this call returns&nbsp;0.
  3559. <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
  3560. <span class="apii">[-2, +0, <em>m</em>]</span>
  3561. <pre>void lua_rawset (lua_State *L, int index);</pre>
  3562. <p>
  3563. Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
  3564. (i.e., without metamethods).
  3565. <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
  3566. <span class="apii">[-1, +0, <em>m</em>]</span>
  3567. <pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre>
  3568. <p>
  3569. Does the equivalent of <code>t[i] = v</code>,
  3570. where <code>t</code> is the table at the given index
  3571. and <code>v</code> is the value on the top of the stack.
  3572. <p>
  3573. This function pops the value from the stack.
  3574. The assignment is raw,
  3575. that is, it does not use the <code>__newindex</code> metavalue.
  3576. <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
  3577. <span class="apii">[-1, +0, <em>m</em>]</span>
  3578. <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
  3579. <p>
  3580. Does the equivalent of <code>t[p] = v</code>,
  3581. where <code>t</code> is the table at the given index,
  3582. <code>p</code> is encoded as a light userdata,
  3583. and <code>v</code> is the value on the top of the stack.
  3584. <p>
  3585. This function pops the value from the stack.
  3586. The assignment is raw,
  3587. that is, it does not use the <code>__newindex</code> metavalue.
  3588. <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
  3589. <pre>typedef const char * (*lua_Reader) (lua_State *L,
  3590. void *data,
  3591. size_t *size);</pre>
  3592. <p>
  3593. The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
  3594. Every time <a href="#lua_load"><code>lua_load</code></a> needs another piece of the chunk,
  3595. it calls the reader,
  3596. passing along its <code>data</code> parameter.
  3597. The reader must return a pointer to a block of memory
  3598. with a new piece of the chunk
  3599. and set <code>size</code> to the block size.
  3600. The block must exist until the reader function is called again.
  3601. To signal the end of the chunk,
  3602. the reader must return <code>NULL</code> or set <code>size</code> to zero.
  3603. The reader function may return pieces of any size greater than zero.
  3604. <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
  3605. <span class="apii">[-0, +0, <em>e</em>]</span>
  3606. <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre>
  3607. <p>
  3608. Sets the C&nbsp;function <code>f</code> as the new value of global <code>name</code>.
  3609. It is defined as a macro:
  3610. <pre>
  3611. #define lua_register(L,n,f) \
  3612. (lua_pushcfunction(L, f), lua_setglobal(L, n))
  3613. </pre>
  3614. <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
  3615. <span class="apii">[-1, +0, &ndash;]</span>
  3616. <pre>void lua_remove (lua_State *L, int index);</pre>
  3617. <p>
  3618. Removes the element at the given valid index,
  3619. shifting down the elements above this index to fill the gap.
  3620. This function cannot be called with a pseudo-index,
  3621. because a pseudo-index is not an actual stack position.
  3622. <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
  3623. <span class="apii">[-1, +0, &ndash;]</span>
  3624. <pre>void lua_replace (lua_State *L, int index);</pre>
  3625. <p>
  3626. Moves the top element into the given valid index
  3627. without shifting any element
  3628. (therefore replacing the value at that given index),
  3629. and then pops the top element.
  3630. <hr><h3><a name="lua_resetthread"><code>lua_resetthread</code></a></h3><p>
  3631. <span class="apii">[-0, +?, &ndash;]</span>
  3632. <pre>int lua_resetthread (lua_State *L);</pre>
  3633. <p>
  3634. Resets a thread, cleaning its call stack and closing all pending
  3635. to-be-closed variables.
  3636. Returns a status code:
  3637. <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> for no errors in closing methods,
  3638. or an error status otherwise.
  3639. In case of error,
  3640. leaves the error object on the top of the stack,
  3641. <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
  3642. <span class="apii">[-?, +?, &ndash;]</span>
  3643. <pre>int lua_resume (lua_State *L, lua_State *from, int nargs,
  3644. int *nresults);</pre>
  3645. <p>
  3646. Starts and resumes a coroutine in the given thread <code>L</code>.
  3647. <p>
  3648. To start a coroutine,
  3649. you push the main function plus any arguments
  3650. onto the empty stack of the thread.
  3651. then you call <a href="#lua_resume"><code>lua_resume</code></a>,
  3652. with <code>nargs</code> being the number of arguments.
  3653. This call returns when the coroutine suspends or finishes its execution.
  3654. When it returns,
  3655. <code>*nresults</code> is updated and
  3656. the top of the stack contains
  3657. the <code>*nresults</code> values passed to <a href="#lua_yield"><code>lua_yield</code></a>
  3658. or returned by the body function.
  3659. <a href="#lua_resume"><code>lua_resume</code></a> returns
  3660. <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
  3661. <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
  3662. without errors,
  3663. or an error code in case of errors (see <a href="#4.4.1">&sect;4.4.1</a>).
  3664. In case of errors,
  3665. the error object is on the top of the stack.
  3666. <p>
  3667. To resume a coroutine,
  3668. you remove the <code>*nresults</code> yielded values from its stack,
  3669. push the values to be passed as results from <code>yield</code>,
  3670. and then call <a href="#lua_resume"><code>lua_resume</code></a>.
  3671. <p>
  3672. The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
  3673. If there is no such coroutine,
  3674. this parameter can be <code>NULL</code>.
  3675. <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
  3676. <span class="apii">[-0, +0, &ndash;]</span>
  3677. <pre>void lua_rotate (lua_State *L, int idx, int n);</pre>
  3678. <p>
  3679. Rotates the stack elements between the valid index <code>idx</code>
  3680. and the top of the stack.
  3681. The elements are rotated <code>n</code> positions in the direction of the top,
  3682. for a positive <code>n</code>,
  3683. or <code>-n</code> positions in the direction of the bottom,
  3684. for a negative <code>n</code>.
  3685. The absolute value of <code>n</code> must not be greater than the size
  3686. of the slice being rotated.
  3687. This function cannot be called with a pseudo-index,
  3688. because a pseudo-index is not an actual stack position.
  3689. <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
  3690. <span class="apii">[-0, +0, &ndash;]</span>
  3691. <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
  3692. <p>
  3693. Changes the allocator function of a given state to <code>f</code>
  3694. with user data <code>ud</code>.
  3695. <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
  3696. <span class="apii">[-1, +0, <em>e</em>]</span>
  3697. <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
  3698. <p>
  3699. Does the equivalent to <code>t[k] = v</code>,
  3700. where <code>t</code> is the value at the given index
  3701. and <code>v</code> is the value on the top of the stack.
  3702. <p>
  3703. This function pops the value from the stack.
  3704. As in Lua, this function may trigger a metamethod
  3705. for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
  3706. <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
  3707. <span class="apii">[-1, +0, <em>e</em>]</span>
  3708. <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
  3709. <p>
  3710. Pops a value from the stack and
  3711. sets it as the new value of global <code>name</code>.
  3712. <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p>
  3713. <span class="apii">[-1, +0, <em>e</em>]</span>
  3714. <pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre>
  3715. <p>
  3716. Does the equivalent to <code>t[n] = v</code>,
  3717. where <code>t</code> is the value at the given index
  3718. and <code>v</code> is the value on the top of the stack.
  3719. <p>
  3720. This function pops the value from the stack.
  3721. As in Lua, this function may trigger a metamethod
  3722. for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
  3723. <hr><h3><a name="lua_setiuservalue"><code>lua_setiuservalue</code></a></h3><p>
  3724. <span class="apii">[-1, +0, &ndash;]</span>
  3725. <pre>int lua_setiuservalue (lua_State *L, int index, int n);</pre>
  3726. <p>
  3727. Pops a value from the stack and sets it as
  3728. the new <code>n</code>-th user value associated to the
  3729. full userdata at the given index.
  3730. Returns 0 if the userdata does not have that value.
  3731. <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
  3732. <span class="apii">[-1, +0, &ndash;]</span>
  3733. <pre>int lua_setmetatable (lua_State *L, int index);</pre>
  3734. <p>
  3735. Pops a table or <b>nil</b> from the stack and
  3736. sets that value as the new metatable for the value at the given index.
  3737. (<b>nil</b> means no metatable.)
  3738. <p>
  3739. (For historical reasons, this function returns an <code>int</code>,
  3740. which now is always 1.)
  3741. <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
  3742. <span class="apii">[-2, +0, <em>e</em>]</span>
  3743. <pre>void lua_settable (lua_State *L, int index);</pre>
  3744. <p>
  3745. Does the equivalent to <code>t[k] = v</code>,
  3746. where <code>t</code> is the value at the given index,
  3747. <code>v</code> is the value on the top of the stack,
  3748. and <code>k</code> is the value just below the top.
  3749. <p>
  3750. This function pops both the key and the value from the stack.
  3751. As in Lua, this function may trigger a metamethod
  3752. for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
  3753. <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
  3754. <span class="apii">[-?, +?, &ndash;]</span>
  3755. <pre>void lua_settop (lua_State *L, int index);</pre>
  3756. <p>
  3757. Accepts any index, or&nbsp;0,
  3758. and sets the stack top to this index.
  3759. If the new top is greater than the old one,
  3760. then the new elements are filled with <b>nil</b>.
  3761. If <code>index</code> is&nbsp;0, then all stack elements are removed.
  3762. <hr><h3><a name="lua_setwarnf"><code>lua_setwarnf</code></a></h3><p>
  3763. <span class="apii">[-0, +0, &ndash;]</span>
  3764. <pre>void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);</pre>
  3765. <p>
  3766. Sets the warning function to be used by Lua to emit warnings
  3767. (see <a href="#lua_WarnFunction"><code>lua_WarnFunction</code></a>).
  3768. The <code>ud</code> parameter sets the value <code>ud</code> passed to
  3769. the warning function.
  3770. <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
  3771. <pre>typedef struct lua_State lua_State;</pre>
  3772. <p>
  3773. An opaque structure that points to a thread and indirectly
  3774. (through the thread) to the whole state of a Lua interpreter.
  3775. The Lua library is fully reentrant:
  3776. it has no global variables.
  3777. All information about a state is accessible through this structure.
  3778. <p>
  3779. A pointer to this structure must be passed as the first argument to
  3780. every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
  3781. which creates a Lua state from scratch.
  3782. <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
  3783. <span class="apii">[-0, +0, &ndash;]</span>
  3784. <pre>int lua_status (lua_State *L);</pre>
  3785. <p>
  3786. Returns the status of the thread <code>L</code>.
  3787. <p>
  3788. The status can be <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> for a normal thread,
  3789. an error code if the thread finished the execution
  3790. of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
  3791. or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
  3792. <p>
  3793. You can call functions only in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
  3794. You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
  3795. (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
  3796. (to resume a coroutine).
  3797. <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p>
  3798. <span class="apii">[-0, +1, &ndash;]</span>
  3799. <pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre>
  3800. <p>
  3801. Converts the zero-terminated string <code>s</code> to a number,
  3802. pushes that number into the stack,
  3803. and returns the total size of the string,
  3804. that is, its length plus one.
  3805. The conversion can result in an integer or a float,
  3806. according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
  3807. The string may have leading and trailing whitespaces and a sign.
  3808. If the string is not a valid numeral,
  3809. returns 0 and pushes nothing.
  3810. (Note that the result can be used as a boolean,
  3811. true if the conversion succeeds.)
  3812. <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
  3813. <span class="apii">[-0, +0, &ndash;]</span>
  3814. <pre>int lua_toboolean (lua_State *L, int index);</pre>
  3815. <p>
  3816. Converts the Lua value at the given index to a C&nbsp;boolean
  3817. value (0&nbsp;or&nbsp;1).
  3818. Like all tests in Lua,
  3819. <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
  3820. different from <b>false</b> and <b>nil</b>;
  3821. otherwise it returns false.
  3822. (If you want to accept only actual boolean values,
  3823. use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
  3824. <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
  3825. <span class="apii">[-0, +0, &ndash;]</span>
  3826. <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
  3827. <p>
  3828. Converts a value at the given index to a C&nbsp;function.
  3829. That value must be a C&nbsp;function;
  3830. otherwise, returns <code>NULL</code>.
  3831. <hr><h3><a name="lua_toclose"><code>lua_toclose</code></a></h3><p>
  3832. <span class="apii">[-0, +0, <em>v</em>]</span>
  3833. <pre>void lua_toclose (lua_State *L, int index);</pre>
  3834. <p>
  3835. Marks the given index in the stack as a
  3836. to-be-closed "variable" (see <a href="#3.3.8">&sect;3.3.8</a>).
  3837. Like a to-be-closed variable in Lua,
  3838. the value at that index in the stack will be closed
  3839. when it goes out of scope.
  3840. Here, in the context of a C function,
  3841. to go out of scope means that the running function returns to Lua,
  3842. there is an error,
  3843. or the index is removed from the stack through
  3844. <a href="#lua_settop"><code>lua_settop</code></a> or <a href="#lua_pop"><code>lua_pop</code></a>.
  3845. An index marked as to-be-closed should not be removed from the stack
  3846. by any other function in the API except <a href="#lua_settop"><code>lua_settop</code></a> or <a href="#lua_pop"><code>lua_pop</code></a>.
  3847. <p>
  3848. This function should not be called for an index
  3849. that is equal to or below an active to-be-closed index.
  3850. <p>
  3851. This function can raise an out-of-memory error.
  3852. In that case, the value in the given index is immediately closed,
  3853. as if it was already marked.
  3854. <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
  3855. <span class="apii">[-0, +0, &ndash;]</span>
  3856. <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
  3857. <p>
  3858. Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
  3859. <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
  3860. <span class="apii">[-0, +0, &ndash;]</span>
  3861. <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
  3862. <p>
  3863. Converts the Lua value at the given index
  3864. to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
  3865. The Lua value must be an integer,
  3866. or a number or string convertible to an integer (see <a href="#3.4.3">&sect;3.4.3</a>);
  3867. otherwise, <code>lua_tointegerx</code> returns&nbsp;0.
  3868. <p>
  3869. If <code>isnum</code> is not <code>NULL</code>,
  3870. its referent is assigned a boolean value that
  3871. indicates whether the operation succeeded.
  3872. <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
  3873. <span class="apii">[-0, +0, <em>m</em>]</span>
  3874. <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
  3875. <p>
  3876. Converts the Lua value at the given index to a C&nbsp;string.
  3877. If <code>len</code> is not <code>NULL</code>,
  3878. it sets <code>*len</code> with the string length.
  3879. The Lua value must be a string or a number;
  3880. otherwise, the function returns <code>NULL</code>.
  3881. If the value is a number,
  3882. then <code>lua_tolstring</code> also
  3883. <em>changes the actual value in the stack to a string</em>.
  3884. (This change confuses <a href="#lua_next"><code>lua_next</code></a>
  3885. when <code>lua_tolstring</code> is applied to keys during a table traversal.)
  3886. <p>
  3887. <code>lua_tolstring</code> returns a pointer
  3888. to a string inside the Lua state.
  3889. This string always has a zero ('<code>\0</code>')
  3890. after its last character (as in&nbsp;C),
  3891. but can contain other zeros in its body.
  3892. <p>
  3893. Because Lua has garbage collection,
  3894. there is no guarantee that the pointer returned by <code>lua_tolstring</code>
  3895. will be valid after the corresponding Lua value is removed from the stack.
  3896. <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
  3897. <span class="apii">[-0, +0, &ndash;]</span>
  3898. <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
  3899. <p>
  3900. Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
  3901. <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
  3902. <span class="apii">[-0, +0, &ndash;]</span>
  3903. <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
  3904. <p>
  3905. Converts the Lua value at the given index
  3906. to the C&nbsp;type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
  3907. The Lua value must be a number or a string convertible to a number
  3908. (see <a href="#3.4.3">&sect;3.4.3</a>);
  3909. otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns&nbsp;0.
  3910. <p>
  3911. If <code>isnum</code> is not <code>NULL</code>,
  3912. its referent is assigned a boolean value that
  3913. indicates whether the operation succeeded.
  3914. <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
  3915. <span class="apii">[-0, +0, &ndash;]</span>
  3916. <pre>const void *lua_topointer (lua_State *L, int index);</pre>
  3917. <p>
  3918. Converts the value at the given index to a generic
  3919. C&nbsp;pointer (<code>void*</code>).
  3920. The value can be a userdata, a table, a thread, a string, or a function;
  3921. otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
  3922. Different objects will give different pointers.
  3923. There is no way to convert the pointer back to its original value.
  3924. <p>
  3925. Typically this function is used only for hashing and debug information.
  3926. <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
  3927. <span class="apii">[-0, +0, <em>m</em>]</span>
  3928. <pre>const char *lua_tostring (lua_State *L, int index);</pre>
  3929. <p>
  3930. Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
  3931. <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
  3932. <span class="apii">[-0, +0, &ndash;]</span>
  3933. <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
  3934. <p>
  3935. Converts the value at the given index to a Lua thread
  3936. (represented as <code>lua_State*</code>).
  3937. This value must be a thread;
  3938. otherwise, the function returns <code>NULL</code>.
  3939. <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
  3940. <span class="apii">[-0, +0, &ndash;]</span>
  3941. <pre>void *lua_touserdata (lua_State *L, int index);</pre>
  3942. <p>
  3943. If the value at the given index is a full userdata,
  3944. returns its memory-block address.
  3945. If the value is a light userdata,
  3946. returns its value (a pointer).
  3947. Otherwise, returns <code>NULL</code>.
  3948. <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
  3949. <span class="apii">[-0, +0, &ndash;]</span>
  3950. <pre>int lua_type (lua_State *L, int index);</pre>
  3951. <p>
  3952. Returns the type of the value in the given valid index,
  3953. or <code>LUA_TNONE</code> for a non-valid but acceptable index.
  3954. The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
  3955. defined in <code>lua.h</code>:
  3956. <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>,
  3957. <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
  3958. <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
  3959. <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
  3960. <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
  3961. <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
  3962. <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
  3963. <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
  3964. and
  3965. <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
  3966. <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
  3967. <span class="apii">[-0, +0, &ndash;]</span>
  3968. <pre>const char *lua_typename (lua_State *L, int tp);</pre>
  3969. <p>
  3970. Returns the name of the type encoded by the value <code>tp</code>,
  3971. which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
  3972. <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
  3973. <pre>typedef ... lua_Unsigned;</pre>
  3974. <p>
  3975. The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
  3976. <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
  3977. <span class="apii">[-0, +0, &ndash;]</span>
  3978. <pre>int lua_upvalueindex (int i);</pre>
  3979. <p>
  3980. Returns the pseudo-index that represents the <code>i</code>-th upvalue of
  3981. the running function (see <a href="#4.2">&sect;4.2</a>).
  3982. <code>i</code> must be in the range <em>[1,256]</em>.
  3983. <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
  3984. <span class="apii">[-0, +0, &ndash;]</span>
  3985. <pre>lua_Number lua_version (lua_State *L);</pre>
  3986. <p>
  3987. Returns the version number of this core.
  3988. <hr><h3><a name="lua_WarnFunction"><code>lua_WarnFunction</code></a></h3>
  3989. <pre>typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);</pre>
  3990. <p>
  3991. The type of warning functions, called by Lua to emit warnings.
  3992. The first parameter is an opaque pointer
  3993. set by <a href="#lua_setwarnf"><code>lua_setwarnf</code></a>.
  3994. The second parameter is the warning message.
  3995. The third parameter is a boolean that
  3996. indicates whether the message is
  3997. to be continued by the message in the next call.
  3998. <p>
  3999. See <a href="#pdf-warn"><code>warn</code></a> for more details about warnings.
  4000. <hr><h3><a name="lua_warning"><code>lua_warning</code></a></h3><p>
  4001. <span class="apii">[-0, +0, &ndash;]</span>
  4002. <pre>void lua_warning (lua_State *L, const char *msg, int tocont);</pre>
  4003. <p>
  4004. Emits a warning with the given message.
  4005. A message in a call with <code>tocont</code> true should be
  4006. continued in another call to this function.
  4007. <p>
  4008. See <a href="#pdf-warn"><code>warn</code></a> for more details about warnings.
  4009. <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
  4010. <pre>typedef int (*lua_Writer) (lua_State *L,
  4011. const void* p,
  4012. size_t sz,
  4013. void* ud);</pre>
  4014. <p>
  4015. The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
  4016. Every time <a href="#lua_dump"><code>lua_dump</code></a> produces another piece of chunk,
  4017. it calls the writer,
  4018. passing along the buffer to be written (<code>p</code>),
  4019. its size (<code>sz</code>),
  4020. and the <code>ud</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
  4021. <p>
  4022. The writer returns an error code:
  4023. 0&nbsp;means no errors;
  4024. any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
  4025. calling the writer again.
  4026. <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
  4027. <span class="apii">[-?, +?, &ndash;]</span>
  4028. <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
  4029. <p>
  4030. Exchange values between different threads of the same state.
  4031. <p>
  4032. This function pops <code>n</code> values from the stack <code>from</code>,
  4033. and pushes them onto the stack <code>to</code>.
  4034. <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
  4035. <span class="apii">[-?, +?, <em>v</em>]</span>
  4036. <pre>int lua_yield (lua_State *L, int nresults);</pre>
  4037. <p>
  4038. This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  4039. but it has no continuation (see <a href="#4.5">&sect;4.5</a>).
  4040. Therefore, when the thread resumes,
  4041. it continues the function that called
  4042. the function calling <code>lua_yield</code>.
  4043. To avoid surprises,
  4044. this function should be called only in a tail call.
  4045. <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
  4046. <span class="apii">[-?, +?, <em>v</em>]</span>
  4047. <pre>int lua_yieldk (lua_State *L,
  4048. int nresults,
  4049. lua_KContext ctx,
  4050. lua_KFunction k);</pre>
  4051. <p>
  4052. Yields a coroutine (thread).
  4053. <p>
  4054. When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  4055. the running coroutine suspends its execution,
  4056. and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
  4057. The parameter <code>nresults</code> is the number of values from the stack
  4058. that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
  4059. <p>
  4060. When the coroutine is resumed again,
  4061. Lua calls the given continuation function <code>k</code> to continue
  4062. the execution of the C&nbsp;function that yielded (see <a href="#4.5">&sect;4.5</a>).
  4063. This continuation function receives the same stack
  4064. from the previous function,
  4065. with the <code>n</code> results removed and
  4066. replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
  4067. Moreover,
  4068. the continuation function receives the value <code>ctx</code>
  4069. that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
  4070. <p>
  4071. Usually, this function does not return;
  4072. when the coroutine eventually resumes,
  4073. it continues executing the continuation function.
  4074. However, there is one special case,
  4075. which is when this function is called
  4076. from inside a line or a count hook (see <a href="#4.7">&sect;4.7</a>).
  4077. In that case, <code>lua_yieldk</code> should be called with no continuation
  4078. (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>) and no results,
  4079. and the hook should return immediately after the call.
  4080. Lua will yield and,
  4081. when the coroutine resumes again,
  4082. it will continue the normal execution
  4083. of the (Lua) function that triggered the hook.
  4084. <p>
  4085. This function can raise an error if it is called from a thread
  4086. with a pending C call with no continuation function
  4087. (what is called a <em>C-call boundary</em>),
  4088. or it is called from a thread that is not running inside a resume
  4089. (typically the main thread).
  4090. <h2>4.7 &ndash; <a name="4.7">The Debug Interface</a></h2>
  4091. <p>
  4092. Lua has no built-in debugging facilities.
  4093. Instead, it offers a special interface
  4094. by means of functions and <em>hooks</em>.
  4095. This interface allows the construction of different
  4096. kinds of debuggers, profilers, and other tools
  4097. that need "inside information" from the interpreter.
  4098. <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
  4099. <pre>typedef struct lua_Debug {
  4100. int event;
  4101. const char *name; /* (n) */
  4102. const char *namewhat; /* (n) */
  4103. const char *what; /* (S) */
  4104. const char *source; /* (S) */
  4105. size_t srclen; /* (S) */
  4106. int currentline; /* (l) */
  4107. int linedefined; /* (S) */
  4108. int lastlinedefined; /* (S) */
  4109. unsigned char nups; /* (u) number of upvalues */
  4110. unsigned char nparams; /* (u) number of parameters */
  4111. char isvararg; /* (u) */
  4112. char istailcall; /* (t) */
  4113. unsigned short ftransfer; /* (r) index of first value transferred */
  4114. unsigned short ntransfer; /* (r) number of transferred values */
  4115. char short_src[LUA_IDSIZE]; /* (S) */
  4116. /* private part */
  4117. <em>other fields</em>
  4118. } lua_Debug;</pre>
  4119. <p>
  4120. A structure used to carry different pieces of
  4121. information about a function or an activation record.
  4122. <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
  4123. of this structure, for later use.
  4124. To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
  4125. you must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  4126. <p>
  4127. The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
  4128. <ul>
  4129. <li><b><code>source</code>: </b>
  4130. the source of the chunk that created the function.
  4131. If <code>source</code> starts with a '<code>@</code>',
  4132. it means that the function was defined in a file where
  4133. the file name follows the '<code>@</code>'.
  4134. If <code>source</code> starts with a '<code>=</code>',
  4135. the remainder of its contents describes the source in a user-dependent manner.
  4136. Otherwise,
  4137. the function was defined in a string where
  4138. <code>source</code> is that string.
  4139. </li>
  4140. <li><b><code>srclen</code>: </b>
  4141. The length of the string <code>source</code>.
  4142. </li>
  4143. <li><b><code>short_src</code>: </b>
  4144. a "printable" version of <code>source</code>, to be used in error messages.
  4145. </li>
  4146. <li><b><code>linedefined</code>: </b>
  4147. the line number where the definition of the function starts.
  4148. </li>
  4149. <li><b><code>lastlinedefined</code>: </b>
  4150. the line number where the definition of the function ends.
  4151. </li>
  4152. <li><b><code>what</code>: </b>
  4153. the string <code>"Lua"</code> if the function is a Lua function,
  4154. <code>"C"</code> if it is a C&nbsp;function,
  4155. <code>"main"</code> if it is the main part of a chunk.
  4156. </li>
  4157. <li><b><code>currentline</code>: </b>
  4158. the current line where the given function is executing.
  4159. When no line information is available,
  4160. <code>currentline</code> is set to -1.
  4161. </li>
  4162. <li><b><code>name</code>: </b>
  4163. a reasonable name for the given function.
  4164. Because functions in Lua are first-class values,
  4165. they do not have a fixed name:
  4166. some functions can be the value of multiple global variables,
  4167. while others can be stored only in a table field.
  4168. The <code>lua_getinfo</code> function checks how the function was
  4169. called to find a suitable name.
  4170. If it cannot find a name,
  4171. then <code>name</code> is set to <code>NULL</code>.
  4172. </li>
  4173. <li><b><code>namewhat</code>: </b>
  4174. explains the <code>name</code> field.
  4175. The value of <code>namewhat</code> can be
  4176. <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
  4177. <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
  4178. according to how the function was called.
  4179. (Lua uses the empty string when no other option seems to apply.)
  4180. </li>
  4181. <li><b><code>istailcall</code>: </b>
  4182. true if this function invocation was called by a tail call.
  4183. In this case, the caller of this level is not in the stack.
  4184. </li>
  4185. <li><b><code>nups</code>: </b>
  4186. the number of upvalues of the function.
  4187. </li>
  4188. <li><b><code>nparams</code>: </b>
  4189. the number of parameters of the function
  4190. (always 0&nbsp;for C&nbsp;functions).
  4191. </li>
  4192. <li><b><code>isvararg</code>: </b>
  4193. true if the function is a vararg function
  4194. (always true for C&nbsp;functions).
  4195. </li>
  4196. <li><b><code>ftransfer</code>: </b>
  4197. the index on the stack of the first value being "transferred",
  4198. that is, parameters in a call or return values in a return.
  4199. (The other values are in consecutive indices.)
  4200. Using this index, you can access and modify these values
  4201. through <a href="#lua_getlocal"><code>lua_getlocal</code></a> and <a href="#lua_setlocal"><code>lua_setlocal</code></a>.
  4202. This field is only meaningful during a
  4203. call hook, denoting the first parameter,
  4204. or a return hook, denoting the first value being returned.
  4205. (For call hooks, this value is always 1.)
  4206. </li>
  4207. <li><b><code>ntransfer</code>: </b>
  4208. The number of values being transferred (see previous item).
  4209. (For calls of Lua functions,
  4210. this value is always equal to <code>nparams</code>.)
  4211. </li>
  4212. </ul>
  4213. <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
  4214. <span class="apii">[-0, +0, &ndash;]</span>
  4215. <pre>lua_Hook lua_gethook (lua_State *L);</pre>
  4216. <p>
  4217. Returns the current hook function.
  4218. <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
  4219. <span class="apii">[-0, +0, &ndash;]</span>
  4220. <pre>int lua_gethookcount (lua_State *L);</pre>
  4221. <p>
  4222. Returns the current hook count.
  4223. <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
  4224. <span class="apii">[-0, +0, &ndash;]</span>
  4225. <pre>int lua_gethookmask (lua_State *L);</pre>
  4226. <p>
  4227. Returns the current hook mask.
  4228. <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
  4229. <span class="apii">[-(0|1), +(0|1|2), <em>m</em>]</span>
  4230. <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
  4231. <p>
  4232. Gets information about a specific function or function invocation.
  4233. <p>
  4234. To get information about a function invocation,
  4235. the parameter <code>ar</code> must be a valid activation record that was
  4236. filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
  4237. given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
  4238. <p>
  4239. To get information about a function, you push it onto the stack
  4240. and start the <code>what</code> string with the character '<code>&gt;</code>'.
  4241. (In that case,
  4242. <code>lua_getinfo</code> pops the function from the top of the stack.)
  4243. For instance, to know in which line a function <code>f</code> was defined,
  4244. you can write the following code:
  4245. <pre>
  4246. lua_Debug ar;
  4247. lua_getglobal(L, "f"); /* get global 'f' */
  4248. lua_getinfo(L, "&gt;S", &amp;ar);
  4249. printf("%d\n", ar.linedefined);
  4250. </pre>
  4251. <p>
  4252. Each character in the string <code>what</code>
  4253. selects some fields of the structure <code>ar</code> to be filled or
  4254. a value to be pushed on the stack:
  4255. <ul>
  4256. <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>;
  4257. </li>
  4258. <li><b>'<code>S</code>': </b>
  4259. fills in the fields <code>source</code>, <code>short_src</code>,
  4260. <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
  4261. </li>
  4262. <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>;
  4263. </li>
  4264. <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>;
  4265. </li>
  4266. <li><b>'<code>u</code>': </b> fills in the fields
  4267. <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>;
  4268. </li>
  4269. <li><b>'<code>f</code>': </b>
  4270. pushes onto the stack the function that is
  4271. running at the given level;
  4272. </li>
  4273. <li><b>'<code>L</code>': </b>
  4274. pushes onto the stack a table whose indices are the
  4275. numbers of the lines that are valid on the function.
  4276. (A <em>valid line</em> is a line with some associated code,
  4277. that is, a line where you can put a break point.
  4278. Non-valid lines include empty lines and comments.)
  4279. <p>
  4280. If this option is given together with option '<code>f</code>',
  4281. its table is pushed after the function.
  4282. <p>
  4283. This is the only option that can raise a memory error.
  4284. </li>
  4285. </ul>
  4286. <p>
  4287. This function returns 0 to signal an invalid option in <code>what</code>;
  4288. even then the valid options are handled correctly.
  4289. <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
  4290. <span class="apii">[-0, +(0|1), &ndash;]</span>
  4291. <pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
  4292. <p>
  4293. Gets information about a local variable or a temporary value
  4294. of a given activation record or a given function.
  4295. <p>
  4296. In the first case,
  4297. the parameter <code>ar</code> must be a valid activation record that was
  4298. filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
  4299. given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
  4300. The index <code>n</code> selects which local variable to inspect;
  4301. see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
  4302. and names.
  4303. <p>
  4304. <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
  4305. and returns its name.
  4306. <p>
  4307. In the second case, <code>ar</code> must be <code>NULL</code> and the function
  4308. to be inspected must be on the top of the stack.
  4309. In this case, only parameters of Lua functions are visible
  4310. (as there is no information about what variables are active)
  4311. and no values are pushed onto the stack.
  4312. <p>
  4313. Returns <code>NULL</code> (and pushes nothing)
  4314. when the index is greater than
  4315. the number of active local variables.
  4316. <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
  4317. <span class="apii">[-0, +0, &ndash;]</span>
  4318. <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
  4319. <p>
  4320. Gets information about the interpreter runtime stack.
  4321. <p>
  4322. This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
  4323. an identification of the <em>activation record</em>
  4324. of the function executing at a given level.
  4325. Level&nbsp;0 is the current running function,
  4326. whereas level <em>n+1</em> is the function that has called level <em>n</em>
  4327. (except for tail calls, which do not count on the stack).
  4328. When called with a level greater than the stack depth,
  4329. <a href="#lua_getstack"><code>lua_getstack</code></a> returns 0;
  4330. otherwise it returns 1.
  4331. <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
  4332. <span class="apii">[-0, +(0|1), &ndash;]</span>
  4333. <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
  4334. <p>
  4335. Gets information about the <code>n</code>-th upvalue
  4336. of the closure at index <code>funcindex</code>.
  4337. It pushes the upvalue's value onto the stack
  4338. and returns its name.
  4339. Returns <code>NULL</code> (and pushes nothing)
  4340. when the index <code>n</code> is greater than the number of upvalues.
  4341. <p>
  4342. See <a href="#pdf-debug.getupvalue"><code>debug.getupvalue</code></a> for more information about upvalues.
  4343. <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
  4344. <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
  4345. <p>
  4346. Type for debugging hook functions.
  4347. <p>
  4348. Whenever a hook is called, its <code>ar</code> argument has its field
  4349. <code>event</code> set to the specific event that triggered the hook.
  4350. Lua identifies these events with the following constants:
  4351. <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
  4352. <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
  4353. and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
  4354. Moreover, for line events, the field <code>currentline</code> is also set.
  4355. To get the value of any other field in <code>ar</code>,
  4356. the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  4357. <p>
  4358. For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>,
  4359. the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
  4360. in this case, there will be no corresponding return event.
  4361. <p>
  4362. While Lua is running a hook, it disables other calls to hooks.
  4363. Therefore, if a hook calls back Lua to execute a function or a chunk,
  4364. this execution occurs without any calls to hooks.
  4365. <p>
  4366. Hook functions cannot have continuations,
  4367. that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  4368. <a href="#lua_pcallk"><code>lua_pcallk</code></a>, or <a href="#lua_callk"><code>lua_callk</code></a> with a non-null <code>k</code>.
  4369. <p>
  4370. Hook functions can yield under the following conditions:
  4371. Only count and line events can yield;
  4372. to yield, a hook function must finish its execution
  4373. calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero
  4374. (that is, with no values).
  4375. <hr><h3><a name="lua_setcstacklimit"><code>lua_setcstacklimit</code></a></h3><p>
  4376. <span class="apii">[-0, +0, &ndash;]</span>
  4377. <pre>int (lua_setcstacklimit) (lua_State *L, unsigned int limit);</pre>
  4378. <p>
  4379. Sets a new limit for the C stack.
  4380. This limit controls how deeply nested calls can go in Lua,
  4381. with the intent of avoiding a stack overflow.
  4382. Returns the old limit in case of success,
  4383. or zero in case of error.
  4384. For more details about this function,
  4385. see <a href="#pdf-debug.setcstacklimit"><code>debug.setcstacklimit</code></a>,
  4386. its equivalent in the standard library.
  4387. <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
  4388. <span class="apii">[-0, +0, &ndash;]</span>
  4389. <pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
  4390. <p>
  4391. Sets the debugging hook function.
  4392. <p>
  4393. Argument <code>f</code> is the hook function.
  4394. <code>mask</code> specifies on which events the hook will be called:
  4395. it is formed by a bitwise OR of the constants
  4396. <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
  4397. <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
  4398. <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
  4399. and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
  4400. The <code>count</code> argument is only meaningful when the mask
  4401. includes <code>LUA_MASKCOUNT</code>.
  4402. For each event, the hook is called as explained below:
  4403. <ul>
  4404. <li><b>The call hook: </b> is called when the interpreter calls a function.
  4405. The hook is called just after Lua enters the new function,
  4406. before the function gets its arguments.
  4407. </li>
  4408. <li><b>The return hook: </b> is called when the interpreter returns from a function.
  4409. The hook is called just before Lua leaves the function.
  4410. </li>
  4411. <li><b>The line hook: </b> is called when the interpreter is about to
  4412. start the execution of a new line of code,
  4413. or when it jumps back in the code (even to the same line).
  4414. This event only happens while Lua is executing a Lua function.
  4415. </li>
  4416. <li><b>The count hook: </b> is called after the interpreter executes every
  4417. <code>count</code> instructions.
  4418. This event only happens while Lua is executing a Lua function.
  4419. </li>
  4420. </ul>
  4421. <p>
  4422. Hooks are disabled by setting <code>mask</code> to zero.
  4423. <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
  4424. <span class="apii">[-(0|1), +0, &ndash;]</span>
  4425. <pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
  4426. <p>
  4427. Sets the value of a local variable of a given activation record.
  4428. It assigns the value on the top of the stack
  4429. to the variable and returns its name.
  4430. It also pops the value from the stack.
  4431. <p>
  4432. Returns <code>NULL</code> (and pops nothing)
  4433. when the index is greater than
  4434. the number of active local variables.
  4435. <p>
  4436. Parameters <code>ar</code> and <code>n</code> are as in the function <a href="#lua_getlocal"><code>lua_getlocal</code></a>.
  4437. <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
  4438. <span class="apii">[-(0|1), +0, &ndash;]</span>
  4439. <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
  4440. <p>
  4441. Sets the value of a closure's upvalue.
  4442. It assigns the value on the top of the stack
  4443. to the upvalue and returns its name.
  4444. It also pops the value from the stack.
  4445. <p>
  4446. Returns <code>NULL</code> (and pops nothing)
  4447. when the index <code>n</code> is greater than the number of upvalues.
  4448. <p>
  4449. Parameters <code>funcindex</code> and <code>n</code> are as in
  4450. the function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>.
  4451. <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
  4452. <span class="apii">[-0, +0, &ndash;]</span>
  4453. <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre>
  4454. <p>
  4455. Returns a unique identifier for the upvalue numbered <code>n</code>
  4456. from the closure at index <code>funcindex</code>.
  4457. <p>
  4458. These unique identifiers allow a program to check whether different
  4459. closures share upvalues.
  4460. Lua closures that share an upvalue
  4461. (that is, that access a same external local variable)
  4462. will return identical ids for those upvalue indices.
  4463. <p>
  4464. Parameters <code>funcindex</code> and <code>n</code> are as in
  4465. the function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>,
  4466. but <code>n</code> cannot be greater than the number of upvalues.
  4467. <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
  4468. <span class="apii">[-0, +0, &ndash;]</span>
  4469. <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
  4470. int funcindex2, int n2);</pre>
  4471. <p>
  4472. Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
  4473. refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
  4474. <h1>5 &ndash; <a name="5">The Auxiliary Library</a></h1>
  4475. <p>
  4476. The <em>auxiliary library</em> provides several convenient functions
  4477. to interface C with Lua.
  4478. While the basic API provides the primitive functions for all
  4479. interactions between C and Lua,
  4480. the auxiliary library provides higher-level functions for some
  4481. common tasks.
  4482. <p>
  4483. All functions and types from the auxiliary library
  4484. are defined in header file <code>lauxlib.h</code> and
  4485. have a prefix <code>luaL_</code>.
  4486. <p>
  4487. All functions in the auxiliary library are built on
  4488. top of the basic API,
  4489. and so they provide nothing that cannot be done with that API.
  4490. Nevertheless, the use of the auxiliary library ensures
  4491. more consistency to your code.
  4492. <p>
  4493. Several functions in the auxiliary library use internally some
  4494. extra stack slots.
  4495. When a function in the auxiliary library uses less than five slots,
  4496. it does not check the stack size;
  4497. it simply assumes that there are enough slots.
  4498. <p>
  4499. Several functions in the auxiliary library are used to
  4500. check C&nbsp;function arguments.
  4501. Because the error message is formatted for arguments
  4502. (e.g., "<code>bad argument #1</code>"),
  4503. you should not use these functions for other stack values.
  4504. <p>
  4505. Functions called <code>luaL_check*</code>
  4506. always raise an error if the check is not satisfied.
  4507. <h2>5.1 &ndash; <a name="5.1">Functions and Types</a></h2>
  4508. <p>
  4509. Here we list all functions and types from the auxiliary library
  4510. in alphabetical order.
  4511. <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
  4512. <span class="apii">[-?, +?, <em>m</em>]</span>
  4513. <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
  4514. <p>
  4515. Adds the byte <code>c</code> to the buffer <code>B</code>
  4516. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4517. <hr><h3><a name="luaL_addgsub"><code>luaL_addgsub</code></a></h3><p>
  4518. <span class="apii">[-0, +0, <em>m</em>]</span>
  4519. <pre>const void luaL_addgsub (luaL_Buffer *B, const char *s,
  4520. const char *p, const char *r);</pre>
  4521. <p>
  4522. Adds a copy of the string <code>s</code> to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>),
  4523. replacing any occurrence of the string <code>p</code>
  4524. with the string <code>r</code>.
  4525. <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
  4526. <span class="apii">[-?, +?, <em>m</em>]</span>
  4527. <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
  4528. <p>
  4529. Adds the string pointed to by <code>s</code> with length <code>l</code> to
  4530. the buffer <code>B</code>
  4531. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4532. The string can contain embedded zeros.
  4533. <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
  4534. <span class="apii">[-?, +?, &ndash;]</span>
  4535. <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
  4536. <p>
  4537. Adds to the buffer <code>B</code>
  4538. a string of length <code>n</code> previously copied to the
  4539. buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
  4540. <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
  4541. <span class="apii">[-?, +?, <em>m</em>]</span>
  4542. <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
  4543. <p>
  4544. Adds the zero-terminated string pointed to by <code>s</code>
  4545. to the buffer <code>B</code>
  4546. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4547. <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
  4548. <span class="apii">[-1, +?, <em>m</em>]</span>
  4549. <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
  4550. <p>
  4551. Adds the value on the top of the stack
  4552. to the buffer <code>B</code>
  4553. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4554. Pops the value.
  4555. <p>
  4556. This is the only function on string buffers that can (and must)
  4557. be called with an extra element on the stack,
  4558. which is the value to be added to the buffer.
  4559. <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
  4560. <span class="apii">[-0, +0, <em>v</em>]</span>
  4561. <pre>void luaL_argcheck (lua_State *L,
  4562. int cond,
  4563. int arg,
  4564. const char *extramsg);</pre>
  4565. <p>
  4566. Checks whether <code>cond</code> is true.
  4567. If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>).
  4568. <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
  4569. <span class="apii">[-0, +0, <em>v</em>]</span>
  4570. <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre>
  4571. <p>
  4572. Raises an error reporting a problem with argument <code>arg</code>
  4573. of the C&nbsp;function that called it,
  4574. using a standard message
  4575. that includes <code>extramsg</code> as a comment:
  4576. <pre>
  4577. bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>)
  4578. </pre><p>
  4579. This function never returns.
  4580. <hr><h3><a name="luaL_argexpected"><code>luaL_argexpected</code></a></h3><p>
  4581. <span class="apii">[-0, +0, <em>v</em>]</span>
  4582. <pre>void luaL_argexpected (lua_State *L,
  4583. int cond,
  4584. int arg,
  4585. const char *tname);</pre>
  4586. <p>
  4587. Checks whether <code>cond</code> is true.
  4588. If it is not, raises an error about the type of the argument <code>arg</code>
  4589. with a standard message (see <a href="#luaL_typeerror"><code>luaL_typeerror</code></a>).
  4590. <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
  4591. <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
  4592. <p>
  4593. Type for a <em>string buffer</em>.
  4594. <p>
  4595. A string buffer allows C&nbsp;code to build Lua strings piecemeal.
  4596. Its pattern of use is as follows:
  4597. <ul>
  4598. <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
  4599. <li>Then initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>
  4600. <li>
  4601. Then add string pieces to the buffer calling any of
  4602. the <code>luaL_add*</code> functions.
  4603. </li>
  4604. <li>
  4605. Finish by calling <code>luaL_pushresult(&amp;b)</code>.
  4606. This call leaves the final string on the top of the stack.
  4607. </li>
  4608. </ul>
  4609. <p>
  4610. If you know beforehand the maximum size of the resulting string,
  4611. you can use the buffer like this:
  4612. <ul>
  4613. <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
  4614. <li>Then initialize it and preallocate a space of
  4615. size <code>sz</code> with a call <code>luaL_buffinitsize(L, &amp;b, sz)</code>.</li>
  4616. <li>Then produce the string into that space.</li>
  4617. <li>
  4618. Finish by calling <code>luaL_pushresultsize(&amp;b, sz)</code>,
  4619. where <code>sz</code> is the total size of the resulting string
  4620. copied into that space (which may be less than or
  4621. equal to the preallocated size).
  4622. </li>
  4623. </ul>
  4624. <p>
  4625. During its normal operation,
  4626. a string buffer uses a variable number of stack slots.
  4627. So, while using a buffer, you cannot assume that you know where
  4628. the top of the stack is.
  4629. You can use the stack between successive calls to buffer operations
  4630. as long as that use is balanced;
  4631. that is,
  4632. when you call a buffer operation,
  4633. the stack is at the same level
  4634. it was immediately after the previous buffer operation.
  4635. (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
  4636. After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>,
  4637. the stack is back to its level when the buffer was initialized,
  4638. plus the final string on its top.
  4639. <hr><h3><a name="luaL_buffaddr"><code>luaL_buffaddr</code></a></h3><p>
  4640. <span class="apii">[-0, +0, &ndash;]</span>
  4641. <pre>char *luaL_buffaddr (luaL_Buffer *B);</pre>
  4642. <p>
  4643. Returns the address of the current content of buffer <code>B</code>
  4644. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4645. Note that any addition to the buffer may invalidate this address.
  4646. <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
  4647. <span class="apii">[-0, +0, &ndash;]</span>
  4648. <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
  4649. <p>
  4650. Initializes a buffer <code>B</code>
  4651. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4652. This function does not allocate any space;
  4653. the buffer must be declared as a variable.
  4654. <hr><h3><a name="luaL_bufflen"><code>luaL_bufflen</code></a></h3><p>
  4655. <span class="apii">[-0, +0, &ndash;]</span>
  4656. <pre>size_t luaL_bufflen (luaL_Buffer *B);</pre>
  4657. <p>
  4658. Returns the length of the current content of buffer <code>B</code>
  4659. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4660. <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
  4661. <span class="apii">[-?, +?, <em>m</em>]</span>
  4662. <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre>
  4663. <p>
  4664. Equivalent to the sequence
  4665. <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>.
  4666. <hr><h3><a name="luaL_buffsub"><code>luaL_buffsub</code></a></h3><p>
  4667. <span class="apii">[-0, +0, &ndash;]</span>
  4668. <pre>void luaL_buffsub (luaL_Buffer *B, int n);</pre>
  4669. <p>
  4670. Removes <code>n</code> bytes from the the buffer <code>B</code>
  4671. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4672. The buffer must have at least that many bytes.
  4673. <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
  4674. <span class="apii">[-0, +(0|1), <em>e</em>]</span>
  4675. <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
  4676. <p>
  4677. Calls a metamethod.
  4678. <p>
  4679. If the object at index <code>obj</code> has a metatable and this
  4680. metatable has a field <code>e</code>,
  4681. this function calls this field passing the object as its only argument.
  4682. In this case this function returns true and pushes onto the
  4683. stack the value returned by the call.
  4684. If there is no metatable or no metamethod,
  4685. this function returns false without pushing any value on the stack.
  4686. <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
  4687. <span class="apii">[-0, +0, <em>v</em>]</span>
  4688. <pre>void luaL_checkany (lua_State *L, int arg);</pre>
  4689. <p>
  4690. Checks whether the function has an argument
  4691. of any type (including <b>nil</b>) at position <code>arg</code>.
  4692. <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
  4693. <span class="apii">[-0, +0, <em>v</em>]</span>
  4694. <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre>
  4695. <p>
  4696. Checks whether the function argument <code>arg</code> is an integer
  4697. (or can be converted to an integer)
  4698. and returns this integer.
  4699. <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
  4700. <span class="apii">[-0, +0, <em>v</em>]</span>
  4701. <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre>
  4702. <p>
  4703. Checks whether the function argument <code>arg</code> is a string
  4704. and returns this string;
  4705. if <code>l</code> is not <code>NULL</code> fills its referent
  4706. with the string's length.
  4707. <p>
  4708. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  4709. so all conversions and caveats of that function apply here.
  4710. <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
  4711. <span class="apii">[-0, +0, <em>v</em>]</span>
  4712. <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre>
  4713. <p>
  4714. Checks whether the function argument <code>arg</code> is a number
  4715. and returns this number converted to a <code>lua_Number</code>.
  4716. <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
  4717. <span class="apii">[-0, +0, <em>v</em>]</span>
  4718. <pre>int luaL_checkoption (lua_State *L,
  4719. int arg,
  4720. const char *def,
  4721. const char *const lst[]);</pre>
  4722. <p>
  4723. Checks whether the function argument <code>arg</code> is a string and
  4724. searches for this string in the array <code>lst</code>
  4725. (which must be NULL-terminated).
  4726. Returns the index in the array where the string was found.
  4727. Raises an error if the argument is not a string or
  4728. if the string cannot be found.
  4729. <p>
  4730. If <code>def</code> is not <code>NULL</code>,
  4731. the function uses <code>def</code> as a default value when
  4732. there is no argument <code>arg</code> or when this argument is <b>nil</b>.
  4733. <p>
  4734. This is a useful function for mapping strings to C&nbsp;enums.
  4735. (The usual convention in Lua libraries is
  4736. to use strings instead of numbers to select options.)
  4737. <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
  4738. <span class="apii">[-0, +0, <em>v</em>]</span>
  4739. <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
  4740. <p>
  4741. Grows the stack size to <code>top + sz</code> elements,
  4742. raising an error if the stack cannot grow to that size.
  4743. <code>msg</code> is an additional text to go into the error message
  4744. (or <code>NULL</code> for no additional text).
  4745. <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
  4746. <span class="apii">[-0, +0, <em>v</em>]</span>
  4747. <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre>
  4748. <p>
  4749. Checks whether the function argument <code>arg</code> is a string
  4750. and returns this string.
  4751. <p>
  4752. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  4753. so all conversions and caveats of that function apply here.
  4754. <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
  4755. <span class="apii">[-0, +0, <em>v</em>]</span>
  4756. <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre>
  4757. <p>
  4758. Checks whether the function argument <code>arg</code> has type <code>t</code>.
  4759. See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
  4760. <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
  4761. <span class="apii">[-0, +0, <em>v</em>]</span>
  4762. <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre>
  4763. <p>
  4764. Checks whether the function argument <code>arg</code> is a userdata
  4765. of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and
  4766. returns the userdata's memory-block address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
  4767. <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
  4768. <span class="apii">[-0, +0, <em>v</em>]</span>
  4769. <pre>void luaL_checkversion (lua_State *L);</pre>
  4770. <p>
  4771. Checks whether the code making the call and the Lua library being called
  4772. are using the same version of Lua and the same numeric types.
  4773. <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
  4774. <span class="apii">[-0, +?, <em>m</em>]</span>
  4775. <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
  4776. <p>
  4777. Loads and runs the given file.
  4778. It is defined as the following macro:
  4779. <pre>
  4780. (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
  4781. </pre><p>
  4782. It returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if there are no errors,
  4783. or an error code in case of errors (see <a href="#4.4.1">&sect;4.4.1</a>).
  4784. <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
  4785. <span class="apii">[-0, +?, &ndash;]</span>
  4786. <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
  4787. <p>
  4788. Loads and runs the given string.
  4789. It is defined as the following macro:
  4790. <pre>
  4791. (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
  4792. </pre><p>
  4793. It returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if there are no errors,
  4794. or an error code in case of errors (see <a href="#4.4.1">&sect;4.4.1</a>).
  4795. <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
  4796. <span class="apii">[-0, +0, <em>v</em>]</span>
  4797. <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
  4798. <p>
  4799. Raises an error.
  4800. The error message format is given by <code>fmt</code>
  4801. plus any extra arguments,
  4802. following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
  4803. It also adds at the beginning of the message the file name and
  4804. the line number where the error occurred,
  4805. if this information is available.
  4806. <p>
  4807. This function never returns,
  4808. but it is an idiom to use it in C&nbsp;functions
  4809. as <code>return luaL_error(<em>args</em>)</code>.
  4810. <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
  4811. <span class="apii">[-0, +3, <em>m</em>]</span>
  4812. <pre>int luaL_execresult (lua_State *L, int stat);</pre>
  4813. <p>
  4814. This function produces the return values for
  4815. process-related functions in the standard library
  4816. (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
  4817. <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
  4818. <span class="apii">[-0, +(1|3), <em>m</em>]</span>
  4819. <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre>
  4820. <p>
  4821. This function produces the return values for
  4822. file-related functions in the standard library
  4823. (<a href="#pdf-io.open"><code>io.open</code></a>, <a href="#pdf-os.rename"><code>os.rename</code></a>, <a href="#pdf-file:seek"><code>file:seek</code></a>, etc.).
  4824. <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
  4825. <span class="apii">[-0, +(0|1), <em>m</em>]</span>
  4826. <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
  4827. <p>
  4828. Pushes onto the stack the field <code>e</code> from the metatable
  4829. of the object at index <code>obj</code> and returns the type of the pushed value.
  4830. If the object does not have a metatable,
  4831. or if the metatable does not have this field,
  4832. pushes nothing and returns <code>LUA_TNIL</code>.
  4833. <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
  4834. <span class="apii">[-0, +1, <em>m</em>]</span>
  4835. <pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre>
  4836. <p>
  4837. Pushes onto the stack the metatable associated with the name <code>tname</code>
  4838. in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>),
  4839. or <b>nil</b> if there is no metatable associated with that name.
  4840. Returns the type of the pushed value.
  4841. <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
  4842. <span class="apii">[-0, +1, <em>e</em>]</span>
  4843. <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
  4844. <p>
  4845. Ensures that the value <code>t[fname]</code>,
  4846. where <code>t</code> is the value at index <code>idx</code>,
  4847. is a table,
  4848. and pushes that table onto the stack.
  4849. Returns true if it finds a previous table there
  4850. and false if it creates a new table.
  4851. <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
  4852. <span class="apii">[-0, +1, <em>m</em>]</span>
  4853. <pre>const char *luaL_gsub (lua_State *L,
  4854. const char *s,
  4855. const char *p,
  4856. const char *r);</pre>
  4857. <p>
  4858. Creates a copy of string <code>s</code>,
  4859. replacing any occurrence of the string <code>p</code>
  4860. with the string <code>r</code>.
  4861. Pushes the resulting string on the stack and returns it.
  4862. <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
  4863. <span class="apii">[-0, +0, <em>e</em>]</span>
  4864. <pre>lua_Integer luaL_len (lua_State *L, int index);</pre>
  4865. <p>
  4866. Returns the "length" of the value at the given index
  4867. as a number;
  4868. it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>).
  4869. Raises an error if the result of the operation is not an integer.
  4870. (This case can only happen through metamethods.)
  4871. <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
  4872. <span class="apii">[-0, +1, &ndash;]</span>
  4873. <pre>int luaL_loadbuffer (lua_State *L,
  4874. const char *buff,
  4875. size_t sz,
  4876. const char *name);</pre>
  4877. <p>
  4878. Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
  4879. <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
  4880. <span class="apii">[-0, +1, &ndash;]</span>
  4881. <pre>int luaL_loadbufferx (lua_State *L,
  4882. const char *buff,
  4883. size_t sz,
  4884. const char *name,
  4885. const char *mode);</pre>
  4886. <p>
  4887. Loads a buffer as a Lua chunk.
  4888. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
  4889. buffer pointed to by <code>buff</code> with size <code>sz</code>.
  4890. <p>
  4891. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
  4892. <code>name</code> is the chunk name,
  4893. used for debug information and error messages.
  4894. The string <code>mode</code> works as in the function <a href="#lua_load"><code>lua_load</code></a>.
  4895. <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
  4896. <span class="apii">[-0, +1, <em>m</em>]</span>
  4897. <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
  4898. <p>
  4899. Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
  4900. <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
  4901. <span class="apii">[-0, +1, <em>m</em>]</span>
  4902. <pre>int luaL_loadfilex (lua_State *L, const char *filename,
  4903. const char *mode);</pre>
  4904. <p>
  4905. Loads a file as a Lua chunk.
  4906. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
  4907. named <code>filename</code>.
  4908. If <code>filename</code> is <code>NULL</code>,
  4909. then it loads from the standard input.
  4910. The first line in the file is ignored if it starts with a <code>#</code>.
  4911. <p>
  4912. The string <code>mode</code> works as in the function <a href="#lua_load"><code>lua_load</code></a>.
  4913. <p>
  4914. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>
  4915. or <a href="#pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a> for file-related errors.
  4916. <p>
  4917. As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
  4918. it does not run it.
  4919. <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
  4920. <span class="apii">[-0, +1, &ndash;]</span>
  4921. <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
  4922. <p>
  4923. Loads a string as a Lua chunk.
  4924. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
  4925. the zero-terminated string <code>s</code>.
  4926. <p>
  4927. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
  4928. <p>
  4929. Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
  4930. it does not run it.
  4931. <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
  4932. <span class="apii">[-0, +1, <em>m</em>]</span>
  4933. <pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre>
  4934. <p>
  4935. Creates a new table and registers there
  4936. the functions in the list <code>l</code>.
  4937. <p>
  4938. It is implemented as the following macro:
  4939. <pre>
  4940. (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
  4941. </pre><p>
  4942. The array <code>l</code> must be the actual array,
  4943. not a pointer to it.
  4944. <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
  4945. <span class="apii">[-0, +1, <em>m</em>]</span>
  4946. <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre>
  4947. <p>
  4948. Creates a new table with a size optimized
  4949. to store all entries in the array <code>l</code>
  4950. (but does not actually store them).
  4951. It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
  4952. (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
  4953. <p>
  4954. It is implemented as a macro.
  4955. The array <code>l</code> must be the actual array,
  4956. not a pointer to it.
  4957. <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
  4958. <span class="apii">[-0, +1, <em>m</em>]</span>
  4959. <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
  4960. <p>
  4961. If the registry already has the key <code>tname</code>,
  4962. returns 0.
  4963. Otherwise,
  4964. creates a new table to be used as a metatable for userdata,
  4965. adds to this new table the pair <code>__name = tname</code>,
  4966. adds to the registry the pair <code>[tname] = new table</code>,
  4967. and returns 1.
  4968. <p>
  4969. In both cases,
  4970. the function pushes onto the stack the final value associated
  4971. with <code>tname</code> in the registry.
  4972. <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
  4973. <span class="apii">[-0, +0, &ndash;]</span>
  4974. <pre>lua_State *luaL_newstate (void);</pre>
  4975. <p>
  4976. Creates a new Lua state.
  4977. It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
  4978. allocator based on the standard&nbsp;C allocation functions
  4979. and then sets a warning function and a panic function (see <a href="#4.4">&sect;4.4</a>)
  4980. that print messages to the standard error output.
  4981. <p>
  4982. Returns the new state,
  4983. or <code>NULL</code> if there is a memory allocation error.
  4984. <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
  4985. <span class="apii">[-0, +0, <em>e</em>]</span>
  4986. <pre>void luaL_openlibs (lua_State *L);</pre>
  4987. <p>
  4988. Opens all standard Lua libraries into the given state.
  4989. <hr><h3><a name="luaL_opt"><code>luaL_opt</code></a></h3><p>
  4990. <span class="apii">[-0, +0, &ndash;]</span>
  4991. <pre>T luaL_opt (L, func, arg, dflt);</pre>
  4992. <p>
  4993. This macro is defined as follows:
  4994. <pre>
  4995. (lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg)))
  4996. </pre><p>
  4997. In words, if the argument <code>arg</code> is nil or absent,
  4998. the macro results in the default <code>dflt</code>.
  4999. Otherwise, it results in the result of calling <code>func</code>
  5000. with the state <code>L</code> and the argument index <code>arg</code> as
  5001. arguments.
  5002. Note that it evaluates the expression <code>dflt</code> only if needed.
  5003. <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
  5004. <span class="apii">[-0, +0, <em>v</em>]</span>
  5005. <pre>lua_Integer luaL_optinteger (lua_State *L,
  5006. int arg,
  5007. lua_Integer d);</pre>
  5008. <p>
  5009. If the function argument <code>arg</code> is an integer
  5010. (or it is convertible to an integer),
  5011. returns this integer.
  5012. If this argument is absent or is <b>nil</b>,
  5013. returns <code>d</code>.
  5014. Otherwise, raises an error.
  5015. <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
  5016. <span class="apii">[-0, +0, <em>v</em>]</span>
  5017. <pre>const char *luaL_optlstring (lua_State *L,
  5018. int arg,
  5019. const char *d,
  5020. size_t *l);</pre>
  5021. <p>
  5022. If the function argument <code>arg</code> is a string,
  5023. returns this string.
  5024. If this argument is absent or is <b>nil</b>,
  5025. returns <code>d</code>.
  5026. Otherwise, raises an error.
  5027. <p>
  5028. If <code>l</code> is not <code>NULL</code>,
  5029. fills its referent with the result's length.
  5030. If the result is <code>NULL</code>
  5031. (only possible when returning <code>d</code> and <code>d == NULL</code>),
  5032. its length is considered zero.
  5033. <p>
  5034. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  5035. so all conversions and caveats of that function apply here.
  5036. <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
  5037. <span class="apii">[-0, +0, <em>v</em>]</span>
  5038. <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre>
  5039. <p>
  5040. If the function argument <code>arg</code> is a number,
  5041. returns this number as a <code>lua_Number</code>.
  5042. If this argument is absent or is <b>nil</b>,
  5043. returns <code>d</code>.
  5044. Otherwise, raises an error.
  5045. <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
  5046. <span class="apii">[-0, +0, <em>v</em>]</span>
  5047. <pre>const char *luaL_optstring (lua_State *L,
  5048. int arg,
  5049. const char *d);</pre>
  5050. <p>
  5051. If the function argument <code>arg</code> is a string,
  5052. returns this string.
  5053. If this argument is absent or is <b>nil</b>,
  5054. returns <code>d</code>.
  5055. Otherwise, raises an error.
  5056. <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
  5057. <span class="apii">[-?, +?, <em>m</em>]</span>
  5058. <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
  5059. <p>
  5060. Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
  5061. with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
  5062. <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
  5063. <span class="apii">[-?, +?, <em>m</em>]</span>
  5064. <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre>
  5065. <p>
  5066. Returns an address to a space of size <code>sz</code>
  5067. where you can copy a string to be added to buffer <code>B</code>
  5068. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  5069. After copying the string into this space you must call
  5070. <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
  5071. it to the buffer.
  5072. <hr><h3><a name="luaL_pushfail"><code>luaL_pushfail</code></a></h3><p>
  5073. <span class="apii">[-0, +1, &ndash;]</span>
  5074. <pre>void luaL_pushfail (lua_State *L);</pre>
  5075. <p>
  5076. Pushes the <b>fail</b> value onto the stack (see <a href="#6">&sect;6</a>).
  5077. <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
  5078. <span class="apii">[-?, +1, <em>m</em>]</span>
  5079. <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
  5080. <p>
  5081. Finishes the use of buffer <code>B</code> leaving the final string on
  5082. the top of the stack.
  5083. <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
  5084. <span class="apii">[-?, +1, <em>m</em>]</span>
  5085. <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre>
  5086. <p>
  5087. Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>.
  5088. <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
  5089. <span class="apii">[-1, +0, <em>m</em>]</span>
  5090. <pre>int luaL_ref (lua_State *L, int t);</pre>
  5091. <p>
  5092. Creates and returns a <em>reference</em>,
  5093. in the table at index <code>t</code>,
  5094. for the object on the top of the stack (and pops the object).
  5095. <p>
  5096. A reference is a unique integer key.
  5097. As long as you do not manually add integer keys into the table <code>t</code>,
  5098. <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
  5099. You can retrieve an object referred by the reference <code>r</code>
  5100. by calling <code>lua_rawgeti(L, t, r)</code>.
  5101. The function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference.
  5102. <p>
  5103. If the object on the top of the stack is <b>nil</b>,
  5104. <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
  5105. The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
  5106. from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
  5107. <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
  5108. <pre>typedef struct luaL_Reg {
  5109. const char *name;
  5110. lua_CFunction func;
  5111. } luaL_Reg;</pre>
  5112. <p>
  5113. Type for arrays of functions to be registered by
  5114. <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
  5115. <code>name</code> is the function name and <code>func</code> is a pointer to
  5116. the function.
  5117. Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry
  5118. in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
  5119. <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
  5120. <span class="apii">[-0, +1, <em>e</em>]</span>
  5121. <pre>void luaL_requiref (lua_State *L, const char *modname,
  5122. lua_CFunction openf, int glb);</pre>
  5123. <p>
  5124. If <code>package.loaded[modname]</code> is not true,
  5125. calls the function <code>openf</code> with the string <code>modname</code> as an argument
  5126. and sets the call result to <code>package.loaded[modname]</code>,
  5127. as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
  5128. <p>
  5129. If <code>glb</code> is true,
  5130. also stores the module into the global <code>modname</code>.
  5131. <p>
  5132. Leaves a copy of the module on the stack.
  5133. <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
  5134. <span class="apii">[-nup, +0, <em>m</em>]</span>
  5135. <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre>
  5136. <p>
  5137. Registers all functions in the array <code>l</code>
  5138. (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
  5139. (below optional upvalues, see next).
  5140. <p>
  5141. When <code>nup</code> is not zero,
  5142. all functions are created with <code>nup</code> upvalues,
  5143. initialized with copies of the <code>nup</code> values
  5144. previously pushed on the stack
  5145. on top of the library table.
  5146. These values are popped from the stack after the registration.
  5147. <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
  5148. <span class="apii">[-0, +0, &ndash;]</span>
  5149. <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre>
  5150. <p>
  5151. Sets the metatable of the object on the top of the stack
  5152. as the metatable associated with name <code>tname</code>
  5153. in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
  5154. <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3>
  5155. <pre>typedef struct luaL_Stream {
  5156. FILE *f;
  5157. lua_CFunction closef;
  5158. } luaL_Stream;</pre>
  5159. <p>
  5160. The standard representation for file handles
  5161. used by the standard I/O library.
  5162. <p>
  5163. A file handle is implemented as a full userdata,
  5164. with a metatable called <code>LUA_FILEHANDLE</code>
  5165. (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name).
  5166. The metatable is created by the I/O library
  5167. (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
  5168. <p>
  5169. This userdata must start with the structure <code>luaL_Stream</code>;
  5170. it can contain other data after this initial structure.
  5171. The field <code>f</code> points to the corresponding C stream
  5172. (or it can be <code>NULL</code> to indicate an incompletely created handle).
  5173. The field <code>closef</code> points to a Lua function
  5174. that will be called to close the stream
  5175. when the handle is closed or collected;
  5176. this function receives the file handle as its sole argument and
  5177. must return either a true value, in case of success,
  5178. or a false value plus an error message, in case of error.
  5179. Once Lua calls this field,
  5180. it changes the field value to <code>NULL</code>
  5181. to signal that the handle is closed.
  5182. <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
  5183. <span class="apii">[-0, +0, <em>m</em>]</span>
  5184. <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre>
  5185. <p>
  5186. This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
  5187. except that, when the test fails,
  5188. it returns <code>NULL</code> instead of raising an error.
  5189. <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
  5190. <span class="apii">[-0, +1, <em>e</em>]</span>
  5191. <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre>
  5192. <p>
  5193. Converts any Lua value at the given index to a C&nbsp;string
  5194. in a reasonable format.
  5195. The resulting string is pushed onto the stack and also
  5196. returned by the function.
  5197. If <code>len</code> is not <code>NULL</code>,
  5198. the function also sets <code>*len</code> with the string length.
  5199. <p>
  5200. If the value has a metatable with a <code>__tostring</code> field,
  5201. then <code>luaL_tolstring</code> calls the corresponding metamethod
  5202. with the value as argument,
  5203. and uses the result of the call as its result.
  5204. <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
  5205. <span class="apii">[-0, +1, <em>m</em>]</span>
  5206. <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
  5207. int level);</pre>
  5208. <p>
  5209. Creates and pushes a traceback of the stack <code>L1</code>.
  5210. If <code>msg</code> is not <code>NULL</code>, it is appended
  5211. at the beginning of the traceback.
  5212. The <code>level</code> parameter tells at which level
  5213. to start the traceback.
  5214. <hr><h3><a name="luaL_typeerror"><code>luaL_typeerror</code></a></h3><p>
  5215. <span class="apii">[-0, +0, <em>v</em>]</span>
  5216. <pre>const char *luaL_typeerror (lua_State *L,
  5217. int arg,
  5218. const char *tname);</pre>
  5219. <p>
  5220. Raises a type error for the argument <code>arg</code>
  5221. of the C&nbsp;function that called it,
  5222. using a standard message;
  5223. <code>tname</code> is a "name" for the expected type.
  5224. This function never returns.
  5225. <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
  5226. <span class="apii">[-0, +0, &ndash;]</span>
  5227. <pre>const char *luaL_typename (lua_State *L, int index);</pre>
  5228. <p>
  5229. Returns the name of the type of the value at the given index.
  5230. <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
  5231. <span class="apii">[-0, +0, &ndash;]</span>
  5232. <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
  5233. <p>
  5234. Releases the reference <code>ref</code> from the table at index <code>t</code>
  5235. (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
  5236. The entry is removed from the table,
  5237. so that the referred object can be collected.
  5238. The reference <code>ref</code> is also freed to be used again.
  5239. <p>
  5240. 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>,
  5241. <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
  5242. <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
  5243. <span class="apii">[-0, +1, <em>m</em>]</span>
  5244. <pre>void luaL_where (lua_State *L, int lvl);</pre>
  5245. <p>
  5246. Pushes onto the stack a string identifying the current position
  5247. of the control at level <code>lvl</code> in the call stack.
  5248. Typically this string has the following format:
  5249. <pre>
  5250. <em>chunkname</em>:<em>currentline</em>:
  5251. </pre><p>
  5252. Level&nbsp;0 is the running function,
  5253. level&nbsp;1 is the function that called the running function,
  5254. etc.
  5255. <p>
  5256. This function is used to build a prefix for error messages.
  5257. <h1>6 &ndash; <a name="6">The Standard Libraries</a></h1>
  5258. <p>
  5259. The standard Lua libraries provide useful functions
  5260. that are implemented in&nbsp;C through the C&nbsp;API.
  5261. Some of these functions provide essential services to the language
  5262. (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>);
  5263. others provide access to outside services (e.g., I/O);
  5264. and others could be implemented in Lua itself,
  5265. but that for different reasons
  5266. deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>).
  5267. <p>
  5268. All libraries are implemented through the official C&nbsp;API
  5269. and are provided as separate C&nbsp;modules.
  5270. Unless otherwise noted,
  5271. these library functions do not adjust its number of arguments
  5272. to its expected parameters.
  5273. For instance, a function documented as <code>foo(arg)</code>
  5274. should not be called without an argument.
  5275. <p>
  5276. The notation <b>fail</b> means a false value representing
  5277. some kind of failure.
  5278. (Currently, <b>fail</b> is equal to <b>nil</b>,
  5279. but that may change in future versions.
  5280. The recommendation is to always test the success of these functions
  5281. with <code>(not status)</code>, instead of <code>(status == nil)</code>.)
  5282. <p>
  5283. Currently, Lua has the following standard libraries:
  5284. <ul>
  5285. <li>basic library (<a href="#6.1">&sect;6.1</a>);</li>
  5286. <li>coroutine library (<a href="#6.2">&sect;6.2</a>);</li>
  5287. <li>package library (<a href="#6.3">&sect;6.3</a>);</li>
  5288. <li>string manipulation (<a href="#6.4">&sect;6.4</a>);</li>
  5289. <li>basic UTF-8 support (<a href="#6.5">&sect;6.5</a>);</li>
  5290. <li>table manipulation (<a href="#6.6">&sect;6.6</a>);</li>
  5291. <li>mathematical functions (<a href="#6.7">&sect;6.7</a>) (sin, log, etc.);</li>
  5292. <li>input and output (<a href="#6.8">&sect;6.8</a>);</li>
  5293. <li>operating system facilities (<a href="#6.9">&sect;6.9</a>);</li>
  5294. <li>debug facilities (<a href="#6.10">&sect;6.10</a>).</li>
  5295. </ul><p>
  5296. Except for the basic and the package libraries,
  5297. each library provides all its functions as fields of a global table
  5298. or as methods of its objects.
  5299. <p>
  5300. To have access to these libraries,
  5301. the C&nbsp;host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function,
  5302. which opens all standard libraries.
  5303. Alternatively,
  5304. the host program can open them individually by using
  5305. <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call
  5306. <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library),
  5307. <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library),
  5308. <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library),
  5309. <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library),
  5310. <a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF-8 library),
  5311. <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library),
  5312. <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library),
  5313. <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library),
  5314. <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library),
  5315. and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library).
  5316. These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>.
  5317. <h2>6.1 &ndash; <a name="6.1">Basic Functions</a></h2>
  5318. <p>
  5319. The basic library provides core functions to Lua.
  5320. If you do not include this library in your application,
  5321. you should check carefully whether you need to provide
  5322. implementations for some of its facilities.
  5323. <p>
  5324. <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
  5325. <p>
  5326. Raises an error if
  5327. the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
  5328. otherwise, returns all its arguments.
  5329. In case of error,
  5330. <code>message</code> is the error object;
  5331. when absent, it defaults to "<code>assertion failed!</code>"
  5332. <p>
  5333. <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3>
  5334. <p>
  5335. This function is a generic interface to the garbage collector.
  5336. It performs different functions according to its first argument, <code>opt</code>:
  5337. <ul>
  5338. <li><b>"<code>collect</code>": </b>
  5339. Performs a full garbage-collection cycle.
  5340. This is the default option.
  5341. </li>
  5342. <li><b>"<code>stop</code>": </b>
  5343. Stops automatic execution of the garbage collector.
  5344. The collector will run only when explicitly invoked,
  5345. until a call to restart it.
  5346. </li>
  5347. <li><b>"<code>restart</code>": </b>
  5348. Restarts automatic execution of the garbage collector.
  5349. </li>
  5350. <li><b>"<code>count</code>": </b>
  5351. Returns the total memory in use by Lua in Kbytes.
  5352. The value has a fractional part,
  5353. so that it multiplied by 1024
  5354. gives the exact number of bytes in use by Lua.
  5355. </li>
  5356. <li><b>"<code>step</code>": </b>
  5357. Performs a garbage-collection step.
  5358. The step "size" is controlled by <code>arg</code>.
  5359. With a zero value,
  5360. the collector will perform one basic (indivisible) step.
  5361. For non-zero values,
  5362. the collector will perform as if that amount of memory
  5363. (in Kbytes) had been allocated by Lua.
  5364. Returns <b>true</b> if the step finished a collection cycle.
  5365. </li>
  5366. <li><b>"<code>isrunning</code>": </b>
  5367. Returns a boolean that tells whether the collector is running
  5368. (i.e., not stopped).
  5369. </li>
  5370. <li><b>"<code>incremental</code>": </b>
  5371. Change the collector mode to incremental.
  5372. This option can be followed by three numbers:
  5373. the garbage-collector pause,
  5374. the step multiplier,
  5375. and the step size (see <a href="#2.5.1">&sect;2.5.1</a>).
  5376. A zero means to not change that value.
  5377. </li>
  5378. <li><b>"<code>generational</code>": </b>
  5379. Change the collector mode to generational.
  5380. This option can be followed by two numbers:
  5381. the garbage-collector minor multiplier
  5382. and the major multiplier (see <a href="#2.5.2">&sect;2.5.2</a>).
  5383. A zero means to not change that value.
  5384. </li>
  5385. </ul><p>
  5386. See <a href="#2.5">&sect;2.5</a> for more details about garbage collection
  5387. and some of these options.
  5388. <p>
  5389. <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3>
  5390. Opens the named file and executes its content as a Lua chunk.
  5391. When called without arguments,
  5392. <code>dofile</code> executes the content of the standard input (<code>stdin</code>).
  5393. Returns all values returned by the chunk.
  5394. In case of errors, <code>dofile</code> propagates the error
  5395. to its caller.
  5396. (That is, <code>dofile</code> does not run in protected mode.)
  5397. <p>
  5398. <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
  5399. Raises an error (see <a href="#2.3">&sect;2.3</a>) with @{message} as the error object.
  5400. This function never returns.
  5401. <p>
  5402. Usually, <code>error</code> adds some information about the error position
  5403. at the beginning of the message, if the message is a string.
  5404. The <code>level</code> argument specifies how to get the error position.
  5405. With level&nbsp;1 (the default), the error position is where the
  5406. <code>error</code> function was called.
  5407. Level&nbsp;2 points the error to where the function
  5408. that called <code>error</code> was called; and so on.
  5409. Passing a level&nbsp;0 avoids the addition of error position information
  5410. to the message.
  5411. <p>
  5412. <hr><h3><a name="pdf-_G"><code>_G</code></a></h3>
  5413. A global variable (not a function) that
  5414. holds the global environment (see <a href="#2.2">&sect;2.2</a>).
  5415. Lua itself does not use this variable;
  5416. changing its value does not affect any environment,
  5417. nor vice versa.
  5418. <p>
  5419. <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3>
  5420. <p>
  5421. If <code>object</code> does not have a metatable, returns <b>nil</b>.
  5422. Otherwise,
  5423. if the object's metatable has a <code>__metatable</code> field,
  5424. returns the associated value.
  5425. Otherwise, returns the metatable of the given object.
  5426. <p>
  5427. <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3>
  5428. <p>
  5429. Returns three values (an iterator function, the table <code>t</code>, and 0)
  5430. so that the construction
  5431. <pre>
  5432. for i,v in ipairs(t) do <em>body</em> end
  5433. </pre><p>
  5434. will iterate over the key&ndash;value pairs
  5435. (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
  5436. up to the first absent index.
  5437. <p>
  5438. <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3>
  5439. <p>
  5440. Loads a chunk.
  5441. <p>
  5442. If <code>chunk</code> is a string, the chunk is this string.
  5443. If <code>chunk</code> is a function,
  5444. <code>load</code> calls it repeatedly to get the chunk pieces.
  5445. Each call to <code>chunk</code> must return a string that concatenates
  5446. with previous results.
  5447. A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
  5448. <p>
  5449. If there are no syntactic errors,
  5450. <code>load</code> returns the compiled chunk as a function;
  5451. otherwise, it returns <b>fail</b> plus the error message.
  5452. <p>
  5453. When you load a main chunk,
  5454. the resulting function will always have exactly one upvalue,
  5455. the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
  5456. However,
  5457. when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>),
  5458. the resulting function can have an arbitrary number of upvalues,
  5459. and there is no guarantee that its first upvalue will be
  5460. the <code>_ENV</code> variable.
  5461. (A non-main function may not even have an <code>_ENV</code> upvalue.)
  5462. <p>
  5463. Regardless, if the resulting function has any upvalues,
  5464. its first upvalue is set to the value of <code>env</code>,
  5465. if that parameter is given,
  5466. or to the value of the global environment.
  5467. Other upvalues are initialized with <b>nil</b>.
  5468. All upvalues are fresh, that is,
  5469. they are not shared with any other function.
  5470. <p>
  5471. <code>chunkname</code> is used as the name of the chunk for error messages
  5472. and debug information (see <a href="#4.7">&sect;4.7</a>).
  5473. When absent,
  5474. it defaults to <code>chunk</code>, if <code>chunk</code> is a string,
  5475. or to "<code>=(load)</code>" otherwise.
  5476. <p>
  5477. The string <code>mode</code> controls whether the chunk can be text or binary
  5478. (that is, a precompiled chunk).
  5479. It may be the string "<code>b</code>" (only binary chunks),
  5480. "<code>t</code>" (only text chunks),
  5481. or "<code>bt</code>" (both binary and text).
  5482. The default is "<code>bt</code>".
  5483. <p>
  5484. Lua does not check the consistency of binary chunks.
  5485. Maliciously crafted binary chunks can crash
  5486. the interpreter.
  5487. <p>
  5488. <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3>
  5489. <p>
  5490. Similar to <a href="#pdf-load"><code>load</code></a>,
  5491. but gets the chunk from file <code>filename</code>
  5492. or from the standard input,
  5493. if no file name is given.
  5494. <p>
  5495. <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
  5496. <p>
  5497. Allows a program to traverse all fields of a table.
  5498. Its first argument is a table and its second argument
  5499. is an index in this table.
  5500. A call to <code>next</code> returns the next index of the table
  5501. and its associated value.
  5502. When called with <b>nil</b> as its second argument,
  5503. <code>next</code> returns an initial index
  5504. and its associated value.
  5505. When called with the last index,
  5506. or with <b>nil</b> in an empty table,
  5507. <code>next</code> returns <b>nil</b>.
  5508. If the second argument is absent, then it is interpreted as <b>nil</b>.
  5509. In particular,
  5510. you can use <code>next(t)</code> to check whether a table is empty.
  5511. <p>
  5512. The order in which the indices are enumerated is not specified,
  5513. <em>even for numeric indices</em>.
  5514. (To traverse a table in numerical order,
  5515. use a numerical <b>for</b>.)
  5516. <p>
  5517. The behavior of <code>next</code> is undefined if,
  5518. during the traversal,
  5519. you assign any value to a non-existent field in the table.
  5520. You may however modify existing fields.
  5521. In particular, you may set existing fields to nil.
  5522. <p>
  5523. <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3>
  5524. <p>
  5525. If <code>t</code> has a metamethod <code>__pairs</code>,
  5526. calls it with <code>t</code> as argument and returns the first three
  5527. results from the call.
  5528. <p>
  5529. Otherwise,
  5530. returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>,
  5531. so that the construction
  5532. <pre>
  5533. for k,v in pairs(t) do <em>body</em> end
  5534. </pre><p>
  5535. will iterate over all key&ndash;value pairs of table <code>t</code>.
  5536. <p>
  5537. See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
  5538. the table during its traversal.
  5539. <p>
  5540. <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, &middot;&middot;&middot;])</code></a></h3>
  5541. <p>
  5542. Calls the function <code>f</code> with
  5543. the given arguments in <em>protected mode</em>.
  5544. This means that any error inside&nbsp;<code>f</code> is not propagated;
  5545. instead, <code>pcall</code> catches the error
  5546. and returns a status code.
  5547. Its first result is the status code (a boolean),
  5548. which is true if the call succeeds without errors.
  5549. In such case, <code>pcall</code> also returns all results from the call,
  5550. after this first result.
  5551. In case of any error, <code>pcall</code> returns <b>false</b> plus the error object.
  5552. Note that errors caught by <code>pcall</code> do not call a message handler.
  5553. <p>
  5554. <hr><h3><a name="pdf-print"><code>print (&middot;&middot;&middot;)</code></a></h3>
  5555. Receives any number of arguments
  5556. and prints their values to <code>stdout</code>,
  5557. converting each argument to a string
  5558. following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
  5559. <p>
  5560. The function <code>print</code> is not intended for formatted output,
  5561. but only as a quick way to show a value,
  5562. for instance for debugging.
  5563. For complete control over the output,
  5564. use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
  5565. <p>
  5566. <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3>
  5567. Checks whether <code>v1</code> is equal to <code>v2</code>,
  5568. without invoking the <code>__eq</code> metamethod.
  5569. Returns a boolean.
  5570. <p>
  5571. <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3>
  5572. Gets the real value of <code>table[index]</code>,
  5573. without using the <code>__index</code> metavalue.
  5574. <code>table</code> must be a table;
  5575. <code>index</code> may be any value.
  5576. <p>
  5577. <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3>
  5578. Returns the length of the object <code>v</code>,
  5579. which must be a table or a string,
  5580. without invoking the <code>__len</code> metamethod.
  5581. Returns an integer.
  5582. <p>
  5583. <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
  5584. Sets the real value of <code>table[index]</code> to <code>value</code>,
  5585. without using the <code>__newindex</code> metavalue.
  5586. <code>table</code> must be a table,
  5587. <code>index</code> any value different from <b>nil</b> and NaN,
  5588. and <code>value</code> any Lua value.
  5589. <p>
  5590. This function returns <code>table</code>.
  5591. <p>
  5592. <hr><h3><a name="pdf-select"><code>select (index, &middot;&middot;&middot;)</code></a></h3>
  5593. <p>
  5594. If <code>index</code> is a number,
  5595. returns all arguments after argument number <code>index</code>;
  5596. a negative number indexes from the end (-1 is the last argument).
  5597. Otherwise, <code>index</code> must be the string <code>"#"</code>,
  5598. and <code>select</code> returns the total number of extra arguments it received.
  5599. <p>
  5600. <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3>
  5601. <p>
  5602. Sets the metatable for the given table.
  5603. If <code>metatable</code> is <b>nil</b>,
  5604. removes the metatable of the given table.
  5605. If the original metatable has a <code>__metatable</code> field,
  5606. raises an error.
  5607. <p>
  5608. This function returns <code>table</code>.
  5609. <p>
  5610. To change the metatable of other types from Lua code,
  5611. you must use the debug library (<a href="#6.10">&sect;6.10</a>).
  5612. <p>
  5613. <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
  5614. <p>
  5615. When called with no <code>base</code>,
  5616. <code>tonumber</code> tries to convert its argument to a number.
  5617. If the argument is already a number or
  5618. a string convertible to a number,
  5619. then <code>tonumber</code> returns this number;
  5620. otherwise, it returns <b>fail</b>.
  5621. <p>
  5622. The conversion of strings can result in integers or floats,
  5623. according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
  5624. The string may have leading and trailing spaces and a sign.
  5625. <p>
  5626. When called with <code>base</code>,
  5627. then <code>e</code> must be a string to be interpreted as
  5628. an integer numeral in that base.
  5629. The base may be any integer between 2 and 36, inclusive.
  5630. In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
  5631. represents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,
  5632. with '<code>Z</code>' representing 35.
  5633. If the string <code>e</code> is not a valid numeral in the given base,
  5634. the function returns <b>fail</b>.
  5635. <p>
  5636. <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3>
  5637. <p>
  5638. Receives a value of any type and
  5639. converts it to a string in a human-readable format.
  5640. <p>
  5641. If the metatable of <code>v</code> has a <code>__tostring</code> field,
  5642. then <code>tostring</code> calls the corresponding value
  5643. with <code>v</code> as argument,
  5644. and uses the result of the call as its result.
  5645. Otherwise, if the metatable of <code>v</code> has a <code>__name</code> field
  5646. with a string value,
  5647. <code>tostring</code> may use that string in its final result.
  5648. <p>
  5649. For complete control of how numbers are converted,
  5650. use <a href="#pdf-string.format"><code>string.format</code></a>.
  5651. <p>
  5652. <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3>
  5653. <p>
  5654. Returns the type of its only argument, coded as a string.
  5655. The possible results of this function are
  5656. "<code>nil</code>" (a string, not the value <b>nil</b>),
  5657. "<code>number</code>",
  5658. "<code>string</code>",
  5659. "<code>boolean</code>",
  5660. "<code>table</code>",
  5661. "<code>function</code>",
  5662. "<code>thread</code>",
  5663. and "<code>userdata</code>".
  5664. <p>
  5665. <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3>
  5666. <p>
  5667. A global variable (not a function) that
  5668. holds a string containing the running Lua version.
  5669. The current value of this variable is "<code>Lua 5.4</code>".
  5670. <p>
  5671. <hr><h3><a name="pdf-warn"><code>warn (msg1, &middot;&middot;&middot;)</code></a></h3>
  5672. <p>
  5673. Emits a warning with a message composed by the concatenation
  5674. of all its arguments (which should be strings).
  5675. <p>
  5676. By convention,
  5677. a one-piece message starting with '<code>@</code>'
  5678. is intended to be a <em>control message</em>,
  5679. which is a message to the warning system itself.
  5680. In particular, the standard warning function in Lua
  5681. recognizes the control messages "<code>@off</code>",
  5682. to stop the emission of warnings,
  5683. and "<code>@on</code>", to (re)start the emission;
  5684. it ignores unknown control messages.
  5685. <p>
  5686. <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, &middot;&middot;&middot;])</code></a></h3>
  5687. <p>
  5688. This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
  5689. except that it sets a new message handler <code>msgh</code>.
  5690. <h2>6.2 &ndash; <a name="6.2">Coroutine Manipulation</a></h2>
  5691. <p>
  5692. This library comprises the operations to manipulate coroutines,
  5693. which come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>.
  5694. See <a href="#2.6">&sect;2.6</a> for a general description of coroutines.
  5695. <p>
  5696. <hr><h3><a name="pdf-coroutine.close"><code>coroutine.close (co)</code></a></h3>
  5697. <p>
  5698. Closes coroutine <code>co</code>,
  5699. that is,
  5700. closes all its pending to-be-closed variables
  5701. and puts the coroutine in a dead state.
  5702. The given coroutine must be dead or suspended.
  5703. In case of error closing some variable,
  5704. returns <b>false</b> plus the error object;
  5705. otherwise returns <b>true</b>.
  5706. <p>
  5707. <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3>
  5708. <p>
  5709. Creates a new coroutine, with body <code>f</code>.
  5710. <code>f</code> must be a function.
  5711. Returns this new coroutine,
  5712. an object with type <code>"thread"</code>.
  5713. <p>
  5714. <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ([co])</code></a></h3>
  5715. <p>
  5716. Returns true when the coroutine <code>co</code> can yield.
  5717. The default for <code>co</code> is the running coroutine.
  5718. <p>
  5719. A coroutine is yieldable if it is not the main thread and
  5720. it is not inside a non-yieldable C&nbsp;function.
  5721. <p>
  5722. <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>
  5723. <p>
  5724. Starts or continues the execution of coroutine <code>co</code>.
  5725. The first time you resume a coroutine,
  5726. it starts running its body.
  5727. The values <code>val1</code>, ... are passed
  5728. as the arguments to the body function.
  5729. If the coroutine has yielded,
  5730. <code>resume</code> restarts it;
  5731. the values <code>val1</code>, ... are passed
  5732. as the results from the yield.
  5733. <p>
  5734. If the coroutine runs without any errors,
  5735. <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>
  5736. (when the coroutine yields) or any values returned by the body function
  5737. (when the coroutine terminates).
  5738. If there is any error,
  5739. <code>resume</code> returns <b>false</b> plus the error message.
  5740. <p>
  5741. <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3>
  5742. <p>
  5743. Returns the running coroutine plus a boolean,
  5744. true when the running coroutine is the main one.
  5745. <p>
  5746. <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3>
  5747. <p>
  5748. Returns the status of the coroutine <code>co</code>, as a string:
  5749. <code>"running"</code>,
  5750. if the coroutine is running
  5751. (that is, it is the one that called <code>status</code>);
  5752. <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>,
  5753. or if it has not started running yet;
  5754. <code>"normal"</code> if the coroutine is active but not running
  5755. (that is, it has resumed another coroutine);
  5756. and <code>"dead"</code> if the coroutine has finished its body function,
  5757. or if it has stopped with an error.
  5758. <p>
  5759. <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3>
  5760. <p>
  5761. Creates a new coroutine, with body <code>f</code>;
  5762. <code>f</code> must be a function.
  5763. Returns a function that resumes the coroutine each time it is called.
  5764. Any arguments passed to this function behave as the
  5765. extra arguments to <code>resume</code>.
  5766. The function returns the same values returned by <code>resume</code>,
  5767. except the first boolean.
  5768. In case of error,
  5769. the function closes the coroutine and propagates the error.
  5770. <p>
  5771. <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (&middot;&middot;&middot;)</code></a></h3>
  5772. <p>
  5773. Suspends the execution of the calling coroutine.
  5774. Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>.
  5775. <h2>6.3 &ndash; <a name="6.3">Modules</a></h2>
  5776. <p>
  5777. The package library provides basic
  5778. facilities for loading modules in Lua.
  5779. It exports one function directly in the global environment:
  5780. <a href="#pdf-require"><code>require</code></a>.
  5781. Everything else is exported in the table <a name="pdf-package"><code>package</code></a>.
  5782. <p>
  5783. <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
  5784. <p>
  5785. Loads the given module.
  5786. The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table
  5787. to determine whether <code>modname</code> is already loaded.
  5788. If it is, then <code>require</code> returns the value stored
  5789. at <code>package.loaded[modname]</code>.
  5790. (The absence of a second result in this case
  5791. signals that this call did not have to load the module.)
  5792. Otherwise, it tries to find a <em>loader</em> for the module.
  5793. <p>
  5794. To find a loader,
  5795. <code>require</code> is guided by the table <a href="#pdf-package.searchers"><code>package.searchers</code></a>.
  5796. Each item in this table is a search function,
  5797. that searches for the module in a particular way.
  5798. By changing this table,
  5799. we can change how <code>require</code> looks for a module.
  5800. The following explanation is based on the default configuration
  5801. for <a href="#pdf-package.searchers"><code>package.searchers</code></a>.
  5802. <p>
  5803. First <code>require</code> queries <code>package.preload[modname]</code>.
  5804. If it has a value,
  5805. this value (which must be a function) is the loader.
  5806. Otherwise <code>require</code> searches for a Lua loader using the
  5807. path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
  5808. If that also fails, it searches for a C&nbsp;loader using the
  5809. path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
  5810. If that also fails,
  5811. it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>).
  5812. <p>
  5813. Once a loader is found,
  5814. <code>require</code> calls the loader with two arguments:
  5815. <code>modname</code> and an extra value,
  5816. a <em>loader data</em>,
  5817. also returned by the searcher.
  5818. The loader data can be any value useful to the module;
  5819. for the default searchers,
  5820. it indicates where the loader was found.
  5821. (For instance, if the loader came from a file,
  5822. this extra value is the file path.)
  5823. If the loader returns any non-nil value,
  5824. <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
  5825. If the loader does not return a non-nil value and
  5826. has not assigned any value to <code>package.loaded[modname]</code>,
  5827. then <code>require</code> assigns <b>true</b> to this entry.
  5828. In any case, <code>require</code> returns the
  5829. final value of <code>package.loaded[modname]</code>.
  5830. Besides that value, <code>require</code> also returns as a second result
  5831. the loader data returned by the searcher,
  5832. which indicates how <code>require</code> found the module.
  5833. <p>
  5834. If there is any error loading or running the module,
  5835. or if it cannot find any loader for the module,
  5836. then <code>require</code> raises an error.
  5837. <p>
  5838. <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3>
  5839. <p>
  5840. A string describing some compile-time configurations for packages.
  5841. This string is a sequence of lines:
  5842. <ul>
  5843. <li>The first line is the directory separator string.
  5844. Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li>
  5845. <li>The second line is the character that separates templates in a path.
  5846. Default is '<code>;</code>'.</li>
  5847. <li>The third line is the string that marks the
  5848. substitution points in a template.
  5849. Default is '<code>?</code>'.</li>
  5850. <li>The fourth line is a string that, in a path in Windows,
  5851. is replaced by the executable's directory.
  5852. Default is '<code>!</code>'.</li>
  5853. <li>The fifth line is a mark to ignore all text after it
  5854. when building the <code>luaopen_</code> function name.
  5855. Default is '<code>-</code>'.</li>
  5856. </ul>
  5857. <p>
  5858. <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
  5859. <p>
  5860. A string with the path used by <a href="#pdf-require"><code>require</code></a>
  5861. to search for a C&nbsp;loader.
  5862. <p>
  5863. Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
  5864. it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
  5865. using the environment variable <a name="pdf-LUA_CPATH_5_4"><code>LUA_CPATH_5_4</code></a>,
  5866. or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>,
  5867. or a default path defined in <code>luaconf.h</code>.
  5868. <p>
  5869. <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
  5870. <p>
  5871. A table used by <a href="#pdf-require"><code>require</code></a> to control which
  5872. modules are already loaded.
  5873. When you require a module <code>modname</code> and
  5874. <code>package.loaded[modname]</code> is not false,
  5875. <a href="#pdf-require"><code>require</code></a> simply returns the value stored there.
  5876. <p>
  5877. This variable is only a reference to the real table;
  5878. assignments to this variable do not change the
  5879. table used by <a href="#pdf-require"><code>require</code></a>.
  5880. <p>
  5881. <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
  5882. <p>
  5883. Dynamically links the host program with the C&nbsp;library <code>libname</code>.
  5884. <p>
  5885. If <code>funcname</code> is "<code>*</code>",
  5886. then it only links with the library,
  5887. making the symbols exported by the library
  5888. available to other dynamically linked libraries.
  5889. Otherwise,
  5890. it looks for a function <code>funcname</code> inside the library
  5891. and returns this function as a C&nbsp;function.
  5892. So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype
  5893. (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  5894. <p>
  5895. This is a low-level function.
  5896. It completely bypasses the package and module system.
  5897. Unlike <a href="#pdf-require"><code>require</code></a>,
  5898. it does not perform any path searching and
  5899. does not automatically adds extensions.
  5900. <code>libname</code> must be the complete file name of the C&nbsp;library,
  5901. including if necessary a path and an extension.
  5902. <code>funcname</code> must be the exact name exported by the C&nbsp;library
  5903. (which may depend on the C&nbsp;compiler and linker used).
  5904. <p>
  5905. This function is not supported by Standard&nbsp;C.
  5906. As such, it is only available on some platforms
  5907. (Windows, Linux, Mac OS X, Solaris, BSD,
  5908. plus other Unix systems that support the <code>dlfcn</code> standard).
  5909. <p>
  5910. <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
  5911. <p>
  5912. A string with the path used by <a href="#pdf-require"><code>require</code></a>
  5913. to search for a Lua loader.
  5914. <p>
  5915. At start-up, Lua initializes this variable with
  5916. the value of the environment variable <a name="pdf-LUA_PATH_5_4"><code>LUA_PATH_5_4</code></a> or
  5917. the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
  5918. with a default path defined in <code>luaconf.h</code>,
  5919. if those environment variables are not defined.
  5920. A "<code>;;</code>" in the value of the environment variable
  5921. is replaced by the default path.
  5922. <p>
  5923. <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
  5924. <p>
  5925. A table to store loaders for specific modules
  5926. (see <a href="#pdf-require"><code>require</code></a>).
  5927. <p>
  5928. This variable is only a reference to the real table;
  5929. assignments to this variable do not change the
  5930. table used by <a href="#pdf-require"><code>require</code></a>.
  5931. <p>
  5932. <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3>
  5933. <p>
  5934. A table used by <a href="#pdf-require"><code>require</code></a> to control how to find modules.
  5935. <p>
  5936. Each entry in this table is a <em>searcher function</em>.
  5937. When looking for a module,
  5938. <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
  5939. with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
  5940. sole argument.
  5941. If the searcher finds the module,
  5942. it returns another function, the module <em>loader</em>,
  5943. plus an extra value, a <em>loader data</em>,
  5944. that will be passed to that loader and
  5945. returned as a second result by <a href="#pdf-require"><code>require</code></a>.
  5946. If it cannot find the module,
  5947. it returns a string explaining why
  5948. (or <b>nil</b> if it has nothing to say).
  5949. <p>
  5950. Lua initializes this table with four searcher functions.
  5951. <p>
  5952. The first searcher simply looks for a loader in the
  5953. <a href="#pdf-package.preload"><code>package.preload</code></a> table.
  5954. <p>
  5955. The second searcher looks for a loader as a Lua library,
  5956. using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
  5957. The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
  5958. <p>
  5959. The third searcher looks for a loader as a C&nbsp;library,
  5960. using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
  5961. Again,
  5962. the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
  5963. For instance,
  5964. if the C&nbsp;path is the string
  5965. <pre>
  5966. "./?.so;./?.dll;/usr/local/?/init.so"
  5967. </pre><p>
  5968. the searcher for module <code>foo</code>
  5969. will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,
  5970. and <code>/usr/local/foo/init.so</code>, in that order.
  5971. Once it finds a C&nbsp;library,
  5972. this searcher first uses a dynamic link facility to link the
  5973. application with the library.
  5974. Then it tries to find a C&nbsp;function inside the library to
  5975. be used as the loader.
  5976. The name of this C&nbsp;function is the string "<code>luaopen_</code>"
  5977. concatenated with a copy of the module name where each dot
  5978. is replaced by an underscore.
  5979. Moreover, if the module name has a hyphen,
  5980. its suffix after (and including) the first hyphen is removed.
  5981. For instance, if the module name is <code>a.b.c-v2.1</code>,
  5982. the function name will be <code>luaopen_a_b_c</code>.
  5983. <p>
  5984. The fourth searcher tries an <em>all-in-one loader</em>.
  5985. It searches the C&nbsp;path for a library for
  5986. the root name of the given module.
  5987. For instance, when requiring <code>a.b.c</code>,
  5988. it will search for a C&nbsp;library for <code>a</code>.
  5989. If found, it looks into it for an open function for
  5990. the submodule;
  5991. in our example, that would be <code>luaopen_a_b_c</code>.
  5992. With this facility, a package can pack several C&nbsp;submodules
  5993. into one single library,
  5994. with each submodule keeping its original open function.
  5995. <p>
  5996. All searchers except the first one (preload) return as the extra value
  5997. the file path where the module was found,
  5998. as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
  5999. The first searcher always returns the string "<code>:preload:</code>".
  6000. <p>
  6001. Searchers should raise no errors and have no side effects in Lua.
  6002. (They may have side effects in C,
  6003. for instance by linking the application with a library.)
  6004. <p>
  6005. <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3>
  6006. <p>
  6007. Searches for the given <code>name</code> in the given <code>path</code>.
  6008. <p>
  6009. A path is a string containing a sequence of
  6010. <em>templates</em> separated by semicolons.
  6011. For each template,
  6012. the function replaces each interrogation mark (if any)
  6013. in the template with a copy of <code>name</code>
  6014. wherein all occurrences of <code>sep</code>
  6015. (a dot, by default)
  6016. were replaced by <code>rep</code>
  6017. (the system's directory separator, by default),
  6018. and then tries to open the resulting file name.
  6019. <p>
  6020. For instance, if the path is the string
  6021. <pre>
  6022. "./?.lua;./?.lc;/usr/local/?/init.lua"
  6023. </pre><p>
  6024. the search for the name <code>foo.a</code>
  6025. will try to open the files
  6026. <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and
  6027. <code>/usr/local/foo/a/init.lua</code>, in that order.
  6028. <p>
  6029. Returns the resulting name of the first file that it can
  6030. open in read mode (after closing the file),
  6031. or <b>fail</b> plus an error message if none succeeds.
  6032. (This error message lists all file names it tried to open.)
  6033. <h2>6.4 &ndash; <a name="6.4">String Manipulation</a></h2>
  6034. <p>
  6035. This library provides generic functions for string manipulation,
  6036. such as finding and extracting substrings, and pattern matching.
  6037. When indexing a string in Lua, the first character is at position&nbsp;1
  6038. (not at&nbsp;0, as in C).
  6039. Indices are allowed to be negative and are interpreted as indexing backwards,
  6040. from the end of the string.
  6041. Thus, the last character is at position -1, and so on.
  6042. <p>
  6043. The string library provides all its functions inside the table
  6044. <a name="pdf-string"><code>string</code></a>.
  6045. It also sets a metatable for strings
  6046. where the <code>__index</code> field points to the <code>string</code> table.
  6047. Therefore, you can use the string functions in object-oriented style.
  6048. For instance, <code>string.byte(s,i)</code>
  6049. can be written as <code>s:byte(i)</code>.
  6050. <p>
  6051. The string library assumes one-byte character encodings.
  6052. <p>
  6053. <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
  6054. Returns the internal numeric codes of the characters <code>s[i]</code>,
  6055. <code>s[i+1]</code>, ..., <code>s[j]</code>.
  6056. The default value for <code>i</code> is&nbsp;1;
  6057. the default value for <code>j</code> is&nbsp;<code>i</code>.
  6058. These indices are corrected
  6059. following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>.
  6060. <p>
  6061. Numeric codes are not necessarily portable across platforms.
  6062. <p>
  6063. <hr><h3><a name="pdf-string.char"><code>string.char (&middot;&middot;&middot;)</code></a></h3>
  6064. Receives zero or more integers.
  6065. Returns a string with length equal to the number of arguments,
  6066. in which each character has the internal numeric code equal
  6067. to its corresponding argument.
  6068. <p>
  6069. Numeric codes are not necessarily portable across platforms.
  6070. <p>
  6071. <hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3>
  6072. <p>
  6073. Returns a string containing a binary representation
  6074. (a <em>binary chunk</em>)
  6075. of the given function,
  6076. so that a later <a href="#pdf-load"><code>load</code></a> on this string returns
  6077. a copy of the function (but with new upvalues).
  6078. If <code>strip</code> is a true value,
  6079. the binary representation may not include all debug information
  6080. about the function,
  6081. to save space.
  6082. <p>
  6083. Functions with upvalues have only their number of upvalues saved.
  6084. When (re)loaded,
  6085. those upvalues receive fresh instances.
  6086. (See the <a href="#pdf-load"><code>load</code></a> function for details about
  6087. how these upvalues are initialized.
  6088. You can use the debug library to serialize
  6089. and reload the upvalues of a function
  6090. in a way adequate to your needs.)
  6091. <p>
  6092. <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
  6093. <p>
  6094. Looks for the first match of
  6095. <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
  6096. If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
  6097. where this occurrence starts and ends;
  6098. otherwise, it returns <b>fail</b>.
  6099. A third, optional numeric argument <code>init</code> specifies
  6100. where to start the search;
  6101. its default value is&nbsp;1 and can be negative.
  6102. A value of <b>true</b> as a fourth, optional argument <code>plain</code>
  6103. turns off the pattern matching facilities,
  6104. so the function does a plain "find substring" operation,
  6105. with no characters in <code>pattern</code> being considered magic.
  6106. <p>
  6107. If the pattern has captures,
  6108. then in a successful match
  6109. the captured values are also returned,
  6110. after the two indices.
  6111. <p>
  6112. <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, &middot;&middot;&middot;)</code></a></h3>
  6113. <p>
  6114. Returns a formatted version of its variable number of arguments
  6115. following the description given in its first argument,
  6116. which must be a string.
  6117. The format string follows the same rules as the ISO&nbsp;C function <code>sprintf</code>.
  6118. The only differences are that the conversion specifiers and modifiers
  6119. <code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, and <code>n</code> are not supported
  6120. and that there is an extra specifier, <code>q</code>.
  6121. <p>
  6122. The specifier <code>q</code> formats booleans, nil, numbers, and strings
  6123. in a way that the result is a valid constant in Lua source code.
  6124. Booleans and nil are written in the obvious way
  6125. (<code>true</code>, <code>false</code>, <code>nil</code>).
  6126. Floats are written in hexadecimal,
  6127. to preserve full precision.
  6128. A string is written between double quotes,
  6129. using escape sequences when necessary to ensure that
  6130. it can safely be read back by the Lua interpreter.
  6131. For instance, the call
  6132. <pre>
  6133. string.format('%q', 'a string with "quotes" and \n new line')
  6134. </pre><p>
  6135. may produce the string:
  6136. <pre>
  6137. "a string with \"quotes\" and \
  6138. new line"
  6139. </pre><p>
  6140. This specifier does not support modifiers (flags, width, length).
  6141. <p>
  6142. The conversion specifiers
  6143. <code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>,
  6144. <code>G</code>, and <code>g</code> all expect a number as argument.
  6145. The specifiers <code>c</code>, <code>d</code>,
  6146. <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
  6147. expect an integer.
  6148. When Lua is compiled with a C89 compiler,
  6149. the specifiers <code>A</code> and <code>a</code> (hexadecimal floats)
  6150. do not support modifiers.
  6151. <p>
  6152. The specifier <code>s</code> expects a string;
  6153. if its argument is not a string,
  6154. it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
  6155. If the specifier has any modifier,
  6156. the corresponding string argument should not contain embedded zeros.
  6157. <p>
  6158. The specifier <code>p</code> formats the pointer
  6159. returned by <a href="#lua_topointer"><code>lua_topointer</code></a>.
  6160. That gives a unique string identifier for tables, userdata,
  6161. threads, strings, and functions.
  6162. For other values (numbers, nil, booleans),
  6163. this specifier results in a string representing
  6164. the pointer <code>NULL</code>.
  6165. <p>
  6166. <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern [, init])</code></a></h3>
  6167. Returns an iterator function that,
  6168. each time it is called,
  6169. returns the next captures from <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>)
  6170. over the string <code>s</code>.
  6171. If <code>pattern</code> specifies no captures,
  6172. then the whole match is produced in each call.
  6173. A third, optional numeric argument <code>init</code> specifies
  6174. where to start the search;
  6175. its default value is&nbsp;1 and can be negative.
  6176. <p>
  6177. As an example, the following loop
  6178. will iterate over all the words from string <code>s</code>,
  6179. printing one per line:
  6180. <pre>
  6181. s = "hello world from Lua"
  6182. for w in string.gmatch(s, "%a+") do
  6183. print(w)
  6184. end
  6185. </pre><p>
  6186. The next example collects all pairs <code>key=value</code> from the
  6187. given string into a table:
  6188. <pre>
  6189. t = {}
  6190. s = "from=world, to=Lua"
  6191. for k, v in string.gmatch(s, "(%w+)=(%w+)") do
  6192. t[k] = v
  6193. end
  6194. </pre>
  6195. <p>
  6196. For this function, a caret '<code>^</code>' at the start of a pattern does not
  6197. work as an anchor, as this would prevent the iteration.
  6198. <p>
  6199. <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
  6200. Returns a copy of <code>s</code>
  6201. in which all (or the first <code>n</code>, if given)
  6202. occurrences of the <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) have been
  6203. replaced by a replacement string specified by <code>repl</code>,
  6204. which can be a string, a table, or a function.
  6205. <code>gsub</code> also returns, as its second value,
  6206. the total number of matches that occurred.
  6207. The name <code>gsub</code> comes from <em>Global SUBstitution</em>.
  6208. <p>
  6209. If <code>repl</code> is a string, then its value is used for replacement.
  6210. The character&nbsp;<code>%</code> works as an escape character:
  6211. any sequence in <code>repl</code> of the form <code>%<em>d</em></code>,
  6212. with <em>d</em> between 1 and 9,
  6213. stands for the value of the <em>d</em>-th captured substring;
  6214. the sequence <code>%0</code> stands for the whole match;
  6215. the sequence <code>%%</code> stands for a single&nbsp;<code>%</code>.
  6216. <p>
  6217. If <code>repl</code> is a table, then the table is queried for every match,
  6218. using the first capture as the key.
  6219. <p>
  6220. If <code>repl</code> is a function, then this function is called every time a
  6221. match occurs, with all captured substrings passed as arguments,
  6222. in order.
  6223. <p>
  6224. In any case,
  6225. if the pattern specifies no captures,
  6226. then it behaves as if the whole pattern was inside a capture.
  6227. <p>
  6228. If the value returned by the table query or by the function call
  6229. is a string or a number,
  6230. then it is used as the replacement string;
  6231. otherwise, if it is <b>false</b> or <b>nil</b>,
  6232. then there is no replacement
  6233. (that is, the original match is kept in the string).
  6234. <p>
  6235. Here are some examples:
  6236. <pre>
  6237. x = string.gsub("hello world", "(%w+)", "%1 %1")
  6238. --&gt; x="hello hello world world"
  6239. x = string.gsub("hello world", "%w+", "%0 %0", 1)
  6240. --&gt; x="hello hello world"
  6241. x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
  6242. --&gt; x="world hello Lua from"
  6243. x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
  6244. --&gt; x="home = /home/roberto, user = roberto"
  6245. x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
  6246. return load(s)()
  6247. end)
  6248. --&gt; x="4+5 = 9"
  6249. local t = {name="lua", version="5.4"}
  6250. x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
  6251. --&gt; x="lua-5.4.tar.gz"
  6252. </pre>
  6253. <p>
  6254. <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3>
  6255. <p>
  6256. Receives a string and returns its length.
  6257. The empty string <code>""</code> has length 0.
  6258. Embedded zeros are counted,
  6259. so <code>"a\000bc\000"</code> has length 5.
  6260. <p>
  6261. <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3>
  6262. <p>
  6263. Receives a string and returns a copy of this string with all
  6264. uppercase letters changed to lowercase.
  6265. All other characters are left unchanged.
  6266. The definition of what an uppercase letter is depends on the current locale.
  6267. <p>
  6268. <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
  6269. <p>
  6270. Looks for the first <em>match</em> of
  6271. the <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
  6272. If it finds one, then <code>match</code> returns
  6273. the captures from the pattern;
  6274. otherwise it returns <b>fail</b>.
  6275. If <code>pattern</code> specifies no captures,
  6276. then the whole match is returned.
  6277. A third, optional numeric argument <code>init</code> specifies
  6278. where to start the search;
  6279. its default value is&nbsp;1 and can be negative.
  6280. <p>
  6281. <hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, &middot;&middot;&middot;)</code></a></h3>
  6282. <p>
  6283. Returns a binary string containing the values <code>v1</code>, <code>v2</code>, etc.
  6284. serialized in binary form (packed)
  6285. according to the format string <code>fmt</code> (see <a href="#6.4.2">&sect;6.4.2</a>).
  6286. <p>
  6287. <hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3>
  6288. <p>
  6289. Returns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a>
  6290. with the given format.
  6291. The format string cannot have the variable-length options
  6292. '<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">&sect;6.4.2</a>).
  6293. <p>
  6294. <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
  6295. <p>
  6296. Returns a string that is the concatenation of <code>n</code> copies of
  6297. the string <code>s</code> separated by the string <code>sep</code>.
  6298. The default value for <code>sep</code> is the empty string
  6299. (that is, no separator).
  6300. Returns the empty string if <code>n</code> is not positive.
  6301. <p>
  6302. (Note that it is very easy to exhaust the memory of your machine
  6303. with a single call to this function.)
  6304. <p>
  6305. <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3>
  6306. <p>
  6307. Returns a string that is the string <code>s</code> reversed.
  6308. <p>
  6309. <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3>
  6310. <p>
  6311. Returns the substring of <code>s</code> that
  6312. starts at <code>i</code> and continues until <code>j</code>;
  6313. <code>i</code> and <code>j</code> can be negative.
  6314. If <code>j</code> is absent, then it is assumed to be equal to -1
  6315. (which is the same as the string length).
  6316. In particular,
  6317. the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
  6318. with length <code>j</code>,
  6319. and <code>string.sub(s, -i)</code> (for a positive <code>i</code>)
  6320. returns a suffix of <code>s</code>
  6321. with length <code>i</code>.
  6322. <p>
  6323. If, after the translation of negative indices,
  6324. <code>i</code> is less than 1,
  6325. it is corrected to 1.
  6326. If <code>j</code> is greater than the string length,
  6327. it is corrected to that length.
  6328. If, after these corrections,
  6329. <code>i</code> is greater than <code>j</code>,
  6330. the function returns the empty string.
  6331. <p>
  6332. <hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3>
  6333. <p>
  6334. Returns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>)
  6335. according to the format string <code>fmt</code> (see <a href="#6.4.2">&sect;6.4.2</a>).
  6336. An optional <code>pos</code> marks where
  6337. to start reading in <code>s</code> (default is 1).
  6338. After the read values,
  6339. this function also returns the index of the first unread byte in <code>s</code>.
  6340. <p>
  6341. <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3>
  6342. <p>
  6343. Receives a string and returns a copy of this string with all
  6344. lowercase letters changed to uppercase.
  6345. All other characters are left unchanged.
  6346. The definition of what a lowercase letter is depends on the current locale.
  6347. <h3>6.4.1 &ndash; <a name="6.4.1">Patterns</a></h3>
  6348. <p>
  6349. Patterns in Lua are described by regular strings,
  6350. which are interpreted as patterns by the pattern-matching functions
  6351. <a href="#pdf-string.find"><code>string.find</code></a>,
  6352. <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>,
  6353. <a href="#pdf-string.gsub"><code>string.gsub</code></a>,
  6354. and <a href="#pdf-string.match"><code>string.match</code></a>.
  6355. This section describes the syntax and the meaning
  6356. (that is, what they match) of these strings.
  6357. <h4>Character Class:</h4><p>
  6358. A <em>character class</em> is used to represent a set of characters.
  6359. The following combinations are allowed in describing a character class:
  6360. <ul>
  6361. <li><b><em>x</em>: </b>
  6362. (where <em>x</em> is not one of the <em>magic characters</em>
  6363. <code>^$()%.[]*+-?</code>)
  6364. represents the character <em>x</em> itself.
  6365. </li>
  6366. <li><b><code>.</code>: </b> (a dot) represents all characters.</li>
  6367. <li><b><code>%a</code>: </b> represents all letters.</li>
  6368. <li><b><code>%c</code>: </b> represents all control characters.</li>
  6369. <li><b><code>%d</code>: </b> represents all digits.</li>
  6370. <li><b><code>%g</code>: </b> represents all printable characters except space.</li>
  6371. <li><b><code>%l</code>: </b> represents all lowercase letters.</li>
  6372. <li><b><code>%p</code>: </b> represents all punctuation characters.</li>
  6373. <li><b><code>%s</code>: </b> represents all space characters.</li>
  6374. <li><b><code>%u</code>: </b> represents all uppercase letters.</li>
  6375. <li><b><code>%w</code>: </b> represents all alphanumeric characters.</li>
  6376. <li><b><code>%x</code>: </b> represents all hexadecimal digits.</li>
  6377. <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character)
  6378. represents the character <em>x</em>.
  6379. This is the standard way to escape the magic characters.
  6380. Any non-alphanumeric character
  6381. (including all punctuation characters, even the non-magical)
  6382. can be preceded by a '<code>%</code>' to represent itself in a pattern.
  6383. </li>
  6384. <li><b><code>[<em>set</em>]</code>: </b>
  6385. represents the class which is the union of all
  6386. characters in <em>set</em>.
  6387. A range of characters can be specified by
  6388. separating the end characters of the range,
  6389. in ascending order, with a '<code>-</code>'.
  6390. All classes <code>%</code><em>x</em> described above can also be used as
  6391. components in <em>set</em>.
  6392. All other characters in <em>set</em> represent themselves.
  6393. For example, <code>[%w_]</code> (or <code>[_%w]</code>)
  6394. represents all alphanumeric characters plus the underscore,
  6395. <code>[0-7]</code> represents the octal digits,
  6396. and <code>[0-7%l%-]</code> represents the octal digits plus
  6397. the lowercase letters plus the '<code>-</code>' character.
  6398. <p>
  6399. You can put a closing square bracket in a set
  6400. by positioning it as the first character in the set.
  6401. You can put a hyphen in a set
  6402. by positioning it as the first or the last character in the set.
  6403. (You can also use an escape for both cases.)
  6404. <p>
  6405. The interaction between ranges and classes is not defined.
  6406. Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>
  6407. have no meaning.
  6408. </li>
  6409. <li><b><code>[^<em>set</em>]</code>: </b>
  6410. represents the complement of <em>set</em>,
  6411. where <em>set</em> is interpreted as above.
  6412. </li>
  6413. </ul><p>
  6414. For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),
  6415. the corresponding uppercase letter represents the complement of the class.
  6416. For instance, <code>%S</code> represents all non-space characters.
  6417. <p>
  6418. The definitions of letter, space, and other character groups
  6419. depend on the current locale.
  6420. In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.
  6421. <h4>Pattern Item:</h4><p>
  6422. A <em>pattern item</em> can be
  6423. <ul>
  6424. <li>
  6425. a single character class,
  6426. which matches any single character in the class;
  6427. </li>
  6428. <li>
  6429. a single character class followed by '<code>*</code>',
  6430. which matches sequences of zero or more characters in the class.
  6431. These repetition items will always match the longest possible sequence;
  6432. </li>
  6433. <li>
  6434. a single character class followed by '<code>+</code>',
  6435. which matches sequences of one or more characters in the class.
  6436. These repetition items will always match the longest possible sequence;
  6437. </li>
  6438. <li>
  6439. a single character class followed by '<code>-</code>',
  6440. which also matches sequences of zero or more characters in the class.
  6441. Unlike '<code>*</code>',
  6442. these repetition items will always match the shortest possible sequence;
  6443. </li>
  6444. <li>
  6445. a single character class followed by '<code>?</code>',
  6446. which matches zero or one occurrence of a character in the class.
  6447. It always matches one occurrence if possible;
  6448. </li>
  6449. <li>
  6450. <code>%<em>n</em></code>, for <em>n</em> between 1 and 9;
  6451. such item matches a substring equal to the <em>n</em>-th captured string
  6452. (see below);
  6453. </li>
  6454. <li>
  6455. <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters;
  6456. such item matches strings that start with&nbsp;<em>x</em>, end with&nbsp;<em>y</em>,
  6457. and where the <em>x</em> and <em>y</em> are <em>balanced</em>.
  6458. This means that, if one reads the string from left to right,
  6459. counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,
  6460. the ending <em>y</em> is the first <em>y</em> where the count reaches 0.
  6461. For instance, the item <code>%b()</code> matches expressions with
  6462. balanced parentheses.
  6463. </li>
  6464. <li>
  6465. <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>;
  6466. such item matches an empty string at any position such that
  6467. the next character belongs to <em>set</em>
  6468. and the previous character does not belong to <em>set</em>.
  6469. The set <em>set</em> is interpreted as previously described.
  6470. The beginning and the end of the subject are handled as if
  6471. they were the character '<code>\0</code>'.
  6472. </li>
  6473. </ul>
  6474. <h4>Pattern:</h4><p>
  6475. A <em>pattern</em> is a sequence of pattern items.
  6476. A caret '<code>^</code>' at the beginning of a pattern anchors the match at the
  6477. beginning of the subject string.
  6478. A '<code>$</code>' at the end of a pattern anchors the match at the
  6479. end of the subject string.
  6480. At other positions,
  6481. '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves.
  6482. <h4>Captures:</h4><p>
  6483. A pattern can contain sub-patterns enclosed in parentheses;
  6484. they describe <em>captures</em>.
  6485. When a match succeeds, the substrings of the subject string
  6486. that match captures are stored (<em>captured</em>) for future use.
  6487. Captures are numbered according to their left parentheses.
  6488. For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>,
  6489. the part of the string matching <code>"a*(.)%w(%s*)"</code> is
  6490. stored as the first capture, and therefore has number&nbsp;1;
  6491. the character matching "<code>.</code>" is captured with number&nbsp;2,
  6492. and the part matching "<code>%s*</code>" has number&nbsp;3.
  6493. <p>
  6494. As a special case, the capture <code>()</code> captures
  6495. the current string position (a number).
  6496. For instance, if we apply the pattern <code>"()aa()"</code> on the
  6497. string <code>"flaaap"</code>, there will be two captures: 3&nbsp;and&nbsp;5.
  6498. <h4>Multiple matches:</h4><p>
  6499. The function <a href="#pdf-string.gsub"><code>string.gsub</code></a> and the iterator <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>
  6500. match multiple occurrences of the given pattern in the subject.
  6501. For these functions,
  6502. a new match is considered valid only
  6503. if it ends at least one byte after the end of the previous match.
  6504. In other words, the pattern machine never accepts the
  6505. empty string as a match immediately after another match.
  6506. As an example,
  6507. consider the results of the following code:
  6508. <pre>
  6509. &gt; string.gsub("abc", "()a*()", print);
  6510. --&gt; 1 2
  6511. --&gt; 3 3
  6512. --&gt; 4 4
  6513. </pre><p>
  6514. The second and third results come from Lua matching an empty
  6515. string after '<code>b</code>' and another one after '<code>c</code>'.
  6516. Lua does not match an empty string after '<code>a</code>',
  6517. because it would end at the same position of the previous match.
  6518. <h3>6.4.2 &ndash; <a name="6.4.2">Format Strings for Pack and Unpack</a></h3>
  6519. <p>
  6520. The first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>,
  6521. <a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a>
  6522. is a format string,
  6523. which describes the layout of the structure being created or read.
  6524. <p>
  6525. A format string is a sequence of conversion options.
  6526. The conversion options are as follows:
  6527. <ul>
  6528. <li><b><code>&lt;</code>: </b>sets little endian</li>
  6529. <li><b><code>&gt;</code>: </b>sets big endian</li>
  6530. <li><b><code>=</code>: </b>sets native endian</li>
  6531. <li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code>
  6532. (default is native alignment)</li>
  6533. <li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li>
  6534. <li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li>
  6535. <li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li>
  6536. <li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li>
  6537. <li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li>
  6538. <li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li>
  6539. <li><b><code>j</code>: </b>a <code>lua_Integer</code></li>
  6540. <li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li>
  6541. <li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li>
  6542. <li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes
  6543. (default is native size)</li>
  6544. <li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes
  6545. (default is native size)</li>
  6546. <li><b><code>f</code>: </b>a <code>float</code> (native size)</li>
  6547. <li><b><code>d</code>: </b>a <code>double</code> (native size)</li>
  6548. <li><b><code>n</code>: </b>a <code>lua_Number</code></li>
  6549. <li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li>
  6550. <li><b><code>z</code>: </b>a zero-terminated string</li>
  6551. <li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length
  6552. coded as an unsigned integer with <code>n</code> bytes
  6553. (default is a <code>size_t</code>)</li>
  6554. <li><b><code>x</code>: </b>one byte of padding</li>
  6555. <li><b><code>X<em>op</em></code>: </b>an empty item that aligns
  6556. according to option <code>op</code>
  6557. (which is otherwise ignored)</li>
  6558. <li><b>'<code> </code>': </b>(space) ignored</li>
  6559. </ul><p>
  6560. (A "<code>[<em>n</em>]</code>" means an optional integral numeral.)
  6561. Except for padding, spaces, and configurations
  6562. (options "<code>xX &lt;=&gt;!</code>"),
  6563. each option corresponds to an argument in <a href="#pdf-string.pack"><code>string.pack</code></a>
  6564. or a result in <a href="#pdf-string.unpack"><code>string.unpack</code></a>.
  6565. <p>
  6566. For options "<code>!<em>n</em></code>", "<code>s<em>n</em></code>", "<code>i<em>n</em></code>", and "<code>I<em>n</em></code>",
  6567. <code>n</code> can be any integer between 1 and 16.
  6568. All integral options check overflows;
  6569. <a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size;
  6570. <a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer.
  6571. For the unsigned options,
  6572. Lua integers are treated as unsigned values too.
  6573. <p>
  6574. Any format string starts as if prefixed by "<code>!1=</code>",
  6575. that is,
  6576. with maximum alignment of 1 (no alignment)
  6577. and native endianness.
  6578. <p>
  6579. Native endianness assumes that the whole system is
  6580. either big or little endian.
  6581. The packing functions will not emulate correctly the behavior
  6582. of mixed-endian formats.
  6583. <p>
  6584. Alignment works as follows:
  6585. For each option,
  6586. the format gets extra padding until the data starts
  6587. at an offset that is a multiple of the minimum between the
  6588. option size and the maximum alignment;
  6589. this minimum must be a power of 2.
  6590. Options "<code>c</code>" and "<code>z</code>" are not aligned;
  6591. option "<code>s</code>" follows the alignment of its starting integer.
  6592. <p>
  6593. All padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a>
  6594. and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>.
  6595. <h2>6.5 &ndash; <a name="6.5">UTF-8 Support</a></h2>
  6596. <p>
  6597. This library provides basic support for UTF-8 encoding.
  6598. It provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>.
  6599. This library does not provide any support for Unicode other
  6600. than the handling of the encoding.
  6601. Any operation that needs the meaning of a character,
  6602. such as character classification, is outside its scope.
  6603. <p>
  6604. Unless stated otherwise,
  6605. all functions that expect a byte position as a parameter
  6606. assume that the given position is either the start of a byte sequence
  6607. or one plus the length of the subject string.
  6608. As in the string library,
  6609. negative indices count from the end of the string.
  6610. <p>
  6611. Functions that create byte sequences
  6612. accept all values up to <code>0x7FFFFFFF</code>,
  6613. as defined in the original UTF-8 specification;
  6614. that implies byte sequences of up to six bytes.
  6615. <p>
  6616. Functions that interpret byte sequences only accept
  6617. valid sequences (well formed and not overlong).
  6618. By default, they only accept byte sequences
  6619. that result in valid Unicode code points,
  6620. rejecting values greater than <code>10FFFF</code> and surrogates.
  6621. A boolean argument <code>lax</code>, when available,
  6622. lifts these checks,
  6623. so that all values up to <code>0x7FFFFFFF</code> are accepted.
  6624. (Not well formed and overlong sequences are still rejected.)
  6625. <p>
  6626. <hr><h3><a name="pdf-utf8.char"><code>utf8.char (&middot;&middot;&middot;)</code></a></h3>
  6627. <p>
  6628. Receives zero or more integers,
  6629. converts each one to its corresponding UTF-8 byte sequence
  6630. and returns a string with the concatenation of all these sequences.
  6631. <p>
  6632. <hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3>
  6633. <p>
  6634. The pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xFD][\x80-\xBF]*</code>"
  6635. (see <a href="#6.4.1">&sect;6.4.1</a>),
  6636. which matches exactly one UTF-8 byte sequence,
  6637. assuming that the subject is a valid UTF-8 string.
  6638. <p>
  6639. <hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s [, lax])</code></a></h3>
  6640. <p>
  6641. Returns values so that the construction
  6642. <pre>
  6643. for p, c in utf8.codes(s) do <em>body</em> end
  6644. </pre><p>
  6645. will iterate over all UTF-8 characters in string <code>s</code>,
  6646. with <code>p</code> being the position (in bytes) and <code>c</code> the code point
  6647. of each character.
  6648. It raises an error if it meets any invalid byte sequence.
  6649. <p>
  6650. <hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j [, lax]]])</code></a></h3>
  6651. <p>
  6652. Returns the code points (as integers) from all characters in <code>s</code>
  6653. that start between byte position <code>i</code> and <code>j</code> (both included).
  6654. The default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>.
  6655. It raises an error if it meets any invalid byte sequence.
  6656. <p>
  6657. <hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j [, lax]]])</code></a></h3>
  6658. <p>
  6659. Returns the number of UTF-8 characters in string <code>s</code>
  6660. that start between positions <code>i</code> and <code>j</code> (both inclusive).
  6661. The default for <code>i</code> is 1 and for <code>j</code> is -1.
  6662. If it finds any invalid byte sequence,
  6663. returns <b>fail</b> plus the position of the first invalid byte.
  6664. <p>
  6665. <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
  6666. <p>
  6667. Returns the position (in bytes) where the encoding of the
  6668. <code>n</code>-th character of <code>s</code>
  6669. (counting from position <code>i</code>) starts.
  6670. A negative <code>n</code> gets characters before position <code>i</code>.
  6671. The default for <code>i</code> is 1 when <code>n</code> is non-negative
  6672. and <code>#s + 1</code> otherwise,
  6673. so that <code>utf8.offset(s, -n)</code> gets the offset of the
  6674. <code>n</code>-th character from the end of the string.
  6675. If the specified character is neither in the subject
  6676. nor right after its end,
  6677. the function returns <b>fail</b>.
  6678. <p>
  6679. As a special case,
  6680. when <code>n</code> is 0 the function returns the start of the encoding
  6681. of the character that contains the <code>i</code>-th byte of <code>s</code>.
  6682. <p>
  6683. This function assumes that <code>s</code> is a valid UTF-8 string.
  6684. <h2>6.6 &ndash; <a name="6.6">Table Manipulation</a></h2>
  6685. <p>
  6686. This library provides generic functions for table manipulation.
  6687. It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
  6688. <p>
  6689. Remember that, whenever an operation needs the length of a table,
  6690. all caveats about the length operator apply (see <a href="#3.4.7">&sect;3.4.7</a>).
  6691. All functions ignore non-numeric keys
  6692. in the tables given as arguments.
  6693. <p>
  6694. <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
  6695. <p>
  6696. Given a list where all elements are strings or numbers,
  6697. returns the string <code>list[i]..sep..list[i+1] &middot;&middot;&middot; sep..list[j]</code>.
  6698. The default value for <code>sep</code> is the empty string,
  6699. the default for <code>i</code> is 1,
  6700. and the default for <code>j</code> is <code>#list</code>.
  6701. If <code>i</code> is greater than <code>j</code>, returns the empty string.
  6702. <p>
  6703. <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
  6704. <p>
  6705. Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>,
  6706. shifting up the elements
  6707. <code>list[pos], list[pos+1], &middot;&middot;&middot;, list[#list]</code>.
  6708. The default value for <code>pos</code> is <code>#list+1</code>,
  6709. so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
  6710. of the list <code>t</code>.
  6711. <p>
  6712. <hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3>
  6713. <p>
  6714. Moves elements from the table <code>a1</code> to the table <code>a2</code>,
  6715. performing the equivalent to the following
  6716. multiple assignment:
  6717. <code>a2[t],&middot;&middot;&middot; = a1[f],&middot;&middot;&middot;,a1[e]</code>.
  6718. The default for <code>a2</code> is <code>a1</code>.
  6719. The destination range can overlap with the source range.
  6720. The number of elements to be moved must fit in a Lua integer.
  6721. <p>
  6722. Returns the destination table <code>a2</code>.
  6723. <p>
  6724. <hr><h3><a name="pdf-table.pack"><code>table.pack (&middot;&middot;&middot;)</code></a></h3>
  6725. <p>
  6726. Returns a new table with all arguments stored into keys 1, 2, etc.
  6727. and with a field "<code>n</code>" with the total number of arguments.
  6728. Note that the resulting table may not be a sequence,
  6729. if some arguments are <b>nil</b>.
  6730. <p>
  6731. <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3>
  6732. <p>
  6733. Removes from <code>list</code> the element at position <code>pos</code>,
  6734. returning the value of the removed element.
  6735. When <code>pos</code> is an integer between 1 and <code>#list</code>,
  6736. it shifts down the elements
  6737. <code>list[pos+1], list[pos+2], &middot;&middot;&middot;, list[#list]</code>
  6738. and erases element <code>list[#list]</code>;
  6739. The index <code>pos</code> can also be 0 when <code>#list</code> is 0,
  6740. or <code>#list + 1</code>.
  6741. <p>
  6742. The default value for <code>pos</code> is <code>#list</code>,
  6743. so that a call <code>table.remove(l)</code> removes the last element
  6744. of the list <code>l</code>.
  6745. <p>
  6746. <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3>
  6747. <p>
  6748. Sorts the list elements in a given order, <em>in-place</em>,
  6749. from <code>list[1]</code> to <code>list[#list]</code>.
  6750. If <code>comp</code> is given,
  6751. then it must be a function that receives two list elements
  6752. and returns true when the first element must come
  6753. before the second in the final order
  6754. (so that, after the sort,
  6755. <code>i &lt; j</code> implies <code>not comp(list[j],list[i])</code>).
  6756. If <code>comp</code> is not given,
  6757. then the standard Lua operator <code>&lt;</code> is used instead.
  6758. <p>
  6759. Note that the <code>comp</code> function must define
  6760. a strict partial order over the elements in the list;
  6761. that is, it must be asymmetric and transitive.
  6762. Otherwise, no valid sort may be possible.
  6763. <p>
  6764. The sort algorithm is not stable:
  6765. elements considered equal by the given order
  6766. may have their relative positions changed by the sort.
  6767. <p>
  6768. <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3>
  6769. <p>
  6770. Returns the elements from the given list.
  6771. This function is equivalent to
  6772. <pre>
  6773. return list[i], list[i+1], &middot;&middot;&middot;, list[j]
  6774. </pre><p>
  6775. By default, <code>i</code> is&nbsp;1 and <code>j</code> is <code>#list</code>.
  6776. <h2>6.7 &ndash; <a name="6.7">Mathematical Functions</a></h2>
  6777. <p>
  6778. This library provides basic mathematical functions.
  6779. It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
  6780. Functions with the annotation "<code>integer/float</code>" give
  6781. integer results for integer arguments
  6782. and float results for non-integer arguments.
  6783. The rounding functions
  6784. <a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a>
  6785. return an integer when the result fits in the range of an integer,
  6786. or a float otherwise.
  6787. <p>
  6788. <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3>
  6789. <p>
  6790. Returns the maximum value between <code>x</code> and <code>-x</code>. (integer/float)
  6791. <p>
  6792. <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3>
  6793. <p>
  6794. Returns the arc cosine of <code>x</code> (in radians).
  6795. <p>
  6796. <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3>
  6797. <p>
  6798. Returns the arc sine of <code>x</code> (in radians).
  6799. <p>
  6800. <hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3>
  6801. <p>
  6802. Returns the arc tangent of <code>y/x</code> (in radians),
  6803. but uses the signs of both arguments to find the
  6804. quadrant of the result.
  6805. It also handles correctly the case of <code>x</code> being zero.
  6806. <p>
  6807. The default value for <code>x</code> is 1,
  6808. so that the call <code>math.atan(y)</code>
  6809. returns the arc tangent of <code>y</code>.
  6810. <p>
  6811. <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
  6812. <p>
  6813. Returns the smallest integral value greater than or equal to <code>x</code>.
  6814. <p>
  6815. <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
  6816. <p>
  6817. Returns the cosine of <code>x</code> (assumed to be in radians).
  6818. <p>
  6819. <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3>
  6820. <p>
  6821. Converts the angle <code>x</code> from radians to degrees.
  6822. <p>
  6823. <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
  6824. <p>
  6825. Returns the value <em>e<sup>x</sup></em>
  6826. (where <code>e</code> is the base of natural logarithms).
  6827. <p>
  6828. <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3>
  6829. <p>
  6830. Returns the largest integral value less than or equal to <code>x</code>.
  6831. <p>
  6832. <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3>
  6833. <p>
  6834. Returns the remainder of the division of <code>x</code> by <code>y</code>
  6835. that rounds the quotient towards zero. (integer/float)
  6836. <p>
  6837. <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3>
  6838. <p>
  6839. The float value <code>HUGE_VAL</code>,
  6840. a value greater than any other numeric value.
  6841. <p>
  6842. <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
  6843. <p>
  6844. Returns the logarithm of <code>x</code> in the given base.
  6845. The default for <code>base</code> is <em>e</em>
  6846. (so that the function returns the natural logarithm of <code>x</code>).
  6847. <p>
  6848. <hr><h3><a name="pdf-math.max"><code>math.max (x, &middot;&middot;&middot;)</code></a></h3>
  6849. <p>
  6850. Returns the argument with the maximum value,
  6851. according to the Lua operator <code>&lt;</code>.
  6852. <p>
  6853. <hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3>
  6854. An integer with the maximum value for an integer.
  6855. <p>
  6856. <hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
  6857. <p>
  6858. Returns the argument with the minimum value,
  6859. according to the Lua operator <code>&lt;</code>.
  6860. <p>
  6861. <hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
  6862. An integer with the minimum value for an integer.
  6863. <p>
  6864. <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
  6865. <p>
  6866. Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
  6867. Its second result is always a float.
  6868. <p>
  6869. <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
  6870. <p>
  6871. The value of <em>&pi;</em>.
  6872. <p>
  6873. <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3>
  6874. <p>
  6875. Converts the angle <code>x</code> from degrees to radians.
  6876. <p>
  6877. <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
  6878. <p>
  6879. When called without arguments,
  6880. returns a pseudo-random float with uniform distribution
  6881. in the range <em>[0,1)</em>.
  6882. When called with two integers <code>m</code> and <code>n</code>,
  6883. <code>math.random</code> returns a pseudo-random integer
  6884. with uniform distribution in the range <em>[m, n]</em>.
  6885. The call <code>math.random(n)</code>, for a positive <code>n</code>,
  6886. is equivalent to <code>math.random(1,n)</code>.
  6887. The call <code>math.random(0)</code> produces an integer with
  6888. all bits (pseudo)random.
  6889. <p>
  6890. This function uses the <code>xoshiro256**</code> algorithm to produce
  6891. pseudo-random 64-bit integers,
  6892. which are the results of calls with argument&nbsp;0.
  6893. Other results (ranges and floats)
  6894. are unbiased extracted from these integers.
  6895. <p>
  6896. Lua initializes its pseudo-random generator with the equivalent of
  6897. a call to <a href="#pdf-math.randomseed"><code>math.randomseed</code></a> with no arguments,
  6898. so that <code>math.random</code> should generate
  6899. different sequences of results each time the program runs.
  6900. <p>
  6901. <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed ([x [, y]])</code></a></h3>
  6902. <p>
  6903. When called with at least one argument,
  6904. the integer parameters <code>x</code> and <code>y</code> are
  6905. joined into a 128-bit <em>seed</em> that
  6906. is used to reinitialize the pseudo-random generator;
  6907. equal seeds produce equal sequences of numbers.
  6908. The default for <code>y</code> is zero.
  6909. <p>
  6910. When called with no arguments,
  6911. Lua generates a seed with
  6912. a weak attempt for randomness.
  6913. <p>
  6914. This function returns the two seed components
  6915. that were effectively used,
  6916. so that setting them again repeats the sequence.
  6917. <p>
  6918. To ensure a required level of randomness to the initial state
  6919. (or contrarily, to have a deterministic sequence,
  6920. for instance when debugging a program),
  6921. you should call <a href="#pdf-math.randomseed"><code>math.randomseed</code></a> with explicit arguments.
  6922. <p>
  6923. <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3>
  6924. <p>
  6925. Returns the sine of <code>x</code> (assumed to be in radians).
  6926. <p>
  6927. <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
  6928. <p>
  6929. Returns the square root of <code>x</code>.
  6930. (You can also use the expression <code>x^0.5</code> to compute this value.)
  6931. <p>
  6932. <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3>
  6933. <p>
  6934. Returns the tangent of <code>x</code> (assumed to be in radians).
  6935. <p>
  6936. <hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3>
  6937. <p>
  6938. If the value <code>x</code> is convertible to an integer,
  6939. returns that integer.
  6940. Otherwise, returns <b>fail</b>.
  6941. <p>
  6942. <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
  6943. <p>
  6944. Returns "<code>integer</code>" if <code>x</code> is an integer,
  6945. "<code>float</code>" if it is a float,
  6946. or <b>fail</b> if <code>x</code> is not a number.
  6947. <p>
  6948. <hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3>
  6949. <p>
  6950. Returns a boolean,
  6951. true if and only if integer <code>m</code> is below integer <code>n</code> when
  6952. they are compared as unsigned integers.
  6953. <h2>6.8 &ndash; <a name="6.8">Input and Output Facilities</a></h2>
  6954. <p>
  6955. The I/O library provides two different styles for file manipulation.
  6956. The first one uses implicit file handles;
  6957. that is, there are operations to set a default input file and a
  6958. default output file,
  6959. and all input/output operations are done over these default files.
  6960. The second style uses explicit file handles.
  6961. <p>
  6962. When using implicit file handles,
  6963. all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
  6964. When using explicit file handles,
  6965. the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle
  6966. and then all operations are supplied as methods of the file handle.
  6967. <p>
  6968. The metatable for file handles provides metamethods
  6969. for <code>__gc</code> and <code>__close</code> that try
  6970. to close the file when called.
  6971. <p>
  6972. The table <code>io</code> also provides
  6973. three predefined file handles with their usual meanings from C:
  6974. <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>.
  6975. The I/O library never closes these files.
  6976. <p>
  6977. Unless otherwise stated,
  6978. all I/O functions return <b>fail</b> on failure,
  6979. plus an error message as a second result and
  6980. a system-dependent error code as a third result,
  6981. and some non-false value on success.
  6982. On non-POSIX systems,
  6983. the computation of the error message and error code
  6984. in case of errors
  6985. may be not thread safe,
  6986. because they rely on the global C variable <code>errno</code>.
  6987. <p>
  6988. <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
  6989. <p>
  6990. Equivalent to <code>file:close()</code>.
  6991. Without a <code>file</code>, closes the default output file.
  6992. <p>
  6993. <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
  6994. <p>
  6995. Equivalent to <code>io.output():flush()</code>.
  6996. <p>
  6997. <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
  6998. <p>
  6999. When called with a file name, it opens the named file (in text mode),
  7000. and sets its handle as the default input file.
  7001. When called with a file handle,
  7002. it simply sets this file handle as the default input file.
  7003. When called without arguments,
  7004. it returns the current default input file.
  7005. <p>
  7006. In case of errors this function raises the error,
  7007. instead of returning an error code.
  7008. <p>
  7009. <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename, &middot;&middot;&middot;])</code></a></h3>
  7010. <p>
  7011. Opens the given file name in read mode
  7012. and returns an iterator function that
  7013. works like <code>file:lines(&middot;&middot;&middot;)</code> over the opened file.
  7014. When the iterator function fails to read any value,
  7015. it automatically closes the file.
  7016. Besides the iterator function,
  7017. <code>io.lines</code> returns three other values:
  7018. two <b>nil</b> values as placeholders,
  7019. plus the created file handle.
  7020. Therefore, when used in a generic <b>for</b> loop,
  7021. the file is closed also if the loop is interrupted by an
  7022. error or a <b>break</b>.
  7023. <p>
  7024. The call <code>io.lines()</code> (with no file name) is equivalent
  7025. to <code>io.input():lines("l")</code>;
  7026. that is, it iterates over the lines of the default input file.
  7027. In this case, the iterator does not close the file when the loop ends.
  7028. <p>
  7029. In case of errors opening the file,
  7030. this function raises the error,
  7031. instead of returning an error code.
  7032. <p>
  7033. <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
  7034. <p>
  7035. This function opens a file,
  7036. in the mode specified in the string <code>mode</code>.
  7037. In case of success,
  7038. it returns a new file handle.
  7039. <p>
  7040. The <code>mode</code> string can be any of the following:
  7041. <ul>
  7042. <li><b>"<code>r</code>": </b> read mode (the default);</li>
  7043. <li><b>"<code>w</code>": </b> write mode;</li>
  7044. <li><b>"<code>a</code>": </b> append mode;</li>
  7045. <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li>
  7046. <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li>
  7047. <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved,
  7048. writing is only allowed at the end of file.</li>
  7049. </ul><p>
  7050. The <code>mode</code> string can also have a '<code>b</code>' at the end,
  7051. which is needed in some systems to open the file in binary mode.
  7052. <p>
  7053. <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
  7054. <p>
  7055. Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file.
  7056. <p>
  7057. <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
  7058. <p>
  7059. This function is system dependent and is not available
  7060. on all platforms.
  7061. <p>
  7062. Starts the program <code>prog</code> in a separated process and returns
  7063. a file handle that you can use to read data from this program
  7064. (if <code>mode</code> is <code>"r"</code>, the default)
  7065. or to write data to this program
  7066. (if <code>mode</code> is <code>"w"</code>).
  7067. <p>
  7068. <hr><h3><a name="pdf-io.read"><code>io.read (&middot;&middot;&middot;)</code></a></h3>
  7069. <p>
  7070. Equivalent to <code>io.input():read(&middot;&middot;&middot;)</code>.
  7071. <p>
  7072. <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
  7073. <p>
  7074. In case of success,
  7075. returns a handle for a temporary file.
  7076. This file is opened in update mode
  7077. and it is automatically removed when the program ends.
  7078. <p>
  7079. <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
  7080. <p>
  7081. Checks whether <code>obj</code> is a valid file handle.
  7082. Returns the string <code>"file"</code> if <code>obj</code> is an open file handle,
  7083. <code>"closed file"</code> if <code>obj</code> is a closed file handle,
  7084. or <b>fail</b> if <code>obj</code> is not a file handle.
  7085. <p>
  7086. <hr><h3><a name="pdf-io.write"><code>io.write (&middot;&middot;&middot;)</code></a></h3>
  7087. <p>
  7088. Equivalent to <code>io.output():write(&middot;&middot;&middot;)</code>.
  7089. <p>
  7090. <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
  7091. <p>
  7092. Closes <code>file</code>.
  7093. Note that files are automatically closed when
  7094. their handles are garbage collected,
  7095. but that takes an unpredictable amount of time to happen.
  7096. <p>
  7097. When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
  7098. <a href="#pdf-file:close"><code>file:close</code></a> returns the same values
  7099. returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
  7100. <p>
  7101. <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
  7102. <p>
  7103. Saves any written data to <code>file</code>.
  7104. <p>
  7105. <hr><h3><a name="pdf-file:lines"><code>file:lines (&middot;&middot;&middot;)</code></a></h3>
  7106. <p>
  7107. Returns an iterator function that,
  7108. each time it is called,
  7109. reads the file according to the given formats.
  7110. When no format is given,
  7111. uses "<code>l</code>" as a default.
  7112. As an example, the construction
  7113. <pre>
  7114. for c in file:lines(1) do <em>body</em> end
  7115. </pre><p>
  7116. will iterate over all characters of the file,
  7117. starting at the current position.
  7118. Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
  7119. when the loop ends.
  7120. <p>
  7121. <hr><h3><a name="pdf-file:read"><code>file:read (&middot;&middot;&middot;)</code></a></h3>
  7122. <p>
  7123. Reads the file <code>file</code>,
  7124. according to the given formats, which specify what to read.
  7125. For each format,
  7126. the function returns a string or a number with the characters read,
  7127. or <b>fail</b> if it cannot read data with the specified format.
  7128. (In this latter case,
  7129. the function does not read subsequent formats.)
  7130. When called without arguments,
  7131. it uses a default format that reads the next line
  7132. (see below).
  7133. <p>
  7134. The available formats are
  7135. <ul>
  7136. <li><b>"<code>n</code>": </b>
  7137. reads a numeral and returns it as a float or an integer,
  7138. following the lexical conventions of Lua.
  7139. (The numeral may have leading whitespaces and a sign.)
  7140. This format always reads the longest input sequence that
  7141. is a valid prefix for a numeral;
  7142. if that prefix does not form a valid numeral
  7143. (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>")
  7144. or it is too long (more than 200 characters),
  7145. it is discarded and the format returns <b>fail</b>.
  7146. </li>
  7147. <li><b>"<code>a</code>": </b>
  7148. reads the whole file, starting at the current position.
  7149. On end of file, it returns the empty string;
  7150. this format never fails.
  7151. </li>
  7152. <li><b>"<code>l</code>": </b>
  7153. reads the next line skipping the end of line,
  7154. returning <b>fail</b> on end of file.
  7155. This is the default format.
  7156. </li>
  7157. <li><b>"<code>L</code>": </b>
  7158. reads the next line keeping the end-of-line character (if present),
  7159. returning <b>fail</b> on end of file.
  7160. </li>
  7161. <li><b><em>number</em>: </b>
  7162. reads a string with up to this number of bytes,
  7163. returning <b>fail</b> on end of file.
  7164. If <code>number</code> is zero,
  7165. it reads nothing and returns an empty string,
  7166. or <b>fail</b> on end of file.
  7167. </li>
  7168. </ul><p>
  7169. The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
  7170. <p>
  7171. <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3>
  7172. <p>
  7173. Sets and gets the file position,
  7174. measured from the beginning of the file,
  7175. to the position given by <code>offset</code> plus a base
  7176. specified by the string <code>whence</code>, as follows:
  7177. <ul>
  7178. <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li>
  7179. <li><b>"<code>cur</code>": </b> base is current position;</li>
  7180. <li><b>"<code>end</code>": </b> base is end of file;</li>
  7181. </ul><p>
  7182. In case of success, <code>seek</code> returns the final file position,
  7183. measured in bytes from the beginning of the file.
  7184. If <code>seek</code> fails, it returns <b>fail</b>,
  7185. plus a string describing the error.
  7186. <p>
  7187. The default value for <code>whence</code> is <code>"cur"</code>,
  7188. and for <code>offset</code> is 0.
  7189. Therefore, the call <code>file:seek()</code> returns the current
  7190. file position, without changing it;
  7191. the call <code>file:seek("set")</code> sets the position to the
  7192. beginning of the file (and returns 0);
  7193. and the call <code>file:seek("end")</code> sets the position to the
  7194. end of the file, and returns its size.
  7195. <p>
  7196. <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
  7197. <p>
  7198. Sets the buffering mode for a file.
  7199. There are three available modes:
  7200. <ul>
  7201. <li><b>"<code>no</code>": </b> no buffering.</li>
  7202. <li><b>"<code>full</code>": </b> full buffering.</li>
  7203. <li><b>"<code>line</code>": </b> line buffering.</li>
  7204. </ul>
  7205. <p>
  7206. For the last two cases,
  7207. <code>size</code> is a hint for the size of the buffer, in bytes.
  7208. The default is an appropriate size.
  7209. <p>
  7210. The specific behavior of each mode is non portable;
  7211. check the underlying ISO&nbsp;C function <code>setvbuf</code> in your platform for
  7212. more details.
  7213. <p>
  7214. <hr><h3><a name="pdf-file:write"><code>file:write (&middot;&middot;&middot;)</code></a></h3>
  7215. <p>
  7216. Writes the value of each of its arguments to <code>file</code>.
  7217. The arguments must be strings or numbers.
  7218. <p>
  7219. In case of success, this function returns <code>file</code>.
  7220. <h2>6.9 &ndash; <a name="6.9">Operating System Facilities</a></h2>
  7221. <p>
  7222. This library is implemented through table <a name="pdf-os"><code>os</code></a>.
  7223. <p>
  7224. <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
  7225. <p>
  7226. Returns an approximation of the amount in seconds of CPU time
  7227. used by the program,
  7228. as returned by the underlying ISO&nbsp;C function <code>clock</code>.
  7229. <p>
  7230. <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
  7231. <p>
  7232. Returns a string or a table containing date and time,
  7233. formatted according to the given string <code>format</code>.
  7234. <p>
  7235. If the <code>time</code> argument is present,
  7236. this is the time to be formatted
  7237. (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
  7238. Otherwise, <code>date</code> formats the current time.
  7239. <p>
  7240. If <code>format</code> starts with '<code>!</code>',
  7241. then the date is formatted in Coordinated Universal Time.
  7242. After this optional character,
  7243. if <code>format</code> is the string "<code>*t</code>",
  7244. then <code>date</code> returns a table with the following fields:
  7245. <code>year</code>, <code>month</code> (1&ndash;12), <code>day</code> (1&ndash;31),
  7246. <code>hour</code> (0&ndash;23), <code>min</code> (0&ndash;59),
  7247. <code>sec</code> (0&ndash;61, due to leap seconds),
  7248. <code>wday</code> (weekday, 1&ndash;7, Sunday is&nbsp;1),
  7249. <code>yday</code> (day of the year, 1&ndash;366),
  7250. and <code>isdst</code> (daylight saving flag, a boolean).
  7251. This last field may be absent
  7252. if the information is not available.
  7253. <p>
  7254. If <code>format</code> is not "<code>*t</code>",
  7255. then <code>date</code> returns the date as a string,
  7256. formatted according to the same rules as the ISO&nbsp;C function <code>strftime</code>.
  7257. <p>
  7258. If <code>format</code> is absent, it defaults to "<code>%c</code>",
  7259. which gives a human-readable date and time representation
  7260. using the current locale.
  7261. <p>
  7262. On non-POSIX systems,
  7263. this function may be not thread safe
  7264. because of its reliance on C&nbsp;function <code>gmtime</code> and C&nbsp;function <code>localtime</code>.
  7265. <p>
  7266. <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
  7267. <p>
  7268. Returns the difference, in seconds,
  7269. from time <code>t1</code> to time <code>t2</code>
  7270. (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>).
  7271. In POSIX, Windows, and some other systems,
  7272. this value is exactly <code>t2</code><em>-</em><code>t1</code>.
  7273. <p>
  7274. <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
  7275. <p>
  7276. This function is equivalent to the ISO&nbsp;C function <code>system</code>.
  7277. It passes <code>command</code> to be executed by an operating system shell.
  7278. Its first result is <b>true</b>
  7279. if the command terminated successfully,
  7280. or <b>fail</b> otherwise.
  7281. After this first result
  7282. the function returns a string plus a number,
  7283. as follows:
  7284. <ul>
  7285. <li><b>"<code>exit</code>": </b>
  7286. the command terminated normally;
  7287. the following number is the exit status of the command.
  7288. </li>
  7289. <li><b>"<code>signal</code>": </b>
  7290. the command was terminated by a signal;
  7291. the following number is the signal that terminated the command.
  7292. </li>
  7293. </ul>
  7294. <p>
  7295. When called without a <code>command</code>,
  7296. <code>os.execute</code> returns a boolean that is true if a shell is available.
  7297. <p>
  7298. <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
  7299. <p>
  7300. Calls the ISO&nbsp;C function <code>exit</code> to terminate the host program.
  7301. If <code>code</code> is <b>true</b>,
  7302. the returned status is <code>EXIT_SUCCESS</code>;
  7303. if <code>code</code> is <b>false</b>,
  7304. the returned status is <code>EXIT_FAILURE</code>;
  7305. if <code>code</code> is a number,
  7306. the returned status is this number.
  7307. The default value for <code>code</code> is <b>true</b>.
  7308. <p>
  7309. If the optional second argument <code>close</code> is true,
  7310. closes the Lua state before exiting.
  7311. <p>
  7312. <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
  7313. <p>
  7314. Returns the value of the process environment variable <code>varname</code>
  7315. or <b>fail</b> if the variable is not defined.
  7316. <p>
  7317. <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
  7318. <p>
  7319. Deletes the file (or empty directory, on POSIX systems)
  7320. with the given name.
  7321. If this function fails, it returns <b>fail</b>
  7322. plus a string describing the error and the error code.
  7323. Otherwise, it returns true.
  7324. <p>
  7325. <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
  7326. <p>
  7327. Renames the file or directory named <code>oldname</code> to <code>newname</code>.
  7328. If this function fails, it returns <b>fail</b>,
  7329. plus a string describing the error and the error code.
  7330. Otherwise, it returns true.
  7331. <p>
  7332. <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
  7333. <p>
  7334. Sets the current locale of the program.
  7335. <code>locale</code> is a system-dependent string specifying a locale;
  7336. <code>category</code> is an optional string describing which category to change:
  7337. <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
  7338. <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
  7339. the default category is <code>"all"</code>.
  7340. The function returns the name of the new locale,
  7341. or <b>fail</b> if the request cannot be honored.
  7342. <p>
  7343. If <code>locale</code> is the empty string,
  7344. the current locale is set to an implementation-defined native locale.
  7345. If <code>locale</code> is the string "<code>C</code>",
  7346. the current locale is set to the standard C locale.
  7347. <p>
  7348. When called with <b>nil</b> as the first argument,
  7349. this function only returns the name of the current locale
  7350. for the given category.
  7351. <p>
  7352. This function may be not thread safe
  7353. because of its reliance on C&nbsp;function <code>setlocale</code>.
  7354. <p>
  7355. <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
  7356. <p>
  7357. Returns the current time when called without arguments,
  7358. or a time representing the local date and time specified by the given table.
  7359. This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
  7360. and may have fields
  7361. <code>hour</code> (default is 12),
  7362. <code>min</code> (default is 0),
  7363. <code>sec</code> (default is 0),
  7364. and <code>isdst</code> (default is <b>nil</b>).
  7365. Other fields are ignored.
  7366. For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
  7367. <p>
  7368. When the function is called,
  7369. the values in these fields do not need to be inside their valid ranges.
  7370. For instance, if <code>sec</code> is -10,
  7371. it means 10 seconds before the time specified by the other fields;
  7372. if <code>hour</code> is 1000,
  7373. it means 1000 hours after the time specified by the other fields.
  7374. <p>
  7375. The returned value is a number, whose meaning depends on your system.
  7376. In POSIX, Windows, and some other systems,
  7377. this number counts the number
  7378. of seconds since some given start time (the "epoch").
  7379. In other systems, the meaning is not specified,
  7380. and the number returned by <code>time</code> can be used only as an argument to
  7381. <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>.
  7382. <p>
  7383. When called with a table,
  7384. <code>os.time</code> also normalizes all the fields
  7385. documented in the <a href="#pdf-os.date"><code>os.date</code></a> function,
  7386. so that they represent the same time as before the call
  7387. but with values inside their valid ranges.
  7388. <p>
  7389. <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
  7390. <p>
  7391. Returns a string with a file name that can
  7392. be used for a temporary file.
  7393. The file must be explicitly opened before its use
  7394. and explicitly removed when no longer needed.
  7395. <p>
  7396. In POSIX systems,
  7397. this function also creates a file with that name,
  7398. to avoid security risks.
  7399. (Someone else might create the file with wrong permissions
  7400. in the time between getting the name and creating the file.)
  7401. You still have to open the file to use it
  7402. and to remove it (even if you do not use it).
  7403. <p>
  7404. When possible,
  7405. you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
  7406. which automatically removes the file when the program ends.
  7407. <h2>6.10 &ndash; <a name="6.10">The Debug Library</a></h2>
  7408. <p>
  7409. This library provides
  7410. the functionality of the debug interface (<a href="#4.7">&sect;4.7</a>) to Lua programs.
  7411. You should exert care when using this library.
  7412. Several of its functions
  7413. violate basic assumptions about Lua code
  7414. (e.g., that variables local to a function
  7415. cannot be accessed from outside;
  7416. that userdata metatables cannot be changed by Lua code;
  7417. that Lua programs do not crash)
  7418. and therefore can compromise otherwise secure code.
  7419. Moreover, some functions in this library may be slow.
  7420. <p>
  7421. All functions in this library are provided
  7422. inside the <a name="pdf-debug"><code>debug</code></a> table.
  7423. All functions that operate over a thread
  7424. have an optional first argument which is the
  7425. thread to operate over.
  7426. The default is always the current thread.
  7427. <p>
  7428. <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
  7429. <p>
  7430. Enters an interactive mode with the user,
  7431. running each string that the user enters.
  7432. Using simple commands and other debug facilities,
  7433. the user can inspect global and local variables,
  7434. change their values, evaluate expressions, and so on.
  7435. A line containing only the word <code>cont</code> finishes this function,
  7436. so that the caller continues its execution.
  7437. <p>
  7438. Note that commands for <code>debug.debug</code> are not lexically nested
  7439. within any function and so have no direct access to local variables.
  7440. <p>
  7441. <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
  7442. <p>
  7443. Returns the current hook settings of the thread, as three values:
  7444. the current hook function, the current hook mask,
  7445. and the current hook count,
  7446. as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function.
  7447. <p>
  7448. Returns <b>fail</b> if there is no active hook.
  7449. <p>
  7450. <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
  7451. <p>
  7452. Returns a table with information about a function.
  7453. You can give the function directly
  7454. or you can give a number as the value of <code>f</code>,
  7455. which means the function running at level <code>f</code> of the call stack
  7456. of the given thread:
  7457. level&nbsp;0 is the current function (<code>getinfo</code> itself);
  7458. level&nbsp;1 is the function that called <code>getinfo</code>
  7459. (except for tail calls, which do not count on the stack);
  7460. and so on.
  7461. If <code>f</code> is a number greater than the number of active functions,
  7462. then <code>getinfo</code> returns <b>fail</b>.
  7463. <p>
  7464. The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>,
  7465. with the string <code>what</code> describing which fields to fill in.
  7466. The default for <code>what</code> is to get all information available,
  7467. except the table of valid lines.
  7468. If present,
  7469. the option '<code>f</code>'
  7470. adds a field named <code>func</code> with the function itself.
  7471. If present,
  7472. the option '<code>L</code>'
  7473. adds a field named <code>activelines</code> with the table of
  7474. valid lines.
  7475. <p>
  7476. For instance, the expression <code>debug.getinfo(1,"n").name</code> returns
  7477. a name for the current function,
  7478. if a reasonable name can be found,
  7479. and the expression <code>debug.getinfo(print)</code>
  7480. returns a table with all available information
  7481. about the <a href="#pdf-print"><code>print</code></a> function.
  7482. <p>
  7483. <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3>
  7484. <p>
  7485. This function returns the name and the value of the local variable
  7486. with index <code>local</code> of the function at level <code>f</code> of the stack.
  7487. This function accesses not only explicit local variables,
  7488. but also parameters and temporary values.
  7489. <p>
  7490. The first parameter or local variable has index&nbsp;1, and so on,
  7491. following the order that they are declared in the code,
  7492. counting only the variables that are active
  7493. in the current scope of the function.
  7494. Compile-time constants may not appear in this listing,
  7495. if they were optimized away by the compiler.
  7496. Negative indices refer to vararg arguments;
  7497. -1 is the first vararg argument.
  7498. The function returns <b>fail</b>
  7499. if there is no variable with the given index,
  7500. and raises an error when called with a level out of range.
  7501. (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
  7502. <p>
  7503. Variable names starting with '<code>(</code>' (open parenthesis)
  7504. represent variables with no known names
  7505. (internal variables such as loop control variables,
  7506. and variables from chunks saved without debug information).
  7507. <p>
  7508. The parameter <code>f</code> may also be a function.
  7509. In that case, <code>getlocal</code> returns only the name of function parameters.
  7510. <p>
  7511. <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3>
  7512. <p>
  7513. Returns the metatable of the given <code>value</code>
  7514. or <b>nil</b> if it does not have a metatable.
  7515. <p>
  7516. <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
  7517. <p>
  7518. Returns the registry table (see <a href="#4.3">&sect;4.3</a>).
  7519. <p>
  7520. <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3>
  7521. <p>
  7522. This function returns the name and the value of the upvalue
  7523. with index <code>up</code> of the function <code>f</code>.
  7524. The function returns <b>fail</b>
  7525. if there is no upvalue with the given index.
  7526. <p>
  7527. (For Lua functions,
  7528. upvalues are the external local variables that the function uses,
  7529. and that are consequently included in its closure.)
  7530. <p>
  7531. For C&nbsp;functions, this function uses the empty string <code>""</code>
  7532. as a name for all upvalues.
  7533. <p>
  7534. Variable name '<code>?</code>' (interrogation mark)
  7535. represents variables with no known names
  7536. (variables from chunks saved without debug information).
  7537. <p>
  7538. <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u, n)</code></a></h3>
  7539. <p>
  7540. Returns the <code>n</code>-th user value associated
  7541. to the userdata <code>u</code> plus a boolean,
  7542. <b>false</b> if the userdata does not have that value.
  7543. <p>
  7544. <hr><h3><a name="pdf-debug.setcstacklimit"><code>debug.setcstacklimit (limit)</code></a></h3>
  7545. <p>
  7546. Sets a new limit for the C stack.
  7547. This limit controls how deeply nested calls can go in Lua,
  7548. with the intent of avoiding a stack overflow.
  7549. A limit too small restricts recursive calls pointlessly;
  7550. a limit too large exposes the interpreter to stack-overflow crashes.
  7551. Unfortunately, there is no way to know a priori
  7552. the maximum safe limit for a platform.
  7553. <p>
  7554. Each call made from Lua code counts one unit.
  7555. Other operations (e.g., calls made from C to Lua or resuming a coroutine)
  7556. may have a higher cost.
  7557. <p>
  7558. This function has the following restrictions:
  7559. <ul>
  7560. <li>It can only be called from the main coroutine (thread);</li>
  7561. <li>It cannot be called while handling a stack-overflow error;</li>
  7562. <li><code>limit</code> must be less than 40000;</li>
  7563. <li><code>limit</code> cannot be less than the amount of C stack in use.</li>
  7564. </ul><p>
  7565. If a call does not respect some restriction,
  7566. it returns a false value.
  7567. Otherwise,
  7568. the call returns the old limit.
  7569. <p>
  7570. <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
  7571. <p>
  7572. Sets the given function as the debug hook.
  7573. The string <code>mask</code> and the number <code>count</code> describe
  7574. when the hook will be called.
  7575. The string mask may have any combination of the following characters,
  7576. with the given meaning:
  7577. <ul>
  7578. <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li>
  7579. <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li>
  7580. <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li>
  7581. </ul><p>
  7582. Moreover,
  7583. with a <code>count</code> different from zero,
  7584. the hook is called also after every <code>count</code> instructions.
  7585. <p>
  7586. When called without arguments,
  7587. <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
  7588. <p>
  7589. When the hook is called, its first parameter is a string
  7590. describing the event that has triggered its call:
  7591. <code>"call"</code>, <code>"tail call"</code>, <code>"return"</code>,
  7592. <code>"line"</code>, and <code>"count"</code>.
  7593. For line events,
  7594. the hook also gets the new line number as its second parameter.
  7595. Inside a hook,
  7596. you can call <code>getinfo</code> with level&nbsp;2 to get more information about
  7597. the running function.
  7598. (Level&nbsp;0 is the <code>getinfo</code> function,
  7599. and level&nbsp;1 is the hook function.)
  7600. <p>
  7601. <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>
  7602. <p>
  7603. This function assigns the value <code>value</code> to the local variable
  7604. with index <code>local</code> of the function at level <code>level</code> of the stack.
  7605. The function returns <b>fail</b> if there is no local
  7606. variable with the given index,
  7607. and raises an error when called with a <code>level</code> out of range.
  7608. (You can call <code>getinfo</code> to check whether the level is valid.)
  7609. Otherwise, it returns the name of the local variable.
  7610. <p>
  7611. See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
  7612. variable indices and names.
  7613. <p>
  7614. <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
  7615. <p>
  7616. Sets the metatable for the given <code>value</code> to the given <code>table</code>
  7617. (which can be <b>nil</b>).
  7618. Returns <code>value</code>.
  7619. <p>
  7620. <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
  7621. <p>
  7622. This function assigns the value <code>value</code> to the upvalue
  7623. with index <code>up</code> of the function <code>f</code>.
  7624. The function returns <b>fail</b> if there is no upvalue
  7625. with the given index.
  7626. Otherwise, it returns the name of the upvalue.
  7627. <p>
  7628. See <a href="#pdf-debug.getupvalue"><code>debug.getupvalue</code></a> for more information about upvalues.
  7629. <p>
  7630. <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value, n)</code></a></h3>
  7631. <p>
  7632. Sets the given <code>value</code> as
  7633. the <code>n</code>-th user value associated to the given <code>udata</code>.
  7634. <code>udata</code> must be a full userdata.
  7635. <p>
  7636. Returns <code>udata</code>,
  7637. or <b>fail</b> if the userdata does not have that value.
  7638. <p>
  7639. <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
  7640. <p>
  7641. If <code>message</code> is present but is neither a string nor <b>nil</b>,
  7642. this function returns <code>message</code> without further processing.
  7643. Otherwise,
  7644. it returns a string with a traceback of the call stack.
  7645. The optional <code>message</code> string is appended
  7646. at the beginning of the traceback.
  7647. An optional <code>level</code> number tells at which level
  7648. to start the traceback
  7649. (default is 1, the function calling <code>traceback</code>).
  7650. <p>
  7651. <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3>
  7652. <p>
  7653. Returns a unique identifier (as a light userdata)
  7654. for the upvalue numbered <code>n</code>
  7655. from the given function.
  7656. <p>
  7657. These unique identifiers allow a program to check whether different
  7658. closures share upvalues.
  7659. Lua closures that share an upvalue
  7660. (that is, that access a same external local variable)
  7661. will return identical ids for those upvalue indices.
  7662. <p>
  7663. <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
  7664. <p>
  7665. Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
  7666. refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
  7667. <h1>7 &ndash; <a name="7">Lua Standalone</a></h1>
  7668. <p>
  7669. Although Lua has been designed as an extension language,
  7670. to be embedded in a host C&nbsp;program,
  7671. it is also frequently used as a standalone language.
  7672. An interpreter for Lua as a standalone language,
  7673. called simply <code>lua</code>,
  7674. is provided with the standard distribution.
  7675. The standalone interpreter includes
  7676. all standard libraries.
  7677. Its usage is:
  7678. <pre>
  7679. lua [options] [script [args]]
  7680. </pre><p>
  7681. The options are:
  7682. <ul>
  7683. <li><b><code>-e <em>stat</em></code>: </b> execute string <em>stat</em>;</li>
  7684. <li><b><code>-i</code>: </b> enter interactive mode after running <em>script</em>;</li>
  7685. <li><b><code>-l <em>mod</em></code>: </b> "require" <em>mod</em> and assign the
  7686. result to global <em>mod</em>;</li>
  7687. <li><b><code>-v</code>: </b> print version information;</li>
  7688. <li><b><code>-E</code>: </b> ignore environment variables;</li>
  7689. <li><b><code>-W</code>: </b> turn warnings on;</li>
  7690. <li><b><code>--</code>: </b> stop handling options;</li>
  7691. <li><b><code>-</code>: </b> execute <code>stdin</code> as a file and stop handling options.</li>
  7692. </ul><p>
  7693. After handling its options, <code>lua</code> runs the given <em>script</em>.
  7694. When called without arguments,
  7695. <code>lua</code> behaves as <code>lua -v -i</code>
  7696. when the standard input (<code>stdin</code>) is a terminal,
  7697. and as <code>lua -</code> otherwise.
  7698. <p>
  7699. When called without the option <code>-E</code>,
  7700. the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_4"><code>LUA_INIT_5_4</code></a>
  7701. (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
  7702. before running any argument.
  7703. If the variable content has the format <code>@<em>filename</em></code>,
  7704. then <code>lua</code> executes the file.
  7705. Otherwise, <code>lua</code> executes the string itself.
  7706. <p>
  7707. When called with the option <code>-E</code>,
  7708. Lua does not consult any environment variables.
  7709. In particular,
  7710. the values of <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a>
  7711. are set with the default paths defined in <code>luaconf.h</code>.
  7712. <p>
  7713. The options <code>-e</code>, <code>-l</code>, and <code>-W</code> are handled in
  7714. the order they appear.
  7715. For instance, an invocation like
  7716. <pre>
  7717. $ lua -e 'a=1' -llib1 script.lua
  7718. </pre><p>
  7719. will first set <code>a</code> to 1, then require the library <code>lib1</code>,
  7720. and finally run the file <code>script.lua</code> with no arguments.
  7721. (Here <code>$</code> is the shell prompt. Your prompt may be different.)
  7722. <p>
  7723. Before running any code,
  7724. <code>lua</code> collects all command-line arguments
  7725. in a global table called <code>arg</code>.
  7726. The script name goes to index 0,
  7727. the first argument after the script name goes to index 1,
  7728. and so on.
  7729. Any arguments before the script name
  7730. (that is, the interpreter name plus its options)
  7731. go to negative indices.
  7732. For instance, in the call
  7733. <pre>
  7734. $ lua -la b.lua t1 t2
  7735. </pre><p>
  7736. the table is like this:
  7737. <pre>
  7738. arg = { [-2] = "lua", [-1] = "-la",
  7739. [0] = "b.lua",
  7740. [1] = "t1", [2] = "t2" }
  7741. </pre><p>
  7742. If there is no script in the call,
  7743. the interpreter name goes to index 0,
  7744. followed by the other arguments.
  7745. For instance, the call
  7746. <pre>
  7747. $ lua -e "print(arg[1])"
  7748. </pre><p>
  7749. will print "<code>-e</code>".
  7750. If there is a script,
  7751. the script is called with arguments
  7752. <code>arg[1]</code>, &middot;&middot;&middot;, <code>arg[#arg]</code>.
  7753. Like all chunks in Lua,
  7754. the script is compiled as a vararg function.
  7755. <p>
  7756. In interactive mode,
  7757. Lua repeatedly prompts and waits for a line.
  7758. After reading a line,
  7759. Lua first try to interpret the line as an expression.
  7760. If it succeeds, it prints its value.
  7761. Otherwise, it interprets the line as a statement.
  7762. If you write an incomplete statement,
  7763. the interpreter waits for its completion
  7764. by issuing a different prompt.
  7765. <p>
  7766. If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string,
  7767. then its value is used as the prompt.
  7768. Similarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a string,
  7769. its value is used as the secondary prompt
  7770. (issued during incomplete statements).
  7771. <p>
  7772. In case of unprotected errors in the script,
  7773. the interpreter reports the error to the standard error stream.
  7774. If the error object is not a string but
  7775. has a metamethod <code>__tostring</code>,
  7776. the interpreter calls this metamethod to produce the final message.
  7777. Otherwise, the interpreter converts the error object to a string
  7778. and adds a stack traceback to it.
  7779. When warnings are on,
  7780. they are simply printed in the standard error output.
  7781. <p>
  7782. When finishing normally,
  7783. the interpreter closes its main Lua state
  7784. (see <a href="#lua_close"><code>lua_close</code></a>).
  7785. The script can avoid this step by
  7786. calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
  7787. <p>
  7788. To allow the use of Lua as a
  7789. script interpreter in Unix systems,
  7790. Lua skips the first line of a file chunk if it starts with <code>#</code>.
  7791. Therefore, Lua scripts can be made into executable programs
  7792. by using <code>chmod +x</code> and the&nbsp;<code>#!</code> form,
  7793. as in
  7794. <pre>
  7795. #!/usr/local/bin/lua
  7796. </pre><p>
  7797. Of course,
  7798. the location of the Lua interpreter may be different in your machine.
  7799. If <code>lua</code> is in your <code>PATH</code>,
  7800. then
  7801. <pre>
  7802. #!/usr/bin/env lua
  7803. </pre><p>
  7804. is a more portable solution.
  7805. <h1>8 &ndash; <a name="8">Incompatibilities with the Previous Version</a></h1>
  7806. <p>
  7807. Here we list the incompatibilities that you may find when moving a program
  7808. from Lua&nbsp;5.3 to Lua&nbsp;5.4.
  7809. <p>
  7810. You can avoid some incompatibilities by compiling Lua with
  7811. appropriate options (see file <code>luaconf.h</code>).
  7812. However,
  7813. all these compatibility options will be removed in the future.
  7814. More often than not,
  7815. compatibility issues arise when these compatibility options
  7816. are removed.
  7817. So, whenever you have the chance,
  7818. you should try to test your code with a version of Lua compiled
  7819. with all compatibility options turned off.
  7820. That will ease transitions to newer versions of Lua.
  7821. <p>
  7822. Lua versions can always change the C API in ways that
  7823. do not imply source-code changes in a program,
  7824. such as the numeric values for constants
  7825. or the implementation of functions as macros.
  7826. Therefore,
  7827. you should never assume that binaries are compatible between
  7828. different Lua versions.
  7829. Always recompile clients of the Lua API when
  7830. using a new version.
  7831. <p>
  7832. Similarly, Lua versions can always change the internal representation
  7833. of precompiled chunks;
  7834. precompiled chunks are not compatible between different Lua versions.
  7835. <p>
  7836. The standard paths in the official distribution may
  7837. change between versions.
  7838. <h2>8.1 &ndash; <a name="8.1">Incompatibilities in the Language</a></h2>
  7839. <ul>
  7840. <li>
  7841. The coercion of strings to numbers in
  7842. arithmetic and bitwise operations
  7843. has been removed from the core language.
  7844. The string library does a similar job
  7845. for arithmetic (but not for bitwise) operations
  7846. using the string metamethods.
  7847. However, unlike in previous versions,
  7848. the new implementation preserves the implicit type of the numeral
  7849. in the string.
  7850. For instance, the result of <code>"1" + "2"</code> now is an integer,
  7851. not a float.
  7852. </li>
  7853. <li>
  7854. Literal decimal integer constants that overflow are read as floats,
  7855. instead of wrapping around.
  7856. You can use hexadecimal notation for such constants if you
  7857. want the old behavior
  7858. (reading them as integers with wrap around).
  7859. </li>
  7860. <li>
  7861. The use of the <code>__lt</code> metamethod to emulate <code>__le</code>
  7862. has been removed.
  7863. When needed, this metamethod must be explicitly defined.
  7864. </li>
  7865. <li>
  7866. The semantics of the numerical <b>for</b> loop
  7867. over integers changed in some details.
  7868. In particular, the control variable never wraps around.
  7869. </li>
  7870. <li>
  7871. A label for a <b>goto</b> cannot be declared where a label with the same
  7872. name is visible, even if this other label is declared in an enclosing
  7873. block.
  7874. </li>
  7875. <li>
  7876. When finalizing an object,
  7877. Lua does not ignore <code>__gc</code> metamethods that are not functions.
  7878. Any value will be called, if present.
  7879. (Non-callable values will generate a warning,
  7880. like any other error when calling a finalizer.)
  7881. </li>
  7882. </ul>
  7883. <h2>8.2 &ndash; <a name="8.2">Incompatibilities in the Libraries</a></h2>
  7884. <ul>
  7885. <li>
  7886. The function <a href="#pdf-print"><code>print</code></a> does not call <a href="#pdf-tostring"><code>tostring</code></a>
  7887. to format its arguments;
  7888. instead, it has this functionality hardwired.
  7889. You should use <code>__tostring</code> to modify how values are printed.
  7890. </li>
  7891. <li>
  7892. The pseudo-random number generator used by the function <a href="#pdf-math.random"><code>math.random</code></a>
  7893. now starts with a somewhat random seed.
  7894. Moreover, it uses a different algorithm.
  7895. </li>
  7896. <li>
  7897. By default, the decoding functions in the <a href="#pdf-utf8"><code>utf8</code></a> library
  7898. do not accept surrogates as valid code points.
  7899. An extra parameter in these functions makes them more permissive.
  7900. </li>
  7901. <li>
  7902. The options "<code>setpause</code>" and "<code>setstepmul</code>"
  7903. of the function <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> are deprecated.
  7904. You should use the new option "<code>incremental</code>" to set them.
  7905. </li>
  7906. <li>
  7907. The function <a href="#pdf-io.lines"><code>io.lines</code></a> now returns four values,
  7908. instead of just one.
  7909. That can be a problem when it is used as the sole
  7910. argument to another function that has optional parameters,
  7911. such as in <code>load(io.lines(filename, "L"))</code>.
  7912. To fix that issue,
  7913. you can wrap the call into parentheses,
  7914. to adjust its number of results to one.
  7915. </li>
  7916. </ul>
  7917. <h2>8.3 &ndash; <a name="8.3">Incompatibilities in the API</a></h2>
  7918. <ul>
  7919. <li>
  7920. Full userdata now has an arbitrary number of associated user values.
  7921. Therefore, the functions <code>lua_newuserdata</code>,
  7922. <code>lua_setuservalue</code>, and <code>lua_getuservalue</code> were
  7923. replaced by <a href="#lua_newuserdatauv"><code>lua_newuserdatauv</code></a>,
  7924. <a href="#lua_setiuservalue"><code>lua_setiuservalue</code></a>, and <a href="#lua_getiuservalue"><code>lua_getiuservalue</code></a>,
  7925. which have an extra argument.
  7926. <p>
  7927. For compatibility, the old names still work as macros assuming
  7928. one single user value.
  7929. Note, however, that userdata with zero user values
  7930. are more efficient memory-wise.
  7931. </li>
  7932. <li>
  7933. The function <a href="#lua_resume"><code>lua_resume</code></a> has an extra parameter.
  7934. This out parameter returns the number of values on
  7935. the top of the stack that were yielded or returned by the coroutine.
  7936. (In previous versions,
  7937. those values were the entire stack.)
  7938. </li>
  7939. <li>
  7940. The function <a href="#lua_version"><code>lua_version</code></a> returns the version number,
  7941. instead of an address of the version number.
  7942. The Lua core should work correctly with libraries using their
  7943. own static copies of the same core,
  7944. so there is no need to check whether they are using the same
  7945. address space.
  7946. </li>
  7947. <li>
  7948. The constant <code>LUA_ERRGCMM</code> was removed.
  7949. Errors in finalizers are never propagated;
  7950. instead, they generate a warning.
  7951. </li>
  7952. <li>
  7953. The options <code>LUA_GCSETPAUSE</code> and <code>LUA_GCSETSTEPMUL</code>
  7954. of the function <a href="#lua_gc"><code>lua_gc</code></a> are deprecated.
  7955. You should use the new option <code>LUA_GCINC</code> to set them.
  7956. </li>
  7957. </ul>
  7958. <h1>9 &ndash; <a name="9">The Complete Syntax of Lua</a></h1>
  7959. <p>
  7960. Here is the complete syntax of Lua in extended BNF.
  7961. As usual in extended BNF,
  7962. {A} means 0 or more As,
  7963. and [A] means an optional A.
  7964. (For operator precedences, see <a href="#3.4.8">&sect;3.4.8</a>;
  7965. for a description of the terminals
  7966. Name, Numeral,
  7967. and LiteralString, see <a href="#3.1">&sect;3.1</a>.)
  7968. <pre>
  7969. chunk ::= block
  7970. block ::= {stat} [retstat]
  7971. stat ::= &lsquo;<b>;</b>&rsquo; |
  7972. varlist &lsquo;<b>=</b>&rsquo; explist |
  7973. functioncall |
  7974. label |
  7975. <b>break</b> |
  7976. <b>goto</b> Name |
  7977. <b>do</b> block <b>end</b> |
  7978. <b>while</b> exp <b>do</b> block <b>end</b> |
  7979. <b>repeat</b> block <b>until</b> exp |
  7980. <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
  7981. <b>for</b> Name &lsquo;<b>=</b>&rsquo; exp &lsquo;<b>,</b>&rsquo; exp [&lsquo;<b>,</b>&rsquo; exp] <b>do</b> block <b>end</b> |
  7982. <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> |
  7983. <b>function</b> funcname funcbody |
  7984. <b>local</b> <b>function</b> Name funcbody |
  7985. <b>local</b> attnamelist [&lsquo;<b>=</b>&rsquo; explist]
  7986. attnamelist ::= Name attrib {&lsquo;<b>,</b>&rsquo; Name attrib}
  7987. attrib ::= [&lsquo;<b>&lt;</b>&rsquo; Name &lsquo;<b>&gt;</b>&rsquo;]
  7988. retstat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
  7989. label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
  7990. funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
  7991. varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
  7992. var ::= Name | prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; | prefixexp &lsquo;<b>.</b>&rsquo; Name
  7993. namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
  7994. explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
  7995. exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | &lsquo;<b>...</b>&rsquo; | functiondef |
  7996. prefixexp | tableconstructor | exp binop exp | unop exp
  7997. prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
  7998. functioncall ::= prefixexp args | prefixexp &lsquo;<b>:</b>&rsquo; Name args
  7999. args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo; | tableconstructor | LiteralString
  8000. functiondef ::= <b>function</b> funcbody
  8001. funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
  8002. parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
  8003. tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
  8004. fieldlist ::= field {fieldsep field} [fieldsep]
  8005. field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
  8006. fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
  8007. binop ::= &lsquo;<b>+</b>&rsquo; | &lsquo;<b>-</b>&rsquo; | &lsquo;<b>*</b>&rsquo; | &lsquo;<b>/</b>&rsquo; | &lsquo;<b>//</b>&rsquo; | &lsquo;<b>^</b>&rsquo; | &lsquo;<b>%</b>&rsquo; |
  8008. &lsquo;<b>&amp;</b>&rsquo; | &lsquo;<b>~</b>&rsquo; | &lsquo;<b>|</b>&rsquo; | &lsquo;<b>&gt;&gt;</b>&rsquo; | &lsquo;<b>&lt;&lt;</b>&rsquo; | &lsquo;<b>..</b>&rsquo; |
  8009. &lsquo;<b>&lt;</b>&rsquo; | &lsquo;<b>&lt;=</b>&rsquo; | &lsquo;<b>&gt;</b>&rsquo; | &lsquo;<b>&gt;=</b>&rsquo; | &lsquo;<b>==</b>&rsquo; | &lsquo;<b>~=</b>&rsquo; |
  8010. <b>and</b> | <b>or</b>
  8011. unop ::= &lsquo;<b>-</b>&rsquo; | <b>not</b> | &lsquo;<b>#</b>&rsquo; | &lsquo;<b>~</b>&rsquo;
  8012. </pre>
  8013. <p>
  8014. <P CLASS="footer">
  8015. Last update:
  8016. Thu Jun 18 16:10:16 UTC 2020
  8017. </P>
  8018. <!--
  8019. Last change: revised for Lua 5.4.0 (final)
  8020. -->
  8021. </body></html>