sol.hpp 871 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674
  1. // The MIT License (MIT)
  2. // Copyright (c) 2013-2020 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 2020-10-03 21:34:24.496436 UTC
  19. // This header was generated with sol v3.2.1 (revision 48eea7b5)
  20. // https://github.com/ThePhD/sol2
  21. #ifndef SOL_SINGLE_INCLUDE_HPP
  22. #define SOL_SINGLE_INCLUDE_HPP
  23. // beginning of sol/sol.hpp
  24. #ifndef SOL_HPP
  25. #define SOL_HPP
  26. // beginning of sol/version.hpp
  27. #include <sol/config.hpp>
  28. #include <cstdint>
  29. #define SOL_VERSION_MAJOR 3
  30. #define SOL_VERSION_MINOR 5
  31. #define SOL_VERSION_PATCH 0
  32. #define SOL_VERSION_STRING "3.5.0"
  33. #define SOL_VERSION ((SOL_VERSION_MAJOR * 100000) + (SOL_VERSION_MINOR * 100) + (SOL_VERSION_PATCH))
  34. #define SOL_IS_ON(OP_SYMBOL) ((3 OP_SYMBOL 3) != 0)
  35. #define SOL_IS_OFF(OP_SYMBOL) ((3 OP_SYMBOL 3) == 0)
  36. #define SOL_IS_DEFAULT_ON(OP_SYMBOL) ((3 OP_SYMBOL 3) > 3)
  37. #define SOL_IS_DEFAULT_OFF(OP_SYMBOL) ((3 OP_SYMBOL 3 OP_SYMBOL 3) < 0)
  38. #define SOL_ON |
  39. #define SOL_OFF ^
  40. #define SOL_DEFAULT_ON +
  41. #define SOL_DEFAULT_OFF -
  42. #if defined(_MSC_VER)
  43. #define SOL_COMPILER_CLANG_I_ SOL_OFF
  44. #define SOL_COMPILER_GCC_I_ SOL_OFF
  45. #define SOL_COMPILER_EDG_I_ SOL_OFF
  46. #define SOL_COMPILER_VCXX_I_ SOL_ON
  47. #elif defined(__clang__)
  48. #define SOL_COMPILER_CLANG_I_ SOL_ON
  49. #define SOL_COMPILER_GCC_I_ SOL_OFF
  50. #define SOL_COMPILER_EDG_I_ SOL_OFF
  51. #define SOL_COMPILER_VCXX_I_ SOL_OFF
  52. #elif defined(__GNUC__)
  53. #define SOL_COMPILER_CLANG_I_ SOL_OFF
  54. #define SOL_COMPILER_GCC_I_ SOL_ON
  55. #define SOL_COMPILER_EDG_I_ SOL_OFF
  56. #define SOL_COMPILER_VCXX_I_ SOL_OFF
  57. #else
  58. #define SOL_COMPILER_CLANG_I_ SOL_OFF
  59. #define SOL_COMPILER_GCC_I_ SOL_OFF
  60. #define SOL_COMPILER_EDG_I_ SOL_OFF
  61. #define SOL_COMPILER_VCXX_I_ SOL_OFF
  62. #endif
  63. #if defined(__MINGW32__)
  64. #define SOL_COMPILER_FRONTEND_MINGW_I_ SOL_ON
  65. #else
  66. #define SOL_COMPILER_FRONTEND_MINGW_I_ SOL_OFF
  67. #endif
  68. #if SIZE_MAX <= 0xFFFFULL
  69. #define SOL_PLATFORM_X16_I_ SOL_ON
  70. #define SOL_PLATFORM_X86_I_ SOL_OFF
  71. #define SOL_PLATFORM_X64_I_ SOL_OFF
  72. #elif SIZE_MAX <= 0xFFFFFFFFULL
  73. #define SOL_PLATFORM_X16_I_ SOL_OFF
  74. #define SOL_PLATFORM_X86_I_ SOL_ON
  75. #define SOL_PLATFORM_X64_I_ SOL_OFF
  76. #else
  77. #define SOL_PLATFORM_X16_I_ SOL_OFF
  78. #define SOL_PLATFORM_X86_I_ SOL_OFF
  79. #define SOL_PLATFORM_X64_I_ SOL_ON
  80. #endif
  81. #define SOL_PLATFORM_ARM32_I_ SOL_OFF
  82. #define SOL_PLATFORM_ARM64_I_ SOL_OFF
  83. #if defined(_WIN32)
  84. #define SOL_PLATFORM_WINDOWS_I_ SOL_ON
  85. #else
  86. #define SOL_PLATFORM_WINDOWS_I_ SOL_OFF
  87. #endif
  88. #if defined(__APPLE__)
  89. #define SOL_PLATFORM_APPLE_I_ SOL_ON
  90. #else
  91. #define SOL_PLATFORM_APPLE_I_ SOL_OFF
  92. #endif
  93. #if defined(__unix__)
  94. #define SOL_PLATFORM_UNIXLIKE_I_ SOL_ON
  95. #else
  96. #define SOL_PLATFORM_UNIXLIKE_I_ SOL_OFF
  97. #endif
  98. #if defined(__linux__)
  99. #define SOL_PLATFORM_LINUXLIKE_I_ SOL_ON
  100. #else
  101. #define SOL_PLATFORM_LINUXLIKE_I_ SOL_OFF
  102. #endif
  103. #define SOL_PLATFORM_APPLE_IPHONE_I_ SOL_OFF
  104. #define SOL_PLATFORM_BSDLIKE_I_ SOL_OFF
  105. #if defined(SOL_IN_DEBUG_DETECTED)
  106. #if SOL_IN_DEBUG_DETECTED != 0
  107. #define SOL_DEBUG_BUILD_I_ SOL_ON
  108. #else
  109. #define SOL_DEBUG_BUILD_I_ SOL_OFF
  110. #endif
  111. #elif !defined(NDEBUG)
  112. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_) && defined(_DEBUG)
  113. #define SOL_DEBUG_BUILD_I_ SOL_ON
  114. #elif (SOL_IS_ON(SOL_COMPILER_CLANG_I_) || SOL_IS_ON(SOL_COMPILER_GCC_I_)) && !defined(__OPTIMIZE__)
  115. #define SOL_DEBUG_BUILD_I_ SOL_ON
  116. #else
  117. #define SOL_DEBUG_BUILD_I_ SOL_OFF
  118. #endif
  119. #else
  120. #define SOL_DEBUG_BUILD_I_ SOL_DEFAULT_OFF
  121. #endif // We are in a debug mode of some sort
  122. #if defined(SOL_NO_EXCEPTIONS)
  123. #if (SOL_NO_EXCEPTIONS != 0)
  124. #define SOL_EXCEPTIONS_I_ SOL_OFF
  125. #else
  126. #define SOL_EXCEPTIONS_I_ SOL_ON
  127. #endif
  128. #elif SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  129. #if !defined(_CPPUNWIND)
  130. #define SOL_EXCEPTIONS_I_ SOL_OFF
  131. #else
  132. #define SOL_EXCEPTIONS_I_ SOL_ON
  133. #endif
  134. #elif SOL_IS_ON(SOL_COMPILER_CLANG_I_) || SOL_IS_ON(SOL_COMPILER_GCC_I_)
  135. #if !defined(__EXCEPTIONS)
  136. #define SOL_EXCEPTIONS_I_ SOL_OFF
  137. #else
  138. #define SOL_EXCEPTIONS_I_ SOL_ON
  139. #endif
  140. #else
  141. #define SOL_EXCEPTIONS_I_ SOL_DEFAULT_ON
  142. #endif
  143. #if defined(SOL_NO_RTTI)
  144. #if (SOL_NO_RTTI != 0)
  145. #define SOL_RTTI_I_ SOL_OFF
  146. #else
  147. #define SOL_RTTI_I_ SOL_ON
  148. #endif
  149. #elif SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  150. #if !defined(_CPPRTTI)
  151. #define SOL_RTTI_I_ SOL_OFF
  152. #else
  153. #define SOL_RTTI_I_ SOL_ON
  154. #endif
  155. #elif SOL_IS_ON(SOL_COMPILER_CLANG_I_) || SOL_IS_ON(SOL_COMPILER_GCC_I_)
  156. #if !defined(__GXX_RTTI)
  157. #define SOL_RTTI_I_ SOL_OFF
  158. #else
  159. #define SOL_RTTI_I_ SOL_ON
  160. #endif
  161. #else
  162. #define SOL_RTTI_I_ SOL_DEFAULT_ON
  163. #endif
  164. #if defined(SOL_NO_THREAD_LOCAL) && (SOL_NO_THREAD_LOCAL != 0)
  165. #define SOL_USE_THREAD_LOCAL_I_ SOL_OFF
  166. #else
  167. #define SOL_USE_THREAD_LOCAL_I_ SOL_DEFAULT_ON
  168. #endif // thread_local keyword is bjorked on some platforms
  169. #if defined(SOL_ALL_SAFETIES_ON) && (SOL_ALL_SAFETIES_ON != 0)
  170. #define SOL_ALL_SAFETIES_ON_I_ SOL_ON
  171. #else
  172. #define SOL_ALL_SAFETIES_ON_I_ SOL_DEFAULT_OFF
  173. #endif
  174. #if defined(SOL_SAFE_GETTER) && (SOL_SAFE_GETTER != 0)
  175. #define SOL_SAFE_GETTER_I_ SOL_ON
  176. #else
  177. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  178. #define SOL_SAFE_GETTER_I_ SOL_ON
  179. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  180. #define SOL_SAFE_GETTER_I_ SOL_DEFAULT_ON
  181. #else
  182. #define SOL_SAFE_GETTER_I_ SOL_DEFAULT_OFF
  183. #endif
  184. #endif
  185. #if defined(SOL_SAFE_USERTYPE) && (SOL_SAFE_USERTYPE != 0)
  186. #define SOL_SAFE_USERTYPE_I_ SOL_ON
  187. #else
  188. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  189. #define SOL_SAFE_USERTYPE_I_ SOL_ON
  190. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  191. #define SOL_SAFE_USERTYPE_I_ SOL_DEFAULT_ON
  192. #else
  193. #define SOL_SAFE_USERTYPE_I_ SOL_DEFAULT_OFF
  194. #endif
  195. #endif
  196. #if defined(SOL_SAFE_REFERENCES) && (SOL_SAFE_REFERENCES != 0)
  197. #define SOL_SAFE_REFERENCES_I_ SOL_ON
  198. #else
  199. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  200. #define SOL_SAFE_REFERENCES_I_ SOL_ON
  201. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  202. #define SOL_SAFE_REFERENCES_I_ SOL_DEFAULT_ON
  203. #else
  204. #define SOL_SAFE_REFERENCES_I_ SOL_DEFAULT_OFF
  205. #endif
  206. #endif
  207. #if (defined(SOL_SAFE_FUNCTIONS) && (SOL_SAFE_FUNCTIONS != 0)) \
  208. || (defined(SOL_SAFE_FUNCTION_OBJECTS) && (SOL_SAFE_FUNCTION_OBJECTS != 0))
  209. #define SOL_SAFE_FUNCTION_OBJECTS_I_ SOL_ON
  210. #else
  211. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  212. #define SOL_SAFE_FUNCTION_OBJECTS_I_ SOL_ON
  213. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  214. #define SOL_SAFE_FUNCTION_OBJECTS_I_ SOL_DEFAULT_ON
  215. #else
  216. #define SOL_SAFE_FUNCTION_OBJECTS_I_ SOL_DEFAULT_OFF
  217. #endif
  218. #endif
  219. #if defined(SOL_SAFE_FUNCTION_CALLS) && (SOL_SAFE_FUNCTION_CALLS != 0)
  220. #define SOL_SAFE_FUNCTION_CALLS_I_ SOL_ON
  221. #else
  222. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  223. #define SOL_SAFE_FUNCTION_CALLS_I_ SOL_ON
  224. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  225. #define SOL_SAFE_FUNCTION_CALLS_I_ SOL_DEFAULT_ON
  226. #else
  227. #define SOL_SAFE_FUNCTION_CALLS_I_ SOL_DEFAULT_OFF
  228. #endif
  229. #endif
  230. #if defined(SOL_SAFE_PROXIES) && (SOL_SAFE_PROXIES != 0)
  231. #define SOL_SAFE_PROXIES_I_ SOL_ON
  232. #else
  233. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  234. #define SOL_SAFE_PROXIES_I_ SOL_ON
  235. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  236. #define SOL_SAFE_PROXIES_I_ SOL_DEFAULT_ON
  237. #else
  238. #define SOL_SAFE_PROXIES_I_ SOL_DEFAULT_OFF
  239. #endif
  240. #endif
  241. #if defined(SOL_SAFE_NUMERICS) && (SOL_SAFE_NUMERICS != 0)
  242. #define SOL_SAFE_NUMERICS_I_ SOL_ON
  243. #else
  244. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  245. #define SOL_SAFE_NUMERICS_I_ SOL_ON
  246. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  247. #define SOL_SAFE_NUMERICS_I_ SOL_DEFAULT_ON
  248. #else
  249. #define SOL_SAFE_NUMERICS_I_ SOL_DEFAULT_OFF
  250. #endif
  251. #endif
  252. #if defined(SOL_SAFE_STACK_CHECK) && (SOL_SAFE_STACK_CHECK != 0)
  253. #define SOL_SAFE_STACK_CHECK_I_ SOL_ON
  254. #else
  255. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  256. #define SOL_SAFE_STACK_CHECK_I_ SOL_ON
  257. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  258. #define SOL_SAFE_STACK_CHECK_I_ SOL_DEFAULT_ON
  259. #else
  260. #define SOL_SAFE_STACK_CHECK_I_ SOL_DEFAULT_OFF
  261. #endif
  262. #endif
  263. #if (defined(SOL_NO_CHECK_NUMBER_PRECISION) && (SOL_NO_CHECK_NUMBER_PRECISION != 0)) \
  264. || (defined(SOL_NO_CHECKING_NUMBER_PRECISION) && (SOL_NO_CHECKING_NUMBER_PRECISION != 0))
  265. #define SOL_NUMBER_PRECISION_CHECKS_I_ SOL_OFF
  266. #else
  267. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  268. #define SOL_NUMBER_PRECISION_CHECKS_I_ SOL_ON
  269. #elif SOL_IS_ON(SOL_SAFE_NUMERICS_I_)
  270. #define SOL_NUMBER_PRECISION_CHECKS_I_ SOL_ON
  271. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  272. #define SOL_NUMBER_PRECISION_CHECKS_I_ SOL_DEFAULT_ON
  273. #else
  274. #define SOL_NUMBER_PRECISION_CHECKS_I_ SOL_DEFAULT_OFF
  275. #endif
  276. #endif
  277. #if defined(SOL_STRINGS_ARE_NUMBERS)
  278. #if (SOL_STRINGS_ARE_NUMBERS != 0)
  279. #define SOL_STRINGS_ARE_NUMBERS_I_ SOL_ON
  280. #else
  281. #define SOL_STRINGS_ARE_NUMBERS_I_ SOL_OFF
  282. #endif
  283. #else
  284. #define SOL_STRINGS_ARE_NUMBERS_I_ SOL_DEFAULT_OFF
  285. #endif
  286. #if defined(SOL_ENABLE_INTEROP) && (SOL_ENABLE_INTEROP != 0) \
  287. || defined(SOL_USE_INTEROP) && (SOL_USE_INTEROP != 0)
  288. #define SOL_USE_INTEROP_I_ SOL_ON
  289. #else
  290. #define SOL_USE_INTEROP_I_ SOL_DEFAULT_OFF
  291. #endif
  292. #if defined(SOL_NO_NIL)
  293. #if (SOL_NO_NIL != 0)
  294. #define SOL_NIL_I_ SOL_OFF
  295. #else
  296. #define SOL_NIL_I_ SOL_ON
  297. #endif
  298. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) || defined(__OBJC__) || defined(nil)
  299. #define SOL_NIL_I_ SOL_DEFAULT_OFF
  300. #else
  301. #define SOL_NIL_I_ SOL_DEFAULT_ON
  302. #endif
  303. #if defined(SOL_USERTYPE_TYPE_BINDING_INFO)
  304. #if (SOL_USERTYPE_TYPE_BINDING_INFO != 0)
  305. #define SOL_USERTYPE_TYPE_BINDING_INFO_I_ SOL_ON
  306. #else
  307. #define SOL_USERTYPE_TYPE_BINDING_INFO_I_ SOL_OFF
  308. #endif
  309. #else
  310. #define SOL_USERTYPE_TYPE_BINDING_INFO_I_ SOL_DEFAULT_ON
  311. #endif // We should generate a my_type.__type table with lots of class information for usertypes
  312. #if defined(SOL_AUTOMAGICAL_TYPES_BY_DEFAULT)
  313. #if (SOL_AUTOMAGICAL_TYPES_BY_DEFAULT != 0)
  314. #define SOL_DEFAULT_AUTOMAGICAL_USERTYPES_I_ SOL_ON
  315. #else
  316. #define SOL_DEFAULT_AUTOMAGICAL_USERTYPES_I_ SOL_OFF
  317. #endif
  318. #elif defined(SOL_DEFAULT_AUTOMAGICAL_USERTYPES)
  319. #if (SOL_DEFAULT_AUTOMAGICAL_USERTYPES != 0)
  320. #define SOL_DEFAULT_AUTOMAGICAL_USERTYPES_I_ SOL_ON
  321. #else
  322. #define SOL_DEFAULT_AUTOMAGICAL_USERTYPES_I_ SOL_OFF
  323. #endif
  324. #else
  325. #define SOL_DEFAULT_AUTOMAGICAL_USERTYPES_I_ SOL_DEFAULT_ON
  326. #endif // make is_automagical on/off by default
  327. #if defined(SOL_STD_VARIANT)
  328. #if (SOL_STD_VARIANT != 0)
  329. #define SOL_STD_VARIANT_I_ SOL_ON
  330. #else
  331. #define SOL_STD_VARIANT_I_ SOL_OFF
  332. #endif
  333. #else
  334. #if SOL_IS_ON(SOL_COMPILER_CLANG_I_) && SOL_IS_ON(SOL_PLATFORM_APPLE_I_)
  335. #if defined(__has_include)
  336. #if __has_include(<variant>)
  337. #define SOL_STD_VARIANT_I_ SOL_ON
  338. #else
  339. #define SOL_STD_VARIANT_I_ SOL_OFF
  340. #endif
  341. #else
  342. #define SOL_STD_VARIANT_I_ SOL_OFF
  343. #endif
  344. #else
  345. #define SOL_STD_VARIANT_I_ SOL_DEFAULT_ON
  346. #endif
  347. #endif // make is_automagical on/off by default
  348. #if defined(SOL_NOEXCEPT_FUNCTION_TYPE)
  349. #if (SOL_NOEXCEPT_FUNCTION_TYPE != 0)
  350. #define SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_ SOL_ON
  351. #else
  352. #define SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_ SOL_OFF
  353. #endif
  354. #else
  355. #if defined(__cpp_noexcept_function_type)
  356. #define SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_ SOL_ON
  357. #elif SOL_IS_ON(SOL_COMPILER_VCXX_I_) && (defined(_MSVC_LANG) && (_MSVC_LANG < 201403L))
  358. // There is a bug in the VC++ compiler??
  359. // on /std:c++latest under x86 conditions (VS 15.5.2),
  360. // compiler errors are tossed for noexcept markings being on function types
  361. // that are identical in every other way to their non-noexcept marked types function types...
  362. // VS 2019: There is absolutely a bug.
  363. #define SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_ SOL_OFF
  364. #else
  365. #define SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_ SOL_DEFAULT_ON
  366. #endif
  367. #endif // noexcept is part of a function's type
  368. #if defined(SOL_STACK_STRING_OPTIMIZATION_SIZE) && SOL_STACK_STRING_OPTIMIZATION_SIZE > 0
  369. #define SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_ SOL_STACK_STRING_OPTIMIZATION_SIZE
  370. #else
  371. #define SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_ 1024
  372. #endif
  373. #if defined(SOL_ID_SIZE) && SOL_ID_SIZE > 0
  374. #define SOL_ID_SIZE_I_ SOL_ID_SIZE
  375. #else
  376. #define SOL_ID_SIZE_I_ 512
  377. #endif
  378. #if defined(LUA_IDSIZE) && LUA_IDSIZE > 0
  379. #define SOL_FILE_ID_SIZE_I_ LUA_IDSIZE
  380. #elif defined(SOL_ID_SIZE) && SOL_ID_SIZE > 0
  381. #define SOL_FILE_ID_SIZE_I_ SOL_FILE_ID_SIZE
  382. #else
  383. #define SOL_FILE_ID_SIZE_I_ 2048
  384. #endif
  385. #if defined(SOL_PRINT_ERRORS)
  386. #if (SOL_PRINT_ERRORS != 0)
  387. #define SOL_PRINT_ERRORS_I_ SOL_ON
  388. #else
  389. #define SOL_PRINT_ERRORS_I_ SOL_OFF
  390. #endif
  391. #else
  392. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  393. #define SOL_PRINT_ERRORS_I_ SOL_ON
  394. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  395. #define SOL_PRINT_ERRORS_I_ SOL_DEFAULT_ON
  396. #else
  397. #define SOL_PRINT_ERRORS_I_ SOL_OFF
  398. #endif
  399. #endif
  400. #if defined(SOL_DEFAULT_PASS_ON_ERROR) && (SOL_DEFAULT_PASS_ON_ERROR != 0)
  401. #define SOL_DEFAULT_PASS_ON_ERROR_I_ SOL_ON
  402. #else
  403. #if SOL_IS_ON(SOL_ALL_SAFETIES_ON_I_)
  404. #define SOL_DEFAULT_PASS_ON_ERROR_I_ SOL_ON
  405. #elif SOL_IS_ON(SOL_DEBUG_BUILD_I_)
  406. #define SOL_DEFAULT_PASS_ON_ERROR_I_ SOL_DEFAULT_ON
  407. #else
  408. #define SOL_DEFAULT_PASS_ON_ERROR_I_ SOL_OFF
  409. #endif
  410. #endif
  411. #if defined(SOL_USING_CXX_LUA)
  412. #if (SOL_USING_CXX_LUA != 0)
  413. #define SOL_USE_CXX_LUA_I_ SOL_ON
  414. #else
  415. #define SOL_USE_CXX_LUA_I_ SOL_OFF
  416. #endif
  417. #elif defined(SOL_USE_CXX_LUA)
  418. #if (SOL_USE_CXX_LUA != 0)
  419. #define SOL_USE_CXX_LUA_I_ SOL_ON
  420. #else
  421. #define SOL_USE_CXX_LUA_I_ SOL_OFF
  422. #endif
  423. #else
  424. #define SOL_USE_CXX_LUA_I_ SOL_OFF
  425. #endif
  426. #if defined(SOL_USING_CXX_LUAJIT)
  427. #if (SOL_USING_CXX_LUA != 0)
  428. #define SOL_USE_CXX_LUAJIT_I_ SOL_ON
  429. #else
  430. #define SOL_USE_CXX_LUAJIT_I_ SOL_OFF
  431. #endif
  432. #elif defined(SOL_USE_CXX_LUAJIT)
  433. #if (SOL_USE_CXX_LUA != 0)
  434. #define SOL_USE_CXX_LUAJIT_I_ SOL_ON
  435. #else
  436. #define SOL_USE_CXX_LUAJIT_I_ SOL_OFF
  437. #endif
  438. #else
  439. #define SOL_USE_CXX_LUAJIT_I_ SOL_OFF
  440. #endif
  441. #if defined(SOL_NO_LUA_HPP)
  442. #if (SOL_NO_LUA_HPP != 0)
  443. #define SOL_USE_LUA_HPP_I_ SOL_OFF
  444. #else
  445. #define SOL_USE_LUA_HPP_I_ SOL_ON
  446. #endif
  447. #elif defined(SOL_USING_CXX_LUA)
  448. #define SOL_USE_LUA_HPP_I_ SOL_OFF
  449. #elif defined(__has_include)
  450. #if __has_include(<lua.hpp>)
  451. #define SOL_USE_LUA_HPP_I_ SOL_ON
  452. #else
  453. #define SOL_USE_LUA_HPP_I_ SOL_OFF
  454. #endif
  455. #else
  456. #define SOL_USE_LUA_HPP_I_ SOL_DEFAULT_ON
  457. #endif
  458. #if defined(SOL_CONTAINERS_START)
  459. #define SOL_CONTAINER_START_INDEX_I_ SOL_CONTAINERS_START
  460. #elif defined(SOL_CONTAINERS_START_INDEX)
  461. #define SOL_CONTAINER_START_INDEX_I_ SOL_CONTAINERS_START_INDEX
  462. #elif defined(SOL_CONTAINER_START_INDEX)
  463. #define SOL_CONTAINER_START_INDEX_I_ SOL_CONTAINER_START_INDEX
  464. #else
  465. #define SOL_CONTAINER_START_INDEX_I_ 1
  466. #endif
  467. #if defined (SOL_NO_MEMORY_ALIGNMENT)
  468. #if (SOL_NO_MEMORY_ALIGNMENT != 0)
  469. #define SOL_ALIGN_MEMORY_I_ SOL_OFF
  470. #else
  471. #define SOL_ALIGN_MEMORY_I_ SOL_ON
  472. #endif
  473. #else
  474. #define SOL_ALIGN_MEMORY_I_ SOL_DEFAULT_ON
  475. #endif
  476. #if defined(SOL_USE_BOOST)
  477. #if (SOL_USE_BOOST != 0)
  478. #define SOL_USE_BOOST_I_ SOL_ON
  479. #else
  480. #define SOL_USE_BOOST_I_ SOL_OFF
  481. #endif
  482. #else
  483. #define SOL_USE_BOOST_I_ SOL_OFF
  484. #endif
  485. #if defined(SOL_USE_UNSAFE_BASE_LOOKUP)
  486. #if (SOL_USE_UNSAFE_BASE_LOOKUP != 0)
  487. #define SOL_USE_UNSAFE_BASE_LOOKUP_I_ SOL_ON
  488. #else
  489. #define SOL_USE_UNSAFE_BASE_LOOKUP_I_ SOL_OFF
  490. #endif
  491. #else
  492. #define SOL_USE_UNSAFE_BASE_LOOKUP_I_ SOL_OFF
  493. #endif
  494. #if defined(SOL_INSIDE_UNREAL)
  495. #if (SOL_INSIDE_UNREAL != 0)
  496. #define SOL_INSIDE_UNREAL_ENGINE_I_ SOL_ON
  497. #else
  498. #define SOL_INSIDE_UNREAL_ENGINE_I_ SOL_OFF
  499. #endif
  500. #else
  501. #if defined(UE_BUILD_DEBUG) || defined(UE_BUILD_DEVELOPMENT) || defined(UE_BUILD_TEST) || defined(UE_BUILD_SHIPPING) || defined(UE_SERVER)
  502. #define SOL_INSIDE_UNREAL_ENGINE_I_ SOL_ON
  503. #else
  504. #define SOL_INSIDE_UNREAL_ENGINE_I_ SOL_DEFAULT_OFF
  505. #endif
  506. #endif
  507. #if defined(SOL_NO_COMPAT)
  508. #if (SOL_NO_COMPAT != 0)
  509. #define SOL_USE_COMPATIBILITY_LAYER_I_ SOL_OFF
  510. #else
  511. #define SOL_USE_COMPATIBILITY_LAYER_I_ SOL_ON
  512. #endif
  513. #else
  514. #define SOL_USE_COMPATIBILITY_LAYER_I_ SOL_DEFAULT_ON
  515. #endif
  516. #if defined(SOL_GET_FUNCTION_POINTER_UNSAFE)
  517. #if (SOL_GET_FUNCTION_POINTER_UNSAFE != 0)
  518. #define SOL_GET_FUNCTION_POINTER_UNSAFE_I_ SOL_ON
  519. #else
  520. #define SOL_GET_FUNCTION_POINTER_UNSAFE_I_ SOL_OFF
  521. #endif
  522. #else
  523. #define SOL_GET_FUNCTION_POINTER_UNSAFE_I_ SOL_DEFAULT_OFF
  524. #endif
  525. #if SOL_IS_ON(SOL_COMPILER_FRONTEND_MINGW_I_) && defined(__GNUC__) && (__GNUC__ < 6)
  526. // MinGW is off its rocker in some places...
  527. #define SOL_MINGW_CCTYPE_IS_POISONED_I_ SOL_ON
  528. #else
  529. #define SOL_MINGW_CCTYPE_IS_POISONED_I_ SOL_DEFAULT_OFF
  530. #endif
  531. // end of sol/version.hpp
  532. #if SOL_IS_ON(SOL_INSIDE_UNREAL_ENGINE_I_)
  533. #ifdef check
  534. #pragma push_macro("check")
  535. #undef check
  536. #endif
  537. #endif // Unreal Engine 4 Bullshit
  538. #if SOL_IS_ON(SOL_COMPILER_GCC_I_)
  539. #pragma GCC diagnostic push
  540. #pragma GCC diagnostic ignored "-Wshadow"
  541. #pragma GCC diagnostic ignored "-Wconversion"
  542. #if __GNUC__ > 6
  543. #pragma GCC diagnostic ignored "-Wnoexcept-type"
  544. #endif
  545. #elif SOL_IS_ON(SOL_COMPILER_CLANG_I_)
  546. #elif SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  547. #pragma warning(push)
  548. #pragma warning(disable : 4505) // unreferenced local function has been removed GEE THANKS
  549. #endif // clang++ vs. g++ vs. VC++
  550. // beginning of sol/forward.hpp
  551. #ifndef SOL_FORWARD_HPP
  552. #define SOL_FORWARD_HPP
  553. #include <utility>
  554. #include <type_traits>
  555. #include <string_view>
  556. #if SOL_IS_ON(SOL_USE_CXX_LUA_I_) || SOL_IS_ON(SOL_USE_CXX_LUAJIT_I_)
  557. struct lua_State;
  558. #else
  559. extern "C" {
  560. struct lua_State;
  561. }
  562. #endif // C++ Mangling for Lua vs. Not
  563. namespace sol {
  564. enum class type;
  565. class stateless_reference;
  566. template <bool b>
  567. class basic_reference;
  568. using reference = basic_reference<false>;
  569. using main_reference = basic_reference<true>;
  570. class stateless_stack_reference;
  571. class stack_reference;
  572. template <typename A>
  573. class basic_bytecode;
  574. struct lua_value;
  575. struct proxy_base_tag;
  576. template <typename>
  577. struct proxy_base;
  578. template <typename, typename>
  579. struct table_proxy;
  580. template <bool, typename>
  581. class basic_table_core;
  582. template <bool b>
  583. using table_core = basic_table_core<b, reference>;
  584. template <bool b>
  585. using main_table_core = basic_table_core<b, main_reference>;
  586. template <bool b>
  587. using stack_table_core = basic_table_core<b, stack_reference>;
  588. template <typename base_type>
  589. using basic_table = basic_table_core<false, base_type>;
  590. using table = table_core<false>;
  591. using global_table = table_core<true>;
  592. using main_table = main_table_core<false>;
  593. using main_global_table = main_table_core<true>;
  594. using stack_table = stack_table_core<false>;
  595. using stack_global_table = stack_table_core<true>;
  596. template <typename>
  597. struct basic_lua_table;
  598. using lua_table = basic_lua_table<reference>;
  599. using stack_lua_table = basic_lua_table<stack_reference>;
  600. template <typename T, typename base_type>
  601. class basic_usertype;
  602. template <typename T>
  603. using usertype = basic_usertype<T, reference>;
  604. template <typename T>
  605. using stack_usertype = basic_usertype<T, stack_reference>;
  606. template <typename base_type>
  607. class basic_metatable;
  608. using metatable = basic_metatable<reference>;
  609. using stack_metatable = basic_metatable<stack_reference>;
  610. template <typename base_t>
  611. struct basic_environment;
  612. using environment = basic_environment<reference>;
  613. using main_environment = basic_environment<main_reference>;
  614. using stack_environment = basic_environment<stack_reference>;
  615. template <typename T, bool>
  616. class basic_function;
  617. template <typename T, bool, typename H>
  618. class basic_protected_function;
  619. using unsafe_function = basic_function<reference, false>;
  620. using safe_function = basic_protected_function<reference, false, reference>;
  621. using main_unsafe_function = basic_function<main_reference, false>;
  622. using main_safe_function = basic_protected_function<main_reference, false, reference>;
  623. using stack_unsafe_function = basic_function<stack_reference, false>;
  624. using stack_safe_function = basic_protected_function<stack_reference, false, reference>;
  625. using stack_aligned_unsafe_function = basic_function<stack_reference, true>;
  626. using stack_aligned_safe_function = basic_protected_function<stack_reference, true, reference>;
  627. using protected_function = safe_function;
  628. using main_protected_function = main_safe_function;
  629. using stack_protected_function = stack_safe_function;
  630. using stack_aligned_protected_function = stack_aligned_safe_function;
  631. #if SOL_IS_ON(SOL_SAFE_FUNCTION_OBJECTS_I_)
  632. using function = protected_function;
  633. using main_function = main_protected_function;
  634. using stack_function = stack_protected_function;
  635. using stack_aligned_function = stack_aligned_safe_function;
  636. #else
  637. using function = unsafe_function;
  638. using main_function = main_unsafe_function;
  639. using stack_function = stack_unsafe_function;
  640. using stack_aligned_function = stack_aligned_unsafe_function;
  641. #endif
  642. using stack_aligned_stack_handler_function = basic_protected_function<stack_reference, true, stack_reference>;
  643. struct unsafe_function_result;
  644. struct protected_function_result;
  645. using safe_function_result = protected_function_result;
  646. #if SOL_IS_ON(SOL_SAFE_FUNCTION_OBJECTS_I_)
  647. using function_result = safe_function_result;
  648. #else
  649. using function_result = unsafe_function_result;
  650. #endif
  651. template <typename base_t>
  652. class basic_object_base;
  653. template <typename base_t>
  654. class basic_object;
  655. template <typename base_t>
  656. class basic_userdata;
  657. template <typename base_t>
  658. class basic_lightuserdata;
  659. template <typename base_t>
  660. class basic_coroutine;
  661. template <typename base_t>
  662. class basic_thread;
  663. using object = basic_object<reference>;
  664. using userdata = basic_userdata<reference>;
  665. using lightuserdata = basic_lightuserdata<reference>;
  666. using thread = basic_thread<reference>;
  667. using coroutine = basic_coroutine<reference>;
  668. using main_object = basic_object<main_reference>;
  669. using main_userdata = basic_userdata<main_reference>;
  670. using main_lightuserdata = basic_lightuserdata<main_reference>;
  671. using main_coroutine = basic_coroutine<main_reference>;
  672. using stack_object = basic_object<stack_reference>;
  673. using stack_userdata = basic_userdata<stack_reference>;
  674. using stack_lightuserdata = basic_lightuserdata<stack_reference>;
  675. using stack_thread = basic_thread<stack_reference>;
  676. using stack_coroutine = basic_coroutine<stack_reference>;
  677. struct stack_proxy_base;
  678. struct stack_proxy;
  679. struct variadic_args;
  680. struct variadic_results;
  681. struct stack_count;
  682. struct this_state;
  683. struct this_main_state;
  684. struct this_environment;
  685. class state_view;
  686. class state;
  687. template <typename T>
  688. struct as_table_t;
  689. template <typename T>
  690. struct as_container_t;
  691. template <typename T>
  692. struct nested;
  693. template <typename T>
  694. struct light;
  695. template <typename T>
  696. struct user;
  697. template <typename T>
  698. struct as_args_t;
  699. template <typename T>
  700. struct protect_t;
  701. template <typename F, typename... Policies>
  702. struct policy_wrapper;
  703. template <typename T>
  704. struct usertype_traits;
  705. template <typename T>
  706. struct unique_usertype_traits;
  707. template <typename... Args>
  708. struct types {
  709. typedef std::make_index_sequence<sizeof...(Args)> indices;
  710. static constexpr std::size_t size() {
  711. return sizeof...(Args);
  712. }
  713. };
  714. template <typename T>
  715. struct derive : std::false_type {
  716. typedef types<> type;
  717. };
  718. template <typename T>
  719. struct base : std::false_type {
  720. typedef types<> type;
  721. };
  722. template <typename T>
  723. struct weak_derive {
  724. static bool value;
  725. };
  726. template <typename T>
  727. bool weak_derive<T>::value = false;
  728. namespace stack {
  729. struct record;
  730. }
  731. #if SOL_IS_OFF(SOL_USE_BOOST_I_)
  732. template <class T>
  733. class optional;
  734. template <class T>
  735. class optional<T&>;
  736. #endif
  737. using check_handler_type = int(lua_State*, int, type, type, const char*);
  738. } // namespace sol
  739. #define SOL_BASE_CLASSES(T, ...) \
  740. namespace sol { \
  741. template <> \
  742. struct base<T> : std::true_type { \
  743. typedef ::sol::types<__VA_ARGS__> type; \
  744. }; \
  745. } \
  746. void a_sol3_detail_function_decl_please_no_collide()
  747. #define SOL_DERIVED_CLASSES(T, ...) \
  748. namespace sol { \
  749. template <> \
  750. struct derive<T> : std::true_type { \
  751. typedef ::sol::types<__VA_ARGS__> type; \
  752. }; \
  753. } \
  754. void a_sol3_detail_function_decl_please_no_collide()
  755. #endif // SOL_FORWARD_HPP
  756. // end of sol/forward.hpp
  757. // beginning of sol/forward_detail.hpp
  758. #ifndef SOL_FORWARD_DETAIL_HPP
  759. #define SOL_FORWARD_DETAIL_HPP
  760. // beginning of sol/traits.hpp
  761. // beginning of sol/tuple.hpp
  762. // beginning of sol/base_traits.hpp
  763. #include <type_traits>
  764. namespace sol {
  765. namespace detail {
  766. struct unchecked_t {};
  767. const unchecked_t unchecked = unchecked_t{};
  768. } // namespace detail
  769. namespace meta {
  770. using sfinae_yes_t = std::true_type;
  771. using sfinae_no_t = std::false_type;
  772. template <typename T>
  773. using void_t = void;
  774. template <typename T>
  775. using unqualified = std::remove_cv<std::remove_reference_t<T>>;
  776. template <typename T>
  777. using unqualified_t = typename unqualified<T>::type;
  778. namespace meta_detail {
  779. template <typename T>
  780. struct unqualified_non_alias : unqualified<T> {};
  781. template <template <class...> class Test, class, class... Args>
  782. struct is_detected : std::false_type {};
  783. template <template <class...> class Test, class... Args>
  784. struct is_detected<Test, void_t<Test<Args...>>, Args...> : std::true_type {};
  785. } // namespace meta_detail
  786. template <template <class...> class Trait, class... Args>
  787. using is_detected = typename meta_detail::is_detected<Trait, void, Args...>::type;
  788. template <template <class...> class Trait, class... Args>
  789. constexpr inline bool is_detected_v = is_detected<Trait, Args...>::value;
  790. template <std::size_t I>
  791. using index_value = std::integral_constant<std::size_t, I>;
  792. template <bool>
  793. struct conditional {
  794. template <typename T, typename U>
  795. using type = T;
  796. };
  797. template <>
  798. struct conditional<false> {
  799. template <typename T, typename U>
  800. using type = U;
  801. };
  802. template <bool B, typename T, typename U>
  803. using conditional_t = typename conditional<B>::template type<T, U>;
  804. namespace meta_detail {
  805. template <typename T, template <typename...> class Templ>
  806. struct is_specialization_of : std::false_type {};
  807. template <typename... T, template <typename...> class Templ>
  808. struct is_specialization_of<Templ<T...>, Templ> : std::true_type {};
  809. } // namespace meta_detail
  810. template <typename T, template <typename...> class Templ>
  811. using is_specialization_of = meta_detail::is_specialization_of<std::remove_cv_t<T>, Templ>;
  812. template <typename T, template <typename...> class Templ>
  813. inline constexpr bool is_specialization_of_v = is_specialization_of<std::remove_cv_t<T>, Templ>::value;
  814. template <typename T>
  815. struct identity {
  816. typedef T type;
  817. };
  818. template <typename T>
  819. using identity_t = typename identity<T>::type;
  820. template <typename T>
  821. using is_builtin_type = std::integral_constant<bool, std::is_arithmetic<T>::value || std::is_pointer<T>::value || std::is_array<T>::value>;
  822. } // namespace meta
  823. } // namespace sol
  824. // end of sol/base_traits.hpp
  825. #include <tuple>
  826. #include <cstddef>
  827. namespace sol {
  828. namespace detail {
  829. using swallow = std::initializer_list<int>;
  830. } // namespace detail
  831. namespace meta {
  832. template <typename T>
  833. using is_tuple = is_specialization_of<T, std::tuple>;
  834. template <typename T>
  835. constexpr inline bool is_tuple_v = is_tuple<T>::value;
  836. namespace detail {
  837. template <typename... Args>
  838. struct tuple_types_ { typedef types<Args...> type; };
  839. template <typename... Args>
  840. struct tuple_types_<std::tuple<Args...>> { typedef types<Args...> type; };
  841. } // namespace detail
  842. template <typename... Args>
  843. using tuple_types = typename detail::tuple_types_<Args...>::type;
  844. template <typename Arg>
  845. struct pop_front_type;
  846. template <typename Arg>
  847. using pop_front_type_t = typename pop_front_type<Arg>::type;
  848. template <typename... Args>
  849. struct pop_front_type<types<Args...>> {
  850. typedef void front_type;
  851. typedef types<Args...> type;
  852. };
  853. template <typename Arg, typename... Args>
  854. struct pop_front_type<types<Arg, Args...>> {
  855. typedef Arg front_type;
  856. typedef types<Args...> type;
  857. };
  858. template <std::size_t N, typename Tuple>
  859. using tuple_element = std::tuple_element<N, std::remove_reference_t<Tuple>>;
  860. template <std::size_t N, typename Tuple>
  861. using tuple_element_t = std::tuple_element_t<N, std::remove_reference_t<Tuple>>;
  862. template <std::size_t N, typename Tuple>
  863. using unqualified_tuple_element = unqualified<tuple_element_t<N, Tuple>>;
  864. template <std::size_t N, typename Tuple>
  865. using unqualified_tuple_element_t = unqualified_t<tuple_element_t<N, Tuple>>;
  866. } // namespace meta
  867. } // namespace sol
  868. // end of sol/tuple.hpp
  869. // beginning of sol/bind_traits.hpp
  870. namespace sol { namespace meta {
  871. namespace meta_detail {
  872. template <class F>
  873. struct check_deducible_signature {
  874. struct nat {};
  875. template <class G>
  876. static auto test(int) -> decltype(&G::operator(), void());
  877. template <class>
  878. static auto test(...) -> nat;
  879. using type = std::is_void<decltype(test<F>(0))>;
  880. };
  881. } // namespace meta_detail
  882. template <class F>
  883. struct has_deducible_signature : meta_detail::check_deducible_signature<F>::type {};
  884. namespace meta_detail {
  885. template <std::size_t I, typename T>
  886. struct void_tuple_element : meta::tuple_element<I, T> {};
  887. template <std::size_t I>
  888. struct void_tuple_element<I, std::tuple<>> {
  889. typedef void type;
  890. };
  891. template <std::size_t I, typename T>
  892. using void_tuple_element_t = typename void_tuple_element<I, T>::type;
  893. template <bool it_is_noexcept, bool has_c_variadic, typename T, typename R, typename... Args>
  894. struct basic_traits {
  895. private:
  896. using first_type = meta::conditional_t<std::is_void<T>::value, int, T>&;
  897. public:
  898. inline static constexpr const bool is_noexcept = it_is_noexcept;
  899. inline static constexpr bool is_member_function = std::is_void<T>::value;
  900. inline static constexpr bool has_c_var_arg = has_c_variadic;
  901. inline static constexpr std::size_t arity = sizeof...(Args);
  902. inline static constexpr std::size_t free_arity = sizeof...(Args) + static_cast<std::size_t>(!std::is_void<T>::value);
  903. typedef types<Args...> args_list;
  904. typedef std::tuple<Args...> args_tuple;
  905. typedef T object_type;
  906. typedef R return_type;
  907. typedef tuple_types<R> returns_list;
  908. typedef R(function_type)(Args...);
  909. typedef meta::conditional_t<std::is_void<T>::value, args_list, types<first_type, Args...>> free_args_list;
  910. typedef meta::conditional_t<std::is_void<T>::value, R(Args...), R(first_type, Args...)> free_function_type;
  911. typedef meta::conditional_t<std::is_void<T>::value, R (*)(Args...), R (*)(first_type, Args...)> free_function_pointer_type;
  912. typedef std::remove_pointer_t<free_function_pointer_type> signature_type;
  913. template <std::size_t i>
  914. using arg_at = void_tuple_element_t<i, args_tuple>;
  915. };
  916. template <typename Signature, bool b = has_deducible_signature<Signature>::value>
  917. struct fx_traits : basic_traits<false, false, void, void> {};
  918. // Free Functions
  919. template <typename R, typename... Args>
  920. struct fx_traits<R(Args...), false> : basic_traits<false, false, void, R, Args...> {
  921. typedef R (*function_pointer_type)(Args...);
  922. };
  923. template <typename R, typename... Args>
  924. struct fx_traits<R (*)(Args...), false> : basic_traits<false, false, void, R, Args...> {
  925. typedef R (*function_pointer_type)(Args...);
  926. };
  927. template <typename R, typename... Args>
  928. struct fx_traits<R(Args..., ...), false> : basic_traits<false, true, void, R, Args...> {
  929. typedef R (*function_pointer_type)(Args..., ...);
  930. };
  931. template <typename R, typename... Args>
  932. struct fx_traits<R (*)(Args..., ...), false> : basic_traits<false, true, void, R, Args...> {
  933. typedef R (*function_pointer_type)(Args..., ...);
  934. };
  935. // Member Functions
  936. /* C-Style Variadics */
  937. template <typename T, typename R, typename... Args>
  938. struct fx_traits<R (T::*)(Args...), false> : basic_traits<false, false, T, R, Args...> {
  939. typedef R (T::*function_pointer_type)(Args...);
  940. };
  941. template <typename T, typename R, typename... Args>
  942. struct fx_traits<R (T::*)(Args..., ...), false> : basic_traits<false, true, T, R, Args...> {
  943. typedef R (T::*function_pointer_type)(Args..., ...);
  944. };
  945. /* Const Volatile */
  946. template <typename T, typename R, typename... Args>
  947. struct fx_traits<R (T::*)(Args...) const, false> : basic_traits<false, false, T, R, Args...> {
  948. typedef R (T::*function_pointer_type)(Args...) const;
  949. };
  950. template <typename T, typename R, typename... Args>
  951. struct fx_traits<R (T::*)(Args..., ...) const, false> : basic_traits<false, true, T, R, Args...> {
  952. typedef R (T::*function_pointer_type)(Args..., ...) const;
  953. };
  954. template <typename T, typename R, typename... Args>
  955. struct fx_traits<R (T::*)(Args...) const volatile, false> : basic_traits<false, false, T, R, Args...> {
  956. typedef R (T::*function_pointer_type)(Args...) const volatile;
  957. };
  958. template <typename T, typename R, typename... Args>
  959. struct fx_traits<R (T::*)(Args..., ...) const volatile, false> : basic_traits<false, true, T, R, Args...> {
  960. typedef R (T::*function_pointer_type)(Args..., ...) const volatile;
  961. };
  962. /* Member Function Qualifiers */
  963. template <typename T, typename R, typename... Args>
  964. struct fx_traits<R (T::*)(Args...)&, false> : basic_traits<false, false, T, R, Args...> {
  965. typedef R (T::*function_pointer_type)(Args...) &;
  966. };
  967. template <typename T, typename R, typename... Args>
  968. struct fx_traits<R (T::*)(Args..., ...)&, false> : basic_traits<false, true, T, R, Args...> {
  969. typedef R (T::*function_pointer_type)(Args..., ...) &;
  970. };
  971. template <typename T, typename R, typename... Args>
  972. struct fx_traits<R (T::*)(Args...) const&, false> : basic_traits<false, false, T, R, Args...> {
  973. typedef R (T::*function_pointer_type)(Args...) const&;
  974. };
  975. template <typename T, typename R, typename... Args>
  976. struct fx_traits<R (T::*)(Args..., ...) const&, false> : basic_traits<false, true, T, R, Args...> {
  977. typedef R (T::*function_pointer_type)(Args..., ...) const&;
  978. };
  979. template <typename T, typename R, typename... Args>
  980. struct fx_traits<R (T::*)(Args...) const volatile&, false> : basic_traits<false, false, T, R, Args...> {
  981. typedef R (T::*function_pointer_type)(Args...) const volatile&;
  982. };
  983. template <typename T, typename R, typename... Args>
  984. struct fx_traits<R (T::*)(Args..., ...) const volatile&, false> : basic_traits<false, true, T, R, Args...> {
  985. typedef R (T::*function_pointer_type)(Args..., ...) const volatile&;
  986. };
  987. template <typename T, typename R, typename... Args>
  988. struct fx_traits<R (T::*)(Args...)&&, false> : basic_traits<false, false, T, R, Args...> {
  989. typedef R (T::*function_pointer_type)(Args...) &&;
  990. };
  991. template <typename T, typename R, typename... Args>
  992. struct fx_traits<R (T::*)(Args..., ...)&&, false> : basic_traits<false, true, T, R, Args...> {
  993. typedef R (T::*function_pointer_type)(Args..., ...) &&;
  994. };
  995. template <typename T, typename R, typename... Args>
  996. struct fx_traits<R (T::*)(Args...) const&&, false> : basic_traits<false, false, T, R, Args...> {
  997. typedef R (T::*function_pointer_type)(Args...) const&&;
  998. };
  999. template <typename T, typename R, typename... Args>
  1000. struct fx_traits<R (T::*)(Args..., ...) const&&, false> : basic_traits<false, true, T, R, Args...> {
  1001. typedef R (T::*function_pointer_type)(Args..., ...) const&&;
  1002. };
  1003. template <typename T, typename R, typename... Args>
  1004. struct fx_traits<R (T::*)(Args...) const volatile&&, false> : basic_traits<false, false, T, R, Args...> {
  1005. typedef R (T::*function_pointer_type)(Args...) const volatile&&;
  1006. };
  1007. template <typename T, typename R, typename... Args>
  1008. struct fx_traits<R (T::*)(Args..., ...) const volatile&&, false> : basic_traits<false, true, T, R, Args...> {
  1009. typedef R (T::*function_pointer_type)(Args..., ...) const volatile&&;
  1010. };
  1011. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  1012. template <typename R, typename... Args>
  1013. struct fx_traits<R(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  1014. typedef R (*function_pointer_type)(Args...) noexcept;
  1015. };
  1016. template <typename R, typename... Args>
  1017. struct fx_traits<R (*)(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  1018. typedef R (*function_pointer_type)(Args...) noexcept;
  1019. };
  1020. template <typename R, typename... Args>
  1021. struct fx_traits<R(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  1022. typedef R (*function_pointer_type)(Args..., ...) noexcept;
  1023. };
  1024. template <typename R, typename... Args>
  1025. struct fx_traits<R (*)(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  1026. typedef R (*function_pointer_type)(Args..., ...) noexcept;
  1027. };
  1028. template <typename T, typename R, typename... Args>
  1029. struct fx_traits<R (T::*)(Args...) noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1030. typedef R (T::*function_pointer_type)(Args...) noexcept;
  1031. };
  1032. template <typename T, typename R, typename... Args>
  1033. struct fx_traits<R (T::*)(Args..., ...) noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1034. typedef R (T::*function_pointer_type)(Args..., ...) noexcept;
  1035. };
  1036. /* Const Volatile */
  1037. template <typename T, typename R, typename... Args>
  1038. struct fx_traits<R (T::*)(Args...) const noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1039. typedef R (T::*function_pointer_type)(Args...) const noexcept;
  1040. };
  1041. template <typename T, typename R, typename... Args>
  1042. struct fx_traits<R (T::*)(Args..., ...) const noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1043. typedef R (T::*function_pointer_type)(Args..., ...) const noexcept;
  1044. };
  1045. template <typename T, typename R, typename... Args>
  1046. struct fx_traits<R (T::*)(Args...) const volatile noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1047. typedef R (T::*function_pointer_type)(Args...) const volatile noexcept;
  1048. };
  1049. template <typename T, typename R, typename... Args>
  1050. struct fx_traits<R (T::*)(Args..., ...) const volatile noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1051. typedef R (T::*function_pointer_type)(Args..., ...) const volatile noexcept;
  1052. };
  1053. template <typename T, typename R, typename... Args>
  1054. struct fx_traits<R (T::*)(Args...) & noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1055. typedef R (T::*function_pointer_type)(Args...) & noexcept;
  1056. };
  1057. template <typename T, typename R, typename... Args>
  1058. struct fx_traits<R (T::*)(Args..., ...) & noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1059. typedef R (T::*function_pointer_type)(Args..., ...) & noexcept;
  1060. };
  1061. template <typename T, typename R, typename... Args>
  1062. struct fx_traits<R (T::*)(Args...) const& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1063. typedef R (T::*function_pointer_type)(Args...) const& noexcept;
  1064. };
  1065. template <typename T, typename R, typename... Args>
  1066. struct fx_traits<R (T::*)(Args..., ...) const& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1067. typedef R (T::*function_pointer_type)(Args..., ...) const& noexcept;
  1068. };
  1069. template <typename T, typename R, typename... Args>
  1070. struct fx_traits<R (T::*)(Args...) const volatile& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1071. typedef R (T::*function_pointer_type)(Args...) const volatile& noexcept;
  1072. };
  1073. template <typename T, typename R, typename... Args>
  1074. struct fx_traits<R (T::*)(Args..., ...) const volatile& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1075. typedef R (T::*function_pointer_type)(Args..., ...) const volatile& noexcept;
  1076. };
  1077. template <typename T, typename R, typename... Args>
  1078. struct fx_traits<R (T::*)(Args...) && noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1079. typedef R (T::*function_pointer_type)(Args...) && noexcept;
  1080. };
  1081. template <typename T, typename R, typename... Args>
  1082. struct fx_traits<R (T::*)(Args..., ...) && noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1083. typedef R (T::*function_pointer_type)(Args..., ...) && noexcept;
  1084. };
  1085. template <typename T, typename R, typename... Args>
  1086. struct fx_traits<R (T::*)(Args...) const&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1087. typedef R (T::*function_pointer_type)(Args...) const&& noexcept;
  1088. };
  1089. template <typename T, typename R, typename... Args>
  1090. struct fx_traits<R (T::*)(Args..., ...) const&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1091. typedef R (T::*function_pointer_type)(Args..., ...) const&& noexcept;
  1092. };
  1093. template <typename T, typename R, typename... Args>
  1094. struct fx_traits<R (T::*)(Args...) const volatile&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1095. typedef R (T::*function_pointer_type)(Args...) const volatile&& noexcept;
  1096. };
  1097. template <typename T, typename R, typename... Args>
  1098. struct fx_traits<R (T::*)(Args..., ...) const volatile&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1099. typedef R (T::*function_pointer_type)(Args..., ...) const volatile&& noexcept;
  1100. };
  1101. #endif // noexcept is part of a function's type
  1102. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_) && SOL_IS_ON(SOL_PLATFORM_X86_I_)
  1103. template <typename R, typename... Args>
  1104. struct fx_traits<R __stdcall(Args...), false> : basic_traits<false, false, void, R, Args...> {
  1105. typedef R(__stdcall* function_pointer_type)(Args...);
  1106. };
  1107. template <typename R, typename... Args>
  1108. struct fx_traits<R(__stdcall*)(Args...), false> : basic_traits<false, false, void, R, Args...> {
  1109. typedef R(__stdcall* function_pointer_type)(Args...);
  1110. };
  1111. template <typename T, typename R, typename... Args>
  1112. struct fx_traits<R (__stdcall T::*)(Args...), false> : basic_traits<false, false, T, R, Args...> {
  1113. typedef R (__stdcall T::*function_pointer_type)(Args...);
  1114. };
  1115. /* Const Volatile */
  1116. template <typename T, typename R, typename... Args>
  1117. struct fx_traits<R (__stdcall T::*)(Args...) const, false> : basic_traits<false, false, T, R, Args...> {
  1118. typedef R (__stdcall T::*function_pointer_type)(Args...) const;
  1119. };
  1120. template <typename T, typename R, typename... Args>
  1121. struct fx_traits<R (__stdcall T::*)(Args...) const volatile, false> : basic_traits<false, false, T, R, Args...> {
  1122. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile;
  1123. };
  1124. /* Member Function Qualifiers */
  1125. template <typename T, typename R, typename... Args>
  1126. struct fx_traits<R (__stdcall T::*)(Args...)&, false> : basic_traits<false, false, T, R, Args...> {
  1127. typedef R (__stdcall T::*function_pointer_type)(Args...) &;
  1128. };
  1129. template <typename T, typename R, typename... Args>
  1130. struct fx_traits<R (__stdcall T::*)(Args...) const&, false> : basic_traits<false, false, T, R, Args...> {
  1131. typedef R (__stdcall T::*function_pointer_type)(Args...) const&;
  1132. };
  1133. template <typename T, typename R, typename... Args>
  1134. struct fx_traits<R (__stdcall T::*)(Args...) const volatile&, false> : basic_traits<false, false, T, R, Args...> {
  1135. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&;
  1136. };
  1137. template <typename T, typename R, typename... Args>
  1138. struct fx_traits<R (__stdcall T::*)(Args...)&&, false> : basic_traits<false, false, T, R, Args...> {
  1139. typedef R (__stdcall T::*function_pointer_type)(Args...) &&;
  1140. };
  1141. template <typename T, typename R, typename... Args>
  1142. struct fx_traits<R (__stdcall T::*)(Args...) const&&, false> : basic_traits<false, false, T, R, Args...> {
  1143. typedef R (__stdcall T::*function_pointer_type)(Args...) const&&;
  1144. };
  1145. template <typename T, typename R, typename... Args>
  1146. struct fx_traits<R (__stdcall T::*)(Args...) const volatile&&, false> : basic_traits<false, false, T, R, Args...> {
  1147. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&&;
  1148. };
  1149. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  1150. template <typename R, typename... Args>
  1151. struct fx_traits<R __stdcall(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  1152. typedef R(__stdcall* function_pointer_type)(Args...) noexcept;
  1153. };
  1154. template <typename R, typename... Args>
  1155. struct fx_traits<R(__stdcall*)(Args...) noexcept, false> : basic_traits<true, false, void, R, Args...> {
  1156. typedef R(__stdcall* function_pointer_type)(Args...) noexcept;
  1157. };
  1158. /* __stdcall cannot be applied to functions with varargs*/
  1159. /*template <typename R, typename... Args>
  1160. struct fx_traits<__stdcall R(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  1161. typedef R(__stdcall* function_pointer_type)(Args..., ...) noexcept;
  1162. };
  1163. template <typename R, typename... Args>
  1164. struct fx_traits<R (__stdcall *)(Args..., ...) noexcept, false> : basic_traits<true, true, void, R, Args...> {
  1165. typedef R(__stdcall* function_pointer_type)(Args..., ...) noexcept;
  1166. };*/
  1167. template <typename T, typename R, typename... Args>
  1168. struct fx_traits<R (__stdcall T::*)(Args...) noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1169. typedef R (__stdcall T::*function_pointer_type)(Args...) noexcept;
  1170. };
  1171. /* __stdcall does not work with varargs */
  1172. /*template <typename T, typename R, typename... Args>
  1173. struct fx_traits<R (__stdcall T::*)(Args..., ...) noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1174. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) noexcept;
  1175. };*/
  1176. /* Const Volatile */
  1177. template <typename T, typename R, typename... Args>
  1178. struct fx_traits<R (__stdcall T::*)(Args...) const noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1179. typedef R (__stdcall T::*function_pointer_type)(Args...) const noexcept;
  1180. };
  1181. /* __stdcall does not work with varargs */
  1182. /*template <typename T, typename R, typename... Args>
  1183. struct fx_traits<R (__stdcall T::*)(Args..., ...) const noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1184. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const noexcept;
  1185. };*/
  1186. template <typename T, typename R, typename... Args>
  1187. struct fx_traits<R (__stdcall T::*)(Args...) const volatile noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1188. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile noexcept;
  1189. };
  1190. /* __stdcall does not work with varargs */
  1191. /*template <typename T, typename R, typename... Args>
  1192. struct fx_traits<R (__stdcall T::*)(Args..., ...) const volatile noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1193. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const volatile noexcept;
  1194. };*/
  1195. template <typename T, typename R, typename... Args>
  1196. struct fx_traits<R (__stdcall T::*)(Args...) & noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1197. typedef R (__stdcall T::*function_pointer_type)(Args...) & noexcept;
  1198. };
  1199. /* __stdcall does not work with varargs */
  1200. /*template <typename T, typename R, typename... Args>
  1201. struct fx_traits<R (__stdcall T::*)(Args..., ...) & noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1202. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) & noexcept;
  1203. };*/
  1204. template <typename T, typename R, typename... Args>
  1205. struct fx_traits<R (__stdcall T::*)(Args...) const& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1206. typedef R (__stdcall T::*function_pointer_type)(Args...) const& noexcept;
  1207. };
  1208. /* __stdcall does not work with varargs */
  1209. /*template <typename T, typename R, typename... Args>
  1210. struct fx_traits<R (__stdcall T::*)(Args..., ...) const& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1211. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const& noexcept;
  1212. };*/
  1213. template <typename T, typename R, typename... Args>
  1214. struct fx_traits<R (__stdcall T::*)(Args...) const volatile& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1215. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile& noexcept;
  1216. };
  1217. /* __stdcall does not work with varargs */
  1218. /*template <typename T, typename R, typename... Args>
  1219. struct fx_traits<R (__stdcall T::*)(Args..., ...) const volatile& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1220. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const volatile& noexcept;
  1221. };*/
  1222. template <typename T, typename R, typename... Args>
  1223. struct fx_traits<R (__stdcall T::*)(Args...) && noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1224. typedef R (__stdcall T::*function_pointer_type)(Args...) && noexcept;
  1225. };
  1226. /* __stdcall does not work with varargs */
  1227. /*template <typename T, typename R, typename... Args>
  1228. struct fx_traits<R (__stdcall T::*)(Args..., ...) && noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1229. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) && noexcept;
  1230. };*/
  1231. template <typename T, typename R, typename... Args>
  1232. struct fx_traits<R (__stdcall T::*)(Args...) const&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1233. typedef R (__stdcall T::*function_pointer_type)(Args...) const&& noexcept;
  1234. };
  1235. /* __stdcall does not work with varargs */
  1236. /*template <typename T, typename R, typename... Args>
  1237. struct fx_traits<R (__stdcall T::*)(Args..., ...) const&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1238. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const&& noexcept;
  1239. };*/
  1240. template <typename T, typename R, typename... Args>
  1241. struct fx_traits<R (__stdcall T::*)(Args...) const volatile&& noexcept, false> : basic_traits<true, false, T, R, Args...> {
  1242. typedef R (__stdcall T::*function_pointer_type)(Args...) const volatile&& noexcept;
  1243. };
  1244. /* __stdcall does not work with varargs */
  1245. /*template <typename T, typename R, typename... Args>
  1246. struct fx_traits<R (__stdcall T::*)(Args..., ...) const volatile&& noexcept, false> : basic_traits<true, true, T, R, Args...> {
  1247. typedef R (__stdcall T::*function_pointer_type)(Args..., ...) const volatile&& noexcept;
  1248. };*/
  1249. #endif // noexcept is part of a function's type
  1250. #endif // __stdcall x86 VC++ bug
  1251. template <typename Signature>
  1252. struct fx_traits<Signature, true>
  1253. : public fx_traits<typename fx_traits<decltype(&Signature::operator())>::function_type, false> {};
  1254. template <typename Signature, bool b = std::is_member_object_pointer<Signature>::value>
  1255. struct callable_traits
  1256. : public fx_traits<std::decay_t<Signature>> {};
  1257. template <typename R, typename T>
  1258. struct callable_traits<R(T::*), true> {
  1259. typedef meta::conditional_t<std::is_array_v<R>, std::add_lvalue_reference_t<R>, R> return_type;
  1260. typedef return_type Arg;
  1261. typedef T object_type;
  1262. using signature_type = R(T::*);
  1263. inline static constexpr bool is_noexcept = false;
  1264. inline static constexpr bool is_member_function = false;
  1265. inline static constexpr std::size_t arity = 1;
  1266. inline static constexpr std::size_t free_arity = 2;
  1267. typedef std::tuple<Arg> args_tuple;
  1268. typedef types<Arg> args_list;
  1269. typedef types<T, Arg> free_args_list;
  1270. typedef meta::tuple_types<return_type> returns_list;
  1271. typedef return_type(function_type)(T&, return_type);
  1272. typedef return_type (*function_pointer_type)(T&, Arg);
  1273. typedef return_type (*free_function_pointer_type)(T&, Arg);
  1274. template <std::size_t i>
  1275. using arg_at = void_tuple_element_t<i, args_tuple>;
  1276. };
  1277. } // namespace meta_detail
  1278. template <typename Signature>
  1279. struct bind_traits : meta_detail::callable_traits<Signature> {};
  1280. template <typename Signature>
  1281. using function_args_t = typename bind_traits<Signature>::args_list;
  1282. template <typename Signature>
  1283. using function_signature_t = typename bind_traits<Signature>::signature_type;
  1284. template <typename Signature>
  1285. using function_return_t = typename bind_traits<Signature>::return_type;
  1286. }} // namespace sol::meta
  1287. // end of sol/bind_traits.hpp
  1288. // beginning of sol/pointer_like.hpp
  1289. #include <utility>
  1290. #include <type_traits>
  1291. namespace sol {
  1292. namespace meta {
  1293. namespace meta_detail {
  1294. template <typename T>
  1295. using is_dereferenceable_test = decltype(*std::declval<T>());
  1296. template <typename T>
  1297. using is_explicitly_dereferenceable_test = decltype(std::declval<T>().operator*());
  1298. }
  1299. template <typename T>
  1300. using is_pointer_like = std::integral_constant<bool, !std::is_array_v<T> && (std::is_pointer_v<T> || is_detected_v<meta_detail::is_explicitly_dereferenceable_test, T>)>;
  1301. template <typename T>
  1302. constexpr inline bool is_pointer_like_v = is_pointer_like<T>::value;
  1303. } // namespace meta
  1304. namespace detail {
  1305. template <typename T>
  1306. auto unwrap(T&& item) -> decltype(std::forward<T>(item)) {
  1307. return std::forward<T>(item);
  1308. }
  1309. template <typename T>
  1310. T& unwrap(std::reference_wrapper<T> arg) {
  1311. return arg.get();
  1312. }
  1313. template <typename T>
  1314. inline decltype(auto) deref(T&& item) {
  1315. using Tu = meta::unqualified_t<T>;
  1316. if constexpr (meta::is_pointer_like_v<Tu>) {
  1317. return *std::forward<T>(item);
  1318. }
  1319. else {
  1320. return std::forward<T>(item);
  1321. }
  1322. }
  1323. template <typename T>
  1324. inline decltype(auto) deref_move_only(T&& item) {
  1325. using Tu = meta::unqualified_t<T>;
  1326. if constexpr (meta::is_pointer_like_v<Tu> && !std::is_pointer_v<Tu> && !std::is_copy_constructible_v<Tu>) {
  1327. return *std::forward<T>(item);
  1328. }
  1329. else {
  1330. return std::forward<T>(item);
  1331. }
  1332. }
  1333. template <typename T>
  1334. inline T* ptr(T& val) {
  1335. return std::addressof(val);
  1336. }
  1337. template <typename T>
  1338. inline T* ptr(std::reference_wrapper<T> val) {
  1339. return std::addressof(val.get());
  1340. }
  1341. template <typename T>
  1342. inline T* ptr(T* val) {
  1343. return val;
  1344. }
  1345. } // namespace detail
  1346. } // namespace sol
  1347. // end of sol/pointer_like.hpp
  1348. // beginning of sol/string_view.hpp
  1349. #include <cstddef>
  1350. #include <string>
  1351. #include <string_view>
  1352. #include <functional>
  1353. namespace sol {
  1354. template <typename C, typename T = std::char_traits<C>>
  1355. using basic_string_view = std::basic_string_view<C, T>;
  1356. typedef std::string_view string_view;
  1357. typedef std::wstring_view wstring_view;
  1358. typedef std::u16string_view u16string_view;
  1359. typedef std::u32string_view u32string_view;
  1360. typedef std::hash<std::string_view> string_view_hash;
  1361. } // namespace sol
  1362. // end of sol/string_view.hpp
  1363. #include <type_traits>
  1364. #include <cstdint>
  1365. #include <memory>
  1366. #include <functional>
  1367. #include <array>
  1368. #include <iterator>
  1369. #include <iosfwd>
  1370. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  1371. #include <variant>
  1372. #endif // variant is weird on XCode, thanks XCode
  1373. namespace sol { namespace meta {
  1374. template <typename T>
  1375. struct unwrapped {
  1376. typedef T type;
  1377. };
  1378. template <typename T>
  1379. struct unwrapped<std::reference_wrapper<T>> {
  1380. typedef T type;
  1381. };
  1382. template <typename T>
  1383. using unwrapped_t = typename unwrapped<T>::type;
  1384. template <typename T>
  1385. struct unwrap_unqualified : unwrapped<unqualified_t<T>> {};
  1386. template <typename T>
  1387. using unwrap_unqualified_t = typename unwrap_unqualified<T>::type;
  1388. template <typename T>
  1389. struct remove_member_pointer;
  1390. template <typename R, typename T>
  1391. struct remove_member_pointer<R T::*> {
  1392. typedef R type;
  1393. };
  1394. template <typename R, typename T>
  1395. struct remove_member_pointer<R T::*const> {
  1396. typedef R type;
  1397. };
  1398. template <typename T>
  1399. using remove_member_pointer_t = remove_member_pointer<T>;
  1400. template <typename T, typename...>
  1401. struct all_same : std::true_type {};
  1402. template <typename T, typename U, typename... Args>
  1403. struct all_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T, U>::value && all_same<T, Args...>::value> {};
  1404. template <typename T, typename...>
  1405. struct any_same : std::false_type {};
  1406. template <typename T, typename U, typename... Args>
  1407. struct any_same<T, U, Args...> : std::integral_constant<bool, std::is_same<T, U>::value || any_same<T, Args...>::value> {};
  1408. template <typename T, typename... Args>
  1409. constexpr inline bool any_same_v = any_same<T, Args...>::value;
  1410. template <bool B>
  1411. using boolean = std::integral_constant<bool, B>;
  1412. template <bool B>
  1413. constexpr inline bool boolean_v = boolean<B>::value;
  1414. template <typename T>
  1415. using neg = boolean<!T::value>;
  1416. template <typename T>
  1417. constexpr inline bool neg_v = neg<T>::value;
  1418. template <typename... Args>
  1419. struct all : boolean<true> {};
  1420. template <typename T, typename... Args>
  1421. struct all<T, Args...> : std::conditional_t<T::value, all<Args...>, boolean<false>> {};
  1422. template <typename... Args>
  1423. struct any : boolean<false> {};
  1424. template <typename T, typename... Args>
  1425. struct any<T, Args...> : std::conditional_t<T::value, boolean<true>, any<Args...>> {};
  1426. template <typename T, typename... Args>
  1427. constexpr inline bool all_v = all<T, Args...>::value;
  1428. template <typename T, typename... Args>
  1429. constexpr inline bool any_v = any<T, Args...>::value;
  1430. enum class enable_t { _ };
  1431. constexpr const auto enabler = enable_t::_;
  1432. template <bool value, typename T = void>
  1433. using disable_if_t = std::enable_if_t<!value, T>;
  1434. template <typename... Args>
  1435. using enable = std::enable_if_t<all<Args...>::value, enable_t>;
  1436. template <typename... Args>
  1437. using disable = std::enable_if_t<neg<all<Args...>>::value, enable_t>;
  1438. template <typename... Args>
  1439. using enable_any = std::enable_if_t<any<Args...>::value, enable_t>;
  1440. template <typename... Args>
  1441. using disable_any = std::enable_if_t<neg<any<Args...>>::value, enable_t>;
  1442. template <typename V, typename... Vs>
  1443. struct find_in_pack_v : boolean<false> {};
  1444. template <typename V, typename Vs1, typename... Vs>
  1445. struct find_in_pack_v<V, Vs1, Vs...> : any<boolean<(V::value == Vs1::value)>, find_in_pack_v<V, Vs...>> {};
  1446. namespace meta_detail {
  1447. template <std::size_t I, typename T, typename... Args>
  1448. struct index_in_pack : std::integral_constant<std::size_t, SIZE_MAX> {};
  1449. template <std::size_t I, typename T, typename T1, typename... Args>
  1450. struct index_in_pack<I, T, T1, Args...>
  1451. : conditional_t<std::is_same<T, T1>::value, std::integral_constant<std::ptrdiff_t, I>, index_in_pack<I + 1, T, Args...>> {};
  1452. } // namespace meta_detail
  1453. template <typename T, typename... Args>
  1454. struct index_in_pack : meta_detail::index_in_pack<0, T, Args...> {};
  1455. template <typename T, typename List>
  1456. struct index_in : meta_detail::index_in_pack<0, T, List> {};
  1457. template <typename T, typename... Args>
  1458. struct index_in<T, types<Args...>> : meta_detail::index_in_pack<0, T, Args...> {};
  1459. template <std::size_t I, typename... Args>
  1460. struct at_in_pack {};
  1461. template <std::size_t I, typename... Args>
  1462. using at_in_pack_t = typename at_in_pack<I, Args...>::type;
  1463. template <std::size_t I, typename Arg, typename... Args>
  1464. struct at_in_pack<I, Arg, Args...> : std::conditional<I == 0, Arg, at_in_pack_t<I - 1, Args...>> {};
  1465. template <typename Arg, typename... Args>
  1466. struct at_in_pack<0, Arg, Args...> {
  1467. typedef Arg type;
  1468. };
  1469. namespace meta_detail {
  1470. template <typename, typename TI>
  1471. using on_even = meta::boolean<(TI::value % 2) == 0>;
  1472. template <typename, typename TI>
  1473. using on_odd = meta::boolean<(TI::value % 2) == 1>;
  1474. template <typename, typename>
  1475. using on_always = std::true_type;
  1476. template <template <typename...> class When, std::size_t Limit, std::size_t I, template <typename...> class Pred, typename... Ts>
  1477. struct count_when_for_pack : std::integral_constant<std::size_t, 0> {};
  1478. template <template <typename...> class When, std::size_t Limit, std::size_t I, template <typename...> class Pred, typename T, typename... Ts>
  1479. struct count_when_for_pack<When, Limit, I, Pred, T, Ts...> : conditional_t < sizeof...(Ts)
  1480. == 0
  1481. || Limit<2, std::integral_constant<std::size_t, I + static_cast<std::size_t>(Limit != 0 && Pred<T>::value)>,
  1482. count_when_for_pack<When, Limit - static_cast<std::size_t>(When<T, std::integral_constant<std::size_t, I>>::value),
  1483. I + static_cast<std::size_t>(When<T, std::integral_constant<std::size_t, I>>::value&& Pred<T>::value), Pred, Ts...>> {};
  1484. } // namespace meta_detail
  1485. template <template <typename...> class Pred, typename... Ts>
  1486. struct count_for_pack : meta_detail::count_when_for_pack<meta_detail::on_always, sizeof...(Ts), 0, Pred, Ts...> {};
  1487. template <template <typename...> class Pred, typename... Ts>
  1488. inline constexpr std::size_t count_for_pack_v = count_for_pack<Pred, Ts...>::value;
  1489. template <template <typename...> class Pred, typename List>
  1490. struct count_for;
  1491. template <template <typename...> class Pred, typename... Args>
  1492. struct count_for<Pred, types<Args...>> : count_for_pack<Pred, Args...> {};
  1493. template <std::size_t Limit, template <typename...> class Pred, typename... Ts>
  1494. struct count_for_to_pack : meta_detail::count_when_for_pack<meta_detail::on_always, Limit, 0, Pred, Ts...> {};
  1495. template <std::size_t Limit, template <typename...> class Pred, typename... Ts>
  1496. inline constexpr std::size_t count_for_to_pack_v = count_for_to_pack<Limit, Pred, Ts...>::value;
  1497. template <template <typename...> class When, std::size_t Limit, template <typename...> class Pred, typename... Ts>
  1498. struct count_when_for_to_pack : meta_detail::count_when_for_pack<When, Limit, 0, Pred, Ts...> {};
  1499. template <template <typename...> class When, std::size_t Limit, template <typename...> class Pred, typename... Ts>
  1500. inline constexpr std::size_t count_when_for_to_pack_v = count_when_for_to_pack<When, Limit, Pred, Ts...>::value;
  1501. template <template <typename...> class Pred, typename... Ts>
  1502. using count_even_for_pack = count_when_for_to_pack<meta_detail::on_even, sizeof...(Ts), Pred, Ts...>;
  1503. template <template <typename...> class Pred, typename... Ts>
  1504. inline constexpr std::size_t count_even_for_pack_v = count_even_for_pack<Pred, Ts...>::value;
  1505. template <template <typename...> class Pred, typename... Ts>
  1506. using count_odd_for_pack = count_when_for_to_pack<meta_detail::on_odd, sizeof...(Ts), Pred, Ts...>;
  1507. template <template <typename...> class Pred, typename... Ts>
  1508. inline constexpr std::size_t count_odd_for_pack_v = count_odd_for_pack<Pred, Ts...>::value;
  1509. template <typename... Args>
  1510. struct return_type {
  1511. typedef std::tuple<Args...> type;
  1512. };
  1513. template <typename T>
  1514. struct return_type<T> {
  1515. typedef T type;
  1516. };
  1517. template <>
  1518. struct return_type<> {
  1519. typedef void type;
  1520. };
  1521. template <typename... Args>
  1522. using return_type_t = typename return_type<Args...>::type;
  1523. namespace meta_detail {
  1524. template <typename>
  1525. struct always_true : std::true_type {};
  1526. struct is_invokable_tester {
  1527. template <typename Fun, typename... Args>
  1528. static always_true<decltype(std::declval<Fun>()(std::declval<Args>()...))> test(int);
  1529. template <typename...>
  1530. static std::false_type test(...);
  1531. };
  1532. } // namespace meta_detail
  1533. template <typename T>
  1534. struct is_invokable;
  1535. template <typename Fun, typename... Args>
  1536. struct is_invokable<Fun(Args...)> : decltype(meta_detail::is_invokable_tester::test<Fun, Args...>(0)) {};
  1537. namespace meta_detail {
  1538. template <typename T, typename = void>
  1539. struct is_callable : std::is_function<std::remove_pointer_t<T>> {};
  1540. template <typename T>
  1541. struct is_callable<T,
  1542. std::enable_if_t<std::is_final<unqualified_t<T>>::value && std::is_class<unqualified_t<T>>::value
  1543. && std::is_same<decltype(void(&T::operator())), void>::value>> {};
  1544. template <typename T>
  1545. struct is_callable<T,
  1546. std::enable_if_t<!std::is_final<unqualified_t<T>>::value && std::is_class<unqualified_t<T>>::value
  1547. && std::is_destructible<unqualified_t<T>>::value>> {
  1548. struct F {
  1549. void operator()() {};
  1550. };
  1551. struct Derived : T, F {};
  1552. template <typename U, U>
  1553. struct Check;
  1554. template <typename V>
  1555. static sfinae_no_t test(Check<void (F::*)(), &V::operator()>*);
  1556. template <typename>
  1557. static sfinae_yes_t test(...);
  1558. static constexpr bool value = std::is_same_v<decltype(test<Derived>(0)), sfinae_yes_t>;
  1559. };
  1560. template <typename T>
  1561. struct is_callable<T,
  1562. std::enable_if_t<!std::is_final<unqualified_t<T>>::value && std::is_class<unqualified_t<T>>::value
  1563. && !std::is_destructible<unqualified_t<T>>::value>> {
  1564. struct F {
  1565. void operator()() {};
  1566. };
  1567. struct Derived : T, F {
  1568. ~Derived() = delete;
  1569. };
  1570. template <typename U, U>
  1571. struct Check;
  1572. template <typename V>
  1573. static sfinae_no_t test(Check<void (F::*)(), &V::operator()>*);
  1574. template <typename>
  1575. static sfinae_yes_t test(...);
  1576. static constexpr bool value = std::is_same_v<decltype(test<Derived>(0)), sfinae_yes_t>;
  1577. };
  1578. struct has_begin_end_impl {
  1579. template <typename T, typename U = unqualified_t<T>, typename B = decltype(std::declval<U&>().begin()),
  1580. typename E = decltype(std::declval<U&>().end())>
  1581. static std::true_type test(int);
  1582. template <typename...>
  1583. static std::false_type test(...);
  1584. };
  1585. struct has_key_type_impl {
  1586. template <typename T, typename U = unqualified_t<T>, typename V = typename U::key_type>
  1587. static std::true_type test(int);
  1588. template <typename...>
  1589. static std::false_type test(...);
  1590. };
  1591. struct has_key_comp_impl {
  1592. template <typename T, typename V = decltype(std::declval<unqualified_t<T>>().key_comp())>
  1593. static std::true_type test(int);
  1594. template <typename...>
  1595. static std::false_type test(...);
  1596. };
  1597. struct has_load_factor_impl {
  1598. template <typename T, typename V = decltype(std::declval<unqualified_t<T>>().load_factor())>
  1599. static std::true_type test(int);
  1600. template <typename...>
  1601. static std::false_type test(...);
  1602. };
  1603. struct has_mapped_type_impl {
  1604. template <typename T, typename V = typename unqualified_t<T>::mapped_type>
  1605. static std::true_type test(int);
  1606. template <typename...>
  1607. static std::false_type test(...);
  1608. };
  1609. struct has_value_type_impl {
  1610. template <typename T, typename V = typename unqualified_t<T>::value_type>
  1611. static std::true_type test(int);
  1612. template <typename...>
  1613. static std::false_type test(...);
  1614. };
  1615. struct has_iterator_impl {
  1616. template <typename T, typename V = typename unqualified_t<T>::iterator>
  1617. static std::true_type test(int);
  1618. template <typename...>
  1619. static std::false_type test(...);
  1620. };
  1621. struct has_key_value_pair_impl {
  1622. template <typename T, typename U = unqualified_t<T>, typename V = typename U::value_type, typename F = decltype(std::declval<V&>().first),
  1623. typename S = decltype(std::declval<V&>().second)>
  1624. static std::true_type test(int);
  1625. template <typename...>
  1626. static std::false_type test(...);
  1627. };
  1628. template <typename T>
  1629. struct has_push_back_test {
  1630. private:
  1631. template <typename C>
  1632. static sfinae_yes_t test(decltype(std::declval<C>().push_back(std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  1633. template <typename C>
  1634. static sfinae_no_t test(...);
  1635. public:
  1636. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), sfinae_yes_t>;
  1637. };
  1638. template <typename T>
  1639. struct has_insert_test {
  1640. private:
  1641. template <typename C>
  1642. static sfinae_yes_t test(decltype(std::declval<C>().insert(std::declval<std::add_rvalue_reference_t<typename C::const_iterator>>(),
  1643. std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  1644. template <typename C>
  1645. static sfinae_no_t test(...);
  1646. public:
  1647. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), sfinae_yes_t>;
  1648. };
  1649. template <typename T>
  1650. struct has_insert_after_test {
  1651. private:
  1652. template <typename C>
  1653. static sfinae_yes_t test(decltype(std::declval<C>().insert_after(std::declval<std::add_rvalue_reference_t<typename C::const_iterator>>(),
  1654. std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  1655. template <typename C>
  1656. static sfinae_no_t test(...);
  1657. public:
  1658. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), sfinae_yes_t>;
  1659. };
  1660. template <typename T>
  1661. struct has_size_test {
  1662. private:
  1663. template <typename C>
  1664. static sfinae_yes_t test(decltype(std::declval<C>().size())*);
  1665. template <typename C>
  1666. static sfinae_no_t test(...);
  1667. public:
  1668. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), sfinae_yes_t>;
  1669. };
  1670. template <typename T>
  1671. struct has_max_size_test {
  1672. private:
  1673. template <typename C>
  1674. static sfinae_yes_t test(decltype(std::declval<C>().max_size())*);
  1675. template <typename C>
  1676. static sfinae_no_t test(...);
  1677. public:
  1678. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), sfinae_yes_t>;
  1679. };
  1680. template <typename T>
  1681. struct has_to_string_test {
  1682. private:
  1683. template <typename C>
  1684. static sfinae_yes_t test(decltype(std::declval<C>().to_string())*);
  1685. template <typename C>
  1686. static sfinae_no_t test(...);
  1687. public:
  1688. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), sfinae_yes_t>;
  1689. };
  1690. template <typename T, typename U, typename = void>
  1691. class supports_op_less_test : public std::false_type {};
  1692. template <typename T, typename U>
  1693. class supports_op_less_test<T, U, void_t<decltype(std::declval<T&>() < std::declval<U&>())>>
  1694. : public std::integral_constant<bool,
  1695. !is_specialization_of_v<unqualified_t<T>, std::variant> && !is_specialization_of_v<unqualified_t<U>, std::variant>> {};
  1696. template <typename T, typename U, typename = void>
  1697. class supports_op_equal_test : public std::false_type {};
  1698. template <typename T, typename U>
  1699. class supports_op_equal_test<T, U, void_t<decltype(std::declval<T&>() == std::declval<U&>())>>
  1700. : public std::integral_constant<bool,
  1701. !is_specialization_of_v<unqualified_t<T>, std::variant> && !is_specialization_of_v<unqualified_t<U>, std::variant>> {};
  1702. template <typename T, typename U, typename = void>
  1703. class supports_op_less_equal_test : public std::false_type {};
  1704. template <typename T, typename U>
  1705. class supports_op_less_equal_test<T, U, void_t<decltype(std::declval<T&>() <= std::declval<U&>())>>
  1706. : public std::integral_constant<bool,
  1707. !is_specialization_of_v<unqualified_t<T>, std::variant> && !is_specialization_of_v<unqualified_t<U>, std::variant>> {};
  1708. template <typename T, typename U, typename = void>
  1709. class supports_op_left_shift_test : public std::false_type {};
  1710. template <typename T, typename U>
  1711. class supports_op_left_shift_test<T, U, void_t<decltype(std::declval<T&>() << std::declval<U&>())>> : public std::true_type {};
  1712. template <typename T, typename = void>
  1713. class supports_adl_to_string_test : public std::false_type {};
  1714. template <typename T>
  1715. class supports_adl_to_string_test<T, void_t<decltype(to_string(std::declval<const T&>()))>> : public std::true_type {};
  1716. template <typename T, bool b>
  1717. struct is_matched_lookup_impl : std::false_type {};
  1718. template <typename T>
  1719. struct is_matched_lookup_impl<T, true> : std::is_same<typename T::key_type, typename T::value_type> {};
  1720. template <typename T>
  1721. using non_void_t = meta::conditional_t<std::is_void_v<T>, ::sol::detail::unchecked_t, T>;
  1722. } // namespace meta_detail
  1723. template <typename T, typename U = T>
  1724. class supports_op_less : public meta_detail::supports_op_less_test<T, U> {};
  1725. template <typename T, typename U = T>
  1726. class supports_op_equal : public meta_detail::supports_op_equal_test<T, U> {};
  1727. template <typename T, typename U = T>
  1728. class supports_op_less_equal : public meta_detail::supports_op_less_equal_test<T, U> {};
  1729. template <typename T, typename U = T>
  1730. class supports_op_left_shift : public meta_detail::supports_op_left_shift_test<T, U> {};
  1731. template <typename T>
  1732. class supports_adl_to_string : public meta_detail::supports_adl_to_string_test<T> {};
  1733. template <typename T>
  1734. class supports_to_string_member : public meta::boolean<meta_detail::has_to_string_test<meta_detail::non_void_t<T>>::value> {};
  1735. template <typename T>
  1736. using is_callable = boolean<meta_detail::is_callable<T>::value>;
  1737. template <typename T>
  1738. constexpr inline bool is_callable_v = is_callable<T>::value;
  1739. template <typename T>
  1740. struct has_begin_end : decltype(meta_detail::has_begin_end_impl::test<T>(0)) {};
  1741. template <typename T>
  1742. constexpr inline bool has_begin_end_v = has_begin_end<T>::value;
  1743. template <typename T>
  1744. struct has_key_value_pair : decltype(meta_detail::has_key_value_pair_impl::test<T>(0)) {};
  1745. template <typename T>
  1746. struct has_key_type : decltype(meta_detail::has_key_type_impl::test<T>(0)) {};
  1747. template <typename T>
  1748. struct has_key_comp : decltype(meta_detail::has_key_comp_impl::test<T>(0)) {};
  1749. template <typename T>
  1750. struct has_load_factor : decltype(meta_detail::has_load_factor_impl::test<T>(0)) {};
  1751. template <typename T>
  1752. struct has_mapped_type : decltype(meta_detail::has_mapped_type_impl::test<T>(0)) {};
  1753. template <typename T>
  1754. struct has_iterator : decltype(meta_detail::has_iterator_impl::test<T>(0)) {};
  1755. template <typename T>
  1756. struct has_value_type : decltype(meta_detail::has_value_type_impl::test<T>(0)) {};
  1757. template <typename T>
  1758. using has_push_back = meta::boolean<meta_detail::has_push_back_test<T>::value>;
  1759. template <typename T>
  1760. using has_max_size = meta::boolean<meta_detail::has_max_size_test<T>::value>;
  1761. template <typename T>
  1762. using has_insert = meta::boolean<meta_detail::has_insert_test<T>::value>;
  1763. template <typename T>
  1764. using has_insert_after = meta::boolean<meta_detail::has_insert_after_test<T>::value>;
  1765. template <typename T>
  1766. using has_size = meta::boolean<meta_detail::has_size_test<T>::value>;
  1767. template <typename T>
  1768. using is_associative = meta::all<has_key_type<T>, has_key_value_pair<T>, has_mapped_type<T>>;
  1769. template <typename T>
  1770. using is_lookup = meta::all<has_key_type<T>, has_value_type<T>>;
  1771. template <typename T>
  1772. using is_ordered = meta::all<has_key_comp<T>, meta::neg<has_load_factor<T>>>;
  1773. template <typename T>
  1774. using is_matched_lookup = meta_detail::is_matched_lookup_impl<T, is_lookup<T>::value>;
  1775. template <typename T>
  1776. using is_initializer_list = meta::is_specialization_of<T, std::initializer_list>;
  1777. template <typename T>
  1778. constexpr inline bool is_initializer_list_v = is_initializer_list<T>::value;
  1779. template <typename T, typename CharT = char>
  1780. using is_string_literal_array_of = boolean<std::is_array_v<T> && std::is_same_v<std::remove_all_extents_t<T>, CharT>>;
  1781. template <typename T, typename CharT = char>
  1782. constexpr inline bool is_string_literal_array_of_v = is_string_literal_array_of<T, CharT>::value;
  1783. template <typename T>
  1784. using is_string_literal_array = boolean<std::is_array_v<T> && any_same_v<std::remove_all_extents_t<T>, char, char16_t, char32_t, wchar_t>>;
  1785. template <typename T>
  1786. constexpr inline bool is_string_literal_array_v = is_string_literal_array<T>::value;
  1787. template <typename T, typename CharT>
  1788. struct is_string_of : std::false_type {};
  1789. template <typename CharT, typename CharTargetT, typename TraitsT, typename AllocT>
  1790. struct is_string_of<std::basic_string<CharT, TraitsT, AllocT>, CharTargetT> : std::is_same<CharT, CharTargetT> {};
  1791. template <typename T, typename CharT>
  1792. constexpr inline bool is_string_of_v = is_string_of<T, CharT>::value;
  1793. template <typename T, typename CharT>
  1794. struct is_string_view_of : std::false_type {};
  1795. template <typename CharT, typename CharTargetT, typename TraitsT>
  1796. struct is_string_view_of<std::basic_string_view<CharT, TraitsT>, CharTargetT> : std::is_same<CharT, CharTargetT> {};
  1797. template <typename T, typename CharT>
  1798. constexpr inline bool is_string_view_of_v = is_string_view_of<T, CharT>::value;
  1799. template <typename T>
  1800. using is_string_like
  1801. = meta::boolean<is_specialization_of_v<T, std::basic_string> || is_specialization_of_v<T, std::basic_string_view> || is_string_literal_array_v<T>>;
  1802. template <typename T>
  1803. constexpr inline bool is_string_like_v = is_string_like<T>::value;
  1804. template <typename T, typename CharT = char>
  1805. using is_string_constructible = meta::boolean<
  1806. is_string_literal_array_of_v<T,
  1807. CharT> || std::is_same_v<T, const CharT*> || std::is_same_v<T, CharT> || is_string_of_v<T, CharT> || std::is_same_v<T, std::initializer_list<CharT>> || is_string_view_of_v<T, CharT>>;
  1808. template <typename T, typename CharT = char>
  1809. constexpr inline bool is_string_constructible_v = is_string_constructible<T, CharT>::value;
  1810. template <typename T>
  1811. using is_string_like_or_constructible = meta::boolean<is_string_like_v<T> || is_string_constructible_v<T>>;
  1812. template <typename T>
  1813. struct is_pair : std::false_type {};
  1814. template <typename T1, typename T2>
  1815. struct is_pair<std::pair<T1, T2>> : std::true_type {};
  1816. template <typename T, typename Char>
  1817. using is_c_str_of = any<std::is_same<T, const Char*>, std::is_same<T, Char const* const>, std::is_same<T, Char*>, is_string_of<T, Char>,
  1818. is_string_literal_array_of<T, Char>>;
  1819. template <typename T, typename Char>
  1820. constexpr inline bool is_c_str_of_v = is_c_str_of<T, Char>::value;
  1821. template <typename T>
  1822. using is_c_str = is_c_str_of<T, char>;
  1823. template <typename T>
  1824. constexpr inline bool is_c_str_v = is_c_str<T>::value;
  1825. template <typename T>
  1826. struct is_move_only : all<neg<std::is_reference<T>>, neg<std::is_copy_constructible<unqualified_t<T>>>, std::is_move_constructible<unqualified_t<T>>> {};
  1827. template <typename T>
  1828. using is_not_move_only = neg<is_move_only<T>>;
  1829. namespace meta_detail {
  1830. template <typename T>
  1831. decltype(auto) force_tuple(T&& x) {
  1832. if constexpr (meta::is_specialization_of_v<meta::unqualified_t<T>, std::tuple>) {
  1833. return std::forward<T>(x);
  1834. }
  1835. else {
  1836. return std::tuple<T>(std::forward<T>(x));
  1837. }
  1838. }
  1839. } // namespace meta_detail
  1840. template <typename... X>
  1841. decltype(auto) tuplefy(X&&... x) {
  1842. return std::tuple_cat(meta_detail::force_tuple(std::forward<X>(x))...);
  1843. }
  1844. template <typename T, typename = void>
  1845. struct iterator_tag {
  1846. using type = std::input_iterator_tag;
  1847. };
  1848. template <typename T>
  1849. struct iterator_tag<T, conditional_t<false, typename std::iterator_traits<T>::iterator_category, void>> {
  1850. using type = typename std::iterator_traits<T>::iterator_category;
  1851. };
  1852. }} // namespace sol::meta
  1853. // end of sol/traits.hpp
  1854. namespace sol {
  1855. namespace detail {
  1856. const bool default_safe_function_calls =
  1857. #if SOL_IS_ON(SOL_SAFE_FUNCTION_CALLS_I_)
  1858. true;
  1859. #else
  1860. false;
  1861. #endif
  1862. } // namespace detail
  1863. namespace meta { namespace meta_detail {
  1864. }} // namespace meta::meta_detail
  1865. namespace stack { namespace stack_detail {
  1866. using undefined_method_func = void (*)(stack_reference);
  1867. template <typename T>
  1868. void set_undefined_methods_on(stack_reference);
  1869. struct undefined_metatable;
  1870. }} // namespace stack::stack_detail
  1871. } // namespace sol
  1872. #endif // SOL_FORWARD_DETAIL_HPP
  1873. // end of sol/forward_detail.hpp
  1874. // beginning of sol/bytecode.hpp
  1875. // beginning of sol/compatibility.hpp
  1876. // beginning of sol/compatibility/lua_version.hpp
  1877. #if SOL_IS_ON(SOL_USE_CXX_LUA_I_)
  1878. #include <lua.h>
  1879. #include <lualib.h>
  1880. #include <lauxlib.h>
  1881. #elif SOL_IS_ON(SOL_USE_LUA_HPP_I_)
  1882. #include <lua.hpp>
  1883. #else
  1884. extern "C" {
  1885. #include <lua.h>
  1886. #include <lauxlib.h>
  1887. #include <lualib.h>
  1888. }
  1889. #endif // C++ Mangling for Lua vs. Not
  1890. #if defined(SOL_LUAJIT)
  1891. #if (SOL_LUAJIT != 0)
  1892. #define SOL_USE_LUAJIT_I_ SOL_ON
  1893. #else
  1894. #define SOL_USE_LUAJIT_I_ SOL_OFF
  1895. #endif
  1896. #elif defined(LUAJIT_VERSION)
  1897. #define SOL_USE_LUAJIT_I_ SOL_OFF
  1898. #else
  1899. #define SOL_USE_LUAJIT_I_ SOL_DEFAULT_OFF
  1900. #endif // luajit
  1901. #if SOL_IS_ON(SOL_USE_CXX_LUAJIT_I_)
  1902. #include <luajit.h>
  1903. #elif SOL_IS_ON(SOL_USE_LUAJIT_I_)
  1904. extern "C" {
  1905. #include <luajit.h>
  1906. }
  1907. #endif // C++ LuaJIT ... whatever that means
  1908. #if defined(SOL_LUAJIT_VERSION)
  1909. #define SOL_LUAJIT_VERSION_I_ SOL_LUAJIT_VERSION
  1910. #elif SOL_IS_ON(SOL_USE_LUAJIT_I_)
  1911. #define SOL_LUAJIT_VERSION_I_ LUAJIT_VERSION_NUM
  1912. #else
  1913. #define SOL_LUAJIT_VERSION_I_ 0
  1914. #endif
  1915. #if defined(MOONJIT_VERSION)
  1916. #define SOL_USE_MOONJIT_I_ SOL_ON
  1917. #else
  1918. #define SOL_USE_MOONJIT_I_ SOL_OFF
  1919. #endif
  1920. #if !defined(SOL_LUA_VERSION)
  1921. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502
  1922. #define SOL_LUA_VERSION LUA_VERSION_NUM
  1923. #elif defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  1924. #define SOL_LUA_VERSION LUA_VERSION_NUM
  1925. #elif !defined(LUA_VERSION_NUM) || !(LUA_VERSION_NUM)
  1926. // Definitely 5.0
  1927. #define SOL_LUA_VERSION 500
  1928. #else
  1929. // ??? Not sure, assume latest?
  1930. #define SOL_LUA_VERSION 504
  1931. #endif // Lua Version 503, 502, 501 || luajit, 500
  1932. #endif // SOL_LUA_VERSION
  1933. #if defined(SOL_LUA_VERSION)
  1934. #define SOL_LUA_VESION_I_ SOL_LUA_VERSION
  1935. #else
  1936. #define SOL_LUA_VESION_I_ 504
  1937. #endif
  1938. #if defined(SOL_EXCEPTIONS_ALWAYS_UNSAFE)
  1939. #if (SOL_EXCEPTIONS_ALWAYS_UNSAFE != 0)
  1940. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_OFF
  1941. #else
  1942. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_ON
  1943. #endif
  1944. #elif defined(SOL_EXCEPTIONS_SAFE_PROPAGATION)
  1945. #if (SOL_EXCEPTIONS_SAFE_PROPAGATION != 0)
  1946. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_ON
  1947. #else
  1948. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_OFF
  1949. #endif
  1950. #elif SOL_LUAJIT_VERSION_I_ >= 20100
  1951. // LuaJIT 2.1.0-beta3 and better have exception support locked in for all platforms (mostly)
  1952. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_DEFAULT_ON
  1953. #elif SOL_LUAJIT_VERSION_I_ >= 20000
  1954. // LuaJIT 2.0.x have exception support only on x64 builds
  1955. #if SOL_IS_ON(SOL_PLATFORM_X64_I_)
  1956. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_DEFAULT_ON
  1957. #else
  1958. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_OFF
  1959. #endif
  1960. #else
  1961. // otherwise, there is no exception safety for
  1962. // shoving exceptions through Lua and errors should
  1963. // always be serialized
  1964. #define SOL_PROPAGATE_EXCEPTIONS_I_ SOL_DEFAULT_OFF
  1965. #endif // LuaJIT beta 02.01.00 have better exception handling on all platforms since beta3
  1966. #if defined(SOL_LUAJIT_USE_EXCEPTION_TRAMPOLINE)
  1967. #if (SOL_LUAJIT_USE_EXCEPTION_TRAMPOLINE != 0)
  1968. #define SOL_USE_LUAJIT_EXCEPTION_TRAMPOLINE_I_ SOL_ON
  1969. #else
  1970. #define SOL_USE_LUAJIT_EXCEPTION_TRAMPOLINE_I_ SOL_OFF
  1971. #endif
  1972. #else
  1973. #if SOL_IS_OFF(SOL_PROPAGATE_EXCEPTIONS_I_) && SOL_IS_ON(SOL_USE_LUAJIT_I_)
  1974. #define SOL_USE_LUAJIT_EXCEPTION_TRAMPOLINE_I_ SOL_ON
  1975. #else
  1976. #define SOL_USE_LUAJIT_EXCEPTION_TRAMPOLINE_I_ SOL_DEFAULT_OFF
  1977. #endif
  1978. #endif
  1979. #if defined(SOL_LUAL_STREAM_HAS_CLOSE_FUNCTION)
  1980. #if (SOL_LUAL_STREAM_HAS_CLOSE_FUNCTION != 0)
  1981. #define SOL_LUAL_STREAM_USE_CLOSE_FUNCTION_I_ SOL_ON
  1982. #else
  1983. #define SOL_LUAL_STREAM_USE_CLOSE_FUNCTION_I_ SOL_OFF
  1984. #endif
  1985. #else
  1986. #if SOL_IS_OFF(SOL_USE_LUAJIT_I_) && (SOL_LUA_VERSION > 501)
  1987. #define SOL_LUAL_STREAM_USE_CLOSE_FUNCTION_I_ SOL_ON
  1988. #else
  1989. #define SOL_LUAL_STREAM_USE_CLOSE_FUNCTION_I_ SOL_DEFAULT_OFF
  1990. #endif
  1991. #endif
  1992. // end of sol/compatibility/lua_version.hpp
  1993. #if SOL_IS_ON(SOL_USE_COMPATIBILITY_LAYER_I_)
  1994. #if SOL_IS_ON(SOL_USE_CXX_LUA_I_) || SOL_IS_ON(SOL_USE_CXX_LUAJIT_I_)
  1995. #ifndef COMPAT53_LUA_CPP
  1996. #define COMPAT53_LUA_CPP 1
  1997. #endif // Build Lua Compat layer as C++
  1998. #endif
  1999. #ifndef COMPAT53_INCLUDE_SOURCE
  2000. #define COMPAT53_INCLUDE_SOURCE 1
  2001. #endif // Build Compat Layer Inline
  2002. // beginning of sol/compatibility/compat-5.3.h
  2003. #ifndef KEPLER_PROJECT_COMPAT53_H_
  2004. #define KEPLER_PROJECT_COMPAT53_H_
  2005. #include <stddef.h>
  2006. #include <limits.h>
  2007. #include <string.h>
  2008. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  2009. extern "C" {
  2010. #endif
  2011. #include <lua.h>
  2012. #include <lauxlib.h>
  2013. #include <lualib.h>
  2014. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  2015. }
  2016. #endif
  2017. #ifndef COMPAT53_PREFIX
  2018. /* we chose this name because many other lua bindings / libs have
  2019. * their own compatibility layer, and that use the compat53 declaration
  2020. * frequently, causing all kinds of linker / compiler issues
  2021. */
  2022. # define COMPAT53_PREFIX kp_compat53
  2023. #endif // COMPAT53_PREFIX
  2024. #ifndef COMPAT53_API
  2025. # if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE
  2026. # if defined(__GNUC__) || defined(__clang__)
  2027. # define COMPAT53_API __attribute__((__unused__)) static inline
  2028. # else
  2029. # define COMPAT53_API static inline
  2030. # endif /* Clang/GCC */
  2031. # else /* COMPAT53_INCLUDE_SOURCE */
  2032. /* we are not including source, so everything is extern */
  2033. # define COMPAT53_API extern
  2034. # endif /* COMPAT53_INCLUDE_SOURCE */
  2035. #endif /* COMPAT53_PREFIX */
  2036. #define COMPAT53_CONCAT_HELPER(a, b) a##b
  2037. #define COMPAT53_CONCAT(a, b) COMPAT53_CONCAT_HELPER(a, b)
  2038. /* declarations for Lua 5.1 */
  2039. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  2040. /* XXX not implemented:
  2041. * lua_arith (new operators)
  2042. * lua_upvalueid
  2043. * lua_upvaluejoin
  2044. * lua_version
  2045. * lua_yieldk
  2046. */
  2047. #ifndef LUA_OK
  2048. # define LUA_OK 0
  2049. #endif
  2050. #ifndef LUA_OPADD
  2051. # define LUA_OPADD 0
  2052. #endif
  2053. #ifndef LUA_OPSUB
  2054. # define LUA_OPSUB 1
  2055. #endif
  2056. #ifndef LUA_OPMUL
  2057. # define LUA_OPMUL 2
  2058. #endif
  2059. #ifndef LUA_OPDIV
  2060. # define LUA_OPDIV 3
  2061. #endif
  2062. #ifndef LUA_OPMOD
  2063. # define LUA_OPMOD 4
  2064. #endif
  2065. #ifndef LUA_OPPOW
  2066. # define LUA_OPPOW 5
  2067. #endif
  2068. #ifndef LUA_OPUNM
  2069. # define LUA_OPUNM 6
  2070. #endif
  2071. #ifndef LUA_OPEQ
  2072. # define LUA_OPEQ 0
  2073. #endif
  2074. #ifndef LUA_OPLT
  2075. # define LUA_OPLT 1
  2076. #endif
  2077. #ifndef LUA_OPLE
  2078. # define LUA_OPLE 2
  2079. #endif
  2080. /* LuaJIT/Lua 5.1 does not have the updated
  2081. * error codes for thread status/function returns (but some patched versions do)
  2082. * define it only if it's not found
  2083. */
  2084. #if !defined(LUA_ERRGCMM)
  2085. /* Use + 2 because in some versions of Lua (Lua 5.1)
  2086. * LUA_ERRFILE is defined as (LUA_ERRERR+1)
  2087. * so we need to avoid it (LuaJIT might have something at this
  2088. * integer value too)
  2089. */
  2090. # define LUA_ERRGCMM (LUA_ERRERR + 2)
  2091. #endif /* LUA_ERRGCMM define */
  2092. #if !defined(MOONJIT_VERSION)
  2093. typedef size_t lua_Unsigned;
  2094. #endif
  2095. typedef struct luaL_Buffer_53 {
  2096. luaL_Buffer b; /* make incorrect code crash! */
  2097. char *ptr;
  2098. size_t nelems;
  2099. size_t capacity;
  2100. lua_State *L2;
  2101. } luaL_Buffer_53;
  2102. #define luaL_Buffer luaL_Buffer_53
  2103. /* In PUC-Rio 5.1, userdata is a simple FILE*
  2104. * In LuaJIT, it's a struct where the first member is a FILE*
  2105. * We can't support the `closef` member
  2106. */
  2107. typedef struct luaL_Stream {
  2108. FILE *f;
  2109. } luaL_Stream;
  2110. #define lua_absindex COMPAT53_CONCAT(COMPAT53_PREFIX, _absindex)
  2111. COMPAT53_API int lua_absindex(lua_State *L, int i);
  2112. #define lua_arith COMPAT53_CONCAT(COMPAT53_PREFIX, _arith)
  2113. COMPAT53_API void lua_arith(lua_State *L, int op);
  2114. #define lua_compare COMPAT53_CONCAT(COMPAT53_PREFIX, _compare)
  2115. COMPAT53_API int lua_compare(lua_State *L, int idx1, int idx2, int op);
  2116. #define lua_copy COMPAT53_CONCAT(COMPAT53_PREFIX, _copy)
  2117. COMPAT53_API void lua_copy(lua_State *L, int from, int to);
  2118. #define lua_getuservalue(L, i) \
  2119. (lua_getfenv((L), (i)), lua_type((L), -1))
  2120. #define lua_setuservalue(L, i) \
  2121. (luaL_checktype((L), -1, LUA_TTABLE), lua_setfenv((L), (i)))
  2122. #define lua_len COMPAT53_CONCAT(COMPAT53_PREFIX, _len)
  2123. COMPAT53_API void lua_len(lua_State *L, int i);
  2124. #define lua_pushstring(L, s) \
  2125. (lua_pushstring((L), (s)), lua_tostring((L), -1))
  2126. #define lua_pushlstring(L, s, len) \
  2127. ((((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len))), lua_tostring((L), -1))
  2128. #ifndef luaL_newlibtable
  2129. # define luaL_newlibtable(L, l) \
  2130. (lua_createtable((L), 0, sizeof((l))/sizeof(*(l))-1))
  2131. #endif
  2132. #ifndef luaL_newlib
  2133. # define luaL_newlib(L, l) \
  2134. (luaL_newlibtable((L), (l)), luaL_register((L), NULL, (l)))
  2135. #endif
  2136. #ifndef lua_pushglobaltable
  2137. # define lua_pushglobaltable(L) \
  2138. lua_pushvalue((L), LUA_GLOBALSINDEX)
  2139. #endif
  2140. #define lua_rawgetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawgetp)
  2141. COMPAT53_API int lua_rawgetp(lua_State *L, int i, const void *p);
  2142. #define lua_rawsetp COMPAT53_CONCAT(COMPAT53_PREFIX, _rawsetp)
  2143. COMPAT53_API void lua_rawsetp(lua_State *L, int i, const void *p);
  2144. #define lua_rawlen(L, i) lua_objlen((L), (i))
  2145. #define lua_tointeger(L, i) lua_tointegerx((L), (i), NULL)
  2146. #define lua_tonumberx COMPAT53_CONCAT(COMPAT53_PREFIX, _tonumberx)
  2147. COMPAT53_API lua_Number lua_tonumberx(lua_State *L, int i, int *isnum);
  2148. #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion)
  2149. COMPAT53_API void luaL_checkversion(lua_State *L);
  2150. #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
  2151. COMPAT53_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode);
  2152. #define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex)
  2153. COMPAT53_API int luaL_loadfilex(lua_State *L, const char *filename, const char *mode);
  2154. #define luaL_loadbufferx COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadbufferx)
  2155. COMPAT53_API int luaL_loadbufferx(lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
  2156. #define luaL_checkstack COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkstack_53)
  2157. COMPAT53_API void luaL_checkstack(lua_State *L, int sp, const char *msg);
  2158. #define luaL_getsubtable COMPAT53_CONCAT(COMPAT53_PREFIX, L_getsubtable)
  2159. COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char *name);
  2160. #define luaL_len COMPAT53_CONCAT(COMPAT53_PREFIX, L_len)
  2161. COMPAT53_API lua_Integer luaL_len(lua_State *L, int i);
  2162. #define luaL_setfuncs COMPAT53_CONCAT(COMPAT53_PREFIX, L_setfuncs)
  2163. COMPAT53_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup);
  2164. #define luaL_setmetatable COMPAT53_CONCAT(COMPAT53_PREFIX, L_setmetatable)
  2165. COMPAT53_API void luaL_setmetatable(lua_State *L, const char *tname);
  2166. #define luaL_testudata COMPAT53_CONCAT(COMPAT53_PREFIX, L_testudata)
  2167. COMPAT53_API void *luaL_testudata(lua_State *L, int i, const char *tname);
  2168. #define luaL_traceback COMPAT53_CONCAT(COMPAT53_PREFIX, L_traceback)
  2169. COMPAT53_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level);
  2170. #define luaL_fileresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_fileresult)
  2171. COMPAT53_API int luaL_fileresult(lua_State *L, int stat, const char *fname);
  2172. #define luaL_execresult COMPAT53_CONCAT(COMPAT53_PREFIX, L_execresult)
  2173. COMPAT53_API int luaL_execresult(lua_State *L, int stat);
  2174. #define lua_callk(L, na, nr, ctx, cont) \
  2175. ((void)(ctx), (void)(cont), lua_call((L), (na), (nr)))
  2176. #define lua_pcallk(L, na, nr, err, ctx, cont) \
  2177. ((void)(ctx), (void)(cont), lua_pcall((L), (na), (nr), (err)))
  2178. #define lua_resume(L, from, nargs) \
  2179. ((void)(from), lua_resume((L), (nargs)))
  2180. #define luaL_buffinit COMPAT53_CONCAT(COMPAT53_PREFIX, _buffinit_53)
  2181. COMPAT53_API void luaL_buffinit(lua_State *L, luaL_Buffer_53 *B);
  2182. #define luaL_prepbuffsize COMPAT53_CONCAT(COMPAT53_PREFIX, _prepbufsize_53)
  2183. COMPAT53_API char *luaL_prepbuffsize(luaL_Buffer_53 *B, size_t s);
  2184. #define luaL_addlstring COMPAT53_CONCAT(COMPAT53_PREFIX, _addlstring_53)
  2185. COMPAT53_API void luaL_addlstring(luaL_Buffer_53 *B, const char *s, size_t l);
  2186. #define luaL_addvalue COMPAT53_CONCAT(COMPAT53_PREFIX, _addvalue_53)
  2187. COMPAT53_API void luaL_addvalue(luaL_Buffer_53 *B);
  2188. #define luaL_pushresult COMPAT53_CONCAT(COMPAT53_PREFIX, _pushresult_53)
  2189. COMPAT53_API void luaL_pushresult(luaL_Buffer_53 *B);
  2190. #undef luaL_buffinitsize
  2191. #define luaL_buffinitsize(L, B, s) \
  2192. (luaL_buffinit((L), (B)), luaL_prepbuffsize((B), (s)))
  2193. #undef luaL_prepbuffer
  2194. #define luaL_prepbuffer(B) \
  2195. luaL_prepbuffsize((B), LUAL_BUFFERSIZE)
  2196. #undef luaL_addchar
  2197. #define luaL_addchar(B, c) \
  2198. ((void)((B)->nelems < (B)->capacity || luaL_prepbuffsize((B), 1)), \
  2199. ((B)->ptr[(B)->nelems++] = (c)))
  2200. #undef luaL_addsize
  2201. #define luaL_addsize(B, s) \
  2202. ((B)->nelems += (s))
  2203. #undef luaL_addstring
  2204. #define luaL_addstring(B, s) \
  2205. luaL_addlstring((B), (s), strlen((s)))
  2206. #undef luaL_pushresultsize
  2207. #define luaL_pushresultsize(B, s) \
  2208. (luaL_addsize((B), (s)), luaL_pushresult((B)))
  2209. #if defined(LUA_COMPAT_APIINTCASTS)
  2210. #define lua_pushunsigned(L, n) \
  2211. lua_pushinteger((L), (lua_Integer)(n))
  2212. #define lua_tounsignedx(L, i, is) \
  2213. ((lua_Unsigned)lua_tointegerx((L), (i), (is)))
  2214. #define lua_tounsigned(L, i) \
  2215. lua_tounsignedx((L), (i), NULL)
  2216. #define luaL_checkunsigned(L, a) \
  2217. ((lua_Unsigned)luaL_checkinteger((L), (a)))
  2218. #define luaL_optunsigned(L, a, d) \
  2219. ((lua_Unsigned)luaL_optinteger((L), (a), (lua_Integer)(d)))
  2220. #endif
  2221. #endif /* Lua 5.1 only */
  2222. /* declarations for Lua 5.1 and 5.2 */
  2223. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  2224. typedef int lua_KContext;
  2225. typedef int(*lua_KFunction)(lua_State *L, int status, lua_KContext ctx);
  2226. #define lua_dump(L, w, d, s) \
  2227. ((void)(s), lua_dump((L), (w), (d)))
  2228. #define lua_getfield(L, i, k) \
  2229. (lua_getfield((L), (i), (k)), lua_type((L), -1))
  2230. #define lua_gettable(L, i) \
  2231. (lua_gettable((L), (i)), lua_type((L), -1))
  2232. #define lua_geti COMPAT53_CONCAT(COMPAT53_PREFIX, _geti)
  2233. COMPAT53_API int lua_geti(lua_State *L, int index, lua_Integer i);
  2234. #define lua_isinteger COMPAT53_CONCAT(COMPAT53_PREFIX, _isinteger)
  2235. COMPAT53_API int lua_isinteger(lua_State *L, int index);
  2236. #define lua_tointegerx COMPAT53_CONCAT(COMPAT53_PREFIX, _tointegerx_53)
  2237. COMPAT53_API lua_Integer lua_tointegerx(lua_State *L, int i, int *isnum);
  2238. #define lua_numbertointeger(n, p) \
  2239. ((*(p) = (lua_Integer)(n)), 1)
  2240. #define lua_rawget(L, i) \
  2241. (lua_rawget((L), (i)), lua_type((L), -1))
  2242. #define lua_rawgeti(L, i, n) \
  2243. (lua_rawgeti((L), (i), (n)), lua_type((L), -1))
  2244. #define lua_rotate COMPAT53_CONCAT(COMPAT53_PREFIX, _rotate)
  2245. COMPAT53_API void lua_rotate(lua_State *L, int idx, int n);
  2246. #define lua_seti COMPAT53_CONCAT(COMPAT53_PREFIX, _seti)
  2247. COMPAT53_API void lua_seti(lua_State *L, int index, lua_Integer i);
  2248. #define lua_stringtonumber COMPAT53_CONCAT(COMPAT53_PREFIX, _stringtonumber)
  2249. COMPAT53_API size_t lua_stringtonumber(lua_State *L, const char *s);
  2250. #define luaL_tolstring COMPAT53_CONCAT(COMPAT53_PREFIX, L_tolstring)
  2251. COMPAT53_API const char *luaL_tolstring(lua_State *L, int idx, size_t *len);
  2252. #define luaL_getmetafield(L, o, e) \
  2253. (luaL_getmetafield((L), (o), (e)) ? lua_type((L), -1) : LUA_TNIL)
  2254. #define luaL_newmetatable(L, tn) \
  2255. (luaL_newmetatable((L), (tn)) ? (lua_pushstring((L), (tn)), lua_setfield((L), -2, "__name"), 1) : 0)
  2256. #define luaL_requiref COMPAT53_CONCAT(COMPAT53_PREFIX, L_requiref_53)
  2257. COMPAT53_API void luaL_requiref(lua_State *L, const char *modname,
  2258. lua_CFunction openf, int glb);
  2259. #endif /* Lua 5.1 and Lua 5.2 */
  2260. /* declarations for Lua 5.2 */
  2261. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 502
  2262. /* XXX not implemented:
  2263. * lua_isyieldable
  2264. * lua_getextraspace
  2265. * lua_arith (new operators)
  2266. * lua_pushfstring (new formats)
  2267. */
  2268. #define lua_getglobal(L, n) \
  2269. (lua_getglobal((L), (n)), lua_type((L), -1))
  2270. #define lua_getuservalue(L, i) \
  2271. (lua_getuservalue((L), (i)), lua_type((L), -1))
  2272. #define lua_pushlstring(L, s, len) \
  2273. (((len) == 0) ? lua_pushlstring((L), "", 0) : lua_pushlstring((L), (s), (len)))
  2274. #define lua_rawgetp(L, i, p) \
  2275. (lua_rawgetp((L), (i), (p)), lua_type((L), -1))
  2276. #define LUA_KFUNCTION(_name) \
  2277. static int (_name)(lua_State *L, int status, lua_KContext ctx); \
  2278. static int (_name ## _52)(lua_State *L) { \
  2279. lua_KContext ctx; \
  2280. int status = lua_getctx(L, &ctx); \
  2281. return (_name)(L, status, ctx); \
  2282. } \
  2283. static int (_name)(lua_State *L, int status, lua_KContext ctx)
  2284. #define lua_pcallk(L, na, nr, err, ctx, cont) \
  2285. lua_pcallk((L), (na), (nr), (err), (ctx), cont ## _52)
  2286. #define lua_callk(L, na, nr, ctx, cont) \
  2287. lua_callk((L), (na), (nr), (ctx), cont ## _52)
  2288. #define lua_yieldk(L, nr, ctx, cont) \
  2289. lua_yieldk((L), (nr), (ctx), cont ## _52)
  2290. #ifdef lua_call
  2291. # undef lua_call
  2292. # define lua_call(L, na, nr) \
  2293. (lua_callk)((L), (na), (nr), 0, NULL)
  2294. #endif
  2295. #ifdef lua_pcall
  2296. # undef lua_pcall
  2297. # define lua_pcall(L, na, nr, err) \
  2298. (lua_pcallk)((L), (na), (nr), (err), 0, NULL)
  2299. #endif
  2300. #ifdef lua_yield
  2301. # undef lua_yield
  2302. # define lua_yield(L, nr) \
  2303. (lua_yieldk)((L), (nr), 0, NULL)
  2304. #endif
  2305. #endif /* Lua 5.2 only */
  2306. /* other Lua versions */
  2307. #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 504
  2308. # error "unsupported Lua version (i.e. not Lua 5.1, 5.2, 5.3, or 5.4)"
  2309. #endif /* other Lua versions except 5.1, 5.2, 5.3, and 5.4 */
  2310. /* helper macro for defining continuation functions (for every version
  2311. * *except* Lua 5.2) */
  2312. #ifndef LUA_KFUNCTION
  2313. #define LUA_KFUNCTION(_name) \
  2314. static int (_name)(lua_State *L, int status, lua_KContext ctx)
  2315. #endif
  2316. #if defined(COMPAT53_INCLUDE_SOURCE) && COMPAT53_INCLUDE_SOURCE == 1
  2317. // beginning of sol/compatibility/compat-5.3.c.h
  2318. #include <stddef.h>
  2319. #include <stdlib.h>
  2320. #include <string.h>
  2321. #include <ctype.h>
  2322. #include <errno.h>
  2323. #include <stdio.h>
  2324. /* don't compile it again if it already is included via compat53.h */
  2325. #ifndef KEPLER_PROJECT_COMPAT53_C_
  2326. #define KEPLER_PROJECT_COMPAT53_C_
  2327. /* definitions for Lua 5.1 only */
  2328. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
  2329. #ifndef COMPAT53_FOPEN_NO_LOCK
  2330. #if defined(_MSC_VER)
  2331. #define COMPAT53_FOPEN_NO_LOCK 1
  2332. #else /* otherwise */
  2333. #define COMPAT53_FOPEN_NO_LOCK 0
  2334. #endif /* VC++ only so far */
  2335. #endif /* No-lock fopen_s usage if possible */
  2336. #if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
  2337. #include <share.h>
  2338. #endif /* VC++ _fsopen for share-allowed file read */
  2339. #ifndef COMPAT53_HAVE_STRERROR_R
  2340. #if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined(__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6))
  2341. #define COMPAT53_HAVE_STRERROR_R 1
  2342. #else /* none of the defines matched: define to 0 */
  2343. #define COMPAT53_HAVE_STRERROR_R 0
  2344. #endif /* have strerror_r of some form */
  2345. #endif /* strerror_r */
  2346. #ifndef COMPAT53_HAVE_STRERROR_S
  2347. #if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__)
  2348. #define COMPAT53_HAVE_STRERROR_S 1
  2349. #else /* not VC++ or C11 */
  2350. #define COMPAT53_HAVE_STRERROR_S 0
  2351. #endif /* strerror_s from VC++ or C11 */
  2352. #endif /* strerror_s */
  2353. #ifndef COMPAT53_LUA_FILE_BUFFER_SIZE
  2354. #define COMPAT53_LUA_FILE_BUFFER_SIZE 4096
  2355. #endif /* Lua File Buffer Size */
  2356. static char* compat53_strerror(int en, char* buff, size_t sz) {
  2357. #if COMPAT53_HAVE_STRERROR_R
  2358. /* use strerror_r here, because it's available on these specific platforms */
  2359. if (sz > 0) {
  2360. buff[0] = '\0';
  2361. /* we don't care whether the GNU version or the XSI version is used: */
  2362. if (strerror_r(en, buff, sz)) {
  2363. /* Yes, we really DO want to ignore the return value!
  2364. * GCC makes that extra hard, not even a (void) cast will do. */
  2365. }
  2366. if (buff[0] == '\0') {
  2367. /* Buffer is unchanged, so we probably have called GNU strerror_r which
  2368. * returned a static constant string. Chances are that strerror will
  2369. * return the same static constant string and therefore be thread-safe. */
  2370. return strerror(en);
  2371. }
  2372. }
  2373. return buff; /* sz is 0 *or* strerror_r wrote into the buffer */
  2374. #elif COMPAT53_HAVE_STRERROR_S
  2375. /* for MSVC and other C11 implementations, use strerror_s since it's
  2376. * provided by default by the libraries */
  2377. strerror_s(buff, sz, en);
  2378. return buff;
  2379. #else
  2380. /* fallback, but strerror is not guaranteed to be threadsafe due to modifying
  2381. * errno itself and some impls not locking a static buffer for it ... but most
  2382. * known systems have threadsafe errno: this might only change if the locale
  2383. * is changed out from under someone while this function is being called */
  2384. (void)buff;
  2385. (void)sz;
  2386. return strerror(en);
  2387. #endif
  2388. }
  2389. COMPAT53_API int lua_absindex(lua_State* L, int i) {
  2390. if (i < 0 && i > LUA_REGISTRYINDEX)
  2391. i += lua_gettop(L) + 1;
  2392. return i;
  2393. }
  2394. static void compat53_call_lua(lua_State* L, char const code[], size_t len, int nargs, int nret) {
  2395. lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)code);
  2396. if (lua_type(L, -1) != LUA_TFUNCTION) {
  2397. lua_pop(L, 1);
  2398. if (luaL_loadbuffer(L, code, len, "=none"))
  2399. lua_error(L);
  2400. lua_pushvalue(L, -1);
  2401. lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)code);
  2402. }
  2403. lua_insert(L, -nargs - 1);
  2404. lua_call(L, nargs, nret);
  2405. }
  2406. static const char compat53_arith_code[]
  2407. = "local op,a,b=...\n"
  2408. "if op==0 then return a+b\n"
  2409. "elseif op==1 then return a-b\n"
  2410. "elseif op==2 then return a*b\n"
  2411. "elseif op==3 then return a/b\n"
  2412. "elseif op==4 then return a%b\n"
  2413. "elseif op==5 then return a^b\n"
  2414. "elseif op==6 then return -a\n"
  2415. "end\n";
  2416. COMPAT53_API void lua_arith(lua_State* L, int op) {
  2417. if (op < LUA_OPADD || op > LUA_OPUNM)
  2418. luaL_error(L, "invalid 'op' argument for lua_arith");
  2419. luaL_checkstack(L, 5, "not enough stack slots");
  2420. if (op == LUA_OPUNM)
  2421. lua_pushvalue(L, -1);
  2422. lua_pushnumber(L, op);
  2423. lua_insert(L, -3);
  2424. compat53_call_lua(L, compat53_arith_code, sizeof(compat53_arith_code) - 1, 3, 1);
  2425. }
  2426. static const char compat53_compare_code[]
  2427. = "local a,b=...\n"
  2428. "return a<=b\n";
  2429. COMPAT53_API int lua_compare(lua_State* L, int idx1, int idx2, int op) {
  2430. int result = 0;
  2431. switch (op) {
  2432. case LUA_OPEQ:
  2433. return lua_equal(L, idx1, idx2);
  2434. case LUA_OPLT:
  2435. return lua_lessthan(L, idx1, idx2);
  2436. case LUA_OPLE:
  2437. luaL_checkstack(L, 5, "not enough stack slots");
  2438. idx1 = lua_absindex(L, idx1);
  2439. idx2 = lua_absindex(L, idx2);
  2440. lua_pushvalue(L, idx1);
  2441. lua_pushvalue(L, idx2);
  2442. compat53_call_lua(L, compat53_compare_code, sizeof(compat53_compare_code) - 1, 2, 1);
  2443. result = lua_toboolean(L, -1);
  2444. lua_pop(L, 1);
  2445. return result;
  2446. default:
  2447. luaL_error(L, "invalid 'op' argument for lua_compare");
  2448. }
  2449. return 0;
  2450. }
  2451. COMPAT53_API void lua_copy(lua_State* L, int from, int to) {
  2452. int abs_to = lua_absindex(L, to);
  2453. luaL_checkstack(L, 1, "not enough stack slots");
  2454. lua_pushvalue(L, from);
  2455. lua_replace(L, abs_to);
  2456. }
  2457. COMPAT53_API void lua_len(lua_State* L, int i) {
  2458. switch (lua_type(L, i)) {
  2459. case LUA_TSTRING:
  2460. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  2461. break;
  2462. case LUA_TTABLE:
  2463. if (!luaL_callmeta(L, i, "__len"))
  2464. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  2465. break;
  2466. case LUA_TUSERDATA:
  2467. if (luaL_callmeta(L, i, "__len"))
  2468. break;
  2469. /* FALLTHROUGH */
  2470. default:
  2471. luaL_error(L, "attempt to get length of a %s value", lua_typename(L, lua_type(L, i)));
  2472. }
  2473. }
  2474. COMPAT53_API int lua_rawgetp(lua_State* L, int i, const void* p) {
  2475. int abs_i = lua_absindex(L, i);
  2476. lua_pushlightuserdata(L, (void*)p);
  2477. lua_rawget(L, abs_i);
  2478. return lua_type(L, -1);
  2479. }
  2480. COMPAT53_API void lua_rawsetp(lua_State* L, int i, const void* p) {
  2481. int abs_i = lua_absindex(L, i);
  2482. luaL_checkstack(L, 1, "not enough stack slots");
  2483. lua_pushlightuserdata(L, (void*)p);
  2484. lua_insert(L, -2);
  2485. lua_rawset(L, abs_i);
  2486. }
  2487. COMPAT53_API lua_Number lua_tonumberx(lua_State* L, int i, int* isnum) {
  2488. lua_Number n = lua_tonumber(L, i);
  2489. if (isnum != NULL) {
  2490. *isnum = (n != 0 || lua_isnumber(L, i));
  2491. }
  2492. return n;
  2493. }
  2494. COMPAT53_API void luaL_checkversion(lua_State* L) {
  2495. (void)L;
  2496. }
  2497. COMPAT53_API void luaL_checkstack(lua_State* L, int sp, const char* msg) {
  2498. if (!lua_checkstack(L, sp + LUA_MINSTACK)) {
  2499. if (msg != NULL)
  2500. luaL_error(L, "stack overflow (%s)", msg);
  2501. else {
  2502. lua_pushliteral(L, "stack overflow");
  2503. lua_error(L);
  2504. }
  2505. }
  2506. }
  2507. COMPAT53_API int luaL_getsubtable(lua_State* L, int i, const char* name) {
  2508. int abs_i = lua_absindex(L, i);
  2509. luaL_checkstack(L, 3, "not enough stack slots");
  2510. lua_pushstring(L, name);
  2511. lua_gettable(L, abs_i);
  2512. if (lua_istable(L, -1))
  2513. return 1;
  2514. lua_pop(L, 1);
  2515. lua_newtable(L);
  2516. lua_pushstring(L, name);
  2517. lua_pushvalue(L, -2);
  2518. lua_settable(L, abs_i);
  2519. return 0;
  2520. }
  2521. COMPAT53_API lua_Integer luaL_len(lua_State* L, int i) {
  2522. lua_Integer res = 0;
  2523. int isnum = 0;
  2524. luaL_checkstack(L, 1, "not enough stack slots");
  2525. lua_len(L, i);
  2526. res = lua_tointegerx(L, -1, &isnum);
  2527. lua_pop(L, 1);
  2528. if (!isnum)
  2529. luaL_error(L, "object length is not an integer");
  2530. return res;
  2531. }
  2532. COMPAT53_API void luaL_setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
  2533. luaL_checkstack(L, nup + 1, "too many upvalues");
  2534. for (; l->name != NULL; l++) { /* fill the table with given functions */
  2535. int i;
  2536. lua_pushstring(L, l->name);
  2537. for (i = 0; i < nup; i++) /* copy upvalues to the top */
  2538. lua_pushvalue(L, -(nup + 1));
  2539. lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
  2540. lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
  2541. }
  2542. lua_pop(L, nup); /* remove upvalues */
  2543. }
  2544. COMPAT53_API void luaL_setmetatable(lua_State* L, const char* tname) {
  2545. luaL_checkstack(L, 1, "not enough stack slots");
  2546. luaL_getmetatable(L, tname);
  2547. lua_setmetatable(L, -2);
  2548. }
  2549. COMPAT53_API void* luaL_testudata(lua_State* L, int i, const char* tname) {
  2550. void* p = lua_touserdata(L, i);
  2551. luaL_checkstack(L, 2, "not enough stack slots");
  2552. if (p == NULL || !lua_getmetatable(L, i))
  2553. return NULL;
  2554. else {
  2555. int res = 0;
  2556. luaL_getmetatable(L, tname);
  2557. res = lua_rawequal(L, -1, -2);
  2558. lua_pop(L, 2);
  2559. if (!res)
  2560. p = NULL;
  2561. }
  2562. return p;
  2563. }
  2564. static int compat53_countlevels(lua_State* L) {
  2565. lua_Debug ar;
  2566. int li = 1, le = 1;
  2567. /* find an upper bound */
  2568. while (lua_getstack(L, le, &ar)) {
  2569. li = le;
  2570. le *= 2;
  2571. }
  2572. /* do a binary search */
  2573. while (li < le) {
  2574. int m = (li + le) / 2;
  2575. if (lua_getstack(L, m, &ar))
  2576. li = m + 1;
  2577. else
  2578. le = m;
  2579. }
  2580. return le - 1;
  2581. }
  2582. static int compat53_findfield(lua_State* L, int objidx, int level) {
  2583. if (level == 0 || !lua_istable(L, -1))
  2584. return 0; /* not found */
  2585. lua_pushnil(L); /* start 'next' loop */
  2586. while (lua_next(L, -2)) { /* for each pair in table */
  2587. if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
  2588. if (lua_rawequal(L, objidx, -1)) { /* found object? */
  2589. lua_pop(L, 1); /* remove value (but keep name) */
  2590. return 1;
  2591. }
  2592. else if (compat53_findfield(L, objidx, level - 1)) { /* try recursively */
  2593. lua_remove(L, -2); /* remove table (but keep name) */
  2594. lua_pushliteral(L, ".");
  2595. lua_insert(L, -2); /* place '.' between the two names */
  2596. lua_concat(L, 3);
  2597. return 1;
  2598. }
  2599. }
  2600. lua_pop(L, 1); /* remove value */
  2601. }
  2602. return 0; /* not found */
  2603. }
  2604. static int compat53_pushglobalfuncname(lua_State* L, lua_Debug* ar) {
  2605. int top = lua_gettop(L);
  2606. lua_getinfo(L, "f", ar); /* push function */
  2607. lua_pushvalue(L, LUA_GLOBALSINDEX);
  2608. if (compat53_findfield(L, top + 1, 2)) {
  2609. lua_copy(L, -1, top + 1); /* move name to proper place */
  2610. lua_pop(L, 2); /* remove pushed values */
  2611. return 1;
  2612. }
  2613. else {
  2614. lua_settop(L, top); /* remove function and global table */
  2615. return 0;
  2616. }
  2617. }
  2618. static void compat53_pushfuncname(lua_State* L, lua_Debug* ar) {
  2619. if (*ar->namewhat != '\0') /* is there a name? */
  2620. lua_pushfstring(L, "function " LUA_QS, ar->name);
  2621. else if (*ar->what == 'm') /* main? */
  2622. lua_pushliteral(L, "main chunk");
  2623. else if (*ar->what == 'C') {
  2624. if (compat53_pushglobalfuncname(L, ar)) {
  2625. lua_pushfstring(L, "function " LUA_QS, lua_tostring(L, -1));
  2626. lua_remove(L, -2); /* remove name */
  2627. }
  2628. else
  2629. lua_pushliteral(L, "?");
  2630. }
  2631. else
  2632. lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined);
  2633. }
  2634. #define COMPAT53_LEVELS1 12 /* size of the first part of the stack */
  2635. #define COMPAT53_LEVELS2 10 /* size of the second part of the stack */
  2636. COMPAT53_API void luaL_traceback(lua_State* L, lua_State* L1, const char* msg, int level) {
  2637. lua_Debug ar;
  2638. int top = lua_gettop(L);
  2639. int numlevels = compat53_countlevels(L1);
  2640. int mark = (numlevels > COMPAT53_LEVELS1 + COMPAT53_LEVELS2) ? COMPAT53_LEVELS1 : 0;
  2641. if (msg)
  2642. lua_pushfstring(L, "%s\n", msg);
  2643. lua_pushliteral(L, "stack traceback:");
  2644. while (lua_getstack(L1, level++, &ar)) {
  2645. if (level == mark) { /* too many levels? */
  2646. lua_pushliteral(L, "\n\t..."); /* add a '...' */
  2647. level = numlevels - COMPAT53_LEVELS2; /* and skip to last ones */
  2648. }
  2649. else {
  2650. lua_getinfo(L1, "Slnt", &ar);
  2651. lua_pushfstring(L, "\n\t%s:", ar.short_src);
  2652. if (ar.currentline > 0)
  2653. lua_pushfstring(L, "%d:", ar.currentline);
  2654. lua_pushliteral(L, " in ");
  2655. compat53_pushfuncname(L, &ar);
  2656. lua_concat(L, lua_gettop(L) - top);
  2657. }
  2658. }
  2659. lua_concat(L, lua_gettop(L) - top);
  2660. }
  2661. COMPAT53_API int luaL_fileresult(lua_State* L, int stat, const char* fname) {
  2662. const char* serr = NULL;
  2663. int en = errno; /* calls to Lua API may change this value */
  2664. char buf[512] = { 0 };
  2665. if (stat) {
  2666. lua_pushboolean(L, 1);
  2667. return 1;
  2668. }
  2669. else {
  2670. lua_pushnil(L);
  2671. serr = compat53_strerror(en, buf, sizeof(buf));
  2672. if (fname)
  2673. lua_pushfstring(L, "%s: %s", fname, serr);
  2674. else
  2675. lua_pushstring(L, serr);
  2676. lua_pushnumber(L, (lua_Number)en);
  2677. return 3;
  2678. }
  2679. }
  2680. static int compat53_checkmode(lua_State* L, const char* mode, const char* modename, int err) {
  2681. if (mode && strchr(mode, modename[0]) == NULL) {
  2682. lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode);
  2683. return err;
  2684. }
  2685. return LUA_OK;
  2686. }
  2687. typedef struct {
  2688. lua_Reader reader;
  2689. void* ud;
  2690. int has_peeked_data;
  2691. const char* peeked_data;
  2692. size_t peeked_data_size;
  2693. } compat53_reader_data;
  2694. static const char* compat53_reader(lua_State* L, void* ud, size_t* size) {
  2695. compat53_reader_data* data = (compat53_reader_data*)ud;
  2696. if (data->has_peeked_data) {
  2697. data->has_peeked_data = 0;
  2698. *size = data->peeked_data_size;
  2699. return data->peeked_data;
  2700. }
  2701. else
  2702. return data->reader(L, data->ud, size);
  2703. }
  2704. COMPAT53_API int lua_load(lua_State* L, lua_Reader reader, void* data, const char* source, const char* mode) {
  2705. int status = LUA_OK;
  2706. compat53_reader_data compat53_data = { reader, data, 1, 0, 0 };
  2707. compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size));
  2708. if (compat53_data.peeked_data && compat53_data.peeked_data_size && compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */
  2709. status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
  2710. else
  2711. status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
  2712. if (status != LUA_OK)
  2713. return status;
  2714. /* we need to call the original 5.1 version of lua_load! */
  2715. #undef lua_load
  2716. return lua_load(L, compat53_reader, &compat53_data, source);
  2717. #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53)
  2718. }
  2719. typedef struct {
  2720. int n; /* number of pre-read characters */
  2721. FILE* f; /* file being read */
  2722. char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
  2723. } compat53_LoadF;
  2724. static const char* compat53_getF(lua_State* L, void* ud, size_t* size) {
  2725. compat53_LoadF* lf = (compat53_LoadF*)ud;
  2726. (void)L; /* not used */
  2727. if (lf->n > 0) { /* are there pre-read characters to be read? */
  2728. *size = lf->n; /* return them (chars already in buffer) */
  2729. lf->n = 0; /* no more pre-read characters */
  2730. }
  2731. else { /* read a block from file */
  2732. /* 'fread' can return > 0 *and* set the EOF flag. If next call to
  2733. 'compat53_getF' called 'fread', it might still wait for user input.
  2734. The next check avoids this problem. */
  2735. if (feof(lf->f))
  2736. return NULL;
  2737. *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
  2738. }
  2739. return lf->buff;
  2740. }
  2741. static int compat53_errfile(lua_State* L, const char* what, int fnameindex) {
  2742. char buf[512] = { 0 };
  2743. const char* serr = compat53_strerror(errno, buf, sizeof(buf));
  2744. const char* filename = lua_tostring(L, fnameindex) + 1;
  2745. lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
  2746. lua_remove(L, fnameindex);
  2747. return LUA_ERRFILE;
  2748. }
  2749. static int compat53_skipBOM(compat53_LoadF* lf) {
  2750. const char* p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
  2751. int c;
  2752. lf->n = 0;
  2753. do {
  2754. c = getc(lf->f);
  2755. if (c == EOF || c != *(const unsigned char*)p++)
  2756. return c;
  2757. lf->buff[lf->n++] = (char)c; /* to be read by the parser */
  2758. } while (*p != '\0');
  2759. lf->n = 0; /* prefix matched; discard it */
  2760. return getc(lf->f); /* return next character */
  2761. }
  2762. /*
  2763. ** reads the first character of file 'f' and skips an optional BOM mark
  2764. ** in its beginning plus its first line if it starts with '#'. Returns
  2765. ** true if it skipped the first line. In any case, '*cp' has the
  2766. ** first "valid" character of the file (after the optional BOM and
  2767. ** a first-line comment).
  2768. */
  2769. static int compat53_skipcomment(compat53_LoadF* lf, int* cp) {
  2770. int c = *cp = compat53_skipBOM(lf);
  2771. if (c == '#') { /* first line is a comment (Unix exec. file)? */
  2772. do { /* skip first line */
  2773. c = getc(lf->f);
  2774. } while (c != EOF && c != '\n');
  2775. *cp = getc(lf->f); /* skip end-of-line, if present */
  2776. return 1; /* there was a comment */
  2777. }
  2778. else
  2779. return 0; /* no comment */
  2780. }
  2781. COMPAT53_API int luaL_loadfilex(lua_State* L, const char* filename, const char* mode) {
  2782. compat53_LoadF lf;
  2783. int status, readstatus;
  2784. int c;
  2785. int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
  2786. if (filename == NULL) {
  2787. lua_pushliteral(L, "=stdin");
  2788. lf.f = stdin;
  2789. }
  2790. else {
  2791. lua_pushfstring(L, "@%s", filename);
  2792. #if defined(_MSC_VER)
  2793. /* This code is here to stop a deprecation error that stops builds
  2794. * if a certain macro is defined. While normally not caring would
  2795. * be best, some header-only libraries and builds can't afford to
  2796. * dictate this to the user. A quick check shows that fopen_s this
  2797. * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET,
  2798. * possibly even before that so we don't need to do any version
  2799. * number checks, since this has been there since forever. */
  2800. /* TO USER: if you want the behavior of typical fopen_s/fopen,
  2801. * which does lock the file on VC++, define the macro used below to 0 */
  2802. #if COMPAT53_FOPEN_NO_LOCK
  2803. lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */
  2804. if (lf.f == NULL)
  2805. return compat53_errfile(L, "open", fnameindex);
  2806. #else /* use default locking version */
  2807. if (fopen_s(&lf.f, filename, "r") != 0)
  2808. return compat53_errfile(L, "open", fnameindex);
  2809. #endif /* Locking vs. No-locking fopen variants */
  2810. #else
  2811. lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
  2812. if (lf.f == NULL)
  2813. return compat53_errfile(L, "open", fnameindex);
  2814. #endif
  2815. }
  2816. if (compat53_skipcomment(&lf, &c)) /* read initial portion */
  2817. lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
  2818. if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
  2819. #if defined(_MSC_VER)
  2820. if (freopen_s(&lf.f, filename, "rb", lf.f) != 0)
  2821. return compat53_errfile(L, "reopen", fnameindex);
  2822. #else
  2823. lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
  2824. if (lf.f == NULL)
  2825. return compat53_errfile(L, "reopen", fnameindex);
  2826. #endif
  2827. compat53_skipcomment(&lf, &c); /* re-read initial portion */
  2828. }
  2829. if (c != EOF)
  2830. lf.buff[lf.n++] = (char)c; /* 'c' is the first character of the stream */
  2831. status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode);
  2832. readstatus = ferror(lf.f);
  2833. if (filename)
  2834. fclose(lf.f); /* close file (even in case of errors) */
  2835. if (readstatus) {
  2836. lua_settop(L, fnameindex); /* ignore results from 'lua_load' */
  2837. return compat53_errfile(L, "read", fnameindex);
  2838. }
  2839. lua_remove(L, fnameindex);
  2840. return status;
  2841. }
  2842. COMPAT53_API int luaL_loadbufferx(lua_State* L, const char* buff, size_t sz, const char* name, const char* mode) {
  2843. int status = LUA_OK;
  2844. if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) {
  2845. status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX);
  2846. }
  2847. else {
  2848. status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX);
  2849. }
  2850. if (status != LUA_OK)
  2851. return status;
  2852. return luaL_loadbuffer(L, buff, sz, name);
  2853. }
  2854. #if !defined(l_inspectstat) \
  2855. && (defined(unix) || defined(__unix) || defined(__unix__) || defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || (defined(__APPLE__) && defined(__MACH__)))
  2856. /* some form of unix; check feature macros in unistd.h for details */
  2857. #include <unistd.h>
  2858. /* check posix version; the relevant include files and macros probably
  2859. * were available before 2001, but I'm not sure */
  2860. #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
  2861. #include <sys/wait.h>
  2862. #define l_inspectstat(stat, what) \
  2863. if (WIFEXITED(stat)) { \
  2864. stat = WEXITSTATUS(stat); \
  2865. } \
  2866. else if (WIFSIGNALED(stat)) { \
  2867. stat = WTERMSIG(stat); \
  2868. what = "signal"; \
  2869. }
  2870. #endif
  2871. #endif
  2872. /* provide default (no-op) version */
  2873. #if !defined(l_inspectstat)
  2874. #define l_inspectstat(stat, what) ((void)0)
  2875. #endif
  2876. COMPAT53_API int luaL_execresult(lua_State* L, int stat) {
  2877. const char* what = "exit";
  2878. if (stat == -1)
  2879. return luaL_fileresult(L, 0, NULL);
  2880. else {
  2881. l_inspectstat(stat, what);
  2882. if (*what == 'e' && stat == 0)
  2883. lua_pushboolean(L, 1);
  2884. else
  2885. lua_pushnil(L);
  2886. lua_pushstring(L, what);
  2887. lua_pushinteger(L, stat);
  2888. return 3;
  2889. }
  2890. }
  2891. COMPAT53_API void luaL_buffinit(lua_State* L, luaL_Buffer_53* B) {
  2892. /* make it crash if used via pointer to a 5.1-style luaL_Buffer */
  2893. B->b.p = NULL;
  2894. B->b.L = NULL;
  2895. B->b.lvl = 0;
  2896. /* reuse the buffer from the 5.1-style luaL_Buffer though! */
  2897. B->ptr = B->b.buffer;
  2898. B->capacity = LUAL_BUFFERSIZE;
  2899. B->nelems = 0;
  2900. B->L2 = L;
  2901. }
  2902. COMPAT53_API char* luaL_prepbuffsize(luaL_Buffer_53* B, size_t s) {
  2903. if (B->capacity - B->nelems < s) { /* needs to grow */
  2904. char* newptr = NULL;
  2905. size_t newcap = B->capacity * 2;
  2906. if (newcap - B->nelems < s)
  2907. newcap = B->nelems + s;
  2908. if (newcap < B->capacity) /* overflow */
  2909. luaL_error(B->L2, "buffer too large");
  2910. newptr = (char*)lua_newuserdata(B->L2, newcap);
  2911. memcpy(newptr, B->ptr, B->nelems);
  2912. if (B->ptr != B->b.buffer)
  2913. lua_replace(B->L2, -2); /* remove old buffer */
  2914. B->ptr = newptr;
  2915. B->capacity = newcap;
  2916. }
  2917. return B->ptr + B->nelems;
  2918. }
  2919. COMPAT53_API void luaL_addlstring(luaL_Buffer_53* B, const char* s, size_t l) {
  2920. memcpy(luaL_prepbuffsize(B, l), s, l);
  2921. luaL_addsize(B, l);
  2922. }
  2923. COMPAT53_API void luaL_addvalue(luaL_Buffer_53* B) {
  2924. size_t len = 0;
  2925. const char* s = lua_tolstring(B->L2, -1, &len);
  2926. if (!s)
  2927. luaL_error(B->L2, "cannot convert value to string");
  2928. if (B->ptr != B->b.buffer)
  2929. lua_insert(B->L2, -2); /* userdata buffer must be at stack top */
  2930. luaL_addlstring(B, s, len);
  2931. lua_remove(B->L2, B->ptr != B->b.buffer ? -2 : -1);
  2932. }
  2933. void luaL_pushresult(luaL_Buffer_53* B) {
  2934. lua_pushlstring(B->L2, B->ptr, B->nelems);
  2935. if (B->ptr != B->b.buffer)
  2936. lua_replace(B->L2, -2); /* remove userdata buffer */
  2937. }
  2938. #endif /* Lua 5.1 */
  2939. /* definitions for Lua 5.1 and Lua 5.2 */
  2940. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
  2941. COMPAT53_API int lua_geti(lua_State* L, int index, lua_Integer i) {
  2942. index = lua_absindex(L, index);
  2943. lua_pushinteger(L, i);
  2944. lua_gettable(L, index);
  2945. return lua_type(L, -1);
  2946. }
  2947. COMPAT53_API int lua_isinteger(lua_State* L, int index) {
  2948. if (lua_type(L, index) == LUA_TNUMBER) {
  2949. lua_Number n = lua_tonumber(L, index);
  2950. lua_Integer i = lua_tointeger(L, index);
  2951. if (i == n)
  2952. return 1;
  2953. }
  2954. return 0;
  2955. }
  2956. COMPAT53_API lua_Integer lua_tointegerx(lua_State* L, int i, int* isnum) {
  2957. int ok = 0;
  2958. lua_Number n = lua_tonumberx(L, i, &ok);
  2959. if (ok) {
  2960. if (n == (lua_Integer)n) {
  2961. if (isnum)
  2962. *isnum = 1;
  2963. return (lua_Integer)n;
  2964. }
  2965. }
  2966. if (isnum)
  2967. *isnum = 0;
  2968. return 0;
  2969. }
  2970. static void compat53_reverse(lua_State* L, int a, int b) {
  2971. for (; a < b; ++a, --b) {
  2972. lua_pushvalue(L, a);
  2973. lua_pushvalue(L, b);
  2974. lua_replace(L, a);
  2975. lua_replace(L, b);
  2976. }
  2977. }
  2978. COMPAT53_API void lua_rotate(lua_State* L, int idx, int n) {
  2979. int n_elems = 0;
  2980. idx = lua_absindex(L, idx);
  2981. n_elems = lua_gettop(L) - idx + 1;
  2982. if (n < 0)
  2983. n += n_elems;
  2984. if (n > 0 && n < n_elems) {
  2985. luaL_checkstack(L, 2, "not enough stack slots available");
  2986. n = n_elems - n;
  2987. compat53_reverse(L, idx, idx + n - 1);
  2988. compat53_reverse(L, idx + n, idx + n_elems - 1);
  2989. compat53_reverse(L, idx, idx + n_elems - 1);
  2990. }
  2991. }
  2992. COMPAT53_API void lua_seti(lua_State* L, int index, lua_Integer i) {
  2993. luaL_checkstack(L, 1, "not enough stack slots available");
  2994. index = lua_absindex(L, index);
  2995. lua_pushinteger(L, i);
  2996. lua_insert(L, -2);
  2997. lua_settable(L, index);
  2998. }
  2999. #if !defined(lua_str2number)
  3000. #define lua_str2number(s, p) strtod((s), (p))
  3001. #endif
  3002. COMPAT53_API size_t lua_stringtonumber(lua_State* L, const char* s) {
  3003. char* endptr;
  3004. lua_Number n = lua_str2number(s, &endptr);
  3005. if (endptr != s) {
  3006. while (*endptr != '\0' && isspace((unsigned char)*endptr))
  3007. ++endptr;
  3008. if (*endptr == '\0') {
  3009. lua_pushnumber(L, n);
  3010. return endptr - s + 1;
  3011. }
  3012. }
  3013. return 0;
  3014. }
  3015. COMPAT53_API const char* luaL_tolstring(lua_State* L, int idx, size_t* len) {
  3016. if (!luaL_callmeta(L, idx, "__tostring")) {
  3017. int t = lua_type(L, idx), tt = 0;
  3018. char const* name = NULL;
  3019. switch (t) {
  3020. case LUA_TNIL:
  3021. lua_pushliteral(L, "nil");
  3022. break;
  3023. case LUA_TSTRING:
  3024. case LUA_TNUMBER:
  3025. lua_pushvalue(L, idx);
  3026. break;
  3027. case LUA_TBOOLEAN:
  3028. if (lua_toboolean(L, idx))
  3029. lua_pushliteral(L, "true");
  3030. else
  3031. lua_pushliteral(L, "false");
  3032. break;
  3033. default:
  3034. tt = luaL_getmetafield(L, idx, "__name");
  3035. name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
  3036. lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
  3037. if (tt != LUA_TNIL)
  3038. lua_replace(L, -2);
  3039. break;
  3040. }
  3041. }
  3042. else {
  3043. if (!lua_isstring(L, -1))
  3044. luaL_error(L, "'__tostring' must return a string");
  3045. }
  3046. return lua_tolstring(L, -1, len);
  3047. }
  3048. COMPAT53_API void luaL_requiref(lua_State* L, const char* modname, lua_CFunction openf, int glb) {
  3049. luaL_checkstack(L, 3, "not enough stack slots available");
  3050. luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
  3051. if (lua_getfield(L, -1, modname) == LUA_TNIL) {
  3052. lua_pop(L, 1);
  3053. lua_pushcfunction(L, openf);
  3054. lua_pushstring(L, modname);
  3055. lua_call(L, 1, 1);
  3056. lua_pushvalue(L, -1);
  3057. lua_setfield(L, -3, modname);
  3058. }
  3059. if (glb) {
  3060. lua_pushvalue(L, -1);
  3061. lua_setglobal(L, modname);
  3062. }
  3063. lua_replace(L, -2);
  3064. }
  3065. #endif /* Lua 5.1 and 5.2 */
  3066. #endif /* KEPLER_PROJECT_COMPAT53_C_ */
  3067. /*********************************************************************
  3068. * This file contains parts of Lua 5.2's and Lua 5.3's source code:
  3069. *
  3070. * Copyright (C) 1994-2014 Lua.org, PUC-Rio.
  3071. *
  3072. * Permission is hereby granted, free of charge, to any person obtaining
  3073. * a copy of this software and associated documentation files (the
  3074. * "Software"), to deal in the Software without restriction, including
  3075. * without limitation the rights to use, copy, modify, merge, publish,
  3076. * distribute, sublicense, and/or sell copies of the Software, and to
  3077. * permit persons to whom the Software is furnished to do so, subject to
  3078. * the following conditions:
  3079. *
  3080. * The above copyright notice and this permission notice shall be
  3081. * included in all copies or substantial portions of the Software.
  3082. *
  3083. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  3084. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  3085. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  3086. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  3087. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  3088. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  3089. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  3090. *********************************************************************/
  3091. // end of sol/compatibility/compat-5.3.c.h
  3092. #endif
  3093. #endif /* KEPLER_PROJECT_COMPAT53_H_ */
  3094. // end of sol/compatibility/compat-5.3.h
  3095. // beginning of sol/compatibility/compat-5.4.h
  3096. #ifndef NOT_KEPLER_PROJECT_COMPAT54_H_
  3097. #define NOT_KEPLER_PROJECT_COMPAT54_H_
  3098. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  3099. extern "C" {
  3100. #endif
  3101. #include <lua.h>
  3102. #include <lauxlib.h>
  3103. #include <lualib.h>
  3104. #if defined(__cplusplus) && !defined(COMPAT53_LUA_CPP)
  3105. }
  3106. #endif
  3107. #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 504
  3108. #if !defined(LUA_ERRGCMM)
  3109. /* So Lua 5.4 actually removes this, which breaks sol2...
  3110. man, this API is quite unstable...!
  3111. */
  3112. # define LUA_ERRGCMM (LUA_ERRERR + 2)
  3113. #endif /* LUA_ERRGCMM define */
  3114. #endif // Lua 5.4 only
  3115. #endif // NOT_KEPLER_PROJECT_COMPAT54_H_// end of sol/compatibility/compat-5.4.h
  3116. #endif
  3117. // end of sol/compatibility.hpp
  3118. #include <vector>
  3119. #include <cstdint>
  3120. #include <cstddef>
  3121. namespace sol {
  3122. template <typename Allocator = std::allocator<std::byte>>
  3123. class basic_bytecode : private std::vector<std::byte, Allocator> {
  3124. private:
  3125. using base_t = std::vector<std::byte, Allocator>;
  3126. public:
  3127. using typename base_t::allocator_type;
  3128. using typename base_t::const_iterator;
  3129. using typename base_t::const_pointer;
  3130. using typename base_t::const_reference;
  3131. using typename base_t::const_reverse_iterator;
  3132. using typename base_t::difference_type;
  3133. using typename base_t::iterator;
  3134. using typename base_t::pointer;
  3135. using typename base_t::reference;
  3136. using typename base_t::reverse_iterator;
  3137. using typename base_t::size_type;
  3138. using typename base_t::value_type;
  3139. using base_t::base_t;
  3140. using base_t::operator=;
  3141. using base_t::data;
  3142. using base_t::empty;
  3143. using base_t::max_size;
  3144. using base_t::size;
  3145. using base_t::at;
  3146. using base_t::operator[];
  3147. using base_t::back;
  3148. using base_t::front;
  3149. using base_t::begin;
  3150. using base_t::cbegin;
  3151. using base_t::cend;
  3152. using base_t::end;
  3153. using base_t::crbegin;
  3154. using base_t::crend;
  3155. using base_t::rbegin;
  3156. using base_t::rend;
  3157. using base_t::get_allocator;
  3158. using base_t::swap;
  3159. using base_t::clear;
  3160. using base_t::emplace;
  3161. using base_t::emplace_back;
  3162. using base_t::erase;
  3163. using base_t::insert;
  3164. using base_t::pop_back;
  3165. using base_t::push_back;
  3166. using base_t::reserve;
  3167. using base_t::resize;
  3168. using base_t::shrink_to_fit;
  3169. string_view as_string_view() const {
  3170. return string_view(reinterpret_cast<const char*>(this->data()), this->size());
  3171. }
  3172. };
  3173. template <typename Container>
  3174. inline int basic_insert_dump_writer(lua_State*, const void* memory, size_t memory_size, void* userdata) {
  3175. using storage_t = Container;
  3176. const std::byte* p_code = static_cast<const std::byte*>(memory);
  3177. storage_t& bc = *static_cast<storage_t*>(userdata);
  3178. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  3179. bc.insert(bc.cend(), p_code, p_code + memory_size);
  3180. #else
  3181. try {
  3182. bc.insert(bc.cend(), p_code, p_code + memory_size);
  3183. }
  3184. catch (...) {
  3185. return -1;
  3186. }
  3187. #endif
  3188. return 0;
  3189. }
  3190. using bytecode = basic_bytecode<>;
  3191. constexpr inline auto bytecode_dump_writer = &basic_insert_dump_writer<bytecode>;
  3192. } // namespace sol
  3193. // end of sol/bytecode.hpp
  3194. // beginning of sol/stack.hpp
  3195. // beginning of sol/trampoline.hpp
  3196. // beginning of sol/types.hpp
  3197. // beginning of sol/error.hpp
  3198. #include <stdexcept>
  3199. #include <string>
  3200. #include <array>
  3201. namespace sol {
  3202. namespace detail {
  3203. struct direct_error_tag {};
  3204. const auto direct_error = direct_error_tag{};
  3205. struct error_result {
  3206. int results;
  3207. const char* format_string;
  3208. std::array<const char*, 4> args_strings;
  3209. error_result() : results(0), format_string(nullptr) {
  3210. }
  3211. error_result(int results) : results(results), format_string(nullptr) {
  3212. }
  3213. error_result(const char* fmt, const char* msg) : results(0), format_string(fmt) {
  3214. args_strings[0] = msg;
  3215. }
  3216. };
  3217. inline int handle_errors(lua_State* L, const error_result& er) {
  3218. if (er.format_string == nullptr) {
  3219. return er.results;
  3220. }
  3221. return luaL_error(L, er.format_string, er.args_strings[0], er.args_strings[1], er.args_strings[2], er.args_strings[3]);
  3222. }
  3223. } // namespace detail
  3224. class error : public std::runtime_error {
  3225. private:
  3226. // Because VC++ is upsetting, most of the time!
  3227. std::string what_reason;
  3228. public:
  3229. error(const std::string& str) : error(detail::direct_error, "lua: error: " + str) {
  3230. }
  3231. error(std::string&& str) : error(detail::direct_error, "lua: error: " + std::move(str)) {
  3232. }
  3233. error(detail::direct_error_tag, const std::string& str) : std::runtime_error(""), what_reason(str) {
  3234. }
  3235. error(detail::direct_error_tag, std::string&& str) : std::runtime_error(""), what_reason(std::move(str)) {
  3236. }
  3237. error(const error& e) = default;
  3238. error(error&& e) = default;
  3239. error& operator=(const error& e) = default;
  3240. error& operator=(error&& e) = default;
  3241. virtual const char* what() const noexcept override {
  3242. return what_reason.c_str();
  3243. }
  3244. };
  3245. } // namespace sol
  3246. // end of sol/error.hpp
  3247. // beginning of sol/optional.hpp
  3248. // beginning of sol/in_place.hpp
  3249. #include <cstddef>
  3250. #include <utility>
  3251. namespace sol {
  3252. using in_place_t = std::in_place_t;
  3253. constexpr std::in_place_t in_place {};
  3254. constexpr std::in_place_t in_place_of {};
  3255. template <typename T>
  3256. using in_place_type_t = std::in_place_type_t<T>;
  3257. template <typename T>
  3258. constexpr std::in_place_type_t<T> in_place_type {};
  3259. template <size_t I>
  3260. using in_place_index_t = std::in_place_index_t<I>;
  3261. template <size_t I>
  3262. constexpr in_place_index_t<I> in_place_index {};
  3263. } // namespace sol
  3264. // end of sol/in_place.hpp
  3265. #if SOL_IS_ON(SOL_USE_BOOST_I_)
  3266. #include <boost/optional.hpp>
  3267. #else
  3268. // beginning of sol/optional_implementation.hpp
  3269. #define SOL_TL_OPTIONAL_VERSION_MAJOR 0
  3270. #define SOL_TL_OPTIONAL_VERSION_MINOR 5
  3271. #include <exception>
  3272. #include <functional>
  3273. #include <new>
  3274. #include <type_traits>
  3275. #include <utility>
  3276. #include <cstdlib>
  3277. #include <optional>
  3278. #if (defined(_MSC_VER) && _MSC_VER == 1900)
  3279. #define SOL_TL_OPTIONAL_MSVC2015
  3280. #endif
  3281. #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
  3282. #define SOL_TL_OPTIONAL_GCC49
  3283. #endif
  3284. #if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && !defined(__clang__))
  3285. #define SOL_TL_OPTIONAL_GCC54
  3286. #endif
  3287. #if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && !defined(__clang__))
  3288. #define SOL_TL_OPTIONAL_GCC55
  3289. #endif
  3290. #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__))
  3291. #define SOL_TL_OPTIONAL_NO_CONSTRR
  3292. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::has_trivial_copy_constructor<T>::value
  3293. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign<T>::value
  3294. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
  3295. #elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__))
  3296. #ifndef SOL_TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
  3297. #define SOL_TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
  3298. namespace sol { namespace detail {
  3299. template <class T>
  3300. struct is_trivially_copy_constructible : std::is_trivially_copy_constructible<T> {};
  3301. #ifdef _GLIBCXX_VECTOR
  3302. template <class T, class A>
  3303. struct is_trivially_copy_constructible<std::vector<T, A>> : std::is_trivially_copy_constructible<T> {};
  3304. #endif
  3305. }} // namespace sol::detail
  3306. #endif
  3307. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) sol::detail::is_trivially_copy_constructible<T>::value
  3308. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable<T>::value
  3309. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
  3310. #else
  3311. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::is_trivially_copy_constructible<T>::value
  3312. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable<T>::value
  3313. #define SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>::value
  3314. #endif
  3315. #if __cplusplus > 201103L
  3316. #define SOL_TL_OPTIONAL_CXX14
  3317. #endif
  3318. #if (__cplusplus == 201103L || defined(SOL_TL_OPTIONAL_MSVC2015) || defined(SOL_TL_OPTIONAL_GCC49))
  3319. #define SOL_TL_OPTIONAL_11_CONSTEXPR
  3320. #else
  3321. /// \exclude
  3322. #define SOL_TL_OPTIONAL_11_CONSTEXPR constexpr
  3323. #endif
  3324. namespace sol {
  3325. #ifndef SOL_TL_MONOSTATE_INPLACE_MUTEX
  3326. #define SOL_TL_MONOSTATE_INPLACE_MUTEX
  3327. /// \brief Used to represent an optional with no data; essentially a bool
  3328. class monostate {};
  3329. #endif
  3330. template <class T>
  3331. class optional;
  3332. /// \exclude
  3333. namespace detail {
  3334. #ifndef SOL_TL_TRAITS_MUTEX
  3335. #define SOL_TL_TRAITS_MUTEX
  3336. // C++14-style aliases for brevity
  3337. template <class T>
  3338. using remove_const_t = typename std::remove_const<T>::type;
  3339. template <class T>
  3340. using remove_reference_t = typename std::remove_reference<T>::type;
  3341. template <class T>
  3342. using decay_t = typename std::decay<T>::type;
  3343. template <bool E, class T = void>
  3344. using enable_if_t = typename std::enable_if<E, T>::type;
  3345. template <bool B, class T, class F>
  3346. using conditional_t = typename std::conditional<B, T, F>::type;
  3347. // std::conjunction from C++17
  3348. template <class...>
  3349. struct conjunction : std::true_type {};
  3350. template <class B>
  3351. struct conjunction<B> : B {};
  3352. template <class B, class... Bs>
  3353. struct conjunction<B, Bs...> : std::conditional<bool(B::value), conjunction<Bs...>, B>::type {};
  3354. #if defined(_LIBCPP_VERSION) && __cplusplus == 201103L
  3355. #define SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
  3356. #endif
  3357. #ifdef SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
  3358. template <class T>
  3359. struct is_pointer_to_non_const_member_func : std::false_type {};
  3360. template <class T, class Ret, class... Args>
  3361. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)> : std::true_type {};
  3362. template <class T, class Ret, class... Args>
  3363. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)&> : std::true_type {};
  3364. template <class T, class Ret, class... Args>
  3365. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &&> : std::true_type {};
  3366. template <class T, class Ret, class... Args>
  3367. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile> : std::true_type {};
  3368. template <class T, class Ret, class... Args>
  3369. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&> : std::true_type {};
  3370. template <class T, class Ret, class... Args>
  3371. struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&&> : std::true_type {};
  3372. template <class T>
  3373. struct is_const_or_const_ref : std::false_type {};
  3374. template <class T>
  3375. struct is_const_or_const_ref<T const&> : std::true_type {};
  3376. template <class T>
  3377. struct is_const_or_const_ref<T const> : std::true_type {};
  3378. #endif
  3379. // std::invoke from C++17
  3380. // https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
  3381. template <typename Fn, typename... Args,
  3382. #ifdef SOL_TL_OPTIONAL_LIBCXX_MEM_FN_WORKAROUND
  3383. typename = enable_if_t<!(is_pointer_to_non_const_member_func<Fn>::value && is_const_or_const_ref<Args...>::value)>,
  3384. #endif
  3385. typename = enable_if_t<std::is_member_pointer<decay_t<Fn>>::value>, int = 0>
  3386. constexpr auto invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
  3387. -> decltype(std::mem_fn(f)(std::forward<Args>(args)...)) {
  3388. return std::mem_fn(f)(std::forward<Args>(args)...);
  3389. }
  3390. template <typename Fn, typename... Args, typename = enable_if_t<!std::is_member_pointer<decay_t<Fn>>::value>>
  3391. constexpr auto invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::forward<Fn>(f)(std::forward<Args>(args)...)))
  3392. -> decltype(std::forward<Fn>(f)(std::forward<Args>(args)...)) {
  3393. return std::forward<Fn>(f)(std::forward<Args>(args)...);
  3394. }
  3395. // std::invoke_result from C++17
  3396. template <class F, class, class... Us>
  3397. struct invoke_result_impl;
  3398. template <class F, class... Us>
  3399. struct invoke_result_impl<F, decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...), void()), Us...> {
  3400. using type = decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...));
  3401. };
  3402. template <class F, class... Us>
  3403. using invoke_result = invoke_result_impl<F, void, Us...>;
  3404. template <class F, class... Us>
  3405. using invoke_result_t = typename invoke_result<F, Us...>::type;
  3406. #endif
  3407. // std::void_t from C++17
  3408. template <class...>
  3409. struct voider {
  3410. using type = void;
  3411. };
  3412. template <class... Ts>
  3413. using void_t = typename voider<Ts...>::type;
  3414. // Trait for checking if a type is a sol::optional
  3415. template <class T>
  3416. struct is_optional_impl : std::false_type {};
  3417. template <class T>
  3418. struct is_optional_impl<optional<T>> : std::true_type {};
  3419. template <class T>
  3420. using is_optional = is_optional_impl<decay_t<T>>;
  3421. // Change void to sol::monostate
  3422. template <class U>
  3423. using fixup_void = conditional_t<std::is_void<U>::value, monostate, U>;
  3424. template <class F, class U, class = invoke_result_t<F, U>>
  3425. using get_map_return = optional<fixup_void<invoke_result_t<F, U>>>;
  3426. // Check if invoking F for some Us returns void
  3427. template <class F, class = void, class... U>
  3428. struct returns_void_impl;
  3429. template <class F, class... U>
  3430. struct returns_void_impl<F, void_t<invoke_result_t<F, U...>>, U...> : std::is_void<invoke_result_t<F, U...>> {};
  3431. template <class F, class... U>
  3432. using returns_void = returns_void_impl<F, void, U...>;
  3433. template <class T, class... U>
  3434. using enable_if_ret_void = enable_if_t<returns_void<T&&, U...>::value>;
  3435. template <class T, class... U>
  3436. using disable_if_ret_void = enable_if_t<!returns_void<T&&, U...>::value>;
  3437. template <class T, class U>
  3438. using enable_forward_value = detail::enable_if_t<std::is_constructible<T, U&&>::value && !std::is_same<detail::decay_t<U>, in_place_t>::value
  3439. && !std::is_same<optional<T>, detail::decay_t<U>>::value>;
  3440. template <class T, class U, class Other>
  3441. using enable_from_other = detail::enable_if_t<std::is_constructible<T, Other>::value && !std::is_constructible<T, optional<U>&>::value
  3442. && !std::is_constructible<T, optional<U>&&>::value && !std::is_constructible<T, const optional<U>&>::value
  3443. && !std::is_constructible<T, const optional<U>&&>::value && !std::is_convertible<optional<U>&, T>::value
  3444. && !std::is_convertible<optional<U>&&, T>::value && !std::is_convertible<const optional<U>&, T>::value
  3445. && !std::is_convertible<const optional<U>&&, T>::value>;
  3446. template <class T, class U>
  3447. using enable_assign_forward = detail::enable_if_t<!std::is_same<optional<T>, detail::decay_t<U>>::value
  3448. && !detail::conjunction<std::is_scalar<T>, std::is_same<T, detail::decay_t<U>>>::value && std::is_constructible<T, U>::value
  3449. && std::is_assignable<T&, U>::value>;
  3450. template <class T, class U, class Other>
  3451. using enable_assign_from_other = detail::enable_if_t<std::is_constructible<T, Other>::value && std::is_assignable<T&, Other>::value
  3452. && !std::is_constructible<T, optional<U>&>::value && !std::is_constructible<T, optional<U>&&>::value
  3453. && !std::is_constructible<T, const optional<U>&>::value && !std::is_constructible<T, const optional<U>&&>::value
  3454. && !std::is_convertible<optional<U>&, T>::value && !std::is_convertible<optional<U>&&, T>::value
  3455. && !std::is_convertible<const optional<U>&, T>::value && !std::is_convertible<const optional<U>&&, T>::value
  3456. && !std::is_assignable<T&, optional<U>&>::value && !std::is_assignable<T&, optional<U>&&>::value
  3457. && !std::is_assignable<T&, const optional<U>&>::value && !std::is_assignable<T&, const optional<U>&&>::value>;
  3458. #ifdef _MSC_VER
  3459. // TODO make a version which works with MSVC
  3460. template <class T, class U = T>
  3461. struct is_swappable : std::true_type {};
  3462. template <class T, class U = T>
  3463. struct is_nothrow_swappable : std::true_type {};
  3464. #else
  3465. // https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept
  3466. namespace swap_adl_tests {
  3467. // if swap ADL finds this then it would call std::swap otherwise (same
  3468. // signature)
  3469. struct tag {};
  3470. template <class T>
  3471. tag swap(T&, T&);
  3472. template <class T, std::size_t N>
  3473. tag swap(T (&a)[N], T (&b)[N]);
  3474. // helper functions to test if an unqualified swap is possible, and if it
  3475. // becomes std::swap
  3476. template <class, class>
  3477. std::false_type can_swap(...) noexcept(false);
  3478. template <class T, class U, class = decltype(swap(std::declval<T&>(), std::declval<U&>()))>
  3479. std::true_type can_swap(int) noexcept(noexcept(swap(std::declval<T&>(), std::declval<U&>())));
  3480. template <class, class>
  3481. std::false_type uses_std(...);
  3482. template <class T, class U>
  3483. std::is_same<decltype(swap(std::declval<T&>(), std::declval<U&>())), tag> uses_std(int);
  3484. template <class T>
  3485. struct is_std_swap_noexcept
  3486. : std::integral_constant<bool, std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value> {};
  3487. template <class T, std::size_t N>
  3488. struct is_std_swap_noexcept<T[N]> : is_std_swap_noexcept<T> {};
  3489. template <class T, class U>
  3490. struct is_adl_swap_noexcept : std::integral_constant<bool, noexcept(can_swap<T, U>(0))> {};
  3491. } // namespace swap_adl_tests
  3492. template <class T, class U = T>
  3493. struct is_swappable : std::integral_constant<bool,
  3494. decltype(detail::swap_adl_tests::can_swap<T, U>(0))::value
  3495. && (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value
  3496. || (std::is_move_assignable<T>::value && std::is_move_constructible<T>::value))> {};
  3497. template <class T, std::size_t N>
  3498. struct is_swappable<T[N], T[N]> : std::integral_constant<bool,
  3499. decltype(detail::swap_adl_tests::can_swap<T[N], T[N]>(0))::value
  3500. && (!decltype(detail::swap_adl_tests::uses_std<T[N], T[N]>(0))::value || is_swappable<T, T>::value)> {};
  3501. template <class T, class U = T>
  3502. struct is_nothrow_swappable
  3503. : std::integral_constant<bool,
  3504. is_swappable<T, U>::value
  3505. && ((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value&& detail::swap_adl_tests::is_std_swap_noexcept<T>::value)
  3506. || (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value&& detail::swap_adl_tests::is_adl_swap_noexcept<T, U>::value))> {};
  3507. #endif
  3508. // The storage base manages the actual storage, and correctly propagates
  3509. // trivial destruction from T. This case is for when T is not trivially
  3510. // destructible.
  3511. template <class T, bool = ::std::is_trivially_destructible<T>::value>
  3512. struct optional_storage_base {
  3513. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {
  3514. }
  3515. template <class... U>
  3516. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U&&... u) : m_value(std::forward<U>(u)...), m_has_value(true) {
  3517. }
  3518. ~optional_storage_base() {
  3519. if (m_has_value) {
  3520. m_value.~T();
  3521. m_has_value = false;
  3522. }
  3523. }
  3524. struct dummy {};
  3525. union {
  3526. dummy m_dummy;
  3527. T m_value;
  3528. };
  3529. bool m_has_value;
  3530. };
  3531. // This case is for when T is trivially destructible.
  3532. template <class T>
  3533. struct optional_storage_base<T, true> {
  3534. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {
  3535. }
  3536. template <class... U>
  3537. SOL_TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U&&... u) : m_value(std::forward<U>(u)...), m_has_value(true) {
  3538. }
  3539. // No destructor, so this class is trivially destructible
  3540. struct dummy {};
  3541. union {
  3542. dummy m_dummy;
  3543. T m_value;
  3544. };
  3545. bool m_has_value = false;
  3546. };
  3547. // This base class provides some handy member functions which can be used in
  3548. // further derived classes
  3549. template <class T>
  3550. struct optional_operations_base : optional_storage_base<T> {
  3551. using optional_storage_base<T>::optional_storage_base;
  3552. void hard_reset() noexcept {
  3553. get().~T();
  3554. this->m_has_value = false;
  3555. }
  3556. template <class... Args>
  3557. void construct(Args&&... args) noexcept {
  3558. new (std::addressof(this->m_value)) T(std::forward<Args>(args)...);
  3559. this->m_has_value = true;
  3560. }
  3561. template <class Opt>
  3562. void assign(Opt&& rhs) {
  3563. if (this->has_value()) {
  3564. if (rhs.has_value()) {
  3565. this->m_value = std::forward<Opt>(rhs).get();
  3566. }
  3567. else {
  3568. this->m_value.~T();
  3569. this->m_has_value = false;
  3570. }
  3571. }
  3572. else if (rhs.has_value()) {
  3573. construct(std::forward<Opt>(rhs).get());
  3574. }
  3575. }
  3576. bool has_value() const {
  3577. return this->m_has_value;
  3578. }
  3579. SOL_TL_OPTIONAL_11_CONSTEXPR T& get() & {
  3580. return this->m_value;
  3581. }
  3582. SOL_TL_OPTIONAL_11_CONSTEXPR const T& get() const& {
  3583. return this->m_value;
  3584. }
  3585. SOL_TL_OPTIONAL_11_CONSTEXPR T&& get() && {
  3586. return std::move(this->m_value);
  3587. }
  3588. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  3589. constexpr const T&& get() const&& {
  3590. return std::move(this->m_value);
  3591. }
  3592. #endif
  3593. };
  3594. // This class manages conditionally having a trivial copy constructor
  3595. // This specialization is for when T is trivially copy constructible
  3596. template <class T, bool = SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)>
  3597. struct optional_copy_base : optional_operations_base<T> {
  3598. using optional_operations_base<T>::optional_operations_base;
  3599. };
  3600. // This specialization is for when T is not trivially copy constructible
  3601. template <class T>
  3602. struct optional_copy_base<T, false> : optional_operations_base<T> {
  3603. using base_t = optional_operations_base<T>;
  3604. using base_t::base_t;
  3605. optional_copy_base() = default;
  3606. optional_copy_base(const optional_copy_base& rhs) : base_t() {
  3607. if (rhs.has_value()) {
  3608. this->construct(rhs.get());
  3609. }
  3610. else {
  3611. this->m_has_value = false;
  3612. }
  3613. }
  3614. optional_copy_base(optional_copy_base&& rhs) = default;
  3615. optional_copy_base& operator=(const optional_copy_base& rhs) = default;
  3616. optional_copy_base& operator=(optional_copy_base&& rhs) = default;
  3617. };
  3618. #ifndef SOL_TL_OPTIONAL_GCC49
  3619. template <class T, bool = std::is_trivially_move_constructible<T>::value>
  3620. struct optional_move_base : optional_copy_base<T> {
  3621. using optional_copy_base<T>::optional_copy_base;
  3622. };
  3623. #else
  3624. template <class T, bool = false>
  3625. struct optional_move_base;
  3626. #endif
  3627. template <class T>
  3628. struct optional_move_base<T, false> : optional_copy_base<T> {
  3629. using optional_copy_base<T>::optional_copy_base;
  3630. optional_move_base() = default;
  3631. optional_move_base(const optional_move_base& rhs) = default;
  3632. optional_move_base(optional_move_base&& rhs) noexcept(std::is_nothrow_move_constructible<T>::value) {
  3633. if (rhs.has_value()) {
  3634. this->construct(std::move(rhs.get()));
  3635. }
  3636. else {
  3637. this->m_has_value = false;
  3638. }
  3639. }
  3640. optional_move_base& operator=(const optional_move_base& rhs) = default;
  3641. optional_move_base& operator=(optional_move_base&& rhs) = default;
  3642. };
  3643. // This class manages conditionally having a trivial copy assignment operator
  3644. template <class T,
  3645. bool = SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) && SOL_TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)
  3646. && SOL_TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T)>
  3647. struct optional_copy_assign_base : optional_move_base<T> {
  3648. using optional_move_base<T>::optional_move_base;
  3649. };
  3650. template <class T>
  3651. struct optional_copy_assign_base<T, false> : optional_move_base<T> {
  3652. using optional_move_base<T>::optional_move_base;
  3653. optional_copy_assign_base() = default;
  3654. optional_copy_assign_base(const optional_copy_assign_base& rhs) = default;
  3655. optional_copy_assign_base(optional_copy_assign_base&& rhs) = default;
  3656. optional_copy_assign_base& operator=(const optional_copy_assign_base& rhs) {
  3657. this->assign(rhs);
  3658. return *this;
  3659. }
  3660. optional_copy_assign_base& operator=(optional_copy_assign_base&& rhs) = default;
  3661. };
  3662. #ifndef SOL_TL_OPTIONAL_GCC49
  3663. template <class T,
  3664. bool = std::is_trivially_destructible<T>::value&& std::is_trivially_move_constructible<T>::value&& std::is_trivially_move_assignable<T>::value>
  3665. struct optional_move_assign_base : optional_copy_assign_base<T> {
  3666. using optional_copy_assign_base<T>::optional_copy_assign_base;
  3667. };
  3668. #else
  3669. template <class T, bool = false>
  3670. struct optional_move_assign_base;
  3671. #endif
  3672. template <class T>
  3673. struct optional_move_assign_base<T, false> : optional_copy_assign_base<T> {
  3674. using optional_copy_assign_base<T>::optional_copy_assign_base;
  3675. optional_move_assign_base() = default;
  3676. optional_move_assign_base(const optional_move_assign_base& rhs) = default;
  3677. optional_move_assign_base(optional_move_assign_base&& rhs) = default;
  3678. optional_move_assign_base& operator=(const optional_move_assign_base& rhs) = default;
  3679. optional_move_assign_base& operator=(optional_move_assign_base&& rhs) noexcept(
  3680. std::is_nothrow_move_constructible<T>::value&& std::is_nothrow_move_assignable<T>::value) {
  3681. this->assign(std::move(rhs));
  3682. return *this;
  3683. }
  3684. };
  3685. // optional_delete_ctor_base will conditionally delete copy and move
  3686. // constructors depending on whether T is copy/move constructible
  3687. template <class T, bool EnableCopy = std::is_copy_constructible<T>::value, bool EnableMove = std::is_move_constructible<T>::value>
  3688. struct optional_delete_ctor_base {
  3689. optional_delete_ctor_base() = default;
  3690. optional_delete_ctor_base(const optional_delete_ctor_base&) = default;
  3691. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = default;
  3692. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  3693. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  3694. };
  3695. template <class T>
  3696. struct optional_delete_ctor_base<T, true, false> {
  3697. optional_delete_ctor_base() = default;
  3698. optional_delete_ctor_base(const optional_delete_ctor_base&) = default;
  3699. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = delete;
  3700. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  3701. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  3702. };
  3703. template <class T>
  3704. struct optional_delete_ctor_base<T, false, true> {
  3705. optional_delete_ctor_base() = default;
  3706. optional_delete_ctor_base(const optional_delete_ctor_base&) = delete;
  3707. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = default;
  3708. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  3709. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  3710. };
  3711. template <class T>
  3712. struct optional_delete_ctor_base<T, false, false> {
  3713. optional_delete_ctor_base() = default;
  3714. optional_delete_ctor_base(const optional_delete_ctor_base&) = delete;
  3715. optional_delete_ctor_base(optional_delete_ctor_base&&) noexcept = delete;
  3716. optional_delete_ctor_base& operator=(const optional_delete_ctor_base&) = default;
  3717. optional_delete_ctor_base& operator=(optional_delete_ctor_base&&) noexcept = default;
  3718. };
  3719. // optional_delete_assign_base will conditionally delete copy and move
  3720. // constructors depending on whether T is copy/move constructible + assignable
  3721. template <class T, bool EnableCopy = (std::is_copy_constructible<T>::value && std::is_copy_assignable<T>::value),
  3722. bool EnableMove = (std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)>
  3723. struct optional_delete_assign_base {
  3724. optional_delete_assign_base() = default;
  3725. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  3726. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  3727. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = default;
  3728. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = default;
  3729. };
  3730. template <class T>
  3731. struct optional_delete_assign_base<T, true, false> {
  3732. optional_delete_assign_base() = default;
  3733. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  3734. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  3735. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = default;
  3736. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = delete;
  3737. };
  3738. template <class T>
  3739. struct optional_delete_assign_base<T, false, true> {
  3740. optional_delete_assign_base() = default;
  3741. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  3742. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  3743. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = delete;
  3744. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = default;
  3745. };
  3746. template <class T>
  3747. struct optional_delete_assign_base<T, false, false> {
  3748. optional_delete_assign_base() = default;
  3749. optional_delete_assign_base(const optional_delete_assign_base&) = default;
  3750. optional_delete_assign_base(optional_delete_assign_base&&) noexcept = default;
  3751. optional_delete_assign_base& operator=(const optional_delete_assign_base&) = delete;
  3752. optional_delete_assign_base& operator=(optional_delete_assign_base&&) noexcept = delete;
  3753. };
  3754. } // namespace detail
  3755. /// \brief A tag type to represent an empty optional
  3756. using nullopt_t = std::nullopt_t;
  3757. /// \brief Represents an empty optional
  3758. /// \synopsis static constexpr nullopt_t nullopt;
  3759. ///
  3760. /// *Examples*:
  3761. /// ```
  3762. /// sol::optional<int> a = sol::nullopt;
  3763. /// void foo (sol::optional<int>);
  3764. /// foo(sol::nullopt); //pass an empty optional
  3765. /// ```
  3766. using std::nullopt;
  3767. class bad_optional_access : public std::exception {
  3768. public:
  3769. bad_optional_access() = default;
  3770. const char* what() const noexcept {
  3771. return "Optional has no value";
  3772. }
  3773. };
  3774. /// An optional object is an object that contains the storage for another
  3775. /// object and manages the lifetime of this contained object, if any. The
  3776. /// contained object may be initialized after the optional object has been
  3777. /// initialized, and may be destroyed before the optional object has been
  3778. /// destroyed. The initialization state of the contained object is tracked by
  3779. /// the optional object.
  3780. template <class T>
  3781. class optional : private detail::optional_move_assign_base<T>,
  3782. private detail::optional_delete_ctor_base<T>,
  3783. private detail::optional_delete_assign_base<T> {
  3784. using base = detail::optional_move_assign_base<T>;
  3785. static_assert(!std::is_same<T, in_place_t>::value, "instantiation of optional with in_place_t is ill-formed");
  3786. static_assert(!std::is_same<detail::decay_t<T>, nullopt_t>::value, "instantiation of optional with nullopt_t is ill-formed");
  3787. public:
  3788. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  3789. /// \group and_then
  3790. /// Carries out some operation which returns an optional on the stored
  3791. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  3792. /// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
  3793. /// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
  3794. /// `std::optional<U>`. The return value is empty if `*this` is empty,
  3795. /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
  3796. /// is returned.
  3797. /// \group and_then
  3798. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &;
  3799. template <class F>
  3800. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) & {
  3801. using result = detail::invoke_result_t<F, T&>;
  3802. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3803. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  3804. }
  3805. /// \group and_then
  3806. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &&;
  3807. template <class F>
  3808. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) && {
  3809. using result = detail::invoke_result_t<F, T&&>;
  3810. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3811. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  3812. }
  3813. /// \group and_then
  3814. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &;
  3815. template <class F>
  3816. constexpr auto and_then(F&& f) const& {
  3817. using result = detail::invoke_result_t<F, const T&>;
  3818. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3819. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  3820. }
  3821. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  3822. /// \group and_then
  3823. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &&;
  3824. template <class F>
  3825. constexpr auto and_then(F&& f) const&& {
  3826. using result = detail::invoke_result_t<F, const T&&>;
  3827. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3828. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  3829. }
  3830. #endif
  3831. #else
  3832. /// \group and_then
  3833. /// Carries out some operation which returns an optional on the stored
  3834. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  3835. /// value())` returns a `std::optional<U>` for some `U`.
  3836. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  3837. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  3838. /// `*this` is empty, otherwise the return value of
  3839. /// `std::invoke(std::forward<F>(f), value())` is returned.
  3840. /// \group and_then
  3841. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &;
  3842. template <class F>
  3843. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) & {
  3844. using result = detail::invoke_result_t<F, T&>;
  3845. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3846. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  3847. }
  3848. /// \group and_then
  3849. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &&;
  3850. template <class F>
  3851. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&&> and_then(F&& f) && {
  3852. using result = detail::invoke_result_t<F, T&&>;
  3853. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3854. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  3855. }
  3856. /// \group and_then
  3857. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &;
  3858. template <class F>
  3859. constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const& {
  3860. using result = detail::invoke_result_t<F, const T&>;
  3861. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3862. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  3863. }
  3864. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  3865. /// \group and_then
  3866. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &&;
  3867. template <class F>
  3868. constexpr detail::invoke_result_t<F, const T&&> and_then(F&& f) const&& {
  3869. using result = detail::invoke_result_t<F, const T&&>;
  3870. static_assert(detail::is_optional<result>::value, "F must return an optional");
  3871. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : result(nullopt);
  3872. }
  3873. #endif
  3874. #endif
  3875. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  3876. /// \brief Carries out some operation on the stored object if there is one.
  3877. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  3878. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  3879. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  3880. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  3881. /// returned.
  3882. ///
  3883. /// \group map
  3884. /// \synopsis template <class F> constexpr auto map(F &&f) &;
  3885. template <class F>
  3886. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) & {
  3887. return optional_map_impl(*this, std::forward<F>(f));
  3888. }
  3889. /// \group map
  3890. /// \synopsis template <class F> constexpr auto map(F &&f) &&;
  3891. template <class F>
  3892. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) && {
  3893. return optional_map_impl(std::move(*this), std::forward<F>(f));
  3894. }
  3895. /// \group map
  3896. /// \synopsis template <class F> constexpr auto map(F &&f) const&;
  3897. template <class F>
  3898. constexpr auto map(F&& f) const& {
  3899. return optional_map_impl(*this, std::forward<F>(f));
  3900. }
  3901. /// \group map
  3902. /// \synopsis template <class F> constexpr auto map(F &&f) const&&;
  3903. template <class F>
  3904. constexpr auto map(F&& f) const&& {
  3905. return optional_map_impl(std::move(*this), std::forward<F>(f));
  3906. }
  3907. #else
  3908. /// \brief Carries out some operation on the stored object if there is one.
  3909. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  3910. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  3911. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  3912. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  3913. /// returned.
  3914. ///
  3915. /// \group map
  3916. /// \synopsis template <class F> auto map(F &&f) &;
  3917. template <class F>
  3918. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&>(), std::declval<F&&>())) map(F&& f) & {
  3919. return optional_map_impl(*this, std::forward<F>(f));
  3920. }
  3921. /// \group map
  3922. /// \synopsis template <class F> auto map(F &&f) &&;
  3923. template <class F>
  3924. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&&>(), std::declval<F&&>())) map(F&& f) && {
  3925. return optional_map_impl(std::move(*this), std::forward<F>(f));
  3926. }
  3927. /// \group map
  3928. /// \synopsis template <class F> auto map(F &&f) const&;
  3929. template <class F>
  3930. constexpr decltype(optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) map(F&& f) const& {
  3931. return optional_map_impl(*this, std::forward<F>(f));
  3932. }
  3933. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  3934. /// \group map
  3935. /// \synopsis template <class F> auto map(F &&f) const&&;
  3936. template <class F>
  3937. constexpr decltype(optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) map(F&& f) const&& {
  3938. return optional_map_impl(std::move(*this), std::forward<F>(f));
  3939. }
  3940. #endif
  3941. #endif
  3942. /// \brief Calls `f` if the optional is empty
  3943. /// \requires `std::invoke_result_t<F>` must be void or convertible to
  3944. /// `optional<T>`.
  3945. /// \effects If `*this` has a value, returns `*this`.
  3946. /// Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns
  3947. /// `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`.
  3948. ///
  3949. /// \group or_else
  3950. /// \synopsis template <class F> optional<T> or_else (F &&f) &;
  3951. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  3952. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  3953. if (has_value())
  3954. return *this;
  3955. std::forward<F>(f)();
  3956. return nullopt;
  3957. }
  3958. /// \exclude
  3959. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  3960. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  3961. return has_value() ? *this : std::forward<F>(f)();
  3962. }
  3963. /// \group or_else
  3964. /// \synopsis template <class F> optional<T> or_else (F &&f) &&;
  3965. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  3966. optional<T> or_else(F&& f) && {
  3967. if (has_value())
  3968. return std::move(*this);
  3969. std::forward<F>(f)();
  3970. return nullopt;
  3971. }
  3972. /// \exclude
  3973. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  3974. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) && {
  3975. return has_value() ? std::move(*this) : std::forward<F>(f)();
  3976. }
  3977. /// \group or_else
  3978. /// \synopsis template <class F> optional<T> or_else (F &&f) const &;
  3979. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  3980. optional<T> or_else(F&& f) const& {
  3981. if (has_value())
  3982. return *this;
  3983. std::forward<F>(f)();
  3984. return nullopt;
  3985. }
  3986. /// \exclude
  3987. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  3988. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) const& {
  3989. return has_value() ? *this : std::forward<F>(f)();
  3990. }
  3991. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  3992. /// \exclude
  3993. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  3994. optional<T> or_else(F&& f) const&& {
  3995. if (has_value())
  3996. return std::move(*this);
  3997. std::forward<F>(f)();
  3998. return nullopt;
  3999. }
  4000. /// \exclude
  4001. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  4002. optional<T> or_else(F&& f) const&& {
  4003. return has_value() ? std::move(*this) : std::forward<F>(f)();
  4004. }
  4005. #endif
  4006. /// \brief Maps the stored value with `f` if there is one, otherwise returns
  4007. /// `u`.
  4008. ///
  4009. /// \details If there is a value stored, then `f` is called with `**this`
  4010. /// and the value is returned. Otherwise `u` is returned.
  4011. ///
  4012. /// \group map_or
  4013. template <class F, class U>
  4014. U map_or(F&& f, U&& u) & {
  4015. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  4016. }
  4017. /// \group map_or
  4018. template <class F, class U>
  4019. U map_or(F&& f, U&& u) && {
  4020. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  4021. }
  4022. /// \group map_or
  4023. template <class F, class U>
  4024. U map_or(F&& f, U&& u) const& {
  4025. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  4026. }
  4027. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4028. /// \group map_or
  4029. template <class F, class U>
  4030. U map_or(F&& f, U&& u) const&& {
  4031. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  4032. }
  4033. #endif
  4034. /// \brief Maps the stored value with `f` if there is one, otherwise calls
  4035. /// `u` and returns the result.
  4036. ///
  4037. /// \details If there is a value stored, then `f` is
  4038. /// called with `**this` and the value is returned. Otherwise
  4039. /// `std::forward<U>(u)()` is returned.
  4040. ///
  4041. /// \group map_or_else
  4042. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) &;
  4043. template <class F, class U>
  4044. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) & {
  4045. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  4046. }
  4047. /// \group map_or_else
  4048. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
  4049. /// &&;
  4050. template <class F, class U>
  4051. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) && {
  4052. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  4053. }
  4054. /// \group map_or_else
  4055. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
  4056. /// const &;
  4057. template <class F, class U>
  4058. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const& {
  4059. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  4060. }
  4061. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4062. /// \group map_or_else
  4063. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
  4064. /// const &&;
  4065. template <class F, class U>
  4066. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const&& {
  4067. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  4068. }
  4069. #endif
  4070. /// \returns `u` if `*this` has a value, otherwise an empty optional.
  4071. template <class U>
  4072. constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const {
  4073. using result = optional<detail::decay_t<U>>;
  4074. return has_value() ? result { u } : result { nullopt };
  4075. }
  4076. /// \returns `rhs` if `*this` is empty, otherwise the current value.
  4077. /// \group disjunction
  4078. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) & {
  4079. return has_value() ? *this : rhs;
  4080. }
  4081. /// \group disjunction
  4082. constexpr optional disjunction(const optional& rhs) const& {
  4083. return has_value() ? *this : rhs;
  4084. }
  4085. /// \group disjunction
  4086. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) && {
  4087. return has_value() ? std::move(*this) : rhs;
  4088. }
  4089. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4090. /// \group disjunction
  4091. constexpr optional disjunction(const optional& rhs) const&& {
  4092. return has_value() ? std::move(*this) : rhs;
  4093. }
  4094. #endif
  4095. /// \group disjunction
  4096. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) & {
  4097. return has_value() ? *this : std::move(rhs);
  4098. }
  4099. /// \group disjunction
  4100. constexpr optional disjunction(optional&& rhs) const& {
  4101. return has_value() ? *this : std::move(rhs);
  4102. }
  4103. /// \group disjunction
  4104. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) && {
  4105. return has_value() ? std::move(*this) : std::move(rhs);
  4106. }
  4107. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4108. /// \group disjunction
  4109. constexpr optional disjunction(optional&& rhs) const&& {
  4110. return has_value() ? std::move(*this) : std::move(rhs);
  4111. }
  4112. #endif
  4113. /// Takes the value out of the optional, leaving it empty
  4114. /// \group take
  4115. optional take() & {
  4116. optional ret = *this;
  4117. reset();
  4118. return ret;
  4119. }
  4120. /// \group take
  4121. optional take() const& {
  4122. optional ret = *this;
  4123. reset();
  4124. return ret;
  4125. }
  4126. /// \group take
  4127. optional take() && {
  4128. optional ret = std::move(*this);
  4129. reset();
  4130. return ret;
  4131. }
  4132. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4133. /// \group take
  4134. optional take() const&& {
  4135. optional ret = std::move(*this);
  4136. reset();
  4137. return ret;
  4138. }
  4139. #endif
  4140. using value_type = T;
  4141. /// Constructs an optional that does not contain a value.
  4142. /// \group ctor_empty
  4143. constexpr optional() noexcept = default;
  4144. /// \group ctor_empty
  4145. constexpr optional(nullopt_t) noexcept {
  4146. }
  4147. /// Copy constructor
  4148. ///
  4149. /// If `rhs` contains a value, the stored value is direct-initialized with
  4150. /// it. Otherwise, the constructed optional is empty.
  4151. SOL_TL_OPTIONAL_11_CONSTEXPR optional(const optional& rhs) = default;
  4152. /// Move constructor
  4153. ///
  4154. /// If `rhs` contains a value, the stored value is direct-initialized with
  4155. /// it. Otherwise, the constructed optional is empty.
  4156. SOL_TL_OPTIONAL_11_CONSTEXPR optional(optional&& rhs) = default;
  4157. /// Constructs the stored value in-place using the given arguments.
  4158. /// \group in_place
  4159. /// \synopsis template <class... Args> constexpr explicit optional(in_place_t, Args&&... args);
  4160. template <class... Args>
  4161. constexpr explicit optional(detail::enable_if_t<std::is_constructible<T, Args...>::value, in_place_t>, Args&&... args)
  4162. : base(in_place, std::forward<Args>(args)...) {
  4163. }
  4164. /// \group in_place
  4165. /// \synopsis template <class U, class... Args>\nconstexpr explicit optional(in_place_t, std::initializer_list<U>&, Args&&... args);
  4166. template <class U, class... Args>
  4167. SOL_TL_OPTIONAL_11_CONSTEXPR explicit optional(detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, in_place_t>,
  4168. std::initializer_list<U> il, Args&&... args) {
  4169. this->construct(il, std::forward<Args>(args)...);
  4170. }
  4171. #if 0 // SOL_MODIFICATION
  4172. /// Constructs the stored value with `u`.
  4173. /// \synopsis template <class U=T> constexpr optional(U &&u);
  4174. template <class U = T, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr, detail::enable_forward_value<T, U>* = nullptr>
  4175. constexpr optional(U&& u) : base(in_place, std::forward<U>(u)) {
  4176. }
  4177. /// \exclude
  4178. template <class U = T, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr, detail::enable_forward_value<T, U>* = nullptr>
  4179. constexpr explicit optional(U&& u) : base(in_place, std::forward<U>(u)) {
  4180. }
  4181. #else
  4182. /// Constructs the stored value with `u`.
  4183. /// \synopsis template <class U=T> constexpr optional(U &&u);
  4184. constexpr optional(T&& u) : base(in_place, std::move(u)) {
  4185. }
  4186. /// \exclude
  4187. constexpr optional(const T& u) : base(in_place, u) {
  4188. }
  4189. #endif // sol3 modification
  4190. /// Converting copy constructor.
  4191. /// \synopsis template <class U> optional(const optional<U> &rhs);
  4192. template <class U, detail::enable_from_other<T, U, const U&>* = nullptr, detail::enable_if_t<std::is_convertible<const U&, T>::value>* = nullptr>
  4193. optional(const optional<U>& rhs) {
  4194. if (rhs.has_value()) {
  4195. this->construct(*rhs);
  4196. }
  4197. }
  4198. /// \exclude
  4199. template <class U, detail::enable_from_other<T, U, const U&>* = nullptr, detail::enable_if_t<!std::is_convertible<const U&, T>::value>* = nullptr>
  4200. explicit optional(const optional<U>& rhs) {
  4201. if (rhs.has_value()) {
  4202. this->construct(*rhs);
  4203. }
  4204. }
  4205. /// Converting move constructor.
  4206. /// \synopsis template <class U> optional(optional<U> &&rhs);
  4207. template <class U, detail::enable_from_other<T, U, U&&>* = nullptr, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr>
  4208. optional(optional<U>&& rhs) {
  4209. if (rhs.has_value()) {
  4210. this->construct(std::move(*rhs));
  4211. }
  4212. }
  4213. /// \exclude
  4214. template <class U, detail::enable_from_other<T, U, U&&>* = nullptr, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr>
  4215. explicit optional(optional<U>&& rhs) {
  4216. this->construct(std::move(*rhs));
  4217. }
  4218. /// Destroys the stored value if there is one.
  4219. ~optional() = default;
  4220. /// Assignment to empty.
  4221. ///
  4222. /// Destroys the current value if there is one.
  4223. optional& operator=(nullopt_t) noexcept {
  4224. if (has_value()) {
  4225. this->m_value.~T();
  4226. this->m_has_value = false;
  4227. }
  4228. return *this;
  4229. }
  4230. /// Copy assignment.
  4231. ///
  4232. /// Copies the value from `rhs` if there is one. Otherwise resets the stored
  4233. /// value in `*this`.
  4234. optional& operator=(const optional& rhs) = default;
  4235. /// Move assignment.
  4236. ///
  4237. /// Moves the value from `rhs` if there is one. Otherwise resets the stored
  4238. /// value in `*this`.
  4239. optional& operator=(optional&& rhs) = default;
  4240. /// Assigns the stored value from `u`, destroying the old value if there was
  4241. /// one.
  4242. /// \synopsis optional &operator=(U &&u);
  4243. template <class U = T, detail::enable_assign_forward<T, U>* = nullptr>
  4244. optional& operator=(U&& u) {
  4245. if (has_value()) {
  4246. this->m_value = std::forward<U>(u);
  4247. }
  4248. else {
  4249. this->construct(std::forward<U>(u));
  4250. }
  4251. return *this;
  4252. }
  4253. /// Converting copy assignment operator.
  4254. ///
  4255. /// Copies the value from `rhs` if there is one. Otherwise resets the stored
  4256. /// value in `*this`.
  4257. /// \synopsis optional &operator=(const optional<U> & rhs);
  4258. template <class U, detail::enable_assign_from_other<T, U, const U&>* = nullptr>
  4259. optional& operator=(const optional<U>& rhs) {
  4260. if (has_value()) {
  4261. if (rhs.has_value()) {
  4262. this->m_value = *rhs;
  4263. }
  4264. else {
  4265. this->hard_reset();
  4266. }
  4267. }
  4268. if (rhs.has_value()) {
  4269. this->construct(*rhs);
  4270. }
  4271. return *this;
  4272. }
  4273. // TODO check exception guarantee
  4274. /// Converting move assignment operator.
  4275. ///
  4276. /// Moves the value from `rhs` if there is one. Otherwise resets the stored
  4277. /// value in `*this`.
  4278. /// \synopsis optional &operator=(optional<U> && rhs);
  4279. template <class U, detail::enable_assign_from_other<T, U, U>* = nullptr>
  4280. optional& operator=(optional<U>&& rhs) {
  4281. if (has_value()) {
  4282. if (rhs.has_value()) {
  4283. this->m_value = std::move(*rhs);
  4284. }
  4285. else {
  4286. this->hard_reset();
  4287. }
  4288. }
  4289. if (rhs.has_value()) {
  4290. this->construct(std::move(*rhs));
  4291. }
  4292. return *this;
  4293. }
  4294. /// Constructs the value in-place, destroying the current one if there is
  4295. /// one.
  4296. /// \group emplace
  4297. template <class... Args>
  4298. T& emplace(Args&&... args) {
  4299. static_assert(std::is_constructible<T, Args&&...>::value, "T must be constructible with Args");
  4300. *this = nullopt;
  4301. this->construct(std::forward<Args>(args)...);
  4302. return value();
  4303. }
  4304. /// \group emplace
  4305. /// \synopsis template <class U, class... Args>\nT& emplace(std::initializer_list<U> il, Args &&... args);
  4306. template <class U, class... Args>
  4307. detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, T&> emplace(std::initializer_list<U> il, Args&&... args) {
  4308. *this = nullopt;
  4309. this->construct(il, std::forward<Args>(args)...);
  4310. return value();
  4311. }
  4312. /// Swaps this optional with the other.
  4313. ///
  4314. /// If neither optionals have a value, nothing happens.
  4315. /// If both have a value, the values are swapped.
  4316. /// If one has a value, it is moved to the other and the movee is left
  4317. /// valueless.
  4318. void swap(optional& rhs) noexcept(std::is_nothrow_move_constructible<T>::value&& detail::is_nothrow_swappable<T>::value) {
  4319. if (has_value()) {
  4320. if (rhs.has_value()) {
  4321. using std::swap;
  4322. swap(**this, *rhs);
  4323. }
  4324. else {
  4325. new (std::addressof(rhs.m_value)) T(std::move(this->m_value));
  4326. this->m_value.T::~T();
  4327. }
  4328. }
  4329. else if (rhs.has_value()) {
  4330. new (std::addressof(this->m_value)) T(std::move(rhs.m_value));
  4331. rhs.m_value.T::~T();
  4332. }
  4333. }
  4334. /// \returns a pointer to the stored value
  4335. /// \requires a value is stored
  4336. /// \group pointer
  4337. /// \synopsis constexpr const T *operator->() const;
  4338. constexpr const T* operator->() const {
  4339. return std::addressof(this->m_value);
  4340. }
  4341. /// \group pointer
  4342. /// \synopsis constexpr T *operator->();
  4343. SOL_TL_OPTIONAL_11_CONSTEXPR T* operator->() {
  4344. return std::addressof(this->m_value);
  4345. }
  4346. /// \returns the stored value
  4347. /// \requires a value is stored
  4348. /// \group deref
  4349. /// \synopsis constexpr T &operator*();
  4350. SOL_TL_OPTIONAL_11_CONSTEXPR T& operator*() & {
  4351. return this->m_value;
  4352. }
  4353. /// \group deref
  4354. /// \synopsis constexpr const T &operator*() const;
  4355. constexpr const T& operator*() const& {
  4356. return this->m_value;
  4357. }
  4358. /// \exclude
  4359. SOL_TL_OPTIONAL_11_CONSTEXPR T&& operator*() && {
  4360. return std::move(this->m_value);
  4361. }
  4362. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4363. /// \exclude
  4364. constexpr const T&& operator*() const&& {
  4365. return std::move(this->m_value);
  4366. }
  4367. #endif
  4368. /// \returns whether or not the optional has a value
  4369. /// \group has_value
  4370. constexpr bool has_value() const noexcept {
  4371. return this->m_has_value;
  4372. }
  4373. /// \group has_value
  4374. constexpr explicit operator bool() const noexcept {
  4375. return this->m_has_value;
  4376. }
  4377. /// \returns the contained value if there is one, otherwise throws
  4378. /// [bad_optional_access]
  4379. /// \group value
  4380. /// \synopsis constexpr T &value();
  4381. SOL_TL_OPTIONAL_11_CONSTEXPR T& value() & {
  4382. if (has_value())
  4383. return this->m_value;
  4384. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  4385. std::abort();
  4386. #else
  4387. throw bad_optional_access();
  4388. #endif // No exceptions allowed
  4389. }
  4390. /// \group value
  4391. /// \synopsis constexpr const T &value() const;
  4392. SOL_TL_OPTIONAL_11_CONSTEXPR const T& value() const& {
  4393. if (has_value())
  4394. return this->m_value;
  4395. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  4396. std::abort();
  4397. #else
  4398. throw bad_optional_access();
  4399. #endif // No exceptions allowed
  4400. }
  4401. /// \exclude
  4402. SOL_TL_OPTIONAL_11_CONSTEXPR T&& value() && {
  4403. if (has_value())
  4404. return std::move(this->m_value);
  4405. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  4406. std::abort();
  4407. #else
  4408. throw bad_optional_access();
  4409. #endif // No exceptions allowed
  4410. }
  4411. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4412. /// \exclude
  4413. SOL_TL_OPTIONAL_11_CONSTEXPR const T&& value() const&& {
  4414. if (has_value())
  4415. return std::move(this->m_value);
  4416. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  4417. std::abort();
  4418. #else
  4419. throw bad_optional_access();
  4420. #endif // No exceptions allowed
  4421. }
  4422. #endif
  4423. /// \returns the stored value if there is one, otherwise returns `u`
  4424. /// \group value_or
  4425. template <class U>
  4426. constexpr T value_or(U&& u) const& {
  4427. static_assert(std::is_copy_constructible<T>::value && std::is_convertible<U&&, T>::value, "T must be copy constructible and convertible from U");
  4428. return has_value() ? **this : static_cast<T>(std::forward<U>(u));
  4429. }
  4430. /// \group value_or
  4431. template <class U>
  4432. SOL_TL_OPTIONAL_11_CONSTEXPR T value_or(U&& u) && {
  4433. static_assert(std::is_move_constructible<T>::value && std::is_convertible<U&&, T>::value, "T must be move constructible and convertible from U");
  4434. return has_value() ? **this : static_cast<T>(std::forward<U>(u));
  4435. }
  4436. /// Destroys the stored value if one exists, making the optional empty
  4437. void reset() noexcept {
  4438. if (has_value()) {
  4439. this->m_value.~T();
  4440. this->m_has_value = false;
  4441. }
  4442. }
  4443. }; // namespace sol
  4444. /// \group relop
  4445. /// \brief Compares two optional objects
  4446. /// \details If both optionals contain a value, they are compared with `T`s
  4447. /// relational operators. Otherwise `lhs` and `rhs` are equal only if they are
  4448. /// both empty, and `lhs` is less than `rhs` only if `rhs` is empty and `lhs`
  4449. /// is not.
  4450. template <class T, class U>
  4451. inline constexpr bool operator==(const optional<T>& lhs, const optional<U>& rhs) {
  4452. return lhs.has_value() == rhs.has_value() && (!lhs.has_value() || *lhs == *rhs);
  4453. }
  4454. /// \group relop
  4455. template <class T, class U>
  4456. inline constexpr bool operator!=(const optional<T>& lhs, const optional<U>& rhs) {
  4457. return lhs.has_value() != rhs.has_value() || (lhs.has_value() && *lhs != *rhs);
  4458. }
  4459. /// \group relop
  4460. template <class T, class U>
  4461. inline constexpr bool operator<(const optional<T>& lhs, const optional<U>& rhs) {
  4462. return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs);
  4463. }
  4464. /// \group relop
  4465. template <class T, class U>
  4466. inline constexpr bool operator>(const optional<T>& lhs, const optional<U>& rhs) {
  4467. return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs);
  4468. }
  4469. /// \group relop
  4470. template <class T, class U>
  4471. inline constexpr bool operator<=(const optional<T>& lhs, const optional<U>& rhs) {
  4472. return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs);
  4473. }
  4474. /// \group relop
  4475. template <class T, class U>
  4476. inline constexpr bool operator>=(const optional<T>& lhs, const optional<U>& rhs) {
  4477. return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs);
  4478. }
  4479. /// \group relop_nullopt
  4480. /// \brief Compares an optional to a `nullopt`
  4481. /// \details Equivalent to comparing the optional to an empty optional
  4482. template <class T>
  4483. inline constexpr bool operator==(const optional<T>& lhs, nullopt_t) noexcept {
  4484. return !lhs.has_value();
  4485. }
  4486. /// \group relop_nullopt
  4487. template <class T>
  4488. inline constexpr bool operator==(nullopt_t, const optional<T>& rhs) noexcept {
  4489. return !rhs.has_value();
  4490. }
  4491. /// \group relop_nullopt
  4492. template <class T>
  4493. inline constexpr bool operator!=(const optional<T>& lhs, nullopt_t) noexcept {
  4494. return lhs.has_value();
  4495. }
  4496. /// \group relop_nullopt
  4497. template <class T>
  4498. inline constexpr bool operator!=(nullopt_t, const optional<T>& rhs) noexcept {
  4499. return rhs.has_value();
  4500. }
  4501. /// \group relop_nullopt
  4502. template <class T>
  4503. inline constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
  4504. return false;
  4505. }
  4506. /// \group relop_nullopt
  4507. template <class T>
  4508. inline constexpr bool operator<(nullopt_t, const optional<T>& rhs) noexcept {
  4509. return rhs.has_value();
  4510. }
  4511. /// \group relop_nullopt
  4512. template <class T>
  4513. inline constexpr bool operator<=(const optional<T>& lhs, nullopt_t) noexcept {
  4514. return !lhs.has_value();
  4515. }
  4516. /// \group relop_nullopt
  4517. template <class T>
  4518. inline constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
  4519. return true;
  4520. }
  4521. /// \group relop_nullopt
  4522. template <class T>
  4523. inline constexpr bool operator>(const optional<T>& lhs, nullopt_t) noexcept {
  4524. return lhs.has_value();
  4525. }
  4526. /// \group relop_nullopt
  4527. template <class T>
  4528. inline constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
  4529. return false;
  4530. }
  4531. /// \group relop_nullopt
  4532. template <class T>
  4533. inline constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
  4534. return true;
  4535. }
  4536. /// \group relop_nullopt
  4537. template <class T>
  4538. inline constexpr bool operator>=(nullopt_t, const optional<T>& rhs) noexcept {
  4539. return !rhs.has_value();
  4540. }
  4541. /// \group relop_t
  4542. /// \brief Compares the optional with a value.
  4543. /// \details If the optional has a value, it is compared with the other value
  4544. /// using `T`s relational operators. Otherwise, the optional is considered
  4545. /// less than the value.
  4546. template <class T, class U>
  4547. inline constexpr bool operator==(const optional<T>& lhs, const U& rhs) {
  4548. return lhs.has_value() ? *lhs == rhs : false;
  4549. }
  4550. /// \group relop_t
  4551. template <class T, class U>
  4552. inline constexpr bool operator==(const U& lhs, const optional<T>& rhs) {
  4553. return rhs.has_value() ? lhs == *rhs : false;
  4554. }
  4555. /// \group relop_t
  4556. template <class T, class U>
  4557. inline constexpr bool operator!=(const optional<T>& lhs, const U& rhs) {
  4558. return lhs.has_value() ? *lhs != rhs : true;
  4559. }
  4560. /// \group relop_t
  4561. template <class T, class U>
  4562. inline constexpr bool operator!=(const U& lhs, const optional<T>& rhs) {
  4563. return rhs.has_value() ? lhs != *rhs : true;
  4564. }
  4565. /// \group relop_t
  4566. template <class T, class U>
  4567. inline constexpr bool operator<(const optional<T>& lhs, const U& rhs) {
  4568. return lhs.has_value() ? *lhs < rhs : true;
  4569. }
  4570. /// \group relop_t
  4571. template <class T, class U>
  4572. inline constexpr bool operator<(const U& lhs, const optional<T>& rhs) {
  4573. return rhs.has_value() ? lhs < *rhs : false;
  4574. }
  4575. /// \group relop_t
  4576. template <class T, class U>
  4577. inline constexpr bool operator<=(const optional<T>& lhs, const U& rhs) {
  4578. return lhs.has_value() ? *lhs <= rhs : true;
  4579. }
  4580. /// \group relop_t
  4581. template <class T, class U>
  4582. inline constexpr bool operator<=(const U& lhs, const optional<T>& rhs) {
  4583. return rhs.has_value() ? lhs <= *rhs : false;
  4584. }
  4585. /// \group relop_t
  4586. template <class T, class U>
  4587. inline constexpr bool operator>(const optional<T>& lhs, const U& rhs) {
  4588. return lhs.has_value() ? *lhs > rhs : false;
  4589. }
  4590. /// \group relop_t
  4591. template <class T, class U>
  4592. inline constexpr bool operator>(const U& lhs, const optional<T>& rhs) {
  4593. return rhs.has_value() ? lhs > *rhs : true;
  4594. }
  4595. /// \group relop_t
  4596. template <class T, class U>
  4597. inline constexpr bool operator>=(const optional<T>& lhs, const U& rhs) {
  4598. return lhs.has_value() ? *lhs >= rhs : false;
  4599. }
  4600. /// \group relop_t
  4601. template <class T, class U>
  4602. inline constexpr bool operator>=(const U& lhs, const optional<T>& rhs) {
  4603. return rhs.has_value() ? lhs >= *rhs : true;
  4604. }
  4605. /// \synopsis template <class T>\nvoid swap(optional<T> &lhs, optional<T> &rhs);
  4606. template <class T, detail::enable_if_t<std::is_move_constructible<T>::value>* = nullptr, detail::enable_if_t<detail::is_swappable<T>::value>* = nullptr>
  4607. void swap(optional<T>& lhs, optional<T>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
  4608. return lhs.swap(rhs);
  4609. }
  4610. namespace detail {
  4611. struct i_am_secret {};
  4612. } // namespace detail
  4613. template <class T = detail::i_am_secret, class U, class Ret = detail::conditional_t<std::is_same<T, detail::i_am_secret>::value, detail::decay_t<U>, T>>
  4614. inline constexpr optional<Ret> make_optional(U&& v) {
  4615. return optional<Ret>(std::forward<U>(v));
  4616. }
  4617. template <class T, class... Args>
  4618. inline constexpr optional<T> make_optional(Args&&... args) {
  4619. return optional<T>(in_place, std::forward<Args>(args)...);
  4620. }
  4621. template <class T, class U, class... Args>
  4622. inline constexpr optional<T> make_optional(std::initializer_list<U> il, Args&&... args) {
  4623. return optional<T>(in_place, il, std::forward<Args>(args)...);
  4624. }
  4625. #if __cplusplus >= 201703L
  4626. template <class T>
  4627. optional(T)->optional<T>;
  4628. #endif
  4629. /// \exclude
  4630. namespace detail {
  4631. #ifdef SOL_TL_OPTIONAL_CXX14
  4632. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  4633. detail::enable_if_t<!std::is_void<Ret>::value>* = nullptr>
  4634. constexpr auto optional_map_impl(Opt&& opt, F&& f) {
  4635. return opt.has_value() ? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt)) : optional<Ret>(nullopt);
  4636. }
  4637. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  4638. detail::enable_if_t<std::is_void<Ret>::value>* = nullptr>
  4639. auto optional_map_impl(Opt&& opt, F&& f) {
  4640. if (opt.has_value()) {
  4641. detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
  4642. return make_optional(monostate {});
  4643. }
  4644. return optional<monostate>(nullopt);
  4645. }
  4646. #else
  4647. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  4648. detail::enable_if_t<!std::is_void<Ret>::value>* = nullptr>
  4649. constexpr auto optional_map_impl(Opt&& opt, F&& f) -> optional<Ret> {
  4650. return opt.has_value() ? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt)) : optional<Ret>(nullopt);
  4651. }
  4652. template <class Opt, class F, class Ret = decltype(detail::invoke(std::declval<F>(), *std::declval<Opt>())),
  4653. detail::enable_if_t<std::is_void<Ret>::value>* = nullptr>
  4654. auto optional_map_impl(Opt&& opt, F&& f) -> optional<monostate> {
  4655. if (opt.has_value()) {
  4656. detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
  4657. return monostate {};
  4658. }
  4659. return nullopt;
  4660. }
  4661. #endif
  4662. } // namespace detail
  4663. /// Specialization for when `T` is a reference. `optional<T&>` acts similarly
  4664. /// to a `T*`, but provides more operations and shows intent more clearly.
  4665. ///
  4666. /// *Examples*:
  4667. ///
  4668. /// ```
  4669. /// int i = 42;
  4670. /// sol::optional<int&> o = i;
  4671. /// *o == 42; //true
  4672. /// i = 12;
  4673. /// *o = 12; //true
  4674. /// &*o == &i; //true
  4675. /// ```
  4676. ///
  4677. /// Assignment has rebind semantics rather than assign-through semantics:
  4678. ///
  4679. /// ```
  4680. /// int j = 8;
  4681. /// o = j;
  4682. ///
  4683. /// &*o == &j; //true
  4684. /// ```
  4685. template <class T>
  4686. class optional<T&> {
  4687. public:
  4688. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  4689. /// \group and_then
  4690. /// Carries out some operation which returns an optional on the stored
  4691. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  4692. /// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
  4693. /// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
  4694. /// `std::optional<U>`. The return value is empty if `*this` is empty,
  4695. /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
  4696. /// is returned.
  4697. /// \group and_then
  4698. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &;
  4699. template <class F>
  4700. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) & {
  4701. using result = detail::invoke_result_t<F, T&>;
  4702. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4703. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4704. }
  4705. /// \group and_then
  4706. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &&;
  4707. template <class F>
  4708. SOL_TL_OPTIONAL_11_CONSTEXPR auto and_then(F&& f) && {
  4709. using result = detail::invoke_result_t<F, T&>;
  4710. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4711. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4712. }
  4713. /// \group and_then
  4714. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &;
  4715. template <class F>
  4716. constexpr auto and_then(F&& f) const& {
  4717. using result = detail::invoke_result_t<F, const T&>;
  4718. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4719. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4720. }
  4721. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4722. /// \group and_then
  4723. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &&;
  4724. template <class F>
  4725. constexpr auto and_then(F&& f) const&& {
  4726. using result = detail::invoke_result_t<F, const T&>;
  4727. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4728. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4729. }
  4730. #endif
  4731. #else
  4732. /// \group and_then
  4733. /// Carries out some operation which returns an optional on the stored
  4734. /// object if there is one. \requires `std::invoke(std::forward<F>(f),
  4735. /// value())` returns a `std::optional<U>` for some `U`. \returns Let `U` be
  4736. /// the result of `std::invoke(std::forward<F>(f), value())`. Returns a
  4737. /// `std::optional<U>`. The return value is empty if `*this` is empty,
  4738. /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
  4739. /// is returned.
  4740. /// \group and_then
  4741. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &;
  4742. template <class F>
  4743. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) & {
  4744. using result = detail::invoke_result_t<F, T&>;
  4745. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4746. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4747. }
  4748. /// \group and_then
  4749. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &&;
  4750. template <class F>
  4751. SOL_TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T&> and_then(F&& f) && {
  4752. using result = detail::invoke_result_t<F, T&>;
  4753. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4754. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4755. }
  4756. /// \group and_then
  4757. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &;
  4758. template <class F>
  4759. constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const& {
  4760. using result = detail::invoke_result_t<F, const T&>;
  4761. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4762. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4763. }
  4764. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4765. /// \group and_then
  4766. /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &&;
  4767. template <class F>
  4768. constexpr detail::invoke_result_t<F, const T&> and_then(F&& f) const&& {
  4769. using result = detail::invoke_result_t<F, const T&>;
  4770. static_assert(detail::is_optional<result>::value, "F must return an optional");
  4771. return has_value() ? detail::invoke(std::forward<F>(f), **this) : result(nullopt);
  4772. }
  4773. #endif
  4774. #endif
  4775. #if defined(SOL_TL_OPTIONAL_CXX14) && !defined(SOL_TL_OPTIONAL_GCC49) && !defined(SOL_TL_OPTIONAL_GCC54) && !defined(SOL_TL_OPTIONAL_GCC55)
  4776. /// \brief Carries out some operation on the stored object if there is one.
  4777. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  4778. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  4779. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  4780. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  4781. /// returned.
  4782. ///
  4783. /// \group map
  4784. /// \synopsis template <class F> constexpr auto map(F &&f) &;
  4785. template <class F>
  4786. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) & {
  4787. return detail::optional_map_impl(*this, std::forward<F>(f));
  4788. }
  4789. /// \group map
  4790. /// \synopsis template <class F> constexpr auto map(F &&f) &&;
  4791. template <class F>
  4792. SOL_TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) && {
  4793. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  4794. }
  4795. /// \group map
  4796. /// \synopsis template <class F> constexpr auto map(F &&f) const&;
  4797. template <class F>
  4798. constexpr auto map(F&& f) const& {
  4799. return detail::optional_map_impl(*this, std::forward<F>(f));
  4800. }
  4801. /// \group map
  4802. /// \synopsis template <class F> constexpr auto map(F &&f) const&&;
  4803. template <class F>
  4804. constexpr auto map(F&& f) const&& {
  4805. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  4806. }
  4807. #else
  4808. /// \brief Carries out some operation on the stored object if there is one.
  4809. /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
  4810. /// value())`. Returns a `std::optional<U>`. The return value is empty if
  4811. /// `*this` is empty, otherwise an `optional<U>` is constructed from the
  4812. /// return value of `std::invoke(std::forward<F>(f), value())` and is
  4813. /// returned.
  4814. ///
  4815. /// \group map
  4816. /// \synopsis template <class F> auto map(F &&f) &;
  4817. template <class F>
  4818. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval<optional&>(), std::declval<F&&>())) map(F&& f) & {
  4819. return detail::optional_map_impl(*this, std::forward<F>(f));
  4820. }
  4821. /// \group map
  4822. /// \synopsis template <class F> auto map(F &&f) &&;
  4823. template <class F>
  4824. SOL_TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval<optional&&>(), std::declval<F&&>())) map(F&& f) && {
  4825. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  4826. }
  4827. /// \group map
  4828. /// \synopsis template <class F> auto map(F &&f) const&;
  4829. template <class F>
  4830. constexpr decltype(detail::optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) map(F&& f) const& {
  4831. return detail::optional_map_impl(*this, std::forward<F>(f));
  4832. }
  4833. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4834. /// \group map
  4835. /// \synopsis template <class F> auto map(F &&f) const&&;
  4836. template <class F>
  4837. constexpr decltype(detail::optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) map(F&& f) const&& {
  4838. return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
  4839. }
  4840. #endif
  4841. #endif
  4842. /// \brief Calls `f` if the optional is empty
  4843. /// \requires `std::invoke_result_t<F>` must be void or convertible to
  4844. /// `optional<T>`. \effects If `*this` has a value, returns `*this`.
  4845. /// Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns
  4846. /// `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`.
  4847. ///
  4848. /// \group or_else
  4849. /// \synopsis template <class F> optional<T> or_else (F &&f) &;
  4850. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  4851. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  4852. if (has_value())
  4853. return *this;
  4854. std::forward<F>(f)();
  4855. return nullopt;
  4856. }
  4857. /// \exclude
  4858. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  4859. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) & {
  4860. return has_value() ? *this : std::forward<F>(f)();
  4861. }
  4862. /// \group or_else
  4863. /// \synopsis template <class F> optional<T> or_else (F &&f) &&;
  4864. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  4865. optional<T> or_else(F&& f) && {
  4866. if (has_value())
  4867. return std::move(*this);
  4868. std::forward<F>(f)();
  4869. return nullopt;
  4870. }
  4871. /// \exclude
  4872. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  4873. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) && {
  4874. return has_value() ? std::move(*this) : std::forward<F>(f)();
  4875. }
  4876. /// \group or_else
  4877. /// \synopsis template <class F> optional<T> or_else (F &&f) const &;
  4878. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  4879. optional<T> or_else(F&& f) const& {
  4880. if (has_value())
  4881. return *this;
  4882. std::forward<F>(f)();
  4883. return nullopt;
  4884. }
  4885. /// \exclude
  4886. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  4887. optional<T> SOL_TL_OPTIONAL_11_CONSTEXPR or_else(F&& f) const& {
  4888. return has_value() ? *this : std::forward<F>(f)();
  4889. }
  4890. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4891. /// \exclude
  4892. template <class F, detail::enable_if_ret_void<F>* = nullptr>
  4893. optional<T> or_else(F&& f) const&& {
  4894. if (has_value())
  4895. return std::move(*this);
  4896. std::forward<F>(f)();
  4897. return nullopt;
  4898. }
  4899. /// \exclude
  4900. template <class F, detail::disable_if_ret_void<F>* = nullptr>
  4901. optional<T> or_else(F&& f) const&& {
  4902. return has_value() ? std::move(*this) : std::forward<F>(f)();
  4903. }
  4904. #endif
  4905. /// \brief Maps the stored value with `f` if there is one, otherwise returns
  4906. /// `u`.
  4907. ///
  4908. /// \details If there is a value stored, then `f` is called with `**this`
  4909. /// and the value is returned. Otherwise `u` is returned.
  4910. ///
  4911. /// \group map_or
  4912. template <class F, class U>
  4913. U map_or(F&& f, U&& u) & {
  4914. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  4915. }
  4916. /// \group map_or
  4917. template <class F, class U>
  4918. U map_or(F&& f, U&& u) && {
  4919. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  4920. }
  4921. /// \group map_or
  4922. template <class F, class U>
  4923. U map_or(F&& f, U&& u) const& {
  4924. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u);
  4925. }
  4926. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4927. /// \group map_or
  4928. template <class F, class U>
  4929. U map_or(F&& f, U&& u) const&& {
  4930. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u);
  4931. }
  4932. #endif
  4933. /// \brief Maps the stored value with `f` if there is one, otherwise calls
  4934. /// `u` and returns the result.
  4935. ///
  4936. /// \details If there is a value stored, then `f` is
  4937. /// called with `**this` and the value is returned. Otherwise
  4938. /// `std::forward<U>(u)()` is returned.
  4939. ///
  4940. /// \group map_or_else
  4941. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) &;
  4942. template <class F, class U>
  4943. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) & {
  4944. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  4945. }
  4946. /// \group map_or_else
  4947. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
  4948. /// &&;
  4949. template <class F, class U>
  4950. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) && {
  4951. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  4952. }
  4953. /// \group map_or_else
  4954. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
  4955. /// const &;
  4956. template <class F, class U>
  4957. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const& {
  4958. return has_value() ? detail::invoke(std::forward<F>(f), **this) : std::forward<U>(u)();
  4959. }
  4960. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4961. /// \group map_or_else
  4962. /// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
  4963. /// const &&;
  4964. template <class F, class U>
  4965. detail::invoke_result_t<U> map_or_else(F&& f, U&& u) const&& {
  4966. return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this)) : std::forward<U>(u)();
  4967. }
  4968. #endif
  4969. /// \returns `u` if `*this` has a value, otherwise an empty optional.
  4970. template <class U>
  4971. constexpr optional<typename std::decay<U>::type> conjunction(U&& u) const {
  4972. using result = optional<detail::decay_t<U>>;
  4973. return has_value() ? result { u } : result { nullopt };
  4974. }
  4975. /// \returns `rhs` if `*this` is empty, otherwise the current value.
  4976. /// \group disjunction
  4977. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) & {
  4978. return has_value() ? *this : rhs;
  4979. }
  4980. /// \group disjunction
  4981. constexpr optional disjunction(const optional& rhs) const& {
  4982. return has_value() ? *this : rhs;
  4983. }
  4984. /// \group disjunction
  4985. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional& rhs) && {
  4986. return has_value() ? std::move(*this) : rhs;
  4987. }
  4988. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  4989. /// \group disjunction
  4990. constexpr optional disjunction(const optional& rhs) const&& {
  4991. return has_value() ? std::move(*this) : rhs;
  4992. }
  4993. #endif
  4994. /// \group disjunction
  4995. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) & {
  4996. return has_value() ? *this : std::move(rhs);
  4997. }
  4998. /// \group disjunction
  4999. constexpr optional disjunction(optional&& rhs) const& {
  5000. return has_value() ? *this : std::move(rhs);
  5001. }
  5002. /// \group disjunction
  5003. SOL_TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional&& rhs) && {
  5004. return has_value() ? std::move(*this) : std::move(rhs);
  5005. }
  5006. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  5007. /// \group disjunction
  5008. constexpr optional disjunction(optional&& rhs) const&& {
  5009. return has_value() ? std::move(*this) : std::move(rhs);
  5010. }
  5011. #endif
  5012. /// Takes the value out of the optional, leaving it empty
  5013. /// \group take
  5014. optional take() & {
  5015. optional ret = *this;
  5016. reset();
  5017. return ret;
  5018. }
  5019. /// \group take
  5020. optional take() const& {
  5021. optional ret = *this;
  5022. reset();
  5023. return ret;
  5024. }
  5025. /// \group take
  5026. optional take() && {
  5027. optional ret = std::move(*this);
  5028. reset();
  5029. return ret;
  5030. }
  5031. #ifndef SOL_TL_OPTIONAL_NO_CONSTRR
  5032. /// \group take
  5033. optional take() const&& {
  5034. optional ret = std::move(*this);
  5035. reset();
  5036. return ret;
  5037. }
  5038. #endif
  5039. using value_type = T&;
  5040. /// Constructs an optional that does not contain a value.
  5041. /// \group ctor_empty
  5042. constexpr optional() noexcept : m_value(nullptr) {
  5043. }
  5044. /// \group ctor_empty
  5045. constexpr optional(nullopt_t) noexcept : m_value(nullptr) {
  5046. }
  5047. /// Copy constructor
  5048. ///
  5049. /// If `rhs` contains a value, the stored value is direct-initialized with
  5050. /// it. Otherwise, the constructed optional is empty.
  5051. SOL_TL_OPTIONAL_11_CONSTEXPR optional(const optional& rhs) noexcept = default;
  5052. /// Move constructor
  5053. ///
  5054. /// If `rhs` contains a value, the stored value is direct-initialized with
  5055. /// it. Otherwise, the constructed optional is empty.
  5056. SOL_TL_OPTIONAL_11_CONSTEXPR optional(optional&& rhs) = default;
  5057. /// Constructs the stored value with `u`.
  5058. /// \synopsis template <class U=T> constexpr optional(U &&u);
  5059. template <class U = T, detail::enable_if_t<!detail::is_optional<detail::decay_t<U>>::value>* = nullptr>
  5060. constexpr optional(U&& u) : m_value(std::addressof(u)) {
  5061. static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
  5062. }
  5063. /// \exclude
  5064. template <class U>
  5065. constexpr explicit optional(const optional<U>& rhs) : optional(*rhs) {
  5066. }
  5067. /// No-op
  5068. ~optional() = default;
  5069. /// Assignment to empty.
  5070. ///
  5071. /// Destroys the current value if there is one.
  5072. optional& operator=(nullopt_t) noexcept {
  5073. m_value = nullptr;
  5074. return *this;
  5075. }
  5076. /// Copy assignment.
  5077. ///
  5078. /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise
  5079. /// resets the stored value in `*this`.
  5080. optional& operator=(const optional& rhs) = default;
  5081. /// Rebinds this optional to `u`.
  5082. ///
  5083. /// \requires `U` must be an lvalue reference.
  5084. /// \synopsis optional &operator=(U &&u);
  5085. template <class U = T, detail::enable_if_t<!detail::is_optional<detail::decay_t<U>>::value>* = nullptr>
  5086. optional& operator=(U&& u) {
  5087. static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
  5088. m_value = std::addressof(u);
  5089. return *this;
  5090. }
  5091. /// Converting copy assignment operator.
  5092. ///
  5093. /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise
  5094. /// resets the stored value in `*this`.
  5095. template <class U>
  5096. optional& operator=(const optional<U>& rhs) {
  5097. m_value = std::addressof(rhs.value());
  5098. return *this;
  5099. }
  5100. /// Constructs the value in-place, destroying the current one if there is
  5101. /// one.
  5102. ///
  5103. /// \group emplace
  5104. template <class... Args>
  5105. T& emplace(Args&&... args) noexcept {
  5106. static_assert(std::is_constructible<T, Args&&...>::value, "T must be constructible with Args");
  5107. *this = nullopt;
  5108. this->construct(std::forward<Args>(args)...);
  5109. }
  5110. /// Swaps this optional with the other.
  5111. ///
  5112. /// If neither optionals have a value, nothing happens.
  5113. /// If both have a value, the values are swapped.
  5114. /// If one has a value, it is moved to the other and the movee is left
  5115. /// valueless.
  5116. void swap(optional& rhs) noexcept {
  5117. std::swap(m_value, rhs.m_value);
  5118. }
  5119. /// \returns a pointer to the stored value
  5120. /// \requires a value is stored
  5121. /// \group pointer
  5122. /// \synopsis constexpr const T *operator->() const;
  5123. constexpr const T* operator->() const {
  5124. return m_value;
  5125. }
  5126. /// \group pointer
  5127. /// \synopsis constexpr T *operator->();
  5128. SOL_TL_OPTIONAL_11_CONSTEXPR T* operator->() {
  5129. return m_value;
  5130. }
  5131. /// \returns the stored value
  5132. /// \requires a value is stored
  5133. /// \group deref
  5134. /// \synopsis constexpr T &operator*();
  5135. SOL_TL_OPTIONAL_11_CONSTEXPR T& operator*() {
  5136. return *m_value;
  5137. }
  5138. /// \group deref
  5139. /// \synopsis constexpr const T &operator*() const;
  5140. constexpr const T& operator*() const {
  5141. return *m_value;
  5142. }
  5143. /// \returns whether or not the optional has a value
  5144. /// \group has_value
  5145. constexpr bool has_value() const noexcept {
  5146. return m_value != nullptr;
  5147. }
  5148. /// \group has_value
  5149. constexpr explicit operator bool() const noexcept {
  5150. return m_value != nullptr;
  5151. }
  5152. /// \returns the contained value if there is one, otherwise throws
  5153. /// [bad_optional_access]
  5154. /// \group value
  5155. /// synopsis constexpr T &value();
  5156. SOL_TL_OPTIONAL_11_CONSTEXPR T& value() {
  5157. if (has_value())
  5158. return *m_value;
  5159. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  5160. std::abort();
  5161. #else
  5162. throw bad_optional_access();
  5163. #endif // No exceptions allowed
  5164. }
  5165. /// \group value
  5166. /// \synopsis constexpr const T &value() const;
  5167. SOL_TL_OPTIONAL_11_CONSTEXPR const T& value() const {
  5168. if (has_value())
  5169. return *m_value;
  5170. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  5171. std::abort();
  5172. #else
  5173. throw bad_optional_access();
  5174. #endif // No exceptions allowed
  5175. }
  5176. /// \returns the stored value if there is one, otherwise returns `u`
  5177. /// \group value_or
  5178. template <class U>
  5179. constexpr T& value_or(U&& u) const {
  5180. static_assert(std::is_convertible<U&&, T&>::value, "T must be convertible from U");
  5181. return has_value() ? const_cast<T&>(**this) : static_cast<T&>(std::forward<U>(u));
  5182. }
  5183. /// Destroys the stored value if one exists, making the optional empty
  5184. void reset() noexcept {
  5185. m_value = nullptr;
  5186. }
  5187. private:
  5188. T* m_value;
  5189. };
  5190. } // namespace sol
  5191. namespace std {
  5192. // TODO SFINAE
  5193. template <class T>
  5194. struct hash<::sol::optional<T>> {
  5195. ::std::size_t operator()(const ::sol::optional<T>& o) const {
  5196. if (!o.has_value())
  5197. return 0;
  5198. return ::std::hash<::sol::detail::remove_const_t<T>>()(*o);
  5199. }
  5200. };
  5201. } // namespace std
  5202. // end of sol/optional_implementation.hpp
  5203. #endif // Boost vs. Better optional
  5204. #include <optional>
  5205. namespace sol {
  5206. #if SOL_IS_ON(SOL_USE_BOOST_I_)
  5207. template <typename T>
  5208. using optional = boost::optional<T>;
  5209. using nullopt_t = boost::none_t;
  5210. const nullopt_t nullopt = boost::none;
  5211. #endif // Boost vs. Better optional
  5212. namespace meta {
  5213. template <typename T>
  5214. using is_optional = any<is_specialization_of<T, optional>, is_specialization_of<T, std::optional>>;
  5215. template <typename T>
  5216. constexpr inline bool is_optional_v = is_optional<T>::value;
  5217. } // namespace meta
  5218. namespace detail {
  5219. template <typename T>
  5220. struct associated_nullopt {
  5221. inline static constexpr std::nullopt_t value = std::nullopt;
  5222. };
  5223. #if SOL_IS_ON(SOL_USE_BOOST_I_)
  5224. template <typename T>
  5225. struct associated_nullopt<boost::optional<T>> {
  5226. inline static constexpr std::nullopt_t value = boost::nullopt;
  5227. };
  5228. #endif // Boost nullopt
  5229. template <typename T>
  5230. inline constexpr auto associated_nullopt_v = associated_nullopt<T>::value;
  5231. } // namespace detail
  5232. } // namespace sol
  5233. // end of sol/optional.hpp
  5234. // beginning of sol/raii.hpp
  5235. #include <memory>
  5236. namespace sol {
  5237. namespace detail {
  5238. struct default_construct {
  5239. template <typename T, typename... Args>
  5240. static void construct(T&& obj, Args&&... args) {
  5241. typedef meta::unqualified_t<T> Tu;
  5242. std::allocator<Tu> alloc{};
  5243. std::allocator_traits<std::allocator<Tu>>::construct(alloc, std::forward<T>(obj), std::forward<Args>(args)...);
  5244. }
  5245. template <typename T, typename... Args>
  5246. void operator()(T&& obj, Args&&... args) const {
  5247. construct(std::forward<T>(obj), std::forward<Args>(args)...);
  5248. }
  5249. };
  5250. struct default_destruct {
  5251. template <typename T>
  5252. static void destroy(T&& obj) {
  5253. std::allocator<meta::unqualified_t<T>> alloc{};
  5254. alloc.destroy(obj);
  5255. }
  5256. template <typename T>
  5257. void operator()(T&& obj) const {
  5258. destroy(std::forward<T>(obj));
  5259. }
  5260. };
  5261. struct deleter {
  5262. template <typename T>
  5263. void operator()(T* p) const {
  5264. delete p;
  5265. }
  5266. };
  5267. struct state_deleter {
  5268. void operator()(lua_State* L) const {
  5269. lua_close(L);
  5270. }
  5271. };
  5272. template <typename T, typename Dx, typename... Args>
  5273. inline std::unique_ptr<T, Dx> make_unique_deleter(Args&&... args) {
  5274. return std::unique_ptr<T, Dx>(new T(std::forward<Args>(args)...));
  5275. }
  5276. template <typename Tag, typename T>
  5277. struct tagged {
  5278. private:
  5279. T value_;
  5280. public:
  5281. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, tagged>> = meta::enabler>
  5282. tagged(Arg&& arg, Args&&... args)
  5283. : value_(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  5284. }
  5285. T& value() & {
  5286. return value_;
  5287. }
  5288. T const& value() const& {
  5289. return value_;
  5290. }
  5291. T&& value() && {
  5292. return std::move(value_);
  5293. }
  5294. };
  5295. } // namespace detail
  5296. template <typename... Args>
  5297. struct constructor_list {};
  5298. template <typename... Args>
  5299. using constructors = constructor_list<Args...>;
  5300. const auto default_constructor = constructors<types<>>{};
  5301. struct no_construction {};
  5302. const auto no_constructor = no_construction{};
  5303. struct call_construction {};
  5304. const auto call_constructor = call_construction{};
  5305. template <typename... Functions>
  5306. struct constructor_wrapper {
  5307. std::tuple<Functions...> functions;
  5308. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, constructor_wrapper>> = meta::enabler>
  5309. constructor_wrapper(Arg&& arg, Args&&... args)
  5310. : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  5311. }
  5312. };
  5313. template <typename... Functions>
  5314. inline auto initializers(Functions&&... functions) {
  5315. return constructor_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
  5316. }
  5317. template <typename... Functions>
  5318. struct factory_wrapper {
  5319. std::tuple<Functions...> functions;
  5320. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, factory_wrapper>> = meta::enabler>
  5321. factory_wrapper(Arg&& arg, Args&&... args)
  5322. : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  5323. }
  5324. };
  5325. template <typename... Functions>
  5326. inline auto factories(Functions&&... functions) {
  5327. return factory_wrapper<std::decay_t<Functions>...>(std::forward<Functions>(functions)...);
  5328. }
  5329. template <typename Function>
  5330. struct destructor_wrapper {
  5331. Function fx;
  5332. destructor_wrapper(Function f)
  5333. : fx(std::move(f)) {
  5334. }
  5335. };
  5336. template <>
  5337. struct destructor_wrapper<void> {};
  5338. const destructor_wrapper<void> default_destructor{};
  5339. template <typename Fx>
  5340. inline auto destructor(Fx&& fx) {
  5341. return destructor_wrapper<std::decay_t<Fx>>(std::forward<Fx>(fx));
  5342. }
  5343. } // namespace sol
  5344. // end of sol/raii.hpp
  5345. // beginning of sol/policies.hpp
  5346. #include <array>
  5347. namespace sol {
  5348. namespace detail {
  5349. struct policy_base_tag {};
  5350. } // namespace detail
  5351. template <int Target, int... In>
  5352. struct static_stack_dependencies : detail::policy_base_tag {};
  5353. typedef static_stack_dependencies<-1, 1> self_dependency;
  5354. template <int... In>
  5355. struct returns_self_with : detail::policy_base_tag {};
  5356. typedef returns_self_with<> returns_self;
  5357. struct stack_dependencies : detail::policy_base_tag {
  5358. int target;
  5359. std::array<int, 64> stack_indices;
  5360. std::size_t len;
  5361. template <typename... Args>
  5362. stack_dependencies(int stack_target, Args&&... args) : target(stack_target), stack_indices(), len(sizeof...(Args)) {
  5363. std::size_t i = 0;
  5364. (void)detail::swallow{ int(), (stack_indices[i++] = static_cast<int>(std::forward<Args>(args)), int())... };
  5365. }
  5366. int& operator[](std::size_t i) {
  5367. return stack_indices[i];
  5368. }
  5369. const int& operator[](std::size_t i) const {
  5370. return stack_indices[i];
  5371. }
  5372. std::size_t size() const {
  5373. return len;
  5374. }
  5375. };
  5376. template <typename F, typename... Policies>
  5377. struct policy_wrapper {
  5378. typedef std::index_sequence_for<Policies...> indices;
  5379. F value;
  5380. std::tuple<Policies...> policies;
  5381. template <typename Fx, typename... Args, meta::enable<meta::neg<std::is_same<meta::unqualified_t<Fx>, policy_wrapper>>> = meta::enabler>
  5382. policy_wrapper(Fx&& fx, Args&&... args) : value(std::forward<Fx>(fx)), policies(std::forward<Args>(args)...) {
  5383. }
  5384. policy_wrapper(const policy_wrapper&) = default;
  5385. policy_wrapper& operator=(const policy_wrapper&) = default;
  5386. policy_wrapper(policy_wrapper&&) = default;
  5387. policy_wrapper& operator=(policy_wrapper&&) = default;
  5388. };
  5389. template <typename F, typename... Args>
  5390. auto policies(F&& f, Args&&... args) {
  5391. return policy_wrapper<std::decay_t<F>, std::decay_t<Args>...>(std::forward<F>(f), std::forward<Args>(args)...);
  5392. }
  5393. namespace detail {
  5394. template <typename T>
  5395. using is_policy = meta::is_specialization_of<T, policy_wrapper>;
  5396. template <typename T>
  5397. inline constexpr bool is_policy_v = is_policy<T>::value;
  5398. } // namespace detail
  5399. } // namespace sol
  5400. // end of sol/policies.hpp
  5401. // beginning of sol/ebco.hpp
  5402. #include <type_traits>
  5403. #include <utility>
  5404. namespace sol { namespace detail {
  5405. template <typename T, std::size_t tag = 0, typename = void>
  5406. struct ebco {
  5407. T value_;
  5408. ebco() = default;
  5409. ebco(const ebco&) = default;
  5410. ebco(ebco&&) = default;
  5411. ebco& operator=(const ebco&) = default;
  5412. ebco& operator=(ebco&&) = default;
  5413. ebco(const T& v) : value_(v){};
  5414. ebco(T&& v) : value_(std::move(v)){};
  5415. ebco& operator=(const T& v) {
  5416. value_ = v;
  5417. return *this;
  5418. }
  5419. ebco& operator=(T&& v) {
  5420. value_ = std::move(v);
  5421. return *this;
  5422. };
  5423. template <typename Arg, typename... Args,
  5424. typename = std::enable_if_t<!std::is_same_v<std::remove_reference_t<std::remove_cv_t<Arg>>,
  5425. ebco> && !std::is_same_v<std::remove_reference_t<std::remove_cv_t<Arg>>, T>>>
  5426. ebco(Arg&& arg, Args&&... args) : T(std::forward<Arg>(arg), std::forward<Args>(args)...){}
  5427. T& value() & {
  5428. return value_;
  5429. }
  5430. T const& value() const & {
  5431. return value_;
  5432. }
  5433. T&& value() && {
  5434. return std::move(value_);
  5435. }
  5436. };
  5437. template <typename T, std::size_t tag>
  5438. struct ebco<T, tag, std::enable_if_t<!std::is_reference_v<T> && std::is_class_v<T> && !std::is_final_v<T>>> : T {
  5439. ebco() = default;
  5440. ebco(const ebco&) = default;
  5441. ebco(ebco&&) = default;
  5442. ebco(const T& v) : T(v){};
  5443. ebco(T&& v) : T(std::move(v)){};
  5444. template <typename Arg, typename... Args,
  5445. typename = std::enable_if_t<!std::is_same_v<std::remove_reference_t<std::remove_cv_t<Arg>>,
  5446. ebco> && !std::is_same_v<std::remove_reference_t<std::remove_cv_t<Arg>>, T>>>
  5447. ebco(Arg&& arg, Args&&... args) : T(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  5448. }
  5449. ebco& operator=(const ebco&) = default;
  5450. ebco& operator=(ebco&&) = default;
  5451. ebco& operator=(const T& v) {
  5452. static_cast<T&>(*this) = v;
  5453. return *this;
  5454. }
  5455. ebco& operator=(T&& v) {
  5456. static_cast<T&>(*this) = std::move(v);
  5457. return *this;
  5458. };
  5459. T& value() & {
  5460. return static_cast<T&>(*this);
  5461. }
  5462. T const& value() const & {
  5463. return static_cast<T const&>(*this);
  5464. }
  5465. T&& value() && {
  5466. return std::move(static_cast<T&>(*this));
  5467. }
  5468. };
  5469. template <typename T, std::size_t tag>
  5470. struct ebco<T&, tag> {
  5471. T& ref;
  5472. ebco() = default;
  5473. ebco(const ebco&) = default;
  5474. ebco(ebco&&) = default;
  5475. ebco(T& v) : ref(v){};
  5476. ebco& operator=(const ebco&) = default;
  5477. ebco& operator=(ebco&&) = default;
  5478. ebco& operator=(T& v) {
  5479. ref = v;
  5480. return *this;
  5481. }
  5482. T& value() const {
  5483. return const_cast<ebco<T&, tag>&>(*this).ref;
  5484. }
  5485. };
  5486. template <typename T, std::size_t tag>
  5487. struct ebco<T&&, tag> {
  5488. T&& ref;
  5489. ebco() = default;
  5490. ebco(const ebco&) = default;
  5491. ebco(ebco&&) = default;
  5492. ebco(T&& v) : ref(v){};
  5493. ebco& operator=(const ebco&) = default;
  5494. ebco& operator=(ebco&&) = default;
  5495. ebco& operator=(T&& v) {
  5496. ref = std::move(v);
  5497. return *this;
  5498. }
  5499. T& value() & {
  5500. return ref;
  5501. }
  5502. const T& value() const & {
  5503. return ref;
  5504. }
  5505. T&& value() && {
  5506. return std::move(ref);
  5507. }
  5508. };
  5509. }} // namespace sol::detail
  5510. // end of sol/ebco.hpp
  5511. #include <array>
  5512. #include <initializer_list>
  5513. #include <string>
  5514. #include <string_view>
  5515. #include <optional>
  5516. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  5517. #include <variant>
  5518. #endif // variant shenanigans (thanks, Mac OSX)
  5519. namespace sol {
  5520. namespace detail {
  5521. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  5522. typedef int (*lua_CFunction_noexcept)(lua_State* L) noexcept;
  5523. #else
  5524. typedef int (*lua_CFunction_noexcept)(lua_State* L);
  5525. #endif // noexcept function type for lua_CFunction
  5526. template <typename T>
  5527. struct unique_usertype { };
  5528. template <typename T>
  5529. struct implicit_wrapper {
  5530. T& item;
  5531. implicit_wrapper(T* item) : item(*item) {
  5532. }
  5533. implicit_wrapper(T& item) : item(item) {
  5534. }
  5535. operator T&() {
  5536. return item;
  5537. }
  5538. operator T*() {
  5539. return std::addressof(item);
  5540. }
  5541. };
  5542. struct yield_tag_t { };
  5543. const yield_tag_t yield_tag = yield_tag_t {};
  5544. } // namespace detail
  5545. struct lua_nil_t { };
  5546. inline constexpr lua_nil_t lua_nil {};
  5547. inline bool operator==(lua_nil_t, lua_nil_t) {
  5548. return true;
  5549. }
  5550. inline bool operator!=(lua_nil_t, lua_nil_t) {
  5551. return false;
  5552. }
  5553. #if SOL_IS_ON(SOL_NIL_I_)
  5554. using nil_t = lua_nil_t;
  5555. inline constexpr const nil_t& nil = lua_nil;
  5556. #endif
  5557. namespace detail {
  5558. struct non_lua_nil_t { };
  5559. } // namespace detail
  5560. struct metatable_key_t { };
  5561. const metatable_key_t metatable_key = {};
  5562. struct env_key_t { };
  5563. const env_key_t env_key = {};
  5564. struct no_metatable_t { };
  5565. const no_metatable_t no_metatable = {};
  5566. template <typename T>
  5567. struct yielding_t {
  5568. T func;
  5569. yielding_t() = default;
  5570. yielding_t(const yielding_t&) = default;
  5571. yielding_t(yielding_t&&) = default;
  5572. yielding_t& operator=(const yielding_t&) = default;
  5573. yielding_t& operator=(yielding_t&&) = default;
  5574. template <typename Arg,
  5575. meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, yielding_t>>,
  5576. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  5577. yielding_t(Arg&& arg) : func(std::forward<Arg>(arg)) {
  5578. }
  5579. template <typename Arg0, typename Arg1, typename... Args>
  5580. yielding_t(Arg0&& arg0, Arg1&& arg1, Args&&... args) : func(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  5581. }
  5582. };
  5583. template <typename F>
  5584. inline yielding_t<std::decay_t<F>> yielding(F&& f) {
  5585. return yielding_t<std::decay_t<F>>(std::forward<F>(f));
  5586. }
  5587. typedef std::remove_pointer_t<lua_CFunction> lua_CFunction_ref;
  5588. template <typename T>
  5589. struct non_null { };
  5590. template <typename... Args>
  5591. struct function_sig { };
  5592. struct upvalue_index {
  5593. int index;
  5594. upvalue_index(int idx) : index(lua_upvalueindex(idx)) {
  5595. }
  5596. operator int() const {
  5597. return index;
  5598. }
  5599. };
  5600. struct raw_index {
  5601. int index;
  5602. raw_index(int i) : index(i) {
  5603. }
  5604. operator int() const {
  5605. return index;
  5606. }
  5607. };
  5608. struct absolute_index {
  5609. int index;
  5610. absolute_index(lua_State* L, int idx) : index(lua_absindex(L, idx)) {
  5611. }
  5612. operator int() const {
  5613. return index;
  5614. }
  5615. };
  5616. struct ref_index {
  5617. int index;
  5618. ref_index(int idx) : index(idx) {
  5619. }
  5620. operator int() const {
  5621. return index;
  5622. }
  5623. };
  5624. struct stack_count {
  5625. int count;
  5626. stack_count(int cnt) : count(cnt) {
  5627. }
  5628. };
  5629. struct lightuserdata_value {
  5630. void* value;
  5631. lightuserdata_value(void* data) : value(data) {
  5632. }
  5633. operator void*() const {
  5634. return value;
  5635. }
  5636. };
  5637. struct userdata_value {
  5638. void* value;
  5639. userdata_value(void* data) : value(data) {
  5640. }
  5641. operator void*() const {
  5642. return value;
  5643. }
  5644. };
  5645. template <typename L>
  5646. struct light {
  5647. L* value;
  5648. light(L& x) : value(std::addressof(x)) {
  5649. }
  5650. light(L* x) : value(x) {
  5651. }
  5652. light(void* x) : value(static_cast<L*>(x)) {
  5653. }
  5654. operator L*() const {
  5655. return value;
  5656. }
  5657. operator L&() const {
  5658. return *value;
  5659. }
  5660. };
  5661. template <typename T>
  5662. auto make_light(T& l) {
  5663. typedef meta::unwrapped_t<std::remove_pointer_t<std::remove_pointer_t<T>>> L;
  5664. return light<L>(l);
  5665. }
  5666. template <typename U>
  5667. struct user {
  5668. U value;
  5669. user(U&& x) : value(std::forward<U>(x)) {
  5670. }
  5671. operator std::add_pointer_t<std::remove_reference_t<U>>() {
  5672. return std::addressof(value);
  5673. }
  5674. operator std::add_lvalue_reference_t<U>() {
  5675. return value;
  5676. }
  5677. operator std::add_const_t<std::add_lvalue_reference_t<U>> &() const {
  5678. return value;
  5679. }
  5680. };
  5681. template <typename T>
  5682. auto make_user(T&& u) {
  5683. typedef meta::unwrapped_t<meta::unqualified_t<T>> U;
  5684. return user<U>(std::forward<T>(u));
  5685. }
  5686. template <typename T>
  5687. struct metatable_registry_key {
  5688. T key;
  5689. metatable_registry_key(T key) : key(std::forward<T>(key)) {
  5690. }
  5691. };
  5692. template <typename T>
  5693. auto meta_registry_key(T&& key) {
  5694. typedef meta::unqualified_t<T> K;
  5695. return metatable_registry_key<K>(std::forward<T>(key));
  5696. }
  5697. template <typename... Upvalues>
  5698. struct closure {
  5699. lua_CFunction c_function;
  5700. std::tuple<Upvalues...> upvalues;
  5701. closure(lua_CFunction f, Upvalues... targetupvalues) : c_function(f), upvalues(std::forward<Upvalues>(targetupvalues)...) {
  5702. }
  5703. };
  5704. template <>
  5705. struct closure<> {
  5706. lua_CFunction c_function;
  5707. int upvalues;
  5708. closure(lua_CFunction f, int upvalue_count = 0) : c_function(f), upvalues(upvalue_count) {
  5709. }
  5710. };
  5711. typedef closure<> c_closure;
  5712. template <typename... Args>
  5713. closure<Args...> make_closure(lua_CFunction f, Args&&... args) {
  5714. return closure<Args...>(f, std::forward<Args>(args)...);
  5715. }
  5716. template <typename Sig, typename... Ps>
  5717. struct function_arguments {
  5718. std::tuple<Ps...> arguments;
  5719. template <typename Arg, typename... Args, meta::disable<std::is_same<meta::unqualified_t<Arg>, function_arguments>> = meta::enabler>
  5720. function_arguments(Arg&& arg, Args&&... args) : arguments(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  5721. }
  5722. };
  5723. template <typename Sig = function_sig<>, typename... Args>
  5724. auto as_function(Args&&... args) {
  5725. return function_arguments<Sig, std::decay_t<Args>...>(std::forward<Args>(args)...);
  5726. }
  5727. template <typename Sig = function_sig<>, typename... Args>
  5728. auto as_function_reference(Args&&... args) {
  5729. return function_arguments<Sig, Args...>(std::forward<Args>(args)...);
  5730. }
  5731. template <typename T>
  5732. struct as_table_t {
  5733. private:
  5734. T value_;
  5735. public:
  5736. as_table_t() = default;
  5737. as_table_t(const as_table_t&) = default;
  5738. as_table_t(as_table_t&&) = default;
  5739. as_table_t& operator=(const as_table_t&) = default;
  5740. as_table_t& operator=(as_table_t&&) = default;
  5741. template <typename Arg,
  5742. meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, as_table_t>>,
  5743. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  5744. as_table_t(Arg&& arg) : value_(std::forward<Arg>(arg)) {
  5745. }
  5746. template <typename Arg0, typename Arg1, typename... Args>
  5747. as_table_t(Arg0&& arg0, Arg1&& arg1, Args&&... args) : value_(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  5748. }
  5749. T& value() & {
  5750. return value_;
  5751. }
  5752. T&& value() && {
  5753. return std::move(value_);
  5754. }
  5755. const T& value() const& {
  5756. return value_;
  5757. }
  5758. operator std::add_lvalue_reference_t<T>() {
  5759. return value_;
  5760. }
  5761. };
  5762. template <typename T>
  5763. struct nested {
  5764. private:
  5765. T value_;
  5766. public:
  5767. using nested_type = T;
  5768. nested() = default;
  5769. nested(const nested&) = default;
  5770. nested(nested&&) = default;
  5771. nested& operator=(const nested&) = default;
  5772. nested& operator=(nested&&) = default;
  5773. template <typename Arg,
  5774. meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, nested>>,
  5775. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  5776. nested(Arg&& arg) : value_(std::forward<Arg>(arg)) {
  5777. }
  5778. template <typename Arg0, typename Arg1, typename... Args>
  5779. nested(Arg0&& arg0, Arg1&& arg1, Args&&... args) : value_(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  5780. }
  5781. T& value() & {
  5782. return value_;
  5783. }
  5784. T&& value() && {
  5785. return std::move(value_);
  5786. }
  5787. const T& value() const& {
  5788. return value_;
  5789. }
  5790. operator std::add_lvalue_reference_t<T>() {
  5791. return value_;
  5792. }
  5793. };
  5794. struct nested_tag_t { };
  5795. constexpr inline nested_tag_t nested_tag {};
  5796. template <typename T>
  5797. as_table_t<T> as_table_ref(T&& container) {
  5798. return as_table_t<T>(std::forward<T>(container));
  5799. }
  5800. template <typename T>
  5801. as_table_t<meta::unqualified_t<T>> as_table(T&& container) {
  5802. return as_table_t<meta::unqualified_t<T>>(std::forward<T>(container));
  5803. }
  5804. template <typename T>
  5805. nested<T> as_nested_ref(T&& container) {
  5806. return nested<T>(std::forward<T>(container));
  5807. }
  5808. template <typename T>
  5809. nested<meta::unqualified_t<T>> as_nested(T&& container) {
  5810. return nested<meta::unqualified_t<T>>(std::forward<T>(container));
  5811. }
  5812. template <typename T>
  5813. struct as_container_t {
  5814. private:
  5815. T value_;
  5816. public:
  5817. using type = T;
  5818. as_container_t() = default;
  5819. as_container_t(const as_container_t&) = default;
  5820. as_container_t(as_container_t&&) = default;
  5821. as_container_t& operator=(const as_container_t&) = default;
  5822. as_container_t& operator=(as_container_t&&) = default;
  5823. template <typename Arg,
  5824. meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, as_container_t>>,
  5825. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  5826. as_container_t(Arg&& arg) : value_(std::forward<Arg>(arg)) {
  5827. }
  5828. template <typename Arg0, typename Arg1, typename... Args>
  5829. as_container_t(Arg0&& arg0, Arg1&& arg1, Args&&... args) : value_(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  5830. }
  5831. T& value() & {
  5832. return value_;
  5833. }
  5834. T&& value() && {
  5835. return std::move(value_);
  5836. }
  5837. const T& value() const& {
  5838. return value_;
  5839. }
  5840. };
  5841. template <typename T>
  5842. struct as_container_t<T&> {
  5843. private:
  5844. std::reference_wrapper<T> value_;
  5845. public:
  5846. as_container_t(T& value) : value_(value) {
  5847. }
  5848. T& value() {
  5849. return value_;
  5850. }
  5851. operator T&() {
  5852. return value();
  5853. }
  5854. };
  5855. template <typename T>
  5856. auto as_container(T&& value) {
  5857. return as_container_t<T>(std::forward<T>(value));
  5858. }
  5859. template <typename T>
  5860. struct push_invoke_t {
  5861. private:
  5862. T value_;
  5863. public:
  5864. push_invoke_t() = default;
  5865. push_invoke_t(const push_invoke_t&) = default;
  5866. push_invoke_t(push_invoke_t&&) = default;
  5867. push_invoke_t& operator=(const push_invoke_t&) = default;
  5868. push_invoke_t& operator=(push_invoke_t&&) = default;
  5869. template <typename Arg,
  5870. meta::enable<meta::neg<std::is_same<meta::unqualified_t<Arg>, push_invoke_t>>,
  5871. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<Arg>>>> = meta::enabler>
  5872. push_invoke_t(Arg&& arg) : value_(std::forward<Arg>(arg)) {
  5873. }
  5874. template <typename Arg0, typename Arg1, typename... Args>
  5875. push_invoke_t(Arg0&& arg0, Arg1&& arg1, Args&&... args) : value_(std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...) {
  5876. }
  5877. T& value() & {
  5878. return value_;
  5879. }
  5880. T&& value() && {
  5881. return std::move(value_);
  5882. }
  5883. const T& value() const& {
  5884. return value_;
  5885. }
  5886. };
  5887. template <typename T>
  5888. struct push_invoke_t<T&> {
  5889. std::reference_wrapper<T> value_;
  5890. push_invoke_t(T& value) : value_(value) {
  5891. }
  5892. T& value() {
  5893. return value_;
  5894. }
  5895. };
  5896. template <typename Fx>
  5897. auto push_invoke(Fx&& fx) {
  5898. return push_invoke_t<Fx>(std::forward<Fx>(fx));
  5899. }
  5900. struct override_value_t { };
  5901. constexpr inline override_value_t override_value = override_value_t();
  5902. struct update_if_empty_t { };
  5903. constexpr inline update_if_empty_t update_if_empty = update_if_empty_t();
  5904. struct create_if_nil_t { };
  5905. constexpr inline create_if_nil_t create_if_nil = create_if_nil_t();
  5906. namespace detail {
  5907. enum insert_mode { none = 0x0, update_if_empty = 0x01, override_value = 0x02, create_if_nil = 0x04 };
  5908. template <typename T, typename...>
  5909. using is_insert_mode = std::integral_constant<bool,
  5910. std::is_same_v<T, override_value_t> || std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, create_if_nil_t>>;
  5911. template <typename T, typename...>
  5912. using is_not_insert_mode = meta::neg<is_insert_mode<T>>;
  5913. } // namespace detail
  5914. struct this_state {
  5915. lua_State* L;
  5916. this_state(lua_State* Ls) : L(Ls) {
  5917. }
  5918. operator lua_State*() const noexcept {
  5919. return lua_state();
  5920. }
  5921. lua_State* operator->() const noexcept {
  5922. return lua_state();
  5923. }
  5924. lua_State* lua_state() const noexcept {
  5925. return L;
  5926. }
  5927. };
  5928. struct this_main_state {
  5929. lua_State* L;
  5930. this_main_state(lua_State* Ls) : L(Ls) {
  5931. }
  5932. operator lua_State*() const noexcept {
  5933. return lua_state();
  5934. }
  5935. lua_State* operator->() const noexcept {
  5936. return lua_state();
  5937. }
  5938. lua_State* lua_state() const noexcept {
  5939. return L;
  5940. }
  5941. };
  5942. struct new_table {
  5943. int sequence_hint = 0;
  5944. int map_hint = 0;
  5945. new_table() = default;
  5946. new_table(const new_table&) = default;
  5947. new_table(new_table&&) = default;
  5948. new_table& operator=(const new_table&) = default;
  5949. new_table& operator=(new_table&&) = default;
  5950. new_table(int sequence_hint, int map_hint = 0) : sequence_hint(sequence_hint), map_hint(map_hint) {
  5951. }
  5952. };
  5953. const new_table create = {};
  5954. enum class lib : char {
  5955. // print, assert, and other base functions
  5956. base,
  5957. // require and other package functions
  5958. package,
  5959. // coroutine functions and utilities
  5960. coroutine,
  5961. // string library
  5962. string,
  5963. // functionality from the OS
  5964. os,
  5965. // all things math
  5966. math,
  5967. // the table manipulator and observer functions
  5968. table,
  5969. // the debug library
  5970. debug,
  5971. // the bit library: different based on which you're using
  5972. bit32,
  5973. // input/output library
  5974. io,
  5975. // LuaJIT only
  5976. ffi,
  5977. // LuaJIT only
  5978. jit,
  5979. // library for handling utf8: new to Lua
  5980. utf8,
  5981. // do not use
  5982. count
  5983. };
  5984. enum class call_syntax { dot = 0, colon = 1 };
  5985. enum class load_mode {
  5986. any = 0,
  5987. text = 1,
  5988. binary = 2,
  5989. };
  5990. enum class call_status : int {
  5991. ok = LUA_OK,
  5992. yielded = LUA_YIELD,
  5993. runtime = LUA_ERRRUN,
  5994. memory = LUA_ERRMEM,
  5995. handler = LUA_ERRERR,
  5996. gc = LUA_ERRGCMM,
  5997. syntax = LUA_ERRSYNTAX,
  5998. file = LUA_ERRFILE,
  5999. };
  6000. enum class thread_status : int {
  6001. ok = LUA_OK,
  6002. yielded = LUA_YIELD,
  6003. runtime = LUA_ERRRUN,
  6004. memory = LUA_ERRMEM,
  6005. gc = LUA_ERRGCMM,
  6006. handler = LUA_ERRERR,
  6007. dead = -1,
  6008. };
  6009. enum class load_status : int {
  6010. ok = LUA_OK,
  6011. syntax = LUA_ERRSYNTAX,
  6012. memory = LUA_ERRMEM,
  6013. gc = LUA_ERRGCMM,
  6014. file = LUA_ERRFILE,
  6015. };
  6016. enum class gc_mode : int {
  6017. incremental = 0,
  6018. generational = 1,
  6019. default_value = incremental,
  6020. };
  6021. enum class type : int {
  6022. none = LUA_TNONE,
  6023. lua_nil = LUA_TNIL,
  6024. #if SOL_IS_ON(SOL_NIL_I_)
  6025. nil = lua_nil,
  6026. #endif // Objective C/C++ Keyword that's found in OSX SDK and OBJC -- check for all forms to protect
  6027. string = LUA_TSTRING,
  6028. number = LUA_TNUMBER,
  6029. thread = LUA_TTHREAD,
  6030. boolean = LUA_TBOOLEAN,
  6031. function = LUA_TFUNCTION,
  6032. userdata = LUA_TUSERDATA,
  6033. lightuserdata = LUA_TLIGHTUSERDATA,
  6034. table = LUA_TTABLE,
  6035. poly = -0xFFFF
  6036. };
  6037. inline const std::string& to_string(call_status c) {
  6038. static const std::array<std::string, 10> names { { "ok",
  6039. "yielded",
  6040. "runtime",
  6041. "memory",
  6042. "handler",
  6043. "gc",
  6044. "syntax",
  6045. "file",
  6046. "CRITICAL_EXCEPTION_FAILURE",
  6047. "CRITICAL_INDETERMINATE_STATE_FAILURE" } };
  6048. switch (c) {
  6049. case call_status::ok:
  6050. return names[0];
  6051. case call_status::yielded:
  6052. return names[1];
  6053. case call_status::runtime:
  6054. return names[2];
  6055. case call_status::memory:
  6056. return names[3];
  6057. case call_status::handler:
  6058. return names[4];
  6059. case call_status::gc:
  6060. return names[5];
  6061. case call_status::syntax:
  6062. return names[6];
  6063. case call_status::file:
  6064. return names[7];
  6065. }
  6066. if (static_cast<std::ptrdiff_t>(c) == -1) {
  6067. // One of the many cases where a critical exception error has occurred
  6068. return names[8];
  6069. }
  6070. return names[9];
  6071. }
  6072. inline bool is_indeterminate_call_failure(call_status c) {
  6073. switch (c) {
  6074. case call_status::ok:
  6075. case call_status::yielded:
  6076. case call_status::runtime:
  6077. case call_status::memory:
  6078. case call_status::handler:
  6079. case call_status::gc:
  6080. case call_status::syntax:
  6081. case call_status::file:
  6082. return false;
  6083. }
  6084. return true;
  6085. }
  6086. inline const std::string& to_string(load_status c) {
  6087. static const std::array<std::string, 7> names {
  6088. { "ok", "memory", "gc", "syntax", "file", "CRITICAL_EXCEPTION_FAILURE", "CRITICAL_INDETERMINATE_STATE_FAILURE" }
  6089. };
  6090. switch (c) {
  6091. case load_status::ok:
  6092. return names[0];
  6093. case load_status::memory:
  6094. return names[1];
  6095. case load_status::gc:
  6096. return names[2];
  6097. case load_status::syntax:
  6098. return names[3];
  6099. case load_status::file:
  6100. return names[4];
  6101. }
  6102. if (static_cast<int>(c) == -1) {
  6103. // One of the many cases where a critical exception error has occurred
  6104. return names[5];
  6105. }
  6106. return names[6];
  6107. }
  6108. inline const std::string& to_string(load_mode c) {
  6109. static const std::array<std::string, 3> names { {
  6110. "bt",
  6111. "t",
  6112. "b",
  6113. } };
  6114. return names[static_cast<std::size_t>(c)];
  6115. }
  6116. enum class meta_function {
  6117. construct,
  6118. index,
  6119. new_index,
  6120. mode,
  6121. call,
  6122. call_function = call,
  6123. metatable,
  6124. to_string,
  6125. length,
  6126. unary_minus,
  6127. addition,
  6128. subtraction,
  6129. multiplication,
  6130. division,
  6131. modulus,
  6132. power_of,
  6133. involution = power_of,
  6134. concatenation,
  6135. equal_to,
  6136. less_than,
  6137. less_than_or_equal_to,
  6138. garbage_collect,
  6139. floor_division,
  6140. bitwise_left_shift,
  6141. bitwise_right_shift,
  6142. bitwise_not,
  6143. bitwise_and,
  6144. bitwise_or,
  6145. bitwise_xor,
  6146. pairs,
  6147. ipairs,
  6148. next,
  6149. type,
  6150. type_info,
  6151. call_construct,
  6152. storage,
  6153. gc_names,
  6154. static_index,
  6155. static_new_index,
  6156. };
  6157. typedef meta_function meta_method;
  6158. inline const std::array<std::string, 37>& meta_function_names() {
  6159. static const std::array<std::string, 37> names = { { "new",
  6160. "__index",
  6161. "__newindex",
  6162. "__mode",
  6163. "__call",
  6164. "__metatable",
  6165. "__tostring",
  6166. "__len",
  6167. "__unm",
  6168. "__add",
  6169. "__sub",
  6170. "__mul",
  6171. "__div",
  6172. "__mod",
  6173. "__pow",
  6174. "__concat",
  6175. "__eq",
  6176. "__lt",
  6177. "__le",
  6178. "__gc",
  6179. "__idiv",
  6180. "__shl",
  6181. "__shr",
  6182. "__bnot",
  6183. "__band",
  6184. "__bor",
  6185. "__bxor",
  6186. "__pairs",
  6187. "__ipairs",
  6188. "next",
  6189. "__type",
  6190. "__typeinfo",
  6191. "__sol.call_new",
  6192. "__sol.storage",
  6193. "__sol.gc_names",
  6194. "__sol.static_index",
  6195. "__sol.static_new_index" } };
  6196. return names;
  6197. }
  6198. inline const std::string& to_string(meta_function mf) {
  6199. return meta_function_names()[static_cast<int>(mf)];
  6200. }
  6201. inline type type_of(lua_State* L, int index) {
  6202. return static_cast<type>(lua_type(L, index));
  6203. }
  6204. inline std::string type_name(lua_State* L, type t) {
  6205. return lua_typename(L, static_cast<int>(t));
  6206. }
  6207. template <typename T>
  6208. struct is_lua_reference
  6209. : std::integral_constant<bool, std::is_base_of_v<reference, T> || std::is_base_of_v<main_reference, T> || std::is_base_of_v<stack_reference, T>> { };
  6210. template <typename T>
  6211. inline constexpr bool is_lua_reference_v = is_lua_reference<T>::value;
  6212. template <typename T>
  6213. struct is_lua_reference_or_proxy : std::integral_constant<bool, is_lua_reference_v<T> || meta::is_specialization_of_v<T, table_proxy>> { };
  6214. template <typename T>
  6215. inline constexpr bool is_lua_reference_or_proxy_v = is_lua_reference_or_proxy<T>::value;
  6216. template <typename T>
  6217. struct is_transparent_argument : std::false_type { };
  6218. template <typename T>
  6219. constexpr inline bool is_transparent_argument_v = is_transparent_argument<T>::value;
  6220. template <>
  6221. struct is_transparent_argument<this_state> : std::true_type { };
  6222. template <>
  6223. struct is_transparent_argument<this_main_state> : std::true_type { };
  6224. template <>
  6225. struct is_transparent_argument<this_environment> : std::true_type { };
  6226. template <>
  6227. struct is_transparent_argument<variadic_args> : std::true_type { };
  6228. template <typename T>
  6229. struct is_variadic_arguments : std::is_same<T, variadic_args> { };
  6230. template <typename T>
  6231. struct is_container
  6232. : std::integral_constant<bool,
  6233. !std::is_same_v<state_view,
  6234. T> && !std::is_same_v<state, T> && !meta::is_initializer_list_v<T> && !meta::is_string_like_v<T> && !meta::is_string_literal_array_v<T> && !is_transparent_argument_v<T> && !is_lua_reference_v<T> && (meta::has_begin_end_v<T> || std::is_array_v<T>)> {
  6235. };
  6236. template <typename T>
  6237. constexpr inline bool is_container_v = is_container<T>::value;
  6238. template <typename T>
  6239. struct is_to_stringable : meta::any<meta::supports_to_string_member<meta::unqualified_t<T>>, meta::supports_adl_to_string<meta::unqualified_t<T>>,
  6240. meta::supports_op_left_shift<std::ostream, meta::unqualified_t<T>>> { };
  6241. namespace detail {
  6242. template <typename T, typename = void>
  6243. struct lua_type_of : std::integral_constant<type, type::userdata> { };
  6244. template <typename C, typename T, typename A>
  6245. struct lua_type_of<std::basic_string<C, T, A>> : std::integral_constant<type, type::string> { };
  6246. template <typename C, typename T>
  6247. struct lua_type_of<basic_string_view<C, T>> : std::integral_constant<type, type::string> { };
  6248. template <std::size_t N>
  6249. struct lua_type_of<char[N]> : std::integral_constant<type, type::string> { };
  6250. template <std::size_t N>
  6251. struct lua_type_of<wchar_t[N]> : std::integral_constant<type, type::string> { };
  6252. template <std::size_t N>
  6253. struct lua_type_of<char16_t[N]> : std::integral_constant<type, type::string> { };
  6254. template <std::size_t N>
  6255. struct lua_type_of<char32_t[N]> : std::integral_constant<type, type::string> { };
  6256. template <>
  6257. struct lua_type_of<char> : std::integral_constant<type, type::string> { };
  6258. template <>
  6259. struct lua_type_of<wchar_t> : std::integral_constant<type, type::string> { };
  6260. template <>
  6261. struct lua_type_of<char16_t> : std::integral_constant<type, type::string> { };
  6262. template <>
  6263. struct lua_type_of<char32_t> : std::integral_constant<type, type::string> { };
  6264. template <>
  6265. struct lua_type_of<const char*> : std::integral_constant<type, type::string> { };
  6266. template <>
  6267. struct lua_type_of<const char16_t*> : std::integral_constant<type, type::string> { };
  6268. template <>
  6269. struct lua_type_of<const char32_t*> : std::integral_constant<type, type::string> { };
  6270. template <>
  6271. struct lua_type_of<bool> : std::integral_constant<type, type::boolean> { };
  6272. template <>
  6273. struct lua_type_of<lua_nil_t> : std::integral_constant<type, type::lua_nil> { };
  6274. template <>
  6275. struct lua_type_of<nullopt_t> : std::integral_constant<type, type::lua_nil> { };
  6276. template <>
  6277. struct lua_type_of<lua_value> : std::integral_constant<type, type::poly> { };
  6278. template <>
  6279. struct lua_type_of<detail::non_lua_nil_t> : std::integral_constant<type, type::poly> { };
  6280. template <>
  6281. struct lua_type_of<std::nullptr_t> : std::integral_constant<type, type::lua_nil> { };
  6282. template <>
  6283. struct lua_type_of<error> : std::integral_constant<type, type::string> { };
  6284. template <bool b, typename Base>
  6285. struct lua_type_of<basic_table_core<b, Base>> : std::integral_constant<type, type::table> { };
  6286. template <typename Base>
  6287. struct lua_type_of<basic_lua_table<Base>> : std::integral_constant<type, type::table> { };
  6288. template <typename Base>
  6289. struct lua_type_of<basic_metatable<Base>> : std::integral_constant<type, type::table> { };
  6290. template <typename T, typename Base>
  6291. struct lua_type_of<basic_usertype<T, Base>> : std::integral_constant<type, type::table> { };
  6292. template <>
  6293. struct lua_type_of<metatable_key_t> : std::integral_constant<type, type::table> { };
  6294. template <typename B>
  6295. struct lua_type_of<basic_environment<B>> : std::integral_constant<type, type::poly> { };
  6296. template <>
  6297. struct lua_type_of<env_key_t> : std::integral_constant<type, type::poly> { };
  6298. template <>
  6299. struct lua_type_of<new_table> : std::integral_constant<type, type::table> { };
  6300. template <typename T>
  6301. struct lua_type_of<as_table_t<T>> : std::integral_constant<type, type::table> { };
  6302. template <typename T>
  6303. struct lua_type_of<std::initializer_list<T>> : std::integral_constant<type, type::table> { };
  6304. template <bool b>
  6305. struct lua_type_of<basic_reference<b>> : std::integral_constant<type, type::poly> { };
  6306. template <>
  6307. struct lua_type_of<stack_reference> : std::integral_constant<type, type::poly> { };
  6308. template <typename Base>
  6309. struct lua_type_of<basic_object<Base>> : std::integral_constant<type, type::poly> { };
  6310. template <typename... Args>
  6311. struct lua_type_of<std::tuple<Args...>> : std::integral_constant<type, type::poly> { };
  6312. template <typename A, typename B>
  6313. struct lua_type_of<std::pair<A, B>> : std::integral_constant<type, type::poly> { };
  6314. template <>
  6315. struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> { };
  6316. template <>
  6317. struct lua_type_of<const void*> : std::integral_constant<type, type::lightuserdata> { };
  6318. template <>
  6319. struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> { };
  6320. template <>
  6321. struct lua_type_of<userdata_value> : std::integral_constant<type, type::userdata> { };
  6322. template <typename T>
  6323. struct lua_type_of<light<T>> : std::integral_constant<type, type::lightuserdata> { };
  6324. template <typename T>
  6325. struct lua_type_of<user<T>> : std::integral_constant<type, type::userdata> { };
  6326. template <typename Base>
  6327. struct lua_type_of<basic_lightuserdata<Base>> : std::integral_constant<type, type::lightuserdata> { };
  6328. template <typename Base>
  6329. struct lua_type_of<basic_userdata<Base>> : std::integral_constant<type, type::userdata> { };
  6330. template <>
  6331. struct lua_type_of<lua_CFunction> : std::integral_constant<type, type::function> { };
  6332. template <>
  6333. struct lua_type_of<std::remove_pointer_t<lua_CFunction>> : std::integral_constant<type, type::function> { };
  6334. template <typename Base, bool aligned>
  6335. struct lua_type_of<basic_function<Base, aligned>> : std::integral_constant<type, type::function> { };
  6336. template <typename Base, bool aligned, typename Handler>
  6337. struct lua_type_of<basic_protected_function<Base, aligned, Handler>> : std::integral_constant<type, type::function> { };
  6338. template <typename Base>
  6339. struct lua_type_of<basic_coroutine<Base>> : std::integral_constant<type, type::function> { };
  6340. template <typename Base>
  6341. struct lua_type_of<basic_thread<Base>> : std::integral_constant<type, type::thread> { };
  6342. template <typename Signature>
  6343. struct lua_type_of<std::function<Signature>> : std::integral_constant<type, type::function> { };
  6344. template <typename T>
  6345. struct lua_type_of<optional<T>> : std::integral_constant<type, type::poly> { };
  6346. template <typename T>
  6347. struct lua_type_of<std::optional<T>> : std::integral_constant<type, type::poly> { };
  6348. template <>
  6349. struct lua_type_of<variadic_args> : std::integral_constant<type, type::poly> { };
  6350. template <>
  6351. struct lua_type_of<variadic_results> : std::integral_constant<type, type::poly> { };
  6352. template <>
  6353. struct lua_type_of<stack_count> : std::integral_constant<type, type::poly> { };
  6354. template <>
  6355. struct lua_type_of<this_state> : std::integral_constant<type, type::poly> { };
  6356. template <>
  6357. struct lua_type_of<this_main_state> : std::integral_constant<type, type::poly> { };
  6358. template <>
  6359. struct lua_type_of<this_environment> : std::integral_constant<type, type::poly> { };
  6360. template <>
  6361. struct lua_type_of<type> : std::integral_constant<type, type::poly> { };
  6362. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  6363. template <typename T>
  6364. struct lua_type_of<T*> : std::integral_constant<type, std::is_function_v<T> ? type::function : type::userdata> { };
  6365. #else
  6366. template <typename T>
  6367. struct lua_type_of<T*> : std::integral_constant<type, type::userdata> { };
  6368. #endif
  6369. template <typename T>
  6370. struct lua_type_of<T, std::enable_if_t<std::is_arithmetic_v<T> || std::is_same_v<T, lua_Number> || std::is_same_v<T, lua_Integer>>>
  6371. : std::integral_constant<type, type::number> { };
  6372. template <typename T>
  6373. struct lua_type_of<T, std::enable_if_t<std::is_function_v<T>>> : std::integral_constant<type, type::function> { };
  6374. template <typename T>
  6375. struct lua_type_of<T, std::enable_if_t<std::is_enum_v<T>>> : std::integral_constant<type, type::number> { };
  6376. template <>
  6377. struct lua_type_of<meta_function> : std::integral_constant<type, type::string> { };
  6378. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  6379. template <typename... Tn>
  6380. struct lua_type_of<std::variant<Tn...>> : std::integral_constant<type, type::poly> { };
  6381. #endif // std::variant deployment sucks on Clang
  6382. template <typename T>
  6383. struct lua_type_of<nested<T>> : meta::conditional_t<::sol::is_container_v<T>, std::integral_constant<type, type::table>, lua_type_of<T>> { };
  6384. template <typename C, C v, template <typename...> class V, typename... Args>
  6385. struct accumulate : std::integral_constant<C, v> { };
  6386. template <typename C, C v, template <typename...> class V, typename T, typename... Args>
  6387. struct accumulate<C, v, V, T, Args...> : accumulate<C, v + V<T>::value, V, Args...> { };
  6388. template <typename C, C v, template <typename...> class V, typename List>
  6389. struct accumulate_list;
  6390. template <typename C, C v, template <typename...> class V, typename... Args>
  6391. struct accumulate_list<C, v, V, types<Args...>> : accumulate<C, v, V, Args...> { };
  6392. } // namespace detail
  6393. template <typename T>
  6394. struct lua_type_of : detail::lua_type_of<T> {
  6395. typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
  6396. };
  6397. template <typename T>
  6398. inline constexpr type lua_type_of_v = lua_type_of<T>::value;
  6399. template <typename T>
  6400. struct lua_size : std::integral_constant<int, 1> {
  6401. typedef int SOL_INTERNAL_UNSPECIALIZED_MARKER_;
  6402. };
  6403. template <typename A, typename B>
  6404. struct lua_size<std::pair<A, B>> : std::integral_constant<int, lua_size<A>::value + lua_size<B>::value> { };
  6405. template <typename... Args>
  6406. struct lua_size<std::tuple<Args...>> : std::integral_constant<int, detail::accumulate<int, 0, lua_size, Args...>::value> { };
  6407. template <typename T>
  6408. inline constexpr int lua_size_v = lua_size<T>::value;
  6409. namespace detail {
  6410. template <typename...>
  6411. struct void_ {
  6412. typedef void type;
  6413. };
  6414. template <typename T, typename = void>
  6415. struct has_internal_marker_impl : std::false_type { };
  6416. template <typename T>
  6417. struct has_internal_marker_impl<T, typename void_<typename T::SOL_INTERNAL_UNSPECIALIZED_MARKER_>::type> : std::true_type { };
  6418. template <typename T>
  6419. using has_internal_marker = has_internal_marker_impl<T>;
  6420. template <typename T>
  6421. constexpr inline bool has_internal_marker_v = has_internal_marker<T>::value;
  6422. } // namespace detail
  6423. template <typename T>
  6424. struct is_lua_primitive
  6425. : std::integral_constant<bool,
  6426. type::userdata
  6427. != lua_type_of_v<
  6428. T> || ((type::userdata == lua_type_of_v<T>)&&detail::has_internal_marker_v<lua_type_of<T>> && !detail::has_internal_marker_v<lua_size<T>>)
  6429. || is_lua_reference_or_proxy_v<T> || meta::is_specialization_of_v<T, std::tuple> || meta::is_specialization_of_v<T, std::pair>> { };
  6430. template <typename T>
  6431. constexpr inline bool is_lua_primitive_v = is_lua_primitive<T>::value;
  6432. template <typename T>
  6433. struct is_main_threaded : std::is_base_of<main_reference, T> { };
  6434. template <typename T>
  6435. struct is_stack_based : std::is_base_of<stack_reference, T> { };
  6436. template <>
  6437. struct is_stack_based<variadic_args> : std::true_type { };
  6438. template <>
  6439. struct is_stack_based<unsafe_function_result> : std::true_type { };
  6440. template <>
  6441. struct is_stack_based<protected_function_result> : std::true_type { };
  6442. template <>
  6443. struct is_stack_based<stack_proxy> : std::true_type { };
  6444. template <>
  6445. struct is_stack_based<stack_proxy_base> : std::true_type { };
  6446. template <>
  6447. struct is_stack_based<stack_count> : std::true_type { };
  6448. template <typename T>
  6449. constexpr inline bool is_stack_based_v = is_stack_based<T>::value;
  6450. template <typename T>
  6451. struct is_lua_primitive<T*> : std::true_type { };
  6452. template <>
  6453. struct is_lua_primitive<unsafe_function_result> : std::true_type { };
  6454. template <>
  6455. struct is_lua_primitive<protected_function_result> : std::true_type { };
  6456. template <typename T>
  6457. struct is_lua_primitive<std::reference_wrapper<T>> : std::true_type { };
  6458. template <typename T>
  6459. struct is_lua_primitive<user<T>> : std::true_type { };
  6460. template <typename T>
  6461. struct is_lua_primitive<light<T>> : is_lua_primitive<T*> { };
  6462. template <typename T>
  6463. struct is_lua_primitive<optional<T>> : std::true_type { };
  6464. template <typename T>
  6465. struct is_lua_primitive<std::optional<T>> : std::true_type { };
  6466. template <typename T>
  6467. struct is_lua_primitive<as_table_t<T>> : std::true_type { };
  6468. template <typename T>
  6469. struct is_lua_primitive<nested<T>> : std::true_type { };
  6470. template <>
  6471. struct is_lua_primitive<userdata_value> : std::true_type { };
  6472. template <>
  6473. struct is_lua_primitive<lightuserdata_value> : std::true_type { };
  6474. template <>
  6475. struct is_lua_primitive<stack_proxy> : std::true_type { };
  6476. template <>
  6477. struct is_lua_primitive<stack_proxy_base> : std::true_type { };
  6478. template <typename T>
  6479. struct is_lua_primitive<non_null<T>> : is_lua_primitive<T*> { };
  6480. template <typename T>
  6481. struct is_lua_index : std::is_integral<T> { };
  6482. template <>
  6483. struct is_lua_index<raw_index> : std::true_type { };
  6484. template <>
  6485. struct is_lua_index<absolute_index> : std::true_type { };
  6486. template <>
  6487. struct is_lua_index<ref_index> : std::true_type { };
  6488. template <>
  6489. struct is_lua_index<upvalue_index> : std::true_type { };
  6490. template <typename Signature>
  6491. struct lua_bind_traits : meta::bind_traits<Signature> {
  6492. private:
  6493. typedef meta::bind_traits<Signature> base_t;
  6494. public:
  6495. typedef std::integral_constant<bool, meta::count_for<is_variadic_arguments, typename base_t::args_list>::value != 0> runtime_variadics_t;
  6496. static const std::size_t true_arity = base_t::arity;
  6497. static const std::size_t arity = detail::accumulate_list<std::size_t, 0, lua_size, typename base_t::args_list>::value
  6498. - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
  6499. static const std::size_t true_free_arity = base_t::free_arity;
  6500. static const std::size_t free_arity = detail::accumulate_list<std::size_t, 0, lua_size, typename base_t::free_args_list>::value
  6501. - meta::count_for<is_transparent_argument, typename base_t::args_list>::value;
  6502. };
  6503. template <typename T>
  6504. struct is_table : std::false_type { };
  6505. template <bool x, typename T>
  6506. struct is_table<basic_table_core<x, T>> : std::true_type { };
  6507. template <typename T>
  6508. struct is_table<basic_lua_table<T>> : std::true_type { };
  6509. template <typename T>
  6510. inline constexpr bool is_table_v = is_table<T>::value;
  6511. template <typename T>
  6512. struct is_stack_table : std::false_type { };
  6513. template <bool x, typename T>
  6514. struct is_stack_table<basic_table_core<x, T>> : std::integral_constant<bool, std::is_base_of_v<stack_reference, T>> { };
  6515. template <typename T>
  6516. struct is_stack_table<basic_lua_table<T>> : std::integral_constant<bool, std::is_base_of_v<stack_reference, T>> { };
  6517. template <typename T>
  6518. inline constexpr bool is_stack_table_v = is_stack_table<T>::value;
  6519. template <typename T>
  6520. struct is_function : std::false_type { };
  6521. template <typename T, bool aligned>
  6522. struct is_function<basic_function<T, aligned>> : std::true_type { };
  6523. template <typename T, bool aligned, typename Handler>
  6524. struct is_function<basic_protected_function<T, aligned, Handler>> : std::true_type { };
  6525. template <typename T>
  6526. using is_lightuserdata = meta::is_specialization_of<T, basic_lightuserdata>;
  6527. template <typename T>
  6528. inline constexpr bool is_lightuserdata_v = is_lightuserdata<T>::value;
  6529. template <typename T>
  6530. using is_userdata = meta::is_specialization_of<T, basic_userdata>;
  6531. template <typename T>
  6532. inline constexpr bool is_userdata_v = is_userdata<T>::value;
  6533. template <typename T>
  6534. using is_environment = std::integral_constant<bool, is_userdata_v<T> || is_table_v<T> || meta::is_specialization_of_v<T, basic_environment>>;
  6535. template <typename T>
  6536. inline constexpr bool is_environment_v = is_environment<T>::value;
  6537. template <typename T>
  6538. using is_table_like = std::integral_constant<bool, is_table_v<T> || is_environment_v<T> || is_userdata_v<T>>;
  6539. template <typename T>
  6540. inline constexpr bool is_table_like_v = is_table_like<T>::value;
  6541. template <typename T>
  6542. struct is_automagical
  6543. : std::integral_constant<bool,
  6544. (SOL_IS_ON(SOL_DEFAULT_AUTOMAGICAL_USERTYPES_I_))
  6545. || (std::is_array_v<
  6546. meta::unqualified_t<T>> || (!std::is_same_v<meta::unqualified_t<T>, state> && !std::is_same_v<meta::unqualified_t<T>, state_view>))> {
  6547. };
  6548. template <typename T>
  6549. inline type type_of() {
  6550. return lua_type_of<meta::unqualified_t<T>>::value;
  6551. }
  6552. namespace detail {
  6553. template <typename T>
  6554. struct is_non_factory_constructor : std::false_type { };
  6555. template <typename... Args>
  6556. struct is_non_factory_constructor<constructors<Args...>> : std::true_type { };
  6557. template <typename... Args>
  6558. struct is_non_factory_constructor<constructor_wrapper<Args...>> : std::true_type { };
  6559. template <>
  6560. struct is_non_factory_constructor<no_construction> : std::true_type { };
  6561. template <typename T>
  6562. inline constexpr bool is_non_factory_constructor_v = is_non_factory_constructor<T>::value;
  6563. template <typename T>
  6564. struct is_constructor : is_non_factory_constructor<T> { };
  6565. template <typename... Args>
  6566. struct is_constructor<factory_wrapper<Args...>> : std::true_type { };
  6567. template <typename T>
  6568. struct is_constructor<protect_t<T>> : is_constructor<meta::unqualified_t<T>> { };
  6569. template <typename F, typename... Policies>
  6570. struct is_constructor<policy_wrapper<F, Policies...>> : is_constructor<meta::unqualified_t<F>> { };
  6571. template <typename T>
  6572. inline constexpr bool is_constructor_v = is_constructor<T>::value;
  6573. template <typename... Args>
  6574. using any_is_constructor = meta::any<is_constructor<meta::unqualified_t<Args>>...>;
  6575. template <typename... Args>
  6576. inline constexpr bool any_is_constructor_v = any_is_constructor<Args...>::value;
  6577. template <typename T>
  6578. struct is_destructor : std::false_type { };
  6579. template <typename Fx>
  6580. struct is_destructor<destructor_wrapper<Fx>> : std::true_type { };
  6581. template <typename... Args>
  6582. using any_is_destructor = meta::any<is_destructor<meta::unqualified_t<Args>>...>;
  6583. template <typename... Args>
  6584. inline constexpr bool any_is_destructor_v = any_is_destructor<Args...>::value;
  6585. } // namespace detail
  6586. template <typename T>
  6587. using is_lua_c_function = meta::any<std::is_same<lua_CFunction, T>, std::is_same<detail::lua_CFunction_noexcept, T>, std::is_same<lua_CFunction_ref, T>>;
  6588. template <typename T>
  6589. inline constexpr bool is_lua_c_function_v = is_lua_c_function<T>::value;
  6590. struct automagic_enrollments {
  6591. bool default_constructor = true;
  6592. bool destructor = true;
  6593. bool pairs_operator = true;
  6594. bool to_string_operator = true;
  6595. bool call_operator = true;
  6596. bool less_than_operator = true;
  6597. bool less_than_or_equal_to_operator = true;
  6598. bool length_operator = true;
  6599. bool equal_to_operator = true;
  6600. };
  6601. } // namespace sol
  6602. // end of sol/types.hpp
  6603. #include <exception>
  6604. #include <cstring>
  6605. #if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
  6606. #include <iostream>
  6607. #endif
  6608. namespace sol {
  6609. // must push a single object to be the error object
  6610. // NOTE: the VAST MAJORITY of all Lua libraries -- C or otherwise -- expect a string for the type of error
  6611. // break this convention at your own risk
  6612. using exception_handler_function = int (*)(lua_State*, optional<const std::exception&>, string_view);
  6613. namespace detail {
  6614. inline const char (&default_exception_handler_name())[11] {
  6615. static const char name[11] = "sol.\xE2\x98\xA2\xE2\x98\xA2";
  6616. return name;
  6617. }
  6618. // must push at least 1 object on the stack
  6619. inline int default_exception_handler(lua_State* L, optional<const std::exception&>, string_view what) {
  6620. #if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
  6621. std::cerr << "[sol3] An exception occurred: ";
  6622. std::cerr.write(what.data(), what.size());
  6623. std::cerr << std::endl;
  6624. #endif
  6625. lua_pushlstring(L, what.data(), what.size());
  6626. return 1;
  6627. }
  6628. inline int call_exception_handler(lua_State* L, optional<const std::exception&> maybe_ex, string_view what) {
  6629. lua_getglobal(L, default_exception_handler_name());
  6630. type t = static_cast<type>(lua_type(L, -1));
  6631. if (t != type::lightuserdata) {
  6632. lua_pop(L, 1);
  6633. return default_exception_handler(L, std::move(maybe_ex), std::move(what));
  6634. }
  6635. void* vfunc = lua_touserdata(L, -1);
  6636. lua_pop(L, 1);
  6637. if (vfunc == nullptr) {
  6638. return default_exception_handler(L, std::move(maybe_ex), std::move(what));
  6639. }
  6640. exception_handler_function exfunc = reinterpret_cast<exception_handler_function>(vfunc);
  6641. return exfunc(L, std::move(maybe_ex), std::move(what));
  6642. }
  6643. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  6644. template <lua_CFunction f>
  6645. int static_trampoline(lua_State* L) noexcept {
  6646. return f(L);
  6647. }
  6648. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  6649. template <lua_CFunction_noexcept f>
  6650. int static_trampoline_noexcept(lua_State* L) noexcept {
  6651. return f(L);
  6652. }
  6653. #else
  6654. template <lua_CFunction f>
  6655. int static_trampoline_noexcept(lua_State* L) noexcept {
  6656. return f(L);
  6657. }
  6658. #endif
  6659. template <typename Fx, typename... Args>
  6660. int trampoline(lua_State* L, Fx&& f, Args&&... args) noexcept {
  6661. return f(L, std::forward<Args>(args)...);
  6662. }
  6663. inline int c_trampoline(lua_State* L, lua_CFunction f) noexcept {
  6664. return trampoline(L, f);
  6665. }
  6666. #else
  6667. inline int lua_cfunction_trampoline(lua_State* L, lua_CFunction f) {
  6668. #if SOL_IS_ON(SOL_PROPAGATE_EXCEPTIONS_I_)
  6669. return f(L);
  6670. #else
  6671. try {
  6672. return f(L);
  6673. }
  6674. catch (const char* cs) {
  6675. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
  6676. }
  6677. catch (const std::string& s) {
  6678. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
  6679. }
  6680. catch (const std::exception& e) {
  6681. call_exception_handler(L, optional<const std::exception&>(e), e.what());
  6682. }
  6683. #if SOL_IS_OFF(SOL_USE_LUAJIT_I_)
  6684. // LuaJIT cannot have the catchall when the safe propagation is on
  6685. // but LuaJIT will swallow all C++ errors
  6686. // if we don't at least catch std::exception ones
  6687. catch (...) {
  6688. call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
  6689. }
  6690. #endif // LuaJIT cannot have the catchall, but we must catch std::exceps for it
  6691. return lua_error(L);
  6692. #endif // Safe exceptions
  6693. }
  6694. template <lua_CFunction f>
  6695. int static_trampoline(lua_State* L) {
  6696. return lua_cfunction_trampoline(L, f);
  6697. }
  6698. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  6699. template <lua_CFunction_noexcept f>
  6700. int static_trampoline_noexcept(lua_State* L) noexcept {
  6701. return f(L);
  6702. }
  6703. #else
  6704. template <lua_CFunction f>
  6705. int static_trampoline_noexcept(lua_State* L) noexcept {
  6706. return f(L);
  6707. }
  6708. #endif
  6709. template <typename Fx, typename... Args>
  6710. int trampoline(lua_State* L, Fx&& f, Args&&... args) {
  6711. if constexpr (meta::bind_traits<meta::unqualified_t<Fx>>::is_noexcept) {
  6712. return f(L, std::forward<Args>(args)...);
  6713. }
  6714. else {
  6715. #if SOL_IS_ON(SOL_PROPAGATE_EXCEPTIONS_I_)
  6716. return f(L, std::forward<Args>(args)...);
  6717. #else
  6718. try {
  6719. return f(L, std::forward<Args>(args)...);
  6720. }
  6721. catch (const char* cs) {
  6722. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
  6723. }
  6724. catch (const std::string& s) {
  6725. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
  6726. }
  6727. catch (const std::exception& e) {
  6728. call_exception_handler(L, optional<const std::exception&>(e), e.what());
  6729. }
  6730. #if SOL_IS_OFF(SOL_USE_LUAJIT_I_)
  6731. // LuaJIT cannot have the catchall when the safe propagation is on
  6732. // but LuaJIT will swallow all C++ errors
  6733. // if we don't at least catch std::exception ones
  6734. catch (...) {
  6735. call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
  6736. }
  6737. #endif
  6738. return lua_error(L);
  6739. #endif
  6740. }
  6741. }
  6742. inline int c_trampoline(lua_State* L, lua_CFunction f) {
  6743. return trampoline(L, f);
  6744. }
  6745. #endif // Exceptions vs. No Exceptions
  6746. template <typename F, F fx>
  6747. inline int typed_static_trampoline(lua_State* L) {
  6748. if constexpr (meta::bind_traits<F>::is_noexcept) {
  6749. return static_trampoline_noexcept<fx>(L);
  6750. }
  6751. else {
  6752. return static_trampoline<fx>(L);
  6753. }
  6754. }
  6755. } // namespace detail
  6756. inline void set_default_exception_handler(lua_State* L, exception_handler_function exf = &detail::default_exception_handler) {
  6757. static_assert(sizeof(void*) >= sizeof(exception_handler_function),
  6758. "void* storage is too small to transport the exception handler: please file a bug on the sol2 issue tracker to get this looked at!");
  6759. void* storage;
  6760. std::memcpy(&storage, &exf, sizeof(exception_handler_function));
  6761. lua_pushlightuserdata(L, storage);
  6762. lua_setglobal(L, detail::default_exception_handler_name());
  6763. }
  6764. } // namespace sol
  6765. // end of sol/trampoline.hpp
  6766. // beginning of sol/stack_core.hpp
  6767. // beginning of sol/inheritance.hpp
  6768. // beginning of sol/usertype_traits.hpp
  6769. // beginning of sol/demangle.hpp
  6770. #include <string>
  6771. #include <array>
  6772. #include <cctype>
  6773. #if SOL_IS_ON(SOL_MINGW_CCTYPE_IS_POISONED_I_)
  6774. extern "C" {
  6775. #include <ctype.h>
  6776. }
  6777. #endif // MinGW is on some stuff
  6778. #include <locale>
  6779. namespace sol { namespace detail {
  6780. inline constexpr std::array<string_view, 9> removals { { "{anonymous}",
  6781. "(anonymous namespace)",
  6782. "public:",
  6783. "private:",
  6784. "protected:",
  6785. "struct ",
  6786. "class ",
  6787. "`anonymous-namespace'",
  6788. "`anonymous namespace'" } };
  6789. #if SOL_IS_ON(SOL_COMPILER_GCC_I_) || SOL_IS_ON(SOL_COMPILER_CLANG_I_)
  6790. inline std::string ctti_get_type_name_from_sig(std::string name) {
  6791. // cardinal sins from MINGW
  6792. using namespace std;
  6793. std::size_t start = name.find_first_of('[');
  6794. start = name.find_first_of('=', start);
  6795. std::size_t end = name.find_last_of(']');
  6796. if (end == std::string::npos)
  6797. end = name.size();
  6798. if (start == std::string::npos)
  6799. start = 0;
  6800. if (start < name.size() - 1)
  6801. start += 1;
  6802. name = name.substr(start, end - start);
  6803. start = name.rfind("seperator_mark");
  6804. if (start != std::string::npos) {
  6805. name.erase(start - 2, name.length());
  6806. }
  6807. while (!name.empty() && isblank(name.front()))
  6808. name.erase(name.begin());
  6809. while (!name.empty() && isblank(name.back()))
  6810. name.pop_back();
  6811. for (std::size_t r = 0; r < removals.size(); ++r) {
  6812. auto found = name.find(removals[r]);
  6813. while (found != std::string::npos) {
  6814. name.erase(found, removals[r].size());
  6815. found = name.find(removals[r]);
  6816. }
  6817. }
  6818. return name;
  6819. }
  6820. template <typename T, class seperator_mark = int>
  6821. inline std::string ctti_get_type_name() {
  6822. return ctti_get_type_name_from_sig(__PRETTY_FUNCTION__);
  6823. }
  6824. #elif SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  6825. inline std::string ctti_get_type_name_from_sig(std::string name) {
  6826. std::size_t start = name.find("get_type_name");
  6827. if (start == std::string::npos)
  6828. start = 0;
  6829. else
  6830. start += 13;
  6831. if (start < name.size() - 1)
  6832. start += 1;
  6833. std::size_t end = name.find_last_of('>');
  6834. if (end == std::string::npos)
  6835. end = name.size();
  6836. name = name.substr(start, end - start);
  6837. if (name.find("struct", 0) == 0)
  6838. name.replace(0, 6, "", 0);
  6839. if (name.find("class", 0) == 0)
  6840. name.replace(0, 5, "", 0);
  6841. while (!name.empty() && isblank(name.front()))
  6842. name.erase(name.begin());
  6843. while (!name.empty() && isblank(name.back()))
  6844. name.pop_back();
  6845. for (std::size_t r = 0; r < removals.size(); ++r) {
  6846. auto found = name.find(removals[r]);
  6847. while (found != std::string::npos) {
  6848. name.erase(found, removals[r].size());
  6849. found = name.find(removals[r]);
  6850. }
  6851. }
  6852. return name;
  6853. }
  6854. template <typename T>
  6855. std::string ctti_get_type_name() {
  6856. return ctti_get_type_name_from_sig(__FUNCSIG__);
  6857. }
  6858. #else
  6859. #error Compiler not supported for demangling
  6860. #endif // compilers
  6861. template <typename T>
  6862. std::string demangle_once() {
  6863. std::string realname = ctti_get_type_name<T>();
  6864. return realname;
  6865. }
  6866. inline std::string short_demangle_from_type_name(std::string realname) {
  6867. // This isn't the most complete but it'll do for now...?
  6868. static const std::array<std::string, 10> ops = {
  6869. { "operator<", "operator<<", "operator<<=", "operator<=", "operator>", "operator>>", "operator>>=", "operator>=", "operator->", "operator->*" }
  6870. };
  6871. int level = 0;
  6872. std::ptrdiff_t idx = 0;
  6873. for (idx = static_cast<std::ptrdiff_t>(realname.empty() ? 0 : realname.size() - 1); idx > 0; --idx) {
  6874. if (level == 0 && realname[idx] == ':') {
  6875. break;
  6876. }
  6877. bool isleft = realname[idx] == '<';
  6878. bool isright = realname[idx] == '>';
  6879. if (!isleft && !isright)
  6880. continue;
  6881. bool earlybreak = false;
  6882. for (const auto& op : ops) {
  6883. std::size_t nisop = realname.rfind(op, idx);
  6884. if (nisop == std::string::npos)
  6885. continue;
  6886. std::size_t nisopidx = idx - op.size() + 1;
  6887. if (nisop == nisopidx) {
  6888. idx = static_cast<std::ptrdiff_t>(nisopidx);
  6889. earlybreak = true;
  6890. }
  6891. break;
  6892. }
  6893. if (earlybreak) {
  6894. continue;
  6895. }
  6896. level += isleft ? -1 : 1;
  6897. }
  6898. if (idx > 0) {
  6899. realname.erase(0, realname.length() < static_cast<std::size_t>(idx) ? realname.length() : idx + 1);
  6900. }
  6901. return realname;
  6902. }
  6903. template <typename T>
  6904. std::string short_demangle_once() {
  6905. std::string realname = ctti_get_type_name<T>();
  6906. return short_demangle_from_type_name(realname);
  6907. }
  6908. template <typename T>
  6909. const std::string& demangle() {
  6910. static const std::string d = demangle_once<T>();
  6911. return d;
  6912. }
  6913. template <typename T>
  6914. const std::string& short_demangle() {
  6915. static const std::string d = short_demangle_once<T>();
  6916. return d;
  6917. }
  6918. }} // namespace sol::detail
  6919. // end of sol/demangle.hpp
  6920. namespace sol {
  6921. template <typename T>
  6922. struct usertype_traits {
  6923. static const std::string& name() {
  6924. static const std::string& n = detail::short_demangle<T>();
  6925. return n;
  6926. }
  6927. static const std::string& qualified_name() {
  6928. static const std::string& q_n = detail::demangle<T>();
  6929. return q_n;
  6930. }
  6931. static const std::string& metatable() {
  6932. static const std::string m = std::string("sol.").append(detail::demangle<T>());
  6933. return m;
  6934. }
  6935. static const std::string& user_metatable() {
  6936. static const std::string u_m = std::string("sol.").append(detail::demangle<T>()).append(".user");
  6937. return u_m;
  6938. }
  6939. static const std::string& user_gc_metatable() {
  6940. static const std::string u_g_m = std::string("sol.").append(detail::demangle<T>()).append(".user\xE2\x99\xBB");
  6941. return u_g_m;
  6942. }
  6943. static const std::string& gc_table() {
  6944. static const std::string g_t = std::string("sol.").append(detail::demangle<T>()).append(".\xE2\x99\xBB");
  6945. return g_t;
  6946. }
  6947. };
  6948. } // namespace sol
  6949. // end of sol/usertype_traits.hpp
  6950. // beginning of sol/unique_usertype_traits.hpp
  6951. #include <memory>
  6952. namespace sol {
  6953. template <typename T>
  6954. struct unique_usertype_traits {
  6955. typedef T type;
  6956. typedef T actual_type;
  6957. template <typename X>
  6958. using rebind_base = void;
  6959. static const bool value = false;
  6960. template <typename U>
  6961. static bool is_null(U&&) {
  6962. return false;
  6963. }
  6964. template <typename U>
  6965. static auto get(U&& value) {
  6966. return std::addressof(detail::deref(value));
  6967. }
  6968. };
  6969. template <typename T>
  6970. struct unique_usertype_traits<std::shared_ptr<T>> {
  6971. typedef T type;
  6972. typedef std::shared_ptr<T> actual_type;
  6973. // rebind is non-void
  6974. // if and only if unique usertype
  6975. // is cast-capable
  6976. template <typename X>
  6977. using rebind_base = std::shared_ptr<X>;
  6978. static const bool value = true;
  6979. static bool is_null(const actual_type& p) {
  6980. return p == nullptr;
  6981. }
  6982. static type* get(const actual_type& p) {
  6983. return p.get();
  6984. }
  6985. };
  6986. template <typename T, typename D>
  6987. struct unique_usertype_traits<std::unique_ptr<T, D>> {
  6988. using type = T;
  6989. using actual_type = std::unique_ptr<T, D>;
  6990. static const bool value = true;
  6991. static bool is_null(const actual_type& p) {
  6992. return p == nullptr;
  6993. }
  6994. static type* get(const actual_type& p) {
  6995. return p.get();
  6996. }
  6997. };
  6998. template <typename T>
  6999. struct is_unique_usertype : std::integral_constant<bool, unique_usertype_traits<T>::value> {};
  7000. template <typename T>
  7001. inline constexpr bool is_unique_usertype_v = is_unique_usertype<T>::value;
  7002. namespace detail {
  7003. template <typename T, typename Rebind = void>
  7004. using is_base_rebindable_test = typename T::template rebind_base<Rebind>;
  7005. }
  7006. template <typename T>
  7007. using is_base_rebindable = meta::is_detected<detail::is_base_rebindable_test, T>;
  7008. template <typename T>
  7009. inline constexpr bool is_base_rebindable_v = is_base_rebindable<T>::value;
  7010. namespace detail {
  7011. template <typename T, typename = void>
  7012. struct is_base_rebindable_non_void_sfinae : std::false_type {};
  7013. template <typename T>
  7014. struct is_base_rebindable_non_void_sfinae<T, std::enable_if_t<is_base_rebindable_v<T>>>
  7015. : std::integral_constant<bool, !std::is_void_v<typename T::template rebind_base<void>>> {};
  7016. } // namespace detail
  7017. template <typename T>
  7018. using is_base_rebindable_non_void = meta::is_detected<detail::is_base_rebindable_test, T>;
  7019. template <typename T>
  7020. inline constexpr bool is_base_rebindable_non_void_v = is_base_rebindable_non_void<T>::value;
  7021. } // namespace sol
  7022. // end of sol/unique_usertype_traits.hpp
  7023. namespace sol {
  7024. template <typename... Args>
  7025. struct base_list {};
  7026. template <typename... Args>
  7027. using bases = base_list<Args...>;
  7028. typedef bases<> base_classes_tag;
  7029. const auto base_classes = base_classes_tag();
  7030. template <typename... Args>
  7031. struct is_to_stringable<base_list<Args...>> : std::false_type {};
  7032. namespace detail {
  7033. inline decltype(auto) base_class_check_key() {
  7034. static const auto& key = "class_check";
  7035. return key;
  7036. }
  7037. inline decltype(auto) base_class_cast_key() {
  7038. static const auto& key = "class_cast";
  7039. return key;
  7040. }
  7041. inline decltype(auto) base_class_index_propogation_key() {
  7042. static const auto& key = u8"\xF0\x9F\x8C\xB2.index";
  7043. return key;
  7044. }
  7045. inline decltype(auto) base_class_new_index_propogation_key() {
  7046. static const auto& key = u8"\xF0\x9F\x8C\xB2.new_index";
  7047. return key;
  7048. }
  7049. template <typename T>
  7050. struct inheritance {
  7051. typedef typename base<T>::type bases_t;
  7052. static bool type_check_bases(types<>, const string_view&) {
  7053. return false;
  7054. }
  7055. template <typename Base, typename... Args>
  7056. static bool type_check_bases(types<Base, Args...>, const string_view& ti) {
  7057. return ti == usertype_traits<Base>::qualified_name() || type_check_bases(types<Args...>(), ti);
  7058. }
  7059. static bool type_check(const string_view& ti) {
  7060. return ti == usertype_traits<T>::qualified_name() || type_check_bases(bases_t(), ti);
  7061. }
  7062. template <typename ...Bases>
  7063. static bool type_check_with(const string_view& ti) {
  7064. return ti == usertype_traits<T>::qualified_name() || type_check_bases(types<Bases...>(), ti);
  7065. }
  7066. static void* type_cast_bases(types<>, T*, const string_view&) {
  7067. return nullptr;
  7068. }
  7069. template <typename Base, typename... Args>
  7070. static void* type_cast_bases(types<Base, Args...>, T* data, const string_view& ti) {
  7071. // Make sure to convert to T first, and then dynamic cast to the proper type
  7072. return ti != usertype_traits<Base>::qualified_name() ? type_cast_bases(types<Args...>(), data, ti) : static_cast<void*>(static_cast<Base*>(data));
  7073. }
  7074. static void* type_cast(void* voiddata, const string_view& ti) {
  7075. T* data = static_cast<T*>(voiddata);
  7076. return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(bases_t(), data, ti) : data);
  7077. }
  7078. template <typename... Bases>
  7079. static void* type_cast_with(void* voiddata, const string_view& ti) {
  7080. T* data = static_cast<T*>(voiddata);
  7081. return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(types<Bases...>(), data, ti) : data);
  7082. }
  7083. template <typename U>
  7084. static bool type_unique_cast_bases(types<>, void*, void*, const string_view&) {
  7085. return 0;
  7086. }
  7087. template <typename U, typename Base, typename... Args>
  7088. static int type_unique_cast_bases(types<Base, Args...>, void* source_data, void* target_data, const string_view& ti) {
  7089. using uu_traits = unique_usertype_traits<U>;
  7090. using base_ptr = typename uu_traits::template rebind_base<Base>;
  7091. string_view base_ti = usertype_traits<Base>::qualified_name();
  7092. if (base_ti == ti) {
  7093. if (target_data != nullptr) {
  7094. U* source = static_cast<U*>(source_data);
  7095. base_ptr* target = static_cast<base_ptr*>(target_data);
  7096. // perform proper derived -> base conversion
  7097. *target = *source;
  7098. }
  7099. return 2;
  7100. }
  7101. return type_unique_cast_bases<U>(types<Args...>(), source_data, target_data, ti);
  7102. }
  7103. template <typename U>
  7104. static int type_unique_cast(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
  7105. typedef unique_usertype_traits<U> uu_traits;
  7106. if constexpr (is_base_rebindable_v<uu_traits>) {
  7107. typedef typename uu_traits::template rebind_base<void> rebind_t;
  7108. typedef meta::conditional_t<std::is_void<rebind_t>::value, types<>, bases_t> cond_bases_t;
  7109. string_view this_rebind_ti = usertype_traits<rebind_t>::qualified_name();
  7110. if (rebind_ti != this_rebind_ti) {
  7111. // this is not even of the same unique type
  7112. return 0;
  7113. }
  7114. string_view this_ti = usertype_traits<T>::qualified_name();
  7115. if (ti == this_ti) {
  7116. // direct match, return 1
  7117. return 1;
  7118. }
  7119. return type_unique_cast_bases<U>(cond_bases_t(), source_data, target_data, ti);
  7120. }
  7121. else {
  7122. (void)rebind_ti;
  7123. string_view this_ti = usertype_traits<T>::qualified_name();
  7124. if (ti == this_ti) {
  7125. // direct match, return 1
  7126. return 1;
  7127. }
  7128. return type_unique_cast_bases<U>(types<>(), source_data, target_data, ti);
  7129. }
  7130. }
  7131. template <typename U, typename... Bases>
  7132. static int type_unique_cast_with(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
  7133. using uc_bases_t = types<Bases...>;
  7134. typedef unique_usertype_traits<U> uu_traits;
  7135. if constexpr (is_base_rebindable_v<uu_traits>) {
  7136. using rebind_t = typename uu_traits::template rebind_base<void>;
  7137. using cond_bases_t = meta::conditional_t<std::is_void<rebind_t>::value, types<>, uc_bases_t>;
  7138. string_view this_rebind_ti = usertype_traits<rebind_t>::qualified_name();
  7139. if (rebind_ti != this_rebind_ti) {
  7140. // this is not even of the same unique type
  7141. return 0;
  7142. }
  7143. string_view this_ti = usertype_traits<T>::qualified_name();
  7144. if (ti == this_ti) {
  7145. // direct match, return 1
  7146. return 1;
  7147. }
  7148. return type_unique_cast_bases<U>(cond_bases_t(), source_data, target_data, ti);
  7149. }
  7150. else {
  7151. (void)rebind_ti;
  7152. string_view this_ti = usertype_traits<T>::qualified_name();
  7153. if (ti == this_ti) {
  7154. // direct match, return 1
  7155. return 1;
  7156. }
  7157. return type_unique_cast_bases<U>(types<>(), source_data, target_data, ti);
  7158. }
  7159. }
  7160. };
  7161. using inheritance_check_function = decltype(&inheritance<void>::type_check);
  7162. using inheritance_cast_function = decltype(&inheritance<void>::type_cast);
  7163. using inheritance_unique_cast_function = decltype(&inheritance<void>::type_unique_cast<void>);
  7164. } // namespace detail
  7165. } // namespace sol
  7166. // end of sol/inheritance.hpp
  7167. // beginning of sol/error_handler.hpp
  7168. #include <cstdio>
  7169. namespace sol {
  7170. namespace detail {
  7171. constexpr const char* not_a_number = "not a numeric type";
  7172. constexpr const char* not_a_number_or_number_string = "not a numeric type or numeric string";
  7173. constexpr const char* not_a_number_integral = "not a numeric type that fits exactly an integer (number maybe has significant decimals)";
  7174. constexpr const char* not_a_number_or_number_string_integral
  7175. = "not a numeric type or a numeric string that fits exactly an integer (e.g. number maybe has significant decimals)";
  7176. constexpr const char* not_enough_stack_space = "not enough space left on Lua stack";
  7177. constexpr const char* not_enough_stack_space_floating = "not enough space left on Lua stack for a floating point number";
  7178. constexpr const char* not_enough_stack_space_integral = "not enough space left on Lua stack for an integral number";
  7179. constexpr const char* not_enough_stack_space_string = "not enough space left on Lua stack for a string";
  7180. constexpr const char* not_enough_stack_space_meta_function_name = "not enough space left on Lua stack for the name of a meta_function";
  7181. constexpr const char* not_enough_stack_space_userdata = "not enough space left on Lua stack to create a sol3 userdata";
  7182. constexpr const char* not_enough_stack_space_generic = "not enough space left on Lua stack to push valuees";
  7183. constexpr const char* not_enough_stack_space_environment = "not enough space left on Lua stack to retrieve environment";
  7184. constexpr const char* protected_function_error = "caught (...) unknown error during protected_function call";
  7185. inline void accumulate_and_mark(const std::string& n, std::string& aux_message, int& marker) {
  7186. if (marker > 0) {
  7187. aux_message += ", ";
  7188. }
  7189. aux_message += n;
  7190. ++marker;
  7191. }
  7192. } // namespace detail
  7193. inline std::string associated_type_name(lua_State* L, int index, type t) {
  7194. switch (t) {
  7195. case type::poly:
  7196. return "anything";
  7197. case type::userdata: {
  7198. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7199. luaL_checkstack(L, 2, "not enough space to push get the type name");
  7200. #endif // make sure stack doesn't overflow
  7201. if (lua_getmetatable(L, index) == 0) {
  7202. break;
  7203. }
  7204. lua_pushlstring(L, "__name", 6);
  7205. lua_rawget(L, -2);
  7206. size_t sz;
  7207. const char* name = lua_tolstring(L, -1, &sz);
  7208. std::string tn(name, static_cast<std::string::size_type>(sz));
  7209. lua_pop(L, 2);
  7210. return tn;
  7211. }
  7212. default:
  7213. break;
  7214. }
  7215. return lua_typename(L, static_cast<int>(t));
  7216. }
  7217. inline int push_type_panic_string(lua_State* L, int index, type expected, type actual, string_view message, string_view aux_message) noexcept {
  7218. const char* err = message.size() == 0
  7219. ? (aux_message.size() == 0 ? "stack index %d, expected %s, received %s" : "stack index %d, expected %s, received %s: %s")
  7220. : "stack index %d, expected %s, received %s: %s %s";
  7221. const char* type_name = expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(expected));
  7222. {
  7223. std::string actual_name = associated_type_name(L, index, actual);
  7224. lua_pushfstring(L, err, index, type_name, actual_name.c_str(), message.data(), aux_message.data());
  7225. }
  7226. return 1;
  7227. }
  7228. inline int type_panic_string(lua_State* L, int index, type expected, type actual, string_view message = "") noexcept(false) {
  7229. push_type_panic_string(L, index, expected, actual, message, "");
  7230. return lua_error(L);
  7231. }
  7232. inline int type_panic_c_str(lua_State* L, int index, type expected, type actual, const char* message = nullptr) noexcept(false) {
  7233. push_type_panic_string(L, index, expected, actual, message == nullptr ? "" : message, "");
  7234. return lua_error(L);
  7235. }
  7236. struct type_panic_t {
  7237. int operator()(lua_State* L, int index, type expected, type actual) const noexcept(false) {
  7238. return type_panic_c_str(L, index, expected, actual, nullptr);
  7239. }
  7240. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  7241. return type_panic_c_str(L, index, expected, actual, message.data());
  7242. }
  7243. };
  7244. const type_panic_t type_panic = {};
  7245. struct constructor_handler {
  7246. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  7247. push_type_panic_string(L, index, expected, actual, message, "(type check failed in constructor)");
  7248. return lua_error(L);
  7249. }
  7250. };
  7251. template <typename F = void>
  7252. struct argument_handler {
  7253. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  7254. push_type_panic_string(L, index, expected, actual, message, "(bad argument to variable or function call)");
  7255. return lua_error(L);
  7256. }
  7257. };
  7258. template <typename R, typename... Args>
  7259. struct argument_handler<types<R, Args...>> {
  7260. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  7261. {
  7262. std::string aux_message = "(bad argument into '";
  7263. aux_message += detail::demangle<R>();
  7264. aux_message += "(";
  7265. int marker = 0;
  7266. (void)detail::swallow { int(), (detail::accumulate_and_mark(detail::demangle<Args>(), aux_message, marker), int())... };
  7267. aux_message += ")')";
  7268. push_type_panic_string(L, index, expected, actual, message, aux_message);
  7269. }
  7270. return lua_error(L);
  7271. }
  7272. };
  7273. // Specify this function as the handler for lua::check if you know there's nothing wrong
  7274. inline int no_panic(lua_State*, int, type, type, const char* = nullptr) noexcept {
  7275. return 0;
  7276. }
  7277. inline void type_error(lua_State* L, int expected, int actual) noexcept(false) {
  7278. luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual));
  7279. }
  7280. inline void type_error(lua_State* L, type expected, type actual) noexcept(false) {
  7281. type_error(L, static_cast<int>(expected), static_cast<int>(actual));
  7282. }
  7283. inline void type_assert(lua_State* L, int index, type expected, type actual) noexcept(false) {
  7284. if (expected != type::poly && expected != actual) {
  7285. type_panic_c_str(L, index, expected, actual, nullptr);
  7286. }
  7287. }
  7288. inline void type_assert(lua_State* L, int index, type expected) {
  7289. type actual = type_of(L, index);
  7290. type_assert(L, index, expected, actual);
  7291. }
  7292. } // namespace sol
  7293. // end of sol/error_handler.hpp
  7294. // beginning of sol/reference.hpp
  7295. // beginning of sol/stack_reference.hpp
  7296. namespace sol {
  7297. namespace detail {
  7298. inline bool xmovable(lua_State* leftL, lua_State* rightL) {
  7299. if (rightL == nullptr || leftL == nullptr || leftL == rightL) {
  7300. return false;
  7301. }
  7302. const void* leftregistry = lua_topointer(leftL, LUA_REGISTRYINDEX);
  7303. const void* rightregistry = lua_topointer(rightL, LUA_REGISTRYINDEX);
  7304. return leftregistry == rightregistry;
  7305. }
  7306. } // namespace detail
  7307. class stateless_stack_reference {
  7308. private:
  7309. friend class stack_reference;
  7310. int index = 0;
  7311. int registry_index() const noexcept {
  7312. return LUA_NOREF;
  7313. }
  7314. public:
  7315. stateless_stack_reference() noexcept = default;
  7316. stateless_stack_reference(lua_nil_t) noexcept : stateless_stack_reference(){};
  7317. stateless_stack_reference(lua_State* L, int i) noexcept : stateless_stack_reference(absolute_index(L, i)) {
  7318. }
  7319. stateless_stack_reference(lua_State*, absolute_index i) noexcept : stateless_stack_reference(i) {
  7320. }
  7321. stateless_stack_reference(lua_State*, raw_index i) noexcept : stateless_stack_reference(i) {
  7322. }
  7323. stateless_stack_reference(absolute_index i) noexcept : index(i) {
  7324. }
  7325. stateless_stack_reference(raw_index i) noexcept : index(i) {
  7326. }
  7327. stateless_stack_reference(lua_State*, ref_index) noexcept = delete;
  7328. stateless_stack_reference(ref_index) noexcept = delete;
  7329. stateless_stack_reference(const reference&) noexcept = delete;
  7330. stateless_stack_reference(const stateless_stack_reference&) noexcept = default;
  7331. stateless_stack_reference(stateless_stack_reference&& o) noexcept = default;
  7332. stateless_stack_reference& operator=(stateless_stack_reference&&) noexcept = default;
  7333. stateless_stack_reference& operator=(const stateless_stack_reference&) noexcept = default;
  7334. int push(lua_State* L) const noexcept {
  7335. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7336. luaL_checkstack(L, 1, "not enough Lua stack space to push a single reference value");
  7337. #endif // make sure stack doesn't overflow
  7338. lua_pushvalue(L, index);
  7339. return 1;
  7340. }
  7341. void pop(lua_State* L, int n = 1) const noexcept {
  7342. lua_pop(L, n);
  7343. }
  7344. int stack_index() const noexcept {
  7345. return index;
  7346. }
  7347. const void* pointer(lua_State* L) const noexcept {
  7348. const void* vp = lua_topointer(L, stack_index());
  7349. return vp;
  7350. }
  7351. type get_type(lua_State* L) const noexcept {
  7352. int result = lua_type(L, index);
  7353. return static_cast<type>(result);
  7354. }
  7355. bool valid(lua_State* L) const noexcept {
  7356. type t = get_type(L);
  7357. return t != type::lua_nil && t != type::none;
  7358. }
  7359. void abandon(lua_State* = nullptr) {
  7360. index = 0;
  7361. }
  7362. };
  7363. class stack_reference : public stateless_stack_reference {
  7364. private:
  7365. lua_State* luastate = nullptr;
  7366. public:
  7367. stack_reference() noexcept = default;
  7368. stack_reference(lua_nil_t) noexcept
  7369. : stack_reference() {};
  7370. stack_reference(lua_State* L, lua_nil_t) noexcept : stateless_stack_reference(L, 0), luastate(L) {
  7371. }
  7372. stack_reference(lua_State* L, int i) noexcept : stateless_stack_reference(L, i), luastate(L) {
  7373. }
  7374. stack_reference(lua_State* L, absolute_index i) noexcept : stateless_stack_reference(L, i), luastate(L) {
  7375. }
  7376. stack_reference(lua_State* L, raw_index i) noexcept : stateless_stack_reference(L, i), luastate(L) {
  7377. }
  7378. stack_reference(lua_State* L, ref_index i) noexcept = delete;
  7379. stack_reference(lua_State* L, const reference& r) noexcept = delete;
  7380. stack_reference(lua_State* L, const stack_reference& r) noexcept
  7381. : luastate(L) {
  7382. if (!r.valid()) {
  7383. index = 0;
  7384. return;
  7385. }
  7386. int i = r.stack_index();
  7387. if (detail::xmovable(lua_state(), r.lua_state())) {
  7388. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7389. luaL_checkstack(L, 1, "not enough Lua stack space to push a single reference value");
  7390. #endif // make sure stack doesn't overflow
  7391. lua_pushvalue(r.lua_state(), r.index);
  7392. lua_xmove(r.lua_state(), luastate, 1);
  7393. i = absolute_index(luastate, -1);
  7394. }
  7395. index = i;
  7396. }
  7397. stack_reference(stack_reference&& o) noexcept = default;
  7398. stack_reference& operator=(stack_reference&&) noexcept = default;
  7399. stack_reference(const stack_reference&) noexcept = default;
  7400. stack_reference& operator=(const stack_reference&) noexcept = default;
  7401. int push() const noexcept {
  7402. return push(lua_state());
  7403. }
  7404. int push(lua_State* Ls) const noexcept {
  7405. return stateless_stack_reference::push(Ls);
  7406. }
  7407. void pop() const noexcept {
  7408. pop(lua_state());
  7409. }
  7410. void pop(lua_State* Ls, int n = 1) const noexcept {
  7411. stateless_stack_reference::pop(Ls, n);
  7412. }
  7413. const void* pointer() const noexcept {
  7414. return stateless_stack_reference::pointer(lua_state());
  7415. }
  7416. type get_type() const noexcept {
  7417. return stateless_stack_reference::get_type(lua_state());
  7418. }
  7419. lua_State* lua_state() const noexcept {
  7420. return luastate;
  7421. }
  7422. bool valid() const noexcept {
  7423. return stateless_stack_reference::valid(lua_state());
  7424. }
  7425. void abandon () {
  7426. stateless_stack_reference::abandon(lua_state());
  7427. }
  7428. };
  7429. inline bool operator==(const stack_reference& l, const stack_reference& r) {
  7430. return lua_compare(l.lua_state(), l.stack_index(), r.stack_index(), LUA_OPEQ) == 0;
  7431. }
  7432. inline bool operator!=(const stack_reference& l, const stack_reference& r) {
  7433. return !operator==(l, r);
  7434. }
  7435. inline bool operator==(const stack_reference& lhs, const lua_nil_t&) {
  7436. return !lhs.valid();
  7437. }
  7438. inline bool operator==(const lua_nil_t&, const stack_reference& rhs) {
  7439. return !rhs.valid();
  7440. }
  7441. inline bool operator!=(const stack_reference& lhs, const lua_nil_t&) {
  7442. return lhs.valid();
  7443. }
  7444. inline bool operator!=(const lua_nil_t&, const stack_reference& rhs) {
  7445. return rhs.valid();
  7446. }
  7447. struct stack_reference_equals {
  7448. bool operator()(const lua_nil_t& lhs, const stack_reference& rhs) const {
  7449. return lhs == rhs;
  7450. }
  7451. bool operator()(const stack_reference& lhs, const lua_nil_t& rhs) const {
  7452. return lhs == rhs;
  7453. }
  7454. bool operator()(const stack_reference& lhs, const stack_reference& rhs) const {
  7455. return lhs == rhs;
  7456. }
  7457. };
  7458. struct stack_reference_hash {
  7459. typedef stack_reference argument_type;
  7460. typedef std::size_t result_type;
  7461. result_type operator()(const argument_type& lhs) const {
  7462. std::hash<const void*> h;
  7463. return h(lhs.pointer());
  7464. }
  7465. };
  7466. } // namespace sol
  7467. // end of sol/stack_reference.hpp
  7468. #include <functional>
  7469. namespace sol {
  7470. namespace detail {
  7471. inline const char (&default_main_thread_name())[9] {
  7472. static const char name[9] = "sol.\xF0\x9F\x93\x8C";
  7473. return name;
  7474. }
  7475. } // namespace detail
  7476. namespace stack {
  7477. inline void remove(lua_State* L, int rawindex, int count) {
  7478. if (count < 1)
  7479. return;
  7480. int top = lua_gettop(L);
  7481. if (top < 1) {
  7482. return;
  7483. }
  7484. if (rawindex == -count || top == rawindex) {
  7485. // Slice them right off the top
  7486. lua_pop(L, static_cast<int>(count));
  7487. return;
  7488. }
  7489. // Remove each item one at a time using stack operations
  7490. // Probably slower, maybe, haven't benchmarked,
  7491. // but necessary
  7492. int index = lua_absindex(L, rawindex);
  7493. if (index < 0) {
  7494. index = lua_gettop(L) + (index + 1);
  7495. }
  7496. int last = index + count;
  7497. for (int i = index; i < last; ++i) {
  7498. lua_remove(L, index);
  7499. }
  7500. }
  7501. struct push_popper_at {
  7502. lua_State* L;
  7503. int index;
  7504. int count;
  7505. push_popper_at(lua_State* luastate, int index = -1, int count = 1) : L(luastate), index(index), count(count) {
  7506. }
  7507. ~push_popper_at() {
  7508. remove(L, index, count);
  7509. }
  7510. };
  7511. template <bool top_level>
  7512. struct push_popper_n {
  7513. lua_State* L;
  7514. int t;
  7515. push_popper_n(lua_State* luastate, int x) : L(luastate), t(x) {
  7516. }
  7517. push_popper_n(const push_popper_n&) = delete;
  7518. push_popper_n(push_popper_n&&) = default;
  7519. push_popper_n& operator=(const push_popper_n&) = delete;
  7520. push_popper_n& operator=(push_popper_n&&) = default;
  7521. ~push_popper_n() {
  7522. lua_pop(L, t);
  7523. }
  7524. };
  7525. template <>
  7526. struct push_popper_n<true> {
  7527. push_popper_n(lua_State*, int) {
  7528. }
  7529. };
  7530. template <bool, typename T, typename = void>
  7531. struct push_popper {
  7532. using Tu = meta::unqualified_t<T>;
  7533. T t;
  7534. int idx;
  7535. push_popper(T x) : t(x), idx(lua_absindex(t.lua_state(), -t.push())) {
  7536. }
  7537. int index_of(const Tu&) {
  7538. return idx;
  7539. }
  7540. ~push_popper() {
  7541. t.pop();
  7542. }
  7543. };
  7544. template <typename T, typename C>
  7545. struct push_popper<true, T, C> {
  7546. using Tu = meta::unqualified_t<T>;
  7547. push_popper(T) {
  7548. }
  7549. int index_of(const Tu&) {
  7550. return -1;
  7551. }
  7552. ~push_popper() {
  7553. }
  7554. };
  7555. template <typename T>
  7556. struct push_popper<false, T, std::enable_if_t<is_stack_based_v<meta::unqualified_t<T>>>> {
  7557. using Tu = meta::unqualified_t<T>;
  7558. push_popper(T) {
  7559. }
  7560. int index_of(const Tu& r) {
  7561. return r.stack_index();
  7562. }
  7563. ~push_popper() {
  7564. }
  7565. };
  7566. template <bool top_level = false, typename T>
  7567. push_popper<top_level, T> push_pop(T&& x) {
  7568. return push_popper<top_level, T>(std::forward<T>(x));
  7569. }
  7570. template <typename T>
  7571. push_popper_at push_pop_at(T&& x) {
  7572. int c = x.push();
  7573. lua_State* L = x.lua_state();
  7574. return push_popper_at(L, lua_absindex(L, -c), c);
  7575. }
  7576. template <bool top_level = false>
  7577. push_popper_n<top_level> pop_n(lua_State* L, int x) {
  7578. return push_popper_n<top_level>(L, x);
  7579. }
  7580. } // namespace stack
  7581. inline lua_State* main_thread(lua_State* L, lua_State* backup_if_unsupported = nullptr) {
  7582. #if SOL_LUA_VESION_I_ < 502
  7583. if (L == nullptr)
  7584. return backup_if_unsupported;
  7585. lua_getglobal(L, detail::default_main_thread_name());
  7586. auto pp = stack::pop_n(L, 1);
  7587. if (type_of(L, -1) == type::thread) {
  7588. return lua_tothread(L, -1);
  7589. }
  7590. return backup_if_unsupported;
  7591. #else
  7592. if (L == nullptr)
  7593. return backup_if_unsupported;
  7594. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
  7595. lua_State* Lmain = lua_tothread(L, -1);
  7596. lua_pop(L, 1);
  7597. return Lmain;
  7598. #endif // Lua 5.2+ has the main thread unqualified_getter
  7599. }
  7600. namespace detail {
  7601. struct global_tag {
  7602. } const global_ {};
  7603. struct no_safety_tag {
  7604. } const no_safety {};
  7605. template <bool b>
  7606. inline lua_State* pick_main_thread(lua_State* L, lua_State* backup_if_unsupported = nullptr) {
  7607. (void)L;
  7608. (void)backup_if_unsupported;
  7609. if (b) {
  7610. return main_thread(L, backup_if_unsupported);
  7611. }
  7612. return L;
  7613. }
  7614. } // namespace detail
  7615. class stateless_reference {
  7616. private:
  7617. template <bool o_main_only>
  7618. friend class basic_reference;
  7619. int ref = LUA_NOREF;
  7620. int copy(lua_State* L) const noexcept {
  7621. if (ref == LUA_NOREF)
  7622. return LUA_NOREF;
  7623. push(L);
  7624. return luaL_ref(L, LUA_REGISTRYINDEX);
  7625. }
  7626. lua_State* copy_assign(lua_State* L, lua_State* rL, const stateless_reference& r) {
  7627. if (valid(L)) {
  7628. deref(L);
  7629. }
  7630. ref = r.copy(L);
  7631. return rL;
  7632. }
  7633. lua_State* move_assign(lua_State* L, lua_State* rL, stateless_reference&& r) {
  7634. if (valid(L)) {
  7635. deref(L);
  7636. }
  7637. ref = r.ref;
  7638. r.ref = LUA_NOREF;
  7639. return rL;
  7640. }
  7641. protected:
  7642. int stack_index() const noexcept {
  7643. return -1;
  7644. }
  7645. stateless_reference(lua_State* L, detail::global_tag) noexcept {
  7646. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7647. luaL_checkstack(L, 1, "not enough Lua stack space to push this reference value");
  7648. #endif // make sure stack doesn't overflow
  7649. lua_pushglobaltable(L);
  7650. ref = luaL_ref(L, LUA_REGISTRYINDEX);
  7651. }
  7652. stateless_reference(int raw_ref_index) noexcept : ref(raw_ref_index) {
  7653. }
  7654. public:
  7655. stateless_reference() noexcept = default;
  7656. stateless_reference(lua_nil_t) noexcept : stateless_reference() {
  7657. }
  7658. stateless_reference(const stack_reference& r) noexcept : stateless_reference(r.lua_state(), r.stack_index()) {
  7659. }
  7660. stateless_reference(stack_reference&& r) noexcept : stateless_reference(r.lua_state(), r.stack_index()) {
  7661. }
  7662. stateless_reference(lua_State* L, const stateless_reference& r) noexcept {
  7663. if (r.ref == LUA_REFNIL) {
  7664. ref = LUA_REFNIL;
  7665. return;
  7666. }
  7667. if (r.ref == LUA_NOREF || L == nullptr) {
  7668. ref = LUA_NOREF;
  7669. return;
  7670. }
  7671. ref = r.copy(L);
  7672. }
  7673. stateless_reference(lua_State* L, stateless_reference&& r) noexcept {
  7674. if (r.ref == LUA_REFNIL) {
  7675. ref = LUA_REFNIL;
  7676. return;
  7677. }
  7678. if (r.ref == LUA_NOREF || L == nullptr) {
  7679. ref = LUA_NOREF;
  7680. return;
  7681. }
  7682. ref = r.ref;
  7683. r.ref = LUA_NOREF;
  7684. }
  7685. stateless_reference(lua_State* L, const stack_reference& r) noexcept {
  7686. if (L == nullptr || r.lua_state() == nullptr || r.get_type() == type::none) {
  7687. ref = LUA_NOREF;
  7688. return;
  7689. }
  7690. if (r.get_type() == type::lua_nil) {
  7691. ref = LUA_REFNIL;
  7692. return;
  7693. }
  7694. if (L != r.lua_state() && !detail::xmovable(L, r.lua_state())) {
  7695. return;
  7696. }
  7697. r.push(L);
  7698. ref = luaL_ref(L, LUA_REGISTRYINDEX);
  7699. }
  7700. stateless_reference(lua_State* L, int index = -1) noexcept {
  7701. // use L to stick with that state's execution stack
  7702. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7703. luaL_checkstack(L, 1, "not enough Lua stack space to push this reference value");
  7704. #endif // make sure stack doesn't overflow
  7705. lua_pushvalue(L, index);
  7706. ref = luaL_ref(L, LUA_REGISTRYINDEX);
  7707. }
  7708. stateless_reference(lua_State* L, ref_index index) noexcept {
  7709. lua_rawgeti(L, LUA_REGISTRYINDEX, index.index);
  7710. ref = luaL_ref(L, LUA_REGISTRYINDEX);
  7711. }
  7712. stateless_reference(lua_State*, lua_nil_t) noexcept {
  7713. }
  7714. ~stateless_reference() noexcept = default;
  7715. stateless_reference(const stateless_reference& o) noexcept = delete;
  7716. stateless_reference& operator=(const stateless_reference& r) noexcept = delete;
  7717. stateless_reference(stateless_reference&& o) noexcept : ref(o.ref) {
  7718. o.ref = LUA_NOREF;
  7719. }
  7720. stateless_reference& operator=(stateless_reference&& o) noexcept {
  7721. ref = o.ref;
  7722. o.ref = LUA_NOREF;
  7723. return *this;
  7724. }
  7725. int push(lua_State* L) const noexcept {
  7726. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7727. luaL_checkstack(L, 1, "not enough Lua stack space to push this reference value");
  7728. #endif // make sure stack doesn't overflow
  7729. lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
  7730. return 1;
  7731. }
  7732. void pop(lua_State* L, int n = 1) const noexcept {
  7733. lua_pop(L, n);
  7734. }
  7735. int registry_index() const noexcept {
  7736. return ref;
  7737. }
  7738. bool valid(lua_State*) const noexcept {
  7739. return !(ref == LUA_NOREF || ref == LUA_REFNIL);
  7740. }
  7741. const void* pointer(lua_State* L) const noexcept {
  7742. int si = push(L);
  7743. const void* vp = lua_topointer(L, -si);
  7744. lua_pop(L, si);
  7745. return vp;
  7746. }
  7747. type get_type(lua_State* L) const noexcept {
  7748. int p = push(L);
  7749. int result = lua_type(L, -1);
  7750. pop(L, p);
  7751. return static_cast<type>(result);
  7752. }
  7753. void abandon(lua_State* = nullptr) {
  7754. ref = LUA_NOREF;
  7755. }
  7756. void deref(lua_State* L) const noexcept {
  7757. luaL_unref(L, LUA_REGISTRYINDEX, ref);
  7758. }
  7759. };
  7760. template <bool main_only = false>
  7761. class basic_reference : public stateless_reference {
  7762. private:
  7763. template <bool o_main_only>
  7764. friend class basic_reference;
  7765. lua_State* luastate = nullptr; // non-owning
  7766. template <bool r_main_only>
  7767. void copy_assign(const basic_reference<r_main_only>& r) {
  7768. if (valid()) {
  7769. deref();
  7770. }
  7771. if (r.ref == LUA_REFNIL) {
  7772. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  7773. ref = LUA_REFNIL;
  7774. return;
  7775. }
  7776. if (r.ref == LUA_NOREF) {
  7777. luastate = r.luastate;
  7778. ref = LUA_NOREF;
  7779. return;
  7780. }
  7781. if (detail::xmovable(lua_state(), r.lua_state())) {
  7782. r.push(lua_state());
  7783. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  7784. return;
  7785. }
  7786. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  7787. ref = r.copy();
  7788. }
  7789. template <bool r_main_only>
  7790. void move_assign(basic_reference<r_main_only>&& r) {
  7791. if (valid()) {
  7792. deref();
  7793. }
  7794. if (r.ref == LUA_REFNIL) {
  7795. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  7796. ref = LUA_REFNIL;
  7797. return;
  7798. }
  7799. if (r.ref == LUA_NOREF) {
  7800. luastate = r.luastate;
  7801. ref = LUA_NOREF;
  7802. return;
  7803. }
  7804. if (detail::xmovable(lua_state(), r.lua_state())) {
  7805. r.push(lua_state());
  7806. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  7807. return;
  7808. }
  7809. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  7810. ref = r.ref;
  7811. r.ref = LUA_NOREF;
  7812. r.luastate = nullptr;
  7813. }
  7814. protected:
  7815. basic_reference(lua_State* L, detail::global_tag) noexcept
  7816. : basic_reference(detail::pick_main_thread<main_only>(L, L), detail::global_, detail::global_) {
  7817. }
  7818. basic_reference(lua_State* L, detail::global_tag, detail::global_tag) noexcept : stateless_reference(L, detail::global_), luastate(L) {
  7819. }
  7820. basic_reference(lua_State* oL, const basic_reference<!main_only>& o) noexcept : stateless_reference(oL, o), luastate(oL) {
  7821. }
  7822. void deref() const noexcept {
  7823. return stateless_reference::deref(lua_state());
  7824. }
  7825. int copy() const noexcept {
  7826. return copy(lua_state());
  7827. }
  7828. int copy(lua_State* L) const noexcept {
  7829. return stateless_reference::copy(L);
  7830. }
  7831. public:
  7832. basic_reference() noexcept = default;
  7833. basic_reference(lua_nil_t) noexcept : basic_reference() {
  7834. }
  7835. basic_reference(const stack_reference& r) noexcept : basic_reference(r.lua_state(), r.stack_index()) {
  7836. }
  7837. basic_reference(stack_reference&& r) noexcept : basic_reference(r.lua_state(), r.stack_index()) {
  7838. }
  7839. template <bool r_main_only>
  7840. basic_reference(lua_State* L, const basic_reference<r_main_only>& r) noexcept : luastate(detail::pick_main_thread<main_only>(L, L)) {
  7841. if (r.ref == LUA_REFNIL) {
  7842. ref = LUA_REFNIL;
  7843. return;
  7844. }
  7845. if (r.ref == LUA_NOREF || lua_state() == nullptr) {
  7846. ref = LUA_NOREF;
  7847. return;
  7848. }
  7849. if (detail::xmovable(lua_state(), r.lua_state())) {
  7850. r.push(lua_state());
  7851. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  7852. return;
  7853. }
  7854. ref = r.copy();
  7855. }
  7856. template <bool r_main_only>
  7857. basic_reference(lua_State* L, basic_reference<r_main_only>&& r) noexcept : luastate(detail::pick_main_thread<main_only>(L, L)) {
  7858. if (r.ref == LUA_REFNIL) {
  7859. ref = LUA_REFNIL;
  7860. return;
  7861. }
  7862. if (r.ref == LUA_NOREF || lua_state() == nullptr) {
  7863. ref = LUA_NOREF;
  7864. return;
  7865. }
  7866. if (detail::xmovable(lua_state(), r.lua_state())) {
  7867. r.push(lua_state());
  7868. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  7869. return;
  7870. }
  7871. ref = r.ref;
  7872. r.ref = LUA_NOREF;
  7873. r.luastate = nullptr;
  7874. }
  7875. basic_reference(lua_State* L, const stack_reference& r) noexcept : luastate(detail::pick_main_thread<main_only>(L, L)) {
  7876. if (lua_state() == nullptr || r.lua_state() == nullptr || r.get_type() == type::none) {
  7877. ref = LUA_NOREF;
  7878. return;
  7879. }
  7880. if (r.get_type() == type::lua_nil) {
  7881. ref = LUA_REFNIL;
  7882. return;
  7883. }
  7884. if (lua_state() != r.lua_state() && !detail::xmovable(lua_state(), r.lua_state())) {
  7885. return;
  7886. }
  7887. r.push(lua_state());
  7888. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  7889. }
  7890. basic_reference(lua_State* L, int index = -1) noexcept : luastate(detail::pick_main_thread<main_only>(L, L)) {
  7891. // use L to stick with that state's execution stack
  7892. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7893. luaL_checkstack(L, 1, "not enough Lua stack space to push this reference value");
  7894. #endif // make sure stack doesn't overflow
  7895. lua_pushvalue(L, index);
  7896. ref = luaL_ref(L, LUA_REGISTRYINDEX);
  7897. }
  7898. basic_reference(lua_State* L, ref_index index) noexcept : luastate(detail::pick_main_thread<main_only>(L, L)) {
  7899. lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, index.index);
  7900. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  7901. }
  7902. basic_reference(lua_State* L, lua_nil_t) noexcept : luastate(detail::pick_main_thread<main_only>(L, L)) {
  7903. }
  7904. ~basic_reference() noexcept {
  7905. if (lua_state() == nullptr || ref == LUA_NOREF)
  7906. return;
  7907. deref();
  7908. }
  7909. basic_reference(const basic_reference& o) noexcept : stateless_reference(o.copy()), luastate(o.lua_state()) {
  7910. }
  7911. basic_reference(basic_reference&& o) noexcept : stateless_reference(std::move(o)), luastate(o.lua_state()) {
  7912. o.luastate = nullptr;
  7913. }
  7914. basic_reference(const basic_reference<!main_only>& o) noexcept
  7915. : basic_reference(detail::pick_main_thread<main_only>(o.lua_state(), o.lua_state()), o) {
  7916. }
  7917. basic_reference(basic_reference<!main_only>&& o) noexcept
  7918. : stateless_reference(std::move(o)), luastate(detail::pick_main_thread<main_only>(o.lua_state(), o.lua_state())) {
  7919. o.luastate = nullptr;
  7920. o.ref = LUA_NOREF;
  7921. }
  7922. basic_reference& operator=(basic_reference&& r) noexcept {
  7923. move_assign(std::move(r));
  7924. return *this;
  7925. }
  7926. basic_reference& operator=(const basic_reference& r) noexcept {
  7927. copy_assign(r);
  7928. return *this;
  7929. }
  7930. basic_reference& operator=(basic_reference<!main_only>&& r) noexcept {
  7931. move_assign(std::move(r));
  7932. return *this;
  7933. }
  7934. basic_reference& operator=(const basic_reference<!main_only>& r) noexcept {
  7935. copy_assign(r);
  7936. return *this;
  7937. }
  7938. basic_reference& operator=(const lua_nil_t&) noexcept {
  7939. if (valid()) {
  7940. deref();
  7941. }
  7942. luastate = nullptr;
  7943. ref = LUA_NOREF;
  7944. return *this;
  7945. }
  7946. template <typename Super>
  7947. basic_reference& operator=(proxy_base<Super>&& r);
  7948. template <typename Super>
  7949. basic_reference& operator=(const proxy_base<Super>& r);
  7950. int push() const noexcept {
  7951. return push(lua_state());
  7952. }
  7953. int push(lua_State* L) const noexcept {
  7954. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  7955. luaL_checkstack(L, 1, "not enough Lua stack space to push this reference value");
  7956. #endif // make sure stack doesn't overflow
  7957. if (lua_state() == nullptr) {
  7958. lua_pushnil(L);
  7959. return 1;
  7960. }
  7961. lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, ref);
  7962. if (L != lua_state()) {
  7963. lua_xmove(lua_state(), L, 1);
  7964. }
  7965. return 1;
  7966. }
  7967. void pop() const noexcept {
  7968. pop(lua_state());
  7969. }
  7970. void pop(lua_State* L, int n = 1) const noexcept {
  7971. stateless_reference::pop(L, n);
  7972. }
  7973. int registry_index() const noexcept {
  7974. return stateless_reference::registry_index();
  7975. }
  7976. bool valid() const noexcept {
  7977. return stateless_reference::valid(lua_state());
  7978. }
  7979. const void* pointer() const noexcept {
  7980. return stateless_reference::pointer(lua_state());
  7981. }
  7982. explicit operator bool() const noexcept {
  7983. return valid();
  7984. }
  7985. type get_type() const noexcept {
  7986. return stateless_reference::get_type(lua_state());
  7987. }
  7988. lua_State* lua_state() const noexcept {
  7989. return luastate;
  7990. }
  7991. };
  7992. template <bool lb, bool rb>
  7993. inline bool operator==(const basic_reference<lb>& l, const basic_reference<rb>& r) {
  7994. auto ppl = stack::push_pop(l);
  7995. auto ppr = stack::push_pop(r);
  7996. return lua_compare(l.lua_state(), -1, -2, LUA_OPEQ) == 1;
  7997. }
  7998. template <bool lb, bool rb>
  7999. inline bool operator!=(const basic_reference<lb>& l, const basic_reference<rb>& r) {
  8000. return !operator==(l, r);
  8001. }
  8002. template <bool lb>
  8003. inline bool operator==(const basic_reference<lb>& l, const stack_reference& r) {
  8004. auto ppl = stack::push_pop(l);
  8005. return lua_compare(l.lua_state(), -1, r.stack_index(), LUA_OPEQ) == 1;
  8006. }
  8007. template <bool lb>
  8008. inline bool operator!=(const basic_reference<lb>& l, const stack_reference& r) {
  8009. return !operator==(l, r);
  8010. }
  8011. template <bool rb>
  8012. inline bool operator==(const stack_reference& l, const basic_reference<rb>& r) {
  8013. auto ppr = stack::push_pop(r);
  8014. return lua_compare(l.lua_state(), -1, r.stack_index(), LUA_OPEQ) == 1;
  8015. }
  8016. template <bool rb>
  8017. inline bool operator!=(const stack_reference& l, const basic_reference<rb>& r) {
  8018. return !operator==(l, r);
  8019. }
  8020. template <bool lb>
  8021. inline bool operator==(const basic_reference<lb>& lhs, const lua_nil_t&) {
  8022. return !lhs.valid();
  8023. }
  8024. template <bool rb>
  8025. inline bool operator==(const lua_nil_t&, const basic_reference<rb>& rhs) {
  8026. return !rhs.valid();
  8027. }
  8028. template <bool lb>
  8029. inline bool operator!=(const basic_reference<lb>& lhs, const lua_nil_t&) {
  8030. return lhs.valid();
  8031. }
  8032. template <bool rb>
  8033. inline bool operator!=(const lua_nil_t&, const basic_reference<rb>& rhs) {
  8034. return rhs.valid();
  8035. }
  8036. struct reference_equals : public stack_reference_equals {
  8037. template <bool rb>
  8038. bool operator()(const lua_nil_t& lhs, const basic_reference<rb>& rhs) const {
  8039. return lhs == rhs;
  8040. }
  8041. template <bool lb>
  8042. bool operator()(const basic_reference<lb>& lhs, const lua_nil_t& rhs) const {
  8043. return lhs == rhs;
  8044. }
  8045. template <bool lb, bool rb>
  8046. bool operator()(const basic_reference<lb>& lhs, const basic_reference<rb>& rhs) const {
  8047. return lhs == rhs;
  8048. }
  8049. template <bool lb>
  8050. bool operator()(const basic_reference<lb>& lhs, const stack_reference& rhs) const {
  8051. return lhs == rhs;
  8052. }
  8053. template <bool rb>
  8054. bool operator()(const stack_reference& lhs, const basic_reference<rb>& rhs) const {
  8055. return lhs == rhs;
  8056. }
  8057. };
  8058. struct reference_hash : public stack_reference_hash {
  8059. typedef reference argument_type;
  8060. typedef std::size_t result_type;
  8061. template <bool lb>
  8062. result_type operator()(const basic_reference<lb>& lhs) const {
  8063. std::hash<const void*> h;
  8064. return h(lhs.pointer());
  8065. }
  8066. };
  8067. } // namespace sol
  8068. // end of sol/reference.hpp
  8069. // beginning of sol/tie.hpp
  8070. namespace sol {
  8071. namespace detail {
  8072. template <typename T>
  8073. struct is_speshul : std::false_type {};
  8074. } // namespace detail
  8075. template <typename T>
  8076. struct tie_size : std::tuple_size<T> {};
  8077. template <typename T>
  8078. struct is_tieable : std::integral_constant<bool, (::sol::tie_size<T>::value > 0)> {};
  8079. template <typename... Tn>
  8080. struct tie_t : public std::tuple<std::add_lvalue_reference_t<Tn>...> {
  8081. private:
  8082. typedef std::tuple<std::add_lvalue_reference_t<Tn>...> base_t;
  8083. template <typename T>
  8084. void set(std::false_type, T&& target) {
  8085. std::get<0>(*this) = std::forward<T>(target);
  8086. }
  8087. template <typename T>
  8088. void set(std::true_type, T&& target) {
  8089. typedef tie_size<meta::unqualified_t<T>> value_size;
  8090. typedef tie_size<std::tuple<Tn...>> tie_size;
  8091. typedef meta::conditional_t<(value_size::value < tie_size::value), value_size, tie_size> indices_size;
  8092. typedef std::make_index_sequence<indices_size::value> indices;
  8093. set_extra(detail::is_speshul<meta::unqualified_t<T>>(), indices(), std::forward<T>(target));
  8094. }
  8095. template <std::size_t... I, typename T>
  8096. void set_extra(std::true_type, std::index_sequence<I...>, T&& target) {
  8097. using std::get;
  8098. (void)detail::swallow{0,
  8099. (get<I>(static_cast<base_t&>(*this)) = get<I>(types<Tn...>(), target), 0)..., 0};
  8100. }
  8101. template <std::size_t... I, typename T>
  8102. void set_extra(std::false_type, std::index_sequence<I...>, T&& target) {
  8103. using std::get;
  8104. (void)detail::swallow{0,
  8105. (get<I>(static_cast<base_t&>(*this)) = get<I>(target), 0)..., 0};
  8106. }
  8107. public:
  8108. using base_t::base_t;
  8109. template <typename T>
  8110. tie_t& operator=(T&& value) {
  8111. typedef is_tieable<meta::unqualified_t<T>> tieable;
  8112. set(tieable(), std::forward<T>(value));
  8113. return *this;
  8114. }
  8115. };
  8116. template <typename... Tn>
  8117. struct tie_size<tie_t<Tn...>> : std::tuple_size<std::tuple<Tn...>> {};
  8118. namespace adl_barrier_detail {
  8119. template <typename... Tn>
  8120. inline tie_t<std::remove_reference_t<Tn>...> tie(Tn&&... argn) {
  8121. return tie_t<std::remove_reference_t<Tn>...>(std::forward<Tn>(argn)...);
  8122. }
  8123. } // namespace adl_barrier_detail
  8124. using namespace adl_barrier_detail;
  8125. } // namespace sol
  8126. // end of sol/tie.hpp
  8127. // beginning of sol/stack_guard.hpp
  8128. #include <functional>
  8129. namespace sol {
  8130. namespace detail {
  8131. inline void stack_fail(int, int) {
  8132. #if !(defined(SOL_NO_EXCEPTIONS) && SOL_NO_EXCEPTIONS)
  8133. throw error(detail::direct_error, "imbalanced stack after operation finish");
  8134. #else
  8135. // Lol, what do you want, an error printout? :3c
  8136. // There's no sane default here. The right way would be C-style abort(), and that's not acceptable, so
  8137. // hopefully someone will register their own stack_fail thing for the `fx` parameter of stack_guard.
  8138. #endif // No Exceptions
  8139. }
  8140. } // namespace detail
  8141. struct stack_guard {
  8142. lua_State* L;
  8143. int top;
  8144. std::function<void(int, int)> on_mismatch;
  8145. stack_guard(lua_State* L) : stack_guard(L, lua_gettop(L)) {
  8146. }
  8147. stack_guard(lua_State* L, int top, std::function<void(int, int)> fx = detail::stack_fail) : L(L), top(top), on_mismatch(std::move(fx)) {
  8148. }
  8149. bool check_stack(int modification = 0) const {
  8150. int bottom = lua_gettop(L) + modification;
  8151. if (top == bottom) {
  8152. return true;
  8153. }
  8154. on_mismatch(top, bottom);
  8155. return false;
  8156. }
  8157. ~stack_guard() {
  8158. check_stack();
  8159. }
  8160. };
  8161. } // namespace sol
  8162. // end of sol/stack_guard.hpp
  8163. #include <vector>
  8164. #include <bitset>
  8165. #include <forward_list>
  8166. #include <string>
  8167. #include <algorithm>
  8168. #include <sstream>
  8169. #include <optional>
  8170. namespace sol {
  8171. namespace detail {
  8172. struct with_function_tag { };
  8173. struct as_reference_tag { };
  8174. template <typename T>
  8175. struct as_pointer_tag { };
  8176. template <typename T>
  8177. struct as_value_tag { };
  8178. template <typename T>
  8179. struct as_unique_tag { };
  8180. template <typename T>
  8181. struct as_table_tag { };
  8182. using lua_reg_table = luaL_Reg[64];
  8183. using unique_destructor = void (*)(void*);
  8184. using unique_tag = detail::inheritance_unique_cast_function;
  8185. inline void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space, std::size_t& required_space) {
  8186. // this handels arbitrary alignments...
  8187. // make this into a power-of-2-only?
  8188. // actually can't: this is a C++14-compatible framework,
  8189. // power of 2 alignment is C++17
  8190. std::uintptr_t initial = reinterpret_cast<std::uintptr_t>(ptr);
  8191. std::uintptr_t offby = static_cast<std::uintptr_t>(initial % alignment);
  8192. std::uintptr_t padding = (alignment - offby) % alignment;
  8193. required_space += size + padding;
  8194. if (space < required_space) {
  8195. return nullptr;
  8196. }
  8197. ptr = static_cast<void*>(static_cast<char*>(ptr) + padding);
  8198. space -= padding;
  8199. return ptr;
  8200. }
  8201. inline void* align(std::size_t alignment, std::size_t size, void*& ptr, std::size_t& space) {
  8202. std::size_t required_space = 0;
  8203. return align(alignment, size, ptr, space, required_space);
  8204. }
  8205. inline void align_one(std::size_t a, std::size_t s, void*& target_alignment) {
  8206. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  8207. target_alignment = align(a, s, target_alignment, space);
  8208. target_alignment = static_cast<void*>(static_cast<char*>(target_alignment) + s);
  8209. }
  8210. template <typename... Args>
  8211. std::size_t aligned_space_for(void* alignment = nullptr) {
  8212. // use temporary storage to prevent strict UB shenanigans
  8213. char alignment_shim[(std::max)({ sizeof(Args)... }) + (std::max)({ alignof(Args)... })] {};
  8214. char* start = alignment == nullptr ? static_cast<char*>(alignment) : alignment_shim;
  8215. (void)detail::swallow { int {}, (align_one(std::alignment_of_v<Args>, sizeof(Args), alignment), int {})... };
  8216. return static_cast<char*>(alignment) - start;
  8217. }
  8218. inline void* align_usertype_pointer(void* ptr) {
  8219. using use_align = std::integral_constant<bool,
  8220. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8221. false
  8222. #else
  8223. (std::alignment_of<void*>::value > 1)
  8224. #endif
  8225. >;
  8226. if (!use_align::value) {
  8227. return ptr;
  8228. }
  8229. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  8230. return align(std::alignment_of<void*>::value, sizeof(void*), ptr, space);
  8231. }
  8232. template <bool pre_aligned = false, bool pre_shifted = false>
  8233. void* align_usertype_unique_destructor(void* ptr) {
  8234. using use_align = std::integral_constant<bool,
  8235. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8236. false
  8237. #else
  8238. (std::alignment_of<unique_destructor>::value > 1)
  8239. #endif
  8240. >;
  8241. if (!pre_aligned) {
  8242. ptr = align_usertype_pointer(ptr);
  8243. }
  8244. if (!pre_shifted) {
  8245. ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(void*));
  8246. }
  8247. if (!use_align::value) {
  8248. return static_cast<void*>(static_cast<void**>(ptr) + 1);
  8249. }
  8250. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  8251. return align(std::alignment_of<unique_destructor>::value, sizeof(unique_destructor), ptr, space);
  8252. }
  8253. template <bool pre_aligned = false, bool pre_shifted = false>
  8254. void* align_usertype_unique_tag(void* ptr) {
  8255. using use_align = std::integral_constant<bool,
  8256. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8257. false
  8258. #else
  8259. (std::alignment_of<unique_tag>::value > 1)
  8260. #endif
  8261. >;
  8262. if (!pre_aligned) {
  8263. ptr = align_usertype_unique_destructor(ptr);
  8264. }
  8265. if (!pre_shifted) {
  8266. ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(unique_destructor));
  8267. }
  8268. if (!use_align::value) {
  8269. return ptr;
  8270. }
  8271. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  8272. return align(std::alignment_of<unique_tag>::value, sizeof(unique_tag), ptr, space);
  8273. }
  8274. template <typename T, bool pre_aligned = false, bool pre_shifted = false>
  8275. void* align_usertype_unique(void* ptr) {
  8276. typedef std::integral_constant<bool,
  8277. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8278. false
  8279. #else
  8280. (std::alignment_of_v<T> > 1)
  8281. #endif
  8282. >
  8283. use_align;
  8284. if (!pre_aligned) {
  8285. ptr = align_usertype_unique_tag(ptr);
  8286. }
  8287. if (!pre_shifted) {
  8288. ptr = static_cast<void*>(static_cast<char*>(ptr) + sizeof(unique_tag));
  8289. }
  8290. if (!use_align::value) {
  8291. return ptr;
  8292. }
  8293. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  8294. return align(std::alignment_of_v<T>, sizeof(T), ptr, space);
  8295. }
  8296. template <typename T>
  8297. void* align_user(void* ptr) {
  8298. typedef std::integral_constant<bool,
  8299. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8300. false
  8301. #else
  8302. (std::alignment_of_v<T> > 1)
  8303. #endif
  8304. >
  8305. use_align;
  8306. if (!use_align::value) {
  8307. return ptr;
  8308. }
  8309. std::size_t space = (std::numeric_limits<std::size_t>::max)();
  8310. return align(std::alignment_of_v<T>, sizeof(T), ptr, space);
  8311. }
  8312. template <typename T>
  8313. T** usertype_allocate_pointer(lua_State* L) {
  8314. typedef std::integral_constant<bool,
  8315. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8316. false
  8317. #else
  8318. (std::alignment_of<T*>::value > 1)
  8319. #endif
  8320. >
  8321. use_align;
  8322. if (!use_align::value) {
  8323. T** pointerpointer = static_cast<T**>(lua_newuserdata(L, sizeof(T*)));
  8324. return pointerpointer;
  8325. }
  8326. static const std::size_t initial_size = aligned_space_for<T*>(nullptr);
  8327. static const std::size_t misaligned_size = aligned_space_for<T*>(reinterpret_cast<void*>(0x1));
  8328. std::size_t allocated_size = initial_size;
  8329. void* unadjusted = lua_newuserdata(L, initial_size);
  8330. void* adjusted = align(std::alignment_of<T*>::value, sizeof(T*), unadjusted, allocated_size);
  8331. if (adjusted == nullptr) {
  8332. lua_pop(L, 1);
  8333. // what kind of absolute garbage trash allocator are we dealing with?
  8334. // whatever, add some padding in the case of MAXIMAL alignment waste...
  8335. allocated_size = misaligned_size;
  8336. unadjusted = lua_newuserdata(L, allocated_size);
  8337. adjusted = align(std::alignment_of<T*>::value, sizeof(T*), unadjusted, allocated_size);
  8338. if (adjusted == nullptr) {
  8339. // trash allocator can burn in hell
  8340. lua_pop(L, 1);
  8341. // luaL_error(L, "if you are the one that wrote this allocator you should feel bad for doing a
  8342. // worse job than malloc/realloc and should go read some books, yeah?");
  8343. luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T*>().data());
  8344. }
  8345. }
  8346. return static_cast<T**>(adjusted);
  8347. }
  8348. inline bool attempt_alloc(lua_State* L, std::size_t ptr_align, std::size_t ptr_size, std::size_t value_align, std::size_t value_size,
  8349. std::size_t allocated_size, void*& pointer_adjusted, void*& data_adjusted) {
  8350. void* adjusted = lua_newuserdata(L, allocated_size);
  8351. pointer_adjusted = align(ptr_align, ptr_size, adjusted, allocated_size);
  8352. if (pointer_adjusted == nullptr) {
  8353. lua_pop(L, 1);
  8354. return false;
  8355. }
  8356. // subtract size of what we're going to allocate there
  8357. allocated_size -= ptr_size;
  8358. adjusted = static_cast<void*>(static_cast<char*>(pointer_adjusted) + ptr_size);
  8359. data_adjusted = align(value_align, value_size, adjusted, allocated_size);
  8360. if (data_adjusted == nullptr) {
  8361. lua_pop(L, 1);
  8362. return false;
  8363. }
  8364. return true;
  8365. }
  8366. inline bool attempt_alloc_unique(lua_State* L, std::size_t ptr_align, std::size_t ptr_size, std::size_t real_align, std::size_t real_size,
  8367. std::size_t allocated_size, void*& pointer_adjusted, void*& dx_adjusted, void*& id_adjusted, void*& data_adjusted) {
  8368. void* adjusted = lua_newuserdata(L, allocated_size);
  8369. pointer_adjusted = align(ptr_align, ptr_size, adjusted, allocated_size);
  8370. if (pointer_adjusted == nullptr) {
  8371. lua_pop(L, 1);
  8372. return false;
  8373. }
  8374. allocated_size -= ptr_size;
  8375. adjusted = static_cast<void*>(static_cast<char*>(pointer_adjusted) + ptr_size);
  8376. dx_adjusted = align(std::alignment_of_v<unique_destructor>, sizeof(unique_destructor), adjusted, allocated_size);
  8377. if (dx_adjusted == nullptr) {
  8378. lua_pop(L, 1);
  8379. return false;
  8380. }
  8381. allocated_size -= sizeof(unique_destructor);
  8382. adjusted = static_cast<void*>(static_cast<char*>(dx_adjusted) + sizeof(unique_destructor));
  8383. id_adjusted = align(std::alignment_of_v<unique_tag>, sizeof(unique_tag), adjusted, allocated_size);
  8384. if (id_adjusted == nullptr) {
  8385. lua_pop(L, 1);
  8386. return false;
  8387. }
  8388. allocated_size -= sizeof(unique_tag);
  8389. adjusted = static_cast<void*>(static_cast<char*>(id_adjusted) + sizeof(unique_tag));
  8390. data_adjusted = align(real_align, real_size, adjusted, allocated_size);
  8391. if (data_adjusted == nullptr) {
  8392. lua_pop(L, 1);
  8393. return false;
  8394. }
  8395. return true;
  8396. }
  8397. template <typename T>
  8398. T* usertype_allocate(lua_State* L) {
  8399. typedef std::integral_constant<bool,
  8400. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8401. false
  8402. #else
  8403. (std::alignment_of<T*>::value > 1 || std::alignment_of_v<T> > 1)
  8404. #endif
  8405. >
  8406. use_align;
  8407. if (!use_align::value) {
  8408. T** pointerpointer = static_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
  8409. T*& pointerreference = *pointerpointer;
  8410. T* allocationtarget = reinterpret_cast<T*>(pointerpointer + 1);
  8411. pointerreference = allocationtarget;
  8412. return allocationtarget;
  8413. }
  8414. /* the assumption is that `lua_newuserdata` -- unless someone
  8415. passes a specific lua_Alloc that gives us bogus, un-aligned pointers
  8416. -- uses malloc, which tends to hand out more or less aligned pointers to memory
  8417. (most of the time, anyhow)
  8418. but it's not guaranteed, so we have to do a post-adjustment check and increase padding
  8419. we do this preliminarily with compile-time stuff, to see
  8420. if we strike lucky with the allocator and alignment values
  8421. otherwise, we have to re-allocate the userdata and
  8422. over-allocate some space for additional padding because
  8423. compilers are optimized for aligned reads/writes
  8424. (and clang will barf UBsan errors on us for not being aligned)
  8425. */
  8426. static const std::size_t initial_size = aligned_space_for<T*, T>(nullptr);
  8427. static const std::size_t misaligned_size = aligned_space_for<T*, T>(reinterpret_cast<void*>(0x1));
  8428. void* pointer_adjusted;
  8429. void* data_adjusted;
  8430. bool result
  8431. = attempt_alloc(L, std::alignment_of_v<T*>, sizeof(T*), std::alignment_of_v<T>, sizeof(T), initial_size, pointer_adjusted, data_adjusted);
  8432. if (!result) {
  8433. // we're likely to get something that fails to perform the proper allocation a second time,
  8434. // so we use the suggested_new_size bump to help us out here
  8435. pointer_adjusted = nullptr;
  8436. data_adjusted = nullptr;
  8437. result = attempt_alloc(
  8438. L, std::alignment_of_v<T*>, sizeof(T*), std::alignment_of_v<T>, sizeof(T), misaligned_size, pointer_adjusted, data_adjusted);
  8439. if (!result) {
  8440. if (pointer_adjusted == nullptr) {
  8441. luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle<T>().c_str());
  8442. }
  8443. else {
  8444. luaL_error(L, "aligned allocation of userdata block (data section) for '%s' failed", detail::demangle<T>().c_str());
  8445. }
  8446. return nullptr;
  8447. }
  8448. }
  8449. T** pointerpointer = reinterpret_cast<T**>(pointer_adjusted);
  8450. T*& pointerreference = *pointerpointer;
  8451. T* allocationtarget = reinterpret_cast<T*>(data_adjusted);
  8452. pointerreference = allocationtarget;
  8453. return allocationtarget;
  8454. }
  8455. template <typename T, typename Real>
  8456. Real* usertype_unique_allocate(lua_State* L, T**& pref, unique_destructor*& dx, unique_tag*& id) {
  8457. typedef std::integral_constant<bool,
  8458. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8459. false
  8460. #else
  8461. (std::alignment_of<T*>::value > 1 || std::alignment_of<unique_tag>::value > 1 || std::alignment_of<unique_destructor>::value > 1
  8462. || std::alignment_of<Real>::value > 1)
  8463. #endif
  8464. >
  8465. use_align;
  8466. if (!use_align::value) {
  8467. pref = static_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(detail::unique_destructor) + sizeof(unique_tag) + sizeof(Real)));
  8468. dx = static_cast<detail::unique_destructor*>(static_cast<void*>(pref + 1));
  8469. id = static_cast<unique_tag*>(static_cast<void*>(dx + 1));
  8470. Real* mem = static_cast<Real*>(static_cast<void*>(id + 1));
  8471. return mem;
  8472. }
  8473. static const std::size_t initial_size = aligned_space_for<T*, unique_destructor, unique_tag, Real>(nullptr);
  8474. static const std::size_t misaligned_size = aligned_space_for<T*, unique_destructor, unique_tag, Real>(reinterpret_cast<void*>(0x1));
  8475. void* pointer_adjusted;
  8476. void* dx_adjusted;
  8477. void* id_adjusted;
  8478. void* data_adjusted;
  8479. bool result = attempt_alloc_unique(L,
  8480. std::alignment_of_v<T*>,
  8481. sizeof(T*),
  8482. std::alignment_of_v<Real>,
  8483. sizeof(Real),
  8484. initial_size,
  8485. pointer_adjusted,
  8486. dx_adjusted,
  8487. id_adjusted,
  8488. data_adjusted);
  8489. if (!result) {
  8490. // we're likely to get something that fails to perform the proper allocation a second time,
  8491. // so we use the suggested_new_size bump to help us out here
  8492. pointer_adjusted = nullptr;
  8493. dx_adjusted = nullptr;
  8494. id_adjusted = nullptr;
  8495. data_adjusted = nullptr;
  8496. result = attempt_alloc_unique(L,
  8497. std::alignment_of_v<T*>,
  8498. sizeof(T*),
  8499. std::alignment_of_v<Real>,
  8500. sizeof(Real),
  8501. misaligned_size,
  8502. pointer_adjusted,
  8503. dx_adjusted,
  8504. id_adjusted,
  8505. data_adjusted);
  8506. if (!result) {
  8507. if (pointer_adjusted == nullptr) {
  8508. luaL_error(L, "aligned allocation of userdata block (pointer section) for '%s' failed", detail::demangle<T>().c_str());
  8509. }
  8510. else if (dx_adjusted == nullptr) {
  8511. luaL_error(L, "aligned allocation of userdata block (deleter section) for '%s' failed", detail::demangle<T>().c_str());
  8512. }
  8513. else {
  8514. luaL_error(L, "aligned allocation of userdata block (data section) for '%s' failed", detail::demangle<T>().c_str());
  8515. }
  8516. return nullptr;
  8517. }
  8518. }
  8519. pref = static_cast<T**>(pointer_adjusted);
  8520. dx = static_cast<detail::unique_destructor*>(dx_adjusted);
  8521. id = static_cast<unique_tag*>(id_adjusted);
  8522. Real* mem = static_cast<Real*>(data_adjusted);
  8523. return mem;
  8524. }
  8525. template <typename T>
  8526. T* user_allocate(lua_State* L) {
  8527. typedef std::integral_constant<bool,
  8528. #if SOL_IS_OFF(SOL_ALIGN_MEMORY_I_)
  8529. false
  8530. #else
  8531. (std::alignment_of_v<T> > 1)
  8532. #endif
  8533. >
  8534. use_align;
  8535. if (!use_align::value) {
  8536. T* pointer = static_cast<T*>(lua_newuserdata(L, sizeof(T)));
  8537. return pointer;
  8538. }
  8539. static const std::size_t initial_size = aligned_space_for<T>(nullptr);
  8540. static const std::size_t misaligned_size = aligned_space_for<T>(reinterpret_cast<void*>(0x1));
  8541. std::size_t allocated_size = initial_size;
  8542. void* unadjusted = lua_newuserdata(L, allocated_size);
  8543. void* adjusted = align(std::alignment_of_v<T>, sizeof(T), unadjusted, allocated_size);
  8544. if (adjusted == nullptr) {
  8545. lua_pop(L, 1);
  8546. // try again, add extra space for alignment padding
  8547. allocated_size = misaligned_size;
  8548. unadjusted = lua_newuserdata(L, allocated_size);
  8549. adjusted = align(std::alignment_of_v<T>, sizeof(T), unadjusted, allocated_size);
  8550. if (adjusted == nullptr) {
  8551. lua_pop(L, 1);
  8552. luaL_error(L, "cannot properly align memory for '%s'", detail::demangle<T>().data());
  8553. }
  8554. }
  8555. return static_cast<T*>(adjusted);
  8556. }
  8557. template <typename T>
  8558. int usertype_alloc_destruct(lua_State* L) {
  8559. void* memory = lua_touserdata(L, 1);
  8560. memory = align_usertype_pointer(memory);
  8561. T** pdata = static_cast<T**>(memory);
  8562. T* data = *pdata;
  8563. std::allocator<T> alloc {};
  8564. std::allocator_traits<std::allocator<T>>::destroy(alloc, data);
  8565. return 0;
  8566. }
  8567. template <typename T>
  8568. int unique_destruct(lua_State* L) {
  8569. void* memory = lua_touserdata(L, 1);
  8570. memory = align_usertype_unique_destructor(memory);
  8571. unique_destructor& dx = *static_cast<unique_destructor*>(memory);
  8572. memory = align_usertype_unique_tag<true>(memory);
  8573. (dx)(memory);
  8574. return 0;
  8575. }
  8576. template <typename T>
  8577. int user_alloc_destruct(lua_State* L) {
  8578. void* memory = lua_touserdata(L, 1);
  8579. memory = align_user<T>(memory);
  8580. T* data = static_cast<T*>(memory);
  8581. std::allocator<T> alloc;
  8582. std::allocator_traits<std::allocator<T>>::destroy(alloc, data);
  8583. return 0;
  8584. }
  8585. template <typename T, typename Real>
  8586. void usertype_unique_alloc_destroy(void* memory) {
  8587. memory = align_usertype_unique<Real, true>(memory);
  8588. Real* target = static_cast<Real*>(memory);
  8589. std::allocator<Real> alloc;
  8590. std::allocator_traits<std::allocator<Real>>::destroy(alloc, target);
  8591. }
  8592. template <typename T>
  8593. int cannot_destruct(lua_State* L) {
  8594. return luaL_error(L,
  8595. "cannot call the destructor for '%s': it is either hidden (protected/private) or removed with '= "
  8596. "delete' and thusly this type is being destroyed without properly destructing, invoking undefined "
  8597. "behavior: please bind a usertype and specify a custom destructor to define the behavior properly",
  8598. detail::demangle<T>().data());
  8599. }
  8600. template <typename T>
  8601. void reserve(T&, std::size_t) {
  8602. }
  8603. template <typename T, typename Al>
  8604. void reserve(std::vector<T, Al>& vec, std::size_t hint) {
  8605. vec.reserve(hint);
  8606. }
  8607. template <typename T, typename Tr, typename Al>
  8608. void reserve(std::basic_string<T, Tr, Al>& str, std::size_t hint) {
  8609. str.reserve(hint);
  8610. }
  8611. inline bool property_always_true(meta_function) {
  8612. return true;
  8613. }
  8614. struct properties_enrollment_allowed {
  8615. int& times_through;
  8616. std::bitset<64>& properties;
  8617. automagic_enrollments& enrollments;
  8618. properties_enrollment_allowed(int& times, std::bitset<64>& props, automagic_enrollments& enroll)
  8619. : times_through(times), properties(props), enrollments(enroll) {
  8620. }
  8621. bool operator()(meta_function mf) const {
  8622. bool p = properties[static_cast<int>(mf)];
  8623. if (times_through > 0) {
  8624. return p;
  8625. }
  8626. switch (mf) {
  8627. case meta_function::length:
  8628. return enrollments.length_operator && !p;
  8629. case meta_function::pairs:
  8630. return enrollments.pairs_operator && !p;
  8631. case meta_function::call:
  8632. return enrollments.call_operator && !p;
  8633. case meta_function::less_than:
  8634. return enrollments.less_than_operator && !p;
  8635. case meta_function::less_than_or_equal_to:
  8636. return enrollments.less_than_or_equal_to_operator && !p;
  8637. case meta_function::equal_to:
  8638. return enrollments.equal_to_operator && !p;
  8639. default:
  8640. break;
  8641. }
  8642. return !p;
  8643. }
  8644. };
  8645. struct indexed_insert {
  8646. lua_reg_table& l;
  8647. int& index;
  8648. indexed_insert(lua_reg_table& cont, int& idx) : l(cont), index(idx) {
  8649. }
  8650. void operator()(meta_function mf, lua_CFunction f) {
  8651. l[index] = luaL_Reg { to_string(mf).c_str(), f };
  8652. ++index;
  8653. }
  8654. };
  8655. } // namespace detail
  8656. namespace stack {
  8657. template <typename T, bool global = false, bool raw = false, typename = void>
  8658. struct field_getter;
  8659. template <typename T, typename P, bool global = false, bool raw = false, typename = void>
  8660. struct probe_field_getter;
  8661. template <typename T, bool global = false, bool raw = false, typename = void>
  8662. struct field_setter;
  8663. template <typename T, typename = void>
  8664. struct unqualified_getter;
  8665. template <typename T, typename = void>
  8666. struct qualified_getter;
  8667. template <typename T, typename = void>
  8668. struct qualified_interop_getter;
  8669. template <typename T, typename = void>
  8670. struct unqualified_interop_getter;
  8671. template <typename T, typename = void>
  8672. struct popper;
  8673. template <typename T, typename = void>
  8674. struct unqualified_pusher;
  8675. template <typename T, type t, typename = void>
  8676. struct unqualified_checker;
  8677. template <typename T, type t, typename = void>
  8678. struct qualified_checker;
  8679. template <typename T, typename = void>
  8680. struct unqualified_check_getter;
  8681. template <typename T, typename = void>
  8682. struct qualified_check_getter;
  8683. struct probe {
  8684. bool success;
  8685. int levels;
  8686. probe(bool s, int l) : success(s), levels(l) {
  8687. }
  8688. operator bool() const {
  8689. return success;
  8690. };
  8691. };
  8692. struct record {
  8693. int last;
  8694. int used;
  8695. record() noexcept : last(), used() {
  8696. }
  8697. void use(int count) noexcept {
  8698. last = count;
  8699. used += count;
  8700. }
  8701. };
  8702. namespace stack_detail {
  8703. template <typename Function>
  8704. Function* get_function_pointer(lua_State*, int, record&) noexcept;
  8705. template <typename Function, typename Handler>
  8706. bool check_function_pointer(lua_State* L, int index, Handler&& handler, record& tracking) noexcept;
  8707. } // namespace stack_detail
  8708. } // namespace stack
  8709. namespace meta { namespace meta_detail {
  8710. template <typename T>
  8711. using adl_sol_lua_get_test_t = decltype(sol_lua_get(types<T>(), static_cast<lua_State*>(nullptr), -1, std::declval<stack::record&>()));
  8712. template <typename T>
  8713. using adl_sol_lua_interop_get_test_t
  8714. = decltype(sol_lua_interop_get(types<T>(), static_cast<lua_State*>(nullptr), -1, static_cast<void*>(nullptr), std::declval<stack::record&>()));
  8715. template <typename T>
  8716. using adl_sol_lua_check_test_t = decltype(sol_lua_check(types<T>(), static_cast<lua_State*>(nullptr), -1, no_panic, std::declval<stack::record&>()));
  8717. template <typename T>
  8718. using adl_sol_lua_interop_check_test_t
  8719. = decltype(sol_lua_interop_check(types<T>(), static_cast<lua_State*>(nullptr), -1, type::none, no_panic, std::declval<stack::record&>()));
  8720. template <typename T>
  8721. using adl_sol_lua_check_get_test_t
  8722. = decltype(sol_lua_check_get(types<T>(), static_cast<lua_State*>(nullptr), -1, no_panic, std::declval<stack::record&>()));
  8723. template <typename... Args>
  8724. using adl_sol_lua_push_test_t = decltype(sol_lua_push(static_cast<lua_State*>(nullptr), std::declval<Args>()...));
  8725. template <typename T, typename... Args>
  8726. using adl_sol_lua_push_exact_test_t = decltype(sol_lua_push(types<T>(), static_cast<lua_State*>(nullptr), std::declval<Args>()...));
  8727. template <typename T>
  8728. inline constexpr bool is_adl_sol_lua_get_v = meta::is_detected_v<adl_sol_lua_get_test_t, T>;
  8729. template <typename T>
  8730. inline constexpr bool is_adl_sol_lua_interop_get_v = meta::is_detected_v<adl_sol_lua_interop_get_test_t, T>;
  8731. template <typename T>
  8732. inline constexpr bool is_adl_sol_lua_check_v = meta::is_detected_v<adl_sol_lua_check_test_t, T>;
  8733. template <typename T>
  8734. inline constexpr bool is_adl_sol_lua_interop_check_v = meta::is_detected_v<adl_sol_lua_interop_check_test_t, T>;
  8735. template <typename T>
  8736. inline constexpr bool is_adl_sol_lua_check_get_v = meta::is_detected_v<adl_sol_lua_check_get_test_t, T>;
  8737. template <typename... Args>
  8738. inline constexpr bool is_adl_sol_lua_push_v = meta::is_detected_v<adl_sol_lua_push_test_t, Args...>;
  8739. template <typename T, typename... Args>
  8740. inline constexpr bool is_adl_sol_lua_push_exact_v = meta::is_detected_v<adl_sol_lua_push_exact_test_t, T, Args...>;
  8741. }} // namespace meta::meta_detail
  8742. namespace stack {
  8743. namespace stack_detail {
  8744. constexpr const char* not_enough_stack_space = "not enough space left on Lua stack";
  8745. constexpr const char* not_enough_stack_space_floating = "not enough space left on Lua stack for a floating point number";
  8746. constexpr const char* not_enough_stack_space_integral = "not enough space left on Lua stack for an integral number";
  8747. constexpr const char* not_enough_stack_space_string = "not enough space left on Lua stack for a string";
  8748. constexpr const char* not_enough_stack_space_meta_function_name = "not enough space left on Lua stack for the name of a meta_function";
  8749. constexpr const char* not_enough_stack_space_userdata = "not enough space left on Lua stack to create a sol3 userdata";
  8750. constexpr const char* not_enough_stack_space_generic = "not enough space left on Lua stack to push valuees";
  8751. constexpr const char* not_enough_stack_space_environment = "not enough space left on Lua stack to retrieve environment";
  8752. template <typename T>
  8753. struct strip {
  8754. typedef T type;
  8755. };
  8756. template <typename T>
  8757. struct strip<std::reference_wrapper<T>> {
  8758. typedef T& type;
  8759. };
  8760. template <typename T>
  8761. struct strip<user<T>> {
  8762. typedef T& type;
  8763. };
  8764. template <typename T>
  8765. struct strip<non_null<T>> {
  8766. typedef T type;
  8767. };
  8768. template <typename T>
  8769. using strip_t = typename strip<T>::type;
  8770. template <typename C>
  8771. static int get_size_hint(C& c) {
  8772. return static_cast<int>(c.size());
  8773. }
  8774. template <typename V, typename Al>
  8775. static int get_size_hint(const std::forward_list<V, Al>&) {
  8776. // forward_list makes me sad
  8777. return static_cast<int>(32);
  8778. }
  8779. template <typename T>
  8780. decltype(auto) unchecked_unqualified_get(lua_State* L, int index, record& tracking) {
  8781. using Tu = meta::unqualified_t<T>;
  8782. if constexpr (meta::meta_detail::is_adl_sol_lua_get_v<Tu>) {
  8783. return sol_lua_get(types<Tu>(), L, index, tracking);
  8784. }
  8785. else {
  8786. unqualified_getter<Tu> g {};
  8787. (void)g;
  8788. return g.get(L, index, tracking);
  8789. }
  8790. }
  8791. template <typename T>
  8792. decltype(auto) unchecked_get(lua_State* L, int index, record& tracking) {
  8793. if constexpr (meta::meta_detail::is_adl_sol_lua_get_v<T>) {
  8794. return sol_lua_get(types<T>(), L, index, tracking);
  8795. }
  8796. else {
  8797. qualified_getter<T> g {};
  8798. (void)g;
  8799. return g.get(L, index, tracking);
  8800. }
  8801. }
  8802. template <typename T>
  8803. decltype(auto) unqualified_interop_get(lua_State* L, int index, void* unadjusted_pointer, record& tracking) {
  8804. using Tu = meta::unqualified_t<T>;
  8805. if constexpr (meta::meta_detail::is_adl_sol_lua_interop_get_v<Tu>) {
  8806. return sol_lua_interop_get(types<Tu>(), L, index, unadjusted_pointer, tracking);
  8807. }
  8808. else {
  8809. (void)L;
  8810. (void)index;
  8811. (void)unadjusted_pointer;
  8812. (void)tracking;
  8813. using Ti = stack_detail::strip_t<Tu>;
  8814. return std::pair<bool, Ti*> { false, nullptr };
  8815. }
  8816. }
  8817. template <typename T>
  8818. decltype(auto) interop_get(lua_State* L, int index, void* unadjusted_pointer, record& tracking) {
  8819. if constexpr (meta::meta_detail::is_adl_sol_lua_interop_get_v<T>) {
  8820. return sol_lua_interop_get(types<T>(), L, index, unadjusted_pointer, tracking);
  8821. }
  8822. else {
  8823. return unqualified_interop_get<T>(L, index, unadjusted_pointer, tracking);
  8824. }
  8825. }
  8826. template <typename T, typename Handler>
  8827. bool unqualified_interop_check(lua_State* L, int index, type index_type, Handler&& handler, record& tracking) {
  8828. using Tu = meta::unqualified_t<T>;
  8829. if constexpr (meta::meta_detail::is_adl_sol_lua_interop_check_v<Tu>) {
  8830. return sol_lua_interop_check(types<Tu>(), L, index, index_type, std::forward<Handler>(handler), tracking);
  8831. }
  8832. else {
  8833. (void)L;
  8834. (void)index;
  8835. (void)index_type;
  8836. (void)handler;
  8837. (void)tracking;
  8838. return false;
  8839. }
  8840. }
  8841. template <typename T, typename Handler>
  8842. bool interop_check(lua_State* L, int index, type index_type, Handler&& handler, record& tracking) {
  8843. if constexpr (meta::meta_detail::is_adl_sol_lua_interop_check_v<T>) {
  8844. return sol_lua_interop_check(types<T>(), L, index, index_type, std::forward<Handler>(handler), tracking);
  8845. }
  8846. else {
  8847. return unqualified_interop_check<T>(L, index, index_type, std::forward<Handler>(handler), tracking);
  8848. }
  8849. }
  8850. using undefined_method_func = void (*)(stack_reference);
  8851. struct undefined_metatable {
  8852. lua_State* L;
  8853. const char* key;
  8854. undefined_method_func on_new_table;
  8855. undefined_metatable(lua_State* l, const char* k, undefined_method_func umf) : L(l), key(k), on_new_table(umf) {
  8856. }
  8857. void operator()() const {
  8858. if (luaL_newmetatable(L, key) == 1) {
  8859. on_new_table(stack_reference(L, -1));
  8860. }
  8861. lua_setmetatable(L, -2);
  8862. }
  8863. };
  8864. } // namespace stack_detail
  8865. inline bool maybe_indexable(lua_State* L, int index = -1) {
  8866. type t = type_of(L, index);
  8867. return t == type::userdata || t == type::table;
  8868. }
  8869. inline int top(lua_State* L) {
  8870. return lua_gettop(L);
  8871. }
  8872. inline bool is_main_thread(lua_State* L) {
  8873. int ismainthread = lua_pushthread(L);
  8874. lua_pop(L, 1);
  8875. return ismainthread == 1;
  8876. }
  8877. inline void coroutine_create_guard(lua_State* L) {
  8878. if (is_main_thread(L)) {
  8879. return;
  8880. }
  8881. int stacksize = lua_gettop(L);
  8882. if (stacksize < 1) {
  8883. return;
  8884. }
  8885. if (type_of(L, 1) != type::function) {
  8886. return;
  8887. }
  8888. // well now we're screwed...
  8889. // we can clean the stack and pray it doesn't destroy anything?
  8890. lua_pop(L, stacksize);
  8891. }
  8892. inline void clear(lua_State* L, int table_index) {
  8893. lua_pushnil(L);
  8894. while (lua_next(L, table_index) != 0) {
  8895. // remove value
  8896. lua_pop(L, 1);
  8897. // duplicate key to protect form rawset
  8898. lua_pushvalue(L, -1);
  8899. // push new value
  8900. lua_pushnil(L);
  8901. // table_index%[key] = nil
  8902. lua_rawset(L, table_index);
  8903. }
  8904. }
  8905. inline void clear(reference& r) {
  8906. auto pp = push_pop<false>(r);
  8907. int stack_index = pp.index_of(r);
  8908. clear(r.lua_state(), stack_index);
  8909. }
  8910. inline void clear(stack_reference& r) {
  8911. clear(r.lua_state(), r.stack_index());
  8912. }
  8913. template <typename T, typename... Args>
  8914. int push(lua_State* L, T&& t, Args&&... args) {
  8915. using Tu = meta::unqualified_t<T>;
  8916. if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<T, T, Args...>) {
  8917. return sol_lua_push(types<T>(), L, std::forward<T>(t), std::forward<Args>(args)...);
  8918. }
  8919. else if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<Tu, T, Args...>) {
  8920. return sol_lua_push(types<Tu>(), L, std::forward<T>(t), std::forward<Args>(args)...);
  8921. }
  8922. else if constexpr (meta::meta_detail::is_adl_sol_lua_push_v<T, Args...>) {
  8923. return sol_lua_push(L, std::forward<T>(t), std::forward<Args>(args)...);
  8924. }
  8925. else {
  8926. unqualified_pusher<Tu> p {};
  8927. (void)p;
  8928. return p.push(L, std::forward<T>(t), std::forward<Args>(args)...);
  8929. }
  8930. }
  8931. // overload allows to use a pusher of a specific type, but pass in any kind of args
  8932. template <typename T, typename Arg, typename... Args, typename = std::enable_if_t<!std::is_same<T, Arg>::value>>
  8933. int push(lua_State* L, Arg&& arg, Args&&... args) {
  8934. using Tu = meta::unqualified_t<T>;
  8935. if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<T, Arg, Args...>) {
  8936. return sol_lua_push(types<T>(), L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8937. }
  8938. else if constexpr (meta::meta_detail::is_adl_sol_lua_push_exact_v<Tu, Arg, Args...>) {
  8939. return sol_lua_push(types<Tu>(), L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8940. }
  8941. else if constexpr (meta::meta_detail::is_adl_sol_lua_push_v<Arg, Args...>) {
  8942. return sol_lua_push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8943. }
  8944. else {
  8945. unqualified_pusher<Tu> p {};
  8946. (void)p;
  8947. return p.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8948. }
  8949. }
  8950. template <typename T, typename... Args>
  8951. int push_userdata(lua_State* L, T&& t, Args&&... args) {
  8952. using U = meta::unqualified_t<T>;
  8953. using Tr = meta::conditional_t<std::is_pointer_v<U>,
  8954. detail::as_pointer_tag<std::remove_pointer_t<U>>,
  8955. meta::conditional_t<is_unique_usertype_v<U>, detail::as_unique_tag<U>, detail::as_value_tag<U>>>;
  8956. return stack::push<Tr>(L, std::forward<T>(t), std::forward<Args>(args)...);
  8957. }
  8958. template <typename T, typename Arg, typename... Args>
  8959. int push_userdata(lua_State* L, Arg&& arg, Args&&... args) {
  8960. using U = meta::unqualified_t<T>;
  8961. using Tr = meta::conditional_t<std::is_pointer_v<U>,
  8962. detail::as_pointer_tag<std::remove_pointer_t<U>>,
  8963. meta::conditional_t<is_unique_usertype_v<U>, detail::as_unique_tag<U>, detail::as_value_tag<U>>>;
  8964. return stack::push<Tr>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8965. }
  8966. namespace stack_detail {
  8967. template <typename T, typename Arg, typename... Args>
  8968. int push_reference(lua_State* L, Arg&& arg, Args&&... args) {
  8969. using use_reference_tag = meta::all<std::is_lvalue_reference<T>,
  8970. meta::neg<std::is_const<std::remove_reference_t<T>>>,
  8971. meta::neg<is_lua_primitive<meta::unqualified_t<T>>>,
  8972. meta::neg<is_unique_usertype<meta::unqualified_t<T>>>>;
  8973. using Tr = meta::conditional_t<use_reference_tag::value, detail::as_reference_tag, meta::unqualified_t<T>>;
  8974. return stack::push<Tr>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8975. }
  8976. } // namespace stack_detail
  8977. template <typename T, typename... Args>
  8978. int push_reference(lua_State* L, T&& t, Args&&... args) {
  8979. return stack_detail::push_reference<T>(L, std::forward<T>(t), std::forward<Args>(args)...);
  8980. }
  8981. template <typename T, typename Arg, typename... Args>
  8982. int push_reference(lua_State* L, Arg&& arg, Args&&... args) {
  8983. return stack_detail::push_reference<T>(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  8984. }
  8985. inline int multi_push(lua_State*) {
  8986. // do nothing
  8987. return 0;
  8988. }
  8989. template <typename T, typename... Args>
  8990. int multi_push(lua_State* L, T&& t, Args&&... args) {
  8991. int pushcount = push(L, std::forward<T>(t));
  8992. void(detail::swallow { (pushcount += stack::push(L, std::forward<Args>(args)), 0)... });
  8993. return pushcount;
  8994. }
  8995. inline int multi_push_reference(lua_State*) {
  8996. // do nothing
  8997. return 0;
  8998. }
  8999. template <typename T, typename... Args>
  9000. int multi_push_reference(lua_State* L, T&& t, Args&&... args) {
  9001. int pushcount = push_reference(L, std::forward<T>(t));
  9002. void(detail::swallow { (pushcount += stack::push_reference(L, std::forward<Args>(args)), 0)... });
  9003. return pushcount;
  9004. }
  9005. template <typename T, typename Handler>
  9006. bool unqualified_check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9007. using Tu = meta::unqualified_t<T>;
  9008. if constexpr (meta::meta_detail::is_adl_sol_lua_check_v<Tu>) {
  9009. return sol_lua_check(types<Tu>(), L, index, std::forward<Handler>(handler), tracking);
  9010. }
  9011. else {
  9012. unqualified_checker<Tu, lua_type_of_v<Tu>> c;
  9013. // VC++ has a bad warning here: shut it up
  9014. (void)c;
  9015. return c.check(L, index, std::forward<Handler>(handler), tracking);
  9016. }
  9017. }
  9018. template <typename T, typename Handler>
  9019. bool unqualified_check(lua_State* L, int index, Handler&& handler) {
  9020. record tracking {};
  9021. return unqualified_check<T>(L, index, std::forward<Handler>(handler), tracking);
  9022. }
  9023. template <typename T>
  9024. bool unqualified_check(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9025. auto handler = no_panic;
  9026. return unqualified_check<T>(L, index, handler);
  9027. }
  9028. template <typename T, typename Handler>
  9029. bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9030. if constexpr (meta::meta_detail::is_adl_sol_lua_check_v<T>) {
  9031. return sol_lua_check(types<T>(), L, index, std::forward<Handler>(handler), tracking);
  9032. }
  9033. else {
  9034. using Tu = meta::unqualified_t<T>;
  9035. qualified_checker<T, lua_type_of_v<Tu>> c;
  9036. // VC++ has a bad warning here: shut it up
  9037. (void)c;
  9038. return c.check(L, index, std::forward<Handler>(handler), tracking);
  9039. }
  9040. }
  9041. template <typename T, typename Handler>
  9042. bool check(lua_State* L, int index, Handler&& handler) {
  9043. record tracking {};
  9044. return check<T>(L, index, std::forward<Handler>(handler), tracking);
  9045. }
  9046. template <typename T>
  9047. bool check(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9048. auto handler = no_panic;
  9049. return check<T>(L, index, handler);
  9050. }
  9051. template <typename T, typename Handler>
  9052. bool check_usertype(lua_State* L, int index, type, Handler&& handler, record& tracking) {
  9053. using Tu = meta::unqualified_t<T>;
  9054. using detail_t = meta::conditional_t<std::is_pointer_v<T>, detail::as_pointer_tag<Tu>, detail::as_value_tag<Tu>>;
  9055. return check<detail_t>(L, index, std::forward<Handler>(handler), tracking);
  9056. }
  9057. template <typename T, typename Handler>
  9058. bool check_usertype(lua_State* L, int index, Handler&& handler, record& tracking) {
  9059. using Tu = meta::unqualified_t<T>;
  9060. using detail_t = meta::conditional_t<std::is_pointer_v<T>, detail::as_pointer_tag<Tu>, detail::as_value_tag<Tu>>;
  9061. return check<detail_t>(L, index, std::forward<Handler>(handler), tracking);
  9062. }
  9063. template <typename T, typename Handler>
  9064. bool check_usertype(lua_State* L, int index, Handler&& handler) {
  9065. record tracking {};
  9066. return check_usertype<T>(L, index, std::forward<Handler>(handler), tracking);
  9067. }
  9068. template <typename T>
  9069. bool check_usertype(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9070. auto handler = no_panic;
  9071. return check_usertype<T>(L, index, handler);
  9072. }
  9073. template <typename T, typename Handler>
  9074. decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler, record& tracking) {
  9075. using Tu = meta::unqualified_t<T>;
  9076. if constexpr (meta::meta_detail::is_adl_sol_lua_check_get_v<T>) {
  9077. return sol_lua_check_get(types<T>(), L, index, std::forward<Handler>(handler), tracking);
  9078. }
  9079. else if constexpr (meta::meta_detail::is_adl_sol_lua_check_get_v<Tu>) {
  9080. return sol_lua_check_get(types<Tu>(), L, index, std::forward<Handler>(handler), tracking);
  9081. }
  9082. else {
  9083. unqualified_check_getter<Tu> cg {};
  9084. (void)cg;
  9085. return cg.get(L, index, std::forward<Handler>(handler), tracking);
  9086. }
  9087. }
  9088. template <typename T, typename Handler>
  9089. decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler) {
  9090. record tracking {};
  9091. return unqualified_check_get<T>(L, index, handler, tracking);
  9092. }
  9093. template <typename T>
  9094. decltype(auto) unqualified_check_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9095. auto handler = no_panic;
  9096. return unqualified_check_get<T>(L, index, handler);
  9097. }
  9098. template <typename T, typename Handler>
  9099. decltype(auto) check_get(lua_State* L, int index, Handler&& handler, record& tracking) {
  9100. if constexpr (meta::meta_detail::is_adl_sol_lua_check_get_v<T>) {
  9101. return sol_lua_check_get(types<T>(), L, index, std::forward<Handler>(handler), tracking);
  9102. }
  9103. else {
  9104. qualified_check_getter<T> cg {};
  9105. (void)cg;
  9106. return cg.get(L, index, std::forward<Handler>(handler), tracking);
  9107. }
  9108. }
  9109. template <typename T, typename Handler>
  9110. decltype(auto) check_get(lua_State* L, int index, Handler&& handler) {
  9111. record tracking {};
  9112. return check_get<T>(L, index, handler, tracking);
  9113. }
  9114. template <typename T>
  9115. decltype(auto) check_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9116. auto handler = no_panic;
  9117. return check_get<T>(L, index, handler);
  9118. }
  9119. namespace stack_detail {
  9120. template <typename Handler>
  9121. bool check_types(lua_State*, int, Handler&&, record&) {
  9122. return true;
  9123. }
  9124. template <typename T, typename... Args, typename Handler>
  9125. bool check_types(lua_State* L, int firstargument, Handler&& handler, record& tracking) {
  9126. if (!stack::check<T>(L, firstargument + tracking.used, handler, tracking))
  9127. return false;
  9128. return check_types<Args...>(L, firstargument, std::forward<Handler>(handler), tracking);
  9129. }
  9130. template <typename... Args, typename Handler>
  9131. bool check_types(types<Args...>, lua_State* L, int index, Handler&& handler, record& tracking) {
  9132. return check_types<Args...>(L, index, std::forward<Handler>(handler), tracking);
  9133. }
  9134. } // namespace stack_detail
  9135. template <typename... Args, typename Handler>
  9136. bool multi_check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9137. return stack_detail::check_types<Args...>(L, index, std::forward<Handler>(handler), tracking);
  9138. }
  9139. template <typename... Args, typename Handler>
  9140. bool multi_check(lua_State* L, int index, Handler&& handler) {
  9141. record tracking {};
  9142. return multi_check<Args...>(L, index, std::forward<Handler>(handler), tracking);
  9143. }
  9144. template <typename... Args>
  9145. bool multi_check(lua_State* L, int index) {
  9146. return multi_check<Args...>(L, index);
  9147. }
  9148. template <typename T>
  9149. auto unqualified_get(lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_unqualified_get<T>(L, index, tracking)) {
  9150. #if SOL_IS_ON(SOL_SAFE_GETTER_I_)
  9151. static constexpr bool is_op = meta::is_optional_v<T>;
  9152. if constexpr (is_op) {
  9153. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  9154. }
  9155. else {
  9156. if (is_lua_reference<T>::value) {
  9157. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  9158. }
  9159. auto op = unqualified_check_get<T>(L, index, type_panic_c_str, tracking);
  9160. return *std::move(op);
  9161. }
  9162. #else
  9163. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  9164. #endif
  9165. }
  9166. template <typename T>
  9167. decltype(auto) unqualified_get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9168. record tracking {};
  9169. return unqualified_get<T>(L, index, tracking);
  9170. }
  9171. template <typename T>
  9172. auto get(lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get<T>(L, index, tracking)) {
  9173. #if SOL_IS_ON(SOL_SAFE_GETTER_I_)
  9174. static constexpr bool is_op = meta::is_optional_v<T>;
  9175. if constexpr (is_op) {
  9176. return stack_detail::unchecked_get<T>(L, index, tracking);
  9177. }
  9178. else {
  9179. if (is_lua_reference<T>::value) {
  9180. return stack_detail::unchecked_get<T>(L, index, tracking);
  9181. }
  9182. auto op = check_get<T>(L, index, type_panic_c_str, tracking);
  9183. return *std::move(op);
  9184. }
  9185. #else
  9186. return stack_detail::unchecked_get<T>(L, index, tracking);
  9187. #endif
  9188. }
  9189. template <typename T>
  9190. decltype(auto) get(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9191. record tracking {};
  9192. return get<T>(L, index, tracking);
  9193. }
  9194. template <typename T>
  9195. decltype(auto) get_usertype(lua_State* L, int index, record& tracking) {
  9196. using UT = meta::conditional_t<std::is_pointer<T>::value, detail::as_pointer_tag<std::remove_pointer_t<T>>, detail::as_value_tag<T>>;
  9197. return get<UT>(L, index, tracking);
  9198. }
  9199. template <typename T>
  9200. decltype(auto) get_usertype(lua_State* L, int index = -lua_size<meta::unqualified_t<T>>::value) {
  9201. record tracking {};
  9202. return get_usertype<T>(L, index, tracking);
  9203. }
  9204. template <typename T>
  9205. decltype(auto) pop(lua_State* L) {
  9206. return popper<meta::unqualified_t<T>> {}.pop(L);
  9207. }
  9208. template <bool global = false, bool raw = false, typename Key>
  9209. void get_field(lua_State* L, Key&& key) {
  9210. field_getter<meta::unqualified_t<Key>, global, raw> {}.get(L, std::forward<Key>(key));
  9211. }
  9212. template <bool global = false, bool raw = false, typename Key>
  9213. void get_field(lua_State* L, Key&& key, int tableindex) {
  9214. field_getter<meta::unqualified_t<Key>, global, raw> {}.get(L, std::forward<Key>(key), tableindex);
  9215. }
  9216. template <bool global = false, typename Key>
  9217. void raw_get_field(lua_State* L, Key&& key) {
  9218. get_field<global, true>(L, std::forward<Key>(key));
  9219. }
  9220. template <bool global = false, typename Key>
  9221. void raw_get_field(lua_State* L, Key&& key, int tableindex) {
  9222. get_field<global, true>(L, std::forward<Key>(key), tableindex);
  9223. }
  9224. template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
  9225. probe probe_get_field(lua_State* L, Key&& key) {
  9226. return probe_field_getter<meta::unqualified_t<Key>, C, global, raw> {}.get(L, std::forward<Key>(key));
  9227. }
  9228. template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
  9229. probe probe_get_field(lua_State* L, Key&& key, int tableindex) {
  9230. return probe_field_getter<meta::unqualified_t<Key>, C, global, raw> {}.get(L, std::forward<Key>(key), tableindex);
  9231. }
  9232. template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
  9233. probe probe_raw_get_field(lua_State* L, Key&& key) {
  9234. return probe_get_field<global, true, C>(L, std::forward<Key>(key));
  9235. }
  9236. template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
  9237. probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) {
  9238. return probe_get_field<global, true, C>(L, std::forward<Key>(key), tableindex);
  9239. }
  9240. template <bool global = false, bool raw = false, typename Key, typename Value>
  9241. void set_field(lua_State* L, Key&& key, Value&& value) {
  9242. field_setter<meta::unqualified_t<Key>, global, raw> {}.set(L, std::forward<Key>(key), std::forward<Value>(value));
  9243. }
  9244. template <bool global = false, bool raw = false, typename Key, typename Value>
  9245. void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
  9246. field_setter<meta::unqualified_t<Key>, global, raw> {}.set(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
  9247. }
  9248. template <bool global = false, typename Key, typename Value>
  9249. void raw_set_field(lua_State* L, Key&& key, Value&& value) {
  9250. set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value));
  9251. }
  9252. template <bool global = false, typename Key, typename Value>
  9253. void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
  9254. set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
  9255. }
  9256. template <typename T, typename F>
  9257. void modify_unique_usertype_as(const stack_reference& obj, F&& f) {
  9258. using u_traits = unique_usertype_traits<T>;
  9259. void* raw = lua_touserdata(obj.lua_state(), obj.stack_index());
  9260. void* ptr_memory = detail::align_usertype_pointer(raw);
  9261. void* uu_memory = detail::align_usertype_unique<T>(raw);
  9262. T& uu = *static_cast<T*>(uu_memory);
  9263. f(uu);
  9264. *static_cast<void**>(ptr_memory) = static_cast<void*>(u_traits::get(uu));
  9265. }
  9266. template <typename F>
  9267. void modify_unique_usertype(const stack_reference& obj, F&& f) {
  9268. using bt = meta::bind_traits<meta::unqualified_t<F>>;
  9269. using T = typename bt::template arg_at<0>;
  9270. using Tu = meta::unqualified_t<T>;
  9271. modify_unique_usertype_as<Tu>(obj, std::forward<F>(f));
  9272. }
  9273. } // namespace stack
  9274. namespace detail {
  9275. template <typename T>
  9276. lua_CFunction make_destructor(std::true_type) {
  9277. if constexpr (is_unique_usertype_v<T>) {
  9278. return &unique_destruct<T>;
  9279. }
  9280. else if constexpr (!std::is_pointer_v<T>) {
  9281. return &usertype_alloc_destruct<T>;
  9282. }
  9283. else {
  9284. return &cannot_destruct<T>;
  9285. }
  9286. }
  9287. template <typename T>
  9288. lua_CFunction make_destructor(std::false_type) {
  9289. return &cannot_destruct<T>;
  9290. }
  9291. template <typename T>
  9292. lua_CFunction make_destructor() {
  9293. return make_destructor<T>(std::is_destructible<T>());
  9294. }
  9295. struct no_comp {
  9296. template <typename A, typename B>
  9297. bool operator()(A&&, B&&) const {
  9298. return false;
  9299. }
  9300. };
  9301. template <typename T>
  9302. int is_check(lua_State* L) {
  9303. return stack::push(L, stack::check<T>(L, 1, &no_panic));
  9304. }
  9305. template <typename T>
  9306. int member_default_to_string(std::true_type, lua_State* L) {
  9307. decltype(auto) ts = stack::get<T>(L, 1).to_string();
  9308. return stack::push(L, std::forward<decltype(ts)>(ts));
  9309. }
  9310. template <typename T>
  9311. int member_default_to_string(std::false_type, lua_State* L) {
  9312. return luaL_error(L,
  9313. "cannot perform to_string on '%s': no 'to_string' overload in namespace, 'to_string' member "
  9314. "function, or operator<<(ostream&, ...) present",
  9315. detail::demangle<T>().data());
  9316. }
  9317. template <typename T>
  9318. int adl_default_to_string(std::true_type, lua_State* L) {
  9319. using namespace std;
  9320. decltype(auto) ts = to_string(stack::get<T>(L, 1));
  9321. return stack::push(L, std::forward<decltype(ts)>(ts));
  9322. }
  9323. template <typename T>
  9324. int adl_default_to_string(std::false_type, lua_State* L) {
  9325. return member_default_to_string<T>(meta::supports_to_string_member<T>(), L);
  9326. }
  9327. template <typename T>
  9328. int oss_default_to_string(std::true_type, lua_State* L) {
  9329. std::ostringstream oss;
  9330. oss << stack::unqualified_get<T>(L, 1);
  9331. return stack::push(L, oss.str());
  9332. }
  9333. template <typename T>
  9334. int oss_default_to_string(std::false_type, lua_State* L) {
  9335. return adl_default_to_string<T>(meta::supports_adl_to_string<T>(), L);
  9336. }
  9337. template <typename T>
  9338. int default_to_string(lua_State* L) {
  9339. return oss_default_to_string<T>(meta::supports_op_left_shift<std::ostream, T>(), L);
  9340. }
  9341. template <typename T>
  9342. int default_size(lua_State* L) {
  9343. decltype(auto) self = stack::unqualified_get<T>(L, 1);
  9344. return stack::push(L, self.size());
  9345. }
  9346. template <typename T, typename Op>
  9347. int comparsion_operator_wrap(lua_State* L) {
  9348. if constexpr (std::is_void_v<T>) {
  9349. return stack::push(L, false);
  9350. }
  9351. else {
  9352. auto maybel = stack::unqualified_check_get<T>(L, 1);
  9353. if (!maybel) {
  9354. return stack::push(L, false);
  9355. }
  9356. auto mayber = stack::unqualified_check_get<T>(L, 2);
  9357. if (!mayber) {
  9358. return stack::push(L, false);
  9359. }
  9360. decltype(auto) l = *maybel;
  9361. decltype(auto) r = *mayber;
  9362. if constexpr (std::is_same_v<no_comp, Op>) {
  9363. std::equal_to<> op;
  9364. return stack::push(L, op(detail::ptr(l), detail::ptr(r)));
  9365. }
  9366. else {
  9367. if constexpr (std::is_same_v<std::equal_to<>, Op> // clang-format hack
  9368. || std::is_same_v<std::less_equal<>, Op> //
  9369. || std::is_same_v<std::less_equal<>, Op>) { //
  9370. if (detail::ptr(l) == detail::ptr(r)) {
  9371. return stack::push(L, true);
  9372. }
  9373. }
  9374. Op op;
  9375. return stack::push(L, op(detail::deref(l), detail::deref(r)));
  9376. }
  9377. }
  9378. }
  9379. template <typename T, typename IFx, typename Fx>
  9380. void insert_default_registrations(IFx&& ifx, Fx&& fx);
  9381. template <typename T, bool, bool>
  9382. struct get_is_primitive : is_lua_primitive<T> { };
  9383. template <typename T>
  9384. struct get_is_primitive<T, true, false>
  9385. : meta::neg<std::is_reference<decltype(sol_lua_get(types<T>(), nullptr, -1, std::declval<stack::record&>()))>> { };
  9386. template <typename T>
  9387. struct get_is_primitive<T, false, true>
  9388. : meta::neg<std::is_reference<decltype(sol_lua_get(types<meta::unqualified_t<T>>(), nullptr, -1, std::declval<stack::record&>()))>> { };
  9389. template <typename T>
  9390. struct get_is_primitive<T, true, true> : get_is_primitive<T, true, false> { };
  9391. } // namespace detail
  9392. template <typename T>
  9393. struct is_proxy_primitive
  9394. : detail::get_is_primitive<T, meta::meta_detail::is_adl_sol_lua_get_v<T>, meta::meta_detail::is_adl_sol_lua_get_v<meta::unqualified_t<T>>> { };
  9395. } // namespace sol
  9396. // end of sol/stack_core.hpp
  9397. // beginning of sol/stack_check.hpp
  9398. // beginning of sol/stack_check_unqualified.hpp
  9399. #include <memory>
  9400. #include <functional>
  9401. #include <utility>
  9402. #include <cmath>
  9403. #include <optional>
  9404. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  9405. #include <variant>
  9406. #endif // variant shenanigans
  9407. namespace sol { namespace stack {
  9408. namespace stack_detail {
  9409. inline bool impl_check_metatable(lua_State* L, int index, const std::string& metakey, bool poptable) {
  9410. luaL_getmetatable(L, &metakey[0]);
  9411. const type expectedmetatabletype = static_cast<type>(lua_type(L, -1));
  9412. if (expectedmetatabletype != type::lua_nil) {
  9413. if (lua_rawequal(L, -1, index) == 1) {
  9414. lua_pop(L, 1 + static_cast<int>(poptable));
  9415. return true;
  9416. }
  9417. }
  9418. lua_pop(L, 1);
  9419. return false;
  9420. }
  9421. template <typename T, bool poptable = true>
  9422. inline bool check_metatable(lua_State* L, int index = -2) {
  9423. return impl_check_metatable(L, index, usertype_traits<T>::metatable(), poptable);
  9424. }
  9425. template <type expected, int (*check_func)(lua_State*, int)>
  9426. struct basic_check {
  9427. template <typename Handler>
  9428. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9429. tracking.use(1);
  9430. bool success = check_func(L, index) == 1;
  9431. if (!success) {
  9432. // expected type, actual type
  9433. handler(L, index, expected, type_of(L, index), "");
  9434. }
  9435. return success;
  9436. }
  9437. };
  9438. } // namespace stack_detail
  9439. template <typename T, typename>
  9440. struct unqualified_interop_checker {
  9441. template <typename Handler>
  9442. static bool check(lua_State*, int, type, Handler&&, record&) {
  9443. return false;
  9444. }
  9445. };
  9446. template <typename T, typename>
  9447. struct qualified_interop_checker {
  9448. template <typename Handler>
  9449. static bool check(lua_State* L, int index, type index_type, Handler&& handler, record& tracking) {
  9450. return stack_detail::unqualified_interop_check<T>(L, index, index_type, std::forward<Handler>(handler), tracking);
  9451. }
  9452. };
  9453. template <typename T, type expected, typename>
  9454. struct unqualified_checker {
  9455. template <typename Handler>
  9456. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9457. if constexpr (std::is_same_v<T, bool>) {
  9458. tracking.use(1);
  9459. bool success = lua_isboolean(L, index) == 1;
  9460. if (!success) {
  9461. // expected type, actual type
  9462. handler(L, index, expected, type_of(L, index), "");
  9463. }
  9464. return success;
  9465. }
  9466. else if constexpr (meta::any_same_v<T, char /* , char8_t*/, char16_t, char32_t>) {
  9467. return stack::check<std::basic_string<T>>(L, index, std::forward<Handler>(handler), tracking);
  9468. }
  9469. else if constexpr (std::is_integral_v<T> || std::is_same_v<T, lua_Integer>) {
  9470. tracking.use(1);
  9471. #if SOL_LUA_VESION_I_ >= 503
  9472. // Lua 5.3 and greater checks for numeric precision
  9473. #if SOL_IS_ON(SOL_STRINGS_ARE_NUMBERS_I_)
  9474. // imprecise, sloppy conversions
  9475. int isnum = 0;
  9476. lua_tointegerx(L, index, &isnum);
  9477. const bool success = isnum != 0;
  9478. if (!success) {
  9479. // expected type, actual type
  9480. handler(L, index, type::number, type_of(L, index), detail::not_a_number_or_number_string_integral);
  9481. }
  9482. #elif SOL_IS_ON(SOL_NUMBER_PRECISION_CHECKS_I_)
  9483. // this check is precise, do not convert
  9484. if (lua_isinteger(L, index) == 1) {
  9485. return true;
  9486. }
  9487. const bool success = false;
  9488. if (!success) {
  9489. // expected type, actual type
  9490. handler(L, index, type::number, type_of(L, index), detail::not_a_number_integral);
  9491. }
  9492. #else
  9493. // Numerics are neither safe nor string-convertible
  9494. type t = type_of(L, index);
  9495. const bool success = t == type::number;
  9496. #endif
  9497. if (!success) {
  9498. // expected type, actual type
  9499. handler(L, index, type::number, type_of(L, index), detail::not_a_number);
  9500. }
  9501. return success;
  9502. #else
  9503. // Lua 5.2 and below checks
  9504. #if SOL_IS_OFF(SOL_STRINGS_ARE_NUMBERS_I_)
  9505. // must pre-check, because it will convert
  9506. type t = type_of(L, index);
  9507. if (t != type::number) {
  9508. // expected type, actual type
  9509. handler(L, index, type::number, t, detail::not_a_number);
  9510. return false;
  9511. }
  9512. #endif // Do not allow strings to be numbers
  9513. #if SOL_IS_ON(SOL_NUMBER_PRECISION_CHECKS_I_)
  9514. int isnum = 0;
  9515. const lua_Number v = lua_tonumberx(L, index, &isnum);
  9516. const bool success = isnum != 0 && static_cast<lua_Number>(llround(v)) == v;
  9517. #else
  9518. const bool success = true;
  9519. #endif // Safe numerics and number precision checking
  9520. if (!success) {
  9521. // Use defines to provide a better error message!
  9522. #if SOL_IS_ON(SOL_STRINGS_ARE_NUMBERS_I_)
  9523. handler(L, index, type::number, type_of(L, index), detail::not_a_number_or_number_string);
  9524. #elif SOL_IS_ON(SOL_NUMBER_PRECISION_CHECKS_I_)
  9525. handler(L, index, type::number, t, detail::not_a_number_or_number_string);
  9526. #else
  9527. handler(L, index, type::number, t, detail::not_a_number);
  9528. #endif
  9529. }
  9530. return success;
  9531. #endif
  9532. }
  9533. else if constexpr (std::is_floating_point_v<T> || std::is_same_v<T, lua_Number>) {
  9534. tracking.use(1);
  9535. #if SOL_IS_ON(SOL_STRINGS_ARE_NUMBERS_I_)
  9536. bool success = lua_isnumber(L, index) == 1;
  9537. if (!success) {
  9538. // expected type, actual type
  9539. handler(L, index, type::number, type_of(L, index), detail::not_a_number_or_number_string);
  9540. }
  9541. return success;
  9542. #else
  9543. type t = type_of(L, index);
  9544. bool success = t == type::number;
  9545. if (!success) {
  9546. // expected type, actual type
  9547. handler(L, index, type::number, t, detail::not_a_number);
  9548. }
  9549. return success;
  9550. #endif // Strings are Numbers
  9551. }
  9552. else if constexpr (meta::any_same_v<T, type, this_state, this_main_state, this_environment, variadic_args>) {
  9553. (void)L;
  9554. (void)index;
  9555. (void)handler;
  9556. tracking.use(0);
  9557. return true;
  9558. }
  9559. else if constexpr (is_unique_usertype_v<T>) {
  9560. using proper_T = typename unique_usertype_traits<T>::type;
  9561. const type indextype = type_of(L, index);
  9562. tracking.use(1);
  9563. if (indextype != type::userdata) {
  9564. handler(L, index, type::userdata, indextype, "value is not a userdata");
  9565. return false;
  9566. }
  9567. if (lua_getmetatable(L, index) == 0) {
  9568. return true;
  9569. }
  9570. int metatableindex = lua_gettop(L);
  9571. if (stack_detail::check_metatable<detail::unique_usertype<proper_T>>(L, metatableindex)) {
  9572. void* memory = lua_touserdata(L, index);
  9573. memory = detail::align_usertype_unique_destructor(memory);
  9574. detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
  9575. bool success = &detail::usertype_unique_alloc_destroy<proper_T, T> == pdx;
  9576. if (!success) {
  9577. memory = detail::align_usertype_unique_tag<true>(memory);
  9578. #if 0
  9579. // New version
  9580. #else
  9581. const char*& name_tag = *static_cast<const char**>(memory);
  9582. success = usertype_traits<T>::qualified_name() == name_tag;
  9583. #endif
  9584. if (!success) {
  9585. handler(L, index, type::userdata, indextype, "value is a userdata but is not the correct unique usertype");
  9586. }
  9587. }
  9588. return success;
  9589. }
  9590. lua_pop(L, 1);
  9591. handler(L, index, type::userdata, indextype, "unrecognized userdata (not pushed by sol?)");
  9592. return false;
  9593. }
  9594. else if constexpr (meta::any_same_v<T, lua_nil_t, std::nullopt_t, nullopt_t>) {
  9595. bool success = lua_isnil(L, index);
  9596. if (success) {
  9597. tracking.use(1);
  9598. return success;
  9599. }
  9600. tracking.use(0);
  9601. success = lua_isnone(L, index);
  9602. if (!success) {
  9603. // expected type, actual type
  9604. handler(L, index, expected, type_of(L, index), "");
  9605. }
  9606. return success;
  9607. }
  9608. else if constexpr (std::is_same_v<T, env_key_t>) {
  9609. tracking.use(1);
  9610. type t = type_of(L, index);
  9611. if (t == type::table || t == type::none || t == type::lua_nil || t == type::userdata) {
  9612. return true;
  9613. }
  9614. handler(L, index, type::table, t, "value cannot not have a valid environment");
  9615. return true;
  9616. }
  9617. else if constexpr (std::is_same_v<T, detail::non_lua_nil_t>) {
  9618. return !stack::unqualified_check<lua_nil_t>(L, index, std::forward<Handler>(handler), tracking);
  9619. }
  9620. else if constexpr (meta::is_specialization_of_v<T, basic_lua_table>) {
  9621. tracking.use(1);
  9622. type t = type_of(L, index);
  9623. if (t != type::table) {
  9624. handler(L, index, type::table, t, "value is not a table");
  9625. return false;
  9626. }
  9627. return true;
  9628. }
  9629. else if constexpr (meta::is_specialization_of_v<T, basic_bytecode>) {
  9630. tracking.use(1);
  9631. type t = type_of(L, index);
  9632. if (t != type::function) {
  9633. handler(L, index, type::function, t, "value is not a function that can be dumped");
  9634. return false;
  9635. }
  9636. return true;
  9637. }
  9638. else if constexpr (meta::is_specialization_of_v<T, basic_environment>) {
  9639. tracking.use(1);
  9640. if (lua_getmetatable(L, index) == 0) {
  9641. return true;
  9642. }
  9643. type t = type_of(L, -1);
  9644. if (t == type::table || t == type::none || t == type::lua_nil) {
  9645. lua_pop(L, 1);
  9646. return true;
  9647. }
  9648. if (t != type::userdata) {
  9649. lua_pop(L, 1);
  9650. handler(L, index, type::table, t, "value does not have a valid metatable");
  9651. return false;
  9652. }
  9653. return true;
  9654. }
  9655. else if constexpr (std::is_same_v<T, metatable_key_t>) {
  9656. tracking.use(1);
  9657. if (lua_getmetatable(L, index) == 0) {
  9658. return true;
  9659. }
  9660. type t = type_of(L, -1);
  9661. if (t == type::table || t == type::none || t == type::lua_nil) {
  9662. lua_pop(L, 1);
  9663. return true;
  9664. }
  9665. if (t != type::userdata) {
  9666. lua_pop(L, 1);
  9667. handler(L, index, expected, t, "value does not have a valid metatable");
  9668. return false;
  9669. }
  9670. return true;
  9671. }
  9672. else if constexpr (std::is_same_v<T, luaL_Stream*> || std::is_same_v<T, luaL_Stream>) {
  9673. if (lua_getmetatable(L, index) == 0) {
  9674. type t = type_of(L, index);
  9675. handler(L, index, expected, t, "value is not a valid luaL_Stream (has no metatable/is not a valid value)");
  9676. return false;
  9677. }
  9678. luaL_getmetatable(L, LUA_FILEHANDLE);
  9679. if (type_of(L, index) != type::table) {
  9680. type t = type_of(L, index);
  9681. lua_pop(L, 1);
  9682. handler(L,
  9683. index,
  9684. expected,
  9685. t,
  9686. "value is not a valid luaL_Stream (there is no metatable for luaL_Stream -- did you forget to "
  9687. "my_lua_state.open_libraries(sol::lib::state) or equivalent?)");
  9688. return false;
  9689. }
  9690. int is_stream_table = lua_compare(L, -1, -2, LUA_OPEQ);
  9691. lua_pop(L, 2);
  9692. if (is_stream_table == 0) {
  9693. type t = type_of(L, index);
  9694. handler(L, index, expected, t, "value is not a valid luaL_Stream (incorrect metatable)");
  9695. return false;
  9696. }
  9697. return true;
  9698. }
  9699. else if constexpr (meta::is_optional_v<T>) {
  9700. using ValueType = typename T::value_type;
  9701. (void)handler;
  9702. type t = type_of(L, index);
  9703. if (t == type::none) {
  9704. tracking.use(0);
  9705. return true;
  9706. }
  9707. if (t == type::lua_nil) {
  9708. tracking.use(1);
  9709. return true;
  9710. }
  9711. return stack::unqualified_check<ValueType>(L, index, no_panic, tracking);
  9712. }
  9713. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  9714. else if constexpr (std::is_function_v<T> || (std::is_pointer_v<T> && std::is_function_v<std::remove_pointer_t<T>>)) {
  9715. return stack_detail::check_function_pointer<std::remove_pointer_t<T>>(L, index, std::forward<Handler>(handler), tracking);
  9716. }
  9717. #endif
  9718. else if constexpr (expected == type::userdata) {
  9719. if constexpr (meta::any_same_v<T, userdata_value> || meta::is_specialization_of_v<T, basic_userdata>) {
  9720. tracking.use(1);
  9721. type t = type_of(L, index);
  9722. bool success = t == type::userdata;
  9723. if (!success) {
  9724. // expected type, actual type
  9725. handler(L, index, type::userdata, t, "");
  9726. }
  9727. return success;
  9728. }
  9729. else if constexpr (meta::is_specialization_of_v<T, user>) {
  9730. unqualified_checker<lightuserdata_value, type::userdata> c;
  9731. (void)c;
  9732. return c.check(L, index, std::forward<Handler>(handler), tracking);
  9733. }
  9734. else {
  9735. if constexpr (std::is_pointer_v<T>) {
  9736. return check_usertype<T>(L, index, std::forward<Handler>(handler), tracking);
  9737. }
  9738. else if constexpr (meta::is_specialization_of_v<T, std::reference_wrapper>) {
  9739. using T_internal = typename T::type;
  9740. return stack::check<T_internal>(L, index, std::forward<Handler>(handler), tracking);
  9741. }
  9742. else {
  9743. return check_usertype<T>(L, index, std::forward<Handler>(handler), tracking);
  9744. }
  9745. }
  9746. }
  9747. else if constexpr (expected == type::poly) {
  9748. tracking.use(1);
  9749. bool success = is_lua_reference_v<T> || !lua_isnone(L, index);
  9750. if (!success) {
  9751. // expected type, actual type
  9752. handler(L, index, type::poly, type_of(L, index), "");
  9753. }
  9754. return success;
  9755. }
  9756. else if constexpr (expected == type::lightuserdata) {
  9757. tracking.use(1);
  9758. type t = type_of(L, index);
  9759. bool success = t == type::userdata || t == type::lightuserdata;
  9760. if (!success) {
  9761. // expected type, actual type
  9762. handler(L, index, type::lightuserdata, t, "");
  9763. }
  9764. return success;
  9765. }
  9766. else if constexpr (expected == type::function) {
  9767. if constexpr (meta::any_same_v<T, lua_CFunction, std::remove_pointer_t<lua_CFunction>, c_closure>) {
  9768. tracking.use(1);
  9769. bool success = lua_iscfunction(L, index) == 1;
  9770. if (!success) {
  9771. // expected type, actual type
  9772. handler(L, index, expected, type_of(L, index), "");
  9773. }
  9774. return success;
  9775. }
  9776. else {
  9777. tracking.use(1);
  9778. type t = type_of(L, index);
  9779. if (t == type::lua_nil || t == type::none || t == type::function) {
  9780. // allow for lua_nil to be returned
  9781. return true;
  9782. }
  9783. if (t != type::userdata && t != type::table) {
  9784. handler(L, index, type::function, t, "must be a function or table or a userdata");
  9785. return false;
  9786. }
  9787. // Do advanced check for call-style userdata?
  9788. static const auto& callkey = to_string(meta_function::call);
  9789. if (lua_getmetatable(L, index) == 0) {
  9790. // No metatable, no __call key possible
  9791. handler(L, index, type::function, t, "value is not a function and does not have overriden metatable");
  9792. return false;
  9793. }
  9794. if (lua_isnoneornil(L, -1)) {
  9795. lua_pop(L, 1);
  9796. handler(L, index, type::function, t, "value is not a function and does not have valid metatable");
  9797. return false;
  9798. }
  9799. lua_getfield(L, -1, &callkey[0]);
  9800. if (lua_isnoneornil(L, -1)) {
  9801. lua_pop(L, 2);
  9802. handler(L, index, type::function, t, "value's metatable does not have __call overridden in metatable, cannot call this type");
  9803. return false;
  9804. }
  9805. // has call, is definitely a function
  9806. lua_pop(L, 2);
  9807. return true;
  9808. }
  9809. }
  9810. else if constexpr (expected == type::table) {
  9811. tracking.use(1);
  9812. type t = type_of(L, index);
  9813. if (t == type::table) {
  9814. return true;
  9815. }
  9816. if (t != type::userdata) {
  9817. handler(L, index, type::table, t, "value is not a table or a userdata that can behave like one");
  9818. return false;
  9819. }
  9820. return true;
  9821. }
  9822. else {
  9823. tracking.use(1);
  9824. const type indextype = type_of(L, index);
  9825. bool success = expected == indextype;
  9826. if (!success) {
  9827. // expected type, actual type, message
  9828. handler(L, index, expected, indextype, "");
  9829. }
  9830. return success;
  9831. }
  9832. }
  9833. };
  9834. template <typename T>
  9835. struct unqualified_checker<non_null<T>, type::userdata> : unqualified_checker<T, lua_type_of_v<T>> { };
  9836. template <typename T>
  9837. struct unqualified_checker<detail::as_value_tag<T>, type::userdata> {
  9838. template <typename Handler>
  9839. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9840. const type indextype = type_of(L, index);
  9841. return check(types<T>(), L, index, indextype, std::forward<Handler>(handler), tracking);
  9842. }
  9843. template <typename U, typename Handler>
  9844. static bool check(types<U>, lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
  9845. if constexpr (
  9846. std::is_same_v<T,
  9847. lightuserdata_value> || std::is_same_v<T, userdata_value> || std::is_same_v<T, userdata> || std::is_same_v<T, lightuserdata>) {
  9848. tracking.use(1);
  9849. if (indextype != type::userdata) {
  9850. handler(L, index, type::userdata, indextype, "value is not a valid userdata");
  9851. return false;
  9852. }
  9853. return true;
  9854. }
  9855. else {
  9856. #if SOL_IS_ON(SOL_USE_INTEROP_I_)
  9857. if (stack_detail::interop_check<U>(L, index, indextype, handler, tracking)) {
  9858. return true;
  9859. }
  9860. #endif // interop extensibility
  9861. tracking.use(1);
  9862. if (indextype != type::userdata) {
  9863. handler(L, index, type::userdata, indextype, "value is not a valid userdata");
  9864. return false;
  9865. }
  9866. if (lua_getmetatable(L, index) == 0) {
  9867. return true;
  9868. }
  9869. int metatableindex = lua_gettop(L);
  9870. if (stack_detail::check_metatable<U>(L, metatableindex))
  9871. return true;
  9872. if (stack_detail::check_metatable<U*>(L, metatableindex))
  9873. return true;
  9874. if (stack_detail::check_metatable<detail::unique_usertype<U>>(L, metatableindex))
  9875. return true;
  9876. if (stack_detail::check_metatable<as_container_t<U>>(L, metatableindex))
  9877. return true;
  9878. bool success = false;
  9879. bool has_derived = derive<T>::value || weak_derive<T>::value;
  9880. if (has_derived) {
  9881. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  9882. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  9883. #endif // make sure stack doesn't overflow
  9884. auto pn = stack::pop_n(L, 1);
  9885. lua_pushstring(L, &detail::base_class_check_key()[0]);
  9886. lua_rawget(L, metatableindex);
  9887. if (type_of(L, -1) != type::lua_nil) {
  9888. void* basecastdata = lua_touserdata(L, -1);
  9889. detail::inheritance_check_function ic = reinterpret_cast<detail::inheritance_check_function>(basecastdata);
  9890. success = ic(usertype_traits<T>::qualified_name());
  9891. }
  9892. }
  9893. lua_pop(L, 1);
  9894. if (!success) {
  9895. handler(L, index, type::userdata, indextype, "value at this index does not properly reflect the desired type");
  9896. return false;
  9897. }
  9898. return true;
  9899. }
  9900. }
  9901. };
  9902. template <typename T>
  9903. struct unqualified_checker<detail::as_pointer_tag<T>, type::userdata> {
  9904. template <typename Handler>
  9905. static bool check(lua_State* L, int index, type indextype, Handler&& handler, record& tracking) {
  9906. if (indextype == type::lua_nil) {
  9907. tracking.use(1);
  9908. return true;
  9909. }
  9910. return check_usertype<std::remove_pointer_t<T>>(L, index, std::forward<Handler>(handler), tracking);
  9911. }
  9912. template <typename Handler>
  9913. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9914. const type indextype = type_of(L, index);
  9915. return check(L, index, indextype, std::forward<Handler>(handler), tracking);
  9916. }
  9917. };
  9918. template <typename... Args>
  9919. struct unqualified_checker<std::tuple<Args...>, type::poly> {
  9920. template <typename Handler>
  9921. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9922. return stack::multi_check<Args...>(L, index, std::forward<Handler>(handler), tracking);
  9923. }
  9924. };
  9925. template <typename A, typename B>
  9926. struct unqualified_checker<std::pair<A, B>, type::poly> {
  9927. template <typename Handler>
  9928. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9929. return stack::multi_check<A, B>(L, index, std::forward<Handler>(handler), tracking);
  9930. }
  9931. };
  9932. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  9933. template <typename... Tn>
  9934. struct unqualified_checker<std::variant<Tn...>, type::poly> {
  9935. typedef std::variant<Tn...> V;
  9936. typedef std::variant_size<V> V_size;
  9937. typedef std::integral_constant<bool, V_size::value == 0> V_is_empty;
  9938. template <typename Handler>
  9939. static bool is_one(std::integral_constant<std::size_t, 0>, lua_State* L, int index, Handler&& handler, record& tracking) {
  9940. if constexpr (V_is_empty::value) {
  9941. if (lua_isnone(L, index)) {
  9942. return true;
  9943. }
  9944. }
  9945. tracking.use(1);
  9946. handler(L, index, type::poly, type_of(L, index), "value does not fit any type present in the variant");
  9947. return false;
  9948. }
  9949. template <std::size_t I, typename Handler>
  9950. static bool is_one(std::integral_constant<std::size_t, I>, lua_State* L, int index, Handler&& handler, record& tracking) {
  9951. typedef std::variant_alternative_t<I - 1, V> T;
  9952. record temp_tracking = tracking;
  9953. if (stack::check<T>(L, index, no_panic, temp_tracking)) {
  9954. tracking = temp_tracking;
  9955. return true;
  9956. }
  9957. return is_one(std::integral_constant<std::size_t, I - 1>(), L, index, std::forward<Handler>(handler), tracking);
  9958. }
  9959. template <typename Handler>
  9960. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9961. return is_one(std::integral_constant<std::size_t, V_size::value>(), L, index, std::forward<Handler>(handler), tracking);
  9962. }
  9963. };
  9964. #endif // variant shenanigans
  9965. }} // namespace sol::stack
  9966. // end of sol/stack_check_unqualified.hpp
  9967. // beginning of sol/stack_check_qualified.hpp
  9968. namespace sol {
  9969. namespace stack {
  9970. template <typename X, type expected, typename>
  9971. struct qualified_checker {
  9972. template <typename Handler>
  9973. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  9974. if constexpr (!std::is_reference_v<X> && is_unique_usertype_v<X>) {
  9975. using u_traits = unique_usertype_traits<meta::unqualified_t<X>>;
  9976. using T = typename u_traits::type;
  9977. if constexpr (is_base_rebindable_non_void_v<u_traits>) {
  9978. using rebind_t = typename u_traits::template rebind_base<void>;
  9979. // we have a unique pointer type that can be
  9980. // rebound to a base/derived type
  9981. const type indextype = type_of(L, index);
  9982. tracking.use(1);
  9983. if (indextype != type::userdata) {
  9984. handler(L, index, type::userdata, indextype, "value is not a userdata");
  9985. return false;
  9986. }
  9987. void* memory = lua_touserdata(L, index);
  9988. memory = detail::align_usertype_unique_destructor(memory);
  9989. detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
  9990. if (&detail::usertype_unique_alloc_destroy<T, X> == pdx) {
  9991. return true;
  9992. }
  9993. if constexpr (derive<T>::value) {
  9994. memory = detail::align_usertype_unique_tag<true, false>(memory);
  9995. detail::unique_tag& ic = *reinterpret_cast<detail::unique_tag*>(memory);
  9996. string_view ti = usertype_traits<T>::qualified_name();
  9997. string_view rebind_ti = usertype_traits<rebind_t>::qualified_name();
  9998. if (ic(nullptr, nullptr, ti, rebind_ti) != 0) {
  9999. return true;
  10000. }
  10001. }
  10002. handler(L, index, type::userdata, indextype, "value is a userdata but is not the correct unique usertype");
  10003. return false;
  10004. }
  10005. else {
  10006. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  10007. }
  10008. }
  10009. else if constexpr (!std::is_reference_v<X> && is_container_v<X>) {
  10010. if (type_of(L, index) == type::userdata) {
  10011. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  10012. }
  10013. else {
  10014. return stack::unqualified_check<nested<X>>(L, index, std::forward<Handler>(handler), tracking);
  10015. }
  10016. }
  10017. else if constexpr (!std::is_reference_v<X> && meta::is_specialization_of_v<X, nested>) {
  10018. using NestedX = typename meta::unqualified_t<X>::nested_type;
  10019. return stack::check<NestedX>(L, index, ::std::forward<Handler>(handler), tracking);
  10020. }
  10021. else {
  10022. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  10023. }
  10024. }
  10025. };
  10026. }
  10027. } // namespace sol::stack
  10028. // end of sol/stack_check_qualified.hpp
  10029. // end of sol/stack_check.hpp
  10030. // beginning of sol/stack_get.hpp
  10031. // beginning of sol/stack_get_unqualified.hpp
  10032. // beginning of sol/overload.hpp
  10033. #include <utility>
  10034. namespace sol {
  10035. template <typename... Functions>
  10036. struct overload_set {
  10037. std::tuple<Functions...> functions;
  10038. template <typename Arg, typename... Args, meta::disable<std::is_same<overload_set, meta::unqualified_t<Arg>>> = meta::enabler>
  10039. overload_set(Arg&& arg, Args&&... args)
  10040. : functions(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  10041. }
  10042. overload_set(const overload_set&) = default;
  10043. overload_set(overload_set&&) = default;
  10044. overload_set& operator=(const overload_set&) = default;
  10045. overload_set& operator=(overload_set&&) = default;
  10046. };
  10047. template <typename... Args>
  10048. decltype(auto) overload(Args&&... args) {
  10049. return overload_set<std::decay_t<Args>...>(std::forward<Args>(args)...);
  10050. }
  10051. } // namespace sol
  10052. // end of sol/overload.hpp
  10053. // beginning of sol/unicode.hpp
  10054. #include <array>
  10055. #include <cstring>
  10056. namespace sol {
  10057. // Everything here was lifted pretty much straight out of
  10058. // ogonek, because fuck figuring it out=
  10059. namespace unicode {
  10060. enum class error_code {
  10061. ok = 0,
  10062. invalid_code_point,
  10063. invalid_code_unit,
  10064. invalid_leading_surrogate,
  10065. invalid_trailing_surrogate,
  10066. sequence_too_short,
  10067. overlong_sequence,
  10068. };
  10069. inline const string_view& to_string(error_code ec) {
  10070. static const string_view storage[7] = {
  10071. "ok",
  10072. "invalid code points",
  10073. "invalid code unit",
  10074. "invalid leading surrogate",
  10075. "invalid trailing surrogate",
  10076. "sequence too short",
  10077. "overlong sequence"
  10078. };
  10079. return storage[static_cast<std::size_t>(ec)];
  10080. }
  10081. template <typename It>
  10082. struct decoded_result {
  10083. error_code error;
  10084. char32_t codepoint;
  10085. It next;
  10086. };
  10087. template <typename C>
  10088. struct encoded_result {
  10089. error_code error;
  10090. std::size_t code_units_size;
  10091. std::array<C, 4> code_units;
  10092. };
  10093. struct unicode_detail {
  10094. // codepoint related
  10095. static constexpr char32_t last_code_point = 0x10FFFF;
  10096. static constexpr char32_t first_lead_surrogate = 0xD800;
  10097. static constexpr char32_t last_lead_surrogate = 0xDBFF;
  10098. static constexpr char32_t first_trail_surrogate = 0xDC00;
  10099. static constexpr char32_t last_trail_surrogate = 0xDFFF;
  10100. static constexpr char32_t first_surrogate = first_lead_surrogate;
  10101. static constexpr char32_t last_surrogate = last_trail_surrogate;
  10102. static constexpr bool is_lead_surrogate(char32_t u) {
  10103. return u >= first_lead_surrogate && u <= last_lead_surrogate;
  10104. }
  10105. static constexpr bool is_trail_surrogate(char32_t u) {
  10106. return u >= first_trail_surrogate && u <= last_trail_surrogate;
  10107. }
  10108. static constexpr bool is_surrogate(char32_t u) {
  10109. return u >= first_surrogate && u <= last_surrogate;
  10110. }
  10111. // utf8 related
  10112. static constexpr auto last_1byte_value = 0x7Fu;
  10113. static constexpr auto last_2byte_value = 0x7FFu;
  10114. static constexpr auto last_3byte_value = 0xFFFFu;
  10115. static constexpr auto start_2byte_mask = 0x80u;
  10116. static constexpr auto start_3byte_mask = 0xE0u;
  10117. static constexpr auto start_4byte_mask = 0xF0u;
  10118. static constexpr auto continuation_mask = 0xC0u;
  10119. static constexpr auto continuation_signature = 0x80u;
  10120. static constexpr bool is_invalid(unsigned char b) {
  10121. return b == 0xC0 || b == 0xC1 || b > 0xF4;
  10122. }
  10123. static constexpr bool is_continuation(unsigned char b) {
  10124. return (b & unicode_detail::continuation_mask) == unicode_detail::continuation_signature;
  10125. }
  10126. static constexpr bool is_overlong(char32_t u, std::size_t bytes) {
  10127. return u <= unicode_detail::last_1byte_value || (u <= unicode_detail::last_2byte_value && bytes > 2)
  10128. || (u <= unicode_detail::last_3byte_value && bytes > 3);
  10129. }
  10130. static constexpr int sequence_length(unsigned char b) {
  10131. return (b & start_2byte_mask) == 0 ? 1
  10132. : (b & start_3byte_mask) != start_3byte_mask ? 2
  10133. : (b & start_4byte_mask) != start_4byte_mask ? 3
  10134. : 4;
  10135. }
  10136. static constexpr char32_t decode(unsigned char b0, unsigned char b1) {
  10137. return ((b0 & 0x1F) << 6) | (b1 & 0x3F);
  10138. }
  10139. static constexpr char32_t decode(unsigned char b0, unsigned char b1, unsigned char b2) {
  10140. return ((b0 & 0x0F) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F);
  10141. }
  10142. static constexpr char32_t decode(unsigned char b0, unsigned char b1, unsigned char b2, unsigned char b3) {
  10143. return ((b0 & 0x07) << 18) | ((b1 & 0x3F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F);
  10144. }
  10145. // utf16 related
  10146. static constexpr char32_t last_bmp_value = 0xFFFF;
  10147. static constexpr char32_t normalizing_value = 0x10000;
  10148. static constexpr int lead_surrogate_bitmask = 0xFFC00;
  10149. static constexpr int trail_surrogate_bitmask = 0x3FF;
  10150. static constexpr int lead_shifted_bits = 10;
  10151. static constexpr char32_t replacement = 0xFFFD;
  10152. static char32_t combine_surrogates(char16_t lead, char16_t trail) {
  10153. auto hi = lead - first_lead_surrogate;
  10154. auto lo = trail - first_trail_surrogate;
  10155. return normalizing_value + ((hi << lead_shifted_bits) | lo);
  10156. }
  10157. };
  10158. inline encoded_result<char> code_point_to_utf8(char32_t codepoint) {
  10159. encoded_result<char> er;
  10160. er.error = error_code::ok;
  10161. if (codepoint <= unicode_detail::last_1byte_value) {
  10162. er.code_units_size = 1;
  10163. er.code_units = std::array<char, 4>{ { static_cast<char>(codepoint) } };
  10164. }
  10165. else if (codepoint <= unicode_detail::last_2byte_value) {
  10166. er.code_units_size = 2;
  10167. er.code_units = std::array<char, 4>{{
  10168. static_cast<char>(0xC0 | ((codepoint & 0x7C0) >> 6)),
  10169. static_cast<char>(0x80 | (codepoint & 0x3F)),
  10170. }};
  10171. }
  10172. else if (codepoint <= unicode_detail::last_3byte_value) {
  10173. er.code_units_size = 3;
  10174. er.code_units = std::array<char, 4>{{
  10175. static_cast<char>(0xE0 | ((codepoint & 0xF000) >> 12)),
  10176. static_cast<char>(0x80 | ((codepoint & 0xFC0) >> 6)),
  10177. static_cast<char>(0x80 | (codepoint & 0x3F)),
  10178. }};
  10179. }
  10180. else {
  10181. er.code_units_size = 4;
  10182. er.code_units = std::array<char, 4>{ {
  10183. static_cast<char>(0xF0 | ((codepoint & 0x1C0000) >> 18)),
  10184. static_cast<char>(0x80 | ((codepoint & 0x3F000) >> 12)),
  10185. static_cast<char>(0x80 | ((codepoint & 0xFC0) >> 6)),
  10186. static_cast<char>(0x80 | (codepoint & 0x3F)),
  10187. } };
  10188. }
  10189. return er;
  10190. }
  10191. inline encoded_result<char16_t> code_point_to_utf16(char32_t codepoint) {
  10192. encoded_result<char16_t> er;
  10193. if (codepoint <= unicode_detail::last_bmp_value) {
  10194. er.code_units_size = 1;
  10195. er.code_units = std::array<char16_t, 4>{ { static_cast<char16_t>(codepoint) } };
  10196. er.error = error_code::ok;
  10197. }
  10198. else {
  10199. auto normal = codepoint - unicode_detail::normalizing_value;
  10200. auto lead = unicode_detail::first_lead_surrogate + ((normal & unicode_detail::lead_surrogate_bitmask) >> unicode_detail::lead_shifted_bits);
  10201. auto trail = unicode_detail::first_trail_surrogate + (normal & unicode_detail::trail_surrogate_bitmask);
  10202. er.code_units = std::array<char16_t, 4>{ {
  10203. static_cast<char16_t>(lead),
  10204. static_cast<char16_t>(trail)
  10205. } };
  10206. er.code_units_size = 2;
  10207. er.error = error_code::ok;
  10208. }
  10209. return er;
  10210. }
  10211. inline encoded_result<char32_t> code_point_to_utf32(char32_t codepoint) {
  10212. encoded_result<char32_t> er;
  10213. er.code_units_size = 1;
  10214. er.code_units[0] = codepoint;
  10215. er.error = error_code::ok;
  10216. return er;
  10217. }
  10218. template <typename It>
  10219. inline decoded_result<It> utf8_to_code_point(It it, It last) {
  10220. decoded_result<It> dr;
  10221. if (it == last) {
  10222. dr.next = it;
  10223. dr.error = error_code::sequence_too_short;
  10224. return dr;
  10225. }
  10226. unsigned char b0 = *it;
  10227. std::size_t length = unicode_detail::sequence_length(b0);
  10228. if (length == 1) {
  10229. dr.codepoint = static_cast<char32_t>(b0);
  10230. dr.error = error_code::ok;
  10231. ++it;
  10232. dr.next = it;
  10233. return dr;
  10234. }
  10235. if (unicode_detail::is_invalid(b0) || unicode_detail::is_continuation(b0)) {
  10236. dr.error = error_code::invalid_code_unit;
  10237. dr.next = it;
  10238. return dr;
  10239. }
  10240. ++it;
  10241. std::array<unsigned char, 4> b;
  10242. b[0] = b0;
  10243. for (std::size_t i = 1; i < length; ++i) {
  10244. b[i] = *it;
  10245. if (!unicode_detail::is_continuation(b[i])) {
  10246. dr.error = error_code::invalid_code_unit;
  10247. dr.next = it;
  10248. return dr;
  10249. }
  10250. ++it;
  10251. }
  10252. char32_t decoded;
  10253. switch (length) {
  10254. case 2:
  10255. decoded = unicode_detail::decode(b[0], b[1]);
  10256. break;
  10257. case 3:
  10258. decoded = unicode_detail::decode(b[0], b[1], b[2]);
  10259. break;
  10260. default:
  10261. decoded = unicode_detail::decode(b[0], b[1], b[2], b[3]);
  10262. break;
  10263. }
  10264. if (unicode_detail::is_overlong(decoded, length)) {
  10265. dr.error = error_code::overlong_sequence;
  10266. return dr;
  10267. }
  10268. if (unicode_detail::is_surrogate(decoded) || decoded > unicode_detail::last_code_point) {
  10269. dr.error = error_code::invalid_code_point;
  10270. return dr;
  10271. }
  10272. // then everything is fine
  10273. dr.codepoint = decoded;
  10274. dr.error = error_code::ok;
  10275. dr.next = it;
  10276. return dr;
  10277. }
  10278. template <typename It>
  10279. inline decoded_result<It> utf16_to_code_point(It it, It last) {
  10280. decoded_result<It> dr;
  10281. if (it == last) {
  10282. dr.next = it;
  10283. dr.error = error_code::sequence_too_short;
  10284. return dr;
  10285. }
  10286. char16_t lead = static_cast<char16_t>(*it);
  10287. if (!unicode_detail::is_surrogate(lead)) {
  10288. ++it;
  10289. dr.codepoint = static_cast<char32_t>(lead);
  10290. dr.next = it;
  10291. dr.error = error_code::ok;
  10292. return dr;
  10293. }
  10294. if (!unicode_detail::is_lead_surrogate(lead)) {
  10295. dr.error = error_code::invalid_leading_surrogate;
  10296. dr.next = it;
  10297. return dr;
  10298. }
  10299. ++it;
  10300. auto trail = *it;
  10301. if (!unicode_detail::is_trail_surrogate(trail)) {
  10302. dr.error = error_code::invalid_trailing_surrogate;
  10303. dr.next = it;
  10304. return dr;
  10305. }
  10306. dr.codepoint = unicode_detail::combine_surrogates(lead, trail);
  10307. dr.next = ++it;
  10308. dr.error = error_code::ok;
  10309. return dr;
  10310. }
  10311. template <typename It>
  10312. inline decoded_result<It> utf32_to_code_point(It it, It last) {
  10313. decoded_result<It> dr;
  10314. if (it == last) {
  10315. dr.next = it;
  10316. dr.error = error_code::sequence_too_short;
  10317. return dr;
  10318. }
  10319. dr.codepoint = static_cast<char32_t>(*it);
  10320. dr.next = ++it;
  10321. dr.error = error_code::ok;
  10322. return dr;
  10323. }
  10324. }
  10325. }
  10326. // end of sol/unicode.hpp
  10327. #include <memory>
  10328. #include <functional>
  10329. #include <utility>
  10330. #include <cstdlib>
  10331. #include <cmath>
  10332. #include <string_view>
  10333. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  10334. #include <variant>
  10335. #endif // Apple clang screwed up
  10336. namespace sol { namespace stack {
  10337. namespace stack_detail {
  10338. template <typename Ch>
  10339. struct count_code_units_utf {
  10340. std::size_t needed_size;
  10341. count_code_units_utf() : needed_size(0) {
  10342. }
  10343. void operator()(const unicode::encoded_result<Ch> er) {
  10344. needed_size += er.code_units_size;
  10345. }
  10346. };
  10347. template <typename Ch, typename ErCh>
  10348. struct copy_code_units_utf {
  10349. Ch* target_;
  10350. copy_code_units_utf(Ch* target) : target_(target) {
  10351. }
  10352. void operator()(const unicode::encoded_result<ErCh> er) {
  10353. std::memcpy(target_, er.code_units.data(), er.code_units_size * sizeof(ErCh));
  10354. target_ += er.code_units_size;
  10355. }
  10356. };
  10357. template <typename Ch, typename F>
  10358. inline void convert(const char* strb, const char* stre, F&& f) {
  10359. char32_t cp = 0;
  10360. for (const char* strtarget = strb; strtarget < stre;) {
  10361. auto dr = unicode::utf8_to_code_point(strtarget, stre);
  10362. if (dr.error != unicode::error_code::ok) {
  10363. cp = unicode::unicode_detail::replacement;
  10364. ++strtarget;
  10365. }
  10366. else {
  10367. cp = dr.codepoint;
  10368. strtarget = dr.next;
  10369. }
  10370. if constexpr (std::is_same_v<Ch, char32_t>) {
  10371. auto er = unicode::code_point_to_utf32(cp);
  10372. f(er);
  10373. }
  10374. else {
  10375. auto er = unicode::code_point_to_utf16(cp);
  10376. f(er);
  10377. }
  10378. }
  10379. }
  10380. template <typename BaseCh, typename S>
  10381. inline S get_into(lua_State* L, int index, record& tracking) {
  10382. using Ch = typename S::value_type;
  10383. tracking.use(1);
  10384. size_t len;
  10385. auto utf8p = lua_tolstring(L, index, &len);
  10386. if (len < 1)
  10387. return S();
  10388. const char* strb = utf8p;
  10389. const char* stre = utf8p + len;
  10390. stack_detail::count_code_units_utf<BaseCh> count_units;
  10391. convert<BaseCh>(strb, stre, count_units);
  10392. S r(count_units.needed_size, static_cast<Ch>(0));
  10393. r.resize(count_units.needed_size);
  10394. Ch* target = &r[0];
  10395. stack_detail::copy_code_units_utf<Ch, BaseCh> copy_units(target);
  10396. convert<BaseCh>(strb, stre, copy_units);
  10397. return r;
  10398. }
  10399. } // namespace stack_detail
  10400. template <typename T, typename>
  10401. struct unqualified_getter {
  10402. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  10403. if constexpr (std::is_same_v<T, bool>) {
  10404. tracking.use(1);
  10405. return lua_toboolean(L, index) != 0;
  10406. }
  10407. else if constexpr (std::is_enum_v<T>) {
  10408. tracking.use(1);
  10409. return static_cast<T>(lua_tointegerx(L, index, nullptr));
  10410. }
  10411. else if constexpr (std::is_integral_v<T> || std::is_same_v<T, lua_Integer>) {
  10412. tracking.use(1);
  10413. #if SOL_LUA_VESION_I_ >= 503
  10414. if (lua_isinteger(L, index) != 0) {
  10415. return static_cast<T>(lua_tointeger(L, index));
  10416. }
  10417. #endif
  10418. return static_cast<T>(llround(lua_tonumber(L, index)));
  10419. }
  10420. else if constexpr (std::is_floating_point_v<T> || std::is_same_v<T, lua_Number>) {
  10421. tracking.use(1);
  10422. return static_cast<T>(lua_tonumber(L, index));
  10423. }
  10424. else if constexpr (is_lua_reference_v<T>) {
  10425. tracking.use(1);
  10426. return T(L, index);
  10427. }
  10428. else if constexpr (is_unique_usertype_v<T>) {
  10429. using Real = typename unique_usertype_traits<T>::actual_type;
  10430. tracking.use(1);
  10431. void* memory = lua_touserdata(L, index);
  10432. memory = detail::align_usertype_unique<Real>(memory);
  10433. Real* mem = static_cast<Real*>(memory);
  10434. return *mem;
  10435. }
  10436. else if constexpr (meta::is_optional_v<T>) {
  10437. using ValueType = typename T::value_type;
  10438. return unqualified_check_getter<ValueType>::template get_using<T>(L, index, no_panic, tracking);
  10439. }
  10440. else if constexpr (std::is_same_v<T, luaL_Stream*>) {
  10441. luaL_Stream* pstream = static_cast<luaL_Stream*>(lua_touserdata(L, index));
  10442. return pstream;
  10443. }
  10444. else if constexpr (std::is_same_v<T, luaL_Stream>) {
  10445. luaL_Stream* pstream = static_cast<luaL_Stream*>(lua_touserdata(L, index));
  10446. return *pstream;
  10447. }
  10448. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  10449. else if constexpr (std::is_function_v<T> || (std::is_pointer_v<T> && std::is_function_v<std::remove_pointer_t<T>>)) {
  10450. return stack_detail::get_function_pointer<std::remove_pointer_t<T>>(L, index, tracking);
  10451. }
  10452. #endif
  10453. else {
  10454. return stack_detail::unchecked_unqualified_get<detail::as_value_tag<T>>(L, index, tracking);
  10455. }
  10456. }
  10457. };
  10458. template <typename X, typename>
  10459. struct qualified_getter {
  10460. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  10461. using Tu = meta::unqualified_t<X>;
  10462. static constexpr bool is_userdata_of_some_kind
  10463. = !std::is_reference_v<
  10464. X> && is_container_v<Tu> && std::is_default_constructible_v<Tu> && !is_lua_primitive_v<Tu> && !is_transparent_argument_v<Tu>;
  10465. if constexpr (is_userdata_of_some_kind) {
  10466. if (type_of(L, index) == type::userdata) {
  10467. return static_cast<Tu>(stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking));
  10468. }
  10469. else {
  10470. return stack_detail::unchecked_unqualified_get<sol::nested<Tu>>(L, index, tracking);
  10471. }
  10472. }
  10473. else if constexpr (!std::is_reference_v<X> && is_unique_usertype_v<Tu> && !is_base_rebindable_non_void_v<unique_usertype_traits<Tu>>) {
  10474. using u_traits = unique_usertype_traits<Tu>;
  10475. using T = typename u_traits::type;
  10476. using Real = typename u_traits::actual_type;
  10477. tracking.use(1);
  10478. void* memory = lua_touserdata(L, index);
  10479. memory = detail::align_usertype_unique_destructor(memory);
  10480. detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
  10481. if (&detail::usertype_unique_alloc_destroy<T, X> == pdx) {
  10482. memory = detail::align_usertype_unique_tag<true, false>(memory);
  10483. memory = detail::align_usertype_unique<Real, true, false>(memory);
  10484. Real* mem = static_cast<Real*>(memory);
  10485. return static_cast<Real>(*mem);
  10486. }
  10487. Real r(nullptr);
  10488. if constexpr (!derive<T>::value) {
  10489. // TODO: abort / terminate, maybe only in debug modes?
  10490. return static_cast<Real>(std::move(r));
  10491. }
  10492. else {
  10493. memory = detail::align_usertype_unique_tag<true, false>(memory);
  10494. detail::unique_tag& ic = *reinterpret_cast<detail::unique_tag*>(memory);
  10495. memory = detail::align_usertype_unique<Real, true, false>(memory);
  10496. string_view ti = usertype_traits<T>::qualified_name();
  10497. int cast_operation;
  10498. if constexpr (is_base_rebindable_v<u_traits>) {
  10499. using rebind_t = typename u_traits::template rebind_base<void>;
  10500. string_view rebind_ti = usertype_traits<rebind_t>::qualified_name();
  10501. cast_operation = ic(memory, &r, ti, rebind_ti);
  10502. }
  10503. else {
  10504. string_view rebind_ti("");
  10505. cast_operation = ic(memory, &r, ti, rebind_ti);
  10506. }
  10507. switch (cast_operation) {
  10508. case 1: {
  10509. // it's a perfect match,
  10510. // alias memory directly
  10511. Real* mem = static_cast<Real*>(memory);
  10512. return static_cast<Real>(*mem);
  10513. }
  10514. case 2:
  10515. // it's a base match, return the
  10516. // aliased creation
  10517. return static_cast<Real>(std::move(r));
  10518. default:
  10519. // uh oh..
  10520. break;
  10521. }
  10522. // TODO: abort / terminate, maybe only in debug modes?
  10523. return static_cast<Real>(r);
  10524. }
  10525. }
  10526. else {
  10527. return stack_detail::unchecked_unqualified_get<Tu>(L, index, tracking);
  10528. }
  10529. }
  10530. };
  10531. template <typename T>
  10532. struct unqualified_getter<as_table_t<T>> {
  10533. using Tu = meta::unqualified_t<T>;
  10534. template <typename V>
  10535. static void push_back_at_end(std::true_type, types<V>, lua_State* L, T& cont, std::size_t) {
  10536. cont.push_back(stack::get<V>(L, -lua_size<V>::value));
  10537. }
  10538. template <typename V>
  10539. static void push_back_at_end(std::false_type, types<V> t, lua_State* L, T& cont, std::size_t idx) {
  10540. insert_at_end(meta::has_insert<Tu>(), t, L, cont, idx);
  10541. }
  10542. template <typename V>
  10543. static void insert_at_end(std::true_type, types<V>, lua_State* L, T& cont, std::size_t) {
  10544. using std::cend;
  10545. cont.insert(cend(cont), stack::get<V>(L, -lua_size<V>::value));
  10546. }
  10547. template <typename V>
  10548. static void insert_at_end(std::false_type, types<V>, lua_State* L, T& cont, std::size_t idx) {
  10549. cont[idx] = stack::get<V>(L, -lua_size<V>::value);
  10550. }
  10551. static bool max_size_check(std::false_type, T&, std::size_t) {
  10552. return false;
  10553. }
  10554. static bool max_size_check(std::true_type, T& cont, std::size_t idx) {
  10555. return idx >= cont.max_size();
  10556. }
  10557. static T get(lua_State* L, int relindex, record& tracking) {
  10558. return get(meta::is_associative<Tu>(), L, relindex, tracking);
  10559. }
  10560. static T get(std::false_type, lua_State* L, int relindex, record& tracking) {
  10561. typedef typename Tu::value_type V;
  10562. return get(types<V>(), L, relindex, tracking);
  10563. }
  10564. template <typename V>
  10565. static T get(types<V> t, lua_State* L, int relindex, record& tracking) {
  10566. tracking.use(1);
  10567. // the W4 flag is really great,
  10568. // so great that it can tell my for loops (twice nested)
  10569. // below never actually terminate
  10570. // without hitting where the gotos have infested
  10571. // so now I would get the error W4XXX unreachable
  10572. // me that the return cont at the end of this function
  10573. // which is fair until other compilers complain
  10574. // that there isn't a return and that based on
  10575. // SOME MAGICAL FORCE
  10576. // control flow falls off the end of a non-void function
  10577. // so it needs to be there for the compilers that are
  10578. // too flimsy to analyze the basic blocks...
  10579. // (I'm sure I should file a bug but those compilers are already
  10580. // in the wild; it doesn't matter if I fix them,
  10581. // someone else is still going to get some old-ass compiler
  10582. // and then bother me about the unclean build for the 30th
  10583. // time)
  10584. // "Why not an IIFE?"
  10585. // Because additional lambdas / functions which serve as
  10586. // capture-all-and-then-invoke bloat binary sizes
  10587. // by an actually detectable amount
  10588. // (one user uses sol2 pretty heavily and 22 MB of binary size
  10589. // was saved by reducing reliance on lambdas in templates)
  10590. // This would really be solved by having break N;
  10591. // be a real, proper thing...
  10592. // but instead, we have to use labels and gotos
  10593. // and earn the universal vitriol of the dogmatic
  10594. // programming community
  10595. // all in all: W4 is great!~
  10596. int index = lua_absindex(L, relindex);
  10597. T cont;
  10598. std::size_t idx = 0;
  10599. #if SOL_LUA_VESION_I_ >= 503
  10600. // This method is HIGHLY performant over regular table iteration
  10601. // thanks to the Lua API changes in 5.3
  10602. // Questionable in 5.4
  10603. for (lua_Integer i = 0;; i += lua_size<V>::value) {
  10604. if (max_size_check(meta::has_max_size<Tu>(), cont, idx)) {
  10605. // see above comment
  10606. goto done;
  10607. }
  10608. bool isnil = false;
  10609. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  10610. #if defined(LUA_NILINTABLE) && LUA_NILINTABLE && SOL_LUA_VESION_I_ >= 600
  10611. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  10612. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  10613. #endif // make sure stack doesn't overflow
  10614. lua_pushinteger(L, static_cast<lua_Integer>(i + vi));
  10615. if (lua_keyin(L, index) == 0) {
  10616. // it's time to stop
  10617. isnil = true;
  10618. }
  10619. else {
  10620. // we have a key, have to get the value
  10621. lua_geti(L, index, i + vi);
  10622. }
  10623. #else
  10624. type vt = static_cast<type>(lua_geti(L, index, i + vi));
  10625. isnil = vt == type::none || vt == type::lua_nil;
  10626. #endif
  10627. if (isnil) {
  10628. if (i == 0) {
  10629. break;
  10630. }
  10631. #if defined(LUA_NILINTABLE) && LUA_NILINTABLE && SOL_LUA_VESION_I_ >= 600
  10632. lua_pop(L, vi);
  10633. #else
  10634. lua_pop(L, (vi + 1));
  10635. #endif
  10636. // see above comment
  10637. goto done;
  10638. }
  10639. }
  10640. if (isnil) {
  10641. #if defined(LUA_NILINTABLE) && LUA_NILINTABLE && SOL_LUA_VESION_I_ >= 600
  10642. #else
  10643. lua_pop(L, lua_size<V>::value);
  10644. #endif
  10645. continue;
  10646. }
  10647. push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
  10648. ++idx;
  10649. lua_pop(L, lua_size<V>::value);
  10650. }
  10651. #else
  10652. // Zzzz slower but necessary thanks to the lower version API and missing functions qq
  10653. for (lua_Integer i = 0;; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
  10654. if (idx >= cont.max_size()) {
  10655. // see above comment
  10656. goto done;
  10657. }
  10658. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  10659. luaL_checkstack(L, 2, detail::not_enough_stack_space_generic);
  10660. #endif // make sure stack doesn't overflow
  10661. bool isnil = false;
  10662. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  10663. lua_pushinteger(L, i);
  10664. lua_gettable(L, index);
  10665. type vt = type_of(L, -1);
  10666. isnil = vt == type::lua_nil;
  10667. if (isnil) {
  10668. if (i == 0) {
  10669. break;
  10670. }
  10671. lua_pop(L, (vi + 1));
  10672. // see above comment
  10673. goto done;
  10674. }
  10675. }
  10676. if (isnil)
  10677. continue;
  10678. push_back_at_end(meta::has_push_back<Tu>(), t, L, cont, idx);
  10679. ++idx;
  10680. }
  10681. #endif
  10682. done:
  10683. return cont;
  10684. }
  10685. static T get(std::true_type, lua_State* L, int index, record& tracking) {
  10686. typedef typename Tu::value_type P;
  10687. typedef typename P::first_type K;
  10688. typedef typename P::second_type V;
  10689. return get(types<K, V>(), L, index, tracking);
  10690. }
  10691. template <typename K, typename V>
  10692. static T get(types<K, V>, lua_State* L, int relindex, record& tracking) {
  10693. tracking.use(1);
  10694. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  10695. luaL_checkstack(L, 3, detail::not_enough_stack_space_generic);
  10696. #endif // make sure stack doesn't overflow
  10697. T associative;
  10698. int index = lua_absindex(L, relindex);
  10699. lua_pushnil(L);
  10700. while (lua_next(L, index) != 0) {
  10701. decltype(auto) key = stack::check_get<K>(L, -2);
  10702. if (!key) {
  10703. lua_pop(L, 1);
  10704. continue;
  10705. }
  10706. associative.emplace(std::forward<decltype(*key)>(*key), stack::get<V>(L, -1));
  10707. lua_pop(L, 1);
  10708. }
  10709. return associative;
  10710. }
  10711. };
  10712. template <typename T, typename Al>
  10713. struct unqualified_getter<as_table_t<std::forward_list<T, Al>>> {
  10714. typedef std::forward_list<T, Al> C;
  10715. static C get(lua_State* L, int relindex, record& tracking) {
  10716. return get(meta::has_key_value_pair<C>(), L, relindex, tracking);
  10717. }
  10718. static C get(std::true_type, lua_State* L, int index, record& tracking) {
  10719. typedef typename T::value_type P;
  10720. typedef typename P::first_type K;
  10721. typedef typename P::second_type V;
  10722. return get(types<K, V>(), L, index, tracking);
  10723. }
  10724. static C get(std::false_type, lua_State* L, int relindex, record& tracking) {
  10725. typedef typename C::value_type V;
  10726. return get(types<V>(), L, relindex, tracking);
  10727. }
  10728. template <typename V>
  10729. static C get(types<V>, lua_State* L, int relindex, record& tracking) {
  10730. tracking.use(1);
  10731. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  10732. luaL_checkstack(L, 3, detail::not_enough_stack_space_generic);
  10733. #endif // make sure stack doesn't overflow
  10734. int index = lua_absindex(L, relindex);
  10735. C cont;
  10736. auto at = cont.cbefore_begin();
  10737. std::size_t idx = 0;
  10738. #if SOL_LUA_VESION_I_ >= 503
  10739. // This method is HIGHLY performant over regular table iteration thanks to the Lua API changes in 5.3
  10740. for (lua_Integer i = 0;; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
  10741. if (idx >= cont.max_size()) {
  10742. goto done;
  10743. }
  10744. bool isnil = false;
  10745. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  10746. type t = static_cast<type>(lua_geti(L, index, i + vi));
  10747. isnil = t == type::lua_nil;
  10748. if (isnil) {
  10749. if (i == 0) {
  10750. break;
  10751. }
  10752. lua_pop(L, (vi + 1));
  10753. goto done;
  10754. }
  10755. }
  10756. if (isnil)
  10757. continue;
  10758. at = cont.insert_after(at, stack::get<V>(L, -lua_size<V>::value));
  10759. ++idx;
  10760. }
  10761. #else
  10762. // Zzzz slower but necessary thanks to the lower version API and missing functions qq
  10763. for (lua_Integer i = 0;; i += lua_size<V>::value, lua_pop(L, lua_size<V>::value)) {
  10764. if (idx >= cont.max_size()) {
  10765. goto done;
  10766. }
  10767. bool isnil = false;
  10768. for (int vi = 0; vi < lua_size<V>::value; ++vi) {
  10769. lua_pushinteger(L, i);
  10770. lua_gettable(L, index);
  10771. type t = type_of(L, -1);
  10772. isnil = t == type::lua_nil;
  10773. if (isnil) {
  10774. if (i == 0) {
  10775. break;
  10776. }
  10777. lua_pop(L, (vi + 1));
  10778. goto done;
  10779. }
  10780. }
  10781. if (isnil)
  10782. continue;
  10783. at = cont.insert_after(at, stack::get<V>(L, -lua_size<V>::value));
  10784. ++idx;
  10785. }
  10786. #endif
  10787. done:
  10788. return cont;
  10789. }
  10790. template <typename K, typename V>
  10791. static C get(types<K, V>, lua_State* L, int relindex, record& tracking) {
  10792. tracking.use(1);
  10793. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  10794. luaL_checkstack(L, 3, detail::not_enough_stack_space_generic);
  10795. #endif // make sure stack doesn't overflow
  10796. C associative;
  10797. auto at = associative.cbefore_begin();
  10798. int index = lua_absindex(L, relindex);
  10799. lua_pushnil(L);
  10800. while (lua_next(L, index) != 0) {
  10801. decltype(auto) key = stack::check_get<K>(L, -2);
  10802. if (!key) {
  10803. lua_pop(L, 1);
  10804. continue;
  10805. }
  10806. at = associative.emplace_after(at, std::forward<decltype(*key)>(*key), stack::get<V>(L, -1));
  10807. lua_pop(L, 1);
  10808. }
  10809. return associative;
  10810. }
  10811. };
  10812. template <typename T>
  10813. struct unqualified_getter<nested<T>> {
  10814. static T get(lua_State* L, int index, record& tracking) {
  10815. using Tu = meta::unqualified_t<T>;
  10816. if constexpr (is_container_v<Tu>) {
  10817. if constexpr (meta::is_associative<Tu>::value) {
  10818. typedef typename T::value_type P;
  10819. typedef typename P::first_type K;
  10820. typedef typename P::second_type V;
  10821. unqualified_getter<as_table_t<T>> g;
  10822. // VC++ has a bad warning here: shut it up
  10823. (void)g;
  10824. return g.get(types<K, nested<V>>(), L, index, tracking);
  10825. }
  10826. else {
  10827. typedef typename T::value_type V;
  10828. unqualified_getter<as_table_t<T>> g;
  10829. // VC++ has a bad warning here: shut it up
  10830. (void)g;
  10831. return g.get(types<nested<V>>(), L, index, tracking);
  10832. }
  10833. }
  10834. else {
  10835. unqualified_getter<Tu> g;
  10836. // VC++ has a bad warning here: shut it up
  10837. (void)g;
  10838. return g.get(L, index, tracking);
  10839. }
  10840. }
  10841. };
  10842. template <typename T>
  10843. struct unqualified_getter<as_container_t<T>> {
  10844. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  10845. return stack::unqualified_get<T>(L, index, tracking);
  10846. }
  10847. };
  10848. template <typename T>
  10849. struct unqualified_getter<as_container_t<T>*> {
  10850. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  10851. return stack::unqualified_get<T*>(L, index, tracking);
  10852. }
  10853. };
  10854. template <>
  10855. struct unqualified_getter<userdata_value> {
  10856. static userdata_value get(lua_State* L, int index, record& tracking) {
  10857. tracking.use(1);
  10858. return userdata_value(lua_touserdata(L, index));
  10859. }
  10860. };
  10861. template <>
  10862. struct unqualified_getter<lightuserdata_value> {
  10863. static lightuserdata_value get(lua_State* L, int index, record& tracking) {
  10864. tracking.use(1);
  10865. return lightuserdata_value(lua_touserdata(L, index));
  10866. }
  10867. };
  10868. template <typename T>
  10869. struct unqualified_getter<light<T>> {
  10870. static light<T> get(lua_State* L, int index, record& tracking) {
  10871. tracking.use(1);
  10872. void* memory = lua_touserdata(L, index);
  10873. return light<T>(static_cast<T*>(memory));
  10874. }
  10875. };
  10876. template <typename T>
  10877. struct unqualified_getter<user<T>> {
  10878. static std::add_lvalue_reference_t<T> get(lua_State* L, int index, record& tracking) {
  10879. tracking.use(1);
  10880. void* memory = lua_touserdata(L, index);
  10881. memory = detail::align_user<T>(memory);
  10882. return *static_cast<std::remove_reference_t<T>*>(memory);
  10883. }
  10884. };
  10885. template <typename T>
  10886. struct unqualified_getter<user<T*>> {
  10887. static T* get(lua_State* L, int index, record& tracking) {
  10888. tracking.use(1);
  10889. void* memory = lua_touserdata(L, index);
  10890. memory = detail::align_user<T*>(memory);
  10891. return static_cast<T*>(memory);
  10892. }
  10893. };
  10894. template <>
  10895. struct unqualified_getter<type> {
  10896. static type get(lua_State* L, int index, record& tracking) {
  10897. tracking.use(1);
  10898. return static_cast<type>(lua_type(L, index));
  10899. }
  10900. };
  10901. template <>
  10902. struct unqualified_getter<std::string> {
  10903. static std::string get(lua_State* L, int index, record& tracking) {
  10904. tracking.use(1);
  10905. std::size_t len;
  10906. auto str = lua_tolstring(L, index, &len);
  10907. return std::string(str, len);
  10908. }
  10909. };
  10910. template <>
  10911. struct unqualified_getter<const char*> {
  10912. static const char* get(lua_State* L, int index, record& tracking) {
  10913. tracking.use(1);
  10914. size_t sz;
  10915. return lua_tolstring(L, index, &sz);
  10916. }
  10917. };
  10918. template <>
  10919. struct unqualified_getter<char> {
  10920. static char get(lua_State* L, int index, record& tracking) {
  10921. tracking.use(1);
  10922. size_t len;
  10923. auto str = lua_tolstring(L, index, &len);
  10924. return len > 0 ? str[0] : '\0';
  10925. }
  10926. };
  10927. template <typename Traits>
  10928. struct unqualified_getter<basic_string_view<char, Traits>> {
  10929. static string_view get(lua_State* L, int index, record& tracking) {
  10930. tracking.use(1);
  10931. size_t sz;
  10932. const char* str = lua_tolstring(L, index, &sz);
  10933. return basic_string_view<char, Traits>(str, sz);
  10934. }
  10935. };
  10936. template <typename Traits, typename Al>
  10937. struct unqualified_getter<std::basic_string<wchar_t, Traits, Al>> {
  10938. using S = std::basic_string<wchar_t, Traits, Al>;
  10939. static S get(lua_State* L, int index, record& tracking) {
  10940. using Ch = meta::conditional_t<sizeof(wchar_t) == 2, char16_t, char32_t>;
  10941. return stack_detail::get_into<Ch, S>(L, index, tracking);
  10942. }
  10943. };
  10944. template <typename Traits, typename Al>
  10945. struct unqualified_getter<std::basic_string<char16_t, Traits, Al>> {
  10946. static std::basic_string<char16_t, Traits, Al> get(lua_State* L, int index, record& tracking) {
  10947. return stack_detail::get_into<char16_t, std::basic_string<char16_t, Traits, Al>>(L, index, tracking);
  10948. }
  10949. };
  10950. template <typename Traits, typename Al>
  10951. struct unqualified_getter<std::basic_string<char32_t, Traits, Al>> {
  10952. static std::basic_string<char32_t, Traits, Al> get(lua_State* L, int index, record& tracking) {
  10953. return stack_detail::get_into<char32_t, std::basic_string<char32_t, Traits, Al>>(L, index, tracking);
  10954. }
  10955. };
  10956. template <>
  10957. struct unqualified_getter<char16_t> {
  10958. static char16_t get(lua_State* L, int index, record& tracking) {
  10959. string_view utf8 = stack::get<string_view>(L, index, tracking);
  10960. const char* strb = utf8.data();
  10961. const char* stre = utf8.data() + utf8.size();
  10962. char32_t cp = 0;
  10963. auto dr = unicode::utf8_to_code_point(strb, stre);
  10964. if (dr.error != unicode::error_code::ok) {
  10965. cp = unicode::unicode_detail::replacement;
  10966. }
  10967. else {
  10968. cp = dr.codepoint;
  10969. }
  10970. auto er = unicode::code_point_to_utf16(cp);
  10971. return er.code_units[0];
  10972. }
  10973. };
  10974. template <>
  10975. struct unqualified_getter<char32_t> {
  10976. static char32_t get(lua_State* L, int index, record& tracking) {
  10977. string_view utf8 = stack::get<string_view>(L, index, tracking);
  10978. const char* strb = utf8.data();
  10979. const char* stre = utf8.data() + utf8.size();
  10980. char32_t cp = 0;
  10981. auto dr = unicode::utf8_to_code_point(strb, stre);
  10982. if (dr.error != unicode::error_code::ok) {
  10983. cp = unicode::unicode_detail::replacement;
  10984. }
  10985. else {
  10986. cp = dr.codepoint;
  10987. }
  10988. auto er = unicode::code_point_to_utf32(cp);
  10989. return er.code_units[0];
  10990. }
  10991. };
  10992. template <>
  10993. struct unqualified_getter<wchar_t> {
  10994. static wchar_t get(lua_State* L, int index, record& tracking) {
  10995. typedef meta::conditional_t<sizeof(wchar_t) == 2, char16_t, char32_t> Ch;
  10996. unqualified_getter<Ch> g;
  10997. (void)g;
  10998. auto c = g.get(L, index, tracking);
  10999. return static_cast<wchar_t>(c);
  11000. }
  11001. };
  11002. template <>
  11003. struct unqualified_getter<meta_function> {
  11004. static meta_function get(lua_State* L, int index, record& tracking) {
  11005. tracking.use(1);
  11006. const char* name = unqualified_getter<const char*> {}.get(L, index, tracking);
  11007. const auto& mfnames = meta_function_names();
  11008. for (std::size_t i = 0; i < mfnames.size(); ++i)
  11009. if (mfnames[i] == name)
  11010. return static_cast<meta_function>(i);
  11011. return meta_function::construct;
  11012. }
  11013. };
  11014. template <>
  11015. struct unqualified_getter<lua_nil_t> {
  11016. static lua_nil_t get(lua_State*, int, record& tracking) {
  11017. tracking.use(1);
  11018. return lua_nil;
  11019. }
  11020. };
  11021. template <>
  11022. struct unqualified_getter<std::nullptr_t> {
  11023. static std::nullptr_t get(lua_State*, int, record& tracking) {
  11024. tracking.use(1);
  11025. return nullptr;
  11026. }
  11027. };
  11028. template <>
  11029. struct unqualified_getter<nullopt_t> {
  11030. static nullopt_t get(lua_State*, int, record& tracking) {
  11031. tracking.use(1);
  11032. return nullopt;
  11033. }
  11034. };
  11035. template <>
  11036. struct unqualified_getter<this_state> {
  11037. static this_state get(lua_State* L, int, record& tracking) {
  11038. tracking.use(0);
  11039. return this_state(L);
  11040. }
  11041. };
  11042. template <>
  11043. struct unqualified_getter<this_main_state> {
  11044. static this_main_state get(lua_State* L, int, record& tracking) {
  11045. tracking.use(0);
  11046. return this_main_state(main_thread(L, L));
  11047. }
  11048. };
  11049. template <>
  11050. struct unqualified_getter<lua_CFunction> {
  11051. static lua_CFunction get(lua_State* L, int index, record& tracking) {
  11052. tracking.use(1);
  11053. return lua_tocfunction(L, index);
  11054. }
  11055. };
  11056. template <>
  11057. struct unqualified_getter<c_closure> {
  11058. static c_closure get(lua_State* L, int index, record& tracking) {
  11059. tracking.use(1);
  11060. return c_closure(lua_tocfunction(L, index), -1);
  11061. }
  11062. };
  11063. template <>
  11064. struct unqualified_getter<error> {
  11065. static error get(lua_State* L, int index, record& tracking) {
  11066. tracking.use(1);
  11067. size_t sz = 0;
  11068. const char* err = lua_tolstring(L, index, &sz);
  11069. if (err == nullptr) {
  11070. return error(detail::direct_error, "");
  11071. }
  11072. return error(detail::direct_error, std::string(err, sz));
  11073. }
  11074. };
  11075. template <>
  11076. struct unqualified_getter<void*> {
  11077. static void* get(lua_State* L, int index, record& tracking) {
  11078. tracking.use(1);
  11079. return lua_touserdata(L, index);
  11080. }
  11081. };
  11082. template <>
  11083. struct unqualified_getter<const void*> {
  11084. static const void* get(lua_State* L, int index, record& tracking) {
  11085. tracking.use(1);
  11086. return lua_touserdata(L, index);
  11087. }
  11088. };
  11089. template <typename T>
  11090. struct unqualified_getter<detail::as_value_tag<T>> {
  11091. static T* get_no_lua_nil(lua_State* L, int index, record& tracking) {
  11092. void* memory = lua_touserdata(L, index);
  11093. #if SOL_IS_ON(SOL_USE_INTEROP_I_)
  11094. auto ugr = stack_detail::interop_get<T>(L, index, memory, tracking);
  11095. if (ugr.first) {
  11096. return ugr.second;
  11097. }
  11098. #endif // interop extensibility
  11099. tracking.use(1);
  11100. void* rawdata = detail::align_usertype_pointer(memory);
  11101. void** pudata = static_cast<void**>(rawdata);
  11102. void* udata = *pudata;
  11103. return get_no_lua_nil_from(L, udata, index, tracking);
  11104. }
  11105. static T* get_no_lua_nil_from(lua_State* L, void* udata, int index, record&) {
  11106. bool has_derived = derive<T>::value || weak_derive<T>::value;
  11107. if (has_derived) {
  11108. if (lua_getmetatable(L, index) == 1) {
  11109. lua_getfield(L, -1, &detail::base_class_cast_key()[0]);
  11110. if (type_of(L, -1) != type::lua_nil) {
  11111. void* basecastdata = lua_touserdata(L, -1);
  11112. detail::inheritance_cast_function ic = reinterpret_cast<detail::inheritance_cast_function>(basecastdata);
  11113. // use the casting function to properly adjust the pointer for the desired T
  11114. udata = ic(udata, usertype_traits<T>::qualified_name());
  11115. }
  11116. lua_pop(L, 2);
  11117. }
  11118. }
  11119. T* obj = static_cast<T*>(udata);
  11120. return obj;
  11121. }
  11122. static T& get(lua_State* L, int index, record& tracking) {
  11123. return *get_no_lua_nil(L, index, tracking);
  11124. }
  11125. };
  11126. template <typename T>
  11127. struct unqualified_getter<detail::as_pointer_tag<T>> {
  11128. static T* get(lua_State* L, int index, record& tracking) {
  11129. type t = type_of(L, index);
  11130. if (t == type::lua_nil) {
  11131. tracking.use(1);
  11132. return nullptr;
  11133. }
  11134. unqualified_getter<detail::as_value_tag<T>> g;
  11135. // Avoid VC++ warning
  11136. (void)g;
  11137. return g.get_no_lua_nil(L, index, tracking);
  11138. }
  11139. };
  11140. template <typename T>
  11141. struct unqualified_getter<non_null<T*>> {
  11142. static T* get(lua_State* L, int index, record& tracking) {
  11143. unqualified_getter<detail::as_value_tag<T>> g;
  11144. // Avoid VC++ warning
  11145. (void)g;
  11146. return g.get_no_lua_nil(L, index, tracking);
  11147. }
  11148. };
  11149. template <typename T>
  11150. struct unqualified_getter<T&> {
  11151. static T& get(lua_State* L, int index, record& tracking) {
  11152. unqualified_getter<detail::as_value_tag<T>> g;
  11153. // Avoid VC++ warning
  11154. (void)g;
  11155. return g.get(L, index, tracking);
  11156. }
  11157. };
  11158. template <typename T>
  11159. struct unqualified_getter<std::reference_wrapper<T>> {
  11160. static T& get(lua_State* L, int index, record& tracking) {
  11161. unqualified_getter<T&> g;
  11162. // Avoid VC++ warning
  11163. (void)g;
  11164. return g.get(L, index, tracking);
  11165. }
  11166. };
  11167. template <typename T>
  11168. struct unqualified_getter<T*> {
  11169. static T* get(lua_State* L, int index, record& tracking) {
  11170. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  11171. if constexpr (std::is_function_v<T>) {
  11172. return stack_detail::get_function_pointer<T>(L, index, tracking);
  11173. }
  11174. else {
  11175. unqualified_getter<detail::as_pointer_tag<T>> g;
  11176. // Avoid VC++ warning
  11177. (void)g;
  11178. return g.get(L, index, tracking);
  11179. }
  11180. #else
  11181. unqualified_getter<detail::as_pointer_tag<T>> g;
  11182. // Avoid VC++ warning
  11183. (void)g;
  11184. return g.get(L, index, tracking);
  11185. #endif
  11186. }
  11187. };
  11188. template <typename... Tn>
  11189. struct unqualified_getter<std::tuple<Tn...>> {
  11190. typedef std::tuple<decltype(stack::get<Tn>(nullptr, 0))...> R;
  11191. template <typename... Args>
  11192. static R apply(std::index_sequence<>, lua_State*, int, record&, Args&&... args) {
  11193. // Fuck you too, VC++
  11194. return R { std::forward<Args>(args)... };
  11195. }
  11196. template <std::size_t I, std::size_t... Ix, typename... Args>
  11197. static R apply(std::index_sequence<I, Ix...>, lua_State* L, int index, record& tracking, Args&&... args) {
  11198. // Fuck you too, VC++
  11199. typedef std::tuple_element_t<I, std::tuple<Tn...>> T;
  11200. return apply(std::index_sequence<Ix...>(), L, index, tracking, std::forward<Args>(args)..., stack::get<T>(L, index + tracking.used, tracking));
  11201. }
  11202. static R get(lua_State* L, int index, record& tracking) {
  11203. return apply(std::make_index_sequence<sizeof...(Tn)>(), L, index, tracking);
  11204. }
  11205. };
  11206. template <typename A, typename B>
  11207. struct unqualified_getter<std::pair<A, B>> {
  11208. static decltype(auto) get(lua_State* L, int index, record& tracking) {
  11209. return std::pair<decltype(stack::get<A>(L, index)), decltype(stack::get<B>(L, index))> { stack::get<A>(L, index, tracking),
  11210. stack::get<B>(L, index + tracking.used, tracking) };
  11211. }
  11212. };
  11213. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  11214. template <typename... Tn>
  11215. struct unqualified_getter<std::variant<Tn...>> {
  11216. using V = std::variant<Tn...>;
  11217. static V get_one(std::integral_constant<std::size_t, std::variant_size_v<V>>, lua_State* L, int index, record& tracking) {
  11218. (void)L;
  11219. (void)index;
  11220. (void)tracking;
  11221. if constexpr (std::variant_size_v<V> == 0) {
  11222. return V();
  11223. }
  11224. else {
  11225. // using T = std::variant_alternative_t<0, V>;
  11226. std::abort();
  11227. // return V(std::in_place_index<0>, stack::get<T>(L, index, tracking));
  11228. }
  11229. }
  11230. template <std::size_t I>
  11231. static V get_one(std::integral_constant<std::size_t, I>, lua_State* L, int index, record& tracking) {
  11232. typedef std::variant_alternative_t<I, V> T;
  11233. record temp_tracking = tracking;
  11234. if (stack::check<T>(L, index, no_panic, temp_tracking)) {
  11235. tracking = temp_tracking;
  11236. return V(std::in_place_index<I>, stack::get<T>(L, index));
  11237. }
  11238. return get_one(std::integral_constant<std::size_t, I + 1>(), L, index, tracking);
  11239. }
  11240. static V get(lua_State* L, int index, record& tracking) {
  11241. return get_one(std::integral_constant<std::size_t, 0>(), L, index, tracking);
  11242. }
  11243. };
  11244. #endif // variant
  11245. }} // namespace sol::stack
  11246. // end of sol/stack_get_unqualified.hpp
  11247. // beginning of sol/stack_get_qualified.hpp
  11248. namespace sol {
  11249. namespace stack {
  11250. // There are no more enable_ifs that can be used here,
  11251. // so this is just for posterity, I guess?
  11252. // maybe I'll fill this file in later.
  11253. }
  11254. } // namespace sol::stack
  11255. // end of sol/stack_get_qualified.hpp
  11256. // end of sol/stack_get.hpp
  11257. // beginning of sol/stack_check_get.hpp
  11258. // beginning of sol/stack_check_get_unqualified.hpp
  11259. #include <cstdlib>
  11260. #include <cmath>
  11261. #include <optional>
  11262. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  11263. #include <variant>
  11264. #endif // variant shenanigans (thanks, Mac OSX)
  11265. namespace sol { namespace stack {
  11266. template <typename T, typename>
  11267. struct unqualified_check_getter {
  11268. typedef decltype(stack_detail::unchecked_unqualified_get<T>(nullptr, -1, std::declval<record&>())) R;
  11269. template <typename Optional, typename Handler>
  11270. static Optional get_using(lua_State* L, int index, Handler&& handler, record& tracking) {
  11271. if constexpr (!meta::meta_detail::is_adl_sol_lua_check_v<T> && !meta::meta_detail::is_adl_sol_lua_get_v<T>) {
  11272. if constexpr (is_lua_reference_v<T>) {
  11273. // actually check if it's none here, otherwise
  11274. // we'll have a none object inside an optional!
  11275. bool success = lua_isnoneornil(L, index) == 0 && stack::check<T>(L, index, no_panic);
  11276. if (!success) {
  11277. // expected type, actual type
  11278. tracking.use(static_cast<int>(success));
  11279. handler(L, index, type::poly, type_of(L, index), "");
  11280. return detail::associated_nullopt_v<Optional>;
  11281. }
  11282. return stack_detail::unchecked_get<T>(L, index, tracking);
  11283. }
  11284. else if constexpr ((std::is_integral_v<T> || std::is_same_v<T, lua_Integer>)&&!std::is_same_v<T, bool>) {
  11285. #if SOL_LUA_VESION_I_ >= 503
  11286. if (lua_isinteger(L, index) != 0) {
  11287. tracking.use(1);
  11288. return static_cast<T>(lua_tointeger(L, index));
  11289. }
  11290. #endif
  11291. int isnum = 0;
  11292. const lua_Number value = lua_tonumberx(L, index, &isnum);
  11293. if (isnum != 0) {
  11294. #if SOL_IS_ON(SOL_NUMBER_PRECISION_CHECKS_I_)
  11295. const auto integer_value = llround(value);
  11296. if (static_cast<lua_Number>(integer_value) == value) {
  11297. tracking.use(1);
  11298. return static_cast<T>(integer_value);
  11299. }
  11300. #else
  11301. tracking.use(1);
  11302. return static_cast<T>(value);
  11303. #endif
  11304. }
  11305. const type t = type_of(L, index);
  11306. tracking.use(static_cast<int>(t != type::none));
  11307. handler(L, index, type::number, t, "not an integer");
  11308. return detail::associated_nullopt_v<Optional>;
  11309. }
  11310. else if constexpr (std::is_floating_point_v<T> || std::is_same_v<T, lua_Number>) {
  11311. int isnum = 0;
  11312. lua_Number value = lua_tonumberx(L, index, &isnum);
  11313. if (isnum == 0) {
  11314. type t = type_of(L, index);
  11315. tracking.use(static_cast<int>(t != type::none));
  11316. handler(L, index, type::number, t, "not a valid floating point number");
  11317. return detail::associated_nullopt_v<Optional>;
  11318. }
  11319. tracking.use(1);
  11320. return static_cast<T>(value);
  11321. }
  11322. else if constexpr (std::is_enum_v<T> && !meta::any_same_v<T, meta_function, type>) {
  11323. int isnum = 0;
  11324. lua_Integer value = lua_tointegerx(L, index, &isnum);
  11325. if (isnum == 0) {
  11326. type t = type_of(L, index);
  11327. tracking.use(static_cast<int>(t != type::none));
  11328. handler(L, index, type::number, t, "not a valid enumeration value");
  11329. return detail::associated_nullopt_v<Optional>;
  11330. }
  11331. tracking.use(1);
  11332. return static_cast<T>(value);
  11333. }
  11334. else {
  11335. if (!unqualified_check<T>(L, index, std::forward<Handler>(handler))) {
  11336. tracking.use(static_cast<int>(!lua_isnone(L, index)));
  11337. return detail::associated_nullopt_v<Optional>;
  11338. }
  11339. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  11340. }
  11341. }
  11342. else {
  11343. if (!unqualified_check<T>(L, index, std::forward<Handler>(handler))) {
  11344. tracking.use(static_cast<int>(!lua_isnone(L, index)));
  11345. return detail::associated_nullopt_v<Optional>;
  11346. }
  11347. return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
  11348. }
  11349. }
  11350. template <typename Handler>
  11351. static optional<R> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  11352. return get_using<optional<R>>(L, index, std::forward<Handler>(handler), tracking);
  11353. }
  11354. };
  11355. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  11356. template <typename... Tn, typename C>
  11357. struct unqualified_check_getter<std::variant<Tn...>, C> {
  11358. typedef std::variant<Tn...> V;
  11359. typedef std::variant_size<V> V_size;
  11360. typedef std::integral_constant<bool, V_size::value == 0> V_is_empty;
  11361. template <typename Handler>
  11362. static optional<V> get_empty(std::true_type, lua_State*, int, Handler&&, record&) {
  11363. return nullopt;
  11364. }
  11365. template <typename Handler>
  11366. static optional<V> get_empty(std::false_type, lua_State* L, int index, Handler&& handler, record&) {
  11367. // This should never be reached...
  11368. // please check your code and understand what you did to bring yourself here
  11369. // maybe file a bug report, or 5
  11370. handler(
  11371. 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");
  11372. return nullopt;
  11373. }
  11374. template <typename Handler>
  11375. static optional<V> get_one(std::integral_constant<std::size_t, 0>, lua_State* L, int index, Handler&& handler, record& tracking) {
  11376. return get_empty(V_is_empty(), L, index, std::forward<Handler>(handler), tracking);
  11377. }
  11378. template <std::size_t I, typename Handler>
  11379. static optional<V> get_one(std::integral_constant<std::size_t, I>, lua_State* L, int index, Handler&& handler, record& tracking) {
  11380. typedef std::variant_alternative_t<I - 1, V> T;
  11381. if (stack::check<T>(L, index, no_panic, tracking)) {
  11382. return V(std::in_place_index<I - 1>, stack::get<T>(L, index));
  11383. }
  11384. return get_one(std::integral_constant<std::size_t, I - 1>(), L, index, std::forward<Handler>(handler), tracking);
  11385. }
  11386. template <typename Handler>
  11387. static optional<V> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  11388. return get_one(std::integral_constant<std::size_t, V_size::value>(), L, index, std::forward<Handler>(handler), tracking);
  11389. }
  11390. };
  11391. #endif // standard variant
  11392. }} // namespace sol::stack
  11393. // end of sol/stack_check_get_unqualified.hpp
  11394. // beginning of sol/stack_check_get_qualified.hpp
  11395. namespace sol { namespace stack {
  11396. template <typename T, typename C>
  11397. struct qualified_check_getter {
  11398. typedef decltype(stack_detail::unchecked_get<T>(nullptr, -1, std::declval<record&>())) R;
  11399. template <typename Handler>
  11400. static optional<R> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  11401. if constexpr (is_lua_reference_v<T>) {
  11402. // actually check if it's none here, otherwise
  11403. // we'll have a none object inside an optional!
  11404. bool success = lua_isnoneornil(L, index) == 0 && stack::check<T>(L, index, no_panic);
  11405. if (!success) {
  11406. // expected type, actual type
  11407. tracking.use(static_cast<int>(success));
  11408. handler(L, index, type::poly, type_of(L, index), "");
  11409. return nullopt;
  11410. }
  11411. return stack_detail::unchecked_get<T>(L, index, tracking);
  11412. }
  11413. else {
  11414. if (!check<T>(L, index, std::forward<Handler>(handler))) {
  11415. tracking.use(static_cast<int>(!lua_isnone(L, index)));
  11416. return nullopt;
  11417. }
  11418. return stack_detail::unchecked_get<T>(L, index, tracking);
  11419. }
  11420. }
  11421. };
  11422. template <typename T>
  11423. struct qualified_getter<T, std::enable_if_t<meta::is_optional_v<T>>> {
  11424. static T get(lua_State* L, int index, record& tracking) {
  11425. using ValueType = typename meta::unqualified_t<T>::value_type;
  11426. if constexpr (is_lua_reference_v<ValueType>) {
  11427. // actually check if it's none here, otherwise
  11428. // we'll have a none object inside an optional!
  11429. bool success = lua_isnoneornil(L, index) == 0 && stack::check<ValueType>(L, index, no_panic);
  11430. if (!success) {
  11431. // expected type, actual type
  11432. tracking.use(static_cast<int>(success));
  11433. return {};
  11434. }
  11435. return stack_detail::unchecked_get<ValueType>(L, index, tracking);
  11436. }
  11437. else {
  11438. if (!check<ValueType>(L, index, &no_panic)) {
  11439. tracking.use(static_cast<int>(!lua_isnone(L, index)));
  11440. return {};
  11441. }
  11442. return stack_detail::unchecked_get<ValueType>(L, index, tracking);
  11443. }
  11444. }
  11445. };
  11446. }} // namespace sol::stack
  11447. // end of sol/stack_check_get_qualified.hpp
  11448. // end of sol/stack_check_get.hpp
  11449. // beginning of sol/stack_push.hpp
  11450. #include <memory>
  11451. #include <type_traits>
  11452. #include <cassert>
  11453. #include <limits>
  11454. #include <cmath>
  11455. #include <string_view>
  11456. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  11457. #include <variant>
  11458. #endif // Can use variant
  11459. namespace sol { namespace stack {
  11460. namespace stack_detail {
  11461. template <typename T>
  11462. inline bool integer_value_fits(const T& value) {
  11463. if constexpr (sizeof(T) < sizeof(lua_Integer) || (std::is_signed_v<T> && sizeof(T) == sizeof(lua_Integer))) {
  11464. (void)value;
  11465. return true;
  11466. }
  11467. else {
  11468. auto u_min = static_cast<std::intmax_t>((std::numeric_limits<lua_Integer>::min)());
  11469. auto u_max = static_cast<std::uintmax_t>((std::numeric_limits<lua_Integer>::max)());
  11470. auto t_min = static_cast<std::intmax_t>((std::numeric_limits<T>::min)());
  11471. auto t_max = static_cast<std::uintmax_t>((std::numeric_limits<T>::max)());
  11472. return (u_min <= t_min || value >= static_cast<T>(u_min)) && (u_max >= t_max || value <= static_cast<T>(u_max));
  11473. }
  11474. }
  11475. template <typename T>
  11476. int msvc_is_ass_with_if_constexpr_push_enum(std::true_type, lua_State* L, const T& value) {
  11477. if constexpr (meta::any_same_v<std::underlying_type_t<T>, char /*, char8_t*/, char16_t, char32_t>) {
  11478. if constexpr (std::is_signed_v<T>) {
  11479. return stack::push(L, static_cast<std::int_least32_t>(value));
  11480. }
  11481. else {
  11482. return stack::push(L, static_cast<std::uint_least32_t>(value));
  11483. }
  11484. }
  11485. else {
  11486. return stack::push(L, static_cast<std::underlying_type_t<T>>(value));
  11487. }
  11488. }
  11489. template <typename T>
  11490. int msvc_is_ass_with_if_constexpr_push_enum(std::false_type, lua_State*, const T&) {
  11491. return 0;
  11492. }
  11493. } // namespace stack_detail
  11494. inline int push_environment_of(lua_State* L, int index = -1) {
  11495. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11496. luaL_checkstack(L, 1, detail::not_enough_stack_space_environment);
  11497. #endif // make sure stack doesn't overflow
  11498. #if SOL_LUA_VESION_I_ < 502
  11499. // Use lua_getfenv
  11500. lua_getfenv(L, index);
  11501. #else
  11502. // Use upvalues as explained in Lua 5.2 and beyond's manual
  11503. if (lua_getupvalue(L, index, 1) == nullptr) {
  11504. push(L, lua_nil);
  11505. return 1;
  11506. }
  11507. #endif
  11508. return 1;
  11509. }
  11510. template <typename T>
  11511. int push_environment_of(const T& target) {
  11512. target.push();
  11513. return push_environment_of(target.lua_state(), -1) + 1;
  11514. }
  11515. template <typename T>
  11516. struct unqualified_pusher<detail::as_value_tag<T>> {
  11517. template <typename F, typename... Args>
  11518. static int push_fx(lua_State* L, F&& f, Args&&... args) {
  11519. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11520. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  11521. #endif // make sure stack doesn't overflow
  11522. // Basically, we store all user-data like this:
  11523. // If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new
  11524. // data in the first sizeof(T*) bytes, and then however many bytes it takes to
  11525. // do the actual object. Things that are std::ref or plain T* are stored as
  11526. // just the sizeof(T*), and nothing else.
  11527. T* obj = detail::usertype_allocate<T>(L);
  11528. f();
  11529. std::allocator<T> alloc {};
  11530. std::allocator_traits<std::allocator<T>>::construct(alloc, obj, std::forward<Args>(args)...);
  11531. return 1;
  11532. }
  11533. template <typename K, typename... Args>
  11534. static int push_keyed(lua_State* L, K&& k, Args&&... args) {
  11535. stack_detail::undefined_metatable fx(L, &k[0], &stack::stack_detail::set_undefined_methods_on<T>);
  11536. return push_fx(L, fx, std::forward<Args>(args)...);
  11537. }
  11538. template <typename Arg, typename... Args>
  11539. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  11540. if constexpr (std::is_same_v<meta::unqualified_t<Arg>, detail::with_function_tag>) {
  11541. (void)arg;
  11542. return push_fx(L, std::forward<Args>(args)...);
  11543. }
  11544. else {
  11545. return push_keyed(L, usertype_traits<T>::metatable(), std::forward<Arg>(arg), std::forward<Args>(args)...);
  11546. }
  11547. }
  11548. static int push(lua_State* L) {
  11549. return push_keyed(L, usertype_traits<T>::metatable());
  11550. }
  11551. };
  11552. template <typename T>
  11553. struct unqualified_pusher<detail::as_pointer_tag<T>> {
  11554. typedef meta::unqualified_t<T> U;
  11555. template <typename F>
  11556. static int push_fx(lua_State* L, F&& f, T* obj) {
  11557. if (obj == nullptr)
  11558. return stack::push(L, lua_nil);
  11559. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11560. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  11561. #endif // make sure stack doesn't overflow
  11562. T** pref = detail::usertype_allocate_pointer<T>(L);
  11563. f();
  11564. *pref = obj;
  11565. return 1;
  11566. }
  11567. template <typename K>
  11568. static int push_keyed(lua_State* L, K&& k, T* obj) {
  11569. stack_detail::undefined_metatable fx(L, &k[0], &stack::stack_detail::set_undefined_methods_on<U*>);
  11570. return push_fx(L, fx, obj);
  11571. }
  11572. template <typename Arg, typename... Args>
  11573. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  11574. if constexpr (std::is_same_v<meta::unqualified_t<Arg>, detail::with_function_tag>) {
  11575. (void)arg;
  11576. return push_fx(L, std::forward<Args>(args)...);
  11577. }
  11578. else {
  11579. return push_keyed(L, usertype_traits<U*>::metatable(), std::forward<Arg>(arg), std::forward<Args>(args)...);
  11580. }
  11581. }
  11582. };
  11583. template <>
  11584. struct unqualified_pusher<detail::as_reference_tag> {
  11585. template <typename T>
  11586. static int push(lua_State* L, T&& obj) {
  11587. return stack::push(L, detail::ptr(obj));
  11588. }
  11589. };
  11590. namespace stack_detail {
  11591. template <typename T>
  11592. struct uu_pusher {
  11593. using u_traits = unique_usertype_traits<T>;
  11594. using P = typename u_traits::type;
  11595. using Real = typename u_traits::actual_type;
  11596. template <typename Arg, typename... Args>
  11597. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  11598. if constexpr (std::is_base_of_v<Real, meta::unqualified_t<Arg>>) {
  11599. if (u_traits::is_null(arg)) {
  11600. return stack::push(L, lua_nil);
  11601. }
  11602. return push_deep(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  11603. }
  11604. else {
  11605. return push_deep(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
  11606. }
  11607. }
  11608. template <typename... Args>
  11609. static int push_deep(lua_State* L, Args&&... args) {
  11610. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11611. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  11612. #endif // make sure stack doesn't overflow
  11613. P** pref = nullptr;
  11614. detail::unique_destructor* fx = nullptr;
  11615. detail::unique_tag* id = nullptr;
  11616. Real* mem = detail::usertype_unique_allocate<P, Real>(L, pref, fx, id);
  11617. if (luaL_newmetatable(L, &usertype_traits<detail::unique_usertype<std::remove_cv_t<P>>>::metatable()[0]) == 1) {
  11618. detail::lua_reg_table l {};
  11619. int index = 0;
  11620. detail::indexed_insert insert_fx(l, index);
  11621. detail::insert_default_registrations<P>(insert_fx, detail::property_always_true);
  11622. l[index] = { to_string(meta_function::garbage_collect).c_str(), detail::make_destructor<T>() };
  11623. luaL_setfuncs(L, l, 0);
  11624. }
  11625. lua_setmetatable(L, -2);
  11626. *fx = detail::usertype_unique_alloc_destroy<P, Real>;
  11627. *id = &detail::inheritance<P>::template type_unique_cast<Real>;
  11628. detail::default_construct::construct(mem, std::forward<Args>(args)...);
  11629. *pref = unique_usertype_traits<T>::get(*mem);
  11630. return 1;
  11631. }
  11632. };
  11633. } // namespace stack_detail
  11634. template <typename T>
  11635. struct unqualified_pusher<detail::as_unique_tag<T>> {
  11636. template <typename... Args>
  11637. static int push(lua_State* L, Args&&... args) {
  11638. stack_detail::uu_pusher<T> p;
  11639. (void)p;
  11640. return p.push(L, std::forward<Args>(args)...);
  11641. }
  11642. };
  11643. template <typename T, typename>
  11644. struct unqualified_pusher {
  11645. template <typename... Args>
  11646. static int push(lua_State* L, Args&&... args) {
  11647. using Tu = meta::unqualified_t<T>;
  11648. if constexpr (is_lua_reference_v<Tu>) {
  11649. using int_arr = int[];
  11650. int_arr p { (std::forward<Args>(args).push(L))... };
  11651. return p[0];
  11652. }
  11653. else if constexpr (std::is_same_v<Tu, bool>) {
  11654. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11655. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11656. #endif // make sure stack doesn't overflow
  11657. lua_pushboolean(L, std::forward<Args>(args)...);
  11658. return 1;
  11659. }
  11660. else if constexpr (std::is_integral_v<Tu> || std::is_same_v<Tu, lua_Integer>) {
  11661. const Tu& value(std::forward<Args>(args)...);
  11662. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11663. luaL_checkstack(L, 1, detail::not_enough_stack_space_integral);
  11664. #endif // make sure stack doesn't overflow
  11665. #if SOL_LUA_VESION_I_ >= 503
  11666. if (stack_detail::integer_value_fits<Tu>(value)) {
  11667. lua_pushinteger(L, static_cast<lua_Integer>(value));
  11668. return 1;
  11669. }
  11670. #endif // Lua 5.3 and above
  11671. #if SOL_IS_ON(SOL_NUMBER_PRECISION_CHECKS_I_)
  11672. if (static_cast<T>(llround(static_cast<lua_Number>(value))) != value) {
  11673. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  11674. // Is this really worth it?
  11675. assert(false && "integer value will be misrepresented in lua");
  11676. lua_pushinteger(L, static_cast<lua_Integer>(value));
  11677. return 1;
  11678. #else
  11679. throw error(detail::direct_error, "integer value will be misrepresented in lua");
  11680. #endif // No Exceptions
  11681. }
  11682. #endif // Safe Numerics and Number Precision Check
  11683. lua_pushnumber(L, static_cast<lua_Number>(value));
  11684. return 1;
  11685. }
  11686. else if constexpr (std::is_floating_point_v<Tu> || std::is_same_v<Tu, lua_Number>) {
  11687. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11688. luaL_checkstack(L, 1, detail::not_enough_stack_space_floating);
  11689. #endif // make sure stack doesn't overflow
  11690. lua_pushnumber(L, std::forward<Args>(args)...);
  11691. return 1;
  11692. }
  11693. else if constexpr (std::is_same_v<Tu, luaL_Stream*>) {
  11694. luaL_Stream* source { std::forward<Args>(args)... };
  11695. luaL_Stream* stream = static_cast<luaL_Stream*>(lua_newuserdata(L, sizeof(luaL_Stream)));
  11696. stream->f = source->f;
  11697. #if SOL_IS_ON(SOL_LUAL_STREAM_USE_CLOSE_FUNCTION_I_)
  11698. stream->closef = source->closef;
  11699. #endif // LuaJIT and Lua 5.1 and below do not have
  11700. return 1;
  11701. }
  11702. else if constexpr (std::is_same_v<Tu, luaL_Stream>) {
  11703. luaL_Stream& source(std::forward<Args>(args)...);
  11704. luaL_Stream* stream = static_cast<luaL_Stream*>(lua_newuserdata(L, sizeof(luaL_Stream)));
  11705. stream->f = source.f;
  11706. #if SOL_IS_ON(SOL_LUAL_STREAM_USE_CLOSE_FUNCTION_I_)
  11707. stream->closef = source.closef;
  11708. #endif // LuaJIT and Lua 5.1 and below do not have
  11709. return 1;
  11710. }
  11711. else if constexpr (std::is_enum_v<Tu>) {
  11712. return stack_detail::msvc_is_ass_with_if_constexpr_push_enum(std::true_type(), L, std::forward<Args>(args)...);
  11713. }
  11714. else if constexpr (std::is_pointer_v<Tu>) {
  11715. return stack::push<detail::as_pointer_tag<std::remove_pointer_t<T>>>(L, std::forward<Args>(args)...);
  11716. }
  11717. else if constexpr (is_unique_usertype_v<Tu>) {
  11718. return stack::push<detail::as_unique_tag<T>>(L, std::forward<Args>(args)...);
  11719. }
  11720. else {
  11721. return stack::push<detail::as_value_tag<T>>(L, std::forward<Args>(args)...);
  11722. }
  11723. }
  11724. };
  11725. template <typename T>
  11726. struct unqualified_pusher<std::reference_wrapper<T>> {
  11727. static int push(lua_State* L, const std::reference_wrapper<T>& t) {
  11728. return stack::push(L, std::addressof(detail::deref(t.get())));
  11729. }
  11730. };
  11731. template <typename T>
  11732. struct unqualified_pusher<detail::as_table_tag<T>> {
  11733. using has_kvp = meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>>;
  11734. static int push(lua_State* L, const T& tablecont) {
  11735. return push(has_kvp(), std::false_type(), L, tablecont);
  11736. }
  11737. static int push(lua_State* L, const T& tablecont, nested_tag_t) {
  11738. return push(has_kvp(), std::true_type(), L, tablecont);
  11739. }
  11740. static int push(std::true_type, lua_State* L, const T& tablecont) {
  11741. return push(has_kvp(), std::true_type(), L, tablecont);
  11742. }
  11743. static int push(std::false_type, lua_State* L, const T& tablecont) {
  11744. return push(has_kvp(), std::false_type(), L, tablecont);
  11745. }
  11746. template <bool is_nested>
  11747. static int push(std::true_type, std::integral_constant<bool, is_nested>, lua_State* L, const T& tablecont) {
  11748. auto& cont = detail::deref(detail::unwrap(tablecont));
  11749. lua_createtable(L, static_cast<int>(cont.size()), 0);
  11750. int tableindex = lua_gettop(L);
  11751. for (const auto& pair : cont) {
  11752. if (is_nested) {
  11753. set_field(L, pair.first, as_nested_ref(pair.second), tableindex);
  11754. }
  11755. else {
  11756. set_field(L, pair.first, pair.second, tableindex);
  11757. }
  11758. }
  11759. return 1;
  11760. }
  11761. template <bool is_nested>
  11762. static int push(std::false_type, std::integral_constant<bool, is_nested>, lua_State* L, const T& tablecont) {
  11763. auto& cont = detail::deref(detail::unwrap(tablecont));
  11764. lua_createtable(L, stack_detail::get_size_hint(cont), 0);
  11765. int tableindex = lua_gettop(L);
  11766. std::size_t index = 1;
  11767. for (const auto& i : cont) {
  11768. #if SOL_LUA_VESION_I_ >= 503
  11769. int p = is_nested ? stack::push(L, as_nested_ref(i)) : stack::push(L, i);
  11770. for (int pi = 0; pi < p; ++pi) {
  11771. lua_seti(L, tableindex, static_cast<lua_Integer>(index++));
  11772. }
  11773. #else
  11774. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11775. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11776. #endif // make sure stack doesn't overflow
  11777. lua_pushinteger(L, static_cast<lua_Integer>(index));
  11778. int p = is_nested ? stack::push(L, as_nested_ref(i)) : stack::push(L, i);
  11779. if (p == 1) {
  11780. ++index;
  11781. lua_settable(L, tableindex);
  11782. }
  11783. else {
  11784. int firstindex = tableindex + 1 + 1;
  11785. for (int pi = 0; pi < p; ++pi) {
  11786. stack::push(L, index);
  11787. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11788. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11789. #endif // make sure stack doesn't overflow
  11790. lua_pushvalue(L, firstindex);
  11791. lua_settable(L, tableindex);
  11792. ++index;
  11793. ++firstindex;
  11794. }
  11795. lua_pop(L, 1 + p);
  11796. }
  11797. #endif // Lua Version 5.3 and others
  11798. }
  11799. // TODO: figure out a better way to do this...?
  11800. // set_field(L, -1, cont.size());
  11801. return 1;
  11802. }
  11803. };
  11804. template <typename T>
  11805. struct unqualified_pusher<as_table_t<T>> {
  11806. static int push(lua_State* L, const T& v) {
  11807. using inner_t = std::remove_pointer_t<meta::unwrap_unqualified_t<T>>;
  11808. if constexpr (is_container_v<inner_t>) {
  11809. return stack::push<detail::as_table_tag<T>>(L, v);
  11810. }
  11811. else {
  11812. return stack::push(L, v);
  11813. }
  11814. }
  11815. };
  11816. template <typename T>
  11817. struct unqualified_pusher<nested<T>> {
  11818. static int push(lua_State* L, const T& tablecont) {
  11819. using Tu = meta::unwrap_unqualified_t<T>;
  11820. using inner_t = std::remove_pointer_t<Tu>;
  11821. if constexpr (is_container_v<inner_t>) {
  11822. return stack::push<detail::as_table_tag<T>>(L, tablecont, nested_tag);
  11823. }
  11824. else {
  11825. return stack::push<Tu>(L, tablecont);
  11826. }
  11827. }
  11828. };
  11829. template <typename T>
  11830. struct unqualified_pusher<std::initializer_list<T>> {
  11831. static int push(lua_State* L, const std::initializer_list<T>& il) {
  11832. unqualified_pusher<detail::as_table_tag<std::initializer_list<T>>> p {};
  11833. // silence annoying VC++ warning
  11834. (void)p;
  11835. return p.push(L, il);
  11836. }
  11837. };
  11838. template <>
  11839. struct unqualified_pusher<lua_nil_t> {
  11840. static int push(lua_State* L, lua_nil_t) {
  11841. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11842. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11843. #endif // make sure stack doesn't overflow
  11844. lua_pushnil(L);
  11845. return 1;
  11846. }
  11847. };
  11848. template <>
  11849. struct unqualified_pusher<stack_count> {
  11850. static int push(lua_State*, stack_count st) {
  11851. return st.count;
  11852. }
  11853. };
  11854. template <>
  11855. struct unqualified_pusher<metatable_key_t> {
  11856. static int push(lua_State* L, metatable_key_t) {
  11857. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11858. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11859. #endif // make sure stack doesn't overflow
  11860. lua_pushlstring(L, to_string(meta_function::metatable).c_str(), 4);
  11861. return 1;
  11862. }
  11863. };
  11864. template <>
  11865. struct unqualified_pusher<std::remove_pointer_t<lua_CFunction>> {
  11866. static int push(lua_State* L, lua_CFunction func, int n = 0) {
  11867. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11868. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11869. #endif // make sure stack doesn't overflow
  11870. lua_pushcclosure(L, func, n);
  11871. return 1;
  11872. }
  11873. };
  11874. template <>
  11875. struct unqualified_pusher<lua_CFunction> {
  11876. static int push(lua_State* L, lua_CFunction func, int n = 0) {
  11877. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11878. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11879. #endif // make sure stack doesn't overflow
  11880. lua_pushcclosure(L, func, n);
  11881. return 1;
  11882. }
  11883. };
  11884. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  11885. template <>
  11886. struct unqualified_pusher<std::remove_pointer_t<detail::lua_CFunction_noexcept>> {
  11887. static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) {
  11888. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11889. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11890. #endif // make sure stack doesn't overflow
  11891. lua_pushcclosure(L, func, n);
  11892. return 1;
  11893. }
  11894. };
  11895. template <>
  11896. struct unqualified_pusher<detail::lua_CFunction_noexcept> {
  11897. static int push(lua_State* L, detail::lua_CFunction_noexcept func, int n = 0) {
  11898. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11899. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11900. #endif // make sure stack doesn't overflow
  11901. lua_pushcclosure(L, func, n);
  11902. return 1;
  11903. }
  11904. };
  11905. #endif // noexcept function type
  11906. template <>
  11907. struct unqualified_pusher<c_closure> {
  11908. static int push(lua_State* L, c_closure cc) {
  11909. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11910. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11911. #endif // make sure stack doesn't overflow
  11912. lua_pushcclosure(L, cc.c_function, cc.upvalues);
  11913. return 1;
  11914. }
  11915. };
  11916. template <typename Arg, typename... Args>
  11917. struct unqualified_pusher<closure<Arg, Args...>> {
  11918. template <std::size_t... I, typename T>
  11919. static int push(std::index_sequence<I...>, lua_State* L, T&& c) {
  11920. using f_tuple = decltype(std::forward<T>(c).upvalues);
  11921. int pushcount = multi_push(L, std::get<I>(std::forward<f_tuple>(std::forward<T>(c).upvalues))...);
  11922. return stack::push(L, c_closure(c.c_function, pushcount));
  11923. }
  11924. template <typename T>
  11925. static int push(lua_State* L, T&& c) {
  11926. return push(std::make_index_sequence<1 + sizeof...(Args)>(), L, std::forward<T>(c));
  11927. }
  11928. };
  11929. template <>
  11930. struct unqualified_pusher<void*> {
  11931. static int push(lua_State* L, void* userdata) {
  11932. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11933. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11934. #endif // make sure stack doesn't overflow
  11935. lua_pushlightuserdata(L, userdata);
  11936. return 1;
  11937. }
  11938. };
  11939. template <>
  11940. struct unqualified_pusher<const void*> {
  11941. static int push(lua_State* L, const void* userdata) {
  11942. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11943. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11944. #endif // make sure stack doesn't overflow
  11945. lua_pushlightuserdata(L, const_cast<void*>(userdata));
  11946. return 1;
  11947. }
  11948. };
  11949. template <>
  11950. struct unqualified_pusher<lightuserdata_value> {
  11951. static int push(lua_State* L, lightuserdata_value userdata) {
  11952. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11953. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11954. #endif // make sure stack doesn't overflow
  11955. lua_pushlightuserdata(L, userdata);
  11956. return 1;
  11957. }
  11958. };
  11959. template <typename T>
  11960. struct unqualified_pusher<light<T>> {
  11961. static int push(lua_State* L, light<T> l) {
  11962. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11963. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11964. #endif // make sure stack doesn't overflow
  11965. lua_pushlightuserdata(L, static_cast<void*>(l.value));
  11966. return 1;
  11967. }
  11968. };
  11969. template <typename T>
  11970. struct unqualified_pusher<user<T>> {
  11971. template <bool with_meta = true, typename Key, typename... Args>
  11972. static int push_with(lua_State* L, Key&& name, Args&&... args) {
  11973. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11974. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  11975. #endif // make sure stack doesn't overflow
  11976. // A dumb pusher
  11977. T* data = detail::user_allocate<T>(L);
  11978. if (with_meta) {
  11979. // Make sure we have a plain GC set for this data
  11980. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  11981. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  11982. #endif // make sure stack doesn't overflow
  11983. if (luaL_newmetatable(L, name) != 0) {
  11984. lua_CFunction cdel = detail::user_alloc_destruct<T>;
  11985. lua_pushcclosure(L, cdel, 0);
  11986. lua_setfield(L, -2, "__gc");
  11987. }
  11988. lua_setmetatable(L, -2);
  11989. }
  11990. std::allocator<T> alloc {};
  11991. std::allocator_traits<std::allocator<T>>::construct(alloc, data, std::forward<Args>(args)...);
  11992. return 1;
  11993. }
  11994. template <typename Arg, typename... Args>
  11995. static int push(lua_State* L, Arg&& arg, Args&&... args) {
  11996. if constexpr (std::is_same_v<meta::unqualified_t<Arg>, metatable_key_t>) {
  11997. const auto name = &arg[0];
  11998. return push_with<true>(L, name, std::forward<Args>(args)...);
  11999. }
  12000. else if constexpr (std::is_same_v<meta::unqualified_t<Arg>, no_metatable_t>) {
  12001. (void)arg;
  12002. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  12003. return push_with<false>(L, name, std::forward<Args>(args)...);
  12004. }
  12005. else {
  12006. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  12007. return push_with(L, name, std::forward<Arg>(arg), std::forward<Args>(args)...);
  12008. }
  12009. }
  12010. static int push(lua_State* L, const user<T>& u) {
  12011. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  12012. return push_with(L, name, u.value);
  12013. }
  12014. static int push(lua_State* L, user<T>&& u) {
  12015. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  12016. return push_with(L, name, std::move(u.value));
  12017. }
  12018. static int push(lua_State* L, no_metatable_t, const user<T>& u) {
  12019. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  12020. return push_with<false>(L, name, u.value);
  12021. }
  12022. static int push(lua_State* L, no_metatable_t, user<T>&& u) {
  12023. const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
  12024. return push_with<false>(L, name, std::move(u.value));
  12025. }
  12026. };
  12027. template <>
  12028. struct unqualified_pusher<userdata_value> {
  12029. static int push(lua_State* L, userdata_value data) {
  12030. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12031. luaL_checkstack(L, 1, detail::not_enough_stack_space_userdata);
  12032. #endif // make sure stack doesn't overflow
  12033. void** ud = detail::usertype_allocate_pointer<void>(L);
  12034. *ud = data.value;
  12035. return 1;
  12036. }
  12037. };
  12038. template <>
  12039. struct unqualified_pusher<const char*> {
  12040. static int push_sized(lua_State* L, const char* str, std::size_t len) {
  12041. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12042. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  12043. #endif // make sure stack doesn't overflow
  12044. lua_pushlstring(L, str, len);
  12045. return 1;
  12046. }
  12047. static int push(lua_State* L, const char* str) {
  12048. if (str == nullptr)
  12049. return stack::push(L, lua_nil);
  12050. return push_sized(L, str, std::char_traits<char>::length(str));
  12051. }
  12052. static int push(lua_State* L, const char* strb, const char* stre) {
  12053. return push_sized(L, strb, stre - strb);
  12054. }
  12055. static int push(lua_State* L, const char* str, std::size_t len) {
  12056. return push_sized(L, str, len);
  12057. }
  12058. };
  12059. template <>
  12060. struct unqualified_pusher<char*> {
  12061. static int push_sized(lua_State* L, const char* str, std::size_t len) {
  12062. unqualified_pusher<const char*> p {};
  12063. (void)p;
  12064. return p.push_sized(L, str, len);
  12065. }
  12066. static int push(lua_State* L, const char* str) {
  12067. unqualified_pusher<const char*> p {};
  12068. (void)p;
  12069. return p.push(L, str);
  12070. }
  12071. static int push(lua_State* L, const char* strb, const char* stre) {
  12072. unqualified_pusher<const char*> p {};
  12073. (void)p;
  12074. return p.push(L, strb, stre);
  12075. }
  12076. static int push(lua_State* L, const char* str, std::size_t len) {
  12077. unqualified_pusher<const char*> p {};
  12078. (void)p;
  12079. return p.push(L, str, len);
  12080. }
  12081. };
  12082. template <size_t N>
  12083. struct unqualified_pusher<char[N]> {
  12084. static int push(lua_State* L, const char (&str)[N]) {
  12085. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12086. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  12087. #endif // make sure stack doesn't overflow
  12088. lua_pushlstring(L, str, std::char_traits<char>::length(str));
  12089. return 1;
  12090. }
  12091. static int push(lua_State* L, const char (&str)[N], std::size_t sz) {
  12092. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12093. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  12094. #endif // make sure stack doesn't overflow
  12095. lua_pushlstring(L, str, sz);
  12096. return 1;
  12097. }
  12098. };
  12099. template <>
  12100. struct unqualified_pusher<char> {
  12101. static int push(lua_State* L, char c) {
  12102. const char str[2] = { c, '\0' };
  12103. return stack::push(L, static_cast<const char*>(str), 1);
  12104. }
  12105. };
  12106. template <typename Ch, typename Traits, typename Al>
  12107. struct unqualified_pusher<std::basic_string<Ch, Traits, Al>> {
  12108. static int push(lua_State* L, const std::basic_string<Ch, Traits, Al>& str) {
  12109. if constexpr (!std::is_same_v<Ch, char>) {
  12110. return stack::push(L, str.data(), str.size());
  12111. }
  12112. else {
  12113. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12114. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  12115. #endif // make sure stack doesn't overflow
  12116. lua_pushlstring(L, str.c_str(), str.size());
  12117. return 1;
  12118. }
  12119. }
  12120. static int push(lua_State* L, const std::basic_string<Ch, Traits, Al>& str, std::size_t sz) {
  12121. if constexpr (!std::is_same_v<Ch, char>) {
  12122. return stack::push(L, str.data(), sz);
  12123. }
  12124. else {
  12125. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12126. luaL_checkstack(L, 1, detail::not_enough_stack_space_string);
  12127. #endif // make sure stack doesn't overflow
  12128. lua_pushlstring(L, str.c_str(), sz);
  12129. return 1;
  12130. }
  12131. }
  12132. };
  12133. template <typename Ch, typename Traits>
  12134. struct unqualified_pusher<basic_string_view<Ch, Traits>> {
  12135. static int push(lua_State* L, const basic_string_view<Ch, Traits>& sv) {
  12136. return stack::push(L, sv.data(), sv.length());
  12137. }
  12138. static int push(lua_State* L, const basic_string_view<Ch, Traits>& sv, std::size_t n) {
  12139. return stack::push(L, sv.data(), n);
  12140. }
  12141. };
  12142. template <>
  12143. struct unqualified_pusher<meta_function> {
  12144. static int push(lua_State* L, meta_function m) {
  12145. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12146. luaL_checkstack(L, 1, detail::not_enough_stack_space_meta_function_name);
  12147. #endif // make sure stack doesn't overflow
  12148. const std::string& str = to_string(m);
  12149. lua_pushlstring(L, str.c_str(), str.size());
  12150. return 1;
  12151. }
  12152. };
  12153. template <>
  12154. struct unqualified_pusher<absolute_index> {
  12155. static int push(lua_State* L, absolute_index ai) {
  12156. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12157. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  12158. #endif // make sure stack doesn't overflow
  12159. lua_pushvalue(L, ai);
  12160. return 1;
  12161. }
  12162. };
  12163. template <>
  12164. struct unqualified_pusher<raw_index> {
  12165. static int push(lua_State* L, raw_index ri) {
  12166. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12167. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  12168. #endif // make sure stack doesn't overflow
  12169. lua_pushvalue(L, ri);
  12170. return 1;
  12171. }
  12172. };
  12173. template <>
  12174. struct unqualified_pusher<ref_index> {
  12175. static int push(lua_State* L, ref_index ri) {
  12176. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12177. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  12178. #endif // make sure stack doesn't overflow
  12179. lua_rawgeti(L, LUA_REGISTRYINDEX, ri);
  12180. return 1;
  12181. }
  12182. };
  12183. template <>
  12184. struct unqualified_pusher<const wchar_t*> {
  12185. static int push(lua_State* L, const wchar_t* wstr) {
  12186. return push(L, wstr, std::char_traits<wchar_t>::length(wstr));
  12187. }
  12188. static int push(lua_State* L, const wchar_t* wstr, std::size_t sz) {
  12189. return push(L, wstr, wstr + sz);
  12190. }
  12191. static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
  12192. if constexpr (sizeof(wchar_t) == 2) {
  12193. const char16_t* sb = reinterpret_cast<const char16_t*>(strb);
  12194. const char16_t* se = reinterpret_cast<const char16_t*>(stre);
  12195. return stack::push(L, sb, se);
  12196. }
  12197. else {
  12198. const char32_t* sb = reinterpret_cast<const char32_t*>(strb);
  12199. const char32_t* se = reinterpret_cast<const char32_t*>(stre);
  12200. return stack::push(L, sb, se);
  12201. }
  12202. }
  12203. };
  12204. template <>
  12205. struct unqualified_pusher<wchar_t*> {
  12206. static int push(lua_State* L, const wchar_t* str) {
  12207. unqualified_pusher<const wchar_t*> p {};
  12208. (void)p;
  12209. return p.push(L, str);
  12210. }
  12211. static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
  12212. unqualified_pusher<const wchar_t*> p {};
  12213. (void)p;
  12214. return p.push(L, strb, stre);
  12215. }
  12216. static int push(lua_State* L, const wchar_t* str, std::size_t len) {
  12217. unqualified_pusher<const wchar_t*> p {};
  12218. (void)p;
  12219. return p.push(L, str, len);
  12220. }
  12221. };
  12222. template <>
  12223. struct unqualified_pusher<const char16_t*> {
  12224. static int convert_into(lua_State* L, char* start, std::size_t, const char16_t* strb, const char16_t* stre) {
  12225. char* target = start;
  12226. char32_t cp = 0;
  12227. for (const char16_t* strtarget = strb; strtarget < stre;) {
  12228. auto dr = unicode::utf16_to_code_point(strtarget, stre);
  12229. if (dr.error != unicode::error_code::ok) {
  12230. cp = unicode::unicode_detail::replacement;
  12231. }
  12232. else {
  12233. cp = dr.codepoint;
  12234. }
  12235. auto er = unicode::code_point_to_utf8(cp);
  12236. const char* utf8data = er.code_units.data();
  12237. std::memcpy(target, utf8data, er.code_units_size);
  12238. target += er.code_units_size;
  12239. strtarget = dr.next;
  12240. }
  12241. return stack::push(L, start, target);
  12242. }
  12243. static int push(lua_State* L, const char16_t* u16str) {
  12244. return push(L, u16str, std::char_traits<char16_t>::length(u16str));
  12245. }
  12246. static int push(lua_State* L, const char16_t* u16str, std::size_t sz) {
  12247. return push(L, u16str, u16str + sz);
  12248. }
  12249. static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
  12250. char sbo[SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_];
  12251. // if our max string space is small enough, use SBO
  12252. // right off the bat
  12253. std::size_t max_possible_code_units = (stre - strb) * 4;
  12254. if (max_possible_code_units <= SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  12255. return convert_into(L, sbo, max_possible_code_units, strb, stre);
  12256. }
  12257. // otherwise, we must manually count/check size
  12258. std::size_t needed_size = 0;
  12259. for (const char16_t* strtarget = strb; strtarget < stre;) {
  12260. auto dr = unicode::utf16_to_code_point(strtarget, stre);
  12261. auto er = unicode::code_point_to_utf8(dr.codepoint);
  12262. needed_size += er.code_units_size;
  12263. strtarget = dr.next;
  12264. }
  12265. if (needed_size < SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  12266. return convert_into(L, sbo, needed_size, strb, stre);
  12267. }
  12268. std::string u8str("", 0);
  12269. u8str.resize(needed_size);
  12270. char* target = const_cast<char*>(u8str.data());
  12271. return convert_into(L, target, needed_size, strb, stre);
  12272. }
  12273. };
  12274. template <>
  12275. struct unqualified_pusher<char16_t*> {
  12276. static int push(lua_State* L, const char16_t* str) {
  12277. unqualified_pusher<const char16_t*> p {};
  12278. (void)p;
  12279. return p.push(L, str);
  12280. }
  12281. static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
  12282. unqualified_pusher<const char16_t*> p {};
  12283. (void)p;
  12284. return p.push(L, strb, stre);
  12285. }
  12286. static int push(lua_State* L, const char16_t* str, std::size_t len) {
  12287. unqualified_pusher<const char16_t*> p {};
  12288. (void)p;
  12289. return p.push(L, str, len);
  12290. }
  12291. };
  12292. template <>
  12293. struct unqualified_pusher<const char32_t*> {
  12294. static int convert_into(lua_State* L, char* start, std::size_t, const char32_t* strb, const char32_t* stre) {
  12295. char* target = start;
  12296. char32_t cp = 0;
  12297. for (const char32_t* strtarget = strb; strtarget < stre;) {
  12298. auto dr = unicode::utf32_to_code_point(strtarget, stre);
  12299. if (dr.error != unicode::error_code::ok) {
  12300. cp = unicode::unicode_detail::replacement;
  12301. }
  12302. else {
  12303. cp = dr.codepoint;
  12304. }
  12305. auto er = unicode::code_point_to_utf8(cp);
  12306. const char* data = er.code_units.data();
  12307. std::memcpy(target, data, er.code_units_size);
  12308. target += er.code_units_size;
  12309. strtarget = dr.next;
  12310. }
  12311. return stack::push(L, start, target);
  12312. }
  12313. static int push(lua_State* L, const char32_t* u32str) {
  12314. return push(L, u32str, u32str + std::char_traits<char32_t>::length(u32str));
  12315. }
  12316. static int push(lua_State* L, const char32_t* u32str, std::size_t sz) {
  12317. return push(L, u32str, u32str + sz);
  12318. }
  12319. static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
  12320. char sbo[SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_];
  12321. // if our max string space is small enough, use SBO
  12322. // right off the bat
  12323. std::size_t max_possible_code_units = (stre - strb) * 4;
  12324. if (max_possible_code_units <= SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  12325. return convert_into(L, sbo, max_possible_code_units, strb, stre);
  12326. }
  12327. // otherwise, we must manually count/check size
  12328. std::size_t needed_size = 0;
  12329. for (const char32_t* strtarget = strb; strtarget < stre;) {
  12330. auto dr = unicode::utf32_to_code_point(strtarget, stre);
  12331. auto er = unicode::code_point_to_utf8(dr.codepoint);
  12332. needed_size += er.code_units_size;
  12333. strtarget = dr.next;
  12334. }
  12335. if (needed_size < SOL_OPTIMIZATION_STRING_CONVERSION_STACK_SIZE_I_) {
  12336. return convert_into(L, sbo, needed_size, strb, stre);
  12337. }
  12338. std::string u8str("", 0);
  12339. u8str.resize(needed_size);
  12340. char* target = const_cast<char*>(u8str.data());
  12341. return convert_into(L, target, needed_size, strb, stre);
  12342. }
  12343. };
  12344. template <>
  12345. struct unqualified_pusher<char32_t*> {
  12346. static int push(lua_State* L, const char32_t* str) {
  12347. unqualified_pusher<const char32_t*> p {};
  12348. (void)p;
  12349. return p.push(L, str);
  12350. }
  12351. static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
  12352. unqualified_pusher<const char32_t*> p {};
  12353. (void)p;
  12354. return p.push(L, strb, stre);
  12355. }
  12356. static int push(lua_State* L, const char32_t* str, std::size_t len) {
  12357. unqualified_pusher<const char32_t*> p {};
  12358. (void)p;
  12359. return p.push(L, str, len);
  12360. }
  12361. };
  12362. template <size_t N>
  12363. struct unqualified_pusher<wchar_t[N]> {
  12364. static int push(lua_State* L, const wchar_t (&str)[N]) {
  12365. return push(L, str, std::char_traits<wchar_t>::length(str));
  12366. }
  12367. static int push(lua_State* L, const wchar_t (&str)[N], std::size_t sz) {
  12368. const wchar_t* str_ptr = static_cast<const wchar_t*>(str);
  12369. return stack::push<const wchar_t*>(L, str_ptr, str_ptr + sz);
  12370. }
  12371. };
  12372. template <size_t N>
  12373. struct unqualified_pusher<char16_t[N]> {
  12374. static int push(lua_State* L, const char16_t (&str)[N]) {
  12375. return push(L, str, std::char_traits<char16_t>::length(str));
  12376. }
  12377. static int push(lua_State* L, const char16_t (&str)[N], std::size_t sz) {
  12378. const char16_t* str_ptr = static_cast<const char16_t*>(str);
  12379. return stack::push<const char16_t*>(L, str_ptr, str_ptr + sz);
  12380. }
  12381. };
  12382. template <size_t N>
  12383. struct unqualified_pusher<char32_t[N]> {
  12384. static int push(lua_State* L, const char32_t (&str)[N]) {
  12385. return push(L, str, std::char_traits<char32_t>::length(str));
  12386. }
  12387. static int push(lua_State* L, const char32_t (&str)[N], std::size_t sz) {
  12388. const char32_t* str_ptr = static_cast<const char32_t*>(str);
  12389. return stack::push<const char32_t*>(L, str_ptr, str_ptr + sz);
  12390. }
  12391. };
  12392. template <>
  12393. struct unqualified_pusher<wchar_t> {
  12394. static int push(lua_State* L, wchar_t c) {
  12395. const wchar_t str[2] = { c, '\0' };
  12396. return stack::push(L, static_cast<const wchar_t*>(str), 1);
  12397. }
  12398. };
  12399. template <>
  12400. struct unqualified_pusher<char16_t> {
  12401. static int push(lua_State* L, char16_t c) {
  12402. const char16_t str[2] = { c, '\0' };
  12403. return stack::push(L, static_cast<const char16_t*>(str), 1);
  12404. }
  12405. };
  12406. template <>
  12407. struct unqualified_pusher<char32_t> {
  12408. static int push(lua_State* L, char32_t c) {
  12409. const char32_t str[2] = { c, '\0' };
  12410. return stack::push(L, static_cast<const char32_t*>(str), 1);
  12411. }
  12412. };
  12413. template <typename... Args>
  12414. struct unqualified_pusher<std::tuple<Args...>> {
  12415. template <std::size_t... I, typename T>
  12416. static int push(std::index_sequence<I...>, lua_State* L, T&& t) {
  12417. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  12418. luaL_checkstack(L, static_cast<int>(sizeof...(I)), detail::not_enough_stack_space_generic);
  12419. #endif // make sure stack doesn't overflow
  12420. int pushcount = 0;
  12421. (void)detail::swallow { 0, (pushcount += stack::push(L, std::get<I>(std::forward<T>(t))), 0)... };
  12422. return pushcount;
  12423. }
  12424. template <typename T>
  12425. static int push(lua_State* L, T&& t) {
  12426. return push(std::index_sequence_for<Args...>(), L, std::forward<T>(t));
  12427. }
  12428. };
  12429. template <typename A, typename B>
  12430. struct unqualified_pusher<std::pair<A, B>> {
  12431. template <typename T>
  12432. static int push(lua_State* L, T&& t) {
  12433. int pushcount = stack::push(L, std::get<0>(std::forward<T>(t)));
  12434. pushcount += stack::push(L, std::get<1>(std::forward<T>(t)));
  12435. return pushcount;
  12436. }
  12437. };
  12438. template <typename T>
  12439. struct unqualified_pusher<T, std::enable_if_t<meta::is_optional_v<T>>> {
  12440. using ValueType = typename meta::unqualified_t<T>::value_type;
  12441. template <typename Optional>
  12442. static int push(lua_State* L, Optional&& op) {
  12443. using QualifiedValueType = meta::conditional_t<std::is_lvalue_reference_v<Optional>, ValueType&, ValueType&&>;
  12444. if (!op) {
  12445. return stack::push(L, nullopt);
  12446. }
  12447. return stack::push(L, static_cast<QualifiedValueType>(op.value()));
  12448. }
  12449. };
  12450. template <>
  12451. struct unqualified_pusher<nullopt_t> {
  12452. static int push(lua_State* L, nullopt_t) {
  12453. return stack::push(L, lua_nil);
  12454. }
  12455. };
  12456. template <>
  12457. struct unqualified_pusher<std::nullptr_t> {
  12458. static int push(lua_State* L, std::nullptr_t) {
  12459. return stack::push(L, lua_nil);
  12460. }
  12461. };
  12462. template <>
  12463. struct unqualified_pusher<this_state> {
  12464. static int push(lua_State*, const this_state&) {
  12465. return 0;
  12466. }
  12467. };
  12468. template <>
  12469. struct unqualified_pusher<this_main_state> {
  12470. static int push(lua_State*, const this_main_state&) {
  12471. return 0;
  12472. }
  12473. };
  12474. template <>
  12475. struct unqualified_pusher<new_table> {
  12476. static int push(lua_State* L, const new_table& nt) {
  12477. lua_createtable(L, nt.sequence_hint, nt.map_hint);
  12478. return 1;
  12479. }
  12480. };
  12481. template <typename Allocator>
  12482. struct unqualified_pusher<basic_bytecode<Allocator>> {
  12483. template <typename T>
  12484. static int push(lua_State* L, T&& bc, const char* bytecode_name) {
  12485. const auto first = bc.data();
  12486. const auto bcsize = bc.size();
  12487. // pushes either the function, or an error
  12488. // if it errors, shit goes south, and people can test that upstream
  12489. (void)luaL_loadbuffer(
  12490. L, reinterpret_cast<const char*>(first), static_cast<std::size_t>(bcsize * (sizeof(*first) / sizeof(const char))), bytecode_name);
  12491. return 1;
  12492. }
  12493. template <typename T>
  12494. static int push(lua_State* L, T&& bc) {
  12495. return push(L, std::forward<bc>(bc), "bytecode");
  12496. }
  12497. };
  12498. #if SOL_IS_ON(SOL_STD_VARIANT_I_)
  12499. namespace stack_detail {
  12500. struct push_function {
  12501. lua_State* L;
  12502. push_function(lua_State* L) : L(L) {
  12503. }
  12504. template <typename T>
  12505. int operator()(T&& value) const {
  12506. return stack::push<T>(L, std::forward<T>(value));
  12507. }
  12508. };
  12509. } // namespace stack_detail
  12510. template <typename... Tn>
  12511. struct unqualified_pusher<std::variant<Tn...>> {
  12512. static int push(lua_State* L, const std::variant<Tn...>& v) {
  12513. return std::visit(stack_detail::push_function(L), v);
  12514. }
  12515. static int push(lua_State* L, std::variant<Tn...>&& v) {
  12516. return std::visit(stack_detail::push_function(L), std::move(v));
  12517. }
  12518. };
  12519. #endif // Variant because Clang is terrible
  12520. }} // namespace sol::stack
  12521. // end of sol/stack_push.hpp
  12522. // beginning of sol/stack_pop.hpp
  12523. #include <utility>
  12524. #include <tuple>
  12525. namespace sol {
  12526. namespace stack {
  12527. template <typename T, typename>
  12528. struct popper {
  12529. inline static decltype(auto) pop(lua_State* L) {
  12530. if constexpr (is_stack_based_v<meta::unqualified_t<T>>) {
  12531. static_assert(!is_stack_based_v<meta::unqualified_t<T>>,
  12532. "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 "
  12533. "scope!");
  12534. }
  12535. else {
  12536. record tracking{};
  12537. decltype(auto) r = get<T>(L, -lua_size<T>::value, tracking);
  12538. lua_pop(L, tracking.used);
  12539. return r;
  12540. }
  12541. }
  12542. };
  12543. }
  12544. } // namespace sol::stack
  12545. // end of sol/stack_pop.hpp
  12546. // beginning of sol/stack_field.hpp
  12547. namespace sol { namespace stack {
  12548. template <typename T, bool global, bool raw, typename>
  12549. struct field_getter {
  12550. static constexpr int default_table_index = meta::conditional_t < meta::is_c_str_v<T>
  12551. #if SOL_LUA_VESION_I_ >= 503
  12552. || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
  12553. #endif // integer global keys 5.3 or better
  12554. || (raw && std::is_void_v<std::remove_pointer_t<T>>),
  12555. std::integral_constant<int, -1>, std::integral_constant<int, -2> > ::value;
  12556. template <typename Key>
  12557. void get(lua_State* L, Key&& key, int tableindex = default_table_index) {
  12558. if constexpr (std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, override_value_t> || std::is_same_v<T, create_if_nil_t>) {
  12559. (void)L;
  12560. (void)key;
  12561. (void)tableindex;
  12562. }
  12563. else if constexpr (std::is_same_v<T, env_key_t>) {
  12564. (void)key;
  12565. #if SOL_LUA_VESION_I_ < 502
  12566. // Use lua_setfenv
  12567. lua_getfenv(L, tableindex);
  12568. #else
  12569. // Use upvalues as explained in Lua 5.2 and beyond's manual
  12570. if (lua_getupvalue(L, tableindex, 1) == nullptr) {
  12571. push(L, lua_nil);
  12572. }
  12573. #endif
  12574. }
  12575. else if constexpr (std::is_same_v<T, metatable_key_t>) {
  12576. (void)key;
  12577. if (lua_getmetatable(L, tableindex) == 0)
  12578. push(L, lua_nil);
  12579. }
  12580. else if constexpr (raw) {
  12581. if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  12582. lua_rawgeti(L, tableindex, static_cast<lua_Integer>(key));
  12583. }
  12584. #if SOL_LUA_VESION_I_ >= 502
  12585. else if constexpr (std::is_void_v<std::remove_pointer_t<T>>) {
  12586. lua_rawgetp(L, tableindex, key);
  12587. }
  12588. #endif // Lua 5.2.x+
  12589. else {
  12590. push(L, std::forward<Key>(key));
  12591. lua_rawget(L, tableindex);
  12592. }
  12593. }
  12594. else {
  12595. if constexpr (meta::is_c_str_v<T>) {
  12596. if constexpr (global) {
  12597. (void)tableindex;
  12598. lua_getglobal(L, &key[0]);
  12599. }
  12600. else {
  12601. lua_getfield(L, tableindex, &key[0]);
  12602. }
  12603. }
  12604. #if SOL_LUA_VESION_I_ >= 503
  12605. else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  12606. lua_geti(L, tableindex, static_cast<lua_Integer>(key));
  12607. }
  12608. #endif // Lua 5.3.x+
  12609. else {
  12610. push(L, std::forward<Key>(key));
  12611. lua_gettable(L, tableindex);
  12612. }
  12613. }
  12614. }
  12615. };
  12616. template <typename... Args, bool b, bool raw, typename C>
  12617. struct field_getter<std::tuple<Args...>, b, raw, C> {
  12618. template <std::size_t... I, typename Keys>
  12619. void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) {
  12620. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
  12621. void(detail::swallow { (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
  12622. reference saved(L, -1);
  12623. lua_pop(L, static_cast<int>(sizeof...(I)));
  12624. saved.push();
  12625. }
  12626. template <typename Keys>
  12627. void get(lua_State* L, Keys&& keys) {
  12628. apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), lua_absindex(L, -1));
  12629. }
  12630. template <typename Keys>
  12631. void get(lua_State* L, Keys&& keys, int tableindex) {
  12632. apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), tableindex);
  12633. }
  12634. };
  12635. template <typename A, typename B, bool b, bool raw, typename C>
  12636. struct field_getter<std::pair<A, B>, b, raw, C> {
  12637. template <typename Keys>
  12638. void get(lua_State* L, Keys&& keys, int tableindex) {
  12639. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
  12640. get_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)));
  12641. reference saved(L, -1);
  12642. lua_pop(L, static_cast<int>(2));
  12643. saved.push();
  12644. }
  12645. template <typename Keys>
  12646. void get(lua_State* L, Keys&& keys) {
  12647. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)));
  12648. get_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)));
  12649. reference saved(L, -1);
  12650. lua_pop(L, static_cast<int>(2));
  12651. saved.push();
  12652. }
  12653. };
  12654. template <typename T, bool global, bool raw, typename>
  12655. struct field_setter {
  12656. static constexpr int default_table_index
  12657. = meta::conditional_t < (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
  12658. || (std::is_integral_v<T> && !std::is_same_v<T, bool>) || (raw && std::is_void_v<std::remove_pointer_t<T>>),
  12659. std::integral_constant<int, -2>, std::integral_constant<int, -3> > ::value;
  12660. template <typename Key, typename Value>
  12661. void set(lua_State* L, Key&& key, Value&& value, int tableindex = default_table_index) {
  12662. if constexpr (std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, override_value_t>) {
  12663. (void)L;
  12664. (void)key;
  12665. (void)value;
  12666. (void)tableindex;
  12667. }
  12668. else if constexpr (std::is_same_v<T, metatable_key_t>) {
  12669. (void)key;
  12670. push(L, std::forward<Value>(value));
  12671. lua_setmetatable(L, tableindex);
  12672. }
  12673. else if constexpr (raw) {
  12674. if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  12675. push(L, std::forward<Value>(value));
  12676. lua_rawseti(L, tableindex, static_cast<lua_Integer>(key));
  12677. }
  12678. #if SOL_LUA_VESION_I_ >= 502
  12679. else if constexpr (std::is_void_v<std::remove_pointer_t<T>>) {
  12680. push(L, std::forward<Value>(value));
  12681. lua_rawsetp(L, tableindex, std::forward<Key>(key));
  12682. }
  12683. #endif // Lua 5.2.x
  12684. else {
  12685. push(L, std::forward<Key>(key));
  12686. push(L, std::forward<Value>(value));
  12687. lua_rawset(L, tableindex);
  12688. }
  12689. }
  12690. else {
  12691. if constexpr (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) {
  12692. if constexpr (global) {
  12693. push(L, std::forward<Value>(value));
  12694. lua_setglobal(L, &key[0]);
  12695. (void)tableindex;
  12696. }
  12697. else {
  12698. push(L, std::forward<Value>(value));
  12699. lua_setfield(L, tableindex, &key[0]);
  12700. }
  12701. }
  12702. #if SOL_LUA_VESION_I_ >= 503
  12703. else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
  12704. push(L, std::forward<Value>(value));
  12705. lua_seti(L, tableindex, static_cast<lua_Integer>(key));
  12706. }
  12707. #endif // Lua 5.3.x
  12708. else {
  12709. push(L, std::forward<Key>(key));
  12710. push(L, std::forward<Value>(value));
  12711. lua_settable(L, tableindex);
  12712. }
  12713. }
  12714. }
  12715. };
  12716. template <typename... Args, bool b, bool raw, typename C>
  12717. struct field_setter<std::tuple<Args...>, b, raw, C> {
  12718. template <bool g, std::size_t I, typename Keys, typename Value>
  12719. void apply(std::index_sequence<I>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  12720. I < 1 ? set_field<g, raw>(L, std::get<I>(std::forward<Keys>(keys)), std::forward<Value>(value), tableindex)
  12721. : set_field<g, raw>(L, std::get<I>(std::forward<Keys>(keys)), std::forward<Value>(value));
  12722. }
  12723. template <bool g, std::size_t I0, std::size_t I1, std::size_t... I, typename Keys, typename Value>
  12724. void apply(std::index_sequence<I0, I1, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  12725. I0 < 1 ? get_field<g, raw>(L, std::get<I0>(std::forward<Keys>(keys)), tableindex)
  12726. : get_field<g, raw>(L, std::get<I0>(std::forward<Keys>(keys)), -1);
  12727. apply<false>(std::index_sequence<I1, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), -1);
  12728. }
  12729. template <bool g, std::size_t I0, std::size_t... I, typename Keys, typename Value>
  12730. void top_apply(std::index_sequence<I0, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
  12731. apply<g>(std::index_sequence<I0, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
  12732. lua_pop(L, static_cast<int>(sizeof...(I)));
  12733. }
  12734. template <typename Keys, typename Value>
  12735. void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -3) {
  12736. top_apply<b>(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
  12737. }
  12738. };
  12739. template <typename A, typename B, bool b, bool raw, typename C>
  12740. struct field_setter<std::pair<A, B>, b, raw, C> {
  12741. template <typename Keys, typename Value>
  12742. void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -1) {
  12743. get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
  12744. set_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)), std::forward<Value>(value), lua_gettop(L));
  12745. lua_pop(L, 1);
  12746. }
  12747. };
  12748. }} // namespace sol::stack
  12749. // end of sol/stack_field.hpp
  12750. // beginning of sol/stack_probe.hpp
  12751. namespace sol {
  12752. namespace stack {
  12753. template <typename T, typename P, bool b, bool raw, typename>
  12754. struct probe_field_getter {
  12755. template <typename Key>
  12756. probe get(lua_State* L, Key&& key, int tableindex = -2) {
  12757. if constexpr(!b) {
  12758. if (!maybe_indexable(L, tableindex)) {
  12759. return probe(false, 0);
  12760. }
  12761. }
  12762. get_field<b, raw>(L, std::forward<Key>(key), tableindex);
  12763. return probe(check<P>(L), 1);
  12764. }
  12765. };
  12766. template <typename A, typename B, typename P, bool b, bool raw, typename C>
  12767. struct probe_field_getter<std::pair<A, B>, P, b, raw, C> {
  12768. template <typename Keys>
  12769. probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
  12770. if (!b && !maybe_indexable(L, tableindex)) {
  12771. return probe(false, 0);
  12772. }
  12773. get_field<b, raw>(L, std::get<0>(keys), tableindex);
  12774. if (!maybe_indexable(L)) {
  12775. return probe(false, 1);
  12776. }
  12777. get_field<false, raw>(L, std::get<1>(keys), tableindex);
  12778. return probe(check<P>(L), 2);
  12779. }
  12780. };
  12781. template <typename... Args, typename P, bool b, bool raw, typename C>
  12782. struct probe_field_getter<std::tuple<Args...>, P, b, raw, C> {
  12783. template <std::size_t I, typename Keys>
  12784. probe apply(std::index_sequence<I>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
  12785. get_field<(I<1) && b, raw>(L, std::get<I>(keys), tableindex);
  12786. return probe(check<P>(L), sofar);
  12787. }
  12788. template <std::size_t I, std::size_t I1, std::size_t... In, typename Keys>
  12789. probe apply(std::index_sequence<I, I1, In...>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
  12790. get_field < I<1 && b, raw>(L, std::get<I>(keys), tableindex);
  12791. if (!maybe_indexable(L)) {
  12792. return probe(false, sofar);
  12793. }
  12794. return apply(std::index_sequence<I1, In...>(), sofar + 1, L, std::forward<Keys>(keys), -1);
  12795. }
  12796. template <typename Keys>
  12797. probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
  12798. if constexpr (!b) {
  12799. if (!maybe_indexable(L, tableindex)) {
  12800. return probe(false, 0);
  12801. }
  12802. return apply(std::index_sequence_for<Args...>(), 1, L, std::forward<Keys>(keys), tableindex);
  12803. }
  12804. else {
  12805. return apply(std::index_sequence_for<Args...>(), 1, L, std::forward<Keys>(keys), tableindex);
  12806. }
  12807. }
  12808. };
  12809. }
  12810. } // namespace sol::stack
  12811. // end of sol/stack_probe.hpp
  12812. #include <cstring>
  12813. #include <array>
  12814. namespace sol {
  12815. namespace detail {
  12816. using typical_chunk_name_t = char[SOL_ID_SIZE_I_];
  12817. using typical_file_chunk_name_t = char[SOL_FILE_ID_SIZE_I_];
  12818. inline const std::string& default_chunk_name() {
  12819. static const std::string name = "";
  12820. return name;
  12821. }
  12822. template <std::size_t N>
  12823. const char* make_chunk_name(const string_view& code, const std::string& chunkname, char (&basechunkname)[N]) {
  12824. if (chunkname.empty()) {
  12825. auto it = code.cbegin();
  12826. auto e = code.cend();
  12827. std::size_t i = 0;
  12828. static const std::size_t n = N - 4;
  12829. for (i = 0; i < n && it != e; ++i, ++it) {
  12830. basechunkname[i] = *it;
  12831. }
  12832. if (it != e) {
  12833. for (std::size_t c = 0; c < 3; ++i, ++c) {
  12834. basechunkname[i] = '.';
  12835. }
  12836. }
  12837. basechunkname[i] = '\0';
  12838. return &basechunkname[0];
  12839. }
  12840. else {
  12841. return chunkname.c_str();
  12842. }
  12843. }
  12844. inline void clear_entries(stack_reference r) {
  12845. stack::push(r.lua_state(), lua_nil);
  12846. while (lua_next(r.lua_state(), -2)) {
  12847. absolute_index key(r.lua_state(), -2);
  12848. auto pn = stack::pop_n(r.lua_state(), 1);
  12849. stack::set_field<false, true>(r.lua_state(), key, lua_nil, r.stack_index());
  12850. }
  12851. }
  12852. inline void clear_entries(const reference& registry_reference) {
  12853. auto pp = stack::push_pop(registry_reference);
  12854. stack_reference ref(registry_reference.lua_state(), -1);
  12855. clear_entries(ref);
  12856. }
  12857. } // namespace detail
  12858. namespace stack {
  12859. namespace stack_detail {
  12860. template <typename T>
  12861. inline int push_as_upvalues(lua_State* L, T& item) {
  12862. typedef std::decay_t<T> TValue;
  12863. static const std::size_t itemsize = sizeof(TValue);
  12864. static const std::size_t voidsize = sizeof(void*);
  12865. static const std::size_t voidsizem1 = voidsize - 1;
  12866. static const std::size_t data_t_count = (sizeof(TValue) + voidsizem1) / voidsize;
  12867. typedef std::array<void*, data_t_count> data_t;
  12868. data_t data { {} };
  12869. std::memcpy(&data[0], std::addressof(item), itemsize);
  12870. int pushcount = 0;
  12871. for (const auto& v : data) {
  12872. lua_pushlightuserdata(L, v);
  12873. pushcount += 1;
  12874. }
  12875. return pushcount;
  12876. }
  12877. template <typename T>
  12878. inline std::pair<T, int> get_as_upvalues(lua_State* L, int index = 2) {
  12879. static const std::size_t data_t_count = (sizeof(T) + (sizeof(void*) - 1)) / sizeof(void*);
  12880. typedef std::array<void*, data_t_count> data_t;
  12881. data_t voiddata { {} };
  12882. for (std::size_t i = 0, d = 0; d < sizeof(T); ++i, d += sizeof(void*)) {
  12883. voiddata[i] = lua_touserdata(L, upvalue_index(index++));
  12884. }
  12885. return std::pair<T, int>(*reinterpret_cast<T*>(static_cast<void*>(voiddata.data())), index);
  12886. }
  12887. template <typename T>
  12888. inline std::pair<T, int> get_as_upvalues_using_function(lua_State* L, int function_index = -1) {
  12889. static const std::size_t data_t_count = (sizeof(T) + (sizeof(void*) - 1)) / sizeof(void*);
  12890. typedef std::array<void*, data_t_count> data_t;
  12891. function_index = lua_absindex(L, function_index);
  12892. int index = 0;
  12893. data_t voiddata { {} };
  12894. for (std::size_t d = 0; d < sizeof(T); d += sizeof(void*)) {
  12895. // first upvalue is nullptr to respect environment shenanigans
  12896. // So +2 instead of +1
  12897. const char* upvalue_name = lua_getupvalue(L, function_index, index + 2);
  12898. if (upvalue_name == nullptr) {
  12899. // We should freak out here...
  12900. break;
  12901. }
  12902. voiddata[index] = lua_touserdata(L, -1);
  12903. ++index;
  12904. }
  12905. lua_pop(L, index);
  12906. return std::pair<T, int>(*reinterpret_cast<T*>(static_cast<void*>(voiddata.data())), index);
  12907. }
  12908. template <typename Fx, typename... Args>
  12909. static decltype(auto) eval(types<>, std::index_sequence<>, lua_State*, int, record&, Fx&& fx, Args&&... args) {
  12910. return std::forward<Fx>(fx)(std::forward<Args>(args)...);
  12911. }
  12912. template <typename Fx, typename Arg, typename... Args, std::size_t I, std::size_t... Is, typename... FxArgs>
  12913. static decltype(auto) eval(
  12914. types<Arg, Args...>, std::index_sequence<I, Is...>, lua_State* L, int start, record& tracking, Fx&& fx, FxArgs&&... fxargs) {
  12915. return eval(types<Args...>(),
  12916. std::index_sequence<Is...>(),
  12917. L,
  12918. start,
  12919. tracking,
  12920. std::forward<Fx>(fx),
  12921. std::forward<FxArgs>(fxargs)...,
  12922. stack_detail::unchecked_get<Arg>(L, start + tracking.used, tracking));
  12923. }
  12924. template <bool checkargs = detail::default_safe_function_calls, std::size_t... I, typename R, typename... Args, typename Fx, typename... FxArgs>
  12925. inline decltype(auto) call(types<R>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
  12926. static_assert(meta::all<meta::is_not_move_only<Args>...>::value,
  12927. "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 "
  12928. "a reference and std::move it manually if this was your intention.");
  12929. if constexpr (checkargs) {
  12930. argument_handler<types<R, Args...>> handler {};
  12931. multi_check<Args...>(L, start, handler);
  12932. }
  12933. record tracking {};
  12934. if constexpr (std::is_void_v<R>) {
  12935. eval(ta, tai, L, start, tracking, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  12936. }
  12937. else {
  12938. return eval(ta, tai, L, start, tracking, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  12939. }
  12940. }
  12941. } // namespace stack_detail
  12942. template <typename T>
  12943. int set_ref(lua_State* L, T&& arg, int tableindex = -2) {
  12944. push(L, std::forward<T>(arg));
  12945. return luaL_ref(L, tableindex);
  12946. }
  12947. template <bool check_args = detail::default_safe_function_calls, typename R, typename... Args, typename Fx, typename... FxArgs>
  12948. inline decltype(auto) call(types<R> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
  12949. using args_indices = std::make_index_sequence<sizeof...(Args)>;
  12950. if constexpr (std::is_void_v<R>) {
  12951. stack_detail::call<check_args>(tr, ta, args_indices(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  12952. }
  12953. else {
  12954. return stack_detail::call<check_args>(tr, ta, args_indices(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  12955. }
  12956. }
  12957. template <bool check_args = detail::default_safe_function_calls, typename R, typename... Args, typename Fx, typename... FxArgs>
  12958. inline decltype(auto) call(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
  12959. if constexpr (std::is_void_v<R>) {
  12960. call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  12961. }
  12962. else {
  12963. return call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
  12964. }
  12965. }
  12966. template <bool check_args = detail::default_safe_function_calls, typename R, typename... Args, typename Fx, typename... FxArgs>
  12967. inline decltype(auto) call_from_top(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
  12968. using expected_count_t = meta::count_for_pack<lua_size, Args...>;
  12969. if constexpr (std::is_void_v<R>) {
  12970. call<check_args>(tr,
  12971. ta,
  12972. L,
  12973. (std::max)(static_cast<int>(lua_gettop(L) - expected_count_t::value), static_cast<int>(0)),
  12974. std::forward<Fx>(fx),
  12975. std::forward<FxArgs>(args)...);
  12976. }
  12977. else {
  12978. return call<check_args>(tr,
  12979. ta,
  12980. L,
  12981. (std::max)(static_cast<int>(lua_gettop(L) - expected_count_t::value), static_cast<int>(0)),
  12982. std::forward<Fx>(fx),
  12983. std::forward<FxArgs>(args)...);
  12984. }
  12985. }
  12986. template <bool check_args = detail::default_safe_function_calls, bool clean_stack = true, typename Ret0, typename... Ret, typename... Args,
  12987. typename Fx, typename... FxArgs>
  12988. inline int call_into_lua(types<Ret0, Ret...> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) {
  12989. if constexpr (std::is_void_v<Ret0>) {
  12990. call<check_args>(tr, ta, L, start, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)...);
  12991. if constexpr (clean_stack) {
  12992. lua_settop(L, 0);
  12993. }
  12994. return 0;
  12995. }
  12996. else {
  12997. (void)tr;
  12998. decltype(auto) r
  12999. = call<check_args>(types<meta::return_type_t<Ret0, Ret...>>(), ta, L, start, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)...);
  13000. using R = meta::unqualified_t<decltype(r)>;
  13001. using is_stack = meta::any<is_stack_based<R>, std::is_same<R, absolute_index>, std::is_same<R, ref_index>, std::is_same<R, raw_index>>;
  13002. if constexpr (clean_stack && !is_stack::value) {
  13003. lua_settop(L, 0);
  13004. }
  13005. return push_reference(L, std::forward<decltype(r)>(r));
  13006. }
  13007. }
  13008. template <bool check_args = detail::default_safe_function_calls, bool clean_stack = true, typename Fx, typename... FxArgs>
  13009. inline int call_lua(lua_State* L, int start, Fx&& fx, FxArgs&&... fxargs) {
  13010. using traits_type = lua_bind_traits<meta::unqualified_t<Fx>>;
  13011. using args_list = typename traits_type::args_list;
  13012. using returns_list = typename traits_type::returns_list;
  13013. return call_into_lua<check_args, clean_stack>(returns_list(), args_list(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(fxargs)...);
  13014. }
  13015. inline call_syntax get_call_syntax(lua_State* L, const string_view& key, int index) {
  13016. if (lua_gettop(L) < 1) {
  13017. return call_syntax::dot;
  13018. }
  13019. luaL_getmetatable(L, key.data());
  13020. auto pn = pop_n(L, 1);
  13021. if (lua_compare(L, -1, index, LUA_OPEQ) != 1) {
  13022. return call_syntax::dot;
  13023. }
  13024. return call_syntax::colon;
  13025. }
  13026. inline void script(
  13027. lua_State* L, lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  13028. detail::typical_chunk_name_t basechunkname = {};
  13029. const char* chunknametarget = detail::make_chunk_name("lua_Reader", chunkname, basechunkname);
  13030. if (lua_load(L, reader, data, chunknametarget, to_string(mode).c_str()) || lua_pcall(L, 0, LUA_MULTRET, 0)) {
  13031. lua_error(L);
  13032. }
  13033. }
  13034. inline void script(
  13035. lua_State* L, const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  13036. detail::typical_chunk_name_t basechunkname = {};
  13037. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  13038. if (luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()) || lua_pcall(L, 0, LUA_MULTRET, 0)) {
  13039. lua_error(L);
  13040. }
  13041. }
  13042. inline void script_file(lua_State* L, const std::string& filename, load_mode mode = load_mode::any) {
  13043. if (luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()) || lua_pcall(L, 0, LUA_MULTRET, 0)) {
  13044. lua_error(L);
  13045. }
  13046. }
  13047. inline void luajit_exception_handler(lua_State* L, int (*handler)(lua_State*, lua_CFunction) = detail::c_trampoline) {
  13048. #if SOL_IS_ON(SOL_USE_LUAJIT_EXCEPTION_TRAMPOLINE_I_)
  13049. if (L == nullptr) {
  13050. return;
  13051. }
  13052. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  13053. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  13054. #endif // make sure stack doesn't overflow
  13055. lua_pushlightuserdata(L, (void*)handler);
  13056. auto pn = pop_n(L, 1);
  13057. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
  13058. #else
  13059. (void)L;
  13060. (void)handler;
  13061. #endif
  13062. }
  13063. inline void luajit_exception_off(lua_State* L) {
  13064. #if SOL_IS_ON(SOL_USE_LUAJIT_EXCEPTION_TRAMPOLINE_I_)
  13065. if (L == nullptr) {
  13066. return;
  13067. }
  13068. luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_OFF);
  13069. #else
  13070. (void)L;
  13071. #endif
  13072. }
  13073. } // namespace stack
  13074. } // namespace sol
  13075. // end of sol/stack.hpp
  13076. // beginning of sol/object.hpp
  13077. // beginning of sol/make_reference.hpp
  13078. namespace sol {
  13079. template <typename R = reference, bool should_pop = !is_stack_based_v<R>, typename T>
  13080. R make_reference(lua_State* L, T&& value) {
  13081. int backpedal = stack::push(L, std::forward<T>(value));
  13082. R r = stack::get<R>(L, -backpedal);
  13083. if (should_pop) {
  13084. lua_pop(L, backpedal);
  13085. }
  13086. return r;
  13087. }
  13088. template <typename T, typename R = reference, bool should_pop = !is_stack_based_v<R>, typename... Args>
  13089. R make_reference(lua_State* L, Args&&... args) {
  13090. int backpedal = stack::push<T>(L, std::forward<Args>(args)...);
  13091. R r = stack::get<R>(L, -backpedal);
  13092. if (should_pop) {
  13093. lua_pop(L, backpedal);
  13094. }
  13095. return r;
  13096. }
  13097. template <typename R = reference, bool should_pop = !is_stack_based_v<R>, typename T>
  13098. R make_reference_userdata(lua_State* L, T&& value) {
  13099. int backpedal = stack::push_userdata(L, std::forward<T>(value));
  13100. R r = stack::get<R>(L, -backpedal);
  13101. if (should_pop) {
  13102. lua_pop(L, backpedal);
  13103. }
  13104. return r;
  13105. }
  13106. template <typename T, typename R = reference, bool should_pop = !is_stack_based_v<R>, typename... Args>
  13107. R make_reference_userdata(lua_State* L, Args&&... args) {
  13108. int backpedal = stack::push_userdata<T>(L, std::forward<Args>(args)...);
  13109. R r = stack::get<R>(L, -backpedal);
  13110. if (should_pop) {
  13111. lua_pop(L, backpedal);
  13112. }
  13113. return r;
  13114. }
  13115. } // namespace sol
  13116. // end of sol/make_reference.hpp
  13117. // beginning of sol/object_base.hpp
  13118. namespace sol {
  13119. template <typename ref_t>
  13120. class basic_object_base : public ref_t {
  13121. private:
  13122. using base_t = ref_t;
  13123. template <typename T>
  13124. decltype(auto) as_stack(std::true_type) const {
  13125. return stack::get<T>(base_t::lua_state(), base_t::stack_index());
  13126. }
  13127. template <typename T>
  13128. decltype(auto) as_stack(std::false_type) const {
  13129. base_t::push();
  13130. return stack::pop<T>(base_t::lua_state());
  13131. }
  13132. template <typename T>
  13133. bool is_stack(std::true_type) const {
  13134. return stack::check<T>(base_t::lua_state(), base_t::stack_index(), no_panic);
  13135. }
  13136. template <typename T>
  13137. bool is_stack(std::false_type) const {
  13138. int r = base_t::registry_index();
  13139. if (r == LUA_REFNIL)
  13140. return meta::any_same<meta::unqualified_t<T>, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false;
  13141. if (r == LUA_NOREF)
  13142. return false;
  13143. auto pp = stack::push_pop(*this);
  13144. return stack::check<T>(base_t::lua_state(), -1, no_panic);
  13145. }
  13146. public:
  13147. basic_object_base() noexcept = default;
  13148. basic_object_base(const basic_object_base&) = default;
  13149. basic_object_base(basic_object_base&&) = default;
  13150. basic_object_base& operator=(const basic_object_base&) = default;
  13151. basic_object_base& operator=(basic_object_base&&) = default;
  13152. template <typename T, typename... Args, meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_object_base>>> = meta::enabler>
  13153. basic_object_base(T&& arg, Args&&... args)
  13154. : base_t(std::forward<T>(arg), std::forward<Args>(args)...) {
  13155. }
  13156. template <typename T>
  13157. decltype(auto) as() const {
  13158. return as_stack<T>(is_stack_based<base_t>());
  13159. }
  13160. template <typename T>
  13161. bool is() const {
  13162. return is_stack<T>(is_stack_based<base_t>());
  13163. }
  13164. };
  13165. } // namespace sol
  13166. // end of sol/object_base.hpp
  13167. namespace sol {
  13168. template <typename base_type>
  13169. class basic_object : public basic_object_base<base_type> {
  13170. private:
  13171. typedef basic_object_base<base_type> base_t;
  13172. template <bool invert_and_pop = false>
  13173. basic_object(std::integral_constant<bool, invert_and_pop>, lua_State* L, int index = -1) noexcept
  13174. : base_t(L, index) {
  13175. if (invert_and_pop) {
  13176. lua_pop(L, -index);
  13177. }
  13178. }
  13179. protected:
  13180. basic_object(detail::no_safety_tag, lua_nil_t n) : base_t(n) {
  13181. }
  13182. basic_object(detail::no_safety_tag, lua_State* L, int index) : base_t(L, index) {
  13183. }
  13184. basic_object(lua_State* L, detail::global_tag t) : base_t(L, t) {
  13185. }
  13186. basic_object(detail::no_safety_tag, lua_State* L, ref_index index) : base_t(L, index) {
  13187. }
  13188. template <typename T,
  13189. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_object>>, meta::neg<std::is_same<base_type, stack_reference>>,
  13190. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13191. basic_object(detail::no_safety_tag, T&& r) noexcept : base_t(std::forward<T>(r)) {
  13192. }
  13193. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13194. basic_object(detail::no_safety_tag, lua_State* L, T&& r) noexcept : base_t(L, std::forward<T>(r)) {
  13195. }
  13196. public:
  13197. basic_object() noexcept = default;
  13198. 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>
  13199. basic_object(T&& r)
  13200. : base_t(std::forward<T>(r)) {
  13201. }
  13202. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  13203. basic_object(lua_State* L, T&& r)
  13204. : base_t(L, std::forward<T>(r)) {
  13205. }
  13206. basic_object(lua_nil_t r)
  13207. : base_t(r) {
  13208. }
  13209. basic_object(const basic_object&) = default;
  13210. basic_object(basic_object&&) = default;
  13211. basic_object(const stack_reference& r) noexcept
  13212. : basic_object(r.lua_state(), r.stack_index()) {
  13213. }
  13214. basic_object(stack_reference&& r) noexcept
  13215. : basic_object(r.lua_state(), r.stack_index()) {
  13216. }
  13217. template <typename Super>
  13218. basic_object(const proxy_base<Super>& r) noexcept
  13219. : basic_object(r.operator basic_object()) {
  13220. }
  13221. template <typename Super>
  13222. basic_object(proxy_base<Super>&& r) noexcept
  13223. : basic_object(r.operator basic_object()) {
  13224. }
  13225. basic_object(lua_State* L, lua_nil_t r) noexcept
  13226. : base_t(L, r) {
  13227. }
  13228. basic_object(lua_State* L, int index = -1) noexcept
  13229. : base_t(L, index) {
  13230. }
  13231. basic_object(lua_State* L, absolute_index index) noexcept
  13232. : base_t(L, index) {
  13233. }
  13234. basic_object(lua_State* L, raw_index index) noexcept
  13235. : base_t(L, index) {
  13236. }
  13237. basic_object(lua_State* L, ref_index index) noexcept
  13238. : base_t(L, index) {
  13239. }
  13240. template <typename T, typename... Args>
  13241. basic_object(lua_State* L, in_place_type_t<T>, Args&&... args) noexcept
  13242. : basic_object(std::integral_constant<bool, !is_stack_based<base_t>::value>(), L, -stack::push<T>(L, std::forward<Args>(args)...)) {
  13243. }
  13244. template <typename T, typename... Args>
  13245. basic_object(lua_State* L, in_place_t, T&& arg, Args&&... args) noexcept
  13246. : basic_object(L, in_place_type<T>, std::forward<T>(arg), std::forward<Args>(args)...) {
  13247. }
  13248. basic_object& operator=(const basic_object&) = default;
  13249. basic_object& operator=(basic_object&&) = default;
  13250. basic_object& operator=(const base_type& b) {
  13251. base_t::operator=(b);
  13252. return *this;
  13253. }
  13254. basic_object& operator=(base_type&& b) {
  13255. base_t::operator=(std::move(b));
  13256. return *this;
  13257. }
  13258. template <typename Super>
  13259. basic_object& operator=(const proxy_base<Super>& r) {
  13260. this->operator=(r.operator basic_object());
  13261. return *this;
  13262. }
  13263. template <typename Super>
  13264. basic_object& operator=(proxy_base<Super>&& r) {
  13265. this->operator=(r.operator basic_object());
  13266. return *this;
  13267. }
  13268. };
  13269. template <typename T>
  13270. object make_object(lua_State* L, T&& value) {
  13271. return make_reference<object, true>(L, std::forward<T>(value));
  13272. }
  13273. template <typename T, typename... Args>
  13274. object make_object(lua_State* L, Args&&... args) {
  13275. return make_reference<T, object, true>(L, std::forward<Args>(args)...);
  13276. }
  13277. template <typename T>
  13278. object make_object_userdata(lua_State* L, T&& value) {
  13279. return make_reference_userdata<object, true>(L, std::forward<T>(value));
  13280. }
  13281. template <typename T, typename... Args>
  13282. object make_object_userdata(lua_State* L, Args&&... args) {
  13283. return make_reference_userdata<T, object, true>(L, std::forward<Args>(args)...);
  13284. }
  13285. } // namespace sol
  13286. // end of sol/object.hpp
  13287. // beginning of sol/function.hpp
  13288. // beginning of sol/unsafe_function.hpp
  13289. // beginning of sol/function_result.hpp
  13290. // beginning of sol/protected_function_result.hpp
  13291. // beginning of sol/proxy_base.hpp
  13292. namespace sol {
  13293. struct proxy_base_tag {};
  13294. namespace detail {
  13295. template <typename T>
  13296. using proxy_key_t = meta::conditional_t<meta::is_specialization_of_v<meta::unqualified_t<T>, std::tuple>, T,
  13297. std::tuple<meta::conditional_t<std::is_array_v<meta::unqualified_t<T>>, std::remove_reference_t<T>&, meta::unqualified_t<T>>>>;
  13298. }
  13299. template <typename Super>
  13300. struct proxy_base : proxy_base_tag {
  13301. operator std::string() const {
  13302. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  13303. return super.template get<std::string>();
  13304. }
  13305. template <typename T, meta::enable<meta::neg<meta::is_string_constructible<T>>, is_proxy_primitive<meta::unqualified_t<T>>> = meta::enabler>
  13306. operator T() const {
  13307. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  13308. return super.template get<T>();
  13309. }
  13310. template <typename T, meta::enable<meta::neg<meta::is_string_constructible<T>>, meta::neg<is_proxy_primitive<meta::unqualified_t<T>>>> = meta::enabler>
  13311. operator T&() const {
  13312. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  13313. return super.template get<T&>();
  13314. }
  13315. lua_State* lua_state() const {
  13316. const Super& super = *static_cast<const Super*>(static_cast<const void*>(this));
  13317. return super.lua_state();
  13318. }
  13319. };
  13320. } // namespace sol
  13321. // end of sol/proxy_base.hpp
  13322. // beginning of sol/stack_iterator.hpp
  13323. #include <limits>
  13324. #include <iterator>
  13325. namespace sol {
  13326. template <typename proxy_t, bool is_const>
  13327. struct stack_iterator {
  13328. typedef meta::conditional_t<is_const, const proxy_t, proxy_t> reference;
  13329. typedef meta::conditional_t<is_const, const proxy_t*, proxy_t*> pointer;
  13330. typedef proxy_t value_type;
  13331. typedef std::ptrdiff_t difference_type;
  13332. typedef std::random_access_iterator_tag iterator_category;
  13333. lua_State* L;
  13334. int index;
  13335. int stacktop;
  13336. proxy_t sp;
  13337. stack_iterator()
  13338. : L(nullptr), index((std::numeric_limits<int>::max)()), stacktop((std::numeric_limits<int>::max)()), sp() {
  13339. }
  13340. stack_iterator(const stack_iterator<proxy_t, true>& r)
  13341. : L(r.L), index(r.index), stacktop(r.stacktop), sp(r.sp) {
  13342. }
  13343. stack_iterator(lua_State* luastate, int idx, int topidx)
  13344. : L(luastate), index(idx), stacktop(topidx), sp(luastate, idx) {
  13345. }
  13346. reference operator*() {
  13347. return proxy_t(L, index);
  13348. }
  13349. reference operator*() const {
  13350. return proxy_t(L, index);
  13351. }
  13352. pointer operator->() {
  13353. sp = proxy_t(L, index);
  13354. return &sp;
  13355. }
  13356. pointer operator->() const {
  13357. const_cast<proxy_t&>(sp) = proxy_t(L, index);
  13358. return &sp;
  13359. }
  13360. stack_iterator& operator++() {
  13361. ++index;
  13362. return *this;
  13363. }
  13364. stack_iterator operator++(int) {
  13365. auto r = *this;
  13366. this->operator++();
  13367. return r;
  13368. }
  13369. stack_iterator& operator--() {
  13370. --index;
  13371. return *this;
  13372. }
  13373. stack_iterator operator--(int) {
  13374. auto r = *this;
  13375. this->operator--();
  13376. return r;
  13377. }
  13378. stack_iterator& operator+=(difference_type idx) {
  13379. index += static_cast<int>(idx);
  13380. return *this;
  13381. }
  13382. stack_iterator& operator-=(difference_type idx) {
  13383. index -= static_cast<int>(idx);
  13384. return *this;
  13385. }
  13386. difference_type operator-(const stack_iterator& r) const {
  13387. return index - r.index;
  13388. }
  13389. stack_iterator operator+(difference_type idx) const {
  13390. stack_iterator r = *this;
  13391. r += idx;
  13392. return r;
  13393. }
  13394. reference operator[](difference_type idx) const {
  13395. return proxy_t(L, index + static_cast<int>(idx));
  13396. }
  13397. bool operator==(const stack_iterator& r) const {
  13398. if (stacktop == (std::numeric_limits<int>::max)()) {
  13399. return r.index == r.stacktop;
  13400. }
  13401. else if (r.stacktop == (std::numeric_limits<int>::max)()) {
  13402. return index == stacktop;
  13403. }
  13404. return index == r.index;
  13405. }
  13406. bool operator!=(const stack_iterator& r) const {
  13407. return !(this->operator==(r));
  13408. }
  13409. bool operator<(const stack_iterator& r) const {
  13410. return index < r.index;
  13411. }
  13412. bool operator>(const stack_iterator& r) const {
  13413. return index > r.index;
  13414. }
  13415. bool operator<=(const stack_iterator& r) const {
  13416. return index <= r.index;
  13417. }
  13418. bool operator>=(const stack_iterator& r) const {
  13419. return index >= r.index;
  13420. }
  13421. };
  13422. template <typename proxy_t, bool is_const>
  13423. 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) {
  13424. return r + n;
  13425. }
  13426. } // namespace sol
  13427. // end of sol/stack_iterator.hpp
  13428. // beginning of sol/stack_proxy.hpp
  13429. // beginning of sol/stack_proxy_base.hpp
  13430. namespace sol {
  13431. struct stack_proxy_base : public proxy_base<stack_proxy_base> {
  13432. private:
  13433. lua_State* L;
  13434. int index;
  13435. public:
  13436. stack_proxy_base()
  13437. : L(nullptr), index(0) {
  13438. }
  13439. stack_proxy_base(lua_State* L, int index)
  13440. : L(L), index(index) {
  13441. }
  13442. template <typename T>
  13443. decltype(auto) get() const {
  13444. return stack::get<T>(L, stack_index());
  13445. }
  13446. template <typename T>
  13447. bool is() const {
  13448. return stack::check<T>(L, stack_index());
  13449. }
  13450. template <typename T>
  13451. decltype(auto) as() const {
  13452. return get<T>();
  13453. }
  13454. type get_type() const noexcept {
  13455. return type_of(lua_state(), stack_index());
  13456. }
  13457. int push() const {
  13458. return push(L);
  13459. }
  13460. int push(lua_State* Ls) const {
  13461. lua_pushvalue(Ls, index);
  13462. return 1;
  13463. }
  13464. lua_State* lua_state() const {
  13465. return L;
  13466. }
  13467. int stack_index() const {
  13468. return index;
  13469. }
  13470. };
  13471. namespace stack {
  13472. template <>
  13473. struct unqualified_getter<stack_proxy_base> {
  13474. static stack_proxy_base get(lua_State* L, int index = -1) {
  13475. return stack_proxy_base(L, index);
  13476. }
  13477. };
  13478. template <>
  13479. struct unqualified_pusher<stack_proxy_base> {
  13480. static int push(lua_State*, const stack_proxy_base& ref) {
  13481. return ref.push();
  13482. }
  13483. };
  13484. } // namespace stack
  13485. } // namespace sol
  13486. // end of sol/stack_proxy_base.hpp
  13487. namespace sol {
  13488. struct stack_proxy : public stack_proxy_base {
  13489. public:
  13490. stack_proxy() : stack_proxy_base() {
  13491. }
  13492. stack_proxy(lua_State* L, int index) : stack_proxy_base(L, index) {
  13493. }
  13494. template <typename... Ret, typename... Args>
  13495. decltype(auto) call(Args&&... args);
  13496. template <typename... Args>
  13497. decltype(auto) operator()(Args&&... args) {
  13498. return call<>(std::forward<Args>(args)...);
  13499. }
  13500. };
  13501. namespace stack {
  13502. template <>
  13503. struct unqualified_getter<stack_proxy> {
  13504. static stack_proxy get(lua_State* L, int index, record& tracking) {
  13505. tracking.use(0);
  13506. return stack_proxy(L, index);
  13507. }
  13508. };
  13509. template <>
  13510. struct unqualified_pusher<stack_proxy> {
  13511. static int push(lua_State*, const stack_proxy& ref) {
  13512. return ref.push();
  13513. }
  13514. };
  13515. } // namespace stack
  13516. } // namespace sol
  13517. // end of sol/stack_proxy.hpp
  13518. #include <cstdint>
  13519. namespace sol {
  13520. struct protected_function_result : public proxy_base<protected_function_result> {
  13521. private:
  13522. lua_State* L;
  13523. int index;
  13524. int returncount;
  13525. int popcount;
  13526. call_status err;
  13527. public:
  13528. typedef stack_proxy reference_type;
  13529. typedef stack_proxy value_type;
  13530. typedef stack_proxy* pointer;
  13531. typedef std::ptrdiff_t difference_type;
  13532. typedef std::size_t size_type;
  13533. typedef stack_iterator<stack_proxy, false> iterator;
  13534. typedef stack_iterator<stack_proxy, true> const_iterator;
  13535. typedef std::reverse_iterator<iterator> reverse_iterator;
  13536. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  13537. protected_function_result() noexcept = default;
  13538. protected_function_result(lua_State* Ls, int idx = -1, int retnum = 0, int popped = 0, call_status pferr = call_status::ok) noexcept
  13539. : L(Ls), index(idx), returncount(retnum), popcount(popped), err(pferr) {
  13540. }
  13541. // We do not want anyone to copy these around willy-nilly
  13542. // Will likely break people, but also will probably get rid of quiet bugs that have
  13543. // been lurking. (E.g., Vanilla Lua will just quietly discard over-pops and under-pops:
  13544. // LuaJIT and other Lua engines will implode and segfault at random later times.)
  13545. protected_function_result(const protected_function_result&) = delete;
  13546. protected_function_result& operator=(const protected_function_result&) = delete;
  13547. protected_function_result(protected_function_result&& o) noexcept
  13548. : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) {
  13549. // Must be manual, otherwise destructor will screw us
  13550. // return count being 0 is enough to keep things clean
  13551. // but we will be thorough
  13552. o.abandon();
  13553. }
  13554. protected_function_result& operator=(protected_function_result&& o) noexcept {
  13555. L = o.L;
  13556. index = o.index;
  13557. returncount = o.returncount;
  13558. popcount = o.popcount;
  13559. err = o.err;
  13560. // Must be manual, otherwise destructor will screw us
  13561. // return count being 0 is enough to keep things clean
  13562. // but we will be thorough
  13563. o.abandon();
  13564. return *this;
  13565. }
  13566. protected_function_result(const unsafe_function_result& o) = delete;
  13567. protected_function_result& operator=(const unsafe_function_result& o) = delete;
  13568. protected_function_result(unsafe_function_result&& o) noexcept;
  13569. protected_function_result& operator=(unsafe_function_result&& o) noexcept;
  13570. call_status status() const noexcept {
  13571. return err;
  13572. }
  13573. bool valid() const noexcept {
  13574. return status() == call_status::ok || status() == call_status::yielded;
  13575. }
  13576. template <typename T>
  13577. decltype(auto) get(int index_offset = 0) const {
  13578. using UT = meta::unqualified_t<T>;
  13579. int target = index + index_offset;
  13580. if constexpr (meta::is_optional_v<UT>) {
  13581. using ValueType = typename UT::value_type;
  13582. if constexpr (std::is_same_v<ValueType, error>) {
  13583. if (valid()) {
  13584. return UT();
  13585. }
  13586. return UT(error(detail::direct_error, stack::get<std::string>(L, target)));
  13587. }
  13588. else {
  13589. if (!valid()) {
  13590. return UT();
  13591. }
  13592. return stack::get<UT>(L, target);
  13593. }
  13594. }
  13595. else {
  13596. if constexpr (std::is_same_v<T, error>) {
  13597. #if SOL_IS_ON(SOL_SAFE_PROXIES_I_)
  13598. if (valid()) {
  13599. type t = type_of(L, target);
  13600. type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is an error)");
  13601. }
  13602. #endif // Check Argument Safety
  13603. return error(detail::direct_error, stack::get<std::string>(L, target));
  13604. }
  13605. else {
  13606. #if SOL_IS_ON(SOL_SAFE_PROXIES_I_)
  13607. if (!valid()) {
  13608. type t = type_of(L, target);
  13609. type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is not an error)");
  13610. }
  13611. #endif // Check Argument Safety
  13612. return stack::get<T>(L, target);
  13613. }
  13614. }
  13615. }
  13616. type get_type(int index_offset = 0) const noexcept {
  13617. return type_of(L, index + static_cast<int>(index_offset));
  13618. }
  13619. stack_proxy operator[](difference_type index_offset) const {
  13620. return stack_proxy(L, index + static_cast<int>(index_offset));
  13621. }
  13622. iterator begin() {
  13623. return iterator(L, index, stack_index() + return_count());
  13624. }
  13625. iterator end() {
  13626. return iterator(L, stack_index() + return_count(), stack_index() + return_count());
  13627. }
  13628. const_iterator begin() const {
  13629. return const_iterator(L, index, stack_index() + return_count());
  13630. }
  13631. const_iterator end() const {
  13632. return const_iterator(L, stack_index() + return_count(), stack_index() + return_count());
  13633. }
  13634. const_iterator cbegin() const {
  13635. return begin();
  13636. }
  13637. const_iterator cend() const {
  13638. return end();
  13639. }
  13640. reverse_iterator rbegin() {
  13641. return std::reverse_iterator<iterator>(begin());
  13642. }
  13643. reverse_iterator rend() {
  13644. return std::reverse_iterator<iterator>(end());
  13645. }
  13646. const_reverse_iterator rbegin() const {
  13647. return std::reverse_iterator<const_iterator>(begin());
  13648. }
  13649. const_reverse_iterator rend() const {
  13650. return std::reverse_iterator<const_iterator>(end());
  13651. }
  13652. const_reverse_iterator crbegin() const {
  13653. return std::reverse_iterator<const_iterator>(cbegin());
  13654. }
  13655. const_reverse_iterator crend() const {
  13656. return std::reverse_iterator<const_iterator>(cend());
  13657. }
  13658. lua_State* lua_state() const noexcept {
  13659. return L;
  13660. };
  13661. int stack_index() const noexcept {
  13662. return index;
  13663. };
  13664. int return_count() const noexcept {
  13665. return returncount;
  13666. };
  13667. int pop_count() const noexcept {
  13668. return popcount;
  13669. };
  13670. void abandon() noexcept {
  13671. // L = nullptr;
  13672. index = 0;
  13673. returncount = 0;
  13674. popcount = 0;
  13675. err = call_status::runtime;
  13676. }
  13677. ~protected_function_result() {
  13678. stack::remove(L, index, popcount);
  13679. }
  13680. };
  13681. namespace stack {
  13682. template <>
  13683. struct unqualified_pusher<protected_function_result> {
  13684. static int push(lua_State* L, const protected_function_result& pfr) {
  13685. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  13686. luaL_checkstack(L, static_cast<int>(pfr.pop_count()), detail::not_enough_stack_space_generic);
  13687. #endif // make sure stack doesn't overflow
  13688. int p = 0;
  13689. for (int i = 0; i < pfr.pop_count(); ++i) {
  13690. lua_pushvalue(L, i + pfr.stack_index());
  13691. ++p;
  13692. }
  13693. return p;
  13694. }
  13695. };
  13696. } // namespace stack
  13697. } // namespace sol
  13698. // end of sol/protected_function_result.hpp
  13699. // beginning of sol/unsafe_function_result.hpp
  13700. #include <cstdint>
  13701. namespace sol {
  13702. struct unsafe_function_result : public proxy_base<unsafe_function_result> {
  13703. private:
  13704. lua_State* L;
  13705. int index;
  13706. int returncount;
  13707. public:
  13708. typedef stack_proxy reference_type;
  13709. typedef stack_proxy value_type;
  13710. typedef stack_proxy* pointer;
  13711. typedef std::ptrdiff_t difference_type;
  13712. typedef std::size_t size_type;
  13713. typedef stack_iterator<stack_proxy, false> iterator;
  13714. typedef stack_iterator<stack_proxy, true> const_iterator;
  13715. typedef std::reverse_iterator<iterator> reverse_iterator;
  13716. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  13717. unsafe_function_result() noexcept = default;
  13718. unsafe_function_result(lua_State* Ls, int idx = -1, int retnum = 0) noexcept : L(Ls), index(idx), returncount(retnum) {
  13719. }
  13720. // We do not want anyone to copy these around willy-nilly
  13721. // Will likely break people, but also will probably get rid of quiet bugs that have
  13722. // been lurking. (E.g., Vanilla Lua will just quietly discard over-pops and under-pops:
  13723. // LuaJIT and other Lua engines will implode and segfault at random later times.)
  13724. unsafe_function_result(const unsafe_function_result&) = delete;
  13725. unsafe_function_result& operator=(const unsafe_function_result&) = delete;
  13726. unsafe_function_result(unsafe_function_result&& o) noexcept : L(o.L), index(o.index), returncount(o.returncount) {
  13727. // Must be manual, otherwise destructor will screw us
  13728. // return count being 0 is enough to keep things clean
  13729. // but will be thorough
  13730. o.abandon();
  13731. }
  13732. unsafe_function_result& operator=(unsafe_function_result&& o) noexcept {
  13733. L = o.L;
  13734. index = o.index;
  13735. returncount = o.returncount;
  13736. // Must be manual, otherwise destructor will screw us
  13737. // return count being 0 is enough to keep things clean
  13738. // but will be thorough
  13739. o.abandon();
  13740. return *this;
  13741. }
  13742. unsafe_function_result(const protected_function_result& o) = delete;
  13743. unsafe_function_result& operator=(const protected_function_result& o) = delete;
  13744. unsafe_function_result(protected_function_result&& o) noexcept;
  13745. unsafe_function_result& operator=(protected_function_result&& o) noexcept;
  13746. template <typename T>
  13747. decltype(auto) get(difference_type index_offset = 0) const {
  13748. return stack::get<T>(L, index + static_cast<int>(index_offset));
  13749. }
  13750. type get_type(difference_type index_offset = 0) const noexcept {
  13751. return type_of(L, index + static_cast<int>(index_offset));
  13752. }
  13753. stack_proxy operator[](difference_type index_offset) const {
  13754. return stack_proxy(L, index + static_cast<int>(index_offset));
  13755. }
  13756. iterator begin() {
  13757. return iterator(L, index, stack_index() + return_count());
  13758. }
  13759. iterator end() {
  13760. return iterator(L, stack_index() + return_count(), stack_index() + return_count());
  13761. }
  13762. const_iterator begin() const {
  13763. return const_iterator(L, index, stack_index() + return_count());
  13764. }
  13765. const_iterator end() const {
  13766. return const_iterator(L, stack_index() + return_count(), stack_index() + return_count());
  13767. }
  13768. const_iterator cbegin() const {
  13769. return begin();
  13770. }
  13771. const_iterator cend() const {
  13772. return end();
  13773. }
  13774. reverse_iterator rbegin() {
  13775. return std::reverse_iterator<iterator>(begin());
  13776. }
  13777. reverse_iterator rend() {
  13778. return std::reverse_iterator<iterator>(end());
  13779. }
  13780. const_reverse_iterator rbegin() const {
  13781. return std::reverse_iterator<const_iterator>(begin());
  13782. }
  13783. const_reverse_iterator rend() const {
  13784. return std::reverse_iterator<const_iterator>(end());
  13785. }
  13786. const_reverse_iterator crbegin() const {
  13787. return std::reverse_iterator<const_iterator>(cbegin());
  13788. }
  13789. const_reverse_iterator crend() const {
  13790. return std::reverse_iterator<const_iterator>(cend());
  13791. }
  13792. call_status status() const noexcept {
  13793. return call_status::ok;
  13794. }
  13795. bool valid() const noexcept {
  13796. return status() == call_status::ok || status() == call_status::yielded;
  13797. }
  13798. lua_State* lua_state() const {
  13799. return L;
  13800. };
  13801. int stack_index() const {
  13802. return index;
  13803. };
  13804. int return_count() const {
  13805. return returncount;
  13806. };
  13807. void abandon() noexcept {
  13808. // L = nullptr;
  13809. index = 0;
  13810. returncount = 0;
  13811. }
  13812. ~unsafe_function_result() {
  13813. lua_pop(L, returncount);
  13814. }
  13815. };
  13816. namespace stack {
  13817. template <>
  13818. struct unqualified_pusher<unsafe_function_result> {
  13819. static int push(lua_State* L, const unsafe_function_result& fr) {
  13820. int p = 0;
  13821. for (int i = 0; i < fr.return_count(); ++i) {
  13822. lua_pushvalue(L, i + fr.stack_index());
  13823. ++p;
  13824. }
  13825. return p;
  13826. }
  13827. };
  13828. } // namespace stack
  13829. } // namespace sol
  13830. // end of sol/unsafe_function_result.hpp
  13831. #include <cstdint>
  13832. namespace sol {
  13833. namespace detail {
  13834. template <>
  13835. struct is_speshul<unsafe_function_result> : std::true_type {};
  13836. template <>
  13837. struct is_speshul<protected_function_result> : std::true_type {};
  13838. template <std::size_t I, typename... Args, typename T>
  13839. stack_proxy get(types<Args...>, meta::index_value<0>, meta::index_value<I>, const T& fr) {
  13840. return stack_proxy(fr.lua_state(), static_cast<int>(fr.stack_index() + I));
  13841. }
  13842. template <std::size_t I, std::size_t N, typename Arg, typename... Args, typename T, meta::enable<meta::boolean<(N > 0)>> = meta::enabler>
  13843. stack_proxy get(types<Arg, Args...>, meta::index_value<N>, meta::index_value<I>, const T& fr) {
  13844. return get(types<Args...>(), meta::index_value<N - 1>(), meta::index_value<I + lua_size<Arg>::value>(), fr);
  13845. }
  13846. } // namespace detail
  13847. template <>
  13848. struct tie_size<unsafe_function_result> : std::integral_constant<std::size_t, SIZE_MAX> {};
  13849. template <>
  13850. struct tie_size<protected_function_result> : std::integral_constant<std::size_t, SIZE_MAX> {};
  13851. template <std::size_t I>
  13852. stack_proxy get(const unsafe_function_result& fr) {
  13853. return stack_proxy(fr.lua_state(), static_cast<int>(fr.stack_index() + I));
  13854. }
  13855. template <std::size_t I, typename... Args>
  13856. stack_proxy get(types<Args...> t, const unsafe_function_result& fr) {
  13857. return detail::get(t, meta::index_value<I>(), meta::index_value<0>(), fr);
  13858. }
  13859. template <std::size_t I>
  13860. stack_proxy get(const protected_function_result& fr) {
  13861. return stack_proxy(fr.lua_state(), static_cast<int>(fr.stack_index() + I));
  13862. }
  13863. template <std::size_t I, typename... Args>
  13864. stack_proxy get(types<Args...> t, const protected_function_result& fr) {
  13865. return detail::get(t, meta::index_value<I>(), meta::index_value<0>(), fr);
  13866. }
  13867. } // namespace sol
  13868. // end of sol/function_result.hpp
  13869. // beginning of sol/function_types.hpp
  13870. // beginning of sol/function_types_core.hpp
  13871. // beginning of sol/wrapper.hpp
  13872. namespace sol {
  13873. namespace detail {
  13874. template <typename T>
  13875. using array_return_type = meta::conditional_t<std::is_array<T>::value, std::add_lvalue_reference_t<T>, T>;
  13876. }
  13877. template <typename F, typename = void>
  13878. struct wrapper {
  13879. typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
  13880. typedef typename traits_type::args_list args_list;
  13881. typedef typename traits_type::args_list free_args_list;
  13882. typedef typename traits_type::returns_list returns_list;
  13883. template <typename... Args>
  13884. static decltype(auto) call(F& f, Args&&... args) {
  13885. return f(std::forward<Args>(args)...);
  13886. }
  13887. struct caller {
  13888. template <typename... Args>
  13889. decltype(auto) operator()(F& fx, Args&&... args) const {
  13890. return call(fx, std::forward<Args>(args)...);
  13891. }
  13892. };
  13893. };
  13894. template <typename F>
  13895. struct wrapper<F, std::enable_if_t<std::is_function<std::remove_pointer_t<meta::unqualified_t<F>>>::value>> {
  13896. typedef lua_bind_traits<std::remove_pointer_t<meta::unqualified_t<F>>> traits_type;
  13897. typedef typename traits_type::args_list args_list;
  13898. typedef typename traits_type::args_list free_args_list;
  13899. typedef typename traits_type::returns_list returns_list;
  13900. template <F fx, typename... Args>
  13901. static decltype(auto) invoke(Args&&... args) {
  13902. return fx(std::forward<Args>(args)...);
  13903. }
  13904. template <typename... Args>
  13905. static decltype(auto) call(F& fx, Args&&... args) {
  13906. return fx(std::forward<Args>(args)...);
  13907. }
  13908. struct caller {
  13909. template <typename... Args>
  13910. decltype(auto) operator()(F& fx, Args&&... args) const {
  13911. return call(fx, std::forward<Args>(args)...);
  13912. }
  13913. };
  13914. template <F fx>
  13915. struct invoker {
  13916. template <typename... Args>
  13917. decltype(auto) operator()(Args&&... args) const {
  13918. return invoke<fx>(std::forward<Args>(args)...);
  13919. }
  13920. };
  13921. };
  13922. template <typename F>
  13923. struct wrapper<F, std::enable_if_t<std::is_member_object_pointer<meta::unqualified_t<F>>::value>> {
  13924. typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
  13925. typedef typename traits_type::object_type object_type;
  13926. typedef typename traits_type::return_type return_type;
  13927. typedef typename traits_type::args_list args_list;
  13928. typedef types<object_type&, return_type> free_args_list;
  13929. typedef typename traits_type::returns_list returns_list;
  13930. template <F fx>
  13931. static auto call(object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
  13932. return mem.*fx;
  13933. }
  13934. template <F fx, typename Arg, typename... Args>
  13935. static decltype(auto) invoke(object_type& mem, Arg&& arg, Args&&...) {
  13936. return mem.*fx = std::forward<Arg>(arg);
  13937. }
  13938. template <typename Fx>
  13939. static auto call(Fx&& fx, object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
  13940. return mem.*fx;
  13941. }
  13942. template <typename Fx, typename Arg, typename... Args>
  13943. static void call(Fx&& fx, object_type& mem, Arg&& arg, Args&&...) {
  13944. using actual_type = meta::unqualified_t<detail::array_return_type<decltype(mem.*fx)>>;
  13945. if constexpr (std::is_array_v<actual_type>) {
  13946. using std::cbegin;
  13947. using std::cend;
  13948. auto first = cbegin(arg);
  13949. auto last = cend(arg);
  13950. for (std::size_t i = 0; first != last; ++i, ++first) {
  13951. (mem.*fx)[i] = *first;
  13952. }
  13953. }
  13954. else {
  13955. (mem.*fx) = std::forward<Arg>(arg);
  13956. }
  13957. }
  13958. struct caller {
  13959. template <typename Fx, typename... Args>
  13960. decltype(auto) operator()(Fx&& fx, object_type& mem, Args&&... args) const {
  13961. return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
  13962. }
  13963. };
  13964. template <F fx>
  13965. struct invoker {
  13966. template <typename... Args>
  13967. decltype(auto) operator()(Args&&... args) const {
  13968. return invoke<fx>(std::forward<Args>(args)...);
  13969. }
  13970. };
  13971. };
  13972. template <typename F, typename R, typename O, typename... FArgs>
  13973. struct member_function_wrapper {
  13974. typedef O object_type;
  13975. typedef lua_bind_traits<F> traits_type;
  13976. typedef typename traits_type::args_list args_list;
  13977. typedef types<object_type&, FArgs...> free_args_list;
  13978. typedef meta::tuple_types<R> returns_list;
  13979. template <F fx, typename... Args>
  13980. static R invoke(O& mem, Args&&... args) {
  13981. return (mem.*fx)(std::forward<Args>(args)...);
  13982. }
  13983. template <typename Fx, typename... Args>
  13984. static R call(Fx&& fx, O& mem, Args&&... args) {
  13985. return (mem.*fx)(std::forward<Args>(args)...);
  13986. }
  13987. struct caller {
  13988. template <typename Fx, typename... Args>
  13989. decltype(auto) operator()(Fx&& fx, O& mem, Args&&... args) const {
  13990. return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
  13991. }
  13992. };
  13993. template <F fx>
  13994. struct invoker {
  13995. template <typename... Args>
  13996. decltype(auto) operator()(O& mem, Args&&... args) const {
  13997. return invoke<fx>(mem, std::forward<Args>(args)...);
  13998. }
  13999. };
  14000. };
  14001. template <typename R, typename O, typename... Args>
  14002. struct wrapper<R (O::*)(Args...)> : public member_function_wrapper<R (O::*)(Args...), R, O, Args...> {};
  14003. template <typename R, typename O, typename... Args>
  14004. struct wrapper<R (O::*)(Args...) const> : public member_function_wrapper<R (O::*)(Args...) const, R, O, Args...> {};
  14005. template <typename R, typename O, typename... Args>
  14006. struct wrapper<R (O::*)(Args...) const volatile> : public member_function_wrapper<R (O::*)(Args...) const volatile, R, O, Args...> {};
  14007. template <typename R, typename O, typename... Args>
  14008. struct wrapper<R (O::*)(Args...)&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> {};
  14009. template <typename R, typename O, typename... Args>
  14010. struct wrapper<R (O::*)(Args...) const&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> {};
  14011. template <typename R, typename O, typename... Args>
  14012. struct wrapper<R (O::*)(Args...) const volatile&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> {};
  14013. template <typename R, typename O, typename... Args>
  14014. struct wrapper<R (O::*)(Args..., ...)&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> {};
  14015. template <typename R, typename O, typename... Args>
  14016. struct wrapper<R (O::*)(Args..., ...) const&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> {};
  14017. template <typename R, typename O, typename... Args>
  14018. struct wrapper<R (O::*)(Args..., ...) const volatile&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> {};
  14019. template <typename R, typename O, typename... Args>
  14020. struct wrapper<R (O::*)(Args...) &&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> {};
  14021. template <typename R, typename O, typename... Args>
  14022. struct wrapper<R (O::*)(Args...) const&&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> {};
  14023. template <typename R, typename O, typename... Args>
  14024. struct wrapper<R (O::*)(Args...) const volatile&&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> {};
  14025. template <typename R, typename O, typename... Args>
  14026. struct wrapper<R (O::*)(Args..., ...) &&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> {};
  14027. template <typename R, typename O, typename... Args>
  14028. struct wrapper<R (O::*)(Args..., ...) const&&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> {};
  14029. template <typename R, typename O, typename... Args>
  14030. struct wrapper<R (O::*)(Args..., ...) const volatile&&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> {};
  14031. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  14032. // noexcept has become a part of a function's type
  14033. template <typename R, typename O, typename... Args>
  14034. struct wrapper<R (O::*)(Args...) noexcept> : public member_function_wrapper<R (O::*)(Args...) noexcept, R, O, Args...> {};
  14035. template <typename R, typename O, typename... Args>
  14036. struct wrapper<R (O::*)(Args...) const noexcept> : public member_function_wrapper<R (O::*)(Args...) const noexcept, R, O, Args...> {};
  14037. template <typename R, typename O, typename... Args>
  14038. struct wrapper<R (O::*)(Args...) const volatile noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile noexcept, R, O, Args...> {};
  14039. template <typename R, typename O, typename... Args>
  14040. struct wrapper<R (O::*)(Args...) & noexcept> : public member_function_wrapper<R (O::*)(Args...) & noexcept, R, O, Args...> {};
  14041. template <typename R, typename O, typename... Args>
  14042. struct wrapper<R (O::*)(Args...) const& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> {};
  14043. template <typename R, typename O, typename... Args>
  14044. struct wrapper<R (O::*)(Args...) const volatile& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> {};
  14045. template <typename R, typename O, typename... Args>
  14046. struct wrapper<R (O::*)(Args..., ...) & noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) & noexcept, R, O, Args...> {};
  14047. template <typename R, typename O, typename... Args>
  14048. struct wrapper<R (O::*)(Args..., ...) const& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> {};
  14049. template <typename R, typename O, typename... Args>
  14050. struct wrapper<R (O::*)(Args..., ...) const volatile& noexcept>
  14051. : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> {};
  14052. template <typename R, typename O, typename... Args>
  14053. struct wrapper<R (O::*)(Args...) && noexcept> : public member_function_wrapper<R (O::*)(Args...) & noexcept, R, O, Args...> {};
  14054. template <typename R, typename O, typename... Args>
  14055. struct wrapper<R (O::*)(Args...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> {};
  14056. template <typename R, typename O, typename... Args>
  14057. struct wrapper<R (O::*)(Args...) const volatile&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> {};
  14058. template <typename R, typename O, typename... Args>
  14059. struct wrapper<R (O::*)(Args..., ...) && noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) & noexcept, R, O, Args...> {};
  14060. template <typename R, typename O, typename... Args>
  14061. struct wrapper<R (O::*)(Args..., ...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> {};
  14062. template <typename R, typename O, typename... Args>
  14063. struct wrapper<R (O::*)(Args..., ...) const volatile&& noexcept>
  14064. : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> {};
  14065. #endif // noexcept is part of a function's type
  14066. } // namespace sol
  14067. // end of sol/wrapper.hpp
  14068. #include <memory>
  14069. namespace sol {
  14070. namespace function_detail {
  14071. template <typename Fx, int start = 1, bool is_yielding = false>
  14072. int call(lua_State* L) {
  14073. Fx& fx = stack::get<user<Fx>>(L, upvalue_index(start));
  14074. int nr = fx(L);
  14075. if (is_yielding) {
  14076. return lua_yield(L, nr);
  14077. }
  14078. else {
  14079. return nr;
  14080. }
  14081. }
  14082. }
  14083. } // namespace sol::function_detail
  14084. // end of sol/function_types_core.hpp
  14085. // beginning of sol/function_types_templated.hpp
  14086. // beginning of sol/call.hpp
  14087. // beginning of sol/property.hpp
  14088. #include <type_traits>
  14089. #include <utility>
  14090. namespace sol {
  14091. namespace detail {
  14092. struct no_prop {};
  14093. }
  14094. template <typename R, typename W>
  14095. struct property_wrapper : detail::ebco<R, 0>, detail::ebco<W, 1> {
  14096. private:
  14097. using read_base_t = detail::ebco<R, 0>;
  14098. using write_base_t = detail::ebco<W, 1>;
  14099. public:
  14100. template <typename Rx, typename Wx>
  14101. property_wrapper(Rx&& r, Wx&& w)
  14102. : read_base_t(std::forward<Rx>(r)), write_base_t(std::forward<Wx>(w)) {
  14103. }
  14104. W& write() {
  14105. return write_base_t::value();
  14106. }
  14107. const W& write() const {
  14108. return write_base_t::value();
  14109. }
  14110. R& read() {
  14111. return read_base_t::value();
  14112. }
  14113. const R& read() const {
  14114. return read_base_t::value();
  14115. }
  14116. };
  14117. template <typename F, typename G>
  14118. inline decltype(auto) property(F&& f, G&& g) {
  14119. typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
  14120. typedef lua_bind_traits<meta::unqualified_t<G>> right_traits;
  14121. if constexpr (left_traits::free_arity < right_traits::free_arity) {
  14122. return property_wrapper<std::decay_t<F>, std::decay_t<G>>(std::forward<F>(f), std::forward<G>(g));
  14123. }
  14124. else {
  14125. return property_wrapper<std::decay_t<G>, std::decay_t<F>>(std::forward<G>(g), std::forward<F>(f));
  14126. }
  14127. }
  14128. template <typename F>
  14129. inline decltype(auto) property(F&& f) {
  14130. typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
  14131. if constexpr (left_traits::free_arity < 2) {
  14132. return property_wrapper<std::decay_t<F>, detail::no_prop>(std::forward<F>(f), detail::no_prop());
  14133. }
  14134. else {
  14135. return property_wrapper<detail::no_prop, std::decay_t<F>>(detail::no_prop(), std::forward<F>(f));
  14136. }
  14137. }
  14138. template <typename F>
  14139. inline decltype(auto) readonly_property(F&& f) {
  14140. return property_wrapper<std::decay_t<F>, detail::no_prop>(std::forward<F>(f), detail::no_prop());
  14141. }
  14142. template <typename F>
  14143. inline decltype(auto) writeonly_property(F&& f) {
  14144. return property_wrapper<detail::no_prop, std::decay_t<F>>(detail::no_prop(), std::forward<F>(f));
  14145. }
  14146. template <typename T>
  14147. struct readonly_wrapper : detail::ebco<T> {
  14148. private:
  14149. using base_t = detail::ebco<T>;
  14150. public:
  14151. using base_t::base_t;
  14152. operator T&() {
  14153. return base_t::value();
  14154. }
  14155. operator const T&() const {
  14156. return base_t::value();
  14157. }
  14158. };
  14159. // Allow someone to make a member variable readonly (const)
  14160. template <typename R, typename T>
  14161. inline auto readonly(R T::*v) {
  14162. return readonly_wrapper<meta::unqualified_t<decltype(v)>>(v);
  14163. }
  14164. template <typename T>
  14165. struct var_wrapper : detail::ebco<T> {
  14166. private:
  14167. using base_t = detail::ebco<T>;
  14168. public:
  14169. using base_t::base_t;
  14170. };
  14171. template <typename V>
  14172. inline auto var(V&& v) {
  14173. typedef std::decay_t<V> T;
  14174. return var_wrapper<T>(std::forward<V>(v));
  14175. }
  14176. namespace meta {
  14177. template <typename T>
  14178. struct is_member_object : std::is_member_object_pointer<T> {};
  14179. template <typename T>
  14180. struct is_member_object<readonly_wrapper<T>> : std::true_type {};
  14181. template <typename T>
  14182. inline constexpr bool is_member_object_v = is_member_object<T>::value;
  14183. } // namespace meta
  14184. } // namespace sol
  14185. // end of sol/property.hpp
  14186. // beginning of sol/protect.hpp
  14187. #include <utility>
  14188. namespace sol {
  14189. template <typename T>
  14190. struct protect_t {
  14191. T value;
  14192. template <typename Arg, typename... Args, meta::disable<std::is_same<protect_t, meta::unqualified_t<Arg>>> = meta::enabler>
  14193. protect_t(Arg&& arg, Args&&... args)
  14194. : value(std::forward<Arg>(arg), std::forward<Args>(args)...) {
  14195. }
  14196. protect_t(const protect_t&) = default;
  14197. protect_t(protect_t&&) = default;
  14198. protect_t& operator=(const protect_t&) = default;
  14199. protect_t& operator=(protect_t&&) = default;
  14200. };
  14201. template <typename T>
  14202. auto protect(T&& value) {
  14203. return protect_t<std::decay_t<T>>(std::forward<T>(value));
  14204. }
  14205. } // namespace sol
  14206. // end of sol/protect.hpp
  14207. namespace sol {
  14208. namespace u_detail {
  14209. } // namespace u_detail
  14210. namespace policy_detail {
  14211. template <int I, int... In>
  14212. inline void handle_policy(static_stack_dependencies<I, In...>, lua_State* L, int&) {
  14213. if constexpr (sizeof...(In) == 0) {
  14214. (void)L;
  14215. return;
  14216. }
  14217. else {
  14218. absolute_index ai(L, I);
  14219. if (type_of(L, ai) != type::userdata) {
  14220. return;
  14221. }
  14222. lua_createtable(L, static_cast<int>(sizeof...(In)), 0);
  14223. stack_reference deps(L, -1);
  14224. auto per_dep = [&L, &deps](int i) {
  14225. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  14226. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  14227. #endif // make sure stack doesn't overflow
  14228. lua_pushvalue(L, i);
  14229. luaL_ref(L, deps.stack_index());
  14230. };
  14231. (void)per_dep;
  14232. (void)detail::swallow{ int(), (per_dep(In), int())... };
  14233. lua_setuservalue(L, ai);
  14234. }
  14235. }
  14236. template <int... In>
  14237. inline void handle_policy(returns_self_with<In...>, lua_State* L, int& pushed) {
  14238. pushed = stack::push(L, raw_index(1));
  14239. handle_policy(static_stack_dependencies<-1, In...>(), L, pushed);
  14240. }
  14241. inline void handle_policy(const stack_dependencies& sdeps, lua_State* L, int&) {
  14242. absolute_index ai(L, sdeps.target);
  14243. if (type_of(L, ai) != type::userdata) {
  14244. return;
  14245. }
  14246. lua_createtable(L, static_cast<int>(sdeps.size()), 0);
  14247. stack_reference deps(L, -1);
  14248. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  14249. luaL_checkstack(L, static_cast<int>(sdeps.size()), detail::not_enough_stack_space_generic);
  14250. #endif // make sure stack doesn't overflow
  14251. for (std::size_t i = 0; i < sdeps.size(); ++i) {
  14252. lua_pushvalue(L, sdeps.stack_indices[i]);
  14253. luaL_ref(L, deps.stack_index());
  14254. }
  14255. lua_setuservalue(L, ai);
  14256. }
  14257. template <typename P, meta::disable<std::is_base_of<detail::policy_base_tag, meta::unqualified_t<P>>> = meta::enabler>
  14258. inline void handle_policy(P&& p, lua_State* L, int& pushed) {
  14259. pushed = std::forward<P>(p)(L, pushed);
  14260. }
  14261. } // namespace policy_detail
  14262. namespace function_detail {
  14263. inline int no_construction_error(lua_State* L) {
  14264. return luaL_error(L, "sol: cannot call this constructor (tagged as non-constructible)");
  14265. }
  14266. } // namespace function_detail
  14267. namespace call_detail {
  14268. template <typename R, typename W>
  14269. inline auto& pick(std::true_type, property_wrapper<R, W>& f) {
  14270. return f.read();
  14271. }
  14272. template <typename R, typename W>
  14273. inline auto& pick(std::false_type, property_wrapper<R, W>& f) {
  14274. return f.write();
  14275. }
  14276. template <typename T, typename List>
  14277. struct void_call : void_call<T, meta::function_args_t<List>> {};
  14278. template <typename T, typename... Args>
  14279. struct void_call<T, types<Args...>> {
  14280. static void call(Args...) {
  14281. }
  14282. };
  14283. template <typename T, bool checked, bool clean_stack>
  14284. struct constructor_match {
  14285. T* obj_;
  14286. constructor_match(T* o) : obj_(o) {
  14287. }
  14288. template <typename Fx, std::size_t I, typename... R, typename... Args>
  14289. int operator()(types<Fx>, meta::index_value<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start) const {
  14290. detail::default_construct func{};
  14291. return stack::call_into_lua<checked, clean_stack>(r, a, L, start, func, obj_);
  14292. }
  14293. };
  14294. namespace overload_detail {
  14295. template <std::size_t... M, typename Match, typename... Args>
  14296. inline int overload_match_arity(types<>, std::index_sequence<>, std::index_sequence<M...>, Match&&, lua_State* L, int, int, Args&&...) {
  14297. return luaL_error(L, "sol: no matching function call takes this number of arguments and the specified types");
  14298. }
  14299. template <typename Fx, typename... Fxs, std::size_t I, std::size_t... In, std::size_t... M, typename Match, typename... Args>
  14300. inline int overload_match_arity(types<Fx, Fxs...>, std::index_sequence<I, In...>, std::index_sequence<M...>, Match&& matchfx, lua_State* L,
  14301. int fxarity, int start, Args&&... args) {
  14302. typedef lua_bind_traits<meta::unwrap_unqualified_t<Fx>> traits;
  14303. typedef meta::tuple_types<typename traits::return_type> return_types;
  14304. typedef typename traits::free_args_list args_list;
  14305. // compile-time eliminate any functions that we know ahead of time are of improper arity
  14306. if constexpr (!traits::runtime_variadics_t::value
  14307. && meta::find_in_pack_v<meta::index_value<traits::free_arity>, meta::index_value<M>...>::value) {
  14308. return overload_match_arity(types<Fxs...>(),
  14309. std::index_sequence<In...>(),
  14310. std::index_sequence<M...>(),
  14311. std::forward<Match>(matchfx),
  14312. L,
  14313. fxarity,
  14314. start,
  14315. std::forward<Args>(args)...);
  14316. }
  14317. else {
  14318. if constexpr (!traits::runtime_variadics_t::value) {
  14319. if (traits::free_arity != fxarity) {
  14320. return overload_match_arity(types<Fxs...>(),
  14321. std::index_sequence<In...>(),
  14322. std::index_sequence<traits::free_arity, M...>(),
  14323. std::forward<Match>(matchfx),
  14324. L,
  14325. fxarity,
  14326. start,
  14327. std::forward<Args>(args)...);
  14328. }
  14329. }
  14330. stack::record tracking{};
  14331. if (!stack::stack_detail::check_types(args_list(), L, start, no_panic, tracking)) {
  14332. return overload_match_arity(types<Fxs...>(),
  14333. std::index_sequence<In...>(),
  14334. std::index_sequence<M...>(),
  14335. std::forward<Match>(matchfx),
  14336. L,
  14337. fxarity,
  14338. start,
  14339. std::forward<Args>(args)...);
  14340. }
  14341. return matchfx(types<Fx>(), meta::index_value<I>(), return_types(), args_list(), L, fxarity, start, std::forward<Args>(args)...);
  14342. }
  14343. }
  14344. template <std::size_t... M, typename Match, typename... Args>
  14345. inline int overload_match_arity_single(
  14346. types<>, std::index_sequence<>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  14347. return overload_match_arity(types<>(),
  14348. std::index_sequence<>(),
  14349. std::index_sequence<M...>(),
  14350. std::forward<Match>(matchfx),
  14351. L,
  14352. fxarity,
  14353. start,
  14354. std::forward<Args>(args)...);
  14355. }
  14356. template <typename Fx, std::size_t I, std::size_t... M, typename Match, typename... Args>
  14357. inline int overload_match_arity_single(
  14358. types<Fx>, std::index_sequence<I>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  14359. typedef lua_bind_traits<meta::unwrap_unqualified_t<Fx>> traits;
  14360. typedef meta::tuple_types<typename traits::return_type> return_types;
  14361. typedef typename traits::free_args_list args_list;
  14362. // compile-time eliminate any functions that we know ahead of time are of improper arity
  14363. if constexpr (!traits::runtime_variadics_t::value
  14364. && meta::find_in_pack_v<meta::index_value<traits::free_arity>, meta::index_value<M>...>::value) {
  14365. return overload_match_arity(types<>(),
  14366. std::index_sequence<>(),
  14367. std::index_sequence<M...>(),
  14368. std::forward<Match>(matchfx),
  14369. L,
  14370. fxarity,
  14371. start,
  14372. std::forward<Args>(args)...);
  14373. }
  14374. if constexpr (!traits::runtime_variadics_t::value) {
  14375. if (traits::free_arity != fxarity) {
  14376. return overload_match_arity(types<>(),
  14377. std::index_sequence<>(),
  14378. std::index_sequence<traits::free_arity, M...>(),
  14379. std::forward<Match>(matchfx),
  14380. L,
  14381. fxarity,
  14382. start,
  14383. std::forward<Args>(args)...);
  14384. }
  14385. }
  14386. return matchfx(types<Fx>(), meta::index_value<I>(), return_types(), args_list(), L, fxarity, start, std::forward<Args>(args)...);
  14387. }
  14388. 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,
  14389. typename... Args>
  14390. inline int overload_match_arity_single(types<Fx, Fx1, Fxs...>, std::index_sequence<I, I1, In...>, std::index_sequence<M...>, Match&& matchfx,
  14391. lua_State* L, int fxarity, int start, Args&&... args) {
  14392. typedef lua_bind_traits<meta::unwrap_unqualified_t<Fx>> traits;
  14393. typedef meta::tuple_types<typename traits::return_type> return_types;
  14394. typedef typename traits::free_args_list args_list;
  14395. // compile-time eliminate any functions that we know ahead of time are of improper arity
  14396. if constexpr (!traits::runtime_variadics_t::value
  14397. && meta::find_in_pack_v<meta::index_value<traits::free_arity>, meta::index_value<M>...>::value) {
  14398. return overload_match_arity(types<Fx1, Fxs...>(),
  14399. std::index_sequence<I1, In...>(),
  14400. std::index_sequence<M...>(),
  14401. std::forward<Match>(matchfx),
  14402. L,
  14403. fxarity,
  14404. start,
  14405. std::forward<Args>(args)...);
  14406. }
  14407. else {
  14408. if constexpr (!traits::runtime_variadics_t::value) {
  14409. if (traits::free_arity != fxarity) {
  14410. return overload_match_arity(types<Fx1, Fxs...>(),
  14411. std::index_sequence<I1, In...>(),
  14412. std::index_sequence<traits::free_arity, M...>(),
  14413. std::forward<Match>(matchfx),
  14414. L,
  14415. fxarity,
  14416. start,
  14417. std::forward<Args>(args)...);
  14418. }
  14419. }
  14420. stack::record tracking{};
  14421. if (!stack::stack_detail::check_types(args_list(), L, start, no_panic, tracking)) {
  14422. return overload_match_arity(types<Fx1, Fxs...>(),
  14423. std::index_sequence<I1, In...>(),
  14424. std::index_sequence<M...>(),
  14425. std::forward<Match>(matchfx),
  14426. L,
  14427. fxarity,
  14428. start,
  14429. std::forward<Args>(args)...);
  14430. }
  14431. return matchfx(types<Fx>(), meta::index_value<I>(), return_types(), args_list(), L, fxarity, start, std::forward<Args>(args)...);
  14432. }
  14433. }
  14434. } // namespace overload_detail
  14435. template <typename... Functions, typename Match, typename... Args>
  14436. inline int overload_match_arity(Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  14437. return overload_detail::overload_match_arity_single(types<Functions...>(),
  14438. std::make_index_sequence<sizeof...(Functions)>(),
  14439. std::index_sequence<>(),
  14440. std::forward<Match>(matchfx),
  14441. L,
  14442. fxarity,
  14443. start,
  14444. std::forward<Args>(args)...);
  14445. }
  14446. template <typename... Functions, typename Match, typename... Args>
  14447. inline int overload_match(Match&& matchfx, lua_State* L, int start, Args&&... args) {
  14448. int fxarity = lua_gettop(L) - (start - 1);
  14449. return overload_match_arity<Functions...>(std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  14450. }
  14451. template <typename T, typename... TypeLists, typename Match, typename... Args>
  14452. inline int construct_match(Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
  14453. // use same overload resolution matching as all other parts of the framework
  14454. return overload_match_arity<decltype(void_call<T, TypeLists>::call)...>(
  14455. std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
  14456. }
  14457. template <typename T, bool checked, bool clean_stack, typename... TypeLists>
  14458. inline int construct_trampolined(lua_State* L) {
  14459. static const auto& meta = usertype_traits<T>::metatable();
  14460. int argcount = lua_gettop(L);
  14461. call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, usertype_traits<T>::user_metatable(), 1) : call_syntax::dot;
  14462. argcount -= static_cast<int>(syntax);
  14463. T* obj = detail::usertype_allocate<T>(L);
  14464. reference userdataref(L, -1);
  14465. stack::stack_detail::undefined_metatable umf(L, &meta[0], &stack::stack_detail::set_undefined_methods_on<T>);
  14466. umf();
  14467. // put userdata at the first index
  14468. lua_insert(L, 1);
  14469. construct_match<T, TypeLists...>(constructor_match<T, checked, clean_stack>(obj), L, argcount, 1 + static_cast<int>(syntax));
  14470. userdataref.push();
  14471. return 1;
  14472. }
  14473. template <typename T, bool checked, bool clean_stack, typename... TypeLists>
  14474. inline int construct(lua_State* L) {
  14475. return detail::static_trampoline<&construct_trampolined<T, checked, clean_stack, TypeLists...>>(L);
  14476. }
  14477. template <typename F, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename = void>
  14478. struct agnostic_lua_call_wrapper {
  14479. template <typename Fx, typename... Args>
  14480. static int call(lua_State* L, Fx&& f, Args&&... args) {
  14481. using uFx = meta::unqualified_t<Fx>;
  14482. static constexpr bool is_ref = is_lua_reference_v<uFx>;
  14483. if constexpr (is_ref) {
  14484. if constexpr (is_index) {
  14485. return stack::push(L, std::forward<Fx>(f), std::forward<Args>(args)...);
  14486. }
  14487. else {
  14488. std::forward<Fx>(f) = stack::unqualified_get<F>(L, boost + (is_variable ? 3 : 1));
  14489. return 0;
  14490. }
  14491. }
  14492. else {
  14493. using wrap = wrapper<uFx>;
  14494. using traits_type = typename wrap::traits_type;
  14495. using fp_t = typename traits_type::function_pointer_type;
  14496. constexpr bool is_function_pointer_convertible
  14497. = std::is_class_v<uFx> && std::is_convertible_v<std::decay_t<Fx>, fp_t>;
  14498. if constexpr (is_function_pointer_convertible) {
  14499. fp_t fx = f;
  14500. return agnostic_lua_call_wrapper<fp_t, is_index, is_variable, checked, boost, clean_stack>{}.call(
  14501. L, fx, std::forward<Args>(args)...);
  14502. }
  14503. else {
  14504. using returns_list = typename wrap::returns_list;
  14505. using args_list = typename wrap::free_args_list;
  14506. using caller = typename wrap::caller;
  14507. return stack::call_into_lua<checked, clean_stack>(
  14508. returns_list(), args_list(), L, boost + 1, caller(), std::forward<Fx>(f), std::forward<Args>(args)...);
  14509. }
  14510. }
  14511. }
  14512. };
  14513. template <typename T, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14514. struct agnostic_lua_call_wrapper<var_wrapper<T>, is_index, is_variable, checked, boost, clean_stack, C> {
  14515. template <typename F>
  14516. static int call(lua_State* L, F&& f) {
  14517. if constexpr (is_index) {
  14518. constexpr bool is_stack = is_stack_based_v<meta::unqualified_t<decltype(detail::unwrap(f.value()))>>;
  14519. if constexpr (clean_stack && !is_stack) {
  14520. lua_settop(L, 0);
  14521. }
  14522. return stack::push_reference(L, detail::unwrap(f.value()));
  14523. }
  14524. else {
  14525. if constexpr (std::is_const_v<meta::unwrapped_t<T>>) {
  14526. (void)f;
  14527. return luaL_error(L, "sol: cannot write to a readonly (const) variable");
  14528. }
  14529. else {
  14530. using R = meta::unwrapped_t<T>;
  14531. if constexpr (std::is_assignable_v<std::add_lvalue_reference_t<meta::unqualified_t<R>>, R>) {
  14532. detail::unwrap(f.value()) = stack::unqualified_get<meta::unwrapped_t<T>>(L, boost + (is_variable ? 3 : 1));
  14533. if (clean_stack) {
  14534. lua_settop(L, 0);
  14535. }
  14536. return 0;
  14537. }
  14538. else {
  14539. return luaL_error(L, "sol: cannot write to this variable: copy assignment/constructor not available");
  14540. }
  14541. }
  14542. }
  14543. }
  14544. };
  14545. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14546. struct agnostic_lua_call_wrapper<lua_CFunction_ref, is_index, is_variable, checked, boost, clean_stack, C> {
  14547. static int call(lua_State* L, lua_CFunction_ref f) {
  14548. return f(L);
  14549. }
  14550. };
  14551. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14552. struct agnostic_lua_call_wrapper<lua_CFunction, is_index, is_variable, checked, boost, clean_stack, C> {
  14553. static int call(lua_State* L, lua_CFunction f) {
  14554. return f(L);
  14555. }
  14556. };
  14557. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  14558. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14559. struct agnostic_lua_call_wrapper<detail::lua_CFunction_noexcept, is_index, is_variable, checked, boost, clean_stack, C> {
  14560. static int call(lua_State* L, detail::lua_CFunction_noexcept f) {
  14561. return f(L);
  14562. }
  14563. };
  14564. #endif // noexcept function types
  14565. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14566. struct agnostic_lua_call_wrapper<detail::no_prop, is_index, is_variable, checked, boost, clean_stack, C> {
  14567. static int call(lua_State* L, const detail::no_prop&) {
  14568. return luaL_error(L, is_index ? "sol: cannot read from a writeonly property" : "sol: cannot write to a readonly property");
  14569. }
  14570. };
  14571. template <bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14572. struct agnostic_lua_call_wrapper<no_construction, is_index, is_variable, checked, boost, clean_stack, C> {
  14573. static int call(lua_State* L, const no_construction&) {
  14574. return function_detail::no_construction_error(L);
  14575. }
  14576. };
  14577. template <typename... Args, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14578. struct agnostic_lua_call_wrapper<bases<Args...>, is_index, is_variable, checked, boost, clean_stack, C> {
  14579. static int call(lua_State*, const bases<Args...>&) {
  14580. // Uh. How did you even call this, lul
  14581. return 0;
  14582. }
  14583. };
  14584. template <typename T, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14585. struct agnostic_lua_call_wrapper<std::reference_wrapper<T>, is_index, is_variable, checked, boost, clean_stack, C> {
  14586. static int call(lua_State* L, std::reference_wrapper<T> f) {
  14587. agnostic_lua_call_wrapper<T, is_index, is_variable, checked, boost, clean_stack> alcw{};
  14588. return alcw.call(L, f.get());
  14589. }
  14590. };
  14591. template <typename T, typename F, bool is_index, bool is_variable, bool checked = detail::default_safe_function_calls, int boost = 0,
  14592. bool clean_stack = true, typename = void>
  14593. struct lua_call_wrapper {
  14594. template <typename Fx, typename... Args>
  14595. static int call(lua_State* L, Fx&& fx, Args&&... args) {
  14596. if constexpr (std::is_member_function_pointer_v<F>) {
  14597. using wrap = wrapper<F>;
  14598. using object_type = typename wrap::object_type;
  14599. if constexpr (sizeof...(Args) < 1) {
  14600. using Ta = meta::conditional_t<std::is_void_v<T>, object_type, T>;
  14601. static_assert(std::is_base_of_v<object_type, Ta>, "It seems like you might have accidentally bound a class type with a member function method that does not correspond to the class. For example, there could be a small type in your new_usertype<T>(...) binding, where you specify one class \"T\" but then bind member methods from a complete unrelated class. Check things over!");
  14602. #if SOL_IS_ON(SOL_SAFE_USERTYPE_I_)
  14603. auto maybeo = stack::check_get<Ta*>(L, 1);
  14604. if (!maybeo || maybeo.value() == nullptr) {
  14605. return luaL_error(L,
  14606. "sol: received nil for 'self' argument (use ':' for accessing member functions, make sure member variables are "
  14607. "preceeded by the "
  14608. "actual object with '.' syntax)");
  14609. }
  14610. object_type* o = static_cast<object_type*>(maybeo.value());
  14611. return call(L, std::forward<Fx>(fx), *o);
  14612. #else
  14613. object_type& o = static_cast<object_type&>(*stack::unqualified_get<non_null<Ta*>>(L, 1));
  14614. return call(L, std::forward<Fx>(fx), o);
  14615. #endif // Safety
  14616. }
  14617. else {
  14618. using returns_list = typename wrap::returns_list;
  14619. using args_list = typename wrap::args_list;
  14620. using caller = typename wrap::caller;
  14621. return stack::call_into_lua<checked, clean_stack>(
  14622. returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), std::forward<Fx>(fx), std::forward<Args>(args)...);
  14623. }
  14624. }
  14625. else if constexpr (std::is_member_object_pointer_v<F>) {
  14626. using wrap = wrapper<F>;
  14627. using object_type = typename wrap::object_type;
  14628. if constexpr (is_index) {
  14629. if constexpr (sizeof...(Args) < 1) {
  14630. using Ta = meta::conditional_t<std::is_void_v<T>, object_type, T>;
  14631. static_assert(std::is_base_of_v<object_type, Ta>, "It seems like you might have accidentally bound a class type with a member function method that does not correspond to the class. For example, there could be a small type in your new_usertype<T>(...) binding, where you specify one class \"T\" but then bind member methods from a complete unrelated class. Check things over!");
  14632. #if SOL_IS_ON(SOL_SAFE_USERTYPE_I_)
  14633. auto maybeo = stack::check_get<Ta*>(L, 1);
  14634. if (!maybeo || maybeo.value() == nullptr) {
  14635. if (is_variable) {
  14636. return luaL_error(L, "sol: 'self' argument is lua_nil (bad '.' access?)");
  14637. }
  14638. return luaL_error(L, "sol: 'self' argument is lua_nil (pass 'self' as first argument)");
  14639. }
  14640. object_type* o = static_cast<object_type*>(maybeo.value());
  14641. return call(L, std::forward<Fx>(fx), *o);
  14642. #else
  14643. object_type& o = static_cast<object_type&>(*stack::get<non_null<Ta*>>(L, 1));
  14644. return call(L, std::forward<Fx>(fx), o);
  14645. #endif // Safety
  14646. }
  14647. else {
  14648. using returns_list = typename wrap::returns_list;
  14649. using caller = typename wrap::caller;
  14650. return stack::call_into_lua<checked, clean_stack>(returns_list(),
  14651. types<>(),
  14652. L,
  14653. boost + (is_variable ? 3 : 2),
  14654. caller(),
  14655. std::forward<Fx>(fx),
  14656. std::forward<Args>(args)...);
  14657. }
  14658. }
  14659. else {
  14660. using traits_type = lua_bind_traits<F>;
  14661. using return_type = typename traits_type::return_type;
  14662. constexpr bool ret_is_const = std::is_const_v<std::remove_reference_t<return_type>>;
  14663. if constexpr (ret_is_const) {
  14664. (void)fx;
  14665. (void)detail::swallow{ 0, (static_cast<void>(args), 0)... };
  14666. return luaL_error(L, "sol: cannot write to a readonly (const) variable");
  14667. }
  14668. else {
  14669. using u_return_type = meta::unqualified_t<return_type>;
  14670. constexpr bool is_assignable = std::is_copy_assignable_v<u_return_type> || std::is_array_v<u_return_type>;
  14671. if constexpr (!is_assignable) {
  14672. (void)fx;
  14673. (void)detail::swallow{ 0, ((void)args, 0)... };
  14674. return luaL_error(L, "sol: cannot write to this variable: copy assignment/constructor not available");
  14675. }
  14676. else {
  14677. using args_list = typename wrap::args_list;
  14678. using caller = typename wrap::caller;
  14679. if constexpr (sizeof...(Args) > 0) {
  14680. return stack::call_into_lua<checked, clean_stack>(types<void>(),
  14681. args_list(),
  14682. L,
  14683. boost + (is_variable ? 3 : 2),
  14684. caller(),
  14685. std::forward<Fx>(fx),
  14686. std::forward<Args>(args)...);
  14687. }
  14688. else {
  14689. using Ta = meta::conditional_t<std::is_void_v<T>, object_type, T>;
  14690. #if SOL_IS_ON(SOL_SAFE_USERTYPE_I_)
  14691. auto maybeo = stack::check_get<Ta*>(L, 1);
  14692. if (!maybeo || maybeo.value() == nullptr) {
  14693. if (is_variable) {
  14694. return luaL_error(L, "sol: received nil for 'self' argument (bad '.' access?)");
  14695. }
  14696. return luaL_error(L, "sol: received nil for 'self' argument (pass 'self' as first argument)");
  14697. }
  14698. object_type* po = static_cast<object_type*>(maybeo.value());
  14699. object_type& o = *po;
  14700. #else
  14701. object_type& o = static_cast<object_type&>(*stack::get<non_null<Ta*>>(L, 1));
  14702. #endif // Safety
  14703. return stack::call_into_lua<checked, clean_stack>(
  14704. types<void>(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), std::forward<Fx>(fx), o);
  14705. }
  14706. }
  14707. }
  14708. }
  14709. }
  14710. else {
  14711. agnostic_lua_call_wrapper<F, is_index, is_variable, checked, boost, clean_stack> alcw{};
  14712. return alcw.call(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  14713. }
  14714. }
  14715. };
  14716. template <typename T, typename F, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14717. struct lua_call_wrapper<T, readonly_wrapper<F>, is_index, is_variable, checked, boost, clean_stack, C> {
  14718. using traits_type = lua_bind_traits<F>;
  14719. using wrap = wrapper<F>;
  14720. using object_type = typename wrap::object_type;
  14721. static int call(lua_State* L, readonly_wrapper<F>&& rw) {
  14722. if constexpr (!is_index) {
  14723. (void)rw;
  14724. return luaL_error(L, "sol: cannot write to a sol::readonly variable");
  14725. }
  14726. else {
  14727. lua_call_wrapper<T, F, true, is_variable, checked, boost, clean_stack, C> lcw;
  14728. return lcw.call(L, std::move(rw.value()));
  14729. }
  14730. }
  14731. static int call(lua_State* L, readonly_wrapper<F>&& rw, object_type& o) {
  14732. if constexpr (!is_index) {
  14733. (void)o;
  14734. return call(L, std::move(rw));
  14735. }
  14736. else {
  14737. lua_call_wrapper<T, F, true, is_variable, checked, boost, clean_stack, C> lcw;
  14738. return lcw.call(L, rw.value(), o);
  14739. }
  14740. }
  14741. static int call(lua_State* L, const readonly_wrapper<F>& rw) {
  14742. if constexpr (!is_index) {
  14743. (void)rw;
  14744. return luaL_error(L, "sol: cannot write to a sol::readonly variable");
  14745. }
  14746. else {
  14747. lua_call_wrapper<T, F, true, is_variable, checked, boost, clean_stack, C> lcw;
  14748. return lcw.call(L, rw.value());
  14749. }
  14750. }
  14751. static int call(lua_State* L, const readonly_wrapper<F>& rw, object_type& o) {
  14752. if constexpr (!is_index) {
  14753. (void)o;
  14754. return call(L, rw);
  14755. }
  14756. else {
  14757. lua_call_wrapper<T, F, true, is_variable, checked, boost, clean_stack, C> lcw;
  14758. return lcw.call(L, rw.value(), o);
  14759. }
  14760. }
  14761. };
  14762. template <typename T, typename... Args, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14763. struct lua_call_wrapper<T, constructor_list<Args...>, is_index, is_variable, checked, boost, clean_stack, C> {
  14764. typedef constructor_list<Args...> F;
  14765. static int call(lua_State* L, F&) {
  14766. const auto& meta = usertype_traits<T>::metatable();
  14767. int argcount = lua_gettop(L);
  14768. call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, usertype_traits<T>::user_metatable(), 1) : call_syntax::dot;
  14769. argcount -= static_cast<int>(syntax);
  14770. T* obj = detail::usertype_allocate<T>(L);
  14771. reference userdataref(L, -1);
  14772. stack::stack_detail::undefined_metatable umf(L, &meta[0], &stack::stack_detail::set_undefined_methods_on<T>);
  14773. umf();
  14774. // put userdata at the first index
  14775. lua_insert(L, 1);
  14776. construct_match<T, Args...>(constructor_match<T, checked, clean_stack>(obj), L, argcount, boost + 1 + 1 + static_cast<int>(syntax));
  14777. userdataref.push();
  14778. return 1;
  14779. }
  14780. };
  14781. template <typename T, typename... Cxs, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14782. struct lua_call_wrapper<T, constructor_wrapper<Cxs...>, is_index, is_variable, checked, boost, clean_stack, C> {
  14783. typedef constructor_wrapper<Cxs...> F;
  14784. struct onmatch {
  14785. template <typename Fx, std::size_t I, typename... R, typename... Args>
  14786. int operator()(types<Fx>, meta::index_value<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start, F& f) {
  14787. const auto& meta = usertype_traits<T>::metatable();
  14788. T* obj = detail::usertype_allocate<T>(L);
  14789. reference userdataref(L, -1);
  14790. stack::stack_detail::undefined_metatable umf(L, &meta[0], &stack::stack_detail::set_undefined_methods_on<T>);
  14791. umf();
  14792. auto& func = std::get<I>(f.functions);
  14793. // put userdata at the first index
  14794. lua_insert(L, 1);
  14795. stack::call_into_lua<checked, clean_stack>(r, a, L, boost + 1 + start, func, detail::implicit_wrapper<T>(obj));
  14796. userdataref.push();
  14797. return 1;
  14798. }
  14799. };
  14800. static int call(lua_State* L, F& f) {
  14801. call_syntax syntax = stack::get_call_syntax(L, usertype_traits<T>::user_metatable(), 1);
  14802. int syntaxval = static_cast<int>(syntax);
  14803. int argcount = lua_gettop(L) - syntaxval;
  14804. return construct_match<T, meta::pop_front_type_t<meta::function_args_t<Cxs>>...>(onmatch(), L, argcount, 1 + syntaxval, f);
  14805. }
  14806. };
  14807. template <typename T, typename Fx, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14808. struct lua_call_wrapper<T, destructor_wrapper<Fx>, is_index, is_variable, checked, boost, clean_stack, C> {
  14809. template <typename F>
  14810. static int call(lua_State* L, F&& f) {
  14811. if constexpr (std::is_void_v<Fx>) {
  14812. return detail::usertype_alloc_destruct<T>(L);
  14813. }
  14814. else {
  14815. using uFx = meta::unqualified_t<Fx>;
  14816. lua_call_wrapper<T, uFx, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14817. return lcw.call(L, std::forward<F>(f).fx);
  14818. }
  14819. }
  14820. };
  14821. template <typename T, typename... Fs, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14822. struct lua_call_wrapper<T, overload_set<Fs...>, is_index, is_variable, checked, boost, clean_stack, C> {
  14823. typedef overload_set<Fs...> F;
  14824. struct on_match {
  14825. template <typename Fx, std::size_t I, typename... R, typename... Args>
  14826. int operator()(types<Fx>, meta::index_value<I>, types<R...>, types<Args...>, lua_State* L, int, int, F& fx) {
  14827. auto& f = std::get<I>(fx.functions);
  14828. return lua_call_wrapper<T, Fx, is_index, is_variable, checked, boost>{}.call(L, f);
  14829. }
  14830. };
  14831. static int call(lua_State* L, F& fx) {
  14832. return overload_match_arity<Fs...>(on_match(), L, lua_gettop(L), 1, fx);
  14833. }
  14834. };
  14835. template <typename T, typename... Fs, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14836. struct lua_call_wrapper<T, factory_wrapper<Fs...>, is_index, is_variable, checked, boost, clean_stack, C> {
  14837. typedef factory_wrapper<Fs...> F;
  14838. struct on_match {
  14839. template <typename Fx, std::size_t I, typename... R, typename... Args>
  14840. int operator()(types<Fx>, meta::index_value<I>, types<R...>, types<Args...>, lua_State* L, int, int, F& fx) {
  14841. auto& f = std::get<I>(fx.functions);
  14842. return lua_call_wrapper<T, Fx, is_index, is_variable, checked, boost, clean_stack>{}.call(L, f);
  14843. }
  14844. };
  14845. static int call(lua_State* L, F& fx) {
  14846. return overload_match_arity<Fs...>(on_match(), L, lua_gettop(L) - boost, 1 + boost, fx);
  14847. }
  14848. };
  14849. template <typename T, typename R, typename W, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14850. struct lua_call_wrapper<T, property_wrapper<R, W>, is_index, is_variable, checked, boost, clean_stack, C> {
  14851. typedef meta::conditional_t<is_index, R, W> P;
  14852. typedef meta::unqualified_t<P> U;
  14853. typedef wrapper<U> wrap;
  14854. typedef lua_bind_traits<U> traits_type;
  14855. typedef meta::unqualified_t<typename traits_type::template arg_at<0>> object_type;
  14856. template <typename F, typename... Args>
  14857. static int call(lua_State* L, F&& f, Args&&... args) {
  14858. constexpr bool is_specialized = meta::any<std::is_same<U, detail::no_prop>,
  14859. meta::is_specialization_of<U, var_wrapper>,
  14860. meta::is_specialization_of<U, constructor_wrapper>,
  14861. meta::is_specialization_of<U, constructor_list>,
  14862. std::is_member_pointer<U>>::value;
  14863. if constexpr (is_specialized) {
  14864. if constexpr (is_index) {
  14865. decltype(auto) p = f.read();
  14866. lua_call_wrapper<T, meta::unqualified_t<decltype(p)>, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14867. return lcw.call(L, p, std::forward<Args>(args)...);
  14868. }
  14869. else {
  14870. decltype(auto) p = f.write();
  14871. lua_call_wrapper<T, meta::unqualified_t<decltype(p)>, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14872. return lcw.call(L, p, std::forward<Args>(args)...);
  14873. }
  14874. }
  14875. else {
  14876. constexpr bool non_class_object_type = meta::any<std::is_void<object_type>,
  14877. meta::boolean<lua_type_of<meta::unwrap_unqualified_t<object_type>>::value != type::userdata>>::value;
  14878. if constexpr (non_class_object_type) {
  14879. // The type being void means we don't have any arguments, so it might be a free functions?
  14880. using args_list = typename traits_type::free_args_list;
  14881. using returns_list = typename wrap::returns_list;
  14882. using caller = typename wrap::caller;
  14883. if constexpr (is_index) {
  14884. decltype(auto) pf = f.read();
  14885. return stack::call_into_lua<checked, clean_stack>(
  14886. returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), pf);
  14887. }
  14888. else {
  14889. decltype(auto) pf = f.write();
  14890. return stack::call_into_lua<checked, clean_stack>(
  14891. returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), pf);
  14892. }
  14893. }
  14894. else {
  14895. using args_list = meta::pop_front_type_t<typename traits_type::free_args_list>;
  14896. using Ta = T;
  14897. using Oa = std::remove_pointer_t<object_type>;
  14898. #if SOL_IS_ON(SOL_SAFE_USERTYPE_I_)
  14899. auto maybeo = stack::check_get<Ta*>(L, 1);
  14900. if (!maybeo || maybeo.value() == nullptr) {
  14901. if (is_variable) {
  14902. return luaL_error(L, "sol: 'self' argument is lua_nil (bad '.' access?)");
  14903. }
  14904. return luaL_error(L, "sol: 'self' argument is lua_nil (pass 'self' as first argument)");
  14905. }
  14906. Oa* o = static_cast<Oa*>(maybeo.value());
  14907. #else
  14908. Oa* o = static_cast<Oa*>(stack::get<non_null<Ta*>>(L, 1));
  14909. #endif // Safety
  14910. using returns_list = typename wrap::returns_list;
  14911. using caller = typename wrap::caller;
  14912. if constexpr (is_index) {
  14913. decltype(auto) pf = f.read();
  14914. return stack::call_into_lua<checked, clean_stack>(
  14915. returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), pf, detail::implicit_wrapper<Oa>(*o));
  14916. }
  14917. else {
  14918. decltype(auto) pf = f.write();
  14919. return stack::call_into_lua<checked, clean_stack>(
  14920. returns_list(), args_list(), L, boost + (is_variable ? 3 : 2), caller(), pf, detail::implicit_wrapper<Oa>(*o));
  14921. }
  14922. }
  14923. }
  14924. }
  14925. };
  14926. template <typename T, typename V, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14927. struct lua_call_wrapper<T, protect_t<V>, is_index, is_variable, checked, boost, clean_stack, C> {
  14928. typedef protect_t<V> F;
  14929. template <typename... Args>
  14930. static int call(lua_State* L, F& fx, Args&&... args) {
  14931. return lua_call_wrapper<T, V, is_index, is_variable, true, boost, clean_stack>{}.call(L, fx.value, std::forward<Args>(args)...);
  14932. }
  14933. };
  14934. template <typename T, typename F, typename... Policies, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14935. struct lua_call_wrapper<T, policy_wrapper<F, Policies...>, is_index, is_variable, checked, boost, clean_stack, C> {
  14936. typedef policy_wrapper<F, Policies...> P;
  14937. template <std::size_t... In>
  14938. static int call(std::index_sequence<In...>, lua_State* L, P& fx) {
  14939. int pushed = lua_call_wrapper<T, F, is_index, is_variable, checked, boost, false, C>{}.call(L, fx.value);
  14940. (void)detail::swallow{ int(), (policy_detail::handle_policy(std::get<In>(fx.policies), L, pushed), int())... };
  14941. return pushed;
  14942. }
  14943. static int call(lua_State* L, P& fx) {
  14944. typedef typename P::indices indices;
  14945. return call(indices(), L, fx);
  14946. }
  14947. };
  14948. template <typename T, typename Y, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14949. struct lua_call_wrapper<T, yielding_t<Y>, is_index, is_variable, checked, boost, clean_stack, C> {
  14950. template <typename F>
  14951. static int call(lua_State* L, F&& f) {
  14952. return lua_call_wrapper<T, meta::unqualified_t<Y>, is_index, is_variable, checked, boost, clean_stack>{}.call(L, f.func);
  14953. }
  14954. };
  14955. template <typename T, typename Sig, typename P, bool is_index, bool is_variable, bool checked, int boost, bool clean_stack, typename C>
  14956. struct lua_call_wrapper<T, function_arguments<Sig, P>, is_index, is_variable, checked, boost, clean_stack, C> {
  14957. static int call(lua_State* L, const function_arguments<Sig, P>& f) {
  14958. lua_call_wrapper<T, meta::unqualified_t<P>, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14959. return lcw.call(L, std::get<0>(f.arguments));
  14960. }
  14961. static int call(lua_State* L, function_arguments<Sig, P>&& f) {
  14962. lua_call_wrapper<T, meta::unqualified_t<P>, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14963. return lcw.call(L, std::get<0>(std::move(f.arguments)));
  14964. }
  14965. };
  14966. template <typename T, bool is_index, bool is_variable, int boost = 0, bool checked = detail::default_safe_function_calls, bool clean_stack = true,
  14967. typename Fx, typename... Args>
  14968. inline int call_wrapped(lua_State* L, Fx&& fx, Args&&... args) {
  14969. using uFx = meta::unqualified_t<Fx>;
  14970. if constexpr (meta::is_specialization_of_v<uFx, yielding_t>) {
  14971. using real_fx = meta::unqualified_t<decltype(std::forward<Fx>(fx).func)>;
  14972. lua_call_wrapper<T, real_fx, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14973. return lcw.call(L, std::forward<Fx>(fx).func, std::forward<Args>(args)...);
  14974. }
  14975. else {
  14976. lua_call_wrapper<T, uFx, is_index, is_variable, checked, boost, clean_stack> lcw{};
  14977. return lcw.call(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  14978. }
  14979. }
  14980. template <typename T, bool is_index, bool is_variable, typename F, int start = 1, bool checked = detail::default_safe_function_calls,
  14981. bool clean_stack = true>
  14982. inline int call_user(lua_State* L) {
  14983. auto& fx = stack::unqualified_get<user<F>>(L, upvalue_index(start));
  14984. using uFx = meta::unqualified_t<F>;
  14985. int nr = call_wrapped<T, is_index, is_variable, 0, checked, clean_stack>(L, fx);
  14986. if constexpr (meta::is_specialization_of_v<uFx, yielding_t>) {
  14987. return lua_yield(L, nr);
  14988. }
  14989. else {
  14990. return nr;
  14991. }
  14992. }
  14993. template <typename T, typename = void>
  14994. struct is_var_bind : std::false_type {};
  14995. template <typename T>
  14996. struct is_var_bind<T, std::enable_if_t<std::is_member_object_pointer<T>::value>> : std::true_type {};
  14997. template <typename T>
  14998. struct is_var_bind<T, std::enable_if_t<is_lua_reference_or_proxy<T>::value>> : std::true_type {};
  14999. template <>
  15000. struct is_var_bind<detail::no_prop> : std::true_type {};
  15001. template <typename R, typename W>
  15002. struct is_var_bind<property_wrapper<R, W>> : std::true_type {};
  15003. template <typename T>
  15004. struct is_var_bind<var_wrapper<T>> : std::true_type {};
  15005. template <typename T>
  15006. struct is_var_bind<readonly_wrapper<T>> : is_var_bind<meta::unqualified_t<T>> {};
  15007. template <typename F, typename... Policies>
  15008. struct is_var_bind<policy_wrapper<F, Policies...>> : is_var_bind<meta::unqualified_t<F>> {};
  15009. } // namespace call_detail
  15010. template <typename T>
  15011. struct is_variable_binding : call_detail::is_var_bind<meta::unqualified_t<T>> {};
  15012. template <typename T>
  15013. using is_var_wrapper = meta::is_specialization_of<T, var_wrapper>;
  15014. template <typename T>
  15015. struct is_function_binding : meta::neg<is_variable_binding<T>> {};
  15016. } // namespace sol
  15017. // end of sol/call.hpp
  15018. namespace sol {
  15019. namespace function_detail {
  15020. template <typename F, F fx>
  15021. inline int call_wrapper_variable(std::false_type, lua_State* L) {
  15022. typedef meta::bind_traits<meta::unqualified_t<F>> traits_type;
  15023. typedef typename traits_type::args_list args_list;
  15024. typedef meta::tuple_types<typename traits_type::return_type> return_type;
  15025. return stack::call_into_lua(return_type(), args_list(), L, 1, fx);
  15026. }
  15027. template <typename R, typename V, V, typename T>
  15028. inline int call_set_assignable(std::false_type, T&&, lua_State* L) {
  15029. return luaL_error(L, "cannot write to this type: copy assignment/constructor not available");
  15030. }
  15031. template <typename R, typename V, V variable, typename T>
  15032. inline int call_set_assignable(std::true_type, lua_State* L, T&& mem) {
  15033. (mem.*variable) = stack::get<R>(L, 2);
  15034. return 0;
  15035. }
  15036. template <typename R, typename V, V, typename T>
  15037. inline int call_set_variable(std::false_type, lua_State* L, T&&) {
  15038. return luaL_error(L, "cannot write to a const variable");
  15039. }
  15040. template <typename R, typename V, V variable, typename T>
  15041. inline int call_set_variable(std::true_type, lua_State* L, T&& mem) {
  15042. return call_set_assignable<R, V, variable>(std::is_assignable<std::add_lvalue_reference_t<R>, R>(), L, std::forward<T>(mem));
  15043. }
  15044. template <typename V, V variable>
  15045. inline int call_wrapper_variable(std::true_type, lua_State* L) {
  15046. typedef meta::bind_traits<meta::unqualified_t<V>> traits_type;
  15047. typedef typename traits_type::object_type T;
  15048. typedef typename traits_type::return_type R;
  15049. auto& mem = stack::get<T>(L, 1);
  15050. switch (lua_gettop(L)) {
  15051. case 1: {
  15052. decltype(auto) r = (mem.*variable);
  15053. stack::push_reference(L, std::forward<decltype(r)>(r));
  15054. return 1;
  15055. }
  15056. case 2:
  15057. return call_set_variable<R, V, variable>(meta::neg<std::is_const<R>>(), L, mem);
  15058. default:
  15059. return luaL_error(L, "incorrect number of arguments to member variable function call");
  15060. }
  15061. }
  15062. template <typename F, F fx>
  15063. inline int call_wrapper_function(std::false_type, lua_State* L) {
  15064. return call_wrapper_variable<F, fx>(std::is_member_object_pointer<F>(), L);
  15065. }
  15066. template <typename F, F fx>
  15067. inline int call_wrapper_function(std::true_type, lua_State* L) {
  15068. return call_detail::call_wrapped<void, false, false>(L, fx);
  15069. }
  15070. template <typename F, F fx>
  15071. int call_wrapper_entry(lua_State* L) noexcept(meta::bind_traits<F>::is_noexcept) {
  15072. return call_wrapper_function<F, fx>(std::is_member_function_pointer<meta::unqualified_t<F>>(), L);
  15073. }
  15074. template <typename... Fxs>
  15075. struct c_call_matcher {
  15076. template <typename Fx, std::size_t I, typename R, typename... Args>
  15077. int operator()(types<Fx>, meta::index_value<I>, types<R>, types<Args...>, lua_State* L, int, int) const {
  15078. typedef meta::at_in_pack_t<I, Fxs...> target;
  15079. return target::call(L);
  15080. }
  15081. };
  15082. template <typename F, F fx>
  15083. inline int c_call_raw(std::true_type, lua_State* L) {
  15084. return fx(L);
  15085. }
  15086. template <typename F, F fx>
  15087. inline int c_call_raw(std::false_type, lua_State* L) {
  15088. #ifdef __clang__
  15089. return detail::trampoline(L, function_detail::call_wrapper_entry<F, fx>);
  15090. #else
  15091. return detail::typed_static_trampoline<decltype(&function_detail::call_wrapper_entry<F, fx>), (&function_detail::call_wrapper_entry<F, fx>)>(L);
  15092. #endif // fuck you clang :c
  15093. }
  15094. } // namespace function_detail
  15095. template <typename F, F fx>
  15096. inline int c_call(lua_State* L) {
  15097. typedef meta::unqualified_t<F> Fu;
  15098. typedef std::integral_constant<bool,
  15099. std::is_same<Fu, lua_CFunction>::value
  15100. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  15101. || std::is_same<Fu, detail::lua_CFunction_noexcept>::value
  15102. #endif
  15103. >
  15104. is_raw;
  15105. return function_detail::c_call_raw<F, fx>(is_raw(), L);
  15106. }
  15107. template <typename F, F f>
  15108. struct wrap {
  15109. typedef F type;
  15110. static int call(lua_State* L) {
  15111. return c_call<type, f>(L);
  15112. }
  15113. };
  15114. template <typename... Fxs>
  15115. inline int c_call(lua_State* L) {
  15116. if constexpr (sizeof...(Fxs) < 2) {
  15117. using target = meta::at_in_pack_t<0, Fxs...>;
  15118. return target::call(L);
  15119. }
  15120. else {
  15121. return call_detail::overload_match_arity<typename Fxs::type...>(function_detail::c_call_matcher<Fxs...>(), L, lua_gettop(L), 1);
  15122. }
  15123. }
  15124. } // namespace sol
  15125. // end of sol/function_types_templated.hpp
  15126. // beginning of sol/function_types_stateless.hpp
  15127. namespace sol { namespace function_detail {
  15128. template <typename Function, bool is_yielding>
  15129. struct upvalue_free_function {
  15130. using function_type = std::remove_pointer_t<std::decay_t<Function>>;
  15131. using traits_type = meta::bind_traits<function_type>;
  15132. static int real_call(lua_State* L)
  15133. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15134. // MSVC is broken, what a surprise...
  15135. #else
  15136. noexcept(traits_type::is_noexcept)
  15137. #endif
  15138. {
  15139. auto udata = stack::stack_detail::get_as_upvalues<function_type*>(L);
  15140. function_type* fx = udata.first;
  15141. return call_detail::call_wrapped<void, true, false>(L, fx);
  15142. }
  15143. static int call(lua_State* L) {
  15144. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15145. if (is_yielding) {
  15146. return lua_yield(L, nr);
  15147. }
  15148. else {
  15149. return nr;
  15150. }
  15151. }
  15152. int operator()(lua_State* L) {
  15153. return call(L);
  15154. }
  15155. };
  15156. template <typename T, typename Function, bool is_yielding>
  15157. struct upvalue_member_function {
  15158. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15159. typedef lua_bind_traits<function_type> traits_type;
  15160. static int real_call(lua_State* L)
  15161. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15162. // MSVC is broken, what a surprise...
  15163. #else
  15164. noexcept(traits_type::is_noexcept)
  15165. #endif
  15166. {
  15167. // Layout:
  15168. // idx 1...n: verbatim data of member function pointer
  15169. // idx n + 1: is the object's void pointer
  15170. // We don't need to store the size, because the other side is templated
  15171. // with the same member function pointer type
  15172. function_type& memfx = stack::get<user<function_type>>(L, upvalue_index(2));
  15173. auto& item = *static_cast<T*>(stack::get<void*>(L, upvalue_index(3)));
  15174. return call_detail::call_wrapped<T, true, false, -1>(L, memfx, item);
  15175. }
  15176. static int call(lua_State* L)
  15177. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15178. // MSVC is broken, what a surprise...
  15179. #else
  15180. noexcept(traits_type::is_noexcept)
  15181. #endif
  15182. {
  15183. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15184. if (is_yielding) {
  15185. return lua_yield(L, nr);
  15186. }
  15187. else {
  15188. return nr;
  15189. }
  15190. }
  15191. int operator()(lua_State* L) {
  15192. return call(L);
  15193. }
  15194. };
  15195. template <typename T, typename Function, bool is_yielding>
  15196. struct upvalue_member_variable {
  15197. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15198. typedef lua_bind_traits<function_type> traits_type;
  15199. static int real_call(lua_State* L)
  15200. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15201. // MSVC is broken, what a surprise...
  15202. #else
  15203. noexcept(traits_type::is_noexcept)
  15204. #endif
  15205. {
  15206. // Layout:
  15207. // idx 1...n: verbatim data of member variable pointer
  15208. // idx n + 1: is the object's void pointer
  15209. // We don't need to store the size, because the other side is templated
  15210. // with the same member function pointer type
  15211. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  15212. auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
  15213. auto& mem = *objdata.first;
  15214. function_type& var = memberdata.first;
  15215. switch (lua_gettop(L)) {
  15216. case 0:
  15217. return call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
  15218. case 1:
  15219. return call_detail::call_wrapped<T, false, false, -1>(L, var, mem);
  15220. default:
  15221. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  15222. }
  15223. }
  15224. static int call(lua_State* L)
  15225. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15226. // MSVC is broken, what a surprise...
  15227. #else
  15228. noexcept(traits_type::is_noexcept)
  15229. #endif
  15230. {
  15231. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15232. if (is_yielding) {
  15233. return lua_yield(L, nr);
  15234. }
  15235. else {
  15236. return nr;
  15237. }
  15238. }
  15239. int operator()(lua_State* L) {
  15240. return call(L);
  15241. }
  15242. };
  15243. template <typename T, typename Function, bool is_yielding>
  15244. struct upvalue_member_variable<T, readonly_wrapper<Function>, is_yielding> {
  15245. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15246. typedef lua_bind_traits<function_type> traits_type;
  15247. static int real_call(lua_State* L)
  15248. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15249. // MSVC is broken, what a surprise...
  15250. #else
  15251. noexcept(traits_type::is_noexcept)
  15252. #endif
  15253. {
  15254. // Layout:
  15255. // idx 1...n: verbatim data of member variable pointer
  15256. // idx n + 1: is the object's void pointer
  15257. // We don't need to store the size, because the other side is templated
  15258. // with the same member function pointer type
  15259. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  15260. auto objdata = stack::stack_detail::get_as_upvalues<T*>(L, memberdata.second);
  15261. auto& mem = *objdata.first;
  15262. function_type& var = memberdata.first;
  15263. switch (lua_gettop(L)) {
  15264. case 0:
  15265. return call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
  15266. default:
  15267. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  15268. }
  15269. }
  15270. static int call(lua_State* L)
  15271. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15272. // MSVC is broken, what a surprise...
  15273. #else
  15274. noexcept(traits_type::is_noexcept)
  15275. #endif
  15276. {
  15277. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15278. if (is_yielding) {
  15279. return lua_yield(L, nr);
  15280. }
  15281. else {
  15282. return nr;
  15283. }
  15284. }
  15285. int operator()(lua_State* L) {
  15286. return call(L);
  15287. }
  15288. };
  15289. template <typename T, typename Function, bool is_yielding>
  15290. struct upvalue_this_member_function {
  15291. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15292. typedef lua_bind_traits<function_type> traits_type;
  15293. static int real_call(lua_State* L)
  15294. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15295. // MSVC is broken, what a surprise...
  15296. #else
  15297. noexcept(traits_type::is_noexcept)
  15298. #endif
  15299. {
  15300. // Layout:
  15301. // idx 1...n: verbatim data of member variable pointer
  15302. function_type& memfx = stack::get<user<function_type>>(L, upvalue_index(2));
  15303. return call_detail::call_wrapped<T, false, false>(L, memfx);
  15304. }
  15305. static int call(lua_State* L)
  15306. #if SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  15307. // MSVC is broken, what a surprise...
  15308. #else
  15309. noexcept(traits_type::is_noexcept)
  15310. #endif
  15311. {
  15312. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15313. if (is_yielding) {
  15314. return lua_yield(L, nr);
  15315. }
  15316. else {
  15317. return nr;
  15318. }
  15319. }
  15320. int operator()(lua_State* L) {
  15321. return call(L);
  15322. }
  15323. };
  15324. template <typename T, typename Function, bool is_yielding>
  15325. struct upvalue_this_member_variable {
  15326. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15327. static int real_call(lua_State* L) noexcept(false) {
  15328. // Layout:
  15329. // idx 1...n: verbatim data of member variable pointer
  15330. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  15331. function_type& var = memberdata.first;
  15332. switch (lua_gettop(L)) {
  15333. case 1:
  15334. return call_detail::call_wrapped<T, true, false>(L, var);
  15335. case 2:
  15336. return call_detail::call_wrapped<T, false, false>(L, var);
  15337. default:
  15338. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  15339. }
  15340. }
  15341. static int call(lua_State* L) {
  15342. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15343. if (is_yielding) {
  15344. return lua_yield(L, nr);
  15345. }
  15346. else {
  15347. return nr;
  15348. }
  15349. }
  15350. int operator()(lua_State* L) {
  15351. return call(L);
  15352. }
  15353. };
  15354. template <typename T, typename Function, bool is_yielding>
  15355. struct upvalue_this_member_variable<T, readonly_wrapper<Function>, is_yielding> {
  15356. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15357. typedef lua_bind_traits<function_type> traits_type;
  15358. static int real_call(lua_State* L) noexcept(false) {
  15359. // Layout:
  15360. // idx 1...n: verbatim data of member variable pointer
  15361. auto memberdata = stack::stack_detail::get_as_upvalues<function_type>(L);
  15362. function_type& var = memberdata.first;
  15363. switch (lua_gettop(L)) {
  15364. case 1:
  15365. return call_detail::call_wrapped<T, true, false>(L, var);
  15366. default:
  15367. return luaL_error(L, "sol: incorrect number of arguments to member variable function");
  15368. }
  15369. }
  15370. static int call(lua_State* L) {
  15371. int nr = detail::typed_static_trampoline<decltype(&real_call), (&real_call)>(L);
  15372. if (is_yielding) {
  15373. return lua_yield(L, nr);
  15374. }
  15375. else {
  15376. return nr;
  15377. }
  15378. }
  15379. int operator()(lua_State* L) {
  15380. return call(L);
  15381. }
  15382. };
  15383. }} // namespace sol::function_detail
  15384. // end of sol/function_types_stateless.hpp
  15385. // beginning of sol/function_types_stateful.hpp
  15386. namespace sol {
  15387. namespace function_detail {
  15388. template <typename Func, bool is_yielding, bool no_trampoline>
  15389. struct functor_function {
  15390. typedef std::decay_t<meta::unwrap_unqualified_t<Func>> function_type;
  15391. function_type fx;
  15392. template <typename... Args>
  15393. functor_function(function_type f, Args&&... args)
  15394. : fx(std::move(f), std::forward<Args>(args)...) {
  15395. }
  15396. int call(lua_State* L) {
  15397. int nr = call_detail::call_wrapped<void, true, false>(L, fx);
  15398. if (is_yielding) {
  15399. return lua_yield(L, nr);
  15400. }
  15401. else {
  15402. return nr;
  15403. }
  15404. }
  15405. int operator()(lua_State* L) {
  15406. if (!no_trampoline) {
  15407. auto f = [&](lua_State*) -> int { return this->call(L); };
  15408. return detail::trampoline(L, f);
  15409. }
  15410. else {
  15411. return call(L);
  15412. }
  15413. }
  15414. };
  15415. template <typename T, typename Function, bool is_yielding>
  15416. struct member_function {
  15417. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15418. typedef meta::function_return_t<function_type> return_type;
  15419. typedef meta::function_args_t<function_type> args_lists;
  15420. function_type invocation;
  15421. T member;
  15422. template <typename... Args>
  15423. member_function(function_type f, Args&&... args)
  15424. : invocation(std::move(f)), member(std::forward<Args>(args)...) {
  15425. }
  15426. int call(lua_State* L) {
  15427. int nr = call_detail::call_wrapped<T, true, false, -1>(L, invocation, detail::unwrap(detail::deref(member)));
  15428. if (is_yielding) {
  15429. return lua_yield(L, nr);
  15430. }
  15431. else {
  15432. return nr;
  15433. }
  15434. }
  15435. int operator()(lua_State* L) {
  15436. auto f = [&](lua_State*) -> int { return this->call(L); };
  15437. return detail::trampoline(L, f);
  15438. }
  15439. };
  15440. template <typename T, typename Function, bool is_yielding>
  15441. struct member_variable {
  15442. typedef std::remove_pointer_t<std::decay_t<Function>> function_type;
  15443. typedef typename meta::bind_traits<function_type>::return_type return_type;
  15444. typedef typename meta::bind_traits<function_type>::args_list args_lists;
  15445. function_type var;
  15446. T member;
  15447. typedef std::add_lvalue_reference_t<meta::unwrapped_t<std::remove_reference_t<decltype(detail::deref(member))>>> M;
  15448. template <typename... Args>
  15449. member_variable(function_type v, Args&&... args)
  15450. : var(std::move(v)), member(std::forward<Args>(args)...) {
  15451. }
  15452. int call(lua_State* L) {
  15453. int nr;
  15454. {
  15455. M mem = detail::unwrap(detail::deref(member));
  15456. switch (lua_gettop(L)) {
  15457. case 0:
  15458. nr = call_detail::call_wrapped<T, true, false, -1>(L, var, mem);
  15459. break;
  15460. case 1:
  15461. nr = call_detail::call_wrapped<T, false, false, -1>(L, var, mem);
  15462. break;
  15463. default:
  15464. nr = luaL_error(L, "sol: incorrect number of arguments to member variable function");
  15465. break;
  15466. }
  15467. }
  15468. if (is_yielding) {
  15469. return lua_yield(L, nr);
  15470. }
  15471. else {
  15472. return nr;
  15473. }
  15474. }
  15475. int operator()(lua_State* L) {
  15476. auto f = [&](lua_State*) -> int { return this->call(L); };
  15477. return detail::trampoline(L, f);
  15478. }
  15479. };
  15480. }
  15481. } // namespace sol::function_detail
  15482. // end of sol/function_types_stateful.hpp
  15483. // beginning of sol/function_types_overloaded.hpp
  15484. namespace sol {
  15485. namespace function_detail {
  15486. template <int start_skew, typename... Functions>
  15487. struct overloaded_function {
  15488. typedef std::tuple<Functions...> overload_list;
  15489. typedef std::make_index_sequence<sizeof...(Functions)> indices;
  15490. overload_list overloads;
  15491. overloaded_function(overload_list set)
  15492. : overloads(std::move(set)) {
  15493. }
  15494. overloaded_function(Functions... fxs)
  15495. : overloads(fxs...) {
  15496. }
  15497. template <typename Fx, std::size_t I, typename... R, typename... Args>
  15498. static int call(types<Fx>, meta::index_value<I>, types<R...>, types<Args...>, lua_State* L, int, int, overload_list& ol) {
  15499. auto& func = std::get<I>(ol);
  15500. int nr = call_detail::call_wrapped<void, true, false, start_skew>(L, func);
  15501. return nr;
  15502. }
  15503. int operator()(lua_State* L) {
  15504. auto mfx = [](auto&&... args) { return call(std::forward<decltype(args)>(args)...); };
  15505. return call_detail::overload_match<Functions...>(mfx, L, 1 + start_skew, overloads);
  15506. }
  15507. };
  15508. }
  15509. } // namespace sol::function_detail
  15510. // end of sol/function_types_overloaded.hpp
  15511. // beginning of sol/resolve.hpp
  15512. namespace sol {
  15513. #ifndef __clang__
  15514. // constexpr is fine for not-clang
  15515. namespace detail {
  15516. template <typename R, typename... Args, typename F, typename = std::invoke_result_t<meta::unqualified_t<F>, Args...>>
  15517. inline constexpr auto resolve_i(types<R(Args...)>, F &&) -> R (meta::unqualified_t<F>::*)(Args...) {
  15518. using Sig = R(Args...);
  15519. typedef meta::unqualified_t<F> Fu;
  15520. return static_cast<Sig Fu::*>(&Fu::operator());
  15521. }
  15522. template <typename F, typename U = meta::unqualified_t<F>>
  15523. inline constexpr auto resolve_f(std::true_type, F&& f)
  15524. -> decltype(resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f))) {
  15525. return resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f));
  15526. }
  15527. template <typename F>
  15528. inline constexpr void resolve_f(std::false_type, F&&) {
  15529. static_assert(
  15530. meta::has_deducible_signature<F>::value, "Cannot use no-template-parameter call with an overloaded functor: specify the signature");
  15531. }
  15532. template <typename F, typename U = meta::unqualified_t<F>>
  15533. inline constexpr auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::has_deducible_signature<U>(), std::forward<F>(f))) {
  15534. return resolve_f(meta::has_deducible_signature<U> {}, std::forward<F>(f));
  15535. }
  15536. template <typename... Args, typename F, typename R = std::invoke_result_t<F&, Args...>>
  15537. inline constexpr auto resolve_i(types<Args...>, F&& f) -> decltype(resolve_i(types<R(Args...)>(), std::forward<F>(f))) {
  15538. return resolve_i(types<R(Args...)>(), std::forward<F>(f));
  15539. }
  15540. template <typename Sig, typename C>
  15541. inline constexpr Sig C::*resolve_v(std::false_type, Sig C::*mem_func_ptr) {
  15542. return mem_func_ptr;
  15543. }
  15544. template <typename Sig, typename C>
  15545. inline constexpr Sig C::*resolve_v(std::true_type, Sig C::*mem_variable_ptr) {
  15546. return mem_variable_ptr;
  15547. }
  15548. } // namespace detail
  15549. template <typename... Args, typename R>
  15550. inline constexpr auto resolve(R fun_ptr(Args...)) -> R (*)(Args...) {
  15551. return fun_ptr;
  15552. }
  15553. template <typename Sig>
  15554. inline constexpr Sig* resolve(Sig* fun_ptr) {
  15555. return fun_ptr;
  15556. }
  15557. template <typename... Args, typename R, typename C>
  15558. inline constexpr auto resolve(R (C::*mem_ptr)(Args...)) -> R (C::*)(Args...) {
  15559. return mem_ptr;
  15560. }
  15561. template <typename Sig, typename C>
  15562. inline constexpr Sig C::*resolve(Sig C::*mem_ptr) {
  15563. return detail::resolve_v(std::is_member_object_pointer<Sig C::*>(), mem_ptr);
  15564. }
  15565. template <typename... Sig, typename F, meta::disable<std::is_function<meta::unqualified_t<F>>> = meta::enabler>
  15566. inline constexpr auto resolve(F&& f) -> decltype(detail::resolve_i(types<Sig...>(), std::forward<F>(f))) {
  15567. return detail::resolve_i(types<Sig...>(), std::forward<F>(f));
  15568. }
  15569. #else
  15570. // Clang has distinct problems with constexpr arguments,
  15571. // so don't use the constexpr versions inside of clang.
  15572. namespace detail {
  15573. template <typename R, typename... Args, typename F, typename = std::invoke_result_t<meta::unqualified_t<F>, Args...>>
  15574. inline auto resolve_i(types<R(Args...)>, F &&) -> R (meta::unqualified_t<F>::*)(Args...) {
  15575. using Sig = R(Args...);
  15576. typedef meta::unqualified_t<F> Fu;
  15577. return static_cast<Sig Fu::*>(&Fu::operator());
  15578. }
  15579. template <typename F, typename U = meta::unqualified_t<F>>
  15580. inline auto resolve_f(std::true_type, F&& f)
  15581. -> decltype(resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f))) {
  15582. return resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f));
  15583. }
  15584. template <typename F>
  15585. inline void resolve_f(std::false_type, F&&) {
  15586. static_assert(
  15587. meta::has_deducible_signature<F>::value, "Cannot use no-template-parameter call with an overloaded functor: specify the signature");
  15588. }
  15589. template <typename F, typename U = meta::unqualified_t<F>>
  15590. inline auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::has_deducible_signature<U>(), std::forward<F>(f))) {
  15591. return resolve_f(meta::has_deducible_signature<U> {}, std::forward<F>(f));
  15592. }
  15593. template <typename... Args, typename F, typename R = std::invoke_result_t<F&, Args...>>
  15594. inline auto resolve_i(types<Args...>, F&& f) -> decltype(resolve_i(types<R(Args...)>(), std::forward<F>(f))) {
  15595. return resolve_i(types<R(Args...)>(), std::forward<F>(f));
  15596. }
  15597. template <typename Sig, typename C>
  15598. inline Sig C::*resolve_v(std::false_type, Sig C::*mem_func_ptr) {
  15599. return mem_func_ptr;
  15600. }
  15601. template <typename Sig, typename C>
  15602. inline Sig C::*resolve_v(std::true_type, Sig C::*mem_variable_ptr) {
  15603. return mem_variable_ptr;
  15604. }
  15605. } // namespace detail
  15606. template <typename... Args, typename R>
  15607. inline auto resolve(R fun_ptr(Args...)) -> R (*)(Args...) {
  15608. return fun_ptr;
  15609. }
  15610. template <typename Sig>
  15611. inline Sig* resolve(Sig* fun_ptr) {
  15612. return fun_ptr;
  15613. }
  15614. template <typename... Args, typename R, typename C>
  15615. inline auto resolve(R (C::*mem_ptr)(Args...)) -> R (C::*)(Args...) {
  15616. return mem_ptr;
  15617. }
  15618. template <typename Sig, typename C>
  15619. inline Sig C::*resolve(Sig C::*mem_ptr) {
  15620. return detail::resolve_v(std::is_member_object_pointer<Sig C::*>(), mem_ptr);
  15621. }
  15622. template <typename... Sig, typename F>
  15623. inline auto resolve(F&& f) -> decltype(detail::resolve_i(types<Sig...>(), std::forward<F>(f))) {
  15624. return detail::resolve_i(types<Sig...>(), std::forward<F>(f));
  15625. }
  15626. #endif
  15627. } // namespace sol
  15628. // end of sol/resolve.hpp
  15629. namespace sol {
  15630. namespace function_detail {
  15631. template <typename T>
  15632. struct class_indicator {
  15633. using type = T;
  15634. };
  15635. struct call_indicator { };
  15636. template <bool yielding>
  15637. int lua_c_wrapper(lua_State* L) {
  15638. lua_CFunction cf = lua_tocfunction(L, lua_upvalueindex(2));
  15639. int nr = cf(L);
  15640. if constexpr (yielding) {
  15641. return lua_yield(L, nr);
  15642. }
  15643. else {
  15644. return nr;
  15645. }
  15646. }
  15647. template <bool yielding>
  15648. int lua_c_noexcept_wrapper(lua_State* L) noexcept {
  15649. detail::lua_CFunction_noexcept cf = reinterpret_cast<detail::lua_CFunction_noexcept>(lua_tocfunction(L, lua_upvalueindex(2)));
  15650. int nr = cf(L);
  15651. if constexpr (yielding) {
  15652. return lua_yield(L, nr);
  15653. }
  15654. else {
  15655. return nr;
  15656. }
  15657. }
  15658. struct c_function_invocation { };
  15659. template <bool is_yielding, typename Fx, typename... Args>
  15660. void select(lua_State* L, Fx&& fx, Args&&... args);
  15661. template <bool is_yielding, bool no_trampoline, typename Fx, typename... Args>
  15662. void select_set_fx(lua_State* L, Args&&... args) {
  15663. lua_CFunction freefunc = no_trampoline ? detail::static_trampoline<function_detail::call<meta::unqualified_t<Fx>, 2, is_yielding>>
  15664. : function_detail::call<meta::unqualified_t<Fx>, 2, is_yielding>;
  15665. int upvalues = 0;
  15666. upvalues += stack::push(L, nullptr);
  15667. upvalues += stack::push<user<Fx>>(L, std::forward<Args>(args)...);
  15668. stack::push(L, c_closure(freefunc, upvalues));
  15669. }
  15670. template <bool is_yielding, typename R, typename... A, typename Fx, typename... Args>
  15671. void select_convertible(types<R(A...)>, lua_State* L, Fx&& fx, Args&&... args) {
  15672. using dFx = std::decay_t<meta::unwrap_unqualified_t<Fx>>;
  15673. using fx_ptr_t = R (*)(A...);
  15674. constexpr bool is_convertible = std::is_convertible_v<dFx, fx_ptr_t>;
  15675. if constexpr (is_convertible) {
  15676. fx_ptr_t fxptr = detail::unwrap(std::forward<Fx>(fx));
  15677. select<is_yielding>(L, std::move(fxptr), std::forward<Args>(args)...);
  15678. }
  15679. else {
  15680. using F = function_detail::functor_function<dFx, false, true>;
  15681. select_set_fx<is_yielding, false, F>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15682. }
  15683. }
  15684. template <bool is_yielding, typename Fx, typename... Args>
  15685. void select_convertible(types<>, lua_State* L, Fx&& fx, Args&&... args) {
  15686. typedef meta::function_signature_t<meta::unwrap_unqualified_t<Fx>> Sig;
  15687. select_convertible<is_yielding>(types<Sig>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15688. }
  15689. template <bool is_yielding, typename Fx, typename... Args>
  15690. void select_member_variable(lua_State* L, Fx&& fx, Args&&... args) {
  15691. using uFx = meta::unqualified_t<Fx>;
  15692. if constexpr (sizeof...(Args) < 1) {
  15693. using C = typename meta::bind_traits<uFx>::object_type;
  15694. lua_CFunction freefunc = &function_detail::upvalue_this_member_variable<C, Fx, is_yielding>::call;
  15695. int upvalues = 0;
  15696. upvalues += stack::push(L, nullptr);
  15697. upvalues += stack::stack_detail::push_as_upvalues(L, fx);
  15698. stack::push(L, c_closure(freefunc, upvalues));
  15699. }
  15700. else if constexpr (sizeof...(Args) < 2) {
  15701. using Tu = typename meta::meta_detail::unqualified_non_alias<Args...>::type;
  15702. constexpr bool is_reference = meta::is_specialization_of_v<Tu, std::reference_wrapper> || std::is_pointer_v<Tu>;
  15703. if constexpr (meta::is_specialization_of_v<Tu, function_detail::class_indicator>) {
  15704. lua_CFunction freefunc = &function_detail::upvalue_this_member_variable<typename Tu::type, Fx, is_yielding>::call;
  15705. int upvalues = 0;
  15706. upvalues += stack::push(L, nullptr);
  15707. upvalues += stack::stack_detail::push_as_upvalues(L, fx);
  15708. stack::push(L, c_closure(freefunc, upvalues));
  15709. }
  15710. else if constexpr (is_reference) {
  15711. typedef std::decay_t<Fx> dFx;
  15712. dFx memfxptr(std::forward<Fx>(fx));
  15713. auto userptr = detail::ptr(std::forward<Args>(args)...);
  15714. lua_CFunction freefunc
  15715. = &function_detail::upvalue_member_variable<std::decay_t<decltype(*userptr)>, meta::unqualified_t<Fx>, is_yielding>::call;
  15716. int upvalues = 0;
  15717. upvalues += stack::push(L, nullptr);
  15718. upvalues += stack::stack_detail::push_as_upvalues(L, memfxptr);
  15719. upvalues += stack::push(L, static_cast<void const*>(userptr));
  15720. stack::push(L, c_closure(freefunc, upvalues));
  15721. }
  15722. else {
  15723. using clean_fx = std::remove_pointer_t<std::decay_t<Fx>>;
  15724. using F = function_detail::member_variable<Tu, clean_fx, is_yielding>;
  15725. select_set_fx<false, false, F>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15726. }
  15727. }
  15728. else {
  15729. using C = typename meta::bind_traits<uFx>::object_type;
  15730. using clean_fx = std::remove_pointer_t<std::decay_t<Fx>>;
  15731. using F = function_detail::member_variable<C, clean_fx, is_yielding>;
  15732. select_set_fx<false, false, F>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15733. }
  15734. }
  15735. template <bool is_yielding, typename Fx, typename T, typename... Args>
  15736. void select_member_function_with(lua_State* L, Fx&& fx, T&& obj, Args&&... args) {
  15737. using dFx = std::decay_t<Fx>;
  15738. using Tu = meta::unqualified_t<T>;
  15739. if constexpr (meta::is_specialization_of_v<Tu, function_detail::class_indicator>) {
  15740. (void)obj;
  15741. using C = typename Tu::type;
  15742. lua_CFunction freefunc = &function_detail::upvalue_this_member_function<C, dFx, is_yielding>::call;
  15743. int upvalues = 0;
  15744. upvalues += stack::push(L, nullptr);
  15745. upvalues += stack::push<user<dFx>>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15746. stack::push(L, c_closure(freefunc, upvalues));
  15747. }
  15748. else {
  15749. constexpr bool is_reference = meta::is_specialization_of_v<Tu, std::reference_wrapper> || std::is_pointer_v<Tu>;
  15750. if constexpr (is_reference) {
  15751. auto userptr = detail::ptr(std::forward<T>(obj));
  15752. lua_CFunction freefunc = &function_detail::upvalue_member_function<std::decay_t<decltype(*userptr)>, dFx, is_yielding>::call;
  15753. int upvalues = 0;
  15754. upvalues += stack::push(L, nullptr);
  15755. upvalues += stack::push<user<dFx>>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15756. upvalues += stack::push(L, lightuserdata_value(static_cast<void*>(userptr)));
  15757. stack::push(L, c_closure(freefunc, upvalues));
  15758. }
  15759. else {
  15760. using F = function_detail::member_function<Tu, dFx, is_yielding>;
  15761. select_set_fx<false, false, F>(L, std::forward<Fx>(fx), std::forward<T>(obj), std::forward<Args>(args)...);
  15762. }
  15763. }
  15764. }
  15765. template <bool is_yielding, typename Fx, typename... Args>
  15766. void select_member_function(lua_State* L, Fx&& fx, Args&&... args) {
  15767. using dFx = std::decay_t<Fx>;
  15768. if constexpr (sizeof...(Args) < 1) {
  15769. using C = typename meta::bind_traits<meta::unqualified_t<Fx>>::object_type;
  15770. lua_CFunction freefunc = &function_detail::upvalue_this_member_function<C, dFx, is_yielding>::call;
  15771. int upvalues = 0;
  15772. upvalues += stack::push(L, nullptr);
  15773. upvalues += stack::push<user<dFx>>(L, std::forward<Fx>(fx));
  15774. stack::push(L, c_closure(freefunc, upvalues));
  15775. }
  15776. else {
  15777. select_member_function_with<is_yielding>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15778. }
  15779. }
  15780. template <bool is_yielding, typename Fx, typename... Args>
  15781. void select(lua_State* L, Fx&& fx, Args&&... args) {
  15782. using uFx = meta::unqualified_t<Fx>;
  15783. if constexpr (is_lua_reference_v<uFx>) {
  15784. // TODO: hoist into lambda in this case for yielding???
  15785. stack::push(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15786. }
  15787. else if constexpr (is_lua_c_function_v<uFx>) {
  15788. int upvalues = 0;
  15789. upvalues += stack::push(L, nullptr);
  15790. upvalues += stack::push(L, std::forward<Fx>(fx));
  15791. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  15792. if constexpr (std::is_nothrow_invocable_r_v<int, uFx, lua_State*>) {
  15793. detail::lua_CFunction_noexcept cf = &lua_c_noexcept_wrapper<is_yielding>;
  15794. lua_pushcclosure(L, reinterpret_cast<lua_CFunction>(cf), 2);
  15795. }
  15796. else {
  15797. lua_CFunction cf = &lua_c_wrapper<is_yielding>;
  15798. lua_pushcclosure(L, cf, 2);
  15799. }
  15800. #else
  15801. lua_CFunction cf = &function_detail::lua_c_wrapper<is_yielding>;
  15802. lua_pushcclosure(L, cf, 2);
  15803. #endif
  15804. }
  15805. else if constexpr (std::is_function_v<std::remove_pointer_t<uFx>>) {
  15806. std::decay_t<Fx> target(std::forward<Fx>(fx), std::forward<Args>(args)...);
  15807. lua_CFunction freefunc = &function_detail::upvalue_free_function<Fx, is_yielding>::call;
  15808. int upvalues = 0;
  15809. upvalues += stack::push(L, nullptr);
  15810. upvalues += stack::stack_detail::push_as_upvalues(L, target);
  15811. stack::push(L, c_closure(freefunc, upvalues));
  15812. }
  15813. else if constexpr (std::is_member_function_pointer_v<uFx>) {
  15814. select_member_function<is_yielding>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15815. }
  15816. else if constexpr (meta::is_member_object_v<uFx>) {
  15817. select_member_variable<is_yielding>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15818. }
  15819. else {
  15820. select_convertible<is_yielding>(types<>(), L, std::forward<Fx>(fx), std::forward<Args>(args)...);
  15821. }
  15822. }
  15823. } // namespace function_detail
  15824. namespace stack {
  15825. template <typename... Sigs>
  15826. struct unqualified_pusher<function_sig<Sigs...>> {
  15827. template <bool is_yielding, typename Arg0, typename... Args>
  15828. static int push(lua_State* L, Arg0&& arg0, Args&&... args) {
  15829. if constexpr (meta::is_specialization_of_v<meta::unqualified_t<Arg0>, std::function>) {
  15830. if constexpr (is_yielding) {
  15831. return stack::push<meta::unqualified_t<Arg0>>(L, detail::yield_tag, std::forward<Arg0>(arg0), std::forward<Args>(args)...);
  15832. }
  15833. else {
  15834. return stack::push(L, std::forward<Arg0>(arg0), std::forward<Args>(args)...);
  15835. }
  15836. }
  15837. else {
  15838. function_detail::select<is_yielding>(L, std::forward<Arg0>(arg0), std::forward<Args>(args)...);
  15839. return 1;
  15840. }
  15841. }
  15842. template <typename Arg0, typename... Args>
  15843. static int push(lua_State* L, Arg0&& arg0, Args&&... args) {
  15844. if constexpr (std::is_same_v<meta::unqualified_t<Arg0>, detail::yield_tag_t>) {
  15845. push<true>(L, std::forward<Args>(args)...);
  15846. }
  15847. else if constexpr (meta::is_specialization_of_v<meta::unqualified_t<Arg0>, yielding_t>) {
  15848. push<true>(L, std::forward<Arg0>(arg0).func, std::forward<Args>(args)...);
  15849. }
  15850. else {
  15851. push<false>(L, std::forward<Arg0>(arg0), std::forward<Args>(args)...);
  15852. }
  15853. return 1;
  15854. }
  15855. };
  15856. template <typename T>
  15857. struct unqualified_pusher<yielding_t<T>> {
  15858. template <typename... Args>
  15859. static int push(lua_State* L, const yielding_t<T>& f, Args&&... args) {
  15860. if constexpr (meta::is_specialization_of_v<meta::unqualified_t<T>, std::function>) {
  15861. return stack::push<T>(L, detail::yield_tag, f.func, std::forward<Args>(args)...);
  15862. }
  15863. else {
  15864. function_detail::select<true>(L, f.func, std::forward<Args>(args)...);
  15865. return 1;
  15866. }
  15867. }
  15868. template <typename... Args>
  15869. static int push(lua_State* L, yielding_t<T>&& f, Args&&... args) {
  15870. if constexpr (meta::is_specialization_of_v<meta::unqualified_t<T>, std::function>) {
  15871. return stack::push<T>(L, detail::yield_tag, std::move(f.func), std::forward<Args>(args)...);
  15872. }
  15873. else {
  15874. function_detail::select<true>(L, std::move(f.func), std::forward<Args>(args)...);
  15875. return 1;
  15876. }
  15877. }
  15878. };
  15879. template <typename T, typename... Args>
  15880. struct unqualified_pusher<function_arguments<T, Args...>> {
  15881. template <std::size_t... I, typename FP>
  15882. static int push_func(std::index_sequence<I...>, lua_State* L, FP&& fp) {
  15883. return stack::push<T>(L, std::get<I>(std::forward<FP>(fp).arguments)...);
  15884. }
  15885. static int push(lua_State* L, const function_arguments<T, Args...>& fp) {
  15886. return push_func(std::make_index_sequence<sizeof...(Args)>(), L, fp);
  15887. }
  15888. static int push(lua_State* L, function_arguments<T, Args...>&& fp) {
  15889. return push_func(std::make_index_sequence<sizeof...(Args)>(), L, std::move(fp));
  15890. }
  15891. };
  15892. template <typename Signature>
  15893. struct unqualified_pusher<std::function<Signature>> {
  15894. static int push(lua_State* L, detail::yield_tag_t, const std::function<Signature>& fx) {
  15895. if (fx) {
  15896. function_detail::select<true>(L, fx);
  15897. return 1;
  15898. }
  15899. return stack::push(L, lua_nil);
  15900. }
  15901. static int push(lua_State* L, detail::yield_tag_t, std::function<Signature>&& fx) {
  15902. if (fx) {
  15903. function_detail::select<true>(L, std::move(fx));
  15904. return 1;
  15905. }
  15906. return stack::push(L, lua_nil);
  15907. }
  15908. static int push(lua_State* L, const std::function<Signature>& fx) {
  15909. if (fx) {
  15910. function_detail::select<false>(L, fx);
  15911. return 1;
  15912. }
  15913. return stack::push(L, lua_nil);
  15914. }
  15915. static int push(lua_State* L, std::function<Signature>&& fx) {
  15916. if (fx) {
  15917. function_detail::select<false>(L, std::move(fx));
  15918. return 1;
  15919. }
  15920. return stack::push(L, lua_nil);
  15921. }
  15922. };
  15923. template <typename Signature>
  15924. struct unqualified_pusher<Signature, std::enable_if_t<std::is_member_pointer<Signature>::value>> {
  15925. template <typename... Args>
  15926. static int push(lua_State* L, Args&&... args) {
  15927. function_detail::select<false>(L, std::forward<Args>(args)...);
  15928. return 1;
  15929. }
  15930. };
  15931. template <typename Signature>
  15932. struct unqualified_pusher<Signature,
  15933. std::enable_if_t<meta::all<std::is_function<std::remove_pointer_t<Signature>>, meta::neg<std::is_same<Signature, lua_CFunction>>,
  15934. meta::neg<std::is_same<Signature, std::remove_pointer_t<lua_CFunction>>>
  15935. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE_I_)
  15936. ,
  15937. meta::neg<std::is_same<Signature, detail::lua_CFunction_noexcept>>,
  15938. meta::neg<std::is_same<Signature, std::remove_pointer_t<detail::lua_CFunction_noexcept>>>
  15939. #endif // noexcept function types
  15940. >::value>> {
  15941. template <typename F>
  15942. static int push(lua_State* L, F&& f) {
  15943. function_detail::select<false>(L, std::forward<F>(f));
  15944. return 1;
  15945. }
  15946. };
  15947. template <typename... Functions>
  15948. struct unqualified_pusher<overload_set<Functions...>> {
  15949. static int push(lua_State* L, overload_set<Functions...>&& set) {
  15950. using F = function_detail::overloaded_function<0, Functions...>;
  15951. function_detail::select_set_fx<false, false, F>(L, std::move(set.functions));
  15952. return 1;
  15953. }
  15954. static int push(lua_State* L, const overload_set<Functions...>& set) {
  15955. using F = function_detail::overloaded_function<0, Functions...>;
  15956. function_detail::select_set_fx<false, false, F>(L, set.functions);
  15957. return 1;
  15958. }
  15959. };
  15960. template <typename T>
  15961. struct unqualified_pusher<protect_t<T>> {
  15962. static int push(lua_State* L, protect_t<T>&& pw) {
  15963. lua_CFunction cf = call_detail::call_user<void, false, false, protect_t<T>, 2>;
  15964. int upvalues = 0;
  15965. upvalues += stack::push(L, nullptr);
  15966. upvalues += stack::push<user<protect_t<T>>>(L, std::move(pw.value));
  15967. return stack::push(L, c_closure(cf, upvalues));
  15968. }
  15969. static int push(lua_State* L, const protect_t<T>& pw) {
  15970. lua_CFunction cf = call_detail::call_user<void, false, false, protect_t<T>, 2>;
  15971. int upvalues = 0;
  15972. upvalues += stack::push(L, nullptr);
  15973. upvalues += stack::push<user<protect_t<T>>>(L, pw.value);
  15974. return stack::push(L, c_closure(cf, upvalues));
  15975. }
  15976. };
  15977. template <typename F, typename G>
  15978. struct unqualified_pusher<property_wrapper<F, G>> {
  15979. static int push(lua_State* L, property_wrapper<F, G>&& pw) {
  15980. if constexpr (std::is_void_v<F>) {
  15981. return stack::push(L, std::move(pw.write()));
  15982. }
  15983. else if constexpr (std::is_void_v<G>) {
  15984. return stack::push(L, std::move(pw.read()));
  15985. }
  15986. else {
  15987. return stack::push(L, overload(std::move(pw.read()), std::move(pw.write())));
  15988. }
  15989. }
  15990. static int push(lua_State* L, const property_wrapper<F, G>& pw) {
  15991. if constexpr (std::is_void_v<F>) {
  15992. return stack::push(L, pw.write);
  15993. }
  15994. else if constexpr (std::is_void_v<G>) {
  15995. return stack::push(L, pw.read);
  15996. }
  15997. else {
  15998. return stack::push(L, overload(pw.read, pw.write));
  15999. }
  16000. }
  16001. };
  16002. template <typename T>
  16003. struct unqualified_pusher<var_wrapper<T>> {
  16004. static int push(lua_State* L, var_wrapper<T>&& vw) {
  16005. return stack::push(L, std::move(vw.value()));
  16006. }
  16007. static int push(lua_State* L, const var_wrapper<T>& vw) {
  16008. return stack::push(L, vw.value());
  16009. }
  16010. };
  16011. template <typename... Functions>
  16012. struct unqualified_pusher<factory_wrapper<Functions...>> {
  16013. static int push(lua_State* L, const factory_wrapper<Functions...>& fw) {
  16014. using F = function_detail::overloaded_function<0, Functions...>;
  16015. function_detail::select_set_fx<false, false, F>(L, fw.functions);
  16016. return 1;
  16017. }
  16018. static int push(lua_State* L, factory_wrapper<Functions...>&& fw) {
  16019. using F = function_detail::overloaded_function<0, Functions...>;
  16020. function_detail::select_set_fx<false, false, F>(L, std::move(fw.functions));
  16021. return 1;
  16022. }
  16023. static int push(lua_State* L, const factory_wrapper<Functions...>& fw, function_detail::call_indicator) {
  16024. using F = function_detail::overloaded_function<1, Functions...>;
  16025. function_detail::select_set_fx<false, false, F>(L, fw.functions);
  16026. return 1;
  16027. }
  16028. static int push(lua_State* L, factory_wrapper<Functions...>&& fw, function_detail::call_indicator) {
  16029. using F = function_detail::overloaded_function<1, Functions...>;
  16030. function_detail::select_set_fx<false, false, F>(L, std::move(fw.functions));
  16031. return 1;
  16032. }
  16033. };
  16034. template <>
  16035. struct unqualified_pusher<no_construction> {
  16036. static int push(lua_State* L, no_construction) {
  16037. lua_CFunction cf = &function_detail::no_construction_error;
  16038. return stack::push(L, cf);
  16039. }
  16040. static int push(lua_State* L, no_construction c, function_detail::call_indicator) {
  16041. return push(L, c);
  16042. }
  16043. };
  16044. template <typename T>
  16045. struct unqualified_pusher<detail::tagged<T, no_construction>> {
  16046. static int push(lua_State* L, detail::tagged<T, no_construction>) {
  16047. lua_CFunction cf = &function_detail::no_construction_error;
  16048. return stack::push(L, cf);
  16049. }
  16050. static int push(lua_State* L, no_construction c, function_detail::call_indicator) {
  16051. return push(L, c);
  16052. }
  16053. };
  16054. template <typename T, typename... Lists>
  16055. struct unqualified_pusher<detail::tagged<T, constructor_list<Lists...>>> {
  16056. static int push(lua_State* L, detail::tagged<T, constructor_list<Lists...>>) {
  16057. lua_CFunction cf = call_detail::construct<T, detail::default_safe_function_calls, true, Lists...>;
  16058. return stack::push(L, cf);
  16059. }
  16060. static int push(lua_State* L, constructor_list<Lists...>) {
  16061. lua_CFunction cf = call_detail::construct<T, detail::default_safe_function_calls, true, Lists...>;
  16062. return stack::push(L, cf);
  16063. }
  16064. };
  16065. template <typename L0, typename... Lists>
  16066. struct unqualified_pusher<constructor_list<L0, Lists...>> {
  16067. typedef constructor_list<L0, Lists...> cl_t;
  16068. static int push(lua_State* L, cl_t cl) {
  16069. typedef typename meta::bind_traits<L0>::return_type T;
  16070. return stack::push<detail::tagged<T, cl_t>>(L, cl);
  16071. }
  16072. };
  16073. template <typename T, typename... Fxs>
  16074. struct unqualified_pusher<detail::tagged<T, constructor_wrapper<Fxs...>>> {
  16075. static int push(lua_State* L, detail::tagged<T, constructor_wrapper<Fxs...>>&& c) {
  16076. return push(L, std::move(c.value()));
  16077. }
  16078. static int push(lua_State* L, const detail::tagged<T, const constructor_wrapper<Fxs...>>& c) {
  16079. return push(L, c.value());
  16080. }
  16081. static int push(lua_State* L, constructor_wrapper<Fxs...>&& c) {
  16082. lua_CFunction cf = call_detail::call_user<T, false, false, constructor_wrapper<Fxs...>, 2>;
  16083. int upvalues = 0;
  16084. upvalues += stack::push(L, nullptr);
  16085. upvalues += stack::push<user<constructor_wrapper<Fxs...>>>(L, std::move(c));
  16086. return stack::push(L, c_closure(cf, upvalues));
  16087. }
  16088. static int push(lua_State* L, const constructor_wrapper<Fxs...>& c) {
  16089. lua_CFunction cf = call_detail::call_user<T, false, false, constructor_wrapper<Fxs...>, 2>;
  16090. int upvalues = 0;
  16091. upvalues += stack::push(L, nullptr);
  16092. upvalues += stack::push<user<constructor_wrapper<Fxs...>>>(L, c);
  16093. return stack::push(L, c_closure(cf, upvalues));
  16094. }
  16095. };
  16096. template <typename F, typename... Fxs>
  16097. struct unqualified_pusher<constructor_wrapper<F, Fxs...>> {
  16098. static int push(lua_State* L, const constructor_wrapper<F, Fxs...>& c) {
  16099. typedef typename meta::bind_traits<F>::template arg_at<0> arg0;
  16100. typedef meta::unqualified_t<std::remove_pointer_t<arg0>> T;
  16101. return stack::push<detail::tagged<T, constructor_wrapper<F, Fxs...>>>(L, c);
  16102. }
  16103. static int push(lua_State* L, constructor_wrapper<F, Fxs...>&& c) {
  16104. typedef typename meta::bind_traits<F>::template arg_at<0> arg0;
  16105. typedef meta::unqualified_t<std::remove_pointer_t<arg0>> T;
  16106. return stack::push<detail::tagged<T, constructor_wrapper<F, Fxs...>>>(L, std::move(c));
  16107. }
  16108. };
  16109. template <typename T>
  16110. struct unqualified_pusher<detail::tagged<T, destructor_wrapper<void>>> {
  16111. static int push(lua_State* L, destructor_wrapper<void>) {
  16112. lua_CFunction cf = detail::usertype_alloc_destruct<T>;
  16113. return stack::push(L, cf);
  16114. }
  16115. };
  16116. template <typename T, typename Fx>
  16117. struct unqualified_pusher<detail::tagged<T, destructor_wrapper<Fx>>> {
  16118. static int push(lua_State* L, destructor_wrapper<Fx>&& c) {
  16119. lua_CFunction cf = call_detail::call_user<T, false, false, destructor_wrapper<Fx>, 2>;
  16120. int upvalues = 0;
  16121. upvalues += stack::push(L, nullptr);
  16122. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, std::move(c));
  16123. return stack::push(L, c_closure(cf, upvalues));
  16124. }
  16125. static int push(lua_State* L, const destructor_wrapper<Fx>& c) {
  16126. lua_CFunction cf = call_detail::call_user<T, false, false, destructor_wrapper<Fx>, 2>;
  16127. int upvalues = 0;
  16128. upvalues += stack::push(L, nullptr);
  16129. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, c);
  16130. return stack::push(L, c_closure(cf, upvalues));
  16131. }
  16132. };
  16133. template <typename Fx>
  16134. struct unqualified_pusher<destructor_wrapper<Fx>> {
  16135. static int push(lua_State* L, destructor_wrapper<Fx>&& c) {
  16136. lua_CFunction cf = call_detail::call_user<void, false, false, destructor_wrapper<Fx>, 2>;
  16137. int upvalues = 0;
  16138. upvalues += stack::push(L, nullptr);
  16139. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, std::move(c));
  16140. return stack::push(L, c_closure(cf, upvalues));
  16141. }
  16142. static int push(lua_State* L, const destructor_wrapper<Fx>& c) {
  16143. lua_CFunction cf = call_detail::call_user<void, false, false, destructor_wrapper<Fx>, 2>;
  16144. int upvalues = 0;
  16145. upvalues += stack::push(L, nullptr);
  16146. upvalues += stack::push<user<destructor_wrapper<Fx>>>(L, c);
  16147. return stack::push(L, c_closure(cf, upvalues));
  16148. }
  16149. };
  16150. template <typename F, typename... Policies>
  16151. struct unqualified_pusher<policy_wrapper<F, Policies...>> {
  16152. using P = policy_wrapper<F, Policies...>;
  16153. static int push(lua_State* L, const P& p) {
  16154. lua_CFunction cf = call_detail::call_user<void, false, false, P, 2>;
  16155. int upvalues = 0;
  16156. upvalues += stack::push(L, nullptr);
  16157. upvalues += stack::push<user<P>>(L, p);
  16158. return stack::push(L, c_closure(cf, upvalues));
  16159. }
  16160. static int push(lua_State* L, P&& p) {
  16161. lua_CFunction cf = call_detail::call_user<void, false, false, P, 2>;
  16162. int upvalues = 0;
  16163. upvalues += stack::push(L, nullptr);
  16164. upvalues += stack::push<user<P>>(L, std::move(p));
  16165. return stack::push(L, c_closure(cf, upvalues));
  16166. }
  16167. };
  16168. template <typename T, typename F, typename... Policies>
  16169. struct unqualified_pusher<detail::tagged<T, policy_wrapper<F, Policies...>>> {
  16170. using P = policy_wrapper<F, Policies...>;
  16171. using Tagged = detail::tagged<T, P>;
  16172. static int push(lua_State* L, const Tagged& p) {
  16173. lua_CFunction cf = call_detail::call_user<T, false, false, P, 2>;
  16174. int upvalues = 0;
  16175. upvalues += stack::push(L, nullptr);
  16176. upvalues += stack::push<user<P>>(L, p.value());
  16177. return stack::push(L, c_closure(cf, upvalues));
  16178. }
  16179. static int push(lua_State* L, Tagged&& p) {
  16180. lua_CFunction cf = call_detail::call_user<T, false, false, P, 2>;
  16181. int upvalues = 0;
  16182. upvalues += stack::push(L, nullptr);
  16183. upvalues += stack::push<user<P>>(L, std::move(p.value()));
  16184. return stack::push(L, c_closure(cf, upvalues));
  16185. }
  16186. };
  16187. template <typename T>
  16188. struct unqualified_pusher<push_invoke_t<T>> {
  16189. static int push(lua_State* L, push_invoke_t<T>&& pi) {
  16190. if constexpr (std::is_invocable_v<std::add_rvalue_reference_t<T>, lua_State*>) {
  16191. return stack::push(L, std::move(pi.value())(L));
  16192. }
  16193. else {
  16194. return stack::push(L, std::move(pi.value())());
  16195. }
  16196. }
  16197. static int push(lua_State* L, const push_invoke_t<T>& pi) {
  16198. if constexpr (std::is_invocable_v<const T, lua_State*>) {
  16199. return stack::push(L, pi.value()(L));
  16200. }
  16201. else {
  16202. return stack::push(L, pi.value()());
  16203. }
  16204. }
  16205. };
  16206. namespace stack_detail {
  16207. template <typename Function, typename Handler>
  16208. bool check_function_pointer(lua_State* L, int index, Handler&& handler, record& tracking) noexcept {
  16209. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  16210. tracking.use(1);
  16211. bool success = lua_iscfunction(L, index) == 1;
  16212. if (success) {
  16213. // there must be at LEAST 2 upvalues; otherwise, we didn't serialize it.
  16214. const char* upvalue_name = lua_getupvalue(L, index, 2);
  16215. lua_pop(L, 1);
  16216. success = upvalue_name != nullptr;
  16217. }
  16218. if (!success) {
  16219. // expected type, actual type
  16220. handler(
  16221. L, index, type::function, type_of(L, index), "type must be a Lua C Function gotten from a function pointer serialized by sol2");
  16222. }
  16223. return success;
  16224. #else
  16225. return false;
  16226. #endif
  16227. }
  16228. template <typename Function>
  16229. Function* get_function_pointer(lua_State* L, int index, record& tracking) noexcept {
  16230. #if SOL_IS_ON(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  16231. tracking.use(1);
  16232. auto udata = stack::stack_detail::get_as_upvalues_using_function<Function*>(L, index);
  16233. Function* fx = udata.first;
  16234. return fx;
  16235. #else
  16236. static_assert(meta::meta_detail::always_true<Function>::value,
  16237. #if SOL_IS_DEFAULT_OFF(SOL_GET_FUNCTION_POINTER_UNSAFE_I_)
  16238. "You are attempting to retrieve a function pointer type. "
  16239. "This is inherently unsafe in sol2. In order to do this, you must turn on the "
  16240. "SOL_GET_FUNCTION_POINTER_UNSAFE configuration macro, as detailed in the documentation. "
  16241. "Please be careful!"
  16242. #else
  16243. "You are attempting to retrieve a function pointer type. "
  16244. "You explicitly turned off the ability to do this by defining "
  16245. "SOL_GET_FUNCTION_POINTER_UNSAFE or similar to be off. "
  16246. "Please reconsider this!"
  16247. #endif
  16248. );
  16249. return nullptr;
  16250. #endif
  16251. }
  16252. } // namespace stack_detail
  16253. } // namespace stack
  16254. } // namespace sol
  16255. // end of sol/function_types.hpp
  16256. // beginning of sol/dump_handler.hpp
  16257. #include <cstdint>
  16258. #include <exception>
  16259. namespace sol {
  16260. class dump_error : public error {
  16261. private:
  16262. int ec_;
  16263. public:
  16264. dump_error(int error_code_) : error("dump returned non-zero error of " + std::to_string(error_code_)), ec_(error_code_) {
  16265. }
  16266. int error_code() const {
  16267. return ec_;
  16268. }
  16269. };
  16270. inline int dump_pass_on_error(lua_State* L, int result_code, lua_Writer writer_function, void* userdata, bool strip) {
  16271. (void)L;
  16272. (void)writer_function;
  16273. (void)userdata;
  16274. (void)strip;
  16275. return result_code;
  16276. }
  16277. inline int dump_panic_on_error(lua_State* L, int result_code, lua_Writer writer_function, void* userdata, bool strip) {
  16278. (void)L;
  16279. (void)writer_function;
  16280. (void)userdata;
  16281. (void)strip;
  16282. return luaL_error(L, "a non-zero error code (%d) was returned by the lua_Writer for the dump function", result_code);
  16283. }
  16284. inline int dump_throw_on_error(lua_State* L, int result_code, lua_Writer writer_function, void* userdata, bool strip) {
  16285. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  16286. return dump_panic_on_error(L, result_code, writer_function, userdata, strip);
  16287. #else
  16288. (void)L;
  16289. (void)writer_function;
  16290. (void)userdata;
  16291. (void)strip;
  16292. throw dump_error(result_code);
  16293. #endif // no exceptions stuff
  16294. }
  16295. } // namespace sol
  16296. // end of sol/dump_handler.hpp
  16297. #include <cstdint>
  16298. namespace sol {
  16299. template <typename ref_t, bool aligned = false>
  16300. class basic_function : public basic_object<ref_t> {
  16301. private:
  16302. using base_t = basic_object<ref_t>;
  16303. void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
  16304. lua_call(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount));
  16305. }
  16306. template <std::size_t... I, typename... Ret>
  16307. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n) const {
  16308. luacall(n, lua_size<std::tuple<Ret...>>::value);
  16309. return stack::pop<std::tuple<Ret...>>(lua_state());
  16310. }
  16311. template <std::size_t I, typename Ret, meta::enable<meta::neg<std::is_void<Ret>>> = meta::enabler>
  16312. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n) const {
  16313. luacall(n, lua_size<Ret>::value);
  16314. return stack::pop<Ret>(lua_state());
  16315. }
  16316. template <std::size_t I>
  16317. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n) const {
  16318. luacall(n, 0);
  16319. }
  16320. unsafe_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) const {
  16321. int stacksize = lua_gettop(lua_state());
  16322. int firstreturn = (std::max)(1, stacksize - static_cast<int>(n));
  16323. luacall(n, LUA_MULTRET);
  16324. int poststacksize = lua_gettop(lua_state());
  16325. int returncount = poststacksize - (firstreturn - 1);
  16326. return unsafe_function_result(lua_state(), firstreturn, returncount);
  16327. }
  16328. public:
  16329. using base_t::lua_state;
  16330. basic_function() = default;
  16331. 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>
  16332. basic_function(T&& r) noexcept
  16333. : base_t(std::forward<T>(r)) {
  16334. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16335. if (!is_function<meta::unqualified_t<T>>::value) {
  16336. auto pp = stack::push_pop(*this);
  16337. constructor_handler handler{};
  16338. stack::check<basic_function>(lua_state(), -1, handler);
  16339. }
  16340. #endif // Safety
  16341. }
  16342. basic_function(const basic_function&) = default;
  16343. basic_function& operator=(const basic_function&) = default;
  16344. basic_function(basic_function&&) = default;
  16345. basic_function& operator=(basic_function&&) = default;
  16346. basic_function(const stack_reference& r)
  16347. : basic_function(r.lua_state(), r.stack_index()) {
  16348. }
  16349. basic_function(stack_reference&& r)
  16350. : basic_function(r.lua_state(), r.stack_index()) {
  16351. }
  16352. basic_function(lua_nil_t n)
  16353. : base_t(n) {
  16354. }
  16355. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  16356. basic_function(lua_State* L, T&& r)
  16357. : base_t(L, std::forward<T>(r)) {
  16358. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16359. auto pp = stack::push_pop(*this);
  16360. constructor_handler handler{};
  16361. stack::check<basic_function>(lua_state(), -1, handler);
  16362. #endif // Safety
  16363. }
  16364. basic_function(lua_State* L, int index = -1)
  16365. : base_t(L, index) {
  16366. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16367. constructor_handler handler{};
  16368. stack::check<basic_function>(L, index, handler);
  16369. #endif // Safety
  16370. }
  16371. basic_function(lua_State* L, ref_index index)
  16372. : base_t(L, index) {
  16373. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16374. auto pp = stack::push_pop(*this);
  16375. constructor_handler handler{};
  16376. stack::check<basic_function>(lua_state(), -1, handler);
  16377. #endif // Safety
  16378. }
  16379. template <typename Fx>
  16380. int dump(lua_Writer writer, void* userdata, bool strip, Fx&& on_error) const {
  16381. this->push();
  16382. auto ppn = stack::push_popper_n<false>(this->lua_state(), 1);
  16383. int r = lua_dump(this->lua_state(), writer, userdata, strip ? 1 : 0);
  16384. if (r != 0) {
  16385. return on_error(this->lua_state(), r, writer, userdata, strip);
  16386. }
  16387. return r;
  16388. }
  16389. int dump(lua_Writer writer, void* userdata, bool strip = false) const {
  16390. return dump(writer, userdata, strip, &dump_throw_on_error);
  16391. }
  16392. template <typename Container = bytecode>
  16393. Container dump() const {
  16394. Container bc;
  16395. (void)dump(static_cast<lua_Writer>(&basic_insert_dump_writer<Container>), static_cast<void*>(&bc), false, &dump_panic_on_error);
  16396. return bc;
  16397. }
  16398. template <typename Container = bytecode, typename Fx>
  16399. Container dump(Fx&& on_error) const {
  16400. Container bc;
  16401. (void)dump(static_cast<lua_Writer>(&basic_insert_dump_writer<Container>), static_cast<void*>(&bc), false, std::forward<Fx>(on_error));
  16402. return bc;
  16403. }
  16404. template <typename... Args>
  16405. unsafe_function_result operator()(Args&&... args) const {
  16406. return call<>(std::forward<Args>(args)...);
  16407. }
  16408. template <typename... Ret, typename... Args>
  16409. decltype(auto) operator()(types<Ret...>, Args&&... args) const {
  16410. return call<Ret...>(std::forward<Args>(args)...);
  16411. }
  16412. template <typename... Ret, typename... Args>
  16413. decltype(auto) call(Args&&... args) const {
  16414. if (!aligned) {
  16415. base_t::push();
  16416. }
  16417. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  16418. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), static_cast<std::ptrdiff_t>(pushcount));
  16419. }
  16420. };
  16421. } // namespace sol
  16422. // end of sol/unsafe_function.hpp
  16423. // beginning of sol/protected_function.hpp
  16424. // beginning of sol/protected_handler.hpp
  16425. #include <cstdint>
  16426. namespace sol {
  16427. namespace detail {
  16428. inline const char(&default_handler_name())[9]{
  16429. static const char name[9] = "sol.\xF0\x9F\x94\xA9";
  16430. return name;
  16431. }
  16432. template <bool b, typename target_t = reference>
  16433. struct protected_handler {
  16434. typedef is_stack_based<target_t> is_stack;
  16435. const target_t& target;
  16436. int stackindex;
  16437. protected_handler(std::false_type, const target_t& target)
  16438. : target(target), stackindex(0) {
  16439. if (b) {
  16440. stackindex = lua_gettop(target.lua_state()) + 1;
  16441. target.push();
  16442. }
  16443. }
  16444. protected_handler(std::true_type, const target_t& target)
  16445. : target(target), stackindex(0) {
  16446. if (b) {
  16447. stackindex = target.stack_index();
  16448. }
  16449. }
  16450. protected_handler(const target_t& target)
  16451. : protected_handler(is_stack(), target) {
  16452. }
  16453. bool valid() const noexcept {
  16454. return b;
  16455. }
  16456. ~protected_handler() {
  16457. if constexpr (!is_stack::value) {
  16458. if (stackindex != 0) {
  16459. lua_remove(target.lua_state(), stackindex);
  16460. }
  16461. }
  16462. }
  16463. };
  16464. template <typename base_t, typename T>
  16465. basic_function<base_t> force_cast(T& p) {
  16466. return p;
  16467. }
  16468. template <typename Reference, bool is_main_ref = false>
  16469. static Reference get_default_handler(lua_State* L) {
  16470. if (is_stack_based<Reference>::value || L == nullptr)
  16471. return Reference(L, lua_nil);
  16472. L = is_main_ref ? main_thread(L, L) : L;
  16473. lua_getglobal(L, default_handler_name());
  16474. auto pp = stack::pop_n(L, 1);
  16475. return Reference(L, -1);
  16476. }
  16477. template <typename T>
  16478. static void set_default_handler(lua_State* L, const T& ref) {
  16479. if (L == nullptr) {
  16480. return;
  16481. }
  16482. if (!ref.valid()) {
  16483. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK_I_)
  16484. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  16485. #endif // make sure stack doesn't overflow
  16486. lua_pushnil(L);
  16487. lua_setglobal(L, default_handler_name());
  16488. }
  16489. else {
  16490. ref.push(L);
  16491. lua_setglobal(L, default_handler_name());
  16492. }
  16493. }
  16494. } // namespace detail
  16495. } // namespace sol
  16496. // end of sol/protected_handler.hpp
  16497. #include <cstdint>
  16498. #include <algorithm>
  16499. namespace sol {
  16500. namespace detail {
  16501. template <bool b, typename handler_t>
  16502. inline void handle_protected_exception(lua_State* L, optional<const std::exception&> maybe_ex, const char* error, detail::protected_handler<b, handler_t>& h) {
  16503. h.stackindex = 0;
  16504. if (b) {
  16505. h.target.push();
  16506. detail::call_exception_handler(L, maybe_ex, error);
  16507. lua_call(L, 1, 1);
  16508. }
  16509. else {
  16510. detail::call_exception_handler(L, maybe_ex, error);
  16511. }
  16512. }
  16513. }
  16514. template <typename ref_t, bool aligned = false, typename handler_t = reference>
  16515. class basic_protected_function : public basic_object<ref_t> {
  16516. private:
  16517. using base_t = basic_object<ref_t>;
  16518. public:
  16519. using is_stack_handler = is_stack_based<handler_t>;
  16520. static handler_t get_default_handler(lua_State* L) {
  16521. return detail::get_default_handler<handler_t, is_main_threaded<base_t>::value>(L);
  16522. }
  16523. template <typename T>
  16524. static void set_default_handler(const T& ref) {
  16525. detail::set_default_handler(ref.lua_state(), ref);
  16526. }
  16527. private:
  16528. template <bool b>
  16529. call_status luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, detail::protected_handler<b, handler_t>& h) const {
  16530. return static_cast<call_status>(lua_pcall(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stackindex));
  16531. }
  16532. template <std::size_t... I, bool b, typename... Ret>
  16533. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  16534. luacall(n, sizeof...(Ret), h);
  16535. return stack::pop<std::tuple<Ret...>>(lua_state());
  16536. }
  16537. template <std::size_t I, bool b, typename Ret>
  16538. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  16539. luacall(n, 1, h);
  16540. return stack::pop<Ret>(lua_state());
  16541. }
  16542. template <std::size_t I, bool b>
  16543. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  16544. luacall(n, 0, h);
  16545. }
  16546. template <bool b>
  16547. protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n, detail::protected_handler<b, handler_t>& h) const {
  16548. int stacksize = lua_gettop(lua_state());
  16549. int poststacksize = stacksize;
  16550. int firstreturn = 1;
  16551. int returncount = 0;
  16552. call_status code = call_status::ok;
  16553. #if !defined(SOL_NO_EXCEPTIONS) || !SOL_NO_EXCEPTIONS
  16554. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) || (defined(SOL_LUAJIT) && SOL_LUAJIT)
  16555. try {
  16556. #endif // Safe Exception Propagation
  16557. #endif // No Exceptions
  16558. firstreturn = (std::max)(1, static_cast<int>(stacksize - n - static_cast<int>(h.valid() && !is_stack_handler::value)));
  16559. code = luacall(n, LUA_MULTRET, h);
  16560. poststacksize = lua_gettop(lua_state()) - static_cast<int>(h.valid() && !is_stack_handler::value);
  16561. returncount = poststacksize - (firstreturn - 1);
  16562. #ifndef SOL_NO_EXCEPTIONS
  16563. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION) || (defined(SOL_LUAJIT) && SOL_LUAJIT)
  16564. }
  16565. // Handle C++ errors thrown from C++ functions bound inside of lua
  16566. catch (const char* error) {
  16567. detail::handle_protected_exception(lua_state(), optional<const std::exception&>(nullopt), error, h);
  16568. firstreturn = lua_gettop(lua_state());
  16569. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  16570. }
  16571. catch (const std::string& error) {
  16572. detail::handle_protected_exception(lua_state(), optional<const std::exception&>(nullopt), error.c_str(), h);
  16573. firstreturn = lua_gettop(lua_state());
  16574. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  16575. }
  16576. catch (const std::exception& error) {
  16577. detail::handle_protected_exception(lua_state(), optional<const std::exception&>(error), error.what(), h);
  16578. firstreturn = lua_gettop(lua_state());
  16579. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  16580. }
  16581. #if (!defined(SOL_EXCEPTIONS_SAFE_PROPAGATION) || !SOL_NO_EXCEPTIONS_SAFE_PROPAGATION)
  16582. // LuaJIT cannot have the catchall when the safe propagation is on
  16583. // but LuaJIT will swallow all C++ errors
  16584. // if we don't at least catch std::exception ones
  16585. catch (...) {
  16586. detail::handle_protected_exception(lua_state(), optional<const std::exception&>(nullopt), detail::protected_function_error, h);
  16587. firstreturn = lua_gettop(lua_state());
  16588. return protected_function_result(lua_state(), firstreturn, 0, 1, call_status::runtime);
  16589. }
  16590. #endif // LuaJIT
  16591. #else
  16592. // do not handle exceptions: they can be propogated into C++ and keep all type information / rich information
  16593. #endif // Safe Exception Propagation
  16594. #endif // Exceptions vs. No Exceptions
  16595. return protected_function_result(lua_state(), firstreturn, returncount, returncount, code);
  16596. }
  16597. public:
  16598. using base_t::lua_state;
  16599. handler_t error_handler;
  16600. basic_protected_function() = default;
  16601. 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>
  16602. basic_protected_function(T&& r) noexcept
  16603. : base_t(std::forward<T>(r)), error_handler(get_default_handler(r.lua_state())) {
  16604. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16605. if (!is_function<meta::unqualified_t<T>>::value) {
  16606. auto pp = stack::push_pop(*this);
  16607. constructor_handler handler{};
  16608. stack::check<basic_protected_function>(lua_state(), -1, handler);
  16609. }
  16610. #endif // Safety
  16611. }
  16612. basic_protected_function(const basic_protected_function&) = default;
  16613. basic_protected_function& operator=(const basic_protected_function&) = default;
  16614. basic_protected_function(basic_protected_function&&) = default;
  16615. basic_protected_function& operator=(basic_protected_function&&) = default;
  16616. basic_protected_function(const basic_function<base_t>& b)
  16617. : basic_protected_function(b, get_default_handler(b.lua_state())) {
  16618. }
  16619. basic_protected_function(basic_function<base_t>&& b)
  16620. : basic_protected_function(std::move(b), get_default_handler(b.lua_state())) {
  16621. }
  16622. basic_protected_function(const basic_function<base_t>& b, handler_t eh)
  16623. : base_t(b), error_handler(std::move(eh)) {
  16624. }
  16625. basic_protected_function(basic_function<base_t>&& b, handler_t eh)
  16626. : base_t(std::move(b)), error_handler(std::move(eh)) {
  16627. }
  16628. basic_protected_function(const stack_reference& r)
  16629. : basic_protected_function(r.lua_state(), r.stack_index(), get_default_handler(r.lua_state())) {
  16630. }
  16631. basic_protected_function(stack_reference&& r)
  16632. : basic_protected_function(r.lua_state(), r.stack_index(), get_default_handler(r.lua_state())) {
  16633. }
  16634. basic_protected_function(const stack_reference& r, handler_t eh)
  16635. : basic_protected_function(r.lua_state(), r.stack_index(), std::move(eh)) {
  16636. }
  16637. basic_protected_function(stack_reference&& r, handler_t eh)
  16638. : basic_protected_function(r.lua_state(), r.stack_index(), std::move(eh)) {
  16639. }
  16640. template <typename Super>
  16641. basic_protected_function(const proxy_base<Super>& p)
  16642. : basic_protected_function(p, get_default_handler(p.lua_state())) {
  16643. }
  16644. template <typename Super>
  16645. basic_protected_function(proxy_base<Super>&& p)
  16646. : basic_protected_function(std::move(p), get_default_handler(p.lua_state())) {
  16647. }
  16648. 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>
  16649. basic_protected_function(Proxy&& p, Handler&& eh)
  16650. : basic_protected_function(detail::force_cast<base_t>(p), std::forward<Handler>(eh)) {
  16651. }
  16652. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  16653. basic_protected_function(lua_State* L, T&& r)
  16654. : basic_protected_function(L, std::forward<T>(r), get_default_handler(L)) {
  16655. }
  16656. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  16657. basic_protected_function(lua_State* L, T&& r, handler_t eh)
  16658. : base_t(L, std::forward<T>(r)), error_handler(std::move(eh)) {
  16659. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16660. auto pp = stack::push_pop(*this);
  16661. constructor_handler handler{};
  16662. stack::check<basic_protected_function>(lua_state(), -1, handler);
  16663. #endif // Safety
  16664. }
  16665. basic_protected_function(lua_nil_t n)
  16666. : base_t(n), error_handler(n) {
  16667. }
  16668. basic_protected_function(lua_State* L, int index = -1)
  16669. : basic_protected_function(L, index, get_default_handler(L)) {
  16670. }
  16671. basic_protected_function(lua_State* L, int index, handler_t eh)
  16672. : base_t(L, index), error_handler(std::move(eh)) {
  16673. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16674. constructor_handler handler{};
  16675. stack::check<basic_protected_function>(L, index, handler);
  16676. #endif // Safety
  16677. }
  16678. basic_protected_function(lua_State* L, absolute_index index)
  16679. : basic_protected_function(L, index, get_default_handler(L)) {
  16680. }
  16681. basic_protected_function(lua_State* L, absolute_index index, handler_t eh)
  16682. : base_t(L, index), error_handler(std::move(eh)) {
  16683. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16684. constructor_handler handler{};
  16685. stack::check<basic_protected_function>(L, index, handler);
  16686. #endif // Safety
  16687. }
  16688. basic_protected_function(lua_State* L, raw_index index)
  16689. : basic_protected_function(L, index, get_default_handler(L)) {
  16690. }
  16691. basic_protected_function(lua_State* L, raw_index index, handler_t eh)
  16692. : base_t(L, index), error_handler(std::move(eh)) {
  16693. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16694. constructor_handler handler{};
  16695. stack::check<basic_protected_function>(L, index, handler);
  16696. #endif // Safety
  16697. }
  16698. basic_protected_function(lua_State* L, ref_index index)
  16699. : basic_protected_function(L, index, get_default_handler(L)) {
  16700. }
  16701. basic_protected_function(lua_State* L, ref_index index, handler_t eh)
  16702. : base_t(L, index), error_handler(std::move(eh)) {
  16703. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  16704. auto pp = stack::push_pop(*this);
  16705. constructor_handler handler{};
  16706. stack::check<basic_protected_function>(lua_state(), -1, handler);
  16707. #endif // Safety
  16708. }
  16709. template <typename Fx>
  16710. int dump(lua_Writer writer, void* userdata, bool strip, Fx&& on_error) const {
  16711. this->push();
  16712. auto ppn = stack::push_popper_n<false>(this->lua_state(), 1);
  16713. int r = lua_dump(this->lua_state(), writer, userdata, strip ? 1 : 0);
  16714. if (r != 0) {
  16715. return on_error(this->lua_state(), r, writer, userdata, strip);
  16716. }
  16717. return r;
  16718. }
  16719. int dump(lua_Writer writer, void* userdata, bool strip = false) const {
  16720. return dump(writer, userdata, strip, &dump_pass_on_error);
  16721. }
  16722. template <typename Container = bytecode>
  16723. Container dump() const {
  16724. Container bc;
  16725. (void)dump(static_cast<lua_Writer>(&basic_insert_dump_writer<Container>), static_cast<void*>(&bc), false, &dump_throw_on_error);
  16726. return bc;
  16727. }
  16728. template <typename Container = bytecode, typename Fx>
  16729. Container dump(Fx&& on_error) const {
  16730. Container bc;
  16731. (void)dump(static_cast<lua_Writer>(&basic_insert_dump_writer<Container>), static_cast<void*>(&bc), false, std::forward<Fx>(on_error));
  16732. return bc;
  16733. }
  16734. template <typename... Args>
  16735. protected_function_result operator()(Args&&... args) const {
  16736. return call<>(std::forward<Args>(args)...);
  16737. }
  16738. template <typename... Ret, typename... Args>
  16739. decltype(auto) operator()(types<Ret...>, Args&&... args) const {
  16740. return call<Ret...>(std::forward<Args>(args)...);
  16741. }
  16742. template <typename... Ret, typename... Args>
  16743. decltype(auto) call(Args&&... args) const {
  16744. if constexpr (!aligned) {
  16745. // we do not expect the function to already be on the stack: push it
  16746. if (error_handler.valid()) {
  16747. detail::protected_handler<true, handler_t> h(error_handler);
  16748. base_t::push();
  16749. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  16750. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  16751. }
  16752. else {
  16753. detail::protected_handler<false, handler_t> h(error_handler);
  16754. base_t::push();
  16755. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  16756. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  16757. }
  16758. }
  16759. else {
  16760. // the function is already on the stack at the right location
  16761. if (error_handler.valid()) {
  16762. // the handler will be pushed onto the stack manually,
  16763. // since it's not already on the stack this means we need to push our own
  16764. // function on the stack too and swap things to be in-place
  16765. if constexpr (!is_stack_handler::value) {
  16766. // so, we need to remove the function at the top and then dump the handler out ourselves
  16767. base_t::push();
  16768. }
  16769. detail::protected_handler<true, handler_t> h(error_handler);
  16770. if constexpr (!is_stack_handler::value) {
  16771. lua_replace(lua_state(), -3);
  16772. h.stackindex = lua_absindex(lua_state(), -2);
  16773. }
  16774. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  16775. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  16776. }
  16777. else {
  16778. detail::protected_handler<false, handler_t> h(error_handler);
  16779. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  16780. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount, h);
  16781. }
  16782. }
  16783. }
  16784. };
  16785. } // namespace sol
  16786. // end of sol/protected_function.hpp
  16787. #include <functional>
  16788. namespace sol {
  16789. template <typename... Ret, typename... Args>
  16790. decltype(auto) stack_proxy::call(Args&&... args) {
  16791. stack_function sf(this->lua_state(), this->stack_index());
  16792. return sf.template call<Ret...>(std::forward<Args>(args)...);
  16793. }
  16794. inline protected_function_result::protected_function_result(unsafe_function_result&& o) noexcept
  16795. : L(o.lua_state()), index(o.stack_index()), returncount(o.return_count()), popcount(o.return_count()), err(o.status()) {
  16796. // Must be manual, otherwise destructor will screw us
  16797. // return count being 0 is enough to keep things clean
  16798. // but we will be thorough
  16799. o.abandon();
  16800. }
  16801. inline protected_function_result& protected_function_result::operator=(unsafe_function_result&& o) noexcept {
  16802. L = o.lua_state();
  16803. index = o.stack_index();
  16804. returncount = o.return_count();
  16805. popcount = o.return_count();
  16806. err = o.status();
  16807. // Must be manual, otherwise destructor will screw us
  16808. // return count being 0 is enough to keep things clean
  16809. // but we will be thorough
  16810. o.abandon();
  16811. return *this;
  16812. }
  16813. inline unsafe_function_result::unsafe_function_result(protected_function_result&& o) noexcept
  16814. : L(o.lua_state()), index(o.stack_index()), returncount(o.return_count()) {
  16815. // Must be manual, otherwise destructor will screw us
  16816. // return count being 0 is enough to keep things clean
  16817. // but we will be thorough
  16818. o.abandon();
  16819. }
  16820. inline unsafe_function_result& unsafe_function_result::operator=(protected_function_result&& o) noexcept {
  16821. L = o.lua_state();
  16822. index = o.stack_index();
  16823. returncount = o.return_count();
  16824. // Must be manual, otherwise destructor will screw us
  16825. // return count being 0 is enough to keep things clean
  16826. // but we will be thorough
  16827. o.abandon();
  16828. return *this;
  16829. }
  16830. namespace detail {
  16831. template <typename... R>
  16832. struct std_shim {
  16833. unsafe_function lua_func_;
  16834. std_shim(unsafe_function lua_func) : lua_func_(std::move(lua_func)) {
  16835. }
  16836. template <typename... Args>
  16837. meta::return_type_t<R...> operator()(Args&&... args) {
  16838. return lua_func_.call<R...>(std::forward<Args>(args)...);
  16839. }
  16840. };
  16841. template <>
  16842. struct std_shim<void> {
  16843. unsafe_function lua_func_;
  16844. std_shim(unsafe_function lua_func) : lua_func_(std::move(lua_func)) {
  16845. }
  16846. template <typename... Args>
  16847. void operator()(Args&&... args) {
  16848. lua_func_.call<void>(std::forward<Args>(args)...);
  16849. }
  16850. };
  16851. } // namespace detail
  16852. namespace stack {
  16853. template <typename Signature>
  16854. struct unqualified_getter<std::function<Signature>> {
  16855. typedef meta::bind_traits<Signature> fx_t;
  16856. typedef typename fx_t::args_list args_lists;
  16857. typedef meta::tuple_types<typename fx_t::return_type> return_types;
  16858. template <typename... R>
  16859. static std::function<Signature> get_std_func(types<R...>, lua_State* L, int index) {
  16860. detail::std_shim<R...> fx(unsafe_function(L, index));
  16861. return fx;
  16862. }
  16863. static std::function<Signature> get(lua_State* L, int index, record& tracking) {
  16864. tracking.use(1);
  16865. type t = type_of(L, index);
  16866. if (t == type::none || t == type::lua_nil) {
  16867. return nullptr;
  16868. }
  16869. return get_std_func(return_types(), L, index);
  16870. }
  16871. };
  16872. template <typename Allocator>
  16873. struct unqualified_getter<basic_bytecode<Allocator>> {
  16874. static basic_bytecode<Allocator> get(lua_State* L, int index, record& tracking) {
  16875. tracking.use(1);
  16876. stack_function sf(L, index);
  16877. return sf.dump(&dump_panic_on_error);
  16878. }
  16879. };
  16880. } // namespace stack
  16881. } // namespace sol
  16882. // end of sol/function.hpp
  16883. // beginning of sol/usertype.hpp
  16884. // beginning of sol/usertype_core.hpp
  16885. // beginning of sol/deprecate.hpp
  16886. #ifndef SOL_DEPRECATED
  16887. #ifdef _MSC_VER
  16888. #define SOL_DEPRECATED __declspec(deprecated)
  16889. #elif __GNUC__
  16890. #define SOL_DEPRECATED __attribute__((deprecated))
  16891. #else
  16892. #define SOL_DEPRECATED [[deprecated]]
  16893. #endif // compilers
  16894. #endif // SOL_DEPRECATED
  16895. namespace sol {
  16896. namespace detail {
  16897. template <typename T>
  16898. struct SOL_DEPRECATED deprecate_type {
  16899. using type = T;
  16900. };
  16901. }
  16902. } // namespace sol::detail
  16903. // end of sol/deprecate.hpp
  16904. // beginning of sol/usertype_container_launch.hpp
  16905. // beginning of sol/usertype_container.hpp
  16906. namespace sol {
  16907. template <typename T>
  16908. struct usertype_container;
  16909. namespace container_detail {
  16910. template <typename T>
  16911. struct has_clear_test {
  16912. private:
  16913. template <typename C>
  16914. static meta::sfinae_yes_t test(decltype(&C::clear));
  16915. template <typename C>
  16916. static meta::sfinae_no_t test(...);
  16917. public:
  16918. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16919. };
  16920. template <typename T>
  16921. struct has_empty_test {
  16922. private:
  16923. template <typename C>
  16924. static meta::sfinae_yes_t test(decltype(&C::empty));
  16925. template <typename C>
  16926. static meta::sfinae_no_t test(...);
  16927. public:
  16928. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16929. };
  16930. template <typename T>
  16931. struct has_erase_after_test {
  16932. private:
  16933. template <typename C>
  16934. static meta::sfinae_yes_t test(
  16935. decltype(std::declval<C>().erase_after(std::declval<std::add_rvalue_reference_t<typename C::const_iterator>>()))*);
  16936. template <typename C>
  16937. static meta::sfinae_no_t test(...);
  16938. public:
  16939. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16940. };
  16941. template <typename T, typename = void>
  16942. struct has_find_test {
  16943. private:
  16944. template <typename C>
  16945. static meta::sfinae_yes_t test(decltype(std::declval<C>().find(std::declval<std::add_rvalue_reference_t<typename C::value_type>>()))*);
  16946. template <typename C>
  16947. static meta::sfinae_no_t test(...);
  16948. public:
  16949. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16950. };
  16951. template <typename T>
  16952. struct has_find_test<T, std::enable_if_t<meta::is_lookup<T>::value>> {
  16953. private:
  16954. template <typename C>
  16955. static meta::sfinae_yes_t test(decltype(std::declval<C>().find(std::declval<std::add_rvalue_reference_t<typename C::key_type>>()))*);
  16956. template <typename C>
  16957. static meta::sfinae_no_t test(...);
  16958. public:
  16959. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16960. };
  16961. template <typename T>
  16962. struct has_erase_test {
  16963. private:
  16964. template <typename C>
  16965. static meta::sfinae_yes_t test(decltype(std::declval<C>().erase(std::declval<typename C::iterator>()))*);
  16966. template <typename C>
  16967. static meta::sfinae_no_t test(...);
  16968. public:
  16969. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16970. };
  16971. template <typename T>
  16972. struct has_erase_key_test {
  16973. private:
  16974. template <typename C>
  16975. static meta::sfinae_yes_t test(decltype(std::declval<C>().erase(std::declval<typename C::key_type>()))*);
  16976. template <typename C>
  16977. static meta::sfinae_no_t test(...);
  16978. public:
  16979. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16980. };
  16981. template <typename T>
  16982. struct has_traits_find_test {
  16983. private:
  16984. template <typename C>
  16985. static meta::sfinae_yes_t test(decltype(&C::find));
  16986. template <typename C>
  16987. static meta::sfinae_no_t test(...);
  16988. public:
  16989. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  16990. };
  16991. template <typename T>
  16992. struct has_traits_index_of_test {
  16993. private:
  16994. template <typename C>
  16995. static meta::sfinae_yes_t test(decltype(&C::index_of));
  16996. template <typename C>
  16997. static meta::sfinae_no_t test(...);
  16998. public:
  16999. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17000. };
  17001. template <typename T>
  17002. struct has_traits_insert_test {
  17003. private:
  17004. template <typename C>
  17005. static meta::sfinae_yes_t test(decltype(&C::insert));
  17006. template <typename C>
  17007. static meta::sfinae_no_t test(...);
  17008. public:
  17009. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17010. };
  17011. template <typename T>
  17012. struct has_traits_erase_test {
  17013. private:
  17014. template <typename C>
  17015. static meta::sfinae_yes_t test(decltype(&C::erase));
  17016. template <typename C>
  17017. static meta::sfinae_no_t test(...);
  17018. public:
  17019. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17020. };
  17021. template <typename T>
  17022. struct has_traits_index_set_test {
  17023. private:
  17024. template <typename C>
  17025. static meta::sfinae_yes_t test(decltype(&C::index_set));
  17026. template <typename C>
  17027. static meta::sfinae_no_t test(...);
  17028. public:
  17029. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17030. };
  17031. template <typename T>
  17032. struct has_traits_index_get_test {
  17033. private:
  17034. template <typename C>
  17035. static meta::sfinae_yes_t test(decltype(&C::index_get));
  17036. template <typename C>
  17037. static meta::sfinae_no_t test(...);
  17038. public:
  17039. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17040. };
  17041. template <typename T>
  17042. struct has_traits_set_test {
  17043. private:
  17044. template <typename C>
  17045. static meta::sfinae_yes_t test(decltype(&C::set));
  17046. template <typename C>
  17047. static meta::sfinae_no_t test(...);
  17048. public:
  17049. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17050. };
  17051. template <typename T>
  17052. struct has_traits_get_test {
  17053. private:
  17054. template <typename C>
  17055. static meta::sfinae_yes_t test(decltype(&C::get));
  17056. template <typename C>
  17057. static meta::sfinae_no_t test(...);
  17058. public:
  17059. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17060. };
  17061. template <typename T>
  17062. struct has_traits_at_test {
  17063. private:
  17064. template <typename C>
  17065. static meta::sfinae_yes_t test(decltype(&C::at));
  17066. template <typename C>
  17067. static meta::sfinae_no_t test(...);
  17068. public:
  17069. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17070. };
  17071. template <typename T>
  17072. struct has_traits_pairs_test {
  17073. private:
  17074. template <typename C>
  17075. static meta::sfinae_yes_t test(decltype(&C::pairs));
  17076. template <typename C>
  17077. static meta::sfinae_no_t test(...);
  17078. public:
  17079. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17080. };
  17081. template <typename T>
  17082. struct has_traits_ipairs_test {
  17083. private:
  17084. template <typename C>
  17085. static meta::sfinae_yes_t test(decltype(&C::ipairs));
  17086. template <typename C>
  17087. static meta::sfinae_no_t test(...);
  17088. public:
  17089. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17090. };
  17091. template <typename T>
  17092. struct has_traits_next_test {
  17093. private:
  17094. template <typename C>
  17095. static meta::sfinae_yes_t test(decltype(&C::next));
  17096. template <typename C>
  17097. static meta::sfinae_no_t test(...);
  17098. public:
  17099. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17100. };
  17101. template <typename T>
  17102. struct has_traits_add_test {
  17103. private:
  17104. template <typename C>
  17105. static meta::sfinae_yes_t test(decltype(&C::add));
  17106. template <typename C>
  17107. static meta::sfinae_no_t test(...);
  17108. public:
  17109. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17110. };
  17111. template <typename T>
  17112. struct has_traits_size_test {
  17113. private:
  17114. template <typename C>
  17115. static meta::sfinae_yes_t test(decltype(&C::size));
  17116. template <typename C>
  17117. static meta::sfinae_no_t test(...);
  17118. public:
  17119. static constexpr bool value = std::is_same_v<decltype(test<T>(0)), meta::sfinae_yes_t>;
  17120. };
  17121. template <typename T>
  17122. using has_clear = meta::boolean<has_clear_test<T>::value>;
  17123. template <typename T>
  17124. using has_empty = meta::boolean<has_empty_test<T>::value>;
  17125. template <typename T>
  17126. using has_find = meta::boolean<has_find_test<T>::value>;
  17127. template <typename T>
  17128. using has_erase = meta::boolean<has_erase_test<T>::value>;
  17129. template <typename T>
  17130. using has_erase_key = meta::boolean<has_erase_key_test<T>::value>;
  17131. template <typename T>
  17132. using has_erase_after = meta::boolean<has_erase_after_test<T>::value>;
  17133. template <typename T>
  17134. using has_traits_get = meta::boolean<has_traits_get_test<T>::value>;
  17135. template <typename T>
  17136. using has_traits_at = meta::boolean<has_traits_at_test<T>::value>;
  17137. template <typename T>
  17138. using has_traits_set = meta::boolean<has_traits_set_test<T>::value>;
  17139. template <typename T>
  17140. using has_traits_index_get = meta::boolean<has_traits_index_get_test<T>::value>;
  17141. template <typename T>
  17142. using has_traits_index_set = meta::boolean<has_traits_index_set_test<T>::value>;
  17143. template <typename T>
  17144. using has_traits_pairs = meta::boolean<has_traits_pairs_test<T>::value>;
  17145. template <typename T>
  17146. using has_traits_ipairs = meta::boolean<has_traits_ipairs_test<T>::value>;
  17147. template <typename T>
  17148. using has_traits_next = meta::boolean<has_traits_next_test<T>::value>;
  17149. template <typename T>
  17150. using has_traits_add = meta::boolean<has_traits_add_test<T>::value>;
  17151. template <typename T>
  17152. using has_traits_size = meta::boolean<has_traits_size_test<T>::value>;
  17153. template <typename T>
  17154. using has_traits_clear = has_clear<T>;
  17155. template <typename T>
  17156. using has_traits_empty = has_empty<T>;
  17157. template <typename T>
  17158. using has_traits_find = meta::boolean<has_traits_find_test<T>::value>;
  17159. template <typename T>
  17160. using has_traits_index_of = meta::boolean<has_traits_index_of_test<T>::value>;
  17161. template <typename T>
  17162. using has_traits_insert = meta::boolean<has_traits_insert_test<T>::value>;
  17163. template <typename T>
  17164. using has_traits_erase = meta::boolean<has_traits_erase_test<T>::value>;
  17165. template <typename T>
  17166. struct is_forced_container : is_container<T> {};
  17167. template <typename T>
  17168. struct is_forced_container<as_container_t<T>> : std::true_type {};
  17169. template <typename T>
  17170. struct container_decay {
  17171. typedef T type;
  17172. };
  17173. template <typename T>
  17174. struct container_decay<as_container_t<T>> {
  17175. typedef T type;
  17176. };
  17177. template <typename T>
  17178. using container_decay_t = typename container_decay<meta::unqualified_t<T>>::type;
  17179. template <typename T>
  17180. decltype(auto) get_key(std::false_type, T&& t) {
  17181. return std::forward<T>(t);
  17182. }
  17183. template <typename T>
  17184. decltype(auto) get_key(std::true_type, T&& t) {
  17185. return t.first;
  17186. }
  17187. template <typename T>
  17188. decltype(auto) get_value(std::false_type, T&& t) {
  17189. return std::forward<T>(t);
  17190. }
  17191. template <typename T>
  17192. decltype(auto) get_value(std::true_type, T&& t) {
  17193. return t.second;
  17194. }
  17195. template <typename X, typename = void>
  17196. struct usertype_container_default {
  17197. private:
  17198. typedef std::remove_pointer_t<meta::unwrap_unqualified_t<X>> T;
  17199. public:
  17200. typedef lua_nil_t iterator;
  17201. typedef lua_nil_t value_type;
  17202. static int at(lua_State* L) {
  17203. return luaL_error(L, "sol: cannot call 'at(index)' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17204. }
  17205. static int get(lua_State* L) {
  17206. return luaL_error(L, "sol: cannot call 'get(key)' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17207. }
  17208. static int index_get(lua_State* L) {
  17209. return luaL_error(L, "sol: cannot call 'container[key]' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17210. }
  17211. static int set(lua_State* L) {
  17212. 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());
  17213. }
  17214. static int index_set(lua_State* L) {
  17215. return luaL_error(
  17216. L, "sol: cannot call 'container[key] = value' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17217. }
  17218. static int add(lua_State* L) {
  17219. return luaL_error(L, "sol: cannot call 'add' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17220. }
  17221. static int insert(lua_State* L) {
  17222. return luaL_error(L, "sol: cannot call 'insert' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17223. }
  17224. static int find(lua_State* L) {
  17225. return luaL_error(L, "sol: cannot call 'find' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17226. }
  17227. static int index_of(lua_State* L) {
  17228. return luaL_error(L, "sol: cannot call 'index_of' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17229. }
  17230. static int size(lua_State* L) {
  17231. return luaL_error(L, "sol: cannot call 'end' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17232. }
  17233. static int clear(lua_State* L) {
  17234. return luaL_error(L, "sol: cannot call 'clear' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17235. }
  17236. static int empty(lua_State* L) {
  17237. return luaL_error(L, "sol: cannot call 'empty' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17238. }
  17239. static int erase(lua_State* L) {
  17240. return luaL_error(L, "sol: cannot call 'erase' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17241. }
  17242. static int next(lua_State* L) {
  17243. return luaL_error(L, "sol: cannot call 'next' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17244. }
  17245. static int pairs(lua_State* L) {
  17246. return luaL_error(L, "sol: cannot call '__pairs/pairs' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17247. }
  17248. static int ipairs(lua_State* L) {
  17249. return luaL_error(L, "sol: cannot call '__ipairs' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17250. }
  17251. static iterator begin(lua_State* L, T&) {
  17252. luaL_error(L, "sol: cannot call 'being' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17253. return lua_nil;
  17254. }
  17255. static iterator end(lua_State* L, T&) {
  17256. luaL_error(L, "sol: cannot call 'end' on type '%s': it is not recognized as a container", detail::demangle<T>().c_str());
  17257. return lua_nil;
  17258. }
  17259. };
  17260. template <typename X>
  17261. struct usertype_container_default<X,
  17262. std::enable_if_t<meta::all<is_forced_container<meta::unqualified_t<X>>, meta::has_value_type<meta::unqualified_t<container_decay_t<X>>>,
  17263. meta::has_iterator<meta::unqualified_t<container_decay_t<X>>>>::value>> {
  17264. private:
  17265. using T = std::remove_pointer_t<meta::unwrap_unqualified_t<container_decay_t<X>>>;
  17266. private:
  17267. using deferred_uc = usertype_container<X>;
  17268. using is_associative = meta::is_associative<T>;
  17269. using is_lookup = meta::is_lookup<T>;
  17270. using is_ordered = meta::is_ordered<T>;
  17271. using is_matched_lookup = meta::is_matched_lookup<T>;
  17272. using iterator = typename T::iterator;
  17273. using value_type = typename T::value_type;
  17274. typedef meta::conditional_t<is_matched_lookup::value, std::pair<value_type, value_type>,
  17275. meta::conditional_t<is_associative::value || is_lookup::value, value_type, std::pair<std::ptrdiff_t, value_type>>>
  17276. KV;
  17277. typedef typename KV::first_type K;
  17278. typedef typename KV::second_type V;
  17279. typedef meta::conditional_t<is_matched_lookup::value, std::ptrdiff_t, K> next_K;
  17280. typedef decltype(*std::declval<iterator&>()) iterator_return;
  17281. typedef meta::conditional_t<is_associative::value || is_matched_lookup::value, std::add_lvalue_reference_t<V>,
  17282. meta::conditional_t<is_lookup::value, V, iterator_return>>
  17283. captured_type;
  17284. typedef typename meta::iterator_tag<iterator>::type iterator_category;
  17285. typedef std::is_same<iterator_category, std::input_iterator_tag> is_input_iterator;
  17286. typedef meta::conditional_t<is_input_iterator::value, V, decltype(detail::deref_move_only(std::declval<captured_type>()))> push_type;
  17287. typedef std::is_copy_assignable<V> is_copyable;
  17288. typedef meta::neg<meta::any<std::is_const<V>, std::is_const<std::remove_reference_t<iterator_return>>, meta::neg<is_copyable>>> is_writable;
  17289. typedef meta::unqualified_t<decltype(get_key(is_associative(), std::declval<std::add_lvalue_reference_t<value_type>>()))> key_type;
  17290. typedef meta::all<std::is_integral<K>, meta::neg<meta::any<is_associative, is_lookup>>> is_linear_integral;
  17291. struct iter {
  17292. T& source;
  17293. iterator it;
  17294. std::size_t i;
  17295. iter(T& source, iterator it) : source(source), it(std::move(it)), i(0) {
  17296. }
  17297. ~iter() {
  17298. }
  17299. };
  17300. static auto& get_src(lua_State* L) {
  17301. #if SOL_IS_ON(SOL_SAFE_USERTYPE_I_)
  17302. auto p = stack::unqualified_check_get<T*>(L, 1);
  17303. if (!p) {
  17304. luaL_error(L,
  17305. "sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type)",
  17306. detail::demangle<T>().c_str());
  17307. }
  17308. if (p.value() == nullptr) {
  17309. luaL_error(
  17310. L, "sol: 'self' argument is nil (pass 'self' as first argument with ':' or call on a '%s' type)", detail::demangle<T>().c_str());
  17311. }
  17312. return *p.value();
  17313. #else
  17314. return stack::unqualified_get<T>(L, 1);
  17315. #endif // Safe getting with error
  17316. }
  17317. static detail::error_result at_category(std::input_iterator_tag, lua_State* L, T& self, std::ptrdiff_t pos) {
  17318. pos += deferred_uc::index_adjustment(L, self);
  17319. if (pos < 0) {
  17320. return stack::push(L, lua_nil);
  17321. }
  17322. auto it = deferred_uc::begin(L, self);
  17323. auto e = deferred_uc::end(L, self);
  17324. if (it == e) {
  17325. return stack::push(L, lua_nil);
  17326. }
  17327. while (pos > 0) {
  17328. --pos;
  17329. ++it;
  17330. if (it == e) {
  17331. return stack::push(L, lua_nil);
  17332. }
  17333. }
  17334. return get_associative(is_associative(), L, it);
  17335. }
  17336. static detail::error_result at_category(std::random_access_iterator_tag, lua_State* L, T& self, std::ptrdiff_t pos) {
  17337. std::ptrdiff_t len = static_cast<std::ptrdiff_t>(size_start(L, self));
  17338. pos += deferred_uc::index_adjustment(L, self);
  17339. if (pos < 0 || pos >= len) {
  17340. return stack::push(L, lua_nil);
  17341. }
  17342. auto it = std::next(deferred_uc::begin(L, self), pos);
  17343. return get_associative(is_associative(), L, it);
  17344. }
  17345. static detail::error_result at_start(lua_State* L, T& self, std::ptrdiff_t pos) {
  17346. return at_category(iterator_category(), L, self, pos);
  17347. }
  17348. template <typename Iter>
  17349. static detail::error_result get_associative(std::true_type, lua_State* L, Iter& it) {
  17350. decltype(auto) v = *it;
  17351. return stack::stack_detail::push_reference<push_type>(L, detail::deref_move_only(v.second));
  17352. }
  17353. template <typename Iter>
  17354. static detail::error_result get_associative(std::false_type, lua_State* L, Iter& it) {
  17355. return stack::stack_detail::push_reference<push_type>(L, detail::deref_move_only(*it));
  17356. }
  17357. static detail::error_result get_category(std::input_iterator_tag, lua_State* L, T& self, K& key) {
  17358. key += deferred_uc::index_adjustment(L, self);
  17359. if (key < 0) {
  17360. return stack::push(L, lua_nil);
  17361. }
  17362. auto it = deferred_uc::begin(L, self);
  17363. auto e = deferred_uc::end(L, self);
  17364. if (it == e) {
  17365. return stack::push(L, lua_nil);
  17366. }
  17367. while (key > 0) {
  17368. --key;
  17369. ++it;
  17370. if (it == e) {
  17371. return stack::push(L, lua_nil);
  17372. }
  17373. }
  17374. return get_associative(is_associative(), L, it);
  17375. }
  17376. static detail::error_result get_category(std::random_access_iterator_tag, lua_State* L, T& self, K& key) {
  17377. std::ptrdiff_t len = static_cast<std::ptrdiff_t>(size_start(L, self));
  17378. key += deferred_uc::index_adjustment(L, self);
  17379. if (key < 0 || key >= len) {
  17380. return stack::push(L, lua_nil);
  17381. }
  17382. auto it = std::next(deferred_uc::begin(L, self), key);
  17383. return get_associative(is_associative(), L, it);
  17384. }
  17385. static detail::error_result get_it(std::true_type, lua_State* L, T& self, K& key) {
  17386. return get_category(iterator_category(), L, self, key);
  17387. }
  17388. static detail::error_result get_comparative(std::true_type, lua_State* L, T& self, K& key) {
  17389. auto fx = [&](const value_type& r) -> bool { return key == get_key(is_associative(), r); };
  17390. auto e = deferred_uc::end(L, self);
  17391. auto it = std::find_if(deferred_uc::begin(L, self), e, std::ref(fx));
  17392. if (it == e) {
  17393. return stack::push(L, lua_nil);
  17394. }
  17395. return get_associative(is_associative(), L, it);
  17396. }
  17397. static detail::error_result get_comparative(std::false_type, lua_State*, T&, K&) {
  17398. return detail::error_result("cannot get this key on '%s': no suitable way to increment iterator and compare to key value '%s'",
  17399. detail::demangle<T>().data(),
  17400. detail::demangle<K>().data());
  17401. }
  17402. static detail::error_result get_it(std::false_type, lua_State* L, T& self, K& key) {
  17403. return get_comparative(meta::supports_op_equal<K, key_type>(), L, self, key);
  17404. }
  17405. static detail::error_result set_associative(std::true_type, iterator& it, stack_object value) {
  17406. decltype(auto) v = *it;
  17407. v.second = value.as<V>();
  17408. return {};
  17409. }
  17410. static detail::error_result set_associative(std::false_type, iterator& it, stack_object value) {
  17411. decltype(auto) v = *it;
  17412. v = value.as<V>();
  17413. return {};
  17414. }
  17415. static detail::error_result set_writable(std::true_type, lua_State*, T&, iterator& it, stack_object value) {
  17416. return set_associative(is_associative(), it, std::move(value));
  17417. }
  17418. static detail::error_result set_writable(std::false_type, lua_State*, T&, iterator&, stack_object) {
  17419. return detail::error_result(
  17420. "cannot perform a 'set': '%s's iterator reference is not writable (non-copy-assignable or const)", detail::demangle<T>().data());
  17421. }
  17422. static detail::error_result set_category(std::input_iterator_tag, lua_State* L, T& self, stack_object okey, stack_object value) {
  17423. decltype(auto) key = okey.as<K>();
  17424. key += deferred_uc::index_adjustment(L, self);
  17425. auto e = deferred_uc::end(L, self);
  17426. auto it = deferred_uc::begin(L, self);
  17427. auto backit = it;
  17428. for (; key > 0 && it != e; --key, ++it) {
  17429. backit = it;
  17430. }
  17431. if (it == e) {
  17432. if (key == 0) {
  17433. return add_copyable(is_copyable(), L, self, std::move(value), meta::has_insert_after<T>::value ? backit : it);
  17434. }
  17435. return detail::error_result("out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  17436. }
  17437. return set_writable(is_writable(), L, self, it, std::move(value));
  17438. }
  17439. static detail::error_result set_category(std::random_access_iterator_tag, lua_State* L, T& self, stack_object okey, stack_object value) {
  17440. decltype(auto) key = okey.as<K>();
  17441. key += deferred_uc::index_adjustment(L, self);
  17442. if (key < 0) {
  17443. return detail::error_result("sol: out of bounds (too small) for set on '%s'", detail::demangle<T>().c_str());
  17444. }
  17445. std::ptrdiff_t len = static_cast<std::ptrdiff_t>(size_start(L, self));
  17446. if (key == len) {
  17447. return add_copyable(is_copyable(), L, self, std::move(value));
  17448. }
  17449. else if (key >= len) {
  17450. return detail::error_result("sol: out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  17451. }
  17452. auto it = std::next(deferred_uc::begin(L, self), key);
  17453. return set_writable(is_writable(), L, self, it, std::move(value));
  17454. }
  17455. static detail::error_result set_comparative(std::true_type, lua_State* L, T& self, stack_object okey, stack_object value) {
  17456. decltype(auto) key = okey.as<K>();
  17457. if (!is_writable::value) {
  17458. return detail::error_result(
  17459. "cannot perform a 'set': '%s's iterator reference is not writable (non-copy-assignable or const)", detail::demangle<T>().data());
  17460. }
  17461. auto fx = [&](const value_type& r) -> bool { return key == get_key(is_associative(), r); };
  17462. auto e = deferred_uc::end(L, self);
  17463. auto it = std::find_if(deferred_uc::begin(L, self), e, std::ref(fx));
  17464. if (it == e) {
  17465. return {};
  17466. }
  17467. return set_writable(is_writable(), L, self, it, std::move(value));
  17468. }
  17469. static detail::error_result set_comparative(std::false_type, lua_State*, T&, stack_object, stack_object) {
  17470. return detail::error_result("cannot set this value on '%s': no suitable way to increment iterator or compare to '%s' key",
  17471. detail::demangle<T>().data(),
  17472. detail::demangle<K>().data());
  17473. }
  17474. template <typename Iter>
  17475. static detail::error_result set_associative_insert(std::true_type, lua_State*, T& self, Iter& it, K& key, stack_object value) {
  17476. if constexpr (meta::has_insert<T>::value) {
  17477. self.insert(it, value_type(key, value.as<V>()));
  17478. return {};
  17479. }
  17480. else {
  17481. (void)self;
  17482. (void)it;
  17483. (void)key;
  17484. return detail::error_result(
  17485. "cannot call 'set' on '%s': there is no 'insert' function on this associative type", detail::demangle<T>().c_str());
  17486. }
  17487. }
  17488. template <typename Iter>
  17489. static detail::error_result set_associative_insert(std::false_type, lua_State*, T& self, Iter& it, K& key, stack_object) {
  17490. if constexpr (meta::has_insert<T>::value) {
  17491. self.insert(it, key);
  17492. return {};
  17493. }
  17494. else {
  17495. (void)self;
  17496. (void)it;
  17497. (void)key;
  17498. return detail::error_result(
  17499. "cannot call 'set' on '%s': there is no 'insert' function on this non-associative type", detail::demangle<T>().c_str());
  17500. }
  17501. }
  17502. static detail::error_result set_associative_find(std::true_type, lua_State* L, T& self, stack_object okey, stack_object value) {
  17503. decltype(auto) key = okey.as<K>();
  17504. auto it = self.find(key);
  17505. if (it == deferred_uc::end(L, self)) {
  17506. return set_associative_insert(is_associative(), L, self, it, key, std::move(value));
  17507. }
  17508. return set_writable(is_writable(), L, self, it, std::move(value));
  17509. }
  17510. static detail::error_result set_associative_find(std::false_type, lua_State* L, T& self, stack_object key, stack_object value) {
  17511. return set_comparative(meta::supports_op_equal<K, key_type>(), L, self, std::move(key), std::move(value));
  17512. }
  17513. static detail::error_result set_it(std::true_type, lua_State* L, T& self, stack_object key, stack_object value) {
  17514. return set_category(iterator_category(), L, self, std::move(key), std::move(value));
  17515. }
  17516. static detail::error_result set_it(std::false_type, lua_State* L, T& self, stack_object key, stack_object value) {
  17517. return set_associative_find(meta::all<has_find<T>, meta::any<is_associative, is_lookup>>(), L, self, std::move(key), std::move(value));
  17518. }
  17519. template <bool idx_of = false>
  17520. static detail::error_result find_has_associative_lookup(std::true_type, lua_State* L, T& self) {
  17521. if constexpr (!is_ordered::value && idx_of) {
  17522. (void)L;
  17523. (void)self;
  17524. return detail::error_result("cannot perform an 'index_of': '%s's is not an ordered container", detail::demangle<T>().data());
  17525. }
  17526. else {
  17527. decltype(auto) key = stack::unqualified_get<K>(L, 2);
  17528. auto it = self.find(key);
  17529. if (it == deferred_uc::end(L, self)) {
  17530. return stack::push(L, lua_nil);
  17531. }
  17532. if constexpr (idx_of) {
  17533. auto dist = std::distance(deferred_uc::begin(L, self), it);
  17534. dist -= deferred_uc::index_adjustment(L, self);
  17535. return stack::push(L, dist);
  17536. }
  17537. else {
  17538. return get_associative(is_associative(), L, it);
  17539. }
  17540. }
  17541. }
  17542. template <bool idx_of = false>
  17543. static detail::error_result find_has_associative_lookup(std::false_type, lua_State* L, T& self) {
  17544. if constexpr (!is_ordered::value && idx_of) {
  17545. (void)L;
  17546. (void)self;
  17547. return detail::error_result("cannot perform an 'index_of': '%s's is not an ordered container", detail::demangle<T>().data());
  17548. }
  17549. else {
  17550. decltype(auto) value = stack::unqualified_get<V>(L, 2);
  17551. auto it = self.find(value);
  17552. if (it == deferred_uc::end(L, self)) {
  17553. return stack::push(L, lua_nil);
  17554. }
  17555. if constexpr (idx_of) {
  17556. auto dist = std::distance(deferred_uc::begin(L, self), it);
  17557. dist -= deferred_uc::index_adjustment(L, self);
  17558. return stack::push(L, dist);
  17559. }
  17560. else {
  17561. return get_associative(is_associative(), L, it);
  17562. }
  17563. }
  17564. }
  17565. template <bool idx_of = false>
  17566. static detail::error_result find_has(std::true_type, lua_State* L, T& self) {
  17567. return find_has_associative_lookup<idx_of>(meta::any<is_lookup, is_associative>(), L, self);
  17568. }
  17569. template <typename Iter>
  17570. static detail::error_result find_associative_lookup(std::true_type, lua_State* L, T&, Iter& it, std::size_t) {
  17571. return get_associative(is_associative(), L, it);
  17572. }
  17573. template <typename Iter>
  17574. static detail::error_result find_associative_lookup(std::false_type, lua_State* L, T& self, Iter&, std::size_t idx) {
  17575. idx -= deferred_uc::index_adjustment(L, self);
  17576. return stack::push(L, idx);
  17577. }
  17578. template <bool = false>
  17579. static detail::error_result find_comparative(std::false_type, lua_State*, T&) {
  17580. return detail::error_result("cannot call 'find' on '%s': there is no 'find' function and the value_type is not equality comparable",
  17581. detail::demangle<T>().c_str());
  17582. }
  17583. template <bool idx_of = false>
  17584. static detail::error_result find_comparative(std::true_type, lua_State* L, T& self) {
  17585. decltype(auto) value = stack::unqualified_get<V>(L, 2);
  17586. auto it = deferred_uc::begin(L, self);
  17587. auto e = deferred_uc::end(L, self);
  17588. std::size_t idx = 0;
  17589. for (;; ++it, ++idx) {
  17590. if (it == e) {
  17591. return stack::push(L, lua_nil);
  17592. }
  17593. if (value == get_value(is_associative(), *it)) {
  17594. break;
  17595. }
  17596. }
  17597. return find_associative_lookup(meta::all<meta::boolean<!idx_of>, meta::any<is_lookup, is_associative>>(), L, self, it, idx);
  17598. }
  17599. template <bool idx_of = false>
  17600. static detail::error_result find_has(std::false_type, lua_State* L, T& self) {
  17601. return find_comparative<idx_of>(meta::supports_op_equal<V>(), L, self);
  17602. }
  17603. template <typename Iter>
  17604. static detail::error_result add_insert_after(std::false_type, lua_State* L, T& self, stack_object value, Iter&) {
  17605. return add_insert_after(std::false_type(), L, self, value);
  17606. }
  17607. static detail::error_result add_insert_after(std::false_type, lua_State*, T&, stack_object) {
  17608. return detail::error_result("cannot call 'add' on type '%s': no suitable insert/push_back C++ functions", detail::demangle<T>().data());
  17609. }
  17610. template <typename Iter>
  17611. static detail::error_result add_insert_after(std::true_type, lua_State*, T& self, stack_object value, Iter& pos) {
  17612. self.insert_after(pos, value.as<V>());
  17613. return {};
  17614. }
  17615. static detail::error_result add_insert_after(std::true_type, lua_State* L, T& self, stack_object value) {
  17616. auto backit = self.before_begin();
  17617. {
  17618. auto e = deferred_uc::end(L, self);
  17619. for (auto it = deferred_uc::begin(L, self); it != e; ++backit, ++it) {
  17620. }
  17621. }
  17622. return add_insert_after(std::true_type(), L, self, value, backit);
  17623. }
  17624. template <typename Iter>
  17625. static detail::error_result add_insert(std::true_type, lua_State*, T& self, stack_object value, Iter& pos) {
  17626. self.insert(pos, value.as<V>());
  17627. return {};
  17628. }
  17629. static detail::error_result add_insert(std::true_type, lua_State* L, T& self, stack_object value) {
  17630. auto pos = deferred_uc::end(L, self);
  17631. return add_insert(std::true_type(), L, self, value, pos);
  17632. }
  17633. template <typename Iter>
  17634. static detail::error_result add_insert(std::false_type, lua_State* L, T& self, stack_object value, Iter& pos) {
  17635. return add_insert_after(meta::has_insert_after<T>(), L, self, std::move(value), pos);
  17636. }
  17637. static detail::error_result add_insert(std::false_type, lua_State* L, T& self, stack_object value) {
  17638. return add_insert_after(meta::has_insert_after<T>(), L, self, std::move(value));
  17639. }
  17640. template <typename Iter>
  17641. static detail::error_result add_push_back(std::true_type, lua_State*, T& self, stack_object value, Iter&) {
  17642. self.push_back(value.as<V>());
  17643. return {};
  17644. }
  17645. static detail::error_result add_push_back(std::true_type, lua_State*, T& self, stack_object value) {
  17646. self.push_back(value.as<V>());
  17647. return {};
  17648. }
  17649. template <typename Iter>
  17650. static detail::error_result add_push_back(std::false_type, lua_State* L, T& self, stack_object value, Iter& pos) {
  17651. return add_insert(meta::has_insert<T>(), L, self, value, pos);
  17652. }
  17653. static detail::error_result add_push_back(std::false_type, lua_State* L, T& self, stack_object value) {
  17654. return add_insert(meta::has_insert<T>(), L, self, value);
  17655. }
  17656. template <typename Iter>
  17657. static detail::error_result add_associative(std::true_type, lua_State* L, T& self, stack_object key, Iter& pos) {
  17658. if constexpr (meta::has_insert<T>::value) {
  17659. self.insert(pos, value_type(key.as<K>(), stack::unqualified_get<V>(L, 3)));
  17660. return {};
  17661. }
  17662. else {
  17663. (void)L;
  17664. (void)self;
  17665. (void)key;
  17666. (void)pos;
  17667. return detail::error_result(
  17668. "cannot call 'insert' on '%s': there is no 'insert' function on this associative type", detail::demangle<T>().c_str());
  17669. }
  17670. }
  17671. static detail::error_result add_associative(std::true_type, lua_State* L, T& self, stack_object key) {
  17672. auto pos = deferred_uc::end(L, self);
  17673. return add_associative(std::true_type(), L, self, std::move(key), pos);
  17674. }
  17675. template <typename Iter>
  17676. static detail::error_result add_associative(std::false_type, lua_State* L, T& self, stack_object value, Iter& pos) {
  17677. return add_push_back(meta::has_push_back<T>(), L, self, value, pos);
  17678. }
  17679. static detail::error_result add_associative(std::false_type, lua_State* L, T& self, stack_object value) {
  17680. return add_push_back(meta::has_push_back<T>(), L, self, value);
  17681. }
  17682. template <typename Iter>
  17683. static detail::error_result add_copyable(std::true_type, lua_State* L, T& self, stack_object value, Iter& pos) {
  17684. return add_associative(is_associative(), L, self, std::move(value), pos);
  17685. }
  17686. static detail::error_result add_copyable(std::true_type, lua_State* L, T& self, stack_object value) {
  17687. return add_associative(is_associative(), L, self, value);
  17688. }
  17689. template <typename Iter>
  17690. static detail::error_result add_copyable(std::false_type, lua_State* L, T& self, stack_object value, Iter&) {
  17691. return add_copyable(std::false_type(), L, self, std::move(value));
  17692. }
  17693. static detail::error_result add_copyable(std::false_type, lua_State*, T&, stack_object) {
  17694. return detail::error_result("cannot call 'add' on '%s': value_type is non-copyable", detail::demangle<T>().data());
  17695. }
  17696. static detail::error_result insert_lookup(std::true_type, lua_State* L, T& self, stack_object, stack_object value) {
  17697. // TODO: should we warn or error about someone calling insert on an ordered / lookup container with no associativity?
  17698. return add_copyable(std::true_type(), L, self, std::move(value));
  17699. }
  17700. static detail::error_result insert_lookup(std::false_type, lua_State* L, T& self, stack_object where, stack_object value) {
  17701. auto it = deferred_uc::begin(L, self);
  17702. auto key = where.as<K>();
  17703. key += deferred_uc::index_adjustment(L, self);
  17704. std::advance(it, key);
  17705. self.insert(it, value.as<V>());
  17706. return {};
  17707. }
  17708. static detail::error_result insert_after_has(std::true_type, lua_State* L, T& self, stack_object where, stack_object value) {
  17709. auto key = where.as<K>();
  17710. auto backit = self.before_begin();
  17711. {
  17712. key += deferred_uc::index_adjustment(L, self);
  17713. auto e = deferred_uc::end(L, self);
  17714. for (auto it = deferred_uc::begin(L, self); key > 0; ++backit, ++it, --key) {
  17715. if (backit == e) {
  17716. return detail::error_result("sol: out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  17717. }
  17718. }
  17719. }
  17720. self.insert_after(backit, value.as<V>());
  17721. return {};
  17722. }
  17723. static detail::error_result insert_after_has(std::false_type, lua_State*, T&, stack_object, stack_object) {
  17724. return detail::error_result(
  17725. "cannot call 'insert' on '%s': no suitable or similar functionality detected on this container", detail::demangle<T>().data());
  17726. }
  17727. static detail::error_result insert_has(std::true_type, lua_State* L, T& self, stack_object key, stack_object value) {
  17728. return insert_lookup(meta::any<is_associative, is_lookup>(), L, self, std::move(key), std::move(value));
  17729. }
  17730. static detail::error_result insert_has(std::false_type, lua_State* L, T& self, stack_object where, stack_object value) {
  17731. return insert_after_has(meta::has_insert_after<T>(), L, self, where, value);
  17732. }
  17733. static detail::error_result insert_copyable(std::true_type, lua_State* L, T& self, stack_object key, stack_object value) {
  17734. return insert_has(meta::has_insert<T>(), L, self, std::move(key), std::move(value));
  17735. }
  17736. static detail::error_result insert_copyable(std::false_type, lua_State*, T&, stack_object, stack_object) {
  17737. return detail::error_result("cannot call 'insert' on '%s': value_type is non-copyable", detail::demangle<T>().data());
  17738. }
  17739. static detail::error_result erase_integral(std::true_type, lua_State* L, T& self, K& key) {
  17740. auto it = deferred_uc::begin(L, self);
  17741. key += deferred_uc::index_adjustment(L, self);
  17742. std::advance(it, key);
  17743. self.erase(it);
  17744. return {};
  17745. }
  17746. static detail::error_result erase_integral(std::false_type, lua_State* L, T& self, const K& key) {
  17747. auto fx = [&](const value_type& r) -> bool { return key == r; };
  17748. auto e = deferred_uc::end(L, self);
  17749. auto it = std::find_if(deferred_uc::begin(L, self), e, std::ref(fx));
  17750. if (it == e) {
  17751. return {};
  17752. }
  17753. self.erase(it);
  17754. return {};
  17755. }
  17756. static detail::error_result erase_associative_lookup(std::true_type, lua_State*, T& self, const K& key) {
  17757. self.erase(key);
  17758. return {};
  17759. }
  17760. static detail::error_result erase_associative_lookup(std::false_type, lua_State* L, T& self, K& key) {
  17761. return erase_integral(std::is_integral<K>(), L, self, key);
  17762. }
  17763. static detail::error_result erase_after_has(std::true_type, lua_State* L, T& self, K& key) {
  17764. auto backit = self.before_begin();
  17765. {
  17766. key += deferred_uc::index_adjustment(L, self);
  17767. auto e = deferred_uc::end(L, self);
  17768. for (auto it = deferred_uc::begin(L, self); key > 0; ++backit, ++it, --key) {
  17769. if (backit == e) {
  17770. return detail::error_result("sol: out of bounds for erase on '%s'", detail::demangle<T>().c_str());
  17771. }
  17772. }
  17773. }
  17774. self.erase_after(backit);
  17775. return {};
  17776. }
  17777. static detail::error_result erase_after_has(std::false_type, lua_State*, T&, const K&) {
  17778. return detail::error_result("sol: cannot call erase on '%s'", detail::demangle<T>().c_str());
  17779. }
  17780. static detail::error_result erase_key_has(std::true_type, lua_State* L, T& self, K& key) {
  17781. return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
  17782. }
  17783. static detail::error_result erase_key_has(std::false_type, lua_State* L, T& self, K& key) {
  17784. return erase_after_has(has_erase_after<T>(), L, self, key);
  17785. }
  17786. static detail::error_result erase_has(std::true_type, lua_State* L, T& self, K& key) {
  17787. return erase_associative_lookup(meta::any<is_associative, is_lookup>(), L, self, key);
  17788. }
  17789. static detail::error_result erase_has(std::false_type, lua_State* L, T& self, K& key) {
  17790. return erase_key_has(has_erase_key<T>(), L, self, key);
  17791. }
  17792. static auto size_has(std::false_type, lua_State* L, T& self) {
  17793. return std::distance(deferred_uc::begin(L, self), deferred_uc::end(L, self));
  17794. }
  17795. static auto size_has(std::true_type, lua_State*, T& self) {
  17796. return self.size();
  17797. }
  17798. static void clear_has(std::true_type, lua_State*, T& self) {
  17799. self.clear();
  17800. }
  17801. static void clear_has(std::false_type, lua_State* L, T&) {
  17802. luaL_error(L, "sol: cannot call clear on '%s'", detail::demangle<T>().c_str());
  17803. }
  17804. static bool empty_has(std::true_type, lua_State*, T& self) {
  17805. return self.empty();
  17806. }
  17807. static bool empty_has(std::false_type, lua_State* L, T& self) {
  17808. return deferred_uc::begin(L, self) == deferred_uc::end(L, self);
  17809. }
  17810. static detail::error_result get_associative_find(std::true_type, lua_State* L, T& self, K& key) {
  17811. auto it = self.find(key);
  17812. if (it == deferred_uc::end(L, self)) {
  17813. stack::push(L, lua_nil);
  17814. return {};
  17815. }
  17816. return get_associative(std::true_type(), L, it);
  17817. }
  17818. static detail::error_result get_associative_find(std::false_type, lua_State* L, T& self, K& key) {
  17819. return get_it(is_linear_integral(), L, self, key);
  17820. }
  17821. static detail::error_result get_start(lua_State* L, T& self, K& key) {
  17822. return get_associative_find(std::integral_constant < bool, is_associative::value&& has_find<T>::value > (), L, self, key);
  17823. }
  17824. static detail::error_result set_start(lua_State* L, T& self, stack_object key, stack_object value) {
  17825. return set_it(is_linear_integral(), L, self, std::move(key), std::move(value));
  17826. }
  17827. static std::size_t size_start(lua_State* L, T& self) {
  17828. return size_has(meta::has_size<T>(), L, self);
  17829. }
  17830. static void clear_start(lua_State* L, T& self) {
  17831. clear_has(has_clear<T>(), L, self);
  17832. }
  17833. static bool empty_start(lua_State* L, T& self) {
  17834. return empty_has(has_empty<T>(), L, self);
  17835. }
  17836. static detail::error_result erase_start(lua_State* L, T& self, K& key) {
  17837. return erase_has(has_erase<T>(), L, self, key);
  17838. }
  17839. template <bool ip>
  17840. static int next_associative(std::true_type, lua_State* L) {
  17841. iter& i = stack::unqualified_get<user<iter>>(L, 1);
  17842. auto& source = i.source;
  17843. auto& it = i.it;
  17844. if (it == deferred_uc::end(L, source)) {
  17845. return stack::push(L, lua_nil);
  17846. }
  17847. int p;
  17848. if constexpr (ip) {
  17849. ++i.i;
  17850. p = stack::push_reference(L, i.i);
  17851. }
  17852. else {
  17853. p = stack::push_reference(L, it->first);
  17854. }
  17855. p += stack::stack_detail::push_reference<push_type>(L, detail::deref_move_only(it->second));
  17856. std::advance(it, 1);
  17857. return p;
  17858. }
  17859. template <bool>
  17860. static int next_associative(std::false_type, lua_State* L) {
  17861. iter& i = stack::unqualified_get<user<iter>>(L, 1);
  17862. auto& source = i.source;
  17863. auto& it = i.it;
  17864. next_K k = stack::unqualified_get<next_K>(L, 2);
  17865. if (it == deferred_uc::end(L, source)) {
  17866. return stack::push(L, lua_nil);
  17867. }
  17868. int p;
  17869. if constexpr (std::is_integral_v<next_K>) {
  17870. p = stack::push_reference(L, k + 1);
  17871. }
  17872. else {
  17873. p = stack::stack_detail::push_reference(L, k + 1);
  17874. }
  17875. p += stack::stack_detail::push_reference<push_type>(L, detail::deref_move_only(*it));
  17876. std::advance(it, 1);
  17877. return p;
  17878. }
  17879. template <bool ip>
  17880. static int next_iter(lua_State* L) {
  17881. typedef meta::any<is_associative, meta::all<is_lookup, meta::neg<is_matched_lookup>>> is_assoc;
  17882. return next_associative<ip>(is_assoc(), L);
  17883. }
  17884. template <bool ip>
  17885. static int pairs_associative(std::true_type, lua_State* L) {
  17886. auto& src = get_src(L);
  17887. stack::push(L, next_iter<ip>);
  17888. stack::push<user<iter>>(L, src, deferred_uc::begin(L, src));
  17889. stack::push(L, lua_nil);
  17890. return 3;
  17891. }
  17892. template <bool ip>
  17893. static int pairs_associative(std::false_type, lua_State* L) {
  17894. auto& src = get_src(L);
  17895. stack::push(L, next_iter<ip>);
  17896. stack::push<user<iter>>(L, src, deferred_uc::begin(L, src));
  17897. stack::push(L, 0);
  17898. return 3;
  17899. }
  17900. public:
  17901. static int at(lua_State* L) {
  17902. auto& self = get_src(L);
  17903. detail::error_result er;
  17904. {
  17905. std::ptrdiff_t pos = stack::unqualified_get<std::ptrdiff_t>(L, 2);
  17906. er = at_start(L, self, pos);
  17907. }
  17908. return handle_errors(L, er);
  17909. }
  17910. static int get(lua_State* L) {
  17911. auto& self = get_src(L);
  17912. detail::error_result er;
  17913. {
  17914. decltype(auto) key = stack::unqualified_get<K>(L);
  17915. er = get_start(L, self, key);
  17916. }
  17917. return handle_errors(L, er);
  17918. }
  17919. static int index_get(lua_State* L) {
  17920. return get(L);
  17921. }
  17922. static int set(lua_State* L) {
  17923. stack_object value = stack_object(L, raw_index(3));
  17924. if constexpr (is_linear_integral::value) {
  17925. // for non-associative containers,
  17926. // erasure only happens if it is the
  17927. // last index in the container
  17928. auto key = stack::get<K>(L, 2);
  17929. auto self_size = deferred_uc::size(L);
  17930. if (key == static_cast<K>(self_size)) {
  17931. if (type_of(L, 3) == type::lua_nil) {
  17932. return erase(L);
  17933. }
  17934. }
  17935. }
  17936. else {
  17937. if (type_of(L, 3) == type::lua_nil) {
  17938. return erase(L);
  17939. }
  17940. }
  17941. auto& self = get_src(L);
  17942. detail::error_result er = set_start(L, self, stack_object(L, raw_index(2)), std::move(value));
  17943. return handle_errors(L, er);
  17944. }
  17945. static int index_set(lua_State* L) {
  17946. return set(L);
  17947. }
  17948. static int add(lua_State* L) {
  17949. auto& self = get_src(L);
  17950. detail::error_result er = add_copyable(is_copyable(), L, self, stack_object(L, raw_index(2)));
  17951. return handle_errors(L, er);
  17952. }
  17953. static int insert(lua_State* L) {
  17954. auto& self = get_src(L);
  17955. detail::error_result er = insert_copyable(is_copyable(), L, self, stack_object(L, raw_index(2)), stack_object(L, raw_index(3)));
  17956. return handle_errors(L, er);
  17957. }
  17958. static int find(lua_State* L) {
  17959. auto& self = get_src(L);
  17960. detail::error_result er = find_has(has_find<T>(), L, self);
  17961. return handle_errors(L, er);
  17962. }
  17963. static int index_of(lua_State* L) {
  17964. auto& self = get_src(L);
  17965. detail::error_result er = find_has<true>(has_find<T>(), L, self);
  17966. return handle_errors(L, er);
  17967. }
  17968. static iterator begin(lua_State*, T& self) {
  17969. if constexpr (meta::has_begin_end_v<T>) {
  17970. return self.begin();
  17971. }
  17972. else {
  17973. using std::begin;
  17974. return begin(self);
  17975. }
  17976. }
  17977. static iterator end(lua_State*, T& self) {
  17978. if constexpr (meta::has_begin_end_v<T>) {
  17979. return self.end();
  17980. }
  17981. else {
  17982. using std::end;
  17983. return end(self);
  17984. }
  17985. }
  17986. static int size(lua_State* L) {
  17987. auto& self = get_src(L);
  17988. std::size_t r = size_start(L, self);
  17989. return stack::push(L, r);
  17990. }
  17991. static int clear(lua_State* L) {
  17992. auto& self = get_src(L);
  17993. clear_start(L, self);
  17994. return 0;
  17995. }
  17996. static int erase(lua_State* L) {
  17997. auto& self = get_src(L);
  17998. detail::error_result er;
  17999. {
  18000. decltype(auto) key = stack::unqualified_get<K>(L, 2);
  18001. er = erase_start(L, self, key);
  18002. }
  18003. return handle_errors(L, er);
  18004. }
  18005. static int empty(lua_State* L) {
  18006. auto& self = get_src(L);
  18007. return stack::push(L, empty_start(L, self));
  18008. }
  18009. static std::ptrdiff_t index_adjustment(lua_State*, T&) {
  18010. return static_cast<std::ptrdiff_t>((SOL_CONTAINER_START_INDEX_I_) == 0 ? 0 : -(SOL_CONTAINER_START_INDEX_I_));
  18011. }
  18012. static int pairs(lua_State* L) {
  18013. typedef meta::any<is_associative, meta::all<is_lookup, meta::neg<is_matched_lookup>>> is_assoc;
  18014. return pairs_associative<false>(is_assoc(), L);
  18015. }
  18016. static int ipairs(lua_State* L) {
  18017. typedef meta::any<is_associative, meta::all<is_lookup, meta::neg<is_matched_lookup>>> is_assoc;
  18018. return pairs_associative<true>(is_assoc(), L);
  18019. }
  18020. static int next(lua_State* L) {
  18021. return stack::push(L, next_iter<false>);
  18022. }
  18023. };
  18024. template <typename X>
  18025. struct usertype_container_default<X, std::enable_if_t<std::is_array<std::remove_pointer_t<meta::unwrap_unqualified_t<X>>>::value>> {
  18026. private:
  18027. typedef std::remove_pointer_t<meta::unwrap_unqualified_t<X>> T;
  18028. typedef usertype_container<X> deferred_uc;
  18029. public:
  18030. typedef std::remove_extent_t<T> value_type;
  18031. typedef value_type* iterator;
  18032. private:
  18033. struct iter {
  18034. T& source;
  18035. iterator it;
  18036. iter(T& source, iterator it) : source(source), it(std::move(it)) {
  18037. }
  18038. };
  18039. static auto& get_src(lua_State* L) {
  18040. auto p = stack::unqualified_check_get<T*>(L, 1);
  18041. #if SOL_IS_ON(SOL_SAFE_USERTYPE_I_)
  18042. if (!p) {
  18043. luaL_error(L,
  18044. "sol: 'self' is not of type '%s' (pass 'self' as first argument with ':' or call on proper type)",
  18045. detail::demangle<T>().c_str());
  18046. }
  18047. if (p.value() == nullptr) {
  18048. luaL_error(
  18049. L, "sol: 'self' argument is nil (pass 'self' as first argument with ':' or call on a '%s' type)", detail::demangle<T>().c_str());
  18050. }
  18051. #endif // Safe getting with error
  18052. return *p.value();
  18053. }
  18054. static int find(std::true_type, lua_State* L) {
  18055. T& self = get_src(L);
  18056. decltype(auto) value = stack::unqualified_get<value_type>(L, 2);
  18057. std::size_t N = std::extent<T>::value;
  18058. for (std::size_t idx = 0; idx < N; ++idx) {
  18059. using v_t = std::add_const_t<decltype(self[idx])>;
  18060. v_t v = self[idx];
  18061. if (v == value) {
  18062. idx -= deferred_uc::index_adjustment(L, self);
  18063. return stack::push(L, idx);
  18064. }
  18065. }
  18066. return stack::push(L, lua_nil);
  18067. }
  18068. static int find(std::false_type, lua_State* L) {
  18069. return luaL_error(L, "sol: cannot call 'find' on '%s': no supported comparison operator for the value type", detail::demangle<T>().c_str());
  18070. }
  18071. static int next_iter(lua_State* L) {
  18072. iter& i = stack::unqualified_get<user<iter>>(L, 1);
  18073. auto& source = i.source;
  18074. auto& it = i.it;
  18075. std::size_t k = stack::unqualified_get<std::size_t>(L, 2);
  18076. if (it == deferred_uc::end(L, source)) {
  18077. return 0;
  18078. }
  18079. int p;
  18080. p = stack::push(L, k + 1);
  18081. p += stack::push_reference(L, detail::deref_move_only(*it));
  18082. std::advance(it, 1);
  18083. return p;
  18084. }
  18085. public:
  18086. static int clear(lua_State* L) {
  18087. return luaL_error(L, "sol: cannot call 'clear' on type '%s': cannot remove all items from a fixed array", detail::demangle<T>().c_str());
  18088. }
  18089. static int erase(lua_State* L) {
  18090. return luaL_error(L, "sol: cannot call 'erase' on type '%s': cannot remove an item from fixed arrays", detail::demangle<T>().c_str());
  18091. }
  18092. static int add(lua_State* L) {
  18093. return luaL_error(L, "sol: cannot call 'add' on type '%s': cannot add to fixed arrays", detail::demangle<T>().c_str());
  18094. }
  18095. static int insert(lua_State* L) {
  18096. return luaL_error(L, "sol: cannot call 'insert' on type '%s': cannot insert new entries into fixed arrays", detail::demangle<T>().c_str());
  18097. }
  18098. static int at(lua_State* L) {
  18099. return get(L);
  18100. }
  18101. static int get(lua_State* L) {
  18102. T& self = get_src(L);
  18103. std::ptrdiff_t idx = stack::unqualified_get<std::ptrdiff_t>(L, 2);
  18104. idx += deferred_uc::index_adjustment(L, self);
  18105. if (idx >= static_cast<std::ptrdiff_t>(std::extent<T>::value) || idx < 0) {
  18106. return stack::push(L, lua_nil);
  18107. }
  18108. return stack::push_reference(L, detail::deref_move_only(self[idx]));
  18109. }
  18110. static int index_get(lua_State* L) {
  18111. return get(L);
  18112. }
  18113. static int set(lua_State* L) {
  18114. T& self = get_src(L);
  18115. std::ptrdiff_t idx = stack::unqualified_get<std::ptrdiff_t>(L, 2);
  18116. idx += deferred_uc::index_adjustment(L, self);
  18117. if (idx >= static_cast<std::ptrdiff_t>(std::extent<T>::value)) {
  18118. return luaL_error(L, "sol: index out of bounds (too big) for set on '%s'", detail::demangle<T>().c_str());
  18119. }
  18120. if (idx < 0) {
  18121. return luaL_error(L, "sol: index out of bounds (too small) for set on '%s'", detail::demangle<T>().c_str());
  18122. }
  18123. self[idx] = stack::unqualified_get<value_type>(L, 3);
  18124. return 0;
  18125. }
  18126. static int index_set(lua_State* L) {
  18127. return set(L);
  18128. }
  18129. static int index_of(lua_State* L) {
  18130. return find(L);
  18131. }
  18132. static int find(lua_State* L) {
  18133. return find(meta::supports_op_equal<value_type, value_type>(), L);
  18134. }
  18135. static int size(lua_State* L) {
  18136. return stack::push(L, std::extent<T>::value);
  18137. }
  18138. static int empty(lua_State* L) {
  18139. return stack::push(L, std::extent<T>::value > 0);
  18140. }
  18141. static int pairs(lua_State* L) {
  18142. auto& src = get_src(L);
  18143. stack::push(L, next_iter);
  18144. stack::push<user<iter>>(L, src, deferred_uc::begin(L, src));
  18145. stack::push(L, 0);
  18146. return 3;
  18147. }
  18148. static int ipairs(lua_State* L) {
  18149. return pairs(L);
  18150. }
  18151. static int next(lua_State* L) {
  18152. return stack::push(L, next_iter);
  18153. }
  18154. static std::ptrdiff_t index_adjustment(lua_State*, T&) {
  18155. return (SOL_CONTAINER_START_INDEX_I_) == 0 ? 0 : -(SOL_CONTAINER_START_INDEX_I_);
  18156. }
  18157. static iterator begin(lua_State*, T& self) {
  18158. return std::addressof(self[0]);
  18159. }
  18160. static iterator end(lua_State*, T& self) {
  18161. return std::addressof(self[0]) + std::extent<T>::value;
  18162. }
  18163. };
  18164. template <typename X>
  18165. struct usertype_container_default<usertype_container<X>> : usertype_container_default<X> {};
  18166. } // namespace container_detail
  18167. template <typename T>
  18168. struct usertype_container : container_detail::usertype_container_default<T> {};
  18169. } // namespace sol
  18170. // end of sol/usertype_container.hpp
  18171. #include <unordered_map>
  18172. namespace sol {
  18173. namespace container_detail {
  18174. template <typename X>
  18175. struct u_c_launch {
  18176. using T = std::remove_pointer_t<meta::unqualified_t<X>>;
  18177. using uc = usertype_container<T>;
  18178. using default_uc = usertype_container_default<T>;
  18179. static inline int real_index_get_traits(std::true_type, lua_State* L) {
  18180. return uc::index_get(L);
  18181. }
  18182. static inline int real_index_get_traits(std::false_type, lua_State* L) {
  18183. return default_uc::index_get(L);
  18184. }
  18185. static inline int real_index_call(lua_State* L) {
  18186. static const std::unordered_map<string_view, lua_CFunction> calls {
  18187. { "at", &real_at_call },
  18188. { "get", &real_get_call },
  18189. { "set", &real_set_call },
  18190. { "size", &real_length_call },
  18191. { "add", &real_add_call },
  18192. { "empty", &real_empty_call },
  18193. { "insert", &real_insert_call },
  18194. { "clear", &real_clear_call },
  18195. { "find", &real_find_call },
  18196. { "index_of", &real_index_of_call },
  18197. { "erase", &real_erase_call },
  18198. { "pairs", &pairs_call },
  18199. { "next", &next_call },
  18200. };
  18201. auto maybenameview = stack::unqualified_check_get<string_view>(L, 2);
  18202. if (maybenameview) {
  18203. const string_view& name = *maybenameview;
  18204. auto it = calls.find(name);
  18205. if (it != calls.cend()) {
  18206. return stack::push(L, it->second);
  18207. }
  18208. }
  18209. return real_index_get_traits(container_detail::has_traits_index_get<uc>(), L);
  18210. }
  18211. static inline int real_at_traits(std::true_type, lua_State* L) {
  18212. return uc::at(L);
  18213. }
  18214. static inline int real_at_traits(std::false_type, lua_State* L) {
  18215. return default_uc::at(L);
  18216. }
  18217. static inline int real_at_call(lua_State* L) {
  18218. return real_at_traits(container_detail::has_traits_at<uc>(), L);
  18219. }
  18220. static inline int real_get_traits(std::true_type, lua_State* L) {
  18221. return uc::get(L);
  18222. }
  18223. static inline int real_get_traits(std::false_type, lua_State* L) {
  18224. return default_uc::get(L);
  18225. }
  18226. static inline int real_get_call(lua_State* L) {
  18227. return real_get_traits(container_detail::has_traits_get<uc>(), L);
  18228. }
  18229. static inline int real_set_traits(std::true_type, lua_State* L) {
  18230. return uc::set(L);
  18231. }
  18232. static inline int real_set_traits(std::false_type, lua_State* L) {
  18233. return default_uc::set(L);
  18234. }
  18235. static inline int real_set_call(lua_State* L) {
  18236. return real_set_traits(container_detail::has_traits_set<uc>(), L);
  18237. }
  18238. static inline int real_index_set_traits(std::true_type, lua_State* L) {
  18239. return uc::index_set(L);
  18240. }
  18241. static inline int real_index_set_traits(std::false_type, lua_State* L) {
  18242. return default_uc::index_set(L);
  18243. }
  18244. static inline int real_new_index_call(lua_State* L) {
  18245. return real_index_set_traits(container_detail::has_traits_index_set<uc>(), L);
  18246. }
  18247. static inline int real_pairs_traits(std::true_type, lua_State* L) {
  18248. return uc::pairs(L);
  18249. }
  18250. static inline int real_pairs_traits(std::false_type, lua_State* L) {
  18251. return default_uc::pairs(L);
  18252. }
  18253. static inline int real_pairs_call(lua_State* L) {
  18254. return real_pairs_traits(container_detail::has_traits_pairs<uc>(), L);
  18255. }
  18256. static inline int real_ipairs_traits(std::true_type, lua_State* L) {
  18257. return uc::ipairs(L);
  18258. }
  18259. static inline int real_ipairs_traits(std::false_type, lua_State* L) {
  18260. return default_uc::ipairs(L);
  18261. }
  18262. static inline int real_ipairs_call(lua_State* L) {
  18263. return real_ipairs_traits(container_detail::has_traits_ipairs<uc>(), L);
  18264. }
  18265. static inline int real_next_traits(std::true_type, lua_State* L) {
  18266. return uc::next(L);
  18267. }
  18268. static inline int real_next_traits(std::false_type, lua_State* L) {
  18269. return default_uc::next(L);
  18270. }
  18271. static inline int real_next_call(lua_State* L) {
  18272. return real_next_traits(container_detail::has_traits_next<uc>(), L);
  18273. }
  18274. static inline int real_size_traits(std::true_type, lua_State* L) {
  18275. return uc::size(L);
  18276. }
  18277. static inline int real_size_traits(std::false_type, lua_State* L) {
  18278. return default_uc::size(L);
  18279. }
  18280. static inline int real_length_call(lua_State* L) {
  18281. return real_size_traits(container_detail::has_traits_size<uc>(), L);
  18282. }
  18283. static inline int real_add_traits(std::true_type, lua_State* L) {
  18284. return uc::add(L);
  18285. }
  18286. static inline int real_add_traits(std::false_type, lua_State* L) {
  18287. return default_uc::add(L);
  18288. }
  18289. static inline int real_add_call(lua_State* L) {
  18290. return real_add_traits(container_detail::has_traits_add<uc>(), L);
  18291. }
  18292. static inline int real_insert_traits(std::true_type, lua_State* L) {
  18293. return uc::insert(L);
  18294. }
  18295. static inline int real_insert_traits(std::false_type, lua_State* L) {
  18296. return default_uc::insert(L);
  18297. }
  18298. static inline int real_insert_call(lua_State* L) {
  18299. return real_insert_traits(container_detail::has_traits_insert<uc>(), L);
  18300. }
  18301. static inline int real_clear_traits(std::true_type, lua_State* L) {
  18302. return uc::clear(L);
  18303. }
  18304. static inline int real_clear_traits(std::false_type, lua_State* L) {
  18305. return default_uc::clear(L);
  18306. }
  18307. static inline int real_clear_call(lua_State* L) {
  18308. return real_clear_traits(container_detail::has_traits_clear<uc>(), L);
  18309. }
  18310. static inline int real_empty_traits(std::true_type, lua_State* L) {
  18311. return uc::empty(L);
  18312. }
  18313. static inline int real_empty_traits(std::false_type, lua_State* L) {
  18314. return default_uc::empty(L);
  18315. }
  18316. static inline int real_empty_call(lua_State* L) {
  18317. return real_empty_traits(container_detail::has_traits_empty<uc>(), L);
  18318. }
  18319. static inline int real_erase_traits(std::true_type, lua_State* L) {
  18320. return uc::erase(L);
  18321. }
  18322. static inline int real_erase_traits(std::false_type, lua_State* L) {
  18323. return default_uc::erase(L);
  18324. }
  18325. static inline int real_erase_call(lua_State* L) {
  18326. return real_erase_traits(container_detail::has_traits_erase<uc>(), L);
  18327. }
  18328. static inline int real_find_traits(std::true_type, lua_State* L) {
  18329. return uc::find(L);
  18330. }
  18331. static inline int real_find_traits(std::false_type, lua_State* L) {
  18332. return default_uc::find(L);
  18333. }
  18334. static inline int real_find_call(lua_State* L) {
  18335. return real_find_traits(container_detail::has_traits_find<uc>(), L);
  18336. }
  18337. static inline int real_index_of_call(lua_State* L) {
  18338. if constexpr (container_detail::has_traits_index_of<uc>()) {
  18339. return uc::index_of(L);
  18340. }
  18341. else {
  18342. return default_uc::index_of(L);
  18343. }
  18344. }
  18345. static inline int add_call(lua_State* L) {
  18346. return detail::typed_static_trampoline<decltype(&real_add_call), (&real_add_call)>(L);
  18347. }
  18348. static inline int erase_call(lua_State* L) {
  18349. return detail::typed_static_trampoline<decltype(&real_erase_call), (&real_erase_call)>(L);
  18350. }
  18351. static inline int insert_call(lua_State* L) {
  18352. return detail::typed_static_trampoline<decltype(&real_insert_call), (&real_insert_call)>(L);
  18353. }
  18354. static inline int clear_call(lua_State* L) {
  18355. return detail::typed_static_trampoline<decltype(&real_clear_call), (&real_clear_call)>(L);
  18356. }
  18357. static inline int empty_call(lua_State* L) {
  18358. return detail::typed_static_trampoline<decltype(&real_empty_call), (&real_empty_call)>(L);
  18359. }
  18360. static inline int find_call(lua_State* L) {
  18361. return detail::typed_static_trampoline<decltype(&real_find_call), (&real_find_call)>(L);
  18362. }
  18363. static inline int index_of_call(lua_State* L) {
  18364. return detail::typed_static_trampoline<decltype(&real_index_of_call), (&real_index_of_call)>(L);
  18365. }
  18366. static inline int length_call(lua_State* L) {
  18367. return detail::typed_static_trampoline<decltype(&real_length_call), (&real_length_call)>(L);
  18368. }
  18369. static inline int pairs_call(lua_State* L) {
  18370. return detail::typed_static_trampoline<decltype(&real_pairs_call), (&real_pairs_call)>(L);
  18371. }
  18372. static inline int ipairs_call(lua_State* L) {
  18373. return detail::typed_static_trampoline<decltype(&real_ipairs_call), (&real_ipairs_call)>(L);
  18374. }
  18375. static inline int next_call(lua_State* L) {
  18376. return detail::typed_static_trampoline<decltype(&real_next_call), (&real_next_call)>(L);
  18377. }
  18378. static inline int at_call(lua_State* L) {
  18379. return detail::typed_static_trampoline<decltype(&real_at_call), (&real_at_call)>(L);
  18380. }
  18381. static inline int get_call(lua_State* L) {
  18382. return detail::typed_static_trampoline<decltype(&real_get_call), (&real_get_call)>(L);
  18383. }
  18384. static inline int set_call(lua_State* L) {
  18385. return detail::typed_static_trampoline<decltype(&real_set_call), (&real_set_call)>(L);
  18386. }
  18387. static inline int index_call(lua_State* L) {
  18388. return detail::typed_static_trampoline<decltype(&real_index_call), (&real_index_call)>(L);
  18389. }
  18390. static inline int new_index_call(lua_State* L) {
  18391. return detail::typed_static_trampoline<decltype(&real_new_index_call), (&real_new_index_call)>(L);
  18392. }
  18393. };
  18394. } // namespace container_detail
  18395. namespace stack {
  18396. namespace stack_detail {
  18397. template <typename T, bool is_shim = false>
  18398. struct metatable_setup {
  18399. lua_State* L;
  18400. metatable_setup(lua_State* L) : L(L) {
  18401. }
  18402. void operator()() {
  18403. using meta_usertype_container
  18404. = container_detail::u_c_launch<meta::conditional_t<is_shim, as_container_t<std::remove_pointer_t<T>>, std::remove_pointer_t<T>>>;
  18405. static const char* metakey
  18406. = is_shim ? &usertype_traits<as_container_t<std::remove_pointer_t<T>>>::metatable()[0] : &usertype_traits<T>::metatable()[0];
  18407. static const std::array<luaL_Reg, 20> reg = { {
  18408. // clang-format off
  18409. { "__pairs", &meta_usertype_container::pairs_call },
  18410. { "__ipairs", &meta_usertype_container::ipairs_call },
  18411. { "__len", &meta_usertype_container::length_call },
  18412. { "__index", &meta_usertype_container::index_call },
  18413. { "__newindex", &meta_usertype_container::new_index_call },
  18414. { "pairs", &meta_usertype_container::pairs_call },
  18415. { "next", &meta_usertype_container::next_call },
  18416. { "at", &meta_usertype_container::at_call },
  18417. { "get", &meta_usertype_container::get_call },
  18418. { "set", &meta_usertype_container::set_call },
  18419. { "size", &meta_usertype_container::length_call },
  18420. { "empty", &meta_usertype_container::empty_call },
  18421. { "clear", &meta_usertype_container::clear_call },
  18422. { "insert", &meta_usertype_container::insert_call },
  18423. { "add", &meta_usertype_container::add_call },
  18424. { "find", &meta_usertype_container::find_call },
  18425. { "index_of", &meta_usertype_container::index_of_call },
  18426. { "erase", &meta_usertype_container::erase_call },
  18427. std::is_pointer<T>::value ? luaL_Reg{ nullptr, nullptr } : luaL_Reg{ "__gc", &detail::usertype_alloc_destruct<T> },
  18428. { nullptr, nullptr }
  18429. // clang-format on
  18430. } };
  18431. if (luaL_newmetatable(L, metakey) == 1) {
  18432. luaL_setfuncs(L, reg.data(), 0);
  18433. }
  18434. lua_setmetatable(L, -2);
  18435. }
  18436. };
  18437. } // namespace stack_detail
  18438. template <typename T>
  18439. struct unqualified_pusher<as_container_t<T>> {
  18440. using C = meta::unqualified_t<T>;
  18441. static int push_lvalue(std::true_type, lua_State* L, const C& cont) {
  18442. stack_detail::metatable_setup<C*, true> fx(L);
  18443. return stack::push<detail::as_pointer_tag<const C>>(L, detail::with_function_tag(), fx, detail::ptr(cont));
  18444. }
  18445. static int push_lvalue(std::false_type, lua_State* L, const C& cont) {
  18446. stack_detail::metatable_setup<C, true> fx(L);
  18447. return stack::push<detail::as_value_tag<C>>(L, detail::with_function_tag(), fx, cont);
  18448. }
  18449. static int push_rvalue(std::true_type, lua_State* L, C&& cont) {
  18450. stack_detail::metatable_setup<C, true> fx(L);
  18451. return stack::push<detail::as_value_tag<C>>(L, detail::with_function_tag(), fx, std::move(cont));
  18452. }
  18453. static int push_rvalue(std::false_type, lua_State* L, const C& cont) {
  18454. return push_lvalue(std::is_lvalue_reference<T>(), L, cont);
  18455. }
  18456. static int push(lua_State* L, const as_container_t<T>& as_cont) {
  18457. return push_lvalue(std::is_lvalue_reference<T>(), L, as_cont.value());
  18458. }
  18459. static int push(lua_State* L, as_container_t<T>&& as_cont) {
  18460. return push_rvalue(meta::all<std::is_rvalue_reference<T>, meta::neg<std::is_lvalue_reference<T>>>(), L, std::forward<T>(as_cont.value()));
  18461. }
  18462. };
  18463. template <typename T>
  18464. struct unqualified_pusher<as_container_t<T*>> {
  18465. using C = std::add_pointer_t<meta::unqualified_t<std::remove_pointer_t<T>>>;
  18466. static int push(lua_State* L, T* cont) {
  18467. stack_detail::metatable_setup<C> fx(L);
  18468. return stack::push<detail::as_pointer_tag<T>>(L, detail::with_function_tag(), fx, cont);
  18469. }
  18470. };
  18471. template <typename T>
  18472. struct unqualified_pusher<T, std::enable_if_t<is_container_v<T>>> {
  18473. using C = T;
  18474. template <typename... Args>
  18475. static int push(lua_State* L, Args&&... args) {
  18476. stack_detail::metatable_setup<C> fx(L);
  18477. return stack::push<detail::as_value_tag<T>>(L, detail::with_function_tag(), fx, std::forward<Args>(args)...);
  18478. }
  18479. };
  18480. template <typename T>
  18481. struct unqualified_pusher<T*, std::enable_if_t<is_container_v<T>>> {
  18482. using C = std::add_pointer_t<meta::unqualified_t<std::remove_pointer_t<T>>>;
  18483. static int push(lua_State* L, T* cont) {
  18484. stack_detail::metatable_setup<C> fx(L);
  18485. return stack::push<detail::as_pointer_tag<T>>(L, detail::with_function_tag(), fx, cont);
  18486. }
  18487. };
  18488. } // namespace stack
  18489. } // namespace sol
  18490. // end of sol/usertype_container_launch.hpp
  18491. #include <sstream>
  18492. #include <type_traits>
  18493. namespace sol {
  18494. namespace u_detail {
  18495. constexpr const lua_Integer toplevel_magic = static_cast<lua_Integer>(0xCCC2CCC1);
  18496. constexpr const int environment_index = 1;
  18497. constexpr const int usertype_storage_index = 2;
  18498. constexpr const int usertype_storage_base_index = 3;
  18499. constexpr const int exact_function_index = 4;
  18500. constexpr const int magic_index = 5;
  18501. constexpr const int simple_usertype_storage_index = 2;
  18502. constexpr const int index_function_index = 3;
  18503. constexpr const int new_index_function_index = 4;
  18504. constexpr const int base_walking_failed_index = -32467;
  18505. constexpr const int lookup_failed_index = -42469;
  18506. enum class submetatable_type {
  18507. // must be sequential
  18508. value,
  18509. reference,
  18510. unique,
  18511. const_reference,
  18512. const_value,
  18513. // must be LAST!
  18514. named
  18515. };
  18516. inline auto make_string_view(string_view s) {
  18517. return s;
  18518. }
  18519. inline auto make_string_view(call_construction) {
  18520. return string_view(to_string(meta_function::call_function));
  18521. }
  18522. inline auto make_string_view(meta_function mf) {
  18523. return string_view(to_string(mf));
  18524. }
  18525. inline auto make_string_view(base_classes_tag) {
  18526. return string_view(detail::base_class_cast_key());
  18527. }
  18528. template <typename Arg>
  18529. inline std::string make_string(Arg&& arg) {
  18530. string_view s = make_string_view(arg);
  18531. return std::string(s.data(), s.size());
  18532. }
  18533. inline int is_indexer(string_view s) {
  18534. if (s == to_string(meta_function::index)) {
  18535. return 1;
  18536. }
  18537. else if (s == to_string(meta_function::new_index)) {
  18538. return 2;
  18539. }
  18540. return 0;
  18541. }
  18542. inline int is_indexer(meta_function mf) {
  18543. if (mf == meta_function::index) {
  18544. return 1;
  18545. }
  18546. else if (mf == meta_function::new_index) {
  18547. return 2;
  18548. }
  18549. return 0;
  18550. }
  18551. inline int is_indexer(call_construction) {
  18552. return 0;
  18553. }
  18554. } // namespace u_detail
  18555. namespace detail {
  18556. template <typename T, typename IFx, typename Fx>
  18557. inline void insert_default_registrations(IFx&& ifx, Fx&& fx) {
  18558. (void)ifx;
  18559. (void)fx;
  18560. if constexpr (is_automagical<T>::value) {
  18561. if (fx(meta_function::less_than)) {
  18562. if constexpr (meta::supports_op_less<T>::value) {
  18563. lua_CFunction f = &comparsion_operator_wrap<T, std::less<>>;
  18564. ifx(meta_function::less_than, f);
  18565. }
  18566. }
  18567. if (fx(meta_function::less_than_or_equal_to)) {
  18568. if constexpr (meta::supports_op_less_equal<T>::value) {
  18569. lua_CFunction f = &comparsion_operator_wrap<T, std::less_equal<>>;
  18570. ifx(meta_function::less_than_or_equal_to, f);
  18571. }
  18572. }
  18573. if (fx(meta_function::equal_to)) {
  18574. if constexpr (meta::supports_op_equal<T>::value) {
  18575. lua_CFunction f = &comparsion_operator_wrap<T, std::equal_to<>>;
  18576. ifx(meta_function::equal_to, f);
  18577. }
  18578. else {
  18579. lua_CFunction f = &comparsion_operator_wrap<T, no_comp>;
  18580. ifx(meta_function::equal_to, f);
  18581. }
  18582. }
  18583. if (fx(meta_function::pairs)) {
  18584. ifx(meta_function::pairs, &container_detail::u_c_launch<as_container_t<T>>::pairs_call);
  18585. }
  18586. if (fx(meta_function::length)) {
  18587. if constexpr (meta::has_size<const T>::value || meta::has_size<T>::value) {
  18588. auto f = &default_size<T>;
  18589. ifx(meta_function::length, f);
  18590. }
  18591. }
  18592. if (fx(meta_function::to_string)) {
  18593. if constexpr (is_to_stringable<T>::value) {
  18594. auto f = &detail::static_trampoline<&default_to_string<T>>;
  18595. ifx(meta_function::to_string, f);
  18596. }
  18597. }
  18598. if (fx(meta_function::call_function)) {
  18599. if constexpr (meta::has_deducible_signature<T>::value) {
  18600. auto f = &c_call<decltype(&T::operator()), &T::operator()>;
  18601. ifx(meta_function::call_function, f);
  18602. }
  18603. }
  18604. }
  18605. }
  18606. } // namespace detail
  18607. namespace stack { namespace stack_detail {
  18608. template <typename X>
  18609. void set_undefined_methods_on(stack_reference t) {
  18610. using T = std::remove_pointer_t<X>;
  18611. lua_State* L = t.lua_state();
  18612. t.push();
  18613. detail::lua_reg_table l{};
  18614. int index = 0;
  18615. detail::indexed_insert insert_fx(l, index);
  18616. detail::insert_default_registrations<T>(insert_fx, detail::property_always_true);
  18617. if constexpr (!std::is_pointer_v<X>) {
  18618. l[index] = luaL_Reg{ to_string(meta_function::garbage_collect).c_str(), detail::make_destructor<T>() };
  18619. }
  18620. luaL_setfuncs(L, l, 0);
  18621. // __type table
  18622. lua_createtable(L, 0, 2);
  18623. const std::string& name = detail::demangle<T>();
  18624. lua_pushlstring(L, name.c_str(), name.size());
  18625. lua_setfield(L, -2, "name");
  18626. lua_CFunction is_func = &detail::is_check<T>;
  18627. lua_pushcclosure(L, is_func, 0);
  18628. lua_setfield(L, -2, "is");
  18629. lua_setfield(L, t.stack_index(), to_string(meta_function::type).c_str());
  18630. t.pop();
  18631. }
  18632. }} // namespace stack::stack_detail
  18633. } // namespace sol
  18634. // end of sol/usertype_core.hpp
  18635. // beginning of sol/usertype_storage.hpp
  18636. #include <bitset>
  18637. #include <unordered_map>
  18638. namespace sol { namespace u_detail {
  18639. struct usertype_storage_base;
  18640. template <typename T>
  18641. struct usertype_storage;
  18642. optional<usertype_storage_base&> maybe_get_usertype_storage_base(lua_State* L, int index);
  18643. usertype_storage_base& get_usertype_storage_base(lua_State* L, const char* gcmetakey);
  18644. template <typename T>
  18645. optional<usertype_storage<T>&> maybe_get_usertype_storage(lua_State* L);
  18646. template <typename T>
  18647. usertype_storage<T>& get_usertype_storage(lua_State* L);
  18648. using index_call_function = int(lua_State*, void*);
  18649. using change_indexing_mem_func
  18650. = void (usertype_storage_base::*)(lua_State*, submetatable_type, void*, stack_reference&, lua_CFunction, lua_CFunction, lua_CFunction, lua_CFunction);
  18651. struct index_call_storage {
  18652. index_call_function* index;
  18653. index_call_function* new_index;
  18654. void* binding_data;
  18655. };
  18656. struct new_index_call_storage : index_call_storage {
  18657. void* new_binding_data;
  18658. };
  18659. struct binding_base {
  18660. virtual void* data() = 0;
  18661. virtual ~binding_base() {
  18662. }
  18663. };
  18664. template <typename K, typename Fq, typename T = void>
  18665. struct binding : binding_base {
  18666. using uF = meta::unqualified_t<Fq>;
  18667. using F = meta::conditional_t<meta::is_c_str_of_v<uF, char>
  18668. #ifdef __cpp_char8_t
  18669. || meta::is_c_str_of_v<uF, char8_t>
  18670. #endif
  18671. || meta::is_c_str_of_v<uF, char16_t> || meta::is_c_str_of_v<uF, char32_t> || meta::is_c_str_of_v<uF, wchar_t>,
  18672. std::add_pointer_t<std::add_const_t<std::remove_all_extents_t<Fq>>>, std::decay_t<Fq>>;
  18673. F data_;
  18674. template <typename... Args>
  18675. binding(Args&&... args) : data_(std::forward<Args>(args)...) {
  18676. }
  18677. virtual void* data() override {
  18678. return static_cast<void*>(std::addressof(data_));
  18679. }
  18680. template <bool is_index = true, bool is_variable = false>
  18681. static inline int call_with_(lua_State* L, void* target) {
  18682. constexpr int boost = !detail::is_non_factory_constructor<F>::value && std::is_same<K, call_construction>::value ? 1 : 0;
  18683. auto& f = *static_cast<F*>(target);
  18684. return call_detail::call_wrapped<T, is_index, is_variable, boost>(L, f);
  18685. }
  18686. template <bool is_index = true, bool is_variable = false>
  18687. static inline int call_(lua_State* L) {
  18688. void* f = stack::get<void*>(L, upvalue_index(usertype_storage_index));
  18689. return call_with_<is_index, is_variable>(L, f);
  18690. }
  18691. template <bool is_index = true, bool is_variable = false>
  18692. static inline int call(lua_State* L) {
  18693. int r = detail::typed_static_trampoline<decltype(&call_<is_index, is_variable>), (&call_<is_index, is_variable>)>(L);
  18694. if constexpr (meta::is_specialization_of_v<uF, yielding_t>) {
  18695. return lua_yield(L, r);
  18696. }
  18697. else {
  18698. return r;
  18699. }
  18700. }
  18701. template <bool is_index = true, bool is_variable = false>
  18702. static inline int index_call_with_(lua_State* L, void* target) {
  18703. if constexpr (!is_variable) {
  18704. if constexpr (is_lua_c_function_v<std::decay_t<F>>) {
  18705. auto& f = *static_cast<std::decay_t<F>*>(target);
  18706. return stack::push(L, f);
  18707. }
  18708. else {
  18709. // set up upvalues
  18710. // for a chained call
  18711. int upvalues = 0;
  18712. upvalues += stack::push(L, nullptr);
  18713. upvalues += stack::push(L, target);
  18714. auto cfunc = &call<is_index, is_variable>;
  18715. return stack::push(L, c_closure(cfunc, upvalues));
  18716. }
  18717. }
  18718. else {
  18719. constexpr int boost = !detail::is_non_factory_constructor<F>::value && std::is_same<K, call_construction>::value ? 1 : 0;
  18720. auto& f = *static_cast<F*>(target);
  18721. return call_detail::call_wrapped<T, is_index, is_variable, boost>(L, f);
  18722. }
  18723. }
  18724. template <bool is_index = true, bool is_variable = false>
  18725. static inline int index_call_(lua_State* L) {
  18726. void* f = stack::get<void*>(L, upvalue_index(usertype_storage_index));
  18727. return index_call_with_<is_index, is_variable>(L, f);
  18728. }
  18729. template <bool is_index = true, bool is_variable = false>
  18730. static inline int index_call(lua_State* L) {
  18731. int r = detail::typed_static_trampoline<decltype(&index_call_<is_index, is_variable>), (&index_call_<is_index, is_variable>)>(L);
  18732. if constexpr (meta::is_specialization_of_v<uF, yielding_t>) {
  18733. return lua_yield(L, r);
  18734. }
  18735. else {
  18736. return r;
  18737. }
  18738. }
  18739. };
  18740. inline int index_fail(lua_State* L) {
  18741. if (lua_getmetatable(L, 1) == 1) {
  18742. int metatarget = lua_gettop(L);
  18743. stack::get_field<false, true>(L, stack_reference(L, raw_index(2)), metatarget);
  18744. return 1;
  18745. }
  18746. // With runtime extensibility, we can't
  18747. // hard-error things. They have to
  18748. // return nil, like regular table types
  18749. return stack::push(L, lua_nil);
  18750. }
  18751. inline int index_target_fail(lua_State* L, void*) {
  18752. return index_fail(L);
  18753. }
  18754. inline int new_index_fail(lua_State* L) {
  18755. return luaL_error(L, "sol: cannot set (new_index) into this object: no defined new_index operation on usertype");
  18756. }
  18757. inline int new_index_target_fail(lua_State* L, void*) {
  18758. return new_index_fail(L);
  18759. }
  18760. struct string_for_each_metatable_func {
  18761. bool is_destruction = false;
  18762. bool is_index = false;
  18763. bool is_new_index = false;
  18764. bool is_static_index = false;
  18765. bool is_static_new_index = false;
  18766. bool poison_indexing = false;
  18767. bool is_unqualified_lua_CFunction = false;
  18768. bool is_unqualified_lua_reference = false;
  18769. std::string* p_key = nullptr;
  18770. reference* p_binding_ref = nullptr;
  18771. lua_CFunction call_func = nullptr;
  18772. index_call_storage* p_ics = nullptr;
  18773. usertype_storage_base* p_usb = nullptr;
  18774. void* p_derived_usb = nullptr;
  18775. lua_CFunction idx_call = nullptr, new_idx_call = nullptr, meta_idx_call = nullptr, meta_new_idx_call = nullptr;
  18776. change_indexing_mem_func change_indexing;
  18777. void operator()(lua_State* L, submetatable_type smt, reference& fast_index_table) {
  18778. std::string& key = *p_key;
  18779. usertype_storage_base& usb = *p_usb;
  18780. index_call_storage& ics = *p_ics;
  18781. if (smt == submetatable_type::named) {
  18782. // do not override __call or
  18783. // other specific meta functions on named metatable:
  18784. // we need that for call construction
  18785. // and other amenities
  18786. return;
  18787. }
  18788. int fast_index_table_push = fast_index_table.push();
  18789. stack_reference t(L, -fast_index_table_push);
  18790. if (poison_indexing) {
  18791. (usb.*change_indexing)(L, smt, p_derived_usb, t, idx_call, new_idx_call, meta_idx_call, meta_new_idx_call);
  18792. }
  18793. if (is_destruction
  18794. && (smt == submetatable_type::reference || smt == submetatable_type::const_reference || smt == submetatable_type::named
  18795. || smt == submetatable_type::unique)) {
  18796. // gc does not apply to us here
  18797. // for reference types (raw T*, std::ref)
  18798. // for the named metatable itself,
  18799. // or for unique_usertypes, which do their own custom destruction
  18800. t.pop();
  18801. return;
  18802. }
  18803. if (is_index || is_new_index || is_static_index || is_static_new_index) {
  18804. // do not serialize the new_index and index functions here directly
  18805. // we control those...
  18806. t.pop();
  18807. return;
  18808. }
  18809. if (is_unqualified_lua_CFunction) {
  18810. stack::set_field<false, true>(L, key, call_func, t.stack_index());
  18811. }
  18812. else if (is_unqualified_lua_reference) {
  18813. reference& binding_ref = *p_binding_ref;
  18814. stack::set_field<false, true>(L, key, binding_ref, t.stack_index());
  18815. }
  18816. else {
  18817. stack::set_field<false, true>(L, key, make_closure(call_func, nullptr, ics.binding_data), t.stack_index());
  18818. }
  18819. t.pop();
  18820. }
  18821. };
  18822. struct lua_reference_func {
  18823. reference key;
  18824. reference value;
  18825. void operator()(lua_State* L, submetatable_type smt, reference& fast_index_table) {
  18826. if (smt == submetatable_type::named) {
  18827. return;
  18828. }
  18829. int fast_index_table_push = fast_index_table.push();
  18830. stack_reference t(L, -fast_index_table_push);
  18831. stack::set_field<false, true>(L, key, value, t.stack_index());
  18832. t.pop();
  18833. }
  18834. };
  18835. struct update_bases_func {
  18836. detail::inheritance_check_function base_class_check_func;
  18837. detail::inheritance_cast_function base_class_cast_func;
  18838. lua_CFunction idx_call, new_idx_call, meta_idx_call, meta_new_idx_call;
  18839. usertype_storage_base* p_usb;
  18840. void* p_derived_usb;
  18841. change_indexing_mem_func change_indexing;
  18842. void operator()(lua_State* L, submetatable_type smt, reference& fast_index_table) {
  18843. int fast_index_table_push = fast_index_table.push();
  18844. stack_reference t(L, -fast_index_table_push);
  18845. stack::set_field(L, detail::base_class_check_key(), reinterpret_cast<void*>(base_class_check_func), t.stack_index());
  18846. stack::set_field(L, detail::base_class_cast_key(), reinterpret_cast<void*>(base_class_cast_func), t.stack_index());
  18847. // change indexing, forcefully
  18848. (p_usb->*change_indexing)(L, smt, p_derived_usb, t, idx_call, new_idx_call, meta_idx_call, meta_new_idx_call);
  18849. t.pop();
  18850. }
  18851. };
  18852. struct binding_data_equals {
  18853. void* binding_data;
  18854. binding_data_equals(void* b) : binding_data(b) {
  18855. }
  18856. bool operator()(const std::unique_ptr<binding_base>& ptr) const {
  18857. return binding_data == ptr->data();
  18858. }
  18859. };
  18860. struct usertype_storage_base {
  18861. public:
  18862. std::vector<std::unique_ptr<binding_base>> storage;
  18863. std::vector<std::unique_ptr<char[]>> string_keys_storage;
  18864. std::unordered_map<string_view, index_call_storage> string_keys;
  18865. std::unordered_map<reference, reference, reference_hash, reference_equals> auxiliary_keys;
  18866. reference value_index_table;
  18867. reference reference_index_table;
  18868. reference unique_index_table;
  18869. reference const_reference_index_table;
  18870. reference const_value_index_table;
  18871. reference named_index_table;
  18872. reference type_table;
  18873. reference gc_names_table;
  18874. reference named_metatable;
  18875. new_index_call_storage base_index;
  18876. new_index_call_storage static_base_index;
  18877. bool is_using_index;
  18878. bool is_using_new_index;
  18879. std::bitset<64> properties;
  18880. usertype_storage_base(lua_State* L)
  18881. : storage()
  18882. , string_keys()
  18883. , auxiliary_keys()
  18884. , value_index_table()
  18885. , reference_index_table()
  18886. , unique_index_table()
  18887. , const_reference_index_table()
  18888. , type_table(make_reference(L, create))
  18889. , gc_names_table(make_reference(L, create))
  18890. , named_metatable(make_reference(L, create))
  18891. , base_index()
  18892. , static_base_index()
  18893. , is_using_index(false)
  18894. , is_using_new_index(false)
  18895. , properties() {
  18896. base_index.binding_data = nullptr;
  18897. base_index.index = index_target_fail;
  18898. base_index.new_index = new_index_target_fail;
  18899. base_index.new_binding_data = nullptr;
  18900. static_base_index.binding_data = nullptr;
  18901. static_base_index.index = index_target_fail;
  18902. static_base_index.new_binding_data = this;
  18903. static_base_index.new_index = new_index_target_set;
  18904. }
  18905. template <typename Fx>
  18906. void for_each_table(lua_State* L, Fx&& fx) {
  18907. for (int i = 0; i < 6; ++i) {
  18908. submetatable_type smt = static_cast<submetatable_type>(i);
  18909. reference* p_fast_index_table = nullptr;
  18910. switch (smt) {
  18911. case submetatable_type::const_value:
  18912. p_fast_index_table = &this->const_value_index_table;
  18913. break;
  18914. case submetatable_type::reference:
  18915. p_fast_index_table = &this->reference_index_table;
  18916. break;
  18917. case submetatable_type::unique:
  18918. p_fast_index_table = &this->unique_index_table;
  18919. break;
  18920. case submetatable_type::const_reference:
  18921. p_fast_index_table = &this->const_reference_index_table;
  18922. break;
  18923. case submetatable_type::named:
  18924. p_fast_index_table = &this->named_index_table;
  18925. break;
  18926. case submetatable_type::value:
  18927. default:
  18928. p_fast_index_table = &this->value_index_table;
  18929. break;
  18930. }
  18931. fx(L, smt, *p_fast_index_table);
  18932. }
  18933. }
  18934. void add_entry(string_view sv, index_call_storage ics) {
  18935. string_keys_storage.emplace_back(new char[sv.size()]);
  18936. std::unique_ptr<char[]>& sv_storage = string_keys_storage.back();
  18937. std::memcpy(sv_storage.get(), sv.data(), sv.size());
  18938. string_view stored_sv(sv_storage.get(), sv.size());
  18939. string_keys.insert_or_assign(std::move(stored_sv), std::move(ics));
  18940. }
  18941. template <typename T, typename... Bases>
  18942. void update_bases(lua_State* L, bases<Bases...>) {
  18943. static_assert(sizeof(void*) <= sizeof(detail::inheritance_check_function),
  18944. "The size of this data pointer is too small to fit the inheritance checking function: Please file "
  18945. "a bug report.");
  18946. static_assert(sizeof(void*) <= sizeof(detail::inheritance_cast_function),
  18947. "The size of this data pointer is too small to fit the inheritance checking function: Please file "
  18948. "a bug report.");
  18949. static_assert(!meta::any_same<T, Bases...>::value, "base classes cannot list the original class as part of the bases");
  18950. if constexpr (sizeof...(Bases) < 1) {
  18951. return;
  18952. }
  18953. (void)detail::swallow { 0, ((weak_derive<Bases>::value = true), 0)... };
  18954. void* derived_this = static_cast<void*>(static_cast<usertype_storage<T>*>(this));
  18955. update_bases_func for_each_fx;
  18956. for_each_fx.base_class_check_func = &detail::inheritance<T>::template type_check_with<Bases...>;
  18957. for_each_fx.base_class_cast_func = &detail::inheritance<T>::template type_cast_with<Bases...>;
  18958. for_each_fx.idx_call = &usertype_storage<T>::template index_call_with_bases<false, Bases...>;
  18959. for_each_fx.new_idx_call = &usertype_storage<T>::template index_call_with_bases<true, Bases...>;
  18960. for_each_fx.meta_idx_call = &usertype_storage<T>::template meta_index_call_with_bases<false, Bases...>;
  18961. for_each_fx.meta_new_idx_call = &usertype_storage<T>::template meta_index_call_with_bases<true, Bases...>;
  18962. for_each_fx.p_usb = this;
  18963. for_each_fx.p_derived_usb = derived_this;
  18964. for_each_fx.change_indexing = &usertype_storage_base::change_indexing;
  18965. for_each_fx.p_derived_usb = derived_this;
  18966. this->for_each_table(L, for_each_fx);
  18967. }
  18968. void clear() {
  18969. if (value_index_table.valid()) {
  18970. stack::clear(value_index_table);
  18971. }
  18972. if (reference_index_table.valid()) {
  18973. stack::clear(reference_index_table);
  18974. }
  18975. if (unique_index_table.valid()) {
  18976. stack::clear(unique_index_table);
  18977. }
  18978. if (const_reference_index_table.valid()) {
  18979. stack::clear(const_reference_index_table);
  18980. }
  18981. if (const_value_index_table.valid()) {
  18982. stack::clear(const_value_index_table);
  18983. }
  18984. if (named_index_table.valid()) {
  18985. stack::clear(named_index_table);
  18986. }
  18987. if (type_table.valid()) {
  18988. stack::clear(type_table);
  18989. }
  18990. if (gc_names_table.valid()) {
  18991. stack::clear(gc_names_table);
  18992. }
  18993. if (named_metatable.valid()) {
  18994. lua_State* L = named_metatable.lua_state();
  18995. auto pp = stack::push_pop(named_metatable);
  18996. int named_metatable_index = pp.index_of(named_metatable);
  18997. if (lua_getmetatable(L, named_metatable_index) == 1) {
  18998. stack::clear(L, absolute_index(L, -1));
  18999. }
  19000. stack::clear(named_metatable);
  19001. }
  19002. value_index_table = lua_nil;
  19003. reference_index_table = lua_nil;
  19004. unique_index_table = lua_nil;
  19005. const_reference_index_table = lua_nil;
  19006. const_value_index_table = lua_nil;
  19007. named_index_table = lua_nil;
  19008. type_table = lua_nil;
  19009. gc_names_table = lua_nil;
  19010. named_metatable = lua_nil;
  19011. storage.clear();
  19012. string_keys.clear();
  19013. auxiliary_keys.clear();
  19014. }
  19015. template <bool is_new_index, typename Base>
  19016. static void base_walk_index(lua_State* L, usertype_storage_base& self, bool& keep_going, int& base_result) {
  19017. using bases = typename base<Base>::type;
  19018. if (!keep_going) {
  19019. return;
  19020. }
  19021. (void)L;
  19022. (void)self;
  19023. #if SOL_IS_ON(SOL_USE_UNSAFE_BASE_LOOKUP_I_)
  19024. usertype_storage_base& base_storage = get_usertype_storage<Base>(L);
  19025. base_result = self_index_call<is_new_index, true>(bases(), L, base_storage);
  19026. #else
  19027. optional<usertype_storage<Base>&> maybe_base_storage = maybe_get_usertype_storage<Base>(L);
  19028. if (static_cast<bool>(maybe_base_storage)) {
  19029. base_result = self_index_call<is_new_index, true>(bases(), L, *maybe_base_storage);
  19030. keep_going = base_result == base_walking_failed_index;
  19031. }
  19032. #endif // Fast versus slow, safe base lookup
  19033. }
  19034. template <bool is_new_index = false, bool base_walking = false, bool from_named_metatable = false, typename... Bases>
  19035. static inline int self_index_call(types<Bases...>, lua_State* L, usertype_storage_base& self) {
  19036. type k_type = stack::get<type>(L, 2);
  19037. if (k_type == type::string) {
  19038. index_call_storage* target = nullptr;
  19039. {
  19040. string_view k = stack::get<string_view>(L, 2);
  19041. auto it = self.string_keys.find(k);
  19042. if (it != self.string_keys.cend()) {
  19043. target = &it->second;
  19044. }
  19045. }
  19046. if (target != nullptr) {
  19047. // let the target decide what to do
  19048. if constexpr (is_new_index) {
  19049. return (target->new_index)(L, target->binding_data);
  19050. }
  19051. else {
  19052. return (target->index)(L, target->binding_data);
  19053. }
  19054. }
  19055. }
  19056. else if (k_type != type::lua_nil && k_type != type::none) {
  19057. reference* target = nullptr;
  19058. {
  19059. stack_reference k = stack::get<stack_reference>(L, 2);
  19060. auto it = self.auxiliary_keys.find(k);
  19061. if (it != self.auxiliary_keys.cend()) {
  19062. target = &it->second;
  19063. }
  19064. }
  19065. if (target != nullptr) {
  19066. if constexpr (is_new_index) {
  19067. // set value and return
  19068. *target = reference(L, 3);
  19069. return 0;
  19070. }
  19071. else {
  19072. // push target to return
  19073. // what we found
  19074. return stack::push(L, *target);
  19075. }
  19076. }
  19077. }
  19078. // retrieve bases and walk through them.
  19079. bool keep_going = true;
  19080. int base_result;
  19081. (void)keep_going;
  19082. (void)base_result;
  19083. (void)detail::swallow { 1, (base_walk_index<is_new_index, Bases>(L, self, keep_going, base_result), 1)... };
  19084. if constexpr (sizeof...(Bases) > 0) {
  19085. if (!keep_going) {
  19086. return base_result;
  19087. }
  19088. }
  19089. if constexpr (base_walking) {
  19090. // if we're JUST base-walking then don't index-fail, just
  19091. // return the false bits
  19092. return base_walking_failed_index;
  19093. }
  19094. else if constexpr (from_named_metatable) {
  19095. if constexpr (is_new_index) {
  19096. return self.static_base_index.new_index(L, self.static_base_index.new_binding_data);
  19097. }
  19098. else {
  19099. return self.static_base_index.index(L, self.static_base_index.binding_data);
  19100. }
  19101. }
  19102. else {
  19103. if constexpr (is_new_index) {
  19104. return self.base_index.new_index(L, self.base_index.new_binding_data);
  19105. }
  19106. else {
  19107. return self.base_index.index(L, self.base_index.binding_data);
  19108. }
  19109. }
  19110. }
  19111. void change_indexing(lua_State* L, submetatable_type submetatable, void* derived_this, stack_reference& t, lua_CFunction index,
  19112. lua_CFunction new_index, lua_CFunction meta_index, lua_CFunction meta_new_index) {
  19113. usertype_storage_base& this_base = *this;
  19114. void* base_this = static_cast<void*>(&this_base);
  19115. this->is_using_index |= true;
  19116. this->is_using_new_index |= true;
  19117. if (submetatable == submetatable_type::named) {
  19118. stack::set_field(L, metatable_key, named_index_table, t.stack_index());
  19119. stack_reference stack_metametatable(L, -named_metatable.push());
  19120. stack::set_field<false, true>(L,
  19121. meta_function::index,
  19122. make_closure(meta_index, nullptr, derived_this, base_this, nullptr, toplevel_magic),
  19123. stack_metametatable.stack_index());
  19124. stack::set_field<false, true>(L,
  19125. meta_function::new_index,
  19126. make_closure(meta_new_index, nullptr, derived_this, base_this, nullptr, toplevel_magic),
  19127. stack_metametatable.stack_index());
  19128. stack_metametatable.pop();
  19129. }
  19130. else {
  19131. stack::set_field<false, true>(
  19132. L, meta_function::index, make_closure(index, nullptr, derived_this, base_this, nullptr, toplevel_magic), t.stack_index());
  19133. stack::set_field<false, true>(
  19134. L, meta_function::new_index, make_closure(new_index, nullptr, derived_this, base_this, nullptr, toplevel_magic), t.stack_index());
  19135. }
  19136. }
  19137. template <typename T = void, typename Key, typename Value>
  19138. void set(lua_State* L, Key&& key, Value&& value);
  19139. static int new_index_target_set(lua_State* L, void* target) {
  19140. usertype_storage_base& self = *static_cast<usertype_storage_base*>(target);
  19141. self.set(L, reference(L, raw_index(2)), reference(L, raw_index(3)));
  19142. return 0;
  19143. }
  19144. };
  19145. template <typename T>
  19146. struct usertype_storage : usertype_storage_base {
  19147. using usertype_storage_base::usertype_storage_base;
  19148. template <bool is_new_index, bool from_named_metatable>
  19149. static inline int index_call_(lua_State* L) {
  19150. using bases = typename base<T>::type;
  19151. usertype_storage_base& self = stack::get<light<usertype_storage_base>>(L, upvalue_index(usertype_storage_index));
  19152. return self_index_call<is_new_index, false, from_named_metatable>(bases(), L, self);
  19153. }
  19154. template <bool is_new_index, bool from_named_metatable, typename... Bases>
  19155. static inline int index_call_with_bases_(lua_State* L) {
  19156. using bases = types<Bases...>;
  19157. usertype_storage_base& self = stack::get<light<usertype_storage_base>>(L, upvalue_index(usertype_storage_index));
  19158. return self_index_call<is_new_index, false, from_named_metatable>(bases(), L, self);
  19159. }
  19160. template <bool is_new_index>
  19161. static inline int index_call(lua_State* L) {
  19162. return detail::static_trampoline<&index_call_<is_new_index, false>>(L);
  19163. }
  19164. template <bool is_new_index, typename... Bases>
  19165. static inline int index_call_with_bases(lua_State* L) {
  19166. return detail::static_trampoline<&index_call_with_bases_<is_new_index, false, Bases...>>(L);
  19167. }
  19168. template <bool is_new_index>
  19169. static inline int meta_index_call(lua_State* L) {
  19170. return detail::static_trampoline<&index_call_<is_new_index, true>>(L);
  19171. }
  19172. template <bool is_new_index, typename... Bases>
  19173. static inline int meta_index_call_with_bases(lua_State* L) {
  19174. return detail::static_trampoline<&index_call_with_bases_<is_new_index, true, Bases...>>(L);
  19175. }
  19176. template <typename Key, typename Value>
  19177. inline void set(lua_State* L, Key&& key, Value&& value);
  19178. };
  19179. template <typename T>
  19180. inline int destruct_usertype_storage(lua_State* L) {
  19181. return detail::user_alloc_destruct<usertype_storage<T>>(L);
  19182. }
  19183. template <typename T, typename Key, typename Value>
  19184. void usertype_storage_base::set(lua_State* L, Key&& key, Value&& value) {
  19185. using ValueU = meta::unwrap_unqualified_t<Value>;
  19186. using KeyU = meta::unwrap_unqualified_t<Key>;
  19187. using Binding = binding<KeyU, ValueU, T>;
  19188. using is_var_bind = is_variable_binding<ValueU>;
  19189. if constexpr (std::is_same_v<KeyU, call_construction>) {
  19190. (void)key;
  19191. std::unique_ptr<Binding> p_binding = std::make_unique<Binding>(std::forward<Value>(value));
  19192. Binding& b = *p_binding;
  19193. this->storage.push_back(std::move(p_binding));
  19194. this->named_index_table.push();
  19195. absolute_index metametatable_index(L, -1);
  19196. stack::push(L, nullptr);
  19197. stack::push(L, b.data());
  19198. lua_CFunction target_func = &b.template call<false, false>;
  19199. lua_pushcclosure(L, target_func, 2);
  19200. lua_setfield(L, metametatable_index, to_string(meta_function::call).c_str());
  19201. this->named_index_table.pop();
  19202. }
  19203. else if constexpr (std::is_same_v<KeyU, base_classes_tag>) {
  19204. (void)key;
  19205. this->update_bases<T>(L, std::forward<Value>(value));
  19206. }
  19207. else if constexpr ((meta::is_string_like_or_constructible<KeyU>::value || std::is_same_v<KeyU, meta_function>)) {
  19208. std::string s = u_detail::make_string(std::forward<Key>(key));
  19209. auto storage_it = this->storage.end();
  19210. auto string_it = this->string_keys.find(s);
  19211. if (string_it != this->string_keys.cend()) {
  19212. const auto& binding_data = string_it->second.binding_data;
  19213. storage_it = std::find_if(this->storage.begin(), this->storage.end(), binding_data_equals(binding_data));
  19214. this->string_keys.erase(string_it);
  19215. }
  19216. std::unique_ptr<Binding> p_binding = std::make_unique<Binding>(std::forward<Value>(value));
  19217. Binding& b = *p_binding;
  19218. if (storage_it != this->storage.cend()) {
  19219. *storage_it = std::move(p_binding);
  19220. }
  19221. else {
  19222. this->storage.push_back(std::move(p_binding));
  19223. }
  19224. bool is_index = (s == to_string(meta_function::index));
  19225. bool is_new_index = (s == to_string(meta_function::new_index));
  19226. bool is_static_index = (s == to_string(meta_function::static_index));
  19227. bool is_static_new_index = (s == to_string(meta_function::static_new_index));
  19228. bool is_destruction = s == to_string(meta_function::garbage_collect);
  19229. bool poison_indexing = (!is_using_index || !is_using_new_index) && (is_var_bind::value || is_index || is_new_index);
  19230. void* derived_this = static_cast<void*>(static_cast<usertype_storage<T>*>(this));
  19231. index_call_storage ics;
  19232. ics.binding_data = b.data();
  19233. ics.index = is_index || is_static_index ? &Binding::template call_with_<true, is_var_bind::value>
  19234. : &Binding::template index_call_with_<true, is_var_bind::value>;
  19235. ics.new_index = is_new_index || is_static_new_index ? &Binding::template call_with_<false, is_var_bind::value>
  19236. : &Binding::template index_call_with_<false, is_var_bind::value>;
  19237. string_for_each_metatable_func for_each_fx;
  19238. for_each_fx.is_destruction = is_destruction;
  19239. for_each_fx.is_index = is_index;
  19240. for_each_fx.is_new_index = is_new_index;
  19241. for_each_fx.is_static_index = is_static_index;
  19242. for_each_fx.is_static_new_index = is_static_new_index;
  19243. for_each_fx.poison_indexing = poison_indexing;
  19244. for_each_fx.p_key = &s;
  19245. for_each_fx.p_ics = &ics;
  19246. if constexpr (is_lua_c_function_v<ValueU>) {
  19247. for_each_fx.is_unqualified_lua_CFunction = true;
  19248. for_each_fx.call_func = *static_cast<lua_CFunction*>(ics.binding_data);
  19249. }
  19250. else if constexpr (is_lua_reference_or_proxy_v<ValueU>) {
  19251. for_each_fx.is_unqualified_lua_reference = true;
  19252. for_each_fx.p_binding_ref = static_cast<reference*>(ics.binding_data);
  19253. }
  19254. else {
  19255. for_each_fx.call_func = &b.template call<false, is_var_bind::value>;
  19256. }
  19257. for_each_fx.p_usb = this;
  19258. for_each_fx.p_derived_usb = derived_this;
  19259. for_each_fx.idx_call = &usertype_storage<T>::template index_call<false>;
  19260. for_each_fx.new_idx_call = &usertype_storage<T>::template index_call<true>;
  19261. for_each_fx.meta_idx_call = &usertype_storage<T>::template meta_index_call<false>;
  19262. for_each_fx.meta_new_idx_call = &usertype_storage<T>::template meta_index_call<true>;
  19263. for_each_fx.change_indexing = &usertype_storage_base::change_indexing;
  19264. // set base index and base new_index
  19265. // functions here
  19266. if (is_index) {
  19267. this->base_index.index = ics.index;
  19268. this->base_index.binding_data = ics.binding_data;
  19269. }
  19270. if (is_new_index) {
  19271. this->base_index.new_index = ics.new_index;
  19272. this->base_index.new_binding_data = ics.binding_data;
  19273. }
  19274. if (is_static_index) {
  19275. this->static_base_index.index = ics.index;
  19276. this->static_base_index.binding_data = ics.binding_data;
  19277. }
  19278. if (is_static_new_index) {
  19279. this->static_base_index.new_index = ics.new_index;
  19280. this->static_base_index.new_binding_data = ics.binding_data;
  19281. }
  19282. this->for_each_table(L, for_each_fx);
  19283. this->add_entry(s, std::move(ics));
  19284. }
  19285. else {
  19286. // the reference-based implementation might compare poorly and hash
  19287. // poorly in some cases...
  19288. if constexpr (is_lua_reference_v<KeyU> && is_lua_reference_v<ValueU>) {
  19289. if (key.get_type() == type::string) {
  19290. stack::push(L, key);
  19291. std::string string_key = stack::pop<std::string>(L);
  19292. this->set<T>(L, string_key, std::forward<Value>(value));
  19293. }
  19294. else {
  19295. lua_reference_func ref_additions_fx { key, value };
  19296. this->for_each_table(L, ref_additions_fx);
  19297. this->auxiliary_keys.insert_or_assign(std::forward<Key>(key), std::forward<Value>(value));
  19298. }
  19299. }
  19300. else {
  19301. reference ref_key = make_reference(L, std::forward<Key>(key));
  19302. reference ref_value = make_reference(L, std::forward<Value>(value));
  19303. lua_reference_func ref_additions_fx { key, value };
  19304. this->for_each_table(L, ref_additions_fx);
  19305. this->auxiliary_keys.insert_or_assign(std::move(ref_key), std::move(ref_value));
  19306. }
  19307. }
  19308. }
  19309. template <typename T>
  19310. template <typename Key, typename Value>
  19311. void usertype_storage<T>::set(lua_State* L, Key&& key, Value&& value) {
  19312. static_cast<usertype_storage_base&>(*this).set<T>(L, std::forward<Key>(key), std::forward<Value>(value));
  19313. }
  19314. template <typename T>
  19315. inline usertype_storage<T>& create_usertype_storage(lua_State* L) {
  19316. const char* gcmetakey = &usertype_traits<T>::gc_table()[0];
  19317. // Make sure userdata's memory is properly in lua first,
  19318. // otherwise all the light userdata we make later will become invalid
  19319. int usertype_storage_push_count = stack::push<user<usertype_storage<T>>>(L, no_metatable, L);
  19320. stack_reference usertype_storage_ref(L, -usertype_storage_push_count);
  19321. // create and push onto the stack a table to use as metatable for this GC
  19322. // we create a metatable to attach to the regular gc_table
  19323. // so that the destructor is called for the usertype storage
  19324. int usertype_storage_metatabe_count = stack::push(L, new_table(0, 1));
  19325. stack_reference usertype_storage_metatable(L, -usertype_storage_metatabe_count);
  19326. // set the destruction routine on the metatable
  19327. stack::set_field(L, meta_function::garbage_collect, &destruct_usertype_storage<T>, usertype_storage_metatable.stack_index());
  19328. // set the metatable on the usertype storage userdata
  19329. stack::set_field(L, metatable_key, usertype_storage_metatable, usertype_storage_ref.stack_index());
  19330. usertype_storage_metatable.pop();
  19331. // set the usertype storage and its metatable
  19332. // into the global table...
  19333. stack::set_field<true>(L, gcmetakey, usertype_storage_ref);
  19334. usertype_storage_ref.pop();
  19335. // then retrieve the lua-stored version so we have a well-pinned
  19336. // reference that does not die
  19337. stack::get_field<true>(L, gcmetakey);
  19338. usertype_storage<T>& target_umt = stack::pop<user<usertype_storage<T>>>(L);
  19339. return target_umt;
  19340. }
  19341. inline optional<usertype_storage_base&> maybe_get_usertype_storage_base(lua_State* L, int index) {
  19342. stack::record tracking;
  19343. if (!stack::check<user<usertype_storage_base>>(L, index)) {
  19344. return nullopt;
  19345. }
  19346. usertype_storage_base& target_umt = stack::stack_detail::unchecked_unqualified_get<user<usertype_storage_base>>(L, -1, tracking);
  19347. return target_umt;
  19348. }
  19349. inline optional<usertype_storage_base&> maybe_get_usertype_storage_base(lua_State* L, const char* gcmetakey) {
  19350. stack::get_field<true>(L, gcmetakey);
  19351. auto maybe_storage = maybe_get_usertype_storage_base(L, lua_gettop(L));
  19352. lua_pop(L, 1);
  19353. return maybe_storage;
  19354. }
  19355. inline usertype_storage_base& get_usertype_storage_base(lua_State* L, const char* gcmetakey) {
  19356. stack::get_field<true>(L, gcmetakey);
  19357. stack::record tracking;
  19358. usertype_storage_base& target_umt = stack::stack_detail::unchecked_unqualified_get<user<usertype_storage_base>>(L, -1, tracking);
  19359. lua_pop(L, 1);
  19360. return target_umt;
  19361. }
  19362. template <typename T>
  19363. inline optional<usertype_storage<T>&> maybe_get_usertype_storage(lua_State* L) {
  19364. const char* gcmetakey = &usertype_traits<T>::gc_table()[0];
  19365. stack::get_field<true>(L, gcmetakey);
  19366. int target = lua_gettop(L);
  19367. if (!stack::check<user<usertype_storage<T>>>(L, target)) {
  19368. return nullopt;
  19369. }
  19370. usertype_storage<T>& target_umt = stack::pop<user<usertype_storage<T>>>(L);
  19371. return target_umt;
  19372. }
  19373. template <typename T>
  19374. inline usertype_storage<T>& get_usertype_storage(lua_State* L) {
  19375. const char* gcmetakey = &usertype_traits<T>::gc_table()[0];
  19376. stack::get_field<true>(L, gcmetakey);
  19377. usertype_storage<T>& target_umt = stack::pop<user<usertype_storage<T>>>(L);
  19378. return target_umt;
  19379. }
  19380. template <typename T>
  19381. inline void delete_usertype_storage(lua_State* L) {
  19382. using u_traits = usertype_traits<T>;
  19383. #if 0
  19384. using u_const_traits = usertype_traits<const T>;
  19385. using u_unique_traits = usertype_traits<detail::unique_usertype<T>>;
  19386. using u_ref_traits = usertype_traits<T*>;
  19387. using u_const_ref_traits = usertype_traits<T const*>;
  19388. #endif
  19389. using uts = usertype_storage<T>;
  19390. const char* gcmetakey = &u_traits::gc_table()[0];
  19391. stack::get_field<true>(L, gcmetakey);
  19392. if (!stack::check<user<uts>>(L)) {
  19393. lua_pop(L, 1);
  19394. return;
  19395. }
  19396. usertype_storage<T>& target_umt = stack::pop<user<usertype_storage<T>>>(L);
  19397. target_umt.clear();
  19398. // get the registry
  19399. #if 0
  19400. stack_reference registry(L, raw_index(LUA_REGISTRYINDEX));
  19401. registry.push();
  19402. // eliminate all named entries for this usertype
  19403. // in the registry (luaL_newmetatable does
  19404. // [name] = new table
  19405. // in registry upon creation
  19406. stack::set_field(L, &u_traits::metatable()[0], lua_nil, registry.stack_index());
  19407. stack::set_field(L, &u_const_traits::metatable()[0], lua_nil, registry.stack_index());
  19408. stack::set_field(L, &u_const_ref_traits::metatable()[0], lua_nil, registry.stack_index());
  19409. stack::set_field(L, &u_ref_traits::metatable()[0], lua_nil, registry.stack_index());
  19410. stack::set_field(L, &u_unique_traits::metatable()[0], lua_nil, registry.stack_index());
  19411. registry.pop();
  19412. #endif // Registry Cleanout
  19413. stack::set_field<true>(L, gcmetakey, lua_nil);
  19414. }
  19415. template <typename T>
  19416. inline int register_usertype(lua_State* L, automagic_enrollments enrollments = {}) {
  19417. using u_traits = usertype_traits<T>;
  19418. using u_const_traits = usertype_traits<const T>;
  19419. using u_unique_traits = usertype_traits<detail::unique_usertype<T>>;
  19420. using u_ref_traits = usertype_traits<T*>;
  19421. using u_const_ref_traits = usertype_traits<T const*>;
  19422. using uts = usertype_storage<T>;
  19423. // always have __new_index point to usertype_storage method
  19424. // have __index always point to regular fast-lookup
  19425. // meta_method table
  19426. // if __new_index is invoked, runtime-swap
  19427. // to slow __index if necessary
  19428. // (no speed penalty because function calls
  19429. // are all read-only -- only depend on __index
  19430. // to retrieve function and then call happens VIA Lua)
  19431. // __type entry:
  19432. // table contains key -> value lookup,
  19433. // where key is entry in metatable
  19434. // and value is type information as a string as
  19435. // best as we can give it
  19436. // name entry:
  19437. // string that contains raw class name,
  19438. // as defined from C++
  19439. // is entry:
  19440. // checks if argument supplied is of type T
  19441. // __storage entry:
  19442. // a light userdata pointing to the storage
  19443. // mostly to enable this new abstraction
  19444. // to not require the type name `T`
  19445. // to get at the C++ usertype storage within
  19446. // we then let typical definitions potentially override these intrinsics
  19447. // it's the user's fault if they override things or screw them up:
  19448. // these names have been reserved and documented since sol3
  19449. // STEP 0: tell the old usertype (if it exists)
  19450. // to fuck off
  19451. delete_usertype_storage<T>(L);
  19452. // STEP 1: Create backing store for usertype storage
  19453. // Pretty much the most important step.
  19454. // STEP 2: Create Lua tables used for fast method indexing.
  19455. // This is done inside of the storage table's constructor
  19456. usertype_storage<T>& storage = create_usertype_storage<T>(L);
  19457. usertype_storage_base& base_storage = storage;
  19458. void* light_storage = static_cast<void*>(&storage);
  19459. void* light_base_storage = static_cast<void*>(&base_storage);
  19460. // STEP 3: set up GC escape hatch table entirely
  19461. storage.gc_names_table.push();
  19462. stack_reference gnt(L, -1);
  19463. stack::set_field(L, submetatable_type::named, &u_traits::gc_table()[0], gnt.stack_index());
  19464. stack::set_field(L, submetatable_type::const_value, &u_const_traits::metatable()[0], gnt.stack_index());
  19465. stack::set_field(L, submetatable_type::const_reference, &u_const_ref_traits::metatable()[0], gnt.stack_index());
  19466. stack::set_field(L, submetatable_type::reference, &u_ref_traits::metatable()[0], gnt.stack_index());
  19467. stack::set_field(L, submetatable_type::unique, &u_unique_traits::metatable()[0], gnt.stack_index());
  19468. stack::set_field(L, submetatable_type::value, &u_traits::metatable()[0], gnt.stack_index());
  19469. gnt.pop();
  19470. // STEP 4: add some useful information to the type table
  19471. stack_reference stacked_type_table(L, -storage.type_table.push());
  19472. stack::set_field(L, "name", detail::demangle<T>(), stacked_type_table.stack_index());
  19473. stack::set_field(L, "is", &detail::is_check<T>, stacked_type_table.stack_index());
  19474. stacked_type_table.pop();
  19475. // STEP 5: create and hook up metatable,
  19476. // add intrinsics
  19477. // this one is the actual meta-handling table,
  19478. // the next one will be the one for
  19479. int for_each_backing_metatable_calls = 0;
  19480. auto for_each_backing_metatable = [&](lua_State* L, submetatable_type smt, reference& fast_index_table) {
  19481. // Pointer types, AKA "references" from C++
  19482. const char* metakey = nullptr;
  19483. switch (smt) {
  19484. case submetatable_type::const_value:
  19485. metakey = &u_const_traits::metatable()[0];
  19486. break;
  19487. case submetatable_type::reference:
  19488. metakey = &u_ref_traits::metatable()[0];
  19489. break;
  19490. case submetatable_type::unique:
  19491. metakey = &u_unique_traits::metatable()[0];
  19492. break;
  19493. case submetatable_type::const_reference:
  19494. metakey = &u_const_ref_traits::metatable()[0];
  19495. break;
  19496. case submetatable_type::named:
  19497. metakey = &u_traits::user_metatable()[0];
  19498. break;
  19499. case submetatable_type::value:
  19500. default:
  19501. metakey = &u_traits::metatable()[0];
  19502. break;
  19503. }
  19504. luaL_newmetatable(L, metakey);
  19505. if (smt == submetatable_type::named) {
  19506. // the named table itself
  19507. // gets the associated name value
  19508. storage.named_metatable = reference(L, -1);
  19509. lua_pop(L, 1);
  19510. // but the thing we perform the methods on
  19511. // is still the metatable of the named
  19512. // table
  19513. lua_createtable(L, 0, 6);
  19514. }
  19515. stack_reference t(L, -1);
  19516. fast_index_table = reference(t);
  19517. stack::set_field<false, true>(L, meta_function::type, storage.type_table, t.stack_index());
  19518. if constexpr (std::is_destructible_v<T>) {
  19519. // destructible: serialize default
  19520. // destructor here
  19521. switch (smt) {
  19522. case submetatable_type::const_reference:
  19523. case submetatable_type::reference:
  19524. case submetatable_type::named:
  19525. break;
  19526. case submetatable_type::unique:
  19527. stack::set_field<false, true>(L, meta_function::garbage_collect, &detail::unique_destruct<T>, t.stack_index());
  19528. break;
  19529. case submetatable_type::value:
  19530. case submetatable_type::const_value:
  19531. default:
  19532. stack::set_field<false, true>(L, meta_function::garbage_collect, detail::make_destructor<T>(), t.stack_index());
  19533. break;
  19534. }
  19535. }
  19536. else {
  19537. // not destructible: serialize a
  19538. // "hey you messed up"
  19539. // destructor
  19540. switch (smt) {
  19541. case submetatable_type::const_reference:
  19542. case submetatable_type::reference:
  19543. case submetatable_type::named:
  19544. break;
  19545. case submetatable_type::unique:
  19546. stack::set_field<false, true>(L, meta_function::garbage_collect, &detail::cannot_destruct<T>, t.stack_index());
  19547. break;
  19548. case submetatable_type::value:
  19549. case submetatable_type::const_value:
  19550. default:
  19551. stack::set_field<false, true>(L, meta_function::garbage_collect, &detail::cannot_destruct<T>, t.stack_index());
  19552. break;
  19553. }
  19554. }
  19555. static_assert(sizeof(void*) <= sizeof(detail::inheritance_check_function),
  19556. "The size of this data pointer is too small to fit the inheritance checking function: file a bug "
  19557. "report.");
  19558. static_assert(sizeof(void*) <= sizeof(detail::inheritance_cast_function),
  19559. "The size of this data pointer is too small to fit the inheritance checking function: file a bug "
  19560. "report.");
  19561. stack::set_field<false, true>(L, detail::base_class_check_key(), reinterpret_cast<void*>(&detail::inheritance<T>::type_check), t.stack_index());
  19562. stack::set_field<false, true>(L, detail::base_class_cast_key(), reinterpret_cast<void*>(&detail::inheritance<T>::type_cast), t.stack_index());
  19563. auto prop_fx = detail::properties_enrollment_allowed(for_each_backing_metatable_calls, storage.properties, enrollments);
  19564. auto insert_fx = [&L, &t, &storage](meta_function mf, lua_CFunction reg) {
  19565. stack::set_field<false, true>(L, mf, reg, t.stack_index());
  19566. storage.properties[static_cast<int>(mf)] = true;
  19567. };
  19568. detail::insert_default_registrations<T>(insert_fx, prop_fx);
  19569. // There are no variables, so serialize the fast function stuff
  19570. // be sure to reset the index stuff to the non-fast version
  19571. // if the user ever adds something later!
  19572. if (smt == submetatable_type::named) {
  19573. // add escape hatch storage pointer and gc names
  19574. stack::set_field<false, true>(L, meta_function::storage, light_base_storage, t.stack_index());
  19575. stack::set_field<false, true>(L, meta_function::gc_names, storage.gc_names_table, t.stack_index());
  19576. // fancy new_indexing when using the named table
  19577. {
  19578. absolute_index named_metatable_index(L, -storage.named_metatable.push());
  19579. stack::set_field<false, true>(L, metatable_key, t, named_metatable_index);
  19580. storage.named_metatable.pop();
  19581. }
  19582. stack_reference stack_metametatable(L, -storage.named_index_table.push());
  19583. stack::set_field<false, true>(L,
  19584. meta_function::index,
  19585. make_closure(uts::template meta_index_call<false>, nullptr, light_storage, light_base_storage, nullptr, toplevel_magic),
  19586. stack_metametatable.stack_index());
  19587. stack::set_field<false, true>(L,
  19588. meta_function::new_index,
  19589. make_closure(uts::template meta_index_call<true>, nullptr, light_storage, light_base_storage, nullptr, toplevel_magic),
  19590. stack_metametatable.stack_index());
  19591. stack_metametatable.pop();
  19592. }
  19593. else {
  19594. // otherwise just plain for index,
  19595. // and elaborated for new_index
  19596. stack::set_field<false, true>(L, meta_function::index, t, t.stack_index());
  19597. stack::set_field<false, true>(L,
  19598. meta_function::new_index,
  19599. make_closure(uts::template index_call<true>, nullptr, light_storage, light_base_storage, nullptr, toplevel_magic),
  19600. t.stack_index());
  19601. storage.is_using_new_index = true;
  19602. }
  19603. ++for_each_backing_metatable_calls;
  19604. fast_index_table = reference(L, t);
  19605. t.pop();
  19606. };
  19607. storage.for_each_table(L, for_each_backing_metatable);
  19608. // can only use set AFTER we initialize all the metatables
  19609. if constexpr (std::is_default_constructible_v<T>) {
  19610. if (enrollments.default_constructor) {
  19611. storage.set(L, meta_function::construct, constructors<T()>());
  19612. }
  19613. }
  19614. // return the named metatable we want names linked into
  19615. storage.named_metatable.push();
  19616. return 1;
  19617. }
  19618. }} // namespace sol::u_detail
  19619. // end of sol/usertype_storage.hpp
  19620. // beginning of sol/usertype_proxy.hpp
  19621. namespace sol {
  19622. template <typename Table, typename Key>
  19623. struct usertype_proxy : public proxy_base<usertype_proxy<Table, Key>> {
  19624. private:
  19625. using key_type = detail::proxy_key_t<Key>;
  19626. template <typename T, std::size_t... I>
  19627. decltype(auto) tuple_get(std::index_sequence<I...>) const & {
  19628. return tbl.template traverse_get<T>(std::get<I>(key)...);
  19629. }
  19630. template <typename T, std::size_t... I>
  19631. decltype(auto) tuple_get(std::index_sequence<I...>) && {
  19632. return tbl.template traverse_get<T>(std::get<I>(std::move(key))...);
  19633. }
  19634. template <std::size_t... I, typename T>
  19635. void tuple_set(std::index_sequence<I...>, T&& value) & {
  19636. if constexpr (sizeof...(I) > 1) {
  19637. tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
  19638. }
  19639. else {
  19640. tbl.set(std::get<I>(key)..., std::forward<T>(value));
  19641. }
  19642. }
  19643. template <std::size_t... I, typename T>
  19644. void tuple_set(std::index_sequence<I...>, T&& value) && {
  19645. if constexpr (sizeof...(I) > 1) {
  19646. tbl.traverse_set(std::get<I>(std::move(key))..., std::forward<T>(value));
  19647. }
  19648. else {
  19649. tbl.set(std::get<I>(std::move(key))..., std::forward<T>(value));
  19650. }
  19651. }
  19652. public:
  19653. Table tbl;
  19654. key_type key;
  19655. template <typename T>
  19656. usertype_proxy(Table table, T&& k)
  19657. : tbl(table), key(std::forward<T>(k)) {
  19658. }
  19659. template <typename T>
  19660. usertype_proxy& set(T&& item) & {
  19661. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  19662. tuple_set(idx_seq(), std::forward<T>(item));
  19663. return *this;
  19664. }
  19665. template <typename T>
  19666. usertype_proxy&& set(T&& item) && {
  19667. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  19668. std::move(*this).tuple_set(idx_seq(), std::forward<T>(item));
  19669. return std::move(*this);
  19670. }
  19671. template <typename T>
  19672. usertype_proxy& operator=(T&& other) & {
  19673. return set(std::forward<T>(other));
  19674. }
  19675. template <typename T>
  19676. usertype_proxy&& operator=(T&& other) && {
  19677. return std::move(*this).set(std::forward<T>(other));
  19678. }
  19679. template <typename T>
  19680. usertype_proxy& operator=(std::initializer_list<T> other) & {
  19681. return set(std::move(other));
  19682. }
  19683. template <typename T>
  19684. usertype_proxy&& operator=(std::initializer_list<T> other) && {
  19685. return std::move(*this).set(std::move(other));
  19686. }
  19687. template <typename T>
  19688. decltype(auto) get() const& {
  19689. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  19690. return tuple_get<T>(idx_seq());
  19691. }
  19692. template <typename T>
  19693. decltype(auto) get() && {
  19694. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  19695. return std::move(*this).template tuple_get<T>(idx_seq());
  19696. }
  19697. template <typename K>
  19698. decltype(auto) operator[](K&& k) const& {
  19699. auto keys = meta::tuplefy(key, std::forward<K>(k));
  19700. return usertype_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  19701. }
  19702. template <typename K>
  19703. decltype(auto) operator[](K&& k) & {
  19704. auto keys = meta::tuplefy(key, std::forward<K>(k));
  19705. return usertype_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  19706. }
  19707. template <typename K>
  19708. decltype(auto) operator[](K&& k) && {
  19709. auto keys = meta::tuplefy(std::move(key), std::forward<K>(k));
  19710. return usertype_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  19711. }
  19712. template <typename... Ret, typename... Args>
  19713. decltype(auto) call(Args&&... args) {
  19714. #if !defined(__clang__) && defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 191200000
  19715. // MSVC is ass sometimes
  19716. return get<function>().call<Ret...>(std::forward<Args>(args)...);
  19717. #else
  19718. return get<function>().template call<Ret...>(std::forward<Args>(args)...);
  19719. #endif
  19720. }
  19721. template <typename... Args>
  19722. decltype(auto) operator()(Args&&... args) {
  19723. return call<>(std::forward<Args>(args)...);
  19724. }
  19725. bool valid() const {
  19726. auto pp = stack::push_pop(tbl);
  19727. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  19728. lua_pop(lua_state(), p.levels);
  19729. return p;
  19730. }
  19731. int push() const noexcept {
  19732. return push(this->lua_state());
  19733. }
  19734. int push(lua_State* L) const noexcept {
  19735. return get<reference>().push(L);
  19736. }
  19737. type get_type() const {
  19738. type t = type::none;
  19739. auto pp = stack::push_pop(tbl);
  19740. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  19741. if (p) {
  19742. t = type_of(lua_state(), -1);
  19743. }
  19744. lua_pop(lua_state(), p.levels);
  19745. return t;
  19746. }
  19747. lua_State* lua_state() const {
  19748. return tbl.lua_state();
  19749. }
  19750. };
  19751. } // namespace sol
  19752. // end of sol/usertype_proxy.hpp
  19753. // beginning of sol/metatable.hpp
  19754. // beginning of sol/table_core.hpp
  19755. // beginning of sol/table_proxy.hpp
  19756. namespace sol {
  19757. template <typename Table, typename Key>
  19758. struct table_proxy : public proxy_base<table_proxy<Table, Key>> {
  19759. private:
  19760. using key_type = detail::proxy_key_t<Key>;
  19761. template <typename T, std::size_t... I>
  19762. decltype(auto) tuple_get(std::index_sequence<I...>) const& {
  19763. return tbl.template traverse_get<T>(std::get<I>(key)...);
  19764. }
  19765. template <typename T, std::size_t... I>
  19766. decltype(auto) tuple_get(std::index_sequence<I...>) && {
  19767. return tbl.template traverse_get<T>(std::get<I>(std::move(key))...);
  19768. }
  19769. template <std::size_t... I, typename T>
  19770. void tuple_set(std::index_sequence<I...>, T&& value) & {
  19771. tbl.traverse_set(std::get<I>(key)..., std::forward<T>(value));
  19772. }
  19773. template <std::size_t... I, typename T>
  19774. void tuple_set(std::index_sequence<I...>, T&& value) && {
  19775. tbl.traverse_set(std::get<I>(std::move(key))..., std::forward<T>(value));
  19776. }
  19777. auto setup_table(std::true_type) {
  19778. auto p = stack::probe_get_field<std::is_same_v<meta::unqualified_t<Table>, global_table>>(lua_state(), key, tbl.stack_index());
  19779. lua_pop(lua_state(), p.levels);
  19780. return p;
  19781. }
  19782. bool is_valid(std::false_type) {
  19783. auto pp = stack::push_pop(tbl);
  19784. auto p = stack::probe_get_field<std::is_same_v<meta::unqualified_t<Table>, global_table>>(lua_state(), key, lua_gettop(lua_state()));
  19785. lua_pop(lua_state(), p.levels);
  19786. return p;
  19787. }
  19788. public:
  19789. Table tbl;
  19790. key_type key;
  19791. template <typename T>
  19792. table_proxy(Table table, T&& k) : tbl(table), key(std::forward<T>(k)) {
  19793. }
  19794. template <typename T>
  19795. table_proxy& set(T&& item) & {
  19796. tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
  19797. return *this;
  19798. }
  19799. template <typename T>
  19800. table_proxy&& set(T&& item) && {
  19801. tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
  19802. return std::move(*this);
  19803. }
  19804. template <typename... Args>
  19805. table_proxy& set_function(Args&&... args) & {
  19806. tbl.set_function(key, std::forward<Args>(args)...);
  19807. return *this;
  19808. }
  19809. template <typename... Args>
  19810. table_proxy&& set_function(Args&&... args) && {
  19811. tbl.set_function(std::move(key), std::forward<Args>(args)...);
  19812. return std::move(*this);
  19813. }
  19814. template <typename T>
  19815. table_proxy& operator=(T&& other) & {
  19816. using Tu = meta::unwrap_unqualified_t<T>;
  19817. if constexpr (!is_lua_reference_or_proxy_v<Tu> && meta::is_callable_v<Tu>) {
  19818. return set_function(std::forward<T>(other));
  19819. }
  19820. else {
  19821. return set(std::forward<T>(other));
  19822. }
  19823. }
  19824. template <typename T>
  19825. table_proxy&& operator=(T&& other) && {
  19826. using Tu = meta::unwrap_unqualified_t<T>;
  19827. if constexpr (!is_lua_reference_or_proxy_v<Tu> && meta::is_callable_v<Tu>) {
  19828. return std::move(*this).set_function(std::forward<T>(other));
  19829. }
  19830. else {
  19831. return std::move(*this).set(std::forward<T>(other));
  19832. }
  19833. }
  19834. template <typename T>
  19835. table_proxy& operator=(std::initializer_list<T> other) & {
  19836. return set(std::move(other));
  19837. }
  19838. template <typename T>
  19839. table_proxy&& operator=(std::initializer_list<T> other) && {
  19840. return std::move(*this).set(std::move(other));
  19841. }
  19842. template <typename T>
  19843. decltype(auto) get() const& {
  19844. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  19845. return tuple_get<T>(idx_seq());
  19846. }
  19847. template <typename T>
  19848. decltype(auto) get() && {
  19849. using idx_seq = std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>;
  19850. return std::move(*this).template tuple_get<T>(idx_seq());
  19851. }
  19852. template <typename T>
  19853. decltype(auto) get_or(T&& otherwise) const {
  19854. typedef decltype(get<T>()) U;
  19855. optional<U> option = get<optional<U>>();
  19856. if (option) {
  19857. return static_cast<U>(option.value());
  19858. }
  19859. return static_cast<U>(std::forward<T>(otherwise));
  19860. }
  19861. template <typename T, typename D>
  19862. decltype(auto) get_or(D&& otherwise) const {
  19863. optional<T> option = get<optional<T>>();
  19864. if (option) {
  19865. return static_cast<T>(option.value());
  19866. }
  19867. return static_cast<T>(std::forward<D>(otherwise));
  19868. }
  19869. template <typename T>
  19870. decltype(auto) get_or_create() {
  19871. return get_or_create<T>(new_table());
  19872. }
  19873. template <typename T, typename Otherwise>
  19874. decltype(auto) get_or_create(Otherwise&& other) {
  19875. if (!this->valid()) {
  19876. this->set(std::forward<Otherwise>(other));
  19877. }
  19878. return get<T>();
  19879. }
  19880. template <typename K>
  19881. decltype(auto) operator[](K&& k) const& {
  19882. auto keys = meta::tuplefy(key, std::forward<K>(k));
  19883. return table_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  19884. }
  19885. template <typename K>
  19886. decltype(auto) operator[](K&& k) & {
  19887. auto keys = meta::tuplefy(key, std::forward<K>(k));
  19888. return table_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  19889. }
  19890. template <typename K>
  19891. decltype(auto) operator[](K&& k) && {
  19892. auto keys = meta::tuplefy(std::move(key), std::forward<K>(k));
  19893. return table_proxy<Table, decltype(keys)>(tbl, std::move(keys));
  19894. }
  19895. template <typename... Ret, typename... Args>
  19896. decltype(auto) call(Args&&... args) {
  19897. lua_State* L = this->lua_state();
  19898. push(L);
  19899. int idx = lua_gettop(L);
  19900. stack_aligned_function func(L, idx);
  19901. return func.call<Ret...>(std::forward<Args>(args)...);
  19902. }
  19903. template <typename... Args>
  19904. decltype(auto) operator()(Args&&... args) {
  19905. return call<>(std::forward<Args>(args)...);
  19906. }
  19907. bool valid() const {
  19908. auto pp = stack::push_pop(tbl);
  19909. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  19910. lua_pop(lua_state(), p.levels);
  19911. return p;
  19912. }
  19913. int push() const noexcept {
  19914. return push(this->lua_state());
  19915. }
  19916. int push(lua_State* L) const noexcept {
  19917. if constexpr (std::is_same_v<meta::unqualified_t<Table>, global_table> || is_stack_table_v<meta::unqualified_t<Table>>) {
  19918. auto pp = stack::push_pop<true>(tbl);
  19919. int tableindex = pp.index_of(tbl);
  19920. int top_index = lua_gettop(L);
  19921. stack::get_field<true>(lua_state(), key, tableindex);
  19922. lua_replace(L, top_index + 1);
  19923. lua_settop(L, top_index + 1);
  19924. }
  19925. else {
  19926. auto pp = stack::push_pop<false>(tbl);
  19927. int tableindex = pp.index_of(tbl);
  19928. int aftertableindex = lua_gettop(L);
  19929. stack::get_field<false>(lua_state(), key, tableindex);
  19930. lua_replace(L, tableindex);
  19931. lua_settop(L, aftertableindex + 1);
  19932. }
  19933. return 1;
  19934. }
  19935. type get_type() const {
  19936. type t = type::none;
  19937. auto pp = stack::push_pop(tbl);
  19938. auto p = stack::probe_get_field<std::is_same<meta::unqualified_t<Table>, global_table>::value>(lua_state(), key, lua_gettop(lua_state()));
  19939. if (p) {
  19940. t = type_of(lua_state(), -1);
  19941. }
  19942. lua_pop(lua_state(), p.levels);
  19943. return t;
  19944. }
  19945. lua_State* lua_state() const {
  19946. return tbl.lua_state();
  19947. }
  19948. table_proxy& force() {
  19949. if (!this->valid()) {
  19950. this->set(new_table());
  19951. }
  19952. return *this;
  19953. }
  19954. };
  19955. template <typename Table, typename Key, typename T>
  19956. inline bool operator==(T&& left, const table_proxy<Table, Key>& right) {
  19957. using G = decltype(stack::get<T>(nullptr, 0));
  19958. return right.template get<optional<G>>() == left;
  19959. }
  19960. template <typename Table, typename Key, typename T>
  19961. inline bool operator==(const table_proxy<Table, Key>& right, T&& left) {
  19962. using G = decltype(stack::get<T>(nullptr, 0));
  19963. return right.template get<optional<G>>() == left;
  19964. }
  19965. template <typename Table, typename Key, typename T>
  19966. inline bool operator!=(T&& left, const table_proxy<Table, Key>& right) {
  19967. using G = decltype(stack::get<T>(nullptr, 0));
  19968. return right.template get<optional<G>>() != left;
  19969. }
  19970. template <typename Table, typename Key, typename T>
  19971. inline bool operator!=(const table_proxy<Table, Key>& right, T&& left) {
  19972. using G = decltype(stack::get<T>(nullptr, 0));
  19973. return right.template get<optional<G>>() != left;
  19974. }
  19975. template <typename Table, typename Key>
  19976. inline bool operator==(lua_nil_t, const table_proxy<Table, Key>& right) {
  19977. return !right.valid();
  19978. }
  19979. template <typename Table, typename Key>
  19980. inline bool operator==(const table_proxy<Table, Key>& right, lua_nil_t) {
  19981. return !right.valid();
  19982. }
  19983. template <typename Table, typename Key>
  19984. inline bool operator!=(lua_nil_t, const table_proxy<Table, Key>& right) {
  19985. return right.valid();
  19986. }
  19987. template <typename Table, typename Key>
  19988. inline bool operator!=(const table_proxy<Table, Key>& right, lua_nil_t) {
  19989. return right.valid();
  19990. }
  19991. template <bool b>
  19992. template <typename Super>
  19993. basic_reference<b>& basic_reference<b>::operator=(proxy_base<Super>&& r) {
  19994. basic_reference<b> v = r;
  19995. this->operator=(std::move(v));
  19996. return *this;
  19997. }
  19998. template <bool b>
  19999. template <typename Super>
  20000. basic_reference<b>& basic_reference<b>::operator=(const proxy_base<Super>& r) {
  20001. basic_reference<b> v = r;
  20002. this->operator=(std::move(v));
  20003. return *this;
  20004. }
  20005. namespace stack {
  20006. template <typename Table, typename Key>
  20007. struct unqualified_pusher<table_proxy<Table, Key>> {
  20008. static int push(lua_State* L, const table_proxy<Table, Key>& p) {
  20009. return p.push(L);
  20010. }
  20011. };
  20012. } // namespace stack
  20013. } // namespace sol
  20014. // end of sol/table_proxy.hpp
  20015. // beginning of sol/table_iterator.hpp
  20016. #include <iterator>
  20017. namespace sol {
  20018. template <typename reference_type>
  20019. class basic_table_iterator {
  20020. public:
  20021. typedef object key_type;
  20022. typedef object mapped_type;
  20023. typedef std::pair<object, object> value_type;
  20024. typedef std::input_iterator_tag iterator_category;
  20025. typedef std::ptrdiff_t difference_type;
  20026. typedef value_type* pointer;
  20027. typedef value_type& reference;
  20028. typedef const value_type& const_reference;
  20029. private:
  20030. std::pair<object, object> kvp;
  20031. reference_type ref;
  20032. int tableidx = 0;
  20033. int keyidx = 0;
  20034. std::ptrdiff_t idx = 0;
  20035. public:
  20036. basic_table_iterator() : keyidx(-1), idx(-1) {
  20037. }
  20038. basic_table_iterator(reference_type x) : ref(std::move(x)) {
  20039. ref.push();
  20040. tableidx = lua_gettop(ref.lua_state());
  20041. stack::push(ref.lua_state(), lua_nil);
  20042. this->operator++();
  20043. if (idx == -1) {
  20044. return;
  20045. }
  20046. --idx;
  20047. }
  20048. basic_table_iterator& operator++() {
  20049. if (idx == -1)
  20050. return *this;
  20051. if (lua_next(ref.lua_state(), tableidx) == 0) {
  20052. idx = -1;
  20053. keyidx = -1;
  20054. return *this;
  20055. }
  20056. ++idx;
  20057. kvp.first = object(ref.lua_state(), -2);
  20058. kvp.second = object(ref.lua_state(), -1);
  20059. lua_pop(ref.lua_state(), 1);
  20060. // leave key on the stack
  20061. keyidx = lua_gettop(ref.lua_state());
  20062. return *this;
  20063. }
  20064. basic_table_iterator operator++(int) {
  20065. auto saved = *this;
  20066. this->operator++();
  20067. return saved;
  20068. }
  20069. reference operator*() {
  20070. return kvp;
  20071. }
  20072. const_reference operator*() const {
  20073. return kvp;
  20074. }
  20075. bool operator==(const basic_table_iterator& right) const {
  20076. return idx == right.idx;
  20077. }
  20078. bool operator!=(const basic_table_iterator& right) const {
  20079. return idx != right.idx;
  20080. }
  20081. ~basic_table_iterator() {
  20082. if (keyidx != -1) {
  20083. stack::remove(ref.lua_state(), keyidx, 1);
  20084. }
  20085. if (ref.lua_state() != nullptr && ref.valid()) {
  20086. stack::remove(ref.lua_state(), tableidx, 1);
  20087. }
  20088. }
  20089. };
  20090. } // namespace sol
  20091. // end of sol/table_iterator.hpp
  20092. namespace sol {
  20093. namespace detail {
  20094. template <std::size_t n>
  20095. struct clean {
  20096. lua_State* L;
  20097. clean(lua_State* luastate) : L(luastate) {
  20098. }
  20099. ~clean() {
  20100. lua_pop(L, static_cast<int>(n));
  20101. }
  20102. };
  20103. struct ref_clean {
  20104. lua_State* L;
  20105. int& n;
  20106. ref_clean(lua_State* luastate, int& n) : L(luastate), n(n) {
  20107. }
  20108. ~ref_clean() {
  20109. lua_pop(L, static_cast<int>(n));
  20110. }
  20111. };
  20112. inline int fail_on_newindex(lua_State* L) {
  20113. return luaL_error(L, "sol: cannot modify the elements of an enumeration table");
  20114. }
  20115. } // namespace detail
  20116. template <bool top_level, typename ref_t>
  20117. class basic_table_core : public basic_object<ref_t> {
  20118. private:
  20119. using base_t = basic_object<ref_t>;
  20120. friend class state;
  20121. friend class state_view;
  20122. template <typename, typename>
  20123. friend class basic_usertype;
  20124. template <typename>
  20125. friend class basic_metatable;
  20126. template <bool raw, typename... Ret, typename... Keys>
  20127. decltype(auto) tuple_get(int table_index, Keys&&... keys) const {
  20128. if constexpr (sizeof...(Ret) < 2) {
  20129. return traverse_get_single_maybe_tuple<raw, Ret...>(table_index, std::forward<Keys>(keys)...);
  20130. }
  20131. else {
  20132. using multi_ret = decltype(stack::pop<std::tuple<Ret...>>(nullptr));
  20133. return multi_ret(traverse_get_single_maybe_tuple<raw, Ret>(table_index, std::forward<Keys>(keys))...);
  20134. }
  20135. }
  20136. template <bool raw, typename Ret, size_t... I, typename Key>
  20137. decltype(auto) traverse_get_single_tuple(int table_index, std::index_sequence<I...>, Key&& key) const {
  20138. return traverse_get_single<raw, Ret>(table_index, std::get<I>(std::forward<Key>(key))...);
  20139. }
  20140. template <bool raw, typename Ret, typename Key>
  20141. decltype(auto) traverse_get_single_maybe_tuple(int table_index, Key&& key) const {
  20142. if constexpr (meta::is_tuple_v<meta::unqualified_t<Key>>) {
  20143. return traverse_get_single_tuple<raw, Ret>(
  20144. table_index, std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<Key>>>(), std::forward<Key>(key));
  20145. }
  20146. else {
  20147. return traverse_get_single<raw, Ret>(table_index, std::forward<Key>(key));
  20148. }
  20149. }
  20150. template <bool raw, typename Ret, typename... Keys>
  20151. decltype(auto) traverse_get_single(int table_index, Keys&&... keys) const {
  20152. constexpr static bool global = top_level && (meta::count_for_to_pack_v<1, meta::is_c_str, meta::unqualified_t<Keys>...> > 0);
  20153. if constexpr (meta::is_optional_v<meta::unqualified_t<Ret>>) {
  20154. int popcount = 0;
  20155. detail::ref_clean c(base_t::lua_state(), popcount);
  20156. return traverse_get_deep_optional<global, raw, detail::insert_mode::none, Ret>(popcount, table_index, std::forward<Keys>(keys)...);
  20157. }
  20158. else {
  20159. detail::clean<sizeof...(Keys) - meta::count_for_pack_v<detail::is_insert_mode, meta::unqualified_t<Keys>...>> c(base_t::lua_state());
  20160. return traverse_get_deep<global, raw, detail::insert_mode::none, Ret>(table_index, std::forward<Keys>(keys)...);
  20161. }
  20162. }
  20163. template <bool raw, typename Pairs, std::size_t... I>
  20164. void tuple_set(std::index_sequence<I...>, Pairs&& pairs) {
  20165. constexpr static bool global = top_level
  20166. && (meta::count_even_for_pack_v<meta::is_c_str, meta::unqualified_t<decltype(std::get<I * 2>(std::forward<Pairs>(pairs)))>...> > 0);
  20167. auto pp = stack::push_pop<global>(*this);
  20168. int table_index = pp.index_of(*this);
  20169. lua_State* L = base_t::lua_state();
  20170. (void)table_index;
  20171. (void)L;
  20172. void(detail::swallow { (stack::set_field<(top_level), raw>(
  20173. L, std::get<I * 2>(std::forward<Pairs>(pairs)), std::get<I * 2 + 1>(std::forward<Pairs>(pairs)), table_index),
  20174. 0)... });
  20175. }
  20176. template <bool global, bool raw, detail::insert_mode mode, typename T, typename Key, typename... Keys>
  20177. decltype(auto) traverse_get_deep(int table_index, Key&& key, Keys&&... keys) const {
  20178. if constexpr (std::is_same_v<meta::unqualified_t<Key>, create_if_nil_t>) {
  20179. (void)key;
  20180. return traverse_get_deep<false, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::create_if_nil), T>(
  20181. table_index, std::forward<Keys>(keys)...);
  20182. }
  20183. else {
  20184. lua_State* L = base_t::lua_state();
  20185. stack::get_field<global, raw>(L, std::forward<Key>(key), table_index);
  20186. if constexpr (sizeof...(Keys) > 0) {
  20187. if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
  20188. type t = type_of(L, -1);
  20189. if (t == type::lua_nil || t == type::none) {
  20190. lua_pop(L, 1);
  20191. stack::push(L, new_table(0, 0));
  20192. }
  20193. }
  20194. return traverse_get_deep<false, raw, mode, T>(lua_gettop(L), std::forward<Keys>(keys)...);
  20195. }
  20196. else {
  20197. if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
  20198. type t = type_of(L, -1);
  20199. if ((t == type::lua_nil || t == type::none) && (is_table_like_v<T>)) {
  20200. lua_pop(L, 1);
  20201. stack::push(L, new_table(0, 0));
  20202. }
  20203. }
  20204. return stack::get<T>(L);
  20205. }
  20206. }
  20207. }
  20208. template <bool global, bool raw, detail::insert_mode mode, typename T, typename Key, typename... Keys>
  20209. decltype(auto) traverse_get_deep_optional(int& popcount, int table_index, Key&& key, Keys&&... keys) const {
  20210. if constexpr (std::is_same_v<meta::unqualified_t<Key>, create_if_nil_t>) {
  20211. constexpr detail::insert_mode new_mode = static_cast<detail::insert_mode>(mode | detail::insert_mode::create_if_nil);
  20212. (void)key;
  20213. return traverse_get_deep_optional<global, raw, new_mode, T>(popcount, table_index, std::forward<Keys>(keys)...);
  20214. }
  20215. else if constexpr (std::is_same_v<meta::unqualified_t<Key>, update_if_empty_t>) {
  20216. constexpr detail::insert_mode new_mode = static_cast<detail::insert_mode>(mode | detail::insert_mode::update_if_empty);
  20217. (void)key;
  20218. return traverse_get_deep_optional<global, raw, new_mode, T>(popcount, table_index, std::forward<Keys>(keys)...);
  20219. }
  20220. else if constexpr (std::is_same_v<meta::unqualified_t<Key>, override_value_t>) {
  20221. constexpr detail::insert_mode new_mode = static_cast<detail::insert_mode>(mode | detail::insert_mode::override_value);
  20222. (void)key;
  20223. return traverse_get_deep_optional<global, raw, new_mode, T>(popcount, table_index, std::forward<Keys>(keys)...);
  20224. }
  20225. else {
  20226. if constexpr (sizeof...(Keys) > 0) {
  20227. lua_State* L = base_t::lua_state();
  20228. auto p = stack::probe_get_field<global, raw>(L, std::forward<Key>(key), table_index);
  20229. popcount += p.levels;
  20230. if (!p.success) {
  20231. if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
  20232. lua_pop(L, 1);
  20233. constexpr bool is_seq = meta::count_for_to_pack_v<1, std::is_integral, Keys...> > 0;
  20234. stack::push(L, new_table(static_cast<int>(is_seq), static_cast<int>(!is_seq)));
  20235. stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
  20236. }
  20237. else {
  20238. return T(nullopt);
  20239. }
  20240. }
  20241. return traverse_get_deep_optional<false, raw, mode, T>(popcount, lua_gettop(L), std::forward<Keys>(keys)...);
  20242. }
  20243. else {
  20244. using R = decltype(stack::get<T>(nullptr));
  20245. using value_type = typename meta::unqualified_t<R>::value_type;
  20246. lua_State* L = base_t::lua_state();
  20247. auto p = stack::probe_get_field<global, raw, value_type>(L, key, table_index);
  20248. popcount += p.levels;
  20249. if (!p.success) {
  20250. if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
  20251. lua_pop(L, 1);
  20252. stack::push(L, new_table(0, 0));
  20253. stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
  20254. if (stack::check<value_type>(L, lua_gettop(L), no_panic)) {
  20255. return stack::get<T>(L);
  20256. }
  20257. }
  20258. return R(nullopt);
  20259. }
  20260. return stack::get<T>(L);
  20261. }
  20262. }
  20263. }
  20264. template <bool global, bool raw, detail::insert_mode mode, typename Key, typename... Keys>
  20265. void traverse_set_deep(int table_index, Key&& key, Keys&&... keys) const {
  20266. using KeyU = meta::unqualified_t<Key>;
  20267. if constexpr (std::is_same_v<KeyU, update_if_empty_t>) {
  20268. (void)key;
  20269. traverse_set_deep<global, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::update_if_empty)>(
  20270. table_index, std::forward<Keys>(keys)...);
  20271. }
  20272. else if constexpr (std::is_same_v<KeyU, create_if_nil_t>) {
  20273. (void)key;
  20274. traverse_set_deep<global, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::create_if_nil)>(
  20275. table_index, std::forward<Keys>(keys)...);
  20276. }
  20277. else if constexpr (std::is_same_v<KeyU, override_value_t>) {
  20278. (void)key;
  20279. traverse_set_deep<global, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::override_value)>(
  20280. table_index, std::forward<Keys>(keys)...);
  20281. }
  20282. else {
  20283. lua_State* L = base_t::lua_state();
  20284. if constexpr (sizeof...(Keys) == 1) {
  20285. if constexpr ((mode & detail::insert_mode::update_if_empty) == detail::insert_mode::update_if_empty) {
  20286. auto p = stack::probe_get_field<global, raw>(L, key, table_index);
  20287. lua_pop(L, p.levels);
  20288. if (!p.success) {
  20289. stack::set_field<global, raw>(L, std::forward<Key>(key), std::forward<Keys>(keys)..., table_index);
  20290. }
  20291. }
  20292. else {
  20293. stack::set_field<global, raw>(L, std::forward<Key>(key), std::forward<Keys>(keys)..., table_index);
  20294. }
  20295. }
  20296. else {
  20297. if constexpr (mode != detail::insert_mode::none) {
  20298. stack::get_field<global, raw>(L, key, table_index);
  20299. type vt = type_of(L, -1);
  20300. if constexpr ((mode & detail::insert_mode::update_if_empty) == detail::insert_mode::update_if_empty
  20301. || (mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
  20302. if (vt == type::lua_nil || vt == type::none) {
  20303. constexpr bool is_seq = meta::count_for_to_pack_v<1, std::is_integral, Keys...> > 0;
  20304. lua_pop(L, 1);
  20305. stack::push(L, new_table(static_cast<int>(is_seq), static_cast<int>(!is_seq)));
  20306. stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
  20307. }
  20308. }
  20309. else {
  20310. if (vt != type::table) {
  20311. constexpr bool is_seq = meta::count_for_to_pack_v<1, std::is_integral, Keys...> > 0;
  20312. lua_pop(L, 1);
  20313. stack::push(L, new_table(static_cast<int>(is_seq), static_cast<int>(!is_seq)));
  20314. stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
  20315. }
  20316. }
  20317. }
  20318. else {
  20319. stack::get_field<global, raw>(L, std::forward<Key>(key), table_index);
  20320. }
  20321. traverse_set_deep<false, raw, mode>(lua_gettop(L), std::forward<Keys>(keys)...);
  20322. }
  20323. }
  20324. }
  20325. basic_table_core(lua_State* L, detail::global_tag t) noexcept : base_t(L, t) {
  20326. }
  20327. protected:
  20328. basic_table_core(detail::no_safety_tag, lua_nil_t n) : base_t(n) {
  20329. }
  20330. basic_table_core(detail::no_safety_tag, lua_State* L, int index) : base_t(L, index) {
  20331. }
  20332. basic_table_core(detail::no_safety_tag, lua_State* L, ref_index index) : base_t(L, index) {
  20333. }
  20334. template <typename T,
  20335. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<ref_t, stack_reference>>,
  20336. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20337. basic_table_core(detail::no_safety_tag, T&& r) noexcept : base_t(std::forward<T>(r)) {
  20338. }
  20339. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20340. basic_table_core(detail::no_safety_tag, lua_State* L, T&& r) noexcept : base_t(L, std::forward<T>(r)) {
  20341. }
  20342. public:
  20343. using iterator = basic_table_iterator<ref_t>;
  20344. using const_iterator = iterator;
  20345. using base_t::lua_state;
  20346. basic_table_core() noexcept = default;
  20347. basic_table_core(const basic_table_core&) = default;
  20348. basic_table_core(basic_table_core&&) = default;
  20349. basic_table_core& operator=(const basic_table_core&) = default;
  20350. basic_table_core& operator=(basic_table_core&&) = default;
  20351. basic_table_core(const stack_reference& r) : basic_table_core(r.lua_state(), r.stack_index()) {
  20352. }
  20353. basic_table_core(stack_reference&& r) : basic_table_core(r.lua_state(), r.stack_index()) {
  20354. }
  20355. template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20356. basic_table_core(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  20357. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20358. auto pp = stack::push_pop(*this);
  20359. int table_index = pp.index_of(*this);
  20360. constructor_handler handler {};
  20361. stack::check<basic_table_core>(lua_state(), table_index, handler);
  20362. #endif // Safety
  20363. }
  20364. basic_table_core(lua_State* L, const new_table& nt) : base_t(L, -stack::push(L, nt)) {
  20365. if (!is_stack_based<meta::unqualified_t<ref_t>>::value) {
  20366. lua_pop(L, 1);
  20367. }
  20368. }
  20369. basic_table_core(lua_State* L, int index = -1) : basic_table_core(detail::no_safety, L, index) {
  20370. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20371. constructor_handler handler {};
  20372. stack::check<basic_table_core>(L, index, handler);
  20373. #endif // Safety
  20374. }
  20375. basic_table_core(lua_State* L, ref_index index) : basic_table_core(detail::no_safety, L, index) {
  20376. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20377. auto pp = stack::push_pop(*this);
  20378. int table_index = pp.index_of(*this);
  20379. constructor_handler handler {};
  20380. stack::check<basic_table_core>(lua_state(), table_index, handler);
  20381. #endif // Safety
  20382. }
  20383. template <typename T,
  20384. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<ref_t, stack_reference>>,
  20385. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20386. basic_table_core(T&& r) noexcept : basic_table_core(detail::no_safety, std::forward<T>(r)) {
  20387. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20388. if (!is_table<meta::unqualified_t<T>>::value) {
  20389. auto pp = stack::push_pop(*this);
  20390. int table_index = pp.index_of(*this);
  20391. constructor_handler handler {};
  20392. stack::check<basic_table_core>(lua_state(), table_index, handler);
  20393. }
  20394. #endif // Safety
  20395. }
  20396. basic_table_core(lua_nil_t r) noexcept : basic_table_core(detail::no_safety, r) {
  20397. }
  20398. iterator begin() const {
  20399. if (this->get_type() == type::table) {
  20400. return iterator(*this);
  20401. }
  20402. return iterator();
  20403. }
  20404. iterator end() const {
  20405. return iterator();
  20406. }
  20407. const_iterator cbegin() const {
  20408. return begin();
  20409. }
  20410. const_iterator cend() const {
  20411. return end();
  20412. }
  20413. void clear() {
  20414. auto pp = stack::push_pop<false>(*this);
  20415. int table_index = pp.index_of(*this);
  20416. stack::clear(lua_state(), table_index);
  20417. }
  20418. template <typename... Ret, typename... Keys>
  20419. decltype(auto) get(Keys&&... keys) const {
  20420. static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
  20421. constexpr static bool global = meta::all<meta::boolean<top_level>, meta::is_c_str<meta::unqualified_t<Keys>>...>::value;
  20422. auto pp = stack::push_pop<global>(*this);
  20423. int table_index = pp.index_of(*this);
  20424. return tuple_get<false, Ret...>(table_index, std::forward<Keys>(keys)...);
  20425. }
  20426. template <typename T, typename Key>
  20427. decltype(auto) get_or(Key&& key, T&& otherwise) const {
  20428. typedef decltype(get<T>("")) U;
  20429. optional<U> option = get<optional<U>>(std::forward<Key>(key));
  20430. if (option) {
  20431. return static_cast<U>(option.value());
  20432. }
  20433. return static_cast<U>(std::forward<T>(otherwise));
  20434. }
  20435. template <typename T, typename Key, typename D>
  20436. decltype(auto) get_or(Key&& key, D&& otherwise) const {
  20437. optional<T> option = get<optional<T>>(std::forward<Key>(key));
  20438. if (option) {
  20439. return static_cast<T>(option.value());
  20440. }
  20441. return static_cast<T>(std::forward<D>(otherwise));
  20442. }
  20443. template <typename T, typename... Keys>
  20444. decltype(auto) traverse_get(Keys&&... keys) const {
  20445. static_assert(sizeof...(Keys) > 0, "must pass at least 1 key to get");
  20446. constexpr static bool global = top_level && (meta::count_for_to_pack_v<1, meta::is_c_str, meta::unqualified_t<Keys>...> > 0);
  20447. auto pp = stack::push_pop<global>(*this);
  20448. int table_index = pp.index_of(*this);
  20449. return traverse_get_single<false, T>(table_index, std::forward<Keys>(keys)...);
  20450. }
  20451. template <typename... Keys>
  20452. basic_table_core& traverse_set(Keys&&... keys) {
  20453. static_assert(sizeof...(Keys) > 1, "must pass at least 1 key and 1 value to set");
  20454. constexpr static bool global
  20455. = top_level && (meta::count_when_for_to_pack_v<detail::is_not_insert_mode, 1, meta::is_c_str, meta::unqualified_t<Keys>...> > 0);
  20456. auto pp = stack::push_pop<global>(*this);
  20457. int table_index = pp.index_of(*this);
  20458. lua_State* L = base_t::lua_state();
  20459. auto pn = stack::pop_n(L, static_cast<int>(sizeof...(Keys) - 2 - meta::count_for_pack_v<detail::is_insert_mode, meta::unqualified_t<Keys>...>));
  20460. traverse_set_deep<top_level, false, detail::insert_mode::none>(table_index, std::forward<Keys>(keys)...);
  20461. return *this;
  20462. }
  20463. template <typename... Args>
  20464. basic_table_core& set(Args&&... args) {
  20465. if constexpr (sizeof...(Args) == 2) {
  20466. traverse_set(std::forward<Args>(args)...);
  20467. }
  20468. else {
  20469. tuple_set<false>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
  20470. }
  20471. return *this;
  20472. }
  20473. template <typename... Ret, typename... Keys>
  20474. decltype(auto) raw_get(Keys&&... keys) const {
  20475. static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
  20476. constexpr static bool global = top_level && (meta::count_for_to_pack_v<1, meta::is_c_str, meta::unqualified_t<Keys>...> > 0);
  20477. auto pp = stack::push_pop<global>(*this);
  20478. int table_index = pp.index_of(*this);
  20479. return tuple_get<true, Ret...>(table_index, std::forward<Keys>(keys)...);
  20480. }
  20481. template <typename T, typename Key>
  20482. decltype(auto) raw_get_or(Key&& key, T&& otherwise) const {
  20483. typedef decltype(raw_get<T>("")) U;
  20484. optional<U> option = raw_get<optional<U>>(std::forward<Key>(key));
  20485. if (option) {
  20486. return static_cast<U>(option.value());
  20487. }
  20488. return static_cast<U>(std::forward<T>(otherwise));
  20489. }
  20490. template <typename T, typename Key, typename D>
  20491. decltype(auto) raw_get_or(Key&& key, D&& otherwise) const {
  20492. optional<T> option = raw_get<optional<T>>(std::forward<Key>(key));
  20493. if (option) {
  20494. return static_cast<T>(option.value());
  20495. }
  20496. return static_cast<T>(std::forward<D>(otherwise));
  20497. }
  20498. template <typename T, typename... Keys>
  20499. decltype(auto) traverse_raw_get(Keys&&... keys) const {
  20500. constexpr static bool global = top_level && (meta::count_for_to_pack_v<1, meta::is_c_str, meta::unqualified_t<Keys>...> > 0);
  20501. auto pp = stack::push_pop<global>(*this);
  20502. int table_index = pp.index_of(*this);
  20503. return traverse_get_single<true, T>(table_index, std::forward<Keys>(keys)...);
  20504. }
  20505. template <typename... Keys>
  20506. basic_table_core& traverse_raw_set(Keys&&... keys) {
  20507. constexpr static bool global = top_level && (meta::count_for_to_pack_v<1, meta::is_c_str, meta::unqualified_t<Keys>...> > 0);
  20508. auto pp = stack::push_pop<global>(*this);
  20509. lua_State* L = base_t::lua_state();
  20510. auto pn = stack::pop_n(L, static_cast<int>(sizeof...(Keys) - 2 - meta::count_for_pack_v<detail::is_insert_mode, meta::unqualified_t<Keys>...>));
  20511. traverse_set_deep<top_level, true, false>(std::forward<Keys>(keys)...);
  20512. return *this;
  20513. }
  20514. template <typename... Args>
  20515. basic_table_core& raw_set(Args&&... args) {
  20516. tuple_set<true>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
  20517. return *this;
  20518. }
  20519. template <typename Class, typename Key>
  20520. usertype<Class> new_usertype(Key&& key);
  20521. template <typename Class, typename Key>
  20522. usertype<Class> new_usertype(Key&& key, automagic_enrollments enrollment);
  20523. template <typename Class, typename Key, typename Arg, typename... Args,
  20524. typename = std::enable_if_t<!std::is_same_v<meta::unqualified_t<Arg>, automagic_enrollments>>>
  20525. usertype<Class> new_usertype(Key&& key, Arg&& arg, Args&&... args);
  20526. template <bool read_only = true, typename... Args>
  20527. table new_enum(const string_view& name, Args&&... args) {
  20528. table target = create_with(std::forward<Args>(args)...);
  20529. if (read_only) {
  20530. table x = create_with(meta_function::new_index, detail::fail_on_newindex, meta_function::index, target);
  20531. table shim = create_named(name, metatable_key, x);
  20532. return shim;
  20533. }
  20534. else {
  20535. set(name, target);
  20536. return target;
  20537. }
  20538. }
  20539. template <typename T, bool read_only = true>
  20540. table new_enum(const string_view& name, std::initializer_list<std::pair<string_view, T>> items) {
  20541. table target = create(static_cast<int>(items.size()), static_cast<int>(0));
  20542. for (const auto& kvp : items) {
  20543. target.set(kvp.first, kvp.second);
  20544. }
  20545. if constexpr (read_only) {
  20546. table x = create_with(meta_function::new_index, detail::fail_on_newindex, meta_function::index, target);
  20547. table shim = create_named(name, metatable_key, x);
  20548. return shim;
  20549. }
  20550. else {
  20551. set(name, target);
  20552. return target;
  20553. }
  20554. }
  20555. template <typename Key = object, typename Value = object, typename Fx>
  20556. void for_each(Fx&& fx) const {
  20557. lua_State* L = base_t::lua_state();
  20558. if constexpr (std::is_invocable_v<Fx, Key, Value>) {
  20559. auto pp = stack::push_pop(*this);
  20560. int table_index = pp.index_of(*this);
  20561. stack::push(L, lua_nil);
  20562. while (lua_next(L, table_index)) {
  20563. Key key(L, -2);
  20564. Value value(L, -1);
  20565. auto pn = stack::pop_n(L, 1);
  20566. fx(key, value);
  20567. }
  20568. }
  20569. else {
  20570. auto pp = stack::push_pop(*this);
  20571. int table_index = pp.index_of(*this);
  20572. stack::push(L, lua_nil);
  20573. while (lua_next(L, table_index)) {
  20574. Key key(L, -2);
  20575. Value value(L, -1);
  20576. auto pn = stack::pop_n(L, 1);
  20577. std::pair<Key&, Value&> keyvalue(key, value);
  20578. fx(keyvalue);
  20579. }
  20580. }
  20581. }
  20582. size_t size() const {
  20583. auto pp = stack::push_pop(*this);
  20584. int table_index = pp.index_of(*this);
  20585. lua_State* L = base_t::lua_state();
  20586. lua_len(L, table_index);
  20587. return stack::pop<size_t>(L);
  20588. }
  20589. bool empty() const {
  20590. return cbegin() == cend();
  20591. }
  20592. template <typename T>
  20593. auto operator[](T&& key) & {
  20594. return table_proxy<basic_table_core&, detail::proxy_key_t<T>>(*this, std::forward<T>(key));
  20595. }
  20596. template <typename T>
  20597. auto operator[](T&& key) const& {
  20598. return table_proxy<const basic_table_core&, detail::proxy_key_t<T>>(*this, std::forward<T>(key));
  20599. }
  20600. template <typename T>
  20601. auto operator[](T&& key) && {
  20602. return table_proxy<basic_table_core, detail::proxy_key_t<T>>(std::move(*this), std::forward<T>(key));
  20603. }
  20604. template <typename Sig, typename Key, typename... Args>
  20605. basic_table_core& set_function(Key&& key, Args&&... args) {
  20606. set_fx(types<Sig>(), std::forward<Key>(key), std::forward<Args>(args)...);
  20607. return *this;
  20608. }
  20609. template <typename Key, typename... Args>
  20610. basic_table_core& set_function(Key&& key, Args&&... args) {
  20611. set_fx(types<>(), std::forward<Key>(key), std::forward<Args>(args)...);
  20612. return *this;
  20613. }
  20614. template <typename... Args>
  20615. basic_table_core& add(Args&&... args) {
  20616. auto pp = stack::push_pop(*this);
  20617. int table_index = pp.index_of(*this);
  20618. lua_State* L = base_t::lua_state();
  20619. (void)detail::swallow { 0, (stack::set_ref(L, std::forward<Args>(args), table_index), 0)... };
  20620. return *this;
  20621. }
  20622. private:
  20623. template <typename R, typename... Args, typename Fx, typename Key, typename = std::invoke_result_t<Fx, Args...>>
  20624. void set_fx(types<R(Args...)>, Key&& key, Fx&& fx) {
  20625. set_resolved_function<R(Args...)>(std::forward<Key>(key), std::forward<Fx>(fx));
  20626. }
  20627. template <typename Fx, typename Key, meta::enable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
  20628. void set_fx(types<>, Key&& key, Fx&& fx) {
  20629. set(std::forward<Key>(key), std::forward<Fx>(fx));
  20630. }
  20631. template <typename Fx, typename Key, typename... Args,
  20632. meta::disable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
  20633. void set_fx(types<>, Key&& key, Fx&& fx, Args&&... args) {
  20634. set(std::forward<Key>(key), as_function_reference(std::forward<Fx>(fx), std::forward<Args>(args)...));
  20635. }
  20636. template <typename... Sig, typename... Args, typename Key>
  20637. void set_resolved_function(Key&& key, Args&&... args) {
  20638. set(std::forward<Key>(key), as_function_reference<function_sig<Sig...>>(std::forward<Args>(args)...));
  20639. }
  20640. public:
  20641. static inline table create(lua_State* L, int narr = 0, int nrec = 0) {
  20642. lua_createtable(L, narr, nrec);
  20643. table result(L);
  20644. lua_pop(L, 1);
  20645. return result;
  20646. }
  20647. template <typename Key, typename Value, typename... Args>
  20648. static inline table create(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  20649. lua_createtable(L, narr, nrec);
  20650. table result(L);
  20651. result.set(std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  20652. lua_pop(L, 1);
  20653. return result;
  20654. }
  20655. template <typename... Args>
  20656. static inline table create_with(lua_State* L, Args&&... args) {
  20657. static_assert(sizeof...(Args) % 2 == 0, "You must have an even number of arguments for a key, value ... list.");
  20658. constexpr int narr = static_cast<int>(meta::count_odd_for_pack_v<std::is_integral, Args...>);
  20659. return create(L, narr, static_cast<int>((sizeof...(Args) / 2) - narr), std::forward<Args>(args)...);
  20660. }
  20661. table create(int narr = 0, int nrec = 0) {
  20662. return create(base_t::lua_state(), narr, nrec);
  20663. }
  20664. template <typename Key, typename Value, typename... Args>
  20665. table create(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  20666. return create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  20667. }
  20668. template <typename Name>
  20669. table create(Name&& name, int narr = 0, int nrec = 0) {
  20670. table x = create(base_t::lua_state(), narr, nrec);
  20671. this->set(std::forward<Name>(name), x);
  20672. return x;
  20673. }
  20674. template <typename Name, typename Key, typename Value, typename... Args>
  20675. table create(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  20676. table x = create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  20677. this->set(std::forward<Name>(name), x);
  20678. return x;
  20679. }
  20680. template <typename... Args>
  20681. table create_with(Args&&... args) {
  20682. return create_with(base_t::lua_state(), std::forward<Args>(args)...);
  20683. }
  20684. template <typename Name, typename... Args>
  20685. table create_named(Name&& name, Args&&... args) {
  20686. static const int narr = static_cast<int>(meta::count_even_for_pack_v<std::is_integral, Args...>);
  20687. return create(std::forward<Name>(name), narr, (sizeof...(Args) / 2) - narr, std::forward<Args>(args)...);
  20688. }
  20689. };
  20690. } // namespace sol
  20691. // end of sol/table_core.hpp
  20692. namespace sol {
  20693. template <typename base_type>
  20694. class basic_metatable : public basic_table<base_type> {
  20695. typedef basic_table<base_type> base_t;
  20696. friend class state;
  20697. friend class state_view;
  20698. protected:
  20699. basic_metatable(detail::no_safety_tag, lua_nil_t n) : base_t(n) {
  20700. }
  20701. basic_metatable(detail::no_safety_tag, lua_State* L, int index) : base_t(L, index) {
  20702. }
  20703. basic_metatable(detail::no_safety_tag, lua_State* L, ref_index index) : base_t(L, index) {
  20704. }
  20705. template <typename T,
  20706. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_metatable>>, meta::neg<std::is_same<base_type, stack_reference>>,
  20707. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20708. basic_metatable(detail::no_safety_tag, T&& r) noexcept : base_t(std::forward<T>(r)) {
  20709. }
  20710. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20711. basic_metatable(detail::no_safety_tag, lua_State* L, T&& r) noexcept : base_t(L, std::forward<T>(r)) {
  20712. }
  20713. public:
  20714. using base_t::lua_state;
  20715. basic_metatable() noexcept = default;
  20716. basic_metatable(const basic_metatable&) = default;
  20717. basic_metatable(basic_metatable&&) = default;
  20718. basic_metatable& operator=(const basic_metatable&) = default;
  20719. basic_metatable& operator=(basic_metatable&&) = default;
  20720. basic_metatable(const stack_reference& r) : basic_metatable(r.lua_state(), r.stack_index()) {
  20721. }
  20722. basic_metatable(stack_reference&& r) : basic_metatable(r.lua_state(), r.stack_index()) {
  20723. }
  20724. template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20725. basic_metatable(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  20726. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20727. auto pp = stack::push_pop(*this);
  20728. constructor_handler handler{};
  20729. stack::check<basic_metatable>(lua_state(), -1, handler);
  20730. #endif // Safety
  20731. }
  20732. basic_metatable(lua_State* L, int index = -1) : basic_metatable(detail::no_safety, L, index) {
  20733. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20734. constructor_handler handler{};
  20735. stack::check<basic_metatable>(L, index, handler);
  20736. #endif // Safety
  20737. }
  20738. basic_metatable(lua_State* L, ref_index index) : basic_metatable(detail::no_safety, L, index) {
  20739. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20740. auto pp = stack::push_pop(*this);
  20741. constructor_handler handler{};
  20742. stack::check<basic_metatable>(lua_state(), -1, handler);
  20743. #endif // Safety
  20744. }
  20745. template <typename T,
  20746. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_metatable>>, meta::neg<std::is_same<base_type, stack_reference>>,
  20747. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20748. basic_metatable(T&& r) noexcept : basic_metatable(detail::no_safety, std::forward<T>(r)) {
  20749. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20750. if (!is_table<meta::unqualified_t<T>>::value) {
  20751. auto pp = stack::push_pop(*this);
  20752. constructor_handler handler{};
  20753. stack::check<basic_metatable>(base_t::lua_state(), -1, handler);
  20754. }
  20755. #endif // Safety
  20756. }
  20757. basic_metatable(lua_nil_t r) noexcept : basic_metatable(detail::no_safety, r) {
  20758. }
  20759. template <typename Key, typename Value>
  20760. void set(Key&& key, Value&& value);
  20761. void unregister() {
  20762. using ustorage_base = u_detail::usertype_storage_base;
  20763. lua_State* L = this->lua_state();
  20764. auto pp = stack::push_pop(*this);
  20765. int top = lua_gettop(L);
  20766. stack_reference mt(L, -1);
  20767. stack::get_field(L, meta_function::gc_names, mt.stack_index());
  20768. if (type_of(L, -1) != type::table) {
  20769. return;
  20770. }
  20771. stack_reference gc_names_table(L, -1);
  20772. stack::get_field(L, meta_function::storage, mt.stack_index());
  20773. if (type_of(L, -1) != type::lightuserdata) {
  20774. return;
  20775. }
  20776. ustorage_base& base_storage = *static_cast<ustorage_base*>(stack::get<void*>(L, -1));
  20777. std::array<string_view, 6> registry_traits;
  20778. for (std::size_t i = 0; i < registry_traits.size(); ++i) {
  20779. u_detail::submetatable_type smt = static_cast<u_detail::submetatable_type>(i);
  20780. stack::get_field<false, true>(L, smt, gc_names_table.stack_index());
  20781. registry_traits[i] = stack::get<string_view>(L, -1);
  20782. }
  20783. // get the registry
  20784. stack_reference registry(L, raw_index(LUA_REGISTRYINDEX));
  20785. registry.push();
  20786. // eliminate all named entries for this usertype
  20787. // in the registry (luaL_newmetatable does
  20788. // [name] = new table
  20789. // in registry upon creation)
  20790. for (std::size_t i = 0; i < registry_traits.size(); ++i) {
  20791. u_detail::submetatable_type smt = static_cast<u_detail::submetatable_type>(i);
  20792. const string_view& gcmetakey = registry_traits[i];
  20793. if (smt == u_detail::submetatable_type::named) {
  20794. // use .data() to make it treat it like a c string,
  20795. // which it is...
  20796. stack::set_field<true>(L, gcmetakey.data(), lua_nil);
  20797. }
  20798. else {
  20799. // do not change the values in the registry: they need to be present
  20800. // no matter what, for safety's sake
  20801. //stack::set_field(L, gcmetakey, lua_nil, registry.stack_index());
  20802. }
  20803. }
  20804. // destroy all storage and tables
  20805. base_storage.clear();
  20806. // 6 strings from gc_names table,
  20807. // + 1 registry,
  20808. // + 1 gc_names table
  20809. // + 1 light userdata of storage
  20810. // + 1 registry
  20811. // 10 total, 4 left since popping off 6 gc_names tables
  20812. lua_settop(L, top);
  20813. }
  20814. };
  20815. } // namespace sol
  20816. // end of sol/metatable.hpp
  20817. namespace sol {
  20818. template <typename T, typename base_type>
  20819. class basic_usertype : private basic_metatable<base_type> {
  20820. private:
  20821. using base_t = basic_metatable<base_type>;
  20822. using table_base_t = basic_table<base_type>;
  20823. template <typename>
  20824. friend class basic_metatable;
  20825. template <bool, typename>
  20826. friend class basic_table_core;
  20827. template <std::size_t... I, typename... Args>
  20828. void tuple_set(std::index_sequence<I...>, std::tuple<Args...>&& args) {
  20829. (void)args;
  20830. (void)detail::swallow{ 0,
  20831. (this->set(std::get<I * 2>(std::move(args)), std::get<I * 2 + 1>(std::move(args))), 0)... };
  20832. }
  20833. public:
  20834. using base_t::base_t;
  20835. using base_t::pop;
  20836. using base_t::push;
  20837. using base_t::lua_state;
  20838. using base_t::get;
  20839. using base_t::set_function;
  20840. using base_t::traverse_set;
  20841. using base_t::traverse_get;
  20842. using base_t::unregister;
  20843. template <typename Key, typename Value>
  20844. void set(Key&& key, Value&& value) {
  20845. optional<u_detail::usertype_storage<T>&> maybe_uts = u_detail::maybe_get_usertype_storage<T>(this->lua_state());
  20846. if (maybe_uts) {
  20847. u_detail::usertype_storage<T>& uts = *maybe_uts;
  20848. uts.set(this->lua_state(), std::forward<Key>(key), std::forward<Value>(value));
  20849. }
  20850. else {
  20851. using ValueU = meta::unqualified_t<Value>;
  20852. // cannot get metatable: try regular table set?
  20853. if constexpr (detail::is_non_factory_constructor_v<ValueU> || detail::is_policy_v<ValueU>) {
  20854. // tag constructors so we don't get destroyed by lack of info
  20855. table_base_t::set(std::forward<Key>(key), detail::tagged<T, Value>(std::forward<Value>(value)));
  20856. }
  20857. else {
  20858. table_base_t::set(std::forward<Key>(key), std::forward<Value>(value));
  20859. }
  20860. }
  20861. }
  20862. template <typename Key>
  20863. usertype_proxy<basic_usertype&, std::decay_t<Key>> operator[](Key&& key) {
  20864. return usertype_proxy<basic_usertype&, std::decay_t<Key>>(*this, std::forward<Key>(key));
  20865. }
  20866. template <typename Key>
  20867. usertype_proxy<const basic_usertype&, std::decay_t<Key>> operator[](Key&& key) const {
  20868. return usertype_proxy<const basic_usertype&, std::decay_t<Key>>(*this, std::forward<Key>(key));
  20869. }
  20870. };
  20871. } // namespace sol
  20872. // end of sol/usertype.hpp
  20873. // beginning of sol/table.hpp
  20874. // beginning of sol/lua_table.hpp
  20875. namespace sol {
  20876. template <typename ref_t>
  20877. struct basic_lua_table : basic_table_core<false, ref_t> {
  20878. private:
  20879. using base_t = basic_table_core<false, ref_t>;
  20880. friend class state;
  20881. friend class state_view;
  20882. public:
  20883. using base_t::lua_state;
  20884. basic_lua_table() noexcept = default;
  20885. basic_lua_table(const basic_lua_table&) = default;
  20886. basic_lua_table(basic_lua_table&&) = default;
  20887. basic_lua_table& operator=(const basic_lua_table&) = default;
  20888. basic_lua_table& operator=(basic_lua_table&&) = default;
  20889. basic_lua_table(const stack_reference& r) : basic_lua_table(r.lua_state(), r.stack_index()) {
  20890. }
  20891. basic_lua_table(stack_reference&& r) : basic_lua_table(r.lua_state(), r.stack_index()) {
  20892. }
  20893. template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20894. basic_lua_table(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  20895. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20896. auto pp = stack::push_pop(*this);
  20897. constructor_handler handler{};
  20898. stack::check<basic_lua_table>(lua_state(), -1, handler);
  20899. #endif // Safety
  20900. }
  20901. basic_lua_table(lua_State* L, const new_table& nt) : base_t(L, nt) {
  20902. if (!is_stack_based<meta::unqualified_t<ref_t>>::value) {
  20903. lua_pop(L, 1);
  20904. }
  20905. }
  20906. basic_lua_table(lua_State* L, int index = -1) : base_t(detail::no_safety, L, index) {
  20907. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20908. constructor_handler handler{};
  20909. stack::check<basic_lua_table>(L, index, handler);
  20910. #endif // Safety
  20911. }
  20912. basic_lua_table(lua_State* L, ref_index index) : base_t(detail::no_safety, L, index) {
  20913. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20914. auto pp = stack::push_pop(*this);
  20915. constructor_handler handler{};
  20916. stack::check<basic_lua_table>(lua_state(), -1, handler);
  20917. #endif // Safety
  20918. }
  20919. template <typename T,
  20920. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_lua_table>>, meta::neg<std::is_same<ref_t, stack_reference>>,
  20921. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  20922. basic_lua_table(T&& r) noexcept : basic_lua_table(detail::no_safety, std::forward<T>(r)) {
  20923. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  20924. if (!is_table<meta::unqualified_t<T>>::value) {
  20925. auto pp = stack::push_pop(*this);
  20926. constructor_handler handler{};
  20927. stack::check<basic_lua_table>(lua_state(), -1, handler);
  20928. }
  20929. #endif // Safety
  20930. }
  20931. basic_lua_table(lua_nil_t r) noexcept : basic_lua_table(detail::no_safety, r) {
  20932. }
  20933. };
  20934. }
  20935. // end of sol/lua_table.hpp
  20936. namespace sol {
  20937. typedef table_core<false> table;
  20938. template <bool is_global, typename base_type>
  20939. template <typename Class, typename Key>
  20940. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key) {
  20941. automagic_enrollments enrollments;
  20942. return this->new_usertype<Class>(std::forward<Key>(key), std::move(enrollments));
  20943. }
  20944. template <bool is_global, typename base_type>
  20945. template <typename Class, typename Key>
  20946. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key, automagic_enrollments enrollments) {
  20947. int mt_index = u_detail::register_usertype<Class>(this->lua_state(), std::move(enrollments));
  20948. usertype<Class> mt(this->lua_state(), -mt_index);
  20949. lua_pop(this->lua_state(), 1);
  20950. set(std::forward<Key>(key), mt);
  20951. return mt;
  20952. }
  20953. template <bool is_global, typename base_type>
  20954. template <typename Class, typename Key, typename Arg, typename... Args, typename>
  20955. usertype<Class> basic_table_core<is_global, base_type>::new_usertype(Key&& key, Arg&& arg, Args&&... args) {
  20956. automagic_enrollments enrollments;
  20957. enrollments.default_constructor = !detail::any_is_constructor_v<Arg, Args...>;
  20958. enrollments.destructor = !detail::any_is_destructor_v<Arg, Args...>;
  20959. usertype<Class> ut = this->new_usertype<Class>(std::forward<Key>(key), std::move(enrollments));
  20960. static_assert(sizeof...(Args) % 2 == static_cast<std::size_t>(!detail::any_is_constructor_v<Arg>),
  20961. "you must pass an even number of arguments to new_usertype after first passing a constructor");
  20962. if constexpr (detail::any_is_constructor_v<Arg>) {
  20963. ut.set(meta_function::construct, std::forward<Arg>(arg));
  20964. ut.tuple_set(std::make_index_sequence<(sizeof...(Args)) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
  20965. }
  20966. else {
  20967. ut.tuple_set(std::make_index_sequence<(sizeof...(Args) + 1) / 2>(), std::forward_as_tuple(std::forward<Arg>(arg), std::forward<Args>(args)...));
  20968. }
  20969. return ut;
  20970. }
  20971. template <typename base_type>
  20972. template <typename Key, typename Value>
  20973. void basic_metatable<base_type>::set(Key&& key, Value&& value) {
  20974. this->push();
  20975. lua_State* L = this->lua_state();
  20976. int target = lua_gettop(L);
  20977. optional<u_detail::usertype_storage_base&> maybe_uts = u_detail::maybe_get_usertype_storage_base(L, target);
  20978. lua_pop(L, 1);
  20979. if (maybe_uts) {
  20980. u_detail::usertype_storage_base& uts = *maybe_uts;
  20981. uts.set(L, std::forward<Key>(key), std::forward<Value>(value));
  20982. }
  20983. else {
  20984. base_t::set(std::forward<Key>(key), std::forward<Value>(value));
  20985. }
  20986. }
  20987. namespace stack {
  20988. template <>
  20989. struct unqualified_getter<metatable_key_t> {
  20990. static table get(lua_State* L, int index = -1) {
  20991. if (lua_getmetatable(L, index) == 0) {
  20992. return table(L, ref_index(LUA_REFNIL));
  20993. }
  20994. return table(L, -1);
  20995. }
  20996. };
  20997. } // namespace stack
  20998. } // namespace sol
  20999. // end of sol/table.hpp
  21000. // beginning of sol/state.hpp
  21001. // beginning of sol/state_view.hpp
  21002. // beginning of sol/environment.hpp
  21003. namespace sol {
  21004. template <typename base_type>
  21005. struct basic_environment : basic_table<base_type> {
  21006. private:
  21007. typedef basic_table<base_type> base_t;
  21008. public:
  21009. using base_t::lua_state;
  21010. basic_environment() noexcept = default;
  21011. basic_environment(const basic_environment&) = default;
  21012. basic_environment(basic_environment&&) = default;
  21013. basic_environment& operator=(const basic_environment&) = default;
  21014. basic_environment& operator=(basic_environment&&) = default;
  21015. basic_environment(const stack_reference& r) : basic_environment(r.lua_state(), r.stack_index()) {
  21016. }
  21017. basic_environment(stack_reference&& r) : basic_environment(r.lua_state(), r.stack_index()) {
  21018. }
  21019. basic_environment(lua_State* L, new_table nt) : base_t(L, std::move(nt)) {
  21020. }
  21021. template <bool b>
  21022. basic_environment(lua_State* L, new_table t, const basic_reference<b>& fallback) : basic_environment(L, std::move(t)) {
  21023. stack_table mt(L, new_table(0, 1));
  21024. mt.set(meta_function::index, fallback);
  21025. this->set(metatable_key, mt);
  21026. mt.pop();
  21027. }
  21028. basic_environment(env_key_t, const stack_reference& extraction_target)
  21029. : base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) {
  21030. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  21031. constructor_handler handler {};
  21032. stack::check<env_key_t>(this->lua_state(), -1, handler);
  21033. #endif // Safety
  21034. lua_pop(this->lua_state(), 2);
  21035. }
  21036. template <bool b>
  21037. basic_environment(env_key_t, const basic_reference<b>& extraction_target)
  21038. : base_t(detail::no_safety, extraction_target.lua_state(), (stack::push_environment_of(extraction_target), -1)) {
  21039. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  21040. constructor_handler handler {};
  21041. stack::check<env_key_t>(this->lua_state(), -1, handler);
  21042. #endif // Safety
  21043. lua_pop(this->lua_state(), 2);
  21044. }
  21045. basic_environment(lua_State* L, int index = -1) : base_t(detail::no_safety, L, index) {
  21046. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  21047. constructor_handler handler {};
  21048. stack::check<basic_environment>(L, index, handler);
  21049. #endif // Safety
  21050. }
  21051. basic_environment(lua_State* L, ref_index index) : base_t(detail::no_safety, L, index) {
  21052. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  21053. auto pp = stack::push_pop(*this);
  21054. constructor_handler handler {};
  21055. stack::check<basic_environment>(L, -1, handler);
  21056. #endif // Safety
  21057. }
  21058. template <typename T,
  21059. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_environment>>, meta::neg<std::is_same<base_type, stack_reference>>,
  21060. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  21061. basic_environment(T&& r) noexcept : base_t(detail::no_safety, std::forward<T>(r)) {
  21062. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  21063. if (!is_environment<meta::unqualified_t<T>>::value) {
  21064. auto pp = stack::push_pop(*this);
  21065. constructor_handler handler {};
  21066. stack::check<basic_environment>(lua_state(), -1, handler);
  21067. }
  21068. #endif // Safety
  21069. }
  21070. basic_environment(lua_nil_t r) noexcept : base_t(detail::no_safety, r) {
  21071. }
  21072. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  21073. basic_environment(lua_State* L, T&& r) noexcept : base_t(detail::no_safety, L, std::forward<T>(r)) {
  21074. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  21075. if (!is_environment<meta::unqualified_t<T>>::value) {
  21076. auto pp = stack::push_pop(*this);
  21077. constructor_handler handler {};
  21078. stack::check<basic_environment>(lua_state(), -1, handler);
  21079. }
  21080. #endif // Safety
  21081. }
  21082. template <typename T>
  21083. void set_on(const T& target) const {
  21084. lua_State* L = target.lua_state();
  21085. auto pp = stack::push_pop(target);
  21086. #if SOL_LUA_VESION_I_ < 502
  21087. // Use lua_setfenv
  21088. this->push();
  21089. lua_setfenv(L, -2);
  21090. #else
  21091. // Use upvalues as explained in Lua 5.2 and beyond's manual
  21092. this->push();
  21093. const char* name = lua_setupvalue(L, -2, 1);
  21094. if (name == nullptr) {
  21095. this->pop();
  21096. }
  21097. #endif
  21098. }
  21099. };
  21100. template <typename T, typename E>
  21101. void set_environment(const basic_environment<E>& env, const T& target) {
  21102. env.set_on(target);
  21103. }
  21104. template <typename E = reference, typename T>
  21105. basic_environment<E> get_environment(const T& target) {
  21106. lua_State* L = target.lua_state();
  21107. auto pp = stack::pop_n(L, stack::push_environment_of(target));
  21108. return basic_environment<E>(L, -1);
  21109. }
  21110. struct this_environment {
  21111. optional<environment> env;
  21112. this_environment() : env(nullopt) {
  21113. }
  21114. this_environment(environment e) : env(std::move(e)) {
  21115. }
  21116. this_environment(const this_environment&) = default;
  21117. this_environment(this_environment&&) = default;
  21118. this_environment& operator=(const this_environment&) = default;
  21119. this_environment& operator=(this_environment&&) = default;
  21120. explicit operator bool() const {
  21121. return static_cast<bool>(env);
  21122. }
  21123. operator optional<environment> &() {
  21124. return env;
  21125. }
  21126. operator const optional<environment> &() const {
  21127. return env;
  21128. }
  21129. operator environment&() {
  21130. return env.value();
  21131. }
  21132. operator const environment&() const {
  21133. return env.value();
  21134. }
  21135. };
  21136. namespace stack {
  21137. template <>
  21138. struct unqualified_getter<env_key_t> {
  21139. static environment get(lua_State* L, int index, record& tracking) {
  21140. tracking.use(1);
  21141. return get_environment(stack_reference(L, raw_index(index)));
  21142. }
  21143. };
  21144. template <>
  21145. struct unqualified_getter<this_environment> {
  21146. static this_environment get(lua_State* L, int, record& tracking) {
  21147. tracking.use(0);
  21148. lua_Debug info;
  21149. // Level 0 means current function (this C function, which may or may not be useful for us?)
  21150. // Level 1 means next call frame up the stack. (Can be nothing if function called directly from C++ with lua_p/call)
  21151. int pre_stack_size = lua_gettop(L);
  21152. if (lua_getstack(L, 1, &info) != 1) {
  21153. if (lua_getstack(L, 0, &info) != 1) {
  21154. lua_settop(L, pre_stack_size);
  21155. return this_environment();
  21156. }
  21157. }
  21158. if (lua_getinfo(L, "f", &info) == 0) {
  21159. lua_settop(L, pre_stack_size);
  21160. return this_environment();
  21161. }
  21162. stack_reference f(L, -1);
  21163. environment env(env_key, f);
  21164. if (!env.valid()) {
  21165. lua_settop(L, pre_stack_size);
  21166. return this_environment();
  21167. }
  21168. return this_environment(std::move(env));
  21169. }
  21170. };
  21171. } // namespace stack
  21172. } // namespace sol
  21173. // end of sol/environment.hpp
  21174. // beginning of sol/load_result.hpp
  21175. #include <cstdint>
  21176. namespace sol {
  21177. struct load_result : public proxy_base<load_result> {
  21178. private:
  21179. lua_State* L;
  21180. int index;
  21181. int returncount;
  21182. int popcount;
  21183. load_status err;
  21184. public:
  21185. load_result() noexcept = default;
  21186. load_result(lua_State* Ls, int stackindex = -1, int retnum = 0, int popnum = 0, load_status lerr = load_status::ok) noexcept
  21187. : L(Ls), index(stackindex), returncount(retnum), popcount(popnum), err(lerr) {
  21188. }
  21189. // We do not want anyone to copy these around willy-nilly
  21190. // Will likely break people, but also will probably get rid of quiet bugs that have
  21191. // been lurking. (E.g., Vanilla Lua will just quietly discard over-pops and under-pops:
  21192. // LuaJIT and other Lua engines will implode and segfault at random later times.)
  21193. load_result(const load_result&) = delete;
  21194. load_result& operator=(const load_result&) = delete;
  21195. load_result(load_result&& o) noexcept : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) {
  21196. // Must be manual, otherwise destructor will screw us
  21197. // return count being 0 is enough to keep things clean
  21198. // but we will be thorough
  21199. o.L = nullptr;
  21200. o.index = 0;
  21201. o.returncount = 0;
  21202. o.popcount = 0;
  21203. o.err = load_status::syntax;
  21204. }
  21205. load_result& operator=(load_result&& o) noexcept {
  21206. L = o.L;
  21207. index = o.index;
  21208. returncount = o.returncount;
  21209. popcount = o.popcount;
  21210. err = o.err;
  21211. // Must be manual, otherwise destructor will screw us
  21212. // return count being 0 is enough to keep things clean
  21213. // but we will be thorough
  21214. o.L = nullptr;
  21215. o.index = 0;
  21216. o.returncount = 0;
  21217. o.popcount = 0;
  21218. o.err = load_status::syntax;
  21219. return *this;
  21220. }
  21221. load_status status() const noexcept {
  21222. return err;
  21223. }
  21224. bool valid() const noexcept {
  21225. return status() == load_status::ok;
  21226. }
  21227. template <typename T>
  21228. T get() const {
  21229. using UT = meta::unqualified_t<T>;
  21230. if constexpr (meta::is_optional_v<UT>) {
  21231. using ValueType = typename UT::value_type;
  21232. if constexpr (std::is_same_v<ValueType, error>) {
  21233. if (valid()) {
  21234. return UT(nullopt);
  21235. }
  21236. return error(detail::direct_error, stack::get<std::string>(L, index));
  21237. }
  21238. else {
  21239. if (!valid()) {
  21240. return UT(nullopt);
  21241. }
  21242. return stack::get<UT>(L, index);
  21243. }
  21244. }
  21245. else {
  21246. if constexpr (std::is_same_v<T, error>) {
  21247. #if SOL_IS_ON(SOL_SAFE_PROXIES_I_)
  21248. if (valid()) {
  21249. type_panic_c_str(L, index, type_of(L, index), type::none, "expecting an error type (a string, from Lua)");
  21250. }
  21251. #endif // Check proxy type's safety
  21252. return error(detail::direct_error, stack::get<std::string>(L, index));
  21253. }
  21254. else {
  21255. #if SOL_IS_ON(SOL_SAFE_PROXIES_I_)
  21256. if (!valid()) {
  21257. type_panic_c_str(L, index, type_of(L, index), type::none);
  21258. }
  21259. #endif // Check proxy type's safety
  21260. return stack::get<T>(L, index);
  21261. }
  21262. }
  21263. }
  21264. template <typename... Ret, typename... Args>
  21265. decltype(auto) call(Args&&... args) {
  21266. #if !defined(__clang__) && defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 191200000
  21267. // MSVC is ass sometimes
  21268. return get<protected_function>().call<Ret...>(std::forward<Args>(args)...);
  21269. #else
  21270. return get<protected_function>().template call<Ret...>(std::forward<Args>(args)...);
  21271. #endif
  21272. }
  21273. template <typename... Args>
  21274. decltype(auto) operator()(Args&&... args) {
  21275. return call<>(std::forward<Args>(args)...);
  21276. }
  21277. lua_State* lua_state() const noexcept {
  21278. return L;
  21279. };
  21280. int stack_index() const noexcept {
  21281. return index;
  21282. };
  21283. ~load_result() {
  21284. stack::remove(L, index, popcount);
  21285. }
  21286. };
  21287. } // namespace sol
  21288. // end of sol/load_result.hpp
  21289. // beginning of sol/state_handling.hpp
  21290. // beginning of sol/lua_value.hpp
  21291. namespace sol {
  21292. struct lua_value {
  21293. public:
  21294. struct arr : detail::ebco<std::initializer_list<lua_value>> {
  21295. private:
  21296. using base_t = detail::ebco<std::initializer_list<lua_value>>;
  21297. public:
  21298. using base_t::base_t;
  21299. };
  21300. private:
  21301. template <typename T>
  21302. using is_reference_or_lua_value_init_list
  21303. = meta::any<meta::is_specialization_of<T, std::initializer_list>, std::is_same<T, reference>, std::is_same<T, arr>>;
  21304. template <typename T>
  21305. using is_lua_value_single_constructible = meta::any<std::is_same<T, lua_value>, is_reference_or_lua_value_init_list<T>>;
  21306. static lua_State*& thread_local_lua_state() {
  21307. #if SOL_IS_ON(SOL_USE_THREAD_LOCAL_I_)
  21308. static thread_local lua_State* L = nullptr;
  21309. #else
  21310. static lua_State* L = nullptr;
  21311. #endif
  21312. return L;
  21313. }
  21314. reference ref_value;
  21315. public:
  21316. static void set_lua_state(lua_State* L) {
  21317. thread_local_lua_state() = L;
  21318. }
  21319. template <typename T, meta::disable<is_reference_or_lua_value_init_list<meta::unqualified_t<T>>> = meta::enabler>
  21320. lua_value(lua_State* L_, T&& value) : lua_value(((set_lua_state(L_)), std::forward<T>(value))) {
  21321. }
  21322. template <typename T, meta::disable<is_lua_value_single_constructible<meta::unqualified_t<T>>> = meta::enabler>
  21323. lua_value(T&& value) : ref_value(make_reference(thread_local_lua_state(), std::forward<T>(value))) {
  21324. }
  21325. lua_value(lua_State* L_, std::initializer_list<std::pair<lua_value, lua_value>> il)
  21326. : lua_value([&L_, &il]() {
  21327. set_lua_state(L_);
  21328. return std::move(il);
  21329. }()) {
  21330. }
  21331. lua_value(std::initializer_list<std::pair<lua_value, lua_value>> il) : ref_value(make_reference(thread_local_lua_state(), std::move(il))) {
  21332. }
  21333. lua_value(lua_State* L_, arr il)
  21334. : lua_value([&L_, &il]() {
  21335. set_lua_state(L_);
  21336. return std::move(il);
  21337. }()) {
  21338. }
  21339. lua_value(arr il) : ref_value(make_reference(thread_local_lua_state(), std::move(il.value()))) {
  21340. }
  21341. lua_value(lua_State* L_, reference r)
  21342. : lua_value([&L_, &r]() {
  21343. set_lua_state(L_);
  21344. return std::move(r);
  21345. }()) {
  21346. }
  21347. lua_value(reference r) : ref_value(std::move(r)) {
  21348. }
  21349. lua_value(const lua_value&) noexcept = default;
  21350. lua_value(lua_value&&) = default;
  21351. lua_value& operator=(const lua_value&) = default;
  21352. lua_value& operator=(lua_value&&) = default;
  21353. const reference& value() const& {
  21354. return ref_value;
  21355. }
  21356. reference& value() & {
  21357. return ref_value;
  21358. }
  21359. reference&& value() && {
  21360. return std::move(ref_value);
  21361. }
  21362. template <typename T>
  21363. decltype(auto) as() const {
  21364. ref_value.push();
  21365. return stack::pop<T>(ref_value.lua_state());
  21366. }
  21367. template <typename T>
  21368. bool is() const {
  21369. int r = ref_value.registry_index();
  21370. if (r == LUA_REFNIL)
  21371. return meta::any_same<meta::unqualified_t<T>, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false;
  21372. if (r == LUA_NOREF)
  21373. return false;
  21374. auto pp = stack::push_pop(ref_value);
  21375. return stack::check<T>(ref_value.lua_state(), -1, no_panic);
  21376. }
  21377. };
  21378. using array_value = typename lua_value::arr;
  21379. namespace stack {
  21380. template <>
  21381. struct unqualified_pusher<lua_value> {
  21382. static int push(lua_State* L, const lua_value& lv) {
  21383. return stack::push(L, lv.value());
  21384. }
  21385. static int push(lua_State* L, lua_value&& lv) {
  21386. return stack::push(L, std::move(lv).value());
  21387. }
  21388. };
  21389. template <>
  21390. struct unqualified_getter<lua_value> {
  21391. static lua_value get(lua_State* L, int index, record& tracking) {
  21392. return lua_value(L, stack::get<reference>(L, index, tracking));
  21393. }
  21394. };
  21395. } // namespace stack
  21396. } // namespace sol
  21397. // end of sol/lua_value.hpp
  21398. #if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
  21399. #include <iostream>
  21400. #endif
  21401. namespace sol {
  21402. inline void register_main_thread(lua_State* L) {
  21403. #if SOL_LUA_VESION_I_ < 502
  21404. if (L == nullptr) {
  21405. lua_pushnil(L);
  21406. lua_setglobal(L, detail::default_main_thread_name());
  21407. return;
  21408. }
  21409. lua_pushthread(L);
  21410. lua_setglobal(L, detail::default_main_thread_name());
  21411. #else
  21412. (void)L;
  21413. #endif
  21414. }
  21415. inline int default_at_panic(lua_State* L) {
  21416. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  21417. (void)L;
  21418. return -1;
  21419. #else
  21420. size_t messagesize;
  21421. const char* message = lua_tolstring(L, -1, &messagesize);
  21422. if (message) {
  21423. std::string err(message, messagesize);
  21424. lua_settop(L, 0);
  21425. #if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
  21426. std::cerr << "[sol3] An error occurred and panic has been invoked: ";
  21427. std::cerr << err;
  21428. std::cerr << std::endl;
  21429. #endif
  21430. throw error(err);
  21431. }
  21432. lua_settop(L, 0);
  21433. throw error(std::string("An unexpected error occurred and panic has been invoked"));
  21434. #endif // Printing Errors
  21435. }
  21436. inline int default_traceback_error_handler(lua_State* L) {
  21437. std::string msg = "An unknown error has triggered the default error handler";
  21438. optional<string_view> maybetopmsg = stack::unqualified_check_get<string_view>(L, 1, no_panic);
  21439. if (maybetopmsg) {
  21440. const string_view& topmsg = maybetopmsg.value();
  21441. msg.assign(topmsg.data(), topmsg.size());
  21442. }
  21443. luaL_traceback(L, L, msg.c_str(), 1);
  21444. optional<string_view> maybetraceback = stack::unqualified_check_get<string_view>(L, -1, no_panic);
  21445. if (maybetraceback) {
  21446. const string_view& traceback = maybetraceback.value();
  21447. msg.assign(traceback.data(), traceback.size());
  21448. }
  21449. #if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
  21450. // std::cerr << "[sol3] An error occurred and was caught in traceback: ";
  21451. // std::cerr << msg;
  21452. // std::cerr << std::endl;
  21453. #endif // Printing
  21454. return stack::push(L, msg);
  21455. }
  21456. inline void set_default_state(lua_State* L, lua_CFunction panic_function = &default_at_panic,
  21457. lua_CFunction traceback_function = c_call<decltype(&default_traceback_error_handler), &default_traceback_error_handler>,
  21458. exception_handler_function exf = detail::default_exception_handler) {
  21459. lua_atpanic(L, panic_function);
  21460. protected_function::set_default_handler(object(L, in_place, traceback_function));
  21461. set_default_exception_handler(L, exf);
  21462. register_main_thread(L);
  21463. stack::luajit_exception_handler(L);
  21464. lua_value::set_lua_state(L);
  21465. }
  21466. inline std::size_t total_memory_used(lua_State* L) {
  21467. std::size_t kb = lua_gc(L, LUA_GCCOUNT, 0);
  21468. kb *= 1024;
  21469. kb += lua_gc(L, LUA_GCCOUNTB, 0);
  21470. return kb;
  21471. }
  21472. inline protected_function_result script_pass_on_error(lua_State*, protected_function_result result) {
  21473. return result;
  21474. }
  21475. inline protected_function_result script_throw_on_error(lua_State* L, protected_function_result result) {
  21476. type t = type_of(L, result.stack_index());
  21477. std::string err = "sol: ";
  21478. err += to_string(result.status());
  21479. err += " error";
  21480. #if SOL_IS_ON(SOL_EXCEPTIONS_I_)
  21481. std::exception_ptr eptr = std::current_exception();
  21482. if (eptr) {
  21483. err += " with a ";
  21484. try {
  21485. std::rethrow_exception(eptr);
  21486. }
  21487. catch (const std::exception& ex) {
  21488. err += "std::exception -- ";
  21489. err.append(ex.what());
  21490. }
  21491. catch (const std::string& message) {
  21492. err += "thrown message -- ";
  21493. err.append(message);
  21494. }
  21495. catch (const char* message) {
  21496. err += "thrown message -- ";
  21497. err.append(message);
  21498. }
  21499. catch (...) {
  21500. err.append("thrown but unknown type, cannot serialize into error message");
  21501. }
  21502. }
  21503. #endif // serialize exception information if possible
  21504. if (t == type::string) {
  21505. err += ": ";
  21506. string_view serr = stack::unqualified_get<string_view>(L, result.stack_index());
  21507. err.append(serr.data(), serr.size());
  21508. }
  21509. #if SOL_IS_ON(SOL_PRINT_ERRORS_I_)
  21510. std::cerr << "[sol3] An error occurred and has been passed to an error handler: ";
  21511. std::cerr << err;
  21512. std::cerr << std::endl;
  21513. #endif
  21514. // replacing information of stack error into pfr
  21515. int target = result.stack_index();
  21516. if (result.pop_count() > 0) {
  21517. stack::remove(L, target, result.pop_count());
  21518. }
  21519. stack::push(L, err);
  21520. int top = lua_gettop(L);
  21521. int towards = top - target;
  21522. if (towards != 0) {
  21523. lua_rotate(L, top, towards);
  21524. }
  21525. #if SOL_IS_OFF(SOL_EXCEPTIONS_I_)
  21526. return result;
  21527. #else
  21528. // just throw our error
  21529. throw error(detail::direct_error, err);
  21530. #endif // If exceptions are allowed
  21531. }
  21532. inline protected_function_result script_default_on_error(lua_State* L, protected_function_result pfr) {
  21533. #if SOL_IS_ON(SOL_DEFAULT_PASS_ON_ERROR_I_)
  21534. return script_pass_on_error(L, std::move(pfr));
  21535. #else
  21536. return script_throw_on_error(L, std::move(pfr));
  21537. #endif
  21538. }
  21539. namespace stack {
  21540. inline error get_traceback_or_errors(lua_State* L) {
  21541. int p = default_traceback_error_handler(L);
  21542. sol::error err = stack::get<sol::error>(L, -p);
  21543. lua_pop(L, p);
  21544. return err;
  21545. }
  21546. } // namespace stack
  21547. } // namespace sol
  21548. // end of sol/state_handling.hpp
  21549. #include <memory>
  21550. #include <cstddef>
  21551. namespace sol {
  21552. class state_view {
  21553. private:
  21554. lua_State* L;
  21555. table reg;
  21556. global_table global;
  21557. optional<object> is_loaded_package(const std::string& key) {
  21558. auto loaded = reg.traverse_get<optional<object>>("_LOADED", key);
  21559. bool is53mod = loaded && !(loaded->is<bool>() && !loaded->as<bool>());
  21560. if (is53mod)
  21561. return loaded;
  21562. #if SOL_LUA_VESION_I_ <= 501
  21563. auto loaded51 = global.traverse_get<optional<object>>("package", "loaded", key);
  21564. bool is51mod = loaded51 && !(loaded51->is<bool>() && !loaded51->as<bool>());
  21565. if (is51mod)
  21566. return loaded51;
  21567. #endif
  21568. return nullopt;
  21569. }
  21570. template <typename T>
  21571. void ensure_package(const std::string& key, T&& sr) {
  21572. #if SOL_LUA_VESION_I_ <= 501
  21573. auto pkg = global["package"];
  21574. if (!pkg.valid()) {
  21575. pkg = create_table_with("loaded", create_table_with(key, sr));
  21576. }
  21577. else {
  21578. auto ld = pkg["loaded"];
  21579. if (!ld.valid()) {
  21580. ld = create_table_with(key, sr);
  21581. }
  21582. else {
  21583. ld[key] = sr;
  21584. }
  21585. }
  21586. #endif
  21587. auto loaded = reg["_LOADED"];
  21588. if (!loaded.valid()) {
  21589. loaded = create_table_with(key, sr);
  21590. }
  21591. else {
  21592. loaded[key] = sr;
  21593. }
  21594. }
  21595. template <typename Fx>
  21596. object require_core(const std::string& key, Fx&& action, bool create_global = true) {
  21597. optional<object> loaded = is_loaded_package(key);
  21598. if (loaded && loaded->valid())
  21599. return std::move(*loaded);
  21600. action();
  21601. stack_reference sr(L, -1);
  21602. if (create_global)
  21603. set(key, sr);
  21604. ensure_package(key, sr);
  21605. return stack::pop<object>(L);
  21606. }
  21607. public:
  21608. using iterator = typename global_table::iterator;
  21609. using const_iterator = typename global_table::const_iterator;
  21610. state_view(lua_State* Ls) : L(Ls), reg(Ls, LUA_REGISTRYINDEX), global(Ls, detail::global_) {
  21611. }
  21612. state_view(this_state Ls) : state_view(Ls.L) {
  21613. }
  21614. lua_State* lua_state() const {
  21615. return L;
  21616. }
  21617. template <typename... Args>
  21618. void open_libraries(Args&&... args) {
  21619. static_assert(meta::all_same<lib, meta::unqualified_t<Args>...>::value, "all types must be libraries");
  21620. if constexpr (sizeof...(args) == 0) {
  21621. luaL_openlibs(L);
  21622. return;
  21623. }
  21624. else {
  21625. lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
  21626. for (auto&& library : libraries) {
  21627. switch (library) {
  21628. #if SOL_LUA_VESION_I_ <= 501 && defined(SOL_LUAJIT)
  21629. case lib::coroutine:
  21630. #endif // luajit opens coroutine base stuff
  21631. case lib::base:
  21632. luaL_requiref(L, "base", luaopen_base, 1);
  21633. lua_pop(L, 1);
  21634. break;
  21635. case lib::package:
  21636. luaL_requiref(L, "package", luaopen_package, 1);
  21637. lua_pop(L, 1);
  21638. break;
  21639. #if !defined(SOL_LUAJIT)
  21640. case lib::coroutine:
  21641. #if SOL_LUA_VESION_I_ > 501
  21642. luaL_requiref(L, "coroutine", luaopen_coroutine, 1);
  21643. lua_pop(L, 1);
  21644. #endif // Lua 5.2+ only
  21645. break;
  21646. #endif // Not LuaJIT - comes builtin
  21647. case lib::string:
  21648. luaL_requiref(L, "string", luaopen_string, 1);
  21649. lua_pop(L, 1);
  21650. break;
  21651. case lib::table:
  21652. luaL_requiref(L, "table", luaopen_table, 1);
  21653. lua_pop(L, 1);
  21654. break;
  21655. case lib::math:
  21656. luaL_requiref(L, "math", luaopen_math, 1);
  21657. lua_pop(L, 1);
  21658. break;
  21659. case lib::bit32:
  21660. #ifdef SOL_LUAJIT
  21661. luaL_requiref(L, "bit32", luaopen_bit, 1);
  21662. lua_pop(L, 1);
  21663. #elif (SOL_LUA_VESION_I_ == 502) || defined(LUA_COMPAT_BITLIB) || defined(LUA_COMPAT_5_2)
  21664. luaL_requiref(L, "bit32", luaopen_bit32, 1);
  21665. lua_pop(L, 1);
  21666. #else
  21667. #endif // Lua 5.2 only (deprecated in 5.3 (503)) (Can be turned on with Compat flags)
  21668. break;
  21669. case lib::io:
  21670. luaL_requiref(L, "io", luaopen_io, 1);
  21671. lua_pop(L, 1);
  21672. break;
  21673. case lib::os:
  21674. luaL_requiref(L, "os", luaopen_os, 1);
  21675. lua_pop(L, 1);
  21676. break;
  21677. case lib::debug:
  21678. luaL_requiref(L, "debug", luaopen_debug, 1);
  21679. lua_pop(L, 1);
  21680. break;
  21681. case lib::utf8:
  21682. #if SOL_LUA_VESION_I_ > 502 && !defined(SOL_LUAJIT)
  21683. luaL_requiref(L, "utf8", luaopen_utf8, 1);
  21684. lua_pop(L, 1);
  21685. #endif // Lua 5.3+ only
  21686. break;
  21687. case lib::ffi:
  21688. #ifdef SOL_LUAJIT
  21689. luaL_requiref(L, "ffi", luaopen_ffi, 1);
  21690. lua_pop(L, 1);
  21691. #endif // LuaJIT only
  21692. break;
  21693. case lib::jit:
  21694. #ifdef SOL_LUAJIT
  21695. luaL_requiref(L, "jit", luaopen_jit, 0);
  21696. lua_pop(L, 1);
  21697. #endif // LuaJIT Only
  21698. break;
  21699. case lib::count:
  21700. default:
  21701. break;
  21702. }
  21703. }
  21704. }
  21705. }
  21706. object require(const std::string& key, lua_CFunction open_function, bool create_global = true) {
  21707. luaL_requiref(L, key.c_str(), open_function, create_global ? 1 : 0);
  21708. return stack::pop<object>(L);
  21709. }
  21710. object require_script(const std::string& key, const string_view& code, bool create_global = true,
  21711. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21712. auto action = [this, &code, &chunkname, &mode]() { stack::script(L, code, chunkname, mode); };
  21713. return require_core(key, action, create_global);
  21714. }
  21715. object require_file(const std::string& key, const std::string& filename, bool create_global = true, load_mode mode = load_mode::any) {
  21716. auto action = [this, &filename, &mode]() { stack::script_file(L, filename, mode); };
  21717. return require_core(key, action, create_global);
  21718. }
  21719. void clear_package_loaders() {
  21720. optional<table> maybe_package = this->global["package"];
  21721. if (!maybe_package) {
  21722. // package lib wasn't opened
  21723. // open package lib
  21724. return;
  21725. }
  21726. table& package = *maybe_package;
  21727. // yay for version differences...
  21728. // one day Lua 5.1 will die a peaceful death
  21729. // and its old bones will find blissful rest
  21730. auto loaders_proxy = package
  21731. #if SOL_LUA_VESION_I_ < 502
  21732. ["loaders"]
  21733. #else
  21734. ["searchers"]
  21735. #endif
  21736. ;
  21737. if (!loaders_proxy.valid()) {
  21738. // nothing to clear
  21739. return;
  21740. }
  21741. // we need to create the table for loaders
  21742. // table does not exist, so create and move forward
  21743. loaders_proxy = new_table(1, 0);
  21744. }
  21745. template <typename Fx>
  21746. void add_package_loader(Fx&& fx, bool clear_all_package_loaders = false) {
  21747. optional<table> maybe_package = this->global["package"];
  21748. if (!maybe_package) {
  21749. // package lib wasn't opened
  21750. // open package lib
  21751. return;
  21752. }
  21753. table& package = *maybe_package;
  21754. // yay for version differences...
  21755. // one day Lua 5.1 will die a peaceful death
  21756. // and its old bones will find blissful rest
  21757. auto loaders_proxy = package
  21758. #if SOL_LUA_VESION_I_ < 502
  21759. ["loaders"]
  21760. #else
  21761. ["searchers"]
  21762. #endif
  21763. ;
  21764. bool make_new_table = clear_all_package_loaders || !loaders_proxy.valid();
  21765. if (make_new_table) {
  21766. // we need to create the table for loaders
  21767. // table does not exist, so create and move forward
  21768. loaders_proxy = new_table(1, 0);
  21769. }
  21770. optional<table> maybe_loaders = loaders_proxy;
  21771. if (!maybe_loaders) {
  21772. // loaders/searches
  21773. // thing exists in package, but it
  21774. // ain't a table or a table-alike...!
  21775. return;
  21776. }
  21777. table loaders = loaders_proxy;
  21778. loaders.add(std::forward<Fx>(fx));
  21779. }
  21780. template <typename E>
  21781. protected_function_result do_reader(lua_Reader reader, void* data, const basic_environment<E>& env,
  21782. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21783. detail::typical_chunk_name_t basechunkname = {};
  21784. const char* chunknametarget = detail::make_chunk_name("lua_Reader", chunkname, basechunkname);
  21785. load_status x = static_cast<load_status>(lua_load(L, reader, data, chunknametarget, to_string(mode).c_str()));
  21786. if (x != load_status::ok) {
  21787. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  21788. }
  21789. stack_aligned_protected_function pf(L, -1);
  21790. set_environment(env, pf);
  21791. return pf();
  21792. }
  21793. protected_function_result do_reader(
  21794. lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21795. detail::typical_chunk_name_t basechunkname = {};
  21796. const char* chunknametarget = detail::make_chunk_name("lua_Reader", chunkname, basechunkname);
  21797. load_status x = static_cast<load_status>(lua_load(L, reader, data, chunknametarget, to_string(mode).c_str()));
  21798. if (x != load_status::ok) {
  21799. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  21800. }
  21801. stack_aligned_protected_function pf(L, -1);
  21802. return pf();
  21803. }
  21804. template <typename E>
  21805. protected_function_result do_string(const string_view& code, const basic_environment<E>& env,
  21806. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21807. detail::typical_chunk_name_t basechunkname = {};
  21808. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  21809. load_status x = static_cast<load_status>(luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()));
  21810. if (x != load_status::ok) {
  21811. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  21812. }
  21813. stack_aligned_protected_function pf(L, -1);
  21814. set_environment(env, pf);
  21815. return pf();
  21816. }
  21817. protected_function_result do_string(
  21818. const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21819. detail::typical_chunk_name_t basechunkname = {};
  21820. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  21821. load_status x = static_cast<load_status>(luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()));
  21822. if (x != load_status::ok) {
  21823. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  21824. }
  21825. stack_aligned_protected_function pf(L, -1);
  21826. return pf();
  21827. }
  21828. template <typename E>
  21829. protected_function_result do_file(const std::string& filename, const basic_environment<E>& env, load_mode mode = load_mode::any) {
  21830. load_status x = static_cast<load_status>(luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()));
  21831. if (x != load_status::ok) {
  21832. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  21833. }
  21834. stack_aligned_protected_function pf(L, -1);
  21835. set_environment(env, pf);
  21836. return pf();
  21837. }
  21838. protected_function_result do_file(const std::string& filename, load_mode mode = load_mode::any) {
  21839. load_status x = static_cast<load_status>(luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()));
  21840. if (x != load_status::ok) {
  21841. return protected_function_result(L, absolute_index(L, -1), 0, 1, static_cast<call_status>(x));
  21842. }
  21843. stack_aligned_protected_function pf(L, -1);
  21844. return pf();
  21845. }
  21846. template <typename Fx,
  21847. meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>,
  21848. meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  21849. protected_function_result safe_script(
  21850. lua_Reader reader, void* data, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21851. protected_function_result pfr = do_reader(reader, data, chunkname, mode);
  21852. if (!pfr.valid()) {
  21853. return on_error(L, std::move(pfr));
  21854. }
  21855. return pfr;
  21856. }
  21857. protected_function_result safe_script(
  21858. lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21859. return safe_script(reader, data, script_default_on_error, chunkname, mode);
  21860. }
  21861. template <typename Fx,
  21862. meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>,
  21863. meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  21864. protected_function_result safe_script(
  21865. const string_view& code, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21866. protected_function_result pfr = do_string(code, chunkname, mode);
  21867. if (!pfr.valid()) {
  21868. return on_error(L, std::move(pfr));
  21869. }
  21870. return pfr;
  21871. }
  21872. template <typename Fx, typename E>
  21873. protected_function_result safe_script(const string_view& code, const basic_environment<E>& env, Fx&& on_error,
  21874. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21875. protected_function_result pfr = do_string(code, env, chunkname, mode);
  21876. if (!pfr.valid()) {
  21877. return on_error(L, std::move(pfr));
  21878. }
  21879. return pfr;
  21880. }
  21881. template <typename E>
  21882. protected_function_result safe_script(const string_view& code, const basic_environment<E>& env,
  21883. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21884. return safe_script(code, env, script_default_on_error, chunkname, mode);
  21885. }
  21886. protected_function_result safe_script(
  21887. const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21888. return safe_script(code, script_default_on_error, chunkname, mode);
  21889. }
  21890. template <typename Fx,
  21891. meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>,
  21892. meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  21893. protected_function_result safe_script_file(const std::string& filename, Fx&& on_error, load_mode mode = load_mode::any) {
  21894. protected_function_result pfr = do_file(filename, mode);
  21895. if (!pfr.valid()) {
  21896. return on_error(L, std::move(pfr));
  21897. }
  21898. return pfr;
  21899. }
  21900. template <typename Fx, typename E>
  21901. protected_function_result safe_script_file(
  21902. const std::string& filename, const basic_environment<E>& env, Fx&& on_error, load_mode mode = load_mode::any) {
  21903. protected_function_result pfr = do_file(filename, env, mode);
  21904. if (!pfr.valid()) {
  21905. return on_error(L, std::move(pfr));
  21906. }
  21907. return pfr;
  21908. }
  21909. template <typename E>
  21910. protected_function_result safe_script_file(const std::string& filename, const basic_environment<E>& env, load_mode mode = load_mode::any) {
  21911. return safe_script_file(filename, env, script_default_on_error, mode);
  21912. }
  21913. protected_function_result safe_script_file(const std::string& filename, load_mode mode = load_mode::any) {
  21914. return safe_script_file(filename, script_default_on_error, mode);
  21915. }
  21916. template <typename E>
  21917. unsafe_function_result unsafe_script(lua_Reader reader, void* data, const basic_environment<E>& env,
  21918. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21919. detail::typical_chunk_name_t basechunkname = {};
  21920. const char* chunknametarget = detail::make_chunk_name("lua_Reader", chunkname, basechunkname);
  21921. int index = lua_gettop(L);
  21922. if (lua_load(L, reader, data, chunknametarget, to_string(mode).c_str())) {
  21923. lua_error(L);
  21924. }
  21925. set_environment(env, stack_reference(L, raw_index(index + 1)));
  21926. if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
  21927. lua_error(L);
  21928. }
  21929. int postindex = lua_gettop(L);
  21930. int returns = postindex - index;
  21931. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  21932. }
  21933. unsafe_function_result unsafe_script(
  21934. lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21935. int index = lua_gettop(L);
  21936. stack::script(L, reader, data, chunkname, mode);
  21937. int postindex = lua_gettop(L);
  21938. int returns = postindex - index;
  21939. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  21940. }
  21941. template <typename E>
  21942. unsafe_function_result unsafe_script(const string_view& code, const basic_environment<E>& env,
  21943. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21944. detail::typical_chunk_name_t basechunkname = {};
  21945. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  21946. int index = lua_gettop(L);
  21947. if (luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str())) {
  21948. lua_error(L);
  21949. }
  21950. set_environment(env, stack_reference(L, raw_index(index + 1)));
  21951. if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
  21952. lua_error(L);
  21953. }
  21954. int postindex = lua_gettop(L);
  21955. int returns = postindex - index;
  21956. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  21957. }
  21958. unsafe_function_result unsafe_script(
  21959. const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21960. int index = lua_gettop(L);
  21961. stack::script(L, code, chunkname, mode);
  21962. int postindex = lua_gettop(L);
  21963. int returns = postindex - index;
  21964. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  21965. }
  21966. template <typename E>
  21967. unsafe_function_result unsafe_script_file(const std::string& filename, const basic_environment<E>& env, load_mode mode = load_mode::any) {
  21968. int index = lua_gettop(L);
  21969. if (luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str())) {
  21970. lua_error(L);
  21971. }
  21972. set_environment(env, stack_reference(L, raw_index(index + 1)));
  21973. if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
  21974. lua_error(L);
  21975. }
  21976. int postindex = lua_gettop(L);
  21977. int returns = postindex - index;
  21978. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  21979. }
  21980. unsafe_function_result unsafe_script_file(const std::string& filename, load_mode mode = load_mode::any) {
  21981. int index = lua_gettop(L);
  21982. stack::script_file(L, filename, mode);
  21983. int postindex = lua_gettop(L);
  21984. int returns = postindex - index;
  21985. return unsafe_function_result(L, (std::max)(postindex - (returns - 1), 1), returns);
  21986. }
  21987. template <typename Fx,
  21988. meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>,
  21989. meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  21990. protected_function_result script(
  21991. const string_view& code, Fx&& on_error, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  21992. return safe_script(code, std::forward<Fx>(on_error), chunkname, mode);
  21993. }
  21994. template <typename Fx,
  21995. meta::disable_any<meta::is_string_constructible<meta::unqualified_t<Fx>>,
  21996. meta::is_specialization_of<meta::unqualified_t<Fx>, basic_environment>> = meta::enabler>
  21997. protected_function_result script_file(const std::string& filename, Fx&& on_error, load_mode mode = load_mode::any) {
  21998. return safe_script_file(filename, std::forward<Fx>(on_error), mode);
  21999. }
  22000. template <typename Fx, typename E>
  22001. protected_function_result script(const string_view& code, const basic_environment<E>& env, Fx&& on_error,
  22002. const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22003. return safe_script(code, env, std::forward<Fx>(on_error), chunkname, mode);
  22004. }
  22005. template <typename Fx, typename E>
  22006. protected_function_result script_file(const std::string& filename, const basic_environment<E>& env, Fx&& on_error, load_mode mode = load_mode::any) {
  22007. return safe_script_file(filename, env, std::forward<Fx>(on_error), mode);
  22008. }
  22009. protected_function_result script(
  22010. const string_view& code, const environment& env, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22011. return safe_script(code, env, script_default_on_error, chunkname, mode);
  22012. }
  22013. protected_function_result script_file(const std::string& filename, const environment& env, load_mode mode = load_mode::any) {
  22014. return safe_script_file(filename, env, script_default_on_error, mode);
  22015. }
  22016. #if SOL_IS_ON(SOL_SAFE_FUNCTION_OBJECTS_I_)
  22017. protected_function_result script(
  22018. lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22019. return safe_script(reader, data, chunkname, mode);
  22020. }
  22021. protected_function_result script(
  22022. const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22023. return safe_script(code, chunkname, mode);
  22024. }
  22025. protected_function_result script_file(const std::string& filename, load_mode mode = load_mode::any) {
  22026. return safe_script_file(filename, mode);
  22027. }
  22028. #else
  22029. unsafe_function_result script(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22030. return unsafe_script(code, chunkname, mode);
  22031. }
  22032. unsafe_function_result script_file(const std::string& filename, load_mode mode = load_mode::any) {
  22033. return unsafe_script_file(filename, mode);
  22034. }
  22035. #endif
  22036. load_result load(const string_view& code, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22037. detail::typical_chunk_name_t basechunkname = {};
  22038. const char* chunknametarget = detail::make_chunk_name(code, chunkname, basechunkname);
  22039. load_status x = static_cast<load_status>(luaL_loadbufferx(L, code.data(), code.size(), chunknametarget, to_string(mode).c_str()));
  22040. return load_result(L, absolute_index(L, -1), 1, 1, x);
  22041. }
  22042. load_result load_buffer(const char* buff, size_t size, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22043. return load(string_view(buff, size), chunkname, mode);
  22044. }
  22045. load_result load_buffer(
  22046. const std::byte* buff, size_t size, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22047. return load(string_view(reinterpret_cast<const char*>(buff), size), chunkname, mode);
  22048. }
  22049. load_result load_file(const std::string& filename, load_mode mode = load_mode::any) {
  22050. load_status x = static_cast<load_status>(luaL_loadfilex(L, filename.c_str(), to_string(mode).c_str()));
  22051. return load_result(L, absolute_index(L, -1), 1, 1, x);
  22052. }
  22053. load_result load(lua_Reader reader, void* data, const std::string& chunkname = detail::default_chunk_name(), load_mode mode = load_mode::any) {
  22054. detail::typical_chunk_name_t basechunkname = {};
  22055. const char* chunknametarget = detail::make_chunk_name("lua_Reader", chunkname, basechunkname);
  22056. load_status x = static_cast<load_status>(lua_load(L, reader, data, chunknametarget, to_string(mode).c_str()));
  22057. return load_result(L, absolute_index(L, -1), 1, 1, x);
  22058. }
  22059. iterator begin() const {
  22060. return global.begin();
  22061. }
  22062. iterator end() const {
  22063. return global.end();
  22064. }
  22065. const_iterator cbegin() const {
  22066. return global.cbegin();
  22067. }
  22068. const_iterator cend() const {
  22069. return global.cend();
  22070. }
  22071. global_table globals() const {
  22072. // if we return a reference
  22073. // we'll be screwed a bit
  22074. return global;
  22075. }
  22076. global_table& globals() {
  22077. return global;
  22078. }
  22079. table registry() const {
  22080. return reg;
  22081. }
  22082. std::size_t memory_used() const {
  22083. return total_memory_used(lua_state());
  22084. }
  22085. int stack_top() const {
  22086. return stack::top(L);
  22087. }
  22088. int stack_clear() {
  22089. int s = stack_top();
  22090. lua_pop(L, s);
  22091. return s;
  22092. }
  22093. bool supports_gc_mode(gc_mode mode) const noexcept {
  22094. #if SOL_LUA_VESION_I_ >= 504
  22095. // supports all modes
  22096. (void)mode;
  22097. return true;
  22098. #endif
  22099. return mode == gc_mode::default_value;
  22100. }
  22101. bool is_gc_on() const {
  22102. #if SOL_LUA_VESION_I_ >= 502
  22103. return lua_gc(lua_state(), LUA_GCISRUNNING, 0) == 1;
  22104. #else
  22105. // You cannot turn it off in Lua 5.1
  22106. return true;
  22107. #endif
  22108. }
  22109. void collect_garbage() {
  22110. lua_gc(lua_state(), LUA_GCCOLLECT, 0);
  22111. }
  22112. void collect_gc() {
  22113. collect_garbage();
  22114. }
  22115. bool step_gc(int step_size_kilobytes) {
  22116. // THOUGHT: std::chrono-alikes to map "kilobyte size" here...?
  22117. // Make it harder to give MB or KB to a B parameter...?
  22118. // Probably overkill for now.
  22119. #if SOL_LUA_VESION_I_ >= 504
  22120. // The manual implies that this function is almost always successful...
  22121. // is it?? It could depend on the GC mode...
  22122. return lua_gc(lua_state(), LUA_GCSTEP, step_size_kilobytes) != 0;
  22123. #else
  22124. return lua_gc(lua_state(), LUA_GCSTEP, step_size_kilobytes) == 1;
  22125. #endif
  22126. }
  22127. void restart_gc() {
  22128. lua_gc(lua_state(), LUA_GCRESTART, 0);
  22129. }
  22130. void stop_gc() {
  22131. lua_gc(lua_state(), LUA_GCSTOP, 0);
  22132. }
  22133. // Returns the old GC mode. Check support using the supports_gc_mode function.
  22134. gc_mode change_gc_mode_incremental(int pause, int step_multiplier, int step_byte_size) {
  22135. // "What the fuck does any of this mean??"
  22136. // http://www.lua.org/manual/5.4/manual.html#2.5.1
  22137. // THOUGHT: std::chrono-alikes to map "byte size" here...?
  22138. // Make it harder to give MB or KB to a B parameter...?
  22139. // Probably overkill for now.
  22140. #if SOL_LUA_VESION_I_ >= 504
  22141. int old_mode = lua_gc(lua_state(), LUA_GCINC, pause, step_multiplier, step_byte_size);
  22142. if (old_mode == LUA_GCGEN) {
  22143. return gc_mode::generational;
  22144. }
  22145. else if (old_mode == LUA_GCINC) {
  22146. return gc_mode::incremental;
  22147. }
  22148. #else
  22149. lua_gc(lua_state(), LUA_GCSETPAUSE, pause);
  22150. lua_gc(lua_state(), LUA_GCSETSTEPMUL, step_multiplier);
  22151. (void)step_byte_size; // means nothing in older versions
  22152. #endif
  22153. return gc_mode::default_value;
  22154. }
  22155. // Returns the old GC mode. Check support using the supports_gc_mode function.
  22156. gc_mode change_gc_mode_generational(int minor_multiplier, int major_multiplier) {
  22157. #if SOL_LUA_VESION_I_ >= 504
  22158. // "What does this shit mean?"
  22159. // http://www.lua.org/manual/5.4/manual.html#2.5.2
  22160. int old_mode = lua_gc(lua_state(), LUA_GCGEN, minor_multiplier, major_multiplier);
  22161. if (old_mode == LUA_GCGEN) {
  22162. return gc_mode::generational;
  22163. }
  22164. else if (old_mode == LUA_GCINC) {
  22165. return gc_mode::incremental;
  22166. }
  22167. #endif
  22168. return gc_mode::default_value;
  22169. }
  22170. operator lua_State*() const {
  22171. return lua_state();
  22172. }
  22173. void set_panic(lua_CFunction panic) {
  22174. lua_atpanic(lua_state(), panic);
  22175. }
  22176. void set_exception_handler(exception_handler_function handler) {
  22177. set_default_exception_handler(lua_state(), handler);
  22178. }
  22179. template <typename... Args, typename... Keys>
  22180. decltype(auto) get(Keys&&... keys) const {
  22181. return global.get<Args...>(std::forward<Keys>(keys)...);
  22182. }
  22183. template <typename T, typename Key>
  22184. decltype(auto) get_or(Key&& key, T&& otherwise) const {
  22185. return global.get_or(std::forward<Key>(key), std::forward<T>(otherwise));
  22186. }
  22187. template <typename T, typename Key, typename D>
  22188. decltype(auto) get_or(Key&& key, D&& otherwise) const {
  22189. return global.get_or<T>(std::forward<Key>(key), std::forward<D>(otherwise));
  22190. }
  22191. template <typename... Args>
  22192. state_view& set(Args&&... args) {
  22193. global.set(std::forward<Args>(args)...);
  22194. return *this;
  22195. }
  22196. template <typename T, typename... Keys>
  22197. decltype(auto) traverse_get(Keys&&... keys) const {
  22198. return global.traverse_get<T>(std::forward<Keys>(keys)...);
  22199. }
  22200. template <typename... Args>
  22201. state_view& traverse_set(Args&&... args) {
  22202. global.traverse_set(std::forward<Args>(args)...);
  22203. return *this;
  22204. }
  22205. template <typename Class, typename... Args>
  22206. usertype<Class> new_usertype(const std::string& name, Args&&... args) {
  22207. return global.new_usertype<Class>(name, std::forward<Args>(args)...);
  22208. }
  22209. template <bool read_only = true, typename... Args>
  22210. state_view& new_enum(const string_view& name, Args&&... args) {
  22211. global.new_enum<read_only>(name, std::forward<Args>(args)...);
  22212. return *this;
  22213. }
  22214. template <typename T, bool read_only = true>
  22215. state_view& new_enum(const string_view& name, std::initializer_list<std::pair<string_view, T>> items) {
  22216. global.new_enum<T, read_only>(name, std::move(items));
  22217. return *this;
  22218. }
  22219. template <typename Fx>
  22220. void for_each(Fx&& fx) {
  22221. global.for_each(std::forward<Fx>(fx));
  22222. }
  22223. template <typename T>
  22224. table_proxy<global_table&, detail::proxy_key_t<T>> operator[](T&& key) {
  22225. return global[std::forward<T>(key)];
  22226. }
  22227. template <typename T>
  22228. table_proxy<const global_table&, detail::proxy_key_t<T>> operator[](T&& key) const {
  22229. return global[std::forward<T>(key)];
  22230. }
  22231. template <typename Sig, typename... Args, typename Key>
  22232. state_view& set_function(Key&& key, Args&&... args) {
  22233. global.set_function<Sig>(std::forward<Key>(key), std::forward<Args>(args)...);
  22234. return *this;
  22235. }
  22236. template <typename... Args, typename Key>
  22237. state_view& set_function(Key&& key, Args&&... args) {
  22238. global.set_function(std::forward<Key>(key), std::forward<Args>(args)...);
  22239. return *this;
  22240. }
  22241. template <typename Name>
  22242. table create_table(Name&& name, int narr = 0, int nrec = 0) {
  22243. return global.create(std::forward<Name>(name), narr, nrec);
  22244. }
  22245. template <typename Name, typename Key, typename Value, typename... Args>
  22246. table create_table(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  22247. return global.create(std::forward<Name>(name), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  22248. }
  22249. template <typename Name, typename... Args>
  22250. table create_named_table(Name&& name, Args&&... args) {
  22251. table x = global.create_with(std::forward<Args>(args)...);
  22252. global.set(std::forward<Name>(name), x);
  22253. return x;
  22254. }
  22255. table create_table(int narr = 0, int nrec = 0) {
  22256. return create_table(lua_state(), narr, nrec);
  22257. }
  22258. template <typename Key, typename Value, typename... Args>
  22259. table create_table(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  22260. return create_table(lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  22261. }
  22262. template <typename... Args>
  22263. table create_table_with(Args&&... args) {
  22264. return create_table_with(lua_state(), std::forward<Args>(args)...);
  22265. }
  22266. static inline table create_table(lua_State* L, int narr = 0, int nrec = 0) {
  22267. return global_table::create(L, narr, nrec);
  22268. }
  22269. template <typename Key, typename Value, typename... Args>
  22270. static inline table create_table(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
  22271. return global_table::create(L, narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
  22272. }
  22273. template <typename... Args>
  22274. static inline table create_table_with(lua_State* L, Args&&... args) {
  22275. return global_table::create_with(L, std::forward<Args>(args)...);
  22276. }
  22277. };
  22278. } // namespace sol
  22279. // end of sol/state_view.hpp
  22280. // beginning of sol/thread.hpp
  22281. namespace sol {
  22282. struct lua_thread_state {
  22283. lua_State* L;
  22284. lua_thread_state(lua_State* Ls)
  22285. : L(Ls) {
  22286. }
  22287. lua_State* lua_state() const noexcept {
  22288. return L;
  22289. }
  22290. operator lua_State*() const noexcept {
  22291. return lua_state();
  22292. }
  22293. lua_State* operator->() const noexcept {
  22294. return lua_state();
  22295. }
  22296. };
  22297. namespace stack {
  22298. template <>
  22299. struct unqualified_pusher<lua_thread_state> {
  22300. int push(lua_State*, lua_thread_state lts) {
  22301. lua_pushthread(lts.L);
  22302. return 1;
  22303. }
  22304. };
  22305. template <>
  22306. struct unqualified_getter<lua_thread_state> {
  22307. lua_thread_state get(lua_State* L, int index, record& tracking) {
  22308. tracking.use(1);
  22309. lua_thread_state lts( lua_tothread(L, index) );
  22310. return lts;
  22311. }
  22312. };
  22313. template <>
  22314. struct unqualified_check_getter<lua_thread_state> {
  22315. template <typename Handler>
  22316. optional<lua_thread_state> get(lua_State* L, int index, Handler&& handler, record& tracking) {
  22317. lua_thread_state lts( lua_tothread(L, index) );
  22318. if (lts.lua_state() == nullptr) {
  22319. handler(L, index, type::thread, type_of(L, index), "value is not a valid thread type");
  22320. return nullopt;
  22321. }
  22322. tracking.use(1);
  22323. return lts;
  22324. }
  22325. };
  22326. } // namespace stack
  22327. template <typename ref_t>
  22328. class basic_thread : public basic_object<ref_t> {
  22329. private:
  22330. using base_t = basic_object<ref_t>;
  22331. public:
  22332. using base_t::lua_state;
  22333. basic_thread() noexcept = default;
  22334. basic_thread(const basic_thread&) = default;
  22335. basic_thread(basic_thread&&) = default;
  22336. 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>
  22337. basic_thread(T&& r)
  22338. : base_t(std::forward<T>(r)) {
  22339. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22340. auto pp = stack::push_pop(*this);
  22341. constructor_handler handler{};
  22342. stack::check<basic_thread>(lua_state(), -1, handler);
  22343. #endif // Safety
  22344. }
  22345. basic_thread(const stack_reference& r)
  22346. : basic_thread(r.lua_state(), r.stack_index()){};
  22347. basic_thread(stack_reference&& r)
  22348. : basic_thread(r.lua_state(), r.stack_index()){};
  22349. basic_thread& operator=(const basic_thread&) = default;
  22350. basic_thread& operator=(basic_thread&&) = default;
  22351. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  22352. basic_thread(lua_State* L, T&& r)
  22353. : base_t(L, std::forward<T>(r)) {
  22354. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22355. auto pp = stack::push_pop(*this);
  22356. constructor_handler handler{};
  22357. stack::check<basic_thread>(lua_state(), -1, handler);
  22358. #endif // Safety
  22359. }
  22360. basic_thread(lua_State* L, int index = -1)
  22361. : base_t(L, index) {
  22362. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22363. constructor_handler handler{};
  22364. stack::check<basic_thread>(L, index, handler);
  22365. #endif // Safety
  22366. }
  22367. basic_thread(lua_State* L, ref_index index)
  22368. : base_t(L, index) {
  22369. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22370. auto pp = stack::push_pop(*this);
  22371. constructor_handler handler{};
  22372. stack::check<basic_thread>(lua_state(), -1, handler);
  22373. #endif // Safety
  22374. }
  22375. basic_thread(lua_State* L, lua_State* actualthread)
  22376. : basic_thread(L, lua_thread_state{ actualthread }) {
  22377. }
  22378. basic_thread(lua_State* L, this_state actualthread)
  22379. : basic_thread(L, lua_thread_state{ actualthread.L }) {
  22380. }
  22381. basic_thread(lua_State* L, lua_thread_state actualthread)
  22382. : base_t(L, -stack::push(L, actualthread)) {
  22383. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22384. constructor_handler handler{};
  22385. stack::check<basic_thread>(lua_state(), -1, handler);
  22386. #endif // Safety
  22387. if (!is_stack_based<base_t>::value) {
  22388. lua_pop(lua_state(), 1);
  22389. }
  22390. }
  22391. state_view state() const {
  22392. return state_view(this->thread_state());
  22393. }
  22394. bool is_main_thread() const {
  22395. return stack::is_main_thread(this->thread_state());
  22396. }
  22397. lua_State* thread_state() const {
  22398. auto pp = stack::push_pop(*this);
  22399. lua_State* lthread = lua_tothread(lua_state(), -1);
  22400. return lthread;
  22401. }
  22402. thread_status status() const {
  22403. lua_State* lthread = thread_state();
  22404. auto lstat = static_cast<thread_status>(lua_status(lthread));
  22405. if (lstat == thread_status::ok) {
  22406. lua_Debug ar;
  22407. if (lua_getstack(lthread, 0, &ar) > 0)
  22408. return thread_status::ok;
  22409. else if (lua_gettop(lthread) == 0)
  22410. return thread_status::dead;
  22411. else
  22412. return thread_status::yielded;
  22413. }
  22414. return lstat;
  22415. }
  22416. basic_thread create() {
  22417. return create(lua_state());
  22418. }
  22419. static basic_thread create(lua_State* L) {
  22420. lua_newthread(L);
  22421. basic_thread result(L);
  22422. if (!is_stack_based<base_t>::value) {
  22423. lua_pop(L, 1);
  22424. }
  22425. return result;
  22426. }
  22427. };
  22428. typedef basic_thread<reference> thread;
  22429. typedef basic_thread<stack_reference> stack_thread;
  22430. } // namespace sol
  22431. // end of sol/thread.hpp
  22432. namespace sol {
  22433. class state : private std::unique_ptr<lua_State, detail::state_deleter>, public state_view {
  22434. private:
  22435. typedef std::unique_ptr<lua_State, detail::state_deleter> unique_base;
  22436. public:
  22437. state(lua_CFunction panic = default_at_panic)
  22438. : unique_base(luaL_newstate()), state_view(unique_base::get()) {
  22439. set_default_state(unique_base::get(), panic);
  22440. }
  22441. state(lua_CFunction panic, lua_Alloc alfunc, void* alpointer = nullptr)
  22442. : unique_base(lua_newstate(alfunc, alpointer)), state_view(unique_base::get()) {
  22443. set_default_state(unique_base::get(), panic);
  22444. }
  22445. state(const state&) = delete;
  22446. state(state&&) = default;
  22447. state& operator=(const state&) = delete;
  22448. state& operator=(state&& that) {
  22449. state_view::operator=(std::move(that));
  22450. unique_base::operator=(std::move(that));
  22451. return *this;
  22452. }
  22453. using state_view::get;
  22454. ~state() {
  22455. }
  22456. };
  22457. } // namespace sol
  22458. // end of sol/state.hpp
  22459. // beginning of sol/coroutine.hpp
  22460. namespace sol {
  22461. template <typename ref_t>
  22462. class basic_coroutine : public basic_object<ref_t> {
  22463. private:
  22464. using base_t = basic_object<ref_t>;
  22465. public:
  22466. typedef reference handler_t;
  22467. handler_t error_handler;
  22468. private:
  22469. call_status stats = call_status::yielded;
  22470. void luacall(std::ptrdiff_t argcount, std::ptrdiff_t) {
  22471. #if SOL_LUA_VESION_I_ >= 504
  22472. int nresults;
  22473. stats = static_cast<call_status>(lua_resume(lua_state(), nullptr, static_cast<int>(argcount), &nresults));
  22474. #else
  22475. stats = static_cast<call_status>(lua_resume(lua_state(), nullptr, static_cast<int>(argcount)));
  22476. #endif
  22477. }
  22478. template <std::size_t... I, typename... Ret>
  22479. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n) {
  22480. luacall(n, sizeof...(Ret));
  22481. return stack::pop<std::tuple<Ret...>>(lua_state());
  22482. }
  22483. template <std::size_t I, typename Ret>
  22484. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n) {
  22485. luacall(n, 1);
  22486. return stack::pop<Ret>(lua_state());
  22487. }
  22488. template <std::size_t I>
  22489. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n) {
  22490. luacall(n, 0);
  22491. }
  22492. protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) {
  22493. int firstreturn = 1;
  22494. luacall(n, LUA_MULTRET);
  22495. int poststacksize = lua_gettop(this->lua_state());
  22496. int returncount = poststacksize - (firstreturn - 1);
  22497. if (error()) {
  22498. if (error_handler.valid()) {
  22499. string_view err = stack::get<string_view>(this->lua_state(), poststacksize);
  22500. error_handler.push();
  22501. stack::push(this->lua_state(), err);
  22502. lua_call(lua_state(), 1, 1);
  22503. }
  22504. return protected_function_result(this->lua_state(), lua_absindex(this->lua_state(), -1), 1, returncount, status());
  22505. }
  22506. return protected_function_result(this->lua_state(), firstreturn, returncount, returncount, status());
  22507. }
  22508. public:
  22509. using base_t::lua_state;
  22510. basic_coroutine() = default;
  22511. template <typename T,
  22512. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_coroutine>>,
  22513. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<T>>>, meta::neg<std::is_same<base_t, stack_reference>>,
  22514. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  22515. basic_coroutine(T&& r) noexcept
  22516. : base_t(std::forward<T>(r)), error_handler(detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  22517. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22518. if (!is_function<meta::unqualified_t<T>>::value) {
  22519. auto pp = stack::push_pop(*this);
  22520. constructor_handler handler {};
  22521. stack::check<basic_coroutine>(lua_state(), -1, handler);
  22522. }
  22523. #endif // Safety
  22524. }
  22525. basic_coroutine(const basic_coroutine&) = default;
  22526. basic_coroutine& operator=(const basic_coroutine&) = default;
  22527. basic_coroutine(basic_coroutine&&) = default;
  22528. basic_coroutine& operator=(basic_coroutine&&) = default;
  22529. basic_coroutine(const basic_function<base_t>& b)
  22530. : basic_coroutine(b, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(b.lua_state())) {
  22531. }
  22532. basic_coroutine(basic_function<base_t>&& b)
  22533. : basic_coroutine(std::move(b), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(b.lua_state())) {
  22534. }
  22535. basic_coroutine(const basic_function<base_t>& b, handler_t eh) : base_t(b), error_handler(std::move(eh)) {
  22536. }
  22537. basic_coroutine(basic_function<base_t>&& b, handler_t eh) : base_t(std::move(b)), error_handler(std::move(eh)) {
  22538. }
  22539. basic_coroutine(const stack_reference& r)
  22540. : basic_coroutine(r.lua_state(), r.stack_index(), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  22541. }
  22542. basic_coroutine(stack_reference&& r)
  22543. : basic_coroutine(r.lua_state(), r.stack_index(), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  22544. }
  22545. basic_coroutine(const stack_reference& r, handler_t eh) : basic_coroutine(r.lua_state(), r.stack_index(), std::move(eh)) {
  22546. }
  22547. basic_coroutine(stack_reference&& r, handler_t eh) : basic_coroutine(r.lua_state(), r.stack_index(), std::move(eh)) {
  22548. }
  22549. template <typename Super>
  22550. basic_coroutine(const proxy_base<Super>& p)
  22551. : basic_coroutine(p, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(p.lua_state())) {
  22552. }
  22553. template <typename Super>
  22554. basic_coroutine(proxy_base<Super>&& p)
  22555. : basic_coroutine(std::move(p), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(p.lua_state())) {
  22556. }
  22557. template <typename Proxy, typename Handler,
  22558. meta::enable<std::is_base_of<proxy_base_tag, meta::unqualified_t<Proxy>>, meta::neg<is_lua_index<meta::unqualified_t<Handler>>>> = meta::enabler>
  22559. basic_coroutine(Proxy&& p, Handler&& eh) : basic_coroutine(detail::force_cast<base_t>(p), std::forward<Handler>(eh)) {
  22560. }
  22561. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  22562. basic_coroutine(lua_State* L, T&& r)
  22563. : basic_coroutine(L, std::forward<T>(r), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  22564. }
  22565. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  22566. basic_coroutine(lua_State* L, T&& r, handler_t eh) : base_t(L, std::forward<T>(r)), error_handler(std::move(eh)) {
  22567. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22568. auto pp = stack::push_pop(*this);
  22569. constructor_handler handler {};
  22570. stack::check<basic_coroutine>(lua_state(), -1, handler);
  22571. #endif // Safety
  22572. }
  22573. basic_coroutine(lua_nil_t n) : base_t(n), error_handler(n) {
  22574. }
  22575. basic_coroutine(lua_State* L, int index = -1)
  22576. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  22577. }
  22578. basic_coroutine(lua_State* L, int index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) {
  22579. #ifdef SOL_SAFE_REFERENCES
  22580. constructor_handler handler {};
  22581. stack::check<basic_coroutine>(L, index, handler);
  22582. #endif // Safety
  22583. }
  22584. basic_coroutine(lua_State* L, absolute_index index)
  22585. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  22586. }
  22587. basic_coroutine(lua_State* L, absolute_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) {
  22588. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22589. constructor_handler handler {};
  22590. stack::check<basic_coroutine>(L, index, handler);
  22591. #endif // Safety
  22592. }
  22593. basic_coroutine(lua_State* L, raw_index index)
  22594. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  22595. }
  22596. basic_coroutine(lua_State* L, raw_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) {
  22597. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22598. constructor_handler handler {};
  22599. stack::check<basic_coroutine>(L, index, handler);
  22600. #endif // Safety
  22601. }
  22602. basic_coroutine(lua_State* L, ref_index index)
  22603. : basic_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  22604. }
  22605. basic_coroutine(lua_State* L, ref_index index, handler_t eh) : base_t(L, index), error_handler(std::move(eh)) {
  22606. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22607. auto pp = stack::push_pop(*this);
  22608. constructor_handler handler {};
  22609. stack::check<basic_coroutine>(lua_state(), -1, handler);
  22610. #endif // Safety
  22611. }
  22612. call_status status() const noexcept {
  22613. return stats;
  22614. }
  22615. bool error() const noexcept {
  22616. call_status cs = status();
  22617. return cs != call_status::ok && cs != call_status::yielded;
  22618. }
  22619. bool runnable() const noexcept {
  22620. return base_t::valid() && (status() == call_status::yielded);
  22621. }
  22622. explicit operator bool() const noexcept {
  22623. return runnable();
  22624. }
  22625. template <typename... Args>
  22626. protected_function_result operator()(Args&&... args) {
  22627. return call<>(std::forward<Args>(args)...);
  22628. }
  22629. template <typename... Ret, typename... Args>
  22630. decltype(auto) operator()(types<Ret...>, Args&&... args) {
  22631. return call<Ret...>(std::forward<Args>(args)...);
  22632. }
  22633. template <typename... Ret, typename... Args>
  22634. decltype(auto) call(Args&&... args) {
  22635. // some users screw up coroutine.create
  22636. // and try to use it with sol::coroutine without ever calling the first resume in Lua
  22637. // this makes the stack incompatible with other kinds of stacks: protect against this
  22638. // make sure coroutines don't screw us over
  22639. base_t::push();
  22640. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  22641. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount);
  22642. }
  22643. };
  22644. } // namespace sol
  22645. // end of sol/coroutine.hpp
  22646. // beginning of sol/userdata.hpp
  22647. namespace sol {
  22648. template <typename base_type>
  22649. class basic_userdata : public basic_table<base_type> {
  22650. private:
  22651. using base_t = basic_table<base_type>;
  22652. public:
  22653. using base_t::lua_state;
  22654. basic_userdata() noexcept = default;
  22655. 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>
  22656. basic_userdata(T&& r) noexcept
  22657. : base_t(std::forward<T>(r)) {
  22658. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22659. if (!is_userdata<meta::unqualified_t<T>>::value) {
  22660. auto pp = stack::push_pop(*this);
  22661. type_assert(lua_state(), -1, type::userdata);
  22662. }
  22663. #endif // Safety
  22664. }
  22665. basic_userdata(const basic_userdata&) = default;
  22666. basic_userdata(basic_userdata&&) = default;
  22667. basic_userdata& operator=(const basic_userdata&) = default;
  22668. basic_userdata& operator=(basic_userdata&&) = default;
  22669. basic_userdata(const stack_reference& r)
  22670. : basic_userdata(r.lua_state(), r.stack_index()) {
  22671. }
  22672. basic_userdata(stack_reference&& r)
  22673. : basic_userdata(r.lua_state(), r.stack_index()) {
  22674. }
  22675. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  22676. basic_userdata(lua_State* L, T&& r)
  22677. : base_t(L, std::forward<T>(r)) {
  22678. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22679. auto pp = stack::push_pop(*this);
  22680. constructor_handler handler{};
  22681. stack::check<basic_userdata>(L, -1, handler);
  22682. #endif // Safety
  22683. }
  22684. basic_userdata(lua_State* L, int index = -1)
  22685. : base_t(detail::no_safety, L, index) {
  22686. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22687. constructor_handler handler{};
  22688. stack::check<basic_userdata>(L, index, handler);
  22689. #endif // Safety
  22690. }
  22691. basic_userdata(lua_State* L, ref_index index)
  22692. : base_t(detail::no_safety, L, index) {
  22693. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22694. auto pp = stack::push_pop(*this);
  22695. constructor_handler handler{};
  22696. stack::check<basic_userdata>(L, -1, handler);
  22697. #endif // Safety
  22698. }
  22699. };
  22700. template <typename base_type>
  22701. class basic_lightuserdata : public basic_object_base<base_type> {
  22702. typedef basic_object_base<base_type> base_t;
  22703. public:
  22704. using base_t::lua_state;
  22705. basic_lightuserdata() noexcept = default;
  22706. 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>
  22707. basic_lightuserdata(T&& r) noexcept
  22708. : base_t(std::forward<T>(r)) {
  22709. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22710. if (!is_lightuserdata<meta::unqualified_t<T>>::value) {
  22711. auto pp = stack::push_pop(*this);
  22712. type_assert(lua_state(), -1, type::lightuserdata);
  22713. }
  22714. #endif // Safety
  22715. }
  22716. basic_lightuserdata(const basic_lightuserdata&) = default;
  22717. basic_lightuserdata(basic_lightuserdata&&) = default;
  22718. basic_lightuserdata& operator=(const basic_lightuserdata&) = default;
  22719. basic_lightuserdata& operator=(basic_lightuserdata&&) = default;
  22720. basic_lightuserdata(const stack_reference& r)
  22721. : basic_lightuserdata(r.lua_state(), r.stack_index()) {
  22722. }
  22723. basic_lightuserdata(stack_reference&& r)
  22724. : basic_lightuserdata(r.lua_state(), r.stack_index()) {
  22725. }
  22726. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  22727. basic_lightuserdata(lua_State* L, T&& r)
  22728. : basic_lightuserdata(L, std::forward<T>(r)) {
  22729. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22730. auto pp = stack::push_pop(*this);
  22731. constructor_handler handler{};
  22732. stack::check<basic_lightuserdata>(lua_state(), -1, handler);
  22733. #endif // Safety
  22734. }
  22735. basic_lightuserdata(lua_State* L, int index = -1)
  22736. : base_t(L, index) {
  22737. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22738. constructor_handler handler{};
  22739. stack::check<basic_lightuserdata>(L, index, handler);
  22740. #endif // Safety
  22741. }
  22742. basic_lightuserdata(lua_State* L, ref_index index)
  22743. : base_t(L, index) {
  22744. #if SOL_IS_ON(SOL_SAFE_REFERENCES_I_)
  22745. auto pp = stack::push_pop(*this);
  22746. constructor_handler handler{};
  22747. stack::check<basic_lightuserdata>(lua_state(), index, handler);
  22748. #endif // Safety
  22749. }
  22750. };
  22751. } // namespace sol
  22752. // end of sol/userdata.hpp
  22753. // beginning of sol/as_args.hpp
  22754. namespace sol {
  22755. template <typename T>
  22756. struct as_args_t {
  22757. T src;
  22758. };
  22759. template <typename Source>
  22760. auto as_args(Source&& source) {
  22761. return as_args_t<Source> { std::forward<Source>(source) };
  22762. }
  22763. namespace stack {
  22764. template <typename T>
  22765. struct unqualified_pusher<as_args_t<T>> {
  22766. int push(lua_State* L, const as_args_t<T>& e) {
  22767. int p = 0;
  22768. for (const auto& i : e.src) {
  22769. p += stack::push(L, i);
  22770. }
  22771. return p;
  22772. }
  22773. };
  22774. } // namespace stack
  22775. } // namespace sol
  22776. // end of sol/as_args.hpp
  22777. // beginning of sol/variadic_args.hpp
  22778. #include <limits>
  22779. #include <iterator>
  22780. namespace sol {
  22781. struct variadic_args {
  22782. private:
  22783. lua_State* L;
  22784. int index;
  22785. int stacktop;
  22786. public:
  22787. typedef stack_proxy reference_type;
  22788. typedef stack_proxy value_type;
  22789. typedef stack_proxy* pointer;
  22790. typedef std::ptrdiff_t difference_type;
  22791. typedef std::size_t size_type;
  22792. typedef stack_iterator<stack_proxy, false> iterator;
  22793. typedef stack_iterator<stack_proxy, true> const_iterator;
  22794. typedef std::reverse_iterator<iterator> reverse_iterator;
  22795. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  22796. variadic_args() = default;
  22797. variadic_args(lua_State* luastate, int stackindex = -1)
  22798. : L(luastate), index(lua_absindex(luastate, stackindex)), stacktop(lua_gettop(luastate)) {
  22799. }
  22800. variadic_args(lua_State* luastate, int stackindex, int lastindex)
  22801. : L(luastate), index(lua_absindex(luastate, stackindex)), stacktop(lastindex) {
  22802. }
  22803. variadic_args(const variadic_args&) = default;
  22804. variadic_args& operator=(const variadic_args&) = default;
  22805. variadic_args(variadic_args&& o)
  22806. : L(o.L), index(o.index), stacktop(o.stacktop) {
  22807. // Must be manual, otherwise destructor will screw us
  22808. // return count being 0 is enough to keep things clean
  22809. // but will be thorough
  22810. o.L = nullptr;
  22811. o.index = 0;
  22812. o.stacktop = 0;
  22813. }
  22814. variadic_args& operator=(variadic_args&& o) {
  22815. L = o.L;
  22816. index = o.index;
  22817. stacktop = o.stacktop;
  22818. // Must be manual, otherwise destructor will screw us
  22819. // return count being 0 is enough to keep things clean
  22820. // but will be thorough
  22821. o.L = nullptr;
  22822. o.index = 0;
  22823. o.stacktop = 0;
  22824. return *this;
  22825. }
  22826. iterator begin() {
  22827. return iterator(L, index, stacktop + 1);
  22828. }
  22829. iterator end() {
  22830. return iterator(L, stacktop + 1, stacktop + 1);
  22831. }
  22832. const_iterator begin() const {
  22833. return const_iterator(L, index, stacktop + 1);
  22834. }
  22835. const_iterator end() const {
  22836. return const_iterator(L, stacktop + 1, stacktop + 1);
  22837. }
  22838. const_iterator cbegin() const {
  22839. return begin();
  22840. }
  22841. const_iterator cend() const {
  22842. return end();
  22843. }
  22844. reverse_iterator rbegin() {
  22845. return std::reverse_iterator<iterator>(begin());
  22846. }
  22847. reverse_iterator rend() {
  22848. return std::reverse_iterator<iterator>(end());
  22849. }
  22850. const_reverse_iterator rbegin() const {
  22851. return std::reverse_iterator<const_iterator>(begin());
  22852. }
  22853. const_reverse_iterator rend() const {
  22854. return std::reverse_iterator<const_iterator>(end());
  22855. }
  22856. const_reverse_iterator crbegin() const {
  22857. return std::reverse_iterator<const_iterator>(cbegin());
  22858. }
  22859. const_reverse_iterator crend() const {
  22860. return std::reverse_iterator<const_iterator>(cend());
  22861. }
  22862. int push() const {
  22863. return push(L);
  22864. }
  22865. int push(lua_State* target) const {
  22866. int pushcount = 0;
  22867. for (int i = index; i <= stacktop; ++i) {
  22868. lua_pushvalue(L, i);
  22869. pushcount += 1;
  22870. }
  22871. if (target != L) {
  22872. lua_xmove(L, target, pushcount);
  22873. }
  22874. return pushcount;
  22875. }
  22876. template <typename T>
  22877. decltype(auto) get(difference_type index_offset = 0) const {
  22878. return stack::get<T>(L, index + static_cast<int>(index_offset));
  22879. }
  22880. type get_type(difference_type index_offset = 0) const noexcept {
  22881. return type_of(L, index + static_cast<int>(index_offset));
  22882. }
  22883. stack_proxy operator[](difference_type index_offset) const {
  22884. return stack_proxy(L, index + static_cast<int>(index_offset));
  22885. }
  22886. lua_State* lua_state() const {
  22887. return L;
  22888. };
  22889. int stack_index() const {
  22890. return index;
  22891. };
  22892. int leftover_count() const {
  22893. return stacktop - (index - 1);
  22894. }
  22895. std::size_t size() const {
  22896. return static_cast<std::size_t>(leftover_count());
  22897. }
  22898. int top() const {
  22899. return stacktop;
  22900. }
  22901. };
  22902. namespace stack {
  22903. template <>
  22904. struct unqualified_getter<variadic_args> {
  22905. static variadic_args get(lua_State* L, int index, record& tracking) {
  22906. tracking.last = 0;
  22907. return variadic_args(L, index);
  22908. }
  22909. };
  22910. template <>
  22911. struct unqualified_pusher<variadic_args> {
  22912. static int push(lua_State* L, const variadic_args& ref) {
  22913. return ref.push(L);
  22914. }
  22915. };
  22916. } // namespace stack
  22917. } // namespace sol
  22918. // end of sol/variadic_args.hpp
  22919. // beginning of sol/variadic_results.hpp
  22920. // beginning of sol/as_returns.hpp
  22921. namespace sol {
  22922. template <typename T>
  22923. struct as_returns_t {
  22924. T src;
  22925. };
  22926. template <typename Source>
  22927. auto as_returns(Source&& source) {
  22928. return as_returns_t<std::decay_t<Source>>{ std::forward<Source>(source) };
  22929. }
  22930. namespace stack {
  22931. template <typename T>
  22932. struct unqualified_pusher<as_returns_t<T>> {
  22933. int push(lua_State* L, const as_returns_t<T>& e) {
  22934. auto& src = detail::unwrap(e.src);
  22935. int p = 0;
  22936. for (const auto& i : src) {
  22937. p += stack::push(L, i);
  22938. }
  22939. return p;
  22940. }
  22941. };
  22942. } // namespace stack
  22943. } // namespace sol
  22944. // end of sol/as_returns.hpp
  22945. #include <vector>
  22946. namespace sol {
  22947. template <typename Al = typename std::allocator<object>>
  22948. struct basic_variadic_results : public std::vector<object, Al> {
  22949. private:
  22950. using base_t = std::vector<object, Al>;
  22951. public:
  22952. basic_variadic_results() : base_t() {
  22953. }
  22954. basic_variadic_results(unsafe_function_result fr) : base_t() {
  22955. this->reserve(fr.return_count());
  22956. this->insert(this->cend(), fr.begin(), fr.end());
  22957. }
  22958. basic_variadic_results(protected_function_result fr) : base_t() {
  22959. this->reserve(fr.return_count());
  22960. this->insert(this->cend(), fr.begin(), fr.end());
  22961. }
  22962. template <typename Arg0, typename... Args,
  22963. meta::disable_any<std::is_same<meta::unqualified_t<Arg0>, basic_variadic_results>, std::is_same<meta::unqualified_t<Arg0>, function_result>,
  22964. std::is_same<meta::unqualified_t<Arg0>, protected_function_result>> = meta::enabler>
  22965. basic_variadic_results(Arg0&& arg0, Args&&... args) : base_t(std::forward<Arg0>(arg0), std::forward<Args>(args)...) {
  22966. }
  22967. basic_variadic_results(const basic_variadic_results&) = default;
  22968. basic_variadic_results(basic_variadic_results&&) = default;
  22969. };
  22970. struct variadic_results : public basic_variadic_results<> {
  22971. private:
  22972. using base_t = basic_variadic_results<>;
  22973. public:
  22974. using base_t::base_t;
  22975. };
  22976. template <typename Al>
  22977. struct is_container<basic_variadic_results<Al>> : std::false_type { };
  22978. template <>
  22979. struct is_container<variadic_results> : std::false_type { };
  22980. namespace stack {
  22981. template <typename Al>
  22982. struct unqualified_pusher<basic_variadic_results<Al>> {
  22983. int push(lua_State* L, const basic_variadic_results<Al>& e) {
  22984. int p = 0;
  22985. for (const auto& i : e) {
  22986. p += stack::push(L, i);
  22987. }
  22988. return p;
  22989. }
  22990. };
  22991. template <>
  22992. struct unqualified_pusher<variadic_results> {
  22993. int push(lua_State* L, const variadic_results& r) {
  22994. using base_t = basic_variadic_results<>;
  22995. return stack::push(L, static_cast<const base_t&>(r));
  22996. }
  22997. };
  22998. } // namespace stack
  22999. } // namespace sol
  23000. // end of sol/variadic_results.hpp
  23001. #if SOL_IS_ON(SOL_COMPILER_GCC_I_)
  23002. #pragma GCC diagnostic pop
  23003. #elif SOL_IS_ON(SOL_COMPILER_VCXX_I_)
  23004. #pragma warning(pop)
  23005. #endif // g++
  23006. #if SOL_IS_ON(SOL_INSIDE_UNREAL_ENGINE_I_)
  23007. #undef check
  23008. #pragma pop_macro("check")
  23009. #endif // Unreal Engine 4 Bullshit
  23010. #endif // SOL_HPP
  23011. // end of sol/sol.hpp
  23012. #endif // SOL_SINGLE_INCLUDE_HPP