sol.hpp 744 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180
  1. // The MIT License (MIT)
  2. // Copyright (c) 2013-2018 Rapptz, ThePhD and contributors
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  4. // this software and associated documentation files (the "Software"), to deal in
  5. // the Software without restriction, including without limitation the rights to
  6. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7. // the Software, and to permit persons to whom the Software is furnished to do so,
  8. // subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  13. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  14. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  15. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. // This file was generated with a script.
  18. // Generated 2018-11-28 08:50:22.534324 UTC
  19. // This header was generated with sol v2.20.6 (revision 9b782ff)
  20. // https://github.com/ThePhD/sol2
  21. #ifndef SOL_SINGLE_INCLUDE_HPP
  22. #define SOL_SINGLE_INCLUDE_HPP
  23. // beginning of sol.hpp
  24. #ifndef SOL_HPP
  25. #define SOL_HPP
  26. #if defined(UE_BUILD_DEBUG) || defined(UE_BUILD_DEVELOPMENT) || defined(UE_BUILD_TEST) || defined(UE_BUILD_SHIPPING) || defined(UE_SERVER)
  27. #define SOL_INSIDE_UNREAL 1
  28. #endif // Unreal Engine 4 bullshit
  29. #if defined(SOL_INSIDE_UNREAL) && SOL_INSIDE_UNREAL
  30. #ifdef check
  31. #define SOL_INSIDE_UNREAL_REMOVED_CHECK 1
  32. #undef check
  33. #endif
  34. #endif // Unreal Engine 4 Bullshit
  35. #if defined(__GNUC__)
  36. #pragma GCC diagnostic push
  37. #pragma GCC diagnostic ignored "-Wshadow"
  38. #pragma GCC diagnostic ignored "-Wconversion"
  39. #if __GNUC__ > 6
  40. #pragma GCC diagnostic ignored "-Wnoexcept-type"
  41. #endif
  42. #elif defined(__clang__)
  43. #elif defined _MSC_VER
  44. #pragma warning( push )
  45. #pragma warning( disable : 4324 ) // structure was padded due to alignment specifier
  46. #pragma warning( disable : 4503 ) // decorated name horse shit
  47. #pragma warning( disable : 4702 ) // unreachable code
  48. #pragma warning( disable: 4127 ) // 'conditional expression is constant' yeah that's the point your old compilers don't have `if constexpr` you jerk
  49. #pragma warning( disable: 4505 ) // some other nonsense warning
  50. #endif // clang++ vs. g++ vs. VC++
  51. // beginning of sol/forward.hpp
  52. // beginning of sol/feature_test.hpp
  53. #if (defined(__cplusplus) && __cplusplus == 201703L) || (defined(_MSC_VER) && _MSC_VER > 1900 && ((defined(_HAS_CXX17) && _HAS_CXX17 == 1) || (defined(_MSVC_LANG) && (_MSVC_LANG > 201402L))))
  54. #ifndef SOL_CXX17_FEATURES
  55. #define SOL_CXX17_FEATURES 1
  56. #endif // C++17 features macro
  57. #endif // C++17 features check
  58. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  59. #if defined(__cpp_noexcept_function_type) || ((defined(_MSC_VER) && _MSC_VER > 1911) && (defined(_MSVC_LANG) && ((_MSVC_LANG >= 201403L))))
  60. #ifndef SOL_NOEXCEPT_FUNCTION_TYPE
  61. #define SOL_NOEXCEPT_FUNCTION_TYPE 1
  62. #endif // noexcept is part of a function's type
  63. #endif // compiler-specific checks
  64. #if defined(__clang__) && defined(__APPLE__)
  65. #if defined(__has_include)
  66. #if __has_include(<variant>)
  67. #define SOL_STD_VARIANT 1
  68. #endif // has include nonsense
  69. #endif // __has_include
  70. #else
  71. #define SOL_STD_VARIANT 1
  72. #endif // Clang screws up variant
  73. #endif // C++17 only
  74. // beginning of sol/config.hpp
  75. #ifdef _MSC_VER
  76. #if defined(_DEBUG) && !defined(NDEBUG)
  77. #ifndef SOL_IN_DEBUG_DETECTED
  78. #define SOL_IN_DEBUG_DETECTED 1
  79. #endif
  80. #endif // VC++ Debug macros
  81. #ifndef _CPPUNWIND
  82. #ifndef SOL_NO_EXCEPTIONS
  83. #define SOL_NO_EXCEPTIONS 1
  84. #endif
  85. #endif // Automatic Exceptions
  86. #ifndef _CPPRTTI
  87. #ifndef SOL_NO_RTTI
  88. #define SOL_NO_RTTI 1
  89. #endif
  90. #endif // Automatic RTTI
  91. #elif defined(__GNUC__) || defined(__clang__)
  92. #if !defined(NDEBUG) && !defined(__OPTIMIZE__)
  93. #ifndef SOL_IN_DEBUG_DETECTED
  94. #define SOL_IN_DEBUG_DETECTED 1
  95. #endif
  96. #endif // Not Debug && g++ optimizer flag
  97. #ifndef __EXCEPTIONS
  98. #ifndef SOL_NO_EXCEPTIONS
  99. #define SOL_NO_EXCEPTIONS 1
  100. #endif
  101. #endif // No Exceptions
  102. #ifndef __GXX_RTTI
  103. #ifndef SOL_NO_RTII
  104. #define SOL_NO_RTTI 1
  105. #endif
  106. #endif // No RTTI
  107. #endif // vc++ || clang++/g++
  108. #if defined(SOL_CHECK_ARGUMENTS) && SOL_CHECK_ARGUMENTS
  109. // Checks low-level getter function
  110. // (and thusly, affects nearly entire framework)
  111. #if !defined(SOL_SAFE_GETTER)
  112. #define SOL_SAFE_GETTER 1
  113. #endif
  114. // Checks access on usertype functions
  115. // local my_obj = my_type.new()
  116. // my_obj.my_member_function()
  117. // -- bad syntax and crash
  118. #if !defined(SOL_SAFE_USERTYPE)
  119. #define SOL_SAFE_USERTYPE 1
  120. #endif
  121. // Checks sol::reference derived boundaries
  122. // sol::function ref(L, 1);
  123. // sol::userdata sref(L, 2);
  124. #if !defined(SOL_SAFE_REFERENCES)
  125. #define SOL_SAFE_REFERENCES 1
  126. #endif
  127. // Changes all typedefs of sol::function to point to the
  128. // protected_function version, instead of unsafe_function
  129. #if !defined(SOL_SAFE_FUNCTION)
  130. #define SOL_SAFE_FUNCTION 1
  131. #endif
  132. // Checks function parameters and
  133. // returns upon call into/from Lua
  134. // local a = 1
  135. // local b = "woof"
  136. // my_c_function(a, b)
  137. #if !defined(SOL_SAFE_FUNCTION_CALLS)
  138. #define SOL_SAFE_FUNCTION_CALLS 1
  139. #endif
  140. // Checks conversions
  141. // int v = lua["bark"];
  142. // int v2 = my_sol_function();
  143. #if !defined(SOL_SAFE_PROXIES)
  144. #define SOL_SAFE_PROXIES 1
  145. #endif
  146. // Check overflowing number conversions
  147. // for things like 64 bit integers that don't fit in a typical lua_Number
  148. // for Lua 5.1 and 5.2
  149. #if !defined(SOL_SAFE_NUMERICS)
  150. #define SOL_SAFE_NUMERICS 1
  151. #endif
  152. // Turn off Number Precision Checks
  153. // if this is defined, we do not do range
  154. // checks on integers / unsigned integers that might
  155. // be bigger than what Lua can represent
  156. #if !defined(SOL_NO_CHECK_NUMBER_PRECISION)
  157. // off by default
  158. #define SOL_NO_CHECK_NUMBER_PRECISION 0
  159. #endif
  160. #endif // Turn on Safety for all if top-level macro is defined
  161. #if defined(SOL_IN_DEBUG_DETECTED) && SOL_IN_DEBUG_DETECTED
  162. #if !defined(SOL_SAFE_REFERENCES)
  163. // Ensure that references are forcefully type-checked upon construction
  164. #define SOL_SAFE_REFERENCES 1
  165. #endif
  166. // Safe usertypes checks for errors such as
  167. // obj = my_type.new()
  168. // obj.f() -- note the '.' instead of ':'
  169. // usertypes should be safe no matter what
  170. #if !defined(SOL_SAFE_USERTYPE)
  171. #define SOL_SAFE_USERTYPE 1
  172. #endif
  173. #if !defined(SOL_SAFE_FUNCTION_CALLS)
  174. // Function calls from Lua should be automatically safe in debug mode
  175. #define SOL_SAFE_FUNCTION_CALLS 1
  176. #endif
  177. // Print any exceptions / errors that occur
  178. // in debug mode to the default error stream / console
  179. #if !defined(SOL_PRINT_ERRORS)
  180. #define SOL_PRINT_ERRORS 1
  181. #endif
  182. #endif // DEBUG: Turn on all debug safety features for VC++ / g++ / clang++ and similar
  183. #if !defined(SOL_PRINT_ERRORS)
  184. #define SOL_PRINT_ERRORS 0
  185. #endif
  186. #if !defined(SOL_DEFAULT_PASS_ON_ERROR)
  187. #define SOL_DEFAULT_PASS_ON_ERROR 0
  188. #endif
  189. #if !defined(SOL_ENABLE_INTEROP)
  190. #define SOL_ENABLE_INTEROP 0
  191. #endif
  192. #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) || defined(__OBJC__) || defined(nil)
  193. #if !defined(SOL_NO_NIL)
  194. #define SOL_NO_NIL 1
  195. #endif
  196. #endif // avoiding nil defines / keywords
  197. #if defined(SOL_USE_BOOST) && SOL_USE_BOOST
  198. #ifndef SOL_UNORDERED_MAP_COMPATIBLE_HASH
  199. #define SOL_UNORDERED_MAP_COMPATIBLE_HASH 1
  200. #endif // SOL_UNORDERED_MAP_COMPATIBLE_HASH
  201. #endif
  202. #ifndef SOL_STACK_STRING_OPTIMIZATION_SIZE
  203. #define SOL_STACK_STRING_OPTIMIZATION_SIZE 1024
  204. #endif // Optimized conversion routines using a KB or so off the stack
  205. // end of sol/config.hpp
  206. // beginning of sol/config_setup.hpp
  207. // end of sol/config_setup.hpp
  208. // end of sol/feature_test.hpp
  209. namespace sol {
  210. template <bool b>
  211. class basic_reference;
  212. using reference = basic_reference<false>;
  213. using main_reference = basic_reference<true>;
  214. class stack_reference;
  215. struct proxy_base_tag;
  216. template <typename Super>
  217. struct proxy_base;
  218. template <typename Table, typename Key>
  219. struct proxy;
  220. template <typename T>
  221. class usertype;
  222. template <typename T>
  223. class simple_usertype;
  224. template <bool, typename T>
  225. class basic_table_core;
  226. template <bool b>
  227. using table_core = basic_table_core<b, reference>;
  228. template <bool b>
  229. using main_table_core = basic_table_core<b, main_reference>;
  230. template <bool b>
  231. using stack_table_core = basic_table_core<b, stack_reference>;
  232. template <typename T>
  233. using basic_table = basic_table_core<false, T>;
  234. typedef table_core<false> table;
  235. typedef table_core<true> global_table;
  236. typedef main_table_core<false> main_table;
  237. typedef main_table_core<true> main_global_table;
  238. typedef stack_table_core<false> stack_table;
  239. typedef stack_table_core<true> stack_global_table;
  240. template <typename base_t>
  241. struct basic_environment;
  242. using environment = basic_environment<reference>;
  243. using main_environment = basic_environment<main_reference>;
  244. using stack_environment = basic_environment<stack_reference>;
  245. template <typename T, bool>
  246. class basic_function;
  247. template <typename T, bool, typename H>
  248. class basic_protected_function;
  249. using unsafe_function = basic_function<reference, false>;
  250. using safe_function = basic_protected_function<reference, false, reference>;
  251. using main_unsafe_function = basic_function<main_reference, false>;
  252. using main_safe_function = basic_protected_function<main_reference, false, reference>;
  253. using stack_unsafe_function = basic_function<stack_reference, false>;
  254. using stack_safe_function = basic_protected_function<stack_reference, false, reference>;
  255. using stack_aligned_unsafe_function = basic_function<stack_reference, true>;
  256. using stack_aligned_safe_function = basic_protected_function<stack_reference, true, reference>;
  257. using protected_function = safe_function;
  258. using main_protected_function = main_safe_function;
  259. using stack_protected_function = stack_safe_function;
  260. using stack_aligned_protected_function = stack_aligned_safe_function;
  261. #if defined(SOL_SAFE_FUNCTION) && SOL_SAFE_FUNCTION
  262. using function = protected_function;
  263. using main_function = main_protected_function;
  264. using stack_function = stack_protected_function;
  265. #else
  266. using function = unsafe_function;
  267. using main_function = main_unsafe_function;
  268. using stack_function = stack_unsafe_function;
  269. #endif
  270. using stack_aligned_function = stack_aligned_unsafe_function;
  271. using stack_aligned_stack_handler_function = basic_protected_function<stack_reference, true, stack_reference>;
  272. struct unsafe_function_result;
  273. struct protected_function_result;
  274. using safe_function_result = protected_function_result;
  275. #if defined(SOL_SAFE_FUNCTION) && SOL_SAFE_FUNCTION
  276. using function_result = safe_function_result;
  277. #else
  278. using function_result = unsafe_function_result;
  279. #endif
  280. template <typename base_t>
  281. class basic_object;
  282. template <typename base_t>
  283. class basic_userdata;
  284. template <typename base_t>
  285. class basic_lightuserdata;
  286. template <typename base_t>
  287. class basic_coroutine;
  288. template <typename base_t>
  289. class basic_thread;
  290. using object = basic_object<reference>;
  291. using userdata = basic_userdata<reference>;
  292. using lightuserdata = basic_lightuserdata<reference>;
  293. using thread = basic_thread<reference>;
  294. using coroutine = basic_coroutine<reference>;
  295. using main_object = basic_object<main_reference>;
  296. using main_userdata = basic_userdata<main_reference>;
  297. using main_lightuserdata = basic_lightuserdata<main_reference>;
  298. using main_coroutine = basic_coroutine<main_reference>;
  299. using stack_object = basic_object<stack_reference>;
  300. using stack_userdata = basic_userdata<stack_reference>;
  301. using stack_lightuserdata = basic_lightuserdata<stack_reference>;
  302. using stack_thread = basic_thread<stack_reference>;
  303. using stack_coroutine = basic_coroutine<stack_reference>;
  304. struct stack_proxy_base;
  305. struct stack_proxy;
  306. struct variadic_args;
  307. struct variadic_results;
  308. struct stack_count;
  309. struct this_state;
  310. struct this_main_state;
  311. struct this_environment;
  312. template <typename T>
  313. struct as_table_t;
  314. template <typename T>
  315. struct as_container_t;
  316. template <typename T>
  317. struct nested;
  318. template <typename T>
  319. struct light;
  320. template <typename T>
  321. struct user;
  322. template <typename T>
  323. struct as_args_t;
  324. template <typename T>
  325. struct protect_t;
  326. template <typename F, typename... Filters>
  327. struct filter_wrapper;
  328. template <typename T>
  329. struct usertype_traits;
  330. template <typename T>
  331. struct unique_usertype_traits;
  332. } // namespace sol
  333. // end of sol/forward.hpp
  334. // beginning of sol/state.hpp
  335. // beginning of sol/state_view.hpp
  336. // beginning of sol/error.hpp
  337. #include <stdexcept>
  338. #include <string>
  339. namespace sol {
  340. namespace detail {
  341. struct direct_error_tag {};
  342. const auto direct_error = direct_error_tag{};
  343. } // namespace detail
  344. class error : public std::runtime_error {
  345. private:
  346. // Because VC++ is upsetting, most of the time!
  347. std::string w;
  348. public:
  349. error(const std::string& str)
  350. : error(detail::direct_error, "lua: error: " + str) {
  351. }
  352. error(std::string&& str)
  353. : error(detail::direct_error, "lua: error: " + std::move(str)) {
  354. }
  355. error(detail::direct_error_tag, const std::string& str)
  356. : std::runtime_error(""), w(str) {
  357. }
  358. error(detail::direct_error_tag, std::string&& str)
  359. : std::runtime_error(""), w(std::move(str)) {
  360. }
  361. error(const error& e) = default;
  362. error(error&& e) = default;
  363. error& operator=(const error& e) = default;
  364. error& operator=(error&& e) = default;
  365. virtual const char* what() const noexcept override {
  366. return w.c_str();
  367. }
  368. };
  369. } // namespace sol
  370. // end of sol/error.hpp
  371. // beginning of sol/table.hpp
  372. // beginning of sol/table_core.hpp
  373. // beginning of sol/proxy.hpp
  374. // beginning of sol/traits.hpp
  375. // beginning of sol/tuple.hpp
  376. #include <tuple>
  377. #include <cstddef>
  378. namespace sol {
  379. namespace detail {
  380. using swallow = std::initializer_list<int>;
  381. } // namespace detail
  382. template <typename... Args>
  383. struct types {
  384. typedef std::make_index_sequence<sizeof...(Args)> indices;
  385. static constexpr std::size_t size() {
  386. return sizeof...(Args);
  387. }
  388. };
  389. namespace meta {
  390. namespace detail {
  391. template <typename... Args>
  392. struct tuple_types_ { typedef types<Args...> type; };
  393. template <typename... Args>
  394. struct tuple_types_<std::tuple<Args...>> { typedef types<Args...> type; };
  395. } // namespace detail
  396. template <typename T>
  397. using unqualified = std::remove_cv<std::remove_reference_t<T>>;
  398. template <typename T>
  399. using unqualified_t = typename unqualified<T>::type;
  400. template <typename... Args>
  401. using tuple_types = typename detail::tuple_types_<Args...>::type;
  402. template <typename Arg>
  403. struct pop_front_type;
  404. template <typename Arg>
  405. using pop_front_type_t = typename pop_front_type<Arg>::type;
  406. template <typename... Args>
  407. struct pop_front_type<types<Args...>> {
  408. typedef void front_type;
  409. typedef types<Args...> type;
  410. };
  411. template <typename Arg, typename... Args>
  412. struct pop_front_type<types<Arg, Args...>> {
  413. typedef Arg front_type;
  414. typedef types<Args...> type;
  415. };
  416. template <std::size_t N, typename Tuple>
  417. using tuple_element = std::tuple_element<N, std::remove_reference_t<Tuple>>;
  418. template <std::size_t N, typename Tuple>
  419. using tuple_element_t = std::tuple_element_t<N, std::remove_reference_t<Tuple>>;
  420. template <std::size_t N, typename Tuple>
  421. using unqualified_tuple_element = unqualified<tuple_element_t<N, Tuple>>;
  422. template <std::size_t N, typename Tuple>
  423. using unqualified_tuple_element_t = unqualified_t<tuple_element_t<N, Tuple>>;
  424. } // namespace meta
  425. } // namespace sol
  426. // end of sol/tuple.hpp
  427. // beginning of sol/bind_traits.hpp
  428. namespace sol {
  429. namespace meta {
  430. namespace meta_detail {
  431. template <class F>
  432. struct check_deducible_signature {
  433. struct nat {};
  434. template <class G>
  435. static auto test(int) -> decltype(&G::operator(), void());
  436. template <class>
  437. static auto test(...) -> nat;
  438. using type = std::is_void<decltype(test<F>(0))>;
  439. };
  440. } // namespace meta_detail
  441. template <class F>
  442. struct has_deducible_signature : meta_detail::check_deducible_signature<F>::type {};
  443. namespace meta_detail {
  444. template <std::size_t I, typename T>
  445. struct void_tuple_element : meta::tuple_element<I, T> {};
  446. template <std::size_t I>
  447. struct void_tuple_element<I, std::tuple<>> { typedef void type; };
  448. template <std::size_t I, typename T>
  449. using void_tuple_element_t = typename void_tuple_element<I, T>::type;
  450. template <bool it_is_noexcept, bool has_c_variadic, typename T, typename R, typename... Args>
  451. struct basic_traits {
  452. private:
  453. typedef std::conditional_t<std::is_void<T>::value, int, T>& first_type;
  454. public:
  455. static const bool is_noexcept = it_is_noexcept;
  456. static const bool is_member_function = std::is_void<T>::value;
  457. static const bool has_c_var_arg = has_c_variadic;
  458. static const std::size_t arity = sizeof...(Args);
  459. static const std::size_t free_arity = sizeof...(Args) + static_cast<std::size_t>(!std::is_void<T>::value);
  460. typedef types<Args...> args_list;
  461. typedef std::tuple<Args...> args_tuple;
  462. typedef T object_type;
  463. typedef R return_type;
  464. typedef tuple_types<R> returns_list;
  465. typedef R(function_type)(Args...);
  466. typedef std::conditional_t<std::is_void<T>::value, args_list, types<first_type, Args...>> free_args_list;
  467. typedef std::conditional_t<std::is_void<T>::value, R(Args...), R(first_type, Args...)> free_function_type;
  468. typedef std::conditional_t<std::is_void<T>::value, R (*)(Args...), R (*)(first_type, Args...)> free_function_pointer_type;
  469. typedef std::remove_pointer_t<free_function_pointer_type> signature_type;
  470. template <std::size_t i>
  471. using arg_at = void_tuple_element_t<i, args_tuple>;
  472. };
  473. template <typename Signature, bool b = has_deducible_signature<Signature>::value>
  474. struct fx_traits : basic_traits<false, false, void, void> {};
  475. // Free Functions
  476. template <typename R, typename... Args>
  477. struct fx_traits<R(Args...), false> : basic_traits<false, false, void, R, Args...> {
  478. typedef R (*function_pointer_type)(Args...);
  479. };
  480. template <typename R, typename... Args>
  481. struct fx_traits<R (*)(Args...), false> : basic_traits<false, false, void, R, Args...> {
  482. typedef R (*function_pointer_type)(Args...);
  483. };
  484. template <typename R, typename... Args>
  485. struct fx_traits<R(Args..., ...), false> : basic_traits<false, true, void, R, Args...> {
  486. typedef R (*function_pointer_type)(Args..., ...);
  487. };
  488. template <typename R, typename... Args>
  489. struct fx_traits<R (*)(Args..., ...), false> : basic_traits<false, true, void, R, Args...> {
  490. typedef R (*function_pointer_type)(Args..., ...);
  491. };
  492. // Member Functions
  493. /* C-Style Variadics */
  494. template <typename T, typename R, typename... Args>
  495. struct fx_traits<R (T::*)(Args...), false> : basic_traits<false, false, T, R, Args...> {
  496. typedef R (T::*function_pointer_type)(Args...);
  497. };
  498. template <typename T, typename R, typename... Args>
  499. struct fx_traits<R (T::*)(Args..., ...), false> : basic_traits<false, true, T, R, Args...> {
  500. typedef R (T::*function_pointer_type)(Args..., ...);
  501. };
  502. /* Const Volatile */
  503. template <typename T, typename R, typename... Args>
  504. struct fx_traits<R (T::*)(Args...) const, false> : basic_traits<false, false, T, R, Args...> {
  505. typedef R (T::*function_pointer_type)(Args...) const;
  506. };
  507. template <typename T, typename R, typename... Args>
  508. struct fx_traits<R (T::*)(Args..., ...) const, false> : basic_traits<false, true, T, R, Args...> {
  509. typedef R (T::*function_pointer_type)(Args..., ...) const;
  510. };
  511. template <typename T, typename R, typename... Args>
  512. struct fx_traits<R (T::*)(Args...) const volatile, false> : basic_traits<false, false, T, R, Args...> {
  513. typedef R (T::*function_pointer_type)(Args...) const volatile;
  514. };
  515. template <typename T, typename R, typename... Args>
  516. struct fx_traits<R (T::*)(Args..., ...) const volatile, false> : basic_traits<false, true, T, R, Args...> {
  517. typedef R (T::*function_pointer_type)(Args..., ...) const volatile;
  518. };
  519. /* Member Function Qualifiers */
  520. template <typename T, typename R, typename... Args>
  521. struct fx_traits<R (T::*)(Args...)&, false> : basic_traits<false, false, T, R, Args...> {
  522. typedef R (T::*function_pointer_type)(Args...) &;
  523. };
  524. template <typename T, typename R, typename... Args>
  525. struct fx_traits<R (T::*)(Args..., ...)&, false> : basic_traits<false, true, T, R, Args...> {
  526. typedef R (T::*function_pointer_type)(Args..., ...) &;
  527. };
  528. template <typename T, typename R, typename... Args>
  529. struct fx_traits<R (T::*)(Args...) const&, false> : basic_traits<false, false, T, R, Args...> {
  530. typedef R (T::*function_pointer_type)(Args...) const&;
  531. };
  532. template <typename T, typename R, typename... Args>
  533. struct fx_traits<R (T::*)(Args..., ...) const&, false> : basic_traits<false, true, T, R, Args...> {
  534. typedef R (T::*function_pointer_type)(Args..., ...) const&;
  535. };
  536. template <typename T, typename R, typename... Args>
  537. struct fx_traits<R (T::*)(Args...) const volatile&, false> : basic_traits<false, false, T, R, Args...> {
  538. typedef R (T::*function_pointer_type)(Args...) const volatile&;
  539. };
  540. template <typename T, typename R, typename... Args>
  541. struct fx_traits<R (T::*)(Args..., ...) const volatile&, false> : basic_traits<false, true, T, R, Args...> {
  542. typedef R (T::*function_pointer_type)(Args..., ...) const volatile&;
  543. };
  544. template <typename T, typename R, typename... Args>
  545. struct fx_traits<R (T::*)(Args...)&&, false> : basic_traits<false, false, T, R, Args...> {
  546. typedef R (T::*function_pointer_type)(Args...) &&;
  547. };
  548. template <typename T, typename R, typename... Args>
  549. struct fx_traits<R (T::*)(Args..., ...)&&, false> : basic_traits<false, true, T, R, Args...> {
  550. typedef R (T::*function_pointer_type)(Args..., ...) &&;
  551. };
  552. template <typename T, typename R, typename... Args>
  553. struct fx_traits<R (T::*)(Args...) const&&, false> : basic_traits<false, false, T, R, Args...> {
  554. typedef R (T::*function_pointer_type)(Args...) const&&;
  555. };
  556. template <typename T, typename R, typename... Args>
  557. struct fx_traits<R (T::*)(Args..., ...) const&&, false> : basic_traits<false, true, T, R, Args...> {
  558. typedef R (T::*function_pointer_type)(Args..., ...) const&&;
  559. };
  560. template <typename T, typename R, typename... Args>
  561. struct fx_traits<R (T::*)(Args...) const volatile&&, false> : basic_traits<false, false, T, R, Args...> {
  562. typedef R (T::*function_pointer_type)(Args...) const volatile&&;
  563. };
  564. template <typename T, typename R, typename... Args>
  565. struct fx_traits<R (T::*)(Args..., ...) const volatile&&, false> : basic_traits<false, true, T, R, Args...> {
  566. typedef R (T::*function_pointer_type)(Args..., ...) const volatile&&;
  567. };
  568. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  569. template <typename R, typename... Args>
  570. struct fx_traits<R(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  571. typedef R (*function_pointer_type)(Args...) noexcept;
  572. };
  573. template <typename R, typename... Args>
  574. struct fx_traits<R (*)(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  575. typedef R (*function_pointer_type)(Args...) noexcept;
  576. };
  577. template <typename R, typename... Args>
  578. struct fx_traits<R(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  579. typedef R (*function_pointer_type)(Args..., ...) noexcept;
  580. };
  581. template <typename R, typename... Args>
  582. struct fx_traits<R (*)(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  583. typedef R (*function_pointer_type)(Args..., ...) noexcept;
  584. };
  585. template <typename T, typename R, typename... Args>
  586. struct fx_traits<R (T::*)(Args...) noexcept, false> : basic_traits<true, false, T, R, Args...> {
  587. typedef R (T::*function_pointer_type)(Args...) noexcept;
  588. };
  589. template <typename T, typename R, typename... Args>
  590. struct fx_traits<R (T::*)(Args..., ...) noexcept, false> : basic_traits<true, true, T, R, Args...> {
  591. typedef R (T::*function_pointer_type)(Args..., ...) noexcept;
  592. };
  593. /* Const Volatile */
  594. template <typename T, typename R, typename... Args>
  595. struct fx_traits<R (T::*)(Args...) const noexcept, false> : basic_traits<true, false, T, R, Args...> {
  596. typedef R (T::*function_pointer_type)(Args...) const noexcept;
  597. };
  598. template <typename T, typename R, typename... Args>
  599. struct fx_traits<R (T::*)(Args..., ...) const noexcept, false> : basic_traits<true, true, T, R, Args...> {
  600. typedef R (T::*function_pointer_type)(Args..., ...) const noexcept;
  601. };
  602. template <typename T, typename R, typename... Args>
  603. struct fx_traits<R (T::*)(Args...) const volatile noexcept, false> : basic_traits<true, false, T, R, Args...> {
  604. typedef R (T::*function_pointer_type)(Args...) const volatile noexcept;
  605. };
  606. template <typename T, typename R, typename... Args>
  607. struct fx_traits<R (T::*)(Args..., ...) const volatile noexcept, false> : basic_traits<true, true, T, R, Args...> {
  608. typedef R (T::*function_pointer_type)(Args..., ...) const volatile noexcept;
  609. };
  610. template <typename T, typename R, typename... Args>
  611. struct fx_traits<R (T::*)(Args...) & noexcept, false> : basic_traits<true, false, T, R, Args...> {
  612. typedef R (T::*function_pointer_type)(Args...) & noexcept;
  613. };
  614. template <typename T, typename R, typename... Args>
  615. struct fx_traits<R (T::*)(Args..., ...) & noexcept, false> : basic_traits<true, true, T, R, Args...> {
  616. typedef R (T::*function_pointer_type)(Args..., ...) & noexcept;
  617. };
  618. template <typename T, typename R, typename... Args>
  619. struct fx_traits<R (T::*)(Args...) const& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  620. typedef R (T::*function_pointer_type)(Args...) const& noexcept;
  621. };
  622. template <typename T, typename R, typename... Args>
  623. struct fx_traits<R (T::*)(Args..., ...) const& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  624. typedef R (T::*function_pointer_type)(Args..., ...) const& noexcept;
  625. };
  626. template <typename T, typename R, typename... Args>
  627. struct fx_traits<R (T::*)(Args...) const volatile& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  628. typedef R (T::*function_pointer_type)(Args...) const volatile& noexcept;
  629. };
  630. template <typename T, typename R, typename... Args>
  631. struct fx_traits<R (T::*)(Args..., ...) const volatile& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  632. typedef R (T::*function_pointer_type)(Args..., ...) const volatile& noexcept;
  633. };
  634. template <typename T, typename R, typename... Args>
  635. struct fx_traits<R (T::*)(Args...) && noexcept, false> : basic_traits<true, false, T, R, Args...> {
  636. typedef R (T::*function_pointer_type)(Args...) && noexcept;
  637. };
  638. template <typename T, typename R, typename... Args>
  639. struct fx_traits<R (T::*)(Args..., ...) && noexcept, false> : basic_traits<true, true, T, R, Args...> {
  640. typedef R (T::*function_pointer_type)(Args..., ...) && noexcept;
  641. };
  642. template <typename T, typename R, typename... Args>
  643. struct fx_traits<R (T::*)(Args...) const&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  644. typedef R (T::*function_pointer_type)(Args...) const&& noexcept;
  645. };
  646. template <typename T, typename R, typename... Args>
  647. struct fx_traits<R (T::*)(Args..., ...) const&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  648. typedef R (T::*function_pointer_type)(Args..., ...) const&& noexcept;
  649. };
  650. template <typename T, typename R, typename... Args>
  651. struct fx_traits<R (T::*)(Args...) const volatile&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  652. typedef R (T::*function_pointer_type)(Args...) const volatile&& noexcept;
  653. };
  654. template <typename T, typename R, typename... Args>
  655. struct fx_traits<R (T::*)(Args..., ...) const volatile&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  656. typedef R (T::*function_pointer_type)(Args..., ...) const volatile&& noexcept;
  657. };
  658. #endif // noexcept is part of a function's type
  659. #if defined(_MSC_VER) && defined(_M_IX86)
  660. template <typename R, typename... Args>
  661. struct fx_traits<R __stdcall(Args...), false> : basic_traits<false, false, void, R, Args...> {
  662. typedef R(__stdcall* function_pointer_type)(Args...);
  663. };
  664. template <typename R, typename... Args>
  665. struct fx_traits<R(__stdcall*)(Args...), false> : basic_traits<false, false, void, R, Args...> {
  666. typedef R(__stdcall* function_pointer_type)(Args...);
  667. };
  668. template <typename T, typename R, typename... Args>
  669. struct fx_traits<R (__stdcall T::*)(Args...), false> : basic_traits<false, false, T, R, Args...> {
  670. typedef R (__stdcall T::*function_pointer_type)(Args...);
  671. };
  672. /* Const Volatile */
  673. template <typename T, typename R, typename... Args>
  674. struct fx_traits<R (__stdcall T::*)(Args...) const, false> : basic_traits<false, false, T, R, Args...> {
  675. typedef R (__stdcall T::*function_pointer_type)(Args...) const;
  676. };
  677. template <typename T, typename R, typename... Args>
  678. struct fx_traits<R (__stdcall T::*)(Args...) const volatile, false> : basic_traits<false, false, T, R, Args...> {
  679. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile;
  680. };
  681. /* Member Function Qualifiers */
  682. template <typename T, typename R, typename... Args>
  683. struct fx_traits<R (__stdcall T::*)(Args...)&, false> : basic_traits<false, false, T, R, Args...> {
  684. typedef R (__stdcall T::*function_pointer_type)(Args...) &;
  685. };
  686. template <typename T, typename R, typename... Args>
  687. struct fx_traits<R (__stdcall T::*)(Args...) const&, false> : basic_traits<false, false, T, R, Args...> {
  688. typedef R (__stdcall T::*function_pointer_type)(Args...) const&;
  689. };
  690. template <typename T, typename R, typename... Args>
  691. struct fx_traits<R (__stdcall T::*)(Args...) const volatile&, false> : basic_traits<false, false, T, R, Args...> {
  692. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&;
  693. };
  694. template <typename T, typename R, typename... Args>
  695. struct fx_traits<R (__stdcall T::*)(Args...)&&, false> : basic_traits<false, false, T, R, Args...> {
  696. typedef R (__stdcall T::*function_pointer_type)(Args...) &&;
  697. };
  698. template <typename T, typename R, typename... Args>
  699. struct fx_traits<R (__stdcall T::*)(Args...) const&&, false> : basic_traits<false, false, T, R, Args...> {
  700. typedef R (__stdcall T::*function_pointer_type)(Args...) const&&;
  701. };
  702. template <typename T, typename R, typename... Args>
  703. struct fx_traits<R (__stdcall T::*)(Args...) const volatile&&, false> : basic_traits<false, false, T, R, Args...> {
  704. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&&;
  705. };
  706. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  707. template <typename R, typename... Args>
  708. struct fx_traits<R __stdcall(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  709. typedef R(__stdcall* function_pointer_type)(Args...) noexcept;
  710. };
  711. template <typename R, typename... Args>
  712. struct fx_traits<R (__stdcall *)(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  713. typedef R(__stdcall* function_pointer_type)(Args...) noexcept;
  714. };
  715. /* __stdcall cannot be applied to functions with varargs*/
  716. /*template <typename R, typename... Args>
  717. struct fx_traits<__stdcall R(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  718. typedef R(__stdcall* function_pointer_type)(Args..., ...) noexcept;
  719. };
  720. template <typename R, typename... Args>
  721. struct fx_traits<R (__stdcall *)(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  722. typedef R(__stdcall* function_pointer_type)(Args..., ...) noexcept;
  723. };*/
  724. template <typename T, typename R, typename... Args>
  725. struct fx_traits<R (__stdcall T::*)(Args...) noexcept, false> : basic_traits<true, false, T, R, Args...> {
  726. typedef R (__stdcall T::*function_pointer_type)(Args...) noexcept;
  727. };
  728. /* __stdcall does not work with varargs */
  729. /*template <typename T, typename R, typename... Args>
  730. struct fx_traits<R (__stdcall T::*)(Args..., ...) noexcept, false> : basic_traits<true, true, T, R, Args...> {
  731. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) noexcept;
  732. };*/
  733. /* Const Volatile */
  734. template <typename T, typename R, typename... Args>
  735. struct fx_traits<R (__stdcall T::*)(Args...) const noexcept, false> : basic_traits<true, false, T, R, Args...> {
  736. typedef R (__stdcall T::*function_pointer_type)(Args...) const noexcept;
  737. };
  738. /* __stdcall does not work with varargs */
  739. /*template <typename T, typename R, typename... Args>
  740. struct fx_traits<R (__stdcall T::*)(Args..., ...) const noexcept, false> : basic_traits<true, true, T, R, Args...> {
  741. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const noexcept;
  742. };*/
  743. template <typename T, typename R, typename... Args>
  744. struct fx_traits<R (__stdcall T::*)(Args...) const volatile noexcept, false> : basic_traits<true, false, T, R, Args...> {
  745. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile noexcept;
  746. };
  747. /* __stdcall does not work with varargs */
  748. /*template <typename T, typename R, typename... Args>
  749. struct fx_traits<R (__stdcall T::*)(Args..., ...) const volatile noexcept, false> : basic_traits<true, true, T, R, Args...> {
  750. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const volatile noexcept;
  751. };*/
  752. template <typename T, typename R, typename... Args>
  753. struct fx_traits<R (__stdcall T::*)(Args...) & noexcept, false> : basic_traits<true, false, T, R, Args...> {
  754. typedef R (__stdcall T::*function_pointer_type)(Args...) & noexcept;
  755. };
  756. /* __stdcall does not work with varargs */
  757. /*template <typename T, typename R, typename... Args>
  758. struct fx_traits<R (__stdcall T::*)(Args..., ...) & noexcept, false> : basic_traits<true, true, T, R, Args...> {
  759. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) & noexcept;
  760. };*/
  761. template <typename T, typename R, typename... Args>
  762. struct fx_traits<R (__stdcall T::*)(Args...) const& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  763. typedef R (__stdcall T::*function_pointer_type)(Args...) const& noexcept;
  764. };
  765. /* __stdcall does not work with varargs */
  766. /*template <typename T, typename R, typename... Args>
  767. struct fx_traits<R (__stdcall T::*)(Args..., ...) const& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  768. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const& noexcept;
  769. };*/
  770. template <typename T, typename R, typename... Args>
  771. struct fx_traits<R (__stdcall T::*)(Args...) const volatile& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  772. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile& noexcept;
  773. };
  774. /* __stdcall does not work with varargs */
  775. /*template <typename T, typename R, typename... Args>
  776. struct fx_traits<R (__stdcall T::*)(Args..., ...) const volatile& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  777. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const volatile& noexcept;
  778. };*/
  779. template <typename T, typename R, typename... Args>
  780. struct fx_traits<R (__stdcall T::*)(Args...) && noexcept, false> : basic_traits<true, false, T, R, Args...> {
  781. typedef R (__stdcall T::*function_pointer_type)(Args...) && noexcept;
  782. };
  783. /* __stdcall does not work with varargs */
  784. /*template <typename T, typename R, typename... Args>
  785. struct fx_traits<R (__stdcall T::*)(Args..., ...) && noexcept, false> : basic_traits<true, true, T, R, Args...> {
  786. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) && noexcept;
  787. };*/
  788. template <typename T, typename R, typename... Args>
  789. struct fx_traits<R (__stdcall T::*)(Args...) const&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  790. typedef R (__stdcall T::*function_pointer_type)(Args...) const&& noexcept;
  791. };
  792. /* __stdcall does not work with varargs */
  793. /*template <typename T, typename R, typename... Args>
  794. struct fx_traits<R (__stdcall T::*)(Args..., ...) const&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  795. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const&& noexcept;
  796. };*/
  797. template <typename T, typename R, typename... Args>
  798. struct fx_traits<R (__stdcall T::*)(Args...) const volatile&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  799. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&& noexcept;
  800. };
  801. /* __stdcall does not work with varargs */
  802. /*template <typename T, typename R, typename... Args>
  803. struct fx_traits<R (__stdcall T::*)(Args..., ...) const volatile&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  804. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const volatile&& noexcept;
  805. };*/
  806. #endif // noexcept is part of a function's type
  807. #endif // __stdcall x86 VC++ bug
  808. template <typename Signature>
  809. struct fx_traits<Signature, true> : fx_traits<typename fx_traits<decltype(&Signature::operator())>::function_type, false> {};
  810. template <typename Signature, bool b = std::is_member_object_pointer<Signature>::value>
  811. struct callable_traits : fx_traits<std::decay_t<Signature>> {
  812. };
  813. template <typename R, typename T>
  814. struct callable_traits<R(T::*), true> {
  815. typedef std::conditional_t<std::is_array<R>::value, std::add_lvalue_reference_t<T>, R> return_type;
  816. typedef return_type Arg;
  817. typedef T object_type;
  818. using signature_type = R(T::*);
  819. static const bool is_noexcept = false;
  820. static const bool is_member_function = false;
  821. static const std::size_t arity = 1;
  822. static const std::size_t free_arity = 2;
  823. typedef std::tuple<Arg> args_tuple;
  824. typedef types<Arg> args_list;
  825. typedef types<T, Arg> free_args_list;
  826. typedef meta::tuple_types<return_type> returns_list;
  827. typedef return_type(function_type)(T&, return_type);
  828. typedef return_type(*function_pointer_type)(T&, Arg);
  829. typedef return_type(*free_function_pointer_type)(T&, Arg);
  830. template <std::size_t i>
  831. using arg_at = void_tuple_element_t<i, args_tuple>;
  832. };
  833. } // namespace meta_detail
  834. template <typename Signature>
  835. struct bind_traits : meta_detail::callable_traits<Signature> {};
  836. template <typename Signature>
  837. using function_args_t = typename bind_traits<Signature>::args_list;
  838. template <typename Signature>
  839. using function_signature_t = typename bind_traits<Signature>::signature_type;
  840. template <typename Signature>
  841. using function_return_t = typename bind_traits<Signature>::return_type;
  842. }
  843. } // namespace sol::meta
  844. // end of sol/bind_traits.hpp
  845. // beginning of sol/string_view.hpp
  846. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  847. #include <string_view>
  848. #endif // C++17 features
  849. #include <functional>
  850. #if defined(SOL_USE_BOOST) && SOL_USE_BOOST
  851. #include <boost/functional/hash.hpp>
  852. #endif
  853. namespace sol {
  854. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  855. template <typename C, typename T = std::char_traits<C>>
  856. using basic_string_view = std::basic_string_view<C, T>;
  857. typedef std::string_view string_view;
  858. typedef std::wstring_view wstring_view;
  859. typedef std::u16string_view u16string_view;
  860. typedef std::u32string_view u32string_view;
  861. typedef std::hash<std::string_view> string_view_hash;
  862. #else
  863. template <typename Char, typename Traits = std::char_traits<Char>>
  864. struct basic_string_view {
  865. std::size_t s;
  866. const Char* p;
  867. basic_string_view(const std::string& r)
  868. : basic_string_view(r.data(), r.size()) {
  869. }
  870. basic_string_view(const Char* ptr)
  871. : basic_string_view(ptr, Traits::length(ptr)) {
  872. }
  873. basic_string_view(const Char* ptr, std::size_t sz)
  874. : s(sz), p(ptr) {
  875. }
  876. static int compare(const Char* lhs_p, std::size_t lhs_sz, const Char* rhs_p, std::size_t rhs_sz) {
  877. int result = Traits::compare(lhs_p, rhs_p, lhs_sz < rhs_sz ? lhs_sz : rhs_sz);
  878. if (result != 0)
  879. return result;
  880. if (lhs_sz < rhs_sz)
  881. return -1;
  882. if (lhs_sz > rhs_sz)
  883. return 1;
  884. return 0;
  885. }
  886. const Char* begin() const {
  887. return p;
  888. }
  889. const Char* end() const {
  890. return p + s;
  891. }
  892. const Char* cbegin() const {
  893. return p;
  894. }
  895. const Char* cend() const {
  896. return p + s;
  897. }
  898. const Char* data() const {
  899. return p;
  900. }
  901. std::size_t size() const {
  902. return s;
  903. }
  904. std::size_t length() const {
  905. return size();
  906. }
  907. operator std::basic_string<Char, Traits>() const {
  908. return std::basic_string<Char, Traits>(data(), size());
  909. }
  910. bool operator==(const basic_string_view& r) const {
  911. return compare(p, s, r.data(), r.size()) == 0;
  912. }
  913. bool operator==(const Char* r) const {
  914. return compare(r, Traits::length(r), p, s) == 0;
  915. }
  916. bool operator==(const std::basic_string<Char, Traits>& r) const {
  917. return compare(r.data(), r.size(), p, s) == 0;
  918. }
  919. bool operator!=(const basic_string_view& r) const {
  920. return !(*this == r);
  921. }
  922. bool operator!=(const char* r) const {
  923. return !(*this == r);
  924. }
  925. bool operator!=(const std::basic_string<Char, Traits>& r) const {
  926. return !(*this == r);
  927. }
  928. };
  929. template <typename Ch, typename Tr = std::char_traits<Ch>>
  930. struct basic_string_view_hash {
  931. typedef basic_string_view<Ch, Tr> argument_type;
  932. typedef std::size_t result_type;
  933. template <typename Al>
  934. result_type operator()(const std::basic_string<Ch, Tr, Al>& r) const {
  935. return (*this)(argument_type(r.c_str(), r.size()));
  936. }
  937. result_type operator()(const argument_type& r) const {
  938. #if defined(SOL_USE_BOOST) && SOL_USE_BOOST
  939. return boost::hash_range(r.begin(), r.end());
  940. #else
  941. // Modified, from libstdc++
  942. // An implementation attempt at Fowler No Voll, 1a.
  943. // Supposedly, used in MSVC,
  944. // GCC (libstdc++) uses MurmurHash of some sort for 64-bit though...?
  945. // But, well. Can't win them all, right?
  946. // This should normally only apply when NOT using boost,
  947. // so this should almost never be tapped into...
  948. std::size_t hash = 0;
  949. const unsigned char* cptr = reinterpret_cast<const unsigned char*>(r.data());
  950. for (std::size_t sz = r.size(); sz != 0; --sz) {
  951. hash ^= static_cast<size_t>(*cptr++);
  952. hash *= static_cast<size_t>(1099511628211ULL);
  953. }
  954. return hash;
  955. #endif
  956. }
  957. };
  958. } // namespace sol
  959. namespace std {
  960. template <typename Ch, typename Tr>
  961. struct hash< ::sol::basic_string_view<Ch, Tr> > : ::sol::basic_string_view_hash<Ch, Tr> {};
  962. } // namespace std
  963. namespace sol {
  964. using string_view = basic_string_view<char>;
  965. using wstring_view = basic_string_view<wchar_t>;
  966. using u16string_view = basic_string_view<char16_t>;
  967. using u32string_view = basic_string_view<char32_t>;
  968. using string_view_hash = std::hash<string_view>;
  969. #endif // C++17 Support
  970. } // namespace sol
  971. // end of sol/string_view.hpp
  972. #include <type_traits>
  973. #include <cstdint>
  974. #include <memory>
  975. #include <array>
  976. #include <iterator>
  977. #include <iosfwd>
  978. namespace sol {
  979. template <std::size_t I>
  980. using index_value = std::integral_constant<std::size_t, I>;
  981. namespace meta {
  982. typedef std::array<char, 1> sfinae_yes_t;
  983. typedef std::array<char, 2> sfinae_no_t;
  984. template <typename T>
  985. struct identity { typedef T type; };
  986. template <typename T>
  987. using identity_t = typename identity<T>::type;
  988. template <typename... Args>
  989. struct is_tuple : std::false_type {};
  990. template <typename... Args>
  991. struct is_tuple<std::tuple<Args...>> : std::true_type {};
  992. template <typename T>
  993. struct is_builtin_type : std::integral_constant<bool, std::is_arithmetic<T>::value || std::is_pointer<T>::value || std::is_array<T>::value> {};
  994. template <typename T>
  995. struct unwrapped {
  996. typedef T type;
  997. };
  998. template <typename T>
  999. struct unwrapped<std::reference_wrapper<T>> {
  1000. typedef T type;
  1001. };
  1002. template <typename T>
  1003. using unwrapped_t = typename unwrapped<T>::type;
  1004. template <typename T>
  1005. struct unwrap_unqualified : unwrapped<unqualified_t<T>> {};
  1006. template <typename T>
  1007. using unwrap_unqualified_t = typename unwrap_unqualified<T>::type;
  1008. template <typename T>
  1009. struct remove_member_pointer;
  1010. template <typename R, typename T>
  1011. struct remove_member_pointer<R T::*> {
  1012. typedef R type;
  1013. };
  1014. template <typename R, typename T>
  1015. struct remove_member_pointer<R T::*const> {
  1016. typedef R type;
  1017. };
  1018. template <typename T>
  1019. using remove_member_pointer_t = remove_member_pointer<T>;
  1020. namespace meta_detail {
  1021. template <typename T, template <typename...> class Templ>
  1022. struct is_specialization_of : std::false_type {};
  1023. template <typename... T, template <typename...> class Templ>
  1024. struct is_specialization_of<Templ<T...>, Templ> : std::true_type {};
  1025. }
  1026. template <typename T, template <typename...> class Templ>
  1027. using is_specialization_of = meta_detail::is_specialization_of<std::remove_cv_t<T>, Templ>;
  1028. template <class T, class...>
  1029. struct all_same : std::true_type {};
  1030. template <class T, class U, class... Args>
  1031. struct all_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T, U>::value && all_same<T, Args...>::value> {};
  1032. template <class T, class...>
  1033. struct any_same : std::false_type {};
  1034. template <class T, class U, class... Args>
  1035. struct any_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T, U>::value || any_same<T, Args...>::value> {};
  1036. template <bool B>
  1037. using boolean = std::integral_constant<bool, B>;
  1038. template <typename T>
  1039. using invoke_t = typename T::type;
  1040. template <typename T>
  1041. using invoke_b = boolean<T::value>;
  1042. template <typename T>
  1043. using neg = boolean<!T::value>;
  1044. template <typename Condition, typename Then, typename Else>
  1045. using condition = std::conditional_t<Condition::value, Then, Else>;
  1046. template <typename... Args>
  1047. struct all : boolean<true> {};
  1048. template <typename T, typename... Args>
  1049. struct all<T, Args...> : condition<T, all<Args...>, boolean<false>> {};
  1050. template <typename... Args>
  1051. struct any : boolean<false> {};
  1052. template <typename T, typename... Args>
  1053. struct any<T, Args...> : condition<T, boolean<true>, any<Args...>> {};
  1054. enum class enable_t {
  1055. _
  1056. };
  1057. constexpr const auto enabler = enable_t::_;
  1058. template <bool value, typename T = void>
  1059. using disable_if_t = std::enable_if_t<!value, T>;
  1060. template <typename... Args>
  1061. using enable = std::enable_if_t<all<Args...>::value, enable_t>;
  1062. template <typename... Args>
  1063. using disable = std::enable_if_t<neg<all<Args...>>::value, enable_t>;
  1064. template <typename... Args>
  1065. using enable_any = std::enable_if_t<any<Args...>::value, enable_t>;
  1066. template <typename... Args>
  1067. using disable_any = std::enable_if_t<neg<any<Args...>>::value, enable_t>;
  1068. template <typename V, typename... Vs>
  1069. struct find_in_pack_v : boolean<false> {};
  1070. template <typename V, typename Vs1, typename... Vs>
  1071. struct find_in_pack_v<V, Vs1, Vs...> : any<boolean<(V::value == Vs1::value)>, find_in_pack_v<V, Vs...>> {};
  1072. namespace meta_detail {
  1073. template <std::size_t I, typename T, typename... Args>
  1074. struct index_in_pack : std::integral_constant<std::size_t, SIZE_MAX> {};
  1075. template <std::size_t I, typename T, typename T1, typename... Args>
  1076. struct index_in_pack<I, T, T1, Args...> : std::conditional_t<std::is_same<T, T1>::value, std::integral_constant<std::ptrdiff_t, I>, index_in_pack<I + 1, T, Args...>> {};
  1077. } // namespace meta_detail
  1078. template <typename T, typename... Args>
  1079. struct index_in_pack : meta_detail::index_in_pack<0, T, Args...> {};
  1080. template <typename T, typename List>
  1081. struct index_in : meta_detail::index_in_pack<0, T, List> {};
  1082. template <typename T, typename... Args>
  1083. struct index_in<T, types<Args...>> : meta_detail::index_in_pack<0, T, Args...> {};
  1084. template <std::size_t I, typename... Args>
  1085. struct at_in_pack {};
  1086. template <std::size_t I, typename... Args>
  1087. using at_in_pack_t = typename at_in_pack<I, Args...>::type;
  1088. template <std::size_t I, typename Arg, typename... Args>
  1089. struct at_in_pack<I, Arg, Args...> : std::conditional<I == 0, Arg, at_in_pack_t<I - 1, Args...>> {};
  1090. template <typename Arg, typename... Args>
  1091. struct at_in_pack<0, Arg, Args...> { typedef Arg type; };
  1092. namespace meta_detail {
  1093. template <std::size_t Limit, std::size_t I, template <typename...> class Pred, typename... Ts>
  1094. struct count_for_pack : std::integral_constant<std::size_t, 0> {};
  1095. template <std::size_t Limit, std::size_t I, template <typename...> class Pred, typename T, typename... Ts>
  1096. struct count_for_pack<Limit, I, Pred, T, Ts...> : std::conditional_t < sizeof...(Ts)
  1097. == 0
  1098. || Limit<2,
  1099. std::integral_constant<std::size_t, I + static_cast<std::size_t>(Limit != 0 && Pred<T>::value)>,
  1100. count_for_pack<Limit - 1, I + static_cast<std::size_t>(Pred<T>::value), Pred, Ts...>> {};
  1101. template <std::size_t I, template <typename...> class Pred, typename... Ts>
  1102. struct count_2_for_pack : std::integral_constant<std::size_t, 0> {};
  1103. template <std::size_t I, template <typename...> class Pred, typename T, typename U, typename... Ts>
  1104. struct count_2_for_pack<I, Pred, T, U, Ts...> : std::conditional_t<sizeof...(Ts) == 0,
  1105. std::integral_constant<std::size_t, I + static_cast<std::size_t>(Pred<T>::value)>,
  1106. count_2_for_pack<I + static_cast<std::size_t>(Pred<T>::value), Pred, Ts...>> {};
  1107. } // namespace meta_detail
  1108. template <template <typename...> class Pred, typename... Ts>
  1109. struct count_for_pack : meta_detail::count_for_pack<sizeof...(Ts), 0, Pred, Ts...> {};
  1110. template <template <typename...> class Pred, typename List>
  1111. struct count_for;
  1112. template <template <typename...> class Pred, typename... Args>
  1113. struct count_for<Pred, types<Args...>> : count_for_pack<Pred, Args...> {};
  1114. template <std::size_t Limit, template <typename...> class Pred, typename... Ts>
  1115. struct count_for_to_pack : meta_detail::count_for_pack<Limit, 0, Pred, Ts...> {};
  1116. template <template <typename...> class Pred, typename... Ts>
  1117. struct count_2_for_pack : meta_detail::count_2_for_pack<0, Pred, Ts...> {};
  1118. template <typename... Args>
  1119. struct return_type {
  1120. typedef std::tuple<Args...> type;
  1121. };
  1122. template <typename T>
  1123. struct return_type<T> {
  1124. typedef T type;
  1125. };
  1126. template <>
  1127. struct return_type<> {
  1128. typedef void type;
  1129. };
  1130. template <typename... Args>
  1131. using return_type_t = typename return_type<Args...>::type;
  1132. namespace meta_detail {
  1133. template <typename>
  1134. struct always_true : std::true_type {};
  1135. struct is_invokable_tester {
  1136. template <typename Fun, typename... Args>
  1137. static always_true<decltype(std::declval<Fun>()(std::declval<Args>()...))> test(int);
  1138. template <typename...>
  1139. static std::false_type test(...);
  1140. };
  1141. } // namespace meta_detail
  1142. template <typename T>
  1143. struct is_invokable;
  1144. template <typename Fun, typename... Args>
  1145. struct is_invokable<Fun(Args...)> : decltype(meta_detail::is_invokable_tester::test<Fun, Args...>(0)) {};
  1146. namespace meta_detail {
  1147. template <typename T, typename = void>
  1148. struct is_callable : std::is_function<std::remove_pointer_t<T>> {};
  1149. template <typename T>
  1150. struct is_callable<T, std::enable_if_t<std::is_final<unqualified_t<T>>::value
  1151. && std::is_class<unqualified_t<T>>::value
  1152. && std::is_same<decltype(void(&T::operator())), void>::value>> {
  1153. };
  1154. template <typename T>
  1155. struct is_callable<T, std::enable_if_t<!std::is_final<unqualified_t<T>>::value && std::is_class<unqualified_t<T>>::value && std::is_destructible<unqualified_t<T>>::value>> {
  1156. using yes = char;
  1157. using no = struct { char s[2]; };
  1158. struct F {
  1159. void operator()();
  1160. };
  1161. struct Derived : T, F {};
  1162. template <typename U, U>
  1163. struct Check;
  1164. template <typename V>
  1165. static no test(Check<void (F::*)(), &V::operator()>*);
  1166. template <typename>
  1167. static yes test(...);
  1168. static const bool value = sizeof(test<Derived>(0)) == sizeof(yes);
  1169. };
  1170. template <typename T>
  1171. struct is_callable<T, std::enable_if_t<!std::is_final<unqualified_t<T>>::value && std::is_class<unqualified_t<T>>::value && !std::is_destructible<unqualified_t<T>>::value>> {
  1172. using yes = char;
  1173. using no = struct { char s[2]; };
  1174. struct F {
  1175. void operator()();
  1176. };
  1177. struct Derived : T, F {
  1178. ~Derived() = delete;
  1179. };
  1180. template <typename U, U>
  1181. struct Check;
  1182. template <typename V>
  1183. static no test(Check<void (F::*)(), &V::operator()>*);
  1184. template <typename>
  1185. static yes test(...);
  1186. static const bool value = sizeof(test<Derived>(0)) == sizeof(yes);
  1187. };
  1188. struct has_begin_end_impl {
  1189. template <typename T, typename U = unqualified_t<T>,
  1190. typename B = decltype(std::declval<U&>().begin()),
  1191. typename E = decltype(std::declval<U&>().end())>
  1192. static std::true_type test(int);
  1193. template <typename...>
  1194. static std::false_type test(...);
  1195. };
  1196. struct has_key_type_impl {
  1197. template <typename T, typename U = unqualified_t<T>,
  1198. typename V = typename U::key_type>
  1199. static std::true_type test(int);
  1200. template <typename...>
  1201. static std::false_type test(...);
  1202. };
  1203. struct has_mapped_type_impl {
  1204. template <typename T, typename U = unqualified_t<T>,
  1205. typename V = typename U::mapped_type>
  1206. static std::true_type test(int);
  1207. template <typename...>
  1208. static std::false_type test(...);
  1209. };
  1210. struct has_value_type_impl {
  1211. template <typename T, typename U = unqualified_t<T>,
  1212. typename V = typename U::value_type>
  1213. static std::true_type test(int);
  1214. template <typename...>
  1215. static std::false_type test(...);
  1216. };
  1217. struct has_iterator_impl {
  1218. template <typename T, typename U = unqualified_t<T>,
  1219. typename V = typename U::iterator>
  1220. static std::true_type test(int);
  1221. template <typename...>
  1222. static std::false_type test(...);
  1223. };
  1224. struct has_key_value_pair_impl {
  1225. template <typename T, typename U = unqualified_t<T>,
  1226. typename V = typename U::value_type,
  1227. typename F = decltype(std::declval<V&>().first),
  1228. typename S = decltype(std::declval<V&>().second)>
  1229. static std::true_type test(int);
  1230. template <typename...>
  1231. static std::false_type test(...);
  1232. };
  1233. template <typename T>
  1234. struct has_push_back_test {
  1235. private:
  1236. template <typename C>
  1237. static sfinae_yes_t test(decltype(std::declval<C>().push_back(std::declval<std::add_rvalue_reference_t<typename C::value_type>>())) *);
  1238. template <typename C>
  1239. static sfinae_no_t test(...);
  1240. public:
  1241. static const bool value = sizeof(test<T>(0)) == sizeof(sfinae_yes_t);
  1242. };
  1243. template <typename T>
  1244. struct has_insert_test {
  1245. private:
  1246. template <typename C>
  1247. static sfinae_yes_t test(decltype(std::declval<C>().insert(std::declval<std::add_rvalue_reference_t<typename C::const_iterator>>(), std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  1248. template <typename C>
  1249. static sfinae_no_t test(...);
  1250. public:
  1251. static const bool value = sizeof(test<T>(0)) == sizeof(sfinae_yes_t);
  1252. };
  1253. template <typename T>
  1254. struct has_insert_after_test {
  1255. private:
  1256. template <typename C>
  1257. static sfinae_yes_t test(decltype(std::declval<C>().insert_after(std::declval<std::add_rvalue_reference_t<typename C::const_iterator>>(), std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  1258. template <typename C>
  1259. static sfinae_no_t test(...);
  1260. public:
  1261. static const bool value = sizeof(test<T>(0)) == sizeof(sfinae_yes_t);
  1262. };
  1263. template <typename T>
  1264. struct has_size_test {
  1265. private:
  1266. typedef std::array<char, 1> sfinae_yes_t;
  1267. typedef std::array<char, 2> sfinae_no_t;
  1268. template <typename C>
  1269. static sfinae_yes_t test(decltype(std::declval<C>().size())*);
  1270. template <typename C>
  1271. static sfinae_no_t test(...);
  1272. public:
  1273. static const bool value = sizeof(test<T>(0)) == sizeof(sfinae_yes_t);
  1274. };
  1275. template <typename T>
  1276. struct has_max_size_test {
  1277. private:
  1278. typedef std::array<char, 1> sfinae_yes_t;
  1279. typedef std::array<char, 2> sfinae_no_t;
  1280. template <typename C>
  1281. static sfinae_yes_t test(decltype(std::declval<C>().max_size())*);
  1282. template <typename C>
  1283. static sfinae_no_t test(...);
  1284. public:
  1285. static const bool value = sizeof(test<T>(0)) == sizeof(sfinae_yes_t);
  1286. };
  1287. template <typename T>
  1288. struct has_to_string_test {
  1289. private:
  1290. typedef std::array<char, 1> sfinae_yes_t;
  1291. typedef std::array<char, 2> sfinae_no_t;
  1292. template <typename C>
  1293. static sfinae_yes_t test(decltype(std::declval<C>().to_string())*);
  1294. template <typename C>
  1295. static sfinae_no_t test(...);
  1296. public:
  1297. static const bool value = sizeof(test<T>(0)) == sizeof(sfinae_yes_t);
  1298. };
  1299. #if defined(_MSC_VER) && _MSC_VER <= 1910
  1300. template <typename T, typename U, typename = decltype(std::declval<T&>() < std::declval<U&>())>
  1301. std::true_type supports_op_less_test(std::reference_wrapper<T>, std::reference_wrapper<U>);
  1302. std::false_type supports_op_less_test(...);
  1303. template <typename T, typename U, typename = decltype(std::declval<T&>() == std::declval<U&>())>
  1304. std::true_type supports_op_equal_test(std::reference_wrapper<T>, std::reference_wrapper<U>);
  1305. std::false_type supports_op_equal_test(...);
  1306. template <typename T, typename U, typename = decltype(std::declval<T&>() <= std::declval<U&>())>
  1307. std::true_type supports_op_less_equal_test(std::reference_wrapper<T>, std::reference_wrapper<U>);
  1308. std::false_type supports_op_less_equal_test(...);
  1309. template <typename T, typename OS, typename = decltype(std::declval<OS&>() << std::declval<T&>())>
  1310. std::true_type supports_ostream_op(std::reference_wrapper<T>, std::reference_wrapper<OS>);
  1311. std::false_type supports_ostream_op(...);
  1312. template <typename T, typename = decltype(to_string(std::declval<T&>()))>
  1313. std::true_type supports_adl_to_string(std::reference_wrapper<T>);
  1314. std::false_type supports_adl_to_string(...);
  1315. #else
  1316. template <typename T, typename U, typename = decltype(std::declval<T&>() < std::declval<U&>())>
  1317. std::true_type supports_op_less_test(const T&, const U&);
  1318. std::false_type supports_op_less_test(...);
  1319. template <typename T, typename U, typename = decltype(std::declval<T&>() == std::declval<U&>())>
  1320. std::true_type supports_op_equal_test(const T&, const U&);
  1321. std::false_type supports_op_equal_test(...);
  1322. template <typename T, typename U, typename = decltype(std::declval<T&>() <= std::declval<U&>())>
  1323. std::true_type supports_op_less_equal_test(const T&, const U&);
  1324. std::false_type supports_op_less_equal_test(...);
  1325. template <typename T, typename OS, typename = decltype(std::declval<OS&>() << std::declval<T&>())>
  1326. std::true_type supports_ostream_op(const T&, const OS&);
  1327. std::false_type supports_ostream_op(...);
  1328. template <typename T, typename = decltype(to_string(std::declval<T&>()))>
  1329. std::true_type supports_adl_to_string(const T&);
  1330. std::false_type supports_adl_to_string(...);
  1331. #endif
  1332. template <typename T, bool b>
  1333. struct is_matched_lookup_impl : std::false_type {};
  1334. template <typename T>
  1335. struct is_matched_lookup_impl<T, true> : std::is_same<typename T::key_type, typename T::value_type> {};
  1336. } // namespace meta_detail
  1337. #if defined(_MSC_VER) && _MSC_VER <= 1910
  1338. template <typename T, typename U = T>
  1339. using supports_op_less = decltype(meta_detail::supports_op_less_test(std::ref(std::declval<T&>()), std::ref(std::declval<U&>())));
  1340. template <typename T, typename U = T>
  1341. using supports_op_equal = decltype(meta_detail::supports_op_equal_test(std::ref(std::declval<T&>()), std::ref(std::declval<U&>())));
  1342. template <typename T, typename U = T>
  1343. using supports_op_less_equal = decltype(meta_detail::supports_op_less_equal_test(std::ref(std::declval<T&>()), std::ref(std::declval<U&>())));
  1344. template <typename T, typename U = std::ostream>
  1345. using supports_ostream_op = decltype(meta_detail::supports_ostream_op(std::ref(std::declval<T&>()), std::ref(std::declval<U&>())));
  1346. template <typename T>
  1347. using supports_adl_to_string = decltype(meta_detail::supports_adl_to_string(std::ref(std::declval<T&>())));
  1348. #else
  1349. template <typename T, typename U = T>
  1350. using supports_op_less = decltype(meta_detail::supports_op_less_test(std::declval<T&>(), std::declval<U&>()));
  1351. template <typename T, typename U = T>
  1352. using supports_op_equal = decltype(meta_detail::supports_op_equal_test(std::declval<T&>(), std::declval<U&>()));
  1353. template <typename T, typename U = T>
  1354. using supports_op_less_equal = decltype(meta_detail::supports_op_less_equal_test(std::declval<T&>(), std::declval<U&>()));
  1355. template <typename T, typename U = std::ostream>
  1356. using supports_ostream_op = decltype(meta_detail::supports_ostream_op(std::declval<T&>(), std::declval<U&>()));
  1357. template <typename T>
  1358. using supports_adl_to_string = decltype(meta_detail::supports_adl_to_string(std::declval<T&>()));
  1359. #endif
  1360. template <typename T>
  1361. using supports_to_string_member = meta::boolean<meta_detail::has_to_string_test<T>::value>;
  1362. template <typename T>
  1363. struct is_callable : boolean<meta_detail::is_callable<T>::value> {};
  1364. template <typename T>
  1365. struct has_begin_end : decltype(meta_detail::has_begin_end_impl::test<T>(0)) {};
  1366. template <typename T>
  1367. struct has_key_value_pair : decltype(meta_detail::has_key_value_pair_impl::test<T>(0)) {};
  1368. template <typename T>
  1369. struct has_key_type : decltype(meta_detail::has_key_type_impl::test<T>(0)) {};
  1370. template <typename T>
  1371. struct has_mapped_type : decltype(meta_detail::has_mapped_type_impl::test<T>(0)) {};
  1372. template <typename T>
  1373. struct has_iterator : decltype(meta_detail::has_iterator_impl::test<T>(0)) {};
  1374. template <typename T>
  1375. struct has_value_type : decltype(meta_detail::has_value_type_impl::test<T>(0)) {};
  1376. template <typename T>
  1377. using has_push_back = meta::boolean<meta_detail::has_push_back_test<T>::value>;
  1378. template <typename T>
  1379. using has_max_size = meta::boolean<meta_detail::has_max_size_test<T>::value>;
  1380. template <typename T>
  1381. using has_insert = meta::boolean<meta_detail::has_insert_test<T>::value>;
  1382. template <typename T>
  1383. using has_insert_after = meta::boolean<meta_detail::has_insert_after_test<T>::value>;
  1384. template <typename T>
  1385. using has_size = meta::boolean<meta_detail::has_size_test<T>::value || meta_detail::has_size_test<const T>::value>;
  1386. template <typename T>
  1387. struct is_associative : meta::all<has_key_type<T>, has_key_value_pair<T>, has_mapped_type<T>> {};
  1388. template <typename T>
  1389. struct is_lookup : meta::all<has_key_type<T>, has_value_type<T>> {};
  1390. template <typename T>
  1391. struct is_matched_lookup : meta_detail::is_matched_lookup_impl<T, is_lookup<T>::value> {};
  1392. template <typename T>
  1393. using is_string_like = any<
  1394. is_specialization_of<meta::unqualified_t<T>, std::basic_string>,
  1395. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  1396. is_specialization_of<meta::unqualified_t<T>, std::basic_string_view>,
  1397. #else
  1398. is_specialization_of<meta::unqualified_t<T>, basic_string_view>,
  1399. #endif
  1400. meta::all<std::is_array<unqualified_t<T>>, meta::any_same<meta::unqualified_t<std::remove_all_extents_t<meta::unqualified_t<T>>>, char, char16_t, char32_t, wchar_t>>
  1401. >;
  1402. template <typename T>
  1403. using is_string_constructible = any<
  1404. meta::all<std::is_array<unqualified_t<T>>, std::is_same<meta::unqualified_t<std::remove_all_extents_t<meta::unqualified_t<T>>>, char>>,
  1405. std::is_same<unqualified_t<T>, const char*>,
  1406. std::is_same<unqualified_t<T>, char>, std::is_same<unqualified_t<T>, std::string>, std::is_same<unqualified_t<T>, std::initializer_list<char>>
  1407. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  1408. , std::is_same<unqualified_t<T>, std::string_view>
  1409. #endif
  1410. >;
  1411. template <typename T>
  1412. struct is_pair : std::false_type {};
  1413. template <typename T1, typename T2>
  1414. struct is_pair<std::pair<T1, T2>> : std::true_type {};
  1415. template <typename T>
  1416. using is_c_str = any<
  1417. std::is_same<std::decay_t<unqualified_t<T>>, const char*>,
  1418. std::is_same<std::decay_t<unqualified_t<T>>, char*>,
  1419. std::is_same<unqualified_t<T>, std::string>>;
  1420. template <typename T>
  1421. struct is_move_only : all<
  1422. neg<std::is_reference<T>>,
  1423. neg<std::is_copy_constructible<unqualified_t<T>>>,
  1424. std::is_move_constructible<unqualified_t<T>>> {};
  1425. template <typename T>
  1426. using is_not_move_only = neg<is_move_only<T>>;
  1427. namespace meta_detail {
  1428. template <typename T, meta::disable<meta::is_specialization_of<meta::unqualified_t<T>, std::tuple>> = meta::enabler>
  1429. decltype(auto) force_tuple(T&& x) {
  1430. return std::tuple<std::decay_t<T>>(std::forward<T>(x));
  1431. }
  1432. template <typename T, meta::enable<meta::is_specialization_of<meta::unqualified_t<T>, std::tuple>> = meta::enabler>
  1433. decltype(auto) force_tuple(T&& x) {
  1434. return std::forward<T>(x);
  1435. }
  1436. } // namespace meta_detail
  1437. template <typename... X>
  1438. decltype(auto) tuplefy(X&&... x) {
  1439. return std::tuple_cat(meta_detail::force_tuple(std::forward<X>(x))...);
  1440. }
  1441. template <typename T, typename = void>
  1442. struct iterator_tag {
  1443. using type = std::input_iterator_tag;
  1444. };
  1445. template <typename T>
  1446. struct iterator_tag<T, std::conditional_t<false, typename T::iterator_category, void>> {
  1447. using type = typename T::iterator_category;
  1448. };
  1449. } // namespace meta
  1450. namespace detail {
  1451. template <typename T>
  1452. struct is_pointer_like : std::is_pointer<T> {};
  1453. template <typename T, typename D>
  1454. struct is_pointer_like<std::unique_ptr<T, D>> : std::true_type {};
  1455. template <typename T>
  1456. struct is_pointer_like<std::shared_ptr<T>> : std::true_type {};
  1457. template <std::size_t I, typename Tuple>
  1458. decltype(auto) forward_get(Tuple&& tuple) {
  1459. return std::forward<meta::tuple_element_t<I, Tuple>>(std::get<I>(tuple));
  1460. }
  1461. template <std::size_t... I, typename Tuple>
  1462. auto forward_tuple_impl(std::index_sequence<I...>, Tuple&& tuple) -> decltype(std::tuple<decltype(forward_get<I>(tuple))...>(forward_get<I>(tuple)...)) {
  1463. return std::tuple<decltype(forward_get<I>(tuple))...>(std::move(std::get<I>(tuple))...);
  1464. }
  1465. template <typename Tuple>
  1466. auto forward_tuple(Tuple&& tuple) {
  1467. auto x = forward_tuple_impl(std::make_index_sequence<std::tuple_size<meta::unqualified_t<Tuple>>::value>(), std::forward<Tuple>(tuple));
  1468. return x;
  1469. }
  1470. template <typename T>
  1471. auto unwrap(T&& item) -> decltype(std::forward<T>(item)) {
  1472. return std::forward<T>(item);
  1473. }
  1474. template <typename T>
  1475. T& unwrap(std::reference_wrapper<T> arg) {
  1476. return arg.get();
  1477. }
  1478. template <typename T, meta::enable<meta::neg<is_pointer_like<meta::unqualified_t<T>>>> = meta::enabler>
  1479. auto deref(T&& item) -> decltype(std::forward<T>(item)) {
  1480. return std::forward<T>(item);
  1481. }
  1482. template <typename T, meta::enable<is_pointer_like<meta::unqualified_t<T>>> = meta::enabler>
  1483. inline auto deref(T&& item) -> decltype(*std::forward<T>(item)) {
  1484. return *std::forward<T>(item);
  1485. }
  1486. template <typename T, meta::disable<is_pointer_like<meta::unqualified_t<T>>, meta::neg<std::is_pointer<meta::unqualified_t<T>>>> = meta::enabler>
  1487. auto deref_non_pointer(T&& item) -> decltype(std::forward<T>(item)) {
  1488. return std::forward<T>(item);
  1489. }
  1490. template <typename T, meta::enable<is_pointer_like<meta::unqualified_t<T>>, meta::neg<std::is_pointer<meta::unqualified_t<T>>>> = meta::enabler>
  1491. inline auto deref_non_pointer(T&& item) -> decltype(*std::forward<T>(item)) {
  1492. return *std::forward<T>(item);
  1493. }
  1494. template <typename T>
  1495. inline T* ptr(T& val) {
  1496. return std::addressof(val);
  1497. }
  1498. template <typename T>
  1499. inline T* ptr(std::reference_wrapper<T> val) {
  1500. return std::addressof(val.get());
  1501. }
  1502. template <typename T>
  1503. inline T* ptr(T* val) {
  1504. return val;
  1505. }
  1506. } // namespace detail
  1507. } // namespace sol
  1508. // end of sol/traits.hpp
  1509. // beginning of sol/function.hpp
  1510. // beginning of sol/stack.hpp
  1511. // beginning of sol/trampoline.hpp
  1512. // beginning of sol/types.hpp
  1513. // beginning of sol/optional.hpp
  1514. // beginning of sol/compatibility.hpp
  1515. // beginning of sol/compatibility/version.hpp
  1516. #if defined(SOL_USING_CXX_LUA) && SOL_USING_CXX_LUA
  1517. #include <lua.h>
  1518. #include <lualib.h>
  1519. #include <lauxlib.h>
  1520. #if defined(SOL_USING_CXX_LUAJIT) && SOL_USING_CXX_LUAJIT
  1521. #include <luajit.h>
  1522. #endif // C++ LuaJIT ... whatever that means
  1523. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !(SOL_EXCEPTIONS_SAFE_PROPAGATION)) && (!defined(SOL_EXCEPTIONS_ALWAYS_UNSAFE) || !(SOL_EXCEPTIONS_ALWAYS_UNSAFE))
  1524. #define SOL_EXCEPTIONS_SAFE_PROPAGATION 1
  1525. #endif // Exceptions can be propagated safely using C++-compiled Lua
  1526. #else
  1527. #include <lua.hpp>
  1528. #endif // C++ Mangling for Lua
  1529. #ifdef LUAJIT_VERSION
  1530. #ifndef SOL_LUAJIT
  1531. #define SOL_LUAJIT 1
  1532. #ifndef SOL_LUAJIT_VERSION
  1533. #define SOL_LUAJIT_VERSION LUAJIT_VERSION_NUM
  1534. #endif // SOL_LUAJIT_VERSION definition, if not present
  1535. #endif // sol luajit
  1536. #endif // luajit
  1537. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
  1538. #define SOL_LUA_VERSION LUA_VERSION_NUM
  1539. #elif defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  1540. #define SOL_LUA_VERSION LUA_VERSION_NUM
  1541. #elif !defined(LUA_VERSION_NUM) || !(LUA_VERSION_NUM)
  1542. #define SOL_LUA_VERSION 500
  1543. #else
  1544. #define SOL_LUA_VERSION 502
  1545. #endif // Lua Version 502, 501 || luajit, 500
  1546. // end of sol/compatibility/version.hpp
  1547. #if !defined(SOL_NO_COMPAT) || !(SOL_NO_COMPAT)
  1548. #if defined(SOL_USING_CXX_LUA) && SOL_USING_CXX_LUA
  1549. #ifndef COMPAT53_LUA_CPP
  1550. #define COMPAT53_LUA_CPP 1
  1551. #endif // Build Lua Compat layer as C++
  1552. #endif
  1553. #ifndef COMPAT53_INCLUDE_SOURCE
  1554. #define COMPAT53_INCLUDE_SOURCE 1
  1555. #endif // Build Compat Layer Inline
  1556. // beginning of sol/compatibility/compat-5.3.h
  1557. #ifndef KEPLER_PROJECT_COMPAT53_H_
  1558. #define KEPLER_PROJECT_COMPAT53_H_
  1559. #include <stddef.h>
  1560. #include <limits.h>
  1561. #include <string.h>
  1562. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  1563. extern "C" {
  1564. #endif
  1565. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  1566. }
  1567. #endif
  1568. #ifndef COMPAT53_PREFIX
  1569. /* we chose this name because many other lua bindings / libs have
  1570. * their own compatibility layer, and that use the compat53 declaration
  1571. * frequently, causing all kinds of linker / compiler issues
  1572. */
  1573. # define COMPAT53_PREFIX kp_compat53
  1574. #endif // COMPAT53_PREFIX
  1575. #ifndef COMPAT53_API
  1576. # if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE
  1577. # if defined(__GNUC__) || defined(__clang__)
  1578. # define COMPAT53_API __attribute__((__unused__)) static
  1579. # else
  1580. # define COMPAT53_API static
  1581. # endif /* Clang/GCC */
  1582. # else /* COMPAT53_INCLUDE_SOURCE */
  1583. /* we are not including source, so everything is extern */
  1584. # define COMPAT53_API extern
  1585. # endif /* COMPAT53_INCLUDE_SOURCE */
  1586. #endif /* COMPAT53_PREFIX */
  1587. #define COMPAT53_CONCAT_HELPER(a, b) a##b
  1588. #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
  1589. /* declarations for Lua 5.1 */
  1590. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  1591. /* XXX not implemented:
  1592. * lua_arith (new operators)
  1593. * lua_upvalueid
  1594. * lua_upvaluejoin
  1595. * lua_version
  1596. * lua_yieldk
  1597. */
  1598. #ifndef LUA_OK
  1599. # define LUA_OK 0
  1600. #endif
  1601. #ifndef LUA_OPADD
  1602. # define LUA_OPADD 0
  1603. #endif
  1604. #ifndef LUA_OPSUB
  1605. # define LUA_OPSUB 1
  1606. #endif
  1607. #ifndef LUA_OPMUL
  1608. # define LUA_OPMUL 2
  1609. #endif
  1610. #ifndef LUA_OPDIV
  1611. # define LUA_OPDIV 3
  1612. #endif
  1613. #ifndef LUA_OPMOD
  1614. # define LUA_OPMOD 4
  1615. #endif
  1616. #ifndef LUA_OPPOW
  1617. # define LUA_OPPOW 5
  1618. #endif
  1619. #ifndef LUA_OPUNM
  1620. # define LUA_OPUNM 6
  1621. #endif
  1622. #ifndef LUA_OPEQ
  1623. # define LUA_OPEQ 0
  1624. #endif
  1625. #ifndef LUA_OPLT
  1626. # define LUA_OPLT 1
  1627. #endif
  1628. #ifndef LUA_OPLE
  1629. # define LUA_OPLE 2
  1630. #endif
  1631. /* LuaJIT/Lua 5.1 does not have the updated
  1632. * error codes for thread status/function returns (but some patched versions do)
  1633. * define it only if it's not found
  1634. */
  1635. #if !defined(LUA_ERRGCMM)
  1636. /* Use + 2 because in some versions of Lua (Lua 5.1)
  1637. * LUA_ERRFILE is defined as (LUA_ERRERR+1)
  1638. * so we need to avoid it (LuaJIT might have something at this
  1639. * integer value too)
  1640. */
  1641. # define LUA_ERRGCMM (LUA_ERRERR + 2)
  1642. #endif /* LUA_ERRGCMM define */
  1643. typedef size_t lua_Unsigned;
  1644. typedef struct luaL_Buffer_53 {
  1645. luaL_Buffer b; /* make incorrect code crash! */
  1646. char *ptr;
  1647. size_t nelems;
  1648. size_t capacity;
  1649. lua_State *L2;
  1650. } luaL_Buffer_53;
  1651. #define luaL_Buffer luaL_Buffer_53
  1652. /* In PUC-Rio 5.1, userdata is a simple FILE*
  1653. * In LuaJIT, it's a struct where the first member is a FILE*
  1654. * We can't support the `closef` member
  1655. */
  1656. typedef struct luaL_Stream {
  1657. FILE *f;
  1658. } luaL_Stream;
  1659. #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
  1660. COMPAT53_API int lua_absindex(lua_State *L, int i);
  1661. #define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
  1662. COMPAT53_API void lua_arith(lua_State *L, int op);
  1663. #define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
  1664. COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op);
  1665. #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
  1666. COMPAT53_API void lua_copy(lua_State *L, int from, int to);
  1667. #define lua_getuservalue(L, i) \
  1668. (lua_getfenv((L), (i)), lua_type((L), -1))
  1669. #define lua_setuservalue(L, i) \
  1670. (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
  1671. #define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
  1672. COMPAT53_API void lua_len(lua_State *L, int i);
  1673. #define lua_pushstring(L, s) \
  1674. (lua_pushstring((L), (s)), lua_tostring((L), -1))
  1675. #define lua_pushlstring(L, s, len) \
  1676. ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
  1677. #ifndef luaL_newlibtable
  1678. # define luaL_newlibtable(L, l) \
  1679. (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
  1680. #endif
  1681. #ifndef luaL_newlib
  1682. # define luaL_newlib(L, l) \
  1683. (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
  1684. #endif
  1685. #define lua_pushglobaltable(L) \
  1686. lua_pushvalue((L), LUA_GLOBALSINDEX)
  1687. #define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
  1688. COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p);
  1689. #define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
  1690. COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
  1691. #define lua_rawlen(L, i) lua_objlen((L), (i))
  1692. #define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
  1693. #define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
  1694. COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum);
  1695. #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
  1696. COMPAT53_API void luaL_checkversion(lua_State *L);
  1697. #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
  1698. COMPAT53_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode);
  1699. #define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
  1700. COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode);
  1701. #define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx)
  1702. COMPAT53_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
  1703. #define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
  1704. COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg);
  1705. #define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
  1706. COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char *name);
  1707. #define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
  1708. COMPAT53_API lua_Integer luaL_len(lua_State *L, int i);
  1709. #define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
  1710. COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
  1711. #define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
  1712. COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname);
  1713. #define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
  1714. COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname);
  1715. #define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
  1716. COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level);
  1717. #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
  1718. COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname);
  1719. #define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
  1720. COMPAT53_API int luaL_execresult(lua_State *L, int stat);
  1721. #define lua_callk(L, na, nr, ctx, cont) \
  1722. ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
  1723. #define lua_pcallk(L, na, nr, err, ctx, cont) \
  1724. ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
  1725. #define lua_resume(L, from, nargs) \
  1726. ((void)(from), lua_resume((L), (nargs)))
  1727. #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
  1728. COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B);
  1729. #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
  1730. COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s);
  1731. #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
  1732. COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l);
  1733. #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
  1734. COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B);
  1735. #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
  1736. COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B);
  1737. #undef luaL_buffinitsize
  1738. #define luaL_buffinitsize(L, B, s) \
  1739. (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
  1740. #undef luaL_prepbuffer
  1741. #define luaL_prepbuffer(B) \
  1742. luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
  1743. #undef luaL_addchar
  1744. #define luaL_addchar(B, c) \
  1745. ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
  1746. ((B)->ptr[(B)->nelems++] = (c)))
  1747. #undef luaL_addsize
  1748. #define luaL_addsize(B, s) \
  1749. ((B)->nelems += (s))
  1750. #undef luaL_addstring
  1751. #define luaL_addstring(B, s) \
  1752. luaL_addlstring((B), (s), strlen((s)))
  1753. #undef luaL_pushresultsize
  1754. #define luaL_pushresultsize(B, s) \
  1755. (luaL_addsize((B), (s)), luaL_pushresult((B)))
  1756. #if defined(LUA_COMPAT_APIINTCASTS)
  1757. #define lua_pushunsigned(L, n) \
  1758. lua_pushinteger((L), (lua_Integer)(n))
  1759. #define lua_tounsignedx(L, i, is) \
  1760. ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
  1761. #define lua_tounsigned(L, i) \
  1762. lua_tounsignedx((L), (i), NULL)
  1763. #define luaL_checkunsigned(L, a) \
  1764. ((lua_Unsigned)luaL_checkinteger((L), (a)))
  1765. #define luaL_optunsigned(L, a, d) \
  1766. ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
  1767. #endif
  1768. #endif /* Lua 5.1 only */
  1769. /* declarations for Lua 5.1 and 5.2 */
  1770. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  1771. typedef int lua_KContext;
  1772. typedef int(*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
  1773. #define lua_dump(L, w, d, s) \
  1774. ((void)(s), lua_dump((L), (w), (d)))
  1775. #define lua_getfield(L, i, k) \
  1776. (lua_getfield((L), (i), (k)), lua_type((L), -1))
  1777. #define lua_gettable(L, i) \
  1778. (lua_gettable((L), (i)), lua_type((L), -1))
  1779. #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
  1780. COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i);
  1781. #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
  1782. COMPAT53_API int lua_isinteger(lua_State *L, int index);
  1783. #define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
  1784. COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum);
  1785. #define lua_numbertointeger(n, p) \
  1786. ((*(p) = (lua_Integer)(n)), 1)
  1787. #define lua_rawget(L, i) \
  1788. (lua_rawget((L), (i)), lua_type((L), -1))
  1789. #define lua_rawgeti(L, i, n) \
  1790. (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
  1791. #define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
  1792. COMPAT53_API void lua_rotate(lua_State *L, int idx, int n);
  1793. #define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
  1794. COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i);
  1795. #define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
  1796. COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s);
  1797. #define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
  1798. COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len);
  1799. #define luaL_getmetafield(L, o, e) \
  1800. (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
  1801. #define luaL_newmetatable(L, tn) \
  1802. (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
  1803. #define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
  1804. COMPAT53_API void luaL_requiref(lua_State *L, const char *modname,
  1805. lua_CFunction openf, int glb);
  1806. #endif /* Lua 5.1 and Lua 5.2 */
  1807. /* declarations for Lua 5.2 */
  1808. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
  1809. /* XXX not implemented:
  1810. * lua_isyieldable
  1811. * lua_getextraspace
  1812. * lua_arith (new operators)
  1813. * lua_pushfstring (new formats)
  1814. */
  1815. #define lua_getglobal(L, n) \
  1816. (lua_getglobal((L), (n)), lua_type((L), -1))
  1817. #define lua_getuservalue(L, i) \
  1818. (lua_getuservalue((L), (i)), lua_type((L), -1))
  1819. #define lua_pushlstring(L, s, len) \
  1820. (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
  1821. #define lua_rawgetp(L, i, p) \
  1822. (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
  1823. #define LUA_KFUNCTION(_name) \
  1824. static int (_name)(lua_State *L, int status, lua_KContext ctx); \
  1825. static int (_name ## _52)(lua_State *L) { \
  1826. lua_KContext ctx; \
  1827. int status = lua_getctx(L, &ctx); \
  1828. return (_name)(L, status, ctx); \
  1829. } \
  1830. static int (_name)(lua_State *L, int status, lua_KContext ctx)
  1831. #define lua_pcallk(L, na, nr, err, ctx, cont) \
  1832. lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
  1833. #define lua_callk(L, na, nr, ctx, cont) \
  1834. lua_callk((L), (na), (nr), (ctx), cont ## _52)
  1835. #define lua_yieldk(L, nr, ctx, cont) \
  1836. lua_yieldk((L), (nr), (ctx), cont ## _52)
  1837. #ifdef lua_call
  1838. # undef lua_call
  1839. # define lua_call(L, na, nr) \
  1840. (lua_callk)((L), (na), (nr), 0, NULL)
  1841. #endif
  1842. #ifdef lua_pcall
  1843. # undef lua_pcall
  1844. # define lua_pcall(L, na, nr, err) \
  1845. (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
  1846. #endif
  1847. #ifdef lua_yield
  1848. # undef lua_yield
  1849. # define lua_yield(L, nr) \
  1850. (lua_yieldk)((L), (nr), 0, NULL)
  1851. #endif
  1852. #endif /* Lua 5.2 only */
  1853. /* other Lua versions */
  1854. #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
  1855. # error "unsupported Lua version (i.e. not Lua 5.1, 5.2, or 5.3)"
  1856. #endif /* other Lua versions except 5.1, 5.2, and 5.3 */
  1857. /* helper macro for defining continuation functions (for every version
  1858. * *except* Lua 5.2) */
  1859. #ifndef LUA_KFUNCTION
  1860. #define LUA_KFUNCTION(_name) \
  1861. static int (_name)(lua_State *L, int status, lua_KContext ctx)
  1862. #endif
  1863. #if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE == 1
  1864. // beginning of sol/compatibility/compat-5.3.c
  1865. #include <stdlib.h>
  1866. #include <ctype.h>
  1867. #include <errno.h>
  1868. #include <stdio.h>
  1869. /* don't compile it again if it already is included via compat53.h */
  1870. #ifndef KEPLER_PROJECT_COMPAT53_C_
  1871. #define KEPLER_PROJECT_COMPAT53_C_
  1872. /* definitions for Lua 5.1 only */
  1873. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  1874. #ifndef COMPAT53_FOPEN_NO_LOCK
  1875. # if defined(_MSC_VER)
  1876. # define COMPAT53_FOPEN_NO_LOCK 1
  1877. # else /* otherwise */
  1878. # define COMPAT53_FOPEN_NO_LOCK 0
  1879. # endif /* VC++ only so far */
  1880. #endif /* No-lock fopen_s usage if possible */
  1881. #if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
  1882. # include <share.h>
  1883. #endif /* VC++ _fsopen for share-allowed file read */
  1884. #ifndef COMPAT53_HAVE_STRERROR_R
  1885. # if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || \
  1886. (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6))
  1887. # define COMPAT53_HAVE_STRERROR_R 1
  1888. # else /* none of the defines matched: define to 0 */
  1889. # define COMPAT53_HAVE_STRERROR_R 0
  1890. # endif /* have strerror_r of some form */
  1891. #endif /* strerror_r */
  1892. #ifndef COMPAT53_HAVE_STRERROR_S
  1893. # if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
  1894. (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__)
  1895. # define COMPAT53_HAVE_STRERROR_S 1
  1896. # else /* not VC++ or C11 */
  1897. # define COMPAT53_HAVE_STRERROR_S 0
  1898. # endif /* strerror_s from VC++ or C11 */
  1899. #endif /* strerror_s */
  1900. #ifndef COMPAT53_LUA_FILE_BUFFER_SIZE
  1901. # define COMPAT53_LUA_FILE_BUFFER_SIZE 4096
  1902. #endif /* Lua File Buffer Size */
  1903. static char* compat53_strerror(int en, char* buff, size_t sz) {
  1904. #if COMPAT53_HAVE_STRERROR_R
  1905. /* use strerror_r here, because it's available on these specific platforms */
  1906. if (sz > 0) {
  1907. buff[0] = '\0';
  1908. /* we don't care whether the GNU version or the XSI version is used: */
  1909. if (strerror_r(en, buff, sz)) {
  1910. /* Yes, we really DO want to ignore the return value!
  1911. * GCC makes that extra hard, not even a (void) cast will do. */
  1912. }
  1913. if (buff[0] == '\0') {
  1914. /* Buffer is unchanged, so we probably have called GNU strerror_r which
  1915. * returned a static constant string. Chances are that strerror will
  1916. * return the same static constant string and therefore be thread-safe. */
  1917. return strerror(en);
  1918. }
  1919. }
  1920. return buff; /* sz is 0 *or* strerror_r wrote into the buffer */
  1921. #elif COMPAT53_HAVE_STRERROR_S
  1922. /* for MSVC and other C11 implementations, use strerror_s since it's
  1923. * provided by default by the libraries */
  1924. strerror_s(buff, sz, en);
  1925. return buff;
  1926. #else
  1927. /* fallback, but strerror is not guaranteed to be threadsafe due to modifying
  1928. * errno itself and some impls not locking a static buffer for it ... but most
  1929. * known systems have threadsafe errno: this might only change if the locale
  1930. * is changed out from under someone while this function is being called */
  1931. (void)buff;
  1932. (void)sz;
  1933. return strerror(en);
  1934. #endif
  1935. }
  1936. COMPAT53_API int lua_absindex(lua_State *L, int i) {
  1937. if (i < 0 && i > LUA_REGISTRYINDEX)
  1938. i += lua_gettop(L) + 1;
  1939. return i;
  1940. }
  1941. static void compat53_call_lua(lua_State *L, char const code[], size_t len,
  1942. int nargs, int nret) {
  1943. lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
  1944. if (lua_type(L, -1) != LUA_TFUNCTION) {
  1945. lua_pop(L, 1);
  1946. if (luaL_loadbuffer(L, code, len, "=none"))
  1947. lua_error(L);
  1948. lua_pushvalue(L, -1);
  1949. lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
  1950. }
  1951. lua_insert(L, -nargs - 1);
  1952. lua_call(L, nargs, nret);
  1953. }
  1954. static const char compat53_arith_code[] =
  1955. "local op,a,b=...\n"
  1956. "if op==0 then return a+b\n"
  1957. "elseif op==1 then return a-b\n"
  1958. "elseif op==2 then return a*b\n"
  1959. "elseif op==3 then return a/b\n"
  1960. "elseif op==4 then return a%b\n"
  1961. "elseif op==5 then return a^b\n"
  1962. "elseif op==6 then return -a\n"
  1963. "end\n";
  1964. COMPAT53_API void lua_arith(lua_State *L, int op) {
  1965. if (op < LUA_OPADD || op > LUA_OPUNM)
  1966. luaL_error(L, "invalid 'op' argument for lua_arith");
  1967. luaL_checkstack(L, 5, "not enough stack slots");
  1968. if (op == LUA_OPUNM)
  1969. lua_pushvalue(L, -1);
  1970. lua_pushnumber(L, op);
  1971. lua_insert(L, -3);
  1972. compat53_call_lua(L, compat53_arith_code,
  1973. sizeof(compat53_arith_code) - 1, 3, 1);
  1974. }
  1975. static const char compat53_compare_code[] =
  1976. "local a,b=...\n"
  1977. "return a<=b\n";
  1978. COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op) {
  1979. int result = 0;
  1980. switch (op) {
  1981. case LUA_OPEQ:
  1982. return lua_equal(L, idx1, idx2);
  1983. case LUA_OPLT:
  1984. return lua_lessthan(L, idx1, idx2);
  1985. case LUA_OPLE:
  1986. luaL_checkstack(L, 5, "not enough stack slots");
  1987. idx1 = lua_absindex(L, idx1);
  1988. idx2 = lua_absindex(L, idx2);
  1989. lua_pushvalue(L, idx1);
  1990. lua_pushvalue(L, idx2);
  1991. compat53_call_lua(L, compat53_compare_code,
  1992. sizeof(compat53_compare_code) - 1, 2, 1);
  1993. result = lua_toboolean(L, -1);
  1994. lua_pop(L, 1);
  1995. return result;
  1996. default:
  1997. luaL_error(L, "invalid 'op' argument for lua_compare");
  1998. }
  1999. return 0;
  2000. }
  2001. COMPAT53_API void lua_copy(lua_State *L, int from, int to) {
  2002. int abs_to = lua_absindex(L, to);
  2003. luaL_checkstack(L, 1, "not enough stack slots");
  2004. lua_pushvalue(L, from);
  2005. lua_replace(L, abs_to);
  2006. }
  2007. COMPAT53_API void lua_len(lua_State *L, int i) {
  2008. switch (lua_type(L, i)) {
  2009. case LUA_TSTRING:
  2010. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  2011. break;
  2012. case LUA_TTABLE:
  2013. if (!luaL_callmeta(L, i, "__len"))
  2014. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  2015. break;
  2016. case LUA_TUSERDATA:
  2017. if (luaL_callmeta(L, i, "__len"))
  2018. break;
  2019. /* FALLTHROUGH */
  2020. default:
  2021. luaL_error(L, "attempt to get length of a %s value",
  2022. lua_typename(L, lua_type(L, i)));
  2023. }
  2024. }
  2025. COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p) {
  2026. int abs_i = lua_absindex(L, i);
  2027. lua_pushlightuserdata(L, (void*)p);
  2028. lua_rawget(L, abs_i);
  2029. return lua_type(L, -1);
  2030. }
  2031. COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p) {
  2032. int abs_i = lua_absindex(L, i);
  2033. luaL_checkstack(L, 1, "not enough stack slots");
  2034. lua_pushlightuserdata(L, (void*)p);
  2035. lua_insert(L, -2);
  2036. lua_rawset(L, abs_i);
  2037. }
  2038. COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum) {
  2039. lua_Number n = lua_tonumber(L, i);
  2040. if (isnum != NULL) {
  2041. *isnum = (n != 0 || lua_isnumber(L, i));
  2042. }
  2043. return n;
  2044. }
  2045. COMPAT53_API void luaL_checkversion(lua_State *L) {
  2046. (void)L;
  2047. }
  2048. COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg) {
  2049. if (!lua_checkstack(L, sp + LUA_MINSTACK)) {
  2050. if (msg != NULL)
  2051. luaL_error(L, "stack overflow (%s)", msg);
  2052. else {
  2053. lua_pushliteral(L, "stack overflow");
  2054. lua_error(L);
  2055. }
  2056. }
  2057. }
  2058. COMPAT53_API int luaL_getsubtable(lua_State *L, int i, const char *name) {
  2059. int abs_i = lua_absindex(L, i);
  2060. luaL_checkstack(L, 3, "not enough stack slots");
  2061. lua_pushstring(L, name);
  2062. lua_gettable(L, abs_i);
  2063. if (lua_istable(L, -1))
  2064. return 1;
  2065. lua_pop(L, 1);
  2066. lua_newtable(L);
  2067. lua_pushstring(L, name);
  2068. lua_pushvalue(L, -2);
  2069. lua_settable(L, abs_i);
  2070. return 0;
  2071. }
  2072. COMPAT53_API lua_Integer luaL_len(lua_State *L, int i) {
  2073. lua_Integer res = 0;
  2074. int isnum = 0;
  2075. luaL_checkstack(L, 1, "not enough stack slots");
  2076. lua_len(L, i);
  2077. res = lua_tointegerx(L, -1, &isnum);
  2078. lua_pop(L, 1);
  2079. if (!isnum)
  2080. luaL_error(L, "object length is not an integer");
  2081. return res;
  2082. }
  2083. COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
  2084. luaL_checkstack(L, nup + 1, "too many upvalues");
  2085. for (; l->name != NULL; l++) { /* fill the table with given functions */
  2086. int i;
  2087. lua_pushstring(L, l->name);
  2088. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  2089. lua_pushvalue(L, -(nup + 1));
  2090. lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
  2091. lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
  2092. }
  2093. lua_pop(L, nup); /* remove upvalues */
  2094. }
  2095. COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname) {
  2096. luaL_checkstack(L, 1, "not enough stack slots");
  2097. luaL_getmetatable(L, tname);
  2098. lua_setmetatable(L, -2);
  2099. }
  2100. COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname) {
  2101. void *p = lua_touserdata(L, i);
  2102. luaL_checkstack(L, 2, "not enough stack slots");
  2103. if (p == NULL || !lua_getmetatable(L, i))
  2104. return NULL;
  2105. else {
  2106. int res = 0;
  2107. luaL_getmetatable(L, tname);
  2108. res = lua_rawequal(L, -1, -2);
  2109. lua_pop(L, 2);
  2110. if (!res)
  2111. p = NULL;
  2112. }
  2113. return p;
  2114. }
  2115. static int compat53_countlevels(lua_State *L) {
  2116. lua_Debug ar;
  2117. int li = 1, le = 1;
  2118. /* find an upper bound */
  2119. while (lua_getstack(L, le, &ar)) { li = le; le *= 2; }
  2120. /* do a binary search */
  2121. while (li < le) {
  2122. int m = (li + le) / 2;
  2123. if (lua_getstack(L, m, &ar)) li = m + 1;
  2124. else le = m;
  2125. }
  2126. return le - 1;
  2127. }
  2128. static int compat53_findfield(lua_State *L, int objidx, int level) {
  2129. if (level == 0 || !lua_istable(L, -1))
  2130. return 0; /* not found */
  2131. lua_pushnil(L); /* start 'next' loop */
  2132. while (lua_next(L, -2)) { /* for each pair in table */
  2133. if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
  2134. if (lua_rawequal(L, objidx, -1)) { /* found object? */
  2135. lua_pop(L, 1); /* remove value (but keep name) */
  2136. return 1;
  2137. }
  2138. else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
  2139. lua_remove(L, -2); /* remove table (but keep name) */
  2140. lua_pushliteral(L, ".");
  2141. lua_insert(L, -2); /* place '.' between the two names */
  2142. lua_concat(L, 3);
  2143. return 1;
  2144. }
  2145. }
  2146. lua_pop(L, 1); /* remove value */
  2147. }
  2148. return 0; /* not found */
  2149. }
  2150. static int compat53_pushglobalfuncname(lua_State *L, lua_Debug *ar) {
  2151. int top = lua_gettop(L);
  2152. lua_getinfo(L, "f", ar); /* push function */
  2153. lua_pushvalue(L, LUA_GLOBALSINDEX);
  2154. if (compat53_findfield(L, top + 1, 2)) {
  2155. lua_copy(L, -1, top + 1); /* move name to proper place */
  2156. lua_pop(L, 2); /* remove pushed values */
  2157. return 1;
  2158. }
  2159. else {
  2160. lua_settop(L, top); /* remove function and global table */
  2161. return 0;
  2162. }
  2163. }
  2164. static void compat53_pushfuncname(lua_State *L, lua_Debug *ar) {
  2165. if (*ar->namewhat != '\0') /* is there a name? */
  2166. lua_pushfstring(L, "function " LUA_QS, ar->name);
  2167. else if (*ar->what == 'm') /* main? */
  2168. lua_pushliteral(L, "main chunk");
  2169. else if (*ar->what == 'C') {
  2170. if (compat53_pushglobalfuncname(L, ar)) {
  2171. lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
  2172. lua_remove(L, -2); /* remove name */
  2173. }
  2174. else
  2175. lua_pushliteral(L, "?");
  2176. }
  2177. else
  2178. lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
  2179. }
  2180. #define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
  2181. #define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
  2182. COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1,
  2183. const char *msg, int level) {
  2184. lua_Debug ar;
  2185. int top = lua_gettop(L);
  2186. int numlevels = compat53_countlevels(L1);
  2187. int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
  2188. if (msg) lua_pushfstring(L, "%s\n", msg);
  2189. lua_pushliteral(L, "stack traceback:");
  2190. while (lua_getstack(L1, level++, &ar)) {
  2191. if (level == mark) { /* too many levels? */
  2192. lua_pushliteral(L, "\n\t..."); /* add a '...' */
  2193. level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
  2194. }
  2195. else {
  2196. lua_getinfo(L1, "Slnt", &ar);
  2197. lua_pushfstring(L, "\n\t%s:", ar.short_src);
  2198. if (ar.currentline > 0)
  2199. lua_pushfstring(L, "%d:", ar.currentline);
  2200. lua_pushliteral(L, " in ");
  2201. compat53_pushfuncname(L, &ar);
  2202. lua_concat(L, lua_gettop(L) - top);
  2203. }
  2204. }
  2205. lua_concat(L, lua_gettop(L) - top);
  2206. }
  2207. COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname) {
  2208. const char *serr = NULL;
  2209. int en = errno; /* calls to Lua API may change this value */
  2210. char buf[512] = { 0 };
  2211. if (stat) {
  2212. lua_pushboolean(L, 1);
  2213. return 1;
  2214. }
  2215. else {
  2216. lua_pushnil(L);
  2217. serr = compat53_strerror(en, buf, sizeof(buf));
  2218. if (fname)
  2219. lua_pushfstring(L, "%s: %s", fname, serr);
  2220. else
  2221. lua_pushstring(L, serr);
  2222. lua_pushnumber(L, (lua_Number)en);
  2223. return 3;
  2224. }
  2225. }
  2226. static int compat53_checkmode(lua_State *L, const char *mode, const char *modename, int err) {
  2227. if (mode && strchr(mode, modename[0]) == NULL) {
  2228. lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode);
  2229. return err;
  2230. }
  2231. return LUA_OK;
  2232. }
  2233. typedef struct {
  2234. lua_Reader reader;
  2235. void *ud;
  2236. int has_peeked_data;
  2237. const char *peeked_data;
  2238. size_t peeked_data_size;
  2239. } compat53_reader_data;
  2240. static const char *compat53_reader(lua_State *L, void *ud, size_t *size) {
  2241. compat53_reader_data *data = (compat53_reader_data *)ud;
  2242. if (data->has_peeked_data) {
  2243. data->has_peeked_data = 0;
  2244. *size = data->peeked_data_size;
  2245. return data->peeked_data;
  2246. }
  2247. else
  2248. return data->reader(L, data->ud, size);
  2249. }
  2250. COMPAT53_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) {
  2251. int status = LUA_OK;
  2252. compat53_reader_data compat53_data = { reader, data, 1, 0, 0 };
  2253. compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size));
  2254. if (compat53_data.peeked_data && compat53_data.peeked_data_size &&
  2255. compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */
  2256. status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
  2257. else
  2258. status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
  2259. if (status != LUA_OK)
  2260. return status;
  2261. /* we need to call the original 5.1 version of lua_load! */
  2262. #undef lua_load
  2263. return lua_load(L, compat53_reader, &compat53_data, source);
  2264. #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
  2265. }
  2266. typedef struct {
  2267. int n; /* number of pre-read characters */
  2268. FILE *f; /* file being read */
  2269. char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
  2270. } compat53_LoadF;
  2271. static const char *compat53_getF(lua_State *L, void *ud, size_t *size) {
  2272. compat53_LoadF *lf = (compat53_LoadF *)ud;
  2273. (void)L; /* not used */
  2274. if (lf->n > 0) { /* are there pre-read characters to be read? */
  2275. *size = lf->n; /* return them (chars already in buffer) */
  2276. lf->n = 0; /* no more pre-read characters */
  2277. }
  2278. else { /* read a block from file */
  2279. /* 'fread' can return > 0 *and* set the EOF flag. If next call to
  2280. 'compat53_getF' called 'fread', it might still wait for user input.
  2281. The next check avoids this problem. */
  2282. if (feof(lf->f)) return NULL;
  2283. *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
  2284. }
  2285. return lf->buff;
  2286. }
  2287. static int compat53_errfile(lua_State *L, const char *what, int fnameindex) {
  2288. char buf[512] = { 0 };
  2289. const char *serr = compat53_strerror(errno, buf, sizeof(buf));
  2290. const char *filename = lua_tostring(L, fnameindex) + 1;
  2291. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  2292. lua_remove(L, fnameindex);
  2293. return LUA_ERRFILE;
  2294. }
  2295. static int compat53_skipBOM(compat53_LoadF *lf) {
  2296. const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
  2297. int c;
  2298. lf->n = 0;
  2299. do {
  2300. c = getc(lf->f);
  2301. if (c == EOF || c != *(const unsigned char *)p++) return c;
  2302. lf->buff[lf->n++] = (char)c; /* to be read by the parser */
  2303. } while (*p != '\0');
  2304. lf->n = 0; /* prefix matched; discard it */
  2305. return getc(lf->f); /* return next character */
  2306. }
  2307. /*
  2308. ** reads the first character of file 'f' and skips an optional BOM mark
  2309. ** in its beginning plus its first line if it starts with '#'. Returns
  2310. ** true if it skipped the first line. In any case, '*cp' has the
  2311. ** first "valid" character of the file (after the optional BOM and
  2312. ** a first-line comment).
  2313. */
  2314. static int compat53_skipcomment(compat53_LoadF *lf, int *cp) {
  2315. int c = *cp = compat53_skipBOM(lf);
  2316. if (c == '#') { /* first line is a comment (Unix exec. file)? */
  2317. do { /* skip first line */
  2318. c = getc(lf->f);
  2319. } while (c != EOF && c != '\n');
  2320. *cp = getc(lf->f); /* skip end-of-line, if present */
  2321. return 1; /* there was a comment */
  2322. }
  2323. else return 0; /* no comment */
  2324. }
  2325. COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode) {
  2326. compat53_LoadF lf;
  2327. int status, readstatus;
  2328. int c;
  2329. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  2330. if (filename == NULL) {
  2331. lua_pushliteral(L, "=stdin");
  2332. lf.f = stdin;
  2333. }
  2334. else {
  2335. lua_pushfstring(L, "@%s", filename);
  2336. #if defined(_MSC_VER)
  2337. /* This code is here to stop a deprecation error that stops builds
  2338. * if a certain macro is defined. While normally not caring would
  2339. * be best, some header-only libraries and builds can't afford to
  2340. * dictate this to the user. A quick check shows that fopen_s this
  2341. * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
  2342. * possibly even before that so we don't need to do any version
  2343. * number checks, since this has been there since forever. */
  2344. /* TO USER: if you want the behavior of typical fopen_s/fopen,
  2345. * which does lock the file on VC++, define the macro used below to 0 */
  2346. #if COMPAT53_FOPEN_NO_LOCK
  2347. lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
  2348. if (lf.f == NULL)
  2349. return compat53_errfile(L, "open", fnameindex);
  2350. #else /* use default locking version */
  2351. if (fopen_s(&lf.f, filename, "r") != 0)
  2352. return compat53_errfile(L, "open", fnameindex);
  2353. #endif /* Locking vs. No-locking fopen variants */
  2354. #else
  2355. lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
  2356. if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex);
  2357. #endif
  2358. }
  2359. if (compat53_skipcomment(&lf, &c)) /* read initial portion */
  2360. lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
  2361. if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
  2362. #if defined(_MSC_VER)
  2363. if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
  2364. return compat53_errfile(L, "reopen", fnameindex);
  2365. #else
  2366. lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
  2367. if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex);
  2368. #endif
  2369. compat53_skipcomment(&lf, &c); /* re-read initial portion */
  2370. }
  2371. if (c != EOF)
  2372. lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
  2373. status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
  2374. readstatus = ferror(lf.f);
  2375. if (filename) fclose(lf.f); /* close file (even in case of errors) */
  2376. if (readstatus) {
  2377. lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
  2378. return compat53_errfile(L, "read", fnameindex);
  2379. }
  2380. lua_remove(L, fnameindex);
  2381. return status;
  2382. }
  2383. COMPAT53_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) {
  2384. int status = LUA_OK;
  2385. if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) {
  2386. status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
  2387. }
  2388. else {
  2389. status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
  2390. }
  2391. if (status != LUA_OK)
  2392. return status;
  2393. return luaL_loadbuffer(L, buff, sz, name);
  2394. }
  2395. #if !defined(l_inspectstat) && \
  2396. (defined(unix) || defined(__unix) || defined(__unix__) || \
  2397. defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \
  2398. (defined(__APPLE__) && defined(__MACH__)))
  2399. /* some form of unix; check feature macros in unistd.h for details */
  2400. # include <unistd.h>
  2401. /* check posix version; the relevant include files and macros probably
  2402. * were available before 2001, but I'm not sure */
  2403. # if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
  2404. # include <sys/wait.h>
  2405. # define l_inspectstat(stat,what) \
  2406. if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
  2407. else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
  2408. # endif
  2409. #endif
  2410. /* provide default (no-op) version */
  2411. #if !defined(l_inspectstat)
  2412. # define l_inspectstat(stat,what) ((void)0)
  2413. #endif
  2414. COMPAT53_API int luaL_execresult(lua_State *L, int stat) {
  2415. const char *what = "exit";
  2416. if (stat == -1)
  2417. return luaL_fileresult(L, 0, NULL);
  2418. else {
  2419. l_inspectstat(stat, what);
  2420. if (*what == 'e' && stat == 0)
  2421. lua_pushboolean(L, 1);
  2422. else
  2423. lua_pushnil(L);
  2424. lua_pushstring(L, what);
  2425. lua_pushinteger(L, stat);
  2426. return 3;
  2427. }
  2428. }
  2429. COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B) {
  2430. /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
  2431. B->b.p = NULL;
  2432. B->b.L = NULL;
  2433. B->b.lvl = 0;
  2434. /* reuse the buffer from the 5.1-style luaL_Buffer though! */
  2435. B->ptr = B->b.buffer;
  2436. B->capacity = LUAL_BUFFERSIZE;
  2437. B->nelems = 0;
  2438. B->L2 = L;
  2439. }
  2440. COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s) {
  2441. if (B->capacity - B->nelems < s) { /* needs to grow */
  2442. char* newptr = NULL;
  2443. size_t newcap = B->capacity * 2;
  2444. if (newcap - B->nelems < s)
  2445. newcap = B->nelems + s;
  2446. if (newcap < B->capacity) /* overflow */
  2447. luaL_error(B->L2, "buffer too large");
  2448. newptr = (char*)lua_newuserdata(B->L2, newcap);
  2449. memcpy(newptr, B->ptr, B->nelems);
  2450. if (B->ptr != B->b.buffer)
  2451. lua_replace(B->L2, -2); /* remove old buffer */
  2452. B->ptr = newptr;
  2453. B->capacity = newcap;
  2454. }
  2455. return B->ptr + B->nelems;
  2456. }
  2457. COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l) {
  2458. memcpy(luaL_prepbuffsize(B, l), s, l);
  2459. luaL_addsize(B, l);
  2460. }
  2461. COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B) {
  2462. size_t len = 0;
  2463. const char *s = lua_tolstring(B->L2, -1, &len);
  2464. if (!s)
  2465. luaL_error(B->L2, "cannot convert value to string");
  2466. if (B->ptr != B->b.buffer)
  2467. lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
  2468. luaL_addlstring(B, s, len);
  2469. lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
  2470. }
  2471. void luaL_pushresult(luaL_Buffer_53 *B) {
  2472. lua_pushlstring(B->L2, B->ptr, B->nelems);
  2473. if (B->ptr != B->b.buffer)
  2474. lua_replace(B->L2, -2); /* remove userdata buffer */
  2475. }
  2476. #endif /* Lua 5.1 */
  2477. /* definitions for Lua 5.1 and Lua 5.2 */
  2478. #if defined( LUA_VERSION_NUM ) && LUA_VERSION_NUM <= 502
  2479. COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i) {
  2480. index = lua_absindex(L, index);
  2481. lua_pushinteger(L, i);
  2482. lua_gettable(L, index);
  2483. return lua_type(L, -1);
  2484. }
  2485. COMPAT53_API int lua_isinteger(lua_State *L, int index) {
  2486. if (lua_type(L, index) == LUA_TNUMBER) {
  2487. lua_Number n = lua_tonumber(L, index);
  2488. lua_Integer i = lua_tointeger(L, index);
  2489. if (i == n)
  2490. return 1;
  2491. }
  2492. return 0;
  2493. }
  2494. COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum) {
  2495. int ok = 0;
  2496. lua_Number n = lua_tonumberx(L, i, &ok);
  2497. if (ok) {
  2498. if (n == (lua_Integer)n) {
  2499. if (isnum)
  2500. *isnum = 1;
  2501. return (lua_Integer)n;
  2502. }
  2503. }
  2504. if (isnum)
  2505. *isnum = 0;
  2506. return 0;
  2507. }
  2508. static void compat53_reverse(lua_State *L, int a, int b) {
  2509. for (; a < b; ++a, --b) {
  2510. lua_pushvalue(L, a);
  2511. lua_pushvalue(L, b);
  2512. lua_replace(L, a);
  2513. lua_replace(L, b);
  2514. }
  2515. }
  2516. COMPAT53_API void lua_rotate(lua_State *L, int idx, int n) {
  2517. int n_elems = 0;
  2518. idx = lua_absindex(L, idx);
  2519. n_elems = lua_gettop(L) - idx + 1;
  2520. if (n < 0)
  2521. n += n_elems;
  2522. if (n > 0 && n < n_elems) {
  2523. luaL_checkstack(L, 2, "not enough stack slots available");
  2524. n = n_elems - n;
  2525. compat53_reverse(L, idx, idx + n - 1);
  2526. compat53_reverse(L, idx + n, idx + n_elems - 1);
  2527. compat53_reverse(L, idx, idx + n_elems - 1);
  2528. }
  2529. }
  2530. COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i) {
  2531. luaL_checkstack(L, 1, "not enough stack slots available");
  2532. index = lua_absindex(L, index);
  2533. lua_pushinteger(L, i);
  2534. lua_insert(L, -2);
  2535. lua_settable(L, index);
  2536. }
  2537. #if !defined(lua_str2number)
  2538. # define lua_str2number(s, p) strtod((s), (p))
  2539. #endif
  2540. COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s) {
  2541. char* endptr;
  2542. lua_Number n = lua_str2number(s, &endptr);
  2543. if (endptr != s) {
  2544. while (*endptr != '\0' && isspace((unsigned char)*endptr))
  2545. ++endptr;
  2546. if (*endptr == '\0') {
  2547. lua_pushnumber(L, n);
  2548. return endptr - s + 1;
  2549. }
  2550. }
  2551. return 0;
  2552. }
  2553. COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len) {
  2554. if (!luaL_callmeta(L, idx, "__tostring")) {
  2555. int t = lua_type(L, idx), tt = 0;
  2556. char const* name = NULL;
  2557. switch (t) {
  2558. case LUA_TNIL:
  2559. lua_pushliteral(L, "nil");
  2560. break;
  2561. case LUA_TSTRING:
  2562. case LUA_TNUMBER:
  2563. lua_pushvalue(L, idx);
  2564. break;
  2565. case LUA_TBOOLEAN:
  2566. if (lua_toboolean(L, idx))
  2567. lua_pushliteral(L, "true");
  2568. else
  2569. lua_pushliteral(L, "false");
  2570. break;
  2571. default:
  2572. tt = luaL_getmetafield(L, idx, "__name");
  2573. name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
  2574. lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
  2575. if (tt != LUA_TNIL)
  2576. lua_replace(L, -2);
  2577. break;
  2578. }
  2579. }
  2580. else {
  2581. if (!lua_isstring(L, -1))
  2582. luaL_error(L, "'__tostring' must return a string");
  2583. }
  2584. return lua_tolstring(L, -1, len);
  2585. }
  2586. COMPAT53_API void luaL_requiref(lua_State *L, const char *modname,
  2587. lua_CFunction openf, int glb) {
  2588. luaL_checkstack(L, 3, "not enough stack slots available");
  2589. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
  2590. if (lua_getfield(L, -1, modname) == LUA_TNIL) {
  2591. lua_pop(L, 1);
  2592. lua_pushcfunction(L, openf);
  2593. lua_pushstring(L, modname);
  2594. lua_call(L, 1, 1);
  2595. lua_pushvalue(L, -1);
  2596. lua_setfield(L, -3, modname);
  2597. }
  2598. if (glb) {
  2599. lua_pushvalue(L, -1);
  2600. lua_setglobal(L, modname);
  2601. }
  2602. lua_replace(L, -2);
  2603. }
  2604. #endif /* Lua 5.1 and 5.2 */
  2605. #endif /* KEPLER_PROJECT_COMPAT53_C_ */
  2606. /*********************************************************************
  2607. * This file contains parts of Lua 5.2's and Lua 5.3's source code:
  2608. *
  2609. * Copyright (C) 1994-2014 Lua.org, PUC-Rio.
  2610. *
  2611. * Permission is hereby granted, free of charge, to any person obtaining
  2612. * a copy of this software and associated documentation files (the
  2613. * "Software"), to deal in the Software without restriction, including
  2614. * without limitation the rights to use, copy, modify, merge, publish,
  2615. * distribute, sublicense, and/or sell copies of the Software, and to
  2616. * permit persons to whom the Software is furnished to do so, subject to
  2617. * the following conditions:
  2618. *
  2619. * The above copyright notice and this permission notice shall be
  2620. * included in all copies or substantial portions of the Software.
  2621. *
  2622. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  2623. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  2624. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  2625. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  2626. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  2627. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  2628. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  2629. *********************************************************************/
  2630. // end of sol/compatibility/compat-5.3.c
  2631. #endif
  2632. #endif /* KEPLER_PROJECT_COMPAT53_H_ */
  2633. // end of sol/compatibility/compat-5.3.h
  2634. #endif // SOL_NO_COMPAT
  2635. // end of sol/compatibility.hpp
  2636. // beginning of sol/in_place.hpp
  2637. #include <utility>
  2638. namespace sol {
  2639. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  2640. using in_place_t = std::in_place_t;
  2641. constexpr std::in_place_t in_place{};
  2642. constexpr std::in_place_t in_place_of{};
  2643. template <typename T>
  2644. using in_place_type_t = std::in_place_type_t<T>;
  2645. template <typename T>
  2646. constexpr std::in_place_type_t<T> in_place_type{};
  2647. template <size_t I>
  2648. using in_place_index_t = std::in_place_index_t<I>;
  2649. template <size_t I>
  2650. constexpr in_place_index_t<I> in_place_index{};
  2651. #else
  2652. namespace detail {
  2653. struct in_place_of_tag {};
  2654. template <std::size_t I>
  2655. struct in_place_of_i {};
  2656. template <typename T>
  2657. struct in_place_of_t {};
  2658. } // namespace detail
  2659. struct in_place_tag {
  2660. constexpr in_place_tag() = default;
  2661. };
  2662. constexpr inline in_place_tag in_place(detail::in_place_of_tag) {
  2663. return in_place_tag();
  2664. }
  2665. template <typename T>
  2666. constexpr inline in_place_tag in_place(detail::in_place_of_t<T>) {
  2667. return in_place_tag();
  2668. }
  2669. template <std::size_t I>
  2670. constexpr inline in_place_tag in_place(detail::in_place_of_i<I>) {
  2671. return in_place_tag();
  2672. }
  2673. constexpr inline in_place_tag in_place_of(detail::in_place_of_tag) {
  2674. return in_place_tag();
  2675. }
  2676. template <typename T>
  2677. constexpr inline in_place_tag in_place_type(detail::in_place_of_t<T>) {
  2678. return in_place_tag();
  2679. }
  2680. template <std::size_t I>
  2681. constexpr inline in_place_tag in_place_index(detail::in_place_of_i<I>) {
  2682. return in_place_tag();
  2683. }
  2684. using in_place_t = in_place_tag (&)(detail::in_place_of_tag);
  2685. template <typename T>
  2686. using in_place_type_t = in_place_tag (&)(detail::in_place_of_t<T>);
  2687. template <std::size_t I>
  2688. using in_place_index_t = in_place_tag (&)(detail::in_place_of_i<I>);
  2689. #endif
  2690. } // namespace sol
  2691. // end of sol/in_place.hpp
  2692. #if defined(SOL_USE_BOOST) && SOL_USE_BOOST
  2693. #include <boost/optional.hpp>
  2694. #else
  2695. // beginning of sol/optional_implementation.hpp
  2696. #include <initializer_list>
  2697. #include <cassert>
  2698. #if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS
  2699. #include <cstdlib>
  2700. #endif // Exceptions
  2701. #define TR2_OPTIONAL_REQUIRES(...) typename ::std::enable_if<__VA_ARGS__::value, bool>::type = false
  2702. #if defined __GNUC__ // NOTE: GNUC is also defined for Clang
  2703. #if (__GNUC__ >= 5)
  2704. #define TR2_OPTIONAL_GCC_5_0_AND_HIGHER___
  2705. #define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
  2706. #elif (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)
  2707. #define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
  2708. #elif (__GNUC__ > 4)
  2709. #define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
  2710. #endif
  2711. #
  2712. #if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)
  2713. #define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___
  2714. #elif (__GNUC__ > 4)
  2715. #define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___
  2716. #endif
  2717. #
  2718. #if (__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && (__GNUC_PATCHLEVEL__ >= 1)
  2719. #define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
  2720. #elif (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)
  2721. #define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
  2722. #elif (__GNUC__ > 4)
  2723. #define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
  2724. #endif
  2725. #endif
  2726. #
  2727. #if defined __clang_major__
  2728. #if (__clang_major__ == 3 && __clang_minor__ >= 5)
  2729. #define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
  2730. #elif (__clang_major__ > 3)
  2731. #define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
  2732. #endif
  2733. #if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
  2734. #define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
  2735. #elif (__clang_major__ == 3 && __clang_minor__ == 4 && __clang_patchlevel__ >= 2)
  2736. #define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
  2737. #endif
  2738. #endif
  2739. #
  2740. #if defined _MSC_VER
  2741. #if (_MSC_VER >= 1900)
  2742. #define TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
  2743. #endif
  2744. #endif
  2745. #if defined __clang__
  2746. #if (__clang_major__ > 2) || (__clang_major__ == 2) && (__clang_minor__ >= 9)
  2747. #define OPTIONAL_HAS_THIS_RVALUE_REFS 1
  2748. #else
  2749. #define OPTIONAL_HAS_THIS_RVALUE_REFS 0
  2750. #endif
  2751. #elif defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
  2752. #define OPTIONAL_HAS_THIS_RVALUE_REFS 1
  2753. #elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
  2754. #define OPTIONAL_HAS_THIS_RVALUE_REFS 1
  2755. #else
  2756. #define OPTIONAL_HAS_THIS_RVALUE_REFS 0
  2757. #endif
  2758. #if defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
  2759. #define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 1
  2760. #define OPTIONAL_CONSTEXPR_INIT_LIST constexpr
  2761. #else
  2762. #define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 0
  2763. #define OPTIONAL_CONSTEXPR_INIT_LIST
  2764. #endif
  2765. #if defined(TR2_OPTIONAL_MSVC_2015_AND_HIGHER___) || (defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ && (defined __cplusplus) && (__cplusplus != 201103L))
  2766. #define OPTIONAL_HAS_MOVE_ACCESSORS 1
  2767. #else
  2768. #define OPTIONAL_HAS_MOVE_ACCESSORS 0
  2769. #endif
  2770. #// In C++11 constexpr implies const, so we need to make non-const members also non-constexpr
  2771. #if defined(TR2_OPTIONAL_MSVC_2015_AND_HIGHER___) || ((defined __cplusplus) && (__cplusplus == 201103L))
  2772. #define OPTIONAL_MUTABLE_CONSTEXPR
  2773. #else
  2774. #define OPTIONAL_MUTABLE_CONSTEXPR constexpr
  2775. #endif
  2776. #if defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
  2777. #pragma warning(push)
  2778. #pragma warning(disable : 4814)
  2779. #endif
  2780. namespace sol {
  2781. // BEGIN workaround for missing is_trivially_destructible
  2782. #if defined TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
  2783. // leave it: it is already there
  2784. #elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
  2785. // leave it: it is already there
  2786. #elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
  2787. // leave it: it is already there
  2788. #elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS
  2789. // leave it: the user doesn't want it
  2790. #else
  2791. template <typename T>
  2792. using is_trivially_destructible = ::std::has_trivial_destructor<T>;
  2793. #endif
  2794. // END workaround for missing is_trivially_destructible
  2795. #if (defined TR2_OPTIONAL_GCC_4_7_AND_HIGHER___)
  2796. // leave it; our metafunctions are already defined.
  2797. #elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
  2798. // leave it; our metafunctions are already defined.
  2799. #elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
  2800. // leave it: it is already there
  2801. #elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS
  2802. // leave it: the user doesn't want it
  2803. #else
  2804. // workaround for missing traits in GCC and CLANG
  2805. template <class T>
  2806. struct is_nothrow_move_constructible {
  2807. static constexprbool value = ::std::is_nothrow_constructible<T, T&&>::value;
  2808. };
  2809. template <class T, class U>
  2810. struct is_assignable {
  2811. template <class X, class Y>
  2812. static constexprbool has_assign(...) {
  2813. return false;
  2814. }
  2815. template <class X, class Y, size_t S = sizeof((::std::declval<X>() = ::std::declval<Y>(), true))>
  2816. // the comma operator is necessary for the cases where operator= returns void
  2817. static constexprbool has_assign(bool) {
  2818. return true;
  2819. }
  2820. static constexprbool value = has_assign<T, U>(true);
  2821. };
  2822. template <class T>
  2823. struct is_nothrow_move_assignable {
  2824. template <class X, bool has_any_move_assign>
  2825. struct has_nothrow_move_assign {
  2826. static constexprbool value = false;
  2827. };
  2828. template <class X>
  2829. struct has_nothrow_move_assign<X, true> {
  2830. static constexprbool value = noexcept(::std::declval<X&>() = ::std::declval<X&&>());
  2831. };
  2832. static constexprbool value = has_nothrow_move_assign<T, is_assignable<T&, T&&>::value>::value;
  2833. };
  2834. // end workaround
  2835. #endif
  2836. // 20.5.4, optional for object types
  2837. template <class T>
  2838. class optional;
  2839. // 20.5.5, optional for lvalue reference types
  2840. template <class T>
  2841. class optional<T&>;
  2842. // workaround: std utility functions aren't constexpr yet
  2843. template <class T>
  2844. inline constexpr T&& constexpr_forward(typename ::std::remove_reference<T>::type& t) noexcept {
  2845. return static_cast<T&&>(t);
  2846. }
  2847. template <class T>
  2848. inline constexpr T&& constexpr_forward(typename ::std::remove_reference<T>::type&& t) noexcept {
  2849. static_assert(!::std::is_lvalue_reference<T>::value, "!!");
  2850. return static_cast<T&&>(t);
  2851. }
  2852. template <class T>
  2853. inline constexpr typename ::std::remove_reference<T>::type&& constexpr_move(T&& t) noexcept {
  2854. return static_cast<typename ::std::remove_reference<T>::type&&>(t);
  2855. }
  2856. #if defined NDEBUG
  2857. #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR)
  2858. #else
  2859. #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) ((CHECK) ? (EXPR) : ([] { assert(!#CHECK); }(), (EXPR)))
  2860. #endif
  2861. namespace detail_ {
  2862. // static_addressof: a constexpr version of addressof
  2863. template <typename T>
  2864. struct has_overloaded_addressof {
  2865. template <class X>
  2866. static constexpr bool has_overload(...) {
  2867. return false;
  2868. }
  2869. template <class X, size_t S = sizeof(::std::declval<X&>().operator&())>
  2870. static constexpr bool has_overload(bool) {
  2871. return true;
  2872. }
  2873. static constexpr bool value = has_overload<T>(true);
  2874. };
  2875. template <typename T, TR2_OPTIONAL_REQUIRES(!has_overloaded_addressof<T>)>
  2876. constexpr T* static_addressof(T& ref) {
  2877. return &ref;
  2878. }
  2879. template <typename T, TR2_OPTIONAL_REQUIRES(has_overloaded_addressof<T>)>
  2880. T* static_addressof(T& ref) {
  2881. return ::std::addressof(ref);
  2882. }
  2883. // the call to convert<A>(b) has return type A and converts b to type A iff b decltype(b) is implicitly convertible to A
  2884. template <class U>
  2885. constexpr U convert(U v) {
  2886. return v;
  2887. }
  2888. } // namespace detail_
  2889. constexpr struct trivial_init_t {
  2890. } trivial_init{};
  2891. // 20.5.7, Disengaged state indicator
  2892. struct nullopt_t {
  2893. struct init {};
  2894. constexpr explicit nullopt_t(init) {
  2895. }
  2896. };
  2897. constexpr nullopt_t nullopt{nullopt_t::init()};
  2898. // 20.5.8, class bad_optional_access
  2899. class bad_optional_access : public ::std::logic_error {
  2900. public:
  2901. explicit bad_optional_access(const ::std::string& what_arg)
  2902. : ::std::logic_error{what_arg} {
  2903. }
  2904. explicit bad_optional_access(const char* what_arg)
  2905. : ::std::logic_error{what_arg} {
  2906. }
  2907. };
  2908. template <class T>
  2909. struct alignas(T) optional_base {
  2910. char storage_[sizeof(T)];
  2911. bool init_;
  2912. constexpr optional_base() noexcept
  2913. : storage_(), init_(false){};
  2914. explicit optional_base(const T& v)
  2915. : storage_(), init_(true) {
  2916. new (&storage()) T(v);
  2917. }
  2918. explicit optional_base(T&& v)
  2919. : storage_(), init_(true) {
  2920. new (&storage()) T(constexpr_move(v));
  2921. }
  2922. template <class... Args>
  2923. explicit optional_base(in_place_t, Args&&... args)
  2924. : init_(true), storage_() {
  2925. new (&storage()) T(constexpr_forward<Args>(args)...);
  2926. }
  2927. template <class U, class... Args, TR2_OPTIONAL_REQUIRES(::std::is_constructible<T, ::std::initializer_list<U>>)>
  2928. explicit optional_base(in_place_t, ::std::initializer_list<U> il, Args&&... args)
  2929. : init_(true), storage_() {
  2930. new (&storage()) T(il, constexpr_forward<Args>(args)...);
  2931. }
  2932. #if defined __GNUC__
  2933. #pragma GCC diagnostic push
  2934. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  2935. #endif
  2936. T& storage() {
  2937. return *reinterpret_cast<T*>(&storage_[0]);
  2938. }
  2939. constexpr const T& storage() const {
  2940. return *reinterpret_cast<T const*>(&storage_[0]);
  2941. }
  2942. #if defined __GNUC__
  2943. #pragma GCC diagnostic pop
  2944. #endif
  2945. ~optional_base() {
  2946. if (init_) {
  2947. storage().T::~T();
  2948. }
  2949. }
  2950. };
  2951. #if defined __GNUC__ && !defined TR2_OPTIONAL_GCC_5_0_AND_HIGHER___
  2952. // Sorry, GCC 4.x; you're just a piece of shit
  2953. template <typename T>
  2954. using constexpr_optional_base = optional_base<T>;
  2955. #else
  2956. template <class T>
  2957. struct alignas(T) constexpr_optional_base {
  2958. char storage_[sizeof(T)];
  2959. bool init_;
  2960. constexpr constexpr_optional_base() noexcept
  2961. : storage_(), init_(false) {
  2962. }
  2963. explicit constexpr constexpr_optional_base(const T& v)
  2964. : storage_(), init_(true) {
  2965. new (&storage()) T(v);
  2966. }
  2967. explicit constexpr constexpr_optional_base(T&& v)
  2968. : storage_(), init_(true) {
  2969. new (&storage()) T(constexpr_move(v));
  2970. }
  2971. template <class... Args>
  2972. explicit constexpr constexpr_optional_base(in_place_t, Args&&... args)
  2973. : init_(true), storage_() {
  2974. new (&storage()) T(constexpr_forward<Args>(args)...);
  2975. }
  2976. template <class U, class... Args, TR2_OPTIONAL_REQUIRES(::std::is_constructible<T, ::std::initializer_list<U>>)>
  2977. OPTIONAL_CONSTEXPR_INIT_LIST explicit constexpr_optional_base(in_place_t, ::std::initializer_list<U> il, Args&&... args)
  2978. : init_(true), storage_() {
  2979. new (&storage()) T(il, constexpr_forward<Args>(args)...);
  2980. }
  2981. #if defined __GNUC__
  2982. #pragma GCC diagnostic push
  2983. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  2984. #endif
  2985. T& storage() {
  2986. return (*reinterpret_cast<T*>(&storage_[0]));
  2987. }
  2988. constexpr const T& storage() const {
  2989. return (*reinterpret_cast<T const*>(&storage_[0]));
  2990. }
  2991. #if defined __GNUC__
  2992. #pragma GCC diagnostic pop
  2993. #endif
  2994. ~constexpr_optional_base() = default;
  2995. };
  2996. #endif
  2997. template <class T>
  2998. using OptionalBase = typename ::std::conditional<
  2999. ::std::is_trivially_destructible<T>::value,
  3000. constexpr_optional_base<typename ::std::remove_const<T>::type>,
  3001. optional_base<typename ::std::remove_const<T>::type>>::type;
  3002. template <class T>
  3003. class optional : private OptionalBase<T> {
  3004. static_assert(!::std::is_same<typename ::std::decay<T>::type, nullopt_t>::value, "bad T");
  3005. static_assert(!::std::is_same<typename ::std::decay<T>::type, in_place_t>::value, "bad T");
  3006. constexpr bool initialized() const noexcept {
  3007. return OptionalBase<T>::init_;
  3008. }
  3009. typename ::std::remove_const<T>::type* dataptr() {
  3010. return ::std::addressof(OptionalBase<T>::storage());
  3011. }
  3012. constexpr const T* dataptr() const {
  3013. return detail_::static_addressof(OptionalBase<T>::storage());
  3014. }
  3015. #if OPTIONAL_HAS_THIS_RVALUE_REFS == 1
  3016. constexpr const T& contained_val() const& {
  3017. return OptionalBase<T>::storage();
  3018. }
  3019. #if OPTIONAL_HAS_MOVE_ACCESSORS == 1
  3020. OPTIONAL_MUTABLE_CONSTEXPR T&& contained_val() && {
  3021. return ::std::move(OptionalBase<T>::storage());
  3022. }
  3023. OPTIONAL_MUTABLE_CONSTEXPR T& contained_val() & {
  3024. return OptionalBase<T>::storage();
  3025. }
  3026. #else
  3027. T& contained_val() & {
  3028. return OptionalBase<T>::storage();
  3029. }
  3030. T&& contained_val() && {
  3031. return ::std::move(OptionalBase<T>::storage());
  3032. }
  3033. #endif
  3034. #else
  3035. constexpr const T& contained_val() const {
  3036. return OptionalBase<T>::storage();
  3037. }
  3038. T& contained_val() {
  3039. return OptionalBase<T>::storage();
  3040. }
  3041. #endif
  3042. void clear() noexcept {
  3043. if (initialized())
  3044. dataptr()->T::~T();
  3045. OptionalBase<T>::init_ = false;
  3046. }
  3047. template <class... Args>
  3048. void initialize(Args&&... args) noexcept(noexcept(T(::std::forward<Args>(args)...))) {
  3049. assert(!OptionalBase<T>::init_);
  3050. ::new (static_cast<void*>(dataptr())) T(::std::forward<Args>(args)...);
  3051. OptionalBase<T>::init_ = true;
  3052. }
  3053. template <class U, class... Args>
  3054. void initialize(::std::initializer_list<U> il, Args&&... args) noexcept(noexcept(T(il, ::std::forward<Args>(args)...))) {
  3055. assert(!OptionalBase<T>::init_);
  3056. ::new (static_cast<void*>(dataptr())) T(il, ::std::forward<Args>(args)...);
  3057. OptionalBase<T>::init_ = true;
  3058. }
  3059. public:
  3060. typedef T value_type;
  3061. // 20.5.5.1, constructors
  3062. constexpr optional() noexcept
  3063. : OptionalBase<T>(){};
  3064. constexpr optional(nullopt_t) noexcept
  3065. : OptionalBase<T>(){};
  3066. optional(const optional& rhs)
  3067. : OptionalBase<T>() {
  3068. if (rhs.initialized()) {
  3069. ::new (static_cast<void*>(dataptr())) T(*rhs);
  3070. OptionalBase<T>::init_ = true;
  3071. }
  3072. }
  3073. optional(const optional<T&>& rhs)
  3074. : optional() {
  3075. if (rhs) {
  3076. ::new (static_cast<void*>(dataptr())) T(*rhs);
  3077. OptionalBase<T>::init_ = true;
  3078. }
  3079. }
  3080. optional(optional&& rhs) noexcept(::std::is_nothrow_move_constructible<T>::value)
  3081. : OptionalBase<T>() {
  3082. if (rhs.initialized()) {
  3083. ::new (static_cast<void*>(dataptr())) T(::std::move(*rhs));
  3084. OptionalBase<T>::init_ = true;
  3085. }
  3086. }
  3087. constexpr optional(const T& v)
  3088. : OptionalBase<T>(v) {
  3089. }
  3090. constexpr optional(T&& v)
  3091. : OptionalBase<T>(constexpr_move(v)) {
  3092. }
  3093. template <class... Args>
  3094. explicit constexpr optional(in_place_t, Args&&... args)
  3095. : OptionalBase<T>(in_place, constexpr_forward<Args>(args)...) {
  3096. }
  3097. template <class U, class... Args, TR2_OPTIONAL_REQUIRES(::std::is_constructible<T, ::std::initializer_list<U>>)>
  3098. OPTIONAL_CONSTEXPR_INIT_LIST explicit optional(in_place_t, ::std::initializer_list<U> il, Args&&... args)
  3099. : OptionalBase<T>(in_place, il, constexpr_forward<Args>(args)...) {
  3100. }
  3101. // 20.5.4.2, Destructor
  3102. ~optional() = default;
  3103. // 20.5.4.3, assignment
  3104. optional& operator=(nullopt_t) noexcept {
  3105. clear();
  3106. return *this;
  3107. }
  3108. optional& operator=(const optional& rhs) {
  3109. if (initialized() == true && rhs.initialized() == false)
  3110. clear();
  3111. else if (initialized() == false && rhs.initialized() == true)
  3112. initialize(*rhs);
  3113. else if (initialized() == true && rhs.initialized() == true)
  3114. contained_val() = *rhs;
  3115. return *this;
  3116. }
  3117. optional& operator=(optional&& rhs) noexcept(::std::is_nothrow_move_assignable<T>::value&& ::std::is_nothrow_move_constructible<T>::value) {
  3118. if (initialized() == true && rhs.initialized() == false)
  3119. clear();
  3120. else if (initialized() == false && rhs.initialized() == true)
  3121. initialize(::std::move(*rhs));
  3122. else if (initialized() == true && rhs.initialized() == true)
  3123. contained_val() = ::std::move(*rhs);
  3124. return *this;
  3125. }
  3126. template <class U>
  3127. auto operator=(U&& v)
  3128. -> typename ::std::enable_if<
  3129. ::std::is_same<typename ::std::decay<U>::type, T>::value,
  3130. optional&>::type {
  3131. if (initialized()) {
  3132. contained_val() = ::std::forward<U>(v);
  3133. }
  3134. else {
  3135. initialize(::std::forward<U>(v));
  3136. }
  3137. return *this;
  3138. }
  3139. template <class... Args>
  3140. void emplace(Args&&... args) {
  3141. clear();
  3142. initialize(::std::forward<Args>(args)...);
  3143. }
  3144. template <class U, class... Args>
  3145. void emplace(::std::initializer_list<U> il, Args&&... args) {
  3146. clear();
  3147. initialize<U, Args...>(il, ::std::forward<Args>(args)...);
  3148. }
  3149. // 20.5.4.4, Swap
  3150. void swap(optional<T>& rhs) noexcept(::std::is_nothrow_move_constructible<T>::value&& noexcept(swap(::std::declval<T&>(), ::std::declval<T&>()))) {
  3151. if (initialized() == true && rhs.initialized() == false) {
  3152. rhs.initialize(::std::move(**this));
  3153. clear();
  3154. }
  3155. else if (initialized() == false && rhs.initialized() == true) {
  3156. initialize(::std::move(*rhs));
  3157. rhs.clear();
  3158. }
  3159. else if (initialized() == true && rhs.initialized() == true) {
  3160. using ::std::swap;
  3161. swap(**this, *rhs);
  3162. }
  3163. }
  3164. // 20.5.4.5, Observers
  3165. explicit constexpr operator bool() const noexcept {
  3166. return initialized();
  3167. }
  3168. constexpr T const* operator->() const {
  3169. return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), dataptr());
  3170. }
  3171. #if OPTIONAL_HAS_MOVE_ACCESSORS == 1
  3172. OPTIONAL_MUTABLE_CONSTEXPR T* operator->() {
  3173. assert(initialized());
  3174. return dataptr();
  3175. }
  3176. constexpr T const& operator*() const& {
  3177. return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val());
  3178. }
  3179. OPTIONAL_MUTABLE_CONSTEXPR T& operator*() & {
  3180. assert(initialized());
  3181. return contained_val();
  3182. }
  3183. OPTIONAL_MUTABLE_CONSTEXPR T&& operator*() && {
  3184. assert(initialized());
  3185. return constexpr_move(contained_val());
  3186. }
  3187. constexpr T const& value() const& {
  3188. return initialized() ? contained_val()
  3189. #ifdef SOL_NO_EXCEPTIONS
  3190. // we can't abort here
  3191. // because there's no constexpr abort
  3192. : *static_cast<T*>(nullptr);
  3193. #else
  3194. : (throw bad_optional_access("bad optional access"), contained_val());
  3195. #endif
  3196. }
  3197. OPTIONAL_MUTABLE_CONSTEXPR T& value() & {
  3198. return initialized() ? contained_val()
  3199. #ifdef SOL_NO_EXCEPTIONS
  3200. : *static_cast<T*>(nullptr);
  3201. #else
  3202. : (throw bad_optional_access("bad optional access"), contained_val());
  3203. #endif
  3204. }
  3205. OPTIONAL_MUTABLE_CONSTEXPR T&& value() && {
  3206. return initialized() ? contained_val()
  3207. #ifdef SOL_NO_EXCEPTIONS
  3208. // we can't abort here
  3209. // because there's no constexpr abort
  3210. : std::move(*static_cast<T*>(nullptr));
  3211. #else
  3212. : (throw bad_optional_access("bad optional access"), contained_val());
  3213. #endif
  3214. }
  3215. #else
  3216. T* operator->() {
  3217. assert(initialized());
  3218. return dataptr();
  3219. }
  3220. constexpr T const& operator*() const {
  3221. return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val());
  3222. }
  3223. T& operator*() {
  3224. assert(initialized());
  3225. return contained_val();
  3226. }
  3227. constexpr T const& value() const {
  3228. return initialized() ? contained_val()
  3229. #ifdef SOL_NO_EXCEPTIONS
  3230. // we can't abort here
  3231. // because there's no constexpr abort
  3232. : *static_cast<T*>(nullptr);
  3233. #else
  3234. : (throw bad_optional_access("bad optional access"), contained_val());
  3235. #endif
  3236. }
  3237. T& value() {
  3238. return initialized() ? contained_val()
  3239. #ifdef SOL_NO_EXCEPTIONS
  3240. // we can abort here
  3241. // but the others are constexpr, so we can't...
  3242. : (std::abort(), *static_cast<T*>(nullptr));
  3243. #else
  3244. : (throw bad_optional_access("bad optional access"), contained_val());
  3245. #endif
  3246. }
  3247. #endif
  3248. #if OPTIONAL_HAS_THIS_RVALUE_REFS == 1
  3249. template <class V>
  3250. constexpr T value_or(V&& v) const& {
  3251. return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
  3252. }
  3253. #if OPTIONAL_HAS_MOVE_ACCESSORS == 1
  3254. template <class V>
  3255. OPTIONAL_MUTABLE_CONSTEXPR T value_or(V&& v) && {
  3256. return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val()) : detail_::convert<T>(constexpr_forward<V>(v));
  3257. }
  3258. #else
  3259. template <class V>
  3260. T value_or(V&& v) && {
  3261. return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val()) : detail_::convert<T>(constexpr_forward<V>(v));
  3262. }
  3263. #endif
  3264. #else
  3265. template <class V>
  3266. constexpr T value_or(V&& v) const {
  3267. return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
  3268. }
  3269. #endif
  3270. };
  3271. template <class T>
  3272. class optional<T&> {
  3273. static_assert(!::std::is_same<T, nullopt_t>::value, "bad T");
  3274. static_assert(!::std::is_same<T, in_place_t>::value, "bad T");
  3275. T* ref;
  3276. public:
  3277. // 20.5.5.1, construction/destruction
  3278. constexpr optional() noexcept
  3279. : ref(nullptr) {
  3280. }
  3281. constexpr optional(nullopt_t) noexcept
  3282. : ref(nullptr) {
  3283. }
  3284. constexpr optional(T& v) noexcept
  3285. : ref(detail_::static_addressof(v)) {
  3286. }
  3287. optional(T&&) = delete;
  3288. constexpr optional(const optional& rhs) noexcept
  3289. : ref(rhs.ref) {
  3290. }
  3291. explicit constexpr optional(in_place_t, T& v) noexcept
  3292. : ref(detail_::static_addressof(v)) {
  3293. }
  3294. explicit optional(in_place_t, T&&) = delete;
  3295. ~optional() = default;
  3296. // 20.5.5.2, mutation
  3297. optional& operator=(nullopt_t) noexcept {
  3298. ref = nullptr;
  3299. return *this;
  3300. }
  3301. // optional& operator=(const optional& rhs) noexcept {
  3302. // ref = rhs.ref;
  3303. // return *this;
  3304. // }
  3305. // optional& operator=(optional&& rhs) noexcept {
  3306. // ref = rhs.ref;
  3307. // return *this;
  3308. // }
  3309. template <typename U>
  3310. auto operator=(U&& rhs) noexcept
  3311. -> typename ::std::enable_if<
  3312. ::std::is_same<typename ::std::decay<U>::type, optional<T&>>::value,
  3313. optional&>::type {
  3314. ref = rhs.ref;
  3315. return *this;
  3316. }
  3317. template <typename U>
  3318. auto operator=(U&& rhs) noexcept
  3319. -> typename ::std::enable_if<
  3320. !::std::is_same<typename ::std::decay<U>::type, optional<T&>>::value,
  3321. optional&>::type = delete;
  3322. void emplace(T& v) noexcept {
  3323. ref = detail_::static_addressof(v);
  3324. }
  3325. void emplace(T&&) = delete;
  3326. void swap(optional<T&>& rhs) noexcept {
  3327. ::std::swap(ref, rhs.ref);
  3328. }
  3329. // 20.5.5.3, observers
  3330. constexpr T* operator->() const {
  3331. return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, ref);
  3332. }
  3333. constexpr T& operator*() const {
  3334. return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, *ref);
  3335. }
  3336. constexpr T& value() const {
  3337. #ifdef SOL_NO_EXCEPTIONS
  3338. return *ref;
  3339. #else
  3340. return ref ? *ref
  3341. : (throw bad_optional_access("bad optional access"), *ref);
  3342. #endif // Exceptions
  3343. }
  3344. explicit constexpr operator bool() const noexcept {
  3345. return ref != nullptr;
  3346. }
  3347. template <typename V>
  3348. constexpr T& value_or(V&& v) const {
  3349. return *this ? **this : detail_::convert<T&>(constexpr_forward<V>(v));
  3350. }
  3351. };
  3352. template <class T>
  3353. class optional<T&&> {
  3354. static_assert(sizeof(T) == 0, "optional rvalue references disallowed");
  3355. };
  3356. // 20.5.8, Relational operators
  3357. template <class T>
  3358. constexpr bool operator==(const optional<T>& x, const optional<T>& y) {
  3359. return bool(x) != bool(y) ? false : bool(x) == false ? true : *x == *y;
  3360. }
  3361. template <class T>
  3362. constexpr bool operator!=(const optional<T>& x, const optional<T>& y) {
  3363. return !(x == y);
  3364. }
  3365. template <class T>
  3366. constexpr bool operator<(const optional<T>& x, const optional<T>& y) {
  3367. return (!y) ? false : (!x) ? true : *x < *y;
  3368. }
  3369. template <class T>
  3370. constexpr bool operator>(const optional<T>& x, const optional<T>& y) {
  3371. return (y < x);
  3372. }
  3373. template <class T>
  3374. constexpr bool operator<=(const optional<T>& x, const optional<T>& y) {
  3375. return !(y < x);
  3376. }
  3377. template <class T>
  3378. constexpr bool operator>=(const optional<T>& x, const optional<T>& y) {
  3379. return !(x < y);
  3380. }
  3381. // 20.5.9, Comparison with nullopt
  3382. template <class T>
  3383. constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept {
  3384. return (!x);
  3385. }
  3386. template <class T>
  3387. constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept {
  3388. return (!x);
  3389. }
  3390. template <class T>
  3391. constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept {
  3392. return bool(x);
  3393. }
  3394. template <class T>
  3395. constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept {
  3396. return bool(x);
  3397. }
  3398. template <class T>
  3399. constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
  3400. return false;
  3401. }
  3402. template <class T>
  3403. constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept {
  3404. return bool(x);
  3405. }
  3406. template <class T>
  3407. constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept {
  3408. return (!x);
  3409. }
  3410. template <class T>
  3411. constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
  3412. return true;
  3413. }
  3414. template <class T>
  3415. constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept {
  3416. return bool(x);
  3417. }
  3418. template <class T>
  3419. constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
  3420. return false;
  3421. }
  3422. template <class T>
  3423. constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
  3424. return true;
  3425. }
  3426. template <class T>
  3427. constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept {
  3428. return (!x);
  3429. }
  3430. // 20.5.10, Comparison with T
  3431. template <class T>
  3432. constexpr bool operator==(const optional<T>& x, const T& v) {
  3433. return bool(x) ? *x == v : false;
  3434. }
  3435. template <class T>
  3436. constexpr bool operator==(const T& v, const optional<T>& x) {
  3437. return bool(x) ? v == *x : false;
  3438. }
  3439. template <class T>
  3440. constexpr bool operator!=(const optional<T>& x, const T& v) {
  3441. return bool(x) ? *x != v : true;
  3442. }
  3443. template <class T>
  3444. constexpr bool operator!=(const T& v, const optional<T>& x) {
  3445. return bool(x) ? v != *x : true;
  3446. }
  3447. template <class T>
  3448. constexpr bool operator<(const optional<T>& x, const T& v) {
  3449. return bool(x) ? *x < v : true;
  3450. }
  3451. template <class T>
  3452. constexpr bool operator>(const T& v, const optional<T>& x) {
  3453. return bool(x) ? v > *x : true;
  3454. }
  3455. template <class T>
  3456. constexpr bool operator>(const optional<T>& x, const T& v) {
  3457. return bool(x) ? *x > v : false;
  3458. }
  3459. template <class T>
  3460. constexpr bool operator<(const T& v, const optional<T>& x) {
  3461. return bool(x) ? v < *x : false;
  3462. }
  3463. template <class T>
  3464. constexpr bool operator>=(const optional<T>& x, const T& v) {
  3465. return bool(x) ? *x >= v : false;
  3466. }
  3467. template <class T>
  3468. constexpr bool operator<=(const T& v, const optional<T>& x) {
  3469. return bool(x) ? v <= *x : false;
  3470. }
  3471. template <class T>
  3472. constexpr bool operator<=(const optional<T>& x, const T& v) {
  3473. return bool(x) ? *x <= v : true;
  3474. }
  3475. template <class T>
  3476. constexpr bool operator>=(const T& v, const optional<T>& x) {
  3477. return bool(x) ? v >= *x : true;
  3478. }
  3479. // Comparison of optional<T&> with T
  3480. template <class T>
  3481. constexpr bool operator==(const optional<T&>& x, const T& v) {
  3482. return bool(x) ? *x == v : false;
  3483. }
  3484. template <class T>
  3485. constexpr bool operator==(const T& v, const optional<T&>& x) {
  3486. return bool(x) ? v == *x : false;
  3487. }
  3488. template <class T>
  3489. constexpr bool operator!=(const optional<T&>& x, const T& v) {
  3490. return bool(x) ? *x != v : true;
  3491. }
  3492. template <class T>
  3493. constexpr bool operator!=(const T& v, const optional<T&>& x) {
  3494. return bool(x) ? v != *x : true;
  3495. }
  3496. template <class T>
  3497. constexpr bool operator<(const optional<T&>& x, const T& v) {
  3498. return bool(x) ? *x < v : true;
  3499. }
  3500. template <class T>
  3501. constexpr bool operator>(const T& v, const optional<T&>& x) {
  3502. return bool(x) ? v > *x : true;
  3503. }
  3504. template <class T>
  3505. constexpr bool operator>(const optional<T&>& x, const T& v) {
  3506. return bool(x) ? *x > v : false;
  3507. }
  3508. template <class T>
  3509. constexpr bool operator<(const T& v, const optional<T&>& x) {
  3510. return bool(x) ? v < *x : false;
  3511. }
  3512. template <class T>
  3513. constexpr bool operator>=(const optional<T&>& x, const T& v) {
  3514. return bool(x) ? *x >= v : false;
  3515. }
  3516. template <class T>
  3517. constexpr bool operator<=(const T& v, const optional<T&>& x) {
  3518. return bool(x) ? v <= *x : false;
  3519. }
  3520. template <class T>
  3521. constexpr bool operator<=(const optional<T&>& x, const T& v) {
  3522. return bool(x) ? *x <= v : true;
  3523. }
  3524. template <class T>
  3525. constexpr bool operator>=(const T& v, const optional<T&>& x) {
  3526. return bool(x) ? v >= *x : true;
  3527. }
  3528. // Comparison of optional<T const&> with T
  3529. template <class T>
  3530. constexpr bool operator==(const optional<const T&>& x, const T& v) {
  3531. return bool(x) ? *x == v : false;
  3532. }
  3533. template <class T>
  3534. constexpr bool operator==(const T& v, const optional<const T&>& x) {
  3535. return bool(x) ? v == *x : false;
  3536. }
  3537. template <class T>
  3538. constexpr bool operator!=(const optional<const T&>& x, const T& v) {
  3539. return bool(x) ? *x != v : true;
  3540. }
  3541. template <class T>
  3542. constexpr bool operator!=(const T& v, const optional<const T&>& x) {
  3543. return bool(x) ? v != *x : true;
  3544. }
  3545. template <class T>
  3546. constexpr bool operator<(const optional<const T&>& x, const T& v) {
  3547. return bool(x) ? *x < v : true;
  3548. }
  3549. template <class T>
  3550. constexpr bool operator>(const T& v, const optional<const T&>& x) {
  3551. return bool(x) ? v > *x : true;
  3552. }
  3553. template <class T>
  3554. constexpr bool operator>(const optional<const T&>& x, const T& v) {
  3555. return bool(x) ? *x > v : false;
  3556. }
  3557. template <class T>
  3558. constexpr bool operator<(const T& v, const optional<const T&>& x) {
  3559. return bool(x) ? v < *x : false;
  3560. }
  3561. template <class T>
  3562. constexpr bool operator>=(const optional<const T&>& x, const T& v) {
  3563. return bool(x) ? *x >= v : false;
  3564. }
  3565. template <class T>
  3566. constexpr bool operator<=(const T& v, const optional<const T&>& x) {
  3567. return bool(x) ? v <= *x : false;
  3568. }
  3569. template <class T>
  3570. constexpr bool operator<=(const optional<const T&>& x, const T& v) {
  3571. return bool(x) ? *x <= v : true;
  3572. }
  3573. template <class T>
  3574. constexpr bool operator>=(const T& v, const optional<const T&>& x) {
  3575. return bool(x) ? v >= *x : true;
  3576. }
  3577. // 20.5.12, Specialized algorithms
  3578. template <class T>
  3579. void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y))) {
  3580. x.swap(y);
  3581. }
  3582. template <class T>
  3583. constexpr optional<typename ::std::decay<T>::type> make_optional(T&& v) {
  3584. return optional<typename ::std::decay<T>::type>(constexpr_forward<T>(v));
  3585. }
  3586. template <class X>
  3587. constexpr optional<X&> make_optional(::std::reference_wrapper<X> v) {
  3588. return optional<X&>(v.get());
  3589. }
  3590. } // namespace sol
  3591. namespace std {
  3592. template <typename T>
  3593. struct hash<sol::optional<T>> {
  3594. typedef typename hash<T>::result_type result_type;
  3595. typedef sol::optional<T> argument_type;
  3596. constexpr result_type operator()(argument_type const& arg) const {
  3597. return arg ? ::std::hash<T>{}(*arg) : result_type{};
  3598. }
  3599. };
  3600. template <typename T>
  3601. struct hash<sol::optional<T&>> {
  3602. typedef typename hash<T>::result_type result_type;
  3603. typedef sol::optional<T&> argument_type;
  3604. constexpr result_type operator()(argument_type const& arg) const {
  3605. return arg ? ::std::hash<T>{}(*arg) : result_type{};
  3606. }
  3607. };
  3608. } // namespace std
  3609. #if defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
  3610. #pragma warning(pop)
  3611. #endif
  3612. #undef TR2_OPTIONAL_REQUIRES
  3613. #undef TR2_OPTIONAL_ASSERTED_EXPRESSION
  3614. // end of sol/optional_implementation.hpp
  3615. #endif // Boost vs. Better optional
  3616. namespace sol {
  3617. #if defined(SOL_USE_BOOST) && SOL_USE_BOOST
  3618. template <typename T>
  3619. using optional = boost::optional<T>;
  3620. using nullopt_t = boost::none_t;
  3621. const nullopt_t nullopt = boost::none;
  3622. #endif // Boost vs. Better optional
  3623. namespace meta {
  3624. template <typename T>
  3625. struct is_optional : std::false_type {};
  3626. template <typename T>
  3627. struct is_optional<optional<T>> : std::true_type {};
  3628. } // namespace meta
  3629. } // namespace sol
  3630. // end of sol/optional.hpp
  3631. // beginning of sol/forward_detail.hpp
  3632. namespace sol {
  3633. namespace detail {
  3634. const bool default_safe_function_calls =
  3635. #if defined(SOL_SAFE_FUNCTION_CALLS) && SOL_SAFE_FUNCTION_CALLS
  3636. true;
  3637. #else
  3638. false;
  3639. #endif
  3640. } // namespace detail
  3641. namespace meta {
  3642. namespace meta_detail {
  3643. }
  3644. } // namespace meta::meta_detail
  3645. namespace stack {
  3646. namespace stack_detail {
  3647. template <typename T>
  3648. struct undefined_metatable;
  3649. }
  3650. } // namespace stack::stack_detail
  3651. namespace usertype_detail {
  3652. template <typename T, typename Regs, typename Fx>
  3653. void insert_default_registrations(Regs& l, int& index, Fx&& fx);
  3654. template <typename T, typename Regs, meta::enable<meta::neg<std::is_pointer<T>>, std::is_destructible<T>> = meta::enabler>
  3655. void make_destructor(Regs& l, int& index);
  3656. template <typename T, typename Regs, meta::disable<meta::neg<std::is_pointer<T>>, std::is_destructible<T>> = meta::enabler>
  3657. void make_destructor(Regs& l, int& index);
  3658. } // namespace usertype_detail
  3659. } // namespace sol
  3660. // end of sol/forward_detail.hpp
  3661. // beginning of sol/raii.hpp
  3662. namespace sol {
  3663. namespace detail {
  3664. struct default_construct {
  3665. template <typename T, typename... Args>
  3666. static void construct(T&& obj, Args&&... args) {
  3667. typedef meta::unqualified_t<T> Tu;
  3668. std::allocator<Tu> alloc{};
  3669. std::allocator_traits<std::allocator<Tu>>::construct(alloc, std::forward<T>(obj), std::forward<Args>(args)...);
  3670. }
  3671. template <typename T, typename... Args>
  3672. void operator()(T&& obj, Args&&... args) const {
  3673. construct(std::forward<T>(obj), std::forward<Args>(args)...);
  3674. }
  3675. };
  3676. struct default_destruct {
  3677. template <typename T>
  3678. static void destroy(T&& obj) {
  3679. std::allocator<meta::unqualified_t<T>> alloc{};
  3680. alloc.destroy(obj);
  3681. }
  3682. template <typename T>
  3683. void operator()(T&& obj) const {
  3684. destroy(std::forward<T>(obj));
  3685. }
  3686. };
  3687. struct deleter {
  3688. template <typename T>
  3689. void operator()(T* p) const {
  3690. delete p;
  3691. }
  3692. };
  3693. struct state_deleter {
  3694. void operator()(lua_State* L) const {
  3695. lua_close(L);
  3696. }
  3697. };
  3698. template <typename T, typename Dx, typename... Args>
  3699. inline std::unique_ptr<T, Dx> make_unique_deleter(Args&&... args) {
  3700. return std::unique_ptr<T, Dx>(new T(std::forward<Args>(args)...));
  3701. }
  3702. template <typename Tag, typename T>
  3703. struct tagged {
  3704. T value;
  3705. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, tagged>> = meta::enabler>
  3706. tagged(Arg&& arg, Args&&... args)
  3707. : value(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  3708. }
  3709. };
  3710. } // namespace detail
  3711. template <typename... Args>
  3712. struct constructor_list {};
  3713. template <typename... Args>
  3714. using constructors = constructor_list<Args...>;
  3715. const auto default_constructor = constructors<types<>>{};
  3716. struct no_construction {};
  3717. const auto no_constructor = no_construction{};
  3718. struct call_construction {};
  3719. const auto call_constructor = call_construction{};
  3720. template <typename... Functions>
  3721. struct constructor_wrapper {
  3722. std::tuple<Functions...> functions;
  3723. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, constructor_wrapper>> = meta::enabler>
  3724. constructor_wrapper(Arg&& arg, Args&&... args)
  3725. : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  3726. }
  3727. };
  3728. template <typename... Functions>
  3729. inline auto initializers(Functions&&... functions) {
  3730. return constructor_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
  3731. }
  3732. template <typename... Functions>
  3733. struct factory_wrapper {
  3734. std::tuple<Functions...> functions;
  3735. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, factory_wrapper>> = meta::enabler>
  3736. factory_wrapper(Arg&& arg, Args&&... args)
  3737. : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  3738. }
  3739. };
  3740. template <typename... Functions>
  3741. inline auto factories(Functions&&... functions) {
  3742. return factory_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
  3743. }
  3744. template <typename Function>
  3745. struct destructor_wrapper {
  3746. Function fx;
  3747. destructor_wrapper(Function f)
  3748. : fx(std::move(f)) {
  3749. }
  3750. };
  3751. template <>
  3752. struct destructor_wrapper<void> {};
  3753. const destructor_wrapper<void> default_destructor{};
  3754. template <typename Fx>
  3755. inline auto destructor(Fx&& fx) {
  3756. return destructor_wrapper<std::decay_t<Fx>>(std::forward<Fx>(fx));
  3757. }
  3758. } // namespace sol
  3759. // end of sol/raii.hpp
  3760. // beginning of sol/filters.hpp
  3761. namespace sol {
  3762. namespace detail {
  3763. struct filter_base_tag {};
  3764. } // namespace detail
  3765. template <int Target, int... In>
  3766. struct static_stack_dependencies : detail::filter_base_tag {};
  3767. typedef static_stack_dependencies<-1, 1> self_dependency;
  3768. template <int... In>
  3769. struct returns_self_with : detail::filter_base_tag {};
  3770. typedef returns_self_with<> returns_self;
  3771. struct stack_dependencies : detail::filter_base_tag {
  3772. int target;
  3773. std::array<int, 64> stack_indices;
  3774. std::size_t len;
  3775. template <typename... Args>
  3776. stack_dependencies(int stack_target, Args&&... args)
  3777. : target(stack_target), stack_indices(), len(sizeof...(Args)) {
  3778. std::size_t i = 0;
  3779. (void)detail::swallow{int(), (stack_indices[i++] = static_cast<int>(std::forward<Args>(args)), int())...};
  3780. }
  3781. int& operator[](std::size_t i) {
  3782. return stack_indices[i];
  3783. }
  3784. const int& operator[](std::size_t i) const {
  3785. return stack_indices[i];
  3786. }
  3787. std::size_t size() const {
  3788. return len;
  3789. }
  3790. };
  3791. template <typename F, typename... Filters>
  3792. struct filter_wrapper {
  3793. typedef std::index_sequence_for<Filters...> indices;
  3794. F value;
  3795. std::tuple<Filters...> filters;
  3796. template <typename Fx, typename... Args, meta::enable<meta::neg<std::is_same<meta::unqualified_t<Fx>, filter_wrapper>>> = meta::enabler>
  3797. filter_wrapper(Fx&& fx, Args&&... args)
  3798. : value(std::forward<Fx>(fx)), filters(std::forward<Args>(args)...) {
  3799. }
  3800. filter_wrapper(const filter_wrapper&) = default;
  3801. filter_wrapper& operator=(const filter_wrapper&) = default;
  3802. filter_wrapper(filter_wrapper&&) = default;
  3803. filter_wrapper& operator=(filter_wrapper&&) = default;
  3804. };
  3805. template <typename F, typename... Args>
  3806. auto filters(F&& f, Args&&... args) {
  3807. return filter_wrapper<std::decay_t<F>, std::decay_t<Args>...>(std::forward<F>(f), std::forward<Args>(args)...);
  3808. }
  3809. } // namespace sol
  3810. // end of sol/filters.hpp
  3811. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  3812. #include <optional>
  3813. #ifdef SOL_STD_VARIANT
  3814. #include <variant>
  3815. #endif
  3816. #endif // C++17
  3817. #ifdef SOL_USE_BOOST
  3818. #include <boost/unordered_map.hpp>
  3819. #else
  3820. #include <unordered_map>
  3821. #endif // Using Boost
  3822. namespace sol {
  3823. namespace usertype_detail {
  3824. #if defined(SOL_USE_BOOST)
  3825. #if defined(SOL_CXX17_FEATURES)
  3826. template <typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<>>
  3827. using map_t = boost::unordered_map<K, V, H, E>;
  3828. #else
  3829. template <typename K, typename V, typename H = boost::hash<K>, typename E = std::equal_to<>>
  3830. using map_t = boost::unordered_map<K, V, H, E>;
  3831. #endif // C++17 or not, WITH boost
  3832. #else
  3833. template <typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<>>
  3834. using map_t = std::unordered_map<K, V, H, E>;
  3835. #endif // Boost map target
  3836. } // namespace usertype_detail
  3837. namespace detail {
  3838. #ifdef SOL_NOEXCEPT_FUNCTION_TYPE
  3839. typedef int (*lua_CFunction_noexcept)(lua_State* L) noexcept;
  3840. #else
  3841. typedef int (*lua_CFunction_noexcept)(lua_State* L);
  3842. #endif // noexcept function type for lua_CFunction
  3843. template <typename T>
  3844. struct unique_usertype {};
  3845. template <typename T>
  3846. struct implicit_wrapper {
  3847. T& item;
  3848. implicit_wrapper(T* item)
  3849. : item(*item) {
  3850. }
  3851. implicit_wrapper(T& item)
  3852. : item(item) {
  3853. }
  3854. operator T&() {
  3855. return item;
  3856. }
  3857. operator T*() {
  3858. return std::addressof(item);
  3859. }
  3860. };
  3861. struct unchecked_t {};
  3862. const unchecked_t unchecked = unchecked_t{};
  3863. struct yield_tag_t {};
  3864. const yield_tag_t yield_tag = yield_tag_t{};
  3865. } // namespace detail
  3866. struct lua_nil_t {};
  3867. const lua_nil_t lua_nil{};
  3868. inline bool operator==(lua_nil_t, lua_nil_t) {
  3869. return true;
  3870. }
  3871. inline bool operator!=(lua_nil_t, lua_nil_t) {
  3872. return false;
  3873. }
  3874. typedef lua_nil_t nil_t;
  3875. #if !defined(SOL_NO_NIL) || (SOL_NO_NIL == 0)
  3876. const nil_t nil{};
  3877. #endif
  3878. namespace detail {
  3879. struct non_lua_nil_t {};
  3880. } // namespace detail
  3881. struct metatable_t {};
  3882. const metatable_t metatable_key = {};
  3883. struct env_t {};
  3884. const env_t env_key = {};
  3885. struct no_metatable_t {};
  3886. const no_metatable_t no_metatable = {};
  3887. template <typename T>
  3888. struct yielding_t {
  3889. T func;
  3890. yielding_t() = default;
  3891. yielding_t(const yielding_t&) = default;
  3892. yielding_t(yielding_t&&) = default;
  3893. yielding_t& operator=(const yielding_t&) = default;
  3894. yielding_t& operator=(yielding_t&&) = default;
  3895. template <typename Arg, meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, yielding_t>>, meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  3896. yielding_t(Arg&& arg)
  3897. : func(std::forward<Arg>(arg)) {
  3898. }
  3899. template <typename Arg0, typename Arg1, typename... Args>
  3900. yielding_t(Arg0&& arg0, Arg1&& arg1, Args&&... args)
  3901. : func(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  3902. }
  3903. };
  3904. template <typename F>
  3905. inline yielding_t<std::decay_t<F>> yielding(F&& f) {
  3906. return yielding_t<std::decay_t<F>>(std::forward<F>(f));
  3907. }
  3908. typedef std::remove_pointer_t<lua_CFunction> lua_CFunction_ref;
  3909. template <typename T>
  3910. struct unique_usertype_traits {
  3911. typedef T type;
  3912. typedef T actual_type;
  3913. template <typename X>
  3914. using rebind_base = void;
  3915. static const bool value = false;
  3916. template <typename U>
  3917. static bool is_null(U&&) {
  3918. return false;
  3919. }
  3920. template <typename U>
  3921. static auto get(U&& value) {
  3922. return std::addressof(detail::deref(value));
  3923. }
  3924. };
  3925. template <typename T>
  3926. struct unique_usertype_traits<std::shared_ptr<T>> {
  3927. typedef T type;
  3928. typedef std::shared_ptr<T> actual_type;
  3929. // rebind is non-void
  3930. // if and only if unique usertype
  3931. // is cast-capable
  3932. template <typename X>
  3933. using rebind_base = std::shared_ptr<X>;
  3934. static const bool value = true;
  3935. static bool is_null(const actual_type& p) {
  3936. return p == nullptr;
  3937. }
  3938. static type* get(const actual_type& p) {
  3939. return p.get();
  3940. }
  3941. };
  3942. template <typename T, typename D>
  3943. struct unique_usertype_traits<std::unique_ptr<T, D>> {
  3944. typedef T type;
  3945. typedef std::unique_ptr<T, D> actual_type;
  3946. template <typename X>
  3947. using rebind_base = void;
  3948. static const bool value = true;
  3949. static bool is_null(const actual_type& p) {
  3950. return p == nullptr;
  3951. }
  3952. static type* get(const actual_type& p) {
  3953. return p.get();
  3954. }
  3955. };
  3956. template <typename T>
  3957. struct non_null {};
  3958. template <typename... Args>
  3959. struct function_sig {};
  3960. struct upvalue_index {
  3961. int index;
  3962. upvalue_index(int idx)
  3963. : index(lua_upvalueindex(idx)) {
  3964. }
  3965. operator int() const {
  3966. return index;
  3967. }
  3968. };
  3969. struct raw_index {
  3970. int index;
  3971. raw_index(int i)
  3972. : index(i) {
  3973. }
  3974. operator int() const {
  3975. return index;
  3976. }
  3977. };
  3978. struct absolute_index {
  3979. int index;
  3980. absolute_index(lua_State* L, int idx)
  3981. : index(lua_absindex(L, idx)) {
  3982. }
  3983. operator int() const {
  3984. return index;
  3985. }
  3986. };
  3987. struct ref_index {
  3988. int index;
  3989. ref_index(int idx)
  3990. : index(idx) {
  3991. }
  3992. operator int() const {
  3993. return index;
  3994. }
  3995. };
  3996. struct stack_count {
  3997. int count;
  3998. stack_count(int cnt)
  3999. : count(cnt) {
  4000. }
  4001. };
  4002. struct lightuserdata_value {
  4003. void* value;
  4004. lightuserdata_value(void* data)
  4005. : value(data) {
  4006. }
  4007. operator void*() const {
  4008. return value;
  4009. }
  4010. };
  4011. struct userdata_value {
  4012. void* value;
  4013. userdata_value(void* data)
  4014. : value(data) {
  4015. }
  4016. operator void*() const {
  4017. return value;
  4018. }
  4019. };
  4020. template <typename L>
  4021. struct light {
  4022. L* value;
  4023. light(L& x)
  4024. : value(std::addressof(x)) {
  4025. }
  4026. light(L* x)
  4027. : value(x) {
  4028. }
  4029. light(void* x)
  4030. : value(static_cast<L*>(x)) {
  4031. }
  4032. operator L*() const {
  4033. return value;
  4034. }
  4035. operator L&() const {
  4036. return *value;
  4037. }
  4038. };
  4039. template <typename T>
  4040. auto make_light(T& l) {
  4041. typedef meta::unwrapped_t<std::remove_pointer_t<std::remove_pointer_t<T>>> L;
  4042. return light<L>(l);
  4043. }
  4044. template <typename U>
  4045. struct user {
  4046. U value;
  4047. user(U&& x)
  4048. : value(std::forward<U>(x)) {
  4049. }
  4050. operator std::add_pointer_t<std::remove_reference_t<U>>() {
  4051. return std::addressof(value);
  4052. }
  4053. operator std::add_lvalue_reference_t<U>() {
  4054. return value;
  4055. }
  4056. operator std::add_const_t<std::add_lvalue_reference_t<U>>&() const {
  4057. return value;
  4058. }
  4059. };
  4060. template <typename T>
  4061. auto make_user(T&& u) {
  4062. typedef meta::unwrapped_t<meta::unqualified_t<T>> U;
  4063. return user<U>(std::forward<T>(u));
  4064. }
  4065. template <typename T>
  4066. struct metatable_registry_key {
  4067. T key;
  4068. metatable_registry_key(T key)
  4069. : key(std::forward<T>(key)) {
  4070. }
  4071. };
  4072. template <typename T>
  4073. auto meta_registry_key(T&& key) {
  4074. typedef meta::unqualified_t<T> K;
  4075. return metatable_registry_key<K>(std::forward<T>(key));
  4076. }
  4077. template <typename... Upvalues>
  4078. struct closure {
  4079. lua_CFunction c_function;
  4080. std::tuple<Upvalues...> upvalues;
  4081. closure(lua_CFunction f, Upvalues... targetupvalues)
  4082. : c_function(f), upvalues(std::forward<Upvalues>(targetupvalues)...) {
  4083. }
  4084. };
  4085. template <>
  4086. struct closure<> {
  4087. lua_CFunction c_function;
  4088. int upvalues;
  4089. closure(lua_CFunction f, int upvalue_count = 0)
  4090. : c_function(f), upvalues(upvalue_count) {
  4091. }
  4092. };
  4093. typedef closure<> c_closure;
  4094. template <typename... Args>
  4095. closure<Args...> make_closure(lua_CFunction f, Args&&... args) {
  4096. return closure<Args...>(f, std::forward<Args>(args)...);
  4097. }
  4098. template <typename Sig, typename... Ps>
  4099. struct function_arguments {
  4100. std::tuple<Ps...> arguments;
  4101. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, function_arguments>> = meta::enabler>
  4102. function_arguments(Arg&& arg, Args&&... args)
  4103. : arguments(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  4104. }
  4105. };
  4106. template <typename Sig = function_sig<>, typename... Args>
  4107. auto as_function(Args&&... args) {
  4108. return function_arguments<Sig, std::decay_t<Args>...>(std::forward<Args>(args)...);
  4109. }
  4110. template <typename Sig = function_sig<>, typename... Args>
  4111. auto as_function_reference(Args&&... args) {
  4112. return function_arguments<Sig, Args...>(std::forward<Args>(args)...);
  4113. }
  4114. template <typename T>
  4115. struct as_table_t {
  4116. T source;
  4117. as_table_t() = default;
  4118. as_table_t(const as_table_t&) = default;
  4119. as_table_t(as_table_t&&) = default;
  4120. as_table_t& operator=(const as_table_t&) = default;
  4121. as_table_t& operator=(as_table_t&&) = default;
  4122. template <typename Arg, meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, as_table_t>>, meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  4123. as_table_t(Arg&& arg)
  4124. : source(std::forward<Arg>(arg)) {
  4125. }
  4126. template <typename Arg0, typename Arg1, typename... Args>
  4127. as_table_t(Arg0&& arg0, Arg1&& arg1, Args&&... args)
  4128. : source(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  4129. }
  4130. operator std::add_lvalue_reference_t<T>() {
  4131. return source;
  4132. }
  4133. };
  4134. template <typename T>
  4135. struct nested {
  4136. T source;
  4137. nested() = default;
  4138. nested(const nested&) = default;
  4139. nested(nested&&) = default;
  4140. nested& operator=(const nested&) = default;
  4141. nested& operator=(nested&&) = default;
  4142. template <typename Arg, meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, nested>>, meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  4143. nested(Arg&& arg)
  4144. : source(std::forward<Arg>(arg)) {
  4145. }
  4146. template <typename Arg0, typename Arg1, typename... Args>
  4147. nested(Arg0&& arg0, Arg1&& arg1, Args&&... args)
  4148. : source(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  4149. }
  4150. operator std::add_lvalue_reference_t<T>() {
  4151. return source;
  4152. }
  4153. };
  4154. template <typename T>
  4155. as_table_t<T> as_table_ref(T&& container) {
  4156. return as_table_t<T>(std::forward<T>(container));
  4157. }
  4158. template <typename T>
  4159. as_table_t<meta::unqualified_t<T>> as_table(T&& container) {
  4160. return as_table_t<meta::unqualified_t<T>>(std::forward<T>(container));
  4161. }
  4162. template <typename T>
  4163. nested<T> as_nested_ref(T&& container) {
  4164. return nested<T>(std::forward<T>(container));
  4165. }
  4166. template <typename T>
  4167. nested<meta::unqualified_t<T>> as_nested(T&& container) {
  4168. return nested<meta::unqualified_t<T>>(std::forward<T>(container));
  4169. }
  4170. struct this_state {
  4171. lua_State* L;
  4172. this_state(lua_State* Ls)
  4173. : L(Ls) {
  4174. }
  4175. operator lua_State*() const noexcept {
  4176. return lua_state();
  4177. }
  4178. lua_State* operator->() const noexcept {
  4179. return lua_state();
  4180. }
  4181. lua_State* lua_state() const noexcept {
  4182. return L;
  4183. }
  4184. };
  4185. struct this_main_state {
  4186. lua_State* L;
  4187. this_main_state(lua_State* Ls)
  4188. : L(Ls) {
  4189. }
  4190. operator lua_State*() const noexcept {
  4191. return lua_state();
  4192. }
  4193. lua_State* operator->() const noexcept {
  4194. return lua_state();
  4195. }
  4196. lua_State* lua_state() const noexcept {
  4197. return L;
  4198. }
  4199. };
  4200. struct new_table {
  4201. int sequence_hint = 0;
  4202. int map_hint = 0;
  4203. new_table() = default;
  4204. new_table(const new_table&) = default;
  4205. new_table(new_table&&) = default;
  4206. new_table& operator=(const new_table&) = default;
  4207. new_table& operator=(new_table&&) = default;
  4208. new_table(int sequence_hint, int map_hint = 0)
  4209. : sequence_hint(sequence_hint), map_hint(map_hint) {
  4210. }
  4211. };
  4212. enum class lib : char {
  4213. // print, assert, and other base functions
  4214. base,
  4215. // require and other package functions
  4216. package,
  4217. // coroutine functions and utilities
  4218. coroutine,
  4219. // string library
  4220. string,
  4221. // functionality from the OS
  4222. os,
  4223. // all things math
  4224. math,
  4225. // the table manipulator and observer functions
  4226. table,
  4227. // the debug library
  4228. debug,
  4229. // the bit library: different based on which you're using
  4230. bit32,
  4231. // input/output library
  4232. io,
  4233. // LuaJIT only
  4234. ffi,
  4235. // LuaJIT only
  4236. jit,
  4237. // library for handling utf8: new to Lua
  4238. utf8,
  4239. // do not use
  4240. count
  4241. };
  4242. enum class call_syntax {
  4243. dot = 0,
  4244. colon = 1
  4245. };
  4246. enum class load_mode {
  4247. any = 0,
  4248. text = 1,
  4249. binary = 2,
  4250. };
  4251. enum class call_status : int {
  4252. ok = LUA_OK,
  4253. yielded = LUA_YIELD,
  4254. runtime = LUA_ERRRUN,
  4255. memory = LUA_ERRMEM,
  4256. handler = LUA_ERRERR,
  4257. gc = LUA_ERRGCMM,
  4258. syntax = LUA_ERRSYNTAX,
  4259. file = LUA_ERRFILE,
  4260. };
  4261. enum class thread_status : int {
  4262. ok = LUA_OK,
  4263. yielded = LUA_YIELD,
  4264. runtime = LUA_ERRRUN,
  4265. memory = LUA_ERRMEM,
  4266. gc = LUA_ERRGCMM,
  4267. handler = LUA_ERRERR,
  4268. dead = -1,
  4269. };
  4270. enum class load_status : int {
  4271. ok = LUA_OK,
  4272. syntax = LUA_ERRSYNTAX,
  4273. memory = LUA_ERRMEM,
  4274. gc = LUA_ERRGCMM,
  4275. file = LUA_ERRFILE,
  4276. };
  4277. enum class type : int {
  4278. none = LUA_TNONE,
  4279. lua_nil = LUA_TNIL,
  4280. #if !defined(SOL_NO_NIL)
  4281. nil = lua_nil,
  4282. #endif // Objective C/C++ Keyword that's found in OSX SDK and OBJC -- check for all forms to protect
  4283. string = LUA_TSTRING,
  4284. number = LUA_TNUMBER,
  4285. thread = LUA_TTHREAD,
  4286. boolean = LUA_TBOOLEAN,
  4287. function = LUA_TFUNCTION,
  4288. userdata = LUA_TUSERDATA,
  4289. lightuserdata = LUA_TLIGHTUSERDATA,
  4290. table = LUA_TTABLE,
  4291. poly = -0xFFFF
  4292. };
  4293. inline const std::string& to_string(call_status c) {
  4294. static const std::array<std::string, 10> names{ { "ok",
  4295. "yielded",
  4296. "runtime",
  4297. "memory",
  4298. "handler",
  4299. "gc",
  4300. "syntax",
  4301. "file",
  4302. "CRITICAL_EXCEPTION_FAILURE",
  4303. "CRITICAL_INDETERMINATE_STATE_FAILURE" } };
  4304. switch (c) {
  4305. case call_status::ok:
  4306. return names[0];
  4307. case call_status::yielded:
  4308. return names[1];
  4309. case call_status::runtime:
  4310. return names[2];
  4311. case call_status::memory:
  4312. return names[3];
  4313. case call_status::handler:
  4314. return names[4];
  4315. case call_status::gc:
  4316. return names[5];
  4317. case call_status::syntax:
  4318. return names[6];
  4319. case call_status::file:
  4320. return names[7];
  4321. }
  4322. if (static_cast<std::ptrdiff_t>(c) == -1) {
  4323. // One of the many cases where a critical exception error has occurred
  4324. return names[8];
  4325. }
  4326. return names[9];
  4327. }
  4328. inline bool is_indeterminate_call_failure(call_status c) {
  4329. switch (c) {
  4330. case call_status::ok:
  4331. case call_status::yielded:
  4332. case call_status::runtime:
  4333. case call_status::memory:
  4334. case call_status::handler:
  4335. case call_status::gc:
  4336. case call_status::syntax:
  4337. case call_status::file:
  4338. return false;
  4339. }
  4340. return true;
  4341. }
  4342. inline const std::string& to_string(load_status c) {
  4343. static const std::array<std::string, 7> names{ { "ok",
  4344. "memory",
  4345. "gc",
  4346. "syntax",
  4347. "file",
  4348. "CRITICAL_EXCEPTION_FAILURE",
  4349. "CRITICAL_INDETERMINATE_STATE_FAILURE" } };
  4350. switch (c) {
  4351. case load_status::ok:
  4352. return names[0];
  4353. case load_status::memory:
  4354. return names[1];
  4355. case load_status::gc:
  4356. return names[2];
  4357. case load_status::syntax:
  4358. return names[3];
  4359. case load_status::file:
  4360. return names[4];
  4361. }
  4362. if (static_cast<int>(c) == -1) {
  4363. // One of the many cases where a critical exception error has occurred
  4364. return names[5];
  4365. }
  4366. return names[6];
  4367. }
  4368. inline const std::string& to_string(load_mode c) {
  4369. static const std::array<std::string, 3> names{ {
  4370. "bt",
  4371. "t",
  4372. "b",
  4373. } };
  4374. return names[static_cast<std::size_t>(c)];
  4375. }
  4376. enum class meta_function {
  4377. construct,
  4378. index,
  4379. new_index,
  4380. mode,
  4381. call,
  4382. call_function = call,
  4383. metatable,
  4384. to_string,
  4385. length,
  4386. unary_minus,
  4387. addition,
  4388. subtraction,
  4389. multiplication,
  4390. division,
  4391. modulus,
  4392. power_of,
  4393. involution = power_of,
  4394. concatenation,
  4395. equal_to,
  4396. less_than,
  4397. less_than_or_equal_to,
  4398. garbage_collect,
  4399. floor_division,
  4400. bitwise_left_shift,
  4401. bitwise_right_shift,
  4402. bitwise_not,
  4403. bitwise_and,
  4404. bitwise_or,
  4405. bitwise_xor,
  4406. pairs,
  4407. ipairs,
  4408. next,
  4409. type,
  4410. type_info,
  4411. };
  4412. typedef meta_function meta_method;
  4413. inline const std::array<std::string, 32>& meta_function_names() {
  4414. static const std::array<std::string, 32> names = { { "new",
  4415. "__index",
  4416. "__newindex",
  4417. "__mode",
  4418. "__call",
  4419. "__mt",
  4420. "__tostring",
  4421. "__len",
  4422. "__unm",
  4423. "__add",
  4424. "__sub",
  4425. "__mul",
  4426. "__div",
  4427. "__mod",
  4428. "__pow",
  4429. "__concat",
  4430. "__eq",
  4431. "__lt",
  4432. "__le",
  4433. "__gc",
  4434. "__idiv",
  4435. "__shl",
  4436. "__shr",
  4437. "__bnot",
  4438. "__band",
  4439. "__bor",
  4440. "__bxor",
  4441. "__pairs",
  4442. "__ipairs",
  4443. "next",
  4444. "__type",
  4445. "__typeinfo" } };
  4446. return names;
  4447. }
  4448. inline const std::string& to_string(meta_function mf) {
  4449. return meta_function_names()[static_cast<int>(mf)];
  4450. }
  4451. inline type type_of(lua_State* L, int index) {
  4452. return static_cast<type>(lua_type(L, index));
  4453. }
  4454. inline std::string type_name(lua_State* L, type t) {
  4455. return lua_typename(L, static_cast<int>(t));
  4456. }
  4457. template <typename T>
  4458. struct is_lua_reference : std::integral_constant<bool,
  4459. std::is_base_of<reference, meta::unqualified_t<T>>::value
  4460. || std::is_base_of<main_reference, meta::unqualified_t<T>>::value
  4461. || std::is_base_of<stack_reference, meta::unqualified_t<T>>::value> {};
  4462. template <typename T>
  4463. struct is_lua_reference_or_proxy : std::integral_constant<bool,
  4464. is_lua_reference<meta::unqualified_t<T>>::value
  4465. || meta::is_specialization_of<meta::unqualified_t<T>, proxy>::value> {};
  4466. template <typename T>
  4467. struct is_transparent_argument : std::false_type {};
  4468. template <>
  4469. struct is_transparent_argument<this_state> : std::true_type {};
  4470. template <>
  4471. struct is_transparent_argument<this_main_state> : std::true_type {};
  4472. template <>
  4473. struct is_transparent_argument<this_environment> : std::true_type {};
  4474. template <>
  4475. struct is_transparent_argument<variadic_args> : std::true_type {};
  4476. template <typename T>
  4477. struct is_variadic_arguments : std::is_same<meta::unqualified_t<T>, variadic_args> {};
  4478. namespace detail {
  4479. template <typename T>
  4480. struct is_initializer_list : std::false_type {};
  4481. template <typename T>
  4482. struct is_initializer_list<std::initializer_list<T>> : std::true_type {};
  4483. template <typename T, typename C = void>
  4484. struct is_container : std::false_type {};
  4485. template <typename T>
  4486. struct is_container<std::initializer_list<T>> : std::false_type {};
  4487. template <typename T>
  4488. struct is_container<T, std::enable_if_t<meta::is_string_like<meta::unqualified_t<T>>::value>> : std::false_type {};
  4489. template <typename T>
  4490. struct is_container<T, std::enable_if_t<meta::all<std::is_array<meta::unqualified_t<T>>, meta::neg<meta::any_same<std::remove_all_extents_t<meta::unqualified_t<T>>, char, wchar_t, char16_t, char32_t>>>::value>> : std::true_type {};
  4491. template <typename T>
  4492. struct is_container<T, std::enable_if_t<meta::all<meta::has_begin_end<meta::unqualified_t<T>>, meta::neg<is_initializer_list<meta::unqualified_t<T>>>, meta::neg<meta::is_string_like<meta::unqualified_t<T>>>>::value>> : std::true_type {};
  4493. } // namespace detail
  4494. template <typename T>
  4495. struct is_container : detail::is_container<T> {};
  4496. template <typename T>
  4497. struct is_to_stringable : meta::any<meta::supports_to_string_member<meta::unqualified_t<T>>, meta::supports_adl_to_string<meta::unqualified_t<T>>, meta::supports_ostream_op<meta::unqualified_t<T>>> {};
  4498. namespace detail {
  4499. template <typename T, typename = void>
  4500. struct lua_type_of : std::integral_constant<type, type::userdata> {};
  4501. template <typename C, typename T, typename A>
  4502. struct lua_type_of<std::basic_string<C, T, A>> : std::integral_constant<type, type::string> {};
  4503. template <typename C, typename T>
  4504. struct lua_type_of<basic_string_view<C, T>> : std::integral_constant<type, type::string> {};
  4505. template <std::size_t N>
  4506. struct lua_type_of<char[N]> : std::integral_constant<type, type::string> {};
  4507. template <std::size_t N>
  4508. struct lua_type_of<wchar_t[N]> : std::integral_constant<type, type::string> {};
  4509. template <std::size_t N>
  4510. struct lua_type_of<char16_t[N]> : std::integral_constant<type, type::string> {};
  4511. template <std::size_t N>
  4512. struct lua_type_of<char32_t[N]> : std::integral_constant<type, type::string> {};
  4513. template <>
  4514. struct lua_type_of<char> : std::integral_constant<type, type::string> {};
  4515. template <>
  4516. struct lua_type_of<wchar_t> : std::integral_constant<type, type::string> {};
  4517. template <>
  4518. struct lua_type_of<char16_t> : std::integral_constant<type, type::string> {};
  4519. template <>
  4520. struct lua_type_of<char32_t> : std::integral_constant<type, type::string> {};
  4521. template <>
  4522. struct lua_type_of<const char*> : std::integral_constant<type, type::string> {};
  4523. template <>
  4524. struct lua_type_of<const char16_t*> : std::integral_constant<type, type::string> {};
  4525. template <>
  4526. struct lua_type_of<const char32_t*> : std::integral_constant<type, type::string> {};
  4527. template <>
  4528. struct lua_type_of<bool> : std::integral_constant<type, type::boolean> {};
  4529. template <>
  4530. struct lua_type_of<lua_nil_t> : std::integral_constant<type, type::lua_nil> {};
  4531. template <>
  4532. struct lua_type_of<nullopt_t> : std::integral_constant<type, type::lua_nil> {};
  4533. template <>
  4534. struct lua_type_of<detail::non_lua_nil_t> : std::integral_constant<type, type::poly> {};
  4535. template <>
  4536. struct lua_type_of<std::nullptr_t> : std::integral_constant<type, type::lua_nil> {};
  4537. template <>
  4538. struct lua_type_of<error> : std::integral_constant<type, type::string> {};
  4539. template <bool b, typename Base>
  4540. struct lua_type_of<basic_table_core<b, Base>> : std::integral_constant<type, type::table> {};
  4541. template <>
  4542. struct lua_type_of<metatable_t> : std::integral_constant<type, type::table> {};
  4543. template <typename B>
  4544. struct lua_type_of<basic_environment<B>> : std::integral_constant<type, type::poly> {};
  4545. template <>
  4546. struct lua_type_of<env_t> : std::integral_constant<type, type::poly> {};
  4547. template <>
  4548. struct lua_type_of<new_table> : std::integral_constant<type, type::table> {};
  4549. template <typename T>
  4550. struct lua_type_of<as_table_t<T>> : std::integral_constant<type, type::table> {};
  4551. template <typename T>
  4552. struct lua_type_of<std::initializer_list<T>> : std::integral_constant<type, type::table> {};
  4553. template <bool b>
  4554. struct lua_type_of<basic_reference<b>> : std::integral_constant<type, type::poly> {};
  4555. template <>
  4556. struct lua_type_of<stack_reference> : std::integral_constant<type, type::poly> {};
  4557. template <typename Base>
  4558. struct lua_type_of<basic_object<Base>> : std::integral_constant<type, type::poly> {};
  4559. template <typename... Args>
  4560. struct lua_type_of<std::tuple<Args...>> : std::integral_constant<type, type::poly> {};
  4561. template <typename A, typename B>
  4562. struct lua_type_of<std::pair<A, B>> : std::integral_constant<type, type::poly> {};
  4563. template <>
  4564. struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> {};
  4565. template <>
  4566. struct lua_type_of<const void*> : std::integral_constant<type, type::lightuserdata> {};
  4567. template <>
  4568. struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> {};
  4569. template <>
  4570. struct lua_type_of<userdata_value> : std::integral_constant<type, type::userdata> {};
  4571. template <typename T>
  4572. struct lua_type_of<light<T>> : std::integral_constant<type, type::lightuserdata> {};
  4573. template <typename T>
  4574. struct lua_type_of<user<T>> : std::integral_constant<type, type::userdata> {};
  4575. template <typename Base>
  4576. struct lua_type_of<basic_lightuserdata<Base>> : std::integral_constant<type, type::lightuserdata> {};
  4577. template <typename Base>
  4578. struct lua_type_of<basic_userdata<Base>> : std::integral_constant<type, type::userdata> {};
  4579. template <>
  4580. struct lua_type_of<lua_CFunction> : std::integral_constant<type, type::function> {};
  4581. template <>
  4582. struct lua_type_of<std::remove_pointer_t<lua_CFunction>> : std::integral_constant<type, type::function> {};
  4583. template <typename Base, bool aligned>
  4584. struct lua_type_of<basic_function<Base, aligned>> : std::integral_constant<type, type::function> {};
  4585. template <typename Base, bool aligned, typename Handler>
  4586. struct lua_type_of<basic_protected_function<Base, aligned, Handler>> : std::integral_constant<type, type::function> {};
  4587. template <typename Base>
  4588. struct lua_type_of<basic_coroutine<Base>> : std::integral_constant<type, type::function> {};
  4589. template <typename Base>
  4590. struct lua_type_of<basic_thread<Base>> : std::integral_constant<type, type::thread> {};
  4591. template <typename Signature>
  4592. struct lua_type_of<std::function<Signature>> : std::integral_constant<type, type::function> {};
  4593. template <typename T>
  4594. struct lua_type_of<optional<T>> : std::integral_constant<type, type::poly> {};
  4595. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  4596. template <typename T>
  4597. struct lua_type_of<std::optional<T>> : std::integral_constant<type, type::poly> {};
  4598. #endif // std::optional
  4599. template <>
  4600. struct lua_type_of<variadic_args> : std::integral_constant<type, type::poly> {};
  4601. template <>
  4602. struct lua_type_of<variadic_results> : std::integral_constant<type, type::poly> {};
  4603. template <>
  4604. struct lua_type_of<stack_count> : std::integral_constant<type, type::poly> {};
  4605. template <>
  4606. struct lua_type_of<this_state> : std::integral_constant<type, type::poly> {};
  4607. template <>
  4608. struct lua_type_of<this_main_state> : std::integral_constant<type, type::poly> {};
  4609. template <>
  4610. struct lua_type_of<this_environment> : std::integral_constant<type, type::poly> {};
  4611. template <>
  4612. struct lua_type_of<type> : std::integral_constant<type, type::poly> {};
  4613. template <typename T>
  4614. struct lua_type_of<T*> : std::integral_constant<type, type::userdata> {};
  4615. template <typename T>
  4616. struct lua_type_of<T, std::enable_if_t<std::is_arithmetic<T>::value>> : std::integral_constant<type, type::number> {};
  4617. template <typename T>
  4618. struct lua_type_of<T, std::enable_if_t<std::is_enum<T>::value>> : std::integral_constant<type, type::number> {};
  4619. template <>
  4620. struct lua_type_of<meta_function> : std::integral_constant<type, type::string> {};
  4621. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  4622. #ifdef SOL_STD_VARIANT
  4623. template <typename... Tn>
  4624. struct lua_type_of<std::variant<Tn...>> : std::integral_constant<type, type::poly> {};
  4625. #endif // SOL_STD_VARIANT
  4626. #endif // SOL_CXX17_FEATURES
  4627. template <typename T>
  4628. struct lua_type_of<nested<T>, std::enable_if_t<::sol::is_container<T>::value>> : std::integral_constant<type, type::table> {};
  4629. template <typename T>
  4630. struct lua_type_of<nested<T>, std::enable_if_t<!::sol::is_container<T>::value>> : lua_type_of<T> {};
  4631. template <typename C, C v, template <typename...> class V, typename... Args>
  4632. struct accumulate : std::integral_constant<C, v> {};
  4633. template <typename C, C v, template <typename...> class V, typename T, typename... Args>
  4634. struct accumulate<C, v, V, T, Args...> : accumulate<C, v + V<T>::value, V, Args...> {};
  4635. template <typename C, C v, template <typename...> class V, typename List>
  4636. struct accumulate_list;
  4637. template <typename C, C v, template <typename...> class V, typename... Args>
  4638. struct accumulate_list<C, v, V, types<Args...>> : accumulate<C, v, V, Args...> {};
  4639. } // namespace detail
  4640. template <typename T>
  4641. struct is_unique_usertype : std::integral_constant<bool, unique_usertype_traits<T>::value> {};
  4642. template <typename T>
  4643. struct lua_type_of : detail::lua_type_of<T> {
  4644. typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
  4645. };
  4646. template <typename T>
  4647. struct lua_size : std::integral_constant<int, 1> {
  4648. typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
  4649. };
  4650. template <typename A, typename B>
  4651. struct lua_size<std::pair<A, B>> : std::integral_constant<int, lua_size<A>::value + lua_size<B>::value> {};
  4652. template <typename... Args>
  4653. struct lua_size<std::tuple<Args...>> : std::integral_constant<int, detail::accumulate<int, 0, lua_size, Args...>::value> {};
  4654. namespace detail {
  4655. template <typename...>
  4656. struct void_ { typedef void type; };
  4657. template <typename T, typename = void>
  4658. struct has_internal_marker_impl : std::false_type {};
  4659. template <typename T>
  4660. struct has_internal_marker_impl<T, typename void_<typename T::SOL_INTERNAL_UNSPECIALIZED_MARKER_>::type> : std::true_type {};
  4661. template <typename T>
  4662. struct has_internal_marker : has_internal_marker_impl<T> {};
  4663. } // namespace detail
  4664. template <typename T>
  4665. struct is_lua_primitive : std::integral_constant<bool,
  4666. type::userdata != lua_type_of<meta::unqualified_t<T>>::value
  4667. || ((type::userdata == lua_type_of<meta::unqualified_t<T>>::value)
  4668. && detail::has_internal_marker<lua_type_of<meta::unqualified_t<T>>>::value
  4669. && !detail::has_internal_marker<lua_size<meta::unqualified_t<T>>>::value)
  4670. || is_lua_reference<meta::unqualified_t<T>>::value
  4671. || meta::is_specialization_of<meta::unqualified_t<T>, std::tuple>::value
  4672. || meta::is_specialization_of<meta::unqualified_t<T>, std::pair>::value> {};
  4673. template <typename T>
  4674. struct is_main_threaded : std::is_base_of<main_reference, T> {};
  4675. template <typename T>
  4676. struct is_stack_based : std::is_base_of<stack_reference, T> {};
  4677. template <>
  4678. struct is_stack_based<variadic_args> : std::true_type {};
  4679. template <>
  4680. struct is_stack_based<unsafe_function_result> : std::true_type {};
  4681. template <>
  4682. struct is_stack_based<protected_function_result> : std::true_type {};
  4683. template <>
  4684. struct is_stack_based<stack_proxy> : std::true_type {};
  4685. template <>
  4686. struct is_stack_based<stack_proxy_base> : std::true_type {};
  4687. template <typename T>
  4688. struct is_lua_primitive<T*> : std::true_type {};
  4689. template <>
  4690. struct is_lua_primitive<unsafe_function_result> : std::true_type {};
  4691. template <>
  4692. struct is_lua_primitive<protected_function_result> : std::true_type {};
  4693. template <typename T>
  4694. struct is_lua_primitive<std::reference_wrapper<T>> : std::true_type {};
  4695. template <typename T>
  4696. struct is_lua_primitive<user<T>> : std::true_type {};
  4697. template <typename T>
  4698. struct is_lua_primitive<light<T>> : is_lua_primitive<T*> {};
  4699. template <typename T>
  4700. struct is_lua_primitive<optional<T>> : std::true_type {};
  4701. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  4702. template <typename T>
  4703. struct is_lua_primitive<std::optional<T>> : std::true_type {};
  4704. #endif
  4705. template <typename T>
  4706. struct is_lua_primitive<as_table_t<T>> : std::true_type {};
  4707. template <typename T>
  4708. struct is_lua_primitive<nested<T>> : std::true_type {};
  4709. template <>
  4710. struct is_lua_primitive<userdata_value> : std::true_type {};
  4711. template <>
  4712. struct is_lua_primitive<lightuserdata_value> : std::true_type {};
  4713. template <typename T>
  4714. struct is_lua_primitive<non_null<T>> : is_lua_primitive<T*> {};
  4715. template <typename T>
  4716. struct is_proxy_primitive : is_lua_primitive<T> {};
  4717. template <typename T>
  4718. struct is_lua_index : std::is_integral<T> {};
  4719. template <>
  4720. struct is_lua_index<raw_index> : std::true_type {};
  4721. template <>
  4722. struct is_lua_index<absolute_index> : std::true_type {};
  4723. template <>
  4724. struct is_lua_index<ref_index> : std::true_type {};
  4725. template <>
  4726. struct is_lua_index<upvalue_index> : std::true_type {};
  4727. template <typename Signature>
  4728. struct lua_bind_traits : meta::bind_traits<Signature> {
  4729. private:
  4730. typedef meta::bind_traits<Signature> base_t;
  4731. public:
  4732. typedef std::integral_constant<bool, meta::count_for<is_variadic_arguments, typename base_t::args_list>::value != 0> runtime_variadics_t;
  4733. static const std::size_t true_arity = base_t::arity;
  4734. static const std::size_t arity = detail::accumulate_list<std::size_t, 0, lua_size, typename base_t::args_list>::value - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
  4735. static const std::size_t true_free_arity = base_t::free_arity;
  4736. static const std::size_t free_arity = detail::accumulate_list<std::size_t, 0, lua_size, typename base_t::free_args_list>::value - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
  4737. };
  4738. template <typename T>
  4739. struct is_table : std::false_type {};
  4740. template <bool x, typename T>
  4741. struct is_table<basic_table_core<x, T>> : std::true_type {};
  4742. template <typename T>
  4743. struct is_function : std::false_type {};
  4744. template <typename T, bool aligned>
  4745. struct is_function<basic_function<T, aligned>> : std::true_type {};
  4746. template <typename T, bool aligned, typename Handler>
  4747. struct is_function<basic_protected_function<T, aligned, Handler>> : std::true_type {};
  4748. template <typename T>
  4749. struct is_lightuserdata : std::false_type {};
  4750. template <typename T>
  4751. struct is_lightuserdata<basic_lightuserdata<T>> : std::true_type {};
  4752. template <typename T>
  4753. struct is_userdata : std::false_type {};
  4754. template <typename T>
  4755. struct is_userdata<basic_userdata<T>> : std::true_type {};
  4756. template <typename T>
  4757. struct is_environment : std::integral_constant<bool, is_userdata<T>::value || is_table<T>::value> {};
  4758. template <typename T>
  4759. struct is_automagical : meta::neg<std::is_array<meta::unqualified_t<T>>> {};
  4760. template <typename T>
  4761. inline type type_of() {
  4762. return lua_type_of<meta::unqualified_t<T>>::value;
  4763. }
  4764. namespace detail {
  4765. template <typename T>
  4766. struct is_non_factory_constructor : std::false_type {};
  4767. template <typename... Args>
  4768. struct is_non_factory_constructor<constructors<Args...>> : std::true_type {};
  4769. template <typename... Args>
  4770. struct is_non_factory_constructor<constructor_wrapper<Args...>> : std::true_type {};
  4771. template <>
  4772. struct is_non_factory_constructor<no_construction> : std::true_type {};
  4773. template <typename T>
  4774. struct is_constructor : is_non_factory_constructor<T> {};
  4775. template <typename... Args>
  4776. struct is_constructor<factory_wrapper<Args...>> : std::true_type {};
  4777. template <typename T>
  4778. struct is_constructor<protect_t<T>> : is_constructor<meta::unqualified_t<T>> {};
  4779. template <typename F, typename... Filters>
  4780. struct is_constructor<filter_wrapper<F, Filters...>> : is_constructor<meta::unqualified_t<F>> {};
  4781. template <typename... Args>
  4782. using has_constructor = meta::any<is_constructor<meta::unqualified_t<Args>>...>;
  4783. template <typename T>
  4784. struct is_destructor : std::false_type {};
  4785. template <typename Fx>
  4786. struct is_destructor<destructor_wrapper<Fx>> : std::true_type {};
  4787. template <typename... Args>
  4788. using has_destructor = meta::any<is_destructor<meta::unqualified_t<Args>>...>;
  4789. struct add_destructor_tag {};
  4790. struct check_destructor_tag {};
  4791. struct verified_tag {
  4792. } const verified{};
  4793. } // namespace detail
  4794. } // namespace sol
  4795. // end of sol/types.hpp
  4796. #include <exception>
  4797. #include <cstring>
  4798. #if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS
  4799. #include <iostream>
  4800. #endif
  4801. namespace sol {
  4802. // must push a single object to be the error object
  4803. // NOTE: the VAST MAJORITY of all Lua libraries -- C or otherwise -- expect a string for the type of error
  4804. // break this convention at your own risk
  4805. using exception_handler_function = int(*)(lua_State*, optional<const std::exception&>, string_view);
  4806. namespace detail {
  4807. inline const char(&default_exception_handler_name())[11]{
  4808. static const char name[11] = "sol.\xE2\x98\xA2\xE2\x98\xA2";
  4809. return name;
  4810. }
  4811. // must push at least 1 object on the stack
  4812. inline int default_exception_handler(lua_State* L, optional<const std::exception&>, string_view what) {
  4813. #if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS
  4814. std::cerr << "[sol2] An exception occurred: ";
  4815. std::cerr.write(what.data(), what.size());
  4816. std::cerr << std::endl;
  4817. #endif
  4818. lua_pushlstring(L, what.data(), what.size());
  4819. return 1;
  4820. }
  4821. inline int call_exception_handler(lua_State* L, optional<const std::exception&> maybe_ex, string_view what) {
  4822. lua_getglobal(L, default_exception_handler_name());
  4823. type t = static_cast<type>(lua_type(L, -1));
  4824. if (t != type::lightuserdata) {
  4825. lua_pop(L, 1);
  4826. return default_exception_handler(L, std::move(maybe_ex), std::move(what));
  4827. }
  4828. void* vfunc = lua_touserdata(L, -1);
  4829. lua_pop(L, 1);
  4830. if (vfunc == nullptr) {
  4831. return default_exception_handler(L, std::move(maybe_ex), std::move(what));
  4832. }
  4833. exception_handler_function exfunc = reinterpret_cast<exception_handler_function>(vfunc);
  4834. return exfunc(L, std::move(maybe_ex), std::move(what));
  4835. }
  4836. #ifdef SOL_NO_EXCEPTIONS
  4837. template <lua_CFunction f>
  4838. int static_trampoline(lua_State* L) noexcept {
  4839. return f(L);
  4840. }
  4841. #ifdef SOL_NOEXCEPT_FUNCTION_TYPE
  4842. template <lua_CFunction_noexcept f>
  4843. int static_trampoline_noexcept(lua_State* L) noexcept {
  4844. return f(L);
  4845. }
  4846. #else
  4847. template <lua_CFunction f>
  4848. int static_trampoline_noexcept(lua_State* L) noexcept {
  4849. return f(L);
  4850. }
  4851. #endif
  4852. template <typename Fx, typename... Args>
  4853. int trampoline(lua_State* L, Fx&& f, Args&&... args) noexcept {
  4854. return f(L, std::forward<Args>(args)...);
  4855. }
  4856. inline int c_trampoline(lua_State* L, lua_CFunction f) noexcept {
  4857. return trampoline(L, f);
  4858. }
  4859. #else
  4860. template <lua_CFunction f>
  4861. int static_trampoline(lua_State* L) {
  4862. #if defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) && !defined(SOL_LUAJIT)
  4863. return f(L);
  4864. #else
  4865. try {
  4866. return f(L);
  4867. }
  4868. catch (const char* cs) {
  4869. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
  4870. }
  4871. catch (const std::string& s) {
  4872. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
  4873. }
  4874. catch (const std::exception& e) {
  4875. call_exception_handler(L, optional<const std::exception&>(e), e.what());
  4876. }
  4877. #if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION)
  4878. // LuaJIT cannot have the catchall when the safe propagation is on
  4879. // but LuaJIT will swallow all C++ errors
  4880. // if we don't at least catch std::exception ones
  4881. catch (...) {
  4882. call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
  4883. }
  4884. #endif // LuaJIT cannot have the catchall, but we must catch std::exceps for it
  4885. return lua_error(L);
  4886. #endif // Safe exceptions
  4887. }
  4888. #ifdef SOL_NOEXCEPT_FUNCTION_TYPE
  4889. #if 0
  4890. // impossible: g++/clang++ choke as they think this function is ambiguous:
  4891. // to fix, wait for template <auto X> and then switch on no-exceptness of the function
  4892. template <lua_CFunction_noexcept f>
  4893. int static_trampoline(lua_State* L) noexcept {
  4894. return f(L);
  4895. }
  4896. #else
  4897. template <lua_CFunction_noexcept f>
  4898. int static_trampoline_noexcept(lua_State* L) noexcept {
  4899. return f(L);
  4900. }
  4901. #endif // impossible
  4902. #else
  4903. template <lua_CFunction f>
  4904. int static_trampoline_noexcept(lua_State* L) noexcept {
  4905. return f(L);
  4906. }
  4907. #endif // noexcept lua_CFunction type
  4908. template <typename Fx, typename... Args>
  4909. int trampoline(lua_State* L, Fx&& f, Args&&... args) {
  4910. if (meta::bind_traits<meta::unqualified_t<Fx>>::is_noexcept) {
  4911. return f(L, std::forward<Args>(args)...);
  4912. }
  4913. #if defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) && !defined(SOL_LUAJIT)
  4914. return f(L, std::forward<Args>(args)...);
  4915. #else
  4916. try {
  4917. return f(L, std::forward<Args>(args)...);
  4918. }
  4919. catch (const char* cs) {
  4920. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
  4921. }
  4922. catch (const std::string& s) {
  4923. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
  4924. }
  4925. catch (const std::exception& e) {
  4926. call_exception_handler(L, optional<const std::exception&>(e), e.what());
  4927. }
  4928. #if !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION)
  4929. // LuaJIT cannot have the catchall when the safe propagation is on
  4930. // but LuaJIT will swallow all C++ errors
  4931. // if we don't at least catch std::exception ones
  4932. catch (...) {
  4933. call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
  4934. }
  4935. #endif
  4936. return lua_error(L);
  4937. #endif
  4938. }
  4939. inline int c_trampoline(lua_State* L, lua_CFunction f) {
  4940. return trampoline(L, f);
  4941. }
  4942. #endif // Exceptions vs. No Exceptions
  4943. template <typename F, F fx>
  4944. inline int typed_static_trampoline_raw(std::true_type, lua_State* L) {
  4945. return static_trampoline_noexcept<fx>(L);
  4946. }
  4947. template <typename F, F fx>
  4948. inline int typed_static_trampoline_raw(std::false_type, lua_State* L) {
  4949. return static_trampoline<fx>(L);
  4950. }
  4951. template <typename F, F fx>
  4952. inline int typed_static_trampoline(lua_State* L) {
  4953. return typed_static_trampoline_raw<F, fx>(std::integral_constant<bool, meta::bind_traits<F>::is_noexcept>(), L);
  4954. }
  4955. } // namespace detail
  4956. inline void set_default_exception_handler(lua_State* L, exception_handler_function exf = &detail::default_exception_handler) {
  4957. static_assert(sizeof(void*) >= sizeof(exception_handler_function), "void* storage is too small to transport the exception handler: please file a bug on the issue tracker");
  4958. void* storage;
  4959. std::memcpy(&storage, &exf, sizeof(exception_handler_function));
  4960. lua_pushlightuserdata(L, storage);
  4961. lua_setglobal(L, detail::default_exception_handler_name());
  4962. }
  4963. } // sol
  4964. // end of sol/trampoline.hpp
  4965. // beginning of sol/stack_core.hpp
  4966. // beginning of sol/inheritance.hpp
  4967. // beginning of sol/usertype_traits.hpp
  4968. // beginning of sol/demangle.hpp
  4969. #include <cctype>
  4970. #if defined(__GNUC__) && defined(__MINGW32__) && (__GNUC__ < 6)
  4971. extern "C" {
  4972. }
  4973. #endif // MinGW is on some stuff
  4974. #include <locale>
  4975. namespace sol {
  4976. namespace detail {
  4977. #if defined(__GNUC__) || defined(__clang__)
  4978. template <typename T, class seperator_mark = int>
  4979. inline std::string ctti_get_type_name() {
  4980. // cardinal sins from MINGW
  4981. using namespace std;
  4982. static const std::array<std::string, 2> removals = {{"{anonymous}", "(anonymous namespace)"}};
  4983. std::string name = __PRETTY_FUNCTION__;
  4984. std::size_t start = name.find_first_of('[');
  4985. start = name.find_first_of('=', start);
  4986. std::size_t end = name.find_last_of(']');
  4987. if (end == std::string::npos)
  4988. end = name.size();
  4989. if (start == std::string::npos)
  4990. start = 0;
  4991. if (start < name.size() - 1)
  4992. start += 1;
  4993. name = name.substr(start, end - start);
  4994. start = name.rfind("seperator_mark");
  4995. if (start != std::string::npos) {
  4996. name.erase(start - 2, name.length());
  4997. }
  4998. while (!name.empty() && isblank(name.front()))
  4999. name.erase(name.begin());
  5000. while (!name.empty() && isblank(name.back()))
  5001. name.pop_back();
  5002. for (std::size_t r = 0; r < removals.size(); ++r) {
  5003. auto found = name.find(removals[r]);
  5004. while (found != std::string::npos) {
  5005. name.erase(found, removals[r].size());
  5006. found = name.find(removals[r]);
  5007. }
  5008. }
  5009. return name;
  5010. }
  5011. #elif defined(_MSC_VER)
  5012. template <typename T>
  5013. inline std::string ctti_get_type_name() {
  5014. static const std::array<std::string, 7> removals = {{"public:", "private:", "protected:", "struct ", "class ", "`anonymous-namespace'", "`anonymous namespace'"}};
  5015. std::string name = __FUNCSIG__;
  5016. std::size_t start = name.find("get_type_name");
  5017. if (start == std::string::npos)
  5018. start = 0;
  5019. else
  5020. start += 13;
  5021. if (start < name.size() - 1)
  5022. start += 1;
  5023. std::size_t end = name.find_last_of('>');
  5024. if (end == std::string::npos)
  5025. end = name.size();
  5026. name = name.substr(start, end - start);
  5027. if (name.find("struct", 0) == 0)
  5028. name.replace(0, 6, "", 0);
  5029. if (name.find("class", 0) == 0)
  5030. name.replace(0, 5, "", 0);
  5031. while (!name.empty() && isblank(name.front()))
  5032. name.erase(name.begin());
  5033. while (!name.empty() && isblank(name.back()))
  5034. name.pop_back();
  5035. for (std::size_t r = 0; r < removals.size(); ++r) {
  5036. auto found = name.find(removals[r]);
  5037. while (found != std::string::npos) {
  5038. name.erase(found, removals[r].size());
  5039. found = name.find(removals[r]);
  5040. }
  5041. }
  5042. return name;
  5043. }
  5044. #else
  5045. #error Compiler not supported for demangling
  5046. #endif // compilers
  5047. template <typename T>
  5048. inline std::string demangle_once() {
  5049. std::string realname = ctti_get_type_name<T>();
  5050. return realname;
  5051. }
  5052. template <typename T>
  5053. inline std::string short_demangle_once() {
  5054. std::string realname = ctti_get_type_name<T>();
  5055. // This isn't the most complete but it'll do for now...?
  5056. static const std::array<std::string, 10> ops = {{"operator<", "operator<<", "operator<<=", "operator<=", "operator>", "operator>>", "operator>>=", "operator>=", "operator->", "operator->*"}};
  5057. int level = 0;
  5058. std::ptrdiff_t idx = 0;
  5059. for (idx = static_cast<std::ptrdiff_t>(realname.empty() ? 0 : realname.size() - 1); idx > 0; --idx) {
  5060. if (level == 0 && realname[idx] == ':') {
  5061. break;
  5062. }
  5063. bool isleft = realname[idx] == '<';
  5064. bool isright = realname[idx] == '>';
  5065. if (!isleft && !isright)
  5066. continue;
  5067. bool earlybreak = false;
  5068. for (const auto& op : ops) {
  5069. std::size_t nisop = realname.rfind(op, idx);
  5070. if (nisop == std::string::npos)
  5071. continue;
  5072. std::size_t nisopidx = idx - op.size() + 1;
  5073. if (nisop == nisopidx) {
  5074. idx = static_cast<std::ptrdiff_t>(nisopidx);
  5075. earlybreak = true;
  5076. }
  5077. break;
  5078. }
  5079. if (earlybreak) {
  5080. continue;
  5081. }
  5082. level += isleft ? -1 : 1;
  5083. }
  5084. if (idx > 0) {
  5085. realname.erase(0, realname.length() < static_cast<std::size_t>(idx) ? realname.length() : idx + 1);
  5086. }
  5087. return realname;
  5088. }
  5089. template <typename T>
  5090. inline const std::string& demangle() {
  5091. static const std::string d = demangle_once<T>();
  5092. return d;
  5093. }
  5094. template <typename T>
  5095. inline const std::string& short_demangle() {
  5096. static const std::string d = short_demangle_once<T>();
  5097. return d;
  5098. }
  5099. }
  5100. } // namespace sol::detail
  5101. // end of sol/demangle.hpp
  5102. namespace sol {
  5103. template <typename T>
  5104. struct usertype_traits {
  5105. static const std::string& name() {
  5106. static const std::string& n = detail::short_demangle<T>();
  5107. return n;
  5108. }
  5109. static const std::string& qualified_name() {
  5110. static const std::string& q_n = detail::demangle<T>();
  5111. return q_n;
  5112. }
  5113. static const std::string& metatable() {
  5114. static const std::string m = std::string("sol.").append(detail::demangle<T>());
  5115. return m;
  5116. }
  5117. static const std::string& user_metatable() {
  5118. static const std::string u_m = std::string("sol.").append(detail::demangle<T>()).append(".user");
  5119. return u_m;
  5120. }
  5121. static const std::string& user_gc_metatable() {
  5122. static const std::string u_g_m = std::string("sol.").append(detail::demangle<T>()).append(".user\xE2\x99\xBB");
  5123. return u_g_m;
  5124. }
  5125. static const std::string& gc_table() {
  5126. static const std::string g_t = std::string("sol.").append(detail::demangle<T>()).append(".\xE2\x99\xBB");
  5127. return g_t;
  5128. }
  5129. };
  5130. } // namespace sol
  5131. // end of sol/usertype_traits.hpp
  5132. namespace sol {
  5133. template <typename... Args>
  5134. struct base_list {};
  5135. template <typename... Args>
  5136. using bases = base_list<Args...>;
  5137. typedef bases<> base_classes_tag;
  5138. const auto base_classes = base_classes_tag();
  5139. namespace detail {
  5140. template <typename T>
  5141. struct has_derived {
  5142. static bool value;
  5143. };
  5144. template <typename T>
  5145. bool has_derived<T>::value = false;
  5146. inline decltype(auto) base_class_check_key() {
  5147. static const auto& key = "class_check";
  5148. return key;
  5149. }
  5150. inline decltype(auto) base_class_cast_key() {
  5151. static const auto& key = "class_cast";
  5152. return key;
  5153. }
  5154. inline decltype(auto) base_class_index_propogation_key() {
  5155. static const auto& key = u8"\xF0\x9F\x8C\xB2.index";
  5156. return key;
  5157. }
  5158. inline decltype(auto) base_class_new_index_propogation_key() {
  5159. static const auto& key = u8"\xF0\x9F\x8C\xB2.new_index";
  5160. return key;
  5161. }
  5162. template <typename T, typename... Bases>
  5163. struct inheritance {
  5164. static bool type_check_bases(types<>, const std::string&) {
  5165. return false;
  5166. }
  5167. template <typename Base, typename... Args>
  5168. static bool type_check_bases(types<Base, Args...>, const std::string& ti) {
  5169. return ti == usertype_traits<Base>::qualified_name() || type_check_bases(types<Args...>(), ti);
  5170. }
  5171. static bool type_check(const std::string& ti) {
  5172. return ti == usertype_traits<T>::qualified_name() || type_check_bases(types<Bases...>(), ti);
  5173. }
  5174. static void* type_cast_bases(types<>, T*, const std::string&) {
  5175. return nullptr;
  5176. }
  5177. template <typename Base, typename... Args>
  5178. static void* type_cast_bases(types<Base, Args...>, T* data, const std::string& ti) {
  5179. // Make sure to convert to T first, and then dynamic cast to the proper type
  5180. return ti != usertype_traits<Base>::qualified_name() ? type_cast_bases(types<Args...>(), data, ti) : static_cast<void*>(static_cast<Base*>(data));
  5181. }
  5182. static void* type_cast(void* voiddata, const std::string& ti) {
  5183. T* data = static_cast<T*>(voiddata);
  5184. return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(types<Bases...>(), data, ti) : data);
  5185. }
  5186. template <typename U>
  5187. static bool type_unique_cast_bases(void*, void*, const string_view&) {
  5188. return false;
  5189. }
  5190. template <typename U, typename Base, typename... Args>
  5191. static bool type_unique_cast_bases(void* source_data, void* target_data, const string_view& ti) {
  5192. typedef unique_usertype_traits<U> uu_traits;
  5193. typedef typename uu_traits::template rebind_base<Base> base_ptr;
  5194. string_view base_ti = usertype_traits<Base>::qualified_name();
  5195. if (base_ti == ti) {
  5196. if (target_data != nullptr) {
  5197. U* source = static_cast<U*>(source_data);
  5198. base_ptr* target = static_cast<base_ptr*>(target_data);
  5199. // perform proper derived -> base conversion
  5200. *target = *source;
  5201. }
  5202. return true;
  5203. }
  5204. return type_unique_cast_bases<U, Args...>(source_data, target_data, ti);
  5205. }
  5206. template <typename U>
  5207. static bool type_unique_cast(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
  5208. typedef unique_usertype_traits<U> uu_traits;
  5209. typedef typename uu_traits::template rebind_base<T> rebind_t;
  5210. string_view this_rebind_ti = usertype_traits<rebind_t>::qualified_name();
  5211. if (rebind_ti != this_rebind_ti) {
  5212. // this is not even of the same container type
  5213. return false;
  5214. }
  5215. return type_unique_cast_bases<U, Bases...>(source_data, target_data, ti);
  5216. }
  5217. };
  5218. using inheritance_check_function = decltype(&inheritance<void>::type_check);
  5219. using inheritance_cast_function = decltype(&inheritance<void>::type_cast);
  5220. using inheritance_unique_cast_function = decltype(&inheritance<void>::type_unique_cast<void>);
  5221. } // namespace detail
  5222. } // namespace sol
  5223. // end of sol/inheritance.hpp
  5224. // beginning of sol/error_handler.hpp
  5225. namespace sol {
  5226. inline std::string associated_type_name(lua_State* L, int index, type t) {
  5227. switch (t) {
  5228. case type::poly:
  5229. return "anything";
  5230. case type::userdata:
  5231. {
  5232. if (lua_getmetatable(L, index) == 0) {
  5233. break;
  5234. }
  5235. lua_pushlstring(L, "__name", 6);
  5236. lua_rawget(L, -2);
  5237. size_t sz;
  5238. const char* name = lua_tolstring(L, -1, &sz);
  5239. std::string tn(name, static_cast<std::string::size_type>(sz));
  5240. lua_pop(L, 2);
  5241. return name;
  5242. }
  5243. default:
  5244. break;
  5245. }
  5246. return lua_typename(L, static_cast<int>(t));
  5247. }
  5248. inline int type_panic_string(lua_State* L, int index, type expected, type actual, const std::string& message = "") noexcept(false) {
  5249. const char* err = message.empty() ? "stack index %d, expected %s, received %s" : "stack index %d, expected %s, received %s: %s";
  5250. std::string actualname = associated_type_name(L, index, actual);
  5251. return luaL_error(L, err, index,
  5252. expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(expected)),
  5253. actualname.c_str(),
  5254. message.c_str());
  5255. }
  5256. inline int type_panic_c_str(lua_State* L, int index, type expected, type actual, const char* message = nullptr) noexcept(false) {
  5257. const char* err = message == nullptr || (std::char_traits<char>::length(message) == 0) ? "stack index %d, expected %s, received %s" : "stack index %d, expected %s, received %s: %s";
  5258. std::string actualname = associated_type_name(L, index, actual);
  5259. return luaL_error(L, err, index,
  5260. expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(expected)),
  5261. actualname.c_str(),
  5262. message);
  5263. }
  5264. struct type_panic_t {
  5265. int operator()(lua_State* L, int index, type expected, type actual) const noexcept(false) {
  5266. return type_panic_c_str(L, index, expected, actual, nullptr);
  5267. }
  5268. int operator()(lua_State* L, int index, type expected, type actual, const char* message) const noexcept(false) {
  5269. return type_panic_c_str(L, index, expected, actual, message);
  5270. }
  5271. int operator()(lua_State* L, int index, type expected, type actual, const std::string& message) const noexcept(false) {
  5272. return type_panic_string(L, index, expected, actual, message);
  5273. }
  5274. };
  5275. const type_panic_t type_panic = {};
  5276. struct constructor_handler {
  5277. int operator()(lua_State* L, int index, type expected, type actual, const std::string& message) const noexcept(false) {
  5278. std::string str = "(type check failed in constructor)";
  5279. return type_panic_string(L, index, expected, actual, message.empty() ? str : message + " " + str);
  5280. }
  5281. };
  5282. template <typename F = void>
  5283. struct argument_handler {
  5284. int operator()(lua_State* L, int index, type expected, type actual, const std::string& message) const noexcept(false) {
  5285. std::string str = "(bad argument to variable or function call)";
  5286. return type_panic_string(L, index, expected, actual, message.empty() ? str : message + " " + str );
  5287. }
  5288. };
  5289. template <typename R, typename... Args>
  5290. struct argument_handler<types<R, Args...>> {
  5291. int operator()(lua_State* L, int index, type expected, type actual, const std::string& message) const noexcept(false) {
  5292. std::string addendum = "(bad argument into '";
  5293. addendum += detail::demangle<R>();
  5294. addendum += "(";
  5295. int marker = 0;
  5296. auto action = [&addendum, &marker](const std::string& n) {
  5297. if (marker > 0) {
  5298. addendum += ", ";
  5299. }
  5300. addendum += n;
  5301. ++marker;
  5302. };
  5303. (void)detail::swallow{int(), (action(detail::demangle<Args>()), int())...};
  5304. addendum += ")')";
  5305. return type_panic_string(L, index, expected, actual, message.empty() ? addendum : message + " " + addendum);
  5306. }
  5307. };
  5308. // Specify this function as the handler for lua::check if you know there's nothing wrong
  5309. inline int no_panic(lua_State*, int, type, type, const char* = nullptr) noexcept {
  5310. return 0;
  5311. }
  5312. inline void type_error(lua_State* L, int expected, int actual) noexcept(false) {
  5313. luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual));
  5314. }
  5315. inline void type_error(lua_State* L, type expected, type actual) noexcept(false) {
  5316. type_error(L, static_cast<int>(expected), static_cast<int>(actual));
  5317. }
  5318. inline void type_assert(lua_State* L, int index, type expected, type actual) noexcept(false) {
  5319. if (expected != type::poly && expected != actual) {
  5320. type_panic_c_str(L, index, expected, actual, nullptr);
  5321. }
  5322. }
  5323. inline void type_assert(lua_State* L, int index, type expected) {
  5324. type actual = type_of(L, index);
  5325. type_assert(L, index, expected, actual);
  5326. }
  5327. } // namespace sol
  5328. // end of sol/error_handler.hpp
  5329. // beginning of sol/reference.hpp
  5330. // beginning of sol/stack_reference.hpp
  5331. namespace sol {
  5332. namespace detail {
  5333. inline bool xmovable(lua_State* leftL, lua_State* rightL) {
  5334. if (rightL == nullptr || leftL == nullptr || leftL == rightL) {
  5335. return false;
  5336. }
  5337. const void* leftregistry = lua_topointer(leftL, LUA_REGISTRYINDEX);
  5338. const void* rightregistry = lua_topointer(rightL, LUA_REGISTRYINDEX);
  5339. return leftregistry == rightregistry;
  5340. }
  5341. } // namespace detail
  5342. class stack_reference {
  5343. private:
  5344. lua_State* luastate = nullptr;
  5345. int index = 0;
  5346. protected:
  5347. int registry_index() const noexcept {
  5348. return LUA_NOREF;
  5349. }
  5350. public:
  5351. stack_reference() noexcept = default;
  5352. stack_reference(lua_nil_t) noexcept
  5353. : stack_reference(){};
  5354. stack_reference(lua_State* L, lua_nil_t) noexcept
  5355. : luastate(L), index(0) {
  5356. }
  5357. stack_reference(lua_State* L, int i) noexcept
  5358. : stack_reference(L, absolute_index(L, i)) {
  5359. }
  5360. stack_reference(lua_State* L, absolute_index i) noexcept
  5361. : luastate(L), index(i) {
  5362. }
  5363. stack_reference(lua_State* L, raw_index i) noexcept
  5364. : luastate(L), index(i) {
  5365. }
  5366. stack_reference(lua_State* L, ref_index i) noexcept = delete;
  5367. stack_reference(lua_State* L, const reference& r) noexcept = delete;
  5368. stack_reference(lua_State* L, const stack_reference& r) noexcept
  5369. : luastate(L) {
  5370. if (!r.valid()) {
  5371. index = 0;
  5372. return;
  5373. }
  5374. int i = r.stack_index();
  5375. if (detail::xmovable(lua_state(), r.lua_state())) {
  5376. lua_pushvalue(r.lua_state(), r.index);
  5377. lua_xmove(r.lua_state(), luastate, 1);
  5378. i = absolute_index(luastate, -1);
  5379. }
  5380. index = i;
  5381. }
  5382. stack_reference(stack_reference&& o) noexcept = default;
  5383. stack_reference& operator=(stack_reference&&) noexcept = default;
  5384. stack_reference(const stack_reference&) noexcept = default;
  5385. stack_reference& operator=(const stack_reference&) noexcept = default;
  5386. int push() const noexcept {
  5387. return push(lua_state());
  5388. }
  5389. int push(lua_State* Ls) const noexcept {
  5390. if (lua_state() == nullptr) {
  5391. lua_pushnil(Ls);
  5392. return 1;
  5393. }
  5394. lua_pushvalue(lua_state(), index);
  5395. if (Ls != lua_state()) {
  5396. lua_xmove(lua_state(), Ls, 1);
  5397. }
  5398. return 1;
  5399. }
  5400. void pop() const noexcept {
  5401. pop(lua_state());
  5402. }
  5403. void pop(lua_State* Ls, int n = 1) const noexcept {
  5404. lua_pop(Ls, n);
  5405. }
  5406. int stack_index() const noexcept {
  5407. return index;
  5408. }
  5409. const void* pointer() const noexcept {
  5410. const void* vp = lua_topointer(lua_state(), stack_index());
  5411. return vp;
  5412. }
  5413. type get_type() const noexcept {
  5414. int result = lua_type(lua_state(), index);
  5415. return static_cast<type>(result);
  5416. }
  5417. lua_State* lua_state() const noexcept {
  5418. return luastate;
  5419. }
  5420. bool valid() const noexcept {
  5421. type t = get_type();
  5422. return t != type::lua_nil && t != type::none;
  5423. }
  5424. };
  5425. inline bool operator==(const stack_reference& l, const stack_reference& r) {
  5426. return lua_compare(l.lua_state(), l.stack_index(), r.stack_index(), LUA_OPEQ) == 0;
  5427. }
  5428. inline bool operator!=(const stack_reference& l, const stack_reference& r) {
  5429. return !operator==(l, r);
  5430. }
  5431. inline bool operator==(const stack_reference& lhs, const lua_nil_t&) {
  5432. return !lhs.valid();
  5433. }
  5434. inline bool operator==(const lua_nil_t&, const stack_reference& rhs) {
  5435. return !rhs.valid();
  5436. }
  5437. inline bool operator!=(const stack_reference& lhs, const lua_nil_t&) {
  5438. return lhs.valid();
  5439. }
  5440. inline bool operator!=(const lua_nil_t&, const stack_reference& rhs) {
  5441. return rhs.valid();
  5442. }
  5443. } // namespace sol
  5444. // end of sol/stack_reference.hpp
  5445. namespace sol {
  5446. namespace detail {
  5447. inline const char (&default_main_thread_name())[9] {
  5448. static const char name[9] = "sol.\xF0\x9F\x93\x8C";
  5449. return name;
  5450. }
  5451. } // namespace detail
  5452. namespace stack {
  5453. inline void remove(lua_State* L, int rawindex, int count) {
  5454. if (count < 1)
  5455. return;
  5456. int top = lua_gettop(L);
  5457. if (top < 1) {
  5458. return;
  5459. }
  5460. if (rawindex == -count || top == rawindex) {
  5461. // Slice them right off the top
  5462. lua_pop(L, static_cast<int>(count));
  5463. return;
  5464. }
  5465. // Remove each item one at a time using stack operations
  5466. // Probably slower, maybe, haven't benchmarked,
  5467. // but necessary
  5468. int index = lua_absindex(L, rawindex);
  5469. if (index < 0) {
  5470. index = lua_gettop(L) + (index + 1);
  5471. }
  5472. int last = index + count;
  5473. for (int i = index; i < last; ++i) {
  5474. lua_remove(L, index);
  5475. }
  5476. }
  5477. struct push_popper_at {
  5478. lua_State* L;
  5479. int index;
  5480. int count;
  5481. push_popper_at(lua_State* luastate, int index = -1, int count = 1)
  5482. : L(luastate), index(index), count(count) {
  5483. }
  5484. ~push_popper_at() {
  5485. remove(L, index, count);
  5486. }
  5487. };
  5488. template <bool top_level>
  5489. struct push_popper_n {
  5490. lua_State* L;
  5491. int t;
  5492. push_popper_n(lua_State* luastate, int x)
  5493. : L(luastate), t(x) {
  5494. }
  5495. push_popper_n(const push_popper_n&) = delete;
  5496. push_popper_n(push_popper_n&&) = default;
  5497. push_popper_n& operator=(const push_popper_n&) = delete;
  5498. push_popper_n& operator=(push_popper_n&&) = default;
  5499. ~push_popper_n() {
  5500. lua_pop(L, t);
  5501. }
  5502. };
  5503. template <>
  5504. struct push_popper_n<true> {
  5505. push_popper_n(lua_State*, int) {
  5506. }
  5507. };
  5508. template <bool, typename T, typename = void>
  5509. struct push_popper {
  5510. T t;
  5511. push_popper(T x)
  5512. : t(x) {
  5513. t.push();
  5514. }
  5515. ~push_popper() {
  5516. t.pop();
  5517. }
  5518. };
  5519. template <typename T, typename C>
  5520. struct push_popper<true, T, C> {
  5521. push_popper(T) {
  5522. }
  5523. ~push_popper() {
  5524. }
  5525. };
  5526. template <typename T>
  5527. struct push_popper<false, T, std::enable_if_t<std::is_base_of<stack_reference, meta::unqualified_t<T>>::value>> {
  5528. push_popper(T) {
  5529. }
  5530. ~push_popper() {
  5531. }
  5532. };
  5533. template <bool top_level = false, typename T>
  5534. push_popper<top_level, T> push_pop(T&& x) {
  5535. return push_popper<top_level, T>(std::forward<T>(x));
  5536. }
  5537. template <typename T>
  5538. push_popper_at push_pop_at(T&& x) {
  5539. int c = x.push();
  5540. lua_State* L = x.lua_state();
  5541. return push_popper_at(L, lua_absindex(L, -c), c);
  5542. }
  5543. template <bool top_level = false>
  5544. push_popper_n<top_level> pop_n(lua_State* L, int x) {
  5545. return push_popper_n<top_level>(L, x);
  5546. }
  5547. } // namespace stack
  5548. inline lua_State* main_thread(lua_State* L, lua_State* backup_if_unsupported = nullptr) {
  5549. #if SOL_LUA_VERSION < 502
  5550. if (L == nullptr)
  5551. return backup_if_unsupported;
  5552. lua_getglobal(L, detail::default_main_thread_name());
  5553. auto pp = stack::pop_n(L, 1);
  5554. if (type_of(L, -1) == type::thread) {
  5555. return lua_tothread(L, -1);
  5556. }
  5557. return backup_if_unsupported;
  5558. #else
  5559. if (L == nullptr)
  5560. return backup_if_unsupported;
  5561. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
  5562. lua_State* Lmain = lua_tothread(L, -1);
  5563. lua_pop(L, 1);
  5564. return Lmain;
  5565. #endif // Lua 5.2+ has the main thread getter
  5566. }
  5567. namespace detail {
  5568. struct global_tag {
  5569. } const global_{};
  5570. struct no_safety_tag {
  5571. } const no_safety{};
  5572. template <bool b>
  5573. inline lua_State* pick_main_thread(lua_State* L, lua_State* backup_if_unsupported = nullptr) {
  5574. (void)L;
  5575. (void)backup_if_unsupported;
  5576. if (b) {
  5577. return main_thread(L, backup_if_unsupported);
  5578. }
  5579. return L;
  5580. }
  5581. } // namespace detail
  5582. template <bool main_only = false>
  5583. class basic_reference {
  5584. private:
  5585. template <bool o_main_only>
  5586. friend class basic_reference;
  5587. lua_State* luastate = nullptr; // non-owning
  5588. int ref = LUA_NOREF;
  5589. int copy() const noexcept {
  5590. if (ref == LUA_NOREF)
  5591. return LUA_NOREF;
  5592. push();
  5593. return luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5594. }
  5595. template <bool r_main_only>
  5596. void copy_assign(const basic_reference<r_main_only>& r) {
  5597. if (valid()) {
  5598. deref();
  5599. }
  5600. if (r.ref == LUA_REFNIL) {
  5601. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  5602. ref = LUA_REFNIL;
  5603. return;
  5604. }
  5605. if (r.ref == LUA_NOREF) {
  5606. luastate = r.luastate;
  5607. ref = LUA_NOREF;
  5608. return;
  5609. }
  5610. if (detail::xmovable(lua_state(), r.lua_state())) {
  5611. r.push(lua_state());
  5612. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5613. return;
  5614. }
  5615. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  5616. ref = r.copy();
  5617. }
  5618. template <bool r_main_only>
  5619. void move_assign(basic_reference<r_main_only>&& r) {
  5620. if (valid()) {
  5621. deref();
  5622. }
  5623. if (r.ref == LUA_REFNIL) {
  5624. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  5625. ref = LUA_REFNIL;
  5626. return;
  5627. }
  5628. if (r.ref == LUA_NOREF) {
  5629. luastate = r.luastate;
  5630. ref = LUA_NOREF;
  5631. return;
  5632. }
  5633. if (detail::xmovable(lua_state(), r.lua_state())) {
  5634. r.push(lua_state());
  5635. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5636. return;
  5637. }
  5638. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  5639. ref = r.ref;
  5640. r.ref = LUA_NOREF;
  5641. r.luastate = nullptr;
  5642. }
  5643. protected:
  5644. basic_reference(lua_State* L, detail::global_tag) noexcept
  5645. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5646. lua_pushglobaltable(lua_state());
  5647. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5648. }
  5649. int stack_index() const noexcept {
  5650. return -1;
  5651. }
  5652. void deref() const noexcept {
  5653. luaL_unref(lua_state(), LUA_REGISTRYINDEX, ref);
  5654. }
  5655. public:
  5656. basic_reference() noexcept = default;
  5657. basic_reference(lua_nil_t) noexcept
  5658. : basic_reference() {
  5659. }
  5660. basic_reference(const stack_reference& r) noexcept
  5661. : basic_reference(r.lua_state(), r.stack_index()) {
  5662. }
  5663. basic_reference(stack_reference&& r) noexcept
  5664. : basic_reference(r.lua_state(), r.stack_index()) {
  5665. }
  5666. template <bool r_main_only>
  5667. basic_reference(lua_State* L, const basic_reference<r_main_only>& r) noexcept
  5668. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5669. if (r.ref == LUA_REFNIL) {
  5670. ref = LUA_REFNIL;
  5671. return;
  5672. }
  5673. if (r.ref == LUA_NOREF || lua_state() == nullptr) {
  5674. ref = LUA_NOREF;
  5675. return;
  5676. }
  5677. if (detail::xmovable(lua_state(), r.lua_state())) {
  5678. r.push(lua_state());
  5679. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5680. return;
  5681. }
  5682. ref = r.copy();
  5683. }
  5684. template <bool r_main_only>
  5685. basic_reference(lua_State* L, basic_reference<r_main_only>&& r) noexcept
  5686. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5687. if (r.ref == LUA_REFNIL) {
  5688. ref = LUA_REFNIL;
  5689. return;
  5690. }
  5691. if (r.ref == LUA_NOREF || lua_state() == nullptr) {
  5692. ref = LUA_NOREF;
  5693. return;
  5694. }
  5695. if (detail::xmovable(lua_state(), r.lua_state())) {
  5696. r.push(lua_state());
  5697. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5698. return;
  5699. }
  5700. ref = r.ref;
  5701. r.ref = LUA_NOREF;
  5702. r.luastate = nullptr;
  5703. }
  5704. basic_reference(lua_State* L, const stack_reference& r) noexcept
  5705. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5706. if (lua_state() == nullptr || r.lua_state() == nullptr || r.get_type() == type::none) {
  5707. ref = LUA_NOREF;
  5708. return;
  5709. }
  5710. if (r.get_type() == type::lua_nil) {
  5711. ref = LUA_REFNIL;
  5712. return;
  5713. }
  5714. if (lua_state() != r.lua_state() && !detail::xmovable(lua_state(), r.lua_state())) {
  5715. return;
  5716. }
  5717. r.push(lua_state());
  5718. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5719. }
  5720. basic_reference(lua_State* L, int index = -1) noexcept
  5721. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5722. // use L to stick with that state's execution stack
  5723. lua_pushvalue(L, index);
  5724. ref = luaL_ref(L, LUA_REGISTRYINDEX);
  5725. }
  5726. basic_reference(lua_State* L, ref_index index) noexcept
  5727. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5728. lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, index.index);
  5729. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  5730. }
  5731. basic_reference(lua_State* L, lua_nil_t) noexcept
  5732. : luastate(detail::pick_main_thread<main_only>(L, L)) {
  5733. }
  5734. ~basic_reference() noexcept {
  5735. if (lua_state() == nullptr || ref == LUA_NOREF)
  5736. return;
  5737. deref();
  5738. }
  5739. basic_reference(const basic_reference& o) noexcept
  5740. : luastate(o.lua_state()), ref(o.copy()) {
  5741. }
  5742. basic_reference(basic_reference&& o) noexcept
  5743. : luastate(o.lua_state()), ref(o.ref) {
  5744. o.luastate = nullptr;
  5745. o.ref = LUA_NOREF;
  5746. }
  5747. basic_reference(const basic_reference<!main_only>& o) noexcept
  5748. : luastate(detail::pick_main_thread < main_only && !main_only > (o.lua_state(), o.lua_state())), ref(o.copy()) {
  5749. }
  5750. basic_reference(basic_reference<!main_only>&& o) noexcept
  5751. : luastate(detail::pick_main_thread < main_only && !main_only > (o.lua_state(), o.lua_state())), ref(o.ref) {
  5752. o.luastate = nullptr;
  5753. o.ref = LUA_NOREF;
  5754. }
  5755. basic_reference& operator=(basic_reference&& r) noexcept {
  5756. move_assign(std::move(r));
  5757. return *this;
  5758. }
  5759. basic_reference& operator=(const basic_reference& r) noexcept {
  5760. copy_assign(r);
  5761. return *this;
  5762. }
  5763. basic_reference& operator=(basic_reference<!main_only>&& r) noexcept {
  5764. move_assign(std::move(r));
  5765. return *this;
  5766. }
  5767. basic_reference& operator=(const basic_reference<!main_only>& r) noexcept {
  5768. copy_assign(r);
  5769. return *this;
  5770. }
  5771. basic_reference& operator=(const lua_nil_t&) noexcept {
  5772. if (valid()) {
  5773. deref();
  5774. }
  5775. luastate = nullptr;
  5776. ref = LUA_NOREF;
  5777. return *this;
  5778. }
  5779. template <typename Super>
  5780. basic_reference& operator=(proxy_base<Super>&& r);
  5781. template <typename Super>
  5782. basic_reference& operator=(const proxy_base<Super>& r);
  5783. int push() const noexcept {
  5784. return push(lua_state());
  5785. }
  5786. int push(lua_State* Ls) const noexcept {
  5787. if (lua_state() == nullptr) {
  5788. lua_pushnil(Ls);
  5789. return 1;
  5790. }
  5791. lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, ref);
  5792. if (Ls != lua_state()) {
  5793. lua_xmove(lua_state(), Ls, 1);
  5794. }
  5795. return 1;
  5796. }
  5797. void pop() const noexcept {
  5798. pop(lua_state());
  5799. }
  5800. void pop(lua_State* Ls, int n = 1) const noexcept {
  5801. lua_pop(Ls, n);
  5802. }
  5803. int registry_index() const noexcept {
  5804. return ref;
  5805. }
  5806. bool valid() const noexcept {
  5807. return !(ref == LUA_NOREF || ref == LUA_REFNIL);
  5808. }
  5809. const void* pointer() const noexcept {
  5810. int si = push();
  5811. const void* vp = lua_topointer(lua_state(), -si);
  5812. lua_pop(this->lua_state(), si);
  5813. return vp;
  5814. }
  5815. explicit operator bool() const noexcept {
  5816. return valid();
  5817. }
  5818. type get_type() const noexcept {
  5819. auto pp = stack::push_pop(*this);
  5820. int result = lua_type(lua_state(), -1);
  5821. return static_cast<type>(result);
  5822. }
  5823. lua_State* lua_state() const noexcept {
  5824. return luastate;
  5825. }
  5826. };
  5827. template <bool lb, bool rb>
  5828. inline bool operator==(const basic_reference<lb>& l, const basic_reference<rb>& r) {
  5829. auto ppl = stack::push_pop(l);
  5830. auto ppr = stack::push_pop(r);
  5831. return lua_compare(l.lua_state(), -1, -2, LUA_OPEQ) == 1;
  5832. }
  5833. template <bool lb, bool rb>
  5834. inline bool operator!=(const basic_reference<lb>& l, const basic_reference<rb>& r) {
  5835. return !operator==(l, r);
  5836. }
  5837. template <bool lb>
  5838. inline bool operator==(const basic_reference<lb>& lhs, const lua_nil_t&) {
  5839. return !lhs.valid();
  5840. }
  5841. template <bool rb>
  5842. inline bool operator==(const lua_nil_t&, const basic_reference<rb>& rhs) {
  5843. return !rhs.valid();
  5844. }
  5845. template <bool lb>
  5846. inline bool operator!=(const basic_reference<lb>& lhs, const lua_nil_t&) {
  5847. return lhs.valid();
  5848. }
  5849. template <bool rb>
  5850. inline bool operator!=(const lua_nil_t&, const basic_reference<rb>& rhs) {
  5851. return rhs.valid();
  5852. }
  5853. } // namespace sol
  5854. // end of sol/reference.hpp
  5855. // beginning of sol/tie.hpp
  5856. namespace sol {
  5857. namespace detail {
  5858. template <typename T>
  5859. struct is_speshul : std::false_type {};
  5860. } // namespace detail
  5861. template <typename T>
  5862. struct tie_size : std::tuple_size<T> {};
  5863. template <typename T>
  5864. struct is_tieable : std::integral_constant<bool, (::sol::tie_size<T>::value > 0)> {};
  5865. template <typename... Tn>
  5866. struct tie_t : public std::tuple<std::add_lvalue_reference_t<Tn>...> {
  5867. private:
  5868. typedef std::tuple<std::add_lvalue_reference_t<Tn>...> base_t;
  5869. template <typename T>
  5870. void set(std::false_type, T&& target) {
  5871. std::get<0>(*this) = std::forward<T>(target);
  5872. }
  5873. template <typename T>
  5874. void set(std::true_type, T&& target) {
  5875. typedef tie_size<meta::unqualified_t<T>> value_size;
  5876. typedef tie_size<std::tuple<Tn...>> tie_size;
  5877. typedef std::conditional_t<(value_size::value < tie_size::value), value_size, tie_size> indices_size;
  5878. typedef std::make_index_sequence<indices_size::value> indices;
  5879. set_extra(detail::is_speshul<meta::unqualified_t<T>>(), indices(), std::forward<T>(target));
  5880. }
  5881. template <std::size_t... I, typename T>
  5882. void set_extra(std::true_type, std::index_sequence<I...>, T&& target) {
  5883. using std::get;
  5884. (void)detail::swallow{0,
  5885. (get<I>(static_cast<base_t&>(*this)) = get<I>(types<Tn...>(), target), 0)..., 0};
  5886. }
  5887. template <std::size_t... I, typename T>
  5888. void set_extra(std::false_type, std::index_sequence<I...>, T&& target) {
  5889. using std::get;
  5890. (void)detail::swallow{0,
  5891. (get<I>(static_cast<base_t&>(*this)) = get<I>(target), 0)..., 0};
  5892. }
  5893. public:
  5894. using base_t::base_t;
  5895. template <typename T>
  5896. tie_t& operator=(T&& value) {
  5897. typedef is_tieable<meta::unqualified_t<T>> tieable;
  5898. set(tieable(), std::forward<T>(value));
  5899. return *this;
  5900. }
  5901. };
  5902. template <typename... Tn>
  5903. struct tie_size<tie_t<Tn...>> : std::tuple_size<std::tuple<Tn...>> {};
  5904. namespace adl_barrier_detail {
  5905. template <typename... Tn>
  5906. inline tie_t<std::remove_reference_t<Tn>...> tie(Tn&&... argn) {
  5907. return tie_t<std::remove_reference_t<Tn>...>(std::forward<Tn>(argn)...);
  5908. }
  5909. } // namespace adl_barrier_detail
  5910. using namespace adl_barrier_detail;
  5911. } // namespace sol
  5912. // end of sol/tie.hpp
  5913. // beginning of sol/stack_guard.hpp
  5914. namespace sol {
  5915. namespace detail {
  5916. inline void stack_fail(int, int) {
  5917. #if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS)
  5918. throw error(detail::direct_error, "imbalanced stack after operation finish");
  5919. #else
  5920. // Lol, what do you want, an error printout? :3c
  5921. // There's no sane default here. The right way would be C-style abort(), and that's not acceptable, so
  5922. // hopefully someone will register their own stack_fail thing for the `fx` parameter of stack_guard.
  5923. #endif // No Exceptions
  5924. }
  5925. } // namespace detail
  5926. struct stack_guard {
  5927. lua_State* L;
  5928. int top;
  5929. std::function<void(int, int)> on_mismatch;
  5930. stack_guard(lua_State* L)
  5931. : stack_guard(L, lua_gettop(L)) {
  5932. }
  5933. stack_guard(lua_State* L, int top, std::function<void(int, int)> fx = detail::stack_fail)
  5934. : L(L), top(top), on_mismatch(std::move(fx)) {
  5935. }
  5936. bool check_stack(int modification = 0) const {
  5937. int bottom = lua_gettop(L) + modification;
  5938. if (top == bottom) {
  5939. return true;
  5940. }
  5941. on_mismatch(top, bottom);
  5942. return false;
  5943. }
  5944. ~stack_guard() {
  5945. check_stack();
  5946. }
  5947. };
  5948. } // namespace sol
  5949. // end of sol/stack_guard.hpp
  5950. #include <vector>
  5951. #include <forward_list>
  5952. #include <algorithm>
  5953. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  5954. #endif // C++17
  5955. namespace sol {
  5956. namespace detail {
  5957. struct as_reference_tag {};
  5958. template <typename T>
  5959. struct as_pointer_tag {};
  5960. template <typename T>
  5961. struct as_value_tag {};
  5962. template <typename T>
  5963. struct as_table_tag {};
  5964. using unique_destructor = void (*)(void*);
  5965. #if 0
  5966. using unique_tag = detail::inheritance_unique_cast_function;
  5967. #else
  5968. using unique_tag = const char*;
  5969. #endif
  5970. inline void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space, std::size_t& required_space) {
  5971. // this handels arbitrary alignments...
  5972. // make this into a power-of-2-only?
  5973. // actually can't: this is a C++14-compatible framework,
  5974. // power of 2 alignment is C++17
  5975. std::uintptr_t initial = reinterpret_cast<std::uintptr_t>(ptr);
  5976. std::uintptr_t offby = static_cast<std::uintptr_t>(initial % alignment);
  5977. std::uintptr_t padding = (alignment - offby) % alignment;
  5978. required_space += size + padding;
  5979. if (space < required_space) {
  5980. return nullptr;
  5981. }
  5982. ptr = static_cast<void*>(static_cast<char*>(ptr) + padding);
  5983. space -= padding;
  5984. return ptr;
  5985. }
  5986. inline void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space) {
  5987. std::size_t required_space = 0;
  5988. return align(alignment, size, ptr, space, required_space);
  5989. }
  5990. template <typename... Args>
  5991. inline std::size_t aligned_space_for(void* alignment = nullptr) {
  5992. char* start = static_cast<char*>(alignment);
  5993. auto specific_align = [&alignment](std::size_t a, std::size_t s) {
  5994. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  5995. alignment = align(a, s, alignment, space);
  5996. alignment = static_cast<void*>(static_cast<char*>(alignment) + s);
  5997. };
  5998. (void)detail::swallow{ int{}, (specific_align(std::alignment_of<Args>::value, sizeof(Args)), int{})... };
  5999. return static_cast<char*>(alignment) - start;
  6000. }
  6001. inline void* align_usertype_pointer(void* ptr) {
  6002. typedef std::integral_constant<bool,
  6003. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6004. false
  6005. #else
  6006. (std::alignment_of<void*>::value > 1)
  6007. #endif
  6008. >
  6009. use_align;
  6010. if (!use_align::value) {
  6011. return ptr;
  6012. }
  6013. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  6014. return align(std::alignment_of<void*>::value, sizeof(void*), ptr, space);
  6015. }
  6016. template <bool pre_aligned = false>
  6017. inline void* align_usertype_unique_destructor(void* ptr) {
  6018. typedef std::integral_constant<bool,
  6019. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6020. false
  6021. #else
  6022. (std::alignment_of<unique_destructor>::value > 1)
  6023. #endif
  6024. >
  6025. use_align;
  6026. if (!pre_aligned) {
  6027. ptr = align_usertype_pointer(ptr);
  6028. ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(void*));
  6029. }
  6030. if (!use_align::value) {
  6031. return static_cast<void*>(static_cast<void**>(ptr) + 1);
  6032. }
  6033. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  6034. return align(std::alignment_of<unique_destructor>::value, sizeof(unique_destructor), ptr, space);
  6035. }
  6036. template <bool pre_aligned = false>
  6037. inline void* align_usertype_unique_tag(void* ptr) {
  6038. typedef std::integral_constant<bool,
  6039. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6040. false
  6041. #else
  6042. (std::alignment_of<unique_tag>::value > 1)
  6043. #endif
  6044. >
  6045. use_align;
  6046. if (!pre_aligned) {
  6047. ptr = align_usertype_unique_destructor(ptr);
  6048. ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(unique_destructor));
  6049. }
  6050. if (!use_align::value) {
  6051. return ptr;
  6052. }
  6053. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  6054. return align(std::alignment_of<unique_tag>::value, sizeof(unique_tag), ptr, space);
  6055. }
  6056. template <typename T, bool pre_aligned = false>
  6057. inline void* align_usertype_unique(void* ptr) {
  6058. typedef std::integral_constant<bool,
  6059. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6060. false
  6061. #else
  6062. (std::alignment_of<T>::value > 1)
  6063. #endif
  6064. >
  6065. use_align;
  6066. if (!pre_aligned) {
  6067. ptr = align_usertype_unique_tag(ptr);
  6068. ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(unique_tag));
  6069. }
  6070. if (!use_align::value) {
  6071. return ptr;
  6072. }
  6073. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  6074. return align(std::alignment_of<T>::value, sizeof(T), ptr, space);
  6075. }
  6076. template <typename T>
  6077. inline void* align_user(void* ptr) {
  6078. typedef std::integral_constant<bool,
  6079. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6080. false
  6081. #else
  6082. (std::alignment_of<T>::value > 1)
  6083. #endif
  6084. >
  6085. use_align;
  6086. if (!use_align::value) {
  6087. return ptr;
  6088. }
  6089. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  6090. return align(std::alignment_of<T>::value, sizeof(T), ptr, space);
  6091. }
  6092. template <typename T>
  6093. inline T** usertype_allocate_pointer(lua_State* L) {
  6094. typedef std::integral_constant<bool,
  6095. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6096. false
  6097. #else
  6098. (std::alignment_of<T*>::value > 1)
  6099. #endif
  6100. >
  6101. use_align;
  6102. if (!use_align::value) {
  6103. T** pointerpointer = static_cast<T**>(lua_newuserdata(L, sizeof(T*)));
  6104. return pointerpointer;
  6105. }
  6106. static const std::size_t initial_size = aligned_space_for<T*>(nullptr);
  6107. static const std::size_t misaligned_size = aligned_space_for<T*>(reinterpret_cast<void*>(0x1));
  6108. std::size_t allocated_size = initial_size;
  6109. void* unadjusted = lua_newuserdata(L, initial_size);
  6110. void* adjusted = align(std::alignment_of<T*>::value, sizeof(T*), unadjusted, allocated_size);
  6111. if (adjusted == nullptr) {
  6112. lua_pop(L, 1);
  6113. // what kind of absolute garbage trash allocator are we dealing with?
  6114. // whatever, add some padding in the case of MAXIMAL alignment waste...
  6115. allocated_size = misaligned_size;
  6116. unadjusted = lua_newuserdata(L, allocated_size);
  6117. adjusted = align(std::alignment_of<T*>::value, sizeof(T*), unadjusted, allocated_size);
  6118. if (adjusted == nullptr) {
  6119. // trash allocator can burn in hell
  6120. lua_pop(L, 1);
  6121. //luaL_error(L, "if you are the one that wrote this allocator you should feel bad for doing a worse job than malloc/realloc and should go read some books, yeah?");
  6122. luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T*>().data());
  6123. }
  6124. }
  6125. return static_cast<T**>(adjusted);
  6126. }
  6127. template <typename T>
  6128. inline T* usertype_allocate(lua_State* L) {
  6129. typedef std::integral_constant<bool,
  6130. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6131. false
  6132. #else
  6133. (std::alignment_of<T*>::value > 1 || std::alignment_of<T>::value > 1)
  6134. #endif
  6135. >
  6136. use_align;
  6137. if (!use_align::value) {
  6138. T** pointerpointer = static_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
  6139. T*& pointerreference = *pointerpointer;
  6140. T* allocationtarget = reinterpret_cast<T*>(pointerpointer + 1);
  6141. pointerreference = allocationtarget;
  6142. return allocationtarget;
  6143. }
  6144. /* the assumption is that `lua_newuserdata` -- unless someone
  6145. passes a specific lua_Alloc that gives us bogus, un-aligned pointers
  6146. -- uses malloc, which tends to hand out more or less aligned pointers to memory
  6147. (most of the time, anyhow)
  6148. but it's not guaranteed, so we have to do a post-adjustment check and increase padding
  6149. we do this preliminarily with compile-time stuff, to see
  6150. if we strike lucky with the allocator and alignment values
  6151. otherwise, we have to re-allocate the userdata and
  6152. over-allocate some space for additional padding because
  6153. compilers are optimized for aligned reads/writes
  6154. (and clang will barf UBsan errors on us for not being aligned)
  6155. */
  6156. static const std::size_t initial_size = aligned_space_for<T*, T>(nullptr);
  6157. static const std::size_t misaligned_size = aligned_space_for<T*, T>(reinterpret_cast<void*>(0x1));
  6158. void* pointer_adjusted;
  6159. void* data_adjusted;
  6160. auto attempt_alloc = [](lua_State* L, std::size_t allocated_size, void*& pointer_adjusted, void*& data_adjusted) -> bool {
  6161. void* adjusted = lua_newuserdata(L, allocated_size);
  6162. pointer_adjusted = align(std::alignment_of<T*>::value, sizeof(T*), adjusted, allocated_size);
  6163. if (pointer_adjusted == nullptr) {
  6164. lua_pop(L, 1);
  6165. return false;
  6166. }
  6167. // subtract size of what we're going to allocate there
  6168. allocated_size -= sizeof(T*);
  6169. adjusted = static_cast<void*>(static_cast<char*>(pointer_adjusted) + sizeof(T*));
  6170. data_adjusted = align(std::alignment_of<T>::value, sizeof(T), adjusted, allocated_size);
  6171. if (data_adjusted == nullptr) {
  6172. lua_pop(L, 1);
  6173. return false;
  6174. }
  6175. return true;
  6176. };
  6177. bool result = attempt_alloc(L, initial_size, pointer_adjusted, data_adjusted);
  6178. if (!result) {
  6179. // we're likely to get something that fails to perform the proper allocation a second time,
  6180. // so we use the suggested_new_size bump to help us out here
  6181. pointer_adjusted = nullptr;
  6182. data_adjusted = nullptr;
  6183. result = attempt_alloc(L, misaligned_size, pointer_adjusted, data_adjusted);
  6184. if (!result) {
  6185. if (pointer_adjusted == nullptr) {
  6186. luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle<T>().c_str());
  6187. }
  6188. else {
  6189. luaL_error(L, "aligned allocation of userdata block (data section) for '%s' failed", detail::demangle<T>().c_str());
  6190. }
  6191. return nullptr;
  6192. }
  6193. }
  6194. T** pointerpointer = reinterpret_cast<T**>(pointer_adjusted);
  6195. T*& pointerreference = *pointerpointer;
  6196. T* allocationtarget = reinterpret_cast<T*>(data_adjusted);
  6197. pointerreference = allocationtarget;
  6198. return allocationtarget;
  6199. }
  6200. template <typename T, typename Real>
  6201. inline Real* usertype_unique_allocate(lua_State* L, T**& pref, unique_destructor*& dx, unique_tag*& id) {
  6202. typedef std::integral_constant<bool,
  6203. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6204. false
  6205. #else
  6206. (std::alignment_of<T*>::value > 1 || std::alignment_of<unique_tag>::value > 1 || std::alignment_of<unique_destructor>::value > 1 || std::alignment_of<Real>::value > 1)
  6207. #endif
  6208. >
  6209. use_align;
  6210. if (!use_align::value) {
  6211. pref = static_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(detail::unique_destructor) + sizeof(unique_tag) + sizeof(Real)));
  6212. dx = static_cast<detail::unique_destructor*>(static_cast<void*>(pref + 1));
  6213. id = static_cast<unique_tag*>(static_cast<void*>(dx + 1));
  6214. Real* mem = static_cast<Real*>(static_cast<void*>(id + 1));
  6215. return mem;
  6216. }
  6217. static const std::size_t initial_size = aligned_space_for<T*, unique_destructor, unique_tag, Real>(nullptr);
  6218. static const std::size_t misaligned_size = aligned_space_for<T*, unique_destructor, unique_tag, Real>(reinterpret_cast<void*>(0x1));
  6219. void* pointer_adjusted;
  6220. void* dx_adjusted;
  6221. void* id_adjusted;
  6222. void* data_adjusted;
  6223. auto attempt_alloc = [](lua_State* L, std::size_t allocated_size, void*& pointer_adjusted, void*& dx_adjusted, void*& id_adjusted, void*& data_adjusted) -> bool {
  6224. void* adjusted = lua_newuserdata(L, allocated_size);
  6225. pointer_adjusted = align(std::alignment_of<T*>::value, sizeof(T*), adjusted, allocated_size);
  6226. if (pointer_adjusted == nullptr) {
  6227. lua_pop(L, 1);
  6228. return false;
  6229. }
  6230. allocated_size -= sizeof(T*);
  6231. adjusted = static_cast<void*>(static_cast<char*>(pointer_adjusted) + sizeof(T*));
  6232. dx_adjusted = align(std::alignment_of<unique_destructor>::value, sizeof(unique_destructor), adjusted, allocated_size);
  6233. if (dx_adjusted == nullptr) {
  6234. lua_pop(L, 1);
  6235. return false;
  6236. }
  6237. allocated_size -= sizeof(unique_destructor);
  6238. adjusted = static_cast<void*>(static_cast<char*>(dx_adjusted) + sizeof(unique_destructor));
  6239. id_adjusted = align(std::alignment_of<unique_tag>::value, sizeof(unique_tag), adjusted, allocated_size);
  6240. if (id_adjusted == nullptr) {
  6241. lua_pop(L, 1);
  6242. return false;
  6243. }
  6244. allocated_size -= sizeof(unique_tag);
  6245. adjusted = static_cast<void*>(static_cast<char*>(id_adjusted) + sizeof(unique_tag));
  6246. data_adjusted = align(std::alignment_of<Real>::value, sizeof(Real), adjusted, allocated_size);
  6247. if (data_adjusted == nullptr) {
  6248. lua_pop(L, 1);
  6249. return false;
  6250. }
  6251. return true;
  6252. };
  6253. bool result = attempt_alloc(L, initial_size, pointer_adjusted, dx_adjusted, id_adjusted, data_adjusted);
  6254. if (!result) {
  6255. // we're likely to get something that fails to perform the proper allocation a second time,
  6256. // so we use the suggested_new_size bump to help us out here
  6257. pointer_adjusted = nullptr;
  6258. dx_adjusted = nullptr;
  6259. id_adjusted = nullptr;
  6260. data_adjusted = nullptr;
  6261. result = attempt_alloc(L, misaligned_size, pointer_adjusted, dx_adjusted, id_adjusted, data_adjusted);
  6262. if (!result) {
  6263. if (pointer_adjusted == nullptr) {
  6264. luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle<T>().c_str());
  6265. }
  6266. else if (dx_adjusted == nullptr) {
  6267. luaL_error(L, "aligned allocation of userdata block (deleter section) for '%s' failed", detail::demangle<T>().c_str());
  6268. }
  6269. else {
  6270. luaL_error(L, "aligned allocation of userdata block (data section) for '%s' failed", detail::demangle<T>().c_str());
  6271. }
  6272. return nullptr;
  6273. }
  6274. }
  6275. pref = static_cast<T**>(pointer_adjusted);
  6276. dx = static_cast<detail::unique_destructor*>(dx_adjusted);
  6277. id = static_cast<unique_tag*>(id_adjusted);
  6278. Real* mem = static_cast<Real*>(data_adjusted);
  6279. return mem;
  6280. }
  6281. template <typename T>
  6282. inline T* user_allocate(lua_State* L) {
  6283. typedef std::integral_constant<bool,
  6284. #if defined(SOL_NO_MEMORY_ALIGNMENT) && SOL_NO_MEMORY_ALIGNMENT
  6285. false
  6286. #else
  6287. (std::alignment_of<T>::value > 1)
  6288. #endif
  6289. >
  6290. use_align;
  6291. if (!use_align::value) {
  6292. T* pointer = static_cast<T*>(lua_newuserdata(L, sizeof(T)));
  6293. return pointer;
  6294. }
  6295. static const std::size_t initial_size = aligned_space_for<T>(nullptr);
  6296. static const std::size_t misaligned_size = aligned_space_for<T>(reinterpret_cast<void*>(0x1));
  6297. std::size_t allocated_size = initial_size;
  6298. void* unadjusted = lua_newuserdata(L, allocated_size);
  6299. void* adjusted = align(std::alignment_of<T>::value, sizeof(T), unadjusted, allocated_size);
  6300. if (adjusted == nullptr) {
  6301. lua_pop(L, 1);
  6302. // try again, add extra space for alignment padding
  6303. allocated_size = misaligned_size;
  6304. unadjusted = lua_newuserdata(L, allocated_size);
  6305. adjusted = align(std::alignment_of<T>::value, sizeof(T), unadjusted, allocated_size);
  6306. if (adjusted == nullptr) {
  6307. lua_pop(L, 1);
  6308. luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T>().data());
  6309. }
  6310. }
  6311. return static_cast<T*>(adjusted);
  6312. }
  6313. template <typename T>
  6314. inline int usertype_alloc_destruct(lua_State* L) {
  6315. void* memory = lua_touserdata(L, 1);
  6316. memory = align_usertype_pointer(memory);
  6317. T** pdata = static_cast<T**>(memory);
  6318. T* data = *pdata;
  6319. std::allocator<T> alloc{};
  6320. std::allocator_traits<std::allocator<T>>::destroy(alloc, data);
  6321. return 0;
  6322. }
  6323. template <typename T>
  6324. inline int unique_destruct(lua_State* L) {
  6325. void* memory = lua_touserdata(L, 1);
  6326. memory = align_usertype_unique_destructor(memory);
  6327. unique_destructor& dx = *static_cast<unique_destructor*>(memory);
  6328. memory = static_cast<void*>(static_cast<char*>(memory) + sizeof(unique_destructor));
  6329. memory = align_usertype_unique_tag<true>(memory);
  6330. memory = static_cast<void*>(static_cast<char*>(memory) + sizeof(unique_tag));
  6331. (dx)(memory);
  6332. return 0;
  6333. }
  6334. template <typename T>
  6335. inline int user_alloc_destruct(lua_State* L) {
  6336. void* memory = lua_touserdata(L, 1);
  6337. memory = align_user<T>(memory);
  6338. T* data = static_cast<T*>(memory);
  6339. std::allocator<T> alloc;
  6340. std::allocator_traits<std::allocator<T>>::destroy(alloc, data);
  6341. return 0;
  6342. }
  6343. template <typename T, typename Real>
  6344. inline void usertype_unique_alloc_destroy(void* memory) {
  6345. memory = align_usertype_unique<Real, true>(memory);
  6346. Real* target = static_cast<Real*>(memory);
  6347. std::allocator<Real> alloc;
  6348. std::allocator_traits<std::allocator<Real>>::destroy(alloc, target);
  6349. }
  6350. template <typename T>
  6351. inline int cannot_destruct(lua_State* L) {
  6352. return luaL_error(L, "cannot call the destructor for '%s': it is either hidden (protected/private) or removed with '= delete' and thusly this type is being destroyed without properly destructing, invoking undefined behavior: please bind a usertype and specify a custom destructor to define the behavior properly", detail::demangle<T>().data());
  6353. }
  6354. template <typename T>
  6355. void reserve(T&, std::size_t) {
  6356. }
  6357. template <typename T, typename Al>
  6358. void reserve(std::vector<T, Al>& arr, std::size_t hint) {
  6359. arr.reserve(hint);
  6360. }
  6361. template <typename T, typename Tr, typename Al>
  6362. void reserve(std::basic_string<T, Tr, Al>& arr, std::size_t hint) {
  6363. arr.reserve(hint);
  6364. }
  6365. } // namespace detail
  6366. namespace stack {
  6367. template <typename T>
  6368. struct extensible {};
  6369. template <typename T, bool global = false, bool raw = false, typename = void>
  6370. struct field_getter;
  6371. template <typename T, typename P, bool global = false, bool raw = false, typename = void>
  6372. struct probe_field_getter;
  6373. template <typename T, bool global = false, bool raw = false, typename = void>
  6374. struct field_setter;
  6375. template <typename T, typename = void>
  6376. struct getter;
  6377. template <typename T, typename = void>
  6378. struct qualified_getter;
  6379. template <typename T, typename = void>
  6380. struct userdata_getter;
  6381. template <typename T, typename = void>
  6382. struct popper;
  6383. template <typename T, typename = void>
  6384. struct pusher;
  6385. template <typename T, type = lua_type_of<T>::value, typename = void>
  6386. struct checker;
  6387. template <typename T, type = lua_type_of<T>::value, typename = void>
  6388. struct qualified_checker;
  6389. template <typename T, typename = void>
  6390. struct userdata_checker;
  6391. template <typename T, typename = void>
  6392. struct check_getter;
  6393. template <typename T, typename = void>
  6394. struct qualified_check_getter;
  6395. struct probe {
  6396. bool success;
  6397. int levels;
  6398. probe(bool s, int l)
  6399. : success(s), levels(l) {
  6400. }
  6401. operator bool() const {
  6402. return success;
  6403. };
  6404. };
  6405. struct record {
  6406. int last;
  6407. int used;
  6408. record()
  6409. : last(), used() {
  6410. }
  6411. void use(int count) {
  6412. last = count;
  6413. used += count;
  6414. }
  6415. };
  6416. namespace stack_detail {
  6417. template <typename T>
  6418. struct strip {
  6419. typedef T type;
  6420. };
  6421. template <typename T>
  6422. struct strip<std::reference_wrapper<T>> {
  6423. typedef T& type;
  6424. };
  6425. template <typename T>
  6426. struct strip<user<T>> {
  6427. typedef T& type;
  6428. };
  6429. template <typename T>
  6430. struct strip<non_null<T>> {
  6431. typedef T type;
  6432. };
  6433. template <typename T>
  6434. using strip_t = typename strip<T>::type;
  6435. template <typename T>
  6436. struct strip_extensible { typedef T type; };
  6437. template <typename T>
  6438. struct strip_extensible<extensible<T>> { typedef T type; };
  6439. template <typename T>
  6440. using strip_extensible_t = typename strip_extensible<T>::type;
  6441. template <typename C>
  6442. static int get_size_hint(const C& c) {
  6443. return static_cast<int>(c.size());
  6444. }
  6445. template <typename V, typename Al>
  6446. static int get_size_hint(const std::forward_list<V, Al>&) {
  6447. // forward_list makes me sad
  6448. return static_cast<int>(32);
  6449. }
  6450. template <typename T>
  6451. inline decltype(auto) unchecked_unqualified_get(lua_State* L, int index, record& tracking) {
  6452. typedef meta::unqualified_t<T> Tu;
  6453. getter<Tu> g{};
  6454. (void)g;
  6455. return g.get(L, index, tracking);
  6456. }
  6457. template <typename T>
  6458. inline decltype(auto) unchecked_get(lua_State* L, int index, record& tracking) {
  6459. qualified_getter<T> g{};
  6460. (void)g;
  6461. return g.get(L, index, tracking);
  6462. }
  6463. template <typename T, typename Arg, typename... Args>
  6464. inline int push_reference(lua_State* L, Arg&& arg, Args&&... args) {
  6465. typedef meta::all<
  6466. std::is_lvalue_reference<T>,
  6467. meta::neg<std::is_const<T>>,
  6468. meta::neg<is_lua_primitive<meta::unqualified_t<T>>>,
  6469. meta::neg<is_unique_usertype<meta::unqualified_t<T>>>>
  6470. use_reference_tag;
  6471. pusher<std::conditional_t<use_reference_tag::value, detail::as_reference_tag, meta::unqualified_t<T>>> p{};
  6472. (void)p;
  6473. return p.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  6474. }
  6475. template <typename T, typename Handler>
  6476. bool check_usertype(std::false_type, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
  6477. typedef meta::unqualified_t<T> Tu;
  6478. typedef detail::as_value_tag<Tu> detail_t;
  6479. return checker<detail_t, type::userdata>{}.check(types<meta::unqualified_t<T>>(), L, index, indextype, std::forward<Handler>(handler), tracking);
  6480. }
  6481. template <typename T, typename Handler>
  6482. bool check_usertype(std::true_type, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
  6483. typedef meta::unqualified_t<std::remove_pointer_t<meta::unqualified_t<T>>> Tu;
  6484. typedef detail::as_pointer_tag<Tu> detail_t;
  6485. return checker<detail_t, type::userdata>{}.check(L, index, indextype, std::forward<Handler>(handler), tracking);
  6486. }
  6487. } // namespace stack_detail
  6488. inline bool maybe_indexable(lua_State* L, int index = -1) {
  6489. type t = type_of(L, index);
  6490. return t == type::userdata || t == type::table;
  6491. }
  6492. inline int top(lua_State* L) {
  6493. return lua_gettop(L);
  6494. }
  6495. inline bool is_main_thread(lua_State* L) {
  6496. int ismainthread = lua_pushthread(L);
  6497. lua_pop(L, 1);
  6498. return ismainthread == 1;
  6499. }
  6500. inline void coroutine_create_guard(lua_State* L) {
  6501. if (is_main_thread(L)) {
  6502. return;
  6503. }
  6504. int stacksize = lua_gettop(L);
  6505. if (stacksize < 1) {
  6506. return;
  6507. }
  6508. if (type_of(L, 1) != type::function) {
  6509. return;
  6510. }
  6511. // well now we're screwed...
  6512. // we can clean the stack and pray it doesn't destroy anything?
  6513. lua_pop(L, stacksize);
  6514. }
  6515. template <typename T, typename... Args>
  6516. inline int push(lua_State* L, T&& t, Args&&... args) {
  6517. return pusher<meta::unqualified_t<T>>{}.push(L, std::forward<T>(t), std::forward<Args>(args)...);
  6518. }
  6519. // overload allows to use a pusher of a specific type, but pass in any kind of args
  6520. template <typename T, typename Arg, typename... Args, typename = std::enable_if_t<!std::is_same<T, Arg>::value>>
  6521. inline int push(lua_State* L, Arg&& arg, Args&&... args) {
  6522. return pusher<meta::unqualified_t<T>>{}.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  6523. }
  6524. template <typename T, typename... Args>
  6525. inline int push_reference(lua_State* L, T&& t, Args&&... args) {
  6526. return stack_detail::push_reference<T>(L, std::forward<T>(t), std::forward<Args>(args)...);
  6527. }
  6528. template <typename T, typename Arg, typename... Args>
  6529. inline int push_reference(lua_State* L, Arg&& arg, Args&&... args) {
  6530. return stack_detail::push_reference<T>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  6531. }
  6532. inline int multi_push(lua_State*) {
  6533. // do nothing
  6534. return 0;
  6535. }
  6536. template <typename T, typename... Args>
  6537. inline int multi_push(lua_State* L, T&& t, Args&&... args) {
  6538. int pushcount = push(L, std::forward<T>(t));
  6539. void(detail::swallow{ (pushcount += stack::push(L, std::forward<Args>(args)), 0)... });
  6540. return pushcount;
  6541. }
  6542. inline int multi_push_reference(lua_State*) {
  6543. // do nothing
  6544. return 0;
  6545. }
  6546. template <typename T, typename... Args>
  6547. inline int multi_push_reference(lua_State* L, T&& t, Args&&... args) {
  6548. int pushcount = push_reference(L, std::forward<T>(t));
  6549. void(detail::swallow{ (pushcount += stack::push_reference(L, std::forward<Args>(args)), 0)... });
  6550. return pushcount;
  6551. }
  6552. template <typename T, typename Handler>
  6553. bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6554. qualified_checker<T> c;
  6555. // VC++ has a bad warning here: shut it up
  6556. (void)c;
  6557. return c.check(L, index, std::forward<Handler>(handler), tracking);
  6558. }
  6559. template <typename T, typename Handler>
  6560. bool check(lua_State* L, int index, Handler&& handler) {
  6561. record tracking{};
  6562. return check<T>(L, index, std::forward<Handler>(handler), tracking);
  6563. }
  6564. template <typename T>
  6565. bool check(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6566. auto handler = no_panic;
  6567. return check<T>(L, index, handler);
  6568. }
  6569. template <typename T, typename Handler>
  6570. bool unqualified_check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6571. typedef meta::unqualified_t<T> Tu;
  6572. checker<Tu> c;
  6573. // VC++ has a bad warning here: shut it up
  6574. (void)c;
  6575. return c.check(L, index, std::forward<Handler>(handler), tracking);
  6576. }
  6577. template <typename T, typename Handler>
  6578. bool unqualified_check(lua_State* L, int index, Handler&& handler) {
  6579. record tracking{};
  6580. return unqualified_check<T>(L, index, std::forward<Handler>(handler), tracking);
  6581. }
  6582. template <typename T>
  6583. bool unqualified_check(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6584. auto handler = no_panic;
  6585. return unqualified_check<T>(L, index, handler);
  6586. }
  6587. template <typename T, typename Handler>
  6588. bool check_usertype(lua_State* L, int index, Handler&& handler, record& tracking) {
  6589. type indextype = type_of(L, index);
  6590. return stack_detail::check_usertype<T>(std::is_pointer<T>(), L, index, indextype, std::forward<Handler>(handler), tracking);
  6591. }
  6592. template <typename T, typename Handler>
  6593. bool check_usertype(lua_State* L, int index, Handler&& handler) {
  6594. record tracking{};
  6595. return check_usertype<T>(L, index, std::forward<Handler>(handler), tracking);
  6596. }
  6597. template <typename T>
  6598. bool check_usertype(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6599. auto handler = no_panic;
  6600. return check_usertype<T>(L, index, handler);
  6601. }
  6602. template <typename T, typename Handler>
  6603. inline decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler, record& tracking) {
  6604. typedef meta::unqualified_t<T> Tu;
  6605. check_getter<Tu> cg{};
  6606. (void)cg;
  6607. return cg.get(L, index, std::forward<Handler>(handler), tracking);
  6608. }
  6609. template <typename T, typename Handler>
  6610. inline decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler) {
  6611. record tracking{};
  6612. return unqualified_check_get<T>(L, index, handler, tracking);
  6613. }
  6614. template <typename T>
  6615. inline decltype(auto) unqualified_check_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6616. auto handler = no_panic;
  6617. return unqualified_check_get<T>(L, index, handler);
  6618. }
  6619. template <typename T, typename Handler>
  6620. inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler, record& tracking) {
  6621. qualified_check_getter<T> cg{};
  6622. (void)cg;
  6623. return cg.get(L, index, std::forward<Handler>(handler), tracking);
  6624. }
  6625. template <typename T, typename Handler>
  6626. inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler) {
  6627. record tracking{};
  6628. return check_get<T>(L, index, handler, tracking);
  6629. }
  6630. template <typename T>
  6631. inline decltype(auto) check_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6632. auto handler = no_panic;
  6633. return check_get<T>(L, index, handler);
  6634. }
  6635. namespace stack_detail {
  6636. #if defined(SOL_SAFE_GETTER) && SOL_SAFE_GETTER
  6637. template <typename T>
  6638. inline auto tagged_unqualified_get(types<T>, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_unqualified_get<T>(L, index, tracking)) {
  6639. if (is_lua_reference<T>::value) {
  6640. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  6641. }
  6642. auto op = unqualified_check_get<T>(L, index, type_panic_c_str, tracking);
  6643. return *std::move(op);
  6644. }
  6645. template <typename T>
  6646. inline decltype(auto) tagged_unqualified_get(types<optional<T>>, lua_State* L, int index, record& tracking) {
  6647. return stack_detail::unchecked_unqualified_get<optional<T>>(L, index, tracking);
  6648. }
  6649. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  6650. template <typename T>
  6651. inline decltype(auto) tagged_unqualified_get(types<std::optional<T>>, lua_State* L, int index, record& tracking) {
  6652. return stack_detail::unchecked_unqualified_get<std::optional<T>>(L, index, tracking);
  6653. }
  6654. #endif // shitty optional
  6655. template <typename T>
  6656. inline auto tagged_get(types<T>, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get<T>(L, index, tracking)) {
  6657. if (is_lua_reference<T>::value) {
  6658. return stack_detail::unchecked_get<T>(L, index, tracking);
  6659. }
  6660. auto op = check_get<T>(L, index, type_panic_c_str, tracking);
  6661. return *std::move(op);
  6662. }
  6663. template <typename T>
  6664. inline decltype(auto) tagged_get(types<optional<T>>, lua_State* L, int index, record& tracking) {
  6665. return stack_detail::unchecked_get<optional<T>>(L, index, tracking);
  6666. }
  6667. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  6668. template <typename T>
  6669. inline decltype(auto) tagged_get(types<std::optional<T>>, lua_State* L, int index, record& tracking) {
  6670. return stack_detail::unchecked_get<std::optional<T>>(L, index, tracking);
  6671. }
  6672. #endif // shitty optional
  6673. #else
  6674. template <typename T>
  6675. inline decltype(auto) tagged_unqualified_get(types<T>, lua_State* L, int index, record& tracking) {
  6676. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  6677. }
  6678. template <typename T>
  6679. inline decltype(auto) tagged_get(types<T>, lua_State* L, int index, record& tracking) {
  6680. return stack_detail::unchecked_get<T>(L, index, tracking);
  6681. }
  6682. #endif
  6683. template <bool b>
  6684. struct check_types {
  6685. template <typename T, typename... Args, typename Handler>
  6686. static bool check(types<T, Args...>, lua_State* L, int firstargument, Handler&& handler, record& tracking) {
  6687. if (!stack::check<T>(L, firstargument + tracking.used, handler, tracking))
  6688. return false;
  6689. return check(types<Args...>(), L, firstargument, std::forward<Handler>(handler), tracking);
  6690. }
  6691. template <typename Handler>
  6692. static bool check(types<>, lua_State*, int, Handler&&, record&) {
  6693. return true;
  6694. }
  6695. };
  6696. template <>
  6697. struct check_types<false> {
  6698. template <typename... Args, typename Handler>
  6699. static bool check(types<Args...>, lua_State*, int, Handler&&, record&) {
  6700. return true;
  6701. }
  6702. };
  6703. } // namespace stack_detail
  6704. template <bool b, typename... Args, typename Handler>
  6705. bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6706. return stack_detail::check_types<b>{}.check(types<meta::unqualified_t<Args>...>(), L, index, std::forward<Handler>(handler), tracking);
  6707. }
  6708. template <bool b, typename... Args, typename Handler>
  6709. bool multi_check(lua_State* L, int index, Handler&& handler) {
  6710. record tracking{};
  6711. return multi_check<b, Args...>(L, index, std::forward<Handler>(handler), tracking);
  6712. }
  6713. template <bool b, typename... Args>
  6714. bool multi_check(lua_State* L, int index) {
  6715. auto handler = no_panic;
  6716. return multi_check<b, Args...>(L, index, handler);
  6717. }
  6718. template <typename... Args, typename Handler>
  6719. bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6720. return multi_check<true, Args...>(L, index, std::forward<Handler>(handler), tracking);
  6721. }
  6722. template <typename... Args, typename Handler>
  6723. bool multi_check(lua_State* L, int index, Handler&& handler) {
  6724. return multi_check<true, Args...>(L, index, std::forward<Handler>(handler));
  6725. }
  6726. template <typename... Args>
  6727. bool multi_check(lua_State* L, int index) {
  6728. return multi_check<true, Args...>(L, index);
  6729. }
  6730. template <typename T>
  6731. inline decltype(auto) get_usertype(lua_State* L, int index, record& tracking) {
  6732. #if defined(SOL_SAFE_GETTER) && SOL_SAFE_GETTER
  6733. return stack_detail::tagged_get(types<std::conditional_t<std::is_pointer<T>::value, detail::as_pointer_tag<std::remove_pointer_t<T>>, detail::as_value_tag<T>>>(), L, index, tracking);
  6734. #else
  6735. return stack_detail::unchecked_get<std::conditional_t<std::is_pointer<T>::value, detail::as_pointer_tag<std::remove_pointer_t<T>>, detail::as_value_tag<T>>>(L, index, tracking);
  6736. #endif
  6737. }
  6738. template <typename T>
  6739. inline decltype(auto) get_usertype(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6740. record tracking{};
  6741. return get_usertype<T>(L, index, tracking);
  6742. }
  6743. template <typename T>
  6744. inline decltype(auto) unqualified_get(lua_State* L, int index, record& tracking) {
  6745. return stack_detail::tagged_unqualified_get(types<T>(), L, index, tracking);
  6746. }
  6747. template <typename T>
  6748. inline decltype(auto) unqualified_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6749. record tracking{};
  6750. return unqualified_get<T>(L, index, tracking);
  6751. }
  6752. template <typename T>
  6753. inline decltype(auto) get(lua_State* L, int index, record& tracking) {
  6754. return stack_detail::tagged_get(types<T>(), L, index, tracking);
  6755. }
  6756. template <typename T>
  6757. inline decltype(auto) get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  6758. record tracking{};
  6759. return get<T>(L, index, tracking);
  6760. }
  6761. template <typename T>
  6762. inline decltype(auto) pop(lua_State* L) {
  6763. return popper<meta::unqualified_t<T>>{}.pop(L);
  6764. }
  6765. template <bool global = false, bool raw = false, typename Key>
  6766. void get_field(lua_State* L, Key&& key) {
  6767. field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key));
  6768. }
  6769. template <bool global = false, bool raw = false, typename Key>
  6770. void get_field(lua_State* L, Key&& key, int tableindex) {
  6771. field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key), tableindex);
  6772. }
  6773. template <bool global = false, typename Key>
  6774. void raw_get_field(lua_State* L, Key&& key) {
  6775. get_field<global, true>(L, std::forward<Key>(key));
  6776. }
  6777. template <bool global = false, typename Key>
  6778. void raw_get_field(lua_State* L, Key&& key, int tableindex) {
  6779. get_field<global, true>(L, std::forward<Key>(key), tableindex);
  6780. }
  6781. template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
  6782. probe probe_get_field(lua_State* L, Key&& key) {
  6783. return probe_field_getter<meta::unqualified_t<Key>, C, global, raw>{}.get(L, std::forward<Key>(key));
  6784. }
  6785. template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
  6786. probe probe_get_field(lua_State* L, Key&& key, int tableindex) {
  6787. return probe_field_getter<meta::unqualified_t<Key>, C, global, raw>{}.get(L, std::forward<Key>(key), tableindex);
  6788. }
  6789. template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
  6790. probe probe_raw_get_field(lua_State* L, Key&& key) {
  6791. return probe_get_field<global, true, C>(L, std::forward<Key>(key));
  6792. }
  6793. template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
  6794. probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) {
  6795. return probe_get_field<global, true, C>(L, std::forward<Key>(key), tableindex);
  6796. }
  6797. template <bool global = false, bool raw = false, typename Key, typename Value>
  6798. void set_field(lua_State* L, Key&& key, Value&& value) {
  6799. field_setter<meta::unqualified_t<Key>, global, raw>{}.set(L, std::forward<Key>(key), std::forward<Value>(value));
  6800. }
  6801. template <bool global = false, bool raw = false, typename Key, typename Value>
  6802. void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
  6803. field_setter<meta::unqualified_t<Key>, global, raw>{}.set(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
  6804. }
  6805. template <bool global = false, typename Key, typename Value>
  6806. void raw_set_field(lua_State* L, Key&& key, Value&& value) {
  6807. set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value));
  6808. }
  6809. template <bool global = false, typename Key, typename Value>
  6810. void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
  6811. set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
  6812. }
  6813. template <typename T, typename F>
  6814. inline void modify_unique_usertype_as(const stack_reference& obj, F&& f) {
  6815. typedef unique_usertype_traits<T> u_traits;
  6816. void* raw = lua_touserdata(obj.lua_state(), obj.stack_index());
  6817. void* ptr_memory = detail::align_usertype_pointer(raw);
  6818. void* uu_memory = detail::align_usertype_unique<T>(raw);
  6819. T& uu = *static_cast<T*>(uu_memory);
  6820. f(uu);
  6821. *static_cast<void**>(ptr_memory) = static_cast<void*>(u_traits::get(uu));
  6822. }
  6823. template <typename F>
  6824. inline void modify_unique_usertype(const stack_reference& obj, F&& f) {
  6825. typedef meta::bind_traits<meta::unqualified_t<F>> bt;
  6826. typedef typename bt::template arg_at<0> T;
  6827. modify_unique_usertype_as<meta::unqualified_t<T>>(obj, std::forward<F>(f));
  6828. }
  6829. } // namespace stack
  6830. } // namespace sol
  6831. // end of sol/stack_core.hpp
  6832. // beginning of sol/stack_check.hpp
  6833. // beginning of sol/stack_check_unqualified.hpp
  6834. #include <cmath>
  6835. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  6836. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  6837. #endif // SOL_STD_VARIANT
  6838. #endif // SOL_CXX17_FEATURES
  6839. namespace sol {
  6840. namespace stack {
  6841. namespace stack_detail {
  6842. template <typename T, bool poptable = true>
  6843. inline bool check_metatable(lua_State* L, int index = -2) {
  6844. const auto& metakey = usertype_traits<T>::metatable();
  6845. luaL_getmetatable(L, &metakey[0]);
  6846. const type expectedmetatabletype = static_cast<type>(lua_type(L, -1));
  6847. if (expectedmetatabletype != type::lua_nil) {
  6848. if (lua_rawequal(L, -1, index) == 1) {
  6849. lua_pop(L, 1 + static_cast<int>(poptable));
  6850. return true;
  6851. }
  6852. }
  6853. lua_pop(L, 1);
  6854. return false;
  6855. }
  6856. template <type expected, int (*check_func)(lua_State*, int)>
  6857. struct basic_check {
  6858. template <typename Handler>
  6859. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6860. tracking.use(1);
  6861. bool success = check_func(L, index) == 1;
  6862. if (!success) {
  6863. // expected type, actual type
  6864. handler(L, index, expected, type_of(L, index), "");
  6865. }
  6866. return success;
  6867. }
  6868. };
  6869. } // namespace stack_detail
  6870. template <typename T, typename>
  6871. struct userdata_checker {
  6872. template <typename Handler>
  6873. static bool check(lua_State*, int, type, Handler&&, record&) {
  6874. return false;
  6875. }
  6876. };
  6877. template <typename T, type expected, typename>
  6878. struct checker {
  6879. template <typename Handler>
  6880. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6881. tracking.use(1);
  6882. const type indextype = type_of(L, index);
  6883. bool success = expected == indextype;
  6884. if (!success) {
  6885. // expected type, actual type, message
  6886. handler(L, index, expected, indextype, "");
  6887. }
  6888. return success;
  6889. }
  6890. };
  6891. template <typename T, type expected, typename C>
  6892. struct qualified_checker : checker<meta::unqualified_t<T>, lua_type_of<meta::unqualified_t<T>>::value, C> {};
  6893. template <typename T>
  6894. struct checker<T, type::number, std::enable_if_t<std::is_integral<T>::value>> {
  6895. template <typename Handler>
  6896. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6897. tracking.use(1);
  6898. #if SOL_LUA_VERSION >= 503
  6899. #if defined(SOL_STRINGS_ARE_NUMBERS) && SOL_STRINGS_ARE_NUMBERS
  6900. int isnum = 0;
  6901. lua_tointegerx(L, index, &isnum);
  6902. const bool success = isnum != 0;
  6903. if (!success) {
  6904. // expected type, actual type
  6905. handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string");
  6906. }
  6907. #elif (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION)
  6908. // this check is precise, does not convert
  6909. if (lua_isinteger(L, index) == 1) {
  6910. return true;
  6911. }
  6912. const bool success = false;
  6913. if (!success) {
  6914. // expected type, actual type
  6915. handler(L, index, type::number, type_of(L, index), "not a numeric (integral) type");
  6916. }
  6917. #else
  6918. type t = type_of(L, index);
  6919. const bool success = t == type::number;
  6920. #endif // If numbers are enabled, use the imprecise check
  6921. if (!success) {
  6922. // expected type, actual type
  6923. handler(L, index, type::number, type_of(L, index), "not a numeric type");
  6924. }
  6925. return success;
  6926. #else
  6927. #if !defined(SOL_STRINGS_ARE_NUMBERS) || !SOL_STRINGS_ARE_NUMBERS
  6928. // must pre-check, because it will convert
  6929. type t = type_of(L, index);
  6930. if (t != type::number) {
  6931. // expected type, actual type
  6932. handler(L, index, type::number, t, "not a numeric type");
  6933. return false;
  6934. }
  6935. #endif // Do not allow strings to be numbers
  6936. #if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION)
  6937. int isnum = 0;
  6938. const lua_Number v = lua_tonumberx(L, index, &isnum);
  6939. const bool success = isnum != 0 && static_cast<lua_Number>(llround(v)) == v;
  6940. #else
  6941. const bool success = true;
  6942. #endif // Safe numerics and number precision checking
  6943. if (!success) {
  6944. // expected type, actual type
  6945. #if defined(SOL_STRINGS_ARE_NUMBERS) && SOL_STRINGS_ARE_NUMBERS
  6946. handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string");
  6947. #else
  6948. handler(L, index, type::number, t, "not a numeric type");
  6949. #endif
  6950. }
  6951. return success;
  6952. #endif // Lua Version 5.3 versus others
  6953. }
  6954. };
  6955. template <typename T>
  6956. struct checker<T, type::number, std::enable_if_t<std::is_floating_point<T>::value>> {
  6957. template <typename Handler>
  6958. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6959. tracking.use(1);
  6960. #if defined(SOL_STRINGS_ARE_NUMBERS) && SOL_STRINGS_ARE_NUMBERS
  6961. bool success = lua_isnumber(L, index) == 1;
  6962. if (!success) {
  6963. // expected type, actual type
  6964. handler(L, index, type::number, type_of(L, index), "not a numeric type or numeric string");
  6965. }
  6966. return success;
  6967. #else
  6968. type t = type_of(L, index);
  6969. bool success = t == type::number;
  6970. if (!success) {
  6971. // expected type, actual type
  6972. handler(L, index, type::number, t, "not a numeric type");
  6973. }
  6974. return success;
  6975. #endif // Strings are Numbers
  6976. }
  6977. };
  6978. template <type expected, typename C>
  6979. struct checker<lua_nil_t, expected, C> {
  6980. template <typename Handler>
  6981. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  6982. bool success = lua_isnil(L, index);
  6983. if (success) {
  6984. tracking.use(1);
  6985. return success;
  6986. }
  6987. tracking.use(0);
  6988. success = lua_isnone(L, index);
  6989. if (!success) {
  6990. // expected type, actual type
  6991. handler(L, index, expected, type_of(L, index), "");
  6992. }
  6993. return success;
  6994. }
  6995. };
  6996. template <typename C>
  6997. struct checker<detail::non_lua_nil_t, type::poly, C> {
  6998. template <typename Handler>
  6999. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7000. return !stack::unqualified_check<lua_nil_t>(L, index, std::forward<Handler>(handler), tracking);
  7001. }
  7002. };
  7003. template <type expected, typename C>
  7004. struct checker<nullopt_t, expected, C> : checker<lua_nil_t> {};
  7005. template <typename C>
  7006. struct checker<this_state, type::poly, C> {
  7007. template <typename Handler>
  7008. static bool check(lua_State*, int, Handler&&, record& tracking) {
  7009. tracking.use(0);
  7010. return true;
  7011. }
  7012. };
  7013. template <typename C>
  7014. struct checker<this_main_state, type::poly, C> {
  7015. template <typename Handler>
  7016. static bool check(lua_State*, int, Handler&&, record& tracking) {
  7017. tracking.use(0);
  7018. return true;
  7019. }
  7020. };
  7021. template <typename C>
  7022. struct checker<this_environment, type::poly, C> {
  7023. template <typename Handler>
  7024. static bool check(lua_State*, int, Handler&&, record& tracking) {
  7025. tracking.use(0);
  7026. return true;
  7027. }
  7028. };
  7029. template <typename C>
  7030. struct checker<variadic_args, type::poly, C> {
  7031. template <typename Handler>
  7032. static bool check(lua_State*, int, Handler&&, record& tracking) {
  7033. tracking.use(0);
  7034. return true;
  7035. }
  7036. };
  7037. template <typename C>
  7038. struct checker<type, type::poly, C> {
  7039. template <typename Handler>
  7040. static bool check(lua_State*, int, Handler&&, record& tracking) {
  7041. tracking.use(0);
  7042. return true;
  7043. }
  7044. };
  7045. template <typename T, typename C>
  7046. struct checker<T, type::poly, C> {
  7047. template <typename Handler>
  7048. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7049. tracking.use(1);
  7050. bool success = is_lua_reference<T>::value || !lua_isnone(L, index);
  7051. if (!success) {
  7052. // expected type, actual type
  7053. handler(L, index, type::poly, type_of(L, index), "");
  7054. }
  7055. return success;
  7056. }
  7057. };
  7058. template <typename T, typename C>
  7059. struct checker<T, type::lightuserdata, C> {
  7060. template <typename Handler>
  7061. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7062. tracking.use(1);
  7063. type t = type_of(L, index);
  7064. bool success = t == type::userdata || t == type::lightuserdata;
  7065. if (!success) {
  7066. // expected type, actual type
  7067. handler(L, index, type::lightuserdata, t, "");
  7068. }
  7069. return success;
  7070. }
  7071. };
  7072. template <typename C>
  7073. struct checker<userdata_value, type::userdata, C> {
  7074. template <typename Handler>
  7075. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7076. tracking.use(1);
  7077. type t = type_of(L, index);
  7078. bool success = t == type::userdata;
  7079. if (!success) {
  7080. // expected type, actual type
  7081. handler(L, index, type::userdata, t, "");
  7082. }
  7083. return success;
  7084. }
  7085. };
  7086. template <typename B, typename C>
  7087. struct checker<basic_userdata<B>, type::userdata, C> {
  7088. template <typename Handler>
  7089. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7090. return stack::check<userdata_value>(L, index, std::forward<Handler>(handler), tracking);
  7091. }
  7092. };
  7093. template <typename T, typename C>
  7094. struct checker<user<T>, type::userdata, C> : checker<user<T>, type::lightuserdata, C> {};
  7095. template <typename T, typename C>
  7096. struct checker<non_null<T>, type::userdata, C> : checker<T, lua_type_of<T>::value, C> {};
  7097. template <typename C>
  7098. struct checker<lua_CFunction, type::function, C> : stack_detail::basic_check<type::function, lua_iscfunction> {};
  7099. template <typename C>
  7100. struct checker<std::remove_pointer_t<lua_CFunction>, type::function, C> : checker<lua_CFunction, type::function, C> {};
  7101. template <typename C>
  7102. struct checker<c_closure, type::function, C> : checker<lua_CFunction, type::function, C> {};
  7103. template <typename T, typename C>
  7104. struct checker<T, type::function, C> {
  7105. template <typename Handler>
  7106. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7107. tracking.use(1);
  7108. type t = type_of(L, index);
  7109. if (t == type::lua_nil || t == type::none || t == type::function) {
  7110. // allow for lua_nil to be returned
  7111. return true;
  7112. }
  7113. if (t != type::userdata && t != type::table) {
  7114. handler(L, index, type::function, t, "must be a function or table or a userdata");
  7115. return false;
  7116. }
  7117. // Do advanced check for call-style userdata?
  7118. static const auto& callkey = to_string(meta_function::call);
  7119. if (lua_getmetatable(L, index) == 0) {
  7120. // No metatable, no __call key possible
  7121. handler(L, index, type::function, t, "value is not a function and does not have overriden metatable");
  7122. return false;
  7123. }
  7124. if (lua_isnoneornil(L, -1)) {
  7125. lua_pop(L, 1);
  7126. handler(L, index, type::function, t, "value is not a function and does not have valid metatable");
  7127. return false;
  7128. }
  7129. lua_getfield(L, -1, &callkey[0]);
  7130. if (lua_isnoneornil(L, -1)) {
  7131. lua_pop(L, 2);
  7132. handler(L, index, type::function, t, "value's metatable does not have __call overridden in metatable, cannot call this type");
  7133. return false;
  7134. }
  7135. // has call, is definitely a function
  7136. lua_pop(L, 2);
  7137. return true;
  7138. }
  7139. };
  7140. template <typename T, typename C>
  7141. struct checker<T, type::table, C> {
  7142. template <typename Handler>
  7143. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7144. tracking.use(1);
  7145. type t = type_of(L, index);
  7146. if (t == type::table) {
  7147. return true;
  7148. }
  7149. if (t != type::userdata) {
  7150. handler(L, index, type::table, t, "value is not a table or a userdata that can behave like one");
  7151. return false;
  7152. }
  7153. return true;
  7154. }
  7155. };
  7156. template <type expected, typename C>
  7157. struct checker<metatable_t, expected, C> {
  7158. template <typename Handler>
  7159. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7160. tracking.use(1);
  7161. if (lua_getmetatable(L, index) == 0) {
  7162. return true;
  7163. }
  7164. type t = type_of(L, -1);
  7165. if (t == type::table || t == type::none || t == type::lua_nil) {
  7166. lua_pop(L, 1);
  7167. return true;
  7168. }
  7169. if (t != type::userdata) {
  7170. lua_pop(L, 1);
  7171. handler(L, index, expected, t, "value does not have a valid metatable");
  7172. return false;
  7173. }
  7174. return true;
  7175. }
  7176. };
  7177. template <typename C>
  7178. struct checker<env_t, type::poly, C> {
  7179. template <typename Handler>
  7180. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7181. tracking.use(1);
  7182. type t = type_of(L, index);
  7183. if (t == type::table || t == type::none || t == type::lua_nil || t == type::userdata) {
  7184. return true;
  7185. }
  7186. handler(L, index, type::table, t, "value cannot not have a valid environment");
  7187. return true;
  7188. }
  7189. };
  7190. template <typename E, typename C>
  7191. struct checker<basic_environment<E>, type::poly, C> {
  7192. template <typename Handler>
  7193. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7194. tracking.use(1);
  7195. if (lua_getmetatable(L, index) == 0) {
  7196. return true;
  7197. }
  7198. type t = type_of(L, -1);
  7199. if (t == type::table || t == type::none || t == type::lua_nil) {
  7200. lua_pop(L, 1);
  7201. return true;
  7202. }
  7203. if (t != type::userdata) {
  7204. lua_pop(L, 1);
  7205. handler(L, index, type::table, t, "value does not have a valid metatable");
  7206. return false;
  7207. }
  7208. return true;
  7209. }
  7210. };
  7211. template <typename T, typename C>
  7212. struct checker<detail::as_value_tag<T>, type::userdata, C> {
  7213. template <typename Handler>
  7214. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7215. const type indextype = type_of(L, index);
  7216. return check(types<T>(), L, index, indextype, handler, tracking);
  7217. }
  7218. template <typename U, typename Handler>
  7219. static bool check(types<U>, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
  7220. #if defined(SOL_ENABLE_INTEROP) && SOL_ENABLE_INTEROP
  7221. userdata_checker<extensible<T>> uc;
  7222. (void)uc;
  7223. if (uc.check(L, index, indextype, handler, tracking)) {
  7224. return true;
  7225. }
  7226. #endif // interop extensibility
  7227. tracking.use(1);
  7228. if (indextype != type::userdata) {
  7229. handler(L, index, type::userdata, indextype, "value is not a valid userdata");
  7230. return false;
  7231. }
  7232. if (meta::any<std::is_same<T, lightuserdata_value>, std::is_same<T, userdata_value>, std::is_same<T, userdata>, std::is_same<T, lightuserdata>>::value)
  7233. return true;
  7234. if (lua_getmetatable(L, index) == 0) {
  7235. return true;
  7236. }
  7237. int metatableindex = lua_gettop(L);
  7238. if (stack_detail::check_metatable<U>(L, metatableindex))
  7239. return true;
  7240. if (stack_detail::check_metatable<U*>(L, metatableindex))
  7241. return true;
  7242. if (stack_detail::check_metatable<detail::unique_usertype<U>>(L, metatableindex))
  7243. return true;
  7244. if (stack_detail::check_metatable<as_container_t<U>>(L, metatableindex))
  7245. return true;
  7246. bool success = false;
  7247. if (detail::has_derived<T>::value) {
  7248. auto pn = stack::pop_n(L, 1);
  7249. lua_pushstring(L, &detail::base_class_check_key()[0]);
  7250. lua_rawget(L, metatableindex);
  7251. if (type_of(L, -1) != type::lua_nil) {
  7252. void* basecastdata = lua_touserdata(L, -1);
  7253. detail::inheritance_check_function ic = reinterpret_cast<detail::inheritance_check_function>(basecastdata);
  7254. success = ic(usertype_traits<T>::qualified_name());
  7255. }
  7256. }
  7257. if (!success) {
  7258. lua_pop(L, 1);
  7259. handler(L, index, type::userdata, indextype, "value at this index does not properly reflect the desired type");
  7260. return false;
  7261. }
  7262. lua_pop(L, 1);
  7263. return true;
  7264. }
  7265. };
  7266. template <typename T, typename C>
  7267. struct checker<detail::as_pointer_tag<T>, type::userdata, C> {
  7268. template <typename Handler>
  7269. static bool check(lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
  7270. if (indextype == type::lua_nil) {
  7271. tracking.use(1);
  7272. return true;
  7273. }
  7274. return stack_detail::check_usertype<T>(std::false_type(), L, index, indextype, std::forward<Handler>(handler), tracking);
  7275. }
  7276. template <typename Handler>
  7277. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7278. const type indextype = type_of(L, index);
  7279. return check(L, index, handler, indextype, tracking);
  7280. }
  7281. };
  7282. template <typename T, typename C>
  7283. struct checker<T, type::userdata, C> {
  7284. template <typename Handler>
  7285. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7286. return check_usertype<T>(L, index, std::forward<Handler>(handler), tracking);
  7287. }
  7288. };
  7289. template <typename T, typename C>
  7290. struct checker<T*, type::userdata, C> {
  7291. template <typename Handler>
  7292. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7293. return check_usertype<T*>(L, index, std::forward<Handler>(handler), tracking);
  7294. }
  7295. };
  7296. template <typename X>
  7297. struct checker<X, type::userdata, std::enable_if_t<is_unique_usertype<X>::value>> {
  7298. typedef typename unique_usertype_traits<X>::type T;
  7299. template <typename Handler>
  7300. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7301. const type indextype = type_of(L, index);
  7302. tracking.use(1);
  7303. if (indextype != type::userdata) {
  7304. handler(L, index, type::userdata, indextype, "value is not a userdata");
  7305. return false;
  7306. }
  7307. if (lua_getmetatable(L, index) == 0) {
  7308. return true;
  7309. }
  7310. int metatableindex = lua_gettop(L);
  7311. if (stack_detail::check_metatable<detail::unique_usertype<T>>(L, metatableindex)) {
  7312. void* memory = lua_touserdata(L, index);
  7313. memory = detail::align_usertype_unique_destructor(memory);
  7314. detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
  7315. bool success = &detail::usertype_unique_alloc_destroy<T, X> == pdx;
  7316. if (!success) {
  7317. memory = detail::align_usertype_unique_tag<true>(memory);
  7318. #if 0
  7319. // New version
  7320. #else
  7321. const char*& name_tag = *static_cast<const char**>(memory);
  7322. success = usertype_traits<X>::qualified_name() == name_tag;
  7323. #endif
  7324. if (!success) {
  7325. handler(L, index, type::userdata, indextype, "value is a userdata but is not the correct unique usertype");
  7326. }
  7327. }
  7328. return success;
  7329. }
  7330. lua_pop(L, 1);
  7331. handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
  7332. return false;
  7333. }
  7334. };
  7335. template <typename T, typename C>
  7336. struct checker<std::reference_wrapper<T>, type::userdata, C> {
  7337. template <typename Handler>
  7338. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7339. return stack::check<T>(L, index, std::forward<Handler>(handler), tracking);
  7340. }
  7341. };
  7342. template <typename... Args, typename C>
  7343. struct checker<std::tuple<Args...>, type::poly, C> {
  7344. template <typename Handler>
  7345. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7346. return stack::multi_check<Args...>(L, index, std::forward<Handler>(handler), tracking);
  7347. }
  7348. };
  7349. template <typename A, typename B, typename C>
  7350. struct checker<std::pair<A, B>, type::poly, C> {
  7351. template <typename Handler>
  7352. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7353. return stack::multi_check<A, B>(L, index, std::forward<Handler>(handler), tracking);
  7354. }
  7355. };
  7356. template <typename T, typename C>
  7357. struct checker<optional<T>, type::poly, C> {
  7358. template <typename Handler>
  7359. static bool check(lua_State* L, int index, Handler&&, record& tracking) {
  7360. type t = type_of(L, index);
  7361. if (t == type::none) {
  7362. tracking.use(0);
  7363. return true;
  7364. }
  7365. if (t == type::lua_nil) {
  7366. tracking.use(1);
  7367. return true;
  7368. }
  7369. return stack::check<T>(L, index, no_panic, tracking);
  7370. }
  7371. };
  7372. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  7373. template <typename T, typename C>
  7374. struct checker<std::optional<T>, type::poly, C> {
  7375. template <typename Handler>
  7376. static bool check(lua_State* L, int index, Handler&&, record& tracking) {
  7377. type t = type_of(L, index);
  7378. if (t == type::none) {
  7379. tracking.use(0);
  7380. return true;
  7381. }
  7382. if (t == type::lua_nil) {
  7383. tracking.use(1);
  7384. return true;
  7385. }
  7386. return stack::check<T>(L, index, no_panic, tracking);
  7387. }
  7388. };
  7389. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  7390. template <typename... Tn, typename C>
  7391. struct checker<std::variant<Tn...>, type::poly, C> {
  7392. typedef std::variant<Tn...> V;
  7393. typedef std::variant_size<V> V_size;
  7394. typedef std::integral_constant<bool, V_size::value == 0> V_is_empty;
  7395. template <typename Handler>
  7396. static bool is_one(std::integral_constant<std::size_t, 0>, lua_State* L, int index, Handler&& handler, record& tracking) {
  7397. if (V_is_empty::value && lua_isnone(L, index)) {
  7398. return true;
  7399. }
  7400. tracking.use(1);
  7401. handler(L, index, type::poly, type_of(L, index), "value does not fit any type present in the variant");
  7402. return false;
  7403. }
  7404. template <std::size_t I, typename Handler>
  7405. static bool is_one(std::integral_constant<std::size_t, I>, lua_State* L, int index, Handler&& handler, record& tracking) {
  7406. typedef std::variant_alternative_t<I - 1, V> T;
  7407. record temp_tracking = tracking;
  7408. if (stack::check<T>(L, index, no_panic, temp_tracking)) {
  7409. tracking = temp_tracking;
  7410. return true;
  7411. }
  7412. return is_one(std::integral_constant<std::size_t, I - 1>(), L, index, std::forward<Handler>(handler), tracking);
  7413. }
  7414. template <typename Handler>
  7415. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7416. return is_one(std::integral_constant<std::size_t, V_size::value>(), L, index, std::forward<Handler>(handler), tracking);
  7417. }
  7418. };
  7419. #endif // SOL_STD_VARIANT
  7420. #endif // SOL_CXX17_FEATURES
  7421. }
  7422. } // namespace sol::stack
  7423. // end of sol/stack_check_unqualified.hpp
  7424. // beginning of sol/stack_check_qualified.hpp
  7425. namespace sol {
  7426. namespace stack {
  7427. #if 0
  7428. template <typename X>
  7429. struct qualified_checker<X, type::userdata, std::enable_if_t<is_unique_usertype<X>::value && !std::is_reference<X>::value>> {
  7430. typedef unique_usertype_traits<meta::unqualified_t<X>> u_traits;
  7431. typedef typename u_traits::type T;
  7432. template <typename Handler>
  7433. static bool check(std::false_type, lua_State* L, int index, Handler&& handler, record& tracking) {
  7434. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  7435. }
  7436. template <typename Handler>
  7437. static bool check(std::true_type, lua_State* L, int index, Handler&& handler, record& tracking) {
  7438. // we have a unique pointer type that can be
  7439. // rebound to a base/derived type
  7440. const type indextype = type_of(L, index);
  7441. tracking.use(1);
  7442. if (indextype != type::userdata) {
  7443. handler(L, index, type::userdata, indextype, "value is not a userdata");
  7444. return false;
  7445. }
  7446. if (lua_getmetatable(L, index) == 0) {
  7447. return true;
  7448. }
  7449. int metatableindex = lua_gettop(L);
  7450. void* basecastdata = lua_touserdata(L, index);
  7451. void* memory = detail::align_usertype_unique_destructor(basecastdata);
  7452. detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
  7453. if (&detail::usertype_unique_alloc_destroy<T, X> == pdx) {
  7454. return true;
  7455. }
  7456. if (detail::has_derived<T>::value) {
  7457. memory = detail::align_usertype_unique_cast<true>(memory);
  7458. detail::inheritance_unique_cast_function ic = reinterpret_cast<detail::inheritance_unique_cast_function>(memory);
  7459. string_view ti = usertype_traits<T>::qualified_name();
  7460. string_view rebind_ti = usertype_traits<base_id>::qualified_name();
  7461. if (ic(nullptr, basecastdata, ti, rebind_ti)) {
  7462. lua_pop(L, 1);
  7463. }
  7464. }
  7465. handler(L, index, type::userdata, indextype, "value is a userdata but is not the correct unique usertype");
  7466. return false;
  7467. }
  7468. template <typename Handler>
  7469. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7470. return check(meta::neg<std::is_void<typename u_traits::base_id>>(), L, index, std::forward<Handler>(handler), tracking);
  7471. }
  7472. };
  7473. #endif // Not implemented right now...
  7474. template <typename X>
  7475. struct qualified_checker<X, type::userdata, std::enable_if_t<is_container<meta::unqualified_t<X>>::value && !std::is_reference<X>::value>> {
  7476. template <typename Handler>
  7477. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  7478. if (type_of(L, index) == type::userdata) {
  7479. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  7480. }
  7481. else {
  7482. return stack::unqualified_check<nested<X>>(L, index, std::forward<Handler>(handler), tracking);
  7483. }
  7484. }
  7485. };
  7486. }
  7487. } // namespace sol::stack
  7488. // end of sol/stack_check_qualified.hpp
  7489. // end of sol/stack_check.hpp
  7490. // beginning of sol/stack_get.hpp
  7491. // beginning of sol/stack_get_unqualified.hpp
  7492. // beginning of sol/overload.hpp
  7493. namespace sol {
  7494. template <typename... Functions>
  7495. struct overload_set {
  7496. std::tuple<Functions...> functions;
  7497. template <typename Arg, typename... Args, meta::disable<std::is_same<overload_set, meta::unqualified_t<Arg>>> = meta::enabler>
  7498. overload_set(Arg&& arg, Args&&... args)
  7499. : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  7500. }
  7501. overload_set(const overload_set&) = default;
  7502. overload_set(overload_set&&) = default;
  7503. overload_set& operator=(const overload_set&) = default;
  7504. overload_set& operator=(overload_set&&) = default;
  7505. };
  7506. template <typename... Args>
  7507. decltype(auto) overload(Args&&... args) {
  7508. return overload_set<std::decay_t<Args>...>(std::forward<Args>(args)...);
  7509. }
  7510. } // namespace sol
  7511. // end of sol/overload.hpp
  7512. // beginning of sol/unicode.hpp
  7513. namespace sol {
  7514. // Everything here was lifted pretty much straight out of
  7515. // ogonek, because fuck figuring it out=
  7516. namespace unicode {
  7517. enum class error_code {
  7518. ok = 0,
  7519. invalid_code_point,
  7520. invalid_code_unit,
  7521. invalid_leading_surrogate,
  7522. invalid_trailing_surrogate,
  7523. sequence_too_short,
  7524. overlong_sequence,
  7525. };
  7526. inline const string_view& to_string(error_code ec) {
  7527. static const string_view arr[4] = {
  7528. "ok",
  7529. "invalid code points",
  7530. "invalid code unit",
  7531. "overlong sequence"
  7532. };
  7533. return arr[static_cast<std::size_t>(ec)];
  7534. }
  7535. template <typename It>
  7536. struct decoded_result {
  7537. error_code error;
  7538. char32_t codepoint;
  7539. It next;
  7540. };
  7541. template <typename C>
  7542. struct encoded_result {
  7543. error_code error;
  7544. std::size_t code_units_size;
  7545. std::array<C, 4> code_units;
  7546. };
  7547. struct unicode_detail {
  7548. // codepoint related
  7549. static constexpr char32_t last_code_point = 0x10FFFF;
  7550. static constexpr char32_t first_lead_surrogate = 0xD800;
  7551. static constexpr char32_t last_lead_surrogate = 0xDBFF;
  7552. static constexpr char32_t first_trail_surrogate = 0xDC00;
  7553. static constexpr char32_t last_trail_surrogate = 0xDFFF;
  7554. static constexpr char32_t first_surrogate = first_lead_surrogate;
  7555. static constexpr char32_t last_surrogate = last_trail_surrogate;
  7556. static constexpr bool is_lead_surrogate(char32_t u) {
  7557. return u >= first_lead_surrogate && u <= last_lead_surrogate;
  7558. }
  7559. static constexpr bool is_trail_surrogate(char32_t u) {
  7560. return u >= first_trail_surrogate && u <= last_trail_surrogate;
  7561. }
  7562. static constexpr bool is_surrogate(char32_t u) {
  7563. return u >= first_surrogate && u <= last_surrogate;
  7564. }
  7565. // utf8 related
  7566. static constexpr auto last_1byte_value = 0x7Fu;
  7567. static constexpr auto last_2byte_value = 0x7FFu;
  7568. static constexpr auto last_3byte_value = 0xFFFFu;
  7569. static constexpr auto start_2byte_mask = 0x80u;
  7570. static constexpr auto start_3byte_mask = 0xE0u;
  7571. static constexpr auto start_4byte_mask = 0xF0u;
  7572. static constexpr auto continuation_mask = 0xC0u;
  7573. static constexpr auto continuation_signature = 0x80u;
  7574. static constexpr int sequence_length(unsigned char b) {
  7575. return (b & start_2byte_mask) == 0 ? 1
  7576. : (b & start_3byte_mask) != start_3byte_mask ? 2
  7577. : (b & start_4byte_mask) != start_4byte_mask ? 3
  7578. : 4;
  7579. }
  7580. static constexpr char32_t decode(unsigned char b0, unsigned char b1) {
  7581. return ((b0 & 0x1F) << 6) | (b1 & 0x3F);
  7582. }
  7583. static constexpr char32_t decode(unsigned char b0, unsigned char b1, unsigned char b2) {
  7584. return ((b0 & 0x0F) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F);
  7585. }
  7586. static constexpr char32_t decode(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3) {
  7587. return ((b0 & 0x07) << 18) | ((b1 & 0x3F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F);
  7588. }
  7589. // utf16 related
  7590. static constexpr char32_t last_bmp_value = 0xFFFF;
  7591. static constexpr char32_t normalizing_value = 0x10000;
  7592. static constexpr int lead_surrogate_bitmask = 0xFFC00;
  7593. static constexpr int trail_surrogate_bitmask = 0x3FF;
  7594. static constexpr int lead_shifted_bits = 10;
  7595. static constexpr char32_t replacement = 0xFFFD;
  7596. static char32_t combine_surrogates(char16_t lead, char16_t trail) {
  7597. auto hi = lead - first_lead_surrogate;
  7598. auto lo = trail - first_trail_surrogate;
  7599. return normalizing_value + ((hi << lead_shifted_bits) | lo);
  7600. }
  7601. };
  7602. inline encoded_result<char> code_point_to_utf8(char32_t codepoint) {
  7603. encoded_result<char> er;
  7604. er.error = error_code::ok;
  7605. if (codepoint <= unicode_detail::last_1byte_value) {
  7606. er.code_units_size = 1;
  7607. er.code_units = std::array<char, 4>{ { static_cast<char>(codepoint) } };
  7608. }
  7609. else if (codepoint <= unicode_detail::last_2byte_value) {
  7610. er.code_units_size = 2;
  7611. er.code_units = std::array<char, 4>{{
  7612. static_cast<char>(0xC0 | ((codepoint & 0x7C0) >> 6)),
  7613. static_cast<char>(0x80 | (codepoint & 0x3F)),
  7614. }};
  7615. }
  7616. else if (codepoint <= unicode_detail::last_3byte_value) {
  7617. er.code_units_size = 3;
  7618. er.code_units = std::array<char, 4>{{
  7619. static_cast<char>(0xE0 | ((codepoint & 0xF000) >> 12)),
  7620. static_cast<char>(0x80 | ((codepoint & 0xFC0) >> 6)),
  7621. static_cast<char>(0x80 | (codepoint & 0x3F)),
  7622. }};
  7623. }
  7624. else {
  7625. er.code_units_size = 4;
  7626. er.code_units = std::array<char, 4>{ {
  7627. static_cast<char>(0xF0 | ((codepoint & 0x1C0000) >> 18)),
  7628. static_cast<char>(0x80 | ((codepoint & 0x3F000) >> 12)),
  7629. static_cast<char>(0x80 | ((codepoint & 0xFC0) >> 6)),
  7630. static_cast<char>(0x80 | (codepoint & 0x3F)),
  7631. } };
  7632. }
  7633. return er;
  7634. }
  7635. inline encoded_result<char16_t> code_point_to_utf16(char32_t codepoint) {
  7636. encoded_result<char16_t> er;
  7637. if (codepoint <= unicode_detail::last_bmp_value) {
  7638. er.code_units_size = 1;
  7639. er.code_units = std::array<char16_t, 4>{ { static_cast<char16_t>(codepoint) } };
  7640. er.error = error_code::ok;
  7641. }
  7642. else {
  7643. auto normal = codepoint - unicode_detail::normalizing_value;
  7644. auto lead = unicode_detail::first_lead_surrogate + ((normal & unicode_detail::lead_surrogate_bitmask) >> unicode_detail::lead_shifted_bits);
  7645. auto trail = unicode_detail::first_trail_surrogate + (normal & unicode_detail::trail_surrogate_bitmask);
  7646. er.code_units = std::array<char16_t, 4>{ {
  7647. static_cast<char16_t>(lead),
  7648. static_cast<char16_t>(trail)
  7649. } };
  7650. er.code_units_size = 2;
  7651. er.error = error_code::ok;
  7652. }
  7653. return er;
  7654. }
  7655. inline encoded_result<char32_t> code_point_to_utf32(char32_t codepoint) {
  7656. encoded_result<char32_t> er;
  7657. er.code_units_size = 1;
  7658. er.code_units[0] = codepoint;
  7659. er.error = error_code::ok;
  7660. return er;
  7661. }
  7662. template <typename It>
  7663. inline decoded_result<It> utf8_to_code_point(It it, It last) {
  7664. decoded_result<It> dr;
  7665. if (it == last) {
  7666. dr.next = it;
  7667. dr.error = error_code::sequence_too_short;
  7668. return dr;
  7669. }
  7670. unsigned char b0 = *it;
  7671. std::size_t length = unicode_detail::sequence_length(b0);
  7672. if (length == 1) {
  7673. dr.codepoint = static_cast<char32_t>(b0);
  7674. dr.error = error_code::ok;
  7675. ++it;
  7676. dr.next = it;
  7677. return dr;
  7678. }
  7679. auto is_invalid = [](unsigned char b) { return b == 0xC0 || b == 0xC1 || b > 0xF4; };
  7680. auto is_continuation = [](unsigned char b) {
  7681. return (b & unicode_detail::continuation_mask) == unicode_detail::continuation_signature;
  7682. };
  7683. if (is_invalid(b0) || is_continuation(b0)) {
  7684. dr.error = error_code::invalid_code_unit;
  7685. dr.next = it;
  7686. return dr;
  7687. }
  7688. ++it;
  7689. std::array<unsigned char, 4> b;
  7690. b[0] = b0;
  7691. for (std::size_t i = 1; i < length; ++i) {
  7692. b[i] = *it;
  7693. if (!is_continuation(b[i])) {
  7694. dr.error = error_code::invalid_code_unit;
  7695. dr.next = it;
  7696. return dr;
  7697. }
  7698. ++it;
  7699. }
  7700. char32_t decoded;
  7701. switch (length) {
  7702. case 2:
  7703. decoded = unicode_detail::decode(b[0], b[1]);
  7704. break;
  7705. case 3:
  7706. decoded = unicode_detail::decode(b[0], b[1], b[2]);
  7707. break;
  7708. default:
  7709. decoded = unicode_detail::decode(b[0], b[1], b[2], b[3]);
  7710. break;
  7711. }
  7712. auto is_overlong = [](char32_t u, std::size_t bytes) {
  7713. return u <= unicode_detail::last_1byte_value
  7714. || (u <= unicode_detail::last_2byte_value && bytes > 2)
  7715. || (u <= unicode_detail::last_3byte_value && bytes > 3);
  7716. };
  7717. if (is_overlong(decoded, length)) {
  7718. dr.error = error_code::overlong_sequence;
  7719. return dr;
  7720. }
  7721. if (unicode_detail::is_surrogate(decoded) || decoded > unicode_detail::last_code_point) {
  7722. dr.error = error_code::invalid_code_point;
  7723. return dr;
  7724. }
  7725. // then everything is fine
  7726. dr.codepoint = decoded;
  7727. dr.error = error_code::ok;
  7728. dr.next = it;
  7729. return dr;
  7730. }
  7731. template <typename It>
  7732. inline decoded_result<It> utf16_to_code_point(It it, It last) {
  7733. decoded_result<It> dr;
  7734. if (it == last) {
  7735. dr.next = it;
  7736. dr.error = error_code::sequence_too_short;
  7737. return dr;
  7738. }
  7739. char16_t lead = static_cast<char16_t>(*it);
  7740. if (!unicode_detail::is_surrogate(lead)) {
  7741. ++it;
  7742. dr.codepoint = static_cast<char32_t>(lead);
  7743. dr.next = it;
  7744. dr.error = error_code::ok;
  7745. return dr;
  7746. }
  7747. if (!unicode_detail::is_lead_surrogate(lead)) {
  7748. dr.error = error_code::invalid_leading_surrogate;
  7749. dr.next = it;
  7750. return dr;
  7751. }
  7752. ++it;
  7753. auto trail = *it;
  7754. if (!unicode_detail::is_trail_surrogate(trail)) {
  7755. dr.error = error_code::invalid_trailing_surrogate;
  7756. dr.next = it;
  7757. return dr;
  7758. }
  7759. dr.codepoint = unicode_detail::combine_surrogates(lead, trail);
  7760. dr.next = ++it;
  7761. dr.error = error_code::ok;
  7762. return dr;
  7763. }
  7764. template <typename It>
  7765. inline decoded_result<It> utf32_to_code_point(It it, It last) {
  7766. decoded_result<It> dr;
  7767. if (it == last) {
  7768. dr.next = it;
  7769. dr.error = error_code::sequence_too_short;
  7770. return dr;
  7771. }
  7772. dr.codepoint = static_cast<char32_t>(*it);
  7773. dr.next = ++it;
  7774. dr.error = error_code::ok;
  7775. return dr;
  7776. }
  7777. }
  7778. }
  7779. // end of sol/unicode.hpp
  7780. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  7781. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  7782. #endif // Apple clang screwed up
  7783. #endif // C++17
  7784. namespace sol {
  7785. namespace stack {
  7786. template <typename U>
  7787. struct userdata_getter<U> {
  7788. typedef stack_detail::strip_extensible_t<U> T;
  7789. static std::pair<bool, T*> get(lua_State*, int, void*, record&) {
  7790. return { false, nullptr };
  7791. }
  7792. };
  7793. template <typename T, typename>
  7794. struct getter {
  7795. static T& get(lua_State* L, int index, record& tracking) {
  7796. return getter<detail::as_value_tag<T>>{}.get(L, index, tracking);
  7797. }
  7798. };
  7799. template <typename T, typename C>
  7800. struct qualified_getter : getter<meta::unqualified_t<T>, C> {};
  7801. template <typename T>
  7802. struct getter<T, std::enable_if_t<std::is_floating_point<T>::value>> {
  7803. static T get(lua_State* L, int index, record& tracking) {
  7804. tracking.use(1);
  7805. return static_cast<T>(lua_tonumber(L, index));
  7806. }
  7807. };
  7808. template <typename T>
  7809. struct getter<T, std::enable_if_t<std::is_integral<T>::value>> {
  7810. static T get(lua_State* L, int index, record& tracking) {
  7811. tracking.use(1);
  7812. #if SOL_LUA_VERSION >= 503
  7813. if (lua_isinteger(L, index) != 0) {
  7814. return static_cast<T>(lua_tointeger(L, index));
  7815. }
  7816. #endif
  7817. return static_cast<T>(llround(lua_tonumber(L, index)));
  7818. }
  7819. };
  7820. template <typename T>
  7821. struct getter<T, std::enable_if_t<std::is_enum<T>::value>> {
  7822. static T get(lua_State* L, int index, record& tracking) {
  7823. tracking.use(1);
  7824. return static_cast<T>(lua_tointegerx(L, index, nullptr));
  7825. }
  7826. };
  7827. template <typename T>
  7828. struct getter<as_table_t<T>> {
  7829. typedef meta::unqualified_t<T> Tu;
  7830. template <typename V>
  7831. static void push_back_at_end(std::true_type, types<V>, lua_State* L, T& arr, std::size_t) {
  7832. arr.push_back(stack::get<V>(L, -lua_size<V>::value));
  7833. }
  7834. template <typename V>
  7835. static void push_back_at_end(std::false_type, types<V> t, lua_State* L, T& arr, std::size_t idx) {
  7836. insert_at_end(meta::has_insert<Tu>(), t, L, arr, idx);
  7837. }
  7838. template <typename V>
  7839. static void insert_at_end(std::true_type, types<V>, lua_State* L, T& arr, std::size_t) {
  7840. using std::end;
  7841. arr.insert(end(arr), stack::get<V>(L, -lua_size<V>::value));
  7842. }
  7843. template <typename V>
  7844. static void insert_at_end(std::false_type, types<V>, lua_State* L, T& arr, std::size_t idx) {
  7845. arr[idx] = stack::get<V>(L, -lua_size<V>::value);
  7846. }
  7847. static bool max_size_check(std::false_type, T&, std::size_t) {
  7848. return false;
  7849. }
  7850. static bool max_size_check(std::true_type, T& arr, std::size_t idx) {
  7851. return idx >= arr.max_size();
  7852. }
  7853. static T get(lua_State* L, int relindex, record& tracking) {
  7854. return get(meta::has_key_value_pair<meta::unqualified_t<T>>(), L, relindex, tracking);
  7855. }
  7856. static T get(std::false_type, lua_State* L, int relindex, record& tracking) {
  7857. typedef typename T::value_type V;
  7858. return get(types<V>(), L, relindex, tracking);
  7859. }
  7860. template <typename V>
  7861. static T get(types<V> t, lua_State* L, int relindex, record& tracking) {
  7862. tracking.use(1);
  7863. int index = lua_absindex(L, relindex);
  7864. T arr;
  7865. std::size_t idx = 0;
  7866. #if SOL_LUA_VERSION >= 503
  7867. // This method is HIGHLY performant over regular table iteration thanks to the Lua API changes in 5.3
  7868. // Questionable in 5.4
  7869. for (lua_Integer i = 0;; i += lua_size<V>::value) {
  7870. if (max_size_check(meta::has_max_size<Tu>(), arr, idx)) {
  7871. return arr;
  7872. }
  7873. bool isnil = false;
  7874. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  7875. #if defined(LUA_NILINTABLE) && LUA_NILINTABLE
  7876. lua_pushinteger(L, static_cast<lua_Integer>(i + vi));
  7877. if (lua_keyin(L, index) == 0) {
  7878. // it's time to stop
  7879. isnil = true;
  7880. }
  7881. else {
  7882. // we have a key, have to get the value
  7883. lua_geti(L, index, i + vi);
  7884. }
  7885. #else
  7886. type vt = static_cast<type>(lua_geti(L, index, i + vi));
  7887. isnil = vt == type::none
  7888. || vt == type::lua_nil;
  7889. #endif
  7890. if (isnil) {
  7891. if (i == 0) {
  7892. break;
  7893. }
  7894. #if defined(LUA_NILINTABLE) && LUA_NILINTABLE
  7895. lua_pop(L, vi);
  7896. #else
  7897. lua_pop(L, (vi + 1));
  7898. #endif
  7899. return arr;
  7900. }
  7901. }
  7902. if (isnil) {
  7903. #if defined(LUA_NILINTABLE) && LUA_NILINTABLE
  7904. #else
  7905. lua_pop(L, lua_size<V>::value);
  7906. #endif
  7907. continue;
  7908. }
  7909. push_back_at_end(meta::has_push_back<Tu>(), t, L, arr, idx);
  7910. ++idx;
  7911. lua_pop(L, lua_size<V>::value);
  7912. }
  7913. #else
  7914. // Zzzz slower but necessary thanks to the lower version API and missing functions qq
  7915. for (lua_Integer i = 0;; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
  7916. if (idx >= arr.max_size()) {
  7917. return arr;
  7918. }
  7919. bool isnil = false;
  7920. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  7921. lua_pushinteger(L, i);
  7922. lua_gettable(L, index);
  7923. type vt = type_of(L, -1);
  7924. isnil = vt == type::lua_nil;
  7925. if (isnil) {
  7926. if (i == 0) {
  7927. break;
  7928. }
  7929. lua_pop(L, (vi + 1));
  7930. return arr;
  7931. }
  7932. }
  7933. if (isnil)
  7934. continue;
  7935. push_back_at_end(meta::has_push_back<Tu>(), t, L, arr, idx);
  7936. ++idx;
  7937. }
  7938. #endif
  7939. return arr;
  7940. }
  7941. static T get(std::true_type, lua_State* L, int index, record& tracking) {
  7942. typedef typename T::value_type P;
  7943. typedef typename P::first_type K;
  7944. typedef typename P::second_type V;
  7945. return get(types<K, V>(), L, index, tracking);
  7946. }
  7947. template <typename K, typename V>
  7948. static T get(types<K, V>, lua_State* L, int relindex, record& tracking) {
  7949. tracking.use(1);
  7950. T associative;
  7951. int index = lua_absindex(L, relindex);
  7952. lua_pushnil(L);
  7953. while (lua_next(L, index) != 0) {
  7954. decltype(auto) key = stack::check_get<K>(L, -2);
  7955. if (!key) {
  7956. lua_pop(L, 1);
  7957. continue;
  7958. }
  7959. associative.emplace(std::forward<decltype(*key)>(*key), stack::get<V>(L, -1));
  7960. lua_pop(L, 1);
  7961. }
  7962. return associative;
  7963. }
  7964. };
  7965. template <typename T, typename Al>
  7966. struct getter<as_table_t<std::forward_list<T, Al>>> {
  7967. typedef std::forward_list<T, Al> C;
  7968. static C get(lua_State* L, int relindex, record& tracking) {
  7969. return get(meta::has_key_value_pair<C>(), L, relindex, tracking);
  7970. }
  7971. static C get(std::true_type, lua_State* L, int index, record& tracking) {
  7972. typedef typename T::value_type P;
  7973. typedef typename P::first_type K;
  7974. typedef typename P::second_type V;
  7975. return get(types<K, V>(), L, index, tracking);
  7976. }
  7977. static C get(std::false_type, lua_State* L, int relindex, record& tracking) {
  7978. typedef typename C::value_type V;
  7979. return get(types<V>(), L, relindex, tracking);
  7980. }
  7981. template <typename V>
  7982. static C get(types<V>, lua_State* L, int relindex, record& tracking) {
  7983. tracking.use(1);
  7984. int index = lua_absindex(L, relindex);
  7985. C arr;
  7986. auto at = arr.cbefore_begin();
  7987. std::size_t idx = 0;
  7988. #if SOL_LUA_VERSION >= 503
  7989. // This method is HIGHLY performant over regular table iteration thanks to the Lua API changes in 5.3
  7990. for (lua_Integer i = 0;; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
  7991. if (idx >= arr.max_size()) {
  7992. return arr;
  7993. }
  7994. bool isnil = false;
  7995. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  7996. type t = static_cast<type>(lua_geti(L, index, i + vi));
  7997. isnil = t == type::lua_nil;
  7998. if (isnil) {
  7999. if (i == 0) {
  8000. break;
  8001. }
  8002. lua_pop(L, (vi + 1));
  8003. return arr;
  8004. }
  8005. }
  8006. if (isnil)
  8007. continue;
  8008. at = arr.insert_after(at, stack::get<V>(L, -lua_size<V>::value));
  8009. ++idx;
  8010. }
  8011. #else
  8012. // Zzzz slower but necessary thanks to the lower version API and missing functions qq
  8013. for (lua_Integer i = 0;; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
  8014. if (idx >= arr.max_size()) {
  8015. return arr;
  8016. }
  8017. bool isnil = false;
  8018. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  8019. lua_pushinteger(L, i);
  8020. lua_gettable(L, index);
  8021. type t = type_of(L, -1);
  8022. isnil = t == type::lua_nil;
  8023. if (isnil) {
  8024. if (i == 0) {
  8025. break;
  8026. }
  8027. lua_pop(L, (vi + 1));
  8028. return arr;
  8029. }
  8030. }
  8031. if (isnil)
  8032. continue;
  8033. at = arr.insert_after(at, stack::get<V>(L, -lua_size<V>::value));
  8034. ++idx;
  8035. }
  8036. #endif
  8037. return arr;
  8038. }
  8039. template <typename K, typename V>
  8040. static C get(types<K, V>, lua_State* L, int relindex, record& tracking) {
  8041. tracking.use(1);
  8042. C associative;
  8043. auto at = associative.cbefore_begin();
  8044. int index = lua_absindex(L, relindex);
  8045. lua_pushnil(L);
  8046. while (lua_next(L, index) != 0) {
  8047. decltype(auto) key = stack::check_get<K>(L, -2);
  8048. if (!key) {
  8049. lua_pop(L, 1);
  8050. continue;
  8051. }
  8052. at = associative.emplace_after(at, std::forward<decltype(*key)>(*key), stack::get<V>(L, -1));
  8053. lua_pop(L, 1);
  8054. }
  8055. return associative;
  8056. }
  8057. };
  8058. template <typename T>
  8059. struct getter<nested<T>, std::enable_if_t<!is_container<T>::value>> {
  8060. static T get(lua_State* L, int index, record& tracking) {
  8061. getter<T> g;
  8062. // VC++ has a bad warning here: shut it up
  8063. (void)g;
  8064. return g.get(L, index, tracking);
  8065. }
  8066. };
  8067. template <typename T>
  8068. struct getter<nested<T>, std::enable_if_t<meta::all<is_container<T>, meta::neg<meta::has_key_value_pair<meta::unqualified_t<T>>>>::value>> {
  8069. static T get(lua_State* L, int index, record& tracking) {
  8070. typedef typename T::value_type V;
  8071. getter<as_table_t<T>> g;
  8072. // VC++ has a bad warning here: shut it up
  8073. (void)g;
  8074. return g.get(types<nested<V>>(), L, index, tracking);
  8075. }
  8076. };
  8077. template <typename T>
  8078. struct getter<nested<T>, std::enable_if_t<meta::all<is_container<T>, meta::has_key_value_pair<meta::unqualified_t<T>>>::value>> {
  8079. static T get(lua_State* L, int index, record& tracking) {
  8080. typedef typename T::value_type P;
  8081. typedef typename P::first_type K;
  8082. typedef typename P::second_type V;
  8083. getter<as_table_t<T>> g;
  8084. // VC++ has a bad warning here: shut it up
  8085. (void)g;
  8086. return g.get(types<K, nested<V>>(), L, index, tracking);
  8087. }
  8088. };
  8089. template <typename T>
  8090. struct getter<T, std::enable_if_t<is_lua_reference<T>::value>> {
  8091. static T get(lua_State* L, int index, record& tracking) {
  8092. tracking.use(1);
  8093. return T(L, index);
  8094. }
  8095. };
  8096. template <>
  8097. struct getter<userdata_value> {
  8098. static userdata_value get(lua_State* L, int index, record& tracking) {
  8099. tracking.use(1);
  8100. return userdata_value(lua_touserdata(L, index));
  8101. }
  8102. };
  8103. template <>
  8104. struct getter<lightuserdata_value> {
  8105. static lightuserdata_value get(lua_State* L, int index, record& tracking) {
  8106. tracking.use(1);
  8107. return lightuserdata_value(lua_touserdata(L, index));
  8108. }
  8109. };
  8110. template <typename T>
  8111. struct getter<light<T>> {
  8112. static light<T> get(lua_State* L, int index, record& tracking) {
  8113. tracking.use(1);
  8114. void* memory = lua_touserdata(L, index);
  8115. return light<T>(static_cast<T*>(memory));
  8116. }
  8117. };
  8118. template <typename T>
  8119. struct getter<user<T>> {
  8120. static std::add_lvalue_reference_t<T> get(lua_State* L, int index, record& tracking) {
  8121. tracking.use(1);
  8122. void* memory = lua_touserdata(L, index);
  8123. memory = detail::align_user<T>(memory);
  8124. return *static_cast<std::remove_reference_t<T>*>(memory);
  8125. }
  8126. };
  8127. template <typename T>
  8128. struct getter<user<T*>> {
  8129. static T* get(lua_State* L, int index, record& tracking) {
  8130. tracking.use(1);
  8131. void* memory = lua_touserdata(L, index);
  8132. memory = detail::align_user<T*>(memory);
  8133. return static_cast<T*>(memory);
  8134. }
  8135. };
  8136. template <>
  8137. struct getter<type> {
  8138. static type get(lua_State* L, int index, record& tracking) {
  8139. tracking.use(1);
  8140. return static_cast<type>(lua_type(L, index));
  8141. }
  8142. };
  8143. template <>
  8144. struct getter<bool> {
  8145. static bool get(lua_State* L, int index, record& tracking) {
  8146. tracking.use(1);
  8147. return lua_toboolean(L, index) != 0;
  8148. }
  8149. };
  8150. template <>
  8151. struct getter<std::string> {
  8152. static std::string get(lua_State* L, int index, record& tracking) {
  8153. tracking.use(1);
  8154. std::size_t len;
  8155. auto str = lua_tolstring(L, index, &len);
  8156. return std::string(str, len);
  8157. }
  8158. };
  8159. template <>
  8160. struct getter<const char*> {
  8161. static const char* get(lua_State* L, int index, record& tracking) {
  8162. tracking.use(1);
  8163. size_t sz;
  8164. return lua_tolstring(L, index, &sz);
  8165. }
  8166. };
  8167. template <>
  8168. struct getter<char> {
  8169. static char get(lua_State* L, int index, record& tracking) {
  8170. tracking.use(1);
  8171. size_t len;
  8172. auto str = lua_tolstring(L, index, &len);
  8173. return len > 0 ? str[0] : '\0';
  8174. }
  8175. };
  8176. template <typename Traits>
  8177. struct getter<basic_string_view<char, Traits>> {
  8178. static string_view get(lua_State* L, int index, record& tracking) {
  8179. tracking.use(1);
  8180. size_t sz;
  8181. const char* str = lua_tolstring(L, index, &sz);
  8182. return basic_string_view<char, Traits>(str, sz);
  8183. }
  8184. };
  8185. template <typename Traits, typename Al>
  8186. struct getter<std::basic_string<wchar_t, Traits, Al>> {
  8187. typedef std::basic_string<wchar_t, Traits, Al> S;
  8188. static S get(lua_State* L, int index, record& tracking) {
  8189. typedef std::conditional_t<sizeof(wchar_t) == 2, char16_t, char32_t> Ch;
  8190. typedef typename std::allocator_traits<Al>::template rebind_alloc<Ch> ChAl;
  8191. typedef std::char_traits<Ch> ChTraits;
  8192. getter<std::basic_string<Ch, ChTraits, ChAl>> g;
  8193. (void)g;
  8194. return g.template get_into<S>(L, index, tracking);
  8195. }
  8196. };
  8197. template <typename Traits, typename Al>
  8198. struct getter<std::basic_string<char16_t, Traits, Al>> {
  8199. template <typename F>
  8200. static void convert(const char* strb, const char* stre, F&& f) {
  8201. char32_t cp = 0;
  8202. for (const char* strtarget = strb; strtarget < stre;) {
  8203. auto dr = unicode::utf8_to_code_point(strtarget, stre);
  8204. if (dr.error != unicode::error_code::ok) {
  8205. cp = unicode::unicode_detail::replacement;
  8206. ++strtarget;
  8207. }
  8208. else {
  8209. cp = dr.codepoint;
  8210. strtarget = dr.next;
  8211. }
  8212. auto er = unicode::code_point_to_utf16(cp);
  8213. f(er);
  8214. }
  8215. }
  8216. template <typename S>
  8217. static S get_into(lua_State* L, int index, record& tracking) {
  8218. typedef typename S::value_type Ch;
  8219. tracking.use(1);
  8220. size_t len;
  8221. auto utf8p = lua_tolstring(L, index, &len);
  8222. if (len < 1)
  8223. return S();
  8224. std::size_t needed_size = 0;
  8225. const char* strb = utf8p;
  8226. const char* stre = utf8p + len;
  8227. auto count_units = [&needed_size](const unicode::encoded_result<char16_t> er) {
  8228. needed_size += er.code_units_size;
  8229. };
  8230. convert(strb, stre, count_units);
  8231. S r(needed_size, static_cast<Ch>(0));
  8232. r.resize(needed_size);
  8233. Ch* target = &r[0];
  8234. auto copy_units = [&target](const unicode::encoded_result<char16_t> er) {
  8235. std::memcpy(target, er.code_units.data(), er.code_units_size * sizeof(Ch));
  8236. target += er.code_units_size;
  8237. };
  8238. convert(strb, stre, copy_units);
  8239. return r;
  8240. }
  8241. static std::basic_string<char16_t, Traits, Al> get(lua_State* L, int index, record& tracking) {
  8242. return get_into<std::basic_string<char16_t, Traits, Al>>(L, index, tracking);
  8243. }
  8244. };
  8245. template <typename Traits, typename Al>
  8246. struct getter<std::basic_string<char32_t, Traits, Al>> {
  8247. template <typename F>
  8248. static void convert(const char* strb, const char* stre, F&& f) {
  8249. char32_t cp = 0;
  8250. for (const char* strtarget = strb; strtarget < stre;) {
  8251. auto dr = unicode::utf8_to_code_point(strtarget, stre);
  8252. if (dr.error != unicode::error_code::ok) {
  8253. cp = unicode::unicode_detail::replacement;
  8254. ++strtarget;
  8255. }
  8256. else {
  8257. cp = dr.codepoint;
  8258. strtarget = dr.next;
  8259. }
  8260. auto er = unicode::code_point_to_utf32(cp);
  8261. f(er);
  8262. }
  8263. }
  8264. template <typename S>
  8265. static S get_into(lua_State* L, int index, record& tracking) {
  8266. typedef typename S::value_type Ch;
  8267. tracking.use(1);
  8268. size_t len;
  8269. auto utf8p = lua_tolstring(L, index, &len);
  8270. if (len < 1)
  8271. return S();
  8272. std::size_t needed_size = 0;
  8273. const char* strb = utf8p;
  8274. const char* stre = utf8p + len;
  8275. auto count_units = [&needed_size](const unicode::encoded_result<char32_t> er) {
  8276. needed_size += er.code_units_size;
  8277. };
  8278. convert(strb, stre, count_units);
  8279. S r(needed_size, static_cast<Ch>(0));
  8280. r.resize(needed_size);
  8281. Ch* target = &r[0];
  8282. auto copy_units = [&target](const unicode::encoded_result<char32_t> er) {
  8283. std::memcpy(target, er.code_units.data(), er.code_units_size * sizeof(Ch));
  8284. target += er.code_units_size;
  8285. };
  8286. convert(strb, stre, copy_units);
  8287. return r;
  8288. }
  8289. static std::basic_string<char32_t, Traits, Al> get(lua_State* L, int index, record& tracking) {
  8290. return get_into<std::basic_string<char32_t, Traits, Al>>(L, index, tracking);
  8291. }
  8292. };
  8293. template <>
  8294. struct getter<char16_t> {
  8295. static char16_t get(lua_State* L, int index, record& tracking) {
  8296. string_view utf8 = stack::get<string_view>(L, index, tracking);
  8297. const char* strb = utf8.data();
  8298. const char* stre = utf8.data() + utf8.size();
  8299. char32_t cp = 0;
  8300. auto dr = unicode::utf8_to_code_point(strb, stre);
  8301. if (dr.error != unicode::error_code::ok) {
  8302. cp = unicode::unicode_detail::replacement;
  8303. }
  8304. else {
  8305. cp = dr.codepoint;
  8306. }
  8307. auto er = unicode::code_point_to_utf16(cp);
  8308. return er.code_units[0];
  8309. }
  8310. };
  8311. template <>
  8312. struct getter<char32_t> {
  8313. static char32_t get(lua_State* L, int index, record& tracking) {
  8314. string_view utf8 = stack::get<string_view>(L, index, tracking);
  8315. const char* strb = utf8.data();
  8316. const char* stre = utf8.data() + utf8.size();
  8317. char32_t cp = 0;
  8318. auto dr = unicode::utf8_to_code_point(strb, stre);
  8319. if (dr.error != unicode::error_code::ok) {
  8320. cp = unicode::unicode_detail::replacement;
  8321. }
  8322. else {
  8323. cp = dr.codepoint;
  8324. }
  8325. auto er = unicode::code_point_to_utf32(cp);
  8326. return er.code_units[0];
  8327. }
  8328. };
  8329. template <>
  8330. struct getter<wchar_t> {
  8331. static wchar_t get(lua_State* L, int index, record& tracking) {
  8332. typedef std::conditional_t<sizeof(wchar_t) == 2, char16_t, char32_t> Ch;
  8333. getter<Ch> g;
  8334. (void)g;
  8335. auto c = g.get(L, index, tracking);
  8336. return static_cast<wchar_t>(c);
  8337. }
  8338. };
  8339. template <>
  8340. struct getter<meta_function> {
  8341. static meta_function get(lua_State* L, int index, record& tracking) {
  8342. tracking.use(1);
  8343. const char* name = getter<const char*>{}.get(L, index, tracking);
  8344. const auto& mfnames = meta_function_names();
  8345. for (std::size_t i = 0; i < mfnames.size(); ++i)
  8346. if (mfnames[i] == name)
  8347. return static_cast<meta_function>(i);
  8348. return meta_function::construct;
  8349. }
  8350. };
  8351. template <>
  8352. struct getter<lua_nil_t> {
  8353. static lua_nil_t get(lua_State*, int, record& tracking) {
  8354. tracking.use(1);
  8355. return lua_nil;
  8356. }
  8357. };
  8358. template <>
  8359. struct getter<std::nullptr_t> {
  8360. static std::nullptr_t get(lua_State*, int, record& tracking) {
  8361. tracking.use(1);
  8362. return nullptr;
  8363. }
  8364. };
  8365. template <>
  8366. struct getter<nullopt_t> {
  8367. static nullopt_t get(lua_State*, int, record& tracking) {
  8368. tracking.use(1);
  8369. return nullopt;
  8370. }
  8371. };
  8372. template <>
  8373. struct getter<this_state> {
  8374. static this_state get(lua_State* L, int, record& tracking) {
  8375. tracking.use(0);
  8376. return this_state(L);
  8377. }
  8378. };
  8379. template <>
  8380. struct getter<this_main_state> {
  8381. static this_main_state get(lua_State* L, int, record& tracking) {
  8382. tracking.use(0);
  8383. return this_main_state(main_thread(L, L));
  8384. }
  8385. };
  8386. template <>
  8387. struct getter<lua_CFunction> {
  8388. static lua_CFunction get(lua_State* L, int index, record& tracking) {
  8389. tracking.use(1);
  8390. return lua_tocfunction(L, index);
  8391. }
  8392. };
  8393. template <>
  8394. struct getter<c_closure> {
  8395. static c_closure get(lua_State* L, int index, record& tracking) {
  8396. tracking.use(1);
  8397. return c_closure(lua_tocfunction(L, index), -1);
  8398. }
  8399. };
  8400. template <>
  8401. struct getter<error> {
  8402. static error get(lua_State* L, int index, record& tracking) {
  8403. tracking.use(1);
  8404. size_t sz = 0;
  8405. const char* err = lua_tolstring(L, index, &sz);
  8406. if (err == nullptr) {
  8407. return error(detail::direct_error, "");
  8408. }
  8409. return error(detail::direct_error, std::string(err, sz));
  8410. }
  8411. };
  8412. template <>
  8413. struct getter<void*> {
  8414. static void* get(lua_State* L, int index, record& tracking) {
  8415. tracking.use(1);
  8416. return lua_touserdata(L, index);
  8417. }
  8418. };
  8419. template <>
  8420. struct getter<const void*> {
  8421. static const void* get(lua_State* L, int index, record& tracking) {
  8422. tracking.use(1);
  8423. return lua_touserdata(L, index);
  8424. }
  8425. };
  8426. template <typename T>
  8427. struct getter<detail::as_value_tag<T>> {
  8428. static T* get_no_lua_nil(lua_State* L, int index, record& tracking) {
  8429. void* memory = lua_touserdata(L, index);
  8430. #if defined(SOL_ENABLE_INTEROP) && SOL_ENABLE_INTEROP
  8431. userdata_getter<extensible<T>> ug;
  8432. (void)ug;
  8433. auto ugr = ug.get(L, index, memory, tracking);
  8434. if (ugr.first) {
  8435. return ugr.second;
  8436. }
  8437. #endif // interop extensibility
  8438. tracking.use(1);
  8439. void* rawdata = detail::align_usertype_pointer(memory);
  8440. void** pudata = static_cast<void**>(rawdata);
  8441. void* udata = *pudata;
  8442. return get_no_lua_nil_from(L, udata, index, tracking);
  8443. }
  8444. static T* get_no_lua_nil_from(lua_State* L, void* udata, int index, record&) {
  8445. if (detail::has_derived<T>::value && luaL_getmetafield(L, index, &detail::base_class_cast_key()[0]) != 0) {
  8446. void* basecastdata = lua_touserdata(L, -1);
  8447. detail::inheritance_cast_function ic = reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
  8448. // use the casting function to properly adjust the pointer for the desired T
  8449. udata = ic(udata, usertype_traits<T>::qualified_name());
  8450. lua_pop(L, 1);
  8451. }
  8452. T* obj = static_cast<T*>(udata);
  8453. return obj;
  8454. }
  8455. static T& get(lua_State* L, int index, record& tracking) {
  8456. return *get_no_lua_nil(L, index, tracking);
  8457. }
  8458. };
  8459. template <typename T>
  8460. struct getter<detail::as_pointer_tag<T>> {
  8461. static T* get(lua_State* L, int index, record& tracking) {
  8462. type t = type_of(L, index);
  8463. if (t == type::lua_nil) {
  8464. tracking.use(1);
  8465. return nullptr;
  8466. }
  8467. getter<detail::as_value_tag<T>> g;
  8468. // Avoid VC++ warning
  8469. (void)g;
  8470. return g.get_no_lua_nil(L, index, tracking);
  8471. }
  8472. };
  8473. template <typename T>
  8474. struct getter<non_null<T*>> {
  8475. static T* get(lua_State* L, int index, record& tracking) {
  8476. getter<detail::as_value_tag<T>> g;
  8477. // Avoid VC++ warning
  8478. (void)g;
  8479. return g.get_no_lua_nil(L, index, tracking);
  8480. }
  8481. };
  8482. template <typename T>
  8483. struct getter<T&> {
  8484. static T& get(lua_State* L, int index, record& tracking) {
  8485. getter<detail::as_value_tag<T>> g;
  8486. // Avoid VC++ warning
  8487. (void)g;
  8488. return g.get(L, index, tracking);
  8489. }
  8490. };
  8491. template <typename T>
  8492. struct getter<std::reference_wrapper<T>> {
  8493. static T& get(lua_State* L, int index, record& tracking) {
  8494. getter<T&> g;
  8495. // Avoid VC++ warning
  8496. (void)g;
  8497. return g.get(L, index, tracking);
  8498. }
  8499. };
  8500. template <typename T>
  8501. struct getter<T*> {
  8502. static T* get(lua_State* L, int index, record& tracking) {
  8503. getter<detail::as_pointer_tag<T>> g;
  8504. // Avoid VC++ warning
  8505. (void)g;
  8506. return g.get(L, index, tracking);
  8507. }
  8508. };
  8509. template <typename T>
  8510. struct getter<T, std::enable_if_t<is_unique_usertype<T>::value>> {
  8511. typedef typename unique_usertype_traits<T>::type P;
  8512. typedef typename unique_usertype_traits<T>::actual_type Real;
  8513. static Real& get(lua_State* L, int index, record& tracking) {
  8514. tracking.use(1);
  8515. void* memory = lua_touserdata(L, index);
  8516. memory = detail::align_usertype_unique<Real>(memory);
  8517. Real* mem = static_cast<Real*>(memory);
  8518. return *mem;
  8519. }
  8520. };
  8521. template <typename... Tn>
  8522. struct getter<std::tuple<Tn...>> {
  8523. typedef std::tuple<decltype(stack::get<Tn>(nullptr, 0))...> R;
  8524. template <typename... Args>
  8525. static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
  8526. // Fuck you too, VC++
  8527. return R{ std::forward<Args>(args)... };
  8528. }
  8529. template <std::size_t I, std::size_t... Ix, typename... Args>
  8530. static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
  8531. // Fuck you too, VC++
  8532. typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
  8533. return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)..., stack::get<T>(L, index + tracking.used, tracking));
  8534. }
  8535. static R get(lua_State* L, int index, record& tracking) {
  8536. return apply(std::make_index_sequence<sizeof...(Tn)>(), L, index, tracking);
  8537. }
  8538. };
  8539. template <typename A, typename B>
  8540. struct getter<std::pair<A, B>> {
  8541. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  8542. return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))>{ stack::get<A>(L, index, tracking), stack::get<B>(L, index + tracking.used, tracking) };
  8543. }
  8544. };
  8545. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  8546. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  8547. template <typename... Tn>
  8548. struct getter<std::variant<Tn...>> {
  8549. typedef std::variant<Tn...> V;
  8550. typedef std::variant_size<V> V_size;
  8551. typedef std::integral_constant<bool, V_size::value == 0> V_is_empty;
  8552. static V get_empty(std::true_type, lua_State*, int, record&) {
  8553. return V();
  8554. }
  8555. static V get_empty(std::false_type, lua_State* L, int index, record& tracking) {
  8556. typedef std::variant_alternative_t<0, V> T;
  8557. // This should never be reached...
  8558. // please check your code and understand what you did to bring yourself here
  8559. std::abort();
  8560. return V(std::in_place_index<0>, stack::get<T>(L, index, tracking));
  8561. }
  8562. static V get_one(std::integral_constant<std::size_t, 0>, lua_State* L, int index, record& tracking) {
  8563. return get_empty(V_is_empty(), L, index, tracking);
  8564. }
  8565. template <std::size_t I>
  8566. static V get_one(std::integral_constant<std::size_t, I>, lua_State* L, int index, record& tracking) {
  8567. typedef std::variant_alternative_t<I - 1, V> T;
  8568. record temp_tracking = tracking;
  8569. if (stack::check<T>(L, index, no_panic, temp_tracking)) {
  8570. tracking = temp_tracking;
  8571. return V(std::in_place_index<I - 1>, stack::get<T>(L, index));
  8572. }
  8573. return get_one(std::integral_constant<std::size_t, I - 1>(), L, index, tracking);
  8574. }
  8575. static V get(lua_State* L, int index, record& tracking) {
  8576. return get_one(std::integral_constant<std::size_t, V_size::value>(), L, index, tracking);
  8577. }
  8578. };
  8579. #endif // SOL_STD_VARIANT
  8580. #endif // SOL_CXX17_FEATURES
  8581. }
  8582. } // namespace sol::stack
  8583. // end of sol/stack_get_unqualified.hpp
  8584. // beginning of sol/stack_get_qualified.hpp
  8585. namespace sol {
  8586. namespace stack {
  8587. #if 0 // need static reflection / DERIVED_CLASS macros...
  8588. template <typename X>
  8589. struct qualified_getter<X, std::enable_if_t<
  8590. !std::is_reference<X>::value && is_unique_usertype<meta::unqualified_t<X>>::value
  8591. >> {
  8592. typedef typename unique_usertype_traits<meta::unqualified_t<X>>::type P;
  8593. typedef typename unique_usertype_traits<meta::unqualified_t<X>>::actual_type Real;
  8594. static Real& get(lua_State* L, int index, record& tracking) {
  8595. tracking.use(1);
  8596. void* memory = lua_touserdata(L, index);
  8597. void* del = detail::align_usertype_unique_destructor(memory);
  8598. memory = detail::align_usertype_unique<Real>(memory);
  8599. Real* mem = static_cast<Real*>(memory);
  8600. return *mem;
  8601. }
  8602. };
  8603. #endif // need static reflection
  8604. template <typename T>
  8605. struct qualified_getter<T, std::enable_if_t<
  8606. !std::is_reference<T>::value
  8607. && is_container<meta::unqualified_t<T>>::value
  8608. && std::is_default_constructible<meta::unqualified_t<T>>::value
  8609. && !is_lua_primitive<T>::value
  8610. && !is_transparent_argument<T>::value
  8611. >> {
  8612. static T get(lua_State* L, int index, record& tracking) {
  8613. if (type_of(L, index) == type::userdata) {
  8614. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  8615. }
  8616. else {
  8617. return stack_detail::unchecked_unqualified_get<sol::nested<T>>(L, index, tracking);
  8618. }
  8619. }
  8620. };
  8621. }
  8622. } // namespace sol::stack
  8623. // end of sol/stack_get_qualified.hpp
  8624. // end of sol/stack_get.hpp
  8625. // beginning of sol/stack_check_get.hpp
  8626. // beginning of sol/stack_check_get_unqualified.hpp
  8627. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  8628. #endif // C++17
  8629. namespace sol {
  8630. namespace stack {
  8631. template <typename T, typename>
  8632. struct check_getter {
  8633. typedef decltype(stack_detail::unchecked_unqualified_get<T>(nullptr, 0, std::declval<record&>())) R;
  8634. template <typename Handler>
  8635. static optional<R> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  8636. if (!unqualified_check<T>(L, index, std::forward<Handler>(handler))) {
  8637. tracking.use(static_cast<int>(!lua_isnone(L, index)));
  8638. return nullopt;
  8639. }
  8640. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  8641. }
  8642. };
  8643. template <typename T>
  8644. struct check_getter<T, std::enable_if_t<is_lua_reference<T>::value>> {
  8645. template <typename Handler>
  8646. static optional<T> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  8647. // actually check if it's none here, otherwise
  8648. // we'll have a none object inside an optional!
  8649. bool success = lua_isnoneornil(L, index) == 0 && stack::check<T>(L, index, no_panic);
  8650. if (!success) {
  8651. // expected type, actual type
  8652. tracking.use(static_cast<int>(success));
  8653. handler(L, index, type::poly, type_of(L, index), "");
  8654. return nullopt;
  8655. }
  8656. return stack_detail::unchecked_get<T>(L, index, tracking);
  8657. }
  8658. };
  8659. template <typename T>
  8660. struct check_getter<T, std::enable_if_t<std::is_integral<T>::value && lua_type_of<T>::value == type::number>> {
  8661. template <typename Handler>
  8662. static optional<T> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  8663. #if SOL_LUA_VERSION >= 503
  8664. if (lua_isinteger(L, index) != 0) {
  8665. tracking.use(1);
  8666. return static_cast<T>(lua_tointeger(L, index));
  8667. }
  8668. #endif
  8669. int isnum = 0;
  8670. const lua_Number value = lua_tonumberx(L, index, &isnum);
  8671. if (isnum != 0) {
  8672. #if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION)
  8673. const auto integer_value = llround(value);
  8674. if (static_cast<lua_Number>(integer_value) == value) {
  8675. tracking.use(1);
  8676. return static_cast<T>(integer_value);
  8677. }
  8678. #else
  8679. tracking.use(1);
  8680. return static_cast<T>(value);
  8681. #endif
  8682. }
  8683. const type t = type_of(L, index);
  8684. tracking.use(static_cast<int>(t != type::none));
  8685. handler(L, index, type::number, t, "not an integer");
  8686. return nullopt;
  8687. }
  8688. };
  8689. template <typename T>
  8690. struct check_getter<T, std::enable_if_t<std::is_enum<T>::value && !meta::any_same<T, meta_function, type>::value>> {
  8691. template <typename Handler>
  8692. static optional<T> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  8693. int isnum = 0;
  8694. lua_Integer value = lua_tointegerx(L, index, &isnum);
  8695. if (isnum == 0) {
  8696. type t = type_of(L, index);
  8697. tracking.use(static_cast<int>(t != type::none));
  8698. handler(L, index, type::number, t, "not a valid enumeration value");
  8699. return nullopt;
  8700. }
  8701. tracking.use(1);
  8702. return static_cast<T>(value);
  8703. }
  8704. };
  8705. template <typename T>
  8706. struct check_getter<T, std::enable_if_t<std::is_floating_point<T>::value>> {
  8707. template <typename Handler>
  8708. static optional<T> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  8709. int isnum = 0;
  8710. lua_Number value = lua_tonumberx(L, index, &isnum);
  8711. if (isnum == 0) {
  8712. type t = type_of(L, index);
  8713. tracking.use(static_cast<int>(t != type::none));
  8714. handler(L, index, type::number, t, "not a valid floating point number");
  8715. return nullopt;
  8716. }
  8717. tracking.use(1);
  8718. return static_cast<T>(value);
  8719. }
  8720. };
  8721. template <typename T>
  8722. struct getter<optional<T>> {
  8723. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  8724. return check_get<T>(L, index, no_panic, tracking);
  8725. }
  8726. };
  8727. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  8728. template <typename T>
  8729. struct getter<std::optional<T>> {
  8730. static std::optional<T> get(lua_State* L, int index, record& tracking) {
  8731. if (!unqualified_check<T>(L, index, no_panic)) {
  8732. tracking.use(static_cast<int>(!lua_isnone(L, index)));
  8733. return std::nullopt;
  8734. }
  8735. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  8736. }
  8737. };
  8738. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  8739. template <typename... Tn>
  8740. struct check_getter<std::variant<Tn...>> {
  8741. typedef std::variant<Tn...> V;
  8742. typedef std::variant_size<V> V_size;
  8743. typedef std::integral_constant<bool, V_size::value == 0> V_is_empty;
  8744. template <typename Handler>
  8745. static optional<V> get_empty(std::true_type, lua_State*, int, Handler&&, record&) {
  8746. return nullopt;
  8747. }
  8748. template <typename Handler>
  8749. static optional<V> get_empty(std::false_type, lua_State* L, int index, Handler&& handler, record&) {
  8750. // This should never be reached...
  8751. // please check your code and understand what you did to bring yourself here
  8752. // maybe file a bug report, or 5
  8753. handler(L, index, type::poly, type_of(L, index), "this variant code should never be reached: if it has, you have done something so terribly wrong");
  8754. return nullopt;
  8755. }
  8756. template <typename Handler>
  8757. static optional<V> get_one(std::integral_constant<std::size_t, 0>, lua_State* L, int index, Handler&& handler, record& tracking) {
  8758. return get_empty(V_is_empty(), L, index, std::forward<Handler>(handler), tracking);
  8759. }
  8760. template <std::size_t I, typename Handler>
  8761. static optional<V> get_one(std::integral_constant<std::size_t, I>, lua_State* L, int index, Handler&& handler, record& tracking) {
  8762. typedef std::variant_alternative_t<I - 1, V> T;
  8763. if (stack::check<T>(L, index, no_panic, tracking)) {
  8764. return V(std::in_place_index<I - 1>, stack::get<T>(L, index));
  8765. }
  8766. return get_one(std::integral_constant<std::size_t, I - 1>(), L, index, std::forward<Handler>(handler), tracking);
  8767. }
  8768. template <typename Handler>
  8769. static optional<V> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  8770. return get_one(std::integral_constant<std::size_t, V_size::value>(), L, index, std::forward<Handler>(handler), tracking);
  8771. }
  8772. };
  8773. #endif // SOL_STD_VARIANT
  8774. #endif // SOL_CXX17_FEATURES
  8775. }
  8776. } // namespace sol::stack
  8777. // end of sol/stack_check_get_unqualified.hpp
  8778. // beginning of sol/stack_check_get_qualified.hpp
  8779. namespace sol {
  8780. namespace stack {
  8781. template <typename T, typename C>
  8782. struct qualified_check_getter : check_getter<meta::unqualified_t<T>, C> {};
  8783. }
  8784. } // namespace sol::stack
  8785. // end of sol/stack_check_get_qualified.hpp
  8786. // end of sol/stack_check_get.hpp
  8787. // beginning of sol/stack_push.hpp
  8788. #include <limits>
  8789. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  8790. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  8791. #endif // Can use variant
  8792. #endif // C++17
  8793. namespace sol {
  8794. namespace stack {
  8795. inline int push_environment_of(lua_State* L, int index = -1) {
  8796. #if SOL_LUA_VERSION < 502
  8797. // Use lua_getfenv
  8798. lua_getfenv(L, index);
  8799. return 1;
  8800. #else
  8801. // Use upvalues as explained in Lua 5.2 and beyond's manual
  8802. if (lua_getupvalue(L, index, 1) == nullptr) {
  8803. push(L, lua_nil);
  8804. return 1;
  8805. }
  8806. #endif
  8807. return 1;
  8808. }
  8809. template <typename T>
  8810. int push_environment_of(const T& target) {
  8811. target.push();
  8812. return push_environment_of(target.lua_state(), -1) + 1;
  8813. }
  8814. template <typename T>
  8815. struct pusher<detail::as_value_tag<T>> {
  8816. template <typename F, typename... Args>
  8817. static int push_fx(lua_State* L, F&& f, Args&&... args) {
  8818. // Basically, we store all user-data like this:
  8819. // If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new
  8820. // data in the first sizeof(T*) bytes, and then however many bytes it takes to
  8821. // do the actual object. Things that are std::ref or plain T* are stored as
  8822. // just the sizeof(T*), and nothing else.
  8823. T* obj = detail::usertype_allocate<T>(L);
  8824. std::allocator<T> alloc{};
  8825. std::allocator_traits<std::allocator<T>>::construct(alloc, obj, std::forward<Args>(args)...);
  8826. f();
  8827. return 1;
  8828. }
  8829. template <typename K, typename... Args>
  8830. static int push_keyed(lua_State* L, K&& k, Args&&... args) {
  8831. stack_detail::undefined_metatable<T> fx(L, &k[0]);
  8832. return push_fx(L, fx, std::forward<Args>(args)...);
  8833. }
  8834. template <typename... Args>
  8835. static int push(lua_State* L, Args&&... args) {
  8836. return push_keyed(L, usertype_traits<T>::metatable(), std::forward<Args>(args)...);
  8837. }
  8838. };
  8839. template <typename T>
  8840. struct pusher<detail::as_pointer_tag<T>> {
  8841. typedef meta::unqualified_t<T> U;
  8842. template <typename F>
  8843. static int push_fx(lua_State* L, F&& f, T* obj) {
  8844. if (obj == nullptr)
  8845. return stack::push(L, lua_nil);
  8846. T** pref = detail::usertype_allocate_pointer<T>(L);
  8847. *pref = obj;
  8848. f();
  8849. return 1;
  8850. }
  8851. template <typename K>
  8852. static int push_keyed(lua_State* L, K&& k, T* obj) {
  8853. stack_detail::undefined_metatable<U*> fx(L, &k[0]);
  8854. return push_fx(L, fx, obj);
  8855. }
  8856. static int push(lua_State* L, T* obj) {
  8857. return push_keyed(L, usertype_traits<U*>::metatable(), obj);
  8858. }
  8859. };
  8860. template <>
  8861. struct pusher<detail::as_reference_tag> {
  8862. template <typename T>
  8863. static int push(lua_State* L, T&& obj) {
  8864. return stack::push(L, detail::ptr(obj));
  8865. }
  8866. };
  8867. template <typename T, typename>
  8868. struct pusher {
  8869. template <typename... Args>
  8870. static int push(lua_State* L, Args&&... args) {
  8871. return pusher<detail::as_value_tag<T>>{}.push(L, std::forward<Args>(args)...);
  8872. }
  8873. };
  8874. template <typename T>
  8875. struct pusher<T*, meta::disable_if_t<meta::any<is_container<meta::unqualified_t<T>>, std::is_function<meta::unqualified_t<T>>, is_lua_reference<meta::unqualified_t<T>>>::value>> {
  8876. template <typename... Args>
  8877. static int push(lua_State* L, Args&&... args) {
  8878. return pusher<detail::as_pointer_tag<T>>{}.push(L, std::forward<Args>(args)...);
  8879. }
  8880. };
  8881. template <typename T>
  8882. struct pusher<T, std::enable_if_t<is_unique_usertype<T>::value>> {
  8883. typedef typename unique_usertype_traits<T>::type P;
  8884. typedef typename unique_usertype_traits<T>::actual_type Real;
  8885. template <typename Arg, meta::enable<std::is_base_of<Real, meta::unqualified_t<Arg>>> = meta::enabler>
  8886. static int push(lua_State* L, Arg&& arg) {
  8887. if (unique_usertype_traits<T>::is_null(arg)) {
  8888. return stack::push(L, lua_nil);
  8889. }
  8890. return push_deep(L, std::forward<Arg>(arg));
  8891. }
  8892. template <typename Arg0, typename Arg1, typename... Args>
  8893. static int push(lua_State* L, Arg0&& arg0, Arg0&& arg1, Args&&... args) {
  8894. return push_deep(L, std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...);
  8895. }
  8896. template <typename... Args>
  8897. static int push_deep(lua_State* L, Args&&... args) {
  8898. P** pref = nullptr;
  8899. detail::unique_destructor* fx = nullptr;
  8900. detail::unique_tag* id = nullptr;
  8901. Real* mem = detail::usertype_unique_allocate<P, Real>(L, pref, fx, id);
  8902. *fx = detail::usertype_unique_alloc_destroy<P, Real>;
  8903. #if 0
  8904. *id = &detail::inheritance<P>::type_unique_cast_bases<Real>;
  8905. #else
  8906. *id = &usertype_traits<Real>::qualified_name()[0];
  8907. #endif
  8908. detail::default_construct::construct(mem, std::forward<Args>(args)...);
  8909. *pref = unique_usertype_traits<T>::get(*mem);
  8910. if (luaL_newmetatable(L, &usertype_traits<detail::unique_usertype<std::remove_cv_t<P>>>::metatable()[0]) == 1) {
  8911. luaL_Reg l[32]{};
  8912. int index = 0;
  8913. auto prop_fx = [](meta_function) { return true; };
  8914. usertype_detail::insert_default_registrations<P>(l, index, prop_fx);
  8915. usertype_detail::make_destructor<T>(l, index);
  8916. luaL_setfuncs(L, l, 0);
  8917. }
  8918. lua_setmetatable(L, -2);
  8919. return 1;
  8920. }
  8921. };
  8922. template <typename T>
  8923. struct pusher<std::reference_wrapper<T>> {
  8924. static int push(lua_State* L, const std::reference_wrapper<T>& t) {
  8925. return stack::push(L, std::addressof(detail::deref(t.get())));
  8926. }
  8927. };
  8928. template <typename T>
  8929. struct pusher<T, std::enable_if_t<std::is_floating_point<T>::value>> {
  8930. static int push(lua_State* L, const T& value) {
  8931. lua_pushnumber(L, value);
  8932. return 1;
  8933. }
  8934. };
  8935. template <typename T>
  8936. struct pusher<T, std::enable_if_t<std::is_integral<T>::value>> {
  8937. static int push(lua_State* L, const T& value) {
  8938. #if SOL_LUA_VERSION >= 503
  8939. static auto integer_value_fits = [](T const& value) {
  8940. if (sizeof(T) < sizeof(lua_Integer) || (std::is_signed<T>::value && sizeof(T) == sizeof(lua_Integer))) {
  8941. return true;
  8942. }
  8943. auto u_min = static_cast<std::intmax_t>((std::numeric_limits<lua_Integer>::min)());
  8944. auto u_max = static_cast<std::uintmax_t>((std::numeric_limits<lua_Integer>::max)());
  8945. auto t_min = static_cast<std::intmax_t>((std::numeric_limits<T>::min)());
  8946. auto t_max = static_cast<std::uintmax_t>((std::numeric_limits<T>::max)());
  8947. return (u_min <= t_min || value >= static_cast<T>(u_min)) && (u_max >= t_max || value <= static_cast<T>(u_max));
  8948. };
  8949. if (integer_value_fits(value)) {
  8950. lua_pushinteger(L, static_cast<lua_Integer>(value));
  8951. return 1;
  8952. }
  8953. #endif // Lua 5.3 and above
  8954. #if (defined(SOL_SAFE_NUMERICS) && SOL_SAFE_NUMERICS) && !(defined(SOL_NO_CHECK_NUMBER_PRECISION) && SOL_NO_CHECK_NUMBER_PRECISION)
  8955. if (static_cast<T>(llround(static_cast<lua_Number>(value))) != value) {
  8956. #if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS
  8957. // Is this really worth it?
  8958. assert(false && "integer value will be misrepresented in lua");
  8959. lua_pushnumber(L, static_cast<lua_Number>(value));
  8960. return 1;
  8961. #else
  8962. throw error(detail::direct_error, "integer value will be misrepresented in lua");
  8963. #endif // No Exceptions
  8964. }
  8965. #endif // Safe Numerics and Number Precision Check
  8966. lua_pushnumber(L, static_cast<lua_Number>(value));
  8967. return 1;
  8968. }
  8969. };
  8970. template <typename T>
  8971. struct pusher<T, std::enable_if_t<std::is_enum<T>::value>> {
  8972. static int push(lua_State* L, const T& value) {
  8973. if (std::is_same<char, std::underlying_type_t<T>>::value) {
  8974. return stack::push(L, static_cast<int>(value));
  8975. }
  8976. return stack::push(L, static_cast<std::underlying_type_t<T>>(value));
  8977. }
  8978. };
  8979. template <typename T>
  8980. struct pusher<detail::as_table_tag<T>> {
  8981. static int push(lua_State* L, const T& tablecont) {
  8982. typedef meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>> has_kvp;
  8983. return push(has_kvp(), std::false_type(), L, tablecont);
  8984. }
  8985. static int push(std::true_type, lua_State* L, const T& tablecont) {
  8986. typedef meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>> has_kvp;
  8987. return push(has_kvp(), std::true_type(), L, tablecont);
  8988. }
  8989. static int push(std::false_type, lua_State* L, const T& tablecont) {
  8990. typedef meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>> has_kvp;
  8991. return push(has_kvp(), std::false_type(), L, tablecont);
  8992. }
  8993. template <bool is_nested>
  8994. static int push(std::true_type, std::integral_constant<bool, is_nested>, lua_State* L, const T& tablecont) {
  8995. auto& cont = detail::deref(detail::unwrap(tablecont));
  8996. lua_createtable(L, static_cast<int>(cont.size()), 0);
  8997. int tableindex = lua_gettop(L);
  8998. for (const auto& pair : cont) {
  8999. if (is_nested) {
  9000. set_field(L, pair.first, as_nested_ref(pair.second), tableindex);
  9001. }
  9002. else {
  9003. set_field(L, pair.first, pair.second, tableindex);
  9004. }
  9005. }
  9006. return 1;
  9007. }
  9008. template <bool is_nested>
  9009. static int push(std::false_type, std::integral_constant<bool, is_nested>, lua_State* L, const T& tablecont) {
  9010. auto& cont = detail::deref(detail::unwrap(tablecont));
  9011. lua_createtable(L, stack_detail::get_size_hint(cont), 0);
  9012. int tableindex = lua_gettop(L);
  9013. std::size_t index = 1;
  9014. for (const auto& i : cont) {
  9015. #if SOL_LUA_VERSION >= 503
  9016. int p = is_nested ? stack::push(L, as_nested_ref(i)) : stack::push(L, i);
  9017. for (int pi = 0; pi < p; ++pi) {
  9018. lua_seti(L, tableindex, static_cast<lua_Integer>(index++));
  9019. }
  9020. #else
  9021. lua_pushinteger(L, static_cast<lua_Integer>(index));
  9022. int p = is_nested ? stack::push(L, as_nested_ref(i)) : stack::push(L, i);
  9023. if (p == 1) {
  9024. ++index;
  9025. lua_settable(L, tableindex);
  9026. }
  9027. else {
  9028. int firstindex = tableindex + 1 + 1;
  9029. for (int pi = 0; pi < p; ++pi) {
  9030. stack::push(L, index);
  9031. lua_pushvalue(L, firstindex);
  9032. lua_settable(L, tableindex);
  9033. ++index;
  9034. ++firstindex;
  9035. }
  9036. lua_pop(L, 1 + p);
  9037. }
  9038. #endif // Lua Version 5.3 and others
  9039. }
  9040. // TODO: figure out a better way to do this...?
  9041. //set_field(L, -1, cont.size());
  9042. return 1;
  9043. }
  9044. };
  9045. template <typename T>
  9046. struct pusher<as_table_t<T>, std::enable_if_t<is_container<std::remove_pointer_t<meta::unwrap_unqualified_t<T>>>::value>> {
  9047. static int push(lua_State* L, const T& tablecont) {
  9048. return stack::push<detail::as_table_tag<T>>(L, tablecont);
  9049. }
  9050. };
  9051. template <typename T>
  9052. struct pusher<as_table_t<T>, std::enable_if_t<!is_container<std::remove_pointer_t<meta::unwrap_unqualified_t<T>>>::value>> {
  9053. static int push(lua_State* L, const T& v) {
  9054. return stack::push(L, v);
  9055. }
  9056. };
  9057. template <typename T>
  9058. struct pusher<nested<T>, std::enable_if_t<is_container<std::remove_pointer_t<meta::unwrap_unqualified_t<T>>>::value>> {
  9059. static int push(lua_State* L, const T& tablecont) {
  9060. pusher<detail::as_table_tag<T>> p{};
  9061. // silence annoying VC++ warning
  9062. (void)p;
  9063. return p.push(std::true_type(), L, tablecont);
  9064. }
  9065. };
  9066. template <typename T>
  9067. struct pusher<nested<T>, std::enable_if_t<!is_container<std::remove_pointer_t<meta::unwrap_unqualified_t<T>>>::value>> {
  9068. static int push(lua_State* L, const T& tablecont) {
  9069. pusher<meta::unqualified_t<T>> p{};
  9070. // silence annoying VC++ warning
  9071. (void)p;
  9072. return p.push(L, tablecont);
  9073. }
  9074. };
  9075. template <typename T>
  9076. struct pusher<std::initializer_list<T>> {
  9077. static int push(lua_State* L, const std::initializer_list<T>& il) {
  9078. pusher<detail::as_table_tag<std::initializer_list<T>>> p{};
  9079. // silence annoying VC++ warning
  9080. (void)p;
  9081. return p.push(L, il);
  9082. }
  9083. };
  9084. template <typename T>
  9085. struct pusher<T, std::enable_if_t<is_lua_reference<T>::value>> {
  9086. static int push(lua_State* L, const T& ref) {
  9087. return ref.push(L);
  9088. }
  9089. static int push(lua_State* L, T&& ref) {
  9090. return ref.push(L);
  9091. }
  9092. };
  9093. template <>
  9094. struct pusher<bool> {
  9095. static int push(lua_State* L, bool b) {
  9096. lua_pushboolean(L, b);
  9097. return 1;
  9098. }
  9099. };
  9100. template <>
  9101. struct pusher<lua_nil_t> {
  9102. static int push(lua_State* L, lua_nil_t) {
  9103. lua_pushnil(L);
  9104. return 1;
  9105. }
  9106. };
  9107. template <>
  9108. struct pusher<stack_count> {
  9109. static int push(lua_State*, stack_count st) {
  9110. return st.count;
  9111. }
  9112. };
  9113. template <>
  9114. struct pusher<metatable_t> {
  9115. static int push(lua_State* L, metatable_t) {
  9116. lua_pushlstring(L, "__mt", 4);
  9117. return 1;
  9118. }
  9119. };
  9120. template <>
  9121. struct pusher<std::remove_pointer_t<lua_CFunction>> {
  9122. static int push(lua_State* L, lua_CFunction func, int n = 0) {
  9123. lua_pushcclosure(L, func, n);
  9124. return 1;
  9125. }
  9126. };
  9127. template <>
  9128. struct pusher<lua_CFunction> {
  9129. static int push(lua_State* L, lua_CFunction func, int n = 0) {
  9130. lua_pushcclosure(L, func, n);
  9131. return 1;
  9132. }
  9133. };
  9134. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  9135. template <>
  9136. struct pusher<std::remove_pointer_t<detail::lua_CFunction_noexcept>> {
  9137. static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) {
  9138. lua_pushcclosure(L, func, n);
  9139. return 1;
  9140. }
  9141. };
  9142. template <>
  9143. struct pusher<detail::lua_CFunction_noexcept> {
  9144. static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) {
  9145. lua_pushcclosure(L, func, n);
  9146. return 1;
  9147. }
  9148. };
  9149. #endif // noexcept function type
  9150. template <>
  9151. struct pusher<c_closure> {
  9152. static int push(lua_State* L, c_closure cc) {
  9153. lua_pushcclosure(L, cc.c_function, cc.upvalues);
  9154. return 1;
  9155. }
  9156. };
  9157. template <typename Arg, typename... Args>
  9158. struct pusher<closure<Arg, Args...>> {
  9159. template <std::size_t... I, typename T>
  9160. static int push(std::index_sequence<I...>, lua_State* L, T&& c) {
  9161. int pushcount = multi_push(L, detail::forward_get<I>(c.upvalues)...);
  9162. return stack::push(L, c_closure(c.c_function, pushcount));
  9163. }
  9164. template <typename T>
  9165. static int push(lua_State* L, T&& c) {
  9166. return push(std::make_index_sequence<1 + sizeof...(Args)>(), L, std::forward<T>(c));
  9167. }
  9168. };
  9169. template <>
  9170. struct pusher<void*> {
  9171. static int push(lua_State* L, void* userdata) {
  9172. lua_pushlightuserdata(L, userdata);
  9173. return 1;
  9174. }
  9175. };
  9176. template <>
  9177. struct pusher<const void*> {
  9178. static int push(lua_State* L, const void* userdata) {
  9179. lua_pushlightuserdata(L, const_cast<void*>(userdata));
  9180. return 1;
  9181. }
  9182. };
  9183. template <>
  9184. struct pusher<lightuserdata_value> {
  9185. static int push(lua_State* L, lightuserdata_value userdata) {
  9186. lua_pushlightuserdata(L, userdata);
  9187. return 1;
  9188. }
  9189. };
  9190. template <typename T>
  9191. struct pusher<light<T>> {
  9192. static int push(lua_State* L, light<T> l) {
  9193. lua_pushlightuserdata(L, static_cast<void*>(l.value));
  9194. return 1;
  9195. }
  9196. };
  9197. template <typename T>
  9198. struct pusher<user<T>> {
  9199. template <bool with_meta = true, typename Key, typename... Args>
  9200. static int push_with(lua_State* L, Key&& name, Args&&... args) {
  9201. // A dumb pusher
  9202. T* data = detail::user_allocate<T>(L);
  9203. std::allocator<T> alloc{};
  9204. std::allocator_traits<std::allocator<T>>::construct(alloc, data, std::forward<Args>(args)...);
  9205. if (with_meta) {
  9206. // Make sure we have a plain GC set for this data
  9207. if (luaL_newmetatable(L, name) != 0) {
  9208. lua_CFunction cdel = detail::user_alloc_destruct<T>;
  9209. lua_pushcclosure(L, cdel, 0);
  9210. lua_setfield(L, -2, "__gc");
  9211. }
  9212. lua_setmetatable(L, -2);
  9213. }
  9214. return 1;
  9215. }
  9216. template <typename Arg, typename... Args, meta::disable<meta::any_same<meta::unqualified_t<Arg>, no_metatable_t, metatable_t>> = meta::enabler>
  9217. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  9218. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  9219. return push_with(L, name, std::forward<Arg>(arg), std::forward<Args>(args)...);
  9220. }
  9221. template <typename... Args>
  9222. static int push(lua_State* L, no_metatable_t, Args&&... args) {
  9223. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  9224. return push_with<false>(L, name, std::forward<Args>(args)...);
  9225. }
  9226. template <typename Key, typename... Args>
  9227. static int push(lua_State* L, metatable_t, Key&& key, Args&&... args) {
  9228. const auto name = &key[0];
  9229. return push_with<true>(L, name, std::forward<Args>(args)...);
  9230. }
  9231. static int push(lua_State* L, const user<T>& u) {
  9232. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  9233. return push_with(L, name, u.value);
  9234. }
  9235. static int push(lua_State* L, user<T>&& u) {
  9236. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  9237. return push_with(L, name, std::move(u.value));
  9238. }
  9239. static int push(lua_State* L, no_metatable_t, const user<T>& u) {
  9240. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  9241. return push_with<false>(L, name, u.value);
  9242. }
  9243. static int push(lua_State* L, no_metatable_t, user<T>&& u) {
  9244. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  9245. return push_with<false>(L, name, std::move(u.value));
  9246. }
  9247. };
  9248. template <>
  9249. struct pusher<userdata_value> {
  9250. static int push(lua_State* L, userdata_value data) {
  9251. void** ud = detail::usertype_allocate_pointer<void>(L);
  9252. *ud = data.value;
  9253. return 1;
  9254. }
  9255. };
  9256. template <>
  9257. struct pusher<const char*> {
  9258. static int push_sized(lua_State* L, const char* str, std::size_t len) {
  9259. lua_pushlstring(L, str, len);
  9260. return 1;
  9261. }
  9262. static int push(lua_State* L, const char* str) {
  9263. if (str == nullptr)
  9264. return stack::push(L, lua_nil);
  9265. return push_sized(L, str, std::char_traits<char>::length(str));
  9266. }
  9267. static int push(lua_State* L, const char* strb, const char* stre) {
  9268. return push_sized(L, strb, stre - strb);
  9269. }
  9270. static int push(lua_State* L, const char* str, std::size_t len) {
  9271. return push_sized(L, str, len);
  9272. }
  9273. };
  9274. template <>
  9275. struct pusher<char*> {
  9276. static int push_sized(lua_State* L, const char* str, std::size_t len) {
  9277. pusher<const char*> p{};
  9278. (void)p;
  9279. return p.push_sized(L, str, len);
  9280. }
  9281. static int push(lua_State* L, const char* str) {
  9282. pusher<const char*> p{};
  9283. (void)p;
  9284. return p.push(L, str);
  9285. }
  9286. static int push(lua_State* L, const char* strb, const char* stre) {
  9287. pusher<const char*> p{};
  9288. (void)p;
  9289. return p.push(L, strb, stre);
  9290. }
  9291. static int push(lua_State* L, const char* str, std::size_t len) {
  9292. pusher<const char*> p{};
  9293. (void)p;
  9294. return p.push(L, str, len);
  9295. }
  9296. };
  9297. template <size_t N>
  9298. struct pusher<char[N]> {
  9299. static int push(lua_State* L, const char (&str)[N]) {
  9300. lua_pushlstring(L, str, std::char_traits<char>::length(str));
  9301. return 1;
  9302. }
  9303. static int push(lua_State* L, const char (&str)[N], std::size_t sz) {
  9304. lua_pushlstring(L, str, sz);
  9305. return 1;
  9306. }
  9307. };
  9308. template <>
  9309. struct pusher<char> {
  9310. static int push(lua_State* L, char c) {
  9311. const char str[2] = { c, '\0' };
  9312. return stack::push(L, str, 1);
  9313. }
  9314. };
  9315. template <typename Traits, typename Al>
  9316. struct pusher<std::basic_string<char, Traits, Al>> {
  9317. static int push(lua_State* L, const std::basic_string<char, Traits, Al>& str) {
  9318. lua_pushlstring(L, str.c_str(), str.size());
  9319. return 1;
  9320. }
  9321. static int push(lua_State* L, const std::basic_string<char, Traits, Al>& str, std::size_t sz) {
  9322. lua_pushlstring(L, str.c_str(), sz);
  9323. return 1;
  9324. }
  9325. };
  9326. template <typename Ch, typename Traits>
  9327. struct pusher<basic_string_view<Ch, Traits>> {
  9328. static int push(lua_State* L, const basic_string_view<Ch, Traits>& sv) {
  9329. return stack::push(L, sv.data(), sv.length());
  9330. }
  9331. static int push(lua_State* L, const basic_string_view<Ch, Traits>& sv, std::size_t n) {
  9332. return stack::push(L, sv.data(), n);
  9333. }
  9334. };
  9335. template <>
  9336. struct pusher<meta_function> {
  9337. static int push(lua_State* L, meta_function m) {
  9338. const std::string& str = to_string(m);
  9339. lua_pushlstring(L, str.c_str(), str.size());
  9340. return 1;
  9341. }
  9342. };
  9343. template <>
  9344. struct pusher<absolute_index> {
  9345. static int push(lua_State* L, absolute_index ai) {
  9346. lua_pushvalue(L, ai);
  9347. return 1;
  9348. }
  9349. };
  9350. template <>
  9351. struct pusher<raw_index> {
  9352. static int push(lua_State* L, raw_index ri) {
  9353. lua_pushvalue(L, ri);
  9354. return 1;
  9355. }
  9356. };
  9357. template <>
  9358. struct pusher<ref_index> {
  9359. static int push(lua_State* L, ref_index ri) {
  9360. lua_rawgeti(L, LUA_REGISTRYINDEX, ri);
  9361. return 1;
  9362. }
  9363. };
  9364. template <>
  9365. struct pusher<const wchar_t*> {
  9366. static int push(lua_State* L, const wchar_t* wstr) {
  9367. return push(L, wstr, std::char_traits<wchar_t>::length(wstr));
  9368. }
  9369. static int push(lua_State* L, const wchar_t* wstr, std::size_t sz) {
  9370. return push(L, wstr, wstr + sz);
  9371. }
  9372. static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
  9373. if (sizeof(wchar_t) == 2) {
  9374. const char16_t* sb = reinterpret_cast<const char16_t*>(strb);
  9375. const char16_t* se = reinterpret_cast<const char16_t*>(stre);
  9376. return stack::push(L, sb, se);
  9377. }
  9378. const char32_t* sb = reinterpret_cast<const char32_t*>(strb);
  9379. const char32_t* se = reinterpret_cast<const char32_t*>(stre);
  9380. return stack::push(L, sb, se);
  9381. }
  9382. };
  9383. template <>
  9384. struct pusher<wchar_t*> {
  9385. static int push(lua_State* L, const wchar_t* str) {
  9386. pusher<const wchar_t*> p{};
  9387. (void)p;
  9388. return p.push(L, str);
  9389. }
  9390. static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
  9391. pusher<const wchar_t*> p{};
  9392. (void)p;
  9393. return p.push(L, strb, stre);
  9394. }
  9395. static int push(lua_State* L, const wchar_t* str, std::size_t len) {
  9396. pusher<const wchar_t*> p{};
  9397. (void)p;
  9398. return p.push(L, str, len);
  9399. }
  9400. };
  9401. template <>
  9402. struct pusher<const char16_t*> {
  9403. static int convert_into(lua_State* L, char* start, std::size_t, const char16_t* strb, const char16_t* stre) {
  9404. char* target = start;
  9405. char32_t cp = 0;
  9406. for (const char16_t* strtarget = strb; strtarget < stre;) {
  9407. auto dr = unicode::utf16_to_code_point(strtarget, stre);
  9408. if (dr.error != unicode::error_code::ok) {
  9409. cp = unicode::unicode_detail::replacement;
  9410. }
  9411. else {
  9412. cp = dr.codepoint;
  9413. }
  9414. auto er = unicode::code_point_to_utf8(cp);
  9415. const char* utf8data = er.code_units.data();
  9416. std::memcpy(target, utf8data, er.code_units_size);
  9417. target += er.code_units_size;
  9418. strtarget = dr.next;
  9419. }
  9420. return stack::push(L, start, target);
  9421. }
  9422. static int push(lua_State* L, const char16_t* u16str) {
  9423. return push(L, u16str, std::char_traits<char16_t>::length(u16str));
  9424. }
  9425. static int push(lua_State* L, const char16_t* u16str, std::size_t sz) {
  9426. return push(L, u16str, u16str + sz);
  9427. }
  9428. static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
  9429. // TODO: use new unicode methods
  9430. // TODO: use new unicode methods
  9431. char sbo[SOL_STACK_STRING_OPTIMIZATION_SIZE];
  9432. // if our max string space is small enough, use SBO
  9433. // right off the bat
  9434. std::size_t max_possible_code_units = (stre - strb) * 4;
  9435. if (max_possible_code_units <= SOL_STACK_STRING_OPTIMIZATION_SIZE) {
  9436. return convert_into(L, sbo, max_possible_code_units, strb, stre);
  9437. }
  9438. // otherwise, we must manually count/check size
  9439. std::size_t needed_size = 0;
  9440. for (const char16_t* strtarget = strb; strtarget < stre;) {
  9441. auto dr = unicode::utf16_to_code_point(strtarget, stre);
  9442. auto er = unicode::code_point_to_utf8(dr.codepoint);
  9443. needed_size += er.code_units_size;
  9444. strtarget = dr.next;
  9445. }
  9446. if (needed_size < SOL_STACK_STRING_OPTIMIZATION_SIZE) {
  9447. return convert_into(L, sbo, needed_size, strb, stre);
  9448. }
  9449. std::string u8str("", 0);
  9450. u8str.resize(needed_size);
  9451. char* target = &u8str[0];
  9452. return convert_into(L, target, needed_size, strb, stre);
  9453. }
  9454. };
  9455. template <>
  9456. struct pusher<char16_t*> {
  9457. static int push(lua_State* L, const char16_t* str) {
  9458. pusher<const char16_t*> p{};
  9459. (void)p;
  9460. return p.push(L, str);
  9461. }
  9462. static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
  9463. pusher<const char16_t*> p{};
  9464. (void)p;
  9465. return p.push(L, strb, stre);
  9466. }
  9467. static int push(lua_State* L, const char16_t* str, std::size_t len) {
  9468. pusher<const char16_t*> p{};
  9469. (void)p;
  9470. return p.push(L, str, len);
  9471. }
  9472. };
  9473. template <>
  9474. struct pusher<const char32_t*> {
  9475. static int convert_into(lua_State* L, char* start, std::size_t, const char32_t* strb, const char32_t* stre) {
  9476. char* target = start;
  9477. char32_t cp = 0;
  9478. for (const char32_t* strtarget = strb; strtarget < stre;) {
  9479. auto dr = unicode::utf32_to_code_point(strtarget, stre);
  9480. if (dr.error != unicode::error_code::ok) {
  9481. cp = unicode::unicode_detail::replacement;
  9482. }
  9483. else {
  9484. cp = dr.codepoint;
  9485. }
  9486. auto er = unicode::code_point_to_utf8(cp);
  9487. const char* data = er.code_units.data();
  9488. std::memcpy(target, data, er.code_units_size);
  9489. target += er.code_units_size;
  9490. strtarget = dr.next;
  9491. }
  9492. return stack::push(L, start, target);
  9493. }
  9494. static int push(lua_State* L, const char32_t* u32str) {
  9495. return push(L, u32str, u32str + std::char_traits<char32_t>::length(u32str));
  9496. }
  9497. static int push(lua_State* L, const char32_t* u32str, std::size_t sz) {
  9498. return push(L, u32str, u32str + sz);
  9499. }
  9500. static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
  9501. // TODO: use new unicode methods
  9502. char sbo[SOL_STACK_STRING_OPTIMIZATION_SIZE];
  9503. // if our max string space is small enough, use SBO
  9504. // right off the bat
  9505. std::size_t max_possible_code_units = (stre - strb) * 4;
  9506. if (max_possible_code_units <= SOL_STACK_STRING_OPTIMIZATION_SIZE) {
  9507. return convert_into(L, sbo, max_possible_code_units, strb, stre);
  9508. }
  9509. // otherwise, we must manually count/check size
  9510. std::size_t needed_size = 0;
  9511. for (const char32_t* strtarget = strb; strtarget < stre;) {
  9512. auto dr = unicode::utf32_to_code_point(strtarget, stre);
  9513. auto er = unicode::code_point_to_utf8(dr.codepoint);
  9514. needed_size += er.code_units_size;
  9515. strtarget = dr.next;
  9516. }
  9517. if (needed_size < SOL_STACK_STRING_OPTIMIZATION_SIZE) {
  9518. return convert_into(L, sbo, needed_size, strb, stre);
  9519. }
  9520. std::string u8str("", 0);
  9521. u8str.resize(needed_size);
  9522. char* target = &u8str[0];
  9523. return convert_into(L, target, needed_size, strb, stre);
  9524. }
  9525. };
  9526. template <>
  9527. struct pusher<char32_t*> {
  9528. static int push(lua_State* L, const char32_t* str) {
  9529. pusher<const char32_t*> p{};
  9530. (void)p;
  9531. return p.push(L, str);
  9532. }
  9533. static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
  9534. pusher<const char32_t*> p{};
  9535. (void)p;
  9536. return p.push(L, strb, stre);
  9537. }
  9538. static int push(lua_State* L, const char32_t* str, std::size_t len) {
  9539. pusher<const char32_t*> p{};
  9540. (void)p;
  9541. return p.push(L, str, len);
  9542. }
  9543. };
  9544. template <size_t N>
  9545. struct pusher<wchar_t[N]> {
  9546. static int push(lua_State* L, const wchar_t (&str)[N]) {
  9547. return push(L, str, std::char_traits<wchar_t>::length(str));
  9548. }
  9549. static int push(lua_State* L, const wchar_t (&str)[N], std::size_t sz) {
  9550. return stack::push<const wchar_t*>(L, str, str + sz);
  9551. }
  9552. };
  9553. template <size_t N>
  9554. struct pusher<char16_t[N]> {
  9555. static int push(lua_State* L, const char16_t (&str)[N]) {
  9556. return push(L, str, std::char_traits<char16_t>::length(str));
  9557. }
  9558. static int push(lua_State* L, const char16_t (&str)[N], std::size_t sz) {
  9559. return stack::push<const char16_t*>(L, str, str + sz);
  9560. }
  9561. };
  9562. template <size_t N>
  9563. struct pusher<char32_t[N]> {
  9564. static int push(lua_State* L, const char32_t (&str)[N]) {
  9565. return push(L, str, std::char_traits<char32_t>::length(str));
  9566. }
  9567. static int push(lua_State* L, const char32_t (&str)[N], std::size_t sz) {
  9568. return stack::push<const char32_t*>(L, str, str + sz);
  9569. }
  9570. };
  9571. template <>
  9572. struct pusher<wchar_t> {
  9573. static int push(lua_State* L, wchar_t c) {
  9574. const wchar_t str[2] = { c, '\0' };
  9575. return stack::push(L, &str[0], 1);
  9576. }
  9577. };
  9578. template <>
  9579. struct pusher<char16_t> {
  9580. static int push(lua_State* L, char16_t c) {
  9581. const char16_t str[2] = { c, '\0' };
  9582. return stack::push(L, &str[0], 1);
  9583. }
  9584. };
  9585. template <>
  9586. struct pusher<char32_t> {
  9587. static int push(lua_State* L, char32_t c) {
  9588. const char32_t str[2] = { c, '\0' };
  9589. return stack::push(L, &str[0], 1);
  9590. }
  9591. };
  9592. template <typename Ch, typename Traits, typename Al>
  9593. struct pusher<std::basic_string<Ch, Traits, Al>, std::enable_if_t<!std::is_same<Ch, char>::value>> {
  9594. static int push(lua_State* L, const std::basic_string<Ch, Traits, Al>& wstr) {
  9595. return push(L, wstr, wstr.size());
  9596. }
  9597. static int push(lua_State* L, const std::basic_string<Ch, Traits, Al>& wstr, std::size_t sz) {
  9598. return stack::push(L, wstr.data(), wstr.data() + sz);
  9599. }
  9600. };
  9601. template <typename... Args>
  9602. struct pusher<std::tuple<Args...>> {
  9603. template <std::size_t... I, typename T>
  9604. static int push(std::index_sequence<I...>, lua_State* L, T&& t) {
  9605. int pushcount = 0;
  9606. (void)detail::swallow{ 0, (pushcount += stack::push(L, detail::forward_get<I>(t)), 0)... };
  9607. return pushcount;
  9608. }
  9609. template <typename T>
  9610. static int push(lua_State* L, T&& t) {
  9611. return push(std::index_sequence_for<Args...>(), L, std::forward<T>(t));
  9612. }
  9613. };
  9614. template <typename A, typename B>
  9615. struct pusher<std::pair<A, B>> {
  9616. template <typename T>
  9617. static int push(lua_State* L, T&& t) {
  9618. int pushcount = stack::push(L, detail::forward_get<0>(t));
  9619. pushcount += stack::push(L, detail::forward_get<1>(t));
  9620. return pushcount;
  9621. }
  9622. };
  9623. template <typename O>
  9624. struct pusher<optional<O>> {
  9625. template <typename T>
  9626. static int push(lua_State* L, T&& t) {
  9627. if (t == nullopt) {
  9628. return stack::push(L, nullopt);
  9629. }
  9630. return stack::push(L, static_cast<std::conditional_t<std::is_lvalue_reference<T>::value, O&, O&&>>(t.value()));
  9631. }
  9632. };
  9633. template <>
  9634. struct pusher<nullopt_t> {
  9635. static int push(lua_State* L, nullopt_t) {
  9636. return stack::push(L, lua_nil);
  9637. }
  9638. };
  9639. template <>
  9640. struct pusher<std::nullptr_t> {
  9641. static int push(lua_State* L, std::nullptr_t) {
  9642. return stack::push(L, lua_nil);
  9643. }
  9644. };
  9645. template <>
  9646. struct pusher<this_state> {
  9647. static int push(lua_State*, const this_state&) {
  9648. return 0;
  9649. }
  9650. };
  9651. template <>
  9652. struct pusher<this_main_state> {
  9653. static int push(lua_State*, const this_main_state&) {
  9654. return 0;
  9655. }
  9656. };
  9657. template <>
  9658. struct pusher<new_table> {
  9659. static int push(lua_State* L, const new_table& nt) {
  9660. lua_createtable(L, nt.sequence_hint, nt.map_hint);
  9661. return 1;
  9662. }
  9663. };
  9664. #if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
  9665. template <typename O>
  9666. struct pusher<std::optional<O>> {
  9667. template <typename T>
  9668. static int push(lua_State* L, T&& t) {
  9669. if (t == std::nullopt) {
  9670. return stack::push(L, nullopt);
  9671. }
  9672. return stack::push(L, static_cast<std::conditional_t<std::is_lvalue_reference<T>::value, O&, O&&>>(t.value()));
  9673. }
  9674. };
  9675. #if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
  9676. namespace stack_detail {
  9677. struct push_function {
  9678. lua_State* L;
  9679. push_function(lua_State* L)
  9680. : L(L) {
  9681. }
  9682. template <typename T>
  9683. int operator()(T&& value) const {
  9684. return stack::push<T>(L, std::forward<T>(value));
  9685. }
  9686. };
  9687. } // namespace stack_detail
  9688. template <typename... Tn>
  9689. struct pusher<std::variant<Tn...>> {
  9690. static int push(lua_State* L, const std::variant<Tn...>& v) {
  9691. return std::visit(stack_detail::push_function(L), v);
  9692. }
  9693. static int push(lua_State* L, std::variant<Tn...>&& v) {
  9694. return std::visit(stack_detail::push_function(L), std::move(v));
  9695. }
  9696. };
  9697. #endif // Variant because Clang is terrible
  9698. #endif // C++17 Support
  9699. }
  9700. } // namespace sol::stack
  9701. // end of sol/stack_push.hpp
  9702. // beginning of sol/stack_pop.hpp
  9703. namespace sol {
  9704. namespace stack {
  9705. template <typename T, typename>
  9706. struct popper {
  9707. inline static decltype(auto) pop(lua_State* L) {
  9708. record tracking{};
  9709. #ifdef __INTEL_COMPILER
  9710. auto&& r = get<T>(L, -lua_size<T>::value, tracking);
  9711. #else
  9712. decltype(auto) r = get<T>(L, -lua_size<T>::value, tracking);
  9713. #endif
  9714. lua_pop(L, tracking.used);
  9715. return r;
  9716. }
  9717. };
  9718. template <typename T>
  9719. struct popper<T, std::enable_if_t<is_stack_based<meta::unqualified_t<T>>::value>> {
  9720. static_assert(meta::neg<is_stack_based<meta::unqualified_t<T>>>::value, "You cannot pop something that lives solely on the stack: it will not remain on the stack when popped and thusly will go out of scope!");
  9721. };
  9722. }
  9723. } // namespace sol::stack
  9724. // end of sol/stack_pop.hpp
  9725. // beginning of sol/stack_field.hpp
  9726. namespace sol {
  9727. namespace stack {
  9728. template <typename T, bool, bool, typename>
  9729. struct field_getter {
  9730. template <typename Key>
  9731. void get(lua_State* L, Key&& key, int tableindex = -2) {
  9732. push(L, std::forward<Key>(key));
  9733. lua_gettable(L, tableindex);
  9734. }
  9735. };
  9736. template <typename T, bool global, typename C>
  9737. struct field_getter<T, global, true, C> {
  9738. template <typename Key>
  9739. void get(lua_State* L, Key&& key, int tableindex = -2) {
  9740. push(L, std::forward<Key>(key));
  9741. lua_rawget(L, tableindex);
  9742. }
  9743. };
  9744. template <bool b, bool raw, typename C>
  9745. struct field_getter<metatable_t, b, raw, C> {
  9746. void get(lua_State* L, metatable_t, int tableindex = -1) {
  9747. if (lua_getmetatable(L, tableindex) == 0)
  9748. push(L, lua_nil);
  9749. }
  9750. };
  9751. template <bool b, bool raw, typename C>
  9752. struct field_getter<env_t, b, raw, C> {
  9753. void get(lua_State* L, env_t, int tableindex = -1) {
  9754. #if SOL_LUA_VERSION < 502
  9755. // Use lua_setfenv
  9756. lua_getfenv(L, tableindex);
  9757. #else
  9758. // Use upvalues as explained in Lua 5.2 and beyond's manual
  9759. if (lua_getupvalue(L, tableindex, 1) == nullptr) {
  9760. push(L, lua_nil);
  9761. }
  9762. #endif
  9763. }
  9764. };
  9765. template <typename T, bool raw>
  9766. struct field_getter<T, true, raw, std::enable_if_t<meta::is_c_str<T>::value>> {
  9767. template <typename Key>
  9768. void get(lua_State* L, Key&& key, int = -1) {
  9769. lua_getglobal(L, &key[0]);
  9770. }
  9771. };
  9772. template <typename T>
  9773. struct field_getter<T, false, false, std::enable_if_t<meta::is_c_str<T>::value>> {
  9774. template <typename Key>
  9775. void get(lua_State* L, Key&& key, int tableindex = -1) {
  9776. lua_getfield(L, tableindex, &key[0]);
  9777. }
  9778. };
  9779. #if SOL_LUA_VERSION >= 503
  9780. template <typename T>
  9781. struct field_getter<T, false, false, std::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value>> {
  9782. template <typename Key>
  9783. void get(lua_State* L, Key&& key, int tableindex = -1) {
  9784. lua_geti(L, tableindex, static_cast<lua_Integer>(key));
  9785. }
  9786. };
  9787. #endif // Lua 5.3.x
  9788. #if SOL_LUA_VERSION >= 502
  9789. template <typename C>
  9790. struct field_getter<void*, false, true, C> {
  9791. void get(lua_State* L, void* key, int tableindex = -1) {
  9792. lua_rawgetp(L, tableindex, key);
  9793. }
  9794. };
  9795. #endif // Lua 5.3.x
  9796. template <typename T>
  9797. struct field_getter<T, false, true, std::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value>> {
  9798. template <typename Key>
  9799. void get(lua_State* L, Key&& key, int tableindex = -1) {
  9800. lua_rawgeti(L, tableindex, static_cast<lua_Integer>(key));
  9801. }
  9802. };
  9803. template <typename... Args, bool b, bool raw, typename C>
  9804. struct field_getter<std::tuple<Args...>, b, raw, C> {
  9805. template <std::size_t... I, typename Keys>
  9806. void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) {
  9807. get_field<b, raw>(L, detail::forward_get<0>(keys), tableindex);
  9808. void(detail::swallow{(get_field<false, raw>(L, detail::forward_get<I>(keys)), 0)...});
  9809. reference saved(L, -1);
  9810. lua_pop(L, static_cast<int>(sizeof...(I)));
  9811. saved.push();
  9812. }
  9813. template <typename Keys>
  9814. void get(lua_State* L, Keys&& keys) {
  9815. apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), lua_absindex(L, -1));
  9816. }
  9817. template <typename Keys>
  9818. void get(lua_State* L, Keys&& keys, int tableindex) {
  9819. apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), tableindex);
  9820. }
  9821. };
  9822. template <typename A, typename B, bool b, bool raw, typename C>
  9823. struct field_getter<std::pair<A, B>, b, raw, C> {
  9824. template <typename Keys>
  9825. void get(lua_State* L, Keys&& keys, int tableindex) {
  9826. get_field<b, raw>(L, detail::forward_get<0>(keys), tableindex);
  9827. get_field<false, raw>(L, detail::forward_get<1>(keys));
  9828. reference saved(L, -1);
  9829. lua_pop(L, static_cast<int>(2));
  9830. saved.push();
  9831. }
  9832. template <typename Keys>
  9833. void get(lua_State* L, Keys&& keys) {
  9834. get_field<b, raw>(L, detail::forward_get<0>(keys));
  9835. get_field<false, raw>(L, detail::forward_get<1>(keys));
  9836. reference saved(L, -1);
  9837. lua_pop(L, static_cast<int>(2));
  9838. saved.push();
  9839. }
  9840. };
  9841. template <typename T, bool, bool, typename>
  9842. struct field_setter {
  9843. template <typename Key, typename Value>
  9844. void set(lua_State* L, Key&& key, Value&& value, int tableindex = -3) {
  9845. push(L, std::forward<Key>(key));
  9846. push(L, std::forward<Value>(value));
  9847. lua_settable(L, tableindex);
  9848. }
  9849. };
  9850. template <typename T, bool b, typename C>
  9851. struct field_setter<T, b, true, C> {
  9852. template <typename Key, typename Value>
  9853. void set(lua_State* L, Key&& key, Value&& value, int tableindex = -3) {
  9854. push(L, std::forward<Key>(key));
  9855. push(L, std::forward<Value>(value));
  9856. lua_rawset(L, tableindex);
  9857. }
  9858. };
  9859. template <bool b, bool raw, typename C>
  9860. struct field_setter<metatable_t, b, raw, C> {
  9861. template <typename Value>
  9862. void set(lua_State* L, metatable_t, Value&& value, int tableindex = -2) {
  9863. push(L, std::forward<Value>(value));
  9864. lua_setmetatable(L, tableindex);
  9865. }
  9866. };
  9867. template <typename T, bool raw>
  9868. struct field_setter<T, true, raw, std::enable_if_t<meta::is_c_str<T>::value>> {
  9869. template <typename Key, typename Value>
  9870. void set(lua_State* L, Key&& key, Value&& value, int = -2) {
  9871. push(L, std::forward<Value>(value));
  9872. lua_setglobal(L, &key[0]);
  9873. }
  9874. };
  9875. template <typename T>
  9876. struct field_setter<T, false, false, std::enable_if_t<meta::is_c_str<T>::value>> {
  9877. template <typename Key, typename Value>
  9878. void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) {
  9879. push(L, std::forward<Value>(value));
  9880. lua_setfield(L, tableindex, &key[0]);
  9881. }
  9882. };
  9883. #if SOL_LUA_VERSION >= 503
  9884. template <typename T>
  9885. struct field_setter<T, false, false, std::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value>> {
  9886. template <typename Key, typename Value>
  9887. void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) {
  9888. push(L, std::forward<Value>(value));
  9889. lua_seti(L, tableindex, static_cast<lua_Integer>(key));
  9890. }
  9891. };
  9892. #endif // Lua 5.3.x
  9893. template <typename T>
  9894. struct field_setter<T, false, true, std::enable_if_t<std::is_integral<T>::value && !std::is_same<bool, T>::value>> {
  9895. template <typename Key, typename Value>
  9896. void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) {
  9897. push(L, std::forward<Value>(value));
  9898. lua_rawseti(L, tableindex, static_cast<lua_Integer>(key));
  9899. }
  9900. };
  9901. #if SOL_LUA_VERSION >= 502
  9902. template <typename C>
  9903. struct field_setter<void*, false, true, C> {
  9904. template <typename Key, typename Value>
  9905. void set(lua_State* L, void* key, Value&& value, int tableindex = -2) {
  9906. push(L, std::forward<Value>(value));
  9907. lua_rawsetp(L, tableindex, key);
  9908. }
  9909. };
  9910. #endif // Lua 5.2.x
  9911. template <typename... Args, bool b, bool raw, typename C>
  9912. struct field_setter<std::tuple<Args...>, b, raw, C> {
  9913. template <bool g, std::size_t I, typename Key, typename Value>
  9914. void apply(std::index_sequence<I>, lua_State* L, Key&& keys, Value&& value, int tableindex) {
  9915. I < 1 ? set_field<g, raw>(L, detail::forward_get<I>(keys), std::forward<Value>(value), tableindex) : set_field<g, raw>(L, detail::forward_get<I>(keys), std::forward<Value>(value));
  9916. }
  9917. template <bool g, std::size_t I0, std::size_t I1, std::size_t... I, typename Keys, typename Value>
  9918. void apply(std::index_sequence<I0, I1, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  9919. I0 < 1 ? get_field<g, raw>(L, detail::forward_get<I0>(keys), tableindex) : get_field<g, raw>(L, detail::forward_get<I0>(keys), -1);
  9920. apply<false>(std::index_sequence<I1, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), -1);
  9921. }
  9922. template <bool g, std::size_t I0, std::size_t... I, typename Keys, typename Value>
  9923. void top_apply(std::index_sequence<I0, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  9924. apply<g>(std::index_sequence<I0, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
  9925. lua_pop(L, static_cast<int>(sizeof...(I)));
  9926. }
  9927. template <typename Keys, typename Value>
  9928. void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -3) {
  9929. top_apply<b>(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
  9930. }
  9931. };
  9932. template <typename A, typename B, bool b, bool raw, typename C>
  9933. struct field_setter<std::pair<A, B>, b, raw, C> {
  9934. template <typename Keys, typename Value>
  9935. void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -1) {
  9936. get_field<b, raw>(L, detail::forward_get<0>(keys), tableindex);
  9937. set_field<false, raw>(L, detail::forward_get<1>(keys), std::forward<Value>(value));
  9938. lua_pop(L, 1);
  9939. }
  9940. };
  9941. }
  9942. } // namespace sol::stack
  9943. // end of sol/stack_field.hpp
  9944. // beginning of sol/stack_probe.hpp
  9945. namespace sol {
  9946. namespace stack {
  9947. template <typename T, typename P, bool b, bool raw, typename>
  9948. struct probe_field_getter {
  9949. template <typename Key>
  9950. probe get(lua_State* L, Key&& key, int tableindex = -2) {
  9951. if (!b && !maybe_indexable(L, tableindex)) {
  9952. return probe(false, 0);
  9953. }
  9954. get_field<b, raw>(L, std::forward<Key>(key), tableindex);
  9955. return probe(check<P>(L), 1);
  9956. }
  9957. };
  9958. template <typename A, typename B, typename P, bool b, bool raw, typename C>
  9959. struct probe_field_getter<std::pair<A, B>, P, b, raw, C> {
  9960. template <typename Keys>
  9961. probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
  9962. if (!b && !maybe_indexable(L, tableindex)) {
  9963. return probe(false, 0);
  9964. }
  9965. get_field<b, raw>(L, std::get<0>(keys), tableindex);
  9966. if (!maybe_indexable(L)) {
  9967. return probe(false, 1);
  9968. }
  9969. get_field<false, raw>(L, std::get<1>(keys), tableindex);
  9970. return probe(check<P>(L), 2);
  9971. }
  9972. };
  9973. template <typename... Args, typename P, bool b, bool raw, typename C>
  9974. struct probe_field_getter<std::tuple<Args...>, P, b, raw, C> {
  9975. template <std::size_t I, typename Keys>
  9976. probe apply(std::index_sequence<I>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
  9977. get_field < I<1 && b, raw>(L, std::get<I>(keys), tableindex);
  9978. return probe(check<P>(L), sofar);
  9979. }
  9980. template <std::size_t I, std::size_t I1, std::size_t... In, typename Keys>
  9981. probe apply(std::index_sequence<I, I1, In...>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
  9982. get_field < I<1 && b, raw>(L, std::get<I>(keys), tableindex);
  9983. if (!maybe_indexable(L)) {
  9984. return probe(false, sofar);
  9985. }
  9986. return apply(std::index_sequence<I1, In...>(), sofar + 1, L, std::forward<Keys>(keys), -1);
  9987. }
  9988. template <typename Keys>
  9989. probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
  9990. if (!b && !maybe_indexable(L, tableindex)) {
  9991. return probe(false, 0);
  9992. }
  9993. return apply(std::index_sequence_for<Args...>(), 1, L, std::forward<Keys>(keys), tableindex);
  9994. }
  9995. };
  9996. }
  9997. } // namespace sol::stack
  9998. // end of sol/stack_probe.hpp
  9999. namespace sol {
  10000. namespace detail {
  10001. using typical_chunk_name_t = char[32];
  10002. inline const std::string& default_chunk_name() {
  10003. static const std::string name = "";
  10004. return name;
  10005. }
  10006. template <std::size_t N>
  10007. const char* make_chunk_name(const string_view& code, const std::string& chunkname, char (&basechunkname)[N]) {
  10008. if (chunkname.empty()) {
  10009. auto it = code.cbegin();
  10010. auto e = code.cend();
  10011. std::size_t i = 0;
  10012. static const std::size_t n = N - 4;
  10013. for (i = 0; i < n && it != e; ++i, ++it) {
  10014. basechunkname[i] = *it;
  10015. }
  10016. if (it != e) {
  10017. for (std::size_t c = 0; c < 3; ++i, ++c) {
  10018. basechunkname[i] = '.';
  10019. }
  10020. }
  10021. basechunkname[i] = '\0';
  10022. return &basechunkname[0];
  10023. }
  10024. else {
  10025. return chunkname.c_str();
  10026. }
  10027. }
  10028. } // namespace detail
  10029. namespace stack {
  10030. namespace stack_detail {
  10031. template <typename T>
  10032. inline int push_as_upvalues(lua_State* L, T& item) {
  10033. typedef std::decay_t<T> TValue;
  10034. static const std::size_t itemsize = sizeof(TValue);
  10035. static const std::size_t voidsize = sizeof(void*);
  10036. static const std::size_t voidsizem1 = voidsize - 1;
  10037. static const std::size_t data_t_count = (sizeof(TValue) + voidsizem1) / voidsize;
  10038. typedef std::array<void*, data_t_count> data_t;
  10039. data_t data{ {} };
  10040. std::memcpy(&data[0], std::addressof(item), itemsize);
  10041. int pushcount = 0;
  10042. for (auto&& v : data) {
  10043. pushcount += push(L, lightuserdata_value(v));
  10044. }
  10045. return pushcount;
  10046. }
  10047. template <typename T>
  10048. inline std::pair<T, int> get_as_upvalues(lua_State* L, int index = 2) {
  10049. static const std::size_t data_t_count = (sizeof(T) + (sizeof(void*) - 1)) / sizeof(void*);
  10050. typedef std::array<void*, data_t_count> data_t;
  10051. data_t voiddata{ {} };
  10052. for (std::size_t i = 0, d = 0; d < sizeof(T); ++i, d += sizeof(void*)) {
  10053. voiddata[i] = get<lightuserdata_value>(L, upvalue_index(index++));
  10054. }
  10055. return std::pair<T, int>(*reinterpret_cast<T*>(static_cast<void*>(voiddata.data())), index);
  10056. }
  10057. struct evaluator {
  10058. template <typename Fx, typename... Args>
  10059. static decltype(auto) eval(types<>, std::index_sequence<>, lua_State*, int, record&, Fx&& fx, Args&&... args) {
  10060. return std::forward<Fx>(fx)(std::forward<Args>(args)...);
  10061. }
  10062. template <typename Fx, typename Arg, typename... Args, std::size_t I, std::size_t... Is, typename... FxArgs>
  10063. static decltype(auto) eval(types<Arg, Args...>, std::index_sequence<I, Is...>, lua_State* L, int start, record& tracking, Fx&& fx, FxArgs&&... fxargs) {
  10064. return eval(types<Args...>(), std::index_sequence<Is...>(), L, start, tracking, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)..., stack_detail::unchecked_get<Arg>(L, start + tracking.used, tracking));
  10065. }
  10066. };
  10067. template <bool checkargs = detail::default_safe_function_calls , std::size_t... I, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value >>
  10068. inline decltype(auto) call(types<R>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
  10069. #ifndef _MSC_VER
  10070. static_assert(meta::all<meta::is_not_move_only<Args>...>::value, "One of the arguments being bound is a move-only type, and it is not being taken by reference: this will break your code. Please take a reference and std::move it manually if this was your intention.");
  10071. #endif // This compiler make me so sad
  10072. argument_handler<types<R, Args...>> handler{};
  10073. multi_check<checkargs, Args...>(L, start, handler);
  10074. record tracking{};
  10075. return evaluator{}.eval(ta, tai, L, start, tracking, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10076. }
  10077. template <bool checkargs = detail::default_safe_function_calls, std::size_t... I, typename... Args, typename Fx, typename... FxArgs>
  10078. inline void call(types<void>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
  10079. #ifndef _MSC_VER
  10080. static_assert(meta::all<meta::is_not_move_only<Args>...>::value, "One of the arguments being bound is a move-only type, and it is not being taken by reference: this will break your code. Please take a reference and std::move it manually if this was your intention.");
  10081. #endif // This compiler make me so fucking sad
  10082. argument_handler<types<void, Args...>> handler{};
  10083. multi_check<checkargs, Args...>(L, start, handler);
  10084. record tracking{};
  10085. evaluator{}.eval(ta, tai, L, start, tracking, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10086. }
  10087. } // namespace stack_detail
  10088. template <typename T>
  10089. int set_ref(lua_State* L, T&& arg, int tableindex = -2) {
  10090. push(L, std::forward<T>(arg));
  10091. return luaL_ref(L, tableindex);
  10092. }
  10093. template <bool check_args = detail::default_safe_function_calls, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
  10094. inline decltype(auto) call(types<R> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
  10095. typedef std::make_index_sequence<sizeof...(Args)> args_indices;
  10096. return stack_detail::call<check_args>(tr, ta, args_indices(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10097. }
  10098. template <bool check_args = detail::default_safe_function_calls, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
  10099. inline decltype(auto) call(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
  10100. return call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10101. }
  10102. template <bool check_args = detail::default_safe_function_calls, typename... Args, typename Fx, typename... FxArgs>
  10103. inline void call(types<void> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
  10104. typedef std::make_index_sequence<sizeof...(Args)> args_indices;
  10105. stack_detail::call<check_args>(tr, ta, args_indices(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10106. }
  10107. template <bool check_args = detail::default_safe_function_calls, typename... Args, typename Fx, typename... FxArgs>
  10108. inline void call(types<void> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
  10109. call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10110. }
  10111. template <bool check_args = detail::default_safe_function_calls, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
  10112. inline decltype(auto) call_from_top(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
  10113. typedef meta::count_for_pack<lua_size, Args...> expected_count;
  10114. return call<check_args>(tr, ta, L, (std::max)(static_cast<int>(lua_gettop(L) - expected_count::value), static_cast<int>(0)), std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10115. }
  10116. template <bool check_args = detail::default_safe_function_calls, typename... Args, typename Fx, typename... FxArgs>
  10117. inline void call_from_top(types<void> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
  10118. typedef meta::count_for_pack<lua_size, Args...> expected_count;
  10119. call<check_args>(tr, ta, L, (std::max)(static_cast<int>(lua_gettop(L) - expected_count::value), static_cast<int>(0)), std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  10120. }
  10121. template <bool check_args = detail::default_safe_function_calls, bool clean_stack = true, typename... Args, typename Fx, typename... FxArgs>
  10122. inline int call_into_lua(types<void> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) {
  10123. call<check_args>(tr, ta, L, start, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)...);
  10124. if (clean_stack) {
  10125. lua_settop(L, 0);
  10126. }
  10127. return 0;
  10128. }
  10129. template <bool check_args = detail::default_safe_function_calls, bool clean_stack = true, typename Ret0, typename... Ret, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<meta::neg<std::is_void<Ret0>>::value>>
  10130. inline int call_into_lua(types<Ret0, Ret...>, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) {
  10131. decltype(auto) r = call<check_args>(types<meta::return_type_t<Ret0, Ret...>>(), ta, L, start, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)...);
  10132. typedef meta::unqualified_t<decltype(r)> R;
  10133. typedef meta::any<is_stack_based<R>,
  10134. std::is_same<R, absolute_index>,
  10135. std::is_same<R, ref_index>,
  10136. std::is_same<R, raw_index>>
  10137. is_stack;
  10138. if (clean_stack && !is_stack::value) {
  10139. lua_settop(L, 0);
  10140. }
  10141. return push_reference(L, std::forward<decltype(r)>(r));
  10142. }
  10143. template <bool check_args = detail::default_safe_function_calls, bool clean_stack = true, typename Fx, typename... FxArgs>
  10144. inline int call_lua(lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) {
  10145. typedef lua_bind_traits<meta::unqualified_t<Fx>> traits_type;
  10146. typedef typename traits_type::args_list args_list;
  10147. typedef typename traits_type::returns_list returns_list;
  10148. return call_into_lua<check_args, clean_stack>(returns_list(), args_list(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)...);
  10149. }
  10150. inline call_syntax get_call_syntax(lua_State* L, const string_view& key, int index) {
  10151. if (lua_gettop(L) == 0) {
  10152. return call_syntax::dot;
  10153. }
  10154. luaL_getmetatable(L, key.data());
  10155. auto pn = pop_n(L, 1);
  10156. if (lua_compare(L, -1, index, LUA_OPEQ) != 1) {
  10157. return call_syntax::dot;
  10158. }
  10159. return call_syntax::colon;
  10160. }
  10161. inline void script(lua_State* L, const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  10162. detail::typical_chunk_name_t basechunkname = {};
  10163. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  10164. if (luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()) || lua_pcall(L, 0, LUA_MULTRET, 0)) {
  10165. lua_error(L);
  10166. }
  10167. }
  10168. inline void script_file(lua_State* L, const std::string& filename, load_mode mode = load_mode::any) {
  10169. if (luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()) || lua_pcall(L, 0, LUA_MULTRET, 0)) {
  10170. lua_error(L);
  10171. }
  10172. }
  10173. inline void luajit_exception_handler(lua_State* L, int (*handler)(lua_State*, lua_CFunction) = detail::c_trampoline) {
  10174. #if defined(SOL_LUAJIT) && !defined(SOL_EXCEPTIONS_SAFE_PROPAGATION)
  10175. if (L == nullptr) {
  10176. return;
  10177. }
  10178. lua_pushlightuserdata(L, (void*)handler);
  10179. auto pn = pop_n(L, 1);
  10180. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
  10181. #else
  10182. (void)L;
  10183. (void)handler;
  10184. #endif
  10185. }
  10186. inline void luajit_exception_off(lua_State* L) {
  10187. #if defined(SOL_LUAJIT)
  10188. if (L == nullptr) {
  10189. return;
  10190. }
  10191. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_OFF);
  10192. #else
  10193. (void)L;
  10194. #endif
  10195. }
  10196. } // namespace stack
  10197. } // namespace sol
  10198. // end of sol/stack.hpp
  10199. // beginning of sol/unsafe_function.hpp
  10200. // beginning of sol/function_result.hpp
  10201. // beginning of sol/protected_function_result.hpp
  10202. // beginning of sol/proxy_base.hpp
  10203. namespace sol {
  10204. struct proxy_base_tag {};
  10205. template <typename Super>
  10206. struct proxy_base : proxy_base_tag {
  10207. operator std::string() const {
  10208. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  10209. return super.template get<std::string>();
  10210. }
  10211. template <typename T, meta::enable<meta::neg<meta::is_string_constructible<T>>, is_proxy_primitive<meta::unqualified_t<T>>> = meta::enabler>
  10212. operator T() const {
  10213. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  10214. return super.template get<T>();
  10215. }
  10216. template <typename T, meta::enable<meta::neg<meta::is_string_constructible<T>>, meta::neg<is_proxy_primitive<meta::unqualified_t<T>>>> = meta::enabler>
  10217. operator T&() const {
  10218. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  10219. return super.template get<T&>();
  10220. }
  10221. lua_State* lua_state() const {
  10222. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  10223. return super.lua_state();
  10224. }
  10225. };
  10226. } // namespace sol
  10227. // end of sol/proxy_base.hpp
  10228. // beginning of sol/stack_iterator.hpp
  10229. namespace sol {
  10230. template <typename proxy_t, bool is_const>
  10231. struct stack_iterator {
  10232. typedef std::conditional_t<is_const, const proxy_t, proxy_t> reference;
  10233. typedef std::conditional_t<is_const, const proxy_t*, proxy_t*> pointer;
  10234. typedef proxy_t value_type;
  10235. typedef std::ptrdiff_t difference_type;
  10236. typedef std::random_access_iterator_tag iterator_category;
  10237. lua_State* L;
  10238. int index;
  10239. int stacktop;
  10240. proxy_t sp;
  10241. stack_iterator()
  10242. : L(nullptr), index((std::numeric_limits<int>::max)()), stacktop((std::numeric_limits<int>::max)()), sp() {
  10243. }
  10244. stack_iterator(const stack_iterator<proxy_t, true>& r)
  10245. : L(r.L), index(r.index), stacktop(r.stacktop), sp(r.sp) {
  10246. }
  10247. stack_iterator(lua_State* luastate, int idx, int topidx)
  10248. : L(luastate), index(idx), stacktop(topidx), sp(luastate, idx) {
  10249. }
  10250. reference operator*() {
  10251. return proxy_t(L, index);
  10252. }
  10253. reference operator*() const {
  10254. return proxy_t(L, index);
  10255. }
  10256. pointer operator->() {
  10257. sp = proxy_t(L, index);
  10258. return &sp;
  10259. }
  10260. pointer operator->() const {
  10261. const_cast<proxy_t&>(sp) = proxy_t(L, index);
  10262. return &sp;
  10263. }
  10264. stack_iterator& operator++() {
  10265. ++index;
  10266. return *this;
  10267. }
  10268. stack_iterator operator++(int) {
  10269. auto r = *this;
  10270. this->operator++();
  10271. return r;
  10272. }
  10273. stack_iterator& operator--() {
  10274. --index;
  10275. return *this;
  10276. }
  10277. stack_iterator operator--(int) {
  10278. auto r = *this;
  10279. this->operator--();
  10280. return r;
  10281. }
  10282. stack_iterator& operator+=(difference_type idx) {
  10283. index += static_cast<int>(idx);
  10284. return *this;
  10285. }
  10286. stack_iterator& operator-=(difference_type idx) {
  10287. index -= static_cast<int>(idx);
  10288. return *this;
  10289. }
  10290. difference_type operator-(const stack_iterator& r) const {
  10291. return index - r.index;
  10292. }
  10293. stack_iterator operator+(difference_type idx) const {
  10294. stack_iterator r = *this;
  10295. r += idx;
  10296. return r;
  10297. }
  10298. reference operator[](difference_type idx) const {
  10299. return proxy_t(L, index + static_cast<int>(idx));
  10300. }
  10301. bool operator==(const stack_iterator& r) const {
  10302. if (stacktop == (std::numeric_limits<int>::max)()) {
  10303. return r.index == r.stacktop;
  10304. }
  10305. else if (r.stacktop == (std::numeric_limits<int>::max)()) {
  10306. return index == stacktop;
  10307. }
  10308. return index == r.index;
  10309. }
  10310. bool operator!=(const stack_iterator& r) const {
  10311. return !(this->operator==(r));
  10312. }
  10313. bool operator<(const stack_iterator& r) const {
  10314. return index < r.index;
  10315. }
  10316. bool operator>(const stack_iterator& r) const {
  10317. return index > r.index;
  10318. }
  10319. bool operator<=(const stack_iterator& r) const {
  10320. return index <= r.index;
  10321. }
  10322. bool operator>=(const stack_iterator& r) const {
  10323. return index >= r.index;
  10324. }
  10325. };
  10326. template <typename proxy_t, bool is_const>
  10327. inline stack_iterator<proxy_t, is_const> operator+(typename stack_iterator<proxy_t, is_const>::difference_type n, const stack_iterator<proxy_t, is_const>& r) {
  10328. return r + n;
  10329. }
  10330. } // namespace sol
  10331. // end of sol/stack_iterator.hpp
  10332. // beginning of sol/stack_proxy.hpp
  10333. // beginning of sol/stack_proxy_base.hpp
  10334. namespace sol {
  10335. struct stack_proxy_base : public proxy_base<stack_proxy_base> {
  10336. private:
  10337. lua_State* L;
  10338. int index;
  10339. public:
  10340. stack_proxy_base()
  10341. : L(nullptr), index(0) {
  10342. }
  10343. stack_proxy_base(lua_State* L, int index)
  10344. : L(L), index(index) {
  10345. }
  10346. template <typename T>
  10347. decltype(auto) get() const {
  10348. return stack::get<T>(L, stack_index());
  10349. }
  10350. template <typename T>
  10351. bool is() const {
  10352. return stack::check<T>(L, stack_index());
  10353. }
  10354. template <typename T>
  10355. decltype(auto) as() const {
  10356. return get<T>();
  10357. }
  10358. type get_type() const noexcept {
  10359. return type_of(lua_state(), stack_index());
  10360. }
  10361. int push() const {
  10362. return push(L);
  10363. }
  10364. int push(lua_State* Ls) const {
  10365. lua_pushvalue(Ls, index);
  10366. return 1;
  10367. }
  10368. lua_State* lua_state() const {
  10369. return L;
  10370. }
  10371. int stack_index() const {
  10372. return index;
  10373. }
  10374. };
  10375. namespace stack {
  10376. template <>
  10377. struct getter<stack_proxy_base> {
  10378. static stack_proxy_base get(lua_State* L, int index = -1) {
  10379. return stack_proxy_base(L, index);
  10380. }
  10381. };
  10382. template <>
  10383. struct pusher<stack_proxy_base> {
  10384. static int push(lua_State*, const stack_proxy_base& ref) {
  10385. return ref.push();
  10386. }
  10387. };
  10388. } // namespace stack
  10389. } // namespace sol
  10390. // end of sol/stack_proxy_base.hpp
  10391. namespace sol {
  10392. struct stack_proxy : public stack_proxy_base {
  10393. public:
  10394. stack_proxy()
  10395. : stack_proxy_base() {
  10396. }
  10397. stack_proxy(lua_State* L, int index)
  10398. : stack_proxy_base(L, index) {
  10399. }
  10400. template <typename... Ret, typename... Args>
  10401. decltype(auto) call(Args&&... args);
  10402. template <typename... Args>
  10403. decltype(auto) operator()(Args&&... args) {
  10404. return call<>(std::forward<Args>(args)...);
  10405. }
  10406. };
  10407. namespace stack {
  10408. template <>
  10409. struct getter<stack_proxy> {
  10410. static stack_proxy get(lua_State* L, int index = -1) {
  10411. return stack_proxy(L, index);
  10412. }
  10413. };
  10414. template <>
  10415. struct pusher<stack_proxy> {
  10416. static int push(lua_State*, const stack_proxy& ref) {
  10417. return ref.push();
  10418. }
  10419. };
  10420. } // namespace stack
  10421. } // namespace sol
  10422. // end of sol/stack_proxy.hpp
  10423. namespace sol {
  10424. struct protected_function_result : public proxy_base<protected_function_result> {
  10425. private:
  10426. lua_State* L;
  10427. int index;
  10428. int returncount;
  10429. int popcount;
  10430. call_status err;
  10431. template <typename T>
  10432. decltype(auto) tagged_get(types<optional<T>>, int index_offset) const {
  10433. typedef decltype(stack::get<optional<T>>(L, index)) ret_t;
  10434. int target = index + index_offset;
  10435. if (!valid()) {
  10436. return ret_t(nullopt);
  10437. }
  10438. return stack::get<optional<T>>(L, target);
  10439. }
  10440. template <typename T>
  10441. decltype(auto) tagged_get(types<T>, int index_offset) const {
  10442. int target = index + index_offset;
  10443. #if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES
  10444. if (!valid()) {
  10445. type t = type_of(L, target);
  10446. type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is not an error)");
  10447. }
  10448. #endif // Check Argument Safety
  10449. return stack::get<T>(L, target);
  10450. }
  10451. optional<error> tagged_get(types<optional<error>>, int index_offset) const {
  10452. int target = index + index_offset;
  10453. if (valid()) {
  10454. return nullopt;
  10455. }
  10456. return error(detail::direct_error, stack::get<std::string>(L, target));
  10457. }
  10458. error tagged_get(types<error>, int index_offset) const {
  10459. int target = index + index_offset;
  10460. #if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES
  10461. if (valid()) {
  10462. type t = type_of(L, target);
  10463. type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is an error)");
  10464. }
  10465. #endif // Check Argument Safety
  10466. return error(detail::direct_error, stack::get<std::string>(L, target));
  10467. }
  10468. public:
  10469. typedef stack_proxy reference_type;
  10470. typedef stack_proxy value_type;
  10471. typedef stack_proxy* pointer;
  10472. typedef std::ptrdiff_t difference_type;
  10473. typedef std::size_t size_type;
  10474. typedef stack_iterator<stack_proxy, false> iterator;
  10475. typedef stack_iterator<stack_proxy, true> const_iterator;
  10476. typedef std::reverse_iterator<iterator> reverse_iterator;
  10477. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  10478. protected_function_result() = default;
  10479. protected_function_result(lua_State* Ls, int idx = -1, int retnum = 0, int popped = 0, call_status pferr = call_status::ok) noexcept
  10480. : L(Ls), index(idx), returncount(retnum), popcount(popped), err(pferr) {
  10481. }
  10482. protected_function_result(const protected_function_result&) = default;
  10483. protected_function_result& operator=(const protected_function_result&) = default;
  10484. protected_function_result(protected_function_result&& o) noexcept
  10485. : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) {
  10486. // Must be manual, otherwise destructor will screw us
  10487. // return count being 0 is enough to keep things clean
  10488. // but we will be thorough
  10489. o.abandon();
  10490. }
  10491. protected_function_result& operator=(protected_function_result&& o) noexcept {
  10492. L = o.L;
  10493. index = o.index;
  10494. returncount = o.returncount;
  10495. popcount = o.popcount;
  10496. err = o.err;
  10497. // Must be manual, otherwise destructor will screw us
  10498. // return count being 0 is enough to keep things clean
  10499. // but we will be thorough
  10500. o.abandon();
  10501. return *this;
  10502. }
  10503. protected_function_result(const unsafe_function_result& o) = delete;
  10504. protected_function_result& operator=(const unsafe_function_result& o) = delete;
  10505. protected_function_result(unsafe_function_result&& o) noexcept;
  10506. protected_function_result& operator=(unsafe_function_result&& o) noexcept;
  10507. call_status status() const noexcept {
  10508. return err;
  10509. }
  10510. bool valid() const noexcept {
  10511. return status() == call_status::ok || status() == call_status::yielded;
  10512. }
  10513. template <typename T>
  10514. decltype(auto) get(int index_offset = 0) const {
  10515. return tagged_get(types<meta::unqualified_t<T>>(), index_offset);
  10516. }
  10517. type get_type(difference_type index_offset = 0) const noexcept {
  10518. return type_of(L, index + static_cast<int>(index_offset));
  10519. }
  10520. stack_proxy operator[](difference_type index_offset) const {
  10521. return stack_proxy(L, index + static_cast<int>(index_offset));
  10522. }
  10523. iterator begin() {
  10524. return iterator(L, index, stack_index() + return_count());
  10525. }
  10526. iterator end() {
  10527. return iterator(L, stack_index() + return_count(), stack_index() + return_count());
  10528. }
  10529. const_iterator begin() const {
  10530. return const_iterator(L, index, stack_index() + return_count());
  10531. }
  10532. const_iterator end() const {
  10533. return const_iterator(L, stack_index() + return_count(), stack_index() + return_count());
  10534. }
  10535. const_iterator cbegin() const {
  10536. return begin();
  10537. }
  10538. const_iterator cend() const {
  10539. return end();
  10540. }
  10541. reverse_iterator rbegin() {
  10542. return std::reverse_iterator<iterator>(begin());
  10543. }
  10544. reverse_iterator rend() {
  10545. return std::reverse_iterator<iterator>(end());
  10546. }
  10547. const_reverse_iterator rbegin() const {
  10548. return std::reverse_iterator<const_iterator>(begin());
  10549. }
  10550. const_reverse_iterator rend() const {
  10551. return std::reverse_iterator<const_iterator>(end());
  10552. }
  10553. const_reverse_iterator crbegin() const {
  10554. return std::reverse_iterator<const_iterator>(cbegin());
  10555. }
  10556. const_reverse_iterator crend() const {
  10557. return std::reverse_iterator<const_iterator>(cend());
  10558. }
  10559. lua_State* lua_state() const noexcept {
  10560. return L;
  10561. };
  10562. int stack_index() const noexcept {
  10563. return index;
  10564. };
  10565. int return_count() const noexcept {
  10566. return returncount;
  10567. };
  10568. int pop_count() const noexcept {
  10569. return popcount;
  10570. };
  10571. void abandon() noexcept {
  10572. //L = nullptr;
  10573. index = 0;
  10574. returncount = 0;
  10575. popcount = 0;
  10576. err = call_status::runtime;
  10577. }
  10578. ~protected_function_result() {
  10579. stack::remove(L, index, popcount);
  10580. }
  10581. };
  10582. namespace stack {
  10583. template <>
  10584. struct pusher<protected_function_result> {
  10585. static int push(lua_State* L, const protected_function_result& pfr) {
  10586. int p = 0;
  10587. for (int i = 0; i < pfr.pop_count(); ++i) {
  10588. lua_pushvalue(L, i + pfr.stack_index());
  10589. ++p;
  10590. }
  10591. return p;
  10592. }
  10593. };
  10594. } // namespace stack
  10595. } // namespace sol
  10596. // end of sol/protected_function_result.hpp
  10597. // beginning of sol/unsafe_function_result.hpp
  10598. namespace sol {
  10599. struct unsafe_function_result : public proxy_base<unsafe_function_result> {
  10600. private:
  10601. lua_State* L;
  10602. int index;
  10603. int returncount;
  10604. public:
  10605. typedef stack_proxy reference_type;
  10606. typedef stack_proxy value_type;
  10607. typedef stack_proxy* pointer;
  10608. typedef std::ptrdiff_t difference_type;
  10609. typedef std::size_t size_type;
  10610. typedef stack_iterator<stack_proxy, false> iterator;
  10611. typedef stack_iterator<stack_proxy, true> const_iterator;
  10612. typedef std::reverse_iterator<iterator> reverse_iterator;
  10613. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  10614. unsafe_function_result() = default;
  10615. unsafe_function_result(lua_State* Ls, int idx = -1, int retnum = 0)
  10616. : L(Ls), index(idx), returncount(retnum) {
  10617. }
  10618. unsafe_function_result(const unsafe_function_result&) = default;
  10619. unsafe_function_result& operator=(const unsafe_function_result&) = default;
  10620. unsafe_function_result(unsafe_function_result&& o)
  10621. : L(o.L), index(o.index), returncount(o.returncount) {
  10622. // Must be manual, otherwise destructor will screw us
  10623. // return count being 0 is enough to keep things clean
  10624. // but will be thorough
  10625. o.abandon();
  10626. }
  10627. unsafe_function_result& operator=(unsafe_function_result&& o) {
  10628. L = o.L;
  10629. index = o.index;
  10630. returncount = o.returncount;
  10631. // Must be manual, otherwise destructor will screw us
  10632. // return count being 0 is enough to keep things clean
  10633. // but will be thorough
  10634. o.abandon();
  10635. return *this;
  10636. }
  10637. unsafe_function_result(const protected_function_result& o) = delete;
  10638. unsafe_function_result& operator=(const protected_function_result& o) = delete;
  10639. unsafe_function_result(protected_function_result&& o) noexcept;
  10640. unsafe_function_result& operator=(protected_function_result&& o) noexcept;
  10641. template <typename T>
  10642. decltype(auto) get(difference_type index_offset = 0) const {
  10643. return stack::get<T>(L, index + static_cast<int>(index_offset));
  10644. }
  10645. type get_type(difference_type index_offset = 0) const noexcept {
  10646. return type_of(L, index + static_cast<int>(index_offset));
  10647. }
  10648. stack_proxy operator[](difference_type index_offset) const {
  10649. return stack_proxy(L, index + static_cast<int>(index_offset));
  10650. }
  10651. iterator begin() {
  10652. return iterator(L, index, stack_index() + return_count());
  10653. }
  10654. iterator end() {
  10655. return iterator(L, stack_index() + return_count(), stack_index() + return_count());
  10656. }
  10657. const_iterator begin() const {
  10658. return const_iterator(L, index, stack_index() + return_count());
  10659. }
  10660. const_iterator end() const {
  10661. return const_iterator(L, stack_index() + return_count(), stack_index() + return_count());
  10662. }
  10663. const_iterator cbegin() const {
  10664. return begin();
  10665. }
  10666. const_iterator cend() const {
  10667. return end();
  10668. }
  10669. reverse_iterator rbegin() {
  10670. return std::reverse_iterator<iterator>(begin());
  10671. }
  10672. reverse_iterator rend() {
  10673. return std::reverse_iterator<iterator>(end());
  10674. }
  10675. const_reverse_iterator rbegin() const {
  10676. return std::reverse_iterator<const_iterator>(begin());
  10677. }
  10678. const_reverse_iterator rend() const {
  10679. return std::reverse_iterator<const_iterator>(end());
  10680. }
  10681. const_reverse_iterator crbegin() const {
  10682. return std::reverse_iterator<const_iterator>(cbegin());
  10683. }
  10684. const_reverse_iterator crend() const {
  10685. return std::reverse_iterator<const_iterator>(cend());
  10686. }
  10687. call_status status() const noexcept {
  10688. return call_status::ok;
  10689. }
  10690. bool valid() const noexcept {
  10691. return status() == call_status::ok || status() == call_status::yielded;
  10692. }
  10693. lua_State* lua_state() const {
  10694. return L;
  10695. };
  10696. int stack_index() const {
  10697. return index;
  10698. };
  10699. int return_count() const {
  10700. return returncount;
  10701. };
  10702. void abandon() noexcept {
  10703. //L = nullptr;
  10704. index = 0;
  10705. returncount = 0;
  10706. }
  10707. ~unsafe_function_result() {
  10708. lua_pop(L, returncount);
  10709. }
  10710. };
  10711. namespace stack {
  10712. template <>
  10713. struct pusher<unsafe_function_result> {
  10714. static int push(lua_State* L, const unsafe_function_result& fr) {
  10715. int p = 0;
  10716. for (int i = 0; i < fr.return_count(); ++i) {
  10717. lua_pushvalue(L, i + fr.stack_index());
  10718. ++p;
  10719. }
  10720. return p;
  10721. }
  10722. };
  10723. } // namespace stack
  10724. } // namespace sol
  10725. // end of sol/unsafe_function_result.hpp
  10726. namespace sol {
  10727. namespace detail {
  10728. template <>
  10729. struct is_speshul<unsafe_function_result> : std::true_type {};
  10730. template <>
  10731. struct is_speshul<protected_function_result> : std::true_type {};
  10732. template <std::size_t I, typename... Args, typename T>
  10733. stack_proxy get(types<Args...>, index_value<0>, index_value<I>, const T& fr) {
  10734. return stack_proxy(fr.lua_state(), static_cast<int>(fr.stack_index() + I));
  10735. }
  10736. template <std::size_t I, std::size_t N, typename Arg, typename... Args, typename T, meta::enable<meta::boolean<(N > 0)>> = meta::enabler>
  10737. stack_proxy get(types<Arg, Args...>, index_value<N>, index_value<I>, const T& fr) {
  10738. return get(types<Args...>(), index_value<N - 1>(), index_value<I + lua_size<Arg>::value>(), fr);
  10739. }
  10740. } // namespace detail
  10741. template <>
  10742. struct tie_size<unsafe_function_result> : std::integral_constant<std::size_t, SIZE_MAX> {};
  10743. template <>
  10744. struct tie_size<protected_function_result> : std::integral_constant<std::size_t, SIZE_MAX> {};
  10745. template <std::size_t I>
  10746. stack_proxy get(const unsafe_function_result& fr) {
  10747. return stack_proxy(fr.lua_state(), static_cast<int>(fr.stack_index() + I));
  10748. }
  10749. template <std::size_t I, typename... Args>
  10750. stack_proxy get(types<Args...> t, const unsafe_function_result& fr) {
  10751. return detail::get(t, index_value<I>(), index_value<0>(), fr);
  10752. }
  10753. template <std::size_t I>
  10754. stack_proxy get(const protected_function_result& fr) {
  10755. return stack_proxy(fr.lua_state(), static_cast<int>(fr.stack_index() + I));
  10756. }
  10757. template <std::size_t I, typename... Args>
  10758. stack_proxy get(types<Args...> t, const protected_function_result& fr) {
  10759. return detail::get(t, index_value<I>(), index_value<0>(), fr);
  10760. }
  10761. } // namespace sol
  10762. // end of sol/function_result.hpp
  10763. // beginning of sol/function_types.hpp
  10764. // beginning of sol/function_types_core.hpp
  10765. // beginning of sol/wrapper.hpp
  10766. namespace sol {
  10767. namespace detail {
  10768. template <typename T>
  10769. using array_return_type = std::conditional_t<std::is_array<T>::value, std::add_lvalue_reference_t<T>, T>;
  10770. }
  10771. template <typename F, typename = void>
  10772. struct wrapper {
  10773. typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
  10774. typedef typename traits_type::args_list args_list;
  10775. typedef typename traits_type::args_list free_args_list;
  10776. typedef typename traits_type::returns_list returns_list;
  10777. template <typename... Args>
  10778. static decltype(auto) call(F& f, Args&&... args) {
  10779. return f(std::forward<Args>(args)...);
  10780. }
  10781. struct caller {
  10782. template <typename... Args>
  10783. decltype(auto) operator()(F& fx, Args&&... args) const {
  10784. return call(fx, std::forward<Args>(args)...);
  10785. }
  10786. };
  10787. };
  10788. template <typename F>
  10789. struct wrapper<F, std::enable_if_t<std::is_function<std::remove_pointer_t<meta::unqualified_t<F>>>::value>> {
  10790. typedef lua_bind_traits<std::remove_pointer_t<meta::unqualified_t<F>>> traits_type;
  10791. typedef typename traits_type::args_list args_list;
  10792. typedef typename traits_type::args_list free_args_list;
  10793. typedef typename traits_type::returns_list returns_list;
  10794. template <F fx, typename... Args>
  10795. static decltype(auto) invoke(Args&&... args) {
  10796. return fx(std::forward<Args>(args)...);
  10797. }
  10798. template <typename... Args>
  10799. static decltype(auto) call(F& fx, Args&&... args) {
  10800. return fx(std::forward<Args>(args)...);
  10801. }
  10802. struct caller {
  10803. template <typename... Args>
  10804. decltype(auto) operator()(F& fx, Args&&... args) const {
  10805. return call(fx, std::forward<Args>(args)...);
  10806. }
  10807. };
  10808. template <F fx>
  10809. struct invoker {
  10810. template <typename... Args>
  10811. decltype(auto) operator()(Args&&... args) const {
  10812. return invoke<fx>(std::forward<Args>(args)...);
  10813. }
  10814. };
  10815. };
  10816. template <typename F>
  10817. struct wrapper<F, std::enable_if_t<std::is_member_object_pointer<meta::unqualified_t<F>>::value>> {
  10818. typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
  10819. typedef typename traits_type::object_type object_type;
  10820. typedef typename traits_type::return_type return_type;
  10821. typedef typename traits_type::args_list args_list;
  10822. typedef types<object_type&, return_type> free_args_list;
  10823. typedef typename traits_type::returns_list returns_list;
  10824. template <F fx>
  10825. static auto call(object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
  10826. return mem.*fx;
  10827. }
  10828. template <F fx, typename Arg, typename... Args>
  10829. static decltype(auto) invoke(object_type& mem, Arg&& arg, Args&&...) {
  10830. return mem.*fx = std::forward<Arg>(arg);
  10831. }
  10832. template <typename Fx>
  10833. static auto call(Fx&& fx, object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
  10834. return mem.*fx;
  10835. }
  10836. template <typename Fx, typename Arg, typename... Args>
  10837. static void call(Fx&& fx, object_type& mem, Arg&& arg, Args&&...) {
  10838. (mem.*fx) = std::forward<Arg>(arg);
  10839. }
  10840. struct caller {
  10841. template <typename Fx, typename... Args>
  10842. decltype(auto) operator()(Fx&& fx, object_type& mem, Args&&... args) const {
  10843. return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
  10844. }
  10845. };
  10846. template <F fx>
  10847. struct invoker {
  10848. template <typename... Args>
  10849. decltype(auto) operator()(Args&&... args) const {
  10850. return invoke<fx>(std::forward<Args>(args)...);
  10851. }
  10852. };
  10853. };
  10854. template <typename F, typename R, typename O, typename... FArgs>
  10855. struct member_function_wrapper {
  10856. typedef O object_type;
  10857. typedef lua_bind_traits<F> traits_type;
  10858. typedef typename traits_type::args_list args_list;
  10859. typedef types<object_type&, FArgs...> free_args_list;
  10860. typedef meta::tuple_types<R> returns_list;
  10861. template <F fx, typename... Args>
  10862. static R invoke(O& mem, Args&&... args) {
  10863. return (mem.*fx)(std::forward<Args>(args)...);
  10864. }
  10865. template <typename Fx, typename... Args>
  10866. static R call(Fx&& fx, O& mem, Args&&... args) {
  10867. return (mem.*fx)(std::forward<Args>(args)...);
  10868. }
  10869. struct caller {
  10870. template <typename Fx, typename... Args>
  10871. decltype(auto) operator()(Fx&& fx, O& mem, Args&&... args) const {
  10872. return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
  10873. }
  10874. };
  10875. template <F fx>
  10876. struct invoker {
  10877. template <typename... Args>
  10878. decltype(auto) operator()(O& mem, Args&&... args) const {
  10879. return invoke<fx>(mem, std::forward<Args>(args)...);
  10880. }
  10881. };
  10882. };
  10883. template <typename R, typename O, typename... Args>
  10884. struct wrapper<R (O::*)(Args...)> : public member_function_wrapper<R (O::*)(Args...), R, O, Args...> {
  10885. };
  10886. template <typename R, typename O, typename... Args>
  10887. struct wrapper<R (O::*)(Args...) const> : public member_function_wrapper<R (O::*)(Args...) const, R, O, Args...> {
  10888. };
  10889. template <typename R, typename O, typename... Args>
  10890. struct wrapper<R (O::*)(Args...) const volatile> : public member_function_wrapper<R (O::*)(Args...) const volatile, R, O, Args...> {
  10891. };
  10892. template <typename R, typename O, typename... Args>
  10893. struct wrapper<R (O::*)(Args...)&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> {
  10894. };
  10895. template <typename R, typename O, typename... Args>
  10896. struct wrapper<R (O::*)(Args...) const&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> {
  10897. };
  10898. template <typename R, typename O, typename... Args>
  10899. struct wrapper<R (O::*)(Args...) const volatile&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> {
  10900. };
  10901. template <typename R, typename O, typename... Args>
  10902. struct wrapper<R (O::*)(Args..., ...)&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> {
  10903. };
  10904. template <typename R, typename O, typename... Args>
  10905. struct wrapper<R (O::*)(Args..., ...) const&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> {
  10906. };
  10907. template <typename R, typename O, typename... Args>
  10908. struct wrapper<R (O::*)(Args..., ...) const volatile&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> {
  10909. };
  10910. template <typename R, typename O, typename... Args>
  10911. struct wrapper<R (O::*)(Args...) &&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> {
  10912. };
  10913. template <typename R, typename O, typename... Args>
  10914. struct wrapper<R (O::*)(Args...) const&&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> {
  10915. };
  10916. template <typename R, typename O, typename... Args>
  10917. struct wrapper<R (O::*)(Args...) const volatile&&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> {
  10918. };
  10919. template <typename R, typename O, typename... Args>
  10920. struct wrapper<R (O::*)(Args..., ...) &&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> {
  10921. };
  10922. template <typename R, typename O, typename... Args>
  10923. struct wrapper<R (O::*)(Args..., ...) const&&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> {
  10924. };
  10925. template <typename R, typename O, typename... Args>
  10926. struct wrapper<R (O::*)(Args..., ...) const volatile&&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> {
  10927. };
  10928. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  10929. //noexcept has become a part of a function's type
  10930. template <typename R, typename O, typename... Args>
  10931. struct wrapper<R (O::*)(Args...) noexcept> : public member_function_wrapper<R (O::*)(Args...) noexcept, R, O, Args...> {
  10932. };
  10933. template <typename R, typename O, typename... Args>
  10934. struct wrapper<R (O::*)(Args...) const noexcept> : public member_function_wrapper<R (O::*)(Args...) const noexcept, R, O, Args...> {
  10935. };
  10936. template <typename R, typename O, typename... Args>
  10937. struct wrapper<R (O::*)(Args...) const volatile noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile noexcept, R, O, Args...> {
  10938. };
  10939. template <typename R, typename O, typename... Args>
  10940. struct wrapper<R (O::*)(Args...) & noexcept> : public member_function_wrapper<R (O::*)(Args...) & noexcept, R, O, Args...> {
  10941. };
  10942. template <typename R, typename O, typename... Args>
  10943. struct wrapper<R (O::*)(Args...) const& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> {
  10944. };
  10945. template <typename R, typename O, typename... Args>
  10946. struct wrapper<R (O::*)(Args...) const volatile& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> {
  10947. };
  10948. template <typename R, typename O, typename... Args>
  10949. struct wrapper<R (O::*)(Args..., ...) & noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) & noexcept, R, O, Args...> {
  10950. };
  10951. template <typename R, typename O, typename... Args>
  10952. struct wrapper<R (O::*)(Args..., ...) const& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> {
  10953. };
  10954. template <typename R, typename O, typename... Args>
  10955. struct wrapper<R (O::*)(Args..., ...) const volatile& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> {
  10956. };
  10957. template <typename R, typename O, typename... Args>
  10958. struct wrapper<R (O::*)(Args...) && noexcept> : public member_function_wrapper<R (O::*)(Args...) & noexcept, R, O, Args...> {
  10959. };
  10960. template <typename R, typename O, typename... Args>
  10961. struct wrapper<R (O::*)(Args...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> {
  10962. };
  10963. template <typename R, typename O, typename... Args>
  10964. struct wrapper<R (O::*)(Args...) const volatile&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> {
  10965. };
  10966. template <typename R, typename O, typename... Args>
  10967. struct wrapper<R (O::*)(Args..., ...) && noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) & noexcept, R, O, Args...> {
  10968. };
  10969. template <typename R, typename O, typename... Args>
  10970. struct wrapper<R (O::*)(Args..., ...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> {
  10971. };
  10972. template <typename R, typename O, typename... Args>
  10973. struct wrapper<R (O::*)(Args..., ...) const volatile&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> {
  10974. };
  10975. #endif // noexcept is part of a function's type
  10976. } // namespace sol
  10977. // end of sol/wrapper.hpp
  10978. namespace sol {
  10979. namespace function_detail {
  10980. template <typename Fx, int start = 1, bool is_yielding = false>
  10981. inline int call(lua_State* L) {
  10982. Fx& fx = stack::get<user<Fx>>(L, upvalue_index(start));
  10983. int nr = fx(L);
  10984. if (is_yielding) {
  10985. return lua_yield(L, nr);
  10986. }
  10987. else {
  10988. return nr;
  10989. }
  10990. }
  10991. }
  10992. } // namespace sol::function_detail
  10993. // end of sol/function_types_core.hpp
  10994. // beginning of sol/function_types_templated.hpp
  10995. // beginning of sol/call.hpp
  10996. // beginning of sol/protect.hpp
  10997. namespace sol {
  10998. template <typename T>
  10999. struct protect_t {
  11000. T value;
  11001. template <typename Arg, typename... Args, meta::disable<std::is_same<protect_t, meta::unqualified_t<Arg>>> = meta::enabler>
  11002. protect_t(Arg&& arg, Args&&... args)
  11003. : value(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  11004. }
  11005. protect_t(const protect_t&) = default;
  11006. protect_t(protect_t&&) = default;
  11007. protect_t& operator=(const protect_t&) = default;
  11008. protect_t& operator=(protect_t&&) = default;
  11009. };
  11010. template <typename T>
  11011. auto protect(T&& value) {
  11012. return protect_t<std::decay_t<T>>(std::forward<T>(value));
  11013. }
  11014. } // namespace sol
  11015. // end of sol/protect.hpp
  11016. // beginning of sol/property.hpp
  11017. namespace sol {
  11018. struct no_prop {};
  11019. template <typename R, typename W>
  11020. struct property_wrapper {
  11021. typedef std::integral_constant<bool, !std::is_void<R>::value> can_read;
  11022. typedef std::integral_constant<bool, !std::is_void<W>::value> can_write;
  11023. typedef std::conditional_t<can_read::value, R, no_prop> Read;
  11024. typedef std::conditional_t<can_write::value, W, no_prop> Write;
  11025. Read read;
  11026. Write write;
  11027. template <typename Rx, typename Wx>
  11028. property_wrapper(Rx&& r, Wx&& w)
  11029. : read(std::forward<Rx>(r)), write(std::forward<Wx>(w)) {
  11030. }
  11031. };
  11032. namespace property_detail {
  11033. template <typename R, typename W>
  11034. inline decltype(auto) property(std::true_type, R&& read, W&& write) {
  11035. return property_wrapper<std::decay_t<R>, std::decay_t<W>>(std::forward<R>(read), std::forward<W>(write));
  11036. }
  11037. template <typename W, typename R>
  11038. inline decltype(auto) property(std::false_type, W&& write, R&& read) {
  11039. return property_wrapper<std::decay_t<R>, std::decay_t<W>>(std::forward<R>(read), std::forward<W>(write));
  11040. }
  11041. template <typename R>
  11042. inline decltype(auto) property(std::true_type, R&& read) {
  11043. return property_wrapper<std::decay_t<R>, void>(std::forward<R>(read), no_prop());
  11044. }
  11045. template <typename W>
  11046. inline decltype(auto) property(std::false_type, W&& write) {
  11047. return property_wrapper<void, std::decay_t<W>>(no_prop(), std::forward<W>(write));
  11048. }
  11049. } // namespace property_detail
  11050. template <typename F, typename G>
  11051. inline decltype(auto) property(F&& f, G&& g) {
  11052. typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
  11053. typedef lua_bind_traits<meta::unqualified_t<G>> right_traits;
  11054. return property_detail::property(meta::boolean<(left_traits::free_arity < right_traits::free_arity)>(), std::forward<F>(f), std::forward<G>(g));
  11055. }
  11056. template <typename F>
  11057. inline decltype(auto) property(F&& f) {
  11058. typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
  11059. return property_detail::property(meta::boolean<(left_traits::free_arity < 2)>(), std::forward<F>(f));
  11060. }
  11061. template <typename F>
  11062. inline decltype(auto) readonly_property(F&& f) {
  11063. return property_detail::property(std::true_type(), std::forward<F>(f));
  11064. }
  11065. template <typename F>
  11066. inline decltype(auto) writeonly_property(F&& f) {
  11067. return property_detail::property(std::false_type(), std::forward<F>(f));
  11068. }
  11069. template <typename T>
  11070. struct readonly_wrapper {
  11071. T v;
  11072. readonly_wrapper(T v)
  11073. : v(std::move(v)) {
  11074. }
  11075. operator T&() {
  11076. return v;
  11077. }
  11078. operator const T&() const {
  11079. return v;
  11080. }
  11081. };
  11082. // Allow someone to make a member variable readonly (const)
  11083. template <typename R, typename T>
  11084. inline auto readonly(R T::*v) {
  11085. return readonly_wrapper<meta::unqualified_t<decltype(v)>>(v);
  11086. }
  11087. template <typename T>
  11088. struct var_wrapper {
  11089. T value;
  11090. template <typename... Args>
  11091. var_wrapper(Args&&... args)
  11092. : value(std::forward<Args>(args)...) {
  11093. }
  11094. var_wrapper(const var_wrapper&) = default;
  11095. var_wrapper(var_wrapper&&) = default;
  11096. var_wrapper& operator=(const var_wrapper&) = default;
  11097. var_wrapper& operator=(var_wrapper&&) = default;
  11098. };
  11099. template <typename V>
  11100. inline auto var(V&& v) {
  11101. typedef meta::unqualified_t<V> T;
  11102. return var_wrapper<T>(std::forward<V>(v));
  11103. }
  11104. namespace meta {
  11105. template <typename T>
  11106. struct is_member_object : std::is_member_object_pointer<T> {};
  11107. template <typename T>
  11108. struct is_member_object<readonly_wrapper<T>> : std::true_type {};
  11109. } // namespace meta
  11110. } // namespace sol
  11111. // end of sol/property.hpp
  11112. namespace sol {
  11113. namespace usertype_detail {
  11114. } // namespace usertype_detail
  11115. namespace filter_detail {
  11116. template <int I, int... In>
  11117. inline void handle_filter(static_stack_dependencies<I, In...>, lua_State* L, int&) {
  11118. if (sizeof...(In) == 0) {
  11119. return;
  11120. }
  11121. absolute_index ai(L, I);
  11122. if (type_of(L, ai) != type::userdata) {
  11123. return;
  11124. }
  11125. lua_createtable(L, static_cast<int>(sizeof...(In)), 0);
  11126. stack_reference deps(L, -1);
  11127. auto per_dep = [&L, &deps](int i) {
  11128. lua_pushvalue(L, i);
  11129. luaL_ref(L, deps.stack_index());
  11130. };
  11131. (void)per_dep;
  11132. (void)detail::swallow{ int(), (per_dep(In), int())... };
  11133. lua_setuservalue(L, ai);
  11134. }
  11135. template <int... In>
  11136. inline void handle_filter(returns_self_with<In...>, lua_State* L, int& pushed) {
  11137. pushed = stack::push(L, raw_index(1));
  11138. handle_filter(static_stack_dependencies<-1, In...>(), L, pushed);
  11139. }
  11140. inline void handle_filter(const stack_dependencies& sdeps, lua_State* L, int&) {
  11141. absolute_index ai(L, sdeps.target);
  11142. if (type_of(L, ai) != type::userdata) {
  11143. return;
  11144. }
  11145. lua_createtable(L, static_cast<int>(sdeps.size()), 0);
  11146. stack_reference deps(L, -1);
  11147. for (std::size_t i = 0; i < sdeps.size(); ++i) {
  11148. lua_pushvalue(L, sdeps.stack_indices[i]);
  11149. luaL_ref(L, deps.stack_index());
  11150. }
  11151. lua_setuservalue(L, ai);
  11152. }
  11153. template <typename P, meta::disable<std::is_base_of<detail::filter_base_tag, meta::unqualified_t<P>>> = meta::enabler>
  11154. inline void handle_filter(P&& p, lua_State* L, int& pushed) {
  11155. pushed = std::forward<P>(p)(L, pushed);
  11156. }
  11157. } // namespace filter_detail
  11158. namespace function_detail {
  11159. inline int no_construction_error(lua_State* L) {
  11160. return luaL_error(L, "sol: cannot call this constructor (tagged as non-constructible)");
  11161. }
  11162. } // namespace function_detail
  11163. namespace call_detail {
  11164. template <typename R, typename W>
  11165. inline auto& pick(std::true_type, property_wrapper<R, W>& f) {
  11166. return f.read;
  11167. }
  11168. template <typename R, typename W>
  11169. inline auto& pick(std::false_type, property_wrapper<R, W>& f) {
  11170. return f.write;
  11171. }
  11172. template <typename T, typename List>
  11173. struct void_call : void_call<T, meta::function_args_t<List>> {};
  11174. template <typename T, typename... Args>
  11175. struct void_call<T, types<Args...>> {
  11176. static void call(Args...) {
  11177. }
  11178. };
  11179. template <typename T, bool checked, bool clean_stack>
  11180. struct constructor_match {
  11181. T* obj;
  11182. constructor_match(T* o)
  11183. : obj(o) {
  11184. }
  11185. template <typename Fx, std::size_t I, typename... R, typename... Args>
  11186. int operator()(types<Fx>, index_value<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start) const {
  11187. detail::default_construct func{};
  11188. return stack::call_into_lua<checked, clean_stack>(r, a, L, start, func, obj);
  11189. }
  11190. };
  11191. namespace overload_detail {
  11192. template <std::size_t... M, typename Match, typename... Args>
  11193. inline int overload_match_arity(types<>, std::index_sequence<>, std::index_sequence<M...>, Match&&, lua_State* L, int, int, Args&&...) {
  11194. return luaL_error(L, "sol: no matching function call takes this number of arguments and the specified types");
  11195. }
  11196. template <typename Fx, typename... Fxs, std::size_t I, std::size_t... In, std::size_t... M, typename Match, typename... Args>
  11197. inline int overload_match_arity(types<Fx, Fxs...>, std::index_sequence<I, In...>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  11198. typedef lua_bind_traits<meta::unwrap_unqualified_t<Fx>> traits;
  11199. typedef meta::tuple_types<typename traits::return_type> return_types;
  11200. typedef typename traits::free_args_list args_list;
  11201. // compile-time eliminate any functions that we know ahead of time are of improper arity
  11202. if (!traits::runtime_variadics_t::value && meta::find_in_pack_v<index_value<traits::free_arity>, index_value<M>...>::value) {
  11203. return overload_match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11204. }
  11205. if (!traits::runtime_variadics_t::value && traits::free_arity != fxarity) {
  11206. return overload_match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<traits::free_arity, M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11207. }
  11208. stack::record tracking{};
  11209. if (!stack::stack_detail::check_types<true>{}.check(args_list(), L, start, no_panic, tracking)) {
  11210. return overload_match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11211. }
  11212. return matchfx(types<Fx>(), index_value<I>(), return_types(), args_list(), L, fxarity, start, std::forward<Args>(args)...);
  11213. }
  11214. template <std::size_t... M, typename Match, typename... Args>
  11215. inline int overload_match_arity_single(types<>, std::index_sequence<>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  11216. return overload_match_arity(types<>(), std::index_sequence<>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11217. }
  11218. template <typename Fx, std::size_t I, std::size_t... M, typename Match, typename... Args>
  11219. inline int overload_match_arity_single(types<Fx>, std::index_sequence<I>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  11220. typedef lua_bind_traits<meta::unwrap_unqualified_t<Fx>> traits;
  11221. typedef meta::tuple_types<typename traits::return_type> return_types;
  11222. typedef typename traits::free_args_list args_list;
  11223. // compile-time eliminate any functions that we know ahead of time are of improper arity
  11224. if (!traits::runtime_variadics_t::value && meta::find_in_pack_v<index_value<traits::free_arity>, index_value<M>...>::value) {
  11225. return overload_match_arity(types<>(), std::index_sequence<>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11226. }
  11227. if (!traits::runtime_variadics_t::value && traits::free_arity != fxarity) {
  11228. return overload_match_arity(types<>(), std::index_sequence<>(), std::index_sequence<traits::free_arity, M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11229. }
  11230. return matchfx(types<Fx>(), index_value<I>(), return_types(), args_list(), L, fxarity, start, std::forward<Args>(args)...);
  11231. }
  11232. template <typename Fx, typename Fx1, typename... Fxs, std::size_t I, std::size_t I1, std::size_t... In, std::size_t... M, typename Match, typename... Args>
  11233. inline int overload_match_arity_single(types<Fx, Fx1, Fxs...>, std::index_sequence<I, I1, In...>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  11234. typedef lua_bind_traits<meta::unwrap_unqualified_t<Fx>> traits;
  11235. typedef meta::tuple_types<typename traits::return_type> return_types;
  11236. typedef typename traits::free_args_list args_list;
  11237. // compile-time eliminate any functions that we know ahead of time are of improper arity
  11238. if (!traits::runtime_variadics_t::value && meta::find_in_pack_v<index_value<traits::free_arity>, index_value<M>...>::value) {
  11239. return overload_match_arity(types<Fx1, Fxs...>(), std::index_sequence<I1, In...>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11240. }
  11241. if (!traits::runtime_variadics_t::value && traits::free_arity != fxarity) {
  11242. return overload_match_arity(types<Fx1, Fxs...>(), std::index_sequence<I1, In...>(), std::index_sequence<traits::free_arity, M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11243. }
  11244. stack::record tracking{};
  11245. if (!stack::stack_detail::check_types<true>{}.check(args_list(), L, start, no_panic, tracking)) {
  11246. return overload_match_arity(types<Fx1, Fxs...>(), std::index_sequence<I1, In...>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11247. }
  11248. return matchfx(types<Fx>(), index_value<I>(), return_types(), args_list(), L, fxarity, start, std::forward<Args>(args)...);
  11249. }
  11250. } // namespace overload_detail
  11251. template <typename... Functions, typename Match, typename... Args>
  11252. inline int overload_match_arity(Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  11253. return overload_detail::overload_match_arity_single(types<Functions...>(), std::make_index_sequence<sizeof...(Functions)>(), std::index_sequence<>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11254. }
  11255. template <typename... Functions, typename Match, typename... Args>
  11256. inline int overload_match(Match&& matchfx, lua_State* L, int start, Args&&... args) {
  11257. int fxarity = lua_gettop(L) - (start - 1);
  11258. return overload_match_arity<Functions...>(std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11259. }
  11260. template <typename T, typename... TypeLists, typename Match, typename... Args>
  11261. inline int construct_match(Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  11262. // use same overload resolution matching as all other parts of the framework
  11263. return overload_match_arity<decltype(void_call<T, TypeLists>::call)...>(std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  11264. }
  11265. template <typename T, bool checked, bool clean_stack, typename... TypeLists>
  11266. inline int construct_trampolined(lua_State* L) {
  11267. static const auto& meta = usertype_traits<T>::metatable();
  11268. int argcount = lua_gettop(L);
  11269. call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, usertype_traits<T>::user_metatable(), 1) : call_syntax::dot;
  11270. argcount -= static_cast<int>(syntax);
  11271. T* obj = detail::usertype_allocate<T>(L);
  11272. reference userdataref(L, -1);
  11273. userdataref.pop();
  11274. construct_match<T, TypeLists...>(constructor_match<T, checked, clean_stack>(obj), L, argcount, 1 + static_cast<int>(syntax));
  11275. userdataref.push();
  11276. stack::stack_detail::undefined_metatable<T> umf(L, &meta[0]);
  11277. umf();
  11278. return 1;
  11279. }
  11280. template <typename T, bool checked, bool clean_stack, typename... TypeLists>
  11281. inline int construct(lua_State* L) {
  11282. return detail::static_trampoline<&construct_trampolined<T, checked, clean_stack, TypeLists...>>(L);
  11283. }
  11284. template <typename F, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename = void>
  11285. struct agnostic_lua_call_wrapper {
  11286. typedef wrapper<meta::unqualified_t<F>> wrap;
  11287. template <typename Fx, typename... Args>
  11288. static int convertible_call(std::true_type, lua_State* L, Fx&& f, Args&&... args) {
  11289. typedef typename wrap::traits_type traits_type;
  11290. typedef typename traits_type::function_pointer_type fp_t;
  11291. fp_t fx = f;
  11292. return agnostic_lua_call_wrapper<fp_t, is_index, is_variable, checked, boost, clean_stack>{}.call(L, fx, std::forward<Args>(args)...);
  11293. }
  11294. template <typename Fx, typename... Args>
  11295. static int convertible_call(std::false_type, lua_State* L, Fx&& f, Args&&... args) {
  11296. typedef typename wrap::returns_list returns_list;
  11297. typedef typename wrap::free_args_list args_list;
  11298. typedef typename wrap::caller caller;
  11299. return stack::call_into_lua<checked, clean_stack>(returns_list(), args_list(), L, boost + 1, caller(), std::forward<Fx>(f), std::forward<Args>(args)...);
  11300. }
  11301. template <typename Fx, typename... Args>
  11302. static int call(lua_State* L, Fx&& f, Args&&... args) {
  11303. typedef typename wrap::traits_type traits_type;
  11304. typedef typename traits_type::function_pointer_type fp_t;
  11305. return convertible_call(std::conditional_t<std::is_class<meta::unqualified_t<F>>::value, std::is_convertible<std::decay_t<Fx>, fp_t>, std::false_type>(), L, std::forward<Fx>(f), std::forward<Args>(args)...);
  11306. }
  11307. };
  11308. template <typename T, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11309. struct agnostic_lua_call_wrapper<var_wrapper<T>, true, is_variable, checked, boost, clean_stack, C> {
  11310. template <typename F>
  11311. static int call(lua_State* L, F&& f) {
  11312. typedef is_stack_based<meta::unqualified_t<decltype(detail::unwrap(f.value))>> is_stack;
  11313. if (clean_stack && !is_stack::value) {
  11314. lua_settop(L, 0);
  11315. }
  11316. return stack::push_reference(L, detail::unwrap(f.value));
  11317. }
  11318. };
  11319. template <typename T, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11320. struct agnostic_lua_call_wrapper<var_wrapper<T>, false, is_variable, checked, boost, clean_stack, C> {
  11321. template <typename V>
  11322. static int call_assign(std::true_type, lua_State* L, V&& f) {
  11323. detail::unwrap(f.value) = stack::unqualified_get<meta::unwrapped_t<T>>(L, boost + (is_variable ? 3 : 1));
  11324. if (clean_stack) {
  11325. lua_settop(L, 0);
  11326. }
  11327. return 0;
  11328. }
  11329. template <typename... Args>
  11330. static int call_assign(std::false_type, lua_State* L, Args&&...) {
  11331. return luaL_error(L, "sol: cannot write to this variable: copy assignment/constructor not available");
  11332. }
  11333. template <typename... Args>
  11334. static int call_const(std::false_type, lua_State* L, Args&&... args) {
  11335. typedef meta::unwrapped_t<T> R;
  11336. return call_assign(std::is_assignable<std::add_lvalue_reference_t<meta::unqualified_t<R>>, R>(), L, std::forward<Args>(args)...);
  11337. }
  11338. template <typename... Args>
  11339. static int call_const(std::true_type, lua_State* L, Args&&...) {
  11340. return luaL_error(L, "sol: cannot write to a readonly (const) variable");
  11341. }
  11342. template <typename V>
  11343. static int call(lua_State* L, V&& f) {
  11344. return call_const(std::is_const<meta::unwrapped_t<T>>(), L, f);
  11345. }
  11346. };
  11347. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11348. struct agnostic_lua_call_wrapper<lua_CFunction_ref, is_index, is_variable, checked, boost, clean_stack, C> {
  11349. static int call(lua_State* L, lua_CFunction_ref f) {
  11350. return f(L);
  11351. }
  11352. };
  11353. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11354. struct agnostic_lua_call_wrapper<lua_CFunction, is_index, is_variable, checked, boost, clean_stack, C> {
  11355. static int call(lua_State* L, lua_CFunction f) {
  11356. return f(L);
  11357. }
  11358. };
  11359. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  11360. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11361. struct agnostic_lua_call_wrapper<detail::lua_CFunction_noexcept, is_index, is_variable, checked, boost, clean_stack, C> {
  11362. static int call(lua_State* L, detail::lua_CFunction_noexcept f) {
  11363. return f(L);
  11364. }
  11365. };
  11366. #endif // noexcept function types
  11367. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11368. struct agnostic_lua_call_wrapper<no_prop, is_index, is_variable, checked, boost, clean_stack, C> {
  11369. static int call(lua_State* L, const no_prop&) {
  11370. return luaL_error(L, is_index ? "sol: cannot read from a writeonly property" : "sol: cannot write to a readonly property");
  11371. }
  11372. };
  11373. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11374. struct agnostic_lua_call_wrapper<no_construction, is_index, is_variable, checked, boost, clean_stack, C> {
  11375. static int call(lua_State* L, const no_construction&) {
  11376. return function_detail::no_construction_error(L);
  11377. }
  11378. };
  11379. template <typename... Args, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11380. struct agnostic_lua_call_wrapper<bases<Args...>, is_index, is_variable, checked, boost, clean_stack, C> {
  11381. static int call(lua_State*, const bases<Args...>&) {
  11382. // Uh. How did you even call this, lul
  11383. return 0;
  11384. }
  11385. };
  11386. template <typename T, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11387. struct agnostic_lua_call_wrapper<std::reference_wrapper<T>, is_index, is_variable, checked, boost, clean_stack, C> {
  11388. static int call(lua_State* L, std::reference_wrapper<T> f) {
  11389. return agnostic_lua_call_wrapper<T, is_index, is_variable, checked, boost, clean_stack>{}.call(L, f.get());
  11390. }
  11391. };
  11392. template <typename T, typename F, bool is_index, bool is_variable, bool checked = detail::default_safe_function_calls, int boost = 0, bool clean_stack = true, typename = void>
  11393. struct lua_call_wrapper : agnostic_lua_call_wrapper<F, is_index, is_variable, checked, boost, clean_stack> {};
  11394. template <typename T, typename F, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack>
  11395. struct lua_call_wrapper<T, F, is_index, is_variable, checked, boost, clean_stack, std::enable_if_t<std::is_member_function_pointer<F>::value>> {
  11396. typedef wrapper<meta::unqualified_t<F>> wrap;
  11397. typedef typename wrap::object_type object_type;
  11398. template <typename Fx>
  11399. static int call(lua_State* L, Fx&& f, object_type& o) {
  11400. typedef typename wrap::returns_list returns_list;
  11401. typedef typename wrap::args_list args_list;
  11402. typedef typename wrap::caller caller;
  11403. return stack::call_into_lua<checked, clean_stack>(returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), std::forward<Fx>(f), o);
  11404. }
  11405. template <typename Fx>
  11406. static int call(lua_State* L, Fx&& f) {
  11407. typedef std::conditional_t<std::is_void<T>::value, object_type, T> Ta;
  11408. #if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  11409. auto maybeo = stack::unqualified_check_get<Ta*>(L, 1);
  11410. if (!maybeo || maybeo.value() == nullptr) {
  11411. return luaL_error(L, "sol: received nil for 'self' argument (use ':' for accessing member functions, make sure member variables are preceeded by the actual object with '.' syntax)");
  11412. }
  11413. object_type* o = static_cast<object_type*>(maybeo.value());
  11414. return call(L, std::forward<Fx>(f), *o);
  11415. #else
  11416. object_type& o = static_cast<object_type&>(*stack::unqualified_get<non_null<Ta*>>(L, 1));
  11417. return call(L, std::forward<Fx>(f), o);
  11418. #endif // Safety
  11419. }
  11420. };
  11421. template <typename T, typename F, bool is_variable, bool checked, int boost, bool clean_stack>
  11422. struct lua_call_wrapper<T, F, false, is_variable, checked, boost, clean_stack, std::enable_if_t<std::is_member_object_pointer<F>::value>> {
  11423. typedef lua_bind_traits<F> traits_type;
  11424. typedef wrapper<meta::unqualified_t<F>> wrap;
  11425. typedef typename wrap::object_type object_type;
  11426. template <typename V>
  11427. static int call_assign(std::true_type, lua_State* L, V&& f, object_type& o) {
  11428. typedef typename wrap::args_list args_list;
  11429. typedef typename wrap::caller caller;
  11430. return stack::call_into_lua<checked, clean_stack>(types<void>(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), f, o);
  11431. }
  11432. template <typename V>
  11433. static int call_assign(std::true_type, lua_State* L, V&& f) {
  11434. typedef std::conditional_t<std::is_void<T>::value, object_type, T> Ta;
  11435. #if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  11436. auto maybeo = stack::check_get<Ta*>(L, 1);
  11437. if (!maybeo || maybeo.value() == nullptr) {
  11438. if (is_variable) {
  11439. return luaL_error(L, "sol: received nil for 'self' argument (bad '.' access?)");
  11440. }
  11441. return luaL_error(L, "sol: received nil for 'self' argument (pass 'self' as first argument)");
  11442. }
  11443. object_type* o = static_cast<object_type*>(maybeo.value());
  11444. return call_assign(std::true_type(), L, f, *o);
  11445. #else
  11446. object_type& o = static_cast<object_type&>(*stack::get<non_null<Ta*>>(L, 1));
  11447. return call_assign(std::true_type(), L, f, o);
  11448. #endif // Safety
  11449. }
  11450. template <typename... Args>
  11451. static int call_assign(std::false_type, lua_State* L, Args&&...) {
  11452. return luaL_error(L, "sol: cannot write to this variable: copy assignment/constructor not available");
  11453. }
  11454. template <typename... Args>
  11455. static int call_const(std::false_type, lua_State* L, Args&&... args) {
  11456. typedef typename traits_type::return_type R;
  11457. return call_assign(std::is_copy_assignable<meta::unqualified_t<R>>(), L, std::forward<Args>(args)...);
  11458. }
  11459. template <typename... Args>
  11460. static int call_const(std::true_type, lua_State* L, Args&&...) {
  11461. return luaL_error(L, "sol: cannot write to a readonly (const) variable");
  11462. }
  11463. template <typename V>
  11464. static int call(lua_State* L, V&& f) {
  11465. return call_const(std::is_const<typename traits_type::return_type>(), L, std::forward<V>(f));
  11466. }
  11467. template <typename V>
  11468. static int call(lua_State* L, V&& f, object_type& o) {
  11469. return call_const(std::is_const<typename traits_type::return_type>(), L, std::forward<V>(f), o);
  11470. }
  11471. };
  11472. template <typename T, typename F, bool is_variable, bool checked, int boost, bool clean_stack>
  11473. struct lua_call_wrapper<T, F, true, is_variable, checked, boost, clean_stack, std::enable_if_t<std::is_member_object_pointer<F>::value>> {
  11474. typedef lua_bind_traits<F> traits_type;
  11475. typedef wrapper<meta::unqualified_t<F>> wrap;
  11476. typedef typename wrap::object_type object_type;
  11477. template <typename V>
  11478. static int call(lua_State* L, V&& v, object_type& o) {
  11479. typedef typename wrap::returns_list returns_list;
  11480. typedef typename wrap::caller caller;
  11481. F f(std::forward<V>(v));
  11482. return stack::call_into_lua<checked, clean_stack>(returns_list(), types<>(), L, boost + (is_variable ? 3 : 2), caller(), f, o);
  11483. }
  11484. template <typename V>
  11485. static int call(lua_State* L, V&& f) {
  11486. typedef std::conditional_t<std::is_void<T>::value, object_type, T> Ta;
  11487. #if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  11488. auto maybeo = stack::check_get<Ta*>(L, 1);
  11489. if (!maybeo || maybeo.value() == nullptr) {
  11490. if (is_variable) {
  11491. return luaL_error(L, "sol: 'self' argument is lua_nil (bad '.' access?)");
  11492. }
  11493. return luaL_error(L, "sol: 'self' argument is lua_nil (pass 'self' as first argument)");
  11494. }
  11495. object_type* o = static_cast<object_type*>(maybeo.value());
  11496. return call(L, f, *o);
  11497. #else
  11498. object_type& o = static_cast<object_type&>(*stack::get<non_null<Ta*>>(L, 1));
  11499. return call(L, f, o);
  11500. #endif // Safety
  11501. }
  11502. };
  11503. template <typename T, typename F, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11504. struct lua_call_wrapper<T, readonly_wrapper<F>, false, is_variable, checked, boost, clean_stack, C> {
  11505. typedef lua_bind_traits<F> traits_type;
  11506. typedef wrapper<meta::unqualified_t<F>> wrap;
  11507. typedef typename wrap::object_type object_type;
  11508. template <typename V>
  11509. static int call(lua_State* L, V&&) {
  11510. return luaL_error(L, "sol: cannot write to a sol::readonly variable");
  11511. }
  11512. template <typename V>
  11513. static int call(lua_State* L, V&&, object_type&) {
  11514. return luaL_error(L, "sol: cannot write to a sol::readonly variable");
  11515. }
  11516. };
  11517. template <typename T, typename F, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11518. struct lua_call_wrapper<T, readonly_wrapper<F>, true, is_variable, checked, boost, clean_stack, C> : lua_call_wrapper<T, F, true, is_variable, checked, boost, clean_stack, C> {
  11519. };
  11520. template <typename T, typename... Args, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11521. struct lua_call_wrapper<T, constructor_list<Args...>, is_index, is_variable, checked, boost, clean_stack, C> {
  11522. typedef constructor_list<Args...> F;
  11523. static int call(lua_State* L, F&) {
  11524. const auto& meta = usertype_traits<T>::metatable();
  11525. int argcount = lua_gettop(L);
  11526. call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, usertype_traits<T>::user_metatable(), 1) : call_syntax::dot;
  11527. argcount -= static_cast<int>(syntax);
  11528. T* obj = detail::usertype_allocate<T>(L);
  11529. reference userdataref(L, -1);
  11530. construct_match<T, Args...>(constructor_match<T, false, clean_stack>(obj), L, argcount, boost + 1 + static_cast<int>(syntax));
  11531. userdataref.push();
  11532. stack::stack_detail::undefined_metatable<T> umf(L, &meta[0]);
  11533. umf();
  11534. return 1;
  11535. }
  11536. };
  11537. template <typename T, typename... Cxs, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11538. struct lua_call_wrapper<T, constructor_wrapper<Cxs...>, is_index, is_variable, checked, boost, clean_stack, C> {
  11539. typedef constructor_wrapper<Cxs...> F;
  11540. struct onmatch {
  11541. template <typename Fx, std::size_t I, typename... R, typename... Args>
  11542. int operator()(types<Fx>, index_value<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start, F& f) {
  11543. const auto& meta = usertype_traits<T>::metatable();
  11544. T* obj = detail::usertype_allocate<T>(L);
  11545. reference userdataref(L, -1);
  11546. auto& func = std::get<I>(f.functions);
  11547. stack::call_into_lua<checked, clean_stack>(r, a, L, boost + start, func, detail::implicit_wrapper<T>(obj));
  11548. userdataref.push();
  11549. stack::stack_detail::undefined_metatable<T> umf(L, &meta[0]);
  11550. umf();
  11551. return 1;
  11552. }
  11553. };
  11554. static int call(lua_State* L, F& f) {
  11555. call_syntax syntax = stack::get_call_syntax(L, usertype_traits<T>::user_metatable(), 1);
  11556. int syntaxval = static_cast<int>(syntax);
  11557. int argcount = lua_gettop(L) - syntaxval;
  11558. return construct_match<T, meta::pop_front_type_t<meta::function_args_t<Cxs>>...>(onmatch(), L, argcount, 1 + syntaxval, f);
  11559. }
  11560. };
  11561. template <typename T, typename Fx, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack>
  11562. struct lua_call_wrapper<T, destructor_wrapper<Fx>, is_index, is_variable, checked, boost, clean_stack, std::enable_if_t<std::is_void<Fx>::value>> {
  11563. typedef destructor_wrapper<Fx> F;
  11564. static int call(lua_State* L, const F&) {
  11565. return detail::usertype_alloc_destruct<T>(L);
  11566. }
  11567. };
  11568. template <typename T, typename Fx, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack>
  11569. struct lua_call_wrapper<T, destructor_wrapper<Fx>, is_index, is_variable, checked, boost, clean_stack, std::enable_if_t<!std::is_void<Fx>::value>> {
  11570. typedef destructor_wrapper<Fx> F;
  11571. static int call_void(std::true_type, lua_State* L, const F& f) {
  11572. typedef meta::bind_traits<meta::unqualified_t<decltype(f.fx)>> bt;
  11573. typedef typename bt::template arg_at<0> arg0;
  11574. typedef meta::unqualified_t<arg0> O;
  11575. O& obj = stack::get<O>(L);
  11576. f.fx(detail::implicit_wrapper<O>(obj));
  11577. return 0;
  11578. }
  11579. static int call_void(std::false_type, lua_State* L, const F& f) {
  11580. T& obj = stack::get<T>(L);
  11581. f.fx(detail::implicit_wrapper<T>(obj));
  11582. return 0;
  11583. }
  11584. static int call(lua_State* L, const F& f) {
  11585. return call_void(std::is_void<T>(), L, f);
  11586. }
  11587. };
  11588. template <typename T, typename... Fs, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11589. struct lua_call_wrapper<T, overload_set<Fs...>, is_index, is_variable, checked, boost, clean_stack, C> {
  11590. typedef overload_set<Fs...> F;
  11591. struct on_match {
  11592. template <typename Fx, std::size_t I, typename... R, typename... Args>
  11593. int operator()(types<Fx>, index_value<I>, types<R...>, types<Args...>, lua_State* L, int, int, F& fx) {
  11594. auto& f = std::get<I>(fx.functions);
  11595. return lua_call_wrapper<T, Fx, is_index, is_variable, checked, boost>{}.call(L, f);
  11596. }
  11597. };
  11598. static int call(lua_State* L, F& fx) {
  11599. return overload_match_arity<Fs...>(on_match(), L, lua_gettop(L), 1, fx);
  11600. }
  11601. };
  11602. template <typename T, typename... Fs, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11603. struct lua_call_wrapper<T, factory_wrapper<Fs...>, is_index, is_variable, checked, boost, clean_stack, C> {
  11604. typedef factory_wrapper<Fs...> F;
  11605. struct on_match {
  11606. template <typename Fx, std::size_t I, typename... R, typename... Args>
  11607. int operator()(types<Fx>, index_value<I>, types<R...>, types<Args...>, lua_State* L, int, int, F& fx) {
  11608. auto& f = std::get<I>(fx.functions);
  11609. return lua_call_wrapper<T, Fx, is_index, is_variable, checked, boost, clean_stack>{}.call(L, f);
  11610. }
  11611. };
  11612. static int call(lua_State* L, F& fx) {
  11613. return overload_match_arity<Fs...>(on_match(), L, lua_gettop(L) - boost, 1 + boost, fx);
  11614. }
  11615. };
  11616. template <typename T, typename R, typename W, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11617. struct lua_call_wrapper<T, property_wrapper<R, W>, is_index, is_variable, checked, boost, clean_stack, C> {
  11618. typedef std::conditional_t<is_index, R, W> P;
  11619. typedef meta::unqualified_t<P> U;
  11620. typedef wrapper<U> wrap;
  11621. typedef lua_bind_traits<U> traits_type;
  11622. typedef meta::unqualified_t<typename traits_type::template arg_at<0>> object_type;
  11623. template <typename F>
  11624. static int self_call(std::true_type, lua_State* L, F&& f) {
  11625. // The type being void means we don't have any arguments, so it might be a free functions?
  11626. typedef typename traits_type::free_args_list args_list;
  11627. typedef typename wrap::returns_list returns_list;
  11628. typedef typename wrap::caller caller;
  11629. return stack::call_into_lua<checked, clean_stack>(returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), f);
  11630. }
  11631. template <typename F>
  11632. static int self_call(std::false_type, lua_State* L, F&& f) {
  11633. typedef meta::pop_front_type_t<typename traits_type::free_args_list> args_list;
  11634. typedef T Ta;
  11635. typedef std::remove_pointer_t<object_type> Oa;
  11636. #if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  11637. auto maybeo = stack::check_get<Ta*>(L, 1);
  11638. if (!maybeo || maybeo.value() == nullptr) {
  11639. if (is_variable) {
  11640. return luaL_error(L, "sol: 'self' argument is lua_nil (bad '.' access?)");
  11641. }
  11642. return luaL_error(L, "sol: 'self' argument is lua_nil (pass 'self' as first argument)");
  11643. }
  11644. Oa* o = static_cast<Oa*>(maybeo.value());
  11645. #else
  11646. Oa* o = static_cast<Oa*>(stack::get<non_null<Ta*>>(L, 1));
  11647. #endif // Safety
  11648. typedef typename wrap::returns_list returns_list;
  11649. typedef typename wrap::caller caller;
  11650. return stack::call_into_lua<checked, clean_stack>(returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), f, detail::implicit_wrapper<Oa>(*o));
  11651. }
  11652. template <typename F, typename... Args>
  11653. static int defer_call(std::false_type, lua_State* L, F&& f, Args&&... args) {
  11654. return self_call(meta::any<std::is_void<object_type>, meta::boolean<lua_type_of<meta::unwrap_unqualified_t<object_type>>::value != type::userdata>>(), L, pick(meta::boolean<is_index>(), f), std::forward<Args>(args)...);
  11655. }
  11656. template <typename F, typename... Args>
  11657. static int defer_call(std::true_type, lua_State* L, F&& f, Args&&... args) {
  11658. auto& p = pick(meta::boolean<is_index>(), std::forward<F>(f));
  11659. return lua_call_wrapper<T, meta::unqualified_t<decltype(p)>, is_index, is_variable, checked, boost, clean_stack>{}.call(L, p, std::forward<Args>(args)...);
  11660. }
  11661. template <typename F, typename... Args>
  11662. static int call(lua_State* L, F&& f, Args&&... args) {
  11663. typedef meta::any<
  11664. std::is_void<U>,
  11665. std::is_same<U, no_prop>,
  11666. meta::is_specialization_of<U, var_wrapper>,
  11667. meta::is_specialization_of<U, constructor_wrapper>,
  11668. meta::is_specialization_of<U, constructor_list>,
  11669. std::is_member_pointer<U>>
  11670. is_specialized;
  11671. return defer_call(is_specialized(), L, std::forward<F>(f), std::forward<Args>(args)...);
  11672. }
  11673. };
  11674. template <typename T, typename V, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11675. struct lua_call_wrapper<T, protect_t<V>, is_index, is_variable, checked, boost, clean_stack, C> {
  11676. typedef protect_t<V> F;
  11677. template <typename... Args>
  11678. static int call(lua_State* L, F& fx, Args&&... args) {
  11679. return lua_call_wrapper<T, V, is_index, is_variable, true, boost, clean_stack>{}.call(L, fx.value, std::forward<Args>(args)...);
  11680. }
  11681. };
  11682. template <typename T, typename F, typename... Filters, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11683. struct lua_call_wrapper<T, filter_wrapper<F, Filters...>, is_index, is_variable, checked, boost, clean_stack, C> {
  11684. typedef filter_wrapper<F, Filters...> P;
  11685. template <std::size_t... In>
  11686. static int call(std::index_sequence<In...>, lua_State* L, P& fx) {
  11687. int pushed = lua_call_wrapper<T, F, is_index, is_variable, checked, boost, false, C>{}.call(L, fx.value);
  11688. (void)detail::swallow{ int(), (filter_detail::handle_filter(std::get<In>(fx.filters), L, pushed), int())... };
  11689. return pushed;
  11690. }
  11691. static int call(lua_State* L, P& fx) {
  11692. typedef typename P::indices indices;
  11693. return call(indices(), L, fx);
  11694. }
  11695. };
  11696. template <typename T, typename Sig, typename P, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  11697. struct lua_call_wrapper<T, function_arguments<Sig, P>, is_index, is_variable, checked, boost, clean_stack, C> {
  11698. template <typename F>
  11699. static int call(lua_State* L, F&& f) {
  11700. return lua_call_wrapper<T, meta::unqualified_t<P>, is_index, is_variable, checked, boost, clean_stack>{}.call(L, std::get<0>(f.arguments));
  11701. }
  11702. };
  11703. template <typename T, bool is_index, bool is_variable, int boost = 0, bool checked = detail::default_safe_function_calls, bool clean_stack = true, typename Fx, typename... Args>
  11704. inline int call_wrapped(lua_State* L, Fx&& fx, Args&&... args) {
  11705. return lua_call_wrapper<T, meta::unqualified_t<Fx>, is_index, is_variable, checked, boost, clean_stack>{}.call(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  11706. }
  11707. template <typename T, bool is_index, bool is_variable, typename F, int start = 1, bool checked = detail::default_safe_function_calls, bool clean_stack = true>
  11708. inline int call_user(lua_State* L) {
  11709. auto& fx = stack::unqualified_get<user<F>>(L, upvalue_index(start));
  11710. return call_wrapped<T, is_index, is_variable, 0, checked, clean_stack>(L, fx);
  11711. }
  11712. template <typename T, typename = void>
  11713. struct is_var_bind : std::false_type {};
  11714. template <typename T>
  11715. struct is_var_bind<T, std::enable_if_t<std::is_member_object_pointer<T>::value>> : std::true_type {};
  11716. template <>
  11717. struct is_var_bind<no_prop> : std::true_type {};
  11718. template <typename R, typename W>
  11719. struct is_var_bind<property_wrapper<R, W>> : std::true_type {};
  11720. template <typename T>
  11721. struct is_var_bind<var_wrapper<T>> : std::true_type {};
  11722. template <typename T>
  11723. struct is_var_bind<readonly_wrapper<T>> : is_var_bind<meta::unqualified_t<T>> {};
  11724. template <typename F, typename... Filters>
  11725. struct is_var_bind<filter_wrapper<F, Filters...>> : is_var_bind<meta::unqualified_t<F>> {};
  11726. } // namespace call_detail
  11727. template <typename T>
  11728. struct is_variable_binding : call_detail::is_var_bind<meta::unqualified_t<T>> {};
  11729. template <typename T>
  11730. struct is_function_binding : meta::neg<is_variable_binding<T>> {};
  11731. } // namespace sol
  11732. // end of sol/call.hpp
  11733. namespace sol {
  11734. namespace function_detail {
  11735. template <typename F, F fx>
  11736. inline int call_wrapper_variable(std::false_type, lua_State* L) {
  11737. typedef meta::bind_traits<meta::unqualified_t<F>> traits_type;
  11738. typedef typename traits_type::args_list args_list;
  11739. typedef meta::tuple_types<typename traits_type::return_type> return_type;
  11740. return stack::call_into_lua(return_type(), args_list(), L, 1, fx);
  11741. }
  11742. template <typename R, typename V, V, typename T>
  11743. inline int call_set_assignable(std::false_type, T&&, lua_State* L) {
  11744. return luaL_error(L, "cannot write to this type: copy assignment/constructor not available");
  11745. }
  11746. template <typename R, typename V, V variable, typename T>
  11747. inline int call_set_assignable(std::true_type, lua_State* L, T&& mem) {
  11748. (mem.*variable) = stack::get<R>(L, 2);
  11749. return 0;
  11750. }
  11751. template <typename R, typename V, V, typename T>
  11752. inline int call_set_variable(std::false_type, lua_State* L, T&&) {
  11753. return luaL_error(L, "cannot write to a const variable");
  11754. }
  11755. template <typename R, typename V, V variable, typename T>
  11756. inline int call_set_variable(std::true_type, lua_State* L, T&& mem) {
  11757. return call_set_assignable<R, V, variable>(std::is_assignable<std::add_lvalue_reference_t<R>, R>(), L, std::forward<T>(mem));
  11758. }
  11759. template <typename V, V variable>
  11760. inline int call_wrapper_variable(std::true_type, lua_State* L) {
  11761. typedef meta::bind_traits<meta::unqualified_t<V>> traits_type;
  11762. typedef typename traits_type::object_type T;
  11763. typedef typename traits_type::return_type R;
  11764. auto& mem = stack::get<T>(L, 1);
  11765. switch (lua_gettop(L)) {
  11766. case 1: {
  11767. decltype(auto) r = (mem.*variable);
  11768. stack::push_reference(L, std::forward<decltype(r)>(r));
  11769. return 1;
  11770. }
  11771. case 2:
  11772. return call_set_variable<R, V, variable>(meta::neg<std::is_const<R>>(), L, mem);
  11773. default:
  11774. return luaL_error(L, "incorrect number of arguments to member variable function call");
  11775. }
  11776. }
  11777. template <typename F, F fx>
  11778. inline int call_wrapper_function(std::false_type, lua_State* L) {
  11779. return call_wrapper_variable<F, fx>(std::is_member_object_pointer<F>(), L);
  11780. }
  11781. template <typename F, F fx>
  11782. inline int call_wrapper_function(std::true_type, lua_State* L) {
  11783. return call_detail::call_wrapped<void, false, false>(L, fx);
  11784. }
  11785. template <typename F, F fx>
  11786. int call_wrapper_entry(lua_State* L) noexcept(meta::bind_traits<F>::is_noexcept) {
  11787. return call_wrapper_function<F, fx>(std::is_member_function_pointer<meta::unqualified_t<F>>(), L);
  11788. }
  11789. template <typename... Fxs>
  11790. struct c_call_matcher {
  11791. template <typename Fx, std::size_t I, typename R, typename... Args>
  11792. int operator()(types<Fx>, index_value<I>, types<R>, types<Args...>, lua_State* L, int, int) const {
  11793. typedef meta::at_in_pack_t<I, Fxs...> target;
  11794. return target::call(L);
  11795. }
  11796. };
  11797. template <typename F, F fx>
  11798. inline int c_call_raw(std::true_type, lua_State* L) {
  11799. return fx(L);
  11800. }
  11801. template <typename F, F fx>
  11802. inline int c_call_raw(std::false_type, lua_State* L) {
  11803. #ifdef __clang__
  11804. return detail::trampoline(L, function_detail::call_wrapper_entry<F, fx>);
  11805. #else
  11806. return detail::typed_static_trampoline<decltype(&function_detail::call_wrapper_entry<F, fx>), (&function_detail::call_wrapper_entry<F, fx>)>(L);
  11807. #endif // fuck you clang :c
  11808. }
  11809. } // namespace function_detail
  11810. template <typename F, F fx>
  11811. inline int c_call(lua_State* L) {
  11812. typedef meta::unqualified_t<F> Fu;
  11813. typedef std::integral_constant<bool, std::is_same<Fu, lua_CFunction>::value
  11814. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  11815. || std::is_same<Fu, detail::lua_CFunction_noexcept>::value
  11816. #endif
  11817. > is_raw;
  11818. return function_detail::c_call_raw<F, fx>(is_raw(), L);
  11819. }
  11820. template <typename F, F f>
  11821. struct wrap {
  11822. typedef F type;
  11823. static int call(lua_State* L) {
  11824. return c_call<type, f>(L);
  11825. }
  11826. };
  11827. template <typename... Fxs>
  11828. inline int c_call(lua_State* L) {
  11829. if (sizeof...(Fxs) < 2) {
  11830. return meta::at_in_pack_t<0, Fxs...>::call(L);
  11831. }
  11832. else {
  11833. return call_detail::overload_match_arity<typename Fxs::type...>(function_detail::c_call_matcher<Fxs...>(), L, lua_gettop(L), 1);
  11834. }
  11835. }
  11836. } // namespace sol
  11837. // end of sol/function_types_templated.hpp
  11838. // beginning of sol/function_types_stateless.hpp
  11839. namespace sol {
  11840. namespace function_detail {
  11841. template <typename Function, bool is_yielding>
  11842. struct upvalue_free_function {
  11843. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  11844. typedef meta::bind_traits<function_type> traits_type;
  11845. static int real_call(lua_State* L) noexcept(traits_type::is_noexcept) {
  11846. auto udata = stack::stack_detail::get_as_upvalues<function_type*>(L);
  11847. function_type* fx = udata.first;
  11848. return call_detail::call_wrapped<void, true, false>(L, fx);
  11849. }
  11850. static int call(lua_State* L) {
  11851. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  11852. if (is_yielding) {
  11853. return lua_yield(L, nr);
  11854. }
  11855. else {
  11856. return nr;
  11857. }
  11858. }
  11859. int operator()(lua_State* L) {
  11860. return call(L);
  11861. }
  11862. };
  11863. template <typename T, typename Function, bool is_yielding>
  11864. struct upvalue_member_function {
  11865. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  11866. typedef lua_bind_traits<function_type> traits_type;
  11867. static int real_call(lua_State* L) noexcept(traits_type::is_noexcept) {
  11868. // Layout:
  11869. // idx 1...n: verbatim data of member function pointer
  11870. // idx n + 1: is the object's void pointer
  11871. // We don't need to store the size, because the other side is templated
  11872. // with the same member function pointer type
  11873. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  11874. auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
  11875. function_type& memfx = memberdata.first;
  11876. auto& item = *objdata.first;
  11877. return call_detail::call_wrapped<T, true, false, -1>(L, memfx, item);
  11878. }
  11879. static int call(lua_State* L) {
  11880. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  11881. if (is_yielding) {
  11882. return lua_yield(L, nr);
  11883. }
  11884. else {
  11885. return nr;
  11886. }
  11887. }
  11888. int operator()(lua_State* L) {
  11889. return call(L);
  11890. }
  11891. };
  11892. template <typename T, typename Function, bool is_yielding>
  11893. struct upvalue_member_variable {
  11894. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  11895. typedef lua_bind_traits<function_type> traits_type;
  11896. static int real_call(lua_State* L) noexcept(traits_type::is_noexcept) {
  11897. // Layout:
  11898. // idx 1...n: verbatim data of member variable pointer
  11899. // idx n + 1: is the object's void pointer
  11900. // We don't need to store the size, because the other side is templated
  11901. // with the same member function pointer type
  11902. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  11903. auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
  11904. auto& mem = *objdata.first;
  11905. function_type& var = memberdata.first;
  11906. switch (lua_gettop(L)) {
  11907. case 0:
  11908. return call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
  11909. case 1:
  11910. return call_detail::call_wrapped<T, false, false, -1>(L, var, mem);
  11911. default:
  11912. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  11913. }
  11914. }
  11915. static int call(lua_State* L) {
  11916. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  11917. if (is_yielding) {
  11918. return lua_yield(L, nr);
  11919. }
  11920. else {
  11921. return nr;
  11922. }
  11923. }
  11924. int operator()(lua_State* L) {
  11925. return call(L);
  11926. }
  11927. };
  11928. template <typename T, typename Function, bool is_yielding>
  11929. struct upvalue_member_variable<T, readonly_wrapper<Function>, is_yielding> {
  11930. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  11931. typedef lua_bind_traits<function_type> traits_type;
  11932. static int real_call(lua_State* L) noexcept(traits_type::is_noexcept) {
  11933. // Layout:
  11934. // idx 1...n: verbatim data of member variable pointer
  11935. // idx n + 1: is the object's void pointer
  11936. // We don't need to store the size, because the other side is templated
  11937. // with the same member function pointer type
  11938. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  11939. auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
  11940. auto& mem = *objdata.first;
  11941. function_type& var = memberdata.first;
  11942. switch (lua_gettop(L)) {
  11943. case 0:
  11944. return call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
  11945. default:
  11946. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  11947. }
  11948. }
  11949. static int call(lua_State* L) {
  11950. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  11951. if (is_yielding) {
  11952. return lua_yield(L, nr);
  11953. }
  11954. else {
  11955. return nr;
  11956. }
  11957. }
  11958. int operator()(lua_State* L) {
  11959. return call(L);
  11960. }
  11961. };
  11962. template <typename T, typename Function, bool is_yielding>
  11963. struct upvalue_this_member_function {
  11964. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  11965. typedef lua_bind_traits<function_type> traits_type;
  11966. static int real_call(lua_State* L) noexcept(traits_type::is_noexcept) {
  11967. // Layout:
  11968. // idx 1...n: verbatim data of member variable pointer
  11969. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  11970. function_type& memfx = memberdata.first;
  11971. return call_detail::call_wrapped<T, false, false>(L, memfx);
  11972. }
  11973. static int call(lua_State* L) {
  11974. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  11975. if (is_yielding) {
  11976. return lua_yield(L, nr);
  11977. }
  11978. else {
  11979. return nr;
  11980. }
  11981. }
  11982. int operator()(lua_State* L) {
  11983. return call(L);
  11984. }
  11985. };
  11986. template <typename T, typename Function, bool is_yielding>
  11987. struct upvalue_this_member_variable {
  11988. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  11989. static int real_call(lua_State* L) noexcept(false) {
  11990. // Layout:
  11991. // idx 1...n: verbatim data of member variable pointer
  11992. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  11993. function_type& var = memberdata.first;
  11994. switch (lua_gettop(L)) {
  11995. case 1:
  11996. return call_detail::call_wrapped<T, true, false>(L, var);
  11997. case 2:
  11998. return call_detail::call_wrapped<T, false, false>(L, var);
  11999. default:
  12000. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  12001. }
  12002. }
  12003. static int call(lua_State* L) {
  12004. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  12005. if (is_yielding) {
  12006. return lua_yield(L, nr);
  12007. }
  12008. else {
  12009. return nr;
  12010. }
  12011. }
  12012. int operator()(lua_State* L) {
  12013. return call(L);
  12014. }
  12015. };
  12016. template <typename T, typename Function, bool is_yielding>
  12017. struct upvalue_this_member_variable<T, readonly_wrapper<Function>, is_yielding> {
  12018. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  12019. typedef lua_bind_traits<function_type> traits_type;
  12020. static int real_call(lua_State* L) noexcept(false) {
  12021. // Layout:
  12022. // idx 1...n: verbatim data of member variable pointer
  12023. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  12024. function_type& var = memberdata.first;
  12025. switch (lua_gettop(L)) {
  12026. case 1:
  12027. return call_detail::call_wrapped<T, true, false>(L, var);
  12028. default:
  12029. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  12030. }
  12031. }
  12032. static int call(lua_State* L) {
  12033. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  12034. if (is_yielding) {
  12035. return lua_yield(L, nr);
  12036. }
  12037. else {
  12038. return nr;
  12039. }
  12040. }
  12041. int operator()(lua_State* L) {
  12042. return call(L);
  12043. }
  12044. };
  12045. }
  12046. } // namespace sol::function_detail
  12047. // end of sol/function_types_stateless.hpp
  12048. // beginning of sol/function_types_stateful.hpp
  12049. namespace sol {
  12050. namespace function_detail {
  12051. template <typename Func, bool is_yielding, bool no_trampoline>
  12052. struct functor_function {
  12053. typedef std::decay_t<meta::unwrap_unqualified_t<Func>> function_type;
  12054. function_type fx;
  12055. template <typename... Args>
  12056. functor_function(function_type f, Args&&... args)
  12057. : fx(std::move(f), std::forward<Args>(args)...) {
  12058. }
  12059. int call(lua_State* L) {
  12060. int nr = call_detail::call_wrapped<void, true, false>(L, fx);
  12061. if (is_yielding) {
  12062. return lua_yield(L, nr);
  12063. }
  12064. else {
  12065. return nr;
  12066. }
  12067. }
  12068. int operator()(lua_State* L) {
  12069. if (!no_trampoline) {
  12070. auto f = [&](lua_State*) -> int { return this->call(L); };
  12071. return detail::trampoline(L, f);
  12072. }
  12073. else {
  12074. return call(L);
  12075. }
  12076. }
  12077. };
  12078. template <typename T, typename Function, bool is_yielding>
  12079. struct member_function {
  12080. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  12081. typedef meta::function_return_t<function_type> return_type;
  12082. typedef meta::function_args_t<function_type> args_lists;
  12083. function_type invocation;
  12084. T member;
  12085. template <typename... Args>
  12086. member_function(function_type f, Args&&... args)
  12087. : invocation(std::move(f)), member(std::forward<Args>(args)...) {
  12088. }
  12089. int call(lua_State* L) {
  12090. int nr = call_detail::call_wrapped<T, true, false, -1>(L, invocation, detail::unwrap(detail::deref(member)));
  12091. if (is_yielding) {
  12092. return lua_yield(L, nr);
  12093. }
  12094. else {
  12095. return nr;
  12096. }
  12097. }
  12098. int operator()(lua_State* L) {
  12099. auto f = [&](lua_State*) -> int { return this->call(L); };
  12100. return detail::trampoline(L, f);
  12101. }
  12102. };
  12103. template <typename T, typename Function, bool is_yielding>
  12104. struct member_variable {
  12105. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  12106. typedef typename meta::bind_traits<function_type>::return_type return_type;
  12107. typedef typename meta::bind_traits<function_type>::args_list args_lists;
  12108. function_type var;
  12109. T member;
  12110. typedef std::add_lvalue_reference_t<meta::unwrapped_t<std::remove_reference_t<decltype(detail::deref(member))>>> M;
  12111. template <typename... Args>
  12112. member_variable(function_type v, Args&&... args)
  12113. : var(std::move(v)), member(std::forward<Args>(args)...) {
  12114. }
  12115. int call(lua_State* L) {
  12116. int nr;
  12117. {
  12118. M mem = detail::unwrap(detail::deref(member));
  12119. switch (lua_gettop(L)) {
  12120. case 0:
  12121. nr = call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
  12122. break;
  12123. case 1:
  12124. nr = call_detail::call_wrapped<T, false, false, -1>(L, var, mem);
  12125. break;
  12126. default:
  12127. nr = luaL_error(L, "sol: incorrect number of arguments to member variable function");
  12128. break;
  12129. }
  12130. }
  12131. if (is_yielding) {
  12132. return lua_yield(L, nr);
  12133. }
  12134. else {
  12135. return nr;
  12136. }
  12137. }
  12138. int operator()(lua_State* L) {
  12139. auto f = [&](lua_State*) -> int { return this->call(L); };
  12140. return detail::trampoline(L, f);
  12141. }
  12142. };
  12143. }
  12144. } // namespace sol::function_detail
  12145. // end of sol/function_types_stateful.hpp
  12146. // beginning of sol/function_types_overloaded.hpp
  12147. namespace sol {
  12148. namespace function_detail {
  12149. template <int start_skew = 0, typename... Functions>
  12150. struct overloaded_function {
  12151. typedef std::tuple<Functions...> overload_list;
  12152. typedef std::make_index_sequence<sizeof...(Functions)> indices;
  12153. overload_list overloads;
  12154. overloaded_function(overload_list set)
  12155. : overloads(std::move(set)) {
  12156. }
  12157. overloaded_function(Functions... fxs)
  12158. : overloads(fxs...) {
  12159. }
  12160. template <typename Fx, std::size_t I, typename... R, typename... Args>
  12161. int call(types<Fx>, index_value<I>, types<R...>, types<Args...>, lua_State* L, int, int) {
  12162. auto& func = std::get<I>(overloads);
  12163. return call_detail::call_wrapped<void, true, false, start_skew>(L, func);
  12164. }
  12165. int operator()(lua_State* L) {
  12166. auto mfx = [&](auto&&... args) { return this->call(std::forward<decltype(args)>(args)...); };
  12167. return call_detail::overload_match<Functions...>(mfx, L, 1 + start_skew);
  12168. }
  12169. };
  12170. }
  12171. } // namespace sol::function_detail
  12172. // end of sol/function_types_overloaded.hpp
  12173. // beginning of sol/resolve.hpp
  12174. namespace sol {
  12175. #ifndef __clang__
  12176. // constexpr is fine for not-clang
  12177. namespace detail {
  12178. template <typename R, typename... Args, typename F, typename = std::result_of_t<meta::unqualified_t<F>(Args...)>>
  12179. inline constexpr auto resolve_i(types<R(Args...)>, F &&) -> R (meta::unqualified_t<F>::*)(Args...) {
  12180. using Sig = R(Args...);
  12181. typedef meta::unqualified_t<F> Fu;
  12182. return static_cast<Sig Fu::*>(&Fu::operator());
  12183. }
  12184. template <typename F, typename U = meta::unqualified_t<F>>
  12185. inline constexpr auto resolve_f(std::true_type, F&& f)
  12186. -> decltype(resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f))) {
  12187. return resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f));
  12188. }
  12189. template <typename F>
  12190. inline constexpr void resolve_f(std::false_type, F&&) {
  12191. static_assert(meta::has_deducible_signature<F>::value,
  12192. "Cannot use no-template-parameter call with an overloaded functor: specify the signature");
  12193. }
  12194. template <typename F, typename U = meta::unqualified_t<F>>
  12195. inline constexpr auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::has_deducible_signature<U>(), std::forward<F>(f))) {
  12196. return resolve_f(meta::has_deducible_signature<U>{}, std::forward<F>(f));
  12197. }
  12198. template <typename... Args, typename F, typename R = std::result_of_t<F&(Args...)>>
  12199. inline constexpr auto resolve_i(types<Args...>, F&& f) -> decltype(resolve_i(types<R(Args...)>(), std::forward<F>(f))) {
  12200. return resolve_i(types<R(Args...)>(), std::forward<F>(f));
  12201. }
  12202. template <typename Sig, typename C>
  12203. inline constexpr Sig C::*resolve_v(std::false_type, Sig C::*mem_func_ptr) {
  12204. return mem_func_ptr;
  12205. }
  12206. template <typename Sig, typename C>
  12207. inline constexpr Sig C::*resolve_v(std::true_type, Sig C::*mem_variable_ptr) {
  12208. return mem_variable_ptr;
  12209. }
  12210. } // namespace detail
  12211. template <typename... Args, typename R>
  12212. inline constexpr auto resolve(R fun_ptr(Args...)) -> R (*)(Args...) {
  12213. return fun_ptr;
  12214. }
  12215. template <typename Sig>
  12216. inline constexpr Sig* resolve(Sig* fun_ptr) {
  12217. return fun_ptr;
  12218. }
  12219. template <typename... Args, typename R, typename C>
  12220. inline constexpr auto resolve(R (C::*mem_ptr)(Args...)) -> R (C::*)(Args...) {
  12221. return mem_ptr;
  12222. }
  12223. template <typename Sig, typename C>
  12224. inline constexpr Sig C::*resolve(Sig C::*mem_ptr) {
  12225. return detail::resolve_v(std::is_member_object_pointer<Sig C::*>(), mem_ptr);
  12226. }
  12227. template <typename... Sig, typename F, meta::disable<std::is_function<meta::unqualified_t<F>>> = meta::enabler>
  12228. inline constexpr auto resolve(F&& f) -> decltype(detail::resolve_i(types<Sig...>(), std::forward<F>(f))) {
  12229. return detail::resolve_i(types<Sig...>(), std::forward<F>(f));
  12230. }
  12231. #else
  12232. // Clang has distinct problems with constexpr arguments,
  12233. // so don't use the constexpr versions inside of clang.
  12234. namespace detail {
  12235. template <typename R, typename... Args, typename F, typename = std::result_of_t<meta::unqualified_t<F>(Args...)>>
  12236. inline auto resolve_i(types<R(Args...)>, F &&) -> R (meta::unqualified_t<F>::*)(Args...) {
  12237. using Sig = R(Args...);
  12238. typedef meta::unqualified_t<F> Fu;
  12239. return static_cast<Sig Fu::*>(&Fu::operator());
  12240. }
  12241. template <typename F, typename U = meta::unqualified_t<F>>
  12242. inline auto resolve_f(std::true_type, F&& f)
  12243. -> decltype(resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f))) {
  12244. return resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f));
  12245. }
  12246. template <typename F>
  12247. inline void resolve_f(std::false_type, F&&) {
  12248. static_assert(meta::has_deducible_signature<F>::value,
  12249. "Cannot use no-template-parameter call with an overloaded functor: specify the signature");
  12250. }
  12251. template <typename F, typename U = meta::unqualified_t<F>>
  12252. inline auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::has_deducible_signature<U>(), std::forward<F>(f))) {
  12253. return resolve_f(meta::has_deducible_signature<U>{}, std::forward<F>(f));
  12254. }
  12255. template <typename... Args, typename F, typename R = std::result_of_t<F&(Args...)>>
  12256. inline auto resolve_i(types<Args...>, F&& f) -> decltype(resolve_i(types<R(Args...)>(), std::forward<F>(f))) {
  12257. return resolve_i(types<R(Args...)>(), std::forward<F>(f));
  12258. }
  12259. template <typename Sig, typename C>
  12260. inline Sig C::*resolve_v(std::false_type, Sig C::*mem_func_ptr) {
  12261. return mem_func_ptr;
  12262. }
  12263. template <typename Sig, typename C>
  12264. inline Sig C::*resolve_v(std::true_type, Sig C::*mem_variable_ptr) {
  12265. return mem_variable_ptr;
  12266. }
  12267. } // namespace detail
  12268. template <typename... Args, typename R>
  12269. inline auto resolve(R fun_ptr(Args...)) -> R (*)(Args...) {
  12270. return fun_ptr;
  12271. }
  12272. template <typename Sig>
  12273. inline Sig* resolve(Sig* fun_ptr) {
  12274. return fun_ptr;
  12275. }
  12276. template <typename... Args, typename R, typename C>
  12277. inline auto resolve(R (C::*mem_ptr)(Args...)) -> R (C::*)(Args...) {
  12278. return mem_ptr;
  12279. }
  12280. template <typename Sig, typename C>
  12281. inline Sig C::*resolve(Sig C::*mem_ptr) {
  12282. return detail::resolve_v(std::is_member_object_pointer<Sig C::*>(), mem_ptr);
  12283. }
  12284. template <typename... Sig, typename F>
  12285. inline auto resolve(F&& f) -> decltype(detail::resolve_i(types<Sig...>(), std::forward<F>(f))) {
  12286. return detail::resolve_i(types<Sig...>(), std::forward<F>(f));
  12287. }
  12288. #endif
  12289. } // namespace sol
  12290. // end of sol/resolve.hpp
  12291. namespace sol {
  12292. namespace function_detail {
  12293. template <typename T>
  12294. struct class_indicator {};
  12295. struct call_indicator {};
  12296. } // namespace function_detail
  12297. namespace stack {
  12298. template <typename... Sigs>
  12299. struct pusher<function_sig<Sigs...>> {
  12300. template <bool is_yielding, typename... Sig, typename Fx, typename... Args>
  12301. static void select_convertible(std::false_type, types<Sig...>, lua_State* L, Fx&& fx, Args&&... args) {
  12302. typedef std::remove_pointer_t<std::decay_t<Fx>> clean_fx;
  12303. typedef function_detail::functor_function<clean_fx, is_yielding, true> F;
  12304. set_fx<false, F>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12305. }
  12306. template <bool is_yielding, typename R, typename... A, typename Fx, typename... Args>
  12307. static void select_convertible(std::true_type, types<R(A...)>, lua_State* L, Fx&& fx, Args&&... args) {
  12308. using fx_ptr_t = R (*)(A...);
  12309. fx_ptr_t fxptr = detail::unwrap(std::forward<Fx>(fx));
  12310. select_function<is_yielding>(std::true_type(), L, fxptr, std::forward<Args>(args)...);
  12311. }
  12312. template <bool is_yielding, typename R, typename... A, typename Fx, typename... Args>
  12313. static void select_convertible(types<R(A...)> t, lua_State* L, Fx&& fx, Args&&... args) {
  12314. typedef std::decay_t<meta::unwrap_unqualified_t<Fx>> raw_fx_t;
  12315. typedef R (*fx_ptr_t)(A...);
  12316. typedef std::is_convertible<raw_fx_t, fx_ptr_t> is_convertible;
  12317. select_convertible<is_yielding>(is_convertible(), t, L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12318. }
  12319. template <bool is_yielding, typename Fx, typename... Args>
  12320. static void select_convertible(types<>, lua_State* L, Fx&& fx, Args&&... args) {
  12321. typedef meta::function_signature_t<meta::unwrap_unqualified_t<Fx>> Sig;
  12322. select_convertible<is_yielding>(types<Sig>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12323. }
  12324. template <bool is_yielding, typename Fx, typename T, typename... Args>
  12325. static void select_reference_member_variable(std::false_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  12326. typedef std::remove_pointer_t<std::decay_t<Fx>> clean_fx;
  12327. typedef function_detail::member_variable<meta::unwrap_unqualified_t<T>, clean_fx, is_yielding> F;
  12328. set_fx<false, F>(L, std::forward<Fx>(fx), std::forward<T>(obj), std::forward<Args>(args)...);
  12329. }
  12330. template <bool is_yielding, typename Fx, typename T, typename... Args>
  12331. static void select_reference_member_variable(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  12332. typedef std::decay_t<Fx> dFx;
  12333. dFx memfxptr(std::forward<Fx>(fx));
  12334. auto userptr = detail::ptr(std::forward<T>(obj), std::forward<Args>(args)...);
  12335. lua_CFunction freefunc = &function_detail::upvalue_member_variable<std::decay_t<decltype(*userptr)>, meta::unqualified_t<Fx>, is_yielding>::call;
  12336. int upvalues = 0;
  12337. upvalues += stack::push(L, nullptr);
  12338. upvalues += stack::stack_detail::push_as_upvalues(L, memfxptr);
  12339. upvalues += stack::push(L, lightuserdata_value(static_cast<void*>(userptr)));
  12340. stack::push(L, c_closure(freefunc, upvalues));
  12341. }
  12342. template <bool is_yielding, typename Fx, typename... Args>
  12343. static void select_member_variable(std::false_type, lua_State* L, Fx&& fx, Args&&... args) {
  12344. select_convertible<is_yielding>(types<Sigs...>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12345. }
  12346. template <bool is_yielding, typename Fx, typename T, typename... Args, meta::disable<meta::is_specialization_of<meta::unqualified_t<T>, function_detail::class_indicator>> = meta::enabler>
  12347. static void select_member_variable(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  12348. typedef meta::boolean<meta::is_specialization_of<meta::unqualified_t<T>, std::reference_wrapper>::value || std::is_pointer<T>::value> is_reference;
  12349. select_reference_member_variable<is_yielding>(is_reference(), L, std::forward<Fx>(fx), std::forward<T>(obj), std::forward<Args>(args)...);
  12350. }
  12351. template <bool is_yielding, typename Fx, typename C>
  12352. static void select_member_variable(std::true_type, lua_State* L, Fx&& fx, function_detail::class_indicator<C>) {
  12353. lua_CFunction freefunc = &function_detail::upvalue_this_member_variable<C, Fx, is_yielding>::call;
  12354. int upvalues = 0;
  12355. upvalues += stack::push(L, nullptr);
  12356. upvalues += stack::stack_detail::push_as_upvalues(L, fx);
  12357. stack::push(L, c_closure(freefunc, upvalues));
  12358. }
  12359. template <bool is_yielding, typename Fx>
  12360. static void select_member_variable(std::true_type, lua_State* L, Fx&& fx) {
  12361. typedef typename meta::bind_traits<meta::unqualified_t<Fx>>::object_type C;
  12362. lua_CFunction freefunc = &function_detail::upvalue_this_member_variable<C, Fx, is_yielding>::call;
  12363. int upvalues = 0;
  12364. upvalues += stack::push(L, nullptr);
  12365. upvalues += stack::stack_detail::push_as_upvalues(L, fx);
  12366. stack::push(L, c_closure(freefunc, upvalues));
  12367. }
  12368. template <bool is_yielding, typename Fx, typename T, typename... Args>
  12369. static void select_reference_member_function(std::false_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  12370. typedef std::decay_t<Fx> clean_fx;
  12371. typedef function_detail::member_function<meta::unwrap_unqualified_t<T>, clean_fx, is_yielding> F;
  12372. set_fx<false, F>(L, std::forward<Fx>(fx), std::forward<T>(obj), std::forward<Args>(args)...);
  12373. }
  12374. template <bool is_yielding, typename Fx, typename T, typename... Args>
  12375. static void select_reference_member_function(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  12376. typedef std::decay_t<Fx> dFx;
  12377. dFx memfxptr(std::forward<Fx>(fx));
  12378. auto userptr = detail::ptr(std::forward<T>(obj), std::forward<Args>(args)...);
  12379. lua_CFunction freefunc = &function_detail::upvalue_member_function<std::decay_t<decltype(*userptr)>, meta::unqualified_t<Fx>, is_yielding>::call;
  12380. int upvalues = 0;
  12381. upvalues += stack::push(L, nullptr);
  12382. upvalues += stack::stack_detail::push_as_upvalues(L, memfxptr);
  12383. upvalues += stack::push(L, lightuserdata_value(static_cast<void*>(userptr)));
  12384. stack::push(L, c_closure(freefunc, upvalues));
  12385. }
  12386. template <bool is_yielding, typename Fx, typename... Args>
  12387. static void select_member_function(std::false_type, lua_State* L, Fx&& fx, Args&&... args) {
  12388. select_member_variable<is_yielding>(meta::is_member_object<meta::unqualified_t<Fx>>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12389. }
  12390. template <bool is_yielding, typename Fx, typename T, typename... Args, meta::disable<meta::is_specialization_of<meta::unqualified_t<T>, function_detail::class_indicator>> = meta::enabler>
  12391. static void select_member_function(std::true_type, lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  12392. typedef meta::boolean<meta::is_specialization_of<meta::unqualified_t<T>, std::reference_wrapper>::value || std::is_pointer<T>::value> is_reference;
  12393. select_reference_member_function<is_yielding>(is_reference(), L, std::forward<Fx>(fx), std::forward<T>(obj), std::forward<Args>(args)...);
  12394. }
  12395. template <bool is_yielding, typename Fx, typename C>
  12396. static void select_member_function(std::true_type, lua_State* L, Fx&& fx, function_detail::class_indicator<C>) {
  12397. lua_CFunction freefunc = &function_detail::upvalue_this_member_function<C, Fx, is_yielding>::call;
  12398. int upvalues = 0;
  12399. upvalues += stack::push(L, nullptr);
  12400. upvalues += stack::stack_detail::push_as_upvalues(L, fx);
  12401. stack::push(L, c_closure(freefunc, upvalues));
  12402. }
  12403. template <bool is_yielding, typename Fx>
  12404. static void select_member_function(std::true_type, lua_State* L, Fx&& fx) {
  12405. typedef typename meta::bind_traits<meta::unqualified_t<Fx>>::object_type C;
  12406. lua_CFunction freefunc = &function_detail::upvalue_this_member_function<C, Fx, is_yielding>::call;
  12407. int upvalues = 0;
  12408. upvalues += stack::push(L, nullptr);
  12409. upvalues += stack::stack_detail::push_as_upvalues(L, fx);
  12410. stack::push(L, c_closure(freefunc, upvalues));
  12411. }
  12412. template <bool is_yielding, typename Fx, typename... Args>
  12413. static void select_function(std::false_type, lua_State* L, Fx&& fx, Args&&... args) {
  12414. select_member_function<is_yielding>(std::is_member_function_pointer<meta::unqualified_t<Fx>>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12415. }
  12416. template <bool is_yielding, typename Fx, typename... Args>
  12417. static void select_function(std::true_type, lua_State* L, Fx&& fx, Args&&... args) {
  12418. std::decay_t<Fx> target(std::forward<Fx>(fx), std::forward<Args>(args)...);
  12419. lua_CFunction freefunc = &function_detail::upvalue_free_function<Fx, is_yielding>::call;
  12420. int upvalues = 0;
  12421. upvalues += stack::push(L, nullptr);
  12422. upvalues += stack::stack_detail::push_as_upvalues(L, target);
  12423. stack::push(L, c_closure(freefunc, upvalues));
  12424. }
  12425. template <bool is_yielding>
  12426. static void select_function(std::true_type, lua_State* L, lua_CFunction f) {
  12427. // TODO: support yielding
  12428. stack::push(L, f);
  12429. }
  12430. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  12431. template <bool is_yielding>
  12432. static void select_function(std::true_type, lua_State* L, detail::lua_CFunction_noexcept f) {
  12433. // TODO: support yielding
  12434. stack::push(L, f);
  12435. }
  12436. #endif // noexcept function type
  12437. template <bool is_yielding, typename Fx, typename... Args, meta::disable<is_lua_reference<meta::unqualified_t<Fx>>> = meta::enabler>
  12438. static void select(lua_State* L, Fx&& fx, Args&&... args) {
  12439. select_function<is_yielding>(std::is_function<std::remove_pointer_t<meta::unqualified_t<Fx>>>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  12440. }
  12441. template <bool is_yielding, typename Fx, meta::enable<is_lua_reference<meta::unqualified_t<Fx>>> = meta::enabler>
  12442. static void select(lua_State* L, Fx&& fx) {
  12443. // TODO: hoist into lambda in this case??
  12444. stack::push(L, std::forward<Fx>(fx));
  12445. }
  12446. template <bool is_yielding, typename Fx, typename... Args>
  12447. static void set_fx(lua_State* L, Args&&... args) {
  12448. lua_CFunction freefunc = detail::static_trampoline<function_detail::call<meta::unqualified_t<Fx>, 2, is_yielding>>;
  12449. int upvalues = 0;
  12450. upvalues += stack::push(L, nullptr);
  12451. upvalues += stack::push<user<Fx>>(L, std::forward<Args>(args)...);
  12452. stack::push(L, c_closure(freefunc, upvalues));
  12453. }
  12454. template <typename Arg0, typename... Args, meta::disable<std::is_same<detail::yield_tag_t, meta::unqualified_t<Arg0>>> = meta::enabler>
  12455. static int push(lua_State* L, Arg0&& arg0, Args&&... args) {
  12456. // Set will always place one thing (function) on the stack
  12457. select<false>(L, std::forward<Arg0>(arg0), std::forward<Args>(args)...);
  12458. return 1;
  12459. }
  12460. template <typename... Args>
  12461. static int push(lua_State* L, detail::yield_tag_t, Args&&... args) {
  12462. // Set will always place one thing (function) on the stack
  12463. select<true>(L, std::forward<Args>(args)...);
  12464. return 1;
  12465. }
  12466. };
  12467. template <typename T>
  12468. struct pusher<yielding_t<T>> {
  12469. template <typename... Args>
  12470. static int push(lua_State* L, const yielding_t<T>& f, Args&&... args) {
  12471. pusher<function_sig<>> p{};
  12472. (void)p;
  12473. return p.push(L, detail::yield_tag, f.func, std::forward<Args>(args)...);
  12474. }
  12475. template <typename... Args>
  12476. static int push(lua_State* L, yielding_t<T>&& f, Args&&... args) {
  12477. pusher<function_sig<>> p{};
  12478. (void)p;
  12479. return p.push(L, detail::yield_tag, f.func, std::forward<Args>(args)...);
  12480. }
  12481. };
  12482. template <typename T, typename... Args>
  12483. struct pusher<function_arguments<T, Args...>> {
  12484. template <std::size_t... I, typename FP>
  12485. static int push_func(std::index_sequence<I...>, lua_State* L, FP&& fp) {
  12486. return stack::push<T>(L, detail::forward_get<I>(fp.arguments)...);
  12487. }
  12488. static int push(lua_State* L, const function_arguments<T, Args...>& fp) {
  12489. return push_func(std::make_index_sequence<sizeof...(Args)>(), L, fp);
  12490. }
  12491. static int push(lua_State* L, function_arguments<T, Args...>&& fp) {
  12492. return push_func(std::make_index_sequence<sizeof...(Args)>(), L, std::move(fp));
  12493. }
  12494. };
  12495. template <typename Signature>
  12496. struct pusher<std::function<Signature>> {
  12497. static int push(lua_State* L, const std::function<Signature>& fx) {
  12498. return pusher<function_sig<Signature>>{}.push(L, fx);
  12499. }
  12500. static int push(lua_State* L, std::function<Signature>&& fx) {
  12501. return pusher<function_sig<Signature>>{}.push(L, std::move(fx));
  12502. }
  12503. };
  12504. template <typename Signature>
  12505. struct pusher<Signature, std::enable_if_t<std::is_member_pointer<Signature>::value>> {
  12506. template <typename F, typename... Args>
  12507. static int push(lua_State* L, F&& f, Args&&... args) {
  12508. pusher<function_sig<>> p{};
  12509. (void)p;
  12510. return p.push(L, std::forward<F>(f), std::forward<Args>(args)...);
  12511. }
  12512. };
  12513. template <typename Signature>
  12514. struct pusher<Signature, std::enable_if_t<meta::all<std::is_function<std::remove_pointer_t<Signature>>, meta::neg<std::is_same<Signature, lua_CFunction>>, meta::neg<std::is_same<Signature, std::remove_pointer_t<lua_CFunction>>>
  12515. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE) && SOL_NOEXCEPT_FUNCTION_TYPE
  12516. ,
  12517. meta::neg<std::is_same<Signature, detail::lua_CFunction_noexcept>>, meta::neg<std::is_same<Signature, std::remove_pointer_t<detail::lua_CFunction_noexcept>>>
  12518. #endif // noexcept function types
  12519. >::value>> {
  12520. template <typename F>
  12521. static int push(lua_State* L, F&& f) {
  12522. return pusher<function_sig<>>{}.push(L, std::forward<F>(f));
  12523. }
  12524. };
  12525. template <typename... Functions>
  12526. struct pusher<overload_set<Functions...>> {
  12527. static int push(lua_State* L, overload_set<Functions...>&& set) {
  12528. // TODO: yielding
  12529. typedef function_detail::overloaded_function<0, Functions...> F;
  12530. pusher<function_sig<>>{}.set_fx<false, F>(L, std::move(set.functions));
  12531. return 1;
  12532. }
  12533. static int push(lua_State* L, const overload_set<Functions...>& set) {
  12534. // TODO: yielding
  12535. typedef function_detail::overloaded_function<0, Functions...> F;
  12536. pusher<function_sig<>>{}.set_fx<false, F>(L, set.functions);
  12537. return 1;
  12538. }
  12539. };
  12540. template <typename T>
  12541. struct pusher<protect_t<T>> {
  12542. static int push(lua_State* L, protect_t<T>&& pw) {
  12543. lua_CFunction cf = call_detail::call_user<void, false, false, protect_t<T>, 2>;
  12544. int upvalues = 0;
  12545. upvalues += stack::push(L, nullptr);
  12546. upvalues += stack::push<user<protect_t<T>>>(L, std::move(pw.value));
  12547. return stack::push(L, c_closure(cf, upvalues));
  12548. }
  12549. static int push(lua_State* L, const protect_t<T>& pw) {
  12550. lua_CFunction cf = call_detail::call_user<void, false, false, protect_t<T>, 2>;
  12551. int upvalues = 0;
  12552. upvalues += stack::push(L, nullptr);
  12553. upvalues += stack::push<user<protect_t<T>>>(L, pw.value);
  12554. return stack::push(L, c_closure(cf, upvalues));
  12555. }
  12556. };
  12557. template <typename F, typename G>
  12558. struct pusher<property_wrapper<F, G>, std::enable_if_t<!std::is_void<F>::value && !std::is_void<G>::value>> {
  12559. static int push(lua_State* L, property_wrapper<F, G>&& pw) {
  12560. return stack::push(L, overload(std::move(pw.read), std::move(pw.write)));
  12561. }
  12562. static int push(lua_State* L, const property_wrapper<F, G>& pw) {
  12563. return stack::push(L, overload(pw.read, pw.write));
  12564. }
  12565. };
  12566. template <typename F>
  12567. struct pusher<property_wrapper<F, void>> {
  12568. static int push(lua_State* L, property_wrapper<F, void>&& pw) {
  12569. return stack::push(L, std::move(pw.read));
  12570. }
  12571. static int push(lua_State* L, const property_wrapper<F, void>& pw) {
  12572. return stack::push(L, pw.read);
  12573. }
  12574. };
  12575. template <typename F>
  12576. struct pusher<property_wrapper<void, F>> {
  12577. static int push(lua_State* L, property_wrapper<void, F>&& pw) {
  12578. return stack::push(L, std::move(pw.write));
  12579. }
  12580. static int push(lua_State* L, const property_wrapper<void, F>& pw) {
  12581. return stack::push(L, pw.write);
  12582. }
  12583. };
  12584. template <typename T>
  12585. struct pusher<var_wrapper<T>> {
  12586. static int push(lua_State* L, var_wrapper<T>&& vw) {
  12587. return stack::push(L, std::move(vw.value));
  12588. }
  12589. static int push(lua_State* L, const var_wrapper<T>& vw) {
  12590. return stack::push(L, vw.value);
  12591. }
  12592. };
  12593. template <typename... Functions>
  12594. struct pusher<factory_wrapper<Functions...>> {
  12595. static int push(lua_State* L, const factory_wrapper<Functions...>& fw) {
  12596. typedef function_detail::overloaded_function<0, Functions...> F;
  12597. pusher<function_sig<>>{}.set_fx<false, F>(L, fw.functions);
  12598. return 1;
  12599. }
  12600. static int push(lua_State* L, factory_wrapper<Functions...>&& fw) {
  12601. typedef function_detail::overloaded_function<0, Functions...> F;
  12602. pusher<function_sig<>>{}.set_fx<false, F>(L, std::move(fw.functions));
  12603. return 1;
  12604. }
  12605. static int push(lua_State* L, const factory_wrapper<Functions...>& set, function_detail::call_indicator) {
  12606. typedef function_detail::overloaded_function<1, Functions...> F;
  12607. pusher<function_sig<>>{}.set_fx<false, F>(L, set.functions);
  12608. return 1;
  12609. }
  12610. static int push(lua_State* L, factory_wrapper<Functions...>&& set, function_detail::call_indicator) {
  12611. typedef function_detail::overloaded_function<1, Functions...> F;
  12612. pusher<function_sig<>>{}.set_fx<false, F>(L, std::move(set.functions));
  12613. return 1;
  12614. }
  12615. };
  12616. template <>
  12617. struct pusher<no_construction> {
  12618. static int push(lua_State* L, no_construction) {
  12619. lua_CFunction cf = &function_detail::no_construction_error;
  12620. return stack::push(L, cf);
  12621. }
  12622. static int push(lua_State* L, no_construction c, function_detail::call_indicator) {
  12623. return push(L, c);
  12624. }
  12625. };
  12626. template <typename T, typename... Lists>
  12627. struct pusher<detail::tagged<T, constructor_list<Lists...>>> {
  12628. static int push(lua_State* L, detail::tagged<T, constructor_list<Lists...>>) {
  12629. lua_CFunction cf = call_detail::construct<T, detail::default_safe_function_calls, true, Lists...>;
  12630. return stack::push(L, cf);
  12631. }
  12632. static int push(lua_State* L, constructor_list<Lists...>) {
  12633. lua_CFunction cf = call_detail::construct<T, detail::default_safe_function_calls, true, Lists...>;
  12634. return stack::push(L, cf);
  12635. }
  12636. };
  12637. template <typename L0, typename... Lists>
  12638. struct pusher<constructor_list<L0, Lists...>> {
  12639. typedef constructor_list<L0, Lists...> cl_t;
  12640. static int push(lua_State* L, cl_t cl) {
  12641. typedef typename meta::bind_traits<L0>::return_type T;
  12642. return stack::push<detail::tagged<T, cl_t>>(L, cl);
  12643. }
  12644. };
  12645. template <typename T, typename... Fxs>
  12646. struct pusher<detail::tagged<T, constructor_wrapper<Fxs...>>> {
  12647. template <typename C>
  12648. static int push(lua_State* L, C&& c) {
  12649. lua_CFunction cf = call_detail::call_user<T, false, false, constructor_wrapper<Fxs...>, 2>;
  12650. int upvalues = 0;
  12651. upvalues += stack::push(L, nullptr);
  12652. upvalues += stack::push<user<constructor_wrapper<Fxs...>>>(L, std::forward<C>(c));
  12653. return stack::push(L, c_closure(cf, upvalues));
  12654. }
  12655. };
  12656. template <typename F, typename... Fxs>
  12657. struct pusher<constructor_wrapper<F, Fxs...>> {
  12658. template <typename C>
  12659. static int push(lua_State* L, C&& c) {
  12660. typedef typename meta::bind_traits<F>::template arg_at<0> arg0;
  12661. typedef meta::unqualified_t<std::remove_pointer_t<arg0>> T;
  12662. return stack::push<detail::tagged<T, constructor_wrapper<F, Fxs...>>>(L, std::forward<C>(c));
  12663. }
  12664. };
  12665. template <typename T>
  12666. struct pusher<detail::tagged<T, destructor_wrapper<void>>> {
  12667. static int push(lua_State* L, destructor_wrapper<void>) {
  12668. lua_CFunction cf = detail::usertype_alloc_destruct<T>;
  12669. return stack::push(L, cf);
  12670. }
  12671. };
  12672. template <typename T, typename Fx>
  12673. struct pusher<detail::tagged<T, destructor_wrapper<Fx>>> {
  12674. static int push(lua_State* L, destructor_wrapper<Fx>&& c) {
  12675. lua_CFunction cf = call_detail::call_user<T, false, false, destructor_wrapper<Fx>, 2>;
  12676. int upvalues = 0;
  12677. upvalues += stack::push(L, nullptr);
  12678. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, std::move(c));
  12679. return stack::push(L, c_closure(cf, upvalues));
  12680. }
  12681. static int push(lua_State* L, const destructor_wrapper<Fx>& c) {
  12682. lua_CFunction cf = call_detail::call_user<T, false, false, destructor_wrapper<Fx>, 2>;
  12683. int upvalues = 0;
  12684. upvalues += stack::push(L, nullptr);
  12685. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, c);
  12686. return stack::push(L, c_closure(cf, upvalues));
  12687. }
  12688. };
  12689. template <typename Fx>
  12690. struct pusher<destructor_wrapper<Fx>> {
  12691. static int push(lua_State* L, destructor_wrapper<Fx>&& c) {
  12692. lua_CFunction cf = call_detail::call_user<void, false, false, destructor_wrapper<Fx>, 2>;
  12693. int upvalues = 0;
  12694. upvalues += stack::push(L, nullptr);
  12695. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, std::move(c));
  12696. return stack::push(L, c_closure(cf, upvalues));
  12697. }
  12698. static int push(lua_State* L, const destructor_wrapper<Fx>& c) {
  12699. lua_CFunction cf = call_detail::call_user<void, false, false, destructor_wrapper<Fx>, 2>;
  12700. int upvalues = 0;
  12701. upvalues += stack::push(L, nullptr);
  12702. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, c);
  12703. return stack::push(L, c_closure(cf, upvalues));
  12704. }
  12705. };
  12706. template <typename F, typename... Filters>
  12707. struct pusher<filter_wrapper<F, Filters...>> {
  12708. typedef filter_wrapper<F, Filters...> P;
  12709. static int push(lua_State* L, const P& p) {
  12710. lua_CFunction cf = call_detail::call_user<void, false, false, P, 2>;
  12711. int upvalues = 0;
  12712. upvalues += stack::push(L, nullptr);
  12713. upvalues += stack::push<user<P>>(L, p);
  12714. return stack::push(L, c_closure(cf, upvalues));
  12715. }
  12716. static int push(lua_State* L, P&& p) {
  12717. lua_CFunction cf = call_detail::call_user<void, false, false, P, 2>;
  12718. int upvalues = 0;
  12719. upvalues += stack::push(L, nullptr);
  12720. upvalues += stack::push<user<P>>(L, std::move(p));
  12721. return stack::push(L, c_closure(cf, upvalues));
  12722. }
  12723. };
  12724. } // namespace stack
  12725. } // namespace sol
  12726. // end of sol/function_types.hpp
  12727. namespace sol {
  12728. template <typename base_t, bool aligned = false>
  12729. class basic_function : public base_t {
  12730. private:
  12731. void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
  12732. lua_call(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount));
  12733. }
  12734. template <std::size_t... I, typename... Ret>
  12735. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n) const {
  12736. luacall(n, lua_size<std::tuple<Ret...>>::value);
  12737. return stack::pop<std::tuple<Ret...>>(lua_state());
  12738. }
  12739. template <std::size_t I, typename Ret>
  12740. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n) const {
  12741. luacall(n, lua_size<Ret>::value);
  12742. return stack::pop<Ret>(lua_state());
  12743. }
  12744. template <std::size_t I>
  12745. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n) const {
  12746. luacall(n, 0);
  12747. }
  12748. unsafe_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) const {
  12749. int stacksize = lua_gettop(lua_state());
  12750. int firstreturn = (std::max)(1, stacksize - static_cast<int>(n));
  12751. luacall(n, LUA_MULTRET);
  12752. int poststacksize = lua_gettop(lua_state());
  12753. int returncount = poststacksize - (firstreturn - 1);
  12754. return unsafe_function_result(lua_state(), firstreturn, returncount);
  12755. }
  12756. public:
  12757. using base_t::lua_state;
  12758. basic_function() = default;
  12759. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_function>>, meta::neg<std::is_same<base_t, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  12760. basic_function(T&& r) noexcept
  12761. : base_t(std::forward<T>(r)) {
  12762. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  12763. if (!is_function<meta::unqualified_t<T>>::value) {
  12764. auto pp = stack::push_pop(*this);
  12765. constructor_handler handler{};
  12766. stack::check<basic_function>(lua_state(), -1, handler);
  12767. }
  12768. #endif // Safety
  12769. }
  12770. basic_function(const basic_function&) = default;
  12771. basic_function& operator=(const basic_function&) = default;
  12772. basic_function(basic_function&&) = default;
  12773. basic_function& operator=(basic_function&&) = default;
  12774. basic_function(const stack_reference& r)
  12775. : basic_function(r.lua_state(), r.stack_index()) {
  12776. }
  12777. basic_function(stack_reference&& r)
  12778. : basic_function(r.lua_state(), r.stack_index()) {
  12779. }
  12780. basic_function(lua_nil_t n)
  12781. : base_t(n) {
  12782. }
  12783. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  12784. basic_function(lua_State* L, T&& r)
  12785. : base_t(L, std::forward<T>(r)) {
  12786. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  12787. auto pp = stack::push_pop(*this);
  12788. constructor_handler handler{};
  12789. stack::check<basic_function>(lua_state(), -1, handler);
  12790. #endif // Safety
  12791. }
  12792. basic_function(lua_State* L, int index = -1)
  12793. : base_t(L, index) {
  12794. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  12795. constructor_handler handler{};
  12796. stack::check<basic_function>(L, index, handler);
  12797. #endif // Safety
  12798. }
  12799. basic_function(lua_State* L, ref_index index)
  12800. : base_t(L, index) {
  12801. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  12802. auto pp = stack::push_pop(*this);
  12803. constructor_handler handler{};
  12804. stack::check<basic_function>(lua_state(), -1, handler);
  12805. #endif // Safety
  12806. }
  12807. template <typename... Args>
  12808. unsafe_function_result operator()(Args&&... args) const {
  12809. return call<>(std::forward<Args>(args)...);
  12810. }
  12811. template <typename... Ret, typename... Args>
  12812. decltype(auto) operator()(types<Ret...>, Args&&... args) const {
  12813. return call<Ret...>(std::forward<Args>(args)...);
  12814. }
  12815. template <typename... Ret, typename... Args>
  12816. decltype(auto) call(Args&&... args) const {
  12817. if (!aligned) {
  12818. base_t::push();
  12819. }
  12820. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  12821. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount);
  12822. }
  12823. };
  12824. } // namespace sol
  12825. // end of sol/unsafe_function.hpp
  12826. // beginning of sol/protected_function.hpp
  12827. // beginning of sol/protected_handler.hpp
  12828. namespace sol {
  12829. namespace detail {
  12830. inline const char(&default_handler_name())[9]{
  12831. static const char name[9] = "sol.\xF0\x9F\x94\xA9";
  12832. return name;
  12833. }
  12834. template <bool b, typename target_t = reference>
  12835. struct protected_handler {
  12836. typedef is_stack_based<target_t> is_stack;
  12837. const target_t& target;
  12838. int stackindex;
  12839. protected_handler(std::false_type, const target_t& target)
  12840. : target(target), stackindex(0) {
  12841. if (b) {
  12842. stackindex = lua_gettop(target.lua_state()) + 1;
  12843. target.push();
  12844. }
  12845. }
  12846. protected_handler(std::true_type, const target_t& target)
  12847. : target(target), stackindex(0) {
  12848. if (b) {
  12849. stackindex = target.stack_index();
  12850. }
  12851. }
  12852. protected_handler(const target_t& target)
  12853. : protected_handler(is_stack(), target) {
  12854. }
  12855. bool valid() const noexcept {
  12856. return b;
  12857. }
  12858. ~protected_handler() {
  12859. if (!is_stack::value && stackindex != 0) {
  12860. lua_remove(target.lua_state(), stackindex);
  12861. }
  12862. }
  12863. };
  12864. template <typename base_t, typename T>
  12865. basic_function<base_t> force_cast(T& p) {
  12866. return p;
  12867. }
  12868. template <typename Reference, bool is_main_ref = false>
  12869. static Reference get_default_handler(lua_State* L) {
  12870. if (is_stack_based<Reference>::value || L == nullptr)
  12871. return Reference(L, lua_nil);
  12872. L = is_main_ref ? main_thread(L, L) : L;
  12873. lua_getglobal(L, default_handler_name());
  12874. auto pp = stack::pop_n(L, 1);
  12875. return Reference(L, -1);
  12876. }
  12877. template <typename T>
  12878. static void set_default_handler(lua_State* L, const T& ref) {
  12879. if (L == nullptr) {
  12880. return;
  12881. }
  12882. if (!ref.valid()) {
  12883. lua_pushnil(L);
  12884. lua_setglobal(L, default_handler_name());
  12885. }
  12886. else {
  12887. ref.push(L);
  12888. lua_setglobal(L, default_handler_name());
  12889. }
  12890. }
  12891. } // namespace detail
  12892. } // namespace sol
  12893. // end of sol/protected_handler.hpp
  12894. namespace sol {
  12895. template <typename base_t, bool aligned = false, typename handler_t = reference>
  12896. class basic_protected_function : public base_t {
  12897. public:
  12898. typedef is_stack_based<handler_t> is_stack_handler;
  12899. static handler_t get_default_handler(lua_State* L) {
  12900. return detail::get_default_handler<handler_t, is_main_threaded<base_t>::value>(L);
  12901. }
  12902. template <typename T>
  12903. static void set_default_handler(const T& ref) {
  12904. detail::set_default_handler(ref.lua_state(), ref);
  12905. }
  12906. private:
  12907. template <bool b>
  12908. call_status luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, detail::protected_handler<b, handler_t>& h) const {
  12909. return static_cast<call_status>(lua_pcall(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stackindex));
  12910. }
  12911. template <std::size_t... I, bool b, typename... Ret>
  12912. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  12913. luacall(n, sizeof...(Ret), h);
  12914. return stack::pop<std::tuple<Ret...>>(lua_state());
  12915. }
  12916. template <std::size_t I, bool b, typename Ret>
  12917. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  12918. luacall(n, 1, h);
  12919. return stack::pop<Ret>(lua_state());
  12920. }
  12921. template <std::size_t I, bool b>
  12922. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  12923. luacall(n, 0, h);
  12924. }
  12925. template <bool b>
  12926. protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  12927. int stacksize = lua_gettop(lua_state());
  12928. int poststacksize = stacksize;
  12929. int firstreturn = 1;
  12930. int returncount = 0;
  12931. call_status code = call_status::ok;
  12932. #if !defined(SOL_NO_EXCEPTIONS) || !SOL_NO_EXCEPTIONS
  12933. auto onexcept = [&](optional<const std::exception&> maybe_ex, const char* error) {
  12934. h.stackindex = 0;
  12935. if (b) {
  12936. h.target.push();
  12937. detail::call_exception_handler(lua_state(), maybe_ex, error);
  12938. lua_call(lua_state(), 1, 1);
  12939. }
  12940. else {
  12941. detail::call_exception_handler(lua_state(), maybe_ex, error);
  12942. }
  12943. };
  12944. (void)onexcept;
  12945. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) || (defined(SOL_LUAJIT) && SOL_LUAJIT)
  12946. try {
  12947. #endif // Safe Exception Propagation
  12948. #endif // No Exceptions
  12949. firstreturn = (std::max)(1, static_cast<int>(stacksize - n - static_cast<int>(h.valid())));
  12950. code = luacall(n, LUA_MULTRET, h);
  12951. poststacksize = lua_gettop(lua_state()) - static_cast<int>(h.valid());
  12952. returncount = poststacksize - (firstreturn - 1);
  12953. #ifndef SOL_NO_EXCEPTIONS
  12954. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) || (defined(SOL_LUAJIT) && SOL_LUAJIT)
  12955. }
  12956. // Handle C++ errors thrown from C++ functions bound inside of lua
  12957. catch (const char* error) {
  12958. onexcept(optional<const std::exception&>(nullopt), error);
  12959. firstreturn = lua_gettop(lua_state());
  12960. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  12961. }
  12962. catch (const std::string& error) {
  12963. onexcept(optional<const std::exception&>(nullopt), error.c_str());
  12964. firstreturn = lua_gettop(lua_state());
  12965. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  12966. }
  12967. catch (const std::exception& error) {
  12968. onexcept(optional<const std::exception&>(error), error.what());
  12969. firstreturn = lua_gettop(lua_state());
  12970. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  12971. }
  12972. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION)
  12973. // LuaJIT cannot have the catchall when the safe propagation is on
  12974. // but LuaJIT will swallow all C++ errors
  12975. // if we don't at least catch std::exception ones
  12976. catch (...) {
  12977. onexcept(optional<const std::exception&>(nullopt), "caught (...) unknown error during protected_function call");
  12978. firstreturn = lua_gettop(lua_state());
  12979. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  12980. }
  12981. #endif // LuaJIT
  12982. #else
  12983. // do not handle exceptions: they can be propogated into C++ and keep all type information / rich information
  12984. #endif // Safe Exception Propagation
  12985. #endif // Exceptions vs. No Exceptions
  12986. return protected_function_result(lua_state(), firstreturn, returncount, returncount, code);
  12987. }
  12988. public:
  12989. using base_t::lua_state;
  12990. handler_t error_handler;
  12991. basic_protected_function() = default;
  12992. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_protected_function>>, meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<T>>>, meta::neg<std::is_same<base_t, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  12993. basic_protected_function(T&& r) noexcept
  12994. : base_t(std::forward<T>(r)), error_handler(get_default_handler(r.lua_state())) {
  12995. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  12996. if (!is_function<meta::unqualified_t<T>>::value) {
  12997. auto pp = stack::push_pop(*this);
  12998. constructor_handler handler{};
  12999. stack::check<basic_protected_function>(lua_state(), -1, handler);
  13000. }
  13001. #endif // Safety
  13002. }
  13003. basic_protected_function(const basic_protected_function&) = default;
  13004. basic_protected_function& operator=(const basic_protected_function&) = default;
  13005. basic_protected_function(basic_protected_function&&) = default;
  13006. basic_protected_function& operator=(basic_protected_function&&) = default;
  13007. basic_protected_function(const basic_function<base_t>& b)
  13008. : basic_protected_function(b, get_default_handler(b.lua_state())) {
  13009. }
  13010. basic_protected_function(basic_function<base_t>&& b)
  13011. : basic_protected_function(std::move(b), get_default_handler(b.lua_state())) {
  13012. }
  13013. basic_protected_function(const basic_function<base_t>& b, handler_t eh)
  13014. : base_t(b), error_handler(std::move(eh)) {
  13015. }
  13016. basic_protected_function(basic_function<base_t>&& b, handler_t eh)
  13017. : base_t(std::move(b)), error_handler(std::move(eh)) {
  13018. }
  13019. basic_protected_function(const stack_reference& r)
  13020. : basic_protected_function(r.lua_state(), r.stack_index(), get_default_handler(r.lua_state())) {
  13021. }
  13022. basic_protected_function(stack_reference&& r)
  13023. : basic_protected_function(r.lua_state(), r.stack_index(), get_default_handler(r.lua_state())) {
  13024. }
  13025. basic_protected_function(const stack_reference& r, handler_t eh)
  13026. : basic_protected_function(r.lua_state(), r.stack_index(), std::move(eh)) {
  13027. }
  13028. basic_protected_function(stack_reference&& r, handler_t eh)
  13029. : basic_protected_function(r.lua_state(), r.stack_index(), std::move(eh)) {
  13030. }
  13031. template <typename Super>
  13032. basic_protected_function(const proxy_base<Super>& p)
  13033. : basic_protected_function(p, get_default_handler(p.lua_state())) {
  13034. }
  13035. template <typename Super>
  13036. basic_protected_function(proxy_base<Super>&& p)
  13037. : basic_protected_function(std::move(p), get_default_handler(p.lua_state())) {
  13038. }
  13039. template <typename Proxy, typename Handler, meta::enable<std::is_base_of<proxy_base_tag, meta::unqualified_t<Proxy>>, meta::neg<is_lua_index<meta::unqualified_t<Handler>>>> = meta::enabler>
  13040. basic_protected_function(Proxy&& p, Handler&& eh)
  13041. : basic_protected_function(detail::force_cast<base_t>(p), std::forward<Handler>(eh)) {
  13042. }
  13043. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13044. basic_protected_function(lua_State* L, T&& r)
  13045. : basic_protected_function(L, std::forward<T>(r), get_default_handler(L)) {
  13046. }
  13047. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13048. basic_protected_function(lua_State* L, T&& r, handler_t eh)
  13049. : base_t(L, std::forward<T>(r)), error_handler(std::move(eh)) {
  13050. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13051. auto pp = stack::push_pop(*this);
  13052. constructor_handler handler{};
  13053. stack::check<basic_protected_function>(lua_state(), -1, handler);
  13054. #endif // Safety
  13055. }
  13056. basic_protected_function(lua_nil_t n)
  13057. : base_t(n), error_handler(n) {
  13058. }
  13059. basic_protected_function(lua_State* L, int index = -1)
  13060. : basic_protected_function(L, index, get_default_handler(L)) {
  13061. }
  13062. basic_protected_function(lua_State* L, int index, handler_t eh)
  13063. : base_t(L, index), error_handler(std::move(eh)) {
  13064. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13065. constructor_handler handler{};
  13066. stack::check<basic_protected_function>(L, index, handler);
  13067. #endif // Safety
  13068. }
  13069. basic_protected_function(lua_State* L, absolute_index index)
  13070. : basic_protected_function(L, index, get_default_handler(L)) {
  13071. }
  13072. basic_protected_function(lua_State* L, absolute_index index, handler_t eh)
  13073. : base_t(L, index), error_handler(std::move(eh)) {
  13074. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13075. constructor_handler handler{};
  13076. stack::check<basic_protected_function>(L, index, handler);
  13077. #endif // Safety
  13078. }
  13079. basic_protected_function(lua_State* L, raw_index index)
  13080. : basic_protected_function(L, index, get_default_handler(L)) {
  13081. }
  13082. basic_protected_function(lua_State* L, raw_index index, handler_t eh)
  13083. : base_t(L, index), error_handler(std::move(eh)) {
  13084. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13085. constructor_handler handler{};
  13086. stack::check<basic_protected_function>(L, index, handler);
  13087. #endif // Safety
  13088. }
  13089. basic_protected_function(lua_State* L, ref_index index)
  13090. : basic_protected_function(L, index, get_default_handler(L)) {
  13091. }
  13092. basic_protected_function(lua_State* L, ref_index index, handler_t eh)
  13093. : base_t(L, index), error_handler(std::move(eh)) {
  13094. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13095. auto pp = stack::push_pop(*this);
  13096. constructor_handler handler{};
  13097. stack::check<basic_protected_function>(lua_state(), -1, handler);
  13098. #endif // Safety
  13099. }
  13100. template <typename... Args>
  13101. protected_function_result operator()(Args&&... args) const {
  13102. return call<>(std::forward<Args>(args)...);
  13103. }
  13104. template <typename... Ret, typename... Args>
  13105. decltype(auto) operator()(types<Ret...>, Args&&... args) const {
  13106. return call<Ret...>(std::forward<Args>(args)...);
  13107. }
  13108. template <typename... Ret, typename... Args>
  13109. decltype(auto) call(Args&&... args) const {
  13110. if (!aligned) {
  13111. // we do not expect the function to already be on the stack: push it
  13112. if (error_handler.valid()) {
  13113. detail::protected_handler<true, handler_t> h(error_handler);
  13114. base_t::push();
  13115. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  13116. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  13117. }
  13118. else {
  13119. detail::protected_handler<false, handler_t> h(error_handler);
  13120. base_t::push();
  13121. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  13122. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  13123. }
  13124. }
  13125. else {
  13126. // the function is already on the stack at the right location
  13127. if (error_handler.valid()) {
  13128. // the handler will be pushed onto the stack manually,
  13129. // since it's not already on the stack this means we need to push our own
  13130. // function on the stack too and swap things to be in-place
  13131. if (!is_stack_handler::value) {
  13132. // so, we need to remove the function at the top and then dump the handler out ourselves
  13133. base_t::push();
  13134. }
  13135. detail::protected_handler<true, handler_t> h(error_handler);
  13136. if (!is_stack_handler::value) {
  13137. lua_replace(lua_state(), -3);
  13138. h.stackindex = lua_absindex(lua_state(), -2);
  13139. }
  13140. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  13141. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  13142. }
  13143. else {
  13144. detail::protected_handler<false, handler_t> h(error_handler);
  13145. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  13146. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  13147. }
  13148. }
  13149. }
  13150. };
  13151. } // namespace sol
  13152. // end of sol/protected_function.hpp
  13153. namespace sol {
  13154. template <typename... Ret, typename... Args>
  13155. inline decltype(auto) stack_proxy::call(Args&&... args) {
  13156. stack_function sf(this->lua_state(), this->stack_index());
  13157. return sf.template call<Ret...>(std::forward<Args>(args)...);
  13158. }
  13159. inline protected_function_result::protected_function_result(unsafe_function_result&& o) noexcept
  13160. : L(o.lua_state()), index(o.stack_index()), returncount(o.return_count()), popcount(o.return_count()), err(o.status()) {
  13161. // Must be manual, otherwise destructor will screw us
  13162. // return count being 0 is enough to keep things clean
  13163. // but we will be thorough
  13164. o.abandon();
  13165. }
  13166. inline protected_function_result& protected_function_result::operator=(unsafe_function_result&& o) noexcept {
  13167. L = o.lua_state();
  13168. index = o.stack_index();
  13169. returncount = o.return_count();
  13170. popcount = o.return_count();
  13171. err = o.status();
  13172. // Must be manual, otherwise destructor will screw us
  13173. // return count being 0 is enough to keep things clean
  13174. // but we will be thorough
  13175. o.abandon();
  13176. return *this;
  13177. }
  13178. inline unsafe_function_result::unsafe_function_result(protected_function_result&& o) noexcept
  13179. : L(o.lua_state()), index(o.stack_index()), returncount(o.return_count()) {
  13180. // Must be manual, otherwise destructor will screw us
  13181. // return count being 0 is enough to keep things clean
  13182. // but we will be thorough
  13183. o.abandon();
  13184. }
  13185. inline unsafe_function_result& unsafe_function_result::operator=(protected_function_result&& o) noexcept {
  13186. L = o.lua_state();
  13187. index = o.stack_index();
  13188. returncount = o.return_count();
  13189. // Must be manual, otherwise destructor will screw us
  13190. // return count being 0 is enough to keep things clean
  13191. // but we will be thorough
  13192. o.abandon();
  13193. return *this;
  13194. }
  13195. namespace stack {
  13196. template <typename Signature>
  13197. struct getter<std::function<Signature>> {
  13198. typedef meta::bind_traits<Signature> fx_t;
  13199. typedef typename fx_t::args_list args_lists;
  13200. typedef meta::tuple_types<typename fx_t::return_type> return_types;
  13201. template <typename... Args, typename... Ret>
  13202. static std::function<Signature> get_std_func(types<Ret...>, types<Args...>, lua_State* L, int index) {
  13203. unsafe_function f(L, index);
  13204. auto fx = [ f = std::move(f) ](Args && ... args) -> meta::return_type_t<Ret...> {
  13205. return f.call<Ret...>(std::forward<Args>(args)...);
  13206. };
  13207. return std::move(fx);
  13208. }
  13209. template <typename... FxArgs>
  13210. static std::function<Signature> get_std_func(types<void>, types<FxArgs...>, lua_State* L, int index) {
  13211. unsafe_function f(L, index);
  13212. auto fx = [f = std::move(f)](FxArgs&&... args) -> void {
  13213. f(std::forward<FxArgs>(args)...);
  13214. };
  13215. return std::move(fx);
  13216. }
  13217. template <typename... FxArgs>
  13218. static std::function<Signature> get_std_func(types<>, types<FxArgs...> t, lua_State* L, int index) {
  13219. return get_std_func(types<void>(), t, L, index);
  13220. }
  13221. static std::function<Signature> get(lua_State* L, int index, record& tracking) {
  13222. tracking.last = 1;
  13223. tracking.used += 1;
  13224. type t = type_of(L, index);
  13225. if (t == type::none || t == type::lua_nil) {
  13226. return nullptr;
  13227. }
  13228. return get_std_func(return_types(), args_lists(), L, index);
  13229. }
  13230. };
  13231. } // namespace stack
  13232. } // namespace sol
  13233. // end of sol/function.hpp
  13234. namespace sol {
  13235. template <typename Table, typename Key>
  13236. struct proxy : public proxy_base<proxy<Table, Key>> {
  13237. private:
  13238. typedef meta::condition<meta::is_specialization_of<Key, std::tuple>, Key, std::tuple<meta::condition<std::is_array<meta::unqualified_t<Key>>, Key&, meta::unqualified_t<Key>>>> key_type;
  13239. template <typename T, std::size_t... I>
  13240. decltype(auto) tuple_get(std::index_sequence<I...>) const {
  13241. return tbl.template traverse_get<T>(std::get<I>(key)...);
  13242. }
  13243. template <std::size_t... I, typename T>
  13244. void tuple_set(std::index_sequence<I...>, T&& value) {
  13245. tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
  13246. }
  13247. auto setup_table(std::true_type) {
  13248. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, tbl.stack_index());
  13249. lua_pop(lua_state(), p.levels);
  13250. return p;
  13251. }
  13252. bool is_valid(std::false_type) {
  13253. auto pp = stack::push_pop(tbl);
  13254. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  13255. lua_pop(lua_state(), p.levels);
  13256. return p;
  13257. }
  13258. public:
  13259. Table tbl;
  13260. key_type key;
  13261. template <typename T>
  13262. proxy(Table table, T&& k)
  13263. : tbl(table), key(std::forward<T>(k)) {
  13264. }
  13265. template <typename T>
  13266. proxy& set(T&& item) {
  13267. tuple_set(std::make_index_sequence<std::tuple_size<meta::unqualified_t<key_type>>::value>(), std::forward<T>(item));
  13268. return *this;
  13269. }
  13270. template <typename... Args>
  13271. proxy& set_function(Args&&... args) {
  13272. tbl.set_function(key, std::forward<Args>(args)...);
  13273. return *this;
  13274. }
  13275. template <typename U, meta::enable<meta::neg<is_lua_reference_or_proxy<meta::unwrap_unqualified_t<U>>>, meta::is_callable<meta::unwrap_unqualified_t<U>>> = meta::enabler>
  13276. proxy& operator=(U&& other) {
  13277. return set_function(std::forward<U>(other));
  13278. }
  13279. template <typename U, meta::disable<meta::neg<is_lua_reference_or_proxy<meta::unwrap_unqualified_t<U>>>, meta::is_callable<meta::unwrap_unqualified_t<U>>> = meta::enabler>
  13280. proxy& operator=(U&& other) {
  13281. return set(std::forward<U>(other));
  13282. }
  13283. template <typename T>
  13284. proxy& operator=(std::initializer_list<T> other) {
  13285. return set(std::move(other));
  13286. }
  13287. template <typename T>
  13288. decltype(auto) get() const {
  13289. return tuple_get<T>(std::make_index_sequence<std::tuple_size<meta::unqualified_t<key_type>>::value>());
  13290. }
  13291. template <typename T>
  13292. decltype(auto) get_or(T&& otherwise) const {
  13293. typedef decltype(get<T>()) U;
  13294. optional<U> option = get<optional<U>>();
  13295. if (option) {
  13296. return static_cast<U>(option.value());
  13297. }
  13298. return static_cast<U>(std::forward<T>(otherwise));
  13299. }
  13300. template <typename T, typename D>
  13301. decltype(auto) get_or(D&& otherwise) const {
  13302. optional<T> option = get<optional<T>>();
  13303. if (option) {
  13304. return static_cast<T>(option.value());
  13305. }
  13306. return static_cast<T>(std::forward<D>(otherwise));
  13307. }
  13308. template <typename T>
  13309. decltype(auto) get_or_create() {
  13310. return get_or_create<T>(new_table());
  13311. }
  13312. template <typename T, typename Otherwise>
  13313. decltype(auto) get_or_create(Otherwise&& other) {
  13314. if (!this->valid()) {
  13315. this->set(std::forward<Otherwise>(other));
  13316. }
  13317. return get<T>();
  13318. }
  13319. template <typename K>
  13320. decltype(auto) operator[](K&& k) const {
  13321. auto keys = meta::tuplefy(key, std::forward<K>(k));
  13322. return proxy<Table, decltype(keys)>(tbl, std::move(keys));
  13323. }
  13324. template <typename... Ret, typename... Args>
  13325. decltype(auto) call(Args&&... args) {
  13326. #if !defined(__clang__) && defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 191200000
  13327. // MSVC is ass sometimes
  13328. return get<function>().call<Ret...>(std::forward<Args>(args)...);
  13329. #else
  13330. return get<function>().template call<Ret...>(std::forward<Args>(args)...);
  13331. #endif
  13332. }
  13333. template <typename... Args>
  13334. decltype(auto) operator()(Args&&... args) {
  13335. return call<>(std::forward<Args>(args)...);
  13336. }
  13337. bool valid() const {
  13338. auto pp = stack::push_pop(tbl);
  13339. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  13340. lua_pop(lua_state(), p.levels);
  13341. return p;
  13342. }
  13343. int push() const noexcept {
  13344. return push(this->lua_state());
  13345. }
  13346. int push(lua_State* L) const noexcept {
  13347. return get<reference>().push(L);
  13348. }
  13349. type get_type() const {
  13350. type t = type::none;
  13351. auto pp = stack::push_pop(tbl);
  13352. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  13353. if (p) {
  13354. t = type_of(lua_state(), -1);
  13355. }
  13356. lua_pop(lua_state(), p.levels);
  13357. return t;
  13358. }
  13359. lua_State* lua_state() const {
  13360. return tbl.lua_state();
  13361. }
  13362. proxy& force() {
  13363. if (this->valid()) {
  13364. this->set(new_table());
  13365. }
  13366. return *this;
  13367. }
  13368. };
  13369. template <typename Table, typename Key, typename T>
  13370. inline bool operator==(T&& left, const proxy<Table, Key>& right) {
  13371. typedef decltype(stack::get<T>(nullptr, 0)) U;
  13372. return right.template get<optional<U>>() == left;
  13373. }
  13374. template <typename Table, typename Key, typename T>
  13375. inline bool operator==(const proxy<Table, Key>& right, T&& left) {
  13376. typedef decltype(stack::get<T>(nullptr, 0)) U;
  13377. return right.template get<optional<U>>() == left;
  13378. }
  13379. template <typename Table, typename Key, typename T>
  13380. inline bool operator!=(T&& left, const proxy<Table, Key>& right) {
  13381. typedef decltype(stack::get<T>(nullptr, 0)) U;
  13382. return right.template get<optional<U>>() != left;
  13383. }
  13384. template <typename Table, typename Key, typename T>
  13385. inline bool operator!=(const proxy<Table, Key>& right, T&& left) {
  13386. typedef decltype(stack::get<T>(nullptr, 0)) U;
  13387. return right.template get<optional<U>>() != left;
  13388. }
  13389. template <typename Table, typename Key>
  13390. inline bool operator==(lua_nil_t, const proxy<Table, Key>& right) {
  13391. return !right.valid();
  13392. }
  13393. template <typename Table, typename Key>
  13394. inline bool operator==(const proxy<Table, Key>& right, lua_nil_t) {
  13395. return !right.valid();
  13396. }
  13397. template <typename Table, typename Key>
  13398. inline bool operator!=(lua_nil_t, const proxy<Table, Key>& right) {
  13399. return right.valid();
  13400. }
  13401. template <typename Table, typename Key>
  13402. inline bool operator!=(const proxy<Table, Key>& right, lua_nil_t) {
  13403. return right.valid();
  13404. }
  13405. template <bool b>
  13406. template <typename Super>
  13407. basic_reference<b>& basic_reference<b>::operator=(proxy_base<Super>&& r) {
  13408. basic_reference<b> v = r;
  13409. this->operator=(std::move(v));
  13410. return *this;
  13411. }
  13412. template <bool b>
  13413. template <typename Super>
  13414. basic_reference<b>& basic_reference<b>::operator=(const proxy_base<Super>& r) {
  13415. basic_reference<b> v = r;
  13416. this->operator=(std::move(v));
  13417. return *this;
  13418. }
  13419. namespace stack {
  13420. template <typename Table, typename Key>
  13421. struct pusher<proxy<Table, Key>> {
  13422. static int push(lua_State* L, const proxy<Table, Key>& p) {
  13423. reference r = p;
  13424. return r.push(L);
  13425. }
  13426. };
  13427. } // namespace stack
  13428. } // namespace sol
  13429. // end of sol/proxy.hpp
  13430. // beginning of sol/usertype.hpp
  13431. // beginning of sol/usertype_metatable.hpp
  13432. // beginning of sol/deprecate.hpp
  13433. #ifndef SOL_DEPRECATED
  13434. #ifdef _MSC_VER
  13435. #define SOL_DEPRECATED __declspec(deprecated)
  13436. #elif __GNUC__
  13437. #define SOL_DEPRECATED __attribute__((deprecated))
  13438. #else
  13439. #define SOL_DEPRECATED [[deprecated]]
  13440. #endif // compilers
  13441. #endif // SOL_DEPRECATED
  13442. namespace sol {
  13443. namespace detail {
  13444. template <typename T>
  13445. struct SOL_DEPRECATED deprecate_type {
  13446. using type = T;
  13447. };
  13448. }
  13449. } // namespace sol::detail
  13450. // end of sol/deprecate.hpp
  13451. // beginning of sol/object.hpp
  13452. // beginning of sol/object_base.hpp
  13453. namespace sol {
  13454. template <typename base_t>
  13455. class basic_object_base : public base_t {
  13456. private:
  13457. template <typename T>
  13458. decltype(auto) as_stack(std::true_type) const {
  13459. return stack::get<T>(base_t::lua_state(), base_t::stack_index());
  13460. }
  13461. template <typename T>
  13462. decltype(auto) as_stack(std::false_type) const {
  13463. base_t::push();
  13464. return stack::pop<T>(base_t::lua_state());
  13465. }
  13466. template <typename T>
  13467. bool is_stack(std::true_type) const {
  13468. return stack::check<T>(base_t::lua_state(), base_t::stack_index(), no_panic);
  13469. }
  13470. template <typename T>
  13471. bool is_stack(std::false_type) const {
  13472. int r = base_t::registry_index();
  13473. if (r == LUA_REFNIL)
  13474. return meta::any_same<meta::unqualified_t<T>, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false;
  13475. if (r == LUA_NOREF)
  13476. return false;
  13477. auto pp = stack::push_pop(*this);
  13478. return stack::check<T>(base_t::lua_state(), -1, no_panic);
  13479. }
  13480. public:
  13481. basic_object_base() noexcept = default;
  13482. basic_object_base(const basic_object_base&) = default;
  13483. basic_object_base(basic_object_base&&) = default;
  13484. basic_object_base& operator=(const basic_object_base&) = default;
  13485. basic_object_base& operator=(basic_object_base&&) = default;
  13486. template <typename T, typename... Args, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_object_base>>> = meta::enabler>
  13487. basic_object_base(T&& arg, Args&&... args)
  13488. : base_t(std::forward<T>(arg), std::forward<Args>(args)...) {
  13489. }
  13490. template <typename T>
  13491. decltype(auto) as() const {
  13492. return as_stack<T>(is_stack_based<base_t>());
  13493. }
  13494. template <typename T>
  13495. bool is() const {
  13496. return is_stack<T>(is_stack_based<base_t>());
  13497. }
  13498. };
  13499. } // namespace sol
  13500. // end of sol/object_base.hpp
  13501. // beginning of sol/userdata.hpp
  13502. namespace sol {
  13503. template <typename base_type>
  13504. class basic_userdata : public basic_table<base_type> {
  13505. typedef basic_table<base_type> base_t;
  13506. public:
  13507. using base_t::lua_state;
  13508. basic_userdata() noexcept = default;
  13509. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_userdata>>, meta::neg<std::is_same<base_t, stack_reference>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13510. basic_userdata(T&& r) noexcept
  13511. : base_t(std::forward<T>(r)) {
  13512. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13513. if (!is_userdata<meta::unqualified_t<T>>::value) {
  13514. auto pp = stack::push_pop(*this);
  13515. type_assert(lua_state(), -1, type::userdata);
  13516. }
  13517. #endif // Safety
  13518. }
  13519. basic_userdata(const basic_userdata&) = default;
  13520. basic_userdata(basic_userdata&&) = default;
  13521. basic_userdata& operator=(const basic_userdata&) = default;
  13522. basic_userdata& operator=(basic_userdata&&) = default;
  13523. basic_userdata(const stack_reference& r)
  13524. : basic_userdata(r.lua_state(), r.stack_index()) {
  13525. }
  13526. basic_userdata(stack_reference&& r)
  13527. : basic_userdata(r.lua_state(), r.stack_index()) {
  13528. }
  13529. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13530. basic_userdata(lua_State* L, T&& r)
  13531. : base_t(L, std::forward<T>(r)) {
  13532. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13533. auto pp = stack::push_pop(*this);
  13534. constructor_handler handler{};
  13535. stack::check<basic_userdata>(L, -1, handler);
  13536. #endif // Safety
  13537. }
  13538. basic_userdata(lua_State* L, int index = -1)
  13539. : base_t(detail::no_safety, L, index) {
  13540. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13541. constructor_handler handler{};
  13542. stack::check<basic_userdata>(L, index, handler);
  13543. #endif // Safety
  13544. }
  13545. basic_userdata(lua_State* L, ref_index index)
  13546. : base_t(detail::no_safety, L, index) {
  13547. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13548. auto pp = stack::push_pop(*this);
  13549. constructor_handler handler{};
  13550. stack::check<basic_userdata>(L, -1, handler);
  13551. #endif // Safety
  13552. }
  13553. };
  13554. template <typename base_type>
  13555. class basic_lightuserdata : public basic_object_base<base_type> {
  13556. typedef basic_object_base<base_type> base_t;
  13557. public:
  13558. using base_t::lua_state;
  13559. basic_lightuserdata() noexcept = default;
  13560. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_lightuserdata>>, meta::neg<std::is_same<base_t, stack_reference>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13561. basic_lightuserdata(T&& r) noexcept
  13562. : base_t(std::forward<T>(r)) {
  13563. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13564. if (!is_lightuserdata<meta::unqualified_t<T>>::value) {
  13565. auto pp = stack::push_pop(*this);
  13566. type_assert(lua_state(), -1, type::lightuserdata);
  13567. }
  13568. #endif // Safety
  13569. }
  13570. basic_lightuserdata(const basic_lightuserdata&) = default;
  13571. basic_lightuserdata(basic_lightuserdata&&) = default;
  13572. basic_lightuserdata& operator=(const basic_lightuserdata&) = default;
  13573. basic_lightuserdata& operator=(basic_lightuserdata&&) = default;
  13574. basic_lightuserdata(const stack_reference& r)
  13575. : basic_lightuserdata(r.lua_state(), r.stack_index()) {
  13576. }
  13577. basic_lightuserdata(stack_reference&& r)
  13578. : basic_lightuserdata(r.lua_state(), r.stack_index()) {
  13579. }
  13580. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13581. basic_lightuserdata(lua_State* L, T&& r)
  13582. : basic_lightuserdata(L, std::forward<T>(r)) {
  13583. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13584. auto pp = stack::push_pop(*this);
  13585. constructor_handler handler{};
  13586. stack::check<basic_lightuserdata>(lua_state(), -1, handler);
  13587. #endif // Safety
  13588. }
  13589. basic_lightuserdata(lua_State* L, int index = -1)
  13590. : base_t(L, index) {
  13591. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13592. constructor_handler handler{};
  13593. stack::check<basic_lightuserdata>(L, index, handler);
  13594. #endif // Safety
  13595. }
  13596. basic_lightuserdata(lua_State* L, ref_index index)
  13597. : base_t(L, index) {
  13598. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  13599. auto pp = stack::push_pop(*this);
  13600. constructor_handler handler{};
  13601. stack::check<basic_lightuserdata>(lua_state(), index, handler);
  13602. #endif // Safety
  13603. }
  13604. };
  13605. } // namespace sol
  13606. // end of sol/userdata.hpp
  13607. // beginning of sol/as_args.hpp
  13608. namespace sol {
  13609. template <typename T>
  13610. struct as_args_t {
  13611. T src;
  13612. };
  13613. template <typename Source>
  13614. auto as_args(Source&& source) {
  13615. return as_args_t<Source>{ std::forward<Source>(source) };
  13616. }
  13617. namespace stack {
  13618. template <typename T>
  13619. struct pusher<as_args_t<T>> {
  13620. int push(lua_State* L, const as_args_t<T>& e) {
  13621. int p = 0;
  13622. for (const auto& i : e.src) {
  13623. p += stack::push(L, i);
  13624. }
  13625. return p;
  13626. }
  13627. };
  13628. } // namespace stack
  13629. } // namespace sol
  13630. // end of sol/as_args.hpp
  13631. // beginning of sol/variadic_args.hpp
  13632. namespace sol {
  13633. struct variadic_args {
  13634. private:
  13635. lua_State* L;
  13636. int index;
  13637. int stacktop;
  13638. public:
  13639. typedef stack_proxy reference_type;
  13640. typedef stack_proxy value_type;
  13641. typedef stack_proxy* pointer;
  13642. typedef std::ptrdiff_t difference_type;
  13643. typedef std::size_t size_type;
  13644. typedef stack_iterator<stack_proxy, false> iterator;
  13645. typedef stack_iterator<stack_proxy, true> const_iterator;
  13646. typedef std::reverse_iterator<iterator> reverse_iterator;
  13647. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  13648. variadic_args() = default;
  13649. variadic_args(lua_State* luastate, int stackindex = -1)
  13650. : L(luastate), index(lua_absindex(luastate, stackindex)), stacktop(lua_gettop(luastate)) {
  13651. }
  13652. variadic_args(lua_State* luastate, int stackindex, int lastindex)
  13653. : L(luastate), index(lua_absindex(luastate, stackindex)), stacktop(lastindex) {
  13654. }
  13655. variadic_args(const variadic_args&) = default;
  13656. variadic_args& operator=(const variadic_args&) = default;
  13657. variadic_args(variadic_args&& o)
  13658. : L(o.L), index(o.index), stacktop(o.stacktop) {
  13659. // Must be manual, otherwise destructor will screw us
  13660. // return count being 0 is enough to keep things clean
  13661. // but will be thorough
  13662. o.L = nullptr;
  13663. o.index = 0;
  13664. o.stacktop = 0;
  13665. }
  13666. variadic_args& operator=(variadic_args&& o) {
  13667. L = o.L;
  13668. index = o.index;
  13669. stacktop = o.stacktop;
  13670. // Must be manual, otherwise destructor will screw us
  13671. // return count being 0 is enough to keep things clean
  13672. // but will be thorough
  13673. o.L = nullptr;
  13674. o.index = 0;
  13675. o.stacktop = 0;
  13676. return *this;
  13677. }
  13678. iterator begin() {
  13679. return iterator(L, index, stacktop + 1);
  13680. }
  13681. iterator end() {
  13682. return iterator(L, stacktop + 1, stacktop + 1);
  13683. }
  13684. const_iterator begin() const {
  13685. return const_iterator(L, index, stacktop + 1);
  13686. }
  13687. const_iterator end() const {
  13688. return const_iterator(L, stacktop + 1, stacktop + 1);
  13689. }
  13690. const_iterator cbegin() const {
  13691. return begin();
  13692. }
  13693. const_iterator cend() const {
  13694. return end();
  13695. }
  13696. reverse_iterator rbegin() {
  13697. return std::reverse_iterator<iterator>(begin());
  13698. }
  13699. reverse_iterator rend() {
  13700. return std::reverse_iterator<iterator>(end());
  13701. }
  13702. const_reverse_iterator rbegin() const {
  13703. return std::reverse_iterator<const_iterator>(begin());
  13704. }
  13705. const_reverse_iterator rend() const {
  13706. return std::reverse_iterator<const_iterator>(end());
  13707. }
  13708. const_reverse_iterator crbegin() const {
  13709. return std::reverse_iterator<const_iterator>(cbegin());
  13710. }
  13711. const_reverse_iterator crend() const {
  13712. return std::reverse_iterator<const_iterator>(cend());
  13713. }
  13714. int push() const {
  13715. return push(L);
  13716. }
  13717. int push(lua_State* target) const {
  13718. int pushcount = 0;
  13719. for (int i = index; i <= stacktop; ++i) {
  13720. lua_pushvalue(L, i);
  13721. pushcount += 1;
  13722. }
  13723. if (target != L) {
  13724. lua_xmove(L, target, pushcount);
  13725. }
  13726. return pushcount;
  13727. }
  13728. template <typename T>
  13729. decltype(auto) get(difference_type index_offset = 0) const {
  13730. return stack::get<T>(L, index + static_cast<int>(index_offset));
  13731. }
  13732. type get_type(difference_type index_offset = 0) const noexcept {
  13733. return type_of(L, index + static_cast<int>(index_offset));
  13734. }
  13735. stack_proxy operator[](difference_type index_offset) const {
  13736. return stack_proxy(L, index + static_cast<int>(index_offset));
  13737. }
  13738. lua_State* lua_state() const {
  13739. return L;
  13740. };
  13741. int stack_index() const {
  13742. return index;
  13743. };
  13744. int leftover_count() const {
  13745. return stacktop - (index - 1);
  13746. }
  13747. std::size_t size() const {
  13748. return static_cast<std::size_t>(leftover_count());
  13749. }
  13750. int top() const {
  13751. return stacktop;
  13752. }
  13753. };
  13754. namespace stack {
  13755. template <>
  13756. struct getter<variadic_args> {
  13757. static variadic_args get(lua_State* L, int index, record& tracking) {
  13758. tracking.last = 0;
  13759. return variadic_args(L, index);
  13760. }
  13761. };
  13762. template <>
  13763. struct pusher<variadic_args> {
  13764. static int push(lua_State* L, const variadic_args& ref) {
  13765. return ref.push(L);
  13766. }
  13767. };
  13768. } // namespace stack
  13769. } // namespace sol
  13770. // end of sol/variadic_args.hpp
  13771. namespace sol {
  13772. template <typename R = reference, bool should_pop = !is_stack_based<R>::value, typename T>
  13773. R make_reference(lua_State* L, T&& value) {
  13774. int backpedal = stack::push(L, std::forward<T>(value));
  13775. R r = stack::get<R>(L, -backpedal);
  13776. if (should_pop) {
  13777. lua_pop(L, backpedal);
  13778. }
  13779. return r;
  13780. }
  13781. template <typename T, typename R = reference, bool should_pop = !is_stack_based<R>::value, typename... Args>
  13782. R make_reference(lua_State* L, Args&&... args) {
  13783. int backpedal = stack::push<T>(L, std::forward<Args>(args)...);
  13784. R r = stack::get<R>(L, -backpedal);
  13785. if (should_pop) {
  13786. lua_pop(L, backpedal);
  13787. }
  13788. return r;
  13789. }
  13790. template <typename base_type>
  13791. class basic_object : public basic_object_base<base_type> {
  13792. private:
  13793. typedef basic_object_base<base_type> base_t;
  13794. template <bool invert_and_pop = false>
  13795. basic_object(std::integral_constant<bool, invert_and_pop>, lua_State* L, int index = -1) noexcept
  13796. : base_t(L, index) {
  13797. if (invert_and_pop) {
  13798. lua_pop(L, -index);
  13799. }
  13800. }
  13801. public:
  13802. basic_object() noexcept = default;
  13803. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_object>>, meta::neg<std::is_same<base_type, stack_reference>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13804. basic_object(T&& r)
  13805. : base_t(std::forward<T>(r)) {
  13806. }
  13807. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13808. basic_object(lua_State* L, T&& r)
  13809. : base_t(L, std::forward<T>(r)) {
  13810. }
  13811. basic_object(lua_nil_t r)
  13812. : base_t(r) {
  13813. }
  13814. basic_object(const basic_object&) = default;
  13815. basic_object(basic_object&&) = default;
  13816. basic_object(const stack_reference& r) noexcept
  13817. : basic_object(r.lua_state(), r.stack_index()) {
  13818. }
  13819. basic_object(stack_reference&& r) noexcept
  13820. : basic_object(r.lua_state(), r.stack_index()) {
  13821. }
  13822. template <typename Super>
  13823. basic_object(const proxy_base<Super>& r) noexcept
  13824. : basic_object(r.operator basic_object()) {
  13825. }
  13826. template <typename Super>
  13827. basic_object(proxy_base<Super>&& r) noexcept
  13828. : basic_object(r.operator basic_object()) {
  13829. }
  13830. basic_object(lua_State* L, lua_nil_t r) noexcept
  13831. : base_t(L, r) {
  13832. }
  13833. basic_object(lua_State* L, int index = -1) noexcept
  13834. : base_t(L, index) {
  13835. }
  13836. basic_object(lua_State* L, absolute_index index) noexcept
  13837. : base_t(L, index) {
  13838. }
  13839. basic_object(lua_State* L, raw_index index) noexcept
  13840. : base_t(L, index) {
  13841. }
  13842. basic_object(lua_State* L, ref_index index) noexcept
  13843. : base_t(L, index) {
  13844. }
  13845. template <typename T, typename... Args>
  13846. basic_object(lua_State* L, in_place_type_t<T>, Args&&... args) noexcept
  13847. : basic_object(std::integral_constant<bool, !is_stack_based<base_t>::value>(), L, -stack::push<T>(L, std::forward<Args>(args)...)) {
  13848. }
  13849. template <typename T, typename... Args>
  13850. basic_object(lua_State* L, in_place_t, T&& arg, Args&&... args) noexcept
  13851. : basic_object(L, in_place_type<T>, std::forward<T>(arg), std::forward<Args>(args)...) {
  13852. }
  13853. basic_object& operator=(const basic_object&) = default;
  13854. basic_object& operator=(basic_object&&) = default;
  13855. basic_object& operator=(const base_type& b) {
  13856. base_t::operator=(b);
  13857. return *this;
  13858. }
  13859. basic_object& operator=(base_type&& b) {
  13860. base_t::operator=(std::move(b));
  13861. return *this;
  13862. }
  13863. template <typename Super>
  13864. basic_object& operator=(const proxy_base<Super>& r) {
  13865. this->operator=(r.operator basic_object());
  13866. return *this;
  13867. }
  13868. template <typename Super>
  13869. basic_object& operator=(proxy_base<Super>&& r) {
  13870. this->operator=(r.operator basic_object());
  13871. return *this;
  13872. }
  13873. };
  13874. template <typename T>
  13875. object make_object(lua_State* L, T&& value) {
  13876. return make_reference<object, true>(L, std::forward<T>(value));
  13877. }
  13878. template <typename T, typename... Args>
  13879. object make_object(lua_State* L, Args&&... args) {
  13880. return make_reference<T, object, true>(L, std::forward<Args>(args)...);
  13881. }
  13882. } // namespace sol
  13883. // end of sol/object.hpp
  13884. // beginning of sol/container_usertype_metatable.hpp
  13885. // beginning of sol/container_traits.hpp
  13886. namespace sol {
  13887. template <typename T>
  13888. struct container_traits;
  13889. template <typename T>
  13890. struct as_container_t {
  13891. T source;
  13892. as_container_t(T value)
  13893. : source(std::move(value)) {
  13894. }
  13895. operator std::add_rvalue_reference_t<T>() {
  13896. return std::move(source);
  13897. }
  13898. operator std::add_lvalue_reference_t<std::add_const_t<T>>() const {
  13899. return source;
  13900. }
  13901. };
  13902. template <typename T>
  13903. struct as_container_t<T&> {
  13904. std::reference_wrapper<T> source;
  13905. as_container_t(T& value)
  13906. : source(value) {
  13907. }
  13908. operator T&() {
  13909. return source;
  13910. }
  13911. };
  13912. template <typename T>
  13913. auto as_container(T&& value) {
  13914. return as_container_t<T>(std::forward<T>(value));
  13915. }
  13916. namespace container_detail {
  13917. template <typename T>
  13918. struct has_clear_test {
  13919. private:
  13920. typedef std::array<char, 1> one;
  13921. typedef std::array<char, 2> two;
  13922. template <typename C>
  13923. static one test(decltype(&C::clear));
  13924. template <typename C>
  13925. static two test(...);
  13926. public:
  13927. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  13928. };
  13929. template <typename T>
  13930. struct has_empty_test {
  13931. private:
  13932. typedef std::array<char, 1> one;
  13933. typedef std::array<char, 2> two;
  13934. template <typename C>
  13935. static one test(decltype(&C::empty));
  13936. template <typename C>
  13937. static two test(...);
  13938. public:
  13939. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  13940. };
  13941. template <typename T>
  13942. struct has_erase_after_test {
  13943. private:
  13944. typedef std::array<char, 1> one;
  13945. typedef std::array<char, 2> two;
  13946. template <typename C>
  13947. static one test(decltype(std::declval<C>().erase_after(std::declval<std::add_rvalue_reference_t<typename C::const_iterator>>()))*);
  13948. template <typename C>
  13949. static two test(...);
  13950. public:
  13951. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  13952. };
  13953. template <typename T, typename = void>
  13954. struct has_find_test {
  13955. private:
  13956. typedef std::array<char, 1> one;
  13957. typedef std::array<char, 2> two;
  13958. template <typename C>
  13959. static one test(decltype(std::declval<C>().find(std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  13960. template <typename C>
  13961. static two test(...);
  13962. public:
  13963. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  13964. };
  13965. template <typename T>
  13966. struct has_find_test<T, std::enable_if_t<meta::is_lookup<T>::value>> {
  13967. private:
  13968. typedef std::array<char, 1> one;
  13969. typedef std::array<char, 2> two;
  13970. template <typename C>
  13971. static one test(decltype(std::declval<C>().find(std::declval<std::add_rvalue_reference_t<typename C::key_type>>()))*);
  13972. template <typename C>
  13973. static two test(...);
  13974. public:
  13975. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  13976. };
  13977. template <typename T>
  13978. struct has_erase_test {
  13979. private:
  13980. typedef std::array<char, 1> one;
  13981. typedef std::array<char, 2> two;
  13982. template <typename C>
  13983. static one test(decltype(std::declval<C>().erase(std::declval<typename C::iterator>()))*);
  13984. template <typename C>
  13985. static two test(...);
  13986. public:
  13987. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  13988. };
  13989. template <typename T>
  13990. struct has_traits_find_test {
  13991. private:
  13992. typedef std::array<char, 1> one;
  13993. typedef std::array<char, 2> two;
  13994. template <typename C>
  13995. static one test(decltype(&C::find));
  13996. template <typename C>
  13997. static two test(...);
  13998. public:
  13999. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14000. };
  14001. template <typename T>
  14002. struct has_traits_insert_test {
  14003. private:
  14004. typedef std::array<char, 1> one;
  14005. typedef std::array<char, 2> two;
  14006. template <typename C>
  14007. static one test(decltype(&C::insert));
  14008. template <typename C>
  14009. static two test(...);
  14010. public:
  14011. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14012. };
  14013. template <typename T>
  14014. struct has_traits_erase_test {
  14015. private:
  14016. typedef std::array<char, 1> one;
  14017. typedef std::array<char, 2> two;
  14018. template <typename C>
  14019. static one test(decltype(&C::erase));
  14020. template <typename C>
  14021. static two test(...);
  14022. public:
  14023. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14024. };
  14025. template <typename T>
  14026. struct has_traits_index_set_test {
  14027. private:
  14028. typedef std::array<char, 1> one;
  14029. typedef std::array<char, 2> two;
  14030. template <typename C>
  14031. static one test(decltype(&C::index_set));
  14032. template <typename C>
  14033. static two test(...);
  14034. public:
  14035. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14036. };
  14037. template <typename T>
  14038. struct has_traits_index_get_test {
  14039. private:
  14040. typedef std::array<char, 1> one;
  14041. typedef std::array<char, 2> two;
  14042. template <typename C>
  14043. static one test(decltype(&C::index_get));
  14044. template <typename C>
  14045. static two test(...);
  14046. public:
  14047. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14048. };
  14049. template <typename T>
  14050. struct has_traits_set_test {
  14051. private:
  14052. typedef std::array<char, 1> one;
  14053. typedef std::array<char, 2> two;
  14054. template <typename C>
  14055. static one test(decltype(&C::set));
  14056. template <typename C>
  14057. static two test(...);
  14058. public:
  14059. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14060. };
  14061. template <typename T>
  14062. struct has_traits_get_test {
  14063. private:
  14064. typedef std::array<char, 1> one;
  14065. typedef std::array<char, 2> two;
  14066. template <typename C>
  14067. static one test(decltype(&C::get));
  14068. template <typename C>
  14069. static two test(...);
  14070. public:
  14071. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14072. };
  14073. template <typename T>
  14074. struct has_traits_at_test {
  14075. private:
  14076. typedef std::array<char, 1> one;
  14077. typedef std::array<char, 2> two;
  14078. template <typename C>
  14079. static one test(decltype(&C::at));
  14080. template <typename C>
  14081. static two test(...);
  14082. public:
  14083. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14084. };
  14085. template <typename T>
  14086. struct has_traits_pairs_test {
  14087. private:
  14088. typedef std::array<char, 1> one;
  14089. typedef std::array<char, 2> two;
  14090. template <typename C>
  14091. static one test(decltype(&C::pairs));
  14092. template <typename C>
  14093. static two test(...);
  14094. public:
  14095. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14096. };
  14097. template <typename T>
  14098. struct has_traits_ipairs_test {
  14099. private:
  14100. typedef std::array<char, 1> one;
  14101. typedef std::array<char, 2> two;
  14102. template <typename C>
  14103. static one test(decltype(&C::ipairs));
  14104. template <typename C>
  14105. static two test(...);
  14106. public:
  14107. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14108. };
  14109. template <typename T>
  14110. struct has_traits_next_test {
  14111. private:
  14112. typedef std::array<char, 1> one;
  14113. typedef std::array<char, 2> two;
  14114. template <typename C>
  14115. static one test(decltype(&C::next));
  14116. template <typename C>
  14117. static two test(...);
  14118. public:
  14119. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14120. };
  14121. template <typename T>
  14122. struct has_traits_add_test {
  14123. private:
  14124. typedef std::array<char, 1> one;
  14125. typedef std::array<char, 2> two;
  14126. template <typename C>
  14127. static one test(decltype(&C::add));
  14128. template <typename C>
  14129. static two test(...);
  14130. public:
  14131. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14132. };
  14133. template <typename T>
  14134. struct has_traits_size_test {
  14135. private:
  14136. typedef std::array<char, 1> one;
  14137. typedef std::array<char, 2> two;
  14138. template <typename C>
  14139. static one test(decltype(&C::size));
  14140. template <typename C>
  14141. static two test(...);
  14142. public:
  14143. static const bool value = sizeof(test<T>(0)) == sizeof(char);
  14144. };
  14145. template <typename T>
  14146. using has_clear = meta::boolean<has_clear_test<T>::value>;
  14147. template <typename T>
  14148. using has_empty = meta::boolean<has_empty_test<T>::value>;
  14149. template <typename T>
  14150. using has_find = meta::boolean<has_find_test<T>::value>;
  14151. template <typename T>
  14152. using has_erase = meta::boolean<has_erase_test<T>::value>;
  14153. template <typename T>
  14154. using has_erase_after = meta::boolean<has_erase_after_test<T>::value>;
  14155. template <typename T>
  14156. using has_traits_get = meta::boolean<has_traits_get_test<T>::value>;
  14157. template <typename T>
  14158. using has_traits_at = meta::boolean<has_traits_at_test<T>::value>;
  14159. template <typename T>
  14160. using has_traits_set = meta::boolean<has_traits_set_test<T>::value>;
  14161. template <typename T>
  14162. using has_traits_index_get = meta::boolean<has_traits_index_get_test<T>::value>;
  14163. template <typename T>
  14164. using has_traits_index_set = meta::boolean<has_traits_index_set_test<T>::value>;
  14165. template <typename T>
  14166. using has_traits_pairs = meta::boolean<has_traits_pairs_test<T>::value>;
  14167. template <typename T>
  14168. using has_traits_ipairs = meta::boolean<has_traits_ipairs_test<T>::value>;
  14169. template <typename T>
  14170. using has_traits_next = meta::boolean<has_traits_next_test<T>::value>;
  14171. template <typename T>
  14172. using has_traits_add = meta::boolean<has_traits_add_test<T>::value>;
  14173. template <typename T>
  14174. using has_traits_size = meta::boolean<has_traits_size_test<T>::value>;
  14175. template <typename T>
  14176. using has_traits_clear = has_clear<T>;
  14177. template <typename T>
  14178. using has_traits_empty = has_empty<T>;
  14179. template <typename T>
  14180. using has_traits_find = meta::boolean<has_traits_find_test<T>::value>;
  14181. template <typename T>
  14182. using has_traits_insert = meta::boolean<has_traits_insert_test<T>::value>;
  14183. template <typename T>
  14184. using has_traits_erase = meta::boolean<has_traits_erase_test<T>::value>;
  14185. template <typename T>
  14186. struct is_forced_container : is_container<T> {};
  14187. template <typename T>
  14188. struct is_forced_container<as_container_t<T>> : std::true_type {};
  14189. template <typename T>
  14190. struct container_decay {
  14191. typedef T type;
  14192. };
  14193. template <typename T>
  14194. struct container_decay<as_container_t<T>> {
  14195. typedef T type;
  14196. };
  14197. template <typename T>
  14198. using container_decay_t = typename container_decay<meta::unqualified_t<T>>::type;
  14199. template <typename T>
  14200. decltype(auto) get_key(std::false_type, T&& t) {
  14201. return std::forward<T>(t);
  14202. }
  14203. template <typename T>
  14204. decltype(auto) get_key(std::true_type, T&& t) {
  14205. return t.first;
  14206. }
  14207. template <typename T>
  14208. decltype(auto) get_value(std::false_type, T&& t) {
  14209. return std::forward<T>(t);
  14210. }
  14211. template <typename T>
  14212. decltype(auto) get_value(std::true_type, T&& t) {
  14213. return t.second;
  14214. }
  14215. struct error_result {
  14216. int results;
  14217. const char* fmt_;
  14218. std::array<const char*, 4> args;
  14219. error_result() : results(0), fmt_(nullptr) {
  14220. }
  14221. error_result(int results) : results(results), fmt_(nullptr) {
  14222. }
  14223. error_result(const char* fmt_, const char* msg) : results(0), fmt_(fmt_) {
  14224. args[0] = msg;
  14225. }
  14226. };
  14227. inline int handle_errors(lua_State* L, const error_result& er) {
  14228. if (er.fmt_ == nullptr) {
  14229. return er.results;
  14230. }
  14231. return luaL_error(L, er.fmt_, er.args[0], er.args[1], er.args[2], er.args[3]);
  14232. }
  14233. template <typename X, typename = void>
  14234. struct container_traits_default {
  14235. private:
  14236. typedef std::remove_pointer_t<meta::unwrap_unqualified_t<X>> T;
  14237. public:
  14238. typedef lua_nil_t iterator;
  14239. typedef lua_nil_t value_type;
  14240. static int at(lua_State* L) {
  14241. return luaL_error(L, "sol: cannot call 'at(index)' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14242. }
  14243. static int get(lua_State* L) {
  14244. return luaL_error(L, "sol: cannot call 'get(key)' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14245. }
  14246. static int index_get(lua_State* L) {
  14247. return luaL_error(L, "sol: cannot call 'container[key]' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14248. }
  14249. static int set(lua_State* L) {
  14250. return luaL_error(L, "sol: cannot call 'set(key, value)' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14251. }
  14252. static int index_set(lua_State* L) {
  14253. return luaL_error(L, "sol: cannot call 'container[key] = value' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14254. }
  14255. static int add(lua_State* L) {
  14256. return luaL_error(L, "sol: cannot call 'add' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14257. }
  14258. static int insert(lua_State* L) {
  14259. return luaL_error(L, "sol: cannot call 'insert' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14260. }
  14261. static int find(lua_State* L) {
  14262. return luaL_error(L, "sol: cannot call 'find' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14263. }
  14264. static int size(lua_State* L) {
  14265. return luaL_error(L, "sol: cannot call 'end' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14266. }
  14267. static int clear(lua_State* L) {
  14268. return luaL_error(L, "sol: cannot call 'clear' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14269. }
  14270. static int empty(lua_State* L) {
  14271. return luaL_error(L, "sol: cannot call 'empty' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14272. }
  14273. static int erase(lua_State* L) {
  14274. return luaL_error(L, "sol: cannot call 'erase' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14275. }
  14276. static int next(lua_State* L) {
  14277. return luaL_error(L, "sol: cannot call 'next' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14278. }
  14279. static int pairs(lua_State* L) {
  14280. return luaL_error(L, "sol: cannot call '__pairs/pairs' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14281. }
  14282. static int ipairs(lua_State* L) {
  14283. return luaL_error(L, "sol: cannot call '__ipairs' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14284. }
  14285. static iterator begin(lua_State* L, T&) {
  14286. luaL_error(L, "sol: cannot call 'being' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14287. return lua_nil;
  14288. }
  14289. static iterator end(lua_State* L, T&) {
  14290. luaL_error(L, "sol: cannot call 'end' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  14291. return lua_nil;
  14292. }
  14293. };
  14294. template <typename X>
  14295. struct container_traits_default<X, std::enable_if_t<meta::all<is_forced_container<meta::unqualified_t<X>>, meta::has_value_type<meta::unqualified_t<container_decay_t<X>>>, meta::has_iterator<meta::unqualified_t<container_decay_t<X>>>>::value>> {
  14296. private:
  14297. typedef std::remove_pointer_t<meta::unwrap_unqualified_t<container_decay_t<X>>> T;
  14298. private:
  14299. typedef container_traits<X> deferred_traits;
  14300. typedef meta::is_associative<T> is_associative;
  14301. typedef meta::is_lookup<T> is_lookup;
  14302. typedef meta::is_matched_lookup<T> is_matched_lookup;
  14303. typedef typename T::iterator iterator;
  14304. typedef typename T::value_type value_type;
  14305. typedef std::conditional_t<is_matched_lookup::value,
  14306. std::pair<value_type, value_type>,
  14307. std::conditional_t<is_associative::value || is_lookup::value,
  14308. value_type,
  14309. std::pair<std::ptrdiff_t, value_type>
  14310. >
  14311. > KV;
  14312. typedef typename KV::first_type K;
  14313. typedef typename KV::second_type V;
  14314. typedef std::conditional_t<is_matched_lookup::value, std::ptrdiff_t, K> next_K;
  14315. typedef decltype(*std::declval<iterator&>()) iterator_return;
  14316. typedef std::conditional_t<is_associative::value || is_matched_lookup::value,
  14317. std::add_lvalue_reference_t<V>,
  14318. std::conditional_t<is_lookup::value,
  14319. V,
  14320. iterator_return
  14321. >
  14322. > captured_type;
  14323. typedef typename meta::iterator_tag<iterator>::type iterator_category;
  14324. typedef std::is_same<iterator_category, std::input_iterator_tag> is_input_iterator;
  14325. typedef std::conditional_t<is_input_iterator::value,
  14326. V,
  14327. decltype(detail::deref_non_pointer(std::declval<captured_type>()))
  14328. > push_type;
  14329. typedef std::is_copy_assignable<V> is_copyable;
  14330. typedef meta::neg<meta::any<
  14331. std::is_const<V>, std::is_const<std::remove_reference_t<iterator_return>>, meta::neg<is_copyable>
  14332. >> is_writable;
  14333. typedef meta::unqualified_t<decltype(get_key(is_associative(), std::declval<std::add_lvalue_reference_t<value_type>>()))> key_type;
  14334. typedef meta::all<std::is_integral<K>, meta::neg<meta::any<is_associative, is_lookup>>> is_linear_integral;
  14335. struct iter {
  14336. T& source;
  14337. iterator it;
  14338. std::size_t i;
  14339. iter(T& source, iterator it)
  14340. : source(source), it(std::move(it)), i(0) {
  14341. }
  14342. };
  14343. static auto& get_src(lua_State* L) {
  14344. #if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  14345. auto p = stack::unqualified_check_get<T*>(L, 1);
  14346. if (!p) {
  14347. luaL_error(L, "sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type)", detail::demangle<T>().c_str());
  14348. }
  14349. if (p.value() == nullptr) {
  14350. luaL_error(L, "sol: 'self' argument is nil (pass 'self' as first argument with ':' or call on a '%s' type)", detail::demangle<T>().c_str());
  14351. }
  14352. return *p.value();
  14353. #else
  14354. return stack::unqualified_get<T>(L, 1);
  14355. #endif // Safe getting with error
  14356. }
  14357. static error_result at_category(std::input_iterator_tag, lua_State* L, T& self, std::ptrdiff_t pos) {
  14358. pos += deferred_traits::index_adjustment(L, self);
  14359. if (pos < 0) {
  14360. return stack::push(L, lua_nil);
  14361. }
  14362. auto it = deferred_traits::begin(L, self);
  14363. auto e = deferred_traits::end(L, self);
  14364. if (it == e) {
  14365. return stack::push(L, lua_nil);
  14366. }
  14367. while (pos > 0) {
  14368. --pos;
  14369. ++it;
  14370. if (it == e) {
  14371. return stack::push(L, lua_nil);
  14372. }
  14373. }
  14374. return get_associative(is_associative(), L, it);
  14375. }
  14376. static error_result at_category(std::random_access_iterator_tag, lua_State* L, T& self, std::ptrdiff_t pos) {
  14377. std::ptrdiff_t len = static_cast<std::ptrdiff_t>(size_start(L, self));
  14378. pos += deferred_traits::index_adjustment(L, self);
  14379. if (pos < 0 || pos >= len) {
  14380. return stack::push(L, lua_nil);
  14381. }
  14382. auto it = std::next(deferred_traits::begin(L, self), pos);
  14383. return get_associative(is_associative(), L, it);
  14384. }
  14385. static error_result at_start(lua_State* L, T& self, std::ptrdiff_t pos) {
  14386. return at_category(iterator_category(), L, self, pos);
  14387. }
  14388. static error_result get_associative(std::true_type, lua_State* L, iterator& it) {
  14389. auto& v = *it;
  14390. return stack::stack_detail::push_reference<push_type>(L, detail::deref_non_pointer(v.second));
  14391. }
  14392. static error_result get_associative(std::false_type, lua_State* L, iterator& it) {
  14393. return stack::stack_detail::push_reference<push_type>(L, detail::deref_non_pointer(*it));
  14394. }
  14395. static error_result get_category(std::input_iterator_tag, lua_State* L, T& self, K& key) {
  14396. key += deferred_traits::index_adjustment(L, self);
  14397. if (key < 0) {
  14398. return stack::push(L, lua_nil);
  14399. }
  14400. auto it = deferred_traits::begin(L, self);
  14401. auto e = deferred_traits::end(L, self);
  14402. if (it == e) {
  14403. return stack::push(L, lua_nil);
  14404. }
  14405. while (key > 0) {
  14406. --key;
  14407. ++it;
  14408. if (it == e) {
  14409. return stack::push(L, lua_nil);
  14410. }
  14411. }
  14412. return get_associative(is_associative(), L, it);
  14413. }
  14414. static error_result get_category(std::random_access_iterator_tag, lua_State* L, T& self, K& key) {
  14415. std::ptrdiff_t len = static_cast<std::ptrdiff_t>(size_start(L, self));
  14416. key += deferred_traits::index_adjustment(L, self);
  14417. if (key < 0 || key >= len) {
  14418. return stack::push(L, lua_nil);
  14419. }
  14420. auto it = std::next(deferred_traits::begin(L, self), key);
  14421. return get_associative(is_associative(), L, it);
  14422. }
  14423. static error_result get_it(std::true_type, lua_State* L, T& self, K& key) {
  14424. return get_category(iterator_category(), L, self, key);
  14425. }
  14426. static error_result get_comparative(std::true_type, lua_State* L, T& self, K& key) {
  14427. auto fx = [&](const value_type& r) -> bool {
  14428. return key == get_key(is_associative(), r);
  14429. };
  14430. auto e = deferred_traits::end(L, self);
  14431. auto it = std::find_if(deferred_traits::begin(L, self), e, std::ref(fx));
  14432. if (it == e) {
  14433. return stack::push(L, lua_nil);
  14434. }
  14435. return get_associative(is_associative(), L, it);
  14436. }
  14437. static error_result get_comparative(std::false_type, lua_State*, T&, K&) {
  14438. return error_result("cannot get this key on '%s': no suitable way to increment iterator and compare to key value '%s'", detail::demangle<T>().data(), detail::demangle<K>().data());
  14439. }
  14440. static error_result get_it(std::false_type, lua_State* L, T& self, K& key) {
  14441. return get_comparative(meta::supports_op_equal<K, key_type>(), L, self, key);
  14442. }
  14443. static error_result set_associative(std::true_type, iterator& it, stack_object value) {
  14444. auto& v = *it;
  14445. v.second = value.as<V>();
  14446. return {};
  14447. }
  14448. static error_result set_associative(std::false_type, iterator& it, stack_object value) {
  14449. auto& v = *it;
  14450. v = value.as<V>();
  14451. return {};
  14452. }
  14453. static error_result set_writable(std::true_type, lua_State*, T&, iterator& it, stack_object value) {
  14454. return set_associative(is_associative(), it, std::move(value));
  14455. }
  14456. static error_result set_writable(std::false_type, lua_State*, T&, iterator&, stack_object) {
  14457. return error_result("cannot perform a 'set': '%s's iterator reference is not writable (non-copy-assignable or const)", detail::demangle<T>().data());
  14458. }
  14459. static error_result set_category(std::input_iterator_tag, lua_State* L, T& self, stack_object okey, stack_object value) {
  14460. decltype(auto) key = okey.as<K>();
  14461. key += deferred_traits::index_adjustment(L, self);
  14462. auto e = deferred_traits::end(L, self);
  14463. auto it = deferred_traits::begin(L, self);
  14464. auto backit = it;
  14465. for (; key > 0 && it != e; --key, ++it) {
  14466. backit = it;
  14467. }
  14468. if (it == e) {
  14469. if (key == 0) {
  14470. return add_copyable(is_copyable(), L, self, std::move(value), meta::has_insert_after<T>::value ? backit : it);
  14471. }
  14472. return error_result("out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  14473. }
  14474. return set_writable(is_writable(), L, self, it, std::move(value));
  14475. }
  14476. static error_result set_category(std::random_access_iterator_tag, lua_State* L, T& self, stack_object okey, stack_object value) {
  14477. decltype(auto) key = okey.as<K>();
  14478. if (key <= 0) {
  14479. return error_result("sol: out of bounds (too small) for set on '%s'", detail::demangle<T>().c_str());
  14480. }
  14481. key += deferred_traits::index_adjustment(L, self);
  14482. std::ptrdiff_t len = static_cast<std::ptrdiff_t>(size_start(L, self));
  14483. if (key == len) {
  14484. return add_copyable(is_copyable(), L, self, std::move(value));
  14485. }
  14486. else if (key > len) {
  14487. return error_result("sol: out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  14488. }
  14489. auto it = std::next(deferred_traits::begin(L, self), key);
  14490. return set_writable(is_writable(), L, self, it, std::move(value));
  14491. }
  14492. static error_result set_comparative(std::true_type, lua_State* L, T& self, stack_object okey, stack_object value) {
  14493. decltype(auto) key = okey.as<K>();
  14494. if (!is_writable::value) {
  14495. return error_result("cannot perform a 'set': '%s's iterator reference is not writable (non-copy-assignable or const)", detail::demangle<T>().data());
  14496. }
  14497. auto fx = [&](const value_type& r) -> bool {
  14498. return key == get_key(is_associative(), r);
  14499. };
  14500. auto e = deferred_traits::end(L, self);
  14501. auto it = std::find_if(deferred_traits::begin(L, self), e, std::ref(fx));
  14502. if (it == e) {
  14503. return {};
  14504. }
  14505. return set_writable(is_writable(), L, self, it, std::move(value));
  14506. }
  14507. static error_result set_comparative(std::false_type, lua_State*, T&, stack_object, stack_object) {
  14508. return error_result("cannot set this value on '%s': no suitable way to increment iterator or compare to '%s' key", detail::demangle<T>().data(), detail::demangle<K>().data());
  14509. }
  14510. static error_result set_associative_insert(std::true_type, lua_State*, T& self, iterator& it, K& key, stack_object value) {
  14511. self.insert(it, value_type(key, value.as<V>()));
  14512. return {};
  14513. }
  14514. static error_result set_associative_insert(std::false_type, lua_State*, T& self, iterator& it, K& key, stack_object) {
  14515. self.insert(it, key);
  14516. return {};
  14517. }
  14518. static error_result set_associative_find(std::true_type, lua_State* L, T& self, stack_object okey, stack_object value) {
  14519. decltype(auto) key = okey.as<K>();
  14520. auto it = self.find(key);
  14521. if (it == deferred_traits::end(L, self)) {
  14522. return set_associative_insert(is_associative(), L, self, it, key, std::move(value));
  14523. }
  14524. return set_writable(is_writable(), L, self, it, std::move(value));
  14525. }
  14526. static error_result set_associative_find(std::false_type, lua_State* L, T& self, stack_object key, stack_object value) {
  14527. return set_comparative(meta::supports_op_equal<K, key_type>(), L, self, std::move(key), std::move(value));
  14528. }
  14529. static error_result set_it(std::true_type, lua_State* L, T& self, stack_object key, stack_object value) {
  14530. return set_category(iterator_category(), L, self, std::move(key), std::move(value));
  14531. }
  14532. static error_result set_it(std::false_type, lua_State* L, T& self, stack_object key, stack_object value) {
  14533. return set_associative_find(meta::all<has_find<T>, meta::any<is_associative, is_lookup>>(), L, self, std::move(key), std::move(value));
  14534. }
  14535. static error_result find_has_associative_lookup(std::true_type, lua_State* L, T& self) {
  14536. decltype(auto) key = stack::unqualified_get<K>(L, 2);
  14537. auto it = self.find(key);
  14538. if (it == deferred_traits::end(L, self)) {
  14539. return stack::push(L, lua_nil);
  14540. }
  14541. return get_associative(is_associative(), L, it);
  14542. }
  14543. static error_result find_has_associative_lookup(std::false_type, lua_State* L, T& self) {
  14544. decltype(auto) value = stack::unqualified_get<V>(L, 2);
  14545. auto it = self.find(value);
  14546. if (it == deferred_traits::end(L, self)) {
  14547. return stack::push(L, lua_nil);
  14548. }
  14549. return get_associative(is_associative(), L, it);
  14550. }
  14551. static error_result find_has(std::true_type, lua_State* L, T& self) {
  14552. return find_has_associative_lookup(meta::any<is_lookup, is_associative>(), L, self);
  14553. }
  14554. static error_result find_associative_lookup(std::true_type, lua_State* L, iterator& it, std::size_t) {
  14555. return get_associative(is_associative(), L, it);
  14556. }
  14557. static error_result find_associative_lookup(std::false_type, lua_State* L, iterator&, std::size_t index) {
  14558. return stack::push(L, index);
  14559. }
  14560. static error_result find_comparative(std::false_type, lua_State*, T&) {
  14561. return error_result("cannot call 'find' on '%s': there is no 'find' function and the value_type is not equality comparable", detail::demangle<T>().c_str());
  14562. }
  14563. static error_result find_comparative(std::true_type, lua_State* L, T& self) {
  14564. decltype(auto) value = stack::unqualified_get<V>(L, 2);
  14565. auto it = deferred_traits::begin(L, self);
  14566. auto e = deferred_traits::end(L, self);
  14567. std::size_t index = 1;
  14568. for (;; ++it, ++index) {
  14569. if (it == e) {
  14570. return stack::push(L, lua_nil);
  14571. }
  14572. if (value == get_value(is_associative(), *it)) {
  14573. break;
  14574. }
  14575. }
  14576. return find_associative_lookup(meta::any<is_lookup, is_associative>(), L, it, index);
  14577. }
  14578. static error_result find_has(std::false_type, lua_State* L, T& self) {
  14579. return find_comparative(meta::supports_op_equal<V>(), L, self);
  14580. }
  14581. static error_result add_insert_after(std::false_type, lua_State* L, T& self, stack_object value, iterator&) {
  14582. return add_insert_after(std::false_type(), L, self, value);
  14583. }
  14584. static error_result add_insert_after(std::false_type, lua_State*, T&, stack_object) {
  14585. return error_result("cannot call 'add' on type '%s': no suitable insert/push_back C++ functions", detail::demangle<T>().data());
  14586. }
  14587. static error_result add_insert_after(std::true_type, lua_State*, T& self, stack_object value, iterator& pos) {
  14588. self.insert_after(pos, value.as<V>());
  14589. return {};
  14590. }
  14591. static error_result add_insert_after(std::true_type, lua_State* L, T& self, stack_object value) {
  14592. auto backit = self.before_begin();
  14593. {
  14594. auto e = deferred_traits::end(L, self);
  14595. for (auto it = deferred_traits::begin(L, self); it != e; ++backit, ++it) {
  14596. }
  14597. }
  14598. return add_insert_after(std::true_type(), L, self, value, backit);
  14599. }
  14600. static error_result add_insert(std::true_type, lua_State*, T& self, stack_object value, iterator& pos) {
  14601. self.insert(pos, value.as<V>());
  14602. return {};
  14603. }
  14604. static error_result add_insert(std::true_type, lua_State* L, T& self, stack_object value) {
  14605. auto pos = deferred_traits::end(L, self);
  14606. return add_insert(std::true_type(), L, self, value, pos);
  14607. }
  14608. static error_result add_insert(std::false_type, lua_State* L, T& self, stack_object value, iterator& pos) {
  14609. return add_insert_after(meta::has_insert_after<T>(), L, self, std::move(value), pos);
  14610. }
  14611. static error_result add_insert(std::false_type, lua_State* L, T& self, stack_object value) {
  14612. return add_insert_after(meta::has_insert_after<T>(), L, self, std::move(value));
  14613. }
  14614. static error_result add_push_back(std::true_type, lua_State*, T& self, stack_object value, iterator&) {
  14615. self.push_back(value.as<V>());
  14616. return {};
  14617. }
  14618. static error_result add_push_back(std::true_type, lua_State*, T& self, stack_object value) {
  14619. self.push_back(value.as<V>());
  14620. return {};
  14621. }
  14622. static error_result add_push_back(std::false_type, lua_State* L, T& self, stack_object value, iterator& pos) {
  14623. return add_insert(meta::has_insert<T>(), L, self, value, pos);
  14624. }
  14625. static error_result add_push_back(std::false_type, lua_State* L, T& self, stack_object value) {
  14626. return add_insert(meta::has_insert<T>(), L, self, value);
  14627. }
  14628. static error_result add_associative(std::true_type, lua_State* L, T& self, stack_object key, iterator& pos) {
  14629. self.insert(pos, value_type(key.as<K>(), stack::unqualified_get<V>(L, 3)));
  14630. return {};
  14631. }
  14632. static error_result add_associative(std::true_type, lua_State* L, T& self, stack_object key) {
  14633. auto pos = deferred_traits::end(L, self);
  14634. return add_associative(std::true_type(), L, self, std::move(key), pos);
  14635. }
  14636. static error_result add_associative(std::false_type, lua_State* L, T& self, stack_object value, iterator& pos) {
  14637. return add_push_back(meta::has_push_back<T>(), L, self, value, pos);
  14638. }
  14639. static error_result add_associative(std::false_type, lua_State* L, T& self, stack_object value) {
  14640. return add_push_back(meta::has_push_back<T>(), L, self, value);
  14641. }
  14642. static error_result add_copyable(std::true_type, lua_State* L, T& self, stack_object value, iterator& pos) {
  14643. return add_associative(is_associative(), L, self, std::move(value), pos);
  14644. }
  14645. static error_result add_copyable(std::true_type, lua_State* L, T& self, stack_object value) {
  14646. return add_associative(is_associative(), L, self, value);
  14647. }
  14648. static error_result add_copyable(std::false_type, lua_State* L, T& self, stack_object value, iterator&) {
  14649. return add_copyable(std::false_type(), L, self, std::move(value));
  14650. }
  14651. static error_result add_copyable(std::false_type, lua_State*, T&, stack_object) {
  14652. return error_result("cannot call 'add' on '%s': value_type is non-copyable", detail::demangle<T>().data());
  14653. }
  14654. static error_result insert_lookup(std::true_type, lua_State* L, T& self, stack_object, stack_object value) {
  14655. // TODO: should we warn or error about someone calling insert on an ordered / lookup container with no associativity?
  14656. return add_copyable(std::true_type(), L, self, std::move(value));
  14657. }
  14658. static error_result insert_lookup(std::false_type, lua_State* L, T& self, stack_object where, stack_object value) {
  14659. auto it = deferred_traits::begin(L, self);
  14660. auto key = where.as<K>();
  14661. key += deferred_traits::index_adjustment(L, self);
  14662. std::advance(it, key);
  14663. self.insert(it, value.as<V>());
  14664. return {};
  14665. }
  14666. static error_result insert_after_has(std::true_type, lua_State* L, T& self, stack_object where, stack_object value) {
  14667. auto key = where.as<K>();
  14668. auto backit = self.before_begin();
  14669. {
  14670. key += deferred_traits::index_adjustment(L, self);
  14671. auto e = deferred_traits::end(L, self);
  14672. for (auto it = deferred_traits::begin(L, self); key > 0; ++backit, ++it, --key) {
  14673. if (backit == e) {
  14674. return error_result("sol: out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  14675. }
  14676. }
  14677. }
  14678. self.insert_after(backit, value.as<V>());
  14679. return {};
  14680. }
  14681. static error_result insert_after_has(std::false_type, lua_State*, T&, stack_object, stack_object) {
  14682. return error_result("cannot call 'insert' on '%s': no suitable or similar functionality detected on this container", detail::demangle<T>().data());
  14683. }
  14684. static error_result insert_has(std::true_type, lua_State* L, T& self, stack_object key, stack_object value) {
  14685. return insert_lookup(meta::any<is_associative, is_lookup>(), L, self, std::move(key), std::move(value));
  14686. }
  14687. static error_result insert_has(std::false_type, lua_State* L, T& self, stack_object where, stack_object value) {
  14688. return insert_after_has(meta::has_insert_after<T>(), L, self, where, value);
  14689. }
  14690. static error_result insert_copyable(std::true_type, lua_State* L, T& self, stack_object key, stack_object value) {
  14691. return insert_has(meta::has_insert<T>(), L, self, std::move(key), std::move(value));
  14692. }
  14693. static error_result insert_copyable(std::false_type, lua_State*, T&, stack_object, stack_object) {
  14694. return error_result("cannot call 'insert' on '%s': value_type is non-copyable", detail::demangle<T>().data());
  14695. }
  14696. static error_result erase_integral(std::true_type, lua_State* L, T& self, K& key) {
  14697. auto it = deferred_traits::begin(L, self);
  14698. key += deferred_traits::index_adjustment(L, self);
  14699. std::advance(it, key);
  14700. self.erase(it);
  14701. return {};
  14702. }
  14703. static error_result erase_integral(std::false_type, lua_State* L, T& self, const K& key) {
  14704. auto fx = [&](const value_type& r) -> bool {
  14705. return key == r;
  14706. };
  14707. auto e = deferred_traits::end(L, self);
  14708. auto it = std::find_if(deferred_traits::begin(L, self), e, std::ref(fx));
  14709. if (it == e) {
  14710. return {};
  14711. }
  14712. self.erase(it);
  14713. return {};
  14714. }
  14715. static error_result erase_associative_lookup(std::true_type, lua_State*, T& self, const K& key) {
  14716. self.erase(key);
  14717. return {};
  14718. }
  14719. static error_result erase_associative_lookup(std::false_type, lua_State* L, T& self, K& key) {
  14720. return erase_integral(std::is_integral<K>(), L, self, key);
  14721. }
  14722. static error_result erase_after_has(std::true_type, lua_State* L, T& self, K& key) {
  14723. auto backit = self.before_begin();
  14724. {
  14725. key += deferred_traits::index_adjustment(L, self);
  14726. auto e = deferred_traits::end(L, self);
  14727. for (auto it = deferred_traits::begin(L, self); key > 0; ++backit, ++it, --key) {
  14728. if (backit == e) {
  14729. return error_result("sol: out of bounds for erase on '%s'", detail::demangle<T>().c_str());
  14730. }
  14731. }
  14732. }
  14733. self.erase_after(backit);
  14734. return {};
  14735. }
  14736. static error_result erase_after_has(std::false_type, lua_State*, T&, const K&) {
  14737. return error_result("sol: cannot call erase on '%s'", detail::demangle<T>().c_str());
  14738. }
  14739. static error_result erase_has(std::true_type, lua_State* L, T& self, K& key) {
  14740. return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
  14741. }
  14742. static error_result erase_has(std::false_type, lua_State* L, T& self, K& key) {
  14743. return erase_after_has(has_erase_after<T>(), L, self, key);
  14744. }
  14745. static auto size_has(std::false_type, lua_State* L, T& self) {
  14746. return std::distance(deferred_traits::begin(L, self), deferred_traits::end(L, self));
  14747. }
  14748. static auto size_has(std::true_type, lua_State*, T& self) {
  14749. return self.size();
  14750. }
  14751. static void clear_has(std::true_type, lua_State*, T& self) {
  14752. self.clear();
  14753. }
  14754. static void clear_has(std::false_type, lua_State* L, T&) {
  14755. luaL_error(L, "sol: cannot call clear on '%s'", detail::demangle<T>().c_str());
  14756. }
  14757. static bool empty_has(std::true_type, lua_State*, T& self) {
  14758. return self.empty();
  14759. }
  14760. static bool empty_has(std::false_type, lua_State* L, T& self) {
  14761. return deferred_traits::begin(L, self) == deferred_traits::end(L, self);
  14762. }
  14763. static error_result get_start(lua_State* L, T& self, K& key) {
  14764. return get_it(is_linear_integral(), L, self, key);
  14765. }
  14766. static error_result set_start(lua_State* L, T& self, stack_object key, stack_object value) {
  14767. return set_it(is_linear_integral(), L, self, std::move(key), std::move(value));
  14768. }
  14769. static std::size_t size_start(lua_State* L, T& self) {
  14770. return size_has(meta::has_size<T>(), L, self);
  14771. }
  14772. static void clear_start(lua_State* L, T& self) {
  14773. clear_has(has_clear<T>(), L, self);
  14774. }
  14775. static bool empty_start(lua_State* L, T& self) {
  14776. return empty_has(has_empty<T>(), L, self);
  14777. }
  14778. static error_result erase_start(lua_State* L, T& self, K& key) {
  14779. return erase_has(has_erase<T>(), L, self, key);
  14780. }
  14781. template <bool ip>
  14782. static int next_associative(std::true_type, lua_State* L) {
  14783. iter& i = stack::unqualified_get<user<iter>>(L, 1);
  14784. auto& source = i.source;
  14785. auto& it = i.it;
  14786. if (it == deferred_traits::end(L, source)) {
  14787. return 0;
  14788. }
  14789. int p;
  14790. if (ip) {
  14791. ++i.i;
  14792. p = stack::push_reference(L, i.i);
  14793. }
  14794. else {
  14795. p = stack::push_reference(L, it->first);
  14796. }
  14797. p += stack::stack_detail::push_reference<push_type>(L, detail::deref_non_pointer(it->second));
  14798. std::advance(it, 1);
  14799. return p;
  14800. }
  14801. template <bool>
  14802. static int next_associative(std::false_type, lua_State* L) {
  14803. iter& i = stack::unqualified_get<user<iter>>(L, 1);
  14804. auto& source = i.source;
  14805. auto& it = i.it;
  14806. next_K k = stack::unqualified_get<next_K>(L, 2);
  14807. if (it == deferred_traits::end(L, source)) {
  14808. return 0;
  14809. }
  14810. int p;
  14811. p = stack::push_reference(L, k + 1);
  14812. p += stack::stack_detail::push_reference<push_type>(L, detail::deref_non_pointer(*it));
  14813. std::advance(it, 1);
  14814. return p;
  14815. }
  14816. template <bool ip>
  14817. static int next_iter(lua_State* L) {
  14818. typedef meta::any<is_associative, meta::all<is_lookup, meta::neg<is_matched_lookup>>> is_assoc;
  14819. return next_associative<ip>(is_assoc(), L);
  14820. }
  14821. template <bool ip>
  14822. static int pairs_associative(std::true_type, lua_State* L) {
  14823. auto& src = get_src(L);
  14824. stack::push(L, next_iter<ip>);
  14825. stack::push<user<iter>>(L, src, deferred_traits::begin(L, src));
  14826. stack::push(L, lua_nil);
  14827. return 3;
  14828. }
  14829. template <bool ip>
  14830. static int pairs_associative(std::false_type, lua_State* L) {
  14831. auto& src = get_src(L);
  14832. stack::push(L, next_iter<ip>);
  14833. stack::push<user<iter>>(L, src, deferred_traits::begin(L, src));
  14834. stack::push(L, 0);
  14835. return 3;
  14836. }
  14837. public:
  14838. static int at(lua_State* L) {
  14839. auto& self = get_src(L);
  14840. error_result er;
  14841. {
  14842. std::ptrdiff_t pos = stack::unqualified_get<std::ptrdiff_t>(L);
  14843. er = at_start(L, self, pos);
  14844. }
  14845. return handle_errors(L, er);
  14846. }
  14847. static int get(lua_State* L) {
  14848. auto& self = get_src(L);
  14849. error_result er;
  14850. {
  14851. decltype(auto) key = stack::unqualified_get<K>(L);
  14852. er = get_start(L, self, key);
  14853. }
  14854. return handle_errors(L, er);
  14855. }
  14856. static int index_get(lua_State* L) {
  14857. return get(L);
  14858. }
  14859. static int set(lua_State* L) {
  14860. stack_object value = stack_object(L, raw_index(3));
  14861. if (type_of(L, 3) == type::lua_nil) {
  14862. return erase(L);
  14863. }
  14864. auto& self = get_src(L);
  14865. error_result er = set_start(L, self, stack_object(L, raw_index(2)), std::move(value));
  14866. return handle_errors(L, er);
  14867. }
  14868. static int index_set(lua_State* L) {
  14869. return set(L);
  14870. }
  14871. static int add(lua_State* L) {
  14872. auto& self = get_src(L);
  14873. error_result er = add_copyable(is_copyable(), L, self, stack_object(L, raw_index(2)));
  14874. return handle_errors(L, er);
  14875. }
  14876. static int insert(lua_State* L) {
  14877. auto& self = get_src(L);
  14878. error_result er = insert_copyable(is_copyable(), L, self, stack_object(L, raw_index(2)), stack_object(L, raw_index(3)));
  14879. return handle_errors(L, er);
  14880. }
  14881. static int find(lua_State* L) {
  14882. auto& self = get_src(L);
  14883. error_result er = find_has(has_find<T>(), L, self);
  14884. return handle_errors(L, er);
  14885. }
  14886. static iterator begin(lua_State*, T& self) {
  14887. using std::begin;
  14888. return begin(self);
  14889. }
  14890. static iterator end(lua_State*, T& self) {
  14891. using std::end;
  14892. return end(self);
  14893. }
  14894. static int size(lua_State* L) {
  14895. auto& self = get_src(L);
  14896. std::size_t r = size_start(L, self);
  14897. return stack::push(L, r);
  14898. }
  14899. static int clear(lua_State* L) {
  14900. auto& self = get_src(L);
  14901. clear_start(L, self);
  14902. return 0;
  14903. }
  14904. static int erase(lua_State* L) {
  14905. auto& self = get_src(L);
  14906. error_result er;
  14907. {
  14908. decltype(auto) key = stack::unqualified_get<K>(L, 2);
  14909. er = erase_start(L, self, key);
  14910. }
  14911. return handle_errors(L, er);
  14912. }
  14913. static int empty(lua_State* L) {
  14914. auto& self = get_src(L);
  14915. return stack::push(L, empty_start(L, self));
  14916. }
  14917. static std::ptrdiff_t index_adjustment(lua_State*, T&) {
  14918. #if defined(SOL_CONTAINERS_START_INDEX)
  14919. return static_cast<std::ptrdiff_t>((SOL_CONTAINERS_START) == 0 ? 0 : -(SOL_CONTAINERS_START));
  14920. #else
  14921. return static_cast<std::ptrdiff_t>(-1);
  14922. #endif
  14923. }
  14924. static int pairs(lua_State* L) {
  14925. typedef meta::any<is_associative, meta::all<is_lookup, meta::neg<is_matched_lookup>>> is_assoc;
  14926. return pairs_associative<false>(is_assoc(), L);
  14927. }
  14928. static int ipairs(lua_State* L) {
  14929. typedef meta::any<is_associative, meta::all<is_lookup, meta::neg<is_matched_lookup>>> is_assoc;
  14930. return pairs_associative<true>(is_assoc(), L);
  14931. }
  14932. static int next(lua_State* L) {
  14933. return stack::push(L, next_iter<false>);
  14934. }
  14935. };
  14936. template <typename X>
  14937. struct container_traits_default<X, std::enable_if_t<std::is_array<std::remove_pointer_t<meta::unwrap_unqualified_t<X>>>::value>> {
  14938. private:
  14939. typedef std::remove_pointer_t<meta::unwrap_unqualified_t<X>> T;
  14940. typedef container_traits<X> deferred_traits;
  14941. public:
  14942. typedef std::remove_extent_t<T> value_type;
  14943. typedef value_type* iterator;
  14944. private:
  14945. struct iter {
  14946. T& source;
  14947. iterator it;
  14948. iter(T& source, iterator it)
  14949. : source(source), it(std::move(it)) {
  14950. }
  14951. };
  14952. static auto& get_src(lua_State* L) {
  14953. auto p = stack::unqualified_check_get<T*>(L, 1);
  14954. #if defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  14955. if (!p) {
  14956. luaL_error(L, "sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type)", detail::demangle<T>().c_str());
  14957. }
  14958. if (p.value() == nullptr) {
  14959. luaL_error(L, "sol: 'self' argument is nil (pass 'self' as first argument with ':' or call on a '%s' type)", detail::demangle<T>().c_str());
  14960. }
  14961. #endif // Safe getting with error
  14962. return *p.value();
  14963. }
  14964. static int find(std::true_type, lua_State* L) {
  14965. T& self = get_src(L);
  14966. decltype(auto) value = stack::unqualified_get<value_type>(L, 2);
  14967. std::size_t N = std::extent<T>::value;
  14968. for (std::size_t idx = 0; idx < N; ++idx) {
  14969. const auto& v = self[idx];
  14970. if (v == value) {
  14971. return stack::push(L, idx + 1);
  14972. }
  14973. }
  14974. return stack::push(L, lua_nil);
  14975. }
  14976. static int find(std::false_type, lua_State* L) {
  14977. return luaL_error(L, "sol: cannot call 'find' on '%s': no supported comparison operator for the value type", detail::demangle<T>().c_str());
  14978. }
  14979. static int next_iter(lua_State* L) {
  14980. iter& i = stack::unqualified_get<user<iter>>(L, 1);
  14981. auto& source = i.source;
  14982. auto& it = i.it;
  14983. std::size_t k = stack::unqualified_get<std::size_t>(L, 2);
  14984. if (it == deferred_traits::end(L, source)) {
  14985. return 0;
  14986. }
  14987. int p;
  14988. p = stack::push_reference(L, k + 1);
  14989. p += stack::push_reference(L, detail::deref_non_pointer(*it));
  14990. std::advance(it, 1);
  14991. return p;
  14992. }
  14993. public:
  14994. static int clear(lua_State* L) {
  14995. return luaL_error(L, "sol: cannot call 'clear' on type '%s': cannot remove all items from a fixed array", detail::demangle<T>().c_str());
  14996. }
  14997. static int erase(lua_State* L) {
  14998. return luaL_error(L, "sol: cannot call 'erase' on type '%s': cannot remove an item from fixed arrays", detail::demangle<T>().c_str());
  14999. }
  15000. static int add(lua_State* L) {
  15001. return luaL_error(L, "sol: cannot call 'add' on type '%s': cannot add to fixed arrays", detail::demangle<T>().c_str());
  15002. }
  15003. static int insert(lua_State* L) {
  15004. return luaL_error(L, "sol: cannot call 'insert' on type '%s': cannot insert new entries into fixed arrays", detail::demangle<T>().c_str());
  15005. }
  15006. static int at(lua_State* L) {
  15007. return get(L);
  15008. }
  15009. static int get(lua_State* L) {
  15010. T& self = get_src(L);
  15011. std::ptrdiff_t idx = stack::unqualified_get<std::ptrdiff_t>(L, 2);
  15012. idx += deferred_traits::index_adjustment(L, self);
  15013. if (idx >= static_cast<std::ptrdiff_t>(std::extent<T>::value) || idx < 0) {
  15014. return stack::push(L, lua_nil);
  15015. }
  15016. return stack::push_reference(L, detail::deref_non_pointer(self[idx]));
  15017. }
  15018. static int index_get(lua_State* L) {
  15019. return get(L);
  15020. }
  15021. static int set(lua_State* L) {
  15022. T& self = get_src(L);
  15023. std::ptrdiff_t idx = stack::unqualified_get<std::ptrdiff_t>(L, 2);
  15024. idx += deferred_traits::index_adjustment(L, self);
  15025. if (idx >= static_cast<std::ptrdiff_t>(std::extent<T>::value)) {
  15026. return luaL_error(L, "sol: index out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  15027. }
  15028. if (idx < 0) {
  15029. return luaL_error(L, "sol: index out of bounds (too small) for set on '%s'", detail::demangle<T>().c_str());
  15030. }
  15031. self[idx] = stack::unqualified_get<value_type>(L, 3);
  15032. return 0;
  15033. }
  15034. static int index_set(lua_State* L) {
  15035. return set(L);
  15036. }
  15037. static int find(lua_State* L) {
  15038. return find(meta::supports_op_equal<value_type, value_type>(), L);
  15039. }
  15040. static int size(lua_State* L) {
  15041. return stack::push(L, std::extent<T>::value);
  15042. }
  15043. static int empty(lua_State* L) {
  15044. return stack::push(L, std::extent<T>::value > 0);
  15045. }
  15046. static int pairs(lua_State* L) {
  15047. auto& src = get_src(L);
  15048. stack::push(L, next_iter);
  15049. stack::push<user<iter>>(L, src, deferred_traits::begin(L, src));
  15050. stack::push(L, 0);
  15051. return 3;
  15052. }
  15053. static int ipairs(lua_State* L) {
  15054. return pairs(L);
  15055. }
  15056. static int next(lua_State* L) {
  15057. return stack::push(L, next_iter);
  15058. }
  15059. static std::ptrdiff_t index_adjustment(lua_State*, T&) {
  15060. #if defined(SOL_CONTAINERS_START_INDEX)
  15061. return (SOL_CONTAINERS_START) == 0 ? 0 : -(SOL_CONTAINERS_START);
  15062. #else
  15063. return -1;
  15064. #endif
  15065. }
  15066. static iterator begin(lua_State*, T& self) {
  15067. return std::addressof(self[0]);
  15068. }
  15069. static iterator end(lua_State*, T& self) {
  15070. return std::addressof(self[0]) + std::extent<T>::value;
  15071. }
  15072. };
  15073. template <typename X>
  15074. struct container_traits_default<container_traits<X>> : container_traits_default<X> {};
  15075. } // namespace container_detail
  15076. template <typename T>
  15077. struct container_traits : container_detail::container_traits_default<T> {};
  15078. } // namespace sol
  15079. // end of sol/container_traits.hpp
  15080. namespace sol {
  15081. template <typename X>
  15082. struct container_usertype_metatable {
  15083. typedef std::remove_pointer_t<meta::unqualified_t<X>> T;
  15084. typedef container_traits<T> traits;
  15085. typedef container_detail::container_traits_default<T> default_traits;
  15086. static int real_index_get_traits(std::true_type, lua_State* L) {
  15087. return traits::index_get(L);
  15088. }
  15089. static int real_index_get_traits(std::false_type, lua_State* L) {
  15090. return default_traits::index_get(L);
  15091. }
  15092. static int real_index_call(lua_State* L) {
  15093. typedef usertype_detail::map_t<std::string, lua_CFunction> call_map;
  15094. static const call_map calls{
  15095. { "at", &at_call },
  15096. { "get", &real_get_call },
  15097. { "set", &real_set_call },
  15098. { "size", &real_length_call },
  15099. { "add", &real_add_call },
  15100. { "empty", &real_empty_call },
  15101. { "insert", &real_insert_call },
  15102. { "clear", &real_clear_call },
  15103. { "find", &real_find_call },
  15104. { "erase", &real_erase_call },
  15105. { "pairs", &pairs_call },
  15106. { "next", &next_call },
  15107. };
  15108. auto maybenameview = stack::unqualified_check_get<string_view>(L, 2);
  15109. if (maybenameview) {
  15110. const string_view& nameview = *maybenameview;
  15111. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  15112. auto it = calls.find(nameview, string_view_hash(), std::equal_to<string_view>());
  15113. #else
  15114. std::string name(nameview.data(), nameview.size());
  15115. auto it = calls.find(name);
  15116. #endif
  15117. if (it != calls.cend()) {
  15118. return stack::push(L, it->second);
  15119. }
  15120. }
  15121. return real_index_get_traits(container_detail::has_traits_index_get<traits>(), L);
  15122. }
  15123. static int real_at_traits(std::true_type, lua_State* L) {
  15124. return traits::at(L);
  15125. }
  15126. static int real_at_traits(std::false_type, lua_State* L) {
  15127. return default_traits::at(L);
  15128. }
  15129. static int real_at_call(lua_State* L) {
  15130. return real_at_traits(container_detail::has_traits_at<traits>(), L);
  15131. }
  15132. static int real_get_traits(std::true_type, lua_State* L) {
  15133. return traits::get(L);
  15134. }
  15135. static int real_get_traits(std::false_type, lua_State* L) {
  15136. return default_traits::get(L);
  15137. }
  15138. static int real_get_call(lua_State* L) {
  15139. return real_get_traits(container_detail::has_traits_get<traits>(), L);
  15140. }
  15141. static int real_set_traits(std::true_type, lua_State* L) {
  15142. return traits::set(L);
  15143. }
  15144. static int real_set_traits(std::false_type, lua_State* L) {
  15145. return default_traits::set(L);
  15146. }
  15147. static int real_set_call(lua_State* L) {
  15148. return real_set_traits(container_detail::has_traits_set<traits>(), L);
  15149. }
  15150. static int real_index_set_traits(std::true_type, lua_State* L) {
  15151. return traits::index_set(L);
  15152. }
  15153. static int real_index_set_traits(std::false_type, lua_State* L) {
  15154. return default_traits::index_set(L);
  15155. }
  15156. static int real_new_index_call(lua_State* L) {
  15157. return real_index_set_traits(container_detail::has_traits_index_set<traits>(), L);
  15158. }
  15159. static int real_pairs_traits(std::true_type, lua_State* L) {
  15160. return traits::pairs(L);
  15161. }
  15162. static int real_pairs_traits(std::false_type, lua_State* L) {
  15163. return default_traits::pairs(L);
  15164. }
  15165. static int real_pairs_call(lua_State* L) {
  15166. return real_pairs_traits(container_detail::has_traits_pairs<traits>(), L);
  15167. }
  15168. static int real_ipairs_traits(std::true_type, lua_State* L) {
  15169. return traits::ipairs(L);
  15170. }
  15171. static int real_ipairs_traits(std::false_type, lua_State* L) {
  15172. return default_traits::ipairs(L);
  15173. }
  15174. static int real_ipairs_call(lua_State* L) {
  15175. return real_ipairs_traits(container_detail::has_traits_ipairs<traits>(), L);
  15176. }
  15177. static int real_next_traits(std::true_type, lua_State* L) {
  15178. return traits::next(L);
  15179. }
  15180. static int real_next_traits(std::false_type, lua_State* L) {
  15181. return default_traits::next(L);
  15182. }
  15183. static int real_next_call(lua_State* L) {
  15184. return real_next_traits(container_detail::has_traits_next<traits>(), L);
  15185. }
  15186. static int real_size_traits(std::true_type, lua_State* L) {
  15187. return traits::size(L);
  15188. }
  15189. static int real_size_traits(std::false_type, lua_State* L) {
  15190. return default_traits::size(L);
  15191. }
  15192. static int real_length_call(lua_State* L) {
  15193. return real_size_traits(container_detail::has_traits_size<traits>(), L);
  15194. }
  15195. static int real_add_traits(std::true_type, lua_State* L) {
  15196. return traits::add(L);
  15197. }
  15198. static int real_add_traits(std::false_type, lua_State* L) {
  15199. return default_traits::add(L);
  15200. }
  15201. static int real_add_call(lua_State* L) {
  15202. return real_add_traits(container_detail::has_traits_add<traits>(), L);
  15203. }
  15204. static int real_insert_traits(std::true_type, lua_State* L) {
  15205. return traits::insert(L);
  15206. }
  15207. static int real_insert_traits(std::false_type, lua_State* L) {
  15208. return default_traits::insert(L);
  15209. }
  15210. static int real_insert_call(lua_State* L) {
  15211. return real_insert_traits(container_detail::has_traits_insert<traits>(), L);
  15212. }
  15213. static int real_clear_traits(std::true_type, lua_State* L) {
  15214. return traits::clear(L);
  15215. }
  15216. static int real_clear_traits(std::false_type, lua_State* L) {
  15217. return default_traits::clear(L);
  15218. }
  15219. static int real_clear_call(lua_State* L) {
  15220. return real_clear_traits(container_detail::has_traits_clear<traits>(), L);
  15221. }
  15222. static int real_empty_traits(std::true_type, lua_State* L) {
  15223. return traits::empty(L);
  15224. }
  15225. static int real_empty_traits(std::false_type, lua_State* L) {
  15226. return default_traits::empty(L);
  15227. }
  15228. static int real_empty_call(lua_State* L) {
  15229. return real_empty_traits(container_detail::has_traits_empty<traits>(), L);
  15230. }
  15231. static int real_erase_traits(std::true_type, lua_State* L) {
  15232. return traits::erase(L);
  15233. }
  15234. static int real_erase_traits(std::false_type, lua_State* L) {
  15235. return default_traits::erase(L);
  15236. }
  15237. static int real_erase_call(lua_State* L) {
  15238. return real_erase_traits(container_detail::has_traits_erase<traits>(), L);
  15239. }
  15240. static int real_find_traits(std::true_type, lua_State* L) {
  15241. return traits::find(L);
  15242. }
  15243. static int real_find_traits(std::false_type, lua_State* L) {
  15244. return default_traits::find(L);
  15245. }
  15246. static int real_find_call(lua_State* L) {
  15247. return real_find_traits(container_detail::has_traits_find<traits>(), L);
  15248. }
  15249. static int add_call(lua_State* L) {
  15250. return detail::typed_static_trampoline<decltype(&real_add_call), (&real_add_call)>(L);
  15251. }
  15252. static int erase_call(lua_State* L) {
  15253. return detail::typed_static_trampoline<decltype(&real_erase_call), (&real_erase_call)>(L);
  15254. }
  15255. static int insert_call(lua_State* L) {
  15256. return detail::typed_static_trampoline<decltype(&real_insert_call), (&real_insert_call)>(L);
  15257. }
  15258. static int clear_call(lua_State* L) {
  15259. return detail::typed_static_trampoline<decltype(&real_clear_call), (&real_clear_call)>(L);
  15260. }
  15261. static int empty_call(lua_State* L) {
  15262. return detail::typed_static_trampoline<decltype(&real_empty_call), (&real_empty_call)>(L);
  15263. }
  15264. static int find_call(lua_State* L) {
  15265. return detail::typed_static_trampoline<decltype(&real_find_call), (&real_find_call)>(L);
  15266. }
  15267. static int length_call(lua_State* L) {
  15268. return detail::typed_static_trampoline<decltype(&real_length_call), (&real_length_call)>(L);
  15269. }
  15270. static int pairs_call(lua_State* L) {
  15271. return detail::typed_static_trampoline<decltype(&real_pairs_call), (&real_pairs_call)>(L);
  15272. }
  15273. static int ipairs_call(lua_State* L) {
  15274. return detail::typed_static_trampoline<decltype(&real_ipairs_call), (&real_ipairs_call)>(L);
  15275. }
  15276. static int next_call(lua_State* L) {
  15277. return detail::typed_static_trampoline<decltype(&real_next_call), (&real_next_call)>(L);
  15278. }
  15279. static int at_call(lua_State* L) {
  15280. return detail::typed_static_trampoline<decltype(&real_at_call), (&real_at_call)>(L);
  15281. }
  15282. static int get_call(lua_State* L) {
  15283. return detail::typed_static_trampoline<decltype(&real_get_call), (&real_get_call)>(L);
  15284. }
  15285. static int set_call(lua_State* L) {
  15286. return detail::typed_static_trampoline<decltype(&real_set_call), (&real_set_call)>(L);
  15287. }
  15288. static int index_call(lua_State* L) {
  15289. return detail::typed_static_trampoline<decltype(&real_index_call), (&real_index_call)>(L);
  15290. }
  15291. static int new_index_call(lua_State* L) {
  15292. return detail::typed_static_trampoline<decltype(&real_new_index_call), (&real_new_index_call)>(L);
  15293. }
  15294. };
  15295. namespace stack {
  15296. namespace stack_detail {
  15297. template <typename T, bool is_shim = false>
  15298. struct metatable_setup {
  15299. lua_State* L;
  15300. metatable_setup(lua_State* L)
  15301. : L(L) {
  15302. }
  15303. void operator()() {
  15304. typedef container_usertype_metatable<std::conditional_t<is_shim,
  15305. as_container_t<std::remove_pointer_t<T>>,
  15306. std::remove_pointer_t<T>>>
  15307. meta_cumt;
  15308. static const char* metakey = is_shim ? &usertype_traits<as_container_t<std::remove_pointer_t<T>>>::metatable()[0] : &usertype_traits<T>::metatable()[0];
  15309. static const std::array<luaL_Reg, 19> reg = { {
  15310. { "__pairs", &meta_cumt::pairs_call },
  15311. { "__ipairs", &meta_cumt::ipairs_call },
  15312. { "__len", &meta_cumt::length_call },
  15313. { "__index", &meta_cumt::index_call },
  15314. { "__newindex", &meta_cumt::new_index_call },
  15315. { "pairs", &meta_cumt::pairs_call },
  15316. { "next", &meta_cumt::next_call },
  15317. { "at", &meta_cumt::at_call },
  15318. { "get", &meta_cumt::get_call },
  15319. { "set", &meta_cumt::set_call },
  15320. { "size", &meta_cumt::length_call },
  15321. { "empty", &meta_cumt::empty_call },
  15322. { "clear", &meta_cumt::clear_call },
  15323. { "insert", &meta_cumt::insert_call },
  15324. { "add", &meta_cumt::add_call },
  15325. { "find", &meta_cumt::find_call },
  15326. { "erase", &meta_cumt::erase_call },
  15327. std::is_pointer<T>::value ? luaL_Reg{ nullptr, nullptr } : luaL_Reg{ "__gc", &detail::usertype_alloc_destruct<T> },
  15328. { nullptr, nullptr }
  15329. } };
  15330. if (luaL_newmetatable(L, metakey) == 1) {
  15331. luaL_setfuncs(L, reg.data(), 0);
  15332. }
  15333. lua_setmetatable(L, -2);
  15334. }
  15335. };
  15336. } // namespace stack_detail
  15337. template <typename T>
  15338. struct pusher<as_container_t<T>> {
  15339. typedef meta::unqualified_t<T> C;
  15340. static int push_lvalue(std::true_type, lua_State* L, const C& cont) {
  15341. stack_detail::metatable_setup<C*, true> fx(L);
  15342. return pusher<detail::as_pointer_tag<const C>>{}.push_fx(L, fx, detail::ptr(cont));
  15343. }
  15344. static int push_lvalue(std::false_type, lua_State* L, const C& cont) {
  15345. stack_detail::metatable_setup<C, true> fx(L);
  15346. return pusher<detail::as_value_tag<C>>{}.push_fx(L, fx, cont);
  15347. }
  15348. static int push_rvalue(std::true_type, lua_State* L, C&& cont) {
  15349. stack_detail::metatable_setup<C, true> fx(L);
  15350. return pusher<detail::as_value_tag<C>>{}.push_fx(L, fx, std::move(cont));
  15351. }
  15352. static int push_rvalue(std::false_type, lua_State* L, const C& cont) {
  15353. return push_lvalue(std::is_lvalue_reference<T>(), L, cont);
  15354. }
  15355. static int push(lua_State* L, const as_container_t<T>& as_cont) {
  15356. return push_lvalue(std::is_lvalue_reference<T>(), L, as_cont.source);
  15357. }
  15358. static int push(lua_State* L, as_container_t<T>&& as_cont) {
  15359. return push_rvalue(meta::all<std::is_rvalue_reference<T>, meta::neg<std::is_lvalue_reference<T>>>(), L, std::forward<T>(as_cont.source));
  15360. }
  15361. };
  15362. template <typename T>
  15363. struct pusher<as_container_t<T*>> {
  15364. typedef std::add_pointer_t<meta::unqualified_t<std::remove_pointer_t<T>>> C;
  15365. static int push(lua_State* L, T* cont) {
  15366. stack_detail::metatable_setup<C> fx(L);
  15367. return pusher<detail::as_pointer_tag<T>>{}.push_fx(L, fx, cont);
  15368. }
  15369. };
  15370. template <typename T>
  15371. struct pusher<T, std::enable_if_t<meta::all<is_container<meta::unqualified_t<T>>, meta::neg<is_lua_reference<meta::unqualified_t<T>>>>::value>> {
  15372. typedef meta::unqualified_t<T> C;
  15373. static int push(lua_State* L, const T& cont) {
  15374. stack_detail::metatable_setup<C> fx(L);
  15375. return pusher<detail::as_value_tag<T>>{}.push_fx(L, fx, cont);
  15376. }
  15377. static int push(lua_State* L, T&& cont) {
  15378. stack_detail::metatable_setup<C> fx(L);
  15379. return pusher<detail::as_value_tag<T>>{}.push_fx(L, fx, std::move(cont));
  15380. }
  15381. };
  15382. template <typename T>
  15383. struct pusher<T*, std::enable_if_t<meta::all<is_container<meta::unqualified_t<T>>, meta::neg<is_lua_reference<meta::unqualified_t<T>>>>::value>> {
  15384. typedef std::add_pointer_t<meta::unqualified_t<std::remove_pointer_t<T>>> C;
  15385. static int push(lua_State* L, T* cont) {
  15386. stack_detail::metatable_setup<C> fx(L);
  15387. return pusher<detail::as_pointer_tag<T>>{}.push_fx(L, fx, cont);
  15388. }
  15389. };
  15390. template <typename T, typename C>
  15391. struct checker<as_container_t<T>, type::userdata, C> {
  15392. template <typename Handler>
  15393. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  15394. return stack::check<T>(L, index, std::forward<Handler>(handler), tracking);
  15395. }
  15396. };
  15397. template <typename T>
  15398. struct getter<as_container_t<T>> {
  15399. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  15400. return stack::unqualified_get<T>(L, index, tracking);
  15401. }
  15402. };
  15403. template <typename T>
  15404. struct getter<as_container_t<T>*> {
  15405. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  15406. return stack::unqualified_get<T*>(L, index, tracking);
  15407. }
  15408. };
  15409. } // namespace stack
  15410. } // namespace sol
  15411. // end of sol/container_usertype_metatable.hpp
  15412. // beginning of sol/usertype_core.hpp
  15413. #include <sstream>
  15414. namespace sol {
  15415. namespace usertype_detail {
  15416. struct no_comp {
  15417. template <typename A, typename B>
  15418. bool operator()(A&&, B&&) const {
  15419. return false;
  15420. }
  15421. };
  15422. template <typename T>
  15423. int is_check(lua_State* L) {
  15424. return stack::push(L, stack::check<T>(L, 1, &no_panic));
  15425. }
  15426. template <typename T>
  15427. inline int member_default_to_string(std::true_type, lua_State* L) {
  15428. decltype(auto) ts = stack::get<T>(L, 1).to_string();
  15429. return stack::push(L, std::forward<decltype(ts)>(ts));
  15430. }
  15431. template <typename T>
  15432. inline int member_default_to_string(std::false_type, lua_State* L) {
  15433. return luaL_error(L, "cannot perform to_string on '%s': no 'to_string' overload in namespace, 'to_string' member function, or operator<<(ostream&, ...) present", detail::demangle<T>().data());
  15434. }
  15435. template <typename T>
  15436. inline int adl_default_to_string(std::true_type, lua_State* L) {
  15437. using namespace std;
  15438. decltype(auto) ts = to_string(stack::get<T>(L, 1));
  15439. return stack::push(L, std::forward<decltype(ts)>(ts));
  15440. }
  15441. template <typename T>
  15442. inline int adl_default_to_string(std::false_type, lua_State* L) {
  15443. return member_default_to_string<T>(meta::supports_to_string_member<T>(), L);
  15444. }
  15445. template <typename T>
  15446. inline int oss_default_to_string(std::true_type, lua_State* L) {
  15447. std::ostringstream oss;
  15448. oss << stack::unqualified_get<T>(L, 1);
  15449. return stack::push(L, oss.str());
  15450. }
  15451. template <typename T>
  15452. inline int oss_default_to_string(std::false_type, lua_State* L) {
  15453. return adl_default_to_string<T>(meta::supports_adl_to_string<T>(), L);
  15454. }
  15455. template <typename T>
  15456. inline int default_to_string(lua_State* L) {
  15457. return oss_default_to_string<T>(meta::supports_ostream_op<T>(), L);
  15458. }
  15459. template <typename T, typename Op>
  15460. int comparsion_operator_wrap(lua_State* L) {
  15461. auto maybel = stack::unqualified_check_get<T&>(L, 1);
  15462. if (maybel) {
  15463. auto mayber = stack::unqualified_check_get<T&>(L, 2);
  15464. if (mayber) {
  15465. auto& l = *maybel;
  15466. auto& r = *mayber;
  15467. if (std::is_same<no_comp, Op>::value) {
  15468. return stack::push(L, detail::ptr(l) == detail::ptr(r));
  15469. }
  15470. else {
  15471. Op op;
  15472. return stack::push(L, (detail::ptr(l) == detail::ptr(r)) || op(detail::deref(l), detail::deref(r)));
  15473. }
  15474. }
  15475. }
  15476. return stack::push(L, false);
  15477. }
  15478. template <typename T, typename Op, typename Supports, typename Regs, meta::enable<Supports> = meta::enabler>
  15479. inline void make_reg_op(Regs& l, int& index, const char* name) {
  15480. lua_CFunction f = &comparsion_operator_wrap<T, Op>;
  15481. l[index] = luaL_Reg{ name, f };
  15482. ++index;
  15483. }
  15484. template <typename T, typename Op, typename Supports, typename Regs, meta::disable<Supports> = meta::enabler>
  15485. inline void make_reg_op(Regs&, int&, const char*) {
  15486. // Do nothing if there's no support
  15487. }
  15488. template <typename T, typename Supports, typename Regs, meta::enable<Supports> = meta::enabler>
  15489. inline void make_to_string_op(Regs& l, int& index) {
  15490. const char* name = to_string(meta_function::to_string).c_str();
  15491. lua_CFunction f = &detail::static_trampoline<&default_to_string<T>>;
  15492. l[index] = luaL_Reg{ name, f };
  15493. ++index;
  15494. }
  15495. template <typename T, typename Supports, typename Regs, meta::disable<Supports> = meta::enabler>
  15496. inline void make_to_string_op(Regs&, int&) {
  15497. // Do nothing if there's no support
  15498. }
  15499. template <typename T, typename Regs, meta::enable<meta::has_deducible_signature<T>> = meta::enabler>
  15500. inline void make_call_op(Regs& l, int& index) {
  15501. const char* name = to_string(meta_function::call).c_str();
  15502. lua_CFunction f = &c_call<decltype(&T::operator()), &T::operator()>;
  15503. l[index] = luaL_Reg{ name, f };
  15504. ++index;
  15505. }
  15506. template <typename T, typename Regs, meta::disable<meta::has_deducible_signature<T>> = meta::enabler>
  15507. inline void make_call_op(Regs&, int&) {
  15508. // Do nothing if there's no support
  15509. }
  15510. template <typename T, typename Regs>
  15511. inline void make_length_op_const(std::true_type, Regs& l, int& index) {
  15512. const char* name = to_string(meta_function::length).c_str();
  15513. #if defined(__clang__)
  15514. l[index] = luaL_Reg{ name, &c_call<decltype(&T::size), &T::size> };
  15515. #else
  15516. typedef decltype(std::declval<T>().size()) R;
  15517. using sz_func = R(T::*)()const;
  15518. l[index] = luaL_Reg{ name, &c_call<decltype(static_cast<sz_func>(&T::size)), static_cast<sz_func>(&T::size)> };
  15519. #endif
  15520. ++index;
  15521. }
  15522. template <typename T, typename Regs>
  15523. inline void make_length_op_const(std::false_type, Regs& l, int& index) {
  15524. const char* name = to_string(meta_function::length).c_str();
  15525. #if defined(__clang__)
  15526. l[index] = luaL_Reg{ name, &c_call<decltype(&T::size), &T::size> };
  15527. #else
  15528. typedef decltype(std::declval<T>().size()) R;
  15529. using sz_func = R(T::*)();
  15530. l[index] = luaL_Reg{ name, &c_call<decltype(static_cast<sz_func>(&T::size)), static_cast<sz_func>(&T::size)> };
  15531. #endif
  15532. ++index;
  15533. }
  15534. template <typename T, typename Regs, meta::enable<meta::has_size<T>, meta::has_size<const T>> = meta::enabler>
  15535. inline void make_length_op(Regs& l, int& index) {
  15536. make_length_op_const<T>(meta::has_size<const T>(), l, index);
  15537. }
  15538. template <typename T, typename Regs, meta::disable<meta::has_size<T>, meta::has_size<const T>> = meta::enabler>
  15539. inline void make_length_op(Regs&, int&) {
  15540. // Do nothing if there's no support
  15541. }
  15542. template <typename T, typename Regs, meta::enable<meta::neg<std::is_pointer<T>>, std::is_destructible<T>>>
  15543. void make_destructor(Regs& l, int& index) {
  15544. const char* name = to_string(meta_function::garbage_collect).c_str();
  15545. l[index] = luaL_Reg{ name, is_unique_usertype<T>::value ? &detail::unique_destruct<T> : &detail::usertype_alloc_destruct<T> };
  15546. ++index;
  15547. }
  15548. template <typename T, typename Regs, meta::disable<meta::neg<std::is_pointer<T>>, std::is_destructible<T>>>
  15549. void make_destructor(Regs& l, int& index) {
  15550. if (!std::is_destructible<T>::value) {
  15551. // if the value is not destructible, plant an erroring __gc method
  15552. // to warn the user of a problem when it comes around
  15553. // this won't trigger if the user performs `new_usertype` / `new_simple_usertype` and
  15554. // rigs the class up properly
  15555. const char* name = to_string(meta_function::garbage_collect).c_str();
  15556. l[index] = luaL_Reg{ name, &detail::cannot_destruct<T> };
  15557. ++index;
  15558. }
  15559. }
  15560. template <typename T, typename Regs, typename Fx>
  15561. void insert_default_registrations(std::false_type, Regs&, int&, Fx&&) {
  15562. // no-op
  15563. }
  15564. template <typename T, typename Regs, typename Fx>
  15565. void insert_default_registrations(std::true_type, Regs& l, int& index, Fx&& fx) {
  15566. if (fx(meta_function::less_than)) {
  15567. const char* name = to_string(meta_function::less_than).c_str();
  15568. usertype_detail::make_reg_op<T, std::less<>, meta::supports_op_less<T>>(l, index, name);
  15569. }
  15570. if (fx(meta_function::less_than_or_equal_to)) {
  15571. const char* name = to_string(meta_function::less_than_or_equal_to).c_str();
  15572. usertype_detail::make_reg_op<T, std::less_equal<>, meta::supports_op_less_equal<T>>(l, index, name);
  15573. }
  15574. if (fx(meta_function::equal_to)) {
  15575. const char* name = to_string(meta_function::equal_to).c_str();
  15576. usertype_detail::make_reg_op<T, std::conditional_t<meta::supports_op_equal<T>::value, std::equal_to<>, usertype_detail::no_comp>, std::true_type>(l, index, name);
  15577. }
  15578. if (fx(meta_function::pairs)) {
  15579. const char* name = to_string(meta_function::pairs).c_str();
  15580. l[index] = luaL_Reg{ name, container_usertype_metatable<as_container_t<T>>::pairs_call };
  15581. ++index;
  15582. }
  15583. if (fx(meta_function::length)) {
  15584. usertype_detail::make_length_op<T>(l, index);
  15585. }
  15586. if (fx(meta_function::to_string)) {
  15587. usertype_detail::make_to_string_op<T, is_to_stringable<T>>(l, index);
  15588. }
  15589. if (fx(meta_function::call_function)) {
  15590. usertype_detail::make_call_op<T>(l, index);
  15591. }
  15592. }
  15593. template <typename T, typename Regs, typename Fx>
  15594. void insert_default_registrations(Regs& l, int& index, Fx&& fx) {
  15595. insert_default_registrations<T>(is_automagical<T>(), l, index, std::forward<Fx>(fx));
  15596. }
  15597. } // namespace usertype_detail
  15598. namespace stack { namespace stack_detail {
  15599. template <typename T>
  15600. struct undefined_metatable {
  15601. typedef meta::all<meta::neg<std::is_pointer<T>>, std::is_destructible<T>> is_destructible;
  15602. typedef std::remove_pointer_t<T> P;
  15603. lua_State* L;
  15604. const char* key;
  15605. undefined_metatable(lua_State* l, const char* k)
  15606. : L(l), key(k) {
  15607. }
  15608. void operator()() const {
  15609. if (luaL_newmetatable(L, key) == 1) {
  15610. luaL_Reg l[32]{};
  15611. int index = 0;
  15612. auto fx = [](meta_function) { return true; };
  15613. usertype_detail::insert_default_registrations<P>(l, index, fx);
  15614. usertype_detail::make_destructor<T>(l, index);
  15615. luaL_setfuncs(L, l, 0);
  15616. // __type table
  15617. lua_createtable(L, 0, 2);
  15618. const std::string& name = detail::demangle<T>();
  15619. lua_pushlstring(L, name.c_str(), name.size());
  15620. lua_setfield(L, -2, "name");
  15621. lua_CFunction is_func = &usertype_detail::is_check<T>;
  15622. lua_pushcclosure(L, is_func, 0);
  15623. lua_setfield(L, -2, "is");
  15624. lua_setfield(L, -2, to_string(meta_function::type).c_str());
  15625. }
  15626. lua_setmetatable(L, -2);
  15627. }
  15628. };
  15629. }
  15630. } // namespace stack::stack_detail
  15631. } // namespace sol
  15632. // end of sol/usertype_core.hpp
  15633. #include <cstdio>
  15634. #include <bitset>
  15635. namespace sol {
  15636. struct usertype_metatable_core;
  15637. namespace usertype_detail {
  15638. const int metatable_index = 2;
  15639. const int metatable_core_index = 3;
  15640. const int filler_index = 4;
  15641. const int magic_index = 5;
  15642. const int simple_metatable_index = 2;
  15643. const int index_function_index = 3;
  15644. const int newindex_function_index = 4;
  15645. typedef void (*base_walk)(lua_State*, bool&, int&, string_view&);
  15646. typedef int (*member_search)(lua_State*, void*, usertype_metatable_core&, int);
  15647. struct call_information {
  15648. member_search index;
  15649. member_search new_index;
  15650. int runtime_target;
  15651. call_information(member_search index, member_search newindex)
  15652. : call_information(index, newindex, -1) {
  15653. }
  15654. call_information(member_search index, member_search newindex, int runtimetarget)
  15655. : index(index), new_index(newindex), runtime_target(runtimetarget) {
  15656. }
  15657. };
  15658. typedef map_t<std::string, call_information> mapping_t;
  15659. struct variable_wrapper {
  15660. virtual int index(lua_State* L) = 0;
  15661. virtual int new_index(lua_State* L) = 0;
  15662. virtual ~variable_wrapper(){};
  15663. };
  15664. template <typename T, typename F>
  15665. struct callable_binding : variable_wrapper {
  15666. F fx;
  15667. template <typename Arg>
  15668. callable_binding(Arg&& arg)
  15669. : fx(std::forward<Arg>(arg)) {
  15670. }
  15671. virtual int index(lua_State* L) override {
  15672. return call_detail::call_wrapped<T, true, true>(L, fx);
  15673. }
  15674. virtual int new_index(lua_State* L) override {
  15675. return call_detail::call_wrapped<T, false, true>(L, fx);
  15676. }
  15677. };
  15678. typedef map_t<std::string, std::unique_ptr<variable_wrapper>> variable_map;
  15679. typedef map_t<std::string, object> function_map;
  15680. struct simple_map {
  15681. const char* metakey;
  15682. variable_map variables;
  15683. function_map functions;
  15684. object index;
  15685. object newindex;
  15686. base_walk indexbaseclasspropogation;
  15687. base_walk newindexbaseclasspropogation;
  15688. simple_map(const char* mkey, base_walk index, base_walk newindex, object i, object ni, variable_map&& vars, function_map&& funcs)
  15689. : metakey(mkey), variables(std::move(vars)), functions(std::move(funcs)), index(std::move(i)), newindex(std::move(ni)), indexbaseclasspropogation(index), newindexbaseclasspropogation(newindex) {
  15690. }
  15691. };
  15692. } // namespace usertype_detail
  15693. struct usertype_metatable_core {
  15694. usertype_detail::mapping_t mapping;
  15695. lua_CFunction indexfunc;
  15696. lua_CFunction newindexfunc;
  15697. std::vector<object> runtime;
  15698. bool mustindex;
  15699. usertype_metatable_core(lua_CFunction ifx, lua_CFunction nifx)
  15700. : mapping(), indexfunc(ifx), newindexfunc(nifx), runtime(), mustindex(false) {
  15701. }
  15702. usertype_metatable_core(const usertype_metatable_core&) = default;
  15703. usertype_metatable_core(usertype_metatable_core&&) = default;
  15704. usertype_metatable_core& operator=(const usertype_metatable_core&) = default;
  15705. usertype_metatable_core& operator=(usertype_metatable_core&&) = default;
  15706. };
  15707. namespace usertype_detail {
  15708. const lua_Integer toplevel_magic = static_cast<lua_Integer>(0xCCC2CCC1);
  15709. inline int is_indexer(string_view s) {
  15710. if (s == to_string(meta_function::index)) {
  15711. return 1;
  15712. }
  15713. else if (s == to_string(meta_function::new_index)) {
  15714. return 2;
  15715. }
  15716. return 0;
  15717. }
  15718. inline int is_indexer(meta_function mf) {
  15719. if (mf == meta_function::index) {
  15720. return 1;
  15721. }
  15722. else if (mf == meta_function::new_index) {
  15723. return 2;
  15724. }
  15725. return 0;
  15726. }
  15727. inline int is_indexer(call_construction) {
  15728. return 0;
  15729. }
  15730. inline int is_indexer(base_classes_tag) {
  15731. return 0;
  15732. }
  15733. inline auto make_string_view(string_view s) {
  15734. return s;
  15735. }
  15736. inline auto make_string_view(call_construction) {
  15737. return string_view(to_string(meta_function::call_function));
  15738. }
  15739. inline auto make_string_view(meta_function mf) {
  15740. return string_view(to_string(mf));
  15741. }
  15742. inline auto make_string_view(base_classes_tag) {
  15743. return string_view(detail::base_class_cast_key());
  15744. }
  15745. template <typename Arg>
  15746. inline std::string make_string(Arg&& arg) {
  15747. string_view s = make_string_view(arg);
  15748. return std::string(s.data(), s.size());
  15749. }
  15750. template <typename N>
  15751. inline luaL_Reg make_reg(N&& n, lua_CFunction f) {
  15752. luaL_Reg l{make_string_view(std::forward<N>(n)).data(), f};
  15753. return l;
  15754. }
  15755. struct registrar {
  15756. registrar() = default;
  15757. registrar(const registrar&) = default;
  15758. registrar(registrar&&) = default;
  15759. registrar& operator=(const registrar&) = default;
  15760. registrar& operator=(registrar&&) = default;
  15761. virtual int push_um(lua_State* L) = 0;
  15762. virtual ~registrar() {
  15763. }
  15764. };
  15765. inline bool is_toplevel(lua_State* L, int index = magic_index) {
  15766. int isnum = 0;
  15767. lua_Integer magic = lua_tointegerx(L, upvalue_index(index), &isnum);
  15768. return isnum != 0 && magic == toplevel_magic;
  15769. }
  15770. inline int runtime_object_call(lua_State* L, void*, usertype_metatable_core& umc, int runtimetarget) {
  15771. std::vector<object>& runtime = umc.runtime;
  15772. object& runtimeobj = runtime[runtimetarget];
  15773. return stack::push(L, runtimeobj);
  15774. }
  15775. template <typename T, bool is_index>
  15776. inline int indexing_fail(lua_State* L) {
  15777. if (is_index) {
  15778. #if 0 //defined(SOL_SAFE_USERTYPE) && SOL_SAFE_USERTYPE
  15779. auto maybeaccessor = stack::get<optional<string_view>>(L, is_index ? -1 : -2);
  15780. string_view accessor = maybeaccessor.value_or(string_detail::string_shim("(unknown)"));
  15781. return luaL_error(L, "sol: attempt to index (get) nil value \"%s\" on userdata (bad (misspelled?) key name or does not exist)", accessor.data());
  15782. #else
  15783. if (is_toplevel(L)) {
  15784. if (lua_getmetatable(L, 1) == 1) {
  15785. int metatarget = lua_gettop(L);
  15786. stack::get_field(L, stack_reference(L, raw_index(2)), metatarget);
  15787. return 1;
  15788. }
  15789. }
  15790. // With runtime extensibility, we can't hard-error things. They have to return nil, like regular table types, unfortunately...
  15791. return stack::push(L, lua_nil);
  15792. #endif
  15793. }
  15794. else {
  15795. auto maybeaccessor = stack::get<optional<string_view>>(L, is_index ? -1 : -2);
  15796. string_view accessor = maybeaccessor.value_or(string_view("(unknown)"));
  15797. return luaL_error(L, "sol: attempt to index (set) nil value \"%s\" on userdata (bad (misspelled?) key name or does not exist)", accessor.data());
  15798. }
  15799. }
  15800. int runtime_new_index(lua_State* L, void*, usertype_metatable_core&, int runtimetarget);
  15801. template <typename T, bool is_simple>
  15802. inline int metatable_new_index(lua_State* L) {
  15803. if (is_toplevel(L)) {
  15804. auto non_indexable = [&L]() {
  15805. if (is_simple) {
  15806. simple_map& sm = stack::get<user<simple_map>>(L, upvalue_index(simple_metatable_index));
  15807. function_map& functions = sm.functions;
  15808. optional<string_view> maybeaccessor = stack::get<optional<string_view>>(L, 2);
  15809. if (!maybeaccessor) {
  15810. return;
  15811. }
  15812. string_view& accessor_view = maybeaccessor.value();
  15813. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  15814. auto preexistingit = functions.find(accessor_view, string_view_hash(), std::equal_to<string_view>());
  15815. #else
  15816. std::string accessor(accessor_view.data(), accessor_view.size());
  15817. auto preexistingit = functions.find(accessor);
  15818. #endif
  15819. if (preexistingit == functions.cend()) {
  15820. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  15821. std::string accessor(accessor_view.data(), accessor_view.size());
  15822. #endif
  15823. functions.emplace_hint(preexistingit, std::move(accessor), object(L, 3));
  15824. }
  15825. else {
  15826. preexistingit->second = object(L, 3);
  15827. }
  15828. return;
  15829. }
  15830. usertype_metatable_core& umc = stack::get<light<usertype_metatable_core>>(L, upvalue_index(metatable_core_index));
  15831. bool mustindex = umc.mustindex;
  15832. if (!mustindex)
  15833. return;
  15834. optional<string_view> maybeaccessor = stack::get<optional<string_view>>(L, 2);
  15835. if (!maybeaccessor) {
  15836. return;
  15837. }
  15838. string_view& accessor_view = maybeaccessor.value();
  15839. mapping_t& mapping = umc.mapping;
  15840. std::vector<object>& runtime = umc.runtime;
  15841. int target = static_cast<int>(runtime.size());
  15842. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  15843. auto preexistingit = mapping.find(accessor_view, string_view_hash(), std::equal_to<string_view>());
  15844. #else
  15845. std::string accessor(accessor_view.data(), accessor_view.size());
  15846. auto preexistingit = mapping.find(accessor);
  15847. #endif
  15848. if (preexistingit == mapping.cend()) {
  15849. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  15850. std::string accessor(accessor_view.data(), accessor_view.size());
  15851. #endif
  15852. runtime.emplace_back(L, 3);
  15853. mapping.emplace_hint(mapping.cend(), std::move(accessor), call_information(&runtime_object_call, &runtime_new_index, target));
  15854. }
  15855. else {
  15856. target = preexistingit->second.runtime_target;
  15857. runtime[target] = object(L, 3);
  15858. preexistingit->second = call_information(&runtime_object_call, &runtime_new_index, target);
  15859. }
  15860. };
  15861. non_indexable();
  15862. for (std::size_t i = 0; i < 4; lua_settop(L, 3), ++i) {
  15863. const char* metakey = nullptr;
  15864. switch (i) {
  15865. case 0:
  15866. metakey = &usertype_traits<T*>::metatable()[0];
  15867. luaL_getmetatable(L, metakey);
  15868. break;
  15869. case 1:
  15870. metakey = &usertype_traits<detail::unique_usertype<T>>::metatable()[0];
  15871. luaL_getmetatable(L, metakey);
  15872. break;
  15873. case 2:
  15874. metakey = &usertype_traits<T>::metatable()[0];
  15875. luaL_getmetatable(L, metakey);
  15876. break;
  15877. case 3:
  15878. default:
  15879. metakey = &usertype_traits<T>::user_metatable()[0];
  15880. {
  15881. luaL_getmetatable(L, metakey);
  15882. lua_getmetatable(L, -1);
  15883. }
  15884. break;
  15885. }
  15886. int tableindex = lua_gettop(L);
  15887. if (type_of(L, tableindex) == type::lua_nil) {
  15888. continue;
  15889. }
  15890. stack::set_field<false, true>(L, stack_reference(L, raw_index(2)), stack_reference(L, raw_index(3)), tableindex);
  15891. }
  15892. lua_settop(L, 0);
  15893. return 0;
  15894. }
  15895. return indexing_fail<T, false>(L);
  15896. }
  15897. inline int runtime_new_index(lua_State* L, void*, usertype_metatable_core& umc, int runtimetarget) {
  15898. std::vector<object>& runtime = umc.runtime;
  15899. object& runtimeobj = runtime[runtimetarget];
  15900. runtimeobj = object(L, 3);
  15901. return 0;
  15902. }
  15903. template <bool is_index, typename Base>
  15904. static void walk_single_base(lua_State* L, bool& found, int& ret, string_view&) {
  15905. if (found)
  15906. return;
  15907. const char* metakey = &usertype_traits<Base>::metatable()[0];
  15908. const char* gcmetakey = &usertype_traits<Base>::gc_table()[0];
  15909. const char* basewalkkey = is_index ? detail::base_class_index_propogation_key() : detail::base_class_new_index_propogation_key();
  15910. luaL_getmetatable(L, metakey);
  15911. if (type_of(L, -1) == type::lua_nil) {
  15912. lua_pop(L, 1);
  15913. return;
  15914. }
  15915. stack::get_field(L, basewalkkey);
  15916. if (type_of(L, -1) == type::lua_nil) {
  15917. lua_pop(L, 2);
  15918. return;
  15919. }
  15920. lua_CFunction basewalkfunc = stack::pop<lua_CFunction>(L);
  15921. lua_pop(L, 1);
  15922. stack::get_field<true>(L, gcmetakey);
  15923. int value = basewalkfunc(L);
  15924. if (value > -1) {
  15925. found = true;
  15926. ret = value;
  15927. }
  15928. }
  15929. template <bool is_index, typename... Bases>
  15930. static void walk_all_bases(lua_State* L, bool& found, int& ret, string_view& accessor) {
  15931. (void)L;
  15932. (void)found;
  15933. (void)ret;
  15934. (void)accessor;
  15935. (void)detail::swallow{0, (walk_single_base<is_index, Bases>(L, found, ret, accessor), 0)...};
  15936. }
  15937. } // namespace usertype_detail
  15938. template <typename T>
  15939. struct clean_type {
  15940. typedef std::conditional_t<std::is_array<meta::unqualified_t<T>>::value, T&, std::decay_t<T>> type;
  15941. };
  15942. template <typename T>
  15943. using clean_type_t = typename clean_type<T>::type;
  15944. template <typename T, typename IndexSequence, typename... Tn>
  15945. struct usertype_metatable : usertype_detail::registrar {};
  15946. template <typename T, std::size_t... I, typename... Tn>
  15947. struct usertype_metatable<T, std::index_sequence<I...>, Tn...> : usertype_metatable_core, usertype_detail::registrar {
  15948. typedef std::make_index_sequence<sizeof...(I) * 2> indices;
  15949. typedef std::index_sequence<I...> half_indices;
  15950. typedef std::array<luaL_Reg, sizeof...(Tn) / 2 + 1 + 31> regs_t;
  15951. typedef std::tuple<Tn...> RawTuple;
  15952. typedef std::tuple<clean_type_t<Tn>...> Tuple;
  15953. template <std::size_t Idx>
  15954. struct check_binding : is_variable_binding<meta::unqualified_tuple_element_t<Idx, Tuple>> {};
  15955. Tuple functions;
  15956. lua_CFunction destructfunc;
  15957. lua_CFunction callconstructfunc;
  15958. lua_CFunction indexbase;
  15959. lua_CFunction newindexbase;
  15960. usertype_detail::base_walk indexbaseclasspropogation;
  15961. usertype_detail::base_walk newindexbaseclasspropogation;
  15962. void* baseclasscheck;
  15963. void* baseclasscast;
  15964. bool secondarymeta;
  15965. std::bitset<32> properties;
  15966. template <std::size_t Idx, meta::enable<std::is_same<lua_CFunction, meta::unqualified_tuple_element<Idx + 1, RawTuple>>> = meta::enabler>
  15967. lua_CFunction make_func() const {
  15968. return std::get<Idx + 1>(functions);
  15969. }
  15970. template <std::size_t Idx, meta::disable<std::is_same<lua_CFunction, meta::unqualified_tuple_element<Idx + 1, RawTuple>>> = meta::enabler>
  15971. lua_CFunction make_func() const {
  15972. const auto& name = std::get<Idx>(functions);
  15973. return (usertype_detail::make_string_view(name) == "__newindex") ? &call<Idx + 1, false> : &call<Idx + 1, true>;
  15974. }
  15975. static bool contains_variable() {
  15976. typedef meta::any<check_binding<(I * 2 + 1)>...> has_variables;
  15977. return has_variables::value;
  15978. }
  15979. bool contains_index() const {
  15980. bool idx = false;
  15981. (void)detail::swallow{0, ((idx |= (usertype_detail::is_indexer(std::get<I * 2>(functions)) != 0)), 0)...};
  15982. return idx;
  15983. }
  15984. int finish_regs(regs_t& l, int& index) {
  15985. auto prop_fx = [&](meta_function mf) { return !properties[static_cast<int>(mf)]; };
  15986. usertype_detail::insert_default_registrations<T>(l, index, prop_fx);
  15987. if (destructfunc != nullptr) {
  15988. l[index] = luaL_Reg{to_string(meta_function::garbage_collect).c_str(), destructfunc};
  15989. ++index;
  15990. }
  15991. return index;
  15992. }
  15993. template <std::size_t Idx, typename F>
  15994. void make_regs(regs_t&, int&, call_construction, F&&) {
  15995. callconstructfunc = call<Idx + 1>;
  15996. secondarymeta = true;
  15997. }
  15998. template <std::size_t, typename... Bases>
  15999. void make_regs(regs_t&, int&, base_classes_tag, bases<Bases...>) {
  16000. static_assert(!meta::any_same<T, Bases...>::value, "base classes cannot list the original class as part of the bases");
  16001. if (sizeof...(Bases) < 1) {
  16002. return;
  16003. }
  16004. mustindex = true;
  16005. (void)detail::swallow{0, ((detail::has_derived<Bases>::value = true), 0)...};
  16006. static_assert(sizeof(void*) <= sizeof(detail::inheritance_check_function), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report.");
  16007. static_assert(sizeof(void*) <= sizeof(detail::inheritance_cast_function), "The size of this data pointer is too small to fit the inheritance checking function: file a bug report.");
  16008. baseclasscheck = (void*)&detail::inheritance<T, Bases...>::type_check;
  16009. baseclasscast = (void*)&detail::inheritance<T, Bases...>::type_cast;
  16010. indexbaseclasspropogation = usertype_detail::walk_all_bases<true, Bases...>;
  16011. newindexbaseclasspropogation = usertype_detail::walk_all_bases<false, Bases...>;
  16012. }
  16013. template <std::size_t Idx, typename N, typename F, typename = std::enable_if_t<!meta::any_same<meta::unqualified_t<N>, base_classes_tag, call_construction>::value>>
  16014. void make_regs(regs_t& l, int& index, N&& n, F&&) {
  16015. if (is_variable_binding<meta::unqualified_t<F>>::value) {
  16016. return;
  16017. }
  16018. luaL_Reg reg = usertype_detail::make_reg(std::forward<N>(n), make_func<Idx>());
  16019. for (std::size_t i = 0; i < properties.size(); ++i) {
  16020. meta_function mf = static_cast<meta_function>(i);
  16021. const std::string& mfname = to_string(mf);
  16022. if (mfname == reg.name) {
  16023. switch (mf) {
  16024. case meta_function::construct:
  16025. if (properties[i]) {
  16026. #if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS)
  16027. throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16028. #else
  16029. assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16030. #endif
  16031. }
  16032. break;
  16033. case meta_function::garbage_collect:
  16034. if (destructfunc != nullptr) {
  16035. #if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS)
  16036. throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16037. #else
  16038. assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16039. #endif
  16040. }
  16041. destructfunc = reg.func;
  16042. return;
  16043. case meta_function::index:
  16044. indexfunc = reg.func;
  16045. mustindex = true;
  16046. properties.set(i);
  16047. return;
  16048. case meta_function::new_index:
  16049. newindexfunc = reg.func;
  16050. mustindex = true;
  16051. properties.set(i);
  16052. return;
  16053. default:
  16054. break;
  16055. }
  16056. properties.set(i);
  16057. break;
  16058. }
  16059. }
  16060. l[index] = reg;
  16061. ++index;
  16062. }
  16063. template <typename... Args, typename = std::enable_if_t<sizeof...(Args) == sizeof...(Tn)>>
  16064. usertype_metatable(Args&&... args)
  16065. : usertype_metatable_core(&usertype_detail::indexing_fail<T, true>, &usertype_detail::metatable_new_index<T, false>), usertype_detail::registrar(), functions(std::forward<Args>(args)...), destructfunc(nullptr), callconstructfunc(nullptr), indexbase(&core_indexing_call<true>), newindexbase(&core_indexing_call<false>), indexbaseclasspropogation(usertype_detail::walk_all_bases<true>), newindexbaseclasspropogation(usertype_detail::walk_all_bases<false>), baseclasscheck(nullptr), baseclasscast(nullptr), secondarymeta(contains_variable()), properties() {
  16066. properties.reset();
  16067. std::initializer_list<typename usertype_detail::mapping_t::value_type> ilist{{std::pair<std::string, usertype_detail::call_information>(usertype_detail::make_string(std::get<I * 2>(functions)),
  16068. usertype_detail::call_information(&usertype_metatable::real_find_call<I * 2, I * 2 + 1, true>,
  16069. &usertype_metatable::real_find_call<I * 2, I * 2 + 1, false>))}...};
  16070. this->mapping.insert(ilist);
  16071. for (const auto& n : meta_function_names()) {
  16072. this->mapping.erase(n);
  16073. }
  16074. this->mustindex = contains_variable() || contains_index();
  16075. }
  16076. usertype_metatable(const usertype_metatable&) = default;
  16077. usertype_metatable(usertype_metatable&&) = default;
  16078. usertype_metatable& operator=(const usertype_metatable&) = default;
  16079. usertype_metatable& operator=(usertype_metatable&&) = default;
  16080. template <std::size_t I0, std::size_t I1, bool is_index>
  16081. static int real_find_call(lua_State* L, void* um, usertype_metatable_core&, int) {
  16082. auto& f = *static_cast<usertype_metatable*>(um);
  16083. if (is_variable_binding<decltype(std::get<I1>(f.functions))>::value) {
  16084. return real_call_with<I1, is_index, true>(L, f);
  16085. }
  16086. // set up upvalues
  16087. // for a chained call
  16088. int upvalues = 0;
  16089. upvalues += stack::push(L, nullptr);
  16090. upvalues += stack::push(L, light<usertype_metatable>(f));
  16091. auto cfunc = &call<I1, is_index>;
  16092. return stack::push(L, c_closure(cfunc, upvalues));
  16093. }
  16094. template <bool is_index>
  16095. static int real_meta_call(lua_State* L, void* um, int) {
  16096. auto& f = *static_cast<usertype_metatable*>(um);
  16097. return is_index ? f.indexfunc(L) : f.newindexfunc(L);
  16098. }
  16099. template <bool is_index, bool toplevel = false, bool is_meta_bound = false>
  16100. static int core_indexing_call(lua_State* L) {
  16101. usertype_metatable& f = toplevel
  16102. ? static_cast<usertype_metatable&>(stack::get<light<usertype_metatable>>(L, upvalue_index(usertype_detail::metatable_index)))
  16103. : static_cast<usertype_metatable&>(stack::pop<user<usertype_metatable>>(L));
  16104. static const int keyidx = -2 + static_cast<int>(is_index);
  16105. if (toplevel && stack::get<type>(L, keyidx) != type::string) {
  16106. return is_index ? f.indexfunc(L) : f.newindexfunc(L);
  16107. }
  16108. int runtime_target = 0;
  16109. usertype_detail::member_search member = nullptr;
  16110. {
  16111. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  16112. string_view name = stack::get<string_view>(L, keyidx);
  16113. auto memberit = f.mapping.find(name, string_view_hash(), std::equal_to<string_view>());
  16114. #else
  16115. std::string name = stack::get<std::string>(L, keyidx);
  16116. auto memberit = f.mapping.find(name);
  16117. #endif
  16118. if (memberit != f.mapping.cend()) {
  16119. const usertype_detail::call_information& ci = memberit->second;
  16120. member = is_index ? ci.index : ci.new_index;
  16121. runtime_target = ci.runtime_target;
  16122. }
  16123. }
  16124. if (member != nullptr) {
  16125. return (member)(L, static_cast<void*>(&f), static_cast<usertype_metatable_core&>(f), runtime_target);
  16126. }
  16127. if (is_meta_bound && toplevel && !is_index) {
  16128. return usertype_detail::metatable_new_index<T, false>(L);
  16129. }
  16130. string_view accessor = stack::get<string_view>(L, keyidx);
  16131. int ret = 0;
  16132. bool found = false;
  16133. // Otherwise, we need to do propagating calls through the bases
  16134. if (is_index)
  16135. f.indexbaseclasspropogation(L, found, ret, accessor);
  16136. else
  16137. f.newindexbaseclasspropogation(L, found, ret, accessor);
  16138. if (found) {
  16139. return ret;
  16140. }
  16141. if (is_meta_bound) {
  16142. return is_index ? usertype_detail::indexing_fail<T, is_index>(L) : usertype_detail::metatable_new_index<T, false>(L);
  16143. }
  16144. return toplevel ? (is_index ? f.indexfunc(L) : f.newindexfunc(L)) : -1;
  16145. }
  16146. static int real_index_call(lua_State* L) {
  16147. return core_indexing_call<true, true>(L);
  16148. }
  16149. static int real_new_index_call(lua_State* L) {
  16150. return core_indexing_call<false, true>(L);
  16151. }
  16152. static int real_meta_index_call(lua_State* L) {
  16153. return core_indexing_call<true, true, true>(L);
  16154. }
  16155. static int real_meta_new_index_call(lua_State* L) {
  16156. return core_indexing_call<false, true, true>(L);
  16157. }
  16158. template <std::size_t Idx, bool is_index = true, bool is_variable = false>
  16159. static int real_call(lua_State* L) {
  16160. usertype_metatable& f = stack::get<light<usertype_metatable>>(L, upvalue_index(usertype_detail::metatable_index));
  16161. return real_call_with<Idx, is_index, is_variable>(L, f);
  16162. }
  16163. template <std::size_t Idx, bool is_index = true, bool is_variable = false>
  16164. static int real_call_with(lua_State* L, usertype_metatable& um) {
  16165. typedef meta::unqualified_tuple_element_t<Idx - 1, Tuple> K;
  16166. typedef meta::unqualified_tuple_element_t<Idx, Tuple> F;
  16167. static const int boost = !detail::is_non_factory_constructor<F>::value
  16168. && std::is_same<K, call_construction>::value
  16169. ? 1
  16170. : 0;
  16171. auto& f = std::get<Idx>(um.functions);
  16172. return call_detail::call_wrapped<T, is_index, is_variable, boost>(L, f);
  16173. }
  16174. template <std::size_t Idx, bool is_index = true, bool is_variable = false>
  16175. static int call(lua_State* L) {
  16176. return detail::typed_static_trampoline<decltype(&real_call<Idx, is_index, is_variable>), (&real_call<Idx, is_index, is_variable>)>(L);
  16177. }
  16178. template <std::size_t Idx, bool is_index = true, bool is_variable = false>
  16179. static int call_with(lua_State* L) {
  16180. return detail::typed_static_trampoline<decltype(&real_call_with<Idx, is_index, is_variable>), (&real_call_with<Idx, is_index, is_variable>)>(L);
  16181. }
  16182. static int index_call(lua_State* L) {
  16183. return detail::typed_static_trampoline<decltype(&real_index_call), (&real_index_call)>(L);
  16184. }
  16185. static int new_index_call(lua_State* L) {
  16186. return detail::typed_static_trampoline<decltype(&real_new_index_call), (&real_new_index_call)>(L);
  16187. }
  16188. static int meta_index_call(lua_State* L) {
  16189. return detail::typed_static_trampoline<decltype(&real_meta_index_call), (&real_meta_index_call)>(L);
  16190. }
  16191. static int meta_new_index_call(lua_State* L) {
  16192. return detail::typed_static_trampoline<decltype(&real_meta_new_index_call), (&real_meta_new_index_call)>(L);
  16193. }
  16194. virtual int push_um(lua_State* L) override {
  16195. return stack::push(L, std::move(*this));
  16196. }
  16197. ~usertype_metatable() override {
  16198. }
  16199. };
  16200. namespace stack {
  16201. template <typename T, std::size_t... I, typename... Args>
  16202. struct pusher<usertype_metatable<T, std::index_sequence<I...>, Args...>> {
  16203. typedef usertype_metatable<T, std::index_sequence<I...>, Args...> umt_t;
  16204. typedef typename umt_t::regs_t regs_t;
  16205. static umt_t& make_cleanup(lua_State* L, umt_t&& umx) {
  16206. // ensure some sort of uniqueness
  16207. static int uniqueness = 0;
  16208. std::string uniquegcmetakey = usertype_traits<T>::user_gc_metatable();
  16209. // std::to_string doesn't exist in android still, with NDK, so this bullshit
  16210. // is necessary
  16211. // thanks, Android :v
  16212. int appended = snprintf(nullptr, 0, "%d", uniqueness);
  16213. std::size_t insertionpoint = uniquegcmetakey.length() - 1;
  16214. uniquegcmetakey.append(appended, '\0');
  16215. char* uniquetarget = &uniquegcmetakey[insertionpoint];
  16216. snprintf(uniquetarget, uniquegcmetakey.length(), "%d", uniqueness);
  16217. ++uniqueness;
  16218. const char* gcmetakey = &usertype_traits<T>::gc_table()[0];
  16219. // Make sure userdata's memory is properly in lua first,
  16220. // otherwise all the light userdata we make later will become invalid
  16221. stack::push<user<umt_t>>(L, metatable_key, uniquegcmetakey, std::move(umx));
  16222. // Create the top level thing that will act as our deleter later on
  16223. stack_reference umt(L, -1);
  16224. stack::set_field<true>(L, gcmetakey, umt);
  16225. umt.pop();
  16226. stack::get_field<true>(L, gcmetakey);
  16227. umt_t& target_umt = stack::pop<user<umt_t>>(L);
  16228. return target_umt;
  16229. }
  16230. static int push(lua_State* L, umt_t&& umx) {
  16231. umt_t& um = make_cleanup(L, std::move(umx));
  16232. usertype_metatable_core& umc = um;
  16233. regs_t value_table{{}};
  16234. int lastreg = 0;
  16235. (void)detail::swallow{0, (um.template make_regs<(I * 2)>(value_table, lastreg, std::get<(I * 2)>(um.functions), std::get<(I * 2 + 1)>(um.functions)), 0)...};
  16236. um.finish_regs(value_table, lastreg);
  16237. value_table[lastreg] = {nullptr, nullptr};
  16238. regs_t ref_table = value_table;
  16239. regs_t unique_table = value_table;
  16240. bool hasdestructor = !value_table.empty() && to_string(meta_function::garbage_collect) == value_table[lastreg - 1].name;
  16241. if (hasdestructor) {
  16242. ref_table[lastreg - 1] = {nullptr, nullptr};
  16243. }
  16244. unique_table[lastreg - 1] = {value_table[lastreg - 1].name, detail::unique_destruct<T>};
  16245. lua_createtable(L, 0, 2);
  16246. stack_reference type_table(L, -1);
  16247. stack::set_field(L, "name", detail::demangle<T>(), type_table.stack_index());
  16248. stack::set_field(L, "is", &usertype_detail::is_check<T>, type_table.stack_index());
  16249. // Now use um
  16250. const bool& mustindex = umc.mustindex;
  16251. for (std::size_t i = 0; i < 3; ++i) {
  16252. // Pointer types, AKA "references" from C++
  16253. const char* metakey = nullptr;
  16254. luaL_Reg* metaregs = nullptr;
  16255. switch (i) {
  16256. case 0:
  16257. metakey = &usertype_traits<T*>::metatable()[0];
  16258. metaregs = ref_table.data();
  16259. break;
  16260. case 1:
  16261. metakey = &usertype_traits<detail::unique_usertype<T>>::metatable()[0];
  16262. metaregs = unique_table.data();
  16263. break;
  16264. case 2:
  16265. default:
  16266. metakey = &usertype_traits<T>::metatable()[0];
  16267. metaregs = value_table.data();
  16268. break;
  16269. }
  16270. luaL_newmetatable(L, metakey);
  16271. stack_reference t(L, -1);
  16272. stack::set_field(L, meta_function::type, type_table, t.stack_index());
  16273. int upvalues = 0;
  16274. upvalues += stack::push(L, nullptr);
  16275. upvalues += stack::push(L, make_light(um));
  16276. luaL_setfuncs(L, metaregs, upvalues);
  16277. if (um.baseclasscheck != nullptr) {
  16278. stack::set_field(L, detail::base_class_check_key(), um.baseclasscheck, t.stack_index());
  16279. }
  16280. if (um.baseclasscast != nullptr) {
  16281. stack::set_field(L, detail::base_class_cast_key(), um.baseclasscast, t.stack_index());
  16282. }
  16283. stack::set_field(L, detail::base_class_index_propogation_key(), make_closure(um.indexbase, nullptr, make_light(um), make_light(umc)), t.stack_index());
  16284. stack::set_field(L, detail::base_class_new_index_propogation_key(), make_closure(um.newindexbase, nullptr, make_light(um), make_light(umc)), t.stack_index());
  16285. if (mustindex) {
  16286. // Basic index pushing: specialize
  16287. // index and newindex to give variables and stuff
  16288. stack::set_field(L, meta_function::index, make_closure(umt_t::index_call, nullptr, make_light(um), make_light(umc)), t.stack_index());
  16289. stack::set_field(L, meta_function::new_index, make_closure(umt_t::new_index_call, nullptr, make_light(um), make_light(umc)), t.stack_index());
  16290. }
  16291. else {
  16292. // If there's only functions, we can use the fast index version
  16293. stack::set_field(L, meta_function::index, t, t.stack_index());
  16294. }
  16295. // metatable on the metatable
  16296. // for call constructor purposes and such
  16297. lua_createtable(L, 0, 3);
  16298. stack_reference metabehind(L, -1);
  16299. stack::set_field(L, meta_function::type, type_table, metabehind.stack_index());
  16300. if (um.callconstructfunc != nullptr) {
  16301. stack::set_field(L, meta_function::call_function, make_closure(um.callconstructfunc, nullptr, make_light(um), make_light(umc)), metabehind.stack_index());
  16302. }
  16303. if (um.secondarymeta) {
  16304. stack::set_field(L, meta_function::index, make_closure(umt_t::index_call, nullptr, make_light(um), make_light(umc)), metabehind.stack_index());
  16305. stack::set_field(L, meta_function::new_index, make_closure(umt_t::new_index_call, nullptr, make_light(um), make_light(umc)), metabehind.stack_index());
  16306. }
  16307. // type information needs to be present on the behind-tables too
  16308. stack::set_field(L, metatable_key, metabehind, t.stack_index());
  16309. metabehind.pop();
  16310. // We want to just leave the table
  16311. // in the registry only, otherwise we return it
  16312. t.pop();
  16313. }
  16314. // Now for the shim-table that actually gets assigned to the name
  16315. luaL_newmetatable(L, &usertype_traits<T>::user_metatable()[0]);
  16316. stack_reference t(L, -1);
  16317. stack::set_field(L, meta_function::type, type_table, t.stack_index());
  16318. int upvalues = 0;
  16319. upvalues += stack::push(L, nullptr);
  16320. upvalues += stack::push(L, make_light(um));
  16321. luaL_setfuncs(L, value_table.data(), upvalues);
  16322. {
  16323. lua_createtable(L, 0, 3);
  16324. stack_reference metabehind(L, -1);
  16325. // type information needs to be present on the behind-tables too
  16326. stack::set_field(L, meta_function::type, type_table, metabehind.stack_index());
  16327. if (um.callconstructfunc != nullptr) {
  16328. stack::set_field(L, meta_function::call_function, make_closure(um.callconstructfunc, nullptr, make_light(um), make_light(umc)), metabehind.stack_index());
  16329. }
  16330. stack::set_field(L, meta_function::index, make_closure(umt_t::meta_index_call, nullptr, make_light(um), make_light(umc), nullptr, usertype_detail::toplevel_magic), metabehind.stack_index());
  16331. stack::set_field(L, meta_function::new_index, make_closure(umt_t::meta_new_index_call, nullptr, make_light(um), make_light(umc), nullptr, usertype_detail::toplevel_magic), metabehind.stack_index());
  16332. stack::set_field(L, metatable_key, metabehind, t.stack_index());
  16333. metabehind.pop();
  16334. }
  16335. lua_remove(L, type_table.stack_index());
  16336. return 1;
  16337. }
  16338. };
  16339. } // namespace stack
  16340. } // namespace sol
  16341. // end of sol/usertype_metatable.hpp
  16342. // beginning of sol/simple_usertype_metatable.hpp
  16343. namespace sol {
  16344. namespace usertype_detail {
  16345. inline int call_indexing_object(lua_State* L, object& f) {
  16346. int before = lua_gettop(L);
  16347. f.push();
  16348. for (int i = 1; i <= before; ++i) {
  16349. lua_pushvalue(L, i);
  16350. }
  16351. lua_call(L, before, LUA_MULTRET);
  16352. int after = lua_gettop(L);
  16353. return after - before;
  16354. }
  16355. template <typename T, bool is_index, bool toplevel = false, bool has_indexing = false>
  16356. inline int simple_core_indexing_call(lua_State* L) {
  16357. simple_map& sm = toplevel
  16358. ? stack::get<user<simple_map>>(L, upvalue_index(simple_metatable_index))
  16359. : stack::pop<user<simple_map>>(L);
  16360. variable_map& variables = sm.variables;
  16361. function_map& functions = sm.functions;
  16362. static const int keyidx = -2 + static_cast<int>(is_index);
  16363. if (toplevel) {
  16364. if (type_of(L, keyidx) != type::string) {
  16365. if (has_indexing) {
  16366. object& indexingfunc = is_index
  16367. ? sm.index
  16368. : sm.newindex;
  16369. return call_indexing_object(L, indexingfunc);
  16370. }
  16371. else {
  16372. return is_index
  16373. ? indexing_fail<T, is_index>(L)
  16374. : metatable_new_index<T, true>(L);
  16375. }
  16376. }
  16377. }
  16378. string_view accessor = stack::get<string_view>(L, keyidx);
  16379. variable_wrapper* varwrap = nullptr;
  16380. {
  16381. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  16382. string_view& accessorkey = accessor;
  16383. auto vit = variables.find(accessorkey, string_view_hash(), std::equal_to<string_view>());
  16384. #else
  16385. std::string accessorkey(accessor.data(), accessor.size());
  16386. auto vit = variables.find(accessorkey);
  16387. #endif // Compatible Hash
  16388. if (vit != variables.cend()) {
  16389. varwrap = vit->second.get();
  16390. }
  16391. }
  16392. if (varwrap != nullptr) {
  16393. return is_index ? varwrap->index(L) : varwrap->new_index(L);
  16394. }
  16395. bool function_failed = false;
  16396. {
  16397. #if defined(SOL_UNORDERED_MAP_COMPATIBLE_HASH) && SOL_UNORDERED_MAP_COMPATIBLE_HASH
  16398. string_view& accessorkey = accessor;
  16399. auto fit = functions.find(accessorkey, string_view_hash(), std::equal_to<string_view>());
  16400. #else
  16401. std::string accessorkey(accessor.data(), accessor.size());
  16402. auto fit = functions.find(accessorkey);
  16403. #endif // Compatible Hash
  16404. if (fit != functions.cend()) {
  16405. object& func = fit->second;
  16406. if (is_index) {
  16407. return stack::push(L, func);
  16408. }
  16409. else {
  16410. function_failed = true;
  16411. }
  16412. }
  16413. }
  16414. if (function_failed) {
  16415. if (has_indexing && !is_toplevel(L)) {
  16416. object& indexingfunc = is_index
  16417. ? sm.index
  16418. : sm.newindex;
  16419. return call_indexing_object(L, indexingfunc);
  16420. }
  16421. else {
  16422. return is_index
  16423. ? indexing_fail<T, is_index>(L)
  16424. : metatable_new_index<T, true>(L);
  16425. }
  16426. }
  16427. /* Check table storage first for a method that works
  16428. luaL_getmetatable(L, sm.metakey);
  16429. if (type_of(L, -1) != type::lua_nil) {
  16430. stack::get_field<false, true>(L, accessor.c_str(), lua_gettop(L));
  16431. if (type_of(L, -1) != type::lua_nil) {
  16432. // Woo, we found it?
  16433. lua_remove(L, -2);
  16434. return 1;
  16435. }
  16436. lua_pop(L, 1);
  16437. }
  16438. lua_pop(L, 1);
  16439. */
  16440. int ret = 0;
  16441. bool found = false;
  16442. // Otherwise, we need to do propagating calls through the bases
  16443. if (is_index) {
  16444. sm.indexbaseclasspropogation(L, found, ret, accessor);
  16445. }
  16446. else {
  16447. sm.newindexbaseclasspropogation(L, found, ret, accessor);
  16448. }
  16449. if (found) {
  16450. return ret;
  16451. }
  16452. if (toplevel) {
  16453. if (has_indexing && !is_toplevel(L)) {
  16454. object& indexingfunc = is_index
  16455. ? sm.index
  16456. : sm.newindex;
  16457. return call_indexing_object(L, indexingfunc);
  16458. }
  16459. else {
  16460. return is_index
  16461. ? indexing_fail<T, is_index>(L)
  16462. : metatable_new_index<T, true>(L);
  16463. }
  16464. }
  16465. return -1;
  16466. }
  16467. template <typename T, bool has_indexing = false>
  16468. inline int simple_real_index_call(lua_State* L) {
  16469. return simple_core_indexing_call<T, true, true, has_indexing>(L);
  16470. }
  16471. template <typename T, bool has_indexing = false>
  16472. inline int simple_real_new_index_call(lua_State* L) {
  16473. return simple_core_indexing_call<T, false, true, has_indexing>(L);
  16474. }
  16475. template <typename T, bool has_indexing = false>
  16476. inline int simple_index_call(lua_State* L) {
  16477. #if defined(__clang__)
  16478. return detail::trampoline(L, &simple_real_index_call<T, has_indexing>);
  16479. #else
  16480. return detail::typed_static_trampoline<decltype(&simple_real_index_call<T, has_indexing>), (&simple_real_index_call<T, has_indexing>)>(L);
  16481. #endif
  16482. }
  16483. template <typename T, bool has_indexing = false>
  16484. inline int simple_new_index_call(lua_State* L) {
  16485. #if defined(__clang__)
  16486. return detail::trampoline(L, &simple_real_new_index_call<T, has_indexing>);
  16487. #else
  16488. return detail::typed_static_trampoline<decltype(&simple_real_new_index_call<T, has_indexing>), (&simple_real_new_index_call<T, has_indexing>)>(L);
  16489. #endif
  16490. }
  16491. } // namespace usertype_detail
  16492. struct simple_tag {
  16493. } const simple{};
  16494. template <typename T>
  16495. struct simple_usertype_metatable : usertype_detail::registrar {
  16496. public:
  16497. usertype_detail::function_map registrations;
  16498. usertype_detail::variable_map varmap;
  16499. object callconstructfunc;
  16500. object indexfunc;
  16501. object newindexfunc;
  16502. lua_CFunction indexbase;
  16503. lua_CFunction newindexbase;
  16504. usertype_detail::base_walk indexbaseclasspropogation;
  16505. usertype_detail::base_walk newindexbaseclasspropogation;
  16506. void* baseclasscheck;
  16507. void* baseclasscast;
  16508. bool mustindex;
  16509. bool secondarymeta;
  16510. std::array<bool, 32> properties;
  16511. template <typename N>
  16512. void insert(N&& n, object&& o) {
  16513. std::string key = usertype_detail::make_string(std::forward<N>(n));
  16514. int is_indexer = static_cast<int>(usertype_detail::is_indexer(n));
  16515. if (is_indexer == 1) {
  16516. indexfunc = o;
  16517. mustindex = true;
  16518. }
  16519. else if (is_indexer == 2) {
  16520. newindexfunc = o;
  16521. mustindex = true;
  16522. }
  16523. auto hint = registrations.find(key);
  16524. if (hint == registrations.cend()) {
  16525. registrations.emplace_hint(hint, std::move(key), std::move(o));
  16526. return;
  16527. }
  16528. hint->second = std::move(o);
  16529. }
  16530. template <typename N, typename F, typename... Args>
  16531. void insert_prepare(std::true_type, lua_State* L, N&&, F&& f, Args&&... args) {
  16532. object o = make_object<F>(L, std::forward<F>(f), function_detail::call_indicator(), std::forward<Args>(args)...);
  16533. callconstructfunc = std::move(o);
  16534. }
  16535. template <typename N, typename F, typename... Args>
  16536. void insert_prepare(std::false_type, lua_State* L, N&& n, F&& f, Args&&... args) {
  16537. object o = make_object<F>(L, std::forward<F>(f), std::forward<Args>(args)...);
  16538. insert(std::forward<N>(n), std::move(o));
  16539. }
  16540. template <typename N, typename F>
  16541. void add_member_function(std::true_type, lua_State* L, N&& n, F&& f) {
  16542. insert_prepare(std::is_same<meta::unqualified_t<N>, call_construction>(), L, std::forward<N>(n), std::forward<F>(f), function_detail::class_indicator<T>());
  16543. }
  16544. template <typename N, typename F>
  16545. void add_member_function(std::false_type, lua_State* L, N&& n, F&& f) {
  16546. insert_prepare(std::is_same<meta::unqualified_t<N>, call_construction>(), L, std::forward<N>(n), std::forward<F>(f));
  16547. }
  16548. template <typename N, typename F, meta::enable<meta::is_callable<meta::unwrap_unqualified_t<F>>> = meta::enabler>
  16549. void add_function(lua_State* L, N&& n, F&& f) {
  16550. object o = make_object(L, as_function_reference(std::forward<F>(f)));
  16551. if (std::is_same<meta::unqualified_t<N>, call_construction>::value) {
  16552. callconstructfunc = std::move(o);
  16553. return;
  16554. }
  16555. insert(std::forward<N>(n), std::move(o));
  16556. }
  16557. template <typename N, typename F, meta::disable<meta::is_callable<meta::unwrap_unqualified_t<F>>> = meta::enabler>
  16558. void add_function(lua_State* L, N&& n, F&& f) {
  16559. add_member_function(std::is_member_pointer<meta::unwrap_unqualified_t<F>>(), L, std::forward<N>(n), std::forward<F>(f));
  16560. }
  16561. template <typename N, typename F, meta::disable<is_variable_binding<meta::unqualified_t<F>>> = meta::enabler>
  16562. void add(lua_State* L, N&& n, F&& f) {
  16563. add_function(L, std::forward<N>(n), std::forward<F>(f));
  16564. }
  16565. template <typename N, typename F, meta::enable<is_variable_binding<meta::unqualified_t<F>>> = meta::enabler>
  16566. void add(lua_State*, N&& n, F&& f) {
  16567. mustindex = true;
  16568. secondarymeta = true;
  16569. std::string key = usertype_detail::make_string(std::forward<N>(n));
  16570. auto o = std::make_unique<usertype_detail::callable_binding<T, std::decay_t<F>>>(std::forward<F>(f));
  16571. auto hint = varmap.find(key);
  16572. if (hint == varmap.cend()) {
  16573. varmap.emplace_hint(hint, std::move(key), std::move(o));
  16574. return;
  16575. }
  16576. hint->second = std::move(o);
  16577. }
  16578. template <typename N, typename... Fxs>
  16579. void add(lua_State* L, N&& n, constructor_wrapper<Fxs...> c) {
  16580. object o(L, in_place_type<detail::tagged<T, constructor_wrapper<Fxs...>>>, std::move(c));
  16581. if (std::is_same<meta::unqualified_t<N>, call_construction>::value) {
  16582. callconstructfunc = std::move(o);
  16583. return;
  16584. }
  16585. insert(std::forward<N>(n), std::move(o));
  16586. }
  16587. template <typename N, typename... Lists>
  16588. void add(lua_State* L, N&& n, constructor_list<Lists...> c) {
  16589. object o(L, in_place_type<detail::tagged<T, constructor_list<Lists...>>>, std::move(c));
  16590. if (std::is_same<meta::unqualified_t<N>, call_construction>::value) {
  16591. callconstructfunc = std::move(o);
  16592. return;
  16593. }
  16594. insert(std::forward<N>(n), std::move(o));
  16595. }
  16596. template <typename N>
  16597. void add(lua_State* L, N&& n, destructor_wrapper<void> c) {
  16598. object o(L, in_place_type<detail::tagged<T, destructor_wrapper<void>>>, std::move(c));
  16599. if (std::is_same<meta::unqualified_t<N>, call_construction>::value) {
  16600. callconstructfunc = std::move(o);
  16601. return;
  16602. }
  16603. insert(std::forward<N>(n), std::move(o));
  16604. }
  16605. template <typename N, typename Fx>
  16606. void add(lua_State* L, N&& n, destructor_wrapper<Fx> c) {
  16607. object o(L, in_place_type<detail::tagged<T, destructor_wrapper<Fx>>>, std::move(c));
  16608. if (std::is_same<meta::unqualified_t<N>, call_construction>::value) {
  16609. callconstructfunc = std::move(o);
  16610. return;
  16611. }
  16612. insert(std::forward<N>(n), std::move(o));
  16613. }
  16614. template <typename... Bases>
  16615. void add(lua_State*, base_classes_tag, bases<Bases...>) {
  16616. static_assert(sizeof(usertype_detail::base_walk) <= sizeof(void*), "size of function pointer is greater than sizeof(void*); cannot work on this platform. Please file a bug report.");
  16617. static_assert(!meta::any_same<T, Bases...>::value, "base classes cannot list the original class as part of the bases");
  16618. if (sizeof...(Bases) < 1) {
  16619. return;
  16620. }
  16621. mustindex = true;
  16622. (void)detail::swallow{0, ((detail::has_derived<Bases>::value = true), 0)...};
  16623. static_assert(sizeof(void*) <= sizeof(detail::inheritance_check_function), "The size of this data pointer is too small to fit the inheritance checking function: Please file a bug report.");
  16624. static_assert(sizeof(void*) <= sizeof(detail::inheritance_cast_function), "The size of this data pointer is too small to fit the inheritance checking function: Please file a bug report.");
  16625. baseclasscheck = reinterpret_cast<void*>(&detail::inheritance<T, Bases...>::type_check);
  16626. baseclasscast = reinterpret_cast<void*>(&detail::inheritance<T, Bases...>::type_cast);
  16627. indexbaseclasspropogation = usertype_detail::walk_all_bases<true, Bases...>;
  16628. newindexbaseclasspropogation = usertype_detail::walk_all_bases<false, Bases...>;
  16629. }
  16630. private:
  16631. template <std::size_t... I, typename Tuple>
  16632. simple_usertype_metatable(detail::verified_tag, std::index_sequence<I...>, lua_State* L, Tuple&& args)
  16633. : callconstructfunc(lua_nil), indexfunc(lua_nil), newindexfunc(lua_nil), indexbase(&usertype_detail::simple_core_indexing_call<T, true>), newindexbase(&usertype_detail::simple_core_indexing_call<T, false>), indexbaseclasspropogation(usertype_detail::walk_all_bases<true>), newindexbaseclasspropogation(&usertype_detail::walk_all_bases<false>), baseclasscheck(nullptr), baseclasscast(nullptr), mustindex(false), secondarymeta(false), properties() {
  16634. properties.fill(false);
  16635. (void)detail::swallow{0,
  16636. (add(L, detail::forward_get<I * 2>(args), detail::forward_get<I * 2 + 1>(args)), 0)...};
  16637. }
  16638. template <typename... Args>
  16639. simple_usertype_metatable(lua_State* L, detail::verified_tag v, Args&&... args)
  16640. : simple_usertype_metatable(v, std::make_index_sequence<sizeof...(Args) / 2>(), L, std::forward_as_tuple(std::forward<Args>(args)...)) {
  16641. }
  16642. template <typename... Args>
  16643. simple_usertype_metatable(lua_State* L, detail::add_destructor_tag, Args&&... args)
  16644. : simple_usertype_metatable(L, detail::verified, std::forward<Args>(args)..., "__gc", default_destructor) {
  16645. }
  16646. template <typename... Args>
  16647. simple_usertype_metatable(lua_State* L, detail::check_destructor_tag, Args&&... args)
  16648. : simple_usertype_metatable(L, meta::condition<meta::all<std::is_destructible<T>, meta::neg<detail::has_destructor<Args...>>>, detail::add_destructor_tag, detail::verified_tag>(), std::forward<Args>(args)...) {
  16649. }
  16650. public:
  16651. simple_usertype_metatable(lua_State* L)
  16652. : simple_usertype_metatable(L, meta::condition<meta::all<std::is_default_constructible<T>>, decltype(default_constructor), detail::check_destructor_tag>()) {
  16653. }
  16654. template <typename Arg, typename... Args, meta::disable_any<meta::any_same<meta::unqualified_t<Arg>, detail::verified_tag, detail::add_destructor_tag, detail::check_destructor_tag>, meta::is_specialization_of<meta::unqualified_t<Arg>, constructors>, meta::is_specialization_of<meta::unqualified_t<Arg>, constructor_wrapper>> = meta::enabler>
  16655. simple_usertype_metatable(lua_State* L, Arg&& arg, Args&&... args)
  16656. : simple_usertype_metatable(L, meta::condition<meta::all<std::is_default_constructible<T>, meta::neg<detail::has_constructor<Args...>>>, decltype(default_constructor), detail::check_destructor_tag>(), std::forward<Arg>(arg), std::forward<Args>(args)...) {
  16657. }
  16658. template <typename... Args, typename... CArgs>
  16659. simple_usertype_metatable(lua_State* L, constructors<CArgs...> constructorlist, Args&&... args)
  16660. : simple_usertype_metatable(L, detail::check_destructor_tag(), std::forward<Args>(args)..., "new", constructorlist) {
  16661. }
  16662. template <typename... Args, typename... Fxs>
  16663. simple_usertype_metatable(lua_State* L, constructor_wrapper<Fxs...> constructorlist, Args&&... args)
  16664. : simple_usertype_metatable(L, detail::check_destructor_tag(), std::forward<Args>(args)..., "new", constructorlist) {
  16665. }
  16666. simple_usertype_metatable(const simple_usertype_metatable&) = default;
  16667. simple_usertype_metatable(simple_usertype_metatable&&) = default;
  16668. simple_usertype_metatable& operator=(const simple_usertype_metatable&) = default;
  16669. simple_usertype_metatable& operator=(simple_usertype_metatable&&) = default;
  16670. virtual int push_um(lua_State* L) override {
  16671. return stack::push(L, std::move(*this));
  16672. }
  16673. };
  16674. namespace stack {
  16675. template <typename T>
  16676. struct pusher<simple_usertype_metatable<T>> {
  16677. typedef simple_usertype_metatable<T> umt_t;
  16678. static usertype_detail::simple_map& make_cleanup(lua_State* L, umt_t& umx) {
  16679. static int uniqueness = 0;
  16680. std::string uniquegcmetakey = usertype_traits<T>::user_gc_metatable();
  16681. // std::to_string doesn't exist in android still, with NDK, so this bullshit
  16682. // is necessary
  16683. // thanks, Android :v
  16684. int appended = snprintf(nullptr, 0, "%d", uniqueness);
  16685. std::size_t insertionpoint = uniquegcmetakey.length() - 1;
  16686. uniquegcmetakey.append(appended, '\0');
  16687. char* uniquetarget = &uniquegcmetakey[insertionpoint];
  16688. snprintf(uniquetarget, uniquegcmetakey.length(), "%d", uniqueness);
  16689. ++uniqueness;
  16690. const char* gcmetakey = &usertype_traits<T>::gc_table()[0];
  16691. stack::push<user<usertype_detail::simple_map>>(L, metatable_key, uniquegcmetakey, &usertype_traits<T>::metatable()[0],
  16692. umx.indexbaseclasspropogation, umx.newindexbaseclasspropogation,
  16693. std::move(umx.indexfunc), std::move(umx.newindexfunc),
  16694. std::move(umx.varmap), std::move(umx.registrations));
  16695. stack_reference stackvarmap(L, -1);
  16696. stack::set_field<true>(L, gcmetakey, stackvarmap);
  16697. stackvarmap.pop();
  16698. stack::get_field<true>(L, gcmetakey);
  16699. usertype_detail::simple_map& varmap = stack::pop<user<usertype_detail::simple_map>>(L);
  16700. return varmap;
  16701. }
  16702. static int push(lua_State* L, umt_t&& umx) {
  16703. bool hasindex = umx.indexfunc.valid();
  16704. bool hasnewindex = umx.newindexfunc.valid();
  16705. auto& varmap = make_cleanup(L, umx);
  16706. auto& properties = umx.properties;
  16707. auto sic = hasindex ? &usertype_detail::simple_index_call<T, true> : &usertype_detail::simple_index_call<T, false>;
  16708. auto snic = hasnewindex ? &usertype_detail::simple_new_index_call<T, true> : &usertype_detail::simple_new_index_call<T, false>;
  16709. lua_createtable(L, 0, 2);
  16710. stack_reference type_table(L, -1);
  16711. stack::set_field(L, "name", detail::demangle<T>(), type_table.stack_index());
  16712. stack::set_field(L, "is", &usertype_detail::is_check<T>, type_table.stack_index());
  16713. auto safety_check = [&](const std::string& first) {
  16714. for (std::size_t j = 0; j < properties.size(); ++j) {
  16715. meta_function mf = static_cast<meta_function>(j);
  16716. const std::string& mfname = to_string(mf);
  16717. bool& prop = properties[j];
  16718. if (mfname != first)
  16719. continue;
  16720. switch (mf) {
  16721. case meta_function::construct:
  16722. if (prop) {
  16723. #if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS
  16724. assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16725. #else
  16726. throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16727. #endif
  16728. }
  16729. break;
  16730. case meta_function::garbage_collect:
  16731. if (prop) {
  16732. #if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS
  16733. assert(false && "sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16734. #else
  16735. throw error("sol: 2 separate constructor (new) functions were set on this type. Please specify only 1 sol::meta_function::construct/'new' type AND wrap the function in a sol::factories/initializers call, as shown by the documentation and examples, otherwise you may create problems");
  16736. #endif
  16737. }
  16738. return;
  16739. default:
  16740. break;
  16741. }
  16742. prop = true;
  16743. break;
  16744. }
  16745. };
  16746. for (auto& kvp : varmap.functions) {
  16747. auto& first = std::get<0>(kvp);
  16748. safety_check(first);
  16749. }
  16750. auto register_kvp = [&](std::size_t meta_index, stack_reference& t, const std::string& first, object& second) {
  16751. meta_function mf = meta_function::construct;
  16752. for (std::size_t j = 0; j < properties.size(); ++j) {
  16753. mf = static_cast<meta_function>(j);
  16754. const std::string& mfname = to_string(mf);
  16755. bool& prop = properties[j];
  16756. if (mfname != first)
  16757. continue;
  16758. switch (mf) {
  16759. case meta_function::index:
  16760. umx.indexfunc = second;
  16761. break;
  16762. case meta_function::new_index:
  16763. umx.newindexfunc = second;
  16764. break;
  16765. default:
  16766. break;
  16767. }
  16768. prop = true;
  16769. break;
  16770. }
  16771. switch (meta_index) {
  16772. case 0:
  16773. if (mf == meta_function::garbage_collect) {
  16774. return;
  16775. }
  16776. break;
  16777. case 1:
  16778. if (mf == meta_function::garbage_collect) {
  16779. stack::set_field(L, first, detail::unique_destruct<T>, t.stack_index());
  16780. return;
  16781. }
  16782. break;
  16783. case 2:
  16784. default:
  16785. break;
  16786. }
  16787. stack::set_field(L, first, second, t.stack_index());
  16788. };
  16789. for (std::size_t i = 0; i < 3; ++i) {
  16790. const char* metakey = nullptr;
  16791. switch (i) {
  16792. case 0:
  16793. metakey = &usertype_traits<T*>::metatable()[0];
  16794. break;
  16795. case 1:
  16796. metakey = &usertype_traits<detail::unique_usertype<T>>::metatable()[0];
  16797. break;
  16798. case 2:
  16799. default:
  16800. metakey = &usertype_traits<T>::metatable()[0];
  16801. break;
  16802. }
  16803. luaL_newmetatable(L, metakey);
  16804. stack_reference t(L, -1);
  16805. stack::set_field(L, meta_function::type, type_table, t.stack_index());
  16806. for (auto& kvp : varmap.functions) {
  16807. auto& first = std::get<0>(kvp);
  16808. auto& second = std::get<1>(kvp);
  16809. register_kvp(i, t, first, second);
  16810. }
  16811. luaL_Reg opregs[34]{};
  16812. int opregsindex = 0;
  16813. auto prop_fx = [&](meta_function mf) { return !properties[static_cast<int>(mf)]; };
  16814. usertype_detail::insert_default_registrations<T>(opregs, opregsindex, prop_fx);
  16815. t.push();
  16816. luaL_setfuncs(L, opregs, 0);
  16817. t.pop();
  16818. if (umx.baseclasscheck != nullptr) {
  16819. stack::set_field(L, detail::base_class_check_key(), umx.baseclasscheck, t.stack_index());
  16820. }
  16821. if (umx.baseclasscast != nullptr) {
  16822. stack::set_field(L, detail::base_class_cast_key(), umx.baseclasscast, t.stack_index());
  16823. }
  16824. // Base class propagation features
  16825. stack::set_field(L, detail::base_class_index_propogation_key(), umx.indexbase, t.stack_index());
  16826. stack::set_field(L, detail::base_class_new_index_propogation_key(), umx.newindexbase, t.stack_index());
  16827. if (umx.mustindex) {
  16828. // use indexing function
  16829. stack::set_field(L, meta_function::index,
  16830. make_closure(sic,
  16831. nullptr,
  16832. make_light(varmap)),
  16833. t.stack_index());
  16834. stack::set_field(L, meta_function::new_index,
  16835. make_closure(snic,
  16836. nullptr,
  16837. make_light(varmap)),
  16838. t.stack_index());
  16839. }
  16840. else {
  16841. // Metatable indexes itself
  16842. stack::set_field(L, meta_function::index, t, t.stack_index());
  16843. }
  16844. // metatable on the metatable
  16845. // for call constructor purposes and such
  16846. lua_createtable(L, 0, 2 * static_cast<int>(umx.secondarymeta) + static_cast<int>(umx.callconstructfunc.valid()));
  16847. stack_reference metabehind(L, -1);
  16848. stack::set_field(L, meta_function::type, type_table, metabehind.stack_index());
  16849. if (umx.callconstructfunc.valid()) {
  16850. stack::set_field(L, meta_function::call_function, umx.callconstructfunc, metabehind.stack_index());
  16851. }
  16852. if (umx.secondarymeta) {
  16853. stack::set_field(L, meta_function::index,
  16854. make_closure(sic,
  16855. nullptr,
  16856. make_light(varmap)),
  16857. metabehind.stack_index());
  16858. stack::set_field(L, meta_function::new_index,
  16859. make_closure(snic,
  16860. nullptr,
  16861. make_light(varmap)),
  16862. metabehind.stack_index());
  16863. }
  16864. stack::set_field(L, metatable_key, metabehind, t.stack_index());
  16865. metabehind.pop();
  16866. t.pop();
  16867. }
  16868. // Now for the shim-table that actually gets pushed
  16869. luaL_newmetatable(L, &usertype_traits<T>::user_metatable()[0]);
  16870. stack_reference t(L, -1);
  16871. stack::set_field(L, meta_function::type, type_table, t.stack_index());
  16872. for (auto& kvp : varmap.functions) {
  16873. auto& first = std::get<0>(kvp);
  16874. auto& second = std::get<1>(kvp);
  16875. register_kvp(2, t, first, second);
  16876. }
  16877. {
  16878. lua_createtable(L, 0, 2 + static_cast<int>(umx.callconstructfunc.valid()));
  16879. stack_reference metabehind(L, -1);
  16880. stack::set_field(L, meta_function::type, type_table, metabehind.stack_index());
  16881. if (umx.callconstructfunc.valid()) {
  16882. stack::set_field(L, meta_function::call_function, umx.callconstructfunc, metabehind.stack_index());
  16883. }
  16884. // use indexing function
  16885. stack::set_field(L, meta_function::index,
  16886. make_closure(sic,
  16887. nullptr,
  16888. make_light(varmap),
  16889. nullptr,
  16890. nullptr,
  16891. usertype_detail::toplevel_magic),
  16892. metabehind.stack_index());
  16893. stack::set_field(L, meta_function::new_index,
  16894. make_closure(snic,
  16895. nullptr,
  16896. make_light(varmap),
  16897. nullptr,
  16898. nullptr,
  16899. usertype_detail::toplevel_magic),
  16900. metabehind.stack_index());
  16901. stack::set_field(L, metatable_key, metabehind, t.stack_index());
  16902. metabehind.pop();
  16903. }
  16904. lua_remove(L, type_table.stack_index());
  16905. // Don't pop the table when we're done;
  16906. // return it
  16907. return 1;
  16908. }
  16909. };
  16910. } // namespace stack
  16911. } // namespace sol
  16912. // end of sol/simple_usertype_metatable.hpp
  16913. namespace sol {
  16914. template <typename T>
  16915. class usertype {
  16916. private:
  16917. std::unique_ptr<usertype_detail::registrar> metatableregister;
  16918. template <typename... Args>
  16919. usertype(detail::verified_tag, Args&&... args)
  16920. : metatableregister(std::make_unique<usertype_metatable<T, std::make_index_sequence<sizeof...(Args) / 2>, Args...>>(std::forward<Args>(args)...)) {
  16921. static_assert(detail::has_destructor<Args...>::value, "this type does not have an explicit destructor declared; please pass a custom destructor function wrapped in sol::destruct, especially if the type does not have an accessible (private) destructor");
  16922. }
  16923. template <typename... Args>
  16924. usertype(detail::add_destructor_tag, Args&&... args)
  16925. : usertype(detail::verified, std::forward<Args>(args)..., "__gc", default_destructor) {
  16926. }
  16927. template <typename... Args>
  16928. usertype(detail::check_destructor_tag, Args&&... args)
  16929. : usertype(meta::condition<meta::all<std::is_destructible<T>, meta::neg<detail::has_destructor<Args...>>>, detail::add_destructor_tag, detail::verified_tag>(), std::forward<Args>(args)...) {
  16930. }
  16931. public:
  16932. template <typename... Args>
  16933. usertype(Args&&... args)
  16934. : usertype(meta::condition<meta::all<std::is_default_constructible<T>, meta::neg<detail::has_constructor<Args...>>>, decltype(default_constructor), detail::check_destructor_tag>(), std::forward<Args>(args)...) {
  16935. }
  16936. template <typename... Args, typename... CArgs>
  16937. usertype(constructors<CArgs...> constructorlist, Args&&... args)
  16938. : usertype(detail::check_destructor_tag(), std::forward<Args>(args)..., "new", constructorlist) {
  16939. }
  16940. template <typename... Args, typename... Fxs>
  16941. usertype(constructor_wrapper<Fxs...> constructorlist, Args&&... args)
  16942. : usertype(detail::check_destructor_tag(), std::forward<Args>(args)..., "new", constructorlist) {
  16943. }
  16944. template <typename... Args>
  16945. usertype(simple_tag, lua_State* L, Args&&... args)
  16946. : metatableregister(std::make_unique<simple_usertype_metatable<T>>(L, std::forward<Args>(args)...)) {
  16947. }
  16948. usertype_detail::registrar* registrar_data() {
  16949. return metatableregister.get();
  16950. }
  16951. int push(lua_State* L) {
  16952. int r = metatableregister->push_um(L);
  16953. metatableregister = nullptr;
  16954. return r;
  16955. }
  16956. };
  16957. template <typename T>
  16958. class simple_usertype : public usertype<T> {
  16959. private:
  16960. typedef usertype<T> base_t;
  16961. lua_State* state;
  16962. public:
  16963. template <typename... Args>
  16964. simple_usertype(lua_State* L, Args&&... args)
  16965. : base_t(simple, L, std::forward<Args>(args)...), state(L) {
  16966. }
  16967. template <typename N, typename F>
  16968. void set(N&& n, F&& f) {
  16969. auto meta = static_cast<simple_usertype_metatable<T>*>(base_t::registrar_data());
  16970. meta->add(state, std::forward<N>(n), std::forward<F>(f));
  16971. }
  16972. };
  16973. namespace stack {
  16974. template <typename T>
  16975. struct pusher<usertype<T>> {
  16976. static int push(lua_State* L, usertype<T>& user) {
  16977. return user.push(L);
  16978. }
  16979. };
  16980. } // namespace stack
  16981. } // namespace sol
  16982. // end of sol/usertype.hpp
  16983. // beginning of sol/table_iterator.hpp
  16984. namespace sol {
  16985. template <typename reference_type>
  16986. class basic_table_iterator : public std::iterator<std::input_iterator_tag, std::pair<object, object>> {
  16987. public:
  16988. typedef object key_type;
  16989. typedef object mapped_type;
  16990. typedef std::pair<object, object> value_type;
  16991. typedef std::input_iterator_tag iterator_category;
  16992. typedef std::ptrdiff_t difference_type;
  16993. typedef value_type* pointer;
  16994. typedef value_type& reference;
  16995. typedef const value_type& const_reference;
  16996. private:
  16997. std::pair<object, object> kvp;
  16998. reference_type ref;
  16999. int tableidx = 0;
  17000. int keyidx = 0;
  17001. std::ptrdiff_t idx = 0;
  17002. public:
  17003. basic_table_iterator()
  17004. : keyidx(-1), idx(-1) {
  17005. }
  17006. basic_table_iterator(reference_type x)
  17007. : ref(std::move(x)) {
  17008. ref.push();
  17009. tableidx = lua_gettop(ref.lua_state());
  17010. stack::push(ref.lua_state(), lua_nil);
  17011. this->operator++();
  17012. if (idx == -1) {
  17013. return;
  17014. }
  17015. --idx;
  17016. }
  17017. basic_table_iterator& operator++() {
  17018. if (idx == -1)
  17019. return *this;
  17020. if (lua_next(ref.lua_state(), tableidx) == 0) {
  17021. idx = -1;
  17022. keyidx = -1;
  17023. return *this;
  17024. }
  17025. ++idx;
  17026. kvp.first = object(ref.lua_state(), -2);
  17027. kvp.second = object(ref.lua_state(), -1);
  17028. lua_pop(ref.lua_state(), 1);
  17029. // leave key on the stack
  17030. keyidx = lua_gettop(ref.lua_state());
  17031. return *this;
  17032. }
  17033. basic_table_iterator operator++(int) {
  17034. auto saved = *this;
  17035. this->operator++();
  17036. return saved;
  17037. }
  17038. reference operator*() {
  17039. return kvp;
  17040. }
  17041. const_reference operator*() const {
  17042. return kvp;
  17043. }
  17044. bool operator==(const basic_table_iterator& right) const {
  17045. return idx == right.idx;
  17046. }
  17047. bool operator!=(const basic_table_iterator& right) const {
  17048. return idx != right.idx;
  17049. }
  17050. ~basic_table_iterator() {
  17051. if (keyidx != -1) {
  17052. stack::remove(ref.lua_state(), keyidx, 1);
  17053. }
  17054. if (ref.lua_state() != nullptr && ref.valid()) {
  17055. stack::remove(ref.lua_state(), tableidx, 1);
  17056. }
  17057. }
  17058. };
  17059. } // namespace sol
  17060. // end of sol/table_iterator.hpp
  17061. namespace sol {
  17062. namespace detail {
  17063. template <std::size_t n>
  17064. struct clean {
  17065. lua_State* L;
  17066. clean(lua_State* luastate)
  17067. : L(luastate) {
  17068. }
  17069. ~clean() {
  17070. lua_pop(L, static_cast<int>(n));
  17071. }
  17072. };
  17073. struct ref_clean {
  17074. lua_State* L;
  17075. int& n;
  17076. ref_clean(lua_State* luastate, int& n)
  17077. : L(luastate), n(n) {
  17078. }
  17079. ~ref_clean() {
  17080. lua_pop(L, static_cast<int>(n));
  17081. }
  17082. };
  17083. inline int fail_on_newindex(lua_State* L) {
  17084. return luaL_error(L, "sol: cannot modify the elements of an enumeration table");
  17085. }
  17086. } // namespace detail
  17087. const new_table create = new_table{};
  17088. template <bool top_level, typename base_type>
  17089. class basic_table_core : public basic_object_base<base_type> {
  17090. typedef basic_object_base<base_type> base_t;
  17091. friend class state;
  17092. friend class state_view;
  17093. template <typename... Args>
  17094. using is_global = meta::all<meta::boolean<top_level>, meta::is_c_str<Args>...>;
  17095. template <typename Fx>
  17096. void for_each(std::true_type, Fx&& fx) const {
  17097. auto pp = stack::push_pop(*this);
  17098. stack::push(base_t::lua_state(), lua_nil);
  17099. while (lua_next(base_t::lua_state(), -2)) {
  17100. object key(base_t::lua_state(), -2);
  17101. object value(base_t::lua_state(), -1);
  17102. std::pair<object&, object&> keyvalue(key, value);
  17103. auto pn = stack::pop_n(base_t::lua_state(), 1);
  17104. fx(keyvalue);
  17105. }
  17106. }
  17107. template <typename Fx>
  17108. void for_each(std::false_type, Fx&& fx) const {
  17109. auto pp = stack::push_pop(*this);
  17110. stack::push(base_t::lua_state(), lua_nil);
  17111. while (lua_next(base_t::lua_state(), -2)) {
  17112. object key(base_t::lua_state(), -2);
  17113. object value(base_t::lua_state(), -1);
  17114. auto pn = stack::pop_n(base_t::lua_state(), 1);
  17115. fx(key, value);
  17116. }
  17117. }
  17118. template <bool raw, typename Ret0, typename Ret1, typename... Ret, std::size_t... I, typename Keys>
  17119. auto tuple_get(types<Ret0, Ret1, Ret...>, std::index_sequence<0, 1, I...>, Keys&& keys) const
  17120. -> decltype(stack::pop<std::tuple<Ret0, Ret1, Ret...>>(nullptr)) {
  17121. typedef decltype(stack::pop<std::tuple<Ret0, Ret1, Ret...>>(nullptr)) Tup;
  17122. return Tup(
  17123. traverse_get_optional<top_level, raw, Ret0>(meta::is_optional<meta::unqualified_t<Ret0>>(), detail::forward_get<0>(keys)),
  17124. traverse_get_optional<top_level, raw, Ret1>(meta::is_optional<meta::unqualified_t<Ret1>>(), detail::forward_get<1>(keys)),
  17125. traverse_get_optional<top_level, raw, Ret>(meta::is_optional<meta::unqualified_t<Ret>>(), detail::forward_get<I>(keys))...);
  17126. }
  17127. template <bool raw, typename Ret, std::size_t I, typename Keys>
  17128. decltype(auto) tuple_get(types<Ret>, std::index_sequence<I>, Keys&& keys) const {
  17129. return traverse_get_optional<top_level, raw, Ret>(meta::is_optional<meta::unqualified_t<Ret>>(), detail::forward_get<I>(keys));
  17130. }
  17131. template <bool raw, typename Pairs, std::size_t... I>
  17132. void tuple_set(std::index_sequence<I...>, Pairs&& pairs) {
  17133. auto pp = stack::push_pop < top_level && (is_global<decltype(detail::forward_get<I * 2>(pairs))...>::value) > (*this);
  17134. void(detail::swallow{ (stack::set_field<top_level, raw>(base_t::lua_state(),
  17135. detail::forward_get<I * 2>(pairs),
  17136. detail::forward_get<I * 2 + 1>(pairs),
  17137. lua_gettop(base_t::lua_state())),
  17138. 0)... });
  17139. }
  17140. template <bool global, bool raw, typename T, typename Key>
  17141. decltype(auto) traverse_get_deep(Key&& key) const {
  17142. stack::get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key));
  17143. return stack::get<T>(base_t::lua_state());
  17144. }
  17145. template <bool global, bool raw, typename T, typename Key, typename... Keys>
  17146. decltype(auto) traverse_get_deep(Key&& key, Keys&&... keys) const {
  17147. stack::get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key));
  17148. return traverse_get_deep<false, raw, T>(std::forward<Keys>(keys)...);
  17149. }
  17150. template <bool global, bool raw, typename T, std::size_t I, typename Key>
  17151. decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key) const {
  17152. typedef decltype(stack::get<T>(base_t::lua_state())) R;
  17153. auto p = stack::probe_get_field<global, raw, T>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
  17154. popcount += p.levels;
  17155. if (!p.success)
  17156. return R(nullopt);
  17157. return stack::get<T>(base_t::lua_state());
  17158. }
  17159. template <bool global, bool raw, typename T, std::size_t I, typename Key, typename... Keys>
  17160. decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key, Keys&&... keys) const {
  17161. auto p = I > 0 ? stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), -1) : stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
  17162. popcount += p.levels;
  17163. if (!p.success)
  17164. return T(nullopt);
  17165. return traverse_get_deep_optional<false, raw, T, I + 1>(popcount, std::forward<Keys>(keys)...);
  17166. }
  17167. template <bool global, bool raw, typename T, typename... Keys>
  17168. decltype(auto) traverse_get_optional(std::false_type, Keys&&... keys) const {
  17169. detail::clean<sizeof...(Keys)> c(base_t::lua_state());
  17170. return traverse_get_deep<global, raw, T>(std::forward<Keys>(keys)...);
  17171. }
  17172. template <bool global, bool raw, typename T, typename... Keys>
  17173. decltype(auto) traverse_get_optional(std::true_type, Keys&&... keys) const {
  17174. int popcount = 0;
  17175. detail::ref_clean c(base_t::lua_state(), popcount);
  17176. return traverse_get_deep_optional<global, raw, T, 0>(popcount, std::forward<Keys>(keys)...);
  17177. }
  17178. template <bool global, bool raw, typename Key, typename Value>
  17179. void traverse_set_deep(Key&& key, Value&& value) const {
  17180. stack::set_field<global, raw>(base_t::lua_state(), std::forward<Key>(key), std::forward<Value>(value));
  17181. }
  17182. template <bool global, bool raw, typename Key, typename... Keys>
  17183. void traverse_set_deep(Key&& key, Keys&&... keys) const {
  17184. stack::get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key));
  17185. traverse_set_deep<false, raw>(std::forward<Keys>(keys)...);
  17186. }
  17187. basic_table_core(lua_State* L, detail::global_tag t) noexcept
  17188. : base_t(L, t) {
  17189. }
  17190. protected:
  17191. basic_table_core(detail::no_safety_tag, lua_nil_t n)
  17192. : base_t(n) {
  17193. }
  17194. basic_table_core(detail::no_safety_tag, lua_State* L, int index)
  17195. : base_t(L, index) {
  17196. }
  17197. basic_table_core(detail::no_safety_tag, lua_State* L, ref_index index)
  17198. : base_t(L, index) {
  17199. }
  17200. template <typename T, meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<base_type, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  17201. basic_table_core(detail::no_safety_tag, T&& r) noexcept
  17202. : base_t(std::forward<T>(r)) {
  17203. }
  17204. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  17205. basic_table_core(detail::no_safety_tag, lua_State*L, T&& r) noexcept
  17206. : base_t(L, std::forward<T>(r)) {
  17207. }
  17208. public:
  17209. typedef basic_table_iterator<base_type> iterator;
  17210. typedef iterator const_iterator;
  17211. using base_t::lua_state;
  17212. basic_table_core() noexcept = default;
  17213. basic_table_core(const basic_table_core&) = default;
  17214. basic_table_core(basic_table_core&&) = default;
  17215. basic_table_core& operator=(const basic_table_core&) = default;
  17216. basic_table_core& operator=(basic_table_core&&) = default;
  17217. basic_table_core(const stack_reference& r)
  17218. : basic_table_core(r.lua_state(), r.stack_index()) {
  17219. }
  17220. basic_table_core(stack_reference&& r)
  17221. : basic_table_core(r.lua_state(), r.stack_index()) {
  17222. }
  17223. template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  17224. basic_table_core(lua_State* L, T&& r)
  17225. : base_t(L, std::forward<T>(r)) {
  17226. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17227. auto pp = stack::push_pop(*this);
  17228. constructor_handler handler{};
  17229. stack::check<basic_table_core>(lua_state(), -1, handler);
  17230. #endif // Safety
  17231. }
  17232. basic_table_core(lua_State* L, const new_table& nt)
  17233. : base_t(L, -stack::push(L, nt)) {
  17234. if (!is_stack_based<meta::unqualified_t<base_type>>::value) {
  17235. lua_pop(L, 1);
  17236. }
  17237. }
  17238. basic_table_core(lua_State* L, int index = -1)
  17239. : basic_table_core(detail::no_safety, L, index) {
  17240. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17241. constructor_handler handler{};
  17242. stack::check<basic_table_core>(L, index, handler);
  17243. #endif // Safety
  17244. }
  17245. basic_table_core(lua_State* L, ref_index index)
  17246. : basic_table_core(detail::no_safety, L, index) {
  17247. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17248. auto pp = stack::push_pop(*this);
  17249. constructor_handler handler{};
  17250. stack::check<basic_table_core>(lua_state(), -1, handler);
  17251. #endif // Safety
  17252. }
  17253. template <typename T, meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<base_type, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  17254. basic_table_core(T&& r) noexcept
  17255. : basic_table_core(detail::no_safety, std::forward<T>(r)) {
  17256. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17257. if (!is_table<meta::unqualified_t<T>>::value) {
  17258. auto pp = stack::push_pop(*this);
  17259. constructor_handler handler{};
  17260. stack::check<basic_table_core>(base_t::lua_state(), -1, handler);
  17261. }
  17262. #endif // Safety
  17263. }
  17264. basic_table_core(lua_nil_t r) noexcept
  17265. : basic_table_core(detail::no_safety, r) {
  17266. }
  17267. iterator begin() const {
  17268. return iterator(*this);
  17269. }
  17270. iterator end() const {
  17271. return iterator();
  17272. }
  17273. const_iterator cbegin() const {
  17274. return begin();
  17275. }
  17276. const_iterator cend() const {
  17277. return end();
  17278. }
  17279. template <typename... Ret, typename... Keys>
  17280. decltype(auto) get(Keys&&... keys) const {
  17281. static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
  17282. auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
  17283. return tuple_get<false>(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), std::forward_as_tuple(std::forward<Keys>(keys)...));
  17284. }
  17285. template <typename T, typename Key>
  17286. decltype(auto) get_or(Key&& key, T&& otherwise) const {
  17287. typedef decltype(get<T>("")) U;
  17288. optional<U> option = get<optional<U>>(std::forward<Key>(key));
  17289. if (option) {
  17290. return static_cast<U>(option.value());
  17291. }
  17292. return static_cast<U>(std::forward<T>(otherwise));
  17293. }
  17294. template <typename T, typename Key, typename D>
  17295. decltype(auto) get_or(Key&& key, D&& otherwise) const {
  17296. optional<T> option = get<optional<T>>(std::forward<Key>(key));
  17297. if (option) {
  17298. return static_cast<T>(option.value());
  17299. }
  17300. return static_cast<T>(std::forward<D>(otherwise));
  17301. }
  17302. template <typename T, typename... Keys>
  17303. decltype(auto) traverse_get(Keys&&... keys) const {
  17304. auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
  17305. return traverse_get_optional<top_level, false, T>(meta::is_optional<meta::unqualified_t<T>>(), std::forward<Keys>(keys)...);
  17306. }
  17307. template <typename... Keys>
  17308. basic_table_core& traverse_set(Keys&&... keys) {
  17309. auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
  17310. auto pn = stack::pop_n(base_t::lua_state(), static_cast<int>(sizeof...(Keys) - 2));
  17311. traverse_set_deep<top_level, false>(std::forward<Keys>(keys)...);
  17312. return *this;
  17313. }
  17314. template <typename... Args>
  17315. basic_table_core& set(Args&&... args) {
  17316. tuple_set<false>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
  17317. return *this;
  17318. }
  17319. template <typename... Ret, typename... Keys>
  17320. decltype(auto) raw_get(Keys&&... keys) const {
  17321. static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
  17322. auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
  17323. return tuple_get<true>(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), std::forward_as_tuple(std::forward<Keys>(keys)...));
  17324. }
  17325. template <typename T, typename Key>
  17326. decltype(auto) raw_get_or(Key&& key, T&& otherwise) const {
  17327. typedef decltype(raw_get<T>("")) U;
  17328. optional<U> option = raw_get<optional<U>>(std::forward<Key>(key));
  17329. if (option) {
  17330. return static_cast<U>(option.value());
  17331. }
  17332. return static_cast<U>(std::forward<T>(otherwise));
  17333. }
  17334. template <typename T, typename Key, typename D>
  17335. decltype(auto) raw_get_or(Key&& key, D&& otherwise) const {
  17336. optional<T> option = raw_get<optional<T>>(std::forward<Key>(key));
  17337. if (option) {
  17338. return static_cast<T>(option.value());
  17339. }
  17340. return static_cast<T>(std::forward<D>(otherwise));
  17341. }
  17342. template <typename T, typename... Keys>
  17343. decltype(auto) traverse_raw_get(Keys&&... keys) const {
  17344. auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
  17345. return traverse_get_optional<top_level, true, T>(meta::is_optional<meta::unqualified_t<T>>(), std::forward<Keys>(keys)...);
  17346. }
  17347. template <typename... Keys>
  17348. basic_table_core& traverse_raw_set(Keys&&... keys) {
  17349. auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
  17350. auto pn = stack::pop_n(base_t::lua_state(), static_cast<int>(sizeof...(Keys) - 2));
  17351. traverse_set_deep<top_level, true>(std::forward<Keys>(keys)...);
  17352. return *this;
  17353. }
  17354. template <typename... Args>
  17355. basic_table_core& raw_set(Args&&... args) {
  17356. tuple_set<true>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
  17357. return *this;
  17358. }
  17359. template <typename T>
  17360. basic_table_core& set_usertype(usertype<T>& user) {
  17361. return set_usertype(usertype_traits<T>::name(), user);
  17362. }
  17363. template <typename Key, typename T>
  17364. basic_table_core& set_usertype(Key&& key, usertype<T>& user) {
  17365. return set(std::forward<Key>(key), user);
  17366. }
  17367. template <typename Class, typename... Args>
  17368. basic_table_core& new_usertype(const std::string& name, Args&&... args) {
  17369. usertype<Class> utype(std::forward<Args>(args)...);
  17370. set_usertype(name, utype);
  17371. return *this;
  17372. }
  17373. template <typename Class, typename CTor0, typename... CTor, typename... Args>
  17374. basic_table_core& new_usertype(const std::string& name, Args&&... args) {
  17375. constructors<types<CTor0, CTor...>> ctor{};
  17376. return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
  17377. }
  17378. template <typename Class, typename... CArgs, typename... Args>
  17379. basic_table_core& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
  17380. usertype<Class> utype(ctor, std::forward<Args>(args)...);
  17381. set_usertype(name, utype);
  17382. return *this;
  17383. }
  17384. template <typename Class, typename... Args>
  17385. basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) {
  17386. simple_usertype<Class> utype(base_t::lua_state(), std::forward<Args>(args)...);
  17387. set_usertype(name, utype);
  17388. return *this;
  17389. }
  17390. template <typename Class, typename CTor0, typename... CTor, typename... Args>
  17391. basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) {
  17392. constructors<types<CTor0, CTor...>> ctor{};
  17393. return new_simple_usertype<Class>(name, ctor, std::forward<Args>(args)...);
  17394. }
  17395. template <typename Class, typename... CArgs, typename... Args>
  17396. basic_table_core& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
  17397. simple_usertype<Class> utype(base_t::lua_state(), ctor, std::forward<Args>(args)...);
  17398. set_usertype(name, utype);
  17399. return *this;
  17400. }
  17401. template <typename Class, typename... Args>
  17402. simple_usertype<Class> create_simple_usertype(Args&&... args) {
  17403. simple_usertype<Class> utype(base_t::lua_state(), std::forward<Args>(args)...);
  17404. return utype;
  17405. }
  17406. template <typename Class, typename CTor0, typename... CTor, typename... Args>
  17407. simple_usertype<Class> create_simple_usertype(Args&&... args) {
  17408. constructors<types<CTor0, CTor...>> ctor{};
  17409. return create_simple_usertype<Class>(ctor, std::forward<Args>(args)...);
  17410. }
  17411. template <typename Class, typename... CArgs, typename... Args>
  17412. simple_usertype<Class> create_simple_usertype(constructors<CArgs...> ctor, Args&&... args) {
  17413. simple_usertype<Class> utype(base_t::lua_state(), ctor, std::forward<Args>(args)...);
  17414. return utype;
  17415. }
  17416. template <bool read_only = true, typename... Args>
  17417. table new_enum(const string_view& name, Args&&... args) {
  17418. table target = create_with(std::forward<Args>(args)...);
  17419. if (read_only) {
  17420. table x = create_with(
  17421. meta_function::new_index, detail::fail_on_newindex,
  17422. meta_function::index, target);
  17423. table shim = create_named(name, metatable_key, x);
  17424. return shim;
  17425. }
  17426. else {
  17427. set(name, target);
  17428. return target;
  17429. }
  17430. }
  17431. template <typename T, bool read_only = true>
  17432. table new_enum(const string_view& name, std::initializer_list<std::pair<string_view, T>> items) {
  17433. table target = create(static_cast<int>(items.size()), static_cast<int>(0));
  17434. for (const auto& kvp : items) {
  17435. target.set(kvp.first, kvp.second);
  17436. }
  17437. if (read_only) {
  17438. table x = create_with(
  17439. meta_function::new_index, detail::fail_on_newindex,
  17440. meta_function::index, target);
  17441. table shim = create_named(name, metatable_key, x);
  17442. return shim;
  17443. }
  17444. else {
  17445. set(name, target);
  17446. return target;
  17447. }
  17448. }
  17449. template <typename Fx>
  17450. void for_each(Fx&& fx) const {
  17451. typedef meta::is_invokable<Fx(std::pair<object, object>)> is_paired;
  17452. for_each(is_paired(), std::forward<Fx>(fx));
  17453. }
  17454. size_t size() const {
  17455. auto pp = stack::push_pop(*this);
  17456. lua_len(base_t::lua_state(), -1);
  17457. return stack::pop<size_t>(base_t::lua_state());
  17458. }
  17459. bool empty() const {
  17460. return cbegin() == cend();
  17461. }
  17462. template <typename T>
  17463. proxy<basic_table_core&, T> operator[](T&& key) & {
  17464. return proxy<basic_table_core&, T>(*this, std::forward<T>(key));
  17465. }
  17466. template <typename T>
  17467. proxy<const basic_table_core&, T> operator[](T&& key) const& {
  17468. return proxy<const basic_table_core&, T>(*this, std::forward<T>(key));
  17469. }
  17470. template <typename T>
  17471. proxy<basic_table_core, T> operator[](T&& key) && {
  17472. return proxy<basic_table_core, T>(*this, std::forward<T>(key));
  17473. }
  17474. template <typename Sig, typename Key, typename... Args>
  17475. basic_table_core& set_function(Key&& key, Args&&... args) {
  17476. set_fx(types<Sig>(), std::forward<Key>(key), std::forward<Args>(args)...);
  17477. return *this;
  17478. }
  17479. template <typename Key, typename... Args>
  17480. basic_table_core& set_function(Key&& key, Args&&... args) {
  17481. set_fx(types<>(), std::forward<Key>(key), std::forward<Args>(args)...);
  17482. return *this;
  17483. }
  17484. template <typename... Args>
  17485. basic_table_core& add(Args&&... args) {
  17486. auto pp = stack::push_pop(*this);
  17487. (void)detail::swallow{ 0,
  17488. (stack::set_ref(base_t::lua_state(), std::forward<Args>(args)), 0)... };
  17489. return *this;
  17490. }
  17491. private:
  17492. template <typename R, typename... Args, typename Fx, typename Key, typename = std::result_of_t<Fx(Args...)>>
  17493. void set_fx(types<R(Args...)>, Key&& key, Fx&& fx) {
  17494. set_resolved_function<R(Args...)>(std::forward<Key>(key), std::forward<Fx>(fx));
  17495. }
  17496. template <typename Fx, typename Key, meta::enable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
  17497. void set_fx(types<>, Key&& key, Fx&& fx) {
  17498. set(std::forward<Key>(key), std::forward<Fx>(fx));
  17499. }
  17500. template <typename Fx, typename Key, typename... Args, meta::disable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
  17501. void set_fx(types<>, Key&& key, Fx&& fx, Args&&... args) {
  17502. set(std::forward<Key>(key), as_function_reference(std::forward<Fx>(fx), std::forward<Args>(args)...));
  17503. }
  17504. template <typename... Sig, typename... Args, typename Key>
  17505. void set_resolved_function(Key&& key, Args&&... args) {
  17506. set(std::forward<Key>(key), as_function_reference<function_sig<Sig...>>(std::forward<Args>(args)...));
  17507. }
  17508. public:
  17509. static inline table create(lua_State* L, int narr = 0, int nrec = 0) {
  17510. lua_createtable(L, narr, nrec);
  17511. table result(L);
  17512. lua_pop(L, 1);
  17513. return result;
  17514. }
  17515. template <typename Key, typename Value, typename... Args>
  17516. static inline table create(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  17517. lua_createtable(L, narr, nrec);
  17518. table result(L);
  17519. result.set(std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  17520. lua_pop(L, 1);
  17521. return result;
  17522. }
  17523. template <typename... Args>
  17524. static inline table create_with(lua_State* L, Args&&... args) {
  17525. static_assert(sizeof...(Args) % 2 == 0, "You must have an even number of arguments for a key, value ... list.");
  17526. static const int narr = static_cast<int>(meta::count_2_for_pack<std::is_integral, Args...>::value);
  17527. return create(L, narr, static_cast<int>((sizeof...(Args) / 2) - narr), std::forward<Args>(args)...);
  17528. }
  17529. table create(int narr = 0, int nrec = 0) {
  17530. return create(base_t::lua_state(), narr, nrec);
  17531. }
  17532. template <typename Key, typename Value, typename... Args>
  17533. table create(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  17534. return create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  17535. }
  17536. template <typename Name>
  17537. table create(Name&& name, int narr = 0, int nrec = 0) {
  17538. table x = create(base_t::lua_state(), narr, nrec);
  17539. this->set(std::forward<Name>(name), x);
  17540. return x;
  17541. }
  17542. template <typename Name, typename Key, typename Value, typename... Args>
  17543. table create(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  17544. table x = create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  17545. this->set(std::forward<Name>(name), x);
  17546. return x;
  17547. }
  17548. template <typename... Args>
  17549. table create_with(Args&&... args) {
  17550. return create_with(base_t::lua_state(), std::forward<Args>(args)...);
  17551. }
  17552. template <typename Name, typename... Args>
  17553. table create_named(Name&& name, Args&&... args) {
  17554. static const int narr = static_cast<int>(meta::count_2_for_pack<std::is_integral, Args...>::value);
  17555. return create(std::forward<Name>(name), narr, (sizeof...(Args) / 2) - narr, std::forward<Args>(args)...);
  17556. }
  17557. };
  17558. } // namespace sol
  17559. // end of sol/table_core.hpp
  17560. namespace sol {
  17561. typedef table_core<false> table;
  17562. namespace stack {
  17563. template <>
  17564. struct getter<metatable_t> {
  17565. static table get(lua_State* L, int index = -1) {
  17566. if (lua_getmetatable(L, index) == 0) {
  17567. return table(L, ref_index(LUA_REFNIL));
  17568. }
  17569. return table(L, -1);
  17570. }
  17571. };
  17572. } // namespace stack
  17573. } // namespace sol
  17574. // end of sol/table.hpp
  17575. // beginning of sol/environment.hpp
  17576. namespace sol {
  17577. template <typename base_type>
  17578. struct basic_environment : basic_table<base_type> {
  17579. private:
  17580. typedef basic_table<base_type> base_t;
  17581. public:
  17582. using base_t::lua_state;
  17583. basic_environment() noexcept = default;
  17584. basic_environment(const basic_environment&) = default;
  17585. basic_environment(basic_environment&&) = default;
  17586. basic_environment& operator=(const basic_environment&) = default;
  17587. basic_environment& operator=(basic_environment&&) = default;
  17588. basic_environment(const stack_reference& r)
  17589. : basic_environment(r.lua_state(), r.stack_index()) {
  17590. }
  17591. basic_environment(stack_reference&& r)
  17592. : basic_environment(r.lua_state(), r.stack_index()) {
  17593. }
  17594. basic_environment(lua_State* L, new_table nt)
  17595. : base_t(L, std::move(nt)) {
  17596. }
  17597. template <bool b>
  17598. basic_environment(lua_State* L, new_table t, const basic_reference<b>& fallback)
  17599. : basic_environment(L, std::move(t)) {
  17600. stack_table mt(L, new_table(0, 1));
  17601. mt.set(meta_function::index, fallback);
  17602. this->set(metatable_key, mt);
  17603. mt.pop();
  17604. }
  17605. basic_environment(env_t, const stack_reference& extraction_target)
  17606. : base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) {
  17607. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17608. constructor_handler handler{};
  17609. stack::check<env_t>(this->lua_state(), -1, handler);
  17610. #endif // Safety
  17611. lua_pop(this->lua_state(), 2);
  17612. }
  17613. template <bool b>
  17614. basic_environment(env_t, const basic_reference<b>& extraction_target)
  17615. : base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) {
  17616. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17617. constructor_handler handler{};
  17618. stack::check<env_t>(this->lua_state(), -1, handler);
  17619. #endif // Safety
  17620. lua_pop(this->lua_state(), 2);
  17621. }
  17622. basic_environment(lua_State* L, int index = -1)
  17623. : base_t(detail::no_safety, L, index) {
  17624. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17625. constructor_handler handler{};
  17626. stack::check<basic_environment>(L, index, handler);
  17627. #endif // Safety
  17628. }
  17629. basic_environment(lua_State* L, ref_index index)
  17630. : base_t(detail::no_safety, L, index) {
  17631. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17632. auto pp = stack::push_pop(*this);
  17633. constructor_handler handler{};
  17634. stack::check<basic_environment>(L, -1, handler);
  17635. #endif // Safety
  17636. }
  17637. template <typename T, meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_environment>>, meta::neg<std::is_same<base_type, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  17638. basic_environment(T&& r) noexcept
  17639. : base_t(detail::no_safety, std::forward<T>(r)) {
  17640. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17641. if (!is_environment<meta::unqualified_t<T>>::value) {
  17642. auto pp = stack::push_pop(*this);
  17643. constructor_handler handler{};
  17644. stack::check<basic_environment>(lua_state(), -1, handler);
  17645. }
  17646. #endif // Safety
  17647. }
  17648. basic_environment(lua_nil_t r) noexcept
  17649. : base_t(detail::no_safety, r) {
  17650. }
  17651. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  17652. basic_environment(lua_State* L, T&& r) noexcept
  17653. : base_t(detail::no_safety, L, std::forward<T>(r)) {
  17654. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  17655. if (!is_environment<meta::unqualified_t<T>>::value) {
  17656. auto pp = stack::push_pop(*this);
  17657. constructor_handler handler{};
  17658. stack::check<basic_environment>(lua_state(), -1, handler);
  17659. }
  17660. #endif // Safety
  17661. }
  17662. template <typename T>
  17663. void set_on(const T& target) const {
  17664. lua_State* L = target.lua_state();
  17665. auto pp = stack::push_pop(target);
  17666. #if SOL_LUA_VERSION < 502
  17667. // Use lua_setfenv
  17668. this->push();
  17669. lua_setfenv(L, -2);
  17670. #else
  17671. // Use upvalues as explained in Lua 5.2 and beyond's manual
  17672. this->push();
  17673. const char* name = lua_setupvalue(L, -2, 1);
  17674. if (name == nullptr) {
  17675. this->pop();
  17676. }
  17677. #endif
  17678. }
  17679. };
  17680. template <typename T, typename E>
  17681. void set_environment(const basic_environment<E>& env, const T& target) {
  17682. env.set_on(target);
  17683. }
  17684. template <typename E = reference, typename T>
  17685. basic_environment<E> get_environment(const T& target) {
  17686. lua_State* L = target.lua_state();
  17687. auto pp = stack::pop_n(L, stack::push_environment_of(target));
  17688. return basic_environment<E>(L, -1);
  17689. }
  17690. struct this_environment {
  17691. optional<environment> env;
  17692. this_environment()
  17693. : env(nullopt) {
  17694. }
  17695. this_environment(environment e)
  17696. : env(std::move(e)) {
  17697. }
  17698. this_environment(const this_environment&) = default;
  17699. this_environment(this_environment&&) = default;
  17700. this_environment& operator=(const this_environment&) = default;
  17701. this_environment& operator=(this_environment&&) = default;
  17702. explicit operator bool() const {
  17703. return static_cast<bool>(env);
  17704. }
  17705. operator optional<environment>&() {
  17706. return env;
  17707. }
  17708. operator const optional<environment>&() const {
  17709. return env;
  17710. }
  17711. operator environment&() {
  17712. return env.value();
  17713. }
  17714. operator const environment&() const {
  17715. return env.value();
  17716. }
  17717. };
  17718. namespace stack {
  17719. template <>
  17720. struct getter<env_t> {
  17721. static environment get(lua_State* L, int index, record& tracking) {
  17722. tracking.use(1);
  17723. return get_environment(stack_reference(L, raw_index(index)));
  17724. }
  17725. };
  17726. template <>
  17727. struct getter<this_environment> {
  17728. static this_environment get(lua_State* L, int, record& tracking) {
  17729. tracking.use(0);
  17730. lua_Debug info;
  17731. // Level 0 means current function (this C function, which may or may not be useful for us?)
  17732. // Level 1 means next call frame up the stack. (Can be nothing if function called directly from C++ with lua_p/call)
  17733. int pre_stack_size = lua_gettop(L);
  17734. if (lua_getstack(L, 1, &info) != 1) {
  17735. if (lua_getstack(L, 0, &info) != 1) {
  17736. lua_settop(L, pre_stack_size);
  17737. return this_environment();
  17738. }
  17739. }
  17740. if (lua_getinfo(L, "f", &info) == 0) {
  17741. lua_settop(L, pre_stack_size);
  17742. return this_environment();
  17743. }
  17744. stack_reference f(L, -1);
  17745. environment env(env_key, f);
  17746. if (!env.valid()) {
  17747. lua_settop(L, pre_stack_size);
  17748. return this_environment();
  17749. }
  17750. return this_environment(std::move(env));
  17751. }
  17752. };
  17753. } // namespace stack
  17754. } // namespace sol
  17755. // end of sol/environment.hpp
  17756. // beginning of sol/load_result.hpp
  17757. namespace sol {
  17758. struct load_result : public proxy_base<load_result> {
  17759. private:
  17760. lua_State* L;
  17761. int index;
  17762. int returncount;
  17763. int popcount;
  17764. load_status err;
  17765. template <typename T>
  17766. decltype(auto) tagged_get(types<optional<T>>) const {
  17767. if (!valid()) {
  17768. return optional<T>(nullopt);
  17769. }
  17770. return stack::get<optional<T>>(L, index);
  17771. }
  17772. template <typename T>
  17773. decltype(auto) tagged_get(types<T>) const {
  17774. #if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES != 0
  17775. if (!valid()) {
  17776. type_panic_c_str(L, index, type_of(L, index), type::none);
  17777. }
  17778. #endif // Check Argument Safety
  17779. return stack::get<T>(L, index);
  17780. }
  17781. optional<error> tagged_get(types<optional<error>>) const {
  17782. if (valid()) {
  17783. return nullopt;
  17784. }
  17785. return error(detail::direct_error, stack::get<std::string>(L, index));
  17786. }
  17787. error tagged_get(types<error>) const {
  17788. #if defined(SOL_SAFE_PROXIES) && SOL_SAFE_PROXIES != 0
  17789. if (valid()) {
  17790. type_panic_c_str(L, index, type_of(L, index), type::none, "expecting an error type (a string, from Lua)");
  17791. }
  17792. #endif // Check Argument Safety
  17793. return error(detail::direct_error, stack::get<std::string>(L, index));
  17794. }
  17795. public:
  17796. load_result() = default;
  17797. load_result(lua_State* Ls, int stackindex = -1, int retnum = 0, int popnum = 0, load_status lerr = load_status::ok) noexcept
  17798. : L(Ls), index(stackindex), returncount(retnum), popcount(popnum), err(lerr) {
  17799. }
  17800. load_result(const load_result&) = default;
  17801. load_result& operator=(const load_result&) = default;
  17802. load_result(load_result&& o) noexcept
  17803. : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) {
  17804. // Must be manual, otherwise destructor will screw us
  17805. // return count being 0 is enough to keep things clean
  17806. // but we will be thorough
  17807. o.L = nullptr;
  17808. o.index = 0;
  17809. o.returncount = 0;
  17810. o.popcount = 0;
  17811. o.err = load_status::syntax;
  17812. }
  17813. load_result& operator=(load_result&& o) noexcept {
  17814. L = o.L;
  17815. index = o.index;
  17816. returncount = o.returncount;
  17817. popcount = o.popcount;
  17818. err = o.err;
  17819. // Must be manual, otherwise destructor will screw us
  17820. // return count being 0 is enough to keep things clean
  17821. // but we will be thorough
  17822. o.L = nullptr;
  17823. o.index = 0;
  17824. o.returncount = 0;
  17825. o.popcount = 0;
  17826. o.err = load_status::syntax;
  17827. return *this;
  17828. }
  17829. load_status status() const noexcept {
  17830. return err;
  17831. }
  17832. bool valid() const noexcept {
  17833. return status() == load_status::ok;
  17834. }
  17835. template <typename T>
  17836. T get() const {
  17837. return tagged_get(types<meta::unqualified_t<T>>());
  17838. }
  17839. template <typename... Ret, typename... Args>
  17840. decltype(auto) call(Args&&... args) {
  17841. #if !defined(__clang__) && defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 191200000
  17842. // MSVC is ass sometimes
  17843. return get<protected_function>().call<Ret...>(std::forward<Args>(args)...);
  17844. #else
  17845. return get<protected_function>().template call<Ret...>(std::forward<Args>(args)...);
  17846. #endif
  17847. }
  17848. template <typename... Args>
  17849. decltype(auto) operator()(Args&&... args) {
  17850. return call<>(std::forward<Args>(args)...);
  17851. }
  17852. lua_State* lua_state() const noexcept {
  17853. return L;
  17854. };
  17855. int stack_index() const noexcept {
  17856. return index;
  17857. };
  17858. ~load_result() {
  17859. stack::remove(L, index, popcount);
  17860. }
  17861. };
  17862. } // namespace sol
  17863. // end of sol/load_result.hpp
  17864. // beginning of sol/state_handling.hpp
  17865. #if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS
  17866. #endif
  17867. namespace sol {
  17868. inline void register_main_thread(lua_State* L) {
  17869. #if SOL_LUA_VERSION < 502
  17870. if (L == nullptr) {
  17871. lua_pushnil(L);
  17872. lua_setglobal(L, detail::default_main_thread_name());
  17873. return;
  17874. }
  17875. lua_pushthread(L);
  17876. lua_setglobal(L, detail::default_main_thread_name());
  17877. #else
  17878. (void)L;
  17879. #endif
  17880. }
  17881. inline int default_at_panic(lua_State* L) {
  17882. #if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS
  17883. (void)L;
  17884. return -1;
  17885. #else
  17886. size_t messagesize;
  17887. const char* message = lua_tolstring(L, -1, &messagesize);
  17888. if (message) {
  17889. std::string err(message, messagesize);
  17890. lua_settop(L, 0);
  17891. #if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS
  17892. std::cerr << "[sol2] An error occurred and panic has been invoked: ";
  17893. std::cerr << err;
  17894. std::cerr << std::endl;
  17895. #endif
  17896. throw error(err);
  17897. }
  17898. lua_settop(L, 0);
  17899. throw error(std::string("An unexpected error occurred and panic has been invoked"));
  17900. #endif // Printing Errors
  17901. }
  17902. inline int default_traceback_error_handler(lua_State* L) {
  17903. std::string msg = "An unknown error has triggered the default error handler";
  17904. optional<string_view> maybetopmsg = stack::check_get<string_view>(L, 1);
  17905. if (maybetopmsg) {
  17906. const string_view& topmsg = maybetopmsg.value();
  17907. msg.assign(topmsg.data(), topmsg.size());
  17908. }
  17909. luaL_traceback(L, L, msg.c_str(), 1);
  17910. optional<string_view> maybetraceback = stack::check_get<string_view>(L, -1);
  17911. if (maybetraceback) {
  17912. const string_view& traceback = maybetraceback.value();
  17913. msg.assign(traceback.data(), traceback.size());
  17914. }
  17915. #if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS
  17916. //std::cerr << "[sol2] An error occurred and was caught in traceback: ";
  17917. //std::cerr << msg;
  17918. //std::cerr << std::endl;
  17919. #endif // Printing
  17920. return stack::push(L, msg);
  17921. }
  17922. inline void set_default_state(lua_State* L, lua_CFunction panic_function = &default_at_panic, lua_CFunction traceback_function = c_call<decltype(&default_traceback_error_handler), &default_traceback_error_handler>, exception_handler_function exf = detail::default_exception_handler) {
  17923. lua_atpanic(L, panic_function);
  17924. protected_function::set_default_handler(object(L, in_place, traceback_function));
  17925. set_default_exception_handler(L, exf);
  17926. register_main_thread(L);
  17927. stack::luajit_exception_handler(L);
  17928. }
  17929. inline std::size_t total_memory_used(lua_State* L) {
  17930. std::size_t kb = lua_gc(L, LUA_GCCOUNT, 0);
  17931. kb *= 1024;
  17932. kb += lua_gc(L, LUA_GCCOUNTB, 0);
  17933. return kb;
  17934. }
  17935. inline protected_function_result script_pass_on_error(lua_State*, protected_function_result result) {
  17936. return result;
  17937. }
  17938. inline protected_function_result script_throw_on_error(lua_State*L, protected_function_result result) {
  17939. type t = type_of(L, result.stack_index());
  17940. std::string err = "sol: ";
  17941. err += to_string(result.status());
  17942. err += " error";
  17943. #if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS)
  17944. std::exception_ptr eptr = std::current_exception();
  17945. if (eptr) {
  17946. err += " with a ";
  17947. try {
  17948. std::rethrow_exception(eptr);
  17949. }
  17950. catch (const std::exception& ex) {
  17951. err += "std::exception -- ";
  17952. err.append(ex.what());
  17953. }
  17954. catch (const std::string& message) {
  17955. err += "thrown message -- ";
  17956. err.append(message);
  17957. }
  17958. catch (const char* message) {
  17959. err += "thrown message -- ";
  17960. err.append(message);
  17961. }
  17962. catch (...) {
  17963. err.append("thrown but unknown type, cannot serialize into error message");
  17964. }
  17965. }
  17966. #endif // serialize exception information if possible
  17967. if (t == type::string) {
  17968. err += ": ";
  17969. string_view serr = stack::get<string_view>(L, result.stack_index());
  17970. err.append(serr.data(), serr.size());
  17971. }
  17972. #if defined(SOL_PRINT_ERRORS) && SOL_PRINT_ERRORS
  17973. std::cerr << "[sol2] An error occurred and has been passed to an error handler: ";
  17974. std::cerr << err;
  17975. std::cerr << std::endl;
  17976. #endif
  17977. // replacing information of stack error into pfr
  17978. int target = result.stack_index();
  17979. if (result.pop_count() > 0) {
  17980. stack::remove(L, target, result.pop_count());
  17981. }
  17982. stack::push(L, err);
  17983. int top = lua_gettop(L);
  17984. int towards = top - target;
  17985. if (towards != 0) {
  17986. lua_rotate(L, top, towards);
  17987. }
  17988. #if defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS
  17989. return result;
  17990. #else
  17991. // just throw our error
  17992. throw error(detail::direct_error, err);
  17993. #endif // If exceptions are allowed
  17994. }
  17995. inline protected_function_result script_default_on_error(lua_State* L, protected_function_result pfr) {
  17996. #if defined(SOL_DEFAULT_PASS_ON_ERROR) && SOL_DEFAULT_PASS_ON_ERROR
  17997. return script_pass_on_error(L, std::move(pfr));
  17998. #else
  17999. return script_throw_on_error(L, std::move(pfr));
  18000. #endif
  18001. }
  18002. } // namespace sol
  18003. // end of sol/state_handling.hpp
  18004. namespace sol {
  18005. class state_view {
  18006. private:
  18007. lua_State* L;
  18008. table reg;
  18009. global_table global;
  18010. optional<object> is_loaded_package(const std::string& key) {
  18011. auto loaded = reg.traverse_get<optional<object>>("_LOADED", key);
  18012. bool is53mod = loaded && !(loaded->is<bool>() && !loaded->as<bool>());
  18013. if (is53mod)
  18014. return loaded;
  18015. #if SOL_LUA_VERSION <= 501
  18016. auto loaded51 = global.traverse_get<optional<object>>("package", "loaded", key);
  18017. bool is51mod = loaded51 && !(loaded51->is<bool>() && !loaded51->as<bool>());
  18018. if (is51mod)
  18019. return loaded51;
  18020. #endif
  18021. return nullopt;
  18022. }
  18023. template <typename T>
  18024. void ensure_package(const std::string& key, T&& sr) {
  18025. #if SOL_LUA_VERSION <= 501
  18026. auto pkg = global["package"];
  18027. if (!pkg.valid()) {
  18028. pkg = create_table_with("loaded", create_table_with(key, sr));
  18029. }
  18030. else {
  18031. auto ld = pkg["loaded"];
  18032. if (!ld.valid()) {
  18033. ld = create_table_with(key, sr);
  18034. }
  18035. else {
  18036. ld[key] = sr;
  18037. }
  18038. }
  18039. #endif
  18040. auto loaded = reg["_LOADED"];
  18041. if (!loaded.valid()) {
  18042. loaded = create_table_with(key, sr);
  18043. }
  18044. else {
  18045. loaded[key] = sr;
  18046. }
  18047. }
  18048. template <typename Fx>
  18049. object require_core(const std::string& key, Fx&& action, bool create_global = true) {
  18050. optional<object> loaded = is_loaded_package(key);
  18051. if (loaded && loaded->valid())
  18052. return std::move(*loaded);
  18053. action();
  18054. stack_reference sr(L, -1);
  18055. if (create_global)
  18056. set(key, sr);
  18057. ensure_package(key, sr);
  18058. return stack::pop<object>(L);
  18059. }
  18060. public:
  18061. typedef global_table::iterator iterator;
  18062. typedef global_table::const_iterator const_iterator;
  18063. state_view(lua_State* Ls)
  18064. : L(Ls), reg(Ls, LUA_REGISTRYINDEX), global(Ls, detail::global_) {
  18065. }
  18066. state_view(this_state Ls)
  18067. : state_view(Ls.L) {
  18068. }
  18069. lua_State* lua_state() const {
  18070. return L;
  18071. }
  18072. template <typename... Args>
  18073. void open_libraries(Args&&... args) {
  18074. static_assert(meta::all_same<lib, Args...>::value, "all types must be libraries");
  18075. if (sizeof...(args) == 0) {
  18076. luaL_openlibs(L);
  18077. return;
  18078. }
  18079. lib libraries[1 + sizeof...(args)] = {lib::count, std::forward<Args>(args)...};
  18080. for (auto&& library : libraries) {
  18081. switch (library) {
  18082. #if SOL_LUA_VERSION <= 501 && defined(SOL_LUAJIT)
  18083. case lib::coroutine:
  18084. #endif // luajit opens coroutine base stuff
  18085. case lib::base:
  18086. luaL_requiref(L, "base", luaopen_base, 1);
  18087. lua_pop(L, 1);
  18088. break;
  18089. case lib::package:
  18090. luaL_requiref(L, "package", luaopen_package, 1);
  18091. lua_pop(L, 1);
  18092. break;
  18093. #if !defined(SOL_LUAJIT)
  18094. case lib::coroutine:
  18095. #if SOL_LUA_VERSION > 501
  18096. luaL_requiref(L, "coroutine", luaopen_coroutine, 1);
  18097. lua_pop(L, 1);
  18098. #endif // Lua 5.2+ only
  18099. break;
  18100. #endif // Not LuaJIT - comes builtin
  18101. case lib::string:
  18102. luaL_requiref(L, "string", luaopen_string, 1);
  18103. lua_pop(L, 1);
  18104. break;
  18105. case lib::table:
  18106. luaL_requiref(L, "table", luaopen_table, 1);
  18107. lua_pop(L, 1);
  18108. break;
  18109. case lib::math:
  18110. luaL_requiref(L, "math", luaopen_math, 1);
  18111. lua_pop(L, 1);
  18112. break;
  18113. case lib::bit32:
  18114. #ifdef SOL_LUAJIT
  18115. luaL_requiref(L, "bit32", luaopen_bit, 1);
  18116. lua_pop(L, 1);
  18117. #elif (SOL_LUA_VERSION == 502) || defined(LUA_COMPAT_BITLIB) || defined(LUA_COMPAT_5_2)
  18118. luaL_requiref(L, "bit32", luaopen_bit32, 1);
  18119. lua_pop(L, 1);
  18120. #else
  18121. #endif // Lua 5.2 only (deprecated in 5.3 (503)) (Can be turned on with Compat flags)
  18122. break;
  18123. case lib::io:
  18124. luaL_requiref(L, "io", luaopen_io, 1);
  18125. lua_pop(L, 1);
  18126. break;
  18127. case lib::os:
  18128. luaL_requiref(L, "os", luaopen_os, 1);
  18129. lua_pop(L, 1);
  18130. break;
  18131. case lib::debug:
  18132. luaL_requiref(L, "debug", luaopen_debug, 1);
  18133. lua_pop(L, 1);
  18134. break;
  18135. case lib::utf8:
  18136. #if SOL_LUA_VERSION > 502 && !defined(SOL_LUAJIT)
  18137. luaL_requiref(L, "utf8", luaopen_utf8, 1);
  18138. lua_pop(L, 1);
  18139. #endif // Lua 5.3+ only
  18140. break;
  18141. case lib::ffi:
  18142. #ifdef SOL_LUAJIT
  18143. luaL_requiref(L, "ffi", luaopen_ffi, 1);
  18144. lua_pop(L, 1);
  18145. #endif // LuaJIT only
  18146. break;
  18147. case lib::jit:
  18148. #ifdef SOL_LUAJIT
  18149. luaL_requiref(L, "jit", luaopen_jit, 0);
  18150. lua_pop(L, 1);
  18151. #endif // LuaJIT Only
  18152. break;
  18153. case lib::count:
  18154. default:
  18155. break;
  18156. }
  18157. }
  18158. }
  18159. object require(const std::string& key, lua_CFunction open_function, bool create_global = true) {
  18160. luaL_requiref(L, key.c_str(), open_function, create_global ? 1 : 0);
  18161. return stack::pop<object>(L);
  18162. }
  18163. object require_script(const std::string& key, const string_view& code, bool create_global = true, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18164. auto action = [this, &code, &chunkname, &mode]() {
  18165. stack::script(L, code, chunkname, mode);
  18166. };
  18167. return require_core(key, action, create_global);
  18168. }
  18169. object require_file(const std::string& key, const std::string& filename, bool create_global = true, load_mode mode = load_mode::any) {
  18170. auto action = [this, &filename, &mode]() {
  18171. stack::script_file(L, filename, mode);
  18172. };
  18173. return require_core(key, action, create_global);
  18174. }
  18175. template <typename E>
  18176. protected_function_result do_string(const string_view& code, const basic_environment<E>& env, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18177. detail::typical_chunk_name_t basechunkname = {};
  18178. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  18179. load_status x = static_cast<load_status>(luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()));
  18180. if (x != load_status::ok) {
  18181. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  18182. }
  18183. stack_aligned_protected_function pf(L, -1);
  18184. set_environment(env, pf);
  18185. return pf();
  18186. }
  18187. template <typename E>
  18188. protected_function_result do_file(const std::string& filename, const basic_environment<E>& env, load_mode mode = load_mode::any) {
  18189. load_status x = static_cast<load_status>(luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()));
  18190. if (x != load_status::ok) {
  18191. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  18192. }
  18193. stack_aligned_protected_function pf(L, -1);
  18194. set_environment(env, pf);
  18195. return pf();
  18196. }
  18197. protected_function_result do_string(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18198. detail::typical_chunk_name_t basechunkname = {};
  18199. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  18200. load_status x = static_cast<load_status>(luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()));
  18201. if (x != load_status::ok) {
  18202. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  18203. }
  18204. stack_aligned_protected_function pf(L, -1);
  18205. return pf();
  18206. }
  18207. protected_function_result do_file(const std::string& filename, load_mode mode = load_mode::any) {
  18208. load_status x = static_cast<load_status>(luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()));
  18209. if (x != load_status::ok) {
  18210. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  18211. }
  18212. stack_aligned_protected_function pf(L, -1);
  18213. return pf();
  18214. }
  18215. template <typename Fx, meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>, meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  18216. protected_function_result safe_script(const string_view& code, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18217. protected_function_result pfr = do_string(code, chunkname, mode);
  18218. if (!pfr.valid()) {
  18219. return on_error(L, std::move(pfr));
  18220. }
  18221. return pfr;
  18222. }
  18223. template <typename Fx, typename E>
  18224. protected_function_result safe_script(const string_view& code, const basic_environment<E>& env, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18225. protected_function_result pfr = do_string(code, env, chunkname, mode);
  18226. if (!pfr.valid()) {
  18227. return on_error(L, std::move(pfr));
  18228. }
  18229. return pfr;
  18230. }
  18231. template <typename E>
  18232. protected_function_result safe_script(const string_view& code, const basic_environment<E>& env, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18233. return safe_script(code, env, script_default_on_error, chunkname, mode);
  18234. }
  18235. protected_function_result safe_script(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18236. return safe_script(code, script_default_on_error, chunkname, mode);
  18237. }
  18238. template <typename Fx, meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>, meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  18239. protected_function_result safe_script_file(const std::string& filename, Fx&& on_error, load_mode mode = load_mode::any) {
  18240. protected_function_result pfr = do_file(filename, mode);
  18241. if (!pfr.valid()) {
  18242. return on_error(L, std::move(pfr));
  18243. }
  18244. return pfr;
  18245. }
  18246. template <typename Fx, typename E>
  18247. protected_function_result safe_script_file(const std::string& filename, const basic_environment<E>& env, Fx&& on_error, load_mode mode = load_mode::any) {
  18248. protected_function_result pfr = do_file(filename, env, mode);
  18249. if (!pfr.valid()) {
  18250. return on_error(L, std::move(pfr));
  18251. }
  18252. return pfr;
  18253. }
  18254. template <typename E>
  18255. protected_function_result safe_script_file(const std::string& filename, const basic_environment<E>& env, load_mode mode = load_mode::any) {
  18256. return safe_script_file(filename, env, script_default_on_error, mode);
  18257. }
  18258. protected_function_result safe_script_file(const std::string& filename, load_mode mode = load_mode::any) {
  18259. return safe_script_file(filename, script_default_on_error, mode);
  18260. }
  18261. template <typename E>
  18262. unsafe_function_result unsafe_script(const string_view& code, const basic_environment<E>& env, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18263. detail::typical_chunk_name_t basechunkname = {};
  18264. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  18265. int index = lua_gettop(L);
  18266. if (luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str())) {
  18267. lua_error(L);
  18268. }
  18269. set_environment(env, stack_reference(L, raw_index(index + 1)));
  18270. if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
  18271. lua_error(L);
  18272. }
  18273. int postindex = lua_gettop(L);
  18274. int returns = postindex - index;
  18275. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  18276. }
  18277. unsafe_function_result unsafe_script(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18278. int index = lua_gettop(L);
  18279. stack::script(L, code, chunkname, mode);
  18280. int postindex = lua_gettop(L);
  18281. int returns = postindex - index;
  18282. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  18283. }
  18284. template <typename E>
  18285. unsafe_function_result unsafe_script_file(const std::string& filename, const basic_environment<E>& env, load_mode mode = load_mode::any) {
  18286. int index = lua_gettop(L);
  18287. if (luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str())) {
  18288. lua_error(L);
  18289. }
  18290. set_environment(env, stack_reference(L, raw_index(index + 1)));
  18291. if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
  18292. lua_error(L);
  18293. }
  18294. int postindex = lua_gettop(L);
  18295. int returns = postindex - index;
  18296. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  18297. }
  18298. unsafe_function_result unsafe_script_file(const std::string& filename, load_mode mode = load_mode::any) {
  18299. int index = lua_gettop(L);
  18300. stack::script_file(L, filename, mode);
  18301. int postindex = lua_gettop(L);
  18302. int returns = postindex - index;
  18303. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  18304. }
  18305. template <typename Fx, meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>, meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  18306. protected_function_result script(const string_view& code, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18307. return safe_script(code, std::forward<Fx>(on_error), chunkname, mode);
  18308. }
  18309. template <typename Fx, meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>, meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  18310. protected_function_result script_file(const std::string& filename, Fx&& on_error, load_mode mode = load_mode::any) {
  18311. return safe_script_file(filename, std::forward<Fx>(on_error), mode);
  18312. }
  18313. template <typename Fx, typename E>
  18314. protected_function_result script(const string_view& code, const basic_environment<E>& env, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18315. return safe_script(code, env, std::forward<Fx>(on_error), chunkname, mode);
  18316. }
  18317. template <typename Fx, typename E>
  18318. protected_function_result script_file(const std::string& filename, const basic_environment<E>& env, Fx&& on_error, load_mode mode = load_mode::any) {
  18319. return safe_script_file(filename, env, std::forward<Fx>(on_error), mode);
  18320. }
  18321. protected_function_result script(const string_view& code, const environment& env, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18322. return safe_script(code, env, script_default_on_error, chunkname, mode);
  18323. }
  18324. protected_function_result script_file(const std::string& filename, const environment& env, load_mode mode = load_mode::any) {
  18325. return safe_script_file(filename, env, script_default_on_error, mode);
  18326. }
  18327. #if defined(SOL_SAFE_FUNCTION) && SOL_SAFE_FUNCTION
  18328. protected_function_result script(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18329. return safe_script(code, chunkname, mode);
  18330. }
  18331. protected_function_result script_file(const std::string& filename, load_mode mode = load_mode::any) {
  18332. return safe_script_file(filename, mode);
  18333. }
  18334. #else
  18335. unsafe_function_result script(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18336. return unsafe_script(code, chunkname, mode);
  18337. }
  18338. unsafe_function_result script_file(const std::string& filename, load_mode mode = load_mode::any) {
  18339. return unsafe_script_file(filename, mode);
  18340. }
  18341. #endif
  18342. load_result load(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18343. detail::typical_chunk_name_t basechunkname = {};
  18344. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  18345. load_status x = static_cast<load_status>(luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()));
  18346. return load_result(L, absolute_index(L, -1), 1, 1, x);
  18347. }
  18348. load_result load_buffer(const char* buff, size_t size, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18349. return load(string_view(buff, size), chunkname, mode);
  18350. }
  18351. load_result load_file(const std::string& filename, load_mode mode = load_mode::any) {
  18352. load_status x = static_cast<load_status>(luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()));
  18353. return load_result(L, absolute_index(L, -1), 1, 1, x);
  18354. }
  18355. load_result load(lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  18356. detail::typical_chunk_name_t basechunkname = {};
  18357. const char* chunknametarget = detail::make_chunk_name("lua_Reader", chunkname, basechunkname);
  18358. load_status x = static_cast<load_status>(lua_load(L, reader, data, chunknametarget, to_string(mode).c_str()));
  18359. return load_result(L, absolute_index(L, -1), 1, 1, x);
  18360. }
  18361. iterator begin() const {
  18362. return global.begin();
  18363. }
  18364. iterator end() const {
  18365. return global.end();
  18366. }
  18367. const_iterator cbegin() const {
  18368. return global.cbegin();
  18369. }
  18370. const_iterator cend() const {
  18371. return global.cend();
  18372. }
  18373. global_table globals() const {
  18374. return global;
  18375. }
  18376. table registry() const {
  18377. return reg;
  18378. }
  18379. std::size_t memory_used() const {
  18380. return total_memory_used(lua_state());
  18381. }
  18382. int stack_top() const {
  18383. return stack::top(L);
  18384. }
  18385. int stack_clear() {
  18386. int s = stack_top();
  18387. lua_pop(L, s);
  18388. return s;
  18389. }
  18390. void collect_garbage() {
  18391. lua_gc(lua_state(), LUA_GCCOLLECT, 0);
  18392. }
  18393. operator lua_State*() const {
  18394. return lua_state();
  18395. }
  18396. void set_panic(lua_CFunction panic) {
  18397. lua_atpanic(lua_state(), panic);
  18398. }
  18399. void set_exception_handler(exception_handler_function handler) {
  18400. set_default_exception_handler(lua_state(), handler);
  18401. }
  18402. template <typename... Args, typename... Keys>
  18403. decltype(auto) get(Keys&&... keys) const {
  18404. return global.get<Args...>(std::forward<Keys>(keys)...);
  18405. }
  18406. template <typename T, typename Key>
  18407. decltype(auto) get_or(Key&& key, T&& otherwise) const {
  18408. return global.get_or(std::forward<Key>(key), std::forward<T>(otherwise));
  18409. }
  18410. template <typename T, typename Key, typename D>
  18411. decltype(auto) get_or(Key&& key, D&& otherwise) const {
  18412. return global.get_or<T>(std::forward<Key>(key), std::forward<D>(otherwise));
  18413. }
  18414. template <typename... Args>
  18415. state_view& set(Args&&... args) {
  18416. global.set(std::forward<Args>(args)...);
  18417. return *this;
  18418. }
  18419. template <typename T, typename... Keys>
  18420. decltype(auto) traverse_get(Keys&&... keys) const {
  18421. return global.traverse_get<T>(std::forward<Keys>(keys)...);
  18422. }
  18423. template <typename... Args>
  18424. state_view& traverse_set(Args&&... args) {
  18425. global.traverse_set(std::forward<Args>(args)...);
  18426. return *this;
  18427. }
  18428. template <typename T>
  18429. state_view& set_usertype(usertype<T>& user) {
  18430. return set_usertype(usertype_traits<T>::name(), user);
  18431. }
  18432. template <typename Key, typename T>
  18433. state_view& set_usertype(Key&& key, usertype<T>& user) {
  18434. global.set_usertype(std::forward<Key>(key), user);
  18435. return *this;
  18436. }
  18437. template <typename Class, typename... Args>
  18438. state_view& new_usertype(const std::string& name, Args&&... args) {
  18439. global.new_usertype<Class>(name, std::forward<Args>(args)...);
  18440. return *this;
  18441. }
  18442. template <typename Class, typename CTor0, typename... CTor, typename... Args>
  18443. state_view& new_usertype(const std::string& name, Args&&... args) {
  18444. global.new_usertype<Class, CTor0, CTor...>(name, std::forward<Args>(args)...);
  18445. return *this;
  18446. }
  18447. template <typename Class, typename... CArgs, typename... Args>
  18448. state_view& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
  18449. global.new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
  18450. return *this;
  18451. }
  18452. template <typename Class, typename... Args>
  18453. state_view& new_simple_usertype(const std::string& name, Args&&... args) {
  18454. global.new_simple_usertype<Class>(name, std::forward<Args>(args)...);
  18455. return *this;
  18456. }
  18457. template <typename Class, typename CTor0, typename... CTor, typename... Args>
  18458. state_view& new_simple_usertype(const std::string& name, Args&&... args) {
  18459. global.new_simple_usertype<Class, CTor0, CTor...>(name, std::forward<Args>(args)...);
  18460. return *this;
  18461. }
  18462. template <typename Class, typename... CArgs, typename... Args>
  18463. state_view& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
  18464. global.new_simple_usertype<Class>(name, ctor, std::forward<Args>(args)...);
  18465. return *this;
  18466. }
  18467. template <typename Class, typename... Args>
  18468. simple_usertype<Class> create_simple_usertype(Args&&... args) {
  18469. return global.create_simple_usertype<Class>(std::forward<Args>(args)...);
  18470. }
  18471. template <typename Class, typename CTor0, typename... CTor, typename... Args>
  18472. simple_usertype<Class> create_simple_usertype(Args&&... args) {
  18473. return global.create_simple_usertype<Class, CTor0, CTor...>(std::forward<Args>(args)...);
  18474. }
  18475. template <typename Class, typename... CArgs, typename... Args>
  18476. simple_usertype<Class> create_simple_usertype(constructors<CArgs...> ctor, Args&&... args) {
  18477. return global.create_simple_usertype<Class>(ctor, std::forward<Args>(args)...);
  18478. }
  18479. template <bool read_only = true, typename... Args>
  18480. state_view& new_enum(const string_view& name, Args&&... args) {
  18481. global.new_enum<read_only>(name, std::forward<Args>(args)...);
  18482. return *this;
  18483. }
  18484. template <typename T, bool read_only = true>
  18485. state_view& new_enum(const string_view& name, std::initializer_list<std::pair<string_view, T>> items) {
  18486. global.new_enum<T, read_only>(name, std::move(items));
  18487. return *this;
  18488. }
  18489. template <typename Fx>
  18490. void for_each(Fx&& fx) {
  18491. global.for_each(std::forward<Fx>(fx));
  18492. }
  18493. template <typename T>
  18494. proxy<global_table&, T> operator[](T&& key) {
  18495. return global[std::forward<T>(key)];
  18496. }
  18497. template <typename T>
  18498. proxy<const global_table&, T> operator[](T&& key) const {
  18499. return global[std::forward<T>(key)];
  18500. }
  18501. template <typename Sig, typename... Args, typename Key>
  18502. state_view& set_function(Key&& key, Args&&... args) {
  18503. global.set_function<Sig>(std::forward<Key>(key), std::forward<Args>(args)...);
  18504. return *this;
  18505. }
  18506. template <typename... Args, typename Key>
  18507. state_view& set_function(Key&& key, Args&&... args) {
  18508. global.set_function(std::forward<Key>(key), std::forward<Args>(args)...);
  18509. return *this;
  18510. }
  18511. template <typename Name>
  18512. table create_table(Name&& name, int narr = 0, int nrec = 0) {
  18513. return global.create(std::forward<Name>(name), narr, nrec);
  18514. }
  18515. template <typename Name, typename Key, typename Value, typename... Args>
  18516. table create_table(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  18517. return global.create(std::forward<Name>(name), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  18518. }
  18519. template <typename Name, typename... Args>
  18520. table create_named_table(Name&& name, Args&&... args) {
  18521. table x = global.create_with(std::forward<Args>(args)...);
  18522. global.set(std::forward<Name>(name), x);
  18523. return x;
  18524. }
  18525. table create_table(int narr = 0, int nrec = 0) {
  18526. return create_table(lua_state(), narr, nrec);
  18527. }
  18528. template <typename Key, typename Value, typename... Args>
  18529. table create_table(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  18530. return create_table(lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  18531. }
  18532. template <typename... Args>
  18533. table create_table_with(Args&&... args) {
  18534. return create_table_with(lua_state(), std::forward<Args>(args)...);
  18535. }
  18536. static inline table create_table(lua_State* L, int narr = 0, int nrec = 0) {
  18537. return global_table::create(L, narr, nrec);
  18538. }
  18539. template <typename Key, typename Value, typename... Args>
  18540. static inline table create_table(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  18541. return global_table::create(L, narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  18542. }
  18543. template <typename... Args>
  18544. static inline table create_table_with(lua_State* L, Args&&... args) {
  18545. return global_table::create_with(L, std::forward<Args>(args)...);
  18546. }
  18547. };
  18548. } // namespace sol
  18549. // end of sol/state_view.hpp
  18550. // beginning of sol/thread.hpp
  18551. namespace sol {
  18552. struct lua_thread_state {
  18553. lua_State* L;
  18554. lua_thread_state(lua_State* Ls)
  18555. : L(Ls) {
  18556. }
  18557. lua_State* lua_state() const noexcept {
  18558. return L;
  18559. }
  18560. operator lua_State*() const noexcept {
  18561. return lua_state();
  18562. }
  18563. lua_State* operator->() const noexcept {
  18564. return lua_state();
  18565. }
  18566. };
  18567. namespace stack {
  18568. template <>
  18569. struct pusher<lua_thread_state> {
  18570. int push(lua_State*, lua_thread_state lts) {
  18571. lua_pushthread(lts.L);
  18572. return 1;
  18573. }
  18574. };
  18575. template <>
  18576. struct getter<lua_thread_state> {
  18577. lua_thread_state get(lua_State* L, int index, record& tracking) {
  18578. tracking.use(1);
  18579. lua_thread_state lts( lua_tothread(L, index) );
  18580. return lts;
  18581. }
  18582. };
  18583. template <>
  18584. struct check_getter<lua_thread_state> {
  18585. template <typename Handler>
  18586. optional<lua_thread_state> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  18587. lua_thread_state lts( lua_tothread(L, index) );
  18588. if (lts.lua_state() == nullptr) {
  18589. handler(L, index, type::thread, type_of(L, index), "value is not a valid thread type");
  18590. return nullopt;
  18591. }
  18592. tracking.use(1);
  18593. return lts;
  18594. }
  18595. };
  18596. } // namespace stack
  18597. template <typename base_t>
  18598. class basic_thread : public base_t {
  18599. public:
  18600. using base_t::lua_state;
  18601. basic_thread() noexcept = default;
  18602. basic_thread(const basic_thread&) = default;
  18603. basic_thread(basic_thread&&) = default;
  18604. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_thread>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  18605. basic_thread(T&& r)
  18606. : base_t(std::forward<T>(r)) {
  18607. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18608. auto pp = stack::push_pop(*this);
  18609. constructor_handler handler{};
  18610. stack::check<basic_thread>(lua_state(), -1, handler);
  18611. #endif // Safety
  18612. }
  18613. basic_thread(const stack_reference& r)
  18614. : basic_thread(r.lua_state(), r.stack_index()){};
  18615. basic_thread(stack_reference&& r)
  18616. : basic_thread(r.lua_state(), r.stack_index()){};
  18617. basic_thread& operator=(const basic_thread&) = default;
  18618. basic_thread& operator=(basic_thread&&) = default;
  18619. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  18620. basic_thread(lua_State* L, T&& r)
  18621. : base_t(L, std::forward<T>(r)) {
  18622. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18623. auto pp = stack::push_pop(*this);
  18624. constructor_handler handler{};
  18625. stack::check<basic_thread>(lua_state(), -1, handler);
  18626. #endif // Safety
  18627. }
  18628. basic_thread(lua_State* L, int index = -1)
  18629. : base_t(L, index) {
  18630. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18631. constructor_handler handler{};
  18632. stack::check<basic_thread>(L, index, handler);
  18633. #endif // Safety
  18634. }
  18635. basic_thread(lua_State* L, ref_index index)
  18636. : base_t(L, index) {
  18637. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18638. auto pp = stack::push_pop(*this);
  18639. constructor_handler handler{};
  18640. stack::check<basic_thread>(lua_state(), -1, handler);
  18641. #endif // Safety
  18642. }
  18643. basic_thread(lua_State* L, lua_State* actualthread)
  18644. : basic_thread(L, lua_thread_state{ actualthread }) {
  18645. }
  18646. basic_thread(lua_State* L, this_state actualthread)
  18647. : basic_thread(L, lua_thread_state{ actualthread.L }) {
  18648. }
  18649. basic_thread(lua_State* L, lua_thread_state actualthread)
  18650. : base_t(L, -stack::push(L, actualthread)) {
  18651. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18652. constructor_handler handler{};
  18653. stack::check<basic_thread>(lua_state(), -1, handler);
  18654. #endif // Safety
  18655. if (!is_stack_based<base_t>::value) {
  18656. lua_pop(lua_state(), 1);
  18657. }
  18658. }
  18659. state_view state() const {
  18660. return state_view(this->thread_state());
  18661. }
  18662. bool is_main_thread() const {
  18663. return stack::is_main_thread(this->thread_state());
  18664. }
  18665. lua_State* thread_state() const {
  18666. auto pp = stack::push_pop(*this);
  18667. lua_State* lthread = lua_tothread(lua_state(), -1);
  18668. return lthread;
  18669. }
  18670. thread_status status() const {
  18671. lua_State* lthread = thread_state();
  18672. auto lstat = static_cast<thread_status>(lua_status(lthread));
  18673. if (lstat == thread_status::ok) {
  18674. lua_Debug ar;
  18675. if (lua_getstack(lthread, 0, &ar) > 0)
  18676. return thread_status::ok;
  18677. else if (lua_gettop(lthread) == 0)
  18678. return thread_status::dead;
  18679. else
  18680. return thread_status::yielded;
  18681. }
  18682. return lstat;
  18683. }
  18684. basic_thread create() {
  18685. return create(lua_state());
  18686. }
  18687. static basic_thread create(lua_State* L) {
  18688. lua_newthread(L);
  18689. basic_thread result(L);
  18690. if (!is_stack_based<base_t>::value) {
  18691. lua_pop(L, 1);
  18692. }
  18693. return result;
  18694. }
  18695. };
  18696. typedef basic_thread<reference> thread;
  18697. typedef basic_thread<stack_reference> stack_thread;
  18698. } // namespace sol
  18699. // end of sol/thread.hpp
  18700. namespace sol {
  18701. class state : private std::unique_ptr<lua_State, detail::state_deleter>, public state_view {
  18702. private:
  18703. typedef std::unique_ptr<lua_State, detail::state_deleter> unique_base;
  18704. public:
  18705. state(lua_CFunction panic = default_at_panic)
  18706. : unique_base(luaL_newstate()), state_view(unique_base::get()) {
  18707. set_default_state(unique_base::get(), panic);
  18708. }
  18709. state(lua_CFunction panic, lua_Alloc alfunc, void* alpointer = nullptr)
  18710. : unique_base(lua_newstate(alfunc, alpointer)), state_view(unique_base::get()) {
  18711. set_default_state(unique_base::get(), panic);
  18712. }
  18713. state(const state&) = delete;
  18714. state(state&&) = default;
  18715. state& operator=(const state&) = delete;
  18716. state& operator=(state&& that) {
  18717. state_view::operator=(std::move(that));
  18718. unique_base::operator=(std::move(that));
  18719. return *this;
  18720. }
  18721. using state_view::get;
  18722. ~state() {
  18723. }
  18724. };
  18725. } // namespace sol
  18726. // end of sol/state.hpp
  18727. // beginning of sol/coroutine.hpp
  18728. namespace sol {
  18729. template <typename base_t>
  18730. class basic_coroutine : public base_t {
  18731. public:
  18732. typedef reference handler_t;
  18733. handler_t error_handler;
  18734. private:
  18735. call_status stats = call_status::yielded;
  18736. void luacall(std::ptrdiff_t argcount, std::ptrdiff_t) {
  18737. #if SOL_LUA_VERSION >= 504
  18738. int nres;
  18739. stats = static_cast<call_status>(lua_resume(lua_state(), nullptr, static_cast<int>(argcount), &nres));
  18740. #else
  18741. stats = static_cast<call_status>(lua_resume(lua_state(), nullptr, static_cast<int>(argcount)));
  18742. #endif
  18743. }
  18744. template <std::size_t... I, typename... Ret>
  18745. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n) {
  18746. luacall(n, sizeof...(Ret));
  18747. return stack::pop<std::tuple<Ret...>>(lua_state());
  18748. }
  18749. template <std::size_t I, typename Ret>
  18750. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n) {
  18751. luacall(n, 1);
  18752. return stack::pop<Ret>(lua_state());
  18753. }
  18754. template <std::size_t I>
  18755. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n) {
  18756. luacall(n, 0);
  18757. }
  18758. protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) {
  18759. int firstreturn = 1;
  18760. luacall(n, LUA_MULTRET);
  18761. int poststacksize = lua_gettop(this->lua_state());
  18762. int returncount = poststacksize - (firstreturn - 1);
  18763. if (error()) {
  18764. if (error_handler.valid()) {
  18765. string_view err = stack::get<string_view>(this->lua_state(), poststacksize);
  18766. error_handler.push();
  18767. stack::push(this->lua_state(), err);
  18768. lua_call(lua_state(), 1, 1);
  18769. }
  18770. return protected_function_result(this->lua_state(), lua_absindex(this->lua_state(), -1), 1, returncount, status());
  18771. }
  18772. return protected_function_result(this->lua_state(), firstreturn, returncount, returncount, status());
  18773. }
  18774. public:
  18775. using base_t::lua_state;
  18776. basic_coroutine() = default;
  18777. template <typename T, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_coroutine>>, meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<T>>>, meta::neg<std::is_same<base_t, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  18778. basic_coroutine(T&& r) noexcept
  18779. : base_t(std::forward<T>(r)), error_handler(detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  18780. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18781. if (!is_function<meta::unqualified_t<T>>::value) {
  18782. auto pp = stack::push_pop(*this);
  18783. constructor_handler handler{};
  18784. stack::check<basic_coroutine>(lua_state(), -1, handler);
  18785. }
  18786. #endif // Safety
  18787. }
  18788. basic_coroutine(const basic_coroutine&) = default;
  18789. basic_coroutine& operator=(const basic_coroutine&) = default;
  18790. basic_coroutine(basic_coroutine&&) = default;
  18791. basic_coroutine& operator=(basic_coroutine&&) = default;
  18792. basic_coroutine(const basic_function<base_t>& b)
  18793. : basic_coroutine(b, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(b.lua_state())) {
  18794. }
  18795. basic_coroutine(basic_function<base_t>&& b)
  18796. : basic_coroutine(std::move(b), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(b.lua_state())) {
  18797. }
  18798. basic_coroutine(const basic_function<base_t>& b, handler_t eh)
  18799. : base_t(b), error_handler(std::move(eh)) {
  18800. }
  18801. basic_coroutine(basic_function<base_t>&& b, handler_t eh)
  18802. : base_t(std::move(b)), error_handler(std::move(eh)) {
  18803. }
  18804. basic_coroutine(const stack_reference& r)
  18805. : basic_coroutine(r.lua_state(), r.stack_index(), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  18806. }
  18807. basic_coroutine(stack_reference&& r)
  18808. : basic_coroutine(r.lua_state(), r.stack_index(), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  18809. }
  18810. basic_coroutine(const stack_reference& r, handler_t eh)
  18811. : basic_coroutine(r.lua_state(), r.stack_index(), std::move(eh)) {
  18812. }
  18813. basic_coroutine(stack_reference&& r, handler_t eh)
  18814. : basic_coroutine(r.lua_state(), r.stack_index(), std::move(eh)) {
  18815. }
  18816. template <typename Super>
  18817. basic_coroutine(const proxy_base<Super>& p)
  18818. : basic_coroutine(p, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(p.lua_state())) {
  18819. }
  18820. template <typename Super>
  18821. basic_coroutine(proxy_base<Super>&& p)
  18822. : basic_coroutine(std::move(p), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(p.lua_state())) {
  18823. }
  18824. template <typename Proxy, typename Handler, meta::enable<std::is_base_of<proxy_base_tag, meta::unqualified_t<Proxy>>, meta::neg<is_lua_index<meta::unqualified_t<Handler>>>> = meta::enabler>
  18825. basic_coroutine(Proxy&& p, Handler&& eh)
  18826. : basic_coroutine(detail::force_cast<base_t>(p), std::forward<Handler>(eh)) {
  18827. }
  18828. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  18829. basic_coroutine(lua_State* L, T&& r)
  18830. : basic_coroutine(L, std::forward<T>(r), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  18831. }
  18832. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  18833. basic_coroutine(lua_State* L, T&& r, handler_t eh)
  18834. : base_t(L, std::forward<T>(r)), error_handler(std::move(eh)) {
  18835. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18836. auto pp = stack::push_pop(*this);
  18837. constructor_handler handler{};
  18838. stack::check<basic_coroutine>(lua_state(), -1, handler);
  18839. #endif // Safety
  18840. }
  18841. basic_coroutine(lua_nil_t n)
  18842. : base_t(n), error_handler(n) {
  18843. }
  18844. basic_coroutine(lua_State* L, int index = -1)
  18845. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  18846. }
  18847. basic_coroutine(lua_State* L, int index, handler_t eh)
  18848. : base_t(L, index), error_handler(std::move(eh)) {
  18849. #ifdef SOL_SAFE_REFERENCES
  18850. constructor_handler handler{};
  18851. stack::check<basic_coroutine>(L, index, handler);
  18852. #endif // Safety
  18853. }
  18854. basic_coroutine(lua_State* L, absolute_index index)
  18855. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  18856. }
  18857. basic_coroutine(lua_State* L, absolute_index index, handler_t eh)
  18858. : base_t(L, index), error_handler(std::move(eh)) {
  18859. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18860. constructor_handler handler{};
  18861. stack::check<basic_coroutine>(L, index, handler);
  18862. #endif // Safety
  18863. }
  18864. basic_coroutine(lua_State* L, raw_index index)
  18865. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  18866. }
  18867. basic_coroutine(lua_State* L, raw_index index, handler_t eh)
  18868. : base_t(L, index), error_handler(std::move(eh)) {
  18869. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18870. constructor_handler handler{};
  18871. stack::check<basic_coroutine>(L, index, handler);
  18872. #endif // Safety
  18873. }
  18874. basic_coroutine(lua_State* L, ref_index index)
  18875. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  18876. }
  18877. basic_coroutine(lua_State* L, ref_index index, handler_t eh)
  18878. : base_t(L, index), error_handler(std::move(eh)) {
  18879. #if defined(SOL_SAFE_REFERENCES) && SOL_SAFE_REFERENCES
  18880. auto pp = stack::push_pop(*this);
  18881. constructor_handler handler{};
  18882. stack::check<basic_coroutine>(lua_state(), -1, handler);
  18883. #endif // Safety
  18884. }
  18885. call_status status() const noexcept {
  18886. return stats;
  18887. }
  18888. bool error() const noexcept {
  18889. call_status cs = status();
  18890. return cs != call_status::ok && cs != call_status::yielded;
  18891. }
  18892. bool runnable() const noexcept {
  18893. return base_t::valid()
  18894. && (status() == call_status::yielded);
  18895. }
  18896. explicit operator bool() const noexcept {
  18897. return runnable();
  18898. }
  18899. template <typename... Args>
  18900. protected_function_result operator()(Args&&... args) {
  18901. return call<>(std::forward<Args>(args)...);
  18902. }
  18903. template <typename... Ret, typename... Args>
  18904. decltype(auto) operator()(types<Ret...>, Args&&... args) {
  18905. return call<Ret...>(std::forward<Args>(args)...);
  18906. }
  18907. template <typename... Ret, typename... Args>
  18908. decltype(auto) call(Args&&... args) {
  18909. // some users screw up coroutine.create
  18910. // and try to use it with sol::coroutine without ever calling the first resume in Lua
  18911. // this makes the stack incompatible with other kinds of stacks: protect against this
  18912. // make sure coroutines don't screw us over
  18913. base_t::push();
  18914. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  18915. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount);
  18916. }
  18917. };
  18918. } // namespace sol
  18919. // end of sol/coroutine.hpp
  18920. // beginning of sol/variadic_results.hpp
  18921. // beginning of sol/as_returns.hpp
  18922. namespace sol {
  18923. template <typename T>
  18924. struct as_returns_t {
  18925. T src;
  18926. };
  18927. template <typename Source>
  18928. auto as_returns(Source&& source) {
  18929. return as_returns_t<std::decay_t<Source>>{ std::forward<Source>(source) };
  18930. }
  18931. namespace stack {
  18932. template <typename T>
  18933. struct pusher<as_returns_t<T>> {
  18934. int push(lua_State* L, const as_returns_t<T>& e) {
  18935. auto& src = detail::unwrap(e.src);
  18936. int p = 0;
  18937. for (const auto& i : src) {
  18938. p += stack::push(L, i);
  18939. }
  18940. return p;
  18941. }
  18942. };
  18943. } // namespace stack
  18944. } // namespace sol
  18945. // end of sol/as_returns.hpp
  18946. namespace sol {
  18947. struct variadic_results : public std::vector<object> {
  18948. using std::vector<object>::vector;
  18949. };
  18950. namespace stack {
  18951. template <>
  18952. struct pusher<variadic_results> {
  18953. int push(lua_State* L, const variadic_results& e) {
  18954. int p = 0;
  18955. for (const auto& i : e) {
  18956. p += stack::push(L, i);
  18957. }
  18958. return p;
  18959. }
  18960. };
  18961. } // namespace stack
  18962. } // namespace sol
  18963. // end of sol/variadic_results.hpp
  18964. #if defined(__GNUC__)
  18965. #pragma GCC diagnostic pop
  18966. #elif defined _MSC_VER
  18967. #pragma warning( push )
  18968. #endif // g++
  18969. #if defined(SOL_INSIDE_UNREAL) && SOL_INSIDE_UNREAL
  18970. #if defined(SOL_INSIDE_UNREAL_REMOVED_CHECK) && SOL_INSIDE_UNREAL_REMOVED_CHECK
  18971. #if defined(DO_CHECK) && DO_CHECK
  18972. #define check(expr) { if(UNLIKELY(!(expr))) { FDebug::LogAssertFailedMessage( #expr, __FILE__, __LINE__ ); _DebugBreakAndPromptForRemote(); FDebug::AssertFailed( #expr, __FILE__, __LINE__ ); CA_ASSUME(false); } }
  18973. #else
  18974. #define check(expr) { CA_ASSUME(expr); }
  18975. #endif
  18976. #endif
  18977. #endif // Unreal Engine 4 Bullshit
  18978. #endif // SOL_HPP
  18979. // end of sol.hpp
  18980. #endif // SOL_SINGLE_INCLUDE_HPP