manual.html 320 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Lua 5.3 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.3 Reference Manual
  13. </H1>
  14. <P>
  15. by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
  16. <P>
  17. <SMALL>
  18. Copyright &copy; 2015&ndash;2018 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,v 1.167.1.2 2018/06/26 15:49:07 roberto Exp $ -->
  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. incremental 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 <em>dynamically typed language</em>.
  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 <em>first-class values</em>.
  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 usually 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. any other value makes it true.
  107. The type <em>number</em> represents both
  108. integer numbers and real (floating-point) numbers.
  109. The type <em>string</em> represents immutable sequences of bytes.
  110. Lua is 8-bit clean:
  111. strings can contain any 8-bit value,
  112. including embedded zeros ('<code>\0</code>').
  113. Lua is also encoding-agnostic;
  114. it makes no assumptions about the contents of a string.
  115. <p>
  116. The type <em>number</em> uses two internal representations,
  117. or two subtypes,
  118. one called <em>integer</em> and the other called <em>float</em>.
  119. Lua has explicit rules about when each representation is used,
  120. but it also converts between them automatically as needed (see <a href="#3.4.3">&sect;3.4.3</a>).
  121. Therefore,
  122. the programmer may choose to mostly ignore the difference
  123. between integers and floats
  124. or to assume complete control over the representation of each number.
  125. Standard Lua uses 64-bit integers and double-precision (64-bit) floats,
  126. but you can also compile Lua so that it
  127. uses 32-bit integers and/or single-precision (32-bit) floats.
  128. The option with 32 bits for both integers and floats
  129. is particularly attractive
  130. for small machines and embedded systems.
  131. (See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.)
  132. <p>
  133. Lua can call (and manipulate) functions written in Lua and
  134. functions written in C (see <a href="#3.4.10">&sect;3.4.10</a>).
  135. Both are represented by the type <em>function</em>.
  136. <p>
  137. The type <em>userdata</em> is provided to allow arbitrary C&nbsp;data to
  138. be stored in Lua variables.
  139. A userdata value represents a block of raw memory.
  140. There are two kinds of userdata:
  141. <em>full userdata</em>,
  142. which is an object with a block of memory managed by Lua,
  143. and <em>light userdata</em>,
  144. which is simply a C&nbsp;pointer value.
  145. Userdata has no predefined operations in Lua,
  146. except assignment and identity test.
  147. By using <em>metatables</em>,
  148. the programmer can define operations for full userdata values
  149. (see <a href="#2.4">&sect;2.4</a>).
  150. Userdata values cannot be created or modified in Lua,
  151. only through the C&nbsp;API.
  152. This guarantees the integrity of data owned by the host program.
  153. <p>
  154. The type <em>thread</em> represents independent threads of execution
  155. and it is used to implement coroutines (see <a href="#2.6">&sect;2.6</a>).
  156. Lua threads are not related to operating-system threads.
  157. Lua supports coroutines on all systems,
  158. even those that do not support threads natively.
  159. <p>
  160. The type <em>table</em> implements associative arrays,
  161. that is, arrays that can have as indices not only numbers,
  162. but any Lua value except <b>nil</b> and NaN.
  163. (<em>Not a Number</em> is a special value used to represent
  164. undefined or unrepresentable numerical results, such as <code>0/0</code>.)
  165. Tables can be <em>heterogeneous</em>;
  166. that is, they can contain values of all types (except <b>nil</b>).
  167. Any key with value <b>nil</b> is not considered part of the table.
  168. Conversely, any key that is not part of a table has
  169. an associated value <b>nil</b>.
  170. <p>
  171. Tables are the sole data-structuring mechanism in Lua;
  172. they can be used to represent ordinary arrays, lists,
  173. symbol tables, sets, records, graphs, trees, etc.
  174. To represent records, Lua uses the field name as an index.
  175. The language supports this representation by
  176. providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
  177. There are several convenient ways to create tables in Lua
  178. (see <a href="#3.4.9">&sect;3.4.9</a>).
  179. <p>
  180. Like indices,
  181. the values of table fields can be of any type.
  182. In particular,
  183. because functions are first-class values,
  184. table fields can contain functions.
  185. Thus tables can also carry <em>methods</em> (see <a href="#3.4.11">&sect;3.4.11</a>).
  186. <p>
  187. The indexing of tables follows
  188. the definition of raw equality in the language.
  189. The expressions <code>a[i]</code> and <code>a[j]</code>
  190. denote the same table element
  191. if and only if <code>i</code> and <code>j</code> are raw equal
  192. (that is, equal without metamethods).
  193. In particular, floats with integral values
  194. are equal to their respective integers
  195. (e.g., <code>1.0 == 1</code>).
  196. To avoid ambiguities,
  197. any float with integral value used as a key
  198. is converted to its respective integer.
  199. For instance, if you write <code>a[2.0] = true</code>,
  200. the actual key inserted into the table will be the
  201. integer <code>2</code>.
  202. (On the other hand,
  203. 2 and "<code>2</code>" are different Lua values and therefore
  204. denote different table entries.)
  205. <p>
  206. Tables, functions, threads, and (full) userdata values are <em>objects</em>:
  207. variables do not actually <em>contain</em> these values,
  208. only <em>references</em> to them.
  209. Assignment, parameter passing, and function returns
  210. always manipulate references to such values;
  211. these operations do not imply any kind of copy.
  212. <p>
  213. The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type
  214. of a given value (see <a href="#6.1">&sect;6.1</a>).
  215. <h2>2.2 &ndash; <a name="2.2">Environments and the Global Environment</a></h2>
  216. <p>
  217. As will be discussed in <a href="#3.2">&sect;3.2</a> and <a href="#3.3.3">&sect;3.3.3</a>,
  218. any reference to a free name
  219. (that is, a name not bound to any declaration) <code>var</code>
  220. is syntactically translated to <code>_ENV.var</code>.
  221. Moreover, every chunk is compiled in the scope of
  222. an external local variable named <code>_ENV</code> (see <a href="#3.3.2">&sect;3.3.2</a>),
  223. so <code>_ENV</code> itself is never a free name in a chunk.
  224. <p>
  225. Despite the existence of this external <code>_ENV</code> variable and
  226. the translation of free names,
  227. <code>_ENV</code> is a completely regular name.
  228. In particular,
  229. you can define new variables and parameters with that name.
  230. Each reference to a free name uses the <code>_ENV</code> that is
  231. visible at that point in the program,
  232. following the usual visibility rules of Lua (see <a href="#3.5">&sect;3.5</a>).
  233. <p>
  234. Any table used as the value of <code>_ENV</code> is called an <em>environment</em>.
  235. <p>
  236. Lua keeps a distinguished environment called the <em>global environment</em>.
  237. This value is kept at a special index in the C registry (see <a href="#4.5">&sect;4.5</a>).
  238. In Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value.
  239. (<a href="#pdf-_G"><code>_G</code></a> is never used internally.)
  240. <p>
  241. When Lua loads a chunk,
  242. the default value for its <code>_ENV</code> upvalue
  243. is the global environment (see <a href="#pdf-load"><code>load</code></a>).
  244. Therefore, by default,
  245. free names in Lua code refer to entries in the global environment
  246. (and, therefore, they are also called <em>global variables</em>).
  247. Moreover, all standard libraries are loaded in the global environment
  248. and some functions there operate on that environment.
  249. You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>)
  250. to load a chunk with a different environment.
  251. (In C, you have to load the chunk and then change the value
  252. of its first upvalue.)
  253. <h2>2.3 &ndash; <a name="2.3">Error Handling</a></h2>
  254. <p>
  255. Because Lua is an embedded extension language,
  256. all Lua actions start from C&nbsp;code in the host program
  257. calling a function from the Lua library.
  258. (When you use Lua standalone,
  259. the <code>lua</code> application is the host program.)
  260. Whenever an error occurs during
  261. the compilation or execution of a Lua chunk,
  262. control returns to the host,
  263. which can take appropriate measures
  264. (such as printing an error message).
  265. <p>
  266. Lua code can explicitly generate an error by calling the
  267. <a href="#pdf-error"><code>error</code></a> function.
  268. If you need to catch errors in Lua,
  269. you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a>
  270. to call a given function in <em>protected mode</em>.
  271. <p>
  272. Whenever there is an error,
  273. an <em>error object</em> (also called an <em>error message</em>)
  274. is propagated with information about the error.
  275. Lua itself only generates errors whose error object is a string,
  276. but programs may generate errors with
  277. any value as the error object.
  278. It is up to the Lua program or its host to handle such error objects.
  279. <p>
  280. When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>,
  281. you may give a <em>message handler</em>
  282. to be called in case of errors.
  283. This function is called with the original error object
  284. and returns a new error object.
  285. It is called before the error unwinds the stack,
  286. so that it can gather more information about the error,
  287. for instance by inspecting the stack and creating a stack traceback.
  288. This message handler is still protected by the protected call;
  289. so, an error inside the message handler
  290. will call the message handler again.
  291. If this loop goes on for too long,
  292. Lua breaks it and returns an appropriate message.
  293. (The message handler is called only for regular runtime errors.
  294. It is not called for memory-allocation errors
  295. nor for errors while running finalizers.)
  296. <h2>2.4 &ndash; <a name="2.4">Metatables and Metamethods</a></h2>
  297. <p>
  298. Every value in Lua can have a <em>metatable</em>.
  299. This <em>metatable</em> is an ordinary Lua table
  300. that defines the behavior of the original value
  301. under certain special operations.
  302. You can change several aspects of the behavior
  303. of operations over a value by setting specific fields in its metatable.
  304. For instance, when a non-numeric value is the operand of an addition,
  305. Lua checks for a function in the field "<code>__add</code>" of the value's metatable.
  306. If it finds one,
  307. Lua calls this function to perform the addition.
  308. <p>
  309. The key for each event in a metatable is a string
  310. with the event name prefixed by two underscores;
  311. the corresponding values are called <em>metamethods</em>.
  312. In the previous example, the key is "<code>__add</code>"
  313. and the metamethod is the function that performs the addition.
  314. Unless stated otherwise,
  315. metamethods should be function values.
  316. <p>
  317. You can query the metatable of any value
  318. using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function.
  319. Lua queries metamethods in metatables using a raw access (see <a href="#pdf-rawget"><code>rawget</code></a>).
  320. So, to retrieve the metamethod for event <code>ev</code> in object <code>o</code>,
  321. Lua does the equivalent to the following code:
  322. <pre>
  323. rawget(getmetatable(<em>o</em>) or {}, "__<em>ev</em>")
  324. </pre>
  325. <p>
  326. You can replace the metatable of tables
  327. using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function.
  328. You cannot change the metatable of other types from Lua code
  329. (except by using the debug library (<a href="#6.10">&sect;6.10</a>));
  330. you should use the C&nbsp;API for that.
  331. <p>
  332. Tables and full userdata have individual metatables
  333. (although multiple tables and userdata can share their metatables).
  334. Values of all other types share one single metatable per type;
  335. that is, there is one single metatable for all numbers,
  336. one for all strings, etc.
  337. By default, a value has no metatable,
  338. but the string library sets a metatable for the string type (see <a href="#6.4">&sect;6.4</a>).
  339. <p>
  340. A metatable controls how an object behaves in
  341. arithmetic operations, bitwise operations,
  342. order comparisons, concatenation, length operation, calls, and indexing.
  343. A metatable also can define a function to be called
  344. when a userdata or a table is garbage collected (<a href="#2.5">&sect;2.5</a>).
  345. <p>
  346. For the unary operators (negation, length, and bitwise NOT),
  347. the metamethod is computed and called with a dummy second operand,
  348. equal to the first one.
  349. This extra operand is only to simplify Lua's internals
  350. (by making these operators behave like a binary operation)
  351. and may be removed in future versions.
  352. (For most uses this extra operand is irrelevant.)
  353. <p>
  354. A detailed list of events controlled by metatables is given next.
  355. Each operation is identified by its corresponding key.
  356. <ul>
  357. <li><b><code>__add</code>: </b>
  358. the addition (<code>+</code>) operation.
  359. If any operand for an addition is not a number
  360. (nor a string coercible to a number),
  361. Lua will try to call a metamethod.
  362. First, Lua will check the first operand (even if it is valid).
  363. If that operand does not define a metamethod for <code>__add</code>,
  364. then Lua will check the second operand.
  365. If Lua can find a metamethod,
  366. it calls the metamethod with the two operands as arguments,
  367. and the result of the call
  368. (adjusted to one value)
  369. is the result of the operation.
  370. Otherwise,
  371. it raises an error.
  372. </li>
  373. <li><b><code>__sub</code>: </b>
  374. the subtraction (<code>-</code>) operation.
  375. Behavior similar to the addition operation.
  376. </li>
  377. <li><b><code>__mul</code>: </b>
  378. the multiplication (<code>*</code>) operation.
  379. Behavior similar to the addition operation.
  380. </li>
  381. <li><b><code>__div</code>: </b>
  382. the division (<code>/</code>) operation.
  383. Behavior similar to the addition operation.
  384. </li>
  385. <li><b><code>__mod</code>: </b>
  386. the modulo (<code>%</code>) operation.
  387. Behavior similar to the addition operation.
  388. </li>
  389. <li><b><code>__pow</code>: </b>
  390. the exponentiation (<code>^</code>) operation.
  391. Behavior similar to the addition operation.
  392. </li>
  393. <li><b><code>__unm</code>: </b>
  394. the negation (unary <code>-</code>) operation.
  395. Behavior similar to the addition operation.
  396. </li>
  397. <li><b><code>__idiv</code>: </b>
  398. the floor division (<code>//</code>) operation.
  399. Behavior similar to the addition operation.
  400. </li>
  401. <li><b><code>__band</code>: </b>
  402. the bitwise AND (<code>&amp;</code>) operation.
  403. Behavior similar to the addition operation,
  404. except that Lua will try a metamethod
  405. if any operand is neither an integer
  406. nor a value coercible to an integer (see <a href="#3.4.3">&sect;3.4.3</a>).
  407. </li>
  408. <li><b><code>__bor</code>: </b>
  409. the bitwise OR (<code>|</code>) operation.
  410. Behavior similar to the bitwise AND operation.
  411. </li>
  412. <li><b><code>__bxor</code>: </b>
  413. the bitwise exclusive OR (binary <code>~</code>) operation.
  414. Behavior similar to the bitwise AND operation.
  415. </li>
  416. <li><b><code>__bnot</code>: </b>
  417. the bitwise NOT (unary <code>~</code>) operation.
  418. Behavior similar to the bitwise AND operation.
  419. </li>
  420. <li><b><code>__shl</code>: </b>
  421. the bitwise left shift (<code>&lt;&lt;</code>) operation.
  422. Behavior similar to the bitwise AND operation.
  423. </li>
  424. <li><b><code>__shr</code>: </b>
  425. the bitwise right shift (<code>&gt;&gt;</code>) operation.
  426. Behavior similar to the bitwise AND operation.
  427. </li>
  428. <li><b><code>__concat</code>: </b>
  429. the concatenation (<code>..</code>) operation.
  430. Behavior similar to the addition operation,
  431. except that Lua will try a metamethod
  432. if any operand is neither a string nor a number
  433. (which is always coercible to a string).
  434. </li>
  435. <li><b><code>__len</code>: </b>
  436. the length (<code>#</code>) operation.
  437. If the object is not a string,
  438. Lua will try its metamethod.
  439. If there is a metamethod,
  440. Lua calls it with the object as argument,
  441. and the result of the call
  442. (always adjusted to one value)
  443. is the result of the operation.
  444. If there is no metamethod but the object is a table,
  445. then Lua uses the table length operation (see <a href="#3.4.7">&sect;3.4.7</a>).
  446. Otherwise, Lua raises an error.
  447. </li>
  448. <li><b><code>__eq</code>: </b>
  449. the equal (<code>==</code>) operation.
  450. Behavior similar to the addition operation,
  451. except that Lua will try a metamethod only when the values
  452. being compared are either both tables or both full userdata
  453. and they are not primitively equal.
  454. The result of the call is always converted to a boolean.
  455. </li>
  456. <li><b><code>__lt</code>: </b>
  457. the less than (<code>&lt;</code>) operation.
  458. Behavior similar to the addition operation,
  459. except that Lua will try a metamethod only when the values
  460. being compared are neither both numbers nor both strings.
  461. The result of the call is always converted to a boolean.
  462. </li>
  463. <li><b><code>__le</code>: </b>
  464. the less equal (<code>&lt;=</code>) operation.
  465. Unlike other operations,
  466. the less-equal operation can use two different events.
  467. First, Lua looks for the <code>__le</code> metamethod in both operands,
  468. like in the less than operation.
  469. If it cannot find such a metamethod,
  470. then it will try the <code>__lt</code> metamethod,
  471. assuming that <code>a &lt;= b</code> is equivalent to <code>not (b &lt; a)</code>.
  472. As with the other comparison operators,
  473. the result is always a boolean.
  474. (This use of the <code>__lt</code> event can be removed in future versions;
  475. it is also slower than a real <code>__le</code> metamethod.)
  476. </li>
  477. <li><b><code>__index</code>: </b>
  478. The indexing access operation <code>table[key]</code>.
  479. This event happens when <code>table</code> is not a table or
  480. when <code>key</code> is not present in <code>table</code>.
  481. The metamethod is looked up in <code>table</code>.
  482. <p>
  483. Despite the name,
  484. the metamethod for this event can be either a function or a table.
  485. If it is a function,
  486. it is called with <code>table</code> and <code>key</code> as arguments,
  487. and the result of the call
  488. (adjusted to one value)
  489. is the result of the operation.
  490. If it is a table,
  491. the final result is the result of indexing this table with <code>key</code>.
  492. (This indexing is regular, not raw,
  493. and therefore can trigger another metamethod.)
  494. </li>
  495. <li><b><code>__newindex</code>: </b>
  496. The indexing assignment <code>table[key] = value</code>.
  497. Like the index event,
  498. this event happens when <code>table</code> is not a table or
  499. when <code>key</code> is not present in <code>table</code>.
  500. The metamethod is looked up in <code>table</code>.
  501. <p>
  502. Like with indexing,
  503. the metamethod for this event can be either a function or a table.
  504. If it is a function,
  505. it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments.
  506. If it is a table,
  507. Lua does an indexing assignment to this table with the same key and value.
  508. (This assignment is regular, not raw,
  509. and therefore can trigger another metamethod.)
  510. <p>
  511. Whenever there is a <code>__newindex</code> metamethod,
  512. Lua does not perform the primitive assignment.
  513. (If necessary,
  514. the metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a>
  515. to do the assignment.)
  516. </li>
  517. <li><b><code>__call</code>: </b>
  518. The call operation <code>func(args)</code>.
  519. This event happens when Lua tries to call a non-function value
  520. (that is, <code>func</code> is not a function).
  521. The metamethod is looked up in <code>func</code>.
  522. If present,
  523. the metamethod is called with <code>func</code> as its first argument,
  524. followed by the arguments of the original call (<code>args</code>).
  525. All results of the call
  526. are the result of the operation.
  527. (This is the only metamethod that allows multiple results.)
  528. </li>
  529. </ul>
  530. <p>
  531. It is a good practice to add all needed metamethods to a table
  532. before setting it as a metatable of some object.
  533. In particular, the <code>__gc</code> metamethod works only when this order
  534. is followed (see <a href="#2.5.1">&sect;2.5.1</a>).
  535. <p>
  536. Because metatables are regular tables,
  537. they can contain arbitrary fields,
  538. not only the event names defined above.
  539. Some functions in the standard library
  540. (e.g., <a href="#pdf-tostring"><code>tostring</code></a>)
  541. use other fields in metatables for their own purposes.
  542. <h2>2.5 &ndash; <a name="2.5">Garbage Collection</a></h2>
  543. <p>
  544. Lua performs automatic memory management.
  545. This means that
  546. you do not have to worry about allocating memory for new objects
  547. or freeing it when the objects are no longer needed.
  548. Lua manages memory automatically by running
  549. a <em>garbage collector</em> to collect all <em>dead objects</em>
  550. (that is, objects that are no longer accessible from Lua).
  551. All memory used by Lua is subject to automatic management:
  552. strings, tables, userdata, functions, threads, internal structures, etc.
  553. <p>
  554. Lua implements an incremental mark-and-sweep collector.
  555. It uses two numbers to control its garbage-collection cycles:
  556. the <em>garbage-collector pause</em> and
  557. the <em>garbage-collector step multiplier</em>.
  558. Both use percentage points as units
  559. (e.g., a value of 100 means an internal value of 1).
  560. <p>
  561. The garbage-collector pause
  562. controls how long the collector waits before starting a new cycle.
  563. Larger values make the collector less aggressive.
  564. Values smaller than 100 mean the collector will not wait to
  565. start a new cycle.
  566. A value of 200 means that the collector waits for the total memory in use
  567. to double before starting a new cycle.
  568. <p>
  569. The garbage-collector step multiplier
  570. controls the relative speed of the collector relative to
  571. memory allocation.
  572. Larger values make the collector more aggressive but also increase
  573. the size of each incremental step.
  574. You should not use values smaller than 100,
  575. because they make the collector too slow and
  576. can result in the collector never finishing a cycle.
  577. The default is 200,
  578. which means that the collector runs at "twice"
  579. the speed of memory allocation.
  580. <p>
  581. If you set the step multiplier to a very large number
  582. (larger than 10% of the maximum number of
  583. bytes that the program may use),
  584. the collector behaves like a stop-the-world collector.
  585. If you then set the pause to 200,
  586. the collector behaves as in old Lua versions,
  587. doing a complete collection every time Lua doubles its
  588. memory usage.
  589. <p>
  590. You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
  591. or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
  592. You can also use these functions to control
  593. the collector directly (e.g., stop and restart it).
  594. <h3>2.5.1 &ndash; <a name="2.5.1">Garbage-Collection Metamethods</a></h3>
  595. <p>
  596. You can set garbage-collector metamethods for tables
  597. and, using the C&nbsp;API,
  598. for full userdata (see <a href="#2.4">&sect;2.4</a>).
  599. These metamethods are also called <em>finalizers</em>.
  600. Finalizers allow you to coordinate Lua's garbage collection
  601. with external resource management
  602. (such as closing files, network or database connections,
  603. or freeing your own memory).
  604. <p>
  605. For an object (table or userdata) to be finalized when collected,
  606. you must <em>mark</em> it for finalization.
  607. You mark an object for finalization when you set its metatable
  608. and the metatable has a field indexed by the string "<code>__gc</code>".
  609. Note that if you set a metatable without a <code>__gc</code> field
  610. and later create that field in the metatable,
  611. the object will not be marked for finalization.
  612. <p>
  613. When a marked object becomes garbage,
  614. it is not collected immediately by the garbage collector.
  615. Instead, Lua puts it in a list.
  616. After the collection,
  617. Lua goes through that list.
  618. For each object in the list,
  619. it checks the object's <code>__gc</code> metamethod:
  620. If it is a function,
  621. Lua calls it with the object as its single argument;
  622. if the metamethod is not a function,
  623. Lua simply ignores it.
  624. <p>
  625. At the end of each garbage-collection cycle,
  626. the finalizers for objects are called in
  627. the reverse order that the objects were marked for finalization,
  628. among those collected in that cycle;
  629. that is, the first finalizer to be called is the one associated
  630. with the object marked last in the program.
  631. The execution of each finalizer may occur at any point during
  632. the execution of the regular code.
  633. <p>
  634. Because the object being collected must still be used by the finalizer,
  635. that object (and other objects accessible only through it)
  636. must be <em>resurrected</em> by Lua.
  637. Usually, this resurrection is transient,
  638. and the object memory is freed in the next garbage-collection cycle.
  639. However, if the finalizer stores the object in some global place
  640. (e.g., a global variable),
  641. then the resurrection is permanent.
  642. Moreover, if the finalizer marks a finalizing object for finalization again,
  643. its finalizer will be called again in the next cycle where the
  644. object is unreachable.
  645. In any case,
  646. the object memory is freed only in a GC cycle where
  647. the object is unreachable and not marked for finalization.
  648. <p>
  649. When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
  650. Lua calls the finalizers of all objects marked for finalization,
  651. following the reverse order that they were marked.
  652. If any finalizer marks objects for collection during that phase,
  653. these marks have no effect.
  654. <h3>2.5.2 &ndash; <a name="2.5.2">Weak Tables</a></h3>
  655. <p>
  656. A <em>weak table</em> is a table whose elements are
  657. <em>weak references</em>.
  658. A weak reference is ignored by the garbage collector.
  659. In other words,
  660. if the only references to an object are weak references,
  661. then the garbage collector will collect that object.
  662. <p>
  663. A weak table can have weak keys, weak values, or both.
  664. A table with weak values allows the collection of its values,
  665. but prevents the collection of its keys.
  666. A table with both weak keys and weak values allows the collection of
  667. both keys and values.
  668. In any case, if either the key or the value is collected,
  669. the whole pair is removed from the table.
  670. The weakness of a table is controlled by the
  671. <code>__mode</code> field of its metatable.
  672. If the <code>__mode</code> field is a string containing the character&nbsp;'<code>k</code>',
  673. the keys in the table are weak.
  674. If <code>__mode</code> contains '<code>v</code>',
  675. the values in the table are weak.
  676. <p>
  677. A table with weak keys and strong values
  678. is also called an <em>ephemeron table</em>.
  679. In an ephemeron table,
  680. a value is considered reachable only if its key is reachable.
  681. In particular,
  682. if the only reference to a key comes through its value,
  683. the pair is removed.
  684. <p>
  685. Any change in the weakness of a table may take effect only
  686. at the next collect cycle.
  687. In particular, if you change the weakness to a stronger mode,
  688. Lua may still collect some items from that table
  689. before the change takes effect.
  690. <p>
  691. Only objects that have an explicit construction
  692. are removed from weak tables.
  693. Values, such as numbers and light C&nbsp;functions,
  694. are not subject to garbage collection,
  695. and therefore are not removed from weak tables
  696. (unless their associated values are collected).
  697. Although strings are subject to garbage collection,
  698. they do not have an explicit construction,
  699. and therefore are not removed from weak tables.
  700. <p>
  701. Resurrected objects
  702. (that is, objects being finalized
  703. and objects accessible only through objects being finalized)
  704. have a special behavior in weak tables.
  705. They are removed from weak values before running their finalizers,
  706. but are removed from weak keys only in the next collection
  707. after running their finalizers, when such objects are actually freed.
  708. This behavior allows the finalizer to access properties
  709. associated with the object through weak tables.
  710. <p>
  711. If a weak table is among the resurrected objects in a collection cycle,
  712. it may not be properly cleared until the next cycle.
  713. <h2>2.6 &ndash; <a name="2.6">Coroutines</a></h2>
  714. <p>
  715. Lua supports coroutines,
  716. also called <em>collaborative multithreading</em>.
  717. A coroutine in Lua represents an independent thread of execution.
  718. Unlike threads in multithread systems, however,
  719. a coroutine only suspends its execution by explicitly calling
  720. a yield function.
  721. <p>
  722. You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>.
  723. Its sole argument is a function
  724. that is the main function of the coroutine.
  725. The <code>create</code> function only creates a new coroutine and
  726. returns a handle to it (an object of type <em>thread</em>);
  727. it does not start the coroutine.
  728. <p>
  729. You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  730. When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
  731. passing as its first argument
  732. a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
  733. the coroutine starts its execution by
  734. calling its main function.
  735. Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed
  736. as arguments to that function.
  737. After the coroutine starts running,
  738. it runs until it terminates or <em>yields</em>.
  739. <p>
  740. A coroutine can terminate its execution in two ways:
  741. normally, when its main function returns
  742. (explicitly or implicitly, after the last instruction);
  743. and abnormally, if there is an unprotected error.
  744. In case of normal termination,
  745. <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>,
  746. plus any values returned by the coroutine main function.
  747. In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b>
  748. plus an error object.
  749. <p>
  750. A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
  751. When a coroutine yields,
  752. the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately,
  753. even if the yield happens inside nested function calls
  754. (that is, not in the main function,
  755. but in a function directly or indirectly called by the main function).
  756. In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>,
  757. plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
  758. The next time you resume the same coroutine,
  759. it continues its execution from the point where it yielded,
  760. with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra
  761. arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  762. <p>
  763. Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
  764. the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine,
  765. but instead of returning the coroutine itself,
  766. it returns a function that, when called, resumes the coroutine.
  767. Any arguments passed to this function
  768. go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
  769. <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>,
  770. except the first one (the boolean error code).
  771. Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
  772. <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors;
  773. any error is propagated to the caller.
  774. <p>
  775. As an example of how coroutines work,
  776. consider the following code:
  777. <pre>
  778. function foo (a)
  779. print("foo", a)
  780. return coroutine.yield(2*a)
  781. end
  782. co = coroutine.create(function (a,b)
  783. print("co-body", a, b)
  784. local r = foo(a+1)
  785. print("co-body", r)
  786. local r, s = coroutine.yield(a+b, a-b)
  787. print("co-body", r, s)
  788. return b, "end"
  789. end)
  790. print("main", coroutine.resume(co, 1, 10))
  791. print("main", coroutine.resume(co, "r"))
  792. print("main", coroutine.resume(co, "x", "y"))
  793. print("main", coroutine.resume(co, "x", "y"))
  794. </pre><p>
  795. When you run it, it produces the following output:
  796. <pre>
  797. co-body 1 10
  798. foo 2
  799. main true 4
  800. co-body r
  801. main true 11 -9
  802. co-body x y
  803. main true 10 end
  804. main false cannot resume dead coroutine
  805. </pre>
  806. <p>
  807. You can also create and manipulate coroutines through the C API:
  808. see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>,
  809. and <a href="#lua_yield"><code>lua_yield</code></a>.
  810. <h1>3 &ndash; <a name="3">The Language</a></h1>
  811. <p>
  812. This section describes the lexis, the syntax, and the semantics of Lua.
  813. In other words,
  814. this section describes
  815. which tokens are valid,
  816. how they can be combined,
  817. and what their combinations mean.
  818. <p>
  819. Language constructs will be explained using the usual extended BNF notation,
  820. in which
  821. {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and
  822. [<em>a</em>]&nbsp;means an optional <em>a</em>.
  823. Non-terminals are shown like non-terminal,
  824. keywords are shown like <b>kword</b>,
  825. and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;.
  826. The complete syntax of Lua can be found in <a href="#9">&sect;9</a>
  827. at the end of this manual.
  828. <h2>3.1 &ndash; <a name="3.1">Lexical Conventions</a></h2>
  829. <p>
  830. Lua is a free-form language.
  831. It ignores spaces (including new lines) and comments
  832. between lexical elements (tokens),
  833. except as delimiters between names and keywords.
  834. <p>
  835. <em>Names</em>
  836. (also called <em>identifiers</em>)
  837. in Lua can be any string of letters,
  838. digits, and underscores,
  839. not beginning with a digit and
  840. not being a reserved word.
  841. Identifiers are used to name variables, table fields, and labels.
  842. <p>
  843. The following <em>keywords</em> are reserved
  844. and cannot be used as names:
  845. <pre>
  846. and break do else elseif end
  847. false for function goto if in
  848. local nil not or repeat return
  849. then true until while
  850. </pre>
  851. <p>
  852. Lua is a case-sensitive language:
  853. <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
  854. are two different, valid names.
  855. As a convention,
  856. programs should avoid creating
  857. names that start with an underscore followed by
  858. one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>).
  859. <p>
  860. The following strings denote other tokens:
  861. <pre>
  862. + - * / % ^ #
  863. &amp; ~ | &lt;&lt; &gt;&gt; //
  864. == ~= &lt;= &gt;= &lt; &gt; =
  865. ( ) { } [ ] ::
  866. ; : , . .. ...
  867. </pre>
  868. <p>
  869. A <em>short literal string</em>
  870. can be delimited by matching single or double quotes,
  871. and can contain the following C-like escape sequences:
  872. '<code>\a</code>' (bell),
  873. '<code>\b</code>' (backspace),
  874. '<code>\f</code>' (form feed),
  875. '<code>\n</code>' (newline),
  876. '<code>\r</code>' (carriage return),
  877. '<code>\t</code>' (horizontal tab),
  878. '<code>\v</code>' (vertical tab),
  879. '<code>\\</code>' (backslash),
  880. '<code>\"</code>' (quotation mark [double quote]),
  881. and '<code>\'</code>' (apostrophe [single quote]).
  882. A backslash followed by a line break
  883. results in a newline in the string.
  884. The escape sequence '<code>\z</code>' skips the following span
  885. of white-space characters,
  886. including line breaks;
  887. it is particularly useful to break and indent a long literal string
  888. into multiple lines without adding the newlines and spaces
  889. into the string contents.
  890. A short literal string cannot contain unescaped line breaks
  891. nor escapes not forming a valid escape sequence.
  892. <p>
  893. We can specify any byte in a short literal string by its numeric value
  894. (including embedded zeros).
  895. This can be done
  896. with the escape sequence <code>\x<em>XX</em></code>,
  897. where <em>XX</em> is a sequence of exactly two hexadecimal digits,
  898. or with the escape sequence <code>\<em>ddd</em></code>,
  899. where <em>ddd</em> is a sequence of up to three decimal digits.
  900. (Note that if a decimal escape sequence is to be followed by a digit,
  901. it must be expressed using exactly three digits.)
  902. <p>
  903. The UTF-8 encoding of a Unicode character
  904. can be inserted in a literal string with
  905. the escape sequence <code>\u{<em>XXX</em>}</code>
  906. (note the mandatory enclosing brackets),
  907. where <em>XXX</em> is a sequence of one or more hexadecimal digits
  908. representing the character code point.
  909. <p>
  910. Literal strings can also be defined using a long format
  911. enclosed by <em>long brackets</em>.
  912. We define an <em>opening long bracket of level <em>n</em></em> as an opening
  913. square bracket followed by <em>n</em> equal signs followed by another
  914. opening square bracket.
  915. So, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,
  916. an opening long bracket of level&nbsp;1 is written as <code>[=[</code>,
  917. and so on.
  918. A <em>closing long bracket</em> is defined similarly;
  919. for instance,
  920. a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.
  921. A <em>long literal</em> starts with an opening long bracket of any level and
  922. ends at the first closing long bracket of the same level.
  923. It can contain any text except a closing bracket of the same level.
  924. Literals in this bracketed form can run for several lines,
  925. do not interpret any escape sequences,
  926. and ignore long brackets of any other level.
  927. Any kind of end-of-line sequence
  928. (carriage return, newline, carriage return followed by newline,
  929. or newline followed by carriage return)
  930. is converted to a simple newline.
  931. <p>
  932. For convenience,
  933. when the opening long bracket is immediately followed by a newline,
  934. the newline is not included in the string.
  935. As an example, in a system using ASCII
  936. (in which '<code>a</code>' is coded as&nbsp;97,
  937. newline is coded as&nbsp;10, and '<code>1</code>' is coded as&nbsp;49),
  938. the five literal strings below denote the same string:
  939. <pre>
  940. a = 'alo\n123"'
  941. a = "alo\n123\""
  942. a = '\97lo\10\04923"'
  943. a = [[alo
  944. 123"]]
  945. a = [==[
  946. alo
  947. 123"]==]
  948. </pre>
  949. <p>
  950. Any byte in a literal string not
  951. explicitly affected by the previous rules represents itself.
  952. However, Lua opens files for parsing in text mode,
  953. and the system file functions may have problems with
  954. some control characters.
  955. So, it is safer to represent
  956. non-text data as a quoted literal with
  957. explicit escape sequences for the non-text characters.
  958. <p>
  959. A <em>numeric constant</em> (or <em>numeral</em>)
  960. can be written with an optional fractional part
  961. and an optional decimal exponent,
  962. marked by a letter '<code>e</code>' or '<code>E</code>'.
  963. Lua also accepts hexadecimal constants,
  964. which start with <code>0x</code> or <code>0X</code>.
  965. Hexadecimal constants also accept an optional fractional part
  966. plus an optional binary exponent,
  967. marked by a letter '<code>p</code>' or '<code>P</code>'.
  968. A numeric constant with a radix point or an exponent
  969. denotes a float;
  970. otherwise,
  971. if its value fits in an integer,
  972. it denotes an integer.
  973. Examples of valid integer constants are
  974. <pre>
  975. 3 345 0xff 0xBEBADA
  976. </pre><p>
  977. Examples of valid float constants are
  978. <pre>
  979. 3.0 3.1416 314.16e-2 0.31416E1 34e1
  980. 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
  981. </pre>
  982. <p>
  983. A <em>comment</em> starts with a double hyphen (<code>--</code>)
  984. anywhere outside a string.
  985. If the text immediately after <code>--</code> is not an opening long bracket,
  986. the comment is a <em>short comment</em>,
  987. which runs until the end of the line.
  988. Otherwise, it is a <em>long comment</em>,
  989. which runs until the corresponding closing long bracket.
  990. Long comments are frequently used to disable code temporarily.
  991. <h2>3.2 &ndash; <a name="3.2">Variables</a></h2>
  992. <p>
  993. Variables are places that store values.
  994. There are three kinds of variables in Lua:
  995. global variables, local variables, and table fields.
  996. <p>
  997. A single name can denote a global variable or a local variable
  998. (or a function's formal parameter,
  999. which is a particular kind of local variable):
  1000. <pre>
  1001. var ::= Name
  1002. </pre><p>
  1003. Name denotes identifiers, as defined in <a href="#3.1">&sect;3.1</a>.
  1004. <p>
  1005. Any variable name is assumed to be global unless explicitly declared
  1006. as a local (see <a href="#3.3.7">&sect;3.3.7</a>).
  1007. Local variables are <em>lexically scoped</em>:
  1008. local variables can be freely accessed by functions
  1009. defined inside their scope (see <a href="#3.5">&sect;3.5</a>).
  1010. <p>
  1011. Before the first assignment to a variable, its value is <b>nil</b>.
  1012. <p>
  1013. Square brackets are used to index a table:
  1014. <pre>
  1015. var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo;
  1016. </pre><p>
  1017. The meaning of accesses to table fields can be changed via metatables
  1018. (see <a href="#2.4">&sect;2.4</a>).
  1019. <p>
  1020. The syntax <code>var.Name</code> is just syntactic sugar for
  1021. <code>var["Name"]</code>:
  1022. <pre>
  1023. var ::= prefixexp &lsquo;<b>.</b>&rsquo; Name
  1024. </pre>
  1025. <p>
  1026. An access to a global variable <code>x</code>
  1027. is equivalent to <code>_ENV.x</code>.
  1028. Due to the way that chunks are compiled,
  1029. <code>_ENV</code> is never a global name (see <a href="#2.2">&sect;2.2</a>).
  1030. <h2>3.3 &ndash; <a name="3.3">Statements</a></h2>
  1031. <p>
  1032. Lua supports an almost conventional set of statements,
  1033. similar to those in Pascal or C.
  1034. This set includes
  1035. assignments, control structures, function calls,
  1036. and variable declarations.
  1037. <h3>3.3.1 &ndash; <a name="3.3.1">Blocks</a></h3>
  1038. <p>
  1039. A block is a list of statements,
  1040. which are executed sequentially:
  1041. <pre>
  1042. block ::= {stat}
  1043. </pre><p>
  1044. Lua has <em>empty statements</em>
  1045. that allow you to separate statements with semicolons,
  1046. start a block with a semicolon
  1047. or write two semicolons in sequence:
  1048. <pre>
  1049. stat ::= &lsquo;<b>;</b>&rsquo;
  1050. </pre>
  1051. <p>
  1052. Function calls and assignments
  1053. can start with an open parenthesis.
  1054. This possibility leads to an ambiguity in Lua's grammar.
  1055. Consider the following fragment:
  1056. <pre>
  1057. a = b + c
  1058. (print or io.write)('done')
  1059. </pre><p>
  1060. The grammar could see it in two ways:
  1061. <pre>
  1062. a = b + c(print or io.write)('done')
  1063. a = b + c; (print or io.write)('done')
  1064. </pre><p>
  1065. The current parser always sees such constructions
  1066. in the first way,
  1067. interpreting the open parenthesis
  1068. as the start of the arguments to a call.
  1069. To avoid this ambiguity,
  1070. it is a good practice to always precede with a semicolon
  1071. statements that start with a parenthesis:
  1072. <pre>
  1073. ;(print or io.write)('done')
  1074. </pre>
  1075. <p>
  1076. A block can be explicitly delimited to produce a single statement:
  1077. <pre>
  1078. stat ::= <b>do</b> block <b>end</b>
  1079. </pre><p>
  1080. Explicit blocks are useful
  1081. to control the scope of variable declarations.
  1082. Explicit blocks are also sometimes used to
  1083. add a <b>return</b> statement in the middle
  1084. of another block (see <a href="#3.3.4">&sect;3.3.4</a>).
  1085. <h3>3.3.2 &ndash; <a name="3.3.2">Chunks</a></h3>
  1086. <p>
  1087. The unit of compilation of Lua is called a <em>chunk</em>.
  1088. Syntactically,
  1089. a chunk is simply a block:
  1090. <pre>
  1091. chunk ::= block
  1092. </pre>
  1093. <p>
  1094. Lua handles a chunk as the body of an anonymous function
  1095. with a variable number of arguments
  1096. (see <a href="#3.4.11">&sect;3.4.11</a>).
  1097. As such, chunks can define local variables,
  1098. receive arguments, and return values.
  1099. Moreover, such anonymous function is compiled as in the
  1100. scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">&sect;2.2</a>).
  1101. The resulting function always has <code>_ENV</code> as its only upvalue,
  1102. even if it does not use that variable.
  1103. <p>
  1104. A chunk can be stored in a file or in a string inside the host program.
  1105. To execute a chunk,
  1106. Lua first <em>loads</em> it,
  1107. precompiling the chunk's code into instructions for a virtual machine,
  1108. and then Lua executes the compiled code
  1109. with an interpreter for the virtual machine.
  1110. <p>
  1111. Chunks can also be precompiled into binary form;
  1112. see program <code>luac</code> and function <a href="#pdf-string.dump"><code>string.dump</code></a> for details.
  1113. Programs in source and compiled forms are interchangeable;
  1114. Lua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>).
  1115. <h3>3.3.3 &ndash; <a name="3.3.3">Assignment</a></h3>
  1116. <p>
  1117. Lua allows multiple assignments.
  1118. Therefore, the syntax for assignment
  1119. defines a list of variables on the left side
  1120. and a list of expressions on the right side.
  1121. The elements in both lists are separated by commas:
  1122. <pre>
  1123. stat ::= varlist &lsquo;<b>=</b>&rsquo; explist
  1124. varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
  1125. explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
  1126. </pre><p>
  1127. Expressions are discussed in <a href="#3.4">&sect;3.4</a>.
  1128. <p>
  1129. Before the assignment,
  1130. the list of values is <em>adjusted</em> to the length of
  1131. the list of variables.
  1132. If there are more values than needed,
  1133. the excess values are thrown away.
  1134. If there are fewer values than needed,
  1135. the list is extended with as many <b>nil</b>'s as needed.
  1136. If the list of expressions ends with a function call,
  1137. then all values returned by that call enter the list of values,
  1138. before the adjustment
  1139. (except when the call is enclosed in parentheses; see <a href="#3.4">&sect;3.4</a>).
  1140. <p>
  1141. The assignment statement first evaluates all its expressions
  1142. and only then the assignments are performed.
  1143. Thus the code
  1144. <pre>
  1145. i = 3
  1146. i, a[i] = i+1, 20
  1147. </pre><p>
  1148. sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
  1149. because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
  1150. before it is assigned&nbsp;4.
  1151. Similarly, the line
  1152. <pre>
  1153. x, y = y, x
  1154. </pre><p>
  1155. exchanges the values of <code>x</code> and <code>y</code>,
  1156. and
  1157. <pre>
  1158. x, y, z = y, z, x
  1159. </pre><p>
  1160. cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
  1161. <p>
  1162. An assignment to a global name <code>x = val</code>
  1163. is equivalent to the assignment
  1164. <code>_ENV.x = val</code> (see <a href="#2.2">&sect;2.2</a>).
  1165. <p>
  1166. The meaning of assignments to table fields and
  1167. global variables (which are actually table fields, too)
  1168. can be changed via metatables (see <a href="#2.4">&sect;2.4</a>).
  1169. <h3>3.3.4 &ndash; <a name="3.3.4">Control Structures</a></h3><p>
  1170. The control structures
  1171. <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
  1172. familiar syntax:
  1173. <pre>
  1174. stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
  1175. stat ::= <b>repeat</b> block <b>until</b> exp
  1176. stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
  1177. </pre><p>
  1178. Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">&sect;3.3.5</a>).
  1179. <p>
  1180. The condition expression of a
  1181. control structure can return any value.
  1182. Both <b>false</b> and <b>nil</b> are considered false.
  1183. All values different from <b>nil</b> and <b>false</b> are considered true
  1184. (in particular, the number 0 and the empty string are also true).
  1185. <p>
  1186. In the <b>repeat</b>&ndash;<b>until</b> loop,
  1187. the inner block does not end at the <b>until</b> keyword,
  1188. but only after the condition.
  1189. So, the condition can refer to local variables
  1190. declared inside the loop block.
  1191. <p>
  1192. The <b>goto</b> statement transfers the program control to a label.
  1193. For syntactical reasons,
  1194. labels in Lua are considered statements too:
  1195. <pre>
  1196. stat ::= <b>goto</b> Name
  1197. stat ::= label
  1198. label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
  1199. </pre>
  1200. <p>
  1201. A label is visible in the entire block where it is defined,
  1202. except
  1203. inside nested blocks where a label with the same name is defined and
  1204. inside nested functions.
  1205. A goto may jump to any visible label as long as it does not
  1206. enter into the scope of a local variable.
  1207. <p>
  1208. Labels and empty statements are called <em>void statements</em>,
  1209. as they perform no actions.
  1210. <p>
  1211. The <b>break</b> statement terminates the execution of a
  1212. <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
  1213. skipping to the next statement after the loop:
  1214. <pre>
  1215. stat ::= <b>break</b>
  1216. </pre><p>
  1217. A <b>break</b> ends the innermost enclosing loop.
  1218. <p>
  1219. The <b>return</b> statement is used to return values
  1220. from a function or a chunk
  1221. (which is an anonymous function).
  1222. Functions can return more than one value,
  1223. so the syntax for the <b>return</b> statement is
  1224. <pre>
  1225. stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
  1226. </pre>
  1227. <p>
  1228. The <b>return</b> statement can only be written
  1229. as the last statement of a block.
  1230. If it is really necessary to <b>return</b> in the middle of a block,
  1231. then an explicit inner block can be used,
  1232. as in the idiom <code>do return end</code>,
  1233. because now <b>return</b> is the last statement in its (inner) block.
  1234. <h3>3.3.5 &ndash; <a name="3.3.5">For Statement</a></h3>
  1235. <p>
  1236. The <b>for</b> statement has two forms:
  1237. one numerical and one generic.
  1238. <p>
  1239. The numerical <b>for</b> loop repeats a block of code while a
  1240. control variable runs through an arithmetic progression.
  1241. It has the following syntax:
  1242. <pre>
  1243. 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>
  1244. </pre><p>
  1245. The <em>block</em> is repeated for <em>name</em> starting at the value of
  1246. the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
  1247. third <em>exp</em>.
  1248. More precisely, a <b>for</b> statement like
  1249. <pre>
  1250. for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end
  1251. </pre><p>
  1252. is equivalent to the code:
  1253. <pre>
  1254. do
  1255. local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
  1256. if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
  1257. <em>var</em> = <em>var</em> - <em>step</em>
  1258. while true do
  1259. <em>var</em> = <em>var</em> + <em>step</em>
  1260. if (<em>step</em> &gt;= 0 and <em>var</em> &gt; <em>limit</em>) or (<em>step</em> &lt; 0 and <em>var</em> &lt; <em>limit</em>) then
  1261. break
  1262. end
  1263. local v = <em>var</em>
  1264. <em>block</em>
  1265. end
  1266. end
  1267. </pre>
  1268. <p>
  1269. Note the following:
  1270. <ul>
  1271. <li>
  1272. All three control expressions are evaluated only once,
  1273. before the loop starts.
  1274. They must all result in numbers.
  1275. </li>
  1276. <li>
  1277. <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
  1278. The names shown here are for explanatory purposes only.
  1279. </li>
  1280. <li>
  1281. If the third expression (the step) is absent,
  1282. then a step of&nbsp;1 is used.
  1283. </li>
  1284. <li>
  1285. You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop.
  1286. </li>
  1287. <li>
  1288. The loop variable <code>v</code> is local to the loop body.
  1289. If you need its value after the loop,
  1290. assign it to another variable before exiting the loop.
  1291. </li>
  1292. </ul>
  1293. <p>
  1294. The generic <b>for</b> statement works over functions,
  1295. called <em>iterators</em>.
  1296. On each iteration, the iterator function is called to produce a new value,
  1297. stopping when this new value is <b>nil</b>.
  1298. The generic <b>for</b> loop has the following syntax:
  1299. <pre>
  1300. stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
  1301. namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
  1302. </pre><p>
  1303. A <b>for</b> statement like
  1304. <pre>
  1305. for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>explist</em> do <em>block</em> end
  1306. </pre><p>
  1307. is equivalent to the code:
  1308. <pre>
  1309. do
  1310. local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>
  1311. while true do
  1312. local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)
  1313. if <em>var_1</em> == nil then break end
  1314. <em>var</em> = <em>var_1</em>
  1315. <em>block</em>
  1316. end
  1317. end
  1318. </pre><p>
  1319. Note the following:
  1320. <ul>
  1321. <li>
  1322. <code><em>explist</em></code> is evaluated only once.
  1323. Its results are an <em>iterator</em> function,
  1324. a <em>state</em>,
  1325. and an initial value for the first <em>iterator variable</em>.
  1326. </li>
  1327. <li>
  1328. <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.
  1329. The names are here for explanatory purposes only.
  1330. </li>
  1331. <li>
  1332. You can use <b>break</b> to exit a <b>for</b> loop.
  1333. </li>
  1334. <li>
  1335. The loop variables <code><em>var_i</em></code> are local to the loop;
  1336. you cannot use their values after the <b>for</b> ends.
  1337. If you need these values,
  1338. then assign them to other variables before breaking or exiting the loop.
  1339. </li>
  1340. </ul>
  1341. <h3>3.3.6 &ndash; <a name="3.3.6">Function Calls as Statements</a></h3><p>
  1342. To allow possible side-effects,
  1343. function calls can be executed as statements:
  1344. <pre>
  1345. stat ::= functioncall
  1346. </pre><p>
  1347. In this case, all returned values are thrown away.
  1348. Function calls are explained in <a href="#3.4.10">&sect;3.4.10</a>.
  1349. <h3>3.3.7 &ndash; <a name="3.3.7">Local Declarations</a></h3><p>
  1350. Local variables can be declared anywhere inside a block.
  1351. The declaration can include an initial assignment:
  1352. <pre>
  1353. stat ::= <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
  1354. </pre><p>
  1355. If present, an initial assignment has the same semantics
  1356. of a multiple assignment (see <a href="#3.3.3">&sect;3.3.3</a>).
  1357. Otherwise, all variables are initialized with <b>nil</b>.
  1358. <p>
  1359. A chunk is also a block (see <a href="#3.3.2">&sect;3.3.2</a>),
  1360. and so local variables can be declared in a chunk outside any explicit block.
  1361. <p>
  1362. The visibility rules for local variables are explained in <a href="#3.5">&sect;3.5</a>.
  1363. <h2>3.4 &ndash; <a name="3.4">Expressions</a></h2>
  1364. <p>
  1365. The basic expressions in Lua are the following:
  1366. <pre>
  1367. exp ::= prefixexp
  1368. exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
  1369. exp ::= Numeral
  1370. exp ::= LiteralString
  1371. exp ::= functiondef
  1372. exp ::= tableconstructor
  1373. exp ::= &lsquo;<b>...</b>&rsquo;
  1374. exp ::= exp binop exp
  1375. exp ::= unop exp
  1376. prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
  1377. </pre>
  1378. <p>
  1379. Numerals and literal strings are explained in <a href="#3.1">&sect;3.1</a>;
  1380. variables are explained in <a href="#3.2">&sect;3.2</a>;
  1381. function definitions are explained in <a href="#3.4.11">&sect;3.4.11</a>;
  1382. function calls are explained in <a href="#3.4.10">&sect;3.4.10</a>;
  1383. table constructors are explained in <a href="#3.4.9">&sect;3.4.9</a>.
  1384. Vararg expressions,
  1385. denoted by three dots ('<code>...</code>'), can only be used when
  1386. directly inside a vararg function;
  1387. they are explained in <a href="#3.4.11">&sect;3.4.11</a>.
  1388. <p>
  1389. Binary operators comprise arithmetic operators (see <a href="#3.4.1">&sect;3.4.1</a>),
  1390. bitwise operators (see <a href="#3.4.2">&sect;3.4.2</a>),
  1391. 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>),
  1392. and the concatenation operator (see <a href="#3.4.6">&sect;3.4.6</a>).
  1393. Unary operators comprise the unary minus (see <a href="#3.4.1">&sect;3.4.1</a>),
  1394. the unary bitwise NOT (see <a href="#3.4.2">&sect;3.4.2</a>),
  1395. the unary logical <b>not</b> (see <a href="#3.4.5">&sect;3.4.5</a>),
  1396. and the unary <em>length operator</em> (see <a href="#3.4.7">&sect;3.4.7</a>).
  1397. <p>
  1398. Both function calls and vararg expressions can result in multiple values.
  1399. If a function call is used as a statement (see <a href="#3.3.6">&sect;3.3.6</a>),
  1400. then its return list is adjusted to zero elements,
  1401. thus discarding all returned values.
  1402. If an expression is used as the last (or the only) element
  1403. of a list of expressions,
  1404. then no adjustment is made
  1405. (unless the expression is enclosed in parentheses).
  1406. In all other contexts,
  1407. Lua adjusts the result list to one element,
  1408. either discarding all values except the first one
  1409. or adding a single <b>nil</b> if there are no values.
  1410. <p>
  1411. Here are some examples:
  1412. <pre>
  1413. f() -- adjusted to 0 results
  1414. g(f(), x) -- f() is adjusted to 1 result
  1415. g(x, f()) -- g gets x plus all results from f()
  1416. a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
  1417. a,b = ... -- a gets the first vararg argument, b gets
  1418. -- the second (both a and b can get nil if there
  1419. -- is no corresponding vararg argument)
  1420. a,b,c = x, f() -- f() is adjusted to 2 results
  1421. a,b,c = f() -- f() is adjusted to 3 results
  1422. return f() -- returns all results from f()
  1423. return ... -- returns all received vararg arguments
  1424. return x,y,f() -- returns x, y, and all results from f()
  1425. {f()} -- creates a list with all results from f()
  1426. {...} -- creates a list with all vararg arguments
  1427. {f(), nil} -- f() is adjusted to 1 result
  1428. </pre>
  1429. <p>
  1430. Any expression enclosed in parentheses always results in only one value.
  1431. Thus,
  1432. <code>(f(x,y,z))</code> is always a single value,
  1433. even if <code>f</code> returns several values.
  1434. (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
  1435. or <b>nil</b> if <code>f</code> does not return any values.)
  1436. <h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>
  1437. Lua supports the following arithmetic operators:
  1438. <ul>
  1439. <li><b><code>+</code>: </b>addition</li>
  1440. <li><b><code>-</code>: </b>subtraction</li>
  1441. <li><b><code>*</code>: </b>multiplication</li>
  1442. <li><b><code>/</code>: </b>float division</li>
  1443. <li><b><code>//</code>: </b>floor division</li>
  1444. <li><b><code>%</code>: </b>modulo</li>
  1445. <li><b><code>^</code>: </b>exponentiation</li>
  1446. <li><b><code>-</code>: </b>unary minus</li>
  1447. </ul>
  1448. <p>
  1449. With the exception of exponentiation and float division,
  1450. the arithmetic operators work as follows:
  1451. If both operands are integers,
  1452. the operation is performed over integers and the result is an integer.
  1453. Otherwise, if both operands are numbers
  1454. or strings that can be converted to
  1455. numbers (see <a href="#3.4.3">&sect;3.4.3</a>),
  1456. then they are converted to floats,
  1457. the operation is performed following the usual rules
  1458. for floating-point arithmetic
  1459. (usually the IEEE 754 standard),
  1460. and the result is a float.
  1461. <p>
  1462. Exponentiation and float division (<code>/</code>)
  1463. always convert their operands to floats
  1464. and the result is always a float.
  1465. Exponentiation uses the ISO&nbsp;C function <code>pow</code>,
  1466. so that it works for non-integer exponents too.
  1467. <p>
  1468. Floor division (<code>//</code>) is a division
  1469. that rounds the quotient towards minus infinity,
  1470. that is, the floor of the division of its operands.
  1471. <p>
  1472. Modulo is defined as the remainder of a division
  1473. that rounds the quotient towards minus infinity (floor division).
  1474. <p>
  1475. In case of overflows in integer arithmetic,
  1476. all operations <em>wrap around</em>,
  1477. according to the usual rules of two-complement arithmetic.
  1478. (In other words,
  1479. they return the unique representable integer
  1480. that is equal modulo <em>2<sup>64</sup></em> to the mathematical result.)
  1481. <h3>3.4.2 &ndash; <a name="3.4.2">Bitwise Operators</a></h3><p>
  1482. Lua supports the following bitwise operators:
  1483. <ul>
  1484. <li><b><code>&amp;</code>: </b>bitwise AND</li>
  1485. <li><b><code>&#124;</code>: </b>bitwise OR</li>
  1486. <li><b><code>~</code>: </b>bitwise exclusive OR</li>
  1487. <li><b><code>&gt;&gt;</code>: </b>right shift</li>
  1488. <li><b><code>&lt;&lt;</code>: </b>left shift</li>
  1489. <li><b><code>~</code>: </b>unary bitwise NOT</li>
  1490. </ul>
  1491. <p>
  1492. All bitwise operations convert its operands to integers
  1493. (see <a href="#3.4.3">&sect;3.4.3</a>),
  1494. operate on all bits of those integers,
  1495. and result in an integer.
  1496. <p>
  1497. Both right and left shifts fill the vacant bits with zeros.
  1498. Negative displacements shift to the other direction;
  1499. displacements with absolute values equal to or higher than
  1500. the number of bits in an integer
  1501. result in zero (as all bits are shifted out).
  1502. <h3>3.4.3 &ndash; <a name="3.4.3">Coercions and Conversions</a></h3><p>
  1503. Lua provides some automatic conversions between some
  1504. types and representations at run time.
  1505. Bitwise operators always convert float operands to integers.
  1506. Exponentiation and float division
  1507. always convert integer operands to floats.
  1508. All other arithmetic operations applied to mixed numbers
  1509. (integers and floats) convert the integer operand to a float;
  1510. this is called the <em>usual rule</em>.
  1511. The C API also converts both integers to floats and
  1512. floats to integers, as needed.
  1513. Moreover, string concatenation accepts numbers as arguments,
  1514. besides strings.
  1515. <p>
  1516. Lua also converts strings to numbers,
  1517. whenever a number is expected.
  1518. <p>
  1519. In a conversion from integer to float,
  1520. if the integer value has an exact representation as a float,
  1521. that is the result.
  1522. Otherwise,
  1523. the conversion gets the nearest higher or
  1524. the nearest lower representable value.
  1525. This kind of conversion never fails.
  1526. <p>
  1527. The conversion from float to integer
  1528. checks whether the float has an exact representation as an integer
  1529. (that is, the float has an integral value and
  1530. it is in the range of integer representation).
  1531. If it does, that representation is the result.
  1532. Otherwise, the conversion fails.
  1533. <p>
  1534. The conversion from strings to numbers goes as follows:
  1535. First, the string is converted to an integer or a float,
  1536. following its syntax and the rules of the Lua lexer.
  1537. (The string may have also leading and trailing spaces and a sign.)
  1538. Then, the resulting number (float or integer)
  1539. is converted to the type (float or integer) required by the context
  1540. (e.g., the operation that forced the conversion).
  1541. <p>
  1542. All conversions from strings to numbers
  1543. accept both a dot and the current locale mark
  1544. as the radix character.
  1545. (The Lua lexer, however, accepts only a dot.)
  1546. <p>
  1547. The conversion from numbers to strings uses a
  1548. non-specified human-readable format.
  1549. For complete control over how numbers are converted to strings,
  1550. use the <code>format</code> function from the string library
  1551. (see <a href="#pdf-string.format"><code>string.format</code></a>).
  1552. <h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
  1553. Lua supports the following relational operators:
  1554. <ul>
  1555. <li><b><code>==</code>: </b>equality</li>
  1556. <li><b><code>~=</code>: </b>inequality</li>
  1557. <li><b><code>&lt;</code>: </b>less than</li>
  1558. <li><b><code>&gt;</code>: </b>greater than</li>
  1559. <li><b><code>&lt;=</code>: </b>less or equal</li>
  1560. <li><b><code>&gt;=</code>: </b>greater or equal</li>
  1561. </ul><p>
  1562. These operators always result in <b>false</b> or <b>true</b>.
  1563. <p>
  1564. Equality (<code>==</code>) first compares the type of its operands.
  1565. If the types are different, then the result is <b>false</b>.
  1566. Otherwise, the values of the operands are compared.
  1567. Strings are compared in the obvious way.
  1568. Numbers are equal if they denote the same mathematical value.
  1569. <p>
  1570. Tables, userdata, and threads
  1571. are compared by reference:
  1572. two objects are considered equal only if they are the same object.
  1573. Every time you create a new object
  1574. (a table, userdata, or thread),
  1575. this new object is different from any previously existing object.
  1576. A closure is always equal to itself.
  1577. Closures with any detectable difference
  1578. (different behavior, different definition) are always different.
  1579. Closures created at different times but with no detectable differences
  1580. may be classified as equal or not
  1581. (depending on internal caching details).
  1582. <p>
  1583. You can change the way that Lua compares tables and userdata
  1584. by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
  1585. <p>
  1586. Equality comparisons do not convert strings to numbers
  1587. or vice versa.
  1588. Thus, <code>"0"==0</code> evaluates to <b>false</b>,
  1589. and <code>t[0]</code> and <code>t["0"]</code> denote different
  1590. entries in a table.
  1591. <p>
  1592. The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
  1593. <p>
  1594. The order operators work as follows.
  1595. If both arguments are numbers,
  1596. then they are compared according to their mathematical values
  1597. (regardless of their subtypes).
  1598. Otherwise, if both arguments are strings,
  1599. then their values are compared according to the current locale.
  1600. Otherwise, Lua tries to call the "lt" or the "le"
  1601. metamethod (see <a href="#2.4">&sect;2.4</a>).
  1602. A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
  1603. and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
  1604. <p>
  1605. Following the IEEE 754 standard,
  1606. NaN is considered neither smaller than,
  1607. nor equal to, nor greater than any value (including itself).
  1608. <h3>3.4.5 &ndash; <a name="3.4.5">Logical Operators</a></h3><p>
  1609. The logical operators in Lua are
  1610. <b>and</b>, <b>or</b>, and <b>not</b>.
  1611. Like the control structures (see <a href="#3.3.4">&sect;3.3.4</a>),
  1612. all logical operators consider both <b>false</b> and <b>nil</b> as false
  1613. and anything else as true.
  1614. <p>
  1615. The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
  1616. The conjunction operator <b>and</b> returns its first argument
  1617. if this value is <b>false</b> or <b>nil</b>;
  1618. otherwise, <b>and</b> returns its second argument.
  1619. The disjunction operator <b>or</b> returns its first argument
  1620. if this value is different from <b>nil</b> and <b>false</b>;
  1621. otherwise, <b>or</b> returns its second argument.
  1622. Both <b>and</b> and <b>or</b> use short-circuit evaluation;
  1623. that is,
  1624. the second operand is evaluated only if necessary.
  1625. Here are some examples:
  1626. <pre>
  1627. 10 or 20 --&gt; 10
  1628. 10 or error() --&gt; 10
  1629. nil or "a" --&gt; "a"
  1630. nil and 10 --&gt; nil
  1631. false and error() --&gt; false
  1632. false and nil --&gt; false
  1633. false or nil --&gt; nil
  1634. 10 and 20 --&gt; 20
  1635. </pre><p>
  1636. (In this manual,
  1637. <code>--&gt;</code> indicates the result of the preceding expression.)
  1638. <h3>3.4.6 &ndash; <a name="3.4.6">Concatenation</a></h3><p>
  1639. The string concatenation operator in Lua is
  1640. denoted by two dots ('<code>..</code>').
  1641. If both operands are strings or numbers, then they are converted to
  1642. strings according to the rules described in <a href="#3.4.3">&sect;3.4.3</a>.
  1643. Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>).
  1644. <h3>3.4.7 &ndash; <a name="3.4.7">The Length Operator</a></h3>
  1645. <p>
  1646. The length operator is denoted by the unary prefix operator <code>#</code>.
  1647. <p>
  1648. The length of a string is its number of bytes
  1649. (that is, the usual meaning of string length when each
  1650. character is one byte).
  1651. <p>
  1652. The length operator applied on a table
  1653. returns a border in that table.
  1654. A <em>border</em> in a table <code>t</code> is any natural number
  1655. that satisfies the following condition:
  1656. <pre>
  1657. (border == 0 or t[border] ~= nil) and t[border + 1] == nil
  1658. </pre><p>
  1659. In words,
  1660. a border is any (natural) index in a table
  1661. where a non-nil value is followed by a nil value
  1662. (or zero, when index 1 is nil).
  1663. <p>
  1664. A table with exactly one border is called a <em>sequence</em>.
  1665. For instance, the table <code>{10, 20, 30, 40, 50}</code> is a sequence,
  1666. as it has only one border (5).
  1667. The table <code>{10, 20, 30, nil, 50}</code> has two borders (3 and 5),
  1668. and therefore it is not a sequence.
  1669. The table <code>{nil, 20, 30, nil, nil, 60, nil}</code>
  1670. has three borders (0, 3, and 6),
  1671. so it is not a sequence, too.
  1672. The table <code>{}</code> is a sequence with border 0.
  1673. Note that non-natural keys do not interfere
  1674. with whether a table is a sequence.
  1675. <p>
  1676. When <code>t</code> is a sequence,
  1677. <code>#t</code> returns its only border,
  1678. which corresponds to the intuitive notion of the length of the sequence.
  1679. When <code>t</code> is not a sequence,
  1680. <code>#t</code> can return any of its borders.
  1681. (The exact one depends on details of
  1682. the internal representation of the table,
  1683. which in turn can depend on how the table was populated and
  1684. the memory addresses of its non-numeric keys.)
  1685. <p>
  1686. The computation of the length of a table
  1687. has a guaranteed worst time of <em>O(log n)</em>,
  1688. where <em>n</em> is the largest natural key in the table.
  1689. <p>
  1690. A program can modify the behavior of the length operator for
  1691. any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">&sect;2.4</a>).
  1692. <h3>3.4.8 &ndash; <a name="3.4.8">Precedence</a></h3><p>
  1693. Operator precedence in Lua follows the table below,
  1694. from lower to higher priority:
  1695. <pre>
  1696. or
  1697. and
  1698. &lt; &gt; &lt;= &gt;= ~= ==
  1699. |
  1700. ~
  1701. &amp;
  1702. &lt;&lt; &gt;&gt;
  1703. ..
  1704. + -
  1705. * / // %
  1706. unary operators (not # - ~)
  1707. ^
  1708. </pre><p>
  1709. As usual,
  1710. you can use parentheses to change the precedences of an expression.
  1711. The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
  1712. operators are right associative.
  1713. All other binary operators are left associative.
  1714. <h3>3.4.9 &ndash; <a name="3.4.9">Table Constructors</a></h3><p>
  1715. Table constructors are expressions that create tables.
  1716. Every time a constructor is evaluated, a new table is created.
  1717. A constructor can be used to create an empty table
  1718. or to create a table and initialize some of its fields.
  1719. The general syntax for constructors is
  1720. <pre>
  1721. tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
  1722. fieldlist ::= field {fieldsep field} [fieldsep]
  1723. field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
  1724. fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
  1725. </pre>
  1726. <p>
  1727. Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
  1728. with key <code>exp1</code> and value <code>exp2</code>.
  1729. A field of the form <code>name = exp</code> is equivalent to
  1730. <code>["name"] = exp</code>.
  1731. Finally, fields of the form <code>exp</code> are equivalent to
  1732. <code>[i] = exp</code>, where <code>i</code> are consecutive integers
  1733. starting with 1.
  1734. Fields in the other formats do not affect this counting.
  1735. For example,
  1736. <pre>
  1737. a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
  1738. </pre><p>
  1739. is equivalent to
  1740. <pre>
  1741. do
  1742. local t = {}
  1743. t[f(1)] = g
  1744. t[1] = "x" -- 1st exp
  1745. t[2] = "y" -- 2nd exp
  1746. t.x = 1 -- t["x"] = 1
  1747. t[3] = f(x) -- 3rd exp
  1748. t[30] = 23
  1749. t[4] = 45 -- 4th exp
  1750. a = t
  1751. end
  1752. </pre>
  1753. <p>
  1754. The order of the assignments in a constructor is undefined.
  1755. (This order would be relevant only when there are repeated keys.)
  1756. <p>
  1757. If the last field in the list has the form <code>exp</code>
  1758. and the expression is a function call or a vararg expression,
  1759. then all values returned by this expression enter the list consecutively
  1760. (see <a href="#3.4.10">&sect;3.4.10</a>).
  1761. <p>
  1762. The field list can have an optional trailing separator,
  1763. as a convenience for machine-generated code.
  1764. <h3>3.4.10 &ndash; <a name="3.4.10">Function Calls</a></h3><p>
  1765. A function call in Lua has the following syntax:
  1766. <pre>
  1767. functioncall ::= prefixexp args
  1768. </pre><p>
  1769. In a function call,
  1770. first prefixexp and args are evaluated.
  1771. If the value of prefixexp has type <em>function</em>,
  1772. then this function is called
  1773. with the given arguments.
  1774. Otherwise, the prefixexp "call" metamethod is called,
  1775. having as first argument the value of prefixexp,
  1776. followed by the original call arguments
  1777. (see <a href="#2.4">&sect;2.4</a>).
  1778. <p>
  1779. The form
  1780. <pre>
  1781. functioncall ::= prefixexp &lsquo;<b>:</b>&rsquo; Name args
  1782. </pre><p>
  1783. can be used to call "methods".
  1784. A call <code>v:name(<em>args</em>)</code>
  1785. is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
  1786. except that <code>v</code> is evaluated only once.
  1787. <p>
  1788. Arguments have the following syntax:
  1789. <pre>
  1790. args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo;
  1791. args ::= tableconstructor
  1792. args ::= LiteralString
  1793. </pre><p>
  1794. All argument expressions are evaluated before the call.
  1795. A call of the form <code>f{<em>fields</em>}</code> is
  1796. syntactic sugar for <code>f({<em>fields</em>})</code>;
  1797. that is, the argument list is a single new table.
  1798. A call of the form <code>f'<em>string</em>'</code>
  1799. (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)
  1800. is syntactic sugar for <code>f('<em>string</em>')</code>;
  1801. that is, the argument list is a single literal string.
  1802. <p>
  1803. A call of the form <code>return <em>functioncall</em></code> is called
  1804. a <em>tail call</em>.
  1805. Lua implements <em>proper tail calls</em>
  1806. (or <em>proper tail recursion</em>):
  1807. in a tail call,
  1808. the called function reuses the stack entry of the calling function.
  1809. Therefore, there is no limit on the number of nested tail calls that
  1810. a program can execute.
  1811. However, a tail call erases any debug information about the
  1812. calling function.
  1813. Note that a tail call only happens with a particular syntax,
  1814. where the <b>return</b> has one single function call as argument;
  1815. this syntax makes the calling function return exactly
  1816. the returns of the called function.
  1817. So, none of the following examples are tail calls:
  1818. <pre>
  1819. return (f(x)) -- results adjusted to 1
  1820. return 2 * f(x)
  1821. return x, f(x) -- additional results
  1822. f(x); return -- results discarded
  1823. return x or f(x) -- results adjusted to 1
  1824. </pre>
  1825. <h3>3.4.11 &ndash; <a name="3.4.11">Function Definitions</a></h3>
  1826. <p>
  1827. The syntax for function definition is
  1828. <pre>
  1829. functiondef ::= <b>function</b> funcbody
  1830. funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
  1831. </pre>
  1832. <p>
  1833. The following syntactic sugar simplifies function definitions:
  1834. <pre>
  1835. stat ::= <b>function</b> funcname funcbody
  1836. stat ::= <b>local</b> <b>function</b> Name funcbody
  1837. funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
  1838. </pre><p>
  1839. The statement
  1840. <pre>
  1841. function f () <em>body</em> end
  1842. </pre><p>
  1843. translates to
  1844. <pre>
  1845. f = function () <em>body</em> end
  1846. </pre><p>
  1847. The statement
  1848. <pre>
  1849. function t.a.b.c.f () <em>body</em> end
  1850. </pre><p>
  1851. translates to
  1852. <pre>
  1853. t.a.b.c.f = function () <em>body</em> end
  1854. </pre><p>
  1855. The statement
  1856. <pre>
  1857. local function f () <em>body</em> end
  1858. </pre><p>
  1859. translates to
  1860. <pre>
  1861. local f; f = function () <em>body</em> end
  1862. </pre><p>
  1863. not to
  1864. <pre>
  1865. local f = function () <em>body</em> end
  1866. </pre><p>
  1867. (This only makes a difference when the body of the function
  1868. contains references to <code>f</code>.)
  1869. <p>
  1870. A function definition is an executable expression,
  1871. whose value has type <em>function</em>.
  1872. When Lua precompiles a chunk,
  1873. all its function bodies are precompiled too.
  1874. Then, whenever Lua executes the function definition,
  1875. the function is <em>instantiated</em> (or <em>closed</em>).
  1876. This function instance (or <em>closure</em>)
  1877. is the final value of the expression.
  1878. <p>
  1879. Parameters act as local variables that are
  1880. initialized with the argument values:
  1881. <pre>
  1882. parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
  1883. </pre><p>
  1884. When a function is called,
  1885. the list of arguments is adjusted to
  1886. the length of the list of parameters,
  1887. unless the function is a <em>vararg function</em>,
  1888. which is indicated by three dots ('<code>...</code>')
  1889. at the end of its parameter list.
  1890. A vararg function does not adjust its argument list;
  1891. instead, it collects all extra arguments and supplies them
  1892. to the function through a <em>vararg expression</em>,
  1893. which is also written as three dots.
  1894. The value of this expression is a list of all actual extra arguments,
  1895. similar to a function with multiple results.
  1896. If a vararg expression is used inside another expression
  1897. or in the middle of a list of expressions,
  1898. then its return list is adjusted to one element.
  1899. If the expression is used as the last element of a list of expressions,
  1900. then no adjustment is made
  1901. (unless that last expression is enclosed in parentheses).
  1902. <p>
  1903. As an example, consider the following definitions:
  1904. <pre>
  1905. function f(a, b) end
  1906. function g(a, b, ...) end
  1907. function r() return 1,2,3 end
  1908. </pre><p>
  1909. Then, we have the following mapping from arguments to parameters and
  1910. to the vararg expression:
  1911. <pre>
  1912. CALL PARAMETERS
  1913. f(3) a=3, b=nil
  1914. f(3, 4) a=3, b=4
  1915. f(3, 4, 5) a=3, b=4
  1916. f(r(), 10) a=1, b=10
  1917. f(r()) a=1, b=2
  1918. g(3) a=3, b=nil, ... --&gt; (nothing)
  1919. g(3, 4) a=3, b=4, ... --&gt; (nothing)
  1920. g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8
  1921. g(5, r()) a=5, b=1, ... --&gt; 2 3
  1922. </pre>
  1923. <p>
  1924. Results are returned using the <b>return</b> statement (see <a href="#3.3.4">&sect;3.3.4</a>).
  1925. If control reaches the end of a function
  1926. without encountering a <b>return</b> statement,
  1927. then the function returns with no results.
  1928. <p>
  1929. There is a system-dependent limit on the number of values
  1930. that a function may return.
  1931. This limit is guaranteed to be larger than 1000.
  1932. <p>
  1933. The <em>colon</em> syntax
  1934. is used for defining <em>methods</em>,
  1935. that is, functions that have an implicit extra parameter <code>self</code>.
  1936. Thus, the statement
  1937. <pre>
  1938. function t.a.b.c:f (<em>params</em>) <em>body</em> end
  1939. </pre><p>
  1940. is syntactic sugar for
  1941. <pre>
  1942. t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
  1943. </pre>
  1944. <h2>3.5 &ndash; <a name="3.5">Visibility Rules</a></h2>
  1945. <p>
  1946. Lua is a lexically scoped language.
  1947. The scope of a local variable begins at the first statement after
  1948. its declaration and lasts until the last non-void statement
  1949. of the innermost block that includes the declaration.
  1950. Consider the following example:
  1951. <pre>
  1952. x = 10 -- global variable
  1953. do -- new block
  1954. local x = x -- new 'x', with value 10
  1955. print(x) --&gt; 10
  1956. x = x+1
  1957. do -- another block
  1958. local x = x+1 -- another 'x'
  1959. print(x) --&gt; 12
  1960. end
  1961. print(x) --&gt; 11
  1962. end
  1963. print(x) --&gt; 10 (the global one)
  1964. </pre>
  1965. <p>
  1966. Notice that, in a declaration like <code>local x = x</code>,
  1967. the new <code>x</code> being declared is not in scope yet,
  1968. and so the second <code>x</code> refers to the outside variable.
  1969. <p>
  1970. Because of the lexical scoping rules,
  1971. local variables can be freely accessed by functions
  1972. defined inside their scope.
  1973. A local variable used by an inner function is called
  1974. an <em>upvalue</em>, or <em>external local variable</em>,
  1975. inside the inner function.
  1976. <p>
  1977. Notice that each execution of a <b>local</b> statement
  1978. defines new local variables.
  1979. Consider the following example:
  1980. <pre>
  1981. a = {}
  1982. local x = 20
  1983. for i=1,10 do
  1984. local y = 0
  1985. a[i] = function () y=y+1; return x+y end
  1986. end
  1987. </pre><p>
  1988. The loop creates ten closures
  1989. (that is, ten instances of the anonymous function).
  1990. Each of these closures uses a different <code>y</code> variable,
  1991. while all of them share the same <code>x</code>.
  1992. <h1>4 &ndash; <a name="4">The Application Program Interface</a></h1>
  1993. <p>
  1994. This section describes the C&nbsp;API for Lua, that is,
  1995. the set of C&nbsp;functions available to the host program to communicate
  1996. with Lua.
  1997. All API functions and related types and constants
  1998. are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
  1999. <p>
  2000. Even when we use the term "function",
  2001. any facility in the API may be provided as a macro instead.
  2002. Except where stated otherwise,
  2003. all such macros use each of their arguments exactly once
  2004. (except for the first argument, which is always a Lua state),
  2005. and so do not generate any hidden side-effects.
  2006. <p>
  2007. As in most C&nbsp;libraries,
  2008. the Lua API functions do not check their arguments for validity or consistency.
  2009. However, you can change this behavior by compiling Lua
  2010. with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
  2011. <p>
  2012. The Lua library is fully reentrant:
  2013. it has no global variables.
  2014. It keeps all information it needs in a dynamic structure,
  2015. called the <em>Lua state</em>.
  2016. <p>
  2017. Each Lua state has one or more threads,
  2018. which correspond to independent, cooperative lines of execution.
  2019. The type <a href="#lua_State"><code>lua_State</code></a> (despite its name) refers to a thread.
  2020. (Indirectly, through the thread, it also refers to the
  2021. Lua state associated to the thread.)
  2022. <p>
  2023. A pointer to a thread must be passed as the first argument to
  2024. every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
  2025. which creates a Lua state from scratch and returns a pointer
  2026. to the <em>main thread</em> in the new state.
  2027. <h2>4.1 &ndash; <a name="4.1">The Stack</a></h2>
  2028. <p>
  2029. Lua uses a <em>virtual stack</em> to pass values to and from C.
  2030. Each element in this stack represents a Lua value
  2031. (<b>nil</b>, number, string, etc.).
  2032. Functions in the API can access this stack through the
  2033. Lua state parameter that they receive.
  2034. <p>
  2035. Whenever Lua calls C, the called function gets a new stack,
  2036. which is independent of previous stacks and of stacks of
  2037. C&nbsp;functions that are still active.
  2038. This stack initially contains any arguments to the C&nbsp;function
  2039. and it is where the C&nbsp;function can store temporary
  2040. Lua values and must push its results
  2041. to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  2042. <p>
  2043. For convenience,
  2044. most query operations in the API do not follow a strict stack discipline.
  2045. Instead, they can refer to any element in the stack
  2046. by using an <em>index</em>:
  2047. A positive index represents an absolute stack position
  2048. (starting at&nbsp;1);
  2049. a negative index represents an offset relative to the top of the stack.
  2050. More specifically, if the stack has <em>n</em> elements,
  2051. then index&nbsp;1 represents the first element
  2052. (that is, the element that was pushed onto the stack first)
  2053. and
  2054. index&nbsp;<em>n</em> represents the last element;
  2055. index&nbsp;-1 also represents the last element
  2056. (that is, the element at the&nbsp;top)
  2057. and index <em>-n</em> represents the first element.
  2058. <h2>4.2 &ndash; <a name="4.2">Stack Size</a></h2>
  2059. <p>
  2060. When you interact with the Lua API,
  2061. you are responsible for ensuring consistency.
  2062. In particular,
  2063. <em>you are responsible for controlling stack overflow</em>.
  2064. You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
  2065. to ensure that the stack has enough space for pushing new elements.
  2066. <p>
  2067. Whenever Lua calls C,
  2068. it ensures that the stack has space for
  2069. at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
  2070. <code>LUA_MINSTACK</code> is defined as 20,
  2071. so that usually you do not have to worry about stack space
  2072. unless your code has loops pushing elements onto the stack.
  2073. <p>
  2074. When you call a Lua function
  2075. without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
  2076. Lua ensures that the stack has enough space for all results,
  2077. but it does not ensure any extra space.
  2078. So, before pushing anything in the stack after such a call
  2079. you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
  2080. <h2>4.3 &ndash; <a name="4.3">Valid and Acceptable Indices</a></h2>
  2081. <p>
  2082. Any function in the API that receives stack indices
  2083. works only with <em>valid indices</em> or <em>acceptable indices</em>.
  2084. <p>
  2085. A <em>valid index</em> is an index that refers to a
  2086. position that stores a modifiable Lua value.
  2087. It comprises stack indices between&nbsp;1 and the stack top
  2088. (<code>1 &le; abs(index) &le; top</code>)
  2089. plus <em>pseudo-indices</em>,
  2090. which represent some positions that are accessible to C&nbsp;code
  2091. but that are not in the stack.
  2092. Pseudo-indices are used to access the registry (see <a href="#4.5">&sect;4.5</a>)
  2093. and the upvalues of a C&nbsp;function (see <a href="#4.4">&sect;4.4</a>).
  2094. <p>
  2095. Functions that do not need a specific mutable position,
  2096. but only a value (e.g., query functions),
  2097. can be called with acceptable indices.
  2098. An <em>acceptable index</em> can be any valid index,
  2099. but it also can be any positive index after the stack top
  2100. within the space allocated for the stack,
  2101. that is, indices up to the stack size.
  2102. (Note that 0 is never an acceptable index.)
  2103. Except when noted otherwise,
  2104. functions in the API work with acceptable indices.
  2105. <p>
  2106. Acceptable indices serve to avoid extra tests
  2107. against the stack top when querying the stack.
  2108. For instance, a C&nbsp;function can query its third argument
  2109. without the need to first check whether there is a third argument,
  2110. that is, without the need to check whether 3 is a valid index.
  2111. <p>
  2112. For functions that can be called with acceptable indices,
  2113. any non-valid index is treated as if it
  2114. contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
  2115. which behaves like a nil value.
  2116. <h2>4.4 &ndash; <a name="4.4">C Closures</a></h2>
  2117. <p>
  2118. When a C&nbsp;function is created,
  2119. it is possible to associate some values with it,
  2120. thus creating a <em>C&nbsp;closure</em>
  2121. (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
  2122. these values are called <em>upvalues</em> and are
  2123. accessible to the function whenever it is called.
  2124. <p>
  2125. Whenever a C&nbsp;function is called,
  2126. its upvalues are located at specific pseudo-indices.
  2127. These pseudo-indices are produced by the macro
  2128. <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
  2129. The first upvalue associated with a function is at index
  2130. <code>lua_upvalueindex(1)</code>, and so on.
  2131. Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
  2132. where <em>n</em> is greater than the number of upvalues of the
  2133. current function
  2134. (but not greater than 256,
  2135. which is one plus the maximum number of upvalues in a closure),
  2136. produces an acceptable but invalid index.
  2137. <h2>4.5 &ndash; <a name="4.5">Registry</a></h2>
  2138. <p>
  2139. Lua provides a <em>registry</em>,
  2140. a predefined table that can be used by any C&nbsp;code to
  2141. store whatever Lua values it needs to store.
  2142. The registry table is always located at pseudo-index
  2143. <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>.
  2144. Any C&nbsp;library can store data into this table,
  2145. but it must take care to choose keys
  2146. that are different from those used
  2147. by other libraries, to avoid collisions.
  2148. Typically, you should use as key a string containing your library name,
  2149. or a light userdata with the address of a C&nbsp;object in your code,
  2150. or any Lua object created by your code.
  2151. As with variable names,
  2152. string keys starting with an underscore followed by
  2153. uppercase letters are reserved for Lua.
  2154. <p>
  2155. The integer keys in the registry are used
  2156. by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>)
  2157. and by some predefined values.
  2158. Therefore, integer keys must not be used for other purposes.
  2159. <p>
  2160. When you create a new Lua state,
  2161. its registry comes with some predefined values.
  2162. These predefined values are indexed with integer keys
  2163. defined as constants in <code>lua.h</code>.
  2164. The following constants are defined:
  2165. <ul>
  2166. <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has
  2167. the main thread of the state.
  2168. (The main thread is the one created together with the state.)
  2169. </li>
  2170. <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has
  2171. the global environment.
  2172. </li>
  2173. </ul>
  2174. <h2>4.6 &ndash; <a name="4.6">Error Handling in C</a></h2>
  2175. <p>
  2176. Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
  2177. (Lua will use exceptions if you compile it as C++;
  2178. search for <code>LUAI_THROW</code> in the source code for details.)
  2179. When Lua faces any error
  2180. (such as a memory allocation error or a type error)
  2181. it <em>raises</em> an error;
  2182. that is, it does a long jump.
  2183. A <em>protected environment</em> uses <code>setjmp</code>
  2184. to set a recovery point;
  2185. any error jumps to the most recent active recovery point.
  2186. <p>
  2187. Inside a C&nbsp;function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>.
  2188. <p>
  2189. Most functions in the API can raise an error,
  2190. for instance due to a memory allocation error.
  2191. The documentation for each function indicates whether
  2192. it can raise errors.
  2193. <p>
  2194. If an error happens outside any protected environment,
  2195. Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
  2196. and then calls <code>abort</code>,
  2197. thus exiting the host application.
  2198. Your panic function can avoid this exit by
  2199. never returning
  2200. (e.g., doing a long jump to your own recovery point outside Lua).
  2201. <p>
  2202. The panic function,
  2203. as its name implies,
  2204. is a mechanism of last resort.
  2205. Programs should avoid it.
  2206. As a general rule,
  2207. when a C&nbsp;function is called by Lua with a Lua state,
  2208. it can do whatever it wants on that Lua state,
  2209. as it should be already protected.
  2210. However,
  2211. when C code operates on other Lua states
  2212. (e.g., a Lua argument to the function,
  2213. a Lua state stored in the registry, or
  2214. the result of <a href="#lua_newthread"><code>lua_newthread</code></a>),
  2215. it should use them only in API calls that cannot raise errors.
  2216. <p>
  2217. The panic function runs as if it were a message handler (see <a href="#2.3">&sect;2.3</a>);
  2218. in particular, the error object is at the top of the stack.
  2219. However, there is no guarantee about stack space.
  2220. To push anything on the stack,
  2221. the panic function must first check the available space (see <a href="#4.2">&sect;4.2</a>).
  2222. <h2>4.7 &ndash; <a name="4.7">Handling Yields in C</a></h2>
  2223. <p>
  2224. Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
  2225. Therefore, if a C&nbsp;function <code>foo</code> calls an API function
  2226. and this API function yields
  2227. (directly or indirectly by calling another function that yields),
  2228. Lua cannot return to <code>foo</code> any more,
  2229. because the <code>longjmp</code> removes its frame from the C stack.
  2230. <p>
  2231. To avoid this kind of problem,
  2232. Lua raises an error whenever it tries to yield across an API call,
  2233. except for three functions:
  2234. <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>.
  2235. All those functions receive a <em>continuation function</em>
  2236. (as a parameter named <code>k</code>) to continue execution after a yield.
  2237. <p>
  2238. We need to set some terminology to explain continuations.
  2239. We have a C&nbsp;function called from Lua which we will call
  2240. the <em>original function</em>.
  2241. This original function then calls one of those three functions in the C API,
  2242. which we will call the <em>callee function</em>,
  2243. that then yields the current thread.
  2244. (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  2245. 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>
  2246. and the function called by them yields.)
  2247. <p>
  2248. Suppose the running thread yields while executing the callee function.
  2249. After the thread resumes,
  2250. it eventually will finish running the callee function.
  2251. However,
  2252. the callee function cannot return to the original function,
  2253. because its frame in the C stack was destroyed by the yield.
  2254. Instead, Lua calls a <em>continuation function</em>,
  2255. which was given as an argument to the callee function.
  2256. As the name implies,
  2257. the continuation function should continue the task
  2258. of the original function.
  2259. <p>
  2260. As an illustration, consider the following function:
  2261. <pre>
  2262. int original_function (lua_State *L) {
  2263. ... /* code 1 */
  2264. status = lua_pcall(L, n, m, h); /* calls Lua */
  2265. ... /* code 2 */
  2266. }
  2267. </pre><p>
  2268. Now we want to allow
  2269. the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
  2270. First, we can rewrite our function like here:
  2271. <pre>
  2272. int k (lua_State *L, int status, lua_KContext ctx) {
  2273. ... /* code 2 */
  2274. }
  2275. int original_function (lua_State *L) {
  2276. ... /* code 1 */
  2277. return k(L, lua_pcall(L, n, m, h), ctx);
  2278. }
  2279. </pre><p>
  2280. In the above code,
  2281. the new function <code>k</code> is a
  2282. <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
  2283. which should do all the work that the original function
  2284. was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
  2285. Now, we must inform Lua that it must call <code>k</code> if the Lua code
  2286. being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
  2287. (errors or yielding),
  2288. so we rewrite the code as here,
  2289. replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>:
  2290. <pre>
  2291. int original_function (lua_State *L) {
  2292. ... /* code 1 */
  2293. return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
  2294. }
  2295. </pre><p>
  2296. Note the external, explicit call to the continuation:
  2297. Lua will call the continuation only if needed, that is,
  2298. in case of errors or resuming after a yield.
  2299. If the called function returns normally without ever yielding,
  2300. <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally.
  2301. (Of course, instead of calling the continuation in that case,
  2302. you can do the equivalent work directly inside the original function.)
  2303. <p>
  2304. Besides the Lua state,
  2305. the continuation function has two other parameters:
  2306. the final status of the call plus the context value (<code>ctx</code>) that
  2307. was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
  2308. (Lua does not use this context value;
  2309. it only passes this value from the original function to the
  2310. continuation function.)
  2311. For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
  2312. the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
  2313. except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield
  2314. (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
  2315. For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
  2316. the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
  2317. (For these two functions,
  2318. Lua will not call the continuation in case of errors,
  2319. because they do not handle errors.)
  2320. Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
  2321. you should call the continuation function
  2322. with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
  2323. (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
  2324. directly the continuation function,
  2325. because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
  2326. <p>
  2327. Lua treats the continuation function as if it were the original function.
  2328. The continuation function receives the same Lua stack
  2329. from the original function,
  2330. in the same state it would be if the callee function had returned.
  2331. (For instance,
  2332. after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
  2333. removed from the stack and replaced by the results from the call.)
  2334. It also has the same upvalues.
  2335. Whatever it returns is handled by Lua as if it were the return
  2336. of the original function.
  2337. <h2>4.8 &ndash; <a name="4.8">Functions and Types</a></h2>
  2338. <p>
  2339. Here we list all functions and types from the C&nbsp;API in
  2340. alphabetical order.
  2341. Each function has an indicator like this:
  2342. <span class="apii">[-o, +p, <em>x</em>]</span>
  2343. <p>
  2344. The first field, <code>o</code>,
  2345. is how many elements the function pops from the stack.
  2346. The second field, <code>p</code>,
  2347. is how many elements the function pushes onto the stack.
  2348. (Any function always pushes its results after popping its arguments.)
  2349. A field in the form <code>x|y</code> means the function can push (or pop)
  2350. <code>x</code> or <code>y</code> elements,
  2351. depending on the situation;
  2352. an interrogation mark '<code>?</code>' means that
  2353. we cannot know how many elements the function pops/pushes
  2354. by looking only at its arguments
  2355. (e.g., they may depend on what is on the stack).
  2356. The third field, <code>x</code>,
  2357. tells whether the function may raise errors:
  2358. '<code>-</code>' means the function never raises any error;
  2359. '<code>m</code>' means the function may raise out-of-memory errors
  2360. and errors running a <code>__gc</code> metamethod;
  2361. '<code>e</code>' means the function may raise any errors
  2362. (it can run arbitrary Lua code,
  2363. either directly or through metamethods);
  2364. '<code>v</code>' means the function may raise an error on purpose.
  2365. <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
  2366. <span class="apii">[-0, +0, &ndash;]</span>
  2367. <pre>int lua_absindex (lua_State *L, int idx);</pre>
  2368. <p>
  2369. Converts the acceptable index <code>idx</code>
  2370. into an equivalent absolute index
  2371. (that is, one that does not depend on the stack top).
  2372. <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
  2373. <pre>typedef void * (*lua_Alloc) (void *ud,
  2374. void *ptr,
  2375. size_t osize,
  2376. size_t nsize);</pre>
  2377. <p>
  2378. The type of the memory-allocation function used by Lua states.
  2379. The allocator function must provide a
  2380. functionality similar to <code>realloc</code>,
  2381. but not exactly the same.
  2382. Its arguments are
  2383. <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
  2384. <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
  2385. <code>osize</code>, the original size of the block or some code about what
  2386. is being allocated;
  2387. and <code>nsize</code>, the new size of the block.
  2388. <p>
  2389. When <code>ptr</code> is not <code>NULL</code>,
  2390. <code>osize</code> is the size of the block pointed by <code>ptr</code>,
  2391. that is, the size given when it was allocated or reallocated.
  2392. <p>
  2393. When <code>ptr</code> is <code>NULL</code>,
  2394. <code>osize</code> encodes the kind of object that Lua is allocating.
  2395. <code>osize</code> is any of
  2396. <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>,
  2397. <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)
  2398. Lua is creating a new object of that type.
  2399. When <code>osize</code> is some other value,
  2400. Lua is allocating memory for something else.
  2401. <p>
  2402. Lua assumes the following behavior from the allocator function:
  2403. <p>
  2404. When <code>nsize</code> is zero,
  2405. the allocator must behave like <code>free</code>
  2406. and return <code>NULL</code>.
  2407. <p>
  2408. When <code>nsize</code> is not zero,
  2409. the allocator must behave like <code>realloc</code>.
  2410. The allocator returns <code>NULL</code>
  2411. if and only if it cannot fulfill the request.
  2412. Lua assumes that the allocator never fails when
  2413. <code>osize &gt;= nsize</code>.
  2414. <p>
  2415. Here is a simple implementation for the allocator function.
  2416. It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
  2417. <pre>
  2418. static void *l_alloc (void *ud, void *ptr, size_t osize,
  2419. size_t nsize) {
  2420. (void)ud; (void)osize; /* not used */
  2421. if (nsize == 0) {
  2422. free(ptr);
  2423. return NULL;
  2424. }
  2425. else
  2426. return realloc(ptr, nsize);
  2427. }
  2428. </pre><p>
  2429. Note that Standard&nbsp;C ensures
  2430. that <code>free(NULL)</code> has no effect and that
  2431. <code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>.
  2432. This code assumes that <code>realloc</code> does not fail when shrinking a block.
  2433. (Although Standard&nbsp;C does not ensure this behavior,
  2434. it seems to be a safe assumption.)
  2435. <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
  2436. <span class="apii">[-(2|1), +1, <em>e</em>]</span>
  2437. <pre>void lua_arith (lua_State *L, int op);</pre>
  2438. <p>
  2439. Performs an arithmetic or bitwise operation over the two values
  2440. (or one, in the case of negations)
  2441. at the top of the stack,
  2442. with the value at the top being the second operand,
  2443. pops these values, and pushes the result of the operation.
  2444. The function follows the semantics of the corresponding Lua operator
  2445. (that is, it may call metamethods).
  2446. <p>
  2447. The value of <code>op</code> must be one of the following constants:
  2448. <ul>
  2449. <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
  2450. <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
  2451. <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
  2452. <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li>
  2453. <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li>
  2454. <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
  2455. <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
  2456. <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li>
  2457. <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise NOT (<code>~</code>)</li>
  2458. <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise AND (<code>&amp;</code>)</li>
  2459. <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise OR (<code>|</code>)</li>
  2460. <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive OR (<code>~</code>)</li>
  2461. <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code>&lt;&lt;</code>)</li>
  2462. <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>&gt;&gt;</code>)</li>
  2463. </ul>
  2464. <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
  2465. <span class="apii">[-0, +0, &ndash;]</span>
  2466. <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
  2467. <p>
  2468. Sets a new panic function and returns the old one (see <a href="#4.6">&sect;4.6</a>).
  2469. <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
  2470. <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
  2471. <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
  2472. <p>
  2473. Calls a function.
  2474. <p>
  2475. To call a function you must use the following protocol:
  2476. first, the function to be called is pushed onto the stack;
  2477. then, the arguments to the function are pushed
  2478. in direct order;
  2479. that is, the first argument is pushed first.
  2480. Finally you call <a href="#lua_call"><code>lua_call</code></a>;
  2481. <code>nargs</code> is the number of arguments that you pushed onto the stack.
  2482. All arguments and the function value are popped from the stack
  2483. when the function is called.
  2484. The function results are pushed onto the stack when the function returns.
  2485. The number of results is adjusted to <code>nresults</code>,
  2486. unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
  2487. In this case, all results from the function are pushed;
  2488. Lua takes care that the returned values fit into the stack space,
  2489. but it does not ensure any extra space in the stack.
  2490. The function results are pushed onto the stack in direct order
  2491. (the first result is pushed first),
  2492. so that after the call the last result is on the top of the stack.
  2493. <p>
  2494. Any error inside the called function is propagated upwards
  2495. (with a <code>longjmp</code>).
  2496. <p>
  2497. The following example shows how the host program can do the
  2498. equivalent to this Lua code:
  2499. <pre>
  2500. a = f("how", t.x, 14)
  2501. </pre><p>
  2502. Here it is in&nbsp;C:
  2503. <pre>
  2504. lua_getglobal(L, "f"); /* function to be called */
  2505. lua_pushliteral(L, "how"); /* 1st argument */
  2506. lua_getglobal(L, "t"); /* table to be indexed */
  2507. lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
  2508. lua_remove(L, -2); /* remove 't' from the stack */
  2509. lua_pushinteger(L, 14); /* 3rd argument */
  2510. lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
  2511. lua_setglobal(L, "a"); /* set global 'a' */
  2512. </pre><p>
  2513. Note that the code above is <em>balanced</em>:
  2514. at its end, the stack is back to its original configuration.
  2515. This is considered good programming practice.
  2516. <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
  2517. <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
  2518. <pre>void lua_callk (lua_State *L,
  2519. int nargs,
  2520. int nresults,
  2521. lua_KContext ctx,
  2522. lua_KFunction k);</pre>
  2523. <p>
  2524. This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
  2525. but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
  2526. <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
  2527. <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
  2528. <p>
  2529. Type for C&nbsp;functions.
  2530. <p>
  2531. In order to communicate properly with Lua,
  2532. a C&nbsp;function must use the following protocol,
  2533. which defines the way parameters and results are passed:
  2534. a C&nbsp;function receives its arguments from Lua in its stack
  2535. in direct order (the first argument is pushed first).
  2536. So, when the function starts,
  2537. <code>lua_gettop(L)</code> returns the number of arguments received by the function.
  2538. The first argument (if any) is at index 1
  2539. and its last argument is at index <code>lua_gettop(L)</code>.
  2540. To return values to Lua, a C&nbsp;function just pushes them onto the stack,
  2541. in direct order (the first result is pushed first),
  2542. and returns the number of results.
  2543. Any other value in the stack below the results will be properly
  2544. discarded by Lua.
  2545. Like a Lua function, a C&nbsp;function called by Lua can also return
  2546. many results.
  2547. <p>
  2548. As an example, the following function receives a variable number
  2549. of numeric arguments and returns their average and their sum:
  2550. <pre>
  2551. static int foo (lua_State *L) {
  2552. int n = lua_gettop(L); /* number of arguments */
  2553. lua_Number sum = 0.0;
  2554. int i;
  2555. for (i = 1; i &lt;= n; i++) {
  2556. if (!lua_isnumber(L, i)) {
  2557. lua_pushliteral(L, "incorrect argument");
  2558. lua_error(L);
  2559. }
  2560. sum += lua_tonumber(L, i);
  2561. }
  2562. lua_pushnumber(L, sum/n); /* first result */
  2563. lua_pushnumber(L, sum); /* second result */
  2564. return 2; /* number of results */
  2565. }
  2566. </pre>
  2567. <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
  2568. <span class="apii">[-0, +0, &ndash;]</span>
  2569. <pre>int lua_checkstack (lua_State *L, int n);</pre>
  2570. <p>
  2571. Ensures that the stack has space for at least <code>n</code> extra slots
  2572. (that is, that you can safely push up to <code>n</code> values into it).
  2573. It returns false if it cannot fulfill the request,
  2574. either because it would cause the stack
  2575. to be larger than a fixed maximum size
  2576. (typically at least several thousand elements) or
  2577. because it cannot allocate memory for the extra space.
  2578. This function never shrinks the stack;
  2579. if the stack already has space for the extra slots,
  2580. it is left unchanged.
  2581. <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
  2582. <span class="apii">[-0, +0, &ndash;]</span>
  2583. <pre>void lua_close (lua_State *L);</pre>
  2584. <p>
  2585. Destroys all objects in the given Lua state
  2586. (calling the corresponding garbage-collection metamethods, if any)
  2587. and frees all dynamic memory used by this state.
  2588. In several platforms, you may not need to call this function,
  2589. because all resources are naturally released when the host program ends.
  2590. On the other hand, long-running programs that create multiple states,
  2591. such as daemons or web servers,
  2592. will probably need to close states as soon as they are not needed.
  2593. <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
  2594. <span class="apii">[-0, +0, <em>e</em>]</span>
  2595. <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre>
  2596. <p>
  2597. Compares two Lua values.
  2598. Returns 1 if the value at index <code>index1</code> satisfies <code>op</code>
  2599. when compared with the value at index <code>index2</code>,
  2600. following the semantics of the corresponding Lua operator
  2601. (that is, it may call metamethods).
  2602. Otherwise returns&nbsp;0.
  2603. Also returns&nbsp;0 if any of the indices is not valid.
  2604. <p>
  2605. The value of <code>op</code> must be one of the following constants:
  2606. <ul>
  2607. <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li>
  2608. <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code>&lt;</code>)</li>
  2609. <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code>&lt;=</code>)</li>
  2610. </ul>
  2611. <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
  2612. <span class="apii">[-n, +1, <em>e</em>]</span>
  2613. <pre>void lua_concat (lua_State *L, int n);</pre>
  2614. <p>
  2615. Concatenates the <code>n</code> values at the top of the stack,
  2616. pops them, and leaves the result at the top.
  2617. If <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack
  2618. (that is, the function does nothing);
  2619. if <code>n</code> is 0, the result is the empty string.
  2620. Concatenation is performed following the usual semantics of Lua
  2621. (see <a href="#3.4.6">&sect;3.4.6</a>).
  2622. <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
  2623. <span class="apii">[-0, +0, &ndash;]</span>
  2624. <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
  2625. <p>
  2626. Copies the element at index <code>fromidx</code>
  2627. into the valid index <code>toidx</code>,
  2628. replacing the value at that position.
  2629. Values at other positions are not affected.
  2630. <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
  2631. <span class="apii">[-0, +1, <em>m</em>]</span>
  2632. <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
  2633. <p>
  2634. Creates a new empty table and pushes it onto the stack.
  2635. Parameter <code>narr</code> is a hint for how many elements the table
  2636. will have as a sequence;
  2637. parameter <code>nrec</code> is a hint for how many other elements
  2638. the table will have.
  2639. Lua may use these hints to preallocate memory for the new table.
  2640. This preallocation is useful for performance when you know in advance
  2641. how many elements the table will have.
  2642. Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
  2643. <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
  2644. <span class="apii">[-0, +0, &ndash;]</span>
  2645. <pre>int lua_dump (lua_State *L,
  2646. lua_Writer writer,
  2647. void *data,
  2648. int strip);</pre>
  2649. <p>
  2650. Dumps a function as a binary chunk.
  2651. Receives a Lua function on the top of the stack
  2652. and produces a binary chunk that,
  2653. if loaded again,
  2654. results in a function equivalent to the one dumped.
  2655. As it produces parts of the chunk,
  2656. <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
  2657. with the given <code>data</code>
  2658. to write them.
  2659. <p>
  2660. If <code>strip</code> is true,
  2661. the binary representation may not include all debug information
  2662. about the function,
  2663. to save space.
  2664. <p>
  2665. The value returned is the error code returned by the last
  2666. call to the writer;
  2667. 0&nbsp;means no errors.
  2668. <p>
  2669. This function does not pop the Lua function from the stack.
  2670. <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
  2671. <span class="apii">[-1, +0, <em>v</em>]</span>
  2672. <pre>int lua_error (lua_State *L);</pre>
  2673. <p>
  2674. Generates a Lua error,
  2675. using the value at the top of the stack as the error object.
  2676. This function does a long jump,
  2677. and therefore never returns
  2678. (see <a href="#luaL_error"><code>luaL_error</code></a>).
  2679. <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
  2680. <span class="apii">[-0, +0, <em>m</em>]</span>
  2681. <pre>int lua_gc (lua_State *L, int what, int data);</pre>
  2682. <p>
  2683. Controls the garbage collector.
  2684. <p>
  2685. This function performs several tasks,
  2686. according to the value of the parameter <code>what</code>:
  2687. <ul>
  2688. <li><b><code>LUA_GCSTOP</code>: </b>
  2689. stops the garbage collector.
  2690. </li>
  2691. <li><b><code>LUA_GCRESTART</code>: </b>
  2692. restarts the garbage collector.
  2693. </li>
  2694. <li><b><code>LUA_GCCOLLECT</code>: </b>
  2695. performs a full garbage-collection cycle.
  2696. </li>
  2697. <li><b><code>LUA_GCCOUNT</code>: </b>
  2698. returns the current amount of memory (in Kbytes) in use by Lua.
  2699. </li>
  2700. <li><b><code>LUA_GCCOUNTB</code>: </b>
  2701. returns the remainder of dividing the current amount of bytes of
  2702. memory in use by Lua by 1024.
  2703. </li>
  2704. <li><b><code>LUA_GCSTEP</code>: </b>
  2705. performs an incremental step of garbage collection.
  2706. </li>
  2707. <li><b><code>LUA_GCSETPAUSE</code>: </b>
  2708. sets <code>data</code> as the new value
  2709. for the <em>pause</em> of the collector (see <a href="#2.5">&sect;2.5</a>)
  2710. and returns the previous value of the pause.
  2711. </li>
  2712. <li><b><code>LUA_GCSETSTEPMUL</code>: </b>
  2713. sets <code>data</code> as the new value for the <em>step multiplier</em> of
  2714. the collector (see <a href="#2.5">&sect;2.5</a>)
  2715. and returns the previous value of the step multiplier.
  2716. </li>
  2717. <li><b><code>LUA_GCISRUNNING</code>: </b>
  2718. returns a boolean that tells whether the collector is running
  2719. (i.e., not stopped).
  2720. </li>
  2721. </ul>
  2722. <p>
  2723. For more details about these options,
  2724. see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
  2725. <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
  2726. <span class="apii">[-0, +0, &ndash;]</span>
  2727. <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
  2728. <p>
  2729. Returns the memory-allocation function of a given state.
  2730. If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
  2731. opaque pointer given when the memory-allocator function was set.
  2732. <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
  2733. <span class="apii">[-0, +1, <em>e</em>]</span>
  2734. <pre>int lua_getfield (lua_State *L, int index, const char *k);</pre>
  2735. <p>
  2736. Pushes onto the stack the value <code>t[k]</code>,
  2737. where <code>t</code> is the value at the given index.
  2738. As in Lua, this function may trigger a metamethod
  2739. for the "index" event (see <a href="#2.4">&sect;2.4</a>).
  2740. <p>
  2741. Returns the type of the pushed value.
  2742. <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
  2743. <span class="apii">[-0, +0, &ndash;]</span>
  2744. <pre>void *lua_getextraspace (lua_State *L);</pre>
  2745. <p>
  2746. Returns a pointer to a raw memory area associated with the
  2747. given Lua state.
  2748. The application can use this area for any purpose;
  2749. Lua does not use it for anything.
  2750. <p>
  2751. Each new thread has this area initialized with a copy
  2752. of the area of the main thread.
  2753. <p>
  2754. By default, this area has the size of a pointer to void,
  2755. but you can recompile Lua with a different size for this area.
  2756. (See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.)
  2757. <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
  2758. <span class="apii">[-0, +1, <em>e</em>]</span>
  2759. <pre>int lua_getglobal (lua_State *L, const char *name);</pre>
  2760. <p>
  2761. Pushes onto the stack the value of the global <code>name</code>.
  2762. Returns the type of that value.
  2763. <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p>
  2764. <span class="apii">[-0, +1, <em>e</em>]</span>
  2765. <pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre>
  2766. <p>
  2767. Pushes onto the stack the value <code>t[i]</code>,
  2768. where <code>t</code> is the value at the given index.
  2769. As in Lua, this function may trigger a metamethod
  2770. for the "index" event (see <a href="#2.4">&sect;2.4</a>).
  2771. <p>
  2772. Returns the type of the pushed value.
  2773. <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
  2774. <span class="apii">[-0, +(0|1), &ndash;]</span>
  2775. <pre>int lua_getmetatable (lua_State *L, int index);</pre>
  2776. <p>
  2777. If the value at the given index has a metatable,
  2778. the function pushes that metatable onto the stack and returns&nbsp;1.
  2779. Otherwise,
  2780. the function returns&nbsp;0 and pushes nothing on the stack.
  2781. <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
  2782. <span class="apii">[-1, +1, <em>e</em>]</span>
  2783. <pre>int lua_gettable (lua_State *L, int index);</pre>
  2784. <p>
  2785. Pushes onto the stack the value <code>t[k]</code>,
  2786. where <code>t</code> is the value at the given index
  2787. and <code>k</code> is the value at the top of the stack.
  2788. <p>
  2789. This function pops the key from the stack,
  2790. pushing the resulting value in its place.
  2791. As in Lua, this function may trigger a metamethod
  2792. for the "index" event (see <a href="#2.4">&sect;2.4</a>).
  2793. <p>
  2794. Returns the type of the pushed value.
  2795. <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
  2796. <span class="apii">[-0, +0, &ndash;]</span>
  2797. <pre>int lua_gettop (lua_State *L);</pre>
  2798. <p>
  2799. Returns the index of the top element in the stack.
  2800. Because indices start at&nbsp;1,
  2801. this result is equal to the number of elements in the stack;
  2802. in particular, 0&nbsp;means an empty stack.
  2803. <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
  2804. <span class="apii">[-0, +1, &ndash;]</span>
  2805. <pre>int lua_getuservalue (lua_State *L, int index);</pre>
  2806. <p>
  2807. Pushes onto the stack the Lua value associated with the full userdata
  2808. at the given index.
  2809. <p>
  2810. Returns the type of the pushed value.
  2811. <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
  2812. <span class="apii">[-1, +1, &ndash;]</span>
  2813. <pre>void lua_insert (lua_State *L, int index);</pre>
  2814. <p>
  2815. Moves the top element into the given valid index,
  2816. shifting up the elements above this index to open space.
  2817. This function cannot be called with a pseudo-index,
  2818. because a pseudo-index is not an actual stack position.
  2819. <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
  2820. <pre>typedef ... lua_Integer;</pre>
  2821. <p>
  2822. The type of integers in Lua.
  2823. <p>
  2824. By default this type is <code>long long</code>,
  2825. (usually a 64-bit two-complement integer),
  2826. but that can be changed to <code>long</code> or <code>int</code>
  2827. (usually a 32-bit two-complement integer).
  2828. (See <code>LUA_INT_TYPE</code> in <code>luaconf.h</code>.)
  2829. <p>
  2830. Lua also defines the constants
  2831. <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>,
  2832. with the minimum and the maximum values that fit in this type.
  2833. <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
  2834. <span class="apii">[-0, +0, &ndash;]</span>
  2835. <pre>int lua_isboolean (lua_State *L, int index);</pre>
  2836. <p>
  2837. Returns 1 if the value at the given index is a boolean,
  2838. and 0&nbsp;otherwise.
  2839. <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
  2840. <span class="apii">[-0, +0, &ndash;]</span>
  2841. <pre>int lua_iscfunction (lua_State *L, int index);</pre>
  2842. <p>
  2843. Returns 1 if the value at the given index is a C&nbsp;function,
  2844. and 0&nbsp;otherwise.
  2845. <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
  2846. <span class="apii">[-0, +0, &ndash;]</span>
  2847. <pre>int lua_isfunction (lua_State *L, int index);</pre>
  2848. <p>
  2849. Returns 1 if the value at the given index is a function
  2850. (either C or Lua), and 0&nbsp;otherwise.
  2851. <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p>
  2852. <span class="apii">[-0, +0, &ndash;]</span>
  2853. <pre>int lua_isinteger (lua_State *L, int index);</pre>
  2854. <p>
  2855. Returns 1 if the value at the given index is an integer
  2856. (that is, the value is a number and is represented as an integer),
  2857. and 0&nbsp;otherwise.
  2858. <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
  2859. <span class="apii">[-0, +0, &ndash;]</span>
  2860. <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
  2861. <p>
  2862. Returns 1 if the value at the given index is a light userdata,
  2863. and 0&nbsp;otherwise.
  2864. <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
  2865. <span class="apii">[-0, +0, &ndash;]</span>
  2866. <pre>int lua_isnil (lua_State *L, int index);</pre>
  2867. <p>
  2868. Returns 1 if the value at the given index is <b>nil</b>,
  2869. and 0&nbsp;otherwise.
  2870. <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
  2871. <span class="apii">[-0, +0, &ndash;]</span>
  2872. <pre>int lua_isnone (lua_State *L, int index);</pre>
  2873. <p>
  2874. Returns 1 if the given index is not valid,
  2875. and 0&nbsp;otherwise.
  2876. <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
  2877. <span class="apii">[-0, +0, &ndash;]</span>
  2878. <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
  2879. <p>
  2880. Returns 1 if the given index is not valid
  2881. or if the value at this index is <b>nil</b>,
  2882. and 0&nbsp;otherwise.
  2883. <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
  2884. <span class="apii">[-0, +0, &ndash;]</span>
  2885. <pre>int lua_isnumber (lua_State *L, int index);</pre>
  2886. <p>
  2887. Returns 1 if the value at the given index is a number
  2888. or a string convertible to a number,
  2889. and 0&nbsp;otherwise.
  2890. <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
  2891. <span class="apii">[-0, +0, &ndash;]</span>
  2892. <pre>int lua_isstring (lua_State *L, int index);</pre>
  2893. <p>
  2894. Returns 1 if the value at the given index is a string
  2895. or a number (which is always convertible to a string),
  2896. and 0&nbsp;otherwise.
  2897. <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
  2898. <span class="apii">[-0, +0, &ndash;]</span>
  2899. <pre>int lua_istable (lua_State *L, int index);</pre>
  2900. <p>
  2901. Returns 1 if the value at the given index is a table,
  2902. and 0&nbsp;otherwise.
  2903. <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
  2904. <span class="apii">[-0, +0, &ndash;]</span>
  2905. <pre>int lua_isthread (lua_State *L, int index);</pre>
  2906. <p>
  2907. Returns 1 if the value at the given index is a thread,
  2908. and 0&nbsp;otherwise.
  2909. <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
  2910. <span class="apii">[-0, +0, &ndash;]</span>
  2911. <pre>int lua_isuserdata (lua_State *L, int index);</pre>
  2912. <p>
  2913. Returns 1 if the value at the given index is a userdata
  2914. (either full or light), and 0&nbsp;otherwise.
  2915. <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
  2916. <span class="apii">[-0, +0, &ndash;]</span>
  2917. <pre>int lua_isyieldable (lua_State *L);</pre>
  2918. <p>
  2919. Returns 1 if the given coroutine can yield,
  2920. and 0&nbsp;otherwise.
  2921. <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3>
  2922. <pre>typedef ... lua_KContext;</pre>
  2923. <p>
  2924. The type for continuation-function contexts.
  2925. It must be a numeric type.
  2926. This type is defined as <code>intptr_t</code>
  2927. when <code>intptr_t</code> is available,
  2928. so that it can store pointers too.
  2929. Otherwise, it is defined as <code>ptrdiff_t</code>.
  2930. <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
  2931. <pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre>
  2932. <p>
  2933. Type for continuation functions (see <a href="#4.7">&sect;4.7</a>).
  2934. <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
  2935. <span class="apii">[-0, +1, <em>e</em>]</span>
  2936. <pre>void lua_len (lua_State *L, int index);</pre>
  2937. <p>
  2938. Returns the length of the value at the given index.
  2939. It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>) and
  2940. may trigger a metamethod for the "length" event (see <a href="#2.4">&sect;2.4</a>).
  2941. The result is pushed on the stack.
  2942. <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
  2943. <span class="apii">[-0, +1, &ndash;]</span>
  2944. <pre>int lua_load (lua_State *L,
  2945. lua_Reader reader,
  2946. void *data,
  2947. const char *chunkname,
  2948. const char *mode);</pre>
  2949. <p>
  2950. Loads a Lua chunk without running it.
  2951. If there are no errors,
  2952. <code>lua_load</code> pushes the compiled chunk as a Lua
  2953. function on top of the stack.
  2954. Otherwise, it pushes an error message.
  2955. <p>
  2956. The return values of <code>lua_load</code> are:
  2957. <ul>
  2958. <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
  2959. <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
  2960. syntax error during precompilation;</li>
  2961. <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
  2962. memory allocation (out-of-memory) error;</li>
  2963. <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
  2964. error while running a <code>__gc</code> metamethod.
  2965. (This error has no relation with the chunk being loaded.
  2966. It is generated by the garbage collector.)
  2967. </li>
  2968. </ul>
  2969. <p>
  2970. The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
  2971. to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
  2972. The <code>data</code> argument is an opaque value passed to the reader function.
  2973. <p>
  2974. The <code>chunkname</code> argument gives a name to the chunk,
  2975. which is used for error messages and in debug information (see <a href="#4.9">&sect;4.9</a>).
  2976. <p>
  2977. <code>lua_load</code> automatically detects whether the chunk is text or binary
  2978. and loads it accordingly (see program <code>luac</code>).
  2979. The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
  2980. with the addition that
  2981. a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
  2982. <p>
  2983. <code>lua_load</code> uses the stack internally,
  2984. so the reader function must always leave the stack
  2985. unmodified when returning.
  2986. <p>
  2987. If the resulting function has upvalues,
  2988. its first upvalue is set to the value of the global environment
  2989. stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">&sect;4.5</a>).
  2990. When loading main chunks,
  2991. this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
  2992. Other upvalues are initialized with <b>nil</b>.
  2993. <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
  2994. <span class="apii">[-0, +0, &ndash;]</span>
  2995. <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
  2996. <p>
  2997. Creates a new thread running in a new, independent state.
  2998. Returns <code>NULL</code> if it cannot create the thread or the state
  2999. (due to lack of memory).
  3000. The argument <code>f</code> is the allocator function;
  3001. Lua does all memory allocation for this state
  3002. through this function (see <a href="#lua_Alloc"><code>lua_Alloc</code></a>).
  3003. The second argument, <code>ud</code>, is an opaque pointer that Lua
  3004. passes to the allocator in every call.
  3005. <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
  3006. <span class="apii">[-0, +1, <em>m</em>]</span>
  3007. <pre>void lua_newtable (lua_State *L);</pre>
  3008. <p>
  3009. Creates a new empty table and pushes it onto the stack.
  3010. It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
  3011. <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
  3012. <span class="apii">[-0, +1, <em>m</em>]</span>
  3013. <pre>lua_State *lua_newthread (lua_State *L);</pre>
  3014. <p>
  3015. Creates a new thread, pushes it on the stack,
  3016. and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
  3017. The new thread returned by this function shares with the original thread
  3018. its global environment,
  3019. but has an independent execution stack.
  3020. <p>
  3021. There is no explicit function to close or to destroy a thread.
  3022. Threads are subject to garbage collection,
  3023. like any Lua object.
  3024. <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
  3025. <span class="apii">[-0, +1, <em>m</em>]</span>
  3026. <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>
  3027. <p>
  3028. This function allocates a new block of memory with the given size,
  3029. pushes onto the stack a new full userdata with the block address,
  3030. and returns this address.
  3031. The host program can freely use this memory.
  3032. <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
  3033. <span class="apii">[-1, +(2|0), <em>e</em>]</span>
  3034. <pre>int lua_next (lua_State *L, int index);</pre>
  3035. <p>
  3036. Pops a key from the stack,
  3037. and pushes a key&ndash;value pair from the table at the given index
  3038. (the "next" pair after the given key).
  3039. If there are no more elements in the table,
  3040. then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
  3041. <p>
  3042. A typical traversal looks like this:
  3043. <pre>
  3044. /* table is in the stack at index 't' */
  3045. lua_pushnil(L); /* first key */
  3046. while (lua_next(L, t) != 0) {
  3047. /* uses 'key' (at index -2) and 'value' (at index -1) */
  3048. printf("%s - %s\n",
  3049. lua_typename(L, lua_type(L, -2)),
  3050. lua_typename(L, lua_type(L, -1)));
  3051. /* removes 'value'; keeps 'key' for next iteration */
  3052. lua_pop(L, 1);
  3053. }
  3054. </pre>
  3055. <p>
  3056. While traversing a table,
  3057. do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
  3058. unless you know that the key is actually a string.
  3059. Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
  3060. the value at the given index;
  3061. this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
  3062. <p>
  3063. See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
  3064. the table during its traversal.
  3065. <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
  3066. <pre>typedef ... lua_Number;</pre>
  3067. <p>
  3068. The type of floats in Lua.
  3069. <p>
  3070. By default this type is double,
  3071. but that can be changed to a single float or a long double.
  3072. (See <code>LUA_FLOAT_TYPE</code> in <code>luaconf.h</code>.)
  3073. <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3>
  3074. <pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre>
  3075. <p>
  3076. Converts a Lua float to a Lua integer.
  3077. This macro assumes that <code>n</code> has an integral value.
  3078. If that value is within the range of Lua integers,
  3079. it is converted to an integer and assigned to <code>*p</code>.
  3080. The macro results in a boolean indicating whether the
  3081. conversion was successful.
  3082. (Note that this range test can be tricky to do
  3083. correctly without this macro,
  3084. due to roundings.)
  3085. <p>
  3086. This macro may evaluate its arguments more than once.
  3087. <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
  3088. <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
  3089. <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
  3090. <p>
  3091. Calls a function in protected mode.
  3092. <p>
  3093. Both <code>nargs</code> and <code>nresults</code> have the same meaning as
  3094. in <a href="#lua_call"><code>lua_call</code></a>.
  3095. If there are no errors during the call,
  3096. <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
  3097. However, if there is any error,
  3098. <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
  3099. pushes a single value on the stack (the error object),
  3100. and returns an error code.
  3101. Like <a href="#lua_call"><code>lua_call</code></a>,
  3102. <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
  3103. and its arguments from the stack.
  3104. <p>
  3105. If <code>msgh</code> is 0,
  3106. then the error object returned on the stack
  3107. is exactly the original error object.
  3108. Otherwise, <code>msgh</code> is the stack index of a
  3109. <em>message handler</em>.
  3110. (This index cannot be a pseudo-index.)
  3111. In case of runtime errors,
  3112. this function will be called with the error object
  3113. and its return value will be the object
  3114. returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
  3115. <p>
  3116. Typically, the message handler is used to add more debug
  3117. information to the error object, such as a stack traceback.
  3118. Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
  3119. since by then the stack has unwound.
  3120. <p>
  3121. The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
  3122. (defined in <code>lua.h</code>):
  3123. <ul>
  3124. <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b>
  3125. success.</li>
  3126. <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b>
  3127. a runtime error.
  3128. </li>
  3129. <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
  3130. memory allocation error.
  3131. For such errors, Lua does not call the message handler.
  3132. </li>
  3133. <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b>
  3134. error while running the message handler.
  3135. </li>
  3136. <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
  3137. error while running a <code>__gc</code> metamethod.
  3138. For such errors, Lua does not call the message handler
  3139. (as this kind of error typically has no relation
  3140. with the function being called).
  3141. </li>
  3142. </ul>
  3143. <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
  3144. <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
  3145. <pre>int lua_pcallk (lua_State *L,
  3146. int nargs,
  3147. int nresults,
  3148. int msgh,
  3149. lua_KContext ctx,
  3150. lua_KFunction k);</pre>
  3151. <p>
  3152. This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
  3153. but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
  3154. <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
  3155. <span class="apii">[-n, +0, &ndash;]</span>
  3156. <pre>void lua_pop (lua_State *L, int n);</pre>
  3157. <p>
  3158. Pops <code>n</code> elements from the stack.
  3159. <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
  3160. <span class="apii">[-0, +1, &ndash;]</span>
  3161. <pre>void lua_pushboolean (lua_State *L, int b);</pre>
  3162. <p>
  3163. Pushes a boolean value with value <code>b</code> onto the stack.
  3164. <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
  3165. <span class="apii">[-n, +1, <em>m</em>]</span>
  3166. <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
  3167. <p>
  3168. Pushes a new C&nbsp;closure onto the stack.
  3169. <p>
  3170. When a C&nbsp;function is created,
  3171. it is possible to associate some values with it,
  3172. thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);
  3173. these values are then accessible to the function whenever it is called.
  3174. To associate values with a C&nbsp;function,
  3175. first these values must be pushed onto the stack
  3176. (when there are multiple values, the first value is pushed first).
  3177. Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
  3178. is called to create and push the C&nbsp;function onto the stack,
  3179. with the argument <code>n</code> telling how many values will be
  3180. associated with the function.
  3181. <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
  3182. <p>
  3183. The maximum value for <code>n</code> is 255.
  3184. <p>
  3185. When <code>n</code> is zero,
  3186. this function creates a <em>light C&nbsp;function</em>,
  3187. which is just a pointer to the C&nbsp;function.
  3188. In that case, it never raises a memory error.
  3189. <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
  3190. <span class="apii">[-0, +1, &ndash;]</span>
  3191. <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
  3192. <p>
  3193. Pushes a C&nbsp;function onto the stack.
  3194. This function receives a pointer to a C&nbsp;function
  3195. and pushes onto the stack a Lua value of type <code>function</code> that,
  3196. when called, invokes the corresponding C&nbsp;function.
  3197. <p>
  3198. Any function to be callable by Lua must
  3199. follow the correct protocol to receive its parameters
  3200. and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  3201. <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
  3202. <span class="apii">[-0, +1, <em>e</em>]</span>
  3203. <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
  3204. <p>
  3205. Pushes onto the stack a formatted string
  3206. and returns a pointer to this string.
  3207. It is similar to the ISO&nbsp;C function <code>sprintf</code>,
  3208. but has some important differences:
  3209. <ul>
  3210. <li>
  3211. You do not have to allocate space for the result:
  3212. the result is a Lua string and Lua takes care of memory allocation
  3213. (and deallocation, through garbage collection).
  3214. </li>
  3215. <li>
  3216. The conversion specifiers are quite restricted.
  3217. There are no flags, widths, or precisions.
  3218. The conversion specifiers can only be
  3219. '<code>%%</code>' (inserts the character '<code>%</code>'),
  3220. '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
  3221. '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
  3222. '<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>),
  3223. '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
  3224. '<code>%d</code>' (inserts an <code>int</code>),
  3225. '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and
  3226. '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence).
  3227. </li>
  3228. </ul>
  3229. <p>
  3230. Unlike other push functions,
  3231. this function checks for the stack space it needs,
  3232. including the slot for its result.
  3233. <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
  3234. <span class="apii">[-0, +1, &ndash;]</span>
  3235. <pre>void lua_pushglobaltable (lua_State *L);</pre>
  3236. <p>
  3237. Pushes the global environment onto the stack.
  3238. <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
  3239. <span class="apii">[-0, +1, &ndash;]</span>
  3240. <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
  3241. <p>
  3242. Pushes an integer with value <code>n</code> onto the stack.
  3243. <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
  3244. <span class="apii">[-0, +1, &ndash;]</span>
  3245. <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
  3246. <p>
  3247. Pushes a light userdata onto the stack.
  3248. <p>
  3249. Userdata represent C&nbsp;values in Lua.
  3250. A <em>light userdata</em> represents a pointer, a <code>void*</code>.
  3251. It is a value (like a number):
  3252. you do not create it, it has no individual metatable,
  3253. and it is not collected (as it was never created).
  3254. A light userdata is equal to "any"
  3255. light userdata with the same C&nbsp;address.
  3256. <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
  3257. <span class="apii">[-0, +1, <em>m</em>]</span>
  3258. <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre>
  3259. <p>
  3260. This macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>,
  3261. but should be used only when <code>s</code> is a literal string.
  3262. <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
  3263. <span class="apii">[-0, +1, <em>m</em>]</span>
  3264. <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
  3265. <p>
  3266. Pushes the string pointed to by <code>s</code> with size <code>len</code>
  3267. onto the stack.
  3268. Lua makes (or reuses) an internal copy of the given string,
  3269. so the memory at <code>s</code> can be freed or reused immediately after
  3270. the function returns.
  3271. The string can contain any binary data,
  3272. including embedded zeros.
  3273. <p>
  3274. Returns a pointer to the internal copy of the string.
  3275. <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
  3276. <span class="apii">[-0, +1, &ndash;]</span>
  3277. <pre>void lua_pushnil (lua_State *L);</pre>
  3278. <p>
  3279. Pushes a nil value onto the stack.
  3280. <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
  3281. <span class="apii">[-0, +1, &ndash;]</span>
  3282. <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
  3283. <p>
  3284. Pushes a float with value <code>n</code> onto the stack.
  3285. <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
  3286. <span class="apii">[-0, +1, <em>m</em>]</span>
  3287. <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre>
  3288. <p>
  3289. Pushes the zero-terminated string pointed to by <code>s</code>
  3290. onto the stack.
  3291. Lua makes (or reuses) an internal copy of the given string,
  3292. so the memory at <code>s</code> can be freed or reused immediately after
  3293. the function returns.
  3294. <p>
  3295. Returns a pointer to the internal copy of the string.
  3296. <p>
  3297. If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>.
  3298. <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
  3299. <span class="apii">[-0, +1, &ndash;]</span>
  3300. <pre>int lua_pushthread (lua_State *L);</pre>
  3301. <p>
  3302. Pushes the thread represented by <code>L</code> onto the stack.
  3303. Returns 1 if this thread is the main thread of its state.
  3304. <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
  3305. <span class="apii">[-0, +1, &ndash;]</span>
  3306. <pre>void lua_pushvalue (lua_State *L, int index);</pre>
  3307. <p>
  3308. Pushes a copy of the element at the given index
  3309. onto the stack.
  3310. <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
  3311. <span class="apii">[-0, +1, <em>m</em>]</span>
  3312. <pre>const char *lua_pushvfstring (lua_State *L,
  3313. const char *fmt,
  3314. va_list argp);</pre>
  3315. <p>
  3316. Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
  3317. instead of a variable number of arguments.
  3318. <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
  3319. <span class="apii">[-0, +0, &ndash;]</span>
  3320. <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
  3321. <p>
  3322. Returns 1 if the two values in indices <code>index1</code> and
  3323. <code>index2</code> are primitively equal
  3324. (that is, without calling the <code>__eq</code> metamethod).
  3325. Otherwise returns&nbsp;0.
  3326. Also returns&nbsp;0 if any of the indices are not valid.
  3327. <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
  3328. <span class="apii">[-1, +1, &ndash;]</span>
  3329. <pre>int lua_rawget (lua_State *L, int index);</pre>
  3330. <p>
  3331. Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
  3332. (i.e., without metamethods).
  3333. <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
  3334. <span class="apii">[-0, +1, &ndash;]</span>
  3335. <pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre>
  3336. <p>
  3337. Pushes onto the stack the value <code>t[n]</code>,
  3338. where <code>t</code> is the table at the given index.
  3339. The access is raw,
  3340. that is, it does not invoke the <code>__index</code> metamethod.
  3341. <p>
  3342. Returns the type of the pushed value.
  3343. <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
  3344. <span class="apii">[-0, +1, &ndash;]</span>
  3345. <pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre>
  3346. <p>
  3347. Pushes onto the stack the value <code>t[k]</code>,
  3348. where <code>t</code> is the table at the given index and
  3349. <code>k</code> is the pointer <code>p</code> represented as a light userdata.
  3350. The access is raw;
  3351. that is, it does not invoke the <code>__index</code> metamethod.
  3352. <p>
  3353. Returns the type of the pushed value.
  3354. <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
  3355. <span class="apii">[-0, +0, &ndash;]</span>
  3356. <pre>size_t lua_rawlen (lua_State *L, int index);</pre>
  3357. <p>
  3358. Returns the raw "length" of the value at the given index:
  3359. for strings, this is the string length;
  3360. for tables, this is the result of the length operator ('<code>#</code>')
  3361. with no metamethods;
  3362. for userdata, this is the size of the block of memory allocated
  3363. for the userdata;
  3364. for other values, it is&nbsp;0.
  3365. <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
  3366. <span class="apii">[-2, +0, <em>m</em>]</span>
  3367. <pre>void lua_rawset (lua_State *L, int index);</pre>
  3368. <p>
  3369. Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
  3370. (i.e., without metamethods).
  3371. <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
  3372. <span class="apii">[-1, +0, <em>m</em>]</span>
  3373. <pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre>
  3374. <p>
  3375. Does the equivalent of <code>t[i] = v</code>,
  3376. where <code>t</code> is the table at the given index
  3377. and <code>v</code> is the value at the top of the stack.
  3378. <p>
  3379. This function pops the value from the stack.
  3380. The assignment is raw,
  3381. that is, it does not invoke the <code>__newindex</code> metamethod.
  3382. <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
  3383. <span class="apii">[-1, +0, <em>m</em>]</span>
  3384. <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
  3385. <p>
  3386. Does the equivalent of <code>t[p] = v</code>,
  3387. where <code>t</code> is the table at the given index,
  3388. <code>p</code> is encoded as a light userdata,
  3389. and <code>v</code> is the value at the top of the stack.
  3390. <p>
  3391. This function pops the value from the stack.
  3392. The assignment is raw,
  3393. that is, it does not invoke <code>__newindex</code> metamethod.
  3394. <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
  3395. <pre>typedef const char * (*lua_Reader) (lua_State *L,
  3396. void *data,
  3397. size_t *size);</pre>
  3398. <p>
  3399. The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
  3400. Every time it needs another piece of the chunk,
  3401. <a href="#lua_load"><code>lua_load</code></a> calls the reader,
  3402. passing along its <code>data</code> parameter.
  3403. The reader must return a pointer to a block of memory
  3404. with a new piece of the chunk
  3405. and set <code>size</code> to the block size.
  3406. The block must exist until the reader function is called again.
  3407. To signal the end of the chunk,
  3408. the reader must return <code>NULL</code> or set <code>size</code> to zero.
  3409. The reader function may return pieces of any size greater than zero.
  3410. <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
  3411. <span class="apii">[-0, +0, <em>e</em>]</span>
  3412. <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre>
  3413. <p>
  3414. Sets the C&nbsp;function <code>f</code> as the new value of global <code>name</code>.
  3415. It is defined as a macro:
  3416. <pre>
  3417. #define lua_register(L,n,f) \
  3418. (lua_pushcfunction(L, f), lua_setglobal(L, n))
  3419. </pre>
  3420. <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
  3421. <span class="apii">[-1, +0, &ndash;]</span>
  3422. <pre>void lua_remove (lua_State *L, int index);</pre>
  3423. <p>
  3424. Removes the element at the given valid index,
  3425. shifting down the elements above this index to fill the gap.
  3426. This function cannot be called with a pseudo-index,
  3427. because a pseudo-index is not an actual stack position.
  3428. <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
  3429. <span class="apii">[-1, +0, &ndash;]</span>
  3430. <pre>void lua_replace (lua_State *L, int index);</pre>
  3431. <p>
  3432. Moves the top element into the given valid index
  3433. without shifting any element
  3434. (therefore replacing the value at that given index),
  3435. and then pops the top element.
  3436. <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
  3437. <span class="apii">[-?, +?, &ndash;]</span>
  3438. <pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre>
  3439. <p>
  3440. Starts and resumes a coroutine in the given thread <code>L</code>.
  3441. <p>
  3442. To start a coroutine,
  3443. you push onto the thread stack the main function plus any arguments;
  3444. then you call <a href="#lua_resume"><code>lua_resume</code></a>,
  3445. with <code>nargs</code> being the number of arguments.
  3446. This call returns when the coroutine suspends or finishes its execution.
  3447. When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>,
  3448. or all values returned by the body function.
  3449. <a href="#lua_resume"><code>lua_resume</code></a> returns
  3450. <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
  3451. <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
  3452. without errors,
  3453. or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
  3454. <p>
  3455. In case of errors,
  3456. the stack is not unwound,
  3457. so you can use the debug API over it.
  3458. The error object is on the top of the stack.
  3459. <p>
  3460. To resume a coroutine,
  3461. you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>,
  3462. put on its stack only the values to
  3463. be passed as results from <code>yield</code>,
  3464. and then call <a href="#lua_resume"><code>lua_resume</code></a>.
  3465. <p>
  3466. The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
  3467. If there is no such coroutine,
  3468. this parameter can be <code>NULL</code>.
  3469. <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
  3470. <span class="apii">[-0, +0, &ndash;]</span>
  3471. <pre>void lua_rotate (lua_State *L, int idx, int n);</pre>
  3472. <p>
  3473. Rotates the stack elements between the valid index <code>idx</code>
  3474. and the top of the stack.
  3475. The elements are rotated <code>n</code> positions in the direction of the top,
  3476. for a positive <code>n</code>,
  3477. or <code>-n</code> positions in the direction of the bottom,
  3478. for a negative <code>n</code>.
  3479. The absolute value of <code>n</code> must not be greater than the size
  3480. of the slice being rotated.
  3481. This function cannot be called with a pseudo-index,
  3482. because a pseudo-index is not an actual stack position.
  3483. <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
  3484. <span class="apii">[-0, +0, &ndash;]</span>
  3485. <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
  3486. <p>
  3487. Changes the allocator function of a given state to <code>f</code>
  3488. with user data <code>ud</code>.
  3489. <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
  3490. <span class="apii">[-1, +0, <em>e</em>]</span>
  3491. <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
  3492. <p>
  3493. Does the equivalent to <code>t[k] = v</code>,
  3494. where <code>t</code> is the value at the given index
  3495. and <code>v</code> is the value at the top of the stack.
  3496. <p>
  3497. This function pops the value from the stack.
  3498. As in Lua, this function may trigger a metamethod
  3499. for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
  3500. <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
  3501. <span class="apii">[-1, +0, <em>e</em>]</span>
  3502. <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
  3503. <p>
  3504. Pops a value from the stack and
  3505. sets it as the new value of global <code>name</code>.
  3506. <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p>
  3507. <span class="apii">[-1, +0, <em>e</em>]</span>
  3508. <pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre>
  3509. <p>
  3510. Does the equivalent to <code>t[n] = v</code>,
  3511. where <code>t</code> is the value at the given index
  3512. and <code>v</code> is the value at the top of the stack.
  3513. <p>
  3514. This function pops the value from the stack.
  3515. As in Lua, this function may trigger a metamethod
  3516. for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
  3517. <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
  3518. <span class="apii">[-1, +0, &ndash;]</span>
  3519. <pre>void lua_setmetatable (lua_State *L, int index);</pre>
  3520. <p>
  3521. Pops a table from the stack and
  3522. sets it as the new metatable for the value at the given index.
  3523. <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
  3524. <span class="apii">[-2, +0, <em>e</em>]</span>
  3525. <pre>void lua_settable (lua_State *L, int index);</pre>
  3526. <p>
  3527. Does the equivalent to <code>t[k] = v</code>,
  3528. where <code>t</code> is the value at the given index,
  3529. <code>v</code> is the value at the top of the stack,
  3530. and <code>k</code> is the value just below the top.
  3531. <p>
  3532. This function pops both the key and the value from the stack.
  3533. As in Lua, this function may trigger a metamethod
  3534. for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
  3535. <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
  3536. <span class="apii">[-?, +?, &ndash;]</span>
  3537. <pre>void lua_settop (lua_State *L, int index);</pre>
  3538. <p>
  3539. Accepts any index, or&nbsp;0,
  3540. and sets the stack top to this index.
  3541. If the new top is larger than the old one,
  3542. then the new elements are filled with <b>nil</b>.
  3543. If <code>index</code> is&nbsp;0, then all stack elements are removed.
  3544. <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
  3545. <span class="apii">[-1, +0, &ndash;]</span>
  3546. <pre>void lua_setuservalue (lua_State *L, int index);</pre>
  3547. <p>
  3548. Pops a value from the stack and sets it as
  3549. the new value associated to the full userdata at the given index.
  3550. <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
  3551. <pre>typedef struct lua_State lua_State;</pre>
  3552. <p>
  3553. An opaque structure that points to a thread and indirectly
  3554. (through the thread) to the whole state of a Lua interpreter.
  3555. The Lua library is fully reentrant:
  3556. it has no global variables.
  3557. All information about a state is accessible through this structure.
  3558. <p>
  3559. A pointer to this structure must be passed as the first argument to
  3560. every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
  3561. which creates a Lua state from scratch.
  3562. <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
  3563. <span class="apii">[-0, +0, &ndash;]</span>
  3564. <pre>int lua_status (lua_State *L);</pre>
  3565. <p>
  3566. Returns the status of the thread <code>L</code>.
  3567. <p>
  3568. The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread,
  3569. an error code if the thread finished the execution
  3570. of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
  3571. or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
  3572. <p>
  3573. You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
  3574. You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
  3575. (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
  3576. (to resume a coroutine).
  3577. <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p>
  3578. <span class="apii">[-0, +1, &ndash;]</span>
  3579. <pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre>
  3580. <p>
  3581. Converts the zero-terminated string <code>s</code> to a number,
  3582. pushes that number into the stack,
  3583. and returns the total size of the string,
  3584. that is, its length plus one.
  3585. The conversion can result in an integer or a float,
  3586. according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
  3587. The string may have leading and trailing spaces and a sign.
  3588. If the string is not a valid numeral,
  3589. returns 0 and pushes nothing.
  3590. (Note that the result can be used as a boolean,
  3591. true if the conversion succeeds.)
  3592. <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
  3593. <span class="apii">[-0, +0, &ndash;]</span>
  3594. <pre>int lua_toboolean (lua_State *L, int index);</pre>
  3595. <p>
  3596. Converts the Lua value at the given index to a C&nbsp;boolean
  3597. value (0&nbsp;or&nbsp;1).
  3598. Like all tests in Lua,
  3599. <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
  3600. different from <b>false</b> and <b>nil</b>;
  3601. otherwise it returns false.
  3602. (If you want to accept only actual boolean values,
  3603. use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
  3604. <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
  3605. <span class="apii">[-0, +0, &ndash;]</span>
  3606. <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
  3607. <p>
  3608. Converts a value at the given index to a C&nbsp;function.
  3609. That value must be a C&nbsp;function;
  3610. otherwise, returns <code>NULL</code>.
  3611. <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
  3612. <span class="apii">[-0, +0, &ndash;]</span>
  3613. <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
  3614. <p>
  3615. Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
  3616. <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
  3617. <span class="apii">[-0, +0, &ndash;]</span>
  3618. <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
  3619. <p>
  3620. Converts the Lua value at the given index
  3621. to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
  3622. The Lua value must be an integer,
  3623. or a number or string convertible to an integer (see <a href="#3.4.3">&sect;3.4.3</a>);
  3624. otherwise, <code>lua_tointegerx</code> returns&nbsp;0.
  3625. <p>
  3626. If <code>isnum</code> is not <code>NULL</code>,
  3627. its referent is assigned a boolean value that
  3628. indicates whether the operation succeeded.
  3629. <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
  3630. <span class="apii">[-0, +0, <em>m</em>]</span>
  3631. <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
  3632. <p>
  3633. Converts the Lua value at the given index to a C&nbsp;string.
  3634. If <code>len</code> is not <code>NULL</code>,
  3635. it sets <code>*len</code> with the string length.
  3636. The Lua value must be a string or a number;
  3637. otherwise, the function returns <code>NULL</code>.
  3638. If the value is a number,
  3639. then <code>lua_tolstring</code> also
  3640. <em>changes the actual value in the stack to a string</em>.
  3641. (This change confuses <a href="#lua_next"><code>lua_next</code></a>
  3642. when <code>lua_tolstring</code> is applied to keys during a table traversal.)
  3643. <p>
  3644. <code>lua_tolstring</code> returns a pointer
  3645. to a string inside the Lua state.
  3646. This string always has a zero ('<code>\0</code>')
  3647. after its last character (as in&nbsp;C),
  3648. but can contain other zeros in its body.
  3649. <p>
  3650. Because Lua has garbage collection,
  3651. there is no guarantee that the pointer returned by <code>lua_tolstring</code>
  3652. will be valid after the corresponding Lua value is removed from the stack.
  3653. <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
  3654. <span class="apii">[-0, +0, &ndash;]</span>
  3655. <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
  3656. <p>
  3657. Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
  3658. <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
  3659. <span class="apii">[-0, +0, &ndash;]</span>
  3660. <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
  3661. <p>
  3662. Converts the Lua value at the given index
  3663. to the C&nbsp;type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
  3664. The Lua value must be a number or a string convertible to a number
  3665. (see <a href="#3.4.3">&sect;3.4.3</a>);
  3666. otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns&nbsp;0.
  3667. <p>
  3668. If <code>isnum</code> is not <code>NULL</code>,
  3669. its referent is assigned a boolean value that
  3670. indicates whether the operation succeeded.
  3671. <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
  3672. <span class="apii">[-0, +0, &ndash;]</span>
  3673. <pre>const void *lua_topointer (lua_State *L, int index);</pre>
  3674. <p>
  3675. Converts the value at the given index to a generic
  3676. C&nbsp;pointer (<code>void*</code>).
  3677. The value can be a userdata, a table, a thread, or a function;
  3678. otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
  3679. Different objects will give different pointers.
  3680. There is no way to convert the pointer back to its original value.
  3681. <p>
  3682. Typically this function is used only for hashing and debug information.
  3683. <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
  3684. <span class="apii">[-0, +0, <em>m</em>]</span>
  3685. <pre>const char *lua_tostring (lua_State *L, int index);</pre>
  3686. <p>
  3687. Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
  3688. <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
  3689. <span class="apii">[-0, +0, &ndash;]</span>
  3690. <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
  3691. <p>
  3692. Converts the value at the given index to a Lua thread
  3693. (represented as <code>lua_State*</code>).
  3694. This value must be a thread;
  3695. otherwise, the function returns <code>NULL</code>.
  3696. <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
  3697. <span class="apii">[-0, +0, &ndash;]</span>
  3698. <pre>void *lua_touserdata (lua_State *L, int index);</pre>
  3699. <p>
  3700. If the value at the given index is a full userdata,
  3701. returns its block address.
  3702. If the value is a light userdata,
  3703. returns its pointer.
  3704. Otherwise, returns <code>NULL</code>.
  3705. <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
  3706. <span class="apii">[-0, +0, &ndash;]</span>
  3707. <pre>int lua_type (lua_State *L, int index);</pre>
  3708. <p>
  3709. Returns the type of the value in the given valid index,
  3710. or <code>LUA_TNONE</code> for a non-valid (but acceptable) index.
  3711. The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
  3712. defined in <code>lua.h</code>:
  3713. <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a> (0),
  3714. <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
  3715. <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
  3716. <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
  3717. <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
  3718. <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
  3719. <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
  3720. <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
  3721. and
  3722. <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
  3723. <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
  3724. <span class="apii">[-0, +0, &ndash;]</span>
  3725. <pre>const char *lua_typename (lua_State *L, int tp);</pre>
  3726. <p>
  3727. Returns the name of the type encoded by the value <code>tp</code>,
  3728. which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
  3729. <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
  3730. <pre>typedef ... lua_Unsigned;</pre>
  3731. <p>
  3732. The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
  3733. <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
  3734. <span class="apii">[-0, +0, &ndash;]</span>
  3735. <pre>int lua_upvalueindex (int i);</pre>
  3736. <p>
  3737. Returns the pseudo-index that represents the <code>i</code>-th upvalue of
  3738. the running function (see <a href="#4.4">&sect;4.4</a>).
  3739. <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
  3740. <span class="apii">[-0, +0, &ndash;]</span>
  3741. <pre>const lua_Number *lua_version (lua_State *L);</pre>
  3742. <p>
  3743. Returns the address of the version number
  3744. (a C static variable)
  3745. stored in the Lua core.
  3746. When called with a valid <a href="#lua_State"><code>lua_State</code></a>,
  3747. returns the address of the version used to create that state.
  3748. When called with <code>NULL</code>,
  3749. returns the address of the version running the call.
  3750. <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
  3751. <pre>typedef int (*lua_Writer) (lua_State *L,
  3752. const void* p,
  3753. size_t sz,
  3754. void* ud);</pre>
  3755. <p>
  3756. The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
  3757. Every time it produces another piece of chunk,
  3758. <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
  3759. passing along the buffer to be written (<code>p</code>),
  3760. its size (<code>sz</code>),
  3761. and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
  3762. <p>
  3763. The writer returns an error code:
  3764. 0&nbsp;means no errors;
  3765. any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
  3766. calling the writer again.
  3767. <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
  3768. <span class="apii">[-?, +?, &ndash;]</span>
  3769. <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
  3770. <p>
  3771. Exchange values between different threads of the same state.
  3772. <p>
  3773. This function pops <code>n</code> values from the stack <code>from</code>,
  3774. and pushes them onto the stack <code>to</code>.
  3775. <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
  3776. <span class="apii">[-?, +?, <em>e</em>]</span>
  3777. <pre>int lua_yield (lua_State *L, int nresults);</pre>
  3778. <p>
  3779. This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  3780. but it has no continuation (see <a href="#4.7">&sect;4.7</a>).
  3781. Therefore, when the thread resumes,
  3782. it continues the function that called
  3783. the function calling <code>lua_yield</code>.
  3784. <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
  3785. <span class="apii">[-?, +?, <em>e</em>]</span>
  3786. <pre>int lua_yieldk (lua_State *L,
  3787. int nresults,
  3788. lua_KContext ctx,
  3789. lua_KFunction k);</pre>
  3790. <p>
  3791. Yields a coroutine (thread).
  3792. <p>
  3793. When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  3794. the running coroutine suspends its execution,
  3795. and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
  3796. The parameter <code>nresults</code> is the number of values from the stack
  3797. that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
  3798. <p>
  3799. When the coroutine is resumed again,
  3800. Lua calls the given continuation function <code>k</code> to continue
  3801. the execution of the C&nbsp;function that yielded (see <a href="#4.7">&sect;4.7</a>).
  3802. This continuation function receives the same stack
  3803. from the previous function,
  3804. with the <code>n</code> results removed and
  3805. replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
  3806. Moreover,
  3807. the continuation function receives the value <code>ctx</code>
  3808. that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
  3809. <p>
  3810. Usually, this function does not return;
  3811. when the coroutine eventually resumes,
  3812. it continues executing the continuation function.
  3813. However, there is one special case,
  3814. which is when this function is called
  3815. from inside a line or a count hook (see <a href="#4.9">&sect;4.9</a>).
  3816. In that case, <code>lua_yieldk</code> should be called with no continuation
  3817. (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>) and no results,
  3818. and the hook should return immediately after the call.
  3819. Lua will yield and,
  3820. when the coroutine resumes again,
  3821. it will continue the normal execution
  3822. of the (Lua) function that triggered the hook.
  3823. <p>
  3824. This function can raise an error if it is called from a thread
  3825. with a pending C call with no continuation function,
  3826. or it is called from a thread that is not running inside a resume
  3827. (e.g., the main thread).
  3828. <h2>4.9 &ndash; <a name="4.9">The Debug Interface</a></h2>
  3829. <p>
  3830. Lua has no built-in debugging facilities.
  3831. Instead, it offers a special interface
  3832. by means of functions and <em>hooks</em>.
  3833. This interface allows the construction of different
  3834. kinds of debuggers, profilers, and other tools
  3835. that need "inside information" from the interpreter.
  3836. <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
  3837. <pre>typedef struct lua_Debug {
  3838. int event;
  3839. const char *name; /* (n) */
  3840. const char *namewhat; /* (n) */
  3841. const char *what; /* (S) */
  3842. const char *source; /* (S) */
  3843. int currentline; /* (l) */
  3844. int linedefined; /* (S) */
  3845. int lastlinedefined; /* (S) */
  3846. unsigned char nups; /* (u) number of upvalues */
  3847. unsigned char nparams; /* (u) number of parameters */
  3848. char isvararg; /* (u) */
  3849. char istailcall; /* (t) */
  3850. char short_src[LUA_IDSIZE]; /* (S) */
  3851. /* private part */
  3852. <em>other fields</em>
  3853. } lua_Debug;</pre>
  3854. <p>
  3855. A structure used to carry different pieces of
  3856. information about a function or an activation record.
  3857. <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
  3858. of this structure, for later use.
  3859. To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
  3860. call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  3861. <p>
  3862. The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
  3863. <ul>
  3864. <li><b><code>source</code>: </b>
  3865. the name of the chunk that created the function.
  3866. If <code>source</code> starts with a '<code>@</code>',
  3867. it means that the function was defined in a file where
  3868. the file name follows the '<code>@</code>'.
  3869. If <code>source</code> starts with a '<code>=</code>',
  3870. the remainder of its contents describe the source in a user-dependent manner.
  3871. Otherwise,
  3872. the function was defined in a string where
  3873. <code>source</code> is that string.
  3874. </li>
  3875. <li><b><code>short_src</code>: </b>
  3876. a "printable" version of <code>source</code>, to be used in error messages.
  3877. </li>
  3878. <li><b><code>linedefined</code>: </b>
  3879. the line number where the definition of the function starts.
  3880. </li>
  3881. <li><b><code>lastlinedefined</code>: </b>
  3882. the line number where the definition of the function ends.
  3883. </li>
  3884. <li><b><code>what</code>: </b>
  3885. the string <code>"Lua"</code> if the function is a Lua function,
  3886. <code>"C"</code> if it is a C&nbsp;function,
  3887. <code>"main"</code> if it is the main part of a chunk.
  3888. </li>
  3889. <li><b><code>currentline</code>: </b>
  3890. the current line where the given function is executing.
  3891. When no line information is available,
  3892. <code>currentline</code> is set to -1.
  3893. </li>
  3894. <li><b><code>name</code>: </b>
  3895. a reasonable name for the given function.
  3896. Because functions in Lua are first-class values,
  3897. they do not have a fixed name:
  3898. some functions can be the value of multiple global variables,
  3899. while others can be stored only in a table field.
  3900. The <code>lua_getinfo</code> function checks how the function was
  3901. called to find a suitable name.
  3902. If it cannot find a name,
  3903. then <code>name</code> is set to <code>NULL</code>.
  3904. </li>
  3905. <li><b><code>namewhat</code>: </b>
  3906. explains the <code>name</code> field.
  3907. The value of <code>namewhat</code> can be
  3908. <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
  3909. <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
  3910. according to how the function was called.
  3911. (Lua uses the empty string when no other option seems to apply.)
  3912. </li>
  3913. <li><b><code>istailcall</code>: </b>
  3914. true if this function invocation was called by a tail call.
  3915. In this case, the caller of this level is not in the stack.
  3916. </li>
  3917. <li><b><code>nups</code>: </b>
  3918. the number of upvalues of the function.
  3919. </li>
  3920. <li><b><code>nparams</code>: </b>
  3921. the number of fixed parameters of the function
  3922. (always 0&nbsp;for C&nbsp;functions).
  3923. </li>
  3924. <li><b><code>isvararg</code>: </b>
  3925. true if the function is a vararg function
  3926. (always true for C&nbsp;functions).
  3927. </li>
  3928. </ul>
  3929. <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
  3930. <span class="apii">[-0, +0, &ndash;]</span>
  3931. <pre>lua_Hook lua_gethook (lua_State *L);</pre>
  3932. <p>
  3933. Returns the current hook function.
  3934. <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
  3935. <span class="apii">[-0, +0, &ndash;]</span>
  3936. <pre>int lua_gethookcount (lua_State *L);</pre>
  3937. <p>
  3938. Returns the current hook count.
  3939. <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
  3940. <span class="apii">[-0, +0, &ndash;]</span>
  3941. <pre>int lua_gethookmask (lua_State *L);</pre>
  3942. <p>
  3943. Returns the current hook mask.
  3944. <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
  3945. <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span>
  3946. <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
  3947. <p>
  3948. Gets information about a specific function or function invocation.
  3949. <p>
  3950. To get information about a function invocation,
  3951. the parameter <code>ar</code> must be a valid activation record that was
  3952. filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
  3953. given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
  3954. <p>
  3955. To get information about a function, you push it onto the stack
  3956. and start the <code>what</code> string with the character '<code>&gt;</code>'.
  3957. (In that case,
  3958. <code>lua_getinfo</code> pops the function from the top of the stack.)
  3959. For instance, to know in which line a function <code>f</code> was defined,
  3960. you can write the following code:
  3961. <pre>
  3962. lua_Debug ar;
  3963. lua_getglobal(L, "f"); /* get global 'f' */
  3964. lua_getinfo(L, "&gt;S", &amp;ar);
  3965. printf("%d\n", ar.linedefined);
  3966. </pre>
  3967. <p>
  3968. Each character in the string <code>what</code>
  3969. selects some fields of the structure <code>ar</code> to be filled or
  3970. a value to be pushed on the stack:
  3971. <ul>
  3972. <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>;
  3973. </li>
  3974. <li><b>'<code>S</code>': </b>
  3975. fills in the fields <code>source</code>, <code>short_src</code>,
  3976. <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
  3977. </li>
  3978. <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>;
  3979. </li>
  3980. <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>;
  3981. </li>
  3982. <li><b>'<code>u</code>': </b> fills in the fields
  3983. <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>;
  3984. </li>
  3985. <li><b>'<code>f</code>': </b>
  3986. pushes onto the stack the function that is
  3987. running at the given level;
  3988. </li>
  3989. <li><b>'<code>L</code>': </b>
  3990. pushes onto the stack a table whose indices are the
  3991. numbers of the lines that are valid on the function.
  3992. (A <em>valid line</em> is a line with some associated code,
  3993. that is, a line where you can put a break point.
  3994. Non-valid lines include empty lines and comments.)
  3995. <p>
  3996. If this option is given together with option '<code>f</code>',
  3997. its table is pushed after the function.
  3998. </li>
  3999. </ul>
  4000. <p>
  4001. This function returns 0 on error
  4002. (for instance, an invalid option in <code>what</code>).
  4003. <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
  4004. <span class="apii">[-0, +(0|1), &ndash;]</span>
  4005. <pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
  4006. <p>
  4007. Gets information about a local variable of
  4008. a given activation record or a given function.
  4009. <p>
  4010. In the first case,
  4011. the parameter <code>ar</code> must be a valid activation record that was
  4012. filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
  4013. given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
  4014. The index <code>n</code> selects which local variable to inspect;
  4015. see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
  4016. and names.
  4017. <p>
  4018. <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
  4019. and returns its name.
  4020. <p>
  4021. In the second case, <code>ar</code> must be <code>NULL</code> and the function
  4022. to be inspected must be at the top of the stack.
  4023. In this case, only parameters of Lua functions are visible
  4024. (as there is no information about what variables are active)
  4025. and no values are pushed onto the stack.
  4026. <p>
  4027. Returns <code>NULL</code> (and pushes nothing)
  4028. when the index is greater than
  4029. the number of active local variables.
  4030. <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
  4031. <span class="apii">[-0, +0, &ndash;]</span>
  4032. <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
  4033. <p>
  4034. Gets information about the interpreter runtime stack.
  4035. <p>
  4036. This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
  4037. an identification of the <em>activation record</em>
  4038. of the function executing at a given level.
  4039. Level&nbsp;0 is the current running function,
  4040. whereas level <em>n+1</em> is the function that has called level <em>n</em>
  4041. (except for tail calls, which do not count on the stack).
  4042. When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
  4043. when called with a level greater than the stack depth,
  4044. it returns 0.
  4045. <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
  4046. <span class="apii">[-0, +(0|1), &ndash;]</span>
  4047. <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
  4048. <p>
  4049. Gets information about the <code>n</code>-th upvalue
  4050. of the closure at index <code>funcindex</code>.
  4051. It pushes the upvalue's value onto the stack
  4052. and returns its name.
  4053. Returns <code>NULL</code> (and pushes nothing)
  4054. when the index <code>n</code> is greater than the number of upvalues.
  4055. <p>
  4056. For C&nbsp;functions, this function uses the empty string <code>""</code>
  4057. as a name for all upvalues.
  4058. (For Lua functions,
  4059. upvalues are the external local variables that the function uses,
  4060. and that are consequently included in its closure.)
  4061. <p>
  4062. Upvalues have no particular order,
  4063. as they are active through the whole function.
  4064. They are numbered in an arbitrary order.
  4065. <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
  4066. <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
  4067. <p>
  4068. Type for debugging hook functions.
  4069. <p>
  4070. Whenever a hook is called, its <code>ar</code> argument has its field
  4071. <code>event</code> set to the specific event that triggered the hook.
  4072. Lua identifies these events with the following constants:
  4073. <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
  4074. <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
  4075. and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
  4076. Moreover, for line events, the field <code>currentline</code> is also set.
  4077. To get the value of any other field in <code>ar</code>,
  4078. the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
  4079. <p>
  4080. For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>,
  4081. the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
  4082. in this case, there will be no corresponding return event.
  4083. <p>
  4084. While Lua is running a hook, it disables other calls to hooks.
  4085. Therefore, if a hook calls back Lua to execute a function or a chunk,
  4086. this execution occurs without any calls to hooks.
  4087. <p>
  4088. Hook functions cannot have continuations,
  4089. that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
  4090. <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>.
  4091. <p>
  4092. Hook functions can yield under the following conditions:
  4093. Only count and line events can yield;
  4094. to yield, a hook function must finish its execution
  4095. calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero
  4096. (that is, with no values).
  4097. <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
  4098. <span class="apii">[-0, +0, &ndash;]</span>
  4099. <pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
  4100. <p>
  4101. Sets the debugging hook function.
  4102. <p>
  4103. Argument <code>f</code> is the hook function.
  4104. <code>mask</code> specifies on which events the hook will be called:
  4105. it is formed by a bitwise OR of the constants
  4106. <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
  4107. <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
  4108. <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
  4109. and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
  4110. The <code>count</code> argument is only meaningful when the mask
  4111. includes <code>LUA_MASKCOUNT</code>.
  4112. For each event, the hook is called as explained below:
  4113. <ul>
  4114. <li><b>The call hook: </b> is called when the interpreter calls a function.
  4115. The hook is called just after Lua enters the new function,
  4116. before the function gets its arguments.
  4117. </li>
  4118. <li><b>The return hook: </b> is called when the interpreter returns from a function.
  4119. The hook is called just before Lua leaves the function.
  4120. There is no standard way to access the values
  4121. to be returned by the function.
  4122. </li>
  4123. <li><b>The line hook: </b> is called when the interpreter is about to
  4124. start the execution of a new line of code,
  4125. or when it jumps back in the code (even to the same line).
  4126. (This event only happens while Lua is executing a Lua function.)
  4127. </li>
  4128. <li><b>The count hook: </b> is called after the interpreter executes every
  4129. <code>count</code> instructions.
  4130. (This event only happens while Lua is executing a Lua function.)
  4131. </li>
  4132. </ul>
  4133. <p>
  4134. A hook is disabled by setting <code>mask</code> to zero.
  4135. <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
  4136. <span class="apii">[-(0|1), +0, &ndash;]</span>
  4137. <pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
  4138. <p>
  4139. Sets the value of a local variable of a given activation record.
  4140. It assigns the value at the top of the stack
  4141. to the variable and returns its name.
  4142. It also pops the value from the stack.
  4143. <p>
  4144. Returns <code>NULL</code> (and pops nothing)
  4145. when the index is greater than
  4146. the number of active local variables.
  4147. <p>
  4148. Parameters <code>ar</code> and <code>n</code> are as in function <a href="#lua_getlocal"><code>lua_getlocal</code></a>.
  4149. <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
  4150. <span class="apii">[-(0|1), +0, &ndash;]</span>
  4151. <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
  4152. <p>
  4153. Sets the value of a closure's upvalue.
  4154. It assigns the value at the top of the stack
  4155. to the upvalue and returns its name.
  4156. It also pops the value from the stack.
  4157. <p>
  4158. Returns <code>NULL</code> (and pops nothing)
  4159. when the index <code>n</code> is greater than the number of upvalues.
  4160. <p>
  4161. Parameters <code>funcindex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>.
  4162. <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
  4163. <span class="apii">[-0, +0, &ndash;]</span>
  4164. <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre>
  4165. <p>
  4166. Returns a unique identifier for the upvalue numbered <code>n</code>
  4167. from the closure at index <code>funcindex</code>.
  4168. <p>
  4169. These unique identifiers allow a program to check whether different
  4170. closures share upvalues.
  4171. Lua closures that share an upvalue
  4172. (that is, that access a same external local variable)
  4173. will return identical ids for those upvalue indices.
  4174. <p>
  4175. Parameters <code>funcindex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>,
  4176. but <code>n</code> cannot be greater than the number of upvalues.
  4177. <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
  4178. <span class="apii">[-0, +0, &ndash;]</span>
  4179. <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
  4180. int funcindex2, int n2);</pre>
  4181. <p>
  4182. Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
  4183. refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
  4184. <h1>5 &ndash; <a name="5">The Auxiliary Library</a></h1>
  4185. <p>
  4186. The <em>auxiliary library</em> provides several convenient functions
  4187. to interface C with Lua.
  4188. While the basic API provides the primitive functions for all
  4189. interactions between C and Lua,
  4190. the auxiliary library provides higher-level functions for some
  4191. common tasks.
  4192. <p>
  4193. All functions and types from the auxiliary library
  4194. are defined in header file <code>lauxlib.h</code> and
  4195. have a prefix <code>luaL_</code>.
  4196. <p>
  4197. All functions in the auxiliary library are built on
  4198. top of the basic API,
  4199. and so they provide nothing that cannot be done with that API.
  4200. Nevertheless, the use of the auxiliary library ensures
  4201. more consistency to your code.
  4202. <p>
  4203. Several functions in the auxiliary library use internally some
  4204. extra stack slots.
  4205. When a function in the auxiliary library uses less than five slots,
  4206. it does not check the stack size;
  4207. it simply assumes that there are enough slots.
  4208. <p>
  4209. Several functions in the auxiliary library are used to
  4210. check C&nbsp;function arguments.
  4211. Because the error message is formatted for arguments
  4212. (e.g., "<code>bad argument #1</code>"),
  4213. you should not use these functions for other stack values.
  4214. <p>
  4215. Functions called <code>luaL_check*</code>
  4216. always raise an error if the check is not satisfied.
  4217. <h2>5.1 &ndash; <a name="5.1">Functions and Types</a></h2>
  4218. <p>
  4219. Here we list all functions and types from the auxiliary library
  4220. in alphabetical order.
  4221. <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
  4222. <span class="apii">[-?, +?, <em>m</em>]</span>
  4223. <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
  4224. <p>
  4225. Adds the byte <code>c</code> to the buffer <code>B</code>
  4226. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4227. <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
  4228. <span class="apii">[-?, +?, <em>m</em>]</span>
  4229. <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
  4230. <p>
  4231. Adds the string pointed to by <code>s</code> with length <code>l</code> to
  4232. the buffer <code>B</code>
  4233. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4234. The string can contain embedded zeros.
  4235. <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
  4236. <span class="apii">[-?, +?, &ndash;]</span>
  4237. <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
  4238. <p>
  4239. Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
  4240. a string of length <code>n</code> previously copied to the
  4241. buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
  4242. <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
  4243. <span class="apii">[-?, +?, <em>m</em>]</span>
  4244. <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
  4245. <p>
  4246. Adds the zero-terminated string pointed to by <code>s</code>
  4247. to the buffer <code>B</code>
  4248. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4249. <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
  4250. <span class="apii">[-1, +?, <em>m</em>]</span>
  4251. <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
  4252. <p>
  4253. Adds the value at the top of the stack
  4254. to the buffer <code>B</code>
  4255. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4256. Pops the value.
  4257. <p>
  4258. This is the only function on string buffers that can (and must)
  4259. be called with an extra element on the stack,
  4260. which is the value to be added to the buffer.
  4261. <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
  4262. <span class="apii">[-0, +0, <em>v</em>]</span>
  4263. <pre>void luaL_argcheck (lua_State *L,
  4264. int cond,
  4265. int arg,
  4266. const char *extramsg);</pre>
  4267. <p>
  4268. Checks whether <code>cond</code> is true.
  4269. If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>).
  4270. <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
  4271. <span class="apii">[-0, +0, <em>v</em>]</span>
  4272. <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre>
  4273. <p>
  4274. Raises an error reporting a problem with argument <code>arg</code>
  4275. of the C&nbsp;function that called it,
  4276. using a standard message
  4277. that includes <code>extramsg</code> as a comment:
  4278. <pre>
  4279. bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>)
  4280. </pre><p>
  4281. This function never returns.
  4282. <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
  4283. <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
  4284. <p>
  4285. Type for a <em>string buffer</em>.
  4286. <p>
  4287. A string buffer allows C&nbsp;code to build Lua strings piecemeal.
  4288. Its pattern of use is as follows:
  4289. <ul>
  4290. <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
  4291. <li>Then initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>
  4292. <li>
  4293. Then add string pieces to the buffer calling any of
  4294. the <code>luaL_add*</code> functions.
  4295. </li>
  4296. <li>
  4297. Finish by calling <code>luaL_pushresult(&amp;b)</code>.
  4298. This call leaves the final string on the top of the stack.
  4299. </li>
  4300. </ul>
  4301. <p>
  4302. If you know beforehand the total size of the resulting string,
  4303. you can use the buffer like this:
  4304. <ul>
  4305. <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
  4306. <li>Then initialize it and preallocate a space of
  4307. size <code>sz</code> with a call <code>luaL_buffinitsize(L, &amp;b, sz)</code>.</li>
  4308. <li>Then copy the string into that space.</li>
  4309. <li>
  4310. Finish by calling <code>luaL_pushresultsize(&amp;b, sz)</code>,
  4311. where <code>sz</code> is the total size of the resulting string
  4312. copied into that space.
  4313. </li>
  4314. </ul>
  4315. <p>
  4316. During its normal operation,
  4317. a string buffer uses a variable number of stack slots.
  4318. So, while using a buffer, you cannot assume that you know where
  4319. the top of the stack is.
  4320. You can use the stack between successive calls to buffer operations
  4321. as long as that use is balanced;
  4322. that is,
  4323. when you call a buffer operation,
  4324. the stack is at the same level
  4325. it was immediately after the previous buffer operation.
  4326. (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
  4327. After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
  4328. level when the buffer was initialized,
  4329. plus the final string on its top.
  4330. <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
  4331. <span class="apii">[-0, +0, &ndash;]</span>
  4332. <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
  4333. <p>
  4334. Initializes a buffer <code>B</code>.
  4335. This function does not allocate any space;
  4336. the buffer must be declared as a variable
  4337. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4338. <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
  4339. <span class="apii">[-?, +?, <em>m</em>]</span>
  4340. <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre>
  4341. <p>
  4342. Equivalent to the sequence
  4343. <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>.
  4344. <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
  4345. <span class="apii">[-0, +(0|1), <em>e</em>]</span>
  4346. <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
  4347. <p>
  4348. Calls a metamethod.
  4349. <p>
  4350. If the object at index <code>obj</code> has a metatable and this
  4351. metatable has a field <code>e</code>,
  4352. this function calls this field passing the object as its only argument.
  4353. In this case this function returns true and pushes onto the
  4354. stack the value returned by the call.
  4355. If there is no metatable or no metamethod,
  4356. this function returns false (without pushing any value on the stack).
  4357. <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
  4358. <span class="apii">[-0, +0, <em>v</em>]</span>
  4359. <pre>void luaL_checkany (lua_State *L, int arg);</pre>
  4360. <p>
  4361. Checks whether the function has an argument
  4362. of any type (including <b>nil</b>) at position <code>arg</code>.
  4363. <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
  4364. <span class="apii">[-0, +0, <em>v</em>]</span>
  4365. <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre>
  4366. <p>
  4367. Checks whether the function argument <code>arg</code> is an integer
  4368. (or can be converted to an integer)
  4369. and returns this integer cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
  4370. <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
  4371. <span class="apii">[-0, +0, <em>v</em>]</span>
  4372. <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre>
  4373. <p>
  4374. Checks whether the function argument <code>arg</code> is a string
  4375. and returns this string;
  4376. if <code>l</code> is not <code>NULL</code> fills <code>*l</code>
  4377. with the string's length.
  4378. <p>
  4379. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  4380. so all conversions and caveats of that function apply here.
  4381. <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
  4382. <span class="apii">[-0, +0, <em>v</em>]</span>
  4383. <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre>
  4384. <p>
  4385. Checks whether the function argument <code>arg</code> is a number
  4386. and returns this number.
  4387. <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
  4388. <span class="apii">[-0, +0, <em>v</em>]</span>
  4389. <pre>int luaL_checkoption (lua_State *L,
  4390. int arg,
  4391. const char *def,
  4392. const char *const lst[]);</pre>
  4393. <p>
  4394. Checks whether the function argument <code>arg</code> is a string and
  4395. searches for this string in the array <code>lst</code>
  4396. (which must be NULL-terminated).
  4397. Returns the index in the array where the string was found.
  4398. Raises an error if the argument is not a string or
  4399. if the string cannot be found.
  4400. <p>
  4401. If <code>def</code> is not <code>NULL</code>,
  4402. the function uses <code>def</code> as a default value when
  4403. there is no argument <code>arg</code> or when this argument is <b>nil</b>.
  4404. <p>
  4405. This is a useful function for mapping strings to C&nbsp;enums.
  4406. (The usual convention in Lua libraries is
  4407. to use strings instead of numbers to select options.)
  4408. <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
  4409. <span class="apii">[-0, +0, <em>v</em>]</span>
  4410. <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
  4411. <p>
  4412. Grows the stack size to <code>top + sz</code> elements,
  4413. raising an error if the stack cannot grow to that size.
  4414. <code>msg</code> is an additional text to go into the error message
  4415. (or <code>NULL</code> for no additional text).
  4416. <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
  4417. <span class="apii">[-0, +0, <em>v</em>]</span>
  4418. <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre>
  4419. <p>
  4420. Checks whether the function argument <code>arg</code> is a string
  4421. and returns this string.
  4422. <p>
  4423. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  4424. so all conversions and caveats of that function apply here.
  4425. <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
  4426. <span class="apii">[-0, +0, <em>v</em>]</span>
  4427. <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre>
  4428. <p>
  4429. Checks whether the function argument <code>arg</code> has type <code>t</code>.
  4430. See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
  4431. <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
  4432. <span class="apii">[-0, +0, <em>v</em>]</span>
  4433. <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre>
  4434. <p>
  4435. Checks whether the function argument <code>arg</code> is a userdata
  4436. of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and
  4437. returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
  4438. <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
  4439. <span class="apii">[-0, +0, <em>v</em>]</span>
  4440. <pre>void luaL_checkversion (lua_State *L);</pre>
  4441. <p>
  4442. Checks whether the core running the call,
  4443. the core that created the Lua state,
  4444. and the code making the call are all using the same version of Lua.
  4445. Also checks whether the core running the call
  4446. and the core that created the Lua state
  4447. are using the same address space.
  4448. <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
  4449. <span class="apii">[-0, +?, <em>e</em>]</span>
  4450. <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
  4451. <p>
  4452. Loads and runs the given file.
  4453. It is defined as the following macro:
  4454. <pre>
  4455. (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
  4456. </pre><p>
  4457. It returns false if there are no errors
  4458. or true in case of errors.
  4459. <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
  4460. <span class="apii">[-0, +?, &ndash;]</span>
  4461. <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
  4462. <p>
  4463. Loads and runs the given string.
  4464. It is defined as the following macro:
  4465. <pre>
  4466. (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
  4467. </pre><p>
  4468. It returns false if there are no errors
  4469. or true in case of errors.
  4470. <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
  4471. <span class="apii">[-0, +0, <em>v</em>]</span>
  4472. <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
  4473. <p>
  4474. Raises an error.
  4475. The error message format is given by <code>fmt</code>
  4476. plus any extra arguments,
  4477. following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
  4478. It also adds at the beginning of the message the file name and
  4479. the line number where the error occurred,
  4480. if this information is available.
  4481. <p>
  4482. This function never returns,
  4483. but it is an idiom to use it in C&nbsp;functions
  4484. as <code>return luaL_error(<em>args</em>)</code>.
  4485. <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
  4486. <span class="apii">[-0, +3, <em>m</em>]</span>
  4487. <pre>int luaL_execresult (lua_State *L, int stat);</pre>
  4488. <p>
  4489. This function produces the return values for
  4490. process-related functions in the standard library
  4491. (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
  4492. <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
  4493. <span class="apii">[-0, +(1|3), <em>m</em>]</span>
  4494. <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre>
  4495. <p>
  4496. This function produces the return values for
  4497. file-related functions in the standard library
  4498. (<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.).
  4499. <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
  4500. <span class="apii">[-0, +(0|1), <em>m</em>]</span>
  4501. <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
  4502. <p>
  4503. Pushes onto the stack the field <code>e</code> from the metatable
  4504. of the object at index <code>obj</code> and returns the type of the pushed value.
  4505. If the object does not have a metatable,
  4506. or if the metatable does not have this field,
  4507. pushes nothing and returns <code>LUA_TNIL</code>.
  4508. <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
  4509. <span class="apii">[-0, +1, <em>m</em>]</span>
  4510. <pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre>
  4511. <p>
  4512. Pushes onto the stack the metatable associated with name <code>tname</code>
  4513. in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>)
  4514. (<b>nil</b> if there is no metatable associated with that name).
  4515. Returns the type of the pushed value.
  4516. <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
  4517. <span class="apii">[-0, +1, <em>e</em>]</span>
  4518. <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
  4519. <p>
  4520. Ensures that the value <code>t[fname]</code>,
  4521. where <code>t</code> is the value at index <code>idx</code>,
  4522. is a table,
  4523. and pushes that table onto the stack.
  4524. Returns true if it finds a previous table there
  4525. and false if it creates a new table.
  4526. <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
  4527. <span class="apii">[-0, +1, <em>m</em>]</span>
  4528. <pre>const char *luaL_gsub (lua_State *L,
  4529. const char *s,
  4530. const char *p,
  4531. const char *r);</pre>
  4532. <p>
  4533. Creates a copy of string <code>s</code> by replacing
  4534. any occurrence of the string <code>p</code>
  4535. with the string <code>r</code>.
  4536. Pushes the resulting string on the stack and returns it.
  4537. <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
  4538. <span class="apii">[-0, +0, <em>e</em>]</span>
  4539. <pre>lua_Integer luaL_len (lua_State *L, int index);</pre>
  4540. <p>
  4541. Returns the "length" of the value at the given index
  4542. as a number;
  4543. it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>).
  4544. Raises an error if the result of the operation is not an integer.
  4545. (This case only can happen through metamethods.)
  4546. <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
  4547. <span class="apii">[-0, +1, &ndash;]</span>
  4548. <pre>int luaL_loadbuffer (lua_State *L,
  4549. const char *buff,
  4550. size_t sz,
  4551. const char *name);</pre>
  4552. <p>
  4553. Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
  4554. <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
  4555. <span class="apii">[-0, +1, &ndash;]</span>
  4556. <pre>int luaL_loadbufferx (lua_State *L,
  4557. const char *buff,
  4558. size_t sz,
  4559. const char *name,
  4560. const char *mode);</pre>
  4561. <p>
  4562. Loads a buffer as a Lua chunk.
  4563. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
  4564. buffer pointed to by <code>buff</code> with size <code>sz</code>.
  4565. <p>
  4566. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
  4567. <code>name</code> is the chunk name,
  4568. used for debug information and error messages.
  4569. The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
  4570. <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
  4571. <span class="apii">[-0, +1, <em>m</em>]</span>
  4572. <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
  4573. <p>
  4574. Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
  4575. <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
  4576. <span class="apii">[-0, +1, <em>m</em>]</span>
  4577. <pre>int luaL_loadfilex (lua_State *L, const char *filename,
  4578. const char *mode);</pre>
  4579. <p>
  4580. Loads a file as a Lua chunk.
  4581. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
  4582. named <code>filename</code>.
  4583. If <code>filename</code> is <code>NULL</code>,
  4584. then it loads from the standard input.
  4585. The first line in the file is ignored if it starts with a <code>#</code>.
  4586. <p>
  4587. The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
  4588. <p>
  4589. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
  4590. but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
  4591. for file-related errors
  4592. (e.g., it cannot open or read the file).
  4593. <p>
  4594. As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
  4595. it does not run it.
  4596. <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
  4597. <span class="apii">[-0, +1, &ndash;]</span>
  4598. <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
  4599. <p>
  4600. Loads a string as a Lua chunk.
  4601. This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
  4602. the zero-terminated string <code>s</code>.
  4603. <p>
  4604. This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
  4605. <p>
  4606. Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
  4607. it does not run it.
  4608. <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
  4609. <span class="apii">[-0, +1, <em>m</em>]</span>
  4610. <pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre>
  4611. <p>
  4612. Creates a new table and registers there
  4613. the functions in list <code>l</code>.
  4614. <p>
  4615. It is implemented as the following macro:
  4616. <pre>
  4617. (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
  4618. </pre><p>
  4619. The array <code>l</code> must be the actual array,
  4620. not a pointer to it.
  4621. <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
  4622. <span class="apii">[-0, +1, <em>m</em>]</span>
  4623. <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre>
  4624. <p>
  4625. Creates a new table with a size optimized
  4626. to store all entries in the array <code>l</code>
  4627. (but does not actually store them).
  4628. It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
  4629. (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
  4630. <p>
  4631. It is implemented as a macro.
  4632. The array <code>l</code> must be the actual array,
  4633. not a pointer to it.
  4634. <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
  4635. <span class="apii">[-0, +1, <em>m</em>]</span>
  4636. <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
  4637. <p>
  4638. If the registry already has the key <code>tname</code>,
  4639. returns 0.
  4640. Otherwise,
  4641. creates a new table to be used as a metatable for userdata,
  4642. adds to this new table the pair <code>__name = tname</code>,
  4643. adds to the registry the pair <code>[tname] = new table</code>,
  4644. and returns 1.
  4645. (The entry <code>__name</code> is used by some error-reporting functions.)
  4646. <p>
  4647. In both cases pushes onto the stack the final value associated
  4648. with <code>tname</code> in the registry.
  4649. <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
  4650. <span class="apii">[-0, +0, &ndash;]</span>
  4651. <pre>lua_State *luaL_newstate (void);</pre>
  4652. <p>
  4653. Creates a new Lua state.
  4654. It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
  4655. allocator based on the standard&nbsp;C <code>realloc</code> function
  4656. and then sets a panic function (see <a href="#4.6">&sect;4.6</a>) that prints
  4657. an error message to the standard error output in case of fatal
  4658. errors.
  4659. <p>
  4660. Returns the new state,
  4661. or <code>NULL</code> if there is a memory allocation error.
  4662. <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
  4663. <span class="apii">[-0, +0, <em>e</em>]</span>
  4664. <pre>void luaL_openlibs (lua_State *L);</pre>
  4665. <p>
  4666. Opens all standard Lua libraries into the given state.
  4667. <hr><h3><a name="luaL_opt"><code>luaL_opt</code></a></h3><p>
  4668. <span class="apii">[-0, +0, <em>e</em>]</span>
  4669. <pre>T luaL_opt (L, func, arg, dflt);</pre>
  4670. <p>
  4671. This macro is defined as follows:
  4672. <pre>
  4673. (lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg)))
  4674. </pre><p>
  4675. In words, if the argument <code>arg</code> is nil or absent,
  4676. the macro results in the default <code>dflt</code>.
  4677. Otherwise, it results in the result of calling <code>func</code>
  4678. with the state <code>L</code> and the argument index <code>arg</code> as
  4679. arguments.
  4680. Note that it evaluates the expression <code>dflt</code> only if needed.
  4681. <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
  4682. <span class="apii">[-0, +0, <em>v</em>]</span>
  4683. <pre>lua_Integer luaL_optinteger (lua_State *L,
  4684. int arg,
  4685. lua_Integer d);</pre>
  4686. <p>
  4687. If the function argument <code>arg</code> is an integer
  4688. (or convertible to an integer),
  4689. returns this integer.
  4690. If this argument is absent or is <b>nil</b>,
  4691. returns <code>d</code>.
  4692. Otherwise, raises an error.
  4693. <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
  4694. <span class="apii">[-0, +0, <em>v</em>]</span>
  4695. <pre>const char *luaL_optlstring (lua_State *L,
  4696. int arg,
  4697. const char *d,
  4698. size_t *l);</pre>
  4699. <p>
  4700. If the function argument <code>arg</code> is a string,
  4701. returns this string.
  4702. If this argument is absent or is <b>nil</b>,
  4703. returns <code>d</code>.
  4704. Otherwise, raises an error.
  4705. <p>
  4706. If <code>l</code> is not <code>NULL</code>,
  4707. fills the position <code>*l</code> with the result's length.
  4708. If the result is <code>NULL</code>
  4709. (only possible when returning <code>d</code> and <code>d == NULL</code>),
  4710. its length is considered zero.
  4711. <p>
  4712. This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
  4713. so all conversions and caveats of that function apply here.
  4714. <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
  4715. <span class="apii">[-0, +0, <em>v</em>]</span>
  4716. <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre>
  4717. <p>
  4718. If the function argument <code>arg</code> is a number,
  4719. returns this number.
  4720. If this argument is absent or is <b>nil</b>,
  4721. returns <code>d</code>.
  4722. Otherwise, raises an error.
  4723. <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
  4724. <span class="apii">[-0, +0, <em>v</em>]</span>
  4725. <pre>const char *luaL_optstring (lua_State *L,
  4726. int arg,
  4727. const char *d);</pre>
  4728. <p>
  4729. If the function argument <code>arg</code> is a string,
  4730. returns this string.
  4731. If this argument is absent or is <b>nil</b>,
  4732. returns <code>d</code>.
  4733. Otherwise, raises an error.
  4734. <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
  4735. <span class="apii">[-?, +?, <em>m</em>]</span>
  4736. <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
  4737. <p>
  4738. Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
  4739. with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
  4740. <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
  4741. <span class="apii">[-?, +?, <em>m</em>]</span>
  4742. <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre>
  4743. <p>
  4744. Returns an address to a space of size <code>sz</code>
  4745. where you can copy a string to be added to buffer <code>B</code>
  4746. (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
  4747. After copying the string into this space you must call
  4748. <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
  4749. it to the buffer.
  4750. <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
  4751. <span class="apii">[-?, +1, <em>m</em>]</span>
  4752. <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
  4753. <p>
  4754. Finishes the use of buffer <code>B</code> leaving the final string on
  4755. the top of the stack.
  4756. <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
  4757. <span class="apii">[-?, +1, <em>m</em>]</span>
  4758. <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre>
  4759. <p>
  4760. Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>.
  4761. <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
  4762. <span class="apii">[-1, +0, <em>m</em>]</span>
  4763. <pre>int luaL_ref (lua_State *L, int t);</pre>
  4764. <p>
  4765. Creates and returns a <em>reference</em>,
  4766. in the table at index <code>t</code>,
  4767. for the object at the top of the stack (and pops the object).
  4768. <p>
  4769. A reference is a unique integer key.
  4770. As long as you do not manually add integer keys into table <code>t</code>,
  4771. <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
  4772. You can retrieve an object referred by reference <code>r</code>
  4773. by calling <code>lua_rawgeti(L, t, r)</code>.
  4774. Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object.
  4775. <p>
  4776. If the object at the top of the stack is <b>nil</b>,
  4777. <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
  4778. The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
  4779. from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
  4780. <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
  4781. <pre>typedef struct luaL_Reg {
  4782. const char *name;
  4783. lua_CFunction func;
  4784. } luaL_Reg;</pre>
  4785. <p>
  4786. Type for arrays of functions to be registered by
  4787. <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
  4788. <code>name</code> is the function name and <code>func</code> is a pointer to
  4789. the function.
  4790. Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry
  4791. in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
  4792. <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
  4793. <span class="apii">[-0, +1, <em>e</em>]</span>
  4794. <pre>void luaL_requiref (lua_State *L, const char *modname,
  4795. lua_CFunction openf, int glb);</pre>
  4796. <p>
  4797. If <code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</code></a>,
  4798. calls function <code>openf</code> with string <code>modname</code> as an argument
  4799. and sets the call result in <code>package.loaded[modname]</code>,
  4800. as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
  4801. <p>
  4802. If <code>glb</code> is true,
  4803. also stores the module into global <code>modname</code>.
  4804. <p>
  4805. Leaves a copy of the module on the stack.
  4806. <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
  4807. <span class="apii">[-nup, +0, <em>m</em>]</span>
  4808. <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre>
  4809. <p>
  4810. Registers all functions in the array <code>l</code>
  4811. (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
  4812. (below optional upvalues, see next).
  4813. <p>
  4814. When <code>nup</code> is not zero,
  4815. all functions are created sharing <code>nup</code> upvalues,
  4816. which must be previously pushed on the stack
  4817. on top of the library table.
  4818. These values are popped from the stack after the registration.
  4819. <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
  4820. <span class="apii">[-0, +0, &ndash;]</span>
  4821. <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre>
  4822. <p>
  4823. Sets the metatable of the object at the top of the stack
  4824. as the metatable associated with name <code>tname</code>
  4825. in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
  4826. <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3>
  4827. <pre>typedef struct luaL_Stream {
  4828. FILE *f;
  4829. lua_CFunction closef;
  4830. } luaL_Stream;</pre>
  4831. <p>
  4832. The standard representation for file handles,
  4833. which is used by the standard I/O library.
  4834. <p>
  4835. A file handle is implemented as a full userdata,
  4836. with a metatable called <code>LUA_FILEHANDLE</code>
  4837. (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name).
  4838. The metatable is created by the I/O library
  4839. (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
  4840. <p>
  4841. This userdata must start with the structure <code>luaL_Stream</code>;
  4842. it can contain other data after this initial structure.
  4843. Field <code>f</code> points to the corresponding C stream
  4844. (or it can be <code>NULL</code> to indicate an incompletely created handle).
  4845. Field <code>closef</code> points to a Lua function
  4846. that will be called to close the stream
  4847. when the handle is closed or collected;
  4848. this function receives the file handle as its sole argument and
  4849. must return either <b>true</b> (in case of success)
  4850. or <b>nil</b> plus an error message (in case of error).
  4851. Once Lua calls this field,
  4852. it changes the field value to <code>NULL</code>
  4853. to signal that the handle is closed.
  4854. <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
  4855. <span class="apii">[-0, +0, <em>m</em>]</span>
  4856. <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre>
  4857. <p>
  4858. This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
  4859. except that, when the test fails,
  4860. it returns <code>NULL</code> instead of raising an error.
  4861. <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
  4862. <span class="apii">[-0, +1, <em>e</em>]</span>
  4863. <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre>
  4864. <p>
  4865. Converts any Lua value at the given index to a C&nbsp;string
  4866. in a reasonable format.
  4867. The resulting string is pushed onto the stack and also
  4868. returned by the function.
  4869. If <code>len</code> is not <code>NULL</code>,
  4870. the function also sets <code>*len</code> with the string length.
  4871. <p>
  4872. If the value has a metatable with a <code>__tostring</code> field,
  4873. then <code>luaL_tolstring</code> calls the corresponding metamethod
  4874. with the value as argument,
  4875. and uses the result of the call as its result.
  4876. <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
  4877. <span class="apii">[-0, +1, <em>m</em>]</span>
  4878. <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
  4879. int level);</pre>
  4880. <p>
  4881. Creates and pushes a traceback of the stack <code>L1</code>.
  4882. If <code>msg</code> is not <code>NULL</code> it is appended
  4883. at the beginning of the traceback.
  4884. The <code>level</code> parameter tells at which level
  4885. to start the traceback.
  4886. <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
  4887. <span class="apii">[-0, +0, &ndash;]</span>
  4888. <pre>const char *luaL_typename (lua_State *L, int index);</pre>
  4889. <p>
  4890. Returns the name of the type of the value at the given index.
  4891. <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
  4892. <span class="apii">[-0, +0, &ndash;]</span>
  4893. <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
  4894. <p>
  4895. Releases reference <code>ref</code> from the table at index <code>t</code>
  4896. (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
  4897. The entry is removed from the table,
  4898. so that the referred object can be collected.
  4899. The reference <code>ref</code> is also freed to be used again.
  4900. <p>
  4901. 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>,
  4902. <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
  4903. <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
  4904. <span class="apii">[-0, +1, <em>m</em>]</span>
  4905. <pre>void luaL_where (lua_State *L, int lvl);</pre>
  4906. <p>
  4907. Pushes onto the stack a string identifying the current position
  4908. of the control at level <code>lvl</code> in the call stack.
  4909. Typically this string has the following format:
  4910. <pre>
  4911. <em>chunkname</em>:<em>currentline</em>:
  4912. </pre><p>
  4913. Level&nbsp;0 is the running function,
  4914. level&nbsp;1 is the function that called the running function,
  4915. etc.
  4916. <p>
  4917. This function is used to build a prefix for error messages.
  4918. <h1>6 &ndash; <a name="6">Standard Libraries</a></h1>
  4919. <p>
  4920. The standard Lua libraries provide useful functions
  4921. that are implemented directly through the C&nbsp;API.
  4922. Some of these functions provide essential services to the language
  4923. (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>);
  4924. others provide access to "outside" services (e.g., I/O);
  4925. and others could be implemented in Lua itself,
  4926. but are quite useful or have critical performance requirements that
  4927. deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>).
  4928. <p>
  4929. All libraries are implemented through the official C&nbsp;API
  4930. and are provided as separate C&nbsp;modules.
  4931. Currently, Lua has the following standard libraries:
  4932. <ul>
  4933. <li>basic library (<a href="#6.1">&sect;6.1</a>);</li>
  4934. <li>coroutine library (<a href="#6.2">&sect;6.2</a>);</li>
  4935. <li>package library (<a href="#6.3">&sect;6.3</a>);</li>
  4936. <li>string manipulation (<a href="#6.4">&sect;6.4</a>);</li>
  4937. <li>basic UTF-8 support (<a href="#6.5">&sect;6.5</a>);</li>
  4938. <li>table manipulation (<a href="#6.6">&sect;6.6</a>);</li>
  4939. <li>mathematical functions (<a href="#6.7">&sect;6.7</a>) (sin, log, etc.);</li>
  4940. <li>input and output (<a href="#6.8">&sect;6.8</a>);</li>
  4941. <li>operating system facilities (<a href="#6.9">&sect;6.9</a>);</li>
  4942. <li>debug facilities (<a href="#6.10">&sect;6.10</a>).</li>
  4943. </ul><p>
  4944. Except for the basic and the package libraries,
  4945. each library provides all its functions as fields of a global table
  4946. or as methods of its objects.
  4947. <p>
  4948. To have access to these libraries,
  4949. the C&nbsp;host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function,
  4950. which opens all standard libraries.
  4951. Alternatively,
  4952. the host program can open them individually by using
  4953. <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call
  4954. <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library),
  4955. <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library),
  4956. <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library),
  4957. <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library),
  4958. <a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF8 library),
  4959. <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library),
  4960. <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library),
  4961. <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library),
  4962. <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library),
  4963. and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library).
  4964. These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>.
  4965. <h2>6.1 &ndash; <a name="6.1">Basic Functions</a></h2>
  4966. <p>
  4967. The basic library provides core functions to Lua.
  4968. If you do not include this library in your application,
  4969. you should check carefully whether you need to provide
  4970. implementations for some of its facilities.
  4971. <p>
  4972. <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
  4973. <p>
  4974. Calls <a href="#pdf-error"><code>error</code></a> if
  4975. the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
  4976. otherwise, returns all its arguments.
  4977. In case of error,
  4978. <code>message</code> is the error object;
  4979. when absent, it defaults to "<code>assertion failed!</code>"
  4980. <p>
  4981. <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3>
  4982. <p>
  4983. This function is a generic interface to the garbage collector.
  4984. It performs different functions according to its first argument, <code>opt</code>:
  4985. <ul>
  4986. <li><b>"<code>collect</code>": </b>
  4987. performs a full garbage-collection cycle.
  4988. This is the default option.
  4989. </li>
  4990. <li><b>"<code>stop</code>": </b>
  4991. stops automatic execution of the garbage collector.
  4992. The collector will run only when explicitly invoked,
  4993. until a call to restart it.
  4994. </li>
  4995. <li><b>"<code>restart</code>": </b>
  4996. restarts automatic execution of the garbage collector.
  4997. </li>
  4998. <li><b>"<code>count</code>": </b>
  4999. returns the total memory in use by Lua in Kbytes.
  5000. The value has a fractional part,
  5001. so that it multiplied by 1024
  5002. gives the exact number of bytes in use by Lua
  5003. (except for overflows).
  5004. </li>
  5005. <li><b>"<code>step</code>": </b>
  5006. performs a garbage-collection step.
  5007. The step "size" is controlled by <code>arg</code>.
  5008. With a zero value,
  5009. the collector will perform one basic (indivisible) step.
  5010. For non-zero values,
  5011. the collector will perform as if that amount of memory
  5012. (in KBytes) had been allocated by Lua.
  5013. Returns <b>true</b> if the step finished a collection cycle.
  5014. </li>
  5015. <li><b>"<code>setpause</code>": </b>
  5016. sets <code>arg</code> as the new value for the <em>pause</em> of
  5017. the collector (see <a href="#2.5">&sect;2.5</a>).
  5018. Returns the previous value for <em>pause</em>.
  5019. </li>
  5020. <li><b>"<code>setstepmul</code>": </b>
  5021. sets <code>arg</code> as the new value for the <em>step multiplier</em> of
  5022. the collector (see <a href="#2.5">&sect;2.5</a>).
  5023. Returns the previous value for <em>step</em>.
  5024. </li>
  5025. <li><b>"<code>isrunning</code>": </b>
  5026. returns a boolean that tells whether the collector is running
  5027. (i.e., not stopped).
  5028. </li>
  5029. </ul>
  5030. <p>
  5031. <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3>
  5032. Opens the named file and executes its contents as a Lua chunk.
  5033. When called without arguments,
  5034. <code>dofile</code> executes the contents of the standard input (<code>stdin</code>).
  5035. Returns all values returned by the chunk.
  5036. In case of errors, <code>dofile</code> propagates the error
  5037. to its caller (that is, <code>dofile</code> does not run in protected mode).
  5038. <p>
  5039. <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
  5040. Terminates the last protected function called
  5041. and returns <code>message</code> as the error object.
  5042. Function <code>error</code> never returns.
  5043. <p>
  5044. Usually, <code>error</code> adds some information about the error position
  5045. at the beginning of the message, if the message is a string.
  5046. The <code>level</code> argument specifies how to get the error position.
  5047. With level&nbsp;1 (the default), the error position is where the
  5048. <code>error</code> function was called.
  5049. Level&nbsp;2 points the error to where the function
  5050. that called <code>error</code> was called; and so on.
  5051. Passing a level&nbsp;0 avoids the addition of error position information
  5052. to the message.
  5053. <p>
  5054. <hr><h3><a name="pdf-_G"><code>_G</code></a></h3>
  5055. A global variable (not a function) that
  5056. holds the global environment (see <a href="#2.2">&sect;2.2</a>).
  5057. Lua itself does not use this variable;
  5058. changing its value does not affect any environment,
  5059. nor vice versa.
  5060. <p>
  5061. <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3>
  5062. <p>
  5063. If <code>object</code> does not have a metatable, returns <b>nil</b>.
  5064. Otherwise,
  5065. if the object's metatable has a <code>__metatable</code> field,
  5066. returns the associated value.
  5067. Otherwise, returns the metatable of the given object.
  5068. <p>
  5069. <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3>
  5070. <p>
  5071. Returns three values (an iterator function, the table <code>t</code>, and 0)
  5072. so that the construction
  5073. <pre>
  5074. for i,v in ipairs(t) do <em>body</em> end
  5075. </pre><p>
  5076. will iterate over the key&ndash;value pairs
  5077. (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
  5078. up to the first nil value.
  5079. <p>
  5080. <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3>
  5081. <p>
  5082. Loads a chunk.
  5083. <p>
  5084. If <code>chunk</code> is a string, the chunk is this string.
  5085. If <code>chunk</code> is a function,
  5086. <code>load</code> calls it repeatedly to get the chunk pieces.
  5087. Each call to <code>chunk</code> must return a string that concatenates
  5088. with previous results.
  5089. A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
  5090. <p>
  5091. If there are no syntactic errors,
  5092. returns the compiled chunk as a function;
  5093. otherwise, returns <b>nil</b> plus the error message.
  5094. <p>
  5095. If the resulting function has upvalues,
  5096. the first upvalue is set to the value of <code>env</code>,
  5097. if that parameter is given,
  5098. or to the value of the global environment.
  5099. Other upvalues are initialized with <b>nil</b>.
  5100. (When you load a main chunk,
  5101. the resulting function will always have exactly one upvalue,
  5102. the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
  5103. However,
  5104. when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>),
  5105. the resulting function can have an arbitrary number of upvalues.)
  5106. All upvalues are fresh, that is,
  5107. they are not shared with any other function.
  5108. <p>
  5109. <code>chunkname</code> is used as the name of the chunk for error messages
  5110. and debug information (see <a href="#4.9">&sect;4.9</a>).
  5111. When absent,
  5112. it defaults to <code>chunk</code>, if <code>chunk</code> is a string,
  5113. or to "<code>=(load)</code>" otherwise.
  5114. <p>
  5115. The string <code>mode</code> controls whether the chunk can be text or binary
  5116. (that is, a precompiled chunk).
  5117. It may be the string "<code>b</code>" (only binary chunks),
  5118. "<code>t</code>" (only text chunks),
  5119. or "<code>bt</code>" (both binary and text).
  5120. The default is "<code>bt</code>".
  5121. <p>
  5122. Lua does not check the consistency of binary chunks.
  5123. Maliciously crafted binary chunks can crash
  5124. the interpreter.
  5125. <p>
  5126. <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3>
  5127. <p>
  5128. Similar to <a href="#pdf-load"><code>load</code></a>,
  5129. but gets the chunk from file <code>filename</code>
  5130. or from the standard input,
  5131. if no file name is given.
  5132. <p>
  5133. <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
  5134. <p>
  5135. Allows a program to traverse all fields of a table.
  5136. Its first argument is a table and its second argument
  5137. is an index in this table.
  5138. <code>next</code> returns the next index of the table
  5139. and its associated value.
  5140. When called with <b>nil</b> as its second argument,
  5141. <code>next</code> returns an initial index
  5142. and its associated value.
  5143. When called with the last index,
  5144. or with <b>nil</b> in an empty table,
  5145. <code>next</code> returns <b>nil</b>.
  5146. If the second argument is absent, then it is interpreted as <b>nil</b>.
  5147. In particular,
  5148. you can use <code>next(t)</code> to check whether a table is empty.
  5149. <p>
  5150. The order in which the indices are enumerated is not specified,
  5151. <em>even for numeric indices</em>.
  5152. (To traverse a table in numerical order,
  5153. use a numerical <b>for</b>.)
  5154. <p>
  5155. The behavior of <code>next</code> is undefined if,
  5156. during the traversal,
  5157. you assign any value to a non-existent field in the table.
  5158. You may however modify existing fields.
  5159. In particular, you may clear existing fields.
  5160. <p>
  5161. <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3>
  5162. <p>
  5163. If <code>t</code> has a metamethod <code>__pairs</code>,
  5164. calls it with <code>t</code> as argument and returns the first three
  5165. results from the call.
  5166. <p>
  5167. Otherwise,
  5168. returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>,
  5169. so that the construction
  5170. <pre>
  5171. for k,v in pairs(t) do <em>body</em> end
  5172. </pre><p>
  5173. will iterate over all key&ndash;value pairs of table <code>t</code>.
  5174. <p>
  5175. See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
  5176. the table during its traversal.
  5177. <p>
  5178. <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, &middot;&middot;&middot;])</code></a></h3>
  5179. <p>
  5180. Calls function <code>f</code> with
  5181. the given arguments in <em>protected mode</em>.
  5182. This means that any error inside&nbsp;<code>f</code> is not propagated;
  5183. instead, <code>pcall</code> catches the error
  5184. and returns a status code.
  5185. Its first result is the status code (a boolean),
  5186. which is true if the call succeeds without errors.
  5187. In such case, <code>pcall</code> also returns all results from the call,
  5188. after this first result.
  5189. In case of any error, <code>pcall</code> returns <b>false</b> plus the error message.
  5190. <p>
  5191. <hr><h3><a name="pdf-print"><code>print (&middot;&middot;&middot;)</code></a></h3>
  5192. Receives any number of arguments
  5193. and prints their values to <code>stdout</code>,
  5194. using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string.
  5195. <code>print</code> is not intended for formatted output,
  5196. but only as a quick way to show a value,
  5197. for instance for debugging.
  5198. For complete control over the output,
  5199. use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
  5200. <p>
  5201. <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3>
  5202. Checks whether <code>v1</code> is equal to <code>v2</code>,
  5203. without invoking the <code>__eq</code> metamethod.
  5204. Returns a boolean.
  5205. <p>
  5206. <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3>
  5207. Gets the real value of <code>table[index]</code>,
  5208. without invoking the <code>__index</code> metamethod.
  5209. <code>table</code> must be a table;
  5210. <code>index</code> may be any value.
  5211. <p>
  5212. <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3>
  5213. Returns the length of the object <code>v</code>,
  5214. which must be a table or a string,
  5215. without invoking the <code>__len</code> metamethod.
  5216. Returns an integer.
  5217. <p>
  5218. <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
  5219. Sets the real value of <code>table[index]</code> to <code>value</code>,
  5220. without invoking the <code>__newindex</code> metamethod.
  5221. <code>table</code> must be a table,
  5222. <code>index</code> any value different from <b>nil</b> and NaN,
  5223. and <code>value</code> any Lua value.
  5224. <p>
  5225. This function returns <code>table</code>.
  5226. <p>
  5227. <hr><h3><a name="pdf-select"><code>select (index, &middot;&middot;&middot;)</code></a></h3>
  5228. <p>
  5229. If <code>index</code> is a number,
  5230. returns all arguments after argument number <code>index</code>;
  5231. a negative number indexes from the end (-1 is the last argument).
  5232. Otherwise, <code>index</code> must be the string <code>"#"</code>,
  5233. and <code>select</code> returns the total number of extra arguments it received.
  5234. <p>
  5235. <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3>
  5236. <p>
  5237. Sets the metatable for the given table.
  5238. (To change the metatable of other types from Lua code,
  5239. you must use the debug library (<a href="#6.10">&sect;6.10</a>).)
  5240. If <code>metatable</code> is <b>nil</b>,
  5241. removes the metatable of the given table.
  5242. If the original metatable has a <code>__metatable</code> field,
  5243. raises an error.
  5244. <p>
  5245. This function returns <code>table</code>.
  5246. <p>
  5247. <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
  5248. <p>
  5249. When called with no <code>base</code>,
  5250. <code>tonumber</code> tries to convert its argument to a number.
  5251. If the argument is already a number or
  5252. a string convertible to a number,
  5253. then <code>tonumber</code> returns this number;
  5254. otherwise, it returns <b>nil</b>.
  5255. <p>
  5256. The conversion of strings can result in integers or floats,
  5257. according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
  5258. (The string may have leading and trailing spaces and a sign.)
  5259. <p>
  5260. When called with <code>base</code>,
  5261. then <code>e</code> must be a string to be interpreted as
  5262. an integer numeral in that base.
  5263. The base may be any integer between 2 and 36, inclusive.
  5264. In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
  5265. represents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,
  5266. with '<code>Z</code>' representing 35.
  5267. If the string <code>e</code> is not a valid numeral in the given base,
  5268. the function returns <b>nil</b>.
  5269. <p>
  5270. <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3>
  5271. Receives a value of any type and
  5272. converts it to a string in a human-readable format.
  5273. (For complete control of how numbers are converted,
  5274. use <a href="#pdf-string.format"><code>string.format</code></a>.)
  5275. <p>
  5276. If the metatable of <code>v</code> has a <code>__tostring</code> field,
  5277. then <code>tostring</code> calls the corresponding value
  5278. with <code>v</code> as argument,
  5279. and uses the result of the call as its result.
  5280. <p>
  5281. <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3>
  5282. Returns the type of its only argument, coded as a string.
  5283. The possible results of this function are
  5284. "<code>nil</code>" (a string, not the value <b>nil</b>),
  5285. "<code>number</code>",
  5286. "<code>string</code>",
  5287. "<code>boolean</code>",
  5288. "<code>table</code>",
  5289. "<code>function</code>",
  5290. "<code>thread</code>",
  5291. and "<code>userdata</code>".
  5292. <p>
  5293. <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3>
  5294. <p>
  5295. A global variable (not a function) that
  5296. holds a string containing the running Lua version.
  5297. The current value of this variable is "<code>Lua 5.3</code>".
  5298. <p>
  5299. <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, &middot;&middot;&middot;])</code></a></h3>
  5300. <p>
  5301. This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
  5302. except that it sets a new message handler <code>msgh</code>.
  5303. <h2>6.2 &ndash; <a name="6.2">Coroutine Manipulation</a></h2>
  5304. <p>
  5305. This library comprises the operations to manipulate coroutines,
  5306. which come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>.
  5307. See <a href="#2.6">&sect;2.6</a> for a general description of coroutines.
  5308. <p>
  5309. <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3>
  5310. <p>
  5311. Creates a new coroutine, with body <code>f</code>.
  5312. <code>f</code> must be a function.
  5313. Returns this new coroutine,
  5314. an object with type <code>"thread"</code>.
  5315. <p>
  5316. <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3>
  5317. <p>
  5318. Returns true when the running coroutine can yield.
  5319. <p>
  5320. A running coroutine is yieldable if it is not the main thread and
  5321. it is not inside a non-yieldable C&nbsp;function.
  5322. <p>
  5323. <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>
  5324. <p>
  5325. Starts or continues the execution of coroutine <code>co</code>.
  5326. The first time you resume a coroutine,
  5327. it starts running its body.
  5328. The values <code>val1</code>, ... are passed
  5329. as the arguments to the body function.
  5330. If the coroutine has yielded,
  5331. <code>resume</code> restarts it;
  5332. the values <code>val1</code>, ... are passed
  5333. as the results from the yield.
  5334. <p>
  5335. If the coroutine runs without any errors,
  5336. <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>
  5337. (when the coroutine yields) or any values returned by the body function
  5338. (when the coroutine terminates).
  5339. If there is any error,
  5340. <code>resume</code> returns <b>false</b> plus the error message.
  5341. <p>
  5342. <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3>
  5343. <p>
  5344. Returns the running coroutine plus a boolean,
  5345. true when the running coroutine is the main one.
  5346. <p>
  5347. <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3>
  5348. <p>
  5349. Returns the status of coroutine <code>co</code>, as a string:
  5350. <code>"running"</code>,
  5351. if the coroutine is running (that is, it called <code>status</code>);
  5352. <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>,
  5353. or if it has not started running yet;
  5354. <code>"normal"</code> if the coroutine is active but not running
  5355. (that is, it has resumed another coroutine);
  5356. and <code>"dead"</code> if the coroutine has finished its body function,
  5357. or if it has stopped with an error.
  5358. <p>
  5359. <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3>
  5360. <p>
  5361. Creates a new coroutine, with body <code>f</code>.
  5362. <code>f</code> must be a function.
  5363. Returns a function that resumes the coroutine each time it is called.
  5364. Any arguments passed to the function behave as the
  5365. extra arguments to <code>resume</code>.
  5366. Returns the same values returned by <code>resume</code>,
  5367. except the first boolean.
  5368. In case of error, propagates the error.
  5369. <p>
  5370. <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (&middot;&middot;&middot;)</code></a></h3>
  5371. <p>
  5372. Suspends the execution of the calling coroutine.
  5373. Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>.
  5374. <h2>6.3 &ndash; <a name="6.3">Modules</a></h2>
  5375. <p>
  5376. The package library provides basic
  5377. facilities for loading modules in Lua.
  5378. It exports one function directly in the global environment:
  5379. <a href="#pdf-require"><code>require</code></a>.
  5380. Everything else is exported in a table <a name="pdf-package"><code>package</code></a>.
  5381. <p>
  5382. <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
  5383. <p>
  5384. Loads the given module.
  5385. The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table
  5386. to determine whether <code>modname</code> is already loaded.
  5387. If it is, then <code>require</code> returns the value stored
  5388. at <code>package.loaded[modname]</code>.
  5389. Otherwise, it tries to find a <em>loader</em> for the module.
  5390. <p>
  5391. To find a loader,
  5392. <code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</code></a> sequence.
  5393. By changing this sequence,
  5394. we can change how <code>require</code> looks for a module.
  5395. The following explanation is based on the default configuration
  5396. for <a href="#pdf-package.searchers"><code>package.searchers</code></a>.
  5397. <p>
  5398. First <code>require</code> queries <code>package.preload[modname]</code>.
  5399. If it has a value,
  5400. this value (which must be a function) is the loader.
  5401. Otherwise <code>require</code> searches for a Lua loader using the
  5402. path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
  5403. If that also fails, it searches for a C&nbsp;loader using the
  5404. path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
  5405. If that also fails,
  5406. it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>).
  5407. <p>
  5408. Once a loader is found,
  5409. <code>require</code> calls the loader with two arguments:
  5410. <code>modname</code> and an extra value dependent on how it got the loader.
  5411. (If the loader came from a file,
  5412. this extra value is the file name.)
  5413. If the loader returns any non-nil value,
  5414. <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
  5415. If the loader does not return a non-nil value and
  5416. has not assigned any value to <code>package.loaded[modname]</code>,
  5417. then <code>require</code> assigns <b>true</b> to this entry.
  5418. In any case, <code>require</code> returns the
  5419. final value of <code>package.loaded[modname]</code>.
  5420. <p>
  5421. If there is any error loading or running the module,
  5422. or if it cannot find any loader for the module,
  5423. then <code>require</code> raises an error.
  5424. <p>
  5425. <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3>
  5426. <p>
  5427. A string describing some compile-time configurations for packages.
  5428. This string is a sequence of lines:
  5429. <ul>
  5430. <li>The first line is the directory separator string.
  5431. Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li>
  5432. <li>The second line is the character that separates templates in a path.
  5433. Default is '<code>;</code>'.</li>
  5434. <li>The third line is the string that marks the
  5435. substitution points in a template.
  5436. Default is '<code>?</code>'.</li>
  5437. <li>The fourth line is a string that, in a path in Windows,
  5438. is replaced by the executable's directory.
  5439. Default is '<code>!</code>'.</li>
  5440. <li>The fifth line is a mark to ignore all text after it
  5441. when building the <code>luaopen_</code> function name.
  5442. Default is '<code>-</code>'.</li>
  5443. </ul>
  5444. <p>
  5445. <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
  5446. <p>
  5447. The path used by <a href="#pdf-require"><code>require</code></a> to search for a C&nbsp;loader.
  5448. <p>
  5449. Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
  5450. it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
  5451. using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>,
  5452. or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>,
  5453. or a default path defined in <code>luaconf.h</code>.
  5454. <p>
  5455. <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
  5456. <p>
  5457. A table used by <a href="#pdf-require"><code>require</code></a> to control which
  5458. modules are already loaded.
  5459. When you require a module <code>modname</code> and
  5460. <code>package.loaded[modname]</code> is not false,
  5461. <a href="#pdf-require"><code>require</code></a> simply returns the value stored there.
  5462. <p>
  5463. This variable is only a reference to the real table;
  5464. assignments to this variable do not change the
  5465. table used by <a href="#pdf-require"><code>require</code></a>.
  5466. <p>
  5467. <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
  5468. <p>
  5469. Dynamically links the host program with the C&nbsp;library <code>libname</code>.
  5470. <p>
  5471. If <code>funcname</code> is "<code>*</code>",
  5472. then it only links with the library,
  5473. making the symbols exported by the library
  5474. available to other dynamically linked libraries.
  5475. Otherwise,
  5476. it looks for a function <code>funcname</code> inside the library
  5477. and returns this function as a C&nbsp;function.
  5478. So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype
  5479. (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
  5480. <p>
  5481. This is a low-level function.
  5482. It completely bypasses the package and module system.
  5483. Unlike <a href="#pdf-require"><code>require</code></a>,
  5484. it does not perform any path searching and
  5485. does not automatically adds extensions.
  5486. <code>libname</code> must be the complete file name of the C&nbsp;library,
  5487. including if necessary a path and an extension.
  5488. <code>funcname</code> must be the exact name exported by the C&nbsp;library
  5489. (which may depend on the C&nbsp;compiler and linker used).
  5490. <p>
  5491. This function is not supported by Standard&nbsp;C.
  5492. As such, it is only available on some platforms
  5493. (Windows, Linux, Mac OS X, Solaris, BSD,
  5494. plus other Unix systems that support the <code>dlfcn</code> standard).
  5495. <p>
  5496. <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
  5497. <p>
  5498. The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader.
  5499. <p>
  5500. At start-up, Lua initializes this variable with
  5501. the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
  5502. the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
  5503. with a default path defined in <code>luaconf.h</code>,
  5504. if those environment variables are not defined.
  5505. Any "<code>;;</code>" in the value of the environment variable
  5506. is replaced by the default path.
  5507. <p>
  5508. <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
  5509. <p>
  5510. A table to store loaders for specific modules
  5511. (see <a href="#pdf-require"><code>require</code></a>).
  5512. <p>
  5513. This variable is only a reference to the real table;
  5514. assignments to this variable do not change the
  5515. table used by <a href="#pdf-require"><code>require</code></a>.
  5516. <p>
  5517. <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3>
  5518. <p>
  5519. A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules.
  5520. <p>
  5521. Each entry in this table is a <em>searcher function</em>.
  5522. When looking for a module,
  5523. <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
  5524. with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
  5525. sole parameter.
  5526. The function can return another function (the module <em>loader</em>)
  5527. plus an extra value that will be passed to that loader,
  5528. or a string explaining why it did not find that module
  5529. (or <b>nil</b> if it has nothing to say).
  5530. <p>
  5531. Lua initializes this table with four searcher functions.
  5532. <p>
  5533. The first searcher simply looks for a loader in the
  5534. <a href="#pdf-package.preload"><code>package.preload</code></a> table.
  5535. <p>
  5536. The second searcher looks for a loader as a Lua library,
  5537. using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
  5538. The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
  5539. <p>
  5540. The third searcher looks for a loader as a C&nbsp;library,
  5541. using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
  5542. Again,
  5543. the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
  5544. For instance,
  5545. if the C&nbsp;path is the string
  5546. <pre>
  5547. "./?.so;./?.dll;/usr/local/?/init.so"
  5548. </pre><p>
  5549. the searcher for module <code>foo</code>
  5550. will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,
  5551. and <code>/usr/local/foo/init.so</code>, in that order.
  5552. Once it finds a C&nbsp;library,
  5553. this searcher first uses a dynamic link facility to link the
  5554. application with the library.
  5555. Then it tries to find a C&nbsp;function inside the library to
  5556. be used as the loader.
  5557. The name of this C&nbsp;function is the string "<code>luaopen_</code>"
  5558. concatenated with a copy of the module name where each dot
  5559. is replaced by an underscore.
  5560. Moreover, if the module name has a hyphen,
  5561. its suffix after (and including) the first hyphen is removed.
  5562. For instance, if the module name is <code>a.b.c-v2.1</code>,
  5563. the function name will be <code>luaopen_a_b_c</code>.
  5564. <p>
  5565. The fourth searcher tries an <em>all-in-one loader</em>.
  5566. It searches the C&nbsp;path for a library for
  5567. the root name of the given module.
  5568. For instance, when requiring <code>a.b.c</code>,
  5569. it will search for a C&nbsp;library for <code>a</code>.
  5570. If found, it looks into it for an open function for
  5571. the submodule;
  5572. in our example, that would be <code>luaopen_a_b_c</code>.
  5573. With this facility, a package can pack several C&nbsp;submodules
  5574. into one single library,
  5575. with each submodule keeping its original open function.
  5576. <p>
  5577. All searchers except the first one (preload) return as the extra value
  5578. the file name where the module was found,
  5579. as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
  5580. The first searcher returns no extra value.
  5581. <p>
  5582. <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3>
  5583. <p>
  5584. Searches for the given <code>name</code> in the given <code>path</code>.
  5585. <p>
  5586. A path is a string containing a sequence of
  5587. <em>templates</em> separated by semicolons.
  5588. For each template,
  5589. the function replaces each interrogation mark (if any)
  5590. in the template with a copy of <code>name</code>
  5591. wherein all occurrences of <code>sep</code>
  5592. (a dot, by default)
  5593. were replaced by <code>rep</code>
  5594. (the system's directory separator, by default),
  5595. and then tries to open the resulting file name.
  5596. <p>
  5597. For instance, if the path is the string
  5598. <pre>
  5599. "./?.lua;./?.lc;/usr/local/?/init.lua"
  5600. </pre><p>
  5601. the search for the name <code>foo.a</code>
  5602. will try to open the files
  5603. <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and
  5604. <code>/usr/local/foo/a/init.lua</code>, in that order.
  5605. <p>
  5606. Returns the resulting name of the first file that it can
  5607. open in read mode (after closing the file),
  5608. or <b>nil</b> plus an error message if none succeeds.
  5609. (This error message lists all file names it tried to open.)
  5610. <h2>6.4 &ndash; <a name="6.4">String Manipulation</a></h2>
  5611. <p>
  5612. This library provides generic functions for string manipulation,
  5613. such as finding and extracting substrings, and pattern matching.
  5614. When indexing a string in Lua, the first character is at position&nbsp;1
  5615. (not at&nbsp;0, as in C).
  5616. Indices are allowed to be negative and are interpreted as indexing backwards,
  5617. from the end of the string.
  5618. Thus, the last character is at position -1, and so on.
  5619. <p>
  5620. The string library provides all its functions inside the table
  5621. <a name="pdf-string"><code>string</code></a>.
  5622. It also sets a metatable for strings
  5623. where the <code>__index</code> field points to the <code>string</code> table.
  5624. Therefore, you can use the string functions in object-oriented style.
  5625. For instance, <code>string.byte(s,i)</code>
  5626. can be written as <code>s:byte(i)</code>.
  5627. <p>
  5628. The string library assumes one-byte character encodings.
  5629. <p>
  5630. <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
  5631. Returns the internal numeric codes of the characters <code>s[i]</code>,
  5632. <code>s[i+1]</code>, ..., <code>s[j]</code>.
  5633. The default value for <code>i</code> is&nbsp;1;
  5634. the default value for <code>j</code> is&nbsp;<code>i</code>.
  5635. These indices are corrected
  5636. following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>.
  5637. <p>
  5638. Numeric codes are not necessarily portable across platforms.
  5639. <p>
  5640. <hr><h3><a name="pdf-string.char"><code>string.char (&middot;&middot;&middot;)</code></a></h3>
  5641. Receives zero or more integers.
  5642. Returns a string with length equal to the number of arguments,
  5643. in which each character has the internal numeric code equal
  5644. to its corresponding argument.
  5645. <p>
  5646. Numeric codes are not necessarily portable across platforms.
  5647. <p>
  5648. <hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3>
  5649. <p>
  5650. Returns a string containing a binary representation
  5651. (a <em>binary chunk</em>)
  5652. of the given function,
  5653. so that a later <a href="#pdf-load"><code>load</code></a> on this string returns
  5654. a copy of the function (but with new upvalues).
  5655. If <code>strip</code> is a true value,
  5656. the binary representation may not include all debug information
  5657. about the function,
  5658. to save space.
  5659. <p>
  5660. Functions with upvalues have only their number of upvalues saved.
  5661. When (re)loaded,
  5662. those upvalues receive fresh instances containing <b>nil</b>.
  5663. (You can use the debug library to serialize
  5664. and reload the upvalues of a function
  5665. in a way adequate to your needs.)
  5666. <p>
  5667. <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
  5668. <p>
  5669. Looks for the first match of
  5670. <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
  5671. If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
  5672. where this occurrence starts and ends;
  5673. otherwise, it returns <b>nil</b>.
  5674. A third, optional numeric argument <code>init</code> specifies
  5675. where to start the search;
  5676. its default value is&nbsp;1 and can be negative.
  5677. A value of <b>true</b> as a fourth, optional argument <code>plain</code>
  5678. turns off the pattern matching facilities,
  5679. so the function does a plain "find substring" operation,
  5680. with no characters in <code>pattern</code> being considered magic.
  5681. Note that if <code>plain</code> is given, then <code>init</code> must be given as well.
  5682. <p>
  5683. If the pattern has captures,
  5684. then in a successful match
  5685. the captured values are also returned,
  5686. after the two indices.
  5687. <p>
  5688. <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, &middot;&middot;&middot;)</code></a></h3>
  5689. <p>
  5690. Returns a formatted version of its variable number of arguments
  5691. following the description given in its first argument (which must be a string).
  5692. The format string follows the same rules as the ISO&nbsp;C function <code>sprintf</code>.
  5693. The only differences are that the options/modifiers
  5694. <code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, <code>n</code>,
  5695. and <code>p</code> are not supported
  5696. and that there is an extra option, <code>q</code>.
  5697. <p>
  5698. The <code>q</code> option formats a string between double quotes,
  5699. using escape sequences when necessary to ensure that
  5700. it can safely be read back by the Lua interpreter.
  5701. For instance, the call
  5702. <pre>
  5703. string.format('%q', 'a string with "quotes" and \n new line')
  5704. </pre><p>
  5705. may produce the string:
  5706. <pre>
  5707. "a string with \"quotes\" and \
  5708. new line"
  5709. </pre>
  5710. <p>
  5711. Options
  5712. <code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>,
  5713. <code>G</code>, and <code>g</code> all expect a number as argument.
  5714. Options <code>c</code>, <code>d</code>,
  5715. <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
  5716. expect an integer.
  5717. When Lua is compiled with a C89 compiler,
  5718. options <code>A</code> and <code>a</code> (hexadecimal floats)
  5719. do not support any modifier (flags, width, length).
  5720. <p>
  5721. Option <code>s</code> expects a string;
  5722. if its argument is not a string,
  5723. it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
  5724. If the option has any modifier (flags, width, length),
  5725. the string argument should not contain embedded zeros.
  5726. <p>
  5727. <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
  5728. Returns an iterator function that,
  5729. each time it is called,
  5730. returns the next captures from <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>)
  5731. over the string <code>s</code>.
  5732. If <code>pattern</code> specifies no captures,
  5733. then the whole match is produced in each call.
  5734. <p>
  5735. As an example, the following loop
  5736. will iterate over all the words from string <code>s</code>,
  5737. printing one per line:
  5738. <pre>
  5739. s = "hello world from Lua"
  5740. for w in string.gmatch(s, "%a+") do
  5741. print(w)
  5742. end
  5743. </pre><p>
  5744. The next example collects all pairs <code>key=value</code> from the
  5745. given string into a table:
  5746. <pre>
  5747. t = {}
  5748. s = "from=world, to=Lua"
  5749. for k, v in string.gmatch(s, "(%w+)=(%w+)") do
  5750. t[k] = v
  5751. end
  5752. </pre>
  5753. <p>
  5754. For this function, a caret '<code>^</code>' at the start of a pattern does not
  5755. work as an anchor, as this would prevent the iteration.
  5756. <p>
  5757. <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
  5758. Returns a copy of <code>s</code>
  5759. in which all (or the first <code>n</code>, if given)
  5760. occurrences of the <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) have been
  5761. replaced by a replacement string specified by <code>repl</code>,
  5762. which can be a string, a table, or a function.
  5763. <code>gsub</code> also returns, as its second value,
  5764. the total number of matches that occurred.
  5765. The name <code>gsub</code> comes from <em>Global SUBstitution</em>.
  5766. <p>
  5767. If <code>repl</code> is a string, then its value is used for replacement.
  5768. The character&nbsp;<code>%</code> works as an escape character:
  5769. any sequence in <code>repl</code> of the form <code>%<em>d</em></code>,
  5770. with <em>d</em> between 1 and 9,
  5771. stands for the value of the <em>d</em>-th captured substring.
  5772. The sequence <code>%0</code> stands for the whole match.
  5773. The sequence <code>%%</code> stands for a single&nbsp;<code>%</code>.
  5774. <p>
  5775. If <code>repl</code> is a table, then the table is queried for every match,
  5776. using the first capture as the key.
  5777. <p>
  5778. If <code>repl</code> is a function, then this function is called every time a
  5779. match occurs, with all captured substrings passed as arguments,
  5780. in order.
  5781. <p>
  5782. In any case,
  5783. if the pattern specifies no captures,
  5784. then it behaves as if the whole pattern was inside a capture.
  5785. <p>
  5786. If the value returned by the table query or by the function call
  5787. is a string or a number,
  5788. then it is used as the replacement string;
  5789. otherwise, if it is <b>false</b> or <b>nil</b>,
  5790. then there is no replacement
  5791. (that is, the original match is kept in the string).
  5792. <p>
  5793. Here are some examples:
  5794. <pre>
  5795. x = string.gsub("hello world", "(%w+)", "%1 %1")
  5796. --&gt; x="hello hello world world"
  5797. x = string.gsub("hello world", "%w+", "%0 %0", 1)
  5798. --&gt; x="hello hello world"
  5799. x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
  5800. --&gt; x="world hello Lua from"
  5801. x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
  5802. --&gt; x="home = /home/roberto, user = roberto"
  5803. x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
  5804. return load(s)()
  5805. end)
  5806. --&gt; x="4+5 = 9"
  5807. local t = {name="lua", version="5.3"}
  5808. x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
  5809. --&gt; x="lua-5.3.tar.gz"
  5810. </pre>
  5811. <p>
  5812. <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3>
  5813. Receives a string and returns its length.
  5814. The empty string <code>""</code> has length 0.
  5815. Embedded zeros are counted,
  5816. so <code>"a\000bc\000"</code> has length 5.
  5817. <p>
  5818. <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3>
  5819. Receives a string and returns a copy of this string with all
  5820. uppercase letters changed to lowercase.
  5821. All other characters are left unchanged.
  5822. The definition of what an uppercase letter is depends on the current locale.
  5823. <p>
  5824. <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
  5825. Looks for the first <em>match</em> of
  5826. <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
  5827. If it finds one, then <code>match</code> returns
  5828. the captures from the pattern;
  5829. otherwise it returns <b>nil</b>.
  5830. If <code>pattern</code> specifies no captures,
  5831. then the whole match is returned.
  5832. A third, optional numeric argument <code>init</code> specifies
  5833. where to start the search;
  5834. its default value is&nbsp;1 and can be negative.
  5835. <p>
  5836. <hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, &middot;&middot;&middot;)</code></a></h3>
  5837. <p>
  5838. Returns a binary string containing the values <code>v1</code>, <code>v2</code>, etc.
  5839. packed (that is, serialized in binary form)
  5840. according to the format string <code>fmt</code> (see <a href="#6.4.2">&sect;6.4.2</a>).
  5841. <p>
  5842. <hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3>
  5843. <p>
  5844. Returns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a>
  5845. with the given format.
  5846. The format string cannot have the variable-length options
  5847. '<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">&sect;6.4.2</a>).
  5848. <p>
  5849. <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
  5850. Returns a string that is the concatenation of <code>n</code> copies of
  5851. the string <code>s</code> separated by the string <code>sep</code>.
  5852. The default value for <code>sep</code> is the empty string
  5853. (that is, no separator).
  5854. Returns the empty string if <code>n</code> is not positive.
  5855. <p>
  5856. (Note that it is very easy to exhaust the memory of your machine
  5857. with a single call to this function.)
  5858. <p>
  5859. <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3>
  5860. Returns a string that is the string <code>s</code> reversed.
  5861. <p>
  5862. <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3>
  5863. Returns the substring of <code>s</code> that
  5864. starts at <code>i</code> and continues until <code>j</code>;
  5865. <code>i</code> and <code>j</code> can be negative.
  5866. If <code>j</code> is absent, then it is assumed to be equal to -1
  5867. (which is the same as the string length).
  5868. In particular,
  5869. the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
  5870. with length <code>j</code>,
  5871. and <code>string.sub(s, -i)</code> (for a positive <code>i</code>)
  5872. returns a suffix of <code>s</code>
  5873. with length <code>i</code>.
  5874. <p>
  5875. If, after the translation of negative indices,
  5876. <code>i</code> is less than 1,
  5877. it is corrected to 1.
  5878. If <code>j</code> is greater than the string length,
  5879. it is corrected to that length.
  5880. If, after these corrections,
  5881. <code>i</code> is greater than <code>j</code>,
  5882. the function returns the empty string.
  5883. <p>
  5884. <hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3>
  5885. <p>
  5886. Returns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>)
  5887. according to the format string <code>fmt</code> (see <a href="#6.4.2">&sect;6.4.2</a>).
  5888. An optional <code>pos</code> marks where
  5889. to start reading in <code>s</code> (default is 1).
  5890. After the read values,
  5891. this function also returns the index of the first unread byte in <code>s</code>.
  5892. <p>
  5893. <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3>
  5894. Receives a string and returns a copy of this string with all
  5895. lowercase letters changed to uppercase.
  5896. All other characters are left unchanged.
  5897. The definition of what a lowercase letter is depends on the current locale.
  5898. <h3>6.4.1 &ndash; <a name="6.4.1">Patterns</a></h3>
  5899. <p>
  5900. Patterns in Lua are described by regular strings,
  5901. which are interpreted as patterns by the pattern-matching functions
  5902. <a href="#pdf-string.find"><code>string.find</code></a>,
  5903. <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>,
  5904. <a href="#pdf-string.gsub"><code>string.gsub</code></a>,
  5905. and <a href="#pdf-string.match"><code>string.match</code></a>.
  5906. This section describes the syntax and the meaning
  5907. (that is, what they match) of these strings.
  5908. <h4>Character Class:</h4><p>
  5909. A <em>character class</em> is used to represent a set of characters.
  5910. The following combinations are allowed in describing a character class:
  5911. <ul>
  5912. <li><b><em>x</em>: </b>
  5913. (where <em>x</em> is not one of the <em>magic characters</em>
  5914. <code>^$()%.[]*+-?</code>)
  5915. represents the character <em>x</em> itself.
  5916. </li>
  5917. <li><b><code>.</code>: </b> (a dot) represents all characters.</li>
  5918. <li><b><code>%a</code>: </b> represents all letters.</li>
  5919. <li><b><code>%c</code>: </b> represents all control characters.</li>
  5920. <li><b><code>%d</code>: </b> represents all digits.</li>
  5921. <li><b><code>%g</code>: </b> represents all printable characters except space.</li>
  5922. <li><b><code>%l</code>: </b> represents all lowercase letters.</li>
  5923. <li><b><code>%p</code>: </b> represents all punctuation characters.</li>
  5924. <li><b><code>%s</code>: </b> represents all space characters.</li>
  5925. <li><b><code>%u</code>: </b> represents all uppercase letters.</li>
  5926. <li><b><code>%w</code>: </b> represents all alphanumeric characters.</li>
  5927. <li><b><code>%x</code>: </b> represents all hexadecimal digits.</li>
  5928. <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character)
  5929. represents the character <em>x</em>.
  5930. This is the standard way to escape the magic characters.
  5931. Any non-alphanumeric character
  5932. (including all punctuation characters, even the non-magical)
  5933. can be preceded by a '<code>%</code>'
  5934. when used to represent itself in a pattern.
  5935. </li>
  5936. <li><b><code>[<em>set</em>]</code>: </b>
  5937. represents the class which is the union of all
  5938. characters in <em>set</em>.
  5939. A range of characters can be specified by
  5940. separating the end characters of the range,
  5941. in ascending order, with a '<code>-</code>'.
  5942. All classes <code>%</code><em>x</em> described above can also be used as
  5943. components in <em>set</em>.
  5944. All other characters in <em>set</em> represent themselves.
  5945. For example, <code>[%w_]</code> (or <code>[_%w]</code>)
  5946. represents all alphanumeric characters plus the underscore,
  5947. <code>[0-7]</code> represents the octal digits,
  5948. and <code>[0-7%l%-]</code> represents the octal digits plus
  5949. the lowercase letters plus the '<code>-</code>' character.
  5950. <p>
  5951. You can put a closing square bracket in a set
  5952. by positioning it as the first character in the set.
  5953. You can put a hyphen in a set
  5954. by positioning it as the first or the last character in the set.
  5955. (You can also use an escape for both cases.)
  5956. <p>
  5957. The interaction between ranges and classes is not defined.
  5958. Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>
  5959. have no meaning.
  5960. </li>
  5961. <li><b><code>[^<em>set</em>]</code>: </b>
  5962. represents the complement of <em>set</em>,
  5963. where <em>set</em> is interpreted as above.
  5964. </li>
  5965. </ul><p>
  5966. For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),
  5967. the corresponding uppercase letter represents the complement of the class.
  5968. For instance, <code>%S</code> represents all non-space characters.
  5969. <p>
  5970. The definitions of letter, space, and other character groups
  5971. depend on the current locale.
  5972. In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.
  5973. <h4>Pattern Item:</h4><p>
  5974. A <em>pattern item</em> can be
  5975. <ul>
  5976. <li>
  5977. a single character class,
  5978. which matches any single character in the class;
  5979. </li>
  5980. <li>
  5981. a single character class followed by '<code>*</code>',
  5982. which matches zero or more repetitions of characters in the class.
  5983. These repetition items will always match the longest possible sequence;
  5984. </li>
  5985. <li>
  5986. a single character class followed by '<code>+</code>',
  5987. which matches one or more repetitions of characters in the class.
  5988. These repetition items will always match the longest possible sequence;
  5989. </li>
  5990. <li>
  5991. a single character class followed by '<code>-</code>',
  5992. which also matches zero or more repetitions of characters in the class.
  5993. Unlike '<code>*</code>',
  5994. these repetition items will always match the shortest possible sequence;
  5995. </li>
  5996. <li>
  5997. a single character class followed by '<code>?</code>',
  5998. which matches zero or one occurrence of a character in the class.
  5999. It always matches one occurrence if possible;
  6000. </li>
  6001. <li>
  6002. <code>%<em>n</em></code>, for <em>n</em> between 1 and 9;
  6003. such item matches a substring equal to the <em>n</em>-th captured string
  6004. (see below);
  6005. </li>
  6006. <li>
  6007. <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters;
  6008. such item matches strings that start with&nbsp;<em>x</em>, end with&nbsp;<em>y</em>,
  6009. and where the <em>x</em> and <em>y</em> are <em>balanced</em>.
  6010. This means that, if one reads the string from left to right,
  6011. counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,
  6012. the ending <em>y</em> is the first <em>y</em> where the count reaches 0.
  6013. For instance, the item <code>%b()</code> matches expressions with
  6014. balanced parentheses.
  6015. </li>
  6016. <li>
  6017. <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>;
  6018. such item matches an empty string at any position such that
  6019. the next character belongs to <em>set</em>
  6020. and the previous character does not belong to <em>set</em>.
  6021. The set <em>set</em> is interpreted as previously described.
  6022. The beginning and the end of the subject are handled as if
  6023. they were the character '<code>\0</code>'.
  6024. </li>
  6025. </ul>
  6026. <h4>Pattern:</h4><p>
  6027. A <em>pattern</em> is a sequence of pattern items.
  6028. A caret '<code>^</code>' at the beginning of a pattern anchors the match at the
  6029. beginning of the subject string.
  6030. A '<code>$</code>' at the end of a pattern anchors the match at the
  6031. end of the subject string.
  6032. At other positions,
  6033. '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves.
  6034. <h4>Captures:</h4><p>
  6035. A pattern can contain sub-patterns enclosed in parentheses;
  6036. they describe <em>captures</em>.
  6037. When a match succeeds, the substrings of the subject string
  6038. that match captures are stored (<em>captured</em>) for future use.
  6039. Captures are numbered according to their left parentheses.
  6040. For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>,
  6041. the part of the string matching <code>"a*(.)%w(%s*)"</code> is
  6042. stored as the first capture (and therefore has number&nbsp;1);
  6043. the character matching "<code>.</code>" is captured with number&nbsp;2,
  6044. and the part matching "<code>%s*</code>" has number&nbsp;3.
  6045. <p>
  6046. As a special case, the empty capture <code>()</code> captures
  6047. the current string position (a number).
  6048. For instance, if we apply the pattern <code>"()aa()"</code> on the
  6049. string <code>"flaaap"</code>, there will be two captures: 3&nbsp;and&nbsp;5.
  6050. <h3>6.4.2 &ndash; <a name="6.4.2">Format Strings for Pack and Unpack</a></h3>
  6051. <p>
  6052. The first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>,
  6053. <a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a>
  6054. is a format string,
  6055. which describes the layout of the structure being created or read.
  6056. <p>
  6057. A format string is a sequence of conversion options.
  6058. The conversion options are as follows:
  6059. <ul>
  6060. <li><b><code>&lt;</code>: </b>sets little endian</li>
  6061. <li><b><code>&gt;</code>: </b>sets big endian</li>
  6062. <li><b><code>=</code>: </b>sets native endian</li>
  6063. <li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code>
  6064. (default is native alignment)</li>
  6065. <li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li>
  6066. <li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li>
  6067. <li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li>
  6068. <li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li>
  6069. <li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li>
  6070. <li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li>
  6071. <li><b><code>j</code>: </b>a <code>lua_Integer</code></li>
  6072. <li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li>
  6073. <li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li>
  6074. <li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes
  6075. (default is native size)</li>
  6076. <li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes
  6077. (default is native size)</li>
  6078. <li><b><code>f</code>: </b>a <code>float</code> (native size)</li>
  6079. <li><b><code>d</code>: </b>a <code>double</code> (native size)</li>
  6080. <li><b><code>n</code>: </b>a <code>lua_Number</code></li>
  6081. <li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li>
  6082. <li><b><code>z</code>: </b>a zero-terminated string</li>
  6083. <li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length
  6084. coded as an unsigned integer with <code>n</code> bytes
  6085. (default is a <code>size_t</code>)</li>
  6086. <li><b><code>x</code>: </b>one byte of padding</li>
  6087. <li><b><code>X<em>op</em></code>: </b>an empty item that aligns
  6088. according to option <code>op</code>
  6089. (which is otherwise ignored)</li>
  6090. <li><b>'<code> </code>': </b>(empty space) ignored</li>
  6091. </ul><p>
  6092. (A "<code>[<em>n</em>]</code>" means an optional integral numeral.)
  6093. Except for padding, spaces, and configurations
  6094. (options "<code>xX &lt;=&gt;!</code>"),
  6095. each option corresponds to an argument (in <a href="#pdf-string.pack"><code>string.pack</code></a>)
  6096. or a result (in <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
  6097. <p>
  6098. 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>",
  6099. <code>n</code> can be any integer between 1 and 16.
  6100. All integral options check overflows;
  6101. <a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size;
  6102. <a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer.
  6103. <p>
  6104. Any format string starts as if prefixed by "<code>!1=</code>",
  6105. that is,
  6106. with maximum alignment of 1 (no alignment)
  6107. and native endianness.
  6108. <p>
  6109. Alignment works as follows:
  6110. For each option,
  6111. the format gets extra padding until the data starts
  6112. at an offset that is a multiple of the minimum between the
  6113. option size and the maximum alignment;
  6114. this minimum must be a power of 2.
  6115. Options "<code>c</code>" and "<code>z</code>" are not aligned;
  6116. option "<code>s</code>" follows the alignment of its starting integer.
  6117. <p>
  6118. All padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a>
  6119. (and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
  6120. <h2>6.5 &ndash; <a name="6.5">UTF-8 Support</a></h2>
  6121. <p>
  6122. This library provides basic support for UTF-8 encoding.
  6123. It provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>.
  6124. This library does not provide any support for Unicode other
  6125. than the handling of the encoding.
  6126. Any operation that needs the meaning of a character,
  6127. such as character classification, is outside its scope.
  6128. <p>
  6129. Unless stated otherwise,
  6130. all functions that expect a byte position as a parameter
  6131. assume that the given position is either the start of a byte sequence
  6132. or one plus the length of the subject string.
  6133. As in the string library,
  6134. negative indices count from the end of the string.
  6135. <p>
  6136. <hr><h3><a name="pdf-utf8.char"><code>utf8.char (&middot;&middot;&middot;)</code></a></h3>
  6137. Receives zero or more integers,
  6138. converts each one to its corresponding UTF-8 byte sequence
  6139. and returns a string with the concatenation of all these sequences.
  6140. <p>
  6141. <hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3>
  6142. The pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xF4][\x80-\xBF]*</code>"
  6143. (see <a href="#6.4.1">&sect;6.4.1</a>),
  6144. which matches exactly one UTF-8 byte sequence,
  6145. assuming that the subject is a valid UTF-8 string.
  6146. <p>
  6147. <hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s)</code></a></h3>
  6148. <p>
  6149. Returns values so that the construction
  6150. <pre>
  6151. for p, c in utf8.codes(s) do <em>body</em> end
  6152. </pre><p>
  6153. will iterate over all characters in string <code>s</code>,
  6154. with <code>p</code> being the position (in bytes) and <code>c</code> the code point
  6155. of each character.
  6156. It raises an error if it meets any invalid byte sequence.
  6157. <p>
  6158. <hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j]])</code></a></h3>
  6159. Returns the codepoints (as integers) from all characters in <code>s</code>
  6160. that start between byte position <code>i</code> and <code>j</code> (both included).
  6161. The default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>.
  6162. It raises an error if it meets any invalid byte sequence.
  6163. <p>
  6164. <hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3>
  6165. Returns the number of UTF-8 characters in string <code>s</code>
  6166. that start between positions <code>i</code> and <code>j</code> (both inclusive).
  6167. The default for <code>i</code> is 1 and for <code>j</code> is -1.
  6168. If it finds any invalid byte sequence,
  6169. returns a false value plus the position of the first invalid byte.
  6170. <p>
  6171. <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
  6172. Returns the position (in bytes) where the encoding of the
  6173. <code>n</code>-th character of <code>s</code>
  6174. (counting from position <code>i</code>) starts.
  6175. A negative <code>n</code> gets characters before position <code>i</code>.
  6176. The default for <code>i</code> is 1 when <code>n</code> is non-negative
  6177. and <code>#s + 1</code> otherwise,
  6178. so that <code>utf8.offset(s, -n)</code> gets the offset of the
  6179. <code>n</code>-th character from the end of the string.
  6180. If the specified character is neither in the subject
  6181. nor right after its end,
  6182. the function returns <b>nil</b>.
  6183. <p>
  6184. As a special case,
  6185. when <code>n</code> is 0 the function returns the start of the encoding
  6186. of the character that contains the <code>i</code>-th byte of <code>s</code>.
  6187. <p>
  6188. This function assumes that <code>s</code> is a valid UTF-8 string.
  6189. <h2>6.6 &ndash; <a name="6.6">Table Manipulation</a></h2>
  6190. <p>
  6191. This library provides generic functions for table manipulation.
  6192. It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
  6193. <p>
  6194. Remember that, whenever an operation needs the length of a table,
  6195. all caveats about the length operator apply (see <a href="#3.4.7">&sect;3.4.7</a>).
  6196. All functions ignore non-numeric keys
  6197. in the tables given as arguments.
  6198. <p>
  6199. <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
  6200. <p>
  6201. Given a list where all elements are strings or numbers,
  6202. returns the string <code>list[i]..sep..list[i+1] &middot;&middot;&middot; sep..list[j]</code>.
  6203. The default value for <code>sep</code> is the empty string,
  6204. the default for <code>i</code> is 1,
  6205. and the default for <code>j</code> is <code>#list</code>.
  6206. If <code>i</code> is greater than <code>j</code>, returns the empty string.
  6207. <p>
  6208. <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
  6209. <p>
  6210. Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>,
  6211. shifting up the elements
  6212. <code>list[pos], list[pos+1], &middot;&middot;&middot;, list[#list]</code>.
  6213. The default value for <code>pos</code> is <code>#list+1</code>,
  6214. so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
  6215. of list <code>t</code>.
  6216. <p>
  6217. <hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3>
  6218. <p>
  6219. Moves elements from table <code>a1</code> to table <code>a2</code>,
  6220. performing the equivalent to the following
  6221. multiple assignment:
  6222. <code>a2[t],&middot;&middot;&middot; = a1[f],&middot;&middot;&middot;,a1[e]</code>.
  6223. The default for <code>a2</code> is <code>a1</code>.
  6224. The destination range can overlap with the source range.
  6225. The number of elements to be moved must fit in a Lua integer.
  6226. <p>
  6227. Returns the destination table <code>a2</code>.
  6228. <p>
  6229. <hr><h3><a name="pdf-table.pack"><code>table.pack (&middot;&middot;&middot;)</code></a></h3>
  6230. <p>
  6231. Returns a new table with all arguments stored into keys 1, 2, etc.
  6232. and with a field "<code>n</code>" with the total number of arguments.
  6233. Note that the resulting table may not be a sequence.
  6234. <p>
  6235. <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3>
  6236. <p>
  6237. Removes from <code>list</code> the element at position <code>pos</code>,
  6238. returning the value of the removed element.
  6239. When <code>pos</code> is an integer between 1 and <code>#list</code>,
  6240. it shifts down the elements
  6241. <code>list[pos+1], list[pos+2], &middot;&middot;&middot;, list[#list]</code>
  6242. and erases element <code>list[#list]</code>;
  6243. The index <code>pos</code> can also be 0 when <code>#list</code> is 0,
  6244. or <code>#list + 1</code>;
  6245. in those cases, the function erases the element <code>list[pos]</code>.
  6246. <p>
  6247. The default value for <code>pos</code> is <code>#list</code>,
  6248. so that a call <code>table.remove(l)</code> removes the last element
  6249. of list <code>l</code>.
  6250. <p>
  6251. <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3>
  6252. <p>
  6253. Sorts list elements in a given order, <em>in-place</em>,
  6254. from <code>list[1]</code> to <code>list[#list]</code>.
  6255. If <code>comp</code> is given,
  6256. then it must be a function that receives two list elements
  6257. and returns true when the first element must come
  6258. before the second in the final order
  6259. (so that, after the sort,
  6260. <code>i &lt; j</code> implies <code>not comp(list[j],list[i])</code>).
  6261. If <code>comp</code> is not given,
  6262. then the standard Lua operator <code>&lt;</code> is used instead.
  6263. <p>
  6264. Note that the <code>comp</code> function must define
  6265. a strict partial order over the elements in the list;
  6266. that is, it must be asymmetric and transitive.
  6267. Otherwise, no valid sort may be possible.
  6268. <p>
  6269. The sort algorithm is not stable:
  6270. elements considered equal by the given order
  6271. may have their relative positions changed by the sort.
  6272. <p>
  6273. <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3>
  6274. <p>
  6275. Returns the elements from the given list.
  6276. This function is equivalent to
  6277. <pre>
  6278. return list[i], list[i+1], &middot;&middot;&middot;, list[j]
  6279. </pre><p>
  6280. By default, <code>i</code> is&nbsp;1 and <code>j</code> is <code>#list</code>.
  6281. <h2>6.7 &ndash; <a name="6.7">Mathematical Functions</a></h2>
  6282. <p>
  6283. This library provides basic mathematical functions.
  6284. It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
  6285. Functions with the annotation "<code>integer/float</code>" give
  6286. integer results for integer arguments
  6287. and float results for float (or mixed) arguments.
  6288. Rounding functions
  6289. (<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>)
  6290. return an integer when the result fits in the range of an integer,
  6291. or a float otherwise.
  6292. <p>
  6293. <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3>
  6294. <p>
  6295. Returns the absolute value of <code>x</code>. (integer/float)
  6296. <p>
  6297. <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3>
  6298. <p>
  6299. Returns the arc cosine of <code>x</code> (in radians).
  6300. <p>
  6301. <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3>
  6302. <p>
  6303. Returns the arc sine of <code>x</code> (in radians).
  6304. <p>
  6305. <hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3>
  6306. <p>
  6307. Returns the arc tangent of <code>y/x</code> (in radians),
  6308. but uses the signs of both arguments to find the
  6309. quadrant of the result.
  6310. (It also handles correctly the case of <code>x</code> being zero.)
  6311. <p>
  6312. The default value for <code>x</code> is 1,
  6313. so that the call <code>math.atan(y)</code>
  6314. returns the arc tangent of <code>y</code>.
  6315. <p>
  6316. <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
  6317. <p>
  6318. Returns the smallest integral value larger than or equal to <code>x</code>.
  6319. <p>
  6320. <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
  6321. <p>
  6322. Returns the cosine of <code>x</code> (assumed to be in radians).
  6323. <p>
  6324. <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3>
  6325. <p>
  6326. Converts the angle <code>x</code> from radians to degrees.
  6327. <p>
  6328. <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
  6329. <p>
  6330. Returns the value <em>e<sup>x</sup></em>
  6331. (where <code>e</code> is the base of natural logarithms).
  6332. <p>
  6333. <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3>
  6334. <p>
  6335. Returns the largest integral value smaller than or equal to <code>x</code>.
  6336. <p>
  6337. <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3>
  6338. <p>
  6339. Returns the remainder of the division of <code>x</code> by <code>y</code>
  6340. that rounds the quotient towards zero. (integer/float)
  6341. <p>
  6342. <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3>
  6343. <p>
  6344. The float value <code>HUGE_VAL</code>,
  6345. a value larger than any other numeric value.
  6346. <p>
  6347. <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
  6348. <p>
  6349. Returns the logarithm of <code>x</code> in the given base.
  6350. The default for <code>base</code> is <em>e</em>
  6351. (so that the function returns the natural logarithm of <code>x</code>).
  6352. <p>
  6353. <hr><h3><a name="pdf-math.max"><code>math.max (x, &middot;&middot;&middot;)</code></a></h3>
  6354. <p>
  6355. Returns the argument with the maximum value,
  6356. according to the Lua operator <code>&lt;</code>. (integer/float)
  6357. <p>
  6358. <hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3>
  6359. An integer with the maximum value for an integer.
  6360. <p>
  6361. <hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
  6362. <p>
  6363. Returns the argument with the minimum value,
  6364. according to the Lua operator <code>&lt;</code>. (integer/float)
  6365. <p>
  6366. <hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
  6367. An integer with the minimum value for an integer.
  6368. <p>
  6369. <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
  6370. <p>
  6371. Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
  6372. Its second result is always a float.
  6373. <p>
  6374. <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
  6375. <p>
  6376. The value of <em>&pi;</em>.
  6377. <p>
  6378. <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3>
  6379. <p>
  6380. Converts the angle <code>x</code> from degrees to radians.
  6381. <p>
  6382. <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
  6383. <p>
  6384. When called without arguments,
  6385. returns a pseudo-random float with uniform distribution
  6386. in the range <em>[0,1)</em>.
  6387. When called with two integers <code>m</code> and <code>n</code>,
  6388. <code>math.random</code> returns a pseudo-random integer
  6389. with uniform distribution in the range <em>[m, n]</em>.
  6390. (The value <em>n-m</em> cannot be negative and must fit in a Lua integer.)
  6391. The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>.
  6392. <p>
  6393. This function is an interface to the underling
  6394. pseudo-random generator function provided by C.
  6395. <p>
  6396. <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3>
  6397. <p>
  6398. Sets <code>x</code> as the "seed"
  6399. for the pseudo-random generator:
  6400. equal seeds produce equal sequences of numbers.
  6401. <p>
  6402. <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3>
  6403. <p>
  6404. Returns the sine of <code>x</code> (assumed to be in radians).
  6405. <p>
  6406. <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
  6407. <p>
  6408. Returns the square root of <code>x</code>.
  6409. (You can also use the expression <code>x^0.5</code> to compute this value.)
  6410. <p>
  6411. <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3>
  6412. <p>
  6413. Returns the tangent of <code>x</code> (assumed to be in radians).
  6414. <p>
  6415. <hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3>
  6416. <p>
  6417. If the value <code>x</code> is convertible to an integer,
  6418. returns that integer.
  6419. Otherwise, returns <b>nil</b>.
  6420. <p>
  6421. <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
  6422. <p>
  6423. Returns "<code>integer</code>" if <code>x</code> is an integer,
  6424. "<code>float</code>" if it is a float,
  6425. or <b>nil</b> if <code>x</code> is not a number.
  6426. <p>
  6427. <hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3>
  6428. <p>
  6429. Returns a boolean,
  6430. true if and only if integer <code>m</code> is below integer <code>n</code> when
  6431. they are compared as unsigned integers.
  6432. <h2>6.8 &ndash; <a name="6.8">Input and Output Facilities</a></h2>
  6433. <p>
  6434. The I/O library provides two different styles for file manipulation.
  6435. The first one uses implicit file handles;
  6436. that is, there are operations to set a default input file and a
  6437. default output file,
  6438. and all input/output operations are over these default files.
  6439. The second style uses explicit file handles.
  6440. <p>
  6441. When using implicit file handles,
  6442. all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
  6443. When using explicit file handles,
  6444. the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle
  6445. and then all operations are supplied as methods of the file handle.
  6446. <p>
  6447. The table <code>io</code> also provides
  6448. three predefined file handles with their usual meanings from C:
  6449. <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>.
  6450. The I/O library never closes these files.
  6451. <p>
  6452. Unless otherwise stated,
  6453. all I/O functions return <b>nil</b> on failure
  6454. (plus an error message as a second result and
  6455. a system-dependent error code as a third result)
  6456. and some value different from <b>nil</b> on success.
  6457. In non-POSIX systems,
  6458. the computation of the error message and error code
  6459. in case of errors
  6460. may be not thread safe,
  6461. because they rely on the global C variable <code>errno</code>.
  6462. <p>
  6463. <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
  6464. <p>
  6465. Equivalent to <code>file:close()</code>.
  6466. Without a <code>file</code>, closes the default output file.
  6467. <p>
  6468. <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
  6469. <p>
  6470. Equivalent to <code>io.output():flush()</code>.
  6471. <p>
  6472. <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
  6473. <p>
  6474. When called with a file name, it opens the named file (in text mode),
  6475. and sets its handle as the default input file.
  6476. When called with a file handle,
  6477. it simply sets this file handle as the default input file.
  6478. When called without arguments,
  6479. it returns the current default input file.
  6480. <p>
  6481. In case of errors this function raises the error,
  6482. instead of returning an error code.
  6483. <p>
  6484. <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename, &middot;&middot;&middot;])</code></a></h3>
  6485. <p>
  6486. Opens the given file name in read mode
  6487. and returns an iterator function that
  6488. works like <code>file:lines(&middot;&middot;&middot;)</code> over the opened file.
  6489. When the iterator function detects the end of file,
  6490. it returns no values (to finish the loop) and automatically closes the file.
  6491. <p>
  6492. The call <code>io.lines()</code> (with no file name) is equivalent
  6493. to <code>io.input():lines("*l")</code>;
  6494. that is, it iterates over the lines of the default input file.
  6495. In this case, the iterator does not close the file when the loop ends.
  6496. <p>
  6497. In case of errors this function raises the error,
  6498. instead of returning an error code.
  6499. <p>
  6500. <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
  6501. <p>
  6502. This function opens a file,
  6503. in the mode specified in the string <code>mode</code>.
  6504. In case of success,
  6505. it returns a new file handle.
  6506. <p>
  6507. The <code>mode</code> string can be any of the following:
  6508. <ul>
  6509. <li><b>"<code>r</code>": </b> read mode (the default);</li>
  6510. <li><b>"<code>w</code>": </b> write mode;</li>
  6511. <li><b>"<code>a</code>": </b> append mode;</li>
  6512. <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li>
  6513. <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li>
  6514. <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved,
  6515. writing is only allowed at the end of file.</li>
  6516. </ul><p>
  6517. The <code>mode</code> string can also have a '<code>b</code>' at the end,
  6518. which is needed in some systems to open the file in binary mode.
  6519. <p>
  6520. <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
  6521. <p>
  6522. Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file.
  6523. <p>
  6524. <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
  6525. <p>
  6526. This function is system dependent and is not available
  6527. on all platforms.
  6528. <p>
  6529. Starts program <code>prog</code> in a separated process and returns
  6530. a file handle that you can use to read data from this program
  6531. (if <code>mode</code> is <code>"r"</code>, the default)
  6532. or to write data to this program
  6533. (if <code>mode</code> is <code>"w"</code>).
  6534. <p>
  6535. <hr><h3><a name="pdf-io.read"><code>io.read (&middot;&middot;&middot;)</code></a></h3>
  6536. <p>
  6537. Equivalent to <code>io.input():read(&middot;&middot;&middot;)</code>.
  6538. <p>
  6539. <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
  6540. <p>
  6541. In case of success,
  6542. returns a handle for a temporary file.
  6543. This file is opened in update mode
  6544. and it is automatically removed when the program ends.
  6545. <p>
  6546. <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
  6547. <p>
  6548. Checks whether <code>obj</code> is a valid file handle.
  6549. Returns the string <code>"file"</code> if <code>obj</code> is an open file handle,
  6550. <code>"closed file"</code> if <code>obj</code> is a closed file handle,
  6551. or <b>nil</b> if <code>obj</code> is not a file handle.
  6552. <p>
  6553. <hr><h3><a name="pdf-io.write"><code>io.write (&middot;&middot;&middot;)</code></a></h3>
  6554. <p>
  6555. Equivalent to <code>io.output():write(&middot;&middot;&middot;)</code>.
  6556. <p>
  6557. <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
  6558. <p>
  6559. Closes <code>file</code>.
  6560. Note that files are automatically closed when
  6561. their handles are garbage collected,
  6562. but that takes an unpredictable amount of time to happen.
  6563. <p>
  6564. When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
  6565. <a href="#pdf-file:close"><code>file:close</code></a> returns the same values
  6566. returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
  6567. <p>
  6568. <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
  6569. <p>
  6570. Saves any written data to <code>file</code>.
  6571. <p>
  6572. <hr><h3><a name="pdf-file:lines"><code>file:lines (&middot;&middot;&middot;)</code></a></h3>
  6573. <p>
  6574. Returns an iterator function that,
  6575. each time it is called,
  6576. reads the file according to the given formats.
  6577. When no format is given,
  6578. uses "<code>l</code>" as a default.
  6579. As an example, the construction
  6580. <pre>
  6581. for c in file:lines(1) do <em>body</em> end
  6582. </pre><p>
  6583. will iterate over all characters of the file,
  6584. starting at the current position.
  6585. Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
  6586. when the loop ends.
  6587. <p>
  6588. In case of errors this function raises the error,
  6589. instead of returning an error code.
  6590. <p>
  6591. <hr><h3><a name="pdf-file:read"><code>file:read (&middot;&middot;&middot;)</code></a></h3>
  6592. <p>
  6593. Reads the file <code>file</code>,
  6594. according to the given formats, which specify what to read.
  6595. For each format,
  6596. the function returns a string or a number with the characters read,
  6597. or <b>nil</b> if it cannot read data with the specified format.
  6598. (In this latter case,
  6599. the function does not read subsequent formats.)
  6600. When called without formats,
  6601. it uses a default format that reads the next line
  6602. (see below).
  6603. <p>
  6604. The available formats are
  6605. <ul>
  6606. <li><b>"<code>n</code>": </b>
  6607. reads a numeral and returns it as a float or an integer,
  6608. following the lexical conventions of Lua.
  6609. (The numeral may have leading spaces and a sign.)
  6610. This format always reads the longest input sequence that
  6611. is a valid prefix for a numeral;
  6612. if that prefix does not form a valid numeral
  6613. (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
  6614. it is discarded and the function returns <b>nil</b>.
  6615. </li>
  6616. <li><b>"<code>a</code>": </b>
  6617. reads the whole file, starting at the current position.
  6618. On end of file, it returns the empty string.
  6619. </li>
  6620. <li><b>"<code>l</code>": </b>
  6621. reads the next line skipping the end of line,
  6622. returning <b>nil</b> on end of file.
  6623. This is the default format.
  6624. </li>
  6625. <li><b>"<code>L</code>": </b>
  6626. reads the next line keeping the end-of-line character (if present),
  6627. returning <b>nil</b> on end of file.
  6628. </li>
  6629. <li><b><em>number</em>: </b>
  6630. reads a string with up to this number of bytes,
  6631. returning <b>nil</b> on end of file.
  6632. If <code>number</code> is zero,
  6633. it reads nothing and returns an empty string,
  6634. or <b>nil</b> on end of file.
  6635. </li>
  6636. </ul><p>
  6637. The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
  6638. <p>
  6639. <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3>
  6640. <p>
  6641. Sets and gets the file position,
  6642. measured from the beginning of the file,
  6643. to the position given by <code>offset</code> plus a base
  6644. specified by the string <code>whence</code>, as follows:
  6645. <ul>
  6646. <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li>
  6647. <li><b>"<code>cur</code>": </b> base is current position;</li>
  6648. <li><b>"<code>end</code>": </b> base is end of file;</li>
  6649. </ul><p>
  6650. In case of success, <code>seek</code> returns the final file position,
  6651. measured in bytes from the beginning of the file.
  6652. If <code>seek</code> fails, it returns <b>nil</b>,
  6653. plus a string describing the error.
  6654. <p>
  6655. The default value for <code>whence</code> is <code>"cur"</code>,
  6656. and for <code>offset</code> is 0.
  6657. Therefore, the call <code>file:seek()</code> returns the current
  6658. file position, without changing it;
  6659. the call <code>file:seek("set")</code> sets the position to the
  6660. beginning of the file (and returns 0);
  6661. and the call <code>file:seek("end")</code> sets the position to the
  6662. end of the file, and returns its size.
  6663. <p>
  6664. <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
  6665. <p>
  6666. Sets the buffering mode for an output file.
  6667. There are three available modes:
  6668. <ul>
  6669. <li><b>"<code>no</code>": </b>
  6670. no buffering; the result of any output operation appears immediately.
  6671. </li>
  6672. <li><b>"<code>full</code>": </b>
  6673. full buffering; output operation is performed only
  6674. when the buffer is full or when
  6675. you explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>).
  6676. </li>
  6677. <li><b>"<code>line</code>": </b>
  6678. line buffering; output is buffered until a newline is output
  6679. or there is any input from some special files
  6680. (such as a terminal device).
  6681. </li>
  6682. </ul><p>
  6683. For the last two cases, <code>size</code>
  6684. specifies the size of the buffer, in bytes.
  6685. The default is an appropriate size.
  6686. <p>
  6687. <hr><h3><a name="pdf-file:write"><code>file:write (&middot;&middot;&middot;)</code></a></h3>
  6688. <p>
  6689. Writes the value of each of its arguments to <code>file</code>.
  6690. The arguments must be strings or numbers.
  6691. <p>
  6692. In case of success, this function returns <code>file</code>.
  6693. Otherwise it returns <b>nil</b> plus a string describing the error.
  6694. <h2>6.9 &ndash; <a name="6.9">Operating System Facilities</a></h2>
  6695. <p>
  6696. This library is implemented through table <a name="pdf-os"><code>os</code></a>.
  6697. <p>
  6698. <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
  6699. <p>
  6700. Returns an approximation of the amount in seconds of CPU time
  6701. used by the program.
  6702. <p>
  6703. <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
  6704. <p>
  6705. Returns a string or a table containing date and time,
  6706. formatted according to the given string <code>format</code>.
  6707. <p>
  6708. If the <code>time</code> argument is present,
  6709. this is the time to be formatted
  6710. (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
  6711. Otherwise, <code>date</code> formats the current time.
  6712. <p>
  6713. If <code>format</code> starts with '<code>!</code>',
  6714. then the date is formatted in Coordinated Universal Time.
  6715. After this optional character,
  6716. if <code>format</code> is the string "<code>*t</code>",
  6717. then <code>date</code> returns a table with the following fields:
  6718. <code>year</code>, <code>month</code> (1&ndash;12), <code>day</code> (1&ndash;31),
  6719. <code>hour</code> (0&ndash;23), <code>min</code> (0&ndash;59), <code>sec</code> (0&ndash;61),
  6720. <code>wday</code> (weekday, 1&ndash;7, Sunday is&nbsp;1),
  6721. <code>yday</code> (day of the year, 1&ndash;366),
  6722. and <code>isdst</code> (daylight saving flag, a boolean).
  6723. This last field may be absent
  6724. if the information is not available.
  6725. <p>
  6726. If <code>format</code> is not "<code>*t</code>",
  6727. then <code>date</code> returns the date as a string,
  6728. formatted according to the same rules as the ISO&nbsp;C function <code>strftime</code>.
  6729. <p>
  6730. When called without arguments,
  6731. <code>date</code> returns a reasonable date and time representation that depends on
  6732. the host system and on the current locale.
  6733. (More specifically, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>.)
  6734. <p>
  6735. In non-POSIX systems,
  6736. this function may be not thread safe
  6737. because of its reliance on C&nbsp;function <code>gmtime</code> and C&nbsp;function <code>localtime</code>.
  6738. <p>
  6739. <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
  6740. <p>
  6741. Returns the difference, in seconds,
  6742. from time <code>t1</code> to time <code>t2</code>
  6743. (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>).
  6744. In POSIX, Windows, and some other systems,
  6745. this value is exactly <code>t2</code><em>-</em><code>t1</code>.
  6746. <p>
  6747. <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
  6748. <p>
  6749. This function is equivalent to the ISO&nbsp;C function <code>system</code>.
  6750. It passes <code>command</code> to be executed by an operating system shell.
  6751. Its first result is <b>true</b>
  6752. if the command terminated successfully,
  6753. or <b>nil</b> otherwise.
  6754. After this first result
  6755. the function returns a string plus a number,
  6756. as follows:
  6757. <ul>
  6758. <li><b>"<code>exit</code>": </b>
  6759. the command terminated normally;
  6760. the following number is the exit status of the command.
  6761. </li>
  6762. <li><b>"<code>signal</code>": </b>
  6763. the command was terminated by a signal;
  6764. the following number is the signal that terminated the command.
  6765. </li>
  6766. </ul>
  6767. <p>
  6768. When called without a <code>command</code>,
  6769. <code>os.execute</code> returns a boolean that is true if a shell is available.
  6770. <p>
  6771. <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
  6772. <p>
  6773. Calls the ISO&nbsp;C function <code>exit</code> to terminate the host program.
  6774. If <code>code</code> is <b>true</b>,
  6775. the returned status is <code>EXIT_SUCCESS</code>;
  6776. if <code>code</code> is <b>false</b>,
  6777. the returned status is <code>EXIT_FAILURE</code>;
  6778. if <code>code</code> is a number,
  6779. the returned status is this number.
  6780. The default value for <code>code</code> is <b>true</b>.
  6781. <p>
  6782. If the optional second argument <code>close</code> is true,
  6783. closes the Lua state before exiting.
  6784. <p>
  6785. <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
  6786. <p>
  6787. Returns the value of the process environment variable <code>varname</code>,
  6788. or <b>nil</b> if the variable is not defined.
  6789. <p>
  6790. <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
  6791. <p>
  6792. Deletes the file (or empty directory, on POSIX systems)
  6793. with the given name.
  6794. If this function fails, it returns <b>nil</b>,
  6795. plus a string describing the error and the error code.
  6796. Otherwise, it returns true.
  6797. <p>
  6798. <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
  6799. <p>
  6800. Renames the file or directory named <code>oldname</code> to <code>newname</code>.
  6801. If this function fails, it returns <b>nil</b>,
  6802. plus a string describing the error and the error code.
  6803. Otherwise, it returns true.
  6804. <p>
  6805. <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
  6806. <p>
  6807. Sets the current locale of the program.
  6808. <code>locale</code> is a system-dependent string specifying a locale;
  6809. <code>category</code> is an optional string describing which category to change:
  6810. <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
  6811. <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
  6812. the default category is <code>"all"</code>.
  6813. The function returns the name of the new locale,
  6814. or <b>nil</b> if the request cannot be honored.
  6815. <p>
  6816. If <code>locale</code> is the empty string,
  6817. the current locale is set to an implementation-defined native locale.
  6818. If <code>locale</code> is the string "<code>C</code>",
  6819. the current locale is set to the standard C locale.
  6820. <p>
  6821. When called with <b>nil</b> as the first argument,
  6822. this function only returns the name of the current locale
  6823. for the given category.
  6824. <p>
  6825. This function may be not thread safe
  6826. because of its reliance on C&nbsp;function <code>setlocale</code>.
  6827. <p>
  6828. <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
  6829. <p>
  6830. Returns the current time when called without arguments,
  6831. or a time representing the local date and time specified by the given table.
  6832. This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
  6833. and may have fields
  6834. <code>hour</code> (default is 12),
  6835. <code>min</code> (default is 0),
  6836. <code>sec</code> (default is 0),
  6837. and <code>isdst</code> (default is <b>nil</b>).
  6838. Other fields are ignored.
  6839. For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
  6840. <p>
  6841. The values in these fields do not need to be inside their valid ranges.
  6842. For instance, if <code>sec</code> is -10,
  6843. it means -10 seconds from the time specified by the other fields;
  6844. if <code>hour</code> is 1000,
  6845. it means +1000 hours from the time specified by the other fields.
  6846. <p>
  6847. The returned value is a number, whose meaning depends on your system.
  6848. In POSIX, Windows, and some other systems,
  6849. this number counts the number
  6850. of seconds since some given start time (the "epoch").
  6851. In other systems, the meaning is not specified,
  6852. and the number returned by <code>time</code> can be used only as an argument to
  6853. <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>.
  6854. <p>
  6855. <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
  6856. <p>
  6857. Returns a string with a file name that can
  6858. be used for a temporary file.
  6859. The file must be explicitly opened before its use
  6860. and explicitly removed when no longer needed.
  6861. <p>
  6862. In POSIX systems,
  6863. this function also creates a file with that name,
  6864. to avoid security risks.
  6865. (Someone else might create the file with wrong permissions
  6866. in the time between getting the name and creating the file.)
  6867. You still have to open the file to use it
  6868. and to remove it (even if you do not use it).
  6869. <p>
  6870. When possible,
  6871. you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
  6872. which automatically removes the file when the program ends.
  6873. <h2>6.10 &ndash; <a name="6.10">The Debug Library</a></h2>
  6874. <p>
  6875. This library provides
  6876. the functionality of the debug interface (<a href="#4.9">&sect;4.9</a>) to Lua programs.
  6877. You should exert care when using this library.
  6878. Several of its functions
  6879. violate basic assumptions about Lua code
  6880. (e.g., that variables local to a function
  6881. cannot be accessed from outside;
  6882. that userdata metatables cannot be changed by Lua code;
  6883. that Lua programs do not crash)
  6884. and therefore can compromise otherwise secure code.
  6885. Moreover, some functions in this library may be slow.
  6886. <p>
  6887. All functions in this library are provided
  6888. inside the <a name="pdf-debug"><code>debug</code></a> table.
  6889. All functions that operate over a thread
  6890. have an optional first argument which is the
  6891. thread to operate over.
  6892. The default is always the current thread.
  6893. <p>
  6894. <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
  6895. <p>
  6896. Enters an interactive mode with the user,
  6897. running each string that the user enters.
  6898. Using simple commands and other debug facilities,
  6899. the user can inspect global and local variables,
  6900. change their values, evaluate expressions, and so on.
  6901. A line containing only the word <code>cont</code> finishes this function,
  6902. so that the caller continues its execution.
  6903. <p>
  6904. Note that commands for <code>debug.debug</code> are not lexically nested
  6905. within any function and so have no direct access to local variables.
  6906. <p>
  6907. <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
  6908. <p>
  6909. Returns the current hook settings of the thread, as three values:
  6910. the current hook function, the current hook mask,
  6911. and the current hook count
  6912. (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function).
  6913. <p>
  6914. <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
  6915. <p>
  6916. Returns a table with information about a function.
  6917. You can give the function directly
  6918. or you can give a number as the value of <code>f</code>,
  6919. which means the function running at level <code>f</code> of the call stack
  6920. of the given thread:
  6921. level&nbsp;0 is the current function (<code>getinfo</code> itself);
  6922. level&nbsp;1 is the function that called <code>getinfo</code>
  6923. (except for tail calls, which do not count on the stack);
  6924. and so on.
  6925. If <code>f</code> is a number larger than the number of active functions,
  6926. then <code>getinfo</code> returns <b>nil</b>.
  6927. <p>
  6928. The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>,
  6929. with the string <code>what</code> describing which fields to fill in.
  6930. The default for <code>what</code> is to get all information available,
  6931. except the table of valid lines.
  6932. If present,
  6933. the option '<code>f</code>'
  6934. adds a field named <code>func</code> with the function itself.
  6935. If present,
  6936. the option '<code>L</code>'
  6937. adds a field named <code>activelines</code> with the table of
  6938. valid lines.
  6939. <p>
  6940. For instance, the expression <code>debug.getinfo(1,"n").name</code> returns
  6941. a name for the current function,
  6942. if a reasonable name can be found,
  6943. and the expression <code>debug.getinfo(print)</code>
  6944. returns a table with all available information
  6945. about the <a href="#pdf-print"><code>print</code></a> function.
  6946. <p>
  6947. <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3>
  6948. <p>
  6949. This function returns the name and the value of the local variable
  6950. with index <code>local</code> of the function at level <code>f</code> of the stack.
  6951. This function accesses not only explicit local variables,
  6952. but also parameters, temporaries, etc.
  6953. <p>
  6954. The first parameter or local variable has index&nbsp;1, and so on,
  6955. following the order that they are declared in the code,
  6956. counting only the variables that are active
  6957. in the current scope of the function.
  6958. Negative indices refer to vararg arguments;
  6959. -1 is the first vararg argument.
  6960. The function returns <b>nil</b> if there is no variable with the given index,
  6961. and raises an error when called with a level out of range.
  6962. (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
  6963. <p>
  6964. Variable names starting with '<code>(</code>' (open parenthesis)
  6965. represent variables with no known names
  6966. (internal variables such as loop control variables,
  6967. and variables from chunks saved without debug information).
  6968. <p>
  6969. The parameter <code>f</code> may also be a function.
  6970. In that case, <code>getlocal</code> returns only the name of function parameters.
  6971. <p>
  6972. <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3>
  6973. <p>
  6974. Returns the metatable of the given <code>value</code>
  6975. or <b>nil</b> if it does not have a metatable.
  6976. <p>
  6977. <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
  6978. <p>
  6979. Returns the registry table (see <a href="#4.5">&sect;4.5</a>).
  6980. <p>
  6981. <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3>
  6982. <p>
  6983. This function returns the name and the value of the upvalue
  6984. with index <code>up</code> of the function <code>f</code>.
  6985. The function returns <b>nil</b> if there is no upvalue with the given index.
  6986. <p>
  6987. Variable names starting with '<code>(</code>' (open parenthesis)
  6988. represent variables with no known names
  6989. (variables from chunks saved without debug information).
  6990. <p>
  6991. <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3>
  6992. <p>
  6993. Returns the Lua value associated to <code>u</code>.
  6994. If <code>u</code> is not a full userdata,
  6995. returns <b>nil</b>.
  6996. <p>
  6997. <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
  6998. <p>
  6999. Sets the given function as a hook.
  7000. The string <code>mask</code> and the number <code>count</code> describe
  7001. when the hook will be called.
  7002. The string mask may have any combination of the following characters,
  7003. with the given meaning:
  7004. <ul>
  7005. <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li>
  7006. <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li>
  7007. <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li>
  7008. </ul><p>
  7009. Moreover,
  7010. with a <code>count</code> different from zero,
  7011. the hook is called also after every <code>count</code> instructions.
  7012. <p>
  7013. When called without arguments,
  7014. <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
  7015. <p>
  7016. When the hook is called, its first argument is a string
  7017. describing the event that has triggered its call:
  7018. <code>"call"</code> (or <code>"tail call"</code>),
  7019. <code>"return"</code>,
  7020. <code>"line"</code>, and <code>"count"</code>.
  7021. For line events,
  7022. the hook also gets the new line number as its second parameter.
  7023. Inside a hook,
  7024. you can call <code>getinfo</code> with level&nbsp;2 to get more information about
  7025. the running function
  7026. (level&nbsp;0 is the <code>getinfo</code> function,
  7027. and level&nbsp;1 is the hook function).
  7028. <p>
  7029. <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>
  7030. <p>
  7031. This function assigns the value <code>value</code> to the local variable
  7032. with index <code>local</code> of the function at level <code>level</code> of the stack.
  7033. The function returns <b>nil</b> if there is no local
  7034. variable with the given index,
  7035. and raises an error when called with a <code>level</code> out of range.
  7036. (You can call <code>getinfo</code> to check whether the level is valid.)
  7037. Otherwise, it returns the name of the local variable.
  7038. <p>
  7039. See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
  7040. variable indices and names.
  7041. <p>
  7042. <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
  7043. <p>
  7044. Sets the metatable for the given <code>value</code> to the given <code>table</code>
  7045. (which can be <b>nil</b>).
  7046. Returns <code>value</code>.
  7047. <p>
  7048. <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
  7049. <p>
  7050. This function assigns the value <code>value</code> to the upvalue
  7051. with index <code>up</code> of the function <code>f</code>.
  7052. The function returns <b>nil</b> if there is no upvalue
  7053. with the given index.
  7054. Otherwise, it returns the name of the upvalue.
  7055. <p>
  7056. <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3>
  7057. <p>
  7058. Sets the given <code>value</code> as
  7059. the Lua value associated to the given <code>udata</code>.
  7060. <code>udata</code> must be a full userdata.
  7061. <p>
  7062. Returns <code>udata</code>.
  7063. <p>
  7064. <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
  7065. <p>
  7066. If <code>message</code> is present but is neither a string nor <b>nil</b>,
  7067. this function returns <code>message</code> without further processing.
  7068. Otherwise,
  7069. it returns a string with a traceback of the call stack.
  7070. The optional <code>message</code> string is appended
  7071. at the beginning of the traceback.
  7072. An optional <code>level</code> number tells at which level
  7073. to start the traceback
  7074. (default is 1, the function calling <code>traceback</code>).
  7075. <p>
  7076. <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3>
  7077. <p>
  7078. Returns a unique identifier (as a light userdata)
  7079. for the upvalue numbered <code>n</code>
  7080. from the given function.
  7081. <p>
  7082. These unique identifiers allow a program to check whether different
  7083. closures share upvalues.
  7084. Lua closures that share an upvalue
  7085. (that is, that access a same external local variable)
  7086. will return identical ids for those upvalue indices.
  7087. <p>
  7088. <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
  7089. <p>
  7090. Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
  7091. refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
  7092. <h1>7 &ndash; <a name="7">Lua Standalone</a></h1>
  7093. <p>
  7094. Although Lua has been designed as an extension language,
  7095. to be embedded in a host C&nbsp;program,
  7096. it is also frequently used as a standalone language.
  7097. An interpreter for Lua as a standalone language,
  7098. called simply <code>lua</code>,
  7099. is provided with the standard distribution.
  7100. The standalone interpreter includes
  7101. all standard libraries, including the debug library.
  7102. Its usage is:
  7103. <pre>
  7104. lua [options] [script [args]]
  7105. </pre><p>
  7106. The options are:
  7107. <ul>
  7108. <li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li>
  7109. <li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em> and assigns the
  7110. result to global @<em>mod</em>;</li>
  7111. <li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li>
  7112. <li><b><code>-v</code>: </b> prints version information;</li>
  7113. <li><b><code>-E</code>: </b> ignores environment variables;</li>
  7114. <li><b><code>--</code>: </b> stops handling options;</li>
  7115. <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li>
  7116. </ul><p>
  7117. After handling its options, <code>lua</code> runs the given <em>script</em>.
  7118. When called without arguments,
  7119. <code>lua</code> behaves as <code>lua -v -i</code>
  7120. when the standard input (<code>stdin</code>) is a terminal,
  7121. and as <code>lua -</code> otherwise.
  7122. <p>
  7123. When called without option <code>-E</code>,
  7124. the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
  7125. (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
  7126. before running any argument.
  7127. If the variable content has the format <code>@<em>filename</em></code>,
  7128. then <code>lua</code> executes the file.
  7129. Otherwise, <code>lua</code> executes the string itself.
  7130. <p>
  7131. When called with option <code>-E</code>,
  7132. besides ignoring <code>LUA_INIT</code>,
  7133. Lua also ignores
  7134. the values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>,
  7135. setting the values of
  7136. <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a>
  7137. with the default paths defined in <code>luaconf.h</code>.
  7138. <p>
  7139. All options are handled in order, except <code>-i</code> and <code>-E</code>.
  7140. For instance, an invocation like
  7141. <pre>
  7142. $ lua -e'a=1' -e 'print(a)' script.lua
  7143. </pre><p>
  7144. will first set <code>a</code> to 1, then print the value of <code>a</code>,
  7145. and finally run the file <code>script.lua</code> with no arguments.
  7146. (Here <code>$</code> is the shell prompt. Your prompt may be different.)
  7147. <p>
  7148. Before running any code,
  7149. <code>lua</code> collects all command-line arguments
  7150. in a global table called <code>arg</code>.
  7151. The script name goes to index 0,
  7152. the first argument after the script name goes to index 1,
  7153. and so on.
  7154. Any arguments before the script name
  7155. (that is, the interpreter name plus its options)
  7156. go to negative indices.
  7157. For instance, in the call
  7158. <pre>
  7159. $ lua -la b.lua t1 t2
  7160. </pre><p>
  7161. the table is like this:
  7162. <pre>
  7163. arg = { [-2] = "lua", [-1] = "-la",
  7164. [0] = "b.lua",
  7165. [1] = "t1", [2] = "t2" }
  7166. </pre><p>
  7167. If there is no script in the call,
  7168. the interpreter name goes to index 0,
  7169. followed by the other arguments.
  7170. For instance, the call
  7171. <pre>
  7172. $ lua -e "print(arg[1])"
  7173. </pre><p>
  7174. will print "<code>-e</code>".
  7175. If there is a script,
  7176. the script is called with arguments
  7177. <code>arg[1]</code>, &middot;&middot;&middot;, <code>arg[#arg]</code>.
  7178. (Like all chunks in Lua,
  7179. the script is compiled as a vararg function.)
  7180. <p>
  7181. In interactive mode,
  7182. Lua repeatedly prompts and waits for a line.
  7183. After reading a line,
  7184. Lua first try to interpret the line as an expression.
  7185. If it succeeds, it prints its value.
  7186. Otherwise, it interprets the line as a statement.
  7187. If you write an incomplete statement,
  7188. the interpreter waits for its completion
  7189. by issuing a different prompt.
  7190. <p>
  7191. If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string,
  7192. then its value is used as the prompt.
  7193. Similarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a string,
  7194. its value is used as the secondary prompt
  7195. (issued during incomplete statements).
  7196. <p>
  7197. In case of unprotected errors in the script,
  7198. the interpreter reports the error to the standard error stream.
  7199. If the error object is not a string but
  7200. has a metamethod <code>__tostring</code>,
  7201. the interpreter calls this metamethod to produce the final message.
  7202. Otherwise, the interpreter converts the error object to a string
  7203. and adds a stack traceback to it.
  7204. <p>
  7205. When finishing normally,
  7206. the interpreter closes its main Lua state
  7207. (see <a href="#lua_close"><code>lua_close</code></a>).
  7208. The script can avoid this step by
  7209. calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
  7210. <p>
  7211. To allow the use of Lua as a
  7212. script interpreter in Unix systems,
  7213. the standalone interpreter skips
  7214. the first line of a chunk if it starts with <code>#</code>.
  7215. Therefore, Lua scripts can be made into executable programs
  7216. by using <code>chmod +x</code> and the&nbsp;<code>#!</code> form,
  7217. as in
  7218. <pre>
  7219. #!/usr/local/bin/lua
  7220. </pre><p>
  7221. (Of course,
  7222. the location of the Lua interpreter may be different in your machine.
  7223. If <code>lua</code> is in your <code>PATH</code>,
  7224. then
  7225. <pre>
  7226. #!/usr/bin/env lua
  7227. </pre><p>
  7228. is a more portable solution.)
  7229. <h1>8 &ndash; <a name="8">Incompatibilities with the Previous Version</a></h1>
  7230. <p>
  7231. Here we list the incompatibilities that you may find when moving a program
  7232. from Lua&nbsp;5.2 to Lua&nbsp;5.3.
  7233. You can avoid some incompatibilities by compiling Lua with
  7234. appropriate options (see file <code>luaconf.h</code>).
  7235. However,
  7236. all these compatibility options will be removed in the future.
  7237. <p>
  7238. Lua versions can always change the C API in ways that
  7239. do not imply source-code changes in a program,
  7240. such as the numeric values for constants
  7241. or the implementation of functions as macros.
  7242. Therefore,
  7243. you should not assume that binaries are compatible between
  7244. different Lua versions.
  7245. Always recompile clients of the Lua API when
  7246. using a new version.
  7247. <p>
  7248. Similarly, Lua versions can always change the internal representation
  7249. of precompiled chunks;
  7250. precompiled chunks are not compatible between different Lua versions.
  7251. <p>
  7252. The standard paths in the official distribution may
  7253. change between versions.
  7254. <h2>8.1 &ndash; <a name="8.1">Changes in the Language</a></h2>
  7255. <ul>
  7256. <li>
  7257. The main difference between Lua&nbsp;5.2 and Lua&nbsp;5.3 is the
  7258. introduction of an integer subtype for numbers.
  7259. Although this change should not affect "normal" computations,
  7260. some computations
  7261. (mainly those that involve some kind of overflow)
  7262. can give different results.
  7263. <p>
  7264. You can fix these differences by forcing a number to be a float
  7265. (in Lua&nbsp;5.2 all numbers were float),
  7266. in particular writing constants with an ending <code>.0</code>
  7267. or using <code>x = x + 0.0</code> to convert a variable.
  7268. (This recommendation is only for a quick fix
  7269. for an occasional incompatibility;
  7270. it is not a general guideline for good programming.
  7271. For good programming,
  7272. use floats where you need floats
  7273. and integers where you need integers.)
  7274. </li>
  7275. <li>
  7276. The conversion of a float to a string now adds a <code>.0</code> suffix
  7277. to the result if it looks like an integer.
  7278. (For instance, the float 2.0 will be printed as <code>2.0</code>,
  7279. not as <code>2</code>.)
  7280. You should always use an explicit format
  7281. when you need a specific format for numbers.
  7282. <p>
  7283. (Formally this is not an incompatibility,
  7284. because Lua does not specify how numbers are formatted as strings,
  7285. but some programs assumed a specific format.)
  7286. </li>
  7287. <li>
  7288. The generational mode for the garbage collector was removed.
  7289. (It was an experimental feature in Lua&nbsp;5.2.)
  7290. </li>
  7291. </ul>
  7292. <h2>8.2 &ndash; <a name="8.2">Changes in the Libraries</a></h2>
  7293. <ul>
  7294. <li>
  7295. The <code>bit32</code> library has been deprecated.
  7296. It is easy to require a compatible external library or,
  7297. better yet, to replace its functions with appropriate bitwise operations.
  7298. (Keep in mind that <code>bit32</code> operates on 32-bit integers,
  7299. while the bitwise operators in Lua&nbsp;5.3 operate on Lua integers,
  7300. which by default have 64&nbsp;bits.)
  7301. </li>
  7302. <li>
  7303. The Table library now respects metamethods
  7304. for setting and getting elements.
  7305. </li>
  7306. <li>
  7307. The <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and
  7308. its <code>__ipairs</code> metamethod has been deprecated.
  7309. </li>
  7310. <li>
  7311. Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
  7312. For compatibility, Lua will continue to accept (and ignore) this character.
  7313. </li>
  7314. <li>
  7315. The following functions were deprecated in the mathematical library:
  7316. <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>,
  7317. <code>frexp</code>, and <code>ldexp</code>.
  7318. You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
  7319. you can replace <code>math.atan2</code> with <code>math.atan</code>,
  7320. which now accepts one or two arguments;
  7321. you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
  7322. For the other operations,
  7323. you can either use an external library or
  7324. implement them in Lua.
  7325. </li>
  7326. <li>
  7327. The searcher for C loaders used by <a href="#pdf-require"><code>require</code></a>
  7328. changed the way it handles versioned names.
  7329. Now, the version should come after the module name
  7330. (as is usual in most other tools).
  7331. For compatibility, that searcher still tries the old format
  7332. if it cannot find an open function according to the new style.
  7333. (Lua&nbsp;5.2 already worked that way,
  7334. but it did not document the change.)
  7335. </li>
  7336. <li>
  7337. The call <code>collectgarbage("count")</code> now returns only one result.
  7338. (You can compute that second result from the fractional part
  7339. of the first result.)
  7340. </li>
  7341. </ul>
  7342. <h2>8.3 &ndash; <a name="8.3">Changes in the API</a></h2>
  7343. <ul>
  7344. <li>
  7345. Continuation functions now receive as arguments what they needed
  7346. to get through <code>lua_getctx</code>,
  7347. so <code>lua_getctx</code> has been removed.
  7348. Adapt your code accordingly.
  7349. </li>
  7350. <li>
  7351. Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
  7352. Use 0 as the value of this parameter to get the old behavior.
  7353. </li>
  7354. <li>
  7355. Functions to inject/project unsigned integers
  7356. (<code>lua_pushunsigned</code>, <code>lua_tounsigned</code>, <code>lua_tounsignedx</code>,
  7357. <code>luaL_checkunsigned</code>, <code>luaL_optunsigned</code>)
  7358. were deprecated.
  7359. Use their signed equivalents with a type cast.
  7360. </li>
  7361. <li>
  7362. Macros to project non-default integer types
  7363. (<code>luaL_checkint</code>, <code>luaL_optint</code>, <code>luaL_checklong</code>, <code>luaL_optlong</code>)
  7364. were deprecated.
  7365. Use their equivalent over <a href="#lua_Integer"><code>lua_Integer</code></a> with a type cast
  7366. (or, when possible, use <a href="#lua_Integer"><code>lua_Integer</code></a> in your code).
  7367. </li>
  7368. </ul>
  7369. <h1>9 &ndash; <a name="9">The Complete Syntax of Lua</a></h1>
  7370. <p>
  7371. Here is the complete syntax of Lua in extended BNF.
  7372. As usual in extended BNF,
  7373. {A} means 0 or more As,
  7374. and [A] means an optional A.
  7375. (For operator precedences, see <a href="#3.4.8">&sect;3.4.8</a>;
  7376. for a description of the terminals
  7377. Name, Numeral,
  7378. and LiteralString, see <a href="#3.1">&sect;3.1</a>.)
  7379. <pre>
  7380. chunk ::= block
  7381. block ::= {stat} [retstat]
  7382. stat ::= &lsquo;<b>;</b>&rsquo; |
  7383. varlist &lsquo;<b>=</b>&rsquo; explist |
  7384. functioncall |
  7385. label |
  7386. <b>break</b> |
  7387. <b>goto</b> Name |
  7388. <b>do</b> block <b>end</b> |
  7389. <b>while</b> exp <b>do</b> block <b>end</b> |
  7390. <b>repeat</b> block <b>until</b> exp |
  7391. <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
  7392. <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> |
  7393. <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> |
  7394. <b>function</b> funcname funcbody |
  7395. <b>local</b> <b>function</b> Name funcbody |
  7396. <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
  7397. retstat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
  7398. label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
  7399. funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
  7400. varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
  7401. var ::= Name | prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; | prefixexp &lsquo;<b>.</b>&rsquo; Name
  7402. namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
  7403. explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
  7404. exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | &lsquo;<b>...</b>&rsquo; | functiondef |
  7405. prefixexp | tableconstructor | exp binop exp | unop exp
  7406. prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
  7407. functioncall ::= prefixexp args | prefixexp &lsquo;<b>:</b>&rsquo; Name args
  7408. args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo; | tableconstructor | LiteralString
  7409. functiondef ::= <b>function</b> funcbody
  7410. funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
  7411. parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
  7412. tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
  7413. fieldlist ::= field {fieldsep field} [fieldsep]
  7414. field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
  7415. fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
  7416. 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; |
  7417. &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; |
  7418. &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; |
  7419. <b>and</b> | <b>or</b>
  7420. unop ::= &lsquo;<b>-</b>&rsquo; | <b>not</b> | &lsquo;<b>#</b>&rsquo; | &lsquo;<b>~</b>&rsquo;
  7421. </pre>
  7422. <p>
  7423. <P CLASS="footer">
  7424. Last update:
  7425. Tue Jun 26 13:16:37 -03 2018
  7426. </P>
  7427. <!--
  7428. Last change: revised for Lua 5.3.5
  7429. -->
  7430. </body></html>