pm 286 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669
  1. #!/bin/sh
  2. # Prime Mover
  3. #
  4. # (C) 2006 David Given.
  5. # Prime Mover is licensed under the MIT open source license. To get the full
  6. # license text, run this file with the '--license' option.
  7. #
  8. # $Id: shell 52 2006-10-03 22:12:33Z dtrg $
  9. if [ -x "$(which arch 2>/dev/null)" ]; then
  10. ARCH="$(arch)"
  11. elif [ -x "$(which machine 2>/dev/null)" ]; then
  12. ARCH="$(machine)"
  13. elif [ -x "$(which uname 2>/dev/null)" ]; then
  14. ARCH="$(uname -m)"
  15. else
  16. echo "pm: unable to determine target type, proceeding anyway"
  17. ARCH=unknown
  18. fi
  19. THISFILE="$0"
  20. PMEXEC="./.pm-exec-$ARCH"
  21. set -e
  22. GZFILE=/tmp/pm-$$.gz
  23. CFILE=/tmp/pm-$$.c
  24. trap "rm -f $GZFILE $CFILE" EXIT
  25. extract_section() {
  26. sed -e "1,/^XXXXSTART$1/d" "$THISFILE" | (
  27. read size
  28. dd bs=1 count=$size 2> /dev/null
  29. ) > $GZFILE
  30. cat $GZFILE | cat
  31. }
  32. # If the bootstrap's built, run it.
  33. if [ "$PMEXEC" -nt $0 ]; then
  34. extract_section script | "$PMEXEC" /dev/stdin "$@"
  35. exit $?
  36. fi
  37. # Otherwise, compile it and restart.
  38. echo "pm: bootstrapping..."
  39. if [ -x "$(which gcc 2>/dev/null)" ]; then
  40. CC="gcc -O -s"
  41. else
  42. CC="cc"
  43. fi
  44. extract_section interpreter > /tmp/pm-$$.c
  45. $CC $CFILE -o "$PMEXEC" && exec "$THISFILE" "$@"
  46. echo "pm: bootstrap failed."
  47. exit 1
  48. XXXXSTARTscript
  49. 38405
  50. #!/usr/bin/lua
  51. -- Prime Mover
  52. --
  53. -- © 2006 David Given.
  54. -- Prime Mover is licensed under the MIT open source license. Search
  55. -- for 'MIT' in this file to find the full license text.
  56. --
  57. -- $Id: pm 93 2007-02-24 21:20:53Z dtrg $
  58. -- ======================================================================= --
  59. -- GLOBALS --
  60. -- ======================================================================= --
  61. local VERSION = "0.1.1"
  62. -- Fast versions of useful system variables.
  63. local stdin = io.stdin
  64. local stdout = io.stdout
  65. local stderr = io.stderr
  66. local string_find = string.find
  67. local string_gsub = string.gsub
  68. local string_sub = string.sub
  69. local string_byte = string.byte
  70. local table_insert = table.insert
  71. local table_getn = table.getn
  72. local table_concat = table.concat
  73. local posix_stat = posix.stat
  74. local posix_readlink = posix.readlink
  75. local posix_unlink = posix.unlink
  76. local posix_rmdir = posix.rmdir
  77. local os_time = os.time
  78. local _G = _G
  79. local _
  80. -- Option settings.
  81. local delete_output_files_on_error = true
  82. local purge_intermediate_cache = false
  83. local no_execute = false
  84. local input_files = {}
  85. local targets = {}
  86. intermediate_cache_dir = ".pm-cache/"
  87. verbose = false
  88. quiet = false
  89. -- Application globals.
  90. local sandbox = {}
  91. local scope = {object=sandbox, next=nil}
  92. local intermediate_cache = {}
  93. local intermediate_cache_count = 0
  94. local buildstages = 0
  95. -- Atoms.
  96. local PARENT = {}
  97. local EMPTY = {}
  98. local REDIRECT = {}
  99. -- Exported symbols (set to dummy values).
  100. message = 0
  101. filetime = 0
  102. filetouch = 0
  103. install = 0
  104. rendertable = 0
  105. stringmodifier = {}
  106. setmetatable(_G, {__newindex = function(t, key, value)
  107. error("Attempt to write to new global "..key)
  108. end})
  109. -- ======================================================================= --
  110. -- UTILITIES --
  111. -- ======================================================================= --
  112. local function message(...)
  113. stderr:write("pm: ")
  114. stderr:write(unpack(arg))
  115. stderr:write("\n")
  116. end
  117. _G.message = message
  118. local function usererror(...)
  119. stderr:write("pm: ")
  120. stderr:write(unpack(arg))
  121. stderr:write("\n")
  122. os.exit(1)
  123. end
  124. local function traceoutput(...)
  125. stdout:write(unpack(arg))
  126. stdout:write("\n")
  127. end
  128. local function assert(message, result, e)
  129. if result then
  130. return result
  131. end
  132. if (type(message) == "string") then
  133. message = {message}
  134. end
  135. table.insert(message, ": ")
  136. table.insert(message, e)
  137. usererror(unpack(message))
  138. end
  139. -- Concatenates the contents of its arguments to the specified table.
  140. -- (Numeric indices only.)
  141. local function table_append(t, ...)
  142. for _, i in ipairs(arg) do
  143. if (type(i) == "table") then
  144. for _, j in ipairs(i) do
  145. table_insert(t, j)
  146. end
  147. else
  148. table_insert(t, i)
  149. end
  150. end
  151. end
  152. -- Merge the contents of its arguments to the specified table.
  153. -- (Name indices. Will break on numeric indices.)
  154. local function table_merge(t, ...)
  155. for _, i in ipairs(arg) do
  156. for j, k in pairs(i) do
  157. t[j] = k
  158. end
  159. end
  160. end
  161. -- Turn a list of strings into a single quoted string.
  162. function rendertable(i)
  163. if (type(i) == "string") or (type(i) == "number") then
  164. return i
  165. end
  166. if (i == nil) or (i == EMPTY) then
  167. return ""
  168. end
  169. local t = {}
  170. for _, j in ipairs(i) do
  171. local r = string_gsub(j, "\\", "\\\\")
  172. r = string_gsub(r, '"', '\\"')
  173. table_insert(t, r)
  174. end
  175. return '"'..table_concat(t, '" "')..'"'
  176. end
  177. local rendertable = rendertable
  178. -- Returns just the directory part of a path.
  179. local function dirname(f)
  180. local f, n = string_gsub(f, "/[^/]*$", "")
  181. if (n == 0) then
  182. return "."
  183. end
  184. return f
  185. end
  186. posix.dirname = dirname
  187. -- Makes an absolute path.
  188. local function absname(f)
  189. if string.find(f, "^/") then
  190. return f
  191. end
  192. return posix.getcwd().."/"..f
  193. end
  194. posix.absname = absname
  195. -- Copies a file.
  196. local function copy(src, dest)
  197. local s = string_gsub(src, "'", "'\"'\"'")
  198. local d = string_gsub(dest, "'", "'\"'\"'")
  199. local r = os.execute("cp '"..s.."' '"..d.."'")
  200. if (r ~= 0) then
  201. return nil, "unable to copy file"
  202. end
  203. return 0, nil
  204. end
  205. posix.copy = copy
  206. -- Makes all directories that contain f
  207. local function mkcontainerdir(f)
  208. f = dirname(f)
  209. if not posix_stat(f, "type") then
  210. mkcontainerdir(f)
  211. local r = posix.mkdir(f)
  212. if not r then
  213. usererror("unable to create directory '"..f.."'")
  214. end
  215. end
  216. end
  217. -- Install a file (suitable as a command list entry).
  218. local function do_install(self, src, dest)
  219. src = absname(self:__expand(src))
  220. dest = absname(self:__expand(dest))
  221. if verbose then
  222. message("installing '", src, "' --> '", dest, "'")
  223. end
  224. mkcontainerdir(dest)
  225. local f, e = posix.symlink(src, dest)
  226. if f then
  227. return
  228. end
  229. if (e ~= nil) then
  230. f, e = posix.unlink(dest)
  231. if f then
  232. f, e = posix.symlink(src, dest)
  233. if f then
  234. return
  235. end
  236. end
  237. end
  238. self:__error("couldn't install '", src, "' to '", dest,
  239. "': ", e)
  240. end
  241. function install(src, dest)
  242. return function(self, inputs, outputs)
  243. local src = src
  244. local dest = dest
  245. if (dest == nil) then
  246. dest = src
  247. src = outputs[1]
  248. end
  249. if (type(src) ~= "string") then
  250. self:__error("pm.install needs a string or an object for an input")
  251. end
  252. if (type(dest) ~= "string") then
  253. self:__error("pm.install needs a string for a destination")
  254. end
  255. return do_install(self, src, dest)
  256. end
  257. end
  258. -- Perform an error traceback.
  259. local function traceback(e)
  260. local i = 1
  261. while true do
  262. local t = debug.getinfo(i)
  263. if not t then
  264. break
  265. end
  266. if (t.short_src ~= "stdin") and (t.short_src ~= "[C]") then
  267. if (t.currentline == -1) then
  268. t.currentline = ""
  269. end
  270. message(" ", t.short_src, ":", t.currentline)
  271. end
  272. i = i + 1
  273. end
  274. e = string_gsub(e, "^stdin:[0-9]*: ", "")
  275. usererror("error: ", e)
  276. end
  277. -- ======================================================================= --
  278. -- CACHE MANAGEMENT --
  279. -- ======================================================================= --
  280. local statted_files = {}
  281. local function clear_stat_cache()
  282. statted_files = {}
  283. end
  284. -- Returns the timestamp of a file, or 0 if it doesn't exist.
  285. local statted_files = {}
  286. local function filetime(f)
  287. local t = statted_files[f]
  288. if t then
  289. return t
  290. end
  291. -- Stupid BeOS doesn't dereference symlinks on stat().
  292. local realf = f
  293. while true do
  294. local newf, e = posix_readlink(realf)
  295. if e then
  296. break
  297. end
  298. realf = newf
  299. end
  300. t = posix_stat(realf, "mtime") or 0
  301. statted_files[f] = t
  302. return t
  303. end
  304. _G.filetime = filetime
  305. -- Pretends to touch a file by manipulating the stat cache.
  306. local function filetouch(f)
  307. if (type(f) == "string") then
  308. f = {f}
  309. end
  310. local t = os_time()
  311. for _, i in ipairs(f) do
  312. statted_files[i] = t
  313. end
  314. end
  315. _G.filetouch = filetouch
  316. local function create_intermediate_cache()
  317. local d = dirname(intermediate_cache_dir)
  318. if not quiet then
  319. message("creating new intermediate file cache in '"..d.."'")
  320. end
  321. -- Attempt to wipe the old cache directory.
  322. local f = posix.files(d)
  323. if not f then
  324. -- The directory doesn't exist, so create it.
  325. mkcontainerdir(d)
  326. f = posix.mkdir(d)
  327. if not f then
  328. usererror("unable to create intermediate file cache directory")
  329. end
  330. else
  331. -- The directory exists. Delete all files in it recursively.
  332. local function rmdir(root)
  333. local f = posix.files(root)
  334. if not f then
  335. return
  336. end
  337. for i in f do
  338. if ((i ~= ".") and (i ~= "..")) then
  339. local fn = root.."/"..i
  340. local t = posix_stat(fn, "type")
  341. if (t == "regular") then
  342. if not posix_unlink(fn) then
  343. usererror("unable to purge intermediate file cache directory")
  344. end
  345. elseif (t == "directory") then
  346. rmdir(fn)
  347. posix_rmdir(fn)
  348. end
  349. end
  350. end
  351. end
  352. rmdir(d)
  353. end
  354. end
  355. local function save_intermediate_cache()
  356. local fn = intermediate_cache_dir.."index"
  357. local f = io.open(fn, "w")
  358. if not f then
  359. usererror("unable to save intermediate cache index file '", fn, "'")
  360. end
  361. f:write(intermediate_cache_count, "\n")
  362. for i, j in pairs(intermediate_cache) do
  363. f:write(i, "\n")
  364. f:write(j, "\n")
  365. end
  366. f:close()
  367. end
  368. local function load_intermediate_cache()
  369. local fn = intermediate_cache_dir.."index"
  370. local f = io.open(fn, "r")
  371. if not f then
  372. create_intermediate_cache()
  373. return
  374. end
  375. intermediate_cache_count = f:read("*l")
  376. while true do
  377. local l1 = f:read("*l")
  378. local l2 = f:read("*l")
  379. if (l1 == nil) or (l2 == nil) then
  380. break
  381. end
  382. intermediate_cache[l1] = l2
  383. end
  384. f:close()
  385. end
  386. local function create_intermediate_cache_key(key)
  387. local u = intermediate_cache[key]
  388. if not u then
  389. intermediate_cache_count = intermediate_cache_count + 1
  390. u = intermediate_cache_count
  391. intermediate_cache[key] = u
  392. save_intermediate_cache()
  393. end
  394. return u
  395. end
  396. -- ======================================================================= --
  397. -- STRING MODIFIERS --
  398. -- ======================================================================= --
  399. function stringmodifier.dirname(self, s)
  400. if (type(s) == "table") then
  401. if (table_getn(s) == 1) then
  402. s = s[1]
  403. else
  404. self:__error("tried to use string modifier 'dirname' on a table with more than one entry")
  405. end
  406. end
  407. return dirname(s)
  408. end
  409. -- ======================================================================= --
  410. -- CLASS SYSTEM --
  411. -- ======================================================================= --
  412. --- Base class --------------------------------------------------------------
  413. local metaclass = {
  414. class = "metaclass",
  415. -- Creates a new instance of a class by creating a new object and cloning
  416. -- all properties of the called class onto it.
  417. __call = function (self, ...)
  418. local o = {}
  419. for i, j in pairs(self) do
  420. o[i] = j
  421. end
  422. setmetatable(o, o)
  423. -- Determine where this object was defined.
  424. local i = 1
  425. while true do
  426. local s = debug.getinfo(i, "Sl")
  427. if s then
  428. if (string_byte(s.source) == 64) then
  429. o.definedat = string_sub(s.source, 2)..":"..s.currentline
  430. end
  431. else
  432. break
  433. end
  434. i = i + 1
  435. end
  436. -- Call the object's constructor and return it.
  437. o:__init(unpack(arg))
  438. return o
  439. end,
  440. -- Dummy constructor.
  441. __init = function (self, ...)
  442. end,
  443. }
  444. setmetatable(metaclass, metaclass)
  445. --- Top-level build node ----------------------------------------------------
  446. local node = metaclass()
  447. node.class = "node"
  448. -- When constructed, nodes initialise themselves from a supplied table of
  449. -- properties. All node children take exactly one argument, allowing the
  450. -- "constructor {properties}" construction pattern.
  451. function node:__init(t)
  452. metaclass.__init(self)
  453. if (type(t) == "string") then
  454. t = {t}
  455. end
  456. if (type(t) ~= "table") then
  457. self:__error("can't be constructed with a ", type(t), "; try a table or a string")
  458. end
  459. -- Copy over non-numeric parameters.
  460. for i, j in pairs(t) do
  461. if (tonumber(i) == nil) then
  462. self[i] = j
  463. end
  464. end
  465. -- Copy any numeric parameters.
  466. for _, i in ipairs(t) do
  467. table_insert(self, i)
  468. end
  469. -- If we're a class, don't verify.
  470. if t.class then
  471. return
  472. end
  473. -- ensure_n_children
  474. -- When true, ensures that the node has exactly the number of children
  475. -- specified.
  476. if self.ensure_n_children then
  477. local n = self.ensure_n_children
  478. if (table.getn(self) ~= n) then
  479. local one
  480. if (n == 1) then
  481. one = "one child"
  482. else
  483. one = n.." children"
  484. end
  485. self:_error("must have exactly ", one)
  486. end
  487. end
  488. -- ensure_at_least_one_child
  489. -- When true, ensures the the node has at least one child.
  490. if self.ensure_at_least_one_child then
  491. if (table_getn(self) < 1) then
  492. self:__error("must have at least one child")
  493. end
  494. end
  495. -- construct_string_children_with
  496. -- If set, any string children are automatically converted using the
  497. -- specified constructor.
  498. if self.construct_string_children_with then
  499. local constructor = self.construct_string_children_with
  500. for i, j in ipairs(self) do
  501. if (type(j) == "string") then
  502. self[i] = constructor {j}
  503. end
  504. end
  505. end
  506. -- all_children_are_objects
  507. -- When true, verifies that all children are objects and not something
  508. -- else (such as strings).
  509. if self.all_children_are_objects then
  510. for i, j in ipairs(self) do
  511. if (type(j) ~= "table") then
  512. self:__error("doesn't know what to do with child ", i,
  513. ", which is a ", type(j))
  514. end
  515. end
  516. end
  517. -- Ensure that self.install is valid.
  518. if self.install then
  519. local t = type(self.install)
  520. if (t == "string") or
  521. (t == "function") then
  522. self.install = {self.install}
  523. end
  524. if (type(self.install) ~= "table") then
  525. self:__error("doesn't know what to do with its installation command, ",
  526. "which is a ", type(self.install), " but should be a table, function ",
  527. "or string")
  528. end
  529. end
  530. end
  531. -- If an attempt is made to access a variable on a node that doesn't exist,
  532. -- and the variable starts with a capital letter, it's looked up in the
  533. -- property scope.
  534. function node:__index(key)
  535. local i = string_byte(key, 1)
  536. if (i >= 65) and (i <= 90) then
  537. -- Scan up the class hierarchy.
  538. local recurse
  539. recurse = function(s, key)
  540. if not s then
  541. return nil
  542. end
  543. local o = rawget(s.object, key)
  544. if o then
  545. if (type(o) == "table") then
  546. -- Handle lists of the form {PARENT, "foo", "bar"...}
  547. if (o[1] == PARENT) then
  548. local parent = recurse(s.next, key)
  549. local newo = {}
  550. if parent then
  551. if (type(parent) ~= "table") then
  552. parent = {parent}
  553. end
  554. for _, j in ipairs(parent) do
  555. table_insert(newo, j)
  556. end
  557. end
  558. for _, j in ipairs(o) do
  559. if (j ~= PARENT) then
  560. table_insert(newo, j)
  561. end
  562. end
  563. return newo
  564. -- Handle lists of the form {REDIRECT, "newkey"}
  565. elseif (o[1] == REDIRECT) then
  566. return self:__index(o[2])
  567. end
  568. end
  569. return o
  570. end
  571. -- Tail recursion.
  572. return recurse(s.next, key)
  573. end
  574. -- We want this node looked at first, so fake up a scope entry for it.
  575. local fakescope = {
  576. next = scope,
  577. object = self
  578. }
  579. -- Tail recursion.
  580. return recurse(fakescope, key)
  581. end
  582. -- For local properties, just return what's here.
  583. return rawget(self, key)
  584. end
  585. -- Little utility that emits an error message.
  586. function node:__error(...)
  587. usererror("object '", self.class, "', defined at ",
  588. self.definedat, ", ", unpack(arg))
  589. end
  590. -- Causes a node to return its outputs; that is, the files the node will
  591. -- produce when built. The parameter contains a list of input filenames; the
  592. -- outputs of the node's children.
  593. function node:__outputs(inputs)
  594. self:__error("didn't implement __outputs when it should have")
  595. end
  596. -- Causes a node to return its dependencies; that is, a list of *filenames*
  597. -- whose timestamps need to be considered when checking whether a node needs
  598. -- to be rebuilt. This is usually, but not always, the same as the inputs.
  599. function node:__dependencies(inputs, outputs)
  600. return inputs
  601. end
  602. -- Returns the node's timestamp. It will only get built if this is older than its
  603. -- children's timestamps.
  604. function node:__timestamp(inputs, outputs)
  605. local t = 0
  606. for _, i in ipairs(outputs) do
  607. local tt = filetime(i)
  608. if (tt > t) then
  609. t = tt
  610. end
  611. end
  612. return t
  613. end
  614. -- Unconditionally builds the nodes' children, collating their outputs. We
  615. -- push a new scope while we do so, to make this object's definitions visible
  616. -- to the children. (Almost never overridden. Only file() will want to do
  617. -- this, most likely.)
  618. function node:__buildchildren()
  619. local inputs = {}
  620. scope = {object=self, next=scope}
  621. for _, i in ipairs(self) do
  622. table_append(inputs, i:__build())
  623. end
  624. self:__buildadditionalchildren()
  625. scope = scope.next
  626. return inputs
  627. end
  628. -- Provides a hook for building any additional children that aren't actually
  629. -- in the child list.
  630. function node:__buildadditionalchildren()
  631. end
  632. -- Cause the node's children to be built, collating their outputs, and if
  633. -- any output is newer than the node itself, causes the node to be built.
  634. function node:__build()
  635. -- Build children and collate their outputs. These will become this node's
  636. -- inputs.
  637. local inputs = self:__buildchildren()
  638. -- Determine the node's outputs. This will usually be automatically
  639. -- generated, in which case the name will depend on the overall environment ---
  640. -- including the inputs.
  641. local outputs = self:__outputs(inputs)
  642. -- Get the current node's timestamp. If anything this node depends on is
  643. -- newer than that, the node needs rebuilding.
  644. local t = self:__timestamp(inputs, outputs)
  645. local depends = self:__dependencies(inputs, outputs)
  646. local rebuild = false
  647. if (t == 0) then
  648. rebuild = true
  649. end
  650. if (not rebuild and depends) then
  651. for _, i in ipairs(depends) do
  652. local tt = filetime(i)
  653. -- message("comparing ", t, " with ", tt, " (", rendertable({i}), ")")
  654. if (tt > t) then
  655. if verbose then
  656. message("rebuilding ", self.class, " because ", i, " newer than ", rendertable(outputs))
  657. end
  658. rebuild = true
  659. break
  660. end
  661. end
  662. end
  663. if rebuild then
  664. self:__dobuild(inputs, outputs)
  665. filetouch(outputs)
  666. end
  667. -- If an installation command was specified, execute it now.
  668. if self.install then
  669. self:__invoke(self.install, inputs, outputs)
  670. end
  671. -- And return this nodes' outputs.
  672. return outputs
  673. end
  674. -- Builds this node from the specified input files (the node's childrens'
  675. -- outputs).
  676. function node:__dobuild(inputs, outputs)
  677. self:__error("didn't implement __dobuild when it should have")
  678. end
  679. -- Recursively expands any variables in a string.
  680. function node:__expand(s)
  681. local searching = true
  682. while searching do
  683. searching = false
  684. -- Expand %{expressions}%
  685. s = string_gsub(s, "%%{(.-)}%%", function (expr)
  686. searching = true
  687. local f, e = loadstring(expr, "expression")
  688. if not f then
  689. self:__error("couldn't compile the expression '", expr, "': ", e)
  690. end
  691. local env = {self=self}
  692. setmetatable(env, {
  693. __index = function(_, key)
  694. return sandbox[key]
  695. end
  696. })
  697. setfenv(f, env)
  698. f, e = pcall(f, self)
  699. if not f then
  700. self:__error("couldn't evaluate the expression '", expr, "': ", e)
  701. end
  702. return rendertable(e)
  703. end)
  704. -- Expand %varnames%
  705. s = string_gsub(s, "%%(.-)%%", function (varname)
  706. searching = true
  707. -- Parse the string reference.
  708. local _, _, leftcolon, rightcolon = string_find(varname, "([^:]*):?(.*)$")
  709. local _, _, varname, selectfrom, hyphen, selectto = string_find(leftcolon, "^([^[]*)%[?([^-%]]*)(%-?)([^%]]*)]?$")
  710. -- Get the basic value that the rest of the reference is going to
  711. -- depend on.
  712. local result = self:__index(varname)
  713. if not result then
  714. self:__error("doesn't understand variable '", varname, "'")
  715. end
  716. -- Process any selector, if specified.
  717. if (selectfrom ~= "") or (hyphen ~= "") or (selectto ~= "") then
  718. if (type(result) ~= "table") then
  719. self:__error("tried to use a [] selector on variable '", varname,
  720. "', which doesn't contain a table")
  721. end
  722. local n = table_getn(result)
  723. selectfrom = tonumber(selectfrom)
  724. selectto = tonumber(selectto)
  725. if (hyphen ~= "") then
  726. if not selectfrom then
  727. selectfrom = 1
  728. end
  729. if not selectto then
  730. selectto = n
  731. end
  732. else
  733. if not selectto then
  734. selectto = selectfrom
  735. end
  736. if not selectfrom then
  737. self:__error("tried to use an empty selector on variable '", varname, "'")
  738. end
  739. end
  740. if (selectfrom < 1) or (selectto < 1) or
  741. (selectfrom > n) or (selectto > n) or
  742. (selectto < selectfrom) then
  743. self:__error("tried to use an invalid selector [",
  744. selectfrom, "-", selectto, "] on variable '", varname,
  745. "'; only [1-", n, "] is valid")
  746. end
  747. local newresult = {}
  748. for i = selectfrom, selectto do
  749. table_insert(newresult, result[i])
  750. end
  751. result = newresult
  752. end
  753. -- Process any string modifier, if supplied.
  754. if (rightcolon ~= "") then
  755. local f = stringmodifier[rightcolon]
  756. if not f then
  757. self:__error("tried to use an unknown string modifier '",
  758. rightcolon, "' on variable '", varname, "'")
  759. end
  760. result = f(self, result)
  761. end
  762. return rendertable(result)
  763. end)
  764. end
  765. -- Any remaining %% sequences must be empty, and so convert them into
  766. -- single % sequences.
  767. s = string_gsub(s, "%%%%", "%")
  768. return s
  769. end
  770. -- Expands any variables in a command table, and executes it.
  771. function node:__invoke(command, inputs, outputs)
  772. if (type(command) ~= "table") then
  773. command = {command}
  774. end
  775. for _, s in ipairs(command) do
  776. if (type(s) == "string") then
  777. s = self:__expand(s)
  778. if not quiet then
  779. traceoutput(s)
  780. end
  781. if not no_execute then
  782. local r = os.execute(s)
  783. if (r ~= 0) then
  784. return r
  785. end
  786. end
  787. elseif (type(s) == "function") then
  788. local r = s(self, inputs, outputs)
  789. if r then
  790. return r
  791. end
  792. end
  793. end
  794. return false
  795. end
  796. -- ======================================================================= --
  797. -- PROLOGUE --
  798. -- ======================================================================= --
  799. -- The prologue contains the standard library that all pmfiles can refer to.
  800. -- For simplicity, it's implemented by code running inside the sandbox,
  801. -- which means that it's basically identical to user code (and could, in
  802. -- fact, be kept in a seperate file).
  803. -- Here we set up the sandbox.
  804. table_merge(sandbox, {
  805. VERSION = VERSION,
  806. assert = assert,
  807. collectgarbage = collectgarbage,
  808. dofile = dofile,
  809. error = error,
  810. getfenv = getfenv,
  811. getmetatable = getmetatable,
  812. gcinfo = gcinfo,
  813. ipairs = ipairs,
  814. loadfile = loadfile,
  815. loadlib = loadlib,
  816. loadstring = loadstring,
  817. next = next,
  818. pairs = pairs,
  819. pcall = pcall,
  820. print = print,
  821. rawequal = rawequal,
  822. rawget = rawget,
  823. rawset = rawset,
  824. require = require,
  825. setfenv = setfenv,
  826. setmetatable = setmetatable,
  827. tonumber = tonumber,
  828. tostring = tostring,
  829. type = type,
  830. unpack = unpack,
  831. _VERSION = _VERSION,
  832. xpcall = xpcall,
  833. table = table,
  834. io = io,
  835. os = os,
  836. posix = posix,
  837. string = string,
  838. debug = debug,
  839. loadlib = loadlib,
  840. pm = _G,
  841. node = node,
  842. PARENT = PARENT,
  843. EMPTY = EMPTY,
  844. REDIRECT = REDIRECT,
  845. })
  846. -- Cause any reads from undefined keys in the sandbox to fail with an error.
  847. -- This helps debugging pmfiles somewhat.
  848. setmetatable(sandbox, {
  849. __index = function(self, key)
  850. local value = rawget(self, key)
  851. if (value == nil) then
  852. error(key.." could not be found in any applicable scope")
  853. end
  854. return value
  855. end
  856. })
  857. -- Switch into sandbox mode.
  858. setfenv(1, sandbox)
  859. --- Assorted utilities ------------------------------------------------------
  860. -- Includes a file.
  861. function include(f, ...)
  862. local c, e = loadfile(f)
  863. if not c then
  864. usererror("script compilation error: ", e)
  865. end
  866. setfenv(c, sandbox)
  867. local arguments = arg
  868. xpcall(
  869. function()
  870. c(unpack(arguments))
  871. end,
  872. function(e)
  873. message("script execution error --- traceback follows:")
  874. traceback(e)
  875. end
  876. )
  877. end
  878. --- file --------------------------------------------------------------------
  879. -- file() is pretty much the simplest clause. It takes a list of filenames,
  880. -- and outputs them.
  881. --
  882. -- * Building does nothing.
  883. -- * Its outputs are its inputs.
  884. --
  885. -- Note: this clause only takes *strings* as its children. If a reference is
  886. -- made to a file that doesn't exist, an error occurs.
  887. file = node {
  888. class = "file",
  889. ensure_at_least_one_child = true,
  890. __init = function(self, p)
  891. node.__init(self, p)
  892. -- If we're a class, don't verify.
  893. if ((type(p) == "table") and p.class) then
  894. return
  895. end
  896. -- Ensure that the file's children are strings.
  897. for i, j in ipairs(self) do
  898. if (type(j) ~= "string") then
  899. self:__error("doesn't know what to do with child ", i,
  900. ", which is a ", type(j))
  901. end
  902. end
  903. end,
  904. -- File's timestamp is special and will bail if it meets a nonexistant file.
  905. __timestamp = function(self, inputs, outputs)
  906. local t = 0
  907. for _, i in ipairs(outputs) do
  908. i = self:__expand(i)
  909. local tt = filetime(i)
  910. if (tt == 0) then
  911. self:__error("is referring to the file '", i, "' which does not exist")
  912. end
  913. if (tt > t) then
  914. t = tt
  915. end
  916. end
  917. return t
  918. end,
  919. -- Outputs are inputs.
  920. __outputs = function(self, inputs)
  921. local o = {}
  922. local n
  923. if self.only_n_children_are_outputs then
  924. n = self.only_n_children_are_outputs
  925. else
  926. n = table_getn(inputs)
  927. end
  928. for i = 1, n do
  929. o[i] = inputs[i]
  930. end
  931. return o
  932. end,
  933. -- Building children does nothing; outputs are inputs.
  934. __buildchildren = function(self)
  935. local outputs = {}
  936. table_append(outputs, self)
  937. return outputs
  938. end,
  939. -- Building does nothing.
  940. __dobuild = function(self, inputs, outputs)
  941. end,
  942. }
  943. --- group -------------------------------------------------------------------
  944. -- group() is also the simplest clause. It does nothing, existing only to
  945. -- group together its children.
  946. group = node {
  947. class = "group",
  948. -- Outputs are inputs.
  949. __outputs = function(self, inputs)
  950. return inputs
  951. end,
  952. -- Building does nothing.
  953. __dobuild = function(self, inputs, outputs)
  954. end,
  955. }
  956. --- deponly -----------------------------------------------------------------
  957. -- deponly() is the one-and-a-halfth most simplest clause. It acts like
  958. -- group {}, but returns no outputs. It's useful for ensuring that building
  959. -- one node causes another node to be built without actually using the
  960. -- second node's outputs.
  961. deponly = node {
  962. class = "deponly",
  963. ensure_at_least_one_child = true,
  964. -- Emits no outputs
  965. __outputs = function(self, inputs)
  966. return {}
  967. end,
  968. -- Building does nothing.
  969. __dobuild = function(self, inputs, outputs)
  970. end,
  971. }
  972. --- ith ---------------------------------------------------------------------
  973. -- ith() is the second simplest clause. It acts like group {}, but returns
  974. -- only some of the specified output. It is suitable for extracting, say,
  975. -- one output from a clause to pass to cfile {}.
  976. ith = node {
  977. class = "ith",
  978. ensure_at_least_one_child = true,
  979. __init = function(self, p)
  980. node.__init(self, p)
  981. -- If we're a class, don't verify.
  982. if ((type(p) == "table") and p.class) then
  983. return
  984. end
  985. -- If we have an i property, ensure we don't have a from or
  986. -- to property.
  987. if self.i then
  988. if self.from or self.to then
  989. self:__error("can't have both an i property and a from or to property")
  990. end
  991. if (type(self.i) ~= "number") then
  992. self:__error("doesn't know what to do with its i property, ",
  993. "which is a ", type(self.i), " where a number was expected")
  994. end
  995. self.from = self.i
  996. self.to = self.i
  997. end
  998. -- Ensure the from and to properties are numbers, if they exist.
  999. if self.from then
  1000. if (type(self.from) ~= "number") then
  1001. self:__error("doesn't know what to do with its from property, ",
  1002. "which is a ", type(self.from), " where a number was expected")
  1003. end
  1004. end
  1005. if self.to then
  1006. if (type(self.to) ~= "number") then
  1007. self:__error("doesn't know what to do with its to property, ",
  1008. "which is a ", type(self.to), " where a number was expected")
  1009. end
  1010. end
  1011. end,
  1012. -- Emits one output, which is one of the inputs.
  1013. __outputs = function(self, inputs)
  1014. local n = table_getn(inputs)
  1015. local from = self.from or 1
  1016. local to = self.to or n
  1017. if (from < 1) or (to > n) then
  1018. self:__error("tried to select range ", from, " to ", to,
  1019. " from only ", n, " inputs")
  1020. end
  1021. local range = {}
  1022. for i = from, to do
  1023. table_append(range, inputs[i])
  1024. end
  1025. return range
  1026. end,
  1027. -- Building does nothing.
  1028. __dobuild = function(self, inputs, outputs)
  1029. end,
  1030. }
  1031. --- foreach -----------------------------------------------------------------
  1032. -- foreach {} is the counterpart to ith {}. It applies a particular rule to
  1033. -- all of its children.
  1034. foreach = node {
  1035. class = "foreach",
  1036. __init = function(self, p)
  1037. node.__init(self, p)
  1038. -- If we're a class, don't verify.
  1039. if ((type(p) == "table") and p.class) then
  1040. return
  1041. end
  1042. -- Ensure we have a rule property which is a table.
  1043. if not self.rule then
  1044. self:__error("must have a rule property")
  1045. end
  1046. if (type(self.rule) ~= "table") then
  1047. self:__error("doesn't know what to do with its rule property, ",
  1048. "which is a ", type(self.rule), " where a table was expected")
  1049. end
  1050. end,
  1051. -- Build all our children via the rule.
  1052. --
  1053. -- This is pretty much a copy of node.__buildchildren().
  1054. __buildchildren = function(self)
  1055. scope = {object=self, next=scope}
  1056. local intermediate = {}
  1057. for _, i in ipairs(self) do
  1058. table_append(intermediate, i:__build())
  1059. end
  1060. local inputs = {}
  1061. for _, i in ipairs(intermediate) do
  1062. local r = self.rule { i }
  1063. table_append(inputs, r:__build())
  1064. end
  1065. self:__buildadditionalchildren()
  1066. scope = scope.next
  1067. return inputs
  1068. end,
  1069. -- Inputs are outputs --- because __buildchildren has already done the
  1070. -- necessary work.
  1071. __outputs = function(self, inputs)
  1072. return inputs
  1073. end,
  1074. -- Building does nothing.
  1075. __dobuild = function(self, inputs, outputs)
  1076. end,
  1077. }
  1078. --- Simple ---------------------------------------------------------------
  1079. -- simple is the most common clause, and implements make-like behaviour:
  1080. -- the named command is executed in order to rebuild the node.
  1081. --
  1082. -- * The timestamp is the newest timestamp of its outputs.
  1083. -- * Building executes the command.
  1084. -- * Its outputs are automatically generated by expanding the templates
  1085. -- in the 'outputs' variable.
  1086. simple = node {
  1087. class = "file",
  1088. construct_string_children_with = file,
  1089. all_children_are_objects = true,
  1090. __init = function(self, p)
  1091. node.__init(self, p)
  1092. -- If we're a class, don't verify.
  1093. if ((type(p) == "table") and p.class) then
  1094. return
  1095. end
  1096. -- outputs must exist, and must be a table.
  1097. if not self.outputs then
  1098. self:__error("must have an outputs template set")
  1099. end
  1100. if (type(self.outputs) ~= "table") then
  1101. self:__error("doesn't know what to do with its outputs, which is a ",
  1102. type(self.outputs), " but should be a table")
  1103. end
  1104. -- There must be a command which must be a string or table.
  1105. if not self.command then
  1106. self:__error("must have a command specified")
  1107. end
  1108. if (type(self.command) == "string") then
  1109. self.command = {self.command}
  1110. end
  1111. if (type(self.command) ~= "table") then
  1112. self:__error("doesn't know what to do with its command, which is a ",
  1113. type(self.command), " but should be a string or a table")
  1114. end
  1115. end,
  1116. -- Outputs are specified manually.
  1117. __outputs = function(self, inputs)
  1118. self["in"] = inputs
  1119. local input
  1120. if inputs then
  1121. input = inputs[1]
  1122. end
  1123. if not input then
  1124. input = ""
  1125. end
  1126. self.I = string_gsub(input, "^.*/", "")
  1127. self.I = string_gsub(self.I, "%..-$", "")
  1128. -- Construct an outputs array for use in the cache key. This mirrors
  1129. -- what the final array will be, but the unique ID is going to be 0.
  1130. self.out = {}
  1131. self.U = 0
  1132. for _, i in ipairs(self.outputs) do
  1133. i = self:__expand(i)
  1134. table_append(self.out, i)
  1135. end
  1136. -- Determine the cache key we're going to use.
  1137. local cachekey = table_concat(self.command, " && ")
  1138. cachekey = self:__expand(cachekey)
  1139. cachekey = create_intermediate_cache_key(cachekey)
  1140. -- Work out the unique ID.
  1141. --
  1142. -- Note: we're running in the sandbox, so we need to fully qualify
  1143. -- pm.intermediate_cache_dir.
  1144. self.U = pm.intermediate_cache_dir..cachekey
  1145. -- Construct the real outputs array.
  1146. self.out = {}
  1147. for _, i in ipairs(self.outputs) do
  1148. i = self:__expand(i)
  1149. mkcontainerdir(i)
  1150. table_append(self.out, i)
  1151. end
  1152. return self.out
  1153. end,
  1154. -- Building causes the command to be expanded and invoked. The 'children'
  1155. -- variable is set to the input files.
  1156. __dobuild = function(self, inputs, outputs)
  1157. self["in"] = inputs
  1158. self.out = outputs
  1159. local r = self:__invoke(self.command, inputs, outputs)
  1160. if r then
  1161. if delete_output_files_on_error then
  1162. self:__invoke({"%RM% %out%"})
  1163. end
  1164. self:__error("failed to build with return code ", r)
  1165. end
  1166. end,
  1167. }
  1168. --- End of prologue ---------------------------------------------------------
  1169. -- Set a few useful global variables.
  1170. RM = "rm -f"
  1171. INSTALL = "ln -f"
  1172. -- Now we're done, switch out of sandbox mode again. This only works
  1173. -- because we made _G local at the top of the file, which makes it
  1174. -- lexically scoped rather than looked up via the environment.
  1175. setfenv(1, _G)
  1176. -- ======================================================================= --
  1177. -- APPLICATION DRIVER --
  1178. -- ======================================================================= --
  1179. -- Parse and process the command line options.
  1180. do
  1181. local function do_help(opt)
  1182. message("Prime Mover version ", VERSION, " © 2006 David Given")
  1183. stdout:write([[
  1184. Syntax: pm [<options...>] [<targets>]
  1185. Options:
  1186. -h --help Displays this message.
  1187. --license List Prime Mover's redistribution license.
  1188. -cX --cachedir X Sets the object file cache to directory X.
  1189. -p --purge Purges the cache before execution.
  1190. WARNING: will remove *everything* in the cache dir!
  1191. -fX --file X Reads in the pmfile X. May be specified multiple times.
  1192. -DX=Y --define X=Y Defines variable X to value Y (or true if Y omitted)
  1193. -n --no-execute Don't actually execute anything
  1194. -v --verbose Be more verbose
  1195. -q --quiet Be more quiet
  1196. If no pmfiles are explicitly specified, 'pmfile' is read.
  1197. If no targets are explicitly specified, 'default' is built.
  1198. Options and targets may be specified in any order.
  1199. ]])
  1200. os.exit(0)
  1201. end
  1202. local function do_license(opt)
  1203. message("Prime Mover version ", VERSION, " © 2006 David Given")
  1204. stdout:write([[
  1205. Prime Mover is licensed under the MIT open source license.
  1206. Copyright © 2006 David Given
  1207. Permission is hereby granted, free of charge, to any person obtaining a copy
  1208. of this software and associated documentation files (the "Software"), to deal
  1209. in the Software without restriction, including without limitation the rights
  1210. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1211. copies of the Software, and to permit persons to whom the Software is
  1212. furnished to do so, subject to the following conditions:
  1213. The above copyright notice and this permission notice shall be included in
  1214. all copies or substantial portions of the Software.
  1215. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1216. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1217. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1218. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1219. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1220. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1221. SOFTWARE.
  1222. ]])
  1223. os.exit(0)
  1224. end
  1225. local function needarg(opt)
  1226. if not opt then
  1227. usererror("missing option parameter")
  1228. end
  1229. end
  1230. local function do_cachedir(opt)
  1231. needarg(opt)
  1232. intermediate_cache_dir = opt
  1233. return 1
  1234. end
  1235. local function do_inputfile(opt)
  1236. needarg(opt)
  1237. table_append(input_files, opt)
  1238. return 1
  1239. end
  1240. local function do_purgecache(opt)
  1241. purge_intermediate_cache = true
  1242. return 0
  1243. end
  1244. local function do_define(opt)
  1245. needarg(opt)
  1246. local s, e, key, value = string_find(opt, "^([^=]*)=(.*)$")
  1247. if not key then
  1248. key = opt
  1249. value = true
  1250. end
  1251. sandbox[key] = value
  1252. return 1
  1253. end
  1254. local function do_no_execute(opt)
  1255. no_execute = true
  1256. return 0
  1257. end
  1258. local function do_verbose(opt)
  1259. verbose = true
  1260. return 0
  1261. end
  1262. local function do_quiet(opt)
  1263. quiet = true
  1264. return 0
  1265. end
  1266. local argmap = {
  1267. ["h"] = do_help,
  1268. ["help"] = do_help,
  1269. ["c"] = do_cachedir,
  1270. ["cachedir"] = do_cachedir,
  1271. ["p"] = do_purgecache,
  1272. ["purge"] = do_purgecache,
  1273. ["f"] = do_inputfile,
  1274. ["file"] = do_inputfile,
  1275. ["D"] = do_define,
  1276. ["define"] = do_define,
  1277. ["n"] = do_no_execute,
  1278. ["no-execute"] = do_no_execute,
  1279. ["v"] = do_verbose,
  1280. ["verbose"] = do_verbose,
  1281. ["q"] = do_quiet,
  1282. ["quiet"] = do_quiet,
  1283. ["license"] = do_license,
  1284. }
  1285. -- Called on an unrecognised option.
  1286. local function unrecognisedarg(arg)
  1287. usererror("unrecognised option '", arg, "' --- try --help for help")
  1288. end
  1289. -- Do the actual argument parsing.
  1290. for i = 1, table_getn(arg) do
  1291. local o = arg[i]
  1292. local op
  1293. if (string_byte(o, 1) == 45) then
  1294. -- This is an option.
  1295. if (string_byte(o, 2) == 45) then
  1296. -- ...with a -- prefix.
  1297. o = string_sub(o, 3)
  1298. local fn = argmap[o]
  1299. if not fn then
  1300. unrecognisedarg("--"..o)
  1301. end
  1302. local op = arg[i+1]
  1303. i = i + fn(op)
  1304. else
  1305. -- ...without a -- prefix.
  1306. local od = string_sub(o, 2, 2)
  1307. local fn = argmap[od]
  1308. if not fn then
  1309. unrecognisedarg("-"..od)
  1310. end
  1311. op = string_sub(o, 3)
  1312. if (op == "") then
  1313. op = arg[i+1]
  1314. i = i + fn(op)
  1315. else
  1316. fn(op)
  1317. end
  1318. end
  1319. else
  1320. -- This is a target name.
  1321. table_append(targets, o)
  1322. end
  1323. end
  1324. -- Option fallbacks.
  1325. if (table_getn(input_files) == 0) then
  1326. input_files = {"pmfile"}
  1327. end
  1328. if (table_getn(targets) == 0) then
  1329. targets = {"default"}
  1330. end
  1331. end
  1332. -- Load any input files.
  1333. for _, i in ipairs(input_files) do
  1334. sandbox.include(i, unpack(arg))
  1335. end
  1336. -- Set up the intermediate cache.
  1337. if purge_intermediate_cache then
  1338. create_intermediate_cache()
  1339. else
  1340. load_intermediate_cache()
  1341. end
  1342. -- Build any targets.
  1343. for _, i in ipairs(targets) do
  1344. local o = sandbox[i]
  1345. if not o then
  1346. usererror("don't know how to build '", i, "'")
  1347. end
  1348. if ((type(o) ~= "table") and not o.class) then
  1349. usererror("'", i, "' doesn't seem to be a valid target")
  1350. end
  1351. xpcall(
  1352. function()
  1353. o:__build()
  1354. end,
  1355. function(e)
  1356. message("rule engine execution error --- traceback follows:")
  1357. traceback(e)
  1358. end
  1359. )
  1360. end
  1361. XXXXSTARTinterpreter
  1362. 253583
  1363. #include <signal.h>
  1364. #include <sys/wait.h>
  1365. #include <stdarg.h>
  1366. #include <errno.h>
  1367. #include <stdio.h>
  1368. #include <locale.h>
  1369. #include <sys/types.h>
  1370. #include <sys/utsname.h>
  1371. #include <stddef.h>
  1372. #include <grp.h>
  1373. #include <time.h>
  1374. #include <assert.h>
  1375. #include <sys/stat.h>
  1376. #include <ctype.h>
  1377. #include <utime.h>
  1378. #include <setjmp.h>
  1379. #include <dirent.h>
  1380. #include <sys/times.h>
  1381. #include <pwd.h>
  1382. #include <unistd.h>
  1383. #include <string.h>
  1384. #include <limits.h>
  1385. #include <fcntl.h>
  1386. #include <stdlib.h>
  1387. #line 1 "lapi.c"
  1388. #define lapi_c
  1389. #line 1 "lua.h"
  1390. #ifndef lua_h
  1391. #define lua_h
  1392. #define LUA_NUMBER int
  1393. #define LUA_NUMBER_SCAN "%d"
  1394. #define LUA_NUMBER_FMT "%d"
  1395. #define LUA_VERSION "Lua 5.0.2 (patched for Prime Mover)"
  1396. #define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio"
  1397. #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"
  1398. #define LUA_MULTRET (-1)
  1399. #define LUA_REGISTRYINDEX (-10000)
  1400. #define LUA_GLOBALSINDEX (-10001)
  1401. #define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
  1402. #define LUA_ERRRUN 1
  1403. #define LUA_ERRFILE 2
  1404. #define LUA_ERRSYNTAX 3
  1405. #define LUA_ERRMEM 4
  1406. #define LUA_ERRERR 5
  1407. typedef struct lua_State lua_State;typedef int(*lua_CFunction)(lua_State*L);
  1408. typedef const char*(*lua_Chunkreader)(lua_State*L,void*ud,size_t*sz);typedef
  1409. int(*lua_Chunkwriter)(lua_State*L,const void*p,size_t sz,void*ud);
  1410. #define LUA_TNONE (-1)
  1411. #define LUA_TNIL 0
  1412. #define LUA_TBOOLEAN 1
  1413. #define LUA_TLIGHTUSERDATA 2
  1414. #define LUA_TNUMBER 3
  1415. #define LUA_TSTRING 4
  1416. #define LUA_TTABLE 5
  1417. #define LUA_TFUNCTION 6
  1418. #define LUA_TUSERDATA 7
  1419. #define LUA_TTHREAD 8
  1420. #define LUA_MINSTACK 20
  1421. #ifdef LUA_USER_H
  1422. #include LUA_USER_H
  1423. #endif
  1424. #ifndef LUA_NUMBER
  1425. typedef double lua_Number;
  1426. #else
  1427. typedef LUA_NUMBER lua_Number;
  1428. #endif
  1429. #ifndef LUA_API
  1430. #define LUA_API extern
  1431. #endif
  1432. LUA_API lua_State*lua_open(void);LUA_API void lua_close(lua_State*L);LUA_API
  1433. lua_State*lua_newthread(lua_State*L);LUA_API lua_CFunction lua_atpanic(
  1434. lua_State*L,lua_CFunction panicf);LUA_API int lua_gettop(lua_State*L);LUA_API
  1435. void lua_settop(lua_State*L,int idx);LUA_API void lua_pushvalue(lua_State*L,
  1436. int idx);LUA_API void lua_remove(lua_State*L,int idx);LUA_API void lua_insert(
  1437. lua_State*L,int idx);LUA_API void lua_replace(lua_State*L,int idx);LUA_API int
  1438. lua_checkstack(lua_State*L,int sz);LUA_API void lua_xmove(lua_State*from,
  1439. lua_State*to,int n);LUA_API int lua_isnumber(lua_State*L,int idx);LUA_API int
  1440. lua_isstring(lua_State*L,int idx);LUA_API int lua_iscfunction(lua_State*L,int
  1441. idx);LUA_API int lua_isuserdata(lua_State*L,int idx);LUA_API int lua_type(
  1442. lua_State*L,int idx);LUA_API const char*lua_typename(lua_State*L,int tp);
  1443. LUA_API int lua_equal(lua_State*L,int idx1,int idx2);LUA_API int lua_rawequal(
  1444. lua_State*L,int idx1,int idx2);LUA_API int lua_lessthan(lua_State*L,int idx1,
  1445. int idx2);LUA_API lua_Number lua_tonumber(lua_State*L,int idx);LUA_API int
  1446. lua_toboolean(lua_State*L,int idx);LUA_API const char*lua_tostring(lua_State*L
  1447. ,int idx);LUA_API size_t lua_strlen(lua_State*L,int idx);LUA_API lua_CFunction
  1448. lua_tocfunction(lua_State*L,int idx);LUA_API void*lua_touserdata(lua_State*L,
  1449. int idx);LUA_API lua_State*lua_tothread(lua_State*L,int idx);LUA_API const
  1450. void*lua_topointer(lua_State*L,int idx);LUA_API void lua_pushnil(lua_State*L);
  1451. LUA_API void lua_pushnumber(lua_State*L,lua_Number n);LUA_API void
  1452. lua_pushlstring(lua_State*L,const char*s,size_t l);LUA_API void lua_pushstring
  1453. (lua_State*L,const char*s);LUA_API const char*lua_pushvfstring(lua_State*L,
  1454. const char*fmt,va_list argp);LUA_API const char*lua_pushfstring(lua_State*L,
  1455. const char*fmt,...);LUA_API void lua_pushcclosure(lua_State*L,lua_CFunction fn
  1456. ,int n);LUA_API void lua_pushboolean(lua_State*L,int b);LUA_API void
  1457. lua_pushlightuserdata(lua_State*L,void*p);LUA_API void lua_gettable(lua_State*
  1458. L,int idx);LUA_API void lua_rawget(lua_State*L,int idx);LUA_API void
  1459. lua_rawgeti(lua_State*L,int idx,int n);LUA_API void lua_newtable(lua_State*L);
  1460. LUA_API void*lua_newuserdata(lua_State*L,size_t sz);LUA_API int
  1461. lua_getmetatable(lua_State*L,int objindex);LUA_API void lua_getfenv(lua_State*
  1462. L,int idx);LUA_API void lua_settable(lua_State*L,int idx);LUA_API void
  1463. lua_rawset(lua_State*L,int idx);LUA_API void lua_rawseti(lua_State*L,int idx,
  1464. int n);LUA_API int lua_setmetatable(lua_State*L,int objindex);LUA_API int
  1465. lua_setfenv(lua_State*L,int idx);LUA_API void lua_call(lua_State*L,int nargs,
  1466. int nresults);LUA_API int lua_pcall(lua_State*L,int nargs,int nresults,int
  1467. errfunc);LUA_API int lua_cpcall(lua_State*L,lua_CFunction func,void*ud);
  1468. LUA_API int lua_load(lua_State*L,lua_Chunkreader reader,void*dt,const char*
  1469. chunkname);LUA_API int lua_dump(lua_State*L,lua_Chunkwriter writer,void*data);
  1470. LUA_API int lua_yield(lua_State*L,int nresults);LUA_API int lua_resume(
  1471. lua_State*L,int narg);LUA_API int lua_getgcthreshold(lua_State*L);LUA_API int
  1472. lua_getgccount(lua_State*L);LUA_API void lua_setgcthreshold(lua_State*L,int
  1473. newthreshold);LUA_API const char*lua_version(void);LUA_API int lua_error(
  1474. lua_State*L);LUA_API int lua_next(lua_State*L,int idx);LUA_API void lua_concat
  1475. (lua_State*L,int n);
  1476. #define lua_boxpointer(L,u) (*(void**)(lua_newuserdata(L,sizeof(void*)))=(u))
  1477. #define lua_unboxpointer(L,i) (*(void**)(lua_touserdata(L,i)))
  1478. #define lua_pop(L,n) lua_settop(L,-(n)-1)
  1479. #define lua_register(L,n,f) (lua_pushstring(L,n),lua_pushcfunction(L,f),\
  1480. lua_settable(L,LUA_GLOBALSINDEX))
  1481. #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0)
  1482. #define lua_isfunction(L,n) (lua_type(L,n)==LUA_TFUNCTION)
  1483. #define lua_istable(L,n) (lua_type(L,n)==LUA_TTABLE)
  1484. #define lua_islightuserdata(L,n) (lua_type(L,n)==LUA_TLIGHTUSERDATA)
  1485. #define lua_isnil(L,n) (lua_type(L,n)==LUA_TNIL)
  1486. #define lua_isboolean(L,n) (lua_type(L,n)==LUA_TBOOLEAN)
  1487. #define lua_isnone(L,n) (lua_type(L,n)==LUA_TNONE)
  1488. #define lua_isnoneornil(L, n)(lua_type(L,n)<=0)
  1489. #define lua_pushliteral(L, s)lua_pushlstring(L,""s,(sizeof(s)/sizeof(char))-1)
  1490. LUA_API int lua_pushupvalues(lua_State*L);
  1491. #define lua_getregistry(L) lua_pushvalue(L,LUA_REGISTRYINDEX)
  1492. #define lua_setglobal(L,s) (lua_pushstring(L,s),lua_insert(L,-2),lua_settable(\
  1493. L,LUA_GLOBALSINDEX))
  1494. #define lua_getglobal(L,s) (lua_pushstring(L,s),lua_gettable(L,\
  1495. LUA_GLOBALSINDEX))
  1496. #define LUA_NOREF (-2)
  1497. #define LUA_REFNIL (-1)
  1498. #define lua_ref(L,lock) ((lock)?luaL_ref(L,LUA_REGISTRYINDEX):(lua_pushstring(\
  1499. L,"unlocked references are obsolete"),lua_error(L),0))
  1500. #define lua_unref(L,ref) luaL_unref(L,LUA_REGISTRYINDEX,(ref))
  1501. #define lua_getref(L,ref) lua_rawgeti(L,LUA_REGISTRYINDEX,ref)
  1502. #ifndef LUA_NUMBER_SCAN
  1503. #define LUA_NUMBER_SCAN "%lf"
  1504. #endif
  1505. #ifndef LUA_NUMBER_FMT
  1506. #define LUA_NUMBER_FMT "%.14g"
  1507. #endif
  1508. #define LUA_HOOKCALL 0
  1509. #define LUA_HOOKRET 1
  1510. #define LUA_HOOKLINE 2
  1511. #define LUA_HOOKCOUNT 3
  1512. #define LUA_HOOKTAILRET 4
  1513. #define LUA_MASKCALL (1<<LUA_HOOKCALL)
  1514. #define LUA_MASKRET (1<<LUA_HOOKRET)
  1515. #define LUA_MASKLINE (1<<LUA_HOOKLINE)
  1516. #define LUA_MASKCOUNT (1<<LUA_HOOKCOUNT)
  1517. typedef struct lua_Debug lua_Debug;typedef void(*lua_Hook)(lua_State*L,
  1518. lua_Debug*ar);LUA_API int lua_getstack(lua_State*L,int level,lua_Debug*ar);
  1519. LUA_API int lua_getinfo(lua_State*L,const char*what,lua_Debug*ar);LUA_API
  1520. const char*lua_getlocal(lua_State*L,const lua_Debug*ar,int n);LUA_API const
  1521. char*lua_setlocal(lua_State*L,const lua_Debug*ar,int n);LUA_API const char*
  1522. lua_getupvalue(lua_State*L,int funcindex,int n);LUA_API const char*
  1523. lua_setupvalue(lua_State*L,int funcindex,int n);LUA_API int lua_sethook(
  1524. lua_State*L,lua_Hook func,int mask,int count);LUA_API lua_Hook lua_gethook(
  1525. lua_State*L);LUA_API int lua_gethookmask(lua_State*L);LUA_API int
  1526. lua_gethookcount(lua_State*L);
  1527. #define LUA_IDSIZE 60
  1528. struct lua_Debug{int event;const char*name;const char*namewhat;const char*what
  1529. ;const char*source;int currentline;int nups;int linedefined;char short_src[
  1530. LUA_IDSIZE];int i_ci;};
  1531. #endif
  1532. #line 14 "lapi.c"
  1533. #line 1 "lapi.h"
  1534. #ifndef lapi_h
  1535. #define lapi_h
  1536. #line 1 "lobject.h"
  1537. #ifndef lobject_h
  1538. #define lobject_h
  1539. #line 1 "llimits.h"
  1540. #ifndef llimits_h
  1541. #define llimits_h
  1542. #ifndef BITS_INT
  1543. #if INT_MAX-20 <32760
  1544. #define BITS_INT 16
  1545. #else
  1546. #if INT_MAX >2147483640L
  1547. #define BITS_INT 32
  1548. #else
  1549. #error "you must define BITS_INT with number of bits in an integer"
  1550. #endif
  1551. #endif
  1552. #endif
  1553. typedef unsigned int lu_hash;typedef int ls_hash;typedef unsigned long lu_mem;
  1554. #define MAX_LUMEM ULONG_MAX
  1555. typedef long ls_nstr;typedef unsigned char lu_byte;
  1556. #define MAX_SIZET ((size_t)(~(size_t)0)-2)
  1557. #define MAX_INT (INT_MAX-2)
  1558. #define IntPoint(p) ((lu_hash)(p))
  1559. #ifndef LUSER_ALIGNMENT_T
  1560. typedef union{double u;void*s;long l;}L_Umaxalign;
  1561. #else
  1562. typedef LUSER_ALIGNMENT_T L_Umaxalign;
  1563. #endif
  1564. #ifndef LUA_UACNUMBER
  1565. typedef double l_uacNumber;
  1566. #else
  1567. typedef LUA_UACNUMBER l_uacNumber;
  1568. #endif
  1569. #ifndef lua_assert
  1570. #define lua_assert(c)
  1571. #endif
  1572. #ifndef check_exp
  1573. #define check_exp(c,e) (e)
  1574. #endif
  1575. #ifndef UNUSED
  1576. #define UNUSED(x) ((void)(x))
  1577. #endif
  1578. #ifndef cast
  1579. #define cast(t, exp)((t)(exp))
  1580. #endif
  1581. typedef unsigned long Instruction;
  1582. #ifndef LUA_MAXCALLS
  1583. #define LUA_MAXCALLS 4096
  1584. #endif
  1585. #ifndef LUA_MAXCCALLS
  1586. #define LUA_MAXCCALLS 200
  1587. #endif
  1588. #ifndef LUA_MAXCSTACK
  1589. #define LUA_MAXCSTACK 2048
  1590. #endif
  1591. #define MAXSTACK 250
  1592. #ifndef MAXVARS
  1593. #define MAXVARS 200
  1594. #endif
  1595. #ifndef MAXUPVALUES
  1596. #define MAXUPVALUES 32
  1597. #endif
  1598. #ifndef MAXPARAMS
  1599. #define MAXPARAMS 100
  1600. #endif
  1601. #ifndef MINSTRTABSIZE
  1602. #define MINSTRTABSIZE 32
  1603. #endif
  1604. #ifndef LUA_MINBUFFER
  1605. #define LUA_MINBUFFER 32
  1606. #endif
  1607. #ifndef LUA_MAXPARSERLEVEL
  1608. #define LUA_MAXPARSERLEVEL 200
  1609. #endif
  1610. #endif
  1611. #line 12 "lobject.h"
  1612. #define NUM_TAGS LUA_TTHREAD
  1613. #define LUA_TPROTO (NUM_TAGS+1)
  1614. #define LUA_TUPVAL (NUM_TAGS+2)
  1615. typedef union GCObject GCObject;
  1616. #define CommonHeader GCObject*next;lu_byte tt;lu_byte marked
  1617. typedef struct GCheader{CommonHeader;}GCheader;typedef union{GCObject*gc;void*
  1618. p;lua_Number n;int b;}Value;typedef struct lua_TObject{int tt;Value value;}
  1619. TObject;
  1620. #define ttisnil(o) (ttype(o)==LUA_TNIL)
  1621. #define ttisnumber(o) (ttype(o)==LUA_TNUMBER)
  1622. #define ttisstring(o) (ttype(o)==LUA_TSTRING)
  1623. #define ttistable(o) (ttype(o)==LUA_TTABLE)
  1624. #define ttisfunction(o) (ttype(o)==LUA_TFUNCTION)
  1625. #define ttisboolean(o) (ttype(o)==LUA_TBOOLEAN)
  1626. #define ttisuserdata(o) (ttype(o)==LUA_TUSERDATA)
  1627. #define ttisthread(o) (ttype(o)==LUA_TTHREAD)
  1628. #define ttislightuserdata(o) (ttype(o)==LUA_TLIGHTUSERDATA)
  1629. #define ttype(o) ((o)->tt)
  1630. #define gcvalue(o) check_exp(iscollectable(o),(o)->value.gc)
  1631. #define pvalue(o) check_exp(ttislightuserdata(o),(o)->value.p)
  1632. #define nvalue(o) check_exp(ttisnumber(o),(o)->value.n)
  1633. #define tsvalue(o) check_exp(ttisstring(o),&(o)->value.gc->ts)
  1634. #define uvalue(o) check_exp(ttisuserdata(o),&(o)->value.gc->u)
  1635. #define clvalue(o) check_exp(ttisfunction(o),&(o)->value.gc->cl)
  1636. #define hvalue(o) check_exp(ttistable(o),&(o)->value.gc->h)
  1637. #define bvalue(o) check_exp(ttisboolean(o),(o)->value.b)
  1638. #define thvalue(o) check_exp(ttisthread(o),&(o)->value.gc->th)
  1639. #define l_isfalse(o) (ttisnil(o)||(ttisboolean(o)&&bvalue(o)==0))
  1640. #define setnvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TNUMBER;i_o->value.n=(\
  1641. x);}
  1642. #define chgnvalue(obj,x) check_exp(ttype(obj)==LUA_TNUMBER,(obj)->value.n=(x))
  1643. #define setpvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TLIGHTUSERDATA;i_o->\
  1644. value.p=(x);}
  1645. #define setbvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TBOOLEAN;i_o->value.b=\
  1646. (x);}
  1647. #define setsvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TSTRING;i_o->value.gc=\
  1648. cast(GCObject*,(x));lua_assert(i_o->value.gc->gch.tt==LUA_TSTRING);}
  1649. #define setuvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TUSERDATA;i_o->value.\
  1650. gc=cast(GCObject*,(x));lua_assert(i_o->value.gc->gch.tt==LUA_TUSERDATA);}
  1651. #define setthvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TTHREAD;i_o->value.gc\
  1652. =cast(GCObject*,(x));lua_assert(i_o->value.gc->gch.tt==LUA_TTHREAD);}
  1653. #define setclvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TFUNCTION;i_o->value.\
  1654. gc=cast(GCObject*,(x));lua_assert(i_o->value.gc->gch.tt==LUA_TFUNCTION);}
  1655. #define sethvalue(obj,x) {TObject*i_o=(obj);i_o->tt=LUA_TTABLE;i_o->value.gc=\
  1656. cast(GCObject*,(x));lua_assert(i_o->value.gc->gch.tt==LUA_TTABLE);}
  1657. #define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
  1658. #define checkconsistency(obj) lua_assert(!iscollectable(obj)||(ttype(obj)==(\
  1659. obj)->value.gc->gch.tt))
  1660. #define setobj(obj1,obj2) {const TObject*o2=(obj2);TObject*o1=(obj1);\
  1661. checkconsistency(o2);o1->tt=o2->tt;o1->value=o2->value;}
  1662. #define setobjs2s setobj
  1663. #define setobj2s setobj
  1664. #define setsvalue2s setsvalue
  1665. #define setobjt2t setobj
  1666. #define setobj2t setobj
  1667. #define setobj2n setobj
  1668. #define setsvalue2n setsvalue
  1669. #define setttype(obj, tt)(ttype(obj)=(tt))
  1670. #define iscollectable(o) (ttype(o)>=LUA_TSTRING)
  1671. typedef TObject*StkId;typedef union TString{L_Umaxalign dummy;struct{
  1672. CommonHeader;lu_byte reserved;lu_hash hash;size_t len;}tsv;}TString;
  1673. #define getstr(ts) cast(const char*,(ts)+1)
  1674. #define svalue(o) getstr(tsvalue(o))
  1675. typedef union Udata{L_Umaxalign dummy;struct{CommonHeader;struct Table*
  1676. metatable;size_t len;}uv;}Udata;typedef struct Proto{CommonHeader;TObject*k;
  1677. Instruction*code;struct Proto**p;int*lineinfo;struct LocVar*locvars;TString**
  1678. upvalues;TString*source;int sizeupvalues;int sizek;int sizecode;int
  1679. sizelineinfo;int sizep;int sizelocvars;int lineDefined;GCObject*gclist;lu_byte
  1680. nups;lu_byte numparams;lu_byte is_vararg;lu_byte maxstacksize;}Proto;typedef
  1681. struct LocVar{TString*varname;int startpc;int endpc;}LocVar;typedef struct
  1682. UpVal{CommonHeader;TObject*v;TObject value;}UpVal;
  1683. #define ClosureHeader CommonHeader;lu_byte isC;lu_byte nupvalues;GCObject*\
  1684. gclist
  1685. typedef struct CClosure{ClosureHeader;lua_CFunction f;TObject upvalue[1];}
  1686. CClosure;typedef struct LClosure{ClosureHeader;struct Proto*p;TObject g;UpVal*
  1687. upvals[1];}LClosure;typedef union Closure{CClosure c;LClosure l;}Closure;
  1688. #define iscfunction(o) (ttype(o)==LUA_TFUNCTION&&clvalue(o)->c.isC)
  1689. #define isLfunction(o) (ttype(o)==LUA_TFUNCTION&&!clvalue(o)->c.isC)
  1690. typedef struct Node{TObject i_key;TObject i_val;struct Node*next;}Node;typedef
  1691. struct Table{CommonHeader;lu_byte flags;lu_byte lsizenode;struct Table*
  1692. metatable;TObject*array;Node*node;Node*firstfree;GCObject*gclist;int sizearray
  1693. ;}Table;
  1694. #define lmod(s,size) check_exp((size&(size-1))==0,(cast(int,(s)&((size)-1))))
  1695. #define twoto(x) (1<<(x))
  1696. #define sizenode(t) (twoto((t)->lsizenode))
  1697. extern const TObject luaO_nilobject;int luaO_log2(unsigned int x);int
  1698. luaO_int2fb(unsigned int x);
  1699. #define fb2int(x) (((x)&7)<<((x)>>3))
  1700. int luaO_rawequalObj(const TObject*t1,const TObject*t2);int luaO_str2d(const
  1701. char*s,lua_Number*result);const char*luaO_pushvfstring(lua_State*L,const char*
  1702. fmt,va_list argp);const char*luaO_pushfstring(lua_State*L,const char*fmt,...);
  1703. void luaO_chunkid(char*out,const char*source,int len);
  1704. #endif
  1705. #line 12 "lapi.h"
  1706. void luaA_pushobject(lua_State*L,const TObject*o);
  1707. #endif
  1708. #line 16 "lapi.c"
  1709. #line 1 "ldebug.h"
  1710. #ifndef ldebug_h
  1711. #define ldebug_h
  1712. #line 1 "lstate.h"
  1713. #ifndef lstate_h
  1714. #define lstate_h
  1715. #line 1 "ltm.h"
  1716. #ifndef ltm_h
  1717. #define ltm_h
  1718. typedef enum{TM_INDEX,TM_NEWINDEX,TM_GC,TM_MODE,TM_EQ,TM_ADD,TM_SUB,TM_MUL,
  1719. TM_DIV,TM_POW,TM_UNM,TM_LT,TM_LE,TM_CONCAT,TM_CALL,TM_N}TMS;
  1720. #define gfasttm(g,et,e) (((et)->flags&(1u<<(e)))?NULL:luaT_gettm(et,e,(g)->\
  1721. tmname[e]))
  1722. #define fasttm(l,et,e) gfasttm(G(l),et,e)
  1723. const TObject*luaT_gettm(Table*events,TMS event,TString*ename);const TObject*
  1724. luaT_gettmbyobj(lua_State*L,const TObject*o,TMS event);void luaT_init(
  1725. lua_State*L);extern const char*const luaT_typenames[];
  1726. #endif
  1727. #line 14 "lstate.h"
  1728. #line 1 "lzio.h"
  1729. #ifndef lzio_h
  1730. #define lzio_h
  1731. #define EOZ (-1)
  1732. typedef struct Zio ZIO;
  1733. #define char2int(c) cast(int,cast(unsigned char,(c)))
  1734. #define zgetc(z) (((z)->n--)>0?char2int(*(z)->p++):luaZ_fill(z))
  1735. #define zname(z) ((z)->name)
  1736. void luaZ_init(ZIO*z,lua_Chunkreader reader,void*data,const char*name);size_t
  1737. luaZ_read(ZIO*z,void*b,size_t n);int luaZ_lookahead(ZIO*z);typedef struct
  1738. Mbuffer{char*buffer;size_t buffsize;}Mbuffer;char*luaZ_openspace(lua_State*L,
  1739. Mbuffer*buff,size_t n);
  1740. #define luaZ_initbuffer(L, buff)((buff)->buffer=NULL,(buff)->buffsize=0)
  1741. #define luaZ_sizebuffer(buff) ((buff)->buffsize)
  1742. #define luaZ_buffer(buff) ((buff)->buffer)
  1743. #define luaZ_resizebuffer(L, buff,size)(luaM_reallocvector(L,(buff)->buffer,(\
  1744. buff)->buffsize,size,char),(buff)->buffsize=size)
  1745. #define luaZ_freebuffer(L, buff)luaZ_resizebuffer(L,buff,0)
  1746. struct Zio{size_t n;const char*p;lua_Chunkreader reader;void*data;const char*
  1747. name;};int luaZ_fill(ZIO*z);
  1748. #endif
  1749. #line 15 "lstate.h"
  1750. #ifndef lua_lock
  1751. #define lua_lock(L) ((void)0)
  1752. #endif
  1753. #ifndef lua_unlock
  1754. #define lua_unlock(L) ((void)0)
  1755. #endif
  1756. #ifndef lua_userstateopen
  1757. #define lua_userstateopen(l)
  1758. #endif
  1759. struct lua_longjmp;
  1760. #define defaultmeta(L) (&G(L)->_defaultmeta)
  1761. #define gt(L) (&L->_gt)
  1762. #define registry(L) (&G(L)->_registry)
  1763. #define EXTRA_STACK 5
  1764. #define BASIC_CI_SIZE 8
  1765. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  1766. typedef struct stringtable{GCObject**hash;ls_nstr nuse;int size;}stringtable;
  1767. typedef struct CallInfo{StkId base;StkId top;int state;union{struct{const
  1768. Instruction*savedpc;const Instruction**pc;int tailcalls;}l;struct{int dummy;}c
  1769. ;}u;}CallInfo;
  1770. #define CI_C (1<<0)
  1771. #define CI_HASFRAME (1<<1)
  1772. #define CI_CALLING (1<<2)
  1773. #define CI_SAVEDPC (1<<3)
  1774. #define CI_YIELD (1<<4)
  1775. #define ci_func(ci) (clvalue((ci)->base-1))
  1776. typedef struct global_State{stringtable strt;GCObject*rootgc;GCObject*
  1777. rootudata;GCObject*tmudata;Mbuffer buff;lu_mem GCthreshold;lu_mem nblocks;
  1778. lua_CFunction panic;TObject _registry;TObject _defaultmeta;struct lua_State*
  1779. mainthread;Node dummynode[1];TString*tmname[TM_N];}global_State;struct
  1780. lua_State{CommonHeader;StkId top;StkId base;global_State*l_G;CallInfo*ci;StkId
  1781. stack_last;StkId stack;int stacksize;CallInfo*end_ci;CallInfo*base_ci;
  1782. unsigned short size_ci;unsigned short nCcalls;lu_byte hookmask;lu_byte
  1783. allowhook;lu_byte hookinit;int basehookcount;int hookcount;lua_Hook hook;
  1784. TObject _gt;GCObject*openupval;GCObject*gclist;struct lua_longjmp*errorJmp;
  1785. ptrdiff_t errfunc;};
  1786. #define G(L) (L->l_G)
  1787. union GCObject{GCheader gch;union TString ts;union Udata u;union Closure cl;
  1788. struct Table h;struct Proto p;struct UpVal uv;struct lua_State th;};
  1789. #define gcotots(o) check_exp((o)->gch.tt==LUA_TSTRING,&((o)->ts))
  1790. #define gcotou(o) check_exp((o)->gch.tt==LUA_TUSERDATA,&((o)->u))
  1791. #define gcotocl(o) check_exp((o)->gch.tt==LUA_TFUNCTION,&((o)->cl))
  1792. #define gcotoh(o) check_exp((o)->gch.tt==LUA_TTABLE,&((o)->h))
  1793. #define gcotop(o) check_exp((o)->gch.tt==LUA_TPROTO,&((o)->p))
  1794. #define gcotouv(o) check_exp((o)->gch.tt==LUA_TUPVAL,&((o)->uv))
  1795. #define ngcotouv(o) check_exp((o)==NULL||(o)->gch.tt==LUA_TUPVAL,&((o)->uv))
  1796. #define gcototh(o) check_exp((o)->gch.tt==LUA_TTHREAD,&((o)->th))
  1797. #define valtogco(v) (cast(GCObject*,(v)))
  1798. lua_State*luaE_newthread(lua_State*L);void luaE_freethread(lua_State*L,
  1799. lua_State*L1);
  1800. #endif
  1801. #line 12 "ldebug.h"
  1802. #define pcRel(pc, p)(cast(int,(pc)-(p)->code)-1)
  1803. #define getline(f,pc) (((f)->lineinfo)?(f)->lineinfo[pc]:0)
  1804. #define resethookcount(L) (L->hookcount=L->basehookcount)
  1805. void luaG_inithooks(lua_State*L);void luaG_typeerror(lua_State*L,const TObject
  1806. *o,const char*opname);void luaG_concaterror(lua_State*L,StkId p1,StkId p2);
  1807. void luaG_aritherror(lua_State*L,const TObject*p1,const TObject*p2);int
  1808. luaG_ordererror(lua_State*L,const TObject*p1,const TObject*p2);void
  1809. luaG_runerror(lua_State*L,const char*fmt,...);void luaG_errormsg(lua_State*L);
  1810. int luaG_checkcode(const Proto*pt);
  1811. #endif
  1812. #line 17 "lapi.c"
  1813. #line 1 "ldo.h"
  1814. #ifndef ldo_h
  1815. #define ldo_h
  1816. #ifndef HARDSTACKTESTS
  1817. #define condhardstacktests(x) {}
  1818. #else
  1819. #define condhardstacktests(x) x
  1820. #endif
  1821. #define luaD_checkstack(L,n) if((char*)L->stack_last-(char*)L->top<=(n)*(int)\
  1822. sizeof(TObject))luaD_growstack(L,n);else condhardstacktests(luaD_reallocstack(\
  1823. L,L->stacksize));
  1824. #define incr_top(L) {luaD_checkstack(L,1);L->top++;}
  1825. #define savestack(L,p) ((char*)(p)-(char*)L->stack)
  1826. #define restorestack(L,n) ((TObject*)((char*)L->stack+(n)))
  1827. #define saveci(L,p) ((char*)(p)-(char*)L->base_ci)
  1828. #define restoreci(L,n) ((CallInfo*)((char*)L->base_ci+(n)))
  1829. typedef void(*Pfunc)(lua_State*L,void*ud);void luaD_resetprotection(lua_State*
  1830. L);int luaD_protectedparser(lua_State*L,ZIO*z,int bin);void luaD_callhook(
  1831. lua_State*L,int event,int line);StkId luaD_precall(lua_State*L,StkId func);
  1832. void luaD_call(lua_State*L,StkId func,int nResults);int luaD_pcall(lua_State*L
  1833. ,Pfunc func,void*u,ptrdiff_t oldtop,ptrdiff_t ef);void luaD_poscall(lua_State*
  1834. L,int wanted,StkId firstResult);void luaD_reallocCI(lua_State*L,int newsize);
  1835. void luaD_reallocstack(lua_State*L,int newsize);void luaD_growstack(lua_State*
  1836. L,int n);void luaD_throw(lua_State*L,int errcode);int luaD_rawrunprotected(
  1837. lua_State*L,Pfunc f,void*ud);
  1838. #endif
  1839. #line 18 "lapi.c"
  1840. #line 1 "lfunc.h"
  1841. #ifndef lfunc_h
  1842. #define lfunc_h
  1843. Proto*luaF_newproto(lua_State*L);Closure*luaF_newCclosure(lua_State*L,int
  1844. nelems);Closure*luaF_newLclosure(lua_State*L,int nelems,TObject*e);UpVal*
  1845. luaF_findupval(lua_State*L,StkId level);void luaF_close(lua_State*L,StkId
  1846. level);void luaF_freeproto(lua_State*L,Proto*f);void luaF_freeclosure(
  1847. lua_State*L,Closure*c);const char*luaF_getlocalname(const Proto*func,int
  1848. local_number,int pc);
  1849. #endif
  1850. #line 19 "lapi.c"
  1851. #line 1 "lgc.h"
  1852. #ifndef lgc_h
  1853. #define lgc_h
  1854. #define luaC_checkGC(L) {lua_assert(!(L->ci->state&CI_CALLING));if(G(L)->\
  1855. nblocks>=G(L)->GCthreshold)luaC_collectgarbage(L);}
  1856. size_t luaC_separateudata(lua_State*L);void luaC_callGCTM(lua_State*L);void
  1857. luaC_sweep(lua_State*L,int all);void luaC_collectgarbage(lua_State*L);void
  1858. luaC_link(lua_State*L,GCObject*o,lu_byte tt);
  1859. #endif
  1860. #line 20 "lapi.c"
  1861. #line 1 "lmem.h"
  1862. #ifndef lmem_h
  1863. #define lmem_h
  1864. #define MEMERRMSG "not enough memory"
  1865. void*luaM_realloc(lua_State*L,void*oldblock,lu_mem oldsize,lu_mem size);void*
  1866. luaM_growaux(lua_State*L,void*block,int*size,int size_elem,int limit,const
  1867. char*errormsg);
  1868. #define luaM_free(L, b,s)luaM_realloc(L,(b),(s),0)
  1869. #define luaM_freelem(L, b)luaM_realloc(L,(b),sizeof(*(b)),0)
  1870. #define luaM_freearray(L, b,n,t)luaM_realloc(L,(b),cast(lu_mem,n)*cast(lu_mem,\
  1871. sizeof(t)),0)
  1872. #define luaM_malloc(L, t)luaM_realloc(L,NULL,0,(t))
  1873. #define luaM_new(L, t)cast(t*,luaM_malloc(L,sizeof(t)))
  1874. #define luaM_newvector(L, n,t)cast(t*,luaM_malloc(L,cast(lu_mem,n)*cast(lu_mem\
  1875. ,sizeof(t))))
  1876. #define luaM_growvector(L,v,nelems,size,t,limit,e) if(((nelems)+1)>(size))((v)\
  1877. =cast(t*,luaM_growaux(L,v,&(size),sizeof(t),limit,e)))
  1878. #define luaM_reallocvector(L, v,oldn,n,t)((v)=cast(t*,luaM_realloc(L,v,cast(\
  1879. lu_mem,oldn)*cast(lu_mem,sizeof(t)),cast(lu_mem,n)*cast(lu_mem,sizeof(t)))))
  1880. #endif
  1881. #line 21 "lapi.c"
  1882. #line 1 "lstring.h"
  1883. #ifndef lstring_h
  1884. #define lstring_h
  1885. #define sizestring(l) (cast(lu_mem,sizeof(union TString))+(cast(lu_mem,l)+1)*\
  1886. sizeof(char))
  1887. #define sizeudata(l) (cast(lu_mem,sizeof(union Udata))+(l))
  1888. #define luaS_new(L, s)(luaS_newlstr(L,s,strlen(s)))
  1889. #define luaS_newliteral(L, s)(luaS_newlstr(L,""s,(sizeof(s)/sizeof(char))-1))
  1890. #define luaS_fix(s) ((s)->tsv.marked|=(1<<4))
  1891. void luaS_resize(lua_State*L,int newsize);Udata*luaS_newudata(lua_State*L,
  1892. size_t s);void luaS_freeall(lua_State*L);TString*luaS_newlstr(lua_State*L,
  1893. const char*str,size_t l);
  1894. #endif
  1895. #line 24 "lapi.c"
  1896. #line 1 "ltable.h"
  1897. #ifndef ltable_h
  1898. #define ltable_h
  1899. #define gnode(t,i) (&(t)->node[i])
  1900. #define gkey(n) (&(n)->i_key)
  1901. #define gval(n) (&(n)->i_val)
  1902. const TObject*luaH_getnum(Table*t,int key);TObject*luaH_setnum(lua_State*L,
  1903. Table*t,int key);const TObject*luaH_getstr(Table*t,TString*key);const TObject*
  1904. luaH_get(Table*t,const TObject*key);TObject*luaH_set(lua_State*L,Table*t,const
  1905. TObject*key);Table*luaH_new(lua_State*L,int narray,int lnhash);void luaH_free
  1906. (lua_State*L,Table*t);int luaH_next(lua_State*L,Table*t,StkId key);Node*
  1907. luaH_mainposition(const Table*t,const TObject*key);
  1908. #endif
  1909. #line 25 "lapi.c"
  1910. #line 1 "lundump.h"
  1911. #ifndef lundump_h
  1912. #define lundump_h
  1913. Proto*luaU_undump(lua_State*L,ZIO*Z,Mbuffer*buff);int luaU_endianness(void);
  1914. void luaU_dump(lua_State*L,const Proto*Main,lua_Chunkwriter w,void*data);void
  1915. luaU_print(const Proto*Main);
  1916. #define LUA_SIGNATURE "\033Lua"
  1917. #define VERSION 0x50
  1918. #define VERSION0 0x50
  1919. #define TEST_NUMBER ((lua_Number)3.14159265358979323846E7)
  1920. #endif
  1921. #line 27 "lapi.c"
  1922. #line 1 "lvm.h"
  1923. #ifndef lvm_h
  1924. #define lvm_h
  1925. #define tostring(L,o) ((ttype(o)==LUA_TSTRING)||(luaV_tostring(L,o)))
  1926. #define tonumber(o,n) (ttype(o)==LUA_TNUMBER||(((o)=luaV_tonumber(o,n))!=NULL)\
  1927. )
  1928. #define equalobj(L,o1,o2) (ttype(o1)==ttype(o2)&&luaV_equalval(L,o1,o2))
  1929. int luaV_lessthan(lua_State*L,const TObject*l,const TObject*r);int
  1930. luaV_equalval(lua_State*L,const TObject*t1,const TObject*t2);const TObject*
  1931. luaV_tonumber(const TObject*obj,TObject*n);int luaV_tostring(lua_State*L,StkId
  1932. obj);const TObject*luaV_gettable(lua_State*L,const TObject*t,TObject*key,int
  1933. loop);void luaV_settable(lua_State*L,const TObject*t,TObject*key,StkId val);
  1934. StkId luaV_execute(lua_State*L);void luaV_concat(lua_State*L,int total,int
  1935. last);
  1936. #endif
  1937. #line 28 "lapi.c"
  1938. const char lua_ident[]="$Lua: "LUA_VERSION" "LUA_COPYRIGHT" $\n""$Authors: "
  1939. LUA_AUTHORS" $\n""$URL: www.lua.org $\n";
  1940. #ifndef api_check
  1941. #define api_check(L, o)
  1942. #endif
  1943. #define api_checknelems(L, n)api_check(L,(n)<=(L->top-L->base))
  1944. #define api_incr_top(L) {api_check(L,L->top<L->ci->top);L->top++;}
  1945. static TObject*negindex(lua_State*L,int idx){if(idx>LUA_REGISTRYINDEX){
  1946. api_check(L,idx!=0&&-idx<=L->top-L->base);return L->top+idx;}else switch(idx){
  1947. case LUA_REGISTRYINDEX:return registry(L);case LUA_GLOBALSINDEX:return gt(L);
  1948. default:{TObject*func=(L->base-1);idx=LUA_GLOBALSINDEX-idx;lua_assert(
  1949. iscfunction(func));return(idx<=clvalue(func)->c.nupvalues)?&clvalue(func)->c.
  1950. upvalue[idx-1]:NULL;}}}static TObject*luaA_index(lua_State*L,int idx){if(idx>0
  1951. ){api_check(L,idx<=L->top-L->base);return L->base+idx-1;}else{TObject*o=
  1952. negindex(L,idx);api_check(L,o!=NULL);return o;}}static TObject*
  1953. luaA_indexAcceptable(lua_State*L,int idx){if(idx>0){TObject*o=L->base+(idx-1);
  1954. api_check(L,idx<=L->stack_last-L->base);if(o>=L->top)return NULL;else return o
  1955. ;}else return negindex(L,idx);}void luaA_pushobject(lua_State*L,const TObject*
  1956. o){setobj2s(L->top,o);incr_top(L);}LUA_API int lua_checkstack(lua_State*L,int
  1957. size){int res;lua_lock(L);if((L->top-L->base+size)>LUA_MAXCSTACK)res=0;else{
  1958. luaD_checkstack(L,size);if(L->ci->top<L->top+size)L->ci->top=L->top+size;res=1
  1959. ;}lua_unlock(L);return res;}LUA_API void lua_xmove(lua_State*from,lua_State*to
  1960. ,int n){int i;lua_lock(to);api_checknelems(from,n);from->top-=n;for(i=0;i<n;i
  1961. ++){setobj2s(to->top,from->top+i);api_incr_top(to);}lua_unlock(to);}LUA_API
  1962. lua_CFunction lua_atpanic(lua_State*L,lua_CFunction panicf){lua_CFunction old;
  1963. lua_lock(L);old=G(L)->panic;G(L)->panic=panicf;lua_unlock(L);return old;}
  1964. LUA_API lua_State*lua_newthread(lua_State*L){lua_State*L1;lua_lock(L);
  1965. luaC_checkGC(L);L1=luaE_newthread(L);setthvalue(L->top,L1);api_incr_top(L);
  1966. lua_unlock(L);lua_userstateopen(L1);return L1;}LUA_API int lua_gettop(
  1967. lua_State*L){return(L->top-L->base);}LUA_API void lua_settop(lua_State*L,int
  1968. idx){lua_lock(L);if(idx>=0){api_check(L,idx<=L->stack_last-L->base);while(L->
  1969. top<L->base+idx)setnilvalue(L->top++);L->top=L->base+idx;}else{api_check(L,-(
  1970. idx+1)<=(L->top-L->base));L->top+=idx+1;}lua_unlock(L);}LUA_API void
  1971. lua_remove(lua_State*L,int idx){StkId p;lua_lock(L);p=luaA_index(L,idx);while(
  1972. ++p<L->top)setobjs2s(p-1,p);L->top--;lua_unlock(L);}LUA_API void lua_insert(
  1973. lua_State*L,int idx){StkId p;StkId q;lua_lock(L);p=luaA_index(L,idx);for(q=L->
  1974. top;q>p;q--)setobjs2s(q,q-1);setobjs2s(p,L->top);lua_unlock(L);}LUA_API void
  1975. lua_replace(lua_State*L,int idx){lua_lock(L);api_checknelems(L,1);setobj(
  1976. luaA_index(L,idx),L->top-1);L->top--;lua_unlock(L);}LUA_API void lua_pushvalue
  1977. (lua_State*L,int idx){lua_lock(L);setobj2s(L->top,luaA_index(L,idx));
  1978. api_incr_top(L);lua_unlock(L);}LUA_API int lua_type(lua_State*L,int idx){StkId
  1979. o=luaA_indexAcceptable(L,idx);return(o==NULL)?LUA_TNONE:ttype(o);}LUA_API
  1980. const char*lua_typename(lua_State*L,int t){UNUSED(L);return(t==LUA_TNONE)?
  1981. "no value":luaT_typenames[t];}LUA_API int lua_iscfunction(lua_State*L,int idx)
  1982. {StkId o=luaA_indexAcceptable(L,idx);return(o==NULL)?0:iscfunction(o);}LUA_API
  1983. int lua_isnumber(lua_State*L,int idx){TObject n;const TObject*o=
  1984. luaA_indexAcceptable(L,idx);return(o!=NULL&&tonumber(o,&n));}LUA_API int
  1985. lua_isstring(lua_State*L,int idx){int t=lua_type(L,idx);return(t==LUA_TSTRING
  1986. ||t==LUA_TNUMBER);}LUA_API int lua_isuserdata(lua_State*L,int idx){const
  1987. TObject*o=luaA_indexAcceptable(L,idx);return(o!=NULL&&(ttisuserdata(o)||
  1988. ttislightuserdata(o)));}LUA_API int lua_rawequal(lua_State*L,int index1,int
  1989. index2){StkId o1=luaA_indexAcceptable(L,index1);StkId o2=luaA_indexAcceptable(
  1990. L,index2);return(o1==NULL||o2==NULL)?0:luaO_rawequalObj(o1,o2);}LUA_API int
  1991. lua_equal(lua_State*L,int index1,int index2){StkId o1,o2;int i;lua_lock(L);o1=
  1992. luaA_indexAcceptable(L,index1);o2=luaA_indexAcceptable(L,index2);i=(o1==NULL||
  1993. o2==NULL)?0:equalobj(L,o1,o2);lua_unlock(L);return i;}LUA_API int lua_lessthan
  1994. (lua_State*L,int index1,int index2){StkId o1,o2;int i;lua_lock(L);o1=
  1995. luaA_indexAcceptable(L,index1);o2=luaA_indexAcceptable(L,index2);i=(o1==NULL||
  1996. o2==NULL)?0:luaV_lessthan(L,o1,o2);lua_unlock(L);return i;}LUA_API lua_Number
  1997. lua_tonumber(lua_State*L,int idx){TObject n;const TObject*o=
  1998. luaA_indexAcceptable(L,idx);if(o!=NULL&&tonumber(o,&n))return nvalue(o);else
  1999. return 0;}LUA_API int lua_toboolean(lua_State*L,int idx){const TObject*o=
  2000. luaA_indexAcceptable(L,idx);return(o!=NULL)&&!l_isfalse(o);}LUA_API const char
  2001. *lua_tostring(lua_State*L,int idx){StkId o=luaA_indexAcceptable(L,idx);if(o==
  2002. NULL)return NULL;else if(ttisstring(o))return svalue(o);else{const char*s;
  2003. lua_lock(L);s=(luaV_tostring(L,o)?svalue(o):NULL);luaC_checkGC(L);lua_unlock(L
  2004. );return s;}}LUA_API size_t lua_strlen(lua_State*L,int idx){StkId o=
  2005. luaA_indexAcceptable(L,idx);if(o==NULL)return 0;else if(ttisstring(o))return
  2006. tsvalue(o)->tsv.len;else{size_t l;lua_lock(L);l=(luaV_tostring(L,o)?tsvalue(o)
  2007. ->tsv.len:0);lua_unlock(L);return l;}}LUA_API lua_CFunction lua_tocfunction(
  2008. lua_State*L,int idx){StkId o=luaA_indexAcceptable(L,idx);return(o==NULL||!
  2009. iscfunction(o))?NULL:clvalue(o)->c.f;}LUA_API void*lua_touserdata(lua_State*L,
  2010. int idx){StkId o=luaA_indexAcceptable(L,idx);if(o==NULL)return NULL;switch(
  2011. ttype(o)){case LUA_TUSERDATA:return(uvalue(o)+1);case LUA_TLIGHTUSERDATA:
  2012. return pvalue(o);default:return NULL;}}LUA_API lua_State*lua_tothread(
  2013. lua_State*L,int idx){StkId o=luaA_indexAcceptable(L,idx);return(o==NULL||!
  2014. ttisthread(o))?NULL:thvalue(o);}LUA_API const void*lua_topointer(lua_State*L,
  2015. int idx){StkId o=luaA_indexAcceptable(L,idx);if(o==NULL)return NULL;else{
  2016. switch(ttype(o)){case LUA_TTABLE:return hvalue(o);case LUA_TFUNCTION:return
  2017. clvalue(o);case LUA_TTHREAD:return thvalue(o);case LUA_TUSERDATA:case
  2018. LUA_TLIGHTUSERDATA:return lua_touserdata(L,idx);default:return NULL;}}}LUA_API
  2019. void lua_pushnil(lua_State*L){lua_lock(L);setnilvalue(L->top);api_incr_top(L)
  2020. ;lua_unlock(L);}LUA_API void lua_pushnumber(lua_State*L,lua_Number n){lua_lock
  2021. (L);setnvalue(L->top,n);api_incr_top(L);lua_unlock(L);}LUA_API void
  2022. lua_pushlstring(lua_State*L,const char*s,size_t len){lua_lock(L);luaC_checkGC(
  2023. L);setsvalue2s(L->top,luaS_newlstr(L,s,len));api_incr_top(L);lua_unlock(L);}
  2024. LUA_API void lua_pushstring(lua_State*L,const char*s){if(s==NULL)lua_pushnil(L
  2025. );else lua_pushlstring(L,s,strlen(s));}LUA_API const char*lua_pushvfstring(
  2026. lua_State*L,const char*fmt,va_list argp){const char*ret;lua_lock(L);
  2027. luaC_checkGC(L);ret=luaO_pushvfstring(L,fmt,argp);lua_unlock(L);return ret;}
  2028. LUA_API const char*lua_pushfstring(lua_State*L,const char*fmt,...){const char*
  2029. ret;va_list argp;lua_lock(L);luaC_checkGC(L);va_start(argp,fmt);ret=
  2030. luaO_pushvfstring(L,fmt,argp);va_end(argp);lua_unlock(L);return ret;}LUA_API
  2031. void lua_pushcclosure(lua_State*L,lua_CFunction fn,int n){Closure*cl;lua_lock(
  2032. L);luaC_checkGC(L);api_checknelems(L,n);cl=luaF_newCclosure(L,n);cl->c.f=fn;L
  2033. ->top-=n;while(n--)setobj2n(&cl->c.upvalue[n],L->top+n);setclvalue(L->top,cl);
  2034. api_incr_top(L);lua_unlock(L);}LUA_API void lua_pushboolean(lua_State*L,int b)
  2035. {lua_lock(L);setbvalue(L->top,(b!=0));api_incr_top(L);lua_unlock(L);}LUA_API
  2036. void lua_pushlightuserdata(lua_State*L,void*p){lua_lock(L);setpvalue(L->top,p)
  2037. ;api_incr_top(L);lua_unlock(L);}LUA_API void lua_gettable(lua_State*L,int idx)
  2038. {StkId t;lua_lock(L);t=luaA_index(L,idx);setobj2s(L->top-1,luaV_gettable(L,t,L
  2039. ->top-1,0));lua_unlock(L);}LUA_API void lua_rawget(lua_State*L,int idx){StkId
  2040. t;lua_lock(L);t=luaA_index(L,idx);api_check(L,ttistable(t));setobj2s(L->top-1,
  2041. luaH_get(hvalue(t),L->top-1));lua_unlock(L);}LUA_API void lua_rawgeti(
  2042. lua_State*L,int idx,int n){StkId o;lua_lock(L);o=luaA_index(L,idx);api_check(L
  2043. ,ttistable(o));setobj2s(L->top,luaH_getnum(hvalue(o),n));api_incr_top(L);
  2044. lua_unlock(L);}LUA_API void lua_newtable(lua_State*L){lua_lock(L);luaC_checkGC
  2045. (L);sethvalue(L->top,luaH_new(L,0,0));api_incr_top(L);lua_unlock(L);}LUA_API
  2046. int lua_getmetatable(lua_State*L,int objindex){const TObject*obj;Table*mt=NULL
  2047. ;int res;lua_lock(L);obj=luaA_indexAcceptable(L,objindex);if(obj!=NULL){switch
  2048. (ttype(obj)){case LUA_TTABLE:mt=hvalue(obj)->metatable;break;case
  2049. LUA_TUSERDATA:mt=uvalue(obj)->uv.metatable;break;}}if(mt==NULL||mt==hvalue(
  2050. defaultmeta(L)))res=0;else{sethvalue(L->top,mt);api_incr_top(L);res=1;}
  2051. lua_unlock(L);return res;}LUA_API void lua_getfenv(lua_State*L,int idx){StkId
  2052. o;lua_lock(L);o=luaA_index(L,idx);setobj2s(L->top,isLfunction(o)?&clvalue(o)->
  2053. l.g:gt(L));api_incr_top(L);lua_unlock(L);}LUA_API void lua_settable(lua_State*
  2054. L,int idx){StkId t;lua_lock(L);api_checknelems(L,2);t=luaA_index(L,idx);
  2055. luaV_settable(L,t,L->top-2,L->top-1);L->top-=2;lua_unlock(L);}LUA_API void
  2056. lua_rawset(lua_State*L,int idx){StkId t;lua_lock(L);api_checknelems(L,2);t=
  2057. luaA_index(L,idx);api_check(L,ttistable(t));setobj2t(luaH_set(L,hvalue(t),L->
  2058. top-2),L->top-1);L->top-=2;lua_unlock(L);}LUA_API void lua_rawseti(lua_State*L
  2059. ,int idx,int n){StkId o;lua_lock(L);api_checknelems(L,1);o=luaA_index(L,idx);
  2060. api_check(L,ttistable(o));setobj2t(luaH_setnum(L,hvalue(o),n),L->top-1);L->top
  2061. --;lua_unlock(L);}LUA_API int lua_setmetatable(lua_State*L,int objindex){
  2062. TObject*obj,*mt;int res=1;lua_lock(L);api_checknelems(L,1);obj=luaA_index(L,
  2063. objindex);mt=(!ttisnil(L->top-1))?L->top-1:defaultmeta(L);api_check(L,
  2064. ttistable(mt));switch(ttype(obj)){case LUA_TTABLE:{hvalue(obj)->metatable=
  2065. hvalue(mt);break;}case LUA_TUSERDATA:{uvalue(obj)->uv.metatable=hvalue(mt);
  2066. break;}default:{res=0;break;}}L->top--;lua_unlock(L);return res;}LUA_API int
  2067. lua_setfenv(lua_State*L,int idx){StkId o;int res=0;lua_lock(L);api_checknelems
  2068. (L,1);o=luaA_index(L,idx);L->top--;api_check(L,ttistable(L->top));if(
  2069. isLfunction(o)){res=1;clvalue(o)->l.g=*(L->top);}lua_unlock(L);return res;}
  2070. LUA_API void lua_call(lua_State*L,int nargs,int nresults){StkId func;lua_lock(
  2071. L);api_checknelems(L,nargs+1);func=L->top-(nargs+1);luaD_call(L,func,nresults)
  2072. ;lua_unlock(L);}struct CallS{StkId func;int nresults;};static void f_call(
  2073. lua_State*L,void*ud){struct CallS*c=cast(struct CallS*,ud);luaD_call(L,c->func
  2074. ,c->nresults);}LUA_API int lua_pcall(lua_State*L,int nargs,int nresults,int
  2075. errfunc){struct CallS c;int status;ptrdiff_t func;lua_lock(L);func=(errfunc==0
  2076. )?0:savestack(L,luaA_index(L,errfunc));c.func=L->top-(nargs+1);c.nresults=
  2077. nresults;status=luaD_pcall(L,f_call,&c,savestack(L,c.func),func);lua_unlock(L)
  2078. ;return status;}struct CCallS{lua_CFunction func;void*ud;};static void f_Ccall
  2079. (lua_State*L,void*ud){struct CCallS*c=cast(struct CCallS*,ud);Closure*cl;cl=
  2080. luaF_newCclosure(L,0);cl->c.f=c->func;setclvalue(L->top,cl);incr_top(L);
  2081. setpvalue(L->top,c->ud);incr_top(L);luaD_call(L,L->top-2,0);}LUA_API int
  2082. lua_cpcall(lua_State*L,lua_CFunction func,void*ud){struct CCallS c;int status;
  2083. lua_lock(L);c.func=func;c.ud=ud;status=luaD_pcall(L,f_Ccall,&c,savestack(L,L->
  2084. top),0);lua_unlock(L);return status;}LUA_API int lua_load(lua_State*L,
  2085. lua_Chunkreader reader,void*data,const char*chunkname){ZIO z;int status;int c;
  2086. lua_lock(L);if(!chunkname)chunkname="?";luaZ_init(&z,reader,data,chunkname);c=
  2087. luaZ_lookahead(&z);status=luaD_protectedparser(L,&z,(c==LUA_SIGNATURE[0]));
  2088. lua_unlock(L);return status;}LUA_API int lua_dump(lua_State*L,lua_Chunkwriter
  2089. writer,void*data){int status;TObject*o;lua_lock(L);api_checknelems(L,1);o=L->
  2090. top-1;if(isLfunction(o)&&clvalue(o)->l.nupvalues==0){luaU_dump(L,clvalue(o)->l
  2091. .p,writer,data);status=1;}else status=0;lua_unlock(L);return status;}
  2092. #define GCscalel(x) ((x)>>10)
  2093. #define GCscale(x) (cast(int,GCscalel(x)))
  2094. #define GCunscale(x) (cast(lu_mem,x)<<10)
  2095. LUA_API int lua_getgcthreshold(lua_State*L){int threshold;lua_lock(L);
  2096. threshold=GCscale(G(L)->GCthreshold);lua_unlock(L);return threshold;}LUA_API
  2097. int lua_getgccount(lua_State*L){int count;lua_lock(L);count=GCscale(G(L)->
  2098. nblocks);lua_unlock(L);return count;}LUA_API void lua_setgcthreshold(lua_State
  2099. *L,int newthreshold){lua_lock(L);if(cast(lu_mem,newthreshold)>GCscalel(
  2100. MAX_LUMEM))G(L)->GCthreshold=MAX_LUMEM;else G(L)->GCthreshold=GCunscale(
  2101. newthreshold);luaC_checkGC(L);lua_unlock(L);}LUA_API const char*lua_version(
  2102. void){return LUA_VERSION;}LUA_API int lua_error(lua_State*L){lua_lock(L);
  2103. api_checknelems(L,1);luaG_errormsg(L);lua_unlock(L);return 0;}LUA_API int
  2104. lua_next(lua_State*L,int idx){StkId t;int more;lua_lock(L);t=luaA_index(L,idx)
  2105. ;api_check(L,ttistable(t));more=luaH_next(L,hvalue(t),L->top-1);if(more){
  2106. api_incr_top(L);}else L->top-=1;lua_unlock(L);return more;}LUA_API void
  2107. lua_concat(lua_State*L,int n){lua_lock(L);luaC_checkGC(L);api_checknelems(L,n)
  2108. ;if(n>=2){luaV_concat(L,n,L->top-L->base-1);L->top-=(n-1);}else if(n==0){
  2109. setsvalue2s(L->top,luaS_newlstr(L,NULL,0));api_incr_top(L);}lua_unlock(L);}
  2110. LUA_API void*lua_newuserdata(lua_State*L,size_t size){Udata*u;lua_lock(L);
  2111. luaC_checkGC(L);u=luaS_newudata(L,size);setuvalue(L->top,u);api_incr_top(L);
  2112. lua_unlock(L);return u+1;}LUA_API int lua_pushupvalues(lua_State*L){Closure*
  2113. func;int n,i;lua_lock(L);api_check(L,iscfunction(L->base-1));func=clvalue(L->
  2114. base-1);n=func->c.nupvalues;luaD_checkstack(L,n+LUA_MINSTACK);for(i=0;i<n;i++)
  2115. {setobj2s(L->top,&func->c.upvalue[i]);L->top++;}lua_unlock(L);return n;}static
  2116. const char*aux_upvalue(lua_State*L,int funcindex,int n,TObject**val){Closure*
  2117. f;StkId fi=luaA_index(L,funcindex);if(!ttisfunction(fi))return NULL;f=clvalue(
  2118. fi);if(f->c.isC){if(n>f->c.nupvalues)return NULL;*val=&f->c.upvalue[n-1];
  2119. return"";}else{Proto*p=f->l.p;if(n>p->sizeupvalues)return NULL;*val=f->l.
  2120. upvals[n-1]->v;return getstr(p->upvalues[n-1]);}}LUA_API const char*
  2121. lua_getupvalue(lua_State*L,int funcindex,int n){const char*name;TObject*val;
  2122. lua_lock(L);name=aux_upvalue(L,funcindex,n,&val);if(name){setobj2s(L->top,val)
  2123. ;api_incr_top(L);}lua_unlock(L);return name;}LUA_API const char*lua_setupvalue
  2124. (lua_State*L,int funcindex,int n){const char*name;TObject*val;lua_lock(L);
  2125. api_checknelems(L,1);name=aux_upvalue(L,funcindex,n,&val);if(name){L->top--;
  2126. setobj(val,L->top);}lua_unlock(L);return name;}
  2127. #line 1 "lauxlib.c"
  2128. #define lauxlib_c
  2129. #line 1 "lauxlib.h"
  2130. #ifndef lauxlib_h
  2131. #define lauxlib_h
  2132. #ifndef LUALIB_API
  2133. #define LUALIB_API LUA_API
  2134. #endif
  2135. typedef struct luaL_reg{const char*name;lua_CFunction func;}luaL_reg;
  2136. LUALIB_API void luaL_openlib(lua_State*L,const char*libname,const luaL_reg*l,
  2137. int nup);LUALIB_API int luaL_getmetafield(lua_State*L,int obj,const char*e);
  2138. LUALIB_API int luaL_callmeta(lua_State*L,int obj,const char*e);LUALIB_API int
  2139. luaL_typerror(lua_State*L,int narg,const char*tname);LUALIB_API int
  2140. luaL_argerror(lua_State*L,int numarg,const char*extramsg);LUALIB_API const
  2141. char*luaL_checklstring(lua_State*L,int numArg,size_t*l);LUALIB_API const char*
  2142. luaL_optlstring(lua_State*L,int numArg,const char*def,size_t*l);LUALIB_API
  2143. lua_Number luaL_checknumber(lua_State*L,int numArg);LUALIB_API lua_Number
  2144. luaL_optnumber(lua_State*L,int nArg,lua_Number def);LUALIB_API void
  2145. luaL_checkstack(lua_State*L,int sz,const char*msg);LUALIB_API void
  2146. luaL_checktype(lua_State*L,int narg,int t);LUALIB_API void luaL_checkany(
  2147. lua_State*L,int narg);LUALIB_API int luaL_newmetatable(lua_State*L,const char*
  2148. tname);LUALIB_API void luaL_getmetatable(lua_State*L,const char*tname);
  2149. LUALIB_API void*luaL_checkudata(lua_State*L,int ud,const char*tname);
  2150. LUALIB_API void luaL_where(lua_State*L,int lvl);LUALIB_API int luaL_error(
  2151. lua_State*L,const char*fmt,...);LUALIB_API int luaL_findstring(const char*st,
  2152. const char*const lst[]);LUALIB_API int luaL_ref(lua_State*L,int t);LUALIB_API
  2153. void luaL_unref(lua_State*L,int t,int ref);LUALIB_API int luaL_getn(lua_State*
  2154. L,int t);LUALIB_API void luaL_setn(lua_State*L,int t,int n);LUALIB_API int
  2155. luaL_loadfile(lua_State*L,const char*filename);LUALIB_API int luaL_loadbuffer(
  2156. lua_State*L,const char*buff,size_t sz,const char*name);
  2157. #define luaL_argcheck(L, cond,numarg,extramsg)if(!(cond))luaL_argerror(L,\
  2158. numarg,extramsg)
  2159. #define luaL_checkstring(L,n) (luaL_checklstring(L,(n),NULL))
  2160. #define luaL_optstring(L,n,d) (luaL_optlstring(L,(n),(d),NULL))
  2161. #define luaL_checkint(L,n) ((int)luaL_checknumber(L,n))
  2162. #define luaL_checklong(L,n) ((long)luaL_checknumber(L,n))
  2163. #define luaL_optint(L,n,d) ((int)luaL_optnumber(L,n,(lua_Number)(d)))
  2164. #define luaL_optlong(L,n,d) ((long)luaL_optnumber(L,n,(lua_Number)(d)))
  2165. #ifndef LUAL_BUFFERSIZE
  2166. #define LUAL_BUFFERSIZE BUFSIZ
  2167. #endif
  2168. typedef struct luaL_Buffer{char*p;int lvl;lua_State*L;char buffer[
  2169. LUAL_BUFFERSIZE];}luaL_Buffer;
  2170. #define luaL_putchar(B,c) ((void)((B)->p<((B)->buffer+LUAL_BUFFERSIZE)||\
  2171. luaL_prepbuffer(B)),(*(B)->p++=(char)(c)))
  2172. #define luaL_addsize(B,n) ((B)->p+=(n))
  2173. LUALIB_API void luaL_buffinit(lua_State*L,luaL_Buffer*B);LUALIB_API char*
  2174. luaL_prepbuffer(luaL_Buffer*B);LUALIB_API void luaL_addlstring(luaL_Buffer*B,
  2175. const char*s,size_t l);LUALIB_API void luaL_addstring(luaL_Buffer*B,const char
  2176. *s);LUALIB_API void luaL_addvalue(luaL_Buffer*B);LUALIB_API void
  2177. luaL_pushresult(luaL_Buffer*B);LUALIB_API int lua_dofile(lua_State*L,const
  2178. char*filename);LUALIB_API int lua_dostring(lua_State*L,const char*str);
  2179. LUALIB_API int lua_dobuffer(lua_State*L,const char*buff,size_t sz,const char*n
  2180. );
  2181. #define luaL_check_lstr luaL_checklstring
  2182. #define luaL_opt_lstr luaL_optlstring
  2183. #define luaL_check_number luaL_checknumber
  2184. #define luaL_opt_number luaL_optnumber
  2185. #define luaL_arg_check luaL_argcheck
  2186. #define luaL_check_string luaL_checkstring
  2187. #define luaL_opt_string luaL_optstring
  2188. #define luaL_check_int luaL_checkint
  2189. #define luaL_check_long luaL_checklong
  2190. #define luaL_opt_int luaL_optint
  2191. #define luaL_opt_long luaL_optlong
  2192. #endif
  2193. #line 24 "lauxlib.c"
  2194. #define RESERVED_REFS 2
  2195. #define FREELIST_REF 1
  2196. #define ARRAYSIZE_REF 2
  2197. #define abs_index(L, i)((i)>0||(i)<=LUA_REGISTRYINDEX?(i):lua_gettop(L)+(i)+1)
  2198. LUALIB_API int luaL_argerror(lua_State*L,int narg,const char*extramsg){
  2199. lua_Debug ar;lua_getstack(L,0,&ar);lua_getinfo(L,"n",&ar);if(strcmp(ar.
  2200. namewhat,"method")==0){narg--;if(narg==0)return luaL_error(L,
  2201. "calling `%s' on bad self (%s)",ar.name,extramsg);}if(ar.name==NULL)ar.name=
  2202. "?";return luaL_error(L,"bad argument #%d to `%s' (%s)",narg,ar.name,extramsg)
  2203. ;}LUALIB_API int luaL_typerror(lua_State*L,int narg,const char*tname){const
  2204. char*msg=lua_pushfstring(L,"%s expected, got %s",tname,lua_typename(L,lua_type
  2205. (L,narg)));return luaL_argerror(L,narg,msg);}static void tag_error(lua_State*L
  2206. ,int narg,int tag){luaL_typerror(L,narg,lua_typename(L,tag));}LUALIB_API void
  2207. luaL_where(lua_State*L,int level){lua_Debug ar;if(lua_getstack(L,level,&ar)){
  2208. lua_getinfo(L,"Snl",&ar);if(ar.currentline>0){lua_pushfstring(L,"%s:%d: ",ar.
  2209. short_src,ar.currentline);return;}}lua_pushliteral(L,"");}LUALIB_API int
  2210. luaL_error(lua_State*L,const char*fmt,...){va_list argp;va_start(argp,fmt);
  2211. luaL_where(L,1);lua_pushvfstring(L,fmt,argp);va_end(argp);lua_concat(L,2);
  2212. return lua_error(L);}LUALIB_API int luaL_findstring(const char*name,const char
  2213. *const list[]){int i;for(i=0;list[i];i++)if(strcmp(list[i],name)==0)return i;
  2214. return-1;}LUALIB_API int luaL_newmetatable(lua_State*L,const char*tname){
  2215. lua_pushstring(L,tname);lua_rawget(L,LUA_REGISTRYINDEX);if(!lua_isnil(L,-1))
  2216. return 0;lua_pop(L,1);lua_newtable(L);lua_pushstring(L,tname);lua_pushvalue(L,
  2217. -2);lua_rawset(L,LUA_REGISTRYINDEX);lua_pushvalue(L,-1);lua_pushstring(L,tname
  2218. );lua_rawset(L,LUA_REGISTRYINDEX);return 1;}LUALIB_API void luaL_getmetatable(
  2219. lua_State*L,const char*tname){lua_pushstring(L,tname);lua_rawget(L,
  2220. LUA_REGISTRYINDEX);}LUALIB_API void*luaL_checkudata(lua_State*L,int ud,const
  2221. char*tname){const char*tn;if(!lua_getmetatable(L,ud))return NULL;lua_rawget(L,
  2222. LUA_REGISTRYINDEX);tn=lua_tostring(L,-1);if(tn&&(strcmp(tn,tname)==0)){lua_pop
  2223. (L,1);return lua_touserdata(L,ud);}else{lua_pop(L,1);return NULL;}}LUALIB_API
  2224. void luaL_checkstack(lua_State*L,int space,const char*mes){if(!lua_checkstack(
  2225. L,space))luaL_error(L,"stack overflow (%s)",mes);}LUALIB_API void
  2226. luaL_checktype(lua_State*L,int narg,int t){if(lua_type(L,narg)!=t)tag_error(L,
  2227. narg,t);}LUALIB_API void luaL_checkany(lua_State*L,int narg){if(lua_type(L,
  2228. narg)==LUA_TNONE)luaL_argerror(L,narg,"value expected");}LUALIB_API const char
  2229. *luaL_checklstring(lua_State*L,int narg,size_t*len){const char*s=lua_tostring(
  2230. L,narg);if(!s)tag_error(L,narg,LUA_TSTRING);if(len)*len=lua_strlen(L,narg);
  2231. return s;}LUALIB_API const char*luaL_optlstring(lua_State*L,int narg,const
  2232. char*def,size_t*len){if(lua_isnoneornil(L,narg)){if(len)*len=(def?strlen(def):
  2233. 0);return def;}else return luaL_checklstring(L,narg,len);}LUALIB_API
  2234. lua_Number luaL_checknumber(lua_State*L,int narg){lua_Number d=lua_tonumber(L,
  2235. narg);if(d==0&&!lua_isnumber(L,narg))tag_error(L,narg,LUA_TNUMBER);return d;}
  2236. LUALIB_API lua_Number luaL_optnumber(lua_State*L,int narg,lua_Number def){if(
  2237. lua_isnoneornil(L,narg))return def;else return luaL_checknumber(L,narg);}
  2238. LUALIB_API int luaL_getmetafield(lua_State*L,int obj,const char*event){if(!
  2239. lua_getmetatable(L,obj))return 0;lua_pushstring(L,event);lua_rawget(L,-2);if(
  2240. lua_isnil(L,-1)){lua_pop(L,2);return 0;}else{lua_remove(L,-2);return 1;}}
  2241. LUALIB_API int luaL_callmeta(lua_State*L,int obj,const char*event){obj=
  2242. abs_index(L,obj);if(!luaL_getmetafield(L,obj,event))return 0;lua_pushvalue(L,
  2243. obj);lua_call(L,1,1);return 1;}LUALIB_API void luaL_openlib(lua_State*L,const
  2244. char*libname,const luaL_reg*l,int nup){if(libname){lua_pushstring(L,libname);
  2245. lua_gettable(L,LUA_GLOBALSINDEX);if(lua_isnil(L,-1)){lua_pop(L,1);lua_newtable
  2246. (L);lua_pushstring(L,libname);lua_pushvalue(L,-2);lua_settable(L,
  2247. LUA_GLOBALSINDEX);}lua_insert(L,-(nup+1));}for(;l->name;l++){int i;
  2248. lua_pushstring(L,l->name);for(i=0;i<nup;i++)lua_pushvalue(L,-(nup+1));
  2249. lua_pushcclosure(L,l->func,nup);lua_settable(L,-(nup+3));}lua_pop(L,nup);}
  2250. static int checkint(lua_State*L,int topop){int n=(int)lua_tonumber(L,-1);if(n
  2251. ==0&&!lua_isnumber(L,-1))n=-1;lua_pop(L,topop);return n;}static void getsizes(
  2252. lua_State*L){lua_rawgeti(L,LUA_REGISTRYINDEX,ARRAYSIZE_REF);if(lua_isnil(L,-1)
  2253. ){lua_pop(L,1);lua_newtable(L);lua_pushvalue(L,-1);lua_setmetatable(L,-2);
  2254. lua_pushliteral(L,"__mode");lua_pushliteral(L,"k");lua_rawset(L,-3);
  2255. lua_pushvalue(L,-1);lua_rawseti(L,LUA_REGISTRYINDEX,ARRAYSIZE_REF);}}void
  2256. luaL_setn(lua_State*L,int t,int n){t=abs_index(L,t);lua_pushliteral(L,"n");
  2257. lua_rawget(L,t);if(checkint(L,1)>=0){lua_pushliteral(L,"n");lua_pushnumber(L,(
  2258. lua_Number)n);lua_rawset(L,t);}else{getsizes(L);lua_pushvalue(L,t);
  2259. lua_pushnumber(L,(lua_Number)n);lua_rawset(L,-3);lua_pop(L,1);}}int luaL_getn(
  2260. lua_State*L,int t){int n;t=abs_index(L,t);lua_pushliteral(L,"n");lua_rawget(L,
  2261. t);if((n=checkint(L,1))>=0)return n;getsizes(L);lua_pushvalue(L,t);lua_rawget(
  2262. L,-2);if((n=checkint(L,2))>=0)return n;for(n=1;;n++){lua_rawgeti(L,t,n);if(
  2263. lua_isnil(L,-1))break;lua_pop(L,1);}lua_pop(L,1);return n-1;}
  2264. #define bufflen(B) ((B)->p-(B)->buffer)
  2265. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE-bufflen(B)))
  2266. #define LIMIT (LUA_MINSTACK/2)
  2267. static int emptybuffer(luaL_Buffer*B){size_t l=bufflen(B);if(l==0)return 0;
  2268. else{lua_pushlstring(B->L,B->buffer,l);B->p=B->buffer;B->lvl++;return 1;}}
  2269. static void adjuststack(luaL_Buffer*B){if(B->lvl>1){lua_State*L=B->L;int toget
  2270. =1;size_t toplen=lua_strlen(L,-1);do{size_t l=lua_strlen(L,-(toget+1));if(B->
  2271. lvl-toget+1>=LIMIT||toplen>l){toplen+=l;toget++;}else break;}while(toget<B->
  2272. lvl);lua_concat(L,toget);B->lvl=B->lvl-toget+1;}}LUALIB_API char*
  2273. luaL_prepbuffer(luaL_Buffer*B){if(emptybuffer(B))adjuststack(B);return B->
  2274. buffer;}LUALIB_API void luaL_addlstring(luaL_Buffer*B,const char*s,size_t l){
  2275. while(l--)luaL_putchar(B,*s++);}LUALIB_API void luaL_addstring(luaL_Buffer*B,
  2276. const char*s){luaL_addlstring(B,s,strlen(s));}LUALIB_API void luaL_pushresult(
  2277. luaL_Buffer*B){emptybuffer(B);lua_concat(B->L,B->lvl);B->lvl=1;}LUALIB_API
  2278. void luaL_addvalue(luaL_Buffer*B){lua_State*L=B->L;size_t vl=lua_strlen(L,-1);
  2279. if(vl<=bufffree(B)){memcpy(B->p,lua_tostring(L,-1),vl);B->p+=vl;lua_pop(L,1);}
  2280. else{if(emptybuffer(B))lua_insert(L,-2);B->lvl++;adjuststack(B);}}LUALIB_API
  2281. void luaL_buffinit(lua_State*L,luaL_Buffer*B){B->L=L;B->p=B->buffer;B->lvl=0;}
  2282. LUALIB_API int luaL_ref(lua_State*L,int t){int ref;t=abs_index(L,t);if(
  2283. lua_isnil(L,-1)){lua_pop(L,1);return LUA_REFNIL;}lua_rawgeti(L,t,FREELIST_REF)
  2284. ;ref=(int)lua_tonumber(L,-1);lua_pop(L,1);if(ref!=0){lua_rawgeti(L,t,ref);
  2285. lua_rawseti(L,t,FREELIST_REF);}else{ref=luaL_getn(L,t);if(ref<RESERVED_REFS)
  2286. ref=RESERVED_REFS;ref++;luaL_setn(L,t,ref);}lua_rawseti(L,t,ref);return ref;}
  2287. LUALIB_API void luaL_unref(lua_State*L,int t,int ref){if(ref>=0){t=abs_index(L
  2288. ,t);lua_rawgeti(L,t,FREELIST_REF);lua_rawseti(L,t,ref);lua_pushnumber(L,(
  2289. lua_Number)ref);lua_rawseti(L,t,FREELIST_REF);}}typedef struct LoadF{FILE*f;
  2290. char buff[LUAL_BUFFERSIZE];}LoadF;static const char*getF(lua_State*L,void*ud,
  2291. size_t*size){LoadF*lf=(LoadF*)ud;(void)L;if(feof(lf->f))return NULL;*size=
  2292. fread(lf->buff,1,LUAL_BUFFERSIZE,lf->f);return(*size>0)?lf->buff:NULL;}static
  2293. int errfile(lua_State*L,int fnameindex){const char*filename=lua_tostring(L,
  2294. fnameindex)+1;lua_pushfstring(L,"cannot read %s: %s",filename,strerror(errno))
  2295. ;lua_remove(L,fnameindex);return LUA_ERRFILE;}LUALIB_API int luaL_loadfile(
  2296. lua_State*L,const char*filename){LoadF lf;int status,readstatus;int c;int
  2297. fnameindex=lua_gettop(L)+1;if(filename==NULL){lua_pushliteral(L,"=stdin");lf.f
  2298. =stdin;}else{lua_pushfstring(L,"@%s",filename);lf.f=fopen(filename,"r");}if(lf
  2299. .f==NULL)return errfile(L,fnameindex);c=ungetc(getc(lf.f),lf.f);if(!(isspace(c
  2300. )||isprint(c))&&lf.f!=stdin){fclose(lf.f);lf.f=fopen(filename,"rb");if(lf.f==
  2301. NULL)return errfile(L,fnameindex);}status=lua_load(L,getF,&lf,lua_tostring(L,-
  2302. 1));readstatus=ferror(lf.f);if(lf.f!=stdin)fclose(lf.f);if(readstatus){
  2303. lua_settop(L,fnameindex);return errfile(L,fnameindex);}lua_remove(L,fnameindex
  2304. );return status;}typedef struct LoadS{const char*s;size_t size;}LoadS;static
  2305. const char*getS(lua_State*L,void*ud,size_t*size){LoadS*ls=(LoadS*)ud;(void)L;
  2306. if(ls->size==0)return NULL;*size=ls->size;ls->size=0;return ls->s;}LUALIB_API
  2307. int luaL_loadbuffer(lua_State*L,const char*buff,size_t size,const char*name){
  2308. LoadS ls;ls.s=buff;ls.size=size;return lua_load(L,getS,&ls,name);}static void
  2309. callalert(lua_State*L,int status){if(status!=0){lua_getglobal(L,"_ALERT");if(
  2310. lua_isfunction(L,-1)){lua_insert(L,-2);lua_call(L,1,0);}else{fprintf(stderr,
  2311. "%s\n",lua_tostring(L,-2));lua_pop(L,2);}}}static int aux_do(lua_State*L,int
  2312. status){if(status==0){status=lua_pcall(L,0,LUA_MULTRET,0);}callalert(L,status)
  2313. ;return status;}LUALIB_API int lua_dofile(lua_State*L,const char*filename){
  2314. return aux_do(L,luaL_loadfile(L,filename));}LUALIB_API int lua_dobuffer(
  2315. lua_State*L,const char*buff,size_t size,const char*name){return aux_do(L,
  2316. luaL_loadbuffer(L,buff,size,name));}LUALIB_API int lua_dostring(lua_State*L,
  2317. const char*str){return lua_dobuffer(L,str,strlen(str),str);}
  2318. #line 1 "lbaselib.c"
  2319. #define lbaselib_c
  2320. #line 1 "lualib.h"
  2321. #ifndef lualib_h
  2322. #define lualib_h
  2323. #ifndef LUALIB_API
  2324. #define LUALIB_API LUA_API
  2325. #endif
  2326. #define LUA_COLIBNAME "coroutine"
  2327. LUALIB_API int luaopen_base(lua_State*L);
  2328. #define LUA_TABLIBNAME "table"
  2329. LUALIB_API int luaopen_table(lua_State*L);
  2330. #define LUA_IOLIBNAME "io"
  2331. #define LUA_OSLIBNAME "os"
  2332. LUALIB_API int luaopen_io(lua_State*L);
  2333. #define LUA_STRLIBNAME "string"
  2334. LUALIB_API int luaopen_string(lua_State*L);
  2335. #define LUA_MATHLIBNAME "math"
  2336. LUALIB_API int luaopen_math(lua_State*L);
  2337. #define LUA_DBLIBNAME "debug"
  2338. LUALIB_API int luaopen_debug(lua_State*L);LUALIB_API int luaopen_loadlib(
  2339. lua_State*L);
  2340. #ifndef lua_assert
  2341. #define lua_assert(c)
  2342. #endif
  2343. #define lua_baselibopen luaopen_base
  2344. #define lua_tablibopen luaopen_table
  2345. #define lua_iolibopen luaopen_io
  2346. #define lua_strlibopen luaopen_string
  2347. #define lua_mathlibopen luaopen_math
  2348. #define lua_dblibopen luaopen_debug
  2349. #endif
  2350. #line 20 "lbaselib.c"
  2351. static int luaB_print(lua_State*L){int n=lua_gettop(L);int i;lua_getglobal(L,
  2352. "tostring");for(i=1;i<=n;i++){const char*s;lua_pushvalue(L,-1);lua_pushvalue(L
  2353. ,i);lua_call(L,1,1);s=lua_tostring(L,-1);if(s==NULL)return luaL_error(L,
  2354. "`tostring' must return a string to `print'");if(i>1)fputs("\t",stdout);fputs(
  2355. s,stdout);lua_pop(L,1);}fputs("\n",stdout);return 0;}static int luaB_tonumber(
  2356. lua_State*L){int base=luaL_optint(L,2,10);if(base==10){luaL_checkany(L,1);if(
  2357. lua_isnumber(L,1)){lua_pushnumber(L,lua_tonumber(L,1));return 1;}}else{const
  2358. char*s1=luaL_checkstring(L,1);char*s2;unsigned long n;luaL_argcheck(L,2<=base
  2359. &&base<=36,2,"base out of range");n=strtoul(s1,&s2,base);if(s1!=s2){while(
  2360. isspace((unsigned char)(*s2)))s2++;if(*s2=='\0'){lua_pushnumber(L,(lua_Number)
  2361. n);return 1;}}}lua_pushnil(L);return 1;}static int luaB_error(lua_State*L){int
  2362. level=luaL_optint(L,2,1);luaL_checkany(L,1);if(!lua_isstring(L,1)||level==0)
  2363. lua_pushvalue(L,1);else{luaL_where(L,level);lua_pushvalue(L,1);lua_concat(L,2)
  2364. ;}return lua_error(L);}static int luaB_getmetatable(lua_State*L){luaL_checkany
  2365. (L,1);if(!lua_getmetatable(L,1)){lua_pushnil(L);return 1;}luaL_getmetafield(L,
  2366. 1,"__metatable");return 1;}static int luaB_setmetatable(lua_State*L){int t=
  2367. lua_type(L,2);luaL_checktype(L,1,LUA_TTABLE);luaL_argcheck(L,t==LUA_TNIL||t==
  2368. LUA_TTABLE,2,"nil or table expected");if(luaL_getmetafield(L,1,"__metatable"))
  2369. luaL_error(L,"cannot change a protected metatable");lua_settop(L,2);
  2370. lua_setmetatable(L,1);return 1;}static void getfunc(lua_State*L){if(
  2371. lua_isfunction(L,1))lua_pushvalue(L,1);else{lua_Debug ar;int level=luaL_optint
  2372. (L,1,1);luaL_argcheck(L,level>=0,1,"level must be non-negative");if(
  2373. lua_getstack(L,level,&ar)==0)luaL_argerror(L,1,"invalid level");lua_getinfo(L,
  2374. "f",&ar);if(lua_isnil(L,-1))luaL_error(L,
  2375. "no function environment for tail call at level %d",level);}}static int
  2376. aux_getfenv(lua_State*L){lua_getfenv(L,-1);lua_pushliteral(L,"__fenv");
  2377. lua_rawget(L,-2);return!lua_isnil(L,-1);}static int luaB_getfenv(lua_State*L){
  2378. getfunc(L);if(!aux_getfenv(L))lua_pop(L,1);return 1;}static int luaB_setfenv(
  2379. lua_State*L){luaL_checktype(L,2,LUA_TTABLE);getfunc(L);if(aux_getfenv(L))
  2380. luaL_error(L,"`setfenv' cannot change a protected environment");else lua_pop(L
  2381. ,2);lua_pushvalue(L,2);if(lua_isnumber(L,1)&&lua_tonumber(L,1)==0)lua_replace(
  2382. L,LUA_GLOBALSINDEX);else if(lua_setfenv(L,-2)==0)luaL_error(L,
  2383. "`setfenv' cannot change environment of given function");return 0;}static int
  2384. luaB_rawequal(lua_State*L){luaL_checkany(L,1);luaL_checkany(L,2);
  2385. lua_pushboolean(L,lua_rawequal(L,1,2));return 1;}static int luaB_rawget(
  2386. lua_State*L){luaL_checktype(L,1,LUA_TTABLE);luaL_checkany(L,2);lua_rawget(L,1)
  2387. ;return 1;}static int luaB_rawset(lua_State*L){luaL_checktype(L,1,LUA_TTABLE);
  2388. luaL_checkany(L,2);luaL_checkany(L,3);lua_rawset(L,1);return 1;}static int
  2389. luaB_gcinfo(lua_State*L){lua_pushnumber(L,(lua_Number)lua_getgccount(L));
  2390. lua_pushnumber(L,(lua_Number)lua_getgcthreshold(L));return 2;}static int
  2391. luaB_collectgarbage(lua_State*L){lua_setgcthreshold(L,luaL_optint(L,1,0));
  2392. return 0;}static int luaB_type(lua_State*L){luaL_checkany(L,1);lua_pushstring(
  2393. L,lua_typename(L,lua_type(L,1)));return 1;}static int luaB_next(lua_State*L){
  2394. luaL_checktype(L,1,LUA_TTABLE);lua_settop(L,2);if(lua_next(L,1))return 2;else{
  2395. lua_pushnil(L);return 1;}}static int luaB_pairs(lua_State*L){luaL_checktype(L,
  2396. 1,LUA_TTABLE);lua_pushliteral(L,"next");lua_rawget(L,LUA_GLOBALSINDEX);
  2397. lua_pushvalue(L,1);lua_pushnil(L);return 3;}static int luaB_ipairs(lua_State*L
  2398. ){lua_Number i=lua_tonumber(L,2);luaL_checktype(L,1,LUA_TTABLE);if(i==0&&
  2399. lua_isnone(L,2)){lua_pushliteral(L,"ipairs");lua_rawget(L,LUA_GLOBALSINDEX);
  2400. lua_pushvalue(L,1);lua_pushnumber(L,0);return 3;}else{i++;lua_pushnumber(L,i);
  2401. lua_rawgeti(L,1,(int)i);return(lua_isnil(L,-1))?0:2;}}static int load_aux(
  2402. lua_State*L,int status){if(status==0)return 1;else{lua_pushnil(L);lua_insert(L
  2403. ,-2);return 2;}}static int luaB_loadstring(lua_State*L){size_t l;const char*s=
  2404. luaL_checklstring(L,1,&l);const char*chunkname=luaL_optstring(L,2,s);return
  2405. load_aux(L,luaL_loadbuffer(L,s,l,chunkname));}static int luaB_loadfile(
  2406. lua_State*L){const char*fname=luaL_optstring(L,1,NULL);return load_aux(L,
  2407. luaL_loadfile(L,fname));}static int luaB_dofile(lua_State*L){const char*fname=
  2408. luaL_optstring(L,1,NULL);int n=lua_gettop(L);int status=luaL_loadfile(L,fname)
  2409. ;if(status!=0)lua_error(L);lua_call(L,0,LUA_MULTRET);return lua_gettop(L)-n;}
  2410. static int luaB_assert(lua_State*L){luaL_checkany(L,1);if(!lua_toboolean(L,1))
  2411. return luaL_error(L,"%s",luaL_optstring(L,2,"assertion failed!"));lua_settop(L
  2412. ,1);return 1;}static int luaB_unpack(lua_State*L){int n,i;luaL_checktype(L,1,
  2413. LUA_TTABLE);n=luaL_getn(L,1);luaL_checkstack(L,n,"table too big to unpack");
  2414. for(i=1;i<=n;i++)lua_rawgeti(L,1,i);return n;}static int luaB_pcall(lua_State*
  2415. L){int status;luaL_checkany(L,1);status=lua_pcall(L,lua_gettop(L)-1,
  2416. LUA_MULTRET,0);lua_pushboolean(L,(status==0));lua_insert(L,1);return
  2417. lua_gettop(L);}static int luaB_xpcall(lua_State*L){int status;luaL_checkany(L,
  2418. 2);lua_settop(L,2);lua_insert(L,1);status=lua_pcall(L,0,LUA_MULTRET,1);
  2419. lua_pushboolean(L,(status==0));lua_replace(L,1);return lua_gettop(L);}static
  2420. int luaB_tostring(lua_State*L){char buff[128];luaL_checkany(L,1);if(
  2421. luaL_callmeta(L,1,"__tostring"))return 1;switch(lua_type(L,1)){case
  2422. LUA_TNUMBER:lua_pushstring(L,lua_tostring(L,1));return 1;case LUA_TSTRING:
  2423. lua_pushvalue(L,1);return 1;case LUA_TBOOLEAN:lua_pushstring(L,(lua_toboolean(
  2424. L,1)?"true":"false"));return 1;case LUA_TTABLE:sprintf(buff,"table: %p",
  2425. lua_topointer(L,1));break;case LUA_TFUNCTION:sprintf(buff,"function: %p",
  2426. lua_topointer(L,1));break;case LUA_TUSERDATA:case LUA_TLIGHTUSERDATA:sprintf(
  2427. buff,"userdata: %p",lua_touserdata(L,1));break;case LUA_TTHREAD:sprintf(buff,
  2428. "thread: %p",(void*)lua_tothread(L,1));break;case LUA_TNIL:lua_pushliteral(L,
  2429. "nil");return 1;}lua_pushstring(L,buff);return 1;}static int luaB_newproxy(
  2430. lua_State*L){lua_settop(L,1);lua_newuserdata(L,0);if(lua_toboolean(L,1)==0)
  2431. return 1;else if(lua_isboolean(L,1)){lua_newtable(L);lua_pushvalue(L,-1);
  2432. lua_pushboolean(L,1);lua_rawset(L,lua_upvalueindex(1));}else{int validproxy=0;
  2433. if(lua_getmetatable(L,1)){lua_rawget(L,lua_upvalueindex(1));validproxy=
  2434. lua_toboolean(L,-1);lua_pop(L,1);}luaL_argcheck(L,validproxy,1,
  2435. "boolean or proxy expected");lua_getmetatable(L,1);}lua_setmetatable(L,2);
  2436. return 1;}
  2437. #define REQTAB "_LOADED"
  2438. #define LUA_PATH "LUA_PATH"
  2439. #ifndef LUA_PATH_SEP
  2440. #define LUA_PATH_SEP ';'
  2441. #endif
  2442. #ifndef LUA_PATH_MARK
  2443. #define LUA_PATH_MARK '?'
  2444. #endif
  2445. #ifndef LUA_PATH_DEFAULT
  2446. #define LUA_PATH_DEFAULT "?;?.lua"
  2447. #endif
  2448. static const char*getpath(lua_State*L){const char*path;lua_getglobal(L,
  2449. LUA_PATH);path=lua_tostring(L,-1);lua_pop(L,1);if(path)return path;path=getenv
  2450. (LUA_PATH);if(path)return path;return LUA_PATH_DEFAULT;}static const char*
  2451. pushnextpath(lua_State*L,const char*path){const char*l;if(*path=='\0')return
  2452. NULL;if(*path==LUA_PATH_SEP)path++;l=strchr(path,LUA_PATH_SEP);if(l==NULL)l=
  2453. path+strlen(path);lua_pushlstring(L,path,l-path);return l;}static void
  2454. pushcomposename(lua_State*L){const char*path=lua_tostring(L,-1);const char*
  2455. wild;int n=1;while((wild=strchr(path,LUA_PATH_MARK))!=NULL){luaL_checkstack(L,
  2456. 3,"too many marks in a path component");lua_pushlstring(L,path,wild-path);
  2457. lua_pushvalue(L,1);path=wild+1;n+=2;}lua_pushstring(L,path);lua_concat(L,n);}
  2458. static int luaB_require(lua_State*L){const char*path;int status=LUA_ERRFILE;
  2459. luaL_checkstring(L,1);lua_settop(L,1);lua_getglobal(L,REQTAB);if(!lua_istable(
  2460. L,2))return luaL_error(L,"`"REQTAB"' is not a table");path=getpath(L);
  2461. lua_pushvalue(L,1);lua_rawget(L,2);if(lua_toboolean(L,-1))return 1;else{while(
  2462. status==LUA_ERRFILE){lua_settop(L,3);if((path=pushnextpath(L,path))==NULL)
  2463. break;pushcomposename(L);status=luaL_loadfile(L,lua_tostring(L,-1));}}switch(
  2464. status){case 0:{lua_getglobal(L,"_REQUIREDNAME");lua_insert(L,-2);
  2465. lua_pushvalue(L,1);lua_setglobal(L,"_REQUIREDNAME");lua_call(L,0,1);lua_insert
  2466. (L,-2);lua_setglobal(L,"_REQUIREDNAME");if(lua_isnil(L,-1)){lua_pushboolean(L,
  2467. 1);lua_replace(L,-2);}lua_pushvalue(L,1);lua_pushvalue(L,-2);lua_rawset(L,2);
  2468. return 1;}case LUA_ERRFILE:{return luaL_error(L,
  2469. "could not load package `%s' from path `%s'",lua_tostring(L,1),getpath(L));}
  2470. default:{return luaL_error(L,"error loading package `%s' (%s)",lua_tostring(L,
  2471. 1),lua_tostring(L,-1));}}}static const luaL_reg base_funcs[]={{"error",
  2472. luaB_error},{"getmetatable",luaB_getmetatable},{"setmetatable",
  2473. luaB_setmetatable},{"getfenv",luaB_getfenv},{"setfenv",luaB_setfenv},{"next",
  2474. luaB_next},{"ipairs",luaB_ipairs},{"pairs",luaB_pairs},{"print",luaB_print},{
  2475. "tonumber",luaB_tonumber},{"tostring",luaB_tostring},{"type",luaB_type},{
  2476. "assert",luaB_assert},{"unpack",luaB_unpack},{"rawequal",luaB_rawequal},{
  2477. "rawget",luaB_rawget},{"rawset",luaB_rawset},{"pcall",luaB_pcall},{"xpcall",
  2478. luaB_xpcall},{"collectgarbage",luaB_collectgarbage},{"gcinfo",luaB_gcinfo},{
  2479. "loadfile",luaB_loadfile},{"dofile",luaB_dofile},{"loadstring",luaB_loadstring
  2480. },{"require",luaB_require},{NULL,NULL}};static int auxresume(lua_State*L,
  2481. lua_State*co,int narg){int status;if(!lua_checkstack(co,narg))luaL_error(L,
  2482. "too many arguments to resume");lua_xmove(L,co,narg);status=lua_resume(co,narg
  2483. );if(status==0){int nres=lua_gettop(co);if(!lua_checkstack(L,nres))luaL_error(
  2484. L,"too many results to resume");lua_xmove(co,L,nres);return nres;}else{
  2485. lua_xmove(co,L,1);return-1;}}static int luaB_coresume(lua_State*L){lua_State*
  2486. co=lua_tothread(L,1);int r;luaL_argcheck(L,co,1,"coroutine expected");r=
  2487. auxresume(L,co,lua_gettop(L)-1);if(r<0){lua_pushboolean(L,0);lua_insert(L,-2);
  2488. return 2;}else{lua_pushboolean(L,1);lua_insert(L,-(r+1));return r+1;}}static
  2489. int luaB_auxwrap(lua_State*L){lua_State*co=lua_tothread(L,lua_upvalueindex(1))
  2490. ;int r=auxresume(L,co,lua_gettop(L));if(r<0){if(lua_isstring(L,-1)){luaL_where
  2491. (L,1);lua_insert(L,-2);lua_concat(L,2);}lua_error(L);}return r;}static int
  2492. luaB_cocreate(lua_State*L){lua_State*NL=lua_newthread(L);luaL_argcheck(L,
  2493. lua_isfunction(L,1)&&!lua_iscfunction(L,1),1,"Lua function expected");
  2494. lua_pushvalue(L,1);lua_xmove(L,NL,1);return 1;}static int luaB_cowrap(
  2495. lua_State*L){luaB_cocreate(L);lua_pushcclosure(L,luaB_auxwrap,1);return 1;}
  2496. static int luaB_yield(lua_State*L){return lua_yield(L,lua_gettop(L));}static
  2497. int luaB_costatus(lua_State*L){lua_State*co=lua_tothread(L,1);luaL_argcheck(L,
  2498. co,1,"coroutine expected");if(L==co)lua_pushliteral(L,"running");else{
  2499. lua_Debug ar;if(lua_getstack(co,0,&ar)==0&&lua_gettop(co)==0)lua_pushliteral(L
  2500. ,"dead");else lua_pushliteral(L,"suspended");}return 1;}static const luaL_reg
  2501. co_funcs[]={{"create",luaB_cocreate},{"wrap",luaB_cowrap},{"resume",
  2502. luaB_coresume},{"yield",luaB_yield},{"status",luaB_costatus},{NULL,NULL}};
  2503. static void base_open(lua_State*L){lua_pushliteral(L,"_G");lua_pushvalue(L,
  2504. LUA_GLOBALSINDEX);luaL_openlib(L,NULL,base_funcs,0);lua_pushliteral(L,
  2505. "_VERSION");lua_pushliteral(L,LUA_VERSION);lua_rawset(L,-3);lua_pushliteral(L,
  2506. "newproxy");lua_newtable(L);lua_pushvalue(L,-1);lua_setmetatable(L,-2);
  2507. lua_pushliteral(L,"__mode");lua_pushliteral(L,"k");lua_rawset(L,-3);
  2508. lua_pushcclosure(L,luaB_newproxy,1);lua_rawset(L,-3);lua_rawset(L,-1);}
  2509. LUALIB_API int luaopen_base(lua_State*L){base_open(L);luaL_openlib(L,
  2510. LUA_COLIBNAME,co_funcs,0);lua_newtable(L);lua_setglobal(L,REQTAB);return 0;}
  2511. #line 1 "lcode.c"
  2512. #define lcode_c
  2513. #line 1 "lcode.h"
  2514. #ifndef lcode_h
  2515. #define lcode_h
  2516. #line 1 "llex.h"
  2517. #ifndef llex_h
  2518. #define llex_h
  2519. #define FIRST_RESERVED 257
  2520. #define TOKEN_LEN (sizeof("function")/sizeof(char))
  2521. enum RESERVED{TK_AND=FIRST_RESERVED,TK_BREAK,TK_DO,TK_ELSE,TK_ELSEIF,TK_END,
  2522. TK_FALSE,TK_FOR,TK_FUNCTION,TK_IF,TK_IN,TK_LOCAL,TK_NIL,TK_NOT,TK_OR,TK_REPEAT
  2523. ,TK_RETURN,TK_THEN,TK_TRUE,TK_UNTIL,TK_WHILE,TK_NAME,TK_CONCAT,TK_DOTS,TK_EQ,
  2524. TK_GE,TK_LE,TK_NE,TK_NUMBER,TK_STRING,TK_EOS};
  2525. #define NUM_RESERVED (cast(int,TK_WHILE-FIRST_RESERVED+1))
  2526. typedef union{lua_Number r;TString*ts;}SemInfo;typedef struct Token{int token;
  2527. SemInfo seminfo;}Token;typedef struct LexState{int current;int linenumber;int
  2528. lastline;Token t;Token lookahead;struct FuncState*fs;struct lua_State*L;ZIO*z;
  2529. Mbuffer*buff;TString*source;int nestlevel;}LexState;void luaX_init(lua_State*L
  2530. );void luaX_setinput(lua_State*L,LexState*LS,ZIO*z,TString*source);int
  2531. luaX_lex(LexState*LS,SemInfo*seminfo);void luaX_checklimit(LexState*ls,int val
  2532. ,int limit,const char*msg);void luaX_syntaxerror(LexState*ls,const char*s);
  2533. void luaX_errorline(LexState*ls,const char*s,const char*token,int line);const
  2534. char*luaX_token2str(LexState*ls,int token);
  2535. #endif
  2536. #line 11 "lcode.h"
  2537. #line 1 "lopcodes.h"
  2538. #ifndef lopcodes_h
  2539. #define lopcodes_h
  2540. enum OpMode{iABC,iABx,iAsBx};
  2541. #define SIZE_C 9
  2542. #define SIZE_B 9
  2543. #define SIZE_Bx (SIZE_C+SIZE_B)
  2544. #define SIZE_A 8
  2545. #define SIZE_OP 6
  2546. #define POS_C SIZE_OP
  2547. #define POS_B (POS_C+SIZE_C)
  2548. #define POS_Bx POS_C
  2549. #define POS_A (POS_B+SIZE_B)
  2550. #if SIZE_Bx <BITS_INT-1
  2551. #define MAXARG_Bx ((1<<SIZE_Bx)-1)
  2552. #define MAXARG_sBx (MAXARG_Bx>>1)
  2553. #else
  2554. #define MAXARG_Bx MAX_INT
  2555. #define MAXARG_sBx MAX_INT
  2556. #endif
  2557. #define MAXARG_A ((1<<SIZE_A)-1)
  2558. #define MAXARG_B ((1<<SIZE_B)-1)
  2559. #define MAXARG_C ((1<<SIZE_C)-1)
  2560. #define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
  2561. #define MASK0(n,p) (~MASK1(n,p))
  2562. #define GET_OPCODE(i) (cast(OpCode,(i)&MASK1(SIZE_OP,0)))
  2563. #define SET_OPCODE(i,o) ((i)=(((i)&MASK0(SIZE_OP,0))|cast(Instruction,o)))
  2564. #define GETARG_A(i) (cast(int,(i)>>POS_A))
  2565. #define SETARG_A(i,u) ((i)=(((i)&MASK0(SIZE_A,POS_A))|((cast(Instruction,u)<<\
  2566. POS_A)&MASK1(SIZE_A,POS_A))))
  2567. #define GETARG_B(i) (cast(int,((i)>>POS_B)&MASK1(SIZE_B,0)))
  2568. #define SETARG_B(i,b) ((i)=(((i)&MASK0(SIZE_B,POS_B))|((cast(Instruction,b)<<\
  2569. POS_B)&MASK1(SIZE_B,POS_B))))
  2570. #define GETARG_C(i) (cast(int,((i)>>POS_C)&MASK1(SIZE_C,0)))
  2571. #define SETARG_C(i,b) ((i)=(((i)&MASK0(SIZE_C,POS_C))|((cast(Instruction,b)<<\
  2572. POS_C)&MASK1(SIZE_C,POS_C))))
  2573. #define GETARG_Bx(i) (cast(int,((i)>>POS_Bx)&MASK1(SIZE_Bx,0)))
  2574. #define SETARG_Bx(i,b) ((i)=(((i)&MASK0(SIZE_Bx,POS_Bx))|((cast(Instruction,b)\
  2575. <<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))
  2576. #define GETARG_sBx(i) (GETARG_Bx(i)-MAXARG_sBx)
  2577. #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int,(b)+MAXARG_sBx))
  2578. #define CREATE_ABC(o,a,b,c) (cast(Instruction,o)|(cast(Instruction,a)<<POS_A)|\
  2579. (cast(Instruction,b)<<POS_B)|(cast(Instruction,c)<<POS_C))
  2580. #define CREATE_ABx(o,a,bc) (cast(Instruction,o)|(cast(Instruction,a)<<POS_A)|(\
  2581. cast(Instruction,bc)<<POS_Bx))
  2582. #define NO_REG MAXARG_A
  2583. typedef enum{OP_MOVE,OP_LOADK,OP_LOADBOOL,OP_LOADNIL,OP_GETUPVAL,OP_GETGLOBAL,
  2584. OP_GETTABLE,OP_SETGLOBAL,OP_SETUPVAL,OP_SETTABLE,OP_NEWTABLE,OP_SELF,OP_ADD,
  2585. OP_SUB,OP_MUL,OP_DIV,OP_POW,OP_UNM,OP_NOT,OP_CONCAT,OP_JMP,OP_EQ,OP_LT,OP_LE,
  2586. OP_TEST,OP_CALL,OP_TAILCALL,OP_RETURN,OP_FORLOOP,OP_TFORLOOP,OP_TFORPREP,
  2587. OP_SETLIST,OP_SETLISTO,OP_CLOSE,OP_CLOSURE}OpCode;
  2588. #define NUM_OPCODES (cast(int,OP_CLOSURE+1))
  2589. enum OpModeMask{OpModeBreg=2,OpModeBrk,OpModeCrk,OpModesetA,OpModeK,OpModeT};
  2590. extern const lu_byte luaP_opmodes[NUM_OPCODES];
  2591. #define getOpMode(m) (cast(enum OpMode,luaP_opmodes[m]&3))
  2592. #define testOpMode(m, b)(luaP_opmodes[m]&(1<<(b)))
  2593. #ifdef LUA_OPNAMES
  2594. extern const char*const luaP_opnames[];
  2595. #endif
  2596. #define LFIELDS_PER_FLUSH 32
  2597. #endif
  2598. #line 13 "lcode.h"
  2599. #line 1 "lparser.h"
  2600. #ifndef lparser_h
  2601. #define lparser_h
  2602. typedef enum{VVOID,VNIL,VTRUE,VFALSE,VK,VLOCAL,VUPVAL,VGLOBAL,VINDEXED,VJMP,
  2603. VRELOCABLE,VNONRELOC,VCALL}expkind;typedef struct expdesc{expkind k;int info,
  2604. aux;int t;int f;}expdesc;struct BlockCnt;typedef struct FuncState{Proto*f;
  2605. Table*h;struct FuncState*prev;struct LexState*ls;struct lua_State*L;struct
  2606. BlockCnt*bl;int pc;int lasttarget;int jpc;int freereg;int nk;int np;int
  2607. nlocvars;int nactvar;expdesc upvalues[MAXUPVALUES];int actvar[MAXVARS];}
  2608. FuncState;Proto*luaY_parser(lua_State*L,ZIO*z,Mbuffer*buff);
  2609. #endif
  2610. #line 14 "lcode.h"
  2611. #define NO_JUMP (-1)
  2612. typedef enum BinOpr{OPR_ADD,OPR_SUB,OPR_MULT,OPR_DIV,OPR_POW,OPR_CONCAT,OPR_NE
  2613. ,OPR_EQ,OPR_LT,OPR_LE,OPR_GT,OPR_GE,OPR_AND,OPR_OR,OPR_NOBINOPR}BinOpr;
  2614. #define binopistest(op) ((op)>=OPR_NE)
  2615. typedef enum UnOpr{OPR_MINUS,OPR_NOT,OPR_NOUNOPR}UnOpr;
  2616. #define getcode(fs,e) ((fs)->f->code[(e)->info])
  2617. #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)
  2618. int luaK_code(FuncState*fs,Instruction i,int line);int luaK_codeABx(FuncState*
  2619. fs,OpCode o,int A,unsigned int Bx);int luaK_codeABC(FuncState*fs,OpCode o,int
  2620. A,int B,int C);void luaK_fixline(FuncState*fs,int line);void luaK_nil(
  2621. FuncState*fs,int from,int n);void luaK_reserveregs(FuncState*fs,int n);void
  2622. luaK_checkstack(FuncState*fs,int n);int luaK_stringK(FuncState*fs,TString*s);
  2623. int luaK_numberK(FuncState*fs,lua_Number r);void luaK_dischargevars(FuncState*
  2624. fs,expdesc*e);int luaK_exp2anyreg(FuncState*fs,expdesc*e);void
  2625. luaK_exp2nextreg(FuncState*fs,expdesc*e);void luaK_exp2val(FuncState*fs,
  2626. expdesc*e);int luaK_exp2RK(FuncState*fs,expdesc*e);void luaK_self(FuncState*fs
  2627. ,expdesc*e,expdesc*key);void luaK_indexed(FuncState*fs,expdesc*t,expdesc*k);
  2628. void luaK_goiftrue(FuncState*fs,expdesc*e);void luaK_goiffalse(FuncState*fs,
  2629. expdesc*e);void luaK_storevar(FuncState*fs,expdesc*var,expdesc*e);void
  2630. luaK_setcallreturns(FuncState*fs,expdesc*var,int nresults);int luaK_jump(
  2631. FuncState*fs);void luaK_patchlist(FuncState*fs,int list,int target);void
  2632. luaK_patchtohere(FuncState*fs,int list);void luaK_concat(FuncState*fs,int*l1,
  2633. int l2);int luaK_getlabel(FuncState*fs);void luaK_prefix(FuncState*fs,UnOpr op
  2634. ,expdesc*v);void luaK_infix(FuncState*fs,BinOpr op,expdesc*v);void luaK_posfix
  2635. (FuncState*fs,BinOpr op,expdesc*v1,expdesc*v2);
  2636. #endif
  2637. #line 15 "lcode.c"
  2638. #define hasjumps(e) ((e)->t!=(e)->f)
  2639. void luaK_nil(FuncState*fs,int from,int n){Instruction*previous;if(fs->pc>fs->
  2640. lasttarget&&GET_OPCODE(*(previous=&fs->f->code[fs->pc-1]))==OP_LOADNIL){int
  2641. pfrom=GETARG_A(*previous);int pto=GETARG_B(*previous);if(pfrom<=from&&from<=
  2642. pto+1){if(from+n-1>pto)SETARG_B(*previous,from+n-1);return;}}luaK_codeABC(fs,
  2643. OP_LOADNIL,from,from+n-1,0);}int luaK_jump(FuncState*fs){int jpc=fs->jpc;int j
  2644. ;fs->jpc=NO_JUMP;j=luaK_codeAsBx(fs,OP_JMP,0,NO_JUMP);luaK_concat(fs,&j,jpc);
  2645. return j;}static int luaK_condjump(FuncState*fs,OpCode op,int A,int B,int C){
  2646. luaK_codeABC(fs,op,A,B,C);return luaK_jump(fs);}static void luaK_fixjump(
  2647. FuncState*fs,int pc,int dest){Instruction*jmp=&fs->f->code[pc];int offset=dest
  2648. -(pc+1);lua_assert(dest!=NO_JUMP);if(abs(offset)>MAXARG_sBx)luaX_syntaxerror(
  2649. fs->ls,"control structure too long");SETARG_sBx(*jmp,offset);}int
  2650. luaK_getlabel(FuncState*fs){fs->lasttarget=fs->pc;return fs->pc;}static int
  2651. luaK_getjump(FuncState*fs,int pc){int offset=GETARG_sBx(fs->f->code[pc]);if(
  2652. offset==NO_JUMP)return NO_JUMP;else return(pc+1)+offset;}static Instruction*
  2653. getjumpcontrol(FuncState*fs,int pc){Instruction*pi=&fs->f->code[pc];if(pc>=1&&
  2654. testOpMode(GET_OPCODE(*(pi-1)),OpModeT))return pi-1;else return pi;}static int
  2655. need_value(FuncState*fs,int list,int cond){for(;list!=NO_JUMP;list=
  2656. luaK_getjump(fs,list)){Instruction i=*getjumpcontrol(fs,list);if(GET_OPCODE(i)
  2657. !=OP_TEST||GETARG_C(i)!=cond)return 1;}return 0;}static void patchtestreg(
  2658. Instruction*i,int reg){if(reg==NO_REG)reg=GETARG_B(*i);SETARG_A(*i,reg);}
  2659. static void luaK_patchlistaux(FuncState*fs,int list,int ttarget,int treg,int
  2660. ftarget,int freg,int dtarget){while(list!=NO_JUMP){int next=luaK_getjump(fs,
  2661. list);Instruction*i=getjumpcontrol(fs,list);if(GET_OPCODE(*i)!=OP_TEST){
  2662. lua_assert(dtarget!=NO_JUMP);luaK_fixjump(fs,list,dtarget);}else{if(GETARG_C(*
  2663. i)){lua_assert(ttarget!=NO_JUMP);patchtestreg(i,treg);luaK_fixjump(fs,list,
  2664. ttarget);}else{lua_assert(ftarget!=NO_JUMP);patchtestreg(i,freg);luaK_fixjump(
  2665. fs,list,ftarget);}}list=next;}}static void luaK_dischargejpc(FuncState*fs){
  2666. luaK_patchlistaux(fs,fs->jpc,fs->pc,NO_REG,fs->pc,NO_REG,fs->pc);fs->jpc=
  2667. NO_JUMP;}void luaK_patchlist(FuncState*fs,int list,int target){if(target==fs->
  2668. pc)luaK_patchtohere(fs,list);else{lua_assert(target<fs->pc);luaK_patchlistaux(
  2669. fs,list,target,NO_REG,target,NO_REG,target);}}void luaK_patchtohere(FuncState*
  2670. fs,int list){luaK_getlabel(fs);luaK_concat(fs,&fs->jpc,list);}void luaK_concat
  2671. (FuncState*fs,int*l1,int l2){if(l2==NO_JUMP)return;else if(*l1==NO_JUMP)*l1=l2
  2672. ;else{int list=*l1;int next;while((next=luaK_getjump(fs,list))!=NO_JUMP)list=
  2673. next;luaK_fixjump(fs,list,l2);}}void luaK_checkstack(FuncState*fs,int n){int
  2674. newstack=fs->freereg+n;if(newstack>fs->f->maxstacksize){if(newstack>=MAXSTACK)
  2675. luaX_syntaxerror(fs->ls,"function or expression too complex");fs->f->
  2676. maxstacksize=cast(lu_byte,newstack);}}void luaK_reserveregs(FuncState*fs,int n
  2677. ){luaK_checkstack(fs,n);fs->freereg+=n;}static void freereg(FuncState*fs,int
  2678. reg){if(reg>=fs->nactvar&&reg<MAXSTACK){fs->freereg--;lua_assert(reg==fs->
  2679. freereg);}}static void freeexp(FuncState*fs,expdesc*e){if(e->k==VNONRELOC)
  2680. freereg(fs,e->info);}static int addk(FuncState*fs,TObject*k,TObject*v){const
  2681. TObject*idx=luaH_get(fs->h,k);if(ttisnumber(idx)){lua_assert(luaO_rawequalObj(
  2682. &fs->f->k[cast(int,nvalue(idx))],v));return cast(int,nvalue(idx));}else{Proto*
  2683. f=fs->f;luaM_growvector(fs->L,f->k,fs->nk,f->sizek,TObject,MAXARG_Bx,
  2684. "constant table overflow");setobj2n(&f->k[fs->nk],v);setnvalue(luaH_set(fs->L,
  2685. fs->h,k),cast(lua_Number,fs->nk));return fs->nk++;}}int luaK_stringK(FuncState
  2686. *fs,TString*s){TObject o;setsvalue(&o,s);return addk(fs,&o,&o);}int
  2687. luaK_numberK(FuncState*fs,lua_Number r){TObject o;setnvalue(&o,r);return addk(
  2688. fs,&o,&o);}static int nil_constant(FuncState*fs){TObject k,v;setnilvalue(&v);
  2689. sethvalue(&k,fs->h);return addk(fs,&k,&v);}void luaK_setcallreturns(FuncState*
  2690. fs,expdesc*e,int nresults){if(e->k==VCALL){SETARG_C(getcode(fs,e),nresults+1);
  2691. if(nresults==1){e->k=VNONRELOC;e->info=GETARG_A(getcode(fs,e));}}}void
  2692. luaK_dischargevars(FuncState*fs,expdesc*e){switch(e->k){case VLOCAL:{e->k=
  2693. VNONRELOC;break;}case VUPVAL:{e->info=luaK_codeABC(fs,OP_GETUPVAL,0,e->info,0)
  2694. ;e->k=VRELOCABLE;break;}case VGLOBAL:{e->info=luaK_codeABx(fs,OP_GETGLOBAL,0,e
  2695. ->info);e->k=VRELOCABLE;break;}case VINDEXED:{freereg(fs,e->aux);freereg(fs,e
  2696. ->info);e->info=luaK_codeABC(fs,OP_GETTABLE,0,e->info,e->aux);e->k=VRELOCABLE;
  2697. break;}case VCALL:{luaK_setcallreturns(fs,e,1);break;}default:break;}}static
  2698. int code_label(FuncState*fs,int A,int b,int jump){luaK_getlabel(fs);return
  2699. luaK_codeABC(fs,OP_LOADBOOL,A,b,jump);}static void discharge2reg(FuncState*fs,
  2700. expdesc*e,int reg){luaK_dischargevars(fs,e);switch(e->k){case VNIL:{luaK_nil(
  2701. fs,reg,1);break;}case VFALSE:case VTRUE:{luaK_codeABC(fs,OP_LOADBOOL,reg,e->k
  2702. ==VTRUE,0);break;}case VK:{luaK_codeABx(fs,OP_LOADK,reg,e->info);break;}case
  2703. VRELOCABLE:{Instruction*pc=&getcode(fs,e);SETARG_A(*pc,reg);break;}case
  2704. VNONRELOC:{if(reg!=e->info)luaK_codeABC(fs,OP_MOVE,reg,e->info,0);break;}
  2705. default:{lua_assert(e->k==VVOID||e->k==VJMP);return;}}e->info=reg;e->k=
  2706. VNONRELOC;}static void discharge2anyreg(FuncState*fs,expdesc*e){if(e->k!=
  2707. VNONRELOC){luaK_reserveregs(fs,1);discharge2reg(fs,e,fs->freereg-1);}}static
  2708. void luaK_exp2reg(FuncState*fs,expdesc*e,int reg){discharge2reg(fs,e,reg);if(e
  2709. ->k==VJMP)luaK_concat(fs,&e->t,e->info);if(hasjumps(e)){int final;int p_f=
  2710. NO_JUMP;int p_t=NO_JUMP;if(need_value(fs,e->t,1)||need_value(fs,e->f,0)){int
  2711. fj=NO_JUMP;if(e->k!=VJMP)fj=luaK_jump(fs);p_f=code_label(fs,reg,0,1);p_t=
  2712. code_label(fs,reg,1,0);luaK_patchtohere(fs,fj);}final=luaK_getlabel(fs);
  2713. luaK_patchlistaux(fs,e->f,p_f,NO_REG,final,reg,p_f);luaK_patchlistaux(fs,e->t,
  2714. final,reg,p_t,NO_REG,p_t);}e->f=e->t=NO_JUMP;e->info=reg;e->k=VNONRELOC;}void
  2715. luaK_exp2nextreg(FuncState*fs,expdesc*e){luaK_dischargevars(fs,e);freeexp(fs,e
  2716. );luaK_reserveregs(fs,1);luaK_exp2reg(fs,e,fs->freereg-1);}int luaK_exp2anyreg
  2717. (FuncState*fs,expdesc*e){luaK_dischargevars(fs,e);if(e->k==VNONRELOC){if(!
  2718. hasjumps(e))return e->info;if(e->info>=fs->nactvar){luaK_exp2reg(fs,e,e->info)
  2719. ;return e->info;}}luaK_exp2nextreg(fs,e);return e->info;}void luaK_exp2val(
  2720. FuncState*fs,expdesc*e){if(hasjumps(e))luaK_exp2anyreg(fs,e);else
  2721. luaK_dischargevars(fs,e);}int luaK_exp2RK(FuncState*fs,expdesc*e){luaK_exp2val
  2722. (fs,e);switch(e->k){case VNIL:{if(fs->nk+MAXSTACK<=MAXARG_C){e->info=
  2723. nil_constant(fs);e->k=VK;return e->info+MAXSTACK;}else break;}case VK:{if(e->
  2724. info+MAXSTACK<=MAXARG_C)return e->info+MAXSTACK;else break;}default:break;}
  2725. return luaK_exp2anyreg(fs,e);}void luaK_storevar(FuncState*fs,expdesc*var,
  2726. expdesc*exp){switch(var->k){case VLOCAL:{freeexp(fs,exp);luaK_exp2reg(fs,exp,
  2727. var->info);return;}case VUPVAL:{int e=luaK_exp2anyreg(fs,exp);luaK_codeABC(fs,
  2728. OP_SETUPVAL,e,var->info,0);break;}case VGLOBAL:{int e=luaK_exp2anyreg(fs,exp);
  2729. luaK_codeABx(fs,OP_SETGLOBAL,e,var->info);break;}case VINDEXED:{int e=
  2730. luaK_exp2RK(fs,exp);luaK_codeABC(fs,OP_SETTABLE,var->info,var->aux,e);break;}
  2731. default:{lua_assert(0);break;}}freeexp(fs,exp);}void luaK_self(FuncState*fs,
  2732. expdesc*e,expdesc*key){int func;luaK_exp2anyreg(fs,e);freeexp(fs,e);func=fs->
  2733. freereg;luaK_reserveregs(fs,2);luaK_codeABC(fs,OP_SELF,func,e->info,
  2734. luaK_exp2RK(fs,key));freeexp(fs,key);e->info=func;e->k=VNONRELOC;}static void
  2735. invertjump(FuncState*fs,expdesc*e){Instruction*pc=getjumpcontrol(fs,e->info);
  2736. lua_assert(testOpMode(GET_OPCODE(*pc),OpModeT)&&GET_OPCODE(*pc)!=OP_TEST);
  2737. SETARG_A(*pc,!(GETARG_A(*pc)));}static int jumponcond(FuncState*fs,expdesc*e,
  2738. int cond){if(e->k==VRELOCABLE){Instruction ie=getcode(fs,e);if(GET_OPCODE(ie)
  2739. ==OP_NOT){fs->pc--;return luaK_condjump(fs,OP_TEST,NO_REG,GETARG_B(ie),!cond);
  2740. }}discharge2anyreg(fs,e);freeexp(fs,e);return luaK_condjump(fs,OP_TEST,NO_REG,
  2741. e->info,cond);}void luaK_goiftrue(FuncState*fs,expdesc*e){int pc;
  2742. luaK_dischargevars(fs,e);switch(e->k){case VK:case VTRUE:{pc=NO_JUMP;break;}
  2743. case VFALSE:{pc=luaK_jump(fs);break;}case VJMP:{invertjump(fs,e);pc=e->info;
  2744. break;}default:{pc=jumponcond(fs,e,0);break;}}luaK_concat(fs,&e->f,pc);}void
  2745. luaK_goiffalse(FuncState*fs,expdesc*e){int pc;luaK_dischargevars(fs,e);switch(
  2746. e->k){case VNIL:case VFALSE:{pc=NO_JUMP;break;}case VTRUE:{pc=luaK_jump(fs);
  2747. break;}case VJMP:{pc=e->info;break;}default:{pc=jumponcond(fs,e,1);break;}}
  2748. luaK_concat(fs,&e->t,pc);}static void codenot(FuncState*fs,expdesc*e){
  2749. luaK_dischargevars(fs,e);switch(e->k){case VNIL:case VFALSE:{e->k=VTRUE;break;
  2750. }case VK:case VTRUE:{e->k=VFALSE;break;}case VJMP:{invertjump(fs,e);break;}
  2751. case VRELOCABLE:case VNONRELOC:{discharge2anyreg(fs,e);freeexp(fs,e);e->info=
  2752. luaK_codeABC(fs,OP_NOT,0,e->info,0);e->k=VRELOCABLE;break;}default:{lua_assert
  2753. (0);break;}}{int temp=e->f;e->f=e->t;e->t=temp;}}void luaK_indexed(FuncState*
  2754. fs,expdesc*t,expdesc*k){t->aux=luaK_exp2RK(fs,k);t->k=VINDEXED;}void
  2755. luaK_prefix(FuncState*fs,UnOpr op,expdesc*e){if(op==OPR_MINUS){luaK_exp2val(fs
  2756. ,e);if(e->k==VK&&ttisnumber(&fs->f->k[e->info]))e->info=luaK_numberK(fs,-
  2757. nvalue(&fs->f->k[e->info]));else{luaK_exp2anyreg(fs,e);freeexp(fs,e);e->info=
  2758. luaK_codeABC(fs,OP_UNM,0,e->info,0);e->k=VRELOCABLE;}}else codenot(fs,e);}void
  2759. luaK_infix(FuncState*fs,BinOpr op,expdesc*v){switch(op){case OPR_AND:{
  2760. luaK_goiftrue(fs,v);luaK_patchtohere(fs,v->t);v->t=NO_JUMP;break;}case OPR_OR:
  2761. {luaK_goiffalse(fs,v);luaK_patchtohere(fs,v->f);v->f=NO_JUMP;break;}case
  2762. OPR_CONCAT:{luaK_exp2nextreg(fs,v);break;}default:{luaK_exp2RK(fs,v);break;}}}
  2763. static void codebinop(FuncState*fs,expdesc*res,BinOpr op,int o1,int o2){if(op
  2764. <=OPR_POW){OpCode opc=cast(OpCode,(op-OPR_ADD)+OP_ADD);res->info=luaK_codeABC(
  2765. fs,opc,0,o1,o2);res->k=VRELOCABLE;}else{static const OpCode ops[]={OP_EQ,OP_EQ
  2766. ,OP_LT,OP_LE,OP_LT,OP_LE};int cond=1;if(op>=OPR_GT){int temp;temp=o1;o1=o2;o2=
  2767. temp;}else if(op==OPR_NE)cond=0;res->info=luaK_condjump(fs,ops[op-OPR_NE],cond
  2768. ,o1,o2);res->k=VJMP;}}void luaK_posfix(FuncState*fs,BinOpr op,expdesc*e1,
  2769. expdesc*e2){switch(op){case OPR_AND:{lua_assert(e1->t==NO_JUMP);
  2770. luaK_dischargevars(fs,e2);luaK_concat(fs,&e1->f,e2->f);e1->k=e2->k;e1->info=e2
  2771. ->info;e1->aux=e2->aux;e1->t=e2->t;break;}case OPR_OR:{lua_assert(e1->f==
  2772. NO_JUMP);luaK_dischargevars(fs,e2);luaK_concat(fs,&e1->t,e2->t);e1->k=e2->k;e1
  2773. ->info=e2->info;e1->aux=e2->aux;e1->f=e2->f;break;}case OPR_CONCAT:{
  2774. luaK_exp2val(fs,e2);if(e2->k==VRELOCABLE&&GET_OPCODE(getcode(fs,e2))==
  2775. OP_CONCAT){lua_assert(e1->info==GETARG_B(getcode(fs,e2))-1);freeexp(fs,e1);
  2776. SETARG_B(getcode(fs,e2),e1->info);e1->k=e2->k;e1->info=e2->info;}else{
  2777. luaK_exp2nextreg(fs,e2);freeexp(fs,e2);freeexp(fs,e1);e1->info=luaK_codeABC(fs
  2778. ,OP_CONCAT,0,e1->info,e2->info);e1->k=VRELOCABLE;}break;}default:{int o1=
  2779. luaK_exp2RK(fs,e1);int o2=luaK_exp2RK(fs,e2);freeexp(fs,e2);freeexp(fs,e1);
  2780. codebinop(fs,e1,op,o1,o2);}}}void luaK_fixline(FuncState*fs,int line){fs->f->
  2781. lineinfo[fs->pc-1]=line;}int luaK_code(FuncState*fs,Instruction i,int line){
  2782. Proto*f=fs->f;luaK_dischargejpc(fs);luaM_growvector(fs->L,f->code,fs->pc,f->
  2783. sizecode,Instruction,MAX_INT,"code size overflow");f->code[fs->pc]=i;
  2784. luaM_growvector(fs->L,f->lineinfo,fs->pc,f->sizelineinfo,int,MAX_INT,
  2785. "code size overflow");f->lineinfo[fs->pc]=line;return fs->pc++;}int
  2786. luaK_codeABC(FuncState*fs,OpCode o,int a,int b,int c){lua_assert(getOpMode(o)
  2787. ==iABC);return luaK_code(fs,CREATE_ABC(o,a,b,c),fs->ls->lastline);}int
  2788. luaK_codeABx(FuncState*fs,OpCode o,int a,unsigned int bc){lua_assert(getOpMode
  2789. (o)==iABx||getOpMode(o)==iAsBx);return luaK_code(fs,CREATE_ABx(o,a,bc),fs->ls
  2790. ->lastline);}
  2791. #line 1 "ldblib.c"
  2792. #define ldblib_c
  2793. static void settabss(lua_State*L,const char*i,const char*v){lua_pushstring(L,i
  2794. );lua_pushstring(L,v);lua_rawset(L,-3);}static void settabsi(lua_State*L,const
  2795. char*i,int v){lua_pushstring(L,i);lua_pushnumber(L,(lua_Number)v);lua_rawset(
  2796. L,-3);}static int getinfo(lua_State*L){lua_Debug ar;const char*options=
  2797. luaL_optstring(L,2,"flnSu");if(lua_isnumber(L,1)){if(!lua_getstack(L,(int)(
  2798. lua_tonumber(L,1)),&ar)){lua_pushnil(L);return 1;}}else if(lua_isfunction(L,1)
  2799. ){lua_pushfstring(L,">%s",options);options=lua_tostring(L,-1);lua_pushvalue(L,
  2800. 1);}else return luaL_argerror(L,1,"function or level expected");if(!
  2801. lua_getinfo(L,options,&ar))return luaL_argerror(L,2,"invalid option");
  2802. lua_newtable(L);for(;*options;options++){switch(*options){case'S':settabss(L,
  2803. "source",ar.source);settabss(L,"short_src",ar.short_src);settabsi(L,
  2804. "linedefined",ar.linedefined);settabss(L,"what",ar.what);break;case'l':
  2805. settabsi(L,"currentline",ar.currentline);break;case'u':settabsi(L,"nups",ar.
  2806. nups);break;case'n':settabss(L,"name",ar.name);settabss(L,"namewhat",ar.
  2807. namewhat);break;case'f':lua_pushliteral(L,"func");lua_pushvalue(L,-3);
  2808. lua_rawset(L,-3);break;}}return 1;}static int getlocal(lua_State*L){lua_Debug
  2809. ar;const char*name;if(!lua_getstack(L,luaL_checkint(L,1),&ar))return
  2810. luaL_argerror(L,1,"level out of range");name=lua_getlocal(L,&ar,luaL_checkint(
  2811. L,2));if(name){lua_pushstring(L,name);lua_pushvalue(L,-2);return 2;}else{
  2812. lua_pushnil(L);return 1;}}static int setlocal(lua_State*L){lua_Debug ar;if(!
  2813. lua_getstack(L,luaL_checkint(L,1),&ar))return luaL_argerror(L,1,
  2814. "level out of range");luaL_checkany(L,3);lua_pushstring(L,lua_setlocal(L,&ar,
  2815. luaL_checkint(L,2)));return 1;}static int auxupvalue(lua_State*L,int get){
  2816. const char*name;int n=luaL_checkint(L,2);luaL_checktype(L,1,LUA_TFUNCTION);if(
  2817. lua_iscfunction(L,1))return 0;name=get?lua_getupvalue(L,1,n):lua_setupvalue(L,
  2818. 1,n);if(name==NULL)return 0;lua_pushstring(L,name);lua_insert(L,-(get+1));
  2819. return get+1;}static int getupvalue(lua_State*L){return auxupvalue(L,1);}
  2820. static int setupvalue(lua_State*L){luaL_checkany(L,3);return auxupvalue(L,0);}
  2821. static const char KEY_HOOK='h';static void hookf(lua_State*L,lua_Debug*ar){
  2822. static const char*const hooknames[]={"call","return","line","count",
  2823. "tail return"};lua_pushlightuserdata(L,(void*)&KEY_HOOK);lua_rawget(L,
  2824. LUA_REGISTRYINDEX);if(lua_isfunction(L,-1)){lua_pushstring(L,hooknames[(int)ar
  2825. ->event]);if(ar->currentline>=0)lua_pushnumber(L,(lua_Number)ar->currentline);
  2826. else lua_pushnil(L);lua_assert(lua_getinfo(L,"lS",ar));lua_call(L,2,0);}else
  2827. lua_pop(L,1);}static int makemask(const char*smask,int count){int mask=0;if(
  2828. strchr(smask,'c'))mask|=LUA_MASKCALL;if(strchr(smask,'r'))mask|=LUA_MASKRET;if
  2829. (strchr(smask,'l'))mask|=LUA_MASKLINE;if(count>0)mask|=LUA_MASKCOUNT;return
  2830. mask;}static char*unmakemask(int mask,char*smask){int i=0;if(mask&LUA_MASKCALL
  2831. )smask[i++]='c';if(mask&LUA_MASKRET)smask[i++]='r';if(mask&LUA_MASKLINE)smask[
  2832. i++]='l';smask[i]='\0';return smask;}static int sethook(lua_State*L){if(
  2833. lua_isnoneornil(L,1)){lua_settop(L,1);lua_sethook(L,NULL,0,0);}else{const char
  2834. *smask=luaL_checkstring(L,2);int count=luaL_optint(L,3,0);luaL_checktype(L,1,
  2835. LUA_TFUNCTION);lua_sethook(L,hookf,makemask(smask,count),count);}
  2836. lua_pushlightuserdata(L,(void*)&KEY_HOOK);lua_pushvalue(L,1);lua_rawset(L,
  2837. LUA_REGISTRYINDEX);return 0;}static int gethook(lua_State*L){char buff[5];int
  2838. mask=lua_gethookmask(L);lua_Hook hook=lua_gethook(L);if(hook!=NULL&&hook!=
  2839. hookf)lua_pushliteral(L,"external hook");else{lua_pushlightuserdata(L,(void*)&
  2840. KEY_HOOK);lua_rawget(L,LUA_REGISTRYINDEX);}lua_pushstring(L,unmakemask(mask,
  2841. buff));lua_pushnumber(L,(lua_Number)lua_gethookcount(L));return 3;}static int
  2842. debug(lua_State*L){for(;;){char buffer[250];fputs("lua_debug> ",stderr);if(
  2843. fgets(buffer,sizeof(buffer),stdin)==0||strcmp(buffer,"cont\n")==0)return 0;
  2844. lua_dostring(L,buffer);lua_settop(L,0);}}
  2845. #define LEVELS1 12
  2846. #define LEVELS2 10
  2847. static int errorfb(lua_State*L){int level=1;int firstpart=1;lua_Debug ar;if(
  2848. lua_gettop(L)==0)lua_pushliteral(L,"");else if(!lua_isstring(L,1))return 1;
  2849. else lua_pushliteral(L,"\n");lua_pushliteral(L,"stack traceback:");while(
  2850. lua_getstack(L,level++,&ar)){if(level>LEVELS1&&firstpart){if(!lua_getstack(L,
  2851. level+LEVELS2,&ar))level--;else{lua_pushliteral(L,"\n\t...");while(
  2852. lua_getstack(L,level+LEVELS2,&ar))level++;}firstpart=0;continue;}
  2853. lua_pushliteral(L,"\n\t");lua_getinfo(L,"Snl",&ar);lua_pushfstring(L,"%s:",ar.
  2854. short_src);if(ar.currentline>0)lua_pushfstring(L,"%d:",ar.currentline);switch(
  2855. *ar.namewhat){case'g':case'l':case'f':case'm':lua_pushfstring(L,
  2856. " in function `%s'",ar.name);break;default:{if(*ar.what=='m')lua_pushfstring(L
  2857. ," in main chunk");else if(*ar.what=='C'||*ar.what=='t')lua_pushliteral(L," ?"
  2858. );else lua_pushfstring(L," in function <%s:%d>",ar.short_src,ar.linedefined);}
  2859. }lua_concat(L,lua_gettop(L));}lua_concat(L,lua_gettop(L));return 1;}static
  2860. const luaL_reg dblib[]={{"getlocal",getlocal},{"getinfo",getinfo},{"gethook",
  2861. gethook},{"getupvalue",getupvalue},{"sethook",sethook},{"setlocal",setlocal},{
  2862. "setupvalue",setupvalue},{"debug",debug},{"traceback",errorfb},{NULL,NULL}};
  2863. LUALIB_API int luaopen_debug(lua_State*L){luaL_openlib(L,LUA_DBLIBNAME,dblib,0
  2864. );lua_pushliteral(L,"_TRACEBACK");lua_pushcfunction(L,errorfb);lua_settable(L,
  2865. LUA_GLOBALSINDEX);return 1;}
  2866. #line 1 "ldebug.c"
  2867. #define ldebug_c
  2868. static const char*getfuncname(CallInfo*ci,const char**name);
  2869. #define isLua(ci) (!((ci)->state&CI_C))
  2870. static int currentpc(CallInfo*ci){if(!isLua(ci))return-1;if(ci->state&
  2871. CI_HASFRAME)ci->u.l.savedpc=*ci->u.l.pc;return pcRel(ci->u.l.savedpc,ci_func(
  2872. ci)->l.p);}static int currentline(CallInfo*ci){int pc=currentpc(ci);if(pc<0)
  2873. return-1;else return getline(ci_func(ci)->l.p,pc);}void luaG_inithooks(
  2874. lua_State*L){CallInfo*ci;for(ci=L->ci;ci!=L->base_ci;ci--)currentpc(ci);L->
  2875. hookinit=1;}LUA_API int lua_sethook(lua_State*L,lua_Hook func,int mask,int
  2876. count){if(func==NULL||mask==0){mask=0;func=NULL;}L->hook=func;L->basehookcount
  2877. =count;resethookcount(L);L->hookmask=cast(lu_byte,mask);L->hookinit=0;return 1
  2878. ;}LUA_API lua_Hook lua_gethook(lua_State*L){return L->hook;}LUA_API int
  2879. lua_gethookmask(lua_State*L){return L->hookmask;}LUA_API int lua_gethookcount(
  2880. lua_State*L){return L->basehookcount;}LUA_API int lua_getstack(lua_State*L,int
  2881. level,lua_Debug*ar){int status;CallInfo*ci;lua_lock(L);for(ci=L->ci;level>0&&
  2882. ci>L->base_ci;ci--){level--;if(!(ci->state&CI_C))level-=ci->u.l.tailcalls;}if(
  2883. level>0||ci==L->base_ci)status=0;else if(level<0){status=1;ar->i_ci=0;}else{
  2884. status=1;ar->i_ci=ci-L->base_ci;}lua_unlock(L);return status;}static Proto*
  2885. getluaproto(CallInfo*ci){return(isLua(ci)?ci_func(ci)->l.p:NULL);}LUA_API
  2886. const char*lua_getlocal(lua_State*L,const lua_Debug*ar,int n){const char*name;
  2887. CallInfo*ci;Proto*fp;lua_lock(L);name=NULL;ci=L->base_ci+ar->i_ci;fp=
  2888. getluaproto(ci);if(fp){name=luaF_getlocalname(fp,n,currentpc(ci));if(name)
  2889. luaA_pushobject(L,ci->base+(n-1));}lua_unlock(L);return name;}LUA_API const
  2890. char*lua_setlocal(lua_State*L,const lua_Debug*ar,int n){const char*name;
  2891. CallInfo*ci;Proto*fp;lua_lock(L);name=NULL;ci=L->base_ci+ar->i_ci;fp=
  2892. getluaproto(ci);L->top--;if(fp){name=luaF_getlocalname(fp,n,currentpc(ci));if(
  2893. !name||name[0]=='(')name=NULL;else setobjs2s(ci->base+(n-1),L->top);}
  2894. lua_unlock(L);return name;}static void funcinfo(lua_Debug*ar,StkId func){
  2895. Closure*cl=clvalue(func);if(cl->c.isC){ar->source="=[C]";ar->linedefined=-1;ar
  2896. ->what="C";}else{ar->source=getstr(cl->l.p->source);ar->linedefined=cl->l.p->
  2897. lineDefined;ar->what=(ar->linedefined==0)?"main":"Lua";}luaO_chunkid(ar->
  2898. short_src,ar->source,LUA_IDSIZE);}static const char*travglobals(lua_State*L,
  2899. const TObject*o){Table*g=hvalue(gt(L));int i=sizenode(g);while(i--){Node*n=
  2900. gnode(g,i);if(luaO_rawequalObj(o,gval(n))&&ttisstring(gkey(n)))return getstr(
  2901. tsvalue(gkey(n)));}return NULL;}static void info_tailcall(lua_State*L,
  2902. lua_Debug*ar){ar->name=ar->namewhat="";ar->what="tail";ar->linedefined=ar->
  2903. currentline=-1;ar->source="=(tail call)";luaO_chunkid(ar->short_src,ar->source
  2904. ,LUA_IDSIZE);ar->nups=0;setnilvalue(L->top);}static int auxgetinfo(lua_State*L
  2905. ,const char*what,lua_Debug*ar,StkId f,CallInfo*ci){int status=1;for(;*what;
  2906. what++){switch(*what){case'S':{funcinfo(ar,f);break;}case'l':{ar->currentline=
  2907. (ci)?currentline(ci):-1;break;}case'u':{ar->nups=clvalue(f)->c.nupvalues;break
  2908. ;}case'n':{ar->namewhat=(ci)?getfuncname(ci,&ar->name):NULL;if(ar->namewhat==
  2909. NULL){if((ar->name=travglobals(L,f))!=NULL)ar->namewhat="global";else ar->
  2910. namewhat="";}break;}case'f':{setobj2s(L->top,f);break;}default:status=0;}}
  2911. return status;}LUA_API int lua_getinfo(lua_State*L,const char*what,lua_Debug*
  2912. ar){int status=1;lua_lock(L);if(*what=='>'){StkId f=L->top-1;if(!ttisfunction(
  2913. f))luaG_runerror(L,"value for `lua_getinfo' is not a function");status=
  2914. auxgetinfo(L,what+1,ar,f,NULL);L->top--;}else if(ar->i_ci!=0){CallInfo*ci=L->
  2915. base_ci+ar->i_ci;lua_assert(ttisfunction(ci->base-1));status=auxgetinfo(L,what
  2916. ,ar,ci->base-1,ci);}else info_tailcall(L,ar);if(strchr(what,'f'))incr_top(L);
  2917. lua_unlock(L);return status;}
  2918. #define check(x) if(!(x))return 0;
  2919. #define checkjump(pt,pc) check(0<=pc&&pc<pt->sizecode)
  2920. #define checkreg(pt,reg) check((reg)<(pt)->maxstacksize)
  2921. static int precheck(const Proto*pt){check(pt->maxstacksize<=MAXSTACK);check(pt
  2922. ->sizelineinfo==pt->sizecode||pt->sizelineinfo==0);lua_assert(pt->numparams+pt
  2923. ->is_vararg<=pt->maxstacksize);check(GET_OPCODE(pt->code[pt->sizecode-1])==
  2924. OP_RETURN);return 1;}static int checkopenop(const Proto*pt,int pc){Instruction
  2925. i=pt->code[pc+1];switch(GET_OPCODE(i)){case OP_CALL:case OP_TAILCALL:case
  2926. OP_RETURN:{check(GETARG_B(i)==0);return 1;}case OP_SETLISTO:return 1;default:
  2927. return 0;}}static int checkRK(const Proto*pt,int r){return(r<pt->maxstacksize
  2928. ||(r>=MAXSTACK&&r-MAXSTACK<pt->sizek));}static Instruction luaG_symbexec(const
  2929. Proto*pt,int lastpc,int reg){int pc;int last;last=pt->sizecode-1;check(
  2930. precheck(pt));for(pc=0;pc<lastpc;pc++){const Instruction i=pt->code[pc];OpCode
  2931. op=GET_OPCODE(i);int a=GETARG_A(i);int b=0;int c=0;checkreg(pt,a);switch(
  2932. getOpMode(op)){case iABC:{b=GETARG_B(i);c=GETARG_C(i);if(testOpMode(op,
  2933. OpModeBreg)){checkreg(pt,b);}else if(testOpMode(op,OpModeBrk))check(checkRK(pt
  2934. ,b));if(testOpMode(op,OpModeCrk))check(checkRK(pt,c));break;}case iABx:{b=
  2935. GETARG_Bx(i);if(testOpMode(op,OpModeK))check(b<pt->sizek);break;}case iAsBx:{b
  2936. =GETARG_sBx(i);break;}}if(testOpMode(op,OpModesetA)){if(a==reg)last=pc;}if(
  2937. testOpMode(op,OpModeT)){check(pc+2<pt->sizecode);check(GET_OPCODE(pt->code[pc+
  2938. 1])==OP_JMP);}switch(op){case OP_LOADBOOL:{check(c==0||pc+2<pt->sizecode);
  2939. break;}case OP_LOADNIL:{if(a<=reg&&reg<=b)last=pc;break;}case OP_GETUPVAL:case
  2940. OP_SETUPVAL:{check(b<pt->nups);break;}case OP_GETGLOBAL:case OP_SETGLOBAL:{
  2941. check(ttisstring(&pt->k[b]));break;}case OP_SELF:{checkreg(pt,a+1);if(reg==a+1
  2942. )last=pc;break;}case OP_CONCAT:{check(c<MAXSTACK&&b<c);break;}case OP_TFORLOOP
  2943. :checkreg(pt,a+c+5);if(reg>=a)last=pc;case OP_FORLOOP:checkreg(pt,a+2);case
  2944. OP_JMP:{int dest=pc+1+b;check(0<=dest&&dest<pt->sizecode);if(reg!=NO_REG&&pc<
  2945. dest&&dest<=lastpc)pc+=b;break;}case OP_CALL:case OP_TAILCALL:{if(b!=0){
  2946. checkreg(pt,a+b-1);}c--;if(c==LUA_MULTRET){check(checkopenop(pt,pc));}else if(
  2947. c!=0)checkreg(pt,a+c-1);if(reg>=a)last=pc;break;}case OP_RETURN:{b--;if(b>0)
  2948. checkreg(pt,a+b-1);break;}case OP_SETLIST:{checkreg(pt,a+(b&(LFIELDS_PER_FLUSH
  2949. -1))+1);break;}case OP_CLOSURE:{int nup;check(b<pt->sizep);nup=pt->p[b]->nups;
  2950. check(pc+nup<pt->sizecode);for(;nup>0;nup--){OpCode op1=GET_OPCODE(pt->code[pc
  2951. +nup]);check(op1==OP_GETUPVAL||op1==OP_MOVE);}break;}default:break;}}return pt
  2952. ->code[last];}
  2953. #undef check
  2954. #undef checkjump
  2955. #undef checkreg
  2956. int luaG_checkcode(const Proto*pt){return luaG_symbexec(pt,pt->sizecode,NO_REG
  2957. );}static const char*kname(Proto*p,int c){c=c-MAXSTACK;if(c>=0&&ttisstring(&p
  2958. ->k[c]))return svalue(&p->k[c]);else return"?";}static const char*getobjname(
  2959. CallInfo*ci,int stackpos,const char**name){if(isLua(ci)){Proto*p=ci_func(ci)->
  2960. l.p;int pc=currentpc(ci);Instruction i;*name=luaF_getlocalname(p,stackpos+1,pc
  2961. );if(*name)return"local";i=luaG_symbexec(p,pc,stackpos);lua_assert(pc!=-1);
  2962. switch(GET_OPCODE(i)){case OP_GETGLOBAL:{int g=GETARG_Bx(i);lua_assert(
  2963. ttisstring(&p->k[g]));*name=svalue(&p->k[g]);return"global";}case OP_MOVE:{int
  2964. a=GETARG_A(i);int b=GETARG_B(i);if(b<a)return getobjname(ci,b,name);break;}
  2965. case OP_GETTABLE:{int k=GETARG_C(i);*name=kname(p,k);return"field";}case
  2966. OP_SELF:{int k=GETARG_C(i);*name=kname(p,k);return"method";}default:break;}}
  2967. return NULL;}static const char*getfuncname(CallInfo*ci,const char**name){
  2968. Instruction i;if((isLua(ci)&&ci->u.l.tailcalls>0)||!isLua(ci-1))return NULL;ci
  2969. --;i=ci_func(ci)->l.p->code[currentpc(ci)];if(GET_OPCODE(i)==OP_CALL||
  2970. GET_OPCODE(i)==OP_TAILCALL)return getobjname(ci,GETARG_A(i),name);else return
  2971. NULL;}static int isinstack(CallInfo*ci,const TObject*o){StkId p;for(p=ci->base
  2972. ;p<ci->top;p++)if(o==p)return 1;return 0;}void luaG_typeerror(lua_State*L,
  2973. const TObject*o,const char*op){const char*name=NULL;const char*t=
  2974. luaT_typenames[ttype(o)];const char*kind=(isinstack(L->ci,o))?getobjname(L->ci
  2975. ,o-L->base,&name):NULL;if(kind)luaG_runerror(L,
  2976. "attempt to %s %s `%s' (a %s value)",op,kind,name,t);else luaG_runerror(L,
  2977. "attempt to %s a %s value",op,t);}void luaG_concaterror(lua_State*L,StkId p1,
  2978. StkId p2){if(ttisstring(p1))p1=p2;lua_assert(!ttisstring(p1));luaG_typeerror(L
  2979. ,p1,"concatenate");}void luaG_aritherror(lua_State*L,const TObject*p1,const
  2980. TObject*p2){TObject temp;if(luaV_tonumber(p1,&temp)==NULL)p2=p1;luaG_typeerror
  2981. (L,p2,"perform arithmetic on");}int luaG_ordererror(lua_State*L,const TObject*
  2982. p1,const TObject*p2){const char*t1=luaT_typenames[ttype(p1)];const char*t2=
  2983. luaT_typenames[ttype(p2)];if(t1[2]==t2[2])luaG_runerror(L,
  2984. "attempt to compare two %s values",t1);else luaG_runerror(L,
  2985. "attempt to compare %s with %s",t1,t2);return 0;}static void addinfo(lua_State
  2986. *L,const char*msg){CallInfo*ci=L->ci;if(isLua(ci)){char buff[LUA_IDSIZE];int
  2987. line=currentline(ci);luaO_chunkid(buff,getstr(getluaproto(ci)->source),
  2988. LUA_IDSIZE);luaO_pushfstring(L,"%s:%d: %s",buff,line,msg);}}void luaG_errormsg
  2989. (lua_State*L){if(L->errfunc!=0){StkId errfunc=restorestack(L,L->errfunc);if(!
  2990. ttisfunction(errfunc))luaD_throw(L,LUA_ERRERR);setobjs2s(L->top,L->top-1);
  2991. setobjs2s(L->top-1,errfunc);incr_top(L);luaD_call(L,L->top-2,1);}luaD_throw(L,
  2992. LUA_ERRRUN);}void luaG_runerror(lua_State*L,const char*fmt,...){va_list argp;
  2993. va_start(argp,fmt);addinfo(L,luaO_pushvfstring(L,fmt,argp));va_end(argp);
  2994. luaG_errormsg(L);}
  2995. #line 1 "ldo.c"
  2996. #define ldo_c
  2997. struct lua_longjmp{struct lua_longjmp*previous;jmp_buf b;volatile int status;}
  2998. ;static void seterrorobj(lua_State*L,int errcode,StkId oldtop){switch(errcode)
  2999. {case LUA_ERRMEM:{setsvalue2s(oldtop,luaS_new(L,MEMERRMSG));break;}case
  3000. LUA_ERRERR:{setsvalue2s(oldtop,luaS_new(L,"error in error handling"));break;}
  3001. case LUA_ERRSYNTAX:case LUA_ERRRUN:{setobjs2s(oldtop,L->top-1);break;}}L->top=
  3002. oldtop+1;}void luaD_throw(lua_State*L,int errcode){if(L->errorJmp){L->errorJmp
  3003. ->status=errcode;longjmp(L->errorJmp->b,1);}else{G(L)->panic(L);exit(
  3004. EXIT_FAILURE);}}int luaD_rawrunprotected(lua_State*L,Pfunc f,void*ud){struct
  3005. lua_longjmp lj;lj.status=0;lj.previous=L->errorJmp;L->errorJmp=&lj;if(setjmp(
  3006. lj.b)==0)(*f)(L,ud);L->errorJmp=lj.previous;return lj.status;}static void
  3007. restore_stack_limit(lua_State*L){L->stack_last=L->stack+L->stacksize-1;if(L->
  3008. size_ci>LUA_MAXCALLS){int inuse=(L->ci-L->base_ci);if(inuse+1<LUA_MAXCALLS)
  3009. luaD_reallocCI(L,LUA_MAXCALLS);}}static void correctstack(lua_State*L,TObject*
  3010. oldstack){CallInfo*ci;GCObject*up;L->top=(L->top-oldstack)+L->stack;for(up=L->
  3011. openupval;up!=NULL;up=up->gch.next)gcotouv(up)->v=(gcotouv(up)->v-oldstack)+L
  3012. ->stack;for(ci=L->base_ci;ci<=L->ci;ci++){ci->top=(ci->top-oldstack)+L->stack;
  3013. ci->base=(ci->base-oldstack)+L->stack;}L->base=L->ci->base;}void
  3014. luaD_reallocstack(lua_State*L,int newsize){TObject*oldstack=L->stack;
  3015. luaM_reallocvector(L,L->stack,L->stacksize,newsize,TObject);L->stacksize=
  3016. newsize;L->stack_last=L->stack+newsize-1-EXTRA_STACK;correctstack(L,oldstack);
  3017. }void luaD_reallocCI(lua_State*L,int newsize){CallInfo*oldci=L->base_ci;
  3018. luaM_reallocvector(L,L->base_ci,L->size_ci,newsize,CallInfo);L->size_ci=cast(
  3019. unsigned short,newsize);L->ci=(L->ci-oldci)+L->base_ci;L->end_ci=L->base_ci+L
  3020. ->size_ci;}void luaD_growstack(lua_State*L,int n){if(n<=L->stacksize)
  3021. luaD_reallocstack(L,2*L->stacksize);else luaD_reallocstack(L,L->stacksize+n+
  3022. EXTRA_STACK);}static void luaD_growCI(lua_State*L){if(L->size_ci>LUA_MAXCALLS)
  3023. luaD_throw(L,LUA_ERRERR);else{luaD_reallocCI(L,2*L->size_ci);if(L->size_ci>
  3024. LUA_MAXCALLS)luaG_runerror(L,"stack overflow");}}void luaD_callhook(lua_State*
  3025. L,int event,int line){lua_Hook hook=L->hook;if(hook&&L->allowhook){ptrdiff_t
  3026. top=savestack(L,L->top);ptrdiff_t ci_top=savestack(L,L->ci->top);lua_Debug ar;
  3027. ar.event=event;ar.currentline=line;if(event==LUA_HOOKTAILRET)ar.i_ci=0;else ar
  3028. .i_ci=L->ci-L->base_ci;luaD_checkstack(L,LUA_MINSTACK);L->ci->top=L->top+
  3029. LUA_MINSTACK;L->allowhook=0;lua_unlock(L);(*hook)(L,&ar);lua_lock(L);
  3030. lua_assert(!L->allowhook);L->allowhook=1;L->ci->top=restorestack(L,ci_top);L->
  3031. top=restorestack(L,top);}}static void adjust_varargs(lua_State*L,int nfixargs,
  3032. StkId base){int i;Table*htab;TObject nname;int actual=L->top-base;if(actual<
  3033. nfixargs){luaD_checkstack(L,nfixargs-actual);for(;actual<nfixargs;++actual)
  3034. setnilvalue(L->top++);}actual-=nfixargs;htab=luaH_new(L,actual,1);for(i=0;i<
  3035. actual;i++)setobj2n(luaH_setnum(L,htab,i+1),L->top-actual+i);setsvalue(&nname,
  3036. luaS_newliteral(L,"n"));setnvalue(luaH_set(L,htab,&nname),cast(lua_Number,
  3037. actual));L->top-=actual;sethvalue(L->top,htab);incr_top(L);}static StkId
  3038. tryfuncTM(lua_State*L,StkId func){const TObject*tm=luaT_gettmbyobj(L,func,
  3039. TM_CALL);StkId p;ptrdiff_t funcr=savestack(L,func);if(!ttisfunction(tm))
  3040. luaG_typeerror(L,func,"call");for(p=L->top;p>func;p--)setobjs2s(p,p-1);
  3041. incr_top(L);func=restorestack(L,funcr);setobj2s(func,tm);return func;}StkId
  3042. luaD_precall(lua_State*L,StkId func){LClosure*cl;ptrdiff_t funcr=savestack(L,
  3043. func);if(!ttisfunction(func))func=tryfuncTM(L,func);if(L->ci+1==L->end_ci)
  3044. luaD_growCI(L);else condhardstacktests(luaD_reallocCI(L,L->size_ci));cl=&
  3045. clvalue(func)->l;if(!cl->isC){CallInfo*ci;Proto*p=cl->p;if(p->is_vararg)
  3046. adjust_varargs(L,p->numparams,func+1);luaD_checkstack(L,p->maxstacksize);ci=++
  3047. L->ci;L->base=L->ci->base=restorestack(L,funcr)+1;ci->top=L->base+p->
  3048. maxstacksize;ci->u.l.savedpc=p->code;ci->u.l.tailcalls=0;ci->state=CI_SAVEDPC;
  3049. while(L->top<ci->top)setnilvalue(L->top++);L->top=ci->top;return NULL;}else{
  3050. CallInfo*ci;int n;luaD_checkstack(L,LUA_MINSTACK);ci=++L->ci;L->base=L->ci->
  3051. base=restorestack(L,funcr)+1;ci->top=L->top+LUA_MINSTACK;ci->state=CI_C;if(L->
  3052. hookmask&LUA_MASKCALL)luaD_callhook(L,LUA_HOOKCALL,-1);lua_unlock(L);
  3053. #ifdef LUA_COMPATUPVALUES
  3054. lua_pushupvalues(L);
  3055. #endif
  3056. n=(*clvalue(L->base-1)->c.f)(L);lua_lock(L);return L->top-n;}}static StkId
  3057. callrethooks(lua_State*L,StkId firstResult){ptrdiff_t fr=savestack(L,
  3058. firstResult);luaD_callhook(L,LUA_HOOKRET,-1);if(!(L->ci->state&CI_C)){while(L
  3059. ->ci->u.l.tailcalls--)luaD_callhook(L,LUA_HOOKTAILRET,-1);}return restorestack
  3060. (L,fr);}void luaD_poscall(lua_State*L,int wanted,StkId firstResult){StkId res;
  3061. if(L->hookmask&LUA_MASKRET)firstResult=callrethooks(L,firstResult);res=L->base
  3062. -1;L->ci--;L->base=L->ci->base;while(wanted!=0&&firstResult<L->top){setobjs2s(
  3063. res++,firstResult++);wanted--;}while(wanted-->0)setnilvalue(res++);L->top=res;
  3064. }void luaD_call(lua_State*L,StkId func,int nResults){StkId firstResult;
  3065. lua_assert(!(L->ci->state&CI_CALLING));if(++L->nCcalls>=LUA_MAXCCALLS){if(L->
  3066. nCcalls==LUA_MAXCCALLS)luaG_runerror(L,"C stack overflow");else if(L->nCcalls
  3067. >=(LUA_MAXCCALLS+(LUA_MAXCCALLS>>3)))luaD_throw(L,LUA_ERRERR);}firstResult=
  3068. luaD_precall(L,func);if(firstResult==NULL)firstResult=luaV_execute(L);
  3069. luaD_poscall(L,nResults,firstResult);L->nCcalls--;luaC_checkGC(L);}static void
  3070. resume(lua_State*L,void*ud){StkId firstResult;int nargs=*cast(int*,ud);
  3071. CallInfo*ci=L->ci;if(ci==L->base_ci){lua_assert(nargs<L->top-L->base);
  3072. luaD_precall(L,L->top-(nargs+1));}else{lua_assert(ci->state&CI_YIELD);if(ci->
  3073. state&CI_C){int nresults;lua_assert((ci-1)->state&CI_SAVEDPC);lua_assert(
  3074. GET_OPCODE(*((ci-1)->u.l.savedpc-1))==OP_CALL||GET_OPCODE(*((ci-1)->u.l.
  3075. savedpc-1))==OP_TAILCALL);nresults=GETARG_C(*((ci-1)->u.l.savedpc-1))-1;
  3076. luaD_poscall(L,nresults,L->top-nargs);if(nresults>=0)L->top=L->ci->top;}else{
  3077. ci->state&=~CI_YIELD;}}firstResult=luaV_execute(L);if(firstResult!=NULL)
  3078. luaD_poscall(L,LUA_MULTRET,firstResult);}static int resume_error(lua_State*L,
  3079. const char*msg){L->top=L->ci->base;setsvalue2s(L->top,luaS_new(L,msg));
  3080. incr_top(L);lua_unlock(L);return LUA_ERRRUN;}LUA_API int lua_resume(lua_State*
  3081. L,int nargs){int status;lu_byte old_allowhooks;lua_lock(L);if(L->ci==L->
  3082. base_ci){if(nargs>=L->top-L->base)return resume_error(L,
  3083. "cannot resume dead coroutine");}else if(!(L->ci->state&CI_YIELD))return
  3084. resume_error(L,"cannot resume non-suspended coroutine");old_allowhooks=L->
  3085. allowhook;lua_assert(L->errfunc==0&&L->nCcalls==0);status=luaD_rawrunprotected
  3086. (L,resume,&nargs);if(status!=0){L->ci=L->base_ci;L->base=L->ci->base;L->
  3087. nCcalls=0;luaF_close(L,L->base);seterrorobj(L,status,L->base);L->allowhook=
  3088. old_allowhooks;restore_stack_limit(L);}lua_unlock(L);return status;}LUA_API
  3089. int lua_yield(lua_State*L,int nresults){CallInfo*ci;lua_lock(L);ci=L->ci;if(L
  3090. ->nCcalls>0)luaG_runerror(L,
  3091. "attempt to yield across metamethod/C-call boundary");if(ci->state&CI_C){if((
  3092. ci-1)->state&CI_C)luaG_runerror(L,"cannot yield a C function");if(L->top-
  3093. nresults>L->base){int i;for(i=0;i<nresults;i++)setobjs2s(L->base+i,L->top-
  3094. nresults+i);L->top=L->base+nresults;}}ci->state|=CI_YIELD;lua_unlock(L);return
  3095. -1;}int luaD_pcall(lua_State*L,Pfunc func,void*u,ptrdiff_t old_top,ptrdiff_t
  3096. ef){int status;unsigned short oldnCcalls=L->nCcalls;ptrdiff_t old_ci=saveci(L,
  3097. L->ci);lu_byte old_allowhooks=L->allowhook;ptrdiff_t old_errfunc=L->errfunc;L
  3098. ->errfunc=ef;status=luaD_rawrunprotected(L,func,u);if(status!=0){StkId oldtop=
  3099. restorestack(L,old_top);luaF_close(L,oldtop);seterrorobj(L,status,oldtop);L->
  3100. nCcalls=oldnCcalls;L->ci=restoreci(L,old_ci);L->base=L->ci->base;L->allowhook=
  3101. old_allowhooks;restore_stack_limit(L);}L->errfunc=old_errfunc;return status;}
  3102. struct SParser{ZIO*z;Mbuffer buff;int bin;};static void f_parser(lua_State*L,
  3103. void*ud){struct SParser*p;Proto*tf;Closure*cl;luaC_checkGC(L);p=cast(struct
  3104. SParser*,ud);tf=p->bin?luaU_undump(L,p->z,&p->buff):luaY_parser(L,p->z,&p->
  3105. buff);cl=luaF_newLclosure(L,0,gt(L));cl->l.p=tf;setclvalue(L->top,cl);incr_top
  3106. (L);}int luaD_protectedparser(lua_State*L,ZIO*z,int bin){struct SParser p;int
  3107. status;ptrdiff_t oldtopr=savestack(L,L->top);p.z=z;p.bin=bin;luaZ_initbuffer(L
  3108. ,&p.buff);status=luaD_rawrunprotected(L,f_parser,&p);luaZ_freebuffer(L,&p.buff
  3109. );if(status!=0){StkId oldtop=restorestack(L,oldtopr);seterrorobj(L,status,
  3110. oldtop);}return status;}
  3111. #line 1 "ldump.c"
  3112. #define ldump_c
  3113. #define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D)
  3114. #define DumpLiteral(s,D) DumpBlock(""s,(sizeof(s))-1,D)
  3115. typedef struct{lua_State*L;lua_Chunkwriter write;void*data;}DumpState;static
  3116. void DumpBlock(const void*b,size_t size,DumpState*D){lua_unlock(D->L);(*D->
  3117. write)(D->L,b,size,D->data);lua_lock(D->L);}static void DumpByte(int y,
  3118. DumpState*D){char x=(char)y;DumpBlock(&x,sizeof(x),D);}static void DumpInt(int
  3119. x,DumpState*D){DumpBlock(&x,sizeof(x),D);}static void DumpSize(size_t x,
  3120. DumpState*D){DumpBlock(&x,sizeof(x),D);}static void DumpNumber(lua_Number x,
  3121. DumpState*D){DumpBlock(&x,sizeof(x),D);}static void DumpString(TString*s,
  3122. DumpState*D){if(s==NULL||getstr(s)==NULL)DumpSize(0,D);else{size_t size=s->tsv
  3123. .len+1;DumpSize(size,D);DumpBlock(getstr(s),size,D);}}static void DumpCode(
  3124. const Proto*f,DumpState*D){DumpInt(f->sizecode,D);DumpVector(f->code,f->
  3125. sizecode,sizeof(*f->code),D);}static void DumpLocals(const Proto*f,DumpState*D
  3126. ){int i,n=f->sizelocvars;DumpInt(n,D);for(i=0;i<n;i++){DumpString(f->locvars[i
  3127. ].varname,D);DumpInt(f->locvars[i].startpc,D);DumpInt(f->locvars[i].endpc,D);}
  3128. }static void DumpLines(const Proto*f,DumpState*D){DumpInt(f->sizelineinfo,D);
  3129. DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D);}static void
  3130. DumpUpvalues(const Proto*f,DumpState*D){int i,n=f->sizeupvalues;DumpInt(n,D);
  3131. for(i=0;i<n;i++)DumpString(f->upvalues[i],D);}static void DumpFunction(const
  3132. Proto*f,const TString*p,DumpState*D);static void DumpConstants(const Proto*f,
  3133. DumpState*D){int i,n;DumpInt(n=f->sizek,D);for(i=0;i<n;i++){const TObject*o=&f
  3134. ->k[i];DumpByte(ttype(o),D);switch(ttype(o)){case LUA_TNUMBER:DumpNumber(
  3135. nvalue(o),D);break;case LUA_TSTRING:DumpString(tsvalue(o),D);break;case
  3136. LUA_TNIL:break;default:lua_assert(0);break;}}DumpInt(n=f->sizep,D);for(i=0;i<n
  3137. ;i++)DumpFunction(f->p[i],f->source,D);}static void DumpFunction(const Proto*f
  3138. ,const TString*p,DumpState*D){DumpString((f->source==p)?NULL:f->source,D);
  3139. DumpInt(f->lineDefined,D);DumpByte(f->nups,D);DumpByte(f->numparams,D);
  3140. DumpByte(f->is_vararg,D);DumpByte(f->maxstacksize,D);DumpLines(f,D);DumpLocals
  3141. (f,D);DumpUpvalues(f,D);DumpConstants(f,D);DumpCode(f,D);}static void
  3142. DumpHeader(DumpState*D){DumpLiteral(LUA_SIGNATURE,D);DumpByte(VERSION,D);
  3143. DumpByte(luaU_endianness(),D);DumpByte(sizeof(int),D);DumpByte(sizeof(size_t),
  3144. D);DumpByte(sizeof(Instruction),D);DumpByte(SIZE_OP,D);DumpByte(SIZE_A,D);
  3145. DumpByte(SIZE_B,D);DumpByte(SIZE_C,D);DumpByte(sizeof(lua_Number),D);
  3146. DumpNumber(TEST_NUMBER,D);}void luaU_dump(lua_State*L,const Proto*Main,
  3147. lua_Chunkwriter w,void*data){DumpState D;D.L=L;D.write=w;D.data=data;
  3148. DumpHeader(&D);DumpFunction(Main,NULL,&D);}
  3149. #line 1 "lfunc.c"
  3150. #define lfunc_c
  3151. #define sizeCclosure(n) (cast(int,sizeof(CClosure))+cast(int,sizeof(TObject)*(\
  3152. (n)-1)))
  3153. #define sizeLclosure(n) (cast(int,sizeof(LClosure))+cast(int,sizeof(TObject*)*\
  3154. ((n)-1)))
  3155. Closure*luaF_newCclosure(lua_State*L,int nelems){Closure*c=cast(Closure*,
  3156. luaM_malloc(L,sizeCclosure(nelems)));luaC_link(L,valtogco(c),LUA_TFUNCTION);c
  3157. ->c.isC=1;c->c.nupvalues=cast(lu_byte,nelems);return c;}Closure*
  3158. luaF_newLclosure(lua_State*L,int nelems,TObject*e){Closure*c=cast(Closure*,
  3159. luaM_malloc(L,sizeLclosure(nelems)));luaC_link(L,valtogco(c),LUA_TFUNCTION);c
  3160. ->l.isC=0;c->l.g=*e;c->l.nupvalues=cast(lu_byte,nelems);return c;}UpVal*
  3161. luaF_findupval(lua_State*L,StkId level){GCObject**pp=&L->openupval;UpVal*p;
  3162. UpVal*v;while((p=ngcotouv(*pp))!=NULL&&p->v>=level){if(p->v==level)return p;pp
  3163. =&p->next;}v=luaM_new(L,UpVal);v->tt=LUA_TUPVAL;v->marked=1;v->v=level;v->next
  3164. =*pp;*pp=valtogco(v);return v;}void luaF_close(lua_State*L,StkId level){UpVal*
  3165. p;while((p=ngcotouv(L->openupval))!=NULL&&p->v>=level){setobj(&p->value,p->v);
  3166. p->v=&p->value;L->openupval=p->next;luaC_link(L,valtogco(p),LUA_TUPVAL);}}
  3167. Proto*luaF_newproto(lua_State*L){Proto*f=luaM_new(L,Proto);luaC_link(L,
  3168. valtogco(f),LUA_TPROTO);f->k=NULL;f->sizek=0;f->p=NULL;f->sizep=0;f->code=NULL
  3169. ;f->sizecode=0;f->sizelineinfo=0;f->sizeupvalues=0;f->nups=0;f->upvalues=NULL;
  3170. f->numparams=0;f->is_vararg=0;f->maxstacksize=0;f->lineinfo=NULL;f->
  3171. sizelocvars=0;f->locvars=NULL;f->lineDefined=0;f->source=NULL;return f;}void
  3172. luaF_freeproto(lua_State*L,Proto*f){luaM_freearray(L,f->code,f->sizecode,
  3173. Instruction);luaM_freearray(L,f->p,f->sizep,Proto*);luaM_freearray(L,f->k,f->
  3174. sizek,TObject);luaM_freearray(L,f->lineinfo,f->sizelineinfo,int);
  3175. luaM_freearray(L,f->locvars,f->sizelocvars,struct LocVar);luaM_freearray(L,f->
  3176. upvalues,f->sizeupvalues,TString*);luaM_freelem(L,f);}void luaF_freeclosure(
  3177. lua_State*L,Closure*c){int size=(c->c.isC)?sizeCclosure(c->c.nupvalues):
  3178. sizeLclosure(c->l.nupvalues);luaM_free(L,c,size);}const char*luaF_getlocalname
  3179. (const Proto*f,int local_number,int pc){int i;for(i=0;i<f->sizelocvars&&f->
  3180. locvars[i].startpc<=pc;i++){if(pc<f->locvars[i].endpc){local_number--;if(
  3181. local_number==0)return getstr(f->locvars[i].varname);}}return NULL;}
  3182. #line 1 "lgc.c"
  3183. #define lgc_c
  3184. typedef struct GCState{GCObject*tmark;GCObject*wk;GCObject*wv;GCObject*wkv;
  3185. global_State*g;}GCState;
  3186. #define setbit(x,b) ((x)|=(1<<(b)))
  3187. #define resetbit(x,b) ((x)&=cast(lu_byte,~(1<<(b))))
  3188. #define testbit(x,b) ((x)&(1<<(b)))
  3189. #define unmark(x) resetbit((x)->gch.marked,0)
  3190. #define ismarked(x) ((x)->gch.marked&((1<<4)|1))
  3191. #define stringmark(s) setbit((s)->tsv.marked,0)
  3192. #define isfinalized(u) (!testbit((u)->uv.marked,1))
  3193. #define markfinalized(u) resetbit((u)->uv.marked,1)
  3194. #define KEYWEAKBIT 1
  3195. #define VALUEWEAKBIT 2
  3196. #define KEYWEAK (1<<KEYWEAKBIT)
  3197. #define VALUEWEAK (1<<VALUEWEAKBIT)
  3198. #define markobject(st,o) {checkconsistency(o);if(iscollectable(o)&&!ismarked(\
  3199. gcvalue(o)))reallymarkobject(st,gcvalue(o));}
  3200. #define condmarkobject(st,o,c) {checkconsistency(o);if(iscollectable(o)&&!\
  3201. ismarked(gcvalue(o))&&(c))reallymarkobject(st,gcvalue(o));}
  3202. #define markvalue(st,t) {if(!ismarked(valtogco(t)))reallymarkobject(st,\
  3203. valtogco(t));}
  3204. static void reallymarkobject(GCState*st,GCObject*o){lua_assert(!ismarked(o));
  3205. setbit(o->gch.marked,0);switch(o->gch.tt){case LUA_TUSERDATA:{markvalue(st,
  3206. gcotou(o)->uv.metatable);break;}case LUA_TFUNCTION:{gcotocl(o)->c.gclist=st->
  3207. tmark;st->tmark=o;break;}case LUA_TTABLE:{gcotoh(o)->gclist=st->tmark;st->
  3208. tmark=o;break;}case LUA_TTHREAD:{gcototh(o)->gclist=st->tmark;st->tmark=o;
  3209. break;}case LUA_TPROTO:{gcotop(o)->gclist=st->tmark;st->tmark=o;break;}default
  3210. :lua_assert(o->gch.tt==LUA_TSTRING);}}static void marktmu(GCState*st){GCObject
  3211. *u;for(u=st->g->tmudata;u;u=u->gch.next){unmark(u);reallymarkobject(st,u);}}
  3212. size_t luaC_separateudata(lua_State*L){size_t deadmem=0;GCObject**p=&G(L)->
  3213. rootudata;GCObject*curr;GCObject*collected=NULL;GCObject**lastcollected=&
  3214. collected;while((curr=*p)!=NULL){lua_assert(curr->gch.tt==LUA_TUSERDATA);if(
  3215. ismarked(curr)||isfinalized(gcotou(curr)))p=&curr->gch.next;else if(fasttm(L,
  3216. gcotou(curr)->uv.metatable,TM_GC)==NULL){markfinalized(gcotou(curr));p=&curr->
  3217. gch.next;}else{deadmem+=sizeudata(gcotou(curr)->uv.len);*p=curr->gch.next;curr
  3218. ->gch.next=NULL;*lastcollected=curr;lastcollected=&curr->gch.next;}}*
  3219. lastcollected=G(L)->tmudata;G(L)->tmudata=collected;return deadmem;}static
  3220. void removekey(Node*n){setnilvalue(gval(n));if(iscollectable(gkey(n)))setttype
  3221. (gkey(n),LUA_TNONE);}static void traversetable(GCState*st,Table*h){int i;int
  3222. weakkey=0;int weakvalue=0;const TObject*mode;markvalue(st,h->metatable);
  3223. lua_assert(h->lsizenode||h->node==st->g->dummynode);mode=gfasttm(st->g,h->
  3224. metatable,TM_MODE);if(mode&&ttisstring(mode)){weakkey=(strchr(svalue(mode),'k'
  3225. )!=NULL);weakvalue=(strchr(svalue(mode),'v')!=NULL);if(weakkey||weakvalue){
  3226. GCObject**weaklist;h->marked&=~(KEYWEAK|VALUEWEAK);h->marked|=cast(lu_byte,(
  3227. weakkey<<KEYWEAKBIT)|(weakvalue<<VALUEWEAKBIT));weaklist=(weakkey&&weakvalue)?
  3228. &st->wkv:(weakkey)?&st->wk:&st->wv;h->gclist=*weaklist;*weaklist=valtogco(h);}
  3229. }if(!weakvalue){i=h->sizearray;while(i--)markobject(st,&h->array[i]);}i=
  3230. sizenode(h);while(i--){Node*n=gnode(h,i);if(!ttisnil(gval(n))){lua_assert(!
  3231. ttisnil(gkey(n)));condmarkobject(st,gkey(n),!weakkey);condmarkobject(st,gval(n
  3232. ),!weakvalue);}}}static void traverseproto(GCState*st,Proto*f){int i;
  3233. stringmark(f->source);for(i=0;i<f->sizek;i++){if(ttisstring(f->k+i))stringmark
  3234. (tsvalue(f->k+i));}for(i=0;i<f->sizeupvalues;i++)stringmark(f->upvalues[i]);
  3235. for(i=0;i<f->sizep;i++)markvalue(st,f->p[i]);for(i=0;i<f->sizelocvars;i++)
  3236. stringmark(f->locvars[i].varname);lua_assert(luaG_checkcode(f));}static void
  3237. traverseclosure(GCState*st,Closure*cl){if(cl->c.isC){int i;for(i=0;i<cl->c.
  3238. nupvalues;i++)markobject(st,&cl->c.upvalue[i]);}else{int i;lua_assert(cl->l.
  3239. nupvalues==cl->l.p->nups);markvalue(st,hvalue(&cl->l.g));markvalue(st,cl->l.p)
  3240. ;for(i=0;i<cl->l.nupvalues;i++){UpVal*u=cl->l.upvals[i];if(!u->marked){
  3241. markobject(st,&u->value);u->marked=1;}}}}static void checkstacksizes(lua_State
  3242. *L,StkId max){int used=L->ci-L->base_ci;if(4*used<L->size_ci&&2*BASIC_CI_SIZE<
  3243. L->size_ci)luaD_reallocCI(L,L->size_ci/2);else condhardstacktests(
  3244. luaD_reallocCI(L,L->size_ci));used=max-L->stack;if(4*used<L->stacksize&&2*(
  3245. BASIC_STACK_SIZE+EXTRA_STACK)<L->stacksize)luaD_reallocstack(L,L->stacksize/2)
  3246. ;else condhardstacktests(luaD_reallocstack(L,L->stacksize));}static void
  3247. traversestack(GCState*st,lua_State*L1){StkId o,lim;CallInfo*ci;markobject(st,
  3248. gt(L1));lim=L1->top;for(ci=L1->base_ci;ci<=L1->ci;ci++){lua_assert(ci->top<=L1
  3249. ->stack_last);lua_assert(ci->state&(CI_C|CI_HASFRAME|CI_SAVEDPC));if(lim<ci->
  3250. top)lim=ci->top;}for(o=L1->stack;o<L1->top;o++)markobject(st,o);for(;o<=lim;o
  3251. ++)setnilvalue(o);checkstacksizes(L1,lim);}static void propagatemarks(GCState*
  3252. st){while(st->tmark){switch(st->tmark->gch.tt){case LUA_TTABLE:{Table*h=gcotoh
  3253. (st->tmark);st->tmark=h->gclist;traversetable(st,h);break;}case LUA_TFUNCTION:
  3254. {Closure*cl=gcotocl(st->tmark);st->tmark=cl->c.gclist;traverseclosure(st,cl);
  3255. break;}case LUA_TTHREAD:{lua_State*th=gcototh(st->tmark);st->tmark=th->gclist;
  3256. traversestack(st,th);break;}case LUA_TPROTO:{Proto*p=gcotop(st->tmark);st->
  3257. tmark=p->gclist;traverseproto(st,p);break;}default:lua_assert(0);}}}static int
  3258. valismarked(const TObject*o){if(ttisstring(o))stringmark(tsvalue(o));return!
  3259. iscollectable(o)||testbit(o->value.gc->gch.marked,0);}static void
  3260. cleartablekeys(GCObject*l){while(l){Table*h=gcotoh(l);int i=sizenode(h);
  3261. lua_assert(h->marked&KEYWEAK);while(i--){Node*n=gnode(h,i);if(!valismarked(
  3262. gkey(n)))removekey(n);}l=h->gclist;}}static void cleartablevalues(GCObject*l){
  3263. while(l){Table*h=gcotoh(l);int i=h->sizearray;lua_assert(h->marked&VALUEWEAK);
  3264. while(i--){TObject*o=&h->array[i];if(!valismarked(o))setnilvalue(o);}i=
  3265. sizenode(h);while(i--){Node*n=gnode(h,i);if(!valismarked(gval(n)))removekey(n)
  3266. ;}l=h->gclist;}}static void freeobj(lua_State*L,GCObject*o){switch(o->gch.tt){
  3267. case LUA_TPROTO:luaF_freeproto(L,gcotop(o));break;case LUA_TFUNCTION:
  3268. luaF_freeclosure(L,gcotocl(o));break;case LUA_TUPVAL:luaM_freelem(L,gcotouv(o)
  3269. );break;case LUA_TTABLE:luaH_free(L,gcotoh(o));break;case LUA_TTHREAD:{
  3270. lua_assert(gcototh(o)!=L&&gcototh(o)!=G(L)->mainthread);luaE_freethread(L,
  3271. gcototh(o));break;}case LUA_TSTRING:{luaM_free(L,o,sizestring(gcotots(o)->tsv.
  3272. len));break;}case LUA_TUSERDATA:{luaM_free(L,o,sizeudata(gcotou(o)->uv.len));
  3273. break;}default:lua_assert(0);}}static int sweeplist(lua_State*L,GCObject**p,
  3274. int limit){GCObject*curr;int count=0;while((curr=*p)!=NULL){if(curr->gch.
  3275. marked>limit){unmark(curr);p=&curr->gch.next;}else{count++;*p=curr->gch.next;
  3276. freeobj(L,curr);}}return count;}static void sweepstrings(lua_State*L,int all){
  3277. int i;for(i=0;i<G(L)->strt.size;i++){G(L)->strt.nuse-=sweeplist(L,&G(L)->strt.
  3278. hash[i],all);}}static void checkSizes(lua_State*L,size_t deadmem){if(G(L)->
  3279. strt.nuse<cast(ls_nstr,G(L)->strt.size/4)&&G(L)->strt.size>MINSTRTABSIZE*2)
  3280. luaS_resize(L,G(L)->strt.size/2);if(luaZ_sizebuffer(&G(L)->buff)>LUA_MINBUFFER
  3281. *2){size_t newsize=luaZ_sizebuffer(&G(L)->buff)/2;luaZ_resizebuffer(L,&G(L)->
  3282. buff,newsize);}G(L)->GCthreshold=2*G(L)->nblocks-deadmem;}static void do1gcTM(
  3283. lua_State*L,Udata*udata){const TObject*tm=fasttm(L,udata->uv.metatable,TM_GC);
  3284. if(tm!=NULL){setobj2s(L->top,tm);setuvalue(L->top+1,udata);L->top+=2;luaD_call
  3285. (L,L->top-2,0);}}void luaC_callGCTM(lua_State*L){lu_byte oldah=L->allowhook;L
  3286. ->allowhook=0;L->top++;while(G(L)->tmudata!=NULL){GCObject*o=G(L)->tmudata;
  3287. Udata*udata=gcotou(o);G(L)->tmudata=udata->uv.next;udata->uv.next=G(L)->
  3288. rootudata;G(L)->rootudata=o;setuvalue(L->top-1,udata);unmark(o);markfinalized(
  3289. udata);do1gcTM(L,udata);}L->top--;L->allowhook=oldah;}void luaC_sweep(
  3290. lua_State*L,int all){if(all)all=256;sweeplist(L,&G(L)->rootudata,all);
  3291. sweepstrings(L,all);sweeplist(L,&G(L)->rootgc,all);}static void markroot(
  3292. GCState*st,lua_State*L){global_State*g=st->g;markobject(st,defaultmeta(L));
  3293. markobject(st,registry(L));traversestack(st,g->mainthread);if(L!=g->mainthread
  3294. )markvalue(st,L);}static size_t mark(lua_State*L){size_t deadmem;GCState st;
  3295. GCObject*wkv;st.g=G(L);st.tmark=NULL;st.wkv=st.wk=st.wv=NULL;markroot(&st,L);
  3296. propagatemarks(&st);cleartablevalues(st.wkv);cleartablevalues(st.wv);wkv=st.
  3297. wkv;st.wkv=NULL;st.wv=NULL;deadmem=luaC_separateudata(L);marktmu(&st);
  3298. propagatemarks(&st);cleartablekeys(wkv);cleartablekeys(st.wk);cleartablevalues
  3299. (st.wv);cleartablekeys(st.wkv);cleartablevalues(st.wkv);return deadmem;}void
  3300. luaC_collectgarbage(lua_State*L){size_t deadmem=mark(L);luaC_sweep(L,0);
  3301. checkSizes(L,deadmem);luaC_callGCTM(L);}void luaC_link(lua_State*L,GCObject*o,
  3302. lu_byte tt){o->gch.next=G(L)->rootgc;G(L)->rootgc=o;o->gch.marked=0;o->gch.tt=
  3303. tt;}
  3304. #line 1 "liolib.c"
  3305. #define liolib_c
  3306. #ifndef USE_TMPNAME
  3307. #ifdef __GNUC__
  3308. #define USE_TMPNAME 0
  3309. #else
  3310. #define USE_TMPNAME 1
  3311. #endif
  3312. #endif
  3313. #ifndef USE_POPEN
  3314. #ifdef _POSIX_C_SOURCE
  3315. #if _POSIX_C_SOURCE >=2
  3316. #define USE_POPEN 1
  3317. #endif
  3318. #endif
  3319. #endif
  3320. #ifndef USE_POPEN
  3321. #define USE_POPEN 0
  3322. #endif
  3323. #if !USE_POPEN
  3324. #define pclose(f) (-1)
  3325. #endif
  3326. #define FILEHANDLE "FILE*"
  3327. #define IO_INPUT "_input"
  3328. #define IO_OUTPUT "_output"
  3329. static int pushresult(lua_State*L,int i,const char*filename){if(i){
  3330. lua_pushboolean(L,1);return 1;}else{lua_pushnil(L);if(filename)lua_pushfstring
  3331. (L,"%s: %s",filename,strerror(errno));else lua_pushfstring(L,"%s",strerror(
  3332. errno));lua_pushnumber(L,errno);return 3;}}static FILE**topfile(lua_State*L,
  3333. int findex){FILE**f=(FILE**)luaL_checkudata(L,findex,FILEHANDLE);if(f==NULL)
  3334. luaL_argerror(L,findex,"bad file");return f;}static int io_type(lua_State*L){
  3335. FILE**f=(FILE**)luaL_checkudata(L,1,FILEHANDLE);if(f==NULL)lua_pushnil(L);else
  3336. if(*f==NULL)lua_pushliteral(L,"closed file");else lua_pushliteral(L,"file");
  3337. return 1;}static FILE*tofile(lua_State*L,int findex){FILE**f=topfile(L,findex)
  3338. ;if(*f==NULL)luaL_error(L,"attempt to use a closed file");return*f;}static
  3339. FILE**newfile(lua_State*L){FILE**pf=(FILE**)lua_newuserdata(L,sizeof(FILE*));*
  3340. pf=NULL;luaL_getmetatable(L,FILEHANDLE);lua_setmetatable(L,-2);return pf;}
  3341. static void registerfile(lua_State*L,FILE*f,const char*name,const char*impname
  3342. ){lua_pushstring(L,name);*newfile(L)=f;if(impname){lua_pushstring(L,impname);
  3343. lua_pushvalue(L,-2);lua_settable(L,-6);}lua_settable(L,-3);}static int
  3344. aux_close(lua_State*L){FILE*f=tofile(L,1);if(f==stdin||f==stdout||f==stderr)
  3345. return 0;else{int ok=(pclose(f)!=-1)||(fclose(f)==0);if(ok)*(FILE**)
  3346. lua_touserdata(L,1)=NULL;return ok;}}static int io_close(lua_State*L){if(
  3347. lua_isnone(L,1)&&lua_type(L,lua_upvalueindex(1))==LUA_TTABLE){lua_pushstring(L
  3348. ,IO_OUTPUT);lua_rawget(L,lua_upvalueindex(1));}return pushresult(L,aux_close(L
  3349. ),NULL);}static int io_gc(lua_State*L){FILE**f=topfile(L,1);if(*f!=NULL)
  3350. aux_close(L);return 0;}static int io_tostring(lua_State*L){char buff[128];FILE
  3351. **f=topfile(L,1);if(*f==NULL)strcpy(buff,"closed");else sprintf(buff,"%p",
  3352. lua_touserdata(L,1));lua_pushfstring(L,"file (%s)",buff);return 1;}static int
  3353. io_open(lua_State*L){const char*filename=luaL_checkstring(L,1);const char*mode
  3354. =luaL_optstring(L,2,"r");FILE**pf=newfile(L);*pf=fopen(filename,mode);return(*
  3355. pf==NULL)?pushresult(L,0,filename):1;}static int io_popen(lua_State*L){
  3356. #if !USE_POPEN
  3357. luaL_error(L,"`popen' not supported");return 0;
  3358. #else
  3359. const char*filename=luaL_checkstring(L,1);const char*mode=luaL_optstring(L,2,
  3360. "r");FILE**pf=newfile(L);*pf=popen(filename,mode);return(*pf==NULL)?pushresult
  3361. (L,0,filename):1;
  3362. #endif
  3363. }static int io_tmpfile(lua_State*L){FILE**pf=newfile(L);*pf=tmpfile();return(*
  3364. pf==NULL)?pushresult(L,0,NULL):1;}static FILE*getiofile(lua_State*L,const char
  3365. *name){lua_pushstring(L,name);lua_rawget(L,lua_upvalueindex(1));return tofile(
  3366. L,-1);}static int g_iofile(lua_State*L,const char*name,const char*mode){if(!
  3367. lua_isnoneornil(L,1)){const char*filename=lua_tostring(L,1);lua_pushstring(L,
  3368. name);if(filename){FILE**pf=newfile(L);*pf=fopen(filename,mode);if(*pf==NULL){
  3369. lua_pushfstring(L,"%s: %s",filename,strerror(errno));luaL_argerror(L,1,
  3370. lua_tostring(L,-1));}}else{tofile(L,1);lua_pushvalue(L,1);}lua_rawset(L,
  3371. lua_upvalueindex(1));}lua_pushstring(L,name);lua_rawget(L,lua_upvalueindex(1))
  3372. ;return 1;}static int io_input(lua_State*L){return g_iofile(L,IO_INPUT,"r");}
  3373. static int io_output(lua_State*L){return g_iofile(L,IO_OUTPUT,"w");}static int
  3374. io_readline(lua_State*L);static void aux_lines(lua_State*L,int idx,int close)
  3375. {lua_pushliteral(L,FILEHANDLE);lua_rawget(L,LUA_REGISTRYINDEX);lua_pushvalue(L
  3376. ,idx);lua_pushboolean(L,close);lua_pushcclosure(L,io_readline,3);}static int
  3377. f_lines(lua_State*L){tofile(L,1);aux_lines(L,1,0);return 1;}static int
  3378. io_lines(lua_State*L){if(lua_isnoneornil(L,1)){lua_pushstring(L,IO_INPUT);
  3379. lua_rawget(L,lua_upvalueindex(1));return f_lines(L);}else{const char*filename=
  3380. luaL_checkstring(L,1);FILE**pf=newfile(L);*pf=fopen(filename,"r");
  3381. luaL_argcheck(L,*pf,1,strerror(errno));aux_lines(L,lua_gettop(L),1);return 1;}
  3382. }static int read_number(lua_State*L,FILE*f){lua_Number d;if(fscanf(f,
  3383. LUA_NUMBER_SCAN,&d)==1){lua_pushnumber(L,d);return 1;}else return 0;}static
  3384. int test_eof(lua_State*L,FILE*f){int c=getc(f);ungetc(c,f);lua_pushlstring(L,
  3385. NULL,0);return(c!=EOF);}static int read_line(lua_State*L,FILE*f){luaL_Buffer b
  3386. ;luaL_buffinit(L,&b);for(;;){size_t l;char*p=luaL_prepbuffer(&b);if(fgets(p,
  3387. LUAL_BUFFERSIZE,f)==NULL){luaL_pushresult(&b);return(lua_strlen(L,-1)>0);}l=
  3388. strlen(p);if(p[l-1]!='\n')luaL_addsize(&b,l);else{luaL_addsize(&b,l-1);
  3389. luaL_pushresult(&b);return 1;}}}static int read_chars(lua_State*L,FILE*f,
  3390. size_t n){size_t rlen;size_t nr;luaL_Buffer b;luaL_buffinit(L,&b);rlen=
  3391. LUAL_BUFFERSIZE;do{char*p=luaL_prepbuffer(&b);if(rlen>n)rlen=n;nr=fread(p,
  3392. sizeof(char),rlen,f);luaL_addsize(&b,nr);n-=nr;}while(n>0&&nr==rlen);
  3393. luaL_pushresult(&b);return(n==0||lua_strlen(L,-1)>0);}static int g_read(
  3394. lua_State*L,FILE*f,int first){int nargs=lua_gettop(L)-1;int success;int n;if(
  3395. nargs==0){success=read_line(L,f);n=first+1;}else{luaL_checkstack(L,nargs+
  3396. LUA_MINSTACK,"too many arguments");success=1;for(n=first;nargs--&&success;n++)
  3397. {if(lua_type(L,n)==LUA_TNUMBER){size_t l=(size_t)lua_tonumber(L,n);success=(l
  3398. ==0)?test_eof(L,f):read_chars(L,f,l);}else{const char*p=lua_tostring(L,n);
  3399. luaL_argcheck(L,p&&p[0]=='*',n,"invalid option");switch(p[1]){case'n':success=
  3400. read_number(L,f);break;case'l':success=read_line(L,f);break;case'a':read_chars
  3401. (L,f,~((size_t)0));success=1;break;case'w':return luaL_error(L,
  3402. "obsolete option `*w' to `read'");default:return luaL_argerror(L,n,
  3403. "invalid format");}}}}if(!success){lua_pop(L,1);lua_pushnil(L);}return n-first
  3404. ;}static int io_read(lua_State*L){return g_read(L,getiofile(L,IO_INPUT),1);}
  3405. static int f_read(lua_State*L){return g_read(L,tofile(L,1),2);}static int
  3406. io_readline(lua_State*L){FILE*f=*(FILE**)lua_touserdata(L,lua_upvalueindex(2))
  3407. ;if(f==NULL)luaL_error(L,"file is already closed");if(read_line(L,f))return 1;
  3408. else{if(lua_toboolean(L,lua_upvalueindex(3))){lua_settop(L,0);lua_pushvalue(L,
  3409. lua_upvalueindex(2));aux_close(L);}return 0;}}static int g_write(lua_State*L,
  3410. FILE*f,int arg){int nargs=lua_gettop(L)-1;int status=1;for(;nargs--;arg++){if(
  3411. lua_type(L,arg)==LUA_TNUMBER){status=status&&fprintf(f,LUA_NUMBER_FMT,
  3412. lua_tonumber(L,arg))>0;}else{size_t l;const char*s=luaL_checklstring(L,arg,&l)
  3413. ;status=status&&(fwrite(s,sizeof(char),l,f)==l);}}return pushresult(L,status,
  3414. NULL);}static int io_write(lua_State*L){return g_write(L,getiofile(L,IO_OUTPUT
  3415. ),1);}static int f_write(lua_State*L){return g_write(L,tofile(L,1),2);}static
  3416. int f_seek(lua_State*L){static const int mode[]={SEEK_SET,SEEK_CUR,SEEK_END};
  3417. static const char*const modenames[]={"set","cur","end",NULL};FILE*f=tofile(L,1
  3418. );int op=luaL_findstring(luaL_optstring(L,2,"cur"),modenames);long offset=
  3419. luaL_optlong(L,3,0);luaL_argcheck(L,op!=-1,2,"invalid mode");op=fseek(f,offset
  3420. ,mode[op]);if(op)return pushresult(L,0,NULL);else{lua_pushnumber(L,ftell(f));
  3421. return 1;}}static int io_flush(lua_State*L){return pushresult(L,fflush(
  3422. getiofile(L,IO_OUTPUT))==0,NULL);}static int f_flush(lua_State*L){return
  3423. pushresult(L,fflush(tofile(L,1))==0,NULL);}static const luaL_reg iolib[]={{
  3424. "input",io_input},{"output",io_output},{"lines",io_lines},{"close",io_close},{
  3425. "flush",io_flush},{"open",io_open},{"popen",io_popen},{"read",io_read},{
  3426. "tmpfile",io_tmpfile},{"type",io_type},{"write",io_write},{NULL,NULL}};static
  3427. const luaL_reg flib[]={{"flush",f_flush},{"read",f_read},{"lines",f_lines},{
  3428. "seek",f_seek},{"write",f_write},{"close",io_close},{"__gc",io_gc},{
  3429. "__tostring",io_tostring},{NULL,NULL}};static void createmeta(lua_State*L){
  3430. luaL_newmetatable(L,FILEHANDLE);lua_pushliteral(L,"__index");lua_pushvalue(L,-
  3431. 2);lua_rawset(L,-3);luaL_openlib(L,NULL,flib,0);}static int io_execute(
  3432. lua_State*L){lua_pushnumber(L,system(luaL_checkstring(L,1)));return 1;}static
  3433. int io_remove(lua_State*L){const char*filename=luaL_checkstring(L,1);return
  3434. pushresult(L,remove(filename)==0,filename);}static int io_rename(lua_State*L){
  3435. const char*fromname=luaL_checkstring(L,1);const char*toname=luaL_checkstring(L
  3436. ,2);return pushresult(L,rename(fromname,toname)==0,fromname);}static int
  3437. io_tmpname(lua_State*L){
  3438. #if !USE_TMPNAME
  3439. luaL_error(L,"`tmpname' not supported");return 0;
  3440. #else
  3441. char buff[L_tmpnam];if(tmpnam(buff)!=buff)return luaL_error(L,
  3442. "unable to generate a unique filename in `tmpname'");lua_pushstring(L,buff);
  3443. return 1;
  3444. #endif
  3445. }static int io_getenv(lua_State*L){lua_pushstring(L,getenv(luaL_checkstring(L,
  3446. 1)));return 1;}static int io_clock(lua_State*L){lua_pushnumber(L,((lua_Number)
  3447. clock())/(lua_Number)CLOCKS_PER_SEC);return 1;}static void setfield(lua_State*
  3448. L,const char*key,int value){lua_pushstring(L,key);lua_pushnumber(L,value);
  3449. lua_rawset(L,-3);}static void setboolfield(lua_State*L,const char*key,int
  3450. value){lua_pushstring(L,key);lua_pushboolean(L,value);lua_rawset(L,-3);}static
  3451. int getboolfield(lua_State*L,const char*key){int res;lua_pushstring(L,key);
  3452. lua_gettable(L,-2);res=lua_toboolean(L,-1);lua_pop(L,1);return res;}static int
  3453. getfield(lua_State*L,const char*key,int d){int res;lua_pushstring(L,key);
  3454. lua_gettable(L,-2);if(lua_isnumber(L,-1))res=(int)(lua_tonumber(L,-1));else{if
  3455. (d==-2)return luaL_error(L,"field `%s' missing in date table",key);res=d;}
  3456. lua_pop(L,1);return res;}static int io_date(lua_State*L){const char*s=
  3457. luaL_optstring(L,1,"%c");time_t t=(time_t)(luaL_optnumber(L,2,-1));struct tm*
  3458. stm;if(t==(time_t)(-1))t=time(NULL);if(*s=='!'){stm=gmtime(&t);s++;}else stm=
  3459. localtime(&t);if(stm==NULL)lua_pushnil(L);else if(strcmp(s,"*t")==0){
  3460. lua_newtable(L);setfield(L,"sec",stm->tm_sec);setfield(L,"min",stm->tm_min);
  3461. setfield(L,"hour",stm->tm_hour);setfield(L,"day",stm->tm_mday);setfield(L,
  3462. "month",stm->tm_mon+1);setfield(L,"year",stm->tm_year+1900);setfield(L,"wday",
  3463. stm->tm_wday+1);setfield(L,"yday",stm->tm_yday+1);setboolfield(L,"isdst",stm->
  3464. tm_isdst);}else{char b[256];if(strftime(b,sizeof(b),s,stm))lua_pushstring(L,b)
  3465. ;else return luaL_error(L,"`date' format too long");}return 1;}static int
  3466. io_time(lua_State*L){if(lua_isnoneornil(L,1))lua_pushnumber(L,time(NULL));else
  3467. {time_t t;struct tm ts;luaL_checktype(L,1,LUA_TTABLE);lua_settop(L,1);ts.
  3468. tm_sec=getfield(L,"sec",0);ts.tm_min=getfield(L,"min",0);ts.tm_hour=getfield(L
  3469. ,"hour",12);ts.tm_mday=getfield(L,"day",-2);ts.tm_mon=getfield(L,"month",-2)-1
  3470. ;ts.tm_year=getfield(L,"year",-2)-1900;ts.tm_isdst=getboolfield(L,"isdst");t=
  3471. mktime(&ts);if(t==(time_t)(-1))lua_pushnil(L);else lua_pushnumber(L,t);}return
  3472. 1;}static int io_difftime(lua_State*L){lua_pushnumber(L,difftime((time_t)(
  3473. luaL_checknumber(L,1)),(time_t)(luaL_optnumber(L,2,0))));return 1;}static int
  3474. io_setloc(lua_State*L){static const int cat[]={LC_ALL,LC_COLLATE,LC_CTYPE,
  3475. LC_MONETARY,LC_NUMERIC,LC_TIME};static const char*const catnames[]={"all",
  3476. "collate","ctype","monetary","numeric","time",NULL};const char*l=lua_tostring(
  3477. L,1);int op=luaL_findstring(luaL_optstring(L,2,"all"),catnames);luaL_argcheck(
  3478. L,l||lua_isnoneornil(L,1),1,"string expected");luaL_argcheck(L,op!=-1,2,
  3479. "invalid option");lua_pushstring(L,setlocale(cat[op],l));return 1;}static int
  3480. io_exit(lua_State*L){exit(luaL_optint(L,1,EXIT_SUCCESS));return 0;}static
  3481. const luaL_reg syslib[]={{"clock",io_clock},{"date",io_date},{"difftime",
  3482. io_difftime},{"execute",io_execute},{"exit",io_exit},{"getenv",io_getenv},{
  3483. "remove",io_remove},{"rename",io_rename},{"setlocale",io_setloc},{"time",
  3484. io_time},{"tmpname",io_tmpname},{NULL,NULL}};LUALIB_API int luaopen_io(
  3485. lua_State*L){luaL_openlib(L,LUA_OSLIBNAME,syslib,0);createmeta(L);
  3486. lua_pushvalue(L,-1);luaL_openlib(L,LUA_IOLIBNAME,iolib,1);registerfile(L,stdin
  3487. ,"stdin",IO_INPUT);registerfile(L,stdout,"stdout",IO_OUTPUT);registerfile(L,
  3488. stderr,"stderr",NULL);return 1;}
  3489. #line 1 "llex.c"
  3490. #define llex_c
  3491. #define next(LS) (LS->current=zgetc(LS->z))
  3492. static const char*const token2string[]={"and","break","do","else","elseif",
  3493. "end","false","for","function","if","in","local","nil","not","or","repeat",
  3494. "return","then","true","until","while","*name","..","...","==",">=","<=","~=",
  3495. "*number","*string","<eof>"};void luaX_init(lua_State*L){int i;for(i=0;i<
  3496. NUM_RESERVED;i++){TString*ts=luaS_new(L,token2string[i]);luaS_fix(ts);
  3497. lua_assert(strlen(token2string[i])+1<=TOKEN_LEN);ts->tsv.reserved=cast(lu_byte
  3498. ,i+1);}}
  3499. #define MAXSRC 80
  3500. void luaX_checklimit(LexState*ls,int val,int limit,const char*msg){if(val>
  3501. limit){msg=luaO_pushfstring(ls->L,"too many %s (limit=%d)",msg,limit);
  3502. luaX_syntaxerror(ls,msg);}}void luaX_errorline(LexState*ls,const char*s,const
  3503. char*token,int line){lua_State*L=ls->L;char buff[MAXSRC];luaO_chunkid(buff,
  3504. getstr(ls->source),MAXSRC);luaO_pushfstring(L,"%s:%d: %s near `%s'",buff,line,
  3505. s,token);luaD_throw(L,LUA_ERRSYNTAX);}static void luaX_error(LexState*ls,const
  3506. char*s,const char*token){luaX_errorline(ls,s,token,ls->linenumber);}void
  3507. luaX_syntaxerror(LexState*ls,const char*msg){const char*lasttoken;switch(ls->t
  3508. .token){case TK_NAME:lasttoken=getstr(ls->t.seminfo.ts);break;case TK_STRING:
  3509. case TK_NUMBER:lasttoken=luaZ_buffer(ls->buff);break;default:lasttoken=
  3510. luaX_token2str(ls,ls->t.token);break;}luaX_error(ls,msg,lasttoken);}const char
  3511. *luaX_token2str(LexState*ls,int token){if(token<FIRST_RESERVED){lua_assert(
  3512. token==(unsigned char)token);return luaO_pushfstring(ls->L,"%c",token);}else
  3513. return token2string[token-FIRST_RESERVED];}static void luaX_lexerror(LexState*
  3514. ls,const char*s,int token){if(token==TK_EOS)luaX_error(ls,s,luaX_token2str(ls,
  3515. token));else luaX_error(ls,s,luaZ_buffer(ls->buff));}static void inclinenumber
  3516. (LexState*LS){next(LS);++LS->linenumber;luaX_checklimit(LS,LS->linenumber,
  3517. MAX_INT,"lines in a chunk");}void luaX_setinput(lua_State*L,LexState*LS,ZIO*z,
  3518. TString*source){LS->L=L;LS->lookahead.token=TK_EOS;LS->z=z;LS->fs=NULL;LS->
  3519. linenumber=1;LS->lastline=1;LS->source=source;next(LS);if(LS->current=='#'){do
  3520. {next(LS);}while(LS->current!='\n'&&LS->current!=EOZ);}}
  3521. #define EXTRABUFF 32
  3522. #define MAXNOCHECK 5
  3523. #define checkbuffer(LS, len)if(((len)+MAXNOCHECK)*sizeof(char)>luaZ_sizebuffer\
  3524. ((LS)->buff))luaZ_openspace((LS)->L,(LS)->buff,(len)+EXTRABUFF)
  3525. #define save(LS, c,l)(luaZ_buffer((LS)->buff)[l++]=cast(char,c))
  3526. #define save_and_next(LS, l)(save(LS,LS->current,l),next(LS))
  3527. static size_t readname(LexState*LS){size_t l=0;checkbuffer(LS,l);do{
  3528. checkbuffer(LS,l);save_and_next(LS,l);}while(isalnum(LS->current)||LS->current
  3529. =='_');save(LS,'\0',l);return l-1;}static void read_numeral(LexState*LS,int
  3530. comma,SemInfo*seminfo){size_t l=0;checkbuffer(LS,l);if(comma)save(LS,'.',l);
  3531. while(isdigit(LS->current)){checkbuffer(LS,l);save_and_next(LS,l);}if(LS->
  3532. current=='.'){save_and_next(LS,l);if(LS->current=='.'){save_and_next(LS,l);
  3533. save(LS,'\0',l);luaX_lexerror(LS,
  3534. "ambiguous syntax (decimal point x string concatenation)",TK_NUMBER);}}while(
  3535. isdigit(LS->current)){checkbuffer(LS,l);save_and_next(LS,l);}if(LS->current==
  3536. 'e'||LS->current=='E'){save_and_next(LS,l);if(LS->current=='+'||LS->current==
  3537. '-')save_and_next(LS,l);while(isdigit(LS->current)){checkbuffer(LS,l);
  3538. save_and_next(LS,l);}}save(LS,'\0',l);if(!luaO_str2d(luaZ_buffer(LS->buff),&
  3539. seminfo->r))luaX_lexerror(LS,"malformed number",TK_NUMBER);}static void
  3540. read_long_string(LexState*LS,SemInfo*seminfo){int cont=0;size_t l=0;
  3541. checkbuffer(LS,l);save(LS,'[',l);save_and_next(LS,l);if(LS->current=='\n')
  3542. inclinenumber(LS);for(;;){checkbuffer(LS,l);switch(LS->current){case EOZ:save(
  3543. LS,'\0',l);luaX_lexerror(LS,(seminfo)?"unfinished long string":
  3544. "unfinished long comment",TK_EOS);break;case'[':save_and_next(LS,l);if(LS->
  3545. current=='['){cont++;save_and_next(LS,l);}continue;case']':save_and_next(LS,l)
  3546. ;if(LS->current==']'){if(cont==0)goto endloop;cont--;save_and_next(LS,l);}
  3547. continue;case'\n':save(LS,'\n',l);inclinenumber(LS);if(!seminfo)l=0;continue;
  3548. default:save_and_next(LS,l);}}endloop:save_and_next(LS,l);save(LS,'\0',l);if(
  3549. seminfo)seminfo->ts=luaS_newlstr(LS->L,luaZ_buffer(LS->buff)+2,l-5);}static
  3550. void read_string(LexState*LS,int del,SemInfo*seminfo){size_t l=0;checkbuffer(
  3551. LS,l);save_and_next(LS,l);while(LS->current!=del){checkbuffer(LS,l);switch(LS
  3552. ->current){case EOZ:save(LS,'\0',l);luaX_lexerror(LS,"unfinished string",
  3553. TK_EOS);break;case'\n':save(LS,'\0',l);luaX_lexerror(LS,"unfinished string",
  3554. TK_STRING);break;case'\\':next(LS);switch(LS->current){case'a':save(LS,'\a',l)
  3555. ;next(LS);break;case'b':save(LS,'\b',l);next(LS);break;case'f':save(LS,'\f',l)
  3556. ;next(LS);break;case'n':save(LS,'\n',l);next(LS);break;case'r':save(LS,'\r',l)
  3557. ;next(LS);break;case't':save(LS,'\t',l);next(LS);break;case'v':save(LS,'\v',l)
  3558. ;next(LS);break;case'\n':save(LS,'\n',l);inclinenumber(LS);break;case EOZ:
  3559. break;default:{if(!isdigit(LS->current))save_and_next(LS,l);else{int c=0;int i
  3560. =0;do{c=10*c+(LS->current-'0');next(LS);}while(++i<3&&isdigit(LS->current));if
  3561. (c>UCHAR_MAX){save(LS,'\0',l);luaX_lexerror(LS,"escape sequence too large",
  3562. TK_STRING);}save(LS,c,l);}}}break;default:save_and_next(LS,l);}}save_and_next(
  3563. LS,l);save(LS,'\0',l);seminfo->ts=luaS_newlstr(LS->L,luaZ_buffer(LS->buff)+1,l
  3564. -3);}int luaX_lex(LexState*LS,SemInfo*seminfo){for(;;){switch(LS->current){
  3565. case'\n':{inclinenumber(LS);continue;}case'-':{next(LS);if(LS->current!='-')
  3566. return'-';next(LS);if(LS->current=='['&&(next(LS),LS->current=='['))
  3567. read_long_string(LS,NULL);else while(LS->current!='\n'&&LS->current!=EOZ)next(
  3568. LS);continue;}case'[':{next(LS);if(LS->current!='[')return'[';else{
  3569. read_long_string(LS,seminfo);return TK_STRING;}}case'=':{next(LS);if(LS->
  3570. current!='=')return'=';else{next(LS);return TK_EQ;}}case'<':{next(LS);if(LS->
  3571. current!='=')return'<';else{next(LS);return TK_LE;}}case'>':{next(LS);if(LS->
  3572. current!='=')return'>';else{next(LS);return TK_GE;}}case'~':{next(LS);if(LS->
  3573. current!='=')return'~';else{next(LS);return TK_NE;}}case'"':case'\'':{
  3574. read_string(LS,LS->current,seminfo);return TK_STRING;}case'.':{next(LS);if(LS
  3575. ->current=='.'){next(LS);if(LS->current=='.'){next(LS);return TK_DOTS;}else
  3576. return TK_CONCAT;}else if(!isdigit(LS->current))return'.';else{read_numeral(LS
  3577. ,1,seminfo);return TK_NUMBER;}}case EOZ:{return TK_EOS;}default:{if(isspace(LS
  3578. ->current)){next(LS);continue;}else if(isdigit(LS->current)){read_numeral(LS,0
  3579. ,seminfo);return TK_NUMBER;}else if(isalpha(LS->current)||LS->current=='_'){
  3580. size_t l=readname(LS);TString*ts=luaS_newlstr(LS->L,luaZ_buffer(LS->buff),l);
  3581. if(ts->tsv.reserved>0)return ts->tsv.reserved-1+FIRST_RESERVED;seminfo->ts=ts;
  3582. return TK_NAME;}else{int c=LS->current;if(iscntrl(c))luaX_error(LS,
  3583. "invalid control char",luaO_pushfstring(LS->L,"char(%d)",c));next(LS);return c
  3584. ;}}}}}
  3585. #undef next
  3586. #line 1 "lmem.c"
  3587. #define lmem_c
  3588. #ifndef l_realloc
  3589. #define l_realloc(b,os,s) realloc(b,s)
  3590. #endif
  3591. #ifndef l_free
  3592. #define l_free(b,os) free(b)
  3593. #endif
  3594. #define MINSIZEARRAY 4
  3595. void*luaM_growaux(lua_State*L,void*block,int*size,int size_elems,int limit,
  3596. const char*errormsg){void*newblock;int newsize=(*size)*2;if(newsize<
  3597. MINSIZEARRAY)newsize=MINSIZEARRAY;else if(*size>=limit/2){if(*size<limit-
  3598. MINSIZEARRAY)newsize=limit;else luaG_runerror(L,errormsg);}newblock=
  3599. luaM_realloc(L,block,cast(lu_mem,*size)*cast(lu_mem,size_elems),cast(lu_mem,
  3600. newsize)*cast(lu_mem,size_elems));*size=newsize;return newblock;}void*
  3601. luaM_realloc(lua_State*L,void*block,lu_mem oldsize,lu_mem size){lua_assert((
  3602. oldsize==0)==(block==NULL));if(size==0){if(block!=NULL){l_free(block,oldsize);
  3603. block=NULL;}else return NULL;}else if(size>=MAX_SIZET)luaG_runerror(L,
  3604. "memory allocation error: block too big");else{block=l_realloc(block,oldsize,
  3605. size);if(block==NULL){if(L)luaD_throw(L,LUA_ERRMEM);else return NULL;}}if(L){
  3606. lua_assert(G(L)!=NULL&&G(L)->nblocks>0);G(L)->nblocks-=oldsize;G(L)->nblocks+=
  3607. size;}return block;}
  3608. #line 1 "loadlib.c"
  3609. #undef LOADLIB
  3610. #ifdef USE_DLOPEN
  3611. #define LOADLIB
  3612. #include <dlfcn.h> /* dg: magic anchor comment */
  3613. static int loadlib(lua_State*L){const char*path=luaL_checkstring(L,1);const
  3614. char*init=luaL_checkstring(L,2);void*lib=dlopen(path,RTLD_NOW);if(lib!=NULL){
  3615. lua_CFunction f=(lua_CFunction)dlsym(lib,init);if(f!=NULL){
  3616. lua_pushlightuserdata(L,lib);lua_pushcclosure(L,f,1);return 1;}}lua_pushnil(L)
  3617. ;lua_pushstring(L,dlerror());lua_pushstring(L,(lib!=NULL)?"init":"open");if(
  3618. lib!=NULL)dlclose(lib);return 3;}
  3619. #endif
  3620. #ifndef USE_DLL
  3621. #ifdef _WIN32
  3622. #define USE_DLL 1
  3623. #else
  3624. #define USE_DLL 0
  3625. #endif
  3626. #endif
  3627. #if USE_DLL
  3628. #define LOADLIB
  3629. #include <windows.h> /* dg: magic anchor comment */
  3630. static void pusherror(lua_State*L){int error=GetLastError();char buffer[128];
  3631. if(FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,0,
  3632. error,0,buffer,sizeof(buffer),0))lua_pushstring(L,buffer);else lua_pushfstring
  3633. (L,"system error %d\n",error);}static int loadlib(lua_State*L){const char*path
  3634. =luaL_checkstring(L,1);const char*init=luaL_checkstring(L,2);HINSTANCE lib=
  3635. LoadLibrary(path);if(lib!=NULL){lua_CFunction f=(lua_CFunction)GetProcAddress(
  3636. lib,init);if(f!=NULL){lua_pushlightuserdata(L,lib);lua_pushcclosure(L,f,1);
  3637. return 1;}}lua_pushnil(L);pusherror(L);lua_pushstring(L,(lib!=NULL)?"init":
  3638. "open");if(lib!=NULL)FreeLibrary(lib);return 3;}
  3639. #endif
  3640. #ifndef LOADLIB
  3641. #ifdef linux
  3642. #define LOADLIB
  3643. #endif
  3644. #ifdef sun
  3645. #define LOADLIB
  3646. #endif
  3647. #ifdef sgi
  3648. #define LOADLIB
  3649. #endif
  3650. #ifdef BSD
  3651. #define LOADLIB
  3652. #endif
  3653. #ifdef _WIN32
  3654. #define LOADLIB
  3655. #endif
  3656. #ifdef LOADLIB
  3657. #undef LOADLIB
  3658. #define LOADLIB "`loadlib' not installed (check your Lua configuration)"
  3659. #else
  3660. #define LOADLIB "`loadlib' not supported"
  3661. #endif
  3662. static int loadlib(lua_State*L){lua_pushnil(L);lua_pushliteral(L,LOADLIB);
  3663. lua_pushliteral(L,"absent");return 3;}
  3664. #endif
  3665. LUALIB_API int luaopen_loadlib(lua_State*L){lua_register(L,"loadlib",loadlib);
  3666. return 0;}
  3667. #line 1 "lobject.c"
  3668. #define lobject_c
  3669. #ifndef lua_str2number
  3670. #define lua_str2number(s,p) strtod((s),(p))
  3671. #endif
  3672. const TObject luaO_nilobject={LUA_TNIL,{NULL}};int luaO_int2fb(unsigned int x)
  3673. {int m=0;while(x>=(1<<3)){x=(x+1)>>1;m++;}return(m<<3)|cast(int,x);}int
  3674. luaO_log2(unsigned int x){static const lu_byte log_8[255]={0,1,1,2,2,2,2,3,3,3
  3675. ,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
  3676. ,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
  3677. ,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
  3678. ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  3679. ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  3680. ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  3681. ,7,7,7,7,7,7,7,7,7,7,7};if(x>=0x00010000){if(x>=0x01000000)return log_8[((x>>
  3682. 24)&0xff)-1]+24;else return log_8[((x>>16)&0xff)-1]+16;}else{if(x>=0x00000100)
  3683. return log_8[((x>>8)&0xff)-1]+8;else if(x)return log_8[(x&0xff)-1];return-1;}}
  3684. int luaO_rawequalObj(const TObject*t1,const TObject*t2){if(ttype(t1)!=ttype(t2
  3685. ))return 0;else switch(ttype(t1)){case LUA_TNIL:return 1;case LUA_TNUMBER:
  3686. return nvalue(t1)==nvalue(t2);case LUA_TBOOLEAN:return bvalue(t1)==bvalue(t2);
  3687. case LUA_TLIGHTUSERDATA:return pvalue(t1)==pvalue(t2);default:lua_assert(
  3688. iscollectable(t1));return gcvalue(t1)==gcvalue(t2);}}int luaO_str2d(const char
  3689. *s,lua_Number*result){char*endptr;lua_Number res=lua_str2number(s,&endptr);if(
  3690. endptr==s)return 0;while(isspace((unsigned char)(*endptr)))endptr++;if(*endptr
  3691. !='\0')return 0;*result=res;return 1;}static void pushstr(lua_State*L,const
  3692. char*str){setsvalue2s(L->top,luaS_new(L,str));incr_top(L);}const char*
  3693. luaO_pushvfstring(lua_State*L,const char*fmt,va_list argp){int n=1;pushstr(L,
  3694. "");for(;;){const char*e=strchr(fmt,'%');if(e==NULL)break;setsvalue2s(L->top,
  3695. luaS_newlstr(L,fmt,e-fmt));incr_top(L);switch(*(e+1)){case's':pushstr(L,va_arg
  3696. (argp,char*));break;case'c':{char buff[2];buff[0]=cast(char,va_arg(argp,int));
  3697. buff[1]='\0';pushstr(L,buff);break;}case'd':setnvalue(L->top,cast(lua_Number,
  3698. va_arg(argp,int)));incr_top(L);break;case'f':setnvalue(L->top,cast(lua_Number,
  3699. va_arg(argp,l_uacNumber)));incr_top(L);break;case'%':pushstr(L,"%");break;
  3700. default:lua_assert(0);}n+=2;fmt=e+2;}pushstr(L,fmt);luaV_concat(L,n+1,L->top-L
  3701. ->base-1);L->top-=n;return svalue(L->top-1);}const char*luaO_pushfstring(
  3702. lua_State*L,const char*fmt,...){const char*msg;va_list argp;va_start(argp,fmt)
  3703. ;msg=luaO_pushvfstring(L,fmt,argp);va_end(argp);return msg;}void luaO_chunkid(
  3704. char*out,const char*source,int bufflen){if(*source=='='){strncpy(out,source+1,
  3705. bufflen);out[bufflen-1]='\0';}else{if(*source=='@'){int l;source++;bufflen-=
  3706. sizeof(" `...' ");l=strlen(source);strcpy(out,"");if(l>bufflen){source+=(l-
  3707. bufflen);strcat(out,"...");}strcat(out,source);}else{int len=strcspn(source,
  3708. "\n");bufflen-=sizeof(" [string \"...\"] ");if(len>bufflen)len=bufflen;strcpy(
  3709. out,"[string \"");if(source[len]!='\0'){strncat(out,source,len);strcat(out,
  3710. "...");}else strcat(out,source);strcat(out,"\"]");}}}
  3711. #line 1 "lopcodes.c"
  3712. #define lopcodes_c
  3713. #ifdef LUA_OPNAMES
  3714. const char*const luaP_opnames[]={"MOVE","LOADK","LOADBOOL","LOADNIL",
  3715. "GETUPVAL","GETGLOBAL","GETTABLE","SETGLOBAL","SETUPVAL","SETTABLE","NEWTABLE"
  3716. ,"SELF","ADD","SUB","MUL","DIV","POW","UNM","NOT","CONCAT","JMP","EQ","LT",
  3717. "LE","TEST","CALL","TAILCALL","RETURN","FORLOOP","TFORLOOP","TFORPREP",
  3718. "SETLIST","SETLISTO","CLOSE","CLOSURE"};
  3719. #endif
  3720. #define opmode(t,b,bk,ck,sa,k,m) (((t)<<OpModeT)|((b)<<OpModeBreg)|((bk)<<\
  3721. OpModeBrk)|((ck)<<OpModeCrk)|((sa)<<OpModesetA)|((k)<<OpModeK)|(m))
  3722. const lu_byte luaP_opmodes[NUM_OPCODES]={opmode(0,1,0,0,1,0,iABC),opmode(0,0,0
  3723. ,0,1,1,iABx),opmode(0,0,0,0,1,0,iABC),opmode(0,1,0,0,1,0,iABC),opmode(0,0,0,0,
  3724. 1,0,iABC),opmode(0,0,0,0,1,1,iABx),opmode(0,1,0,1,1,0,iABC),opmode(0,0,0,0,0,1
  3725. ,iABx),opmode(0,0,0,0,0,0,iABC),opmode(0,0,1,1,0,0,iABC),opmode(0,0,0,0,1,0,
  3726. iABC),opmode(0,1,0,1,1,0,iABC),opmode(0,0,1,1,1,0,iABC),opmode(0,0,1,1,1,0,
  3727. iABC),opmode(0,0,1,1,1,0,iABC),opmode(0,0,1,1,1,0,iABC),opmode(0,0,1,1,1,0,
  3728. iABC),opmode(0,1,0,0,1,0,iABC),opmode(0,1,0,0,1,0,iABC),opmode(0,1,0,1,1,0,
  3729. iABC),opmode(0,0,0,0,0,0,iAsBx),opmode(1,0,1,1,0,0,iABC),opmode(1,0,1,1,0,0,
  3730. iABC),opmode(1,0,1,1,0,0,iABC),opmode(1,1,0,0,1,0,iABC),opmode(0,0,0,0,0,0,
  3731. iABC),opmode(0,0,0,0,0,0,iABC),opmode(0,0,0,0,0,0,iABC),opmode(0,0,0,0,0,0,
  3732. iAsBx),opmode(1,0,0,0,0,0,iABC),opmode(0,0,0,0,0,0,iAsBx),opmode(0,0,0,0,0,0,
  3733. iABx),opmode(0,0,0,0,0,0,iABx),opmode(0,0,0,0,0,0,iABC),opmode(0,0,0,0,1,0,
  3734. iABx)};
  3735. #line 1 "lparser.c"
  3736. #define lparser_c
  3737. #define getlocvar(fs, i)((fs)->f->locvars[(fs)->actvar[i]])
  3738. #define enterlevel(ls) if(++(ls)->nestlevel>LUA_MAXPARSERLEVEL)\
  3739. luaX_syntaxerror(ls,"too many syntax levels");
  3740. #define leavelevel(ls) ((ls)->nestlevel--)
  3741. typedef struct BlockCnt{struct BlockCnt*previous;int breaklist;int nactvar;int
  3742. upval;int isbreakable;}BlockCnt;static void chunk(LexState*ls);static void
  3743. expr(LexState*ls,expdesc*v);static void next(LexState*ls){ls->lastline=ls->
  3744. linenumber;if(ls->lookahead.token!=TK_EOS){ls->t=ls->lookahead;ls->lookahead.
  3745. token=TK_EOS;}else ls->t.token=luaX_lex(ls,&ls->t.seminfo);}static void
  3746. lookahead(LexState*ls){lua_assert(ls->lookahead.token==TK_EOS);ls->lookahead.
  3747. token=luaX_lex(ls,&ls->lookahead.seminfo);}static void error_expected(LexState
  3748. *ls,int token){luaX_syntaxerror(ls,luaO_pushfstring(ls->L,"`%s' expected",
  3749. luaX_token2str(ls,token)));}static int testnext(LexState*ls,int c){if(ls->t.
  3750. token==c){next(ls);return 1;}else return 0;}static void check(LexState*ls,int
  3751. c){if(!testnext(ls,c))error_expected(ls,c);}
  3752. #define check_condition(ls,c,msg) {if(!(c))luaX_syntaxerror(ls,msg);}
  3753. static void check_match(LexState*ls,int what,int who,int where){if(!testnext(
  3754. ls,what)){if(where==ls->linenumber)error_expected(ls,what);else{
  3755. luaX_syntaxerror(ls,luaO_pushfstring(ls->L,
  3756. "`%s' expected (to close `%s' at line %d)",luaX_token2str(ls,what),
  3757. luaX_token2str(ls,who),where));}}}static TString*str_checkname(LexState*ls){
  3758. TString*ts;check_condition(ls,(ls->t.token==TK_NAME),"<name> expected");ts=ls
  3759. ->t.seminfo.ts;next(ls);return ts;}static void init_exp(expdesc*e,expkind k,
  3760. int i){e->f=e->t=NO_JUMP;e->k=k;e->info=i;}static void codestring(LexState*ls,
  3761. expdesc*e,TString*s){init_exp(e,VK,luaK_stringK(ls->fs,s));}static void
  3762. checkname(LexState*ls,expdesc*e){codestring(ls,e,str_checkname(ls));}static
  3763. int luaI_registerlocalvar(LexState*ls,TString*varname){FuncState*fs=ls->fs;
  3764. Proto*f=fs->f;luaM_growvector(ls->L,f->locvars,fs->nlocvars,f->sizelocvars,
  3765. LocVar,MAX_INT,"");f->locvars[fs->nlocvars].varname=varname;return fs->
  3766. nlocvars++;}static void new_localvar(LexState*ls,TString*name,int n){FuncState
  3767. *fs=ls->fs;luaX_checklimit(ls,fs->nactvar+n+1,MAXVARS,"local variables");fs->
  3768. actvar[fs->nactvar+n]=luaI_registerlocalvar(ls,name);}static void
  3769. adjustlocalvars(LexState*ls,int nvars){FuncState*fs=ls->fs;fs->nactvar+=nvars;
  3770. for(;nvars;nvars--){getlocvar(fs,fs->nactvar-nvars).startpc=fs->pc;}}static
  3771. void removevars(LexState*ls,int tolevel){FuncState*fs=ls->fs;while(fs->nactvar
  3772. >tolevel)getlocvar(fs,--fs->nactvar).endpc=fs->pc;}static void new_localvarstr
  3773. (LexState*ls,const char*name,int n){new_localvar(ls,luaS_new(ls->L,name),n);}
  3774. static void create_local(LexState*ls,const char*name){new_localvarstr(ls,name,
  3775. 0);adjustlocalvars(ls,1);}static int indexupvalue(FuncState*fs,TString*name,
  3776. expdesc*v){int i;Proto*f=fs->f;for(i=0;i<f->nups;i++){if(fs->upvalues[i].k==v
  3777. ->k&&fs->upvalues[i].info==v->info){lua_assert(fs->f->upvalues[i]==name);
  3778. return i;}}luaX_checklimit(fs->ls,f->nups+1,MAXUPVALUES,"upvalues");
  3779. luaM_growvector(fs->L,fs->f->upvalues,f->nups,fs->f->sizeupvalues,TString*,
  3780. MAX_INT,"");fs->f->upvalues[f->nups]=name;fs->upvalues[f->nups]=*v;return f->
  3781. nups++;}static int searchvar(FuncState*fs,TString*n){int i;for(i=fs->nactvar-1
  3782. ;i>=0;i--){if(n==getlocvar(fs,i).varname)return i;}return-1;}static void
  3783. markupval(FuncState*fs,int level){BlockCnt*bl=fs->bl;while(bl&&bl->nactvar>
  3784. level)bl=bl->previous;if(bl)bl->upval=1;}static void singlevaraux(FuncState*fs
  3785. ,TString*n,expdesc*var,int base){if(fs==NULL)init_exp(var,VGLOBAL,NO_REG);else
  3786. {int v=searchvar(fs,n);if(v>=0){init_exp(var,VLOCAL,v);if(!base)markupval(fs,v
  3787. );}else{singlevaraux(fs->prev,n,var,0);if(var->k==VGLOBAL){if(base)var->info=
  3788. luaK_stringK(fs,n);}else{var->info=indexupvalue(fs,n,var);var->k=VUPVAL;}}}}
  3789. static TString*singlevar(LexState*ls,expdesc*var,int base){TString*varname=
  3790. str_checkname(ls);singlevaraux(ls->fs,varname,var,base);return varname;}static
  3791. void adjust_assign(LexState*ls,int nvars,int nexps,expdesc*e){FuncState*fs=ls
  3792. ->fs;int extra=nvars-nexps;if(e->k==VCALL){extra++;if(extra<=0)extra=0;else
  3793. luaK_reserveregs(fs,extra-1);luaK_setcallreturns(fs,e,extra);}else{if(e->k!=
  3794. VVOID)luaK_exp2nextreg(fs,e);if(extra>0){int reg=fs->freereg;luaK_reserveregs(
  3795. fs,extra);luaK_nil(fs,reg,extra);}}}static void code_params(LexState*ls,int
  3796. nparams,int dots){FuncState*fs=ls->fs;adjustlocalvars(ls,nparams);
  3797. luaX_checklimit(ls,fs->nactvar,MAXPARAMS,"parameters");fs->f->numparams=cast(
  3798. lu_byte,fs->nactvar);fs->f->is_vararg=cast(lu_byte,dots);if(dots)create_local(
  3799. ls,"arg");luaK_reserveregs(fs,fs->nactvar);}static void enterblock(FuncState*
  3800. fs,BlockCnt*bl,int isbreakable){bl->breaklist=NO_JUMP;bl->isbreakable=
  3801. isbreakable;bl->nactvar=fs->nactvar;bl->upval=0;bl->previous=fs->bl;fs->bl=bl;
  3802. lua_assert(fs->freereg==fs->nactvar);}static void leaveblock(FuncState*fs){
  3803. BlockCnt*bl=fs->bl;fs->bl=bl->previous;removevars(fs->ls,bl->nactvar);if(bl->
  3804. upval)luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0);lua_assert(bl->nactvar==fs->
  3805. nactvar);fs->freereg=fs->nactvar;luaK_patchtohere(fs,bl->breaklist);}static
  3806. void pushclosure(LexState*ls,FuncState*func,expdesc*v){FuncState*fs=ls->fs;
  3807. Proto*f=fs->f;int i;luaM_growvector(ls->L,f->p,fs->np,f->sizep,Proto*,
  3808. MAXARG_Bx,"constant table overflow");f->p[fs->np++]=func->f;init_exp(v,
  3809. VRELOCABLE,luaK_codeABx(fs,OP_CLOSURE,0,fs->np-1));for(i=0;i<func->f->nups;i++
  3810. ){OpCode o=(func->upvalues[i].k==VLOCAL)?OP_MOVE:OP_GETUPVAL;luaK_codeABC(fs,o
  3811. ,0,func->upvalues[i].info,0);}}static void open_func(LexState*ls,FuncState*fs)
  3812. {Proto*f=luaF_newproto(ls->L);fs->f=f;fs->prev=ls->fs;fs->ls=ls;fs->L=ls->L;ls
  3813. ->fs=fs;fs->pc=0;fs->lasttarget=0;fs->jpc=NO_JUMP;fs->freereg=0;fs->nk=0;fs->h
  3814. =luaH_new(ls->L,0,0);fs->np=0;fs->nlocvars=0;fs->nactvar=0;fs->bl=NULL;f->
  3815. source=ls->source;f->maxstacksize=2;}static void close_func(LexState*ls){
  3816. lua_State*L=ls->L;FuncState*fs=ls->fs;Proto*f=fs->f;removevars(ls,0);
  3817. luaK_codeABC(fs,OP_RETURN,0,1,0);luaM_reallocvector(L,f->code,f->sizecode,fs->
  3818. pc,Instruction);f->sizecode=fs->pc;luaM_reallocvector(L,f->lineinfo,f->
  3819. sizelineinfo,fs->pc,int);f->sizelineinfo=fs->pc;luaM_reallocvector(L,f->k,f->
  3820. sizek,fs->nk,TObject);f->sizek=fs->nk;luaM_reallocvector(L,f->p,f->sizep,fs->
  3821. np,Proto*);f->sizep=fs->np;luaM_reallocvector(L,f->locvars,f->sizelocvars,fs->
  3822. nlocvars,LocVar);f->sizelocvars=fs->nlocvars;luaM_reallocvector(L,f->upvalues,
  3823. f->sizeupvalues,f->nups,TString*);f->sizeupvalues=f->nups;lua_assert(
  3824. luaG_checkcode(f));lua_assert(fs->bl==NULL);ls->fs=fs->prev;}Proto*luaY_parser
  3825. (lua_State*L,ZIO*z,Mbuffer*buff){struct LexState lexstate;struct FuncState
  3826. funcstate;lexstate.buff=buff;lexstate.nestlevel=0;luaX_setinput(L,&lexstate,z,
  3827. luaS_new(L,zname(z)));open_func(&lexstate,&funcstate);next(&lexstate);chunk(&
  3828. lexstate);check_condition(&lexstate,(lexstate.t.token==TK_EOS),
  3829. "<eof> expected");close_func(&lexstate);lua_assert(funcstate.prev==NULL);
  3830. lua_assert(funcstate.f->nups==0);lua_assert(lexstate.nestlevel==0);return
  3831. funcstate.f;}static void luaY_field(LexState*ls,expdesc*v){FuncState*fs=ls->fs
  3832. ;expdesc key;luaK_exp2anyreg(fs,v);next(ls);checkname(ls,&key);luaK_indexed(fs
  3833. ,v,&key);}static void luaY_index(LexState*ls,expdesc*v){next(ls);expr(ls,v);
  3834. luaK_exp2val(ls->fs,v);check(ls,']');}struct ConsControl{expdesc v;expdesc*t;
  3835. int nh;int na;int tostore;};static void recfield(LexState*ls,struct
  3836. ConsControl*cc){FuncState*fs=ls->fs;int reg=ls->fs->freereg;expdesc key,val;if
  3837. (ls->t.token==TK_NAME){luaX_checklimit(ls,cc->nh,MAX_INT,
  3838. "items in a constructor");cc->nh++;checkname(ls,&key);}else luaY_index(ls,&key
  3839. );check(ls,'=');luaK_exp2RK(fs,&key);expr(ls,&val);luaK_codeABC(fs,OP_SETTABLE
  3840. ,cc->t->info,luaK_exp2RK(fs,&key),luaK_exp2RK(fs,&val));fs->freereg=reg;}
  3841. static void closelistfield(FuncState*fs,struct ConsControl*cc){if(cc->v.k==
  3842. VVOID)return;luaK_exp2nextreg(fs,&cc->v);cc->v.k=VVOID;if(cc->tostore==
  3843. LFIELDS_PER_FLUSH){luaK_codeABx(fs,OP_SETLIST,cc->t->info,cc->na-1);cc->
  3844. tostore=0;fs->freereg=cc->t->info+1;}}static void lastlistfield(FuncState*fs,
  3845. struct ConsControl*cc){if(cc->tostore==0)return;if(cc->v.k==VCALL){
  3846. luaK_setcallreturns(fs,&cc->v,LUA_MULTRET);luaK_codeABx(fs,OP_SETLISTO,cc->t->
  3847. info,cc->na-1);}else{if(cc->v.k!=VVOID)luaK_exp2nextreg(fs,&cc->v);
  3848. luaK_codeABx(fs,OP_SETLIST,cc->t->info,cc->na-1);}fs->freereg=cc->t->info+1;}
  3849. static void listfield(LexState*ls,struct ConsControl*cc){expr(ls,&cc->v);
  3850. luaX_checklimit(ls,cc->na,MAXARG_Bx,"items in a constructor");cc->na++;cc->
  3851. tostore++;}static void constructor(LexState*ls,expdesc*t){FuncState*fs=ls->fs;
  3852. int line=ls->linenumber;int pc=luaK_codeABC(fs,OP_NEWTABLE,0,0,0);struct
  3853. ConsControl cc;cc.na=cc.nh=cc.tostore=0;cc.t=t;init_exp(t,VRELOCABLE,pc);
  3854. init_exp(&cc.v,VVOID,0);luaK_exp2nextreg(ls->fs,t);check(ls,'{');do{lua_assert
  3855. (cc.v.k==VVOID||cc.tostore>0);testnext(ls,';');if(ls->t.token=='}')break;
  3856. closelistfield(fs,&cc);switch(ls->t.token){case TK_NAME:{lookahead(ls);if(ls->
  3857. lookahead.token!='=')listfield(ls,&cc);else recfield(ls,&cc);break;}case'[':{
  3858. recfield(ls,&cc);break;}default:{listfield(ls,&cc);break;}}}while(testnext(ls,
  3859. ',')||testnext(ls,';'));check_match(ls,'}','{',line);lastlistfield(fs,&cc);
  3860. SETARG_B(fs->f->code[pc],luaO_int2fb(cc.na));SETARG_C(fs->f->code[pc],
  3861. luaO_log2(cc.nh)+1);}static void parlist(LexState*ls){int nparams=0;int dots=0
  3862. ;if(ls->t.token!=')'){do{switch(ls->t.token){case TK_DOTS:dots=1;next(ls);
  3863. break;case TK_NAME:new_localvar(ls,str_checkname(ls),nparams++);break;default:
  3864. luaX_syntaxerror(ls,"<name> or `...' expected");}}while(!dots&&testnext(ls,','
  3865. ));}code_params(ls,nparams,dots);}static void body(LexState*ls,expdesc*e,int
  3866. needself,int line){FuncState new_fs;open_func(ls,&new_fs);new_fs.f->
  3867. lineDefined=line;check(ls,'(');if(needself)create_local(ls,"self");parlist(ls)
  3868. ;check(ls,')');chunk(ls);check_match(ls,TK_END,TK_FUNCTION,line);close_func(ls
  3869. );pushclosure(ls,&new_fs,e);}static int explist1(LexState*ls,expdesc*v){int n=
  3870. 1;expr(ls,v);while(testnext(ls,',')){luaK_exp2nextreg(ls->fs,v);expr(ls,v);n++
  3871. ;}return n;}static void funcargs(LexState*ls,expdesc*f){FuncState*fs=ls->fs;
  3872. expdesc args;int base,nparams;int line=ls->linenumber;switch(ls->t.token){case
  3873. '(':{if(line!=ls->lastline)luaX_syntaxerror(ls,
  3874. "ambiguous syntax (function call x new statement)");next(ls);if(ls->t.token==
  3875. ')')args.k=VVOID;else{explist1(ls,&args);luaK_setcallreturns(fs,&args,
  3876. LUA_MULTRET);}check_match(ls,')','(',line);break;}case'{':{constructor(ls,&
  3877. args);break;}case TK_STRING:{codestring(ls,&args,ls->t.seminfo.ts);next(ls);
  3878. break;}default:{luaX_syntaxerror(ls,"function arguments expected");return;}}
  3879. lua_assert(f->k==VNONRELOC);base=f->info;if(args.k==VCALL)nparams=LUA_MULTRET;
  3880. else{if(args.k!=VVOID)luaK_exp2nextreg(fs,&args);nparams=fs->freereg-(base+1);
  3881. }init_exp(f,VCALL,luaK_codeABC(fs,OP_CALL,base,nparams+1,2));luaK_fixline(fs,
  3882. line);fs->freereg=base+1;}static void prefixexp(LexState*ls,expdesc*v){switch(
  3883. ls->t.token){case'(':{int line=ls->linenumber;next(ls);expr(ls,v);check_match(
  3884. ls,')','(',line);luaK_dischargevars(ls->fs,v);return;}case TK_NAME:{singlevar(
  3885. ls,v,1);return;}
  3886. #ifdef LUA_COMPATUPSYNTAX
  3887. case'%':{TString*varname;int line=ls->linenumber;next(ls);varname=singlevar(ls
  3888. ,v,1);if(v->k!=VUPVAL)luaX_errorline(ls,"global upvalues are obsolete",getstr(
  3889. varname),line);return;}
  3890. #endif
  3891. default:{luaX_syntaxerror(ls,"unexpected symbol");return;}}}static void
  3892. primaryexp(LexState*ls,expdesc*v){FuncState*fs=ls->fs;prefixexp(ls,v);for(;;){
  3893. switch(ls->t.token){case'.':{luaY_field(ls,v);break;}case'[':{expdesc key;
  3894. luaK_exp2anyreg(fs,v);luaY_index(ls,&key);luaK_indexed(fs,v,&key);break;}case
  3895. ':':{expdesc key;next(ls);checkname(ls,&key);luaK_self(fs,v,&key);funcargs(ls,
  3896. v);break;}case'(':case TK_STRING:case'{':{luaK_exp2nextreg(fs,v);funcargs(ls,v
  3897. );break;}default:return;}}}static void simpleexp(LexState*ls,expdesc*v){switch
  3898. (ls->t.token){case TK_NUMBER:{init_exp(v,VK,luaK_numberK(ls->fs,ls->t.seminfo.
  3899. r));next(ls);break;}case TK_STRING:{codestring(ls,v,ls->t.seminfo.ts);next(ls)
  3900. ;break;}case TK_NIL:{init_exp(v,VNIL,0);next(ls);break;}case TK_TRUE:{init_exp
  3901. (v,VTRUE,0);next(ls);break;}case TK_FALSE:{init_exp(v,VFALSE,0);next(ls);break
  3902. ;}case'{':{constructor(ls,v);break;}case TK_FUNCTION:{next(ls);body(ls,v,0,ls
  3903. ->linenumber);break;}default:{primaryexp(ls,v);break;}}}static UnOpr getunopr(
  3904. int op){switch(op){case TK_NOT:return OPR_NOT;case'-':return OPR_MINUS;default
  3905. :return OPR_NOUNOPR;}}static BinOpr getbinopr(int op){switch(op){case'+':
  3906. return OPR_ADD;case'-':return OPR_SUB;case'*':return OPR_MULT;case'/':return
  3907. OPR_DIV;case'^':return OPR_POW;case TK_CONCAT:return OPR_CONCAT;case TK_NE:
  3908. return OPR_NE;case TK_EQ:return OPR_EQ;case'<':return OPR_LT;case TK_LE:return
  3909. OPR_LE;case'>':return OPR_GT;case TK_GE:return OPR_GE;case TK_AND:return
  3910. OPR_AND;case TK_OR:return OPR_OR;default:return OPR_NOBINOPR;}}static const
  3911. struct{lu_byte left;lu_byte right;}priority[]={{6,6},{6,6},{7,7},{7,7},{10,9},
  3912. {5,4},{3,3},{3,3},{3,3},{3,3},{3,3},{3,3},{2,2},{1,1}};
  3913. #define UNARY_PRIORITY 8
  3914. static BinOpr subexpr(LexState*ls,expdesc*v,int limit){BinOpr op;UnOpr uop;
  3915. enterlevel(ls);uop=getunopr(ls->t.token);if(uop!=OPR_NOUNOPR){next(ls);subexpr
  3916. (ls,v,UNARY_PRIORITY);luaK_prefix(ls->fs,uop,v);}else simpleexp(ls,v);op=
  3917. getbinopr(ls->t.token);while(op!=OPR_NOBINOPR&&cast(int,priority[op].left)>
  3918. limit){expdesc v2;BinOpr nextop;next(ls);luaK_infix(ls->fs,op,v);nextop=
  3919. subexpr(ls,&v2,cast(int,priority[op].right));luaK_posfix(ls->fs,op,v,&v2);op=
  3920. nextop;}leavelevel(ls);return op;}static void expr(LexState*ls,expdesc*v){
  3921. subexpr(ls,v,-1);}static int block_follow(int token){switch(token){case
  3922. TK_ELSE:case TK_ELSEIF:case TK_END:case TK_UNTIL:case TK_EOS:return 1;default:
  3923. return 0;}}static void block(LexState*ls){FuncState*fs=ls->fs;BlockCnt bl;
  3924. enterblock(fs,&bl,0);chunk(ls);lua_assert(bl.breaklist==NO_JUMP);leaveblock(fs
  3925. );}struct LHS_assign{struct LHS_assign*prev;expdesc v;};static void
  3926. check_conflict(LexState*ls,struct LHS_assign*lh,expdesc*v){FuncState*fs=ls->fs
  3927. ;int extra=fs->freereg;int conflict=0;for(;lh;lh=lh->prev){if(lh->v.k==
  3928. VINDEXED){if(lh->v.info==v->info){conflict=1;lh->v.info=extra;}if(lh->v.aux==v
  3929. ->info){conflict=1;lh->v.aux=extra;}}}if(conflict){luaK_codeABC(fs,OP_MOVE,fs
  3930. ->freereg,v->info,0);luaK_reserveregs(fs,1);}}static void assignment(LexState*
  3931. ls,struct LHS_assign*lh,int nvars){expdesc e;check_condition(ls,VLOCAL<=lh->v.
  3932. k&&lh->v.k<=VINDEXED,"syntax error");if(testnext(ls,',')){struct LHS_assign nv
  3933. ;nv.prev=lh;primaryexp(ls,&nv.v);if(nv.v.k==VLOCAL)check_conflict(ls,lh,&nv.v)
  3934. ;assignment(ls,&nv,nvars+1);}else{int nexps;check(ls,'=');nexps=explist1(ls,&e
  3935. );if(nexps!=nvars){adjust_assign(ls,nvars,nexps,&e);if(nexps>nvars)ls->fs->
  3936. freereg-=nexps-nvars;}else{luaK_setcallreturns(ls->fs,&e,1);luaK_storevar(ls->
  3937. fs,&lh->v,&e);return;}}init_exp(&e,VNONRELOC,ls->fs->freereg-1);luaK_storevar(
  3938. ls->fs,&lh->v,&e);}static void cond(LexState*ls,expdesc*v){expr(ls,v);if(v->k
  3939. ==VNIL)v->k=VFALSE;luaK_goiftrue(ls->fs,v);luaK_patchtohere(ls->fs,v->t);}
  3940. #ifndef MAXEXPWHILE
  3941. #define MAXEXPWHILE 100
  3942. #endif
  3943. #define EXTRAEXP 5
  3944. static void whilestat(LexState*ls,int line){Instruction codeexp[MAXEXPWHILE+
  3945. EXTRAEXP];int lineexp;int i;int sizeexp;FuncState*fs=ls->fs;int whileinit,
  3946. blockinit,expinit;expdesc v;BlockCnt bl;next(ls);whileinit=luaK_jump(fs);
  3947. expinit=luaK_getlabel(fs);expr(ls,&v);if(v.k==VK)v.k=VTRUE;lineexp=ls->
  3948. linenumber;luaK_goiffalse(fs,&v);luaK_concat(fs,&v.f,fs->jpc);fs->jpc=NO_JUMP;
  3949. sizeexp=fs->pc-expinit;if(sizeexp>MAXEXPWHILE)luaX_syntaxerror(ls,
  3950. "`while' condition too complex");for(i=0;i<sizeexp;i++)codeexp[i]=fs->f->code[
  3951. expinit+i];fs->pc=expinit;enterblock(fs,&bl,1);check(ls,TK_DO);blockinit=
  3952. luaK_getlabel(fs);block(ls);luaK_patchtohere(fs,whileinit);if(v.t!=NO_JUMP)v.t
  3953. +=fs->pc-expinit;if(v.f!=NO_JUMP)v.f+=fs->pc-expinit;for(i=0;i<sizeexp;i++)
  3954. luaK_code(fs,codeexp[i],lineexp);check_match(ls,TK_END,TK_WHILE,line);
  3955. leaveblock(fs);luaK_patchlist(fs,v.t,blockinit);luaK_patchtohere(fs,v.f);}
  3956. static void repeatstat(LexState*ls,int line){FuncState*fs=ls->fs;int
  3957. repeat_init=luaK_getlabel(fs);expdesc v;BlockCnt bl;enterblock(fs,&bl,1);next(
  3958. ls);block(ls);check_match(ls,TK_UNTIL,TK_REPEAT,line);cond(ls,&v);
  3959. luaK_patchlist(fs,v.f,repeat_init);leaveblock(fs);}static int exp1(LexState*ls
  3960. ){expdesc e;int k;expr(ls,&e);k=e.k;luaK_exp2nextreg(ls->fs,&e);return k;}
  3961. static void forbody(LexState*ls,int base,int line,int nvars,int isnum){
  3962. BlockCnt bl;FuncState*fs=ls->fs;int prep,endfor;adjustlocalvars(ls,nvars);
  3963. check(ls,TK_DO);enterblock(fs,&bl,1);prep=luaK_getlabel(fs);block(ls);
  3964. luaK_patchtohere(fs,prep-1);endfor=(isnum)?luaK_codeAsBx(fs,OP_FORLOOP,base,
  3965. NO_JUMP):luaK_codeABC(fs,OP_TFORLOOP,base,0,nvars-3);luaK_fixline(fs,line);
  3966. luaK_patchlist(fs,(isnum)?endfor:luaK_jump(fs),prep);leaveblock(fs);}static
  3967. void fornum(LexState*ls,TString*varname,int line){FuncState*fs=ls->fs;int base
  3968. =fs->freereg;new_localvar(ls,varname,0);new_localvarstr(ls,"(for limit)",1);
  3969. new_localvarstr(ls,"(for step)",2);check(ls,'=');exp1(ls);check(ls,',');exp1(
  3970. ls);if(testnext(ls,','))exp1(ls);else{luaK_codeABx(fs,OP_LOADK,fs->freereg,
  3971. luaK_numberK(fs,1));luaK_reserveregs(fs,1);}luaK_codeABC(fs,OP_SUB,fs->freereg
  3972. -3,fs->freereg-3,fs->freereg-1);luaK_jump(fs);forbody(ls,base,line,3,1);}
  3973. static void forlist(LexState*ls,TString*indexname){FuncState*fs=ls->fs;expdesc
  3974. e;int nvars=0;int line;int base=fs->freereg;new_localvarstr(ls,
  3975. "(for generator)",nvars++);new_localvarstr(ls,"(for state)",nvars++);
  3976. new_localvar(ls,indexname,nvars++);while(testnext(ls,','))new_localvar(ls,
  3977. str_checkname(ls),nvars++);check(ls,TK_IN);line=ls->linenumber;adjust_assign(
  3978. ls,nvars,explist1(ls,&e),&e);luaK_checkstack(fs,3);luaK_codeAsBx(fs,
  3979. OP_TFORPREP,base,NO_JUMP);forbody(ls,base,line,nvars,0);}static void forstat(
  3980. LexState*ls,int line){FuncState*fs=ls->fs;TString*varname;BlockCnt bl;
  3981. enterblock(fs,&bl,0);next(ls);varname=str_checkname(ls);switch(ls->t.token){
  3982. case'=':fornum(ls,varname,line);break;case',':case TK_IN:forlist(ls,varname);
  3983. break;default:luaX_syntaxerror(ls,"`=' or `in' expected");}check_match(ls,
  3984. TK_END,TK_FOR,line);leaveblock(fs);}static void test_then_block(LexState*ls,
  3985. expdesc*v){next(ls);cond(ls,v);check(ls,TK_THEN);block(ls);}static void ifstat
  3986. (LexState*ls,int line){FuncState*fs=ls->fs;expdesc v;int escapelist=NO_JUMP;
  3987. test_then_block(ls,&v);while(ls->t.token==TK_ELSEIF){luaK_concat(fs,&
  3988. escapelist,luaK_jump(fs));luaK_patchtohere(fs,v.f);test_then_block(ls,&v);}if(
  3989. ls->t.token==TK_ELSE){luaK_concat(fs,&escapelist,luaK_jump(fs));
  3990. luaK_patchtohere(fs,v.f);next(ls);block(ls);}else luaK_concat(fs,&escapelist,v
  3991. .f);luaK_patchtohere(fs,escapelist);check_match(ls,TK_END,TK_IF,line);}static
  3992. void localfunc(LexState*ls){expdesc v,b;FuncState*fs=ls->fs;new_localvar(ls,
  3993. str_checkname(ls),0);init_exp(&v,VLOCAL,fs->freereg);luaK_reserveregs(fs,1);
  3994. adjustlocalvars(ls,1);body(ls,&b,0,ls->linenumber);luaK_storevar(fs,&v,&b);
  3995. getlocvar(fs,fs->nactvar-1).startpc=fs->pc;}static void localstat(LexState*ls)
  3996. {int nvars=0;int nexps;expdesc e;do{new_localvar(ls,str_checkname(ls),nvars++)
  3997. ;}while(testnext(ls,','));if(testnext(ls,'='))nexps=explist1(ls,&e);else{e.k=
  3998. VVOID;nexps=0;}adjust_assign(ls,nvars,nexps,&e);adjustlocalvars(ls,nvars);}
  3999. static int funcname(LexState*ls,expdesc*v){int needself=0;singlevar(ls,v,1);
  4000. while(ls->t.token=='.')luaY_field(ls,v);if(ls->t.token==':'){needself=1;
  4001. luaY_field(ls,v);}return needself;}static void funcstat(LexState*ls,int line){
  4002. int needself;expdesc v,b;next(ls);needself=funcname(ls,&v);body(ls,&b,needself
  4003. ,line);luaK_storevar(ls->fs,&v,&b);luaK_fixline(ls->fs,line);}static void
  4004. exprstat(LexState*ls){FuncState*fs=ls->fs;struct LHS_assign v;primaryexp(ls,&v
  4005. .v);if(v.v.k==VCALL){luaK_setcallreturns(fs,&v.v,0);}else{v.prev=NULL;
  4006. assignment(ls,&v,1);}}static void retstat(LexState*ls){FuncState*fs=ls->fs;
  4007. expdesc e;int first,nret;next(ls);if(block_follow(ls->t.token)||ls->t.token==
  4008. ';')first=nret=0;else{nret=explist1(ls,&e);if(e.k==VCALL){luaK_setcallreturns(
  4009. fs,&e,LUA_MULTRET);if(nret==1){SET_OPCODE(getcode(fs,&e),OP_TAILCALL);
  4010. lua_assert(GETARG_A(getcode(fs,&e))==fs->nactvar);}first=fs->nactvar;nret=
  4011. LUA_MULTRET;}else{if(nret==1)first=luaK_exp2anyreg(fs,&e);else{
  4012. luaK_exp2nextreg(fs,&e);first=fs->nactvar;lua_assert(nret==fs->freereg-first);
  4013. }}}luaK_codeABC(fs,OP_RETURN,first,nret+1,0);}static void breakstat(LexState*
  4014. ls){FuncState*fs=ls->fs;BlockCnt*bl=fs->bl;int upval=0;next(ls);while(bl&&!bl
  4015. ->isbreakable){upval|=bl->upval;bl=bl->previous;}if(!bl)luaX_syntaxerror(ls,
  4016. "no loop to break");if(upval)luaK_codeABC(fs,OP_CLOSE,bl->nactvar,0,0);
  4017. luaK_concat(fs,&bl->breaklist,luaK_jump(fs));}static int statement(LexState*ls
  4018. ){int line=ls->linenumber;switch(ls->t.token){case TK_IF:{ifstat(ls,line);
  4019. return 0;}case TK_WHILE:{whilestat(ls,line);return 0;}case TK_DO:{next(ls);
  4020. block(ls);check_match(ls,TK_END,TK_DO,line);return 0;}case TK_FOR:{forstat(ls,
  4021. line);return 0;}case TK_REPEAT:{repeatstat(ls,line);return 0;}case TK_FUNCTION
  4022. :{funcstat(ls,line);return 0;}case TK_LOCAL:{next(ls);if(testnext(ls,
  4023. TK_FUNCTION))localfunc(ls);else localstat(ls);return 0;}case TK_RETURN:{
  4024. retstat(ls);return 1;}case TK_BREAK:{breakstat(ls);return 1;}default:{exprstat
  4025. (ls);return 0;}}}static void chunk(LexState*ls){int islast=0;enterlevel(ls);
  4026. while(!islast&&!block_follow(ls->t.token)){islast=statement(ls);testnext(ls,
  4027. ';');lua_assert(ls->fs->freereg>=ls->fs->nactvar);ls->fs->freereg=ls->fs->
  4028. nactvar;}leavelevel(ls);}
  4029. #line 1 "lposix.c"
  4030. #define MYNAME "posix"
  4031. #define MYVERSION MYNAME" library for "LUA_VERSION" / Nov 2003"
  4032. #ifndef MYBUFSIZ
  4033. #define MYBUFSIZ 512
  4034. #endif
  4035. #line 1 "modemuncher.h"
  4036. struct modeLookup{char rwx;mode_t bits;};typedef struct modeLookup modeLookup;
  4037. static modeLookup modesel[]={{'r',S_IRUSR},{'w',S_IWUSR},{'x',S_IXUSR},{'r',
  4038. S_IRGRP},{'w',S_IWGRP},{'x',S_IXGRP},{'r',S_IROTH},{'w',S_IWOTH},{'x',S_IXOTH}
  4039. ,{(char)NULL,(mode_t)-1}};static int rwxrwxrwx(mode_t*mode,const char*p){int
  4040. count;mode_t tmp_mode=*mode;tmp_mode&=~(S_ISUID|S_ISGID);for(count=0;count<9;
  4041. count++){if(*p==modesel[count].rwx)tmp_mode|=modesel[count].bits;else if(*p==
  4042. '-')tmp_mode&=~modesel[count].bits;else if(*p=='s')switch(count){case 2:
  4043. tmp_mode|=S_ISUID|S_IXUSR;break;case 5:tmp_mode|=S_ISGID|S_IXGRP;break;default
  4044. :return-4;break;}p++;}*mode=tmp_mode;return 0;}static void modechopper(mode_t
  4045. mode,char*p){int count;char*pp;pp=p;for(count=0;count<9;count++){if(mode&
  4046. modesel[count].bits)*p=modesel[count].rwx;else*p='-';p++;}*p=0;if(mode&S_ISUID
  4047. )pp[2]=(mode&S_IXUSR)?'s':'S';if(mode&S_ISGID)pp[5]=(mode&S_IXGRP)?'s':'S';}
  4048. static int mode_munch(mode_t*mode,const char*p){char op=0;mode_t affected_bits
  4049. ,ch_mode;int doneFlag=0;
  4050. #ifdef DEBUG
  4051. char tmp[10];
  4052. #endif
  4053. #ifdef DEBUG
  4054. modechopper(*mode,tmp);printf("modemuncher: got base mode = %s\n",tmp);
  4055. #endif
  4056. while(!doneFlag){affected_bits=0;ch_mode=0;
  4057. #ifdef DEBUG
  4058. printf("modemuncher step 1\n");
  4059. #endif
  4060. if(*p=='r'||*p=='-')return rwxrwxrwx(mode,p);for(;;p++)switch(*p){case'u':
  4061. affected_bits|=04700;break;case'g':affected_bits|=02070;break;case'o':
  4062. affected_bits|=01007;break;case'a':affected_bits|=07777;break;case' ':break;
  4063. default:goto no_more_affected;}no_more_affected:if(affected_bits==0)
  4064. affected_bits=07777;
  4065. #ifdef DEBUG
  4066. printf("modemuncher step 2 (*p='%c')\n",*p);
  4067. #endif
  4068. switch(*p){case'+':case'-':case'=':op=*p;break;case' ':break;default:return-1;
  4069. }
  4070. #ifdef DEBUG
  4071. printf("modemuncher step 3\n");
  4072. #endif
  4073. for(p++;*p!=0;p++)switch(*p){case'r':ch_mode|=00444;break;case'w':ch_mode|=
  4074. 00222;break;case'x':ch_mode|=00111;break;case's':ch_mode|=06000;break;case' ':
  4075. break;default:goto specs_done;}specs_done:
  4076. #ifdef DEBUG
  4077. printf("modemuncher step 4\n");
  4078. #endif
  4079. if(*p!=',')doneFlag=1;if(*p!=0&&*p!=' '&&*p!=','){
  4080. #ifdef DEBUG
  4081. printf("modemuncher: comma error!\n");printf("modemuncher: doneflag = %u\n",
  4082. doneFlag);
  4083. #endif
  4084. return-2;}p++;if(ch_mode)switch(op){case'+':*mode=*mode|=ch_mode&affected_bits
  4085. ;break;case'-':*mode=*mode&=~(ch_mode&affected_bits);break;case'=':*mode=
  4086. ch_mode&affected_bits;break;default:return-3;}}
  4087. #ifdef DEBUG
  4088. modechopper(*mode,tmp);printf("modemuncher: returning mode = %s\n",tmp);
  4089. #endif
  4090. return 0;}
  4091. #line 38 "lposix.c"
  4092. static const char*filetype(mode_t m){if(S_ISREG(m))return"regular";else if(
  4093. S_ISLNK(m))return"link";else if(S_ISDIR(m))return"directory";else if(S_ISCHR(m
  4094. ))return"character device";else if(S_ISBLK(m))return"block device";else if(
  4095. S_ISFIFO(m))return"fifo";
  4096. #ifdef S_ISSOCK
  4097. else if(S_ISSOCK(m))return"socket";
  4098. #endif
  4099. else return"?";}typedef int(*Selector)(lua_State*L,int i,const void*data);
  4100. static int doselection(lua_State*L,int i,const char*const S[],Selector F,const
  4101. void*data){if(lua_isnone(L,i)){lua_newtable(L);for(i=0;S[i]!=NULL;i++){
  4102. lua_pushstring(L,S[i]);F(L,i,data);lua_settable(L,-3);}return 1;}else{int j=
  4103. luaL_findstring(luaL_checkstring(L,i),S);if(j==-1)luaL_argerror(L,i,
  4104. "unknown selector");return F(L,j,data);}}static void storeindex(lua_State*L,
  4105. int i,const char*value){lua_pushstring(L,value);lua_rawseti(L,-2,i);}static
  4106. void storestring(lua_State*L,const char*name,const char*value){lua_pushstring(
  4107. L,name);lua_pushstring(L,value);lua_settable(L,-3);}static void storenumber(
  4108. lua_State*L,const char*name,lua_Number value){lua_pushstring(L,name);
  4109. lua_pushnumber(L,value);lua_settable(L,-3);}static int pusherror(lua_State*L,
  4110. const char*info){lua_pushnil(L);if(info==NULL)lua_pushstring(L,strerror(errno)
  4111. );else lua_pushfstring(L,"%s: %s",info,strerror(errno));lua_pushnumber(L,errno
  4112. );return 3;}static int lposix_pushresult(lua_State*L,int i,const char*info){if
  4113. (i!=-1){lua_pushnumber(L,i);return 1;}else return pusherror(L,info);}static
  4114. void badoption(lua_State*L,int i,const char*what,int option){luaL_argerror(L,2
  4115. ,lua_pushfstring(L,"unknown %s option `%c'",what,option));}static uid_t
  4116. mygetuid(lua_State*L,int i){if(lua_isnone(L,i))return-1;else if(lua_isnumber(L
  4117. ,i))return(uid_t)lua_tonumber(L,i);else if(lua_isstring(L,i)){struct passwd*p=
  4118. getpwnam(lua_tostring(L,i));return(p==NULL)?-1:p->pw_uid;}else return
  4119. luaL_typerror(L,i,"string or number");}static gid_t mygetgid(lua_State*L,int i
  4120. ){if(lua_isnone(L,i))return-1;else if(lua_isnumber(L,i))return(gid_t)
  4121. lua_tonumber(L,i);else if(lua_isstring(L,i)){struct group*g=getgrnam(
  4122. lua_tostring(L,i));return(g==NULL)?-1:g->gr_gid;}else return luaL_typerror(L,i
  4123. ,"string or number");}static int Perrno(lua_State*L){lua_pushstring(L,strerror
  4124. (errno));lua_pushnumber(L,errno);return 2;}static int Pdir(lua_State*L){const
  4125. char*path=luaL_optstring(L,1,".");DIR*d=opendir(path);if(d==NULL)return
  4126. pusherror(L,path);else{int i;struct dirent*entry;lua_newtable(L);for(i=1;(
  4127. entry=readdir(d))!=NULL;i++)storeindex(L,i,entry->d_name);closedir(d);return 1
  4128. ;}}static int aux_files(lua_State*L){DIR*d=lua_touserdata(L,lua_upvalueindex(1
  4129. ));struct dirent*entry;if(d==NULL)luaL_error(L,"attempt to use closed dir");
  4130. entry=readdir(d);if(entry==NULL){closedir(d);lua_pushnil(L);lua_replace(L,
  4131. lua_upvalueindex(1));lua_pushnil(L);}else{lua_pushstring(L,entry->d_name);
  4132. #if 0
  4133. #ifdef _DIRENT_HAVE_D_TYPE
  4134. lua_pushstring(L,filetype(DTTOIF(entry->d_type)));return 2;
  4135. #endif
  4136. #endif
  4137. }return 1;}static int Pfiles(lua_State*L){const char*path=luaL_optstring(L,1,
  4138. ".");DIR*d=opendir(path);if(d==NULL)return pusherror(L,path);else{
  4139. lua_pushlightuserdata(L,d);lua_pushcclosure(L,aux_files,1);return 1;}}static
  4140. int Pgetcwd(lua_State*L){char buf[MYBUFSIZ];if(getcwd(buf,sizeof(buf))==NULL)
  4141. return pusherror(L,".");else{lua_pushstring(L,buf);return 1;}}static int
  4142. Pmkdir(lua_State*L){const char*path=luaL_checkstring(L,1);return
  4143. lposix_pushresult(L,mkdir(path,0777),path);}static int Pchdir(lua_State*L){
  4144. const char*path=luaL_checkstring(L,1);return lposix_pushresult(L,chdir(path),
  4145. path);}static int Prmdir(lua_State*L){const char*path=luaL_checkstring(L,1);
  4146. return lposix_pushresult(L,rmdir(path),path);}static int Punlink(lua_State*L){
  4147. const char*path=luaL_checkstring(L,1);return lposix_pushresult(L,unlink(path),
  4148. path);}static int Plink(lua_State*L){const char*oldpath=luaL_checkstring(L,1);
  4149. const char*newpath=luaL_checkstring(L,2);return lposix_pushresult(L,link(
  4150. oldpath,newpath),NULL);}static int Psymlink(lua_State*L){const char*oldpath=
  4151. luaL_checkstring(L,1);const char*newpath=luaL_checkstring(L,2);return
  4152. lposix_pushresult(L,symlink(oldpath,newpath),NULL);}static int Preadlink(
  4153. lua_State*L){char buf[MYBUFSIZ];const char*path=luaL_checkstring(L,1);int n=
  4154. readlink(path,buf,sizeof(buf));if(n==-1)return pusherror(L,path);
  4155. lua_pushlstring(L,buf,n);return 1;}static int Paccess(lua_State*L){int mode=
  4156. F_OK;const char*path=luaL_checkstring(L,1);const char*s;for(s=luaL_optstring(L
  4157. ,2,"f");*s!=0;s++)switch(*s){case' ':break;case'r':mode|=R_OK;break;case'w':
  4158. mode|=W_OK;break;case'x':mode|=X_OK;break;case'f':mode|=F_OK;break;default:
  4159. badoption(L,2,"mode",*s);break;}return lposix_pushresult(L,access(path,mode),
  4160. path);}static int Pmkfifo(lua_State*L){const char*path=luaL_checkstring(L,1);
  4161. return lposix_pushresult(L,mkfifo(path,0777),path);}static int Pexec(lua_State
  4162. *L){const char*path=luaL_checkstring(L,1);int i,n=lua_gettop(L);char**argv=
  4163. malloc((n+1)*sizeof(char*));if(argv==NULL)luaL_error(L,"not enough memory");
  4164. argv[0]=(char*)path;for(i=1;i<n;i++)argv[i]=(char*)luaL_checkstring(L,i+1);
  4165. argv[i]=NULL;execvp(path,argv);return pusherror(L,path);}static int Pfork(
  4166. lua_State*L){return lposix_pushresult(L,fork(),NULL);}static int Pwait(
  4167. lua_State*L){pid_t pid=luaL_optint(L,1,-1);return lposix_pushresult(L,waitpid(
  4168. pid,NULL,0),NULL);}static int Pkill(lua_State*L){pid_t pid=luaL_checkint(L,1);
  4169. int sig=luaL_optint(L,2,SIGTERM);return lposix_pushresult(L,kill(pid,sig),NULL
  4170. );}static int Psleep(lua_State*L){unsigned int seconds=luaL_checkint(L,1);
  4171. lua_pushnumber(L,sleep(seconds));return 1;}static int Pputenv(lua_State*L){
  4172. size_t l;const char*s=luaL_checklstring(L,1,&l);char*e=malloc(++l);return
  4173. lposix_pushresult(L,(e==NULL)?-1:putenv(memcpy(e,s,l)),s);}
  4174. #ifdef linux
  4175. static int Psetenv(lua_State*L){const char*name=luaL_checkstring(L,1);const
  4176. char*value=luaL_checkstring(L,2);int overwrite=lua_isnoneornil(L,3)||
  4177. lua_toboolean(L,3);return lposix_pushresult(L,setenv(name,value,overwrite),
  4178. name);}static int Punsetenv(lua_State*L){const char*name=luaL_checkstring(L,1)
  4179. ;unsetenv(name);return 0;}
  4180. #endif
  4181. static int Pgetenv(lua_State*L){if(lua_isnone(L,1)){extern char**environ;char*
  4182. *e;if(*environ==NULL)lua_pushnil(L);else lua_newtable(L);for(e=environ;*e!=
  4183. NULL;e++){char*s=*e;char*eq=strchr(s,'=');if(eq==NULL){lua_pushstring(L,s);
  4184. lua_pushboolean(L,0);}else{lua_pushlstring(L,s,eq-s);lua_pushstring(L,eq+1);}
  4185. lua_settable(L,-3);}}else lua_pushstring(L,getenv(luaL_checkstring(L,1)));
  4186. return 1;}static int Pumask(lua_State*L){char m[10];mode_t mode;umask(mode=
  4187. umask(0));mode=(~mode)&0777;if(!lua_isnone(L,1)){if(mode_munch(&mode,
  4188. luaL_checkstring(L,1))){lua_pushnil(L);return 1;}mode&=0777;umask(~mode);}
  4189. modechopper(mode,m);lua_pushstring(L,m);return 1;}static int Pchmod(lua_State*
  4190. L){mode_t mode;struct stat s;const char*path=luaL_checkstring(L,1);const char*
  4191. modestr=luaL_checkstring(L,2);if(stat(path,&s))return pusherror(L,path);mode=s
  4192. .st_mode;if(mode_munch(&mode,modestr))luaL_argerror(L,2,"bad mode");return
  4193. lposix_pushresult(L,chmod(path,mode),path);}static int Pchown(lua_State*L){
  4194. const char*path=luaL_checkstring(L,1);uid_t uid=mygetuid(L,2);gid_t gid=
  4195. mygetgid(L,3);return lposix_pushresult(L,chown(path,uid,gid),path);}static int
  4196. Putime(lua_State*L){struct utimbuf times;time_t currtime=time(NULL);const
  4197. char*path=luaL_checkstring(L,1);times.modtime=luaL_optnumber(L,2,currtime);
  4198. times.actime=luaL_optnumber(L,3,currtime);return lposix_pushresult(L,utime(
  4199. path,&times),path);}static int FgetID(lua_State*L,int i,const void*data){
  4200. switch(i){case 0:lua_pushnumber(L,getegid());break;case 1:lua_pushnumber(L,
  4201. geteuid());break;case 2:lua_pushnumber(L,getgid());break;case 3:lua_pushnumber
  4202. (L,getuid());break;case 4:lua_pushnumber(L,getpgrp());break;case 5:
  4203. lua_pushnumber(L,getpid());break;case 6:lua_pushnumber(L,getppid());break;}
  4204. return 1;}static const char*const SgetID[]={"egid","euid","gid","uid","pgrp",
  4205. "pid","ppid",NULL};static int Pgetprocessid(lua_State*L){return doselection(L,
  4206. 1,SgetID,FgetID,NULL);}static int Pttyname(lua_State*L){int fd=luaL_optint(L,1
  4207. ,0);lua_pushstring(L,ttyname(fd));return 1;}static int Pctermid(lua_State*L){
  4208. char b[L_ctermid];lua_pushstring(L,ctermid(b));return 1;}static int Pgetlogin(
  4209. lua_State*L){lua_pushstring(L,getlogin());return 1;}static int Fgetpasswd(
  4210. lua_State*L,int i,const void*data){const struct passwd*p=data;switch(i){case 0
  4211. :lua_pushstring(L,p->pw_name);break;case 1:lua_pushnumber(L,p->pw_uid);break;
  4212. case 2:lua_pushnumber(L,p->pw_gid);break;case 3:lua_pushstring(L,p->pw_dir);
  4213. break;case 4:lua_pushstring(L,p->pw_shell);break;case 5:lua_pushstring(L,p->
  4214. pw_gecos);break;case 6:lua_pushstring(L,p->pw_passwd);break;}return 1;}static
  4215. const char*const Sgetpasswd[]={"name","uid","gid","dir","shell","gecos",
  4216. "passwd",NULL};static int Pgetpasswd(lua_State*L){struct passwd*p=NULL;if(
  4217. lua_isnoneornil(L,1))p=getpwuid(geteuid());else if(lua_isnumber(L,1))p=
  4218. getpwuid((uid_t)lua_tonumber(L,1));else if(lua_isstring(L,1))p=getpwnam(
  4219. lua_tostring(L,1));else luaL_typerror(L,1,"string or number");if(p==NULL)
  4220. lua_pushnil(L);else doselection(L,2,Sgetpasswd,Fgetpasswd,p);return 1;}static
  4221. int Pgetgroup(lua_State*L){struct group*g=NULL;if(lua_isnumber(L,1))g=getgrgid
  4222. ((gid_t)lua_tonumber(L,1));else if(lua_isstring(L,1))g=getgrnam(lua_tostring(L
  4223. ,1));else luaL_typerror(L,1,"string or number");if(g==NULL)lua_pushnil(L);else
  4224. {int i;lua_newtable(L);storestring(L,"name",g->gr_name);storenumber(L,"gid",g
  4225. ->gr_gid);for(i=0;g->gr_mem[i]!=NULL;i++)storeindex(L,i+1,g->gr_mem[i]);}
  4226. return 1;}static int Psetuid(lua_State*L){return lposix_pushresult(L,setuid(
  4227. mygetuid(L,1)),NULL);}static int Psetgid(lua_State*L){return lposix_pushresult
  4228. (L,setgid(mygetgid(L,1)),NULL);}struct mytimes{struct tms t;clock_t elapsed;};
  4229. #define pushtime(L,x) lua_pushnumber(L,((lua_Number)x)/CLOCKS_PER_SEC)
  4230. static int Ftimes(lua_State*L,int i,const void*data){const struct mytimes*t=
  4231. data;switch(i){case 0:pushtime(L,t->t.tms_utime);break;case 1:pushtime(L,t->t.
  4232. tms_stime);break;case 2:pushtime(L,t->t.tms_cutime);break;case 3:pushtime(L,t
  4233. ->t.tms_cstime);break;case 4:pushtime(L,t->elapsed);break;}return 1;}static
  4234. const char*const Stimes[]={"utime","stime","cutime","cstime","elapsed",NULL};
  4235. #define storetime(L,name,x) storenumber(L,name,(lua_Number)x/CLK_TCK)
  4236. static int Ptimes(lua_State*L){struct mytimes t;t.elapsed=times(&t.t);return
  4237. doselection(L,1,Stimes,Ftimes,&t);}struct mystat{struct stat s;char mode[10];
  4238. const char*type;};static int Fstat(lua_State*L,int i,const void*data){const
  4239. struct mystat*s=data;switch(i){case 0:lua_pushstring(L,s->mode);break;case 1:
  4240. lua_pushnumber(L,s->s.st_ino);break;case 2:lua_pushnumber(L,s->s.st_dev);break
  4241. ;case 3:lua_pushnumber(L,s->s.st_nlink);break;case 4:lua_pushnumber(L,s->s.
  4242. st_uid);break;case 5:lua_pushnumber(L,s->s.st_gid);break;case 6:lua_pushnumber
  4243. (L,s->s.st_size);break;case 7:lua_pushnumber(L,s->s.st_atime);break;case 8:
  4244. lua_pushnumber(L,s->s.st_mtime);break;case 9:lua_pushnumber(L,s->s.st_ctime);
  4245. break;case 10:lua_pushstring(L,s->type);break;case 11:lua_pushnumber(L,s->s.
  4246. st_mode);break;}return 1;}static const char*const Sstat[]={"mode","ino","dev",
  4247. "nlink","uid","gid","size","atime","mtime","ctime","type","_mode",NULL};static
  4248. int Pstat(lua_State*L){struct mystat s;const char*path=luaL_checkstring(L,1);
  4249. if(stat(path,&s.s)==-1)return pusherror(L,path);s.type=filetype(s.s.st_mode);
  4250. modechopper(s.s.st_mode,s.mode);return doselection(L,2,Sstat,Fstat,&s);}static
  4251. int Puname(lua_State*L){struct utsname u;luaL_Buffer b;const char*s;if(uname(
  4252. &u)==-1)return pusherror(L,NULL);luaL_buffinit(L,&b);for(s=luaL_optstring(L,1,
  4253. "%s %n %r %v %m");*s;s++)if(*s!='%')luaL_putchar(&b,*s);else switch(*++s){case
  4254. '%':luaL_putchar(&b,*s);break;case'm':luaL_addstring(&b,u.machine);break;case
  4255. 'n':luaL_addstring(&b,u.nodename);break;case'r':luaL_addstring(&b,u.release);
  4256. break;case's':luaL_addstring(&b,u.sysname);break;case'v':luaL_addstring(&b,u.
  4257. version);break;default:badoption(L,2,"format",*s);break;}luaL_pushresult(&b);
  4258. return 1;}static const int Kpathconf[]={_PC_LINK_MAX,_PC_MAX_CANON,
  4259. _PC_MAX_INPUT,_PC_NAME_MAX,_PC_PATH_MAX,_PC_PIPE_BUF,_PC_CHOWN_RESTRICTED,
  4260. _PC_NO_TRUNC,_PC_VDISABLE,-1};static int Fpathconf(lua_State*L,int i,const
  4261. void*data){const char*path=data;lua_pushnumber(L,pathconf(path,Kpathconf[i]));
  4262. return 1;}static const char*const Spathconf[]={"link_max","max_canon",
  4263. "max_input","name_max","path_max","pipe_buf","chown_restricted","no_trunc",
  4264. "vdisable",NULL};static int Ppathconf(lua_State*L){const char*path=
  4265. luaL_checkstring(L,1);return doselection(L,2,Spathconf,Fpathconf,path);}static
  4266. const int Ksysconf[]={_SC_ARG_MAX,_SC_CHILD_MAX,_SC_CLK_TCK,_SC_NGROUPS_MAX,
  4267. _SC_STREAM_MAX,_SC_TZNAME_MAX,_SC_OPEN_MAX,_SC_JOB_CONTROL,_SC_SAVED_IDS,
  4268. _SC_VERSION,-1};static int Fsysconf(lua_State*L,int i,const void*data){
  4269. lua_pushnumber(L,sysconf(Ksysconf[i]));return 1;}static const char*const
  4270. Ssysconf[]={"arg_max","child_max","clk_tck","ngroups_max","stream_max",
  4271. "tzname_max","open_max","job_control","saved_ids","version",NULL};static int
  4272. Psysconf(lua_State*L){return doselection(L,1,Ssysconf,Fsysconf,NULL);}static
  4273. const luaL_reg R[]={{"access",Paccess},{"chdir",Pchdir},{"chmod",Pchmod},{
  4274. "chown",Pchown},{"ctermid",Pctermid},{"dir",Pdir},{"errno",Perrno},{"exec",
  4275. Pexec},{"files",Pfiles},{"fork",Pfork},{"getcwd",Pgetcwd},{"getenv",Pgetenv},{
  4276. "getgroup",Pgetgroup},{"getlogin",Pgetlogin},{"getpasswd",Pgetpasswd},{
  4277. "getprocessid",Pgetprocessid},{"kill",Pkill},{"link",Plink},{"mkdir",Pmkdir},{
  4278. "mkfifo",Pmkfifo},{"pathconf",Ppathconf},{"putenv",Pputenv},{"readlink",
  4279. Preadlink},{"rmdir",Prmdir},{"setgid",Psetgid},{"setuid",Psetuid},{"sleep",
  4280. Psleep},{"stat",Pstat},{"symlink",Psymlink},{"sysconf",Psysconf},{"times",
  4281. Ptimes},{"ttyname",Pttyname},{"umask",Pumask},{"uname",Puname},{"unlink",
  4282. Punlink},{"utime",Putime},{"wait",Pwait},
  4283. #ifdef linux
  4284. {"setenv",Psetenv},{"unsetenv",Punsetenv},
  4285. #endif
  4286. {NULL,NULL}};LUALIB_API int luaopen_posix(lua_State*L){luaL_openlib(L,MYNAME,R
  4287. ,0);lua_pushliteral(L,"version");lua_pushliteral(L,MYVERSION);lua_settable(L,-
  4288. 3);return 1;}
  4289. #line 1 "lstate.c"
  4290. #define lstate_c
  4291. #ifndef LUA_USERSTATE
  4292. #define EXTRASPACE 0
  4293. #else
  4294. union UEXTRASPACE{L_Umaxalign a;LUA_USERSTATE b;};
  4295. #define EXTRASPACE (sizeof(union UEXTRASPACE))
  4296. #endif
  4297. static int default_panic(lua_State*L){UNUSED(L);return 0;}static lua_State*
  4298. mallocstate(lua_State*L){lu_byte*block=(lu_byte*)luaM_malloc(L,sizeof(
  4299. lua_State)+EXTRASPACE);if(block==NULL)return NULL;else{block+=EXTRASPACE;
  4300. return cast(lua_State*,block);}}static void freestate(lua_State*L,lua_State*L1
  4301. ){luaM_free(L,cast(lu_byte*,L1)-EXTRASPACE,sizeof(lua_State)+EXTRASPACE);}
  4302. static void stack_init(lua_State*L1,lua_State*L){L1->stack=luaM_newvector(L,
  4303. BASIC_STACK_SIZE+EXTRA_STACK,TObject);L1->stacksize=BASIC_STACK_SIZE+
  4304. EXTRA_STACK;L1->top=L1->stack;L1->stack_last=L1->stack+(L1->stacksize-
  4305. EXTRA_STACK)-1;L1->base_ci=luaM_newvector(L,BASIC_CI_SIZE,CallInfo);L1->ci=L1
  4306. ->base_ci;L1->ci->state=CI_C;setnilvalue(L1->top++);L1->base=L1->ci->base=L1->
  4307. top;L1->ci->top=L1->top+LUA_MINSTACK;L1->size_ci=BASIC_CI_SIZE;L1->end_ci=L1->
  4308. base_ci+L1->size_ci;}static void freestack(lua_State*L,lua_State*L1){
  4309. luaM_freearray(L,L1->base_ci,L1->size_ci,CallInfo);luaM_freearray(L,L1->stack,
  4310. L1->stacksize,TObject);}static void f_luaopen(lua_State*L,void*ud){
  4311. global_State*g=luaM_new(NULL,global_State);UNUSED(ud);if(g==NULL)luaD_throw(L,
  4312. LUA_ERRMEM);L->l_G=g;g->mainthread=L;g->GCthreshold=0;g->strt.size=0;g->strt.
  4313. nuse=0;g->strt.hash=NULL;setnilvalue(defaultmeta(L));setnilvalue(registry(L));
  4314. luaZ_initbuffer(L,&g->buff);g->panic=default_panic;g->rootgc=NULL;g->rootudata
  4315. =NULL;g->tmudata=NULL;setnilvalue(gkey(g->dummynode));setnilvalue(gval(g->
  4316. dummynode));g->dummynode->next=NULL;g->nblocks=sizeof(lua_State)+sizeof(
  4317. global_State);stack_init(L,L);defaultmeta(L)->tt=LUA_TTABLE;sethvalue(
  4318. defaultmeta(L),luaH_new(L,0,0));hvalue(defaultmeta(L))->metatable=hvalue(
  4319. defaultmeta(L));sethvalue(gt(L),luaH_new(L,0,4));sethvalue(registry(L),
  4320. luaH_new(L,4,4));luaS_resize(L,MINSTRTABSIZE);luaT_init(L);luaX_init(L);
  4321. luaS_fix(luaS_newliteral(L,MEMERRMSG));g->GCthreshold=4*G(L)->nblocks;}static
  4322. void preinit_state(lua_State*L){L->stack=NULL;L->stacksize=0;L->errorJmp=NULL;
  4323. L->hook=NULL;L->hookmask=L->hookinit=0;L->basehookcount=0;L->allowhook=1;
  4324. resethookcount(L);L->openupval=NULL;L->size_ci=0;L->nCcalls=0;L->base_ci=L->ci
  4325. =NULL;L->errfunc=0;setnilvalue(gt(L));}static void close_state(lua_State*L){
  4326. luaF_close(L,L->stack);if(G(L)){luaC_sweep(L,1);lua_assert(G(L)->rootgc==NULL)
  4327. ;lua_assert(G(L)->rootudata==NULL);luaS_freeall(L);luaZ_freebuffer(L,&G(L)->
  4328. buff);}freestack(L,L);if(G(L)){lua_assert(G(L)->nblocks==sizeof(lua_State)+
  4329. sizeof(global_State));luaM_freelem(NULL,G(L));}freestate(NULL,L);}lua_State*
  4330. luaE_newthread(lua_State*L){lua_State*L1=mallocstate(L);luaC_link(L,valtogco(
  4331. L1),LUA_TTHREAD);preinit_state(L1);L1->l_G=L->l_G;stack_init(L1,L);setobj2n(gt
  4332. (L1),gt(L));return L1;}void luaE_freethread(lua_State*L,lua_State*L1){
  4333. luaF_close(L1,L1->stack);lua_assert(L1->openupval==NULL);freestack(L,L1);
  4334. freestate(L,L1);}LUA_API lua_State*lua_open(void){lua_State*L=mallocstate(NULL
  4335. );if(L){L->tt=LUA_TTHREAD;L->marked=0;L->next=L->gclist=NULL;preinit_state(L);
  4336. L->l_G=NULL;if(luaD_rawrunprotected(L,f_luaopen,NULL)!=0){close_state(L);L=
  4337. NULL;}}lua_userstateopen(L);return L;}static void callallgcTM(lua_State*L,void
  4338. *ud){UNUSED(ud);luaC_callGCTM(L);}LUA_API void lua_close(lua_State*L){lua_lock
  4339. (L);L=G(L)->mainthread;luaF_close(L,L->stack);luaC_separateudata(L);L->errfunc
  4340. =0;do{L->ci=L->base_ci;L->base=L->top=L->ci->base;L->nCcalls=0;}while(
  4341. luaD_rawrunprotected(L,callallgcTM,NULL)!=0);lua_assert(G(L)->tmudata==NULL);
  4342. close_state(L);}
  4343. #line 1 "lstring.c"
  4344. #define lstring_c
  4345. void luaS_freeall(lua_State*L){lua_assert(G(L)->strt.nuse==0);luaM_freearray(L
  4346. ,G(L)->strt.hash,G(L)->strt.size,TString*);}void luaS_resize(lua_State*L,int
  4347. newsize){GCObject**newhash=luaM_newvector(L,newsize,GCObject*);stringtable*tb=
  4348. &G(L)->strt;int i;for(i=0;i<newsize;i++)newhash[i]=NULL;for(i=0;i<tb->size;i++
  4349. ){GCObject*p=tb->hash[i];while(p){GCObject*next=p->gch.next;lu_hash h=gcotots(
  4350. p)->tsv.hash;int h1=lmod(h,newsize);lua_assert(cast(int,h%newsize)==lmod(h,
  4351. newsize));p->gch.next=newhash[h1];newhash[h1]=p;p=next;}}luaM_freearray(L,tb->
  4352. hash,tb->size,TString*);tb->size=newsize;tb->hash=newhash;}static TString*
  4353. newlstr(lua_State*L,const char*str,size_t l,lu_hash h){TString*ts=cast(TString
  4354. *,luaM_malloc(L,sizestring(l)));stringtable*tb;ts->tsv.len=l;ts->tsv.hash=h;ts
  4355. ->tsv.marked=0;ts->tsv.tt=LUA_TSTRING;ts->tsv.reserved=0;memcpy(ts+1,str,l*
  4356. sizeof(char));((char*)(ts+1))[l]='\0';tb=&G(L)->strt;h=lmod(h,tb->size);ts->
  4357. tsv.next=tb->hash[h];tb->hash[h]=valtogco(ts);tb->nuse++;if(tb->nuse>cast(
  4358. ls_nstr,tb->size)&&tb->size<=MAX_INT/2)luaS_resize(L,tb->size*2);return ts;}
  4359. TString*luaS_newlstr(lua_State*L,const char*str,size_t l){GCObject*o;lu_hash h
  4360. =(lu_hash)l;size_t step=(l>>5)+1;size_t l1;for(l1=l;l1>=step;l1-=step)h=h^((h
  4361. <<5)+(h>>2)+(unsigned char)(str[l1-1]));for(o=G(L)->strt.hash[lmod(h,G(L)->
  4362. strt.size)];o!=NULL;o=o->gch.next){TString*ts=gcotots(o);if(ts->tsv.len==l&&(
  4363. memcmp(str,getstr(ts),l)==0))return ts;}return newlstr(L,str,l,h);}Udata*
  4364. luaS_newudata(lua_State*L,size_t s){Udata*u;u=cast(Udata*,luaM_malloc(L,
  4365. sizeudata(s)));u->uv.marked=(1<<1);u->uv.tt=LUA_TUSERDATA;u->uv.len=s;u->uv.
  4366. metatable=hvalue(defaultmeta(L));u->uv.next=G(L)->rootudata;G(L)->rootudata=
  4367. valtogco(u);return u;}
  4368. #line 1 "lstrlib.c"
  4369. #define lstrlib_c
  4370. #ifndef uchar
  4371. #define uchar(c) ((unsigned char)(c))
  4372. #endif
  4373. typedef long sint32;static int str_len(lua_State*L){size_t l;luaL_checklstring
  4374. (L,1,&l);lua_pushnumber(L,(lua_Number)l);return 1;}static sint32 posrelat(
  4375. sint32 pos,size_t len){return(pos>=0)?pos:(sint32)len+pos+1;}static int
  4376. str_sub(lua_State*L){size_t l;const char*s=luaL_checklstring(L,1,&l);sint32
  4377. start=posrelat(luaL_checklong(L,2),l);sint32 end=posrelat(luaL_optlong(L,3,-1)
  4378. ,l);if(start<1)start=1;if(end>(sint32)l)end=(sint32)l;if(start<=end)
  4379. lua_pushlstring(L,s+start-1,end-start+1);else lua_pushliteral(L,"");return 1;}
  4380. static int str_lower(lua_State*L){size_t l;size_t i;luaL_Buffer b;const char*s
  4381. =luaL_checklstring(L,1,&l);luaL_buffinit(L,&b);for(i=0;i<l;i++)luaL_putchar(&b
  4382. ,tolower(uchar(s[i])));luaL_pushresult(&b);return 1;}static int str_upper(
  4383. lua_State*L){size_t l;size_t i;luaL_Buffer b;const char*s=luaL_checklstring(L,
  4384. 1,&l);luaL_buffinit(L,&b);for(i=0;i<l;i++)luaL_putchar(&b,toupper(uchar(s[i]))
  4385. );luaL_pushresult(&b);return 1;}static int str_rep(lua_State*L){size_t l;
  4386. luaL_Buffer b;const char*s=luaL_checklstring(L,1,&l);int n=luaL_checkint(L,2);
  4387. luaL_buffinit(L,&b);while(n-->0)luaL_addlstring(&b,s,l);luaL_pushresult(&b);
  4388. return 1;}static int str_byte(lua_State*L){size_t l;const char*s=
  4389. luaL_checklstring(L,1,&l);sint32 pos=posrelat(luaL_optlong(L,2,1),l);if(pos<=0
  4390. ||(size_t)(pos)>l)return 0;lua_pushnumber(L,uchar(s[pos-1]));return 1;}static
  4391. int str_char(lua_State*L){int n=lua_gettop(L);int i;luaL_Buffer b;
  4392. luaL_buffinit(L,&b);for(i=1;i<=n;i++){int c=luaL_checkint(L,i);luaL_argcheck(L
  4393. ,uchar(c)==c,i,"invalid value");luaL_putchar(&b,uchar(c));}luaL_pushresult(&b)
  4394. ;return 1;}static int writer(lua_State*L,const void*b,size_t size,void*B){(
  4395. void)L;luaL_addlstring((luaL_Buffer*)B,(const char*)b,size);return 1;}static
  4396. int str_dump(lua_State*L){luaL_Buffer b;luaL_checktype(L,1,LUA_TFUNCTION);
  4397. luaL_buffinit(L,&b);if(!lua_dump(L,writer,&b))luaL_error(L,
  4398. "unable to dump given function");luaL_pushresult(&b);return 1;}
  4399. #ifndef MAX_CAPTURES
  4400. #define MAX_CAPTURES 32
  4401. #endif
  4402. #define CAP_UNFINISHED (-1)
  4403. #define CAP_POSITION (-2)
  4404. typedef struct MatchState{const char*src_init;const char*src_end;lua_State*L;
  4405. int level;struct{const char*init;sint32 len;}capture[MAX_CAPTURES];}MatchState
  4406. ;
  4407. #define ESC '%'
  4408. #define SPECIALS "^$*+?.([%-"
  4409. static int check_capture(MatchState*ms,int l){l-='1';if(l<0||l>=ms->level||ms
  4410. ->capture[l].len==CAP_UNFINISHED)return luaL_error(ms->L,
  4411. "invalid capture index");return l;}static int capture_to_close(MatchState*ms){
  4412. int level=ms->level;for(level--;level>=0;level--)if(ms->capture[level].len==
  4413. CAP_UNFINISHED)return level;return luaL_error(ms->L,"invalid pattern capture")
  4414. ;}static const char*luaI_classend(MatchState*ms,const char*p){switch(*p++){
  4415. case ESC:{if(*p=='\0')luaL_error(ms->L,"malformed pattern (ends with `%')");
  4416. return p+1;}case'[':{if(*p=='^')p++;do{if(*p=='\0')luaL_error(ms->L,
  4417. "malformed pattern (missing `]')");if(*(p++)==ESC&&*p!='\0')p++;}while(*p!=']'
  4418. );return p+1;}default:{return p;}}}static int match_class(int c,int cl){int
  4419. res;switch(tolower(cl)){case'a':res=isalpha(c);break;case'c':res=iscntrl(c);
  4420. break;case'd':res=isdigit(c);break;case'l':res=islower(c);break;case'p':res=
  4421. ispunct(c);break;case's':res=isspace(c);break;case'u':res=isupper(c);break;
  4422. case'w':res=isalnum(c);break;case'x':res=isxdigit(c);break;case'z':res=(c==0);
  4423. break;default:return(cl==c);}return(islower(cl)?res:!res);}static int
  4424. matchbracketclass(int c,const char*p,const char*ec){int sig=1;if(*(p+1)=='^'){
  4425. sig=0;p++;}while(++p<ec){if(*p==ESC){p++;if(match_class(c,*p))return sig;}else
  4426. if((*(p+1)=='-')&&(p+2<ec)){p+=2;if(uchar(*(p-2))<=c&&c<=uchar(*p))return sig
  4427. ;}else if(uchar(*p)==c)return sig;}return!sig;}static int luaI_singlematch(int
  4428. c,const char*p,const char*ep){switch(*p){case'.':return 1;case ESC:return
  4429. match_class(c,*(p+1));case'[':return matchbracketclass(c,p,ep-1);default:
  4430. return(uchar(*p)==c);}}static const char*match(MatchState*ms,const char*s,
  4431. const char*p);static const char*matchbalance(MatchState*ms,const char*s,const
  4432. char*p){if(*p==0||*(p+1)==0)luaL_error(ms->L,"unbalanced pattern");if(*s!=*p)
  4433. return NULL;else{int b=*p;int e=*(p+1);int cont=1;while(++s<ms->src_end){if(*s
  4434. ==e){if(--cont==0)return s+1;}else if(*s==b)cont++;}}return NULL;}static const
  4435. char*max_expand(MatchState*ms,const char*s,const char*p,const char*ep){sint32
  4436. i=0;while((s+i)<ms->src_end&&luaI_singlematch(uchar(*(s+i)),p,ep))i++;while(i
  4437. >=0){const char*res=match(ms,(s+i),ep+1);if(res)return res;i--;}return NULL;}
  4438. static const char*min_expand(MatchState*ms,const char*s,const char*p,const
  4439. char*ep){for(;;){const char*res=match(ms,s,ep+1);if(res!=NULL)return res;else
  4440. if(s<ms->src_end&&luaI_singlematch(uchar(*s),p,ep))s++;else return NULL;}}
  4441. static const char*start_capture(MatchState*ms,const char*s,const char*p,int
  4442. what){const char*res;int level=ms->level;if(level>=MAX_CAPTURES)luaL_error(ms
  4443. ->L,"too many captures");ms->capture[level].init=s;ms->capture[level].len=what
  4444. ;ms->level=level+1;if((res=match(ms,s,p))==NULL)ms->level--;return res;}static
  4445. const char*end_capture(MatchState*ms,const char*s,const char*p){int l=
  4446. capture_to_close(ms);const char*res;ms->capture[l].len=s-ms->capture[l].init;
  4447. if((res=match(ms,s,p))==NULL)ms->capture[l].len=CAP_UNFINISHED;return res;}
  4448. static const char*match_capture(MatchState*ms,const char*s,int l){size_t len;l
  4449. =check_capture(ms,l);len=ms->capture[l].len;if((size_t)(ms->src_end-s)>=len&&
  4450. memcmp(ms->capture[l].init,s,len)==0)return s+len;else return NULL;}static
  4451. const char*match(MatchState*ms,const char*s,const char*p){init:switch(*p){case
  4452. '(':{if(*(p+1)==')')return start_capture(ms,s,p+2,CAP_POSITION);else return
  4453. start_capture(ms,s,p+1,CAP_UNFINISHED);}case')':{return end_capture(ms,s,p+1);
  4454. }case ESC:{switch(*(p+1)){case'b':{s=matchbalance(ms,s,p+2);if(s==NULL)return
  4455. NULL;p+=4;goto init;}case'f':{const char*ep;char previous;p+=2;if(*p!='[')
  4456. luaL_error(ms->L,"missing `[' after `%%f' in pattern");ep=luaI_classend(ms,p);
  4457. previous=(s==ms->src_init)?'\0':*(s-1);if(matchbracketclass(uchar(previous),p,
  4458. ep-1)||!matchbracketclass(uchar(*s),p,ep-1))return NULL;p=ep;goto init;}
  4459. default:{if(isdigit(uchar(*(p+1)))){s=match_capture(ms,s,*(p+1));if(s==NULL)
  4460. return NULL;p+=2;goto init;}goto dflt;}}}case'\0':{return s;}case'$':{if(*(p+1
  4461. )=='\0')return(s==ms->src_end)?s:NULL;else goto dflt;}default:dflt:{const char
  4462. *ep=luaI_classend(ms,p);int m=s<ms->src_end&&luaI_singlematch(uchar(*s),p,ep);
  4463. switch(*ep){case'?':{const char*res;if(m&&((res=match(ms,s+1,ep+1))!=NULL))
  4464. return res;p=ep+1;goto init;}case'*':{return max_expand(ms,s,p,ep);}case'+':{
  4465. return(m?max_expand(ms,s+1,p,ep):NULL);}case'-':{return min_expand(ms,s,p,ep);
  4466. }default:{if(!m)return NULL;s++;p=ep;goto init;}}}}}static const char*lmemfind
  4467. (const char*s1,size_t l1,const char*s2,size_t l2){if(l2==0)return s1;else if(
  4468. l2>l1)return NULL;else{const char*init;l2--;l1=l1-l2;while(l1>0&&(init=(const
  4469. char*)memchr(s1,*s2,l1))!=NULL){init++;if(memcmp(init,s2+1,l2)==0)return init-
  4470. 1;else{l1-=init-s1;s1=init;}}return NULL;}}static void push_onecapture(
  4471. MatchState*ms,int i){int l=ms->capture[i].len;if(l==CAP_UNFINISHED)luaL_error(
  4472. ms->L,"unfinished capture");if(l==CAP_POSITION)lua_pushnumber(ms->L,(
  4473. lua_Number)(ms->capture[i].init-ms->src_init+1));else lua_pushlstring(ms->L,ms
  4474. ->capture[i].init,l);}static int push_captures(MatchState*ms,const char*s,
  4475. const char*e){int i;luaL_checkstack(ms->L,ms->level,"too many captures");if(ms
  4476. ->level==0&&s){lua_pushlstring(ms->L,s,e-s);return 1;}else{for(i=0;i<ms->level
  4477. ;i++)push_onecapture(ms,i);return ms->level;}}static int str_find(lua_State*L)
  4478. {size_t l1,l2;const char*s=luaL_checklstring(L,1,&l1);const char*p=
  4479. luaL_checklstring(L,2,&l2);sint32 init=posrelat(luaL_optlong(L,3,1),l1)-1;if(
  4480. init<0)init=0;else if((size_t)(init)>l1)init=(sint32)l1;if(lua_toboolean(L,4)
  4481. ||strpbrk(p,SPECIALS)==NULL){const char*s2=lmemfind(s+init,l1-init,p,l2);if(s2
  4482. ){lua_pushnumber(L,(lua_Number)(s2-s+1));lua_pushnumber(L,(lua_Number)(s2-s+l2
  4483. ));return 2;}}else{MatchState ms;int anchor=(*p=='^')?(p++,1):0;const char*s1=
  4484. s+init;ms.L=L;ms.src_init=s;ms.src_end=s+l1;do{const char*res;ms.level=0;if((
  4485. res=match(&ms,s1,p))!=NULL){lua_pushnumber(L,(lua_Number)(s1-s+1));
  4486. lua_pushnumber(L,(lua_Number)(res-s));return push_captures(&ms,NULL,0)+2;}}
  4487. while(s1++<ms.src_end&&!anchor);}lua_pushnil(L);return 1;}static int gfind_aux
  4488. (lua_State*L){MatchState ms;const char*s=lua_tostring(L,lua_upvalueindex(1));
  4489. size_t ls=lua_strlen(L,lua_upvalueindex(1));const char*p=lua_tostring(L,
  4490. lua_upvalueindex(2));const char*src;ms.L=L;ms.src_init=s;ms.src_end=s+ls;for(
  4491. src=s+(size_t)lua_tonumber(L,lua_upvalueindex(3));src<=ms.src_end;src++){const
  4492. char*e;ms.level=0;if((e=match(&ms,src,p))!=NULL){int newstart=e-s;if(e==src)
  4493. newstart++;lua_pushnumber(L,(lua_Number)newstart);lua_replace(L,
  4494. lua_upvalueindex(3));return push_captures(&ms,src,e);}}return 0;}static int
  4495. gfind(lua_State*L){luaL_checkstring(L,1);luaL_checkstring(L,2);lua_settop(L,2)
  4496. ;lua_pushnumber(L,0);lua_pushcclosure(L,gfind_aux,3);return 1;}static void
  4497. add_s(MatchState*ms,luaL_Buffer*b,const char*s,const char*e){lua_State*L=ms->L
  4498. ;if(lua_isstring(L,3)){const char*news=lua_tostring(L,3);size_t l=lua_strlen(L
  4499. ,3);size_t i;for(i=0;i<l;i++){if(news[i]!=ESC)luaL_putchar(b,news[i]);else{i++
  4500. ;if(!isdigit(uchar(news[i])))luaL_putchar(b,news[i]);else{int level=
  4501. check_capture(ms,news[i]);push_onecapture(ms,level);luaL_addvalue(b);}}}}else{
  4502. int n;lua_pushvalue(L,3);n=push_captures(ms,s,e);lua_call(L,n,1);if(
  4503. lua_isstring(L,-1))luaL_addvalue(b);else lua_pop(L,1);}}static int str_gsub(
  4504. lua_State*L){size_t srcl;const char*src=luaL_checklstring(L,1,&srcl);const
  4505. char*p=luaL_checkstring(L,2);int max_s=luaL_optint(L,4,srcl+1);int anchor=(*p
  4506. =='^')?(p++,1):0;int n=0;MatchState ms;luaL_Buffer b;luaL_argcheck(L,
  4507. lua_gettop(L)>=3&&(lua_isstring(L,3)||lua_isfunction(L,3)),3,
  4508. "string or function expected");luaL_buffinit(L,&b);ms.L=L;ms.src_init=src;ms.
  4509. src_end=src+srcl;while(n<max_s){const char*e;ms.level=0;e=match(&ms,src,p);if(
  4510. e){n++;add_s(&ms,&b,src,e);}if(e&&e>src)src=e;else if(src<ms.src_end)
  4511. luaL_putchar(&b,*src++);else break;if(anchor)break;}luaL_addlstring(&b,src,ms.
  4512. src_end-src);luaL_pushresult(&b);lua_pushnumber(L,(lua_Number)n);return 2;}
  4513. #define MAX_ITEM 512
  4514. #define MAX_FORMAT 20
  4515. static void luaI_addquoted(lua_State*L,luaL_Buffer*b,int arg){size_t l;const
  4516. char*s=luaL_checklstring(L,arg,&l);luaL_putchar(b,'"');while(l--){switch(*s){
  4517. case'"':case'\\':case'\n':{luaL_putchar(b,'\\');luaL_putchar(b,*s);break;}case
  4518. '\0':{luaL_addlstring(b,"\\000",4);break;}default:{luaL_putchar(b,*s);break;}}
  4519. s++;}luaL_putchar(b,'"');}static const char*scanformat(lua_State*L,const char*
  4520. strfrmt,char*form,int*hasprecision){const char*p=strfrmt;while(strchr("-+ #0",
  4521. *p))p++;if(isdigit(uchar(*p)))p++;if(isdigit(uchar(*p)))p++;if(*p=='.'){p++;*
  4522. hasprecision=1;if(isdigit(uchar(*p)))p++;if(isdigit(uchar(*p)))p++;}if(isdigit
  4523. (uchar(*p)))luaL_error(L,"invalid format (width or precision too long)");if(p-
  4524. strfrmt+2>MAX_FORMAT)luaL_error(L,"invalid format (too long)");form[0]='%';
  4525. strncpy(form+1,strfrmt,p-strfrmt+1);form[p-strfrmt+2]=0;return p;}static int
  4526. str_format(lua_State*L){int arg=1;size_t sfl;const char*strfrmt=
  4527. luaL_checklstring(L,arg,&sfl);const char*strfrmt_end=strfrmt+sfl;luaL_Buffer b
  4528. ;luaL_buffinit(L,&b);while(strfrmt<strfrmt_end){if(*strfrmt!='%')luaL_putchar(
  4529. &b,*strfrmt++);else if(*++strfrmt=='%')luaL_putchar(&b,*strfrmt++);else{char
  4530. form[MAX_FORMAT];char buff[MAX_ITEM];int hasprecision=0;if(isdigit(uchar(*
  4531. strfrmt))&&*(strfrmt+1)=='$')return luaL_error(L,
  4532. "obsolete option (d$) to `format'");arg++;strfrmt=scanformat(L,strfrmt,form,&
  4533. hasprecision);switch(*strfrmt++){case'c':case'd':case'i':{sprintf(buff,form,
  4534. luaL_checkint(L,arg));break;}case'o':case'u':case'x':case'X':{sprintf(buff,
  4535. form,(unsigned int)(luaL_checknumber(L,arg)));break;}case'e':case'E':case'f':
  4536. case'g':case'G':{sprintf(buff,form,luaL_checknumber(L,arg));break;}case'q':{
  4537. luaI_addquoted(L,&b,arg);continue;}case's':{size_t l;const char*s=
  4538. luaL_checklstring(L,arg,&l);if(!hasprecision&&l>=100){lua_pushvalue(L,arg);
  4539. luaL_addvalue(&b);continue;}else{sprintf(buff,form,s);break;}}default:{return
  4540. luaL_error(L,"invalid option to `format'");}}luaL_addlstring(&b,buff,strlen(
  4541. buff));}}luaL_pushresult(&b);return 1;}static const luaL_reg strlib[]={{"len",
  4542. str_len},{"sub",str_sub},{"lower",str_lower},{"upper",str_upper},{"char",
  4543. str_char},{"rep",str_rep},{"byte",str_byte},{"format",str_format},{"dump",
  4544. str_dump},{"find",str_find},{"gfind",gfind},{"gsub",str_gsub},{NULL,NULL}};
  4545. LUALIB_API int luaopen_string(lua_State*L){luaL_openlib(L,LUA_STRLIBNAME,
  4546. strlib,0);return 1;}
  4547. #line 1 "ltable.c"
  4548. #define ltable_c
  4549. #if BITS_INT >26
  4550. #define MAXBITS 24
  4551. #else
  4552. #define MAXBITS (BITS_INT-2)
  4553. #endif
  4554. #define toobig(x) ((((x)-1)>>MAXBITS)!=0)
  4555. #ifndef lua_number2int
  4556. #define lua_number2int(i,n) ((i)=(int)(n))
  4557. #endif
  4558. #define hashpow2(t,n) (gnode(t,lmod((n),sizenode(t))))
  4559. #define hashstr(t,str) hashpow2(t,(str)->tsv.hash)
  4560. #define hashboolean(t,p) hashpow2(t,p)
  4561. #define hashmod(t,n) (gnode(t,((n)%((sizenode(t)-1)|1))))
  4562. #define hashpointer(t,p) hashmod(t,IntPoint(p))
  4563. #define numints cast(int,sizeof(lua_Number)/sizeof(int))
  4564. static Node*hashnum(const Table*t,lua_Number n){unsigned int a[numints];int i;
  4565. n+=1;lua_assert(sizeof(a)<=sizeof(n));memcpy(a,&n,sizeof(a));for(i=1;i<numints
  4566. ;i++)a[0]+=a[i];return hashmod(t,cast(lu_hash,a[0]));}Node*luaH_mainposition(
  4567. const Table*t,const TObject*key){switch(ttype(key)){case LUA_TNUMBER:return
  4568. hashnum(t,nvalue(key));case LUA_TSTRING:return hashstr(t,tsvalue(key));case
  4569. LUA_TBOOLEAN:return hashboolean(t,bvalue(key));case LUA_TLIGHTUSERDATA:return
  4570. hashpointer(t,pvalue(key));default:return hashpointer(t,gcvalue(key));}}static
  4571. int arrayindex(const TObject*key){if(ttisnumber(key)){int k;lua_number2int(k,
  4572. (nvalue(key)));if(cast(lua_Number,k)==nvalue(key)&&k>=1&&!toobig(k))return k;}
  4573. return-1;}static int luaH_index(lua_State*L,Table*t,StkId key){int i;if(
  4574. ttisnil(key))return-1;i=arrayindex(key);if(0<=i&&i<=t->sizearray){return i-1;}
  4575. else{const TObject*v=luaH_get(t,key);if(v==&luaO_nilobject)luaG_runerror(L,
  4576. "invalid key for `next'");i=cast(int,(cast(const lu_byte*,v)-cast(const
  4577. lu_byte*,gval(gnode(t,0))))/sizeof(Node));return i+t->sizearray;}}int
  4578. luaH_next(lua_State*L,Table*t,StkId key){int i=luaH_index(L,t,key);for(i++;i<t
  4579. ->sizearray;i++){if(!ttisnil(&t->array[i])){setnvalue(key,cast(lua_Number,i+1)
  4580. );setobj2s(key+1,&t->array[i]);return 1;}}for(i-=t->sizearray;i<sizenode(t);i
  4581. ++){if(!ttisnil(gval(gnode(t,i)))){setobj2s(key,gkey(gnode(t,i)));setobj2s(key
  4582. +1,gval(gnode(t,i)));return 1;}}return 0;}static void computesizes(int nums[],
  4583. int ntotal,int*narray,int*nhash){int i;int a=nums[0];int na=a;int n=(na==0)?-1
  4584. :0;for(i=1;a<*narray&&*narray>=twoto(i-1);i++){if(nums[i]>0){a+=nums[i];if(a>=
  4585. twoto(i-1)){n=i;na=a;}}}lua_assert(na<=*narray&&*narray<=ntotal);*nhash=ntotal
  4586. -na;*narray=(n==-1)?0:twoto(n);lua_assert(na<=*narray&&na>=*narray/2);}static
  4587. void numuse(const Table*t,int*narray,int*nhash){int nums[MAXBITS+1];int i,lg;
  4588. int totaluse=0;for(i=0,lg=0;lg<=MAXBITS;lg++){int ttlg=twoto(lg);if(ttlg>t->
  4589. sizearray){ttlg=t->sizearray;if(i>=ttlg)break;}nums[lg]=0;for(;i<ttlg;i++){if(
  4590. !ttisnil(&t->array[i])){nums[lg]++;totaluse++;}}}for(;lg<=MAXBITS;lg++)nums[lg
  4591. ]=0;*narray=totaluse;i=sizenode(t);while(i--){Node*n=&t->node[i];if(!ttisnil(
  4592. gval(n))){int k=arrayindex(gkey(n));if(k>=0){nums[luaO_log2(k-1)+1]++;(*narray
  4593. )++;}totaluse++;}}computesizes(nums,totaluse,narray,nhash);}static void
  4594. setarrayvector(lua_State*L,Table*t,int size){int i;luaM_reallocvector(L,t->
  4595. array,t->sizearray,size,TObject);for(i=t->sizearray;i<size;i++)setnilvalue(&t
  4596. ->array[i]);t->sizearray=size;}static void setnodevector(lua_State*L,Table*t,
  4597. int lsize){int i;int size=twoto(lsize);if(lsize>MAXBITS)luaG_runerror(L,
  4598. "table overflow");if(lsize==0){t->node=G(L)->dummynode;lua_assert(ttisnil(gkey
  4599. (t->node)));lua_assert(ttisnil(gval(t->node)));lua_assert(t->node->next==NULL)
  4600. ;}else{t->node=luaM_newvector(L,size,Node);for(i=0;i<size;i++){t->node[i].next
  4601. =NULL;setnilvalue(gkey(gnode(t,i)));setnilvalue(gval(gnode(t,i)));}}t->
  4602. lsizenode=cast(lu_byte,lsize);t->firstfree=gnode(t,size-1);}static void resize
  4603. (lua_State*L,Table*t,int nasize,int nhsize){int i;int oldasize=t->sizearray;
  4604. int oldhsize=t->lsizenode;Node*nold;Node temp[1];if(oldhsize)nold=t->node;else
  4605. {lua_assert(t->node==G(L)->dummynode);temp[0]=t->node[0];nold=temp;setnilvalue
  4606. (gkey(G(L)->dummynode));setnilvalue(gval(G(L)->dummynode));lua_assert(G(L)->
  4607. dummynode->next==NULL);}if(nasize>oldasize)setarrayvector(L,t,nasize);
  4608. setnodevector(L,t,nhsize);if(nasize<oldasize){t->sizearray=nasize;for(i=nasize
  4609. ;i<oldasize;i++){if(!ttisnil(&t->array[i]))setobjt2t(luaH_setnum(L,t,i+1),&t->
  4610. array[i]);}luaM_reallocvector(L,t->array,oldasize,nasize,TObject);}for(i=twoto
  4611. (oldhsize)-1;i>=0;i--){Node*old=nold+i;if(!ttisnil(gval(old)))setobjt2t(
  4612. luaH_set(L,t,gkey(old)),gval(old));}if(oldhsize)luaM_freearray(L,nold,twoto(
  4613. oldhsize),Node);}static void rehash(lua_State*L,Table*t){int nasize,nhsize;
  4614. numuse(t,&nasize,&nhsize);resize(L,t,nasize,luaO_log2(nhsize)+1);}Table*
  4615. luaH_new(lua_State*L,int narray,int lnhash){Table*t=luaM_new(L,Table);
  4616. luaC_link(L,valtogco(t),LUA_TTABLE);t->metatable=hvalue(defaultmeta(L));t->
  4617. flags=cast(lu_byte,~0);t->array=NULL;t->sizearray=0;t->lsizenode=0;t->node=
  4618. NULL;setarrayvector(L,t,narray);setnodevector(L,t,lnhash);return t;}void
  4619. luaH_free(lua_State*L,Table*t){if(t->lsizenode)luaM_freearray(L,t->node,
  4620. sizenode(t),Node);luaM_freearray(L,t->array,t->sizearray,TObject);luaM_freelem
  4621. (L,t);}
  4622. #if 0
  4623. void luaH_remove(Table*t,Node*e){Node*mp=luaH_mainposition(t,gkey(e));if(e!=mp
  4624. ){while(mp->next!=e)mp=mp->next;mp->next=e->next;}else{if(e->next!=NULL)??}
  4625. lua_assert(ttisnil(gval(node)));setnilvalue(gkey(e));e->next=NULL;}
  4626. #endif
  4627. static TObject*newkey(lua_State*L,Table*t,const TObject*key){TObject*val;Node*
  4628. mp=luaH_mainposition(t,key);if(!ttisnil(gval(mp))){Node*othern=
  4629. luaH_mainposition(t,gkey(mp));Node*n=t->firstfree;if(othern!=mp){while(othern
  4630. ->next!=mp)othern=othern->next;othern->next=n;*n=*mp;mp->next=NULL;setnilvalue
  4631. (gval(mp));}else{n->next=mp->next;mp->next=n;mp=n;}}setobj2t(gkey(mp),key);
  4632. lua_assert(ttisnil(gval(mp)));for(;;){if(ttisnil(gkey(t->firstfree)))return
  4633. gval(mp);else if(t->firstfree==t->node)break;else(t->firstfree)--;}setbvalue(
  4634. gval(mp),0);rehash(L,t);val=cast(TObject*,luaH_get(t,key));lua_assert(
  4635. ttisboolean(val));setnilvalue(val);return val;}static const TObject*
  4636. luaH_getany(Table*t,const TObject*key){if(ttisnil(key))return&luaO_nilobject;
  4637. else{Node*n=luaH_mainposition(t,key);do{if(luaO_rawequalObj(gkey(n),key))
  4638. return gval(n);else n=n->next;}while(n);return&luaO_nilobject;}}const TObject*
  4639. luaH_getnum(Table*t,int key){if(1<=key&&key<=t->sizearray)return&t->array[key-
  4640. 1];else{lua_Number nk=cast(lua_Number,key);Node*n=hashnum(t,nk);do{if(
  4641. ttisnumber(gkey(n))&&nvalue(gkey(n))==nk)return gval(n);else n=n->next;}while(
  4642. n);return&luaO_nilobject;}}const TObject*luaH_getstr(Table*t,TString*key){Node
  4643. *n=hashstr(t,key);do{if(ttisstring(gkey(n))&&tsvalue(gkey(n))==key)return gval
  4644. (n);else n=n->next;}while(n);return&luaO_nilobject;}const TObject*luaH_get(
  4645. Table*t,const TObject*key){switch(ttype(key)){case LUA_TSTRING:return
  4646. luaH_getstr(t,tsvalue(key));case LUA_TNUMBER:{int k;lua_number2int(k,(nvalue(
  4647. key)));if(cast(lua_Number,k)==nvalue(key))return luaH_getnum(t,k);}default:
  4648. return luaH_getany(t,key);}}TObject*luaH_set(lua_State*L,Table*t,const TObject
  4649. *key){const TObject*p=luaH_get(t,key);t->flags=0;if(p!=&luaO_nilobject)return
  4650. cast(TObject*,p);else{if(ttisnil(key))luaG_runerror(L,"table index is nil");
  4651. else if(ttisnumber(key)&&nvalue(key)!=nvalue(key))luaG_runerror(L,
  4652. "table index is NaN");return newkey(L,t,key);}}TObject*luaH_setnum(lua_State*L
  4653. ,Table*t,int key){const TObject*p=luaH_getnum(t,key);if(p!=&luaO_nilobject)
  4654. return cast(TObject*,p);else{TObject k;setnvalue(&k,cast(lua_Number,key));
  4655. return newkey(L,t,&k);}}
  4656. #line 1 "ltablib.c"
  4657. #define ltablib_c
  4658. #define aux_getn(L,n) (luaL_checktype(L,n,LUA_TTABLE),luaL_getn(L,n))
  4659. static int luaB_foreachi(lua_State*L){int i;int n=aux_getn(L,1);luaL_checktype
  4660. (L,2,LUA_TFUNCTION);for(i=1;i<=n;i++){lua_pushvalue(L,2);lua_pushnumber(L,(
  4661. lua_Number)i);lua_rawgeti(L,1,i);lua_call(L,2,1);if(!lua_isnil(L,-1))return 1;
  4662. lua_pop(L,1);}return 0;}static int luaB_foreach(lua_State*L){luaL_checktype(L,
  4663. 1,LUA_TTABLE);luaL_checktype(L,2,LUA_TFUNCTION);lua_pushnil(L);for(;;){if(
  4664. lua_next(L,1)==0)return 0;lua_pushvalue(L,2);lua_pushvalue(L,-3);lua_pushvalue
  4665. (L,-3);lua_call(L,2,1);if(!lua_isnil(L,-1))return 1;lua_pop(L,2);}}static int
  4666. luaB_getn(lua_State*L){lua_pushnumber(L,(lua_Number)aux_getn(L,1));return 1;}
  4667. static int luaB_setn(lua_State*L){luaL_checktype(L,1,LUA_TTABLE);luaL_setn(L,1
  4668. ,luaL_checkint(L,2));return 0;}static int luaB_tinsert(lua_State*L){int v=
  4669. lua_gettop(L);int n=aux_getn(L,1)+1;int pos;if(v==2)pos=n;else{pos=
  4670. luaL_checkint(L,2);if(pos>n)n=pos;v=3;}luaL_setn(L,1,n);while(--n>=pos){
  4671. lua_rawgeti(L,1,n);lua_rawseti(L,1,n+1);}lua_pushvalue(L,v);lua_rawseti(L,1,
  4672. pos);return 0;}static int luaB_tremove(lua_State*L){int n=aux_getn(L,1);int
  4673. pos=luaL_optint(L,2,n);if(n<=0)return 0;luaL_setn(L,1,n-1);lua_rawgeti(L,1,pos
  4674. );for(;pos<n;pos++){lua_rawgeti(L,1,pos+1);lua_rawseti(L,1,pos);}lua_pushnil(L
  4675. );lua_rawseti(L,1,n);return 1;}static int str_concat(lua_State*L){luaL_Buffer
  4676. b;size_t lsep;const char*sep=luaL_optlstring(L,2,"",&lsep);int i=luaL_optint(L
  4677. ,3,1);int n=luaL_optint(L,4,0);luaL_checktype(L,1,LUA_TTABLE);if(n==0)n=
  4678. luaL_getn(L,1);luaL_buffinit(L,&b);for(;i<=n;i++){lua_rawgeti(L,1,i);
  4679. luaL_argcheck(L,lua_isstring(L,-1),1,"table contains non-strings");
  4680. luaL_addvalue(&b);if(i!=n)luaL_addlstring(&b,sep,lsep);}luaL_pushresult(&b);
  4681. return 1;}static void set2(lua_State*L,int i,int j){lua_rawseti(L,1,i);
  4682. lua_rawseti(L,1,j);}static int sort_comp(lua_State*L,int a,int b){if(!
  4683. lua_isnil(L,2)){int res;lua_pushvalue(L,2);lua_pushvalue(L,a-1);lua_pushvalue(
  4684. L,b-2);lua_call(L,2,1);res=lua_toboolean(L,-1);lua_pop(L,1);return res;}else
  4685. return lua_lessthan(L,a,b);}static void auxsort(lua_State*L,int l,int u){while
  4686. (l<u){int i,j;lua_rawgeti(L,1,l);lua_rawgeti(L,1,u);if(sort_comp(L,-1,-2))set2
  4687. (L,l,u);else lua_pop(L,2);if(u-l==1)break;i=(l+u)/2;lua_rawgeti(L,1,i);
  4688. lua_rawgeti(L,1,l);if(sort_comp(L,-2,-1))set2(L,i,l);else{lua_pop(L,1);
  4689. lua_rawgeti(L,1,u);if(sort_comp(L,-1,-2))set2(L,i,u);else lua_pop(L,2);}if(u-l
  4690. ==2)break;lua_rawgeti(L,1,i);lua_pushvalue(L,-1);lua_rawgeti(L,1,u-1);set2(L,i
  4691. ,u-1);i=l;j=u-1;for(;;){while(lua_rawgeti(L,1,++i),sort_comp(L,-1,-2)){if(i>u)
  4692. luaL_error(L,"invalid order function for sorting");lua_pop(L,1);}while(
  4693. lua_rawgeti(L,1,--j),sort_comp(L,-3,-1)){if(j<l)luaL_error(L,
  4694. "invalid order function for sorting");lua_pop(L,1);}if(j<i){lua_pop(L,3);break
  4695. ;}set2(L,i,j);}lua_rawgeti(L,1,u-1);lua_rawgeti(L,1,i);set2(L,u-1,i);if(i-l<u-
  4696. i){j=l;i=i-1;l=i+2;}else{j=i+1;i=u;u=j-2;}auxsort(L,j,i);}}static int
  4697. luaB_sort(lua_State*L){int n=aux_getn(L,1);luaL_checkstack(L,40,"");if(!
  4698. lua_isnoneornil(L,2))luaL_checktype(L,2,LUA_TFUNCTION);lua_settop(L,2);auxsort
  4699. (L,1,n);return 0;}static const luaL_reg tab_funcs[]={{"concat",str_concat},{
  4700. "foreach",luaB_foreach},{"foreachi",luaB_foreachi},{"getn",luaB_getn},{"setn",
  4701. luaB_setn},{"sort",luaB_sort},{"insert",luaB_tinsert},{"remove",luaB_tremove},
  4702. {NULL,NULL}};LUALIB_API int luaopen_table(lua_State*L){luaL_openlib(L,
  4703. LUA_TABLIBNAME,tab_funcs,0);return 1;}
  4704. #line 1 "ltests.c"
  4705. #define ltests_c
  4706. #ifdef LUA_DEBUG
  4707. #define lua_pushintegral(L,i) lua_pushnumber(L,cast(lua_Number,(i)))
  4708. static lua_State*lua_state=NULL;int islocked=0;
  4709. #define func_at(L,k) (L->ci->base+(k)-1)
  4710. static void setnameval(lua_State*L,const char*name,int val){lua_pushstring(L,
  4711. name);lua_pushintegral(L,val);lua_settable(L,-3);}
  4712. #define MARK 0x55
  4713. #ifndef EXTERNMEMCHECK
  4714. #define HEADER (sizeof(L_Umaxalign))
  4715. #define MARKSIZE 16
  4716. #define blockhead(b) (cast(char*,b)-HEADER)
  4717. #define setsize(newblock, size)(*cast(size_t*,newblock)=size)
  4718. #define checkblocksize(b, size)(size==(*cast(size_t*,blockhead(b))))
  4719. #define fillmem(mem,size) memset(mem,-MARK,size)
  4720. #else
  4721. #define HEADER 0
  4722. #define MARKSIZE 0
  4723. #define blockhead(b) (b)
  4724. #define setsize(newblock, size)
  4725. #define checkblocksize(b,size) (1)
  4726. #define fillmem(mem,size)
  4727. #endif
  4728. unsigned long memdebug_numblocks=0;unsigned long memdebug_total=0;unsigned
  4729. long memdebug_maxmem=0;unsigned long memdebug_memlimit=ULONG_MAX;static void*
  4730. checkblock(void*block,size_t size){void*b=blockhead(block);int i;for(i=0;i<
  4731. MARKSIZE;i++)lua_assert(*(cast(char*,b)+HEADER+size+i)==MARK+i);return b;}
  4732. static void freeblock(void*block,size_t size){if(block){lua_assert(
  4733. checkblocksize(block,size));block=checkblock(block,size);fillmem(block,size+
  4734. HEADER+MARKSIZE);free(block);memdebug_numblocks--;memdebug_total-=size;}}void*
  4735. debug_realloc(void*block,size_t oldsize,size_t size){lua_assert(oldsize==0||
  4736. checkblocksize(block,oldsize));lua_assert(block!=NULL||size>0);if(size==0){
  4737. freeblock(block,oldsize);return NULL;}else if(size>oldsize&&memdebug_total+
  4738. size-oldsize>memdebug_memlimit)return NULL;else{void*newblock;int i;size_t
  4739. realsize=HEADER+size+MARKSIZE;size_t commonsize=(oldsize<size)?oldsize:size;if
  4740. (realsize<size)return NULL;newblock=malloc(realsize);if(newblock==NULL)return
  4741. NULL;if(block){memcpy(cast(char*,newblock)+HEADER,block,commonsize);freeblock(
  4742. block,oldsize);}fillmem(cast(char*,newblock)+HEADER+commonsize,size-commonsize
  4743. );memdebug_total+=size;if(memdebug_total>memdebug_maxmem)memdebug_maxmem=
  4744. memdebug_total;memdebug_numblocks++;setsize(newblock,size);for(i=0;i<MARKSIZE;
  4745. i++)*(cast(char*,newblock)+HEADER+size+i)=cast(char,MARK+i);return cast(char*,
  4746. newblock)+HEADER;}}static char*buildop(Proto*p,int pc,char*buff){Instruction i
  4747. =p->code[pc];OpCode o=GET_OPCODE(i);const char*name=luaP_opnames[o];int line=
  4748. getline(p,pc);sprintf(buff,"(%4d) %4d - ",line,pc);switch(getOpMode(o)){case
  4749. iABC:sprintf(buff+strlen(buff),"%-12s%4d %4d %4d",name,GETARG_A(i),GETARG_B(i)
  4750. ,GETARG_C(i));break;case iABx:sprintf(buff+strlen(buff),"%-12s%4d %4d",name,
  4751. GETARG_A(i),GETARG_Bx(i));break;case iAsBx:sprintf(buff+strlen(buff),
  4752. "%-12s%4d %4d",name,GETARG_A(i),GETARG_sBx(i));break;}return buff;}
  4753. #if 0
  4754. void luaI_printcode(Proto*pt,int size){int pc;for(pc=0;pc<size;pc++){char buff
  4755. [100];printf("%s\n",buildop(pt,pc,buff));}printf("-------\n");}
  4756. #endif
  4757. static int listcode(lua_State*L){int pc;Proto*p;luaL_argcheck(L,lua_isfunction
  4758. (L,1)&&!lua_iscfunction(L,1),1,"Lua function expected");p=clvalue(func_at(L,1)
  4759. )->l.p;lua_newtable(L);setnameval(L,"maxstack",p->maxstacksize);setnameval(L,
  4760. "numparams",p->numparams);for(pc=0;pc<p->sizecode;pc++){char buff[100];
  4761. lua_pushintegral(L,pc+1);lua_pushstring(L,buildop(p,pc,buff));lua_settable(L,-
  4762. 3);}return 1;}static int listk(lua_State*L){Proto*p;int i;luaL_argcheck(L,
  4763. lua_isfunction(L,1)&&!lua_iscfunction(L,1),1,"Lua function expected");p=
  4764. clvalue(func_at(L,1))->l.p;lua_newtable(L);for(i=0;i<p->sizek;i++){
  4765. lua_pushintegral(L,i+1);luaA_pushobject(L,p->k+i);lua_settable(L,-3);}return 1
  4766. ;}static int listlocals(lua_State*L){Proto*p;int pc=luaL_checkint(L,2)-1;int i
  4767. =0;const char*name;luaL_argcheck(L,lua_isfunction(L,1)&&!lua_iscfunction(L,1),
  4768. 1,"Lua function expected");p=clvalue(func_at(L,1))->l.p;while((name=
  4769. luaF_getlocalname(p,++i,pc))!=NULL)lua_pushstring(L,name);return i-1;}static
  4770. int get_limits(lua_State*L){lua_newtable(L);setnameval(L,"BITS_INT",BITS_INT);
  4771. setnameval(L,"LFPF",LFIELDS_PER_FLUSH);setnameval(L,"MAXVARS",MAXVARS);
  4772. setnameval(L,"MAXPARAMS",MAXPARAMS);setnameval(L,"MAXSTACK",MAXSTACK);
  4773. setnameval(L,"MAXUPVALUES",MAXUPVALUES);return 1;}static int mem_query(
  4774. lua_State*L){if(lua_isnone(L,1)){lua_pushintegral(L,memdebug_total);
  4775. lua_pushintegral(L,memdebug_numblocks);lua_pushintegral(L,memdebug_maxmem);
  4776. return 3;}else{memdebug_memlimit=luaL_checkint(L,1);return 0;}}static int
  4777. hash_query(lua_State*L){if(lua_isnone(L,2)){luaL_argcheck(L,lua_type(L,1)==
  4778. LUA_TSTRING,1,"string expected");lua_pushintegral(L,tsvalue(func_at(L,1))->tsv
  4779. .hash);}else{TObject*o=func_at(L,1);Table*t;luaL_checktype(L,2,LUA_TTABLE);t=
  4780. hvalue(func_at(L,2));lua_pushintegral(L,luaH_mainposition(t,o)-t->node);}
  4781. return 1;}static int stacklevel(lua_State*L){unsigned long a=0;
  4782. lua_pushintegral(L,(int)(L->top-L->stack));lua_pushintegral(L,(int)(L->
  4783. stack_last-L->stack));lua_pushintegral(L,(int)(L->ci-L->base_ci));
  4784. lua_pushintegral(L,(int)(L->end_ci-L->base_ci));lua_pushintegral(L,(unsigned
  4785. long)&a);return 5;}static int table_query(lua_State*L){const Table*t;int i=
  4786. luaL_optint(L,2,-1);luaL_checktype(L,1,LUA_TTABLE);t=hvalue(func_at(L,1));if(i
  4787. ==-1){lua_pushintegral(L,t->sizearray);lua_pushintegral(L,sizenode(t));
  4788. lua_pushintegral(L,t->firstfree-t->node);}else if(i<t->sizearray){
  4789. lua_pushintegral(L,i);luaA_pushobject(L,&t->array[i]);lua_pushnil(L);}else if(
  4790. (i-=t->sizearray)<sizenode(t)){if(!ttisnil(gval(gnode(t,i)))||ttisnil(gkey(
  4791. gnode(t,i)))||ttisnumber(gkey(gnode(t,i)))){luaA_pushobject(L,gkey(gnode(t,i))
  4792. );}else lua_pushstring(L,"<undef>");luaA_pushobject(L,gval(gnode(t,i)));if(t->
  4793. node[i].next)lua_pushintegral(L,t->node[i].next-t->node);else lua_pushnil(L);}
  4794. return 3;}static int string_query(lua_State*L){stringtable*tb=&G(L)->strt;int
  4795. s=luaL_optint(L,2,0)-1;if(s==-1){lua_pushintegral(L,tb->nuse);lua_pushintegral
  4796. (L,tb->size);return 2;}else if(s<tb->size){GCObject*ts;int n=0;for(ts=tb->hash
  4797. [s];ts;ts=ts->gch.next){setsvalue2s(L->top,gcotots(ts));incr_top(L);n++;}
  4798. return n;}return 0;}static int tref(lua_State*L){int level=lua_gettop(L);int
  4799. lock=luaL_optint(L,2,1);luaL_checkany(L,1);lua_pushvalue(L,1);lua_pushintegral
  4800. (L,lua_ref(L,lock));assert(lua_gettop(L)==level+1);return 1;}static int getref
  4801. (lua_State*L){int level=lua_gettop(L);lua_getref(L,luaL_checkint(L,1));assert(
  4802. lua_gettop(L)==level+1);return 1;}static int unref(lua_State*L){int level=
  4803. lua_gettop(L);lua_unref(L,luaL_checkint(L,1));assert(lua_gettop(L)==level);
  4804. return 0;}static int metatable(lua_State*L){luaL_checkany(L,1);if(lua_isnone(L
  4805. ,2)){if(lua_getmetatable(L,1)==0)lua_pushnil(L);}else{lua_settop(L,2);
  4806. luaL_checktype(L,2,LUA_TTABLE);lua_setmetatable(L,1);}return 1;}static int
  4807. upvalue(lua_State*L){int n=luaL_checkint(L,2);luaL_checktype(L,1,LUA_TFUNCTION
  4808. );if(lua_isnone(L,3)){const char*name=lua_getupvalue(L,1,n);if(name==NULL)
  4809. return 0;lua_pushstring(L,name);return 2;}else{const char*name=lua_setupvalue(
  4810. L,1,n);lua_pushstring(L,name);return 1;}}static int newuserdata(lua_State*L){
  4811. size_t size=luaL_checkint(L,1);char*p=cast(char*,lua_newuserdata(L,size));
  4812. while(size--)*p++='\0';return 1;}static int pushuserdata(lua_State*L){
  4813. lua_pushlightuserdata(L,cast(void*,luaL_checkint(L,1)));return 1;}static int
  4814. udataval(lua_State*L){lua_pushintegral(L,cast(int,lua_touserdata(L,1)));return
  4815. 1;}static int doonnewstack(lua_State*L){lua_State*L1=lua_newthread(L);size_t
  4816. l;const char*s=luaL_checklstring(L,1,&l);int status=luaL_loadbuffer(L1,s,l,s);
  4817. if(status==0)status=lua_pcall(L1,0,0,0);lua_pushintegral(L,status);return 1;}
  4818. static int s2d(lua_State*L){lua_pushnumber(L,*cast(const double*,
  4819. luaL_checkstring(L,1)));return 1;}static int d2s(lua_State*L){double d=
  4820. luaL_checknumber(L,1);lua_pushlstring(L,cast(char*,&d),sizeof(d));return 1;}
  4821. static int newstate(lua_State*L){lua_State*L1=lua_open();if(L1){
  4822. lua_userstateopen(L1);lua_pushintegral(L,(unsigned long)L1);}else lua_pushnil(
  4823. L);return 1;}static int loadlib(lua_State*L){static const luaL_reg libs[]={{
  4824. "mathlibopen",luaopen_math},{"strlibopen",luaopen_string},{"iolibopen",
  4825. luaopen_io},{"tablibopen",luaopen_table},{"dblibopen",luaopen_debug},{
  4826. "baselibopen",luaopen_base},{NULL,NULL}};lua_State*L1=cast(lua_State*,cast(
  4827. unsigned long,luaL_checknumber(L,1)));lua_pushvalue(L1,LUA_GLOBALSINDEX);
  4828. luaL_openlib(L1,NULL,libs,0);return 0;}static int closestate(lua_State*L){
  4829. lua_State*L1=cast(lua_State*,cast(unsigned long,luaL_checknumber(L,1)));
  4830. lua_close(L1);lua_unlock(L);return 0;}static int doremote(lua_State*L){
  4831. lua_State*L1=cast(lua_State*,cast(unsigned long,luaL_checknumber(L,1)));size_t
  4832. lcode;const char*code=luaL_checklstring(L,2,&lcode);int status;lua_settop(L1,
  4833. 0);status=luaL_loadbuffer(L1,code,lcode,code);if(status==0)status=lua_pcall(L1
  4834. ,0,LUA_MULTRET,0);if(status!=0){lua_pushnil(L);lua_pushintegral(L,status);
  4835. lua_pushstring(L,lua_tostring(L1,-1));return 3;}else{int i=0;while(!lua_isnone
  4836. (L1,++i))lua_pushstring(L,lua_tostring(L1,i));lua_pop(L1,i-1);return i-1;}}
  4837. static int log2_aux(lua_State*L){lua_pushintegral(L,luaO_log2(luaL_checkint(L,
  4838. 1)));return 1;}static int int2fb_aux(lua_State*L){int b=luaO_int2fb(
  4839. luaL_checkint(L,1));lua_pushintegral(L,b);lua_pushintegral(L,fb2int(b));return
  4840. 2;}static int test_do(lua_State*L){const char*p=luaL_checkstring(L,1);if(*p==
  4841. '@')lua_dofile(L,p+1);else lua_dostring(L,p);return lua_gettop(L);}static
  4842. const char*const delimits=" \t\n,;";static void skip(const char**pc){while(**
  4843. pc!='\0'&&strchr(delimits,**pc))(*pc)++;}static int getnum_aux(lua_State*L,
  4844. const char**pc){int res=0;int sig=1;skip(pc);if(**pc=='.'){res=cast(int,
  4845. lua_tonumber(L,-1));lua_pop(L,1);(*pc)++;return res;}else if(**pc=='-'){sig=-1
  4846. ;(*pc)++;}while(isdigit(cast(int,**pc)))res=res*10+(*(*pc)++)-'0';return sig*
  4847. res;}static const char*getname_aux(char*buff,const char**pc){int i=0;skip(pc);
  4848. while(**pc!='\0'&&!strchr(delimits,**pc))buff[i++]=*(*pc)++;buff[i]='\0';
  4849. return buff;}
  4850. #define EQ(s1) (strcmp(s1,inst)==0)
  4851. #define getnum (getnum_aux(L,&pc))
  4852. #define getname (getname_aux(buff,&pc))
  4853. static int testC(lua_State*L){char buff[30];const char*pc=luaL_checkstring(L,1
  4854. );for(;;){const char*inst=getname;if EQ("")return 0;else if EQ("isnumber"){
  4855. lua_pushintegral(L,lua_isnumber(L,getnum));}else if EQ("isstring"){
  4856. lua_pushintegral(L,lua_isstring(L,getnum));}else if EQ("istable"){
  4857. lua_pushintegral(L,lua_istable(L,getnum));}else if EQ("iscfunction"){
  4858. lua_pushintegral(L,lua_iscfunction(L,getnum));}else if EQ("isfunction"){
  4859. lua_pushintegral(L,lua_isfunction(L,getnum));}else if EQ("isuserdata"){
  4860. lua_pushintegral(L,lua_isuserdata(L,getnum));}else if EQ("isudataval"){
  4861. lua_pushintegral(L,lua_islightuserdata(L,getnum));}else if EQ("isnil"){
  4862. lua_pushintegral(L,lua_isnil(L,getnum));}else if EQ("isnull"){lua_pushintegral
  4863. (L,lua_isnone(L,getnum));}else if EQ("tonumber"){lua_pushnumber(L,lua_tonumber
  4864. (L,getnum));}else if EQ("tostring"){const char*s=lua_tostring(L,getnum);
  4865. lua_pushstring(L,s);}else if EQ("strlen"){lua_pushintegral(L,lua_strlen(L,
  4866. getnum));}else if EQ("tocfunction"){lua_pushcfunction(L,lua_tocfunction(L,
  4867. getnum));}else if EQ("return"){return getnum;}else if EQ("gettop"){
  4868. lua_pushintegral(L,lua_gettop(L));}else if EQ("settop"){lua_settop(L,getnum);}
  4869. else if EQ("pop"){lua_pop(L,getnum);}else if EQ("pushnum"){lua_pushintegral(L,
  4870. getnum);}else if EQ("pushnil"){lua_pushnil(L);}else if EQ("pushbool"){
  4871. lua_pushboolean(L,getnum);}else if EQ("tobool"){lua_pushintegral(L,
  4872. lua_toboolean(L,getnum));}else if EQ("pushvalue"){lua_pushvalue(L,getnum);}
  4873. else if EQ("pushcclosure"){lua_pushcclosure(L,testC,getnum);}else if EQ(
  4874. "pushupvalues"){lua_pushupvalues(L);}else if EQ("remove"){lua_remove(L,getnum)
  4875. ;}else if EQ("insert"){lua_insert(L,getnum);}else if EQ("replace"){lua_replace
  4876. (L,getnum);}else if EQ("gettable"){lua_gettable(L,getnum);}else if EQ(
  4877. "settable"){lua_settable(L,getnum);}else if EQ("next"){lua_next(L,-2);}else if
  4878. EQ("concat"){lua_concat(L,getnum);}else if EQ("lessthan"){int a=getnum;
  4879. lua_pushboolean(L,lua_lessthan(L,a,getnum));}else if EQ("equal"){int a=getnum;
  4880. lua_pushboolean(L,lua_equal(L,a,getnum));}else if EQ("rawcall"){int narg=
  4881. getnum;int nres=getnum;lua_call(L,narg,nres);}else if EQ("call"){int narg=
  4882. getnum;int nres=getnum;lua_pcall(L,narg,nres,0);}else if EQ("loadstring"){
  4883. size_t sl;const char*s=luaL_checklstring(L,getnum,&sl);luaL_loadbuffer(L,s,sl,
  4884. s);}else if EQ("loadfile"){luaL_loadfile(L,luaL_checkstring(L,getnum));}else
  4885. if EQ("setmetatable"){lua_setmetatable(L,getnum);}else if EQ("getmetatable"){
  4886. if(lua_getmetatable(L,getnum)==0)lua_pushnil(L);}else if EQ("type"){
  4887. lua_pushstring(L,lua_typename(L,lua_type(L,getnum)));}else if EQ("getn"){int i
  4888. =getnum;lua_pushintegral(L,luaL_getn(L,i));}else if EQ("setn"){int i=getnum;
  4889. int n=cast(int,lua_tonumber(L,-1));luaL_setn(L,i,n);lua_pop(L,1);}else
  4890. luaL_error(L,"unknown instruction %s",buff);}return 0;}static void yieldf(
  4891. lua_State*L,lua_Debug*ar){lua_yield(L,0);}static int setyhook(lua_State*L){if(
  4892. lua_isnoneornil(L,1))lua_sethook(L,NULL,0,0);else{const char*smask=
  4893. luaL_checkstring(L,1);int count=luaL_optint(L,2,0);int mask=0;if(strchr(smask,
  4894. 'l'))mask|=LUA_MASKLINE;if(count>0)mask|=LUA_MASKCOUNT;lua_sethook(L,yieldf,
  4895. mask,count);}return 0;}static int coresume(lua_State*L){int status;lua_State*
  4896. co=lua_tothread(L,1);luaL_argcheck(L,co,1,"coroutine expected");status=
  4897. lua_resume(co,0);if(status!=0){lua_pushboolean(L,0);lua_insert(L,-2);return 2;
  4898. }else{lua_pushboolean(L,1);return 1;}}static const struct luaL_reg tests_funcs
  4899. []={{"hash",hash_query},{"limits",get_limits},{"listcode",listcode},{"listk",
  4900. listk},{"listlocals",listlocals},{"loadlib",loadlib},{"stacklevel",stacklevel}
  4901. ,{"querystr",string_query},{"querytab",table_query},{"doit",test_do},{"testC",
  4902. testC},{"ref",tref},{"getref",getref},{"unref",unref},{"d2s",d2s},{"s2d",s2d},
  4903. {"metatable",metatable},{"upvalue",upvalue},{"newuserdata",newuserdata},{
  4904. "pushuserdata",pushuserdata},{"udataval",udataval},{"doonnewstack",
  4905. doonnewstack},{"newstate",newstate},{"closestate",closestate},{"doremote",
  4906. doremote},{"log2",log2_aux},{"int2fb",int2fb_aux},{"totalmem",mem_query},{
  4907. "resume",coresume},{"setyhook",setyhook},{NULL,NULL}};static void fim(void){if
  4908. (!islocked)lua_close(lua_state);lua_assert(memdebug_numblocks==0);lua_assert(
  4909. memdebug_total==0);}static int l_panic(lua_State*L){UNUSED(L);fprintf(stderr,
  4910. "unable to recover; exiting\n");return 0;}int luaB_opentests(lua_State*L){
  4911. lua_atpanic(L,l_panic);lua_userstateopen(L);lua_state=L;luaL_openlib(L,"T",
  4912. tests_funcs,0);atexit(fim);return 0;}
  4913. #undef main
  4914. int main(int argc,char*argv[]){char*limit=getenv("MEMLIMIT");if(limit)
  4915. memdebug_memlimit=strtoul(limit,NULL,10);l_main(argc,argv);return 0;}
  4916. #endif
  4917. #line 1 "ltm.c"
  4918. #define ltm_c
  4919. const char*const luaT_typenames[]={"nil","boolean","userdata","number",
  4920. "string","table","function","userdata","thread"};void luaT_init(lua_State*L){
  4921. static const char*const luaT_eventname[]={"__index","__newindex","__gc",
  4922. "__mode","__eq","__add","__sub","__mul","__div","__pow","__unm","__lt","__le",
  4923. "__concat","__call"};int i;for(i=0;i<TM_N;i++){G(L)->tmname[i]=luaS_new(L,
  4924. luaT_eventname[i]);luaS_fix(G(L)->tmname[i]);}}const TObject*luaT_gettm(Table*
  4925. events,TMS event,TString*ename){const TObject*tm=luaH_getstr(events,ename);
  4926. lua_assert(event<=TM_EQ);if(ttisnil(tm)){events->flags|=cast(lu_byte,1u<<event
  4927. );return NULL;}else return tm;}const TObject*luaT_gettmbyobj(lua_State*L,const
  4928. TObject*o,TMS event){TString*ename=G(L)->tmname[event];switch(ttype(o)){case
  4929. LUA_TTABLE:return luaH_getstr(hvalue(o)->metatable,ename);case LUA_TUSERDATA:
  4930. return luaH_getstr(uvalue(o)->uv.metatable,ename);default:return&
  4931. luaO_nilobject;}}
  4932. #line 1 "lua.c"
  4933. #define lua_c
  4934. #ifdef LUA_USERCONFIG
  4935. #include LUA_USERCONFIG
  4936. #endif
  4937. #ifdef _POSIX_C_SOURCE
  4938. #define stdin_is_tty() isatty(0)
  4939. #else
  4940. #define stdin_is_tty() 1
  4941. #endif
  4942. #ifndef PROMPT
  4943. #define PROMPT "> "
  4944. #endif
  4945. #ifndef PROMPT2
  4946. #define PROMPT2 ">> "
  4947. #endif
  4948. #ifndef PROGNAME
  4949. #define PROGNAME "lua"
  4950. #endif
  4951. #ifndef lua_userinit
  4952. #define lua_userinit(L) openstdlibs(L)
  4953. #endif
  4954. #ifndef LUA_EXTRALIBS
  4955. #define LUA_EXTRALIBS
  4956. #endif
  4957. static lua_State*L=NULL;static const char*progname=PROGNAME;LUALIB_API int
  4958. luaopen_posix(lua_State*L);static const luaL_reg lualibs[]={{"base",
  4959. luaopen_base},{"table",luaopen_table},{"io",luaopen_io},{"string",
  4960. luaopen_string},{"debug",luaopen_debug},{"loadlib",luaopen_loadlib},{"posix",
  4961. luaopen_posix},LUA_EXTRALIBS{NULL,NULL}};static void lstop(lua_State*l,
  4962. lua_Debug*ar){(void)ar;lua_sethook(l,NULL,0,0);luaL_error(l,"interrupted!");}
  4963. static void laction(int i){signal(i,SIG_DFL);lua_sethook(L,lstop,LUA_MASKCALL|
  4964. LUA_MASKRET|LUA_MASKCOUNT,1);}static void print_usage(void){fprintf(stderr,
  4965. "usage: %s [options] [script [args]].\n""Available options are:\n"
  4966. " - execute stdin as a file\n"" -e stat execute string `stat'\n"
  4967. " -i enter interactive mode after executing `script'\n"
  4968. " -l name load and run library `name'\n"
  4969. " -v show version information\n"" -- stop handling options\n",
  4970. progname);}static void l_message(const char*pname,const char*msg){if(pname)
  4971. fprintf(stderr,"%s: ",pname);fprintf(stderr,"%s\n",msg);}static int report(int
  4972. status){const char*msg;if(status){msg=lua_tostring(L,-1);if(msg==NULL)msg=
  4973. "(error with no message)";l_message(progname,msg);lua_pop(L,1);}return status;
  4974. }static int lcall(int narg,int clear){int status;int base=lua_gettop(L)-narg;
  4975. lua_pushliteral(L,"_TRACEBACK");lua_rawget(L,LUA_GLOBALSINDEX);lua_insert(L,
  4976. base);signal(SIGINT,laction);status=lua_pcall(L,narg,(clear?0:LUA_MULTRET),
  4977. base);signal(SIGINT,SIG_DFL);lua_remove(L,base);return status;}static void
  4978. print_version(void){l_message(NULL,LUA_VERSION" "LUA_COPYRIGHT);}static void
  4979. getargs(char*argv[],int n){int i;lua_newtable(L);for(i=0;argv[i];i++){
  4980. lua_pushnumber(L,i-n);lua_pushstring(L,argv[i]);lua_rawset(L,-3);}
  4981. lua_pushliteral(L,"n");lua_pushnumber(L,i-n-1);lua_rawset(L,-3);}static int
  4982. docall(int status){if(status==0)status=lcall(0,1);return report(status);}
  4983. static int file_input(const char*name){return docall(luaL_loadfile(L,name));}
  4984. static int dostring(const char*s,const char*name){return docall(
  4985. luaL_loadbuffer(L,s,strlen(s),name));}static int load_file(const char*name){
  4986. lua_pushliteral(L,"require");lua_rawget(L,LUA_GLOBALSINDEX);if(!lua_isfunction
  4987. (L,-1)){lua_pop(L,1);return file_input(name);}else{lua_pushstring(L,name);
  4988. return report(lcall(1,1));}}
  4989. #ifndef lua_saveline
  4990. #define lua_saveline(L,line)
  4991. #endif
  4992. #ifndef lua_readline
  4993. #define lua_readline(L,prompt) readline(L,prompt)
  4994. #ifndef MAXINPUT
  4995. #define MAXINPUT 512
  4996. #endif
  4997. static int readline(lua_State*l,const char*prompt){static char buffer[MAXINPUT
  4998. ];if(prompt){fputs(prompt,stdout);fflush(stdout);}if(fgets(buffer,sizeof(
  4999. buffer),stdin)==NULL)return 0;else{lua_pushstring(l,buffer);return 1;}}
  5000. #endif
  5001. static const char*get_prompt(int firstline){const char*p=NULL;lua_pushstring(L
  5002. ,firstline?"_PROMPT":"_PROMPT2");lua_rawget(L,LUA_GLOBALSINDEX);p=lua_tostring
  5003. (L,-1);if(p==NULL)p=(firstline?PROMPT:PROMPT2);lua_pop(L,1);return p;}static
  5004. int incomplete(int status){if(status==LUA_ERRSYNTAX&&strstr(lua_tostring(L,-1)
  5005. ,"near `<eof>'")!=NULL){lua_pop(L,1);return 1;}else return 0;}static int
  5006. load_string(void){int status;lua_settop(L,0);if(lua_readline(L,get_prompt(1))
  5007. ==0)return-1;if(lua_tostring(L,-1)[0]=='='){lua_pushfstring(L,"return %s",
  5008. lua_tostring(L,-1)+1);lua_remove(L,-2);}for(;;){status=luaL_loadbuffer(L,
  5009. lua_tostring(L,1),lua_strlen(L,1),"=stdin");if(!incomplete(status))break;if(
  5010. lua_readline(L,get_prompt(0))==0)return-1;lua_concat(L,lua_gettop(L));}
  5011. lua_saveline(L,lua_tostring(L,1));lua_remove(L,1);return status;}static void
  5012. manual_input(void){int status;const char*oldprogname=progname;progname=NULL;
  5013. while((status=load_string())!=-1){if(status==0)status=lcall(0,0);report(status
  5014. );if(status==0&&lua_gettop(L)>0){lua_getglobal(L,"print");lua_insert(L,1);if(
  5015. lua_pcall(L,lua_gettop(L)-1,0,0)!=0)l_message(progname,lua_pushfstring(L,
  5016. "error calling `print' (%s)",lua_tostring(L,-1)));}}lua_settop(L,0);fputs("\n"
  5017. ,stdout);progname=oldprogname;}static int handle_argv(char*argv[],int*
  5018. interactive){if(argv[1]==NULL){if(stdin_is_tty()){print_version();manual_input
  5019. ();}else file_input(NULL);}else{int i;for(i=1;argv[i]!=NULL;i++){if(argv[i][0]
  5020. !='-')break;switch(argv[i][1]){case'-':{if(argv[i][2]!='\0'){print_usage();
  5021. return 1;}i++;goto endloop;}case'\0':{file_input(NULL);break;}case'i':{*
  5022. interactive=1;break;}case'v':{print_version();break;}case'e':{const char*chunk
  5023. =argv[i]+2;if(*chunk=='\0')chunk=argv[++i];if(chunk==NULL){print_usage();
  5024. return 1;}if(dostring(chunk,"=<command line>")!=0)return 1;break;}case'l':{
  5025. const char*filename=argv[i]+2;if(*filename=='\0')filename=argv[++i];if(
  5026. filename==NULL){print_usage();return 1;}if(load_file(filename))return 1;break;
  5027. }case'c':{l_message(progname,"option `-c' is deprecated");break;}case's':{
  5028. l_message(progname,"option `-s' is deprecated");break;}default:{print_usage();
  5029. return 1;}}}endloop:if(argv[i]!=NULL){const char*filename=argv[i];getargs(argv
  5030. ,i);lua_setglobal(L,"arg");if(strcmp(filename,"/dev/stdin")==0)filename=NULL;
  5031. return file_input(filename);}}return 0;}static void openstdlibs(lua_State*l){
  5032. const luaL_reg*lib=lualibs;for(;lib->func;lib++){lib->func(l);lua_settop(l,0);
  5033. }}static int handle_luainit(void){const char*init=getenv("LUA_INIT");if(init==
  5034. NULL)return 0;else if(init[0]=='@')return file_input(init+1);else return
  5035. dostring(init,"=LUA_INIT");}struct Smain{int argc;char**argv;int status;};
  5036. static int pmain(lua_State*l){struct Smain*s=(struct Smain*)lua_touserdata(l,1
  5037. );int status;int interactive=0;if(s->argv[0]&&s->argv[0][0])progname=s->argv[0
  5038. ];L=l;lua_userinit(l);status=handle_luainit();if(status==0){status=handle_argv
  5039. (s->argv,&interactive);if(status==0&&interactive)manual_input();}s->status=
  5040. status;return 0;}int main(int argc,char*argv[]){int status;struct Smain s;
  5041. lua_State*l=lua_open();if(l==NULL){l_message(argv[0],
  5042. "cannot create state: not enough memory");return EXIT_FAILURE;}s.argc=argc;s.
  5043. argv=argv;status=lua_cpcall(l,&pmain,&s);report(status);lua_close(l);return(
  5044. status||s.status)?EXIT_FAILURE:EXIT_SUCCESS;}
  5045. #line 1 "lundump.c"
  5046. #define lundump_c
  5047. #define LoadByte (lu_byte)ezgetc
  5048. typedef struct{lua_State*L;ZIO*Z;Mbuffer*b;int swap;const char*name;}LoadState
  5049. ;static void unexpectedEOZ(LoadState*S){luaG_runerror(S->L,
  5050. "unexpected end of file in %s",S->name);}static int ezgetc(LoadState*S){int c=
  5051. zgetc(S->Z);if(c==EOZ)unexpectedEOZ(S);return c;}static void ezread(LoadState*
  5052. S,void*b,int n){int r=luaZ_read(S->Z,b,n);if(r!=0)unexpectedEOZ(S);}static
  5053. void LoadBlock(LoadState*S,void*b,size_t size){if(S->swap){char*p=(char*)b+
  5054. size-1;int n=size;while(n--)*p--=(char)ezgetc(S);}else ezread(S,b,size);}
  5055. static void LoadVector(LoadState*S,void*b,int m,size_t size){if(S->swap){char*
  5056. q=(char*)b;while(m--){char*p=q+size-1;int n=size;while(n--)*p--=(char)ezgetc(S
  5057. );q+=size;}}else ezread(S,b,m*size);}static int LoadInt(LoadState*S){int x;
  5058. LoadBlock(S,&x,sizeof(x));if(x<0)luaG_runerror(S->L,"bad integer in %s",S->
  5059. name);return x;}static size_t LoadSize(LoadState*S){size_t x;LoadBlock(S,&x,
  5060. sizeof(x));return x;}static lua_Number LoadNumber(LoadState*S){lua_Number x;
  5061. LoadBlock(S,&x,sizeof(x));return x;}static TString*LoadString(LoadState*S){
  5062. size_t size=LoadSize(S);if(size==0)return NULL;else{char*s=luaZ_openspace(S->L
  5063. ,S->b,size);ezread(S,s,size);return luaS_newlstr(S->L,s,size-1);}}static void
  5064. LoadCode(LoadState*S,Proto*f){int size=LoadInt(S);f->code=luaM_newvector(S->L,
  5065. size,Instruction);f->sizecode=size;LoadVector(S,f->code,size,sizeof(*f->code))
  5066. ;}static void LoadLocals(LoadState*S,Proto*f){int i,n;n=LoadInt(S);f->locvars=
  5067. luaM_newvector(S->L,n,LocVar);f->sizelocvars=n;for(i=0;i<n;i++){f->locvars[i].
  5068. varname=LoadString(S);f->locvars[i].startpc=LoadInt(S);f->locvars[i].endpc=
  5069. LoadInt(S);}}static void LoadLines(LoadState*S,Proto*f){int size=LoadInt(S);f
  5070. ->lineinfo=luaM_newvector(S->L,size,int);f->sizelineinfo=size;LoadVector(S,f->
  5071. lineinfo,size,sizeof(*f->lineinfo));}static void LoadUpvalues(LoadState*S,
  5072. Proto*f){int i,n;n=LoadInt(S);if(n!=0&&n!=f->nups)luaG_runerror(S->L,
  5073. "bad nupvalues in %s: read %d; expected %d",S->name,n,f->nups);f->upvalues=
  5074. luaM_newvector(S->L,n,TString*);f->sizeupvalues=n;for(i=0;i<n;i++)f->upvalues[
  5075. i]=LoadString(S);}static Proto*LoadFunction(LoadState*S,TString*p);static void
  5076. LoadConstants(LoadState*S,Proto*f){int i,n;n=LoadInt(S);f->k=luaM_newvector(S
  5077. ->L,n,TObject);f->sizek=n;for(i=0;i<n;i++){TObject*o=&f->k[i];int t=LoadByte(S
  5078. );switch(t){case LUA_TNUMBER:setnvalue(o,LoadNumber(S));break;case LUA_TSTRING
  5079. :setsvalue2n(o,LoadString(S));break;case LUA_TNIL:setnilvalue(o);break;default
  5080. :luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);break;}}n=
  5081. LoadInt(S);f->p=luaM_newvector(S->L,n,Proto*);f->sizep=n;for(i=0;i<n;i++)f->p[
  5082. i]=LoadFunction(S,f->source);}static Proto*LoadFunction(LoadState*S,TString*p)
  5083. {Proto*f=luaF_newproto(S->L);f->source=LoadString(S);if(f->source==NULL)f->
  5084. source=p;f->lineDefined=LoadInt(S);f->nups=LoadByte(S);f->numparams=LoadByte(S
  5085. );f->is_vararg=LoadByte(S);f->maxstacksize=LoadByte(S);LoadLines(S,f);
  5086. LoadLocals(S,f);LoadUpvalues(S,f);LoadConstants(S,f);LoadCode(S,f);
  5087. #ifndef TRUST_BINARIES
  5088. if(!luaG_checkcode(f))luaG_runerror(S->L,"bad code in %s",S->name);
  5089. #endif
  5090. return f;}static void LoadSignature(LoadState*S){const char*s=LUA_SIGNATURE;
  5091. while(*s!=0&&ezgetc(S)==*s)++s;if(*s!=0)luaG_runerror(S->L,
  5092. "bad signature in %s",S->name);}static void TestSize(LoadState*S,int s,const
  5093. char*what){int r=LoadByte(S);if(r!=s)luaG_runerror(S->L,
  5094. "virtual machine mismatch in %s: ""size of %s is %d but read %d",S->name,what,
  5095. s,r);}
  5096. #define TESTSIZE(s,w) TestSize(S,s,w)
  5097. #define V(v) v/16,v%16
  5098. static void LoadHeader(LoadState*S){int version;lua_Number x,tx=TEST_NUMBER;
  5099. LoadSignature(S);version=LoadByte(S);if(version>VERSION)luaG_runerror(S->L,
  5100. "%s too new: ""read version %d.%d; expected at most %d.%d",S->name,V(version),
  5101. V(VERSION));if(version<VERSION0)luaG_runerror(S->L,"%s too old: "
  5102. "read version %d.%d; expected at least %d.%d",S->name,V(version),V(VERSION0));
  5103. S->swap=(luaU_endianness()!=LoadByte(S));TESTSIZE(sizeof(int),"int");TESTSIZE(
  5104. sizeof(size_t),"size_t");TESTSIZE(sizeof(Instruction),"Instruction");TESTSIZE(
  5105. SIZE_OP,"OP");TESTSIZE(SIZE_A,"A");TESTSIZE(SIZE_B,"B");TESTSIZE(SIZE_C,"C");
  5106. TESTSIZE(sizeof(lua_Number),"number");x=LoadNumber(S);if((long)x!=(long)tx)
  5107. luaG_runerror(S->L,"unknown number format in %s",S->name);}static Proto*
  5108. LoadChunk(LoadState*S){LoadHeader(S);return LoadFunction(S,NULL);}Proto*
  5109. luaU_undump(lua_State*L,ZIO*Z,Mbuffer*buff){LoadState S;const char*s=zname(Z);
  5110. if(*s=='@'||*s=='=')S.name=s+1;else if(*s==LUA_SIGNATURE[0])S.name=
  5111. "binary string";else S.name=s;S.L=L;S.Z=Z;S.b=buff;return LoadChunk(&S);}int
  5112. luaU_endianness(void){int x=1;return*(char*)&x;}
  5113. #line 1 "lvm.c"
  5114. #define lvm_c
  5115. #ifndef lua_number2str
  5116. #define lua_number2str(s,n) sprintf((s),LUA_NUMBER_FMT,(n))
  5117. #endif
  5118. #define MAXTAGLOOP 100
  5119. const TObject*luaV_tonumber(const TObject*obj,TObject*n){lua_Number num;if(
  5120. ttisnumber(obj))return obj;if(ttisstring(obj)&&luaO_str2d(svalue(obj),&num)){
  5121. setnvalue(n,num);return n;}else return NULL;}int luaV_tostring(lua_State*L,
  5122. StkId obj){if(!ttisnumber(obj))return 0;else{char s[32];lua_number2str(s,
  5123. nvalue(obj));setsvalue2s(obj,luaS_new(L,s));return 1;}}static void traceexec(
  5124. lua_State*L){lu_byte mask=L->hookmask;if(mask&LUA_MASKCOUNT){if(L->hookcount==
  5125. 0){resethookcount(L);luaD_callhook(L,LUA_HOOKCOUNT,-1);return;}}if(mask&
  5126. LUA_MASKLINE){CallInfo*ci=L->ci;Proto*p=ci_func(ci)->l.p;int newline=getline(p
  5127. ,pcRel(*ci->u.l.pc,p));if(!L->hookinit){luaG_inithooks(L);return;}lua_assert(
  5128. ci->state&CI_HASFRAME);if(pcRel(*ci->u.l.pc,p)==0)ci->u.l.savedpc=*ci->u.l.pc;
  5129. if(*ci->u.l.pc<=ci->u.l.savedpc||newline!=getline(p,pcRel(ci->u.l.savedpc,p)))
  5130. {luaD_callhook(L,LUA_HOOKLINE,newline);ci=L->ci;}ci->u.l.savedpc=*ci->u.l.pc;}
  5131. }static void callTMres(lua_State*L,const TObject*f,const TObject*p1,const
  5132. TObject*p2){setobj2s(L->top,f);setobj2s(L->top+1,p1);setobj2s(L->top+2,p2);
  5133. luaD_checkstack(L,3);L->top+=3;luaD_call(L,L->top-3,1);L->top--;}static void
  5134. callTM(lua_State*L,const TObject*f,const TObject*p1,const TObject*p2,const
  5135. TObject*p3){setobj2s(L->top,f);setobj2s(L->top+1,p1);setobj2s(L->top+2,p2);
  5136. setobj2s(L->top+3,p3);luaD_checkstack(L,4);L->top+=4;luaD_call(L,L->top-4,0);}
  5137. static const TObject*luaV_index(lua_State*L,const TObject*t,TObject*key,int
  5138. loop){const TObject*tm=fasttm(L,hvalue(t)->metatable,TM_INDEX);if(tm==NULL)
  5139. return&luaO_nilobject;if(ttisfunction(tm)){callTMres(L,tm,t,key);return L->top
  5140. ;}else return luaV_gettable(L,tm,key,loop);}static const TObject*
  5141. luaV_getnotable(lua_State*L,const TObject*t,TObject*key,int loop){const
  5142. TObject*tm=luaT_gettmbyobj(L,t,TM_INDEX);if(ttisnil(tm))luaG_typeerror(L,t,
  5143. "index");if(ttisfunction(tm)){callTMres(L,tm,t,key);return L->top;}else return
  5144. luaV_gettable(L,tm,key,loop);}const TObject*luaV_gettable(lua_State*L,const
  5145. TObject*t,TObject*key,int loop){if(loop>MAXTAGLOOP)luaG_runerror(L,
  5146. "loop in gettable");if(ttistable(t)){Table*h=hvalue(t);const TObject*v=
  5147. luaH_get(h,key);if(!ttisnil(v))return v;else return luaV_index(L,t,key,loop+1)
  5148. ;}else return luaV_getnotable(L,t,key,loop+1);}void luaV_settable(lua_State*L,
  5149. const TObject*t,TObject*key,StkId val){const TObject*tm;int loop=0;do{if(
  5150. ttistable(t)){Table*h=hvalue(t);TObject*oldval=luaH_set(L,h,key);if(!ttisnil(
  5151. oldval)||(tm=fasttm(L,h->metatable,TM_NEWINDEX))==NULL){setobj2t(oldval,val);
  5152. return;}}else if(ttisnil(tm=luaT_gettmbyobj(L,t,TM_NEWINDEX)))luaG_typeerror(L
  5153. ,t,"index");if(ttisfunction(tm)){callTM(L,tm,t,key,val);return;}t=tm;}while(++
  5154. loop<=MAXTAGLOOP);luaG_runerror(L,"loop in settable");}static int call_binTM(
  5155. lua_State*L,const TObject*p1,const TObject*p2,StkId res,TMS event){ptrdiff_t
  5156. result=savestack(L,res);const TObject*tm=luaT_gettmbyobj(L,p1,event);if(
  5157. ttisnil(tm))tm=luaT_gettmbyobj(L,p2,event);if(!ttisfunction(tm))return 0;
  5158. callTMres(L,tm,p1,p2);res=restorestack(L,result);setobjs2s(res,L->top);return
  5159. 1;}static const TObject*get_compTM(lua_State*L,Table*mt1,Table*mt2,TMS event){
  5160. const TObject*tm1=fasttm(L,mt1,event);const TObject*tm2;if(tm1==NULL)return
  5161. NULL;if(mt1==mt2)return tm1;tm2=fasttm(L,mt2,event);if(tm2==NULL)return NULL;
  5162. if(luaO_rawequalObj(tm1,tm2))return tm1;return NULL;}static int call_orderTM(
  5163. lua_State*L,const TObject*p1,const TObject*p2,TMS event){const TObject*tm1=
  5164. luaT_gettmbyobj(L,p1,event);const TObject*tm2;if(ttisnil(tm1))return-1;tm2=
  5165. luaT_gettmbyobj(L,p2,event);if(!luaO_rawequalObj(tm1,tm2))return-1;callTMres(L
  5166. ,tm1,p1,p2);return!l_isfalse(L->top);}static int luaV_strcmp(const TString*ls,
  5167. const TString*rs){const char*l=getstr(ls);size_t ll=ls->tsv.len;const char*r=
  5168. getstr(rs);size_t lr=rs->tsv.len;for(;;){int temp=strcoll(l,r);if(temp!=0)
  5169. return temp;else{size_t len=strlen(l);if(len==lr)return(len==ll)?0:1;else if(
  5170. len==ll)return-1;len++;l+=len;ll-=len;r+=len;lr-=len;}}}int luaV_lessthan(
  5171. lua_State*L,const TObject*l,const TObject*r){int res;if(ttype(l)!=ttype(r))
  5172. return luaG_ordererror(L,l,r);else if(ttisnumber(l))return nvalue(l)<nvalue(r)
  5173. ;else if(ttisstring(l))return luaV_strcmp(tsvalue(l),tsvalue(r))<0;else if((
  5174. res=call_orderTM(L,l,r,TM_LT))!=-1)return res;return luaG_ordererror(L,l,r);}
  5175. static int luaV_lessequal(lua_State*L,const TObject*l,const TObject*r){int res
  5176. ;if(ttype(l)!=ttype(r))return luaG_ordererror(L,l,r);else if(ttisnumber(l))
  5177. return nvalue(l)<=nvalue(r);else if(ttisstring(l))return luaV_strcmp(tsvalue(l
  5178. ),tsvalue(r))<=0;else if((res=call_orderTM(L,l,r,TM_LE))!=-1)return res;else
  5179. if((res=call_orderTM(L,r,l,TM_LT))!=-1)return!res;return luaG_ordererror(L,l,r
  5180. );}int luaV_equalval(lua_State*L,const TObject*t1,const TObject*t2){const
  5181. TObject*tm;lua_assert(ttype(t1)==ttype(t2));switch(ttype(t1)){case LUA_TNIL:
  5182. return 1;case LUA_TNUMBER:return nvalue(t1)==nvalue(t2);case LUA_TBOOLEAN:
  5183. return bvalue(t1)==bvalue(t2);case LUA_TLIGHTUSERDATA:return pvalue(t1)==
  5184. pvalue(t2);case LUA_TUSERDATA:{if(uvalue(t1)==uvalue(t2))return 1;tm=
  5185. get_compTM(L,uvalue(t1)->uv.metatable,uvalue(t2)->uv.metatable,TM_EQ);break;}
  5186. case LUA_TTABLE:{if(hvalue(t1)==hvalue(t2))return 1;tm=get_compTM(L,hvalue(t1)
  5187. ->metatable,hvalue(t2)->metatable,TM_EQ);break;}default:return gcvalue(t1)==
  5188. gcvalue(t2);}if(tm==NULL)return 0;callTMres(L,tm,t1,t2);return!l_isfalse(L->
  5189. top);}void luaV_concat(lua_State*L,int total,int last){do{StkId top=L->base+
  5190. last+1;int n=2;if(!tostring(L,top-2)||!tostring(L,top-1)){if(!call_binTM(L,top
  5191. -2,top-1,top-2,TM_CONCAT))luaG_concaterror(L,top-2,top-1);}else if(tsvalue(top
  5192. -1)->tsv.len>0){lu_mem tl=cast(lu_mem,tsvalue(top-1)->tsv.len)+cast(lu_mem,
  5193. tsvalue(top-2)->tsv.len);char*buffer;int i;while(n<total&&tostring(L,top-n-1))
  5194. {tl+=tsvalue(top-n-1)->tsv.len;n++;}if(tl>MAX_SIZET)luaG_runerror(L,
  5195. "string size overflow");buffer=luaZ_openspace(L,&G(L)->buff,tl);tl=0;for(i=n;i
  5196. >0;i--){size_t l=tsvalue(top-i)->tsv.len;memcpy(buffer+tl,svalue(top-i),l);tl
  5197. +=l;}setsvalue2s(top-n,luaS_newlstr(L,buffer,tl));}total-=n-1;last-=n-1;}while
  5198. (total>1);}static void Arith(lua_State*L,StkId ra,const TObject*rb,const
  5199. TObject*rc,TMS op){TObject tempb,tempc;const TObject*b,*c;if((b=luaV_tonumber(
  5200. rb,&tempb))!=NULL&&(c=luaV_tonumber(rc,&tempc))!=NULL){switch(op){case TM_ADD:
  5201. setnvalue(ra,nvalue(b)+nvalue(c));break;case TM_SUB:setnvalue(ra,nvalue(b)-
  5202. nvalue(c));break;case TM_MUL:setnvalue(ra,nvalue(b)*nvalue(c));break;case
  5203. TM_DIV:setnvalue(ra,nvalue(b)/nvalue(c));break;case TM_POW:{const TObject*f=
  5204. luaH_getstr(hvalue(gt(L)),G(L)->tmname[TM_POW]);ptrdiff_t res=savestack(L,ra);
  5205. if(!ttisfunction(f))luaG_runerror(L,"`__pow' (`^' operator) is not a function"
  5206. );callTMres(L,f,b,c);ra=restorestack(L,res);setobjs2s(ra,L->top);break;}
  5207. default:lua_assert(0);break;}}else if(!call_binTM(L,rb,rc,ra,op))
  5208. luaG_aritherror(L,rb,rc);}
  5209. #define runtime_check(L, c){if(!(c))return 0;}
  5210. #define RA(i) (base+GETARG_A(i))
  5211. #define XRA(i) (L->base+GETARG_A(i))
  5212. #define RB(i) (base+GETARG_B(i))
  5213. #define RKB(i) ((GETARG_B(i)<MAXSTACK)?RB(i):k+GETARG_B(i)-MAXSTACK)
  5214. #define RC(i) (base+GETARG_C(i))
  5215. #define RKC(i) ((GETARG_C(i)<MAXSTACK)?RC(i):k+GETARG_C(i)-MAXSTACK)
  5216. #define KBx(i) (k+GETARG_Bx(i))
  5217. #define dojump(pc, i)((pc)+=(i))
  5218. StkId luaV_execute(lua_State*L){LClosure*cl;TObject*k;const Instruction*pc;
  5219. callentry:if(L->hookmask&LUA_MASKCALL){L->ci->u.l.pc=&pc;luaD_callhook(L,
  5220. LUA_HOOKCALL,-1);}retentry:L->ci->u.l.pc=&pc;lua_assert(L->ci->state==
  5221. CI_SAVEDPC||L->ci->state==(CI_SAVEDPC|CI_CALLING));L->ci->state=CI_HASFRAME;pc
  5222. =L->ci->u.l.savedpc;cl=&clvalue(L->base-1)->l;k=cl->p->k;for(;;){const
  5223. Instruction i=*pc++;StkId base,ra;if((L->hookmask&(LUA_MASKLINE|LUA_MASKCOUNT)
  5224. )&&(--L->hookcount==0||L->hookmask&LUA_MASKLINE)){traceexec(L);if(L->ci->state
  5225. &CI_YIELD){L->ci->u.l.savedpc=pc-1;L->ci->state=CI_YIELD|CI_SAVEDPC;return
  5226. NULL;}}base=L->base;ra=RA(i);lua_assert(L->ci->state&CI_HASFRAME);lua_assert(
  5227. base==L->ci->base);lua_assert(L->top<=L->stack+L->stacksize&&L->top>=base);
  5228. lua_assert(L->top==L->ci->top||GET_OPCODE(i)==OP_CALL||GET_OPCODE(i)==
  5229. OP_TAILCALL||GET_OPCODE(i)==OP_RETURN||GET_OPCODE(i)==OP_SETLISTO);switch(
  5230. GET_OPCODE(i)){case OP_MOVE:{setobjs2s(ra,RB(i));break;}case OP_LOADK:{
  5231. setobj2s(ra,KBx(i));break;}case OP_LOADBOOL:{setbvalue(ra,GETARG_B(i));if(
  5232. GETARG_C(i))pc++;break;}case OP_LOADNIL:{TObject*rb=RB(i);do{setnilvalue(rb--)
  5233. ;}while(rb>=ra);break;}case OP_GETUPVAL:{int b=GETARG_B(i);setobj2s(ra,cl->
  5234. upvals[b]->v);break;}case OP_GETGLOBAL:{TObject*rb=KBx(i);const TObject*v;
  5235. lua_assert(ttisstring(rb)&&ttistable(&cl->g));v=luaH_getstr(hvalue(&cl->g),
  5236. tsvalue(rb));if(!ttisnil(v)){setobj2s(ra,v);}else setobj2s(XRA(i),luaV_index(L
  5237. ,&cl->g,rb,0));break;}case OP_GETTABLE:{StkId rb=RB(i);TObject*rc=RKC(i);if(
  5238. ttistable(rb)){const TObject*v=luaH_get(hvalue(rb),rc);if(!ttisnil(v)){
  5239. setobj2s(ra,v);}else setobj2s(XRA(i),luaV_index(L,rb,rc,0));}else setobj2s(XRA
  5240. (i),luaV_getnotable(L,rb,rc,0));break;}case OP_SETGLOBAL:{lua_assert(
  5241. ttisstring(KBx(i))&&ttistable(&cl->g));luaV_settable(L,&cl->g,KBx(i),ra);break
  5242. ;}case OP_SETUPVAL:{int b=GETARG_B(i);setobj(cl->upvals[b]->v,ra);break;}case
  5243. OP_SETTABLE:{luaV_settable(L,ra,RKB(i),RKC(i));break;}case OP_NEWTABLE:{int b=
  5244. GETARG_B(i);b=fb2int(b);sethvalue(ra,luaH_new(L,b,GETARG_C(i)));luaC_checkGC(L
  5245. );break;}case OP_SELF:{StkId rb=RB(i);TObject*rc=RKC(i);runtime_check(L,
  5246. ttisstring(rc));setobjs2s(ra+1,rb);if(ttistable(rb)){const TObject*v=
  5247. luaH_getstr(hvalue(rb),tsvalue(rc));if(!ttisnil(v)){setobj2s(ra,v);}else
  5248. setobj2s(XRA(i),luaV_index(L,rb,rc,0));}else setobj2s(XRA(i),luaV_getnotable(L
  5249. ,rb,rc,0));break;}case OP_ADD:{TObject*rb=RKB(i);TObject*rc=RKC(i);if(
  5250. ttisnumber(rb)&&ttisnumber(rc)){setnvalue(ra,nvalue(rb)+nvalue(rc));}else
  5251. Arith(L,ra,rb,rc,TM_ADD);break;}case OP_SUB:{TObject*rb=RKB(i);TObject*rc=RKC(
  5252. i);if(ttisnumber(rb)&&ttisnumber(rc)){setnvalue(ra,nvalue(rb)-nvalue(rc));}
  5253. else Arith(L,ra,rb,rc,TM_SUB);break;}case OP_MUL:{TObject*rb=RKB(i);TObject*rc
  5254. =RKC(i);if(ttisnumber(rb)&&ttisnumber(rc)){setnvalue(ra,nvalue(rb)*nvalue(rc))
  5255. ;}else Arith(L,ra,rb,rc,TM_MUL);break;}case OP_DIV:{TObject*rb=RKB(i);TObject*
  5256. rc=RKC(i);if(ttisnumber(rb)&&ttisnumber(rc)){setnvalue(ra,nvalue(rb)/nvalue(rc
  5257. ));}else Arith(L,ra,rb,rc,TM_DIV);break;}case OP_POW:{Arith(L,ra,RKB(i),RKC(i)
  5258. ,TM_POW);break;}case OP_UNM:{const TObject*rb=RB(i);TObject temp;if(tonumber(
  5259. rb,&temp)){setnvalue(ra,-nvalue(rb));}else{setnilvalue(&temp);if(!call_binTM(L
  5260. ,RB(i),&temp,ra,TM_UNM))luaG_aritherror(L,RB(i),&temp);}break;}case OP_NOT:{
  5261. int res=l_isfalse(RB(i));setbvalue(ra,res);break;}case OP_CONCAT:{int b=
  5262. GETARG_B(i);int c=GETARG_C(i);luaV_concat(L,c-b+1,c);base=L->base;setobjs2s(RA
  5263. (i),base+b);luaC_checkGC(L);break;}case OP_JMP:{dojump(pc,GETARG_sBx(i));break
  5264. ;}case OP_EQ:{if(equalobj(L,RKB(i),RKC(i))!=GETARG_A(i))pc++;else dojump(pc,
  5265. GETARG_sBx(*pc)+1);break;}case OP_LT:{if(luaV_lessthan(L,RKB(i),RKC(i))!=
  5266. GETARG_A(i))pc++;else dojump(pc,GETARG_sBx(*pc)+1);break;}case OP_LE:{if(
  5267. luaV_lessequal(L,RKB(i),RKC(i))!=GETARG_A(i))pc++;else dojump(pc,GETARG_sBx(*
  5268. pc)+1);break;}case OP_TEST:{TObject*rb=RB(i);if(l_isfalse(rb)==GETARG_C(i))pc
  5269. ++;else{setobjs2s(ra,rb);dojump(pc,GETARG_sBx(*pc)+1);}break;}case OP_CALL:
  5270. case OP_TAILCALL:{StkId firstResult;int b=GETARG_B(i);int nresults;if(b!=0)L->
  5271. top=ra+b;nresults=GETARG_C(i)-1;firstResult=luaD_precall(L,ra);if(firstResult)
  5272. {if(firstResult>L->top){lua_assert(L->ci->state==(CI_C|CI_YIELD));(L->ci-1)->u
  5273. .l.savedpc=pc;(L->ci-1)->state=CI_SAVEDPC;return NULL;}luaD_poscall(L,nresults
  5274. ,firstResult);if(nresults>=0)L->top=L->ci->top;}else{if(GET_OPCODE(i)==OP_CALL
  5275. ){(L->ci-1)->u.l.savedpc=pc;(L->ci-1)->state=(CI_SAVEDPC|CI_CALLING);}else{int
  5276. aux;base=(L->ci-1)->base;ra=RA(i);if(L->openupval)luaF_close(L,base);for(aux=
  5277. 0;ra+aux<L->top;aux++)setobjs2s(base+aux-1,ra+aux);(L->ci-1)->top=L->top=base+
  5278. aux;lua_assert(L->ci->state&CI_SAVEDPC);(L->ci-1)->u.l.savedpc=L->ci->u.l.
  5279. savedpc;(L->ci-1)->u.l.tailcalls++;(L->ci-1)->state=CI_SAVEDPC;L->ci--;L->base
  5280. =L->ci->base;}goto callentry;}break;}case OP_RETURN:{CallInfo*ci=L->ci-1;int b
  5281. =GETARG_B(i);if(b!=0)L->top=ra+b-1;lua_assert(L->ci->state&CI_HASFRAME);if(L->
  5282. openupval)luaF_close(L,base);L->ci->state=CI_SAVEDPC;L->ci->u.l.savedpc=pc;if(
  5283. !(ci->state&CI_CALLING)){lua_assert((ci->state&CI_C)||ci->u.l.pc!=&pc);return
  5284. ra;}else{int nresults;lua_assert(ttisfunction(ci->base-1)&&(ci->state&
  5285. CI_SAVEDPC));lua_assert(GET_OPCODE(*(ci->u.l.savedpc-1))==OP_CALL);nresults=
  5286. GETARG_C(*(ci->u.l.savedpc-1))-1;luaD_poscall(L,nresults,ra);if(nresults>=0)L
  5287. ->top=L->ci->top;goto retentry;}}case OP_FORLOOP:{lua_Number step,idx,limit;
  5288. const TObject*plimit=ra+1;const TObject*pstep=ra+2;if(!ttisnumber(ra))
  5289. luaG_runerror(L,"`for' initial value must be a number");if(!tonumber(plimit,ra
  5290. +1))luaG_runerror(L,"`for' limit must be a number");if(!tonumber(pstep,ra+2))
  5291. luaG_runerror(L,"`for' step must be a number");step=nvalue(pstep);idx=nvalue(
  5292. ra)+step;limit=nvalue(plimit);if(step>0?idx<=limit:idx>=limit){dojump(pc,
  5293. GETARG_sBx(i));chgnvalue(ra,idx);}break;}case OP_TFORLOOP:{int nvar=GETARG_C(i
  5294. )+1;StkId cb=ra+nvar+2;setobjs2s(cb,ra);setobjs2s(cb+1,ra+1);setobjs2s(cb+2,ra
  5295. +2);L->top=cb+3;luaD_call(L,cb,nvar);L->top=L->ci->top;ra=XRA(i)+2;cb=ra+nvar;
  5296. do{nvar--;setobjs2s(ra+nvar,cb+nvar);}while(nvar>0);if(ttisnil(ra))pc++;else
  5297. dojump(pc,GETARG_sBx(*pc)+1);break;}case OP_TFORPREP:{if(ttistable(ra)){
  5298. setobjs2s(ra+1,ra);setobj2s(ra,luaH_getstr(hvalue(gt(L)),luaS_new(L,"next")));
  5299. }dojump(pc,GETARG_sBx(i));break;}case OP_SETLIST:case OP_SETLISTO:{int bc;int
  5300. n;Table*h;runtime_check(L,ttistable(ra));h=hvalue(ra);bc=GETARG_Bx(i);if(
  5301. GET_OPCODE(i)==OP_SETLIST)n=(bc&(LFIELDS_PER_FLUSH-1))+1;else{n=L->top-ra-1;L
  5302. ->top=L->ci->top;}bc&=~(LFIELDS_PER_FLUSH-1);for(;n>0;n--)setobj2t(luaH_setnum
  5303. (L,h,bc+n),ra+n);break;}case OP_CLOSE:{luaF_close(L,ra);break;}case OP_CLOSURE
  5304. :{Proto*p;Closure*ncl;int nup,j;p=cl->p->p[GETARG_Bx(i)];nup=p->nups;ncl=
  5305. luaF_newLclosure(L,nup,&cl->g);ncl->l.p=p;for(j=0;j<nup;j++,pc++){if(
  5306. GET_OPCODE(*pc)==OP_GETUPVAL)ncl->l.upvals[j]=cl->upvals[GETARG_B(*pc)];else{
  5307. lua_assert(GET_OPCODE(*pc)==OP_MOVE);ncl->l.upvals[j]=luaF_findupval(L,base+
  5308. GETARG_B(*pc));}}setclvalue(ra,ncl);luaC_checkGC(L);break;}}}}
  5309. #line 1 "lzio.c"
  5310. #define lzio_c
  5311. int luaZ_fill(ZIO*z){size_t size;const char*buff=z->reader(NULL,z->data,&size)
  5312. ;if(buff==NULL||size==0)return EOZ;z->n=size-1;z->p=buff;return char2int(*(z->
  5313. p++));}int luaZ_lookahead(ZIO*z){if(z->n==0){int c=luaZ_fill(z);if(c==EOZ)
  5314. return c;z->n++;z->p--;}return char2int(*z->p);}void luaZ_init(ZIO*z,
  5315. lua_Chunkreader reader,void*data,const char*name){z->reader=reader;z->data=
  5316. data;z->name=name;z->n=0;z->p=NULL;}size_t luaZ_read(ZIO*z,void*b,size_t n){
  5317. while(n){size_t m;if(z->n==0){if(luaZ_fill(z)==EOZ)return n;else{++z->n;--z->p
  5318. ;}}m=(n<=z->n)?n:z->n;memcpy(b,z->p,m);z->n-=m;z->p+=m;b=(char*)b+m;n-=m;}
  5319. return 0;}char*luaZ_openspace(lua_State*L,Mbuffer*buff,size_t n){if(n>buff->
  5320. buffsize){if(n<LUA_MINBUFFER)n=LUA_MINBUFFER;luaM_reallocvector(L,buff->buffer
  5321. ,buff->buffsize,n,char);buff->buffsize=n;}return buff->buffer;}