duktape.c 3.4 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832698336983469835698366983769838698396984069841698426984369844698456984669847698486984969850698516985269853698546985569856698576985869859698606986169862698636986469865698666986769868698696987069871698726987369874698756987669877698786987969880698816988269883698846988569886698876988869889698906989169892698936989469895698966989769898698996990069901699026990369904699056990669907699086990969910699116991269913699146991569916699176991869919699206992169922699236992469925699266992769928699296993069931699326993369934699356993669937699386993969940699416994269943699446994569946699476994869949699506995169952699536995469955699566995769958699596996069961699626996369964699656996669967699686996969970699716997269973699746997569976699776997869979699806998169982699836998469985699866998769988699896999069991699926999369994699956999669997699986999970000700017000270003700047000570006700077000870009700107001170012700137001470015700167001770018700197002070021700227002370024700257002670027700287002970030700317003270033700347003570036700377003870039700407004170042700437004470045700467004770048700497005070051700527005370054700557005670057700587005970060700617006270063700647006570066700677006870069700707007170072700737007470075700767007770078700797008070081700827008370084700857008670087700887008970090700917009270093700947009570096700977009870099701007010170102701037010470105701067010770108701097011070111701127011370114701157011670117701187011970120701217012270123701247012570126701277012870129701307013170132701337013470135701367013770138701397014070141701427014370144701457014670147701487014970150701517015270153701547015570156701577015870159701607016170162701637016470165701667016770168701697017070171701727017370174701757017670177701787017970180701817018270183701847018570186701877018870189701907019170192701937019470195701967019770198701997020070201702027020370204702057020670207702087020970210702117021270213702147021570216702177021870219702207022170222702237022470225702267022770228702297023070231702327023370234702357023670237702387023970240702417024270243702447024570246702477024870249702507025170252702537025470255702567025770258702597026070261702627026370264702657026670267702687026970270702717027270273702747027570276702777027870279702807028170282702837028470285702867028770288702897029070291702927029370294702957029670297702987029970300703017030270303703047030570306703077030870309703107031170312703137031470315703167031770318703197032070321703227032370324703257032670327703287032970330703317033270333703347033570336703377033870339703407034170342703437034470345703467034770348703497035070351703527035370354703557035670357703587035970360703617036270363703647036570366703677036870369703707037170372703737037470375703767037770378703797038070381703827038370384703857038670387703887038970390703917039270393703947039570396703977039870399704007040170402704037040470405704067040770408704097041070411704127041370414704157041670417704187041970420704217042270423704247042570426704277042870429704307043170432704337043470435704367043770438704397044070441704427044370444704457044670447704487044970450704517045270453704547045570456704577045870459704607046170462704637046470465704667046770468704697047070471704727047370474704757047670477704787047970480704817048270483704847048570486704877048870489704907049170492704937049470495704967049770498704997050070501705027050370504705057050670507705087050970510705117051270513705147051570516705177051870519705207052170522705237052470525705267052770528705297053070531705327053370534705357053670537705387053970540705417054270543705447054570546705477054870549705507055170552705537055470555705567055770558705597056070561705627056370564705657056670567705687056970570705717057270573705747057570576705777057870579705807058170582705837058470585705867058770588705897059070591705927059370594705957059670597705987059970600706017060270603706047060570606706077060870609706107061170612706137061470615706167061770618706197062070621706227062370624706257062670627706287062970630706317063270633706347063570636706377063870639706407064170642706437064470645706467064770648706497065070651706527065370654706557065670657706587065970660706617066270663706647066570666706677066870669706707067170672706737067470675706767067770678706797068070681706827068370684706857068670687706887068970690706917069270693706947069570696706977069870699707007070170702707037070470705707067070770708707097071070711707127071370714707157071670717707187071970720707217072270723707247072570726707277072870729707307073170732707337073470735707367073770738707397074070741707427074370744707457074670747707487074970750707517075270753707547075570756707577075870759707607076170762707637076470765707667076770768707697077070771707727077370774707757077670777707787077970780707817078270783707847078570786707877078870789707907079170792707937079470795707967079770798707997080070801708027080370804708057080670807708087080970810708117081270813708147081570816708177081870819708207082170822708237082470825708267082770828708297083070831708327083370834708357083670837708387083970840708417084270843708447084570846708477084870849708507085170852708537085470855708567085770858708597086070861708627086370864708657086670867708687086970870708717087270873708747087570876708777087870879708807088170882708837088470885708867088770888708897089070891708927089370894708957089670897708987089970900709017090270903709047090570906709077090870909709107091170912709137091470915709167091770918709197092070921709227092370924709257092670927709287092970930709317093270933709347093570936709377093870939709407094170942709437094470945709467094770948709497095070951709527095370954709557095670957709587095970960709617096270963709647096570966709677096870969709707097170972709737097470975709767097770978709797098070981709827098370984709857098670987709887098970990709917099270993709947099570996709977099870999710007100171002710037100471005710067100771008710097101071011710127101371014710157101671017710187101971020710217102271023710247102571026710277102871029710307103171032710337103471035710367103771038710397104071041710427104371044710457104671047710487104971050710517105271053710547105571056710577105871059710607106171062710637106471065710667106771068710697107071071710727107371074710757107671077710787107971080710817108271083710847108571086710877108871089710907109171092710937109471095710967109771098710997110071101711027110371104711057110671107711087110971110711117111271113711147111571116711177111871119711207112171122711237112471125711267112771128711297113071131711327113371134711357113671137711387113971140711417114271143711447114571146711477114871149711507115171152711537115471155711567115771158711597116071161711627116371164711657116671167711687116971170711717117271173711747117571176711777117871179711807118171182711837118471185711867118771188711897119071191711927119371194711957119671197711987119971200712017120271203712047120571206712077120871209712107121171212712137121471215712167121771218712197122071221712227122371224712257122671227712287122971230712317123271233712347123571236712377123871239712407124171242712437124471245712467124771248712497125071251712527125371254712557125671257712587125971260712617126271263712647126571266712677126871269712707127171272712737127471275712767127771278712797128071281712827128371284712857128671287712887128971290712917129271293712947129571296712977129871299713007130171302713037130471305713067130771308713097131071311713127131371314713157131671317713187131971320713217132271323713247132571326713277132871329713307133171332713337133471335713367133771338713397134071341713427134371344713457134671347713487134971350713517135271353713547135571356713577135871359713607136171362713637136471365713667136771368713697137071371713727137371374713757137671377713787137971380713817138271383713847138571386713877138871389713907139171392713937139471395713967139771398713997140071401714027140371404714057140671407714087140971410714117141271413714147141571416714177141871419714207142171422714237142471425714267142771428714297143071431714327143371434714357143671437714387143971440714417144271443714447144571446714477144871449714507145171452714537145471455714567145771458714597146071461714627146371464714657146671467714687146971470714717147271473714747147571476714777147871479714807148171482714837148471485714867148771488714897149071491714927149371494714957149671497714987149971500715017150271503715047150571506715077150871509715107151171512715137151471515715167151771518715197152071521715227152371524715257152671527715287152971530715317153271533715347153571536715377153871539715407154171542715437154471545715467154771548715497155071551715527155371554715557155671557715587155971560715617156271563715647156571566715677156871569715707157171572715737157471575715767157771578715797158071581715827158371584715857158671587715887158971590715917159271593715947159571596715977159871599716007160171602716037160471605716067160771608716097161071611716127161371614716157161671617716187161971620716217162271623716247162571626716277162871629716307163171632716337163471635716367163771638716397164071641716427164371644716457164671647716487164971650716517165271653716547165571656716577165871659716607166171662716637166471665716667166771668716697167071671716727167371674716757167671677716787167971680716817168271683716847168571686716877168871689716907169171692716937169471695716967169771698716997170071701717027170371704717057170671707717087170971710717117171271713717147171571716717177171871719717207172171722717237172471725717267172771728717297173071731717327173371734717357173671737717387173971740717417174271743717447174571746717477174871749717507175171752717537175471755717567175771758717597176071761717627176371764717657176671767717687176971770717717177271773717747177571776717777177871779717807178171782717837178471785717867178771788717897179071791717927179371794717957179671797717987179971800718017180271803718047180571806718077180871809718107181171812718137181471815718167181771818718197182071821718227182371824718257182671827718287182971830718317183271833718347183571836718377183871839718407184171842718437184471845718467184771848718497185071851718527185371854718557185671857718587185971860718617186271863718647186571866718677186871869718707187171872718737187471875718767187771878718797188071881718827188371884718857188671887718887188971890718917189271893718947189571896718977189871899719007190171902719037190471905719067190771908719097191071911719127191371914719157191671917719187191971920719217192271923719247192571926719277192871929719307193171932719337193471935719367193771938719397194071941719427194371944719457194671947719487194971950719517195271953719547195571956719577195871959719607196171962719637196471965719667196771968719697197071971719727197371974719757197671977719787197971980719817198271983719847198571986719877198871989719907199171992719937199471995719967199771998719997200072001720027200372004720057200672007720087200972010720117201272013720147201572016720177201872019720207202172022720237202472025720267202772028720297203072031720327203372034720357203672037720387203972040720417204272043720447204572046720477204872049720507205172052720537205472055720567205772058720597206072061720627206372064720657206672067720687206972070720717207272073720747207572076720777207872079720807208172082720837208472085720867208772088720897209072091720927209372094720957209672097720987209972100721017210272103721047210572106721077210872109721107211172112721137211472115721167211772118721197212072121721227212372124721257212672127721287212972130721317213272133721347213572136721377213872139721407214172142721437214472145721467214772148721497215072151721527215372154721557215672157721587215972160721617216272163721647216572166721677216872169721707217172172721737217472175721767217772178721797218072181721827218372184721857218672187721887218972190721917219272193721947219572196721977219872199722007220172202722037220472205722067220772208722097221072211722127221372214722157221672217722187221972220722217222272223722247222572226722277222872229722307223172232722337223472235722367223772238722397224072241722427224372244722457224672247722487224972250722517225272253722547225572256722577225872259722607226172262722637226472265722667226772268722697227072271722727227372274722757227672277722787227972280722817228272283722847228572286722877228872289722907229172292722937229472295722967229772298722997230072301723027230372304723057230672307723087230972310723117231272313723147231572316723177231872319723207232172322723237232472325723267232772328723297233072331723327233372334723357233672337723387233972340723417234272343723447234572346723477234872349723507235172352723537235472355723567235772358723597236072361723627236372364723657236672367723687236972370723717237272373723747237572376723777237872379723807238172382723837238472385723867238772388723897239072391723927239372394723957239672397723987239972400724017240272403724047240572406724077240872409724107241172412724137241472415724167241772418724197242072421724227242372424724257242672427724287242972430724317243272433724347243572436724377243872439724407244172442724437244472445724467244772448724497245072451724527245372454724557245672457724587245972460724617246272463724647246572466724677246872469724707247172472724737247472475724767247772478724797248072481724827248372484724857248672487724887248972490724917249272493724947249572496724977249872499725007250172502725037250472505725067250772508725097251072511725127251372514725157251672517725187251972520725217252272523725247252572526725277252872529725307253172532725337253472535725367253772538725397254072541725427254372544725457254672547725487254972550725517255272553725547255572556725577255872559725607256172562725637256472565725667256772568725697257072571725727257372574725757257672577725787257972580725817258272583725847258572586725877258872589725907259172592725937259472595725967259772598725997260072601726027260372604726057260672607726087260972610726117261272613726147261572616726177261872619726207262172622726237262472625726267262772628726297263072631726327263372634726357263672637726387263972640726417264272643726447264572646726477264872649726507265172652726537265472655726567265772658726597266072661726627266372664726657266672667726687266972670726717267272673726747267572676726777267872679726807268172682726837268472685726867268772688726897269072691726927269372694726957269672697726987269972700727017270272703727047270572706727077270872709727107271172712727137271472715727167271772718727197272072721727227272372724727257272672727727287272972730727317273272733727347273572736727377273872739727407274172742727437274472745727467274772748727497275072751727527275372754727557275672757727587275972760727617276272763727647276572766727677276872769727707277172772727737277472775727767277772778727797278072781727827278372784727857278672787727887278972790727917279272793727947279572796727977279872799728007280172802728037280472805728067280772808728097281072811728127281372814728157281672817728187281972820728217282272823728247282572826728277282872829728307283172832728337283472835728367283772838728397284072841728427284372844728457284672847728487284972850728517285272853728547285572856728577285872859728607286172862728637286472865728667286772868728697287072871728727287372874728757287672877728787287972880728817288272883728847288572886728877288872889728907289172892728937289472895728967289772898728997290072901729027290372904729057290672907729087290972910729117291272913729147291572916729177291872919729207292172922729237292472925729267292772928729297293072931729327293372934729357293672937729387293972940729417294272943729447294572946729477294872949729507295172952729537295472955729567295772958729597296072961729627296372964729657296672967729687296972970729717297272973729747297572976729777297872979729807298172982729837298472985729867298772988729897299072991729927299372994729957299672997729987299973000730017300273003730047300573006730077300873009730107301173012730137301473015730167301773018730197302073021730227302373024730257302673027730287302973030730317303273033730347303573036730377303873039730407304173042730437304473045730467304773048730497305073051730527305373054730557305673057730587305973060730617306273063730647306573066730677306873069730707307173072730737307473075730767307773078730797308073081730827308373084730857308673087730887308973090730917309273093730947309573096730977309873099731007310173102731037310473105731067310773108731097311073111731127311373114731157311673117731187311973120731217312273123731247312573126731277312873129731307313173132731337313473135731367313773138731397314073141731427314373144731457314673147731487314973150731517315273153731547315573156731577315873159731607316173162731637316473165731667316773168731697317073171731727317373174731757317673177731787317973180731817318273183731847318573186731877318873189731907319173192731937319473195731967319773198731997320073201732027320373204732057320673207732087320973210732117321273213732147321573216732177321873219732207322173222732237322473225732267322773228732297323073231732327323373234732357323673237732387323973240732417324273243732447324573246732477324873249732507325173252732537325473255732567325773258732597326073261732627326373264732657326673267732687326973270732717327273273732747327573276732777327873279732807328173282732837328473285732867328773288732897329073291732927329373294732957329673297732987329973300733017330273303733047330573306733077330873309733107331173312733137331473315733167331773318733197332073321733227332373324733257332673327733287332973330733317333273333733347333573336733377333873339733407334173342733437334473345733467334773348733497335073351733527335373354733557335673357733587335973360733617336273363733647336573366733677336873369733707337173372733737337473375733767337773378733797338073381733827338373384733857338673387733887338973390733917339273393733947339573396733977339873399734007340173402734037340473405734067340773408734097341073411734127341373414734157341673417734187341973420734217342273423734247342573426734277342873429734307343173432734337343473435734367343773438734397344073441734427344373444734457344673447734487344973450734517345273453734547345573456734577345873459734607346173462734637346473465734667346773468734697347073471734727347373474734757347673477734787347973480734817348273483734847348573486734877348873489734907349173492734937349473495734967349773498734997350073501735027350373504735057350673507735087350973510735117351273513735147351573516735177351873519735207352173522735237352473525735267352773528735297353073531735327353373534735357353673537735387353973540735417354273543735447354573546735477354873549735507355173552735537355473555735567355773558735597356073561735627356373564735657356673567735687356973570735717357273573735747357573576735777357873579735807358173582735837358473585735867358773588735897359073591735927359373594735957359673597735987359973600736017360273603736047360573606736077360873609736107361173612736137361473615736167361773618736197362073621736227362373624736257362673627736287362973630736317363273633736347363573636736377363873639736407364173642736437364473645736467364773648736497365073651736527365373654736557365673657736587365973660736617366273663736647366573666736677366873669736707367173672736737367473675736767367773678736797368073681736827368373684736857368673687736887368973690736917369273693736947369573696736977369873699737007370173702737037370473705737067370773708737097371073711737127371373714737157371673717737187371973720737217372273723737247372573726737277372873729737307373173732737337373473735737367373773738737397374073741737427374373744737457374673747737487374973750737517375273753737547375573756737577375873759737607376173762737637376473765737667376773768737697377073771737727377373774737757377673777737787377973780737817378273783737847378573786737877378873789737907379173792737937379473795737967379773798737997380073801738027380373804738057380673807738087380973810738117381273813738147381573816738177381873819738207382173822738237382473825738267382773828738297383073831738327383373834738357383673837738387383973840738417384273843738447384573846738477384873849738507385173852738537385473855738567385773858738597386073861738627386373864738657386673867738687386973870738717387273873738747387573876738777387873879738807388173882738837388473885738867388773888738897389073891738927389373894738957389673897738987389973900739017390273903739047390573906739077390873909739107391173912739137391473915739167391773918739197392073921739227392373924739257392673927739287392973930739317393273933739347393573936739377393873939739407394173942739437394473945739467394773948739497395073951739527395373954739557395673957739587395973960739617396273963739647396573966739677396873969739707397173972739737397473975739767397773978739797398073981739827398373984739857398673987739887398973990739917399273993739947399573996739977399873999740007400174002740037400474005740067400774008740097401074011740127401374014740157401674017740187401974020740217402274023740247402574026740277402874029740307403174032740337403474035740367403774038740397404074041740427404374044740457404674047740487404974050740517405274053740547405574056740577405874059740607406174062740637406474065740667406774068740697407074071740727407374074740757407674077740787407974080740817408274083740847408574086740877408874089740907409174092740937409474095740967409774098740997410074101741027410374104741057410674107741087410974110741117411274113741147411574116741177411874119741207412174122741237412474125741267412774128741297413074131741327413374134741357413674137741387413974140741417414274143741447414574146741477414874149741507415174152741537415474155741567415774158741597416074161741627416374164741657416674167741687416974170741717417274173741747417574176741777417874179741807418174182741837418474185741867418774188741897419074191741927419374194741957419674197741987419974200742017420274203742047420574206742077420874209742107421174212742137421474215742167421774218742197422074221742227422374224742257422674227742287422974230742317423274233742347423574236742377423874239742407424174242742437424474245742467424774248742497425074251742527425374254742557425674257742587425974260742617426274263742647426574266742677426874269742707427174272742737427474275742767427774278742797428074281742827428374284742857428674287742887428974290742917429274293742947429574296742977429874299743007430174302743037430474305743067430774308743097431074311743127431374314743157431674317743187431974320743217432274323743247432574326743277432874329743307433174332743337433474335743367433774338743397434074341743427434374344743457434674347743487434974350743517435274353743547435574356743577435874359743607436174362743637436474365743667436774368743697437074371743727437374374743757437674377743787437974380743817438274383743847438574386743877438874389743907439174392743937439474395743967439774398743997440074401744027440374404744057440674407744087440974410744117441274413744147441574416744177441874419744207442174422744237442474425744267442774428744297443074431744327443374434744357443674437744387443974440744417444274443744447444574446744477444874449744507445174452744537445474455744567445774458744597446074461744627446374464744657446674467744687446974470744717447274473744747447574476744777447874479744807448174482744837448474485744867448774488744897449074491744927449374494744957449674497744987449974500745017450274503745047450574506745077450874509745107451174512745137451474515745167451774518745197452074521745227452374524745257452674527745287452974530745317453274533745347453574536745377453874539745407454174542745437454474545745467454774548745497455074551745527455374554745557455674557745587455974560745617456274563745647456574566745677456874569745707457174572745737457474575745767457774578745797458074581745827458374584745857458674587745887458974590745917459274593745947459574596745977459874599746007460174602746037460474605746067460774608746097461074611746127461374614746157461674617746187461974620746217462274623746247462574626746277462874629746307463174632746337463474635746367463774638746397464074641746427464374644746457464674647746487464974650746517465274653746547465574656746577465874659746607466174662746637466474665746667466774668746697467074671746727467374674746757467674677746787467974680746817468274683746847468574686746877468874689746907469174692746937469474695746967469774698746997470074701747027470374704747057470674707747087470974710747117471274713747147471574716747177471874719747207472174722747237472474725747267472774728747297473074731747327473374734747357473674737747387473974740747417474274743747447474574746747477474874749747507475174752747537475474755747567475774758747597476074761747627476374764747657476674767747687476974770747717477274773747747477574776747777477874779747807478174782747837478474785747867478774788747897479074791747927479374794747957479674797747987479974800748017480274803748047480574806748077480874809748107481174812748137481474815748167481774818748197482074821748227482374824748257482674827748287482974830748317483274833748347483574836748377483874839748407484174842748437484474845748467484774848748497485074851748527485374854748557485674857748587485974860748617486274863748647486574866748677486874869748707487174872748737487474875748767487774878748797488074881748827488374884748857488674887748887488974890748917489274893748947489574896748977489874899749007490174902749037490474905749067490774908749097491074911749127491374914749157491674917749187491974920749217492274923749247492574926749277492874929749307493174932749337493474935749367493774938749397494074941749427494374944749457494674947749487494974950749517495274953749547495574956749577495874959749607496174962749637496474965749667496774968749697497074971749727497374974749757497674977749787497974980749817498274983749847498574986749877498874989749907499174992749937499474995749967499774998749997500075001750027500375004750057500675007750087500975010750117501275013750147501575016750177501875019750207502175022750237502475025750267502775028750297503075031750327503375034750357503675037750387503975040750417504275043750447504575046750477504875049750507505175052750537505475055750567505775058750597506075061750627506375064750657506675067750687506975070750717507275073750747507575076750777507875079750807508175082750837508475085750867508775088750897509075091750927509375094750957509675097750987509975100751017510275103751047510575106751077510875109751107511175112751137511475115751167511775118751197512075121751227512375124751257512675127751287512975130751317513275133751347513575136751377513875139751407514175142751437514475145751467514775148751497515075151751527515375154751557515675157751587515975160751617516275163751647516575166751677516875169751707517175172751737517475175751767517775178751797518075181751827518375184751857518675187751887518975190751917519275193751947519575196751977519875199752007520175202752037520475205752067520775208752097521075211752127521375214752157521675217752187521975220752217522275223752247522575226752277522875229752307523175232752337523475235752367523775238752397524075241752427524375244752457524675247752487524975250752517525275253752547525575256752577525875259752607526175262752637526475265752667526775268752697527075271752727527375274752757527675277752787527975280752817528275283752847528575286752877528875289752907529175292752937529475295752967529775298752997530075301753027530375304753057530675307753087530975310753117531275313753147531575316753177531875319753207532175322753237532475325753267532775328753297533075331753327533375334753357533675337753387533975340753417534275343753447534575346753477534875349753507535175352753537535475355753567535775358753597536075361753627536375364753657536675367753687536975370753717537275373753747537575376753777537875379753807538175382753837538475385753867538775388753897539075391753927539375394753957539675397753987539975400754017540275403754047540575406754077540875409754107541175412754137541475415754167541775418754197542075421754227542375424754257542675427754287542975430754317543275433754347543575436754377543875439754407544175442754437544475445754467544775448754497545075451754527545375454754557545675457754587545975460754617546275463754647546575466754677546875469754707547175472754737547475475754767547775478754797548075481754827548375484754857548675487754887548975490754917549275493754947549575496754977549875499755007550175502755037550475505755067550775508755097551075511755127551375514755157551675517755187551975520755217552275523755247552575526755277552875529755307553175532755337553475535755367553775538755397554075541755427554375544755457554675547755487554975550755517555275553755547555575556755577555875559755607556175562755637556475565755667556775568755697557075571755727557375574755757557675577755787557975580755817558275583755847558575586755877558875589755907559175592755937559475595755967559775598755997560075601756027560375604756057560675607756087560975610756117561275613756147561575616756177561875619756207562175622756237562475625756267562775628756297563075631756327563375634756357563675637756387563975640756417564275643756447564575646756477564875649756507565175652756537565475655756567565775658756597566075661756627566375664756657566675667756687566975670756717567275673756747567575676756777567875679756807568175682756837568475685756867568775688756897569075691756927569375694756957569675697756987569975700757017570275703757047570575706757077570875709757107571175712757137571475715757167571775718757197572075721757227572375724757257572675727757287572975730757317573275733757347573575736757377573875739757407574175742757437574475745757467574775748757497575075751757527575375754757557575675757757587575975760757617576275763757647576575766757677576875769757707577175772757737577475775757767577775778757797578075781757827578375784757857578675787757887578975790757917579275793757947579575796757977579875799758007580175802758037580475805758067580775808758097581075811758127581375814758157581675817758187581975820758217582275823758247582575826758277582875829758307583175832758337583475835758367583775838758397584075841758427584375844758457584675847758487584975850758517585275853758547585575856758577585875859758607586175862758637586475865758667586775868758697587075871758727587375874758757587675877758787587975880758817588275883758847588575886758877588875889758907589175892758937589475895758967589775898758997590075901759027590375904759057590675907759087590975910759117591275913759147591575916759177591875919759207592175922759237592475925759267592775928759297593075931759327593375934759357593675937759387593975940759417594275943759447594575946759477594875949759507595175952759537595475955759567595775958759597596075961759627596375964759657596675967759687596975970759717597275973759747597575976759777597875979759807598175982759837598475985759867598775988759897599075991759927599375994759957599675997759987599976000760017600276003760047600576006760077600876009760107601176012760137601476015760167601776018760197602076021760227602376024760257602676027760287602976030760317603276033760347603576036760377603876039760407604176042760437604476045760467604776048760497605076051760527605376054760557605676057760587605976060760617606276063760647606576066760677606876069760707607176072760737607476075760767607776078760797608076081760827608376084760857608676087760887608976090760917609276093760947609576096760977609876099761007610176102761037610476105761067610776108761097611076111761127611376114761157611676117761187611976120761217612276123761247612576126761277612876129761307613176132761337613476135761367613776138761397614076141761427614376144761457614676147761487614976150761517615276153761547615576156761577615876159761607616176162761637616476165761667616776168761697617076171761727617376174761757617676177761787617976180761817618276183761847618576186761877618876189761907619176192761937619476195761967619776198761997620076201762027620376204762057620676207762087620976210762117621276213762147621576216762177621876219762207622176222762237622476225762267622776228762297623076231762327623376234762357623676237762387623976240762417624276243762447624576246762477624876249762507625176252762537625476255762567625776258762597626076261762627626376264762657626676267762687626976270762717627276273762747627576276762777627876279762807628176282762837628476285762867628776288762897629076291762927629376294762957629676297762987629976300763017630276303763047630576306763077630876309763107631176312763137631476315763167631776318763197632076321763227632376324763257632676327763287632976330763317633276333763347633576336763377633876339763407634176342763437634476345763467634776348763497635076351763527635376354763557635676357763587635976360763617636276363763647636576366763677636876369763707637176372763737637476375763767637776378763797638076381763827638376384763857638676387763887638976390763917639276393763947639576396763977639876399764007640176402764037640476405764067640776408764097641076411764127641376414764157641676417764187641976420764217642276423764247642576426764277642876429764307643176432764337643476435764367643776438764397644076441764427644376444764457644676447764487644976450764517645276453764547645576456764577645876459764607646176462764637646476465764667646776468764697647076471764727647376474764757647676477764787647976480764817648276483764847648576486764877648876489764907649176492764937649476495764967649776498764997650076501765027650376504765057650676507765087650976510765117651276513765147651576516765177651876519765207652176522765237652476525765267652776528765297653076531765327653376534765357653676537765387653976540765417654276543765447654576546765477654876549765507655176552765537655476555765567655776558765597656076561765627656376564765657656676567765687656976570765717657276573765747657576576765777657876579765807658176582765837658476585765867658776588765897659076591765927659376594765957659676597765987659976600766017660276603766047660576606766077660876609766107661176612766137661476615766167661776618766197662076621766227662376624766257662676627766287662976630766317663276633766347663576636766377663876639766407664176642766437664476645766467664776648766497665076651766527665376654766557665676657766587665976660766617666276663766647666576666766677666876669766707667176672766737667476675766767667776678766797668076681766827668376684766857668676687766887668976690766917669276693766947669576696766977669876699767007670176702767037670476705767067670776708767097671076711767127671376714767157671676717767187671976720767217672276723767247672576726767277672876729767307673176732767337673476735767367673776738767397674076741767427674376744767457674676747767487674976750767517675276753767547675576756767577675876759767607676176762767637676476765767667676776768767697677076771767727677376774767757677676777767787677976780767817678276783767847678576786767877678876789767907679176792767937679476795767967679776798767997680076801768027680376804768057680676807768087680976810768117681276813768147681576816768177681876819768207682176822768237682476825768267682776828768297683076831768327683376834768357683676837768387683976840768417684276843768447684576846768477684876849768507685176852768537685476855768567685776858768597686076861768627686376864768657686676867768687686976870768717687276873768747687576876768777687876879768807688176882768837688476885768867688776888768897689076891768927689376894768957689676897768987689976900769017690276903769047690576906769077690876909769107691176912769137691476915769167691776918769197692076921769227692376924769257692676927769287692976930769317693276933769347693576936769377693876939769407694176942769437694476945769467694776948769497695076951769527695376954769557695676957769587695976960769617696276963769647696576966769677696876969769707697176972769737697476975769767697776978769797698076981769827698376984769857698676987769887698976990769917699276993769947699576996769977699876999770007700177002770037700477005770067700777008770097701077011770127701377014770157701677017770187701977020770217702277023770247702577026770277702877029770307703177032770337703477035770367703777038770397704077041770427704377044770457704677047770487704977050770517705277053770547705577056770577705877059770607706177062770637706477065770667706777068770697707077071770727707377074770757707677077770787707977080770817708277083770847708577086770877708877089770907709177092770937709477095770967709777098770997710077101771027710377104771057710677107771087710977110771117711277113771147711577116771177711877119771207712177122771237712477125771267712777128771297713077131771327713377134771357713677137771387713977140771417714277143771447714577146771477714877149771507715177152771537715477155771567715777158771597716077161771627716377164771657716677167771687716977170771717717277173771747717577176771777717877179771807718177182771837718477185771867718777188771897719077191771927719377194771957719677197771987719977200772017720277203772047720577206772077720877209772107721177212772137721477215772167721777218772197722077221772227722377224772257722677227772287722977230772317723277233772347723577236772377723877239772407724177242772437724477245772467724777248772497725077251772527725377254772557725677257772587725977260772617726277263772647726577266772677726877269772707727177272772737727477275772767727777278772797728077281772827728377284772857728677287772887728977290772917729277293772947729577296772977729877299773007730177302773037730477305773067730777308773097731077311773127731377314773157731677317773187731977320773217732277323773247732577326773277732877329773307733177332773337733477335773367733777338773397734077341773427734377344773457734677347773487734977350773517735277353773547735577356773577735877359773607736177362773637736477365773667736777368773697737077371773727737377374773757737677377773787737977380773817738277383773847738577386773877738877389773907739177392773937739477395773967739777398773997740077401774027740377404774057740677407774087740977410774117741277413774147741577416774177741877419774207742177422774237742477425774267742777428774297743077431774327743377434774357743677437774387743977440774417744277443774447744577446774477744877449774507745177452774537745477455774567745777458774597746077461774627746377464774657746677467774687746977470774717747277473774747747577476774777747877479774807748177482774837748477485774867748777488774897749077491774927749377494774957749677497774987749977500775017750277503775047750577506775077750877509775107751177512775137751477515775167751777518775197752077521775227752377524775257752677527775287752977530775317753277533775347753577536775377753877539775407754177542775437754477545775467754777548775497755077551775527755377554775557755677557775587755977560775617756277563775647756577566775677756877569775707757177572775737757477575775767757777578775797758077581775827758377584775857758677587775887758977590775917759277593775947759577596775977759877599776007760177602776037760477605776067760777608776097761077611776127761377614776157761677617776187761977620776217762277623776247762577626776277762877629776307763177632776337763477635776367763777638776397764077641776427764377644776457764677647776487764977650776517765277653776547765577656776577765877659776607766177662776637766477665776667766777668776697767077671776727767377674776757767677677776787767977680776817768277683776847768577686776877768877689776907769177692776937769477695776967769777698776997770077701777027770377704777057770677707777087770977710777117771277713777147771577716777177771877719777207772177722777237772477725777267772777728777297773077731777327773377734777357773677737777387773977740777417774277743777447774577746777477774877749777507775177752777537775477755777567775777758777597776077761777627776377764777657776677767777687776977770777717777277773777747777577776777777777877779777807778177782777837778477785777867778777788777897779077791777927779377794777957779677797777987779977800778017780277803778047780577806778077780877809778107781177812778137781477815778167781777818778197782077821778227782377824778257782677827778287782977830778317783277833778347783577836778377783877839778407784177842778437784477845778467784777848778497785077851778527785377854778557785677857778587785977860778617786277863778647786577866778677786877869778707787177872778737787477875778767787777878778797788077881778827788377884778857788677887778887788977890778917789277893778947789577896778977789877899779007790177902779037790477905779067790777908779097791077911779127791377914779157791677917779187791977920779217792277923779247792577926779277792877929779307793177932779337793477935779367793777938779397794077941779427794377944779457794677947779487794977950779517795277953779547795577956779577795877959779607796177962779637796477965779667796777968779697797077971779727797377974779757797677977779787797977980779817798277983779847798577986779877798877989779907799177992779937799477995779967799777998779997800078001780027800378004780057800678007780087800978010780117801278013780147801578016780177801878019780207802178022780237802478025780267802778028780297803078031780327803378034780357803678037780387803978040780417804278043780447804578046780477804878049780507805178052780537805478055780567805778058780597806078061780627806378064780657806678067780687806978070780717807278073780747807578076780777807878079780807808178082780837808478085780867808778088780897809078091780927809378094780957809678097780987809978100781017810278103781047810578106781077810878109781107811178112781137811478115781167811778118781197812078121781227812378124781257812678127781287812978130781317813278133781347813578136781377813878139781407814178142781437814478145781467814778148781497815078151781527815378154781557815678157781587815978160781617816278163781647816578166781677816878169781707817178172781737817478175781767817778178781797818078181781827818378184781857818678187781887818978190781917819278193781947819578196781977819878199782007820178202782037820478205782067820778208782097821078211782127821378214782157821678217782187821978220782217822278223782247822578226782277822878229782307823178232782337823478235782367823778238782397824078241782427824378244782457824678247782487824978250782517825278253782547825578256782577825878259782607826178262782637826478265782667826778268782697827078271782727827378274782757827678277782787827978280782817828278283782847828578286782877828878289782907829178292782937829478295782967829778298782997830078301783027830378304783057830678307783087830978310783117831278313783147831578316783177831878319783207832178322783237832478325783267832778328783297833078331783327833378334783357833678337783387833978340783417834278343783447834578346783477834878349783507835178352783537835478355783567835778358783597836078361783627836378364783657836678367783687836978370783717837278373783747837578376783777837878379783807838178382783837838478385783867838778388783897839078391783927839378394783957839678397783987839978400784017840278403784047840578406784077840878409784107841178412784137841478415784167841778418784197842078421784227842378424784257842678427784287842978430784317843278433784347843578436784377843878439784407844178442784437844478445784467844778448784497845078451784527845378454784557845678457784587845978460784617846278463784647846578466784677846878469784707847178472784737847478475784767847778478784797848078481784827848378484784857848678487784887848978490784917849278493784947849578496784977849878499785007850178502785037850478505785067850778508785097851078511785127851378514785157851678517785187851978520785217852278523785247852578526785277852878529785307853178532785337853478535785367853778538785397854078541785427854378544785457854678547785487854978550785517855278553785547855578556785577855878559785607856178562785637856478565785667856778568785697857078571785727857378574785757857678577785787857978580785817858278583785847858578586785877858878589785907859178592785937859478595785967859778598785997860078601786027860378604786057860678607786087860978610786117861278613786147861578616786177861878619786207862178622786237862478625786267862778628786297863078631786327863378634786357863678637786387863978640786417864278643786447864578646786477864878649786507865178652786537865478655786567865778658786597866078661786627866378664786657866678667786687866978670786717867278673786747867578676786777867878679786807868178682786837868478685786867868778688786897869078691786927869378694786957869678697786987869978700787017870278703787047870578706787077870878709787107871178712787137871478715787167871778718787197872078721787227872378724787257872678727787287872978730787317873278733787347873578736787377873878739787407874178742787437874478745787467874778748787497875078751787527875378754787557875678757787587875978760787617876278763787647876578766787677876878769787707877178772787737877478775787767877778778787797878078781787827878378784787857878678787787887878978790787917879278793787947879578796787977879878799788007880178802788037880478805788067880778808788097881078811788127881378814788157881678817788187881978820788217882278823788247882578826788277882878829788307883178832788337883478835788367883778838788397884078841788427884378844788457884678847788487884978850788517885278853788547885578856788577885878859788607886178862788637886478865788667886778868788697887078871788727887378874788757887678877788787887978880788817888278883788847888578886788877888878889788907889178892788937889478895788967889778898788997890078901789027890378904789057890678907789087890978910789117891278913789147891578916789177891878919789207892178922789237892478925789267892778928789297893078931789327893378934789357893678937789387893978940789417894278943789447894578946789477894878949789507895178952789537895478955789567895778958789597896078961789627896378964789657896678967789687896978970789717897278973789747897578976789777897878979789807898178982789837898478985789867898778988789897899078991789927899378994789957899678997789987899979000790017900279003790047900579006790077900879009790107901179012790137901479015790167901779018790197902079021790227902379024790257902679027790287902979030790317903279033790347903579036790377903879039790407904179042790437904479045790467904779048790497905079051790527905379054790557905679057790587905979060790617906279063790647906579066790677906879069790707907179072790737907479075790767907779078790797908079081790827908379084790857908679087790887908979090790917909279093790947909579096790977909879099791007910179102791037910479105791067910779108791097911079111791127911379114791157911679117791187911979120791217912279123791247912579126791277912879129791307913179132791337913479135791367913779138791397914079141791427914379144791457914679147791487914979150791517915279153791547915579156791577915879159791607916179162791637916479165791667916779168791697917079171791727917379174791757917679177791787917979180791817918279183791847918579186791877918879189791907919179192791937919479195791967919779198791997920079201792027920379204792057920679207792087920979210792117921279213792147921579216792177921879219792207922179222792237922479225792267922779228792297923079231792327923379234792357923679237792387923979240792417924279243792447924579246792477924879249792507925179252792537925479255792567925779258792597926079261792627926379264792657926679267792687926979270792717927279273792747927579276792777927879279792807928179282792837928479285792867928779288792897929079291792927929379294792957929679297792987929979300793017930279303793047930579306793077930879309793107931179312793137931479315793167931779318793197932079321793227932379324793257932679327793287932979330793317933279333793347933579336793377933879339793407934179342793437934479345793467934779348793497935079351793527935379354793557935679357793587935979360793617936279363793647936579366793677936879369793707937179372793737937479375793767937779378793797938079381793827938379384793857938679387793887938979390793917939279393793947939579396793977939879399794007940179402794037940479405794067940779408794097941079411794127941379414794157941679417794187941979420794217942279423794247942579426794277942879429794307943179432794337943479435794367943779438794397944079441794427944379444794457944679447794487944979450794517945279453794547945579456794577945879459794607946179462794637946479465794667946779468794697947079471794727947379474794757947679477794787947979480794817948279483794847948579486794877948879489794907949179492794937949479495794967949779498794997950079501795027950379504795057950679507795087950979510795117951279513795147951579516795177951879519795207952179522795237952479525795267952779528795297953079531795327953379534795357953679537795387953979540795417954279543795447954579546795477954879549795507955179552795537955479555795567955779558795597956079561795627956379564795657956679567795687956979570795717957279573795747957579576795777957879579795807958179582795837958479585795867958779588795897959079591795927959379594795957959679597795987959979600796017960279603796047960579606796077960879609796107961179612796137961479615796167961779618796197962079621796227962379624796257962679627796287962979630796317963279633796347963579636796377963879639796407964179642796437964479645796467964779648796497965079651796527965379654796557965679657796587965979660796617966279663796647966579666796677966879669796707967179672796737967479675796767967779678796797968079681796827968379684796857968679687796887968979690796917969279693796947969579696796977969879699797007970179702797037970479705797067970779708797097971079711797127971379714797157971679717797187971979720797217972279723797247972579726797277972879729797307973179732797337973479735797367973779738797397974079741797427974379744797457974679747797487974979750797517975279753797547975579756797577975879759797607976179762797637976479765797667976779768797697977079771797727977379774797757977679777797787977979780797817978279783797847978579786797877978879789797907979179792797937979479795797967979779798797997980079801798027980379804798057980679807798087980979810798117981279813798147981579816798177981879819798207982179822798237982479825798267982779828798297983079831798327983379834798357983679837798387983979840798417984279843798447984579846798477984879849798507985179852798537985479855798567985779858798597986079861798627986379864798657986679867798687986979870798717987279873798747987579876798777987879879798807988179882798837988479885798867988779888798897989079891798927989379894798957989679897798987989979900799017990279903799047990579906799077990879909799107991179912799137991479915799167991779918799197992079921799227992379924799257992679927799287992979930799317993279933799347993579936799377993879939799407994179942799437994479945799467994779948799497995079951799527995379954799557995679957799587995979960799617996279963799647996579966799677996879969799707997179972799737997479975799767997779978799797998079981799827998379984799857998679987799887998979990799917999279993799947999579996799977999879999800008000180002800038000480005800068000780008800098001080011800128001380014800158001680017800188001980020800218002280023800248002580026800278002880029800308003180032800338003480035800368003780038800398004080041800428004380044800458004680047800488004980050800518005280053800548005580056800578005880059800608006180062800638006480065800668006780068800698007080071800728007380074800758007680077800788007980080800818008280083800848008580086800878008880089800908009180092800938009480095800968009780098800998010080101801028010380104801058010680107801088010980110801118011280113801148011580116801178011880119801208012180122801238012480125801268012780128801298013080131801328013380134801358013680137801388013980140801418014280143801448014580146801478014880149801508015180152801538015480155801568015780158801598016080161801628016380164801658016680167801688016980170801718017280173801748017580176801778017880179801808018180182801838018480185801868018780188801898019080191801928019380194801958019680197801988019980200802018020280203802048020580206802078020880209802108021180212802138021480215802168021780218802198022080221802228022380224802258022680227802288022980230802318023280233802348023580236802378023880239802408024180242802438024480245802468024780248802498025080251802528025380254802558025680257802588025980260802618026280263802648026580266802678026880269802708027180272802738027480275802768027780278802798028080281802828028380284802858028680287802888028980290802918029280293802948029580296802978029880299803008030180302803038030480305803068030780308803098031080311803128031380314803158031680317803188031980320803218032280323803248032580326803278032880329803308033180332803338033480335803368033780338803398034080341803428034380344803458034680347803488034980350803518035280353803548035580356803578035880359803608036180362803638036480365803668036780368803698037080371803728037380374803758037680377803788037980380803818038280383803848038580386803878038880389803908039180392803938039480395803968039780398803998040080401804028040380404804058040680407804088040980410804118041280413804148041580416804178041880419804208042180422804238042480425804268042780428804298043080431804328043380434804358043680437804388043980440804418044280443804448044580446804478044880449804508045180452804538045480455804568045780458804598046080461804628046380464804658046680467804688046980470804718047280473804748047580476804778047880479804808048180482804838048480485804868048780488804898049080491804928049380494804958049680497804988049980500805018050280503805048050580506805078050880509805108051180512805138051480515805168051780518805198052080521805228052380524805258052680527805288052980530805318053280533805348053580536805378053880539805408054180542805438054480545805468054780548805498055080551805528055380554805558055680557805588055980560805618056280563805648056580566805678056880569805708057180572805738057480575805768057780578805798058080581805828058380584805858058680587805888058980590805918059280593805948059580596805978059880599806008060180602806038060480605806068060780608806098061080611806128061380614806158061680617806188061980620806218062280623806248062580626806278062880629806308063180632806338063480635806368063780638806398064080641806428064380644806458064680647806488064980650806518065280653806548065580656806578065880659806608066180662806638066480665806668066780668806698067080671806728067380674806758067680677806788067980680806818068280683806848068580686806878068880689806908069180692806938069480695806968069780698806998070080701807028070380704807058070680707807088070980710807118071280713807148071580716807178071880719807208072180722807238072480725807268072780728807298073080731807328073380734807358073680737807388073980740807418074280743807448074580746807478074880749807508075180752807538075480755807568075780758807598076080761807628076380764807658076680767807688076980770807718077280773807748077580776807778077880779807808078180782807838078480785807868078780788807898079080791807928079380794807958079680797807988079980800808018080280803808048080580806808078080880809808108081180812808138081480815808168081780818808198082080821808228082380824808258082680827808288082980830808318083280833808348083580836808378083880839808408084180842808438084480845808468084780848808498085080851808528085380854808558085680857808588085980860808618086280863808648086580866808678086880869808708087180872808738087480875808768087780878808798088080881808828088380884808858088680887808888088980890808918089280893808948089580896808978089880899809008090180902809038090480905809068090780908809098091080911809128091380914809158091680917809188091980920809218092280923809248092580926809278092880929809308093180932809338093480935809368093780938809398094080941809428094380944809458094680947809488094980950809518095280953809548095580956809578095880959809608096180962809638096480965809668096780968809698097080971809728097380974809758097680977809788097980980809818098280983809848098580986809878098880989809908099180992809938099480995809968099780998809998100081001810028100381004810058100681007810088100981010810118101281013810148101581016810178101881019810208102181022810238102481025810268102781028810298103081031810328103381034810358103681037810388103981040810418104281043810448104581046810478104881049810508105181052810538105481055810568105781058810598106081061810628106381064810658106681067810688106981070810718107281073810748107581076810778107881079810808108181082810838108481085810868108781088810898109081091810928109381094810958109681097810988109981100811018110281103811048110581106811078110881109811108111181112811138111481115811168111781118811198112081121811228112381124811258112681127811288112981130811318113281133811348113581136811378113881139811408114181142811438114481145811468114781148811498115081151811528115381154811558115681157811588115981160811618116281163811648116581166811678116881169811708117181172811738117481175811768117781178811798118081181811828118381184811858118681187811888118981190811918119281193811948119581196811978119881199812008120181202812038120481205812068120781208812098121081211812128121381214812158121681217812188121981220812218122281223812248122581226812278122881229812308123181232812338123481235812368123781238812398124081241812428124381244812458124681247812488124981250812518125281253812548125581256812578125881259812608126181262812638126481265812668126781268812698127081271812728127381274812758127681277812788127981280812818128281283812848128581286812878128881289812908129181292812938129481295812968129781298812998130081301813028130381304813058130681307813088130981310813118131281313813148131581316813178131881319813208132181322813238132481325813268132781328813298133081331813328133381334813358133681337813388133981340813418134281343813448134581346813478134881349813508135181352813538135481355813568135781358813598136081361813628136381364813658136681367813688136981370813718137281373813748137581376813778137881379813808138181382813838138481385813868138781388813898139081391813928139381394813958139681397813988139981400814018140281403814048140581406814078140881409814108141181412814138141481415814168141781418814198142081421814228142381424814258142681427814288142981430814318143281433814348143581436814378143881439814408144181442814438144481445814468144781448814498145081451814528145381454814558145681457814588145981460814618146281463814648146581466814678146881469814708147181472814738147481475814768147781478814798148081481814828148381484814858148681487814888148981490814918149281493814948149581496814978149881499815008150181502815038150481505815068150781508815098151081511815128151381514815158151681517815188151981520815218152281523815248152581526815278152881529815308153181532815338153481535815368153781538815398154081541815428154381544815458154681547815488154981550815518155281553815548155581556815578155881559815608156181562815638156481565815668156781568815698157081571815728157381574815758157681577815788157981580815818158281583815848158581586815878158881589815908159181592815938159481595815968159781598815998160081601816028160381604816058160681607816088160981610816118161281613816148161581616816178161881619816208162181622816238162481625816268162781628816298163081631816328163381634816358163681637816388163981640816418164281643816448164581646816478164881649816508165181652816538165481655816568165781658816598166081661816628166381664816658166681667816688166981670816718167281673816748167581676816778167881679816808168181682816838168481685816868168781688816898169081691816928169381694816958169681697816988169981700817018170281703817048170581706817078170881709817108171181712817138171481715817168171781718817198172081721817228172381724817258172681727817288172981730817318173281733817348173581736817378173881739817408174181742817438174481745817468174781748817498175081751817528175381754817558175681757817588175981760817618176281763817648176581766817678176881769817708177181772817738177481775817768177781778817798178081781817828178381784817858178681787817888178981790817918179281793817948179581796817978179881799818008180181802818038180481805818068180781808818098181081811818128181381814818158181681817818188181981820818218182281823818248182581826818278182881829818308183181832818338183481835818368183781838818398184081841818428184381844818458184681847818488184981850818518185281853818548185581856818578185881859818608186181862818638186481865818668186781868818698187081871818728187381874818758187681877818788187981880818818188281883818848188581886818878188881889818908189181892818938189481895818968189781898818998190081901819028190381904819058190681907819088190981910819118191281913819148191581916819178191881919819208192181922819238192481925819268192781928819298193081931819328193381934819358193681937819388193981940819418194281943819448194581946819478194881949819508195181952819538195481955819568195781958819598196081961819628196381964819658196681967819688196981970819718197281973819748197581976819778197881979819808198181982819838198481985819868198781988819898199081991819928199381994819958199681997819988199982000820018200282003820048200582006820078200882009820108201182012820138201482015820168201782018820198202082021820228202382024820258202682027820288202982030820318203282033820348203582036820378203882039820408204182042820438204482045820468204782048820498205082051820528205382054820558205682057820588205982060820618206282063820648206582066820678206882069820708207182072820738207482075820768207782078820798208082081820828208382084820858208682087820888208982090820918209282093820948209582096820978209882099821008210182102821038210482105821068210782108821098211082111821128211382114821158211682117821188211982120821218212282123821248212582126821278212882129821308213182132821338213482135821368213782138821398214082141821428214382144821458214682147821488214982150821518215282153821548215582156821578215882159821608216182162821638216482165821668216782168821698217082171821728217382174821758217682177821788217982180821818218282183821848218582186821878218882189821908219182192821938219482195821968219782198821998220082201822028220382204822058220682207822088220982210822118221282213822148221582216822178221882219822208222182222822238222482225822268222782228822298223082231822328223382234822358223682237822388223982240822418224282243822448224582246822478224882249822508225182252822538225482255822568225782258822598226082261822628226382264822658226682267822688226982270822718227282273822748227582276822778227882279822808228182282822838228482285822868228782288822898229082291822928229382294822958229682297822988229982300823018230282303823048230582306823078230882309823108231182312823138231482315823168231782318823198232082321823228232382324823258232682327823288232982330823318233282333823348233582336823378233882339823408234182342823438234482345823468234782348823498235082351823528235382354823558235682357823588235982360823618236282363823648236582366823678236882369823708237182372823738237482375823768237782378823798238082381823828238382384823858238682387823888238982390823918239282393823948239582396823978239882399824008240182402824038240482405824068240782408824098241082411824128241382414824158241682417824188241982420824218242282423824248242582426824278242882429824308243182432824338243482435824368243782438824398244082441824428244382444824458244682447824488244982450824518245282453824548245582456824578245882459824608246182462824638246482465824668246782468824698247082471824728247382474824758247682477824788247982480824818248282483824848248582486824878248882489824908249182492824938249482495824968249782498824998250082501825028250382504825058250682507825088250982510825118251282513825148251582516825178251882519825208252182522825238252482525825268252782528825298253082531825328253382534825358253682537825388253982540825418254282543825448254582546825478254882549825508255182552825538255482555825568255782558825598256082561825628256382564825658256682567825688256982570825718257282573825748257582576825778257882579825808258182582825838258482585825868258782588825898259082591825928259382594825958259682597825988259982600826018260282603826048260582606826078260882609826108261182612826138261482615826168261782618826198262082621826228262382624826258262682627826288262982630826318263282633826348263582636826378263882639826408264182642826438264482645826468264782648826498265082651826528265382654826558265682657826588265982660826618266282663826648266582666826678266882669826708267182672826738267482675826768267782678826798268082681826828268382684826858268682687826888268982690826918269282693826948269582696826978269882699827008270182702827038270482705827068270782708827098271082711827128271382714827158271682717827188271982720827218272282723827248272582726827278272882729827308273182732827338273482735827368273782738827398274082741827428274382744827458274682747827488274982750827518275282753827548275582756827578275882759827608276182762827638276482765827668276782768827698277082771827728277382774827758277682777827788277982780827818278282783827848278582786827878278882789827908279182792827938279482795827968279782798827998280082801828028280382804828058280682807828088280982810828118281282813828148281582816828178281882819828208282182822828238282482825828268282782828828298283082831828328283382834828358283682837828388283982840828418284282843828448284582846828478284882849828508285182852828538285482855828568285782858828598286082861828628286382864828658286682867828688286982870828718287282873828748287582876828778287882879828808288182882828838288482885828868288782888828898289082891828928289382894828958289682897828988289982900829018290282903829048290582906829078290882909829108291182912829138291482915829168291782918829198292082921829228292382924829258292682927829288292982930829318293282933829348293582936829378293882939829408294182942829438294482945829468294782948829498295082951829528295382954829558295682957829588295982960829618296282963829648296582966829678296882969829708297182972829738297482975829768297782978829798298082981829828298382984829858298682987829888298982990829918299282993829948299582996829978299882999830008300183002830038300483005830068300783008830098301083011830128301383014830158301683017830188301983020830218302283023830248302583026830278302883029830308303183032830338303483035830368303783038830398304083041830428304383044830458304683047830488304983050830518305283053830548305583056830578305883059830608306183062830638306483065830668306783068830698307083071830728307383074830758307683077830788307983080830818308283083830848308583086830878308883089830908309183092830938309483095830968309783098830998310083101831028310383104831058310683107831088310983110831118311283113831148311583116831178311883119831208312183122831238312483125831268312783128831298313083131831328313383134831358313683137831388313983140831418314283143831448314583146831478314883149831508315183152831538315483155831568315783158831598316083161831628316383164831658316683167831688316983170831718317283173831748317583176831778317883179831808318183182831838318483185831868318783188831898319083191831928319383194831958319683197831988319983200832018320283203832048320583206832078320883209832108321183212832138321483215832168321783218832198322083221832228322383224832258322683227832288322983230832318323283233832348323583236832378323883239832408324183242832438324483245832468324783248832498325083251832528325383254832558325683257832588325983260832618326283263832648326583266832678326883269832708327183272832738327483275832768327783278832798328083281832828328383284832858328683287832888328983290832918329283293832948329583296832978329883299833008330183302833038330483305833068330783308833098331083311833128331383314833158331683317833188331983320833218332283323833248332583326833278332883329833308333183332833338333483335833368333783338833398334083341833428334383344833458334683347833488334983350833518335283353833548335583356833578335883359833608336183362833638336483365833668336783368833698337083371833728337383374833758337683377833788337983380833818338283383833848338583386833878338883389833908339183392833938339483395833968339783398833998340083401834028340383404834058340683407834088340983410834118341283413834148341583416834178341883419834208342183422834238342483425834268342783428834298343083431834328343383434834358343683437834388343983440834418344283443834448344583446834478344883449834508345183452834538345483455834568345783458834598346083461834628346383464834658346683467834688346983470834718347283473834748347583476834778347883479834808348183482834838348483485834868348783488834898349083491834928349383494834958349683497834988349983500835018350283503835048350583506835078350883509835108351183512835138351483515835168351783518835198352083521835228352383524835258352683527835288352983530835318353283533835348353583536835378353883539835408354183542835438354483545835468354783548835498355083551835528355383554835558355683557835588355983560835618356283563835648356583566835678356883569835708357183572835738357483575835768357783578835798358083581835828358383584835858358683587835888358983590835918359283593835948359583596835978359883599836008360183602836038360483605836068360783608836098361083611836128361383614836158361683617836188361983620836218362283623836248362583626836278362883629836308363183632836338363483635836368363783638836398364083641836428364383644836458364683647836488364983650836518365283653836548365583656836578365883659836608366183662836638366483665836668366783668836698367083671836728367383674836758367683677836788367983680836818368283683836848368583686836878368883689836908369183692836938369483695836968369783698836998370083701837028370383704837058370683707837088370983710837118371283713837148371583716837178371883719837208372183722837238372483725837268372783728837298373083731837328373383734837358373683737837388373983740837418374283743837448374583746837478374883749837508375183752837538375483755837568375783758837598376083761837628376383764837658376683767837688376983770837718377283773837748377583776837778377883779837808378183782837838378483785837868378783788837898379083791837928379383794837958379683797837988379983800838018380283803838048380583806838078380883809838108381183812838138381483815838168381783818838198382083821838228382383824838258382683827838288382983830838318383283833838348383583836838378383883839838408384183842838438384483845838468384783848838498385083851838528385383854838558385683857838588385983860838618386283863838648386583866838678386883869838708387183872838738387483875838768387783878838798388083881838828388383884838858388683887838888388983890838918389283893838948389583896838978389883899839008390183902839038390483905839068390783908839098391083911839128391383914839158391683917839188391983920839218392283923839248392583926839278392883929839308393183932839338393483935839368393783938839398394083941839428394383944839458394683947839488394983950839518395283953839548395583956839578395883959839608396183962839638396483965839668396783968839698397083971839728397383974839758397683977839788397983980839818398283983839848398583986839878398883989839908399183992839938399483995839968399783998839998400084001840028400384004840058400684007840088400984010840118401284013840148401584016840178401884019840208402184022840238402484025840268402784028840298403084031840328403384034840358403684037840388403984040840418404284043840448404584046840478404884049840508405184052840538405484055840568405784058840598406084061840628406384064840658406684067840688406984070840718407284073840748407584076840778407884079840808408184082840838408484085840868408784088840898409084091840928409384094840958409684097840988409984100841018410284103841048410584106841078410884109841108411184112841138411484115841168411784118841198412084121841228412384124841258412684127841288412984130841318413284133841348413584136841378413884139841408414184142841438414484145841468414784148841498415084151841528415384154841558415684157841588415984160841618416284163841648416584166841678416884169841708417184172841738417484175841768417784178841798418084181841828418384184841858418684187841888418984190841918419284193841948419584196841978419884199842008420184202842038420484205842068420784208842098421084211842128421384214842158421684217842188421984220842218422284223842248422584226842278422884229842308423184232842338423484235842368423784238842398424084241842428424384244842458424684247842488424984250842518425284253842548425584256842578425884259842608426184262842638426484265842668426784268842698427084271842728427384274842758427684277842788427984280842818428284283842848428584286842878428884289842908429184292842938429484295842968429784298842998430084301843028430384304843058430684307843088430984310843118431284313843148431584316843178431884319843208432184322843238432484325843268432784328843298433084331843328433384334843358433684337843388433984340843418434284343843448434584346843478434884349843508435184352843538435484355843568435784358843598436084361843628436384364843658436684367843688436984370843718437284373843748437584376843778437884379843808438184382843838438484385843868438784388843898439084391843928439384394843958439684397843988439984400844018440284403844048440584406844078440884409844108441184412844138441484415844168441784418844198442084421844228442384424844258442684427844288442984430844318443284433844348443584436844378443884439844408444184442844438444484445844468444784448844498445084451844528445384454844558445684457844588445984460844618446284463844648446584466844678446884469844708447184472844738447484475844768447784478844798448084481844828448384484844858448684487844888448984490844918449284493844948449584496844978449884499845008450184502845038450484505845068450784508845098451084511845128451384514845158451684517845188451984520845218452284523845248452584526845278452884529845308453184532845338453484535845368453784538845398454084541845428454384544845458454684547845488454984550845518455284553845548455584556845578455884559845608456184562845638456484565845668456784568845698457084571845728457384574845758457684577845788457984580845818458284583845848458584586845878458884589845908459184592845938459484595845968459784598845998460084601846028460384604846058460684607846088460984610846118461284613846148461584616846178461884619846208462184622846238462484625846268462784628846298463084631846328463384634846358463684637846388463984640846418464284643846448464584646846478464884649846508465184652846538465484655846568465784658846598466084661846628466384664846658466684667846688466984670846718467284673846748467584676846778467884679846808468184682846838468484685846868468784688846898469084691846928469384694846958469684697846988469984700847018470284703847048470584706847078470884709847108471184712847138471484715847168471784718847198472084721847228472384724847258472684727847288472984730847318473284733847348473584736847378473884739847408474184742847438474484745847468474784748847498475084751847528475384754847558475684757847588475984760847618476284763847648476584766847678476884769847708477184772847738477484775847768477784778847798478084781847828478384784847858478684787847888478984790847918479284793847948479584796847978479884799848008480184802848038480484805848068480784808848098481084811848128481384814848158481684817848188481984820848218482284823848248482584826848278482884829848308483184832848338483484835848368483784838848398484084841848428484384844848458484684847848488484984850848518485284853848548485584856848578485884859848608486184862848638486484865848668486784868848698487084871848728487384874848758487684877848788487984880848818488284883848848488584886848878488884889848908489184892848938489484895848968489784898848998490084901849028490384904849058490684907849088490984910849118491284913849148491584916849178491884919849208492184922849238492484925849268492784928849298493084931849328493384934849358493684937849388493984940849418494284943849448494584946849478494884949849508495184952849538495484955849568495784958849598496084961849628496384964849658496684967849688496984970849718497284973849748497584976849778497884979849808498184982849838498484985849868498784988849898499084991849928499384994849958499684997849988499985000850018500285003850048500585006850078500885009850108501185012850138501485015850168501785018850198502085021850228502385024850258502685027850288502985030850318503285033850348503585036850378503885039850408504185042850438504485045850468504785048850498505085051850528505385054850558505685057850588505985060850618506285063850648506585066850678506885069850708507185072850738507485075850768507785078850798508085081850828508385084850858508685087850888508985090850918509285093850948509585096850978509885099851008510185102851038510485105851068510785108851098511085111851128511385114851158511685117851188511985120851218512285123851248512585126851278512885129851308513185132851338513485135851368513785138851398514085141851428514385144851458514685147851488514985150851518515285153851548515585156851578515885159851608516185162851638516485165851668516785168851698517085171851728517385174851758517685177851788517985180851818518285183851848518585186851878518885189851908519185192851938519485195851968519785198851998520085201852028520385204852058520685207852088520985210852118521285213852148521585216852178521885219852208522185222852238522485225852268522785228852298523085231852328523385234852358523685237852388523985240852418524285243852448524585246852478524885249852508525185252852538525485255852568525785258852598526085261852628526385264852658526685267852688526985270852718527285273852748527585276852778527885279852808528185282852838528485285852868528785288852898529085291852928529385294852958529685297852988529985300853018530285303853048530585306853078530885309853108531185312853138531485315853168531785318853198532085321853228532385324853258532685327853288532985330853318533285333853348533585336853378533885339853408534185342853438534485345853468534785348853498535085351853528535385354853558535685357853588535985360853618536285363853648536585366853678536885369853708537185372853738537485375853768537785378853798538085381853828538385384853858538685387853888538985390853918539285393853948539585396853978539885399854008540185402854038540485405854068540785408854098541085411854128541385414854158541685417854188541985420854218542285423854248542585426854278542885429854308543185432854338543485435854368543785438854398544085441854428544385444854458544685447854488544985450854518545285453854548545585456854578545885459854608546185462854638546485465854668546785468854698547085471854728547385474854758547685477854788547985480854818548285483854848548585486854878548885489854908549185492854938549485495854968549785498854998550085501855028550385504855058550685507855088550985510855118551285513855148551585516855178551885519855208552185522855238552485525855268552785528855298553085531855328553385534855358553685537855388553985540855418554285543855448554585546855478554885549855508555185552855538555485555855568555785558855598556085561855628556385564855658556685567855688556985570855718557285573855748557585576855778557885579855808558185582855838558485585855868558785588855898559085591855928559385594855958559685597855988559985600856018560285603856048560585606856078560885609856108561185612856138561485615856168561785618856198562085621856228562385624856258562685627856288562985630856318563285633856348563585636856378563885639856408564185642856438564485645856468564785648856498565085651856528565385654856558565685657856588565985660856618566285663856648566585666856678566885669856708567185672856738567485675856768567785678856798568085681856828568385684856858568685687856888568985690856918569285693856948569585696856978569885699857008570185702857038570485705857068570785708857098571085711857128571385714857158571685717857188571985720857218572285723857248572585726857278572885729857308573185732857338573485735857368573785738857398574085741857428574385744857458574685747857488574985750857518575285753857548575585756857578575885759857608576185762857638576485765857668576785768857698577085771857728577385774857758577685777857788577985780857818578285783857848578585786857878578885789857908579185792857938579485795857968579785798857998580085801858028580385804858058580685807858088580985810858118581285813858148581585816858178581885819858208582185822858238582485825858268582785828858298583085831858328583385834858358583685837858388583985840858418584285843858448584585846858478584885849858508585185852858538585485855858568585785858858598586085861858628586385864858658586685867858688586985870858718587285873858748587585876858778587885879858808588185882858838588485885858868588785888858898589085891858928589385894858958589685897858988589985900859018590285903859048590585906859078590885909859108591185912859138591485915859168591785918859198592085921859228592385924859258592685927859288592985930859318593285933859348593585936859378593885939859408594185942859438594485945859468594785948859498595085951859528595385954859558595685957859588595985960859618596285963859648596585966859678596885969859708597185972859738597485975859768597785978859798598085981859828598385984859858598685987859888598985990859918599285993859948599585996859978599885999860008600186002860038600486005860068600786008860098601086011860128601386014860158601686017860188601986020860218602286023860248602586026860278602886029860308603186032860338603486035860368603786038860398604086041860428604386044860458604686047860488604986050860518605286053860548605586056860578605886059860608606186062860638606486065860668606786068860698607086071860728607386074860758607686077860788607986080860818608286083860848608586086860878608886089860908609186092860938609486095860968609786098860998610086101861028610386104861058610686107861088610986110861118611286113861148611586116861178611886119861208612186122861238612486125861268612786128861298613086131861328613386134861358613686137861388613986140861418614286143861448614586146861478614886149861508615186152861538615486155861568615786158861598616086161861628616386164861658616686167861688616986170861718617286173861748617586176861778617886179861808618186182861838618486185861868618786188861898619086191861928619386194861958619686197861988619986200862018620286203862048620586206862078620886209862108621186212862138621486215862168621786218862198622086221862228622386224862258622686227862288622986230862318623286233862348623586236862378623886239862408624186242862438624486245862468624786248862498625086251862528625386254862558625686257862588625986260862618626286263862648626586266862678626886269862708627186272862738627486275862768627786278862798628086281862828628386284862858628686287862888628986290862918629286293862948629586296862978629886299863008630186302863038630486305863068630786308863098631086311863128631386314863158631686317863188631986320863218632286323863248632586326863278632886329863308633186332863338633486335863368633786338863398634086341863428634386344863458634686347863488634986350863518635286353863548635586356863578635886359863608636186362863638636486365863668636786368863698637086371863728637386374863758637686377863788637986380863818638286383863848638586386863878638886389863908639186392863938639486395863968639786398863998640086401864028640386404864058640686407864088640986410864118641286413864148641586416864178641886419864208642186422864238642486425864268642786428864298643086431864328643386434864358643686437864388643986440864418644286443864448644586446864478644886449864508645186452864538645486455864568645786458864598646086461864628646386464864658646686467864688646986470864718647286473864748647586476864778647886479864808648186482864838648486485864868648786488864898649086491864928649386494864958649686497864988649986500865018650286503865048650586506865078650886509865108651186512865138651486515865168651786518865198652086521865228652386524865258652686527865288652986530865318653286533865348653586536865378653886539865408654186542865438654486545865468654786548865498655086551865528655386554865558655686557865588655986560865618656286563865648656586566865678656886569865708657186572865738657486575865768657786578865798658086581865828658386584865858658686587865888658986590865918659286593865948659586596865978659886599866008660186602866038660486605866068660786608866098661086611866128661386614866158661686617866188661986620866218662286623866248662586626866278662886629866308663186632866338663486635866368663786638866398664086641866428664386644866458664686647866488664986650866518665286653866548665586656866578665886659866608666186662866638666486665866668666786668866698667086671866728667386674866758667686677866788667986680866818668286683866848668586686866878668886689866908669186692866938669486695866968669786698866998670086701867028670386704867058670686707867088670986710867118671286713867148671586716867178671886719867208672186722867238672486725867268672786728867298673086731867328673386734867358673686737867388673986740867418674286743867448674586746867478674886749867508675186752867538675486755867568675786758867598676086761867628676386764867658676686767867688676986770867718677286773867748677586776867778677886779867808678186782867838678486785867868678786788867898679086791867928679386794867958679686797867988679986800868018680286803868048680586806868078680886809868108681186812868138681486815868168681786818868198682086821868228682386824868258682686827868288682986830868318683286833868348683586836868378683886839868408684186842868438684486845868468684786848868498685086851868528685386854868558685686857868588685986860868618686286863868648686586866868678686886869868708687186872868738687486875868768687786878868798688086881868828688386884868858688686887868888688986890868918689286893868948689586896868978689886899869008690186902869038690486905869068690786908869098691086911869128691386914869158691686917869188691986920869218692286923869248692586926869278692886929869308693186932869338693486935869368693786938869398694086941869428694386944869458694686947869488694986950869518695286953869548695586956869578695886959869608696186962869638696486965869668696786968869698697086971869728697386974869758697686977869788697986980869818698286983869848698586986869878698886989869908699186992869938699486995869968699786998869998700087001870028700387004870058700687007870088700987010870118701287013870148701587016870178701887019870208702187022870238702487025870268702787028870298703087031870328703387034870358703687037870388703987040870418704287043870448704587046870478704887049870508705187052870538705487055870568705787058870598706087061870628706387064870658706687067870688706987070870718707287073870748707587076870778707887079870808708187082870838708487085870868708787088870898709087091870928709387094870958709687097870988709987100871018710287103871048710587106871078710887109871108711187112871138711487115871168711787118871198712087121871228712387124871258712687127871288712987130871318713287133871348713587136871378713887139871408714187142871438714487145871468714787148871498715087151871528715387154871558715687157871588715987160871618716287163871648716587166871678716887169871708717187172871738717487175871768717787178871798718087181871828718387184871858718687187871888718987190871918719287193871948719587196871978719887199872008720187202872038720487205872068720787208872098721087211872128721387214872158721687217872188721987220872218722287223872248722587226872278722887229872308723187232872338723487235872368723787238872398724087241872428724387244872458724687247872488724987250872518725287253872548725587256872578725887259872608726187262872638726487265872668726787268872698727087271872728727387274872758727687277872788727987280872818728287283872848728587286872878728887289872908729187292872938729487295872968729787298872998730087301873028730387304873058730687307873088730987310873118731287313873148731587316873178731887319873208732187322873238732487325873268732787328873298733087331873328733387334873358733687337873388733987340873418734287343873448734587346873478734887349873508735187352873538735487355873568735787358873598736087361873628736387364873658736687367873688736987370873718737287373873748737587376873778737887379873808738187382873838738487385873868738787388873898739087391873928739387394873958739687397873988739987400874018740287403874048740587406874078740887409874108741187412874138741487415874168741787418874198742087421874228742387424874258742687427874288742987430874318743287433874348743587436874378743887439874408744187442874438744487445874468744787448874498745087451874528745387454874558745687457874588745987460874618746287463874648746587466874678746887469874708747187472874738747487475874768747787478874798748087481874828748387484874858748687487874888748987490874918749287493874948749587496874978749887499875008750187502875038750487505875068750787508875098751087511875128751387514875158751687517875188751987520875218752287523875248752587526875278752887529875308753187532875338753487535875368753787538875398754087541875428754387544875458754687547875488754987550875518755287553875548755587556875578755887559875608756187562875638756487565875668756787568875698757087571875728757387574875758757687577875788757987580875818758287583875848758587586875878758887589875908759187592875938759487595875968759787598875998760087601876028760387604876058760687607876088760987610876118761287613876148761587616876178761887619876208762187622876238762487625876268762787628876298763087631876328763387634876358763687637876388763987640876418764287643876448764587646876478764887649876508765187652876538765487655876568765787658876598766087661876628766387664876658766687667876688766987670876718767287673876748767587676876778767887679876808768187682876838768487685876868768787688876898769087691876928769387694876958769687697876988769987700877018770287703877048770587706877078770887709877108771187712877138771487715877168771787718877198772087721877228772387724877258772687727877288772987730877318773287733877348773587736877378773887739877408774187742877438774487745877468774787748877498775087751877528775387754877558775687757877588775987760877618776287763877648776587766877678776887769877708777187772877738777487775877768777787778877798778087781877828778387784877858778687787877888778987790877918779287793877948779587796877978779887799878008780187802878038780487805878068780787808878098781087811878128781387814878158781687817878188781987820878218782287823878248782587826878278782887829878308783187832878338783487835878368783787838878398784087841878428784387844878458784687847878488784987850878518785287853878548785587856878578785887859878608786187862878638786487865878668786787868878698787087871878728787387874878758787687877878788787987880878818788287883878848788587886878878788887889878908789187892878938789487895878968789787898878998790087901879028790387904879058790687907879088790987910879118791287913879148791587916879178791887919879208792187922879238792487925879268792787928879298793087931879328793387934879358793687937879388793987940879418794287943879448794587946879478794887949879508795187952879538795487955879568795787958879598796087961879628796387964879658796687967879688796987970879718797287973879748797587976879778797887979879808798187982879838798487985879868798787988879898799087991879928799387994879958799687997879988799988000880018800288003880048800588006880078800888009880108801188012880138801488015880168801788018880198802088021880228802388024880258802688027880288802988030880318803288033880348803588036880378803888039880408804188042880438804488045880468804788048880498805088051880528805388054880558805688057880588805988060880618806288063880648806588066880678806888069880708807188072880738807488075880768807788078880798808088081880828808388084880858808688087880888808988090880918809288093880948809588096880978809888099881008810188102881038810488105881068810788108881098811088111881128811388114881158811688117881188811988120881218812288123881248812588126881278812888129881308813188132881338813488135881368813788138881398814088141881428814388144881458814688147881488814988150881518815288153881548815588156881578815888159881608816188162881638816488165881668816788168881698817088171881728817388174881758817688177881788817988180881818818288183881848818588186881878818888189881908819188192881938819488195881968819788198881998820088201882028820388204882058820688207882088820988210882118821288213882148821588216882178821888219882208822188222882238822488225882268822788228882298823088231882328823388234882358823688237882388823988240882418824288243882448824588246882478824888249882508825188252882538825488255882568825788258882598826088261882628826388264882658826688267882688826988270882718827288273882748827588276882778827888279882808828188282882838828488285882868828788288882898829088291882928829388294882958829688297882988829988300883018830288303883048830588306883078830888309883108831188312883138831488315883168831788318883198832088321883228832388324883258832688327883288832988330883318833288333883348833588336883378833888339883408834188342883438834488345883468834788348883498835088351883528835388354883558835688357883588835988360883618836288363883648836588366883678836888369883708837188372883738837488375883768837788378883798838088381883828838388384883858838688387883888838988390883918839288393883948839588396883978839888399884008840188402884038840488405884068840788408884098841088411884128841388414884158841688417884188841988420884218842288423884248842588426884278842888429884308843188432884338843488435884368843788438884398844088441884428844388444884458844688447884488844988450884518845288453884548845588456884578845888459884608846188462884638846488465884668846788468884698847088471884728847388474884758847688477884788847988480884818848288483884848848588486884878848888489884908849188492884938849488495884968849788498884998850088501885028850388504885058850688507885088850988510885118851288513885148851588516885178851888519885208852188522885238852488525885268852788528885298853088531885328853388534885358853688537885388853988540885418854288543885448854588546885478854888549885508855188552885538855488555885568855788558885598856088561885628856388564885658856688567885688856988570885718857288573885748857588576885778857888579885808858188582885838858488585885868858788588885898859088591885928859388594885958859688597885988859988600886018860288603886048860588606886078860888609886108861188612886138861488615886168861788618886198862088621886228862388624886258862688627886288862988630886318863288633886348863588636886378863888639886408864188642886438864488645886468864788648886498865088651886528865388654886558865688657886588865988660886618866288663886648866588666886678866888669886708867188672886738867488675886768867788678886798868088681886828868388684886858868688687886888868988690886918869288693886948869588696886978869888699887008870188702887038870488705887068870788708887098871088711887128871388714887158871688717887188871988720887218872288723887248872588726887278872888729887308873188732887338873488735887368873788738887398874088741887428874388744887458874688747887488874988750887518875288753887548875588756887578875888759887608876188762887638876488765887668876788768887698877088771887728877388774887758877688777887788877988780887818878288783887848878588786887878878888789887908879188792887938879488795887968879788798887998880088801888028880388804888058880688807888088880988810888118881288813888148881588816888178881888819888208882188822888238882488825888268882788828888298883088831888328883388834888358883688837888388883988840888418884288843888448884588846888478884888849888508885188852888538885488855888568885788858888598886088861888628886388864888658886688867888688886988870888718887288873888748887588876888778887888879888808888188882888838888488885888868888788888888898889088891888928889388894888958889688897888988889988900889018890288903889048890588906889078890888909889108891188912889138891488915889168891788918889198892088921889228892388924889258892688927889288892988930889318893288933889348893588936889378893888939889408894188942889438894488945889468894788948889498895088951889528895388954889558895688957889588895988960889618896288963889648896588966889678896888969889708897188972889738897488975889768897788978889798898088981889828898388984889858898688987889888898988990889918899288993889948899588996889978899888999890008900189002890038900489005890068900789008890098901089011890128901389014890158901689017890188901989020890218902289023890248902589026890278902889029890308903189032890338903489035890368903789038890398904089041890428904389044890458904689047890488904989050890518905289053890548905589056890578905889059890608906189062890638906489065890668906789068890698907089071890728907389074890758907689077890788907989080890818908289083890848908589086890878908889089890908909189092890938909489095890968909789098890998910089101891028910389104891058910689107891088910989110891118911289113891148911589116891178911889119891208912189122891238912489125891268912789128891298913089131891328913389134891358913689137891388913989140891418914289143891448914589146891478914889149891508915189152891538915489155891568915789158891598916089161891628916389164891658916689167891688916989170891718917289173891748917589176891778917889179891808918189182891838918489185891868918789188891898919089191891928919389194891958919689197891988919989200892018920289203892048920589206892078920889209892108921189212892138921489215892168921789218892198922089221892228922389224892258922689227892288922989230892318923289233892348923589236892378923889239892408924189242892438924489245892468924789248892498925089251892528925389254892558925689257892588925989260892618926289263892648926589266892678926889269892708927189272892738927489275892768927789278892798928089281892828928389284892858928689287892888928989290892918929289293892948929589296892978929889299893008930189302893038930489305893068930789308893098931089311893128931389314893158931689317893188931989320893218932289323893248932589326893278932889329893308933189332893338933489335893368933789338893398934089341893428934389344893458934689347893488934989350893518935289353893548935589356893578935889359893608936189362893638936489365893668936789368893698937089371893728937389374893758937689377893788937989380893818938289383893848938589386893878938889389893908939189392893938939489395893968939789398893998940089401894028940389404894058940689407894088940989410894118941289413894148941589416894178941889419894208942189422894238942489425894268942789428894298943089431894328943389434894358943689437894388943989440894418944289443894448944589446894478944889449894508945189452894538945489455894568945789458894598946089461894628946389464894658946689467894688946989470894718947289473894748947589476894778947889479894808948189482894838948489485894868948789488894898949089491894928949389494894958949689497894988949989500895018950289503895048950589506895078950889509895108951189512895138951489515895168951789518895198952089521895228952389524895258952689527895288952989530895318953289533895348953589536895378953889539895408954189542895438954489545895468954789548895498955089551895528955389554895558955689557895588955989560895618956289563895648956589566895678956889569895708957189572895738957489575895768957789578895798958089581895828958389584895858958689587895888958989590895918959289593895948959589596895978959889599896008960189602896038960489605896068960789608896098961089611896128961389614896158961689617896188961989620896218962289623896248962589626896278962889629896308963189632896338963489635896368963789638896398964089641896428964389644896458964689647896488964989650896518965289653896548965589656896578965889659896608966189662896638966489665896668966789668896698967089671896728967389674896758967689677896788967989680896818968289683896848968589686896878968889689896908969189692896938969489695896968969789698896998970089701897028970389704897058970689707897088970989710897118971289713897148971589716897178971889719897208972189722897238972489725897268972789728897298973089731897328973389734897358973689737897388973989740897418974289743897448974589746897478974889749897508975189752897538975489755897568975789758897598976089761897628976389764897658976689767897688976989770897718977289773897748977589776897778977889779897808978189782897838978489785897868978789788897898979089791897928979389794897958979689797897988979989800898018980289803898048980589806898078980889809898108981189812898138981489815898168981789818898198982089821898228982389824898258982689827898288982989830898318983289833898348983589836898378983889839898408984189842898438984489845898468984789848898498985089851898528985389854898558985689857898588985989860898618986289863898648986589866898678986889869898708987189872898738987489875898768987789878898798988089881898828988389884898858988689887898888988989890898918989289893898948989589896898978989889899899008990189902899038990489905899068990789908899098991089911899128991389914899158991689917899188991989920899218992289923899248992589926899278992889929899308993189932899338993489935899368993789938899398994089941899428994389944899458994689947899488994989950899518995289953899548995589956899578995889959899608996189962899638996489965899668996789968899698997089971899728997389974899758997689977899788997989980899818998289983899848998589986899878998889989899908999189992899938999489995899968999789998899999000090001900029000390004900059000690007900089000990010900119001290013900149001590016900179001890019900209002190022900239002490025900269002790028900299003090031900329003390034900359003690037900389003990040900419004290043900449004590046900479004890049900509005190052900539005490055900569005790058900599006090061900629006390064900659006690067900689006990070900719007290073900749007590076900779007890079900809008190082900839008490085900869008790088900899009090091900929009390094900959009690097900989009990100901019010290103901049010590106901079010890109901109011190112901139011490115901169011790118901199012090121901229012390124901259012690127901289012990130901319013290133901349013590136901379013890139901409014190142901439014490145901469014790148901499015090151901529015390154901559015690157901589015990160901619016290163901649016590166901679016890169901709017190172901739017490175901769017790178901799018090181901829018390184901859018690187901889018990190901919019290193901949019590196901979019890199902009020190202902039020490205902069020790208902099021090211902129021390214902159021690217902189021990220902219022290223902249022590226902279022890229902309023190232902339023490235902369023790238902399024090241902429024390244902459024690247902489024990250902519025290253902549025590256902579025890259902609026190262902639026490265902669026790268902699027090271902729027390274902759027690277902789027990280902819028290283902849028590286902879028890289902909029190292902939029490295902969029790298902999030090301903029030390304903059030690307903089030990310903119031290313903149031590316903179031890319903209032190322903239032490325903269032790328903299033090331903329033390334903359033690337903389033990340903419034290343903449034590346903479034890349903509035190352903539035490355903569035790358903599036090361903629036390364903659036690367903689036990370903719037290373903749037590376903779037890379903809038190382903839038490385903869038790388903899039090391903929039390394903959039690397903989039990400904019040290403904049040590406904079040890409904109041190412904139041490415904169041790418904199042090421904229042390424904259042690427904289042990430904319043290433904349043590436904379043890439904409044190442904439044490445904469044790448904499045090451904529045390454904559045690457904589045990460904619046290463904649046590466904679046890469904709047190472904739047490475904769047790478904799048090481904829048390484904859048690487904889048990490904919049290493904949049590496904979049890499905009050190502905039050490505905069050790508905099051090511905129051390514905159051690517905189051990520905219052290523905249052590526905279052890529905309053190532905339053490535905369053790538905399054090541905429054390544905459054690547905489054990550905519055290553905549055590556905579055890559905609056190562905639056490565905669056790568905699057090571905729057390574905759057690577905789057990580905819058290583905849058590586905879058890589905909059190592905939059490595905969059790598905999060090601906029060390604906059060690607906089060990610906119061290613906149061590616906179061890619906209062190622906239062490625906269062790628906299063090631906329063390634906359063690637906389063990640906419064290643906449064590646906479064890649906509065190652906539065490655906569065790658906599066090661906629066390664906659066690667906689066990670906719067290673906749067590676906779067890679906809068190682906839068490685906869068790688906899069090691906929069390694906959069690697906989069990700907019070290703907049070590706907079070890709907109071190712907139071490715907169071790718907199072090721907229072390724907259072690727907289072990730907319073290733907349073590736907379073890739907409074190742907439074490745907469074790748907499075090751907529075390754907559075690757907589075990760907619076290763907649076590766907679076890769907709077190772907739077490775907769077790778907799078090781907829078390784907859078690787907889078990790907919079290793907949079590796907979079890799908009080190802908039080490805908069080790808908099081090811908129081390814908159081690817908189081990820908219082290823908249082590826908279082890829908309083190832908339083490835908369083790838908399084090841908429084390844908459084690847908489084990850908519085290853908549085590856908579085890859908609086190862908639086490865908669086790868908699087090871908729087390874908759087690877908789087990880908819088290883908849088590886908879088890889908909089190892908939089490895908969089790898908999090090901909029090390904909059090690907909089090990910909119091290913909149091590916909179091890919909209092190922909239092490925909269092790928909299093090931909329093390934909359093690937909389093990940909419094290943909449094590946909479094890949909509095190952909539095490955909569095790958909599096090961909629096390964909659096690967909689096990970909719097290973909749097590976909779097890979909809098190982909839098490985909869098790988909899099090991909929099390994909959099690997909989099991000910019100291003910049100591006910079100891009910109101191012910139101491015910169101791018910199102091021910229102391024910259102691027910289102991030910319103291033910349103591036910379103891039910409104191042910439104491045910469104791048910499105091051910529105391054910559105691057910589105991060910619106291063910649106591066910679106891069910709107191072910739107491075910769107791078910799108091081910829108391084910859108691087910889108991090910919109291093910949109591096910979109891099911009110191102911039110491105911069110791108911099111091111911129111391114911159111691117911189111991120911219112291123911249112591126911279112891129911309113191132911339113491135911369113791138911399114091141911429114391144911459114691147911489114991150911519115291153911549115591156911579115891159911609116191162911639116491165911669116791168911699117091171911729117391174911759117691177911789117991180911819118291183911849118591186911879118891189911909119191192911939119491195911969119791198911999120091201912029120391204912059120691207912089120991210912119121291213912149121591216912179121891219912209122191222912239122491225912269122791228912299123091231912329123391234912359123691237912389123991240912419124291243912449124591246912479124891249912509125191252912539125491255912569125791258912599126091261912629126391264912659126691267912689126991270912719127291273912749127591276912779127891279912809128191282912839128491285912869128791288912899129091291912929129391294912959129691297912989129991300913019130291303913049130591306913079130891309913109131191312913139131491315913169131791318913199132091321913229132391324913259132691327913289132991330913319133291333913349133591336913379133891339913409134191342913439134491345913469134791348913499135091351913529135391354913559135691357913589135991360913619136291363913649136591366913679136891369913709137191372913739137491375913769137791378913799138091381913829138391384913859138691387913889138991390913919139291393913949139591396913979139891399914009140191402914039140491405914069140791408914099141091411914129141391414914159141691417914189141991420914219142291423914249142591426914279142891429914309143191432914339143491435914369143791438914399144091441914429144391444914459144691447914489144991450914519145291453914549145591456914579145891459914609146191462914639146491465914669146791468914699147091471914729147391474914759147691477914789147991480914819148291483914849148591486914879148891489914909149191492914939149491495914969149791498914999150091501915029150391504915059150691507915089150991510915119151291513915149151591516915179151891519915209152191522915239152491525915269152791528915299153091531915329153391534915359153691537915389153991540915419154291543915449154591546915479154891549915509155191552915539155491555915569155791558915599156091561915629156391564915659156691567915689156991570915719157291573915749157591576915779157891579915809158191582915839158491585915869158791588915899159091591915929159391594915959159691597915989159991600916019160291603916049160591606916079160891609916109161191612916139161491615916169161791618916199162091621916229162391624916259162691627916289162991630916319163291633916349163591636916379163891639916409164191642916439164491645916469164791648916499165091651916529165391654916559165691657916589165991660916619166291663916649166591666916679166891669916709167191672916739167491675916769167791678916799168091681916829168391684916859168691687916889168991690916919169291693916949169591696916979169891699917009170191702917039170491705917069170791708917099171091711917129171391714917159171691717917189171991720917219172291723917249172591726917279172891729917309173191732917339173491735917369173791738917399174091741917429174391744917459174691747917489174991750917519175291753917549175591756917579175891759917609176191762917639176491765917669176791768917699177091771917729177391774917759177691777917789177991780917819178291783917849178591786917879178891789917909179191792917939179491795917969179791798917999180091801918029180391804918059180691807918089180991810918119181291813918149181591816918179181891819918209182191822918239182491825918269182791828918299183091831918329183391834918359183691837918389183991840918419184291843918449184591846918479184891849918509185191852918539185491855918569185791858918599186091861918629186391864918659186691867918689186991870918719187291873918749187591876918779187891879918809188191882918839188491885918869188791888918899189091891918929189391894918959189691897918989189991900919019190291903919049190591906919079190891909919109191191912919139191491915919169191791918919199192091921919229192391924919259192691927919289192991930919319193291933919349193591936919379193891939919409194191942919439194491945919469194791948919499195091951919529195391954919559195691957919589195991960919619196291963919649196591966919679196891969919709197191972919739197491975919769197791978919799198091981919829198391984919859198691987919889198991990919919199291993919949199591996919979199891999920009200192002920039200492005920069200792008920099201092011920129201392014920159201692017920189201992020920219202292023920249202592026920279202892029920309203192032920339203492035920369203792038920399204092041920429204392044920459204692047920489204992050920519205292053920549205592056920579205892059920609206192062920639206492065920669206792068920699207092071920729207392074920759207692077920789207992080920819208292083920849208592086920879208892089920909209192092920939209492095920969209792098920999210092101921029210392104921059210692107921089210992110921119211292113921149211592116921179211892119921209212192122921239212492125921269212792128921299213092131921329213392134921359213692137921389213992140921419214292143921449214592146921479214892149921509215192152921539215492155921569215792158921599216092161921629216392164921659216692167921689216992170921719217292173921749217592176921779217892179921809218192182921839218492185921869218792188921899219092191921929219392194921959219692197921989219992200922019220292203922049220592206922079220892209922109221192212922139221492215922169221792218922199222092221922229222392224922259222692227922289222992230922319223292233922349223592236922379223892239922409224192242922439224492245922469224792248922499225092251922529225392254922559225692257922589225992260922619226292263922649226592266922679226892269922709227192272922739227492275922769227792278922799228092281922829228392284922859228692287922889228992290922919229292293922949229592296922979229892299923009230192302923039230492305923069230792308923099231092311923129231392314923159231692317923189231992320923219232292323923249232592326923279232892329923309233192332923339233492335923369233792338923399234092341923429234392344923459234692347923489234992350923519235292353923549235592356923579235892359923609236192362923639236492365923669236792368923699237092371923729237392374923759237692377923789237992380923819238292383923849238592386923879238892389923909239192392923939239492395923969239792398923999240092401924029240392404924059240692407924089240992410924119241292413924149241592416924179241892419924209242192422924239242492425924269242792428924299243092431924329243392434924359243692437924389243992440924419244292443924449244592446924479244892449924509245192452924539245492455924569245792458924599246092461924629246392464924659246692467924689246992470924719247292473924749247592476924779247892479924809248192482924839248492485924869248792488924899249092491924929249392494924959249692497924989249992500925019250292503925049250592506925079250892509925109251192512925139251492515925169251792518925199252092521925229252392524925259252692527925289252992530925319253292533925349253592536925379253892539925409254192542925439254492545925469254792548925499255092551925529255392554925559255692557925589255992560925619256292563925649256592566925679256892569925709257192572925739257492575925769257792578925799258092581925829258392584925859258692587925889258992590925919259292593925949259592596925979259892599926009260192602926039260492605926069260792608926099261092611926129261392614926159261692617926189261992620926219262292623926249262592626926279262892629926309263192632926339263492635926369263792638926399264092641926429264392644926459264692647926489264992650926519265292653926549265592656926579265892659926609266192662926639266492665926669266792668926699267092671926729267392674926759267692677926789267992680926819268292683926849268592686926879268892689926909269192692926939269492695926969269792698926999270092701927029270392704927059270692707927089270992710927119271292713927149271592716927179271892719927209272192722927239272492725927269272792728927299273092731927329273392734927359273692737927389273992740927419274292743927449274592746927479274892749927509275192752927539275492755927569275792758927599276092761927629276392764927659276692767927689276992770927719277292773927749277592776927779277892779927809278192782927839278492785927869278792788927899279092791927929279392794927959279692797927989279992800928019280292803928049280592806928079280892809928109281192812928139281492815928169281792818928199282092821928229282392824928259282692827928289282992830928319283292833928349283592836928379283892839928409284192842928439284492845928469284792848928499285092851928529285392854928559285692857928589285992860928619286292863928649286592866928679286892869928709287192872928739287492875928769287792878928799288092881928829288392884928859288692887928889288992890928919289292893928949289592896928979289892899929009290192902929039290492905929069290792908929099291092911929129291392914929159291692917929189291992920929219292292923929249292592926929279292892929929309293192932929339293492935929369293792938929399294092941929429294392944929459294692947929489294992950929519295292953929549295592956929579295892959929609296192962929639296492965929669296792968929699297092971929729297392974929759297692977929789297992980929819298292983929849298592986929879298892989929909299192992929939299492995929969299792998929999300093001930029300393004930059300693007930089300993010930119301293013930149301593016930179301893019930209302193022930239302493025930269302793028930299303093031930329303393034930359303693037930389303993040930419304293043930449304593046930479304893049930509305193052930539305493055930569305793058930599306093061930629306393064930659306693067930689306993070930719307293073930749307593076930779307893079930809308193082930839308493085930869308793088930899309093091930929309393094930959309693097930989309993100931019310293103931049310593106931079310893109931109311193112931139311493115931169311793118931199312093121931229312393124931259312693127931289312993130931319313293133931349313593136931379313893139931409314193142931439314493145931469314793148931499315093151931529315393154931559315693157931589315993160931619316293163931649316593166931679316893169931709317193172931739317493175931769317793178931799318093181931829318393184931859318693187931889318993190931919319293193931949319593196931979319893199932009320193202932039320493205932069320793208932099321093211932129321393214932159321693217932189321993220932219322293223932249322593226932279322893229932309323193232932339323493235932369323793238932399324093241932429324393244932459324693247932489324993250932519325293253932549325593256932579325893259932609326193262932639326493265932669326793268932699327093271932729327393274932759327693277932789327993280932819328293283932849328593286932879328893289932909329193292932939329493295932969329793298932999330093301933029330393304933059330693307933089330993310933119331293313933149331593316933179331893319933209332193322933239332493325933269332793328933299333093331933329333393334933359333693337933389333993340933419334293343933449334593346933479334893349933509335193352933539335493355933569335793358933599336093361933629336393364933659336693367933689336993370933719337293373933749337593376933779337893379933809338193382933839338493385933869338793388933899339093391933929339393394933959339693397933989339993400934019340293403934049340593406934079340893409934109341193412934139341493415934169341793418934199342093421934229342393424934259342693427934289342993430934319343293433934349343593436934379343893439934409344193442934439344493445934469344793448934499345093451934529345393454934559345693457934589345993460934619346293463934649346593466934679346893469934709347193472934739347493475934769347793478934799348093481934829348393484934859348693487934889348993490934919349293493934949349593496934979349893499935009350193502935039350493505935069350793508935099351093511935129351393514935159351693517935189351993520935219352293523935249352593526935279352893529935309353193532935339353493535935369353793538935399354093541935429354393544935459354693547935489354993550935519355293553935549355593556935579355893559935609356193562935639356493565935669356793568935699357093571935729357393574935759357693577935789357993580935819358293583935849358593586935879358893589935909359193592935939359493595935969359793598935999360093601936029360393604936059360693607936089360993610936119361293613936149361593616936179361893619936209362193622936239362493625936269362793628936299363093631936329363393634936359363693637936389363993640936419364293643936449364593646936479364893649936509365193652936539365493655936569365793658936599366093661936629366393664936659366693667936689366993670936719367293673936749367593676936779367893679936809368193682936839368493685936869368793688936899369093691936929369393694936959369693697936989369993700937019370293703937049370593706937079370893709937109371193712937139371493715937169371793718937199372093721937229372393724937259372693727937289372993730937319373293733937349373593736937379373893739937409374193742937439374493745937469374793748937499375093751937529375393754937559375693757937589375993760937619376293763937649376593766937679376893769937709377193772937739377493775937769377793778937799378093781937829378393784937859378693787937889378993790937919379293793937949379593796937979379893799938009380193802938039380493805938069380793808938099381093811938129381393814938159381693817938189381993820938219382293823938249382593826938279382893829938309383193832938339383493835938369383793838938399384093841938429384393844938459384693847938489384993850938519385293853938549385593856938579385893859938609386193862938639386493865938669386793868938699387093871938729387393874938759387693877938789387993880938819388293883938849388593886938879388893889938909389193892938939389493895938969389793898938999390093901939029390393904939059390693907939089390993910939119391293913939149391593916939179391893919939209392193922939239392493925939269392793928939299393093931939329393393934939359393693937939389393993940939419394293943939449394593946939479394893949939509395193952939539395493955939569395793958939599396093961939629396393964939659396693967939689396993970939719397293973939749397593976939779397893979939809398193982939839398493985939869398793988939899399093991939929399393994939959399693997939989399994000940019400294003940049400594006940079400894009940109401194012940139401494015940169401794018940199402094021940229402394024940259402694027940289402994030940319403294033940349403594036940379403894039940409404194042940439404494045940469404794048940499405094051940529405394054940559405694057940589405994060940619406294063940649406594066940679406894069940709407194072940739407494075940769407794078940799408094081940829408394084940859408694087940889408994090940919409294093940949409594096940979409894099941009410194102941039410494105941069410794108941099411094111941129411394114941159411694117941189411994120941219412294123941249412594126941279412894129941309413194132941339413494135941369413794138941399414094141941429414394144941459414694147941489414994150941519415294153941549415594156941579415894159941609416194162941639416494165941669416794168941699417094171941729417394174941759417694177941789417994180941819418294183941849418594186941879418894189941909419194192941939419494195941969419794198941999420094201942029420394204942059420694207942089420994210942119421294213942149421594216942179421894219942209422194222942239422494225942269422794228942299423094231942329423394234942359423694237942389423994240942419424294243942449424594246942479424894249942509425194252942539425494255942569425794258942599426094261942629426394264942659426694267942689426994270942719427294273942749427594276942779427894279942809428194282942839428494285942869428794288942899429094291942929429394294942959429694297942989429994300943019430294303943049430594306943079430894309943109431194312943139431494315943169431794318943199432094321943229432394324943259432694327943289432994330943319433294333943349433594336943379433894339943409434194342943439434494345943469434794348943499435094351943529435394354943559435694357943589435994360943619436294363943649436594366943679436894369943709437194372943739437494375943769437794378943799438094381943829438394384943859438694387943889438994390943919439294393943949439594396943979439894399944009440194402944039440494405944069440794408944099441094411944129441394414944159441694417944189441994420944219442294423944249442594426944279442894429944309443194432944339443494435944369443794438944399444094441944429444394444944459444694447944489444994450944519445294453944549445594456944579445894459944609446194462944639446494465944669446794468944699447094471944729447394474944759447694477944789447994480944819448294483944849448594486944879448894489944909449194492944939449494495944969449794498944999450094501945029450394504945059450694507945089450994510945119451294513945149451594516945179451894519945209452194522945239452494525945269452794528945299453094531945329453394534945359453694537945389453994540945419454294543945449454594546945479454894549945509455194552945539455494555945569455794558945599456094561945629456394564945659456694567945689456994570945719457294573945749457594576945779457894579945809458194582945839458494585945869458794588945899459094591945929459394594945959459694597945989459994600946019460294603946049460594606946079460894609946109461194612946139461494615946169461794618946199462094621946229462394624946259462694627946289462994630946319463294633946349463594636946379463894639946409464194642946439464494645946469464794648946499465094651946529465394654946559465694657946589465994660946619466294663946649466594666946679466894669946709467194672946739467494675946769467794678946799468094681946829468394684946859468694687946889468994690946919469294693946949469594696946979469894699947009470194702947039470494705947069470794708947099471094711947129471394714947159471694717947189471994720947219472294723947249472594726947279472894729947309473194732947339473494735947369473794738947399474094741947429474394744947459474694747947489474994750947519475294753947549475594756947579475894759947609476194762947639476494765947669476794768947699477094771947729477394774947759477694777947789477994780947819478294783947849478594786947879478894789947909479194792947939479494795947969479794798947999480094801948029480394804948059480694807948089480994810948119481294813948149481594816948179481894819948209482194822948239482494825948269482794828948299483094831948329483394834948359483694837948389483994840948419484294843948449484594846948479484894849948509485194852948539485494855948569485794858948599486094861948629486394864948659486694867948689486994870948719487294873948749487594876948779487894879948809488194882948839488494885948869488794888948899489094891948929489394894948959489694897948989489994900949019490294903949049490594906949079490894909949109491194912949139491494915949169491794918949199492094921949229492394924949259492694927949289492994930949319493294933949349493594936949379493894939949409494194942949439494494945949469494794948949499495094951949529495394954949559495694957949589495994960949619496294963949649496594966949679496894969949709497194972949739497494975949769497794978949799498094981949829498394984949859498694987949889498994990949919499294993949949499594996949979499894999950009500195002950039500495005950069500795008950099501095011950129501395014950159501695017950189501995020950219502295023950249502595026950279502895029950309503195032950339503495035950369503795038950399504095041950429504395044950459504695047950489504995050950519505295053950549505595056950579505895059950609506195062950639506495065950669506795068950699507095071950729507395074950759507695077950789507995080950819508295083950849508595086950879508895089950909509195092950939509495095950969509795098950999510095101951029510395104951059510695107951089510995110951119511295113951149511595116951179511895119951209512195122951239512495125951269512795128951299513095131951329513395134951359513695137951389513995140951419514295143951449514595146951479514895149951509515195152951539515495155951569515795158951599516095161951629516395164951659516695167951689516995170951719517295173951749517595176951779517895179951809518195182951839518495185951869518795188951899519095191951929519395194951959519695197951989519995200952019520295203952049520595206952079520895209952109521195212952139521495215952169521795218952199522095221952229522395224952259522695227952289522995230952319523295233952349523595236952379523895239952409524195242952439524495245952469524795248952499525095251952529525395254952559525695257952589525995260952619526295263952649526595266952679526895269952709527195272952739527495275952769527795278952799528095281952829528395284952859528695287952889528995290952919529295293952949529595296952979529895299953009530195302953039530495305953069530795308953099531095311953129531395314953159531695317953189531995320953219532295323953249532595326953279532895329953309533195332953339533495335953369533795338953399534095341953429534395344953459534695347953489534995350953519535295353953549535595356953579535895359953609536195362953639536495365953669536795368953699537095371953729537395374953759537695377953789537995380953819538295383953849538595386953879538895389953909539195392953939539495395953969539795398953999540095401954029540395404954059540695407954089540995410954119541295413954149541595416954179541895419954209542195422954239542495425954269542795428954299543095431954329543395434954359543695437954389543995440954419544295443954449544595446954479544895449954509545195452954539545495455954569545795458954599546095461954629546395464954659546695467954689546995470954719547295473954749547595476954779547895479954809548195482954839548495485954869548795488954899549095491954929549395494954959549695497954989549995500955019550295503955049550595506955079550895509955109551195512955139551495515955169551795518955199552095521955229552395524955259552695527955289552995530955319553295533955349553595536955379553895539955409554195542955439554495545955469554795548955499555095551955529555395554955559555695557955589555995560955619556295563955649556595566955679556895569955709557195572955739557495575955769557795578955799558095581955829558395584955859558695587955889558995590955919559295593955949559595596955979559895599956009560195602956039560495605956069560795608956099561095611956129561395614956159561695617956189561995620956219562295623956249562595626956279562895629956309563195632956339563495635956369563795638956399564095641956429564395644956459564695647956489564995650956519565295653956549565595656956579565895659956609566195662956639566495665956669566795668956699567095671956729567395674956759567695677956789567995680956819568295683956849568595686956879568895689956909569195692956939569495695956969569795698956999570095701957029570395704957059570695707957089570995710957119571295713957149571595716957179571895719957209572195722957239572495725957269572795728957299573095731957329573395734957359573695737957389573995740957419574295743957449574595746957479574895749957509575195752957539575495755957569575795758957599576095761957629576395764957659576695767957689576995770957719577295773957749577595776957779577895779957809578195782957839578495785957869578795788957899579095791957929579395794957959579695797957989579995800958019580295803958049580595806958079580895809958109581195812958139581495815958169581795818958199582095821958229582395824958259582695827958289582995830958319583295833958349583595836958379583895839958409584195842958439584495845958469584795848958499585095851958529585395854958559585695857958589585995860958619586295863958649586595866958679586895869958709587195872958739587495875958769587795878958799588095881958829588395884958859588695887958889588995890958919589295893958949589595896958979589895899959009590195902959039590495905959069590795908959099591095911959129591395914959159591695917959189591995920959219592295923959249592595926959279592895929959309593195932959339593495935959369593795938959399594095941959429594395944959459594695947959489594995950959519595295953959549595595956959579595895959959609596195962959639596495965959669596795968959699597095971959729597395974959759597695977959789597995980959819598295983959849598595986959879598895989959909599195992959939599495995959969599795998959999600096001960029600396004960059600696007960089600996010960119601296013960149601596016960179601896019960209602196022960239602496025960269602796028960299603096031960329603396034960359603696037960389603996040960419604296043960449604596046960479604896049960509605196052960539605496055960569605796058960599606096061960629606396064960659606696067960689606996070960719607296073960749607596076960779607896079960809608196082960839608496085960869608796088960899609096091960929609396094960959609696097960989609996100961019610296103961049610596106961079610896109961109611196112961139611496115961169611796118961199612096121961229612396124961259612696127961289612996130961319613296133961349613596136961379613896139961409614196142961439614496145961469614796148961499615096151961529615396154961559615696157961589615996160961619616296163961649616596166961679616896169961709617196172961739617496175961769617796178961799618096181961829618396184961859618696187961889618996190961919619296193961949619596196961979619896199962009620196202962039620496205962069620796208962099621096211962129621396214962159621696217962189621996220962219622296223962249622596226962279622896229962309623196232962339623496235962369623796238962399624096241962429624396244962459624696247962489624996250962519625296253962549625596256962579625896259962609626196262962639626496265962669626796268962699627096271962729627396274962759627696277962789627996280962819628296283962849628596286962879628896289962909629196292962939629496295962969629796298962999630096301963029630396304963059630696307963089630996310963119631296313963149631596316963179631896319963209632196322963239632496325963269632796328963299633096331963329633396334963359633696337963389633996340963419634296343963449634596346963479634896349963509635196352963539635496355963569635796358963599636096361963629636396364963659636696367963689636996370963719637296373963749637596376963779637896379963809638196382963839638496385963869638796388963899639096391963929639396394963959639696397963989639996400964019640296403964049640596406964079640896409964109641196412964139641496415964169641796418964199642096421964229642396424964259642696427964289642996430964319643296433964349643596436964379643896439964409644196442964439644496445964469644796448964499645096451964529645396454964559645696457964589645996460964619646296463964649646596466964679646896469964709647196472964739647496475964769647796478964799648096481964829648396484964859648696487964889648996490964919649296493964949649596496964979649896499965009650196502965039650496505965069650796508965099651096511965129651396514965159651696517965189651996520965219652296523965249652596526965279652896529965309653196532965339653496535965369653796538965399654096541965429654396544965459654696547965489654996550965519655296553965549655596556965579655896559965609656196562965639656496565965669656796568965699657096571965729657396574965759657696577965789657996580965819658296583965849658596586965879658896589965909659196592965939659496595965969659796598965999660096601966029660396604966059660696607966089660996610966119661296613966149661596616966179661896619966209662196622966239662496625966269662796628966299663096631966329663396634966359663696637966389663996640966419664296643966449664596646966479664896649966509665196652966539665496655966569665796658966599666096661966629666396664966659666696667966689666996670966719667296673966749667596676966779667896679966809668196682966839668496685966869668796688966899669096691966929669396694966959669696697966989669996700967019670296703967049670596706967079670896709967109671196712967139671496715967169671796718967199672096721967229672396724967259672696727967289672996730967319673296733967349673596736967379673896739967409674196742967439674496745967469674796748967499675096751967529675396754967559675696757967589675996760967619676296763967649676596766967679676896769967709677196772967739677496775967769677796778967799678096781967829678396784967859678696787967889678996790967919679296793967949679596796967979679896799968009680196802
  1. /*
  2. * Single source autogenerated distributable for Duktape 2.3.0.
  3. *
  4. * Git commit d7fdb67f18561a50e06bafd196c6b423af9ad6fe (v2.3.0).
  5. * Git branch master.
  6. *
  7. * See Duktape AUTHORS.rst and LICENSE.txt for copyright and
  8. * licensing information.
  9. */
  10. /* LICENSE.txt */
  11. /*
  12. * ===============
  13. * Duktape license
  14. * ===============
  15. *
  16. * (http://opensource.org/licenses/MIT)
  17. *
  18. * Copyright (c) 2013-2018 by Duktape authors (see AUTHORS.rst)
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining a copy
  21. * of this software and associated documentation files (the "Software"), to deal
  22. * in the Software without restriction, including without limitation the rights
  23. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  24. * copies of the Software, and to permit persons to whom the Software is
  25. * furnished to do so, subject to the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included in
  28. * all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  35. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  36. * THE SOFTWARE.
  37. */
  38. /* AUTHORS.rst */
  39. /*
  40. * ===============
  41. * Duktape authors
  42. * ===============
  43. *
  44. * Copyright
  45. * =========
  46. *
  47. * Duktape copyrights are held by its authors. Each author has a copyright
  48. * to their contribution, and agrees to irrevocably license the contribution
  49. * under the Duktape ``LICENSE.txt``.
  50. *
  51. * Authors
  52. * =======
  53. *
  54. * Please include an e-mail address, a link to your GitHub profile, or something
  55. * similar to allow your contribution to be identified accurately.
  56. *
  57. * The following people have contributed code, website contents, or Wiki contents,
  58. * and agreed to irrevocably license their contributions under the Duktape
  59. * ``LICENSE.txt`` (in order of appearance):
  60. *
  61. * * Sami Vaarala <sami.vaarala@iki.fi>
  62. * * Niki Dobrev
  63. * * Andreas \u00d6man <andreas@lonelycoder.com>
  64. * * L\u00e1szl\u00f3 Lang\u00f3 <llango.u-szeged@partner.samsung.com>
  65. * * Legimet <legimet.calc@gmail.com>
  66. * * Karl Skomski <karl@skomski.com>
  67. * * Bruce Pascoe <fatcerberus1@gmail.com>
  68. * * Ren\u00e9 Hollander <rene@rene8888.at>
  69. * * Julien Hamaide (https://github.com/crazyjul)
  70. * * Sebastian G\u00f6tte (https://github.com/jaseg)
  71. * * Tomasz Magulski (https://github.com/magul)
  72. * * \D. Bohdan (https://github.com/dbohdan)
  73. * * Ond\u0159ej Jirman (https://github.com/megous)
  74. * * Sa\u00fal Ibarra Corretg\u00e9 <saghul@gmail.com>
  75. * * Jeremy HU <huxingyi@msn.com>
  76. * * Ole Andr\u00e9 Vadla Ravn\u00e5s (https://github.com/oleavr)
  77. * * Harold Brenes (https://github.com/harold-b)
  78. * * Oliver Crow (https://github.com/ocrow)
  79. * * Jakub Ch\u0142api\u0144ski (https://github.com/jchlapinski)
  80. * * Brett Vickers (https://github.com/beevik)
  81. * * Dominik Okwieka (https://github.com/okitec)
  82. * * Remko Tron\u00e7on (https://el-tramo.be)
  83. * * Romero Malaquias (rbsm@ic.ufal.br)
  84. * * Michael Drake <michael.drake@codethink.co.uk>
  85. * * Steven Don (https://github.com/shdon)
  86. * * Simon Stone (https://github.com/sstone1)
  87. * * \J. McC. (https://github.com/jmhmccr)
  88. * * Jakub Nowakowski (https://github.com/jimvonmoon)
  89. * * Tommy Nguyen (https://github.com/tn0502)
  90. * * Fabrice Fontaine (https://github.com/ffontaine)
  91. * * Christopher Hiller (https://github.com/boneskull)
  92. * * Gonzalo Diethelm (https://github.com/gonzus)
  93. * * Michal Kasperek (https://github.com/michalkas)
  94. * * Andrew Janke (https://github.com/apjanke)
  95. * * Steve Fan (https://github.com/stevefan1999)
  96. *
  97. * Other contributions
  98. * ===================
  99. *
  100. * The following people have contributed something other than code (e.g. reported
  101. * bugs, provided ideas, etc; roughly in order of appearance):
  102. *
  103. * * Greg Burns
  104. * * Anthony Rabine
  105. * * Carlos Costa
  106. * * Aur\u00e9lien Bouilland
  107. * * Preet Desai (Pris Matic)
  108. * * judofyr (http://www.reddit.com/user/judofyr)
  109. * * Jason Woofenden
  110. * * Micha\u0142 Przyby\u015b
  111. * * Anthony Howe
  112. * * Conrad Pankoff
  113. * * Jim Schimpf
  114. * * Rajaran Gaunker (https://github.com/zimbabao)
  115. * * Andreas \u00d6man
  116. * * Doug Sanden
  117. * * Josh Engebretson (https://github.com/JoshEngebretson)
  118. * * Remo Eichenberger (https://github.com/remoe)
  119. * * Mamod Mehyar (https://github.com/mamod)
  120. * * David Demelier (https://github.com/markand)
  121. * * Tim Caswell (https://github.com/creationix)
  122. * * Mitchell Blank Jr (https://github.com/mitchblank)
  123. * * https://github.com/yushli
  124. * * Seo Sanghyeon (https://github.com/sanxiyn)
  125. * * Han ChoongWoo (https://github.com/tunz)
  126. * * Joshua Peek (https://github.com/josh)
  127. * * Bruce E. Pascoe (https://github.com/fatcerberus)
  128. * * https://github.com/Kelledin
  129. * * https://github.com/sstruchtrup
  130. * * Michael Drake (https://github.com/tlsa)
  131. * * https://github.com/chris-y
  132. * * Laurent Zubiaur (https://github.com/lzubiaur)
  133. * * Neil Kolban (https://github.com/nkolban)
  134. * * Wilhelm Wanecek (https://github.com/wanecek)
  135. * * Andrew Janke (https://github.com/apjanke)
  136. *
  137. * If you are accidentally missing from this list, send me an e-mail
  138. * (``sami.vaarala@iki.fi``) and I'll fix the omission.
  139. */
  140. #line 1 "duk_replacements.c"
  141. /*
  142. * Replacements for missing platform functions.
  143. *
  144. * Unlike the originals, fpclassify() and signbit() replacements don't
  145. * work on any floating point types, only doubles. The C typing here
  146. * mimics the standard prototypes.
  147. */
  148. /* #include duk_internal.h */
  149. #line 1 "duk_internal.h"
  150. /*
  151. * Top-level include file to be used for all (internal) source files.
  152. *
  153. * Source files should not include individual header files, as they
  154. * have not been designed to be individually included.
  155. */
  156. #if !defined(DUK_INTERNAL_H_INCLUDED)
  157. #define DUK_INTERNAL_H_INCLUDED
  158. /*
  159. * The 'duktape.h' header provides the public API, but also handles all
  160. * compiler and platform specific feature detection, Duktape feature
  161. * resolution, inclusion of system headers, etc. These have been merged
  162. * because the public API is also dependent on e.g. detecting appropriate
  163. * C types which is quite platform/compiler specific especially for a non-C99
  164. * build. The public API is also dependent on the resolved feature set.
  165. *
  166. * Some actions taken by the merged header (such as including system headers)
  167. * are not appropriate for building a user application. The define
  168. * DUK_COMPILING_DUKTAPE allows the merged header to skip/include some
  169. * sections depending on what is being built.
  170. */
  171. #define DUK_COMPILING_DUKTAPE
  172. #include "duktape.h"
  173. /*
  174. * User declarations, e.g. prototypes for user functions used by Duktape
  175. * macros.
  176. */
  177. DUK_USE_USER_DECLARE()
  178. /*
  179. * Duktape includes (other than duk_features.h)
  180. *
  181. * The header files expect to be included in an order which satisfies header
  182. * dependencies correctly (the headers themselves don't include any other
  183. * includes). Forward declarations are used to break circular struct/typedef
  184. * dependencies.
  185. */
  186. /* #include duk_dblunion.h */
  187. #line 1 "duk_dblunion.h"
  188. /*
  189. * Union to access IEEE double memory representation, indexes for double
  190. * memory representation, and some macros for double manipulation.
  191. *
  192. * Also used by packed duk_tval. Use a union for bit manipulation to
  193. * minimize aliasing issues in practice. The C99 standard does not
  194. * guarantee that this should work, but it's a very widely supported
  195. * practice for low level manipulation.
  196. *
  197. * IEEE double format summary:
  198. *
  199. * seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
  200. * A B C D E F G H
  201. *
  202. * s sign bit
  203. * eee... exponent field
  204. * fff... fraction
  205. *
  206. * See http://en.wikipedia.org/wiki/Double_precision_floating-point_format.
  207. *
  208. * NaNs are represented as exponent 0x7ff and mantissa != 0. The NaN is a
  209. * signaling NaN when the highest bit of the mantissa is zero, and a quiet
  210. * NaN when the highest bit is set.
  211. *
  212. * At least three memory layouts are relevant here:
  213. *
  214. * A B C D E F G H Big endian (e.g. 68k) DUK_USE_DOUBLE_BE
  215. * H G F E D C B A Little endian (e.g. x86) DUK_USE_DOUBLE_LE
  216. * D C B A H G F E Mixed/cross endian (e.g. ARM) DUK_USE_DOUBLE_ME
  217. *
  218. * ARM is a special case: ARM double values are in mixed/cross endian
  219. * format while ARM duk_uint64_t values are in standard little endian
  220. * format (H G F E D C B A). When a double is read as a duk_uint64_t
  221. * from memory, the register will contain the (logical) value
  222. * E F G H A B C D. This requires some special handling below.
  223. *
  224. * Indexes of various types (8-bit, 16-bit, 32-bit) in memory relative to
  225. * the logical (big endian) order:
  226. *
  227. * byte order duk_uint8_t duk_uint16_t duk_uint32_t
  228. * BE 01234567 0123 01
  229. * LE 76543210 3210 10
  230. * ME (ARM) 32107654 1032 01
  231. *
  232. * Some processors may alter NaN values in a floating point load+store.
  233. * For instance, on X86 a FLD + FSTP may convert a signaling NaN to a
  234. * quiet one. This is catastrophic when NaN space is used in packed
  235. * duk_tval values. See: misc/clang_aliasing.c.
  236. */
  237. #if !defined(DUK_DBLUNION_H_INCLUDED)
  238. #define DUK_DBLUNION_H_INCLUDED
  239. /*
  240. * Union for accessing double parts, also serves as packed duk_tval
  241. */
  242. union duk_double_union {
  243. double d;
  244. float f[2];
  245. #if defined(DUK_USE_64BIT_OPS)
  246. duk_uint64_t ull[1];
  247. #endif
  248. duk_uint32_t ui[2];
  249. duk_uint16_t us[4];
  250. duk_uint8_t uc[8];
  251. #if defined(DUK_USE_PACKED_TVAL)
  252. void *vp[2]; /* used by packed duk_tval, assumes sizeof(void *) == 4 */
  253. #endif
  254. };
  255. typedef union duk_double_union duk_double_union;
  256. /*
  257. * Indexes of various types with respect to big endian (logical) layout
  258. */
  259. #if defined(DUK_USE_DOUBLE_LE)
  260. #if defined(DUK_USE_64BIT_OPS)
  261. #define DUK_DBL_IDX_ULL0 0
  262. #endif
  263. #define DUK_DBL_IDX_UI0 1
  264. #define DUK_DBL_IDX_UI1 0
  265. #define DUK_DBL_IDX_US0 3
  266. #define DUK_DBL_IDX_US1 2
  267. #define DUK_DBL_IDX_US2 1
  268. #define DUK_DBL_IDX_US3 0
  269. #define DUK_DBL_IDX_UC0 7
  270. #define DUK_DBL_IDX_UC1 6
  271. #define DUK_DBL_IDX_UC2 5
  272. #define DUK_DBL_IDX_UC3 4
  273. #define DUK_DBL_IDX_UC4 3
  274. #define DUK_DBL_IDX_UC5 2
  275. #define DUK_DBL_IDX_UC6 1
  276. #define DUK_DBL_IDX_UC7 0
  277. #define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
  278. #define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
  279. #elif defined(DUK_USE_DOUBLE_BE)
  280. #if defined(DUK_USE_64BIT_OPS)
  281. #define DUK_DBL_IDX_ULL0 0
  282. #endif
  283. #define DUK_DBL_IDX_UI0 0
  284. #define DUK_DBL_IDX_UI1 1
  285. #define DUK_DBL_IDX_US0 0
  286. #define DUK_DBL_IDX_US1 1
  287. #define DUK_DBL_IDX_US2 2
  288. #define DUK_DBL_IDX_US3 3
  289. #define DUK_DBL_IDX_UC0 0
  290. #define DUK_DBL_IDX_UC1 1
  291. #define DUK_DBL_IDX_UC2 2
  292. #define DUK_DBL_IDX_UC3 3
  293. #define DUK_DBL_IDX_UC4 4
  294. #define DUK_DBL_IDX_UC5 5
  295. #define DUK_DBL_IDX_UC6 6
  296. #define DUK_DBL_IDX_UC7 7
  297. #define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
  298. #define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
  299. #elif defined(DUK_USE_DOUBLE_ME)
  300. #if defined(DUK_USE_64BIT_OPS)
  301. #define DUK_DBL_IDX_ULL0 0 /* not directly applicable, byte order differs from a double */
  302. #endif
  303. #define DUK_DBL_IDX_UI0 0
  304. #define DUK_DBL_IDX_UI1 1
  305. #define DUK_DBL_IDX_US0 1
  306. #define DUK_DBL_IDX_US1 0
  307. #define DUK_DBL_IDX_US2 3
  308. #define DUK_DBL_IDX_US3 2
  309. #define DUK_DBL_IDX_UC0 3
  310. #define DUK_DBL_IDX_UC1 2
  311. #define DUK_DBL_IDX_UC2 1
  312. #define DUK_DBL_IDX_UC3 0
  313. #define DUK_DBL_IDX_UC4 7
  314. #define DUK_DBL_IDX_UC5 6
  315. #define DUK_DBL_IDX_UC6 5
  316. #define DUK_DBL_IDX_UC7 4
  317. #define DUK_DBL_IDX_VP0 DUK_DBL_IDX_UI0 /* packed tval */
  318. #define DUK_DBL_IDX_VP1 DUK_DBL_IDX_UI1 /* packed tval */
  319. #else
  320. #error internal error
  321. #endif
  322. /*
  323. * Helper macros for reading/writing memory representation parts, used
  324. * by duk_numconv.c and duk_tval.h.
  325. */
  326. #define DUK_DBLUNION_SET_DOUBLE(u,v) do { \
  327. (u)->d = (v); \
  328. } while (0)
  329. #define DUK_DBLUNION_SET_HIGH32(u,v) do { \
  330. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) (v); \
  331. } while (0)
  332. #if defined(DUK_USE_64BIT_OPS)
  333. #if defined(DUK_USE_DOUBLE_ME)
  334. #define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
  335. (u)->ull[DUK_DBL_IDX_ULL0] = (duk_uint64_t) (v); \
  336. } while (0)
  337. #else
  338. #define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
  339. (u)->ull[DUK_DBL_IDX_ULL0] = ((duk_uint64_t) (v)) << 32; \
  340. } while (0)
  341. #endif
  342. #else /* DUK_USE_64BIT_OPS */
  343. #define DUK_DBLUNION_SET_HIGH32_ZERO_LOW32(u,v) do { \
  344. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) (v); \
  345. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) 0; \
  346. } while (0)
  347. #endif /* DUK_USE_64BIT_OPS */
  348. #define DUK_DBLUNION_SET_LOW32(u,v) do { \
  349. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (v); \
  350. } while (0)
  351. #define DUK_DBLUNION_GET_DOUBLE(u) ((u)->d)
  352. #define DUK_DBLUNION_GET_HIGH32(u) ((u)->ui[DUK_DBL_IDX_UI0])
  353. #define DUK_DBLUNION_GET_LOW32(u) ((u)->ui[DUK_DBL_IDX_UI1])
  354. #if defined(DUK_USE_64BIT_OPS)
  355. #if defined(DUK_USE_DOUBLE_ME)
  356. #define DUK_DBLUNION_SET_UINT64(u,v) do { \
  357. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) ((v) >> 32); \
  358. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (v); \
  359. } while (0)
  360. #define DUK_DBLUNION_GET_UINT64(u) \
  361. ((((duk_uint64_t) (u)->ui[DUK_DBL_IDX_UI0]) << 32) | \
  362. ((duk_uint64_t) (u)->ui[DUK_DBL_IDX_UI1]))
  363. #else
  364. #define DUK_DBLUNION_SET_UINT64(u,v) do { \
  365. (u)->ull[DUK_DBL_IDX_ULL0] = (duk_uint64_t) (v); \
  366. } while (0)
  367. #define DUK_DBLUNION_GET_UINT64(u) ((u)->ull[DUK_DBL_IDX_ULL0])
  368. #endif
  369. #define DUK_DBLUNION_SET_INT64(u,v) DUK_DBLUNION_SET_UINT64((u), (duk_uint64_t) (v))
  370. #define DUK_DBLUNION_GET_INT64(u) ((duk_int64_t) DUK_DBLUNION_GET_UINT64((u)))
  371. #endif /* DUK_USE_64BIT_OPS */
  372. /*
  373. * Double NaN manipulation macros related to NaN normalization needed when
  374. * using the packed duk_tval representation. NaN normalization is necessary
  375. * to keep double values compatible with the duk_tval format.
  376. *
  377. * When packed duk_tval is used, the NaN space is used to store pointers
  378. * and other tagged values in addition to NaNs. Actual NaNs are normalized
  379. * to a specific quiet NaN. The macros below are used by the implementation
  380. * to check and normalize NaN values when they might be created. The macros
  381. * are essentially NOPs when the non-packed duk_tval representation is used.
  382. *
  383. * A FULL check is exact and checks all bits. A NOTFULL check is used by
  384. * the packed duk_tval and works correctly for all NaNs except those that
  385. * begin with 0x7ff0. Since the 'normalized NaN' values used with packed
  386. * duk_tval begin with 0x7ff8, the partial check is reliable when packed
  387. * duk_tval is used. The 0x7ff8 prefix means the normalized NaN will be a
  388. * quiet NaN regardless of its remaining lower bits.
  389. *
  390. * The ME variant below is specifically for ARM byte order, which has the
  391. * feature that while doubles have a mixed byte order (32107654), unsigned
  392. * long long values has a little endian byte order (76543210). When writing
  393. * a logical double value through a ULL pointer, the 32-bit words need to be
  394. * swapped; hence the #if defined()s below for ULL writes with DUK_USE_DOUBLE_ME.
  395. * This is not full ARM support but suffices for some environments.
  396. */
  397. #if defined(DUK_USE_64BIT_OPS)
  398. #if defined(DUK_USE_DOUBLE_ME)
  399. /* Macros for 64-bit ops + mixed endian doubles. */
  400. #define DUK__DBLUNION_SET_NAN_FULL(u) do { \
  401. (u)->ull[DUK_DBL_IDX_ULL0] = DUK_U64_CONSTANT(0x000000007ff80000); \
  402. } while (0)
  403. #define DUK__DBLUNION_IS_NAN_FULL(u) \
  404. ((((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x000000007ff00000)) == DUK_U64_CONSTANT(0x000000007ff00000)) && \
  405. ((((u)->ull[DUK_DBL_IDX_ULL0]) & DUK_U64_CONSTANT(0xffffffff000fffff)) != 0))
  406. #define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
  407. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x000000007ff80000))
  408. #define DUK__DBLUNION_IS_ANYINF(u) \
  409. (((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0xffffffff7fffffff)) == DUK_U64_CONSTANT(0x000000007ff00000))
  410. #define DUK__DBLUNION_IS_POSINF(u) \
  411. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x000000007ff00000))
  412. #define DUK__DBLUNION_IS_NEGINF(u) \
  413. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x00000000fff00000))
  414. #define DUK__DBLUNION_IS_ANYZERO(u) \
  415. (((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0xffffffff7fffffff)) == DUK_U64_CONSTANT(0x0000000000000000))
  416. #define DUK__DBLUNION_IS_POSZERO(u) \
  417. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x0000000000000000))
  418. #define DUK__DBLUNION_IS_NEGZERO(u) \
  419. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x0000000080000000))
  420. #else
  421. /* Macros for 64-bit ops + big/little endian doubles. */
  422. #define DUK__DBLUNION_SET_NAN_FULL(u) do { \
  423. (u)->ull[DUK_DBL_IDX_ULL0] = DUK_U64_CONSTANT(0x7ff8000000000000); \
  424. } while (0)
  425. #define DUK__DBLUNION_IS_NAN_FULL(u) \
  426. ((((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x7ff0000000000000)) == DUK_U64_CONSTANT(0x7ff0000000000000)) && \
  427. ((((u)->ull[DUK_DBL_IDX_ULL0]) & DUK_U64_CONSTANT(0x000fffffffffffff)) != 0))
  428. #define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
  429. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x7ff8000000000000))
  430. #define DUK__DBLUNION_IS_ANYINF(u) \
  431. (((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x7fffffffffffffff)) == DUK_U64_CONSTANT(0x7ff0000000000000))
  432. #define DUK__DBLUNION_IS_POSINF(u) \
  433. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x7ff0000000000000))
  434. #define DUK__DBLUNION_IS_NEGINF(u) \
  435. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0xfff0000000000000))
  436. #define DUK__DBLUNION_IS_ANYZERO(u) \
  437. (((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x7fffffffffffffff)) == DUK_U64_CONSTANT(0x0000000000000000))
  438. #define DUK__DBLUNION_IS_POSZERO(u) \
  439. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x0000000000000000))
  440. #define DUK__DBLUNION_IS_NEGZERO(u) \
  441. ((u)->ull[DUK_DBL_IDX_ULL0] == DUK_U64_CONSTANT(0x8000000000000000))
  442. #endif
  443. #else /* DUK_USE_64BIT_OPS */
  444. /* Macros for no 64-bit ops, any endianness. */
  445. #define DUK__DBLUNION_SET_NAN_FULL(u) do { \
  446. (u)->ui[DUK_DBL_IDX_UI0] = (duk_uint32_t) 0x7ff80000UL; \
  447. (u)->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) 0x00000000UL; \
  448. } while (0)
  449. #define DUK__DBLUNION_IS_NAN_FULL(u) \
  450. ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7ff00000UL) == 0x7ff00000UL) && \
  451. (((u)->ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) != 0 || \
  452. (u)->ui[DUK_DBL_IDX_UI1] != 0))
  453. #define DUK__DBLUNION_IS_NORMALIZED_NAN_FULL(u) \
  454. (((u)->ui[DUK_DBL_IDX_UI0] == 0x7ff80000UL) && \
  455. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  456. #define DUK__DBLUNION_IS_ANYINF(u) \
  457. ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7fffffffUL) == 0x7ff00000UL) && \
  458. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  459. #define DUK__DBLUNION_IS_POSINF(u) \
  460. (((u)->ui[DUK_DBL_IDX_UI0] == 0x7ff00000UL) && \
  461. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  462. #define DUK__DBLUNION_IS_NEGINF(u) \
  463. (((u)->ui[DUK_DBL_IDX_UI0] == 0xfff00000UL) && \
  464. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  465. #define DUK__DBLUNION_IS_ANYZERO(u) \
  466. ((((u)->ui[DUK_DBL_IDX_UI0] & 0x7fffffffUL) == 0x00000000UL) && \
  467. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  468. #define DUK__DBLUNION_IS_POSZERO(u) \
  469. (((u)->ui[DUK_DBL_IDX_UI0] == 0x00000000UL) && \
  470. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  471. #define DUK__DBLUNION_IS_NEGZERO(u) \
  472. (((u)->ui[DUK_DBL_IDX_UI0] == 0x80000000UL) && \
  473. ((u)->ui[DUK_DBL_IDX_UI1] == 0x00000000UL))
  474. #endif /* DUK_USE_64BIT_OPS */
  475. #define DUK__DBLUNION_SET_NAN_NOTFULL(u) do { \
  476. (u)->us[DUK_DBL_IDX_US0] = 0x7ff8UL; \
  477. } while (0)
  478. #define DUK__DBLUNION_IS_NAN_NOTFULL(u) \
  479. /* E == 0x7ff, topmost four bits of F != 0 => assume NaN */ \
  480. ((((u)->us[DUK_DBL_IDX_US0] & 0x7ff0UL) == 0x7ff0UL) && \
  481. (((u)->us[DUK_DBL_IDX_US0] & 0x000fUL) != 0x0000UL))
  482. #define DUK__DBLUNION_IS_NORMALIZED_NAN_NOTFULL(u) \
  483. /* E == 0x7ff, F == 8 => normalized NaN */ \
  484. ((u)->us[DUK_DBL_IDX_US0] == 0x7ff8UL)
  485. #define DUK__DBLUNION_NORMALIZE_NAN_CHECK_FULL(u) do { \
  486. if (DUK__DBLUNION_IS_NAN_FULL((u))) { \
  487. DUK__DBLUNION_SET_NAN_FULL((u)); \
  488. } \
  489. } while (0)
  490. #define DUK__DBLUNION_NORMALIZE_NAN_CHECK_NOTFULL(u) do { \
  491. if (DUK__DBLUNION_IS_NAN_NOTFULL((u))) { \
  492. DUK__DBLUNION_SET_NAN_NOTFULL((u)); \
  493. } \
  494. } while (0)
  495. /* Concrete macros for NaN handling used by the implementation internals.
  496. * Chosen so that they match the duk_tval representation: with a packed
  497. * duk_tval, ensure NaNs are properly normalized; with a non-packed duk_tval
  498. * these are essentially NOPs.
  499. */
  500. #if defined(DUK_USE_PACKED_TVAL)
  501. #if defined(DUK_USE_FULL_TVAL)
  502. #define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) DUK__DBLUNION_NORMALIZE_NAN_CHECK_FULL((u))
  503. #define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u))
  504. #define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NORMALIZED_NAN_FULL((u))
  505. #define DUK_DBLUNION_SET_NAN(d) DUK__DBLUNION_SET_NAN_FULL((d))
  506. #else
  507. #define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) DUK__DBLUNION_NORMALIZE_NAN_CHECK_NOTFULL((u))
  508. #define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_NOTFULL((u))
  509. #define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NORMALIZED_NAN_NOTFULL((u))
  510. #define DUK_DBLUNION_SET_NAN(d) DUK__DBLUNION_SET_NAN_NOTFULL((d))
  511. #endif
  512. #define DUK_DBLUNION_IS_NORMALIZED(u) \
  513. (!DUK_DBLUNION_IS_NAN((u)) || /* either not a NaN */ \
  514. DUK_DBLUNION_IS_NORMALIZED_NAN((u))) /* or is a normalized NaN */
  515. #else /* DUK_USE_PACKED_TVAL */
  516. #define DUK_DBLUNION_NORMALIZE_NAN_CHECK(u) /* nop: no need to normalize */
  517. #define DUK_DBLUNION_IS_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u)) /* (DUK_ISNAN((u)->d)) */
  518. #define DUK_DBLUNION_IS_NORMALIZED_NAN(u) DUK__DBLUNION_IS_NAN_FULL((u)) /* (DUK_ISNAN((u)->d)) */
  519. #define DUK_DBLUNION_IS_NORMALIZED(u) 1 /* all doubles are considered normalized */
  520. #define DUK_DBLUNION_SET_NAN(u) do { \
  521. /* in non-packed representation we don't care about which NaN is used */ \
  522. (u)->d = DUK_DOUBLE_NAN; \
  523. } while (0)
  524. #endif /* DUK_USE_PACKED_TVAL */
  525. #define DUK_DBLUNION_IS_ANYINF(u) DUK__DBLUNION_IS_ANYINF((u))
  526. #define DUK_DBLUNION_IS_POSINF(u) DUK__DBLUNION_IS_POSINF((u))
  527. #define DUK_DBLUNION_IS_NEGINF(u) DUK__DBLUNION_IS_NEGINF((u))
  528. #define DUK_DBLUNION_IS_ANYZERO(u) DUK__DBLUNION_IS_ANYZERO((u))
  529. #define DUK_DBLUNION_IS_POSZERO(u) DUK__DBLUNION_IS_POSZERO((u))
  530. #define DUK_DBLUNION_IS_NEGZERO(u) DUK__DBLUNION_IS_NEGZERO((u))
  531. /* XXX: native 64-bit byteswaps when available */
  532. /* 64-bit byteswap, same operation independent of target endianness. */
  533. #define DUK_DBLUNION_BSWAP64(u) do { \
  534. duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
  535. duk__bswaptmp1 = (u)->ui[0]; \
  536. duk__bswaptmp2 = (u)->ui[1]; \
  537. duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
  538. duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
  539. (u)->ui[0] = duk__bswaptmp2; \
  540. (u)->ui[1] = duk__bswaptmp1; \
  541. } while (0)
  542. /* Byteswap an IEEE double in the duk_double_union from host to network
  543. * order. For a big endian target this is a no-op.
  544. */
  545. #if defined(DUK_USE_DOUBLE_LE)
  546. #define DUK_DBLUNION_DOUBLE_HTON(u) do { \
  547. duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
  548. duk__bswaptmp1 = (u)->ui[0]; \
  549. duk__bswaptmp2 = (u)->ui[1]; \
  550. duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
  551. duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
  552. (u)->ui[0] = duk__bswaptmp2; \
  553. (u)->ui[1] = duk__bswaptmp1; \
  554. } while (0)
  555. #elif defined(DUK_USE_DOUBLE_ME)
  556. #define DUK_DBLUNION_DOUBLE_HTON(u) do { \
  557. duk_uint32_t duk__bswaptmp1, duk__bswaptmp2; \
  558. duk__bswaptmp1 = (u)->ui[0]; \
  559. duk__bswaptmp2 = (u)->ui[1]; \
  560. duk__bswaptmp1 = DUK_BSWAP32(duk__bswaptmp1); \
  561. duk__bswaptmp2 = DUK_BSWAP32(duk__bswaptmp2); \
  562. (u)->ui[0] = duk__bswaptmp1; \
  563. (u)->ui[1] = duk__bswaptmp2; \
  564. } while (0)
  565. #elif defined(DUK_USE_DOUBLE_BE)
  566. #define DUK_DBLUNION_DOUBLE_HTON(u) do { } while (0)
  567. #else
  568. #error internal error, double endianness insane
  569. #endif
  570. /* Reverse operation is the same. */
  571. #define DUK_DBLUNION_DOUBLE_NTOH(u) DUK_DBLUNION_DOUBLE_HTON((u))
  572. /* Some sign bit helpers. */
  573. #if defined(DUK_USE_64BIT_OPS)
  574. #define DUK_DBLUNION_HAS_SIGNBIT(u) (((u)->ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x8000000000000000)) != 0)
  575. #define DUK_DBLUNION_GET_SIGNBIT(u) (((u)->ull[DUK_DBL_IDX_ULL0] >> 63U))
  576. #else
  577. #define DUK_DBLUNION_HAS_SIGNBIT(u) (((u)->ui[DUK_DBL_IDX_UI0] & 0x80000000UL) != 0)
  578. #define DUK_DBLUNION_GET_SIGNBIT(u) (((u)->ui[DUK_DBL_IDX_UI0] >> 31U))
  579. #endif
  580. #endif /* DUK_DBLUNION_H_INCLUDED */
  581. /* #include duk_replacements.h */
  582. #line 1 "duk_replacements.h"
  583. #if !defined(DUK_REPLACEMENTS_H_INCLUDED)
  584. #define DUK_REPLACEMENTS_H_INCLUDED
  585. #if !defined(DUK_SINGLE_FILE)
  586. #if defined(DUK_USE_COMPUTED_INFINITY)
  587. DUK_INTERNAL_DECL double duk_computed_infinity;
  588. #endif
  589. #if defined(DUK_USE_COMPUTED_NAN)
  590. DUK_INTERNAL_DECL double duk_computed_nan;
  591. #endif
  592. #endif /* !DUK_SINGLE_FILE */
  593. #if defined(DUK_USE_REPL_FPCLASSIFY)
  594. DUK_INTERNAL_DECL int duk_repl_fpclassify(double x);
  595. #endif
  596. #if defined(DUK_USE_REPL_SIGNBIT)
  597. DUK_INTERNAL_DECL int duk_repl_signbit(double x);
  598. #endif
  599. #if defined(DUK_USE_REPL_ISFINITE)
  600. DUK_INTERNAL_DECL int duk_repl_isfinite(double x);
  601. #endif
  602. #if defined(DUK_USE_REPL_ISNAN)
  603. DUK_INTERNAL_DECL int duk_repl_isnan(double x);
  604. #endif
  605. #if defined(DUK_USE_REPL_ISINF)
  606. DUK_INTERNAL_DECL int duk_repl_isinf(double x);
  607. #endif
  608. #endif /* DUK_REPLACEMENTS_H_INCLUDED */
  609. /* #include duk_jmpbuf.h */
  610. #line 1 "duk_jmpbuf.h"
  611. /*
  612. * Wrapper for jmp_buf.
  613. *
  614. * This is used because jmp_buf is an array type for backward compatibility.
  615. * Wrapping jmp_buf in a struct makes pointer references, sizeof, etc,
  616. * behave more intuitively.
  617. *
  618. * http://en.wikipedia.org/wiki/Setjmp.h#Member_types
  619. */
  620. #if !defined(DUK_JMPBUF_H_INCLUDED)
  621. #define DUK_JMPBUF_H_INCLUDED
  622. #if defined(DUK_USE_CPP_EXCEPTIONS)
  623. struct duk_jmpbuf {
  624. duk_small_int_t dummy; /* unused */
  625. };
  626. #else
  627. struct duk_jmpbuf {
  628. DUK_JMPBUF_TYPE jb;
  629. };
  630. #endif
  631. #endif /* DUK_JMPBUF_H_INCLUDED */
  632. /* #include duk_exception.h */
  633. #line 1 "duk_exception.h"
  634. /*
  635. * Exceptions for Duktape internal throws when C++ exceptions are used
  636. * for long control transfers.
  637. */
  638. #if !defined(DUK_EXCEPTION_H_INCLUDED)
  639. #define DUK_EXCEPTION_H_INCLUDED
  640. #if defined(DUK_USE_CPP_EXCEPTIONS)
  641. /* Internal exception used as a setjmp-longjmp replacement. User code should
  642. * NEVER see or catch this exception, so it doesn't inherit from any base
  643. * class which should minimize the chance of user code accidentally catching
  644. * the exception.
  645. */
  646. class duk_internal_exception {
  647. /* intentionally empty */
  648. };
  649. /* Fatal error, thrown as a specific C++ exception with C++ exceptions
  650. * enabled. It is unsafe to continue; doing so may cause crashes or memory
  651. * leaks. This is intended to be either uncaught, or caught by user code
  652. * aware of the "unsafe to continue" semantics.
  653. */
  654. class duk_fatal_exception : public virtual std::runtime_error {
  655. public:
  656. duk_fatal_exception(const char *message) : std::runtime_error(message) {}
  657. };
  658. #endif
  659. #endif /* DUK_EXCEPTION_H_INCLUDED */
  660. /* #include duk_forwdecl.h */
  661. #line 1 "duk_forwdecl.h"
  662. /*
  663. * Forward declarations for all Duktape structures.
  664. */
  665. #if !defined(DUK_FORWDECL_H_INCLUDED)
  666. #define DUK_FORWDECL_H_INCLUDED
  667. /*
  668. * Forward declarations
  669. */
  670. #if defined(DUK_USE_CPP_EXCEPTIONS)
  671. class duk_internal_exception;
  672. #else
  673. struct duk_jmpbuf;
  674. #endif
  675. /* duk_tval intentionally skipped */
  676. struct duk_heaphdr;
  677. struct duk_heaphdr_string;
  678. struct duk_harray;
  679. struct duk_hstring;
  680. struct duk_hstring_external;
  681. struct duk_hobject;
  682. struct duk_hcompfunc;
  683. struct duk_hnatfunc;
  684. struct duk_hboundfunc;
  685. struct duk_hthread;
  686. struct duk_hbufobj;
  687. struct duk_hdecenv;
  688. struct duk_hobjenv;
  689. struct duk_hproxy;
  690. struct duk_hbuffer;
  691. struct duk_hbuffer_fixed;
  692. struct duk_hbuffer_dynamic;
  693. struct duk_hbuffer_external;
  694. struct duk_propaccessor;
  695. union duk_propvalue;
  696. struct duk_propdesc;
  697. struct duk_heap;
  698. struct duk_breakpoint;
  699. struct duk_activation;
  700. struct duk_catcher;
  701. struct duk_ljstate;
  702. struct duk_strcache_entry;
  703. struct duk_litcache_entry;
  704. struct duk_strtab_entry;
  705. #if defined(DUK_USE_DEBUG)
  706. struct duk_fixedbuffer;
  707. #endif
  708. struct duk_bitdecoder_ctx;
  709. struct duk_bitencoder_ctx;
  710. struct duk_bufwriter_ctx;
  711. struct duk_token;
  712. struct duk_re_token;
  713. struct duk_lexer_point;
  714. struct duk_lexer_ctx;
  715. struct duk_lexer_codepoint;
  716. struct duk_compiler_instr;
  717. struct duk_compiler_func;
  718. struct duk_compiler_ctx;
  719. struct duk_re_matcher_ctx;
  720. struct duk_re_compiler_ctx;
  721. #if defined(DUK_USE_CPP_EXCEPTIONS)
  722. /* no typedef */
  723. #else
  724. typedef struct duk_jmpbuf duk_jmpbuf;
  725. #endif
  726. /* duk_tval intentionally skipped */
  727. typedef struct duk_heaphdr duk_heaphdr;
  728. typedef struct duk_heaphdr_string duk_heaphdr_string;
  729. typedef struct duk_harray duk_harray;
  730. typedef struct duk_hstring duk_hstring;
  731. typedef struct duk_hstring_external duk_hstring_external;
  732. typedef struct duk_hobject duk_hobject;
  733. typedef struct duk_hcompfunc duk_hcompfunc;
  734. typedef struct duk_hnatfunc duk_hnatfunc;
  735. typedef struct duk_hboundfunc duk_hboundfunc;
  736. typedef struct duk_hthread duk_hthread;
  737. typedef struct duk_hbufobj duk_hbufobj;
  738. typedef struct duk_hdecenv duk_hdecenv;
  739. typedef struct duk_hobjenv duk_hobjenv;
  740. typedef struct duk_hproxy duk_hproxy;
  741. typedef struct duk_hbuffer duk_hbuffer;
  742. typedef struct duk_hbuffer_fixed duk_hbuffer_fixed;
  743. typedef struct duk_hbuffer_dynamic duk_hbuffer_dynamic;
  744. typedef struct duk_hbuffer_external duk_hbuffer_external;
  745. typedef struct duk_propaccessor duk_propaccessor;
  746. typedef union duk_propvalue duk_propvalue;
  747. typedef struct duk_propdesc duk_propdesc;
  748. typedef struct duk_heap duk_heap;
  749. typedef struct duk_breakpoint duk_breakpoint;
  750. typedef struct duk_activation duk_activation;
  751. typedef struct duk_catcher duk_catcher;
  752. typedef struct duk_ljstate duk_ljstate;
  753. typedef struct duk_strcache_entry duk_strcache_entry;
  754. typedef struct duk_litcache_entry duk_litcache_entry;
  755. typedef struct duk_strtab_entry duk_strtab_entry;
  756. #if defined(DUK_USE_DEBUG)
  757. typedef struct duk_fixedbuffer duk_fixedbuffer;
  758. #endif
  759. typedef struct duk_bitdecoder_ctx duk_bitdecoder_ctx;
  760. typedef struct duk_bitencoder_ctx duk_bitencoder_ctx;
  761. typedef struct duk_bufwriter_ctx duk_bufwriter_ctx;
  762. typedef struct duk_token duk_token;
  763. typedef struct duk_re_token duk_re_token;
  764. typedef struct duk_lexer_point duk_lexer_point;
  765. typedef struct duk_lexer_ctx duk_lexer_ctx;
  766. typedef struct duk_lexer_codepoint duk_lexer_codepoint;
  767. typedef struct duk_compiler_instr duk_compiler_instr;
  768. typedef struct duk_compiler_func duk_compiler_func;
  769. typedef struct duk_compiler_ctx duk_compiler_ctx;
  770. typedef struct duk_re_matcher_ctx duk_re_matcher_ctx;
  771. typedef struct duk_re_compiler_ctx duk_re_compiler_ctx;
  772. #endif /* DUK_FORWDECL_H_INCLUDED */
  773. /* #include duk_tval.h */
  774. #line 1 "duk_tval.h"
  775. /*
  776. * Tagged type definition (duk_tval) and accessor macros.
  777. *
  778. * Access all fields through the accessor macros, as the representation
  779. * is quite tricky.
  780. *
  781. * There are two packed type alternatives: an 8-byte representation
  782. * based on an IEEE double (preferred for compactness), and a 12-byte
  783. * representation (portability). The latter is needed also in e.g.
  784. * 64-bit environments (it usually pads to 16 bytes per value).
  785. *
  786. * Selecting the tagged type format involves many trade-offs (memory
  787. * use, size and performance of generated code, portability, etc).
  788. *
  789. * NB: because macro arguments are often expressions, macros should
  790. * avoid evaluating their argument more than once.
  791. */
  792. #if !defined(DUK_TVAL_H_INCLUDED)
  793. #define DUK_TVAL_H_INCLUDED
  794. /* sanity */
  795. #if !defined(DUK_USE_DOUBLE_LE) && !defined(DUK_USE_DOUBLE_ME) && !defined(DUK_USE_DOUBLE_BE)
  796. #error unsupported: cannot determine byte order variant
  797. #endif
  798. #if defined(DUK_USE_PACKED_TVAL)
  799. /* ======================================================================== */
  800. /*
  801. * Packed 8-byte representation
  802. */
  803. /* use duk_double_union as duk_tval directly */
  804. typedef union duk_double_union duk_tval;
  805. typedef struct {
  806. duk_uint16_t a;
  807. duk_uint16_t b;
  808. duk_uint16_t c;
  809. duk_uint16_t d;
  810. } duk_tval_unused;
  811. /* tags */
  812. #define DUK_TAG_NORMALIZED_NAN 0x7ff8UL /* the NaN variant we use */
  813. /* avoid tag 0xfff0, no risk of confusion with negative infinity */
  814. #define DUK_TAG_MIN 0xfff1UL
  815. #if defined(DUK_USE_FASTINT)
  816. #define DUK_TAG_FASTINT 0xfff1UL /* embed: integer value */
  817. #endif
  818. #define DUK_TAG_UNUSED 0xfff2UL /* marker; not actual tagged value */
  819. #define DUK_TAG_UNDEFINED 0xfff3UL /* embed: nothing */
  820. #define DUK_TAG_NULL 0xfff4UL /* embed: nothing */
  821. #define DUK_TAG_BOOLEAN 0xfff5UL /* embed: 0 or 1 (false or true) */
  822. /* DUK_TAG_NUMBER would logically go here, but it has multiple 'tags' */
  823. #define DUK_TAG_POINTER 0xfff6UL /* embed: void ptr */
  824. #define DUK_TAG_LIGHTFUNC 0xfff7UL /* embed: func ptr */
  825. #define DUK_TAG_STRING 0xfff8UL /* embed: duk_hstring ptr */
  826. #define DUK_TAG_OBJECT 0xfff9UL /* embed: duk_hobject ptr */
  827. #define DUK_TAG_BUFFER 0xfffaUL /* embed: duk_hbuffer ptr */
  828. #define DUK_TAG_MAX 0xfffaUL
  829. /* for convenience */
  830. #define DUK_XTAG_BOOLEAN_FALSE 0xfff50000UL
  831. #define DUK_XTAG_BOOLEAN_TRUE 0xfff50001UL
  832. #define DUK_TVAL_IS_VALID_TAG(tv) \
  833. (DUK_TVAL_GET_TAG((tv)) - DUK_TAG_MIN <= DUK_TAG_MAX - DUK_TAG_MIN)
  834. /* DUK_TVAL_UNUSED initializer for duk_tval_unused, works for any endianness. */
  835. #define DUK_TVAL_UNUSED_INITIALIZER() \
  836. { DUK_TAG_UNUSED, DUK_TAG_UNUSED, DUK_TAG_UNUSED, DUK_TAG_UNUSED }
  837. /* two casts to avoid gcc warning: "warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]" */
  838. #if defined(DUK_USE_64BIT_OPS)
  839. #if defined(DUK_USE_DOUBLE_ME)
  840. #define DUK__TVAL_SET_TAGGEDPOINTER(tv,h,tag) do { \
  841. (tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 16) | (((duk_uint64_t) (duk_uint32_t) (h)) << 32); \
  842. } while (0)
  843. #else
  844. #define DUK__TVAL_SET_TAGGEDPOINTER(tv,h,tag) do { \
  845. (tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) (tag)) << 48) | ((duk_uint64_t) (duk_uint32_t) (h)); \
  846. } while (0)
  847. #endif
  848. #else /* DUK_USE_64BIT_OPS */
  849. #define DUK__TVAL_SET_TAGGEDPOINTER(tv,h,tag) do { \
  850. duk_tval *duk__tv; \
  851. duk__tv = (tv); \
  852. duk__tv->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) (tag)) << 16; \
  853. duk__tv->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (h); \
  854. } while (0)
  855. #endif /* DUK_USE_64BIT_OPS */
  856. #if defined(DUK_USE_64BIT_OPS)
  857. /* Double casting for pointer to avoid gcc warning (cast from pointer to integer of different size) */
  858. #if defined(DUK_USE_DOUBLE_ME)
  859. #define DUK__TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \
  860. (tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 16) | \
  861. ((duk_uint64_t) (flags)) | \
  862. (((duk_uint64_t) (duk_uint32_t) (fp)) << 32); \
  863. } while (0)
  864. #else
  865. #define DUK__TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \
  866. (tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_LIGHTFUNC) << 48) | \
  867. (((duk_uint64_t) (flags)) << 32) | \
  868. ((duk_uint64_t) (duk_uint32_t) (fp)); \
  869. } while (0)
  870. #endif
  871. #else /* DUK_USE_64BIT_OPS */
  872. #define DUK__TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \
  873. duk_tval *duk__tv; \
  874. duk__tv = (tv); \
  875. duk__tv->ui[DUK_DBL_IDX_UI0] = (((duk_uint32_t) DUK_TAG_LIGHTFUNC) << 16) | ((duk_uint32_t) (flags)); \
  876. duk__tv->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (fp); \
  877. } while (0)
  878. #endif /* DUK_USE_64BIT_OPS */
  879. #if defined(DUK_USE_FASTINT)
  880. /* Note: masking is done for 'i' to deal with negative numbers correctly */
  881. #if defined(DUK_USE_DOUBLE_ME)
  882. #define DUK__TVAL_SET_I48(tv,i) do { \
  883. duk_tval *duk__tv; \
  884. duk__tv = (tv); \
  885. duk__tv->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16 | (((duk_uint32_t) ((i) >> 32)) & 0x0000ffffUL); \
  886. duk__tv->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \
  887. } while (0)
  888. #define DUK__TVAL_SET_U32(tv,i) do { \
  889. duk_tval *duk__tv; \
  890. duk__tv = (tv); \
  891. duk__tv->ui[DUK_DBL_IDX_UI0] = ((duk_uint32_t) DUK_TAG_FASTINT) << 16; \
  892. duk__tv->ui[DUK_DBL_IDX_UI1] = (duk_uint32_t) (i); \
  893. } while (0)
  894. #else
  895. #define DUK__TVAL_SET_I48(tv,i) do { \
  896. (tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & DUK_U64_CONSTANT(0x0000ffffffffffff)); \
  897. } while (0)
  898. #define DUK__TVAL_SET_U32(tv,i) do { \
  899. (tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (duk_uint64_t) (i); \
  900. } while (0)
  901. #endif
  902. /* This needs to go through a cast because sign extension is needed. */
  903. #define DUK__TVAL_SET_I32(tv,i) do { \
  904. duk_int64_t duk__tmp = (duk_int64_t) (i); \
  905. DUK_TVAL_SET_I48((tv), duk__tmp); \
  906. } while (0)
  907. /* XXX: Clumsy sign extend and masking of 16 topmost bits. */
  908. #if defined(DUK_USE_DOUBLE_ME)
  909. #define DUK__TVAL_GET_FASTINT(tv) (((duk_int64_t) ((((duk_uint64_t) (tv)->ui[DUK_DBL_IDX_UI0]) << 32) | ((duk_uint64_t) (tv)->ui[DUK_DBL_IDX_UI1]))) << 16 >> 16)
  910. #else
  911. #define DUK__TVAL_GET_FASTINT(tv) ((((duk_int64_t) (tv)->ull[DUK_DBL_IDX_ULL0]) << 16) >> 16)
  912. #endif
  913. #define DUK__TVAL_GET_FASTINT_U32(tv) ((tv)->ui[DUK_DBL_IDX_UI1])
  914. #define DUK__TVAL_GET_FASTINT_I32(tv) ((duk_int32_t) (tv)->ui[DUK_DBL_IDX_UI1])
  915. #endif /* DUK_USE_FASTINT */
  916. #define DUK_TVAL_SET_UNDEFINED(tv) do { \
  917. (tv)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNDEFINED; \
  918. } while (0)
  919. #define DUK_TVAL_SET_UNUSED(tv) do { \
  920. (tv)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_UNUSED; \
  921. } while (0)
  922. #define DUK_TVAL_SET_NULL(tv) do { \
  923. (tv)->us[DUK_DBL_IDX_US0] = (duk_uint16_t) DUK_TAG_NULL; \
  924. } while (0)
  925. #define DUK_TVAL_SET_BOOLEAN(tv,val) DUK_DBLUNION_SET_HIGH32((tv), (((duk_uint32_t) DUK_TAG_BOOLEAN) << 16) | ((duk_uint32_t) (val)))
  926. #define DUK_TVAL_SET_NAN(tv) DUK_DBLUNION_SET_NAN_FULL((tv))
  927. /* Assumes that caller has normalized NaNs, otherwise trouble ahead. */
  928. #if defined(DUK_USE_FASTINT)
  929. #define DUK_TVAL_SET_DOUBLE(tv,d) do { \
  930. duk_double_t duk__dblval; \
  931. duk__dblval = (d); \
  932. DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \
  933. DUK_DBLUNION_SET_DOUBLE((tv), duk__dblval); \
  934. } while (0)
  935. #define DUK_TVAL_SET_I48(tv,i) DUK__TVAL_SET_I48((tv), (i))
  936. #define DUK_TVAL_SET_I32(tv,i) DUK__TVAL_SET_I32((tv), (i))
  937. #define DUK_TVAL_SET_U32(tv,i) DUK__TVAL_SET_U32((tv), (i))
  938. #define DUK_TVAL_SET_NUMBER_CHKFAST_FAST(tv,d) duk_tval_set_number_chkfast_fast((tv), (d))
  939. #define DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(tv,d) duk_tval_set_number_chkfast_slow((tv), (d))
  940. #define DUK_TVAL_SET_NUMBER(tv,d) DUK_TVAL_SET_DOUBLE((tv), (d))
  941. #define DUK_TVAL_CHKFAST_INPLACE_FAST(tv) do { \
  942. duk_tval *duk__tv; \
  943. duk_double_t duk__d; \
  944. duk__tv = (tv); \
  945. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  946. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  947. DUK_TVAL_SET_NUMBER_CHKFAST_FAST(duk__tv, duk__d); \
  948. } \
  949. } while (0)
  950. #define DUK_TVAL_CHKFAST_INPLACE_SLOW(tv) do { \
  951. duk_tval *duk__tv; \
  952. duk_double_t duk__d; \
  953. duk__tv = (tv); \
  954. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  955. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  956. DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(duk__tv, duk__d); \
  957. } \
  958. } while (0)
  959. #else /* DUK_USE_FASTINT */
  960. #define DUK_TVAL_SET_DOUBLE(tv,d) do { \
  961. duk_double_t duk__dblval; \
  962. duk__dblval = (d); \
  963. DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); \
  964. DUK_DBLUNION_SET_DOUBLE((tv), duk__dblval); \
  965. } while (0)
  966. #define DUK_TVAL_SET_I48(tv,i) DUK_TVAL_SET_DOUBLE((tv), (duk_double_t) (i)) /* XXX: fast int-to-double */
  967. #define DUK_TVAL_SET_I32(tv,i) DUK_TVAL_SET_DOUBLE((tv), (duk_double_t) (i))
  968. #define DUK_TVAL_SET_U32(tv,i) DUK_TVAL_SET_DOUBLE((tv), (duk_double_t) (i))
  969. #define DUK_TVAL_SET_NUMBER_CHKFAST_FAST(tv,d) DUK_TVAL_SET_DOUBLE((tv), (d))
  970. #define DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(tv,d) DUK_TVAL_SET_DOUBLE((tv), (d))
  971. #define DUK_TVAL_SET_NUMBER(tv,d) DUK_TVAL_SET_DOUBLE((tv), (d))
  972. #define DUK_TVAL_CHKFAST_INPLACE_FAST(tv) do { } while (0)
  973. #define DUK_TVAL_CHKFAST_INPLACE_SLOW(tv) do { } while (0)
  974. #endif /* DUK_USE_FASTINT */
  975. #define DUK_TVAL_SET_FASTINT(tv,i) DUK_TVAL_SET_I48((tv), (i)) /* alias */
  976. #define DUK_TVAL_SET_LIGHTFUNC(tv,fp,flags) DUK__TVAL_SET_LIGHTFUNC((tv), (fp), (flags))
  977. #define DUK_TVAL_SET_STRING(tv,h) DUK__TVAL_SET_TAGGEDPOINTER((tv), (h), DUK_TAG_STRING)
  978. #define DUK_TVAL_SET_OBJECT(tv,h) DUK__TVAL_SET_TAGGEDPOINTER((tv), (h), DUK_TAG_OBJECT)
  979. #define DUK_TVAL_SET_BUFFER(tv,h) DUK__TVAL_SET_TAGGEDPOINTER((tv), (h), DUK_TAG_BUFFER)
  980. #define DUK_TVAL_SET_POINTER(tv,p) DUK__TVAL_SET_TAGGEDPOINTER((tv), (p), DUK_TAG_POINTER)
  981. #define DUK_TVAL_SET_TVAL(tv,x) do { *(tv) = *(x); } while (0)
  982. /* getters */
  983. #define DUK_TVAL_GET_BOOLEAN(tv) ((duk_small_uint_t) (tv)->us[DUK_DBL_IDX_US1])
  984. #if defined(DUK_USE_FASTINT)
  985. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->d)
  986. #define DUK_TVAL_GET_FASTINT(tv) DUK__TVAL_GET_FASTINT((tv))
  987. #define DUK_TVAL_GET_FASTINT_U32(tv) DUK__TVAL_GET_FASTINT_U32((tv))
  988. #define DUK_TVAL_GET_FASTINT_I32(tv) DUK__TVAL_GET_FASTINT_I32((tv))
  989. #define DUK_TVAL_GET_NUMBER(tv) duk_tval_get_number_packed((tv))
  990. #else
  991. #define DUK_TVAL_GET_NUMBER(tv) ((tv)->d)
  992. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->d)
  993. #endif
  994. #define DUK_TVAL_GET_LIGHTFUNC(tv,out_fp,out_flags) do { \
  995. (out_flags) = (tv)->ui[DUK_DBL_IDX_UI0] & 0xffffUL; \
  996. (out_fp) = (duk_c_function) (tv)->ui[DUK_DBL_IDX_UI1]; \
  997. } while (0)
  998. #define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv) ((duk_c_function) ((tv)->ui[DUK_DBL_IDX_UI1]))
  999. #define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv) (((duk_small_uint_t) (tv)->ui[DUK_DBL_IDX_UI0]) & 0xffffUL)
  1000. #define DUK_TVAL_GET_STRING(tv) ((duk_hstring *) (tv)->vp[DUK_DBL_IDX_VP1])
  1001. #define DUK_TVAL_GET_OBJECT(tv) ((duk_hobject *) (tv)->vp[DUK_DBL_IDX_VP1])
  1002. #define DUK_TVAL_GET_BUFFER(tv) ((duk_hbuffer *) (tv)->vp[DUK_DBL_IDX_VP1])
  1003. #define DUK_TVAL_GET_POINTER(tv) ((void *) (tv)->vp[DUK_DBL_IDX_VP1])
  1004. #define DUK_TVAL_GET_HEAPHDR(tv) ((duk_heaphdr *) (tv)->vp[DUK_DBL_IDX_VP1])
  1005. /* decoding */
  1006. #define DUK_TVAL_GET_TAG(tv) ((duk_small_uint_t) (tv)->us[DUK_DBL_IDX_US0])
  1007. #define DUK_TVAL_IS_UNDEFINED(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_UNDEFINED)
  1008. #define DUK_TVAL_IS_UNUSED(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_UNUSED)
  1009. #define DUK_TVAL_IS_NULL(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_NULL)
  1010. #define DUK_TVAL_IS_BOOLEAN(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_BOOLEAN)
  1011. #define DUK_TVAL_IS_BOOLEAN_TRUE(tv) ((tv)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_TRUE)
  1012. #define DUK_TVAL_IS_BOOLEAN_FALSE(tv) ((tv)->ui[DUK_DBL_IDX_UI0] == DUK_XTAG_BOOLEAN_FALSE)
  1013. #define DUK_TVAL_IS_LIGHTFUNC(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_LIGHTFUNC)
  1014. #define DUK_TVAL_IS_STRING(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_STRING)
  1015. #define DUK_TVAL_IS_OBJECT(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_OBJECT)
  1016. #define DUK_TVAL_IS_BUFFER(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_BUFFER)
  1017. #define DUK_TVAL_IS_POINTER(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_POINTER)
  1018. #if defined(DUK_USE_FASTINT)
  1019. /* 0xfff0 is -Infinity */
  1020. #define DUK_TVAL_IS_DOUBLE(tv) (DUK_TVAL_GET_TAG((tv)) <= 0xfff0UL)
  1021. #define DUK_TVAL_IS_FASTINT(tv) (DUK_TVAL_GET_TAG((tv)) == DUK_TAG_FASTINT)
  1022. #define DUK_TVAL_IS_NUMBER(tv) (DUK_TVAL_GET_TAG((tv)) <= 0xfff1UL)
  1023. #else
  1024. #define DUK_TVAL_IS_NUMBER(tv) (DUK_TVAL_GET_TAG((tv)) <= 0xfff0UL)
  1025. #define DUK_TVAL_IS_DOUBLE(tv) DUK_TVAL_IS_NUMBER((tv))
  1026. #endif
  1027. /* This is performance critical because it appears in every DECREF. */
  1028. #define DUK_TVAL_IS_HEAP_ALLOCATED(tv) (DUK_TVAL_GET_TAG((tv)) >= DUK_TAG_STRING)
  1029. #if defined(DUK_USE_FASTINT)
  1030. DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_packed(duk_tval *tv);
  1031. #endif
  1032. #else /* DUK_USE_PACKED_TVAL */
  1033. /* ======================================================================== */
  1034. /*
  1035. * Portable 12-byte representation
  1036. */
  1037. /* Note: not initializing all bytes is normally not an issue: Duktape won't
  1038. * read or use the uninitialized bytes so valgrind won't issue warnings.
  1039. * In some special cases a harmless valgrind warning may be issued though.
  1040. * For example, the DumpHeap debugger command writes out a compiled function's
  1041. * 'data' area as is, including any uninitialized bytes, which causes a
  1042. * valgrind warning.
  1043. */
  1044. typedef struct duk_tval_struct duk_tval;
  1045. struct duk_tval_struct {
  1046. duk_small_uint_t t;
  1047. duk_small_uint_t v_extra;
  1048. union {
  1049. duk_double_t d;
  1050. duk_small_int_t i;
  1051. #if defined(DUK_USE_FASTINT)
  1052. duk_int64_t fi; /* if present, forces 16-byte duk_tval */
  1053. #endif
  1054. void *voidptr;
  1055. duk_hstring *hstring;
  1056. duk_hobject *hobject;
  1057. duk_hcompfunc *hcompfunc;
  1058. duk_hnatfunc *hnatfunc;
  1059. duk_hthread *hthread;
  1060. duk_hbuffer *hbuffer;
  1061. duk_heaphdr *heaphdr;
  1062. duk_c_function lightfunc;
  1063. } v;
  1064. };
  1065. typedef struct {
  1066. duk_small_uint_t t;
  1067. duk_small_uint_t v_extra;
  1068. /* The rest of the fields don't matter except for debug dumps and such
  1069. * for which a partial initializer may trigger out-ot-bounds memory
  1070. * reads. Include a double field which is usually as large or larger
  1071. * than pointers (not always however).
  1072. */
  1073. duk_double_t d;
  1074. } duk_tval_unused;
  1075. #define DUK_TVAL_UNUSED_INITIALIZER() \
  1076. { DUK_TAG_UNUSED, 0, 0.0 }
  1077. #define DUK_TAG_MIN 0
  1078. #define DUK_TAG_NUMBER 0 /* DUK_TAG_NUMBER only defined for non-packed duk_tval */
  1079. #if defined(DUK_USE_FASTINT)
  1080. #define DUK_TAG_FASTINT 1
  1081. #endif
  1082. #define DUK_TAG_UNDEFINED 2
  1083. #define DUK_TAG_NULL 3
  1084. #define DUK_TAG_BOOLEAN 4
  1085. #define DUK_TAG_POINTER 5
  1086. #define DUK_TAG_LIGHTFUNC 6
  1087. #define DUK_TAG_UNUSED 7 /* marker; not actual tagged type */
  1088. #define DUK_TAG_STRING 8 /* first heap allocated, match bit boundary */
  1089. #define DUK_TAG_OBJECT 9
  1090. #define DUK_TAG_BUFFER 10
  1091. #define DUK_TAG_MAX 10
  1092. #define DUK_TVAL_IS_VALID_TAG(tv) \
  1093. (DUK_TVAL_GET_TAG((tv)) - DUK_TAG_MIN <= DUK_TAG_MAX - DUK_TAG_MIN)
  1094. /* DUK_TAG_NUMBER is intentionally first, as it is the default clause in code
  1095. * to support the 8-byte representation. Further, it is a non-heap-allocated
  1096. * type so it should come before DUK_TAG_STRING. Finally, it should not break
  1097. * the tag value ranges covered by case-clauses in a switch-case.
  1098. */
  1099. /* setters */
  1100. #define DUK_TVAL_SET_UNDEFINED(tv) do { \
  1101. duk_tval *duk__tv; \
  1102. duk__tv = (tv); \
  1103. duk__tv->t = DUK_TAG_UNDEFINED; \
  1104. } while (0)
  1105. #define DUK_TVAL_SET_UNUSED(tv) do { \
  1106. duk_tval *duk__tv; \
  1107. duk__tv = (tv); \
  1108. duk__tv->t = DUK_TAG_UNUSED; \
  1109. } while (0)
  1110. #define DUK_TVAL_SET_NULL(tv) do { \
  1111. duk_tval *duk__tv; \
  1112. duk__tv = (tv); \
  1113. duk__tv->t = DUK_TAG_NULL; \
  1114. } while (0)
  1115. #define DUK_TVAL_SET_BOOLEAN(tv,val) do { \
  1116. duk_tval *duk__tv; \
  1117. duk__tv = (tv); \
  1118. duk__tv->t = DUK_TAG_BOOLEAN; \
  1119. duk__tv->v.i = (duk_small_int_t) (val); \
  1120. } while (0)
  1121. #if defined(DUK_USE_FASTINT)
  1122. #define DUK_TVAL_SET_DOUBLE(tv,val) do { \
  1123. duk_tval *duk__tv; \
  1124. duk_double_t duk__dblval; \
  1125. duk__dblval = (val); \
  1126. DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); /* nop for unpacked duk_tval */ \
  1127. duk__tv = (tv); \
  1128. duk__tv->t = DUK_TAG_NUMBER; \
  1129. duk__tv->v.d = duk__dblval; \
  1130. } while (0)
  1131. #define DUK_TVAL_SET_I48(tv,val) do { \
  1132. duk_tval *duk__tv; \
  1133. duk__tv = (tv); \
  1134. duk__tv->t = DUK_TAG_FASTINT; \
  1135. duk__tv->v.fi = (val); \
  1136. } while (0)
  1137. #define DUK_TVAL_SET_U32(tv,val) do { \
  1138. duk_tval *duk__tv; \
  1139. duk__tv = (tv); \
  1140. duk__tv->t = DUK_TAG_FASTINT; \
  1141. duk__tv->v.fi = (duk_int64_t) (val); \
  1142. } while (0)
  1143. #define DUK_TVAL_SET_I32(tv,val) do { \
  1144. duk_tval *duk__tv; \
  1145. duk__tv = (tv); \
  1146. duk__tv->t = DUK_TAG_FASTINT; \
  1147. duk__tv->v.fi = (duk_int64_t) (val); \
  1148. } while (0)
  1149. #define DUK_TVAL_SET_NUMBER_CHKFAST_FAST(tv,d) \
  1150. duk_tval_set_number_chkfast_fast((tv), (d))
  1151. #define DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(tv,d) \
  1152. duk_tval_set_number_chkfast_slow((tv), (d))
  1153. #define DUK_TVAL_SET_NUMBER(tv,val) \
  1154. DUK_TVAL_SET_DOUBLE((tv), (val))
  1155. #define DUK_TVAL_CHKFAST_INPLACE_FAST(tv) do { \
  1156. duk_tval *duk__tv; \
  1157. duk_double_t duk__d; \
  1158. duk__tv = (tv); \
  1159. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  1160. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  1161. DUK_TVAL_SET_NUMBER_CHKFAST_FAST(duk__tv, duk__d); \
  1162. } \
  1163. } while (0)
  1164. #define DUK_TVAL_CHKFAST_INPLACE_SLOW(tv) do { \
  1165. duk_tval *duk__tv; \
  1166. duk_double_t duk__d; \
  1167. duk__tv = (tv); \
  1168. if (DUK_TVAL_IS_DOUBLE(duk__tv)) { \
  1169. duk__d = DUK_TVAL_GET_DOUBLE(duk__tv); \
  1170. DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(duk__tv, duk__d); \
  1171. } \
  1172. } while (0)
  1173. #else /* DUK_USE_FASTINT */
  1174. #define DUK_TVAL_SET_DOUBLE(tv,d) \
  1175. DUK_TVAL_SET_NUMBER((tv), (d))
  1176. #define DUK_TVAL_SET_I48(tv,val) \
  1177. DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val)) /* XXX: fast int-to-double */
  1178. #define DUK_TVAL_SET_U32(tv,val) \
  1179. DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val))
  1180. #define DUK_TVAL_SET_I32(tv,val) \
  1181. DUK_TVAL_SET_NUMBER((tv), (duk_double_t) (val))
  1182. #define DUK_TVAL_SET_NUMBER(tv,val) do { \
  1183. duk_tval *duk__tv; \
  1184. duk_double_t duk__dblval; \
  1185. duk__dblval = (val); \
  1186. DUK_ASSERT_DOUBLE_IS_NORMALIZED(duk__dblval); /* nop for unpacked duk_tval */ \
  1187. duk__tv = (tv); \
  1188. duk__tv->t = DUK_TAG_NUMBER; \
  1189. duk__tv->v.d = duk__dblval; \
  1190. } while (0)
  1191. #define DUK_TVAL_SET_NUMBER_CHKFAST_FAST(tv,d) \
  1192. DUK_TVAL_SET_NUMBER((tv), (d))
  1193. #define DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(tv,d) \
  1194. DUK_TVAL_SET_NUMBER((tv), (d))
  1195. #define DUK_TVAL_CHKFAST_INPLACE_FAST(tv) do { } while (0)
  1196. #define DUK_TVAL_CHKFAST_INPLACE_SLOW(tv) do { } while (0)
  1197. #endif /* DUK_USE_FASTINT */
  1198. #define DUK_TVAL_SET_FASTINT(tv,i) \
  1199. DUK_TVAL_SET_I48((tv), (i)) /* alias */
  1200. #define DUK_TVAL_SET_POINTER(tv,hptr) do { \
  1201. duk_tval *duk__tv; \
  1202. duk__tv = (tv); \
  1203. duk__tv->t = DUK_TAG_POINTER; \
  1204. duk__tv->v.voidptr = (hptr); \
  1205. } while (0)
  1206. #define DUK_TVAL_SET_LIGHTFUNC(tv,fp,flags) do { \
  1207. duk_tval *duk__tv; \
  1208. duk__tv = (tv); \
  1209. duk__tv->t = DUK_TAG_LIGHTFUNC; \
  1210. duk__tv->v_extra = (flags); \
  1211. duk__tv->v.lightfunc = (duk_c_function) (fp); \
  1212. } while (0)
  1213. #define DUK_TVAL_SET_STRING(tv,hptr) do { \
  1214. duk_tval *duk__tv; \
  1215. duk__tv = (tv); \
  1216. duk__tv->t = DUK_TAG_STRING; \
  1217. duk__tv->v.hstring = (hptr); \
  1218. } while (0)
  1219. #define DUK_TVAL_SET_OBJECT(tv,hptr) do { \
  1220. duk_tval *duk__tv; \
  1221. duk__tv = (tv); \
  1222. duk__tv->t = DUK_TAG_OBJECT; \
  1223. duk__tv->v.hobject = (hptr); \
  1224. } while (0)
  1225. #define DUK_TVAL_SET_BUFFER(tv,hptr) do { \
  1226. duk_tval *duk__tv; \
  1227. duk__tv = (tv); \
  1228. duk__tv->t = DUK_TAG_BUFFER; \
  1229. duk__tv->v.hbuffer = (hptr); \
  1230. } while (0)
  1231. #define DUK_TVAL_SET_NAN(tv) do { \
  1232. /* in non-packed representation we don't care about which NaN is used */ \
  1233. duk_tval *duk__tv; \
  1234. duk__tv = (tv); \
  1235. duk__tv->t = DUK_TAG_NUMBER; \
  1236. duk__tv->v.d = DUK_DOUBLE_NAN; \
  1237. } while (0)
  1238. #define DUK_TVAL_SET_TVAL(tv,x) do { *(tv) = *(x); } while (0)
  1239. /* getters */
  1240. #define DUK_TVAL_GET_BOOLEAN(tv) ((duk_small_uint_t) (tv)->v.i)
  1241. #if defined(DUK_USE_FASTINT)
  1242. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d)
  1243. #define DUK_TVAL_GET_FASTINT(tv) ((tv)->v.fi)
  1244. #define DUK_TVAL_GET_FASTINT_U32(tv) ((duk_uint32_t) ((tv)->v.fi))
  1245. #define DUK_TVAL_GET_FASTINT_I32(tv) ((duk_int32_t) ((tv)->v.fi))
  1246. #if 0
  1247. #define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \
  1248. (duk_double_t) DUK_TVAL_GET_FASTINT((tv)) : \
  1249. DUK_TVAL_GET_DOUBLE((tv)))
  1250. #define DUK_TVAL_GET_NUMBER(tv) duk_tval_get_number_unpacked((tv))
  1251. #else
  1252. /* This seems reasonable overall. */
  1253. #define DUK_TVAL_GET_NUMBER(tv) (DUK_TVAL_IS_FASTINT((tv)) ? \
  1254. duk_tval_get_number_unpacked_fastint((tv)) : \
  1255. DUK_TVAL_GET_DOUBLE((tv)))
  1256. #endif
  1257. #else
  1258. #define DUK_TVAL_GET_NUMBER(tv) ((tv)->v.d)
  1259. #define DUK_TVAL_GET_DOUBLE(tv) ((tv)->v.d)
  1260. #endif /* DUK_USE_FASTINT */
  1261. #define DUK_TVAL_GET_POINTER(tv) ((tv)->v.voidptr)
  1262. #define DUK_TVAL_GET_LIGHTFUNC(tv,out_fp,out_flags) do { \
  1263. (out_flags) = (duk_uint32_t) (tv)->v_extra; \
  1264. (out_fp) = (tv)->v.lightfunc; \
  1265. } while (0)
  1266. #define DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv) ((tv)->v.lightfunc)
  1267. #define DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv) ((duk_small_uint_t) ((tv)->v_extra))
  1268. #define DUK_TVAL_GET_STRING(tv) ((tv)->v.hstring)
  1269. #define DUK_TVAL_GET_OBJECT(tv) ((tv)->v.hobject)
  1270. #define DUK_TVAL_GET_BUFFER(tv) ((tv)->v.hbuffer)
  1271. #define DUK_TVAL_GET_HEAPHDR(tv) ((tv)->v.heaphdr)
  1272. /* decoding */
  1273. #define DUK_TVAL_GET_TAG(tv) ((tv)->t)
  1274. #define DUK_TVAL_IS_UNDEFINED(tv) ((tv)->t == DUK_TAG_UNDEFINED)
  1275. #define DUK_TVAL_IS_UNUSED(tv) ((tv)->t == DUK_TAG_UNUSED)
  1276. #define DUK_TVAL_IS_NULL(tv) ((tv)->t == DUK_TAG_NULL)
  1277. #define DUK_TVAL_IS_BOOLEAN(tv) ((tv)->t == DUK_TAG_BOOLEAN)
  1278. #define DUK_TVAL_IS_BOOLEAN_TRUE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i != 0))
  1279. #define DUK_TVAL_IS_BOOLEAN_FALSE(tv) (((tv)->t == DUK_TAG_BOOLEAN) && ((tv)->v.i == 0))
  1280. #if defined(DUK_USE_FASTINT)
  1281. #define DUK_TVAL_IS_DOUBLE(tv) ((tv)->t == DUK_TAG_NUMBER)
  1282. #define DUK_TVAL_IS_FASTINT(tv) ((tv)->t == DUK_TAG_FASTINT)
  1283. #define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK_TAG_NUMBER || \
  1284. (tv)->t == DUK_TAG_FASTINT)
  1285. #else
  1286. #define DUK_TVAL_IS_NUMBER(tv) ((tv)->t == DUK_TAG_NUMBER)
  1287. #define DUK_TVAL_IS_DOUBLE(tv) DUK_TVAL_IS_NUMBER((tv))
  1288. #endif /* DUK_USE_FASTINT */
  1289. #define DUK_TVAL_IS_POINTER(tv) ((tv)->t == DUK_TAG_POINTER)
  1290. #define DUK_TVAL_IS_LIGHTFUNC(tv) ((tv)->t == DUK_TAG_LIGHTFUNC)
  1291. #define DUK_TVAL_IS_STRING(tv) ((tv)->t == DUK_TAG_STRING)
  1292. #define DUK_TVAL_IS_OBJECT(tv) ((tv)->t == DUK_TAG_OBJECT)
  1293. #define DUK_TVAL_IS_BUFFER(tv) ((tv)->t == DUK_TAG_BUFFER)
  1294. /* This is performance critical because it's needed for every DECREF.
  1295. * Take advantage of the fact that the first heap allocated tag is 8,
  1296. * so that bit 3 is set for all heap allocated tags (and never set for
  1297. * non-heap-allocated tags).
  1298. */
  1299. #if 0
  1300. #define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t >= DUK_TAG_STRING)
  1301. #endif
  1302. #define DUK_TVAL_IS_HEAP_ALLOCATED(tv) ((tv)->t & 0x08)
  1303. #if defined(DUK_USE_FASTINT)
  1304. #if 0
  1305. DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked(duk_tval *tv);
  1306. #endif
  1307. DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv);
  1308. #endif
  1309. #endif /* DUK_USE_PACKED_TVAL */
  1310. /*
  1311. * Convenience (independent of representation)
  1312. */
  1313. #define DUK_TVAL_SET_BOOLEAN_TRUE(tv) DUK_TVAL_SET_BOOLEAN((tv), 1)
  1314. #define DUK_TVAL_SET_BOOLEAN_FALSE(tv) DUK_TVAL_SET_BOOLEAN((tv), 0)
  1315. #define DUK_TVAL_STRING_IS_SYMBOL(tv) \
  1316. DUK_HSTRING_HAS_SYMBOL(DUK_TVAL_GET_STRING((tv)))
  1317. /* Lightfunc flags packing and unpacking. */
  1318. /* Sign extend: 0x0000##00 -> 0x##000000 -> sign extend to 0xssssss##.
  1319. * Avoid signed shifts due to portability limitations.
  1320. */
  1321. #define DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags) \
  1322. ((duk_int32_t) (duk_int8_t) (((duk_uint16_t) (lf_flags)) >> 8))
  1323. #define DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags) \
  1324. (((lf_flags) >> 4) & 0x0fU)
  1325. #define DUK_LFUNC_FLAGS_GET_NARGS(lf_flags) \
  1326. ((lf_flags) & 0x0fU)
  1327. #define DUK_LFUNC_FLAGS_PACK(magic,length,nargs) \
  1328. ((((duk_small_uint_t) (magic)) & 0xffU) << 8) | ((length) << 4) | (nargs)
  1329. #define DUK_LFUNC_NARGS_VARARGS 0x0f /* varargs marker */
  1330. #define DUK_LFUNC_NARGS_MIN 0x00
  1331. #define DUK_LFUNC_NARGS_MAX 0x0e /* max, excl. varargs marker */
  1332. #define DUK_LFUNC_LENGTH_MIN 0x00
  1333. #define DUK_LFUNC_LENGTH_MAX 0x0f
  1334. #define DUK_LFUNC_MAGIC_MIN (-0x80)
  1335. #define DUK_LFUNC_MAGIC_MAX 0x7f
  1336. /* fastint constants etc */
  1337. #if defined(DUK_USE_FASTINT)
  1338. #define DUK_FASTINT_MIN (DUK_I64_CONSTANT(-0x800000000000))
  1339. #define DUK_FASTINT_MAX (DUK_I64_CONSTANT(0x7fffffffffff))
  1340. #define DUK_FASTINT_BITS 48
  1341. DUK_INTERNAL_DECL void duk_tval_set_number_chkfast_fast(duk_tval *tv, duk_double_t x);
  1342. DUK_INTERNAL_DECL void duk_tval_set_number_chkfast_slow(duk_tval *tv, duk_double_t x);
  1343. #endif
  1344. #endif /* DUK_TVAL_H_INCLUDED */
  1345. /* #include duk_builtins.h */
  1346. #line 1 "duk_builtins.h"
  1347. /*
  1348. * Automatically generated by genbuiltins.py, do not edit!
  1349. */
  1350. #if !defined(DUK_BUILTINS_H_INCLUDED)
  1351. #define DUK_BUILTINS_H_INCLUDED
  1352. #if defined(DUK_USE_ROM_STRINGS)
  1353. #error ROM support not enabled, rerun configure.py with --rom-support
  1354. #else /* DUK_USE_ROM_STRINGS */
  1355. #define DUK_STRIDX_UC_UNDEFINED 0 /* 'Undefined' */
  1356. #define DUK_HEAP_STRING_UC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_UNDEFINED)
  1357. #define DUK_HTHREAD_STRING_UC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_UNDEFINED)
  1358. #define DUK_STRIDX_UC_NULL 1 /* 'Null' */
  1359. #define DUK_HEAP_STRING_UC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NULL)
  1360. #define DUK_HTHREAD_STRING_UC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NULL)
  1361. #define DUK_STRIDX_UC_SYMBOL 2 /* 'Symbol' */
  1362. #define DUK_HEAP_STRING_UC_SYMBOL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_SYMBOL)
  1363. #define DUK_HTHREAD_STRING_UC_SYMBOL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_SYMBOL)
  1364. #define DUK_STRIDX_UC_ARGUMENTS 3 /* 'Arguments' */
  1365. #define DUK_HEAP_STRING_UC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ARGUMENTS)
  1366. #define DUK_HTHREAD_STRING_UC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ARGUMENTS)
  1367. #define DUK_STRIDX_UC_OBJECT 4 /* 'Object' */
  1368. #define DUK_HEAP_STRING_UC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_OBJECT)
  1369. #define DUK_HTHREAD_STRING_UC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_OBJECT)
  1370. #define DUK_STRIDX_UC_FUNCTION 5 /* 'Function' */
  1371. #define DUK_HEAP_STRING_UC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_FUNCTION)
  1372. #define DUK_HTHREAD_STRING_UC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_FUNCTION)
  1373. #define DUK_STRIDX_ARRAY 6 /* 'Array' */
  1374. #define DUK_HEAP_STRING_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY)
  1375. #define DUK_HTHREAD_STRING_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY)
  1376. #define DUK_STRIDX_UC_STRING 7 /* 'String' */
  1377. #define DUK_HEAP_STRING_UC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_STRING)
  1378. #define DUK_HTHREAD_STRING_UC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_STRING)
  1379. #define DUK_STRIDX_UC_BOOLEAN 8 /* 'Boolean' */
  1380. #define DUK_HEAP_STRING_UC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BOOLEAN)
  1381. #define DUK_HTHREAD_STRING_UC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BOOLEAN)
  1382. #define DUK_STRIDX_UC_NUMBER 9 /* 'Number' */
  1383. #define DUK_HEAP_STRING_UC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_NUMBER)
  1384. #define DUK_HTHREAD_STRING_UC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_NUMBER)
  1385. #define DUK_STRIDX_DATE 10 /* 'Date' */
  1386. #define DUK_HEAP_STRING_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATE)
  1387. #define DUK_HTHREAD_STRING_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATE)
  1388. #define DUK_STRIDX_REG_EXP 11 /* 'RegExp' */
  1389. #define DUK_HEAP_STRING_REG_EXP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_REG_EXP)
  1390. #define DUK_HTHREAD_STRING_REG_EXP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_REG_EXP)
  1391. #define DUK_STRIDX_UC_ERROR 12 /* 'Error' */
  1392. #define DUK_HEAP_STRING_UC_ERROR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_ERROR)
  1393. #define DUK_HTHREAD_STRING_UC_ERROR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_ERROR)
  1394. #define DUK_STRIDX_MATH 13 /* 'Math' */
  1395. #define DUK_HEAP_STRING_MATH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MATH)
  1396. #define DUK_HTHREAD_STRING_MATH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MATH)
  1397. #define DUK_STRIDX_JSON 14 /* 'JSON' */
  1398. #define DUK_HEAP_STRING_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON)
  1399. #define DUK_HTHREAD_STRING_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON)
  1400. #define DUK_STRIDX_EMPTY_STRING 15 /* '' */
  1401. #define DUK_HEAP_STRING_EMPTY_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EMPTY_STRING)
  1402. #define DUK_HTHREAD_STRING_EMPTY_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EMPTY_STRING)
  1403. #define DUK_STRIDX_ARRAY_BUFFER 16 /* 'ArrayBuffer' */
  1404. #define DUK_HEAP_STRING_ARRAY_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ARRAY_BUFFER)
  1405. #define DUK_HTHREAD_STRING_ARRAY_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ARRAY_BUFFER)
  1406. #define DUK_STRIDX_DATA_VIEW 17 /* 'DataView' */
  1407. #define DUK_HEAP_STRING_DATA_VIEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA_VIEW)
  1408. #define DUK_HTHREAD_STRING_DATA_VIEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA_VIEW)
  1409. #define DUK_STRIDX_INT8_ARRAY 18 /* 'Int8Array' */
  1410. #define DUK_HEAP_STRING_INT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT8_ARRAY)
  1411. #define DUK_HTHREAD_STRING_INT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT8_ARRAY)
  1412. #define DUK_STRIDX_UINT8_ARRAY 19 /* 'Uint8Array' */
  1413. #define DUK_HEAP_STRING_UINT8_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_ARRAY)
  1414. #define DUK_HTHREAD_STRING_UINT8_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_ARRAY)
  1415. #define DUK_STRIDX_UINT8_CLAMPED_ARRAY 20 /* 'Uint8ClampedArray' */
  1416. #define DUK_HEAP_STRING_UINT8_CLAMPED_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  1417. #define DUK_HTHREAD_STRING_UINT8_CLAMPED_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT8_CLAMPED_ARRAY)
  1418. #define DUK_STRIDX_INT16_ARRAY 21 /* 'Int16Array' */
  1419. #define DUK_HEAP_STRING_INT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT16_ARRAY)
  1420. #define DUK_HTHREAD_STRING_INT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT16_ARRAY)
  1421. #define DUK_STRIDX_UINT16_ARRAY 22 /* 'Uint16Array' */
  1422. #define DUK_HEAP_STRING_UINT16_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT16_ARRAY)
  1423. #define DUK_HTHREAD_STRING_UINT16_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT16_ARRAY)
  1424. #define DUK_STRIDX_INT32_ARRAY 23 /* 'Int32Array' */
  1425. #define DUK_HEAP_STRING_INT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT32_ARRAY)
  1426. #define DUK_HTHREAD_STRING_INT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT32_ARRAY)
  1427. #define DUK_STRIDX_UINT32_ARRAY 24 /* 'Uint32Array' */
  1428. #define DUK_HEAP_STRING_UINT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UINT32_ARRAY)
  1429. #define DUK_HTHREAD_STRING_UINT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UINT32_ARRAY)
  1430. #define DUK_STRIDX_FLOAT32_ARRAY 25 /* 'Float32Array' */
  1431. #define DUK_HEAP_STRING_FLOAT32_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT32_ARRAY)
  1432. #define DUK_HTHREAD_STRING_FLOAT32_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT32_ARRAY)
  1433. #define DUK_STRIDX_FLOAT64_ARRAY 26 /* 'Float64Array' */
  1434. #define DUK_HEAP_STRING_FLOAT64_ARRAY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLOAT64_ARRAY)
  1435. #define DUK_HTHREAD_STRING_FLOAT64_ARRAY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLOAT64_ARRAY)
  1436. #define DUK_STRIDX_GLOBAL 27 /* 'global' */
  1437. #define DUK_HEAP_STRING_GLOBAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GLOBAL)
  1438. #define DUK_HTHREAD_STRING_GLOBAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GLOBAL)
  1439. #define DUK_STRIDX_OBJ_ENV 28 /* 'ObjEnv' */
  1440. #define DUK_HEAP_STRING_OBJ_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OBJ_ENV)
  1441. #define DUK_HTHREAD_STRING_OBJ_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OBJ_ENV)
  1442. #define DUK_STRIDX_DEC_ENV 29 /* 'DecEnv' */
  1443. #define DUK_HEAP_STRING_DEC_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEC_ENV)
  1444. #define DUK_HTHREAD_STRING_DEC_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEC_ENV)
  1445. #define DUK_STRIDX_UC_BUFFER 30 /* 'Buffer' */
  1446. #define DUK_HEAP_STRING_UC_BUFFER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_BUFFER)
  1447. #define DUK_HTHREAD_STRING_UC_BUFFER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_BUFFER)
  1448. #define DUK_STRIDX_UC_POINTER 31 /* 'Pointer' */
  1449. #define DUK_HEAP_STRING_UC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_POINTER)
  1450. #define DUK_HTHREAD_STRING_UC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_POINTER)
  1451. #define DUK_STRIDX_UC_THREAD 32 /* 'Thread' */
  1452. #define DUK_HEAP_STRING_UC_THREAD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_UC_THREAD)
  1453. #define DUK_HTHREAD_STRING_UC_THREAD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_UC_THREAD)
  1454. #define DUK_STRIDX_EVAL 33 /* 'eval' */
  1455. #define DUK_HEAP_STRING_EVAL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EVAL)
  1456. #define DUK_HTHREAD_STRING_EVAL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EVAL)
  1457. #define DUK_STRIDX_VALUE 34 /* 'value' */
  1458. #define DUK_HEAP_STRING_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE)
  1459. #define DUK_HTHREAD_STRING_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE)
  1460. #define DUK_STRIDX_WRITABLE 35 /* 'writable' */
  1461. #define DUK_HEAP_STRING_WRITABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WRITABLE)
  1462. #define DUK_HTHREAD_STRING_WRITABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WRITABLE)
  1463. #define DUK_STRIDX_CONFIGURABLE 36 /* 'configurable' */
  1464. #define DUK_HEAP_STRING_CONFIGURABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONFIGURABLE)
  1465. #define DUK_HTHREAD_STRING_CONFIGURABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONFIGURABLE)
  1466. #define DUK_STRIDX_ENUMERABLE 37 /* 'enumerable' */
  1467. #define DUK_HEAP_STRING_ENUMERABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUMERABLE)
  1468. #define DUK_HTHREAD_STRING_ENUMERABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUMERABLE)
  1469. #define DUK_STRIDX_JOIN 38 /* 'join' */
  1470. #define DUK_HEAP_STRING_JOIN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JOIN)
  1471. #define DUK_HTHREAD_STRING_JOIN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JOIN)
  1472. #define DUK_STRIDX_TO_LOCALE_STRING 39 /* 'toLocaleString' */
  1473. #define DUK_HEAP_STRING_TO_LOCALE_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_LOCALE_STRING)
  1474. #define DUK_HTHREAD_STRING_TO_LOCALE_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_LOCALE_STRING)
  1475. #define DUK_STRIDX_VALUE_OF 40 /* 'valueOf' */
  1476. #define DUK_HEAP_STRING_VALUE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VALUE_OF)
  1477. #define DUK_HTHREAD_STRING_VALUE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VALUE_OF)
  1478. #define DUK_STRIDX_TO_UTC_STRING 41 /* 'toUTCString' */
  1479. #define DUK_HEAP_STRING_TO_UTC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_UTC_STRING)
  1480. #define DUK_HTHREAD_STRING_TO_UTC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_UTC_STRING)
  1481. #define DUK_STRIDX_TO_ISO_STRING 42 /* 'toISOString' */
  1482. #define DUK_HEAP_STRING_TO_ISO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_ISO_STRING)
  1483. #define DUK_HTHREAD_STRING_TO_ISO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_ISO_STRING)
  1484. #define DUK_STRIDX_TO_GMT_STRING 43 /* 'toGMTString' */
  1485. #define DUK_HEAP_STRING_TO_GMT_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_GMT_STRING)
  1486. #define DUK_HTHREAD_STRING_TO_GMT_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_GMT_STRING)
  1487. #define DUK_STRIDX_SOURCE 44 /* 'source' */
  1488. #define DUK_HEAP_STRING_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SOURCE)
  1489. #define DUK_HTHREAD_STRING_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SOURCE)
  1490. #define DUK_STRIDX_IGNORE_CASE 45 /* 'ignoreCase' */
  1491. #define DUK_HEAP_STRING_IGNORE_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IGNORE_CASE)
  1492. #define DUK_HTHREAD_STRING_IGNORE_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IGNORE_CASE)
  1493. #define DUK_STRIDX_MULTILINE 46 /* 'multiline' */
  1494. #define DUK_HEAP_STRING_MULTILINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MULTILINE)
  1495. #define DUK_HTHREAD_STRING_MULTILINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MULTILINE)
  1496. #define DUK_STRIDX_LAST_INDEX 47 /* 'lastIndex' */
  1497. #define DUK_HEAP_STRING_LAST_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LAST_INDEX)
  1498. #define DUK_HTHREAD_STRING_LAST_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LAST_INDEX)
  1499. #define DUK_STRIDX_FLAGS 48 /* 'flags' */
  1500. #define DUK_HEAP_STRING_FLAGS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FLAGS)
  1501. #define DUK_HTHREAD_STRING_FLAGS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FLAGS)
  1502. #define DUK_STRIDX_INDEX 49 /* 'index' */
  1503. #define DUK_HEAP_STRING_INDEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INDEX)
  1504. #define DUK_HTHREAD_STRING_INDEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INDEX)
  1505. #define DUK_STRIDX_PROTOTYPE 50 /* 'prototype' */
  1506. #define DUK_HEAP_STRING_PROTOTYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTOTYPE)
  1507. #define DUK_HTHREAD_STRING_PROTOTYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTOTYPE)
  1508. #define DUK_STRIDX_CONSTRUCTOR 51 /* 'constructor' */
  1509. #define DUK_HEAP_STRING_CONSTRUCTOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCTOR)
  1510. #define DUK_HTHREAD_STRING_CONSTRUCTOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCTOR)
  1511. #define DUK_STRIDX_MESSAGE 52 /* 'message' */
  1512. #define DUK_HEAP_STRING_MESSAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MESSAGE)
  1513. #define DUK_HTHREAD_STRING_MESSAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MESSAGE)
  1514. #define DUK_STRIDX_LC_BOOLEAN 53 /* 'boolean' */
  1515. #define DUK_HEAP_STRING_LC_BOOLEAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_BOOLEAN)
  1516. #define DUK_HTHREAD_STRING_LC_BOOLEAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_BOOLEAN)
  1517. #define DUK_STRIDX_LC_NUMBER 54 /* 'number' */
  1518. #define DUK_HEAP_STRING_LC_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NUMBER)
  1519. #define DUK_HTHREAD_STRING_LC_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NUMBER)
  1520. #define DUK_STRIDX_LC_STRING 55 /* 'string' */
  1521. #define DUK_HEAP_STRING_LC_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_STRING)
  1522. #define DUK_HTHREAD_STRING_LC_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_STRING)
  1523. #define DUK_STRIDX_LC_SYMBOL 56 /* 'symbol' */
  1524. #define DUK_HEAP_STRING_LC_SYMBOL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_SYMBOL)
  1525. #define DUK_HTHREAD_STRING_LC_SYMBOL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_SYMBOL)
  1526. #define DUK_STRIDX_LC_OBJECT 57 /* 'object' */
  1527. #define DUK_HEAP_STRING_LC_OBJECT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_OBJECT)
  1528. #define DUK_HTHREAD_STRING_LC_OBJECT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_OBJECT)
  1529. #define DUK_STRIDX_LC_UNDEFINED 58 /* 'undefined' */
  1530. #define DUK_HEAP_STRING_LC_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_UNDEFINED)
  1531. #define DUK_HTHREAD_STRING_LC_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_UNDEFINED)
  1532. #define DUK_STRIDX_NAN 59 /* 'NaN' */
  1533. #define DUK_HEAP_STRING_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAN)
  1534. #define DUK_HTHREAD_STRING_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAN)
  1535. #define DUK_STRIDX_INFINITY 60 /* 'Infinity' */
  1536. #define DUK_HEAP_STRING_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INFINITY)
  1537. #define DUK_HTHREAD_STRING_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INFINITY)
  1538. #define DUK_STRIDX_MINUS_INFINITY 61 /* '-Infinity' */
  1539. #define DUK_HEAP_STRING_MINUS_INFINITY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_INFINITY)
  1540. #define DUK_HTHREAD_STRING_MINUS_INFINITY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_INFINITY)
  1541. #define DUK_STRIDX_MINUS_ZERO 62 /* '-0' */
  1542. #define DUK_HEAP_STRING_MINUS_ZERO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_MINUS_ZERO)
  1543. #define DUK_HTHREAD_STRING_MINUS_ZERO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_MINUS_ZERO)
  1544. #define DUK_STRIDX_COMMA 63 /* ',' */
  1545. #define DUK_HEAP_STRING_COMMA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMMA)
  1546. #define DUK_HTHREAD_STRING_COMMA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMMA)
  1547. #define DUK_STRIDX_NEWLINE_4SPACE 64 /* '\n ' */
  1548. #define DUK_HEAP_STRING_NEWLINE_4SPACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEWLINE_4SPACE)
  1549. #define DUK_HTHREAD_STRING_NEWLINE_4SPACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEWLINE_4SPACE)
  1550. #define DUK_STRIDX_BRACKETED_ELLIPSIS 65 /* '[...]' */
  1551. #define DUK_HEAP_STRING_BRACKETED_ELLIPSIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BRACKETED_ELLIPSIS)
  1552. #define DUK_HTHREAD_STRING_BRACKETED_ELLIPSIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BRACKETED_ELLIPSIS)
  1553. #define DUK_STRIDX_INVALID_DATE 66 /* 'Invalid Date' */
  1554. #define DUK_HEAP_STRING_INVALID_DATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INVALID_DATE)
  1555. #define DUK_HTHREAD_STRING_INVALID_DATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INVALID_DATE)
  1556. #define DUK_STRIDX_LC_ARGUMENTS 67 /* 'arguments' */
  1557. #define DUK_HEAP_STRING_LC_ARGUMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_ARGUMENTS)
  1558. #define DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_ARGUMENTS)
  1559. #define DUK_STRIDX_CALLEE 68 /* 'callee' */
  1560. #define DUK_HEAP_STRING_CALLEE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLEE)
  1561. #define DUK_HTHREAD_STRING_CALLEE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLEE)
  1562. #define DUK_STRIDX_CALLER 69 /* 'caller' */
  1563. #define DUK_HEAP_STRING_CALLER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CALLER)
  1564. #define DUK_HTHREAD_STRING_CALLER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CALLER)
  1565. #define DUK_STRIDX_APPLY 70 /* 'apply' */
  1566. #define DUK_HEAP_STRING_APPLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_APPLY)
  1567. #define DUK_HTHREAD_STRING_APPLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_APPLY)
  1568. #define DUK_STRIDX_CONSTRUCT 71 /* 'construct' */
  1569. #define DUK_HEAP_STRING_CONSTRUCT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONSTRUCT)
  1570. #define DUK_HTHREAD_STRING_CONSTRUCT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONSTRUCT)
  1571. #define DUK_STRIDX_DELETE_PROPERTY 72 /* 'deleteProperty' */
  1572. #define DUK_HEAP_STRING_DELETE_PROPERTY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE_PROPERTY)
  1573. #define DUK_HTHREAD_STRING_DELETE_PROPERTY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE_PROPERTY)
  1574. #define DUK_STRIDX_GET 73 /* 'get' */
  1575. #define DUK_HEAP_STRING_GET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_GET)
  1576. #define DUK_HTHREAD_STRING_GET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_GET)
  1577. #define DUK_STRIDX_HAS 74 /* 'has' */
  1578. #define DUK_HEAP_STRING_HAS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HAS)
  1579. #define DUK_HTHREAD_STRING_HAS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HAS)
  1580. #define DUK_STRIDX_OWN_KEYS 75 /* 'ownKeys' */
  1581. #define DUK_HEAP_STRING_OWN_KEYS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_OWN_KEYS)
  1582. #define DUK_HTHREAD_STRING_OWN_KEYS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_OWN_KEYS)
  1583. #define DUK_STRIDX_WELLKNOWN_SYMBOL_TO_PRIMITIVE 76 /* '\x81Symbol.toPrimitive\xff' */
  1584. #define DUK_HEAP_STRING_WELLKNOWN_SYMBOL_TO_PRIMITIVE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WELLKNOWN_SYMBOL_TO_PRIMITIVE)
  1585. #define DUK_HTHREAD_STRING_WELLKNOWN_SYMBOL_TO_PRIMITIVE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WELLKNOWN_SYMBOL_TO_PRIMITIVE)
  1586. #define DUK_STRIDX_WELLKNOWN_SYMBOL_HAS_INSTANCE 77 /* '\x81Symbol.hasInstance\xff' */
  1587. #define DUK_HEAP_STRING_WELLKNOWN_SYMBOL_HAS_INSTANCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WELLKNOWN_SYMBOL_HAS_INSTANCE)
  1588. #define DUK_HTHREAD_STRING_WELLKNOWN_SYMBOL_HAS_INSTANCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WELLKNOWN_SYMBOL_HAS_INSTANCE)
  1589. #define DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG 78 /* '\x81Symbol.toStringTag\xff' */
  1590. #define DUK_HEAP_STRING_WELLKNOWN_SYMBOL_TO_STRING_TAG(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG)
  1591. #define DUK_HTHREAD_STRING_WELLKNOWN_SYMBOL_TO_STRING_TAG(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG)
  1592. #define DUK_STRIDX_WELLKNOWN_SYMBOL_IS_CONCAT_SPREADABLE 79 /* '\x81Symbol.isConcatSpreadable\xff' */
  1593. #define DUK_HEAP_STRING_WELLKNOWN_SYMBOL_IS_CONCAT_SPREADABLE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WELLKNOWN_SYMBOL_IS_CONCAT_SPREADABLE)
  1594. #define DUK_HTHREAD_STRING_WELLKNOWN_SYMBOL_IS_CONCAT_SPREADABLE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WELLKNOWN_SYMBOL_IS_CONCAT_SPREADABLE)
  1595. #define DUK_STRIDX_SET_PROTOTYPE_OF 80 /* 'setPrototypeOf' */
  1596. #define DUK_HEAP_STRING_SET_PROTOTYPE_OF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET_PROTOTYPE_OF)
  1597. #define DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET_PROTOTYPE_OF)
  1598. #define DUK_STRIDX___PROTO__ 81 /* '__proto__' */
  1599. #define DUK_HEAP_STRING___PROTO__(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX___PROTO__)
  1600. #define DUK_HTHREAD_STRING___PROTO__(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX___PROTO__)
  1601. #define DUK_STRIDX_TO_STRING 82 /* 'toString' */
  1602. #define DUK_HEAP_STRING_TO_STRING(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_STRING)
  1603. #define DUK_HTHREAD_STRING_TO_STRING(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_STRING)
  1604. #define DUK_STRIDX_TO_JSON 83 /* 'toJSON' */
  1605. #define DUK_HEAP_STRING_TO_JSON(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TO_JSON)
  1606. #define DUK_HTHREAD_STRING_TO_JSON(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TO_JSON)
  1607. #define DUK_STRIDX_TYPE 84 /* 'type' */
  1608. #define DUK_HEAP_STRING_TYPE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPE)
  1609. #define DUK_HTHREAD_STRING_TYPE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPE)
  1610. #define DUK_STRIDX_DATA 85 /* 'data' */
  1611. #define DUK_HEAP_STRING_DATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DATA)
  1612. #define DUK_HTHREAD_STRING_DATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DATA)
  1613. #define DUK_STRIDX_LENGTH 86 /* 'length' */
  1614. #define DUK_HEAP_STRING_LENGTH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LENGTH)
  1615. #define DUK_HTHREAD_STRING_LENGTH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LENGTH)
  1616. #define DUK_STRIDX_SET 87 /* 'set' */
  1617. #define DUK_HEAP_STRING_SET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SET)
  1618. #define DUK_HTHREAD_STRING_SET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SET)
  1619. #define DUK_STRIDX_STACK 88 /* 'stack' */
  1620. #define DUK_HEAP_STRING_STACK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STACK)
  1621. #define DUK_HTHREAD_STRING_STACK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STACK)
  1622. #define DUK_STRIDX_PC 89 /* 'pc' */
  1623. #define DUK_HEAP_STRING_PC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PC)
  1624. #define DUK_HTHREAD_STRING_PC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PC)
  1625. #define DUK_STRIDX_LINE_NUMBER 90 /* 'lineNumber' */
  1626. #define DUK_HEAP_STRING_LINE_NUMBER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LINE_NUMBER)
  1627. #define DUK_HTHREAD_STRING_LINE_NUMBER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LINE_NUMBER)
  1628. #define DUK_STRIDX_INT_TRACEDATA 91 /* '\x82Tracedata' */
  1629. #define DUK_HEAP_STRING_INT_TRACEDATA(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TRACEDATA)
  1630. #define DUK_HTHREAD_STRING_INT_TRACEDATA(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TRACEDATA)
  1631. #define DUK_STRIDX_NAME 92 /* 'name' */
  1632. #define DUK_HEAP_STRING_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NAME)
  1633. #define DUK_HTHREAD_STRING_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NAME)
  1634. #define DUK_STRIDX_FILE_NAME 93 /* 'fileName' */
  1635. #define DUK_HEAP_STRING_FILE_NAME(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FILE_NAME)
  1636. #define DUK_HTHREAD_STRING_FILE_NAME(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FILE_NAME)
  1637. #define DUK_STRIDX_LC_POINTER 94 /* 'pointer' */
  1638. #define DUK_HEAP_STRING_LC_POINTER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_POINTER)
  1639. #define DUK_HTHREAD_STRING_LC_POINTER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_POINTER)
  1640. #define DUK_STRIDX_INT_TARGET 95 /* '\x82Target' */
  1641. #define DUK_HEAP_STRING_INT_TARGET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_TARGET)
  1642. #define DUK_HTHREAD_STRING_INT_TARGET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_TARGET)
  1643. #define DUK_STRIDX_INT_NEXT 96 /* '\x82Next' */
  1644. #define DUK_HEAP_STRING_INT_NEXT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_NEXT)
  1645. #define DUK_HTHREAD_STRING_INT_NEXT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_NEXT)
  1646. #define DUK_STRIDX_INT_BYTECODE 97 /* '\x82Bytecode' */
  1647. #define DUK_HEAP_STRING_INT_BYTECODE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_BYTECODE)
  1648. #define DUK_HTHREAD_STRING_INT_BYTECODE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_BYTECODE)
  1649. #define DUK_STRIDX_INT_FORMALS 98 /* '\x82Formals' */
  1650. #define DUK_HEAP_STRING_INT_FORMALS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FORMALS)
  1651. #define DUK_HTHREAD_STRING_INT_FORMALS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FORMALS)
  1652. #define DUK_STRIDX_INT_VARMAP 99 /* '\x82Varmap' */
  1653. #define DUK_HEAP_STRING_INT_VARMAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARMAP)
  1654. #define DUK_HTHREAD_STRING_INT_VARMAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARMAP)
  1655. #define DUK_STRIDX_INT_SOURCE 100 /* '\x82Source' */
  1656. #define DUK_HEAP_STRING_INT_SOURCE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_SOURCE)
  1657. #define DUK_HTHREAD_STRING_INT_SOURCE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_SOURCE)
  1658. #define DUK_STRIDX_INT_PC2LINE 101 /* '\x82Pc2line' */
  1659. #define DUK_HEAP_STRING_INT_PC2LINE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_PC2LINE)
  1660. #define DUK_HTHREAD_STRING_INT_PC2LINE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_PC2LINE)
  1661. #define DUK_STRIDX_INT_MAP 102 /* '\x82Map' */
  1662. #define DUK_HEAP_STRING_INT_MAP(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_MAP)
  1663. #define DUK_HTHREAD_STRING_INT_MAP(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_MAP)
  1664. #define DUK_STRIDX_INT_VARENV 103 /* '\x82Varenv' */
  1665. #define DUK_HEAP_STRING_INT_VARENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VARENV)
  1666. #define DUK_HTHREAD_STRING_INT_VARENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VARENV)
  1667. #define DUK_STRIDX_INT_FINALIZER 104 /* '\x82Finalizer' */
  1668. #define DUK_HEAP_STRING_INT_FINALIZER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_FINALIZER)
  1669. #define DUK_HTHREAD_STRING_INT_FINALIZER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_FINALIZER)
  1670. #define DUK_STRIDX_INT_VALUE 105 /* '\x82Value' */
  1671. #define DUK_HEAP_STRING_INT_VALUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INT_VALUE)
  1672. #define DUK_HTHREAD_STRING_INT_VALUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INT_VALUE)
  1673. #define DUK_STRIDX_COMPILE 106 /* 'compile' */
  1674. #define DUK_HEAP_STRING_COMPILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_COMPILE)
  1675. #define DUK_HTHREAD_STRING_COMPILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_COMPILE)
  1676. #define DUK_STRIDX_INPUT 107 /* 'input' */
  1677. #define DUK_HEAP_STRING_INPUT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INPUT)
  1678. #define DUK_HTHREAD_STRING_INPUT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INPUT)
  1679. #define DUK_STRIDX_ERR_CREATE 108 /* 'errCreate' */
  1680. #define DUK_HEAP_STRING_ERR_CREATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_CREATE)
  1681. #define DUK_HTHREAD_STRING_ERR_CREATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_CREATE)
  1682. #define DUK_STRIDX_ERR_THROW 109 /* 'errThrow' */
  1683. #define DUK_HEAP_STRING_ERR_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ERR_THROW)
  1684. #define DUK_HTHREAD_STRING_ERR_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ERR_THROW)
  1685. #define DUK_STRIDX_ENV 110 /* 'env' */
  1686. #define DUK_HEAP_STRING_ENV(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENV)
  1687. #define DUK_HTHREAD_STRING_ENV(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENV)
  1688. #define DUK_STRIDX_HEX 111 /* 'hex' */
  1689. #define DUK_HEAP_STRING_HEX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_HEX)
  1690. #define DUK_HTHREAD_STRING_HEX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_HEX)
  1691. #define DUK_STRIDX_BASE64 112 /* 'base64' */
  1692. #define DUK_HEAP_STRING_BASE64(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BASE64)
  1693. #define DUK_HTHREAD_STRING_BASE64(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BASE64)
  1694. #define DUK_STRIDX_JX 113 /* 'jx' */
  1695. #define DUK_HEAP_STRING_JX(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JX)
  1696. #define DUK_HTHREAD_STRING_JX(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JX)
  1697. #define DUK_STRIDX_JC 114 /* 'jc' */
  1698. #define DUK_HEAP_STRING_JC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JC)
  1699. #define DUK_HTHREAD_STRING_JC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JC)
  1700. #define DUK_STRIDX_JSON_EXT_UNDEFINED 115 /* '{"_undef":true}' */
  1701. #define DUK_HEAP_STRING_JSON_EXT_UNDEFINED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_UNDEFINED)
  1702. #define DUK_HTHREAD_STRING_JSON_EXT_UNDEFINED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_UNDEFINED)
  1703. #define DUK_STRIDX_JSON_EXT_NAN 116 /* '{"_nan":true}' */
  1704. #define DUK_HEAP_STRING_JSON_EXT_NAN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NAN)
  1705. #define DUK_HTHREAD_STRING_JSON_EXT_NAN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NAN)
  1706. #define DUK_STRIDX_JSON_EXT_POSINF 117 /* '{"_inf":true}' */
  1707. #define DUK_HEAP_STRING_JSON_EXT_POSINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_POSINF)
  1708. #define DUK_HTHREAD_STRING_JSON_EXT_POSINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_POSINF)
  1709. #define DUK_STRIDX_JSON_EXT_NEGINF 118 /* '{"_ninf":true}' */
  1710. #define DUK_HEAP_STRING_JSON_EXT_NEGINF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_NEGINF)
  1711. #define DUK_HTHREAD_STRING_JSON_EXT_NEGINF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_NEGINF)
  1712. #define DUK_STRIDX_JSON_EXT_FUNCTION1 119 /* '{"_func":true}' */
  1713. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION1(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION1)
  1714. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION1(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION1)
  1715. #define DUK_STRIDX_JSON_EXT_FUNCTION2 120 /* '{_func:true}' */
  1716. #define DUK_HEAP_STRING_JSON_EXT_FUNCTION2(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_JSON_EXT_FUNCTION2)
  1717. #define DUK_HTHREAD_STRING_JSON_EXT_FUNCTION2(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_JSON_EXT_FUNCTION2)
  1718. #define DUK_STRIDX_BREAK 121 /* 'break' */
  1719. #define DUK_HEAP_STRING_BREAK(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_BREAK)
  1720. #define DUK_HTHREAD_STRING_BREAK(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_BREAK)
  1721. #define DUK_STRIDX_CASE 122 /* 'case' */
  1722. #define DUK_HEAP_STRING_CASE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CASE)
  1723. #define DUK_HTHREAD_STRING_CASE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CASE)
  1724. #define DUK_STRIDX_CATCH 123 /* 'catch' */
  1725. #define DUK_HEAP_STRING_CATCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CATCH)
  1726. #define DUK_HTHREAD_STRING_CATCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CATCH)
  1727. #define DUK_STRIDX_CONTINUE 124 /* 'continue' */
  1728. #define DUK_HEAP_STRING_CONTINUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONTINUE)
  1729. #define DUK_HTHREAD_STRING_CONTINUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONTINUE)
  1730. #define DUK_STRIDX_DEBUGGER 125 /* 'debugger' */
  1731. #define DUK_HEAP_STRING_DEBUGGER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEBUGGER)
  1732. #define DUK_HTHREAD_STRING_DEBUGGER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEBUGGER)
  1733. #define DUK_STRIDX_DEFAULT 126 /* 'default' */
  1734. #define DUK_HEAP_STRING_DEFAULT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DEFAULT)
  1735. #define DUK_HTHREAD_STRING_DEFAULT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DEFAULT)
  1736. #define DUK_STRIDX_DELETE 127 /* 'delete' */
  1737. #define DUK_HEAP_STRING_DELETE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DELETE)
  1738. #define DUK_HTHREAD_STRING_DELETE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DELETE)
  1739. #define DUK_STRIDX_DO 128 /* 'do' */
  1740. #define DUK_HEAP_STRING_DO(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_DO)
  1741. #define DUK_HTHREAD_STRING_DO(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_DO)
  1742. #define DUK_STRIDX_ELSE 129 /* 'else' */
  1743. #define DUK_HEAP_STRING_ELSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ELSE)
  1744. #define DUK_HTHREAD_STRING_ELSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ELSE)
  1745. #define DUK_STRIDX_FINALLY 130 /* 'finally' */
  1746. #define DUK_HEAP_STRING_FINALLY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FINALLY)
  1747. #define DUK_HTHREAD_STRING_FINALLY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FINALLY)
  1748. #define DUK_STRIDX_FOR 131 /* 'for' */
  1749. #define DUK_HEAP_STRING_FOR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FOR)
  1750. #define DUK_HTHREAD_STRING_FOR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FOR)
  1751. #define DUK_STRIDX_LC_FUNCTION 132 /* 'function' */
  1752. #define DUK_HEAP_STRING_LC_FUNCTION(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_FUNCTION)
  1753. #define DUK_HTHREAD_STRING_LC_FUNCTION(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_FUNCTION)
  1754. #define DUK_STRIDX_IF 133 /* 'if' */
  1755. #define DUK_HEAP_STRING_IF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IF)
  1756. #define DUK_HTHREAD_STRING_IF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IF)
  1757. #define DUK_STRIDX_IN 134 /* 'in' */
  1758. #define DUK_HEAP_STRING_IN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IN)
  1759. #define DUK_HTHREAD_STRING_IN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IN)
  1760. #define DUK_STRIDX_INSTANCEOF 135 /* 'instanceof' */
  1761. #define DUK_HEAP_STRING_INSTANCEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INSTANCEOF)
  1762. #define DUK_HTHREAD_STRING_INSTANCEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INSTANCEOF)
  1763. #define DUK_STRIDX_NEW 136 /* 'new' */
  1764. #define DUK_HEAP_STRING_NEW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_NEW)
  1765. #define DUK_HTHREAD_STRING_NEW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_NEW)
  1766. #define DUK_STRIDX_RETURN 137 /* 'return' */
  1767. #define DUK_HEAP_STRING_RETURN(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_RETURN)
  1768. #define DUK_HTHREAD_STRING_RETURN(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_RETURN)
  1769. #define DUK_STRIDX_SWITCH 138 /* 'switch' */
  1770. #define DUK_HEAP_STRING_SWITCH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SWITCH)
  1771. #define DUK_HTHREAD_STRING_SWITCH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SWITCH)
  1772. #define DUK_STRIDX_THIS 139 /* 'this' */
  1773. #define DUK_HEAP_STRING_THIS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THIS)
  1774. #define DUK_HTHREAD_STRING_THIS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THIS)
  1775. #define DUK_STRIDX_THROW 140 /* 'throw' */
  1776. #define DUK_HEAP_STRING_THROW(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_THROW)
  1777. #define DUK_HTHREAD_STRING_THROW(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_THROW)
  1778. #define DUK_STRIDX_TRY 141 /* 'try' */
  1779. #define DUK_HEAP_STRING_TRY(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRY)
  1780. #define DUK_HTHREAD_STRING_TRY(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRY)
  1781. #define DUK_STRIDX_TYPEOF 142 /* 'typeof' */
  1782. #define DUK_HEAP_STRING_TYPEOF(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TYPEOF)
  1783. #define DUK_HTHREAD_STRING_TYPEOF(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TYPEOF)
  1784. #define DUK_STRIDX_VAR 143 /* 'var' */
  1785. #define DUK_HEAP_STRING_VAR(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VAR)
  1786. #define DUK_HTHREAD_STRING_VAR(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VAR)
  1787. #define DUK_STRIDX_CONST 144 /* 'const' */
  1788. #define DUK_HEAP_STRING_CONST(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CONST)
  1789. #define DUK_HTHREAD_STRING_CONST(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CONST)
  1790. #define DUK_STRIDX_VOID 145 /* 'void' */
  1791. #define DUK_HEAP_STRING_VOID(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_VOID)
  1792. #define DUK_HTHREAD_STRING_VOID(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_VOID)
  1793. #define DUK_STRIDX_WHILE 146 /* 'while' */
  1794. #define DUK_HEAP_STRING_WHILE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WHILE)
  1795. #define DUK_HTHREAD_STRING_WHILE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WHILE)
  1796. #define DUK_STRIDX_WITH 147 /* 'with' */
  1797. #define DUK_HEAP_STRING_WITH(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_WITH)
  1798. #define DUK_HTHREAD_STRING_WITH(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_WITH)
  1799. #define DUK_STRIDX_CLASS 148 /* 'class' */
  1800. #define DUK_HEAP_STRING_CLASS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_CLASS)
  1801. #define DUK_HTHREAD_STRING_CLASS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_CLASS)
  1802. #define DUK_STRIDX_ENUM 149 /* 'enum' */
  1803. #define DUK_HEAP_STRING_ENUM(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_ENUM)
  1804. #define DUK_HTHREAD_STRING_ENUM(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_ENUM)
  1805. #define DUK_STRIDX_EXPORT 150 /* 'export' */
  1806. #define DUK_HEAP_STRING_EXPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXPORT)
  1807. #define DUK_HTHREAD_STRING_EXPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXPORT)
  1808. #define DUK_STRIDX_EXTENDS 151 /* 'extends' */
  1809. #define DUK_HEAP_STRING_EXTENDS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_EXTENDS)
  1810. #define DUK_HTHREAD_STRING_EXTENDS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_EXTENDS)
  1811. #define DUK_STRIDX_IMPORT 152 /* 'import' */
  1812. #define DUK_HEAP_STRING_IMPORT(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPORT)
  1813. #define DUK_HTHREAD_STRING_IMPORT(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPORT)
  1814. #define DUK_STRIDX_SUPER 153 /* 'super' */
  1815. #define DUK_HEAP_STRING_SUPER(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_SUPER)
  1816. #define DUK_HTHREAD_STRING_SUPER(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_SUPER)
  1817. #define DUK_STRIDX_LC_NULL 154 /* 'null' */
  1818. #define DUK_HEAP_STRING_LC_NULL(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LC_NULL)
  1819. #define DUK_HTHREAD_STRING_LC_NULL(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LC_NULL)
  1820. #define DUK_STRIDX_TRUE 155 /* 'true' */
  1821. #define DUK_HEAP_STRING_TRUE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_TRUE)
  1822. #define DUK_HTHREAD_STRING_TRUE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_TRUE)
  1823. #define DUK_STRIDX_FALSE 156 /* 'false' */
  1824. #define DUK_HEAP_STRING_FALSE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_FALSE)
  1825. #define DUK_HTHREAD_STRING_FALSE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_FALSE)
  1826. #define DUK_STRIDX_IMPLEMENTS 157 /* 'implements' */
  1827. #define DUK_HEAP_STRING_IMPLEMENTS(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_IMPLEMENTS)
  1828. #define DUK_HTHREAD_STRING_IMPLEMENTS(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_IMPLEMENTS)
  1829. #define DUK_STRIDX_INTERFACE 158 /* 'interface' */
  1830. #define DUK_HEAP_STRING_INTERFACE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_INTERFACE)
  1831. #define DUK_HTHREAD_STRING_INTERFACE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_INTERFACE)
  1832. #define DUK_STRIDX_LET 159 /* 'let' */
  1833. #define DUK_HEAP_STRING_LET(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_LET)
  1834. #define DUK_HTHREAD_STRING_LET(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_LET)
  1835. #define DUK_STRIDX_PACKAGE 160 /* 'package' */
  1836. #define DUK_HEAP_STRING_PACKAGE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PACKAGE)
  1837. #define DUK_HTHREAD_STRING_PACKAGE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PACKAGE)
  1838. #define DUK_STRIDX_PRIVATE 161 /* 'private' */
  1839. #define DUK_HEAP_STRING_PRIVATE(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PRIVATE)
  1840. #define DUK_HTHREAD_STRING_PRIVATE(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PRIVATE)
  1841. #define DUK_STRIDX_PROTECTED 162 /* 'protected' */
  1842. #define DUK_HEAP_STRING_PROTECTED(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PROTECTED)
  1843. #define DUK_HTHREAD_STRING_PROTECTED(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PROTECTED)
  1844. #define DUK_STRIDX_PUBLIC 163 /* 'public' */
  1845. #define DUK_HEAP_STRING_PUBLIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_PUBLIC)
  1846. #define DUK_HTHREAD_STRING_PUBLIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_PUBLIC)
  1847. #define DUK_STRIDX_STATIC 164 /* 'static' */
  1848. #define DUK_HEAP_STRING_STATIC(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_STATIC)
  1849. #define DUK_HTHREAD_STRING_STATIC(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_STATIC)
  1850. #define DUK_STRIDX_YIELD 165 /* 'yield' */
  1851. #define DUK_HEAP_STRING_YIELD(heap) DUK_HEAP_GET_STRING((heap),DUK_STRIDX_YIELD)
  1852. #define DUK_HTHREAD_STRING_YIELD(thr) DUK_HTHREAD_GET_STRING((thr),DUK_STRIDX_YIELD)
  1853. #define DUK_HEAP_NUM_STRINGS 166
  1854. #define DUK_STRIDX_START_RESERVED 121
  1855. #define DUK_STRIDX_START_STRICT_RESERVED 157
  1856. #define DUK_STRIDX_END_RESERVED 166 /* exclusive endpoint */
  1857. /* To convert a heap stridx to a token number, subtract
  1858. * DUK_STRIDX_START_RESERVED and add DUK_TOK_START_RESERVED.
  1859. */
  1860. #if !defined(DUK_SINGLE_FILE)
  1861. DUK_INTERNAL_DECL const duk_uint8_t duk_strings_data[967];
  1862. #endif /* !DUK_SINGLE_FILE */
  1863. #define DUK_STRDATA_MAX_STRLEN 27
  1864. #define DUK_STRDATA_DATA_LENGTH 967
  1865. #endif /* DUK_USE_ROM_STRINGS */
  1866. #if defined(DUK_USE_ROM_OBJECTS)
  1867. #error RAM support not enabled, rerun configure.py with --ram-support
  1868. #else /* DUK_USE_ROM_OBJECTS */
  1869. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor(duk_context *ctx);
  1870. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_constructor(duk_context *ctx);
  1871. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype(duk_context *ctx);
  1872. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor(duk_context *ctx);
  1873. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor(duk_context *ctx);
  1874. DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_constructor(duk_context *ctx);
  1875. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_constructor(duk_context *ctx);
  1876. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor(duk_context *ctx);
  1877. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_constructor(duk_context *ctx);
  1878. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_constructor_shared(duk_context *ctx);
  1879. DUK_INTERNAL_DECL duk_ret_t duk_bi_type_error_thrower(duk_context *ctx);
  1880. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_int(duk_context *ctx);
  1881. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_parse_float(duk_context *ctx);
  1882. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_constructor(duk_context *ctx);
  1883. DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_constructor(duk_context *ctx);
  1884. DUK_INTERNAL_DECL duk_ret_t duk_bi_proxy_constructor(duk_context *ctx);
  1885. DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_constructor(duk_context *ctx);
  1886. DUK_INTERNAL_DECL duk_ret_t duk_bi_dataview_constructor(duk_context *ctx);
  1887. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_constructor(duk_context *ctx);
  1888. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_context *ctx);
  1889. DUK_INTERNAL_DECL duk_ret_t duk_bi_textencoder_constructor(duk_context *ctx);
  1890. DUK_INTERNAL_DECL duk_ret_t duk_bi_textdecoder_constructor(duk_context *ctx);
  1891. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_eval(duk_context *ctx);
  1892. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_nan(duk_context *ctx);
  1893. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_is_finite(duk_context *ctx);
  1894. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri(duk_context *ctx);
  1895. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_decode_uri_component(duk_context *ctx);
  1896. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri(duk_context *ctx);
  1897. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_encode_uri_component(duk_context *ctx);
  1898. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_escape(duk_context *ctx);
  1899. DUK_INTERNAL_DECL duk_ret_t duk_bi_global_object_unescape(duk_context *ctx);
  1900. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_getprototype_shared(duk_context *ctx);
  1901. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_setprototype_shared(duk_context *ctx);
  1902. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_context *ctx);
  1903. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_keys_shared(duk_context *ctx);
  1904. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_assign(duk_context *ctx);
  1905. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_create(duk_context *ctx);
  1906. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_property(duk_context *ctx);
  1907. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_define_properties(duk_context *ctx);
  1908. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_context *ctx);
  1909. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_context *ctx);
  1910. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_context *ctx);
  1911. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is_extensible(duk_context *ctx);
  1912. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_constructor_is(duk_context *ctx);
  1913. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_string(duk_context *ctx);
  1914. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_context *ctx);
  1915. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_value_of(duk_context *ctx);
  1916. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_has_own_property(duk_context *ctx);
  1917. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_context *ctx);
  1918. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_context *ctx);
  1919. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_defineaccessor(duk_context *ctx);
  1920. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_lookupaccessor(duk_context *ctx);
  1921. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_to_string(duk_context *ctx);
  1922. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_apply(duk_context *ctx);
  1923. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_call(duk_context *ctx);
  1924. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype_bind(duk_context *ctx);
  1925. DUK_INTERNAL_DECL duk_ret_t duk_bi_native_function_length(duk_context *ctx);
  1926. DUK_INTERNAL_DECL duk_ret_t duk_bi_native_function_name(duk_context *ctx);
  1927. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_constructor_is_array(duk_context *ctx);
  1928. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_to_string(duk_context *ctx);
  1929. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_join_shared(duk_context *ctx);
  1930. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_concat(duk_context *ctx);
  1931. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_pop(duk_context *ctx);
  1932. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_push(duk_context *ctx);
  1933. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reverse(duk_context *ctx);
  1934. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_shift(duk_context *ctx);
  1935. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_slice(duk_context *ctx);
  1936. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_sort(duk_context *ctx);
  1937. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_splice(duk_context *ctx);
  1938. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_unshift(duk_context *ctx);
  1939. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_context *ctx);
  1940. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_iter_shared(duk_context *ctx);
  1941. DUK_INTERNAL_DECL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_context *ctx);
  1942. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor_from_char_code(duk_context *ctx);
  1943. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_constructor_from_code_point(duk_context *ctx);
  1944. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_to_string(duk_context *ctx);
  1945. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_at(duk_context *ctx);
  1946. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_char_code_at(duk_context *ctx);
  1947. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_concat(duk_context *ctx);
  1948. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_context *ctx);
  1949. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_locale_compare(duk_context *ctx);
  1950. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_match(duk_context *ctx);
  1951. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_replace(duk_context *ctx);
  1952. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_search(duk_context *ctx);
  1953. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_slice(duk_context *ctx);
  1954. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_split(duk_context *ctx);
  1955. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substring(duk_context *ctx);
  1956. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_context *ctx);
  1957. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_trim(duk_context *ctx);
  1958. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_repeat(duk_context *ctx);
  1959. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_startswith_endswith(duk_context *ctx);
  1960. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_includes(duk_context *ctx);
  1961. DUK_INTERNAL_DECL duk_ret_t duk_bi_string_prototype_substr(duk_context *ctx);
  1962. DUK_INTERNAL_DECL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_context *ctx);
  1963. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_check_shared(duk_context *ctx);
  1964. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_string(duk_context *ctx);
  1965. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_context *ctx);
  1966. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_value_of(duk_context *ctx);
  1967. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_fixed(duk_context *ctx);
  1968. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_exponential(duk_context *ctx);
  1969. DUK_INTERNAL_DECL duk_ret_t duk_bi_number_prototype_to_precision(duk_context *ctx);
  1970. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_parse(duk_context *ctx);
  1971. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_utc(duk_context *ctx);
  1972. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_constructor_now(duk_context *ctx);
  1973. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_context *ctx);
  1974. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_to_json(duk_context *ctx);
  1975. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_value_of(duk_context *ctx);
  1976. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_shared(duk_context *ctx);
  1977. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_context *ctx);
  1978. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_time(duk_context *ctx);
  1979. DUK_INTERNAL_DECL duk_ret_t duk_bi_date_prototype_set_shared(duk_context *ctx);
  1980. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_exec(duk_context *ctx);
  1981. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_test(duk_context *ctx);
  1982. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_tostring(duk_context *ctx);
  1983. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_flags(duk_context *ctx);
  1984. DUK_INTERNAL_DECL duk_ret_t duk_bi_regexp_prototype_shared_getter(duk_context *ctx);
  1985. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_getter(duk_context *ctx);
  1986. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_stack_setter(duk_context *ctx);
  1987. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_getter(duk_context *ctx);
  1988. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_filename_setter(duk_context *ctx);
  1989. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_context *ctx);
  1990. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_linenumber_setter(duk_context *ctx);
  1991. DUK_INTERNAL_DECL duk_ret_t duk_bi_error_prototype_to_string(duk_context *ctx);
  1992. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_onearg_shared(duk_context *ctx);
  1993. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_twoarg_shared(duk_context *ctx);
  1994. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_clz32(duk_context *ctx);
  1995. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_hypot(duk_context *ctx);
  1996. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_imul(duk_context *ctx);
  1997. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_max(duk_context *ctx);
  1998. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_min(duk_context *ctx);
  1999. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_random(duk_context *ctx);
  2000. DUK_INTERNAL_DECL duk_ret_t duk_bi_math_object_sign(duk_context *ctx);
  2001. DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_parse(duk_context *ctx);
  2002. DUK_INTERNAL_DECL duk_ret_t duk_bi_json_object_stringify(duk_context *ctx);
  2003. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_info(duk_context *ctx);
  2004. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_act(duk_context *ctx);
  2005. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_gc(duk_context *ctx);
  2006. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_fin(duk_context *ctx);
  2007. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_enc(duk_context *ctx);
  2008. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_dec(duk_context *ctx);
  2009. DUK_INTERNAL_DECL duk_ret_t duk_bi_duktape_object_compact(duk_context *ctx);
  2010. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_yield(duk_context *ctx);
  2011. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_resume(duk_context *ctx);
  2012. DUK_INTERNAL_DECL duk_ret_t duk_bi_thread_current(duk_context *ctx);
  2013. DUK_INTERNAL_DECL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_context *ctx);
  2014. DUK_INTERNAL_DECL duk_ret_t duk_bi_reflect_apply(duk_context *ctx);
  2015. DUK_INTERNAL_DECL duk_ret_t duk_bi_reflect_construct(duk_context *ctx);
  2016. DUK_INTERNAL_DECL duk_ret_t duk_bi_reflect_object_delete_property(duk_context *ctx);
  2017. DUK_INTERNAL_DECL duk_ret_t duk_bi_reflect_object_get(duk_context *ctx);
  2018. DUK_INTERNAL_DECL duk_ret_t duk_bi_reflect_object_has(duk_context *ctx);
  2019. DUK_INTERNAL_DECL duk_ret_t duk_bi_reflect_object_set(duk_context *ctx);
  2020. DUK_INTERNAL_DECL duk_ret_t duk_bi_arraybuffer_isview(duk_context *ctx);
  2021. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_bytelength_getter(duk_context *ctx);
  2022. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_slice_shared(duk_context *ctx);
  2023. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_byteoffset_getter(duk_context *ctx);
  2024. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_buffer_getter(duk_context *ctx);
  2025. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_readfield(duk_context *ctx);
  2026. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_writefield(duk_context *ctx);
  2027. DUK_INTERNAL_DECL duk_ret_t duk_bi_typedarray_set(duk_context *ctx);
  2028. DUK_INTERNAL_DECL duk_ret_t duk_bi_uint8array_allocplain(duk_context *ctx);
  2029. DUK_INTERNAL_DECL duk_ret_t duk_bi_uint8array_plainof(duk_context *ctx);
  2030. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_concat(duk_context *ctx);
  2031. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_context *ctx);
  2032. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_context *ctx);
  2033. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_context *ctx);
  2034. DUK_INTERNAL_DECL duk_ret_t duk_bi_buffer_compare_shared(duk_context *ctx);
  2035. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_context *ctx);
  2036. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_context *ctx);
  2037. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_fill(duk_context *ctx);
  2038. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_copy(duk_context *ctx);
  2039. DUK_INTERNAL_DECL duk_ret_t duk_bi_nodejs_buffer_write(duk_context *ctx);
  2040. DUK_INTERNAL_DECL duk_ret_t duk_bi_textencoder_prototype_encoding_getter(duk_context *ctx);
  2041. DUK_INTERNAL_DECL duk_ret_t duk_bi_textencoder_prototype_encode(duk_context *ctx);
  2042. DUK_INTERNAL_DECL duk_ret_t duk_bi_textdecoder_prototype_shared_getter(duk_context *ctx);
  2043. DUK_INTERNAL_DECL duk_ret_t duk_bi_textdecoder_prototype_decode(duk_context *ctx);
  2044. DUK_INTERNAL_DECL duk_ret_t duk_bi_performance_now(duk_context *ctx);
  2045. #if !defined(DUK_SINGLE_FILE)
  2046. DUK_INTERNAL_DECL const duk_c_function duk_bi_native_functions[177];
  2047. #endif /* !DUK_SINGLE_FILE */
  2048. #define DUK_BIDX_GLOBAL 0
  2049. #define DUK_BIDX_GLOBAL_ENV 1
  2050. #define DUK_BIDX_OBJECT_CONSTRUCTOR 2
  2051. #define DUK_BIDX_OBJECT_PROTOTYPE 3
  2052. #define DUK_BIDX_FUNCTION_CONSTRUCTOR 4
  2053. #define DUK_BIDX_FUNCTION_PROTOTYPE 5
  2054. #define DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE 6
  2055. #define DUK_BIDX_ARRAY_CONSTRUCTOR 7
  2056. #define DUK_BIDX_ARRAY_PROTOTYPE 8
  2057. #define DUK_BIDX_STRING_CONSTRUCTOR 9
  2058. #define DUK_BIDX_STRING_PROTOTYPE 10
  2059. #define DUK_BIDX_BOOLEAN_CONSTRUCTOR 11
  2060. #define DUK_BIDX_BOOLEAN_PROTOTYPE 12
  2061. #define DUK_BIDX_NUMBER_CONSTRUCTOR 13
  2062. #define DUK_BIDX_NUMBER_PROTOTYPE 14
  2063. #define DUK_BIDX_DATE_CONSTRUCTOR 15
  2064. #define DUK_BIDX_DATE_PROTOTYPE 16
  2065. #define DUK_BIDX_REGEXP_CONSTRUCTOR 17
  2066. #define DUK_BIDX_REGEXP_PROTOTYPE 18
  2067. #define DUK_BIDX_ERROR_CONSTRUCTOR 19
  2068. #define DUK_BIDX_ERROR_PROTOTYPE 20
  2069. #define DUK_BIDX_EVAL_ERROR_CONSTRUCTOR 21
  2070. #define DUK_BIDX_EVAL_ERROR_PROTOTYPE 22
  2071. #define DUK_BIDX_RANGE_ERROR_CONSTRUCTOR 23
  2072. #define DUK_BIDX_RANGE_ERROR_PROTOTYPE 24
  2073. #define DUK_BIDX_REFERENCE_ERROR_CONSTRUCTOR 25
  2074. #define DUK_BIDX_REFERENCE_ERROR_PROTOTYPE 26
  2075. #define DUK_BIDX_SYNTAX_ERROR_CONSTRUCTOR 27
  2076. #define DUK_BIDX_SYNTAX_ERROR_PROTOTYPE 28
  2077. #define DUK_BIDX_TYPE_ERROR_CONSTRUCTOR 29
  2078. #define DUK_BIDX_TYPE_ERROR_PROTOTYPE 30
  2079. #define DUK_BIDX_URI_ERROR_CONSTRUCTOR 31
  2080. #define DUK_BIDX_URI_ERROR_PROTOTYPE 32
  2081. #define DUK_BIDX_TYPE_ERROR_THROWER 33
  2082. #define DUK_BIDX_DUKTAPE 34
  2083. #define DUK_BIDX_THREAD_PROTOTYPE 35
  2084. #define DUK_BIDX_POINTER_PROTOTYPE 36
  2085. #define DUK_BIDX_DOUBLE_ERROR 37
  2086. #define DUK_BIDX_SYMBOL_PROTOTYPE 38
  2087. #define DUK_BIDX_ARRAYBUFFER_PROTOTYPE 39
  2088. #define DUK_BIDX_DATAVIEW_PROTOTYPE 40
  2089. #define DUK_BIDX_INT8ARRAY_PROTOTYPE 41
  2090. #define DUK_BIDX_UINT8ARRAY_PROTOTYPE 42
  2091. #define DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE 43
  2092. #define DUK_BIDX_INT16ARRAY_PROTOTYPE 44
  2093. #define DUK_BIDX_UINT16ARRAY_PROTOTYPE 45
  2094. #define DUK_BIDX_INT32ARRAY_PROTOTYPE 46
  2095. #define DUK_BIDX_UINT32ARRAY_PROTOTYPE 47
  2096. #define DUK_BIDX_FLOAT32ARRAY_PROTOTYPE 48
  2097. #define DUK_BIDX_FLOAT64ARRAY_PROTOTYPE 49
  2098. #define DUK_BIDX_NODEJS_BUFFER_PROTOTYPE 50
  2099. #define DUK_NUM_BUILTINS 51
  2100. #define DUK_NUM_BIDX_BUILTINS 51
  2101. #define DUK_NUM_ALL_BUILTINS 78
  2102. #if defined(DUK_USE_DOUBLE_LE)
  2103. #if !defined(DUK_SINGLE_FILE)
  2104. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[4116];
  2105. #endif /* !DUK_SINGLE_FILE */
  2106. #define DUK_BUILTINS_DATA_LENGTH 4116
  2107. #elif defined(DUK_USE_DOUBLE_BE)
  2108. #if !defined(DUK_SINGLE_FILE)
  2109. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[4116];
  2110. #endif /* !DUK_SINGLE_FILE */
  2111. #define DUK_BUILTINS_DATA_LENGTH 4116
  2112. #elif defined(DUK_USE_DOUBLE_ME)
  2113. #if !defined(DUK_SINGLE_FILE)
  2114. DUK_INTERNAL_DECL const duk_uint8_t duk_builtins_data[4116];
  2115. #endif /* !DUK_SINGLE_FILE */
  2116. #define DUK_BUILTINS_DATA_LENGTH 4116
  2117. #else
  2118. #error invalid endianness defines
  2119. #endif
  2120. #endif /* DUK_USE_ROM_OBJECTS */
  2121. #endif /* DUK_BUILTINS_H_INCLUDED */
  2122. #line 51 "duk_internal.h"
  2123. /* #include duk_util.h */
  2124. #line 1 "duk_util.h"
  2125. /*
  2126. * Utilities
  2127. */
  2128. #if !defined(DUK_UTIL_H_INCLUDED)
  2129. #define DUK_UTIL_H_INCLUDED
  2130. #if defined(DUK_USE_GET_RANDOM_DOUBLE)
  2131. #define DUK_UTIL_GET_RANDOM_DOUBLE(thr) DUK_USE_GET_RANDOM_DOUBLE((thr)->heap_udata)
  2132. #else
  2133. #define DUK_UTIL_GET_RANDOM_DOUBLE(thr) duk_util_tinyrandom_get_double(thr)
  2134. #endif
  2135. /*
  2136. * Some useful constants
  2137. */
  2138. #define DUK_DOUBLE_2TO32 4294967296.0
  2139. #define DUK_DOUBLE_2TO31 2147483648.0
  2140. #define DUK_DOUBLE_LOG2E 1.4426950408889634
  2141. #define DUK_DOUBLE_LOG10E 0.4342944819032518
  2142. /*
  2143. * Endian conversion
  2144. */
  2145. #if defined(DUK_USE_INTEGER_LE)
  2146. #define DUK_HTON32(x) DUK_BSWAP32((x))
  2147. #define DUK_NTOH32(x) DUK_BSWAP32((x))
  2148. #define DUK_HTON16(x) DUK_BSWAP16((x))
  2149. #define DUK_NTOH16(x) DUK_BSWAP16((x))
  2150. #elif defined(DUK_USE_INTEGER_BE)
  2151. #define DUK_HTON32(x) (x)
  2152. #define DUK_NTOH32(x) (x)
  2153. #define DUK_HTON16(x) (x)
  2154. #define DUK_NTOH16(x) (x)
  2155. #else
  2156. #error internal error, endianness defines broken
  2157. #endif
  2158. /*
  2159. * Bitstream decoder
  2160. */
  2161. struct duk_bitdecoder_ctx {
  2162. const duk_uint8_t *data;
  2163. duk_size_t offset;
  2164. duk_size_t length;
  2165. duk_uint32_t currval;
  2166. duk_small_int_t currbits;
  2167. };
  2168. #define DUK_BD_BITPACKED_STRING_MAXLEN 256
  2169. /*
  2170. * Bitstream encoder
  2171. */
  2172. struct duk_bitencoder_ctx {
  2173. duk_uint8_t *data;
  2174. duk_size_t offset;
  2175. duk_size_t length;
  2176. duk_uint32_t currval;
  2177. duk_small_int_t currbits;
  2178. duk_small_int_t truncated;
  2179. };
  2180. /*
  2181. * Raw write/read macros for big endian, unaligned basic values.
  2182. * Caller ensures there's enough space. The macros update the pointer
  2183. * argument automatically on resizes. The idiom seems a bit odd, but
  2184. * leads to compact code.
  2185. */
  2186. #define DUK_RAW_WRITE_U8(ptr,val) do { \
  2187. *(ptr)++ = (duk_uint8_t) (val); \
  2188. } while (0)
  2189. #define DUK_RAW_WRITE_U16_BE(ptr,val) duk_raw_write_u16_be(&(ptr), (duk_uint16_t) (val))
  2190. #define DUK_RAW_WRITE_U32_BE(ptr,val) duk_raw_write_u32_be(&(ptr), (duk_uint32_t) (val))
  2191. #define DUK_RAW_WRITE_DOUBLE_BE(ptr,val) duk_raw_write_double_be(&(ptr), (duk_double_t) (val))
  2192. #define DUK_RAW_WRITE_XUTF8(ptr,val) do { \
  2193. /* 'ptr' is evaluated both as LHS and RHS. */ \
  2194. duk_uint8_t *duk__ptr; \
  2195. duk_small_int_t duk__len; \
  2196. duk__ptr = (duk_uint8_t *) (ptr); \
  2197. duk__len = duk_unicode_encode_xutf8((duk_ucodepoint_t) (val), duk__ptr); \
  2198. duk__ptr += duk__len; \
  2199. (ptr) = duk__ptr; \
  2200. } while (0)
  2201. #define DUK_RAW_WRITE_CESU8(ptr,val) do { \
  2202. /* 'ptr' is evaluated both as LHS and RHS. */ \
  2203. duk_uint8_t *duk__ptr; \
  2204. duk_small_int_t duk__len; \
  2205. duk__ptr = (duk_uint8_t *) (ptr); \
  2206. duk__len = duk_unicode_encode_cesu8((duk_ucodepoint_t) (val), duk__ptr); \
  2207. duk__ptr += duk__len; \
  2208. (ptr) = duk__ptr; \
  2209. } while (0)
  2210. #define DUK_RAW_READ_U8(ptr) ((duk_uint8_t) (*(ptr)++))
  2211. #define DUK_RAW_READ_U16_BE(ptr) duk_raw_read_u16_be(&(ptr));
  2212. #define DUK_RAW_READ_U32_BE(ptr) duk_raw_read_u32_be(&(ptr));
  2213. #define DUK_RAW_READ_DOUBLE_BE(ptr) duk_raw_read_double_be(&(ptr));
  2214. /*
  2215. * Buffer writer (dynamic buffer only)
  2216. *
  2217. * Helper for writing to a dynamic buffer with a concept of a "slack" area
  2218. * to reduce resizes. You can ensure there is enough space beforehand and
  2219. * then write for a while without further checks, relying on a stable data
  2220. * pointer. Slack handling is automatic so call sites only indicate how
  2221. * much data they need right now.
  2222. *
  2223. * There are several ways to write using bufwriter. The best approach
  2224. * depends mainly on how much performance matters over code footprint.
  2225. * The key issues are (1) ensuring there is space and (2) keeping the
  2226. * pointers consistent. Fast code should ensure space for multiple writes
  2227. * with one ensure call. Fastest inner loop code can temporarily borrow
  2228. * the 'p' pointer but must write it back eventually.
  2229. *
  2230. * Be careful to ensure all macro arguments (other than static pointers like
  2231. * 'thr' and 'bw_ctx') are evaluated exactly once, using temporaries if
  2232. * necessary (if that's not possible, there should be a note near the macro).
  2233. * Buffer write arguments often contain arithmetic etc so this is
  2234. * particularly important here.
  2235. */
  2236. /* XXX: Migrate bufwriter and other read/write helpers to its own header? */
  2237. struct duk_bufwriter_ctx {
  2238. duk_uint8_t *p;
  2239. duk_uint8_t *p_base;
  2240. duk_uint8_t *p_limit;
  2241. duk_hbuffer_dynamic *buf;
  2242. };
  2243. #if defined(DUK_USE_PREFER_SIZE)
  2244. #define DUK_BW_SLACK_ADD 64
  2245. #define DUK_BW_SLACK_SHIFT 4 /* 2^4 -> 1/16 = 6.25% slack */
  2246. #else
  2247. #define DUK_BW_SLACK_ADD 64
  2248. #define DUK_BW_SLACK_SHIFT 2 /* 2^2 -> 1/4 = 25% slack */
  2249. #endif
  2250. /* Initialization and finalization (compaction), converting to other types. */
  2251. #define DUK_BW_INIT_PUSHBUF(thr,bw_ctx,sz) do { \
  2252. duk_bw_init_pushbuf((thr), (bw_ctx), (sz)); \
  2253. } while (0)
  2254. #define DUK_BW_INIT_WITHBUF(thr,bw_ctx,buf) do { \
  2255. duk_bw_init((thr), (bw_ctx), (buf)); \
  2256. } while (0)
  2257. #define DUK_BW_COMPACT(thr,bw_ctx) do { \
  2258. /* Make underlying buffer compact to match DUK_BW_GET_SIZE(). */ \
  2259. duk_bw_compact((thr), (bw_ctx)); \
  2260. } while (0)
  2261. #define DUK_BW_PUSH_AS_STRING(thr,bw_ctx) do { \
  2262. duk_push_lstring((thr), \
  2263. (const char *) (bw_ctx)->p_base, \
  2264. (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \
  2265. } while (0)
  2266. /* Pointers may be NULL for a while when 'buf' size is zero and before any
  2267. * ENSURE calls have been made. Once an ENSURE has been made, the pointers
  2268. * are required to be non-NULL so that it's always valid to use memcpy() and
  2269. * memmove(), even for zero size.
  2270. */
  2271. #define DUK_BW_ASSERT_VALID_EXPR(thr,bw_ctx) \
  2272. DUK_ASSERT_EXPR((bw_ctx) != NULL && \
  2273. (bw_ctx)->buf != NULL && \
  2274. ((DUK_HBUFFER_DYNAMIC_GET_SIZE((bw_ctx)->buf) == 0) || \
  2275. ((bw_ctx)->p != NULL && \
  2276. (bw_ctx)->p_base != NULL && \
  2277. (bw_ctx)->p_limit != NULL && \
  2278. (bw_ctx)->p_limit >= (bw_ctx)->p_base && \
  2279. (bw_ctx)->p >= (bw_ctx)->p_base && \
  2280. (bw_ctx)->p <= (bw_ctx)->p_limit)))
  2281. #define DUK_BW_ASSERT_VALID(thr,bw_ctx) do { \
  2282. DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)); \
  2283. } while (0)
  2284. /* Working with the pointer and current size. */
  2285. #define DUK_BW_GET_PTR(thr,bw_ctx) \
  2286. ((bw_ctx)->p)
  2287. #define DUK_BW_SET_PTR(thr,bw_ctx,ptr) do { \
  2288. (bw_ctx)->p = (ptr); \
  2289. } while (0)
  2290. #define DUK_BW_ADD_PTR(thr,bw_ctx,delta) do { \
  2291. (bw_ctx)->p += (delta); \
  2292. } while (0)
  2293. #define DUK_BW_GET_BASEPTR(thr,bw_ctx) \
  2294. ((bw_ctx)->p_base)
  2295. #define DUK_BW_GET_LIMITPTR(thr,bw_ctx) \
  2296. ((bw_ctx)->p_limit)
  2297. #define DUK_BW_GET_SIZE(thr,bw_ctx) \
  2298. ((duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base))
  2299. #define DUK_BW_SET_SIZE(thr,bw_ctx,sz) do { \
  2300. DUK_ASSERT((duk_size_t) (sz) <= (duk_size_t) ((bw_ctx)->p - (bw_ctx)->p_base)); \
  2301. (bw_ctx)->p = (bw_ctx)->p_base + (sz); \
  2302. } while (0)
  2303. #define DUK_BW_RESET_SIZE(thr,bw_ctx) do { \
  2304. /* Reset to zero size, keep current limit. */ \
  2305. (bw_ctx)->p = (bw_ctx)->p_base; \
  2306. } while (0)
  2307. #define DUK_BW_GET_BUFFER(thr,bw_ctx) \
  2308. ((bw_ctx)->buf)
  2309. /* Ensuring (reserving) space. */
  2310. #define DUK_BW_ENSURE(thr,bw_ctx,sz) do { \
  2311. duk_size_t duk__sz, duk__space; \
  2312. DUK_BW_ASSERT_VALID((thr), (bw_ctx)); \
  2313. duk__sz = (sz); \
  2314. duk__space = (duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p); \
  2315. if (duk__space < duk__sz) { \
  2316. (void) duk_bw_resize((thr), (bw_ctx), duk__sz); \
  2317. } \
  2318. } while (0)
  2319. /* NOTE: Multiple evaluation of 'ptr' in this macro. */
  2320. /* XXX: Rework to use an always-inline function? */
  2321. #define DUK_BW_ENSURE_RAW(thr,bw_ctx,sz,ptr) \
  2322. (((duk_size_t) ((bw_ctx)->p_limit - (ptr)) >= (sz)) ? \
  2323. (ptr) : \
  2324. ((bw_ctx)->p = (ptr), duk_bw_resize((thr),(bw_ctx),(sz))))
  2325. #define DUK_BW_ENSURE_GETPTR(thr,bw_ctx,sz) \
  2326. DUK_BW_ENSURE_RAW((thr), (bw_ctx), (sz), (bw_ctx)->p)
  2327. #define DUK_BW_ASSERT_SPACE_EXPR(thr,bw_ctx,sz) \
  2328. (DUK_BW_ASSERT_VALID_EXPR((thr), (bw_ctx)), \
  2329. DUK_ASSERT_EXPR((duk_size_t) ((bw_ctx)->p_limit - (bw_ctx)->p) >= (duk_size_t) (sz)))
  2330. #define DUK_BW_ASSERT_SPACE(thr,bw_ctx,sz) do { \
  2331. DUK_BW_ASSERT_SPACE_EXPR((thr), (bw_ctx), (sz)); \
  2332. } while (0)
  2333. /* Miscellaneous. */
  2334. #define DUK_BW_SETPTR_AND_COMPACT(thr,bw_ctx,ptr) do { \
  2335. (bw_ctx)->p = (ptr); \
  2336. duk_bw_compact((thr), (bw_ctx)); \
  2337. } while (0)
  2338. /* Fast write calls which assume you control the slack beforehand.
  2339. * Multibyte write variants exist and use a temporary write pointer
  2340. * because byte writes alias with anything: with a stored pointer
  2341. * explicit pointer load/stores get generated (e.g. gcc -Os).
  2342. */
  2343. #define DUK_BW_WRITE_RAW_U8(thr,bw_ctx,val) do { \
  2344. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 1); \
  2345. *(bw_ctx)->p++ = (duk_uint8_t) (val); \
  2346. } while (0)
  2347. #define DUK_BW_WRITE_RAW_U8_2(thr,bw_ctx,val1,val2) do { \
  2348. duk_uint8_t *duk__p; \
  2349. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 2); \
  2350. duk__p = (bw_ctx)->p; \
  2351. *duk__p++ = (duk_uint8_t) (val1); \
  2352. *duk__p++ = (duk_uint8_t) (val2); \
  2353. (bw_ctx)->p = duk__p; \
  2354. } while (0)
  2355. #define DUK_BW_WRITE_RAW_U8_3(thr,bw_ctx,val1,val2,val3) do { \
  2356. duk_uint8_t *duk__p; \
  2357. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 3); \
  2358. duk__p = (bw_ctx)->p; \
  2359. *duk__p++ = (duk_uint8_t) (val1); \
  2360. *duk__p++ = (duk_uint8_t) (val2); \
  2361. *duk__p++ = (duk_uint8_t) (val3); \
  2362. (bw_ctx)->p = duk__p; \
  2363. } while (0)
  2364. #define DUK_BW_WRITE_RAW_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \
  2365. duk_uint8_t *duk__p; \
  2366. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 4); \
  2367. duk__p = (bw_ctx)->p; \
  2368. *duk__p++ = (duk_uint8_t) (val1); \
  2369. *duk__p++ = (duk_uint8_t) (val2); \
  2370. *duk__p++ = (duk_uint8_t) (val3); \
  2371. *duk__p++ = (duk_uint8_t) (val4); \
  2372. (bw_ctx)->p = duk__p; \
  2373. } while (0)
  2374. #define DUK_BW_WRITE_RAW_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \
  2375. duk_uint8_t *duk__p; \
  2376. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 5); \
  2377. duk__p = (bw_ctx)->p; \
  2378. *duk__p++ = (duk_uint8_t) (val1); \
  2379. *duk__p++ = (duk_uint8_t) (val2); \
  2380. *duk__p++ = (duk_uint8_t) (val3); \
  2381. *duk__p++ = (duk_uint8_t) (val4); \
  2382. *duk__p++ = (duk_uint8_t) (val5); \
  2383. (bw_ctx)->p = duk__p; \
  2384. } while (0)
  2385. #define DUK_BW_WRITE_RAW_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \
  2386. duk_uint8_t *duk__p; \
  2387. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), 6); \
  2388. duk__p = (bw_ctx)->p; \
  2389. *duk__p++ = (duk_uint8_t) (val1); \
  2390. *duk__p++ = (duk_uint8_t) (val2); \
  2391. *duk__p++ = (duk_uint8_t) (val3); \
  2392. *duk__p++ = (duk_uint8_t) (val4); \
  2393. *duk__p++ = (duk_uint8_t) (val5); \
  2394. *duk__p++ = (duk_uint8_t) (val6); \
  2395. (bw_ctx)->p = duk__p; \
  2396. } while (0)
  2397. #define DUK_BW_WRITE_RAW_XUTF8(thr,bw_ctx,cp) do { \
  2398. duk_ucodepoint_t duk__cp; \
  2399. duk_small_int_t duk__enc_len; \
  2400. duk__cp = (duk_ucodepoint_t) (cp); \
  2401. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_xutf8_length(duk__cp)); \
  2402. duk__enc_len = duk_unicode_encode_xutf8(duk__cp, (bw_ctx)->p); \
  2403. (bw_ctx)->p += duk__enc_len; \
  2404. } while (0)
  2405. #define DUK_BW_WRITE_RAW_CESU8(thr,bw_ctx,cp) do { \
  2406. duk_ucodepoint_t duk__cp; \
  2407. duk_small_int_t duk__enc_len; \
  2408. duk__cp = (duk_ucodepoint_t) (cp); \
  2409. DUK_BW_ASSERT_SPACE((thr), (bw_ctx), duk_unicode_get_cesu8_length(duk__cp)); \
  2410. duk__enc_len = duk_unicode_encode_cesu8(duk__cp, (bw_ctx)->p); \
  2411. (bw_ctx)->p += duk__enc_len; \
  2412. } while (0)
  2413. /* XXX: add temporary duk__p pointer here too; sharing */
  2414. /* XXX: avoid unsafe variants */
  2415. #define DUK_BW_WRITE_RAW_BYTES(thr,bw_ctx,valptr,valsz) do { \
  2416. const void *duk__valptr; \
  2417. duk_size_t duk__valsz; \
  2418. duk__valptr = (const void *) (valptr); \
  2419. duk__valsz = (duk_size_t) (valsz); \
  2420. duk_memcpy_unsafe((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \
  2421. (bw_ctx)->p += duk__valsz; \
  2422. } while (0)
  2423. #define DUK_BW_WRITE_RAW_CSTRING(thr,bw_ctx,val) do { \
  2424. const duk_uint8_t *duk__val; \
  2425. duk_size_t duk__val_len; \
  2426. duk__val = (const duk_uint8_t *) (val); \
  2427. duk__val_len = DUK_STRLEN((const char *) duk__val); \
  2428. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \
  2429. (bw_ctx)->p += duk__val_len; \
  2430. } while (0)
  2431. #define DUK_BW_WRITE_RAW_HSTRING(thr,bw_ctx,val) do { \
  2432. duk_size_t duk__val_len; \
  2433. duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \
  2434. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \
  2435. (bw_ctx)->p += duk__val_len; \
  2436. } while (0)
  2437. #define DUK_BW_WRITE_RAW_HBUFFER(thr,bw_ctx,val) do { \
  2438. duk_size_t duk__val_len; \
  2439. duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \
  2440. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  2441. (bw_ctx)->p += duk__val_len; \
  2442. } while (0)
  2443. #define DUK_BW_WRITE_RAW_HBUFFER_FIXED(thr,bw_ctx,val) do { \
  2444. duk_size_t duk__val_len; \
  2445. duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \
  2446. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  2447. (bw_ctx)->p += duk__val_len; \
  2448. } while (0)
  2449. #define DUK_BW_WRITE_RAW_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \
  2450. duk_size_t duk__val_len; \
  2451. duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \
  2452. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  2453. (bw_ctx)->p += duk__val_len; \
  2454. } while (0)
  2455. /* Append bytes from a slice already in the buffer. */
  2456. #define DUK_BW_WRITE_RAW_SLICE(thr,bw,dst_off,dst_len) \
  2457. duk_bw_write_raw_slice((thr), (bw), (dst_off), (dst_len))
  2458. /* Insert bytes in the middle of the buffer from an external buffer. */
  2459. #define DUK_BW_INSERT_RAW_BYTES(thr,bw,dst_off,buf,len) \
  2460. duk_bw_insert_raw_bytes((thr), (bw), (dst_off), (buf), (len))
  2461. /* Insert bytes in the middle of the buffer from a slice already
  2462. * in the buffer. Source offset is interpreted "before" the operation.
  2463. */
  2464. #define DUK_BW_INSERT_RAW_SLICE(thr,bw,dst_off,src_off,len) \
  2465. duk_bw_insert_raw_slice((thr), (bw), (dst_off), (src_off), (len))
  2466. /* Insert a reserved area somewhere in the buffer; caller fills it.
  2467. * Evaluates to a (duk_uint_t *) pointing to the start of the reserved
  2468. * area for convenience.
  2469. */
  2470. #define DUK_BW_INSERT_RAW_AREA(thr,bw,off,len) \
  2471. duk_bw_insert_raw_area((thr), (bw), (off), (len))
  2472. /* Remove a slice from inside buffer. */
  2473. #define DUK_BW_REMOVE_RAW_SLICE(thr,bw,off,len) \
  2474. duk_bw_remove_raw_slice((thr), (bw), (off), (len))
  2475. /* Safe write calls which will ensure space first. */
  2476. #define DUK_BW_WRITE_ENSURE_U8(thr,bw_ctx,val) do { \
  2477. DUK_BW_ENSURE((thr), (bw_ctx), 1); \
  2478. DUK_BW_WRITE_RAW_U8((thr), (bw_ctx), (val)); \
  2479. } while (0)
  2480. #define DUK_BW_WRITE_ENSURE_U8_2(thr,bw_ctx,val1,val2) do { \
  2481. DUK_BW_ENSURE((thr), (bw_ctx), 2); \
  2482. DUK_BW_WRITE_RAW_U8_2((thr), (bw_ctx), (val1), (val2)); \
  2483. } while (0)
  2484. #define DUK_BW_WRITE_ENSURE_U8_3(thr,bw_ctx,val1,val2,val3) do { \
  2485. DUK_BW_ENSURE((thr), (bw_ctx), 3); \
  2486. DUK_BW_WRITE_RAW_U8_3((thr), (bw_ctx), (val1), (val2), (val3)); \
  2487. } while (0)
  2488. #define DUK_BW_WRITE_ENSURE_U8_4(thr,bw_ctx,val1,val2,val3,val4) do { \
  2489. DUK_BW_ENSURE((thr), (bw_ctx), 4); \
  2490. DUK_BW_WRITE_RAW_U8_4((thr), (bw_ctx), (val1), (val2), (val3), (val4)); \
  2491. } while (0)
  2492. #define DUK_BW_WRITE_ENSURE_U8_5(thr,bw_ctx,val1,val2,val3,val4,val5) do { \
  2493. DUK_BW_ENSURE((thr), (bw_ctx), 5); \
  2494. DUK_BW_WRITE_RAW_U8_5((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5)); \
  2495. } while (0)
  2496. #define DUK_BW_WRITE_ENSURE_U8_6(thr,bw_ctx,val1,val2,val3,val4,val5,val6) do { \
  2497. DUK_BW_ENSURE((thr), (bw_ctx), 6); \
  2498. DUK_BW_WRITE_RAW_U8_6((thr), (bw_ctx), (val1), (val2), (val3), (val4), (val5), (val6)); \
  2499. } while (0)
  2500. #define DUK_BW_WRITE_ENSURE_XUTF8(thr,bw_ctx,cp) do { \
  2501. DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_XUTF8_LENGTH); \
  2502. DUK_BW_WRITE_RAW_XUTF8((thr), (bw_ctx), (cp)); \
  2503. } while (0)
  2504. #define DUK_BW_WRITE_ENSURE_CESU8(thr,bw_ctx,cp) do { \
  2505. DUK_BW_ENSURE((thr), (bw_ctx), DUK_UNICODE_MAX_CESU8_LENGTH); \
  2506. DUK_BW_WRITE_RAW_CESU8((thr), (bw_ctx), (cp)); \
  2507. } while (0)
  2508. /* XXX: add temporary duk__p pointer here too; sharing */
  2509. /* XXX: avoid unsafe */
  2510. #define DUK_BW_WRITE_ENSURE_BYTES(thr,bw_ctx,valptr,valsz) do { \
  2511. const void *duk__valptr; \
  2512. duk_size_t duk__valsz; \
  2513. duk__valptr = (const void *) (valptr); \
  2514. duk__valsz = (duk_size_t) (valsz); \
  2515. DUK_BW_ENSURE((thr), (bw_ctx), duk__valsz); \
  2516. duk_memcpy_unsafe((void *) ((bw_ctx)->p), duk__valptr, duk__valsz); \
  2517. (bw_ctx)->p += duk__valsz; \
  2518. } while (0)
  2519. #define DUK_BW_WRITE_ENSURE_CSTRING(thr,bw_ctx,val) do { \
  2520. const duk_uint8_t *duk__val; \
  2521. duk_size_t duk__val_len; \
  2522. duk__val = (const duk_uint8_t *) (val); \
  2523. duk__val_len = DUK_STRLEN((const char *) duk__val); \
  2524. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  2525. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) duk__val, duk__val_len); \
  2526. (bw_ctx)->p += duk__val_len; \
  2527. } while (0)
  2528. #define DUK_BW_WRITE_ENSURE_HSTRING(thr,bw_ctx,val) do { \
  2529. duk_size_t duk__val_len; \
  2530. duk__val_len = DUK_HSTRING_GET_BYTELEN((val)); \
  2531. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  2532. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HSTRING_GET_DATA((val)), duk__val_len); \
  2533. (bw_ctx)->p += duk__val_len; \
  2534. } while (0)
  2535. #define DUK_BW_WRITE_ENSURE_HBUFFER(thr,bw_ctx,val) do { \
  2536. duk_size_t duk__val_len; \
  2537. duk__val_len = DUK_HBUFFER_GET_SIZE((val)); \
  2538. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  2539. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  2540. (bw_ctx)->p += duk__val_len; \
  2541. } while (0)
  2542. #define DUK_BW_WRITE_ENSURE_HBUFFER_FIXED(thr,bw_ctx,val) do { \
  2543. duk_size_t duk__val_len; \
  2544. duk__val_len = DUK_HBUFFER_FIXED_GET_SIZE((val)); \
  2545. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  2546. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_FIXED_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  2547. (bw_ctx)->p += duk__val_len; \
  2548. } while (0)
  2549. #define DUK_BW_WRITE_ENSURE_HBUFFER_DYNAMIC(thr,bw_ctx,val) do { \
  2550. duk_size_t duk__val_len; \
  2551. duk__val_len = DUK_HBUFFER_DYNAMIC_GET_SIZE((val)); \
  2552. DUK_BW_ENSURE((thr), (bw_ctx), duk__val_len); \
  2553. duk_memcpy_unsafe((void *) ((bw_ctx)->p), (const void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((thr)->heap, (val)), duk__val_len); \
  2554. (bw_ctx)->p += duk__val_len; \
  2555. } while (0)
  2556. #define DUK_BW_WRITE_ENSURE_SLICE(thr,bw,dst_off,dst_len) \
  2557. duk_bw_write_ensure_slice((thr), (bw), (dst_off), (dst_len))
  2558. #define DUK_BW_INSERT_ENSURE_BYTES(thr,bw,dst_off,buf,len) \
  2559. duk_bw_insert_ensure_bytes((thr), (bw), (dst_off), (buf), (len))
  2560. #define DUK_BW_INSERT_ENSURE_SLICE(thr,bw,dst_off,src_off,len) \
  2561. duk_bw_insert_ensure_slice((thr), (bw), (dst_off), (src_off), (len))
  2562. #define DUK_BW_INSERT_ENSURE_AREA(thr,bw,off,len) \
  2563. /* Evaluates to (duk_uint8_t *) pointing to start of area. */ \
  2564. duk_bw_insert_ensure_area((thr), (bw), (off), (len))
  2565. #define DUK_BW_REMOVE_ENSURE_SLICE(thr,bw,off,len) \
  2566. /* No difference between raw/ensure because the buffer shrinks. */ \
  2567. DUK_BW_REMOVE_RAW_SLICE((thr), (bw), (off), (len))
  2568. /*
  2569. * Externs and prototypes
  2570. */
  2571. #if !defined(DUK_SINGLE_FILE)
  2572. DUK_INTERNAL_DECL const duk_uint8_t duk_lc_digits[36];
  2573. DUK_INTERNAL_DECL const duk_uint8_t duk_uc_nybbles[16];
  2574. DUK_INTERNAL_DECL const duk_int8_t duk_hex_dectab[256];
  2575. #if defined(DUK_USE_HEX_FASTPATH)
  2576. DUK_INTERNAL_DECL const duk_int16_t duk_hex_dectab_shift4[256];
  2577. DUK_INTERNAL_DECL const duk_uint16_t duk_hex_enctab[256];
  2578. #endif
  2579. #endif /* !DUK_SINGLE_FILE */
  2580. /* Note: assumes that duk_util_probe_steps size is 32 */
  2581. #if defined(DUK_USE_HOBJECT_HASH_PART)
  2582. #if !defined(DUK_SINGLE_FILE)
  2583. DUK_INTERNAL_DECL duk_uint8_t duk_util_probe_steps[32];
  2584. #endif /* !DUK_SINGLE_FILE */
  2585. #endif
  2586. #if defined(DUK_USE_STRHASH_DENSE)
  2587. DUK_INTERNAL_DECL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed);
  2588. #endif
  2589. DUK_INTERNAL_DECL duk_uint32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits);
  2590. DUK_INTERNAL_DECL duk_small_uint_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx);
  2591. DUK_INTERNAL_DECL duk_uint32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_uint32_t def_value);
  2592. DUK_INTERNAL_DECL duk_int32_t duk_bd_decode_flagged_signed(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value);
  2593. DUK_INTERNAL_DECL duk_uint32_t duk_bd_decode_varuint(duk_bitdecoder_ctx *ctx);
  2594. DUK_INTERNAL_DECL duk_small_uint_t duk_bd_decode_bitpacked_string(duk_bitdecoder_ctx *bd, duk_uint8_t *out);
  2595. DUK_INTERNAL_DECL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits);
  2596. DUK_INTERNAL_DECL void duk_be_finish(duk_bitencoder_ctx *ctx);
  2597. #if !defined(DUK_USE_GET_RANDOM_DOUBLE)
  2598. DUK_INTERNAL_DECL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr);
  2599. DUK_INTERNAL_DECL void duk_util_tinyrandom_prepare_seed(duk_hthread *thr);
  2600. #endif
  2601. DUK_INTERNAL_DECL void duk_bw_init(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_hbuffer_dynamic *h_buf);
  2602. DUK_INTERNAL_DECL void duk_bw_init_pushbuf(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t buf_size);
  2603. DUK_INTERNAL_DECL duk_uint8_t *duk_bw_resize(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t sz);
  2604. DUK_INTERNAL_DECL void duk_bw_compact(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx);
  2605. DUK_INTERNAL_DECL void duk_bw_write_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len);
  2606. DUK_INTERNAL_DECL void duk_bw_write_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len);
  2607. DUK_INTERNAL_DECL void duk_bw_insert_raw_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len);
  2608. DUK_INTERNAL_DECL void duk_bw_insert_ensure_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len);
  2609. DUK_INTERNAL_DECL void duk_bw_insert_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len);
  2610. DUK_INTERNAL_DECL void duk_bw_insert_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len);
  2611. DUK_INTERNAL_DECL duk_uint8_t *duk_bw_insert_raw_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len);
  2612. DUK_INTERNAL_DECL duk_uint8_t *duk_bw_insert_ensure_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len);
  2613. DUK_INTERNAL_DECL void duk_bw_remove_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len);
  2614. /* No duk_bw_remove_ensure_slice(), functionality would be identical. */
  2615. DUK_INTERNAL_DECL duk_uint16_t duk_raw_read_u16_be(duk_uint8_t **p);
  2616. DUK_INTERNAL_DECL duk_uint32_t duk_raw_read_u32_be(duk_uint8_t **p);
  2617. DUK_INTERNAL_DECL duk_double_t duk_raw_read_double_be(duk_uint8_t **p);
  2618. DUK_INTERNAL_DECL void duk_raw_write_u16_be(duk_uint8_t **p, duk_uint16_t val);
  2619. DUK_INTERNAL_DECL void duk_raw_write_u32_be(duk_uint8_t **p, duk_uint32_t val);
  2620. DUK_INTERNAL_DECL void duk_raw_write_double_be(duk_uint8_t **p, duk_double_t val);
  2621. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */
  2622. DUK_INTERNAL_DECL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len);
  2623. #endif
  2624. /* memcpy(), memmove() etc wrappers. The plain variants like duk_memcpy()
  2625. * assume C99+ and 'src' and 'dst' pointers must be non-NULL even when the
  2626. * operation size is zero. The unsafe variants like duk_memcpy_safe() deal
  2627. * with the zero size case explicitly, and allow NULL pointers in that case
  2628. * (which is undefined behavior in C99+). For the majority of actual targets
  2629. * a NULL pointer with a zero length is fine in practice. These wrappers are
  2630. * macros to force inlining; because there are hundreds of call sites, even a
  2631. * few extra bytes per call site adds up to ~1kB footprint.
  2632. */
  2633. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  2634. #define duk_memcpy(dst,src,len) do { \
  2635. void *duk__dst = (dst); \
  2636. const void *duk__src = (src); \
  2637. duk_size_t duk__len = (len); \
  2638. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2639. DUK_ASSERT(duk__src != NULL || duk__len == 0U); \
  2640. (void) DUK_MEMCPY(duk__dst, duk__src, (size_t) duk__len); \
  2641. } while (0)
  2642. #define duk_memcpy_unsafe(dst,src,len) duk_memcpy((dst), (src), (len))
  2643. #define duk_memmove(dst,src,len) do { \
  2644. void *duk__dst = (dst); \
  2645. const void *duk__src = (src); \
  2646. duk_size_t duk__len = (len); \
  2647. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2648. DUK_ASSERT(duk__src != NULL || duk__len == 0U); \
  2649. (void) DUK_MEMMOVE(duk__dst, duk__src, (size_t) duk__len); \
  2650. } while (0)
  2651. #define duk_memmove_unsafe(dst,src,len) duk_memmove((dst), (src), (len))
  2652. #define duk_memset(dst,val,len) do { \
  2653. void *duk__dst = (dst); \
  2654. duk_small_int_t duk__val = (val); \
  2655. duk_size_t duk__len = (len); \
  2656. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2657. (void) DUK_MEMSET(duk__dst, duk__val, (size_t) duk__len); \
  2658. } while (0)
  2659. #define duk_memset_unsafe(dst,val,len) duk_memset((dst), (val), (len))
  2660. #define duk_memzero(dst,len) do { \
  2661. void *duk__dst = (dst); \
  2662. duk_size_t duk__len = (len); \
  2663. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2664. (void) DUK_MEMZERO(duk__dst, (size_t) duk__len); \
  2665. } while (0)
  2666. #define duk_memzero_unsafe(dst,len) duk_memzero((dst), (len))
  2667. #else /* DUK_USE_ALLOW_UNDEFINED_BEHAVIOR */
  2668. #define duk_memcpy(dst,src,len) do { \
  2669. void *duk__dst = (dst); \
  2670. const void *duk__src = (src); \
  2671. duk_size_t duk__len = (len); \
  2672. DUK_ASSERT(duk__dst != NULL); \
  2673. DUK_ASSERT(duk__src != NULL); \
  2674. (void) DUK_MEMCPY(duk__dst, duk__src, (size_t) duk__len); \
  2675. } while (0)
  2676. #define duk_memcpy_unsafe(dst,src,len) do { \
  2677. void *duk__dst = (dst); \
  2678. const void *duk__src = (src); \
  2679. duk_size_t duk__len = (len); \
  2680. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2681. DUK_ASSERT(duk__src != NULL || duk__len == 0U); \
  2682. if (DUK_LIKELY(duk__len > 0U)) { \
  2683. DUK_ASSERT(duk__dst != NULL); \
  2684. DUK_ASSERT(duk__src != NULL); \
  2685. (void) DUK_MEMCPY(duk__dst, duk__src, (size_t) duk__len); \
  2686. } \
  2687. } while (0)
  2688. #define duk_memmove(dst,src,len) do { \
  2689. void *duk__dst = (dst); \
  2690. const void *duk__src = (src); \
  2691. duk_size_t duk__len = (len); \
  2692. DUK_ASSERT(duk__dst != NULL); \
  2693. DUK_ASSERT(duk__src != NULL); \
  2694. (void) DUK_MEMMOVE(duk__dst, duk__src, (size_t) duk__len); \
  2695. } while (0)
  2696. #define duk_memmove_unsafe(dst,src,len) do { \
  2697. void *duk__dst = (dst); \
  2698. const void *duk__src = (src); \
  2699. duk_size_t duk__len = (len); \
  2700. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2701. DUK_ASSERT(duk__src != NULL || duk__len == 0U); \
  2702. if (DUK_LIKELY(duk__len > 0U)) { \
  2703. DUK_ASSERT(duk__dst != NULL); \
  2704. DUK_ASSERT(duk__src != NULL); \
  2705. (void) DUK_MEMMOVE(duk__dst, duk__src, (size_t) duk__len); \
  2706. } \
  2707. } while (0)
  2708. #define duk_memset(dst,val,len) do { \
  2709. void *duk__dst = (dst); \
  2710. duk_small_int_t duk__val = (val); \
  2711. duk_size_t duk__len = (len); \
  2712. DUK_ASSERT(duk__dst != NULL); \
  2713. (void) DUK_MEMSET(duk__dst, duk__val, (size_t) duk__len); \
  2714. } while (0)
  2715. #define duk_memset_unsafe(dst,val,len) do { \
  2716. void *duk__dst = (dst); \
  2717. duk_small_int_t duk__val = (val); \
  2718. duk_size_t duk__len = (len); \
  2719. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2720. if (DUK_LIKELY(duk__len > 0U)) { \
  2721. DUK_ASSERT(duk__dst != NULL); \
  2722. (void) DUK_MEMSET(duk__dst, duk__val, (size_t) duk__len); \
  2723. } \
  2724. } while (0)
  2725. #define duk_memzero(dst,len) do { \
  2726. void *duk__dst = (dst); \
  2727. duk_size_t duk__len = (len); \
  2728. DUK_ASSERT(duk__dst != NULL); \
  2729. (void) DUK_MEMZERO(duk__dst, (size_t) duk__len); \
  2730. } while (0)
  2731. #define duk_memzero_unsafe(dst,len) do { \
  2732. void *duk__dst = (dst); \
  2733. duk_size_t duk__len = (len); \
  2734. DUK_ASSERT(duk__dst != NULL || duk__len == 0U); \
  2735. if (DUK_LIKELY(duk__len > 0U)) { \
  2736. DUK_ASSERT(duk__dst != NULL); \
  2737. (void) DUK_MEMZERO(duk__dst, (size_t) duk__len); \
  2738. } \
  2739. } while (0)
  2740. #endif /* DUK_USE_ALLOW_UNDEFINED_BEHAVIOR */
  2741. DUK_INTERNAL_DECL duk_small_int_t duk_memcmp(const void *s1, const void *s2, duk_size_t len);
  2742. DUK_INTERNAL_DECL duk_small_int_t duk_memcmp_unsafe(const void *s1, const void *s2, duk_size_t len);
  2743. DUK_INTERNAL_DECL duk_bool_t duk_is_whole_get_int32_nonegzero(duk_double_t x, duk_int32_t *ival);
  2744. DUK_INTERNAL_DECL duk_bool_t duk_is_whole_get_int32(duk_double_t x, duk_int32_t *ival);
  2745. DUK_INTERNAL_DECL duk_bool_t duk_double_is_anyinf(duk_double_t x);
  2746. DUK_INTERNAL_DECL duk_bool_t duk_double_is_posinf(duk_double_t x);
  2747. DUK_INTERNAL_DECL duk_bool_t duk_double_is_neginf(duk_double_t x);
  2748. DUK_INTERNAL_DECL duk_bool_t duk_double_is_nan(duk_double_t x);
  2749. DUK_INTERNAL_DECL duk_bool_t duk_double_is_nan_or_zero(duk_double_t x);
  2750. DUK_INTERNAL_DECL duk_bool_t duk_double_is_nan_or_inf(duk_double_t x);
  2751. DUK_INTERNAL_DECL duk_bool_t duk_double_is_nan_zero_inf(duk_double_t x);
  2752. DUK_INTERNAL_DECL duk_small_uint_t duk_double_signbit(duk_double_t x);
  2753. DUK_INTERNAL_DECL duk_double_t duk_double_trunc_towards_zero(duk_double_t x);
  2754. DUK_INTERNAL_DECL duk_bool_t duk_double_same_sign(duk_double_t x, duk_double_t y);
  2755. DUK_INTERNAL_DECL duk_double_t duk_double_fmin(duk_double_t x, duk_double_t y);
  2756. DUK_INTERNAL_DECL duk_double_t duk_double_fmax(duk_double_t x, duk_double_t y);
  2757. DUK_INTERNAL_DECL duk_bool_t duk_double_is_finite(duk_double_t x);
  2758. DUK_INTERNAL_DECL duk_bool_t duk_double_is_integer(duk_double_t x);
  2759. DUK_INTERNAL_DECL duk_bool_t duk_double_is_safe_integer(duk_double_t x);
  2760. DUK_INTERNAL_DECL duk_double_t duk_double_div(duk_double_t x, duk_double_t y);
  2761. DUK_INTERNAL_DECL duk_int_t duk_double_to_int_t(duk_double_t x);
  2762. DUK_INTERNAL_DECL duk_uint_t duk_double_to_uint_t(duk_double_t x);
  2763. DUK_INTERNAL_DECL duk_int32_t duk_double_to_int32_t(duk_double_t x);
  2764. DUK_INTERNAL_DECL duk_uint32_t duk_double_to_uint32_t(duk_double_t x);
  2765. DUK_INTERNAL_DECL duk_float_t duk_double_to_float_t(duk_double_t x);
  2766. /*
  2767. * Miscellaneous
  2768. */
  2769. /* Example: x = 0x10 = 0b00010000
  2770. * x - 1 = 0x0f = 0b00001111
  2771. * x & (x - 1) == 0
  2772. *
  2773. * x = 0x07 = 0b00000111
  2774. * x - 1 = 0x06 = 0b00000110
  2775. * x & (x - 1) != 0
  2776. *
  2777. * However, incorrectly true for x == 0 so check for that explicitly.
  2778. */
  2779. #define DUK_IS_POWER_OF_TWO(x) \
  2780. ((x) != 0U && ((x) & ((x) - 1U)) == 0U)
  2781. #endif /* DUK_UTIL_H_INCLUDED */
  2782. /* #include duk_strings.h */
  2783. #line 1 "duk_strings.h"
  2784. /*
  2785. * Shared string macros.
  2786. *
  2787. * Using shared macros helps minimize strings data size because it's easy
  2788. * to check if an existing string could be used. String constants don't
  2789. * need to be all defined here; defining a string here makes sense if there's
  2790. * a high chance the string could be reused. Also, using macros allows
  2791. * a call site express the exact string needed, but the macro may map to an
  2792. * approximate string to reduce unique string count. Macros can also be
  2793. * more easily tuned for low memory targets than #if defined()s throughout
  2794. * the code base.
  2795. *
  2796. * Because format strings behave differently in the call site (they need to
  2797. * be followed by format arguments), they use a special prefix DUK_STR_FMT_.
  2798. *
  2799. * On some compilers using explicit shared strings is preferable; on others
  2800. * it may be better to use straight literals because the compiler will combine
  2801. * them anyway, and such strings won't end up unnecessarily in a symbol table.
  2802. */
  2803. #if !defined(DUK_ERRMSG_H_INCLUDED)
  2804. #define DUK_ERRMSG_H_INCLUDED
  2805. /* Mostly API and built-in method related */
  2806. #define DUK_STR_INTERNAL_ERROR "internal error"
  2807. #define DUK_STR_UNSUPPORTED "unsupported"
  2808. #define DUK_STR_INVALID_COUNT "invalid count"
  2809. #define DUK_STR_INVALID_ARGS "invalid args"
  2810. #define DUK_STR_INVALID_STATE "invalid state"
  2811. #define DUK_STR_INVALID_INPUT "invalid input"
  2812. #define DUK_STR_INVALID_LENGTH "invalid length"
  2813. #define DUK_STR_NOT_CONSTRUCTABLE "not constructable"
  2814. #define DUK_STR_CONSTRUCT_ONLY "constructor requires 'new'"
  2815. #define DUK_STR_NOT_CALLABLE "not callable"
  2816. #define DUK_STR_NOT_EXTENSIBLE "not extensible"
  2817. #define DUK_STR_NOT_WRITABLE "not writable"
  2818. #define DUK_STR_NOT_CONFIGURABLE "not configurable"
  2819. #define DUK_STR_INVALID_CONTEXT "invalid context"
  2820. #define DUK_STR_INVALID_INDEX "invalid args"
  2821. #define DUK_STR_PUSH_BEYOND_ALLOC_STACK "cannot push beyond allocated stack"
  2822. #define DUK_STR_NOT_UNDEFINED "unexpected type"
  2823. #define DUK_STR_NOT_NULL "unexpected type"
  2824. #define DUK_STR_NOT_BOOLEAN "unexpected type"
  2825. #define DUK_STR_NOT_NUMBER "unexpected type"
  2826. #define DUK_STR_NOT_STRING "unexpected type"
  2827. #define DUK_STR_NOT_OBJECT "unexpected type"
  2828. #define DUK_STR_NOT_POINTER "unexpected type"
  2829. #define DUK_STR_NOT_BUFFER "not buffer" /* still in use with verbose messages */
  2830. #define DUK_STR_UNEXPECTED_TYPE "unexpected type"
  2831. #define DUK_STR_NOT_THREAD "unexpected type"
  2832. #define DUK_STR_NOT_COMPFUNC "unexpected type"
  2833. #define DUK_STR_NOT_NATFUNC "unexpected type"
  2834. #define DUK_STR_NOT_C_FUNCTION "unexpected type"
  2835. #define DUK_STR_NOT_FUNCTION "unexpected type"
  2836. #define DUK_STR_NOT_REGEXP "unexpected type"
  2837. #define DUK_STR_TOPRIMITIVE_FAILED "coercion to primitive failed"
  2838. #define DUK_STR_NUMBER_OUTSIDE_RANGE "number outside range"
  2839. #define DUK_STR_NOT_OBJECT_COERCIBLE "not object coercible"
  2840. #define DUK_STR_CANNOT_NUMBER_COERCE_SYMBOL "cannot number coerce Symbol"
  2841. #define DUK_STR_CANNOT_STRING_COERCE_SYMBOL "cannot string coerce Symbol"
  2842. #define DUK_STR_STRING_TOO_LONG "string too long"
  2843. #define DUK_STR_BUFFER_TOO_LONG "buffer too long"
  2844. #define DUK_STR_ALLOC_FAILED "alloc failed"
  2845. #define DUK_STR_WRONG_BUFFER_TYPE "wrong buffer type"
  2846. #define DUK_STR_BASE64_ENCODE_FAILED "base64 encode failed"
  2847. #define DUK_STR_SOURCE_DECODE_FAILED "source decode failed"
  2848. #define DUK_STR_UTF8_DECODE_FAILED "utf-8 decode failed"
  2849. #define DUK_STR_BASE64_DECODE_FAILED "base64 decode failed"
  2850. #define DUK_STR_HEX_DECODE_FAILED "hex decode failed"
  2851. #define DUK_STR_INVALID_BYTECODE "invalid bytecode"
  2852. #define DUK_STR_NO_SOURCECODE "no sourcecode"
  2853. #define DUK_STR_RESULT_TOO_LONG "result too long"
  2854. #define DUK_STR_INVALID_CFUNC_RC "invalid C function rc"
  2855. #define DUK_STR_INVALID_INSTANCEOF_RVAL "invalid instanceof rval"
  2856. #define DUK_STR_INVALID_INSTANCEOF_RVAL_NOPROTO "instanceof rval has no .prototype"
  2857. /* JSON */
  2858. #define DUK_STR_FMT_PTR "%p"
  2859. #define DUK_STR_FMT_INVALID_JSON "invalid json (at offset %ld)"
  2860. #define DUK_STR_JSONDEC_RECLIMIT "json decode recursion limit"
  2861. #define DUK_STR_JSONENC_RECLIMIT "json encode recursion limit"
  2862. #define DUK_STR_CYCLIC_INPUT "cyclic input"
  2863. /* Object property access */
  2864. #define DUK_STR_INVALID_BASE "invalid base value"
  2865. #define DUK_STR_STRICT_CALLER_READ "cannot read strict 'caller'"
  2866. #define DUK_STR_PROXY_REJECTED "proxy rejected"
  2867. #define DUK_STR_INVALID_ARRAY_LENGTH "invalid array length"
  2868. #define DUK_STR_SETTER_UNDEFINED "setter undefined"
  2869. #define DUK_STR_INVALID_DESCRIPTOR "invalid descriptor"
  2870. /* Proxy */
  2871. #define DUK_STR_PROXY_REVOKED "proxy revoked"
  2872. #define DUK_STR_INVALID_TRAP_RESULT "invalid trap result"
  2873. /* Variables */
  2874. /* Lexer */
  2875. #define DUK_STR_INVALID_ESCAPE "invalid escape"
  2876. #define DUK_STR_UNTERMINATED_STRING "unterminated string"
  2877. #define DUK_STR_UNTERMINATED_COMMENT "unterminated comment"
  2878. #define DUK_STR_UNTERMINATED_REGEXP "unterminated regexp"
  2879. #define DUK_STR_TOKEN_LIMIT "token limit"
  2880. #define DUK_STR_REGEXP_SUPPORT_DISABLED "regexp support disabled"
  2881. #define DUK_STR_INVALID_NUMBER_LITERAL "invalid number literal"
  2882. #define DUK_STR_INVALID_TOKEN "invalid token"
  2883. /* Compiler */
  2884. #define DUK_STR_PARSE_ERROR "parse error"
  2885. #define DUK_STR_DUPLICATE_LABEL "duplicate label"
  2886. #define DUK_STR_INVALID_LABEL "invalid label"
  2887. #define DUK_STR_INVALID_ARRAY_LITERAL "invalid array literal"
  2888. #define DUK_STR_INVALID_OBJECT_LITERAL "invalid object literal"
  2889. #define DUK_STR_INVALID_VAR_DECLARATION "invalid variable declaration"
  2890. #define DUK_STR_CANNOT_DELETE_IDENTIFIER "cannot delete identifier"
  2891. #define DUK_STR_INVALID_EXPRESSION "invalid expression"
  2892. #define DUK_STR_INVALID_LVALUE "invalid lvalue"
  2893. #define DUK_STR_INVALID_NEWTARGET "invalid new.target"
  2894. #define DUK_STR_EXPECTED_IDENTIFIER "expected identifier"
  2895. #define DUK_STR_EMPTY_EXPR_NOT_ALLOWED "empty expression not allowed"
  2896. #define DUK_STR_INVALID_FOR "invalid for statement"
  2897. #define DUK_STR_INVALID_SWITCH "invalid switch statement"
  2898. #define DUK_STR_INVALID_BREAK_CONT_LABEL "invalid break/continue label"
  2899. #define DUK_STR_INVALID_RETURN "invalid return"
  2900. #define DUK_STR_INVALID_TRY "invalid try"
  2901. #define DUK_STR_INVALID_THROW "invalid throw"
  2902. #define DUK_STR_WITH_IN_STRICT_MODE "with in strict mode"
  2903. #define DUK_STR_FUNC_STMT_NOT_ALLOWED "function statement not allowed"
  2904. #define DUK_STR_UNTERMINATED_STMT "unterminated statement"
  2905. #define DUK_STR_INVALID_ARG_NAME "invalid argument name"
  2906. #define DUK_STR_INVALID_FUNC_NAME "invalid function name"
  2907. #define DUK_STR_INVALID_GETSET_NAME "invalid getter/setter name"
  2908. #define DUK_STR_FUNC_NAME_REQUIRED "function name required"
  2909. /* RegExp */
  2910. #define DUK_STR_INVALID_QUANTIFIER "invalid regexp quantifier"
  2911. #define DUK_STR_INVALID_QUANTIFIER_NO_ATOM "quantifier without preceding atom"
  2912. #define DUK_STR_INVALID_QUANTIFIER_VALUES "quantifier values invalid (qmin > qmax)"
  2913. #define DUK_STR_QUANTIFIER_TOO_MANY_COPIES "quantifier requires too many atom copies"
  2914. #define DUK_STR_UNEXPECTED_CLOSING_PAREN "unexpected closing parenthesis"
  2915. #define DUK_STR_UNEXPECTED_END_OF_PATTERN "unexpected end of pattern"
  2916. #define DUK_STR_UNEXPECTED_REGEXP_TOKEN "unexpected token in regexp"
  2917. #define DUK_STR_INVALID_REGEXP_FLAGS "invalid regexp flags"
  2918. #define DUK_STR_INVALID_REGEXP_ESCAPE "invalid regexp escape"
  2919. #define DUK_STR_INVALID_BACKREFS "invalid backreference(s)"
  2920. #define DUK_STR_INVALID_REGEXP_CHARACTER "invalid regexp character"
  2921. #define DUK_STR_INVALID_REGEXP_GROUP "invalid regexp group"
  2922. #define DUK_STR_UNTERMINATED_CHARCLASS "unterminated character class"
  2923. #define DUK_STR_INVALID_RANGE "invalid range"
  2924. /* Limits */
  2925. #define DUK_STR_VALSTACK_LIMIT "valstack limit"
  2926. #define DUK_STR_CALLSTACK_LIMIT "callstack limit"
  2927. #define DUK_STR_PROTOTYPE_CHAIN_LIMIT "prototype chain limit"
  2928. #define DUK_STR_BOUND_CHAIN_LIMIT "function call bound chain limit"
  2929. #define DUK_STR_C_CALLSTACK_LIMIT "C call stack depth limit"
  2930. #define DUK_STR_COMPILER_RECURSION_LIMIT "compiler recursion limit"
  2931. #define DUK_STR_BYTECODE_LIMIT "bytecode limit"
  2932. #define DUK_STR_REG_LIMIT "register limit"
  2933. #define DUK_STR_TEMP_LIMIT "temp limit"
  2934. #define DUK_STR_CONST_LIMIT "const limit"
  2935. #define DUK_STR_FUNC_LIMIT "function limit"
  2936. #define DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT "regexp compiler recursion limit"
  2937. #define DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT "regexp executor recursion limit"
  2938. #define DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT "regexp step limit"
  2939. #endif /* DUK_ERRMSG_H_INCLUDED */
  2940. /* #include duk_js_bytecode.h */
  2941. #line 1 "duk_js_bytecode.h"
  2942. /*
  2943. * ECMAScript bytecode
  2944. */
  2945. #if !defined(DUK_JS_BYTECODE_H_INCLUDED)
  2946. #define DUK_JS_BYTECODE_H_INCLUDED
  2947. /*
  2948. * Bytecode instruction layout
  2949. * ===========================
  2950. *
  2951. * Instructions are unsigned 32-bit integers divided as follows:
  2952. *
  2953. * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! !
  2954. * !1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!
  2955. * +-----------------------------------------------+---------------+
  2956. * ! C ! B ! A ! OP !
  2957. * +-----------------------------------------------+---------------+
  2958. *
  2959. * OP (8 bits): opcode (DUK_OP_*), access should be fastest
  2960. * consecutive opcodes allocated when opcode needs flags
  2961. * A (8 bits): typically a target register number
  2962. * B (8 bits): typically first source register/constant number
  2963. * C (8 bits): typically second source register/constant number
  2964. *
  2965. * Some instructions combine BC or ABC together for larger parameter values.
  2966. * Signed integers (e.g. jump offsets) are encoded as unsigned, with an
  2967. * opcode specific bias.
  2968. *
  2969. * Some opcodes have flags which are handled by allocating consecutive
  2970. * opcodes to make space for 1-N flags. Flags can also be e.g. in the 'A'
  2971. * field when there's room for the specific opcode.
  2972. *
  2973. * For example, if three flags were needed, they could be allocated from
  2974. * the opcode field as follows:
  2975. *
  2976. * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! !
  2977. * !1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!
  2978. * +-----------------------------------------------+---------------+
  2979. * ! C ! B ! A ! OP !Z!Y!X!
  2980. * +-----------------------------------------------+---------------+
  2981. *
  2982. * Some opcodes accept a reg/const argument which is handled by allocating
  2983. * flags in the OP field, see DUK_BC_ISREG() and DUK_BC_ISCONST(). The
  2984. * following convention is shared by most opcodes, so that the compiler
  2985. * can handle reg/const flagging without opcode specific code paths:
  2986. *
  2987. * !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! !
  2988. * !1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!
  2989. * +-----------------------------------------------+---------------+
  2990. * ! C ! B ! A ! OP !Y!X!
  2991. * +-----------------------------------------------+---------------+
  2992. *
  2993. * X 1=B is const, 0=B is reg
  2994. * Y 1=C is const, 0=C is reg
  2995. *
  2996. * In effect OP, OP + 1, OP + 2, and OP + 3 are allocated from the
  2997. * 8-bit opcode space for a single logical opcode. The base opcode
  2998. * number should be divisible by 4. If the opcode is called 'FOO'
  2999. * the following opcode constants would be defined:
  3000. *
  3001. * DUK_OP_FOO 100 // base opcode number
  3002. * DUK_OP_FOO_RR 100 // FOO, B=reg, C=reg
  3003. * DUK_OP_FOO_CR 101 // FOO, B=const, C=reg
  3004. * DUK_OP_FOO_RC 102 // FOO, B=reg, C=const
  3005. * DUK_OP_FOO_CC 103 // FOO, B=const, C=const
  3006. *
  3007. * If only B or C is a reg/const, the unused opcode combinations can be
  3008. * used for other opcodes (which take no reg/const argument). However,
  3009. * such opcode values are initially reserved, at least while opcode space
  3010. * is available. For example, if 'BAR' uses B for a register field and
  3011. * C is a reg/const:
  3012. *
  3013. * DUK_OP_BAR 116 // base opcode number
  3014. * DUK_OP_BAR_RR 116 // BAR, B=reg, C=reg
  3015. * DUK_OP_BAR_CR_UNUSED 117 // unused, could be repurposed
  3016. * DUK_OP_BAR_RC 118 // BAR, B=reg, C=const
  3017. * DUK_OP_BAR_CC_UNUSED 119 // unused, could be repurposed
  3018. *
  3019. * Macro naming is a bit misleading, e.g. "ABC" in macro name but the
  3020. * field layout is concretely "CBA" in the register.
  3021. */
  3022. typedef duk_uint32_t duk_instr_t;
  3023. #define DUK_BC_SHIFT_OP 0
  3024. #define DUK_BC_SHIFT_A 8
  3025. #define DUK_BC_SHIFT_B 16
  3026. #define DUK_BC_SHIFT_C 24
  3027. #define DUK_BC_SHIFT_BC DUK_BC_SHIFT_B
  3028. #define DUK_BC_SHIFT_ABC DUK_BC_SHIFT_A
  3029. #define DUK_BC_UNSHIFTED_MASK_OP 0xffUL
  3030. #define DUK_BC_UNSHIFTED_MASK_A 0xffUL
  3031. #define DUK_BC_UNSHIFTED_MASK_B 0xffUL
  3032. #define DUK_BC_UNSHIFTED_MASK_C 0xffUL
  3033. #define DUK_BC_UNSHIFTED_MASK_BC 0xffffUL
  3034. #define DUK_BC_UNSHIFTED_MASK_ABC 0xffffffUL
  3035. #define DUK_BC_SHIFTED_MASK_OP (DUK_BC_UNSHIFTED_MASK_OP << DUK_BC_SHIFT_OP)
  3036. #define DUK_BC_SHIFTED_MASK_A (DUK_BC_UNSHIFTED_MASK_A << DUK_BC_SHIFT_A)
  3037. #define DUK_BC_SHIFTED_MASK_B (DUK_BC_UNSHIFTED_MASK_B << DUK_BC_SHIFT_B)
  3038. #define DUK_BC_SHIFTED_MASK_C (DUK_BC_UNSHIFTED_MASK_C << DUK_BC_SHIFT_C)
  3039. #define DUK_BC_SHIFTED_MASK_BC (DUK_BC_UNSHIFTED_MASK_BC << DUK_BC_SHIFT_BC)
  3040. #define DUK_BC_SHIFTED_MASK_ABC (DUK_BC_UNSHIFTED_MASK_ABC << DUK_BC_SHIFT_ABC)
  3041. #define DUK_DEC_OP(x) ((x) & 0xffUL)
  3042. #define DUK_DEC_A(x) (((x) >> 8) & 0xffUL)
  3043. #define DUK_DEC_B(x) (((x) >> 16) & 0xffUL)
  3044. #define DUK_DEC_C(x) (((x) >> 24) & 0xffUL)
  3045. #define DUK_DEC_BC(x) (((x) >> 16) & 0xffffUL)
  3046. #define DUK_DEC_ABC(x) (((x) >> 8) & 0xffffffUL)
  3047. #define DUK_ENC_OP(op) ((duk_instr_t) (op))
  3048. #define DUK_ENC_OP_ABC(op,abc) ((duk_instr_t) ( \
  3049. (((duk_instr_t) (abc)) << 8) | \
  3050. ((duk_instr_t) (op)) \
  3051. ))
  3052. #define DUK_ENC_OP_A_BC(op,a,bc) ((duk_instr_t) ( \
  3053. (((duk_instr_t) (bc)) << 16) | \
  3054. (((duk_instr_t) (a)) << 8) | \
  3055. ((duk_instr_t) (op)) \
  3056. ))
  3057. #define DUK_ENC_OP_A_B_C(op,a,b,c) ((duk_instr_t) ( \
  3058. (((duk_instr_t) (c)) << 24) | \
  3059. (((duk_instr_t) (b)) << 16) | \
  3060. (((duk_instr_t) (a)) << 8) | \
  3061. ((duk_instr_t) (op)) \
  3062. ))
  3063. #define DUK_ENC_OP_A_B(op,a,b) DUK_ENC_OP_A_B_C((op),(a),(b),0)
  3064. #define DUK_ENC_OP_A(op,a) DUK_ENC_OP_A_B_C((op),(a),0,0)
  3065. #define DUK_ENC_OP_BC(op,bc) DUK_ENC_OP_A_BC((op),0,(bc))
  3066. /* Get opcode base value with B/C reg/const flags cleared. */
  3067. #define DUK_BC_NOREGCONST_OP(op) ((op) & 0xfc)
  3068. /* Constants should be signed so that signed arithmetic involving them
  3069. * won't cause values to be coerced accidentally to unsigned.
  3070. */
  3071. #define DUK_BC_OP_MIN 0
  3072. #define DUK_BC_OP_MAX 0xffL
  3073. #define DUK_BC_A_MIN 0
  3074. #define DUK_BC_A_MAX 0xffL
  3075. #define DUK_BC_B_MIN 0
  3076. #define DUK_BC_B_MAX 0xffL
  3077. #define DUK_BC_C_MIN 0
  3078. #define DUK_BC_C_MAX 0xffL
  3079. #define DUK_BC_BC_MIN 0
  3080. #define DUK_BC_BC_MAX 0xffffL
  3081. #define DUK_BC_ABC_MIN 0
  3082. #define DUK_BC_ABC_MAX 0xffffffL
  3083. /* Masks for B/C reg/const indicator in opcode field. */
  3084. #define DUK_BC_REGCONST_B (0x01UL)
  3085. #define DUK_BC_REGCONST_C (0x02UL)
  3086. /* Misc. masks for opcode field. */
  3087. #define DUK_BC_INCDECP_FLAG_DEC (0x04UL)
  3088. #define DUK_BC_INCDECP_FLAG_POST (0x08UL)
  3089. /* Opcodes. */
  3090. #define DUK_OP_LDREG 0
  3091. #define DUK_OP_STREG 1
  3092. #define DUK_OP_JUMP 2
  3093. #define DUK_OP_LDCONST 3
  3094. #define DUK_OP_LDINT 4
  3095. #define DUK_OP_LDINTX 5
  3096. #define DUK_OP_LDTHIS 6
  3097. #define DUK_OP_LDUNDEF 7
  3098. #define DUK_OP_LDNULL 8
  3099. #define DUK_OP_LDTRUE 9
  3100. #define DUK_OP_LDFALSE 10
  3101. #define DUK_OP_GETVAR 11
  3102. #define DUK_OP_BNOT 12
  3103. #define DUK_OP_LNOT 13
  3104. #define DUK_OP_UNM 14
  3105. #define DUK_OP_UNP 15
  3106. #define DUK_OP_EQ 16
  3107. #define DUK_OP_EQ_RR 16
  3108. #define DUK_OP_EQ_CR 17
  3109. #define DUK_OP_EQ_RC 18
  3110. #define DUK_OP_EQ_CC 19
  3111. #define DUK_OP_NEQ 20
  3112. #define DUK_OP_NEQ_RR 20
  3113. #define DUK_OP_NEQ_CR 21
  3114. #define DUK_OP_NEQ_RC 22
  3115. #define DUK_OP_NEQ_CC 23
  3116. #define DUK_OP_SEQ 24
  3117. #define DUK_OP_SEQ_RR 24
  3118. #define DUK_OP_SEQ_CR 25
  3119. #define DUK_OP_SEQ_RC 26
  3120. #define DUK_OP_SEQ_CC 27
  3121. #define DUK_OP_SNEQ 28
  3122. #define DUK_OP_SNEQ_RR 28
  3123. #define DUK_OP_SNEQ_CR 29
  3124. #define DUK_OP_SNEQ_RC 30
  3125. #define DUK_OP_SNEQ_CC 31
  3126. #define DUK_OP_GT 32
  3127. #define DUK_OP_GT_RR 32
  3128. #define DUK_OP_GT_CR 33
  3129. #define DUK_OP_GT_RC 34
  3130. #define DUK_OP_GT_CC 35
  3131. #define DUK_OP_GE 36
  3132. #define DUK_OP_GE_RR 36
  3133. #define DUK_OP_GE_CR 37
  3134. #define DUK_OP_GE_RC 38
  3135. #define DUK_OP_GE_CC 39
  3136. #define DUK_OP_LT 40
  3137. #define DUK_OP_LT_RR 40
  3138. #define DUK_OP_LT_CR 41
  3139. #define DUK_OP_LT_RC 42
  3140. #define DUK_OP_LT_CC 43
  3141. #define DUK_OP_LE 44
  3142. #define DUK_OP_LE_RR 44
  3143. #define DUK_OP_LE_CR 45
  3144. #define DUK_OP_LE_RC 46
  3145. #define DUK_OP_LE_CC 47
  3146. #define DUK_OP_IFTRUE 48
  3147. #define DUK_OP_IFTRUE_R 48
  3148. #define DUK_OP_IFTRUE_C 49
  3149. #define DUK_OP_IFFALSE 50
  3150. #define DUK_OP_IFFALSE_R 50
  3151. #define DUK_OP_IFFALSE_C 51
  3152. #define DUK_OP_ADD 52
  3153. #define DUK_OP_ADD_RR 52
  3154. #define DUK_OP_ADD_CR 53
  3155. #define DUK_OP_ADD_RC 54
  3156. #define DUK_OP_ADD_CC 55
  3157. #define DUK_OP_SUB 56
  3158. #define DUK_OP_SUB_RR 56
  3159. #define DUK_OP_SUB_CR 57
  3160. #define DUK_OP_SUB_RC 58
  3161. #define DUK_OP_SUB_CC 59
  3162. #define DUK_OP_MUL 60
  3163. #define DUK_OP_MUL_RR 60
  3164. #define DUK_OP_MUL_CR 61
  3165. #define DUK_OP_MUL_RC 62
  3166. #define DUK_OP_MUL_CC 63
  3167. #define DUK_OP_DIV 64
  3168. #define DUK_OP_DIV_RR 64
  3169. #define DUK_OP_DIV_CR 65
  3170. #define DUK_OP_DIV_RC 66
  3171. #define DUK_OP_DIV_CC 67
  3172. #define DUK_OP_MOD 68
  3173. #define DUK_OP_MOD_RR 68
  3174. #define DUK_OP_MOD_CR 69
  3175. #define DUK_OP_MOD_RC 70
  3176. #define DUK_OP_MOD_CC 71
  3177. #define DUK_OP_EXP 72
  3178. #define DUK_OP_EXP_RR 72
  3179. #define DUK_OP_EXP_CR 73
  3180. #define DUK_OP_EXP_RC 74
  3181. #define DUK_OP_EXP_CC 75
  3182. #define DUK_OP_BAND 76
  3183. #define DUK_OP_BAND_RR 76
  3184. #define DUK_OP_BAND_CR 77
  3185. #define DUK_OP_BAND_RC 78
  3186. #define DUK_OP_BAND_CC 79
  3187. #define DUK_OP_BOR 80
  3188. #define DUK_OP_BOR_RR 80
  3189. #define DUK_OP_BOR_CR 81
  3190. #define DUK_OP_BOR_RC 82
  3191. #define DUK_OP_BOR_CC 83
  3192. #define DUK_OP_BXOR 84
  3193. #define DUK_OP_BXOR_RR 84
  3194. #define DUK_OP_BXOR_CR 85
  3195. #define DUK_OP_BXOR_RC 86
  3196. #define DUK_OP_BXOR_CC 87
  3197. #define DUK_OP_BASL 88
  3198. #define DUK_OP_BASL_RR 88
  3199. #define DUK_OP_BASL_CR 89
  3200. #define DUK_OP_BASL_RC 90
  3201. #define DUK_OP_BASL_CC 91
  3202. #define DUK_OP_BLSR 92
  3203. #define DUK_OP_BLSR_RR 92
  3204. #define DUK_OP_BLSR_CR 93
  3205. #define DUK_OP_BLSR_RC 94
  3206. #define DUK_OP_BLSR_CC 95
  3207. #define DUK_OP_BASR 96
  3208. #define DUK_OP_BASR_RR 96
  3209. #define DUK_OP_BASR_CR 97
  3210. #define DUK_OP_BASR_RC 98
  3211. #define DUK_OP_BASR_CC 99
  3212. #define DUK_OP_INSTOF 100
  3213. #define DUK_OP_INSTOF_RR 100
  3214. #define DUK_OP_INSTOF_CR 101
  3215. #define DUK_OP_INSTOF_RC 102
  3216. #define DUK_OP_INSTOF_CC 103
  3217. #define DUK_OP_IN 104
  3218. #define DUK_OP_IN_RR 104
  3219. #define DUK_OP_IN_CR 105
  3220. #define DUK_OP_IN_RC 106
  3221. #define DUK_OP_IN_CC 107
  3222. #define DUK_OP_GETPROP 108
  3223. #define DUK_OP_GETPROP_RR 108
  3224. #define DUK_OP_GETPROP_CR 109
  3225. #define DUK_OP_GETPROP_RC 110
  3226. #define DUK_OP_GETPROP_CC 111
  3227. #define DUK_OP_PUTPROP 112
  3228. #define DUK_OP_PUTPROP_RR 112
  3229. #define DUK_OP_PUTPROP_CR 113
  3230. #define DUK_OP_PUTPROP_RC 114
  3231. #define DUK_OP_PUTPROP_CC 115
  3232. #define DUK_OP_DELPROP 116
  3233. #define DUK_OP_DELPROP_RR 116
  3234. #define DUK_OP_DELPROP_CR_UNUSED 117 /* unused now */
  3235. #define DUK_OP_DELPROP_RC 118
  3236. #define DUK_OP_DELPROP_CC_UNUSED 119 /* unused now */
  3237. #define DUK_OP_PREINCR 120 /* pre/post opcode values have constraints, */
  3238. #define DUK_OP_PREDECR 121 /* see duk_js_executor.c and duk_js_compiler.c. */
  3239. #define DUK_OP_POSTINCR 122
  3240. #define DUK_OP_POSTDECR 123
  3241. #define DUK_OP_PREINCV 124
  3242. #define DUK_OP_PREDECV 125
  3243. #define DUK_OP_POSTINCV 126
  3244. #define DUK_OP_POSTDECV 127
  3245. #define DUK_OP_PREINCP 128 /* pre/post inc/dec prop opcodes have constraints */
  3246. #define DUK_OP_PREINCP_RR 128
  3247. #define DUK_OP_PREINCP_CR 129
  3248. #define DUK_OP_PREINCP_RC 130
  3249. #define DUK_OP_PREINCP_CC 131
  3250. #define DUK_OP_PREDECP 132
  3251. #define DUK_OP_PREDECP_RR 132
  3252. #define DUK_OP_PREDECP_CR 133
  3253. #define DUK_OP_PREDECP_RC 134
  3254. #define DUK_OP_PREDECP_CC 135
  3255. #define DUK_OP_POSTINCP 136
  3256. #define DUK_OP_POSTINCP_RR 136
  3257. #define DUK_OP_POSTINCP_CR 137
  3258. #define DUK_OP_POSTINCP_RC 138
  3259. #define DUK_OP_POSTINCP_CC 139
  3260. #define DUK_OP_POSTDECP 140
  3261. #define DUK_OP_POSTDECP_RR 140
  3262. #define DUK_OP_POSTDECP_CR 141
  3263. #define DUK_OP_POSTDECP_RC 142
  3264. #define DUK_OP_POSTDECP_CC 143
  3265. #define DUK_OP_DECLVAR 144
  3266. #define DUK_OP_DECLVAR_RR 144
  3267. #define DUK_OP_DECLVAR_CR 145
  3268. #define DUK_OP_DECLVAR_RC 146
  3269. #define DUK_OP_DECLVAR_CC 147
  3270. #define DUK_OP_REGEXP 148
  3271. #define DUK_OP_REGEXP_RR 148
  3272. #define DUK_OP_REGEXP_CR 149
  3273. #define DUK_OP_REGEXP_RC 150
  3274. #define DUK_OP_REGEXP_CC 151
  3275. #define DUK_OP_CLOSURE 152
  3276. #define DUK_OP_TYPEOF 153
  3277. #define DUK_OP_TYPEOFID 154
  3278. #define DUK_OP_PUTVAR 155
  3279. #define DUK_OP_DELVAR 156
  3280. #define DUK_OP_RETREG 157
  3281. #define DUK_OP_RETUNDEF 158
  3282. #define DUK_OP_RETCONST 159
  3283. #define DUK_OP_RETCONSTN 160 /* return const without incref (e.g. number) */
  3284. #define DUK_OP_LABEL 161
  3285. #define DUK_OP_ENDLABEL 162
  3286. #define DUK_OP_BREAK 163
  3287. #define DUK_OP_CONTINUE 164
  3288. #define DUK_OP_TRYCATCH 165
  3289. #define DUK_OP_ENDTRY 166
  3290. #define DUK_OP_ENDCATCH 167
  3291. #define DUK_OP_ENDFIN 168
  3292. #define DUK_OP_THROW 169
  3293. #define DUK_OP_INVLHS 170
  3294. #define DUK_OP_CSREG 171
  3295. #define DUK_OP_CSVAR 172
  3296. #define DUK_OP_CSVAR_RR 172
  3297. #define DUK_OP_CSVAR_CR 173
  3298. #define DUK_OP_CSVAR_RC 174
  3299. #define DUK_OP_CSVAR_CC 175
  3300. #define DUK_OP_CALL0 176 /* DUK_OP_CALL0 & 0x0F must be zero. */
  3301. #define DUK_OP_CALL1 177
  3302. #define DUK_OP_CALL2 178
  3303. #define DUK_OP_CALL3 179
  3304. #define DUK_OP_CALL4 180
  3305. #define DUK_OP_CALL5 181
  3306. #define DUK_OP_CALL6 182
  3307. #define DUK_OP_CALL7 183
  3308. #define DUK_OP_CALL8 184
  3309. #define DUK_OP_CALL9 185
  3310. #define DUK_OP_CALL10 186
  3311. #define DUK_OP_CALL11 187
  3312. #define DUK_OP_CALL12 188
  3313. #define DUK_OP_CALL13 189
  3314. #define DUK_OP_CALL14 190
  3315. #define DUK_OP_CALL15 191
  3316. #define DUK_OP_NEWOBJ 192
  3317. #define DUK_OP_NEWARR 193
  3318. #define DUK_OP_MPUTOBJ 194
  3319. #define DUK_OP_MPUTOBJI 195
  3320. #define DUK_OP_INITSET 196
  3321. #define DUK_OP_INITGET 197
  3322. #define DUK_OP_MPUTARR 198
  3323. #define DUK_OP_MPUTARRI 199
  3324. #define DUK_OP_SETALEN 200
  3325. #define DUK_OP_INITENUM 201
  3326. #define DUK_OP_NEXTENUM 202
  3327. #define DUK_OP_NEWTARGET 203
  3328. #define DUK_OP_DEBUGGER 204
  3329. #define DUK_OP_NOP 205
  3330. #define DUK_OP_INVALID 206
  3331. #define DUK_OP_UNUSED207 207
  3332. #define DUK_OP_GETPROPC 208
  3333. #define DUK_OP_GETPROPC_RR 208
  3334. #define DUK_OP_GETPROPC_CR 209
  3335. #define DUK_OP_GETPROPC_RC 210
  3336. #define DUK_OP_GETPROPC_CC 211
  3337. #define DUK_OP_UNUSED212 212
  3338. #define DUK_OP_UNUSED213 213
  3339. #define DUK_OP_UNUSED214 214
  3340. #define DUK_OP_UNUSED215 215
  3341. #define DUK_OP_UNUSED216 216
  3342. #define DUK_OP_UNUSED217 217
  3343. #define DUK_OP_UNUSED218 218
  3344. #define DUK_OP_UNUSED219 219
  3345. #define DUK_OP_UNUSED220 220
  3346. #define DUK_OP_UNUSED221 221
  3347. #define DUK_OP_UNUSED222 222
  3348. #define DUK_OP_UNUSED223 223
  3349. #define DUK_OP_UNUSED224 224
  3350. #define DUK_OP_UNUSED225 225
  3351. #define DUK_OP_UNUSED226 226
  3352. #define DUK_OP_UNUSED227 227
  3353. #define DUK_OP_UNUSED228 228
  3354. #define DUK_OP_UNUSED229 229
  3355. #define DUK_OP_UNUSED230 230
  3356. #define DUK_OP_UNUSED231 231
  3357. #define DUK_OP_UNUSED232 232
  3358. #define DUK_OP_UNUSED233 233
  3359. #define DUK_OP_UNUSED234 234
  3360. #define DUK_OP_UNUSED235 235
  3361. #define DUK_OP_UNUSED236 236
  3362. #define DUK_OP_UNUSED237 237
  3363. #define DUK_OP_UNUSED238 238
  3364. #define DUK_OP_UNUSED239 239
  3365. #define DUK_OP_UNUSED240 240
  3366. #define DUK_OP_UNUSED241 241
  3367. #define DUK_OP_UNUSED242 242
  3368. #define DUK_OP_UNUSED243 243
  3369. #define DUK_OP_UNUSED244 244
  3370. #define DUK_OP_UNUSED245 245
  3371. #define DUK_OP_UNUSED246 246
  3372. #define DUK_OP_UNUSED247 247
  3373. #define DUK_OP_UNUSED248 248
  3374. #define DUK_OP_UNUSED249 249
  3375. #define DUK_OP_UNUSED250 250
  3376. #define DUK_OP_UNUSED251 251
  3377. #define DUK_OP_UNUSED252 252
  3378. #define DUK_OP_UNUSED253 253
  3379. #define DUK_OP_UNUSED254 254
  3380. #define DUK_OP_UNUSED255 255
  3381. #define DUK_OP_NONE 256 /* dummy value used as marker (doesn't fit in 8-bit field) */
  3382. /* XXX: Allocate flags from opcode field? Would take 16 opcode slots
  3383. * but avoids shuffling in more cases. Maybe not worth it.
  3384. */
  3385. /* DUK_OP_TRYCATCH flags in A. */
  3386. #define DUK_BC_TRYCATCH_FLAG_HAVE_CATCH (1U << 0)
  3387. #define DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY (1U << 1)
  3388. #define DUK_BC_TRYCATCH_FLAG_CATCH_BINDING (1U << 2)
  3389. #define DUK_BC_TRYCATCH_FLAG_WITH_BINDING (1U << 3)
  3390. /* DUK_OP_DECLVAR flags in A; bottom bits are reserved for propdesc flags
  3391. * (DUK_PROPDESC_FLAG_XXX).
  3392. */
  3393. #define DUK_BC_DECLVAR_FLAG_FUNC_DECL (1U << 4) /* function declaration */
  3394. /* DUK_OP_CALLn flags, part of opcode field. Three lowest bits must match
  3395. * DUK_CALL_FLAG_xxx directly.
  3396. */
  3397. #define DUK_BC_CALL_FLAG_TAILCALL (1U << 0)
  3398. #define DUK_BC_CALL_FLAG_CONSTRUCT (1U << 1)
  3399. #define DUK_BC_CALL_FLAG_CALLED_AS_EVAL (1U << 2)
  3400. #define DUK_BC_CALL_FLAG_INDIRECT (1U << 3)
  3401. /* Misc constants and helper macros. */
  3402. #define DUK_BC_LDINT_BIAS (1L << 15)
  3403. #define DUK_BC_LDINTX_SHIFT 16
  3404. #define DUK_BC_JUMP_BIAS (1L << 23)
  3405. #endif /* DUK_JS_BYTECODE_H_INCLUDED */
  3406. /* #include duk_lexer.h */
  3407. #line 1 "duk_lexer.h"
  3408. /*
  3409. * Lexer defines.
  3410. */
  3411. #if !defined(DUK_LEXER_H_INCLUDED)
  3412. #define DUK_LEXER_H_INCLUDED
  3413. typedef void (*duk_re_range_callback)(void *user, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct);
  3414. /*
  3415. * A token is interpreted as any possible production of InputElementDiv
  3416. * and InputElementRegExp, see E5 Section 7 in its entirety. Note that
  3417. * the E5 "Token" production does not cover all actual tokens of the
  3418. * language (which is explicitly stated in the specification, Section 7.5).
  3419. * Null and boolean literals are defined as part of both ReservedWord
  3420. * (E5 Section 7.6.1) and Literal (E5 Section 7.8) productions. Here,
  3421. * null and boolean values have literal tokens, and are not reserved
  3422. * words.
  3423. *
  3424. * Decimal literal negative/positive sign is -not- part of DUK_TOK_NUMBER.
  3425. * The number tokens always have a non-negative value. The unary minus
  3426. * operator in "-1.0" is optimized during compilation to yield a single
  3427. * negative constant.
  3428. *
  3429. * Token numbering is free except that reserved words are required to be
  3430. * in a continuous range and in a particular order. See genstrings.py.
  3431. */
  3432. #define DUK_LEXER_INITCTX(ctx) duk_lexer_initctx((ctx))
  3433. #define DUK_LEXER_SETPOINT(ctx,pt) duk_lexer_setpoint((ctx), (pt))
  3434. #define DUK_LEXER_GETPOINT(ctx,pt) duk_lexer_getpoint((ctx), (pt))
  3435. /* Currently 6 characters of lookup are actually needed (duk_lexer.c). */
  3436. #define DUK_LEXER_WINDOW_SIZE 6
  3437. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  3438. #define DUK_LEXER_BUFFER_SIZE 64
  3439. #endif
  3440. #define DUK_TOK_MINVAL 0
  3441. /* returned after EOF (infinite amount) */
  3442. #define DUK_TOK_EOF 0
  3443. /* identifier names (E5 Section 7.6) */
  3444. #define DUK_TOK_IDENTIFIER 1
  3445. /* reserved words: keywords */
  3446. #define DUK_TOK_START_RESERVED 2
  3447. #define DUK_TOK_BREAK 2
  3448. #define DUK_TOK_CASE 3
  3449. #define DUK_TOK_CATCH 4
  3450. #define DUK_TOK_CONTINUE 5
  3451. #define DUK_TOK_DEBUGGER 6
  3452. #define DUK_TOK_DEFAULT 7
  3453. #define DUK_TOK_DELETE 8
  3454. #define DUK_TOK_DO 9
  3455. #define DUK_TOK_ELSE 10
  3456. #define DUK_TOK_FINALLY 11
  3457. #define DUK_TOK_FOR 12
  3458. #define DUK_TOK_FUNCTION 13
  3459. #define DUK_TOK_IF 14
  3460. #define DUK_TOK_IN 15
  3461. #define DUK_TOK_INSTANCEOF 16
  3462. #define DUK_TOK_NEW 17
  3463. #define DUK_TOK_RETURN 18
  3464. #define DUK_TOK_SWITCH 19
  3465. #define DUK_TOK_THIS 20
  3466. #define DUK_TOK_THROW 21
  3467. #define DUK_TOK_TRY 22
  3468. #define DUK_TOK_TYPEOF 23
  3469. #define DUK_TOK_VAR 24
  3470. #define DUK_TOK_CONST 25
  3471. #define DUK_TOK_VOID 26
  3472. #define DUK_TOK_WHILE 27
  3473. #define DUK_TOK_WITH 28
  3474. /* reserved words: future reserved words */
  3475. #define DUK_TOK_CLASS 29
  3476. #define DUK_TOK_ENUM 30
  3477. #define DUK_TOK_EXPORT 31
  3478. #define DUK_TOK_EXTENDS 32
  3479. #define DUK_TOK_IMPORT 33
  3480. #define DUK_TOK_SUPER 34
  3481. /* "null", "true", and "false" are always reserved words.
  3482. * Note that "get" and "set" are not!
  3483. */
  3484. #define DUK_TOK_NULL 35
  3485. #define DUK_TOK_TRUE 36
  3486. #define DUK_TOK_FALSE 37
  3487. /* reserved words: additional future reserved words in strict mode */
  3488. #define DUK_TOK_START_STRICT_RESERVED 38 /* inclusive */
  3489. #define DUK_TOK_IMPLEMENTS 38
  3490. #define DUK_TOK_INTERFACE 39
  3491. #define DUK_TOK_LET 40
  3492. #define DUK_TOK_PACKAGE 41
  3493. #define DUK_TOK_PRIVATE 42
  3494. #define DUK_TOK_PROTECTED 43
  3495. #define DUK_TOK_PUBLIC 44
  3496. #define DUK_TOK_STATIC 45
  3497. #define DUK_TOK_YIELD 46
  3498. #define DUK_TOK_END_RESERVED 47 /* exclusive */
  3499. /* "get" and "set" are tokens but NOT ReservedWords. They are currently
  3500. * parsed and identifiers and these defines are actually now unused.
  3501. */
  3502. #define DUK_TOK_GET 47
  3503. #define DUK_TOK_SET 48
  3504. /* punctuators (unlike the spec, also includes "/" and "/=") */
  3505. #define DUK_TOK_LCURLY 49
  3506. #define DUK_TOK_RCURLY 50
  3507. #define DUK_TOK_LBRACKET 51
  3508. #define DUK_TOK_RBRACKET 52
  3509. #define DUK_TOK_LPAREN 53
  3510. #define DUK_TOK_RPAREN 54
  3511. #define DUK_TOK_PERIOD 55
  3512. #define DUK_TOK_SEMICOLON 56
  3513. #define DUK_TOK_COMMA 57
  3514. #define DUK_TOK_LT 58
  3515. #define DUK_TOK_GT 59
  3516. #define DUK_TOK_LE 60
  3517. #define DUK_TOK_GE 61
  3518. #define DUK_TOK_EQ 62
  3519. #define DUK_TOK_NEQ 63
  3520. #define DUK_TOK_SEQ 64
  3521. #define DUK_TOK_SNEQ 65
  3522. #define DUK_TOK_ADD 66
  3523. #define DUK_TOK_SUB 67
  3524. #define DUK_TOK_MUL 68
  3525. #define DUK_TOK_DIV 69
  3526. #define DUK_TOK_MOD 70
  3527. #define DUK_TOK_EXP 71
  3528. #define DUK_TOK_INCREMENT 72
  3529. #define DUK_TOK_DECREMENT 73
  3530. #define DUK_TOK_ALSHIFT 74 /* named "arithmetic" because result is signed */
  3531. #define DUK_TOK_ARSHIFT 75
  3532. #define DUK_TOK_RSHIFT 76
  3533. #define DUK_TOK_BAND 77
  3534. #define DUK_TOK_BOR 78
  3535. #define DUK_TOK_BXOR 79
  3536. #define DUK_TOK_LNOT 80
  3537. #define DUK_TOK_BNOT 81
  3538. #define DUK_TOK_LAND 82
  3539. #define DUK_TOK_LOR 83
  3540. #define DUK_TOK_QUESTION 84
  3541. #define DUK_TOK_COLON 85
  3542. #define DUK_TOK_EQUALSIGN 86
  3543. #define DUK_TOK_ADD_EQ 87
  3544. #define DUK_TOK_SUB_EQ 88
  3545. #define DUK_TOK_MUL_EQ 89
  3546. #define DUK_TOK_DIV_EQ 90
  3547. #define DUK_TOK_MOD_EQ 91
  3548. #define DUK_TOK_EXP_EQ 92
  3549. #define DUK_TOK_ALSHIFT_EQ 93
  3550. #define DUK_TOK_ARSHIFT_EQ 94
  3551. #define DUK_TOK_RSHIFT_EQ 95
  3552. #define DUK_TOK_BAND_EQ 96
  3553. #define DUK_TOK_BOR_EQ 97
  3554. #define DUK_TOK_BXOR_EQ 98
  3555. /* literals (E5 Section 7.8), except null, true, false, which are treated
  3556. * like reserved words (above).
  3557. */
  3558. #define DUK_TOK_NUMBER 99
  3559. #define DUK_TOK_STRING 100
  3560. #define DUK_TOK_REGEXP 101
  3561. #define DUK_TOK_MAXVAL 101 /* inclusive */
  3562. #define DUK_TOK_INVALID DUK_SMALL_UINT_MAX
  3563. /* Convert heap string index to a token (reserved words) */
  3564. #define DUK_STRIDX_TO_TOK(x) ((x) - DUK_STRIDX_START_RESERVED + DUK_TOK_START_RESERVED)
  3565. /* Sanity check */
  3566. #if (DUK_TOK_MAXVAL > 255)
  3567. #error DUK_TOK_MAXVAL too large, code assumes it fits into 8 bits
  3568. #endif
  3569. /* Sanity checks for string and token defines */
  3570. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_BREAK) != DUK_TOK_BREAK)
  3571. #error mismatch in token defines
  3572. #endif
  3573. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CASE) != DUK_TOK_CASE)
  3574. #error mismatch in token defines
  3575. #endif
  3576. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CATCH) != DUK_TOK_CATCH)
  3577. #error mismatch in token defines
  3578. #endif
  3579. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONTINUE) != DUK_TOK_CONTINUE)
  3580. #error mismatch in token defines
  3581. #endif
  3582. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEBUGGER) != DUK_TOK_DEBUGGER)
  3583. #error mismatch in token defines
  3584. #endif
  3585. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DEFAULT) != DUK_TOK_DEFAULT)
  3586. #error mismatch in token defines
  3587. #endif
  3588. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DELETE) != DUK_TOK_DELETE)
  3589. #error mismatch in token defines
  3590. #endif
  3591. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_DO) != DUK_TOK_DO)
  3592. #error mismatch in token defines
  3593. #endif
  3594. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ELSE) != DUK_TOK_ELSE)
  3595. #error mismatch in token defines
  3596. #endif
  3597. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FINALLY) != DUK_TOK_FINALLY)
  3598. #error mismatch in token defines
  3599. #endif
  3600. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FOR) != DUK_TOK_FOR)
  3601. #error mismatch in token defines
  3602. #endif
  3603. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_FUNCTION) != DUK_TOK_FUNCTION)
  3604. #error mismatch in token defines
  3605. #endif
  3606. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IF) != DUK_TOK_IF)
  3607. #error mismatch in token defines
  3608. #endif
  3609. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IN) != DUK_TOK_IN)
  3610. #error mismatch in token defines
  3611. #endif
  3612. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INSTANCEOF) != DUK_TOK_INSTANCEOF)
  3613. #error mismatch in token defines
  3614. #endif
  3615. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_NEW) != DUK_TOK_NEW)
  3616. #error mismatch in token defines
  3617. #endif
  3618. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_RETURN) != DUK_TOK_RETURN)
  3619. #error mismatch in token defines
  3620. #endif
  3621. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SWITCH) != DUK_TOK_SWITCH)
  3622. #error mismatch in token defines
  3623. #endif
  3624. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THIS) != DUK_TOK_THIS)
  3625. #error mismatch in token defines
  3626. #endif
  3627. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_THROW) != DUK_TOK_THROW)
  3628. #error mismatch in token defines
  3629. #endif
  3630. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRY) != DUK_TOK_TRY)
  3631. #error mismatch in token defines
  3632. #endif
  3633. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TYPEOF) != DUK_TOK_TYPEOF)
  3634. #error mismatch in token defines
  3635. #endif
  3636. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VAR) != DUK_TOK_VAR)
  3637. #error mismatch in token defines
  3638. #endif
  3639. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_VOID) != DUK_TOK_VOID)
  3640. #error mismatch in token defines
  3641. #endif
  3642. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WHILE) != DUK_TOK_WHILE)
  3643. #error mismatch in token defines
  3644. #endif
  3645. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_WITH) != DUK_TOK_WITH)
  3646. #error mismatch in token defines
  3647. #endif
  3648. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CLASS) != DUK_TOK_CLASS)
  3649. #error mismatch in token defines
  3650. #endif
  3651. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_CONST) != DUK_TOK_CONST)
  3652. #error mismatch in token defines
  3653. #endif
  3654. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_ENUM) != DUK_TOK_ENUM)
  3655. #error mismatch in token defines
  3656. #endif
  3657. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXPORT) != DUK_TOK_EXPORT)
  3658. #error mismatch in token defines
  3659. #endif
  3660. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_EXTENDS) != DUK_TOK_EXTENDS)
  3661. #error mismatch in token defines
  3662. #endif
  3663. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPORT) != DUK_TOK_IMPORT)
  3664. #error mismatch in token defines
  3665. #endif
  3666. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_SUPER) != DUK_TOK_SUPER)
  3667. #error mismatch in token defines
  3668. #endif
  3669. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LC_NULL) != DUK_TOK_NULL)
  3670. #error mismatch in token defines
  3671. #endif
  3672. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_TRUE) != DUK_TOK_TRUE)
  3673. #error mismatch in token defines
  3674. #endif
  3675. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_FALSE) != DUK_TOK_FALSE)
  3676. #error mismatch in token defines
  3677. #endif
  3678. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_IMPLEMENTS) != DUK_TOK_IMPLEMENTS)
  3679. #error mismatch in token defines
  3680. #endif
  3681. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_INTERFACE) != DUK_TOK_INTERFACE)
  3682. #error mismatch in token defines
  3683. #endif
  3684. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_LET) != DUK_TOK_LET)
  3685. #error mismatch in token defines
  3686. #endif
  3687. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PACKAGE) != DUK_TOK_PACKAGE)
  3688. #error mismatch in token defines
  3689. #endif
  3690. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PRIVATE) != DUK_TOK_PRIVATE)
  3691. #error mismatch in token defines
  3692. #endif
  3693. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PROTECTED) != DUK_TOK_PROTECTED)
  3694. #error mismatch in token defines
  3695. #endif
  3696. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_PUBLIC) != DUK_TOK_PUBLIC)
  3697. #error mismatch in token defines
  3698. #endif
  3699. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_STATIC) != DUK_TOK_STATIC)
  3700. #error mismatch in token defines
  3701. #endif
  3702. #if (DUK_STRIDX_TO_TOK(DUK_STRIDX_YIELD) != DUK_TOK_YIELD)
  3703. #error mismatch in token defines
  3704. #endif
  3705. /* Regexp tokens */
  3706. #define DUK_RETOK_EOF 0
  3707. #define DUK_RETOK_DISJUNCTION 1
  3708. #define DUK_RETOK_QUANTIFIER 2
  3709. #define DUK_RETOK_ASSERT_START 3
  3710. #define DUK_RETOK_ASSERT_END 4
  3711. #define DUK_RETOK_ASSERT_WORD_BOUNDARY 5
  3712. #define DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY 6
  3713. #define DUK_RETOK_ASSERT_START_POS_LOOKAHEAD 7
  3714. #define DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD 8
  3715. #define DUK_RETOK_ATOM_PERIOD 9
  3716. #define DUK_RETOK_ATOM_CHAR 10
  3717. #define DUK_RETOK_ATOM_DIGIT 11 /* assumptions in regexp compiler */
  3718. #define DUK_RETOK_ATOM_NOT_DIGIT 12 /* -""- */
  3719. #define DUK_RETOK_ATOM_WHITE 13 /* -""- */
  3720. #define DUK_RETOK_ATOM_NOT_WHITE 14 /* -""- */
  3721. #define DUK_RETOK_ATOM_WORD_CHAR 15 /* -""- */
  3722. #define DUK_RETOK_ATOM_NOT_WORD_CHAR 16 /* -""- */
  3723. #define DUK_RETOK_ATOM_BACKREFERENCE 17
  3724. #define DUK_RETOK_ATOM_START_CAPTURE_GROUP 18
  3725. #define DUK_RETOK_ATOM_START_NONCAPTURE_GROUP 19
  3726. #define DUK_RETOK_ATOM_START_CHARCLASS 20
  3727. #define DUK_RETOK_ATOM_START_CHARCLASS_INVERTED 21
  3728. #define DUK_RETOK_ATOM_END_GROUP 22
  3729. /* Constants for duk_lexer_ctx.buf. */
  3730. #define DUK_LEXER_TEMP_BUF_LIMIT 256
  3731. /* A token value. Can be memcpy()'d, but note that slot1/slot2 values are on the valstack.
  3732. * Some fields (like num, str1, str2) are only valid for specific token types and may have
  3733. * stale values otherwise.
  3734. */
  3735. struct duk_token {
  3736. duk_small_uint_t t; /* token type (with reserved word identification) */
  3737. duk_small_uint_t t_nores; /* token type (with reserved words as DUK_TOK_IDENTIFER) */
  3738. duk_double_t num; /* numeric value of token */
  3739. duk_hstring *str1; /* string 1 of token (borrowed, stored to ctx->slot1_idx) */
  3740. duk_hstring *str2; /* string 2 of token (borrowed, stored to ctx->slot2_idx) */
  3741. duk_size_t start_offset; /* start byte offset of token in lexer input */
  3742. duk_int_t start_line; /* start line of token (first char) */
  3743. duk_int_t num_escapes; /* number of escapes and line continuations (for directive prologue) */
  3744. duk_bool_t lineterm; /* token was preceded by a lineterm */
  3745. duk_bool_t allow_auto_semi; /* token allows automatic semicolon insertion (eof or preceded by newline) */
  3746. };
  3747. #define DUK_RE_QUANTIFIER_INFINITE ((duk_uint32_t) 0xffffffffUL)
  3748. /* A regexp token value. */
  3749. struct duk_re_token {
  3750. duk_small_uint_t t; /* token type */
  3751. duk_small_uint_t greedy;
  3752. duk_uint32_t num; /* numeric value (character, count) */
  3753. duk_uint32_t qmin;
  3754. duk_uint32_t qmax;
  3755. };
  3756. /* A structure for 'snapshotting' a point for rewinding */
  3757. struct duk_lexer_point {
  3758. duk_size_t offset;
  3759. duk_int_t line;
  3760. };
  3761. /* Lexer codepoint with additional info like offset/line number */
  3762. struct duk_lexer_codepoint {
  3763. duk_codepoint_t codepoint;
  3764. duk_size_t offset;
  3765. duk_int_t line;
  3766. };
  3767. /* Lexer context. Same context is used for ECMAScript and Regexp parsing. */
  3768. struct duk_lexer_ctx {
  3769. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  3770. duk_lexer_codepoint *window; /* unicode code points, window[0] is always next, points to 'buffer' */
  3771. duk_lexer_codepoint buffer[DUK_LEXER_BUFFER_SIZE];
  3772. #else
  3773. duk_lexer_codepoint window[DUK_LEXER_WINDOW_SIZE]; /* unicode code points, window[0] is always next */
  3774. #endif
  3775. duk_hthread *thr; /* thread; minimizes argument passing */
  3776. const duk_uint8_t *input; /* input string (may be a user pointer) */
  3777. duk_size_t input_length; /* input byte length */
  3778. duk_size_t input_offset; /* input offset for window leading edge (not window[0]) */
  3779. duk_int_t input_line; /* input linenumber at input_offset (not window[0]), init to 1 */
  3780. duk_idx_t slot1_idx; /* valstack slot for 1st token value */
  3781. duk_idx_t slot2_idx; /* valstack slot for 2nd token value */
  3782. duk_idx_t buf_idx; /* valstack slot for temp buffer */
  3783. duk_hbuffer_dynamic *buf; /* temp accumulation buffer */
  3784. duk_bufwriter_ctx bw; /* bufwriter for temp accumulation */
  3785. duk_int_t token_count; /* number of tokens parsed */
  3786. duk_int_t token_limit; /* maximum token count before error (sanity backstop) */
  3787. duk_small_uint_t flags; /* lexer flags, use compiler flag defines for now */
  3788. };
  3789. /*
  3790. * Prototypes
  3791. */
  3792. DUK_INTERNAL_DECL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx);
  3793. DUK_INTERNAL_DECL void duk_lexer_getpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt);
  3794. DUK_INTERNAL_DECL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt);
  3795. DUK_INTERNAL_DECL
  3796. void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx,
  3797. duk_token *out_token,
  3798. duk_bool_t strict_mode,
  3799. duk_bool_t regexp_mode);
  3800. #if defined(DUK_USE_REGEXP_SUPPORT)
  3801. DUK_INTERNAL_DECL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token);
  3802. DUK_INTERNAL_DECL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata);
  3803. #endif /* DUK_USE_REGEXP_SUPPORT */
  3804. #endif /* DUK_LEXER_H_INCLUDED */
  3805. /* #include duk_js_compiler.h */
  3806. #line 1 "duk_js_compiler.h"
  3807. /*
  3808. * ECMAScript compiler.
  3809. */
  3810. #if !defined(DUK_JS_COMPILER_H_INCLUDED)
  3811. #define DUK_JS_COMPILER_H_INCLUDED
  3812. /* ECMAScript compiler limits */
  3813. #define DUK_COMPILER_TOKEN_LIMIT 100000000L /* 1e8: protects against deeply nested inner functions */
  3814. /* maximum loopcount for peephole optimization */
  3815. #define DUK_COMPILER_PEEPHOLE_MAXITER 3
  3816. /* maximum bytecode length in instructions */
  3817. #define DUK_COMPILER_MAX_BYTECODE_LENGTH (256L * 1024L * 1024L) /* 1 GB */
  3818. /*
  3819. * Compiler intermediate values
  3820. *
  3821. * Intermediate values describe either plain values (e.g. strings or
  3822. * numbers) or binary operations which have not yet been coerced into
  3823. * either a left-hand-side or right-hand-side role (e.g. object property).
  3824. */
  3825. #define DUK_IVAL_NONE 0 /* no value */
  3826. #define DUK_IVAL_PLAIN 1 /* register, constant, or value */
  3827. #define DUK_IVAL_ARITH 2 /* binary arithmetic; DUK_OP_ADD, DUK_OP_EQ, other binary ops */
  3828. #define DUK_IVAL_PROP 3 /* property access */
  3829. #define DUK_IVAL_VAR 4 /* variable access */
  3830. #define DUK_ISPEC_NONE 0 /* no value */
  3831. #define DUK_ISPEC_VALUE 1 /* value resides in 'valstack_idx' */
  3832. #define DUK_ISPEC_REGCONST 2 /* value resides in a register or constant */
  3833. /* Bit mask which indicates that a regconst is a constant instead of a register.
  3834. * Chosen so that when a regconst is cast to duk_int32_t, all consts are
  3835. * negative values.
  3836. */
  3837. #define DUK_REGCONST_CONST_MARKER DUK_INT32_MIN /* = -0x80000000 */
  3838. /* Type to represent a reg/const reference during compilation, with <0
  3839. * indicating a constant. Some call sites also use -1 to indicate 'none'.
  3840. */
  3841. typedef duk_int32_t duk_regconst_t;
  3842. typedef struct {
  3843. duk_small_uint_t t; /* DUK_ISPEC_XXX */
  3844. duk_regconst_t regconst;
  3845. duk_idx_t valstack_idx; /* always set; points to a reserved valstack slot */
  3846. } duk_ispec;
  3847. typedef struct {
  3848. /*
  3849. * PLAIN: x1
  3850. * ARITH: x1 <op> x2
  3851. * PROP: x1.x2
  3852. * VAR: x1 (name)
  3853. */
  3854. /* XXX: can be optimized for smaller footprint esp. on 32-bit environments */
  3855. duk_small_uint_t t; /* DUK_IVAL_XXX */
  3856. duk_small_uint_t op; /* bytecode opcode for binary ops */
  3857. duk_ispec x1;
  3858. duk_ispec x2;
  3859. } duk_ivalue;
  3860. /*
  3861. * Bytecode instruction representation during compilation
  3862. *
  3863. * Contains the actual instruction and (optionally) debug info.
  3864. */
  3865. struct duk_compiler_instr {
  3866. duk_instr_t ins;
  3867. #if defined(DUK_USE_PC2LINE)
  3868. duk_uint32_t line;
  3869. #endif
  3870. };
  3871. /*
  3872. * Compiler state
  3873. */
  3874. #define DUK_LABEL_FLAG_ALLOW_BREAK (1U << 0)
  3875. #define DUK_LABEL_FLAG_ALLOW_CONTINUE (1U << 1)
  3876. #define DUK_DECL_TYPE_VAR 0
  3877. #define DUK_DECL_TYPE_FUNC 1
  3878. /* XXX: optimize to 16 bytes */
  3879. typedef struct {
  3880. duk_small_uint_t flags;
  3881. duk_int_t label_id; /* numeric label_id (-1 reserved as marker) */
  3882. duk_hstring *h_label; /* borrowed label name */
  3883. duk_int_t catch_depth; /* catch depth at point of definition */
  3884. duk_int_t pc_label; /* pc of label statement:
  3885. * pc+1: break jump site
  3886. * pc+2: continue jump site
  3887. */
  3888. /* Fast jumps (which avoid longjmp) jump directly to the jump sites
  3889. * which are always known even while the iteration/switch statement
  3890. * is still being parsed. A final peephole pass "straightens out"
  3891. * the jumps.
  3892. */
  3893. } duk_labelinfo;
  3894. /* Compiling state of one function, eventually converted to duk_hcompfunc */
  3895. struct duk_compiler_func {
  3896. /* These pointers are at the start of the struct so that they pack
  3897. * nicely. Mixing pointers and integer values is bad on some
  3898. * platforms (e.g. if int is 32 bits and pointers are 64 bits).
  3899. */
  3900. duk_bufwriter_ctx bw_code; /* bufwriter for code */
  3901. duk_hstring *h_name; /* function name (borrowed reference), ends up in _name */
  3902. /* h_code: held in bw_code */
  3903. duk_hobject *h_consts; /* array */
  3904. duk_hobject *h_funcs; /* array of function templates: [func1, offset1, line1, func2, offset2, line2]
  3905. * offset/line points to closing brace to allow skipping on pass 2
  3906. */
  3907. duk_hobject *h_decls; /* array of declarations: [ name1, val1, name2, val2, ... ]
  3908. * valN = (typeN) | (fnum << 8), where fnum is inner func number (0 for vars)
  3909. * record function and variable declarations in pass 1
  3910. */
  3911. duk_hobject *h_labelnames; /* array of active label names */
  3912. duk_hbuffer_dynamic *h_labelinfos; /* C array of duk_labelinfo */
  3913. duk_hobject *h_argnames; /* array of formal argument names (-> _Formals) */
  3914. duk_hobject *h_varmap; /* variable map for pass 2 (identifier -> register number or null (unmapped)) */
  3915. /* Value stack indices for tracking objects. */
  3916. /* code_idx: not needed */
  3917. duk_idx_t consts_idx;
  3918. duk_idx_t funcs_idx;
  3919. duk_idx_t decls_idx;
  3920. duk_idx_t labelnames_idx;
  3921. duk_idx_t labelinfos_idx;
  3922. duk_idx_t argnames_idx;
  3923. duk_idx_t varmap_idx;
  3924. /* Temp reg handling. */
  3925. duk_regconst_t temp_first; /* first register that is a temporary (below: variables) */
  3926. duk_regconst_t temp_next; /* next temporary register to allocate */
  3927. duk_regconst_t temp_max; /* highest value of temp_reg (temp_max - 1 is highest used reg) */
  3928. /* Shuffle registers if large number of regs/consts. */
  3929. duk_regconst_t shuffle1;
  3930. duk_regconst_t shuffle2;
  3931. duk_regconst_t shuffle3;
  3932. /* Stats for current expression being parsed. */
  3933. duk_int_t nud_count;
  3934. duk_int_t led_count;
  3935. duk_int_t paren_level; /* parenthesis count, 0 = top level */
  3936. duk_bool_t expr_lhs; /* expression is left-hand-side compatible */
  3937. duk_bool_t allow_in; /* current paren level allows 'in' token */
  3938. /* Misc. */
  3939. duk_int_t stmt_next; /* statement id allocation (running counter) */
  3940. duk_int_t label_next; /* label id allocation (running counter) */
  3941. duk_int_t catch_depth; /* catch stack depth */
  3942. duk_int_t with_depth; /* with stack depth (affects identifier lookups) */
  3943. duk_int_t fnum_next; /* inner function numbering */
  3944. duk_int_t num_formals; /* number of formal arguments */
  3945. duk_regconst_t reg_stmt_value; /* register for writing value of 'non-empty' statements (global or eval code), -1 is marker */
  3946. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  3947. duk_int_t min_line; /* XXX: typing (duk_hcompfunc has duk_uint32_t) */
  3948. duk_int_t max_line;
  3949. #endif
  3950. /* Status booleans. */
  3951. duk_uint8_t is_function; /* is an actual function (not global/eval code) */
  3952. duk_uint8_t is_eval; /* is eval code */
  3953. duk_uint8_t is_global; /* is global code */
  3954. duk_uint8_t is_namebinding; /* needs a name binding */
  3955. duk_uint8_t is_constructable; /* result is constructable */
  3956. duk_uint8_t is_setget; /* is a setter/getter */
  3957. duk_uint8_t is_strict; /* function is strict */
  3958. duk_uint8_t is_notail; /* function must not be tail called */
  3959. duk_uint8_t in_directive_prologue; /* parsing in "directive prologue", recognize directives */
  3960. duk_uint8_t in_scanning; /* parsing in "scanning" phase (first pass) */
  3961. duk_uint8_t may_direct_eval; /* function may call direct eval */
  3962. duk_uint8_t id_access_arguments; /* function refers to 'arguments' identifier */
  3963. duk_uint8_t id_access_slow; /* function makes one or more slow path accesses that won't match own static variables */
  3964. duk_uint8_t id_access_slow_own; /* function makes one or more slow path accesses that may match own static variables */
  3965. duk_uint8_t is_arguments_shadowed; /* argument/function declaration shadows 'arguments' */
  3966. duk_uint8_t needs_shuffle; /* function needs shuffle registers */
  3967. duk_uint8_t reject_regexp_in_adv; /* reject RegExp literal on next advance() call; needed for handling IdentifierName productions */
  3968. duk_uint8_t allow_regexp_in_adv; /* allow RegExp literal on next advance() call */
  3969. };
  3970. struct duk_compiler_ctx {
  3971. duk_hthread *thr;
  3972. /* filename being compiled (ends up in functions' '_filename' property) */
  3973. duk_hstring *h_filename; /* borrowed reference */
  3974. /* lexing (tokenization) state (contains two valstack slot indices) */
  3975. duk_lexer_ctx lex;
  3976. /* current and previous token for parsing */
  3977. duk_token prev_token;
  3978. duk_token curr_token;
  3979. duk_idx_t tok11_idx; /* curr_token slot1 (matches 'lex' slot1_idx) */
  3980. duk_idx_t tok12_idx; /* curr_token slot2 (matches 'lex' slot2_idx) */
  3981. duk_idx_t tok21_idx; /* prev_token slot1 */
  3982. duk_idx_t tok22_idx; /* prev_token slot2 */
  3983. /* recursion limit */
  3984. duk_int_t recursion_depth;
  3985. duk_int_t recursion_limit;
  3986. /* code emission temporary */
  3987. duk_int_t emit_jumpslot_pc;
  3988. /* current function being compiled (embedded instead of pointer for more compact access) */
  3989. duk_compiler_func curr_func;
  3990. };
  3991. /*
  3992. * Prototypes
  3993. */
  3994. DUK_INTERNAL_DECL void duk_js_compile(duk_hthread *thr, const duk_uint8_t *src_buffer, duk_size_t src_length, duk_small_uint_t flags);
  3995. #endif /* DUK_JS_COMPILER_H_INCLUDED */
  3996. /* #include duk_regexp.h */
  3997. #line 1 "duk_regexp.h"
  3998. /*
  3999. * Regular expression structs, constants, and bytecode defines.
  4000. */
  4001. #if !defined(DUK_REGEXP_H_INCLUDED)
  4002. #define DUK_REGEXP_H_INCLUDED
  4003. /* maximum bytecode copies for {n,m} quantifiers */
  4004. #define DUK_RE_MAX_ATOM_COPIES 1000
  4005. /* regexp compilation limits */
  4006. #define DUK_RE_COMPILE_TOKEN_LIMIT 100000000L /* 1e8 */
  4007. /* regexp execution limits */
  4008. #define DUK_RE_EXECUTE_STEPS_LIMIT 1000000000L /* 1e9 */
  4009. /* regexp opcodes */
  4010. #define DUK_REOP_MATCH 1
  4011. #define DUK_REOP_CHAR 2
  4012. #define DUK_REOP_PERIOD 3
  4013. #define DUK_REOP_RANGES 4
  4014. #define DUK_REOP_INVRANGES 5
  4015. #define DUK_REOP_JUMP 6
  4016. #define DUK_REOP_SPLIT1 7
  4017. #define DUK_REOP_SPLIT2 8
  4018. #define DUK_REOP_SQMINIMAL 9
  4019. #define DUK_REOP_SQGREEDY 10
  4020. #define DUK_REOP_SAVE 11
  4021. #define DUK_REOP_WIPERANGE 12
  4022. #define DUK_REOP_LOOKPOS 13
  4023. #define DUK_REOP_LOOKNEG 14
  4024. #define DUK_REOP_BACKREFERENCE 15
  4025. #define DUK_REOP_ASSERT_START 16
  4026. #define DUK_REOP_ASSERT_END 17
  4027. #define DUK_REOP_ASSERT_WORD_BOUNDARY 18
  4028. #define DUK_REOP_ASSERT_NOT_WORD_BOUNDARY 19
  4029. /* flags */
  4030. #define DUK_RE_FLAG_GLOBAL (1U << 0)
  4031. #define DUK_RE_FLAG_IGNORE_CASE (1U << 1)
  4032. #define DUK_RE_FLAG_MULTILINE (1U << 2)
  4033. struct duk_re_matcher_ctx {
  4034. duk_hthread *thr;
  4035. duk_uint32_t re_flags;
  4036. const duk_uint8_t *input;
  4037. const duk_uint8_t *input_end;
  4038. const duk_uint8_t *bytecode;
  4039. const duk_uint8_t *bytecode_end;
  4040. const duk_uint8_t **saved; /* allocated from valstack (fixed buffer) */
  4041. duk_uint32_t nsaved;
  4042. duk_uint32_t recursion_depth;
  4043. duk_uint32_t recursion_limit;
  4044. duk_uint32_t steps_count;
  4045. duk_uint32_t steps_limit;
  4046. };
  4047. struct duk_re_compiler_ctx {
  4048. duk_hthread *thr;
  4049. duk_uint32_t re_flags;
  4050. duk_lexer_ctx lex;
  4051. duk_re_token curr_token;
  4052. duk_bufwriter_ctx bw;
  4053. duk_uint32_t captures; /* highest capture number emitted so far (used as: ++captures) */
  4054. duk_uint32_t highest_backref;
  4055. duk_uint32_t recursion_depth;
  4056. duk_uint32_t recursion_limit;
  4057. duk_uint32_t nranges; /* internal temporary value, used for char classes */
  4058. };
  4059. /*
  4060. * Prototypes
  4061. */
  4062. #if defined(DUK_USE_REGEXP_SUPPORT)
  4063. DUK_INTERNAL_DECL void duk_regexp_compile(duk_hthread *thr);
  4064. DUK_INTERNAL_DECL void duk_regexp_create_instance(duk_hthread *thr);
  4065. DUK_INTERNAL_DECL void duk_regexp_match(duk_hthread *thr);
  4066. DUK_INTERNAL_DECL void duk_regexp_match_force_global(duk_hthread *thr); /* hacky helper for String.prototype.split() */
  4067. #endif
  4068. #endif /* DUK_REGEXP_H_INCLUDED */
  4069. /* #include duk_heaphdr.h */
  4070. #line 1 "duk_heaphdr.h"
  4071. /*
  4072. * Heap header definition and assorted macros, including ref counting.
  4073. * Access all fields through the accessor macros.
  4074. */
  4075. #if !defined(DUK_HEAPHDR_H_INCLUDED)
  4076. #define DUK_HEAPHDR_H_INCLUDED
  4077. /*
  4078. * Common heap header
  4079. *
  4080. * All heap objects share the same flags and refcount fields. Objects other
  4081. * than strings also need to have a single or double linked list pointers
  4082. * for insertion into the "heap allocated" list. Strings have single linked
  4083. * list pointers for string table chaining.
  4084. *
  4085. * Technically, 'h_refcount' must be wide enough to guarantee that it cannot
  4086. * wrap; otherwise objects might be freed incorrectly after wrapping. The
  4087. * default refcount field is 32 bits even on 64-bit systems: while that's in
  4088. * theory incorrect, the Duktape heap needs to be larger than 64GB for the
  4089. * count to actually wrap (assuming 16-byte duk_tvals). This is very unlikely
  4090. * to ever be an issue, but if it is, disabling DUK_USE_REFCOUNT32 causes
  4091. * Duktape to use size_t for refcounts which should always be safe.
  4092. *
  4093. * Heap header size on 32-bit platforms: 8 bytes without reference counting,
  4094. * 16 bytes with reference counting.
  4095. *
  4096. * Note that 'raw' macros such as DUK_HEAPHDR_GET_REFCOUNT() are not
  4097. * defined without DUK_USE_REFERENCE_COUNTING, so caller must #if defined()
  4098. * around them.
  4099. */
  4100. /* XXX: macro for shared header fields (avoids some padding issues) */
  4101. struct duk_heaphdr {
  4102. duk_uint32_t h_flags;
  4103. #if defined(DUK_USE_REFERENCE_COUNTING)
  4104. #if defined(DUK_USE_ASSERTIONS)
  4105. /* When assertions enabled, used by mark-and-sweep for refcount
  4106. * validation. Largest reasonable type; also detects overflows.
  4107. */
  4108. duk_size_t h_assert_refcount;
  4109. #endif
  4110. #if defined(DUK_USE_REFCOUNT16)
  4111. duk_uint16_t h_refcount;
  4112. #elif defined(DUK_USE_REFCOUNT32)
  4113. duk_uint32_t h_refcount;
  4114. #else
  4115. duk_size_t h_refcount;
  4116. #endif
  4117. #endif /* DUK_USE_REFERENCE_COUNTING */
  4118. #if defined(DUK_USE_HEAPPTR16)
  4119. duk_uint16_t h_next16;
  4120. #else
  4121. duk_heaphdr *h_next;
  4122. #endif
  4123. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  4124. /* refcounting requires direct heap frees, which in turn requires a dual linked heap */
  4125. #if defined(DUK_USE_HEAPPTR16)
  4126. duk_uint16_t h_prev16;
  4127. #else
  4128. duk_heaphdr *h_prev;
  4129. #endif
  4130. #endif
  4131. /* When DUK_USE_HEAPPTR16 (and DUK_USE_REFCOUNT16) is in use, the
  4132. * struct won't align nicely to 4 bytes. This 16-bit extra field
  4133. * is added to make the alignment clean; the field can be used by
  4134. * heap objects when 16-bit packing is used. This field is now
  4135. * conditional to DUK_USE_HEAPPTR16 only, but it is intended to be
  4136. * used with DUK_USE_REFCOUNT16 and DUK_USE_DOUBLE_LINKED_HEAP;
  4137. * this only matter to low memory environments anyway.
  4138. */
  4139. #if defined(DUK_USE_HEAPPTR16)
  4140. duk_uint16_t h_extra16;
  4141. #endif
  4142. };
  4143. struct duk_heaphdr_string {
  4144. /* 16 bits would be enough for shared heaphdr flags and duk_hstring
  4145. * flags. The initial parts of duk_heaphdr_string and duk_heaphdr
  4146. * must match so changing the flags field size here would be quite
  4147. * awkward. However, to minimize struct size, we can pack at least
  4148. * 16 bits of duk_hstring data into the flags field.
  4149. */
  4150. duk_uint32_t h_flags;
  4151. #if defined(DUK_USE_REFERENCE_COUNTING)
  4152. #if defined(DUK_USE_ASSERTIONS)
  4153. /* When assertions enabled, used by mark-and-sweep for refcount
  4154. * validation. Largest reasonable type; also detects overflows.
  4155. */
  4156. duk_size_t h_assert_refcount;
  4157. #endif
  4158. #if defined(DUK_USE_REFCOUNT16)
  4159. duk_uint16_t h_refcount;
  4160. duk_uint16_t h_strextra16; /* round out to 8 bytes */
  4161. #elif defined(DUK_USE_REFCOUNT32)
  4162. duk_uint32_t h_refcount;
  4163. #else
  4164. duk_size_t h_refcount;
  4165. #endif
  4166. #else
  4167. duk_uint16_t h_strextra16;
  4168. #endif /* DUK_USE_REFERENCE_COUNTING */
  4169. duk_hstring *h_next;
  4170. /* No 'h_prev' pointer for strings. */
  4171. };
  4172. #define DUK_HEAPHDR_FLAGS_TYPE_MASK 0x00000003UL
  4173. #define DUK_HEAPHDR_FLAGS_FLAG_MASK (~DUK_HEAPHDR_FLAGS_TYPE_MASK)
  4174. /* 2 bits for heap type */
  4175. #define DUK_HEAPHDR_FLAGS_HEAP_START 2 /* 5 heap flags */
  4176. #define DUK_HEAPHDR_FLAGS_USER_START 7 /* 25 user flags */
  4177. #define DUK_HEAPHDR_HEAP_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_HEAP_START + (n))
  4178. #define DUK_HEAPHDR_USER_FLAG_NUMBER(n) (DUK_HEAPHDR_FLAGS_USER_START + (n))
  4179. #define DUK_HEAPHDR_HEAP_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_HEAP_START + (n)))
  4180. #define DUK_HEAPHDR_USER_FLAG(n) (1UL << (DUK_HEAPHDR_FLAGS_USER_START + (n)))
  4181. #define DUK_HEAPHDR_FLAG_REACHABLE DUK_HEAPHDR_HEAP_FLAG(0) /* mark-and-sweep: reachable */
  4182. #define DUK_HEAPHDR_FLAG_TEMPROOT DUK_HEAPHDR_HEAP_FLAG(1) /* mark-and-sweep: children not processed */
  4183. #define DUK_HEAPHDR_FLAG_FINALIZABLE DUK_HEAPHDR_HEAP_FLAG(2) /* mark-and-sweep: finalizable (on current pass) */
  4184. #define DUK_HEAPHDR_FLAG_FINALIZED DUK_HEAPHDR_HEAP_FLAG(3) /* mark-and-sweep: finalized (on previous pass) */
  4185. #define DUK_HEAPHDR_FLAG_READONLY DUK_HEAPHDR_HEAP_FLAG(4) /* read-only object, in code section */
  4186. #define DUK_HTYPE_MIN 0
  4187. #define DUK_HTYPE_STRING 0
  4188. #define DUK_HTYPE_OBJECT 1
  4189. #define DUK_HTYPE_BUFFER 2
  4190. #define DUK_HTYPE_MAX 2
  4191. #if defined(DUK_USE_HEAPPTR16)
  4192. #define DUK_HEAPHDR_GET_NEXT(heap,h) \
  4193. ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_next16))
  4194. #define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \
  4195. (h)->h_next16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) val); \
  4196. } while (0)
  4197. #else
  4198. #define DUK_HEAPHDR_GET_NEXT(heap,h) ((h)->h_next)
  4199. #define DUK_HEAPHDR_SET_NEXT(heap,h,val) do { \
  4200. (h)->h_next = (val); \
  4201. } while (0)
  4202. #endif
  4203. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  4204. #if defined(DUK_USE_HEAPPTR16)
  4205. #define DUK_HEAPHDR_GET_PREV(heap,h) \
  4206. ((duk_heaphdr *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->h_prev16))
  4207. #define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \
  4208. (h)->h_prev16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (val)); \
  4209. } while (0)
  4210. #else
  4211. #define DUK_HEAPHDR_GET_PREV(heap,h) ((h)->h_prev)
  4212. #define DUK_HEAPHDR_SET_PREV(heap,h,val) do { \
  4213. (h)->h_prev = (val); \
  4214. } while (0)
  4215. #endif
  4216. #endif
  4217. #if defined(DUK_USE_REFERENCE_COUNTING)
  4218. #define DUK_HEAPHDR_GET_REFCOUNT(h) ((h)->h_refcount)
  4219. #define DUK_HEAPHDR_SET_REFCOUNT(h,val) do { \
  4220. (h)->h_refcount = (val); \
  4221. DUK_ASSERT((h)->h_refcount == (val)); /* No truncation. */ \
  4222. } while (0)
  4223. #define DUK_HEAPHDR_PREINC_REFCOUNT(h) (++(h)->h_refcount) /* result: updated refcount */
  4224. #define DUK_HEAPHDR_PREDEC_REFCOUNT(h) (--(h)->h_refcount) /* result: updated refcount */
  4225. #else
  4226. /* refcount macros not defined without refcounting, caller must #if defined() now */
  4227. #endif /* DUK_USE_REFERENCE_COUNTING */
  4228. /*
  4229. * Note: type is treated as a field separate from flags, so some masking is
  4230. * involved in the macros below.
  4231. */
  4232. #define DUK_HEAPHDR_GET_FLAGS_RAW(h) ((h)->h_flags)
  4233. #define DUK_HEAPHDR_SET_FLAGS_RAW(h,val) do { \
  4234. (h)->h_flags = (val); } \
  4235. }
  4236. #define DUK_HEAPHDR_GET_FLAGS(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_FLAG_MASK)
  4237. #define DUK_HEAPHDR_SET_FLAGS(h,val) do { \
  4238. (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) | (val); \
  4239. } while (0)
  4240. #define DUK_HEAPHDR_GET_TYPE(h) ((h)->h_flags & DUK_HEAPHDR_FLAGS_TYPE_MASK)
  4241. #define DUK_HEAPHDR_SET_TYPE(h,val) do { \
  4242. (h)->h_flags = ((h)->h_flags & ~(DUK_HEAPHDR_FLAGS_TYPE_MASK)) | (val); \
  4243. } while (0)
  4244. /* Comparison for type >= DUK_HTYPE_MIN skipped; because DUK_HTYPE_MIN is zero
  4245. * and the comparison is unsigned, it's always true and generates warnings.
  4246. */
  4247. #define DUK_HEAPHDR_HTYPE_VALID(h) ( \
  4248. DUK_HEAPHDR_GET_TYPE((h)) <= DUK_HTYPE_MAX \
  4249. )
  4250. #define DUK_HEAPHDR_SET_TYPE_AND_FLAGS(h,tval,fval) do { \
  4251. (h)->h_flags = ((tval) & DUK_HEAPHDR_FLAGS_TYPE_MASK) | \
  4252. ((fval) & DUK_HEAPHDR_FLAGS_FLAG_MASK); \
  4253. } while (0)
  4254. #define DUK_HEAPHDR_SET_FLAG_BITS(h,bits) do { \
  4255. DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \
  4256. (h)->h_flags |= (bits); \
  4257. } while (0)
  4258. #define DUK_HEAPHDR_CLEAR_FLAG_BITS(h,bits) do { \
  4259. DUK_ASSERT(((bits) & ~(DUK_HEAPHDR_FLAGS_FLAG_MASK)) == 0); \
  4260. (h)->h_flags &= ~((bits)); \
  4261. } while (0)
  4262. #define DUK_HEAPHDR_CHECK_FLAG_BITS(h,bits) (((h)->h_flags & (bits)) != 0)
  4263. #define DUK_HEAPHDR_SET_REACHABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  4264. #define DUK_HEAPHDR_CLEAR_REACHABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  4265. #define DUK_HEAPHDR_HAS_REACHABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_REACHABLE)
  4266. #define DUK_HEAPHDR_SET_TEMPROOT(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  4267. #define DUK_HEAPHDR_CLEAR_TEMPROOT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  4268. #define DUK_HEAPHDR_HAS_TEMPROOT(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_TEMPROOT)
  4269. #define DUK_HEAPHDR_SET_FINALIZABLE(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  4270. #define DUK_HEAPHDR_CLEAR_FINALIZABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  4271. #define DUK_HEAPHDR_HAS_FINALIZABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZABLE)
  4272. #define DUK_HEAPHDR_SET_FINALIZED(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  4273. #define DUK_HEAPHDR_CLEAR_FINALIZED(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  4274. #define DUK_HEAPHDR_HAS_FINALIZED(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_FINALIZED)
  4275. #define DUK_HEAPHDR_SET_READONLY(h) DUK_HEAPHDR_SET_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY)
  4276. #define DUK_HEAPHDR_CLEAR_READONLY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY)
  4277. #define DUK_HEAPHDR_HAS_READONLY(h) DUK_HEAPHDR_CHECK_FLAG_BITS((h),DUK_HEAPHDR_FLAG_READONLY)
  4278. /* get or set a range of flags; m=first bit number, n=number of bits */
  4279. #define DUK_HEAPHDR_GET_FLAG_RANGE(h,m,n) (((h)->h_flags >> (m)) & ((1UL << (n)) - 1UL))
  4280. #define DUK_HEAPHDR_SET_FLAG_RANGE(h,m,n,v) do { \
  4281. (h)->h_flags = \
  4282. ((h)->h_flags & (~(((1UL << (n)) - 1UL) << (m)))) \
  4283. | ((v) << (m)); \
  4284. } while (0)
  4285. /* init pointer fields to null */
  4286. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  4287. #define DUK_HEAPHDR_INIT_NULLS(h) do { \
  4288. DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \
  4289. DUK_HEAPHDR_SET_PREV((h), (void *) NULL); \
  4290. } while (0)
  4291. #else
  4292. #define DUK_HEAPHDR_INIT_NULLS(h) do { \
  4293. DUK_HEAPHDR_SET_NEXT((h), (void *) NULL); \
  4294. } while (0)
  4295. #endif
  4296. #define DUK_HEAPHDR_STRING_INIT_NULLS(h) do { \
  4297. (h)->h_next = NULL; \
  4298. } while (0)
  4299. /*
  4300. * Type tests
  4301. */
  4302. /* Take advantage of the fact that for DUK_HTYPE_xxx numbers the lowest bit
  4303. * is only set for DUK_HTYPE_OBJECT (= 1).
  4304. */
  4305. #if 0
  4306. #define DUK_HEAPHDR_IS_OBJECT(h) (DUK_HEAPHDR_GET_TYPE((h)) == DUK_HTYPE_OBJECT)
  4307. #endif
  4308. #define DUK_HEAPHDR_IS_OBJECT(h) ((h)->h_flags & 0x01UL)
  4309. #define DUK_HEAPHDR_IS_STRING(h) (DUK_HEAPHDR_GET_TYPE((h)) == DUK_HTYPE_STRING)
  4310. #define DUK_HEAPHDR_IS_BUFFER(h) (DUK_HEAPHDR_GET_TYPE((h)) == DUK_HTYPE_BUFFER)
  4311. /*
  4312. * Assert helpers
  4313. */
  4314. /* Check that prev/next links are consistent: if e.g. h->prev is != NULL,
  4315. * h->prev->next should point back to h.
  4316. */
  4317. #if defined(DUK_USE_DOUBLE_LINKED_HEAP) && defined(DUK_USE_ASSERTIONS)
  4318. #define DUK_ASSERT_HEAPHDR_LINKS(heap,h) do { \
  4319. if ((h) != NULL) { \
  4320. duk_heaphdr *h__prev, *h__next; \
  4321. h__prev = DUK_HEAPHDR_GET_PREV((heap), (h)); \
  4322. h__next = DUK_HEAPHDR_GET_NEXT((heap), (h)); \
  4323. DUK_ASSERT(h__prev == NULL || (DUK_HEAPHDR_GET_NEXT((heap), h__prev) == (h))); \
  4324. DUK_ASSERT(h__next == NULL || (DUK_HEAPHDR_GET_PREV((heap), h__next) == (h))); \
  4325. } \
  4326. } while (0)
  4327. #else
  4328. #define DUK_ASSERT_HEAPHDR_LINKS(heap,h) do {} while (0)
  4329. #endif
  4330. #define DUK_ASSERT_HEAPHDR_VALID(h) do { \
  4331. DUK_ASSERT((h) != NULL); \
  4332. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID((h))); \
  4333. } while (0)
  4334. #endif /* DUK_HEAPHDR_H_INCLUDED */
  4335. /* #include duk_refcount.h */
  4336. #line 1 "duk_refcount.h"
  4337. /*
  4338. * Reference counting helper macros. The macros take a thread argument
  4339. * and must thus always be executed in a specific thread context. The
  4340. * thread argument is not really needed anymore: DECREF can operate with
  4341. * a heap pointer only, and INCREF needs neither.
  4342. */
  4343. #if !defined(DUK_REFCOUNT_H_INCLUDED)
  4344. #define DUK_REFCOUNT_H_INCLUDED
  4345. #if defined(DUK_USE_REFERENCE_COUNTING)
  4346. #if defined(DUK_USE_ROM_OBJECTS)
  4347. /* With ROM objects "needs refcount update" is true when the value is
  4348. * heap allocated and is not a ROM object.
  4349. */
  4350. /* XXX: double evaluation for 'tv' argument. */
  4351. #define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) \
  4352. (DUK_TVAL_IS_HEAP_ALLOCATED((tv)) && !DUK_HEAPHDR_HAS_READONLY(DUK_TVAL_GET_HEAPHDR((tv))))
  4353. #define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) (!DUK_HEAPHDR_HAS_READONLY((h)))
  4354. #else /* DUK_USE_ROM_OBJECTS */
  4355. /* Without ROM objects "needs refcount update" == is heap allocated. */
  4356. #define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) DUK_TVAL_IS_HEAP_ALLOCATED((tv))
  4357. #define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) 1
  4358. #endif /* DUK_USE_ROM_OBJECTS */
  4359. /* Fast variants, inline refcount operations except for refzero handling.
  4360. * Can be used explicitly when speed is always more important than size.
  4361. * For a good compiler and a single file build, these are basically the
  4362. * same as a forced inline.
  4363. */
  4364. #define DUK_TVAL_INCREF_FAST(thr,tv) do { \
  4365. duk_tval *duk__tv = (tv); \
  4366. DUK_ASSERT(duk__tv != NULL); \
  4367. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \
  4368. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  4369. DUK_ASSERT(duk__h != NULL); \
  4370. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  4371. DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \
  4372. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) != 0); /* No wrapping. */ \
  4373. } \
  4374. } while (0)
  4375. #define DUK_TVAL_DECREF_FAST(thr,tv) do { \
  4376. duk_tval *duk__tv = (tv); \
  4377. DUK_ASSERT(duk__tv != NULL); \
  4378. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \
  4379. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  4380. DUK_ASSERT(duk__h != NULL); \
  4381. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  4382. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  4383. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  4384. duk_heaphdr_refzero((thr), duk__h); \
  4385. } \
  4386. } \
  4387. } while (0)
  4388. #define DUK_TVAL_DECREF_NORZ_FAST(thr,tv) do { \
  4389. duk_tval *duk__tv = (tv); \
  4390. DUK_ASSERT(duk__tv != NULL); \
  4391. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk__tv)) { \
  4392. duk_heaphdr *duk__h = DUK_TVAL_GET_HEAPHDR(duk__tv); \
  4393. DUK_ASSERT(duk__h != NULL); \
  4394. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  4395. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  4396. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  4397. duk_heaphdr_refzero_norz((thr), duk__h); \
  4398. } \
  4399. } \
  4400. } while (0)
  4401. #define DUK_HEAPHDR_INCREF_FAST(thr,h) do { \
  4402. duk_heaphdr *duk__h = (duk_heaphdr *) (h); \
  4403. DUK_ASSERT(duk__h != NULL); \
  4404. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  4405. if (DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(duk__h)) { \
  4406. DUK_HEAPHDR_PREINC_REFCOUNT(duk__h); \
  4407. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) != 0); /* No wrapping. */ \
  4408. } \
  4409. } while (0)
  4410. #define DUK_HEAPHDR_DECREF_FAST_RAW(thr,h,rzcall,rzcast) do { \
  4411. duk_heaphdr *duk__h = (duk_heaphdr *) (h); \
  4412. DUK_ASSERT(duk__h != NULL); \
  4413. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(duk__h)); \
  4414. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(duk__h) > 0); \
  4415. if (DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(duk__h)) { \
  4416. if (DUK_HEAPHDR_PREDEC_REFCOUNT(duk__h) == 0) { \
  4417. (rzcall)((thr), (rzcast) duk__h); \
  4418. } \
  4419. } \
  4420. } while (0)
  4421. #define DUK_HEAPHDR_DECREF_FAST(thr,h) \
  4422. DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_heaphdr_refzero,duk_heaphdr *)
  4423. #define DUK_HEAPHDR_DECREF_NORZ_FAST(thr,h) \
  4424. DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_heaphdr_refzero_norz,duk_heaphdr *)
  4425. /* Slow variants, call to a helper to reduce code size.
  4426. * Can be used explicitly when size is always more important than speed.
  4427. */
  4428. #define DUK_TVAL_INCREF_SLOW(thr,tv) do { duk_tval_incref((tv)); } while (0)
  4429. #define DUK_TVAL_DECREF_SLOW(thr,tv) do { duk_tval_decref((thr), (tv)); } while (0)
  4430. #define DUK_TVAL_DECREF_NORZ_SLOW(thr,tv) do { duk_tval_decref_norz((thr), (tv)); } while (0)
  4431. #define DUK_HEAPHDR_INCREF_SLOW(thr,h) do { duk_heaphdr_incref((duk_heaphdr *) (h)); } while (0)
  4432. #define DUK_HEAPHDR_DECREF_SLOW(thr,h) do { duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); } while (0)
  4433. #define DUK_HEAPHDR_DECREF_NORZ_SLOW(thr,h) do { duk_heaphdr_decref_norz((thr), (duk_heaphdr *) (h)); } while (0)
  4434. #define DUK_HSTRING_INCREF_SLOW(thr,h) do { duk_heaphdr_incref((duk_heaphdr *) (h)); } while (0)
  4435. #define DUK_HSTRING_DECREF_SLOW(thr,h) do { duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); } while (0)
  4436. #define DUK_HSTRING_DECREF_NORZ_SLOW(thr,h) do { duk_heaphdr_decref_norz((thr), (duk_heaphdr *) (h)); } while (0)
  4437. #define DUK_HBUFFER_INCREF_SLOW(thr,h) do { duk_heaphdr_incref((duk_heaphdr *) (h)); } while (0)
  4438. #define DUK_HBUFFER_DECREF_SLOW(thr,h) do { duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); } while (0)
  4439. #define DUK_HBUFFER_DECREF_NORZ_SLOW(thr,h) do { duk_heaphdr_decref_norz((thr), (duk_heaphdr *) (h)); } while (0)
  4440. #define DUK_HOBJECT_INCREF_SLOW(thr,h) do { duk_heaphdr_incref((duk_heaphdr *) (h)); } while (0)
  4441. #define DUK_HOBJECT_DECREF_SLOW(thr,h) do { duk_heaphdr_decref((thr), (duk_heaphdr *) (h)); } while (0)
  4442. #define DUK_HOBJECT_DECREF_NORZ_SLOW(thr,h) do { duk_heaphdr_decref_norz((thr), (duk_heaphdr *) (h)); } while (0)
  4443. /* Default variants. Selection depends on speed/size preference.
  4444. * Concretely: with gcc 4.8.1 -Os x64 the difference in final binary
  4445. * is about +1kB for _FAST variants.
  4446. */
  4447. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  4448. /* XXX: It would be nice to specialize for specific duk_hobject subtypes
  4449. * but current refzero queue handling prevents that.
  4450. */
  4451. #define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_FAST((thr),(tv))
  4452. #define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_FAST((thr),(tv))
  4453. #define DUK_TVAL_DECREF_NORZ(thr,tv) DUK_TVAL_DECREF_NORZ_FAST((thr),(tv))
  4454. #define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_FAST((thr),(h))
  4455. #define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_heaphdr_refzero,duk_heaphdr *)
  4456. #define DUK_HEAPHDR_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_heaphdr_refzero_norz,duk_heaphdr *)
  4457. #define DUK_HSTRING_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  4458. #define DUK_HSTRING_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hstring_refzero,duk_hstring *)
  4459. #define DUK_HSTRING_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hstring_refzero,duk_hstring *) /* no 'norz' variant */
  4460. #define DUK_HOBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  4461. #define DUK_HOBJECT_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero,duk_hobject *)
  4462. #define DUK_HOBJECT_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero_norz,duk_hobject *)
  4463. #define DUK_HBUFFER_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  4464. #define DUK_HBUFFER_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hbuffer_refzero,duk_hbuffer *)
  4465. #define DUK_HBUFFER_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hbuffer_refzero,duk_hbuffer *) /* no 'norz' variant */
  4466. #define DUK_HCOMPFUNC_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4467. #define DUK_HCOMPFUNC_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero,duk_hobject *)
  4468. #define DUK_HCOMPFUNC_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero_norz,duk_hobject *)
  4469. #define DUK_HNATFUNC_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4470. #define DUK_HNATFUNC_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero,duk_hobject *)
  4471. #define DUK_HNATFUNC_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero_norz,duk_hobject *)
  4472. #define DUK_HBUFOBJ_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4473. #define DUK_HBUFOBJ_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero,duk_hobject *)
  4474. #define DUK_HBUFOBJ_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero_norz,duk_hobject *)
  4475. #define DUK_HTHREAD_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4476. #define DUK_HTHREAD_DECREF(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero,duk_hobject *)
  4477. #define DUK_HTHREAD_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_FAST_RAW((thr),(h),duk_hobject_refzero_norz,duk_hobject *)
  4478. #else
  4479. #define DUK_TVAL_INCREF(thr,tv) DUK_TVAL_INCREF_SLOW((thr),(tv))
  4480. #define DUK_TVAL_DECREF(thr,tv) DUK_TVAL_DECREF_SLOW((thr),(tv))
  4481. #define DUK_TVAL_DECREF_NORZ(thr,tv) DUK_TVAL_DECREF_NORZ_SLOW((thr),(tv))
  4482. #define DUK_HEAPHDR_INCREF(thr,h) DUK_HEAPHDR_INCREF_SLOW((thr),(h))
  4483. #define DUK_HEAPHDR_DECREF(thr,h) DUK_HEAPHDR_DECREF_SLOW((thr),(h))
  4484. #define DUK_HEAPHDR_DECREF_NORZ(thr,h) DUK_HEAPHDR_DECREF_NORZ_SLOW((thr),(h))
  4485. #define DUK_HSTRING_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  4486. #define DUK_HSTRING_DECREF(thr,h) DUK_HSTRING_DECREF_SLOW((thr),(h))
  4487. #define DUK_HSTRING_DECREF_NORZ(thr,h) DUK_HSTRING_DECREF_NORZ_SLOW((thr),(h))
  4488. #define DUK_HOBJECT_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  4489. #define DUK_HOBJECT_DECREF(thr,h) DUK_HOBJECT_DECREF_SLOW((thr),(h))
  4490. #define DUK_HOBJECT_DECREF_NORZ(thr,h) DUK_HOBJECT_DECREF_NORZ_SLOW((thr),(h))
  4491. #define DUK_HBUFFER_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) (h))
  4492. #define DUK_HBUFFER_DECREF(thr,h) DUK_HBUFFER_DECREF_SLOW((thr),(h))
  4493. #define DUK_HBUFFER_DECREF_NORZ(thr,h) DUK_HBUFFER_DECREF_NORZ_SLOW((thr),(h))
  4494. #define DUK_HCOMPFUNC_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4495. #define DUK_HCOMPFUNC_DECREF(thr,h) DUK_HOBJECT_DECREF_SLOW((thr),(duk_hobject *) &(h)->obj)
  4496. #define DUK_HCOMPFUNC_DECREF_NORZ(thr,h) DUK_HOBJECT_DECREF_NORZ_SLOW((thr),(duk_hobject *) &(h)->obj)
  4497. #define DUK_HNATFUNC_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4498. #define DUK_HNATFUNC_DECREF(thr,h) DUK_HOBJECT_DECREF_SLOW((thr),(duk_hobject *) &(h)->obj)
  4499. #define DUK_HNATFUNC_DECREF_NORZ(thr,h) DUK_HOBJECT_DECREF_NORZ_SLOW((thr),(duk_hobject *) &(h)->obj)
  4500. #define DUK_HBUFOBJ_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4501. #define DUK_HBUFOBJ_DECREF(thr,h) DUK_HOBJECT_DECREF_SLOW((thr),(duk_hobject *) &(h)->obj)
  4502. #define DUK_HBUFOB_DECREF_NORZ(thr,h) DUK_HOBJECT_DECREF_NORZ_SLOW((thr),(duk_hobject *) &(h)->obj)
  4503. #define DUK_HTHREAD_INCREF(thr,h) DUK_HEAPHDR_INCREF((thr),(duk_heaphdr *) &(h)->obj)
  4504. #define DUK_HTHREAD_DECREF(thr,h) DUK_HOBJECT_DECREF_SLOW((thr),(duk_hobject *) &(h)->obj)
  4505. #define DUK_HTHREAD_DECREF_NORZ(thr,h) DUK_HOBJECT_DECREF_NORZ_SLOW((thr),(duk_hobject *) &(h)->obj)
  4506. #endif
  4507. /* Convenience for some situations; the above macros don't allow NULLs
  4508. * for performance reasons. Macros cover only actually needed cases.
  4509. */
  4510. #define DUK_HEAPHDR_INCREF_ALLOWNULL(thr,h) do { \
  4511. if ((h) != NULL) { \
  4512. DUK_HEAPHDR_INCREF((thr), (duk_heaphdr *) (h)); \
  4513. } \
  4514. } while (0)
  4515. #define DUK_HEAPHDR_DECREF_ALLOWNULL(thr,h) do { \
  4516. if ((h) != NULL) { \
  4517. DUK_HEAPHDR_DECREF((thr), (duk_heaphdr *) (h)); \
  4518. } \
  4519. } while (0)
  4520. #define DUK_HEAPHDR_DECREF_NORZ_ALLOWNULL(thr,h) do { \
  4521. if ((h) != NULL) { \
  4522. DUK_HEAPHDR_DECREF_NORZ((thr), (duk_heaphdr *) (h)); \
  4523. } \
  4524. } while (0)
  4525. #define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do { \
  4526. if ((h) != NULL) { \
  4527. DUK_HOBJECT_INCREF((thr), (h)); \
  4528. } \
  4529. } while (0)
  4530. #define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do { \
  4531. if ((h) != NULL) { \
  4532. DUK_HOBJECT_DECREF((thr), (h)); \
  4533. } \
  4534. } while (0)
  4535. #define DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr,h) do { \
  4536. if ((h) != NULL) { \
  4537. DUK_HOBJECT_DECREF_NORZ((thr), (h)); \
  4538. } \
  4539. } while (0)
  4540. #define DUK_HBUFFER_INCREF_ALLOWNULL(thr,h) do { \
  4541. if ((h) != NULL) { \
  4542. DUK_HBUFFER_INCREF((thr), (h)); \
  4543. } \
  4544. } while (0)
  4545. #define DUK_HBUFFER_DECREF_ALLOWNULL(thr,h) do { \
  4546. if ((h) != NULL) { \
  4547. DUK_HBUFFER_DECREF((thr), (h)); \
  4548. } \
  4549. } while (0)
  4550. #define DUK_HBUFFER_DECREF_NORZ_ALLOWNULL(thr,h) do { \
  4551. if ((h) != NULL) { \
  4552. DUK_HBUFFER_DECREF_NORZ((thr), (h)); \
  4553. } \
  4554. } while (0)
  4555. #define DUK_HTHREAD_INCREF_ALLOWNULL(thr,h) do { \
  4556. if ((h) != NULL) { \
  4557. DUK_HTHREAD_INCREF((thr), (h)); \
  4558. } \
  4559. } while (0)
  4560. #define DUK_HTHREAD_DECREF_ALLOWNULL(thr,h) do { \
  4561. if ((h) != NULL) { \
  4562. DUK_HTHREAD_DECREF((thr), (h)); \
  4563. } \
  4564. } while (0)
  4565. #define DUK_HTHREAD_DECREF_NORZ_ALLOWNULL(thr,h) do { \
  4566. if ((h) != NULL) { \
  4567. DUK_HTHREAD_DECREF_NORZ((thr), (h)); \
  4568. } \
  4569. } while (0)
  4570. /* Called after one or more DECREF NORZ calls to handle pending side effects.
  4571. * At present DECREF NORZ does freeing inline but doesn't execute finalizers,
  4572. * so these macros check for pending finalizers and execute them. The FAST
  4573. * variant is performance critical.
  4574. */
  4575. #if defined(DUK_USE_FINALIZER_SUPPORT)
  4576. #define DUK_REFZERO_CHECK_FAST(thr) do { \
  4577. duk_refzero_check_fast((thr)); \
  4578. } while (0)
  4579. #define DUK_REFZERO_CHECK_SLOW(thr) do { \
  4580. duk_refzero_check_slow((thr)); \
  4581. } while (0)
  4582. #else /* DUK_USE_FINALIZER_SUPPORT */
  4583. #define DUK_REFZERO_CHECK_FAST(thr) do { } while (0)
  4584. #define DUK_REFZERO_CHECK_SLOW(thr) do { } while (0)
  4585. #endif /* DUK_USE_FINALIZER_SUPPORT */
  4586. /*
  4587. * Macros to set a duk_tval and update refcount of the target (decref the
  4588. * old value and incref the new value if necessary). This is both performance
  4589. * and footprint critical; any changes made should be measured for size/speed.
  4590. */
  4591. #define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \
  4592. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4593. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4594. DUK_TVAL_SET_UNDEFINED(tv__dst); \
  4595. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4596. } while (0)
  4597. #define DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ_ALT0(thr,tvptr_dst) do { \
  4598. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4599. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4600. DUK_TVAL_SET_UNDEFINED(tv__dst); \
  4601. DUK_TVAL_DECREF_NORZ((thr), &tv__tmp); \
  4602. } while (0)
  4603. #define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \
  4604. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4605. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4606. DUK_TVAL_SET_UNUSED(tv__dst); \
  4607. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4608. } while (0)
  4609. #define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \
  4610. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4611. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4612. DUK_TVAL_SET_NULL(tv__dst); \
  4613. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4614. } while (0)
  4615. #define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4616. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4617. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4618. DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \
  4619. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4620. } while (0)
  4621. #define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4622. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4623. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4624. DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \
  4625. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4626. } while (0)
  4627. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4628. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4629. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4630. DUK_TVAL_SET_NUMBER_CHKFAST_FAST(tv__dst, (newval)); \
  4631. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4632. } while (0)
  4633. #define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4634. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4635. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4636. DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \
  4637. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4638. } while (0)
  4639. #define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \
  4640. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4641. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4642. DUK_TVAL_SET_NAN(tv__dst); \
  4643. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4644. } while (0)
  4645. #if defined(DUK_USE_FASTINT)
  4646. #define DUK_TVAL_SET_I48_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4647. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4648. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4649. DUK_TVAL_SET_I48(tv__dst, (newval)); \
  4650. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4651. } while (0)
  4652. #define DUK_TVAL_SET_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4653. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4654. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4655. DUK_TVAL_SET_I32(tv__dst, (newval)); \
  4656. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4657. } while (0)
  4658. #define DUK_TVAL_SET_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4659. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4660. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4661. DUK_TVAL_SET_U32(tv__dst, (newval)); \
  4662. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4663. } while (0)
  4664. #else
  4665. #define DUK_TVAL_SET_DOUBLE_CAST_UPDREF(thr,tvptr_dst,newval) \
  4666. DUK_TVAL_SET_DOUBLE_UPDREF((thr), (tvptr_dst), (duk_double_t) (newval))
  4667. #endif /* DUK_USE_FASTINT */
  4668. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \
  4669. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4670. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4671. DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \
  4672. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4673. } while (0)
  4674. #define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4675. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4676. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4677. DUK_TVAL_SET_STRING(tv__dst, (newval)); \
  4678. DUK_HSTRING_INCREF((thr), (newval)); \
  4679. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4680. } while (0)
  4681. #define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4682. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4683. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4684. DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \
  4685. DUK_HOBJECT_INCREF((thr), (newval)); \
  4686. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4687. } while (0)
  4688. #define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4689. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4690. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4691. DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \
  4692. DUK_HBUFFER_INCREF((thr), (newval)); \
  4693. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4694. } while (0)
  4695. #define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4696. duk_tval *tv__dst; duk_tval tv__tmp; tv__dst = (tvptr_dst); \
  4697. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4698. DUK_TVAL_SET_POINTER(tv__dst, (newval)); \
  4699. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4700. } while (0)
  4701. /* DUK_TVAL_SET_TVAL_UPDREF() is used a lot in executor, property lookups,
  4702. * etc, so it's very important for performance. Measure when changing.
  4703. *
  4704. * NOTE: the source and destination duk_tval pointers may be the same, and
  4705. * the macros MUST deal with that correctly.
  4706. */
  4707. /* Original idiom used, minimal code size. */
  4708. #define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \
  4709. duk_tval *tv__dst, *tv__src; duk_tval tv__tmp; \
  4710. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  4711. DUK_TVAL_SET_TVAL(&tv__tmp, tv__dst); \
  4712. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  4713. DUK_TVAL_INCREF((thr), tv__src); \
  4714. DUK_TVAL_DECREF((thr), &tv__tmp); /* side effects */ \
  4715. } while (0)
  4716. /* Faster alternative: avoid making a temporary copy of tvptr_dst and use
  4717. * fast incref/decref macros.
  4718. */
  4719. #define DUK_TVAL_SET_TVAL_UPDREF_ALT1(thr,tvptr_dst,tvptr_src) do { \
  4720. duk_tval *tv__dst, *tv__src; duk_heaphdr *h__obj; \
  4721. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  4722. DUK_TVAL_INCREF_FAST((thr), tv__src); \
  4723. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv__dst)) { \
  4724. h__obj = DUK_TVAL_GET_HEAPHDR(tv__dst); \
  4725. DUK_ASSERT(h__obj != NULL); \
  4726. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  4727. DUK_HEAPHDR_DECREF_FAST((thr), h__obj); /* side effects */ \
  4728. } else { \
  4729. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  4730. } \
  4731. } while (0)
  4732. /* XXX: no optimized variants yet */
  4733. #define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0
  4734. #define DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ_ALT0
  4735. #define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0
  4736. #define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0
  4737. #define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0
  4738. #define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0
  4739. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0
  4740. #define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0
  4741. #define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0
  4742. #if defined(DUK_USE_FASTINT)
  4743. #define DUK_TVAL_SET_I48_UPDREF DUK_TVAL_SET_I48_UPDREF_ALT0
  4744. #define DUK_TVAL_SET_I32_UPDREF DUK_TVAL_SET_I32_UPDREF_ALT0
  4745. #define DUK_TVAL_SET_U32_UPDREF DUK_TVAL_SET_U32_UPDREF_ALT0
  4746. #else
  4747. #define DUK_TVAL_SET_I48_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF /* XXX: fast int-to-double */
  4748. #define DUK_TVAL_SET_I32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF
  4749. #define DUK_TVAL_SET_U32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF
  4750. #endif /* DUK_USE_FASTINT */
  4751. #define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_I48_UPDREF /* convenience */
  4752. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0
  4753. #define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0
  4754. #define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0
  4755. #define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0
  4756. #define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0
  4757. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  4758. /* Optimized for speed. */
  4759. #define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT1
  4760. #define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT1
  4761. #define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4762. #else
  4763. /* Optimized for size. */
  4764. #define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4765. #define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4766. #define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4767. #endif
  4768. #else /* DUK_USE_REFERENCE_COUNTING */
  4769. #define DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv) 0
  4770. #define DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE(h) 0
  4771. #define DUK_TVAL_INCREF_FAST(thr,v) do {} while (0) /* nop */
  4772. #define DUK_TVAL_DECREF_FAST(thr,v) do {} while (0) /* nop */
  4773. #define DUK_TVAL_DECREF_NORZ_FAST(thr,v) do {} while (0) /* nop */
  4774. #define DUK_TVAL_INCREF_SLOW(thr,v) do {} while (0) /* nop */
  4775. #define DUK_TVAL_DECREF_SLOW(thr,v) do {} while (0) /* nop */
  4776. #define DUK_TVAL_DECREF_NORZ_SLOW(thr,v) do {} while (0) /* nop */
  4777. #define DUK_TVAL_INCREF(thr,v) do {} while (0) /* nop */
  4778. #define DUK_TVAL_DECREF(thr,v) do {} while (0) /* nop */
  4779. #define DUK_TVAL_DECREF_NORZ(thr,v) do {} while (0) /* nop */
  4780. #define DUK_HEAPHDR_INCREF_FAST(thr,h) do {} while (0) /* nop */
  4781. #define DUK_HEAPHDR_DECREF_FAST(thr,h) do {} while (0) /* nop */
  4782. #define DUK_HEAPHDR_DECREF_NORZ_FAST(thr,h) do {} while (0) /* nop */
  4783. #define DUK_HEAPHDR_INCREF_SLOW(thr,h) do {} while (0) /* nop */
  4784. #define DUK_HEAPHDR_DECREF_SLOW(thr,h) do {} while (0) /* nop */
  4785. #define DUK_HEAPHDR_DECREF_NORZ_SLOW(thr,h) do {} while (0) /* nop */
  4786. #define DUK_HEAPHDR_INCREF(thr,h) do {} while (0) /* nop */
  4787. #define DUK_HEAPHDR_DECREF(thr,h) do {} while (0) /* nop */
  4788. #define DUK_HEAPHDR_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4789. #define DUK_HSTRING_INCREF_FAST(thr,h) do {} while (0) /* nop */
  4790. #define DUK_HSTRING_DECREF_FAST(thr,h) do {} while (0) /* nop */
  4791. #define DUK_HSTRING_DECREF_NORZ_FAST(thr,h) do {} while (0) /* nop */
  4792. #define DUK_HSTRING_INCREF_SLOW(thr,h) do {} while (0) /* nop */
  4793. #define DUK_HSTRING_DECREF_SLOW(thr,h) do {} while (0) /* nop */
  4794. #define DUK_HSTRING_DECREF_NORZ_SLOW(thr,h) do {} while (0) /* nop */
  4795. #define DUK_HSTRING_INCREF(thr,h) do {} while (0) /* nop */
  4796. #define DUK_HSTRING_DECREF(thr,h) do {} while (0) /* nop */
  4797. #define DUK_HSTRING_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4798. #define DUK_HOBJECT_INCREF_FAST(thr,h) do {} while (0) /* nop */
  4799. #define DUK_HOBJECT_DECREF_FAST(thr,h) do {} while (0) /* nop */
  4800. #define DUK_HOBJECT_DECREF_NORZ_FAST(thr,h) do {} while (0) /* nop */
  4801. #define DUK_HOBJECT_INCREF_SLOW(thr,h) do {} while (0) /* nop */
  4802. #define DUK_HOBJECT_DECREF_SLOW(thr,h) do {} while (0) /* nop */
  4803. #define DUK_HOBJECT_DECREF_NORZ_SLOW(thr,h) do {} while (0) /* nop */
  4804. #define DUK_HOBJECT_INCREF(thr,h) do {} while (0) /* nop */
  4805. #define DUK_HOBJECT_DECREF(thr,h) do {} while (0) /* nop */
  4806. #define DUK_HOBJECT_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4807. #define DUK_HBUFFER_INCREF_FAST(thr,h) do {} while (0) /* nop */
  4808. #define DUK_HBUFFER_DECREF_FAST(thr,h) do {} while (0) /* nop */
  4809. #define DUK_HBUFFER_DECREF_NORZ_FAST(thr,h) do {} while (0) /* nop */
  4810. #define DUK_HBUFFER_INCREF_SLOW(thr,h) do {} while (0) /* nop */
  4811. #define DUK_HBUFFER_DECREF_SLOW(thr,h) do {} while (0) /* nop */
  4812. #define DUK_HBUFFER_DECREF_NORZ_SLOW(thr,h) do {} while (0) /* nop */
  4813. #define DUK_HBUFFER_INCREF(thr,h) do {} while (0) /* nop */
  4814. #define DUK_HBUFFER_DECREF(thr,h) do {} while (0) /* nop */
  4815. #define DUK_HBUFFER_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4816. #define DUK_HCOMPFUNC_INCREF(thr,h) do {} while (0) /* nop */
  4817. #define DUK_HCOMPFUNC_DECREF(thr,h) do {} while (0) /* nop */
  4818. #define DUK_HCOMPFUNC_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4819. #define DUK_HNATFUNC_INCREF(thr,h) do {} while (0) /* nop */
  4820. #define DUK_HNATFUNC_DECREF(thr,h) do {} while (0) /* nop */
  4821. #define DUK_HNATFUNC_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4822. #define DUK_HBUFOBJ_INCREF(thr,h) do {} while (0) /* nop */
  4823. #define DUK_HBUFOBJ_DECREF(thr,h) do {} while (0) /* nop */
  4824. #define DUK_HBUFOBJ_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4825. #define DUK_HTHREAD_INCREF(thr,h) do {} while (0) /* nop */
  4826. #define DUK_HTHREAD_DECREF(thr,h) do {} while (0) /* nop */
  4827. #define DUK_HTHREAD_DECREF_NORZ(thr,h) do {} while (0) /* nop */
  4828. #define DUK_HOBJECT_INCREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  4829. #define DUK_HOBJECT_DECREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  4830. #define DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr,h) do {} while (0) /* nop */
  4831. #define DUK_HBUFFER_INCREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  4832. #define DUK_HBUFFER_DECREF_ALLOWNULL(thr,h) do {} while (0) /* nop */
  4833. #define DUK_HBUFFER_DECREF_NORZ_ALLOWNULL(thr,h) do {} while (0) /* nop */
  4834. #define DUK_REFZERO_CHECK_FAST(thr) do {} while (0) /* nop */
  4835. #define DUK_REFZERO_CHECK_SLOW(thr) do {} while (0) /* nop */
  4836. #define DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0(thr,tvptr_dst) do { \
  4837. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4838. DUK_TVAL_SET_UNDEFINED(tv__dst); \
  4839. DUK_UNREF((thr)); \
  4840. } while (0)
  4841. #define DUK_TVAL_SET_UNUSED_UPDREF_ALT0(thr,tvptr_dst) do { \
  4842. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4843. DUK_TVAL_SET_UNUSED(tv__dst); \
  4844. DUK_UNREF((thr)); \
  4845. } while (0)
  4846. #define DUK_TVAL_SET_NULL_UPDREF_ALT0(thr,tvptr_dst) do { \
  4847. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4848. DUK_TVAL_SET_NULL(tv__dst); \
  4849. DUK_UNREF((thr)); \
  4850. } while (0)
  4851. #define DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4852. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4853. DUK_TVAL_SET_BOOLEAN(tv__dst, (newval)); \
  4854. DUK_UNREF((thr)); \
  4855. } while (0)
  4856. #define DUK_TVAL_SET_NUMBER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4857. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4858. DUK_TVAL_SET_NUMBER(tv__dst, (newval)); \
  4859. DUK_UNREF((thr)); \
  4860. } while (0)
  4861. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4862. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4863. DUK_TVAL_SET_NUMBER_CHKFAST_FAST(tv__dst, (newval)); \
  4864. DUK_UNREF((thr)); \
  4865. } while (0)
  4866. #define DUK_TVAL_SET_DOUBLE_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4867. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4868. DUK_TVAL_SET_DOUBLE(tv__dst, (newval)); \
  4869. DUK_UNREF((thr)); \
  4870. } while (0)
  4871. #define DUK_TVAL_SET_NAN_UPDREF_ALT0(thr,tvptr_dst) do { \
  4872. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4873. DUK_TVAL_SET_NAN(tv__dst); \
  4874. DUK_UNREF((thr)); \
  4875. } while (0)
  4876. #if defined(DUK_USE_FASTINT)
  4877. #define DUK_TVAL_SET_I48_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4878. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4879. DUK_TVAL_SET_I48(tv__dst, (newval)); \
  4880. DUK_UNREF((thr)); \
  4881. } while (0)
  4882. #define DUK_TVAL_SET_I32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4883. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4884. DUK_TVAL_SET_I32(tv__dst, (newval)); \
  4885. DUK_UNREF((thr)); \
  4886. } while (0)
  4887. #define DUK_TVAL_SET_U32_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4888. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4889. DUK_TVAL_SET_U32(tv__dst, (newval)); \
  4890. DUK_UNREF((thr)); \
  4891. } while (0)
  4892. #else
  4893. #define DUK_TVAL_SET_DOUBLE_CAST_UPDREF(thr,tvptr_dst,newval) \
  4894. DUK_TVAL_SET_DOUBLE_UPDREF((thr), (tvptr_dst), (duk_double_t) (newval))
  4895. #endif /* DUK_USE_FASTINT */
  4896. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0(thr,tvptr_dst,lf_v,lf_fp,lf_flags) do { \
  4897. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4898. DUK_TVAL_SET_LIGHTFUNC(tv__dst, (lf_v), (lf_fp), (lf_flags)); \
  4899. DUK_UNREF((thr)); \
  4900. } while (0)
  4901. #define DUK_TVAL_SET_STRING_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4902. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4903. DUK_TVAL_SET_STRING(tv__dst, (newval)); \
  4904. DUK_UNREF((thr)); \
  4905. } while (0)
  4906. #define DUK_TVAL_SET_OBJECT_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4907. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4908. DUK_TVAL_SET_OBJECT(tv__dst, (newval)); \
  4909. DUK_UNREF((thr)); \
  4910. } while (0)
  4911. #define DUK_TVAL_SET_BUFFER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4912. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4913. DUK_TVAL_SET_BUFFER(tv__dst, (newval)); \
  4914. DUK_UNREF((thr)); \
  4915. } while (0)
  4916. #define DUK_TVAL_SET_POINTER_UPDREF_ALT0(thr,tvptr_dst,newval) do { \
  4917. duk_tval *tv__dst; tv__dst = (tvptr_dst); \
  4918. DUK_TVAL_SET_POINTER(tv__dst, (newval)); \
  4919. DUK_UNREF((thr)); \
  4920. } while (0)
  4921. #define DUK_TVAL_SET_TVAL_UPDREF_ALT0(thr,tvptr_dst,tvptr_src) do { \
  4922. duk_tval *tv__dst, *tv__src; \
  4923. tv__dst = (tvptr_dst); tv__src = (tvptr_src); \
  4924. DUK_TVAL_SET_TVAL(tv__dst, tv__src); \
  4925. DUK_UNREF((thr)); \
  4926. } while (0)
  4927. #define DUK_TVAL_SET_UNDEFINED_UPDREF DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0
  4928. #define DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ DUK_TVAL_SET_UNDEFINED_UPDREF_ALT0
  4929. #define DUK_TVAL_SET_UNUSED_UPDREF DUK_TVAL_SET_UNUSED_UPDREF_ALT0
  4930. #define DUK_TVAL_SET_NULL_UPDREF DUK_TVAL_SET_NULL_UPDREF_ALT0
  4931. #define DUK_TVAL_SET_BOOLEAN_UPDREF DUK_TVAL_SET_BOOLEAN_UPDREF_ALT0
  4932. #define DUK_TVAL_SET_NUMBER_UPDREF DUK_TVAL_SET_NUMBER_UPDREF_ALT0
  4933. #define DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF_ALT0
  4934. #define DUK_TVAL_SET_DOUBLE_UPDREF DUK_TVAL_SET_DOUBLE_UPDREF_ALT0
  4935. #define DUK_TVAL_SET_NAN_UPDREF DUK_TVAL_SET_NAN_UPDREF_ALT0
  4936. #if defined(DUK_USE_FASTINT)
  4937. #define DUK_TVAL_SET_I48_UPDREF DUK_TVAL_SET_I48_UPDREF_ALT0
  4938. #define DUK_TVAL_SET_I32_UPDREF DUK_TVAL_SET_I32_UPDREF_ALT0
  4939. #define DUK_TVAL_SET_U32_UPDREF DUK_TVAL_SET_U32_UPDREF_ALT0
  4940. #else
  4941. #define DUK_TVAL_SET_I48_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF /* XXX: fast-int-to-double */
  4942. #define DUK_TVAL_SET_I32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF
  4943. #define DUK_TVAL_SET_U32_UPDREF DUK_TVAL_SET_DOUBLE_CAST_UPDREF
  4944. #endif /* DUK_USE_FASTINT */
  4945. #define DUK_TVAL_SET_FASTINT_UPDREF DUK_TVAL_SET_I48_UPDREF /* convenience */
  4946. #define DUK_TVAL_SET_LIGHTFUNC_UPDREF DUK_TVAL_SET_LIGHTFUNC_UPDREF_ALT0
  4947. #define DUK_TVAL_SET_STRING_UPDREF DUK_TVAL_SET_STRING_UPDREF_ALT0
  4948. #define DUK_TVAL_SET_OBJECT_UPDREF DUK_TVAL_SET_OBJECT_UPDREF_ALT0
  4949. #define DUK_TVAL_SET_BUFFER_UPDREF DUK_TVAL_SET_BUFFER_UPDREF_ALT0
  4950. #define DUK_TVAL_SET_POINTER_UPDREF DUK_TVAL_SET_POINTER_UPDREF_ALT0
  4951. #define DUK_TVAL_SET_TVAL_UPDREF DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4952. #define DUK_TVAL_SET_TVAL_UPDREF_FAST DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4953. #define DUK_TVAL_SET_TVAL_UPDREF_SLOW DUK_TVAL_SET_TVAL_UPDREF_ALT0
  4954. #endif /* DUK_USE_REFERENCE_COUNTING */
  4955. /*
  4956. * Some convenience macros that don't have optimized implementations now.
  4957. */
  4958. #define DUK_TVAL_SET_TVAL_UPDREF_NORZ(thr,tv_dst,tv_src) do { \
  4959. duk_hthread *duk__thr = (thr); \
  4960. duk_tval *duk__dst = (tv_dst); \
  4961. duk_tval *duk__src = (tv_src); \
  4962. DUK_UNREF(duk__thr); \
  4963. DUK_TVAL_DECREF_NORZ(thr, duk__dst); \
  4964. DUK_TVAL_SET_TVAL(duk__dst, duk__src); \
  4965. DUK_TVAL_INCREF(thr, duk__dst); \
  4966. } while (0)
  4967. #define DUK_TVAL_SET_U32_UPDREF_NORZ(thr,tv_dst,val) do { \
  4968. duk_hthread *duk__thr = (thr); \
  4969. duk_tval *duk__dst = (tv_dst); \
  4970. duk_uint32_t duk__val = (duk_uint32_t) (val); \
  4971. DUK_UNREF(duk__thr); \
  4972. DUK_TVAL_DECREF_NORZ(thr, duk__dst); \
  4973. DUK_TVAL_SET_U32(duk__dst, duk__val); \
  4974. } while (0)
  4975. /*
  4976. * Prototypes
  4977. */
  4978. #if defined(DUK_USE_REFERENCE_COUNTING)
  4979. #if defined(DUK_USE_FINALIZER_SUPPORT)
  4980. DUK_INTERNAL_DECL void duk_refzero_check_slow(duk_hthread *thr);
  4981. DUK_INTERNAL_DECL void duk_refzero_check_fast(duk_hthread *thr);
  4982. #endif
  4983. DUK_INTERNAL_DECL void duk_heaphdr_refcount_finalize_norz(duk_heap *heap, duk_heaphdr *hdr);
  4984. DUK_INTERNAL_DECL void duk_hobject_refcount_finalize_norz(duk_heap *heap, duk_hobject *h);
  4985. #if 0 /* Not needed: fast path handles inline; slow path uses duk_heaphdr_decref() which is needed anyway. */
  4986. DUK_INTERNAL_DECL void duk_hstring_decref(duk_hthread *thr, duk_hstring *h);
  4987. DUK_INTERNAL_DECL void duk_hstring_decref_norz(duk_hthread *thr, duk_hstring *h);
  4988. DUK_INTERNAL_DECL void duk_hbuffer_decref(duk_hthread *thr, duk_hbuffer *h);
  4989. DUK_INTERNAL_DECL void duk_hbuffer_decref_norz(duk_hthread *thr, duk_hbuffer *h);
  4990. DUK_INTERNAL_DECL void duk_hobject_decref(duk_hthread *thr, duk_hobject *h);
  4991. DUK_INTERNAL_DECL void duk_hobject_decref_norz(duk_hthread *thr, duk_hobject *h);
  4992. #endif
  4993. DUK_INTERNAL_DECL void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h);
  4994. DUK_INTERNAL_DECL void duk_heaphdr_refzero_norz(duk_hthread *thr, duk_heaphdr *h);
  4995. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  4996. DUK_INTERNAL_DECL void duk_hstring_refzero(duk_hthread *thr, duk_hstring *h); /* no 'norz' variant */
  4997. DUK_INTERNAL_DECL void duk_hbuffer_refzero(duk_hthread *thr, duk_hbuffer *h); /* no 'norz' variant */
  4998. DUK_INTERNAL_DECL void duk_hobject_refzero(duk_hthread *thr, duk_hobject *h);
  4999. DUK_INTERNAL_DECL void duk_hobject_refzero_norz(duk_hthread *thr, duk_hobject *h);
  5000. #else
  5001. DUK_INTERNAL_DECL void duk_tval_incref(duk_tval *tv);
  5002. DUK_INTERNAL_DECL void duk_tval_decref(duk_hthread *thr, duk_tval *tv);
  5003. DUK_INTERNAL_DECL void duk_tval_decref_norz(duk_hthread *thr, duk_tval *tv);
  5004. DUK_INTERNAL_DECL void duk_heaphdr_incref(duk_heaphdr *h);
  5005. DUK_INTERNAL_DECL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h);
  5006. DUK_INTERNAL_DECL void duk_heaphdr_decref_norz(duk_hthread *thr, duk_heaphdr *h);
  5007. #endif
  5008. #else /* DUK_USE_REFERENCE_COUNTING */
  5009. /* no refcounting */
  5010. #endif /* DUK_USE_REFERENCE_COUNTING */
  5011. #endif /* DUK_REFCOUNT_H_INCLUDED */
  5012. /* #include duk_api_internal.h */
  5013. #line 1 "duk_api_internal.h"
  5014. /*
  5015. * Internal API calls which have (stack and other) semantics similar
  5016. * to the public API.
  5017. */
  5018. #if !defined(DUK_API_INTERNAL_H_INCLUDED)
  5019. #define DUK_API_INTERNAL_H_INCLUDED
  5020. #define DUK_INTERNAL_SYMBOL(x) ("\x82" x)
  5021. /* duk_push_sprintf constants */
  5022. #define DUK_PUSH_SPRINTF_INITIAL_SIZE 256L
  5023. #define DUK_PUSH_SPRINTF_SANITY_LIMIT (1L * 1024L * 1024L * 1024L)
  5024. /* Flag ORed to err_code to indicate __FILE__ / __LINE__ is not
  5025. * blamed as source of error for error fileName / lineNumber.
  5026. */
  5027. #define DUK_ERRCODE_FLAG_NOBLAME_FILELINE (1L << 24)
  5028. /* Current convention is to use duk_size_t for value stack sizes and global indices,
  5029. * and duk_idx_t for local frame indices.
  5030. */
  5031. DUK_INTERNAL_DECL void duk_valstack_grow_check_throw(duk_hthread *thr, duk_size_t min_bytes);
  5032. DUK_INTERNAL_DECL duk_bool_t duk_valstack_grow_check_nothrow(duk_hthread *thr, duk_size_t min_bytes);
  5033. DUK_INTERNAL_DECL void duk_valstack_shrink_check_nothrow(duk_hthread *thr, duk_bool_t snug);
  5034. DUK_INTERNAL_DECL void duk_copy_tvals_incref(duk_hthread *thr, duk_tval *tv_dst, duk_tval *tv_src, duk_size_t count);
  5035. DUK_INTERNAL_DECL duk_tval *duk_reserve_gap(duk_hthread *thr, duk_idx_t idx_base, duk_idx_t count);
  5036. DUK_INTERNAL_DECL void duk_set_top_unsafe(duk_hthread *thr, duk_idx_t idx);
  5037. DUK_INTERNAL_DECL void duk_set_top_and_wipe(duk_hthread *thr, duk_idx_t top, duk_idx_t idx_wipe_start);
  5038. DUK_INTERNAL_DECL void duk_dup_0(duk_hthread *thr);
  5039. DUK_INTERNAL_DECL void duk_dup_1(duk_hthread *thr);
  5040. DUK_INTERNAL_DECL void duk_dup_2(duk_hthread *thr);
  5041. /* duk_dup_m1() would be same as duk_dup_top() */
  5042. DUK_INTERNAL_DECL void duk_dup_m2(duk_hthread *thr);
  5043. DUK_INTERNAL_DECL void duk_dup_m3(duk_hthread *thr);
  5044. DUK_INTERNAL_DECL void duk_dup_m4(duk_hthread *thr);
  5045. DUK_INTERNAL_DECL void duk_remove_unsafe(duk_hthread *thr, duk_idx_t idx);
  5046. DUK_INTERNAL_DECL void duk_remove_m2(duk_hthread *thr);
  5047. DUK_INTERNAL_DECL void duk_remove_n(duk_hthread *thr, duk_idx_t idx, duk_idx_t count);
  5048. DUK_INTERNAL_DECL void duk_remove_n_unsafe(duk_hthread *thr, duk_idx_t idx, duk_idx_t count);
  5049. DUK_INTERNAL_DECL duk_int_t duk_get_type_tval(duk_tval *tv);
  5050. DUK_INTERNAL_DECL duk_uint_t duk_get_type_mask_tval(duk_tval *tv);
  5051. #if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS)
  5052. DUK_INTERNAL_DECL const char *duk_get_type_name(duk_hthread *thr, duk_idx_t idx);
  5053. #endif
  5054. DUK_INTERNAL_DECL duk_small_uint_t duk_get_class_number(duk_hthread *thr, duk_idx_t idx);
  5055. DUK_INTERNAL_DECL duk_tval *duk_get_tval(duk_hthread *thr, duk_idx_t idx);
  5056. DUK_INTERNAL_DECL duk_tval *duk_get_tval_or_unused(duk_hthread *thr, duk_idx_t idx);
  5057. DUK_INTERNAL_DECL duk_tval *duk_require_tval(duk_hthread *thr, duk_idx_t idx);
  5058. DUK_INTERNAL_DECL void duk_push_tval(duk_hthread *thr, duk_tval *tv);
  5059. /* Push the current 'this' binding; throw TypeError if binding is not object
  5060. * coercible (CheckObjectCoercible).
  5061. */
  5062. DUK_INTERNAL_DECL void duk_push_this_check_object_coercible(duk_hthread *thr);
  5063. /* duk_push_this() + CheckObjectCoercible() + duk_to_object() */
  5064. DUK_INTERNAL_DECL duk_hobject *duk_push_this_coercible_to_object(duk_hthread *thr);
  5065. /* duk_push_this() + CheckObjectCoercible() + duk_to_string() */
  5066. DUK_INTERNAL_DECL duk_hstring *duk_push_this_coercible_to_string(duk_hthread *thr);
  5067. DUK_INTERNAL_DECL duk_hstring *duk_push_uint_to_hstring(duk_hthread *thr, duk_uint_t i);
  5068. /* Get a borrowed duk_tval pointer to the current 'this' binding. Caller must
  5069. * make sure there's an active callstack entry. Note that the returned pointer
  5070. * is unstable with regards to side effects.
  5071. */
  5072. DUK_INTERNAL_DECL duk_tval *duk_get_borrowed_this_tval(duk_hthread *thr);
  5073. /* XXX: add fastint support? */
  5074. #define duk_push_u64(thr,val) \
  5075. duk_push_number((thr), (duk_double_t) (val))
  5076. #define duk_push_i64(thr,val) \
  5077. duk_push_number((thr), (duk_double_t) (val))
  5078. /* duk_push_(u)int() is guaranteed to support at least (un)signed 32-bit range */
  5079. #define duk_push_u32(thr,val) \
  5080. duk_push_uint((thr), (duk_uint_t) (val))
  5081. #define duk_push_i32(thr,val) \
  5082. duk_push_int((thr), (duk_int_t) (val))
  5083. /* sometimes stack and array indices need to go on the stack */
  5084. #define duk_push_idx(thr,val) \
  5085. duk_push_int((thr), (duk_int_t) (val))
  5086. #define duk_push_uarridx(thr,val) \
  5087. duk_push_uint((thr), (duk_uint_t) (val))
  5088. #define duk_push_size_t(thr,val) \
  5089. duk_push_uint((thr), (duk_uint_t) (val)) /* XXX: assumed to fit for now */
  5090. DUK_INTERNAL_DECL duk_bool_t duk_is_string_notsymbol(duk_hthread *thr, duk_idx_t idx);
  5091. DUK_INTERNAL_DECL duk_bool_t duk_is_callable_tval(duk_hthread *thr, duk_tval *tv);
  5092. DUK_INTERNAL_DECL duk_hstring *duk_get_hstring(duk_hthread *thr, duk_idx_t idx);
  5093. DUK_INTERNAL_DECL duk_hstring *duk_get_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx);
  5094. DUK_INTERNAL_DECL const char *duk_get_string_notsymbol(duk_hthread *thr, duk_idx_t idx);
  5095. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject(duk_hthread *thr, duk_idx_t idx);
  5096. DUK_INTERNAL_DECL duk_hbuffer *duk_get_hbuffer(duk_hthread *thr, duk_idx_t idx);
  5097. DUK_INTERNAL_DECL duk_hthread *duk_get_hthread(duk_hthread *thr, duk_idx_t idx);
  5098. DUK_INTERNAL_DECL duk_hcompfunc *duk_get_hcompfunc(duk_hthread *thr, duk_idx_t idx);
  5099. DUK_INTERNAL_DECL duk_hnatfunc *duk_get_hnatfunc(duk_hthread *thr, duk_idx_t idx);
  5100. DUK_INTERNAL_DECL void *duk_get_buffer_data_raw(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_len, duk_bool_t throw_flag, duk_bool_t *out_isbuffer);
  5101. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_with_class(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t classnum);
  5102. DUK_INTERNAL_DECL duk_hobject *duk_get_hobject_promote_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask);
  5103. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_promote_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask);
  5104. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_accept_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask);
  5105. #define duk_require_hobject_promote_lfunc(thr,idx) \
  5106. duk_require_hobject_promote_mask((thr), (idx), DUK_TYPE_MASK_LIGHTFUNC)
  5107. #define duk_get_hobject_promote_lfunc(thr,idx) \
  5108. duk_get_hobject_promote_mask((thr), (idx), DUK_TYPE_MASK_LIGHTFUNC)
  5109. #if 0 /*unused*/
  5110. DUK_INTERNAL_DECL void *duk_get_voidptr(duk_hthread *thr, duk_idx_t idx);
  5111. #endif
  5112. DUK_INTERNAL_DECL duk_hstring *duk_known_hstring(duk_hthread *thr, duk_idx_t idx);
  5113. DUK_INTERNAL_DECL duk_hobject *duk_known_hobject(duk_hthread *thr, duk_idx_t idx);
  5114. DUK_INTERNAL_DECL duk_hbuffer *duk_known_hbuffer(duk_hthread *thr, duk_idx_t idx);
  5115. DUK_INTERNAL_DECL duk_hcompfunc *duk_known_hcompfunc(duk_hthread *thr, duk_idx_t idx);
  5116. DUK_INTERNAL_DECL duk_hnatfunc *duk_known_hnatfunc(duk_hthread *thr, duk_idx_t idx);
  5117. DUK_INTERNAL_DECL duk_double_t duk_to_number_tval(duk_hthread *thr, duk_tval *tv);
  5118. DUK_INTERNAL_DECL duk_hstring *duk_to_hstring(duk_hthread *thr, duk_idx_t idx);
  5119. DUK_INTERNAL_DECL duk_hstring *duk_to_hstring_m1(duk_hthread *thr);
  5120. DUK_INTERNAL_DECL duk_hstring *duk_to_hstring_acceptsymbol(duk_hthread *thr, duk_idx_t idx);
  5121. DUK_INTERNAL_DECL duk_hobject *duk_to_hobject(duk_hthread *thr, duk_idx_t idx);
  5122. DUK_INTERNAL_DECL duk_double_t duk_to_number_m1(duk_hthread *thr);
  5123. DUK_INTERNAL_DECL duk_double_t duk_to_number_m2(duk_hthread *thr);
  5124. DUK_INTERNAL_DECL duk_bool_t duk_to_boolean_top_pop(duk_hthread *thr);
  5125. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
  5126. DUK_INTERNAL_DECL duk_hstring *duk_safe_to_hstring(duk_hthread *thr, duk_idx_t idx);
  5127. #endif
  5128. DUK_INTERNAL_DECL void duk_push_class_string_tval(duk_hthread *thr, duk_tval *tv, duk_bool_t avoid_side_effects);
  5129. DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped_raw(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped); /* out_clamped=NULL, RangeError if outside range */
  5130. DUK_INTERNAL_DECL duk_int_t duk_to_int_clamped(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval);
  5131. DUK_INTERNAL_DECL duk_int_t duk_to_int_check_range(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval);
  5132. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  5133. DUK_INTERNAL_DECL duk_uint8_t duk_to_uint8clamped(duk_hthread *thr, duk_idx_t idx);
  5134. #endif
  5135. DUK_INTERNAL_DECL duk_hstring *duk_to_property_key_hstring(duk_hthread *thr, duk_idx_t idx);
  5136. DUK_INTERNAL_DECL duk_hstring *duk_require_hstring(duk_hthread *thr, duk_idx_t idx);
  5137. DUK_INTERNAL_DECL duk_hstring *duk_require_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx);
  5138. DUK_INTERNAL_DECL const char *duk_require_lstring_notsymbol(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len);
  5139. DUK_INTERNAL_DECL const char *duk_require_string_notsymbol(duk_hthread *thr, duk_idx_t idx);
  5140. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject(duk_hthread *thr, duk_idx_t idx);
  5141. DUK_INTERNAL_DECL duk_hbuffer *duk_require_hbuffer(duk_hthread *thr, duk_idx_t idx);
  5142. DUK_INTERNAL_DECL duk_hthread *duk_require_hthread(duk_hthread *thr, duk_idx_t idx);
  5143. DUK_INTERNAL_DECL duk_hcompfunc *duk_require_hcompfunc(duk_hthread *thr, duk_idx_t idx);
  5144. DUK_INTERNAL_DECL duk_hnatfunc *duk_require_hnatfunc(duk_hthread *thr, duk_idx_t idx);
  5145. DUK_INTERNAL_DECL duk_hobject *duk_require_hobject_with_class(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t classnum);
  5146. DUK_INTERNAL_DECL void duk_push_hstring(duk_hthread *thr, duk_hstring *h);
  5147. DUK_INTERNAL_DECL void duk_push_hstring_stridx(duk_hthread *thr, duk_small_uint_t stridx);
  5148. DUK_INTERNAL_DECL void duk_push_hstring_empty(duk_hthread *thr);
  5149. DUK_INTERNAL_DECL void duk_push_hobject(duk_hthread *thr, duk_hobject *h);
  5150. DUK_INTERNAL_DECL void duk_push_hbuffer(duk_hthread *thr, duk_hbuffer *h);
  5151. #define duk_push_hthread(thr,h) \
  5152. duk_push_hobject((thr), (duk_hobject *) (h))
  5153. #define duk_push_hnatfunc(thr,h) \
  5154. duk_push_hobject((thr), (duk_hobject *) (h))
  5155. DUK_INTERNAL_DECL void duk_push_hobject_bidx(duk_hthread *thr, duk_small_int_t builtin_idx);
  5156. DUK_INTERNAL_DECL duk_hobject *duk_push_object_helper(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx);
  5157. DUK_INTERNAL_DECL duk_hobject *duk_push_object_helper_proto(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_hobject *proto);
  5158. DUK_INTERNAL_DECL duk_hcompfunc *duk_push_hcompfunc(duk_hthread *thr);
  5159. DUK_INTERNAL_DECL duk_hboundfunc *duk_push_hboundfunc(duk_hthread *thr);
  5160. DUK_INTERNAL_DECL void duk_push_c_function_builtin(duk_hthread *thr, duk_c_function func, duk_int_t nargs);
  5161. DUK_INTERNAL_DECL void duk_push_c_function_builtin_noconstruct(duk_hthread *thr, duk_c_function func, duk_int_t nargs);
  5162. /* XXX: duk_push_harray() and duk_push_hcompfunc() are inconsistent with
  5163. * duk_push_hobject() etc which don't create a new value.
  5164. */
  5165. DUK_INTERNAL_DECL duk_harray *duk_push_harray(duk_hthread *thr);
  5166. DUK_INTERNAL_DECL duk_harray *duk_push_harray_with_size(duk_hthread *thr, duk_uint32_t size);
  5167. DUK_INTERNAL_DECL duk_tval *duk_push_harray_with_size_outptr(duk_hthread *thr, duk_uint32_t size);
  5168. DUK_INTERNAL_DECL void duk_push_string_funcptr(duk_hthread *thr, duk_uint8_t *ptr, duk_size_t sz);
  5169. DUK_INTERNAL_DECL void duk_push_lightfunc_name_raw(duk_hthread *thr, duk_c_function func, duk_small_uint_t lf_flags);
  5170. DUK_INTERNAL_DECL void duk_push_lightfunc_name(duk_hthread *thr, duk_tval *tv);
  5171. DUK_INTERNAL_DECL void duk_push_lightfunc_tostring(duk_hthread *thr, duk_tval *tv);
  5172. #if 0 /* not used yet */
  5173. DUK_INTERNAL_DECL void duk_push_hnatfunc_name(duk_hthread *thr, duk_hnatfunc *h);
  5174. #endif
  5175. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  5176. DUK_INTERNAL_DECL duk_hbufobj *duk_push_bufobj_raw(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx);
  5177. #endif
  5178. DUK_INTERNAL_DECL void *duk_push_fixed_buffer_nozero(duk_hthread *thr, duk_size_t len);
  5179. DUK_INTERNAL_DECL void *duk_push_fixed_buffer_zero(duk_hthread *thr, duk_size_t len);
  5180. DUK_INTERNAL_DECL const char *duk_push_string_readable(duk_hthread *thr, duk_idx_t idx);
  5181. DUK_INTERNAL_DECL const char *duk_push_string_tval_readable(duk_hthread *thr, duk_tval *tv);
  5182. DUK_INTERNAL_DECL const char *duk_push_string_tval_readable_error(duk_hthread *thr, duk_tval *tv);
  5183. /* The duk_xxx_prop_stridx_short() variants expect their arguments to be short
  5184. * enough to be packed into a single 32-bit integer argument. Argument limits
  5185. * vary per call; typically 16 bits are assigned to the signed value stack index
  5186. * and the stridx. In practice these work well for footprint with constant
  5187. * arguments and such call sites are also easiest to verify to be correct.
  5188. */
  5189. DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx); /* [] -> [val] */
  5190. DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args);
  5191. #define duk_get_prop_stridx_short(thr,obj_idx,stridx) \
  5192. (DUK_ASSERT_EXPR((duk_int_t) (obj_idx) >= -0x8000L && (duk_int_t) (obj_idx) <= 0x7fffL), \
  5193. DUK_ASSERT_EXPR((duk_int_t) (stridx) >= 0 && (duk_int_t) (stridx) <= 0xffffL), \
  5194. duk_get_prop_stridx_short_raw((thr), (((duk_uint_t) (obj_idx)) << 16) + ((duk_uint_t) (stridx))))
  5195. DUK_INTERNAL_DECL duk_bool_t duk_get_prop_stridx_boolean(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx, duk_bool_t *out_has_prop); /* [] -> [] */
  5196. DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx); /* [val] -> [] */
  5197. DUK_INTERNAL_DECL duk_bool_t duk_put_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args);
  5198. #define duk_put_prop_stridx_short(thr,obj_idx,stridx) \
  5199. (DUK_ASSERT_EXPR((duk_int_t) (obj_idx) >= -0x8000L && (duk_int_t) (obj_idx) <= 0x7fffL), \
  5200. DUK_ASSERT_EXPR((duk_int_t) (stridx) >= 0 && (duk_int_t) (stridx) <= 0xffffL), \
  5201. duk_put_prop_stridx_short_raw((thr), (((duk_uint_t) (obj_idx)) << 16) + ((duk_uint_t) (stridx))))
  5202. DUK_INTERNAL_DECL duk_bool_t duk_del_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx); /* [] -> [] */
  5203. #if 0 /* Too few call sites to be useful. */
  5204. DUK_INTERNAL_DECL duk_bool_t duk_del_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args);
  5205. #define duk_del_prop_stridx_short(thr,obj_idx,stridx) \
  5206. (DUK_ASSERT_EXPR((obj_idx) >= -0x8000L && (obj_idx) <= 0x7fffL), \
  5207. DUK_ASSERT_EXPR((stridx) >= 0 && (stridx) <= 0xffffL), \
  5208. duk_del_prop_stridx_short_raw((thr), (((duk_uint_t) (obj_idx)) << 16) + ((duk_uint_t) (stridx))))
  5209. #endif
  5210. #define duk_del_prop_stridx_short(thr,obj_idx,stridx) \
  5211. duk_del_prop_stridx((thr), (obj_idx), (stridx))
  5212. DUK_INTERNAL_DECL duk_bool_t duk_has_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx); /* [] -> [] */
  5213. #if 0 /* Too few call sites to be useful. */
  5214. DUK_INTERNAL_DECL duk_bool_t duk_has_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args);
  5215. #define duk_has_prop_stridx_short(thr,obj_idx,stridx) \
  5216. (DUK_ASSERT_EXPR((obj_idx) >= -0x8000L && (obj_idx) <= 0x7fffL), \
  5217. DUK_ASSERT_EXPR((stridx) >= 0 && (stridx) <= 0xffffL), \
  5218. duk_has_prop_stridx_short_raw((thr), (((duk_uint_t) (obj_idx)) << 16) + ((duk_uint_t) (stridx))))
  5219. #endif
  5220. #define duk_has_prop_stridx_short(thr,obj_idx,stridx) \
  5221. duk_has_prop_stridx((thr), (obj_idx), (stridx))
  5222. DUK_INTERNAL_DECL void duk_xdef_prop(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t desc_flags); /* [key val] -> [] */
  5223. DUK_INTERNAL_DECL void duk_xdef_prop_index(duk_hthread *thr, duk_idx_t obj_idx, duk_uarridx_t arr_idx, duk_small_uint_t desc_flags); /* [val] -> [] */
  5224. /* XXX: Because stridx and desc_flags have a limited range, this call could
  5225. * always pack stridx and desc_flags into a single argument.
  5226. */
  5227. DUK_INTERNAL_DECL void duk_xdef_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx, duk_small_uint_t desc_flags); /* [val] -> [] */
  5228. DUK_INTERNAL_DECL void duk_xdef_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args);
  5229. #define duk_xdef_prop_stridx_short(thr,obj_idx,stridx,desc_flags) \
  5230. (DUK_ASSERT_EXPR((duk_int_t) (obj_idx) >= -0x80L && (duk_int_t) (obj_idx) <= 0x7fL), \
  5231. DUK_ASSERT_EXPR((duk_int_t) (stridx) >= 0 && (duk_int_t) (stridx) <= 0xffffL), \
  5232. DUK_ASSERT_EXPR((duk_int_t) (desc_flags) >= 0 && (duk_int_t) (desc_flags) <= 0xffL), \
  5233. duk_xdef_prop_stridx_short_raw((thr), (((duk_uint_t) (obj_idx)) << 24) + (((duk_uint_t) (stridx)) << 8) + (duk_uint_t) (desc_flags)))
  5234. #define duk_xdef_prop_wec(thr,obj_idx) \
  5235. duk_xdef_prop((thr), (obj_idx), DUK_PROPDESC_FLAGS_WEC)
  5236. #define duk_xdef_prop_index_wec(thr,obj_idx,arr_idx) \
  5237. duk_xdef_prop_index((thr), (obj_idx), (arr_idx), DUK_PROPDESC_FLAGS_WEC)
  5238. #define duk_xdef_prop_stridx_wec(thr,obj_idx,stridx) \
  5239. duk_xdef_prop_stridx((thr), (obj_idx), (stridx), DUK_PROPDESC_FLAGS_WEC)
  5240. #define duk_xdef_prop_stridx_short_wec(thr,obj_idx,stridx) \
  5241. duk_xdef_prop_stridx_short((thr), (obj_idx), (stridx), DUK_PROPDESC_FLAGS_WEC)
  5242. #if 0 /*unused*/
  5243. DUK_INTERNAL_DECL void duk_xdef_prop_stridx_builtin(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags); /* [] -> [] */
  5244. #endif
  5245. DUK_INTERNAL_DECL void duk_xdef_prop_stridx_thrower(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx); /* [] -> [] */
  5246. DUK_INTERNAL_DECL duk_bool_t duk_get_method_stridx(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t stridx);
  5247. DUK_INTERNAL_DECL void duk_pack(duk_hthread *thr, duk_idx_t count);
  5248. DUK_INTERNAL_DECL duk_idx_t duk_unpack_array_like(duk_hthread *thr, duk_idx_t idx);
  5249. #if 0
  5250. DUK_INTERNAL_DECL void duk_unpack(duk_hthread *thr);
  5251. #endif
  5252. DUK_INTERNAL_DECL void duk_require_constructor_call(duk_hthread *thr);
  5253. DUK_INTERNAL_DECL void duk_require_constructable(duk_hthread *thr, duk_idx_t idx);
  5254. DUK_INTERNAL_DECL void duk_push_symbol_descriptive_string(duk_hthread *thr, duk_hstring *h);
  5255. DUK_INTERNAL_DECL void duk_resolve_nonbound_function(duk_hthread *thr);
  5256. DUK_INTERNAL_DECL duk_idx_t duk_get_top_require_min(duk_hthread *thr, duk_idx_t min_top);
  5257. DUK_INTERNAL_DECL duk_idx_t duk_get_top_index_unsafe(duk_hthread *thr);
  5258. DUK_INTERNAL_DECL void duk_pop_n_unsafe(duk_hthread *thr, duk_idx_t count);
  5259. DUK_INTERNAL_DECL void duk_pop_unsafe(duk_hthread *thr);
  5260. DUK_INTERNAL_DECL void duk_pop_2_unsafe(duk_hthread *thr);
  5261. DUK_INTERNAL_DECL void duk_pop_3_unsafe(duk_hthread *thr);
  5262. DUK_INTERNAL_DECL void duk_pop_n_nodecref_unsafe(duk_hthread *thr, duk_idx_t count);
  5263. DUK_INTERNAL_DECL void duk_pop_nodecref_unsafe(duk_hthread *thr);
  5264. DUK_INTERNAL_DECL void duk_pop_2_nodecref_unsafe(duk_hthread *thr);
  5265. DUK_INTERNAL_DECL void duk_pop_3_nodecref_unsafe(duk_hthread *thr);
  5266. DUK_INTERNAL_DECL void duk_pop_undefined(duk_hthread *thr);
  5267. DUK_INTERNAL_DECL void duk_compact_m1(duk_hthread *thr);
  5268. DUK_INTERNAL_DECL void duk_seal_freeze_raw(duk_hthread *thr, duk_idx_t obj_idx, duk_bool_t is_freeze);
  5269. DUK_INTERNAL_DECL void duk_insert_undefined(duk_hthread *thr, duk_idx_t idx);
  5270. DUK_INTERNAL_DECL void duk_insert_undefined_n(duk_hthread *thr, duk_idx_t idx, duk_idx_t count);
  5271. DUK_INTERNAL_DECL void duk_concat_2(duk_hthread *thr);
  5272. DUK_INTERNAL_DECL duk_int_t duk_pcall_method_flags(duk_hthread *thr, duk_idx_t nargs, duk_small_uint_t call_flags);
  5273. #if defined(DUK_USE_SYMBOL_BUILTIN)
  5274. DUK_INTERNAL_DECL void duk_to_primitive_ordinary(duk_hthread *thr, duk_idx_t idx, duk_int_t hint);
  5275. #endif
  5276. /* Raw internal valstack access macros: access is unsafe so call site
  5277. * must have a guarantee that the index is valid. When that is the case,
  5278. * using these macro results in faster and smaller code than duk_get_tval().
  5279. * Both 'ctx' and 'idx' are evaluted multiple times, but only for asserts.
  5280. */
  5281. #define DUK_ASSERT_VALID_NEGIDX(thr,idx) \
  5282. (DUK_ASSERT_EXPR((duk_int_t) (idx) < 0), DUK_ASSERT_EXPR(duk_is_valid_index((thr), (idx))))
  5283. #define DUK_ASSERT_VALID_POSIDX(thr,idx) \
  5284. (DUK_ASSERT_EXPR((duk_int_t) (idx) >= 0), DUK_ASSERT_EXPR(duk_is_valid_index((thr), (idx))))
  5285. #define DUK_GET_TVAL_NEGIDX(thr,idx) \
  5286. (DUK_ASSERT_VALID_NEGIDX((thr),(idx)), ((duk_hthread *) (thr))->valstack_top + (idx))
  5287. #define DUK_GET_TVAL_POSIDX(thr,idx) \
  5288. (DUK_ASSERT_VALID_POSIDX((thr),(idx)), ((duk_hthread *) (thr))->valstack_bottom + (idx))
  5289. #define DUK_GET_HOBJECT_NEGIDX(thr,idx) \
  5290. (DUK_ASSERT_VALID_NEGIDX((thr),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (thr))->valstack_top + (idx)))
  5291. #define DUK_GET_HOBJECT_POSIDX(thr,idx) \
  5292. (DUK_ASSERT_VALID_POSIDX((thr),(idx)), DUK_TVAL_GET_OBJECT(((duk_hthread *) (thr))->valstack_bottom + (idx)))
  5293. #define DUK_GET_THIS_TVAL_PTR(thr) \
  5294. (DUK_ASSERT_EXPR((thr)->valstack_bottom > (thr)->valstack), \
  5295. (thr)->valstack_bottom - 1)
  5296. DUK_INTERNAL_DECL duk_double_t duk_time_get_ecmascript_time(duk_hthread *thr);
  5297. DUK_INTERNAL_DECL duk_double_t duk_time_get_ecmascript_time_nofrac(duk_hthread *thr);
  5298. DUK_INTERNAL_DECL duk_double_t duk_time_get_monotonic_time(duk_hthread *thr);
  5299. #endif /* DUK_API_INTERNAL_H_INCLUDED */
  5300. /* #include duk_hstring.h */
  5301. #line 1 "duk_hstring.h"
  5302. /*
  5303. * Heap string representation.
  5304. *
  5305. * Strings are byte sequences ordinarily stored in extended UTF-8 format,
  5306. * allowing values larger than the official UTF-8 range (used internally)
  5307. * and also allowing UTF-8 encoding of surrogate pairs (CESU-8 format).
  5308. * Strings may also be invalid UTF-8 altogether which is the case e.g. with
  5309. * strings used as internal property names and raw buffers converted to
  5310. * strings. In such cases the 'clen' field contains an inaccurate value.
  5311. *
  5312. * ECMAScript requires support for 32-bit long strings. However, since each
  5313. * 16-bit codepoint can take 3 bytes in CESU-8, this representation can only
  5314. * support about 1.4G codepoint long strings in extreme cases. This is not
  5315. * really a practical issue.
  5316. */
  5317. #if !defined(DUK_HSTRING_H_INCLUDED)
  5318. #define DUK_HSTRING_H_INCLUDED
  5319. /* Impose a maximum string length for now. Restricted artificially to
  5320. * ensure adding a heap header length won't overflow size_t. The limit
  5321. * should be synchronized with DUK_HBUFFER_MAX_BYTELEN.
  5322. *
  5323. * E5.1 makes provisions to support strings longer than 4G characters.
  5324. * This limit should be eliminated on 64-bit platforms (and increased
  5325. * closer to maximum support on 32-bit platforms).
  5326. */
  5327. #if defined(DUK_USE_STRLEN16)
  5328. #define DUK_HSTRING_MAX_BYTELEN (0x0000ffffUL)
  5329. #else
  5330. #define DUK_HSTRING_MAX_BYTELEN (0x7fffffffUL)
  5331. #endif
  5332. /* XXX: could add flags for "is valid CESU-8" (ECMAScript compatible strings),
  5333. * "is valid UTF-8", "is valid extended UTF-8" (internal strings are not,
  5334. * regexp bytecode is), and "contains non-BMP characters". These are not
  5335. * needed right now.
  5336. */
  5337. /* With lowmem builds the high 16 bits of duk_heaphdr are used for other
  5338. * purposes, so this leaves 7 duk_heaphdr flags and 9 duk_hstring flags.
  5339. */
  5340. #define DUK_HSTRING_FLAG_ASCII DUK_HEAPHDR_USER_FLAG(0) /* string is ASCII, clen == blen */
  5341. #define DUK_HSTRING_FLAG_ARRIDX DUK_HEAPHDR_USER_FLAG(1) /* string is a valid array index */
  5342. #define DUK_HSTRING_FLAG_SYMBOL DUK_HEAPHDR_USER_FLAG(2) /* string is a symbol (invalid utf-8) */
  5343. #define DUK_HSTRING_FLAG_HIDDEN DUK_HEAPHDR_USER_FLAG(3) /* string is a hidden symbol (implies symbol, Duktape 1.x internal string) */
  5344. #define DUK_HSTRING_FLAG_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(4) /* string is a reserved word (non-strict) */
  5345. #define DUK_HSTRING_FLAG_STRICT_RESERVED_WORD DUK_HEAPHDR_USER_FLAG(5) /* string is a reserved word (strict) */
  5346. #define DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS DUK_HEAPHDR_USER_FLAG(6) /* string is 'eval' or 'arguments' */
  5347. #define DUK_HSTRING_FLAG_EXTDATA DUK_HEAPHDR_USER_FLAG(7) /* string data is external (duk_hstring_external) */
  5348. #define DUK_HSTRING_FLAG_PINNED_LITERAL DUK_HEAPHDR_USER_FLAG(8) /* string is a literal, and pinned */
  5349. #define DUK_HSTRING_HAS_ASCII(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII)
  5350. #define DUK_HSTRING_HAS_ARRIDX(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  5351. #define DUK_HSTRING_HAS_SYMBOL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_SYMBOL)
  5352. #define DUK_HSTRING_HAS_HIDDEN(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_HIDDEN)
  5353. #define DUK_HSTRING_HAS_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  5354. #define DUK_HSTRING_HAS_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  5355. #define DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  5356. #define DUK_HSTRING_HAS_EXTDATA(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  5357. #define DUK_HSTRING_HAS_PINNED_LITERAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_PINNED_LITERAL)
  5358. #define DUK_HSTRING_SET_ASCII(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII)
  5359. #define DUK_HSTRING_SET_ARRIDX(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  5360. #define DUK_HSTRING_SET_SYMBOL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_SYMBOL)
  5361. #define DUK_HSTRING_SET_HIDDEN(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_HIDDEN)
  5362. #define DUK_HSTRING_SET_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  5363. #define DUK_HSTRING_SET_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  5364. #define DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  5365. #define DUK_HSTRING_SET_EXTDATA(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  5366. #define DUK_HSTRING_SET_PINNED_LITERAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_PINNED_LITERAL)
  5367. #define DUK_HSTRING_CLEAR_ASCII(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ASCII)
  5368. #define DUK_HSTRING_CLEAR_ARRIDX(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_ARRIDX)
  5369. #define DUK_HSTRING_CLEAR_SYMBOL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_SYMBOL)
  5370. #define DUK_HSTRING_CLEAR_HIDDEN(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_HIDDEN)
  5371. #define DUK_HSTRING_CLEAR_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_RESERVED_WORD)
  5372. #define DUK_HSTRING_CLEAR_STRICT_RESERVED_WORD(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_STRICT_RESERVED_WORD)
  5373. #define DUK_HSTRING_CLEAR_EVAL_OR_ARGUMENTS(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS)
  5374. #define DUK_HSTRING_CLEAR_EXTDATA(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_EXTDATA)
  5375. #define DUK_HSTRING_CLEAR_PINNED_LITERAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HSTRING_FLAG_PINNED_LITERAL)
  5376. #if 0 /* Slightly smaller code without explicit flag, but explicit flag
  5377. * is very useful when 'clen' is dropped.
  5378. */
  5379. #define DUK_HSTRING_IS_ASCII(x) (DUK_HSTRING_GET_BYTELEN((x)) == DUK_HSTRING_GET_CHARLEN((x)))
  5380. #endif
  5381. #define DUK_HSTRING_IS_ASCII(x) DUK_HSTRING_HAS_ASCII((x)) /* lazily set! */
  5382. #define DUK_HSTRING_IS_EMPTY(x) (DUK_HSTRING_GET_BYTELEN((x)) == 0)
  5383. #if defined(DUK_USE_STRHASH16)
  5384. #define DUK_HSTRING_GET_HASH(x) ((x)->hdr.h_flags >> 16)
  5385. #define DUK_HSTRING_SET_HASH(x,v) do { \
  5386. (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | ((v) << 16); \
  5387. } while (0)
  5388. #else
  5389. #define DUK_HSTRING_GET_HASH(x) ((x)->hash)
  5390. #define DUK_HSTRING_SET_HASH(x,v) do { \
  5391. (x)->hash = (v); \
  5392. } while (0)
  5393. #endif
  5394. #if defined(DUK_USE_STRLEN16)
  5395. #define DUK_HSTRING_GET_BYTELEN(x) ((x)->hdr.h_strextra16)
  5396. #define DUK_HSTRING_SET_BYTELEN(x,v) do { \
  5397. (x)->hdr.h_strextra16 = (v); \
  5398. } while (0)
  5399. #if defined(DUK_USE_HSTRING_CLEN)
  5400. #define DUK_HSTRING_GET_CHARLEN(x) duk_hstring_get_charlen((x))
  5401. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  5402. (x)->clen16 = (v); \
  5403. } while (0)
  5404. #else
  5405. #define DUK_HSTRING_GET_CHARLEN(x) duk_hstring_get_charlen((x))
  5406. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  5407. DUK_ASSERT(0); /* should never be called */ \
  5408. } while (0)
  5409. #endif
  5410. #else
  5411. #define DUK_HSTRING_GET_BYTELEN(x) ((x)->blen)
  5412. #define DUK_HSTRING_SET_BYTELEN(x,v) do { \
  5413. (x)->blen = (v); \
  5414. } while (0)
  5415. #define DUK_HSTRING_GET_CHARLEN(x) duk_hstring_get_charlen((x))
  5416. #define DUK_HSTRING_SET_CHARLEN(x,v) do { \
  5417. (x)->clen = (v); \
  5418. } while (0)
  5419. #endif
  5420. #if defined(DUK_USE_HSTRING_EXTDATA)
  5421. #define DUK_HSTRING_GET_EXTDATA(x) \
  5422. ((x)->extdata)
  5423. #define DUK_HSTRING_GET_DATA(x) \
  5424. (DUK_HSTRING_HAS_EXTDATA((x)) ? \
  5425. DUK_HSTRING_GET_EXTDATA((const duk_hstring_external *) (x)) : ((const duk_uint8_t *) ((x) + 1)))
  5426. #else
  5427. #define DUK_HSTRING_GET_DATA(x) \
  5428. ((const duk_uint8_t *) ((x) + 1))
  5429. #endif
  5430. #define DUK_HSTRING_GET_DATA_END(x) \
  5431. (DUK_HSTRING_GET_DATA((x)) + (x)->blen)
  5432. /* Marker value; in E5 2^32-1 is not a valid array index (2^32-2 is highest
  5433. * valid).
  5434. */
  5435. #define DUK_HSTRING_NO_ARRAY_INDEX (0xffffffffUL)
  5436. #if defined(DUK_USE_HSTRING_ARRIDX)
  5437. #define DUK_HSTRING_GET_ARRIDX_FAST(h) ((h)->arridx)
  5438. #define DUK_HSTRING_GET_ARRIDX_SLOW(h) ((h)->arridx)
  5439. #else
  5440. /* Get array index related to string (or return DUK_HSTRING_NO_ARRAY_INDEX);
  5441. * avoids helper call if string has no array index value.
  5442. */
  5443. #define DUK_HSTRING_GET_ARRIDX_FAST(h) \
  5444. (DUK_HSTRING_HAS_ARRIDX((h)) ? duk_js_to_arrayindex_hstring_fast_known((h)) : DUK_HSTRING_NO_ARRAY_INDEX)
  5445. /* Slower but more compact variant. */
  5446. #define DUK_HSTRING_GET_ARRIDX_SLOW(h) \
  5447. (duk_js_to_arrayindex_hstring_fast((h)))
  5448. #endif
  5449. /* XXX: these actually fit into duk_hstring */
  5450. #define DUK_SYMBOL_TYPE_HIDDEN 0
  5451. #define DUK_SYMBOL_TYPE_GLOBAL 1
  5452. #define DUK_SYMBOL_TYPE_LOCAL 2
  5453. #define DUK_SYMBOL_TYPE_WELLKNOWN 3
  5454. /*
  5455. * Misc
  5456. */
  5457. struct duk_hstring {
  5458. /* Smaller heaphdr than for other objects, because strings are held
  5459. * in string intern table which requires no link pointers. Much of
  5460. * the 32-bit flags field is unused by flags, so we can stuff a 16-bit
  5461. * field in there.
  5462. */
  5463. duk_heaphdr_string hdr;
  5464. /* String hash. */
  5465. #if defined(DUK_USE_STRHASH16)
  5466. /* If 16-bit hash is in use, stuff it into duk_heaphdr_string flags. */
  5467. #else
  5468. duk_uint32_t hash;
  5469. #endif
  5470. /* Precomputed array index (or DUK_HSTRING_NO_ARRAY_INDEX). */
  5471. #if defined(DUK_USE_HSTRING_ARRIDX)
  5472. duk_uarridx_t arridx;
  5473. #endif
  5474. /* Length in bytes (not counting NUL term). */
  5475. #if defined(DUK_USE_STRLEN16)
  5476. /* placed in duk_heaphdr_string */
  5477. #else
  5478. duk_uint32_t blen;
  5479. #endif
  5480. /* Length in codepoints (must be E5 compatible). */
  5481. #if defined(DUK_USE_STRLEN16)
  5482. #if defined(DUK_USE_HSTRING_CLEN)
  5483. duk_uint16_t clen16;
  5484. #else
  5485. /* computed live */
  5486. #endif
  5487. #else
  5488. duk_uint32_t clen;
  5489. #endif
  5490. /*
  5491. * String data of 'blen+1' bytes follows (+1 for NUL termination
  5492. * convenience for C API). No alignment needs to be guaranteed
  5493. * for strings, but fields above should guarantee alignment-by-4
  5494. * (but not alignment-by-8).
  5495. */
  5496. };
  5497. /* The external string struct is defined even when the feature is inactive. */
  5498. struct duk_hstring_external {
  5499. duk_hstring str;
  5500. /*
  5501. * For an external string, the NUL-terminated string data is stored
  5502. * externally. The user must guarantee that data behind this pointer
  5503. * doesn't change while it's used.
  5504. */
  5505. const duk_uint8_t *extdata;
  5506. };
  5507. /*
  5508. * Prototypes
  5509. */
  5510. DUK_INTERNAL_DECL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos, duk_bool_t surrogate_aware);
  5511. DUK_INTERNAL_DECL duk_bool_t duk_hstring_equals_ascii_cstring(duk_hstring *h, const char *cstr);
  5512. DUK_INTERNAL_DECL duk_size_t duk_hstring_get_charlen(duk_hstring *h);
  5513. #if !defined(DUK_USE_HSTRING_LAZY_CLEN)
  5514. DUK_INTERNAL_DECL void duk_hstring_init_charlen(duk_hstring *h);
  5515. #endif
  5516. #endif /* DUK_HSTRING_H_INCLUDED */
  5517. /* #include duk_hobject.h */
  5518. #line 1 "duk_hobject.h"
  5519. /*
  5520. * Heap object representation.
  5521. *
  5522. * Heap objects are used for ECMAScript objects, arrays, and functions,
  5523. * but also for internal control like declarative and object environment
  5524. * records. Compiled functions, native functions, and threads are also
  5525. * objects but with an extended C struct.
  5526. *
  5527. * Objects provide the required ECMAScript semantics and exotic behaviors
  5528. * especially for property access.
  5529. *
  5530. * Properties are stored in three conceptual parts:
  5531. *
  5532. * 1. A linear 'entry part' contains ordered key-value-attributes triples
  5533. * and is the main method of string properties.
  5534. *
  5535. * 2. An optional linear 'array part' is used for array objects to store a
  5536. * (dense) range of [0,N[ array indexed entries with default attributes
  5537. * (writable, enumerable, configurable). If the array part would become
  5538. * sparse or non-default attributes are required, the array part is
  5539. * abandoned and moved to the 'entry part'.
  5540. *
  5541. * 3. An optional 'hash part' is used to optimize lookups of the entry
  5542. * part; it is used only for objects with sufficiently many properties
  5543. * and can be abandoned without loss of information.
  5544. *
  5545. * These three conceptual parts are stored in a single memory allocated area.
  5546. * This minimizes memory allocation overhead but also means that all three
  5547. * parts are resized together, and makes property access a bit complicated.
  5548. */
  5549. #if !defined(DUK_HOBJECT_H_INCLUDED)
  5550. #define DUK_HOBJECT_H_INCLUDED
  5551. /* Object flags. Make sure this stays in sync with debugger object
  5552. * inspection code.
  5553. */
  5554. /* XXX: some flags are object subtype specific (e.g. common to all function
  5555. * subtypes, duk_harray, etc) and could be reused for different subtypes.
  5556. */
  5557. #define DUK_HOBJECT_FLAG_EXTENSIBLE DUK_HEAPHDR_USER_FLAG(0) /* object is extensible */
  5558. #define DUK_HOBJECT_FLAG_CONSTRUCTABLE DUK_HEAPHDR_USER_FLAG(1) /* object is constructable */
  5559. #define DUK_HOBJECT_FLAG_CALLABLE DUK_HEAPHDR_USER_FLAG(2) /* object is callable */
  5560. #define DUK_HOBJECT_FLAG_BOUNDFUNC DUK_HEAPHDR_USER_FLAG(3) /* object established using Function.prototype.bind() */
  5561. #define DUK_HOBJECT_FLAG_COMPFUNC DUK_HEAPHDR_USER_FLAG(4) /* object is a compiled function (duk_hcompfunc) */
  5562. #define DUK_HOBJECT_FLAG_NATFUNC DUK_HEAPHDR_USER_FLAG(5) /* object is a native function (duk_hnatfunc) */
  5563. #define DUK_HOBJECT_FLAG_BUFOBJ DUK_HEAPHDR_USER_FLAG(6) /* object is a buffer object (duk_hbufobj) (always exotic) */
  5564. #define DUK_HOBJECT_FLAG_FASTREFS DUK_HEAPHDR_USER_FLAG(7) /* object has no fields needing DECREF/marking beyond base duk_hobject header */
  5565. #define DUK_HOBJECT_FLAG_ARRAY_PART DUK_HEAPHDR_USER_FLAG(8) /* object has an array part (a_size may still be 0) */
  5566. #define DUK_HOBJECT_FLAG_STRICT DUK_HEAPHDR_USER_FLAG(9) /* function: function object is strict */
  5567. #define DUK_HOBJECT_FLAG_NOTAIL DUK_HEAPHDR_USER_FLAG(10) /* function: function must not be tail called */
  5568. #define DUK_HOBJECT_FLAG_NEWENV DUK_HEAPHDR_USER_FLAG(11) /* function: create new environment when called (see duk_hcompfunc) */
  5569. #define DUK_HOBJECT_FLAG_NAMEBINDING DUK_HEAPHDR_USER_FLAG(12) /* function: create binding for func name (function templates only, used for named function expressions) */
  5570. #define DUK_HOBJECT_FLAG_CREATEARGS DUK_HEAPHDR_USER_FLAG(13) /* function: create an arguments object on function call */
  5571. #define DUK_HOBJECT_FLAG_HAVE_FINALIZER DUK_HEAPHDR_USER_FLAG(14) /* object has a callable (own) finalizer property */
  5572. #define DUK_HOBJECT_FLAG_EXOTIC_ARRAY DUK_HEAPHDR_USER_FLAG(15) /* 'Array' object, array length and index exotic behavior */
  5573. #define DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ DUK_HEAPHDR_USER_FLAG(16) /* 'String' object, array index exotic behavior */
  5574. #define DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS DUK_HEAPHDR_USER_FLAG(17) /* 'Arguments' object and has arguments exotic behavior (non-strict callee) */
  5575. #define DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ DUK_HEAPHDR_USER_FLAG(18) /* 'Proxy' object */
  5576. #define DUK_HOBJECT_FLAG_SPECIAL_CALL DUK_HEAPHDR_USER_FLAG(19) /* special casing in call behavior, for .call(), .apply(), etc. */
  5577. #define DUK_HOBJECT_FLAG_CLASS_BASE DUK_HEAPHDR_USER_FLAG_NUMBER(20)
  5578. #define DUK_HOBJECT_FLAG_CLASS_BITS 5
  5579. #define DUK_HOBJECT_GET_CLASS_NUMBER(h) \
  5580. DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS)
  5581. #define DUK_HOBJECT_SET_CLASS_NUMBER(h,v) \
  5582. DUK_HEAPHDR_SET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS, (v))
  5583. #define DUK_HOBJECT_GET_CLASS_MASK(h) \
  5584. (1UL << DUK_HEAPHDR_GET_FLAG_RANGE(&(h)->hdr, DUK_HOBJECT_FLAG_CLASS_BASE, DUK_HOBJECT_FLAG_CLASS_BITS))
  5585. /* Macro for creating flag initializer from a class number.
  5586. * Unsigned type cast is needed to avoid warnings about coercing
  5587. * a signed integer to an unsigned one; the largest class values
  5588. * have the highest bit (bit 31) set which causes this.
  5589. */
  5590. #define DUK_HOBJECT_CLASS_AS_FLAGS(v) (((duk_uint_t) (v)) << DUK_HOBJECT_FLAG_CLASS_BASE)
  5591. /* E5 Section 8.6.2 + custom classes */
  5592. #define DUK_HOBJECT_CLASS_NONE 0
  5593. #define DUK_HOBJECT_CLASS_OBJECT 1
  5594. #define DUK_HOBJECT_CLASS_ARRAY 2
  5595. #define DUK_HOBJECT_CLASS_FUNCTION 3
  5596. #define DUK_HOBJECT_CLASS_ARGUMENTS 4
  5597. #define DUK_HOBJECT_CLASS_BOOLEAN 5
  5598. #define DUK_HOBJECT_CLASS_DATE 6
  5599. #define DUK_HOBJECT_CLASS_ERROR 7
  5600. #define DUK_HOBJECT_CLASS_JSON 8
  5601. #define DUK_HOBJECT_CLASS_MATH 9
  5602. #define DUK_HOBJECT_CLASS_NUMBER 10
  5603. #define DUK_HOBJECT_CLASS_REGEXP 11
  5604. #define DUK_HOBJECT_CLASS_STRING 12
  5605. #define DUK_HOBJECT_CLASS_GLOBAL 13
  5606. #define DUK_HOBJECT_CLASS_SYMBOL 14
  5607. #define DUK_HOBJECT_CLASS_OBJENV 15 /* custom */
  5608. #define DUK_HOBJECT_CLASS_DECENV 16 /* custom */
  5609. #define DUK_HOBJECT_CLASS_POINTER 17 /* custom */
  5610. #define DUK_HOBJECT_CLASS_THREAD 18 /* custom; implies DUK_HOBJECT_IS_THREAD */
  5611. #define DUK_HOBJECT_CLASS_BUFOBJ_MIN 19
  5612. #define DUK_HOBJECT_CLASS_ARRAYBUFFER 19 /* implies DUK_HOBJECT_IS_BUFOBJ */
  5613. #define DUK_HOBJECT_CLASS_DATAVIEW 20
  5614. #define DUK_HOBJECT_CLASS_INT8ARRAY 21
  5615. #define DUK_HOBJECT_CLASS_UINT8ARRAY 22
  5616. #define DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY 23
  5617. #define DUK_HOBJECT_CLASS_INT16ARRAY 24
  5618. #define DUK_HOBJECT_CLASS_UINT16ARRAY 25
  5619. #define DUK_HOBJECT_CLASS_INT32ARRAY 26
  5620. #define DUK_HOBJECT_CLASS_UINT32ARRAY 27
  5621. #define DUK_HOBJECT_CLASS_FLOAT32ARRAY 28
  5622. #define DUK_HOBJECT_CLASS_FLOAT64ARRAY 29
  5623. #define DUK_HOBJECT_CLASS_BUFOBJ_MAX 29
  5624. #define DUK_HOBJECT_CLASS_MAX 29
  5625. /* Class masks. */
  5626. #define DUK_HOBJECT_CMASK_ALL ((1UL << (DUK_HOBJECT_CLASS_MAX + 1)) - 1UL)
  5627. #define DUK_HOBJECT_CMASK_NONE (1UL << DUK_HOBJECT_CLASS_NONE)
  5628. #define DUK_HOBJECT_CMASK_ARGUMENTS (1UL << DUK_HOBJECT_CLASS_ARGUMENTS)
  5629. #define DUK_HOBJECT_CMASK_ARRAY (1UL << DUK_HOBJECT_CLASS_ARRAY)
  5630. #define DUK_HOBJECT_CMASK_BOOLEAN (1UL << DUK_HOBJECT_CLASS_BOOLEAN)
  5631. #define DUK_HOBJECT_CMASK_DATE (1UL << DUK_HOBJECT_CLASS_DATE)
  5632. #define DUK_HOBJECT_CMASK_ERROR (1UL << DUK_HOBJECT_CLASS_ERROR)
  5633. #define DUK_HOBJECT_CMASK_FUNCTION (1UL << DUK_HOBJECT_CLASS_FUNCTION)
  5634. #define DUK_HOBJECT_CMASK_JSON (1UL << DUK_HOBJECT_CLASS_JSON)
  5635. #define DUK_HOBJECT_CMASK_MATH (1UL << DUK_HOBJECT_CLASS_MATH)
  5636. #define DUK_HOBJECT_CMASK_NUMBER (1UL << DUK_HOBJECT_CLASS_NUMBER)
  5637. #define DUK_HOBJECT_CMASK_OBJECT (1UL << DUK_HOBJECT_CLASS_OBJECT)
  5638. #define DUK_HOBJECT_CMASK_REGEXP (1UL << DUK_HOBJECT_CLASS_REGEXP)
  5639. #define DUK_HOBJECT_CMASK_STRING (1UL << DUK_HOBJECT_CLASS_STRING)
  5640. #define DUK_HOBJECT_CMASK_GLOBAL (1UL << DUK_HOBJECT_CLASS_GLOBAL)
  5641. #define DUK_HOBJECT_CMASK_SYMBOL (1UL << DUK_HOBJECT_CLASS_SYMBOL)
  5642. #define DUK_HOBJECT_CMASK_OBJENV (1UL << DUK_HOBJECT_CLASS_OBJENV)
  5643. #define DUK_HOBJECT_CMASK_DECENV (1UL << DUK_HOBJECT_CLASS_DECENV)
  5644. #define DUK_HOBJECT_CMASK_POINTER (1UL << DUK_HOBJECT_CLASS_POINTER)
  5645. #define DUK_HOBJECT_CMASK_ARRAYBUFFER (1UL << DUK_HOBJECT_CLASS_ARRAYBUFFER)
  5646. #define DUK_HOBJECT_CMASK_DATAVIEW (1UL << DUK_HOBJECT_CLASS_DATAVIEW)
  5647. #define DUK_HOBJECT_CMASK_INT8ARRAY (1UL << DUK_HOBJECT_CLASS_INT8ARRAY)
  5648. #define DUK_HOBJECT_CMASK_UINT8ARRAY (1UL << DUK_HOBJECT_CLASS_UINT8ARRAY)
  5649. #define DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY (1UL << DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY)
  5650. #define DUK_HOBJECT_CMASK_INT16ARRAY (1UL << DUK_HOBJECT_CLASS_INT16ARRAY)
  5651. #define DUK_HOBJECT_CMASK_UINT16ARRAY (1UL << DUK_HOBJECT_CLASS_UINT16ARRAY)
  5652. #define DUK_HOBJECT_CMASK_INT32ARRAY (1UL << DUK_HOBJECT_CLASS_INT32ARRAY)
  5653. #define DUK_HOBJECT_CMASK_UINT32ARRAY (1UL << DUK_HOBJECT_CLASS_UINT32ARRAY)
  5654. #define DUK_HOBJECT_CMASK_FLOAT32ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT32ARRAY)
  5655. #define DUK_HOBJECT_CMASK_FLOAT64ARRAY (1UL << DUK_HOBJECT_CLASS_FLOAT64ARRAY)
  5656. #define DUK_HOBJECT_CMASK_ALL_BUFOBJS \
  5657. (DUK_HOBJECT_CMASK_ARRAYBUFFER | \
  5658. DUK_HOBJECT_CMASK_DATAVIEW | \
  5659. DUK_HOBJECT_CMASK_INT8ARRAY | \
  5660. DUK_HOBJECT_CMASK_UINT8ARRAY | \
  5661. DUK_HOBJECT_CMASK_UINT8CLAMPEDARRAY | \
  5662. DUK_HOBJECT_CMASK_INT16ARRAY | \
  5663. DUK_HOBJECT_CMASK_UINT16ARRAY | \
  5664. DUK_HOBJECT_CMASK_INT32ARRAY | \
  5665. DUK_HOBJECT_CMASK_UINT32ARRAY | \
  5666. DUK_HOBJECT_CMASK_FLOAT32ARRAY | \
  5667. DUK_HOBJECT_CMASK_FLOAT64ARRAY)
  5668. #define DUK_HOBJECT_IS_OBJENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_OBJENV)
  5669. #define DUK_HOBJECT_IS_DECENV(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DECENV)
  5670. #define DUK_HOBJECT_IS_ENV(h) (DUK_HOBJECT_IS_OBJENV((h)) || DUK_HOBJECT_IS_DECENV((h)))
  5671. #define DUK_HOBJECT_IS_ARRAY(h) DUK_HOBJECT_HAS_EXOTIC_ARRAY((h)) /* Rely on class Array <=> exotic Array */
  5672. #define DUK_HOBJECT_IS_BOUNDFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUNDFUNC)
  5673. #define DUK_HOBJECT_IS_COMPFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPFUNC)
  5674. #define DUK_HOBJECT_IS_NATFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATFUNC)
  5675. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  5676. #define DUK_HOBJECT_IS_BUFOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFOBJ)
  5677. #else
  5678. #define DUK_HOBJECT_IS_BUFOBJ(h) 0
  5679. #endif
  5680. #define DUK_HOBJECT_IS_THREAD(h) (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_THREAD)
  5681. #if defined(DUK_USE_ES6_PROXY)
  5682. #define DUK_HOBJECT_IS_PROXY(h) DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ((h))
  5683. #else
  5684. #define DUK_HOBJECT_IS_PROXY(h) 0
  5685. #endif
  5686. #define DUK_HOBJECT_IS_NONBOUND_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  5687. DUK_HOBJECT_FLAG_COMPFUNC | \
  5688. DUK_HOBJECT_FLAG_NATFUNC)
  5689. #define DUK_HOBJECT_IS_FUNCTION(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, \
  5690. DUK_HOBJECT_FLAG_BOUNDFUNC | \
  5691. DUK_HOBJECT_FLAG_COMPFUNC | \
  5692. DUK_HOBJECT_FLAG_NATFUNC)
  5693. #define DUK_HOBJECT_IS_CALLABLE(h) DUK_HOBJECT_HAS_CALLABLE((h))
  5694. /* Object has any exotic behavior(s). */
  5695. #define DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS (DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \
  5696. DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS | \
  5697. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | \
  5698. DUK_HOBJECT_FLAG_BUFOBJ | \
  5699. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  5700. #define DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_EXOTIC_BEHAVIOR_FLAGS)
  5701. /* Object has any virtual properties (not counting Proxy behavior). */
  5702. #define DUK_HOBJECT_VIRTUAL_PROPERTY_FLAGS (DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \
  5703. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ | \
  5704. DUK_HOBJECT_FLAG_BUFOBJ)
  5705. #define DUK_HOBJECT_HAS_VIRTUAL_PROPERTIES(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_VIRTUAL_PROPERTY_FLAGS)
  5706. #define DUK_HOBJECT_HAS_EXTENSIBLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  5707. #define DUK_HOBJECT_HAS_CONSTRUCTABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  5708. #define DUK_HOBJECT_HAS_CALLABLE(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CALLABLE)
  5709. #define DUK_HOBJECT_HAS_BOUNDFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUNDFUNC)
  5710. #define DUK_HOBJECT_HAS_COMPFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPFUNC)
  5711. #define DUK_HOBJECT_HAS_NATFUNC(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATFUNC)
  5712. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  5713. #define DUK_HOBJECT_HAS_BUFOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFOBJ)
  5714. #else
  5715. #define DUK_HOBJECT_HAS_BUFOBJ(h) 0
  5716. #endif
  5717. #define DUK_HOBJECT_HAS_FASTREFS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_FASTREFS)
  5718. #define DUK_HOBJECT_HAS_ARRAY_PART(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  5719. #define DUK_HOBJECT_HAS_STRICT(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  5720. #define DUK_HOBJECT_HAS_NOTAIL(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  5721. #define DUK_HOBJECT_HAS_NEWENV(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  5722. #define DUK_HOBJECT_HAS_NAMEBINDING(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  5723. #define DUK_HOBJECT_HAS_CREATEARGS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  5724. #define DUK_HOBJECT_HAS_HAVE_FINALIZER(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_HAVE_FINALIZER)
  5725. #define DUK_HOBJECT_HAS_EXOTIC_ARRAY(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  5726. #define DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  5727. #define DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  5728. #if defined(DUK_USE_ES6_PROXY)
  5729. #define DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  5730. #else
  5731. #define DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h) 0
  5732. #endif
  5733. #define DUK_HOBJECT_HAS_SPECIAL_CALL(h) DUK_HEAPHDR_CHECK_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_SPECIAL_CALL)
  5734. #define DUK_HOBJECT_SET_EXTENSIBLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  5735. #define DUK_HOBJECT_SET_CONSTRUCTABLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  5736. #define DUK_HOBJECT_SET_CALLABLE(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CALLABLE)
  5737. #define DUK_HOBJECT_SET_BOUNDFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUNDFUNC)
  5738. #define DUK_HOBJECT_SET_COMPFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPFUNC)
  5739. #define DUK_HOBJECT_SET_NATFUNC(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATFUNC)
  5740. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  5741. #define DUK_HOBJECT_SET_BUFOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFOBJ)
  5742. #endif
  5743. #define DUK_HOBJECT_SET_FASTREFS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_FASTREFS)
  5744. #define DUK_HOBJECT_SET_ARRAY_PART(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  5745. #define DUK_HOBJECT_SET_STRICT(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  5746. #define DUK_HOBJECT_SET_NOTAIL(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  5747. #define DUK_HOBJECT_SET_NEWENV(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  5748. #define DUK_HOBJECT_SET_NAMEBINDING(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  5749. #define DUK_HOBJECT_SET_CREATEARGS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  5750. #define DUK_HOBJECT_SET_HAVE_FINALIZER(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_HAVE_FINALIZER)
  5751. #define DUK_HOBJECT_SET_EXOTIC_ARRAY(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  5752. #define DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  5753. #define DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  5754. #if defined(DUK_USE_ES6_PROXY)
  5755. #define DUK_HOBJECT_SET_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  5756. #endif
  5757. #define DUK_HOBJECT_SET_SPECIAL_CALL(h) DUK_HEAPHDR_SET_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_SPECIAL_CALL)
  5758. #define DUK_HOBJECT_CLEAR_EXTENSIBLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXTENSIBLE)
  5759. #define DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CONSTRUCTABLE)
  5760. #define DUK_HOBJECT_CLEAR_CALLABLE(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CALLABLE)
  5761. #define DUK_HOBJECT_CLEAR_BOUNDFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BOUNDFUNC)
  5762. #define DUK_HOBJECT_CLEAR_COMPFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_COMPFUNC)
  5763. #define DUK_HOBJECT_CLEAR_NATFUNC(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NATFUNC)
  5764. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  5765. #define DUK_HOBJECT_CLEAR_BUFOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_BUFOBJ)
  5766. #endif
  5767. #define DUK_HOBJECT_CLEAR_FASTREFS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_FASTREFS)
  5768. #define DUK_HOBJECT_CLEAR_ARRAY_PART(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_ARRAY_PART)
  5769. #define DUK_HOBJECT_CLEAR_STRICT(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_STRICT)
  5770. #define DUK_HOBJECT_CLEAR_NOTAIL(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NOTAIL)
  5771. #define DUK_HOBJECT_CLEAR_NEWENV(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NEWENV)
  5772. #define DUK_HOBJECT_CLEAR_NAMEBINDING(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_NAMEBINDING)
  5773. #define DUK_HOBJECT_CLEAR_CREATEARGS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_CREATEARGS)
  5774. #define DUK_HOBJECT_CLEAR_HAVE_FINALIZER(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_HAVE_FINALIZER)
  5775. #define DUK_HOBJECT_CLEAR_EXOTIC_ARRAY(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARRAY)
  5776. #define DUK_HOBJECT_CLEAR_EXOTIC_STRINGOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ)
  5777. #define DUK_HOBJECT_CLEAR_EXOTIC_ARGUMENTS(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS)
  5778. #if defined(DUK_USE_ES6_PROXY)
  5779. #define DUK_HOBJECT_CLEAR_EXOTIC_PROXYOBJ(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ)
  5780. #endif
  5781. #define DUK_HOBJECT_CLEAR_SPECIAL_CALL(h) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(h)->hdr, DUK_HOBJECT_FLAG_SPECIAL_CALL)
  5782. /* Object can/cannot use FASTREFS, i.e. has no strong reference fields beyond
  5783. * duk_hobject base header. This is used just for asserts so doesn't need to
  5784. * be optimized.
  5785. */
  5786. #define DUK_HOBJECT_PROHIBITS_FASTREFS(h) \
  5787. (DUK_HOBJECT_IS_COMPFUNC((h)) || DUK_HOBJECT_IS_DECENV((h)) || DUK_HOBJECT_IS_OBJENV((h)) || \
  5788. DUK_HOBJECT_IS_BUFOBJ((h)) || DUK_HOBJECT_IS_THREAD((h)) || DUK_HOBJECT_IS_PROXY((h)) || \
  5789. DUK_HOBJECT_IS_BOUNDFUNC((h)))
  5790. #define DUK_HOBJECT_ALLOWS_FASTREFS(h) (!DUK_HOBJECT_PROHIBITS_FASTREFS((h)))
  5791. /* Flags used for property attributes in duk_propdesc and packed flags.
  5792. * Must fit into 8 bits.
  5793. */
  5794. #define DUK_PROPDESC_FLAG_WRITABLE (1U << 0) /* E5 Section 8.6.1 */
  5795. #define DUK_PROPDESC_FLAG_ENUMERABLE (1U << 1) /* E5 Section 8.6.1 */
  5796. #define DUK_PROPDESC_FLAG_CONFIGURABLE (1U << 2) /* E5 Section 8.6.1 */
  5797. #define DUK_PROPDESC_FLAG_ACCESSOR (1U << 3) /* accessor */
  5798. #define DUK_PROPDESC_FLAG_VIRTUAL (1U << 4) /* property is virtual: used in duk_propdesc, never stored
  5799. * (used by e.g. buffer virtual properties)
  5800. */
  5801. #define DUK_PROPDESC_FLAGS_MASK (DUK_PROPDESC_FLAG_WRITABLE | \
  5802. DUK_PROPDESC_FLAG_ENUMERABLE | \
  5803. DUK_PROPDESC_FLAG_CONFIGURABLE | \
  5804. DUK_PROPDESC_FLAG_ACCESSOR)
  5805. /* Additional flags which are passed in the same flags argument as property
  5806. * flags but are not stored in object properties.
  5807. */
  5808. #define DUK_PROPDESC_FLAG_NO_OVERWRITE (1U << 4) /* internal define property: skip write silently if exists */
  5809. /* Convenience defines for property attributes. */
  5810. #define DUK_PROPDESC_FLAGS_NONE 0
  5811. #define DUK_PROPDESC_FLAGS_W (DUK_PROPDESC_FLAG_WRITABLE)
  5812. #define DUK_PROPDESC_FLAGS_E (DUK_PROPDESC_FLAG_ENUMERABLE)
  5813. #define DUK_PROPDESC_FLAGS_C (DUK_PROPDESC_FLAG_CONFIGURABLE)
  5814. #define DUK_PROPDESC_FLAGS_WE (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_ENUMERABLE)
  5815. #define DUK_PROPDESC_FLAGS_WC (DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE)
  5816. #define DUK_PROPDESC_FLAGS_EC (DUK_PROPDESC_FLAG_ENUMERABLE | DUK_PROPDESC_FLAG_CONFIGURABLE)
  5817. #define DUK_PROPDESC_FLAGS_WEC (DUK_PROPDESC_FLAG_WRITABLE | \
  5818. DUK_PROPDESC_FLAG_ENUMERABLE | \
  5819. DUK_PROPDESC_FLAG_CONFIGURABLE)
  5820. /* Flags for duk_hobject_get_own_propdesc() and variants. */
  5821. #define DUK_GETDESC_FLAG_PUSH_VALUE (1U << 0) /* push value to stack */
  5822. #define DUK_GETDESC_FLAG_IGNORE_PROTOLOOP (1U << 1) /* don't throw for prototype loop */
  5823. /*
  5824. * Macro for object validity check
  5825. *
  5826. * Assert for currently guaranteed relations between flags, for instance.
  5827. */
  5828. #define DUK_ASSERT_HOBJECT_VALID(h) do { \
  5829. DUK_ASSERT((h) != NULL); \
  5830. DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE((h)) || \
  5831. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FUNCTION); \
  5832. DUK_ASSERT(!DUK_HOBJECT_IS_BUFOBJ((h)) || \
  5833. (DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAYBUFFER || \
  5834. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_DATAVIEW || \
  5835. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT8ARRAY || \
  5836. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8ARRAY || \
  5837. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY || \
  5838. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT16ARRAY || \
  5839. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT16ARRAY || \
  5840. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_INT32ARRAY || \
  5841. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_UINT32ARRAY || \
  5842. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT32ARRAY || \
  5843. DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_FLOAT64ARRAY)); \
  5844. /* Object is an Array <=> object has exotic array behavior */ \
  5845. DUK_ASSERT((DUK_HOBJECT_GET_CLASS_NUMBER((h)) == DUK_HOBJECT_CLASS_ARRAY && DUK_HOBJECT_HAS_EXOTIC_ARRAY((h))) || \
  5846. (DUK_HOBJECT_GET_CLASS_NUMBER((h)) != DUK_HOBJECT_CLASS_ARRAY && !DUK_HOBJECT_HAS_EXOTIC_ARRAY((h)))); \
  5847. } while (0)
  5848. /*
  5849. * Macros to access the 'props' allocation.
  5850. */
  5851. #if defined(DUK_USE_HEAPPTR16)
  5852. #define DUK_HOBJECT_GET_PROPS(heap,h) \
  5853. ((duk_uint8_t *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (h))->h_extra16))
  5854. #define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \
  5855. ((duk_heaphdr *) (h))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \
  5856. } while (0)
  5857. #else
  5858. #define DUK_HOBJECT_GET_PROPS(heap,h) \
  5859. ((h)->props)
  5860. #define DUK_HOBJECT_SET_PROPS(heap,h,x) do { \
  5861. (h)->props = (duk_uint8_t *) (x); \
  5862. } while (0)
  5863. #endif
  5864. #if defined(DUK_USE_HOBJECT_LAYOUT_1)
  5865. /* LAYOUT 1 */
  5866. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  5867. ((duk_hstring **) (void *) ( \
  5868. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  5869. ))
  5870. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  5871. ((duk_propvalue *) (void *) ( \
  5872. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5873. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_hstring *) \
  5874. ))
  5875. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  5876. ((duk_uint8_t *) (void *) ( \
  5877. DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \
  5878. ))
  5879. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  5880. ((duk_tval *) (void *) ( \
  5881. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5882. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) \
  5883. ))
  5884. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  5885. ((duk_uint32_t *) (void *) ( \
  5886. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5887. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  5888. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  5889. ))
  5890. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  5891. ( \
  5892. (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  5893. (n_arr) * sizeof(duk_tval) + \
  5894. (n_hash) * sizeof(duk_uint32_t) \
  5895. )
  5896. #define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \
  5897. (set_e_k) = (duk_hstring **) (void *) (p_base); \
  5898. (set_e_pv) = (duk_propvalue *) (void *) ((set_e_k) + (n_ent)); \
  5899. (set_e_f) = (duk_uint8_t *) (void *) ((set_e_pv) + (n_ent)); \
  5900. (set_a) = (duk_tval *) (void *) ((set_e_f) + (n_ent)); \
  5901. (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \
  5902. } while (0)
  5903. #elif defined(DUK_USE_HOBJECT_LAYOUT_2)
  5904. /* LAYOUT 2 */
  5905. #if (DUK_USE_ALIGN_BY == 4)
  5906. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((4 - (e_sz)) & 0x03)
  5907. #elif (DUK_USE_ALIGN_BY == 8)
  5908. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) ((8 - (e_sz)) & 0x07)
  5909. #elif (DUK_USE_ALIGN_BY == 1)
  5910. #define DUK_HOBJECT_E_FLAG_PADDING(e_sz) 0
  5911. #else
  5912. #error invalid DUK_USE_ALIGN_BY
  5913. #endif
  5914. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  5915. ((duk_hstring **) (void *) ( \
  5916. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5917. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \
  5918. ))
  5919. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  5920. ((duk_propvalue *) (void *) ( \
  5921. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  5922. ))
  5923. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  5924. ((duk_uint8_t *) (void *) ( \
  5925. DUK_HOBJECT_GET_PROPS((heap), (h)) + DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue)) \
  5926. ))
  5927. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  5928. ((duk_tval *) (void *) ( \
  5929. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5930. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  5931. DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) \
  5932. ))
  5933. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  5934. ((duk_uint32_t *) (void *) ( \
  5935. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5936. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  5937. DUK_HOBJECT_E_FLAG_PADDING(DUK_HOBJECT_GET_ESIZE((h))) + \
  5938. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  5939. ))
  5940. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  5941. ( \
  5942. (n_ent) * (sizeof(duk_hstring *) + sizeof(duk_propvalue) + sizeof(duk_uint8_t)) + \
  5943. DUK_HOBJECT_E_FLAG_PADDING((n_ent)) + \
  5944. (n_arr) * sizeof(duk_tval) + \
  5945. (n_hash) * sizeof(duk_uint32_t) \
  5946. )
  5947. #define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \
  5948. (set_e_pv) = (duk_propvalue *) (void *) (p_base); \
  5949. (set_e_k) = (duk_hstring **) (void *) ((set_e_pv) + (n_ent)); \
  5950. (set_e_f) = (duk_uint8_t *) (void *) ((set_e_k) + (n_ent)); \
  5951. (set_a) = (duk_tval *) (void *) (((duk_uint8_t *) (set_e_f)) + \
  5952. sizeof(duk_uint8_t) * (n_ent) + \
  5953. DUK_HOBJECT_E_FLAG_PADDING((n_ent))); \
  5954. (set_h) = (duk_uint32_t *) (void *) ((set_a) + (n_arr)); \
  5955. } while (0)
  5956. #elif defined(DUK_USE_HOBJECT_LAYOUT_3)
  5957. /* LAYOUT 3 */
  5958. #define DUK_HOBJECT_E_GET_KEY_BASE(heap,h) \
  5959. ((duk_hstring **) (void *) ( \
  5960. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5961. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) + \
  5962. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  5963. ))
  5964. #define DUK_HOBJECT_E_GET_VALUE_BASE(heap,h) \
  5965. ((duk_propvalue *) (void *) ( \
  5966. DUK_HOBJECT_GET_PROPS((heap), (h)) \
  5967. ))
  5968. #define DUK_HOBJECT_E_GET_FLAGS_BASE(heap,h) \
  5969. ((duk_uint8_t *) (void *) ( \
  5970. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5971. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \
  5972. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) + \
  5973. DUK_HOBJECT_GET_HSIZE((h)) * sizeof(duk_uint32_t) \
  5974. ))
  5975. #define DUK_HOBJECT_A_GET_BASE(heap,h) \
  5976. ((duk_tval *) (void *) ( \
  5977. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5978. DUK_HOBJECT_GET_ESIZE((h)) * sizeof(duk_propvalue) \
  5979. ))
  5980. #define DUK_HOBJECT_H_GET_BASE(heap,h) \
  5981. ((duk_uint32_t *) (void *) ( \
  5982. DUK_HOBJECT_GET_PROPS((heap), (h)) + \
  5983. DUK_HOBJECT_GET_ESIZE((h)) * (sizeof(duk_propvalue) + sizeof(duk_hstring *)) + \
  5984. DUK_HOBJECT_GET_ASIZE((h)) * sizeof(duk_tval) \
  5985. ))
  5986. #define DUK_HOBJECT_P_COMPUTE_SIZE(n_ent,n_arr,n_hash) \
  5987. ( \
  5988. (n_ent) * (sizeof(duk_propvalue) + sizeof(duk_hstring *) + sizeof(duk_uint8_t)) + \
  5989. (n_arr) * sizeof(duk_tval) + \
  5990. (n_hash) * sizeof(duk_uint32_t) \
  5991. )
  5992. #define DUK_HOBJECT_P_SET_REALLOC_PTRS(p_base,set_e_k,set_e_pv,set_e_f,set_a,set_h,n_ent,n_arr,n_hash) do { \
  5993. (set_e_pv) = (duk_propvalue *) (void *) (p_base); \
  5994. (set_a) = (duk_tval *) (void *) ((set_e_pv) + (n_ent)); \
  5995. (set_e_k) = (duk_hstring **) (void *) ((set_a) + (n_arr)); \
  5996. (set_h) = (duk_uint32_t *) (void *) ((set_e_k) + (n_ent)); \
  5997. (set_e_f) = (duk_uint8_t *) (void *) ((set_h) + (n_hash)); \
  5998. } while (0)
  5999. #else
  6000. #error invalid hobject layout defines
  6001. #endif /* hobject property layout */
  6002. #define DUK_HOBJECT_P_ALLOC_SIZE(h) \
  6003. DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE((h)), DUK_HOBJECT_GET_ASIZE((h)), DUK_HOBJECT_GET_HSIZE((h)))
  6004. #define DUK_HOBJECT_E_GET_KEY(heap,h,i) (DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)])
  6005. #define DUK_HOBJECT_E_GET_KEY_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_KEY_BASE((heap), (h))[(i)])
  6006. #define DUK_HOBJECT_E_GET_VALUE(heap,h,i) (DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)])
  6007. #define DUK_HOBJECT_E_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE_BASE((heap), (h))[(i)])
  6008. #define DUK_HOBJECT_E_GET_VALUE_TVAL(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v)
  6009. #define DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v)
  6010. #define DUK_HOBJECT_E_GET_VALUE_GETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get)
  6011. #define DUK_HOBJECT_E_GET_VALUE_GETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get)
  6012. #define DUK_HOBJECT_E_GET_VALUE_SETTER(heap,h,i) (DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set)
  6013. #define DUK_HOBJECT_E_GET_VALUE_SETTER_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set)
  6014. #define DUK_HOBJECT_E_GET_FLAGS(heap,h,i) (DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)])
  6015. #define DUK_HOBJECT_E_GET_FLAGS_PTR(heap,h,i) (&DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)])
  6016. #define DUK_HOBJECT_A_GET_VALUE(heap,h,i) (DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)])
  6017. #define DUK_HOBJECT_A_GET_VALUE_PTR(heap,h,i) (&DUK_HOBJECT_A_GET_BASE((heap), (h))[(i)])
  6018. #define DUK_HOBJECT_H_GET_INDEX(heap,h,i) (DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)])
  6019. #define DUK_HOBJECT_H_GET_INDEX_PTR(heap,h,i) (&DUK_HOBJECT_H_GET_BASE((heap), (h))[(i)])
  6020. #define DUK_HOBJECT_E_SET_KEY(heap,h,i,k) do { \
  6021. DUK_HOBJECT_E_GET_KEY((heap), (h), (i)) = (k); \
  6022. } while (0)
  6023. #define DUK_HOBJECT_E_SET_VALUE(heap,h,i,v) do { \
  6024. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)) = (v); \
  6025. } while (0)
  6026. #define DUK_HOBJECT_E_SET_VALUE_TVAL(heap,h,i,v) do { \
  6027. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).v = (v); \
  6028. } while (0)
  6029. #define DUK_HOBJECT_E_SET_VALUE_GETTER(heap,h,i,v) do { \
  6030. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.get = (v); \
  6031. } while (0)
  6032. #define DUK_HOBJECT_E_SET_VALUE_SETTER(heap,h,i,v) do { \
  6033. DUK_HOBJECT_E_GET_VALUE((heap), (h), (i)).a.set = (v); \
  6034. } while (0)
  6035. #define DUK_HOBJECT_E_SET_FLAGS(heap,h,i,f) do { \
  6036. DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) = (duk_uint8_t) (f); \
  6037. } while (0)
  6038. #define DUK_HOBJECT_A_SET_VALUE(heap,h,i,v) do { \
  6039. DUK_HOBJECT_A_GET_VALUE((heap), (h), (i)) = (v); \
  6040. } while (0)
  6041. #define DUK_HOBJECT_A_SET_VALUE_TVAL(heap,h,i,v) \
  6042. DUK_HOBJECT_A_SET_VALUE((heap), (h), (i), (v)) /* alias for above */
  6043. #define DUK_HOBJECT_H_SET_INDEX(heap,h,i,v) do { \
  6044. DUK_HOBJECT_H_GET_INDEX((heap), (h), (i)) = (v); \
  6045. } while (0)
  6046. #define DUK_HOBJECT_E_SET_FLAG_BITS(heap,h,i,mask) do { \
  6047. DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] |= (mask); \
  6048. } while (0)
  6049. #define DUK_HOBJECT_E_CLEAR_FLAG_BITS(heap,h,i,mask) do { \
  6050. DUK_HOBJECT_E_GET_FLAGS_BASE((heap), (h))[(i)] &= ~(mask); \
  6051. } while (0)
  6052. #define DUK_HOBJECT_E_SLOT_IS_WRITABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_WRITABLE) != 0)
  6053. #define DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ENUMERABLE) != 0)
  6054. #define DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0)
  6055. #define DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap,h,i) ((DUK_HOBJECT_E_GET_FLAGS((heap), (h), (i)) & DUK_PROPDESC_FLAG_ACCESSOR) != 0)
  6056. #define DUK_HOBJECT_E_SLOT_SET_WRITABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE)
  6057. #define DUK_HOBJECT_E_SLOT_SET_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE)
  6058. #define DUK_HOBJECT_E_SLOT_SET_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE)
  6059. #define DUK_HOBJECT_E_SLOT_SET_ACCESSOR(heap,h,i) DUK_HOBJECT_E_SET_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR)
  6060. #define DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_WRITABLE)
  6061. #define DUK_HOBJECT_E_SLOT_CLEAR_ENUMERABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ENUMERABLE)
  6062. #define DUK_HOBJECT_E_SLOT_CLEAR_CONFIGURABLE(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_CONFIGURABLE)
  6063. #define DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(heap,h,i) DUK_HOBJECT_E_CLEAR_FLAG_BITS((heap), (h), (i),DUK_PROPDESC_FLAG_ACCESSOR)
  6064. #define DUK_PROPDESC_IS_WRITABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_WRITABLE) != 0)
  6065. #define DUK_PROPDESC_IS_ENUMERABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_ENUMERABLE) != 0)
  6066. #define DUK_PROPDESC_IS_CONFIGURABLE(p) (((p)->flags & DUK_PROPDESC_FLAG_CONFIGURABLE) != 0)
  6067. #define DUK_PROPDESC_IS_ACCESSOR(p) (((p)->flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0)
  6068. #define DUK_HOBJECT_HASHIDX_UNUSED 0xffffffffUL
  6069. #define DUK_HOBJECT_HASHIDX_DELETED 0xfffffffeUL
  6070. /*
  6071. * Macros for accessing size fields
  6072. */
  6073. #if defined(DUK_USE_OBJSIZES16)
  6074. #define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size16)
  6075. #define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size16 = (v); } while (0)
  6076. #define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next16)
  6077. #define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next16 = (v); } while (0)
  6078. #define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next16++)
  6079. #define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size16)
  6080. #define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size16 = (v); } while (0)
  6081. #if defined(DUK_USE_HOBJECT_HASH_PART)
  6082. #define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size16)
  6083. #define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size16 = (v); } while (0)
  6084. #else
  6085. #define DUK_HOBJECT_GET_HSIZE(h) 0
  6086. #define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0)
  6087. #endif
  6088. #else
  6089. #define DUK_HOBJECT_GET_ESIZE(h) ((h)->e_size)
  6090. #define DUK_HOBJECT_SET_ESIZE(h,v) do { (h)->e_size = (v); } while (0)
  6091. #define DUK_HOBJECT_GET_ENEXT(h) ((h)->e_next)
  6092. #define DUK_HOBJECT_SET_ENEXT(h,v) do { (h)->e_next = (v); } while (0)
  6093. #define DUK_HOBJECT_POSTINC_ENEXT(h) ((h)->e_next++)
  6094. #define DUK_HOBJECT_GET_ASIZE(h) ((h)->a_size)
  6095. #define DUK_HOBJECT_SET_ASIZE(h,v) do { (h)->a_size = (v); } while (0)
  6096. #if defined(DUK_USE_HOBJECT_HASH_PART)
  6097. #define DUK_HOBJECT_GET_HSIZE(h) ((h)->h_size)
  6098. #define DUK_HOBJECT_SET_HSIZE(h,v) do { (h)->h_size = (v); } while (0)
  6099. #else
  6100. #define DUK_HOBJECT_GET_HSIZE(h) 0
  6101. #define DUK_HOBJECT_SET_HSIZE(h,v) do { DUK_ASSERT((v) == 0); } while (0)
  6102. #endif
  6103. #endif
  6104. /*
  6105. * Misc
  6106. */
  6107. /* Maximum prototype traversal depth. Sanity limit which handles e.g.
  6108. * prototype loops (even complex ones like 1->2->3->4->2->3->4->2->3->4).
  6109. */
  6110. #define DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY 10000L
  6111. /*
  6112. * ECMAScript [[Class]]
  6113. */
  6114. /* range check not necessary because all 4-bit values are mapped */
  6115. #define DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(n) duk_class_number_to_stridx[(n)]
  6116. #define DUK_HOBJECT_GET_CLASS_STRING(heap,h) \
  6117. DUK_HEAP_GET_STRING( \
  6118. (heap), \
  6119. DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(DUK_HOBJECT_GET_CLASS_NUMBER((h))) \
  6120. )
  6121. /*
  6122. * Macros for property handling
  6123. */
  6124. #if defined(DUK_USE_HEAPPTR16)
  6125. #define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \
  6126. ((duk_hobject *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->prototype16))
  6127. #define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \
  6128. (h)->prototype16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (x)); \
  6129. } while (0)
  6130. #else
  6131. #define DUK_HOBJECT_GET_PROTOTYPE(heap,h) \
  6132. ((h)->prototype)
  6133. #define DUK_HOBJECT_SET_PROTOTYPE(heap,h,x) do { \
  6134. (h)->prototype = (x); \
  6135. } while (0)
  6136. #endif
  6137. /* Set prototype, DECREF earlier value, INCREF new value (tolerating NULLs). */
  6138. #define DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr,h,p) duk_hobject_set_prototype_updref((thr), (h), (p))
  6139. /* Set initial prototype, assume NULL previous prototype, INCREF new value,
  6140. * tolerate NULL.
  6141. */
  6142. #define DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr,h,proto) do { \
  6143. duk_hthread *duk__thr = (thr); \
  6144. duk_hobject *duk__obj = (h); \
  6145. duk_hobject *duk__proto = (proto); \
  6146. DUK_UNREF(duk__thr); \
  6147. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(duk__thr->heap, duk__obj) == NULL); \
  6148. DUK_HOBJECT_SET_PROTOTYPE(duk__thr->heap, duk__obj, duk__proto); \
  6149. DUK_HOBJECT_INCREF_ALLOWNULL(duk__thr, duk__proto); \
  6150. } while (0)
  6151. /*
  6152. * Finalizer check
  6153. */
  6154. #if defined(DUK_USE_HEAPPTR16)
  6155. #define DUK_HOBJECT_HAS_FINALIZER_FAST(heap,h) duk_hobject_has_finalizer_fast_raw((heap), (h))
  6156. #else
  6157. #define DUK_HOBJECT_HAS_FINALIZER_FAST(heap,h) duk_hobject_has_finalizer_fast_raw((h))
  6158. #endif
  6159. /*
  6160. * Resizing and hash behavior
  6161. */
  6162. /* Sanity limit on max number of properties (allocated, not necessarily used).
  6163. * This is somewhat arbitrary, but if we're close to 2**32 properties some
  6164. * algorithms will fail (e.g. hash size selection, next prime selection).
  6165. * Also, we use negative array/entry table indices to indicate 'not found',
  6166. * so anything above 0x80000000 will cause trouble now.
  6167. */
  6168. #if defined(DUK_USE_OBJSIZES16)
  6169. #define DUK_HOBJECT_MAX_PROPERTIES 0x0000ffffUL
  6170. #else
  6171. #define DUK_HOBJECT_MAX_PROPERTIES 0x3fffffffUL /* 2**30-1 ~= 1G properties */
  6172. #endif
  6173. /* internal align target for props allocation, must be 2*n for some n */
  6174. #if (DUK_USE_ALIGN_BY == 4)
  6175. #define DUK_HOBJECT_ALIGN_TARGET 4
  6176. #elif (DUK_USE_ALIGN_BY == 8)
  6177. #define DUK_HOBJECT_ALIGN_TARGET 8
  6178. #elif (DUK_USE_ALIGN_BY == 1)
  6179. #define DUK_HOBJECT_ALIGN_TARGET 1
  6180. #else
  6181. #error invalid DUK_USE_ALIGN_BY
  6182. #endif
  6183. /*
  6184. * PC-to-line constants
  6185. */
  6186. #define DUK_PC2LINE_SKIP 64
  6187. /* maximum length for a SKIP-1 diffstream: 35 bits per entry, rounded up to bytes */
  6188. #define DUK_PC2LINE_MAX_DIFF_LENGTH (((DUK_PC2LINE_SKIP - 1) * 35 + 7) / 8)
  6189. /*
  6190. * Struct defs
  6191. */
  6192. struct duk_propaccessor {
  6193. duk_hobject *get;
  6194. duk_hobject *set;
  6195. };
  6196. union duk_propvalue {
  6197. /* The get/set pointers could be 16-bit pointer compressed but it
  6198. * would make no difference on 32-bit platforms because duk_tval is
  6199. * 8 bytes or more anyway.
  6200. */
  6201. duk_tval v;
  6202. duk_propaccessor a;
  6203. };
  6204. struct duk_propdesc {
  6205. /* read-only values 'lifted' for ease of use */
  6206. duk_small_uint_t flags;
  6207. duk_hobject *get;
  6208. duk_hobject *set;
  6209. /* for updating (all are set to < 0 for virtual properties) */
  6210. duk_int_t e_idx; /* prop index in 'entry part', < 0 if not there */
  6211. duk_int_t h_idx; /* prop index in 'hash part', < 0 if not there */
  6212. duk_int_t a_idx; /* prop index in 'array part', < 0 if not there */
  6213. };
  6214. struct duk_hobject {
  6215. duk_heaphdr hdr;
  6216. /*
  6217. * 'props' contains {key,value,flags} entries, optional array entries, and
  6218. * an optional hash lookup table for non-array entries in a single 'sliced'
  6219. * allocation. There are several layout options, which differ slightly in
  6220. * generated code size/speed and alignment/padding; duk_features.h selects
  6221. * the layout used.
  6222. *
  6223. * Layout 1 (DUK_USE_HOBJECT_LAYOUT_1):
  6224. *
  6225. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  6226. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  6227. * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable)
  6228. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  6229. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  6230. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  6231. *
  6232. * Layout 2 (DUK_USE_HOBJECT_LAYOUT_2):
  6233. *
  6234. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  6235. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  6236. * e_size * sizeof(duk_uint8_t) + pad bytes of entry flags (e_next gc reachable)
  6237. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  6238. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  6239. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  6240. *
  6241. * Layout 3 (DUK_USE_HOBJECT_LAYOUT_3):
  6242. *
  6243. * e_size * sizeof(duk_propvalue) bytes of entry values (e_next gc reachable)
  6244. * a_size * sizeof(duk_tval) bytes of (opt) array values (plain only) (all gc reachable)
  6245. * e_size * sizeof(duk_hstring *) bytes of entry keys (e_next gc reachable)
  6246. * h_size * sizeof(duk_uint32_t) bytes of (opt) hash indexes to entries (e_size),
  6247. * 0xffffffffUL = unused, 0xfffffffeUL = deleted
  6248. * e_size * sizeof(duk_uint8_t) bytes of entry flags (e_next gc reachable)
  6249. *
  6250. * In layout 1, the 'e_next' count is rounded to 4 or 8 on platforms
  6251. * requiring 4 or 8 byte alignment. This ensures proper alignment
  6252. * for the entries, at the cost of memory footprint. However, it's
  6253. * probably preferable to use another layout on such platforms instead.
  6254. *
  6255. * In layout 2, the key and value parts are swapped to avoid padding
  6256. * the key array on platforms requiring alignment by 8. The flags part
  6257. * is padded to get alignment for array entries. The 'e_next' count does
  6258. * not need to be rounded as in layout 1.
  6259. *
  6260. * In layout 3, entry values and array values are always aligned properly,
  6261. * and assuming pointers are at most 8 bytes, so are the entry keys. Hash
  6262. * indices will be properly aligned (assuming pointers are at least 4 bytes).
  6263. * Finally, flags don't need additional alignment. This layout provides
  6264. * compact allocations without padding (even on platforms with alignment
  6265. * requirements) at the cost of a bit slower lookups.
  6266. *
  6267. * Objects with few keys don't have a hash index; keys are looked up linearly,
  6268. * which is cache efficient because the keys are consecutive. Larger objects
  6269. * have a hash index part which contains integer indexes to the entries part.
  6270. *
  6271. * A single allocation reduces memory allocation overhead but requires more
  6272. * work when any part needs to be resized. A sliced allocation for entries
  6273. * makes linear key matching faster on most platforms (more locality) and
  6274. * skimps on flags size (which would be followed by 3 bytes of padding in
  6275. * most architectures if entries were placed in a struct).
  6276. *
  6277. * 'props' also contains internal properties distinguished with a non-BMP
  6278. * prefix. Often used properties should be placed early in 'props' whenever
  6279. * possible to make accessing them as fast a possible.
  6280. */
  6281. #if defined(DUK_USE_HEAPPTR16)
  6282. /* Located in duk_heaphdr h_extra16. Subclasses of duk_hobject (like
  6283. * duk_hcompfunc) are not free to use h_extra16 for this reason.
  6284. */
  6285. #else
  6286. duk_uint8_t *props;
  6287. #endif
  6288. /* prototype: the only internal property lifted outside 'e' as it is so central */
  6289. #if defined(DUK_USE_HEAPPTR16)
  6290. duk_uint16_t prototype16;
  6291. #else
  6292. duk_hobject *prototype;
  6293. #endif
  6294. #if defined(DUK_USE_OBJSIZES16)
  6295. duk_uint16_t e_size16;
  6296. duk_uint16_t e_next16;
  6297. duk_uint16_t a_size16;
  6298. #if defined(DUK_USE_HOBJECT_HASH_PART)
  6299. duk_uint16_t h_size16;
  6300. #endif
  6301. #else
  6302. duk_uint32_t e_size; /* entry part size */
  6303. duk_uint32_t e_next; /* index for next new key ([0,e_next[ are gc reachable) */
  6304. duk_uint32_t a_size; /* array part size (entirely gc reachable) */
  6305. #if defined(DUK_USE_HOBJECT_HASH_PART)
  6306. duk_uint32_t h_size; /* hash part size or 0 if unused */
  6307. #endif
  6308. #endif
  6309. };
  6310. /*
  6311. * Exposed data
  6312. */
  6313. #if !defined(DUK_SINGLE_FILE)
  6314. DUK_INTERNAL_DECL duk_uint8_t duk_class_number_to_stridx[32];
  6315. #endif /* !DUK_SINGLE_FILE */
  6316. /*
  6317. * Prototypes
  6318. */
  6319. /* alloc and init */
  6320. DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc_unchecked(duk_heap *heap, duk_uint_t hobject_flags);
  6321. DUK_INTERNAL_DECL duk_hobject *duk_hobject_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6322. DUK_INTERNAL_DECL duk_harray *duk_harray_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6323. DUK_INTERNAL_DECL duk_hcompfunc *duk_hcompfunc_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6324. DUK_INTERNAL_DECL duk_hnatfunc *duk_hnatfunc_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6325. DUK_INTERNAL_DECL duk_hboundfunc *duk_hboundfunc_alloc(duk_heap *heap, duk_uint_t hobject_flags);
  6326. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  6327. DUK_INTERNAL_DECL duk_hbufobj *duk_hbufobj_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6328. #endif
  6329. DUK_INTERNAL_DECL duk_hthread *duk_hthread_alloc_unchecked(duk_heap *heap, duk_uint_t hobject_flags);
  6330. DUK_INTERNAL_DECL duk_hthread *duk_hthread_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6331. DUK_INTERNAL_DECL duk_hdecenv *duk_hdecenv_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6332. DUK_INTERNAL_DECL duk_hobjenv *duk_hobjenv_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6333. DUK_INTERNAL_DECL duk_hproxy *duk_hproxy_alloc(duk_hthread *thr, duk_uint_t hobject_flags);
  6334. /* resize */
  6335. DUK_INTERNAL_DECL void duk_hobject_realloc_props(duk_hthread *thr,
  6336. duk_hobject *obj,
  6337. duk_uint32_t new_e_size,
  6338. duk_uint32_t new_a_size,
  6339. duk_uint32_t new_h_size,
  6340. duk_bool_t abandon_array);
  6341. DUK_INTERNAL_DECL void duk_hobject_resize_entrypart(duk_hthread *thr,
  6342. duk_hobject *obj,
  6343. duk_uint32_t new_e_size);
  6344. #if 0 /*unused*/
  6345. DUK_INTERNAL_DECL void duk_hobject_resize_arraypart(duk_hthread *thr,
  6346. duk_hobject *obj,
  6347. duk_uint32_t new_a_size);
  6348. #endif
  6349. /* low-level property functions */
  6350. DUK_INTERNAL_DECL duk_bool_t duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *e_idx, duk_int_t *h_idx);
  6351. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key);
  6352. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_entry_tval_ptr_and_attrs(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_uint_t *out_attrs);
  6353. DUK_INTERNAL_DECL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i);
  6354. DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_own_propdesc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags);
  6355. /* XXX: when optimizing for guaranteed property slots, use a guaranteed
  6356. * slot for internal value; this call can then access it directly.
  6357. */
  6358. #define duk_hobject_get_internal_value_tval_ptr(heap,obj) \
  6359. duk_hobject_find_existing_entry_tval_ptr((heap), (obj), DUK_HEAP_STRING_INT_VALUE((heap)))
  6360. /* core property functions */
  6361. DUK_INTERNAL_DECL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key);
  6362. DUK_INTERNAL_DECL duk_bool_t duk_hobject_putprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_tval *tv_val, duk_bool_t throw_flag);
  6363. DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_bool_t throw_flag);
  6364. DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key);
  6365. /* internal property functions */
  6366. #define DUK_DELPROP_FLAG_THROW (1U << 0)
  6367. #define DUK_DELPROP_FLAG_FORCE (1U << 1)
  6368. DUK_INTERNAL_DECL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags);
  6369. DUK_INTERNAL_DECL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key);
  6370. DUK_INTERNAL_DECL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags);
  6371. DUK_INTERNAL_DECL void duk_hobject_define_property_internal_arridx(duk_hthread *thr, duk_hobject *obj, duk_uarridx_t arr_idx, duk_small_uint_t flags);
  6372. DUK_INTERNAL_DECL duk_size_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj);
  6373. #if defined(DUK_USE_HEAPPTR16)
  6374. DUK_INTERNAL_DECL duk_bool_t duk_hobject_has_finalizer_fast_raw(duk_heap *heap, duk_hobject *obj);
  6375. #else
  6376. DUK_INTERNAL_DECL duk_bool_t duk_hobject_has_finalizer_fast_raw(duk_hobject *obj);
  6377. #endif
  6378. /* helpers for defineProperty() and defineProperties() */
  6379. DUK_INTERNAL_DECL void duk_hobject_prepare_property_descriptor(duk_hthread *thr,
  6380. duk_idx_t idx_in,
  6381. duk_uint_t *out_defprop_flags,
  6382. duk_idx_t *out_idx_value,
  6383. duk_hobject **out_getter,
  6384. duk_hobject **out_setter);
  6385. DUK_INTERNAL_DECL duk_bool_t duk_hobject_define_property_helper(duk_hthread *thr,
  6386. duk_uint_t defprop_flags,
  6387. duk_hobject *obj,
  6388. duk_hstring *key,
  6389. duk_idx_t idx_value,
  6390. duk_hobject *get,
  6391. duk_hobject *set,
  6392. duk_bool_t throw_flag);
  6393. /* Object built-in methods */
  6394. DUK_INTERNAL_DECL void duk_hobject_object_get_own_property_descriptor(duk_hthread *thr, duk_idx_t obj_idx);
  6395. DUK_INTERNAL_DECL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze);
  6396. DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen);
  6397. DUK_INTERNAL_DECL duk_bool_t duk_hobject_object_ownprop_helper(duk_hthread *thr, duk_small_uint_t required_desc_flags);
  6398. /* internal properties */
  6399. DUK_INTERNAL_DECL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv);
  6400. DUK_INTERNAL_DECL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj);
  6401. /* hobject management functions */
  6402. DUK_INTERNAL_DECL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj);
  6403. /* ES2015 proxy */
  6404. #if defined(DUK_USE_ES6_PROXY)
  6405. DUK_INTERNAL_DECL duk_bool_t duk_hobject_proxy_check(duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler);
  6406. DUK_INTERNAL_DECL duk_hobject *duk_hobject_resolve_proxy_target(duk_hobject *obj);
  6407. #endif
  6408. /* enumeration */
  6409. DUK_INTERNAL_DECL void duk_hobject_enumerator_create(duk_hthread *thr, duk_small_uint_t enum_flags);
  6410. DUK_INTERNAL_DECL duk_ret_t duk_hobject_get_enumerated_keys(duk_hthread *thr, duk_small_uint_t enum_flags);
  6411. DUK_INTERNAL_DECL duk_bool_t duk_hobject_enumerator_next(duk_hthread *thr, duk_bool_t get_value);
  6412. /* macros */
  6413. DUK_INTERNAL_DECL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p);
  6414. /* pc2line */
  6415. #if defined(DUK_USE_PC2LINE)
  6416. DUK_INTERNAL_DECL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length);
  6417. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hobject_pc2line_query(duk_hthread *thr, duk_idx_t idx_func, duk_uint_fast32_t pc);
  6418. #endif
  6419. /* misc */
  6420. DUK_INTERNAL_DECL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop);
  6421. #if !defined(DUK_USE_OBJECT_BUILTIN)
  6422. /* These declarations are needed when related built-in is disabled and
  6423. * genbuiltins.py won't automatically emit the declerations.
  6424. */
  6425. DUK_INTERNAL_DECL duk_ret_t duk_bi_object_prototype_to_string(duk_hthread *thr);
  6426. DUK_INTERNAL_DECL duk_ret_t duk_bi_function_prototype(duk_hthread *thr);
  6427. #endif
  6428. #endif /* DUK_HOBJECT_H_INCLUDED */
  6429. /* #include duk_hcompfunc.h */
  6430. #line 1 "duk_hcompfunc.h"
  6431. /*
  6432. * Heap compiled function (ECMAScript function) representation.
  6433. *
  6434. * There is a single data buffer containing the ECMAScript function's
  6435. * bytecode, constants, and inner functions.
  6436. */
  6437. #if !defined(DUK_HCOMPFUNC_H_INCLUDED)
  6438. #define DUK_HCOMPFUNC_H_INCLUDED
  6439. /*
  6440. * Field accessor macros
  6441. */
  6442. /* XXX: casts could be improved, especially for GET/SET DATA */
  6443. #if defined(DUK_USE_HEAPPTR16)
  6444. #define DUK_HCOMPFUNC_GET_DATA(heap,h) \
  6445. ((duk_hbuffer_fixed *) (void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->data16))
  6446. #define DUK_HCOMPFUNC_SET_DATA(heap,h,v) do { \
  6447. (h)->data16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  6448. } while (0)
  6449. #define DUK_HCOMPFUNC_GET_FUNCS(heap,h) \
  6450. ((duk_hobject **) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->funcs16)))
  6451. #define DUK_HCOMPFUNC_SET_FUNCS(heap,h,v) do { \
  6452. (h)->funcs16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  6453. } while (0)
  6454. #define DUK_HCOMPFUNC_GET_BYTECODE(heap,h) \
  6455. ((duk_instr_t *) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->bytecode16)))
  6456. #define DUK_HCOMPFUNC_SET_BYTECODE(heap,h,v) do { \
  6457. (h)->bytecode16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  6458. } while (0)
  6459. #define DUK_HCOMPFUNC_GET_LEXENV(heap,h) \
  6460. ((duk_hobject *) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->lex_env16)))
  6461. #define DUK_HCOMPFUNC_SET_LEXENV(heap,h,v) do { \
  6462. (h)->lex_env16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  6463. } while (0)
  6464. #define DUK_HCOMPFUNC_GET_VARENV(heap,h) \
  6465. ((duk_hobject *) (void *) (DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (h)->var_env16)))
  6466. #define DUK_HCOMPFUNC_SET_VARENV(heap,h,v) do { \
  6467. (h)->var_env16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  6468. } while (0)
  6469. #else
  6470. #define DUK_HCOMPFUNC_GET_DATA(heap,h) ((duk_hbuffer_fixed *) (void *) (h)->data)
  6471. #define DUK_HCOMPFUNC_SET_DATA(heap,h,v) do { \
  6472. (h)->data = (duk_hbuffer *) (v); \
  6473. } while (0)
  6474. #define DUK_HCOMPFUNC_GET_FUNCS(heap,h) ((h)->funcs)
  6475. #define DUK_HCOMPFUNC_SET_FUNCS(heap,h,v) do { \
  6476. (h)->funcs = (v); \
  6477. } while (0)
  6478. #define DUK_HCOMPFUNC_GET_BYTECODE(heap,h) ((h)->bytecode)
  6479. #define DUK_HCOMPFUNC_SET_BYTECODE(heap,h,v) do { \
  6480. (h)->bytecode = (v); \
  6481. } while (0)
  6482. #define DUK_HCOMPFUNC_GET_LEXENV(heap,h) ((h)->lex_env)
  6483. #define DUK_HCOMPFUNC_SET_LEXENV(heap,h,v) do { \
  6484. (h)->lex_env = (v); \
  6485. } while (0)
  6486. #define DUK_HCOMPFUNC_GET_VARENV(heap,h) ((h)->var_env)
  6487. #define DUK_HCOMPFUNC_SET_VARENV(heap,h,v) do { \
  6488. (h)->var_env = (v); \
  6489. } while (0)
  6490. #endif
  6491. /*
  6492. * Accessor macros for function specific data areas
  6493. */
  6494. /* Note: assumes 'data' is always a fixed buffer */
  6495. #define DUK_HCOMPFUNC_GET_BUFFER_BASE(heap,h) \
  6496. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPFUNC_GET_DATA((heap), (h)))
  6497. #define DUK_HCOMPFUNC_GET_CONSTS_BASE(heap,h) \
  6498. ((duk_tval *) (void *) DUK_HCOMPFUNC_GET_BUFFER_BASE((heap), (h)))
  6499. #define DUK_HCOMPFUNC_GET_FUNCS_BASE(heap,h) \
  6500. DUK_HCOMPFUNC_GET_FUNCS((heap), (h))
  6501. #define DUK_HCOMPFUNC_GET_CODE_BASE(heap,h) \
  6502. DUK_HCOMPFUNC_GET_BYTECODE((heap), (h))
  6503. #define DUK_HCOMPFUNC_GET_CONSTS_END(heap,h) \
  6504. ((duk_tval *) (void *) DUK_HCOMPFUNC_GET_FUNCS((heap), (h)))
  6505. #define DUK_HCOMPFUNC_GET_FUNCS_END(heap,h) \
  6506. ((duk_hobject **) (void *) DUK_HCOMPFUNC_GET_BYTECODE((heap), (h)))
  6507. /* XXX: double evaluation of DUK_HCOMPFUNC_GET_DATA() */
  6508. #define DUK_HCOMPFUNC_GET_CODE_END(heap,h) \
  6509. ((duk_instr_t *) (void *) (DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), DUK_HCOMPFUNC_GET_DATA((heap), (h))) + \
  6510. DUK_HBUFFER_GET_SIZE((duk_hbuffer *) DUK_HCOMPFUNC_GET_DATA((heap), h))))
  6511. #define DUK_HCOMPFUNC_GET_CONSTS_SIZE(heap,h) \
  6512. ( \
  6513. (duk_size_t) \
  6514. ( \
  6515. ((const duk_uint8_t *) DUK_HCOMPFUNC_GET_CONSTS_END((heap), (h))) - \
  6516. ((const duk_uint8_t *) DUK_HCOMPFUNC_GET_CONSTS_BASE((heap), (h))) \
  6517. ) \
  6518. )
  6519. #define DUK_HCOMPFUNC_GET_FUNCS_SIZE(heap,h) \
  6520. ( \
  6521. (duk_size_t) \
  6522. ( \
  6523. ((const duk_uint8_t *) DUK_HCOMPFUNC_GET_FUNCS_END((heap), (h))) - \
  6524. ((const duk_uint8_t *) DUK_HCOMPFUNC_GET_FUNCS_BASE((heap), (h))) \
  6525. ) \
  6526. )
  6527. #define DUK_HCOMPFUNC_GET_CODE_SIZE(heap,h) \
  6528. ( \
  6529. (duk_size_t) \
  6530. ( \
  6531. ((const duk_uint8_t *) DUK_HCOMPFUNC_GET_CODE_END((heap),(h))) - \
  6532. ((const duk_uint8_t *) DUK_HCOMPFUNC_GET_CODE_BASE((heap),(h))) \
  6533. ) \
  6534. )
  6535. #define DUK_HCOMPFUNC_GET_CONSTS_COUNT(heap,h) \
  6536. ((duk_size_t) (DUK_HCOMPFUNC_GET_CONSTS_SIZE((heap), (h)) / sizeof(duk_tval)))
  6537. #define DUK_HCOMPFUNC_GET_FUNCS_COUNT(heap,h) \
  6538. ((duk_size_t) (DUK_HCOMPFUNC_GET_FUNCS_SIZE((heap), (h)) / sizeof(duk_hobject *)))
  6539. #define DUK_HCOMPFUNC_GET_CODE_COUNT(heap,h) \
  6540. ((duk_size_t) (DUK_HCOMPFUNC_GET_CODE_SIZE((heap), (h)) / sizeof(duk_instr_t)))
  6541. /*
  6542. * Validity assert
  6543. */
  6544. #define DUK_ASSERT_HCOMPFUNC_VALID(h) do { \
  6545. DUK_ASSERT((h) != NULL); \
  6546. } while (0)
  6547. /*
  6548. * Main struct
  6549. */
  6550. struct duk_hcompfunc {
  6551. /* shared object part */
  6552. duk_hobject obj;
  6553. /*
  6554. * Pointers to function data area for faster access. Function
  6555. * data is a buffer shared between all closures of the same
  6556. * "template" function. The data buffer is always fixed (non-
  6557. * dynamic, hence stable), with a layout as follows:
  6558. *
  6559. * constants (duk_tval)
  6560. * inner functions (duk_hobject *)
  6561. * bytecode (duk_instr_t)
  6562. *
  6563. * Note: bytecode end address can be computed from 'data' buffer
  6564. * size. It is not strictly necessary functionally, assuming
  6565. * bytecode never jumps outside its allocated area. However,
  6566. * it's a safety/robustness feature for avoiding the chance of
  6567. * executing random data as bytecode due to a compiler error.
  6568. *
  6569. * Note: values in the data buffer must be incref'd (they will
  6570. * be decref'd on release) for every compiledfunction referring
  6571. * to the 'data' element.
  6572. */
  6573. /* Data area, fixed allocation, stable data ptrs. */
  6574. #if defined(DUK_USE_HEAPPTR16)
  6575. duk_uint16_t data16;
  6576. #else
  6577. duk_hbuffer *data;
  6578. #endif
  6579. /* No need for constants pointer (= same as data).
  6580. *
  6581. * When using 16-bit packing alignment to 4 is nice. 'funcs' will be
  6582. * 4-byte aligned because 'constants' are duk_tvals. For now the
  6583. * inner function pointers are not compressed, so that 'bytecode' will
  6584. * also be 4-byte aligned.
  6585. */
  6586. #if defined(DUK_USE_HEAPPTR16)
  6587. duk_uint16_t funcs16;
  6588. duk_uint16_t bytecode16;
  6589. #else
  6590. duk_hobject **funcs;
  6591. duk_instr_t *bytecode;
  6592. #endif
  6593. /* Lexenv: lexical environment of closure, NULL for templates.
  6594. * Varenv: variable environment of closure, NULL for templates.
  6595. */
  6596. #if defined(DUK_USE_HEAPPTR16)
  6597. duk_uint16_t lex_env16;
  6598. duk_uint16_t var_env16;
  6599. #else
  6600. duk_hobject *lex_env;
  6601. duk_hobject *var_env;
  6602. #endif
  6603. /*
  6604. * 'nregs' registers are allocated on function entry, at most 'nargs'
  6605. * are initialized to arguments, and the rest to undefined. Arguments
  6606. * above 'nregs' are not mapped to registers. All registers in the
  6607. * active stack range must be initialized because they are GC reachable.
  6608. * 'nargs' is needed so that if the function is given more than 'nargs'
  6609. * arguments, the additional arguments do not 'clobber' registers
  6610. * beyond 'nregs' which must be consistently initialized to undefined.
  6611. *
  6612. * Usually there is no need to know which registers are mapped to
  6613. * local variables. Registers may be allocated to variable in any
  6614. * way (even including gaps). However, a register-variable mapping
  6615. * must be the same for the duration of the function execution and
  6616. * the register cannot be used for anything else.
  6617. *
  6618. * When looking up variables by name, the '_Varmap' map is used.
  6619. * When an activation closes, registers mapped to arguments are
  6620. * copied into the environment record based on the same map. The
  6621. * reverse map (from register to variable) is not currently needed
  6622. * at run time, except for debugging, so it is not maintained.
  6623. */
  6624. duk_uint16_t nregs; /* regs to allocate */
  6625. duk_uint16_t nargs; /* number of arguments allocated to regs */
  6626. /*
  6627. * Additional control information is placed into the object itself
  6628. * as internal properties to avoid unnecessary fields for the
  6629. * majority of functions. The compiler tries to omit internal
  6630. * control fields when possible.
  6631. *
  6632. * Function templates:
  6633. *
  6634. * {
  6635. * name: "func", // declaration, named function expressions
  6636. * fileName: <debug info for creating nice errors>
  6637. * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 },
  6638. * _Formals: [ "arg1", "arg2" ],
  6639. * _Source: "function func(arg1, arg2) { ... }",
  6640. * _Pc2line: <debug info for pc-to-line mapping>,
  6641. * }
  6642. *
  6643. * Function instances:
  6644. *
  6645. * {
  6646. * length: 2,
  6647. * prototype: { constructor: <func> },
  6648. * caller: <thrower>,
  6649. * arguments: <thrower>,
  6650. * name: "func", // declaration, named function expressions
  6651. * fileName: <debug info for creating nice errors>
  6652. * _Varmap: { "arg1": 0, "arg2": 1, "varname": 2 },
  6653. * _Formals: [ "arg1", "arg2" ],
  6654. * _Source: "function func(arg1, arg2) { ... }",
  6655. * _Pc2line: <debug info for pc-to-line mapping>,
  6656. * }
  6657. *
  6658. * More detailed description of these properties can be found
  6659. * in the documentation.
  6660. */
  6661. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  6662. /* Line number range for function. Needed during debugging to
  6663. * determine active breakpoints.
  6664. */
  6665. duk_uint32_t start_line;
  6666. duk_uint32_t end_line;
  6667. #endif
  6668. };
  6669. #endif /* DUK_HCOMPFUNC_H_INCLUDED */
  6670. /* #include duk_hnatfunc.h */
  6671. #line 1 "duk_hnatfunc.h"
  6672. /*
  6673. * Heap native function representation.
  6674. */
  6675. #if !defined(DUK_HNATFUNC_H_INCLUDED)
  6676. #define DUK_HNATFUNC_H_INCLUDED
  6677. #define DUK_HNATFUNC_NARGS_VARARGS ((duk_int16_t) -1)
  6678. #define DUK_HNATFUNC_NARGS_MAX ((duk_int16_t) 0x7fff)
  6679. struct duk_hnatfunc {
  6680. /* shared object part */
  6681. duk_hobject obj;
  6682. duk_c_function func;
  6683. duk_int16_t nargs;
  6684. duk_int16_t magic;
  6685. /* The 'magic' field allows an opaque 16-bit field to be accessed by the
  6686. * Duktape/C function. This allows, for instance, the same native function
  6687. * to be used for a set of very similar functions, with the 'magic' field
  6688. * providing the necessary non-argument flags / values to guide the behavior
  6689. * of the native function. The value is signed on purpose: it is easier to
  6690. * convert a signed value to unsigned (simply AND with 0xffff) than vice
  6691. * versa.
  6692. *
  6693. * Note: cannot place nargs/magic into the heaphdr flags, because
  6694. * duk_hobject takes almost all flags already.
  6695. */
  6696. };
  6697. #endif /* DUK_HNATFUNC_H_INCLUDED */
  6698. /* #include duk_hboundfunc.h */
  6699. #line 1 "duk_hboundfunc.h"
  6700. /*
  6701. * Bound function representation.
  6702. */
  6703. #if !defined(DUK_HBOUNDFUNC_H_INCLUDED)
  6704. #define DUK_HBOUNDFUNC_H_INCLUDED
  6705. /* Artificial limit for args length. Ensures arithmetic won't overflow
  6706. * 32 bits when combining bound functions.
  6707. */
  6708. #define DUK_HBOUNDFUNC_MAX_ARGS 0x20000000UL
  6709. #define DUK_ASSERT_HBOUNDFUNC_VALID(h) do { \
  6710. DUK_ASSERT((h) != NULL); \
  6711. DUK_ASSERT(DUK_HOBJECT_IS_BOUNDFUNC((duk_hobject *) (h))); \
  6712. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(&(h)->target) || \
  6713. (DUK_TVAL_IS_OBJECT(&(h)->target) && \
  6714. DUK_HOBJECT_IS_CALLABLE(DUK_TVAL_GET_OBJECT(&(h)->target)))); \
  6715. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(&(h)->this_binding)); \
  6716. DUK_ASSERT((h)->nargs == 0 || (h)->args != NULL); \
  6717. } while (0)
  6718. struct duk_hboundfunc {
  6719. /* Shared object part. */
  6720. duk_hobject obj;
  6721. /* Final target function, stored as duk_tval so that lightfunc can be
  6722. * represented too.
  6723. */
  6724. duk_tval target;
  6725. /* This binding. */
  6726. duk_tval this_binding;
  6727. /* Arguments to prepend. */
  6728. duk_tval *args; /* Separate allocation. */
  6729. duk_idx_t nargs;
  6730. };
  6731. #endif /* DUK_HBOUNDFUNC_H_INCLUDED */
  6732. /* #include duk_hbufobj.h */
  6733. #line 1 "duk_hbufobj.h"
  6734. /*
  6735. * Heap Buffer object representation. Used for all Buffer variants.
  6736. */
  6737. #if !defined(DUK_HBUFOBJ_H_INCLUDED)
  6738. #define DUK_HBUFOBJ_H_INCLUDED
  6739. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  6740. /* All element accessors are host endian now (driven by TypedArray spec). */
  6741. #define DUK_HBUFOBJ_ELEM_UINT8 0
  6742. #define DUK_HBUFOBJ_ELEM_UINT8CLAMPED 1
  6743. #define DUK_HBUFOBJ_ELEM_INT8 2
  6744. #define DUK_HBUFOBJ_ELEM_UINT16 3
  6745. #define DUK_HBUFOBJ_ELEM_INT16 4
  6746. #define DUK_HBUFOBJ_ELEM_UINT32 5
  6747. #define DUK_HBUFOBJ_ELEM_INT32 6
  6748. #define DUK_HBUFOBJ_ELEM_FLOAT32 7
  6749. #define DUK_HBUFOBJ_ELEM_FLOAT64 8
  6750. #define DUK_HBUFOBJ_ELEM_MAX 8
  6751. #define DUK_ASSERT_HBUFOBJ_VALID(h) do { \
  6752. DUK_ASSERT((h) != NULL); \
  6753. DUK_ASSERT((h)->shift <= 3); \
  6754. DUK_ASSERT((h)->elem_type <= DUK_HBUFOBJ_ELEM_MAX); \
  6755. DUK_ASSERT(((h)->shift == 0 && (h)->elem_type == DUK_HBUFOBJ_ELEM_UINT8) || \
  6756. ((h)->shift == 0 && (h)->elem_type == DUK_HBUFOBJ_ELEM_UINT8CLAMPED) || \
  6757. ((h)->shift == 0 && (h)->elem_type == DUK_HBUFOBJ_ELEM_INT8) || \
  6758. ((h)->shift == 1 && (h)->elem_type == DUK_HBUFOBJ_ELEM_UINT16) || \
  6759. ((h)->shift == 1 && (h)->elem_type == DUK_HBUFOBJ_ELEM_INT16) || \
  6760. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFOBJ_ELEM_UINT32) || \
  6761. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFOBJ_ELEM_INT32) || \
  6762. ((h)->shift == 2 && (h)->elem_type == DUK_HBUFOBJ_ELEM_FLOAT32) || \
  6763. ((h)->shift == 3 && (h)->elem_type == DUK_HBUFOBJ_ELEM_FLOAT64)); \
  6764. DUK_ASSERT((h)->is_typedarray == 0 || (h)->is_typedarray == 1); \
  6765. DUK_ASSERT(DUK_HOBJECT_IS_BUFOBJ((duk_hobject *) (h))); \
  6766. if ((h)->buf == NULL) { \
  6767. DUK_ASSERT((h)->offset == 0); \
  6768. DUK_ASSERT((h)->length == 0); \
  6769. } else { \
  6770. /* No assertions for offset or length; in particular, \
  6771. * it's OK for length to be longer than underlying \
  6772. * buffer. Just ensure they don't wrap when added. \
  6773. */ \
  6774. DUK_ASSERT((h)->offset + (h)->length >= (h)->offset); \
  6775. } \
  6776. } while (0)
  6777. /* Get the current data pointer (caller must ensure buf != NULL) as a
  6778. * duk_uint8_t ptr. Note that the result may be NULL if the underlying
  6779. * buffer has zero size and is not a fixed buffer.
  6780. */
  6781. #define DUK_HBUFOBJ_GET_SLICE_BASE(heap,h) \
  6782. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  6783. (((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR((heap), (h)->buf)) + (h)->offset))
  6784. /* True if slice is full, i.e. offset is zero and length covers the entire
  6785. * buffer. This status may change independently of the duk_hbufobj if
  6786. * the underlying buffer is dynamic and changes without the hbufobj
  6787. * being changed.
  6788. */
  6789. #define DUK_HBUFOBJ_FULL_SLICE(h) \
  6790. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  6791. ((h)->offset == 0 && (h)->length == DUK_HBUFFER_GET_SIZE((h)->buf)))
  6792. /* Validate that the whole slice [0,length[ is contained in the underlying
  6793. * buffer. Caller must ensure 'buf' != NULL.
  6794. */
  6795. #define DUK_HBUFOBJ_VALID_SLICE(h) \
  6796. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  6797. ((h)->offset + (h)->length <= DUK_HBUFFER_GET_SIZE((h)->buf)))
  6798. /* Validate byte read/write for virtual 'offset', i.e. check that the
  6799. * offset, taking into account h->offset, is within the underlying
  6800. * buffer size. This is a safety check which is needed to ensure
  6801. * that even a misconfigured duk_hbufobj never causes memory unsafe
  6802. * behavior (e.g. if an underlying dynamic buffer changes after being
  6803. * setup). Caller must ensure 'buf' != NULL.
  6804. */
  6805. #define DUK_HBUFOBJ_VALID_BYTEOFFSET_INCL(h,off) \
  6806. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  6807. ((h)->offset + (off) < DUK_HBUFFER_GET_SIZE((h)->buf)))
  6808. #define DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h,off) \
  6809. (DUK_ASSERT_EXPR((h) != NULL), DUK_ASSERT_EXPR((h)->buf != NULL), \
  6810. ((h)->offset + (off) <= DUK_HBUFFER_GET_SIZE((h)->buf)))
  6811. /* Clamp an input byte length (already assumed to be within the nominal
  6812. * duk_hbufobj 'length') to the current dynamic buffer limits to yield
  6813. * a byte length limit that's safe for memory accesses. This value can
  6814. * be invalidated by any side effect because it may trigger a user
  6815. * callback that resizes the underlying buffer.
  6816. */
  6817. #define DUK_HBUFOBJ_CLAMP_BYTELENGTH(h,len) \
  6818. (DUK_ASSERT_EXPR((h) != NULL), \
  6819. duk_hbufobj_clamp_bytelength((h), (len)))
  6820. /* Typed arrays have virtual indices, ArrayBuffer and DataView do not. */
  6821. #define DUK_HBUFOBJ_HAS_VIRTUAL_INDICES(h) ((h)->is_typedarray)
  6822. struct duk_hbufobj {
  6823. /* Shared object part. */
  6824. duk_hobject obj;
  6825. /* Underlying buffer (refcounted), may be NULL. */
  6826. duk_hbuffer *buf;
  6827. /* .buffer reference to an ArrayBuffer, may be NULL. */
  6828. duk_hobject *buf_prop;
  6829. /* Slice and accessor information.
  6830. *
  6831. * Because the underlying buffer may be dynamic, these may be
  6832. * invalidated by the buffer being modified so that both offset
  6833. * and length should be validated before every access. Behavior
  6834. * when the underlying buffer has changed doesn't need to be clean:
  6835. * virtual 'length' doesn't need to be affected, reads can return
  6836. * zero/NaN, and writes can be ignored.
  6837. *
  6838. * Note that a data pointer cannot be precomputed because 'buf' may
  6839. * be dynamic and its pointer unstable.
  6840. */
  6841. duk_uint_t offset; /* byte offset to buf */
  6842. duk_uint_t length; /* byte index limit for element access, exclusive */
  6843. duk_uint8_t shift; /* element size shift:
  6844. * 0 = u8/i8
  6845. * 1 = u16/i16
  6846. * 2 = u32/i32/float
  6847. * 3 = double
  6848. */
  6849. duk_uint8_t elem_type; /* element type */
  6850. duk_uint8_t is_typedarray;
  6851. };
  6852. DUK_INTERNAL_DECL duk_uint_t duk_hbufobj_clamp_bytelength(duk_hbufobj *h_bufobj, duk_uint_t len);
  6853. DUK_INTERNAL_DECL void duk_hbufobj_push_uint8array_from_plain(duk_hthread *thr, duk_hbuffer *h_buf);
  6854. DUK_INTERNAL_DECL void duk_hbufobj_push_validated_read(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size);
  6855. DUK_INTERNAL_DECL void duk_hbufobj_validated_write(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size);
  6856. DUK_INTERNAL_DECL void duk_hbufobj_promote_plain(duk_hthread *thr, duk_idx_t idx);
  6857. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  6858. #endif /* DUK_HBUFOBJ_H_INCLUDED */
  6859. /* #include duk_hthread.h */
  6860. #line 1 "duk_hthread.h"
  6861. /*
  6862. * Heap thread object representation.
  6863. *
  6864. * duk_hthread is also the 'context' for public API functions via a
  6865. * different typedef. Most API calls operate on the topmost frame
  6866. * of the value stack only.
  6867. */
  6868. #if !defined(DUK_HTHREAD_H_INCLUDED)
  6869. #define DUK_HTHREAD_H_INCLUDED
  6870. /*
  6871. * Stack constants
  6872. */
  6873. /* Initial valstack size, roughly 0.7kiB. */
  6874. #define DUK_VALSTACK_INITIAL_SIZE 96U
  6875. /* Internal extra elements assumed on function entry, always added to
  6876. * user-defined 'extra' for e.g. the duk_check_stack() call.
  6877. */
  6878. #define DUK_VALSTACK_INTERNAL_EXTRA 32U
  6879. /* Number of elements guaranteed to be user accessible (in addition to call
  6880. * arguments) on Duktape/C function entry. This is the major public API
  6881. * commitment.
  6882. */
  6883. #define DUK_VALSTACK_API_ENTRY_MINIMUM DUK_API_ENTRY_STACK
  6884. /*
  6885. * Activation defines
  6886. */
  6887. #define DUK_ACT_FLAG_STRICT (1U << 0) /* function executes in strict mode */
  6888. #define DUK_ACT_FLAG_TAILCALLED (1U << 1) /* activation has tail called one or more times */
  6889. #define DUK_ACT_FLAG_CONSTRUCT (1U << 2) /* function executes as a constructor (called via "new") */
  6890. #define DUK_ACT_FLAG_PREVENT_YIELD (1U << 3) /* activation prevents yield (native call or "new") */
  6891. #define DUK_ACT_FLAG_DIRECT_EVAL (1U << 4) /* activation is a direct eval call */
  6892. #define DUK_ACT_FLAG_CONSTRUCT_PROXY (1U << 5) /* activation is for Proxy 'construct' call, special return value handling */
  6893. #define DUK_ACT_FLAG_BREAKPOINT_ACTIVE (1U << 6) /* activation has active breakpoint(s) */
  6894. #define DUK_ACT_GET_FUNC(act) ((act)->func)
  6895. /*
  6896. * Flags for __FILE__ / __LINE__ registered into tracedata
  6897. */
  6898. #define DUK_TB_FLAG_NOBLAME_FILELINE (1U << 0) /* don't report __FILE__ / __LINE__ as fileName/lineNumber */
  6899. /*
  6900. * Catcher defines
  6901. */
  6902. /* XXX: remove catcher type entirely */
  6903. /* flags field: LLLLLLFT, L = label (24 bits), F = flags (4 bits), T = type (4 bits) */
  6904. #define DUK_CAT_TYPE_MASK 0x0000000fUL
  6905. #define DUK_CAT_TYPE_BITS 4
  6906. #define DUK_CAT_LABEL_MASK 0xffffff00UL
  6907. #define DUK_CAT_LABEL_BITS 24
  6908. #define DUK_CAT_LABEL_SHIFT 8
  6909. #define DUK_CAT_FLAG_CATCH_ENABLED (1U << 4) /* catch part will catch */
  6910. #define DUK_CAT_FLAG_FINALLY_ENABLED (1U << 5) /* finally part will catch */
  6911. #define DUK_CAT_FLAG_CATCH_BINDING_ENABLED (1U << 6) /* request to create catch binding */
  6912. #define DUK_CAT_FLAG_LEXENV_ACTIVE (1U << 7) /* catch or with binding is currently active */
  6913. #define DUK_CAT_TYPE_UNKNOWN 0
  6914. #define DUK_CAT_TYPE_TCF 1
  6915. #define DUK_CAT_TYPE_LABEL 2
  6916. #define DUK_CAT_GET_TYPE(c) ((c)->flags & DUK_CAT_TYPE_MASK)
  6917. #define DUK_CAT_GET_LABEL(c) (((c)->flags & DUK_CAT_LABEL_MASK) >> DUK_CAT_LABEL_SHIFT)
  6918. #define DUK_CAT_HAS_CATCH_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_ENABLED)
  6919. #define DUK_CAT_HAS_FINALLY_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_FINALLY_ENABLED)
  6920. #define DUK_CAT_HAS_CATCH_BINDING_ENABLED(c) ((c)->flags & DUK_CAT_FLAG_CATCH_BINDING_ENABLED)
  6921. #define DUK_CAT_HAS_LEXENV_ACTIVE(c) ((c)->flags & DUK_CAT_FLAG_LEXENV_ACTIVE)
  6922. #define DUK_CAT_SET_CATCH_ENABLED(c) do { \
  6923. (c)->flags |= DUK_CAT_FLAG_CATCH_ENABLED; \
  6924. } while (0)
  6925. #define DUK_CAT_SET_FINALLY_ENABLED(c) do { \
  6926. (c)->flags |= DUK_CAT_FLAG_FINALLY_ENABLED; \
  6927. } while (0)
  6928. #define DUK_CAT_SET_CATCH_BINDING_ENABLED(c) do { \
  6929. (c)->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \
  6930. } while (0)
  6931. #define DUK_CAT_SET_LEXENV_ACTIVE(c) do { \
  6932. (c)->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE; \
  6933. } while (0)
  6934. #define DUK_CAT_CLEAR_CATCH_ENABLED(c) do { \
  6935. (c)->flags &= ~DUK_CAT_FLAG_CATCH_ENABLED; \
  6936. } while (0)
  6937. #define DUK_CAT_CLEAR_FINALLY_ENABLED(c) do { \
  6938. (c)->flags &= ~DUK_CAT_FLAG_FINALLY_ENABLED; \
  6939. } while (0)
  6940. #define DUK_CAT_CLEAR_CATCH_BINDING_ENABLED(c) do { \
  6941. (c)->flags &= ~DUK_CAT_FLAG_CATCH_BINDING_ENABLED; \
  6942. } while (0)
  6943. #define DUK_CAT_CLEAR_LEXENV_ACTIVE(c) do { \
  6944. (c)->flags &= ~DUK_CAT_FLAG_LEXENV_ACTIVE; \
  6945. } while (0)
  6946. /*
  6947. * Thread defines
  6948. */
  6949. #if defined(DUK_USE_ROM_STRINGS)
  6950. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  6951. ((duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_stridx[(idx)]))
  6952. #else /* DUK_USE_ROM_STRINGS */
  6953. #if defined(DUK_USE_HEAPPTR16)
  6954. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  6955. ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((thr)->heap->heap_udata, (thr)->strs16[(idx)]))
  6956. #else
  6957. #define DUK_HTHREAD_GET_STRING(thr,idx) \
  6958. ((thr)->strs[(idx)])
  6959. #endif
  6960. #endif /* DUK_USE_ROM_STRINGS */
  6961. /* values for the state field */
  6962. #define DUK_HTHREAD_STATE_INACTIVE 1 /* thread not currently running */
  6963. #define DUK_HTHREAD_STATE_RUNNING 2 /* thread currently running (only one at a time) */
  6964. #define DUK_HTHREAD_STATE_RESUMED 3 /* thread resumed another thread (active but not running) */
  6965. #define DUK_HTHREAD_STATE_YIELDED 4 /* thread has yielded */
  6966. #define DUK_HTHREAD_STATE_TERMINATED 5 /* thread has terminated */
  6967. /* Executor interrupt default interval when nothing else requires a
  6968. * smaller value. The default interval must be small enough to allow
  6969. * for reasonable execution timeout checking but large enough to keep
  6970. * impact on execution performance low.
  6971. */
  6972. #if defined(DUK_USE_INTERRUPT_COUNTER)
  6973. #define DUK_HTHREAD_INTCTR_DEFAULT (256L * 1024L)
  6974. #endif
  6975. /*
  6976. * Assert context is valid: non-NULL pointer, fields look sane.
  6977. *
  6978. * This is used by public API call entrypoints to catch invalid 'ctx' pointers
  6979. * as early as possible; invalid 'ctx' pointers cause very odd and difficult to
  6980. * diagnose behavior so it's worth checking even when the check is not 100%.
  6981. */
  6982. /* Assertions for internals. */
  6983. #define DUK_ASSERT_HTHREAD_VALID(thr) do { \
  6984. DUK_ASSERT((thr) != NULL); \
  6985. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) (thr)) == DUK_HTYPE_OBJECT); \
  6986. DUK_ASSERT(DUK_HOBJECT_IS_THREAD((duk_hobject *) (thr))); \
  6987. DUK_ASSERT((thr)->unused1 == 0); \
  6988. DUK_ASSERT((thr)->unused2 == 0); \
  6989. } while (0)
  6990. /* Assertions for public API calls; a bit stronger. */
  6991. #define DUK_ASSERT_CTX_VALID(thr) do { \
  6992. DUK_ASSERT((thr) != NULL); \
  6993. DUK_ASSERT_HTHREAD_VALID((thr)); \
  6994. DUK_ASSERT((thr)->valstack != NULL); \
  6995. DUK_ASSERT((thr)->valstack_bottom != NULL); \
  6996. DUK_ASSERT((thr)->valstack_top != NULL); \
  6997. DUK_ASSERT((thr)->valstack_end != NULL); \
  6998. DUK_ASSERT((thr)->valstack_alloc_end != NULL); \
  6999. DUK_ASSERT((thr)->valstack_alloc_end >= (thr)->valstack); \
  7000. DUK_ASSERT((thr)->valstack_end >= (thr)->valstack); \
  7001. DUK_ASSERT((thr)->valstack_top >= (thr)->valstack); \
  7002. DUK_ASSERT((thr)->valstack_top >= (thr)->valstack_bottom); \
  7003. DUK_ASSERT((thr)->valstack_end >= (thr)->valstack_top); \
  7004. DUK_ASSERT((thr)->valstack_alloc_end >= (thr)->valstack_end); \
  7005. } while (0)
  7006. /* Assertions for API call entry specifically. Checks 'ctx' but also may
  7007. * check internal state (e.g. not in a debugger transport callback).
  7008. */
  7009. #define DUK_ASSERT_API_ENTRY(thr) do { \
  7010. DUK_ASSERT_CTX_VALID((thr)); \
  7011. DUK_ASSERT((thr)->heap != NULL); \
  7012. DUK_ASSERT((thr)->heap->dbg_calling_transport == 0); \
  7013. } while (0)
  7014. /*
  7015. * Assertion helpers.
  7016. */
  7017. #define DUK_ASSERT_STRIDX_VALID(val) \
  7018. DUK_ASSERT((duk_uint_t) (val) < DUK_HEAP_NUM_STRINGS)
  7019. #define DUK_ASSERT_BIDX_VALID(val) \
  7020. DUK_ASSERT((duk_uint_t) (val) < DUK_NUM_BUILTINS)
  7021. /*
  7022. * Misc
  7023. */
  7024. /* Fast access to 'this' binding. Assumes there's a call in progress. */
  7025. #define DUK_HTHREAD_THIS_PTR(thr) \
  7026. (DUK_ASSERT_EXPR((thr) != NULL), \
  7027. DUK_ASSERT_EXPR((thr)->valstack_bottom > (thr)->valstack), \
  7028. (thr)->valstack_bottom - 1)
  7029. /*
  7030. * Struct defines
  7031. */
  7032. /* Fields are ordered for alignment/packing. */
  7033. struct duk_activation {
  7034. duk_tval tv_func; /* borrowed: full duk_tval for function being executed; for lightfuncs */
  7035. duk_hobject *func; /* borrowed: function being executed; for bound function calls, this is the final, real function, NULL for lightfuncs */
  7036. duk_activation *parent; /* previous (parent) activation (or NULL if none) */
  7037. duk_hobject *var_env; /* current variable environment (may be NULL if delayed) */
  7038. duk_hobject *lex_env; /* current lexical environment (may be NULL if delayed) */
  7039. duk_catcher *cat; /* current catcher (or NULL) */
  7040. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  7041. /* Previous value of 'func' caller, restored when unwound. Only in use
  7042. * when 'func' is non-strict.
  7043. */
  7044. duk_hobject *prev_caller;
  7045. #endif
  7046. duk_instr_t *curr_pc; /* next instruction to execute (points to 'func' bytecode, stable pointer), NULL for native calls */
  7047. /* bottom_byteoff and retval_byteoff are only used for book-keeping
  7048. * of ECMAScript-initiated calls, to allow returning to an ECMAScript
  7049. * function properly.
  7050. */
  7051. /* Bottom of valstack for this activation, used to reset
  7052. * valstack_bottom on return; offset is absolute. There's
  7053. * no need to track 'top' because native call handling deals
  7054. * with that using locals, and for ECMAScript returns 'nregs'
  7055. * indicates the necessary top.
  7056. */
  7057. duk_size_t bottom_byteoff;
  7058. /* Return value when returning to this activation (points to caller
  7059. * reg, not callee reg); offset is absolute (only set if activation is
  7060. * not topmost).
  7061. *
  7062. * Note: bottom_byteoff is always set, while retval_byteoff is only
  7063. * applicable for activations below the topmost one. Currently
  7064. * retval_byteoff for the topmost activation is considered garbage
  7065. * (and it not initialized on entry or cleared on return; may contain
  7066. * previous or garbage values).
  7067. */
  7068. duk_size_t retval_byteoff;
  7069. /* Current 'this' binding is the value just below bottom.
  7070. * Previously, 'this' binding was handled with an index to the
  7071. * (calling) valstack. This works for everything except tail
  7072. * calls, which must not "accumulate" valstack temps.
  7073. */
  7074. /* Value stack reserve (valstack_end) byte offset to be restored
  7075. * when returning to this activation. Only used by the bytecode
  7076. * executor.
  7077. */
  7078. duk_size_t reserve_byteoff;
  7079. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  7080. duk_uint32_t prev_line; /* needed for stepping */
  7081. #endif
  7082. duk_small_uint_t flags;
  7083. };
  7084. struct duk_catcher {
  7085. duk_catcher *parent; /* previous (parent) catcher (or NULL if none) */
  7086. duk_hstring *h_varname; /* borrowed reference to catch variable name (or NULL if none) */
  7087. /* (reference is valid as long activation exists) */
  7088. duk_instr_t *pc_base; /* resume execution from pc_base or pc_base+1 (points to 'func' bytecode, stable pointer) */
  7089. duk_size_t idx_base; /* idx_base and idx_base+1 get completion value and type */
  7090. duk_uint32_t flags; /* type and control flags, label number */
  7091. /* XXX: could pack 'flags' and 'idx_base' to same value in practice,
  7092. * on 32-bit targets this would make duk_catcher 16 bytes.
  7093. */
  7094. };
  7095. struct duk_hthread {
  7096. /* Shared object part */
  7097. duk_hobject obj;
  7098. /* Pointer to bytecode executor's 'curr_pc' variable. Used to copy
  7099. * the current PC back into the topmost activation when activation
  7100. * state is about to change (or "syncing" is otherwise needed). This
  7101. * is rather awkward but important for performance, see execution.rst.
  7102. */
  7103. duk_instr_t **ptr_curr_pc;
  7104. /* Backpointers. */
  7105. duk_heap *heap;
  7106. /* Current strictness flag: affects API calls. */
  7107. duk_uint8_t strict;
  7108. /* Thread state. */
  7109. duk_uint8_t state;
  7110. duk_uint8_t unused1;
  7111. duk_uint8_t unused2;
  7112. /* XXX: Valstack and callstack are currently assumed to have non-NULL
  7113. * pointers. Relaxing this would not lead to big benefits (except
  7114. * perhaps for terminated threads).
  7115. */
  7116. /* Value stack: these are expressed as pointers for faster stack
  7117. * manipulation. [valstack,valstack_top[ is GC-reachable,
  7118. * [valstack_top,valstack_alloc_end[ is not GC-reachable but kept
  7119. * initialized as 'undefined'. [valstack,valstack_end[ is the
  7120. * guaranteed/reserved space and the valstack cannot be resized to
  7121. * a smaller size. [valstack_end,valstack_alloc_end[ is currently
  7122. * allocated slack that can be used to grow the current guaranteed
  7123. * space but may be shrunk away without notice.
  7124. *
  7125. *
  7126. * <----------------------- guaranteed --->
  7127. * <---- slack --->
  7128. * <--- frame --->
  7129. * .-------------+=============+----------+--------------.
  7130. * |xxxxxxxxxxxxx|yyyyyyyyyyyyy|uuuuuuuuuu|uuuuuuuuuuuuuu|
  7131. * `-------------+=============+----------+--------------'
  7132. *
  7133. * ^ ^ ^ ^ ^
  7134. * | | | | |
  7135. * valstack bottom top end alloc_end
  7136. *
  7137. * xxx = arbitrary values, below current frame
  7138. * yyy = arbitrary values, inside current frame
  7139. * uuu = outside active value stack, initialized to 'undefined'
  7140. */
  7141. duk_tval *valstack; /* start of valstack allocation */
  7142. duk_tval *valstack_end; /* end of valstack reservation/guarantee (exclusive) */
  7143. duk_tval *valstack_alloc_end; /* end of valstack allocation */
  7144. duk_tval *valstack_bottom; /* bottom of current frame */
  7145. duk_tval *valstack_top; /* top of current frame (exclusive) */
  7146. /* Call stack, represented as a linked list starting from the current
  7147. * activation (or NULL if nothing is active).
  7148. */
  7149. duk_activation *callstack_curr; /* current activation (or NULL if none) */
  7150. duk_size_t callstack_top; /* number of activation records in callstack (0 if none) */
  7151. duk_size_t callstack_preventcount; /* number of activation records in callstack preventing a yield */
  7152. /* Yield/resume book-keeping. */
  7153. duk_hthread *resumer; /* who resumed us (if any) */
  7154. /* Current compiler state (if any), used for augmenting SyntaxErrors. */
  7155. duk_compiler_ctx *compile_ctx;
  7156. #if defined(DUK_USE_INTERRUPT_COUNTER)
  7157. /* Interrupt counter for triggering a slow path check for execution
  7158. * timeout, debugger interaction such as breakpoints, etc. The value
  7159. * is valid for the current running thread, and both the init and
  7160. * counter values are copied whenever a thread switch occurs. It's
  7161. * important for the counter to be conveniently accessible for the
  7162. * bytecode executor inner loop for performance reasons.
  7163. */
  7164. duk_int_t interrupt_counter; /* countdown state */
  7165. duk_int_t interrupt_init; /* start value for current countdown */
  7166. #endif
  7167. /* Builtin-objects; may or may not be shared with other threads,
  7168. * threads existing in different "compartments" will have different
  7169. * built-ins. Must be stored on a per-thread basis because there
  7170. * is no intermediate structure for a thread group / compartment.
  7171. * This takes quite a lot of space, currently 43x4 = 172 bytes on
  7172. * 32-bit platforms.
  7173. *
  7174. * In some cases the builtins array could be ROM based, but it's
  7175. * sometimes edited (e.g. for sandboxing) so it's better to keep
  7176. * this array in RAM.
  7177. */
  7178. duk_hobject *builtins[DUK_NUM_BUILTINS];
  7179. /* Convenience copies from heap/vm for faster access. */
  7180. #if defined(DUK_USE_ROM_STRINGS)
  7181. /* No field needed when strings are in ROM. */
  7182. #else
  7183. #if defined(DUK_USE_HEAPPTR16)
  7184. duk_uint16_t *strs16;
  7185. #else
  7186. duk_hstring **strs;
  7187. #endif
  7188. #endif
  7189. };
  7190. /*
  7191. * Prototypes
  7192. */
  7193. DUK_INTERNAL_DECL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to);
  7194. DUK_INTERNAL_DECL void duk_hthread_create_builtin_objects(duk_hthread *thr);
  7195. DUK_INTERNAL_DECL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr);
  7196. DUK_INTERNAL_DECL void duk_hthread_terminate(duk_hthread *thr);
  7197. DUK_INTERNAL_DECL duk_activation *duk_hthread_activation_alloc(duk_hthread *thr);
  7198. DUK_INTERNAL_DECL void duk_hthread_activation_free(duk_hthread *thr, duk_activation *act);
  7199. DUK_INTERNAL_DECL void duk_hthread_activation_unwind_norz(duk_hthread *thr);
  7200. DUK_INTERNAL_DECL void duk_hthread_activation_unwind_reuse_norz(duk_hthread *thr);
  7201. DUK_INTERNAL_DECL duk_activation *duk_hthread_get_activation_for_level(duk_hthread *thr, duk_int_t level);
  7202. DUK_INTERNAL_DECL duk_catcher *duk_hthread_catcher_alloc(duk_hthread *thr);
  7203. DUK_INTERNAL_DECL void duk_hthread_catcher_free(duk_hthread *thr, duk_catcher *cat);
  7204. DUK_INTERNAL_DECL void duk_hthread_catcher_unwind_norz(duk_hthread *thr, duk_activation *act);
  7205. DUK_INTERNAL_DECL void duk_hthread_catcher_unwind_nolexenv_norz(duk_hthread *thr, duk_activation *act);
  7206. #if defined(DUK_USE_FINALIZER_TORTURE)
  7207. DUK_INTERNAL_DECL void duk_hthread_valstack_torture_realloc(duk_hthread *thr);
  7208. #endif
  7209. DUK_INTERNAL_DECL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  7210. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  7211. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_curr_pc(duk_hthread *thr, duk_activation *act);
  7212. #endif
  7213. DUK_INTERNAL_DECL duk_uint_fast32_t duk_hthread_get_act_prev_pc(duk_hthread *thr, duk_activation *act);
  7214. DUK_INTERNAL_DECL void duk_hthread_sync_currpc(duk_hthread *thr);
  7215. DUK_INTERNAL_DECL void duk_hthread_sync_and_null_currpc(duk_hthread *thr);
  7216. #endif /* DUK_HTHREAD_H_INCLUDED */
  7217. /* #include duk_harray.h */
  7218. #line 1 "duk_harray.h"
  7219. /*
  7220. * Array object representation, used for actual Array instances.
  7221. *
  7222. * All objects with the exotic array behavior (which must coincide with having
  7223. * internal class array) MUST be duk_harrays. No other object can be a
  7224. * duk_harray. However, duk_harrays may not always have an array part.
  7225. */
  7226. #if !defined(DUK_HARRAY_H_INCLUDED)
  7227. #define DUK_HARRAY_H_INCLUDED
  7228. #define DUK_ASSERT_HARRAY_VALID(h) do { \
  7229. DUK_ASSERT((h) != NULL); \
  7230. DUK_ASSERT(DUK_HOBJECT_IS_ARRAY((duk_hobject *) (h))); \
  7231. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY((duk_hobject *) (h))); \
  7232. } while (0)
  7233. #define DUK_HARRAY_LENGTH_WRITABLE(h) (!(h)->length_nonwritable)
  7234. #define DUK_HARRAY_LENGTH_NONWRITABLE(h) ((h)->length_nonwritable)
  7235. #define DUK_HARRAY_SET_LENGTH_WRITABLE(h) do { (h)->length_nonwritable = 0; } while (0)
  7236. #define DUK_HARRAY_SET_LENGTH_NONWRITABLE(h) do { (h)->length_nonwritable = 1; } while (0)
  7237. struct duk_harray {
  7238. /* Shared object part. */
  7239. duk_hobject obj;
  7240. /* Array .length.
  7241. *
  7242. * At present Array .length may be smaller, equal, or even larger
  7243. * than the allocated underlying array part. Fast path code must
  7244. * always take this into account carefully.
  7245. */
  7246. duk_uint32_t length;
  7247. /* Array .length property attributes. The property is always
  7248. * non-enumerable and non-configurable. It's initially writable
  7249. * but per Object.defineProperty() rules it can be made non-writable
  7250. * even if it is non-configurable. Thus we need to track the
  7251. * writability explicitly.
  7252. *
  7253. * XXX: this field to be eliminated and moved into duk_hobject
  7254. * flags field to save space.
  7255. */
  7256. duk_bool_t length_nonwritable;
  7257. };
  7258. #endif /* DUK_HARRAY_H_INCLUDED */
  7259. /* #include duk_henv.h */
  7260. #line 1 "duk_henv.h"
  7261. /*
  7262. * Environment object representation.
  7263. */
  7264. #if !defined(DUK_HENV_H_INCLUDED)
  7265. #define DUK_HENV_H_INCLUDED
  7266. #define DUK_ASSERT_HDECENV_VALID(h) do { \
  7267. DUK_ASSERT((h) != NULL); \
  7268. DUK_ASSERT(DUK_HOBJECT_IS_DECENV((duk_hobject *) (h))); \
  7269. DUK_ASSERT((h)->thread == NULL || (h)->varmap != NULL); \
  7270. } while (0)
  7271. #define DUK_ASSERT_HOBJENV_VALID(h) do { \
  7272. DUK_ASSERT((h) != NULL); \
  7273. DUK_ASSERT(DUK_HOBJECT_IS_OBJENV((duk_hobject *) (h))); \
  7274. DUK_ASSERT((h)->target != NULL); \
  7275. DUK_ASSERT((h)->has_this == 0 || (h)->has_this == 1); \
  7276. } while (0)
  7277. struct duk_hdecenv {
  7278. /* Shared object part. */
  7279. duk_hobject obj;
  7280. /* These control variables provide enough information to access live
  7281. * variables for a closure that is still open. If thread == NULL,
  7282. * the record is closed and the identifiers are in the property table.
  7283. */
  7284. duk_hthread *thread;
  7285. duk_hobject *varmap;
  7286. duk_size_t regbase_byteoff;
  7287. };
  7288. struct duk_hobjenv {
  7289. /* Shared object part. */
  7290. duk_hobject obj;
  7291. /* Target object and 'this' binding for object binding. */
  7292. duk_hobject *target;
  7293. /* The 'target' object is used as a this binding in only some object
  7294. * environments. For example, the global environment does not provide
  7295. * a this binding, but a with statement does.
  7296. */
  7297. duk_bool_t has_this;
  7298. };
  7299. #endif /* DUK_HENV_H_INCLUDED */
  7300. /* #include duk_hbuffer.h */
  7301. #line 1 "duk_hbuffer.h"
  7302. /*
  7303. * Heap buffer representation.
  7304. *
  7305. * Heap allocated user data buffer which is either:
  7306. *
  7307. * 1. A fixed size buffer (data follows header statically)
  7308. * 2. A dynamic size buffer (data pointer follows header)
  7309. *
  7310. * The data pointer for a variable size buffer of zero size may be NULL.
  7311. */
  7312. #if !defined(DUK_HBUFFER_H_INCLUDED)
  7313. #define DUK_HBUFFER_H_INCLUDED
  7314. /*
  7315. * Flags
  7316. *
  7317. * Fixed buffer: 0
  7318. * Dynamic buffer: DUK_HBUFFER_FLAG_DYNAMIC
  7319. * External buffer: DUK_HBUFFER_FLAG_DYNAMIC | DUK_HBUFFER_FLAG_EXTERNAL
  7320. */
  7321. #define DUK_HBUFFER_FLAG_DYNAMIC DUK_HEAPHDR_USER_FLAG(0) /* buffer is behind a pointer, dynamic or external */
  7322. #define DUK_HBUFFER_FLAG_EXTERNAL DUK_HEAPHDR_USER_FLAG(1) /* buffer pointer is to an externally allocated buffer */
  7323. #define DUK_HBUFFER_HAS_DYNAMIC(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  7324. #define DUK_HBUFFER_HAS_EXTERNAL(x) DUK_HEAPHDR_CHECK_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL)
  7325. #define DUK_HBUFFER_SET_DYNAMIC(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  7326. #define DUK_HBUFFER_SET_EXTERNAL(x) DUK_HEAPHDR_SET_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL)
  7327. #define DUK_HBUFFER_CLEAR_DYNAMIC(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_DYNAMIC)
  7328. #define DUK_HBUFFER_CLEAR_EXTERNAL(x) DUK_HEAPHDR_CLEAR_FLAG_BITS(&(x)->hdr, DUK_HBUFFER_FLAG_EXTERNAL)
  7329. /*
  7330. * Misc defines
  7331. */
  7332. /* Impose a maximum buffer length for now. Restricted artificially to
  7333. * ensure resize computations or adding a heap header length won't
  7334. * overflow size_t and that a signed duk_int_t can hold a buffer
  7335. * length. The limit should be synchronized with DUK_HSTRING_MAX_BYTELEN.
  7336. */
  7337. #if defined(DUK_USE_BUFLEN16)
  7338. #define DUK_HBUFFER_MAX_BYTELEN (0x0000ffffUL)
  7339. #else
  7340. /* Intentionally not 0x7fffffffUL; at least JSON code expects that
  7341. * 2*len + 2 fits in 32 bits.
  7342. */
  7343. #define DUK_HBUFFER_MAX_BYTELEN (0x7ffffffeUL)
  7344. #endif
  7345. /*
  7346. * Field access
  7347. */
  7348. #if defined(DUK_USE_BUFLEN16)
  7349. /* size stored in duk_heaphdr unused flag bits */
  7350. #define DUK_HBUFFER_GET_SIZE(x) ((x)->hdr.h_flags >> 16)
  7351. #define DUK_HBUFFER_SET_SIZE(x,v) do { \
  7352. duk_size_t duk__v; \
  7353. duk__v = (v); \
  7354. DUK_ASSERT(duk__v <= 0xffffUL); \
  7355. (x)->hdr.h_flags = ((x)->hdr.h_flags & 0x0000ffffUL) | (((duk_uint32_t) duk__v) << 16); \
  7356. } while (0)
  7357. #define DUK_HBUFFER_ADD_SIZE(x,dv) do { \
  7358. (x)->hdr.h_flags += ((dv) << 16); \
  7359. } while (0)
  7360. #define DUK_HBUFFER_SUB_SIZE(x,dv) do { \
  7361. (x)->hdr.h_flags -= ((dv) << 16); \
  7362. } while (0)
  7363. #else
  7364. #define DUK_HBUFFER_GET_SIZE(x) (((duk_hbuffer *) (x))->size)
  7365. #define DUK_HBUFFER_SET_SIZE(x,v) do { \
  7366. ((duk_hbuffer *) (x))->size = (v); \
  7367. } while (0)
  7368. #define DUK_HBUFFER_ADD_SIZE(x,dv) do { \
  7369. (x)->size += (dv); \
  7370. } while (0)
  7371. #define DUK_HBUFFER_SUB_SIZE(x,dv) do { \
  7372. (x)->size -= (dv); \
  7373. } while (0)
  7374. #endif
  7375. #define DUK_HBUFFER_FIXED_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  7376. #define DUK_HBUFFER_FIXED_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x))
  7377. #define DUK_HBUFFER_DYNAMIC_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  7378. #define DUK_HBUFFER_DYNAMIC_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v))
  7379. #define DUK_HBUFFER_DYNAMIC_ADD_SIZE(x,dv) DUK_HBUFFER_ADD_SIZE((duk_hbuffer *) (x), (dv))
  7380. #define DUK_HBUFFER_DYNAMIC_SUB_SIZE(x,dv) DUK_HBUFFER_SUB_SIZE((duk_hbuffer *) (x), (dv))
  7381. #define DUK_HBUFFER_EXTERNAL_GET_SIZE(x) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) (x))
  7382. #define DUK_HBUFFER_EXTERNAL_SET_SIZE(x,v) DUK_HBUFFER_SET_SIZE((duk_hbuffer *) (x), (v))
  7383. #define DUK_HBUFFER_FIXED_GET_DATA_PTR(heap,x) ((duk_uint8_t *) (((duk_hbuffer_fixed *) (void *) (x)) + 1))
  7384. #if defined(DUK_USE_HEAPPTR16)
  7385. #define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) \
  7386. ((void *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, ((duk_heaphdr *) (x))->h_extra16))
  7387. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \
  7388. ((duk_heaphdr *) (x))->h_extra16 = DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (void *) (v)); \
  7389. } while (0)
  7390. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \
  7391. ((duk_heaphdr *) (x))->h_extra16 = 0; /* assume 0 <=> NULL */ \
  7392. } while (0)
  7393. #else
  7394. #define DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap,x) ((x)->curr_alloc)
  7395. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap,x,v) do { \
  7396. (x)->curr_alloc = (void *) (v); \
  7397. } while (0)
  7398. #define DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(heap,x) do { \
  7399. (x)->curr_alloc = (void *) NULL; \
  7400. } while (0)
  7401. #endif
  7402. /* No pointer compression because pointer is potentially outside of
  7403. * Duktape heap.
  7404. */
  7405. #if defined(DUK_USE_HEAPPTR16)
  7406. #define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \
  7407. ((void *) (x)->curr_alloc)
  7408. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \
  7409. (x)->curr_alloc = (void *) (v); \
  7410. } while (0)
  7411. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \
  7412. (x)->curr_alloc = (void *) NULL; \
  7413. } while (0)
  7414. #else
  7415. #define DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap,x) \
  7416. ((void *) (x)->curr_alloc)
  7417. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap,x,v) do { \
  7418. (x)->curr_alloc = (void *) (v); \
  7419. } while (0)
  7420. #define DUK_HBUFFER_EXTERNAL_SET_DATA_PTR_NULL(heap,x) do { \
  7421. (x)->curr_alloc = (void *) NULL; \
  7422. } while (0)
  7423. #endif
  7424. /* Get a pointer to the current buffer contents (matching current allocation
  7425. * size). May be NULL for zero size dynamic/external buffer.
  7426. */
  7427. #if defined(DUK_USE_HEAPPTR16)
  7428. #define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \
  7429. DUK_HBUFFER_HAS_DYNAMIC((x)) ? \
  7430. ( \
  7431. DUK_HBUFFER_HAS_EXTERNAL((x)) ? \
  7432. DUK_HBUFFER_EXTERNAL_GET_DATA_PTR((heap), (duk_hbuffer_external *) (x)) : \
  7433. DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) \
  7434. ) : \
  7435. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (void *) (x)) \
  7436. )
  7437. #else
  7438. /* Without heap pointer compression duk_hbuffer_dynamic and duk_hbuffer_external
  7439. * have the same layout so checking for fixed vs. dynamic (or external) is enough.
  7440. */
  7441. #define DUK_HBUFFER_GET_DATA_PTR(heap,x) ( \
  7442. DUK_HBUFFER_HAS_DYNAMIC((x)) ? \
  7443. DUK_HBUFFER_DYNAMIC_GET_DATA_PTR((heap), (duk_hbuffer_dynamic *) (x)) : \
  7444. DUK_HBUFFER_FIXED_GET_DATA_PTR((heap), (duk_hbuffer_fixed *) (void *) (x)) \
  7445. )
  7446. #endif
  7447. /*
  7448. * Structs
  7449. */
  7450. /* Shared prefix for all buffer types. */
  7451. struct duk_hbuffer {
  7452. duk_heaphdr hdr;
  7453. /* It's not strictly necessary to track the current size, but
  7454. * it is useful for writing robust native code.
  7455. */
  7456. /* Current size. */
  7457. #if defined(DUK_USE_BUFLEN16)
  7458. /* Stored in duk_heaphdr unused flags. */
  7459. #else
  7460. duk_size_t size;
  7461. #endif
  7462. /*
  7463. * Data following the header depends on the DUK_HBUFFER_FLAG_DYNAMIC
  7464. * flag.
  7465. *
  7466. * If the flag is clear (the buffer is a fixed size one), the buffer
  7467. * data follows the header directly, consisting of 'size' bytes.
  7468. *
  7469. * If the flag is set, the actual buffer is allocated separately, and
  7470. * a few control fields follow the header. Specifically:
  7471. *
  7472. * - a "void *" pointing to the current allocation
  7473. * - a duk_size_t indicating the full allocated size (always >= 'size')
  7474. *
  7475. * If DUK_HBUFFER_FLAG_EXTERNAL is set, the buffer has been allocated
  7476. * by user code, so that Duktape won't be able to resize it and won't
  7477. * free it. This allows buffers to point to e.g. an externally
  7478. * allocated structure such as a frame buffer.
  7479. *
  7480. * Unlike strings, no terminator byte (NUL) is guaranteed after the
  7481. * data. This would be convenient, but would pad aligned user buffers
  7482. * unnecessarily upwards in size. For instance, if user code requested
  7483. * a 64-byte dynamic buffer, 65 bytes would actually be allocated which
  7484. * would then potentially round upwards to perhaps 68 or 72 bytes.
  7485. */
  7486. };
  7487. /* Fixed buffer; data follows struct, with proper alignment guaranteed by
  7488. * struct size.
  7489. */
  7490. #if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA)
  7491. #pragma pack(push, 8)
  7492. #endif
  7493. struct duk_hbuffer_fixed {
  7494. /* A union is used here as a portable struct size / alignment trick:
  7495. * by adding a 32-bit or a 64-bit (unused) union member, the size of
  7496. * the struct is effectively forced to be a multiple of 4 or 8 bytes
  7497. * (respectively) without increasing the size of the struct unless
  7498. * necessary.
  7499. */
  7500. union {
  7501. struct {
  7502. duk_heaphdr hdr;
  7503. #if defined(DUK_USE_BUFLEN16)
  7504. /* Stored in duk_heaphdr unused flags. */
  7505. #else
  7506. duk_size_t size;
  7507. #endif
  7508. } s;
  7509. #if (DUK_USE_ALIGN_BY == 4)
  7510. duk_uint32_t dummy_for_align4;
  7511. #elif (DUK_USE_ALIGN_BY == 8)
  7512. duk_double_t dummy_for_align8_1;
  7513. #if defined(DUK_USE_64BIT_OPS)
  7514. duk_uint64_t dummy_for_align8_2;
  7515. #endif
  7516. #elif (DUK_USE_ALIGN_BY == 1)
  7517. /* no extra padding */
  7518. #else
  7519. #error invalid DUK_USE_ALIGN_BY
  7520. #endif
  7521. } u;
  7522. /*
  7523. * Data follows the struct header. The struct size is padded by the
  7524. * compiler based on the struct members. This guarantees that the
  7525. * buffer data will be aligned-by-4 but not necessarily aligned-by-8.
  7526. *
  7527. * On platforms where alignment does not matter, the struct padding
  7528. * could be removed (if there is any). On platforms where alignment
  7529. * by 8 is required, the struct size must be forced to be a multiple
  7530. * of 8 by some means. Without it, some user code may break, and also
  7531. * Duktape itself breaks (e.g. the compiler stores duk_tvals in a
  7532. * dynamic buffer).
  7533. */
  7534. }
  7535. #if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_GCC_ATTR)
  7536. __attribute__ ((aligned (8)))
  7537. #elif (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_CLANG_ATTR)
  7538. __attribute__ ((aligned (8)))
  7539. #endif
  7540. ;
  7541. #if (DUK_USE_ALIGN_BY == 8) && defined(DUK_USE_PACK_MSVC_PRAGMA)
  7542. #pragma pack(pop)
  7543. #endif
  7544. /* Dynamic buffer with 'curr_alloc' pointing to a dynamic area allocated using
  7545. * heap allocation primitives. Also used for external buffers when low memory
  7546. * options are not used.
  7547. */
  7548. struct duk_hbuffer_dynamic {
  7549. duk_heaphdr hdr;
  7550. #if defined(DUK_USE_BUFLEN16)
  7551. /* Stored in duk_heaphdr unused flags. */
  7552. #else
  7553. duk_size_t size;
  7554. #endif
  7555. #if defined(DUK_USE_HEAPPTR16)
  7556. /* Stored in duk_heaphdr h_extra16. */
  7557. #else
  7558. void *curr_alloc; /* may be NULL if alloc_size == 0 */
  7559. #endif
  7560. /*
  7561. * Allocation size for 'curr_alloc' is alloc_size. There is no
  7562. * automatic NUL terminator for buffers (see above for rationale).
  7563. *
  7564. * 'curr_alloc' is explicitly allocated with heap allocation
  7565. * primitives and will thus always have alignment suitable for
  7566. * e.g. duk_tval and an IEEE double.
  7567. */
  7568. };
  7569. /* External buffer with 'curr_alloc' managed by user code and pointing to an
  7570. * arbitrary address. When heap pointer compression is not used, this struct
  7571. * has the same layout as duk_hbuffer_dynamic.
  7572. */
  7573. struct duk_hbuffer_external {
  7574. duk_heaphdr hdr;
  7575. #if defined(DUK_USE_BUFLEN16)
  7576. /* Stored in duk_heaphdr unused flags. */
  7577. #else
  7578. duk_size_t size;
  7579. #endif
  7580. /* Cannot be compressed as a heap pointer because may point to
  7581. * an arbitrary address.
  7582. */
  7583. void *curr_alloc; /* may be NULL if alloc_size == 0 */
  7584. };
  7585. /*
  7586. * Prototypes
  7587. */
  7588. DUK_INTERNAL_DECL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata);
  7589. DUK_INTERNAL_DECL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud); /* indirect allocs */
  7590. /* dynamic buffer ops */
  7591. DUK_INTERNAL_DECL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size);
  7592. DUK_INTERNAL_DECL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf);
  7593. #endif /* DUK_HBUFFER_H_INCLUDED */
  7594. /* #include duk_hproxy.h */
  7595. #line 1 "duk_hproxy.h"
  7596. /*
  7597. * Proxy object representation.
  7598. */
  7599. #if !defined(DUK_HPROXY_H_INCLUDED)
  7600. #define DUK_HPROXY_H_INCLUDED
  7601. #define DUK_ASSERT_HPROXY_VALID(h) do { \
  7602. DUK_ASSERT((h) != NULL); \
  7603. DUK_ASSERT((h)->target != NULL); \
  7604. DUK_ASSERT((h)->handler != NULL); \
  7605. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ((duk_hobject *) (h))); \
  7606. } while (0)
  7607. struct duk_hproxy {
  7608. /* Shared object part. */
  7609. duk_hobject obj;
  7610. /* Proxy target object. */
  7611. duk_hobject *target;
  7612. /* Proxy handlers (traps). */
  7613. duk_hobject *handler;
  7614. };
  7615. #endif /* DUK_HPROXY_H_INCLUDED */
  7616. /* #include duk_heap.h */
  7617. #line 1 "duk_heap.h"
  7618. /*
  7619. * Heap structure.
  7620. *
  7621. * Heap contains allocated heap objects, interned strings, and built-in
  7622. * strings for one or more threads.
  7623. */
  7624. #if !defined(DUK_HEAP_H_INCLUDED)
  7625. #define DUK_HEAP_H_INCLUDED
  7626. /* alloc function typedefs in duktape.h */
  7627. /*
  7628. * Heap flags
  7629. */
  7630. #define DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED (1U << 0) /* mark-and-sweep marking reached a recursion limit and must use multi-pass marking */
  7631. #define DUK_HEAP_FLAG_INTERRUPT_RUNNING (1U << 1) /* executor interrupt running (used to avoid nested interrupts) */
  7632. #define DUK_HEAP_FLAG_FINALIZER_NORESCUE (1U << 2) /* heap destruction ongoing, finalizer rescue no longer possible */
  7633. #define DUK_HEAP_FLAG_DEBUGGER_PAUSED (1U << 3) /* debugger is paused: talk with debug client until step/resume */
  7634. #define DUK__HEAP_HAS_FLAGS(heap,bits) ((heap)->flags & (bits))
  7635. #define DUK__HEAP_SET_FLAGS(heap,bits) do { \
  7636. (heap)->flags |= (bits); \
  7637. } while (0)
  7638. #define DUK__HEAP_CLEAR_FLAGS(heap,bits) do { \
  7639. (heap)->flags &= ~(bits); \
  7640. } while (0)
  7641. #define DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  7642. #define DUK_HEAP_HAS_INTERRUPT_RUNNING(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  7643. #define DUK_HEAP_HAS_FINALIZER_NORESCUE(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)
  7644. #define DUK_HEAP_HAS_DEBUGGER_PAUSED(heap) DUK__HEAP_HAS_FLAGS((heap), DUK_HEAP_FLAG_DEBUGGER_PAUSED)
  7645. #define DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  7646. #define DUK_HEAP_SET_INTERRUPT_RUNNING(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  7647. #define DUK_HEAP_SET_FINALIZER_NORESCUE(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)
  7648. #define DUK_HEAP_SET_DEBUGGER_PAUSED(heap) DUK__HEAP_SET_FLAGS((heap), DUK_HEAP_FLAG_DEBUGGER_PAUSED)
  7649. #define DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_MARKANDSWEEP_RECLIMIT_REACHED)
  7650. #define DUK_HEAP_CLEAR_INTERRUPT_RUNNING(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_INTERRUPT_RUNNING)
  7651. #define DUK_HEAP_CLEAR_FINALIZER_NORESCUE(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_FINALIZER_NORESCUE)
  7652. #define DUK_HEAP_CLEAR_DEBUGGER_PAUSED(heap) DUK__HEAP_CLEAR_FLAGS((heap), DUK_HEAP_FLAG_DEBUGGER_PAUSED)
  7653. /*
  7654. * Longjmp types, also double as identifying continuation type for a rethrow (in 'finally')
  7655. */
  7656. #define DUK_LJ_TYPE_UNKNOWN 0 /* unused */
  7657. #define DUK_LJ_TYPE_THROW 1 /* value1 -> error object */
  7658. #define DUK_LJ_TYPE_YIELD 2 /* value1 -> yield value, iserror -> error / normal */
  7659. #define DUK_LJ_TYPE_RESUME 3 /* value1 -> resume value, value2 -> resumee thread, iserror -> error/normal */
  7660. #define DUK_LJ_TYPE_BREAK 4 /* value1 -> label number, pseudo-type to indicate a break continuation (for ENDFIN) */
  7661. #define DUK_LJ_TYPE_CONTINUE 5 /* value1 -> label number, pseudo-type to indicate a continue continuation (for ENDFIN) */
  7662. #define DUK_LJ_TYPE_RETURN 6 /* value1 -> return value, pseudo-type to indicate a return continuation (for ENDFIN) */
  7663. #define DUK_LJ_TYPE_NORMAL 7 /* no value, pseudo-type to indicate a normal continuation (for ENDFIN) */
  7664. /*
  7665. * Mark-and-sweep flags
  7666. *
  7667. * These are separate from heap level flags now but could be merged.
  7668. * The heap structure only contains a 'base mark-and-sweep flags'
  7669. * field and the GC caller can impose further flags.
  7670. */
  7671. /* Emergency mark-and-sweep: try extra hard, even at the cost of
  7672. * performance.
  7673. */
  7674. #define DUK_MS_FLAG_EMERGENCY (1U << 0)
  7675. /* Voluntary mark-and-sweep: triggered periodically. */
  7676. #define DUK_MS_FLAG_VOLUNTARY (1U << 1)
  7677. /* Postpone rescue decisions for reachable objects with FINALIZED set.
  7678. * Used during finalize_list processing to avoid incorrect rescue
  7679. * decisions due to finalize_list being a reachability root.
  7680. */
  7681. #define DUK_MS_FLAG_POSTPONE_RESCUE (1U << 2)
  7682. /* Don't compact objects; needed during object property table resize
  7683. * to prevent a recursive resize. It would suffice to protect only the
  7684. * current object being resized, but this is not yet implemented.
  7685. */
  7686. #define DUK_MS_FLAG_NO_OBJECT_COMPACTION (1U << 3)
  7687. /*
  7688. * Thread switching
  7689. *
  7690. * To switch heap->curr_thread, use the macro below so that interrupt counters
  7691. * get updated correctly. The macro allows a NULL target thread because that
  7692. * happens e.g. in call handling.
  7693. */
  7694. #if defined(DUK_USE_INTERRUPT_COUNTER)
  7695. #define DUK_HEAP_SWITCH_THREAD(heap,newthr) duk_heap_switch_thread((heap), (newthr))
  7696. #else
  7697. #define DUK_HEAP_SWITCH_THREAD(heap,newthr) do { \
  7698. (heap)->curr_thread = (newthr); \
  7699. } while (0)
  7700. #endif
  7701. /*
  7702. * Stats
  7703. */
  7704. #if defined(DUK_USE_DEBUG)
  7705. #define DUK_STATS_INC(heap,fieldname) do { \
  7706. (heap)->fieldname += 1; \
  7707. } while (0)
  7708. #else
  7709. #define DUK_STATS_INC(heap,fieldname) do {} while (0)
  7710. #endif
  7711. /*
  7712. * Other heap related defines
  7713. */
  7714. /* Mark-and-sweep interval is relative to combined count of objects and
  7715. * strings kept in the heap during the latest mark-and-sweep pass.
  7716. * Fixed point .8 multiplier and .0 adder. Trigger count (interval) is
  7717. * decreased by each (re)allocation attempt (regardless of size), and each
  7718. * refzero processed object.
  7719. *
  7720. * 'SKIP' indicates how many (re)allocations to wait until a retry if
  7721. * GC is skipped because there is no thread do it with yet (happens
  7722. * only during init phases).
  7723. */
  7724. #if defined(DUK_USE_REFERENCE_COUNTING)
  7725. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 12800L /* 50x heap size */
  7726. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L
  7727. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L
  7728. #else
  7729. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT 256L /* 1x heap size */
  7730. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD 1024L
  7731. #define DUK_HEAP_MARK_AND_SWEEP_TRIGGER_SKIP 256L
  7732. #endif
  7733. /* GC torture. */
  7734. #if defined(DUK_USE_GC_TORTURE)
  7735. #define DUK_GC_TORTURE(heap) do { duk_heap_mark_and_sweep((heap), 0); } while (0)
  7736. #else
  7737. #define DUK_GC_TORTURE(heap) do { } while (0)
  7738. #endif
  7739. /* Stringcache is used for speeding up char-offset-to-byte-offset
  7740. * translations for non-ASCII strings.
  7741. */
  7742. #define DUK_HEAP_STRCACHE_SIZE 4
  7743. #define DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT 16 /* strings up to the this length are not cached */
  7744. /* Some list management macros. */
  7745. #define DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap,hdr) duk_heap_insert_into_heap_allocated((heap), (hdr))
  7746. #if defined(DUK_USE_REFERENCE_COUNTING)
  7747. #define DUK_HEAP_REMOVE_FROM_HEAP_ALLOCATED(heap,hdr) duk_heap_remove_from_heap_allocated((heap), (hdr))
  7748. #endif
  7749. #if defined(DUK_USE_FINALIZER_SUPPORT)
  7750. #define DUK_HEAP_INSERT_INTO_FINALIZE_LIST(heap,hdr) duk_heap_insert_into_finalize_list((heap), (hdr))
  7751. #define DUK_HEAP_REMOVE_FROM_FINALIZE_LIST(heap,hdr) duk_heap_remove_from_finalize_list((heap), (hdr))
  7752. #endif
  7753. /*
  7754. * Built-in strings
  7755. */
  7756. /* heap string indices are autogenerated in duk_strings.h */
  7757. #if defined(DUK_USE_ROM_STRINGS)
  7758. #define DUK_HEAP_GET_STRING(heap,idx) \
  7759. ((duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_stridx[(idx)]))
  7760. #else /* DUK_USE_ROM_STRINGS */
  7761. #if defined(DUK_USE_HEAPPTR16)
  7762. #define DUK_HEAP_GET_STRING(heap,idx) \
  7763. ((duk_hstring *) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (heap)->strs16[(idx)]))
  7764. #else
  7765. #define DUK_HEAP_GET_STRING(heap,idx) \
  7766. ((heap)->strs[(idx)])
  7767. #endif
  7768. #endif /* DUK_USE_ROM_STRINGS */
  7769. /*
  7770. * Raw memory calls: relative to heap, but no GC interaction
  7771. */
  7772. #define DUK_ALLOC_RAW(heap,size) \
  7773. ((heap)->alloc_func((heap)->heap_udata, (size)))
  7774. #define DUK_REALLOC_RAW(heap,ptr,newsize) \
  7775. ((heap)->realloc_func((heap)->heap_udata, (void *) (ptr), (newsize)))
  7776. #define DUK_FREE_RAW(heap,ptr) \
  7777. ((heap)->free_func((heap)->heap_udata, (void *) (ptr)))
  7778. /*
  7779. * Memory calls: relative to heap, GC interaction, but no error throwing.
  7780. *
  7781. * XXX: Currently a mark-and-sweep triggered by memory allocation will run
  7782. * using the heap->heap_thread. This thread is also used for running
  7783. * mark-and-sweep finalization; this is not ideal because it breaks the
  7784. * isolation between multiple global environments.
  7785. *
  7786. * Notes:
  7787. *
  7788. * - DUK_FREE() is required to ignore NULL and any other possible return
  7789. * value of a zero-sized alloc/realloc (same as ANSI C free()).
  7790. *
  7791. * - There is no DUK_REALLOC_ZEROED because we don't assume to know the
  7792. * old size. Caller must zero the reallocated memory.
  7793. *
  7794. * - DUK_REALLOC_INDIRECT() must be used when a mark-and-sweep triggered
  7795. * by an allocation failure might invalidate the original 'ptr', thus
  7796. * causing a realloc retry to use an invalid pointer. Example: we're
  7797. * reallocating the value stack and a finalizer resizes the same value
  7798. * stack during mark-and-sweep. The indirect variant requests for the
  7799. * current location of the pointer being reallocated using a callback
  7800. * right before every realloc attempt; this circuitous approach is used
  7801. * to avoid strict aliasing issues in a more straightforward indirect
  7802. * pointer (void **) approach. Note: the pointer in the storage
  7803. * location is read but is NOT updated; the caller must do that.
  7804. */
  7805. /* callback for indirect reallocs, request for current pointer */
  7806. typedef void *(*duk_mem_getptr)(duk_heap *heap, void *ud);
  7807. #define DUK_ALLOC(heap,size) duk_heap_mem_alloc((heap), (size))
  7808. #define DUK_ALLOC_ZEROED(heap,size) duk_heap_mem_alloc_zeroed((heap), (size))
  7809. #define DUK_REALLOC(heap,ptr,newsize) duk_heap_mem_realloc((heap), (ptr), (newsize))
  7810. #define DUK_REALLOC_INDIRECT(heap,cb,ud,newsize) duk_heap_mem_realloc_indirect((heap), (cb), (ud), (newsize))
  7811. #define DUK_FREE(heap,ptr) duk_heap_mem_free((heap), (ptr))
  7812. /*
  7813. * Checked allocation, relative to a thread
  7814. *
  7815. * DUK_FREE_CHECKED() doesn't actually throw, but accepts a 'thr' argument
  7816. * for convenience.
  7817. */
  7818. #define DUK_ALLOC_CHECKED(thr,size) duk_heap_mem_alloc_checked((thr), (size))
  7819. #define DUK_ALLOC_CHECKED_ZEROED(thr,size) duk_heap_mem_alloc_checked_zeroed((thr), (size))
  7820. #define DUK_FREE_CHECKED(thr,ptr) duk_heap_mem_free((thr)->heap, (ptr))
  7821. /*
  7822. * Memory constants
  7823. */
  7824. #define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT 10 /* Retry allocation after mark-and-sweep for this
  7825. * many times. A single mark-and-sweep round is
  7826. * not guaranteed to free all unreferenced memory
  7827. * because of finalization (in fact, ANY number of
  7828. * rounds is strictly not enough).
  7829. */
  7830. #define DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT 3 /* Starting from this round, use emergency mode
  7831. * for mark-and-sweep.
  7832. */
  7833. /*
  7834. * Debugger support
  7835. */
  7836. /* Maximum number of breakpoints. Only breakpoints that are set are
  7837. * consulted so increasing this has no performance impact.
  7838. */
  7839. #define DUK_HEAP_MAX_BREAKPOINTS 16
  7840. /* Opcode interval for a Date-based status/peek rate limit check. Only
  7841. * relevant when debugger is attached. Requesting a timestamp may be a
  7842. * slow operation on some platforms so this shouldn't be too low. On the
  7843. * other hand a high value makes Duktape react to a pause request slowly.
  7844. */
  7845. #define DUK_HEAP_DBG_RATELIMIT_OPCODES 4000
  7846. /* Milliseconds between status notify and transport peeks. */
  7847. #define DUK_HEAP_DBG_RATELIMIT_MILLISECS 200
  7848. /* Debugger pause flags. */
  7849. #define DUK_PAUSE_FLAG_ONE_OPCODE (1U << 0) /* pause when a single opcode has been executed */
  7850. #define DUK_PAUSE_FLAG_ONE_OPCODE_ACTIVE (1U << 1) /* one opcode pause actually active; artifact of current implementation */
  7851. #define DUK_PAUSE_FLAG_LINE_CHANGE (1U << 2) /* pause when current line number changes */
  7852. #define DUK_PAUSE_FLAG_FUNC_ENTRY (1U << 3) /* pause when entering a function */
  7853. #define DUK_PAUSE_FLAG_FUNC_EXIT (1U << 4) /* pause when exiting current function */
  7854. #define DUK_PAUSE_FLAG_CAUGHT_ERROR (1U << 5) /* pause when about to throw an error that is caught */
  7855. #define DUK_PAUSE_FLAG_UNCAUGHT_ERROR (1U << 6) /* pause when about to throw an error that won't be caught */
  7856. struct duk_breakpoint {
  7857. duk_hstring *filename;
  7858. duk_uint32_t line;
  7859. };
  7860. /*
  7861. * String cache should ideally be at duk_hthread level, but that would
  7862. * cause string finalization to slow down relative to the number of
  7863. * threads; string finalization must check the string cache for "weak"
  7864. * references to the string being finalized to avoid dead pointers.
  7865. *
  7866. * Thus, string caches are now at the heap level now.
  7867. */
  7868. struct duk_strcache_entry {
  7869. duk_hstring *h;
  7870. duk_uint32_t bidx;
  7871. duk_uint32_t cidx;
  7872. };
  7873. /*
  7874. * Longjmp state, contains the information needed to perform a longjmp.
  7875. * Longjmp related values are written to value1, value2, and iserror.
  7876. */
  7877. struct duk_ljstate {
  7878. duk_jmpbuf *jmpbuf_ptr; /* current setjmp() catchpoint */
  7879. duk_small_uint_t type; /* longjmp type */
  7880. duk_bool_t iserror; /* isError flag for yield */
  7881. duk_tval value1; /* 1st related value (type specific) */
  7882. duk_tval value2; /* 2nd related value (type specific) */
  7883. };
  7884. #define DUK_ASSERT_LJSTATE_UNSET(heap) do { \
  7885. DUK_ASSERT(heap != NULL); \
  7886. DUK_ASSERT(heap->lj.type == DUK_LJ_TYPE_UNKNOWN); \
  7887. DUK_ASSERT(heap->lj.iserror == 0); \
  7888. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&heap->lj.value1)); \
  7889. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&heap->lj.value2)); \
  7890. } while (0)
  7891. #define DUK_ASSERT_LJSTATE_SET(heap) do { \
  7892. DUK_ASSERT(heap != NULL); \
  7893. DUK_ASSERT(heap->lj.type != DUK_LJ_TYPE_UNKNOWN); \
  7894. } while (0)
  7895. /*
  7896. * Literal intern cache
  7897. */
  7898. struct duk_litcache_entry {
  7899. const duk_uint8_t *addr;
  7900. duk_hstring *h;
  7901. };
  7902. /*
  7903. * Main heap structure
  7904. */
  7905. struct duk_heap {
  7906. duk_small_uint_t flags;
  7907. /* Allocator functions. */
  7908. duk_alloc_function alloc_func;
  7909. duk_realloc_function realloc_func;
  7910. duk_free_function free_func;
  7911. /* Heap udata, used for allocator functions but also for other heap
  7912. * level callbacks like fatal function, pointer compression, etc.
  7913. */
  7914. void *heap_udata;
  7915. /* Fatal error handling, called e.g. when a longjmp() is needed but
  7916. * lj.jmpbuf_ptr is NULL. fatal_func must never return; it's not
  7917. * declared as "noreturn" because doing that for typedefs is a bit
  7918. * challenging portability-wise.
  7919. */
  7920. duk_fatal_function fatal_func;
  7921. /* Main list of allocated heap objects. Objects are either here,
  7922. * in finalize_list waiting for processing, or in refzero_list
  7923. * temporarily while a DECREF refzero cascade finishes.
  7924. */
  7925. duk_heaphdr *heap_allocated;
  7926. /* Temporary work list for freeing a cascade of objects when a DECREF
  7927. * (or DECREF_NORZ) encounters a zero refcount. Using a work list
  7928. * allows fixed C stack size when refcounts go to zero for a chain of
  7929. * objects. Outside of DECREF this is always a NULL because DECREF is
  7930. * processed without side effects (only memory free calls).
  7931. */
  7932. #if defined(DUK_USE_REFERENCE_COUNTING)
  7933. duk_heaphdr *refzero_list;
  7934. #endif
  7935. #if defined(DUK_USE_FINALIZER_SUPPORT)
  7936. /* Work list for objects to be finalized. */
  7937. duk_heaphdr *finalize_list;
  7938. #if defined(DUK_USE_ASSERTIONS)
  7939. /* Object whose finalizer is executing right now (no nesting). */
  7940. duk_heaphdr *currently_finalizing;
  7941. #endif
  7942. #endif
  7943. /* Freelist for duk_activations and duk_catchers. */
  7944. #if defined(DUK_USE_CACHE_ACTIVATION)
  7945. duk_activation *activation_free;
  7946. #endif
  7947. #if defined(DUK_USE_CACHE_CATCHER)
  7948. duk_catcher *catcher_free;
  7949. #endif
  7950. /* Voluntary mark-and-sweep trigger counter. Intentionally signed
  7951. * because we continue decreasing the value when voluntary GC cannot
  7952. * run.
  7953. */
  7954. #if defined(DUK_USE_VOLUNTARY_GC)
  7955. duk_int_t ms_trigger_counter;
  7956. #endif
  7957. /* Mark-and-sweep recursion control: too deep recursion causes
  7958. * multi-pass processing to avoid growing C stack without bound.
  7959. */
  7960. duk_uint_t ms_recursion_depth;
  7961. /* Mark-and-sweep flags automatically active (used for critical sections). */
  7962. duk_small_uint_t ms_base_flags;
  7963. /* Mark-and-sweep running flag. Prevents re-entry, and also causes
  7964. * refzero events to be ignored (= objects won't be queued to refzero_list).
  7965. */
  7966. duk_uint_t ms_running;
  7967. /* Mark-and-sweep prevent count, stacking. Used to avoid M&S side
  7968. * effects (besides finalizers which are controlled separately) such
  7969. * as compacting the string table or object property tables. This
  7970. * is also bumped when ms_running is set to prevent recursive re-entry.
  7971. * Can also be bumped when mark-and-sweep is not running.
  7972. */
  7973. duk_uint_t ms_prevent_count;
  7974. /* Finalizer processing prevent count, stacking. Bumped when finalizers
  7975. * are processed to prevent recursive finalizer processing (first call site
  7976. * processing finalizers handles all finalizers until the list is empty).
  7977. * Can also be bumped explicitly to prevent finalizer execution.
  7978. */
  7979. duk_uint_t pf_prevent_count;
  7980. /* When processing finalize_list, don't actually run finalizers but
  7981. * queue finalizable objects back to heap_allocated as is. This is
  7982. * used during heap destruction to deal with finalizers that keep
  7983. * on creating more finalizable garbage.
  7984. */
  7985. duk_uint_t pf_skip_finalizers;
  7986. #if defined(DUK_USE_ASSERTIONS)
  7987. /* Set when we're in a critical path where an error throw would cause
  7988. * e.g. sandboxing/protected call violations or state corruption. This
  7989. * is just used for asserts.
  7990. */
  7991. duk_bool_t error_not_allowed;
  7992. #endif
  7993. #if defined(DUK_USE_ASSERTIONS)
  7994. /* Set when heap is still being initialized, helps with writing
  7995. * some assertions.
  7996. */
  7997. duk_bool_t heap_initializing;
  7998. #endif
  7999. /* Marker for detecting internal "double faults", errors thrown when
  8000. * we're trying to create an error object, see duk_error_throw.c.
  8001. */
  8002. duk_bool_t creating_error;
  8003. /* Marker for indicating we're calling a user error augmentation
  8004. * (errCreate/errThrow) function. Errors created/thrown during
  8005. * such a call are not augmented.
  8006. */
  8007. #if defined(DUK_USE_AUGMENT_ERROR_THROW) || defined(DUK_USE_AUGMENT_ERROR_CREATE)
  8008. duk_bool_t augmenting_error;
  8009. #endif
  8010. /* Longjmp state. */
  8011. duk_ljstate lj;
  8012. /* Heap thread, used internally and for finalization. */
  8013. duk_hthread *heap_thread;
  8014. /* Current running thread. */
  8015. duk_hthread *curr_thread;
  8016. /* Heap level "stash" object (e.g., various reachability roots). */
  8017. duk_hobject *heap_object;
  8018. /* duk_handle_call / duk_handle_safe_call recursion depth limiting */
  8019. duk_int_t call_recursion_depth;
  8020. duk_int_t call_recursion_limit;
  8021. /* Mix-in value for computing string hashes; should be reasonably unpredictable. */
  8022. duk_uint32_t hash_seed;
  8023. /* Random number state for duk_util_tinyrandom.c. */
  8024. #if !defined(DUK_USE_GET_RANDOM_DOUBLE)
  8025. #if defined(DUK_USE_PREFER_SIZE) || !defined(DUK_USE_64BIT_OPS)
  8026. duk_uint32_t rnd_state; /* State for Shamir's three-op algorithm */
  8027. #else
  8028. duk_uint64_t rnd_state[2]; /* State for xoroshiro128+ */
  8029. #endif
  8030. #endif
  8031. /* Counter for unique local symbol creation. */
  8032. /* XXX: When 64-bit types are available, it would be more efficient to
  8033. * use a duk_uint64_t at least for incrementing but maybe also for
  8034. * string formatting in the Symbol constructor.
  8035. */
  8036. duk_uint32_t sym_counter[2];
  8037. /* For manual debugging: instruction count based on executor and
  8038. * interrupt counter book-keeping. Inspect debug logs to see how
  8039. * they match up.
  8040. */
  8041. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  8042. duk_int_t inst_count_exec;
  8043. duk_int_t inst_count_interrupt;
  8044. #endif
  8045. /* Debugger state. */
  8046. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8047. /* Callbacks and udata; dbg_read_cb != NULL is used to indicate attached state. */
  8048. duk_debug_read_function dbg_read_cb; /* required, NULL implies detached */
  8049. duk_debug_write_function dbg_write_cb; /* required */
  8050. duk_debug_peek_function dbg_peek_cb;
  8051. duk_debug_read_flush_function dbg_read_flush_cb;
  8052. duk_debug_write_flush_function dbg_write_flush_cb;
  8053. duk_debug_request_function dbg_request_cb;
  8054. duk_debug_detached_function dbg_detached_cb;
  8055. void *dbg_udata;
  8056. /* The following are only relevant when debugger is attached. */
  8057. duk_bool_t dbg_processing; /* currently processing messages or breakpoints: don't enter message processing recursively (e.g. no breakpoints when processing debugger eval) */
  8058. duk_bool_t dbg_state_dirty; /* resend state next time executor is about to run */
  8059. duk_bool_t dbg_force_restart; /* force executor restart to recheck breakpoints; used to handle function returns (see GH-303) */
  8060. duk_bool_t dbg_detaching; /* debugger detaching; used to avoid calling detach handler recursively */
  8061. duk_small_uint_t dbg_pause_flags; /* flags for automatic pause behavior */
  8062. duk_activation *dbg_pause_act; /* activation related to pause behavior (pause on line change, function entry/exit) */
  8063. duk_uint32_t dbg_pause_startline; /* starting line number for line change related pause behavior */
  8064. duk_breakpoint dbg_breakpoints[DUK_HEAP_MAX_BREAKPOINTS]; /* breakpoints: [0,breakpoint_count[ gc reachable */
  8065. duk_small_uint_t dbg_breakpoint_count;
  8066. duk_breakpoint *dbg_breakpoints_active[DUK_HEAP_MAX_BREAKPOINTS + 1]; /* currently active breakpoints: NULL term, borrowed pointers */
  8067. /* XXX: make active breakpoints actual copies instead of pointers? */
  8068. /* These are for rate limiting Status notifications and transport peeking. */
  8069. duk_uint_t dbg_exec_counter; /* cumulative opcode execution count (overflows are OK) */
  8070. duk_uint_t dbg_last_counter; /* value of dbg_exec_counter when we last did a Date-based check */
  8071. duk_double_t dbg_last_time; /* time when status/peek was last done (Date-based rate limit) */
  8072. /* Used to support single-byte stream lookahead. */
  8073. duk_bool_t dbg_have_next_byte;
  8074. duk_uint8_t dbg_next_byte;
  8075. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  8076. #if defined(DUK_USE_ASSERTIONS)
  8077. duk_bool_t dbg_calling_transport; /* transport call in progress, calling into Duktape forbidden */
  8078. #endif
  8079. /* String intern table (weak refs). */
  8080. #if defined(DUK_USE_STRTAB_PTRCOMP)
  8081. duk_uint16_t *strtable16;
  8082. #else
  8083. duk_hstring **strtable;
  8084. #endif
  8085. duk_uint32_t st_mask; /* mask for lookup, st_size - 1 */
  8086. duk_uint32_t st_size; /* stringtable size */
  8087. #if (DUK_USE_STRTAB_MINSIZE != DUK_USE_STRTAB_MAXSIZE)
  8088. duk_uint32_t st_count; /* string count for resize load factor checks */
  8089. #endif
  8090. duk_bool_t st_resizing; /* string table is being resized; avoid recursive resize */
  8091. /* String access cache (codepoint offset -> byte offset) for fast string
  8092. * character looping; 'weak' reference which needs special handling in GC.
  8093. */
  8094. duk_strcache_entry strcache[DUK_HEAP_STRCACHE_SIZE];
  8095. #if defined(DUK_USE_LITCACHE_SIZE)
  8096. /* Literal intern cache. When enabled, strings interned as literals
  8097. * (e.g. duk_push_literal()) will be pinned and cached for the lifetime
  8098. * of the heap.
  8099. */
  8100. duk_litcache_entry litcache[DUK_USE_LITCACHE_SIZE];
  8101. #endif
  8102. /* Built-in strings. */
  8103. #if defined(DUK_USE_ROM_STRINGS)
  8104. /* No field needed when strings are in ROM. */
  8105. #else
  8106. #if defined(DUK_USE_HEAPPTR16)
  8107. duk_uint16_t strs16[DUK_HEAP_NUM_STRINGS];
  8108. #else
  8109. duk_hstring *strs[DUK_HEAP_NUM_STRINGS];
  8110. #endif
  8111. #endif
  8112. /* Stats. */
  8113. #if defined(DUK_USE_DEBUG)
  8114. duk_int_t stats_exec_opcodes;
  8115. duk_int_t stats_exec_interrupt;
  8116. duk_int_t stats_exec_throw;
  8117. duk_int_t stats_call_all;
  8118. duk_int_t stats_call_tailcall;
  8119. duk_int_t stats_call_ecmatoecma;
  8120. duk_int_t stats_safecall_all;
  8121. duk_int_t stats_safecall_nothrow;
  8122. duk_int_t stats_safecall_throw;
  8123. duk_int_t stats_ms_try_count;
  8124. duk_int_t stats_ms_skip_count;
  8125. duk_int_t stats_ms_emergency_count;
  8126. duk_int_t stats_strtab_intern_hit;
  8127. duk_int_t stats_strtab_intern_miss;
  8128. duk_int_t stats_strtab_resize_check;
  8129. duk_int_t stats_strtab_resize_grow;
  8130. duk_int_t stats_strtab_resize_shrink;
  8131. duk_int_t stats_strtab_litcache_hit;
  8132. duk_int_t stats_strtab_litcache_miss;
  8133. duk_int_t stats_strtab_litcache_pin;
  8134. duk_int_t stats_object_realloc_props;
  8135. duk_int_t stats_object_abandon_array;
  8136. duk_int_t stats_getownpropdesc_count;
  8137. duk_int_t stats_getownpropdesc_hit;
  8138. duk_int_t stats_getownpropdesc_miss;
  8139. duk_int_t stats_getpropdesc_count;
  8140. duk_int_t stats_getpropdesc_hit;
  8141. duk_int_t stats_getpropdesc_miss;
  8142. duk_int_t stats_getprop_all;
  8143. duk_int_t stats_getprop_arrayidx;
  8144. duk_int_t stats_getprop_bufobjidx;
  8145. duk_int_t stats_getprop_bufferidx;
  8146. duk_int_t stats_getprop_bufferlen;
  8147. duk_int_t stats_getprop_stringidx;
  8148. duk_int_t stats_getprop_stringlen;
  8149. duk_int_t stats_getprop_proxy;
  8150. duk_int_t stats_getprop_arguments;
  8151. duk_int_t stats_putprop_all;
  8152. duk_int_t stats_putprop_arrayidx;
  8153. duk_int_t stats_putprop_bufobjidx;
  8154. duk_int_t stats_putprop_bufferidx;
  8155. duk_int_t stats_putprop_proxy;
  8156. duk_int_t stats_getvar_all;
  8157. duk_int_t stats_putvar_all;
  8158. #endif
  8159. };
  8160. /*
  8161. * Prototypes
  8162. */
  8163. DUK_INTERNAL_DECL
  8164. duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
  8165. duk_realloc_function realloc_func,
  8166. duk_free_function free_func,
  8167. void *heap_udata,
  8168. duk_fatal_function fatal_func);
  8169. DUK_INTERNAL_DECL void duk_heap_free(duk_heap *heap);
  8170. DUK_INTERNAL_DECL void duk_free_hobject(duk_heap *heap, duk_hobject *h);
  8171. DUK_INTERNAL_DECL void duk_free_hbuffer(duk_heap *heap, duk_hbuffer *h);
  8172. DUK_INTERNAL_DECL void duk_free_hstring(duk_heap *heap, duk_hstring *h);
  8173. DUK_INTERNAL_DECL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr);
  8174. DUK_INTERNAL_DECL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr);
  8175. #if defined(DUK_USE_REFERENCE_COUNTING)
  8176. DUK_INTERNAL_DECL void duk_heap_remove_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr);
  8177. #endif
  8178. #if defined(DUK_USE_FINALIZER_SUPPORT)
  8179. DUK_INTERNAL_DECL void duk_heap_insert_into_finalize_list(duk_heap *heap, duk_heaphdr *hdr);
  8180. DUK_INTERNAL_DECL void duk_heap_remove_from_finalize_list(duk_heap *heap, duk_heaphdr *hdr);
  8181. #endif
  8182. #if defined(DUK_USE_ASSERTIONS)
  8183. DUK_INTERNAL_DECL duk_bool_t duk_heap_in_heap_allocated(duk_heap *heap, duk_heaphdr *ptr);
  8184. #endif
  8185. #if defined(DUK_USE_INTERRUPT_COUNTER)
  8186. DUK_INTERNAL_DECL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr);
  8187. #endif
  8188. DUK_INTERNAL_DECL duk_hstring *duk_heap_strtable_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen);
  8189. DUK_INTERNAL_DECL duk_hstring *duk_heap_strtable_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t len);
  8190. #if defined(DUK_USE_LITCACHE_SIZE)
  8191. DUK_INTERNAL_DECL duk_hstring *duk_heap_strtable_intern_literal_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t blen);
  8192. #endif
  8193. DUK_INTERNAL_DECL duk_hstring *duk_heap_strtable_intern_u32(duk_heap *heap, duk_uint32_t val);
  8194. DUK_INTERNAL_DECL duk_hstring *duk_heap_strtable_intern_u32_checked(duk_hthread *thr, duk_uint32_t val);
  8195. #if defined(DUK_USE_REFERENCE_COUNTING)
  8196. DUK_INTERNAL_DECL void duk_heap_strtable_unlink(duk_heap *heap, duk_hstring *h);
  8197. #endif
  8198. DUK_INTERNAL_DECL void duk_heap_strtable_unlink_prev(duk_heap *heap, duk_hstring *h, duk_hstring *prev);
  8199. DUK_INTERNAL_DECL void duk_heap_strtable_force_resize(duk_heap *heap);
  8200. DUK_INTERNAL void duk_heap_strtable_free(duk_heap *heap);
  8201. #if defined(DUK_USE_DEBUG)
  8202. DUK_INTERNAL void duk_heap_strtable_dump(duk_heap *heap);
  8203. #endif
  8204. DUK_INTERNAL_DECL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h);
  8205. DUK_INTERNAL_DECL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset);
  8206. #if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
  8207. DUK_INTERNAL_DECL void *duk_default_alloc_function(void *udata, duk_size_t size);
  8208. DUK_INTERNAL_DECL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize);
  8209. DUK_INTERNAL_DECL void duk_default_free_function(void *udata, void *ptr);
  8210. #endif
  8211. DUK_INTERNAL_DECL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size);
  8212. DUK_INTERNAL_DECL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size);
  8213. DUK_INTERNAL_DECL void *duk_heap_mem_alloc_checked(duk_hthread *thr, duk_size_t size);
  8214. DUK_INTERNAL_DECL void *duk_heap_mem_alloc_checked_zeroed(duk_hthread *thr, duk_size_t size);
  8215. DUK_INTERNAL_DECL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize);
  8216. DUK_INTERNAL_DECL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize);
  8217. DUK_INTERNAL_DECL void duk_heap_mem_free(duk_heap *heap, void *ptr);
  8218. DUK_INTERNAL_DECL void duk_heap_free_freelists(duk_heap *heap);
  8219. #if defined(DUK_USE_FINALIZER_SUPPORT)
  8220. DUK_INTERNAL_DECL void duk_heap_run_finalizer(duk_heap *heap, duk_hobject *obj);
  8221. DUK_INTERNAL_DECL void duk_heap_process_finalize_list(duk_heap *heap);
  8222. #endif /* DUK_USE_FINALIZER_SUPPORT */
  8223. DUK_INTERNAL_DECL void duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags);
  8224. DUK_INTERNAL_DECL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len);
  8225. #endif /* DUK_HEAP_H_INCLUDED */
  8226. /* #include duk_debugger.h */
  8227. #line 1 "duk_debugger.h"
  8228. #if !defined(DUK_DEBUGGER_H_INCLUDED)
  8229. #define DUK_DEBUGGER_H_INCLUDED
  8230. /* Debugger protocol version is defined in the public API header. */
  8231. /* Initial bytes for markers. */
  8232. #define DUK_DBG_IB_EOM 0x00
  8233. #define DUK_DBG_IB_REQUEST 0x01
  8234. #define DUK_DBG_IB_REPLY 0x02
  8235. #define DUK_DBG_IB_ERROR 0x03
  8236. #define DUK_DBG_IB_NOTIFY 0x04
  8237. /* Other initial bytes. */
  8238. #define DUK_DBG_IB_INT4 0x10
  8239. #define DUK_DBG_IB_STR4 0x11
  8240. #define DUK_DBG_IB_STR2 0x12
  8241. #define DUK_DBG_IB_BUF4 0x13
  8242. #define DUK_DBG_IB_BUF2 0x14
  8243. #define DUK_DBG_IB_UNUSED 0x15
  8244. #define DUK_DBG_IB_UNDEFINED 0x16
  8245. #define DUK_DBG_IB_NULL 0x17
  8246. #define DUK_DBG_IB_TRUE 0x18
  8247. #define DUK_DBG_IB_FALSE 0x19
  8248. #define DUK_DBG_IB_NUMBER 0x1a
  8249. #define DUK_DBG_IB_OBJECT 0x1b
  8250. #define DUK_DBG_IB_POINTER 0x1c
  8251. #define DUK_DBG_IB_LIGHTFUNC 0x1d
  8252. #define DUK_DBG_IB_HEAPPTR 0x1e
  8253. /* The short string/integer initial bytes starting from 0x60 don't have
  8254. * defines now.
  8255. */
  8256. /* Error codes. */
  8257. #define DUK_DBG_ERR_UNKNOWN 0x00
  8258. #define DUK_DBG_ERR_UNSUPPORTED 0x01
  8259. #define DUK_DBG_ERR_TOOMANY 0x02
  8260. #define DUK_DBG_ERR_NOTFOUND 0x03
  8261. #define DUK_DBG_ERR_APPLICATION 0x04
  8262. /* Commands and notifys initiated by Duktape. */
  8263. #define DUK_DBG_CMD_STATUS 0x01
  8264. #define DUK_DBG_CMD_UNUSED_2 0x02 /* Duktape 1.x: print notify */
  8265. #define DUK_DBG_CMD_UNUSED_3 0x03 /* Duktape 1.x: alert notify */
  8266. #define DUK_DBG_CMD_UNUSED_4 0x04 /* Duktape 1.x: log notify */
  8267. #define DUK_DBG_CMD_THROW 0x05
  8268. #define DUK_DBG_CMD_DETACHING 0x06
  8269. #define DUK_DBG_CMD_APPNOTIFY 0x07
  8270. /* Commands initiated by debug client. */
  8271. #define DUK_DBG_CMD_BASICINFO 0x10
  8272. #define DUK_DBG_CMD_TRIGGERSTATUS 0x11
  8273. #define DUK_DBG_CMD_PAUSE 0x12
  8274. #define DUK_DBG_CMD_RESUME 0x13
  8275. #define DUK_DBG_CMD_STEPINTO 0x14
  8276. #define DUK_DBG_CMD_STEPOVER 0x15
  8277. #define DUK_DBG_CMD_STEPOUT 0x16
  8278. #define DUK_DBG_CMD_LISTBREAK 0x17
  8279. #define DUK_DBG_CMD_ADDBREAK 0x18
  8280. #define DUK_DBG_CMD_DELBREAK 0x19
  8281. #define DUK_DBG_CMD_GETVAR 0x1a
  8282. #define DUK_DBG_CMD_PUTVAR 0x1b
  8283. #define DUK_DBG_CMD_GETCALLSTACK 0x1c
  8284. #define DUK_DBG_CMD_GETLOCALS 0x1d
  8285. #define DUK_DBG_CMD_EVAL 0x1e
  8286. #define DUK_DBG_CMD_DETACH 0x1f
  8287. #define DUK_DBG_CMD_DUMPHEAP 0x20
  8288. #define DUK_DBG_CMD_GETBYTECODE 0x21
  8289. #define DUK_DBG_CMD_APPREQUEST 0x22
  8290. #define DUK_DBG_CMD_GETHEAPOBJINFO 0x23
  8291. #define DUK_DBG_CMD_GETOBJPROPDESC 0x24
  8292. #define DUK_DBG_CMD_GETOBJPROPDESCRANGE 0x25
  8293. /* The low 8 bits map directly to duk_hobject.h DUK_PROPDESC_FLAG_xxx.
  8294. * The remaining flags are specific to the debugger.
  8295. */
  8296. #define DUK_DBG_PROPFLAG_SYMBOL (1U << 8)
  8297. #define DUK_DBG_PROPFLAG_HIDDEN (1U << 9)
  8298. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8299. DUK_INTERNAL_DECL void duk_debug_do_detach(duk_heap *heap);
  8300. DUK_INTERNAL_DECL duk_bool_t duk_debug_read_peek(duk_hthread *thr);
  8301. DUK_INTERNAL_DECL void duk_debug_write_flush(duk_hthread *thr);
  8302. DUK_INTERNAL_DECL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length);
  8303. DUK_INTERNAL_DECL void duk_debug_skip_byte(duk_hthread *thr);
  8304. DUK_INTERNAL_DECL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length);
  8305. DUK_INTERNAL_DECL duk_uint8_t duk_debug_read_byte(duk_hthread *thr);
  8306. DUK_INTERNAL_DECL duk_int32_t duk_debug_read_int(duk_hthread *thr);
  8307. DUK_INTERNAL_DECL duk_hstring *duk_debug_read_hstring(duk_hthread *thr);
  8308. /* XXX: exposed duk_debug_read_pointer */
  8309. /* XXX: exposed duk_debug_read_buffer */
  8310. /* XXX: exposed duk_debug_read_hbuffer */
  8311. #if 0
  8312. DUK_INTERNAL_DECL duk_heaphdr *duk_debug_read_heapptr(duk_hthread *thr);
  8313. #endif
  8314. #if defined(DUK_USE_DEBUGGER_INSPECT)
  8315. DUK_INTERNAL_DECL duk_heaphdr *duk_debug_read_any_ptr(duk_hthread *thr);
  8316. #endif
  8317. DUK_INTERNAL_DECL duk_tval *duk_debug_read_tval(duk_hthread *thr);
  8318. DUK_INTERNAL_DECL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length);
  8319. DUK_INTERNAL_DECL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x);
  8320. DUK_INTERNAL_DECL void duk_debug_write_unused(duk_hthread *thr);
  8321. DUK_INTERNAL_DECL void duk_debug_write_undefined(duk_hthread *thr);
  8322. #if defined(DUK_USE_DEBUGGER_INSPECT)
  8323. DUK_INTERNAL_DECL void duk_debug_write_null(duk_hthread *thr);
  8324. #endif
  8325. DUK_INTERNAL_DECL void duk_debug_write_boolean(duk_hthread *thr, duk_uint_t val);
  8326. DUK_INTERNAL_DECL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x);
  8327. DUK_INTERNAL_DECL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x);
  8328. DUK_INTERNAL_DECL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length);
  8329. DUK_INTERNAL_DECL void duk_debug_write_cstring(duk_hthread *thr, const char *data);
  8330. DUK_INTERNAL_DECL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h);
  8331. DUK_INTERNAL_DECL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length);
  8332. DUK_INTERNAL_DECL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h);
  8333. DUK_INTERNAL_DECL void duk_debug_write_pointer(duk_hthread *thr, void *ptr);
  8334. #if defined(DUK_USE_DEBUGGER_DUMPHEAP) || defined(DUK_USE_DEBUGGER_INSPECT)
  8335. DUK_INTERNAL_DECL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h);
  8336. #endif
  8337. DUK_INTERNAL_DECL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj);
  8338. DUK_INTERNAL_DECL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv);
  8339. #if 0 /* unused */
  8340. DUK_INTERNAL_DECL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command);
  8341. #endif
  8342. DUK_INTERNAL_DECL void duk_debug_write_reply(duk_hthread *thr);
  8343. DUK_INTERNAL_DECL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg);
  8344. DUK_INTERNAL_DECL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command);
  8345. DUK_INTERNAL_DECL void duk_debug_write_eom(duk_hthread *thr);
  8346. DUK_INTERNAL_DECL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr);
  8347. DUK_INTERNAL_DECL void duk_debug_send_status(duk_hthread *thr);
  8348. #if defined(DUK_USE_DEBUGGER_THROW_NOTIFY)
  8349. DUK_INTERNAL_DECL void duk_debug_send_throw(duk_hthread *thr, duk_bool_t fatal);
  8350. #endif
  8351. DUK_INTERNAL_DECL void duk_debug_halt_execution(duk_hthread *thr, duk_bool_t use_prev_pc);
  8352. DUK_INTERNAL_DECL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block);
  8353. DUK_INTERNAL_DECL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line);
  8354. DUK_INTERNAL_DECL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index);
  8355. DUK_INTERNAL_DECL duk_bool_t duk_debug_is_attached(duk_heap *heap);
  8356. DUK_INTERNAL_DECL duk_bool_t duk_debug_is_paused(duk_heap *heap);
  8357. DUK_INTERNAL_DECL void duk_debug_set_paused(duk_heap *heap);
  8358. DUK_INTERNAL_DECL void duk_debug_clear_paused(duk_heap *heap);
  8359. DUK_INTERNAL_DECL void duk_debug_clear_pause_state(duk_heap *heap);
  8360. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  8361. #endif /* DUK_DEBUGGER_H_INCLUDED */
  8362. /* #include duk_debug.h */
  8363. #line 1 "duk_debug.h"
  8364. /*
  8365. * Debugging macros, DUK_DPRINT() and its variants in particular.
  8366. *
  8367. * DUK_DPRINT() allows formatted debug prints, and supports standard
  8368. * and Duktape specific formatters. See duk_debug_vsnprintf.c for details.
  8369. *
  8370. * DUK_D(x), DUK_DD(x), and DUK_DDD(x) are used together with log macros
  8371. * for technical reasons. They are concretely used to hide 'x' from the
  8372. * compiler when the corresponding log level is disabled. This allows
  8373. * clean builds on non-C99 compilers, at the cost of more verbose code.
  8374. * Examples:
  8375. *
  8376. * DUK_D(DUK_DPRINT("foo"));
  8377. * DUK_DD(DUK_DDPRINT("foo"));
  8378. * DUK_DDD(DUK_DDDPRINT("foo"));
  8379. *
  8380. * This approach is preferable to the old "double parentheses" hack because
  8381. * double parentheses make the C99 solution worse: __FILE__ and __LINE__ can
  8382. * no longer be added transparently without going through globals, which
  8383. * works poorly with threading.
  8384. */
  8385. #if !defined(DUK_DEBUG_H_INCLUDED)
  8386. #define DUK_DEBUG_H_INCLUDED
  8387. #if defined(DUK_USE_DEBUG)
  8388. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 0)
  8389. #define DUK_D(x) x
  8390. #else
  8391. #define DUK_D(x) do { } while (0) /* omit */
  8392. #endif
  8393. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 1)
  8394. #define DUK_DD(x) x
  8395. #else
  8396. #define DUK_DD(x) do { } while (0) /* omit */
  8397. #endif
  8398. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  8399. #define DUK_DDD(x) x
  8400. #else
  8401. #define DUK_DDD(x) do { } while (0) /* omit */
  8402. #endif
  8403. /*
  8404. * Exposed debug macros: debugging enabled
  8405. */
  8406. #if defined(DUK_USE_VARIADIC_MACROS)
  8407. /* Note: combining __FILE__, __LINE__, and __func__ into fmt would be
  8408. * possible compile time, but waste some space with shared function names.
  8409. */
  8410. #define DUK__DEBUG_LOG(lev,...) duk_debug_log((duk_int_t) (lev), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, DUK_FUNC_MACRO, __VA_ARGS__);
  8411. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 0)
  8412. #define DUK_DPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DEBUG, __VA_ARGS__)
  8413. #else
  8414. #define DUK_DPRINT(...)
  8415. #endif
  8416. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 1)
  8417. #define DUK_DDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDEBUG, __VA_ARGS__)
  8418. #else
  8419. #define DUK_DDPRINT(...)
  8420. #endif
  8421. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  8422. #define DUK_DDDPRINT(...) DUK__DEBUG_LOG(DUK_LEVEL_DDDEBUG, __VA_ARGS__)
  8423. #else
  8424. #define DUK_DDDPRINT(...)
  8425. #endif
  8426. #else /* DUK_USE_VARIADIC_MACROS */
  8427. #define DUK__DEBUG_STASH(lev) \
  8428. (void) DUK_SNPRINTF(duk_debug_file_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FILE_MACRO), \
  8429. (void) (duk_debug_file_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0), \
  8430. (void) (duk_debug_line_stash = (duk_int_t) DUK_LINE_MACRO), \
  8431. (void) DUK_SNPRINTF(duk_debug_func_stash, DUK_DEBUG_STASH_SIZE, "%s", (const char *) DUK_FUNC_MACRO), \
  8432. (void) (duk_debug_func_stash[DUK_DEBUG_STASH_SIZE - 1] = (char) 0), \
  8433. (void) (duk_debug_level_stash = (lev))
  8434. /* Without variadic macros resort to comma expression trickery to handle debug
  8435. * prints. This generates a lot of harmless warnings. These hacks are not
  8436. * needed normally because DUK_D() and friends will hide the entire debug log
  8437. * statement from the compiler.
  8438. */
  8439. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 0)
  8440. #define DUK_DPRINT DUK__DEBUG_STASH(DUK_LEVEL_DEBUG), (void) duk_debug_log /* args go here in parens */
  8441. #else
  8442. #define DUK_DPRINT 0 && /* args go here as a comma expression in parens */
  8443. #endif
  8444. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 1)
  8445. #define DUK_DDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDEBUG), (void) duk_debug_log /* args go here in parens */
  8446. #else
  8447. #define DUK_DDPRINT 0 && /* args */
  8448. #endif
  8449. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  8450. #define DUK_DDDPRINT DUK__DEBUG_STASH(DUK_LEVEL_DDDEBUG), (void) duk_debug_log /* args go here in parens */
  8451. #else
  8452. #define DUK_DDDPRINT 0 && /* args */
  8453. #endif
  8454. #endif /* DUK_USE_VARIADIC_MACROS */
  8455. #else /* DUK_USE_DEBUG */
  8456. /*
  8457. * Exposed debug macros: debugging disabled
  8458. */
  8459. #define DUK_D(x) do { } while (0) /* omit */
  8460. #define DUK_DD(x) do { } while (0) /* omit */
  8461. #define DUK_DDD(x) do { } while (0) /* omit */
  8462. #if defined(DUK_USE_VARIADIC_MACROS)
  8463. #define DUK_DPRINT(...)
  8464. #define DUK_DDPRINT(...)
  8465. #define DUK_DDDPRINT(...)
  8466. #else /* DUK_USE_VARIADIC_MACROS */
  8467. #define DUK_DPRINT 0 && /* args go here as a comma expression in parens */
  8468. #define DUK_DDPRINT 0 && /* args */
  8469. #define DUK_DDDPRINT 0 && /* args */
  8470. #endif /* DUK_USE_VARIADIC_MACROS */
  8471. #endif /* DUK_USE_DEBUG */
  8472. /*
  8473. * Structs
  8474. */
  8475. #if defined(DUK_USE_DEBUG)
  8476. struct duk_fixedbuffer {
  8477. duk_uint8_t *buffer;
  8478. duk_size_t length;
  8479. duk_size_t offset;
  8480. duk_bool_t truncated;
  8481. };
  8482. #endif
  8483. /*
  8484. * Prototypes
  8485. */
  8486. #if defined(DUK_USE_DEBUG)
  8487. DUK_INTERNAL_DECL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap);
  8488. #if 0 /*unused*/
  8489. DUK_INTERNAL_DECL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...);
  8490. #endif
  8491. DUK_INTERNAL_DECL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size);
  8492. #if defined(DUK_USE_VARIADIC_MACROS)
  8493. DUK_INTERNAL_DECL void duk_debug_log(duk_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...);
  8494. #else /* DUK_USE_VARIADIC_MACROS */
  8495. /* parameter passing, not thread safe */
  8496. #define DUK_DEBUG_STASH_SIZE 128
  8497. #if !defined(DUK_SINGLE_FILE)
  8498. DUK_INTERNAL_DECL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
  8499. DUK_INTERNAL_DECL duk_int_t duk_debug_line_stash;
  8500. DUK_INTERNAL_DECL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
  8501. DUK_INTERNAL_DECL duk_int_t duk_debug_level_stash;
  8502. #endif
  8503. DUK_INTERNAL_DECL void duk_debug_log(const char *fmt, ...);
  8504. #endif /* DUK_USE_VARIADIC_MACROS */
  8505. DUK_INTERNAL_DECL void duk_fb_put_bytes(duk_fixedbuffer *fb, const duk_uint8_t *buffer, duk_size_t length);
  8506. DUK_INTERNAL_DECL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x);
  8507. DUK_INTERNAL_DECL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x);
  8508. DUK_INTERNAL_DECL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...);
  8509. DUK_INTERNAL_DECL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size);
  8510. DUK_INTERNAL_DECL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb);
  8511. #endif /* DUK_USE_DEBUG */
  8512. #endif /* DUK_DEBUG_H_INCLUDED */
  8513. /* #include duk_error.h */
  8514. #line 1 "duk_error.h"
  8515. /*
  8516. * Error handling macros, assertion macro, error codes.
  8517. *
  8518. * There are three types of 'errors':
  8519. *
  8520. * 1. Ordinary errors relative to a thread, cause a longjmp, catchable.
  8521. * 2. Fatal errors relative to a heap, cause fatal handler to be called.
  8522. * 3. Fatal errors without context, cause the default (not heap specific)
  8523. * fatal handler to be called.
  8524. *
  8525. * Fatal errors without context are used by debug code such as assertions.
  8526. * By providing a fatal error handler for a Duktape heap, user code can
  8527. * avoid fatal errors without context in non-debug builds.
  8528. */
  8529. #if !defined(DUK_ERROR_H_INCLUDED)
  8530. #define DUK_ERROR_H_INCLUDED
  8531. /*
  8532. * Error codes: defined in duktape.h
  8533. *
  8534. * Error codes are used as a shorthand to throw exceptions from inside
  8535. * the implementation. The appropriate ECMAScript object is constructed
  8536. * based on the code. ECMAScript code throws objects directly. The error
  8537. * codes are defined in the public API header because they are also used
  8538. * by calling code.
  8539. */
  8540. /*
  8541. * Normal error
  8542. *
  8543. * Normal error is thrown with a longjmp() through the current setjmp()
  8544. * catchpoint record in the duk_heap. The 'curr_thread' of the duk_heap
  8545. * identifies the throwing thread.
  8546. *
  8547. * Error formatting is usually unnecessary. The error macros provide a
  8548. * zero argument version (no formatting) and separate macros for small
  8549. * argument counts. Variadic macros are not used to avoid portability
  8550. * issues and avoid the need for stash-based workarounds when they're not
  8551. * available. Vararg calls are avoided for non-formatted error calls
  8552. * because vararg call sites are larger than normal, and there are a lot
  8553. * of call sites with no formatting.
  8554. *
  8555. * Note that special formatting provided by debug macros is NOT available.
  8556. *
  8557. * The _RAW variants allow the caller to specify file and line. This makes
  8558. * it easier to write checked calls which want to use the call site of the
  8559. * checked function, not the error macro call inside the checked function.
  8560. */
  8561. #if defined(DUK_USE_VERBOSE_ERRORS)
  8562. /* Because there are quite many call sites, pack error code (require at most
  8563. * 8-bit) into a single argument.
  8564. */
  8565. #define DUK_ERROR(thr,err,msg) do { \
  8566. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \
  8567. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8568. duk_err_handle_error((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (msg)); \
  8569. } while (0)
  8570. #define DUK_ERROR_RAW(thr,file,line,err,msg) do { \
  8571. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \
  8572. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8573. duk_err_handle_error((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (msg)); \
  8574. } while (0)
  8575. #define DUK_ERROR_FMT1(thr,err,fmt,arg1) do { \
  8576. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \
  8577. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8578. duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1)); \
  8579. } while (0)
  8580. #define DUK_ERROR_RAW_FMT1(thr,file,line,err,fmt,arg1) do { \
  8581. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \
  8582. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8583. duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1)); \
  8584. } while (0)
  8585. #define DUK_ERROR_FMT2(thr,err,fmt,arg1,arg2) do { \
  8586. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \
  8587. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8588. duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2)); \
  8589. } while (0)
  8590. #define DUK_ERROR_RAW_FMT2(thr,file,line,err,fmt,arg1,arg2) do { \
  8591. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \
  8592. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8593. duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2)); \
  8594. } while (0)
  8595. #define DUK_ERROR_FMT3(thr,err,fmt,arg1,arg2,arg3) do { \
  8596. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \
  8597. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8598. duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3)); \
  8599. } while (0)
  8600. #define DUK_ERROR_RAW_FMT3(thr,file,line,err,fmt,arg1,arg2,arg3) do { \
  8601. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \
  8602. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8603. duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3)); \
  8604. } while (0)
  8605. #define DUK_ERROR_FMT4(thr,err,fmt,arg1,arg2,arg3,arg4) do { \
  8606. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) DUK_LINE_MACRO; \
  8607. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8608. duk_err_handle_error_fmt((thr), DUK_FILE_MACRO, (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3), (arg4)); \
  8609. } while (0)
  8610. #define DUK_ERROR_RAW_FMT4(thr,file,line,err,fmt,arg1,arg2,arg3,arg4) do { \
  8611. duk_errcode_t duk__err = (err); duk_int_t duk__line = (duk_int_t) (line); \
  8612. DUK_ASSERT(duk__err >= 0 && duk__err <= 0xff); DUK_ASSERT(duk__line >= 0 && duk__line <= 0x00ffffffL); \
  8613. duk_err_handle_error_fmt((thr), (file), (((duk_uint_t) duk__err) << 24) | ((duk_uint_t) duk__line), (fmt), (arg1), (arg2), (arg3), (arg4)); \
  8614. } while (0)
  8615. #else /* DUK_USE_VERBOSE_ERRORS */
  8616. #define DUK_ERROR(thr,err,msg) duk_err_handle_error((thr), (err))
  8617. #define DUK_ERROR_RAW(thr,file,line,err,msg) duk_err_handle_error((thr), (err))
  8618. #define DUK_ERROR_FMT1(thr,err,fmt,arg1) DUK_ERROR((thr),(err),(fmt))
  8619. #define DUK_ERROR_RAW_FMT1(thr,file,line,err,fmt,arg1) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt))
  8620. #define DUK_ERROR_FMT2(thr,err,fmt,arg1,arg2) DUK_ERROR((thr),(err),(fmt))
  8621. #define DUK_ERROR_RAW_FMT2(thr,file,line,err,fmt,arg1,arg2) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt))
  8622. #define DUK_ERROR_FMT3(thr,err,fmt,arg1,arg2,arg3) DUK_ERROR((thr),(err),(fmt))
  8623. #define DUK_ERROR_RAW_FMT3(thr,file,line,err,fmt,arg1,arg2,arg3) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt))
  8624. #define DUK_ERROR_FMT4(thr,err,fmt,arg1,arg2,arg3,arg4) DUK_ERROR((thr),(err),(fmt))
  8625. #define DUK_ERROR_RAW_FMT4(thr,file,line,err,fmt,arg1,arg2,arg3,arg4) DUK_ERROR_RAW((thr),(file),(line),(err),(fmt))
  8626. #endif /* DUK_USE_VERBOSE_ERRORS */
  8627. /*
  8628. * Fatal error without context
  8629. *
  8630. * The macro is an expression to make it compatible with DUK_ASSERT_EXPR().
  8631. */
  8632. #define DUK_FATAL_WITHOUT_CONTEXT(msg) \
  8633. duk_default_fatal_handler(NULL, (msg))
  8634. /*
  8635. * Error throwing helpers
  8636. *
  8637. * The goal is to provide verbose and configurable error messages. Call
  8638. * sites should be clean in source code and compile to a small footprint.
  8639. * Small footprint is also useful for performance because small cold paths
  8640. * reduce code cache pressure. Adding macros here only makes sense if there
  8641. * are enough call sites to get concrete benefits.
  8642. *
  8643. * DUK_ERROR_xxx() macros are generic and can be used anywhere.
  8644. *
  8645. * DUK_DCERROR_xxx() macros can only be used in Duktape/C functions where
  8646. * the "return DUK_RET_xxx;" shorthand is available for low memory targets.
  8647. * The DUK_DCERROR_xxx() macros always either throw or perform a
  8648. * 'return DUK_RET_xxx' from the calling function.
  8649. */
  8650. #if defined(DUK_USE_VERBOSE_ERRORS)
  8651. /* Verbose errors with key/value summaries (non-paranoid) or without key/value
  8652. * summaries (paranoid, for some security sensitive environments), the paranoid
  8653. * vs. non-paranoid distinction affects only a few specific errors.
  8654. */
  8655. #if defined(DUK_USE_PARANOID_ERRORS)
  8656. #define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,idx,expectname,lowmemstr) do { \
  8657. duk_err_require_type_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (idx), (expectname)); \
  8658. } while (0)
  8659. #else /* DUK_USE_PARANOID_ERRORS */
  8660. #define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,idx,expectname,lowmemstr) do { \
  8661. duk_err_require_type_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (idx), (expectname)); \
  8662. } while (0)
  8663. #endif /* DUK_USE_PARANOID_ERRORS */
  8664. #define DUK_ERROR_INTERNAL(thr) do { \
  8665. duk_err_error_internal((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \
  8666. } while (0)
  8667. #define DUK_DCERROR_INTERNAL(thr) do { \
  8668. DUK_ERROR_INTERNAL((thr)); \
  8669. return 0; \
  8670. } while (0)
  8671. #define DUK_ERROR_ALLOC_FAILED(thr) do { \
  8672. duk_err_error_alloc_failed((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \
  8673. } while (0)
  8674. #define DUK_ERROR_UNSUPPORTED(thr) do { \
  8675. DUK_ERROR((thr), DUK_ERR_ERROR, DUK_STR_UNSUPPORTED); \
  8676. } while (0)
  8677. #define DUK_ERROR_ERROR(thr,msg) do { \
  8678. duk_err_error((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \
  8679. } while (0)
  8680. #define DUK_ERROR_RANGE_INDEX(thr,idx) do { \
  8681. duk_err_range_index((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (idx)); \
  8682. } while (0)
  8683. #define DUK_ERROR_RANGE_PUSH_BEYOND(thr) do { \
  8684. duk_err_range_push_beyond((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \
  8685. } while (0)
  8686. #define DUK_ERROR_RANGE_INVALID_ARGS(thr) do { \
  8687. DUK_ERROR_RANGE((thr), DUK_STR_INVALID_ARGS); \
  8688. } while (0)
  8689. #define DUK_DCERROR_RANGE_INVALID_ARGS(thr) do { \
  8690. DUK_ERROR_RANGE_INVALID_ARGS((thr)); \
  8691. return 0; \
  8692. } while (0)
  8693. #define DUK_ERROR_RANGE_INVALID_COUNT(thr) do { \
  8694. DUK_ERROR_RANGE((thr), DUK_STR_INVALID_COUNT); \
  8695. } while (0)
  8696. #define DUK_DCERROR_RANGE_INVALID_COUNT(thr) do { \
  8697. DUK_ERROR_RANGE_INVALID_COUNT((thr)); \
  8698. return 0; \
  8699. } while (0)
  8700. #define DUK_ERROR_RANGE_INVALID_LENGTH(thr) do { \
  8701. DUK_ERROR_RANGE((thr), DUK_STR_INVALID_LENGTH); \
  8702. } while (0)
  8703. #define DUK_DCERROR_RANGE_INVALID_LENGTH(thr) do { \
  8704. DUK_ERROR_RANGE_INVALID_LENGTH((thr)); \
  8705. return 0; \
  8706. } while (0)
  8707. #define DUK_ERROR_RANGE(thr,msg) do { \
  8708. duk_err_range((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO, (msg)); \
  8709. } while (0)
  8710. #define DUK_ERROR_EVAL(thr,msg) do { \
  8711. DUK_ERROR((thr), DUK_ERR_EVAL_ERROR, (msg)); \
  8712. } while (0)
  8713. #define DUK_ERROR_REFERENCE(thr,msg) do { \
  8714. DUK_ERROR((thr), DUK_ERR_REFERENCE_ERROR, (msg)); \
  8715. } while (0)
  8716. #define DUK_ERROR_SYNTAX(thr,msg) do { \
  8717. DUK_ERROR((thr), DUK_ERR_SYNTAX_ERROR, (msg)); \
  8718. } while (0)
  8719. #define DUK_ERROR_TYPE_INVALID_ARGS(thr) do { \
  8720. duk_err_type_invalid_args((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \
  8721. } while (0)
  8722. #define DUK_DCERROR_TYPE_INVALID_ARGS(thr) do { \
  8723. DUK_ERROR_TYPE_INVALID_ARGS((thr)); \
  8724. return 0; \
  8725. } while (0)
  8726. #define DUK_ERROR_TYPE_INVALID_STATE(thr) do { \
  8727. duk_err_type_invalid_state((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \
  8728. } while (0)
  8729. #define DUK_DCERROR_TYPE_INVALID_STATE(thr) do { \
  8730. DUK_ERROR_TYPE_INVALID_STATE((thr)); \
  8731. return 0; \
  8732. } while (0)
  8733. #define DUK_ERROR_TYPE_INVALID_TRAP_RESULT(thr) do { \
  8734. duk_err_type_invalid_trap_result((thr), DUK_FILE_MACRO, (duk_int_t) DUK_LINE_MACRO); \
  8735. } while (0)
  8736. #define DUK_DCERROR_TYPE_INVALID_TRAP_RESULT(thr) do { \
  8737. DUK_ERROR_TYPE((thr), DUK_STR_INVALID_TRAP_RESULT); \
  8738. } while (0)
  8739. #define DUK_ERROR_TYPE(thr,msg) do { \
  8740. DUK_ERROR((thr), DUK_ERR_TYPE_ERROR, (msg)); \
  8741. } while (0)
  8742. #define DUK_ERROR_URI(thr,msg) do { \
  8743. DUK_ERROR((thr), DUK_ERR_URI_ERROR, (msg)); \
  8744. } while (0)
  8745. #else /* DUK_USE_VERBOSE_ERRORS */
  8746. /* Non-verbose errors for low memory targets: no file, line, or message. */
  8747. #define DUK_ERROR_REQUIRE_TYPE_INDEX(thr,idx,expectname,lowmemstr) do { \
  8748. duk_err_type((thr)); \
  8749. } while (0)
  8750. #define DUK_ERROR_INTERNAL(thr) do { \
  8751. duk_err_error((thr)); \
  8752. } while (0)
  8753. #define DUK_DCERROR_INTERNAL(thr) do { \
  8754. DUK_UNREF((thr)); \
  8755. return DUK_RET_ERROR; \
  8756. } while (0)
  8757. #define DUK_ERROR_ALLOC_FAILED(thr) do { \
  8758. duk_err_error((thr)); \
  8759. } while (0)
  8760. #define DUK_ERROR_UNSUPPORTED(thr) do { \
  8761. duk_err_error((thr)); \
  8762. } while (0)
  8763. #define DUK_ERROR_ERROR(thr,msg) do { \
  8764. duk_err_error((thr)); \
  8765. } while (0)
  8766. #define DUK_ERROR_RANGE_INDEX(thr,idx) do { \
  8767. duk_err_range((thr)); \
  8768. } while (0)
  8769. #define DUK_ERROR_RANGE_PUSH_BEYOND(thr) do { \
  8770. duk_err_range((thr)); \
  8771. } while (0)
  8772. #define DUK_ERROR_RANGE_INVALID_ARGS(thr) do { \
  8773. duk_err_range((thr)); \
  8774. } while (0)
  8775. #define DUK_DCERROR_RANGE_INVALID_ARGS(thr) do { \
  8776. DUK_UNREF((thr)); \
  8777. return DUK_RET_RANGE_ERROR; \
  8778. } while (0)
  8779. #define DUK_ERROR_RANGE_INVALID_COUNT(thr) do { \
  8780. duk_err_range((thr)); \
  8781. } while (0)
  8782. #define DUK_DCERROR_RANGE_INVALID_COUNT(thr) do { \
  8783. DUK_UNREF((thr)); \
  8784. return DUK_RET_RANGE_ERROR; \
  8785. } while (0)
  8786. #define DUK_ERROR_RANGE_INVALID_LENGTH(thr) do { \
  8787. duk_err_range((thr)); \
  8788. } while (0)
  8789. #define DUK_DCERROR_RANGE_INVALID_LENGTH(thr) do { \
  8790. DUK_UNREF((thr)); \
  8791. return DUK_RET_RANGE_ERROR; \
  8792. } while (0)
  8793. #define DUK_ERROR_RANGE(thr,msg) do { \
  8794. duk_err_range((thr)); \
  8795. } while (0)
  8796. #define DUK_ERROR_EVAL(thr,msg) do { \
  8797. duk_err_eval((thr)); \
  8798. } while (0)
  8799. #define DUK_ERROR_REFERENCE(thr,msg) do { \
  8800. duk_err_reference((thr)); \
  8801. } while (0)
  8802. #define DUK_ERROR_SYNTAX(thr,msg) do { \
  8803. duk_err_syntax((thr)); \
  8804. } while (0)
  8805. #define DUK_ERROR_TYPE_INVALID_ARGS(thr) do { \
  8806. duk_err_type((thr)); \
  8807. } while (0)
  8808. #define DUK_DCERROR_TYPE_INVALID_ARGS(thr) do { \
  8809. DUK_UNREF((thr)); \
  8810. return DUK_RET_TYPE_ERROR; \
  8811. } while (0)
  8812. #define DUK_ERROR_TYPE_INVALID_STATE(thr) do { \
  8813. duk_err_type((thr)); \
  8814. } while (0)
  8815. #define DUK_DCERROR_TYPE_INVALID_STATE(thr) do { \
  8816. duk_err_type((thr)); \
  8817. } while (0)
  8818. #define DUK_ERROR_TYPE_INVALID_TRAP_RESULT(thr) do { \
  8819. duk_err_type((thr)); \
  8820. } while (0)
  8821. #define DUK_DCERROR_TYPE_INVALID_TRAP_RESULT(thr) do { \
  8822. DUK_UNREF((thr)); \
  8823. return DUK_RET_TYPE_ERROR; \
  8824. } while (0)
  8825. #define DUK_ERROR_TYPE_INVALID_TRAP_RESULT(thr) do { \
  8826. duk_err_type((thr)); \
  8827. } while (0)
  8828. #define DUK_ERROR_TYPE(thr,msg) do { \
  8829. duk_err_type((thr)); \
  8830. } while (0)
  8831. #define DUK_ERROR_URI(thr,msg) do { \
  8832. duk_err_uri((thr)); \
  8833. } while (0)
  8834. #endif /* DUK_USE_VERBOSE_ERRORS */
  8835. /*
  8836. * Assert macro: failure causes a fatal error.
  8837. *
  8838. * NOTE: since the assert macro doesn't take a heap/context argument, there's
  8839. * no way to look up a heap/context specific fatal error handler which may have
  8840. * been given by the application. Instead, assertion failures always use the
  8841. * internal default fatal error handler; it can be replaced via duk_config.h
  8842. * and then applies to all Duktape heaps.
  8843. */
  8844. #if defined(DUK_USE_ASSERTIONS)
  8845. /* The message should be a compile time constant without formatting (less risk);
  8846. * we don't care about assertion text size because they're not used in production
  8847. * builds.
  8848. */
  8849. #define DUK_ASSERT(x) do { \
  8850. if (!(x)) { \
  8851. DUK_FATAL_WITHOUT_CONTEXT("assertion failed: " #x \
  8852. " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"); \
  8853. } \
  8854. } while (0)
  8855. /* Assertion compatible inside a comma expression, evaluates to void. */
  8856. #define DUK_ASSERT_EXPR(x) \
  8857. ((void) ((x) ? 0 : (DUK_FATAL_WITHOUT_CONTEXT("assertion failed: " #x \
  8858. " (" DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO) ")"), 0)))
  8859. #else /* DUK_USE_ASSERTIONS */
  8860. #define DUK_ASSERT(x) do { /* assertion omitted */ } while (0)
  8861. #define DUK_ASSERT_EXPR(x) ((void) 0)
  8862. #endif /* DUK_USE_ASSERTIONS */
  8863. /* this variant is used when an assert would generate a compile warning by
  8864. * being always true (e.g. >= 0 comparison for an unsigned value
  8865. */
  8866. #define DUK_ASSERT_DISABLE(x) do { /* assertion disabled */ } while (0)
  8867. /*
  8868. * Assertion helpers
  8869. */
  8870. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  8871. #define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) do { \
  8872. DUK_ASSERT((h) == NULL || DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) (h)) > 0); \
  8873. } while (0)
  8874. #define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) do { \
  8875. if ((tv) != NULL && DUK_TVAL_IS_HEAP_ALLOCATED((tv))) { \
  8876. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(DUK_TVAL_GET_HEAPHDR((tv))) > 0); \
  8877. } \
  8878. } while (0)
  8879. #else
  8880. #define DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(h) /* no refcount check */
  8881. #define DUK_ASSERT_REFCOUNT_NONZERO_TVAL(tv) /* no refcount check */
  8882. #endif
  8883. #define DUK_ASSERT_TOP(ctx,n) DUK_ASSERT((duk_idx_t) duk_get_top((ctx)) == (duk_idx_t) (n))
  8884. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_PACKED_TVAL)
  8885. #define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) do { \
  8886. duk_double_union duk__assert_tmp_du; \
  8887. duk__assert_tmp_du.d = (dval); \
  8888. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&duk__assert_tmp_du)); \
  8889. } while (0)
  8890. #else
  8891. #define DUK_ASSERT_DOUBLE_IS_NORMALIZED(dval) /* nop */
  8892. #endif
  8893. #define DUK_ASSERT_VS_SPACE(thr) \
  8894. DUK_ASSERT(thr->valstack_top < thr->valstack_end)
  8895. /*
  8896. * Helper to initialize a memory area (e.g. struct) with garbage when
  8897. * assertions enabled.
  8898. */
  8899. #if defined(DUK_USE_ASSERTIONS)
  8900. #define DUK_ASSERT_SET_GARBAGE(ptr,size) do { \
  8901. duk_memset_unsafe((void *) (ptr), 0x5a, size); \
  8902. } while (0)
  8903. #else
  8904. #define DUK_ASSERT_SET_GARBAGE(ptr,size) do {} while (0)
  8905. #endif
  8906. /*
  8907. * Helper for valstack space
  8908. *
  8909. * Caller of DUK_ASSERT_VALSTACK_SPACE() estimates the number of free stack entries
  8910. * required for its own use, and any child calls which are not (a) Duktape API calls
  8911. * or (b) Duktape calls which involve extending the valstack (e.g. getter call).
  8912. */
  8913. #define DUK_VALSTACK_ASSERT_EXTRA 5 /* this is added to checks to allow for Duktape
  8914. * API calls in addition to function's own use
  8915. */
  8916. #if defined(DUK_USE_ASSERTIONS)
  8917. #define DUK_ASSERT_VALSTACK_SPACE(thr,n) do { \
  8918. DUK_ASSERT((thr) != NULL); \
  8919. DUK_ASSERT((thr)->valstack_end - (thr)->valstack_top >= (n) + DUK_VALSTACK_ASSERT_EXTRA); \
  8920. } while (0)
  8921. #else
  8922. #define DUK_ASSERT_VALSTACK_SPACE(thr,n) /* no valstack space check */
  8923. #endif
  8924. /*
  8925. * Prototypes
  8926. */
  8927. #if defined(DUK_USE_VERBOSE_ERRORS)
  8928. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *msg));
  8929. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error_fmt(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *fmt, ...));
  8930. #else /* DUK_USE_VERBOSE_ERRORS */
  8931. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code));
  8932. #endif /* DUK_USE_VERBOSE_ERRORS */
  8933. #if defined(DUK_USE_VERBOSE_ERRORS)
  8934. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code, const char *msg, const char *filename, duk_int_t line));
  8935. #else
  8936. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code));
  8937. #endif
  8938. DUK_NORETURN(DUK_INTERNAL_DECL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc));
  8939. #define DUK_AUGMENT_FLAG_NOBLAME_FILELINE (1U << 0) /* if set, don't blame C file/line for .fileName and .lineNumber */
  8940. #define DUK_AUGMENT_FLAG_SKIP_ONE (1U << 1) /* if set, skip topmost activation in traceback construction */
  8941. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  8942. DUK_INTERNAL_DECL void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *filename, duk_int_t line, duk_small_uint_t flags);
  8943. #endif
  8944. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  8945. DUK_INTERNAL_DECL void duk_err_augment_error_throw(duk_hthread *thr);
  8946. #endif
  8947. #if defined(DUK_USE_VERBOSE_ERRORS)
  8948. #if defined(DUK_USE_PARANOID_ERRORS)
  8949. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t idx, const char *expect_name));
  8950. #else
  8951. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t idx, const char *expect_name));
  8952. #endif
  8953. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_error_internal(duk_hthread *thr, const char *filename, duk_int_t linenumber));
  8954. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_error_alloc_failed(duk_hthread *thr, const char *filename, duk_int_t linenumber));
  8955. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_error(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message));
  8956. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t idx));
  8957. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range_push_beyond(duk_hthread *thr, const char *filename, duk_int_t linenumber));
  8958. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message));
  8959. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_type_invalid_args(duk_hthread *thr, const char *filename, duk_int_t linenumber));
  8960. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_type_invalid_state(duk_hthread *thr, const char *filename, duk_int_t linenumber));
  8961. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_type_invalid_trap_result(duk_hthread *thr, const char *filename, duk_int_t linenumber));
  8962. #else /* DUK_VERBOSE_ERRORS */
  8963. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_error(duk_hthread *thr));
  8964. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_range(duk_hthread *thr));
  8965. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_eval(duk_hthread *thr));
  8966. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_reference(duk_hthread *thr));
  8967. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_syntax(duk_hthread *thr));
  8968. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_type(duk_hthread *thr));
  8969. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_uri(duk_hthread *thr));
  8970. #endif /* DUK_VERBOSE_ERRORS */
  8971. DUK_NORETURN(DUK_INTERNAL_DECL void duk_err_longjmp(duk_hthread *thr));
  8972. DUK_NORETURN(DUK_INTERNAL_DECL void duk_default_fatal_handler(void *udata, const char *msg));
  8973. DUK_INTERNAL_DECL void duk_err_setup_ljstate1(duk_hthread *thr, duk_small_uint_t lj_type, duk_tval *tv_val);
  8974. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  8975. DUK_INTERNAL_DECL void duk_err_check_debugger_integration(duk_hthread *thr);
  8976. #endif
  8977. DUK_INTERNAL_DECL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t err_code);
  8978. #endif /* DUK_ERROR_H_INCLUDED */
  8979. /* #include duk_unicode.h */
  8980. #line 1 "duk_unicode.h"
  8981. /*
  8982. * Unicode helpers
  8983. */
  8984. #if !defined(DUK_UNICODE_H_INCLUDED)
  8985. #define DUK_UNICODE_H_INCLUDED
  8986. /*
  8987. * UTF-8 / XUTF-8 / CESU-8 constants
  8988. */
  8989. #define DUK_UNICODE_MAX_XUTF8_LENGTH 7 /* up to 36 bit codepoints */
  8990. #define DUK_UNICODE_MAX_XUTF8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */
  8991. #define DUK_UNICODE_MAX_CESU8_LENGTH 6 /* all codepoints up to U+10FFFF */
  8992. #define DUK_UNICODE_MAX_CESU8_BMP_LENGTH 3 /* all codepoints up to U+FFFF */
  8993. /*
  8994. * Useful Unicode codepoints
  8995. *
  8996. * Integer constants must be signed to avoid unexpected coercions
  8997. * in comparisons.
  8998. */
  8999. #define DUK_UNICODE_CP_ZWNJ 0x200cL /* zero-width non-joiner */
  9000. #define DUK_UNICODE_CP_ZWJ 0x200dL /* zero-width joiner */
  9001. #define DUK_UNICODE_CP_REPLACEMENT_CHARACTER 0xfffdL /* http://en.wikipedia.org/wiki/Replacement_character#Replacement_character */
  9002. /*
  9003. * ASCII character constants
  9004. *
  9005. * C character literals like 'x' have a platform specific value and do
  9006. * not match ASCII (UTF-8) values on e.g. EBCDIC platforms. So, use
  9007. * these (admittedly awkward) constants instead. These constants must
  9008. * also have signed values to avoid unexpected coercions in comparisons.
  9009. *
  9010. * http://en.wikipedia.org/wiki/ASCII
  9011. */
  9012. #define DUK_ASC_NUL 0x00
  9013. #define DUK_ASC_SOH 0x01
  9014. #define DUK_ASC_STX 0x02
  9015. #define DUK_ASC_ETX 0x03
  9016. #define DUK_ASC_EOT 0x04
  9017. #define DUK_ASC_ENQ 0x05
  9018. #define DUK_ASC_ACK 0x06
  9019. #define DUK_ASC_BEL 0x07
  9020. #define DUK_ASC_BS 0x08
  9021. #define DUK_ASC_HT 0x09
  9022. #define DUK_ASC_LF 0x0a
  9023. #define DUK_ASC_VT 0x0b
  9024. #define DUK_ASC_FF 0x0c
  9025. #define DUK_ASC_CR 0x0d
  9026. #define DUK_ASC_SO 0x0e
  9027. #define DUK_ASC_SI 0x0f
  9028. #define DUK_ASC_DLE 0x10
  9029. #define DUK_ASC_DC1 0x11
  9030. #define DUK_ASC_DC2 0x12
  9031. #define DUK_ASC_DC3 0x13
  9032. #define DUK_ASC_DC4 0x14
  9033. #define DUK_ASC_NAK 0x15
  9034. #define DUK_ASC_SYN 0x16
  9035. #define DUK_ASC_ETB 0x17
  9036. #define DUK_ASC_CAN 0x18
  9037. #define DUK_ASC_EM 0x19
  9038. #define DUK_ASC_SUB 0x1a
  9039. #define DUK_ASC_ESC 0x1b
  9040. #define DUK_ASC_FS 0x1c
  9041. #define DUK_ASC_GS 0x1d
  9042. #define DUK_ASC_RS 0x1e
  9043. #define DUK_ASC_US 0x1f
  9044. #define DUK_ASC_SPACE 0x20
  9045. #define DUK_ASC_EXCLAMATION 0x21
  9046. #define DUK_ASC_DOUBLEQUOTE 0x22
  9047. #define DUK_ASC_HASH 0x23
  9048. #define DUK_ASC_DOLLAR 0x24
  9049. #define DUK_ASC_PERCENT 0x25
  9050. #define DUK_ASC_AMP 0x26
  9051. #define DUK_ASC_SINGLEQUOTE 0x27
  9052. #define DUK_ASC_LPAREN 0x28
  9053. #define DUK_ASC_RPAREN 0x29
  9054. #define DUK_ASC_STAR 0x2a
  9055. #define DUK_ASC_PLUS 0x2b
  9056. #define DUK_ASC_COMMA 0x2c
  9057. #define DUK_ASC_MINUS 0x2d
  9058. #define DUK_ASC_PERIOD 0x2e
  9059. #define DUK_ASC_SLASH 0x2f
  9060. #define DUK_ASC_0 0x30
  9061. #define DUK_ASC_1 0x31
  9062. #define DUK_ASC_2 0x32
  9063. #define DUK_ASC_3 0x33
  9064. #define DUK_ASC_4 0x34
  9065. #define DUK_ASC_5 0x35
  9066. #define DUK_ASC_6 0x36
  9067. #define DUK_ASC_7 0x37
  9068. #define DUK_ASC_8 0x38
  9069. #define DUK_ASC_9 0x39
  9070. #define DUK_ASC_COLON 0x3a
  9071. #define DUK_ASC_SEMICOLON 0x3b
  9072. #define DUK_ASC_LANGLE 0x3c
  9073. #define DUK_ASC_EQUALS 0x3d
  9074. #define DUK_ASC_RANGLE 0x3e
  9075. #define DUK_ASC_QUESTION 0x3f
  9076. #define DUK_ASC_ATSIGN 0x40
  9077. #define DUK_ASC_UC_A 0x41
  9078. #define DUK_ASC_UC_B 0x42
  9079. #define DUK_ASC_UC_C 0x43
  9080. #define DUK_ASC_UC_D 0x44
  9081. #define DUK_ASC_UC_E 0x45
  9082. #define DUK_ASC_UC_F 0x46
  9083. #define DUK_ASC_UC_G 0x47
  9084. #define DUK_ASC_UC_H 0x48
  9085. #define DUK_ASC_UC_I 0x49
  9086. #define DUK_ASC_UC_J 0x4a
  9087. #define DUK_ASC_UC_K 0x4b
  9088. #define DUK_ASC_UC_L 0x4c
  9089. #define DUK_ASC_UC_M 0x4d
  9090. #define DUK_ASC_UC_N 0x4e
  9091. #define DUK_ASC_UC_O 0x4f
  9092. #define DUK_ASC_UC_P 0x50
  9093. #define DUK_ASC_UC_Q 0x51
  9094. #define DUK_ASC_UC_R 0x52
  9095. #define DUK_ASC_UC_S 0x53
  9096. #define DUK_ASC_UC_T 0x54
  9097. #define DUK_ASC_UC_U 0x55
  9098. #define DUK_ASC_UC_V 0x56
  9099. #define DUK_ASC_UC_W 0x57
  9100. #define DUK_ASC_UC_X 0x58
  9101. #define DUK_ASC_UC_Y 0x59
  9102. #define DUK_ASC_UC_Z 0x5a
  9103. #define DUK_ASC_LBRACKET 0x5b
  9104. #define DUK_ASC_BACKSLASH 0x5c
  9105. #define DUK_ASC_RBRACKET 0x5d
  9106. #define DUK_ASC_CARET 0x5e
  9107. #define DUK_ASC_UNDERSCORE 0x5f
  9108. #define DUK_ASC_GRAVE 0x60
  9109. #define DUK_ASC_LC_A 0x61
  9110. #define DUK_ASC_LC_B 0x62
  9111. #define DUK_ASC_LC_C 0x63
  9112. #define DUK_ASC_LC_D 0x64
  9113. #define DUK_ASC_LC_E 0x65
  9114. #define DUK_ASC_LC_F 0x66
  9115. #define DUK_ASC_LC_G 0x67
  9116. #define DUK_ASC_LC_H 0x68
  9117. #define DUK_ASC_LC_I 0x69
  9118. #define DUK_ASC_LC_J 0x6a
  9119. #define DUK_ASC_LC_K 0x6b
  9120. #define DUK_ASC_LC_L 0x6c
  9121. #define DUK_ASC_LC_M 0x6d
  9122. #define DUK_ASC_LC_N 0x6e
  9123. #define DUK_ASC_LC_O 0x6f
  9124. #define DUK_ASC_LC_P 0x70
  9125. #define DUK_ASC_LC_Q 0x71
  9126. #define DUK_ASC_LC_R 0x72
  9127. #define DUK_ASC_LC_S 0x73
  9128. #define DUK_ASC_LC_T 0x74
  9129. #define DUK_ASC_LC_U 0x75
  9130. #define DUK_ASC_LC_V 0x76
  9131. #define DUK_ASC_LC_W 0x77
  9132. #define DUK_ASC_LC_X 0x78
  9133. #define DUK_ASC_LC_Y 0x79
  9134. #define DUK_ASC_LC_Z 0x7a
  9135. #define DUK_ASC_LCURLY 0x7b
  9136. #define DUK_ASC_PIPE 0x7c
  9137. #define DUK_ASC_RCURLY 0x7d
  9138. #define DUK_ASC_TILDE 0x7e
  9139. #define DUK_ASC_DEL 0x7f
  9140. /*
  9141. * Miscellaneous
  9142. */
  9143. /* Uppercase A is 0x41, lowercase a is 0x61; OR 0x20 to convert uppercase
  9144. * to lowercase.
  9145. */
  9146. #define DUK_LOWERCASE_CHAR_ASCII(x) ((x) | 0x20)
  9147. /*
  9148. * Unicode tables
  9149. */
  9150. #if defined(DUK_USE_SOURCE_NONBMP)
  9151. /*
  9152. * Automatically generated by extract_chars.py, do not edit!
  9153. */
  9154. extern const duk_uint8_t duk_unicode_ids_noa[1063];
  9155. #else
  9156. /*
  9157. * Automatically generated by extract_chars.py, do not edit!
  9158. */
  9159. extern const duk_uint8_t duk_unicode_ids_noabmp[626];
  9160. #endif
  9161. #if defined(DUK_USE_SOURCE_NONBMP)
  9162. /*
  9163. * Automatically generated by extract_chars.py, do not edit!
  9164. */
  9165. extern const duk_uint8_t duk_unicode_ids_m_let_noa[42];
  9166. #else
  9167. /*
  9168. * Automatically generated by extract_chars.py, do not edit!
  9169. */
  9170. extern const duk_uint8_t duk_unicode_ids_m_let_noabmp[24];
  9171. #endif
  9172. #if defined(DUK_USE_SOURCE_NONBMP)
  9173. /*
  9174. * Automatically generated by extract_chars.py, do not edit!
  9175. */
  9176. extern const duk_uint8_t duk_unicode_idp_m_ids_noa[549];
  9177. #else
  9178. /*
  9179. * Automatically generated by extract_chars.py, do not edit!
  9180. */
  9181. extern const duk_uint8_t duk_unicode_idp_m_ids_noabmp[358];
  9182. #endif
  9183. /*
  9184. * Automatically generated by extract_caseconv.py, do not edit!
  9185. */
  9186. extern const duk_uint8_t duk_unicode_caseconv_uc[1386];
  9187. extern const duk_uint8_t duk_unicode_caseconv_lc[680];
  9188. #if defined(DUK_USE_REGEXP_CANON_WORKAROUND)
  9189. /*
  9190. * Automatically generated by extract_caseconv.py, do not edit!
  9191. */
  9192. extern const duk_uint16_t duk_unicode_re_canon_lookup[65536];
  9193. #endif
  9194. #if defined(DUK_USE_REGEXP_CANON_BITMAP)
  9195. /*
  9196. * Automatically generated by extract_caseconv.py, do not edit!
  9197. */
  9198. #define DUK_CANON_BITMAP_BLKSIZE 32
  9199. #define DUK_CANON_BITMAP_BLKSHIFT 5
  9200. #define DUK_CANON_BITMAP_BLKMASK 31
  9201. extern const duk_uint8_t duk_unicode_re_canon_bitmap[256];
  9202. #endif
  9203. /*
  9204. * Extern
  9205. */
  9206. /* duk_unicode_support.c */
  9207. #if !defined(DUK_SINGLE_FILE)
  9208. DUK_INTERNAL_DECL const duk_uint8_t duk_unicode_xutf8_markers[7];
  9209. DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_digit[2];
  9210. DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_white[22];
  9211. DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_wordchar[8];
  9212. DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_digit[4];
  9213. DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_white[24];
  9214. DUK_INTERNAL_DECL const duk_uint16_t duk_unicode_re_ranges_not_wordchar[10];
  9215. DUK_INTERNAL_DECL const duk_int8_t duk_is_idchar_tab[128];
  9216. #endif /* !DUK_SINGLE_FILE */
  9217. /*
  9218. * Prototypes
  9219. */
  9220. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp);
  9221. #if defined(DUK_USE_ASSERTIONS)
  9222. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp);
  9223. #endif
  9224. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out);
  9225. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out);
  9226. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_decode_xutf8(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_ucodepoint_t *out_cp);
  9227. DUK_INTERNAL_DECL duk_ucodepoint_t duk_unicode_decode_xutf8_checked(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end);
  9228. DUK_INTERNAL_DECL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen);
  9229. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp);
  9230. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp);
  9231. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp);
  9232. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp);
  9233. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp);
  9234. DUK_INTERNAL_DECL void duk_unicode_case_convert_string(duk_hthread *thr, duk_bool_t uppercase);
  9235. #if defined(DUK_USE_REGEXP_SUPPORT)
  9236. DUK_INTERNAL_DECL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp);
  9237. DUK_INTERNAL_DECL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t cp);
  9238. #endif
  9239. #endif /* DUK_UNICODE_H_INCLUDED */
  9240. /* #include duk_json.h */
  9241. #line 1 "duk_json.h"
  9242. /*
  9243. * Defines for JSON, especially duk_bi_json.c.
  9244. */
  9245. #if !defined(DUK_JSON_H_INCLUDED)
  9246. #define DUK_JSON_H_INCLUDED
  9247. /* Encoding/decoding flags */
  9248. #define DUK_JSON_FLAG_ASCII_ONLY (1U << 0) /* escape any non-ASCII characters */
  9249. #define DUK_JSON_FLAG_AVOID_KEY_QUOTES (1U << 1) /* avoid key quotes when key is an ASCII Identifier */
  9250. #define DUK_JSON_FLAG_EXT_CUSTOM (1U << 2) /* extended types: custom encoding */
  9251. #define DUK_JSON_FLAG_EXT_COMPATIBLE (1U << 3) /* extended types: compatible encoding */
  9252. /* How much stack to require on entry to object/array encode */
  9253. #define DUK_JSON_ENC_REQSTACK 32
  9254. /* How much stack to require on entry to object/array decode */
  9255. #define DUK_JSON_DEC_REQSTACK 32
  9256. /* How large a loop detection stack to use */
  9257. #define DUK_JSON_ENC_LOOPARRAY 64
  9258. /* Encoding state. Heap object references are all borrowed. */
  9259. typedef struct {
  9260. duk_hthread *thr;
  9261. duk_bufwriter_ctx bw; /* output bufwriter */
  9262. duk_hobject *h_replacer; /* replacer function */
  9263. duk_hstring *h_gap; /* gap (if empty string, NULL) */
  9264. duk_idx_t idx_proplist; /* explicit PropertyList */
  9265. duk_idx_t idx_loop; /* valstack index of loop detection object */
  9266. duk_small_uint_t flags;
  9267. duk_small_uint_t flag_ascii_only;
  9268. duk_small_uint_t flag_avoid_key_quotes;
  9269. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  9270. duk_small_uint_t flag_ext_custom;
  9271. duk_small_uint_t flag_ext_compatible;
  9272. duk_small_uint_t flag_ext_custom_or_compatible;
  9273. #endif
  9274. duk_uint_t recursion_depth;
  9275. duk_uint_t recursion_limit;
  9276. duk_uint_t mask_for_undefined; /* type bit mask: types which certainly produce 'undefined' */
  9277. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  9278. duk_small_uint_t stridx_custom_undefined;
  9279. duk_small_uint_t stridx_custom_nan;
  9280. duk_small_uint_t stridx_custom_neginf;
  9281. duk_small_uint_t stridx_custom_posinf;
  9282. duk_small_uint_t stridx_custom_function;
  9283. #endif
  9284. duk_hobject *visiting[DUK_JSON_ENC_LOOPARRAY]; /* indexed by recursion_depth */
  9285. } duk_json_enc_ctx;
  9286. typedef struct {
  9287. duk_hthread *thr;
  9288. const duk_uint8_t *p;
  9289. const duk_uint8_t *p_start;
  9290. const duk_uint8_t *p_end;
  9291. duk_idx_t idx_reviver;
  9292. duk_small_uint_t flags;
  9293. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  9294. duk_small_uint_t flag_ext_custom;
  9295. duk_small_uint_t flag_ext_compatible;
  9296. duk_small_uint_t flag_ext_custom_or_compatible;
  9297. #endif
  9298. duk_int_t recursion_depth;
  9299. duk_int_t recursion_limit;
  9300. } duk_json_dec_ctx;
  9301. #endif /* DUK_JSON_H_INCLUDED */
  9302. /* #include duk_js.h */
  9303. #line 1 "duk_js.h"
  9304. /*
  9305. * ECMAScript execution, support primitives.
  9306. */
  9307. #if !defined(DUK_JS_H_INCLUDED)
  9308. #define DUK_JS_H_INCLUDED
  9309. /* Flags for call handling. Lowest flags must match bytecode DUK_BC_CALL_FLAG_xxx 1:1. */
  9310. #define DUK_CALL_FLAG_TAILCALL (1U << 0) /* setup for a tail call */
  9311. #define DUK_CALL_FLAG_CONSTRUCT (1U << 1) /* constructor call (i.e. called as 'new Foo()') */
  9312. #define DUK_CALL_FLAG_CALLED_AS_EVAL (1U << 2) /* call was made using the identifier 'eval' */
  9313. #define DUK_CALL_FLAG_ALLOW_ECMATOECMA (1U << 3) /* ecma-to-ecma call with executor reuse is possible */
  9314. #define DUK_CALL_FLAG_DIRECT_EVAL (1U << 4) /* call is a direct eval call */
  9315. #define DUK_CALL_FLAG_CONSTRUCT_PROXY (1U << 5) /* handled via 'construct' proxy trap, check return value invariant(s) */
  9316. #define DUK_CALL_FLAG_DEFAULT_INSTANCE_UPDATED (1U << 6) /* prototype of 'default instance' updated, temporary flag in call handling */
  9317. /* Flags for duk_js_equals_helper(). */
  9318. #define DUK_EQUALS_FLAG_SAMEVALUE (1U << 0) /* use SameValue instead of non-strict equality */
  9319. #define DUK_EQUALS_FLAG_STRICT (1U << 1) /* use strict equality instead of non-strict equality */
  9320. /* Flags for duk_js_compare_helper(). */
  9321. #define DUK_COMPARE_FLAG_NEGATE (1U << 0) /* negate result */
  9322. #define DUK_COMPARE_FLAG_EVAL_LEFT_FIRST (1U << 1) /* eval left argument first */
  9323. /* conversions, coercions, comparison, etc */
  9324. DUK_INTERNAL_DECL duk_bool_t duk_js_toboolean(duk_tval *tv);
  9325. DUK_INTERNAL_DECL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv);
  9326. DUK_INTERNAL_DECL duk_double_t duk_js_tointeger_number(duk_double_t x);
  9327. DUK_INTERNAL_DECL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv);
  9328. DUK_INTERNAL_DECL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv);
  9329. DUK_INTERNAL_DECL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv);
  9330. DUK_INTERNAL_DECL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv);
  9331. DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_string(const duk_uint8_t *str, duk_uint32_t blen);
  9332. #if !defined(DUK_USE_HSTRING_ARRIDX)
  9333. DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_hstring_fast_known(duk_hstring *h);
  9334. DUK_INTERNAL_DECL duk_uarridx_t duk_js_to_arrayindex_hstring_fast(duk_hstring *h);
  9335. #endif
  9336. DUK_INTERNAL_DECL duk_bool_t duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_t flags);
  9337. DUK_INTERNAL_DECL duk_small_int_t duk_js_data_compare(const duk_uint8_t *buf1, const duk_uint8_t *buf2, duk_size_t len1, duk_size_t len2);
  9338. DUK_INTERNAL_DECL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2);
  9339. #if 0 /* unused */
  9340. DUK_INTERNAL_DECL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2);
  9341. #endif
  9342. DUK_INTERNAL_DECL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_t flags);
  9343. DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  9344. #if defined(DUK_USE_SYMBOL_BUILTIN)
  9345. DUK_INTERNAL_DECL duk_bool_t duk_js_instanceof_ordinary(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  9346. #endif
  9347. DUK_INTERNAL_DECL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y);
  9348. DUK_INTERNAL_DECL duk_small_uint_t duk_js_typeof_stridx(duk_tval *tv_x);
  9349. /* arithmetic */
  9350. DUK_INTERNAL_DECL double duk_js_arith_pow(double x, double y);
  9351. DUK_INTERNAL_DECL double duk_js_arith_mod(double x, double y);
  9352. #define duk_js_equals(thr,tv_x,tv_y) \
  9353. duk_js_equals_helper((thr), (tv_x), (tv_y), 0)
  9354. #define duk_js_strict_equals(tv_x,tv_y) \
  9355. duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_STRICT)
  9356. #define duk_js_samevalue(tv_x,tv_y) \
  9357. duk_js_equals_helper(NULL, (tv_x), (tv_y), DUK_EQUALS_FLAG_SAMEVALUE)
  9358. /* E5 Sections 11.8.1, 11.8.5; x < y */
  9359. #define duk_js_lessthan(thr,tv_x,tv_y) \
  9360. duk_js_compare_helper((thr), (tv_x), (tv_Y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST)
  9361. /* E5 Sections 11.8.2, 11.8.5; x > y --> y < x */
  9362. #define duk_js_greaterthan(thr,tv_x,tv_y) \
  9363. duk_js_compare_helper((thr), (tv_y), (tv_x), 0)
  9364. /* E5 Sections 11.8.3, 11.8.5; x <= y --> not (x > y) --> not (y < x) */
  9365. #define duk_js_lessthanorequal(thr,tv_x,tv_y) \
  9366. duk_js_compare_helper((thr), (tv_y), (tv_x), DUK_COMPARE_FLAG_NEGATE)
  9367. /* E5 Sections 11.8.4, 11.8.5; x >= y --> not (x < y) */
  9368. #define duk_js_greaterthanorequal(thr,tv_x,tv_y) \
  9369. duk_js_compare_helper((thr), (tv_x), (tv_y), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE)
  9370. /* identifiers and environment handling */
  9371. #if 0 /*unused*/
  9372. DUK_INTERNAL duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name);
  9373. #endif
  9374. DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_bool_t throw_flag);
  9375. DUK_INTERNAL_DECL duk_bool_t duk_js_getvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_bool_t throw_flag);
  9376. DUK_INTERNAL_DECL void duk_js_putvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name, duk_tval *val, duk_bool_t strict);
  9377. DUK_INTERNAL_DECL void duk_js_putvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_bool_t strict);
  9378. #if 0 /*unused*/
  9379. DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_envrec(duk_hthread *thr, duk_hobject *env, duk_hstring *name);
  9380. #endif
  9381. DUK_INTERNAL_DECL duk_bool_t duk_js_delvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name);
  9382. DUK_INTERNAL_DECL duk_bool_t duk_js_declvar_activation(duk_hthread *thr, duk_activation *act, duk_hstring *name, duk_tval *val, duk_small_uint_t prop_flags, duk_bool_t is_func_decl);
  9383. DUK_INTERNAL_DECL void duk_js_init_activation_environment_records_delayed(duk_hthread *thr, duk_activation *act);
  9384. DUK_INTERNAL_DECL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env);
  9385. DUK_INTERNAL_DECL duk_hobject *duk_create_activation_environment_record(duk_hthread *thr, duk_hobject *func, duk_size_t bottom_byteoff);
  9386. DUK_INTERNAL_DECL void duk_js_push_closure(duk_hthread *thr,
  9387. duk_hcompfunc *fun_temp,
  9388. duk_hobject *outer_var_env,
  9389. duk_hobject *outer_lex_env,
  9390. duk_bool_t add_auto_proto);
  9391. /* call handling */
  9392. DUK_INTERNAL_DECL duk_int_t duk_handle_call_unprotected(duk_hthread *thr, duk_idx_t idx_func, duk_small_uint_t call_flags);
  9393. DUK_INTERNAL_DECL duk_int_t duk_handle_call_unprotected_nargs(duk_hthread *thr, duk_idx_t nargs, duk_small_uint_t call_flags);
  9394. DUK_INTERNAL_DECL duk_int_t duk_handle_safe_call(duk_hthread *thr, duk_safe_call_function func, void *udata, duk_idx_t num_stack_args, duk_idx_t num_stack_res);
  9395. DUK_INTERNAL_DECL void duk_call_construct_postprocess(duk_hthread *thr, duk_small_uint_t proxy_invariant);
  9396. #if defined(DUK_USE_VERBOSE_ERRORS)
  9397. DUK_INTERNAL_DECL void duk_call_setup_propcall_error(duk_hthread *thr, duk_tval *tv_targ, duk_tval *tv_base, duk_tval *tv_key);
  9398. #endif
  9399. /* bytecode execution */
  9400. DUK_INTERNAL_DECL void duk_js_execute_bytecode(duk_hthread *exec_thr);
  9401. #endif /* DUK_JS_H_INCLUDED */
  9402. /* #include duk_numconv.h */
  9403. #line 1 "duk_numconv.h"
  9404. /*
  9405. * Number-to-string conversion. The semantics of these is very tightly
  9406. * bound with the ECMAScript semantics required for call sites.
  9407. */
  9408. #if !defined(DUK_NUMCONV_H_INCLUDED)
  9409. #define DUK_NUMCONV_H_INCLUDED
  9410. /* Output a specified number of digits instead of using the shortest
  9411. * form. Used for toPrecision() and toFixed().
  9412. */
  9413. #define DUK_N2S_FLAG_FIXED_FORMAT (1U << 0)
  9414. /* Force exponential format. Used for toExponential(). */
  9415. #define DUK_N2S_FLAG_FORCE_EXP (1U << 1)
  9416. /* If number would need zero padding (for whole number part), use
  9417. * exponential format instead. E.g. if input number is 12300, 3
  9418. * digits are generated ("123"), output "1.23e+4" instead of "12300".
  9419. * Used for toPrecision().
  9420. */
  9421. #define DUK_N2S_FLAG_NO_ZERO_PAD (1U << 2)
  9422. /* Digit count indicates number of fractions (i.e. an absolute
  9423. * digit index instead of a relative one). Used together with
  9424. * DUK_N2S_FLAG_FIXED_FORMAT for toFixed().
  9425. */
  9426. #define DUK_N2S_FLAG_FRACTION_DIGITS (1U << 3)
  9427. /*
  9428. * String-to-number conversion
  9429. */
  9430. /* Maximum exponent value when parsing numbers. This is not strictly
  9431. * compliant as there should be no upper limit, but as we parse the
  9432. * exponent without a bigint, impose some limit. The limit should be
  9433. * small enough that multiplying it (or limit-1 to be precise) won't
  9434. * overflow signed 32-bit integer range. Exponent is only parsed with
  9435. * radix 10, but with maximum radix (36) a safe limit is:
  9436. * (10000000*36).toString(16) -> '15752a00'
  9437. */
  9438. #define DUK_S2N_MAX_EXPONENT 10000000L
  9439. /* Trim white space (= allow leading and trailing whitespace) */
  9440. #define DUK_S2N_FLAG_TRIM_WHITE (1U << 0)
  9441. /* Allow exponent */
  9442. #define DUK_S2N_FLAG_ALLOW_EXP (1U << 1)
  9443. /* Allow trailing garbage (e.g. treat "123foo" as "123) */
  9444. #define DUK_S2N_FLAG_ALLOW_GARBAGE (1U << 2)
  9445. /* Allow leading plus sign */
  9446. #define DUK_S2N_FLAG_ALLOW_PLUS (1U << 3)
  9447. /* Allow leading minus sign */
  9448. #define DUK_S2N_FLAG_ALLOW_MINUS (1U << 4)
  9449. /* Allow 'Infinity' */
  9450. #define DUK_S2N_FLAG_ALLOW_INF (1U << 5)
  9451. /* Allow fraction part */
  9452. #define DUK_S2N_FLAG_ALLOW_FRAC (1U << 6)
  9453. /* Allow naked fraction (e.g. ".123") */
  9454. #define DUK_S2N_FLAG_ALLOW_NAKED_FRAC (1U << 7)
  9455. /* Allow empty fraction (e.g. "123.") */
  9456. #define DUK_S2N_FLAG_ALLOW_EMPTY_FRAC (1U << 8)
  9457. /* Allow empty string to be interpreted as 0 */
  9458. #define DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO (1U << 9)
  9459. /* Allow leading zeroes (e.g. "0123" -> "123") */
  9460. #define DUK_S2N_FLAG_ALLOW_LEADING_ZERO (1U << 10)
  9461. /* Allow automatic detection of hex base ("0x" or "0X" prefix),
  9462. * overrides radix argument and forces integer mode.
  9463. */
  9464. #define DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT (1U << 11)
  9465. /* Allow automatic detection of legacy octal base ("0n"),
  9466. * overrides radix argument and forces integer mode.
  9467. */
  9468. #define DUK_S2N_FLAG_ALLOW_AUTO_LEGACY_OCT_INT (1U << 12)
  9469. /* Allow automatic detection of ES2015 octal base ("0o123"),
  9470. * overrides radix argument and forces integer mode.
  9471. */
  9472. #define DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT (1U << 13)
  9473. /* Allow automatic detection of ES2015 binary base ("0b10001"),
  9474. * overrides radix argument and forces integer mode.
  9475. */
  9476. #define DUK_S2N_FLAG_ALLOW_AUTO_BIN_INT (1U << 14)
  9477. /*
  9478. * Prototypes
  9479. */
  9480. DUK_INTERNAL_DECL void duk_numconv_stringify(duk_hthread *thr, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags);
  9481. DUK_INTERNAL_DECL void duk_numconv_parse(duk_hthread *thr, duk_small_int_t radix, duk_small_uint_t flags);
  9482. #endif /* DUK_NUMCONV_H_INCLUDED */
  9483. /* #include duk_bi_protos.h */
  9484. #line 1 "duk_bi_protos.h"
  9485. /*
  9486. * Prototypes for built-in functions not automatically covered by the
  9487. * header declarations emitted by genbuiltins.py.
  9488. */
  9489. #if !defined(DUK_BUILTIN_PROTOS_H_INCLUDED)
  9490. #define DUK_BUILTIN_PROTOS_H_INCLUDED
  9491. /* Buffer size needed for ISO 8601 formatting.
  9492. * Accurate value is 32 + 1 for NUL termination:
  9493. * >>> len('+123456-01-23T12:34:56.123+12:34')
  9494. * 32
  9495. * Include additional space to be safe.
  9496. */
  9497. #define DUK_BI_DATE_ISO8601_BUFSIZE 40
  9498. /* Helpers exposed for internal use */
  9499. DUK_INTERNAL_DECL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags);
  9500. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags);
  9501. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year);
  9502. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x);
  9503. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t year);
  9504. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x);
  9505. /* Built-in providers */
  9506. #if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY)
  9507. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_gettimeofday(void);
  9508. #endif
  9509. #if defined(DUK_USE_DATE_NOW_TIME)
  9510. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_time(void);
  9511. #endif
  9512. #if defined(DUK_USE_DATE_NOW_WINDOWS)
  9513. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows(void);
  9514. #endif
  9515. #if defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS)
  9516. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows_subms(void);
  9517. #endif
  9518. #if defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME_S) || defined(DUK_USE_DATE_TZO_GMTIME)
  9519. DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d);
  9520. #endif
  9521. #if defined(DUK_USE_DATE_TZO_WINDOWS)
  9522. DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d);
  9523. #endif
  9524. #if defined(DUK_USE_DATE_TZO_WINDOWS_NO_DST)
  9525. DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows_no_dst(duk_double_t d);
  9526. #endif
  9527. #if defined(DUK_USE_DATE_PRS_STRPTIME)
  9528. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_strptime(duk_hthread *thr, const char *str);
  9529. #endif
  9530. #if defined(DUK_USE_DATE_PRS_GETDATE)
  9531. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_parse_string_getdate(duk_hthread *thr, const char *str);
  9532. #endif
  9533. #if defined(DUK_USE_DATE_FMT_STRFTIME)
  9534. DUK_INTERNAL_DECL duk_bool_t duk_bi_date_format_parts_strftime(duk_hthread *thr, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags);
  9535. #endif
  9536. #if defined(DUK_USE_GET_MONOTONIC_TIME_CLOCK_GETTIME)
  9537. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_monotonic_time_clock_gettime(void);
  9538. #endif
  9539. #if defined(DUK_USE_GET_MONOTONIC_TIME_WINDOWS_QPC)
  9540. DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_monotonic_time_windows_qpc(void);
  9541. #endif
  9542. DUK_INTERNAL_DECL
  9543. void duk_bi_json_parse_helper(duk_hthread *thr,
  9544. duk_idx_t idx_value,
  9545. duk_idx_t idx_reviver,
  9546. duk_small_uint_t flags);
  9547. DUK_INTERNAL_DECL
  9548. void duk_bi_json_stringify_helper(duk_hthread *thr,
  9549. duk_idx_t idx_value,
  9550. duk_idx_t idx_replacer,
  9551. duk_idx_t idx_space,
  9552. duk_small_uint_t flags);
  9553. DUK_INTERNAL_DECL duk_ret_t duk_textdecoder_decode_utf8_nodejs(duk_hthread *thr);
  9554. #if defined(DUK_USE_ES6_PROXY)
  9555. DUK_INTERNAL_DECL void duk_proxy_ownkeys_postprocess(duk_hthread *thr, duk_hobject *h_proxy_target, duk_uint_t flags);
  9556. #endif
  9557. #endif /* DUK_BUILTIN_PROTOS_H_INCLUDED */
  9558. /* #include duk_selftest.h */
  9559. #line 1 "duk_selftest.h"
  9560. /*
  9561. * Selftest code
  9562. */
  9563. #if !defined(DUK_SELFTEST_H_INCLUDED)
  9564. #define DUK_SELFTEST_H_INCLUDED
  9565. #if defined(DUK_USE_SELF_TESTS)
  9566. DUK_INTERNAL_DECL duk_uint_t duk_selftest_run_tests(duk_alloc_function alloc_func,
  9567. duk_realloc_function realloc_func,
  9568. duk_free_function free_func,
  9569. void *udata);
  9570. #endif
  9571. #endif /* DUK_SELFTEST_H_INCLUDED */
  9572. #line 82 "duk_internal.h"
  9573. #endif /* DUK_INTERNAL_H_INCLUDED */
  9574. #line 10 "duk_replacements.c"
  9575. #if defined(DUK_USE_COMPUTED_NAN)
  9576. DUK_INTERNAL double duk_computed_nan;
  9577. #endif
  9578. #if defined(DUK_USE_COMPUTED_INFINITY)
  9579. DUK_INTERNAL double duk_computed_infinity;
  9580. #endif
  9581. #if defined(DUK_USE_REPL_FPCLASSIFY)
  9582. DUK_INTERNAL int duk_repl_fpclassify(double x) {
  9583. duk_double_union u;
  9584. duk_uint_fast16_t expt;
  9585. duk_small_int_t mzero;
  9586. u.d = x;
  9587. expt = (duk_uint_fast16_t) (u.us[DUK_DBL_IDX_US0] & 0x7ff0UL);
  9588. if (expt > 0x0000UL && expt < 0x7ff0UL) {
  9589. /* expt values [0x001,0x7fe] = normal */
  9590. return DUK_FP_NORMAL;
  9591. }
  9592. mzero = (u.ui[DUK_DBL_IDX_UI1] == 0 && (u.ui[DUK_DBL_IDX_UI0] & 0x000fffffUL) == 0);
  9593. if (expt == 0x0000UL) {
  9594. /* expt 0x000 is zero/subnormal */
  9595. if (mzero) {
  9596. return DUK_FP_ZERO;
  9597. } else {
  9598. return DUK_FP_SUBNORMAL;
  9599. }
  9600. } else {
  9601. /* expt 0xfff is infinite/nan */
  9602. if (mzero) {
  9603. return DUK_FP_INFINITE;
  9604. } else {
  9605. return DUK_FP_NAN;
  9606. }
  9607. }
  9608. }
  9609. #endif
  9610. #if defined(DUK_USE_REPL_SIGNBIT)
  9611. DUK_INTERNAL int duk_repl_signbit(double x) {
  9612. duk_double_union u;
  9613. u.d = x;
  9614. return (int) (u.uc[DUK_DBL_IDX_UC0] & 0x80UL);
  9615. }
  9616. #endif
  9617. #if defined(DUK_USE_REPL_ISFINITE)
  9618. DUK_INTERNAL int duk_repl_isfinite(double x) {
  9619. int c = DUK_FPCLASSIFY(x);
  9620. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  9621. return 0;
  9622. } else {
  9623. return 1;
  9624. }
  9625. }
  9626. #endif
  9627. #if defined(DUK_USE_REPL_ISNAN)
  9628. DUK_INTERNAL int duk_repl_isnan(double x) {
  9629. int c = DUK_FPCLASSIFY(x);
  9630. return (c == DUK_FP_NAN);
  9631. }
  9632. #endif
  9633. #if defined(DUK_USE_REPL_ISINF)
  9634. DUK_INTERNAL int duk_repl_isinf(double x) {
  9635. int c = DUK_FPCLASSIFY(x);
  9636. return (c == DUK_FP_INFINITE);
  9637. }
  9638. #endif
  9639. #line 1 "duk_debug_macros.c"
  9640. /*
  9641. * Debugging macro calls.
  9642. */
  9643. /* #include duk_internal.h -> already included */
  9644. #if defined(DUK_USE_DEBUG)
  9645. /*
  9646. * Debugging enabled
  9647. */
  9648. #include <stdio.h>
  9649. #include <stdlib.h>
  9650. #include <stdarg.h>
  9651. #if !defined(DUK_USE_DEBUG_WRITE)
  9652. #error debugging enabled (DUK_USE_DEBUG) but DUK_USE_DEBUG_WRITE not defined
  9653. #endif
  9654. #define DUK__DEBUG_BUFSIZE DUK_USE_DEBUG_BUFSIZE
  9655. #if defined(DUK_USE_VARIADIC_MACROS)
  9656. DUK_INTERNAL void duk_debug_log(duk_int_t level, const char *file, duk_int_t line, const char *func, const char *fmt, ...) {
  9657. va_list ap;
  9658. long arg_level;
  9659. const char *arg_file;
  9660. long arg_line;
  9661. const char *arg_func;
  9662. const char *arg_msg;
  9663. char buf[DUK__DEBUG_BUFSIZE];
  9664. va_start(ap, fmt);
  9665. duk_memzero((void *) buf, (size_t) DUK__DEBUG_BUFSIZE);
  9666. duk_debug_vsnprintf(buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
  9667. arg_level = (long) level;
  9668. arg_file = (const char *) file;
  9669. arg_line = (long) line;
  9670. arg_func = (const char *) func;
  9671. arg_msg = (const char *) buf;
  9672. DUK_USE_DEBUG_WRITE(arg_level, arg_file, arg_line, arg_func, arg_msg);
  9673. va_end(ap);
  9674. }
  9675. #else /* DUK_USE_VARIADIC_MACROS */
  9676. DUK_INTERNAL char duk_debug_file_stash[DUK_DEBUG_STASH_SIZE];
  9677. DUK_INTERNAL duk_int_t duk_debug_line_stash;
  9678. DUK_INTERNAL char duk_debug_func_stash[DUK_DEBUG_STASH_SIZE];
  9679. DUK_INTERNAL duk_int_t duk_debug_level_stash;
  9680. DUK_INTERNAL void duk_debug_log(const char *fmt, ...) {
  9681. va_list ap;
  9682. long arg_level;
  9683. const char *arg_file;
  9684. long arg_line;
  9685. const char *arg_func;
  9686. const char *arg_msg;
  9687. char buf[DUK__DEBUG_BUFSIZE];
  9688. va_start(ap, fmt);
  9689. duk_memzero((void *) buf, (size_t) DUK__DEBUG_BUFSIZE);
  9690. duk_debug_vsnprintf(buf, DUK__DEBUG_BUFSIZE - 1, fmt, ap);
  9691. arg_level = (long) duk_debug_level_stash;
  9692. arg_file = (const char *) duk_debug_file_stash;
  9693. arg_line = (long) duk_debug_line_stash;
  9694. arg_func = (const char *) duk_debug_func_stash;
  9695. arg_msg = (const char *) buf;
  9696. DUK_USE_DEBUG_WRITE(arg_level, arg_file, arg_line, arg_func, arg_msg);
  9697. va_end(ap);
  9698. }
  9699. #endif /* DUK_USE_VARIADIC_MACROS */
  9700. #else /* DUK_USE_DEBUG */
  9701. /*
  9702. * Debugging disabled
  9703. */
  9704. #endif /* DUK_USE_DEBUG */
  9705. /* automatic undefs */
  9706. #undef DUK__DEBUG_BUFSIZE
  9707. #line 1 "duk_builtins.c"
  9708. /*
  9709. * Automatically generated by genbuiltins.py, do not edit!
  9710. */
  9711. /* #include duk_internal.h -> already included */
  9712. #if defined(DUK_USE_ASSERTIONS)
  9713. #define DUK__REFCINIT(refc) 0 /*h_assert_refcount*/, (refc) /*actual*/
  9714. #else
  9715. #define DUK__REFCINIT(refc) (refc) /*actual*/
  9716. #endif
  9717. #if defined(DUK_USE_ROM_STRINGS)
  9718. #error ROM support not enabled, rerun configure.py with --rom-support
  9719. #else /* DUK_USE_ROM_STRINGS */
  9720. DUK_INTERNAL const duk_uint8_t duk_strings_data[967] = {
  9721. 79,40,209,144,168,105,6,78,54,139,89,185,44,48,46,90,120,8,154,140,35,103,
  9722. 35,113,193,73,5,52,112,180,104,166,135,52,188,4,98,12,27,146,156,80,211,31,
  9723. 129,115,150,64,52,220,109,24,18,68,156,24,38,67,114,36,55,9,119,151,132,
  9724. 140,93,18,113,128,153,201,212,201,205,2,248,8,196,24,224,104,82,146,40,224,
  9725. 193,48,114,168,37,147,196,54,123,28,4,98,12,43,148,67,103,177,192,70,32,
  9726. 196,121,68,54,123,28,18,192,199,144,124,4,98,12,43,136,108,244,117,184,8,
  9727. 196,24,95,40,134,207,71,91,128,140,65,133,113,13,158,158,151,1,24,131,11,
  9728. 229,16,217,233,233,112,17,136,48,206,21,110,4,244,244,184,8,196,24,103,10,
  9729. 183,2,122,218,156,4,98,12,24,203,112,64,179,113,193,79,8,218,155,131,32,
  9730. 184,70,212,220,13,10,82,68,252,123,144,217,146,38,228,207,18,0,100,37,64,
  9731. 178,212,11,161,17,104,162,96,10,200,193,57,165,65,169,16,5,100,81,27,70,18,
  9732. 32,10,200,68,185,13,116,221,197,184,64,89,57,41,197,13,49,234,5,208,156,
  9733. 113,87,55,118,147,20,187,56,161,166,92,221,212,73,210,236,226,134,153,115,
  9734. 119,76,201,203,179,138,26,99,73,212,136,136,164,25,174,137,56,32,72,137,
  9735. 101,23,52,45,13,34,86,9,79,136,104,201,114,149,96,52,138,134,140,151,75,
  9736. 226,233,186,120,121,22,39,54,83,141,5,55,68,236,36,164,3,16,225,115,150,64,
  9737. 52,205,163,2,72,154,83,138,26,99,75,12,11,150,103,5,36,20,211,70,140,133,
  9738. 67,72,49,241,160,227,81,196,52,168,106,39,132,252,183,136,105,80,212,79,2,
  9739. 249,110,128,126,88,95,133,109,237,237,237,151,235,127,46,249,119,203,190,
  9740. 186,206,33,181,2,208,61,190,12,19,34,65,19,81,132,108,228,97,1,107,33,12,
  9741. 32,45,100,137,64,247,175,9,19,155,41,198,130,155,134,69,146,100,227,226,
  9742. 231,146,51,192,204,73,140,224,145,221,102,241,68,196,169,248,30,75,12,11,
  9743. 151,242,233,187,143,138,24,137,162,164,255,253,63,3,201,97,129,114,254,92,
  9744. 112,75,136,108,166,6,136,159,255,167,224,121,44,48,46,95,203,166,238,74,
  9745. 113,67,77,201,128,223,255,223,224,121,44,48,46,95,203,145,46,9,205,16,39,
  9746. 201,62,36,0,192,21,147,255,238,145,39,199,197,211,116,240,242,113,197,78,
  9747. 214,211,226,233,187,107,105,19,119,37,56,161,166,52,221,212,201,205,36,240,
  9748. 242,16,96,152,12,178,52,211,56,228,73,150,83,0,148,39,137,75,67,73,198,209,
  9749. 129,36,85,185,201,196,2,32,193,48,17,160,97,16,84,44,156,104,24,67,189,200,
  9750. 108,201,19,238,114,96,137,137,50,238,113,164,188,211,185,192,226,100,19,
  9751. 134,68,110,112,174,139,0,185,31,115,149,4,88,7,159,115,146,117,34,34,35,
  9752. 115,143,22,146,208,210,19,115,140,3,207,185,202,130,36,109,85,185,194,161,
  9753. 160,90,50,72,155,115,149,2,232,67,137,204,122,22,66,161,175,164,210,72,199,
  9754. 130,137,1,50,32,145,143,38,120,186,195,35,106,51,146,230,8,36,77,109,65,38,
  9755. 226,72,159,191,189,181,70,140,133,222,249,212,227,66,125,245,187,251,219,
  9756. 77,3,119,190,117,56,208,159,125,110,254,246,210,26,93,239,157,78,52,39,223,
  9757. 93,191,189,180,212,52,187,223,58,156,104,79,190,187,127,123,104,180,104,
  9758. 183,190,117,56,208,159,125,102,254,209,104,209,124,234,113,161,62,250,80,
  9759. 196,128,81,4,9,16,162,4,196,116,9,205,154,27,66,32,100,13,12,98,68,227,33,
  9760. 65,69,204,195,34,201,50,8,110,33,23,34,28,168,104,22,188,12,174,138,11,70,
  9761. 138,104,115,68,130,137,13,82,27,41,129,162,35,138,54,146,198,137,39,72,180,
  9762. 210,178,38,35,146,103,68,139,51,197,214,28,227,131,79,15,35,138,58,130,37,
  9763. 19,155,41,146,174,64,203,99,161,100,37,145,51,148,75,4,164,66,54,140,49,46,
  9764. 247,70,103,37,230,70,142,70,67,30,232,204,178,163,201,18,54,139,89,39,26,
  9765. 16,165,2,228,69,33,143,89,24,70,206,73,67,102,72,148,2,32,214,73,157,224,
  9766. 18,128,98,29,241,69,65,50,37,241,116,200,41,144,102,125,2,180,8,210,152,38,
  9767. 129,23,8,34,198,
  9768. };
  9769. #endif /* DUK_USE_ROM_STRINGS */
  9770. #if defined(DUK_USE_ROM_OBJECTS)
  9771. #error ROM support not enabled, rerun configure.py with --rom-support
  9772. #else /* DUK_USE_ROM_OBJECTS */
  9773. /* native functions: 177 */
  9774. DUK_INTERNAL const duk_c_function duk_bi_native_functions[177] = {
  9775. NULL,
  9776. duk_bi_array_constructor,
  9777. duk_bi_array_constructor_is_array,
  9778. duk_bi_array_prototype_concat,
  9779. duk_bi_array_prototype_indexof_shared,
  9780. duk_bi_array_prototype_iter_shared,
  9781. duk_bi_array_prototype_join_shared,
  9782. duk_bi_array_prototype_pop,
  9783. duk_bi_array_prototype_push,
  9784. duk_bi_array_prototype_reduce_shared,
  9785. duk_bi_array_prototype_reverse,
  9786. duk_bi_array_prototype_shift,
  9787. duk_bi_array_prototype_slice,
  9788. duk_bi_array_prototype_sort,
  9789. duk_bi_array_prototype_splice,
  9790. duk_bi_array_prototype_to_string,
  9791. duk_bi_array_prototype_unshift,
  9792. duk_bi_arraybuffer_constructor,
  9793. duk_bi_arraybuffer_isview,
  9794. duk_bi_boolean_constructor,
  9795. duk_bi_boolean_prototype_tostring_shared,
  9796. duk_bi_buffer_compare_shared,
  9797. duk_bi_buffer_readfield,
  9798. duk_bi_buffer_slice_shared,
  9799. duk_bi_buffer_writefield,
  9800. duk_bi_dataview_constructor,
  9801. duk_bi_date_constructor,
  9802. duk_bi_date_constructor_now,
  9803. duk_bi_date_constructor_parse,
  9804. duk_bi_date_constructor_utc,
  9805. duk_bi_date_prototype_get_shared,
  9806. duk_bi_date_prototype_get_timezone_offset,
  9807. duk_bi_date_prototype_set_shared,
  9808. duk_bi_date_prototype_set_time,
  9809. duk_bi_date_prototype_to_json,
  9810. duk_bi_date_prototype_tostring_shared,
  9811. duk_bi_date_prototype_value_of,
  9812. duk_bi_duktape_object_act,
  9813. duk_bi_duktape_object_compact,
  9814. duk_bi_duktape_object_dec,
  9815. duk_bi_duktape_object_enc,
  9816. duk_bi_duktape_object_fin,
  9817. duk_bi_duktape_object_gc,
  9818. duk_bi_duktape_object_info,
  9819. duk_bi_error_constructor_shared,
  9820. duk_bi_error_prototype_filename_getter,
  9821. duk_bi_error_prototype_filename_setter,
  9822. duk_bi_error_prototype_linenumber_getter,
  9823. duk_bi_error_prototype_linenumber_setter,
  9824. duk_bi_error_prototype_stack_getter,
  9825. duk_bi_error_prototype_stack_setter,
  9826. duk_bi_error_prototype_to_string,
  9827. duk_bi_function_constructor,
  9828. duk_bi_function_prototype,
  9829. duk_bi_function_prototype_apply,
  9830. duk_bi_function_prototype_bind,
  9831. duk_bi_function_prototype_call,
  9832. duk_bi_function_prototype_to_string,
  9833. duk_bi_global_object_decode_uri,
  9834. duk_bi_global_object_decode_uri_component,
  9835. duk_bi_global_object_encode_uri,
  9836. duk_bi_global_object_encode_uri_component,
  9837. duk_bi_global_object_escape,
  9838. duk_bi_global_object_eval,
  9839. duk_bi_global_object_is_finite,
  9840. duk_bi_global_object_is_nan,
  9841. duk_bi_global_object_parse_float,
  9842. duk_bi_global_object_parse_int,
  9843. duk_bi_global_object_unescape,
  9844. duk_bi_json_object_parse,
  9845. duk_bi_json_object_stringify,
  9846. duk_bi_math_object_clz32,
  9847. duk_bi_math_object_hypot,
  9848. duk_bi_math_object_imul,
  9849. duk_bi_math_object_max,
  9850. duk_bi_math_object_min,
  9851. duk_bi_math_object_onearg_shared,
  9852. duk_bi_math_object_random,
  9853. duk_bi_math_object_sign,
  9854. duk_bi_math_object_twoarg_shared,
  9855. duk_bi_native_function_length,
  9856. duk_bi_native_function_name,
  9857. duk_bi_nodejs_buffer_byte_length,
  9858. duk_bi_nodejs_buffer_concat,
  9859. duk_bi_nodejs_buffer_constructor,
  9860. duk_bi_nodejs_buffer_copy,
  9861. duk_bi_nodejs_buffer_fill,
  9862. duk_bi_nodejs_buffer_is_buffer,
  9863. duk_bi_nodejs_buffer_is_encoding,
  9864. duk_bi_nodejs_buffer_tojson,
  9865. duk_bi_nodejs_buffer_tostring,
  9866. duk_bi_nodejs_buffer_write,
  9867. duk_bi_number_check_shared,
  9868. duk_bi_number_constructor,
  9869. duk_bi_number_prototype_to_exponential,
  9870. duk_bi_number_prototype_to_fixed,
  9871. duk_bi_number_prototype_to_locale_string,
  9872. duk_bi_number_prototype_to_precision,
  9873. duk_bi_number_prototype_to_string,
  9874. duk_bi_number_prototype_value_of,
  9875. duk_bi_object_constructor,
  9876. duk_bi_object_constructor_assign,
  9877. duk_bi_object_constructor_create,
  9878. duk_bi_object_constructor_define_properties,
  9879. duk_bi_object_constructor_define_property,
  9880. duk_bi_object_constructor_get_own_property_descriptor,
  9881. duk_bi_object_constructor_is,
  9882. duk_bi_object_constructor_is_extensible,
  9883. duk_bi_object_constructor_is_sealed_frozen_shared,
  9884. duk_bi_object_constructor_keys_shared,
  9885. duk_bi_object_constructor_prevent_extensions,
  9886. duk_bi_object_constructor_seal_freeze_shared,
  9887. duk_bi_object_getprototype_shared,
  9888. duk_bi_object_prototype_defineaccessor,
  9889. duk_bi_object_prototype_has_own_property,
  9890. duk_bi_object_prototype_is_prototype_of,
  9891. duk_bi_object_prototype_lookupaccessor,
  9892. duk_bi_object_prototype_property_is_enumerable,
  9893. duk_bi_object_prototype_to_locale_string,
  9894. duk_bi_object_prototype_to_string,
  9895. duk_bi_object_prototype_value_of,
  9896. duk_bi_object_setprototype_shared,
  9897. duk_bi_performance_now,
  9898. duk_bi_pointer_constructor,
  9899. duk_bi_pointer_prototype_tostring_shared,
  9900. duk_bi_proxy_constructor,
  9901. duk_bi_reflect_apply,
  9902. duk_bi_reflect_construct,
  9903. duk_bi_reflect_object_delete_property,
  9904. duk_bi_reflect_object_get,
  9905. duk_bi_reflect_object_has,
  9906. duk_bi_reflect_object_set,
  9907. duk_bi_regexp_constructor,
  9908. duk_bi_regexp_prototype_exec,
  9909. duk_bi_regexp_prototype_flags,
  9910. duk_bi_regexp_prototype_shared_getter,
  9911. duk_bi_regexp_prototype_test,
  9912. duk_bi_regexp_prototype_tostring,
  9913. duk_bi_string_constructor,
  9914. duk_bi_string_constructor_from_char_code,
  9915. duk_bi_string_constructor_from_code_point,
  9916. duk_bi_string_prototype_caseconv_shared,
  9917. duk_bi_string_prototype_char_at,
  9918. duk_bi_string_prototype_char_code_at,
  9919. duk_bi_string_prototype_concat,
  9920. duk_bi_string_prototype_includes,
  9921. duk_bi_string_prototype_indexof_shared,
  9922. duk_bi_string_prototype_locale_compare,
  9923. duk_bi_string_prototype_match,
  9924. duk_bi_string_prototype_repeat,
  9925. duk_bi_string_prototype_replace,
  9926. duk_bi_string_prototype_search,
  9927. duk_bi_string_prototype_slice,
  9928. duk_bi_string_prototype_split,
  9929. duk_bi_string_prototype_startswith_endswith,
  9930. duk_bi_string_prototype_substr,
  9931. duk_bi_string_prototype_substring,
  9932. duk_bi_string_prototype_to_string,
  9933. duk_bi_string_prototype_trim,
  9934. duk_bi_textdecoder_constructor,
  9935. duk_bi_textdecoder_prototype_decode,
  9936. duk_bi_textdecoder_prototype_shared_getter,
  9937. duk_bi_textencoder_constructor,
  9938. duk_bi_textencoder_prototype_encode,
  9939. duk_bi_textencoder_prototype_encoding_getter,
  9940. duk_bi_thread_constructor,
  9941. duk_bi_thread_current,
  9942. duk_bi_thread_resume,
  9943. duk_bi_thread_yield,
  9944. duk_bi_type_error_thrower,
  9945. duk_bi_typedarray_buffer_getter,
  9946. duk_bi_typedarray_bytelength_getter,
  9947. duk_bi_typedarray_byteoffset_getter,
  9948. duk_bi_typedarray_constructor,
  9949. duk_bi_typedarray_set,
  9950. duk_bi_uint8array_allocplain,
  9951. duk_bi_uint8array_plainof,
  9952. };
  9953. #if defined(DUK_USE_DOUBLE_LE)
  9954. DUK_INTERNAL const duk_uint8_t duk_builtins_data[4116] = {
  9955. 144,148,105,224,32,68,52,228,62,12,104,200,165,132,52,167,194,138,105,243,
  9956. 124,57,28,211,57,18,64,52,238,126,44,138,111,171,241,164,19,87,129,30,33,
  9957. 167,16,145,159,8,211,136,9,225,42,5,240,145,139,163,163,8,211,136,10,228,
  9958. 64,211,19,132,140,93,29,56,70,156,64,119,34,66,146,36,104,137,194,70,46,
  9959. 142,172,35,78,32,47,146,195,102,11,240,145,139,163,175,8,211,136,9,228,240,
  9960. 242,112,145,139,163,179,8,211,136,8,237,34,130,118,49,116,118,225,26,48,0,
  9961. 1,82,29,201,158,46,183,39,135,147,132,140,93,16,132,76,66,33,8,66,16,132,
  9962. 33,8,66,26,179,233,97,167,60,150,34,33,154,112,0,1,75,247,35,79,95,237,198,
  9963. 174,200,47,31,23,95,17,13,51,19,35,93,68,216,209,128,0,10,208,174,79,15,32,
  9964. 248,8,196,24,8,107,192,0,5,106,118,27,94,0,0,43,83,227,94,0,0,43,84,46,215,
  9965. 128,0,10,213,28,198,188,0,0,86,169,100,53,224,0,2,181,79,85,175,0,0,21,170,
  9966. 154,45,120,0,0,173,85,217,107,192,0,5,106,182,243,86,193,106,52,127,130,
  9967. 249,50,94,124,35,68,225,146,49,13,31,186,23,201,146,243,224,200,39,12,145,
  9968. 136,67,134,19,49,0,0,0,0,0,0,3,225,255,51,0,0,0,0,0,0,3,193,255,47,18,1,
  9969. 172,19,120,71,10,25,196,136,113,162,156,136,199,42,57,204,144,115,132,240,
  9970. 149,2,248,72,197,209,58,2,185,16,52,196,225,35,23,68,233,14,228,72,82,68,
  9971. 141,17,56,72,197,209,58,130,249,44,54,96,191,9,24,186,39,88,79,39,135,147,
  9972. 132,140,93,19,176,35,180,138,9,216,197,209,59,82,79,35,40,242,65,248,58,42,
  9973. 96,121,14,232,94,62,46,190,15,42,31,145,33,86,65,76,242,214,143,73,48,242,
  9974. 243,79,49,56,243,115,207,57,64,243,180,79,61,72,243,244,207,65,80,244,53,
  9975. 79,69,88,244,98,30,8,200,156,67,102,120,241,79,4,100,78,21,110,4,207,32,47,
  9976. 147,37,231,194,52,78,25,34,122,81,124,153,47,62,12,130,112,201,19,211,139,
  9977. 121,34,87,69,128,104,137,239,83,18,238,108,165,2,162,92,104,56,220,233,1,8,
  9978. 151,10,134,162,100,206,16,18,50,9,195,39,105,20,101,136,18,25,4,225,147,
  9979. 180,138,5,215,49,238,105,27,60,185,1,36,104,156,50,118,145,70,96,129,34,52,
  9980. 78,25,59,72,160,93,115,30,230,145,179,204,144,12,73,8,15,38,104,128,138,52,
  9981. 146,16,30,77,1,0,2,11,132,193,198,36,248,248,186,110,158,30,78,56,188,194,
  9982. 70,183,170,136,48,98,79,142,179,120,248,185,228,140,241,193,146,66,138,31,
  9983. 55,71,138,128,153,137,62,58,205,227,226,231,146,51,199,26,6,18,92,146,64,
  9984. 96,74,72,51,120,43,192,97,68,128,153,56,72,7,12,133,67,73,199,197,207,36,
  9985. 103,142,35,2,3,33,80,210,113,241,115,201,25,160,146,225,160,9,34,1,124,178,
  9986. 1,139,18,19,36,229,146,8,190,36,169,27,62,18,243,35,100,135,54,92,162,2,17,
  9987. 46,72,128,89,7,200,32,33,18,225,98,236,145,188,130,64,196,75,132,188,200,
  9988. 217,32,43,39,28,128,69,19,18,228,144,42,98,79,142,179,120,248,185,228,140,
  9989. 241,201,97,129,114,229,201,37,2,68,184,200,1,147,93,159,153,213,34,235,250,
  9990. 96,48,157,32,24,94,160,1,199,4,184,235,55,143,139,158,72,207,28,226,3,81,
  9991. 46,62,46,155,167,135,147,142,47,60,129,71,197,207,36,103,142,34,92,35,104,
  9992. 194,68,1,89,58,36,8,109,109,12,133,67,73,195,18,115,36,118,182,185,168,8,
  9993. 109,109,12,133,67,73,201,18,115,36,118,182,185,168,130,27,91,75,115,149,71,
  9994. 240,196,156,201,29,173,174,129,2,27,91,75,115,149,71,242,68,156,201,29,173,
  9995. 174,129,34,12,16,28,128,62,191,42,3,71,146,68,4,16,22,188,161,240,16,40,
  9996. 104,242,103,196,16,93,158,125,96,110,115,235,64,131,16,16,58,37,192,70,32,
  9997. 194,144,114,25,67,95,40,6,18,8,32,48,156,209,2,108,124,96,224,144,6,247,62,
  9998. 16,0,143,164,143,12,248,15,18,84,145,145,34,128,11,35,160,179,140,0,44,150,
  9999. 129,18,58,0,146,116,103,32,128,105,61,104,17,36,175,1,232,217,29,5,156,179,
  10000. 224,58,26,50,95,142,43,159,64,181,130,83,226,26,50,95,142,43,159,192,7,255,
  10001. 248,41,42,72,226,1,160,18,78,97,32,26,64,114,186,60,32,4,120,6,148,13,128,
  10002. 124,3,76,12,84,46,100,140,3,78,13,18,14,130,36,67,232,23,18,14,130,39,34,
  10003. 131,30,113,15,224,3,255,253,6,48,40,194,197,204,224,142,8,240,78,25,60,231,
  10004. 192,210,197,204,224,156,50,113,238,67,103,232,62,28,138,156,104,82,170,107,
  10005. 255,32,48,191,144,1,132,112,71,128,159,168,128,161,28,17,224,156,50,112,19,
  10006. 245,144,22,39,12,156,123,144,217,240,19,245,146,3,9,205,16,39,236,62,3,161,
  10007. 163,37,248,226,251,141,1,107,4,167,196,52,100,191,28,95,113,164,13,91,132,
  10008. 5,147,130,115,30,8,147,222,64,43,1,49,31,224,64,60,72,245,128,68,249,32,13,
  10009. 34,2,34,63,204,128,89,45,2,39,209,0,89,61,104,159,213,0,153,80,50,156,80,
  10010. 211,126,16,11,155,184,183,88,145,224,129,34,122,64,17,155,184,183,8,11,39,
  10011. 22,235,18,60,16,36,79,72,1,115,119,40,247,146,60,16,36,79,72,32,140,221,
  10012. 197,184,64,89,57,71,188,145,224,129,34,122,65,1,39,20,51,244,0,52,72,242,2,
  10013. 127,18,2,165,48,70,114,229,145,51,253,141,1,4,104,229,203,34,103,251,26,64,
  10014. 132,52,75,160,201,47,105,160,26,84,12,167,31,186,8,50,0,114,58,113,163,46,
  10015. 190,120,35,11,60,4,25,68,81,61,96,47,181,80,46,132,129,255,255,255,255,255,
  10016. 255,222,254,39,172,67,118,170,5,208,144,0,64,0,0,0,0,0,0,51,16,0,0,0,0,0,0,
  10017. 62,31,200,245,238,146,38,138,147,105,13,42,26,137,226,0,0,0,0,0,0,7,131,
  10018. 249,30,180,134,4,209,82,109,33,165,67,81,60,64,0,0,0,0,0,0,240,255,15,210,
  10019. 62,72,91,155,0,0,0,0,0,0,2,192,240,135,88,11,237,72,5,38,210,27,50,24,145,
  10020. 129,255,255,255,255,255,254,126,134,67,172,67,118,164,2,147,105,13,153,12,
  10021. 72,192,255,255,255,255,255,255,63,195,16,240,70,68,226,27,51,199,138,120,
  10022. 35,34,112,171,112,38,121,7,16,137,112,168,106,38,77,193,1,40,151,16,217,
  10023. 144,196,142,224,144,21,18,227,65,198,238,9,67,81,46,72,5,39,16,217,144,196,
  10024. 142,224,152,228,148,227,64,0,0,0,0,0,0,0,0,131,175,223,16,194,111,8,97,119,
  10025. 224,3,205,220,42,46,65,238,200,13,155,184,75,189,205,35,102,128,47,116,64,
  10026. 92,221,199,196,130,68,144,230,239,72,65,152,12,21,224,140,137,92,128,62,
  10027. 210,98,177,252,3,107,173,88,3,146,211,141,32,0,0,0,0,0,3,225,255,19,175,
  10028. 188,0,100,221,193,130,100,228,167,20,52,215,129,3,38,238,77,12,39,37,56,
  10029. 161,166,188,10,194,94,6,18,155,184,183,8,11,39,6,9,147,146,156,80,211,94,7,
  10030. 18,155,184,183,8,11,39,38,134,19,146,156,80,211,94,8,12,53,224,130,195,222,
  10031. 8,77,133,210,24,91,224,3,152,147,228,208,194,95,0,44,196,159,11,69,175,152,
  10032. 32,35,100,33,135,24,147,237,38,34,246,139,95,48,64,70,200,68,8,49,39,198,
  10033. 57,179,61,144,138,22,98,79,180,152,153,215,54,103,178,17,129,204,73,240,96,
  10034. 153,44,132,112,163,18,125,164,196,62,130,100,178,18,1,140,73,240,96,197,
  10035. 144,146,18,98,79,180,152,135,208,98,200,74,8,49,39,195,186,145,149,144,150,
  10036. 22,98,79,180,152,143,215,82,50,178,19,2,140,73,241,136,109,38,73,89,9,161,
  10037. 166,36,251,73,137,157,67,105,50,74,200,78,10,49,39,201,16,78,104,229,100,
  10038. 39,134,152,147,237,38,41,116,130,115,71,43,33,64,60,196,159,24,133,173,18,
  10039. 32,156,209,202,200,81,18,49,39,218,76,76,234,22,180,72,130,115,71,43,33,72,
  10040. 68,196,159,38,134,19,46,105,56,226,150,68,157,160,1,228,73,242,104,97,46,
  10041. 16,31,34,79,140,66,214,137,16,78,104,229,108,169,137,72,147,237,38,38,117,
  10042. 11,90,36,65,57,163,149,178,168,21,34,79,146,32,156,209,202,218,250,161,178,
  10043. 36,251,73,138,93,32,156,209,202,218,250,193,82,36,248,196,54,147,36,173,
  10044. 191,174,27,34,79,180,152,153,212,54,147,36,173,191,176,17,34,79,135,117,35,
  10045. 43,115,236,133,200,147,237,38,35,245,212,140,173,207,180,15,34,79,131,4,
  10046. 201,108,173,133,72,147,237,38,33,244,19,37,178,184,17,34,79,140,115,102,
  10047. 123,107,238,133,200,147,237,38,38,117,205,153,237,175,188,23,34,79,133,162,
  10048. 215,204,16,17,182,254,248,116,137,62,210,98,47,104,181,243,4,4,109,191,192,
  10049. 131,152,147,230,8,8,217,12,16,60,137,62,96,128,141,178,193,160,206,1,201,
  10050. 176,113,146,0,0,0,0,0,0,0,0,49,185,252,65,137,207,227,37,215,207,227,12,86,
  10051. 127,24,152,188,254,49,88,33,46,65,120,72,4,153,37,63,33,13,127,148,4,26,0,
  10052. 57,62,6,228,163,228,74,86,215,62,55,28,110,179,226,113,70,223,62,47,24,38,
  10053. 191,30,2,125,32,40,20,87,114,41,225,42,5,240,145,139,163,145,41,68,250,128,
  10054. 80,41,174,228,85,200,129,166,39,9,24,186,57,18,148,79,172,5,2,170,238,69,
  10055. 220,137,10,72,145,162,39,9,24,186,57,18,148,79,176,5,2,186,238,69,124,150,
  10056. 27,48,95,132,140,93,28,137,74,39,218,2,129,101,119,34,158,79,15,39,9,24,
  10057. 186,57,18,148,79,184,5,2,218,238,69,29,164,80,78,198,46,142,68,165,16,64,
  10058. 28,24,61,73,25,33,205,128,0,0,0,0,1,167,166,129,108,242,151,15,39,8,34,26,
  10059. 87,97,200,3,0,167,129,32,8,194,195,16,6,84,55,10,60,3,35,69,132,30,1,140,
  10060. 130,193,143,1,196,230,60,2,158,8,131,153,64,115,42,46,191,176,8,194,246,0,
  10061. 80,5,220,193,95,6,234,5,100,225,35,23,71,35,6,228,140,93,29,180,55,108,145,
  10062. 139,163,182,112,52,107,67,76,56,3,153,132,20,28,76,156,89,26,105,158,62,0,
  10063. 0,42,193,2,201,104,17,41,34,156,204,176,160,226,100,226,200,211,76,241,240,
  10064. 0,1,86,2,131,137,147,142,41,100,73,199,192,0,5,96,6,13,10,82,70,62,0,0,42,
  10065. 130,88,115,18,124,67,103,177,69,49,129,6,36,249,68,54,123,20,82,216,65,137,
  10066. 62,33,179,209,214,162,152,208,147,18,124,162,27,61,29,106,41,112,32,196,
  10067. 159,16,217,233,233,81,76,112,73,137,62,81,13,158,158,149,20,186,20,98,79,
  10068. 133,91,129,61,61,42,41,120,40,196,159,10,183,2,122,218,148,82,248,60,137,
  10069. 62,33,179,216,166,216,192,137,18,124,162,27,61,138,109,108,34,68,159,16,
  10070. 217,232,235,83,108,104,76,137,62,81,13,158,142,181,54,184,17,34,79,136,108,
  10071. 244,244,169,182,56,38,68,159,40,134,207,79,74,155,93,10,145,39,194,173,192,
  10072. 158,158,149,54,188,21,34,79,133,91,129,61,109,74,109,125,155,51,136,71,161,
  10073. 196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,7,129,249,155,51,168,71,
  10074. 161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,7,129,249,155,51,200,
  10075. 71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,7,129,249,155,51,
  10076. 232,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,0,2,1,155,52,
  10077. 8,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,0,2,1,155,52,40,
  10078. 71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,0,130,1,155,52,72,
  10079. 71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,0,130,1,155,52,
  10080. 104,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,0,130,1,155,
  10081. 52,136,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,0,0,0,1,2,1,135,
  10082. 52,166,32,76,72,1,246,136,235,103,177,69,0,136,144,3,226,27,61,138,41,44,
  10083. 50,36,0,251,68,117,179,209,214,234,201,69,16,50,36,0,251,68,117,179,209,
  10084. 214,232,73,69,34,5,196,128,31,16,217,232,235,117,100,162,147,2,226,64,15,
  10085. 136,108,244,117,186,18,81,74,129,145,32,7,218,35,173,158,158,151,86,74,40,
  10086. 161,145,32,7,218,35,173,158,158,151,66,74,41,20,46,36,0,248,134,207,79,75,
  10087. 171,37,20,154,23,18,0,124,67,103,167,165,208,146,138,85,11,137,0,62,21,110,
  10088. 4,250,178,81,70,11,137,0,62,21,110,4,250,18,81,72,193,145,32,7,193,186,129,
  10089. 89,58,178,81,71,12,137,0,62,13,212,10,201,208,146,138,71,10,137,0,62,209,
  10090. 29,108,250,178,81,104,1,81,32,7,218,35,173,159,66,74,45,32,38,36,0,248,134,
  10091. 207,171,37,22,160,19,18,0,124,67,103,208,146,139,88,10,180,81,50,118,136,
  10092. 235,103,177,77,128,155,69,19,39,16,217,236,83,105,97,182,138,38,78,209,29,
  10093. 108,244,117,186,178,83,100,13,180,81,50,118,136,235,103,163,173,208,146,
  10094. 155,68,12,180,81,50,113,13,158,142,183,86,74,109,48,50,209,68,201,196,54,
  10095. 122,58,221,9,41,181,64,219,69,19,39,104,142,182,122,122,93,89,41,178,134,
  10096. 218,40,153,59,68,117,179,211,210,232,73,77,162,134,90,40,153,56,134,207,79,
  10097. 75,171,37,54,154,25,104,162,100,226,27,61,61,46,132,148,218,168,101,162,
  10098. 137,147,133,91,129,62,172,148,217,131,45,20,76,156,42,220,9,244,36,166,209,
  10099. 131,109,20,76,156,27,168,21,147,171,37,54,112,219,69,19,39,6,234,5,100,232,
  10100. 73,77,163,133,218,40,153,59,68,117,179,234,201,78,32,5,218,40,153,59,68,
  10101. 117,179,232,73,78,36,5,90,40,153,56,134,207,171,37,56,160,21,104,162,100,
  10102. 226,27,62,132,148,226,195,95,182,97,176,218,128,8,84,45,123,38,1,137,10,1,
  10103. 114,160,64,56,156,199,130,36,160,72,8,39,63,27,24,1,100,180,8,148,146,0,45,
  10104. 162,137,147,111,2,8,4,16,7,8,96,120,72,13,42,226,145,97,87,224,168,1,58,
  10105. 182,232,232,64,22,85,181,187,177,107,2,64,7,213,183,74,7,121,207,215,242,
  10106. 17,119,49,248,94,173,198,210,36,15,232,34,182,84,113,95,115,240,221,91,141,
  10107. 163,160,72,1,220,164,194,175,121,123,103,224,186,244,64,24,45,68,84,251,33,
  10108. 9,64,15,217,66,51,209,218,210,129,154,118,254,205,61,65,204,126,23,178,132,
  10109. 103,165,3,52,237,253,154,122,131,216,254,168,48,6,90,130,1,0,39,75,80,72,8,
  10110. 9,33,186,130,80,64,76,13,212,19,2,130,96,110,150,173,0,65,6,51,212,20,128,
  10111. 65,17,11,212,19,130,137,121,211,210,209,144,6,39,75,80,80,0,201,119,234,10,
  10112. 8,41,86,231,71,80,80,129,79,135,186,122,69,224,34,25,69,233,80,3,91,141,
  10113. 168,40,96,139,113,180,181,5,36,21,110,54,142,134,160,165,1,176,23,211,47,0,
  10114. 216,134,233,215,128,111,117,181,104,128,209,3,70,230,106,64,5,139,168,209,
  10115. 234,10,32,36,144,102,234,136,3,146,27,168,40,160,146,132,103,168,40,192,
  10116. 115,3,117,5,28,22,113,163,69,168,41,103,1,66,188,17,145,52,40,4,202,113,67,
  10117. 76,130,227,68,194,13,240,108,0,0,83,96,0,2,161,0,104,146,84,97,48,0,1,78,
  10118. 192,56,169,24,145,179,192,0,5,48,8,56,16,32,128,56,18,52,125,166,86,147,
  10119. 182,140,28,50,21,13,39,31,23,60,145,158,56,140,141,47,113,6,155,186,188,24,
  10120. 49,39,199,89,188,124,92,242,70,120,224,201,33,69,15,155,163,197,68,14,49,
  10121. 39,199,197,211,116,240,242,113,197,230,18,180,253,228,3,17,46,18,243,35,
  10122. 100,128,172,156,114,70,163,146,76,34,248,146,164,108,248,75,204,141,146,28,
  10123. 217,114,137,27,78,251,241,173,234,162,160,225,1,3,34,92,170,9,105,164,32,
  10124. 225,64,131,155,1,193,133,7,19,39,22,70,154,103,143,128,0,10,176,20,28,76,
  10125. 156,113,75,34,78,62,0,0,43,0,48,104,82,146,49,240,0,1,84,11,180,192,0,5,
  10126. 114,1,18,160,65,24,131,20,145,25,172,48,132,122,28,76,146,218,121,35,180,
  10127. 69,145,132,108,224,0,0,0,0,0,0,120,31,153,172,56,132,122,28,76,146,218,121,
  10128. 35,180,69,145,132,108,224,0,0,0,0,0,0,120,31,168,160,45,110,23,30,176,33,
  10129. 184,0,0,175,32,29,235,2,27,199,23,0,0,22,4,51,88,129,8,244,56,153,37,180,
  10130. 242,71,104,139,35,8,217,192,0,0,0,0,0,0,240,63,51,88,145,8,244,56,153,37,
  10131. 180,242,71,104,139,35,8,217,192,0,0,0,0,0,0,0,64,51,88,161,8,244,56,153,37,
  10132. 180,242,71,104,139,35,8,217,192,0,0,0,0,0,0,0,64,51,88,177,8,244,56,153,37,
  10133. 180,242,71,104,139,35,8,217,192,0,0,0,0,0,0,16,64,51,88,193,8,244,56,153,
  10134. 37,180,242,71,104,139,35,8,217,192,0,0,0,0,0,0,16,64,51,88,209,8,244,56,
  10135. 153,37,180,242,71,104,139,35,8,217,192,0,0,0,0,0,0,16,64,51,88,225,8,244,
  10136. 56,153,37,180,242,71,104,139,35,8,217,192,0,0,0,0,0,0,32,64,32,227,194,0,
  10137. 97,57,162,4,245,232,5,34,92,35,68,225,161,166,218,16,16,137,112,52,41,73,
  10138. 29,153,1,65,196,201,197,145,166,153,245,200,3,137,204,120,34,74,8,200,58,
  10139. 112,28,211,32,130,52,78,26,26,110,248,0,0,164,4,12,70,137,195,39,252,73,
  10140. 240,117,32,57,168,97,4,104,156,52,52,221,255,160,20,160,152,23,223,250,32,
  10141. 148,25,174,137,58,23,51,191,244,84,12,50,9,195,39,240,81,238,2,3,107,173,
  10142. 214,3,192,
  10143. };
  10144. #elif defined(DUK_USE_DOUBLE_BE)
  10145. DUK_INTERNAL const duk_uint8_t duk_builtins_data[4116] = {
  10146. 144,148,105,224,32,68,52,228,62,12,104,200,165,132,52,167,194,138,105,243,
  10147. 124,57,28,211,57,18,64,52,238,126,44,138,111,171,241,164,19,87,129,30,33,
  10148. 167,16,145,159,8,211,136,9,225,42,5,240,145,139,163,163,8,211,136,10,228,
  10149. 64,211,19,132,140,93,29,56,70,156,64,119,34,66,146,36,104,137,194,70,46,
  10150. 142,172,35,78,32,47,146,195,102,11,240,145,139,163,175,8,211,136,9,228,240,
  10151. 242,112,145,139,163,179,8,211,136,8,237,34,130,118,49,116,118,225,26,48,0,
  10152. 1,82,29,201,158,46,183,39,135,147,132,140,93,16,132,76,66,33,8,66,16,132,
  10153. 33,8,66,26,179,233,97,167,60,150,34,33,154,112,0,1,75,247,35,79,95,237,198,
  10154. 174,200,47,31,23,95,17,13,51,19,35,93,68,216,209,128,0,10,208,174,79,15,32,
  10155. 248,8,196,24,8,107,192,0,5,106,118,27,94,0,0,43,83,227,94,0,0,43,84,46,215,
  10156. 128,0,10,213,28,198,188,0,0,86,169,100,53,224,0,2,181,79,85,175,0,0,21,170,
  10157. 154,45,120,0,0,173,85,217,107,192,0,5,106,182,243,86,193,106,52,127,130,
  10158. 249,50,94,124,35,68,225,146,49,13,31,186,23,201,146,243,224,200,39,12,145,
  10159. 136,67,134,19,49,1,255,224,0,0,0,0,0,3,51,1,255,192,0,0,0,0,0,3,47,18,1,
  10160. 172,19,120,71,10,25,196,136,113,162,156,136,199,42,57,204,144,115,132,240,
  10161. 149,2,248,72,197,209,58,2,185,16,52,196,225,35,23,68,233,14,228,72,82,68,
  10162. 141,17,56,72,197,209,58,130,249,44,54,96,191,9,24,186,39,88,79,39,135,147,
  10163. 132,140,93,19,176,35,180,138,9,216,197,209,59,82,79,35,40,242,65,248,58,42,
  10164. 96,121,14,232,94,62,46,190,15,42,31,145,33,86,65,76,242,214,143,73,48,242,
  10165. 243,79,49,56,243,115,207,57,64,243,180,79,61,72,243,244,207,65,80,244,53,
  10166. 79,69,88,244,98,30,8,200,156,67,102,120,241,79,4,100,78,21,110,4,207,32,47,
  10167. 147,37,231,194,52,78,25,34,122,81,124,153,47,62,12,130,112,201,19,211,139,
  10168. 121,34,87,69,128,104,137,239,83,18,238,108,165,2,162,92,104,56,220,233,1,8,
  10169. 151,10,134,162,100,206,16,18,50,9,195,39,105,20,101,136,18,25,4,225,147,
  10170. 180,138,5,215,49,238,105,27,60,185,1,36,104,156,50,118,145,70,96,129,34,52,
  10171. 78,25,59,72,160,93,115,30,230,145,179,204,144,12,73,8,15,38,104,128,138,52,
  10172. 146,16,30,77,1,0,2,11,132,193,198,36,248,248,186,110,158,30,78,56,188,194,
  10173. 70,183,170,136,48,98,79,142,179,120,248,185,228,140,241,193,146,66,138,31,
  10174. 55,71,138,128,153,137,62,58,205,227,226,231,146,51,199,26,6,18,92,146,64,
  10175. 96,74,72,51,120,43,192,97,68,128,153,56,72,7,12,133,67,73,199,197,207,36,
  10176. 103,142,35,2,3,33,80,210,113,241,115,201,25,160,146,225,160,9,34,1,124,178,
  10177. 1,139,18,19,36,229,146,8,190,36,169,27,62,18,243,35,100,135,54,92,162,2,17,
  10178. 46,72,128,89,7,200,32,33,18,225,98,236,145,188,130,64,196,75,132,188,200,
  10179. 217,32,43,39,28,128,69,19,18,228,144,42,98,79,142,179,120,248,185,228,140,
  10180. 241,201,97,129,114,229,201,37,2,68,184,200,1,147,93,159,153,213,34,235,250,
  10181. 96,48,157,32,24,94,160,1,199,4,184,235,55,143,139,158,72,207,28,226,3,81,
  10182. 46,62,46,155,167,135,147,142,47,60,129,71,197,207,36,103,142,34,92,35,104,
  10183. 194,68,1,89,58,36,8,109,109,12,133,67,73,195,18,115,36,118,182,185,168,8,
  10184. 109,109,12,133,67,73,201,18,115,36,118,182,185,168,130,27,91,75,115,149,71,
  10185. 240,196,156,201,29,173,174,129,2,27,91,75,115,149,71,242,68,156,201,29,173,
  10186. 174,129,34,12,16,28,128,62,191,42,3,71,146,68,4,16,22,188,161,240,16,40,
  10187. 104,242,103,196,16,93,158,125,96,110,115,235,64,131,16,16,58,37,192,70,32,
  10188. 194,144,114,25,67,95,40,6,18,8,32,48,156,209,2,108,124,96,224,144,6,247,62,
  10189. 16,0,143,164,143,12,248,15,18,84,145,145,34,128,11,35,160,179,140,0,44,150,
  10190. 129,18,58,0,146,116,103,32,128,105,61,104,17,36,175,1,232,217,29,5,156,179,
  10191. 224,58,26,50,95,142,43,159,64,181,130,83,226,26,50,95,142,43,159,192,7,255,
  10192. 248,41,42,72,226,1,160,18,78,97,32,26,64,114,186,60,32,4,120,6,148,13,128,
  10193. 124,3,76,12,84,46,100,140,3,78,13,18,14,130,36,67,232,23,18,14,130,39,34,
  10194. 131,30,113,15,224,3,255,253,6,48,40,194,197,204,224,142,8,240,78,25,60,231,
  10195. 192,210,197,204,224,156,50,113,238,67,103,232,62,28,138,156,104,82,170,107,
  10196. 255,32,48,191,144,1,132,112,71,128,159,168,128,161,28,17,224,156,50,112,19,
  10197. 245,144,22,39,12,156,123,144,217,240,19,245,146,3,9,205,16,39,236,62,3,161,
  10198. 163,37,248,226,251,141,1,107,4,167,196,52,100,191,28,95,113,164,13,91,132,
  10199. 5,147,130,115,30,8,147,222,64,43,1,49,31,224,64,60,72,245,128,68,249,32,13,
  10200. 34,2,34,63,204,128,89,45,2,39,209,0,89,61,104,159,213,0,153,80,50,156,80,
  10201. 211,126,16,11,155,184,183,88,145,224,129,34,122,64,17,155,184,183,8,11,39,
  10202. 22,235,18,60,16,36,79,72,1,115,119,40,247,146,60,16,36,79,72,32,140,221,
  10203. 197,184,64,89,57,71,188,145,224,129,34,122,65,1,39,20,51,244,0,52,72,242,2,
  10204. 127,18,2,165,48,70,114,229,145,51,253,141,1,4,104,229,203,34,103,251,26,64,
  10205. 132,52,75,160,201,47,105,160,26,84,12,167,31,186,8,50,0,114,58,113,163,46,
  10206. 190,120,35,11,60,4,25,68,81,61,96,47,181,80,46,132,128,255,223,255,255,255,
  10207. 255,255,254,39,172,67,118,170,5,208,144,0,0,0,0,0,0,0,0,115,16,31,254,0,0,
  10208. 0,0,0,0,8,245,238,146,38,138,147,105,13,42,26,137,226,3,255,128,0,0,0,0,0,
  10209. 1,30,180,134,4,209,82,109,33,165,67,81,60,64,255,240,0,0,0,0,0,0,15,210,62,
  10210. 72,91,155,0,242,192,0,0,0,0,0,0,135,88,11,237,72,5,38,210,27,50,24,145,128,
  10211. 134,127,255,255,255,255,255,254,67,172,67,118,164,2,147,105,13,153,12,72,
  10212. 192,195,63,255,255,255,255,255,255,16,240,70,68,226,27,51,199,138,120,35,
  10213. 34,112,171,112,38,121,7,16,137,112,168,106,38,77,193,1,40,151,16,217,144,
  10214. 196,142,224,144,21,18,227,65,198,238,9,67,81,46,72,5,39,16,217,144,196,142,
  10215. 224,152,228,148,227,64,0,0,0,0,0,0,0,0,131,175,223,16,194,111,8,97,119,224,
  10216. 3,205,220,42,46,65,238,200,13,155,184,75,189,205,35,102,128,47,116,64,92,
  10217. 221,199,196,130,68,144,230,239,72,65,152,12,21,224,140,137,92,128,62,210,
  10218. 98,177,252,3,107,173,88,3,146,211,141,33,255,224,0,0,0,0,0,3,19,175,188,0,
  10219. 100,221,193,130,100,228,167,20,52,215,129,3,38,238,77,12,39,37,56,161,166,
  10220. 188,10,194,94,6,18,155,184,183,8,11,39,6,9,147,146,156,80,211,94,7,18,155,
  10221. 184,183,8,11,39,38,134,19,146,156,80,211,94,8,12,53,224,130,195,222,8,77,
  10222. 133,210,24,91,224,3,152,147,228,208,194,95,0,44,196,159,11,69,175,152,32,
  10223. 35,100,33,135,24,147,237,38,34,246,139,95,48,64,70,200,68,8,49,39,198,57,
  10224. 179,61,144,138,22,98,79,180,152,153,215,54,103,178,17,129,204,73,240,96,
  10225. 153,44,132,112,163,18,125,164,196,62,130,100,178,18,1,140,73,240,96,197,
  10226. 144,146,18,98,79,180,152,135,208,98,200,74,8,49,39,195,186,145,149,144,150,
  10227. 22,98,79,180,152,143,215,82,50,178,19,2,140,73,241,136,109,38,73,89,9,161,
  10228. 166,36,251,73,137,157,67,105,50,74,200,78,10,49,39,201,16,78,104,229,100,
  10229. 39,134,152,147,237,38,41,116,130,115,71,43,33,64,60,196,159,24,133,173,18,
  10230. 32,156,209,202,200,81,18,49,39,218,76,76,234,22,180,72,130,115,71,43,33,72,
  10231. 68,196,159,38,134,19,46,105,56,226,150,68,157,160,1,228,73,242,104,97,46,
  10232. 16,31,34,79,140,66,214,137,16,78,104,229,108,169,137,72,147,237,38,38,117,
  10233. 11,90,36,65,57,163,149,178,168,21,34,79,146,32,156,209,202,218,250,161,178,
  10234. 36,251,73,138,93,32,156,209,202,218,250,193,82,36,248,196,54,147,36,173,
  10235. 191,174,27,34,79,180,152,153,212,54,147,36,173,191,176,17,34,79,135,117,35,
  10236. 43,115,236,133,200,147,237,38,35,245,212,140,173,207,180,15,34,79,131,4,
  10237. 201,108,173,133,72,147,237,38,33,244,19,37,178,184,17,34,79,140,115,102,
  10238. 123,107,238,133,200,147,237,38,38,117,205,153,237,175,188,23,34,79,133,162,
  10239. 215,204,16,17,182,254,248,116,137,62,210,98,47,104,181,243,4,4,109,191,192,
  10240. 131,152,147,230,8,8,217,12,16,60,137,62,96,128,141,178,193,160,206,1,201,
  10241. 176,113,146,0,0,0,0,0,0,0,0,49,185,252,65,137,207,227,37,215,207,227,12,86,
  10242. 127,24,152,188,254,49,88,33,46,65,120,72,4,153,37,63,33,13,127,148,4,26,0,
  10243. 57,62,6,228,163,228,74,86,215,62,55,28,110,179,226,113,70,223,62,47,24,38,
  10244. 191,30,2,125,32,40,20,87,114,41,225,42,5,240,145,139,163,145,41,68,250,128,
  10245. 80,41,174,228,85,200,129,166,39,9,24,186,57,18,148,79,172,5,2,170,238,69,
  10246. 220,137,10,72,145,162,39,9,24,186,57,18,148,79,176,5,2,186,238,69,124,150,
  10247. 27,48,95,132,140,93,28,137,74,39,218,2,129,101,119,34,158,79,15,39,9,24,
  10248. 186,57,18,148,79,184,5,2,218,238,69,29,164,80,78,198,46,142,68,165,16,64,
  10249. 28,24,61,73,25,33,205,128,129,167,166,0,0,0,0,1,108,242,151,15,39,8,34,26,
  10250. 87,97,200,3,0,167,129,32,8,194,195,16,6,84,55,10,60,3,35,69,132,30,1,140,
  10251. 130,193,143,1,196,230,60,2,158,8,131,153,64,115,42,46,191,176,8,194,246,0,
  10252. 80,5,220,193,95,6,234,5,100,225,35,23,71,35,6,228,140,93,29,180,55,108,145,
  10253. 139,163,182,112,52,107,67,76,56,3,153,132,20,28,76,156,89,26,105,158,62,0,
  10254. 0,42,193,2,201,104,17,41,34,156,204,176,160,226,100,226,200,211,76,241,240,
  10255. 0,1,86,2,131,137,147,142,41,100,73,199,192,0,5,96,6,13,10,82,70,62,0,0,42,
  10256. 130,88,115,18,124,67,103,177,69,49,129,6,36,249,68,54,123,20,82,216,65,137,
  10257. 62,33,179,209,214,162,152,208,147,18,124,162,27,61,29,106,41,112,32,196,
  10258. 159,16,217,233,233,81,76,112,73,137,62,81,13,158,158,149,20,186,20,98,79,
  10259. 133,91,129,61,61,42,41,120,40,196,159,10,183,2,122,218,148,82,248,60,137,
  10260. 62,33,179,216,166,216,192,137,18,124,162,27,61,138,109,108,34,68,159,16,
  10261. 217,232,235,83,108,104,76,137,62,81,13,158,142,181,54,184,17,34,79,136,108,
  10262. 244,244,169,182,56,38,68,159,40,134,207,79,74,155,93,10,145,39,194,173,192,
  10263. 158,158,149,54,188,21,34,79,133,91,129,61,109,74,109,125,155,51,136,71,161,
  10264. 196,201,45,167,146,59,68,89,24,70,206,1,255,128,0,0,0,0,0,1,155,51,168,71,
  10265. 161,196,201,45,167,146,59,68,89,24,70,206,1,255,128,0,0,0,0,0,1,155,51,200,
  10266. 71,161,196,201,45,167,146,59,68,89,24,70,206,1,255,128,0,0,0,0,0,1,155,51,
  10267. 232,71,161,196,201,45,167,146,59,68,89,24,70,206,2,0,0,0,0,0,0,0,1,155,52,
  10268. 8,71,161,196,201,45,167,146,59,68,89,24,70,206,2,0,0,0,0,0,0,0,1,155,52,40,
  10269. 71,161,196,201,45,167,146,59,68,89,24,70,206,2,0,128,0,0,0,0,0,1,155,52,72,
  10270. 71,161,196,201,45,167,146,59,68,89,24,70,206,2,0,128,0,0,0,0,0,1,155,52,
  10271. 104,71,161,196,201,45,167,146,59,68,89,24,70,206,2,0,128,0,0,0,0,0,1,155,
  10272. 52,136,71,161,196,201,45,167,146,59,68,89,24,70,206,2,1,0,0,0,0,0,0,1,135,
  10273. 52,166,32,76,72,1,246,136,235,103,177,69,0,136,144,3,226,27,61,138,41,44,
  10274. 50,36,0,251,68,117,179,209,214,234,201,69,16,50,36,0,251,68,117,179,209,
  10275. 214,232,73,69,34,5,196,128,31,16,217,232,235,117,100,162,147,2,226,64,15,
  10276. 136,108,244,117,186,18,81,74,129,145,32,7,218,35,173,158,158,151,86,74,40,
  10277. 161,145,32,7,218,35,173,158,158,151,66,74,41,20,46,36,0,248,134,207,79,75,
  10278. 171,37,20,154,23,18,0,124,67,103,167,165,208,146,138,85,11,137,0,62,21,110,
  10279. 4,250,178,81,70,11,137,0,62,21,110,4,250,18,81,72,193,145,32,7,193,186,129,
  10280. 89,58,178,81,71,12,137,0,62,13,212,10,201,208,146,138,71,10,137,0,62,209,
  10281. 29,108,250,178,81,104,1,81,32,7,218,35,173,159,66,74,45,32,38,36,0,248,134,
  10282. 207,171,37,22,160,19,18,0,124,67,103,208,146,139,88,10,180,81,50,118,136,
  10283. 235,103,177,77,128,155,69,19,39,16,217,236,83,105,97,182,138,38,78,209,29,
  10284. 108,244,117,186,178,83,100,13,180,81,50,118,136,235,103,163,173,208,146,
  10285. 155,68,12,180,81,50,113,13,158,142,183,86,74,109,48,50,209,68,201,196,54,
  10286. 122,58,221,9,41,181,64,219,69,19,39,104,142,182,122,122,93,89,41,178,134,
  10287. 218,40,153,59,68,117,179,211,210,232,73,77,162,134,90,40,153,56,134,207,79,
  10288. 75,171,37,54,154,25,104,162,100,226,27,61,61,46,132,148,218,168,101,162,
  10289. 137,147,133,91,129,62,172,148,217,131,45,20,76,156,42,220,9,244,36,166,209,
  10290. 131,109,20,76,156,27,168,21,147,171,37,54,112,219,69,19,39,6,234,5,100,232,
  10291. 73,77,163,133,218,40,153,59,68,117,179,234,201,78,32,5,218,40,153,59,68,
  10292. 117,179,232,73,78,36,5,90,40,153,56,134,207,171,37,56,160,21,104,162,100,
  10293. 226,27,62,132,148,226,195,95,182,97,176,218,128,8,84,45,123,38,1,137,10,1,
  10294. 114,160,64,56,156,199,130,36,160,72,8,39,63,27,24,1,100,180,8,148,146,0,45,
  10295. 162,137,147,111,2,8,4,16,7,8,96,120,72,8,0,183,225,81,98,138,237,33,58,182,
  10296. 232,232,64,64,2,107,177,187,181,85,22,7,213,183,74,1,255,49,114,23,247,209,
  10297. 207,120,94,173,198,210,36,3,255,113,84,118,82,184,47,224,221,91,141,163,
  10298. 160,72,7,251,121,111,98,164,220,161,192,186,244,64,64,9,33,251,84,68,45,24,
  10299. 15,217,66,51,209,218,210,128,127,205,65,60,204,254,119,154,23,178,132,103,
  10300. 165,0,255,218,130,121,153,252,239,54,168,48,6,90,130,1,0,39,75,80,72,8,9,
  10301. 33,186,130,80,64,76,13,212,19,2,130,96,110,150,173,0,65,6,51,212,20,128,65,
  10302. 17,11,212,19,130,137,121,211,210,209,144,6,39,75,80,80,0,201,119,234,10,8,
  10303. 41,86,231,71,80,80,129,79,135,186,122,69,224,34,25,69,233,80,3,91,141,168,
  10304. 40,96,139,113,180,181,5,36,21,110,54,142,134,160,165,1,176,23,211,47,0,216,
  10305. 134,233,215,128,111,117,181,104,128,209,3,70,230,106,64,5,139,168,209,234,
  10306. 10,32,36,144,102,234,136,3,146,27,168,40,160,146,132,103,168,40,192,115,3,
  10307. 117,5,28,22,113,163,69,168,41,103,1,66,188,17,145,52,40,4,202,113,67,76,
  10308. 130,227,68,194,13,240,108,0,0,83,96,0,2,161,0,104,146,84,97,48,0,1,78,192,
  10309. 56,169,24,145,179,192,0,5,48,8,56,16,32,128,56,18,52,125,166,86,147,182,
  10310. 140,28,50,21,13,39,31,23,60,145,158,56,140,141,47,113,6,155,186,188,24,49,
  10311. 39,199,89,188,124,92,242,70,120,224,201,33,69,15,155,163,197,68,14,49,39,
  10312. 199,197,211,116,240,242,113,197,230,18,180,253,228,3,17,46,18,243,35,100,
  10313. 128,172,156,114,70,163,146,76,34,248,146,164,108,248,75,204,141,146,28,217,
  10314. 114,137,27,78,251,241,173,234,162,160,225,1,3,34,92,170,9,105,164,32,225,
  10315. 64,131,155,1,193,133,7,19,39,22,70,154,103,143,128,0,10,176,20,28,76,156,
  10316. 113,75,34,78,62,0,0,43,0,48,104,82,146,49,240,0,1,84,11,180,192,0,5,114,1,
  10317. 18,160,65,24,131,20,145,25,172,48,132,122,28,76,146,218,121,35,180,69,145,
  10318. 132,108,224,31,248,0,0,0,0,0,0,25,172,56,132,122,28,76,146,218,121,35,180,
  10319. 69,145,132,108,224,31,248,0,0,0,0,0,0,40,160,45,110,23,30,176,33,184,0,0,
  10320. 175,32,29,235,2,27,199,23,0,0,22,4,51,88,129,8,244,56,153,37,180,242,71,
  10321. 104,139,35,8,217,192,63,240,0,0,0,0,0,0,51,88,145,8,244,56,153,37,180,242,
  10322. 71,104,139,35,8,217,192,64,0,0,0,0,0,0,0,51,88,161,8,244,56,153,37,180,242,
  10323. 71,104,139,35,8,217,192,64,0,0,0,0,0,0,0,51,88,177,8,244,56,153,37,180,242,
  10324. 71,104,139,35,8,217,192,64,16,0,0,0,0,0,0,51,88,193,8,244,56,153,37,180,
  10325. 242,71,104,139,35,8,217,192,64,16,0,0,0,0,0,0,51,88,209,8,244,56,153,37,
  10326. 180,242,71,104,139,35,8,217,192,64,16,0,0,0,0,0,0,51,88,225,8,244,56,153,
  10327. 37,180,242,71,104,139,35,8,217,192,64,32,0,0,0,0,0,0,32,227,194,0,97,57,
  10328. 162,4,245,232,5,34,92,35,68,225,161,166,218,16,16,137,112,52,41,73,29,153,
  10329. 1,65,196,201,197,145,166,153,245,200,3,137,204,120,34,74,8,200,58,112,28,
  10330. 211,32,130,52,78,26,26,110,248,0,0,164,4,12,70,137,195,39,252,73,240,117,
  10331. 32,57,168,97,4,104,156,52,52,221,255,160,20,160,152,23,223,250,32,148,25,
  10332. 174,137,58,23,51,191,244,84,12,50,9,195,39,240,81,238,2,3,107,173,214,3,
  10333. 192,
  10334. };
  10335. #elif defined(DUK_USE_DOUBLE_ME)
  10336. DUK_INTERNAL const duk_uint8_t duk_builtins_data[4116] = {
  10337. 144,148,105,224,32,68,52,228,62,12,104,200,165,132,52,167,194,138,105,243,
  10338. 124,57,28,211,57,18,64,52,238,126,44,138,111,171,241,164,19,87,129,30,33,
  10339. 167,16,145,159,8,211,136,9,225,42,5,240,145,139,163,163,8,211,136,10,228,
  10340. 64,211,19,132,140,93,29,56,70,156,64,119,34,66,146,36,104,137,194,70,46,
  10341. 142,172,35,78,32,47,146,195,102,11,240,145,139,163,175,8,211,136,9,228,240,
  10342. 242,112,145,139,163,179,8,211,136,8,237,34,130,118,49,116,118,225,26,48,0,
  10343. 1,82,29,201,158,46,183,39,135,147,132,140,93,16,132,76,66,33,8,66,16,132,
  10344. 33,8,66,26,179,233,97,167,60,150,34,33,154,112,0,1,75,247,35,79,95,237,198,
  10345. 174,200,47,31,23,95,17,13,51,19,35,93,68,216,209,128,0,10,208,174,79,15,32,
  10346. 248,8,196,24,8,107,192,0,5,106,118,27,94,0,0,43,83,227,94,0,0,43,84,46,215,
  10347. 128,0,10,213,28,198,188,0,0,86,169,100,53,224,0,2,181,79,85,175,0,0,21,170,
  10348. 154,45,120,0,0,173,85,217,107,192,0,5,106,182,243,86,193,106,52,127,130,
  10349. 249,50,94,124,35,68,225,146,49,13,31,186,23,201,146,243,224,200,39,12,145,
  10350. 136,67,134,19,49,0,0,3,225,252,0,0,0,3,51,0,0,3,193,252,0,0,0,3,47,18,1,
  10351. 172,19,120,71,10,25,196,136,113,162,156,136,199,42,57,204,144,115,132,240,
  10352. 149,2,248,72,197,209,58,2,185,16,52,196,225,35,23,68,233,14,228,72,82,68,
  10353. 141,17,56,72,197,209,58,130,249,44,54,96,191,9,24,186,39,88,79,39,135,147,
  10354. 132,140,93,19,176,35,180,138,9,216,197,209,59,82,79,35,40,242,65,248,58,42,
  10355. 96,121,14,232,94,62,46,190,15,42,31,145,33,86,65,76,242,214,143,73,48,242,
  10356. 243,79,49,56,243,115,207,57,64,243,180,79,61,72,243,244,207,65,80,244,53,
  10357. 79,69,88,244,98,30,8,200,156,67,102,120,241,79,4,100,78,21,110,4,207,32,47,
  10358. 147,37,231,194,52,78,25,34,122,81,124,153,47,62,12,130,112,201,19,211,139,
  10359. 121,34,87,69,128,104,137,239,83,18,238,108,165,2,162,92,104,56,220,233,1,8,
  10360. 151,10,134,162,100,206,16,18,50,9,195,39,105,20,101,136,18,25,4,225,147,
  10361. 180,138,5,215,49,238,105,27,60,185,1,36,104,156,50,118,145,70,96,129,34,52,
  10362. 78,25,59,72,160,93,115,30,230,145,179,204,144,12,73,8,15,38,104,128,138,52,
  10363. 146,16,30,77,1,0,2,11,132,193,198,36,248,248,186,110,158,30,78,56,188,194,
  10364. 70,183,170,136,48,98,79,142,179,120,248,185,228,140,241,193,146,66,138,31,
  10365. 55,71,138,128,153,137,62,58,205,227,226,231,146,51,199,26,6,18,92,146,64,
  10366. 96,74,72,51,120,43,192,97,68,128,153,56,72,7,12,133,67,73,199,197,207,36,
  10367. 103,142,35,2,3,33,80,210,113,241,115,201,25,160,146,225,160,9,34,1,124,178,
  10368. 1,139,18,19,36,229,146,8,190,36,169,27,62,18,243,35,100,135,54,92,162,2,17,
  10369. 46,72,128,89,7,200,32,33,18,225,98,236,145,188,130,64,196,75,132,188,200,
  10370. 217,32,43,39,28,128,69,19,18,228,144,42,98,79,142,179,120,248,185,228,140,
  10371. 241,201,97,129,114,229,201,37,2,68,184,200,1,147,93,159,153,213,34,235,250,
  10372. 96,48,157,32,24,94,160,1,199,4,184,235,55,143,139,158,72,207,28,226,3,81,
  10373. 46,62,46,155,167,135,147,142,47,60,129,71,197,207,36,103,142,34,92,35,104,
  10374. 194,68,1,89,58,36,8,109,109,12,133,67,73,195,18,115,36,118,182,185,168,8,
  10375. 109,109,12,133,67,73,201,18,115,36,118,182,185,168,130,27,91,75,115,149,71,
  10376. 240,196,156,201,29,173,174,129,2,27,91,75,115,149,71,242,68,156,201,29,173,
  10377. 174,129,34,12,16,28,128,62,191,42,3,71,146,68,4,16,22,188,161,240,16,40,
  10378. 104,242,103,196,16,93,158,125,96,110,115,235,64,131,16,16,58,37,192,70,32,
  10379. 194,144,114,25,67,95,40,6,18,8,32,48,156,209,2,108,124,96,224,144,6,247,62,
  10380. 16,0,143,164,143,12,248,15,18,84,145,145,34,128,11,35,160,179,140,0,44,150,
  10381. 129,18,58,0,146,116,103,32,128,105,61,104,17,36,175,1,232,217,29,5,156,179,
  10382. 224,58,26,50,95,142,43,159,64,181,130,83,226,26,50,95,142,43,159,192,7,255,
  10383. 248,41,42,72,226,1,160,18,78,97,32,26,64,114,186,60,32,4,120,6,148,13,128,
  10384. 124,3,76,12,84,46,100,140,3,78,13,18,14,130,36,67,232,23,18,14,130,39,34,
  10385. 131,30,113,15,224,3,255,253,6,48,40,194,197,204,224,142,8,240,78,25,60,231,
  10386. 192,210,197,204,224,156,50,113,238,67,103,232,62,28,138,156,104,82,170,107,
  10387. 255,32,48,191,144,1,132,112,71,128,159,168,128,161,28,17,224,156,50,112,19,
  10388. 245,144,22,39,12,156,123,144,217,240,19,245,146,3,9,205,16,39,236,62,3,161,
  10389. 163,37,248,226,251,141,1,107,4,167,196,52,100,191,28,95,113,164,13,91,132,
  10390. 5,147,130,115,30,8,147,222,64,43,1,49,31,224,64,60,72,245,128,68,249,32,13,
  10391. 34,2,34,63,204,128,89,45,2,39,209,0,89,61,104,159,213,0,153,80,50,156,80,
  10392. 211,126,16,11,155,184,183,88,145,224,129,34,122,64,17,155,184,183,8,11,39,
  10393. 22,235,18,60,16,36,79,72,1,115,119,40,247,146,60,16,36,79,72,32,140,221,
  10394. 197,184,64,89,57,71,188,145,224,129,34,122,65,1,39,20,51,244,0,52,72,242,2,
  10395. 127,18,2,165,48,70,114,229,145,51,253,141,1,4,104,229,203,34,103,251,26,64,
  10396. 132,52,75,160,201,47,105,160,26,84,12,167,31,186,8,50,0,114,58,113,163,46,
  10397. 190,120,35,11,60,4,25,68,81,61,96,47,181,80,46,132,129,255,255,222,255,255,
  10398. 255,255,254,39,172,67,118,170,5,208,144,0,0,0,0,0,64,0,0,51,16,0,0,62,31,
  10399. 192,0,0,0,8,245,238,146,38,138,147,105,13,42,26,137,226,0,0,7,131,248,0,0,
  10400. 0,1,30,180,134,4,209,82,109,33,165,67,81,60,64,0,0,240,255,0,0,0,0,15,210,
  10401. 62,72,91,155,0,0,2,192,240,0,0,0,0,135,88,11,237,72,5,38,210,27,50,24,145,
  10402. 129,255,254,126,135,255,255,255,254,67,172,67,118,164,2,147,105,13,153,12,
  10403. 72,192,255,255,63,195,255,255,255,255,16,240,70,68,226,27,51,199,138,120,
  10404. 35,34,112,171,112,38,121,7,16,137,112,168,106,38,77,193,1,40,151,16,217,
  10405. 144,196,142,224,144,21,18,227,65,198,238,9,67,81,46,72,5,39,16,217,144,196,
  10406. 142,224,152,228,148,227,64,0,0,0,0,0,0,0,0,131,175,223,16,194,111,8,97,119,
  10407. 224,3,205,220,42,46,65,238,200,13,155,184,75,189,205,35,102,128,47,116,64,
  10408. 92,221,199,196,130,68,144,230,239,72,65,152,12,21,224,140,137,92,128,62,
  10409. 210,98,177,252,3,107,173,88,3,146,211,141,32,0,3,225,252,0,0,0,3,19,175,
  10410. 188,0,100,221,193,130,100,228,167,20,52,215,129,3,38,238,77,12,39,37,56,
  10411. 161,166,188,10,194,94,6,18,155,184,183,8,11,39,6,9,147,146,156,80,211,94,7,
  10412. 18,155,184,183,8,11,39,38,134,19,146,156,80,211,94,8,12,53,224,130,195,222,
  10413. 8,77,133,210,24,91,224,3,152,147,228,208,194,95,0,44,196,159,11,69,175,152,
  10414. 32,35,100,33,135,24,147,237,38,34,246,139,95,48,64,70,200,68,8,49,39,198,
  10415. 57,179,61,144,138,22,98,79,180,152,153,215,54,103,178,17,129,204,73,240,96,
  10416. 153,44,132,112,163,18,125,164,196,62,130,100,178,18,1,140,73,240,96,197,
  10417. 144,146,18,98,79,180,152,135,208,98,200,74,8,49,39,195,186,145,149,144,150,
  10418. 22,98,79,180,152,143,215,82,50,178,19,2,140,73,241,136,109,38,73,89,9,161,
  10419. 166,36,251,73,137,157,67,105,50,74,200,78,10,49,39,201,16,78,104,229,100,
  10420. 39,134,152,147,237,38,41,116,130,115,71,43,33,64,60,196,159,24,133,173,18,
  10421. 32,156,209,202,200,81,18,49,39,218,76,76,234,22,180,72,130,115,71,43,33,72,
  10422. 68,196,159,38,134,19,46,105,56,226,150,68,157,160,1,228,73,242,104,97,46,
  10423. 16,31,34,79,140,66,214,137,16,78,104,229,108,169,137,72,147,237,38,38,117,
  10424. 11,90,36,65,57,163,149,178,168,21,34,79,146,32,156,209,202,218,250,161,178,
  10425. 36,251,73,138,93,32,156,209,202,218,250,193,82,36,248,196,54,147,36,173,
  10426. 191,174,27,34,79,180,152,153,212,54,147,36,173,191,176,17,34,79,135,117,35,
  10427. 43,115,236,133,200,147,237,38,35,245,212,140,173,207,180,15,34,79,131,4,
  10428. 201,108,173,133,72,147,237,38,33,244,19,37,178,184,17,34,79,140,115,102,
  10429. 123,107,238,133,200,147,237,38,38,117,205,153,237,175,188,23,34,79,133,162,
  10430. 215,204,16,17,182,254,248,116,137,62,210,98,47,104,181,243,4,4,109,191,192,
  10431. 131,152,147,230,8,8,217,12,16,60,137,62,96,128,141,178,193,160,206,1,201,
  10432. 176,113,146,0,0,0,0,0,0,0,0,49,185,252,65,137,207,227,37,215,207,227,12,86,
  10433. 127,24,152,188,254,49,88,33,46,65,120,72,4,153,37,63,33,13,127,148,4,26,0,
  10434. 57,62,6,228,163,228,74,86,215,62,55,28,110,179,226,113,70,223,62,47,24,38,
  10435. 191,30,2,125,32,40,20,87,114,41,225,42,5,240,145,139,163,145,41,68,250,128,
  10436. 80,41,174,228,85,200,129,166,39,9,24,186,57,18,148,79,172,5,2,170,238,69,
  10437. 220,137,10,72,145,162,39,9,24,186,57,18,148,79,176,5,2,186,238,69,124,150,
  10438. 27,48,95,132,140,93,28,137,74,39,218,2,129,101,119,34,158,79,15,39,9,24,
  10439. 186,57,18,148,79,184,5,2,218,238,69,29,164,80,78,198,46,142,68,165,16,64,
  10440. 28,24,61,73,25,33,205,128,1,167,166,128,0,0,0,1,108,242,151,15,39,8,34,26,
  10441. 87,97,200,3,0,167,129,32,8,194,195,16,6,84,55,10,60,3,35,69,132,30,1,140,
  10442. 130,193,143,1,196,230,60,2,158,8,131,153,64,115,42,46,191,176,8,194,246,0,
  10443. 80,5,220,193,95,6,234,5,100,225,35,23,71,35,6,228,140,93,29,180,55,108,145,
  10444. 139,163,182,112,52,107,67,76,56,3,153,132,20,28,76,156,89,26,105,158,62,0,
  10445. 0,42,193,2,201,104,17,41,34,156,204,176,160,226,100,226,200,211,76,241,240,
  10446. 0,1,86,2,131,137,147,142,41,100,73,199,192,0,5,96,6,13,10,82,70,62,0,0,42,
  10447. 130,88,115,18,124,67,103,177,69,49,129,6,36,249,68,54,123,20,82,216,65,137,
  10448. 62,33,179,209,214,162,152,208,147,18,124,162,27,61,29,106,41,112,32,196,
  10449. 159,16,217,233,233,81,76,112,73,137,62,81,13,158,158,149,20,186,20,98,79,
  10450. 133,91,129,61,61,42,41,120,40,196,159,10,183,2,122,218,148,82,248,60,137,
  10451. 62,33,179,216,166,216,192,137,18,124,162,27,61,138,109,108,34,68,159,16,
  10452. 217,232,235,83,108,104,76,137,62,81,13,158,142,181,54,184,17,34,79,136,108,
  10453. 244,244,169,182,56,38,68,159,40,134,207,79,74,155,93,10,145,39,194,173,192,
  10454. 158,158,149,54,188,21,34,79,133,91,129,61,109,74,109,125,155,51,136,71,161,
  10455. 196,201,45,167,146,59,68,89,24,70,206,0,0,7,129,248,0,0,0,1,155,51,168,71,
  10456. 161,196,201,45,167,146,59,68,89,24,70,206,0,0,7,129,248,0,0,0,1,155,51,200,
  10457. 71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,7,129,248,0,0,0,1,155,51,
  10458. 232,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,2,0,0,0,0,1,155,52,
  10459. 8,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,2,0,0,0,0,1,155,52,40,
  10460. 71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,130,0,0,0,0,1,155,52,72,
  10461. 71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,130,0,0,0,0,1,155,52,
  10462. 104,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,0,130,0,0,0,0,1,155,
  10463. 52,136,71,161,196,201,45,167,146,59,68,89,24,70,206,0,0,1,2,0,0,0,0,1,135,
  10464. 52,166,32,76,72,1,246,136,235,103,177,69,0,136,144,3,226,27,61,138,41,44,
  10465. 50,36,0,251,68,117,179,209,214,234,201,69,16,50,36,0,251,68,117,179,209,
  10466. 214,232,73,69,34,5,196,128,31,16,217,232,235,117,100,162,147,2,226,64,15,
  10467. 136,108,244,117,186,18,81,74,129,145,32,7,218,35,173,158,158,151,86,74,40,
  10468. 161,145,32,7,218,35,173,158,158,151,66,74,41,20,46,36,0,248,134,207,79,75,
  10469. 171,37,20,154,23,18,0,124,67,103,167,165,208,146,138,85,11,137,0,62,21,110,
  10470. 4,250,178,81,70,11,137,0,62,21,110,4,250,18,81,72,193,145,32,7,193,186,129,
  10471. 89,58,178,81,71,12,137,0,62,13,212,10,201,208,146,138,71,10,137,0,62,209,
  10472. 29,108,250,178,81,104,1,81,32,7,218,35,173,159,66,74,45,32,38,36,0,248,134,
  10473. 207,171,37,22,160,19,18,0,124,67,103,208,146,139,88,10,180,81,50,118,136,
  10474. 235,103,177,77,128,155,69,19,39,16,217,236,83,105,97,182,138,38,78,209,29,
  10475. 108,244,117,186,178,83,100,13,180,81,50,118,136,235,103,163,173,208,146,
  10476. 155,68,12,180,81,50,113,13,158,142,183,86,74,109,48,50,209,68,201,196,54,
  10477. 122,58,221,9,41,181,64,219,69,19,39,104,142,182,122,122,93,89,41,178,134,
  10478. 218,40,153,59,68,117,179,211,210,232,73,77,162,134,90,40,153,56,134,207,79,
  10479. 75,171,37,54,154,25,104,162,100,226,27,61,61,46,132,148,218,168,101,162,
  10480. 137,147,133,91,129,62,172,148,217,131,45,20,76,156,42,220,9,244,36,166,209,
  10481. 131,109,20,76,156,27,168,21,147,171,37,54,112,219,69,19,39,6,234,5,100,232,
  10482. 73,77,163,133,218,40,153,59,68,117,179,234,201,78,32,5,218,40,153,59,68,
  10483. 117,179,232,73,78,36,5,90,40,153,56,134,207,171,37,56,160,21,104,162,100,
  10484. 226,27,62,132,148,226,195,95,182,97,176,218,128,8,84,45,123,38,1,137,10,1,
  10485. 114,160,64,56,156,199,130,36,160,72,8,39,63,27,24,1,100,180,8,148,146,0,45,
  10486. 162,137,147,111,2,8,4,16,7,8,96,120,72,1,87,224,168,13,42,226,145,97,58,
  10487. 182,232,232,64,177,107,2,64,22,85,181,187,7,213,183,74,2,17,119,49,255,121,
  10488. 207,215,240,94,173,198,210,36,4,113,95,115,255,232,34,182,80,221,91,141,
  10489. 163,160,72,15,121,123,103,225,220,164,194,160,186,244,64,251,33,9,64,24,45,
  10490. 68,84,15,217,66,51,209,218,210,129,61,65,204,127,154,118,254,204,23,178,
  10491. 132,103,165,2,122,131,216,255,52,237,253,154,168,48,6,90,130,1,0,39,75,80,
  10492. 72,8,9,33,186,130,80,64,76,13,212,19,2,130,96,110,150,173,0,65,6,51,212,20,
  10493. 128,65,17,11,212,19,130,137,121,211,210,209,144,6,39,75,80,80,0,201,119,
  10494. 234,10,8,41,86,231,71,80,80,129,79,135,186,122,69,224,34,25,69,233,80,3,91,
  10495. 141,168,40,96,139,113,180,181,5,36,21,110,54,142,134,160,165,1,176,23,211,
  10496. 47,0,216,134,233,215,128,111,117,181,104,128,209,3,70,230,106,64,5,139,168,
  10497. 209,234,10,32,36,144,102,234,136,3,146,27,168,40,160,146,132,103,168,40,
  10498. 192,115,3,117,5,28,22,113,163,69,168,41,103,1,66,188,17,145,52,40,4,202,
  10499. 113,67,76,130,227,68,194,13,240,108,0,0,83,96,0,2,161,0,104,146,84,97,48,0,
  10500. 1,78,192,56,169,24,145,179,192,0,5,48,8,56,16,32,128,56,18,52,125,166,86,
  10501. 147,182,140,28,50,21,13,39,31,23,60,145,158,56,140,141,47,113,6,155,186,
  10502. 188,24,49,39,199,89,188,124,92,242,70,120,224,201,33,69,15,155,163,197,68,
  10503. 14,49,39,199,197,211,116,240,242,113,197,230,18,180,253,228,3,17,46,18,243,
  10504. 35,100,128,172,156,114,70,163,146,76,34,248,146,164,108,248,75,204,141,146,
  10505. 28,217,114,137,27,78,251,241,173,234,162,160,225,1,3,34,92,170,9,105,164,
  10506. 32,225,64,131,155,1,193,133,7,19,39,22,70,154,103,143,128,0,10,176,20,28,
  10507. 76,156,113,75,34,78,62,0,0,43,0,48,104,82,146,49,240,0,1,84,11,180,192,0,5,
  10508. 114,1,18,160,65,24,131,20,145,25,172,48,132,122,28,76,146,218,121,35,180,
  10509. 69,145,132,108,224,0,0,120,31,128,0,0,0,25,172,56,132,122,28,76,146,218,
  10510. 121,35,180,69,145,132,108,224,0,0,120,31,128,0,0,0,40,160,45,110,23,30,176,
  10511. 33,184,0,0,175,32,29,235,2,27,199,23,0,0,22,4,51,88,129,8,244,56,153,37,
  10512. 180,242,71,104,139,35,8,217,192,0,0,240,63,0,0,0,0,51,88,145,8,244,56,153,
  10513. 37,180,242,71,104,139,35,8,217,192,0,0,0,64,0,0,0,0,51,88,161,8,244,56,153,
  10514. 37,180,242,71,104,139,35,8,217,192,0,0,0,64,0,0,0,0,51,88,177,8,244,56,153,
  10515. 37,180,242,71,104,139,35,8,217,192,0,0,16,64,0,0,0,0,51,88,193,8,244,56,
  10516. 153,37,180,242,71,104,139,35,8,217,192,0,0,16,64,0,0,0,0,51,88,209,8,244,
  10517. 56,153,37,180,242,71,104,139,35,8,217,192,0,0,16,64,0,0,0,0,51,88,225,8,
  10518. 244,56,153,37,180,242,71,104,139,35,8,217,192,0,0,32,64,0,0,0,0,32,227,194,
  10519. 0,97,57,162,4,245,232,5,34,92,35,68,225,161,166,218,16,16,137,112,52,41,73,
  10520. 29,153,1,65,196,201,197,145,166,153,245,200,3,137,204,120,34,74,8,200,58,
  10521. 112,28,211,32,130,52,78,26,26,110,248,0,0,164,4,12,70,137,195,39,252,73,
  10522. 240,117,32,57,168,97,4,104,156,52,52,221,255,160,20,160,152,23,223,250,32,
  10523. 148,25,174,137,58,23,51,191,244,84,12,50,9,195,39,240,81,238,2,3,107,173,
  10524. 214,3,192,
  10525. };
  10526. #else
  10527. #error invalid endianness defines
  10528. #endif
  10529. #endif /* DUK_USE_ROM_OBJECTS */
  10530. /* automatic undefs */
  10531. #undef DUK__REFCINIT
  10532. #line 1 "duk_error_macros.c"
  10533. /*
  10534. * Error and fatal handling.
  10535. */
  10536. /* #include duk_internal.h -> already included */
  10537. #define DUK__ERRFMT_BUFSIZE 256 /* size for formatting buffers */
  10538. #if defined(DUK_USE_VERBOSE_ERRORS)
  10539. DUK_INTERNAL DUK_COLD void duk_err_handle_error_fmt(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *fmt, ...) {
  10540. va_list ap;
  10541. char msg[DUK__ERRFMT_BUFSIZE];
  10542. va_start(ap, fmt);
  10543. (void) DUK_VSNPRINTF(msg, sizeof(msg), fmt, ap);
  10544. msg[sizeof(msg) - 1] = (char) 0;
  10545. duk_err_create_and_throw(thr, (duk_errcode_t) (line_and_code >> 24), msg, filename, (duk_int_t) (line_and_code & 0x00ffffffL));
  10546. va_end(ap); /* dead code, but ensures portability (see Linux man page notes) */
  10547. }
  10548. DUK_INTERNAL DUK_COLD void duk_err_handle_error(duk_hthread *thr, const char *filename, duk_uint_t line_and_code, const char *msg) {
  10549. duk_err_create_and_throw(thr, (duk_errcode_t) (line_and_code >> 24), msg, filename, (duk_int_t) (line_and_code & 0x00ffffffL));
  10550. }
  10551. #else /* DUK_USE_VERBOSE_ERRORS */
  10552. DUK_INTERNAL DUK_COLD void duk_err_handle_error(duk_hthread *thr, duk_errcode_t code) {
  10553. duk_err_create_and_throw(thr, code);
  10554. }
  10555. #endif /* DUK_USE_VERBOSE_ERRORS */
  10556. /*
  10557. * Error throwing helpers
  10558. */
  10559. #if defined(DUK_USE_VERBOSE_ERRORS)
  10560. #if defined(DUK_USE_PARANOID_ERRORS)
  10561. DUK_INTERNAL DUK_COLD void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t idx, const char *expect_name) {
  10562. DUK_ERROR_RAW_FMT3(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, "%s required, found %s (stack index %ld)",
  10563. expect_name, duk_get_type_name(thr, idx), (long) idx);
  10564. }
  10565. #else
  10566. DUK_INTERNAL DUK_COLD void duk_err_require_type_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t idx, const char *expect_name) {
  10567. DUK_ERROR_RAW_FMT3(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, "%s required, found %s (stack index %ld)",
  10568. expect_name, duk_push_string_readable(thr, idx), (long) idx);
  10569. }
  10570. #endif
  10571. DUK_INTERNAL DUK_COLD void duk_err_error_internal(duk_hthread *thr, const char *filename, duk_int_t linenumber) {
  10572. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_ERROR, DUK_STR_INTERNAL_ERROR);
  10573. }
  10574. DUK_INTERNAL DUK_COLD void duk_err_error_alloc_failed(duk_hthread *thr, const char *filename, duk_int_t linenumber) {
  10575. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_ERROR, DUK_STR_ALLOC_FAILED);
  10576. }
  10577. DUK_INTERNAL DUK_COLD void duk_err_error(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) {
  10578. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_ERROR, message);
  10579. }
  10580. DUK_INTERNAL DUK_COLD void duk_err_range(duk_hthread *thr, const char *filename, duk_int_t linenumber, const char *message) {
  10581. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_RANGE_ERROR, message);
  10582. }
  10583. DUK_INTERNAL DUK_COLD void duk_err_range_index(duk_hthread *thr, const char *filename, duk_int_t linenumber, duk_idx_t idx) {
  10584. DUK_ERROR_RAW_FMT1(thr, filename, linenumber, DUK_ERR_RANGE_ERROR, "invalid stack index %ld", (long) (idx));
  10585. }
  10586. DUK_INTERNAL DUK_COLD void duk_err_range_push_beyond(duk_hthread *thr, const char *filename, duk_int_t linenumber) {
  10587. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_RANGE_ERROR, DUK_STR_PUSH_BEYOND_ALLOC_STACK);
  10588. }
  10589. DUK_INTERNAL DUK_COLD void duk_err_type_invalid_args(duk_hthread *thr, const char *filename, duk_int_t linenumber) {
  10590. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_ARGS);
  10591. }
  10592. DUK_INTERNAL DUK_COLD void duk_err_type_invalid_state(duk_hthread *thr, const char *filename, duk_int_t linenumber) {
  10593. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_STATE);
  10594. }
  10595. DUK_INTERNAL DUK_COLD void duk_err_type_invalid_trap_result(duk_hthread *thr, const char *filename, duk_int_t linenumber) {
  10596. DUK_ERROR_RAW(thr, filename, linenumber, DUK_ERR_TYPE_ERROR, DUK_STR_INVALID_TRAP_RESULT);
  10597. }
  10598. #else
  10599. /* The file/line arguments are NULL and 0, they're ignored by DUK_ERROR_RAW()
  10600. * when non-verbose errors are used.
  10601. */
  10602. DUK_NORETURN(DUK_LOCAL_DECL void duk__err_shared(duk_hthread *thr, duk_errcode_t code));
  10603. DUK_LOCAL void duk__err_shared(duk_hthread *thr, duk_errcode_t code) {
  10604. DUK_ERROR_RAW(thr, NULL, 0, code, NULL);
  10605. }
  10606. DUK_INTERNAL DUK_COLD void duk_err_error(duk_hthread *thr) {
  10607. duk__err_shared(thr, DUK_ERR_ERROR);
  10608. }
  10609. DUK_INTERNAL DUK_COLD void duk_err_range(duk_hthread *thr) {
  10610. duk__err_shared(thr, DUK_ERR_RANGE_ERROR);
  10611. }
  10612. DUK_INTERNAL DUK_COLD void duk_err_eval(duk_hthread *thr) {
  10613. duk__err_shared(thr, DUK_ERR_EVAL_ERROR);
  10614. }
  10615. DUK_INTERNAL DUK_COLD void duk_err_reference(duk_hthread *thr) {
  10616. duk__err_shared(thr, DUK_ERR_REFERENCE_ERROR);
  10617. }
  10618. DUK_INTERNAL DUK_COLD void duk_err_syntax(duk_hthread *thr) {
  10619. duk__err_shared(thr, DUK_ERR_SYNTAX_ERROR);
  10620. }
  10621. DUK_INTERNAL DUK_COLD void duk_err_type(duk_hthread *thr) {
  10622. duk__err_shared(thr, DUK_ERR_TYPE_ERROR);
  10623. }
  10624. DUK_INTERNAL DUK_COLD void duk_err_uri(duk_hthread *thr) {
  10625. duk__err_shared(thr, DUK_ERR_URI_ERROR);
  10626. }
  10627. #endif
  10628. /*
  10629. * Default fatal error handler
  10630. */
  10631. DUK_INTERNAL DUK_COLD void duk_default_fatal_handler(void *udata, const char *msg) {
  10632. DUK_UNREF(udata);
  10633. DUK_UNREF(msg);
  10634. msg = msg ? msg : "NULL";
  10635. #if defined(DUK_USE_FATAL_HANDLER)
  10636. /* duk_config.h provided a custom default fatal handler. */
  10637. DUK_D(DUK_DPRINT("custom default fatal error handler called: %s", msg));
  10638. DUK_USE_FATAL_HANDLER(udata, msg);
  10639. #elif defined(DUK_USE_CPP_EXCEPTIONS)
  10640. /* With C++ use a duk_fatal_exception which user code can catch in
  10641. * a natural way.
  10642. */
  10643. DUK_D(DUK_DPRINT("built-in default C++ fatal error handler called: %s", msg));
  10644. throw duk_fatal_exception(msg);
  10645. #else
  10646. /* Default behavior is to abort() on error. There's no printout
  10647. * which makes this awkward, so it's always recommended to use an
  10648. * explicit fatal error handler.
  10649. *
  10650. * ====================================================================
  10651. * NOTE: If you are seeing this, you are most likely dealing with an
  10652. * uncaught error. You should provide a fatal error handler in Duktape
  10653. * heap creation, and should consider using a protected call as your
  10654. * first call into an empty Duktape context to properly handle errors.
  10655. * See:
  10656. * - http://duktape.org/guide.html#error-handling
  10657. * - http://wiki.duktape.org/HowtoFatalErrors.html
  10658. * - http://duktape.org/api.html#taglist-protected
  10659. * ====================================================================
  10660. */
  10661. DUK_D(DUK_DPRINT("built-in default fatal error handler called: %s", msg));
  10662. DUK_ABORT();
  10663. #endif
  10664. DUK_D(DUK_DPRINT("fatal error handler returned, enter forever loop"));
  10665. for (;;) {
  10666. /* Loop forever to ensure we don't return. */
  10667. }
  10668. }
  10669. /* automatic undefs */
  10670. #undef DUK__ERRFMT_BUFSIZE
  10671. #line 1 "duk_unicode_support.c"
  10672. /*
  10673. * Various Unicode help functions for character classification predicates,
  10674. * case conversion, decoding, etc.
  10675. */
  10676. /* #include duk_internal.h -> already included */
  10677. /*
  10678. * Fast path tables
  10679. */
  10680. #if defined(DUK_USE_IDCHAR_FASTPATH)
  10681. DUK_INTERNAL const duk_int8_t duk_is_idchar_tab[128] = {
  10682. /* 0: not IdentifierStart or IdentifierPart
  10683. * 1: IdentifierStart and IdentifierPart
  10684. * -1: IdentifierPart only
  10685. */
  10686. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00...0x0f */
  10687. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10...0x1f */
  10688. 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20...0x2f */
  10689. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, /* 0x30...0x3f */
  10690. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40...0x4f */
  10691. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50...0x5f */
  10692. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60...0x6f */
  10693. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70...0x7f */
  10694. };
  10695. #endif
  10696. /*
  10697. * XUTF-8 and CESU-8 encoding/decoding
  10698. */
  10699. DUK_INTERNAL duk_small_int_t duk_unicode_get_xutf8_length(duk_ucodepoint_t cp) {
  10700. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  10701. if (x < 0x80UL) {
  10702. /* 7 bits */
  10703. return 1;
  10704. } else if (x < 0x800UL) {
  10705. /* 11 bits */
  10706. return 2;
  10707. } else if (x < 0x10000UL) {
  10708. /* 16 bits */
  10709. return 3;
  10710. } else if (x < 0x200000UL) {
  10711. /* 21 bits */
  10712. return 4;
  10713. } else if (x < 0x4000000UL) {
  10714. /* 26 bits */
  10715. return 5;
  10716. } else if (x < (duk_ucodepoint_t) 0x80000000UL) {
  10717. /* 31 bits */
  10718. return 6;
  10719. } else {
  10720. /* 36 bits */
  10721. return 7;
  10722. }
  10723. }
  10724. #if defined(DUK_USE_ASSERTIONS)
  10725. DUK_INTERNAL duk_small_int_t duk_unicode_get_cesu8_length(duk_ucodepoint_t cp) {
  10726. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  10727. if (x < 0x80UL) {
  10728. /* 7 bits */
  10729. return 1;
  10730. } else if (x < 0x800UL) {
  10731. /* 11 bits */
  10732. return 2;
  10733. } else if (x < 0x10000UL) {
  10734. /* 16 bits */
  10735. return 3;
  10736. } else {
  10737. /* Encoded as surrogate pair, each encoding to 3 bytes for
  10738. * 6 bytes total. Codepoints above U+10FFFF encode as 6 bytes
  10739. * too, see duk_unicode_encode_cesu8().
  10740. */
  10741. return 3 + 3;
  10742. }
  10743. }
  10744. #endif /* DUK_USE_ASSERTIONS */
  10745. DUK_INTERNAL const duk_uint8_t duk_unicode_xutf8_markers[7] = {
  10746. 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe
  10747. };
  10748. /* Encode to extended UTF-8; 'out' must have space for at least
  10749. * DUK_UNICODE_MAX_XUTF8_LENGTH bytes. Allows encoding of any
  10750. * 32-bit (unsigned) codepoint.
  10751. */
  10752. DUK_INTERNAL duk_small_int_t duk_unicode_encode_xutf8(duk_ucodepoint_t cp, duk_uint8_t *out) {
  10753. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  10754. duk_small_int_t len;
  10755. duk_uint8_t marker;
  10756. duk_small_int_t i;
  10757. len = duk_unicode_get_xutf8_length(cp);
  10758. DUK_ASSERT(len > 0);
  10759. marker = duk_unicode_xutf8_markers[len - 1]; /* 64-bit OK because always >= 0 */
  10760. i = len;
  10761. DUK_ASSERT(i > 0);
  10762. do {
  10763. i--;
  10764. if (i > 0) {
  10765. out[i] = (duk_uint8_t) (0x80 + (x & 0x3f));
  10766. x >>= 6;
  10767. } else {
  10768. /* Note: masking of 'x' is not necessary because of
  10769. * range check and shifting -> no bits overlapping
  10770. * the marker should be set.
  10771. */
  10772. out[0] = (duk_uint8_t) (marker + x);
  10773. }
  10774. } while (i > 0);
  10775. return len;
  10776. }
  10777. /* Encode to CESU-8; 'out' must have space for at least
  10778. * DUK_UNICODE_MAX_CESU8_LENGTH bytes; codepoints above U+10FFFF
  10779. * will encode to garbage but won't overwrite the output buffer.
  10780. */
  10781. DUK_INTERNAL duk_small_int_t duk_unicode_encode_cesu8(duk_ucodepoint_t cp, duk_uint8_t *out) {
  10782. duk_uint_fast32_t x = (duk_uint_fast32_t) cp;
  10783. duk_small_int_t len;
  10784. if (x < 0x80UL) {
  10785. out[0] = (duk_uint8_t) x;
  10786. len = 1;
  10787. } else if (x < 0x800UL) {
  10788. out[0] = (duk_uint8_t) (0xc0 + ((x >> 6) & 0x1f));
  10789. out[1] = (duk_uint8_t) (0x80 + (x & 0x3f));
  10790. len = 2;
  10791. } else if (x < 0x10000UL) {
  10792. /* surrogate pairs get encoded here */
  10793. out[0] = (duk_uint8_t) (0xe0 + ((x >> 12) & 0x0f));
  10794. out[1] = (duk_uint8_t) (0x80 + ((x >> 6) & 0x3f));
  10795. out[2] = (duk_uint8_t) (0x80 + (x & 0x3f));
  10796. len = 3;
  10797. } else {
  10798. /*
  10799. * Unicode codepoints above U+FFFF are encoded as surrogate
  10800. * pairs here. This ensures that all CESU-8 codepoints are
  10801. * 16-bit values as expected in ECMAScript. The surrogate
  10802. * pairs always get a 3-byte encoding (each) in CESU-8.
  10803. * See: http://en.wikipedia.org/wiki/Surrogate_pair
  10804. *
  10805. * 20-bit codepoint, 10 bits (A and B) per surrogate pair:
  10806. *
  10807. * x = 0b00000000 0000AAAA AAAAAABB BBBBBBBB
  10808. * sp1 = 0b110110AA AAAAAAAA (0xd800 + ((x >> 10) & 0x3ff))
  10809. * sp2 = 0b110111BB BBBBBBBB (0xdc00 + (x & 0x3ff))
  10810. *
  10811. * Encoded into CESU-8:
  10812. *
  10813. * sp1 -> 0b11101101 (0xe0 + ((sp1 >> 12) & 0x0f))
  10814. * -> 0b1010AAAA (0x80 + ((sp1 >> 6) & 0x3f))
  10815. * -> 0b10AAAAAA (0x80 + (sp1 & 0x3f))
  10816. * sp2 -> 0b11101101 (0xe0 + ((sp2 >> 12) & 0x0f))
  10817. * -> 0b1011BBBB (0x80 + ((sp2 >> 6) & 0x3f))
  10818. * -> 0b10BBBBBB (0x80 + (sp2 & 0x3f))
  10819. *
  10820. * Note that 0x10000 must be subtracted first. The code below
  10821. * avoids the sp1, sp2 temporaries which saves around 20 bytes
  10822. * of code.
  10823. */
  10824. x -= 0x10000UL;
  10825. out[0] = (duk_uint8_t) (0xed);
  10826. out[1] = (duk_uint8_t) (0xa0 + ((x >> 16) & 0x0f));
  10827. out[2] = (duk_uint8_t) (0x80 + ((x >> 10) & 0x3f));
  10828. out[3] = (duk_uint8_t) (0xed);
  10829. out[4] = (duk_uint8_t) (0xb0 + ((x >> 6) & 0x0f));
  10830. out[5] = (duk_uint8_t) (0x80 + (x & 0x3f));
  10831. len = 6;
  10832. }
  10833. return len;
  10834. }
  10835. /* Decode helper. Return zero on error. */
  10836. DUK_INTERNAL duk_small_int_t duk_unicode_decode_xutf8(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_ucodepoint_t *out_cp) {
  10837. const duk_uint8_t *p;
  10838. duk_uint32_t res;
  10839. duk_uint_fast8_t ch;
  10840. duk_small_int_t n;
  10841. DUK_UNREF(thr);
  10842. p = *ptr;
  10843. if (p < ptr_start || p >= ptr_end) {
  10844. goto fail;
  10845. }
  10846. /*
  10847. * UTF-8 decoder which accepts longer than standard byte sequences.
  10848. * This allows full 32-bit code points to be used.
  10849. */
  10850. ch = (duk_uint_fast8_t) (*p++);
  10851. if (ch < 0x80) {
  10852. /* 0xxx xxxx [7 bits] */
  10853. res = (duk_uint32_t) (ch & 0x7f);
  10854. n = 0;
  10855. } else if (ch < 0xc0) {
  10856. /* 10xx xxxx -> invalid */
  10857. goto fail;
  10858. } else if (ch < 0xe0) {
  10859. /* 110x xxxx 10xx xxxx [11 bits] */
  10860. res = (duk_uint32_t) (ch & 0x1f);
  10861. n = 1;
  10862. } else if (ch < 0xf0) {
  10863. /* 1110 xxxx 10xx xxxx 10xx xxxx [16 bits] */
  10864. res = (duk_uint32_t) (ch & 0x0f);
  10865. n = 2;
  10866. } else if (ch < 0xf8) {
  10867. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx [21 bits] */
  10868. res = (duk_uint32_t) (ch & 0x07);
  10869. n = 3;
  10870. } else if (ch < 0xfc) {
  10871. /* 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [26 bits] */
  10872. res = (duk_uint32_t) (ch & 0x03);
  10873. n = 4;
  10874. } else if (ch < 0xfe) {
  10875. /* 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [31 bits] */
  10876. res = (duk_uint32_t) (ch & 0x01);
  10877. n = 5;
  10878. } else if (ch < 0xff) {
  10879. /* 1111 1110 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [36 bits] */
  10880. res = (duk_uint32_t) (0);
  10881. n = 6;
  10882. } else {
  10883. /* 8-byte format could be:
  10884. * 1111 1111 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx [41 bits]
  10885. *
  10886. * However, this format would not have a zero bit following the
  10887. * leading one bits and would not allow 0xFF to be used as an
  10888. * "invalid xutf-8" marker for internal keys. Further, 8-byte
  10889. * encodings (up to 41 bit code points) are not currently needed.
  10890. */
  10891. goto fail;
  10892. }
  10893. DUK_ASSERT(p >= ptr_start); /* verified at beginning */
  10894. if (p + n > ptr_end) {
  10895. /* check pointer at end */
  10896. goto fail;
  10897. }
  10898. while (n > 0) {
  10899. DUK_ASSERT(p >= ptr_start && p < ptr_end);
  10900. ch = (duk_uint_fast8_t) (*p++);
  10901. #if 0
  10902. if (ch & 0xc0 != 0x80) {
  10903. /* not a continuation byte */
  10904. p--;
  10905. *ptr = p;
  10906. *out_cp = DUK_UNICODE_CP_REPLACEMENT_CHARACTER;
  10907. return 1;
  10908. }
  10909. #endif
  10910. res = (res << 6) + (duk_uint32_t) (ch & 0x3f);
  10911. n--;
  10912. }
  10913. *ptr = p;
  10914. *out_cp = res;
  10915. return 1;
  10916. fail:
  10917. return 0;
  10918. }
  10919. /* used by e.g. duk_regexp_executor.c, string built-ins */
  10920. DUK_INTERNAL duk_ucodepoint_t duk_unicode_decode_xutf8_checked(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end) {
  10921. duk_ucodepoint_t cp;
  10922. if (duk_unicode_decode_xutf8(thr, ptr, ptr_start, ptr_end, &cp)) {
  10923. return cp;
  10924. }
  10925. DUK_ERROR_INTERNAL(thr);
  10926. DUK_WO_NORETURN(return 0;);
  10927. }
  10928. /* Compute (extended) utf-8 length without codepoint encoding validation,
  10929. * used for string interning.
  10930. *
  10931. * NOTE: This algorithm is performance critical, more so than string hashing
  10932. * in some cases. It is needed when interning a string and needs to scan
  10933. * every byte of the string with no skipping. Having an ASCII fast path
  10934. * is useful if possible in the algorithm. The current algorithms were
  10935. * chosen from several variants, based on x64 gcc -O2 testing. See:
  10936. * https://github.com/svaarala/duktape/pull/422
  10937. *
  10938. * NOTE: must match tools/dukutil.py:duk_unicode_unvalidated_utf8_length().
  10939. */
  10940. #if defined(DUK_USE_PREFER_SIZE)
  10941. /* Small variant; roughly 150 bytes smaller than the fast variant. */
  10942. DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) {
  10943. const duk_uint8_t *p;
  10944. const duk_uint8_t *p_end;
  10945. duk_size_t ncont;
  10946. duk_size_t clen;
  10947. p = data;
  10948. p_end = data + blen;
  10949. ncont = 0;
  10950. while (p != p_end) {
  10951. duk_uint8_t x;
  10952. x = *p++;
  10953. if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) {
  10954. ncont++;
  10955. }
  10956. }
  10957. DUK_ASSERT(ncont <= blen);
  10958. clen = blen - ncont;
  10959. DUK_ASSERT(clen <= blen);
  10960. return clen;
  10961. }
  10962. #else /* DUK_USE_PREFER_SIZE */
  10963. /* This seems like a good overall approach. Fast path for ASCII in 4 byte
  10964. * blocks.
  10965. */
  10966. DUK_INTERNAL duk_size_t duk_unicode_unvalidated_utf8_length(const duk_uint8_t *data, duk_size_t blen) {
  10967. const duk_uint8_t *p;
  10968. const duk_uint8_t *p_end;
  10969. const duk_uint32_t *p32_end;
  10970. const duk_uint32_t *p32;
  10971. duk_size_t ncont;
  10972. duk_size_t clen;
  10973. ncont = 0; /* number of continuation (non-initial) bytes in [0x80,0xbf] */
  10974. p = data;
  10975. p_end = data + blen;
  10976. if (blen < 16) {
  10977. goto skip_fastpath;
  10978. }
  10979. /* Align 'p' to 4; the input data may have arbitrary alignment.
  10980. * End of string check not needed because blen >= 16.
  10981. */
  10982. while (((duk_size_t) (const void *) p) & 0x03U) {
  10983. duk_uint8_t x;
  10984. x = *p++;
  10985. if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) {
  10986. ncont++;
  10987. }
  10988. }
  10989. /* Full, aligned 4-byte reads. */
  10990. p32_end = (const duk_uint32_t *) (const void *) (p + ((duk_size_t) (p_end - p) & (duk_size_t) (~0x03)));
  10991. p32 = (const duk_uint32_t *) (const void *) p;
  10992. while (p32 != (const duk_uint32_t *) p32_end) {
  10993. duk_uint32_t x;
  10994. x = *p32++;
  10995. if (DUK_LIKELY((x & 0x80808080UL) == 0)) {
  10996. ; /* ASCII fast path */
  10997. } else {
  10998. /* Flip highest bit of each byte which changes
  10999. * the bit pattern 10xxxxxx into 00xxxxxx which
  11000. * allows an easy bit mask test.
  11001. */
  11002. x ^= 0x80808080UL;
  11003. if (DUK_UNLIKELY(!(x & 0xc0000000UL))) {
  11004. ncont++;
  11005. }
  11006. if (DUK_UNLIKELY(!(x & 0x00c00000UL))) {
  11007. ncont++;
  11008. }
  11009. if (DUK_UNLIKELY(!(x & 0x0000c000UL))) {
  11010. ncont++;
  11011. }
  11012. if (DUK_UNLIKELY(!(x & 0x000000c0UL))) {
  11013. ncont++;
  11014. }
  11015. }
  11016. }
  11017. p = (const duk_uint8_t *) p32;
  11018. /* Fall through to handle the rest. */
  11019. skip_fastpath:
  11020. while (p != p_end) {
  11021. duk_uint8_t x;
  11022. x = *p++;
  11023. if (DUK_UNLIKELY(x >= 0x80 && x <= 0xbf)) {
  11024. ncont++;
  11025. }
  11026. }
  11027. DUK_ASSERT(ncont <= blen);
  11028. clen = blen - ncont;
  11029. DUK_ASSERT(clen <= blen);
  11030. return clen;
  11031. }
  11032. #endif /* DUK_USE_PREFER_SIZE */
  11033. /*
  11034. * Unicode range matcher
  11035. *
  11036. * Matches a codepoint against a packed bitstream of character ranges.
  11037. * Used for slow path Unicode matching.
  11038. */
  11039. /* Must match tools/extract_chars.py, generate_match_table3(). */
  11040. DUK_LOCAL duk_uint32_t duk__uni_decode_value(duk_bitdecoder_ctx *bd_ctx) {
  11041. duk_uint32_t t;
  11042. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 4);
  11043. if (t <= 0x0eU) {
  11044. return t;
  11045. }
  11046. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 8);
  11047. if (t <= 0xfdU) {
  11048. return t + 0x0f;
  11049. }
  11050. if (t == 0xfeU) {
  11051. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 12);
  11052. return t + 0x0fU + 0xfeU;
  11053. } else {
  11054. t = (duk_uint32_t) duk_bd_decode(bd_ctx, 24);
  11055. return t + 0x0fU + 0xfeU + 0x1000UL;
  11056. }
  11057. }
  11058. DUK_LOCAL duk_small_int_t duk__uni_range_match(const duk_uint8_t *unitab, duk_size_t unilen, duk_codepoint_t cp) {
  11059. duk_bitdecoder_ctx bd_ctx;
  11060. duk_codepoint_t prev_re;
  11061. duk_memzero(&bd_ctx, sizeof(bd_ctx));
  11062. bd_ctx.data = (const duk_uint8_t *) unitab;
  11063. bd_ctx.length = (duk_size_t) unilen;
  11064. prev_re = 0;
  11065. for (;;) {
  11066. duk_codepoint_t r1, r2;
  11067. r1 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx);
  11068. if (r1 == 0) {
  11069. break;
  11070. }
  11071. r2 = (duk_codepoint_t) duk__uni_decode_value(&bd_ctx);
  11072. r1 = prev_re + r1;
  11073. r2 = r1 + r2;
  11074. prev_re = r2;
  11075. /* [r1,r2] is the range */
  11076. DUK_DDD(DUK_DDDPRINT("duk__uni_range_match: cp=%06lx range=[0x%06lx,0x%06lx]",
  11077. (unsigned long) cp, (unsigned long) r1, (unsigned long) r2));
  11078. if (cp >= r1 && cp <= r2) {
  11079. return 1;
  11080. }
  11081. }
  11082. return 0;
  11083. }
  11084. /*
  11085. * "WhiteSpace" production check.
  11086. */
  11087. DUK_INTERNAL duk_small_int_t duk_unicode_is_whitespace(duk_codepoint_t cp) {
  11088. /*
  11089. * E5 Section 7.2 specifies six characters specifically as
  11090. * white space:
  11091. *
  11092. * 0009;<control>;Cc;0;S;;;;;N;CHARACTER TABULATION;;;;
  11093. * 000B;<control>;Cc;0;S;;;;;N;LINE TABULATION;;;;
  11094. * 000C;<control>;Cc;0;WS;;;;;N;FORM FEED (FF);;;;
  11095. * 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  11096. * 00A0;NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;NON-BREAKING SPACE;;;;
  11097. * FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;;
  11098. *
  11099. * It also specifies any Unicode category 'Zs' characters as white
  11100. * space. These can be extracted with the "tools/extract_chars.py" script.
  11101. * Current result:
  11102. *
  11103. * RAW OUTPUT:
  11104. * ===========
  11105. * 0020;SPACE;Zs;0;WS;;;;;N;;;;;
  11106. * 00A0;NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;NON-BREAKING SPACE;;;;
  11107. * 1680;OGHAM SPACE MARK;Zs;0;WS;;;;;N;;;;;
  11108. * 180E;MONGOLIAN VOWEL SEPARATOR;Zs;0;WS;;;;;N;;;;;
  11109. * 2000;EN QUAD;Zs;0;WS;2002;;;;N;;;;;
  11110. * 2001;EM QUAD;Zs;0;WS;2003;;;;N;;;;;
  11111. * 2002;EN SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11112. * 2003;EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11113. * 2004;THREE-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11114. * 2005;FOUR-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11115. * 2006;SIX-PER-EM SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11116. * 2007;FIGURE SPACE;Zs;0;WS;<noBreak> 0020;;;;N;;;;;
  11117. * 2008;PUNCTUATION SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11118. * 2009;THIN SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11119. * 200A;HAIR SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11120. * 202F;NARROW NO-BREAK SPACE;Zs;0;CS;<noBreak> 0020;;;;N;;;;;
  11121. * 205F;MEDIUM MATHEMATICAL SPACE;Zs;0;WS;<compat> 0020;;;;N;;;;;
  11122. * 3000;IDEOGRAPHIC SPACE;Zs;0;WS;<wide> 0020;;;;N;;;;;
  11123. *
  11124. * RANGES:
  11125. * =======
  11126. * 0x0020
  11127. * 0x00a0
  11128. * 0x1680
  11129. * 0x180e
  11130. * 0x2000 ... 0x200a
  11131. * 0x202f
  11132. * 0x205f
  11133. * 0x3000
  11134. *
  11135. * A manual decoder (below) is probably most compact for this.
  11136. */
  11137. duk_uint_fast8_t lo;
  11138. duk_uint_fast32_t hi;
  11139. /* cp == -1 (EOF) never matches and causes return value 0 */
  11140. lo = (duk_uint_fast8_t) (cp & 0xff);
  11141. hi = (duk_uint_fast32_t) (cp >> 8); /* does not fit into an uchar */
  11142. if (hi == 0x0000UL) {
  11143. if (lo == 0x09U || lo == 0x0bU || lo == 0x0cU ||
  11144. lo == 0x20U || lo == 0xa0U) {
  11145. return 1;
  11146. }
  11147. } else if (hi == 0x0020UL) {
  11148. if (lo <= 0x0aU || lo == 0x2fU || lo == 0x5fU) {
  11149. return 1;
  11150. }
  11151. } else if (cp == 0x1680L || cp == 0x180eL || cp == 0x3000L ||
  11152. cp == 0xfeffL) {
  11153. return 1;
  11154. }
  11155. return 0;
  11156. }
  11157. /*
  11158. * "LineTerminator" production check.
  11159. */
  11160. DUK_INTERNAL duk_small_int_t duk_unicode_is_line_terminator(duk_codepoint_t cp) {
  11161. /*
  11162. * E5 Section 7.3
  11163. *
  11164. * A LineTerminatorSequence essentially merges <CR> <LF> sequences
  11165. * into a single line terminator. This must be handled by the caller.
  11166. */
  11167. if (cp == 0x000aL || cp == 0x000dL || cp == 0x2028L ||
  11168. cp == 0x2029L) {
  11169. return 1;
  11170. }
  11171. return 0;
  11172. }
  11173. /*
  11174. * "IdentifierStart" production check.
  11175. */
  11176. DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_start(duk_codepoint_t cp) {
  11177. /*
  11178. * E5 Section 7.6:
  11179. *
  11180. * IdentifierStart:
  11181. * UnicodeLetter
  11182. * $
  11183. * _
  11184. * \ UnicodeEscapeSequence
  11185. *
  11186. * IdentifierStart production has one multi-character production:
  11187. *
  11188. * \ UnicodeEscapeSequence
  11189. *
  11190. * The '\' character is -not- matched by this function. Rather, the caller
  11191. * should decode the escape and then call this function to check whether the
  11192. * decoded character is acceptable (see discussion in E5 Section 7.6).
  11193. *
  11194. * The "UnicodeLetter" alternative of the production allows letters
  11195. * from various Unicode categories. These can be extracted with the
  11196. * "tools/extract_chars.py" script.
  11197. *
  11198. * Because the result has hundreds of Unicode codepoint ranges, matching
  11199. * for any values >= 0x80 are done using a very slow range-by-range scan
  11200. * and a packed range format.
  11201. *
  11202. * The ASCII portion (codepoints 0x00 ... 0x7f) is fast-pathed below because
  11203. * it matters the most. The ASCII related ranges of IdentifierStart are:
  11204. *
  11205. * 0x0041 ... 0x005a ['A' ... 'Z']
  11206. * 0x0061 ... 0x007a ['a' ... 'z']
  11207. * 0x0024 ['$']
  11208. * 0x005f ['_']
  11209. */
  11210. /* ASCII (and EOF) fast path -- quick accept and reject */
  11211. if (cp <= 0x7fL) {
  11212. #if defined(DUK_USE_IDCHAR_FASTPATH)
  11213. return (cp >= 0) && (duk_is_idchar_tab[cp] > 0);
  11214. #else
  11215. if ((cp >= 'a' && cp <= 'z') ||
  11216. (cp >= 'A' && cp <= 'Z') ||
  11217. cp == '_' || cp == '$') {
  11218. return 1;
  11219. }
  11220. return 0;
  11221. #endif
  11222. }
  11223. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  11224. #if defined(DUK_USE_SOURCE_NONBMP)
  11225. if (duk__uni_range_match(duk_unicode_ids_noa,
  11226. (duk_size_t) sizeof(duk_unicode_ids_noa),
  11227. (duk_codepoint_t) cp)) {
  11228. return 1;
  11229. }
  11230. return 0;
  11231. #else
  11232. if (cp < 0x10000L) {
  11233. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  11234. sizeof(duk_unicode_ids_noabmp),
  11235. (duk_codepoint_t) cp)) {
  11236. return 1;
  11237. }
  11238. return 0;
  11239. } else {
  11240. /* without explicit non-BMP support, assume non-BMP characters
  11241. * are always accepted as identifier characters.
  11242. */
  11243. return 1;
  11244. }
  11245. #endif
  11246. }
  11247. /*
  11248. * "IdentifierPart" production check.
  11249. */
  11250. DUK_INTERNAL duk_small_int_t duk_unicode_is_identifier_part(duk_codepoint_t cp) {
  11251. /*
  11252. * E5 Section 7.6:
  11253. *
  11254. * IdentifierPart:
  11255. * IdentifierStart
  11256. * UnicodeCombiningMark
  11257. * UnicodeDigit
  11258. * UnicodeConnectorPunctuation
  11259. * <ZWNJ> [U+200C]
  11260. * <ZWJ> [U+200D]
  11261. *
  11262. * IdentifierPart production has one multi-character production
  11263. * as part of its IdentifierStart alternative. The '\' character
  11264. * of an escape sequence is not matched here, see discussion in
  11265. * duk_unicode_is_identifier_start().
  11266. *
  11267. * To match non-ASCII characters (codepoints >= 0x80), a very slow
  11268. * linear range-by-range scan is used. The codepoint is first compared
  11269. * to the IdentifierStart ranges, and if it doesn't match, then to a
  11270. * set consisting of code points in IdentifierPart but not in
  11271. * IdentifierStart. This is done to keep the unicode range data small,
  11272. * at the expense of speed.
  11273. *
  11274. * The ASCII fast path consists of:
  11275. *
  11276. * 0x0030 ... 0x0039 ['0' ... '9', UnicodeDigit]
  11277. * 0x0041 ... 0x005a ['A' ... 'Z', IdentifierStart]
  11278. * 0x0061 ... 0x007a ['a' ... 'z', IdentifierStart]
  11279. * 0x0024 ['$', IdentifierStart]
  11280. * 0x005f ['_', IdentifierStart and
  11281. * UnicodeConnectorPunctuation]
  11282. *
  11283. * UnicodeCombiningMark has no code points <= 0x7f.
  11284. *
  11285. * The matching code reuses the "identifier start" tables, and then
  11286. * consults a separate range set for characters in "identifier part"
  11287. * but not in "identifier start". These can be extracted with the
  11288. * "tools/extract_chars.py" script.
  11289. *
  11290. * UnicodeCombiningMark -> categories Mn, Mc
  11291. * UnicodeDigit -> categories Nd
  11292. * UnicodeConnectorPunctuation -> categories Pc
  11293. */
  11294. /* ASCII (and EOF) fast path -- quick accept and reject */
  11295. if (cp <= 0x7fL) {
  11296. #if defined(DUK_USE_IDCHAR_FASTPATH)
  11297. return (cp >= 0) && (duk_is_idchar_tab[cp] != 0);
  11298. #else
  11299. if ((cp >= 'a' && cp <= 'z') ||
  11300. (cp >= 'A' && cp <= 'Z') ||
  11301. (cp >= '0' && cp <= '9') ||
  11302. cp == '_' || cp == '$') {
  11303. return 1;
  11304. }
  11305. return 0;
  11306. #endif
  11307. }
  11308. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  11309. #if defined(DUK_USE_SOURCE_NONBMP)
  11310. if (duk__uni_range_match(duk_unicode_ids_noa,
  11311. sizeof(duk_unicode_ids_noa),
  11312. (duk_codepoint_t) cp) ||
  11313. duk__uni_range_match(duk_unicode_idp_m_ids_noa,
  11314. sizeof(duk_unicode_idp_m_ids_noa),
  11315. (duk_codepoint_t) cp)) {
  11316. return 1;
  11317. }
  11318. return 0;
  11319. #else
  11320. if (cp < 0x10000L) {
  11321. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  11322. sizeof(duk_unicode_ids_noabmp),
  11323. (duk_codepoint_t) cp) ||
  11324. duk__uni_range_match(duk_unicode_idp_m_ids_noabmp,
  11325. sizeof(duk_unicode_idp_m_ids_noabmp),
  11326. (duk_codepoint_t) cp)) {
  11327. return 1;
  11328. }
  11329. return 0;
  11330. } else {
  11331. /* without explicit non-BMP support, assume non-BMP characters
  11332. * are always accepted as identifier characters.
  11333. */
  11334. return 1;
  11335. }
  11336. #endif
  11337. }
  11338. /*
  11339. * Unicode letter check.
  11340. */
  11341. DUK_INTERNAL duk_small_int_t duk_unicode_is_letter(duk_codepoint_t cp) {
  11342. /*
  11343. * Unicode letter is now taken to be the categories:
  11344. *
  11345. * Lu, Ll, Lt, Lm, Lo
  11346. *
  11347. * (Not sure if this is exactly correct.)
  11348. *
  11349. * The ASCII fast path consists of:
  11350. *
  11351. * 0x0041 ... 0x005a ['A' ... 'Z']
  11352. * 0x0061 ... 0x007a ['a' ... 'z']
  11353. */
  11354. /* ASCII (and EOF) fast path -- quick accept and reject */
  11355. if (cp <= 0x7fL) {
  11356. if ((cp >= 'a' && cp <= 'z') ||
  11357. (cp >= 'A' && cp <= 'Z')) {
  11358. return 1;
  11359. }
  11360. return 0;
  11361. }
  11362. /* Non-ASCII slow path (range-by-range linear comparison), very slow */
  11363. #if defined(DUK_USE_SOURCE_NONBMP)
  11364. if (duk__uni_range_match(duk_unicode_ids_noa,
  11365. sizeof(duk_unicode_ids_noa),
  11366. (duk_codepoint_t) cp) &&
  11367. !duk__uni_range_match(duk_unicode_ids_m_let_noa,
  11368. sizeof(duk_unicode_ids_m_let_noa),
  11369. (duk_codepoint_t) cp)) {
  11370. return 1;
  11371. }
  11372. return 0;
  11373. #else
  11374. if (cp < 0x10000L) {
  11375. if (duk__uni_range_match(duk_unicode_ids_noabmp,
  11376. sizeof(duk_unicode_ids_noabmp),
  11377. (duk_codepoint_t) cp) &&
  11378. !duk__uni_range_match(duk_unicode_ids_m_let_noabmp,
  11379. sizeof(duk_unicode_ids_m_let_noabmp),
  11380. (duk_codepoint_t) cp)) {
  11381. return 1;
  11382. }
  11383. return 0;
  11384. } else {
  11385. /* without explicit non-BMP support, assume non-BMP characters
  11386. * are always accepted as letters.
  11387. */
  11388. return 1;
  11389. }
  11390. #endif
  11391. }
  11392. /*
  11393. * Complex case conversion helper which decodes a bit-packed conversion
  11394. * control stream generated by tools/extract_caseconv.py. The conversion
  11395. * is very slow because it runs through the conversion data in a linear
  11396. * fashion to save space (which is why ASCII characters have a special
  11397. * fast path before arriving here).
  11398. *
  11399. * The particular bit counts etc have been determined experimentally to
  11400. * be small but still sufficient, and must match the Python script
  11401. * (tools/extract_caseconv.py).
  11402. *
  11403. * The return value is the case converted codepoint or -1 if the conversion
  11404. * results in multiple characters (this is useful for regexp Canonicalization
  11405. * operation). If 'buf' is not NULL, the result codepoint(s) are also
  11406. * appended to the hbuffer.
  11407. *
  11408. * Context and locale specific rules must be checked before consulting
  11409. * this function.
  11410. */
  11411. DUK_LOCAL
  11412. duk_codepoint_t duk__slow_case_conversion(duk_hthread *thr,
  11413. duk_bufwriter_ctx *bw,
  11414. duk_codepoint_t cp,
  11415. duk_bitdecoder_ctx *bd_ctx) {
  11416. duk_small_int_t skip = 0;
  11417. duk_small_int_t n;
  11418. duk_small_int_t t;
  11419. duk_small_int_t count;
  11420. duk_codepoint_t tmp_cp;
  11421. duk_codepoint_t start_i;
  11422. duk_codepoint_t start_o;
  11423. DUK_ASSERT(bd_ctx != NULL);
  11424. DUK_UNREF(thr);
  11425. DUK_DDD(DUK_DDDPRINT("slow case conversion for codepoint: %ld", (long) cp));
  11426. /* range conversion with a "skip" */
  11427. DUK_DDD(DUK_DDDPRINT("checking ranges"));
  11428. for (;;) {
  11429. skip++;
  11430. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 6);
  11431. if (n == 0x3f) {
  11432. /* end marker */
  11433. break;
  11434. }
  11435. DUK_DDD(DUK_DDDPRINT("skip=%ld, n=%ld", (long) skip, (long) n));
  11436. while (n--) {
  11437. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11438. start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11439. count = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  11440. DUK_DDD(DUK_DDDPRINT("range: start_i=%ld, start_o=%ld, count=%ld, skip=%ld",
  11441. (long) start_i, (long) start_o, (long) count, (long) skip));
  11442. if (cp >= start_i) {
  11443. tmp_cp = cp - start_i; /* always >= 0 */
  11444. if (tmp_cp < (duk_codepoint_t) count * (duk_codepoint_t) skip &&
  11445. (tmp_cp % (duk_codepoint_t) skip) == 0) {
  11446. DUK_DDD(DUK_DDDPRINT("range matches input codepoint"));
  11447. cp = start_o + tmp_cp;
  11448. goto single;
  11449. }
  11450. }
  11451. }
  11452. }
  11453. /* 1:1 conversion */
  11454. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  11455. DUK_DDD(DUK_DDDPRINT("checking 1:1 conversions (count %ld)", (long) n));
  11456. while (n--) {
  11457. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11458. start_o = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11459. DUK_DDD(DUK_DDDPRINT("1:1 conversion %ld -> %ld", (long) start_i, (long) start_o));
  11460. if (cp == start_i) {
  11461. DUK_DDD(DUK_DDDPRINT("1:1 matches input codepoint"));
  11462. cp = start_o;
  11463. goto single;
  11464. }
  11465. }
  11466. /* complex, multicharacter conversion */
  11467. n = (duk_small_int_t) duk_bd_decode(bd_ctx, 7);
  11468. DUK_DDD(DUK_DDDPRINT("checking 1:n conversions (count %ld)", (long) n));
  11469. while (n--) {
  11470. start_i = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11471. t = (duk_small_int_t) duk_bd_decode(bd_ctx, 2);
  11472. DUK_DDD(DUK_DDDPRINT("1:n conversion %ld -> %ld chars", (long) start_i, (long) t));
  11473. if (cp == start_i) {
  11474. DUK_DDD(DUK_DDDPRINT("1:n matches input codepoint"));
  11475. if (bw != NULL) {
  11476. while (t--) {
  11477. tmp_cp = (duk_codepoint_t) duk_bd_decode(bd_ctx, 16);
  11478. DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) tmp_cp);
  11479. }
  11480. }
  11481. return -1;
  11482. } else {
  11483. while (t--) {
  11484. (void) duk_bd_decode(bd_ctx, 16);
  11485. }
  11486. }
  11487. }
  11488. /* default: no change */
  11489. DUK_DDD(DUK_DDDPRINT("no rule matches, output is same as input"));
  11490. /* fall through */
  11491. single:
  11492. if (bw != NULL) {
  11493. DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp);
  11494. }
  11495. return cp;
  11496. }
  11497. /*
  11498. * Case conversion helper, with context/local sensitivity.
  11499. * For proper case conversion, one needs to know the character
  11500. * and the preceding and following characters, as well as
  11501. * locale/language.
  11502. */
  11503. /* XXX: add 'language' argument when locale/language sensitive rule
  11504. * support added.
  11505. */
  11506. DUK_LOCAL
  11507. duk_codepoint_t duk__case_transform_helper(duk_hthread *thr,
  11508. duk_bufwriter_ctx *bw,
  11509. duk_codepoint_t cp,
  11510. duk_codepoint_t prev,
  11511. duk_codepoint_t next,
  11512. duk_bool_t uppercase) {
  11513. duk_bitdecoder_ctx bd_ctx;
  11514. /* fast path for ASCII */
  11515. if (cp < 0x80L) {
  11516. /* XXX: there are language sensitive rules for the ASCII range.
  11517. * If/when language/locale support is implemented, they need to
  11518. * be implemented here for the fast path. There are no context
  11519. * sensitive rules for ASCII range.
  11520. */
  11521. if (uppercase) {
  11522. if (cp >= 'a' && cp <= 'z') {
  11523. cp = cp - 'a' + 'A';
  11524. }
  11525. } else {
  11526. if (cp >= 'A' && cp <= 'Z') {
  11527. cp = cp - 'A' + 'a';
  11528. }
  11529. }
  11530. if (bw != NULL) {
  11531. DUK_BW_WRITE_RAW_U8(thr, bw, (duk_uint8_t) cp);
  11532. }
  11533. return cp;
  11534. }
  11535. /* context and locale specific rules which cannot currently be represented
  11536. * in the caseconv bitstream: hardcoded rules in C
  11537. */
  11538. if (uppercase) {
  11539. /* XXX: turkish / azeri */
  11540. } else {
  11541. /*
  11542. * Final sigma context specific rule. This is a rather tricky
  11543. * rule and this handling is probably not 100% correct now.
  11544. * The rule is not locale/language specific so it is supported.
  11545. */
  11546. if (cp == 0x03a3L && /* U+03A3 = GREEK CAPITAL LETTER SIGMA */
  11547. duk_unicode_is_letter(prev) && /* prev exists and is not a letter */
  11548. !duk_unicode_is_letter(next)) { /* next does not exist or next is not a letter */
  11549. /* Capital sigma occurred at "end of word", lowercase to
  11550. * U+03C2 = GREEK SMALL LETTER FINAL SIGMA. Otherwise
  11551. * fall through and let the normal rules lowercase it to
  11552. * U+03C3 = GREEK SMALL LETTER SIGMA.
  11553. */
  11554. cp = 0x03c2L;
  11555. goto singlechar;
  11556. }
  11557. /* XXX: lithuanian not implemented */
  11558. /* XXX: lithuanian, explicit dot rules */
  11559. /* XXX: turkish / azeri, lowercase rules */
  11560. }
  11561. /* 1:1 or special conversions, but not locale/context specific: script generated rules */
  11562. duk_memzero(&bd_ctx, sizeof(bd_ctx));
  11563. if (uppercase) {
  11564. bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_uc;
  11565. bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_uc);
  11566. } else {
  11567. bd_ctx.data = (const duk_uint8_t *) duk_unicode_caseconv_lc;
  11568. bd_ctx.length = (duk_size_t) sizeof(duk_unicode_caseconv_lc);
  11569. }
  11570. return duk__slow_case_conversion(thr, bw, cp, &bd_ctx);
  11571. singlechar:
  11572. if (bw != NULL) {
  11573. DUK_BW_WRITE_RAW_XUTF8(thr, bw, (duk_ucodepoint_t) cp);
  11574. }
  11575. return cp;
  11576. /* unused now, not needed until Turkish/Azeri */
  11577. #if 0
  11578. nochar:
  11579. return -1;
  11580. #endif
  11581. }
  11582. /*
  11583. * Replace valstack top with case converted version.
  11584. */
  11585. DUK_INTERNAL void duk_unicode_case_convert_string(duk_hthread *thr, duk_bool_t uppercase) {
  11586. duk_hstring *h_input;
  11587. duk_bufwriter_ctx bw_alloc;
  11588. duk_bufwriter_ctx *bw;
  11589. const duk_uint8_t *p, *p_start, *p_end;
  11590. duk_codepoint_t prev, curr, next;
  11591. h_input = duk_require_hstring(thr, -1); /* Accept symbols. */
  11592. DUK_ASSERT(h_input != NULL);
  11593. bw = &bw_alloc;
  11594. DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input));
  11595. /* [ ... input buffer ] */
  11596. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  11597. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  11598. p = p_start;
  11599. prev = -1; DUK_UNREF(prev);
  11600. curr = -1;
  11601. next = -1;
  11602. for (;;) {
  11603. prev = curr;
  11604. curr = next;
  11605. next = -1;
  11606. if (p < p_end) {
  11607. next = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  11608. } else {
  11609. /* end of input and last char has been processed */
  11610. if (curr < 0) {
  11611. break;
  11612. }
  11613. }
  11614. /* on first round, skip */
  11615. if (curr >= 0) {
  11616. /* XXX: could add a fast path to process chunks of input codepoints,
  11617. * but relative benefit would be quite small.
  11618. */
  11619. /* Ensure space for maximum multi-character result; estimate is overkill. */
  11620. DUK_BW_ENSURE(thr, bw, 8 * DUK_UNICODE_MAX_XUTF8_LENGTH);
  11621. duk__case_transform_helper(thr,
  11622. bw,
  11623. (duk_codepoint_t) curr,
  11624. prev,
  11625. next,
  11626. uppercase);
  11627. }
  11628. }
  11629. DUK_BW_COMPACT(thr, bw);
  11630. (void) duk_buffer_to_string(thr, -1); /* Safe, output is encoded. */
  11631. /* invalidates h_buf pointer */
  11632. duk_remove_m2(thr);
  11633. }
  11634. #if defined(DUK_USE_REGEXP_SUPPORT)
  11635. /*
  11636. * Canonicalize() abstract operation needed for canonicalization of individual
  11637. * codepoints during regexp compilation and execution, see E5 Section 15.10.2.8.
  11638. * Note that codepoints are canonicalized one character at a time, so no context
  11639. * specific rules can apply. Locale specific rules can apply, though.
  11640. */
  11641. DUK_INTERNAL duk_codepoint_t duk_unicode_re_canonicalize_char(duk_hthread *thr, duk_codepoint_t cp) {
  11642. #if defined(DUK_USE_REGEXP_CANON_WORKAROUND)
  11643. /* Fast canonicalization lookup at the cost of 128kB footprint. */
  11644. DUK_ASSERT(cp >= 0);
  11645. DUK_UNREF(thr);
  11646. if (DUK_LIKELY(cp < 0x10000L)) {
  11647. return (duk_codepoint_t) duk_unicode_re_canon_lookup[cp];
  11648. }
  11649. return cp;
  11650. #else /* DUK_USE_REGEXP_CANON_WORKAROUND */
  11651. duk_codepoint_t y;
  11652. y = duk__case_transform_helper(thr,
  11653. NULL, /* NULL is allowed, no output */
  11654. cp, /* curr char */
  11655. -1, /* prev char */
  11656. -1, /* next char */
  11657. 1); /* uppercase */
  11658. if ((y < 0) || (cp >= 0x80 && y < 0x80)) {
  11659. /* multiple codepoint conversion or non-ASCII mapped to ASCII
  11660. * --> leave as is.
  11661. */
  11662. return cp;
  11663. }
  11664. return y;
  11665. #endif /* DUK_USE_REGEXP_CANON_WORKAROUND */
  11666. }
  11667. /*
  11668. * E5 Section 15.10.2.6 "IsWordChar" abstract operation. Assume
  11669. * x < 0 for characters read outside the string.
  11670. */
  11671. DUK_INTERNAL duk_small_int_t duk_unicode_re_is_wordchar(duk_codepoint_t x) {
  11672. /*
  11673. * Note: the description in E5 Section 15.10.2.6 has a typo, it
  11674. * contains 'A' twice and lacks 'a'; the intent is [0-9a-zA-Z_].
  11675. */
  11676. if ((x >= '0' && x <= '9') ||
  11677. (x >= 'a' && x <= 'z') ||
  11678. (x >= 'A' && x <= 'Z') ||
  11679. (x == '_')) {
  11680. return 1;
  11681. }
  11682. return 0;
  11683. }
  11684. /*
  11685. * Regexp range tables
  11686. */
  11687. /* exposed because lexer needs these too */
  11688. DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_digit[2] = {
  11689. (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL,
  11690. };
  11691. DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_white[22] = {
  11692. (duk_uint16_t) 0x0009UL, (duk_uint16_t) 0x000DUL,
  11693. (duk_uint16_t) 0x0020UL, (duk_uint16_t) 0x0020UL,
  11694. (duk_uint16_t) 0x00A0UL, (duk_uint16_t) 0x00A0UL,
  11695. (duk_uint16_t) 0x1680UL, (duk_uint16_t) 0x1680UL,
  11696. (duk_uint16_t) 0x180EUL, (duk_uint16_t) 0x180EUL,
  11697. (duk_uint16_t) 0x2000UL, (duk_uint16_t) 0x200AUL,
  11698. (duk_uint16_t) 0x2028UL, (duk_uint16_t) 0x2029UL,
  11699. (duk_uint16_t) 0x202FUL, (duk_uint16_t) 0x202FUL,
  11700. (duk_uint16_t) 0x205FUL, (duk_uint16_t) 0x205FUL,
  11701. (duk_uint16_t) 0x3000UL, (duk_uint16_t) 0x3000UL,
  11702. (duk_uint16_t) 0xFEFFUL, (duk_uint16_t) 0xFEFFUL,
  11703. };
  11704. DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_wordchar[8] = {
  11705. (duk_uint16_t) 0x0030UL, (duk_uint16_t) 0x0039UL,
  11706. (duk_uint16_t) 0x0041UL, (duk_uint16_t) 0x005AUL,
  11707. (duk_uint16_t) 0x005FUL, (duk_uint16_t) 0x005FUL,
  11708. (duk_uint16_t) 0x0061UL, (duk_uint16_t) 0x007AUL,
  11709. };
  11710. DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_digit[4] = {
  11711. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL,
  11712. (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0xFFFFUL,
  11713. };
  11714. DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_white[24] = {
  11715. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x0008UL,
  11716. (duk_uint16_t) 0x000EUL, (duk_uint16_t) 0x001FUL,
  11717. (duk_uint16_t) 0x0021UL, (duk_uint16_t) 0x009FUL,
  11718. (duk_uint16_t) 0x00A1UL, (duk_uint16_t) 0x167FUL,
  11719. (duk_uint16_t) 0x1681UL, (duk_uint16_t) 0x180DUL,
  11720. (duk_uint16_t) 0x180FUL, (duk_uint16_t) 0x1FFFUL,
  11721. (duk_uint16_t) 0x200BUL, (duk_uint16_t) 0x2027UL,
  11722. (duk_uint16_t) 0x202AUL, (duk_uint16_t) 0x202EUL,
  11723. (duk_uint16_t) 0x2030UL, (duk_uint16_t) 0x205EUL,
  11724. (duk_uint16_t) 0x2060UL, (duk_uint16_t) 0x2FFFUL,
  11725. (duk_uint16_t) 0x3001UL, (duk_uint16_t) 0xFEFEUL,
  11726. (duk_uint16_t) 0xFF00UL, (duk_uint16_t) 0xFFFFUL,
  11727. };
  11728. DUK_INTERNAL const duk_uint16_t duk_unicode_re_ranges_not_wordchar[10] = {
  11729. (duk_uint16_t) 0x0000UL, (duk_uint16_t) 0x002FUL,
  11730. (duk_uint16_t) 0x003AUL, (duk_uint16_t) 0x0040UL,
  11731. (duk_uint16_t) 0x005BUL, (duk_uint16_t) 0x005EUL,
  11732. (duk_uint16_t) 0x0060UL, (duk_uint16_t) 0x0060UL,
  11733. (duk_uint16_t) 0x007BUL, (duk_uint16_t) 0xFFFFUL,
  11734. };
  11735. #endif /* DUK_USE_REGEXP_SUPPORT */
  11736. #line 1 "duk_util_misc.c"
  11737. /*
  11738. * Misc util stuff.
  11739. */
  11740. /* #include duk_internal.h -> already included */
  11741. /*
  11742. * Lowercase digits for radix values 2 to 36. Also doubles as lowercase
  11743. * hex nybble table.
  11744. */
  11745. DUK_INTERNAL const duk_uint8_t duk_lc_digits[36] = {
  11746. DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3,
  11747. DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7,
  11748. DUK_ASC_8, DUK_ASC_9, DUK_ASC_LC_A, DUK_ASC_LC_B,
  11749. DUK_ASC_LC_C, DUK_ASC_LC_D, DUK_ASC_LC_E, DUK_ASC_LC_F,
  11750. DUK_ASC_LC_G, DUK_ASC_LC_H, DUK_ASC_LC_I, DUK_ASC_LC_J,
  11751. DUK_ASC_LC_K, DUK_ASC_LC_L, DUK_ASC_LC_M, DUK_ASC_LC_N,
  11752. DUK_ASC_LC_O, DUK_ASC_LC_P, DUK_ASC_LC_Q, DUK_ASC_LC_R,
  11753. DUK_ASC_LC_S, DUK_ASC_LC_T, DUK_ASC_LC_U, DUK_ASC_LC_V,
  11754. DUK_ASC_LC_W, DUK_ASC_LC_X, DUK_ASC_LC_Y, DUK_ASC_LC_Z
  11755. };
  11756. DUK_INTERNAL const duk_uint8_t duk_uc_nybbles[16] = {
  11757. DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3,
  11758. DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7,
  11759. DUK_ASC_8, DUK_ASC_9, DUK_ASC_UC_A, DUK_ASC_UC_B,
  11760. DUK_ASC_UC_C, DUK_ASC_UC_D, DUK_ASC_UC_E, DUK_ASC_UC_F
  11761. };
  11762. /*
  11763. * Table for hex decoding ASCII hex digits
  11764. */
  11765. DUK_INTERNAL const duk_int8_t duk_hex_dectab[256] = {
  11766. /* -1 if invalid */
  11767. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */
  11768. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */
  11769. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */
  11770. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */
  11771. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */
  11772. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */
  11773. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */
  11774. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */
  11775. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */
  11776. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */
  11777. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */
  11778. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */
  11779. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */
  11780. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */
  11781. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */
  11782. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */
  11783. };
  11784. #if defined(DUK_USE_HEX_FASTPATH)
  11785. /* Preshifted << 4. Must use 16-bit entry to allow negative value signaling. */
  11786. DUK_INTERNAL const duk_int16_t duk_hex_dectab_shift4[256] = {
  11787. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0f */
  11788. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1f */
  11789. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20-0x2f */
  11790. 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, -1, -1, -1, -1, -1, -1, /* 0x30-0x3f */
  11791. -1, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40-0x4f */
  11792. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50-0x5f */
  11793. -1, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60-0x6f */
  11794. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70-0x7f */
  11795. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80-0x8f */
  11796. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90-0x9f */
  11797. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xa0-0xaf */
  11798. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xb0-0xbf */
  11799. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xc0-0xcf */
  11800. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xd0-0xdf */
  11801. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xe0-0xef */
  11802. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 /* 0xf0-0xff */
  11803. };
  11804. #endif
  11805. /*
  11806. * Table for hex encoding bytes
  11807. */
  11808. #if defined(DUK_USE_HEX_FASTPATH)
  11809. /* Lookup to encode one byte directly into 2 characters:
  11810. *
  11811. * def genhextab(bswap):
  11812. * for i in xrange(256):
  11813. * t = chr(i).encode('hex')
  11814. * if bswap:
  11815. * t = t[1] + t[0]
  11816. * print('0x' + t.encode('hex') + 'U')
  11817. * print('big endian'); genhextab(False)
  11818. * print('little endian'); genhextab(True)
  11819. */
  11820. DUK_INTERNAL const duk_uint16_t duk_hex_enctab[256] = {
  11821. #if defined(DUK_USE_INTEGER_BE)
  11822. 0x3030U, 0x3031U, 0x3032U, 0x3033U, 0x3034U, 0x3035U, 0x3036U, 0x3037U,
  11823. 0x3038U, 0x3039U, 0x3061U, 0x3062U, 0x3063U, 0x3064U, 0x3065U, 0x3066U,
  11824. 0x3130U, 0x3131U, 0x3132U, 0x3133U, 0x3134U, 0x3135U, 0x3136U, 0x3137U,
  11825. 0x3138U, 0x3139U, 0x3161U, 0x3162U, 0x3163U, 0x3164U, 0x3165U, 0x3166U,
  11826. 0x3230U, 0x3231U, 0x3232U, 0x3233U, 0x3234U, 0x3235U, 0x3236U, 0x3237U,
  11827. 0x3238U, 0x3239U, 0x3261U, 0x3262U, 0x3263U, 0x3264U, 0x3265U, 0x3266U,
  11828. 0x3330U, 0x3331U, 0x3332U, 0x3333U, 0x3334U, 0x3335U, 0x3336U, 0x3337U,
  11829. 0x3338U, 0x3339U, 0x3361U, 0x3362U, 0x3363U, 0x3364U, 0x3365U, 0x3366U,
  11830. 0x3430U, 0x3431U, 0x3432U, 0x3433U, 0x3434U, 0x3435U, 0x3436U, 0x3437U,
  11831. 0x3438U, 0x3439U, 0x3461U, 0x3462U, 0x3463U, 0x3464U, 0x3465U, 0x3466U,
  11832. 0x3530U, 0x3531U, 0x3532U, 0x3533U, 0x3534U, 0x3535U, 0x3536U, 0x3537U,
  11833. 0x3538U, 0x3539U, 0x3561U, 0x3562U, 0x3563U, 0x3564U, 0x3565U, 0x3566U,
  11834. 0x3630U, 0x3631U, 0x3632U, 0x3633U, 0x3634U, 0x3635U, 0x3636U, 0x3637U,
  11835. 0x3638U, 0x3639U, 0x3661U, 0x3662U, 0x3663U, 0x3664U, 0x3665U, 0x3666U,
  11836. 0x3730U, 0x3731U, 0x3732U, 0x3733U, 0x3734U, 0x3735U, 0x3736U, 0x3737U,
  11837. 0x3738U, 0x3739U, 0x3761U, 0x3762U, 0x3763U, 0x3764U, 0x3765U, 0x3766U,
  11838. 0x3830U, 0x3831U, 0x3832U, 0x3833U, 0x3834U, 0x3835U, 0x3836U, 0x3837U,
  11839. 0x3838U, 0x3839U, 0x3861U, 0x3862U, 0x3863U, 0x3864U, 0x3865U, 0x3866U,
  11840. 0x3930U, 0x3931U, 0x3932U, 0x3933U, 0x3934U, 0x3935U, 0x3936U, 0x3937U,
  11841. 0x3938U, 0x3939U, 0x3961U, 0x3962U, 0x3963U, 0x3964U, 0x3965U, 0x3966U,
  11842. 0x6130U, 0x6131U, 0x6132U, 0x6133U, 0x6134U, 0x6135U, 0x6136U, 0x6137U,
  11843. 0x6138U, 0x6139U, 0x6161U, 0x6162U, 0x6163U, 0x6164U, 0x6165U, 0x6166U,
  11844. 0x6230U, 0x6231U, 0x6232U, 0x6233U, 0x6234U, 0x6235U, 0x6236U, 0x6237U,
  11845. 0x6238U, 0x6239U, 0x6261U, 0x6262U, 0x6263U, 0x6264U, 0x6265U, 0x6266U,
  11846. 0x6330U, 0x6331U, 0x6332U, 0x6333U, 0x6334U, 0x6335U, 0x6336U, 0x6337U,
  11847. 0x6338U, 0x6339U, 0x6361U, 0x6362U, 0x6363U, 0x6364U, 0x6365U, 0x6366U,
  11848. 0x6430U, 0x6431U, 0x6432U, 0x6433U, 0x6434U, 0x6435U, 0x6436U, 0x6437U,
  11849. 0x6438U, 0x6439U, 0x6461U, 0x6462U, 0x6463U, 0x6464U, 0x6465U, 0x6466U,
  11850. 0x6530U, 0x6531U, 0x6532U, 0x6533U, 0x6534U, 0x6535U, 0x6536U, 0x6537U,
  11851. 0x6538U, 0x6539U, 0x6561U, 0x6562U, 0x6563U, 0x6564U, 0x6565U, 0x6566U,
  11852. 0x6630U, 0x6631U, 0x6632U, 0x6633U, 0x6634U, 0x6635U, 0x6636U, 0x6637U,
  11853. 0x6638U, 0x6639U, 0x6661U, 0x6662U, 0x6663U, 0x6664U, 0x6665U, 0x6666U
  11854. #else /* DUK_USE_INTEGER_BE */
  11855. 0x3030U, 0x3130U, 0x3230U, 0x3330U, 0x3430U, 0x3530U, 0x3630U, 0x3730U,
  11856. 0x3830U, 0x3930U, 0x6130U, 0x6230U, 0x6330U, 0x6430U, 0x6530U, 0x6630U,
  11857. 0x3031U, 0x3131U, 0x3231U, 0x3331U, 0x3431U, 0x3531U, 0x3631U, 0x3731U,
  11858. 0x3831U, 0x3931U, 0x6131U, 0x6231U, 0x6331U, 0x6431U, 0x6531U, 0x6631U,
  11859. 0x3032U, 0x3132U, 0x3232U, 0x3332U, 0x3432U, 0x3532U, 0x3632U, 0x3732U,
  11860. 0x3832U, 0x3932U, 0x6132U, 0x6232U, 0x6332U, 0x6432U, 0x6532U, 0x6632U,
  11861. 0x3033U, 0x3133U, 0x3233U, 0x3333U, 0x3433U, 0x3533U, 0x3633U, 0x3733U,
  11862. 0x3833U, 0x3933U, 0x6133U, 0x6233U, 0x6333U, 0x6433U, 0x6533U, 0x6633U,
  11863. 0x3034U, 0x3134U, 0x3234U, 0x3334U, 0x3434U, 0x3534U, 0x3634U, 0x3734U,
  11864. 0x3834U, 0x3934U, 0x6134U, 0x6234U, 0x6334U, 0x6434U, 0x6534U, 0x6634U,
  11865. 0x3035U, 0x3135U, 0x3235U, 0x3335U, 0x3435U, 0x3535U, 0x3635U, 0x3735U,
  11866. 0x3835U, 0x3935U, 0x6135U, 0x6235U, 0x6335U, 0x6435U, 0x6535U, 0x6635U,
  11867. 0x3036U, 0x3136U, 0x3236U, 0x3336U, 0x3436U, 0x3536U, 0x3636U, 0x3736U,
  11868. 0x3836U, 0x3936U, 0x6136U, 0x6236U, 0x6336U, 0x6436U, 0x6536U, 0x6636U,
  11869. 0x3037U, 0x3137U, 0x3237U, 0x3337U, 0x3437U, 0x3537U, 0x3637U, 0x3737U,
  11870. 0x3837U, 0x3937U, 0x6137U, 0x6237U, 0x6337U, 0x6437U, 0x6537U, 0x6637U,
  11871. 0x3038U, 0x3138U, 0x3238U, 0x3338U, 0x3438U, 0x3538U, 0x3638U, 0x3738U,
  11872. 0x3838U, 0x3938U, 0x6138U, 0x6238U, 0x6338U, 0x6438U, 0x6538U, 0x6638U,
  11873. 0x3039U, 0x3139U, 0x3239U, 0x3339U, 0x3439U, 0x3539U, 0x3639U, 0x3739U,
  11874. 0x3839U, 0x3939U, 0x6139U, 0x6239U, 0x6339U, 0x6439U, 0x6539U, 0x6639U,
  11875. 0x3061U, 0x3161U, 0x3261U, 0x3361U, 0x3461U, 0x3561U, 0x3661U, 0x3761U,
  11876. 0x3861U, 0x3961U, 0x6161U, 0x6261U, 0x6361U, 0x6461U, 0x6561U, 0x6661U,
  11877. 0x3062U, 0x3162U, 0x3262U, 0x3362U, 0x3462U, 0x3562U, 0x3662U, 0x3762U,
  11878. 0x3862U, 0x3962U, 0x6162U, 0x6262U, 0x6362U, 0x6462U, 0x6562U, 0x6662U,
  11879. 0x3063U, 0x3163U, 0x3263U, 0x3363U, 0x3463U, 0x3563U, 0x3663U, 0x3763U,
  11880. 0x3863U, 0x3963U, 0x6163U, 0x6263U, 0x6363U, 0x6463U, 0x6563U, 0x6663U,
  11881. 0x3064U, 0x3164U, 0x3264U, 0x3364U, 0x3464U, 0x3564U, 0x3664U, 0x3764U,
  11882. 0x3864U, 0x3964U, 0x6164U, 0x6264U, 0x6364U, 0x6464U, 0x6564U, 0x6664U,
  11883. 0x3065U, 0x3165U, 0x3265U, 0x3365U, 0x3465U, 0x3565U, 0x3665U, 0x3765U,
  11884. 0x3865U, 0x3965U, 0x6165U, 0x6265U, 0x6365U, 0x6465U, 0x6565U, 0x6665U,
  11885. 0x3066U, 0x3166U, 0x3266U, 0x3366U, 0x3466U, 0x3566U, 0x3666U, 0x3766U,
  11886. 0x3866U, 0x3966U, 0x6166U, 0x6266U, 0x6366U, 0x6466U, 0x6566U, 0x6666U
  11887. #endif /* DUK_USE_INTEGER_BE */
  11888. };
  11889. #endif /* DUK_USE_HEX_FASTPATH */
  11890. /*
  11891. * Arbitrary byteswap for potentially unaligned values
  11892. *
  11893. * Used to byteswap pointers e.g. in debugger code.
  11894. */
  11895. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* For now only needed by the debugger. */
  11896. DUK_INTERNAL void duk_byteswap_bytes(duk_uint8_t *p, duk_small_uint_t len) {
  11897. duk_uint8_t tmp;
  11898. duk_uint8_t *q = p + len - 1;
  11899. while (p - q < 0) {
  11900. tmp = *p;
  11901. *p = *q;
  11902. *q = tmp;
  11903. p++;
  11904. q--;
  11905. }
  11906. }
  11907. #endif
  11908. #line 1 "duk_hobject_class.c"
  11909. /*
  11910. * Hobject ECMAScript [[Class]].
  11911. */
  11912. /* #include duk_internal.h -> already included */
  11913. #if (DUK_STRIDX_UC_ARGUMENTS > 255)
  11914. #error constant too large
  11915. #endif
  11916. #if (DUK_STRIDX_ARRAY > 255)
  11917. #error constant too large
  11918. #endif
  11919. #if (DUK_STRIDX_UC_BOOLEAN > 255)
  11920. #error constant too large
  11921. #endif
  11922. #if (DUK_STRIDX_DATE > 255)
  11923. #error constant too large
  11924. #endif
  11925. #if (DUK_STRIDX_UC_ERROR > 255)
  11926. #error constant too large
  11927. #endif
  11928. #if (DUK_STRIDX_UC_FUNCTION > 255)
  11929. #error constant too large
  11930. #endif
  11931. #if (DUK_STRIDX_JSON > 255)
  11932. #error constant too large
  11933. #endif
  11934. #if (DUK_STRIDX_MATH > 255)
  11935. #error constant too large
  11936. #endif
  11937. #if (DUK_STRIDX_UC_NUMBER > 255)
  11938. #error constant too large
  11939. #endif
  11940. #if (DUK_STRIDX_UC_OBJECT > 255)
  11941. #error constant too large
  11942. #endif
  11943. #if (DUK_STRIDX_REG_EXP > 255)
  11944. #error constant too large
  11945. #endif
  11946. #if (DUK_STRIDX_UC_STRING > 255)
  11947. #error constant too large
  11948. #endif
  11949. #if (DUK_STRIDX_GLOBAL > 255)
  11950. #error constant too large
  11951. #endif
  11952. #if (DUK_STRIDX_OBJ_ENV > 255)
  11953. #error constant too large
  11954. #endif
  11955. #if (DUK_STRIDX_DEC_ENV > 255)
  11956. #error constant too large
  11957. #endif
  11958. #if (DUK_STRIDX_UC_POINTER > 255)
  11959. #error constant too large
  11960. #endif
  11961. #if (DUK_STRIDX_UC_THREAD > 255)
  11962. #error constant too large
  11963. #endif
  11964. #if (DUK_STRIDX_ARRAY_BUFFER > 255)
  11965. #error constant too large
  11966. #endif
  11967. #if (DUK_STRIDX_DATA_VIEW > 255)
  11968. #error constant too large
  11969. #endif
  11970. #if (DUK_STRIDX_INT8_ARRAY > 255)
  11971. #error constant too large
  11972. #endif
  11973. #if (DUK_STRIDX_UINT8_ARRAY > 255)
  11974. #error constant too large
  11975. #endif
  11976. #if (DUK_STRIDX_UINT8_CLAMPED_ARRAY > 255)
  11977. #error constant too large
  11978. #endif
  11979. #if (DUK_STRIDX_INT16_ARRAY > 255)
  11980. #error constant too large
  11981. #endif
  11982. #if (DUK_STRIDX_UINT16_ARRAY > 255)
  11983. #error constant too large
  11984. #endif
  11985. #if (DUK_STRIDX_INT32_ARRAY > 255)
  11986. #error constant too large
  11987. #endif
  11988. #if (DUK_STRIDX_UINT32_ARRAY > 255)
  11989. #error constant too large
  11990. #endif
  11991. #if (DUK_STRIDX_FLOAT32_ARRAY > 255)
  11992. #error constant too large
  11993. #endif
  11994. #if (DUK_STRIDX_FLOAT64_ARRAY > 255)
  11995. #error constant too large
  11996. #endif
  11997. #if (DUK_STRIDX_EMPTY_STRING > 255)
  11998. #error constant too large
  11999. #endif
  12000. /* Note: assumes that these string indexes are 8-bit, genstrings.py must ensure that */
  12001. DUK_INTERNAL duk_uint8_t duk_class_number_to_stridx[32] = {
  12002. DUK_STRIDX_EMPTY_STRING, /* NONE, intentionally empty */
  12003. DUK_STRIDX_UC_OBJECT,
  12004. DUK_STRIDX_ARRAY,
  12005. DUK_STRIDX_UC_FUNCTION,
  12006. DUK_STRIDX_UC_ARGUMENTS,
  12007. DUK_STRIDX_UC_BOOLEAN,
  12008. DUK_STRIDX_DATE,
  12009. DUK_STRIDX_UC_ERROR,
  12010. DUK_STRIDX_JSON,
  12011. DUK_STRIDX_MATH,
  12012. DUK_STRIDX_UC_NUMBER,
  12013. DUK_STRIDX_REG_EXP,
  12014. DUK_STRIDX_UC_STRING,
  12015. DUK_STRIDX_GLOBAL,
  12016. DUK_STRIDX_UC_SYMBOL,
  12017. DUK_STRIDX_OBJ_ENV,
  12018. DUK_STRIDX_DEC_ENV,
  12019. DUK_STRIDX_UC_POINTER,
  12020. DUK_STRIDX_UC_THREAD,
  12021. DUK_STRIDX_ARRAY_BUFFER,
  12022. DUK_STRIDX_DATA_VIEW,
  12023. DUK_STRIDX_INT8_ARRAY,
  12024. DUK_STRIDX_UINT8_ARRAY,
  12025. DUK_STRIDX_UINT8_CLAMPED_ARRAY,
  12026. DUK_STRIDX_INT16_ARRAY,
  12027. DUK_STRIDX_UINT16_ARRAY,
  12028. DUK_STRIDX_INT32_ARRAY,
  12029. DUK_STRIDX_UINT32_ARRAY,
  12030. DUK_STRIDX_FLOAT32_ARRAY,
  12031. DUK_STRIDX_FLOAT64_ARRAY,
  12032. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  12033. DUK_STRIDX_EMPTY_STRING, /* UNUSED, intentionally empty */
  12034. };
  12035. #line 1 "duk_alloc_default.c"
  12036. /*
  12037. * Default allocation functions.
  12038. *
  12039. * Assumes behavior such as malloc allowing zero size, yielding
  12040. * a NULL or a unique pointer which is a no-op for free.
  12041. */
  12042. /* #include duk_internal.h -> already included */
  12043. #if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
  12044. DUK_INTERNAL void *duk_default_alloc_function(void *udata, duk_size_t size) {
  12045. void *res;
  12046. DUK_UNREF(udata);
  12047. res = DUK_ANSI_MALLOC(size);
  12048. DUK_DDD(DUK_DDDPRINT("default alloc function: %lu -> %p",
  12049. (unsigned long) size, (void *) res));
  12050. return res;
  12051. }
  12052. DUK_INTERNAL void *duk_default_realloc_function(void *udata, void *ptr, duk_size_t newsize) {
  12053. void *res;
  12054. DUK_UNREF(udata);
  12055. res = DUK_ANSI_REALLOC(ptr, newsize);
  12056. DUK_DDD(DUK_DDDPRINT("default realloc function: %p %lu -> %p",
  12057. (void *) ptr, (unsigned long) newsize, (void *) res));
  12058. return res;
  12059. }
  12060. DUK_INTERNAL void duk_default_free_function(void *udata, void *ptr) {
  12061. DUK_DDD(DUK_DDDPRINT("default free function: %p", (void *) ptr));
  12062. DUK_UNREF(udata);
  12063. DUK_ANSI_FREE(ptr);
  12064. }
  12065. #endif /* DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS */
  12066. #line 1 "duk_api_buffer.c"
  12067. /*
  12068. * Buffer
  12069. */
  12070. /* #include duk_internal.h -> already included */
  12071. DUK_EXTERNAL void *duk_resize_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t new_size) {
  12072. duk_hbuffer_dynamic *h;
  12073. DUK_ASSERT_API_ENTRY(thr);
  12074. h = (duk_hbuffer_dynamic *) duk_require_hbuffer(thr, idx);
  12075. DUK_ASSERT(h != NULL);
  12076. if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) {
  12077. DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE);
  12078. DUK_WO_NORETURN(return NULL;);
  12079. }
  12080. /* Maximum size check is handled by callee. */
  12081. duk_hbuffer_resize(thr, h, new_size);
  12082. return DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h);
  12083. }
  12084. DUK_EXTERNAL void *duk_steal_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
  12085. duk_hbuffer_dynamic *h;
  12086. void *ptr;
  12087. duk_size_t sz;
  12088. DUK_ASSERT_API_ENTRY(thr);
  12089. h = (duk_hbuffer_dynamic *) duk_require_hbuffer(thr, idx);
  12090. DUK_ASSERT(h != NULL);
  12091. if (!(DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h))) {
  12092. DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE);
  12093. DUK_WO_NORETURN(return NULL;);
  12094. }
  12095. /* Forget the previous allocation, setting size to 0 and alloc to
  12096. * NULL. Caller is responsible for freeing the previous allocation.
  12097. * Getting the allocation and clearing it is done in the same API
  12098. * call to avoid any chance of a realloc.
  12099. */
  12100. ptr = DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h);
  12101. sz = DUK_HBUFFER_DYNAMIC_GET_SIZE(h);
  12102. if (out_size) {
  12103. *out_size = sz;
  12104. }
  12105. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR_NULL(thr->heap, h);
  12106. DUK_HBUFFER_DYNAMIC_SET_SIZE(h, 0);
  12107. return ptr;
  12108. }
  12109. DUK_EXTERNAL void duk_config_buffer(duk_hthread *thr, duk_idx_t idx, void *ptr, duk_size_t len) {
  12110. duk_hbuffer_external *h;
  12111. DUK_ASSERT_API_ENTRY(thr);
  12112. h = (duk_hbuffer_external *) duk_require_hbuffer(thr, idx);
  12113. DUK_ASSERT(h != NULL);
  12114. if (!DUK_HBUFFER_HAS_EXTERNAL(h)) {
  12115. DUK_ERROR_TYPE(thr, DUK_STR_WRONG_BUFFER_TYPE);
  12116. DUK_WO_NORETURN(return;);
  12117. }
  12118. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h));
  12119. DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(thr->heap, h, ptr);
  12120. DUK_HBUFFER_EXTERNAL_SET_SIZE(h, len);
  12121. }
  12122. #line 1 "duk_api_bytecode.c"
  12123. /*
  12124. * Bytecode dump/load
  12125. *
  12126. * The bytecode load primitive is more important performance-wise than the
  12127. * dump primitive.
  12128. *
  12129. * Unlike most Duktape API calls, bytecode dump/load is not guaranteed to be
  12130. * memory safe for invalid arguments - caller beware! There's little point
  12131. * in trying to achieve memory safety unless bytecode instructions are also
  12132. * validated which is not easy to do with indirect register references etc.
  12133. */
  12134. /* #include duk_internal.h -> already included */
  12135. #if defined(DUK_USE_BYTECODE_DUMP_SUPPORT)
  12136. #define DUK__SER_MARKER 0xbf
  12137. #define DUK__SER_STRING 0x00
  12138. #define DUK__SER_NUMBER 0x01
  12139. #define DUK__BYTECODE_INITIAL_ALLOC 256
  12140. #define DUK__NO_FORMALS 0xffffffffUL
  12141. /*
  12142. * Dump/load helpers, xxx_raw() helpers do no buffer checks
  12143. */
  12144. DUK_LOCAL duk_uint8_t *duk__load_string_raw(duk_hthread *thr, duk_uint8_t *p) {
  12145. duk_uint32_t len;
  12146. len = DUK_RAW_READ_U32_BE(p);
  12147. duk_push_lstring(thr, (const char *) p, len);
  12148. p += len;
  12149. return p;
  12150. }
  12151. DUK_LOCAL duk_uint8_t *duk__load_buffer_raw(duk_hthread *thr, duk_uint8_t *p) {
  12152. duk_uint32_t len;
  12153. duk_uint8_t *buf;
  12154. len = DUK_RAW_READ_U32_BE(p);
  12155. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, (duk_size_t) len);
  12156. DUK_ASSERT(buf != NULL);
  12157. duk_memcpy((void *) buf, (const void *) p, (size_t) len);
  12158. p += len;
  12159. return p;
  12160. }
  12161. DUK_LOCAL duk_uint8_t *duk__dump_hstring_raw(duk_uint8_t *p, duk_hstring *h) {
  12162. duk_size_t len;
  12163. duk_uint32_t tmp32;
  12164. DUK_ASSERT(h != NULL);
  12165. len = DUK_HSTRING_GET_BYTELEN(h);
  12166. DUK_ASSERT(len <= 0xffffffffUL); /* string limits */
  12167. tmp32 = (duk_uint32_t) len;
  12168. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12169. duk_memcpy((void *) p,
  12170. (const void *) DUK_HSTRING_GET_DATA(h),
  12171. len);
  12172. p += len;
  12173. return p;
  12174. }
  12175. DUK_LOCAL duk_uint8_t *duk__dump_hbuffer_raw(duk_hthread *thr, duk_uint8_t *p, duk_hbuffer *h) {
  12176. duk_size_t len;
  12177. duk_uint32_t tmp32;
  12178. DUK_ASSERT(thr != NULL);
  12179. DUK_ASSERT(h != NULL);
  12180. DUK_UNREF(thr);
  12181. len = DUK_HBUFFER_GET_SIZE(h);
  12182. DUK_ASSERT(len <= 0xffffffffUL); /* buffer limits */
  12183. tmp32 = (duk_uint32_t) len;
  12184. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12185. /* When len == 0, buffer data pointer may be NULL. */
  12186. duk_memcpy_unsafe((void *) p,
  12187. (const void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h),
  12188. len);
  12189. p += len;
  12190. return p;
  12191. }
  12192. DUK_LOCAL duk_uint8_t *duk__dump_string_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx) {
  12193. duk_hstring *h_str;
  12194. duk_tval *tv;
  12195. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx));
  12196. if (tv != NULL && DUK_TVAL_IS_STRING(tv)) {
  12197. h_str = DUK_TVAL_GET_STRING(tv);
  12198. DUK_ASSERT(h_str != NULL);
  12199. } else {
  12200. h_str = DUK_HTHREAD_STRING_EMPTY_STRING(thr);
  12201. DUK_ASSERT(h_str != NULL);
  12202. }
  12203. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  12204. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U + DUK_HSTRING_GET_BYTELEN(h_str), p);
  12205. p = duk__dump_hstring_raw(p, h_str);
  12206. return p;
  12207. }
  12208. DUK_LOCAL duk_uint8_t *duk__dump_buffer_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx) {
  12209. duk_tval *tv;
  12210. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx));
  12211. if (tv != NULL && DUK_TVAL_IS_BUFFER(tv)) {
  12212. duk_hbuffer *h_buf;
  12213. h_buf = DUK_TVAL_GET_BUFFER(tv);
  12214. DUK_ASSERT(h_buf != NULL);
  12215. DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  12216. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U + DUK_HBUFFER_GET_SIZE(h_buf), p);
  12217. p = duk__dump_hbuffer_raw(thr, p, h_buf);
  12218. } else {
  12219. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U, p);
  12220. DUK_RAW_WRITE_U32_BE(p, 0);
  12221. }
  12222. return p;
  12223. }
  12224. DUK_LOCAL duk_uint8_t *duk__dump_uint32_prop(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func, duk_small_uint_t stridx, duk_uint32_t def_value) {
  12225. duk_tval *tv;
  12226. duk_uint32_t val;
  12227. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_GET_STRING(thr, stridx));
  12228. if (tv != NULL && DUK_TVAL_IS_NUMBER(tv)) {
  12229. val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv);
  12230. } else {
  12231. val = def_value;
  12232. }
  12233. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U, p);
  12234. DUK_RAW_WRITE_U32_BE(p, val);
  12235. return p;
  12236. }
  12237. DUK_LOCAL duk_uint8_t *duk__dump_varmap(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) {
  12238. duk_tval *tv;
  12239. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  12240. if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) {
  12241. duk_hobject *h;
  12242. duk_uint_fast32_t i;
  12243. h = DUK_TVAL_GET_OBJECT(tv);
  12244. DUK_ASSERT(h != NULL);
  12245. /* We know _Varmap only has own properties so walk property
  12246. * table directly. We also know _Varmap is dense and all
  12247. * values are numbers; assert for these. GC and finalizers
  12248. * shouldn't affect _Varmap so side effects should be fine.
  12249. */
  12250. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  12251. duk_hstring *key;
  12252. duk_tval *tv_val;
  12253. duk_uint32_t val;
  12254. key = DUK_HOBJECT_E_GET_KEY(thr->heap, h, i);
  12255. DUK_ASSERT(key != NULL); /* _Varmap is dense */
  12256. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h, i));
  12257. tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h, i);
  12258. DUK_ASSERT(tv_val != NULL);
  12259. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_val)); /* known to be number; in fact an integer */
  12260. #if defined(DUK_USE_FASTINT)
  12261. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv_val));
  12262. DUK_ASSERT(DUK_TVAL_GET_FASTINT(tv_val) == (duk_int64_t) DUK_TVAL_GET_FASTINT_U32(tv_val)); /* known to be 32-bit */
  12263. val = DUK_TVAL_GET_FASTINT_U32(tv_val);
  12264. #else
  12265. val = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv_val);
  12266. #endif
  12267. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  12268. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U + DUK_HSTRING_GET_BYTELEN(key) + 4U, p);
  12269. p = duk__dump_hstring_raw(p, key);
  12270. DUK_RAW_WRITE_U32_BE(p, val);
  12271. }
  12272. }
  12273. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U, p);
  12274. DUK_RAW_WRITE_U32_BE(p, 0); /* end of _Varmap */
  12275. return p;
  12276. }
  12277. DUK_LOCAL duk_uint8_t *duk__dump_formals(duk_hthread *thr, duk_uint8_t *p, duk_bufwriter_ctx *bw_ctx, duk_hobject *func) {
  12278. duk_tval *tv;
  12279. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) func, DUK_HTHREAD_STRING_INT_FORMALS(thr));
  12280. if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) {
  12281. duk_harray *h;
  12282. duk_uint32_t i;
  12283. /* Here we rely on _Formals being a dense array containing
  12284. * strings. This should be the case unless _Formals has been
  12285. * tweaked by the application (which we don't support right
  12286. * now).
  12287. */
  12288. h = (duk_harray *) DUK_TVAL_GET_OBJECT(tv);
  12289. DUK_ASSERT(h != NULL);
  12290. DUK_ASSERT(DUK_HOBJECT_IS_ARRAY((duk_hobject *) h));
  12291. DUK_ASSERT(h->length <= DUK_HOBJECT_GET_ASIZE((duk_hobject *) h));
  12292. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U, p);
  12293. DUK_ASSERT(h->length != DUK__NO_FORMALS); /* limits */
  12294. DUK_RAW_WRITE_U32_BE(p, h->length);
  12295. for (i = 0; i < h->length; i++) {
  12296. duk_tval *tv_val;
  12297. duk_hstring *varname;
  12298. tv_val = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, (duk_hobject *) h, i);
  12299. DUK_ASSERT(tv_val != NULL);
  12300. DUK_ASSERT(DUK_TVAL_IS_STRING(tv_val));
  12301. varname = DUK_TVAL_GET_STRING(tv_val);
  12302. DUK_ASSERT(varname != NULL);
  12303. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(varname) >= 1);
  12304. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  12305. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U + DUK_HSTRING_GET_BYTELEN(varname), p);
  12306. p = duk__dump_hstring_raw(p, varname);
  12307. }
  12308. } else {
  12309. DUK_DD(DUK_DDPRINT("dumping function without _Formals, emit marker to indicate missing _Formals"));
  12310. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 4U, p);
  12311. DUK_RAW_WRITE_U32_BE(p, DUK__NO_FORMALS); /* marker: no formals */
  12312. }
  12313. return p;
  12314. }
  12315. static duk_uint8_t *duk__dump_func(duk_hthread *thr, duk_hcompfunc *func, duk_bufwriter_ctx *bw_ctx, duk_uint8_t *p) {
  12316. duk_tval *tv, *tv_end;
  12317. duk_instr_t *ins, *ins_end;
  12318. duk_hobject **fn, **fn_end;
  12319. duk_hstring *h_str;
  12320. duk_uint32_t count_instr;
  12321. duk_uint32_t tmp32;
  12322. duk_uint16_t tmp16;
  12323. duk_double_t d;
  12324. DUK_DD(DUK_DDPRINT("dumping function %p to %p: "
  12325. "consts=[%p,%p[ (%ld bytes, %ld items), "
  12326. "funcs=[%p,%p[ (%ld bytes, %ld items), "
  12327. "code=[%p,%p[ (%ld bytes, %ld items)",
  12328. (void *) func,
  12329. (void *) p,
  12330. (void *) DUK_HCOMPFUNC_GET_CONSTS_BASE(thr->heap, func),
  12331. (void *) DUK_HCOMPFUNC_GET_CONSTS_END(thr->heap, func),
  12332. (long) DUK_HCOMPFUNC_GET_CONSTS_SIZE(thr->heap, func),
  12333. (long) DUK_HCOMPFUNC_GET_CONSTS_COUNT(thr->heap, func),
  12334. (void *) DUK_HCOMPFUNC_GET_FUNCS_BASE(thr->heap, func),
  12335. (void *) DUK_HCOMPFUNC_GET_FUNCS_END(thr->heap, func),
  12336. (long) DUK_HCOMPFUNC_GET_FUNCS_SIZE(thr->heap, func),
  12337. (long) DUK_HCOMPFUNC_GET_FUNCS_COUNT(thr->heap, func),
  12338. (void *) DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, func),
  12339. (void *) DUK_HCOMPFUNC_GET_CODE_END(thr->heap, func),
  12340. (long) DUK_HCOMPFUNC_GET_CODE_SIZE(thr->heap, func),
  12341. (long) DUK_HCOMPFUNC_GET_CODE_COUNT(thr->heap, func)));
  12342. DUK_ASSERT(DUK_USE_ESBC_MAX_BYTES <= 0x7fffffffUL); /* ensures no overflow */
  12343. count_instr = (duk_uint32_t) DUK_HCOMPFUNC_GET_CODE_COUNT(thr->heap, func);
  12344. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 3U * 4U + 2U * 2U + 3U * 4U + count_instr * 4U, p);
  12345. /* Fixed header info. */
  12346. tmp32 = count_instr;
  12347. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12348. tmp32 = (duk_uint32_t) DUK_HCOMPFUNC_GET_CONSTS_COUNT(thr->heap, func);
  12349. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12350. tmp32 = (duk_uint32_t) DUK_HCOMPFUNC_GET_FUNCS_COUNT(thr->heap, func);
  12351. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12352. tmp16 = func->nregs;
  12353. DUK_RAW_WRITE_U16_BE(p, tmp16);
  12354. tmp16 = func->nargs;
  12355. DUK_RAW_WRITE_U16_BE(p, tmp16);
  12356. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  12357. tmp32 = func->start_line;
  12358. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12359. tmp32 = func->end_line;
  12360. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12361. #else
  12362. DUK_RAW_WRITE_U32_BE(p, 0);
  12363. DUK_RAW_WRITE_U32_BE(p, 0);
  12364. #endif
  12365. tmp32 = DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) func); /* masks flags, only duk_hobject flags */
  12366. tmp32 &= ~(DUK_HOBJECT_FLAG_HAVE_FINALIZER); /* finalizer flag is lost */
  12367. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12368. /* Bytecode instructions: endian conversion needed unless
  12369. * platform is big endian.
  12370. */
  12371. ins = DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, func);
  12372. ins_end = DUK_HCOMPFUNC_GET_CODE_END(thr->heap, func);
  12373. DUK_ASSERT((duk_size_t) (ins_end - ins) == (duk_size_t) count_instr);
  12374. #if defined(DUK_USE_INTEGER_BE)
  12375. duk_memcpy_unsafe((void *) p, (const void *) ins, (size_t) (ins_end - ins));
  12376. p += (size_t) (ins_end - ins);
  12377. #else
  12378. while (ins != ins_end) {
  12379. tmp32 = (duk_uint32_t) (*ins);
  12380. DUK_RAW_WRITE_U32_BE(p, tmp32);
  12381. ins++;
  12382. }
  12383. #endif
  12384. /* Constants: variable size encoding. */
  12385. tv = DUK_HCOMPFUNC_GET_CONSTS_BASE(thr->heap, func);
  12386. tv_end = DUK_HCOMPFUNC_GET_CONSTS_END(thr->heap, func);
  12387. while (tv != tv_end) {
  12388. /* constants are strings or numbers now */
  12389. DUK_ASSERT(DUK_TVAL_IS_STRING(tv) ||
  12390. DUK_TVAL_IS_NUMBER(tv));
  12391. if (DUK_TVAL_IS_STRING(tv)) {
  12392. h_str = DUK_TVAL_GET_STRING(tv);
  12393. DUK_ASSERT(h_str != NULL);
  12394. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= 0x7fffffffUL); /* ensures no overflow */
  12395. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1U + 4U + DUK_HSTRING_GET_BYTELEN(h_str), p);
  12396. *p++ = DUK__SER_STRING;
  12397. p = duk__dump_hstring_raw(p, h_str);
  12398. } else {
  12399. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  12400. p = DUK_BW_ENSURE_RAW(thr, bw_ctx, 1U + 8U, p);
  12401. *p++ = DUK__SER_NUMBER;
  12402. d = DUK_TVAL_GET_NUMBER(tv);
  12403. DUK_RAW_WRITE_DOUBLE_BE(p, d);
  12404. }
  12405. tv++;
  12406. }
  12407. /* Inner functions recursively. */
  12408. fn = (duk_hobject **) DUK_HCOMPFUNC_GET_FUNCS_BASE(thr->heap, func);
  12409. fn_end = (duk_hobject **) DUK_HCOMPFUNC_GET_FUNCS_END(thr->heap, func);
  12410. while (fn != fn_end) {
  12411. /* XXX: This causes recursion up to inner function depth
  12412. * which is normally not an issue, e.g. mark-and-sweep uses
  12413. * a recursion limiter to avoid C stack issues. Avoiding
  12414. * this would mean some sort of a work list or just refusing
  12415. * to serialize deep functions.
  12416. */
  12417. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(*fn));
  12418. p = duk__dump_func(thr, (duk_hcompfunc *) *fn, bw_ctx, p);
  12419. fn++;
  12420. }
  12421. /* Lexenv and varenv are not dumped. */
  12422. /* Object extra properties.
  12423. *
  12424. * There are some difference between function templates and functions.
  12425. * For example, function templates don't have .length and nargs is
  12426. * normally used to instantiate the functions.
  12427. */
  12428. p = duk__dump_uint32_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_LENGTH, (duk_uint32_t) func->nargs);
  12429. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  12430. p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_NAME);
  12431. #endif
  12432. #if defined(DUK_USE_FUNC_FILENAME_PROPERTY)
  12433. p = duk__dump_string_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_FILE_NAME);
  12434. #endif
  12435. #if defined(DUK_USE_PC2LINE)
  12436. p = duk__dump_buffer_prop(thr, p, bw_ctx, (duk_hobject *) func, DUK_STRIDX_INT_PC2LINE);
  12437. #endif
  12438. p = duk__dump_varmap(thr, p, bw_ctx, (duk_hobject *) func);
  12439. p = duk__dump_formals(thr, p, bw_ctx, (duk_hobject *) func);
  12440. DUK_DD(DUK_DDPRINT("serialized function %p -> final pointer %p", (void *) func, (void *) p));
  12441. return p;
  12442. }
  12443. /* Load a function from bytecode. The function object returned here must
  12444. * match what is created by duk_js_push_closure() with respect to its flags,
  12445. * properties, etc.
  12446. *
  12447. * NOTE: there are intentionally no input buffer length / bound checks.
  12448. * Adding them would be easy but wouldn't ensure memory safety as untrusted
  12449. * or broken bytecode is unsafe during execution unless the opcodes themselves
  12450. * are validated (which is quite complex, especially for indirect opcodes).
  12451. */
  12452. #define DUK__ASSERT_LEFT(n) do { \
  12453. DUK_ASSERT((duk_size_t) (p_end - p) >= (duk_size_t) (n)); \
  12454. } while (0)
  12455. static duk_uint8_t *duk__load_func(duk_hthread *thr, duk_uint8_t *p, duk_uint8_t *p_end) {
  12456. duk_hcompfunc *h_fun;
  12457. duk_hbuffer *h_data;
  12458. duk_size_t data_size;
  12459. duk_uint32_t count_instr, count_const, count_funcs;
  12460. duk_uint32_t n;
  12461. duk_uint32_t tmp32;
  12462. duk_small_uint_t const_type;
  12463. duk_uint8_t *fun_data;
  12464. duk_uint8_t *q;
  12465. duk_idx_t idx_base;
  12466. duk_tval *tv1;
  12467. duk_uarridx_t arr_idx;
  12468. duk_uarridx_t arr_limit;
  12469. duk_hobject *func_env;
  12470. duk_bool_t need_pop;
  12471. /* XXX: There's some overlap with duk_js_closure() here, but
  12472. * seems difficult to share code. Ensure that the final function
  12473. * looks the same as created by duk_js_closure().
  12474. */
  12475. DUK_ASSERT(thr != NULL);
  12476. DUK_DD(DUK_DDPRINT("loading function, p=%p, p_end=%p", (void *) p, (void *) p_end));
  12477. DUK__ASSERT_LEFT(3 * 4);
  12478. count_instr = DUK_RAW_READ_U32_BE(p);
  12479. count_const = DUK_RAW_READ_U32_BE(p);
  12480. count_funcs = DUK_RAW_READ_U32_BE(p);
  12481. data_size = sizeof(duk_tval) * count_const +
  12482. sizeof(duk_hobject *) * count_funcs +
  12483. sizeof(duk_instr_t) * count_instr;
  12484. DUK_DD(DUK_DDPRINT("instr=%ld, const=%ld, funcs=%ld, data_size=%ld",
  12485. (long) count_instr, (long) count_const,
  12486. (long) count_const, (long) data_size));
  12487. /* Value stack is used to ensure reachability of constants and
  12488. * inner functions being loaded. Require enough space to handle
  12489. * large functions correctly.
  12490. */
  12491. duk_require_stack(thr, (duk_idx_t) (2 + count_const + count_funcs));
  12492. idx_base = duk_get_top(thr);
  12493. /* Push function object, init flags etc. This must match
  12494. * duk_js_push_closure() quite carefully.
  12495. */
  12496. h_fun = duk_push_hcompfunc(thr);
  12497. DUK_ASSERT(h_fun != NULL);
  12498. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) h_fun));
  12499. DUK_ASSERT(DUK_HCOMPFUNC_GET_DATA(thr->heap, h_fun) == NULL);
  12500. DUK_ASSERT(DUK_HCOMPFUNC_GET_FUNCS(thr->heap, h_fun) == NULL);
  12501. DUK_ASSERT(DUK_HCOMPFUNC_GET_BYTECODE(thr->heap, h_fun) == NULL);
  12502. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_fun) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  12503. h_fun->nregs = DUK_RAW_READ_U16_BE(p);
  12504. h_fun->nargs = DUK_RAW_READ_U16_BE(p);
  12505. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  12506. h_fun->start_line = DUK_RAW_READ_U32_BE(p);
  12507. h_fun->end_line = DUK_RAW_READ_U32_BE(p);
  12508. #else
  12509. p += 8; /* skip line info */
  12510. #endif
  12511. /* duk_hcompfunc flags; quite version specific */
  12512. tmp32 = DUK_RAW_READ_U32_BE(p);
  12513. DUK_HEAPHDR_SET_FLAGS((duk_heaphdr *) h_fun, tmp32); /* masks flags to only change duk_hobject flags */
  12514. /* standard prototype (no need to set here, already set) */
  12515. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_fun) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  12516. #if 0
  12517. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &h_fun->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  12518. #endif
  12519. /* assert just a few critical flags */
  12520. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h_fun) == DUK_HTYPE_OBJECT);
  12521. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(&h_fun->obj));
  12522. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(&h_fun->obj));
  12523. DUK_ASSERT(!DUK_HOBJECT_HAS_NATFUNC(&h_fun->obj));
  12524. DUK_ASSERT(!DUK_HOBJECT_IS_THREAD(&h_fun->obj));
  12525. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&h_fun->obj));
  12526. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&h_fun->obj));
  12527. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&h_fun->obj));
  12528. /* Create function 'data' buffer but don't attach it yet. */
  12529. fun_data = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, data_size);
  12530. DUK_ASSERT(fun_data != NULL);
  12531. /* Load bytecode instructions. */
  12532. DUK_ASSERT(sizeof(duk_instr_t) == 4);
  12533. DUK__ASSERT_LEFT(count_instr * sizeof(duk_instr_t));
  12534. #if defined(DUK_USE_INTEGER_BE)
  12535. q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs;
  12536. duk_memcpy((void *) q,
  12537. (const void *) p,
  12538. sizeof(duk_instr_t) * count_instr);
  12539. p += sizeof(duk_instr_t) * count_instr;
  12540. #else
  12541. q = fun_data + sizeof(duk_tval) * count_const + sizeof(duk_hobject *) * count_funcs;
  12542. for (n = count_instr; n > 0; n--) {
  12543. *((duk_instr_t *) (void *) q) = DUK_RAW_READ_U32_BE(p);
  12544. q += sizeof(duk_instr_t);
  12545. }
  12546. #endif
  12547. /* Load constants onto value stack but don't yet copy to buffer. */
  12548. for (n = count_const; n > 0; n--) {
  12549. DUK__ASSERT_LEFT(1);
  12550. const_type = DUK_RAW_READ_U8(p);
  12551. switch (const_type) {
  12552. case DUK__SER_STRING: {
  12553. p = duk__load_string_raw(thr, p);
  12554. break;
  12555. }
  12556. case DUK__SER_NUMBER: {
  12557. /* Important to do a fastint check so that constants are
  12558. * properly read back as fastints.
  12559. */
  12560. duk_tval tv_tmp;
  12561. duk_double_t val;
  12562. DUK__ASSERT_LEFT(8);
  12563. val = DUK_RAW_READ_DOUBLE_BE(p);
  12564. DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(&tv_tmp, val);
  12565. duk_push_tval(thr, &tv_tmp);
  12566. break;
  12567. }
  12568. default: {
  12569. goto format_error;
  12570. }
  12571. }
  12572. }
  12573. /* Load inner functions to value stack, but don't yet copy to buffer. */
  12574. for (n = count_funcs; n > 0; n--) {
  12575. p = duk__load_func(thr, p, p_end);
  12576. if (p == NULL) {
  12577. goto format_error;
  12578. }
  12579. }
  12580. /* With constants and inner functions on value stack, we can now
  12581. * atomically finish the function 'data' buffer, bump refcounts,
  12582. * etc.
  12583. *
  12584. * Here we take advantage of the value stack being just a duk_tval
  12585. * array: we can just memcpy() the constants as long as we incref
  12586. * them afterwards.
  12587. */
  12588. h_data = (duk_hbuffer *) duk_known_hbuffer(thr, idx_base + 1);
  12589. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC(h_data));
  12590. DUK_HCOMPFUNC_SET_DATA(thr->heap, h_fun, h_data);
  12591. DUK_HBUFFER_INCREF(thr, h_data);
  12592. tv1 = duk_get_tval(thr, idx_base + 2); /* may be NULL if no constants or inner funcs */
  12593. DUK_ASSERT((count_const == 0 && count_funcs == 0) || tv1 != NULL);
  12594. q = fun_data;
  12595. duk_memcpy_unsafe((void *) q, (const void *) tv1, sizeof(duk_tval) * count_const);
  12596. for (n = count_const; n > 0; n--) {
  12597. DUK_TVAL_INCREF_FAST(thr, (duk_tval *) (void *) q); /* no side effects */
  12598. q += sizeof(duk_tval);
  12599. }
  12600. tv1 += count_const;
  12601. DUK_HCOMPFUNC_SET_FUNCS(thr->heap, h_fun, (duk_hobject **) (void *) q);
  12602. for (n = count_funcs; n > 0; n--) {
  12603. duk_hobject *h_obj;
  12604. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  12605. h_obj = DUK_TVAL_GET_OBJECT(tv1);
  12606. DUK_ASSERT(h_obj != NULL);
  12607. tv1++;
  12608. DUK_HOBJECT_INCREF(thr, h_obj);
  12609. *((duk_hobject **) (void *) q) = h_obj;
  12610. q += sizeof(duk_hobject *);
  12611. }
  12612. DUK_HCOMPFUNC_SET_BYTECODE(thr->heap, h_fun, (duk_instr_t *) (void *) q);
  12613. /* The function object is now reachable and refcounts are fine,
  12614. * so we can pop off all the temporaries.
  12615. */
  12616. DUK_DDD(DUK_DDDPRINT("function is reachable, reset top; func: %!iT", duk_get_tval(thr, idx_base)));
  12617. duk_set_top(thr, idx_base + 1);
  12618. /* Setup function properties. */
  12619. tmp32 = DUK_RAW_READ_U32_BE(p);
  12620. duk_push_u32(thr, tmp32);
  12621. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_C);
  12622. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  12623. p = duk__load_string_raw(thr, p); /* -> [ func funcname ] */
  12624. func_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  12625. DUK_ASSERT(func_env != NULL);
  12626. need_pop = 0;
  12627. if (DUK_HOBJECT_HAS_NAMEBINDING((duk_hobject *) h_fun)) {
  12628. /* Original function instance/template had NAMEBINDING.
  12629. * Must create a lexical environment on loading to allow
  12630. * recursive functions like 'function foo() { foo(); }'.
  12631. */
  12632. duk_hdecenv *new_env;
  12633. new_env = duk_hdecenv_alloc(thr,
  12634. DUK_HOBJECT_FLAG_EXTENSIBLE |
  12635. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV));
  12636. DUK_ASSERT(new_env != NULL);
  12637. DUK_ASSERT(new_env->thread == NULL); /* Closed. */
  12638. DUK_ASSERT(new_env->varmap == NULL);
  12639. DUK_ASSERT(new_env->regbase_byteoff == 0);
  12640. DUK_ASSERT_HDECENV_VALID(new_env);
  12641. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) new_env) == NULL);
  12642. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, (duk_hobject *) new_env, func_env);
  12643. DUK_HOBJECT_INCREF(thr, func_env);
  12644. func_env = (duk_hobject *) new_env;
  12645. duk_push_hobject(thr, (duk_hobject *) new_env);
  12646. duk_dup_m2(thr); /* -> [ func funcname env funcname ] */
  12647. duk_dup(thr, idx_base); /* -> [ func funcname env funcname func ] */
  12648. duk_xdef_prop(thr, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ func funcname env ] */
  12649. need_pop = 1; /* Need to pop env, but -after- updating h_fun and increfs. */
  12650. }
  12651. DUK_ASSERT(func_env != NULL);
  12652. DUK_HCOMPFUNC_SET_LEXENV(thr->heap, h_fun, func_env);
  12653. DUK_HCOMPFUNC_SET_VARENV(thr->heap, h_fun, func_env);
  12654. DUK_HOBJECT_INCREF(thr, func_env);
  12655. DUK_HOBJECT_INCREF(thr, func_env);
  12656. if (need_pop) {
  12657. duk_pop(thr);
  12658. }
  12659. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C);
  12660. #endif /* DUK_USE_FUNC_NAME_PROPERTY */
  12661. #if defined(DUK_USE_FUNC_FILENAME_PROPERTY)
  12662. p = duk__load_string_raw(thr, p);
  12663. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_C);
  12664. #endif /* DUK_USE_FUNC_FILENAME_PROPERTY */
  12665. if (DUK_HOBJECT_HAS_CONSTRUCTABLE((duk_hobject *) h_fun)) {
  12666. /* Restore empty external .prototype only for constructable
  12667. * functions.
  12668. */
  12669. duk_push_object(thr);
  12670. duk_dup_m2(thr);
  12671. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* func.prototype.constructor = func */
  12672. duk_compact_m1(thr);
  12673. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W);
  12674. }
  12675. #if defined(DUK_USE_PC2LINE)
  12676. p = duk__load_buffer_raw(thr, p);
  12677. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_WC);
  12678. #endif /* DUK_USE_PC2LINE */
  12679. duk_push_object(thr); /* _Varmap */
  12680. for (;;) {
  12681. /* XXX: awkward */
  12682. p = duk__load_string_raw(thr, p);
  12683. if (duk_get_length(thr, -1) == 0) {
  12684. duk_pop(thr);
  12685. break;
  12686. }
  12687. tmp32 = DUK_RAW_READ_U32_BE(p);
  12688. duk_push_u32(thr, tmp32);
  12689. duk_put_prop(thr, -3);
  12690. }
  12691. duk_compact_m1(thr);
  12692. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE);
  12693. /* _Formals may have been missing in the original function, which is
  12694. * handled using a marker length.
  12695. */
  12696. arr_limit = DUK_RAW_READ_U32_BE(p);
  12697. if (arr_limit != DUK__NO_FORMALS) {
  12698. duk_push_array(thr); /* _Formals */
  12699. for (arr_idx = 0; arr_idx < arr_limit; arr_idx++) {
  12700. p = duk__load_string_raw(thr, p);
  12701. duk_put_prop_index(thr, -2, arr_idx);
  12702. }
  12703. duk_compact_m1(thr);
  12704. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE);
  12705. } else {
  12706. DUK_DD(DUK_DDPRINT("no _Formals in dumped function"));
  12707. }
  12708. /* Return with final function pushed on stack top. */
  12709. DUK_DD(DUK_DDPRINT("final loaded function: %!iT", duk_get_tval(thr, -1)));
  12710. DUK_ASSERT_TOP(thr, idx_base + 1);
  12711. return p;
  12712. format_error:
  12713. return NULL;
  12714. }
  12715. DUK_EXTERNAL void duk_dump_function(duk_hthread *thr) {
  12716. duk_hcompfunc *func;
  12717. duk_bufwriter_ctx bw_ctx_alloc;
  12718. duk_bufwriter_ctx *bw_ctx = &bw_ctx_alloc;
  12719. duk_uint8_t *p;
  12720. DUK_ASSERT_API_ENTRY(thr);
  12721. /* Bound functions don't have all properties so we'd either need to
  12722. * lookup the non-bound target function or reject bound functions.
  12723. * For now, bound functions are rejected with TypeError.
  12724. */
  12725. func = duk_require_hcompfunc(thr, -1);
  12726. DUK_ASSERT(func != NULL);
  12727. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(&func->obj));
  12728. /* Estimating the result size beforehand would be costly, so
  12729. * start with a reasonable size and extend as needed.
  12730. */
  12731. DUK_BW_INIT_PUSHBUF(thr, bw_ctx, DUK__BYTECODE_INITIAL_ALLOC);
  12732. p = DUK_BW_GET_PTR(thr, bw_ctx);
  12733. *p++ = DUK__SER_MARKER;
  12734. p = duk__dump_func(thr, func, bw_ctx, p);
  12735. DUK_BW_SET_PTR(thr, bw_ctx, p);
  12736. DUK_BW_COMPACT(thr, bw_ctx);
  12737. DUK_DD(DUK_DDPRINT("serialized result: %!T", duk_get_tval(thr, -1)));
  12738. duk_remove_m2(thr); /* [ ... func buf ] -> [ ... buf ] */
  12739. }
  12740. DUK_EXTERNAL void duk_load_function(duk_hthread *thr) {
  12741. duk_uint8_t *p_buf, *p, *p_end;
  12742. duk_size_t sz;
  12743. DUK_ASSERT_API_ENTRY(thr);
  12744. p_buf = (duk_uint8_t *) duk_require_buffer(thr, -1, &sz);
  12745. DUK_ASSERT(p_buf != NULL);
  12746. /* The caller is responsible for being sure that bytecode being loaded
  12747. * is valid and trusted. Invalid bytecode can cause memory unsafe
  12748. * behavior directly during loading or later during bytecode execution
  12749. * (instruction validation would be quite complex to implement).
  12750. *
  12751. * This signature check is the only sanity check for detecting
  12752. * accidental invalid inputs. The initial byte ensures no ordinary
  12753. * string or Symbol will be accepted by accident.
  12754. */
  12755. p = p_buf;
  12756. p_end = p_buf + sz;
  12757. if (sz < 1 || p[0] != DUK__SER_MARKER) {
  12758. goto format_error;
  12759. }
  12760. p++;
  12761. p = duk__load_func(thr, p, p_end);
  12762. if (p == NULL) {
  12763. goto format_error;
  12764. }
  12765. duk_remove_m2(thr); /* [ ... buf func ] -> [ ... func ] */
  12766. return;
  12767. format_error:
  12768. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_BYTECODE);
  12769. DUK_WO_NORETURN(return;);
  12770. }
  12771. #else /* DUK_USE_BYTECODE_DUMP_SUPPORT */
  12772. DUK_EXTERNAL void duk_dump_function(duk_hthread *thr) {
  12773. DUK_ASSERT_API_ENTRY(thr);
  12774. DUK_ERROR_UNSUPPORTED(thr);
  12775. DUK_WO_NORETURN(return;);
  12776. }
  12777. DUK_EXTERNAL void duk_load_function(duk_hthread *thr) {
  12778. DUK_ASSERT_API_ENTRY(thr);
  12779. DUK_ERROR_UNSUPPORTED(thr);
  12780. DUK_WO_NORETURN(return;);
  12781. }
  12782. #endif /* DUK_USE_BYTECODE_DUMP_SUPPORT */
  12783. /* automatic undefs */
  12784. #undef DUK__ASSERT_LEFT
  12785. #undef DUK__BYTECODE_INITIAL_ALLOC
  12786. #undef DUK__NO_FORMALS
  12787. #undef DUK__SER_MARKER
  12788. #undef DUK__SER_NUMBER
  12789. #undef DUK__SER_STRING
  12790. #line 1 "duk_api_call.c"
  12791. /*
  12792. * Calls.
  12793. *
  12794. * Protected variants should avoid ever throwing an error. Must be careful
  12795. * to catch errors related to value stack manipulation and property lookup,
  12796. * not just the call itself.
  12797. *
  12798. * The only exception is when arguments are insane, e.g. nargs/nrets are out
  12799. * of bounds; in such cases an error is thrown for two reasons. First, we
  12800. * can't always respect the value stack input/output guarantees in such cases
  12801. * so the caller would end up with the value stack in an unexpected state.
  12802. * Second, an attempt to create an error might itself fail (although this
  12803. * could be avoided by pushing a preallocated object/string or a primitive
  12804. * value).
  12805. */
  12806. /* #include duk_internal.h -> already included */
  12807. /*
  12808. * Helpers
  12809. */
  12810. struct duk__pcall_prop_args {
  12811. duk_idx_t obj_idx;
  12812. duk_idx_t nargs;
  12813. duk_small_uint_t call_flags;
  12814. };
  12815. typedef struct duk__pcall_prop_args duk__pcall_prop_args;
  12816. struct duk__pcall_method_args {
  12817. duk_idx_t nargs;
  12818. duk_small_uint_t call_flags;
  12819. };
  12820. typedef struct duk__pcall_method_args duk__pcall_method_args;
  12821. struct duk__pcall_args {
  12822. duk_idx_t nargs;
  12823. duk_small_uint_t call_flags;
  12824. };
  12825. typedef struct duk__pcall_args duk__pcall_args;
  12826. /* Compute and validate idx_func for a certain 'nargs' and 'other'
  12827. * parameter count (1 or 2, depending on whether 'this' binding is
  12828. * present).
  12829. */
  12830. DUK_LOCAL duk_idx_t duk__call_get_idx_func(duk_hthread *thr, duk_idx_t nargs, duk_idx_t other) {
  12831. duk_idx_t idx_func;
  12832. /* XXX: byte arithmetic? */
  12833. DUK_ASSERT(other >= 0);
  12834. idx_func = duk_get_top(thr) - nargs - other;
  12835. if (DUK_UNLIKELY((idx_func | nargs) < 0)) { /* idx_func < 0 || nargs < 0; OR sign bits */
  12836. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  12837. DUK_WO_NORETURN(return 0;);
  12838. }
  12839. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  12840. return idx_func;
  12841. }
  12842. /* Compute idx_func, assume index will be valid. This is a valid assumption
  12843. * for protected calls: nargs < 0 is checked explicitly and duk_safe_call()
  12844. * validates the argument count.
  12845. */
  12846. DUK_LOCAL duk_idx_t duk__call_get_idx_func_unvalidated(duk_hthread *thr, duk_idx_t nargs, duk_idx_t other) {
  12847. duk_idx_t idx_func;
  12848. /* XXX: byte arithmetic? */
  12849. DUK_ASSERT(nargs >= 0);
  12850. DUK_ASSERT(other >= 0);
  12851. idx_func = duk_get_top(thr) - nargs - other;
  12852. DUK_ASSERT(idx_func >= 0);
  12853. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  12854. return idx_func;
  12855. }
  12856. /* Prepare value stack for a method call through an object property.
  12857. * May currently throw an error e.g. when getting the property.
  12858. */
  12859. DUK_LOCAL void duk__call_prop_prep_stack(duk_hthread *thr, duk_idx_t normalized_obj_idx, duk_idx_t nargs) {
  12860. DUK_ASSERT_CTX_VALID(thr);
  12861. DUK_ASSERT(nargs >= 0);
  12862. DUK_DDD(DUK_DDDPRINT("duk__call_prop_prep_stack, normalized_obj_idx=%ld, nargs=%ld, stacktop=%ld",
  12863. (long) normalized_obj_idx, (long) nargs, (long) duk_get_top(thr)));
  12864. /* [... key arg1 ... argN] */
  12865. /* duplicate key */
  12866. duk_dup(thr, -nargs - 1); /* Note: -nargs alone would fail for nargs == 0, this is OK */
  12867. (void) duk_get_prop(thr, normalized_obj_idx);
  12868. DUK_DDD(DUK_DDDPRINT("func: %!T", (duk_tval *) duk_get_tval(thr, -1)));
  12869. #if defined(DUK_USE_VERBOSE_ERRORS)
  12870. if (DUK_UNLIKELY(!duk_is_callable(thr, -1))) {
  12871. duk_tval *tv_targ;
  12872. duk_tval *tv_base;
  12873. duk_tval *tv_key;
  12874. tv_targ = DUK_GET_TVAL_NEGIDX(thr, -1);
  12875. tv_base = DUK_GET_TVAL_POSIDX(thr, normalized_obj_idx);
  12876. tv_key = DUK_GET_TVAL_NEGIDX(thr, -nargs - 2);
  12877. DUK_ASSERT(tv_targ >= thr->valstack_bottom && tv_targ < thr->valstack_top);
  12878. DUK_ASSERT(tv_base >= thr->valstack_bottom && tv_base < thr->valstack_top);
  12879. DUK_ASSERT(tv_key >= thr->valstack_bottom && tv_key < thr->valstack_top);
  12880. duk_call_setup_propcall_error(thr, tv_targ, tv_base, tv_key);
  12881. }
  12882. #endif
  12883. /* [... key arg1 ... argN func] */
  12884. duk_replace(thr, -nargs - 2);
  12885. /* [... func arg1 ... argN] */
  12886. duk_dup(thr, normalized_obj_idx);
  12887. duk_insert(thr, -nargs - 1);
  12888. /* [... func this arg1 ... argN] */
  12889. }
  12890. DUK_EXTERNAL void duk_call(duk_hthread *thr, duk_idx_t nargs) {
  12891. duk_small_uint_t call_flags;
  12892. duk_idx_t idx_func;
  12893. DUK_ASSERT_API_ENTRY(thr);
  12894. idx_func = duk__call_get_idx_func(thr, nargs, 1);
  12895. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  12896. duk_insert_undefined(thr, idx_func + 1);
  12897. call_flags = 0; /* not protected, respect reclimit, not constructor */
  12898. duk_handle_call_unprotected(thr, idx_func, call_flags);
  12899. }
  12900. DUK_EXTERNAL void duk_call_method(duk_hthread *thr, duk_idx_t nargs) {
  12901. duk_small_uint_t call_flags;
  12902. duk_idx_t idx_func;
  12903. DUK_ASSERT_API_ENTRY(thr);
  12904. idx_func = duk__call_get_idx_func(thr, nargs, 2);
  12905. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  12906. call_flags = 0; /* not protected, respect reclimit, not constructor */
  12907. duk_handle_call_unprotected(thr, idx_func, call_flags);
  12908. }
  12909. DUK_EXTERNAL void duk_call_prop(duk_hthread *thr, duk_idx_t obj_idx, duk_idx_t nargs) {
  12910. /*
  12911. * XXX: if duk_handle_call() took values through indices, this could be
  12912. * made much more sensible. However, duk_handle_call() needs to fudge
  12913. * the 'this' and 'func' values to handle bound functions, which is now
  12914. * done "in-place", so this is not a trivial change.
  12915. */
  12916. DUK_ASSERT_API_ENTRY(thr);
  12917. obj_idx = duk_require_normalize_index(thr, obj_idx); /* make absolute */
  12918. if (DUK_UNLIKELY(nargs < 0)) {
  12919. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  12920. DUK_WO_NORETURN(return;);
  12921. }
  12922. duk__call_prop_prep_stack(thr, obj_idx, nargs);
  12923. duk_call_method(thr, nargs);
  12924. }
  12925. DUK_LOCAL duk_ret_t duk__pcall_raw(duk_hthread *thr, void *udata) {
  12926. duk__pcall_args *args;
  12927. duk_idx_t idx_func;
  12928. duk_int_t ret;
  12929. DUK_ASSERT_CTX_VALID(thr);
  12930. DUK_ASSERT(udata != NULL);
  12931. args = (duk__pcall_args *) udata;
  12932. idx_func = duk__call_get_idx_func_unvalidated(thr, args->nargs, 1);
  12933. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  12934. duk_insert_undefined(thr, idx_func + 1);
  12935. ret = duk_handle_call_unprotected(thr, idx_func, args->call_flags);
  12936. DUK_ASSERT(ret == 0);
  12937. DUK_UNREF(ret);
  12938. return 1;
  12939. }
  12940. DUK_EXTERNAL duk_int_t duk_pcall(duk_hthread *thr, duk_idx_t nargs) {
  12941. duk__pcall_args args;
  12942. DUK_ASSERT_API_ENTRY(thr);
  12943. args.nargs = nargs;
  12944. if (DUK_UNLIKELY(nargs < 0)) {
  12945. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  12946. DUK_WO_NORETURN(return DUK_EXEC_ERROR;);
  12947. }
  12948. args.call_flags = 0;
  12949. return duk_safe_call(thr, duk__pcall_raw, (void *) &args /*udata*/, nargs + 1 /*nargs*/, 1 /*nrets*/);
  12950. }
  12951. DUK_LOCAL duk_ret_t duk__pcall_method_raw(duk_hthread *thr, void *udata) {
  12952. duk__pcall_method_args *args;
  12953. duk_idx_t idx_func;
  12954. duk_int_t ret;
  12955. DUK_ASSERT_CTX_VALID(thr);
  12956. DUK_ASSERT(udata != NULL);
  12957. args = (duk__pcall_method_args *) udata;
  12958. idx_func = duk__call_get_idx_func_unvalidated(thr, args->nargs, 2);
  12959. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  12960. ret = duk_handle_call_unprotected(thr, idx_func, args->call_flags);
  12961. DUK_ASSERT(ret == 0);
  12962. DUK_UNREF(ret);
  12963. return 1;
  12964. }
  12965. DUK_INTERNAL duk_int_t duk_pcall_method_flags(duk_hthread *thr, duk_idx_t nargs, duk_small_uint_t call_flags) {
  12966. duk__pcall_method_args args;
  12967. DUK_ASSERT_API_ENTRY(thr);
  12968. args.nargs = nargs;
  12969. if (DUK_UNLIKELY(nargs < 0)) {
  12970. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  12971. DUK_WO_NORETURN(return DUK_EXEC_ERROR;);
  12972. }
  12973. args.call_flags = call_flags;
  12974. return duk_safe_call(thr, duk__pcall_method_raw, (void *) &args /*udata*/, nargs + 2 /*nargs*/, 1 /*nrets*/);
  12975. }
  12976. DUK_EXTERNAL duk_int_t duk_pcall_method(duk_hthread *thr, duk_idx_t nargs) {
  12977. DUK_ASSERT_API_ENTRY(thr);
  12978. return duk_pcall_method_flags(thr, nargs, 0);
  12979. }
  12980. DUK_LOCAL duk_ret_t duk__pcall_prop_raw(duk_hthread *thr, void *udata) {
  12981. duk__pcall_prop_args *args;
  12982. duk_idx_t obj_idx;
  12983. duk_int_t ret;
  12984. DUK_ASSERT_CTX_VALID(thr);
  12985. DUK_ASSERT(udata != NULL);
  12986. args = (duk__pcall_prop_args *) udata;
  12987. obj_idx = duk_require_normalize_index(thr, args->obj_idx); /* make absolute */
  12988. duk__call_prop_prep_stack(thr, obj_idx, args->nargs);
  12989. ret = duk_handle_call_unprotected_nargs(thr, args->nargs, args->call_flags);
  12990. DUK_ASSERT(ret == 0);
  12991. DUK_UNREF(ret);
  12992. return 1;
  12993. }
  12994. DUK_EXTERNAL duk_int_t duk_pcall_prop(duk_hthread *thr, duk_idx_t obj_idx, duk_idx_t nargs) {
  12995. duk__pcall_prop_args args;
  12996. DUK_ASSERT_API_ENTRY(thr);
  12997. args.obj_idx = obj_idx;
  12998. args.nargs = nargs;
  12999. if (DUK_UNLIKELY(nargs < 0)) {
  13000. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  13001. DUK_WO_NORETURN(return DUK_EXEC_ERROR;);
  13002. }
  13003. args.call_flags = 0;
  13004. return duk_safe_call(thr, duk__pcall_prop_raw, (void *) &args /*udata*/, nargs + 1 /*nargs*/, 1 /*nrets*/);
  13005. }
  13006. DUK_EXTERNAL duk_int_t duk_safe_call(duk_hthread *thr, duk_safe_call_function func, void *udata, duk_idx_t nargs, duk_idx_t nrets) {
  13007. duk_int_t rc;
  13008. DUK_ASSERT_API_ENTRY(thr);
  13009. /* nargs condition; fail if: top - bottom < nargs
  13010. * <=> top < bottom + nargs
  13011. * nrets condition; fail if: end - (top - nargs) < nrets
  13012. * <=> end - top + nargs < nrets
  13013. * <=> end + nargs < top + nrets
  13014. */
  13015. /* XXX: check for any reserve? */
  13016. if (DUK_UNLIKELY((nargs | nrets) < 0 || /* nargs < 0 || nrets < 0; OR sign bits */
  13017. thr->valstack_top < thr->valstack_bottom + nargs || /* nargs too large compared to top */
  13018. thr->valstack_end + nargs < thr->valstack_top + nrets)) { /* nrets too large compared to reserve */
  13019. DUK_D(DUK_DPRINT("not enough stack reserve for safe call or invalid arguments: "
  13020. "nargs=%ld < 0 (?), nrets=%ld < 0 (?), top=%ld < bottom=%ld + nargs=%ld (?), "
  13021. "end=%ld + nargs=%ld < top=%ld + nrets=%ld (?)",
  13022. (long) nargs,
  13023. (long) nrets,
  13024. (long) (thr->valstack_top - thr->valstack),
  13025. (long) (thr->valstack_bottom - thr->valstack),
  13026. (long) nargs,
  13027. (long) (thr->valstack_end - thr->valstack),
  13028. (long) nargs,
  13029. (long) (thr->valstack_top - thr->valstack),
  13030. (long) nrets));
  13031. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  13032. DUK_WO_NORETURN(return DUK_EXEC_ERROR;);
  13033. }
  13034. rc = duk_handle_safe_call(thr, /* thread */
  13035. func, /* func */
  13036. udata, /* udata */
  13037. nargs, /* num_stack_args */
  13038. nrets); /* num_stack_res */
  13039. return rc;
  13040. }
  13041. DUK_EXTERNAL void duk_new(duk_hthread *thr, duk_idx_t nargs) {
  13042. duk_idx_t idx_func;
  13043. DUK_ASSERT_API_ENTRY(thr);
  13044. idx_func = duk__call_get_idx_func(thr, nargs, 1);
  13045. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  13046. duk_push_object(thr); /* default instance; internal proto updated by call handling */
  13047. duk_insert(thr, idx_func + 1);
  13048. duk_handle_call_unprotected(thr, idx_func, DUK_CALL_FLAG_CONSTRUCT);
  13049. }
  13050. DUK_LOCAL duk_ret_t duk__pnew_helper(duk_hthread *thr, void *udata) {
  13051. duk_idx_t nargs;
  13052. DUK_ASSERT(udata != NULL);
  13053. nargs = *((duk_idx_t *) udata);
  13054. duk_new(thr, nargs);
  13055. return 1;
  13056. }
  13057. DUK_EXTERNAL duk_int_t duk_pnew(duk_hthread *thr, duk_idx_t nargs) {
  13058. duk_int_t rc;
  13059. DUK_ASSERT_API_ENTRY(thr);
  13060. /* For now, just use duk_safe_call() to wrap duk_new(). We can't
  13061. * simply use a protected duk_handle_call() because pushing the
  13062. * default instance might throw.
  13063. */
  13064. if (DUK_UNLIKELY(nargs < 0)) {
  13065. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  13066. DUK_WO_NORETURN(return DUK_EXEC_ERROR;);
  13067. }
  13068. rc = duk_safe_call(thr, duk__pnew_helper, (void *) &nargs /*udata*/, nargs + 1 /*nargs*/, 1 /*nrets*/);
  13069. return rc;
  13070. }
  13071. DUK_EXTERNAL duk_bool_t duk_is_constructor_call(duk_hthread *thr) {
  13072. duk_activation *act;
  13073. DUK_ASSERT_API_ENTRY(thr);
  13074. act = thr->callstack_curr;
  13075. if (act != NULL) {
  13076. return ((act->flags & DUK_ACT_FLAG_CONSTRUCT) != 0 ? 1 : 0);
  13077. }
  13078. return 0;
  13079. }
  13080. /* XXX: Make this obsolete by adding a function flag for rejecting a
  13081. * non-constructor call automatically?
  13082. */
  13083. DUK_INTERNAL void duk_require_constructor_call(duk_hthread *thr) {
  13084. DUK_ASSERT_API_ENTRY(thr);
  13085. if (!duk_is_constructor_call(thr)) {
  13086. DUK_ERROR_TYPE(thr, DUK_STR_CONSTRUCT_ONLY);
  13087. DUK_WO_NORETURN(return;);
  13088. }
  13089. }
  13090. DUK_EXTERNAL duk_bool_t duk_is_strict_call(duk_hthread *thr) {
  13091. duk_activation *act;
  13092. /* For user code this could just return 1 (strict) always
  13093. * because all Duktape/C functions are considered strict,
  13094. * and strict is also the default when nothing is running.
  13095. * However, Duktape may call this function internally when
  13096. * the current activation is an ECMAScript function, so
  13097. * this cannot be replaced by a 'return 1' without fixing
  13098. * the internal call sites.
  13099. */
  13100. DUK_ASSERT_API_ENTRY(thr);
  13101. act = thr->callstack_curr;
  13102. if (act != NULL) {
  13103. return ((act->flags & DUK_ACT_FLAG_STRICT) != 0 ? 1 : 0);
  13104. } else {
  13105. /* Strict by default. */
  13106. return 1;
  13107. }
  13108. }
  13109. /*
  13110. * Duktape/C function magic
  13111. */
  13112. DUK_EXTERNAL duk_int_t duk_get_current_magic(duk_hthread *thr) {
  13113. duk_activation *act;
  13114. duk_hobject *func;
  13115. DUK_ASSERT_API_ENTRY(thr);
  13116. act = thr->callstack_curr;
  13117. if (act) {
  13118. func = DUK_ACT_GET_FUNC(act);
  13119. if (!func) {
  13120. duk_tval *tv = &act->tv_func;
  13121. duk_small_uint_t lf_flags;
  13122. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  13123. return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  13124. }
  13125. DUK_ASSERT(func != NULL);
  13126. if (DUK_HOBJECT_IS_NATFUNC(func)) {
  13127. duk_hnatfunc *nf = (duk_hnatfunc *) func;
  13128. return (duk_int_t) nf->magic;
  13129. }
  13130. }
  13131. return 0;
  13132. }
  13133. DUK_EXTERNAL duk_int_t duk_get_magic(duk_hthread *thr, duk_idx_t idx) {
  13134. duk_tval *tv;
  13135. duk_hobject *h;
  13136. DUK_ASSERT_API_ENTRY(thr);
  13137. tv = duk_require_tval(thr, idx);
  13138. if (DUK_TVAL_IS_OBJECT(tv)) {
  13139. h = DUK_TVAL_GET_OBJECT(tv);
  13140. DUK_ASSERT(h != NULL);
  13141. if (!DUK_HOBJECT_HAS_NATFUNC(h)) {
  13142. goto type_error;
  13143. }
  13144. return (duk_int_t) ((duk_hnatfunc *) h)->magic;
  13145. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  13146. duk_small_uint_t lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  13147. return (duk_int_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  13148. }
  13149. /* fall through */
  13150. type_error:
  13151. DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE);
  13152. DUK_WO_NORETURN(return 0;);
  13153. }
  13154. DUK_EXTERNAL void duk_set_magic(duk_hthread *thr, duk_idx_t idx, duk_int_t magic) {
  13155. duk_hnatfunc *nf;
  13156. DUK_ASSERT_API_ENTRY(thr);
  13157. nf = duk_require_hnatfunc(thr, idx);
  13158. DUK_ASSERT(nf != NULL);
  13159. nf->magic = (duk_int16_t) magic;
  13160. }
  13161. /*
  13162. * Misc helpers
  13163. */
  13164. /* Resolve a bound function on value stack top to a non-bound target
  13165. * (leave other values as is).
  13166. */
  13167. DUK_INTERNAL void duk_resolve_nonbound_function(duk_hthread *thr) {
  13168. duk_tval *tv;
  13169. DUK_ASSERT_HTHREAD_VALID(thr);
  13170. tv = DUK_GET_TVAL_NEGIDX(thr, -1);
  13171. if (DUK_TVAL_IS_OBJECT(tv)) {
  13172. duk_hobject *h;
  13173. h = DUK_TVAL_GET_OBJECT(tv);
  13174. DUK_ASSERT(h != NULL);
  13175. if (DUK_HOBJECT_HAS_BOUNDFUNC(h)) {
  13176. duk_push_tval(thr, &((duk_hboundfunc *) (void *) h)->target);
  13177. duk_replace(thr, -2);
  13178. #if 0
  13179. DUK_TVAL_SET_TVAL(tv, &((duk_hboundfunc *) h)->target);
  13180. DUK_TVAL_INCREF(thr, tv);
  13181. DUK_HOBJECT_DECREF_NORZ(thr, h);
  13182. #endif
  13183. /* Rely on Function.prototype.bind() on never creating a bound
  13184. * function whose target is not proper. This is now safe
  13185. * because the target is not even an internal property but a
  13186. * struct member.
  13187. */
  13188. DUK_ASSERT(duk_is_lightfunc(thr, -1) || duk_is_callable(thr, -1));
  13189. }
  13190. }
  13191. /* Lightfuncs cannot be bound but are always callable and
  13192. * constructable.
  13193. */
  13194. }
  13195. #line 1 "duk_api_codec.c"
  13196. /*
  13197. * Encoding and decoding basic formats: hex, base64.
  13198. *
  13199. * These are in-place operations which may allow an optimized implementation.
  13200. *
  13201. * Base-64: https://tools.ietf.org/html/rfc4648#section-4
  13202. */
  13203. /* #include duk_internal.h -> already included */
  13204. /*
  13205. * Misc helpers
  13206. */
  13207. /* Shared handling for encode/decode argument. Fast path handling for
  13208. * buffer and string values because they're the most common. In particular,
  13209. * avoid creating a temporary string or buffer when possible.
  13210. */
  13211. DUK_LOCAL const duk_uint8_t *duk__prep_codec_arg(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
  13212. void *ptr;
  13213. duk_bool_t isbuffer;
  13214. DUK_ASSERT(duk_is_valid_index(thr, idx)); /* checked by caller */
  13215. /* XXX: with def_ptr set to a stack related pointer, isbuffer could
  13216. * be removed from the helper?
  13217. */
  13218. ptr = duk_get_buffer_data_raw(thr, idx, out_len, NULL /*def_ptr*/, 0 /*def_size*/, 0 /*throw_flag*/, &isbuffer);
  13219. if (isbuffer) {
  13220. DUK_ASSERT(*out_len == 0 || ptr != NULL);
  13221. return (const duk_uint8_t *) ptr;
  13222. }
  13223. return (const duk_uint8_t *) duk_to_lstring(thr, idx, out_len);
  13224. }
  13225. /*
  13226. * Base64
  13227. */
  13228. #if defined(DUK_USE_BASE64_SUPPORT)
  13229. /* Bytes emitted for number of padding characters in range [0,4]. */
  13230. DUK_LOCAL const duk_int8_t duk__base64_decode_nequal_step[5] = {
  13231. 3, /* #### -> 24 bits, emit 3 bytes */
  13232. 2, /* ###= -> 18 bits, emit 2 bytes */
  13233. 1, /* ##== -> 12 bits, emit 1 byte */
  13234. -1, /* #=== -> 6 bits, error */
  13235. 0, /* ==== -> 0 bits, emit 0 bytes */
  13236. };
  13237. #if defined(DUK_USE_BASE64_FASTPATH)
  13238. DUK_LOCAL const duk_uint8_t duk__base64_enctab_fast[64] = {
  13239. 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U, 0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU, 0x50U, /* A...P */
  13240. 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U, 0x58U, 0x59U, 0x5aU, 0x61U, 0x62U, 0x63U, 0x64U, 0x65U, 0x66U, /* Q...f */
  13241. 0x67U, 0x68U, 0x69U, 0x6aU, 0x6bU, 0x6cU, 0x6dU, 0x6eU, 0x6fU, 0x70U, 0x71U, 0x72U, 0x73U, 0x74U, 0x75U, 0x76U, /* g...v */
  13242. 0x77U, 0x78U, 0x79U, 0x7aU, 0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U, 0x38U, 0x39U, 0x2bU, 0x2fU /* w.../ */
  13243. };
  13244. #endif /* DUK_USE_BASE64_FASTPATH */
  13245. #if defined(DUK_USE_BASE64_FASTPATH)
  13246. /* Decode table for one byte of input:
  13247. * -1 = allowed whitespace
  13248. * -2 = padding
  13249. * -3 = error
  13250. * 0...63 decoded bytes
  13251. */
  13252. DUK_LOCAL const duk_int8_t duk__base64_dectab_fast[256] = {
  13253. -3, -3, -3, -3, -3, -3, -3, -3, -3, -1, -1, -3, -3, -1, -3, -3, /* 0x00...0x0f */
  13254. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0x10...0x1f */
  13255. -1, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, 62, -3, -3, -3, 63, /* 0x20...0x2f */
  13256. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -3, -3, -3, -2, -3, -3, /* 0x30...0x3f */
  13257. -3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40...0x4f */
  13258. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -3, -3, -3, -3, -3, /* 0x50...0x5f */
  13259. -3, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60...0x6f */
  13260. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -3, -3, -3, -3, -3, /* 0x70...0x7f */
  13261. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0x80...0x8f */
  13262. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0x90...0x9f */
  13263. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0xa0...0xaf */
  13264. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0xb0...0xbf */
  13265. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0xc0...0xcf */
  13266. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0xd0...0xdf */
  13267. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, /* 0xe0...0xef */
  13268. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3 /* 0xf0...0xff */
  13269. };
  13270. #endif /* DUK_USE_BASE64_FASTPATH */
  13271. #if defined(DUK_USE_BASE64_FASTPATH)
  13272. DUK_LOCAL DUK_ALWAYS_INLINE void duk__base64_encode_fast_3(const duk_uint8_t *src, duk_uint8_t *dst) {
  13273. duk_uint_t t;
  13274. t = (duk_uint_t) src[0];
  13275. t = (t << 8) + (duk_uint_t) src[1];
  13276. t = (t << 8) + (duk_uint_t) src[2];
  13277. dst[0] = duk__base64_enctab_fast[t >> 18];
  13278. dst[1] = duk__base64_enctab_fast[(t >> 12) & 0x3fU];
  13279. dst[2] = duk__base64_enctab_fast[(t >> 6) & 0x3fU];
  13280. dst[3] = duk__base64_enctab_fast[t & 0x3fU];
  13281. #if 0
  13282. /* Tested: not faster on x64, most likely due to aliasing between
  13283. * output and input index computation.
  13284. */
  13285. /* aaaaaabb bbbbcccc ccdddddd */
  13286. dst[0] = duk__base64_enctab_fast[(src[0] >> 2) & 0x3fU];
  13287. dst[1] = duk__base64_enctab_fast[((src[0] << 4) & 0x30U) | ((src[1] >> 4) & 0x0fU)];
  13288. dst[2] = duk__base64_enctab_fast[((src[1] << 2) & 0x3fU) | ((src[2] >> 6) & 0x03U)];
  13289. dst[3] = duk__base64_enctab_fast[src[2] & 0x3fU];
  13290. #endif
  13291. }
  13292. DUK_LOCAL DUK_ALWAYS_INLINE void duk__base64_encode_fast_2(const duk_uint8_t *src, duk_uint8_t *dst) {
  13293. duk_uint_t t;
  13294. t = (duk_uint_t) src[0];
  13295. t = (t << 8) + (duk_uint_t) src[1];
  13296. dst[0] = duk__base64_enctab_fast[t >> 10]; /* XXXXXX-- -------- */
  13297. dst[1] = duk__base64_enctab_fast[(t >> 4) & 0x3fU]; /* ------XX XXXX---- */
  13298. dst[2] = duk__base64_enctab_fast[(t << 2) & 0x3fU]; /* -------- ----XXXX */
  13299. dst[3] = DUK_ASC_EQUALS;
  13300. }
  13301. DUK_LOCAL DUK_ALWAYS_INLINE void duk__base64_encode_fast_1(const duk_uint8_t *src, duk_uint8_t *dst) {
  13302. duk_uint_t t;
  13303. t = (duk_uint_t) src[0];
  13304. dst[0] = duk__base64_enctab_fast[t >> 2]; /* XXXXXX-- */
  13305. dst[1] = duk__base64_enctab_fast[(t << 4) & 0x3fU]; /* ------XX */
  13306. dst[2] = DUK_ASC_EQUALS;
  13307. dst[3] = DUK_ASC_EQUALS;
  13308. }
  13309. DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst) {
  13310. duk_size_t n;
  13311. const duk_uint8_t *p;
  13312. duk_uint8_t *q;
  13313. n = srclen;
  13314. p = src;
  13315. q = dst;
  13316. if (n >= 16U) {
  13317. /* Fast path, unrolled by 4, allows interleaving. Process
  13318. * 12-byte input chunks which encode to 16-char output chunks.
  13319. * Only enter when at least one block is emitted (avoids div+mul
  13320. * for short inputs too).
  13321. */
  13322. const duk_uint8_t *p_end_fast;
  13323. p_end_fast = p + ((n / 12U) * 12U);
  13324. DUK_ASSERT(p_end_fast >= p + 12);
  13325. do {
  13326. duk__base64_encode_fast_3(p, q);
  13327. duk__base64_encode_fast_3(p + 3, q + 4);
  13328. duk__base64_encode_fast_3(p + 6, q + 8);
  13329. duk__base64_encode_fast_3(p + 9, q + 12);
  13330. p += 12;
  13331. q += 16;
  13332. } while (DUK_LIKELY(p != p_end_fast));
  13333. DUK_ASSERT(src + srclen >= p);
  13334. n = (duk_size_t) (src + srclen - p);
  13335. DUK_ASSERT(n < 12U);
  13336. }
  13337. /* Remainder. */
  13338. while (n >= 3U) {
  13339. duk__base64_encode_fast_3(p, q);
  13340. p += 3;
  13341. q += 4;
  13342. n -= 3U;
  13343. }
  13344. DUK_ASSERT(n == 0U || n == 1U || n == 2U);
  13345. if (n == 1U) {
  13346. duk__base64_encode_fast_1(p, q);
  13347. #if 0 /* Unnecessary. */
  13348. p += 1;
  13349. q += 4;
  13350. n -= 1U;
  13351. #endif
  13352. } else if (n == 2U) {
  13353. duk__base64_encode_fast_2(p, q);
  13354. #if 0 /* Unnecessary. */
  13355. p += 2;
  13356. q += 4;
  13357. n -= 2U;
  13358. #endif
  13359. } else {
  13360. DUK_ASSERT(n == 0U); /* nothing to do */
  13361. ;
  13362. }
  13363. }
  13364. #else /* DUK_USE_BASE64_FASTPATH */
  13365. DUK_LOCAL void duk__base64_encode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst) {
  13366. duk_small_uint_t i, npad;
  13367. duk_uint_t t, x, y;
  13368. const duk_uint8_t *p;
  13369. const duk_uint8_t *p_end;
  13370. duk_uint8_t *q;
  13371. p = src;
  13372. p_end = src + srclen;
  13373. q = dst;
  13374. npad = 0U;
  13375. while (p < p_end) {
  13376. /* Read 3 bytes into 't', padded by zero. */
  13377. t = 0;
  13378. for (i = 0; i < 3; i++) {
  13379. t = t << 8;
  13380. if (p < p_end) {
  13381. t += (duk_uint_t) (*p++);
  13382. } else {
  13383. /* This only happens on the last loop and we're
  13384. * guaranteed to exit on the next loop.
  13385. */
  13386. npad++;
  13387. }
  13388. }
  13389. DUK_ASSERT(npad <= 2U);
  13390. /* Emit 4 encoded characters. If npad > 0, some of the
  13391. * chars will be incorrect (zero bits) but we fix up the
  13392. * padding after the loop. A straightforward 64-byte
  13393. * lookup would be faster and cleaner, but this is shorter.
  13394. */
  13395. for (i = 0; i < 4; i++) {
  13396. x = ((t >> 18) & 0x3fU);
  13397. t = t << 6;
  13398. if (x <= 51U) {
  13399. if (x <= 25) {
  13400. y = x + DUK_ASC_UC_A;
  13401. } else {
  13402. y = x - 26 + DUK_ASC_LC_A;
  13403. }
  13404. } else {
  13405. if (x <= 61U) {
  13406. y = x - 52 + DUK_ASC_0;
  13407. } else if (x == 62) {
  13408. y = DUK_ASC_PLUS;
  13409. } else {
  13410. DUK_ASSERT(x == 63);
  13411. y = DUK_ASC_SLASH;
  13412. }
  13413. }
  13414. *q++ = (duk_uint8_t) y;
  13415. }
  13416. }
  13417. /* Handle padding by rewriting 0-2 bogus characters at the end.
  13418. *
  13419. * Missing bytes npad base64 example
  13420. * 0 0 ####
  13421. * 1 1 ###=
  13422. * 2 2 ##==
  13423. */
  13424. DUK_ASSERT(npad <= 2U);
  13425. while (npad > 0U) {
  13426. *(q - npad) = DUK_ASC_EQUALS;
  13427. npad--;
  13428. }
  13429. }
  13430. #endif /* DUK_USE_BASE64_FASTPATH */
  13431. #if defined(DUK_USE_BASE64_FASTPATH)
  13432. DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst, duk_uint8_t **out_dst_final) {
  13433. duk_int_t x;
  13434. duk_uint_t t;
  13435. duk_small_uint_t n_equal;
  13436. duk_int8_t step;
  13437. const duk_uint8_t *p;
  13438. const duk_uint8_t *p_end;
  13439. const duk_uint8_t *p_end_safe;
  13440. duk_uint8_t *q;
  13441. p = src;
  13442. p_end = src + srclen;
  13443. p_end_safe = p_end - 8; /* If 'src <= src_end_safe', safe to read 8 bytes. */
  13444. q = dst;
  13445. /* Alternate between a fast path which processes clean groups with no
  13446. * padding or whitespace, and a slow path which processes one arbitrary
  13447. * group and then re-enters the fast path. This handles e.g. base64
  13448. * with newlines reasonably well because the majority of a line is in
  13449. * the fast path.
  13450. */
  13451. for (;;) {
  13452. /* Fast path, on each loop handle two 4-char input groups.
  13453. * If both are clean, emit 6 bytes and continue. If first
  13454. * is clean, emit 3 bytes and drop out; otherwise emit
  13455. * nothing and drop out. This approach could be extended to
  13456. * more groups per loop, but for inputs with e.g. periodic
  13457. * newlines (which are common) it might not be an improvement.
  13458. */
  13459. while (DUK_LIKELY(p <= p_end_safe)) {
  13460. duk_int_t t1, t2;
  13461. /* The lookup byte is intentionally sign extended to
  13462. * (at least) 32 bits and then ORed. This ensures
  13463. * that is at least 1 byte is negative, the highest
  13464. * bit of the accumulator will be set at the end and
  13465. * we don't need to check every byte.
  13466. *
  13467. * Read all input bytes first before writing output
  13468. * bytes to minimize aliasing.
  13469. */
  13470. DUK_DDD(DUK_DDDPRINT("fast loop: p=%p, p_end_safe=%p, p_end=%p",
  13471. (const void *) p, (const void *) p_end_safe, (const void *) p_end));
  13472. t1 = (duk_int_t) duk__base64_dectab_fast[p[0]];
  13473. t1 = (duk_int_t) ((duk_uint_t) t1 << 6) | (duk_int_t) duk__base64_dectab_fast[p[1]];
  13474. t1 = (duk_int_t) ((duk_uint_t) t1 << 6) | (duk_int_t) duk__base64_dectab_fast[p[2]];
  13475. t1 = (duk_int_t) ((duk_uint_t) t1 << 6) | (duk_int_t) duk__base64_dectab_fast[p[3]];
  13476. t2 = (duk_int_t) duk__base64_dectab_fast[p[4]];
  13477. t2 = (duk_int_t) ((duk_uint_t) t2 << 6) | (duk_int_t) duk__base64_dectab_fast[p[5]];
  13478. t2 = (duk_int_t) ((duk_uint_t) t2 << 6) | (duk_int_t) duk__base64_dectab_fast[p[6]];
  13479. t2 = (duk_int_t) ((duk_uint_t) t2 << 6) | (duk_int_t) duk__base64_dectab_fast[p[7]];
  13480. q[0] = (duk_uint8_t) (((duk_uint_t) t1 >> 16) & 0xffU);
  13481. q[1] = (duk_uint8_t) (((duk_uint_t) t1 >> 8) & 0xffU);
  13482. q[2] = (duk_uint8_t) ((duk_uint_t) t1 & 0xffU);
  13483. q[3] = (duk_uint8_t) (((duk_uint_t) t2 >> 16) & 0xffU);
  13484. q[4] = (duk_uint8_t) (((duk_uint_t) t2 >> 8) & 0xffU);
  13485. q[5] = (duk_uint8_t) ((duk_uint_t) t2 & 0xffU);
  13486. /* Optimistic check using one branch. */
  13487. if (DUK_LIKELY((t1 | t2) >= 0)) {
  13488. p += 8;
  13489. q += 6;
  13490. } else if (t1 >= 0) {
  13491. DUK_DDD(DUK_DDDPRINT("fast loop first group was clean, second was not, process one slow path group"));
  13492. DUK_ASSERT(t2 < 0);
  13493. p += 4;
  13494. q += 3;
  13495. break;
  13496. } else {
  13497. DUK_DDD(DUK_DDDPRINT("fast loop first group was not clean, second does not matter, process one slow path group"));
  13498. DUK_ASSERT(t1 < 0);
  13499. break;
  13500. }
  13501. } /* fast path */
  13502. /* Slow path step 1: try to scan a 4-character encoded group,
  13503. * end-of-input, or start-of-padding. We exit with:
  13504. * 1. n_chars == 4: full group, no padding, no end-of-input.
  13505. * 2. n_chars < 4: partial group (may also be 0), encountered
  13506. * padding or end of input.
  13507. *
  13508. * The accumulator is initialized to 1; this allows us to detect
  13509. * a full group by comparing >= 0x1000000 without an extra
  13510. * counter variable.
  13511. */
  13512. t = 1UL;
  13513. for (;;) {
  13514. DUK_DDD(DUK_DDDPRINT("slow loop: p=%p, p_end=%p, t=%lu",
  13515. (const void *) p, (const void *) p_end, (unsigned long) t));
  13516. if (DUK_LIKELY(p < p_end)) {
  13517. x = duk__base64_dectab_fast[*p++];
  13518. if (DUK_LIKELY(x >= 0)) {
  13519. DUK_ASSERT(x >= 0 && x <= 63);
  13520. t = (t << 6) + (duk_uint_t) x;
  13521. if (t >= 0x1000000UL) {
  13522. break;
  13523. }
  13524. } else if (x == -1) {
  13525. continue; /* allowed ascii whitespace */
  13526. } else if (x == -2) {
  13527. p--;
  13528. break; /* start of padding */
  13529. } else {
  13530. DUK_ASSERT(x == -3);
  13531. goto decode_error;
  13532. }
  13533. } else {
  13534. break; /* end of input */
  13535. }
  13536. } /* slow path step 1 */
  13537. /* Complete the padding by simulating pad characters,
  13538. * regardless of actual input padding chars.
  13539. */
  13540. n_equal = 0;
  13541. while (t < 0x1000000UL) {
  13542. t = (t << 6) + 0U;
  13543. n_equal++;
  13544. }
  13545. /* Slow path step 2: deal with full/partial group, padding,
  13546. * etc. Note that for num chars in [0,3] we intentionally emit
  13547. * 3 bytes but don't step forward that much, buffer space is
  13548. * guaranteed in setup.
  13549. *
  13550. * num chars:
  13551. * 0 #### no output (= step 0)
  13552. * 1 #=== reject, 6 bits of data
  13553. * 2 ##== 12 bits of data, output 1 byte (= step 1)
  13554. * 3 ###= 18 bits of data, output 2 bytes (= step 2)
  13555. * 4 #### 24 bits of data, output 3 bytes (= step 3)
  13556. */
  13557. q[0] = (duk_uint8_t) ((t >> 16) & 0xffU);
  13558. q[1] = (duk_uint8_t) ((t >> 8) & 0xffU);
  13559. q[2] = (duk_uint8_t) (t & 0xffU);
  13560. DUK_ASSERT(n_equal <= 4);
  13561. step = duk__base64_decode_nequal_step[n_equal];
  13562. if (DUK_UNLIKELY(step < 0)) {
  13563. goto decode_error;
  13564. }
  13565. q += step;
  13566. /* Slow path step 3: read and ignore padding and whitespace
  13567. * until (a) next non-padding and non-whitespace character
  13568. * after which we resume the fast path, or (b) end of input.
  13569. * This allows us to accept missing, partial, full, and extra
  13570. * padding cases uniformly. We also support concatenated
  13571. * base-64 documents because we resume scanning afterwards.
  13572. *
  13573. * Note that to support concatenated documents well, the '='
  13574. * padding found inside the input must also allow for 'extra'
  13575. * padding. For example, 'Zm===' decodes to 'f' and has one
  13576. * extra padding char. So, 'Zm===Zm' should decode 'ff', even
  13577. * though the standard break-up would be 'Zm==' + '=Zm' which
  13578. * doesn't make sense.
  13579. *
  13580. * We also accept prepended padding like '==Zm9', because it
  13581. * is equivalent to an empty document with extra padding ('==')
  13582. * followed by a valid document.
  13583. */
  13584. for (;;) {
  13585. if (DUK_UNLIKELY(p >= p_end)) {
  13586. goto done;
  13587. }
  13588. x = duk__base64_dectab_fast[*p++];
  13589. if (x == -1 || x == -2) {
  13590. ; /* padding or whitespace, keep eating */
  13591. } else {
  13592. p--;
  13593. break; /* backtrack and go back to fast path, even for -1 */
  13594. }
  13595. } /* slow path step 3 */
  13596. } /* outer fast+slow path loop */
  13597. done:
  13598. DUK_DDD(DUK_DDDPRINT("done; p=%p, p_end=%p",
  13599. (const void *) p, (const void *) p_end));
  13600. DUK_ASSERT(p == p_end);
  13601. *out_dst_final = q;
  13602. return 1;
  13603. decode_error:
  13604. return 0;
  13605. }
  13606. #else /* DUK_USE_BASE64_FASTPATH */
  13607. DUK_LOCAL duk_bool_t duk__base64_decode_helper(const duk_uint8_t *src, duk_size_t srclen, duk_uint8_t *dst, duk_uint8_t **out_dst_final) {
  13608. duk_uint_t t, x;
  13609. duk_int_t y;
  13610. duk_int8_t step;
  13611. const duk_uint8_t *p;
  13612. const duk_uint8_t *p_end;
  13613. duk_uint8_t *q;
  13614. /* 0x09, 0x0a, or 0x0d */
  13615. duk_uint32_t mask_white = (1U << 9) | (1U << 10) | (1U << 13);
  13616. /* 't' tracks progress of the decoded group:
  13617. *
  13618. * t == 1 no valid chars yet
  13619. * t >= 0x40 1x6 = 6 bits shifted in
  13620. * t >= 0x1000 2x6 = 12 bits shifted in
  13621. * t >= 0x40000 3x6 = 18 bits shifted in
  13622. * t >= 0x1000000 4x6 = 24 bits shifted in
  13623. *
  13624. * By initializing t=1 there's no need for a separate counter for
  13625. * the number of characters found so far.
  13626. */
  13627. p = src;
  13628. p_end = src + srclen;
  13629. q = dst;
  13630. t = 1UL;
  13631. for (;;) {
  13632. duk_small_uint_t n_equal;
  13633. DUK_ASSERT(t >= 1U);
  13634. if (p >= p_end) {
  13635. /* End of input: if input exists, treat like
  13636. * start of padding, finish the block, then
  13637. * re-enter here to see we're done.
  13638. */
  13639. if (t == 1U) {
  13640. break;
  13641. } else {
  13642. goto simulate_padding;
  13643. }
  13644. }
  13645. x = *p++;
  13646. if (x >= 0x41U) {
  13647. /* Valid: a-z and A-Z. */
  13648. DUK_ASSERT(x >= 0x41U && x <= 0xffU);
  13649. if (x >= 0x61U && x <= 0x7aU) {
  13650. y = (duk_int_t) x - 0x61 + 26;
  13651. } else if (x <= 0x5aU) {
  13652. y = (duk_int_t) x - 0x41;
  13653. } else {
  13654. goto decode_error;
  13655. }
  13656. } else if (x >= 0x30U) {
  13657. /* Valid: 0-9 and =. */
  13658. DUK_ASSERT(x >= 0x30U && x <= 0x40U);
  13659. if (x <= 0x39U) {
  13660. y = (duk_int_t) x - 0x30 + 52;
  13661. } else if (x == 0x3dU) {
  13662. /* Skip padding and whitespace unless we're in the
  13663. * middle of a block. Otherwise complete group by
  13664. * simulating shifting in the correct padding.
  13665. */
  13666. if (t == 1U) {
  13667. continue;
  13668. }
  13669. goto simulate_padding;
  13670. } else {
  13671. goto decode_error;
  13672. }
  13673. } else if (x >= 0x20U) {
  13674. /* Valid: +, /, and 0x20 whitespace. */
  13675. DUK_ASSERT(x >= 0x20U && x <= 0x2fU);
  13676. if (x == 0x2bU) {
  13677. y = 62;
  13678. } else if (x == 0x2fU) {
  13679. y = 63;
  13680. } else if (x == 0x20U) {
  13681. continue;
  13682. } else {
  13683. goto decode_error;
  13684. }
  13685. } else {
  13686. /* Valid: whitespace. */
  13687. duk_uint32_t m;
  13688. DUK_ASSERT(x < 0x20U); /* 0x00 to 0x1f */
  13689. m = (1U << x);
  13690. if (mask_white & m) {
  13691. /* Allow basic ASCII whitespace. */
  13692. continue;
  13693. } else {
  13694. goto decode_error;
  13695. }
  13696. }
  13697. DUK_ASSERT(y >= 0 && y <= 63);
  13698. t = (t << 6) + (duk_uint_t) y;
  13699. if (t < 0x1000000UL) {
  13700. continue;
  13701. }
  13702. /* fall through; no padding will be added */
  13703. simulate_padding:
  13704. n_equal = 0;
  13705. while (t < 0x1000000UL) {
  13706. t = (t << 6) + 0U;
  13707. n_equal++;
  13708. }
  13709. /* Output 3 bytes from 't' and advance as needed. */
  13710. q[0] = (duk_uint8_t) ((t >> 16) & 0xffU);
  13711. q[1] = (duk_uint8_t) ((t >> 8) & 0xffU);
  13712. q[2] = (duk_uint8_t) (t & 0xffU);
  13713. DUK_ASSERT(n_equal <= 4U);
  13714. step = duk__base64_decode_nequal_step[n_equal];
  13715. if (step < 0) {
  13716. goto decode_error;
  13717. }
  13718. q += step;
  13719. /* Re-enter loop. The actual padding characters are skipped
  13720. * by the main loop. This handles cases like missing, partial,
  13721. * full, and extra padding, and allows parsing of concatenated
  13722. * documents (with extra padding) like: Zm===Zm. Also extra
  13723. * prepended padding is accepted: ===Zm9v.
  13724. */
  13725. t = 1U;
  13726. }
  13727. DUK_ASSERT(t == 1UL);
  13728. *out_dst_final = q;
  13729. return 1;
  13730. decode_error:
  13731. return 0;
  13732. }
  13733. #endif /* DUK_USE_BASE64_FASTPATH */
  13734. DUK_EXTERNAL const char *duk_base64_encode(duk_hthread *thr, duk_idx_t idx) {
  13735. const duk_uint8_t *src;
  13736. duk_size_t srclen;
  13737. duk_size_t dstlen;
  13738. duk_uint8_t *dst;
  13739. const char *ret;
  13740. DUK_ASSERT_API_ENTRY(thr);
  13741. idx = duk_require_normalize_index(thr, idx);
  13742. src = duk__prep_codec_arg(thr, idx, &srclen);
  13743. /* Note: for srclen=0, src may be NULL */
  13744. /* Compute exact output length. Computation must not wrap; this
  13745. * limit works for 32-bit size_t:
  13746. * >>> srclen = 3221225469
  13747. * >>> '%x' % ((srclen + 2) / 3 * 4)
  13748. * 'fffffffc'
  13749. */
  13750. if (srclen > 3221225469UL) {
  13751. goto type_error;
  13752. }
  13753. dstlen = (srclen + 2U) / 3U * 4U;
  13754. dst = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, dstlen);
  13755. duk__base64_encode_helper((const duk_uint8_t *) src, srclen, dst);
  13756. ret = duk_buffer_to_string(thr, -1); /* Safe, result is ASCII. */
  13757. duk_replace(thr, idx);
  13758. return ret;
  13759. type_error:
  13760. DUK_ERROR_TYPE(thr, DUK_STR_BASE64_ENCODE_FAILED);
  13761. DUK_WO_NORETURN(return NULL;);
  13762. }
  13763. DUK_EXTERNAL void duk_base64_decode(duk_hthread *thr, duk_idx_t idx) {
  13764. const duk_uint8_t *src;
  13765. duk_size_t srclen;
  13766. duk_size_t dstlen;
  13767. duk_uint8_t *dst;
  13768. duk_uint8_t *dst_final;
  13769. DUK_ASSERT_API_ENTRY(thr);
  13770. idx = duk_require_normalize_index(thr, idx);
  13771. src = duk__prep_codec_arg(thr, idx, &srclen);
  13772. /* Round up and add safety margin. Avoid addition before division to
  13773. * avoid possibility of wrapping. Margin includes +3 for rounding up,
  13774. * and +3 for one extra group: the decoder may emit and then backtrack
  13775. * a full group (3 bytes) from zero-sized input for technical reasons.
  13776. * Similarly, 'xx' may ecause 1+3 = bytes to be emitted and then
  13777. * backtracked.
  13778. */
  13779. dstlen = (srclen / 4) * 3 + 6; /* upper limit, assuming no whitespace etc */
  13780. dst = (duk_uint8_t *) duk_push_dynamic_buffer(thr, dstlen);
  13781. /* Note: for dstlen=0, dst may be NULL */
  13782. if (!duk__base64_decode_helper((const duk_uint8_t *) src, srclen, dst, &dst_final)) {
  13783. goto type_error;
  13784. }
  13785. /* XXX: convert to fixed buffer? */
  13786. (void) duk_resize_buffer(thr, -1, (duk_size_t) (dst_final - dst));
  13787. duk_replace(thr, idx);
  13788. return;
  13789. type_error:
  13790. DUK_ERROR_TYPE(thr, DUK_STR_BASE64_DECODE_FAILED);
  13791. DUK_WO_NORETURN(return;);
  13792. }
  13793. #else /* DUK_USE_BASE64_SUPPORT */
  13794. DUK_EXTERNAL const char *duk_base64_encode(duk_hthread *thr, duk_idx_t idx) {
  13795. DUK_UNREF(idx);
  13796. DUK_ERROR_UNSUPPORTED(thr);
  13797. DUK_WO_NORETURN(return;);
  13798. }
  13799. DUK_EXTERNAL void duk_base64_decode(duk_hthread *thr, duk_idx_t idx) {
  13800. DUK_UNREF(idx);
  13801. DUK_ERROR_UNSUPPORTED(thr);
  13802. DUK_WO_NORETURN(return;);
  13803. }
  13804. #endif /* DUK_USE_BASE64_SUPPORT */
  13805. /*
  13806. * Hex
  13807. */
  13808. #if defined(DUK_USE_HEX_SUPPORT)
  13809. DUK_EXTERNAL const char *duk_hex_encode(duk_hthread *thr, duk_idx_t idx) {
  13810. const duk_uint8_t *inp;
  13811. duk_size_t len;
  13812. duk_size_t i;
  13813. duk_uint8_t *buf;
  13814. const char *ret;
  13815. #if defined(DUK_USE_HEX_FASTPATH)
  13816. duk_size_t len_safe;
  13817. duk_uint16_t *p16;
  13818. #endif
  13819. DUK_ASSERT_API_ENTRY(thr);
  13820. idx = duk_require_normalize_index(thr, idx);
  13821. inp = duk__prep_codec_arg(thr, idx, &len);
  13822. DUK_ASSERT(inp != NULL || len == 0);
  13823. /* Fixed buffer, no zeroing because we'll fill all the data. */
  13824. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, len * 2);
  13825. DUK_ASSERT(buf != NULL);
  13826. #if defined(DUK_USE_HEX_FASTPATH)
  13827. DUK_ASSERT((((duk_size_t) buf) & 0x01U) == 0); /* pointer is aligned, guaranteed for fixed buffer */
  13828. p16 = (duk_uint16_t *) (void *) buf;
  13829. len_safe = len & ~0x03U;
  13830. for (i = 0; i < len_safe; i += 4) {
  13831. p16[0] = duk_hex_enctab[inp[i]];
  13832. p16[1] = duk_hex_enctab[inp[i + 1]];
  13833. p16[2] = duk_hex_enctab[inp[i + 2]];
  13834. p16[3] = duk_hex_enctab[inp[i + 3]];
  13835. p16 += 4;
  13836. }
  13837. for (; i < len; i++) {
  13838. *p16++ = duk_hex_enctab[inp[i]];
  13839. }
  13840. #else /* DUK_USE_HEX_FASTPATH */
  13841. for (i = 0; i < len; i++) {
  13842. duk_small_uint_t t;
  13843. t = (duk_small_uint_t) inp[i];
  13844. buf[i*2 + 0] = duk_lc_digits[t >> 4];
  13845. buf[i*2 + 1] = duk_lc_digits[t & 0x0f];
  13846. }
  13847. #endif /* DUK_USE_HEX_FASTPATH */
  13848. /* XXX: Using a string return value forces a string intern which is
  13849. * not always necessary. As a rough performance measure, hex encode
  13850. * time for tests/perf/test-hex-encode.js dropped from ~35s to ~15s
  13851. * without string coercion. Change to returning a buffer and let the
  13852. * caller coerce to string if necessary?
  13853. */
  13854. ret = duk_buffer_to_string(thr, -1); /* Safe, result is ASCII. */
  13855. duk_replace(thr, idx);
  13856. return ret;
  13857. }
  13858. DUK_EXTERNAL void duk_hex_decode(duk_hthread *thr, duk_idx_t idx) {
  13859. const duk_uint8_t *inp;
  13860. duk_size_t len;
  13861. duk_size_t i;
  13862. duk_int_t t;
  13863. duk_uint8_t *buf;
  13864. #if defined(DUK_USE_HEX_FASTPATH)
  13865. duk_int_t chk;
  13866. duk_uint8_t *p;
  13867. duk_size_t len_safe;
  13868. #endif
  13869. DUK_ASSERT_API_ENTRY(thr);
  13870. idx = duk_require_normalize_index(thr, idx);
  13871. inp = duk__prep_codec_arg(thr, idx, &len);
  13872. DUK_ASSERT(inp != NULL || len == 0);
  13873. if (len & 0x01) {
  13874. goto type_error;
  13875. }
  13876. /* Fixed buffer, no zeroing because we'll fill all the data. */
  13877. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, len / 2);
  13878. DUK_ASSERT(buf != NULL);
  13879. #if defined(DUK_USE_HEX_FASTPATH)
  13880. p = buf;
  13881. len_safe = len & ~0x07U;
  13882. for (i = 0; i < len_safe; i += 8) {
  13883. t = ((duk_int_t) duk_hex_dectab_shift4[inp[i]]) |
  13884. ((duk_int_t) duk_hex_dectab[inp[i + 1]]);
  13885. chk = t;
  13886. p[0] = (duk_uint8_t) t;
  13887. t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 2]]) |
  13888. ((duk_int_t) duk_hex_dectab[inp[i + 3]]);
  13889. chk |= t;
  13890. p[1] = (duk_uint8_t) t;
  13891. t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 4]]) |
  13892. ((duk_int_t) duk_hex_dectab[inp[i + 5]]);
  13893. chk |= t;
  13894. p[2] = (duk_uint8_t) t;
  13895. t = ((duk_int_t) duk_hex_dectab_shift4[inp[i + 6]]) |
  13896. ((duk_int_t) duk_hex_dectab[inp[i + 7]]);
  13897. chk |= t;
  13898. p[3] = (duk_uint8_t) t;
  13899. p += 4;
  13900. /* Check if any lookup above had a negative result. */
  13901. if (DUK_UNLIKELY(chk < 0)) {
  13902. goto type_error;
  13903. }
  13904. }
  13905. for (; i < len; i += 2) {
  13906. /* First cast to duk_int_t to sign extend, second cast to
  13907. * duk_uint_t to avoid signed left shift, and final cast to
  13908. * duk_int_t result type.
  13909. */
  13910. t = (duk_int_t) ((((duk_uint_t) (duk_int_t) duk_hex_dectab[inp[i]]) << 4U) |
  13911. ((duk_uint_t) (duk_int_t) duk_hex_dectab[inp[i + 1]]));
  13912. if (DUK_UNLIKELY(t < 0)) {
  13913. goto type_error;
  13914. }
  13915. *p++ = (duk_uint8_t) t;
  13916. }
  13917. #else /* DUK_USE_HEX_FASTPATH */
  13918. for (i = 0; i < len; i += 2) {
  13919. /* For invalid characters the value -1 gets extended to
  13920. * at least 16 bits. If either nybble is invalid, the
  13921. * resulting 't' will be < 0.
  13922. */
  13923. t = (duk_int_t) ((((duk_uint_t) (duk_int_t) duk_hex_dectab[inp[i]]) << 4U) |
  13924. ((duk_uint_t) (duk_int_t) duk_hex_dectab[inp[i + 1]]));
  13925. if (DUK_UNLIKELY(t < 0)) {
  13926. goto type_error;
  13927. }
  13928. buf[i >> 1] = (duk_uint8_t) t;
  13929. }
  13930. #endif /* DUK_USE_HEX_FASTPATH */
  13931. duk_replace(thr, idx);
  13932. return;
  13933. type_error:
  13934. DUK_ERROR_TYPE(thr, DUK_STR_HEX_DECODE_FAILED);
  13935. DUK_WO_NORETURN(return;);
  13936. }
  13937. #else /* DUK_USE_HEX_SUPPORT */
  13938. DUK_EXTERNAL const char *duk_hex_encode(duk_hthread *thr, duk_idx_t idx) {
  13939. DUK_UNREF(idx);
  13940. DUK_ERROR_UNSUPPORTED(thr);
  13941. DUK_WO_NORETURN(return;);
  13942. }
  13943. DUK_EXTERNAL void duk_hex_decode(duk_hthread *thr, duk_idx_t idx) {
  13944. DUK_UNREF(idx);
  13945. DUK_ERROR_UNSUPPORTED(thr);
  13946. DUK_WO_NORETURN(return;);
  13947. }
  13948. #endif /* DUK_USE_HEX_SUPPORT */
  13949. /*
  13950. * JSON
  13951. */
  13952. #if defined(DUK_USE_JSON_SUPPORT)
  13953. DUK_EXTERNAL const char *duk_json_encode(duk_hthread *thr, duk_idx_t idx) {
  13954. #if defined(DUK_USE_ASSERTIONS)
  13955. duk_idx_t top_at_entry;
  13956. #endif
  13957. const char *ret;
  13958. DUK_ASSERT_API_ENTRY(thr);
  13959. #if defined(DUK_USE_ASSERTIONS)
  13960. top_at_entry = duk_get_top(thr);
  13961. #endif
  13962. idx = duk_require_normalize_index(thr, idx);
  13963. duk_bi_json_stringify_helper(thr,
  13964. idx /*idx_value*/,
  13965. DUK_INVALID_INDEX /*idx_replacer*/,
  13966. DUK_INVALID_INDEX /*idx_space*/,
  13967. 0 /*flags*/);
  13968. DUK_ASSERT(duk_is_string(thr, -1));
  13969. duk_replace(thr, idx);
  13970. ret = duk_get_string(thr, idx);
  13971. DUK_ASSERT(duk_get_top(thr) == top_at_entry);
  13972. return ret;
  13973. }
  13974. DUK_EXTERNAL void duk_json_decode(duk_hthread *thr, duk_idx_t idx) {
  13975. #if defined(DUK_USE_ASSERTIONS)
  13976. duk_idx_t top_at_entry;
  13977. #endif
  13978. DUK_ASSERT_API_ENTRY(thr);
  13979. #if defined(DUK_USE_ASSERTIONS)
  13980. top_at_entry = duk_get_top(thr);
  13981. #endif
  13982. idx = duk_require_normalize_index(thr, idx);
  13983. duk_bi_json_parse_helper(thr,
  13984. idx /*idx_value*/,
  13985. DUK_INVALID_INDEX /*idx_reviver*/,
  13986. 0 /*flags*/);
  13987. duk_replace(thr, idx);
  13988. DUK_ASSERT(duk_get_top(thr) == top_at_entry);
  13989. }
  13990. #else /* DUK_USE_JSON_SUPPORT */
  13991. DUK_EXTERNAL const char *duk_json_encode(duk_hthread *thr, duk_idx_t idx) {
  13992. DUK_ASSERT_API_ENTRY(thr);
  13993. DUK_UNREF(idx);
  13994. DUK_ERROR_UNSUPPORTED(thr);
  13995. DUK_WO_NORETURN(return NULL;);
  13996. }
  13997. DUK_EXTERNAL void duk_json_decode(duk_hthread *thr, duk_idx_t idx) {
  13998. DUK_ASSERT_API_ENTRY(thr);
  13999. DUK_UNREF(idx);
  14000. DUK_ERROR_UNSUPPORTED(thr);
  14001. DUK_WO_NORETURN(return;);
  14002. }
  14003. #endif /* DUK_USE_JSON_SUPPORT */
  14004. #line 1 "duk_api_compile.c"
  14005. /*
  14006. * Compilation and evaluation
  14007. */
  14008. /* #include duk_internal.h -> already included */
  14009. typedef struct duk__compile_raw_args duk__compile_raw_args;
  14010. struct duk__compile_raw_args {
  14011. duk_size_t src_length; /* should be first on 64-bit platforms */
  14012. const duk_uint8_t *src_buffer;
  14013. duk_uint_t flags;
  14014. };
  14015. /* Eval is just a wrapper now. */
  14016. DUK_EXTERNAL duk_int_t duk_eval_raw(duk_hthread *thr, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) {
  14017. duk_int_t rc;
  14018. DUK_ASSERT_API_ENTRY(thr);
  14019. /* Note: strictness is *not* inherited from the current Duktape/C.
  14020. * This would be confusing because the current strictness state
  14021. * depends on whether we're running inside a Duktape/C activation
  14022. * (= strict mode) or outside of any activation (= non-strict mode).
  14023. * See tests/api/test-eval-strictness.c for more discussion.
  14024. */
  14025. /* [ ... source? filename? ] (depends on flags) */
  14026. rc = duk_compile_raw(thr, src_buffer, src_length, flags | DUK_COMPILE_EVAL); /* may be safe, or non-safe depending on flags */
  14027. /* [ ... closure/error ] */
  14028. if (rc != DUK_EXEC_SUCCESS) {
  14029. rc = DUK_EXEC_ERROR;
  14030. goto got_rc;
  14031. }
  14032. duk_push_global_object(thr); /* explicit 'this' binding, see GH-164 */
  14033. if (flags & DUK_COMPILE_SAFE) {
  14034. rc = duk_pcall_method(thr, 0);
  14035. } else {
  14036. duk_call_method(thr, 0);
  14037. rc = DUK_EXEC_SUCCESS;
  14038. }
  14039. /* [ ... result/error ] */
  14040. got_rc:
  14041. if (flags & DUK_COMPILE_NORESULT) {
  14042. duk_pop(thr);
  14043. }
  14044. return rc;
  14045. }
  14046. /* Helper which can be called both directly and with duk_safe_call(). */
  14047. DUK_LOCAL duk_ret_t duk__do_compile(duk_hthread *thr, void *udata) {
  14048. duk__compile_raw_args *comp_args;
  14049. duk_uint_t flags;
  14050. duk_hcompfunc *h_templ;
  14051. DUK_ASSERT_CTX_VALID(thr);
  14052. DUK_ASSERT(udata != NULL);
  14053. /* Note: strictness is not inherited from the current Duktape/C
  14054. * context. Otherwise it would not be possible to compile
  14055. * non-strict code inside a Duktape/C activation (which is
  14056. * always strict now). See tests/api/test-eval-strictness.c
  14057. * for discussion.
  14058. */
  14059. /* [ ... source? filename? ] (depends on flags) */
  14060. comp_args = (duk__compile_raw_args *) udata;
  14061. flags = comp_args->flags;
  14062. if (flags & DUK_COMPILE_NOFILENAME) {
  14063. /* Automatic filename: 'eval' or 'input'. */
  14064. duk_push_hstring_stridx(thr, (flags & DUK_COMPILE_EVAL) ? DUK_STRIDX_EVAL : DUK_STRIDX_INPUT);
  14065. }
  14066. /* [ ... source? filename ] */
  14067. if (!comp_args->src_buffer) {
  14068. duk_hstring *h_sourcecode;
  14069. h_sourcecode = duk_get_hstring(thr, -2);
  14070. if ((flags & DUK_COMPILE_NOSOURCE) || /* args incorrect */
  14071. (h_sourcecode == NULL)) { /* e.g. duk_push_string_file_raw() pushed undefined */
  14072. DUK_ERROR_TYPE(thr, DUK_STR_NO_SOURCECODE);
  14073. DUK_WO_NORETURN(return 0;);
  14074. }
  14075. DUK_ASSERT(h_sourcecode != NULL);
  14076. comp_args->src_buffer = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode);
  14077. comp_args->src_length = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode);
  14078. }
  14079. DUK_ASSERT(comp_args->src_buffer != NULL);
  14080. if (flags & DUK_COMPILE_FUNCTION) {
  14081. flags |= DUK_COMPILE_EVAL | DUK_COMPILE_FUNCEXPR;
  14082. }
  14083. /* [ ... source? filename ] */
  14084. duk_js_compile(thr, comp_args->src_buffer, comp_args->src_length, flags);
  14085. /* [ ... source? func_template ] */
  14086. if (flags & DUK_COMPILE_NOSOURCE) {
  14087. ;
  14088. } else {
  14089. duk_remove_m2(thr);
  14090. }
  14091. /* [ ... func_template ] */
  14092. h_templ = (duk_hcompfunc *) duk_known_hobject(thr, -1);
  14093. duk_js_push_closure(thr,
  14094. h_templ,
  14095. thr->builtins[DUK_BIDX_GLOBAL_ENV],
  14096. thr->builtins[DUK_BIDX_GLOBAL_ENV],
  14097. 1 /*add_auto_proto*/);
  14098. duk_remove_m2(thr); /* -> [ ... closure ] */
  14099. /* [ ... closure ] */
  14100. return 1;
  14101. }
  14102. DUK_EXTERNAL duk_int_t duk_compile_raw(duk_hthread *thr, const char *src_buffer, duk_size_t src_length, duk_uint_t flags) {
  14103. duk__compile_raw_args comp_args_alloc;
  14104. duk__compile_raw_args *comp_args = &comp_args_alloc;
  14105. DUK_ASSERT_API_ENTRY(thr);
  14106. if ((flags & DUK_COMPILE_STRLEN) && (src_buffer != NULL)) {
  14107. /* String length is computed here to avoid multiple evaluation
  14108. * of a macro argument in the calling side.
  14109. */
  14110. src_length = DUK_STRLEN(src_buffer);
  14111. }
  14112. comp_args->src_buffer = (const duk_uint8_t *) src_buffer;
  14113. comp_args->src_length = src_length;
  14114. comp_args->flags = flags;
  14115. /* [ ... source? filename? ] (depends on flags) */
  14116. if (flags & DUK_COMPILE_SAFE) {
  14117. duk_int_t rc;
  14118. duk_int_t nargs;
  14119. duk_int_t nrets = 1;
  14120. /* Arguments can be: [ source? filename? &comp_args] so that
  14121. * nargs is 1 to 3. Call site encodes the correct nargs count
  14122. * directly into flags.
  14123. */
  14124. nargs = flags & 0x07;
  14125. DUK_ASSERT(nargs == ((flags & DUK_COMPILE_NOSOURCE) ? 0 : 1) +
  14126. ((flags & DUK_COMPILE_NOFILENAME) ? 0 : 1));
  14127. rc = duk_safe_call(thr, duk__do_compile, (void *) comp_args, nargs, nrets);
  14128. /* [ ... closure ] */
  14129. return rc;
  14130. }
  14131. (void) duk__do_compile(thr, (void *) comp_args);
  14132. /* [ ... closure ] */
  14133. return DUK_EXEC_SUCCESS;
  14134. }
  14135. #line 1 "duk_api_debug.c"
  14136. /*
  14137. * Debugging related API calls
  14138. */
  14139. /* #include duk_internal.h -> already included */
  14140. #if defined(DUK_USE_JSON_SUPPORT)
  14141. DUK_EXTERNAL void duk_push_context_dump(duk_hthread *thr) {
  14142. duk_idx_t idx;
  14143. duk_idx_t top;
  14144. DUK_ASSERT_API_ENTRY(thr);
  14145. /* We don't duk_require_stack() here now, but rely on the caller having
  14146. * enough space.
  14147. */
  14148. top = duk_get_top(thr);
  14149. duk_push_array(thr);
  14150. for (idx = 0; idx < top; idx++) {
  14151. duk_dup(thr, idx);
  14152. duk_put_prop_index(thr, -2, (duk_uarridx_t) idx);
  14153. }
  14154. /* XXX: conversion errors should not propagate outwards.
  14155. * Perhaps values need to be coerced individually?
  14156. */
  14157. duk_bi_json_stringify_helper(thr,
  14158. duk_get_top_index(thr), /*idx_value*/
  14159. DUK_INVALID_INDEX, /*idx_replacer*/
  14160. DUK_INVALID_INDEX, /*idx_space*/
  14161. DUK_JSON_FLAG_EXT_CUSTOM |
  14162. DUK_JSON_FLAG_ASCII_ONLY |
  14163. DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
  14164. duk_push_sprintf(thr, "ctx: top=%ld, stack=%s", (long) top, (const char *) duk_safe_to_string(thr, -1));
  14165. duk_replace(thr, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */
  14166. duk_pop(thr);
  14167. DUK_ASSERT(duk_is_string(thr, -1));
  14168. }
  14169. #else /* DUK_USE_JSON_SUPPORT */
  14170. DUK_EXTERNAL void duk_push_context_dump(duk_hthread *thr) {
  14171. DUK_ASSERT_API_ENTRY(thr);
  14172. DUK_ERROR_UNSUPPORTED(thr);
  14173. DUK_WO_NORETURN(return;);
  14174. }
  14175. #endif /* DUK_USE_JSON_SUPPORT */
  14176. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  14177. DUK_EXTERNAL void duk_debugger_attach(duk_hthread *thr,
  14178. duk_debug_read_function read_cb,
  14179. duk_debug_write_function write_cb,
  14180. duk_debug_peek_function peek_cb,
  14181. duk_debug_read_flush_function read_flush_cb,
  14182. duk_debug_write_flush_function write_flush_cb,
  14183. duk_debug_request_function request_cb,
  14184. duk_debug_detached_function detached_cb,
  14185. void *udata) {
  14186. duk_heap *heap;
  14187. const char *str;
  14188. duk_size_t len;
  14189. /* XXX: should there be an error or an automatic detach if
  14190. * already attached?
  14191. */
  14192. DUK_D(DUK_DPRINT("application called duk_debugger_attach()"));
  14193. DUK_ASSERT_API_ENTRY(thr);
  14194. DUK_ASSERT(read_cb != NULL);
  14195. DUK_ASSERT(write_cb != NULL);
  14196. /* Other callbacks are optional. */
  14197. heap = thr->heap;
  14198. heap->dbg_read_cb = read_cb;
  14199. heap->dbg_write_cb = write_cb;
  14200. heap->dbg_peek_cb = peek_cb;
  14201. heap->dbg_read_flush_cb = read_flush_cb;
  14202. heap->dbg_write_flush_cb = write_flush_cb;
  14203. heap->dbg_request_cb = request_cb;
  14204. heap->dbg_detached_cb = detached_cb;
  14205. heap->dbg_udata = udata;
  14206. heap->dbg_have_next_byte = 0;
  14207. /* Start in paused state. */
  14208. heap->dbg_processing = 0;
  14209. heap->dbg_state_dirty = 0;
  14210. heap->dbg_force_restart = 0;
  14211. heap->dbg_pause_flags = 0;
  14212. heap->dbg_pause_act = NULL;
  14213. heap->dbg_pause_startline = 0;
  14214. heap->dbg_exec_counter = 0;
  14215. heap->dbg_last_counter = 0;
  14216. heap->dbg_last_time = 0.0;
  14217. duk_debug_set_paused(heap); /* XXX: overlap with fields above */
  14218. /* Send version identification and flush right afterwards. Note that
  14219. * we must write raw, unframed bytes here.
  14220. */
  14221. duk_push_sprintf(thr, "%ld %ld %s %s\n",
  14222. (long) DUK_DEBUG_PROTOCOL_VERSION,
  14223. (long) DUK_VERSION,
  14224. (const char *) DUK_GIT_DESCRIBE,
  14225. (const char *) DUK_USE_TARGET_INFO);
  14226. str = duk_get_lstring(thr, -1, &len);
  14227. DUK_ASSERT(str != NULL);
  14228. duk_debug_write_bytes(thr, (const duk_uint8_t *) str, len);
  14229. duk_debug_write_flush(thr);
  14230. duk_pop(thr);
  14231. }
  14232. DUK_EXTERNAL void duk_debugger_detach(duk_hthread *thr) {
  14233. DUK_D(DUK_DPRINT("application called duk_debugger_detach()"));
  14234. DUK_ASSERT_API_ENTRY(thr);
  14235. DUK_ASSERT(thr->heap != NULL);
  14236. /* Can be called multiple times with no harm. */
  14237. duk_debug_do_detach(thr->heap);
  14238. }
  14239. DUK_EXTERNAL void duk_debugger_cooperate(duk_hthread *thr) {
  14240. duk_bool_t processed_messages;
  14241. DUK_ASSERT_API_ENTRY(thr);
  14242. DUK_ASSERT(thr->heap != NULL);
  14243. if (!duk_debug_is_attached(thr->heap)) {
  14244. return;
  14245. }
  14246. if (thr->callstack_curr != NULL || thr->heap->dbg_processing) {
  14247. /* Calling duk_debugger_cooperate() while Duktape is being
  14248. * called into is not supported. This is not a 100% check
  14249. * but prevents any damage in most cases.
  14250. */
  14251. return;
  14252. }
  14253. processed_messages = duk_debug_process_messages(thr, 1 /*no_block*/);
  14254. DUK_UNREF(processed_messages);
  14255. }
  14256. DUK_EXTERNAL duk_bool_t duk_debugger_notify(duk_hthread *thr, duk_idx_t nvalues) {
  14257. duk_idx_t top;
  14258. duk_idx_t idx;
  14259. duk_bool_t ret = 0;
  14260. DUK_ASSERT_API_ENTRY(thr);
  14261. DUK_ASSERT(thr->heap != NULL);
  14262. DUK_D(DUK_DPRINT("application called duk_debugger_notify() with nvalues=%ld", (long) nvalues));
  14263. top = duk_get_top(thr);
  14264. if (top < nvalues) {
  14265. DUK_ERROR_RANGE(thr, "not enough stack values for notify");
  14266. DUK_WO_NORETURN(return 0;);
  14267. }
  14268. if (duk_debug_is_attached(thr->heap)) {
  14269. duk_debug_write_notify(thr, DUK_DBG_CMD_APPNOTIFY);
  14270. for (idx = top - nvalues; idx < top; idx++) {
  14271. duk_tval *tv = DUK_GET_TVAL_POSIDX(thr, idx);
  14272. duk_debug_write_tval(thr, tv);
  14273. }
  14274. duk_debug_write_eom(thr);
  14275. /* Return non-zero (true) if we have a good reason to believe
  14276. * the notify was delivered; if we're still attached at least
  14277. * a transport error was not indicated by the transport write
  14278. * callback. This is not a 100% guarantee of course.
  14279. */
  14280. if (duk_debug_is_attached(thr->heap)) {
  14281. ret = 1;
  14282. }
  14283. }
  14284. duk_pop_n(thr, nvalues);
  14285. return ret;
  14286. }
  14287. DUK_EXTERNAL void duk_debugger_pause(duk_hthread *thr) {
  14288. DUK_ASSERT_API_ENTRY(thr);
  14289. DUK_ASSERT(thr->heap != NULL);
  14290. DUK_D(DUK_DPRINT("application called duk_debugger_pause()"));
  14291. /* Treat like a debugger statement: ignore when not attached. */
  14292. if (duk_debug_is_attached(thr->heap)) {
  14293. if (duk_debug_is_paused(thr->heap)) {
  14294. DUK_D(DUK_DPRINT("duk_debugger_pause() called when already paused; ignoring"));
  14295. } else {
  14296. duk_debug_set_paused(thr->heap);
  14297. /* Pause on the next opcode executed. This is always safe to do even
  14298. * inside the debugger message loop: the interrupt counter will be reset
  14299. * to its proper value when the message loop exits.
  14300. */
  14301. thr->interrupt_init = 1;
  14302. thr->interrupt_counter = 0;
  14303. }
  14304. }
  14305. }
  14306. #else /* DUK_USE_DEBUGGER_SUPPORT */
  14307. DUK_EXTERNAL void duk_debugger_attach(duk_hthread *thr,
  14308. duk_debug_read_function read_cb,
  14309. duk_debug_write_function write_cb,
  14310. duk_debug_peek_function peek_cb,
  14311. duk_debug_read_flush_function read_flush_cb,
  14312. duk_debug_write_flush_function write_flush_cb,
  14313. duk_debug_request_function request_cb,
  14314. duk_debug_detached_function detached_cb,
  14315. void *udata) {
  14316. DUK_ASSERT_API_ENTRY(thr);
  14317. DUK_UNREF(read_cb);
  14318. DUK_UNREF(write_cb);
  14319. DUK_UNREF(peek_cb);
  14320. DUK_UNREF(read_flush_cb);
  14321. DUK_UNREF(write_flush_cb);
  14322. DUK_UNREF(request_cb);
  14323. DUK_UNREF(detached_cb);
  14324. DUK_UNREF(udata);
  14325. DUK_ERROR_TYPE(thr, "no debugger support");
  14326. DUK_WO_NORETURN(return;);
  14327. }
  14328. DUK_EXTERNAL void duk_debugger_detach(duk_hthread *thr) {
  14329. DUK_ASSERT_API_ENTRY(thr);
  14330. DUK_ERROR_TYPE(thr, "no debugger support");
  14331. DUK_WO_NORETURN(return;);
  14332. }
  14333. DUK_EXTERNAL void duk_debugger_cooperate(duk_hthread *thr) {
  14334. /* nop */
  14335. DUK_ASSERT_API_ENTRY(thr);
  14336. DUK_UNREF(thr);
  14337. }
  14338. DUK_EXTERNAL duk_bool_t duk_debugger_notify(duk_hthread *thr, duk_idx_t nvalues) {
  14339. duk_idx_t top;
  14340. DUK_ASSERT_API_ENTRY(thr);
  14341. top = duk_get_top(thr);
  14342. if (top < nvalues) {
  14343. DUK_ERROR_RANGE_INVALID_COUNT(thr);
  14344. DUK_WO_NORETURN(return 0;);
  14345. }
  14346. /* No debugger support, just pop values. */
  14347. duk_pop_n(thr, nvalues);
  14348. return 0;
  14349. }
  14350. DUK_EXTERNAL void duk_debugger_pause(duk_hthread *thr) {
  14351. /* Treat like debugger statement: nop */
  14352. DUK_ASSERT_API_ENTRY(thr);
  14353. DUK_UNREF(thr);
  14354. }
  14355. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  14356. #line 1 "duk_api_heap.c"
  14357. /*
  14358. * Heap creation and destruction
  14359. */
  14360. /* #include duk_internal.h -> already included */
  14361. typedef struct duk_internal_thread_state duk_internal_thread_state;
  14362. struct duk_internal_thread_state {
  14363. duk_ljstate lj;
  14364. duk_bool_t creating_error;
  14365. duk_hthread *curr_thread;
  14366. duk_int_t call_recursion_depth;
  14367. };
  14368. DUK_EXTERNAL duk_hthread *duk_create_heap(duk_alloc_function alloc_func,
  14369. duk_realloc_function realloc_func,
  14370. duk_free_function free_func,
  14371. void *heap_udata,
  14372. duk_fatal_function fatal_handler) {
  14373. duk_heap *heap = NULL;
  14374. duk_hthread *thr;
  14375. /* Assume that either all memory funcs are NULL or non-NULL, mixed
  14376. * cases will now be unsafe.
  14377. */
  14378. /* XXX: just assert non-NULL values here and make caller arguments
  14379. * do the defaulting to the default implementations (smaller code)?
  14380. */
  14381. if (!alloc_func) {
  14382. DUK_ASSERT(realloc_func == NULL);
  14383. DUK_ASSERT(free_func == NULL);
  14384. #if defined(DUK_USE_PROVIDE_DEFAULT_ALLOC_FUNCTIONS)
  14385. alloc_func = duk_default_alloc_function;
  14386. realloc_func = duk_default_realloc_function;
  14387. free_func = duk_default_free_function;
  14388. #else
  14389. DUK_D(DUK_DPRINT("no allocation functions given and no default providers"));
  14390. return NULL;
  14391. #endif
  14392. } else {
  14393. DUK_ASSERT(realloc_func != NULL);
  14394. DUK_ASSERT(free_func != NULL);
  14395. }
  14396. if (!fatal_handler) {
  14397. fatal_handler = duk_default_fatal_handler;
  14398. }
  14399. DUK_ASSERT(alloc_func != NULL);
  14400. DUK_ASSERT(realloc_func != NULL);
  14401. DUK_ASSERT(free_func != NULL);
  14402. DUK_ASSERT(fatal_handler != NULL);
  14403. heap = duk_heap_alloc(alloc_func, realloc_func, free_func, heap_udata, fatal_handler);
  14404. if (!heap) {
  14405. return NULL;
  14406. }
  14407. thr = heap->heap_thread;
  14408. DUK_ASSERT(thr != NULL);
  14409. DUK_ASSERT(thr->heap != NULL);
  14410. return thr;
  14411. }
  14412. DUK_EXTERNAL void duk_destroy_heap(duk_hthread *thr) {
  14413. duk_heap *heap;
  14414. if (!thr) {
  14415. return;
  14416. }
  14417. DUK_ASSERT_API_ENTRY(thr);
  14418. heap = thr->heap;
  14419. DUK_ASSERT(heap != NULL);
  14420. duk_heap_free(heap);
  14421. }
  14422. DUK_EXTERNAL void duk_suspend(duk_hthread *thr, duk_thread_state *state) {
  14423. duk_internal_thread_state *snapshot = (duk_internal_thread_state *) (void *) state;
  14424. duk_heap *heap;
  14425. duk_ljstate *lj;
  14426. DUK_ASSERT_API_ENTRY(thr);
  14427. DUK_ASSERT(thr->heap != NULL);
  14428. DUK_ASSERT(state != NULL); /* unvalidated */
  14429. /* Currently not supported when called from within a finalizer.
  14430. * If that is done, the finalizer will remain running indefinitely,
  14431. * preventing other finalizers from executing. The assert is a bit
  14432. * wider, checking that it would be OK to run pending finalizers.
  14433. */
  14434. DUK_ASSERT(thr->heap->pf_prevent_count == 0);
  14435. /* Currently not supported to duk_suspend() from an errCreate()
  14436. * call.
  14437. */
  14438. DUK_ASSERT(thr->heap->creating_error == 0);
  14439. heap = thr->heap;
  14440. lj = &heap->lj;
  14441. duk_push_tval(thr, &lj->value1);
  14442. duk_push_tval(thr, &lj->value2);
  14443. /* XXX: creating_error == 0 is asserted above, so no need to store. */
  14444. duk_memcpy((void *) &snapshot->lj, (const void *) lj, sizeof(duk_ljstate));
  14445. snapshot->creating_error = heap->creating_error;
  14446. snapshot->curr_thread = heap->curr_thread;
  14447. snapshot->call_recursion_depth = heap->call_recursion_depth;
  14448. lj->jmpbuf_ptr = NULL;
  14449. lj->type = DUK_LJ_TYPE_UNKNOWN;
  14450. DUK_TVAL_SET_UNDEFINED(&lj->value1);
  14451. DUK_TVAL_SET_UNDEFINED(&lj->value2);
  14452. heap->creating_error = 0;
  14453. heap->curr_thread = NULL;
  14454. heap->call_recursion_depth = 0;
  14455. }
  14456. DUK_EXTERNAL void duk_resume(duk_hthread *thr, const duk_thread_state *state) {
  14457. const duk_internal_thread_state *snapshot = (const duk_internal_thread_state *) (const void *) state;
  14458. duk_heap *heap;
  14459. DUK_ASSERT_API_ENTRY(thr);
  14460. DUK_ASSERT(thr->heap != NULL);
  14461. DUK_ASSERT(state != NULL); /* unvalidated */
  14462. /* Shouldn't be necessary if duk_suspend() is called before
  14463. * duk_resume(), but assert in case API sequence is incorrect.
  14464. */
  14465. DUK_ASSERT(thr->heap->pf_prevent_count == 0);
  14466. DUK_ASSERT(thr->heap->creating_error == 0);
  14467. heap = thr->heap;
  14468. duk_memcpy((void *) &heap->lj, (const void *) &snapshot->lj, sizeof(duk_ljstate));
  14469. heap->creating_error = snapshot->creating_error;
  14470. heap->curr_thread = snapshot->curr_thread;
  14471. heap->call_recursion_depth = snapshot->call_recursion_depth;
  14472. duk_pop_2(thr);
  14473. }
  14474. /* XXX: better place for this */
  14475. DUK_EXTERNAL void duk_set_global_object(duk_hthread *thr) {
  14476. duk_hobject *h_glob;
  14477. duk_hobject *h_prev_glob;
  14478. duk_hobjenv *h_env;
  14479. duk_hobject *h_prev_env;
  14480. DUK_ASSERT_API_ENTRY(thr);
  14481. DUK_D(DUK_DPRINT("replace global object with: %!T", duk_get_tval(thr, -1)));
  14482. h_glob = duk_require_hobject(thr, -1);
  14483. DUK_ASSERT(h_glob != NULL);
  14484. /*
  14485. * Replace global object.
  14486. */
  14487. h_prev_glob = thr->builtins[DUK_BIDX_GLOBAL];
  14488. DUK_UNREF(h_prev_glob);
  14489. thr->builtins[DUK_BIDX_GLOBAL] = h_glob;
  14490. DUK_HOBJECT_INCREF(thr, h_glob);
  14491. DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_glob); /* side effects, in theory (referenced by global env) */
  14492. /*
  14493. * Replace lexical environment for global scope
  14494. *
  14495. * Create a new object environment for the global lexical scope.
  14496. * We can't just reset the _Target property of the current one,
  14497. * because the lexical scope is shared by other threads with the
  14498. * same (initial) built-ins.
  14499. */
  14500. h_env = duk_hobjenv_alloc(thr,
  14501. DUK_HOBJECT_FLAG_EXTENSIBLE |
  14502. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV));
  14503. DUK_ASSERT(h_env != NULL);
  14504. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_env) == NULL);
  14505. DUK_ASSERT(h_env->target == NULL);
  14506. DUK_ASSERT(h_glob != NULL);
  14507. h_env->target = h_glob;
  14508. DUK_HOBJECT_INCREF(thr, h_glob);
  14509. DUK_ASSERT(h_env->has_this == 0);
  14510. /* [ ... new_glob ] */
  14511. h_prev_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  14512. thr->builtins[DUK_BIDX_GLOBAL_ENV] = (duk_hobject *) h_env;
  14513. DUK_HOBJECT_INCREF(thr, (duk_hobject *) h_env);
  14514. DUK_HOBJECT_DECREF_ALLOWNULL(thr, h_prev_env); /* side effects */
  14515. DUK_UNREF(h_env); /* without refcounts */
  14516. DUK_UNREF(h_prev_env);
  14517. /* [ ... new_glob ] */
  14518. duk_pop(thr);
  14519. /* [ ... ] */
  14520. }
  14521. #line 1 "duk_api_inspect.c"
  14522. /*
  14523. * Inspection
  14524. */
  14525. /* #include duk_internal.h -> already included */
  14526. /* For footprint efficient multiple value setting: arrays are much better than
  14527. * varargs, format string with parsing is often better than string pointer arrays.
  14528. */
  14529. DUK_LOCAL void duk__inspect_multiple_uint(duk_hthread *thr, const char *fmt, duk_int_t *vals) {
  14530. duk_int_t val;
  14531. const char *p;
  14532. const char *p_curr;
  14533. duk_size_t len;
  14534. for (p = fmt;;) {
  14535. len = DUK_STRLEN(p);
  14536. p_curr = p;
  14537. p += len + 1;
  14538. if (len == 0) {
  14539. /* Double NUL (= empty key) terminates. */
  14540. break;
  14541. }
  14542. val = *vals++;
  14543. if (val >= 0) {
  14544. /* Negative values are markers to skip key. */
  14545. duk_push_string(thr, p_curr);
  14546. duk_push_int(thr, val);
  14547. duk_put_prop(thr, -3);
  14548. }
  14549. }
  14550. }
  14551. /* Raw helper to extract internal information / statistics about a value.
  14552. * The return value is an object with properties that are version specific.
  14553. * The properties must not expose anything that would lead to security
  14554. * issues (e.g. exposing compiled function 'data' buffer might be an issue).
  14555. * Currently only counts and sizes and such are given so there shouldn't
  14556. * be security implications.
  14557. */
  14558. #define DUK__IDX_TYPE 0
  14559. #define DUK__IDX_ITAG 1
  14560. #define DUK__IDX_REFC 2
  14561. #define DUK__IDX_HBYTES 3
  14562. #define DUK__IDX_CLASS 4
  14563. #define DUK__IDX_PBYTES 5
  14564. #define DUK__IDX_ESIZE 6
  14565. #define DUK__IDX_ENEXT 7
  14566. #define DUK__IDX_ASIZE 8
  14567. #define DUK__IDX_HSIZE 9
  14568. #define DUK__IDX_BCBYTES 10
  14569. #define DUK__IDX_DBYTES 11
  14570. #define DUK__IDX_TSTATE 12
  14571. #define DUK__IDX_VARIANT 13
  14572. DUK_EXTERNAL void duk_inspect_value(duk_hthread *thr, duk_idx_t idx) {
  14573. duk_tval *tv;
  14574. duk_heaphdr *h;
  14575. /* The temporary values should be in an array rather than individual
  14576. * variables which (in practice) ensures that the compiler won't map
  14577. * them to registers and emit a lot of unnecessary shuffling code.
  14578. */
  14579. duk_int_t vals[14];
  14580. DUK_ASSERT_API_ENTRY(thr);
  14581. /* Assume two's complement and set everything to -1. */
  14582. duk_memset((void *) &vals, (int) 0xff, sizeof(vals));
  14583. DUK_ASSERT(vals[DUK__IDX_TYPE] == -1); /* spot check one */
  14584. tv = duk_get_tval_or_unused(thr, idx);
  14585. h = (DUK_TVAL_IS_HEAP_ALLOCATED(tv) ? DUK_TVAL_GET_HEAPHDR(tv) : NULL);
  14586. vals[DUK__IDX_TYPE] = duk_get_type_tval(tv);
  14587. vals[DUK__IDX_ITAG] = (duk_int_t) DUK_TVAL_GET_TAG(tv);
  14588. duk_push_bare_object(thr); /* Invalidates 'tv'. */
  14589. tv = NULL;
  14590. if (h == NULL) {
  14591. goto finish;
  14592. }
  14593. duk_push_pointer(thr, (void *) h);
  14594. duk_put_prop_literal(thr, -2, "hptr");
  14595. #if 0
  14596. /* Covers a lot of information, e.g. buffer and string variants. */
  14597. duk_push_uint(thr, (duk_uint_t) DUK_HEAPHDR_GET_FLAGS(h));
  14598. duk_put_prop_literal(thr, -2, "hflags");
  14599. #endif
  14600. #if defined(DUK_USE_REFERENCE_COUNTING)
  14601. vals[DUK__IDX_REFC] = (duk_int_t) DUK_HEAPHDR_GET_REFCOUNT(h);
  14602. #endif
  14603. vals[DUK__IDX_VARIANT] = 0;
  14604. /* Heaphdr size and additional allocation size, followed by
  14605. * type specific stuff (with varying value count).
  14606. */
  14607. switch ((duk_small_int_t) DUK_HEAPHDR_GET_TYPE(h)) {
  14608. case DUK_HTYPE_STRING: {
  14609. duk_hstring *h_str = (duk_hstring *) h;
  14610. vals[DUK__IDX_HBYTES] = (duk_int_t) (sizeof(duk_hstring) + DUK_HSTRING_GET_BYTELEN(h_str) + 1);
  14611. #if defined(DUK_USE_HSTRING_EXTDATA)
  14612. if (DUK_HSTRING_HAS_EXTDATA(h_str)) {
  14613. vals[DUK__IDX_VARIANT] = 1;
  14614. }
  14615. #endif
  14616. break;
  14617. }
  14618. case DUK_HTYPE_OBJECT: {
  14619. duk_hobject *h_obj = (duk_hobject *) h;
  14620. /* XXX: variants here are maybe pointless; class is enough? */
  14621. if (DUK_HOBJECT_IS_ARRAY(h_obj)) {
  14622. vals[DUK__IDX_HBYTES] = sizeof(duk_harray);
  14623. } else if (DUK_HOBJECT_IS_COMPFUNC(h_obj)) {
  14624. vals[DUK__IDX_HBYTES] = sizeof(duk_hcompfunc);
  14625. } else if (DUK_HOBJECT_IS_NATFUNC(h_obj)) {
  14626. vals[DUK__IDX_HBYTES] = sizeof(duk_hnatfunc);
  14627. } else if (DUK_HOBJECT_IS_THREAD(h_obj)) {
  14628. vals[DUK__IDX_HBYTES] = sizeof(duk_hthread);
  14629. vals[DUK__IDX_TSTATE] = ((duk_hthread *) h_obj)->state;
  14630. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  14631. } else if (DUK_HOBJECT_IS_BUFOBJ(h_obj)) {
  14632. vals[DUK__IDX_HBYTES] = sizeof(duk_hbufobj);
  14633. /* XXX: some size information */
  14634. #endif
  14635. } else {
  14636. vals[DUK__IDX_HBYTES] = (duk_small_uint_t) sizeof(duk_hobject);
  14637. }
  14638. vals[DUK__IDX_CLASS] = (duk_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h_obj);
  14639. vals[DUK__IDX_PBYTES] = (duk_int_t) DUK_HOBJECT_P_ALLOC_SIZE(h_obj);
  14640. vals[DUK__IDX_ESIZE] = (duk_int_t) DUK_HOBJECT_GET_ESIZE(h_obj);
  14641. vals[DUK__IDX_ENEXT] = (duk_int_t) DUK_HOBJECT_GET_ENEXT(h_obj);
  14642. vals[DUK__IDX_ASIZE] = (duk_int_t) DUK_HOBJECT_GET_ASIZE(h_obj);
  14643. vals[DUK__IDX_HSIZE] = (duk_int_t) DUK_HOBJECT_GET_HSIZE(h_obj);
  14644. /* Note: e_next indicates the number of gc-reachable entries
  14645. * in the entry part, and also indicates the index where the
  14646. * next new property would be inserted. It does *not* indicate
  14647. * the number of non-NULL keys present in the object. That
  14648. * value could be counted separately but requires a pass through
  14649. * the key list.
  14650. */
  14651. if (DUK_HOBJECT_IS_COMPFUNC(h_obj)) {
  14652. duk_hbuffer *h_data = (duk_hbuffer *) DUK_HCOMPFUNC_GET_DATA(thr->heap, (duk_hcompfunc *) h_obj);
  14653. vals[DUK__IDX_BCBYTES] = (duk_int_t) (h_data ? DUK_HBUFFER_GET_SIZE(h_data) : 0);
  14654. }
  14655. break;
  14656. }
  14657. case DUK_HTYPE_BUFFER: {
  14658. duk_hbuffer *h_buf = (duk_hbuffer *) h;
  14659. if (DUK_HBUFFER_HAS_DYNAMIC(h_buf)) {
  14660. if (DUK_HBUFFER_HAS_EXTERNAL(h_buf)) {
  14661. vals[DUK__IDX_VARIANT] = 2; /* buffer variant 2: external */
  14662. vals[DUK__IDX_HBYTES] = (duk_uint_t) (sizeof(duk_hbuffer_external));
  14663. } else {
  14664. /* When alloc_size == 0 the second allocation may not
  14665. * actually exist.
  14666. */
  14667. vals[DUK__IDX_VARIANT] = 1; /* buffer variant 1: dynamic */
  14668. vals[DUK__IDX_HBYTES] = (duk_uint_t) (sizeof(duk_hbuffer_dynamic));
  14669. }
  14670. vals[DUK__IDX_DBYTES] = (duk_int_t) (DUK_HBUFFER_GET_SIZE(h_buf));
  14671. } else {
  14672. DUK_ASSERT(vals[DUK__IDX_VARIANT] == 0); /* buffer variant 0: fixed */
  14673. vals[DUK__IDX_HBYTES] = (duk_int_t) (sizeof(duk_hbuffer_fixed) + DUK_HBUFFER_GET_SIZE(h_buf));
  14674. }
  14675. break;
  14676. }
  14677. }
  14678. finish:
  14679. duk__inspect_multiple_uint(thr,
  14680. "type" "\x00" "itag" "\x00" "refc" "\x00" "hbytes" "\x00" "class" "\x00"
  14681. "pbytes" "\x00" "esize" "\x00" "enext" "\x00" "asize" "\x00" "hsize" "\x00"
  14682. "bcbytes" "\x00" "dbytes" "\x00" "tstate" "\x00" "variant" "\x00" "\x00",
  14683. (duk_int_t *) &vals);
  14684. }
  14685. DUK_EXTERNAL void duk_inspect_callstack_entry(duk_hthread *thr, duk_int_t level) {
  14686. duk_activation *act;
  14687. duk_uint_fast32_t pc;
  14688. duk_uint_fast32_t line;
  14689. DUK_ASSERT_API_ENTRY(thr);
  14690. /* -1 = top callstack entry
  14691. * -2 = caller of level -1
  14692. * etc
  14693. */
  14694. act = duk_hthread_get_activation_for_level(thr, level);
  14695. if (act == NULL) {
  14696. duk_push_undefined(thr);
  14697. return;
  14698. }
  14699. duk_push_bare_object(thr);
  14700. /* Relevant PC is just before current one because PC is
  14701. * post-incremented. This should match what error augment
  14702. * code does.
  14703. */
  14704. pc = duk_hthread_get_act_prev_pc(thr, act);
  14705. duk_push_tval(thr, &act->tv_func);
  14706. duk_push_uint(thr, (duk_uint_t) pc);
  14707. duk_put_prop_stridx_short(thr, -3, DUK_STRIDX_PC);
  14708. #if defined(DUK_USE_PC2LINE)
  14709. line = duk_hobject_pc2line_query(thr, -1, pc);
  14710. #else
  14711. line = 0;
  14712. #endif
  14713. duk_push_uint(thr, (duk_uint_t) line);
  14714. duk_put_prop_stridx_short(thr, -3, DUK_STRIDX_LINE_NUMBER);
  14715. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_LC_FUNCTION);
  14716. /* Providing access to e.g. act->lex_env would be dangerous: these
  14717. * internal structures must never be accessible to the application.
  14718. * Duktape relies on them having consistent data, and this consistency
  14719. * is only asserted for, not checked for.
  14720. */
  14721. }
  14722. /* automatic undefs */
  14723. #undef DUK__IDX_ASIZE
  14724. #undef DUK__IDX_BCBYTES
  14725. #undef DUK__IDX_CLASS
  14726. #undef DUK__IDX_DBYTES
  14727. #undef DUK__IDX_ENEXT
  14728. #undef DUK__IDX_ESIZE
  14729. #undef DUK__IDX_HBYTES
  14730. #undef DUK__IDX_HSIZE
  14731. #undef DUK__IDX_ITAG
  14732. #undef DUK__IDX_PBYTES
  14733. #undef DUK__IDX_REFC
  14734. #undef DUK__IDX_TSTATE
  14735. #undef DUK__IDX_TYPE
  14736. #undef DUK__IDX_VARIANT
  14737. #line 1 "duk_api_memory.c"
  14738. /*
  14739. * Memory calls.
  14740. */
  14741. /* #include duk_internal.h -> already included */
  14742. DUK_EXTERNAL void *duk_alloc_raw(duk_hthread *thr, duk_size_t size) {
  14743. DUK_ASSERT_API_ENTRY(thr);
  14744. return DUK_ALLOC_RAW(thr->heap, size);
  14745. }
  14746. DUK_EXTERNAL void duk_free_raw(duk_hthread *thr, void *ptr) {
  14747. DUK_ASSERT_API_ENTRY(thr);
  14748. DUK_FREE_RAW(thr->heap, ptr);
  14749. }
  14750. DUK_EXTERNAL void *duk_realloc_raw(duk_hthread *thr, void *ptr, duk_size_t size) {
  14751. DUK_ASSERT_API_ENTRY(thr);
  14752. return DUK_REALLOC_RAW(thr->heap, ptr, size);
  14753. }
  14754. DUK_EXTERNAL void *duk_alloc(duk_hthread *thr, duk_size_t size) {
  14755. DUK_ASSERT_API_ENTRY(thr);
  14756. return DUK_ALLOC(thr->heap, size);
  14757. }
  14758. DUK_EXTERNAL void duk_free(duk_hthread *thr, void *ptr) {
  14759. DUK_ASSERT_API_ENTRY(thr);
  14760. DUK_FREE_CHECKED(thr, ptr);
  14761. }
  14762. DUK_EXTERNAL void *duk_realloc(duk_hthread *thr, void *ptr, duk_size_t size) {
  14763. DUK_ASSERT_API_ENTRY(thr);
  14764. /*
  14765. * Note: since this is an exposed API call, there should be
  14766. * no way a mark-and-sweep could have a side effect on the
  14767. * memory allocation behind 'ptr'; the pointer should never
  14768. * be something that Duktape wants to change.
  14769. *
  14770. * Thus, no need to use DUK_REALLOC_INDIRECT (and we don't
  14771. * have the storage location here anyway).
  14772. */
  14773. return DUK_REALLOC(thr->heap, ptr, size);
  14774. }
  14775. DUK_EXTERNAL void duk_get_memory_functions(duk_hthread *thr, duk_memory_functions *out_funcs) {
  14776. duk_heap *heap;
  14777. DUK_ASSERT_API_ENTRY(thr);
  14778. DUK_ASSERT(out_funcs != NULL);
  14779. DUK_ASSERT(thr != NULL);
  14780. DUK_ASSERT(thr->heap != NULL);
  14781. heap = thr->heap;
  14782. out_funcs->alloc_func = heap->alloc_func;
  14783. out_funcs->realloc_func = heap->realloc_func;
  14784. out_funcs->free_func = heap->free_func;
  14785. out_funcs->udata = heap->heap_udata;
  14786. }
  14787. DUK_EXTERNAL void duk_gc(duk_hthread *thr, duk_uint_t flags) {
  14788. duk_heap *heap;
  14789. duk_small_uint_t ms_flags;
  14790. DUK_ASSERT_API_ENTRY(thr);
  14791. heap = thr->heap;
  14792. DUK_ASSERT(heap != NULL);
  14793. DUK_D(DUK_DPRINT("mark-and-sweep requested by application"));
  14794. DUK_ASSERT(DUK_GC_COMPACT == DUK_MS_FLAG_EMERGENCY); /* Compact flag is 1:1 with emergency flag which forces compaction. */
  14795. ms_flags = (duk_small_uint_t) flags;
  14796. duk_heap_mark_and_sweep(heap, ms_flags);
  14797. }
  14798. #line 1 "duk_api_object.c"
  14799. /*
  14800. * Object handling: property access and other support functions.
  14801. */
  14802. /* #include duk_internal.h -> already included */
  14803. /*
  14804. * Property handling
  14805. *
  14806. * The API exposes only the most common property handling functions.
  14807. * The caller can invoke ECMAScript built-ins for full control (e.g.
  14808. * defineProperty, getOwnPropertyDescriptor).
  14809. */
  14810. DUK_EXTERNAL duk_bool_t duk_get_prop(duk_hthread *thr, duk_idx_t obj_idx) {
  14811. duk_tval *tv_obj;
  14812. duk_tval *tv_key;
  14813. duk_bool_t rc;
  14814. DUK_ASSERT_API_ENTRY(thr);
  14815. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  14816. * resize is not necessary for a property get right now.
  14817. */
  14818. tv_obj = duk_require_tval(thr, obj_idx);
  14819. tv_key = duk_require_tval(thr, -1);
  14820. rc = duk_hobject_getprop(thr, tv_obj, tv_key);
  14821. DUK_ASSERT(rc == 0 || rc == 1);
  14822. /* a value is left on stack regardless of rc */
  14823. duk_remove_m2(thr); /* remove key */
  14824. DUK_ASSERT(duk_is_undefined(thr, -1) || rc == 1);
  14825. return rc; /* 1 if property found, 0 otherwise */
  14826. }
  14827. DUK_EXTERNAL duk_bool_t duk_get_prop_string(duk_hthread *thr, duk_idx_t obj_idx, const char *key) {
  14828. DUK_ASSERT_API_ENTRY(thr);
  14829. DUK_ASSERT(key != NULL);
  14830. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14831. (void) duk_push_string(thr, key);
  14832. return duk_get_prop(thr, obj_idx);
  14833. }
  14834. DUK_EXTERNAL duk_bool_t duk_get_prop_lstring(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  14835. DUK_ASSERT_API_ENTRY(thr);
  14836. DUK_ASSERT(key != NULL);
  14837. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14838. (void) duk_push_lstring(thr, key, key_len);
  14839. return duk_get_prop(thr, obj_idx);
  14840. }
  14841. #if !defined(DUK_USE_PREFER_SIZE)
  14842. DUK_EXTERNAL duk_bool_t duk_get_prop_literal_raw(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  14843. DUK_ASSERT_API_ENTRY(thr);
  14844. DUK_ASSERT(key != NULL);
  14845. DUK_ASSERT(key[key_len] == (char) 0);
  14846. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14847. (void) duk_push_literal_raw(thr, key, key_len);
  14848. return duk_get_prop(thr, obj_idx);
  14849. }
  14850. #endif
  14851. DUK_EXTERNAL duk_bool_t duk_get_prop_index(duk_hthread *thr, duk_idx_t obj_idx, duk_uarridx_t arr_idx) {
  14852. DUK_ASSERT_API_ENTRY(thr);
  14853. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14854. duk_push_uarridx(thr, arr_idx);
  14855. return duk_get_prop(thr, obj_idx);
  14856. }
  14857. DUK_EXTERNAL duk_bool_t duk_get_prop_heapptr(duk_hthread *thr, duk_idx_t obj_idx, void *ptr) {
  14858. DUK_ASSERT_API_ENTRY(thr);
  14859. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14860. (void) duk_push_heapptr(thr, ptr); /* NULL -> 'undefined' */
  14861. return duk_get_prop(thr, obj_idx);
  14862. }
  14863. DUK_INTERNAL duk_bool_t duk_get_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx) {
  14864. DUK_ASSERT_API_ENTRY(thr);
  14865. DUK_ASSERT_STRIDX_VALID(stridx);
  14866. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14867. (void) duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
  14868. return duk_get_prop(thr, obj_idx);
  14869. }
  14870. DUK_INTERNAL duk_bool_t duk_get_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args) {
  14871. return duk_get_prop_stridx(thr, (duk_idx_t) (duk_int16_t) (packed_args >> 16),
  14872. (duk_small_uint_t) (packed_args & 0xffffUL));
  14873. }
  14874. DUK_INTERNAL duk_bool_t duk_get_prop_stridx_boolean(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx, duk_bool_t *out_has_prop) {
  14875. duk_bool_t rc;
  14876. DUK_ASSERT_API_ENTRY(thr);
  14877. DUK_ASSERT_STRIDX_VALID(stridx);
  14878. rc = duk_get_prop_stridx(thr, obj_idx, stridx);
  14879. if (out_has_prop) {
  14880. *out_has_prop = rc;
  14881. }
  14882. return duk_to_boolean_top_pop(thr);
  14883. }
  14884. DUK_LOCAL duk_bool_t duk__put_prop_shared(duk_hthread *thr, duk_idx_t obj_idx, duk_idx_t idx_key) {
  14885. duk_tval *tv_obj;
  14886. duk_tval *tv_key;
  14887. duk_tval *tv_val;
  14888. duk_bool_t throw_flag;
  14889. duk_bool_t rc;
  14890. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  14891. * resize is not necessary for a property put right now (putprop protects
  14892. * against it internally).
  14893. */
  14894. /* Key and value indices are either (-2, -1) or (-1, -2). Given idx_key,
  14895. * idx_val is always (idx_key ^ 0x01).
  14896. */
  14897. DUK_ASSERT((idx_key == -2 && (idx_key ^ 1) == -1) ||
  14898. (idx_key == -1 && (idx_key ^ 1) == -2));
  14899. /* XXX: Direct access; faster validation. */
  14900. tv_obj = duk_require_tval(thr, obj_idx);
  14901. tv_key = duk_require_tval(thr, idx_key);
  14902. tv_val = duk_require_tval(thr, idx_key ^ 1);
  14903. throw_flag = duk_is_strict_call(thr);
  14904. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, throw_flag);
  14905. DUK_ASSERT(rc == 0 || rc == 1);
  14906. duk_pop_2(thr); /* remove key and value */
  14907. return rc; /* 1 if property found, 0 otherwise */
  14908. }
  14909. DUK_EXTERNAL duk_bool_t duk_put_prop(duk_hthread *thr, duk_idx_t obj_idx) {
  14910. DUK_ASSERT_API_ENTRY(thr);
  14911. return duk__put_prop_shared(thr, obj_idx, -2);
  14912. }
  14913. DUK_EXTERNAL duk_bool_t duk_put_prop_string(duk_hthread *thr, duk_idx_t obj_idx, const char *key) {
  14914. DUK_ASSERT_API_ENTRY(thr);
  14915. DUK_ASSERT(key != NULL);
  14916. /* Careful here and with other duk_put_prop_xxx() helpers: the
  14917. * target object and the property value may be in the same value
  14918. * stack slot (unusual, but still conceptually clear).
  14919. */
  14920. obj_idx = duk_normalize_index(thr, obj_idx);
  14921. (void) duk_push_string(thr, key);
  14922. return duk__put_prop_shared(thr, obj_idx, -1);
  14923. }
  14924. DUK_EXTERNAL duk_bool_t duk_put_prop_lstring(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  14925. DUK_ASSERT_API_ENTRY(thr);
  14926. DUK_ASSERT(key != NULL);
  14927. obj_idx = duk_normalize_index(thr, obj_idx);
  14928. (void) duk_push_lstring(thr, key, key_len);
  14929. return duk__put_prop_shared(thr, obj_idx, -1);
  14930. }
  14931. #if !defined(DUK_USE_PREFER_SIZE)
  14932. DUK_EXTERNAL duk_bool_t duk_put_prop_literal_raw(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  14933. DUK_ASSERT_API_ENTRY(thr);
  14934. DUK_ASSERT(key != NULL);
  14935. DUK_ASSERT(key[key_len] == (char) 0);
  14936. obj_idx = duk_normalize_index(thr, obj_idx);
  14937. (void) duk_push_literal_raw(thr, key, key_len);
  14938. return duk__put_prop_shared(thr, obj_idx, -1);
  14939. }
  14940. #endif
  14941. DUK_EXTERNAL duk_bool_t duk_put_prop_index(duk_hthread *thr, duk_idx_t obj_idx, duk_uarridx_t arr_idx) {
  14942. DUK_ASSERT_API_ENTRY(thr);
  14943. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14944. duk_push_uarridx(thr, arr_idx);
  14945. return duk__put_prop_shared(thr, obj_idx, -1);
  14946. }
  14947. DUK_EXTERNAL duk_bool_t duk_put_prop_heapptr(duk_hthread *thr, duk_idx_t obj_idx, void *ptr) {
  14948. DUK_ASSERT_API_ENTRY(thr);
  14949. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14950. (void) duk_push_heapptr(thr, ptr); /* NULL -> 'undefined' */
  14951. return duk__put_prop_shared(thr, obj_idx, -1);
  14952. }
  14953. DUK_INTERNAL duk_bool_t duk_put_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx) {
  14954. DUK_ASSERT_API_ENTRY(thr);
  14955. DUK_ASSERT_STRIDX_VALID(stridx);
  14956. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14957. duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
  14958. return duk__put_prop_shared(thr, obj_idx, -1);
  14959. }
  14960. DUK_INTERNAL duk_bool_t duk_put_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args) {
  14961. return duk_put_prop_stridx(thr, (duk_idx_t) (duk_int16_t) (packed_args >> 16),
  14962. (duk_small_uint_t) (packed_args & 0xffffUL));
  14963. }
  14964. DUK_EXTERNAL duk_bool_t duk_del_prop(duk_hthread *thr, duk_idx_t obj_idx) {
  14965. duk_tval *tv_obj;
  14966. duk_tval *tv_key;
  14967. duk_bool_t throw_flag;
  14968. duk_bool_t rc;
  14969. DUK_ASSERT_API_ENTRY(thr);
  14970. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  14971. * resize is not necessary for a property delete right now.
  14972. */
  14973. tv_obj = duk_require_tval(thr, obj_idx);
  14974. tv_key = duk_require_tval(thr, -1);
  14975. throw_flag = duk_is_strict_call(thr);
  14976. rc = duk_hobject_delprop(thr, tv_obj, tv_key, throw_flag);
  14977. DUK_ASSERT(rc == 0 || rc == 1);
  14978. duk_pop(thr); /* remove key */
  14979. return rc;
  14980. }
  14981. DUK_EXTERNAL duk_bool_t duk_del_prop_string(duk_hthread *thr, duk_idx_t obj_idx, const char *key) {
  14982. DUK_ASSERT_API_ENTRY(thr);
  14983. DUK_ASSERT(key != NULL);
  14984. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14985. (void) duk_push_string(thr, key);
  14986. return duk_del_prop(thr, obj_idx);
  14987. }
  14988. DUK_EXTERNAL duk_bool_t duk_del_prop_lstring(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  14989. DUK_ASSERT_API_ENTRY(thr);
  14990. DUK_ASSERT(key != NULL);
  14991. obj_idx = duk_require_normalize_index(thr, obj_idx);
  14992. (void) duk_push_lstring(thr, key, key_len);
  14993. return duk_del_prop(thr, obj_idx);
  14994. }
  14995. #if !defined(DUK_USE_PREFER_SIZE)
  14996. DUK_EXTERNAL duk_bool_t duk_del_prop_literal_raw(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  14997. DUK_ASSERT_API_ENTRY(thr);
  14998. DUK_ASSERT(key != NULL);
  14999. DUK_ASSERT(key[key_len] == (char) 0);
  15000. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15001. (void) duk_push_literal_raw(thr, key, key_len);
  15002. return duk_del_prop(thr, obj_idx);
  15003. }
  15004. #endif
  15005. DUK_EXTERNAL duk_bool_t duk_del_prop_index(duk_hthread *thr, duk_idx_t obj_idx, duk_uarridx_t arr_idx) {
  15006. DUK_ASSERT_API_ENTRY(thr);
  15007. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15008. duk_push_uarridx(thr, arr_idx);
  15009. return duk_del_prop(thr, obj_idx);
  15010. }
  15011. DUK_EXTERNAL duk_bool_t duk_del_prop_heapptr(duk_hthread *thr, duk_idx_t obj_idx, void *ptr) {
  15012. DUK_ASSERT_API_ENTRY(thr);
  15013. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15014. (void) duk_push_heapptr(thr, ptr); /* NULL -> 'undefined' */
  15015. return duk_del_prop(thr, obj_idx);
  15016. }
  15017. DUK_INTERNAL duk_bool_t duk_del_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx) {
  15018. DUK_ASSERT_API_ENTRY(thr);
  15019. DUK_ASSERT_STRIDX_VALID(stridx);
  15020. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15021. duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
  15022. return duk_del_prop(thr, obj_idx);
  15023. }
  15024. #if 0
  15025. DUK_INTERNAL duk_bool_t duk_del_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args) {
  15026. return duk_del_prop_stridx(thr, (duk_idx_t) (duk_int16_t) (packed_args >> 16),
  15027. (duk_small_uint_t) (packed_args & 0xffffUL));
  15028. }
  15029. #endif
  15030. DUK_EXTERNAL duk_bool_t duk_has_prop(duk_hthread *thr, duk_idx_t obj_idx) {
  15031. duk_tval *tv_obj;
  15032. duk_tval *tv_key;
  15033. duk_bool_t rc;
  15034. DUK_ASSERT_API_ENTRY(thr);
  15035. /* Note: copying tv_obj and tv_key to locals to shield against a valstack
  15036. * resize is not necessary for a property existence check right now.
  15037. */
  15038. tv_obj = duk_require_tval(thr, obj_idx);
  15039. tv_key = duk_require_tval(thr, -1);
  15040. rc = duk_hobject_hasprop(thr, tv_obj, tv_key);
  15041. DUK_ASSERT(rc == 0 || rc == 1);
  15042. duk_pop(thr); /* remove key */
  15043. return rc; /* 1 if property found, 0 otherwise */
  15044. }
  15045. DUK_EXTERNAL duk_bool_t duk_has_prop_string(duk_hthread *thr, duk_idx_t obj_idx, const char *key) {
  15046. DUK_ASSERT_API_ENTRY(thr);
  15047. DUK_ASSERT(key != NULL);
  15048. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15049. (void) duk_push_string(thr, key);
  15050. return duk_has_prop(thr, obj_idx);
  15051. }
  15052. DUK_EXTERNAL duk_bool_t duk_has_prop_lstring(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  15053. DUK_ASSERT_API_ENTRY(thr);
  15054. DUK_ASSERT(key != NULL);
  15055. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15056. (void) duk_push_lstring(thr, key, key_len);
  15057. return duk_has_prop(thr, obj_idx);
  15058. }
  15059. #if !defined(DUK_USE_PREFER_SIZE)
  15060. DUK_EXTERNAL duk_bool_t duk_has_prop_literal_raw(duk_hthread *thr, duk_idx_t obj_idx, const char *key, duk_size_t key_len) {
  15061. DUK_ASSERT_API_ENTRY(thr);
  15062. DUK_ASSERT(key != NULL);
  15063. DUK_ASSERT(key[key_len] == (char) 0);
  15064. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15065. (void) duk_push_literal_raw(thr, key, key_len);
  15066. return duk_has_prop(thr, obj_idx);
  15067. }
  15068. #endif
  15069. DUK_EXTERNAL duk_bool_t duk_has_prop_index(duk_hthread *thr, duk_idx_t obj_idx, duk_uarridx_t arr_idx) {
  15070. DUK_ASSERT_API_ENTRY(thr);
  15071. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15072. duk_push_uarridx(thr, arr_idx);
  15073. return duk_has_prop(thr, obj_idx);
  15074. }
  15075. DUK_EXTERNAL duk_bool_t duk_has_prop_heapptr(duk_hthread *thr, duk_idx_t obj_idx, void *ptr) {
  15076. DUK_ASSERT_API_ENTRY(thr);
  15077. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15078. (void) duk_push_heapptr(thr, ptr); /* NULL -> 'undefined' */
  15079. return duk_has_prop(thr, obj_idx);
  15080. }
  15081. DUK_INTERNAL duk_bool_t duk_has_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx) {
  15082. DUK_ASSERT_API_ENTRY(thr);
  15083. DUK_ASSERT_STRIDX_VALID(stridx);
  15084. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15085. duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
  15086. return duk_has_prop(thr, obj_idx);
  15087. }
  15088. #if 0
  15089. DUK_INTERNAL duk_bool_t duk_has_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args) {
  15090. return duk_has_prop_stridx(thr, (duk_idx_t) (duk_int16_t) (packed_args >> 16),
  15091. (duk_small_uint_t) (packed_args & 0xffffUL));
  15092. }
  15093. #endif
  15094. /* Define own property without inheritance lookups and such. This differs from
  15095. * [[DefineOwnProperty]] because special behaviors (like Array 'length') are
  15096. * not invoked by this method. The caller must be careful to invoke any such
  15097. * behaviors if necessary.
  15098. */
  15099. DUK_INTERNAL void duk_xdef_prop(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t desc_flags) {
  15100. duk_hobject *obj;
  15101. duk_hstring *key;
  15102. DUK_ASSERT_API_ENTRY(thr);
  15103. obj = duk_require_hobject(thr, obj_idx);
  15104. DUK_ASSERT(obj != NULL);
  15105. key = duk_to_property_key_hstring(thr, -2);
  15106. DUK_ASSERT(key != NULL);
  15107. DUK_ASSERT(duk_require_tval(thr, -1) != NULL);
  15108. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  15109. duk_pop(thr); /* pop key */
  15110. }
  15111. DUK_INTERNAL void duk_xdef_prop_index(duk_hthread *thr, duk_idx_t obj_idx, duk_uarridx_t arr_idx, duk_small_uint_t desc_flags) {
  15112. duk_hobject *obj;
  15113. DUK_ASSERT_API_ENTRY(thr);
  15114. obj = duk_require_hobject(thr, obj_idx);
  15115. DUK_ASSERT(obj != NULL);
  15116. duk_hobject_define_property_internal_arridx(thr, obj, arr_idx, desc_flags);
  15117. /* value popped by call */
  15118. }
  15119. DUK_INTERNAL void duk_xdef_prop_stridx(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx, duk_small_uint_t desc_flags) {
  15120. duk_hobject *obj;
  15121. duk_hstring *key;
  15122. DUK_ASSERT_API_ENTRY(thr);
  15123. DUK_ASSERT_STRIDX_VALID(stridx);
  15124. obj = duk_require_hobject(thr, obj_idx);
  15125. DUK_ASSERT(obj != NULL);
  15126. key = DUK_HTHREAD_GET_STRING(thr, stridx);
  15127. DUK_ASSERT(key != NULL);
  15128. DUK_ASSERT(duk_require_tval(thr, -1) != NULL);
  15129. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  15130. /* value popped by call */
  15131. }
  15132. DUK_INTERNAL void duk_xdef_prop_stridx_short_raw(duk_hthread *thr, duk_uint_t packed_args) {
  15133. duk_xdef_prop_stridx(thr, (duk_idx_t) (duk_int8_t) (packed_args >> 24),
  15134. (duk_small_uint_t) (packed_args >> 8) & 0xffffUL,
  15135. (duk_small_uint_t) (packed_args & 0xffL));
  15136. }
  15137. #if 0 /*unused*/
  15138. DUK_INTERNAL void duk_xdef_prop_stridx_builtin(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx, duk_small_int_t builtin_idx, duk_small_uint_t desc_flags) {
  15139. duk_hobject *obj;
  15140. duk_hstring *key;
  15141. DUK_ASSERT_API_ENTRY(thr);
  15142. DUK_ASSERT_STRIDX_VALID(stridx);
  15143. DUK_ASSERT_BIDX_VALID(builtin_idx);
  15144. obj = duk_require_hobject(thr, obj_idx);
  15145. DUK_ASSERT(obj != NULL);
  15146. key = DUK_HTHREAD_GET_STRING(thr, stridx);
  15147. DUK_ASSERT(key != NULL);
  15148. duk_push_hobject(thr, thr->builtins[builtin_idx]);
  15149. duk_hobject_define_property_internal(thr, obj, key, desc_flags);
  15150. /* value popped by call */
  15151. }
  15152. #endif
  15153. /* This is a rare property helper; it sets the global thrower (E5 Section 13.2.3)
  15154. * setter/getter into an object property. This is needed by the 'arguments'
  15155. * object creation code, function instance creation code, and Function.prototype.bind().
  15156. */
  15157. DUK_INTERNAL void duk_xdef_prop_stridx_thrower(duk_hthread *thr, duk_idx_t obj_idx, duk_small_uint_t stridx) {
  15158. DUK_ASSERT_API_ENTRY(thr);
  15159. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15160. duk_push_hstring_stridx(thr, stridx);
  15161. duk_push_hobject_bidx(thr, DUK_BIDX_TYPE_ERROR_THROWER);
  15162. duk_dup_top(thr);
  15163. duk_def_prop(thr, obj_idx, DUK_DEFPROP_HAVE_SETTER | DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_FORCE); /* attributes always 0 */
  15164. }
  15165. /* Object.getOwnPropertyDescriptor() equivalent C binding. */
  15166. DUK_EXTERNAL void duk_get_prop_desc(duk_hthread *thr, duk_idx_t obj_idx, duk_uint_t flags) {
  15167. DUK_ASSERT_API_ENTRY(thr);
  15168. DUK_UNREF(flags); /* no flags defined yet */
  15169. duk_hobject_object_get_own_property_descriptor(thr, obj_idx); /* [ ... key ] -> [ ... desc ] */
  15170. }
  15171. /* Object.defineProperty() equivalent C binding. */
  15172. DUK_EXTERNAL void duk_def_prop(duk_hthread *thr, duk_idx_t obj_idx, duk_uint_t flags) {
  15173. duk_idx_t idx_base;
  15174. duk_hobject *obj;
  15175. duk_hstring *key;
  15176. duk_idx_t idx_value;
  15177. duk_hobject *get;
  15178. duk_hobject *set;
  15179. duk_uint_t is_data_desc;
  15180. duk_uint_t is_acc_desc;
  15181. DUK_ASSERT_API_ENTRY(thr);
  15182. obj = duk_require_hobject(thr, obj_idx);
  15183. is_data_desc = flags & (DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
  15184. is_acc_desc = flags & (DUK_DEFPROP_HAVE_GETTER | DUK_DEFPROP_HAVE_SETTER);
  15185. if (is_data_desc && is_acc_desc) {
  15186. /* "Have" flags must not be conflicting so that they would
  15187. * apply to both a plain property and an accessor at the same
  15188. * time.
  15189. */
  15190. goto fail_invalid_desc;
  15191. }
  15192. idx_base = duk_get_top_index(thr);
  15193. if (flags & DUK_DEFPROP_HAVE_SETTER) {
  15194. duk_require_type_mask(thr, idx_base, DUK_TYPE_MASK_UNDEFINED |
  15195. DUK_TYPE_MASK_OBJECT |
  15196. DUK_TYPE_MASK_LIGHTFUNC);
  15197. set = duk_get_hobject_promote_lfunc(thr, idx_base);
  15198. if (set != NULL && !DUK_HOBJECT_IS_CALLABLE(set)) {
  15199. goto fail_not_callable;
  15200. }
  15201. idx_base--;
  15202. } else {
  15203. set = NULL;
  15204. }
  15205. if (flags & DUK_DEFPROP_HAVE_GETTER) {
  15206. duk_require_type_mask(thr, idx_base, DUK_TYPE_MASK_UNDEFINED |
  15207. DUK_TYPE_MASK_OBJECT |
  15208. DUK_TYPE_MASK_LIGHTFUNC);
  15209. get = duk_get_hobject_promote_lfunc(thr, idx_base);
  15210. if (get != NULL && !DUK_HOBJECT_IS_CALLABLE(get)) {
  15211. goto fail_not_callable;
  15212. }
  15213. idx_base--;
  15214. } else {
  15215. get = NULL;
  15216. }
  15217. if (flags & DUK_DEFPROP_HAVE_VALUE) {
  15218. idx_value = idx_base;
  15219. idx_base--;
  15220. } else {
  15221. idx_value = (duk_idx_t) -1;
  15222. }
  15223. key = duk_to_property_key_hstring(thr, idx_base);
  15224. DUK_ASSERT(key != NULL);
  15225. duk_require_valid_index(thr, idx_base);
  15226. duk_hobject_define_property_helper(thr,
  15227. flags /*defprop_flags*/,
  15228. obj,
  15229. key,
  15230. idx_value,
  15231. get,
  15232. set,
  15233. 1 /*throw_flag*/);
  15234. /* Clean up stack */
  15235. duk_set_top(thr, idx_base);
  15236. /* [ ... obj ... ] */
  15237. return;
  15238. fail_invalid_desc:
  15239. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_DESCRIPTOR);
  15240. DUK_WO_NORETURN(return;);
  15241. fail_not_callable:
  15242. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CALLABLE);
  15243. DUK_WO_NORETURN(return;);
  15244. }
  15245. /*
  15246. * Object related
  15247. *
  15248. * Note: seal() and freeze() are accessible through ECMAScript bindings,
  15249. * and are not exposed through the API.
  15250. */
  15251. DUK_EXTERNAL void duk_compact(duk_hthread *thr, duk_idx_t obj_idx) {
  15252. duk_hobject *obj;
  15253. DUK_ASSERT_API_ENTRY(thr);
  15254. obj = duk_get_hobject(thr, obj_idx);
  15255. if (obj) {
  15256. /* Note: this may fail, caller should protect the call if necessary */
  15257. duk_hobject_compact_props(thr, obj);
  15258. }
  15259. }
  15260. DUK_INTERNAL void duk_compact_m1(duk_hthread *thr) {
  15261. DUK_ASSERT_API_ENTRY(thr);
  15262. duk_compact(thr, -1);
  15263. }
  15264. /* XXX: the duk_hobject_enum.c stack APIs should be reworked */
  15265. DUK_EXTERNAL void duk_enum(duk_hthread *thr, duk_idx_t obj_idx, duk_uint_t enum_flags) {
  15266. DUK_ASSERT_API_ENTRY(thr);
  15267. duk_dup(thr, obj_idx);
  15268. duk_require_hobject_promote_mask(thr, -1, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  15269. duk_hobject_enumerator_create(thr, enum_flags); /* [target] -> [enum] */
  15270. }
  15271. DUK_EXTERNAL duk_bool_t duk_next(duk_hthread *thr, duk_idx_t enum_index, duk_bool_t get_value) {
  15272. DUK_ASSERT_API_ENTRY(thr);
  15273. duk_require_hobject(thr, enum_index);
  15274. duk_dup(thr, enum_index);
  15275. return duk_hobject_enumerator_next(thr, get_value);
  15276. }
  15277. DUK_INTERNAL void duk_seal_freeze_raw(duk_hthread *thr, duk_idx_t obj_idx, duk_bool_t is_freeze) {
  15278. duk_tval *tv;
  15279. duk_hobject *h;
  15280. DUK_ASSERT_API_ENTRY(thr);
  15281. tv = duk_require_tval(thr, obj_idx);
  15282. DUK_ASSERT(tv != NULL);
  15283. /* Seal/freeze are quite rare in practice so it'd be nice to get the
  15284. * correct behavior simply via automatic promotion (at the cost of some
  15285. * memory churn). However, the promoted objects don't behave the same,
  15286. * e.g. promoted lightfuncs are extensible.
  15287. */
  15288. switch (DUK_TVAL_GET_TAG(tv)) {
  15289. case DUK_TAG_BUFFER:
  15290. /* Plain buffer: already sealed, but not frozen (and can't be frozen
  15291. * because index properties can't be made non-writable.
  15292. */
  15293. if (is_freeze) {
  15294. goto fail_cannot_freeze;
  15295. }
  15296. break;
  15297. case DUK_TAG_LIGHTFUNC:
  15298. /* Lightfunc: already sealed and frozen, success. */
  15299. break;
  15300. case DUK_TAG_OBJECT:
  15301. h = DUK_TVAL_GET_OBJECT(tv);
  15302. DUK_ASSERT(h != NULL);
  15303. if (is_freeze && DUK_HOBJECT_IS_BUFOBJ(h)) {
  15304. /* Buffer objects cannot be frozen because there's no internal
  15305. * support for making virtual array indices non-writable.
  15306. */
  15307. DUK_DD(DUK_DDPRINT("cannot freeze a buffer object"));
  15308. goto fail_cannot_freeze;
  15309. }
  15310. duk_hobject_object_seal_freeze_helper(thr, h, is_freeze);
  15311. /* Sealed and frozen objects cannot gain any more properties,
  15312. * so this is a good time to compact them.
  15313. */
  15314. duk_hobject_compact_props(thr, h);
  15315. break;
  15316. default:
  15317. /* ES2015 Sections 19.1.2.5, 19.1.2.17 */
  15318. break;
  15319. }
  15320. return;
  15321. fail_cannot_freeze:
  15322. DUK_ERROR_TYPE_INVALID_ARGS(thr); /* XXX: proper error message */
  15323. DUK_WO_NORETURN(return;);
  15324. }
  15325. DUK_EXTERNAL void duk_seal(duk_hthread *thr, duk_idx_t obj_idx) {
  15326. DUK_ASSERT_API_ENTRY(thr);
  15327. duk_seal_freeze_raw(thr, obj_idx, 0 /*is_freeze*/);
  15328. }
  15329. DUK_EXTERNAL void duk_freeze(duk_hthread *thr, duk_idx_t obj_idx) {
  15330. DUK_ASSERT_API_ENTRY(thr);
  15331. duk_seal_freeze_raw(thr, obj_idx, 1 /*is_freeze*/);
  15332. }
  15333. /*
  15334. * Helpers for writing multiple properties
  15335. */
  15336. DUK_EXTERNAL void duk_put_function_list(duk_hthread *thr, duk_idx_t obj_idx, const duk_function_list_entry *funcs) {
  15337. const duk_function_list_entry *ent = funcs;
  15338. DUK_ASSERT_API_ENTRY(thr);
  15339. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15340. if (ent != NULL) {
  15341. while (ent->key != NULL) {
  15342. duk_push_c_function(thr, ent->value, ent->nargs);
  15343. duk_put_prop_string(thr, obj_idx, ent->key);
  15344. ent++;
  15345. }
  15346. }
  15347. }
  15348. DUK_EXTERNAL void duk_put_number_list(duk_hthread *thr, duk_idx_t obj_idx, const duk_number_list_entry *numbers) {
  15349. const duk_number_list_entry *ent = numbers;
  15350. duk_tval *tv;
  15351. DUK_ASSERT_API_ENTRY(thr);
  15352. obj_idx = duk_require_normalize_index(thr, obj_idx);
  15353. if (ent != NULL) {
  15354. while (ent->key != NULL) {
  15355. tv = thr->valstack_top++;
  15356. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv)); /* value stack init policy */
  15357. DUK_TVAL_SET_NUMBER_CHKFAST_SLOW(tv, ent->value); /* no need for decref/incref */
  15358. duk_put_prop_string(thr, obj_idx, ent->key);
  15359. ent++;
  15360. }
  15361. }
  15362. }
  15363. /*
  15364. * Shortcut for accessing global object properties
  15365. */
  15366. DUK_EXTERNAL duk_bool_t duk_get_global_string(duk_hthread *thr, const char *key) {
  15367. duk_bool_t ret;
  15368. DUK_ASSERT_API_ENTRY(thr);
  15369. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15370. /* XXX: direct implementation */
  15371. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15372. ret = duk_get_prop_string(thr, -1, key);
  15373. duk_remove_m2(thr);
  15374. return ret;
  15375. }
  15376. DUK_EXTERNAL duk_bool_t duk_get_global_lstring(duk_hthread *thr, const char *key, duk_size_t key_len) {
  15377. duk_bool_t ret;
  15378. DUK_ASSERT_API_ENTRY(thr);
  15379. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15380. /* XXX: direct implementation */
  15381. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15382. ret = duk_get_prop_lstring(thr, -1, key, key_len);
  15383. duk_remove_m2(thr);
  15384. return ret;
  15385. }
  15386. #if !defined(DUK_USE_PREFER_SIZE)
  15387. DUK_EXTERNAL duk_bool_t duk_get_global_literal_raw(duk_hthread *thr, const char *key, duk_size_t key_len) {
  15388. duk_bool_t ret;
  15389. DUK_ASSERT_API_ENTRY(thr);
  15390. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15391. DUK_ASSERT(key[key_len] == (char) 0);
  15392. /* XXX: direct implementation */
  15393. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15394. ret = duk_get_prop_literal_raw(thr, -1, key, key_len);
  15395. duk_remove_m2(thr);
  15396. return ret;
  15397. }
  15398. #endif
  15399. DUK_EXTERNAL duk_bool_t duk_get_global_heapptr(duk_hthread *thr, void *ptr) {
  15400. duk_bool_t ret;
  15401. DUK_ASSERT_API_ENTRY(thr);
  15402. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15403. /* XXX: direct implementation */
  15404. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15405. ret = duk_get_prop_heapptr(thr, -1, ptr);
  15406. duk_remove_m2(thr);
  15407. return ret;
  15408. }
  15409. DUK_EXTERNAL duk_bool_t duk_put_global_string(duk_hthread *thr, const char *key) {
  15410. duk_bool_t ret;
  15411. DUK_ASSERT_API_ENTRY(thr);
  15412. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15413. /* XXX: direct implementation */
  15414. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15415. duk_insert(thr, -2);
  15416. ret = duk_put_prop_string(thr, -2, key); /* [ ... global val ] -> [ ... global ] */
  15417. duk_pop(thr);
  15418. return ret;
  15419. }
  15420. DUK_EXTERNAL duk_bool_t duk_put_global_lstring(duk_hthread *thr, const char *key, duk_size_t key_len) {
  15421. duk_bool_t ret;
  15422. DUK_ASSERT_API_ENTRY(thr);
  15423. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15424. /* XXX: direct implementation */
  15425. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15426. duk_insert(thr, -2);
  15427. ret = duk_put_prop_lstring(thr, -2, key, key_len); /* [ ... global val ] -> [ ... global ] */
  15428. duk_pop(thr);
  15429. return ret;
  15430. }
  15431. #if !defined(DUK_USE_PREFER_SIZE)
  15432. DUK_EXTERNAL duk_bool_t duk_put_global_literal_raw(duk_hthread *thr, const char *key, duk_size_t key_len) {
  15433. duk_bool_t ret;
  15434. DUK_ASSERT_API_ENTRY(thr);
  15435. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15436. DUK_ASSERT(key[key_len] == (char) 0);
  15437. /* XXX: direct implementation */
  15438. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15439. duk_insert(thr, -2);
  15440. ret = duk_put_prop_literal_raw(thr, -2, key, key_len); /* [ ... global val ] -> [ ... global ] */
  15441. duk_pop(thr);
  15442. return ret;
  15443. }
  15444. #endif
  15445. DUK_EXTERNAL duk_bool_t duk_put_global_heapptr(duk_hthread *thr, void *ptr) {
  15446. duk_bool_t ret;
  15447. DUK_ASSERT_API_ENTRY(thr);
  15448. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  15449. /* XXX: direct implementation */
  15450. duk_push_hobject(thr, thr->builtins[DUK_BIDX_GLOBAL]);
  15451. duk_insert(thr, -2);
  15452. ret = duk_put_prop_heapptr(thr, -2, ptr); /* [ ... global val ] -> [ ... global ] */
  15453. duk_pop(thr);
  15454. return ret;
  15455. }
  15456. /*
  15457. * ES2015 GetMethod()
  15458. */
  15459. DUK_INTERNAL duk_bool_t duk_get_method_stridx(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t stridx) {
  15460. (void) duk_get_prop_stridx(thr, idx, stridx);
  15461. if (duk_is_null_or_undefined(thr, -1)) {
  15462. duk_pop_nodecref_unsafe(thr);
  15463. return 0;
  15464. }
  15465. if (!duk_is_callable(thr, -1)) {
  15466. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CALLABLE);
  15467. DUK_WO_NORETURN(return 0;);
  15468. }
  15469. return 1;
  15470. }
  15471. /*
  15472. * Object prototype
  15473. */
  15474. DUK_EXTERNAL void duk_get_prototype(duk_hthread *thr, duk_idx_t idx) {
  15475. duk_hobject *obj;
  15476. duk_hobject *proto;
  15477. DUK_ASSERT_API_ENTRY(thr);
  15478. obj = duk_require_hobject(thr, idx);
  15479. DUK_ASSERT(obj != NULL);
  15480. /* XXX: shared helper for duk_push_hobject_or_undefined()? */
  15481. proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, obj);
  15482. if (proto) {
  15483. duk_push_hobject(thr, proto);
  15484. } else {
  15485. duk_push_undefined(thr);
  15486. }
  15487. }
  15488. DUK_EXTERNAL void duk_set_prototype(duk_hthread *thr, duk_idx_t idx) {
  15489. duk_hobject *obj;
  15490. duk_hobject *proto;
  15491. DUK_ASSERT_API_ENTRY(thr);
  15492. obj = duk_require_hobject(thr, idx);
  15493. DUK_ASSERT(obj != NULL);
  15494. duk_require_type_mask(thr, -1, DUK_TYPE_MASK_UNDEFINED |
  15495. DUK_TYPE_MASK_OBJECT);
  15496. proto = duk_get_hobject(thr, -1);
  15497. /* proto can also be NULL here (allowed explicitly) */
  15498. #if defined(DUK_USE_ROM_OBJECTS)
  15499. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) {
  15500. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE); /* XXX: "read only object"? */
  15501. DUK_WO_NORETURN(return;);
  15502. }
  15503. #endif
  15504. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, obj, proto);
  15505. duk_pop(thr);
  15506. }
  15507. /*
  15508. * Object finalizer
  15509. */
  15510. #if defined(DUK_USE_FINALIZER_SUPPORT)
  15511. /* XXX: these could be implemented as macros calling an internal function
  15512. * directly.
  15513. * XXX: same issue as with Duktape.fin: there's no way to delete the property
  15514. * now (just set it to undefined).
  15515. */
  15516. DUK_EXTERNAL void duk_get_finalizer(duk_hthread *thr, duk_idx_t idx) {
  15517. DUK_ASSERT_API_ENTRY(thr);
  15518. duk_get_prop_stridx(thr, idx, DUK_STRIDX_INT_FINALIZER);
  15519. }
  15520. DUK_EXTERNAL void duk_set_finalizer(duk_hthread *thr, duk_idx_t idx) {
  15521. duk_hobject *h;
  15522. duk_bool_t callable;
  15523. DUK_ASSERT_API_ENTRY(thr);
  15524. h = duk_require_hobject(thr, idx); /* Get before 'put' so that 'idx' is correct. */
  15525. callable = duk_is_callable(thr, -1);
  15526. duk_put_prop_stridx(thr, idx, DUK_STRIDX_INT_FINALIZER);
  15527. /* In addition to setting the finalizer property, keep a "have
  15528. * finalizer" flag in duk_hobject in sync so that refzero can do
  15529. * a very quick finalizer check by walking the prototype chain
  15530. * and checking the flag alone. (Note that this means that just
  15531. * setting _Finalizer on an object won't affect finalizer checks.)
  15532. *
  15533. * NOTE: if the argument is a Proxy object, this flag will be set
  15534. * on the Proxy, not the target. As a result, the target won't get
  15535. * a finalizer flag and the Proxy also won't be finalized as there's
  15536. * an explicit Proxy check in finalization now.
  15537. */
  15538. if (callable) {
  15539. DUK_HOBJECT_SET_HAVE_FINALIZER(h);
  15540. } else {
  15541. DUK_HOBJECT_CLEAR_HAVE_FINALIZER(h);
  15542. }
  15543. }
  15544. #else /* DUK_USE_FINALIZER_SUPPORT */
  15545. DUK_EXTERNAL void duk_get_finalizer(duk_hthread *thr, duk_idx_t idx) {
  15546. DUK_ASSERT_API_ENTRY(thr);
  15547. DUK_UNREF(idx);
  15548. DUK_ERROR_UNSUPPORTED(thr);
  15549. DUK_WO_NORETURN(return;);
  15550. }
  15551. DUK_EXTERNAL void duk_set_finalizer(duk_hthread *thr, duk_idx_t idx) {
  15552. DUK_ASSERT_API_ENTRY(thr);
  15553. DUK_UNREF(idx);
  15554. DUK_ERROR_UNSUPPORTED(thr);
  15555. DUK_WO_NORETURN(return;);
  15556. }
  15557. #endif /* DUK_USE_FINALIZER_SUPPORT */
  15558. #line 1 "duk_api_random.c"
  15559. /*
  15560. * Random numbers
  15561. */
  15562. /* #include duk_internal.h -> already included */
  15563. DUK_EXTERNAL duk_double_t duk_random(duk_hthread *thr) {
  15564. return (duk_double_t) DUK_UTIL_GET_RANDOM_DOUBLE(thr);
  15565. }
  15566. #line 1 "duk_api_stack.c"
  15567. /*
  15568. * API calls related to general value stack manipulation: resizing the value
  15569. * stack, pushing and popping values, type checking and reading values,
  15570. * coercing values, etc.
  15571. *
  15572. * Also contains internal functions (such as duk_get_tval()), defined
  15573. * in duk_api_internal.h, with semantics similar to the public API.
  15574. */
  15575. /* XXX: repetition of stack pre-checks -> helper or macro or inline */
  15576. /* XXX: shared api error strings, and perhaps even throw code for rare cases? */
  15577. /* #include duk_internal.h -> already included */
  15578. /*
  15579. * Forward declarations
  15580. */
  15581. DUK_LOCAL_DECL duk_idx_t duk__push_c_function_raw(duk_hthread *thr, duk_c_function func, duk_idx_t nargs, duk_uint_t flags, duk_small_uint_t proto_bidx);
  15582. /*
  15583. * Global state for working around missing variadic macros
  15584. */
  15585. #if !defined(DUK_USE_VARIADIC_MACROS)
  15586. DUK_EXTERNAL const char *duk_api_global_filename = NULL;
  15587. DUK_EXTERNAL duk_int_t duk_api_global_line = 0;
  15588. #endif
  15589. /*
  15590. * Misc helpers
  15591. */
  15592. DUK_LOCAL const char * const duk__symbol_type_strings[4] = {
  15593. "hidden", "global", "local", "wellknown"
  15594. };
  15595. #if !defined(DUK_USE_PACKED_TVAL)
  15596. DUK_LOCAL const duk_uint_t duk__type_from_tag[] = {
  15597. DUK_TYPE_NUMBER,
  15598. DUK_TYPE_NUMBER, /* fastint */
  15599. DUK_TYPE_UNDEFINED,
  15600. DUK_TYPE_NULL,
  15601. DUK_TYPE_BOOLEAN,
  15602. DUK_TYPE_POINTER,
  15603. DUK_TYPE_LIGHTFUNC,
  15604. DUK_TYPE_NONE,
  15605. DUK_TYPE_STRING,
  15606. DUK_TYPE_OBJECT,
  15607. DUK_TYPE_BUFFER,
  15608. };
  15609. DUK_LOCAL const duk_uint_t duk__type_mask_from_tag[] = {
  15610. DUK_TYPE_MASK_NUMBER,
  15611. DUK_TYPE_MASK_NUMBER, /* fastint */
  15612. DUK_TYPE_MASK_UNDEFINED,
  15613. DUK_TYPE_MASK_NULL,
  15614. DUK_TYPE_MASK_BOOLEAN,
  15615. DUK_TYPE_MASK_POINTER,
  15616. DUK_TYPE_MASK_LIGHTFUNC,
  15617. DUK_TYPE_MASK_NONE,
  15618. DUK_TYPE_MASK_STRING,
  15619. DUK_TYPE_MASK_OBJECT,
  15620. DUK_TYPE_MASK_BUFFER,
  15621. };
  15622. #endif /* !DUK_USE_PACKED_TVAL */
  15623. /* Assert that there's room for one value. */
  15624. #define DUK__ASSERT_SPACE() do { \
  15625. DUK_ASSERT(!(thr->valstack_top >= thr->valstack_end)); \
  15626. } while (0)
  15627. /* Check that there's room to push one value. */
  15628. #if defined(DUK_USE_VALSTACK_UNSAFE)
  15629. /* Faster but value stack overruns are memory unsafe. */
  15630. #define DUK__CHECK_SPACE() DUK__ASSERT_SPACE()
  15631. #else
  15632. #define DUK__CHECK_SPACE() do { \
  15633. if (DUK_UNLIKELY(thr->valstack_top >= thr->valstack_end)) { \
  15634. DUK_ERROR_RANGE_PUSH_BEYOND(thr); \
  15635. } \
  15636. } while (0)
  15637. #endif
  15638. DUK_LOCAL duk_small_uint_t duk__get_symbol_type(duk_hstring *h) {
  15639. const duk_uint8_t *data;
  15640. duk_size_t len;
  15641. DUK_ASSERT(h != NULL);
  15642. DUK_ASSERT(DUK_HSTRING_HAS_SYMBOL(h));
  15643. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(h) >= 1); /* always true, symbol prefix */
  15644. data = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  15645. len = DUK_HSTRING_GET_BYTELEN(h);
  15646. DUK_ASSERT(len >= 1);
  15647. /* XXX: differentiate between 0x82 and 0xff (hidden vs. internal?)? */
  15648. if (data[0] == 0xffU) {
  15649. return DUK_SYMBOL_TYPE_HIDDEN;
  15650. } else if (data[0] == 0x82U) {
  15651. return DUK_SYMBOL_TYPE_HIDDEN;
  15652. } else if (data[0] == 0x80U) {
  15653. return DUK_SYMBOL_TYPE_GLOBAL;
  15654. } else if (data[len - 1] != 0xffU) {
  15655. return DUK_SYMBOL_TYPE_LOCAL;
  15656. } else {
  15657. return DUK_SYMBOL_TYPE_WELLKNOWN;
  15658. }
  15659. }
  15660. DUK_LOCAL const char *duk__get_symbol_type_string(duk_hstring *h) {
  15661. duk_small_uint_t idx;
  15662. idx = duk__get_symbol_type(h);
  15663. DUK_ASSERT(idx < sizeof(duk__symbol_type_strings));
  15664. return duk__symbol_type_strings[idx];
  15665. }
  15666. DUK_LOCAL_DECL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_hthread *thr, duk_idx_t idx, duk_uint_t tag);
  15667. DUK_LOCAL duk_int_t duk__api_coerce_d2i(duk_hthread *thr, duk_idx_t idx, duk_int_t def_value, duk_bool_t require) {
  15668. duk_tval *tv;
  15669. duk_small_int_t c;
  15670. duk_double_t d;
  15671. tv = duk_get_tval_or_unused(thr, idx);
  15672. DUK_ASSERT(tv != NULL);
  15673. /*
  15674. * Special cases like NaN and +/- Infinity are handled explicitly
  15675. * because a plain C coercion from double to int handles these cases
  15676. * in undesirable ways. For instance, NaN may coerce to INT_MIN
  15677. * (not zero), and INT_MAX + 1 may coerce to INT_MIN (not INT_MAX).
  15678. *
  15679. * This double-to-int coercion differs from ToInteger() because it
  15680. * has a finite range (ToInteger() allows e.g. +/- Infinity). It
  15681. * also differs from ToInt32() because the INT_MIN/INT_MAX clamping
  15682. * depends on the size of the int type on the platform. In particular,
  15683. * on platforms with a 64-bit int type, the full range is allowed.
  15684. */
  15685. #if defined(DUK_USE_FASTINT)
  15686. if (DUK_TVAL_IS_FASTINT(tv)) {
  15687. duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
  15688. #if (DUK_INT_MAX <= 0x7fffffffL)
  15689. /* Clamping only necessary for 32-bit ints. */
  15690. if (t < DUK_INT_MIN) {
  15691. t = DUK_INT_MIN;
  15692. } else if (t > DUK_INT_MAX) {
  15693. t = DUK_INT_MAX;
  15694. }
  15695. #endif
  15696. return (duk_int_t) t;
  15697. }
  15698. #endif
  15699. if (DUK_TVAL_IS_NUMBER(tv)) {
  15700. d = DUK_TVAL_GET_NUMBER(tv);
  15701. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  15702. if (c == DUK_FP_NAN) {
  15703. return 0;
  15704. } else if (d < (duk_double_t) DUK_INT_MIN) {
  15705. /* covers -Infinity */
  15706. return DUK_INT_MIN;
  15707. } else if (d > (duk_double_t) DUK_INT_MAX) {
  15708. /* covers +Infinity */
  15709. return DUK_INT_MAX;
  15710. } else {
  15711. /* coerce towards zero */
  15712. return (duk_int_t) d;
  15713. }
  15714. }
  15715. if (require) {
  15716. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "number", DUK_STR_NOT_NUMBER);
  15717. DUK_WO_NORETURN(return 0;);
  15718. }
  15719. return def_value;
  15720. }
  15721. DUK_LOCAL duk_uint_t duk__api_coerce_d2ui(duk_hthread *thr, duk_idx_t idx, duk_uint_t def_value, duk_bool_t require) {
  15722. duk_tval *tv;
  15723. duk_small_int_t c;
  15724. duk_double_t d;
  15725. /* Same as above but for unsigned int range. */
  15726. tv = duk_get_tval_or_unused(thr, idx);
  15727. DUK_ASSERT(tv != NULL);
  15728. #if defined(DUK_USE_FASTINT)
  15729. if (DUK_TVAL_IS_FASTINT(tv)) {
  15730. duk_int64_t t = DUK_TVAL_GET_FASTINT(tv);
  15731. if (t < 0) {
  15732. t = 0;
  15733. }
  15734. #if (DUK_UINT_MAX <= 0xffffffffUL)
  15735. /* Clamping only necessary for 32-bit ints. */
  15736. else if (t > DUK_UINT_MAX) {
  15737. t = DUK_UINT_MAX;
  15738. }
  15739. #endif
  15740. return (duk_uint_t) t;
  15741. }
  15742. #endif
  15743. if (DUK_TVAL_IS_NUMBER(tv)) {
  15744. d = DUK_TVAL_GET_NUMBER(tv);
  15745. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  15746. if (c == DUK_FP_NAN) {
  15747. return 0;
  15748. } else if (d < 0.0) {
  15749. /* covers -Infinity */
  15750. return (duk_uint_t) 0;
  15751. } else if (d > (duk_double_t) DUK_UINT_MAX) {
  15752. /* covers +Infinity */
  15753. return (duk_uint_t) DUK_UINT_MAX;
  15754. } else {
  15755. /* coerce towards zero */
  15756. return (duk_uint_t) d;
  15757. }
  15758. }
  15759. if (require) {
  15760. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "number", DUK_STR_NOT_NUMBER);
  15761. DUK_WO_NORETURN(return 0;);
  15762. }
  15763. return def_value;
  15764. }
  15765. /*
  15766. * Stack index validation/normalization and getting a stack duk_tval ptr.
  15767. *
  15768. * These are called by many API entrypoints so the implementations must be
  15769. * fast and "inlined".
  15770. *
  15771. * There's some repetition because of this; keep the functions in sync.
  15772. */
  15773. DUK_EXTERNAL duk_idx_t duk_normalize_index(duk_hthread *thr, duk_idx_t idx) {
  15774. duk_uidx_t vs_size;
  15775. duk_uidx_t uidx;
  15776. DUK_ASSERT_API_ENTRY(thr);
  15777. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15778. /* Care must be taken to avoid pointer wrapping in the index
  15779. * validation. For instance, on a 32-bit platform with 8-byte
  15780. * duk_tval the index 0x20000000UL would wrap the memory space
  15781. * once.
  15782. */
  15783. /* Assume value stack sizes (in elements) fits into duk_idx_t. */
  15784. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  15785. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  15786. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  15787. if (idx < 0) {
  15788. uidx = vs_size + (duk_uidx_t) idx;
  15789. } else {
  15790. /* since index non-negative */
  15791. DUK_ASSERT(idx != DUK_INVALID_INDEX);
  15792. uidx = (duk_uidx_t) idx;
  15793. }
  15794. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  15795. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  15796. if (DUK_LIKELY(uidx < vs_size)) {
  15797. return (duk_idx_t) uidx;
  15798. }
  15799. return DUK_INVALID_INDEX;
  15800. }
  15801. DUK_EXTERNAL duk_idx_t duk_require_normalize_index(duk_hthread *thr, duk_idx_t idx) {
  15802. duk_uidx_t vs_size;
  15803. duk_uidx_t uidx;
  15804. DUK_ASSERT_API_ENTRY(thr);
  15805. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15806. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  15807. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  15808. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  15809. if (idx < 0) {
  15810. uidx = vs_size + (duk_uidx_t) idx;
  15811. } else {
  15812. DUK_ASSERT(idx != DUK_INVALID_INDEX);
  15813. uidx = (duk_uidx_t) idx;
  15814. }
  15815. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  15816. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  15817. if (DUK_LIKELY(uidx < vs_size)) {
  15818. return (duk_idx_t) uidx;
  15819. }
  15820. DUK_ERROR_RANGE_INDEX(thr, idx);
  15821. DUK_WO_NORETURN(return 0;);
  15822. }
  15823. DUK_INTERNAL duk_tval *duk_get_tval(duk_hthread *thr, duk_idx_t idx) {
  15824. duk_uidx_t vs_size;
  15825. duk_uidx_t uidx;
  15826. DUK_ASSERT_API_ENTRY(thr);
  15827. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15828. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  15829. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  15830. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  15831. if (idx < 0) {
  15832. uidx = vs_size + (duk_uidx_t) idx;
  15833. } else {
  15834. DUK_ASSERT(idx != DUK_INVALID_INDEX);
  15835. uidx = (duk_uidx_t) idx;
  15836. }
  15837. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  15838. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  15839. if (DUK_LIKELY(uidx < vs_size)) {
  15840. return thr->valstack_bottom + uidx;
  15841. }
  15842. return NULL;
  15843. }
  15844. /* Variant of duk_get_tval() which is guaranteed to return a valid duk_tval
  15845. * pointer. When duk_get_tval() would return NULL, this variant returns a
  15846. * pointer to a duk_tval with tag DUK_TAG_UNUSED. This allows the call site
  15847. * to avoid an unnecessary NULL check which sometimes leads to better code.
  15848. * The return duk_tval is read only (at least for the UNUSED value).
  15849. */
  15850. DUK_LOCAL const duk_tval_unused duk__const_tval_unused = DUK_TVAL_UNUSED_INITIALIZER();
  15851. DUK_INTERNAL duk_tval *duk_get_tval_or_unused(duk_hthread *thr, duk_idx_t idx) {
  15852. duk_tval *tv;
  15853. DUK_ASSERT_API_ENTRY(thr);
  15854. tv = duk_get_tval(thr, idx);
  15855. if (tv != NULL) {
  15856. return tv;
  15857. }
  15858. return (duk_tval *) DUK_LOSE_CONST(&duk__const_tval_unused);
  15859. }
  15860. DUK_INTERNAL duk_tval *duk_require_tval(duk_hthread *thr, duk_idx_t idx) {
  15861. duk_uidx_t vs_size;
  15862. duk_uidx_t uidx;
  15863. DUK_ASSERT_API_ENTRY(thr);
  15864. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15865. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  15866. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  15867. DUK_ASSERT_DISABLE(vs_size >= 0); /* unsigned */
  15868. /* Use unsigned arithmetic to optimize comparison. */
  15869. if (idx < 0) {
  15870. uidx = vs_size + (duk_uidx_t) idx;
  15871. } else {
  15872. DUK_ASSERT(idx != DUK_INVALID_INDEX);
  15873. uidx = (duk_uidx_t) idx;
  15874. }
  15875. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  15876. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  15877. if (DUK_LIKELY(uidx < vs_size)) {
  15878. return thr->valstack_bottom + uidx;
  15879. }
  15880. DUK_ERROR_RANGE_INDEX(thr, idx);
  15881. DUK_WO_NORETURN(return NULL;);
  15882. }
  15883. /* Non-critical. */
  15884. DUK_EXTERNAL duk_bool_t duk_is_valid_index(duk_hthread *thr, duk_idx_t idx) {
  15885. DUK_ASSERT_API_ENTRY(thr);
  15886. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15887. return (duk_normalize_index(thr, idx) >= 0);
  15888. }
  15889. /* Non-critical. */
  15890. DUK_EXTERNAL void duk_require_valid_index(duk_hthread *thr, duk_idx_t idx) {
  15891. DUK_ASSERT_API_ENTRY(thr);
  15892. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15893. if (DUK_UNLIKELY(duk_normalize_index(thr, idx) < 0)) {
  15894. DUK_ERROR_RANGE_INDEX(thr, idx);
  15895. DUK_WO_NORETURN(return;);
  15896. }
  15897. }
  15898. /*
  15899. * Value stack top handling
  15900. */
  15901. DUK_EXTERNAL duk_idx_t duk_get_top(duk_hthread *thr) {
  15902. DUK_ASSERT_API_ENTRY(thr);
  15903. return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  15904. }
  15905. /* Internal helper to get current top but to require a minimum top value
  15906. * (TypeError if not met).
  15907. */
  15908. DUK_INTERNAL duk_idx_t duk_get_top_require_min(duk_hthread *thr, duk_idx_t min_top) {
  15909. duk_idx_t ret;
  15910. DUK_ASSERT_API_ENTRY(thr);
  15911. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  15912. if (DUK_UNLIKELY(ret < min_top)) {
  15913. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  15914. DUK_WO_NORETURN(return 0;);
  15915. }
  15916. return ret;
  15917. }
  15918. /* Set stack top within currently allocated range, but don't reallocate.
  15919. * This is performance critical especially for call handling, so whenever
  15920. * changing, profile and look at generated code.
  15921. */
  15922. DUK_EXTERNAL void duk_set_top(duk_hthread *thr, duk_idx_t idx) {
  15923. duk_uidx_t vs_size;
  15924. duk_uidx_t vs_limit;
  15925. duk_uidx_t uidx;
  15926. duk_tval *tv;
  15927. DUK_ASSERT_API_ENTRY(thr);
  15928. DUK_ASSERT(DUK_INVALID_INDEX < 0);
  15929. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  15930. DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom);
  15931. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  15932. vs_limit = (duk_uidx_t) (thr->valstack_end - thr->valstack_bottom);
  15933. if (idx < 0) {
  15934. /* Negative indices are always within allocated stack but
  15935. * must not go below zero index.
  15936. */
  15937. uidx = vs_size + (duk_uidx_t) idx;
  15938. } else {
  15939. /* Positive index can be higher than valstack top but must
  15940. * not go above allocated stack (equality is OK).
  15941. */
  15942. uidx = (duk_uidx_t) idx;
  15943. }
  15944. /* DUK_INVALID_INDEX won't be accepted as a valid index. */
  15945. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_size);
  15946. DUK_ASSERT(vs_size + (duk_uidx_t) DUK_INVALID_INDEX >= vs_limit);
  15947. #if defined(DUK_USE_VALSTACK_UNSAFE)
  15948. DUK_ASSERT(uidx <= vs_limit);
  15949. DUK_UNREF(vs_limit);
  15950. #else
  15951. if (DUK_UNLIKELY(uidx > vs_limit)) {
  15952. DUK_ERROR_RANGE_INDEX(thr, idx);
  15953. DUK_WO_NORETURN(return;);
  15954. }
  15955. #endif
  15956. DUK_ASSERT(uidx <= vs_limit);
  15957. /* Handle change in value stack top. Respect value stack
  15958. * initialization policy: 'undefined' above top. Note that
  15959. * DECREF may cause a side effect that reallocates valstack,
  15960. * so must relookup after DECREF.
  15961. */
  15962. if (uidx >= vs_size) {
  15963. /* Stack size increases or stays the same. */
  15964. #if defined(DUK_USE_ASSERTIONS)
  15965. duk_uidx_t count;
  15966. count = uidx - vs_size;
  15967. while (count != 0) {
  15968. count--;
  15969. tv = thr->valstack_top + count;
  15970. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
  15971. }
  15972. #endif
  15973. thr->valstack_top = thr->valstack_bottom + uidx;
  15974. } else {
  15975. /* Stack size decreases. */
  15976. #if defined(DUK_USE_REFERENCE_COUNTING)
  15977. duk_uidx_t count;
  15978. duk_tval *tv_end;
  15979. count = vs_size - uidx;
  15980. DUK_ASSERT(count > 0);
  15981. tv = thr->valstack_top;
  15982. tv_end = tv - count;
  15983. DUK_ASSERT(tv > tv_end); /* Because count > 0. */
  15984. do {
  15985. tv--;
  15986. DUK_ASSERT(tv >= thr->valstack_bottom);
  15987. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
  15988. } while (tv != tv_end);
  15989. thr->valstack_top = tv_end;
  15990. DUK_REFZERO_CHECK_FAST(thr);
  15991. #else /* DUK_USE_REFERENCE_COUNTING */
  15992. duk_uidx_t count;
  15993. duk_tval *tv_end;
  15994. count = vs_size - uidx;
  15995. tv = thr->valstack_top;
  15996. tv_end = tv - count;
  15997. DUK_ASSERT(tv > tv_end);
  15998. do {
  15999. tv--;
  16000. DUK_TVAL_SET_UNDEFINED(tv);
  16001. } while (tv != tv_end);
  16002. thr->valstack_top = tv_end;
  16003. #endif /* DUK_USE_REFERENCE_COUNTING */
  16004. }
  16005. }
  16006. /* Internal variant with a non-negative index and no runtime size checks. */
  16007. #if defined(DUK_USE_PREFER_SIZE)
  16008. DUK_INTERNAL void duk_set_top_unsafe(duk_hthread *thr, duk_idx_t idx) {
  16009. DUK_ASSERT_API_ENTRY(thr);
  16010. duk_set_top(thr, idx);
  16011. }
  16012. #else /* DUK_USE_PREFER_SIZE */
  16013. DUK_INTERNAL void duk_set_top_unsafe(duk_hthread *thr, duk_idx_t idx) {
  16014. duk_uidx_t uidx;
  16015. duk_uidx_t vs_size;
  16016. duk_tval *tv;
  16017. DUK_ASSERT_API_ENTRY(thr);
  16018. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16019. DUK_ASSERT(thr->valstack_end >= thr->valstack_bottom);
  16020. DUK_ASSERT(idx >= 0);
  16021. DUK_ASSERT(idx <= (duk_idx_t) (thr->valstack_end - thr->valstack_bottom));
  16022. /* XXX: byte arithmetic */
  16023. uidx = (duk_uidx_t) idx;
  16024. vs_size = (duk_uidx_t) (thr->valstack_top - thr->valstack_bottom);
  16025. if (uidx >= vs_size) {
  16026. /* Stack size increases or stays the same. */
  16027. #if defined(DUK_USE_ASSERTIONS)
  16028. duk_uidx_t count;
  16029. count = uidx - vs_size;
  16030. while (count != 0) {
  16031. count--;
  16032. tv = thr->valstack_top + count;
  16033. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
  16034. }
  16035. #endif
  16036. thr->valstack_top = thr->valstack_bottom + uidx;
  16037. } else {
  16038. /* Stack size decreases. */
  16039. #if defined(DUK_USE_REFERENCE_COUNTING)
  16040. duk_uidx_t count;
  16041. duk_tval *tv_end;
  16042. count = vs_size - uidx;
  16043. DUK_ASSERT(count > 0);
  16044. tv = thr->valstack_top;
  16045. tv_end = tv - count;
  16046. DUK_ASSERT(tv > tv_end); /* Because count > 0. */
  16047. do {
  16048. tv--;
  16049. DUK_ASSERT(tv >= thr->valstack_bottom);
  16050. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
  16051. } while (tv != tv_end);
  16052. thr->valstack_top = tv_end;
  16053. DUK_REFZERO_CHECK_FAST(thr);
  16054. #else /* DUK_USE_REFERENCE_COUNTING */
  16055. duk_uidx_t count;
  16056. duk_tval *tv_end;
  16057. count = vs_size - uidx;
  16058. tv = thr->valstack_top;
  16059. tv_end = tv - count;
  16060. DUK_ASSERT(tv > tv_end);
  16061. do {
  16062. tv--;
  16063. DUK_TVAL_SET_UNDEFINED(tv);
  16064. } while (tv != tv_end);
  16065. thr->valstack_top = tv_end;
  16066. #endif /* DUK_USE_REFERENCE_COUNTING */
  16067. }
  16068. }
  16069. #endif /* DUK_USE_PREFER_SIZE */
  16070. /* Internal helper: set top to 'top', and set [idx_wipe_start,top[ to
  16071. * 'undefined' (doing nothing if idx_wipe_start == top). Indices are
  16072. * positive and within value stack reserve. This is used by call handling.
  16073. */
  16074. DUK_INTERNAL void duk_set_top_and_wipe(duk_hthread *thr, duk_idx_t top, duk_idx_t idx_wipe_start) {
  16075. DUK_ASSERT_API_ENTRY(thr);
  16076. DUK_ASSERT(top >= 0);
  16077. DUK_ASSERT(idx_wipe_start >= 0);
  16078. DUK_ASSERT(idx_wipe_start <= top);
  16079. DUK_ASSERT(thr->valstack_bottom + top <= thr->valstack_end);
  16080. DUK_ASSERT(thr->valstack_bottom + idx_wipe_start <= thr->valstack_end);
  16081. duk_set_top_unsafe(thr, idx_wipe_start);
  16082. duk_set_top_unsafe(thr, top);
  16083. }
  16084. DUK_EXTERNAL duk_idx_t duk_get_top_index(duk_hthread *thr) {
  16085. duk_idx_t ret;
  16086. DUK_ASSERT_API_ENTRY(thr);
  16087. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom) - 1;
  16088. if (DUK_UNLIKELY(ret < 0)) {
  16089. /* Return invalid index; if caller uses this without checking
  16090. * in another API call, the index won't map to a valid stack
  16091. * entry.
  16092. */
  16093. return DUK_INVALID_INDEX;
  16094. }
  16095. return ret;
  16096. }
  16097. /* Internal variant: call assumes there is at least one element on the value
  16098. * stack frame; this is only asserted for.
  16099. */
  16100. DUK_INTERNAL duk_idx_t duk_get_top_index_unsafe(duk_hthread *thr) {
  16101. duk_idx_t ret;
  16102. DUK_ASSERT_API_ENTRY(thr);
  16103. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom) - 1;
  16104. return ret;
  16105. }
  16106. DUK_EXTERNAL duk_idx_t duk_require_top_index(duk_hthread *thr) {
  16107. duk_idx_t ret;
  16108. DUK_ASSERT_API_ENTRY(thr);
  16109. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom) - 1;
  16110. if (DUK_UNLIKELY(ret < 0)) {
  16111. DUK_ERROR_RANGE_INDEX(thr, -1);
  16112. DUK_WO_NORETURN(return 0;);
  16113. }
  16114. return ret;
  16115. }
  16116. /*
  16117. * Value stack resizing.
  16118. *
  16119. * This resizing happens above the current "top": the value stack can be
  16120. * grown or shrunk, but the "top" is not affected. The value stack cannot
  16121. * be resized to a size below the current reserve.
  16122. *
  16123. * The low level reallocation primitive must carefully recompute all value
  16124. * stack pointers, and must also work if ALL pointers are NULL. The resize
  16125. * is quite tricky because the valstack realloc may cause a mark-and-sweep,
  16126. * which may run finalizers. Running finalizers may resize the valstack
  16127. * recursively (the same value stack we're working on). So, after realloc
  16128. * returns, we know that the valstack bottom, top, and reserve should still
  16129. * be the same (there should not be live values above the "top"), but its
  16130. * underlying size, alloc_end, and base pointer may have changed.
  16131. *
  16132. * 'new_size' is known to be <= DUK_USE_VALSTACK_LIMIT, which ensures that
  16133. * size_t and pointer arithmetic won't wrap in duk__resize_valstack().
  16134. */
  16135. /* Low level valstack resize primitive, used for both grow and shrink. All
  16136. * adjustments for slack etc have already been done. Doesn't throw but does
  16137. * have allocation side effects.
  16138. */
  16139. DUK_LOCAL DUK_COLD DUK_NOINLINE duk_bool_t duk__resize_valstack(duk_hthread *thr, duk_size_t new_size) {
  16140. duk_tval *pre_valstack;
  16141. duk_tval *pre_bottom;
  16142. duk_tval *pre_top;
  16143. duk_tval *pre_end;
  16144. duk_tval *pre_alloc_end;
  16145. duk_ptrdiff_t ptr_diff;
  16146. duk_tval *new_valstack;
  16147. duk_size_t new_alloc_size;
  16148. duk_tval *tv_prev_alloc_end;
  16149. duk_tval *p;
  16150. DUK_ASSERT_HTHREAD_VALID(thr);
  16151. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  16152. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16153. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  16154. DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
  16155. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) <= new_size); /* can't resize below 'top' */
  16156. DUK_ASSERT(new_size <= DUK_USE_VALSTACK_LIMIT); /* valstack limit caller has check, prevents wrapping */
  16157. DUK_ASSERT(new_size <= DUK_SIZE_MAX / sizeof(duk_tval)); /* specific assert for wrapping */
  16158. /* Pre-realloc pointer copies for asserts and debug logs. */
  16159. pre_valstack = thr->valstack;
  16160. pre_bottom = thr->valstack_bottom;
  16161. pre_top = thr->valstack_top;
  16162. pre_end = thr->valstack_end;
  16163. pre_alloc_end = thr->valstack_alloc_end;
  16164. DUK_UNREF(pre_valstack);
  16165. DUK_UNREF(pre_bottom);
  16166. DUK_UNREF(pre_top);
  16167. DUK_UNREF(pre_end);
  16168. DUK_UNREF(pre_alloc_end);
  16169. /* If finalizer torture enabled, force base pointer change every time
  16170. * when it would be allowed.
  16171. */
  16172. #if defined(DUK_USE_FINALIZER_TORTURE)
  16173. if (thr->heap->pf_prevent_count == 0) {
  16174. duk_hthread_valstack_torture_realloc(thr);
  16175. }
  16176. #endif
  16177. /* Allocate a new valstack using DUK_REALLOC_DIRECT() to deal with
  16178. * a side effect changing the base pointer.
  16179. */
  16180. new_alloc_size = sizeof(duk_tval) * new_size;
  16181. new_valstack = (duk_tval *) DUK_REALLOC_INDIRECT(thr->heap, duk_hthread_get_valstack_ptr, (void *) thr, new_alloc_size);
  16182. if (DUK_UNLIKELY(new_valstack == NULL)) {
  16183. /* Because new_size != 0, if condition doesn't need to be
  16184. * (new_valstack != NULL || new_size == 0).
  16185. */
  16186. DUK_ASSERT(new_size != 0);
  16187. DUK_D(DUK_DPRINT("failed to resize valstack to %lu entries (%lu bytes)",
  16188. (unsigned long) new_size, (unsigned long) new_alloc_size));
  16189. return 0;
  16190. }
  16191. /* Debug log any changes in pointer(s) by side effects. These don't
  16192. * necessarily imply any incorrect behavior, but should be rare in
  16193. * practice.
  16194. */
  16195. #if defined(DUK_USE_DEBUG)
  16196. if (thr->valstack != pre_valstack) {
  16197. DUK_D(DUK_DPRINT("valstack base pointer changed during valstack resize: %p -> %p",
  16198. (void *) pre_valstack, (void *) thr->valstack));
  16199. }
  16200. if (thr->valstack_bottom != pre_bottom) {
  16201. DUK_D(DUK_DPRINT("valstack bottom pointer changed during valstack resize: %p -> %p",
  16202. (void *) pre_bottom, (void *) thr->valstack_bottom));
  16203. }
  16204. if (thr->valstack_top != pre_top) {
  16205. DUK_D(DUK_DPRINT("valstack top pointer changed during valstack resize: %p -> %p",
  16206. (void *) pre_top, (void *) thr->valstack_top));
  16207. }
  16208. if (thr->valstack_end != pre_end) {
  16209. DUK_D(DUK_DPRINT("valstack end pointer changed during valstack resize: %p -> %p",
  16210. (void *) pre_end, (void *) thr->valstack_end));
  16211. }
  16212. if (thr->valstack_alloc_end != pre_alloc_end) {
  16213. DUK_D(DUK_DPRINT("valstack alloc_end pointer changed during valstack resize: %p -> %p",
  16214. (void *) pre_alloc_end, (void *) thr->valstack_alloc_end));
  16215. }
  16216. #endif
  16217. /* Assertions: offsets for bottom, top, and end (reserve) must not
  16218. * have changed even with side effects because they are always
  16219. * restored in unwind. For alloc_end there's no guarantee: it may
  16220. * have grown or shrunk (but remain above 'end').
  16221. */
  16222. DUK_ASSERT(thr->valstack_bottom - thr->valstack == pre_bottom - pre_valstack);
  16223. DUK_ASSERT(thr->valstack_top - thr->valstack == pre_top - pre_valstack);
  16224. DUK_ASSERT(thr->valstack_end - thr->valstack == pre_end - pre_valstack);
  16225. DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
  16226. /* Write new pointers. Most pointers can be handled as a pointer
  16227. * difference.
  16228. */
  16229. ptr_diff = (duk_ptrdiff_t) ((duk_uint8_t *) new_valstack - (duk_uint8_t *) thr->valstack);
  16230. tv_prev_alloc_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_alloc_end + ptr_diff);
  16231. thr->valstack = new_valstack;
  16232. thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_bottom + ptr_diff);
  16233. thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_top + ptr_diff);
  16234. thr->valstack_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_end + ptr_diff);
  16235. thr->valstack_alloc_end = (duk_tval *) (void *) ((duk_uint8_t *) new_valstack + new_alloc_size);
  16236. /* Assertions: pointer sanity after pointer updates. */
  16237. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  16238. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  16239. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  16240. DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
  16241. DUK_D(DUK_DPRINT("resized valstack %lu -> %lu elements (%lu -> %lu bytes): "
  16242. "base=%p -> %p, bottom=%p -> %p (%ld), top=%p -> %p (%ld), "
  16243. "end=%p -> %p (%ld), alloc_end=%p -> %p (%ld);"
  16244. " tv_prev_alloc_end=%p (-> %ld inits; <0 means shrink)",
  16245. (unsigned long) (pre_alloc_end - pre_valstack),
  16246. (unsigned long) new_size,
  16247. (unsigned long) ((duk_uint8_t *) pre_alloc_end - (duk_uint8_t *) pre_valstack),
  16248. (unsigned long) new_alloc_size,
  16249. (void *) pre_valstack, (void *) thr->valstack,
  16250. (void *) pre_bottom, (void *) thr->valstack_bottom, (long) (thr->valstack_bottom - thr->valstack),
  16251. (void *) pre_top, (void *) thr->valstack_top, (long) (thr->valstack_top - thr->valstack),
  16252. (void *) pre_end, (void *) thr->valstack_end, (long) (thr->valstack_end - thr->valstack),
  16253. (void *) pre_alloc_end, (void *) thr->valstack_alloc_end, (long) (thr->valstack_alloc_end - thr->valstack),
  16254. (void *) tv_prev_alloc_end, (long) (thr->valstack_alloc_end - tv_prev_alloc_end)));
  16255. /* If allocation grew, init any new slots to 'undefined'. */
  16256. p = tv_prev_alloc_end;
  16257. while (p < thr->valstack_alloc_end) {
  16258. /* Never executed if new size is smaller. */
  16259. DUK_TVAL_SET_UNDEFINED(p);
  16260. p++;
  16261. }
  16262. /* Assert for value stack initialization policy. */
  16263. #if defined(DUK_USE_ASSERTIONS)
  16264. p = thr->valstack_top;
  16265. while (p < thr->valstack_alloc_end) {
  16266. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(p));
  16267. p++;
  16268. }
  16269. #endif
  16270. return 1;
  16271. }
  16272. DUK_LOCAL DUK_COLD DUK_NOINLINE duk_bool_t duk__valstack_grow(duk_hthread *thr, duk_size_t min_bytes, duk_bool_t throw_on_error) {
  16273. duk_size_t min_size;
  16274. duk_size_t new_size;
  16275. DUK_ASSERT(min_bytes / sizeof(duk_tval) * sizeof(duk_tval) == min_bytes);
  16276. min_size = min_bytes / sizeof(duk_tval); /* from bytes to slots */
  16277. #if defined(DUK_USE_VALSTACK_GROW_SHIFT)
  16278. /* New size is minimum size plus a proportional slack, e.g. shift of
  16279. * 2 means a 25% slack.
  16280. */
  16281. new_size = min_size + (min_size >> DUK_USE_VALSTACK_GROW_SHIFT);
  16282. #else
  16283. /* New size is tight with no slack. This is sometimes preferred in
  16284. * low memory environments.
  16285. */
  16286. new_size = min_size;
  16287. #endif
  16288. if (DUK_UNLIKELY(new_size > DUK_USE_VALSTACK_LIMIT || new_size < min_size /*wrap*/)) {
  16289. /* Note: may be triggered even if minimal new_size would not reach the limit,
  16290. * plan limit accordingly.
  16291. */
  16292. if (throw_on_error) {
  16293. DUK_ERROR_RANGE(thr, DUK_STR_VALSTACK_LIMIT);
  16294. DUK_WO_NORETURN(return 0;);
  16295. }
  16296. return 0;
  16297. }
  16298. if (duk__resize_valstack(thr, new_size) == 0) {
  16299. if (throw_on_error) {
  16300. DUK_ERROR_ALLOC_FAILED(thr);
  16301. DUK_WO_NORETURN(return 0;);
  16302. }
  16303. return 0;
  16304. }
  16305. thr->valstack_end = thr->valstack + min_size;
  16306. DUK_ASSERT(thr->valstack_alloc_end >= thr->valstack_end);
  16307. return 1;
  16308. }
  16309. /* Hot, inlined value stack grow check. Because value stack almost never
  16310. * grows, the actual resize call is in a NOINLINE helper.
  16311. */
  16312. DUK_INTERNAL DUK_INLINE void duk_valstack_grow_check_throw(duk_hthread *thr, duk_size_t min_bytes) {
  16313. duk_tval *tv;
  16314. tv = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + min_bytes);
  16315. if (DUK_LIKELY(thr->valstack_end >= tv)) {
  16316. return;
  16317. }
  16318. if (DUK_LIKELY(thr->valstack_alloc_end >= tv)) {
  16319. /* Values in [valstack_top,valstack_alloc_end[ are initialized
  16320. * to 'undefined' so we can just move the end pointer.
  16321. */
  16322. thr->valstack_end = tv;
  16323. return;
  16324. }
  16325. (void) duk__valstack_grow(thr, min_bytes, 1 /*throw_on_error*/);
  16326. }
  16327. /* Hot, inlined value stack grow check which doesn't throw. */
  16328. DUK_INTERNAL DUK_INLINE duk_bool_t duk_valstack_grow_check_nothrow(duk_hthread *thr, duk_size_t min_bytes) {
  16329. duk_tval *tv;
  16330. tv = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + min_bytes);
  16331. if (DUK_LIKELY(thr->valstack_end >= tv)) {
  16332. return 1;
  16333. }
  16334. if (DUK_LIKELY(thr->valstack_alloc_end >= tv)) {
  16335. thr->valstack_end = tv;
  16336. return 1;
  16337. }
  16338. return duk__valstack_grow(thr, min_bytes, 0 /*throw_on_error*/);
  16339. }
  16340. /* Value stack shrink check, called from mark-and-sweep. */
  16341. DUK_INTERNAL void duk_valstack_shrink_check_nothrow(duk_hthread *thr, duk_bool_t snug) {
  16342. duk_size_t alloc_bytes;
  16343. duk_size_t reserve_bytes;
  16344. duk_size_t shrink_bytes;
  16345. alloc_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_alloc_end - (duk_uint8_t *) thr->valstack);
  16346. reserve_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack);
  16347. DUK_ASSERT(alloc_bytes >= reserve_bytes);
  16348. /* We're free to shrink the value stack allocation down to
  16349. * reserve_bytes but not more. If 'snug' (emergency GC)
  16350. * shrink whatever we can. Otherwise only shrink if the new
  16351. * size would be considerably smaller.
  16352. */
  16353. #if defined(DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT)
  16354. if (snug) {
  16355. shrink_bytes = reserve_bytes;
  16356. } else {
  16357. duk_size_t proportion, slack;
  16358. /* Require that value stack shrinks by at least X% of its
  16359. * current size. For example, shift of 2 means at least
  16360. * 25%. The proportion is computed as bytes and may not
  16361. * be a multiple of sizeof(duk_tval); that's OK here.
  16362. */
  16363. proportion = alloc_bytes >> DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT;
  16364. if (alloc_bytes - reserve_bytes < proportion) {
  16365. /* Too little would be freed, do nothing. */
  16366. return;
  16367. }
  16368. /* Keep a slack after shrinking. The slack is again a
  16369. * proportion of the current size (the proportion should
  16370. * of course be smaller than the check proportion above).
  16371. */
  16372. #if defined(DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT)
  16373. DUK_ASSERT(DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT > DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT);
  16374. slack = alloc_bytes >> DUK_USE_VALSTACK_SHRINK_SLACK_SHIFT;
  16375. #else
  16376. slack = 0;
  16377. #endif
  16378. shrink_bytes = reserve_bytes +
  16379. slack / sizeof(duk_tval) * sizeof(duk_tval); /* multiple of duk_tval */
  16380. }
  16381. #else /* DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT */
  16382. /* Always snug, useful in some low memory environments. */
  16383. DUK_UNREF(snug);
  16384. shrink_bytes = reserve_bytes;
  16385. #endif /* DUK_USE_VALSTACK_SHRINK_CHECK_SHIFT */
  16386. DUK_D(DUK_DPRINT("valstack shrink check: alloc_bytes=%ld, reserve_bytes=%ld, shrink_bytes=%ld (unvalidated)",
  16387. (long) alloc_bytes, (long) reserve_bytes, (long) shrink_bytes));
  16388. DUK_ASSERT(shrink_bytes >= reserve_bytes);
  16389. if (shrink_bytes >= alloc_bytes) {
  16390. /* Skip if shrink target is same as current one (or higher,
  16391. * though that shouldn't happen in practice).
  16392. */
  16393. return;
  16394. }
  16395. DUK_ASSERT(shrink_bytes / sizeof(duk_tval) * sizeof(duk_tval) == shrink_bytes);
  16396. DUK_D(DUK_DPRINT("valstack shrink check: decided to shrink, snug: %ld", (long) snug));
  16397. duk__resize_valstack(thr, shrink_bytes / sizeof(duk_tval));
  16398. }
  16399. DUK_EXTERNAL duk_bool_t duk_check_stack(duk_hthread *thr, duk_idx_t extra) {
  16400. duk_size_t min_new_bytes;
  16401. DUK_ASSERT_API_ENTRY(thr);
  16402. DUK_ASSERT(thr != NULL);
  16403. if (DUK_UNLIKELY(extra < 0 || extra > DUK_USE_VALSTACK_LIMIT)) {
  16404. if (extra < 0) {
  16405. /* Clamping to zero makes the API more robust to calling code
  16406. * calculation errors.
  16407. */
  16408. extra = 0;
  16409. } else {
  16410. /* Cause grow check to fail without wrapping arithmetic. */
  16411. extra = DUK_USE_VALSTACK_LIMIT;
  16412. }
  16413. }
  16414. min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) thr->valstack) +
  16415. sizeof(duk_tval) * ((duk_size_t) extra + DUK_VALSTACK_INTERNAL_EXTRA);
  16416. return duk_valstack_grow_check_nothrow(thr, min_new_bytes);
  16417. }
  16418. DUK_EXTERNAL void duk_require_stack(duk_hthread *thr, duk_idx_t extra) {
  16419. duk_size_t min_new_bytes;
  16420. DUK_ASSERT_API_ENTRY(thr);
  16421. DUK_ASSERT(thr != NULL);
  16422. if (DUK_UNLIKELY(extra < 0 || extra > DUK_USE_VALSTACK_LIMIT)) {
  16423. if (extra < 0) {
  16424. /* Clamping to zero makes the API more robust to calling code
  16425. * calculation errors.
  16426. */
  16427. extra = 0;
  16428. } else {
  16429. /* Cause grow check to fail without wrapping arithmetic. */
  16430. extra = DUK_USE_VALSTACK_LIMIT;
  16431. }
  16432. }
  16433. min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) thr->valstack) +
  16434. sizeof(duk_tval) * ((duk_size_t) extra + DUK_VALSTACK_INTERNAL_EXTRA);
  16435. duk_valstack_grow_check_throw(thr, min_new_bytes);
  16436. }
  16437. DUK_EXTERNAL duk_bool_t duk_check_stack_top(duk_hthread *thr, duk_idx_t top) {
  16438. duk_size_t min_new_bytes;
  16439. DUK_ASSERT_API_ENTRY(thr);
  16440. if (DUK_UNLIKELY(top < 0 || top > DUK_USE_VALSTACK_LIMIT)) {
  16441. if (top < 0) {
  16442. /* Clamping to zero makes the API more robust to calling code
  16443. * calculation errors.
  16444. */
  16445. top = 0;
  16446. } else {
  16447. /* Cause grow check to fail without wrapping arithmetic. */
  16448. top = DUK_USE_VALSTACK_LIMIT;
  16449. }
  16450. }
  16451. DUK_ASSERT(top >= 0);
  16452. min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack) +
  16453. sizeof(duk_tval) * ((duk_size_t) top + DUK_VALSTACK_INTERNAL_EXTRA);
  16454. return duk_valstack_grow_check_nothrow(thr, min_new_bytes);
  16455. }
  16456. DUK_EXTERNAL void duk_require_stack_top(duk_hthread *thr, duk_idx_t top) {
  16457. duk_size_t min_new_bytes;
  16458. DUK_ASSERT_API_ENTRY(thr);
  16459. if (DUK_UNLIKELY(top < 0 || top > DUK_USE_VALSTACK_LIMIT)) {
  16460. if (top < 0) {
  16461. /* Clamping to zero makes the API more robust to calling code
  16462. * calculation errors.
  16463. */
  16464. top = 0;
  16465. } else {
  16466. /* Cause grow check to fail without wrapping arithmetic. */
  16467. top = DUK_USE_VALSTACK_LIMIT;
  16468. }
  16469. }
  16470. DUK_ASSERT(top >= 0);
  16471. min_new_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack) +
  16472. sizeof(duk_tval) * ((duk_size_t) top + DUK_VALSTACK_INTERNAL_EXTRA);
  16473. duk_valstack_grow_check_throw(thr, min_new_bytes);
  16474. }
  16475. /*
  16476. * Basic stack manipulation: swap, dup, insert, replace, etc
  16477. */
  16478. DUK_EXTERNAL void duk_swap(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
  16479. duk_tval *tv1;
  16480. duk_tval *tv2;
  16481. duk_tval tv_tmp;
  16482. DUK_ASSERT_API_ENTRY(thr);
  16483. tv1 = duk_require_tval(thr, idx1);
  16484. DUK_ASSERT(tv1 != NULL);
  16485. tv2 = duk_require_tval(thr, idx2);
  16486. DUK_ASSERT(tv2 != NULL);
  16487. /* If tv1==tv2 this is a NOP, no check is needed */
  16488. DUK_TVAL_SET_TVAL(&tv_tmp, tv1);
  16489. DUK_TVAL_SET_TVAL(tv1, tv2);
  16490. DUK_TVAL_SET_TVAL(tv2, &tv_tmp);
  16491. }
  16492. DUK_EXTERNAL void duk_swap_top(duk_hthread *thr, duk_idx_t idx) {
  16493. DUK_ASSERT_API_ENTRY(thr);
  16494. duk_swap(thr, idx, -1);
  16495. }
  16496. DUK_EXTERNAL void duk_dup(duk_hthread *thr, duk_idx_t from_idx) {
  16497. duk_tval *tv_from;
  16498. duk_tval *tv_to;
  16499. DUK_ASSERT_API_ENTRY(thr);
  16500. DUK__CHECK_SPACE();
  16501. tv_from = duk_require_tval(thr, from_idx);
  16502. tv_to = thr->valstack_top++;
  16503. DUK_ASSERT(tv_from != NULL);
  16504. DUK_ASSERT(tv_to != NULL);
  16505. DUK_TVAL_SET_TVAL(tv_to, tv_from);
  16506. DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
  16507. }
  16508. DUK_EXTERNAL void duk_dup_top(duk_hthread *thr) {
  16509. #if defined(DUK_USE_PREFER_SIZE)
  16510. duk_dup(thr, -1);
  16511. #else
  16512. duk_tval *tv_from;
  16513. duk_tval *tv_to;
  16514. DUK_ASSERT_API_ENTRY(thr);
  16515. DUK__CHECK_SPACE();
  16516. if (DUK_UNLIKELY(thr->valstack_top - thr->valstack_bottom <= 0)) {
  16517. DUK_ERROR_RANGE_INDEX(thr, -1);
  16518. DUK_WO_NORETURN(return;);
  16519. }
  16520. tv_from = thr->valstack_top - 1;
  16521. tv_to = thr->valstack_top++;
  16522. DUK_ASSERT(tv_from != NULL);
  16523. DUK_ASSERT(tv_to != NULL);
  16524. DUK_TVAL_SET_TVAL(tv_to, tv_from);
  16525. DUK_TVAL_INCREF(thr, tv_to); /* no side effects */
  16526. #endif
  16527. }
  16528. DUK_INTERNAL void duk_dup_0(duk_hthread *thr) {
  16529. DUK_ASSERT_API_ENTRY(thr);
  16530. duk_dup(thr, 0);
  16531. }
  16532. DUK_INTERNAL void duk_dup_1(duk_hthread *thr) {
  16533. DUK_ASSERT_API_ENTRY(thr);
  16534. duk_dup(thr, 1);
  16535. }
  16536. DUK_INTERNAL void duk_dup_2(duk_hthread *thr) {
  16537. DUK_ASSERT_API_ENTRY(thr);
  16538. duk_dup(thr, 2);
  16539. }
  16540. DUK_INTERNAL void duk_dup_m2(duk_hthread *thr) {
  16541. DUK_ASSERT_API_ENTRY(thr);
  16542. duk_dup(thr, -2);
  16543. }
  16544. DUK_INTERNAL void duk_dup_m3(duk_hthread *thr) {
  16545. DUK_ASSERT_API_ENTRY(thr);
  16546. duk_dup(thr, -3);
  16547. }
  16548. DUK_INTERNAL void duk_dup_m4(duk_hthread *thr) {
  16549. DUK_ASSERT_API_ENTRY(thr);
  16550. duk_dup(thr, -4);
  16551. }
  16552. DUK_EXTERNAL void duk_insert(duk_hthread *thr, duk_idx_t to_idx) {
  16553. duk_tval *p;
  16554. duk_tval *q;
  16555. duk_tval tv_tmp;
  16556. duk_size_t nbytes;
  16557. DUK_ASSERT_API_ENTRY(thr);
  16558. p = duk_require_tval(thr, to_idx);
  16559. DUK_ASSERT(p != NULL);
  16560. q = duk_require_tval(thr, -1);
  16561. DUK_ASSERT(q != NULL);
  16562. DUK_ASSERT(q >= p);
  16563. /* nbytes
  16564. * <--------->
  16565. * [ ... | p | x | x | q ]
  16566. * => [ ... | q | p | x | x ]
  16567. */
  16568. nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
  16569. DUK_DDD(DUK_DDDPRINT("duk_insert: to_idx=%ld, p=%p, q=%p, nbytes=%lu",
  16570. (long) to_idx, (void *) p, (void *) q, (unsigned long) nbytes));
  16571. /* No net refcount changes. No need to special case nbytes == 0
  16572. * (p == q).
  16573. */
  16574. DUK_TVAL_SET_TVAL(&tv_tmp, q);
  16575. duk_memmove((void *) (p + 1), (const void *) p, (size_t) nbytes);
  16576. DUK_TVAL_SET_TVAL(p, &tv_tmp);
  16577. }
  16578. DUK_INTERNAL void duk_insert_undefined(duk_hthread *thr, duk_idx_t idx) {
  16579. DUK_ASSERT_API_ENTRY(thr);
  16580. DUK_ASSERT(idx >= 0); /* Doesn't support negative indices. */
  16581. duk_push_undefined(thr);
  16582. duk_insert(thr, idx);
  16583. }
  16584. DUK_INTERNAL void duk_insert_undefined_n(duk_hthread *thr, duk_idx_t idx, duk_idx_t count) {
  16585. duk_tval *tv, *tv_end;
  16586. DUK_ASSERT_API_ENTRY(thr);
  16587. DUK_ASSERT(idx >= 0); /* Doesn't support negative indices or count. */
  16588. DUK_ASSERT(count >= 0);
  16589. tv = duk_reserve_gap(thr, idx, count);
  16590. tv_end = tv + count;
  16591. while (tv != tv_end) {
  16592. DUK_TVAL_SET_UNDEFINED(tv);
  16593. tv++;
  16594. }
  16595. }
  16596. DUK_EXTERNAL void duk_replace(duk_hthread *thr, duk_idx_t to_idx) {
  16597. duk_tval *tv1;
  16598. duk_tval *tv2;
  16599. duk_tval tv_tmp;
  16600. DUK_ASSERT_API_ENTRY(thr);
  16601. tv1 = duk_require_tval(thr, -1);
  16602. DUK_ASSERT(tv1 != NULL);
  16603. tv2 = duk_require_tval(thr, to_idx);
  16604. DUK_ASSERT(tv2 != NULL);
  16605. /* For tv1 == tv2, both pointing to stack top, the end result
  16606. * is same as duk_pop(thr).
  16607. */
  16608. DUK_TVAL_SET_TVAL(&tv_tmp, tv2);
  16609. DUK_TVAL_SET_TVAL(tv2, tv1);
  16610. DUK_TVAL_SET_UNDEFINED(tv1);
  16611. thr->valstack_top--;
  16612. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  16613. }
  16614. DUK_EXTERNAL void duk_copy(duk_hthread *thr, duk_idx_t from_idx, duk_idx_t to_idx) {
  16615. duk_tval *tv1;
  16616. duk_tval *tv2;
  16617. DUK_ASSERT_API_ENTRY(thr);
  16618. tv1 = duk_require_tval(thr, from_idx);
  16619. DUK_ASSERT(tv1 != NULL);
  16620. tv2 = duk_require_tval(thr, to_idx);
  16621. DUK_ASSERT(tv2 != NULL);
  16622. /* For tv1 == tv2, this is a no-op (no explicit check needed). */
  16623. DUK_TVAL_SET_TVAL_UPDREF(thr, tv2, tv1); /* side effects */
  16624. }
  16625. DUK_EXTERNAL void duk_remove(duk_hthread *thr, duk_idx_t idx) {
  16626. duk_tval *p;
  16627. duk_tval *q;
  16628. #if defined(DUK_USE_REFERENCE_COUNTING)
  16629. duk_tval tv_tmp;
  16630. #endif
  16631. duk_size_t nbytes;
  16632. DUK_ASSERT_API_ENTRY(thr);
  16633. p = duk_require_tval(thr, idx);
  16634. DUK_ASSERT(p != NULL);
  16635. q = duk_require_tval(thr, -1);
  16636. DUK_ASSERT(q != NULL);
  16637. DUK_ASSERT(q >= p);
  16638. /* nbytes zero size case
  16639. * <--------->
  16640. * [ ... | p | x | x | q ] [ ... | p==q ]
  16641. * => [ ... | x | x | q ] [ ... ]
  16642. */
  16643. #if defined(DUK_USE_REFERENCE_COUNTING)
  16644. /* use a temp: decref only when valstack reachable values are correct */
  16645. DUK_TVAL_SET_TVAL(&tv_tmp, p);
  16646. #endif
  16647. nbytes = (duk_size_t) (((duk_uint8_t *) q) - ((duk_uint8_t *) p)); /* Note: 'q' is top-1 */
  16648. duk_memmove((void *) p, (const void *) (p + 1), (size_t) nbytes);
  16649. DUK_TVAL_SET_UNDEFINED(q);
  16650. thr->valstack_top--;
  16651. #if defined(DUK_USE_REFERENCE_COUNTING)
  16652. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  16653. #endif
  16654. }
  16655. DUK_INTERNAL void duk_remove_unsafe(duk_hthread *thr, duk_idx_t idx) {
  16656. DUK_ASSERT_API_ENTRY(thr);
  16657. duk_remove(thr, idx); /* XXX: no optimization for now */
  16658. }
  16659. DUK_INTERNAL void duk_remove_m2(duk_hthread *thr) {
  16660. DUK_ASSERT_API_ENTRY(thr);
  16661. duk_remove(thr, -2);
  16662. }
  16663. DUK_INTERNAL void duk_remove_n(duk_hthread *thr, duk_idx_t idx, duk_idx_t count) {
  16664. #if defined(DUK_USE_PREFER_SIZE)
  16665. /* XXX: maybe too slow even when preferring size? */
  16666. DUK_ASSERT_API_ENTRY(thr);
  16667. DUK_ASSERT(count >= 0);
  16668. DUK_ASSERT(idx >= 0);
  16669. while (count-- > 0) {
  16670. duk_remove(thr, idx);
  16671. }
  16672. #else /* DUK_USE_PREFER_SIZE */
  16673. duk_tval *tv_src;
  16674. duk_tval *tv_dst;
  16675. duk_tval *tv_newtop;
  16676. duk_tval *tv;
  16677. duk_size_t bytes;
  16678. DUK_ASSERT_API_ENTRY(thr);
  16679. DUK_ASSERT(count >= 0);
  16680. DUK_ASSERT(idx >= 0);
  16681. tv_dst = thr->valstack_bottom + idx;
  16682. DUK_ASSERT(tv_dst <= thr->valstack_top);
  16683. tv_src = tv_dst + count;
  16684. DUK_ASSERT(tv_src <= thr->valstack_top);
  16685. bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) tv_src);
  16686. for (tv = tv_dst; tv < tv_src; tv++) {
  16687. DUK_TVAL_DECREF_NORZ(thr, tv);
  16688. }
  16689. duk_memmove((void *) tv_dst, (const void *) tv_src, bytes);
  16690. tv_newtop = thr->valstack_top - count;
  16691. for (tv = tv_newtop; tv < thr->valstack_top; tv++) {
  16692. DUK_TVAL_SET_UNDEFINED(tv);
  16693. }
  16694. thr->valstack_top = tv_newtop;
  16695. /* When not preferring size, only NORZ macros are used; caller
  16696. * is expected to DUK_REFZERO_CHECK().
  16697. */
  16698. #endif /* DUK_USE_PREFER_SIZE */
  16699. }
  16700. DUK_INTERNAL void duk_remove_n_unsafe(duk_hthread *thr, duk_idx_t idx, duk_idx_t count) {
  16701. DUK_ASSERT_API_ENTRY(thr);
  16702. duk_remove_n(thr, idx, count); /* XXX: no optimization for now */
  16703. }
  16704. /*
  16705. * Stack slice primitives
  16706. */
  16707. DUK_EXTERNAL void duk_xcopymove_raw(duk_hthread *to_thr, duk_hthread *from_thr, duk_idx_t count, duk_bool_t is_copy) {
  16708. void *src;
  16709. duk_size_t nbytes;
  16710. duk_tval *p;
  16711. duk_tval *q;
  16712. /* XXX: several pointer comparison issues here */
  16713. DUK_ASSERT_API_ENTRY(to_thr);
  16714. DUK_ASSERT_CTX_VALID(to_thr);
  16715. DUK_ASSERT_CTX_VALID(from_thr);
  16716. DUK_ASSERT(to_thr->heap == from_thr->heap);
  16717. if (DUK_UNLIKELY(to_thr == from_thr)) {
  16718. DUK_ERROR_TYPE(to_thr, DUK_STR_INVALID_CONTEXT);
  16719. DUK_WO_NORETURN(return;);
  16720. }
  16721. if (DUK_UNLIKELY((duk_uidx_t) count > (duk_uidx_t) DUK_USE_VALSTACK_LIMIT)) {
  16722. /* Maximum value check ensures 'nbytes' won't wrap below.
  16723. * Also handles negative count.
  16724. */
  16725. DUK_ERROR_RANGE_INVALID_COUNT(to_thr);
  16726. DUK_WO_NORETURN(return;);
  16727. }
  16728. DUK_ASSERT(count >= 0);
  16729. nbytes = sizeof(duk_tval) * (duk_size_t) count;
  16730. if (DUK_UNLIKELY(nbytes == 0)) {
  16731. return;
  16732. }
  16733. DUK_ASSERT(to_thr->valstack_top <= to_thr->valstack_end);
  16734. if (DUK_UNLIKELY((duk_size_t) ((duk_uint8_t *) to_thr->valstack_end - (duk_uint8_t *) to_thr->valstack_top) < nbytes)) {
  16735. DUK_ERROR_RANGE_PUSH_BEYOND(to_thr);
  16736. DUK_WO_NORETURN(return;);
  16737. }
  16738. src = (void *) ((duk_uint8_t *) from_thr->valstack_top - nbytes);
  16739. if (DUK_UNLIKELY(src < (void *) from_thr->valstack_bottom)) {
  16740. DUK_ERROR_RANGE_INVALID_COUNT(to_thr);
  16741. DUK_WO_NORETURN(return;);
  16742. }
  16743. /* Copy values (no overlap even if to_thr == from_thr; that's not
  16744. * allowed now anyway).
  16745. */
  16746. DUK_ASSERT(nbytes > 0);
  16747. duk_memcpy((void *) to_thr->valstack_top, (const void *) src, (size_t) nbytes);
  16748. p = to_thr->valstack_top;
  16749. to_thr->valstack_top = (duk_tval *) (void *) (((duk_uint8_t *) p) + nbytes);
  16750. if (is_copy) {
  16751. /* Incref copies, keep originals. */
  16752. q = to_thr->valstack_top;
  16753. while (p < q) {
  16754. DUK_TVAL_INCREF(to_thr, p); /* no side effects */
  16755. p++;
  16756. }
  16757. } else {
  16758. /* No net refcount change. */
  16759. p = from_thr->valstack_top;
  16760. q = (duk_tval *) (void *) (((duk_uint8_t *) p) - nbytes);
  16761. from_thr->valstack_top = q;
  16762. while (p > q) {
  16763. p--;
  16764. DUK_TVAL_SET_UNDEFINED(p);
  16765. /* XXX: fast primitive to set a bunch of values to UNDEFINED */
  16766. }
  16767. }
  16768. }
  16769. /* Internal helper: reserve a gap of 'count' elements at 'idx_base' and return a
  16770. * pointer to the gap. Values in the gap are garbage and MUST be initialized by
  16771. * the caller before any side effects may occur. The caller must ensure there's
  16772. * enough stack reserve for 'count' values.
  16773. */
  16774. DUK_INTERNAL duk_tval *duk_reserve_gap(duk_hthread *thr, duk_idx_t idx_base, duk_idx_t count) {
  16775. duk_tval *tv_src;
  16776. duk_tval *tv_dst;
  16777. duk_size_t gap_bytes;
  16778. duk_size_t copy_bytes;
  16779. /* Caller is responsible for ensuring there's enough preallocated
  16780. * value stack.
  16781. */
  16782. DUK_ASSERT_API_ENTRY(thr);
  16783. DUK_ASSERT(count >= 0);
  16784. DUK_ASSERT((duk_size_t) (thr->valstack_end - thr->valstack_top) >= (duk_size_t) count);
  16785. tv_src = thr->valstack_bottom + idx_base;
  16786. gap_bytes = (duk_size_t) count * sizeof(duk_tval);
  16787. tv_dst = (duk_tval *) (void *) ((duk_uint8_t *) tv_src + gap_bytes);
  16788. copy_bytes = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) tv_src);
  16789. thr->valstack_top = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_top + gap_bytes);
  16790. duk_memmove((void *) tv_dst, (const void *) tv_src, copy_bytes);
  16791. /* Values in the gap are left as garbage: caller must fill them in
  16792. * and INCREF them before any side effects.
  16793. */
  16794. return tv_src;
  16795. }
  16796. /*
  16797. * Get/opt/require
  16798. */
  16799. DUK_EXTERNAL void duk_require_undefined(duk_hthread *thr, duk_idx_t idx) {
  16800. duk_tval *tv;
  16801. DUK_ASSERT_API_ENTRY(thr);
  16802. tv = duk_get_tval_or_unused(thr, idx);
  16803. DUK_ASSERT(tv != NULL);
  16804. if (DUK_UNLIKELY(!DUK_TVAL_IS_UNDEFINED(tv))) {
  16805. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "undefined", DUK_STR_NOT_UNDEFINED);
  16806. DUK_WO_NORETURN(return;);
  16807. }
  16808. }
  16809. DUK_EXTERNAL void duk_require_null(duk_hthread *thr, duk_idx_t idx) {
  16810. duk_tval *tv;
  16811. DUK_ASSERT_API_ENTRY(thr);
  16812. tv = duk_get_tval_or_unused(thr, idx);
  16813. DUK_ASSERT(tv != NULL);
  16814. if (DUK_UNLIKELY(!DUK_TVAL_IS_NULL(tv))) {
  16815. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "null", DUK_STR_NOT_NULL);
  16816. DUK_WO_NORETURN(return;);
  16817. }
  16818. }
  16819. DUK_LOCAL DUK_ALWAYS_INLINE duk_bool_t duk__get_boolean_raw(duk_hthread *thr, duk_idx_t idx, duk_bool_t def_value) {
  16820. duk_bool_t ret;
  16821. duk_tval *tv;
  16822. DUK_ASSERT_CTX_VALID(thr);
  16823. tv = duk_get_tval_or_unused(thr, idx);
  16824. DUK_ASSERT(tv != NULL);
  16825. if (DUK_TVAL_IS_BOOLEAN(tv)) {
  16826. ret = DUK_TVAL_GET_BOOLEAN(tv);
  16827. DUK_ASSERT(ret == 0 || ret == 1);
  16828. } else {
  16829. ret = def_value;
  16830. /* Not guaranteed to be 0 or 1. */
  16831. }
  16832. return ret;
  16833. }
  16834. DUK_EXTERNAL duk_bool_t duk_get_boolean(duk_hthread *thr, duk_idx_t idx) {
  16835. DUK_ASSERT_API_ENTRY(thr);
  16836. return duk__get_boolean_raw(thr, idx, 0); /* default: false */
  16837. }
  16838. DUK_EXTERNAL duk_bool_t duk_get_boolean_default(duk_hthread *thr, duk_idx_t idx, duk_bool_t def_value) {
  16839. DUK_ASSERT_API_ENTRY(thr);
  16840. return duk__get_boolean_raw(thr, idx, def_value);
  16841. }
  16842. DUK_EXTERNAL duk_bool_t duk_require_boolean(duk_hthread *thr, duk_idx_t idx) {
  16843. duk_tval *tv;
  16844. duk_bool_t ret;
  16845. DUK_ASSERT_API_ENTRY(thr);
  16846. tv = duk_get_tval_or_unused(thr, idx);
  16847. DUK_ASSERT(tv != NULL);
  16848. if (DUK_LIKELY(DUK_TVAL_IS_BOOLEAN(tv))) {
  16849. ret = DUK_TVAL_GET_BOOLEAN(tv);
  16850. DUK_ASSERT(ret == 0 || ret == 1);
  16851. return ret;
  16852. } else {
  16853. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "boolean", DUK_STR_NOT_BOOLEAN);
  16854. DUK_WO_NORETURN(return 0;);
  16855. }
  16856. }
  16857. DUK_EXTERNAL duk_bool_t duk_opt_boolean(duk_hthread *thr, duk_idx_t idx, duk_bool_t def_value) {
  16858. DUK_ASSERT_API_ENTRY(thr);
  16859. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  16860. return def_value;
  16861. }
  16862. return duk_require_boolean(thr, idx);
  16863. }
  16864. DUK_LOCAL DUK_ALWAYS_INLINE duk_double_t duk__get_number_raw(duk_hthread *thr, duk_idx_t idx, duk_double_t def_value) {
  16865. duk_double_union ret;
  16866. duk_tval *tv;
  16867. DUK_ASSERT_CTX_VALID(thr);
  16868. tv = duk_get_tval_or_unused(thr, idx);
  16869. DUK_ASSERT(tv != NULL);
  16870. #if defined(DUK_USE_FASTINT)
  16871. if (DUK_TVAL_IS_FASTINT(tv)) {
  16872. ret.d = (duk_double_t) DUK_TVAL_GET_FASTINT(tv); /* XXX: cast trick */
  16873. }
  16874. else
  16875. #endif
  16876. if (DUK_TVAL_IS_DOUBLE(tv)) {
  16877. /* When using packed duk_tval, number must be in NaN-normalized form
  16878. * for it to be a duk_tval, so no need to normalize. NOP for unpacked
  16879. * duk_tval.
  16880. */
  16881. ret.d = DUK_TVAL_GET_DOUBLE(tv);
  16882. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&ret));
  16883. } else {
  16884. ret.d = def_value;
  16885. /* Default value (including NaN) may not be normalized. */
  16886. }
  16887. return ret.d;
  16888. }
  16889. DUK_EXTERNAL duk_double_t duk_get_number(duk_hthread *thr, duk_idx_t idx) {
  16890. DUK_ASSERT_API_ENTRY(thr);
  16891. return duk__get_number_raw(thr, idx, DUK_DOUBLE_NAN); /* default: NaN */
  16892. }
  16893. DUK_EXTERNAL duk_double_t duk_get_number_default(duk_hthread *thr, duk_idx_t idx, duk_double_t def_value) {
  16894. DUK_ASSERT_API_ENTRY(thr);
  16895. return duk__get_number_raw(thr, idx, def_value);
  16896. }
  16897. DUK_EXTERNAL duk_double_t duk_require_number(duk_hthread *thr, duk_idx_t idx) {
  16898. duk_tval *tv;
  16899. duk_double_union ret;
  16900. DUK_ASSERT_API_ENTRY(thr);
  16901. tv = duk_get_tval_or_unused(thr, idx);
  16902. DUK_ASSERT(tv != NULL);
  16903. if (DUK_UNLIKELY(!DUK_TVAL_IS_NUMBER(tv))) {
  16904. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "number", DUK_STR_NOT_NUMBER);
  16905. DUK_WO_NORETURN(return 0.0;);
  16906. }
  16907. ret.d = DUK_TVAL_GET_NUMBER(tv);
  16908. /* When using packed duk_tval, number must be in NaN-normalized form
  16909. * for it to be a duk_tval, so no need to normalize. NOP for unpacked
  16910. * duk_tval.
  16911. */
  16912. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&ret));
  16913. return ret.d;
  16914. }
  16915. DUK_EXTERNAL duk_double_t duk_opt_number(duk_hthread *thr, duk_idx_t idx, duk_double_t def_value) {
  16916. DUK_ASSERT_API_ENTRY(thr);
  16917. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  16918. /* User provided default is not NaN normalized. */
  16919. return def_value;
  16920. }
  16921. return duk_require_number(thr, idx);
  16922. }
  16923. DUK_EXTERNAL duk_int_t duk_get_int(duk_hthread *thr, duk_idx_t idx) {
  16924. DUK_ASSERT_API_ENTRY(thr);
  16925. return (duk_int_t) duk__api_coerce_d2i(thr, idx, 0 /*def_value*/, 0 /*require*/);
  16926. }
  16927. DUK_EXTERNAL duk_uint_t duk_get_uint(duk_hthread *thr, duk_idx_t idx) {
  16928. DUK_ASSERT_API_ENTRY(thr);
  16929. return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, 0 /*def_value*/, 0 /*require*/);
  16930. }
  16931. DUK_EXTERNAL duk_int_t duk_get_int_default(duk_hthread *thr, duk_idx_t idx, duk_int_t def_value) {
  16932. DUK_ASSERT_API_ENTRY(thr);
  16933. return (duk_int_t) duk__api_coerce_d2i(thr, idx, def_value, 0 /*require*/);
  16934. }
  16935. DUK_EXTERNAL duk_uint_t duk_get_uint_default(duk_hthread *thr, duk_idx_t idx, duk_uint_t def_value) {
  16936. DUK_ASSERT_API_ENTRY(thr);
  16937. return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, def_value, 0 /*require*/);
  16938. }
  16939. DUK_EXTERNAL duk_int_t duk_require_int(duk_hthread *thr, duk_idx_t idx) {
  16940. DUK_ASSERT_API_ENTRY(thr);
  16941. return (duk_int_t) duk__api_coerce_d2i(thr, idx, 0 /*def_value*/, 1 /*require*/);
  16942. }
  16943. DUK_EXTERNAL duk_uint_t duk_require_uint(duk_hthread *thr, duk_idx_t idx) {
  16944. DUK_ASSERT_API_ENTRY(thr);
  16945. return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, 0 /*def_value*/, 1 /*require*/);
  16946. }
  16947. DUK_EXTERNAL duk_int_t duk_opt_int(duk_hthread *thr, duk_idx_t idx, duk_int_t def_value) {
  16948. DUK_ASSERT_API_ENTRY(thr);
  16949. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  16950. return def_value;
  16951. }
  16952. return duk_require_int(thr, idx);
  16953. }
  16954. DUK_EXTERNAL duk_uint_t duk_opt_uint(duk_hthread *thr, duk_idx_t idx, duk_uint_t def_value) {
  16955. DUK_ASSERT_API_ENTRY(thr);
  16956. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  16957. return def_value;
  16958. }
  16959. return duk_require_uint(thr, idx);
  16960. }
  16961. DUK_EXTERNAL const char *duk_get_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
  16962. duk_hstring *h;
  16963. const char *ret;
  16964. duk_size_t len;
  16965. DUK_ASSERT_API_ENTRY(thr);
  16966. h = duk_get_hstring(thr, idx);
  16967. if (h != NULL) {
  16968. len = DUK_HSTRING_GET_BYTELEN(h);
  16969. ret = (const char *) DUK_HSTRING_GET_DATA(h);
  16970. } else {
  16971. len = 0;
  16972. ret = NULL;
  16973. }
  16974. if (out_len != NULL) {
  16975. *out_len = len;
  16976. }
  16977. return ret;
  16978. }
  16979. DUK_EXTERNAL const char *duk_require_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
  16980. duk_hstring *h;
  16981. DUK_ASSERT_API_ENTRY(thr);
  16982. h = duk_require_hstring(thr, idx);
  16983. DUK_ASSERT(h != NULL);
  16984. if (out_len) {
  16985. *out_len = DUK_HSTRING_GET_BYTELEN(h);
  16986. }
  16987. return (const char *) DUK_HSTRING_GET_DATA(h);
  16988. }
  16989. DUK_INTERNAL const char *duk_require_lstring_notsymbol(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
  16990. duk_hstring *h;
  16991. DUK_ASSERT_API_ENTRY(thr);
  16992. h = duk_require_hstring_notsymbol(thr, idx);
  16993. DUK_ASSERT(h != NULL);
  16994. if (out_len) {
  16995. *out_len = DUK_HSTRING_GET_BYTELEN(h);
  16996. }
  16997. return (const char *) DUK_HSTRING_GET_DATA(h);
  16998. }
  16999. DUK_EXTERNAL const char *duk_get_string(duk_hthread *thr, duk_idx_t idx) {
  17000. duk_hstring *h;
  17001. DUK_ASSERT_API_ENTRY(thr);
  17002. h = duk_get_hstring(thr, idx);
  17003. if (h != NULL) {
  17004. return (const char *) DUK_HSTRING_GET_DATA(h);
  17005. } else {
  17006. return NULL;
  17007. }
  17008. }
  17009. DUK_EXTERNAL const char *duk_opt_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len, const char *def_ptr, duk_size_t def_len) {
  17010. DUK_ASSERT_API_ENTRY(thr);
  17011. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17012. if (out_len != NULL) {
  17013. *out_len = def_len;
  17014. }
  17015. return def_ptr;
  17016. }
  17017. return duk_require_lstring(thr, idx, out_len);
  17018. }
  17019. DUK_EXTERNAL const char *duk_opt_string(duk_hthread *thr, duk_idx_t idx, const char *def_ptr) {
  17020. DUK_ASSERT_API_ENTRY(thr);
  17021. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17022. return def_ptr;
  17023. }
  17024. return duk_require_string(thr, idx);
  17025. }
  17026. DUK_EXTERNAL const char *duk_get_lstring_default(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len, const char *def_ptr, duk_size_t def_len) {
  17027. duk_hstring *h;
  17028. const char *ret;
  17029. duk_size_t len;
  17030. DUK_ASSERT_API_ENTRY(thr);
  17031. h = duk_get_hstring(thr, idx);
  17032. if (h != NULL) {
  17033. len = DUK_HSTRING_GET_BYTELEN(h);
  17034. ret = (const char *) DUK_HSTRING_GET_DATA(h);
  17035. } else {
  17036. len = def_len;
  17037. ret = def_ptr;
  17038. }
  17039. if (out_len != NULL) {
  17040. *out_len = len;
  17041. }
  17042. return ret;
  17043. }
  17044. DUK_EXTERNAL const char *duk_get_string_default(duk_hthread *thr, duk_idx_t idx, const char *def_value) {
  17045. duk_hstring *h;
  17046. DUK_ASSERT_API_ENTRY(thr);
  17047. h = duk_get_hstring(thr, idx);
  17048. if (h != NULL) {
  17049. return (const char *) DUK_HSTRING_GET_DATA(h);
  17050. } else {
  17051. return def_value;
  17052. }
  17053. }
  17054. DUK_INTERNAL const char *duk_get_string_notsymbol(duk_hthread *thr, duk_idx_t idx) {
  17055. duk_hstring *h;
  17056. DUK_ASSERT_API_ENTRY(thr);
  17057. h = duk_get_hstring_notsymbol(thr, idx);
  17058. if (h) {
  17059. return (const char *) DUK_HSTRING_GET_DATA(h);
  17060. } else {
  17061. return NULL;
  17062. }
  17063. }
  17064. DUK_EXTERNAL const char *duk_require_string(duk_hthread *thr, duk_idx_t idx) {
  17065. DUK_ASSERT_API_ENTRY(thr);
  17066. return duk_require_lstring(thr, idx, NULL);
  17067. }
  17068. DUK_INTERNAL const char *duk_require_string_notsymbol(duk_hthread *thr, duk_idx_t idx) {
  17069. duk_hstring *h;
  17070. DUK_ASSERT_API_ENTRY(thr);
  17071. h = duk_require_hstring_notsymbol(thr, idx);
  17072. DUK_ASSERT(h != NULL);
  17073. return (const char *) DUK_HSTRING_GET_DATA(h);
  17074. }
  17075. DUK_EXTERNAL void duk_require_object(duk_hthread *thr, duk_idx_t idx) {
  17076. duk_tval *tv;
  17077. DUK_ASSERT_API_ENTRY(thr);
  17078. tv = duk_get_tval_or_unused(thr, idx);
  17079. DUK_ASSERT(tv != NULL);
  17080. if (DUK_UNLIKELY(!DUK_TVAL_IS_OBJECT(tv))) {
  17081. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "object", DUK_STR_NOT_OBJECT);
  17082. DUK_WO_NORETURN(return;);
  17083. }
  17084. }
  17085. DUK_LOCAL void *duk__get_pointer_raw(duk_hthread *thr, duk_idx_t idx, void *def_value) {
  17086. duk_tval *tv;
  17087. void *p;
  17088. DUK_ASSERT_CTX_VALID(thr);
  17089. tv = duk_get_tval_or_unused(thr, idx);
  17090. DUK_ASSERT(tv != NULL);
  17091. if (!DUK_TVAL_IS_POINTER(tv)) {
  17092. return def_value;
  17093. }
  17094. p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
  17095. return p;
  17096. }
  17097. DUK_EXTERNAL void *duk_get_pointer(duk_hthread *thr, duk_idx_t idx) {
  17098. DUK_ASSERT_API_ENTRY(thr);
  17099. return duk__get_pointer_raw(thr, idx, NULL /*def_value*/);
  17100. }
  17101. DUK_EXTERNAL void *duk_opt_pointer(duk_hthread *thr, duk_idx_t idx, void *def_value) {
  17102. DUK_ASSERT_API_ENTRY(thr);
  17103. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17104. return def_value;
  17105. }
  17106. return duk_require_pointer(thr, idx);
  17107. }
  17108. DUK_EXTERNAL void *duk_get_pointer_default(duk_hthread *thr, duk_idx_t idx, void *def_value) {
  17109. DUK_ASSERT_API_ENTRY(thr);
  17110. return duk__get_pointer_raw(thr, idx, def_value);
  17111. }
  17112. DUK_EXTERNAL void *duk_require_pointer(duk_hthread *thr, duk_idx_t idx) {
  17113. duk_tval *tv;
  17114. void *p;
  17115. DUK_ASSERT_API_ENTRY(thr);
  17116. /* Note: here we must be wary of the fact that a pointer may be
  17117. * valid and be a NULL.
  17118. */
  17119. tv = duk_get_tval_or_unused(thr, idx);
  17120. DUK_ASSERT(tv != NULL);
  17121. if (DUK_UNLIKELY(!DUK_TVAL_IS_POINTER(tv))) {
  17122. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "pointer", DUK_STR_NOT_POINTER);
  17123. DUK_WO_NORETURN(return NULL;);
  17124. }
  17125. p = DUK_TVAL_GET_POINTER(tv); /* may be NULL */
  17126. return p;
  17127. }
  17128. #if 0 /*unused*/
  17129. DUK_INTERNAL void *duk_get_voidptr(duk_hthread *thr, duk_idx_t idx) {
  17130. duk_tval *tv;
  17131. duk_heaphdr *h;
  17132. DUK_ASSERT_API_ENTRY(thr);
  17133. tv = duk_get_tval_or_unused(thr, idx);
  17134. DUK_ASSERT(tv != NULL);
  17135. if (!DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  17136. return NULL;
  17137. }
  17138. h = DUK_TVAL_GET_HEAPHDR(tv);
  17139. DUK_ASSERT(h != NULL);
  17140. return (void *) h;
  17141. }
  17142. #endif
  17143. DUK_LOCAL void *duk__get_buffer_helper(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size, duk_bool_t throw_flag) {
  17144. duk_hbuffer *h;
  17145. void *ret;
  17146. duk_size_t len;
  17147. duk_tval *tv;
  17148. DUK_ASSERT_CTX_VALID(thr);
  17149. if (out_size != NULL) {
  17150. *out_size = 0;
  17151. }
  17152. tv = duk_get_tval_or_unused(thr, idx);
  17153. DUK_ASSERT(tv != NULL);
  17154. if (DUK_LIKELY(DUK_TVAL_IS_BUFFER(tv))) {
  17155. h = DUK_TVAL_GET_BUFFER(tv);
  17156. DUK_ASSERT(h != NULL);
  17157. len = DUK_HBUFFER_GET_SIZE(h);
  17158. ret = DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  17159. } else {
  17160. if (throw_flag) {
  17161. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "buffer", DUK_STR_NOT_BUFFER);
  17162. DUK_WO_NORETURN(return NULL;);
  17163. }
  17164. len = def_size;
  17165. ret = def_ptr;
  17166. }
  17167. if (out_size != NULL) {
  17168. *out_size = len;
  17169. }
  17170. return ret;
  17171. }
  17172. DUK_EXTERNAL void *duk_get_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
  17173. DUK_ASSERT_API_ENTRY(thr);
  17174. return duk__get_buffer_helper(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 0 /*throw_flag*/);
  17175. }
  17176. DUK_EXTERNAL void *duk_opt_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size) {
  17177. DUK_ASSERT_API_ENTRY(thr);
  17178. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17179. if (out_size != NULL) {
  17180. *out_size = def_size;
  17181. }
  17182. return def_ptr;
  17183. }
  17184. return duk_require_buffer(thr, idx, out_size);
  17185. }
  17186. DUK_EXTERNAL void *duk_get_buffer_default(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_len) {
  17187. DUK_ASSERT_API_ENTRY(thr);
  17188. return duk__get_buffer_helper(thr, idx, out_size, def_ptr, def_len, 0 /*throw_flag*/);
  17189. }
  17190. DUK_EXTERNAL void *duk_require_buffer(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
  17191. DUK_ASSERT_API_ENTRY(thr);
  17192. return duk__get_buffer_helper(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 1 /*throw_flag*/);
  17193. }
  17194. /* Get the active buffer data area for a plain buffer or a buffer object.
  17195. * Return NULL if the the value is not a buffer. Note that a buffer may
  17196. * have a NULL data pointer when its size is zero, the optional 'out_isbuffer'
  17197. * argument allows caller to detect this reliably.
  17198. */
  17199. DUK_INTERNAL void *duk_get_buffer_data_raw(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size, duk_bool_t throw_flag, duk_bool_t *out_isbuffer) {
  17200. duk_tval *tv;
  17201. DUK_ASSERT_API_ENTRY(thr);
  17202. if (out_isbuffer != NULL) {
  17203. *out_isbuffer = 0;
  17204. }
  17205. if (out_size != NULL) {
  17206. *out_size = def_size;
  17207. }
  17208. tv = duk_get_tval_or_unused(thr, idx);
  17209. DUK_ASSERT(tv != NULL);
  17210. if (DUK_TVAL_IS_BUFFER(tv)) {
  17211. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  17212. DUK_ASSERT(h != NULL);
  17213. if (out_size != NULL) {
  17214. *out_size = DUK_HBUFFER_GET_SIZE(h);
  17215. }
  17216. if (out_isbuffer != NULL) {
  17217. *out_isbuffer = 1;
  17218. }
  17219. return (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h); /* may be NULL (but only if size is 0) */
  17220. }
  17221. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  17222. else if (DUK_TVAL_IS_OBJECT(tv)) {
  17223. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  17224. DUK_ASSERT(h != NULL);
  17225. if (DUK_HOBJECT_IS_BUFOBJ(h)) {
  17226. /* XXX: this is probably a useful shared helper: for a
  17227. * duk_hbufobj, get a validated buffer pointer/length.
  17228. */
  17229. duk_hbufobj *h_bufobj = (duk_hbufobj *) h;
  17230. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  17231. if (h_bufobj->buf != NULL &&
  17232. DUK_HBUFOBJ_VALID_SLICE(h_bufobj)) {
  17233. duk_uint8_t *p;
  17234. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf);
  17235. if (out_size != NULL) {
  17236. *out_size = (duk_size_t) h_bufobj->length;
  17237. }
  17238. if (out_isbuffer != NULL) {
  17239. *out_isbuffer = 1;
  17240. }
  17241. return (void *) (p + h_bufobj->offset);
  17242. }
  17243. /* if slice not fully valid, treat as error */
  17244. }
  17245. }
  17246. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  17247. if (throw_flag) {
  17248. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "buffer", DUK_STR_NOT_BUFFER);
  17249. DUK_WO_NORETURN(return NULL;);
  17250. }
  17251. return def_ptr;
  17252. }
  17253. DUK_EXTERNAL void *duk_get_buffer_data(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
  17254. DUK_ASSERT_API_ENTRY(thr);
  17255. return duk_get_buffer_data_raw(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 0 /*throw_flag*/, NULL);
  17256. }
  17257. DUK_EXTERNAL void *duk_get_buffer_data_default(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size) {
  17258. DUK_ASSERT_API_ENTRY(thr);
  17259. return duk_get_buffer_data_raw(thr, idx, out_size, def_ptr, def_size, 0 /*throw_flag*/, NULL);
  17260. }
  17261. DUK_EXTERNAL void *duk_opt_buffer_data(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, void *def_ptr, duk_size_t def_size) {
  17262. DUK_ASSERT_API_ENTRY(thr);
  17263. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17264. if (out_size != NULL) {
  17265. *out_size = def_size;
  17266. }
  17267. return def_ptr;
  17268. }
  17269. return duk_require_buffer_data(thr, idx, out_size);
  17270. }
  17271. DUK_EXTERNAL void *duk_require_buffer_data(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size) {
  17272. DUK_ASSERT_API_ENTRY(thr);
  17273. return duk_get_buffer_data_raw(thr, idx, out_size, NULL /*def_ptr*/, 0 /*def_size*/, 1 /*throw_flag*/, NULL);
  17274. }
  17275. /* Raw helper for getting a value from the stack, checking its tag.
  17276. * The tag cannot be a number because numbers don't have an internal
  17277. * tag in the packed representation.
  17278. */
  17279. DUK_LOCAL duk_heaphdr *duk__get_tagged_heaphdr_raw(duk_hthread *thr, duk_idx_t idx, duk_uint_t tag) {
  17280. duk_tval *tv;
  17281. duk_heaphdr *ret;
  17282. DUK_ASSERT_CTX_VALID(thr);
  17283. tv = duk_get_tval_or_unused(thr, idx);
  17284. DUK_ASSERT(tv != NULL);
  17285. if (DUK_TVAL_GET_TAG(tv) != tag) {
  17286. return (duk_heaphdr *) NULL;
  17287. }
  17288. ret = DUK_TVAL_GET_HEAPHDR(tv);
  17289. DUK_ASSERT(ret != NULL); /* tagged null pointers should never occur */
  17290. return ret;
  17291. }
  17292. DUK_INTERNAL duk_hstring *duk_get_hstring(duk_hthread *thr, duk_idx_t idx) {
  17293. DUK_ASSERT_API_ENTRY(thr);
  17294. return (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
  17295. }
  17296. DUK_INTERNAL duk_hstring *duk_get_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx) {
  17297. duk_hstring *h;
  17298. DUK_ASSERT_API_ENTRY(thr);
  17299. h = (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
  17300. if (DUK_UNLIKELY(h && DUK_HSTRING_HAS_SYMBOL(h))) {
  17301. return NULL;
  17302. }
  17303. return h;
  17304. }
  17305. DUK_INTERNAL duk_hstring *duk_require_hstring(duk_hthread *thr, duk_idx_t idx) {
  17306. duk_hstring *h;
  17307. DUK_ASSERT_API_ENTRY(thr);
  17308. h = (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
  17309. if (DUK_UNLIKELY(h == NULL)) {
  17310. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "string", DUK_STR_NOT_STRING);
  17311. DUK_WO_NORETURN(return NULL;);
  17312. }
  17313. return h;
  17314. }
  17315. DUK_INTERNAL duk_hstring *duk_require_hstring_notsymbol(duk_hthread *thr, duk_idx_t idx) {
  17316. duk_hstring *h;
  17317. DUK_ASSERT_API_ENTRY(thr);
  17318. h = (duk_hstring *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_STRING);
  17319. if (DUK_UNLIKELY(h == NULL || DUK_HSTRING_HAS_SYMBOL(h))) {
  17320. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "string", DUK_STR_NOT_STRING);
  17321. DUK_WO_NORETURN(return NULL;);
  17322. }
  17323. return h;
  17324. }
  17325. DUK_INTERNAL duk_hobject *duk_get_hobject(duk_hthread *thr, duk_idx_t idx) {
  17326. DUK_ASSERT_API_ENTRY(thr);
  17327. return (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17328. }
  17329. DUK_INTERNAL duk_hobject *duk_require_hobject(duk_hthread *thr, duk_idx_t idx) {
  17330. duk_hobject *h;
  17331. DUK_ASSERT_API_ENTRY(thr);
  17332. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17333. if (DUK_UNLIKELY(h == NULL)) {
  17334. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "object", DUK_STR_NOT_OBJECT);
  17335. DUK_WO_NORETURN(return NULL;);
  17336. }
  17337. return h;
  17338. }
  17339. DUK_INTERNAL duk_hbuffer *duk_get_hbuffer(duk_hthread *thr, duk_idx_t idx) {
  17340. DUK_ASSERT_API_ENTRY(thr);
  17341. return (duk_hbuffer *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_BUFFER);
  17342. }
  17343. DUK_INTERNAL duk_hbuffer *duk_require_hbuffer(duk_hthread *thr, duk_idx_t idx) {
  17344. duk_hbuffer *h;
  17345. DUK_ASSERT_API_ENTRY(thr);
  17346. h = (duk_hbuffer *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_BUFFER);
  17347. if (DUK_UNLIKELY(h == NULL)) {
  17348. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "buffer", DUK_STR_NOT_BUFFER);
  17349. DUK_WO_NORETURN(return NULL;);
  17350. }
  17351. return h;
  17352. }
  17353. DUK_INTERNAL duk_hthread *duk_get_hthread(duk_hthread *thr, duk_idx_t idx) {
  17354. duk_hobject *h;
  17355. DUK_ASSERT_API_ENTRY(thr);
  17356. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17357. if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_IS_THREAD(h))) {
  17358. h = NULL;
  17359. }
  17360. return (duk_hthread *) h;
  17361. }
  17362. DUK_INTERNAL duk_hthread *duk_require_hthread(duk_hthread *thr, duk_idx_t idx) {
  17363. duk_hobject *h;
  17364. DUK_ASSERT_API_ENTRY(thr);
  17365. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17366. if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_IS_THREAD(h)))) {
  17367. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "thread", DUK_STR_NOT_THREAD);
  17368. DUK_WO_NORETURN(return NULL;);
  17369. }
  17370. return (duk_hthread *) h;
  17371. }
  17372. DUK_INTERNAL duk_hcompfunc *duk_get_hcompfunc(duk_hthread *thr, duk_idx_t idx) {
  17373. duk_hobject *h;
  17374. DUK_ASSERT_API_ENTRY(thr);
  17375. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17376. if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_IS_COMPFUNC(h))) {
  17377. h = NULL;
  17378. }
  17379. return (duk_hcompfunc *) h;
  17380. }
  17381. DUK_INTERNAL duk_hcompfunc *duk_require_hcompfunc(duk_hthread *thr, duk_idx_t idx) {
  17382. duk_hobject *h;
  17383. DUK_ASSERT_API_ENTRY(thr);
  17384. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17385. if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_IS_COMPFUNC(h)))) {
  17386. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "compiledfunction", DUK_STR_NOT_COMPFUNC);
  17387. DUK_WO_NORETURN(return NULL;);
  17388. }
  17389. return (duk_hcompfunc *) h;
  17390. }
  17391. DUK_INTERNAL duk_hnatfunc *duk_get_hnatfunc(duk_hthread *thr, duk_idx_t idx) {
  17392. duk_hobject *h;
  17393. DUK_ASSERT_API_ENTRY(thr);
  17394. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17395. if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_IS_NATFUNC(h))) {
  17396. h = NULL;
  17397. }
  17398. return (duk_hnatfunc *) h;
  17399. }
  17400. DUK_INTERNAL duk_hnatfunc *duk_require_hnatfunc(duk_hthread *thr, duk_idx_t idx) {
  17401. duk_hobject *h;
  17402. DUK_ASSERT_API_ENTRY(thr);
  17403. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17404. if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_IS_NATFUNC(h)))) {
  17405. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "nativefunction", DUK_STR_NOT_NATFUNC);
  17406. DUK_WO_NORETURN(return NULL;);
  17407. }
  17408. return (duk_hnatfunc *) h;
  17409. }
  17410. DUK_EXTERNAL duk_c_function duk_get_c_function(duk_hthread *thr, duk_idx_t idx) {
  17411. duk_tval *tv;
  17412. duk_hobject *h;
  17413. duk_hnatfunc *f;
  17414. DUK_ASSERT_API_ENTRY(thr);
  17415. tv = duk_get_tval_or_unused(thr, idx);
  17416. DUK_ASSERT(tv != NULL);
  17417. if (DUK_UNLIKELY(!DUK_TVAL_IS_OBJECT(tv))) {
  17418. return NULL;
  17419. }
  17420. h = DUK_TVAL_GET_OBJECT(tv);
  17421. DUK_ASSERT(h != NULL);
  17422. if (DUK_UNLIKELY(!DUK_HOBJECT_IS_NATFUNC(h))) {
  17423. return NULL;
  17424. }
  17425. DUK_ASSERT(DUK_HOBJECT_HAS_NATFUNC(h));
  17426. f = (duk_hnatfunc *) h;
  17427. return f->func;
  17428. }
  17429. DUK_EXTERNAL duk_c_function duk_opt_c_function(duk_hthread *thr, duk_idx_t idx, duk_c_function def_value) {
  17430. DUK_ASSERT_API_ENTRY(thr);
  17431. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17432. return def_value;
  17433. }
  17434. return duk_require_c_function(thr, idx);
  17435. }
  17436. DUK_EXTERNAL duk_c_function duk_get_c_function_default(duk_hthread *thr, duk_idx_t idx, duk_c_function def_value) {
  17437. duk_c_function ret;
  17438. DUK_ASSERT_API_ENTRY(thr);
  17439. ret = duk_get_c_function(thr, idx);
  17440. if (ret != NULL) {
  17441. return ret;
  17442. }
  17443. return def_value;
  17444. }
  17445. DUK_EXTERNAL duk_c_function duk_require_c_function(duk_hthread *thr, duk_idx_t idx) {
  17446. duk_c_function ret;
  17447. DUK_ASSERT_API_ENTRY(thr);
  17448. ret = duk_get_c_function(thr, idx);
  17449. if (DUK_UNLIKELY(!ret)) {
  17450. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "nativefunction", DUK_STR_NOT_NATFUNC);
  17451. DUK_WO_NORETURN(return ret;);
  17452. }
  17453. return ret;
  17454. }
  17455. DUK_EXTERNAL void duk_require_function(duk_hthread *thr, duk_idx_t idx) {
  17456. DUK_ASSERT_API_ENTRY(thr);
  17457. if (DUK_UNLIKELY(!duk_is_function(thr, idx))) {
  17458. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "function", DUK_STR_NOT_FUNCTION);
  17459. DUK_WO_NORETURN(return;);
  17460. }
  17461. }
  17462. DUK_INTERNAL void duk_require_constructable(duk_hthread *thr, duk_idx_t idx) {
  17463. duk_hobject *h;
  17464. DUK_ASSERT_API_ENTRY(thr);
  17465. h = duk_require_hobject_accept_mask(thr, idx, DUK_TYPE_MASK_LIGHTFUNC);
  17466. if (DUK_UNLIKELY(h != NULL && !DUK_HOBJECT_HAS_CONSTRUCTABLE(h))) {
  17467. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "constructable", DUK_STR_NOT_CONSTRUCTABLE);
  17468. DUK_WO_NORETURN(return;);
  17469. }
  17470. /* Lightfuncs (h == NULL) are constructable. */
  17471. }
  17472. DUK_EXTERNAL duk_hthread *duk_get_context(duk_hthread *thr, duk_idx_t idx) {
  17473. DUK_ASSERT_API_ENTRY(thr);
  17474. return duk_get_hthread(thr, idx);
  17475. }
  17476. DUK_EXTERNAL duk_hthread *duk_require_context(duk_hthread *thr, duk_idx_t idx) {
  17477. DUK_ASSERT_API_ENTRY(thr);
  17478. return duk_require_hthread(thr, idx);
  17479. }
  17480. DUK_EXTERNAL duk_hthread *duk_opt_context(duk_hthread *thr, duk_idx_t idx, duk_hthread *def_value) {
  17481. DUK_ASSERT_API_ENTRY(thr);
  17482. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17483. return def_value;
  17484. }
  17485. return duk_require_context(thr, idx);
  17486. }
  17487. DUK_EXTERNAL duk_hthread *duk_get_context_default(duk_hthread *thr, duk_idx_t idx, duk_hthread *def_value) {
  17488. duk_hthread *ret;
  17489. DUK_ASSERT_API_ENTRY(thr);
  17490. ret = duk_get_context(thr, idx);
  17491. if (ret != NULL) {
  17492. return ret;
  17493. }
  17494. return def_value;
  17495. }
  17496. DUK_EXTERNAL void *duk_get_heapptr(duk_hthread *thr, duk_idx_t idx) {
  17497. duk_tval *tv;
  17498. void *ret;
  17499. DUK_ASSERT_API_ENTRY(thr);
  17500. tv = duk_get_tval_or_unused(thr, idx);
  17501. DUK_ASSERT(tv != NULL);
  17502. if (DUK_UNLIKELY(!DUK_TVAL_IS_HEAP_ALLOCATED(tv))) {
  17503. return (void *) NULL;
  17504. }
  17505. ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  17506. DUK_ASSERT(ret != NULL);
  17507. return ret;
  17508. }
  17509. DUK_EXTERNAL void *duk_opt_heapptr(duk_hthread *thr, duk_idx_t idx, void *def_value) {
  17510. DUK_ASSERT_API_ENTRY(thr);
  17511. if (duk_check_type_mask(thr, idx, DUK_TYPE_MASK_NONE | DUK_TYPE_MASK_UNDEFINED)) {
  17512. return def_value;
  17513. }
  17514. return duk_require_heapptr(thr, idx);
  17515. }
  17516. DUK_EXTERNAL void *duk_get_heapptr_default(duk_hthread *thr, duk_idx_t idx, void *def_value) {
  17517. void *ret;
  17518. DUK_ASSERT_API_ENTRY(thr);
  17519. ret = duk_get_heapptr(thr, idx);
  17520. if (ret != NULL) {
  17521. return ret;
  17522. }
  17523. return def_value;
  17524. }
  17525. DUK_EXTERNAL void *duk_require_heapptr(duk_hthread *thr, duk_idx_t idx) {
  17526. duk_tval *tv;
  17527. void *ret;
  17528. DUK_ASSERT_API_ENTRY(thr);
  17529. tv = duk_get_tval_or_unused(thr, idx);
  17530. DUK_ASSERT(tv != NULL);
  17531. if (DUK_UNLIKELY(!DUK_TVAL_IS_HEAP_ALLOCATED(tv))) {
  17532. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "heapobject", DUK_STR_UNEXPECTED_TYPE);
  17533. DUK_WO_NORETURN(return NULL;);
  17534. }
  17535. ret = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  17536. DUK_ASSERT(ret != NULL);
  17537. return ret;
  17538. }
  17539. /* Internal helper for getting/requiring a duk_hobject with possible promotion. */
  17540. DUK_LOCAL duk_hobject *duk__get_hobject_promote_mask_raw(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
  17541. duk_uint_t val_mask;
  17542. duk_hobject *res;
  17543. DUK_ASSERT_CTX_VALID(thr);
  17544. res = duk_get_hobject(thr, idx); /* common case, not promoted */
  17545. if (DUK_LIKELY(res != NULL)) {
  17546. DUK_ASSERT(res != NULL);
  17547. return res;
  17548. }
  17549. val_mask = duk_get_type_mask(thr, idx);
  17550. if (val_mask & type_mask) {
  17551. if (type_mask & DUK_TYPE_MASK_PROMOTE) {
  17552. res = duk_to_hobject(thr, idx);
  17553. DUK_ASSERT(res != NULL);
  17554. return res;
  17555. } else {
  17556. return NULL; /* accept without promoting */
  17557. }
  17558. }
  17559. if (type_mask & DUK_TYPE_MASK_THROW) {
  17560. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, "object", DUK_STR_NOT_OBJECT);
  17561. DUK_WO_NORETURN(return NULL;);
  17562. }
  17563. return NULL;
  17564. }
  17565. /* Get a duk_hobject * at 'idx'; if the value is not an object but matches the
  17566. * supplied 'type_mask', promote it to an object and return the duk_hobject *.
  17567. * This is useful for call sites which want an object but also accept a plain
  17568. * buffer and/or a lightfunc which gets automatically promoted to an object.
  17569. * Return value is NULL if value is neither an object nor a plain type allowed
  17570. * by the mask.
  17571. */
  17572. DUK_INTERNAL duk_hobject *duk_get_hobject_promote_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
  17573. DUK_ASSERT_API_ENTRY(thr);
  17574. return duk__get_hobject_promote_mask_raw(thr, idx, type_mask | DUK_TYPE_MASK_PROMOTE);
  17575. }
  17576. /* Like duk_get_hobject_promote_mask() but throw a TypeError instead of
  17577. * returning a NULL.
  17578. */
  17579. DUK_INTERNAL duk_hobject *duk_require_hobject_promote_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
  17580. DUK_ASSERT_API_ENTRY(thr);
  17581. return duk__get_hobject_promote_mask_raw(thr, idx, type_mask | DUK_TYPE_MASK_THROW | DUK_TYPE_MASK_PROMOTE);
  17582. }
  17583. /* Require a duk_hobject * at 'idx'; if the value is not an object but matches the
  17584. * supplied 'type_mask', return a NULL instead. Otherwise throw a TypeError.
  17585. */
  17586. DUK_INTERNAL duk_hobject *duk_require_hobject_accept_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t type_mask) {
  17587. DUK_ASSERT_API_ENTRY(thr);
  17588. return duk__get_hobject_promote_mask_raw(thr, idx, type_mask | DUK_TYPE_MASK_THROW);
  17589. }
  17590. DUK_INTERNAL duk_hobject *duk_get_hobject_with_class(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t classnum) {
  17591. duk_hobject *h;
  17592. DUK_ASSERT_API_ENTRY(thr);
  17593. DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */
  17594. DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX);
  17595. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17596. if (DUK_UNLIKELY(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) != classnum)) {
  17597. h = NULL;
  17598. }
  17599. return h;
  17600. }
  17601. DUK_INTERNAL duk_hobject *duk_require_hobject_with_class(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t classnum) {
  17602. duk_hobject *h;
  17603. DUK_ASSERT_API_ENTRY(thr);
  17604. DUK_ASSERT_DISABLE(classnum >= 0); /* unsigned */
  17605. DUK_ASSERT(classnum <= DUK_HOBJECT_CLASS_MAX);
  17606. h = (duk_hobject *) duk__get_tagged_heaphdr_raw(thr, idx, DUK_TAG_OBJECT);
  17607. if (DUK_UNLIKELY(!(h != NULL && DUK_HOBJECT_GET_CLASS_NUMBER(h) == classnum))) {
  17608. duk_hstring *h_class;
  17609. h_class = DUK_HTHREAD_GET_STRING(thr, DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum));
  17610. DUK_UNREF(h_class);
  17611. DUK_ERROR_REQUIRE_TYPE_INDEX(thr, idx, (const char *) DUK_HSTRING_GET_DATA(h_class), DUK_STR_UNEXPECTED_TYPE);
  17612. DUK_WO_NORETURN(return NULL;);
  17613. }
  17614. return h;
  17615. }
  17616. DUK_EXTERNAL duk_size_t duk_get_length(duk_hthread *thr, duk_idx_t idx) {
  17617. duk_tval *tv;
  17618. DUK_ASSERT_API_ENTRY(thr);
  17619. tv = duk_get_tval_or_unused(thr, idx);
  17620. DUK_ASSERT(tv != NULL);
  17621. switch (DUK_TVAL_GET_TAG(tv)) {
  17622. case DUK_TAG_UNDEFINED:
  17623. case DUK_TAG_NULL:
  17624. case DUK_TAG_BOOLEAN:
  17625. case DUK_TAG_POINTER:
  17626. return 0;
  17627. #if defined(DUK_USE_PREFER_SIZE)
  17628. /* String and buffer have a virtual non-configurable .length property
  17629. * which is within size_t range so it can be looked up without specific
  17630. * type checks. Lightfuncs inherit from %NativeFunctionPrototype%
  17631. * which provides an inherited .length accessor; it could be overwritten
  17632. * to produce unexpected types or values, but just number convert and
  17633. * duk_size_t cast for now.
  17634. */
  17635. case DUK_TAG_STRING:
  17636. case DUK_TAG_BUFFER:
  17637. case DUK_TAG_LIGHTFUNC: {
  17638. duk_size_t ret;
  17639. duk_get_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
  17640. ret = (duk_size_t) duk_to_number_m1(thr);
  17641. duk_pop_unsafe(thr);
  17642. return ret;
  17643. }
  17644. #else /* DUK_USE_PREFER_SIZE */
  17645. case DUK_TAG_STRING: {
  17646. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  17647. DUK_ASSERT(h != NULL);
  17648. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  17649. return 0;
  17650. }
  17651. return (duk_size_t) DUK_HSTRING_GET_CHARLEN(h);
  17652. }
  17653. case DUK_TAG_BUFFER: {
  17654. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  17655. DUK_ASSERT(h != NULL);
  17656. return (duk_size_t) DUK_HBUFFER_GET_SIZE(h);
  17657. }
  17658. case DUK_TAG_LIGHTFUNC: {
  17659. /* We could look up the length from the lightfunc duk_tval,
  17660. * but since Duktape 2.2 lightfunc .length comes from
  17661. * %NativeFunctionPrototype% which can be overridden, so
  17662. * look up the property explicitly.
  17663. */
  17664. duk_size_t ret;
  17665. duk_get_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
  17666. ret = (duk_size_t) duk_to_number_m1(thr);
  17667. duk_pop_unsafe(thr);
  17668. return ret;
  17669. }
  17670. #endif /* DUK_USE_PREFER_SIZE */
  17671. case DUK_TAG_OBJECT: {
  17672. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  17673. DUK_ASSERT(h != NULL);
  17674. return (duk_size_t) duk_hobject_get_length(thr, h);
  17675. }
  17676. #if defined(DUK_USE_FASTINT)
  17677. case DUK_TAG_FASTINT:
  17678. #endif
  17679. default:
  17680. /* number or 'unused' */
  17681. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv) || DUK_TVAL_IS_UNUSED(tv));
  17682. return 0;
  17683. }
  17684. DUK_UNREACHABLE();
  17685. }
  17686. /*
  17687. * duk_known_xxx() helpers
  17688. *
  17689. * Used internally when we're 100% sure that a certain index is valid and
  17690. * contains an object of a certain type. For example, if we duk_push_object()
  17691. * we can then safely duk_known_hobject(thr, -1). These helpers just assert
  17692. * for the index and type, and if the assumptions are not valid, memory unsafe
  17693. * behavior happens.
  17694. */
  17695. DUK_LOCAL duk_heaphdr *duk__known_heaphdr(duk_hthread *thr, duk_idx_t idx) {
  17696. duk_tval *tv;
  17697. duk_heaphdr *h;
  17698. DUK_ASSERT_CTX_VALID(thr);
  17699. if (idx < 0) {
  17700. tv = thr->valstack_top + idx;
  17701. } else {
  17702. tv = thr->valstack_bottom + idx;
  17703. }
  17704. DUK_ASSERT(tv >= thr->valstack_bottom);
  17705. DUK_ASSERT(tv < thr->valstack_top);
  17706. h = DUK_TVAL_GET_HEAPHDR(tv);
  17707. DUK_ASSERT(h != NULL);
  17708. return h;
  17709. }
  17710. DUK_INTERNAL duk_hstring *duk_known_hstring(duk_hthread *thr, duk_idx_t idx) {
  17711. DUK_ASSERT_API_ENTRY(thr);
  17712. DUK_ASSERT(duk_get_hstring(thr, idx) != NULL);
  17713. return (duk_hstring *) duk__known_heaphdr(thr, idx);
  17714. }
  17715. DUK_INTERNAL duk_hobject *duk_known_hobject(duk_hthread *thr, duk_idx_t idx) {
  17716. DUK_ASSERT_API_ENTRY(thr);
  17717. DUK_ASSERT(duk_get_hobject(thr, idx) != NULL);
  17718. return (duk_hobject *) duk__known_heaphdr(thr, idx);
  17719. }
  17720. DUK_INTERNAL duk_hbuffer *duk_known_hbuffer(duk_hthread *thr, duk_idx_t idx) {
  17721. DUK_ASSERT_API_ENTRY(thr);
  17722. DUK_ASSERT(duk_get_hbuffer(thr, idx) != NULL);
  17723. return (duk_hbuffer *) duk__known_heaphdr(thr, idx);
  17724. }
  17725. DUK_INTERNAL duk_hcompfunc *duk_known_hcompfunc(duk_hthread *thr, duk_idx_t idx) {
  17726. DUK_ASSERT_API_ENTRY(thr);
  17727. DUK_ASSERT(duk_get_hcompfunc(thr, idx) != NULL);
  17728. return (duk_hcompfunc *) duk__known_heaphdr(thr, idx);
  17729. }
  17730. DUK_INTERNAL duk_hnatfunc *duk_known_hnatfunc(duk_hthread *thr, duk_idx_t idx) {
  17731. DUK_ASSERT_API_ENTRY(thr);
  17732. DUK_ASSERT(duk_get_hnatfunc(thr, idx) != NULL);
  17733. return (duk_hnatfunc *) duk__known_heaphdr(thr, idx);
  17734. }
  17735. DUK_EXTERNAL void duk_set_length(duk_hthread *thr, duk_idx_t idx, duk_size_t len) {
  17736. DUK_ASSERT_API_ENTRY(thr);
  17737. idx = duk_normalize_index(thr, idx);
  17738. duk_push_uint(thr, (duk_uint_t) len);
  17739. duk_put_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
  17740. }
  17741. /*
  17742. * Conversions and coercions
  17743. *
  17744. * The conversion/coercions are in-place operations on the value stack.
  17745. * Some operations are implemented here directly, while others call a
  17746. * helper in duk_js_ops.c after validating arguments.
  17747. */
  17748. /* E5 Section 8.12.8 */
  17749. DUK_LOCAL duk_bool_t duk__defaultvalue_coerce_attempt(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t func_stridx) {
  17750. if (duk_get_prop_stridx(thr, idx, func_stridx)) {
  17751. /* [ ... func ] */
  17752. if (duk_is_callable(thr, -1)) {
  17753. duk_dup(thr, idx); /* -> [ ... func this ] */
  17754. duk_call_method(thr, 0); /* -> [ ... retval ] */
  17755. if (duk_is_primitive(thr, -1)) {
  17756. duk_replace(thr, idx);
  17757. return 1;
  17758. }
  17759. /* [ ... retval ]; popped below */
  17760. }
  17761. }
  17762. duk_pop_unsafe(thr); /* [ ... func/retval ] -> [ ... ] */
  17763. return 0;
  17764. }
  17765. DUK_EXTERNAL void duk_to_undefined(duk_hthread *thr, duk_idx_t idx) {
  17766. duk_tval *tv;
  17767. DUK_ASSERT_API_ENTRY(thr);
  17768. tv = duk_require_tval(thr, idx);
  17769. DUK_ASSERT(tv != NULL);
  17770. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  17771. }
  17772. DUK_EXTERNAL void duk_to_null(duk_hthread *thr, duk_idx_t idx) {
  17773. duk_tval *tv;
  17774. DUK_ASSERT_API_ENTRY(thr);
  17775. tv = duk_require_tval(thr, idx);
  17776. DUK_ASSERT(tv != NULL);
  17777. DUK_TVAL_SET_NULL_UPDREF(thr, tv); /* side effects */
  17778. }
  17779. /* E5 Section 9.1 */
  17780. DUK_LOCAL const char * const duk__toprim_hint_strings[3] = {
  17781. "default", "string", "number"
  17782. };
  17783. DUK_LOCAL void duk__to_primitive_helper(duk_hthread *thr, duk_idx_t idx, duk_int_t hint, duk_bool_t check_symbol) {
  17784. /* Inline initializer for coercers[] is not allowed by old compilers like BCC. */
  17785. duk_small_uint_t coercers[2];
  17786. DUK_ASSERT_API_ENTRY(thr);
  17787. DUK_ASSERT(hint == DUK_HINT_NONE || hint == DUK_HINT_NUMBER || hint == DUK_HINT_STRING);
  17788. idx = duk_require_normalize_index(thr, idx);
  17789. /* If already primitive, return as is. */
  17790. if (!duk_check_type_mask(thr, idx, DUK_TYPE_MASK_OBJECT |
  17791. DUK_TYPE_MASK_LIGHTFUNC |
  17792. DUK_TYPE_MASK_BUFFER)) {
  17793. DUK_ASSERT(!duk_is_buffer(thr, idx)); /* duk_to_string() relies on this behavior */
  17794. return;
  17795. }
  17796. /* @@toPrimitive lookup. Also do for plain buffers and lightfuncs
  17797. * which mimic objects.
  17798. */
  17799. if (check_symbol && duk_get_method_stridx(thr, idx, DUK_STRIDX_WELLKNOWN_SYMBOL_TO_PRIMITIVE)) {
  17800. DUK_ASSERT(hint >= 0 && (duk_size_t) hint < sizeof(duk__toprim_hint_strings) / sizeof(const char *));
  17801. duk_dup(thr, idx);
  17802. duk_push_string(thr, duk__toprim_hint_strings[hint]);
  17803. duk_call_method(thr, 1); /* [ ... method value hint ] -> [ ... res] */
  17804. if (duk_check_type_mask(thr, -1, DUK_TYPE_MASK_OBJECT |
  17805. DUK_TYPE_MASK_LIGHTFUNC |
  17806. DUK_TYPE_MASK_BUFFER)) {
  17807. goto fail;
  17808. }
  17809. duk_replace(thr, idx);
  17810. return;
  17811. }
  17812. /* Objects are coerced based on E5 specification.
  17813. * Lightfuncs are coerced because they behave like
  17814. * objects even if they're internally a primitive
  17815. * type. Same applies to plain buffers, which behave
  17816. * like ArrayBuffer objects since Duktape 2.x.
  17817. */
  17818. /* Hint magic for Date is unnecessary in ES2015 because of
  17819. * Date.prototype[@@toPrimitive]. However, it is needed if
  17820. * symbol support is not enabled.
  17821. */
  17822. #if defined(DUK_USE_SYMBOL_BUILTIN)
  17823. if (hint == DUK_HINT_NONE) {
  17824. hint = DUK_HINT_NUMBER;
  17825. }
  17826. #else /* DUK_USE_SYMBOL_BUILTIN */
  17827. if (hint == DUK_HINT_NONE) {
  17828. duk_small_uint_t class_number;
  17829. class_number = duk_get_class_number(thr, idx);
  17830. if (class_number == DUK_HOBJECT_CLASS_DATE) {
  17831. hint = DUK_HINT_STRING;
  17832. } else {
  17833. hint = DUK_HINT_NUMBER;
  17834. }
  17835. }
  17836. #endif /* DUK_USE_SYMBOL_BUILTIN */
  17837. coercers[0] = DUK_STRIDX_VALUE_OF;
  17838. coercers[1] = DUK_STRIDX_TO_STRING;
  17839. if (hint == DUK_HINT_STRING) {
  17840. coercers[0] = DUK_STRIDX_TO_STRING;
  17841. coercers[1] = DUK_STRIDX_VALUE_OF;
  17842. }
  17843. if (duk__defaultvalue_coerce_attempt(thr, idx, coercers[0])) {
  17844. DUK_ASSERT(!duk_is_buffer(thr, idx)); /* duk_to_string() relies on this behavior */
  17845. return;
  17846. }
  17847. if (duk__defaultvalue_coerce_attempt(thr, idx, coercers[1])) {
  17848. DUK_ASSERT(!duk_is_buffer(thr, idx)); /* duk_to_string() relies on this behavior */
  17849. return;
  17850. }
  17851. fail:
  17852. DUK_ERROR_TYPE(thr, DUK_STR_TOPRIMITIVE_FAILED);
  17853. DUK_WO_NORETURN(return;);
  17854. }
  17855. DUK_EXTERNAL void duk_to_primitive(duk_hthread *thr, duk_idx_t idx, duk_int_t hint) {
  17856. duk__to_primitive_helper(thr, idx, hint, 1 /*check_symbol*/);
  17857. }
  17858. #if defined(DUK_USE_SYMBOL_BUILTIN)
  17859. DUK_INTERNAL void duk_to_primitive_ordinary(duk_hthread *thr, duk_idx_t idx, duk_int_t hint) {
  17860. duk__to_primitive_helper(thr, idx, hint, 0 /*check_symbol*/);
  17861. }
  17862. #endif
  17863. /* E5 Section 9.2 */
  17864. DUK_EXTERNAL duk_bool_t duk_to_boolean(duk_hthread *thr, duk_idx_t idx) {
  17865. duk_tval *tv;
  17866. duk_bool_t val;
  17867. DUK_ASSERT_API_ENTRY(thr);
  17868. idx = duk_require_normalize_index(thr, idx);
  17869. tv = DUK_GET_TVAL_POSIDX(thr, idx);
  17870. DUK_ASSERT(tv != NULL);
  17871. val = duk_js_toboolean(tv);
  17872. DUK_ASSERT(val == 0 || val == 1);
  17873. /* Note: no need to re-lookup tv, conversion is side effect free. */
  17874. DUK_ASSERT(tv != NULL);
  17875. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, val); /* side effects */
  17876. return val;
  17877. }
  17878. DUK_INTERNAL duk_bool_t duk_to_boolean_top_pop(duk_hthread *thr) {
  17879. duk_tval *tv;
  17880. duk_bool_t val;
  17881. DUK_ASSERT_API_ENTRY(thr);
  17882. tv = duk_require_tval(thr, -1);
  17883. DUK_ASSERT(tv != NULL);
  17884. val = duk_js_toboolean(tv);
  17885. DUK_ASSERT(val == 0 || val == 1);
  17886. duk_pop_unsafe(thr);
  17887. return val;
  17888. }
  17889. DUK_EXTERNAL duk_double_t duk_to_number(duk_hthread *thr, duk_idx_t idx) {
  17890. duk_tval *tv;
  17891. duk_double_t d;
  17892. DUK_ASSERT_API_ENTRY(thr);
  17893. /* XXX: No need to normalize; the whole operation could be inlined here to
  17894. * avoid 'tv' re-lookup.
  17895. */
  17896. idx = duk_require_normalize_index(thr, idx);
  17897. tv = DUK_GET_TVAL_POSIDX(thr, idx);
  17898. DUK_ASSERT(tv != NULL);
  17899. d = duk_js_tonumber(thr, tv); /* XXX: fastint coercion? now result will always be a non-fastint */
  17900. /* ToNumber() may have side effects so must relookup 'tv'. */
  17901. tv = DUK_GET_TVAL_POSIDX(thr, idx);
  17902. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
  17903. return d;
  17904. }
  17905. DUK_INTERNAL duk_double_t duk_to_number_m1(duk_hthread *thr) {
  17906. DUK_ASSERT_API_ENTRY(thr);
  17907. return duk_to_number(thr, -1);
  17908. }
  17909. DUK_INTERNAL duk_double_t duk_to_number_m2(duk_hthread *thr) {
  17910. DUK_ASSERT_API_ENTRY(thr);
  17911. return duk_to_number(thr, -2);
  17912. }
  17913. DUK_INTERNAL duk_double_t duk_to_number_tval(duk_hthread *thr, duk_tval *tv) {
  17914. #if defined(DUK_USE_PREFER_SIZE)
  17915. duk_double_t res;
  17916. DUK_ASSERT_API_ENTRY(thr);
  17917. duk_push_tval(thr, tv);
  17918. res = duk_to_number_m1(thr);
  17919. duk_pop_unsafe(thr);
  17920. return res;
  17921. #else
  17922. duk_double_t res;
  17923. duk_tval *tv_dst;
  17924. DUK_ASSERT_API_ENTRY(thr);
  17925. DUK__ASSERT_SPACE();
  17926. tv_dst = thr->valstack_top++;
  17927. DUK_TVAL_SET_TVAL(tv_dst, tv);
  17928. DUK_TVAL_INCREF(thr, tv_dst); /* decref not necessary */
  17929. res = duk_to_number_m1(thr); /* invalidates tv_dst */
  17930. tv_dst = --thr->valstack_top;
  17931. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_dst));
  17932. DUK_ASSERT(!DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv_dst)); /* plain number */
  17933. DUK_TVAL_SET_UNDEFINED(tv_dst); /* valstack init policy */
  17934. return res;
  17935. #endif
  17936. }
  17937. /* XXX: combine all the integer conversions: they share everything
  17938. * but the helper function for coercion.
  17939. */
  17940. typedef duk_double_t (*duk__toint_coercer)(duk_hthread *thr, duk_tval *tv);
  17941. DUK_LOCAL duk_double_t duk__to_int_uint_helper(duk_hthread *thr, duk_idx_t idx, duk__toint_coercer coerce_func) {
  17942. duk_tval *tv;
  17943. duk_double_t d;
  17944. DUK_ASSERT_CTX_VALID(thr);
  17945. tv = duk_require_tval(thr, idx);
  17946. DUK_ASSERT(tv != NULL);
  17947. #if defined(DUK_USE_FASTINT)
  17948. /* If argument is a fastint, guarantee that it remains one.
  17949. * There's no downgrade check for other cases.
  17950. */
  17951. if (DUK_TVAL_IS_FASTINT(tv)) {
  17952. /* XXX: Unnecessary conversion back and forth. */
  17953. return (duk_double_t) DUK_TVAL_GET_FASTINT(tv);
  17954. }
  17955. #endif
  17956. d = coerce_func(thr, tv);
  17957. /* XXX: fastint? */
  17958. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17959. tv = duk_require_tval(thr, idx);
  17960. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, d); /* side effects */
  17961. return d;
  17962. }
  17963. DUK_EXTERNAL duk_int_t duk_to_int(duk_hthread *thr, duk_idx_t idx) {
  17964. /* Value coercion (in stack): ToInteger(), E5 Section 9.4,
  17965. * API return value coercion: custom.
  17966. */
  17967. DUK_ASSERT_API_ENTRY(thr);
  17968. (void) duk__to_int_uint_helper(thr, idx, duk_js_tointeger);
  17969. return (duk_int_t) duk__api_coerce_d2i(thr, idx, 0 /*def_value*/, 0 /*require*/);
  17970. }
  17971. DUK_EXTERNAL duk_uint_t duk_to_uint(duk_hthread *thr, duk_idx_t idx) {
  17972. /* Value coercion (in stack): ToInteger(), E5 Section 9.4,
  17973. * API return value coercion: custom.
  17974. */
  17975. DUK_ASSERT_API_ENTRY(thr);
  17976. (void) duk__to_int_uint_helper(thr, idx, duk_js_tointeger);
  17977. return (duk_uint_t) duk__api_coerce_d2ui(thr, idx, 0 /*def_value*/, 0 /*require*/);
  17978. }
  17979. DUK_EXTERNAL duk_int32_t duk_to_int32(duk_hthread *thr, duk_idx_t idx) {
  17980. duk_tval *tv;
  17981. duk_int32_t ret;
  17982. DUK_ASSERT_API_ENTRY(thr);
  17983. tv = duk_require_tval(thr, idx);
  17984. DUK_ASSERT(tv != NULL);
  17985. ret = duk_js_toint32(thr, tv);
  17986. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17987. tv = duk_require_tval(thr, idx);
  17988. DUK_TVAL_SET_I32_UPDREF(thr, tv, ret); /* side effects */
  17989. return ret;
  17990. }
  17991. DUK_EXTERNAL duk_uint32_t duk_to_uint32(duk_hthread *thr, duk_idx_t idx) {
  17992. duk_tval *tv;
  17993. duk_uint32_t ret;
  17994. DUK_ASSERT_API_ENTRY(thr);
  17995. tv = duk_require_tval(thr, idx);
  17996. DUK_ASSERT(tv != NULL);
  17997. ret = duk_js_touint32(thr, tv);
  17998. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  17999. tv = duk_require_tval(thr, idx);
  18000. DUK_TVAL_SET_U32_UPDREF(thr, tv, ret); /* side effects */
  18001. return ret;
  18002. }
  18003. DUK_EXTERNAL duk_uint16_t duk_to_uint16(duk_hthread *thr, duk_idx_t idx) {
  18004. duk_tval *tv;
  18005. duk_uint16_t ret;
  18006. DUK_ASSERT_API_ENTRY(thr);
  18007. tv = duk_require_tval(thr, idx);
  18008. DUK_ASSERT(tv != NULL);
  18009. ret = duk_js_touint16(thr, tv);
  18010. /* Relookup in case coerce_func() has side effects, e.g. ends up coercing an object */
  18011. tv = duk_require_tval(thr, idx);
  18012. DUK_TVAL_SET_U32_UPDREF(thr, tv, ret); /* side effects */
  18013. return ret;
  18014. }
  18015. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  18016. /* Special coercion for Uint8ClampedArray. */
  18017. DUK_INTERNAL duk_uint8_t duk_to_uint8clamped(duk_hthread *thr, duk_idx_t idx) {
  18018. duk_double_t d;
  18019. duk_double_t t;
  18020. duk_uint8_t ret;
  18021. DUK_ASSERT_API_ENTRY(thr);
  18022. /* XXX: Simplify this algorithm, should be possible to come up with
  18023. * a shorter and faster algorithm by inspecting IEEE representation
  18024. * directly.
  18025. */
  18026. d = duk_to_number(thr, idx);
  18027. if (d <= 0.0) {
  18028. return 0;
  18029. } else if (d >= 255) {
  18030. return 255;
  18031. } else if (DUK_ISNAN(d)) {
  18032. /* Avoid NaN-to-integer coercion as it is compiler specific. */
  18033. return 0;
  18034. }
  18035. t = d - DUK_FLOOR(d);
  18036. if (t == 0.5) {
  18037. /* Exact halfway, round to even. */
  18038. ret = (duk_uint8_t) d;
  18039. ret = (ret + 1) & 0xfe; /* Example: d=3.5, t=0.5 -> ret = (3 + 1) & 0xfe = 4 & 0xfe = 4
  18040. * Example: d=4.5, t=0.5 -> ret = (4 + 1) & 0xfe = 5 & 0xfe = 4
  18041. */
  18042. } else {
  18043. /* Not halfway, round to nearest. */
  18044. ret = (duk_uint8_t) (d + 0.5);
  18045. }
  18046. return ret;
  18047. }
  18048. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  18049. DUK_EXTERNAL const char *duk_to_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
  18050. DUK_ASSERT_API_ENTRY(thr);
  18051. (void) duk_to_string(thr, idx);
  18052. DUK_ASSERT(duk_is_string(thr, idx));
  18053. return duk_require_lstring(thr, idx, out_len);
  18054. }
  18055. DUK_LOCAL duk_ret_t duk__safe_to_string_raw(duk_hthread *thr, void *udata) {
  18056. DUK_ASSERT_CTX_VALID(thr);
  18057. DUK_UNREF(udata);
  18058. duk_to_string(thr, -1);
  18059. return 1;
  18060. }
  18061. DUK_EXTERNAL const char *duk_safe_to_lstring(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_len) {
  18062. DUK_ASSERT_API_ENTRY(thr);
  18063. idx = duk_require_normalize_index(thr, idx);
  18064. /* We intentionally ignore the duk_safe_call() return value and only
  18065. * check the output type. This way we don't also need to check that
  18066. * the returned value is indeed a string in the success case.
  18067. */
  18068. duk_dup(thr, idx);
  18069. (void) duk_safe_call(thr, duk__safe_to_string_raw, NULL /*udata*/, 1 /*nargs*/, 1 /*nrets*/);
  18070. if (!duk_is_string(thr, -1)) {
  18071. /* Error: try coercing error to string once. */
  18072. (void) duk_safe_call(thr, duk__safe_to_string_raw, NULL /*udata*/, 1 /*nargs*/, 1 /*nrets*/);
  18073. if (!duk_is_string(thr, -1)) {
  18074. /* Double error */
  18075. duk_pop_unsafe(thr);
  18076. duk_push_hstring_stridx(thr, DUK_STRIDX_UC_ERROR);
  18077. } else {
  18078. ;
  18079. }
  18080. } else {
  18081. /* String; may be a symbol, accepted. */
  18082. ;
  18083. }
  18084. DUK_ASSERT(duk_is_string(thr, -1));
  18085. duk_replace(thr, idx);
  18086. DUK_ASSERT(duk_get_string(thr, idx) != NULL);
  18087. return duk_get_lstring(thr, idx, out_len);
  18088. }
  18089. DUK_INTERNAL duk_hstring *duk_to_property_key_hstring(duk_hthread *thr, duk_idx_t idx) {
  18090. duk_hstring *h;
  18091. DUK_ASSERT_API_ENTRY(thr);
  18092. duk_to_primitive(thr, idx, DUK_HINT_STRING); /* needed for e.g. Symbol objects */
  18093. h = duk_get_hstring(thr, idx);
  18094. if (h == NULL) {
  18095. /* The "is string?" check may seem unnecessary, but as things
  18096. * are duk_to_hstring() invokes ToString() which fails for
  18097. * symbols. But since symbols are already strings for Duktape
  18098. * C API, we check for that before doing the coercion.
  18099. */
  18100. h = duk_to_hstring(thr, idx);
  18101. }
  18102. DUK_ASSERT(h != NULL);
  18103. return h;
  18104. }
  18105. #if defined(DUK_USE_DEBUGGER_SUPPORT) /* only needed by debugger for now */
  18106. DUK_INTERNAL duk_hstring *duk_safe_to_hstring(duk_hthread *thr, duk_idx_t idx) {
  18107. DUK_ASSERT_API_ENTRY(thr);
  18108. (void) duk_safe_to_string(thr, idx);
  18109. DUK_ASSERT(duk_is_string(thr, idx));
  18110. DUK_ASSERT(duk_get_hstring(thr, idx) != NULL);
  18111. return duk_known_hstring(thr, idx);
  18112. }
  18113. #endif
  18114. /* Push Object.prototype.toString() output for 'tv'. */
  18115. #if 0 /* See XXX note why this variant doesn't work. */
  18116. DUK_INTERNAL void duk_push_class_string_tval(duk_hthread *thr, duk_tval *tv, duk_bool_t avoid_side_effects) {
  18117. duk_uint_t stridx_bidx = 0; /* (prototype_bidx << 16) + default_tag_stridx */
  18118. DUK_ASSERT_API_ENTRY(thr);
  18119. /* Conceptually for any non-undefined/null value we should do a
  18120. * ToObject() coercion and look up @@toStringTag (from the object
  18121. * prototype) to see if a custom tag should be used. Avoid the
  18122. * actual conversion by doing a prototype lookup without the object
  18123. * coercion. However, see problem below.
  18124. */
  18125. duk_push_literal(thr, "[object "); /* -> [ ... "[object" ] */
  18126. switch (DUK_TVAL_GET_TAG(tv)) {
  18127. case DUK_TAG_UNUSED: /* Treat like 'undefined', shouldn't happen. */
  18128. case DUK_TAG_UNDEFINED: {
  18129. stridx_bidx = DUK_STRIDX_UC_UNDEFINED;
  18130. goto use_stridx;
  18131. }
  18132. case DUK_TAG_NULL: {
  18133. stridx_bidx = DUK_STRIDX_UC_NULL;
  18134. goto use_stridx;
  18135. }
  18136. case DUK_TAG_BOOLEAN: {
  18137. stridx_bidx = (DUK_BIDX_BOOLEAN_PROTOTYPE << 16) + DUK_STRIDX_UC_BOOLEAN;
  18138. goto use_proto_bidx;
  18139. }
  18140. case DUK_TAG_POINTER: {
  18141. stridx_bidx = (DUK_BIDX_POINTER_PROTOTYPE << 16) + DUK_STRIDX_UC_POINTER;
  18142. goto use_proto_bidx;
  18143. }
  18144. case DUK_TAG_LIGHTFUNC: {
  18145. stridx_bidx = (DUK_BIDX_FUNCTION_PROTOTYPE << 16) + DUK_STRIDX_UC_FUNCTION;
  18146. goto use_proto_bidx;
  18147. }
  18148. case DUK_TAG_STRING: {
  18149. duk_hstring *h;
  18150. h = DUK_TVAL_GET_STRING(tv);
  18151. DUK_ASSERT(h != NULL);
  18152. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  18153. /* Even without DUK_USE_SYMBOL_BUILTIN the Symbol
  18154. * prototype exists so we can lookup @@toStringTag
  18155. * and provide [object Symbol] for symbol values
  18156. * created from C code.
  18157. */
  18158. stridx_bidx = (DUK_BIDX_SYMBOL_PROTOTYPE << 16) + DUK_STRIDX_UC_SYMBOL;
  18159. } else {
  18160. stridx_bidx = (DUK_BIDX_STRING_PROTOTYPE << 16) + DUK_STRIDX_UC_STRING;
  18161. }
  18162. goto use_proto_bidx;
  18163. }
  18164. case DUK_TAG_OBJECT: {
  18165. duk_push_tval(thr, tv);
  18166. stridx_bidx = 0xffffffffUL; /* Marker value. */
  18167. goto use_pushed_object;
  18168. }
  18169. case DUK_TAG_BUFFER: {
  18170. stridx_bidx = (DUK_BIDX_UINT8ARRAY_PROTOTYPE << 16) + DUK_STRIDX_UINT8_ARRAY;
  18171. goto use_proto_bidx;
  18172. }
  18173. #if defined(DUK_USE_FASTINT)
  18174. case DUK_TAG_FASTINT:
  18175. /* Fall through to generic number case. */
  18176. #endif
  18177. default: {
  18178. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv)); /* number (maybe fastint) */
  18179. stridx_bidx = (DUK_BIDX_NUMBER_PROTOTYPE << 16) + DUK_STRIDX_UC_NUMBER;
  18180. goto use_proto_bidx;
  18181. }
  18182. }
  18183. DUK_ASSERT(0); /* Never here. */
  18184. use_proto_bidx:
  18185. DUK_ASSERT_BIDX_VALID((stridx_bidx >> 16) & 0xffffUL);
  18186. duk_push_hobject(thr, thr->builtins[(stridx_bidx >> 16) & 0xffffUL]);
  18187. /* Fall through. */
  18188. use_pushed_object:
  18189. /* [ ... "[object" obj ] */
  18190. #if defined(DUK_USE_SYMBOL_BUILTIN)
  18191. /* XXX: better handling with avoid_side_effects == 1; lookup tval
  18192. * without Proxy or getter side effects, and use it in sanitized
  18193. * form if it's a string.
  18194. */
  18195. if (!avoid_side_effects) {
  18196. /* XXX: The problem with using the prototype object as the
  18197. * lookup base is that if @@toStringTag is a getter, its
  18198. * 'this' binding must be the ToObject() coerced input value,
  18199. * not the prototype object of the type.
  18200. */
  18201. (void) duk_get_prop_stridx(thr, -1, DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG);
  18202. if (duk_is_string_notsymbol(thr, -1)) {
  18203. duk_remove_m2(thr);
  18204. goto finish;
  18205. }
  18206. duk_pop_unsafe(thr);
  18207. }
  18208. #endif
  18209. if (stridx_bidx == 0xffffffffUL) {
  18210. duk_hobject *h_obj;
  18211. duk_small_uint_t classnum;
  18212. h_obj = duk_known_hobject(thr, -1);
  18213. DUK_ASSERT(h_obj != NULL);
  18214. classnum = DUK_HOBJECT_GET_CLASS_NUMBER(h_obj);
  18215. stridx_bidx = DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum);
  18216. } else {
  18217. /* stridx_bidx already has the desired fallback stridx. */
  18218. ;
  18219. }
  18220. duk_pop_unsafe(thr);
  18221. /* Fall through. */
  18222. use_stridx:
  18223. /* [ ... "[object" ] */
  18224. duk_push_hstring_stridx(thr, stridx_bidx & 0xffffUL);
  18225. finish:
  18226. /* [ ... "[object" tag ] */
  18227. duk_push_literal(thr, "]");
  18228. duk_concat(thr, 3); /* [ ... "[object" tag "]" ] -> [ ... res ] */
  18229. }
  18230. #endif /* 0 */
  18231. DUK_INTERNAL void duk_push_class_string_tval(duk_hthread *thr, duk_tval *tv, duk_bool_t avoid_side_effects) {
  18232. duk_hobject *h_obj;
  18233. duk_small_uint_t classnum;
  18234. duk_small_uint_t stridx;
  18235. duk_tval tv_tmp;
  18236. DUK_ASSERT_API_ENTRY(thr);
  18237. DUK_ASSERT(tv != NULL);
  18238. /* Stabilize 'tv', duk_push_literal() may trigger side effects. */
  18239. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  18240. tv = &tv_tmp;
  18241. /* Conceptually for any non-undefined/null value we should do a
  18242. * ToObject() coercion and look up @@toStringTag (from the object
  18243. * prototype) to see if a custom result should be used. We'd like to
  18244. * avoid the actual conversion, but even for primitive types the
  18245. * prototype may have @@toStringTag. What's worse, the @@toStringTag
  18246. * property may be a getter that must get the object coerced value
  18247. * (not the prototype) as its 'this' binding.
  18248. *
  18249. * For now, do an actual object coercion. This could be avoided by
  18250. * doing a side effect free lookup to see if a getter would be invoked.
  18251. * If not, the value can be read directly and the object coercion could
  18252. * be avoided. This may not be worth it in practice, because
  18253. * Object.prototype.toString() is usually not performance critical.
  18254. */
  18255. duk_push_literal(thr, "[object "); /* -> [ ... "[object" ] */
  18256. switch (DUK_TVAL_GET_TAG(tv)) {
  18257. case DUK_TAG_UNUSED: /* Treat like 'undefined', shouldn't happen. */
  18258. case DUK_TAG_UNDEFINED: {
  18259. duk_push_hstring_stridx(thr, DUK_STRIDX_UC_UNDEFINED);
  18260. goto finish;
  18261. }
  18262. case DUK_TAG_NULL: {
  18263. duk_push_hstring_stridx(thr, DUK_STRIDX_UC_NULL);
  18264. goto finish;
  18265. }
  18266. }
  18267. duk_push_tval(thr, tv);
  18268. tv = NULL; /* Invalidated by ToObject(). */
  18269. duk_to_object(thr, -1);
  18270. /* [ ... "[object" obj ] */
  18271. #if defined(DUK_USE_SYMBOL_BUILTIN)
  18272. /* XXX: better handling with avoid_side_effects == 1; lookup tval
  18273. * without Proxy or getter side effects, and use it in sanitized
  18274. * form if it's a string.
  18275. */
  18276. if (!avoid_side_effects) {
  18277. (void) duk_get_prop_stridx(thr, -1, DUK_STRIDX_WELLKNOWN_SYMBOL_TO_STRING_TAG);
  18278. if (duk_is_string_notsymbol(thr, -1)) {
  18279. duk_remove_m2(thr);
  18280. goto finish;
  18281. }
  18282. duk_pop_unsafe(thr);
  18283. }
  18284. #else
  18285. DUK_UNREF(avoid_side_effects);
  18286. #endif
  18287. h_obj = duk_known_hobject(thr, -1);
  18288. DUK_ASSERT(h_obj != NULL);
  18289. classnum = DUK_HOBJECT_GET_CLASS_NUMBER(h_obj);
  18290. stridx = DUK_HOBJECT_CLASS_NUMBER_TO_STRIDX(classnum);
  18291. duk_pop_unsafe(thr);
  18292. duk_push_hstring_stridx(thr, stridx);
  18293. finish:
  18294. /* [ ... "[object" tag ] */
  18295. duk_push_literal(thr, "]");
  18296. duk_concat(thr, 3); /* [ ... "[object" tag "]" ] -> [ ... res ] */
  18297. }
  18298. /* XXX: other variants like uint, u32 etc */
  18299. DUK_INTERNAL duk_int_t duk_to_int_clamped_raw(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval, duk_bool_t *out_clamped) {
  18300. duk_tval *tv;
  18301. duk_tval tv_tmp;
  18302. duk_double_t d, dmin, dmax;
  18303. duk_int_t res;
  18304. duk_bool_t clamped = 0;
  18305. DUK_ASSERT_API_ENTRY(thr);
  18306. tv = duk_require_tval(thr, idx);
  18307. DUK_ASSERT(tv != NULL);
  18308. d = duk_js_tointeger(thr, tv); /* E5 Section 9.4, ToInteger() */
  18309. dmin = (duk_double_t) minval;
  18310. dmax = (duk_double_t) maxval;
  18311. if (d < dmin) {
  18312. clamped = 1;
  18313. res = minval;
  18314. d = dmin;
  18315. } else if (d > dmax) {
  18316. clamped = 1;
  18317. res = maxval;
  18318. d = dmax;
  18319. } else {
  18320. res = (duk_int_t) d;
  18321. }
  18322. DUK_UNREF(d); /* SCANBUILD: with suitable dmin/dmax limits 'd' is unused */
  18323. /* 'd' and 'res' agree here */
  18324. /* Relookup in case duk_js_tointeger() ends up e.g. coercing an object. */
  18325. tv = duk_get_tval(thr, idx);
  18326. DUK_ASSERT(tv != NULL); /* not popped by side effect */
  18327. DUK_TVAL_SET_TVAL(&tv_tmp, tv);
  18328. #if defined(DUK_USE_FASTINT)
  18329. #if (DUK_INT_MAX <= 0x7fffffffL)
  18330. DUK_TVAL_SET_I32(tv, res);
  18331. #else
  18332. /* Clamping needed if duk_int_t is 64 bits. */
  18333. if (res >= DUK_FASTINT_MIN && res <= DUK_FASTINT_MAX) {
  18334. DUK_TVAL_SET_FASTINT(tv, res);
  18335. } else {
  18336. DUK_TVAL_SET_NUMBER(tv, d);
  18337. }
  18338. #endif
  18339. #else
  18340. DUK_TVAL_SET_NUMBER(tv, d); /* no need to incref */
  18341. #endif
  18342. DUK_TVAL_DECREF(thr, &tv_tmp); /* side effects */
  18343. if (out_clamped) {
  18344. *out_clamped = clamped;
  18345. } else {
  18346. /* coerced value is updated to value stack even when RangeError thrown */
  18347. if (clamped) {
  18348. DUK_ERROR_RANGE(thr, DUK_STR_NUMBER_OUTSIDE_RANGE);
  18349. DUK_WO_NORETURN(return 0;);
  18350. }
  18351. }
  18352. return res;
  18353. }
  18354. DUK_INTERNAL duk_int_t duk_to_int_clamped(duk_hthread *thr, duk_idx_t idx, duk_idx_t minval, duk_idx_t maxval) {
  18355. duk_bool_t dummy;
  18356. DUK_ASSERT_API_ENTRY(thr);
  18357. return duk_to_int_clamped_raw(thr, idx, minval, maxval, &dummy);
  18358. }
  18359. DUK_INTERNAL duk_int_t duk_to_int_check_range(duk_hthread *thr, duk_idx_t idx, duk_int_t minval, duk_int_t maxval) {
  18360. DUK_ASSERT_API_ENTRY(thr);
  18361. return duk_to_int_clamped_raw(thr, idx, minval, maxval, NULL); /* out_clamped==NULL -> RangeError if outside range */
  18362. }
  18363. DUK_EXTERNAL const char *duk_to_string(duk_hthread *thr, duk_idx_t idx) {
  18364. duk_tval *tv;
  18365. DUK_ASSERT_API_ENTRY(thr);
  18366. idx = duk_require_normalize_index(thr, idx);
  18367. tv = DUK_GET_TVAL_POSIDX(thr, idx);
  18368. DUK_ASSERT(tv != NULL);
  18369. switch (DUK_TVAL_GET_TAG(tv)) {
  18370. case DUK_TAG_UNDEFINED: {
  18371. duk_push_hstring_stridx(thr, DUK_STRIDX_LC_UNDEFINED);
  18372. break;
  18373. }
  18374. case DUK_TAG_NULL: {
  18375. duk_push_hstring_stridx(thr, DUK_STRIDX_LC_NULL);
  18376. break;
  18377. }
  18378. case DUK_TAG_BOOLEAN: {
  18379. if (DUK_TVAL_GET_BOOLEAN(tv)) {
  18380. duk_push_hstring_stridx(thr, DUK_STRIDX_TRUE);
  18381. } else {
  18382. duk_push_hstring_stridx(thr, DUK_STRIDX_FALSE);
  18383. }
  18384. break;
  18385. }
  18386. case DUK_TAG_STRING: {
  18387. /* Nop for actual strings, TypeError for Symbols.
  18388. * Because various internals rely on ToString() coercion of
  18389. * internal strings, -allow- (NOP) string coercion for hidden
  18390. * symbols.
  18391. */
  18392. #if 1
  18393. duk_hstring *h;
  18394. h = DUK_TVAL_GET_STRING(tv);
  18395. DUK_ASSERT(h != NULL);
  18396. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  18397. DUK_ERROR_TYPE(thr, DUK_STR_CANNOT_STRING_COERCE_SYMBOL);
  18398. DUK_WO_NORETURN(goto skip_replace;);
  18399. } else {
  18400. goto skip_replace;
  18401. }
  18402. #else
  18403. goto skip_replace;
  18404. #endif
  18405. break;
  18406. }
  18407. case DUK_TAG_BUFFER: /* Go through Uint8Array.prototype.toString() for coercion. */
  18408. case DUK_TAG_OBJECT: {
  18409. /* Plain buffers: go through ArrayBuffer.prototype.toString()
  18410. * for coercion.
  18411. *
  18412. * Symbol objects: duk_to_primitive() results in a plain symbol
  18413. * value, and duk_to_string() then causes a TypeError.
  18414. */
  18415. duk_to_primitive(thr, idx, DUK_HINT_STRING);
  18416. DUK_ASSERT(!duk_is_buffer(thr, idx)); /* ToPrimitive() must guarantee */
  18417. DUK_ASSERT(!duk_is_object(thr, idx));
  18418. return duk_to_string(thr, idx); /* Note: recursive call */
  18419. }
  18420. case DUK_TAG_POINTER: {
  18421. void *ptr = DUK_TVAL_GET_POINTER(tv);
  18422. if (ptr != NULL) {
  18423. duk_push_sprintf(thr, DUK_STR_FMT_PTR, (void *) ptr);
  18424. } else {
  18425. /* Represent a null pointer as 'null' to be consistent with
  18426. * the JX format variant. Native '%p' format for a NULL
  18427. * pointer may be e.g. '(nil)'.
  18428. */
  18429. duk_push_hstring_stridx(thr, DUK_STRIDX_LC_NULL);
  18430. }
  18431. break;
  18432. }
  18433. case DUK_TAG_LIGHTFUNC: {
  18434. /* Should match Function.prototype.toString() */
  18435. duk_push_lightfunc_tostring(thr, tv);
  18436. break;
  18437. }
  18438. #if defined(DUK_USE_FASTINT)
  18439. case DUK_TAG_FASTINT:
  18440. #endif
  18441. default: {
  18442. /* number */
  18443. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18444. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18445. duk_push_tval(thr, tv);
  18446. duk_numconv_stringify(thr,
  18447. 10 /*radix*/,
  18448. 0 /*precision:shortest*/,
  18449. 0 /*force_exponential*/);
  18450. break;
  18451. }
  18452. }
  18453. duk_replace(thr, idx);
  18454. skip_replace:
  18455. DUK_ASSERT(duk_is_string(thr, idx));
  18456. return duk_require_string(thr, idx);
  18457. }
  18458. DUK_INTERNAL duk_hstring *duk_to_hstring(duk_hthread *thr, duk_idx_t idx) {
  18459. duk_hstring *ret;
  18460. DUK_ASSERT_API_ENTRY(thr);
  18461. duk_to_string(thr, idx);
  18462. ret = duk_get_hstring(thr, idx);
  18463. DUK_ASSERT(ret != NULL);
  18464. return ret;
  18465. }
  18466. DUK_INTERNAL duk_hstring *duk_to_hstring_m1(duk_hthread *thr) {
  18467. DUK_ASSERT_API_ENTRY(thr);
  18468. return duk_to_hstring(thr, -1);
  18469. }
  18470. DUK_INTERNAL duk_hstring *duk_to_hstring_acceptsymbol(duk_hthread *thr, duk_idx_t idx) {
  18471. duk_hstring *ret;
  18472. DUK_ASSERT_API_ENTRY(thr);
  18473. ret = duk_get_hstring(thr, idx);
  18474. if (DUK_UNLIKELY(ret && DUK_HSTRING_HAS_SYMBOL(ret))) {
  18475. return ret;
  18476. }
  18477. return duk_to_hstring(thr, idx);
  18478. }
  18479. /* Convert a plain buffer or any buffer object into a string, using the buffer
  18480. * bytes 1:1 in the internal string representation. For views the active byte
  18481. * slice (not element slice interpreted as an initializer) is used. This is
  18482. * necessary in Duktape 2.x because ToString(plainBuffer) no longer creates a
  18483. * string with the same bytes as in the buffer but rather (usually)
  18484. * '[object ArrayBuffer]'.
  18485. */
  18486. DUK_EXTERNAL const char *duk_buffer_to_string(duk_hthread *thr, duk_idx_t idx) {
  18487. void *ptr_src;
  18488. duk_size_t len;
  18489. const char *res;
  18490. DUK_ASSERT_API_ENTRY(thr);
  18491. idx = duk_require_normalize_index(thr, idx);
  18492. ptr_src = duk_require_buffer_data(thr, idx, &len);
  18493. DUK_ASSERT(ptr_src != NULL || len == 0);
  18494. res = duk_push_lstring(thr, (const char *) ptr_src, len);
  18495. duk_replace(thr, idx);
  18496. return res;
  18497. }
  18498. DUK_EXTERNAL void *duk_to_buffer_raw(duk_hthread *thr, duk_idx_t idx, duk_size_t *out_size, duk_uint_t mode) {
  18499. duk_hbuffer *h_buf;
  18500. const duk_uint8_t *src_data;
  18501. duk_size_t src_size;
  18502. duk_uint8_t *dst_data;
  18503. DUK_ASSERT_API_ENTRY(thr);
  18504. idx = duk_require_normalize_index(thr, idx);
  18505. h_buf = duk_get_hbuffer(thr, idx);
  18506. if (h_buf != NULL) {
  18507. /* Buffer is kept as is, with the fixed/dynamic nature of the
  18508. * buffer only changed if requested. An external buffer
  18509. * is converted into a non-external dynamic buffer in a
  18510. * duk_to_dynamic_buffer() call.
  18511. */
  18512. duk_uint_t tmp;
  18513. duk_uint8_t *tmp_ptr;
  18514. tmp_ptr = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf);
  18515. src_data = (const duk_uint8_t *) tmp_ptr;
  18516. src_size = DUK_HBUFFER_GET_SIZE(h_buf);
  18517. tmp = (DUK_HBUFFER_HAS_DYNAMIC(h_buf) ? DUK_BUF_MODE_DYNAMIC : DUK_BUF_MODE_FIXED);
  18518. if ((tmp == mode && !DUK_HBUFFER_HAS_EXTERNAL(h_buf)) ||
  18519. mode == DUK_BUF_MODE_DONTCARE) {
  18520. /* Note: src_data may be NULL if input is a zero-size
  18521. * dynamic buffer.
  18522. */
  18523. dst_data = tmp_ptr;
  18524. goto skip_copy;
  18525. }
  18526. } else {
  18527. /* Non-buffer value is first ToString() coerced, then converted
  18528. * to a buffer (fixed buffer is used unless a dynamic buffer is
  18529. * explicitly requested). Symbols are rejected with a TypeError.
  18530. * XXX: C API could maybe allow symbol-to-buffer coercion?
  18531. */
  18532. src_data = (const duk_uint8_t *) duk_to_lstring(thr, idx, &src_size);
  18533. }
  18534. dst_data = (duk_uint8_t *) duk_push_buffer(thr, src_size, (mode == DUK_BUF_MODE_DYNAMIC) /*dynamic*/);
  18535. /* dst_data may be NULL if size is zero. */
  18536. duk_memcpy_unsafe((void *) dst_data, (const void *) src_data, (size_t) src_size);
  18537. duk_replace(thr, idx);
  18538. skip_copy:
  18539. if (out_size) {
  18540. *out_size = src_size;
  18541. }
  18542. return dst_data;
  18543. }
  18544. DUK_EXTERNAL void *duk_to_pointer(duk_hthread *thr, duk_idx_t idx) {
  18545. duk_tval *tv;
  18546. void *res;
  18547. DUK_ASSERT_API_ENTRY(thr);
  18548. idx = duk_require_normalize_index(thr, idx);
  18549. tv = DUK_GET_TVAL_POSIDX(thr, idx);
  18550. DUK_ASSERT(tv != NULL);
  18551. switch (DUK_TVAL_GET_TAG(tv)) {
  18552. case DUK_TAG_UNDEFINED:
  18553. case DUK_TAG_NULL:
  18554. case DUK_TAG_BOOLEAN:
  18555. res = NULL;
  18556. break;
  18557. case DUK_TAG_POINTER:
  18558. res = DUK_TVAL_GET_POINTER(tv);
  18559. break;
  18560. case DUK_TAG_STRING:
  18561. case DUK_TAG_OBJECT:
  18562. case DUK_TAG_BUFFER:
  18563. /* Heap allocated: return heap pointer which is NOT useful
  18564. * for the caller, except for debugging.
  18565. */
  18566. res = (void *) DUK_TVAL_GET_HEAPHDR(tv);
  18567. break;
  18568. case DUK_TAG_LIGHTFUNC:
  18569. /* Function pointers do not always cast correctly to void *
  18570. * (depends on memory and segmentation model for instance),
  18571. * so they coerce to NULL.
  18572. */
  18573. res = NULL;
  18574. break;
  18575. #if defined(DUK_USE_FASTINT)
  18576. case DUK_TAG_FASTINT:
  18577. #endif
  18578. default:
  18579. /* number */
  18580. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18581. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18582. res = NULL;
  18583. break;
  18584. }
  18585. duk_push_pointer(thr, res);
  18586. duk_replace(thr, idx);
  18587. return res;
  18588. }
  18589. DUK_LOCAL void duk__push_func_from_lightfunc(duk_hthread *thr, duk_c_function func, duk_small_uint_t lf_flags) {
  18590. duk_idx_t nargs;
  18591. duk_uint_t flags = 0; /* shared flags for a subset of types */
  18592. duk_small_uint_t lf_len;
  18593. duk_hnatfunc *nf;
  18594. nargs = (duk_idx_t) DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
  18595. if (nargs == DUK_LFUNC_NARGS_VARARGS) {
  18596. nargs = (duk_idx_t) DUK_VARARGS;
  18597. }
  18598. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18599. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  18600. DUK_HOBJECT_FLAG_CALLABLE |
  18601. DUK_HOBJECT_FLAG_FASTREFS |
  18602. DUK_HOBJECT_FLAG_NATFUNC |
  18603. DUK_HOBJECT_FLAG_NEWENV |
  18604. DUK_HOBJECT_FLAG_STRICT |
  18605. DUK_HOBJECT_FLAG_NOTAIL |
  18606. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  18607. (void) duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE);
  18608. lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  18609. if ((duk_idx_t) lf_len != nargs) {
  18610. /* Explicit length is only needed if it differs from 'nargs'. */
  18611. duk_push_int(thr, (duk_int_t) lf_len);
  18612. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_NONE);
  18613. }
  18614. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  18615. duk_push_lightfunc_name_raw(thr, func, lf_flags);
  18616. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C);
  18617. #endif
  18618. nf = duk_known_hnatfunc(thr, -1);
  18619. nf->magic = (duk_int16_t) DUK_LFUNC_FLAGS_GET_MAGIC(lf_flags);
  18620. }
  18621. DUK_EXTERNAL void duk_to_object(duk_hthread *thr, duk_idx_t idx) {
  18622. duk_tval *tv;
  18623. duk_uint_t flags = 0; /* shared flags for a subset of types */
  18624. duk_small_int_t proto = 0;
  18625. DUK_ASSERT_API_ENTRY(thr);
  18626. idx = duk_require_normalize_index(thr, idx);
  18627. tv = DUK_GET_TVAL_POSIDX(thr, idx);
  18628. DUK_ASSERT(tv != NULL);
  18629. switch (DUK_TVAL_GET_TAG(tv)) {
  18630. #if !defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  18631. case DUK_TAG_BUFFER: /* With no bufferobject support, don't object coerce. */
  18632. #endif
  18633. case DUK_TAG_UNDEFINED:
  18634. case DUK_TAG_NULL: {
  18635. DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE);
  18636. DUK_WO_NORETURN(return;);
  18637. break;
  18638. }
  18639. case DUK_TAG_BOOLEAN: {
  18640. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18641. DUK_HOBJECT_FLAG_FASTREFS |
  18642. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_BOOLEAN);
  18643. proto = DUK_BIDX_BOOLEAN_PROTOTYPE;
  18644. goto create_object;
  18645. }
  18646. case DUK_TAG_STRING: {
  18647. duk_hstring *h;
  18648. h = DUK_TVAL_GET_STRING(tv);
  18649. DUK_ASSERT(h != NULL);
  18650. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  18651. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18652. DUK_HOBJECT_FLAG_FASTREFS |
  18653. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_SYMBOL);
  18654. proto = DUK_BIDX_SYMBOL_PROTOTYPE;
  18655. } else {
  18656. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18657. DUK_HOBJECT_FLAG_FASTREFS |
  18658. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
  18659. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
  18660. proto = DUK_BIDX_STRING_PROTOTYPE;
  18661. }
  18662. goto create_object;
  18663. }
  18664. case DUK_TAG_OBJECT: {
  18665. /* nop */
  18666. break;
  18667. }
  18668. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  18669. case DUK_TAG_BUFFER: {
  18670. /* A plain buffer object coerces to a full ArrayBuffer which
  18671. * is not fully transparent behavior (ToObject() should be a
  18672. * nop for an object). This behavior matches lightfuncs which
  18673. * also coerce to an equivalent Function object. There are
  18674. * also downsides to defining ToObject(plainBuffer) as a no-op;
  18675. * for example duk_to_hobject() could result in a NULL pointer.
  18676. */
  18677. duk_hbuffer *h_buf;
  18678. h_buf = DUK_TVAL_GET_BUFFER(tv);
  18679. DUK_ASSERT(h_buf != NULL);
  18680. duk_hbufobj_push_uint8array_from_plain(thr, h_buf);
  18681. goto replace_value;
  18682. }
  18683. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  18684. case DUK_TAG_POINTER: {
  18685. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18686. DUK_HOBJECT_FLAG_FASTREFS |
  18687. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER);
  18688. proto = DUK_BIDX_POINTER_PROTOTYPE;
  18689. goto create_object;
  18690. }
  18691. case DUK_TAG_LIGHTFUNC: {
  18692. /* Lightfunc coerces to a Function instance with concrete
  18693. * properties. Since 'length' is virtual for Duktape/C
  18694. * functions, don't need to define that. The result is made
  18695. * extensible to mimic what happens to strings in object
  18696. * coercion:
  18697. *
  18698. * > Object.isExtensible(Object('foo'))
  18699. * true
  18700. */
  18701. duk_small_uint_t lf_flags;
  18702. duk_c_function func;
  18703. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  18704. duk__push_func_from_lightfunc(thr, func, lf_flags);
  18705. goto replace_value;
  18706. }
  18707. #if defined(DUK_USE_FASTINT)
  18708. case DUK_TAG_FASTINT:
  18709. #endif
  18710. default: {
  18711. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18712. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18713. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  18714. DUK_HOBJECT_FLAG_FASTREFS |
  18715. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_NUMBER);
  18716. proto = DUK_BIDX_NUMBER_PROTOTYPE;
  18717. goto create_object;
  18718. }
  18719. }
  18720. DUK_ASSERT(duk_is_object(thr, idx));
  18721. return;
  18722. create_object:
  18723. (void) duk_push_object_helper(thr, flags, proto);
  18724. /* Note: Boolean prototype's internal value property is not writable,
  18725. * but duk_xdef_prop_stridx() disregards the write protection. Boolean
  18726. * instances are immutable.
  18727. *
  18728. * String and buffer special behaviors are already enabled which is not
  18729. * ideal, but a write to the internal value is not affected by them.
  18730. */
  18731. duk_dup(thr, idx);
  18732. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  18733. replace_value:
  18734. duk_replace(thr, idx);
  18735. DUK_ASSERT(duk_is_object(thr, idx));
  18736. }
  18737. DUK_INTERNAL duk_hobject *duk_to_hobject(duk_hthread *thr, duk_idx_t idx) {
  18738. duk_hobject *ret;
  18739. DUK_ASSERT_API_ENTRY(thr);
  18740. duk_to_object(thr, idx);
  18741. ret = duk_known_hobject(thr, idx);
  18742. return ret;
  18743. }
  18744. /*
  18745. * Type checking
  18746. */
  18747. DUK_LOCAL duk_bool_t duk__tag_check(duk_hthread *thr, duk_idx_t idx, duk_small_uint_t tag) {
  18748. duk_tval *tv;
  18749. tv = duk_get_tval_or_unused(thr, idx);
  18750. DUK_ASSERT(tv != NULL);
  18751. return (DUK_TVAL_GET_TAG(tv) == tag);
  18752. }
  18753. DUK_LOCAL duk_bool_t duk__obj_flag_any_default_false(duk_hthread *thr, duk_idx_t idx, duk_uint_t flag_mask) {
  18754. duk_hobject *obj;
  18755. DUK_ASSERT_API_ENTRY(thr);
  18756. obj = duk_get_hobject(thr, idx);
  18757. if (obj) {
  18758. return (DUK_HEAPHDR_CHECK_FLAG_BITS((duk_heaphdr *) obj, flag_mask) ? 1 : 0);
  18759. }
  18760. return 0;
  18761. }
  18762. DUK_INTERNAL duk_int_t duk_get_type_tval(duk_tval *tv) {
  18763. DUK_ASSERT(tv != NULL);
  18764. #if defined(DUK_USE_PACKED_TVAL)
  18765. switch (DUK_TVAL_GET_TAG(tv)) {
  18766. case DUK_TAG_UNUSED:
  18767. return DUK_TYPE_NONE;
  18768. case DUK_TAG_UNDEFINED:
  18769. return DUK_TYPE_UNDEFINED;
  18770. case DUK_TAG_NULL:
  18771. return DUK_TYPE_NULL;
  18772. case DUK_TAG_BOOLEAN:
  18773. return DUK_TYPE_BOOLEAN;
  18774. case DUK_TAG_STRING:
  18775. return DUK_TYPE_STRING;
  18776. case DUK_TAG_OBJECT:
  18777. return DUK_TYPE_OBJECT;
  18778. case DUK_TAG_BUFFER:
  18779. return DUK_TYPE_BUFFER;
  18780. case DUK_TAG_POINTER:
  18781. return DUK_TYPE_POINTER;
  18782. case DUK_TAG_LIGHTFUNC:
  18783. return DUK_TYPE_LIGHTFUNC;
  18784. #if defined(DUK_USE_FASTINT)
  18785. case DUK_TAG_FASTINT:
  18786. #endif
  18787. default:
  18788. /* Note: number has no explicit tag (in 8-byte representation) */
  18789. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18790. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18791. return DUK_TYPE_NUMBER;
  18792. }
  18793. #else /* DUK_USE_PACKED_TVAL */
  18794. DUK_ASSERT(DUK_TVAL_IS_VALID_TAG(tv));
  18795. DUK_ASSERT(sizeof(duk__type_from_tag) / sizeof(duk_uint_t) == DUK_TAG_MAX - DUK_TAG_MIN + 1);
  18796. return (duk_int_t) duk__type_from_tag[DUK_TVAL_GET_TAG(tv) - DUK_TAG_MIN];
  18797. #endif /* DUK_USE_PACKED_TVAL */
  18798. }
  18799. DUK_EXTERNAL duk_int_t duk_get_type(duk_hthread *thr, duk_idx_t idx) {
  18800. duk_tval *tv;
  18801. DUK_ASSERT_API_ENTRY(thr);
  18802. tv = duk_get_tval_or_unused(thr, idx);
  18803. DUK_ASSERT(tv != NULL);
  18804. return duk_get_type_tval(tv);
  18805. }
  18806. #if defined(DUK_USE_VERBOSE_ERRORS) && defined(DUK_USE_PARANOID_ERRORS)
  18807. DUK_LOCAL const char * const duk__type_names[] = {
  18808. "none",
  18809. "undefined",
  18810. "null",
  18811. "boolean",
  18812. "number",
  18813. "string",
  18814. "object",
  18815. "buffer",
  18816. "pointer",
  18817. "lightfunc"
  18818. };
  18819. DUK_INTERNAL const char *duk_get_type_name(duk_hthread *thr, duk_idx_t idx) {
  18820. duk_int_t type_tag;
  18821. DUK_ASSERT_API_ENTRY(thr);
  18822. type_tag = duk_get_type(thr, idx);
  18823. DUK_ASSERT(type_tag >= DUK_TYPE_MIN && type_tag <= DUK_TYPE_MAX);
  18824. DUK_ASSERT(DUK_TYPE_MIN == 0 && sizeof(duk__type_names) / sizeof(const char *) == DUK_TYPE_MAX + 1);
  18825. return duk__type_names[type_tag];
  18826. }
  18827. #endif /* DUK_USE_VERBOSE_ERRORS && DUK_USE_PARANOID_ERRORS */
  18828. DUK_INTERNAL duk_small_uint_t duk_get_class_number(duk_hthread *thr, duk_idx_t idx) {
  18829. duk_tval *tv;
  18830. duk_hobject *obj;
  18831. DUK_ASSERT_API_ENTRY(thr);
  18832. tv = duk_get_tval_or_unused(thr, idx);
  18833. DUK_ASSERT(tv != NULL);
  18834. switch (DUK_TVAL_GET_TAG(tv)) {
  18835. case DUK_TAG_OBJECT:
  18836. obj = DUK_TVAL_GET_OBJECT(tv);
  18837. DUK_ASSERT(obj != NULL);
  18838. return DUK_HOBJECT_GET_CLASS_NUMBER(obj);
  18839. case DUK_TAG_BUFFER:
  18840. /* Buffers behave like Uint8Array objects. */
  18841. return DUK_HOBJECT_CLASS_UINT8ARRAY;
  18842. case DUK_TAG_LIGHTFUNC:
  18843. /* Lightfuncs behave like Function objects. */
  18844. return DUK_HOBJECT_CLASS_FUNCTION;
  18845. default:
  18846. /* Primitive or UNUSED, no class number. */
  18847. return DUK_HOBJECT_CLASS_NONE;
  18848. }
  18849. }
  18850. DUK_EXTERNAL duk_bool_t duk_check_type(duk_hthread *thr, duk_idx_t idx, duk_int_t type) {
  18851. DUK_ASSERT_API_ENTRY(thr);
  18852. return (duk_get_type(thr, idx) == type) ? 1 : 0;
  18853. }
  18854. DUK_INTERNAL duk_uint_t duk_get_type_mask_tval(duk_tval *tv) {
  18855. DUK_ASSERT(tv != NULL);
  18856. #if defined(DUK_USE_PACKED_TVAL)
  18857. switch (DUK_TVAL_GET_TAG(tv)) {
  18858. case DUK_TAG_UNUSED:
  18859. return DUK_TYPE_MASK_NONE;
  18860. case DUK_TAG_UNDEFINED:
  18861. return DUK_TYPE_MASK_UNDEFINED;
  18862. case DUK_TAG_NULL:
  18863. return DUK_TYPE_MASK_NULL;
  18864. case DUK_TAG_BOOLEAN:
  18865. return DUK_TYPE_MASK_BOOLEAN;
  18866. case DUK_TAG_STRING:
  18867. return DUK_TYPE_MASK_STRING;
  18868. case DUK_TAG_OBJECT:
  18869. return DUK_TYPE_MASK_OBJECT;
  18870. case DUK_TAG_BUFFER:
  18871. return DUK_TYPE_MASK_BUFFER;
  18872. case DUK_TAG_POINTER:
  18873. return DUK_TYPE_MASK_POINTER;
  18874. case DUK_TAG_LIGHTFUNC:
  18875. return DUK_TYPE_MASK_LIGHTFUNC;
  18876. #if defined(DUK_USE_FASTINT)
  18877. case DUK_TAG_FASTINT:
  18878. #endif
  18879. default:
  18880. /* Note: number has no explicit tag (in 8-byte representation) */
  18881. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  18882. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  18883. return DUK_TYPE_MASK_NUMBER;
  18884. }
  18885. #else /* DUK_USE_PACKED_TVAL */
  18886. DUK_ASSERT(DUK_TVAL_IS_VALID_TAG(tv));
  18887. DUK_ASSERT(sizeof(duk__type_mask_from_tag) / sizeof(duk_uint_t) == DUK_TAG_MAX - DUK_TAG_MIN + 1);
  18888. return duk__type_mask_from_tag[DUK_TVAL_GET_TAG(tv) - DUK_TAG_MIN];
  18889. #endif /* DUK_USE_PACKED_TVAL */
  18890. }
  18891. DUK_EXTERNAL duk_uint_t duk_get_type_mask(duk_hthread *thr, duk_idx_t idx) {
  18892. duk_tval *tv;
  18893. DUK_ASSERT_API_ENTRY(thr);
  18894. tv = duk_get_tval_or_unused(thr, idx);
  18895. DUK_ASSERT(tv != NULL);
  18896. return duk_get_type_mask_tval(tv);
  18897. }
  18898. DUK_EXTERNAL duk_bool_t duk_check_type_mask(duk_hthread *thr, duk_idx_t idx, duk_uint_t mask) {
  18899. DUK_ASSERT_API_ENTRY(thr);
  18900. if (DUK_LIKELY((duk_get_type_mask(thr, idx) & mask) != 0U)) {
  18901. return 1;
  18902. }
  18903. if (mask & DUK_TYPE_MASK_THROW) {
  18904. DUK_ERROR_TYPE(thr, DUK_STR_UNEXPECTED_TYPE);
  18905. DUK_WO_NORETURN(return 0;);
  18906. }
  18907. return 0;
  18908. }
  18909. DUK_EXTERNAL duk_bool_t duk_is_undefined(duk_hthread *thr, duk_idx_t idx) {
  18910. DUK_ASSERT_API_ENTRY(thr);
  18911. return duk__tag_check(thr, idx, DUK_TAG_UNDEFINED);
  18912. }
  18913. DUK_EXTERNAL duk_bool_t duk_is_null(duk_hthread *thr, duk_idx_t idx) {
  18914. DUK_ASSERT_API_ENTRY(thr);
  18915. return duk__tag_check(thr, idx, DUK_TAG_NULL);
  18916. }
  18917. DUK_EXTERNAL duk_bool_t duk_is_boolean(duk_hthread *thr, duk_idx_t idx) {
  18918. DUK_ASSERT_API_ENTRY(thr);
  18919. return duk__tag_check(thr, idx, DUK_TAG_BOOLEAN);
  18920. }
  18921. DUK_EXTERNAL duk_bool_t duk_is_number(duk_hthread *thr, duk_idx_t idx) {
  18922. duk_tval *tv;
  18923. DUK_ASSERT_API_ENTRY(thr);
  18924. /*
  18925. * Number is special because it doesn't have a specific
  18926. * tag in the 8-byte representation.
  18927. */
  18928. /* XXX: shorter version for unpacked representation? */
  18929. tv = duk_get_tval_or_unused(thr, idx);
  18930. DUK_ASSERT(tv != NULL);
  18931. return DUK_TVAL_IS_NUMBER(tv);
  18932. }
  18933. DUK_EXTERNAL duk_bool_t duk_is_nan(duk_hthread *thr, duk_idx_t idx) {
  18934. /* XXX: This will now return false for non-numbers, even though they would
  18935. * coerce to NaN (as a general rule). In particular, duk_get_number()
  18936. * returns a NaN for non-numbers, so should this function also return
  18937. * true for non-numbers?
  18938. */
  18939. duk_tval *tv;
  18940. DUK_ASSERT_API_ENTRY(thr);
  18941. tv = duk_get_tval_or_unused(thr, idx);
  18942. DUK_ASSERT(tv != NULL);
  18943. /* XXX: for packed duk_tval an explicit "is number" check is unnecessary */
  18944. if (!DUK_TVAL_IS_NUMBER(tv)) {
  18945. return 0;
  18946. }
  18947. return (duk_bool_t) DUK_ISNAN(DUK_TVAL_GET_NUMBER(tv));
  18948. }
  18949. DUK_EXTERNAL duk_bool_t duk_is_string(duk_hthread *thr, duk_idx_t idx) {
  18950. DUK_ASSERT_API_ENTRY(thr);
  18951. return duk__tag_check(thr, idx, DUK_TAG_STRING);
  18952. }
  18953. DUK_INTERNAL duk_bool_t duk_is_string_notsymbol(duk_hthread *thr, duk_idx_t idx) {
  18954. DUK_ASSERT_API_ENTRY(thr);
  18955. return duk_get_hstring_notsymbol(thr, idx) != NULL;
  18956. }
  18957. DUK_EXTERNAL duk_bool_t duk_is_object(duk_hthread *thr, duk_idx_t idx) {
  18958. DUK_ASSERT_API_ENTRY(thr);
  18959. return duk__tag_check(thr, idx, DUK_TAG_OBJECT);
  18960. }
  18961. DUK_EXTERNAL duk_bool_t duk_is_buffer(duk_hthread *thr, duk_idx_t idx) {
  18962. DUK_ASSERT_API_ENTRY(thr);
  18963. return duk__tag_check(thr, idx, DUK_TAG_BUFFER);
  18964. }
  18965. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  18966. DUK_EXTERNAL duk_bool_t duk_is_buffer_data(duk_hthread *thr, duk_idx_t idx) {
  18967. duk_tval *tv;
  18968. DUK_ASSERT_API_ENTRY(thr);
  18969. tv = duk_get_tval_or_unused(thr, idx);
  18970. DUK_ASSERT(tv != NULL);
  18971. if (DUK_TVAL_IS_BUFFER(tv)) {
  18972. return 1;
  18973. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  18974. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  18975. DUK_ASSERT(h != NULL);
  18976. if (DUK_HOBJECT_IS_BUFOBJ(h)) {
  18977. return 1;
  18978. }
  18979. }
  18980. return 0;
  18981. }
  18982. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  18983. DUK_EXTERNAL duk_bool_t duk_is_buffer_data(duk_hthread *thr, duk_idx_t idx) {
  18984. DUK_ASSERT_API_ENTRY(thr);
  18985. return duk_is_buffer(thr, idx);
  18986. }
  18987. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  18988. DUK_EXTERNAL duk_bool_t duk_is_pointer(duk_hthread *thr, duk_idx_t idx) {
  18989. DUK_ASSERT_API_ENTRY(thr);
  18990. return duk__tag_check(thr, idx, DUK_TAG_POINTER);
  18991. }
  18992. DUK_EXTERNAL duk_bool_t duk_is_lightfunc(duk_hthread *thr, duk_idx_t idx) {
  18993. DUK_ASSERT_API_ENTRY(thr);
  18994. return duk__tag_check(thr, idx, DUK_TAG_LIGHTFUNC);
  18995. }
  18996. DUK_EXTERNAL duk_bool_t duk_is_symbol(duk_hthread *thr, duk_idx_t idx) {
  18997. duk_hstring *h;
  18998. DUK_ASSERT_API_ENTRY(thr);
  18999. h = duk_get_hstring(thr, idx);
  19000. /* Use DUK_LIKELY() here because caller may be more likely to type
  19001. * check an expected symbol than not.
  19002. */
  19003. if (DUK_LIKELY(h != NULL && DUK_HSTRING_HAS_SYMBOL(h))) {
  19004. return 1;
  19005. }
  19006. return 0;
  19007. }
  19008. DUK_EXTERNAL duk_bool_t duk_is_array(duk_hthread *thr, duk_idx_t idx) {
  19009. duk_hobject *obj;
  19010. DUK_ASSERT_API_ENTRY(thr);
  19011. obj = duk_get_hobject(thr, idx);
  19012. if (obj) {
  19013. return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_ARRAY ? 1 : 0);
  19014. }
  19015. return 0;
  19016. }
  19017. DUK_EXTERNAL duk_bool_t duk_is_function(duk_hthread *thr, duk_idx_t idx) {
  19018. duk_tval *tv;
  19019. DUK_ASSERT_API_ENTRY(thr);
  19020. tv = duk_get_tval_or_unused(thr, idx);
  19021. if (DUK_TVAL_IS_OBJECT(tv)) {
  19022. duk_hobject *h;
  19023. h = DUK_TVAL_GET_OBJECT(tv);
  19024. DUK_ASSERT(h != NULL);
  19025. return DUK_HOBJECT_HAS_CALLABLE(h) ? 1 : 0;
  19026. }
  19027. if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  19028. return 1;
  19029. }
  19030. return 0;
  19031. }
  19032. DUK_INTERNAL duk_bool_t duk_is_callable_tval(duk_hthread *thr, duk_tval *tv) {
  19033. DUK_ASSERT_API_ENTRY(thr);
  19034. DUK_UNREF(thr);
  19035. if (DUK_TVAL_IS_OBJECT(tv)) {
  19036. duk_hobject *h;
  19037. h = DUK_TVAL_GET_OBJECT(tv);
  19038. DUK_ASSERT(h != NULL);
  19039. return DUK_HOBJECT_HAS_CALLABLE(h) ? 1 : 0;
  19040. }
  19041. if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  19042. return 1;
  19043. }
  19044. return 0;
  19045. }
  19046. DUK_EXTERNAL duk_bool_t duk_is_constructable(duk_hthread *thr, duk_idx_t idx) {
  19047. duk_tval *tv;
  19048. DUK_ASSERT_API_ENTRY(thr);
  19049. tv = duk_get_tval_or_unused(thr, idx);
  19050. if (DUK_TVAL_IS_OBJECT(tv)) {
  19051. duk_hobject *h;
  19052. h = DUK_TVAL_GET_OBJECT(tv);
  19053. DUK_ASSERT(h != NULL);
  19054. return DUK_HOBJECT_HAS_CONSTRUCTABLE(h) ? 1 : 0;
  19055. }
  19056. if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  19057. return 1;
  19058. }
  19059. return 0;
  19060. }
  19061. DUK_EXTERNAL duk_bool_t duk_is_c_function(duk_hthread *thr, duk_idx_t idx) {
  19062. DUK_ASSERT_API_ENTRY(thr);
  19063. return duk__obj_flag_any_default_false(thr,
  19064. idx,
  19065. DUK_HOBJECT_FLAG_NATFUNC);
  19066. }
  19067. DUK_EXTERNAL duk_bool_t duk_is_ecmascript_function(duk_hthread *thr, duk_idx_t idx) {
  19068. DUK_ASSERT_API_ENTRY(thr);
  19069. return duk__obj_flag_any_default_false(thr,
  19070. idx,
  19071. DUK_HOBJECT_FLAG_COMPFUNC);
  19072. }
  19073. DUK_EXTERNAL duk_bool_t duk_is_bound_function(duk_hthread *thr, duk_idx_t idx) {
  19074. DUK_ASSERT_API_ENTRY(thr);
  19075. return duk__obj_flag_any_default_false(thr,
  19076. idx,
  19077. DUK_HOBJECT_FLAG_BOUNDFUNC);
  19078. }
  19079. DUK_EXTERNAL duk_bool_t duk_is_thread(duk_hthread *thr, duk_idx_t idx) {
  19080. duk_hobject *obj;
  19081. DUK_ASSERT_API_ENTRY(thr);
  19082. obj = duk_get_hobject(thr, idx);
  19083. if (obj) {
  19084. return (DUK_HOBJECT_GET_CLASS_NUMBER(obj) == DUK_HOBJECT_CLASS_THREAD ? 1 : 0);
  19085. }
  19086. return 0;
  19087. }
  19088. DUK_EXTERNAL duk_bool_t duk_is_fixed_buffer(duk_hthread *thr, duk_idx_t idx) {
  19089. duk_tval *tv;
  19090. DUK_ASSERT_API_ENTRY(thr);
  19091. tv = duk_get_tval_or_unused(thr, idx);
  19092. DUK_ASSERT(tv != NULL);
  19093. if (DUK_TVAL_IS_BUFFER(tv)) {
  19094. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  19095. DUK_ASSERT(h != NULL);
  19096. return (DUK_HBUFFER_HAS_DYNAMIC(h) ? 0 : 1);
  19097. }
  19098. return 0;
  19099. }
  19100. DUK_EXTERNAL duk_bool_t duk_is_dynamic_buffer(duk_hthread *thr, duk_idx_t idx) {
  19101. duk_tval *tv;
  19102. DUK_ASSERT_API_ENTRY(thr);
  19103. tv = duk_get_tval_or_unused(thr, idx);
  19104. DUK_ASSERT(tv != NULL);
  19105. if (DUK_TVAL_IS_BUFFER(tv)) {
  19106. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  19107. DUK_ASSERT(h != NULL);
  19108. return (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
  19109. }
  19110. return 0;
  19111. }
  19112. DUK_EXTERNAL duk_bool_t duk_is_external_buffer(duk_hthread *thr, duk_idx_t idx) {
  19113. duk_tval *tv;
  19114. DUK_ASSERT_API_ENTRY(thr);
  19115. tv = duk_get_tval_or_unused(thr, idx);
  19116. DUK_ASSERT(tv != NULL);
  19117. if (DUK_TVAL_IS_BUFFER(tv)) {
  19118. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  19119. DUK_ASSERT(h != NULL);
  19120. return (DUK_HBUFFER_HAS_DYNAMIC(h) && DUK_HBUFFER_HAS_EXTERNAL(h) ? 1 : 0);
  19121. }
  19122. return 0;
  19123. }
  19124. DUK_EXTERNAL duk_errcode_t duk_get_error_code(duk_hthread *thr, duk_idx_t idx) {
  19125. duk_hobject *h;
  19126. duk_uint_t sanity;
  19127. DUK_ASSERT_API_ENTRY(thr);
  19128. h = duk_get_hobject(thr, idx);
  19129. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  19130. do {
  19131. if (!h) {
  19132. return DUK_ERR_NONE;
  19133. }
  19134. /* XXX: something more convenient? */
  19135. if (h == thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE]) {
  19136. return DUK_ERR_EVAL_ERROR;
  19137. }
  19138. if (h == thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE]) {
  19139. return DUK_ERR_RANGE_ERROR;
  19140. }
  19141. if (h == thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE]) {
  19142. return DUK_ERR_REFERENCE_ERROR;
  19143. }
  19144. if (h == thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE]) {
  19145. return DUK_ERR_SYNTAX_ERROR;
  19146. }
  19147. if (h == thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE]) {
  19148. return DUK_ERR_TYPE_ERROR;
  19149. }
  19150. if (h == thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE]) {
  19151. return DUK_ERR_URI_ERROR;
  19152. }
  19153. if (h == thr->builtins[DUK_BIDX_ERROR_PROTOTYPE]) {
  19154. return DUK_ERR_ERROR;
  19155. }
  19156. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  19157. } while (--sanity > 0);
  19158. return DUK_ERR_NONE;
  19159. }
  19160. /*
  19161. * Pushers
  19162. */
  19163. DUK_INTERNAL void duk_push_tval(duk_hthread *thr, duk_tval *tv) {
  19164. duk_tval *tv_slot;
  19165. DUK_ASSERT_API_ENTRY(thr);
  19166. DUK_ASSERT(tv != NULL);
  19167. DUK__CHECK_SPACE();
  19168. tv_slot = thr->valstack_top++;
  19169. DUK_TVAL_SET_TVAL(tv_slot, tv);
  19170. DUK_TVAL_INCREF(thr, tv); /* no side effects */
  19171. }
  19172. DUK_EXTERNAL void duk_push_undefined(duk_hthread *thr) {
  19173. DUK_ASSERT_API_ENTRY(thr);
  19174. DUK__CHECK_SPACE();
  19175. /* Because value stack init policy is 'undefined above top',
  19176. * we don't need to write, just assert.
  19177. */
  19178. thr->valstack_top++;
  19179. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
  19180. }
  19181. DUK_EXTERNAL void duk_push_null(duk_hthread *thr) {
  19182. duk_tval *tv_slot;
  19183. DUK_ASSERT_API_ENTRY(thr);
  19184. DUK__CHECK_SPACE();
  19185. tv_slot = thr->valstack_top++;
  19186. DUK_TVAL_SET_NULL(tv_slot);
  19187. }
  19188. DUK_EXTERNAL void duk_push_boolean(duk_hthread *thr, duk_bool_t val) {
  19189. duk_tval *tv_slot;
  19190. duk_small_int_t b;
  19191. DUK_ASSERT_API_ENTRY(thr);
  19192. DUK__CHECK_SPACE();
  19193. b = (val ? 1 : 0); /* ensure value is 1 or 0 (not other non-zero) */
  19194. tv_slot = thr->valstack_top++;
  19195. DUK_TVAL_SET_BOOLEAN(tv_slot, b);
  19196. }
  19197. DUK_EXTERNAL void duk_push_true(duk_hthread *thr) {
  19198. duk_tval *tv_slot;
  19199. DUK_ASSERT_API_ENTRY(thr);
  19200. DUK__CHECK_SPACE();
  19201. tv_slot = thr->valstack_top++;
  19202. DUK_TVAL_SET_BOOLEAN_TRUE(tv_slot);
  19203. }
  19204. DUK_EXTERNAL void duk_push_false(duk_hthread *thr) {
  19205. duk_tval *tv_slot;
  19206. DUK_ASSERT_API_ENTRY(thr);
  19207. DUK__CHECK_SPACE();
  19208. tv_slot = thr->valstack_top++;
  19209. DUK_TVAL_SET_BOOLEAN_FALSE(tv_slot);
  19210. }
  19211. /* normalize NaN which may not match our canonical internal NaN */
  19212. DUK_EXTERNAL void duk_push_number(duk_hthread *thr, duk_double_t val) {
  19213. duk_tval *tv_slot;
  19214. duk_double_union du;
  19215. DUK_ASSERT_API_ENTRY(thr);
  19216. DUK__CHECK_SPACE();
  19217. du.d = val;
  19218. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  19219. tv_slot = thr->valstack_top++;
  19220. DUK_TVAL_SET_NUMBER(tv_slot, du.d);
  19221. }
  19222. DUK_EXTERNAL void duk_push_int(duk_hthread *thr, duk_int_t val) {
  19223. #if defined(DUK_USE_FASTINT)
  19224. duk_tval *tv_slot;
  19225. DUK_ASSERT_API_ENTRY(thr);
  19226. DUK__CHECK_SPACE();
  19227. tv_slot = thr->valstack_top++;
  19228. #if DUK_INT_MAX <= 0x7fffffffL
  19229. DUK_TVAL_SET_I32(tv_slot, (duk_int32_t) val);
  19230. #else
  19231. if (val >= DUK_FASTINT_MIN && val <= DUK_FASTINT_MAX) {
  19232. DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
  19233. } else {
  19234. duk_double_t = (duk_double_t) val;
  19235. DUK_TVAL_SET_NUMBER(tv_slot, d);
  19236. }
  19237. #endif
  19238. #else /* DUK_USE_FASTINT */
  19239. duk_tval *tv_slot;
  19240. duk_double_t d;
  19241. DUK_ASSERT_API_ENTRY(thr);
  19242. DUK__CHECK_SPACE();
  19243. d = (duk_double_t) val;
  19244. tv_slot = thr->valstack_top++;
  19245. DUK_TVAL_SET_NUMBER(tv_slot, d);
  19246. #endif /* DUK_USE_FASTINT */
  19247. }
  19248. DUK_EXTERNAL void duk_push_uint(duk_hthread *thr, duk_uint_t val) {
  19249. #if defined(DUK_USE_FASTINT)
  19250. duk_tval *tv_slot;
  19251. DUK_ASSERT_API_ENTRY(thr);
  19252. DUK__CHECK_SPACE();
  19253. tv_slot = thr->valstack_top++;
  19254. #if DUK_UINT_MAX <= 0xffffffffUL
  19255. DUK_TVAL_SET_U32(tv_slot, (duk_uint32_t) val);
  19256. #else
  19257. if (val <= DUK_FASTINT_MAX) { /* val is unsigned so >= 0 */
  19258. /* XXX: take advantage of val being unsigned, no need to mask */
  19259. DUK_TVAL_SET_FASTINT(tv_slot, (duk_int64_t) val);
  19260. } else {
  19261. duk_double_t = (duk_double_t) val;
  19262. DUK_TVAL_SET_NUMBER(tv_slot, d);
  19263. }
  19264. #endif
  19265. #else /* DUK_USE_FASTINT */
  19266. duk_tval *tv_slot;
  19267. duk_double_t d;
  19268. DUK_ASSERT_API_ENTRY(thr);
  19269. DUK__CHECK_SPACE();
  19270. d = (duk_double_t) val;
  19271. tv_slot = thr->valstack_top++;
  19272. DUK_TVAL_SET_NUMBER(tv_slot, d);
  19273. #endif /* DUK_USE_FASTINT */
  19274. }
  19275. DUK_EXTERNAL void duk_push_nan(duk_hthread *thr) {
  19276. duk_tval *tv_slot;
  19277. duk_double_union du;
  19278. DUK_ASSERT_API_ENTRY(thr);
  19279. DUK__CHECK_SPACE();
  19280. DUK_DBLUNION_SET_NAN(&du);
  19281. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  19282. tv_slot = thr->valstack_top++;
  19283. DUK_TVAL_SET_NUMBER(tv_slot, du.d);
  19284. }
  19285. DUK_EXTERNAL const char *duk_push_lstring(duk_hthread *thr, const char *str, duk_size_t len) {
  19286. duk_hstring *h;
  19287. duk_tval *tv_slot;
  19288. DUK_ASSERT_API_ENTRY(thr);
  19289. /* Check stack before interning (avoid hanging temp). */
  19290. DUK__CHECK_SPACE();
  19291. /* NULL with zero length represents an empty string; NULL with higher
  19292. * length is also now treated like an empty string although it is
  19293. * a bit dubious. This is unlike duk_push_string() which pushes a
  19294. * 'null' if the input string is a NULL.
  19295. */
  19296. if (DUK_UNLIKELY(str == NULL)) {
  19297. len = 0U;
  19298. }
  19299. /* Check for maximum string length. */
  19300. if (DUK_UNLIKELY(len > DUK_HSTRING_MAX_BYTELEN)) {
  19301. DUK_ERROR_RANGE(thr, DUK_STR_STRING_TOO_LONG);
  19302. DUK_WO_NORETURN(return NULL;);
  19303. }
  19304. h = duk_heap_strtable_intern_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len);
  19305. DUK_ASSERT(h != NULL);
  19306. tv_slot = thr->valstack_top++;
  19307. DUK_TVAL_SET_STRING(tv_slot, h);
  19308. DUK_HSTRING_INCREF(thr, h); /* no side effects */
  19309. return (const char *) DUK_HSTRING_GET_DATA(h);
  19310. }
  19311. DUK_EXTERNAL const char *duk_push_string(duk_hthread *thr, const char *str) {
  19312. DUK_ASSERT_API_ENTRY(thr);
  19313. if (str) {
  19314. return duk_push_lstring(thr, str, DUK_STRLEN(str));
  19315. } else {
  19316. duk_push_null(thr);
  19317. return NULL;
  19318. }
  19319. }
  19320. #if !defined(DUK_USE_PREFER_SIZE)
  19321. #if defined(DUK_USE_LITCACHE_SIZE)
  19322. DUK_EXTERNAL const char *duk_push_literal_raw(duk_hthread *thr, const char *str, duk_size_t len) {
  19323. duk_hstring *h;
  19324. duk_tval *tv_slot;
  19325. DUK_ASSERT_API_ENTRY(thr);
  19326. DUK_ASSERT(str != NULL);
  19327. DUK_ASSERT(str[len] == (char) 0);
  19328. /* Check for maximum string length. */
  19329. if (DUK_UNLIKELY(len > DUK_HSTRING_MAX_BYTELEN)) {
  19330. DUK_ERROR_RANGE(thr, DUK_STR_STRING_TOO_LONG);
  19331. DUK_WO_NORETURN(return NULL;);
  19332. }
  19333. h = duk_heap_strtable_intern_literal_checked(thr, (const duk_uint8_t *) str, (duk_uint32_t) len);
  19334. DUK_ASSERT(h != NULL);
  19335. tv_slot = thr->valstack_top++;
  19336. DUK_TVAL_SET_STRING(tv_slot, h);
  19337. DUK_HSTRING_INCREF(thr, h); /* no side effects */
  19338. return (const char *) DUK_HSTRING_GET_DATA(h);
  19339. }
  19340. #else /* DUK_USE_LITCACHE_SIZE */
  19341. DUK_EXTERNAL const char *duk_push_literal_raw(duk_hthread *thr, const char *str, duk_size_t len) {
  19342. DUK_ASSERT_API_ENTRY(thr);
  19343. DUK_ASSERT(str != NULL);
  19344. DUK_ASSERT(str[len] == (char) 0);
  19345. return duk_push_lstring(thr, str, len);
  19346. }
  19347. #endif /* DUK_USE_LITCACHE_SIZE */
  19348. #endif /* !DUK_USE_PREFER_SIZE */
  19349. DUK_EXTERNAL void duk_push_pointer(duk_hthread *thr, void *val) {
  19350. duk_tval *tv_slot;
  19351. DUK_ASSERT_API_ENTRY(thr);
  19352. DUK__CHECK_SPACE();
  19353. tv_slot = thr->valstack_top++;
  19354. DUK_TVAL_SET_POINTER(tv_slot, val);
  19355. }
  19356. DUK_INTERNAL duk_hstring *duk_push_uint_to_hstring(duk_hthread *thr, duk_uint_t i) {
  19357. duk_hstring *h_tmp;
  19358. DUK_ASSERT_API_ENTRY(thr);
  19359. /* XXX: this could be a direct DUK_SPRINTF to a buffer followed by duk_push_string() */
  19360. duk_push_uint(thr, (duk_uint_t) i);
  19361. h_tmp = duk_to_hstring_m1(thr);
  19362. DUK_ASSERT(h_tmp != NULL);
  19363. return h_tmp;
  19364. }
  19365. DUK_LOCAL void duk__push_this_helper(duk_hthread *thr, duk_small_uint_t check_object_coercible) {
  19366. duk_tval *tv_slot;
  19367. DUK__CHECK_SPACE();
  19368. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* because of valstack init policy */
  19369. tv_slot = thr->valstack_top++;
  19370. if (DUK_UNLIKELY(thr->callstack_curr == NULL)) {
  19371. if (check_object_coercible) {
  19372. goto type_error;
  19373. }
  19374. /* 'undefined' already on stack top */
  19375. } else {
  19376. duk_tval *tv;
  19377. /* 'this' binding is just before current activation's bottom */
  19378. DUK_ASSERT(thr->valstack_bottom > thr->valstack);
  19379. tv = thr->valstack_bottom - 1;
  19380. if (check_object_coercible &&
  19381. (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv))) {
  19382. /* XXX: better macro for DUK_TVAL_IS_UNDEFINED_OR_NULL(tv) */
  19383. goto type_error;
  19384. }
  19385. DUK_TVAL_SET_TVAL(tv_slot, tv);
  19386. DUK_TVAL_INCREF(thr, tv);
  19387. }
  19388. return;
  19389. type_error:
  19390. DUK_ERROR_TYPE(thr, DUK_STR_NOT_OBJECT_COERCIBLE);
  19391. DUK_WO_NORETURN(return;);
  19392. }
  19393. DUK_EXTERNAL void duk_push_this(duk_hthread *thr) {
  19394. DUK_ASSERT_API_ENTRY(thr);
  19395. duk__push_this_helper(thr, 0 /*check_object_coercible*/);
  19396. }
  19397. DUK_INTERNAL void duk_push_this_check_object_coercible(duk_hthread *thr) {
  19398. DUK_ASSERT_API_ENTRY(thr);
  19399. duk__push_this_helper(thr, 1 /*check_object_coercible*/);
  19400. }
  19401. DUK_INTERNAL duk_hobject *duk_push_this_coercible_to_object(duk_hthread *thr) {
  19402. duk_hobject *h;
  19403. DUK_ASSERT_API_ENTRY(thr);
  19404. duk__push_this_helper(thr, 1 /*check_object_coercible*/);
  19405. h = duk_to_hobject(thr, -1);
  19406. DUK_ASSERT(h != NULL);
  19407. return h;
  19408. }
  19409. DUK_INTERNAL duk_hstring *duk_push_this_coercible_to_string(duk_hthread *thr) {
  19410. DUK_ASSERT_API_ENTRY(thr);
  19411. duk__push_this_helper(thr, 1 /*check_object_coercible*/);
  19412. return duk_to_hstring_m1(thr); /* This will reject all Symbol values; accepts Symbol objects. */
  19413. }
  19414. DUK_INTERNAL duk_tval *duk_get_borrowed_this_tval(duk_hthread *thr) {
  19415. DUK_ASSERT_API_ENTRY(thr);
  19416. DUK_ASSERT(thr->callstack_top > 0); /* caller required to know */
  19417. DUK_ASSERT(thr->callstack_curr != NULL); /* caller required to know */
  19418. DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* consequence of above */
  19419. DUK_ASSERT(thr->valstack_bottom - 1 >= thr->valstack); /* 'this' binding exists */
  19420. return thr->valstack_bottom - 1;
  19421. }
  19422. DUK_EXTERNAL void duk_push_new_target(duk_hthread *thr) {
  19423. duk_activation *act;
  19424. DUK_ASSERT_API_ENTRY(thr);
  19425. /* https://www.ecma-international.org/ecma-262/6.0/#sec-meta-properties-runtime-semantics-evaluation
  19426. * https://www.ecma-international.org/ecma-262/6.0/#sec-getnewtarget
  19427. *
  19428. * No newTarget support now, so as a first approximation
  19429. * use the resolved (non-bound) target function.
  19430. *
  19431. * Check CONSTRUCT flag from current function, or if running
  19432. * direct eval, from a non-direct-eval parent (with possibly
  19433. * more than one nested direct eval). An alternative to this
  19434. * would be to store [[NewTarget]] as a hidden symbol of the
  19435. * lexical scope, and then just look up that variable.
  19436. *
  19437. * Calls from the application will either be for an empty
  19438. * call stack, or a Duktape/C function as the top activation.
  19439. */
  19440. act = thr->callstack_curr;
  19441. for (;;) {
  19442. if (act == NULL) {
  19443. break;
  19444. }
  19445. if (act->flags & DUK_ACT_FLAG_CONSTRUCT) {
  19446. duk_push_tval(thr, &act->tv_func);
  19447. return;
  19448. } else if (act->flags & DUK_ACT_FLAG_DIRECT_EVAL) {
  19449. act = act->parent;
  19450. } else {
  19451. break;
  19452. }
  19453. }
  19454. duk_push_undefined(thr);
  19455. }
  19456. DUK_EXTERNAL void duk_push_current_function(duk_hthread *thr) {
  19457. duk_activation *act;
  19458. DUK_ASSERT_API_ENTRY(thr);
  19459. act = thr->callstack_curr;
  19460. if (act != NULL) {
  19461. duk_push_tval(thr, &act->tv_func);
  19462. } else {
  19463. duk_push_undefined(thr);
  19464. }
  19465. }
  19466. DUK_EXTERNAL void duk_push_current_thread(duk_hthread *thr) {
  19467. DUK_ASSERT_API_ENTRY(thr);
  19468. if (thr->heap->curr_thread) {
  19469. duk_push_hobject(thr, (duk_hobject *) thr->heap->curr_thread);
  19470. } else {
  19471. duk_push_undefined(thr);
  19472. }
  19473. }
  19474. DUK_EXTERNAL void duk_push_global_object(duk_hthread *thr) {
  19475. DUK_ASSERT_API_ENTRY(thr);
  19476. duk_push_hobject_bidx(thr, DUK_BIDX_GLOBAL);
  19477. }
  19478. /* XXX: size optimize */
  19479. DUK_LOCAL void duk__push_stash(duk_hthread *thr) {
  19480. if (!duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE)) {
  19481. DUK_DDD(DUK_DDDPRINT("creating heap/global/thread stash on first use"));
  19482. duk_pop_unsafe(thr);
  19483. duk_push_bare_object(thr);
  19484. duk_dup_top(thr);
  19485. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_C); /* [ ... parent stash stash ] -> [ ... parent stash ] */
  19486. }
  19487. duk_remove_m2(thr);
  19488. }
  19489. DUK_EXTERNAL void duk_push_heap_stash(duk_hthread *thr) {
  19490. duk_heap *heap;
  19491. DUK_ASSERT_API_ENTRY(thr);
  19492. heap = thr->heap;
  19493. DUK_ASSERT(heap->heap_object != NULL);
  19494. duk_push_hobject(thr, heap->heap_object);
  19495. duk__push_stash(thr);
  19496. }
  19497. DUK_EXTERNAL void duk_push_global_stash(duk_hthread *thr) {
  19498. DUK_ASSERT_API_ENTRY(thr);
  19499. duk_push_global_object(thr);
  19500. duk__push_stash(thr);
  19501. }
  19502. DUK_EXTERNAL void duk_push_thread_stash(duk_hthread *thr, duk_hthread *target_thr) {
  19503. DUK_ASSERT_API_ENTRY(thr);
  19504. if (DUK_UNLIKELY(target_thr == NULL)) {
  19505. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  19506. DUK_WO_NORETURN(return;);
  19507. }
  19508. duk_push_hobject(thr, (duk_hobject *) target_thr);
  19509. duk__push_stash(thr);
  19510. }
  19511. /* XXX: duk_ssize_t would be useful here */
  19512. DUK_LOCAL duk_int_t duk__try_push_vsprintf(duk_hthread *thr, void *buf, duk_size_t sz, const char *fmt, va_list ap) {
  19513. duk_int_t len;
  19514. DUK_ASSERT_CTX_VALID(thr);
  19515. DUK_UNREF(thr);
  19516. /* NUL terminator handling doesn't matter here */
  19517. len = DUK_VSNPRINTF((char *) buf, sz, fmt, ap);
  19518. if (len < (duk_int_t) sz) {
  19519. /* Return value of 'sz' or more indicates output was (potentially)
  19520. * truncated.
  19521. */
  19522. return (duk_int_t) len;
  19523. }
  19524. return -1;
  19525. }
  19526. DUK_EXTERNAL const char *duk_push_vsprintf(duk_hthread *thr, const char *fmt, va_list ap) {
  19527. duk_uint8_t stack_buf[DUK_PUSH_SPRINTF_INITIAL_SIZE];
  19528. duk_size_t sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
  19529. duk_bool_t pushed_buf = 0;
  19530. void *buf;
  19531. duk_int_t len; /* XXX: duk_ssize_t */
  19532. const char *res;
  19533. DUK_ASSERT_API_ENTRY(thr);
  19534. /* special handling of fmt==NULL */
  19535. if (!fmt) {
  19536. duk_hstring *h_str;
  19537. duk_push_hstring_empty(thr);
  19538. h_str = duk_known_hstring(thr, -1);
  19539. return (const char *) DUK_HSTRING_GET_DATA(h_str);
  19540. }
  19541. /* initial estimate based on format string */
  19542. sz = DUK_STRLEN(fmt) + 16; /* format plus something to avoid just missing */
  19543. if (sz < DUK_PUSH_SPRINTF_INITIAL_SIZE) {
  19544. sz = DUK_PUSH_SPRINTF_INITIAL_SIZE;
  19545. }
  19546. DUK_ASSERT(sz > 0);
  19547. /* Try to make do with a stack buffer to avoid allocating a temporary buffer.
  19548. * This works 99% of the time which is quite nice.
  19549. */
  19550. for (;;) {
  19551. va_list ap_copy; /* copied so that 'ap' can be reused */
  19552. if (sz <= sizeof(stack_buf)) {
  19553. buf = stack_buf;
  19554. } else if (!pushed_buf) {
  19555. pushed_buf = 1;
  19556. buf = duk_push_dynamic_buffer(thr, sz);
  19557. } else {
  19558. buf = duk_resize_buffer(thr, -1, sz);
  19559. }
  19560. DUK_ASSERT(buf != NULL);
  19561. DUK_VA_COPY(ap_copy, ap);
  19562. len = duk__try_push_vsprintf(thr, buf, sz, fmt, ap_copy);
  19563. va_end(ap_copy);
  19564. if (len >= 0) {
  19565. break;
  19566. }
  19567. /* failed, resize and try again */
  19568. sz = sz * 2;
  19569. if (DUK_UNLIKELY(sz >= DUK_PUSH_SPRINTF_SANITY_LIMIT)) {
  19570. DUK_ERROR_RANGE(thr, DUK_STR_RESULT_TOO_LONG);
  19571. DUK_WO_NORETURN(return NULL;);
  19572. }
  19573. }
  19574. /* Cannot use duk_buffer_to_string() on the buffer because it is
  19575. * usually larger than 'len'; 'buf' is also usually a stack buffer.
  19576. */
  19577. res = duk_push_lstring(thr, (const char *) buf, (duk_size_t) len); /* [ buf? res ] */
  19578. if (pushed_buf) {
  19579. duk_remove_m2(thr);
  19580. }
  19581. return res;
  19582. }
  19583. DUK_EXTERNAL const char *duk_push_sprintf(duk_hthread *thr, const char *fmt, ...) {
  19584. va_list ap;
  19585. const char *ret;
  19586. DUK_ASSERT_API_ENTRY(thr);
  19587. /* allow fmt==NULL */
  19588. va_start(ap, fmt);
  19589. ret = duk_push_vsprintf(thr, fmt, ap);
  19590. va_end(ap);
  19591. return ret;
  19592. }
  19593. DUK_INTERNAL duk_hobject *duk_push_object_helper(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
  19594. duk_tval *tv_slot;
  19595. duk_hobject *h;
  19596. DUK_ASSERT_API_ENTRY(thr);
  19597. DUK_ASSERT(prototype_bidx == -1 ||
  19598. (prototype_bidx >= 0 && prototype_bidx < DUK_NUM_BUILTINS));
  19599. DUK__CHECK_SPACE();
  19600. h = duk_hobject_alloc(thr, hobject_flags_and_class);
  19601. DUK_ASSERT(h != NULL);
  19602. DUK_DDD(DUK_DDDPRINT("created object with flags: 0x%08lx", (unsigned long) h->hdr.h_flags));
  19603. tv_slot = thr->valstack_top;
  19604. DUK_TVAL_SET_OBJECT(tv_slot, h);
  19605. DUK_HOBJECT_INCREF(thr, h); /* no side effects */
  19606. thr->valstack_top++;
  19607. /* object is now reachable */
  19608. if (prototype_bidx >= 0) {
  19609. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, h, thr->builtins[prototype_bidx]);
  19610. } else {
  19611. DUK_ASSERT(prototype_bidx == -1);
  19612. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h) == NULL);
  19613. }
  19614. return h;
  19615. }
  19616. DUK_INTERNAL duk_hobject *duk_push_object_helper_proto(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_hobject *proto) {
  19617. duk_hobject *h;
  19618. DUK_ASSERT_API_ENTRY(thr);
  19619. h = duk_push_object_helper(thr, hobject_flags_and_class, -1);
  19620. DUK_ASSERT(h != NULL);
  19621. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, h, proto);
  19622. return h;
  19623. }
  19624. DUK_EXTERNAL duk_idx_t duk_push_object(duk_hthread *thr) {
  19625. DUK_ASSERT_API_ENTRY(thr);
  19626. (void) duk_push_object_helper(thr,
  19627. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19628. DUK_HOBJECT_FLAG_FASTREFS |
  19629. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  19630. DUK_BIDX_OBJECT_PROTOTYPE);
  19631. return duk_get_top_index_unsafe(thr);
  19632. }
  19633. DUK_EXTERNAL duk_idx_t duk_push_array(duk_hthread *thr) {
  19634. duk_uint_t flags;
  19635. duk_harray *obj;
  19636. duk_idx_t ret;
  19637. duk_tval *tv_slot;
  19638. DUK_ASSERT_API_ENTRY(thr);
  19639. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19640. DUK_HOBJECT_FLAG_FASTREFS |
  19641. DUK_HOBJECT_FLAG_ARRAY_PART |
  19642. DUK_HOBJECT_FLAG_EXOTIC_ARRAY |
  19643. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAY);
  19644. obj = duk_harray_alloc(thr, flags);
  19645. DUK_ASSERT(obj != NULL);
  19646. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_ARRAY_PROTOTYPE]);
  19647. tv_slot = thr->valstack_top;
  19648. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19649. DUK_HOBJECT_INCREF(thr, obj); /* XXX: could preallocate with refcount = 1 */
  19650. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19651. thr->valstack_top++;
  19652. DUK_ASSERT(obj->length == 0); /* Array .length starts at zero. */
  19653. return ret;
  19654. }
  19655. DUK_INTERNAL duk_harray *duk_push_harray(duk_hthread *thr) {
  19656. /* XXX: API call could do this directly, cast to void in API macro. */
  19657. duk_harray *a;
  19658. DUK_ASSERT_API_ENTRY(thr);
  19659. (void) duk_push_array(thr);
  19660. DUK_ASSERT(DUK_TVAL_IS_OBJECT(thr->valstack_top - 1));
  19661. a = (duk_harray *) DUK_TVAL_GET_OBJECT(thr->valstack_top - 1);
  19662. DUK_ASSERT(a != NULL);
  19663. return a;
  19664. }
  19665. /* Push a duk_harray with preallocated size (.length also set to match size).
  19666. * Caller may then populate array part of the duk_harray directly.
  19667. */
  19668. DUK_INTERNAL duk_harray *duk_push_harray_with_size(duk_hthread *thr, duk_uint32_t size) {
  19669. duk_harray *a;
  19670. DUK_ASSERT_API_ENTRY(thr);
  19671. a = duk_push_harray(thr);
  19672. duk_hobject_realloc_props(thr,
  19673. (duk_hobject *) a,
  19674. 0,
  19675. size,
  19676. 0,
  19677. 0);
  19678. a->length = size;
  19679. return a;
  19680. }
  19681. DUK_INTERNAL duk_tval *duk_push_harray_with_size_outptr(duk_hthread *thr, duk_uint32_t size) {
  19682. duk_harray *a;
  19683. DUK_ASSERT_API_ENTRY(thr);
  19684. a = duk_push_harray_with_size(thr, size);
  19685. DUK_ASSERT(a != NULL);
  19686. return DUK_HOBJECT_A_GET_BASE(thr->heap, (duk_hobject *) a);
  19687. }
  19688. DUK_EXTERNAL duk_idx_t duk_push_thread_raw(duk_hthread *thr, duk_uint_t flags) {
  19689. duk_hthread *obj;
  19690. duk_idx_t ret;
  19691. duk_tval *tv_slot;
  19692. DUK_ASSERT_API_ENTRY(thr);
  19693. DUK__CHECK_SPACE();
  19694. obj = duk_hthread_alloc(thr,
  19695. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19696. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
  19697. DUK_ASSERT(obj != NULL);
  19698. obj->state = DUK_HTHREAD_STATE_INACTIVE;
  19699. #if defined(DUK_USE_ROM_STRINGS)
  19700. /* Nothing to initialize, strs[] is in ROM. */
  19701. #else
  19702. #if defined(DUK_USE_HEAPPTR16)
  19703. obj->strs16 = thr->strs16;
  19704. #else
  19705. obj->strs = thr->strs;
  19706. #endif
  19707. #endif
  19708. DUK_DDD(DUK_DDDPRINT("created thread object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
  19709. /* make the new thread reachable */
  19710. tv_slot = thr->valstack_top;
  19711. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19712. DUK_HTHREAD_INCREF(thr, obj);
  19713. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19714. thr->valstack_top++;
  19715. /* important to do this *after* pushing, to make the thread reachable for gc */
  19716. if (DUK_UNLIKELY(!duk_hthread_init_stacks(thr->heap, obj))) {
  19717. DUK_ERROR_ALLOC_FAILED(thr);
  19718. DUK_WO_NORETURN(return 0;);
  19719. }
  19720. /* initialize built-ins - either by copying or creating new ones */
  19721. if (flags & DUK_THREAD_NEW_GLOBAL_ENV) {
  19722. duk_hthread_create_builtin_objects(obj);
  19723. } else {
  19724. duk_hthread_copy_builtin_objects(thr, obj);
  19725. }
  19726. /* default prototype */
  19727. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, obj->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
  19728. /* Initial stack size satisfies the stack slack constraints so there
  19729. * is no need to require stack here.
  19730. */
  19731. DUK_ASSERT(DUK_VALSTACK_INITIAL_SIZE >=
  19732. DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  19733. return ret;
  19734. }
  19735. DUK_INTERNAL duk_hcompfunc *duk_push_hcompfunc(duk_hthread *thr) {
  19736. duk_hcompfunc *obj;
  19737. duk_tval *tv_slot;
  19738. DUK_ASSERT_API_ENTRY(thr);
  19739. DUK__CHECK_SPACE();
  19740. /* Template functions are not strictly constructable (they don't
  19741. * have a "prototype" property for instance), so leave the
  19742. * DUK_HOBJECT_FLAG_CONSRUCTABLE flag cleared here.
  19743. */
  19744. obj = duk_hcompfunc_alloc(thr,
  19745. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19746. DUK_HOBJECT_FLAG_CALLABLE |
  19747. DUK_HOBJECT_FLAG_COMPFUNC |
  19748. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION));
  19749. if (DUK_UNLIKELY(obj == NULL)) {
  19750. DUK_ERROR_ALLOC_FAILED(thr);
  19751. DUK_WO_NORETURN(return NULL;);
  19752. }
  19753. DUK_DDD(DUK_DDDPRINT("created compiled function object with flags: 0x%08lx", (unsigned long) obj->obj.hdr.h_flags));
  19754. tv_slot = thr->valstack_top;
  19755. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19756. DUK_HOBJECT_INCREF(thr, obj);
  19757. thr->valstack_top++;
  19758. /* default prototype */
  19759. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) obj) == NULL);
  19760. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  19761. return obj;
  19762. }
  19763. DUK_INTERNAL duk_hboundfunc *duk_push_hboundfunc(duk_hthread *thr) {
  19764. duk_hboundfunc *obj;
  19765. duk_tval *tv_slot;
  19766. DUK_ASSERT_API_ENTRY(thr);
  19767. DUK__CHECK_SPACE();
  19768. obj = duk_hboundfunc_alloc(thr->heap,
  19769. DUK_HOBJECT_FLAG_EXTENSIBLE |
  19770. DUK_HOBJECT_FLAG_BOUNDFUNC |
  19771. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  19772. DUK_HOBJECT_FLAG_CALLABLE |
  19773. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION));
  19774. if (!obj) {
  19775. DUK_ERROR_ALLOC_FAILED(thr);
  19776. DUK_WO_NORETURN(return NULL;);
  19777. }
  19778. tv_slot = thr->valstack_top++;
  19779. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19780. DUK_HOBJECT_INCREF(thr, obj);
  19781. /* Prototype is left as NULL because the caller always sets it (and
  19782. * it depends on the target function).
  19783. */
  19784. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) obj) == NULL);
  19785. return obj;
  19786. }
  19787. DUK_LOCAL duk_idx_t duk__push_c_function_raw(duk_hthread *thr, duk_c_function func, duk_idx_t nargs, duk_uint_t flags, duk_small_uint_t proto_bidx) {
  19788. duk_hnatfunc *obj;
  19789. duk_idx_t ret;
  19790. duk_tval *tv_slot;
  19791. duk_int16_t func_nargs;
  19792. DUK_ASSERT_CTX_VALID(thr);
  19793. DUK__CHECK_SPACE();
  19794. if (DUK_UNLIKELY(func == NULL)) {
  19795. goto api_error;
  19796. }
  19797. if (nargs >= 0 && nargs < DUK_HNATFUNC_NARGS_MAX) {
  19798. func_nargs = (duk_int16_t) nargs;
  19799. } else if (nargs == DUK_VARARGS) {
  19800. func_nargs = DUK_HNATFUNC_NARGS_VARARGS;
  19801. } else {
  19802. goto api_error;
  19803. }
  19804. obj = duk_hnatfunc_alloc(thr, flags);
  19805. DUK_ASSERT(obj != NULL);
  19806. obj->func = func;
  19807. obj->nargs = func_nargs;
  19808. DUK_DDD(DUK_DDDPRINT("created native function object with flags: 0x%08lx, nargs=%ld",
  19809. (unsigned long) obj->obj.hdr.h_flags, (long) obj->nargs));
  19810. tv_slot = thr->valstack_top;
  19811. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19812. DUK_HOBJECT_INCREF(thr, obj);
  19813. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  19814. thr->valstack_top++;
  19815. DUK_ASSERT_BIDX_VALID(proto_bidx);
  19816. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[proto_bidx]);
  19817. return ret;
  19818. api_error:
  19819. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  19820. DUK_WO_NORETURN(return 0;);
  19821. }
  19822. DUK_EXTERNAL duk_idx_t duk_push_c_function(duk_hthread *thr, duk_c_function func, duk_int_t nargs) {
  19823. duk_uint_t flags;
  19824. DUK_ASSERT_API_ENTRY(thr);
  19825. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19826. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  19827. DUK_HOBJECT_FLAG_CALLABLE |
  19828. DUK_HOBJECT_FLAG_FASTREFS |
  19829. DUK_HOBJECT_FLAG_NATFUNC |
  19830. DUK_HOBJECT_FLAG_NEWENV |
  19831. DUK_HOBJECT_FLAG_STRICT |
  19832. DUK_HOBJECT_FLAG_NOTAIL |
  19833. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  19834. /* Default prototype is a Duktape specific %NativeFunctionPrototype%
  19835. * which provides .length and .name getters.
  19836. */
  19837. return duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE);
  19838. }
  19839. DUK_INTERNAL void duk_push_c_function_builtin(duk_hthread *thr, duk_c_function func, duk_int_t nargs) {
  19840. duk_uint_t flags;
  19841. DUK_ASSERT_API_ENTRY(thr);
  19842. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19843. DUK_HOBJECT_FLAG_CONSTRUCTABLE |
  19844. DUK_HOBJECT_FLAG_CALLABLE |
  19845. DUK_HOBJECT_FLAG_FASTREFS |
  19846. DUK_HOBJECT_FLAG_NATFUNC |
  19847. DUK_HOBJECT_FLAG_NEWENV |
  19848. DUK_HOBJECT_FLAG_STRICT |
  19849. DUK_HOBJECT_FLAG_NOTAIL |
  19850. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  19851. /* Must use Function.prototype for standard built-in functions. */
  19852. (void) duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_FUNCTION_PROTOTYPE);
  19853. }
  19854. DUK_INTERNAL void duk_push_c_function_builtin_noconstruct(duk_hthread *thr, duk_c_function func, duk_int_t nargs) {
  19855. duk_uint_t flags;
  19856. DUK_ASSERT_API_ENTRY(thr);
  19857. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  19858. DUK_HOBJECT_FLAG_CALLABLE |
  19859. DUK_HOBJECT_FLAG_FASTREFS |
  19860. DUK_HOBJECT_FLAG_NATFUNC |
  19861. DUK_HOBJECT_FLAG_NEWENV |
  19862. DUK_HOBJECT_FLAG_STRICT |
  19863. DUK_HOBJECT_FLAG_NOTAIL |
  19864. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION);
  19865. /* Must use Function.prototype for standard built-in functions. */
  19866. (void) duk__push_c_function_raw(thr, func, nargs, flags, DUK_BIDX_FUNCTION_PROTOTYPE);
  19867. }
  19868. DUK_EXTERNAL duk_idx_t duk_push_c_lightfunc(duk_hthread *thr, duk_c_function func, duk_idx_t nargs, duk_idx_t length, duk_int_t magic) {
  19869. duk_small_uint_t lf_flags;
  19870. duk_tval *tv_slot;
  19871. DUK_ASSERT_API_ENTRY(thr);
  19872. DUK__CHECK_SPACE();
  19873. if (nargs >= DUK_LFUNC_NARGS_MIN && nargs <= DUK_LFUNC_NARGS_MAX) {
  19874. /* as is */
  19875. } else if (nargs == DUK_VARARGS) {
  19876. nargs = DUK_LFUNC_NARGS_VARARGS;
  19877. } else {
  19878. goto api_error;
  19879. }
  19880. if (DUK_UNLIKELY(!(length >= DUK_LFUNC_LENGTH_MIN && length <= DUK_LFUNC_LENGTH_MAX))) {
  19881. goto api_error;
  19882. }
  19883. if (DUK_UNLIKELY(!(magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX))) {
  19884. goto api_error;
  19885. }
  19886. lf_flags = DUK_LFUNC_FLAGS_PACK((duk_small_int_t) magic, (duk_small_uint_t) length, (duk_small_uint_t) nargs);
  19887. tv_slot = thr->valstack_top++;
  19888. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv_slot));
  19889. DUK_TVAL_SET_LIGHTFUNC(tv_slot, func, lf_flags);
  19890. DUK_ASSERT(tv_slot >= thr->valstack_bottom);
  19891. return (duk_idx_t) (tv_slot - thr->valstack_bottom);
  19892. api_error:
  19893. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  19894. DUK_WO_NORETURN(return 0;);
  19895. }
  19896. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  19897. DUK_INTERNAL duk_hbufobj *duk_push_bufobj_raw(duk_hthread *thr, duk_uint_t hobject_flags_and_class, duk_small_int_t prototype_bidx) {
  19898. duk_hbufobj *obj;
  19899. duk_tval *tv_slot;
  19900. DUK_ASSERT_API_ENTRY(thr);
  19901. DUK_ASSERT(prototype_bidx >= 0);
  19902. DUK__CHECK_SPACE();
  19903. obj = duk_hbufobj_alloc(thr, hobject_flags_and_class);
  19904. DUK_ASSERT(obj != NULL);
  19905. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) obj, thr->builtins[prototype_bidx]);
  19906. DUK_ASSERT_HBUFOBJ_VALID(obj);
  19907. tv_slot = thr->valstack_top;
  19908. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) obj);
  19909. DUK_HOBJECT_INCREF(thr, obj);
  19910. thr->valstack_top++;
  19911. return obj;
  19912. }
  19913. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  19914. /* XXX: There's quite a bit of overlap with buffer creation handling in
  19915. * duk_bi_buffer.c. Look for overlap and refactor.
  19916. */
  19917. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  19918. #define DUK__PACK_ARGS(classnum,protobidx,elemtype,elemshift,istypedarray) \
  19919. (((classnum) << 24) | ((protobidx) << 16) | ((elemtype) << 8) | ((elemshift) << 4) | (istypedarray))
  19920. static const duk_uint32_t duk__bufobj_flags_lookup[] = {
  19921. /* Node.js Buffers are Uint8Array instances which inherit from Buffer.prototype. */
  19922. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_ARRAYBUFFER, DUK_BIDX_ARRAYBUFFER_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_ARRAYBUFFER */
  19923. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_NODEJS_BUFFER */
  19924. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_DATAVIEW, DUK_BIDX_DATAVIEW_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 0), /* DUK_BUFOBJ_DATAVIEW */
  19925. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT8ARRAY, DUK_BIDX_INT8ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_INT8, 0, 1), /* DUK_BUFOBJ_INT8ARRAY */
  19926. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8ARRAY, DUK_BIDX_UINT8ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8, 0, 1), /* DUK_BUFOBJ_UINT8ARRAY */
  19927. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY, DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT8CLAMPED, 0, 1), /* DUK_BUFOBJ_UINT8CLAMPEDARRAY */
  19928. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT16ARRAY, DUK_BIDX_INT16ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_INT16, 1, 1), /* DUK_BUFOBJ_INT16ARRAY */
  19929. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT16ARRAY, DUK_BIDX_UINT16ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT16, 1, 1), /* DUK_BUFOBJ_UINT16ARRAY */
  19930. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_INT32ARRAY, DUK_BIDX_INT32ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_INT32, 2, 1), /* DUK_BUFOBJ_INT32ARRAY */
  19931. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_UINT32ARRAY, DUK_BIDX_UINT32ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_UINT32, 2, 1), /* DUK_BUFOBJ_UINT32ARRAY */
  19932. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT32ARRAY, DUK_BIDX_FLOAT32ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_FLOAT32, 2, 1), /* DUK_BUFOBJ_FLOAT32ARRAY */
  19933. DUK__PACK_ARGS(DUK_HOBJECT_CLASS_FLOAT64ARRAY, DUK_BIDX_FLOAT64ARRAY_PROTOTYPE, DUK_HBUFOBJ_ELEM_FLOAT64, 3, 1) /* DUK_BUFOBJ_FLOAT64ARRAY */
  19934. };
  19935. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  19936. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  19937. DUK_EXTERNAL void duk_push_buffer_object(duk_hthread *thr, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) {
  19938. duk_hbufobj *h_bufobj;
  19939. duk_hbuffer *h_val;
  19940. duk_hobject *h_arraybuf;
  19941. duk_uint32_t tmp;
  19942. duk_uint_t classnum;
  19943. duk_uint_t protobidx;
  19944. duk_uint_t lookupidx;
  19945. duk_uint_t uint_offset, uint_length, uint_added;
  19946. DUK_ASSERT_API_ENTRY(thr);
  19947. /* The underlying types for offset/length in duk_hbufobj is
  19948. * duk_uint_t; make sure argument values fit.
  19949. */
  19950. uint_offset = (duk_uint_t) byte_offset;
  19951. uint_length = (duk_uint_t) byte_length;
  19952. if (sizeof(duk_size_t) != sizeof(duk_uint_t)) {
  19953. if (DUK_UNLIKELY((duk_size_t) uint_offset != byte_offset || (duk_size_t) uint_length != byte_length)) {
  19954. goto range_error;
  19955. }
  19956. }
  19957. DUK_ASSERT_DISABLE(flags >= 0); /* flags is unsigned */
  19958. lookupidx = flags;
  19959. if (DUK_UNLIKELY(lookupidx >= sizeof(duk__bufobj_flags_lookup) / sizeof(duk_uint32_t))) {
  19960. goto arg_error;
  19961. }
  19962. tmp = duk__bufobj_flags_lookup[lookupidx];
  19963. classnum = tmp >> 24;
  19964. protobidx = (tmp >> 16) & 0xff;
  19965. h_arraybuf = duk_get_hobject(thr, idx_buffer);
  19966. if (h_arraybuf != NULL && /* argument is an object */
  19967. flags != DUK_BUFOBJ_ARRAYBUFFER && /* creating a view */
  19968. DUK_HOBJECT_GET_CLASS_NUMBER(h_arraybuf) == DUK_HOBJECT_CLASS_ARRAYBUFFER /* argument is ArrayBuffer */) {
  19969. duk_uint_t tmp_offset;
  19970. DUK_ASSERT_HBUFOBJ_VALID((duk_hbufobj *) h_arraybuf);
  19971. h_val = ((duk_hbufobj *) h_arraybuf)->buf;
  19972. if (DUK_UNLIKELY(h_val == NULL)) {
  19973. goto arg_error;
  19974. }
  19975. tmp_offset = uint_offset + ((duk_hbufobj *) h_arraybuf)->offset;
  19976. if (DUK_UNLIKELY(tmp_offset < uint_offset)) {
  19977. goto range_error;
  19978. }
  19979. uint_offset = tmp_offset;
  19980. /* Note intentional difference to new TypedArray(): we allow
  19981. * caller to create an uncovered typed array (which is memory
  19982. * safe); new TypedArray() rejects it.
  19983. */
  19984. } else {
  19985. /* Handle unexpected object arguments here too, for nice error
  19986. * messages.
  19987. */
  19988. h_arraybuf = NULL;
  19989. h_val = duk_require_hbuffer(thr, idx_buffer);
  19990. }
  19991. /* Wrap check for offset+length. */
  19992. uint_added = uint_offset + uint_length;
  19993. if (DUK_UNLIKELY(uint_added < uint_offset)) {
  19994. goto range_error;
  19995. }
  19996. DUK_ASSERT(uint_added >= uint_offset && uint_added >= uint_length);
  19997. DUK_ASSERT(h_val != NULL);
  19998. h_bufobj = duk_push_bufobj_raw(thr,
  19999. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20000. DUK_HOBJECT_FLAG_BUFOBJ |
  20001. DUK_HOBJECT_CLASS_AS_FLAGS(classnum),
  20002. (duk_small_int_t) protobidx);
  20003. DUK_ASSERT(h_bufobj != NULL);
  20004. h_bufobj->buf = h_val;
  20005. DUK_HBUFFER_INCREF(thr, h_val);
  20006. h_bufobj->buf_prop = h_arraybuf;
  20007. DUK_HOBJECT_INCREF_ALLOWNULL(thr, h_arraybuf);
  20008. h_bufobj->offset = uint_offset;
  20009. h_bufobj->length = uint_length;
  20010. h_bufobj->shift = (tmp >> 4) & 0x0f;
  20011. h_bufobj->elem_type = (tmp >> 8) & 0xff;
  20012. h_bufobj->is_typedarray = tmp & 0x0f;
  20013. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  20014. /* TypedArray views need an automatic ArrayBuffer which must be
  20015. * provided as .buffer property of the view. The ArrayBuffer is
  20016. * referenced via duk_hbufobj->buf_prop and an inherited .buffer
  20017. * accessor returns it. The ArrayBuffer is created lazily on first
  20018. * access if necessary so we don't need to do anything more here.
  20019. */
  20020. return;
  20021. range_error:
  20022. DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARGS);
  20023. DUK_WO_NORETURN(return;);
  20024. arg_error:
  20025. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_ARGS);
  20026. DUK_WO_NORETURN(return;);
  20027. }
  20028. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  20029. DUK_EXTERNAL void duk_push_buffer_object(duk_hthread *thr, duk_idx_t idx_buffer, duk_size_t byte_offset, duk_size_t byte_length, duk_uint_t flags) {
  20030. DUK_ASSERT_API_ENTRY(thr);
  20031. DUK_UNREF(idx_buffer);
  20032. DUK_UNREF(byte_offset);
  20033. DUK_UNREF(byte_length);
  20034. DUK_UNREF(flags);
  20035. DUK_ERROR_UNSUPPORTED(thr);
  20036. DUK_WO_NORETURN(return;);
  20037. }
  20038. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  20039. DUK_EXTERNAL duk_idx_t duk_push_error_object_va_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
  20040. duk_hobject *proto;
  20041. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  20042. duk_small_uint_t augment_flags;
  20043. #endif
  20044. DUK_ASSERT_API_ENTRY(thr);
  20045. DUK_ASSERT(thr != NULL);
  20046. DUK_UNREF(filename);
  20047. DUK_UNREF(line);
  20048. /* Error code also packs a tracedata related flag. */
  20049. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  20050. augment_flags = 0;
  20051. if (err_code & DUK_ERRCODE_FLAG_NOBLAME_FILELINE) {
  20052. augment_flags = DUK_AUGMENT_FLAG_NOBLAME_FILELINE;
  20053. }
  20054. #endif
  20055. err_code = err_code & (~DUK_ERRCODE_FLAG_NOBLAME_FILELINE);
  20056. /* error gets its 'name' from the prototype */
  20057. proto = duk_error_prototype_from_code(thr, err_code);
  20058. (void) duk_push_object_helper_proto(thr,
  20059. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20060. DUK_HOBJECT_FLAG_FASTREFS |
  20061. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR),
  20062. proto);
  20063. /* ... and its 'message' from an instance property */
  20064. if (fmt) {
  20065. duk_push_vsprintf(thr, fmt, ap);
  20066. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  20067. } else {
  20068. /* If no explicit message given, put error code into message field
  20069. * (as a number). This is not fully in keeping with the ECMAScript
  20070. * error model because messages are supposed to be strings (Error
  20071. * constructors use ToString() on their argument). However, it's
  20072. * probably more useful than having a separate 'code' property.
  20073. */
  20074. duk_push_int(thr, err_code);
  20075. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  20076. }
  20077. /* XXX: .code = err_code disabled, not sure if useful */
  20078. /* Creation time error augmentation */
  20079. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  20080. /* filename may be NULL in which case file/line is not recorded */
  20081. duk_err_augment_error_create(thr, thr, filename, line, augment_flags); /* may throw an error */
  20082. #endif
  20083. return duk_get_top_index_unsafe(thr);
  20084. }
  20085. DUK_EXTERNAL duk_idx_t duk_push_error_object_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
  20086. va_list ap;
  20087. duk_idx_t ret;
  20088. DUK_ASSERT_API_ENTRY(thr);
  20089. va_start(ap, fmt);
  20090. ret = duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
  20091. va_end(ap);
  20092. return ret;
  20093. }
  20094. #if !defined(DUK_USE_VARIADIC_MACROS)
  20095. DUK_EXTERNAL duk_idx_t duk_push_error_object_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, ...) {
  20096. const char *filename = duk_api_global_filename;
  20097. duk_int_t line = duk_api_global_line;
  20098. va_list ap;
  20099. duk_idx_t ret;
  20100. DUK_ASSERT_API_ENTRY(thr);
  20101. duk_api_global_filename = NULL;
  20102. duk_api_global_line = 0;
  20103. va_start(ap, fmt);
  20104. ret = duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
  20105. va_end(ap);
  20106. return ret;
  20107. }
  20108. #endif /* DUK_USE_VARIADIC_MACROS */
  20109. DUK_EXTERNAL void *duk_push_buffer_raw(duk_hthread *thr, duk_size_t size, duk_small_uint_t flags) {
  20110. duk_tval *tv_slot;
  20111. duk_hbuffer *h;
  20112. void *buf_data;
  20113. DUK_ASSERT_API_ENTRY(thr);
  20114. DUK__CHECK_SPACE();
  20115. /* Check for maximum buffer length. */
  20116. if (DUK_UNLIKELY(size > DUK_HBUFFER_MAX_BYTELEN)) {
  20117. DUK_ERROR_RANGE(thr, DUK_STR_BUFFER_TOO_LONG);
  20118. DUK_WO_NORETURN(return NULL;);
  20119. }
  20120. h = duk_hbuffer_alloc(thr->heap, size, flags, &buf_data);
  20121. if (DUK_UNLIKELY(h == NULL)) {
  20122. DUK_ERROR_ALLOC_FAILED(thr);
  20123. DUK_WO_NORETURN(return NULL;);
  20124. }
  20125. tv_slot = thr->valstack_top;
  20126. DUK_TVAL_SET_BUFFER(tv_slot, h);
  20127. DUK_HBUFFER_INCREF(thr, h);
  20128. thr->valstack_top++;
  20129. return (void *) buf_data;
  20130. }
  20131. DUK_INTERNAL void *duk_push_fixed_buffer_nozero(duk_hthread *thr, duk_size_t len) {
  20132. DUK_ASSERT_API_ENTRY(thr);
  20133. return duk_push_buffer_raw(thr, len, DUK_BUF_FLAG_NOZERO);
  20134. }
  20135. DUK_INTERNAL void *duk_push_fixed_buffer_zero(duk_hthread *thr, duk_size_t len) {
  20136. void *ptr;
  20137. DUK_ASSERT_API_ENTRY(thr);
  20138. ptr = duk_push_buffer_raw(thr, len, 0);
  20139. DUK_ASSERT(ptr != NULL);
  20140. #if !defined(DUK_USE_ZERO_BUFFER_DATA)
  20141. /* ES2015 requires zeroing even when DUK_USE_ZERO_BUFFER_DATA
  20142. * is not set.
  20143. */
  20144. duk_memzero((void *) ptr, (size_t) len);
  20145. #endif
  20146. return ptr;
  20147. }
  20148. #if defined(DUK_USE_ES6_PROXY)
  20149. DUK_EXTERNAL duk_idx_t duk_push_proxy(duk_hthread *thr, duk_uint_t proxy_flags) {
  20150. duk_hobject *h_target;
  20151. duk_hobject *h_handler;
  20152. duk_hproxy *h_proxy;
  20153. duk_tval *tv_slot;
  20154. duk_uint_t flags;
  20155. DUK_ASSERT_API_ENTRY(thr);
  20156. DUK_UNREF(proxy_flags);
  20157. /* DUK__CHECK_SPACE() unnecessary because the Proxy is written to
  20158. * value stack in-place.
  20159. */
  20160. #if 0
  20161. DUK__CHECK_SPACE();
  20162. #endif
  20163. /* Reject a proxy object as the target because it would need
  20164. * special handling in property lookups. (ES2015 has no such
  20165. * restriction.)
  20166. */
  20167. h_target = duk_require_hobject_promote_mask(thr, -2, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  20168. DUK_ASSERT(h_target != NULL);
  20169. if (DUK_HOBJECT_IS_PROXY(h_target)) {
  20170. goto fail_args;
  20171. }
  20172. /* Reject a proxy object as the handler because it would cause
  20173. * potentially unbounded recursion. (ES2015 has no such
  20174. * restriction.)
  20175. *
  20176. * There's little practical reason to use a lightfunc or a plain
  20177. * buffer as the handler table: one could only provide traps via
  20178. * their prototype objects (Function.prototype and ArrayBuffer.prototype).
  20179. * Even so, as lightfuncs and plain buffers mimic their object
  20180. * counterparts, they're promoted and accepted here.
  20181. */
  20182. h_handler = duk_require_hobject_promote_mask(thr, -1, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  20183. DUK_ASSERT(h_handler != NULL);
  20184. if (DUK_HOBJECT_IS_PROXY(h_handler)) {
  20185. goto fail_args;
  20186. }
  20187. /* XXX: Proxy object currently has no prototype, so ToPrimitive()
  20188. * coercion fails which is a bit confusing.
  20189. */
  20190. /* CALLABLE and CONSTRUCTABLE flags are copied from the (initial)
  20191. * target, see ES2015 Sections 9.5.15 and 9.5.13.
  20192. */
  20193. flags = DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h_target) &
  20194. (DUK_HOBJECT_FLAG_CALLABLE | DUK_HOBJECT_FLAG_CONSTRUCTABLE);
  20195. flags |= DUK_HOBJECT_FLAG_EXTENSIBLE |
  20196. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ;
  20197. if (flags & DUK_HOBJECT_FLAG_CALLABLE) {
  20198. flags |= DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_FUNCTION) |
  20199. DUK_HOBJECT_FLAG_SPECIAL_CALL;
  20200. } else {
  20201. flags |= DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT);
  20202. }
  20203. h_proxy = duk_hproxy_alloc(thr, flags);
  20204. DUK_ASSERT(h_proxy != NULL);
  20205. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_proxy) == NULL);
  20206. /* Initialize Proxy target and handler references; avoid INCREF
  20207. * by stealing the value stack refcounts via direct value stack
  20208. * manipulation. INCREF is needed for the Proxy itself however.
  20209. */
  20210. DUK_ASSERT(h_target != NULL);
  20211. h_proxy->target = h_target;
  20212. DUK_ASSERT(h_handler != NULL);
  20213. h_proxy->handler = h_handler;
  20214. DUK_ASSERT_HPROXY_VALID(h_proxy);
  20215. DUK_ASSERT(duk_get_hobject(thr, -2) == h_target);
  20216. DUK_ASSERT(duk_get_hobject(thr, -1) == h_handler);
  20217. tv_slot = thr->valstack_top - 2;
  20218. DUK_ASSERT(tv_slot >= thr->valstack_bottom);
  20219. DUK_TVAL_SET_OBJECT(tv_slot, (duk_hobject *) h_proxy);
  20220. DUK_HOBJECT_INCREF(thr, (duk_hobject *) h_proxy);
  20221. tv_slot++;
  20222. DUK_TVAL_SET_UNDEFINED(tv_slot); /* [ ... target handler ] -> [ ... proxy undefined ] */
  20223. thr->valstack_top = tv_slot; /* -> [ ... proxy ] */
  20224. DUK_DD(DUK_DDPRINT("created Proxy: %!iT", duk_get_tval(thr, -1)));
  20225. return (duk_idx_t) (thr->valstack_top - thr->valstack_bottom - 1);
  20226. fail_args:
  20227. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  20228. DUK_WO_NORETURN(return 0;);
  20229. }
  20230. #else /* DUK_USE_ES6_PROXY */
  20231. DUK_EXTERNAL duk_idx_t duk_push_proxy(duk_hthread *thr, duk_uint_t proxy_flags) {
  20232. DUK_ASSERT_API_ENTRY(thr);
  20233. DUK_UNREF(proxy_flags);
  20234. DUK_ERROR_UNSUPPORTED(thr);
  20235. DUK_WO_NORETURN(return 0;);
  20236. }
  20237. #endif /* DUK_USE_ES6_PROXY */
  20238. #if defined(DUK_USE_ASSERTIONS)
  20239. DUK_LOCAL void duk__validate_push_heapptr(duk_hthread *thr, void *ptr) {
  20240. duk_heaphdr *h;
  20241. duk_heaphdr *curr;
  20242. duk_bool_t found = 0;
  20243. h = (duk_heaphdr *) ptr;
  20244. if (h == NULL) {
  20245. /* Allowed. */
  20246. return;
  20247. }
  20248. DUK_ASSERT(h != NULL);
  20249. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  20250. /* One particular problem case is where an object has been
  20251. * queued for finalization but the finalizer hasn't yet been
  20252. * executed.
  20253. *
  20254. * Corner case: we're running in a finalizer for object X, and
  20255. * user code calls duk_push_heapptr() for X itself. In this
  20256. * case X will be in finalize_list, and we can detect the case
  20257. * by seeing that X's FINALIZED flag is set (which is done before
  20258. * the finalizer starts executing).
  20259. */
  20260. #if defined(DUK_USE_FINALIZER_SUPPORT)
  20261. for (curr = thr->heap->finalize_list;
  20262. curr != NULL;
  20263. curr = DUK_HEAPHDR_GET_NEXT(thr->heap, curr)) {
  20264. /* FINALIZABLE is set for all objects on finalize_list
  20265. * except for an object being finalized right now. So
  20266. * can't assert here.
  20267. */
  20268. #if 0
  20269. DUK_ASSERT(DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  20270. #endif
  20271. if (curr == h) {
  20272. if (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h)) {
  20273. /* Object is currently being finalized. */
  20274. DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
  20275. found = 1;
  20276. } else {
  20277. /* Not being finalized but on finalize_list,
  20278. * allowed since Duktape 2.1.
  20279. */
  20280. DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
  20281. found = 1;
  20282. }
  20283. }
  20284. }
  20285. #endif /* DUK_USE_FINALIZER_SUPPORT */
  20286. #if defined(DUK_USE_REFERENCE_COUNTING)
  20287. /* Because refzero_list is now processed to completion inline with
  20288. * no side effects, it's always empty here.
  20289. */
  20290. DUK_ASSERT(thr->heap->refzero_list == NULL);
  20291. #endif
  20292. /* If not present in finalize_list (or refzero_list), it
  20293. * must be either in heap_allocated or the string table.
  20294. */
  20295. if (DUK_HEAPHDR_IS_STRING(h)) {
  20296. duk_uint32_t i;
  20297. duk_hstring *str;
  20298. duk_heap *heap = thr->heap;
  20299. DUK_ASSERT(found == 0);
  20300. for (i = 0; i < heap->st_size; i++) {
  20301. #if defined(DUK_USE_STRTAB_PTRCOMP)
  20302. str = DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, heap->strtable16[i]);
  20303. #else
  20304. str = heap->strtable[i];
  20305. #endif
  20306. while (str != NULL) {
  20307. if (str == (duk_hstring *) h) {
  20308. DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
  20309. found = 1;
  20310. break;
  20311. }
  20312. str = str->hdr.h_next;
  20313. }
  20314. }
  20315. DUK_ASSERT(found != 0);
  20316. } else {
  20317. for (curr = thr->heap->heap_allocated;
  20318. curr != NULL;
  20319. curr = DUK_HEAPHDR_GET_NEXT(thr->heap, curr)) {
  20320. if (curr == h) {
  20321. DUK_ASSERT(found == 0); /* Would indicate corrupted lists. */
  20322. found = 1;
  20323. }
  20324. }
  20325. DUK_ASSERT(found != 0);
  20326. }
  20327. }
  20328. #endif /* DUK_USE_ASSERTIONS */
  20329. DUK_EXTERNAL duk_idx_t duk_push_heapptr(duk_hthread *thr, void *ptr) {
  20330. duk_idx_t ret;
  20331. duk_tval *tv;
  20332. DUK_ASSERT_API_ENTRY(thr);
  20333. /* Reviving an object using a heap pointer is a dangerous API
  20334. * operation: if the application doesn't guarantee that the
  20335. * pointer target is always reachable, difficult-to-diagnose
  20336. * problems may ensue. Try to validate the 'ptr' argument to
  20337. * the extent possible.
  20338. */
  20339. #if defined(DUK_USE_ASSERTIONS)
  20340. duk__validate_push_heapptr(thr, ptr);
  20341. #endif
  20342. DUK__CHECK_SPACE();
  20343. ret = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  20344. tv = thr->valstack_top++;
  20345. if (ptr == NULL) {
  20346. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
  20347. return ret;
  20348. }
  20349. DUK_ASSERT_HEAPHDR_VALID((duk_heaphdr *) ptr);
  20350. /* If the argument is on finalize_list it has technically been
  20351. * unreachable before duk_push_heapptr() but it's still safe to
  20352. * push it. Starting from Duktape 2.1 allow application code to
  20353. * do so. There are two main cases:
  20354. *
  20355. * (1) The object is on the finalize_list and we're called by
  20356. * the finalizer for the object being finalized. In this
  20357. * case do nothing: finalize_list handling will deal with
  20358. * the object queueing. This is detected by the object not
  20359. * having a FINALIZABLE flag despite being on the finalize_list;
  20360. * the flag is cleared for the object being finalized only.
  20361. *
  20362. * (2) The object is on the finalize_list but is not currently
  20363. * being processed. In this case the object can be queued
  20364. * back to heap_allocated with a few flags cleared, in effect
  20365. * cancelling the finalizer.
  20366. */
  20367. if (DUK_UNLIKELY(DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) ptr))) {
  20368. duk_heaphdr *curr;
  20369. DUK_D(DUK_DPRINT("duk_push_heapptr() with a pointer on finalize_list, autorescue"));
  20370. curr = (duk_heaphdr *) ptr;
  20371. DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
  20372. /* Because FINALIZED is set prior to finalizer call, it will
  20373. * be set for the object being currently finalized, but not
  20374. * for other objects on finalize_list.
  20375. */
  20376. DUK_HEAPHDR_CLEAR_FINALIZED(curr);
  20377. /* Dequeue object from finalize_list and queue it back to
  20378. * heap_allocated.
  20379. */
  20380. #if defined(DUK_USE_REFERENCE_COUNTING)
  20381. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) >= 1); /* Preincremented on finalize_list insert. */
  20382. DUK_HEAPHDR_PREDEC_REFCOUNT(curr);
  20383. #endif
  20384. DUK_HEAP_REMOVE_FROM_FINALIZE_LIST(thr->heap, curr);
  20385. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(thr->heap, curr);
  20386. /* Continue with the rest. */
  20387. }
  20388. switch (DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr)) {
  20389. case DUK_HTYPE_STRING:
  20390. DUK_TVAL_SET_STRING(tv, (duk_hstring *) ptr);
  20391. break;
  20392. case DUK_HTYPE_OBJECT:
  20393. DUK_TVAL_SET_OBJECT(tv, (duk_hobject *) ptr);
  20394. break;
  20395. default:
  20396. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) ptr) == DUK_HTYPE_BUFFER);
  20397. DUK_TVAL_SET_BUFFER(tv, (duk_hbuffer *) ptr);
  20398. break;
  20399. }
  20400. DUK_HEAPHDR_INCREF(thr, (duk_heaphdr *) ptr);
  20401. return ret;
  20402. }
  20403. /* Push object with no prototype, i.e. a "bare" object. */
  20404. DUK_EXTERNAL duk_idx_t duk_push_bare_object(duk_hthread *thr) {
  20405. DUK_ASSERT_API_ENTRY(thr);
  20406. (void) duk_push_object_helper(thr,
  20407. DUK_HOBJECT_FLAG_EXTENSIBLE |
  20408. DUK_HOBJECT_FLAG_FASTREFS |
  20409. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  20410. -1); /* no prototype */
  20411. return duk_get_top_index_unsafe(thr);
  20412. }
  20413. DUK_INTERNAL void duk_push_hstring(duk_hthread *thr, duk_hstring *h) {
  20414. duk_tval tv;
  20415. DUK_ASSERT_API_ENTRY(thr);
  20416. DUK_ASSERT(h != NULL);
  20417. DUK_TVAL_SET_STRING(&tv, h);
  20418. duk_push_tval(thr, &tv);
  20419. }
  20420. DUK_INTERNAL void duk_push_hstring_stridx(duk_hthread *thr, duk_small_uint_t stridx) {
  20421. DUK_ASSERT_API_ENTRY(thr);
  20422. DUK_ASSERT_STRIDX_VALID(stridx);
  20423. duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
  20424. }
  20425. DUK_INTERNAL void duk_push_hstring_empty(duk_hthread *thr) {
  20426. DUK_ASSERT_API_ENTRY(thr);
  20427. duk_push_hstring(thr, DUK_HTHREAD_GET_STRING(thr, DUK_STRIDX_EMPTY_STRING));
  20428. }
  20429. DUK_INTERNAL void duk_push_hobject(duk_hthread *thr, duk_hobject *h) {
  20430. duk_tval tv;
  20431. DUK_ASSERT_API_ENTRY(thr);
  20432. DUK_ASSERT(h != NULL);
  20433. DUK_TVAL_SET_OBJECT(&tv, h);
  20434. duk_push_tval(thr, &tv);
  20435. }
  20436. DUK_INTERNAL void duk_push_hbuffer(duk_hthread *thr, duk_hbuffer *h) {
  20437. duk_tval tv;
  20438. DUK_ASSERT_API_ENTRY(thr);
  20439. DUK_ASSERT(h != NULL);
  20440. DUK_TVAL_SET_BUFFER(&tv, h);
  20441. duk_push_tval(thr, &tv);
  20442. }
  20443. DUK_INTERNAL void duk_push_hobject_bidx(duk_hthread *thr, duk_small_int_t builtin_idx) {
  20444. DUK_ASSERT_API_ENTRY(thr);
  20445. DUK_ASSERT(builtin_idx >= 0 && builtin_idx < DUK_NUM_BUILTINS);
  20446. DUK_ASSERT(thr->builtins[builtin_idx] != NULL);
  20447. duk_push_hobject(thr, thr->builtins[builtin_idx]);
  20448. }
  20449. /*
  20450. * Poppers
  20451. */
  20452. DUK_LOCAL DUK_ALWAYS_INLINE void duk__pop_n_unsafe_raw(duk_hthread *thr, duk_idx_t count) {
  20453. duk_tval *tv;
  20454. #if defined(DUK_USE_REFERENCE_COUNTING)
  20455. duk_tval *tv_end;
  20456. #endif
  20457. DUK_ASSERT_CTX_VALID(thr);
  20458. DUK_ASSERT(count >= 0);
  20459. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) count);
  20460. #if defined(DUK_USE_REFERENCE_COUNTING)
  20461. tv = thr->valstack_top;
  20462. tv_end = tv - count;
  20463. while (tv != tv_end) {
  20464. tv--;
  20465. DUK_ASSERT(tv >= thr->valstack_bottom);
  20466. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
  20467. }
  20468. thr->valstack_top = tv;
  20469. DUK_REFZERO_CHECK_FAST(thr);
  20470. #else
  20471. tv = thr->valstack_top;
  20472. while (count > 0) {
  20473. count--;
  20474. tv--;
  20475. DUK_ASSERT(tv >= thr->valstack_bottom);
  20476. DUK_TVAL_SET_UNDEFINED(tv);
  20477. }
  20478. thr->valstack_top = tv;
  20479. #endif
  20480. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20481. }
  20482. DUK_EXTERNAL void duk_pop_n(duk_hthread *thr, duk_idx_t count) {
  20483. DUK_ASSERT_API_ENTRY(thr);
  20484. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20485. if (DUK_UNLIKELY((duk_uidx_t) (thr->valstack_top - thr->valstack_bottom) < (duk_uidx_t) count)) {
  20486. DUK_ERROR_RANGE_INVALID_COUNT(thr);
  20487. DUK_WO_NORETURN(return;);
  20488. }
  20489. DUK_ASSERT(count >= 0);
  20490. duk__pop_n_unsafe_raw(thr, count);
  20491. }
  20492. #if defined(DUK_USE_PREFER_SIZE)
  20493. DUK_INTERNAL void duk_pop_n_unsafe(duk_hthread *thr, duk_idx_t count) {
  20494. DUK_ASSERT_API_ENTRY(thr);
  20495. duk_pop_n(thr, count);
  20496. }
  20497. #else /* DUK_USE_PREFER_SIZE */
  20498. DUK_INTERNAL void duk_pop_n_unsafe(duk_hthread *thr, duk_idx_t count) {
  20499. DUK_ASSERT_API_ENTRY(thr);
  20500. duk__pop_n_unsafe_raw(thr, count);
  20501. }
  20502. #endif /* DUK_USE_PREFER_SIZE */
  20503. /* Pop N elements without DECREF (in effect "stealing" any actual refcounts). */
  20504. #if defined(DUK_USE_REFERENCE_COUNTING)
  20505. DUK_INTERNAL void duk_pop_n_nodecref_unsafe(duk_hthread *thr, duk_idx_t count) {
  20506. duk_tval *tv;
  20507. DUK_ASSERT_API_ENTRY(thr);
  20508. DUK_ASSERT(count >= 0);
  20509. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20510. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) count);
  20511. tv = thr->valstack_top;
  20512. while (count > 0) {
  20513. count--;
  20514. tv--;
  20515. DUK_ASSERT(tv >= thr->valstack_bottom);
  20516. DUK_TVAL_SET_UNDEFINED(tv);
  20517. }
  20518. thr->valstack_top = tv;
  20519. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20520. }
  20521. #else /* DUK_USE_REFERENCE_COUNTING */
  20522. DUK_INTERNAL void duk_pop_n_nodecref_unsafe(duk_hthread *thr, duk_idx_t count) {
  20523. DUK_ASSERT_API_ENTRY(thr);
  20524. duk_pop_n_unsafe(thr, count);
  20525. }
  20526. #endif /* DUK_USE_REFERENCE_COUNTING */
  20527. /* Popping one element is called so often that when footprint is not an issue,
  20528. * compile a specialized function for it.
  20529. */
  20530. #if defined(DUK_USE_PREFER_SIZE)
  20531. DUK_EXTERNAL void duk_pop(duk_hthread *thr) {
  20532. DUK_ASSERT_API_ENTRY(thr);
  20533. duk_pop_n(thr, 1);
  20534. }
  20535. DUK_INTERNAL void duk_pop_unsafe(duk_hthread *thr) {
  20536. DUK_ASSERT_API_ENTRY(thr);
  20537. duk_pop_n_unsafe(thr, 1);
  20538. }
  20539. DUK_INTERNAL void duk_pop_nodecref_unsafe(duk_hthread *thr) {
  20540. DUK_ASSERT_API_ENTRY(thr);
  20541. duk_pop_n_nodecref_unsafe(thr, 1);
  20542. }
  20543. #else /* DUK_USE_PREFER_SIZE */
  20544. DUK_LOCAL DUK_ALWAYS_INLINE void duk__pop_unsafe_raw(duk_hthread *thr) {
  20545. duk_tval *tv;
  20546. DUK_ASSERT_CTX_VALID(thr);
  20547. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  20548. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20549. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 1);
  20550. tv = --thr->valstack_top;
  20551. DUK_ASSERT(tv >= thr->valstack_bottom);
  20552. #if defined(DUK_USE_REFERENCE_COUNTING)
  20553. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  20554. #else
  20555. DUK_TVAL_SET_UNDEFINED(tv);
  20556. #endif
  20557. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20558. }
  20559. DUK_EXTERNAL void duk_pop(duk_hthread *thr) {
  20560. DUK_ASSERT_API_ENTRY(thr);
  20561. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20562. if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) {
  20563. DUK_ERROR_RANGE_INVALID_COUNT(thr);
  20564. DUK_WO_NORETURN(return;);
  20565. }
  20566. duk__pop_unsafe_raw(thr);
  20567. }
  20568. DUK_INTERNAL void duk_pop_unsafe(duk_hthread *thr) {
  20569. DUK_ASSERT_API_ENTRY(thr);
  20570. duk__pop_unsafe_raw(thr);
  20571. }
  20572. DUK_INTERNAL void duk_pop_nodecref_unsafe(duk_hthread *thr) {
  20573. duk_tval *tv;
  20574. DUK_ASSERT_API_ENTRY(thr);
  20575. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  20576. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20577. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 1);
  20578. tv = --thr->valstack_top;
  20579. DUK_ASSERT(tv >= thr->valstack_bottom);
  20580. DUK_TVAL_SET_UNDEFINED(tv);
  20581. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20582. }
  20583. #endif /* !DUK_USE_PREFER_SIZE */
  20584. #if defined(DUK_USE_PREFER_SIZE)
  20585. DUK_INTERNAL void duk_pop_undefined(duk_hthread *thr) {
  20586. DUK_ASSERT_API_ENTRY(thr);
  20587. duk_pop_nodecref_unsafe(thr);
  20588. }
  20589. #else /* DUK_USE_PREFER_SIZE */
  20590. DUK_INTERNAL void duk_pop_undefined(duk_hthread *thr) {
  20591. DUK_ASSERT_API_ENTRY(thr);
  20592. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  20593. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20594. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 1);
  20595. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
  20596. thr->valstack_top--;
  20597. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20598. }
  20599. #endif /* !DUK_USE_PREFER_SIZE */
  20600. #if defined(DUK_USE_PREFER_SIZE)
  20601. DUK_EXTERNAL void duk_pop_2(duk_hthread *thr) {
  20602. DUK_ASSERT_API_ENTRY(thr);
  20603. duk_pop_n(thr, 2);
  20604. }
  20605. DUK_INTERNAL void duk_pop_2_unsafe(duk_hthread *thr) {
  20606. DUK_ASSERT_API_ENTRY(thr);
  20607. duk_pop_n_unsafe(thr, 2);
  20608. }
  20609. DUK_INTERNAL void duk_pop_2_nodecref_unsafe(duk_hthread *thr) {
  20610. DUK_ASSERT_API_ENTRY(thr);
  20611. duk_pop_n_nodecref_unsafe(thr, 2);
  20612. }
  20613. #else
  20614. DUK_LOCAL DUK_ALWAYS_INLINE void duk__pop_2_unsafe_raw(duk_hthread *thr) {
  20615. duk_tval *tv;
  20616. DUK_ASSERT_CTX_VALID(thr);
  20617. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  20618. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20619. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 2);
  20620. tv = --thr->valstack_top;
  20621. DUK_ASSERT(tv >= thr->valstack_bottom);
  20622. #if defined(DUK_USE_REFERENCE_COUNTING)
  20623. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  20624. #else
  20625. DUK_TVAL_SET_UNDEFINED(tv);
  20626. #endif
  20627. tv = --thr->valstack_top;
  20628. DUK_ASSERT(tv >= thr->valstack_bottom);
  20629. #if defined(DUK_USE_REFERENCE_COUNTING)
  20630. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv); /* side effects */
  20631. #else
  20632. DUK_TVAL_SET_UNDEFINED(tv);
  20633. #endif
  20634. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20635. }
  20636. DUK_EXTERNAL void duk_pop_2(duk_hthread *thr) {
  20637. DUK_ASSERT_API_ENTRY(thr);
  20638. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20639. if (DUK_UNLIKELY(thr->valstack_top - 2 < thr->valstack_bottom)) {
  20640. DUK_ERROR_RANGE_INVALID_COUNT(thr);
  20641. DUK_WO_NORETURN(return;);
  20642. }
  20643. duk__pop_2_unsafe_raw(thr);
  20644. }
  20645. DUK_INTERNAL void duk_pop_2_unsafe(duk_hthread *thr) {
  20646. DUK_ASSERT_API_ENTRY(thr);
  20647. duk__pop_2_unsafe_raw(thr);
  20648. }
  20649. DUK_INTERNAL void duk_pop_2_nodecref_unsafe(duk_hthread *thr) {
  20650. DUK_ASSERT_API_ENTRY(thr);
  20651. DUK_ASSERT(thr->valstack_top != thr->valstack_bottom);
  20652. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20653. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) >= (duk_size_t) 2);
  20654. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 1));
  20655. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top - 2));
  20656. thr->valstack_top -= 2;
  20657. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20658. }
  20659. #endif /* !DUK_USE_PREFER_SIZE */
  20660. DUK_EXTERNAL void duk_pop_3(duk_hthread *thr) {
  20661. DUK_ASSERT_API_ENTRY(thr);
  20662. duk_pop_n(thr, 3);
  20663. }
  20664. DUK_INTERNAL void duk_pop_3_unsafe(duk_hthread *thr) {
  20665. DUK_ASSERT_API_ENTRY(thr);
  20666. duk_pop_n_unsafe(thr, 3);
  20667. }
  20668. DUK_INTERNAL void duk_pop_3_nodecref_unsafe(duk_hthread *thr) {
  20669. DUK_ASSERT_API_ENTRY(thr);
  20670. duk_pop_n_nodecref_unsafe(thr, 3);
  20671. }
  20672. /*
  20673. * Pack and unpack (pack value stack entries into an array and vice versa)
  20674. */
  20675. /* XXX: pack index range? array index offset? */
  20676. DUK_INTERNAL void duk_pack(duk_hthread *thr, duk_idx_t count) {
  20677. duk_tval *tv_src;
  20678. duk_tval *tv_dst;
  20679. duk_tval *tv_curr;
  20680. duk_tval *tv_limit;
  20681. duk_idx_t top;
  20682. DUK_ASSERT_API_ENTRY(thr);
  20683. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20684. top = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  20685. DUK_ASSERT(top >= 0);
  20686. if (DUK_UNLIKELY((duk_uidx_t) count > (duk_uidx_t) top)) {
  20687. /* Also handles negative count. */
  20688. DUK_ERROR_RANGE_INVALID_COUNT(thr);
  20689. DUK_WO_NORETURN(return;);
  20690. }
  20691. DUK_ASSERT(count >= 0);
  20692. /* Wrapping is controlled by the check above: value stack top can be
  20693. * at most DUK_USE_VALSTACK_LIMIT which is low enough so that
  20694. * multiplying with sizeof(duk_tval) won't wrap.
  20695. */
  20696. DUK_ASSERT(count >= 0 && count <= (duk_idx_t) DUK_USE_VALSTACK_LIMIT);
  20697. DUK_ASSERT((duk_size_t) count <= DUK_SIZE_MAX / sizeof(duk_tval)); /* no wrapping */
  20698. tv_dst = duk_push_harray_with_size_outptr(thr, (duk_uint32_t) count); /* XXX: uninitialized would be OK */
  20699. DUK_ASSERT(count == 0 || tv_dst != NULL);
  20700. /* Copy value stack values directly to the array part without
  20701. * any refcount updates: net refcount changes are zero.
  20702. */
  20703. tv_src = thr->valstack_top - count - 1;
  20704. duk_memcpy_unsafe((void *) tv_dst, (const void *) tv_src, (size_t) count * sizeof(duk_tval));
  20705. /* Overwrite result array to final value stack location and wipe
  20706. * the rest; no refcount operations needed.
  20707. */
  20708. tv_dst = tv_src; /* when count == 0, same as tv_src (OK) */
  20709. tv_src = thr->valstack_top - 1;
  20710. DUK_TVAL_SET_TVAL(tv_dst, tv_src);
  20711. /* XXX: internal helper to wipe a value stack segment? */
  20712. tv_curr = tv_dst + 1;
  20713. tv_limit = thr->valstack_top;
  20714. while (tv_curr != tv_limit) {
  20715. /* Wipe policy: keep as 'undefined'. */
  20716. DUK_TVAL_SET_UNDEFINED(tv_curr);
  20717. tv_curr++;
  20718. }
  20719. thr->valstack_top = tv_dst + 1;
  20720. }
  20721. DUK_INTERNAL duk_idx_t duk_unpack_array_like(duk_hthread *thr, duk_idx_t idx) {
  20722. duk_tval *tv;
  20723. DUK_ASSERT_API_ENTRY(thr);
  20724. tv = duk_require_tval(thr, idx);
  20725. if (DUK_LIKELY(DUK_TVAL_IS_OBJECT(tv))) {
  20726. duk_hobject *h;
  20727. duk_uint32_t len;
  20728. duk_uint32_t i;
  20729. h = DUK_TVAL_GET_OBJECT(tv);
  20730. DUK_ASSERT(h != NULL);
  20731. DUK_UNREF(h);
  20732. #if defined(DUK_USE_ARRAY_FASTPATH) /* close enough */
  20733. if (DUK_LIKELY(DUK_HOBJECT_IS_ARRAY(h) &&
  20734. ((duk_harray *) h)->length <= DUK_HOBJECT_GET_ASIZE(h))) {
  20735. duk_harray *h_arr;
  20736. duk_tval *tv_src;
  20737. duk_tval *tv_dst;
  20738. h_arr = (duk_harray *) h;
  20739. len = h_arr->length;
  20740. if (DUK_UNLIKELY(len >= 0x80000000UL)) {
  20741. goto fail_over_2g;
  20742. }
  20743. duk_require_stack(thr, (duk_idx_t) len);
  20744. /* The potential allocation in duk_require_stack() may
  20745. * run a finalizer which modifies the argArray so that
  20746. * e.g. becomes sparse. So, we need to recheck that the
  20747. * array didn't change size and that there's still a
  20748. * valid backing array part.
  20749. *
  20750. * XXX: alternatively, could prevent finalizers for the
  20751. * duration.
  20752. */
  20753. if (DUK_UNLIKELY(len != h_arr->length ||
  20754. h_arr->length > DUK_HOBJECT_GET_ASIZE((duk_hobject *) h_arr))) {
  20755. goto skip_fast;
  20756. }
  20757. /* Main fast path: arguments array is almost always
  20758. * an actual array (though it might also be an arguments
  20759. * object).
  20760. */
  20761. DUK_DDD(DUK_DDDPRINT("fast path for %ld elements", (long) h_arr->length));
  20762. tv_src = DUK_HOBJECT_A_GET_BASE(thr->heap, h);
  20763. tv_dst = thr->valstack_top;
  20764. while (len-- > 0) {
  20765. DUK_ASSERT(tv_dst < thr->valstack_end);
  20766. if (DUK_UNLIKELY(DUK_TVAL_IS_UNUSED(tv_src))) {
  20767. /* Gaps are very unlikely. Skip over them,
  20768. * without an ancestor lookup (technically
  20769. * not compliant).
  20770. */
  20771. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv_dst)); /* valstack policy */
  20772. } else {
  20773. DUK_TVAL_SET_TVAL(tv_dst, tv_src);
  20774. DUK_TVAL_INCREF(thr, tv_dst);
  20775. }
  20776. tv_src++;
  20777. tv_dst++;
  20778. }
  20779. DUK_ASSERT(tv_dst <= thr->valstack_end);
  20780. thr->valstack_top = tv_dst;
  20781. return (duk_idx_t) h_arr->length;
  20782. }
  20783. skip_fast:
  20784. #endif /* DUK_USE_ARRAY_FASTPATH */
  20785. /* Slow path: actual lookups. The initial 'length' lookup
  20786. * decides the output length, regardless of side effects that
  20787. * may resize or change the argArray while we read the
  20788. * indices.
  20789. */
  20790. idx = duk_normalize_index(thr, idx);
  20791. duk_get_prop_stridx(thr, idx, DUK_STRIDX_LENGTH);
  20792. len = duk_to_uint32(thr, -1); /* ToUint32() coercion required */
  20793. if (DUK_UNLIKELY(len >= 0x80000000UL)) {
  20794. goto fail_over_2g;
  20795. }
  20796. duk_pop_unsafe(thr);
  20797. DUK_DDD(DUK_DDDPRINT("slow path for %ld elements", (long) len));
  20798. duk_require_stack(thr, (duk_idx_t) len);
  20799. for (i = 0; i < len; i++) {
  20800. duk_get_prop_index(thr, idx, (duk_uarridx_t) i);
  20801. }
  20802. return (duk_idx_t) len;
  20803. } else if (DUK_TVAL_IS_UNDEFINED(tv) || DUK_TVAL_IS_NULL(tv)) {
  20804. return 0;
  20805. }
  20806. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  20807. DUK_WO_NORETURN(return 0;);
  20808. fail_over_2g:
  20809. DUK_ERROR_RANGE_INVALID_LENGTH(thr);
  20810. DUK_WO_NORETURN(return 0;);
  20811. }
  20812. /*
  20813. * Error throwing
  20814. */
  20815. DUK_EXTERNAL void duk_throw_raw(duk_hthread *thr) {
  20816. duk_tval *tv_val;
  20817. DUK_ASSERT_API_ENTRY(thr);
  20818. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  20819. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  20820. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  20821. if (DUK_UNLIKELY(thr->valstack_top == thr->valstack_bottom)) {
  20822. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  20823. DUK_WO_NORETURN(return;);
  20824. }
  20825. /* Errors are augmented when they are created, not when they are
  20826. * thrown or re-thrown. The current error handler, however, runs
  20827. * just before an error is thrown.
  20828. */
  20829. /* Sync so that augmentation sees up-to-date activations, NULL
  20830. * thr->ptr_curr_pc so that it's not used if side effects occur
  20831. * in augmentation or longjmp handling.
  20832. */
  20833. duk_hthread_sync_and_null_currpc(thr);
  20834. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  20835. DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (before throw augment)", (duk_tval *) duk_get_tval(thr, -1)));
  20836. duk_err_augment_error_throw(thr);
  20837. #endif
  20838. DUK_DDD(DUK_DDDPRINT("THROW ERROR (API): %!dT (after throw augment)", (duk_tval *) duk_get_tval(thr, -1)));
  20839. tv_val = DUK_GET_TVAL_NEGIDX(thr, -1);
  20840. duk_err_setup_ljstate1(thr, DUK_LJ_TYPE_THROW, tv_val);
  20841. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  20842. duk_err_check_debugger_integration(thr);
  20843. #endif
  20844. /* thr->heap->lj.jmpbuf_ptr is checked by duk_err_longjmp() so we don't
  20845. * need to check that here. If the value is NULL, a fatal error occurs
  20846. * because we can't return.
  20847. */
  20848. duk_err_longjmp(thr);
  20849. DUK_UNREACHABLE();
  20850. }
  20851. DUK_EXTERNAL void duk_fatal_raw(duk_hthread *thr, const char *err_msg) {
  20852. DUK_ASSERT_API_ENTRY(thr);
  20853. DUK_ASSERT(thr != NULL);
  20854. DUK_ASSERT(thr->heap != NULL);
  20855. DUK_ASSERT(thr->heap->fatal_func != NULL);
  20856. DUK_D(DUK_DPRINT("fatal error occurred: %s", err_msg ? err_msg : "NULL"));
  20857. /* fatal_func should be noreturn, but noreturn declarations on function
  20858. * pointers has a very spotty support apparently so it's not currently
  20859. * done.
  20860. */
  20861. thr->heap->fatal_func(thr->heap->heap_udata, err_msg);
  20862. /* If the fatal handler returns, all bets are off. It'd be nice to
  20863. * print something here but since we don't want to depend on stdio,
  20864. * there's no way to do so portably.
  20865. */
  20866. DUK_D(DUK_DPRINT("fatal error handler returned, all bets are off!"));
  20867. for (;;) {
  20868. /* loop forever, don't return (function marked noreturn) */
  20869. }
  20870. }
  20871. DUK_EXTERNAL void duk_error_va_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, va_list ap) {
  20872. DUK_ASSERT_API_ENTRY(thr);
  20873. duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
  20874. (void) duk_throw(thr);
  20875. DUK_WO_NORETURN(return;);
  20876. }
  20877. DUK_EXTERNAL void duk_error_raw(duk_hthread *thr, duk_errcode_t err_code, const char *filename, duk_int_t line, const char *fmt, ...) {
  20878. va_list ap;
  20879. DUK_ASSERT_API_ENTRY(thr);
  20880. va_start(ap, fmt);
  20881. duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
  20882. va_end(ap);
  20883. (void) duk_throw(thr);
  20884. DUK_WO_NORETURN(return;);
  20885. }
  20886. #if !defined(DUK_USE_VARIADIC_MACROS)
  20887. DUK_NORETURN(DUK_LOCAL_DECL void duk__throw_error_from_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, va_list ap));
  20888. DUK_LOCAL void duk__throw_error_from_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, va_list ap) {
  20889. const char *filename;
  20890. duk_int_t line;
  20891. DUK_ASSERT_CTX_VALID(thr);
  20892. filename = duk_api_global_filename;
  20893. line = duk_api_global_line;
  20894. duk_api_global_filename = NULL;
  20895. duk_api_global_line = 0;
  20896. duk_push_error_object_va_raw(thr, err_code, filename, line, fmt, ap);
  20897. (void) duk_throw(thr);
  20898. DUK_WO_NORETURN(return;);
  20899. }
  20900. #define DUK__ERROR_STASH_SHARED(code) do { \
  20901. va_list ap; \
  20902. va_start(ap, fmt); \
  20903. duk__throw_error_from_stash(thr, (code), fmt, ap); \
  20904. va_end(ap); \
  20905. DUK_WO_NORETURN(return 0;); \
  20906. } while (0)
  20907. DUK_EXTERNAL duk_ret_t duk_error_stash(duk_hthread *thr, duk_errcode_t err_code, const char *fmt, ...) {
  20908. DUK_ASSERT_API_ENTRY(thr);
  20909. DUK__ERROR_STASH_SHARED(err_code);
  20910. }
  20911. DUK_EXTERNAL duk_ret_t duk_generic_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20912. DUK_ASSERT_API_ENTRY(thr);
  20913. DUK__ERROR_STASH_SHARED(DUK_ERR_ERROR);
  20914. }
  20915. DUK_EXTERNAL duk_ret_t duk_eval_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20916. DUK_ASSERT_API_ENTRY(thr);
  20917. DUK__ERROR_STASH_SHARED(DUK_ERR_EVAL_ERROR);
  20918. }
  20919. DUK_EXTERNAL duk_ret_t duk_range_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20920. DUK_ASSERT_API_ENTRY(thr);
  20921. DUK__ERROR_STASH_SHARED(DUK_ERR_RANGE_ERROR);
  20922. }
  20923. DUK_EXTERNAL duk_ret_t duk_reference_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20924. DUK_ASSERT_API_ENTRY(thr);
  20925. DUK__ERROR_STASH_SHARED(DUK_ERR_REFERENCE_ERROR);
  20926. }
  20927. DUK_EXTERNAL duk_ret_t duk_syntax_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20928. DUK_ASSERT_API_ENTRY(thr);
  20929. DUK__ERROR_STASH_SHARED(DUK_ERR_SYNTAX_ERROR);
  20930. }
  20931. DUK_EXTERNAL duk_ret_t duk_type_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20932. DUK_ASSERT_API_ENTRY(thr);
  20933. DUK__ERROR_STASH_SHARED(DUK_ERR_TYPE_ERROR);
  20934. }
  20935. DUK_EXTERNAL duk_ret_t duk_uri_error_stash(duk_hthread *thr, const char *fmt, ...) {
  20936. DUK_ASSERT_API_ENTRY(thr);
  20937. DUK__ERROR_STASH_SHARED(DUK_ERR_URI_ERROR);
  20938. }
  20939. #endif /* DUK_USE_VARIADIC_MACROS */
  20940. /*
  20941. * Comparison
  20942. */
  20943. DUK_EXTERNAL duk_bool_t duk_equals(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
  20944. duk_tval *tv1, *tv2;
  20945. DUK_ASSERT_API_ENTRY(thr);
  20946. tv1 = duk_get_tval(thr, idx1);
  20947. tv2 = duk_get_tval(thr, idx2);
  20948. if ((tv1 == NULL) || (tv2 == NULL)) {
  20949. return 0;
  20950. }
  20951. /* Coercion may be needed, the helper handles that by pushing the
  20952. * tagged values to the stack.
  20953. */
  20954. return duk_js_equals(thr, tv1, tv2);
  20955. }
  20956. DUK_EXTERNAL duk_bool_t duk_strict_equals(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
  20957. duk_tval *tv1, *tv2;
  20958. DUK_ASSERT_API_ENTRY(thr);
  20959. tv1 = duk_get_tval(thr, idx1);
  20960. tv2 = duk_get_tval(thr, idx2);
  20961. if ((tv1 == NULL) || (tv2 == NULL)) {
  20962. return 0;
  20963. }
  20964. /* No coercions or other side effects, so safe */
  20965. return duk_js_strict_equals(tv1, tv2);
  20966. }
  20967. DUK_EXTERNAL duk_bool_t duk_samevalue(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
  20968. duk_tval *tv1, *tv2;
  20969. DUK_ASSERT_API_ENTRY(thr);
  20970. tv1 = duk_get_tval(thr, idx1);
  20971. tv2 = duk_get_tval(thr, idx2);
  20972. if ((tv1 == NULL) || (tv2 == NULL)) {
  20973. return 0;
  20974. }
  20975. /* No coercions or other side effects, so safe */
  20976. return duk_js_samevalue(tv1, tv2);
  20977. }
  20978. /*
  20979. * instanceof
  20980. */
  20981. DUK_EXTERNAL duk_bool_t duk_instanceof(duk_hthread *thr, duk_idx_t idx1, duk_idx_t idx2) {
  20982. duk_tval *tv1, *tv2;
  20983. DUK_ASSERT_API_ENTRY(thr);
  20984. /* Index validation is strict, which differs from duk_equals().
  20985. * The strict behavior mimics how instanceof itself works, e.g.
  20986. * it is a TypeError if rval is not a -callable- object. It would
  20987. * be somewhat inconsistent if rval would be allowed to be
  20988. * non-existent without a TypeError.
  20989. */
  20990. tv1 = duk_require_tval(thr, idx1);
  20991. DUK_ASSERT(tv1 != NULL);
  20992. tv2 = duk_require_tval(thr, idx2);
  20993. DUK_ASSERT(tv2 != NULL);
  20994. return duk_js_instanceof(thr, tv1, tv2);
  20995. }
  20996. /*
  20997. * Lightfunc
  20998. */
  20999. DUK_INTERNAL void duk_push_lightfunc_name_raw(duk_hthread *thr, duk_c_function func, duk_small_uint_t lf_flags) {
  21000. /* Lightfunc name, includes Duktape/C native function pointer, which
  21001. * can often be used to locate the function from a symbol table.
  21002. * The name also includes the 16-bit duk_tval flags field because it
  21003. * includes the magic value. Because a single native function often
  21004. * provides different functionality depending on the magic value, it
  21005. * seems reasonably to include it in the name.
  21006. *
  21007. * On the other hand, a complicated name increases string table
  21008. * pressure in low memory environments (but only when function name
  21009. * is accessed).
  21010. */
  21011. DUK_ASSERT_API_ENTRY(thr);
  21012. duk_push_literal(thr, "light_");
  21013. duk_push_string_funcptr(thr, (duk_uint8_t *) &func, sizeof(func));
  21014. duk_push_sprintf(thr, "_%04x", (unsigned int) lf_flags);
  21015. duk_concat(thr, 3);
  21016. }
  21017. DUK_INTERNAL void duk_push_lightfunc_name(duk_hthread *thr, duk_tval *tv) {
  21018. duk_c_function func;
  21019. duk_small_uint_t lf_flags;
  21020. DUK_ASSERT_API_ENTRY(thr);
  21021. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
  21022. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  21023. duk_push_lightfunc_name_raw(thr, func, lf_flags);
  21024. }
  21025. DUK_INTERNAL void duk_push_lightfunc_tostring(duk_hthread *thr, duk_tval *tv) {
  21026. duk_c_function func;
  21027. duk_small_uint_t lf_flags;
  21028. DUK_ASSERT_API_ENTRY(thr);
  21029. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv));
  21030. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags); /* read before 'tv' potentially invalidated */
  21031. duk_push_literal(thr, "function ");
  21032. duk_push_lightfunc_name_raw(thr, func, lf_flags);
  21033. duk_push_literal(thr, "() { [lightfunc code] }");
  21034. duk_concat(thr, 3);
  21035. }
  21036. /*
  21037. * Function pointers
  21038. *
  21039. * Printing function pointers is non-portable, so we do that by hex printing
  21040. * bytes from memory.
  21041. */
  21042. DUK_INTERNAL void duk_push_string_funcptr(duk_hthread *thr, duk_uint8_t *ptr, duk_size_t sz) {
  21043. duk_uint8_t buf[32 * 2];
  21044. duk_uint8_t *p, *q;
  21045. duk_small_uint_t i;
  21046. duk_small_uint_t t;
  21047. DUK_ASSERT_API_ENTRY(thr);
  21048. DUK_ASSERT(sz <= 32); /* sanity limit for function pointer size */
  21049. p = buf;
  21050. #if defined(DUK_USE_INTEGER_LE)
  21051. q = ptr + sz;
  21052. #else
  21053. q = ptr;
  21054. #endif
  21055. for (i = 0; i < sz; i++) {
  21056. #if defined(DUK_USE_INTEGER_LE)
  21057. t = *(--q);
  21058. #else
  21059. t = *(q++);
  21060. #endif
  21061. *p++ = duk_lc_digits[t >> 4];
  21062. *p++ = duk_lc_digits[t & 0x0f];
  21063. }
  21064. duk_push_lstring(thr, (const char *) buf, sz * 2);
  21065. }
  21066. /*
  21067. * Push readable string summarizing duk_tval. The operation is side effect
  21068. * free and will only throw from internal errors (e.g. out of memory).
  21069. * This is used by e.g. property access code to summarize a key/base safely,
  21070. * and is not intended to be fast (but small and safe).
  21071. */
  21072. /* String limits for summary strings. */
  21073. #define DUK__READABLE_SUMMARY_MAXCHARS 96 /* maximum supported by helper */
  21074. #define DUK__READABLE_STRING_MAXCHARS 32 /* for strings/symbols */
  21075. #define DUK__READABLE_ERRMSG_MAXCHARS 96 /* for error messages */
  21076. /* String sanitizer which escapes ASCII control characters and a few other
  21077. * ASCII characters, passes Unicode as is, and replaces invalid UTF-8 with
  21078. * question marks. No errors are thrown for any input string, except in out
  21079. * of memory situations.
  21080. */
  21081. DUK_LOCAL void duk__push_hstring_readable_unicode(duk_hthread *thr, duk_hstring *h_input, duk_small_uint_t maxchars) {
  21082. const duk_uint8_t *p, *p_start, *p_end;
  21083. duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH * DUK__READABLE_SUMMARY_MAXCHARS +
  21084. 2 /*quotes*/ + 3 /*periods*/];
  21085. duk_uint8_t *q;
  21086. duk_ucodepoint_t cp;
  21087. duk_small_uint_t nchars;
  21088. DUK_ASSERT_CTX_VALID(thr);
  21089. DUK_ASSERT(h_input != NULL);
  21090. DUK_ASSERT(maxchars <= DUK__READABLE_SUMMARY_MAXCHARS);
  21091. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  21092. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  21093. p = p_start;
  21094. q = buf;
  21095. nchars = 0;
  21096. *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
  21097. for (;;) {
  21098. if (p >= p_end) {
  21099. break;
  21100. }
  21101. if (nchars == maxchars) {
  21102. *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
  21103. *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
  21104. *q++ = (duk_uint8_t) DUK_ASC_PERIOD;
  21105. break;
  21106. }
  21107. if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
  21108. if (cp < 0x20 || cp == 0x7f || cp == DUK_ASC_SINGLEQUOTE || cp == DUK_ASC_BACKSLASH) {
  21109. DUK_ASSERT(DUK_UNICODE_MAX_XUTF8_LENGTH >= 4); /* estimate is valid */
  21110. DUK_ASSERT((cp >> 4) <= 0x0f);
  21111. *q++ = (duk_uint8_t) DUK_ASC_BACKSLASH;
  21112. *q++ = (duk_uint8_t) DUK_ASC_LC_X;
  21113. *q++ = (duk_uint8_t) duk_lc_digits[cp >> 4];
  21114. *q++ = (duk_uint8_t) duk_lc_digits[cp & 0x0f];
  21115. } else {
  21116. q += duk_unicode_encode_xutf8(cp, q);
  21117. }
  21118. } else {
  21119. p++; /* advance manually */
  21120. *q++ = (duk_uint8_t) DUK_ASC_QUESTION;
  21121. }
  21122. nchars++;
  21123. }
  21124. *q++ = (duk_uint8_t) DUK_ASC_SINGLEQUOTE;
  21125. duk_push_lstring(thr, (const char *) buf, (duk_size_t) (q - buf));
  21126. }
  21127. DUK_LOCAL const char *duk__push_string_tval_readable(duk_hthread *thr, duk_tval *tv, duk_bool_t error_aware) {
  21128. DUK_ASSERT_CTX_VALID(thr);
  21129. /* 'tv' may be NULL */
  21130. if (tv == NULL) {
  21131. duk_push_literal(thr, "none");
  21132. } else {
  21133. switch (DUK_TVAL_GET_TAG(tv)) {
  21134. case DUK_TAG_STRING: {
  21135. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  21136. if (DUK_HSTRING_HAS_SYMBOL(h)) {
  21137. /* XXX: string summary produces question marks
  21138. * so this is not very ideal.
  21139. */
  21140. duk_push_literal(thr, "[Symbol ");
  21141. duk_push_string(thr, duk__get_symbol_type_string(h));
  21142. duk_push_literal(thr, " ");
  21143. duk__push_hstring_readable_unicode(thr, h, DUK__READABLE_STRING_MAXCHARS);
  21144. duk_push_literal(thr, "]");
  21145. duk_concat(thr, 5);
  21146. break;
  21147. }
  21148. duk__push_hstring_readable_unicode(thr, h, DUK__READABLE_STRING_MAXCHARS);
  21149. break;
  21150. }
  21151. case DUK_TAG_OBJECT: {
  21152. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  21153. DUK_ASSERT(h != NULL);
  21154. if (error_aware &&
  21155. duk_hobject_prototype_chain_contains(thr, h, thr->builtins[DUK_BIDX_ERROR_PROTOTYPE], 1 /*ignore_loop*/)) {
  21156. /* Get error message in a side effect free way if
  21157. * possible; if not, summarize as a generic object.
  21158. * Error message currently gets quoted.
  21159. */
  21160. /* XXX: better internal getprop call; get without side effects
  21161. * but traverse inheritance chain.
  21162. */
  21163. duk_tval *tv_msg;
  21164. tv_msg = duk_hobject_find_existing_entry_tval_ptr(thr->heap, h, DUK_HTHREAD_STRING_MESSAGE(thr));
  21165. if (tv_msg != NULL && DUK_TVAL_IS_STRING(tv_msg)) {
  21166. /* It's critical to avoid recursion so
  21167. * only summarize a string .message.
  21168. */
  21169. duk__push_hstring_readable_unicode(thr, DUK_TVAL_GET_STRING(tv_msg), DUK__READABLE_ERRMSG_MAXCHARS);
  21170. break;
  21171. }
  21172. }
  21173. duk_push_class_string_tval(thr, tv, 1 /*avoid_side_effects*/);
  21174. break;
  21175. }
  21176. case DUK_TAG_BUFFER: {
  21177. /* While plain buffers mimic Uint8Arrays, they summarize differently.
  21178. * This is useful so that the summarized string accurately reflects the
  21179. * internal type which may matter for figuring out bugs etc.
  21180. */
  21181. /* XXX: Hex encoded, length limited buffer summary here? */
  21182. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv);
  21183. DUK_ASSERT(h != NULL);
  21184. duk_push_sprintf(thr, "[buffer:%ld]", (long) DUK_HBUFFER_GET_SIZE(h));
  21185. break;
  21186. }
  21187. case DUK_TAG_POINTER: {
  21188. /* Surround with parentheses like in JX, ensures NULL pointer
  21189. * is distinguishable from null value ("(null)" vs "null").
  21190. */
  21191. duk_push_tval(thr, tv);
  21192. duk_push_sprintf(thr, "(%s)", duk_to_string(thr, -1));
  21193. duk_remove_m2(thr);
  21194. break;
  21195. }
  21196. default: {
  21197. duk_push_tval(thr, tv);
  21198. break;
  21199. }
  21200. }
  21201. }
  21202. return duk_to_string(thr, -1);
  21203. }
  21204. DUK_INTERNAL const char *duk_push_string_tval_readable(duk_hthread *thr, duk_tval *tv) {
  21205. DUK_ASSERT_API_ENTRY(thr);
  21206. return duk__push_string_tval_readable(thr, tv, 0 /*error_aware*/);
  21207. }
  21208. DUK_INTERNAL const char *duk_push_string_readable(duk_hthread *thr, duk_idx_t idx) {
  21209. DUK_ASSERT_API_ENTRY(thr);
  21210. return duk_push_string_tval_readable(thr, duk_get_tval(thr, idx));
  21211. }
  21212. DUK_INTERNAL const char *duk_push_string_tval_readable_error(duk_hthread *thr, duk_tval *tv) {
  21213. DUK_ASSERT_API_ENTRY(thr);
  21214. return duk__push_string_tval_readable(thr, tv, 1 /*error_aware*/);
  21215. }
  21216. DUK_INTERNAL void duk_push_symbol_descriptive_string(duk_hthread *thr, duk_hstring *h) {
  21217. const duk_uint8_t *p;
  21218. const duk_uint8_t *p_end;
  21219. const duk_uint8_t *q;
  21220. DUK_ASSERT_API_ENTRY(thr);
  21221. /* .toString() */
  21222. duk_push_literal(thr, "Symbol(");
  21223. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  21224. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  21225. DUK_ASSERT(p[0] == 0xff || (p[0] & 0xc0) == 0x80);
  21226. p++;
  21227. for (q = p; q < p_end; q++) {
  21228. if (*q == 0xffU) {
  21229. /* Terminate either at end-of-string (but NUL MUST
  21230. * be accepted without terminating description) or
  21231. * 0xFF, which is used to mark start of unique trailer
  21232. * (and cannot occur in CESU-8 / extended UTF-8).
  21233. */
  21234. break;
  21235. }
  21236. }
  21237. duk_push_lstring(thr, (const char *) p, (duk_size_t) (q - p));
  21238. duk_push_literal(thr, ")");
  21239. duk_concat(thr, 3);
  21240. }
  21241. /*
  21242. * Functions
  21243. */
  21244. #if 0 /* not used yet */
  21245. DUK_INTERNAL void duk_push_hnatfunc_name(duk_hthread *thr, duk_hnatfunc *h) {
  21246. duk_c_function func;
  21247. DUK_ASSERT_API_ENTRY(thr);
  21248. DUK_ASSERT(h != NULL);
  21249. DUK_ASSERT(DUK_HOBJECT_IS_NATFUNC((duk_hobject *) h));
  21250. duk_push_sprintf(thr, "native_");
  21251. func = h->func;
  21252. duk_push_string_funcptr(thr, (duk_uint8_t *) &func, sizeof(func));
  21253. duk_push_sprintf(thr, "_%04x_%04x",
  21254. (unsigned int) (duk_uint16_t) h->nargs,
  21255. (unsigned int) (duk_uint16_t) h->magic);
  21256. duk_concat(thr, 3);
  21257. }
  21258. #endif
  21259. /*
  21260. * duk_tval slice copy
  21261. */
  21262. DUK_INTERNAL void duk_copy_tvals_incref(duk_hthread *thr, duk_tval *tv_dst, duk_tval *tv_src, duk_size_t count) {
  21263. duk_tval *tv;
  21264. DUK_ASSERT_API_ENTRY(thr);
  21265. DUK_UNREF(thr);
  21266. DUK_ASSERT(count * sizeof(duk_tval) >= count); /* no wrap */
  21267. duk_memcpy_unsafe((void *) tv_dst, (const void *) tv_src, count * sizeof(duk_tval));
  21268. tv = tv_dst;
  21269. while (count-- > 0) {
  21270. DUK_TVAL_INCREF(thr, tv);
  21271. tv++;
  21272. }
  21273. }
  21274. /* automatic undefs */
  21275. #undef DUK__ASSERT_SPACE
  21276. #undef DUK__CHECK_SPACE
  21277. #undef DUK__ERROR_STASH_SHARED
  21278. #undef DUK__PACK_ARGS
  21279. #undef DUK__READABLE_ERRMSG_MAXCHARS
  21280. #undef DUK__READABLE_STRING_MAXCHARS
  21281. #undef DUK__READABLE_SUMMARY_MAXCHARS
  21282. #line 1 "duk_api_string.c"
  21283. /*
  21284. * String manipulation
  21285. */
  21286. /* #include duk_internal.h -> already included */
  21287. DUK_LOCAL void duk__concat_and_join_helper(duk_hthread *thr, duk_idx_t count_in, duk_bool_t is_join) {
  21288. duk_uint_t count;
  21289. duk_uint_t i;
  21290. duk_size_t idx;
  21291. duk_size_t len;
  21292. duk_hstring *h;
  21293. duk_uint8_t *buf;
  21294. DUK_ASSERT_CTX_VALID(thr);
  21295. if (DUK_UNLIKELY(count_in <= 0)) {
  21296. if (count_in < 0) {
  21297. DUK_ERROR_RANGE_INVALID_COUNT(thr);
  21298. DUK_WO_NORETURN(return;);
  21299. }
  21300. DUK_ASSERT(count_in == 0);
  21301. duk_push_hstring_empty(thr);
  21302. return;
  21303. }
  21304. count = (duk_uint_t) count_in;
  21305. if (is_join) {
  21306. duk_size_t t1, t2, limit;
  21307. h = duk_to_hstring(thr, -((duk_idx_t) count) - 1);
  21308. DUK_ASSERT(h != NULL);
  21309. /* A bit tricky overflow test, see doc/code-issues.rst. */
  21310. t1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  21311. t2 = (duk_size_t) (count - 1);
  21312. limit = (duk_size_t) DUK_HSTRING_MAX_BYTELEN;
  21313. if (DUK_UNLIKELY(t2 != 0 && t1 > limit / t2)) {
  21314. /* Combined size of separators already overflows. */
  21315. goto error_overflow;
  21316. }
  21317. len = (duk_size_t) (t1 * t2);
  21318. } else {
  21319. len = (duk_size_t) 0;
  21320. }
  21321. for (i = count; i >= 1; i--) {
  21322. duk_size_t new_len;
  21323. h = duk_to_hstring(thr, -((duk_idx_t) i));
  21324. new_len = len + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  21325. /* Impose a string maximum length, need to handle overflow
  21326. * correctly.
  21327. */
  21328. if (new_len < len || /* wrapped */
  21329. new_len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN) {
  21330. goto error_overflow;
  21331. }
  21332. len = new_len;
  21333. }
  21334. DUK_DDD(DUK_DDDPRINT("join/concat %lu strings, total length %lu bytes",
  21335. (unsigned long) count, (unsigned long) len));
  21336. /* Use stack allocated buffer to ensure reachability in errors
  21337. * (e.g. intern error).
  21338. */
  21339. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, len);
  21340. DUK_ASSERT(buf != NULL);
  21341. /* [ ... (sep) str1 str2 ... strN buf ] */
  21342. idx = 0;
  21343. for (i = count; i >= 1; i--) {
  21344. if (is_join && i != count) {
  21345. h = duk_require_hstring(thr, -((duk_idx_t) count) - 2); /* extra -1 for buffer */
  21346. duk_memcpy(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  21347. idx += DUK_HSTRING_GET_BYTELEN(h);
  21348. }
  21349. h = duk_require_hstring(thr, -((duk_idx_t) i) - 1); /* extra -1 for buffer */
  21350. duk_memcpy(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  21351. idx += DUK_HSTRING_GET_BYTELEN(h);
  21352. }
  21353. DUK_ASSERT(idx == len);
  21354. /* [ ... (sep) str1 str2 ... strN buf ] */
  21355. /* Get rid of the strings early to minimize memory use before intern. */
  21356. if (is_join) {
  21357. duk_replace(thr, -((duk_idx_t) count) - 2); /* overwrite sep */
  21358. duk_pop_n(thr, (duk_idx_t) count);
  21359. } else {
  21360. duk_replace(thr, -((duk_idx_t) count) - 1); /* overwrite str1 */
  21361. duk_pop_n(thr, (duk_idx_t) (count - 1));
  21362. }
  21363. /* [ ... buf ] */
  21364. (void) duk_buffer_to_string(thr, -1); /* Safe if inputs are safe. */
  21365. /* [ ... res ] */
  21366. return;
  21367. error_overflow:
  21368. DUK_ERROR_RANGE(thr, DUK_STR_RESULT_TOO_LONG);
  21369. DUK_WO_NORETURN(return;);
  21370. }
  21371. DUK_EXTERNAL void duk_concat(duk_hthread *thr, duk_idx_t count) {
  21372. DUK_ASSERT_API_ENTRY(thr);
  21373. duk__concat_and_join_helper(thr, count, 0 /*is_join*/);
  21374. }
  21375. #if defined(DUK_USE_PREFER_SIZE)
  21376. DUK_INTERNAL void duk_concat_2(duk_hthread *thr) {
  21377. DUK_ASSERT_API_ENTRY(thr);
  21378. duk_concat(thr, 2);
  21379. }
  21380. #else /* DUK_USE_PREFER_SIZE */
  21381. DUK_INTERNAL void duk_concat_2(duk_hthread *thr) {
  21382. duk_hstring *h1;
  21383. duk_hstring *h2;
  21384. duk_uint8_t *buf;
  21385. duk_size_t len1;
  21386. duk_size_t len2;
  21387. duk_size_t len;
  21388. DUK_ASSERT_API_ENTRY(thr);
  21389. DUK_ASSERT(duk_get_top(thr) >= 2); /* Trusted caller. */
  21390. h1 = duk_to_hstring(thr, -2);
  21391. h2 = duk_to_hstring(thr, -1);
  21392. len1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1);
  21393. len2 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2);
  21394. len = len1 + len2;
  21395. if (DUK_UNLIKELY(len < len1 || /* wrapped */
  21396. len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN)) {
  21397. goto error_overflow;
  21398. }
  21399. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, len);
  21400. DUK_ASSERT(buf != NULL);
  21401. duk_memcpy((void *) buf, (const void *) DUK_HSTRING_GET_DATA(h1), (size_t) len1);
  21402. duk_memcpy((void *) (buf + len1), (const void *) DUK_HSTRING_GET_DATA(h2), (size_t) len2);
  21403. (void) duk_buffer_to_string(thr, -1); /* Safe if inputs are safe. */
  21404. /* [ ... str1 str2 buf ] */
  21405. duk_replace(thr, -3);
  21406. duk_pop_unsafe(thr);
  21407. return;
  21408. error_overflow:
  21409. DUK_ERROR_RANGE(thr, DUK_STR_RESULT_TOO_LONG);
  21410. DUK_WO_NORETURN(return;);
  21411. }
  21412. #endif /* DUK_USE_PREFER_SIZE */
  21413. DUK_EXTERNAL void duk_join(duk_hthread *thr, duk_idx_t count) {
  21414. DUK_ASSERT_API_ENTRY(thr);
  21415. duk__concat_and_join_helper(thr, count, 1 /*is_join*/);
  21416. }
  21417. /* XXX: could map/decode be unified with duk_unicode_support.c code?
  21418. * Case conversion needs also the character surroundings though.
  21419. */
  21420. DUK_EXTERNAL void duk_decode_string(duk_hthread *thr, duk_idx_t idx, duk_decode_char_function callback, void *udata) {
  21421. duk_hstring *h_input;
  21422. const duk_uint8_t *p, *p_start, *p_end;
  21423. duk_codepoint_t cp;
  21424. DUK_ASSERT_API_ENTRY(thr);
  21425. h_input = duk_require_hstring(thr, idx); /* Accept symbols. */
  21426. DUK_ASSERT(h_input != NULL);
  21427. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  21428. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  21429. p = p_start;
  21430. for (;;) {
  21431. if (p >= p_end) {
  21432. break;
  21433. }
  21434. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  21435. callback(udata, cp);
  21436. }
  21437. }
  21438. DUK_EXTERNAL void duk_map_string(duk_hthread *thr, duk_idx_t idx, duk_map_char_function callback, void *udata) {
  21439. duk_hstring *h_input;
  21440. duk_bufwriter_ctx bw_alloc;
  21441. duk_bufwriter_ctx *bw;
  21442. const duk_uint8_t *p, *p_start, *p_end;
  21443. duk_codepoint_t cp;
  21444. DUK_ASSERT_API_ENTRY(thr);
  21445. idx = duk_normalize_index(thr, idx);
  21446. h_input = duk_require_hstring(thr, idx); /* Accept symbols. */
  21447. DUK_ASSERT(h_input != NULL);
  21448. bw = &bw_alloc;
  21449. DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); /* Reasonable output estimate. */
  21450. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  21451. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  21452. p = p_start;
  21453. for (;;) {
  21454. /* XXX: could write output in chunks with fewer ensure calls,
  21455. * but relative benefit would be small here.
  21456. */
  21457. if (p >= p_end) {
  21458. break;
  21459. }
  21460. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
  21461. cp = callback(udata, cp);
  21462. DUK_BW_WRITE_ENSURE_XUTF8(thr, bw, cp);
  21463. }
  21464. DUK_BW_COMPACT(thr, bw);
  21465. (void) duk_buffer_to_string(thr, -1); /* Safe, extended UTF-8 encoded. */
  21466. duk_replace(thr, idx);
  21467. }
  21468. DUK_EXTERNAL void duk_substring(duk_hthread *thr, duk_idx_t idx, duk_size_t start_offset, duk_size_t end_offset) {
  21469. duk_hstring *h;
  21470. duk_hstring *res;
  21471. duk_size_t start_byte_offset;
  21472. duk_size_t end_byte_offset;
  21473. duk_size_t charlen;
  21474. DUK_ASSERT_API_ENTRY(thr);
  21475. idx = duk_require_normalize_index(thr, idx); /* Accept symbols. */
  21476. h = duk_require_hstring(thr, idx);
  21477. DUK_ASSERT(h != NULL);
  21478. charlen = DUK_HSTRING_GET_CHARLEN(h);
  21479. if (end_offset >= charlen) {
  21480. end_offset = charlen;
  21481. }
  21482. if (start_offset > end_offset) {
  21483. start_offset = end_offset;
  21484. }
  21485. DUK_ASSERT_DISABLE(start_offset >= 0);
  21486. DUK_ASSERT(start_offset <= end_offset && start_offset <= DUK_HSTRING_GET_CHARLEN(h));
  21487. DUK_ASSERT_DISABLE(end_offset >= 0);
  21488. DUK_ASSERT(end_offset >= start_offset && end_offset <= DUK_HSTRING_GET_CHARLEN(h));
  21489. /* Guaranteed by string limits. */
  21490. DUK_ASSERT(start_offset <= DUK_UINT32_MAX);
  21491. DUK_ASSERT(end_offset <= DUK_UINT32_MAX);
  21492. start_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) start_offset);
  21493. end_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) end_offset);
  21494. DUK_ASSERT(end_byte_offset >= start_byte_offset);
  21495. DUK_ASSERT(end_byte_offset - start_byte_offset <= DUK_UINT32_MAX); /* Guaranteed by string limits. */
  21496. /* No size check is necessary. */
  21497. res = duk_heap_strtable_intern_checked(thr,
  21498. DUK_HSTRING_GET_DATA(h) + start_byte_offset,
  21499. (duk_uint32_t) (end_byte_offset - start_byte_offset));
  21500. duk_push_hstring(thr, res);
  21501. duk_replace(thr, idx);
  21502. }
  21503. /* XXX: this is quite clunky. Add Unicode helpers to scan backwards and
  21504. * forwards with a callback to process codepoints?
  21505. */
  21506. DUK_EXTERNAL void duk_trim(duk_hthread *thr, duk_idx_t idx) {
  21507. duk_hstring *h;
  21508. const duk_uint8_t *p, *p_start, *p_end, *p_tmp1, *p_tmp2; /* pointers for scanning */
  21509. const duk_uint8_t *q_start, *q_end; /* start (incl) and end (excl) of trimmed part */
  21510. duk_codepoint_t cp;
  21511. DUK_ASSERT_API_ENTRY(thr);
  21512. idx = duk_require_normalize_index(thr, idx); /* Accept symbols. */
  21513. h = duk_require_hstring(thr, idx);
  21514. DUK_ASSERT(h != NULL);
  21515. p_start = DUK_HSTRING_GET_DATA(h);
  21516. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
  21517. p = p_start;
  21518. while (p < p_end) {
  21519. p_tmp1 = p;
  21520. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp1, p_start, p_end);
  21521. if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
  21522. break;
  21523. }
  21524. p = p_tmp1;
  21525. }
  21526. q_start = p;
  21527. if (p == p_end) {
  21528. /* Entire string is whitespace. */
  21529. q_end = p;
  21530. goto scan_done;
  21531. }
  21532. p = p_end;
  21533. while (p > p_start) {
  21534. p_tmp1 = p;
  21535. while (p > p_start) {
  21536. p--;
  21537. if (((*p) & 0xc0) != 0x80) {
  21538. break;
  21539. }
  21540. }
  21541. p_tmp2 = p;
  21542. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp2, p_start, p_end);
  21543. if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
  21544. p = p_tmp1;
  21545. break;
  21546. }
  21547. }
  21548. q_end = p;
  21549. scan_done:
  21550. /* This may happen when forward and backward scanning disagree
  21551. * (possible for non-extended-UTF-8 strings).
  21552. */
  21553. if (q_end < q_start) {
  21554. q_end = q_start;
  21555. }
  21556. DUK_ASSERT(q_start >= p_start && q_start <= p_end);
  21557. DUK_ASSERT(q_end >= p_start && q_end <= p_end);
  21558. DUK_ASSERT(q_end >= q_start);
  21559. DUK_DDD(DUK_DDDPRINT("trim: p_start=%p, p_end=%p, q_start=%p, q_end=%p",
  21560. (const void *) p_start, (const void *) p_end,
  21561. (const void *) q_start, (const void *) q_end));
  21562. if (q_start == p_start && q_end == p_end) {
  21563. DUK_DDD(DUK_DDDPRINT("nothing was trimmed: avoid interning (hashing etc)"));
  21564. return;
  21565. }
  21566. duk_push_lstring(thr, (const char *) q_start, (duk_size_t) (q_end - q_start));
  21567. duk_replace(thr, idx);
  21568. }
  21569. DUK_EXTERNAL duk_codepoint_t duk_char_code_at(duk_hthread *thr, duk_idx_t idx, duk_size_t char_offset) {
  21570. duk_hstring *h;
  21571. duk_ucodepoint_t cp;
  21572. DUK_ASSERT_API_ENTRY(thr);
  21573. /* XXX: Share code with String.prototype.charCodeAt? Main difference
  21574. * is handling of clamped offsets.
  21575. */
  21576. h = duk_require_hstring(thr, idx); /* Accept symbols. */
  21577. DUK_ASSERT(h != NULL);
  21578. DUK_ASSERT_DISABLE(char_offset >= 0); /* Always true, arg is unsigned. */
  21579. if (char_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
  21580. return 0;
  21581. }
  21582. DUK_ASSERT(char_offset <= DUK_UINT_MAX); /* Guaranteed by string limits. */
  21583. cp = duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) char_offset, 0 /*surrogate_aware*/);
  21584. return (duk_codepoint_t) cp;
  21585. }
  21586. #line 1 "duk_api_time.c"
  21587. /*
  21588. * Date/time.
  21589. */
  21590. /* #include duk_internal.h -> already included */
  21591. DUK_INTERNAL duk_double_t duk_time_get_ecmascript_time(duk_hthread *thr) {
  21592. /* ECMAScript time, with millisecond fractions. Exposed via
  21593. * duk_get_now() for example.
  21594. */
  21595. DUK_UNREF(thr);
  21596. return (duk_double_t) DUK_USE_DATE_GET_NOW(thr);
  21597. }
  21598. DUK_INTERNAL duk_double_t duk_time_get_ecmascript_time_nofrac(duk_hthread *thr) {
  21599. /* ECMAScript time without millisecond fractions. Exposed via
  21600. * the Date built-in which doesn't allow fractions.
  21601. */
  21602. DUK_UNREF(thr);
  21603. return (duk_double_t) DUK_FLOOR(DUK_USE_DATE_GET_NOW(thr));
  21604. }
  21605. DUK_INTERNAL duk_double_t duk_time_get_monotonic_time(duk_hthread *thr) {
  21606. DUK_UNREF(thr);
  21607. #if defined(DUK_USE_GET_MONOTONIC_TIME)
  21608. return (duk_double_t) DUK_USE_GET_MONOTONIC_TIME(thr);
  21609. #else
  21610. return (duk_double_t) DUK_USE_DATE_GET_NOW(thr);
  21611. #endif
  21612. }
  21613. DUK_EXTERNAL duk_double_t duk_get_now(duk_hthread *thr) {
  21614. DUK_ASSERT_API_ENTRY(thr);
  21615. DUK_UNREF(thr);
  21616. /* This API intentionally allows millisecond fractions. */
  21617. return duk_time_get_ecmascript_time(thr);
  21618. }
  21619. #if 0 /* XXX: worth exposing? */
  21620. DUK_EXTERNAL duk_double_t duk_get_monotonic_time(duk_hthread *thr) {
  21621. DUK_ASSERT_API_ENTRY(thr);
  21622. DUK_UNREF(thr);
  21623. return duk_time_get_monotonic_time(thr);
  21624. }
  21625. #endif
  21626. DUK_EXTERNAL void duk_time_to_components(duk_hthread *thr, duk_double_t timeval, duk_time_components *comp) {
  21627. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  21628. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  21629. duk_uint_t flags;
  21630. DUK_ASSERT_API_ENTRY(thr);
  21631. DUK_ASSERT(comp != NULL); /* XXX: or check? */
  21632. DUK_UNREF(thr);
  21633. /* Convert as one-based, but change month to zero-based to match the
  21634. * ECMAScript Date built-in behavior 1:1.
  21635. */
  21636. flags = DUK_DATE_FLAG_ONEBASED | DUK_DATE_FLAG_NAN_TO_ZERO;
  21637. duk_bi_date_timeval_to_parts(timeval, parts, dparts, flags);
  21638. /* XXX: sub-millisecond accuracy for the API */
  21639. DUK_ASSERT(dparts[DUK_DATE_IDX_MONTH] >= 1.0 && dparts[DUK_DATE_IDX_MONTH] <= 12.0);
  21640. comp->year = dparts[DUK_DATE_IDX_YEAR];
  21641. comp->month = dparts[DUK_DATE_IDX_MONTH] - 1.0;
  21642. comp->day = dparts[DUK_DATE_IDX_DAY];
  21643. comp->hours = dparts[DUK_DATE_IDX_HOUR];
  21644. comp->minutes = dparts[DUK_DATE_IDX_MINUTE];
  21645. comp->seconds = dparts[DUK_DATE_IDX_SECOND];
  21646. comp->milliseconds = dparts[DUK_DATE_IDX_MILLISECOND];
  21647. comp->weekday = dparts[DUK_DATE_IDX_WEEKDAY];
  21648. }
  21649. DUK_EXTERNAL duk_double_t duk_components_to_time(duk_hthread *thr, duk_time_components *comp) {
  21650. duk_double_t d;
  21651. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  21652. duk_uint_t flags;
  21653. DUK_ASSERT_API_ENTRY(thr);
  21654. DUK_ASSERT(comp != NULL); /* XXX: or check? */
  21655. DUK_UNREF(thr);
  21656. /* Match Date constructor behavior (with UTC time). Month is given
  21657. * as zero-based. Day-of-month is given as one-based so normalize
  21658. * it to zero-based as the internal conversion helpers expects all
  21659. * components to be zero-based.
  21660. */
  21661. flags = 0;
  21662. /* XXX: expensive conversion; use array format in API instead, or unify
  21663. * time provider and time API to use same struct?
  21664. */
  21665. dparts[DUK_DATE_IDX_YEAR] = comp->year;
  21666. dparts[DUK_DATE_IDX_MONTH] = comp->month;
  21667. dparts[DUK_DATE_IDX_DAY] = comp->day - 1.0;
  21668. dparts[DUK_DATE_IDX_HOUR] = comp->hours;
  21669. dparts[DUK_DATE_IDX_MINUTE] = comp->minutes;
  21670. dparts[DUK_DATE_IDX_SECOND] = comp->seconds;
  21671. dparts[DUK_DATE_IDX_MILLISECOND] = comp->milliseconds;
  21672. dparts[DUK_DATE_IDX_WEEKDAY] = 0; /* ignored */
  21673. d = duk_bi_date_get_timeval_from_dparts(dparts, flags);
  21674. return d;
  21675. }
  21676. #line 1 "duk_bi_array.c"
  21677. /*
  21678. * Array built-ins
  21679. *
  21680. * Most Array built-ins are intentionally generic in ECMAScript, and are
  21681. * intended to work even when the 'this' binding is not an Array instance.
  21682. * This ECMAScript feature is also used by much real world code. For this
  21683. * reason the implementations here don't assume exotic Array behavior or
  21684. * e.g. presence of a .length property. However, some algorithms have a
  21685. * fast path for duk_harray backed actual Array instances, enabled when
  21686. * footprint is not a concern.
  21687. *
  21688. * XXX: the "Throw" flag should be set for (almost?) all [[Put]] and
  21689. * [[Delete]] operations, but it's currently false throughout. Go through
  21690. * all put/delete cases and check throw flag use. Need a new API primitive
  21691. * which allows throws flag to be specified.
  21692. *
  21693. * XXX: array lengths above 2G won't work reliably. There are many places
  21694. * where one needs a full signed 32-bit range ([-0xffffffff, 0xffffffff],
  21695. * i.e. -33- bits). Although array 'length' cannot be written to be outside
  21696. * the unsigned 32-bit range (E5.1 Section 15.4.5.1 throws a RangeError if so)
  21697. * some intermediate values may be above 0xffffffff and this may not be always
  21698. * correctly handled now (duk_uint32_t is not enough for all algorithms).
  21699. * For instance, push() can legitimately write entries beyond length 0xffffffff
  21700. * and cause a RangeError only at the end. To do this properly, the current
  21701. * push() implementation tracks the array index using a 'double' instead of a
  21702. * duk_uint32_t (which is somewhat awkward). See test-bi-array-push-maxlen.js.
  21703. *
  21704. * On using "put" vs. "def" prop
  21705. * =============================
  21706. *
  21707. * Code below must be careful to use the appropriate primitive as it matters
  21708. * for compliance. When using "put" there may be inherited properties in
  21709. * Array.prototype which cause side effects when values are written. When
  21710. * using "define" there are no such side effects, and many test262 test cases
  21711. * check for this (for real world code, such side effects are very rare).
  21712. * Both "put" and "define" are used in the E5.1 specification; as a rule,
  21713. * "put" is used when modifying an existing array (or a non-array 'this'
  21714. * binding) and "define" for setting values into a fresh result array.
  21715. */
  21716. /* #include duk_internal.h -> already included */
  21717. /* Perform an intermediate join when this many elements have been pushed
  21718. * on the value stack.
  21719. */
  21720. #define DUK__ARRAY_MID_JOIN_LIMIT 4096
  21721. #if defined(DUK_USE_ARRAY_BUILTIN)
  21722. /*
  21723. * Shared helpers.
  21724. */
  21725. /* Shared entry code for many Array built-ins: the 'this' binding is pushed
  21726. * on the value stack and object coerced, and the current .length is returned.
  21727. * Note that length is left on stack (it could be popped, but that's not
  21728. * usually necessary because call handling will clean it up automatically).
  21729. */
  21730. DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32(duk_hthread *thr) {
  21731. duk_uint32_t len;
  21732. /* XXX: push more directly? */
  21733. (void) duk_push_this_coercible_to_object(thr);
  21734. DUK_ASSERT_HOBJECT_VALID(duk_get_hobject(thr, -1));
  21735. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_LENGTH);
  21736. len = duk_to_uint32(thr, -1);
  21737. /* -> [ ... ToObject(this) ToUint32(length) ] */
  21738. return len;
  21739. }
  21740. DUK_LOCAL duk_uint32_t duk__push_this_obj_len_u32_limited(duk_hthread *thr) {
  21741. /* Range limited to [0, 0x7fffffff] range, i.e. range that can be
  21742. * represented with duk_int32_t. Use this when the method doesn't
  21743. * handle the full 32-bit unsigned range correctly.
  21744. */
  21745. duk_uint32_t ret = duk__push_this_obj_len_u32(thr);
  21746. if (DUK_UNLIKELY(ret >= 0x80000000UL)) {
  21747. DUK_ERROR_RANGE_INVALID_LENGTH(thr);
  21748. DUK_WO_NORETURN(return 0U;);
  21749. }
  21750. return ret;
  21751. }
  21752. #if defined(DUK_USE_ARRAY_FASTPATH)
  21753. /* Check if 'this' binding is an Array instance (duk_harray) which satisfies
  21754. * a few other guarantees for fast path operation. The fast path doesn't
  21755. * need to handle all operations, even for duk_harrays, but must handle a
  21756. * significant fraction to improve performance. Return a non-NULL duk_harray
  21757. * pointer when all fast path criteria are met, NULL otherwise.
  21758. */
  21759. DUK_LOCAL duk_harray *duk__arraypart_fastpath_this(duk_hthread *thr) {
  21760. duk_tval *tv;
  21761. duk_hobject *h;
  21762. duk_uint_t flags_mask, flags_bits, flags_value;
  21763. DUK_ASSERT(thr->valstack_bottom > thr->valstack); /* because call in progress */
  21764. tv = DUK_GET_THIS_TVAL_PTR(thr);
  21765. /* Fast path requires that 'this' is a duk_harray. Read only arrays
  21766. * (ROM backed) are also rejected for simplicity.
  21767. */
  21768. if (!DUK_TVAL_IS_OBJECT(tv)) {
  21769. DUK_DD(DUK_DDPRINT("reject array fast path: not an object"));
  21770. return NULL;
  21771. }
  21772. h = DUK_TVAL_GET_OBJECT(tv);
  21773. DUK_ASSERT(h != NULL);
  21774. flags_mask = DUK_HOBJECT_FLAG_ARRAY_PART | \
  21775. DUK_HOBJECT_FLAG_EXOTIC_ARRAY | \
  21776. DUK_HEAPHDR_FLAG_READONLY;
  21777. flags_bits = DUK_HOBJECT_FLAG_ARRAY_PART | \
  21778. DUK_HOBJECT_FLAG_EXOTIC_ARRAY;
  21779. flags_value = DUK_HEAPHDR_GET_FLAGS_RAW((duk_heaphdr *) h);
  21780. if ((flags_value & flags_mask) != flags_bits) {
  21781. DUK_DD(DUK_DDPRINT("reject array fast path: object flag check failed"));
  21782. return NULL;
  21783. }
  21784. /* In some cases a duk_harray's 'length' may be larger than the
  21785. * current array part allocation. Avoid the fast path in these
  21786. * cases, so that all fast path code can safely assume that all
  21787. * items in the range [0,length[ are backed by the current array
  21788. * part allocation.
  21789. */
  21790. if (((duk_harray *) h)->length > DUK_HOBJECT_GET_ASIZE(h)) {
  21791. DUK_DD(DUK_DDPRINT("reject array fast path: length > array part size"));
  21792. return NULL;
  21793. }
  21794. /* Guarantees for fast path. */
  21795. DUK_ASSERT(h != NULL);
  21796. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(h) == 0 || DUK_HOBJECT_A_GET_BASE(thr->heap, h) != NULL);
  21797. DUK_ASSERT(((duk_harray *) h)->length <= DUK_HOBJECT_GET_ASIZE(h));
  21798. DUK_DD(DUK_DDPRINT("array fast path allowed for: %!O", (duk_heaphdr *) h));
  21799. return (duk_harray *) h;
  21800. }
  21801. #endif /* DUK_USE_ARRAY_FASTPATH */
  21802. /*
  21803. * Constructor
  21804. */
  21805. DUK_INTERNAL duk_ret_t duk_bi_array_constructor(duk_hthread *thr) {
  21806. duk_idx_t nargs;
  21807. duk_harray *a;
  21808. duk_double_t d;
  21809. duk_uint32_t len;
  21810. duk_uint32_t len_prealloc;
  21811. nargs = duk_get_top(thr);
  21812. if (nargs == 1 && duk_is_number(thr, 0)) {
  21813. /* XXX: expensive check (also shared elsewhere - so add a shared internal API call?) */
  21814. d = duk_get_number(thr, 0);
  21815. len = duk_to_uint32(thr, 0);
  21816. if (((duk_double_t) len) != d) {
  21817. DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
  21818. }
  21819. /* For small lengths create a dense preallocated array.
  21820. * For large arrays preallocate an initial part.
  21821. */
  21822. len_prealloc = len < 64 ? len : 64;
  21823. a = duk_push_harray_with_size(thr, len_prealloc);
  21824. DUK_ASSERT(a != NULL);
  21825. a->length = len;
  21826. return 1;
  21827. }
  21828. duk_pack(thr, nargs);
  21829. return 1;
  21830. }
  21831. /*
  21832. * isArray()
  21833. */
  21834. DUK_INTERNAL duk_ret_t duk_bi_array_constructor_is_array(duk_hthread *thr) {
  21835. duk_hobject *h;
  21836. h = duk_get_hobject_with_class(thr, 0, DUK_HOBJECT_CLASS_ARRAY);
  21837. duk_push_boolean(thr, (h != NULL));
  21838. return 1;
  21839. }
  21840. /*
  21841. * toString()
  21842. */
  21843. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_to_string(duk_hthread *thr) {
  21844. (void) duk_push_this_coercible_to_object(thr);
  21845. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_JOIN);
  21846. /* [ ... this func ] */
  21847. if (!duk_is_callable(thr, -1)) {
  21848. /* Fall back to the initial (original) Object.toString(). We don't
  21849. * currently have pointers to the built-in functions, only the top
  21850. * level global objects (like "Array") so this is now done in a bit
  21851. * of a hacky manner. It would be cleaner to push the (original)
  21852. * function and use duk_call_method().
  21853. */
  21854. /* XXX: 'this' will be ToObject() coerced twice, which is incorrect
  21855. * but should have no visible side effects.
  21856. */
  21857. DUK_DDD(DUK_DDDPRINT("this.join is not callable, fall back to (original) Object.toString"));
  21858. duk_set_top(thr, 0);
  21859. return duk_bi_object_prototype_to_string(thr); /* has access to 'this' binding */
  21860. }
  21861. /* [ ... this func ] */
  21862. duk_insert(thr, -2);
  21863. /* [ ... func this ] */
  21864. DUK_DDD(DUK_DDDPRINT("calling: func=%!iT, this=%!iT",
  21865. (duk_tval *) duk_get_tval(thr, -2),
  21866. (duk_tval *) duk_get_tval(thr, -1)));
  21867. duk_call_method(thr, 0);
  21868. return 1;
  21869. }
  21870. /*
  21871. * concat()
  21872. */
  21873. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_concat(duk_hthread *thr) {
  21874. duk_idx_t i, n;
  21875. duk_uint32_t j, idx, len;
  21876. duk_hobject *h;
  21877. duk_size_t tmp_len;
  21878. /* XXX: In ES2015 Array .length can be up to 2^53-1. The current
  21879. * implementation is limited to 2^32-1.
  21880. */
  21881. /* XXX: Fast path for array 'this' and array element. */
  21882. /* XXX: The insert here is a bit expensive if there are a lot of items.
  21883. * It could also be special cased in the outermost for loop quite easily
  21884. * (as the element is dup()'d anyway).
  21885. */
  21886. (void) duk_push_this_coercible_to_object(thr);
  21887. duk_insert(thr, 0);
  21888. n = duk_get_top(thr);
  21889. duk_push_array(thr); /* -> [ ToObject(this) item1 ... itemN arr ] */
  21890. /* NOTE: The Array special behaviors are NOT invoked by duk_xdef_prop_index()
  21891. * (which differs from the official algorithm). If no error is thrown, this
  21892. * doesn't matter as the length is updated at the end. However, if an error
  21893. * is thrown, the length will be unset. That shouldn't matter because the
  21894. * caller won't get a reference to the intermediate value.
  21895. */
  21896. idx = 0;
  21897. for (i = 0; i < n; i++) {
  21898. duk_bool_t spreadable;
  21899. duk_bool_t need_has_check;
  21900. DUK_ASSERT_TOP(thr, n + 1);
  21901. /* [ ToObject(this) item1 ... itemN arr ] */
  21902. h = duk_get_hobject(thr, i);
  21903. if (h == NULL) {
  21904. spreadable = 0;
  21905. } else {
  21906. #if defined(DUK_USE_SYMBOL_BUILTIN)
  21907. duk_get_prop_stridx(thr, i, DUK_STRIDX_WELLKNOWN_SYMBOL_IS_CONCAT_SPREADABLE);
  21908. if (duk_is_undefined(thr, -1)) {
  21909. spreadable = (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY);
  21910. } else {
  21911. spreadable = duk_to_boolean(thr, -1);
  21912. }
  21913. duk_pop_nodecref_unsafe(thr);
  21914. #else
  21915. spreadable = (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY);
  21916. #endif
  21917. }
  21918. if (!spreadable) {
  21919. duk_dup(thr, i);
  21920. duk_xdef_prop_index_wec(thr, -2, idx);
  21921. idx++;
  21922. if (DUK_UNLIKELY(idx == 0U)) {
  21923. /* Index after update is 0, and index written
  21924. * was 0xffffffffUL which is no longer a valid
  21925. * array index.
  21926. */
  21927. goto fail_wrap;
  21928. }
  21929. continue;
  21930. }
  21931. DUK_ASSERT(duk_is_object(thr, i));
  21932. need_has_check = (DUK_HOBJECT_IS_PROXY(h) != 0); /* Always 0 w/o Proxy support. */
  21933. /* [ ToObject(this) item1 ... itemN arr ] */
  21934. tmp_len = duk_get_length(thr, i);
  21935. len = (duk_uint32_t) tmp_len;
  21936. if (DUK_UNLIKELY(tmp_len != (duk_size_t) len)) {
  21937. goto fail_wrap;
  21938. }
  21939. if (DUK_UNLIKELY(idx + len < idx)) {
  21940. /* Result length must be at most 0xffffffffUL to be
  21941. * a valid 32-bit array index.
  21942. */
  21943. goto fail_wrap;
  21944. }
  21945. for (j = 0; j < len; j++) {
  21946. /* For a Proxy element, an explicit 'has' check is
  21947. * needed to allow the Proxy to present gaps.
  21948. */
  21949. if (need_has_check) {
  21950. if (duk_has_prop_index(thr, i, j)) {
  21951. duk_get_prop_index(thr, i, j);
  21952. duk_xdef_prop_index_wec(thr, -2, idx);
  21953. }
  21954. } else {
  21955. if (duk_get_prop_index(thr, i, j)) {
  21956. duk_xdef_prop_index_wec(thr, -2, idx);
  21957. } else {
  21958. duk_pop_undefined(thr);
  21959. }
  21960. }
  21961. idx++;
  21962. DUK_ASSERT(idx != 0U); /* Wrap check above. */
  21963. }
  21964. }
  21965. /* ES5.1 has a specification "bug" in that nonexistent trailing
  21966. * elements don't affect the result .length. Test262 and other
  21967. * engines disagree, and the specification bug was fixed in ES2015
  21968. * (see NOTE 1 in https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.concat).
  21969. */
  21970. duk_push_uarridx(thr, idx);
  21971. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  21972. DUK_ASSERT_TOP(thr, n + 1);
  21973. return 1;
  21974. fail_wrap:
  21975. DUK_ERROR_RANGE_INVALID_LENGTH(thr);
  21976. DUK_WO_NORETURN(return 0;);
  21977. }
  21978. /*
  21979. * join(), toLocaleString()
  21980. *
  21981. * Note: checking valstack is necessary, but only in the per-element loop.
  21982. *
  21983. * Note: the trivial approach of pushing all the elements on the value stack
  21984. * and then calling duk_join() fails when the array contains a large number
  21985. * of elements. This problem can't be offloaded to duk_join() because the
  21986. * elements to join must be handled here and have special handling. Current
  21987. * approach is to do intermediate joins with very large number of elements.
  21988. * There is no fancy handling; the prefix gets re-joined multiple times.
  21989. */
  21990. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_join_shared(duk_hthread *thr) {
  21991. duk_uint32_t len, count;
  21992. duk_uint32_t idx;
  21993. duk_small_int_t to_locale_string = duk_get_current_magic(thr);
  21994. duk_idx_t valstack_required;
  21995. /* For join(), nargs is 1. For toLocaleString(), nargs is 0 and
  21996. * setting the top essentially pushes an undefined to the stack,
  21997. * thus defaulting to a comma separator.
  21998. */
  21999. duk_set_top(thr, 1);
  22000. if (duk_is_undefined(thr, 0)) {
  22001. duk_pop_undefined(thr);
  22002. duk_push_hstring_stridx(thr, DUK_STRIDX_COMMA);
  22003. } else {
  22004. duk_to_string(thr, 0);
  22005. }
  22006. len = duk__push_this_obj_len_u32(thr);
  22007. /* [ sep ToObject(this) len ] */
  22008. DUK_DDD(DUK_DDDPRINT("sep=%!T, this=%!T, len=%lu",
  22009. (duk_tval *) duk_get_tval(thr, 0),
  22010. (duk_tval *) duk_get_tval(thr, 1),
  22011. (unsigned long) len));
  22012. /* The extra (+4) is tight. */
  22013. valstack_required = (duk_idx_t) ((len >= DUK__ARRAY_MID_JOIN_LIMIT ?
  22014. DUK__ARRAY_MID_JOIN_LIMIT : len) + 4);
  22015. duk_require_stack(thr, valstack_required);
  22016. duk_dup_0(thr);
  22017. /* [ sep ToObject(this) len sep ] */
  22018. count = 0;
  22019. idx = 0;
  22020. for (;;) {
  22021. DUK_DDD(DUK_DDDPRINT("join idx=%ld", (long) idx));
  22022. if (count >= DUK__ARRAY_MID_JOIN_LIMIT || /* intermediate join to avoid valstack overflow */
  22023. idx >= len) { /* end of loop (careful with len==0) */
  22024. /* [ sep ToObject(this) len sep str0 ... str(count-1) ] */
  22025. DUK_DDD(DUK_DDDPRINT("mid/final join, count=%ld, idx=%ld, len=%ld",
  22026. (long) count, (long) idx, (long) len));
  22027. duk_join(thr, (duk_idx_t) count); /* -> [ sep ToObject(this) len str ] */
  22028. duk_dup_0(thr); /* -> [ sep ToObject(this) len str sep ] */
  22029. duk_insert(thr, -2); /* -> [ sep ToObject(this) len sep str ] */
  22030. count = 1;
  22031. }
  22032. if (idx >= len) {
  22033. /* if true, the stack already contains the final result */
  22034. break;
  22035. }
  22036. duk_get_prop_index(thr, 1, (duk_uarridx_t) idx);
  22037. if (duk_is_null_or_undefined(thr, -1)) {
  22038. duk_pop_nodecref_unsafe(thr);
  22039. duk_push_hstring_empty(thr);
  22040. } else {
  22041. if (to_locale_string) {
  22042. duk_to_object(thr, -1);
  22043. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_TO_LOCALE_STRING);
  22044. duk_insert(thr, -2); /* -> [ ... toLocaleString ToObject(val) ] */
  22045. duk_call_method(thr, 0);
  22046. }
  22047. duk_to_string(thr, -1);
  22048. }
  22049. count++;
  22050. idx++;
  22051. }
  22052. /* [ sep ToObject(this) len sep result ] */
  22053. return 1;
  22054. }
  22055. /*
  22056. * pop(), push()
  22057. */
  22058. #if defined(DUK_USE_ARRAY_FASTPATH)
  22059. DUK_LOCAL duk_ret_t duk__array_pop_fastpath(duk_hthread *thr, duk_harray *h_arr) {
  22060. duk_tval *tv_arraypart;
  22061. duk_tval *tv_val;
  22062. duk_uint32_t len;
  22063. tv_arraypart = DUK_HOBJECT_A_GET_BASE(thr->heap, (duk_hobject *) h_arr);
  22064. len = h_arr->length;
  22065. if (len <= 0) {
  22066. /* nop, return undefined */
  22067. return 0;
  22068. }
  22069. len--;
  22070. h_arr->length = len;
  22071. /* Fast path doesn't check for an index property inherited from
  22072. * Array.prototype. This is quite often acceptable; if not,
  22073. * disable fast path.
  22074. */
  22075. DUK_ASSERT_VS_SPACE(thr);
  22076. tv_val = tv_arraypart + len;
  22077. if (DUK_TVAL_IS_UNUSED(tv_val)) {
  22078. /* No net refcount change. Value stack already has
  22079. * 'undefined' based on value stack init policy.
  22080. */
  22081. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top));
  22082. DUK_ASSERT(DUK_TVAL_IS_UNUSED(tv_val));
  22083. } else {
  22084. /* No net refcount change. */
  22085. DUK_TVAL_SET_TVAL(thr->valstack_top, tv_val);
  22086. DUK_TVAL_SET_UNUSED(tv_val);
  22087. }
  22088. thr->valstack_top++;
  22089. /* XXX: there's no shrink check in the fast path now */
  22090. return 1;
  22091. }
  22092. #endif /* DUK_USE_ARRAY_FASTPATH */
  22093. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_pop(duk_hthread *thr) {
  22094. duk_uint32_t len;
  22095. duk_uint32_t idx;
  22096. #if defined(DUK_USE_ARRAY_FASTPATH)
  22097. duk_harray *h_arr;
  22098. #endif
  22099. DUK_ASSERT_TOP(thr, 0);
  22100. #if defined(DUK_USE_ARRAY_FASTPATH)
  22101. h_arr = duk__arraypart_fastpath_this(thr);
  22102. if (h_arr) {
  22103. return duk__array_pop_fastpath(thr, h_arr);
  22104. }
  22105. #endif
  22106. /* XXX: Merge fastpath check into a related call (push this, coerce length, etc)? */
  22107. len = duk__push_this_obj_len_u32(thr);
  22108. if (len == 0) {
  22109. duk_push_int(thr, 0);
  22110. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LENGTH);
  22111. return 0;
  22112. }
  22113. idx = len - 1;
  22114. duk_get_prop_index(thr, 0, (duk_uarridx_t) idx);
  22115. duk_del_prop_index(thr, 0, (duk_uarridx_t) idx);
  22116. duk_push_u32(thr, idx);
  22117. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LENGTH);
  22118. return 1;
  22119. }
  22120. #if defined(DUK_USE_ARRAY_FASTPATH)
  22121. DUK_LOCAL duk_ret_t duk__array_push_fastpath(duk_hthread *thr, duk_harray *h_arr) {
  22122. duk_tval *tv_arraypart;
  22123. duk_tval *tv_src;
  22124. duk_tval *tv_dst;
  22125. duk_uint32_t len;
  22126. duk_idx_t i, n;
  22127. len = h_arr->length;
  22128. tv_arraypart = DUK_HOBJECT_A_GET_BASE(thr->heap, (duk_hobject *) h_arr);
  22129. n = (duk_idx_t) (thr->valstack_top - thr->valstack_bottom);
  22130. DUK_ASSERT(n >= 0);
  22131. DUK_ASSERT((duk_uint32_t) n <= DUK_UINT32_MAX);
  22132. if (DUK_UNLIKELY(len + (duk_uint32_t) n < len)) {
  22133. DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw"));
  22134. DUK_DCERROR_RANGE_INVALID_LENGTH(thr); /* != 0 return value returned as is by caller */
  22135. }
  22136. if (len + (duk_uint32_t) n > DUK_HOBJECT_GET_ASIZE((duk_hobject *) h_arr)) {
  22137. /* Array part would need to be extended. Rely on slow path
  22138. * for now.
  22139. *
  22140. * XXX: Rework hobject code a bit and add extend support.
  22141. */
  22142. return 0;
  22143. }
  22144. tv_src = thr->valstack_bottom;
  22145. tv_dst = tv_arraypart + len;
  22146. for (i = 0; i < n; i++) {
  22147. /* No net refcount change; reset value stack values to
  22148. * undefined to satisfy value stack init policy.
  22149. */
  22150. DUK_TVAL_SET_TVAL(tv_dst, tv_src);
  22151. DUK_TVAL_SET_UNDEFINED(tv_src);
  22152. tv_src++;
  22153. tv_dst++;
  22154. }
  22155. thr->valstack_top = thr->valstack_bottom;
  22156. len += (duk_uint32_t) n;
  22157. h_arr->length = len;
  22158. DUK_ASSERT((duk_uint_t) len == len);
  22159. duk_push_uint(thr, (duk_uint_t) len);
  22160. return 1;
  22161. }
  22162. #endif /* DUK_USE_ARRAY_FASTPATH */
  22163. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_push(duk_hthread *thr) {
  22164. /* Note: 'this' is not necessarily an Array object. The push()
  22165. * algorithm is supposed to work for other kinds of objects too,
  22166. * so the algorithm has e.g. an explicit update for the 'length'
  22167. * property which is normally "magical" in arrays.
  22168. */
  22169. duk_uint32_t len;
  22170. duk_idx_t i, n;
  22171. #if defined(DUK_USE_ARRAY_FASTPATH)
  22172. duk_harray *h_arr;
  22173. #endif
  22174. #if defined(DUK_USE_ARRAY_FASTPATH)
  22175. h_arr = duk__arraypart_fastpath_this(thr);
  22176. if (h_arr) {
  22177. duk_ret_t rc;
  22178. rc = duk__array_push_fastpath(thr, h_arr);
  22179. if (rc != 0) {
  22180. return rc;
  22181. }
  22182. DUK_DD(DUK_DDPRINT("array push() fast path exited, resize case"));
  22183. }
  22184. #endif
  22185. n = duk_get_top(thr);
  22186. len = duk__push_this_obj_len_u32(thr);
  22187. /* [ arg1 ... argN obj length ] */
  22188. /* Technically Array.prototype.push() can create an Array with length
  22189. * longer than 2^32-1, i.e. outside the 32-bit range. The final length
  22190. * is *not* wrapped to 32 bits in the specification.
  22191. *
  22192. * This implementation tracks length with a uint32 because it's much
  22193. * more practical.
  22194. *
  22195. * See: test-bi-array-push-maxlen.js.
  22196. */
  22197. if (len + (duk_uint32_t) n < len) {
  22198. DUK_D(DUK_DPRINT("Array.prototype.push() would go beyond 32-bit length, throw"));
  22199. DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
  22200. }
  22201. for (i = 0; i < n; i++) {
  22202. duk_dup(thr, i);
  22203. duk_put_prop_index(thr, -3, (duk_uarridx_t) (len + (duk_uint32_t) i));
  22204. }
  22205. len += (duk_uint32_t) n;
  22206. duk_push_u32(thr, len);
  22207. duk_dup_top(thr);
  22208. duk_put_prop_stridx_short(thr, -4, DUK_STRIDX_LENGTH);
  22209. /* [ arg1 ... argN obj length new_length ] */
  22210. return 1;
  22211. }
  22212. /*
  22213. * sort()
  22214. *
  22215. * Currently qsort with random pivot. This is now really, really slow,
  22216. * because there is no fast path for array parts.
  22217. *
  22218. * Signed indices are used because qsort() leaves and degenerate cases
  22219. * may use a negative offset.
  22220. */
  22221. DUK_LOCAL duk_small_int_t duk__array_sort_compare(duk_hthread *thr, duk_int_t idx1, duk_int_t idx2) {
  22222. duk_bool_t have1, have2;
  22223. duk_bool_t undef1, undef2;
  22224. duk_small_int_t ret;
  22225. duk_idx_t idx_obj = 1; /* fixed offsets in valstack */
  22226. duk_idx_t idx_fn = 0;
  22227. duk_hstring *h1, *h2;
  22228. /* Fast exit if indices are identical. This is valid for a non-existent property,
  22229. * for an undefined value, and almost always for ToString() coerced comparison of
  22230. * arbitrary values (corner cases where this is not the case include e.g. a an
  22231. * object with varying ToString() coercion).
  22232. *
  22233. * The specification does not prohibit "caching" of values read from the array, so
  22234. * assuming equality for comparing an index with itself falls into the category of
  22235. * "caching".
  22236. *
  22237. * Also, compareFn may be inconsistent, so skipping a call to compareFn here may
  22238. * have an effect on the final result. The specification does not require any
  22239. * specific behavior for inconsistent compare functions, so again, this fast path
  22240. * is OK.
  22241. */
  22242. if (idx1 == idx2) {
  22243. DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld -> indices identical, quick exit",
  22244. (long) idx1, (long) idx2));
  22245. return 0;
  22246. }
  22247. have1 = duk_get_prop_index(thr, idx_obj, (duk_uarridx_t) idx1);
  22248. have2 = duk_get_prop_index(thr, idx_obj, (duk_uarridx_t) idx2);
  22249. DUK_DDD(DUK_DDDPRINT("duk__array_sort_compare: idx1=%ld, idx2=%ld, have1=%ld, have2=%ld, val1=%!T, val2=%!T",
  22250. (long) idx1, (long) idx2, (long) have1, (long) have2,
  22251. (duk_tval *) duk_get_tval(thr, -2), (duk_tval *) duk_get_tval(thr, -1)));
  22252. if (have1) {
  22253. if (have2) {
  22254. ;
  22255. } else {
  22256. ret = -1;
  22257. goto pop_ret;
  22258. }
  22259. } else {
  22260. if (have2) {
  22261. ret = 1;
  22262. goto pop_ret;
  22263. } else {
  22264. ret = 0;
  22265. goto pop_ret;
  22266. }
  22267. }
  22268. undef1 = duk_is_undefined(thr, -2);
  22269. undef2 = duk_is_undefined(thr, -1);
  22270. if (undef1) {
  22271. if (undef2) {
  22272. ret = 0;
  22273. goto pop_ret;
  22274. } else {
  22275. ret = 1;
  22276. goto pop_ret;
  22277. }
  22278. } else {
  22279. if (undef2) {
  22280. ret = -1;
  22281. goto pop_ret;
  22282. } else {
  22283. ;
  22284. }
  22285. }
  22286. if (!duk_is_undefined(thr, idx_fn)) {
  22287. duk_double_t d;
  22288. /* No need to check callable; duk_call() will do that. */
  22289. duk_dup(thr, idx_fn); /* -> [ ... x y fn ] */
  22290. duk_insert(thr, -3); /* -> [ ... fn x y ] */
  22291. duk_call(thr, 2); /* -> [ ... res ] */
  22292. /* ES5 is a bit vague about what to do if the return value is
  22293. * not a number. ES2015 provides a concrete description:
  22294. * http://www.ecma-international.org/ecma-262/6.0/#sec-sortcompare.
  22295. */
  22296. d = duk_to_number_m1(thr);
  22297. if (d < 0.0) {
  22298. ret = -1;
  22299. } else if (d > 0.0) {
  22300. ret = 1;
  22301. } else {
  22302. /* Because NaN compares to false, NaN is handled here
  22303. * without an explicit check above.
  22304. */
  22305. ret = 0;
  22306. }
  22307. duk_pop_nodecref_unsafe(thr);
  22308. DUK_DDD(DUK_DDDPRINT("-> result %ld (from comparefn, after coercion)", (long) ret));
  22309. return ret;
  22310. }
  22311. /* string compare is the default (a bit oddly) */
  22312. /* XXX: any special handling for plain array; causes repeated coercion now? */
  22313. h1 = duk_to_hstring(thr, -2);
  22314. h2 = duk_to_hstring_m1(thr);
  22315. DUK_ASSERT(h1 != NULL);
  22316. DUK_ASSERT(h2 != NULL);
  22317. ret = duk_js_string_compare(h1, h2); /* retval is directly usable */
  22318. goto pop_ret;
  22319. pop_ret:
  22320. duk_pop_2_unsafe(thr);
  22321. DUK_DDD(DUK_DDDPRINT("-> result %ld", (long) ret));
  22322. return ret;
  22323. }
  22324. DUK_LOCAL void duk__array_sort_swap(duk_hthread *thr, duk_int_t l, duk_int_t r) {
  22325. duk_bool_t have_l, have_r;
  22326. duk_idx_t idx_obj = 1; /* fixed offset in valstack */
  22327. if (l == r) {
  22328. return;
  22329. }
  22330. /* swap elements; deal with non-existent elements correctly */
  22331. have_l = duk_get_prop_index(thr, idx_obj, (duk_uarridx_t) l);
  22332. have_r = duk_get_prop_index(thr, idx_obj, (duk_uarridx_t) r);
  22333. if (have_r) {
  22334. /* right exists, [[Put]] regardless whether or not left exists */
  22335. duk_put_prop_index(thr, idx_obj, (duk_uarridx_t) l);
  22336. } else {
  22337. duk_del_prop_index(thr, idx_obj, (duk_uarridx_t) l);
  22338. duk_pop_undefined(thr);
  22339. }
  22340. if (have_l) {
  22341. duk_put_prop_index(thr, idx_obj, (duk_uarridx_t) r);
  22342. } else {
  22343. duk_del_prop_index(thr, idx_obj, (duk_uarridx_t) r);
  22344. duk_pop_undefined(thr);
  22345. }
  22346. }
  22347. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  22348. /* Debug print which visualizes the qsort partitioning process. */
  22349. DUK_LOCAL void duk__debuglog_qsort_state(duk_hthread *thr, duk_int_t lo, duk_int_t hi, duk_int_t pivot) {
  22350. char buf[4096];
  22351. char *ptr = buf;
  22352. duk_int_t i, n;
  22353. n = (duk_int_t) duk_get_length(thr, 1);
  22354. if (n > 4000) {
  22355. n = 4000;
  22356. }
  22357. *ptr++ = '[';
  22358. for (i = 0; i < n; i++) {
  22359. if (i == pivot) {
  22360. *ptr++ = '|';
  22361. } else if (i == lo) {
  22362. *ptr++ = '<';
  22363. } else if (i == hi) {
  22364. *ptr++ = '>';
  22365. } else if (i >= lo && i <= hi) {
  22366. *ptr++ = '-';
  22367. } else {
  22368. *ptr++ = ' ';
  22369. }
  22370. }
  22371. *ptr++ = ']';
  22372. *ptr++ = '\0';
  22373. DUK_DDD(DUK_DDDPRINT("%s (lo=%ld, hi=%ld, pivot=%ld)",
  22374. (const char *) buf, (long) lo, (long) hi, (long) pivot));
  22375. }
  22376. #endif
  22377. DUK_LOCAL void duk__array_qsort(duk_hthread *thr, duk_int_t lo, duk_int_t hi) {
  22378. duk_int_t p, l, r;
  22379. /* The lo/hi indices may be crossed and hi < 0 is possible at entry. */
  22380. DUK_DDD(DUK_DDDPRINT("duk__array_qsort: lo=%ld, hi=%ld, obj=%!T",
  22381. (long) lo, (long) hi, (duk_tval *) duk_get_tval(thr, 1)));
  22382. DUK_ASSERT_TOP(thr, 3);
  22383. /* In some cases it may be that lo > hi, or hi < 0; these
  22384. * degenerate cases happen e.g. for empty arrays, and in
  22385. * recursion leaves.
  22386. */
  22387. /* trivial cases */
  22388. if (hi - lo < 1) {
  22389. DUK_DDD(DUK_DDDPRINT("degenerate case, return immediately"));
  22390. return;
  22391. }
  22392. DUK_ASSERT(hi > lo);
  22393. DUK_ASSERT(hi - lo + 1 >= 2);
  22394. /* randomized pivot selection */
  22395. p = lo + (duk_int_t) (DUK_UTIL_GET_RANDOM_DOUBLE(thr) * (duk_double_t) (hi - lo + 1));
  22396. DUK_ASSERT(p >= lo && p <= hi);
  22397. DUK_DDD(DUK_DDDPRINT("lo=%ld, hi=%ld, chose pivot p=%ld", (long) lo, (long) hi, (long) p));
  22398. /* move pivot out of the way */
  22399. duk__array_sort_swap(thr, p, lo);
  22400. p = lo;
  22401. DUK_DDD(DUK_DDDPRINT("pivot moved out of the way: %!T", (duk_tval *) duk_get_tval(thr, 1)));
  22402. l = lo + 1;
  22403. r = hi;
  22404. for (;;) {
  22405. /* find elements to swap */
  22406. for (;;) {
  22407. DUK_DDD(DUK_DDDPRINT("left scan: l=%ld, r=%ld, p=%ld",
  22408. (long) l, (long) r, (long) p));
  22409. if (l >= hi) {
  22410. break;
  22411. }
  22412. if (duk__array_sort_compare(thr, l, p) >= 0) { /* !(l < p) */
  22413. break;
  22414. }
  22415. l++;
  22416. }
  22417. for (;;) {
  22418. DUK_DDD(DUK_DDDPRINT("right scan: l=%ld, r=%ld, p=%ld",
  22419. (long) l, (long) r, (long) p));
  22420. if (r <= lo) {
  22421. break;
  22422. }
  22423. if (duk__array_sort_compare(thr, p, r) >= 0) { /* !(p < r) */
  22424. break;
  22425. }
  22426. r--;
  22427. }
  22428. if (l >= r) {
  22429. goto done;
  22430. }
  22431. DUK_ASSERT(l < r);
  22432. DUK_DDD(DUK_DDDPRINT("swap %ld and %ld", (long) l, (long) r));
  22433. duk__array_sort_swap(thr, l, r);
  22434. DUK_DDD(DUK_DDDPRINT("after swap: %!T", (duk_tval *) duk_get_tval(thr, 1)));
  22435. l++;
  22436. r--;
  22437. }
  22438. done:
  22439. /* Note that 'l' and 'r' may cross, i.e. r < l */
  22440. DUK_ASSERT(l >= lo && l <= hi);
  22441. DUK_ASSERT(r >= lo && r <= hi);
  22442. /* XXX: there's no explicit recursion bound here now. For the average
  22443. * qsort recursion depth O(log n) that's not really necessary: e.g. for
  22444. * 2**32 recursion depth would be about 32 which is OK. However, qsort
  22445. * worst case recursion depth is O(n) which may be a problem.
  22446. */
  22447. /* move pivot to its final place */
  22448. DUK_DDD(DUK_DDDPRINT("before final pivot swap: %!T", (duk_tval *) duk_get_tval(thr, 1)));
  22449. duk__array_sort_swap(thr, lo, r);
  22450. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  22451. duk__debuglog_qsort_state(thr, lo, hi, r);
  22452. #endif
  22453. DUK_DDD(DUK_DDDPRINT("recurse: pivot=%ld, obj=%!T", (long) r, (duk_tval *) duk_get_tval(thr, 1)));
  22454. duk__array_qsort(thr, lo, r - 1);
  22455. duk__array_qsort(thr, r + 1, hi);
  22456. }
  22457. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_sort(duk_hthread *thr) {
  22458. duk_uint32_t len;
  22459. /* XXX: len >= 0x80000000 won't work below because a signed type
  22460. * is needed by qsort.
  22461. */
  22462. len = duk__push_this_obj_len_u32_limited(thr);
  22463. /* stack[0] = compareFn
  22464. * stack[1] = ToObject(this)
  22465. * stack[2] = ToUint32(length)
  22466. */
  22467. if (len > 0) {
  22468. /* avoid degenerate cases, so that (len - 1) won't underflow */
  22469. duk__array_qsort(thr, (duk_int_t) 0, (duk_int_t) (len - 1));
  22470. }
  22471. DUK_ASSERT_TOP(thr, 3);
  22472. duk_pop_nodecref_unsafe(thr);
  22473. return 1; /* return ToObject(this) */
  22474. }
  22475. /*
  22476. * splice()
  22477. */
  22478. /* XXX: this compiles to over 500 bytes now, even without special handling
  22479. * for an array part. Uses signed ints so does not handle full array range correctly.
  22480. */
  22481. /* XXX: can shift() / unshift() use the same helper?
  22482. * shift() is (close to?) <--> splice(0, 1)
  22483. * unshift is (close to?) <--> splice(0, 0, [items])?
  22484. */
  22485. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_splice(duk_hthread *thr) {
  22486. duk_idx_t nargs;
  22487. duk_uint32_t len_u32;
  22488. duk_int_t len;
  22489. duk_bool_t have_delcount;
  22490. duk_int_t item_count;
  22491. duk_int_t act_start;
  22492. duk_int_t del_count;
  22493. duk_int_t i, n;
  22494. DUK_UNREF(have_delcount);
  22495. nargs = duk_get_top(thr);
  22496. if (nargs < 2) {
  22497. duk_set_top(thr, 2);
  22498. nargs = 2;
  22499. have_delcount = 0;
  22500. } else {
  22501. have_delcount = 1;
  22502. }
  22503. /* XXX: len >= 0x80000000 won't work below because we need to be
  22504. * able to represent -len.
  22505. */
  22506. len_u32 = duk__push_this_obj_len_u32_limited(thr);
  22507. len = (duk_int_t) len_u32;
  22508. DUK_ASSERT(len >= 0);
  22509. act_start = duk_to_int_clamped(thr, 0, -len, len);
  22510. if (act_start < 0) {
  22511. act_start = len + act_start;
  22512. }
  22513. DUK_ASSERT(act_start >= 0 && act_start <= len);
  22514. #if defined(DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT)
  22515. if (have_delcount) {
  22516. #endif
  22517. del_count = duk_to_int_clamped(thr, 1, 0, len - act_start);
  22518. #if defined(DUK_USE_NONSTD_ARRAY_SPLICE_DELCOUNT)
  22519. } else {
  22520. /* E5.1 standard behavior when deleteCount is not given would be
  22521. * to treat it just like if 'undefined' was given, which coerces
  22522. * ultimately to 0. Real world behavior is to splice to the end
  22523. * of array, see test-bi-array-proto-splice-no-delcount.js.
  22524. */
  22525. del_count = len - act_start;
  22526. }
  22527. #endif
  22528. DUK_ASSERT(nargs >= 2);
  22529. item_count = (duk_int_t) (nargs - 2);
  22530. DUK_ASSERT(del_count >= 0 && del_count <= len - act_start);
  22531. DUK_ASSERT(del_count + act_start <= len);
  22532. /* For now, restrict result array into 32-bit length range. */
  22533. if (((duk_double_t) len) - ((duk_double_t) del_count) + ((duk_double_t) item_count) > (duk_double_t) DUK_UINT32_MAX) {
  22534. DUK_D(DUK_DPRINT("Array.prototype.splice() would go beyond 32-bit length, throw"));
  22535. DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
  22536. }
  22537. duk_push_array(thr);
  22538. /* stack[0] = start
  22539. * stack[1] = deleteCount
  22540. * stack[2...nargs-1] = items
  22541. * stack[nargs] = ToObject(this) -3
  22542. * stack[nargs+1] = ToUint32(length) -2
  22543. * stack[nargs+2] = result array -1
  22544. */
  22545. DUK_ASSERT_TOP(thr, nargs + 3);
  22546. /* Step 9: copy elements-to-be-deleted into the result array */
  22547. for (i = 0; i < del_count; i++) {
  22548. if (duk_get_prop_index(thr, -3, (duk_uarridx_t) (act_start + i))) {
  22549. duk_xdef_prop_index_wec(thr, -2, (duk_uarridx_t) i); /* throw flag irrelevant (false in std alg) */
  22550. } else {
  22551. duk_pop_undefined(thr);
  22552. }
  22553. }
  22554. duk_push_u32(thr, (duk_uint32_t) del_count);
  22555. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  22556. /* Steps 12 and 13: reorganize elements to make room for itemCount elements */
  22557. if (item_count < del_count) {
  22558. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 1
  22559. * -> [ A B F G H ] (conceptual intermediate step)
  22560. * -> [ A B . F G H ] (placeholder marked)
  22561. * [ A B C F G H ] (actual result at this point, C will be replaced)
  22562. */
  22563. DUK_ASSERT_TOP(thr, nargs + 3);
  22564. n = len - del_count;
  22565. for (i = act_start; i < n; i++) {
  22566. if (duk_get_prop_index(thr, -3, (duk_uarridx_t) (i + del_count))) {
  22567. duk_put_prop_index(thr, -4, (duk_uarridx_t) (i + item_count));
  22568. } else {
  22569. duk_pop_undefined(thr);
  22570. duk_del_prop_index(thr, -3, (duk_uarridx_t) (i + item_count));
  22571. }
  22572. }
  22573. DUK_ASSERT_TOP(thr, nargs + 3);
  22574. /* loop iterator init and limit changed from standard algorithm */
  22575. n = len - del_count + item_count;
  22576. for (i = len - 1; i >= n; i--) {
  22577. duk_del_prop_index(thr, -3, (duk_uarridx_t) i);
  22578. }
  22579. DUK_ASSERT_TOP(thr, nargs + 3);
  22580. } else if (item_count > del_count) {
  22581. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 4
  22582. * -> [ A B F G H ] (conceptual intermediate step)
  22583. * -> [ A B . . . . F G H ] (placeholder marked)
  22584. * [ A B C D E F F G H ] (actual result at this point)
  22585. */
  22586. DUK_ASSERT_TOP(thr, nargs + 3);
  22587. /* loop iterator init and limit changed from standard algorithm */
  22588. for (i = len - del_count - 1; i >= act_start; i--) {
  22589. if (duk_get_prop_index(thr, -3, (duk_uarridx_t) (i + del_count))) {
  22590. duk_put_prop_index(thr, -4, (duk_uarridx_t) (i + item_count));
  22591. } else {
  22592. duk_pop_undefined(thr);
  22593. duk_del_prop_index(thr, -3, (duk_uarridx_t) (i + item_count));
  22594. }
  22595. }
  22596. DUK_ASSERT_TOP(thr, nargs + 3);
  22597. } else {
  22598. /* [ A B C D E F G H ] rel_index = 2, del_count 3, item count 3
  22599. * -> [ A B F G H ] (conceptual intermediate step)
  22600. * -> [ A B . . . F G H ] (placeholder marked)
  22601. * [ A B C D E F G H ] (actual result at this point)
  22602. */
  22603. }
  22604. DUK_ASSERT_TOP(thr, nargs + 3);
  22605. /* Step 15: insert itemCount elements into the hole made above */
  22606. for (i = 0; i < item_count; i++) {
  22607. duk_dup(thr, i + 2); /* args start at index 2 */
  22608. duk_put_prop_index(thr, -4, (duk_uarridx_t) (act_start + i));
  22609. }
  22610. /* Step 16: update length; note that the final length may be above 32 bit range
  22611. * (but we checked above that this isn't the case here)
  22612. */
  22613. duk_push_u32(thr, (duk_uint32_t) (len - del_count + item_count));
  22614. duk_put_prop_stridx_short(thr, -4, DUK_STRIDX_LENGTH);
  22615. /* result array is already at the top of stack */
  22616. DUK_ASSERT_TOP(thr, nargs + 3);
  22617. return 1;
  22618. }
  22619. /*
  22620. * reverse()
  22621. */
  22622. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reverse(duk_hthread *thr) {
  22623. duk_uint32_t len;
  22624. duk_uint32_t middle;
  22625. duk_uint32_t lower, upper;
  22626. duk_bool_t have_lower, have_upper;
  22627. len = duk__push_this_obj_len_u32(thr);
  22628. middle = len / 2;
  22629. /* If len <= 1, middle will be 0 and for-loop bails out
  22630. * immediately (0 < 0 -> false).
  22631. */
  22632. for (lower = 0; lower < middle; lower++) {
  22633. DUK_ASSERT(len >= 2);
  22634. DUK_ASSERT_TOP(thr, 2);
  22635. DUK_ASSERT(len >= lower + 1);
  22636. upper = len - lower - 1;
  22637. have_lower = duk_get_prop_index(thr, -2, (duk_uarridx_t) lower);
  22638. have_upper = duk_get_prop_index(thr, -3, (duk_uarridx_t) upper);
  22639. /* [ ToObject(this) ToUint32(length) lowerValue upperValue ] */
  22640. if (have_upper) {
  22641. duk_put_prop_index(thr, -4, (duk_uarridx_t) lower);
  22642. } else {
  22643. duk_del_prop_index(thr, -4, (duk_uarridx_t) lower);
  22644. duk_pop_undefined(thr);
  22645. }
  22646. if (have_lower) {
  22647. duk_put_prop_index(thr, -3, (duk_uarridx_t) upper);
  22648. } else {
  22649. duk_del_prop_index(thr, -3, (duk_uarridx_t) upper);
  22650. duk_pop_undefined(thr);
  22651. }
  22652. DUK_ASSERT_TOP(thr, 2);
  22653. }
  22654. DUK_ASSERT_TOP(thr, 2);
  22655. duk_pop_unsafe(thr); /* -> [ ToObject(this) ] */
  22656. return 1;
  22657. }
  22658. /*
  22659. * slice()
  22660. */
  22661. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_slice(duk_hthread *thr) {
  22662. duk_uint32_t len_u32;
  22663. duk_int_t len;
  22664. duk_int_t start, end;
  22665. duk_int_t i;
  22666. duk_uarridx_t idx;
  22667. duk_uint32_t res_length = 0;
  22668. /* XXX: len >= 0x80000000 won't work below because we need to be
  22669. * able to represent -len.
  22670. */
  22671. len_u32 = duk__push_this_obj_len_u32_limited(thr);
  22672. len = (duk_int_t) len_u32;
  22673. DUK_ASSERT(len >= 0);
  22674. duk_push_array(thr);
  22675. /* stack[0] = start
  22676. * stack[1] = end
  22677. * stack[2] = ToObject(this)
  22678. * stack[3] = ToUint32(length)
  22679. * stack[4] = result array
  22680. */
  22681. start = duk_to_int_clamped(thr, 0, -len, len);
  22682. if (start < 0) {
  22683. start = len + start;
  22684. }
  22685. /* XXX: could duk_is_undefined() provide defaulting undefined to 'len'
  22686. * (the upper limit)?
  22687. */
  22688. if (duk_is_undefined(thr, 1)) {
  22689. end = len;
  22690. } else {
  22691. end = duk_to_int_clamped(thr, 1, -len, len);
  22692. if (end < 0) {
  22693. end = len + end;
  22694. }
  22695. }
  22696. DUK_ASSERT(start >= 0 && start <= len);
  22697. DUK_ASSERT(end >= 0 && end <= len);
  22698. idx = 0;
  22699. for (i = start; i < end; i++) {
  22700. DUK_ASSERT_TOP(thr, 5);
  22701. if (duk_get_prop_index(thr, 2, (duk_uarridx_t) i)) {
  22702. duk_xdef_prop_index_wec(thr, 4, idx);
  22703. res_length = idx + 1;
  22704. } else {
  22705. duk_pop_undefined(thr);
  22706. }
  22707. idx++;
  22708. DUK_ASSERT_TOP(thr, 5);
  22709. }
  22710. duk_push_u32(thr, res_length);
  22711. duk_xdef_prop_stridx_short(thr, 4, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  22712. DUK_ASSERT_TOP(thr, 5);
  22713. return 1;
  22714. }
  22715. /*
  22716. * shift()
  22717. */
  22718. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_shift(duk_hthread *thr) {
  22719. duk_uint32_t len;
  22720. duk_uint32_t i;
  22721. len = duk__push_this_obj_len_u32(thr);
  22722. if (len == 0) {
  22723. duk_push_int(thr, 0);
  22724. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LENGTH);
  22725. return 0;
  22726. }
  22727. duk_get_prop_index(thr, 0, 0);
  22728. /* stack[0] = object (this)
  22729. * stack[1] = ToUint32(length)
  22730. * stack[2] = elem at index 0 (retval)
  22731. */
  22732. for (i = 1; i < len; i++) {
  22733. DUK_ASSERT_TOP(thr, 3);
  22734. if (duk_get_prop_index(thr, 0, (duk_uarridx_t) i)) {
  22735. /* fromPresent = true */
  22736. duk_put_prop_index(thr, 0, (duk_uarridx_t) (i - 1));
  22737. } else {
  22738. /* fromPresent = false */
  22739. duk_del_prop_index(thr, 0, (duk_uarridx_t) (i - 1));
  22740. duk_pop_undefined(thr);
  22741. }
  22742. }
  22743. duk_del_prop_index(thr, 0, (duk_uarridx_t) (len - 1));
  22744. duk_push_u32(thr, (duk_uint32_t) (len - 1));
  22745. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LENGTH);
  22746. DUK_ASSERT_TOP(thr, 3);
  22747. return 1;
  22748. }
  22749. /*
  22750. * unshift()
  22751. */
  22752. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_unshift(duk_hthread *thr) {
  22753. duk_idx_t nargs;
  22754. duk_uint32_t len;
  22755. duk_uint32_t i;
  22756. nargs = duk_get_top(thr);
  22757. len = duk__push_this_obj_len_u32(thr);
  22758. /* stack[0...nargs-1] = unshift args (vararg)
  22759. * stack[nargs] = ToObject(this)
  22760. * stack[nargs+1] = ToUint32(length)
  22761. */
  22762. DUK_ASSERT_TOP(thr, nargs + 2);
  22763. /* Note: unshift() may operate on indices above unsigned 32-bit range
  22764. * and the final length may be >= 2**32. However, we restrict the
  22765. * final result to 32-bit range for practicality.
  22766. */
  22767. if (len + (duk_uint32_t) nargs < len) {
  22768. DUK_D(DUK_DPRINT("Array.prototype.unshift() would go beyond 32-bit length, throw"));
  22769. DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
  22770. }
  22771. i = len;
  22772. while (i > 0) {
  22773. DUK_ASSERT_TOP(thr, nargs + 2);
  22774. i--;
  22775. /* k+argCount-1; note that may be above 32-bit range */
  22776. if (duk_get_prop_index(thr, -2, (duk_uarridx_t) i)) {
  22777. /* fromPresent = true */
  22778. /* [ ... ToObject(this) ToUint32(length) val ] */
  22779. duk_put_prop_index(thr, -3, (duk_uarridx_t) (i + (duk_uint32_t) nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */
  22780. } else {
  22781. /* fromPresent = false */
  22782. /* [ ... ToObject(this) ToUint32(length) val ] */
  22783. duk_pop_undefined(thr);
  22784. duk_del_prop_index(thr, -2, (duk_uarridx_t) (i + (duk_uint32_t) nargs)); /* -> [ ... ToObject(this) ToUint32(length) ] */
  22785. }
  22786. DUK_ASSERT_TOP(thr, nargs + 2);
  22787. }
  22788. for (i = 0; i < (duk_uint32_t) nargs; i++) {
  22789. DUK_ASSERT_TOP(thr, nargs + 2);
  22790. duk_dup(thr, (duk_idx_t) i); /* -> [ ... ToObject(this) ToUint32(length) arg[i] ] */
  22791. duk_put_prop_index(thr, -3, (duk_uarridx_t) i);
  22792. DUK_ASSERT_TOP(thr, nargs + 2);
  22793. }
  22794. DUK_ASSERT_TOP(thr, nargs + 2);
  22795. duk_push_u32(thr, len + (duk_uint32_t) nargs);
  22796. duk_dup_top(thr); /* -> [ ... ToObject(this) ToUint32(length) final_len final_len ] */
  22797. duk_put_prop_stridx_short(thr, -4, DUK_STRIDX_LENGTH);
  22798. return 1;
  22799. }
  22800. /*
  22801. * indexOf(), lastIndexOf()
  22802. */
  22803. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_indexof_shared(duk_hthread *thr) {
  22804. duk_idx_t nargs;
  22805. duk_int_t i, len;
  22806. duk_int_t from_idx;
  22807. duk_small_int_t idx_step = duk_get_current_magic(thr); /* idx_step is +1 for indexOf, -1 for lastIndexOf */
  22808. /* lastIndexOf() needs to be a vararg function because we must distinguish
  22809. * between an undefined fromIndex and a "not given" fromIndex; indexOf() is
  22810. * made vararg for symmetry although it doesn't strictly need to be.
  22811. */
  22812. nargs = duk_get_top(thr);
  22813. duk_set_top(thr, 2);
  22814. /* XXX: must be able to represent -len */
  22815. len = (duk_int_t) duk__push_this_obj_len_u32_limited(thr);
  22816. if (len == 0) {
  22817. goto not_found;
  22818. }
  22819. /* Index clamping is a bit tricky, we must ensure that we'll only iterate
  22820. * through elements that exist and that the specific requirements from E5.1
  22821. * Sections 15.4.4.14 and 15.4.4.15 are fulfilled; especially:
  22822. *
  22823. * - indexOf: clamp to [-len,len], negative handling -> [0,len],
  22824. * if clamped result is len, for-loop bails out immediately
  22825. *
  22826. * - lastIndexOf: clamp to [-len-1, len-1], negative handling -> [-1, len-1],
  22827. * if clamped result is -1, for-loop bails out immediately
  22828. *
  22829. * If fromIndex is not given, ToInteger(undefined) = 0, which is correct
  22830. * for indexOf() but incorrect for lastIndexOf(). Hence special handling,
  22831. * and why lastIndexOf() needs to be a vararg function.
  22832. */
  22833. if (nargs >= 2) {
  22834. /* indexOf: clamp fromIndex to [-len, len]
  22835. * (if fromIndex == len, for-loop terminates directly)
  22836. *
  22837. * lastIndexOf: clamp fromIndex to [-len - 1, len - 1]
  22838. * (if clamped to -len-1 -> fromIndex becomes -1, terminates for-loop directly)
  22839. */
  22840. from_idx = duk_to_int_clamped(thr,
  22841. 1,
  22842. (idx_step > 0 ? -len : -len - 1),
  22843. (idx_step > 0 ? len : len - 1));
  22844. if (from_idx < 0) {
  22845. /* for lastIndexOf, result may be -1 (mark immediate termination) */
  22846. from_idx = len + from_idx;
  22847. }
  22848. } else {
  22849. /* for indexOf, ToInteger(undefined) would be 0, i.e. correct, but
  22850. * handle both indexOf and lastIndexOf specially here.
  22851. */
  22852. if (idx_step > 0) {
  22853. from_idx = 0;
  22854. } else {
  22855. from_idx = len - 1;
  22856. }
  22857. }
  22858. /* stack[0] = searchElement
  22859. * stack[1] = fromIndex
  22860. * stack[2] = object
  22861. * stack[3] = length (not needed, but not popped above)
  22862. */
  22863. for (i = from_idx; i >= 0 && i < len; i += idx_step) {
  22864. DUK_ASSERT_TOP(thr, 4);
  22865. if (duk_get_prop_index(thr, 2, (duk_uarridx_t) i)) {
  22866. DUK_ASSERT_TOP(thr, 5);
  22867. if (duk_strict_equals(thr, 0, 4)) {
  22868. duk_push_int(thr, i);
  22869. return 1;
  22870. }
  22871. }
  22872. duk_pop_unsafe(thr);
  22873. }
  22874. not_found:
  22875. duk_push_int(thr, -1);
  22876. return 1;
  22877. }
  22878. /*
  22879. * every(), some(), forEach(), map(), filter()
  22880. */
  22881. #define DUK__ITER_EVERY 0
  22882. #define DUK__ITER_SOME 1
  22883. #define DUK__ITER_FOREACH 2
  22884. #define DUK__ITER_MAP 3
  22885. #define DUK__ITER_FILTER 4
  22886. /* XXX: This helper is a bit awkward because the handling for the different iteration
  22887. * callers is quite different. This now compiles to a bit less than 500 bytes, so with
  22888. * 5 callers the net result is about 100 bytes / caller.
  22889. */
  22890. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_iter_shared(duk_hthread *thr) {
  22891. duk_uint32_t len;
  22892. duk_uint32_t i;
  22893. duk_uarridx_t k;
  22894. duk_bool_t bval;
  22895. duk_small_int_t iter_type = duk_get_current_magic(thr);
  22896. duk_uint32_t res_length = 0;
  22897. /* each call this helper serves has nargs==2 */
  22898. DUK_ASSERT_TOP(thr, 2);
  22899. len = duk__push_this_obj_len_u32(thr);
  22900. duk_require_callable(thr, 0);
  22901. /* if thisArg not supplied, behave as if undefined was supplied */
  22902. if (iter_type == DUK__ITER_MAP || iter_type == DUK__ITER_FILTER) {
  22903. duk_push_array(thr);
  22904. } else {
  22905. duk_push_undefined(thr);
  22906. }
  22907. /* stack[0] = callback
  22908. * stack[1] = thisArg
  22909. * stack[2] = object
  22910. * stack[3] = ToUint32(length) (unused, but avoid unnecessary pop)
  22911. * stack[4] = result array (or undefined)
  22912. */
  22913. k = 0; /* result index for filter() */
  22914. for (i = 0; i < len; i++) {
  22915. DUK_ASSERT_TOP(thr, 5);
  22916. if (!duk_get_prop_index(thr, 2, (duk_uarridx_t) i)) {
  22917. /* For 'map' trailing missing elements don't invoke the
  22918. * callback but count towards the result length.
  22919. */
  22920. if (iter_type == DUK__ITER_MAP) {
  22921. res_length = i + 1;
  22922. }
  22923. duk_pop_undefined(thr);
  22924. continue;
  22925. }
  22926. /* The original value needs to be preserved for filter(), hence
  22927. * this funny order. We can't re-get the value because of side
  22928. * effects.
  22929. */
  22930. duk_dup_0(thr);
  22931. duk_dup_1(thr);
  22932. duk_dup_m3(thr);
  22933. duk_push_u32(thr, i);
  22934. duk_dup_2(thr); /* [ ... val callback thisArg val i obj ] */
  22935. duk_call_method(thr, 3); /* -> [ ... val retval ] */
  22936. switch (iter_type) {
  22937. case DUK__ITER_EVERY:
  22938. bval = duk_to_boolean(thr, -1);
  22939. if (!bval) {
  22940. /* stack top contains 'false' */
  22941. return 1;
  22942. }
  22943. break;
  22944. case DUK__ITER_SOME:
  22945. bval = duk_to_boolean(thr, -1);
  22946. if (bval) {
  22947. /* stack top contains 'true' */
  22948. return 1;
  22949. }
  22950. break;
  22951. case DUK__ITER_FOREACH:
  22952. /* nop */
  22953. break;
  22954. case DUK__ITER_MAP:
  22955. duk_dup_top(thr);
  22956. duk_xdef_prop_index_wec(thr, 4, (duk_uarridx_t) i); /* retval to result[i] */
  22957. res_length = i + 1;
  22958. break;
  22959. case DUK__ITER_FILTER:
  22960. bval = duk_to_boolean(thr, -1);
  22961. if (bval) {
  22962. duk_dup_m2(thr); /* orig value */
  22963. duk_xdef_prop_index_wec(thr, 4, (duk_uarridx_t) k);
  22964. k++;
  22965. res_length = k;
  22966. }
  22967. break;
  22968. default:
  22969. DUK_UNREACHABLE();
  22970. break;
  22971. }
  22972. duk_pop_2_unsafe(thr);
  22973. DUK_ASSERT_TOP(thr, 5);
  22974. }
  22975. switch (iter_type) {
  22976. case DUK__ITER_EVERY:
  22977. duk_push_true(thr);
  22978. break;
  22979. case DUK__ITER_SOME:
  22980. duk_push_false(thr);
  22981. break;
  22982. case DUK__ITER_FOREACH:
  22983. duk_push_undefined(thr);
  22984. break;
  22985. case DUK__ITER_MAP:
  22986. case DUK__ITER_FILTER:
  22987. DUK_ASSERT_TOP(thr, 5);
  22988. DUK_ASSERT(duk_is_array(thr, -1)); /* topmost element is the result array already */
  22989. duk_push_u32(thr, res_length);
  22990. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_W);
  22991. break;
  22992. default:
  22993. DUK_UNREACHABLE();
  22994. break;
  22995. }
  22996. return 1;
  22997. }
  22998. /*
  22999. * reduce(), reduceRight()
  23000. */
  23001. DUK_INTERNAL duk_ret_t duk_bi_array_prototype_reduce_shared(duk_hthread *thr) {
  23002. duk_idx_t nargs;
  23003. duk_bool_t have_acc;
  23004. duk_uint32_t i, len;
  23005. duk_small_int_t idx_step = duk_get_current_magic(thr); /* idx_step is +1 for reduce, -1 for reduceRight */
  23006. /* We're a varargs function because we need to detect whether
  23007. * initialValue was given or not.
  23008. */
  23009. nargs = duk_get_top(thr);
  23010. DUK_DDD(DUK_DDDPRINT("nargs=%ld", (long) nargs));
  23011. duk_set_top(thr, 2);
  23012. len = duk__push_this_obj_len_u32(thr);
  23013. duk_require_callable(thr, 0);
  23014. /* stack[0] = callback fn
  23015. * stack[1] = initialValue
  23016. * stack[2] = object (coerced this)
  23017. * stack[3] = length (not needed, but not popped above)
  23018. * stack[4] = accumulator
  23019. */
  23020. have_acc = 0;
  23021. if (nargs >= 2) {
  23022. duk_dup_1(thr);
  23023. have_acc = 1;
  23024. }
  23025. DUK_DDD(DUK_DDDPRINT("have_acc=%ld, acc=%!T",
  23026. (long) have_acc, (duk_tval *) duk_get_tval(thr, 3)));
  23027. /* For len == 0, i is initialized to len - 1 which underflows.
  23028. * The condition (i < len) will then exit the for-loop on the
  23029. * first round which is correct. Similarly, loop termination
  23030. * happens by i underflowing.
  23031. */
  23032. for (i = (idx_step >= 0 ? 0 : len - 1);
  23033. i < len; /* i >= 0 would always be true */
  23034. i += (duk_uint32_t) idx_step) {
  23035. DUK_DDD(DUK_DDDPRINT("i=%ld, len=%ld, have_acc=%ld, top=%ld, acc=%!T",
  23036. (long) i, (long) len, (long) have_acc,
  23037. (long) duk_get_top(thr),
  23038. (duk_tval *) duk_get_tval(thr, 4)));
  23039. DUK_ASSERT((have_acc && duk_get_top(thr) == 5) ||
  23040. (!have_acc && duk_get_top(thr) == 4));
  23041. if (!duk_has_prop_index(thr, 2, (duk_uarridx_t) i)) {
  23042. continue;
  23043. }
  23044. if (!have_acc) {
  23045. DUK_ASSERT_TOP(thr, 4);
  23046. duk_get_prop_index(thr, 2, (duk_uarridx_t) i);
  23047. have_acc = 1;
  23048. DUK_ASSERT_TOP(thr, 5);
  23049. } else {
  23050. DUK_ASSERT_TOP(thr, 5);
  23051. duk_dup_0(thr);
  23052. duk_dup(thr, 4);
  23053. duk_get_prop_index(thr, 2, (duk_uarridx_t) i);
  23054. duk_push_u32(thr, i);
  23055. duk_dup_2(thr);
  23056. DUK_DDD(DUK_DDDPRINT("calling reduce function: func=%!T, prev=%!T, curr=%!T, idx=%!T, obj=%!T",
  23057. (duk_tval *) duk_get_tval(thr, -5), (duk_tval *) duk_get_tval(thr, -4),
  23058. (duk_tval *) duk_get_tval(thr, -3), (duk_tval *) duk_get_tval(thr, -2),
  23059. (duk_tval *) duk_get_tval(thr, -1)));
  23060. duk_call(thr, 4);
  23061. DUK_DDD(DUK_DDDPRINT("-> result: %!T", (duk_tval *) duk_get_tval(thr, -1)));
  23062. duk_replace(thr, 4);
  23063. DUK_ASSERT_TOP(thr, 5);
  23064. }
  23065. }
  23066. if (!have_acc) {
  23067. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  23068. }
  23069. DUK_ASSERT_TOP(thr, 5);
  23070. return 1;
  23071. }
  23072. #endif /* DUK_USE_ARRAY_BUILTIN */
  23073. /* automatic undefs */
  23074. #undef DUK__ARRAY_MID_JOIN_LIMIT
  23075. #undef DUK__ITER_EVERY
  23076. #undef DUK__ITER_FILTER
  23077. #undef DUK__ITER_FOREACH
  23078. #undef DUK__ITER_MAP
  23079. #undef DUK__ITER_SOME
  23080. #line 1 "duk_bi_boolean.c"
  23081. /*
  23082. * Boolean built-ins
  23083. */
  23084. /* #include duk_internal.h -> already included */
  23085. #if defined(DUK_USE_BOOLEAN_BUILTIN)
  23086. /* Shared helper to provide toString() and valueOf(). Checks 'this', gets
  23087. * the primitive value to stack top, and optionally coerces with ToString().
  23088. */
  23089. DUK_INTERNAL duk_ret_t duk_bi_boolean_prototype_tostring_shared(duk_hthread *thr) {
  23090. duk_tval *tv;
  23091. duk_hobject *h;
  23092. duk_small_int_t coerce_tostring = duk_get_current_magic(thr);
  23093. /* XXX: there is room to use a shared helper here, many built-ins
  23094. * check the 'this' type, and if it's an object, check its class,
  23095. * then get its internal value, etc.
  23096. */
  23097. duk_push_this(thr);
  23098. tv = duk_get_tval(thr, -1);
  23099. DUK_ASSERT(tv != NULL);
  23100. if (DUK_TVAL_IS_BOOLEAN(tv)) {
  23101. goto type_ok;
  23102. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  23103. h = DUK_TVAL_GET_OBJECT(tv);
  23104. DUK_ASSERT(h != NULL);
  23105. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_BOOLEAN) {
  23106. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
  23107. DUK_ASSERT(duk_is_boolean(thr, -1));
  23108. goto type_ok;
  23109. }
  23110. }
  23111. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  23112. /* never here */
  23113. type_ok:
  23114. if (coerce_tostring) {
  23115. duk_to_string(thr, -1);
  23116. }
  23117. return 1;
  23118. }
  23119. DUK_INTERNAL duk_ret_t duk_bi_boolean_constructor(duk_hthread *thr) {
  23120. duk_hobject *h_this;
  23121. duk_to_boolean(thr, 0);
  23122. if (duk_is_constructor_call(thr)) {
  23123. /* XXX: helper; rely on Boolean.prototype as being non-writable, non-configurable */
  23124. duk_push_this(thr);
  23125. h_this = duk_known_hobject(thr, -1);
  23126. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE]);
  23127. DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_BOOLEAN);
  23128. duk_dup_0(thr); /* -> [ val obj val ] */
  23129. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE); /* XXX: proper flags? */
  23130. } /* unbalanced stack */
  23131. return 1;
  23132. }
  23133. #endif /* DUK_USE_BOOLEAN_BUILTIN */
  23134. #line 1 "duk_bi_buffer.c"
  23135. /*
  23136. * ES2015 TypedArray and Node.js Buffer built-ins
  23137. */
  23138. /* #include duk_internal.h -> already included */
  23139. /*
  23140. * Helpers for buffer handling, enabled with DUK_USE_BUFFEROBJECT_SUPPORT.
  23141. */
  23142. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23143. /* Map class number (minus DUK_HOBJECT_CLASS_BUFOBJ_MIN) to a bidx for the
  23144. * default internal prototype.
  23145. */
  23146. static const duk_uint8_t duk__buffer_proto_from_classnum[] = {
  23147. DUK_BIDX_ARRAYBUFFER_PROTOTYPE,
  23148. DUK_BIDX_DATAVIEW_PROTOTYPE,
  23149. DUK_BIDX_INT8ARRAY_PROTOTYPE,
  23150. DUK_BIDX_UINT8ARRAY_PROTOTYPE,
  23151. DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE,
  23152. DUK_BIDX_INT16ARRAY_PROTOTYPE,
  23153. DUK_BIDX_UINT16ARRAY_PROTOTYPE,
  23154. DUK_BIDX_INT32ARRAY_PROTOTYPE,
  23155. DUK_BIDX_UINT32ARRAY_PROTOTYPE,
  23156. DUK_BIDX_FLOAT32ARRAY_PROTOTYPE,
  23157. DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
  23158. };
  23159. /* Map DUK_HBUFOBJ_ELEM_xxx to duk_hobject class number.
  23160. * Sync with duk_hbufobj.h and duk_hobject.h.
  23161. */
  23162. static const duk_uint8_t duk__buffer_class_from_elemtype[9] = {
  23163. DUK_HOBJECT_CLASS_UINT8ARRAY,
  23164. DUK_HOBJECT_CLASS_UINT8CLAMPEDARRAY,
  23165. DUK_HOBJECT_CLASS_INT8ARRAY,
  23166. DUK_HOBJECT_CLASS_UINT16ARRAY,
  23167. DUK_HOBJECT_CLASS_INT16ARRAY,
  23168. DUK_HOBJECT_CLASS_UINT32ARRAY,
  23169. DUK_HOBJECT_CLASS_INT32ARRAY,
  23170. DUK_HOBJECT_CLASS_FLOAT32ARRAY,
  23171. DUK_HOBJECT_CLASS_FLOAT64ARRAY
  23172. };
  23173. /* Map DUK_HBUFOBJ_ELEM_xxx to prototype object built-in index.
  23174. * Sync with duk_hbufobj.h.
  23175. */
  23176. static const duk_uint8_t duk__buffer_proto_from_elemtype[9] = {
  23177. DUK_BIDX_UINT8ARRAY_PROTOTYPE,
  23178. DUK_BIDX_UINT8CLAMPEDARRAY_PROTOTYPE,
  23179. DUK_BIDX_INT8ARRAY_PROTOTYPE,
  23180. DUK_BIDX_UINT16ARRAY_PROTOTYPE,
  23181. DUK_BIDX_INT16ARRAY_PROTOTYPE,
  23182. DUK_BIDX_UINT32ARRAY_PROTOTYPE,
  23183. DUK_BIDX_INT32ARRAY_PROTOTYPE,
  23184. DUK_BIDX_FLOAT32ARRAY_PROTOTYPE,
  23185. DUK_BIDX_FLOAT64ARRAY_PROTOTYPE
  23186. };
  23187. /* Map DUK__FLD_xxx to byte size. */
  23188. static const duk_uint8_t duk__buffer_nbytes_from_fldtype[6] = {
  23189. 1, /* DUK__FLD_8BIT */
  23190. 2, /* DUK__FLD_16BIT */
  23191. 4, /* DUK__FLD_32BIT */
  23192. 4, /* DUK__FLD_FLOAT */
  23193. 8, /* DUK__FLD_DOUBLE */
  23194. 0 /* DUK__FLD_VARINT; not relevant here */
  23195. };
  23196. /* Bitfield for each DUK_HBUFOBJ_ELEM_xxx indicating which element types
  23197. * are compatible with a blind byte copy for the TypedArray set() method (also
  23198. * used for TypedArray constructor). Array index is target buffer elem type,
  23199. * bitfield indicates compatible source types. The types must have same byte
  23200. * size and they must be coercion compatible.
  23201. */
  23202. #if !defined(DUK_USE_PREFER_SIZE)
  23203. static duk_uint16_t duk__buffer_elemtype_copy_compatible[9] = {
  23204. /* xxx -> DUK_HBUFOBJ_ELEM_UINT8 */
  23205. (1U << DUK_HBUFOBJ_ELEM_UINT8) |
  23206. (1U << DUK_HBUFOBJ_ELEM_UINT8CLAMPED) |
  23207. (1U << DUK_HBUFOBJ_ELEM_INT8),
  23208. /* xxx -> DUK_HBUFOBJ_ELEM_UINT8CLAMPED
  23209. * Note: INT8 is -not- copy compatible, e.g. -1 would coerce to 0x00.
  23210. */
  23211. (1U << DUK_HBUFOBJ_ELEM_UINT8) |
  23212. (1U << DUK_HBUFOBJ_ELEM_UINT8CLAMPED),
  23213. /* xxx -> DUK_HBUFOBJ_ELEM_INT8 */
  23214. (1U << DUK_HBUFOBJ_ELEM_UINT8) |
  23215. (1U << DUK_HBUFOBJ_ELEM_UINT8CLAMPED) |
  23216. (1U << DUK_HBUFOBJ_ELEM_INT8),
  23217. /* xxx -> DUK_HBUFOBJ_ELEM_UINT16 */
  23218. (1U << DUK_HBUFOBJ_ELEM_UINT16) |
  23219. (1U << DUK_HBUFOBJ_ELEM_INT16),
  23220. /* xxx -> DUK_HBUFOBJ_ELEM_INT16 */
  23221. (1U << DUK_HBUFOBJ_ELEM_UINT16) |
  23222. (1U << DUK_HBUFOBJ_ELEM_INT16),
  23223. /* xxx -> DUK_HBUFOBJ_ELEM_UINT32 */
  23224. (1U << DUK_HBUFOBJ_ELEM_UINT32) |
  23225. (1U << DUK_HBUFOBJ_ELEM_INT32),
  23226. /* xxx -> DUK_HBUFOBJ_ELEM_INT32 */
  23227. (1U << DUK_HBUFOBJ_ELEM_UINT32) |
  23228. (1U << DUK_HBUFOBJ_ELEM_INT32),
  23229. /* xxx -> DUK_HBUFOBJ_ELEM_FLOAT32 */
  23230. (1U << DUK_HBUFOBJ_ELEM_FLOAT32),
  23231. /* xxx -> DUK_HBUFOBJ_ELEM_FLOAT64 */
  23232. (1U << DUK_HBUFOBJ_ELEM_FLOAT64)
  23233. };
  23234. #endif /* !DUK_USE_PREFER_SIZE */
  23235. DUK_LOCAL duk_hbufobj *duk__hbufobj_promote_this(duk_hthread *thr) {
  23236. duk_tval *tv_dst;
  23237. duk_hbufobj *res;
  23238. duk_push_this(thr);
  23239. DUK_ASSERT(duk_is_buffer(thr, -1));
  23240. res = (duk_hbufobj *) duk_to_hobject(thr, -1);
  23241. DUK_ASSERT_HBUFOBJ_VALID(res);
  23242. DUK_DD(DUK_DDPRINT("promoted 'this' automatically to an ArrayBuffer: %!iT", duk_get_tval(thr, -1)));
  23243. tv_dst = duk_get_borrowed_this_tval(thr);
  23244. DUK_TVAL_SET_OBJECT_UPDREF(thr, tv_dst, (duk_hobject *) res);
  23245. duk_pop(thr);
  23246. return res;
  23247. }
  23248. #define DUK__BUFOBJ_FLAG_THROW (1 << 0)
  23249. #define DUK__BUFOBJ_FLAG_PROMOTE (1 << 1)
  23250. /* Shared helper. When DUK__BUFOBJ_FLAG_PROMOTE is given, the return value is
  23251. * always a duk_hbufobj *. Without the flag the return value can also be a
  23252. * plain buffer, and the caller must check for it using DUK_HEAPHDR_IS_BUFFER().
  23253. */
  23254. DUK_LOCAL duk_heaphdr *duk__getrequire_bufobj_this(duk_hthread *thr, duk_small_uint_t flags) {
  23255. duk_tval *tv;
  23256. duk_hbufobj *h_this;
  23257. DUK_ASSERT(thr != NULL);
  23258. tv = duk_get_borrowed_this_tval(thr);
  23259. DUK_ASSERT(tv != NULL);
  23260. if (DUK_TVAL_IS_OBJECT(tv)) {
  23261. h_this = (duk_hbufobj *) DUK_TVAL_GET_OBJECT(tv);
  23262. DUK_ASSERT(h_this != NULL);
  23263. if (DUK_HOBJECT_IS_BUFOBJ((duk_hobject *) h_this)) {
  23264. DUK_ASSERT_HBUFOBJ_VALID(h_this);
  23265. return (duk_heaphdr *) h_this;
  23266. }
  23267. } else if (DUK_TVAL_IS_BUFFER(tv)) {
  23268. if (flags & DUK__BUFOBJ_FLAG_PROMOTE) {
  23269. /* Promote a plain buffer to a Uint8Array. This is very
  23270. * inefficient but allows plain buffer to be used wherever an
  23271. * Uint8Array is used with very small cost; hot path functions
  23272. * like index read/write calls should provide direct buffer
  23273. * support to avoid promotion.
  23274. */
  23275. /* XXX: make this conditional to a flag if call sites need it? */
  23276. h_this = duk__hbufobj_promote_this(thr);
  23277. DUK_ASSERT(h_this != NULL);
  23278. DUK_ASSERT_HBUFOBJ_VALID(h_this);
  23279. return (duk_heaphdr *) h_this;
  23280. } else {
  23281. /* XXX: ugly, share return pointer for duk_hbuffer. */
  23282. return (duk_heaphdr *) DUK_TVAL_GET_BUFFER(tv);
  23283. }
  23284. }
  23285. if (flags & DUK__BUFOBJ_FLAG_THROW) {
  23286. DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER);
  23287. DUK_WO_NORETURN(return NULL;);
  23288. }
  23289. return NULL;
  23290. }
  23291. /* Check that 'this' is a duk_hbufobj and return a pointer to it. */
  23292. DUK_LOCAL duk_hbufobj *duk__get_bufobj_this(duk_hthread *thr) {
  23293. return (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_PROMOTE);
  23294. }
  23295. /* Check that 'this' is a duk_hbufobj and return a pointer to it
  23296. * (NULL if not).
  23297. */
  23298. DUK_LOCAL duk_hbufobj *duk__require_bufobj_this(duk_hthread *thr) {
  23299. return (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_THROW | DUK__BUFOBJ_FLAG_PROMOTE);
  23300. }
  23301. /* Check that value is a duk_hbufobj and return a pointer to it. */
  23302. DUK_LOCAL duk_hbufobj *duk__require_bufobj_value(duk_hthread *thr, duk_idx_t idx) {
  23303. duk_tval *tv;
  23304. duk_hbufobj *h_obj;
  23305. /* Don't accept relative indices now. */
  23306. DUK_ASSERT(idx >= 0);
  23307. tv = duk_require_tval(thr, idx);
  23308. DUK_ASSERT(tv != NULL);
  23309. if (DUK_TVAL_IS_OBJECT(tv)) {
  23310. h_obj = (duk_hbufobj *) DUK_TVAL_GET_OBJECT(tv);
  23311. DUK_ASSERT(h_obj != NULL);
  23312. if (DUK_HOBJECT_IS_BUFOBJ((duk_hobject *) h_obj)) {
  23313. DUK_ASSERT_HBUFOBJ_VALID(h_obj);
  23314. return h_obj;
  23315. }
  23316. } else if (DUK_TVAL_IS_BUFFER(tv)) {
  23317. h_obj = (duk_hbufobj *) duk_to_hobject(thr, idx);
  23318. DUK_ASSERT(h_obj != NULL);
  23319. DUK_ASSERT_HBUFOBJ_VALID(h_obj);
  23320. return h_obj;
  23321. }
  23322. DUK_ERROR_TYPE(thr, DUK_STR_NOT_BUFFER);
  23323. DUK_WO_NORETURN(return NULL;);
  23324. }
  23325. DUK_LOCAL void duk__set_bufobj_buffer(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_hbuffer *h_val) {
  23326. DUK_ASSERT(thr != NULL);
  23327. DUK_ASSERT(h_bufobj != NULL);
  23328. DUK_ASSERT(h_bufobj->buf == NULL); /* no need to decref */
  23329. DUK_ASSERT(h_val != NULL);
  23330. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  23331. DUK_UNREF(thr);
  23332. h_bufobj->buf = h_val;
  23333. DUK_HBUFFER_INCREF(thr, h_val);
  23334. h_bufobj->length = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_val);
  23335. DUK_ASSERT(h_bufobj->shift == 0);
  23336. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFOBJ_ELEM_UINT8);
  23337. DUK_ASSERT(h_bufobj->is_typedarray == 0);
  23338. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  23339. }
  23340. /* Shared offset/length coercion helper. */
  23341. DUK_LOCAL void duk__resolve_offset_opt_length(duk_hthread *thr,
  23342. duk_hbufobj *h_bufarg,
  23343. duk_idx_t idx_offset,
  23344. duk_idx_t idx_length,
  23345. duk_uint_t *out_offset,
  23346. duk_uint_t *out_length,
  23347. duk_bool_t throw_flag) {
  23348. duk_int_t offset_signed;
  23349. duk_int_t length_signed;
  23350. duk_uint_t offset;
  23351. duk_uint_t length;
  23352. offset_signed = duk_to_int(thr, idx_offset);
  23353. if (offset_signed < 0) {
  23354. goto fail_range;
  23355. }
  23356. offset = (duk_uint_t) offset_signed;
  23357. if (offset > h_bufarg->length) {
  23358. goto fail_range;
  23359. }
  23360. DUK_ASSERT_DISABLE(offset >= 0); /* unsigned */
  23361. DUK_ASSERT(offset <= h_bufarg->length);
  23362. if (duk_is_undefined(thr, idx_length)) {
  23363. DUK_ASSERT(h_bufarg->length >= offset);
  23364. length = h_bufarg->length - offset; /* >= 0 */
  23365. } else {
  23366. length_signed = duk_to_int(thr, idx_length);
  23367. if (length_signed < 0) {
  23368. goto fail_range;
  23369. }
  23370. length = (duk_uint_t) length_signed;
  23371. DUK_ASSERT(h_bufarg->length >= offset);
  23372. if (length > h_bufarg->length - offset) {
  23373. /* Unlike for negative arguments, some call sites
  23374. * want length to be clamped if it's positive.
  23375. */
  23376. if (throw_flag) {
  23377. goto fail_range;
  23378. } else {
  23379. length = h_bufarg->length - offset;
  23380. }
  23381. }
  23382. }
  23383. DUK_ASSERT_DISABLE(length >= 0); /* unsigned */
  23384. DUK_ASSERT(offset + length <= h_bufarg->length);
  23385. *out_offset = offset;
  23386. *out_length = length;
  23387. return;
  23388. fail_range:
  23389. DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARGS);
  23390. DUK_WO_NORETURN(return;);
  23391. }
  23392. /* Shared lenient buffer length clamping helper. No negative indices, no
  23393. * element/byte shifting.
  23394. */
  23395. DUK_LOCAL void duk__clamp_startend_nonegidx_noshift(duk_hthread *thr,
  23396. duk_int_t buffer_length,
  23397. duk_idx_t idx_start,
  23398. duk_idx_t idx_end,
  23399. duk_int_t *out_start_offset,
  23400. duk_int_t *out_end_offset) {
  23401. duk_int_t start_offset;
  23402. duk_int_t end_offset;
  23403. DUK_ASSERT(out_start_offset != NULL);
  23404. DUK_ASSERT(out_end_offset != NULL);
  23405. /* undefined coerces to zero which is correct */
  23406. start_offset = duk_to_int_clamped(thr, idx_start, 0, buffer_length);
  23407. if (duk_is_undefined(thr, idx_end)) {
  23408. end_offset = buffer_length;
  23409. } else {
  23410. end_offset = duk_to_int_clamped(thr, idx_end, start_offset, buffer_length);
  23411. }
  23412. DUK_ASSERT(start_offset >= 0);
  23413. DUK_ASSERT(start_offset <= buffer_length);
  23414. DUK_ASSERT(end_offset >= 0);
  23415. DUK_ASSERT(end_offset <= buffer_length);
  23416. DUK_ASSERT(start_offset <= end_offset);
  23417. *out_start_offset = start_offset;
  23418. *out_end_offset = end_offset;
  23419. }
  23420. /* Shared lenient buffer length clamping helper. Indices are treated as
  23421. * element indices (though output values are byte offsets) which only
  23422. * really matters for TypedArray views as other buffer object have a zero
  23423. * shift. Negative indices are counted from end of input slice; crossed
  23424. * indices are clamped to zero length; and final indices are clamped
  23425. * against input slice. Used for e.g. ArrayBuffer slice().
  23426. */
  23427. DUK_LOCAL void duk__clamp_startend_negidx_shifted(duk_hthread *thr,
  23428. duk_int_t buffer_length,
  23429. duk_uint8_t buffer_shift,
  23430. duk_idx_t idx_start,
  23431. duk_idx_t idx_end,
  23432. duk_int_t *out_start_offset,
  23433. duk_int_t *out_end_offset) {
  23434. duk_int_t start_offset;
  23435. duk_int_t end_offset;
  23436. DUK_ASSERT(out_start_offset != NULL);
  23437. DUK_ASSERT(out_end_offset != NULL);
  23438. buffer_length >>= buffer_shift; /* as (full) elements */
  23439. /* Resolve start/end offset as element indices first; arguments
  23440. * at idx_start/idx_end are element offsets. Working with element
  23441. * indices first also avoids potential for wrapping.
  23442. */
  23443. start_offset = duk_to_int(thr, idx_start);
  23444. if (start_offset < 0) {
  23445. start_offset = buffer_length + start_offset;
  23446. }
  23447. if (duk_is_undefined(thr, idx_end)) {
  23448. end_offset = buffer_length;
  23449. } else {
  23450. end_offset = duk_to_int(thr, idx_end);
  23451. if (end_offset < 0) {
  23452. end_offset = buffer_length + end_offset;
  23453. }
  23454. }
  23455. /* Note: start_offset/end_offset can still be < 0 here. */
  23456. if (start_offset < 0) {
  23457. start_offset = 0;
  23458. } else if (start_offset > buffer_length) {
  23459. start_offset = buffer_length;
  23460. }
  23461. if (end_offset < start_offset) {
  23462. end_offset = start_offset;
  23463. } else if (end_offset > buffer_length) {
  23464. end_offset = buffer_length;
  23465. }
  23466. DUK_ASSERT(start_offset >= 0);
  23467. DUK_ASSERT(start_offset <= buffer_length);
  23468. DUK_ASSERT(end_offset >= 0);
  23469. DUK_ASSERT(end_offset <= buffer_length);
  23470. DUK_ASSERT(start_offset <= end_offset);
  23471. /* Convert indices to byte offsets. */
  23472. start_offset <<= buffer_shift;
  23473. end_offset <<= buffer_shift;
  23474. *out_start_offset = start_offset;
  23475. *out_end_offset = end_offset;
  23476. }
  23477. DUK_INTERNAL void duk_hbufobj_promote_plain(duk_hthread *thr, duk_idx_t idx) {
  23478. if (duk_is_buffer(thr, idx)) {
  23479. duk_to_object(thr, idx);
  23480. }
  23481. }
  23482. DUK_INTERNAL void duk_hbufobj_push_uint8array_from_plain(duk_hthread *thr, duk_hbuffer *h_buf) {
  23483. /* Push Uint8Array which will share the same underlying buffer as
  23484. * the plain buffer argument. Also create an ArrayBuffer with the
  23485. * same backing for the result .buffer property.
  23486. */
  23487. duk_push_hbuffer(thr, h_buf);
  23488. duk_push_buffer_object(thr, -1, 0, (duk_size_t) DUK_HBUFFER_GET_SIZE(h_buf), DUK_BUFOBJ_UINT8ARRAY);
  23489. duk_remove_m2(thr);
  23490. #if 0
  23491. /* More verbose equivalent; maybe useful if e.g. .buffer is omitted. */
  23492. h_bufobj = duk_push_bufobj_raw(thr,
  23493. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23494. DUK_HOBJECT_FLAG_BUFOBJ |
  23495. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_UINT8ARRAY),
  23496. DUK_BIDX_UINT8ARRAY_PROTOTYPE);
  23497. DUK_ASSERT(h_bufobj != NULL);
  23498. duk__set_bufobj_buffer(thr, h_bufobj, h_buf);
  23499. h_bufobj->is_typedarray = 1;
  23500. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  23501. h_arrbuf = duk_push_bufobj_raw(thr,
  23502. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23503. DUK_HOBJECT_FLAG_BUFOBJ |
  23504. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  23505. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  23506. DUK_ASSERT(h_arrbuf != NULL);
  23507. duk__set_bufobj_buffer(thr, h_arrbuf, h_buf);
  23508. DUK_ASSERT(h_arrbuf->is_typedarray == 0);
  23509. DUK_ASSERT_HBUFOBJ_VALID(h_arrbuf);
  23510. DUK_ASSERT(h_bufobj->buf_prop == NULL);
  23511. h_bufobj->buf_prop = (duk_hobject *) h_arrbuf;
  23512. DUK_ASSERT(h_arrbuf != NULL);
  23513. DUK_HBUFOBJ_INCREF(thr, h_arrbuf);
  23514. duk_pop(thr);
  23515. #endif
  23516. }
  23517. /* Indexed read helper for buffer objects, also called from outside this file. */
  23518. DUK_INTERNAL void duk_hbufobj_push_validated_read(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) {
  23519. duk_double_union du;
  23520. DUK_ASSERT(elem_size > 0);
  23521. duk_memcpy((void *) du.uc, (const void *) p, (size_t) elem_size);
  23522. switch (h_bufobj->elem_type) {
  23523. case DUK_HBUFOBJ_ELEM_UINT8:
  23524. case DUK_HBUFOBJ_ELEM_UINT8CLAMPED:
  23525. duk_push_uint(thr, (duk_uint_t) du.uc[0]);
  23526. break;
  23527. case DUK_HBUFOBJ_ELEM_INT8:
  23528. duk_push_int(thr, (duk_int_t) (duk_int8_t) du.uc[0]);
  23529. break;
  23530. case DUK_HBUFOBJ_ELEM_UINT16:
  23531. duk_push_uint(thr, (duk_uint_t) du.us[0]);
  23532. break;
  23533. case DUK_HBUFOBJ_ELEM_INT16:
  23534. duk_push_int(thr, (duk_int_t) (duk_int16_t) du.us[0]);
  23535. break;
  23536. case DUK_HBUFOBJ_ELEM_UINT32:
  23537. duk_push_uint(thr, (duk_uint_t) du.ui[0]);
  23538. break;
  23539. case DUK_HBUFOBJ_ELEM_INT32:
  23540. duk_push_int(thr, (duk_int_t) (duk_int32_t) du.ui[0]);
  23541. break;
  23542. case DUK_HBUFOBJ_ELEM_FLOAT32:
  23543. duk_push_number(thr, (duk_double_t) du.f[0]);
  23544. break;
  23545. case DUK_HBUFOBJ_ELEM_FLOAT64:
  23546. duk_push_number(thr, (duk_double_t) du.d);
  23547. break;
  23548. default:
  23549. DUK_UNREACHABLE();
  23550. }
  23551. }
  23552. /* Indexed write helper for buffer objects, also called from outside this file. */
  23553. DUK_INTERNAL void duk_hbufobj_validated_write(duk_hthread *thr, duk_hbufobj *h_bufobj, duk_uint8_t *p, duk_small_uint_t elem_size) {
  23554. duk_double_union du;
  23555. /* NOTE! Caller must ensure that any side effects from the
  23556. * coercions below are safe. If that cannot be guaranteed
  23557. * (which is normally the case), caller must coerce the
  23558. * argument using duk_to_number() before any pointer
  23559. * validations; the result of duk_to_number() always coerces
  23560. * without side effects here.
  23561. */
  23562. switch (h_bufobj->elem_type) {
  23563. case DUK_HBUFOBJ_ELEM_UINT8:
  23564. du.uc[0] = (duk_uint8_t) duk_to_uint32(thr, -1);
  23565. break;
  23566. case DUK_HBUFOBJ_ELEM_UINT8CLAMPED:
  23567. du.uc[0] = (duk_uint8_t) duk_to_uint8clamped(thr, -1);
  23568. break;
  23569. case DUK_HBUFOBJ_ELEM_INT8:
  23570. du.uc[0] = (duk_uint8_t) duk_to_int32(thr, -1);
  23571. break;
  23572. case DUK_HBUFOBJ_ELEM_UINT16:
  23573. du.us[0] = (duk_uint16_t) duk_to_uint32(thr, -1);
  23574. break;
  23575. case DUK_HBUFOBJ_ELEM_INT16:
  23576. du.us[0] = (duk_uint16_t) duk_to_int32(thr, -1);
  23577. break;
  23578. case DUK_HBUFOBJ_ELEM_UINT32:
  23579. du.ui[0] = (duk_uint32_t) duk_to_uint32(thr, -1);
  23580. break;
  23581. case DUK_HBUFOBJ_ELEM_INT32:
  23582. du.ui[0] = (duk_uint32_t) duk_to_int32(thr, -1);
  23583. break;
  23584. case DUK_HBUFOBJ_ELEM_FLOAT32:
  23585. /* A double-to-float cast is undefined behavior in C99 if
  23586. * the cast is out-of-range, so use a helper. Example:
  23587. * runtime error: value -1e+100 is outside the range of representable values of type 'float'
  23588. */
  23589. du.f[0] = duk_double_to_float_t(duk_to_number_m1(thr));
  23590. break;
  23591. case DUK_HBUFOBJ_ELEM_FLOAT64:
  23592. du.d = (duk_double_t) duk_to_number_m1(thr);
  23593. break;
  23594. default:
  23595. DUK_UNREACHABLE();
  23596. }
  23597. DUK_ASSERT(elem_size > 0);
  23598. duk_memcpy((void *) p, (const void *) du.uc, (size_t) elem_size);
  23599. }
  23600. /* Helper to create a fixed buffer from argument value at index 0.
  23601. * Node.js and allocPlain() compatible.
  23602. */
  23603. DUK_LOCAL duk_hbuffer *duk__hbufobj_fixed_from_argvalue(duk_hthread *thr) {
  23604. duk_int_t len;
  23605. duk_int_t i;
  23606. duk_size_t buf_size;
  23607. duk_uint8_t *buf;
  23608. switch (duk_get_type(thr, 0)) {
  23609. case DUK_TYPE_NUMBER: {
  23610. len = duk_to_int_clamped(thr, 0, 0, DUK_INT_MAX);
  23611. (void) duk_push_fixed_buffer_zero(thr, (duk_size_t) len);
  23612. break;
  23613. }
  23614. case DUK_TYPE_BUFFER: { /* Treat like Uint8Array. */
  23615. goto slow_copy;
  23616. }
  23617. case DUK_TYPE_OBJECT: {
  23618. duk_hobject *h;
  23619. duk_hbufobj *h_bufobj;
  23620. /* For Node.js Buffers "Passing an ArrayBuffer returns a Buffer
  23621. * that shares allocated memory with the given ArrayBuffer."
  23622. * https://nodejs.org/api/buffer.html#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe
  23623. */
  23624. h = duk_known_hobject(thr, 0);
  23625. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAYBUFFER) {
  23626. DUK_ASSERT(DUK_HOBJECT_IS_BUFOBJ(h));
  23627. h_bufobj = (duk_hbufobj *) h;
  23628. if (DUK_UNLIKELY(h_bufobj->buf == NULL)) {
  23629. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  23630. DUK_WO_NORETURN(return NULL;);
  23631. }
  23632. if (DUK_UNLIKELY(h_bufobj->offset != 0 || h_bufobj->length != DUK_HBUFFER_GET_SIZE(h_bufobj->buf))) {
  23633. /* No support for ArrayBuffers with slice
  23634. * offset/length.
  23635. */
  23636. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  23637. DUK_WO_NORETURN(return NULL;);
  23638. }
  23639. duk_push_hbuffer(thr, h_bufobj->buf);
  23640. return h_bufobj->buf;
  23641. }
  23642. goto slow_copy;
  23643. }
  23644. case DUK_TYPE_STRING: {
  23645. /* ignore encoding for now */
  23646. duk_require_hstring_notsymbol(thr, 0);
  23647. duk_dup_0(thr);
  23648. (void) duk_to_buffer(thr, -1, &buf_size);
  23649. break;
  23650. }
  23651. default:
  23652. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  23653. DUK_WO_NORETURN(return NULL;);
  23654. }
  23655. done:
  23656. DUK_ASSERT(duk_is_buffer(thr, -1));
  23657. return duk_known_hbuffer(thr, -1);
  23658. slow_copy:
  23659. /* XXX: fast path for typed arrays and other buffer objects? */
  23660. (void) duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_LENGTH);
  23661. len = duk_to_int_clamped(thr, -1, 0, DUK_INT_MAX);
  23662. duk_pop(thr);
  23663. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, (duk_size_t) len); /* no zeroing, all indices get initialized */
  23664. for (i = 0; i < len; i++) {
  23665. /* XXX: fast path for array or buffer arguments? */
  23666. duk_get_prop_index(thr, 0, (duk_uarridx_t) i);
  23667. buf[i] = (duk_uint8_t) (duk_to_uint32(thr, -1) & 0xffU);
  23668. duk_pop(thr);
  23669. }
  23670. goto done;
  23671. }
  23672. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23673. /*
  23674. * Node.js Buffer constructor
  23675. *
  23676. * Node.js Buffers are just Uint8Arrays with internal prototype set to
  23677. * Buffer.prototype so they're handled otherwise the same as Uint8Array.
  23678. * However, the constructor arguments are very different so a separate
  23679. * constructor entry point is used.
  23680. */
  23681. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23682. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_constructor(duk_hthread *thr) {
  23683. duk_hbuffer *h_buf;
  23684. h_buf = duk__hbufobj_fixed_from_argvalue(thr);
  23685. DUK_ASSERT(h_buf != NULL);
  23686. duk_push_buffer_object(thr,
  23687. -1,
  23688. 0,
  23689. DUK_HBUFFER_FIXED_GET_SIZE((duk_hbuffer_fixed *) (void *) h_buf),
  23690. DUK_BUFOBJ_UINT8ARRAY);
  23691. duk_push_hobject_bidx(thr, DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
  23692. duk_set_prototype(thr, -2);
  23693. /* XXX: a more direct implementation */
  23694. return 1;
  23695. }
  23696. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23697. /*
  23698. * ArrayBuffer, DataView, and TypedArray constructors
  23699. */
  23700. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23701. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_constructor(duk_hthread *thr) {
  23702. duk_hbufobj *h_bufobj;
  23703. duk_hbuffer *h_val;
  23704. duk_int_t len;
  23705. DUK_ASSERT_CTX_VALID(thr);
  23706. duk_require_constructor_call(thr);
  23707. len = duk_to_int(thr, 0);
  23708. if (len < 0) {
  23709. goto fail_length;
  23710. }
  23711. (void) duk_push_fixed_buffer_zero(thr, (duk_size_t) len);
  23712. h_val = (duk_hbuffer *) duk_known_hbuffer(thr, -1);
  23713. h_bufobj = duk_push_bufobj_raw(thr,
  23714. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23715. DUK_HOBJECT_FLAG_BUFOBJ |
  23716. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  23717. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  23718. DUK_ASSERT(h_bufobj != NULL);
  23719. duk__set_bufobj_buffer(thr, h_bufobj, h_val);
  23720. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  23721. return 1;
  23722. fail_length:
  23723. DUK_DCERROR_RANGE_INVALID_LENGTH(thr);
  23724. }
  23725. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  23726. /* Format of magic, bits:
  23727. * 0...1: elem size shift (0-3)
  23728. * 2...5: elem type (DUK_HBUFOBJ_ELEM_xxx)
  23729. *
  23730. * XXX: add prototype bidx explicitly to magic instead of using a mapping?
  23731. */
  23732. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  23733. DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_hthread *thr) {
  23734. duk_tval *tv;
  23735. duk_hobject *h_obj;
  23736. duk_hbufobj *h_bufobj = NULL;
  23737. duk_hbufobj *h_bufarg = NULL;
  23738. duk_hbuffer *h_val;
  23739. duk_small_uint_t magic;
  23740. duk_small_uint_t shift;
  23741. duk_small_uint_t elem_type;
  23742. duk_small_uint_t elem_size;
  23743. duk_small_uint_t class_num;
  23744. duk_small_uint_t proto_bidx;
  23745. duk_uint_t align_mask;
  23746. duk_uint_t elem_length;
  23747. duk_int_t elem_length_signed;
  23748. duk_uint_t byte_length;
  23749. duk_small_uint_t copy_mode;
  23750. /* XXX: The same copy helpers could be shared with at least some
  23751. * buffer functions.
  23752. */
  23753. duk_require_constructor_call(thr);
  23754. /* We could fit built-in index into magic but that'd make the magic
  23755. * number dependent on built-in numbering (genbuiltins.py doesn't
  23756. * handle that yet). So map both class and prototype from the
  23757. * element type.
  23758. */
  23759. magic = (duk_small_uint_t) duk_get_current_magic(thr);
  23760. shift = magic & 0x03U; /* bits 0...1: shift */
  23761. elem_type = (magic >> 2) & 0x0fU; /* bits 2...5: type */
  23762. elem_size = 1U << shift;
  23763. align_mask = elem_size - 1;
  23764. DUK_ASSERT(elem_type < sizeof(duk__buffer_proto_from_elemtype) / sizeof(duk_uint8_t));
  23765. proto_bidx = duk__buffer_proto_from_elemtype[elem_type];
  23766. DUK_ASSERT(proto_bidx < DUK_NUM_BUILTINS);
  23767. DUK_ASSERT(elem_type < sizeof(duk__buffer_class_from_elemtype) / sizeof(duk_uint8_t));
  23768. class_num = duk__buffer_class_from_elemtype[elem_type];
  23769. DUK_DD(DUK_DDPRINT("typedarray constructor, magic=%d, shift=%d, elem_type=%d, "
  23770. "elem_size=%d, proto_bidx=%d, class_num=%d",
  23771. (int) magic, (int) shift, (int) elem_type, (int) elem_size,
  23772. (int) proto_bidx, (int) class_num));
  23773. /* Argument variants. When the argument is an ArrayBuffer a view to
  23774. * the same buffer is created; otherwise a new ArrayBuffer is always
  23775. * created.
  23776. */
  23777. /* XXX: initial iteration to treat a plain buffer like an ArrayBuffer:
  23778. * coerce to an ArrayBuffer object and use that as .buffer. The underlying
  23779. * buffer will be the same but result .buffer !== inputPlainBuffer.
  23780. */
  23781. duk_hbufobj_promote_plain(thr, 0);
  23782. tv = duk_get_tval(thr, 0);
  23783. DUK_ASSERT(tv != NULL); /* arg count */
  23784. if (DUK_TVAL_IS_OBJECT(tv)) {
  23785. h_obj = DUK_TVAL_GET_OBJECT(tv);
  23786. DUK_ASSERT(h_obj != NULL);
  23787. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_ARRAYBUFFER) {
  23788. /* ArrayBuffer: unlike any other argument variant, create
  23789. * a view into the existing buffer.
  23790. */
  23791. duk_int_t byte_offset_signed;
  23792. duk_uint_t byte_offset;
  23793. h_bufarg = (duk_hbufobj *) h_obj;
  23794. byte_offset_signed = duk_to_int(thr, 1);
  23795. if (byte_offset_signed < 0) {
  23796. goto fail_arguments;
  23797. }
  23798. byte_offset = (duk_uint_t) byte_offset_signed;
  23799. if (byte_offset > h_bufarg->length ||
  23800. (byte_offset & align_mask) != 0) {
  23801. /* Must be >= 0 and multiple of element size. */
  23802. goto fail_arguments;
  23803. }
  23804. if (duk_is_undefined(thr, 2)) {
  23805. DUK_ASSERT(h_bufarg->length >= byte_offset);
  23806. byte_length = h_bufarg->length - byte_offset;
  23807. if ((byte_length & align_mask) != 0) {
  23808. /* Must be element size multiple from
  23809. * start offset to end of buffer.
  23810. */
  23811. goto fail_arguments;
  23812. }
  23813. elem_length = (byte_length >> shift);
  23814. } else {
  23815. elem_length_signed = duk_to_int(thr, 2);
  23816. if (elem_length_signed < 0) {
  23817. goto fail_arguments;
  23818. }
  23819. elem_length = (duk_uint_t) elem_length_signed;
  23820. byte_length = elem_length << shift;
  23821. if ((byte_length >> shift) != elem_length) {
  23822. /* Byte length would overflow. */
  23823. /* XXX: easier check with less code? */
  23824. goto fail_arguments;
  23825. }
  23826. DUK_ASSERT(h_bufarg->length >= byte_offset);
  23827. if (byte_length > h_bufarg->length - byte_offset) {
  23828. /* Not enough data. */
  23829. goto fail_arguments;
  23830. }
  23831. }
  23832. DUK_UNREF(elem_length);
  23833. DUK_ASSERT_DISABLE(byte_offset >= 0);
  23834. DUK_ASSERT(byte_offset <= h_bufarg->length);
  23835. DUK_ASSERT_DISABLE(byte_length >= 0);
  23836. DUK_ASSERT(byte_offset + byte_length <= h_bufarg->length);
  23837. DUK_ASSERT((elem_length << shift) == byte_length);
  23838. h_bufobj = duk_push_bufobj_raw(thr,
  23839. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23840. DUK_HOBJECT_FLAG_BUFOBJ |
  23841. DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
  23842. (duk_small_int_t) proto_bidx);
  23843. h_val = h_bufarg->buf;
  23844. if (h_val == NULL) {
  23845. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  23846. }
  23847. h_bufobj->buf = h_val;
  23848. DUK_HBUFFER_INCREF(thr, h_val);
  23849. h_bufobj->offset = h_bufarg->offset + byte_offset;
  23850. h_bufobj->length = byte_length;
  23851. h_bufobj->shift = (duk_uint8_t) shift;
  23852. h_bufobj->elem_type = (duk_uint8_t) elem_type;
  23853. h_bufobj->is_typedarray = 1;
  23854. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  23855. /* Set .buffer to the argument ArrayBuffer. */
  23856. DUK_ASSERT(h_bufobj->buf_prop == NULL);
  23857. h_bufobj->buf_prop = (duk_hobject *) h_bufarg;
  23858. DUK_ASSERT(h_bufarg != NULL);
  23859. DUK_HBUFOBJ_INCREF(thr, h_bufarg);
  23860. return 1;
  23861. } else if (DUK_HOBJECT_IS_BUFOBJ(h_obj)) {
  23862. /* TypedArray (or other non-ArrayBuffer duk_hbufobj).
  23863. * Conceptually same behavior as for an Array-like argument,
  23864. * with a few fast paths.
  23865. */
  23866. h_bufarg = (duk_hbufobj *) h_obj;
  23867. DUK_ASSERT_HBUFOBJ_VALID(h_bufarg);
  23868. elem_length_signed = (duk_int_t) (h_bufarg->length >> h_bufarg->shift);
  23869. if (h_bufarg->buf == NULL) {
  23870. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  23871. }
  23872. /* Select copy mode. Must take into account element
  23873. * compatibility and validity of the underlying source
  23874. * buffer.
  23875. */
  23876. DUK_DDD(DUK_DDDPRINT("selecting copy mode for bufobj arg, "
  23877. "src byte_length=%ld, src shift=%d, "
  23878. "src/dst elem_length=%ld; "
  23879. "dst shift=%d -> dst byte_length=%ld",
  23880. (long) h_bufarg->length, (int) h_bufarg->shift,
  23881. (long) elem_length_signed, (int) shift,
  23882. (long) (elem_length_signed << shift)));
  23883. copy_mode = 2; /* default is explicit index read/write copy */
  23884. #if !defined(DUK_USE_PREFER_SIZE)
  23885. /* With a size optimized build copy_mode 2 is enough.
  23886. * Modes 0 and 1 are faster but conceptually the same.
  23887. */
  23888. DUK_ASSERT(elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
  23889. if (DUK_HBUFOBJ_VALID_SLICE(h_bufarg)) {
  23890. if ((duk__buffer_elemtype_copy_compatible[elem_type] & (1 << h_bufarg->elem_type)) != 0) {
  23891. DUK_DDD(DUK_DDDPRINT("source/target are copy compatible, memcpy"));
  23892. DUK_ASSERT(shift == h_bufarg->shift); /* byte sizes will match */
  23893. copy_mode = 0;
  23894. } else {
  23895. DUK_DDD(DUK_DDDPRINT("source/target not copy compatible but valid, fast copy"));
  23896. copy_mode = 1;
  23897. }
  23898. }
  23899. #endif /* !DUK_USE_PREFER_SIZE */
  23900. } else {
  23901. /* Array or Array-like */
  23902. elem_length_signed = (duk_int_t) duk_get_length(thr, 0);
  23903. copy_mode = 2;
  23904. }
  23905. } else {
  23906. /* Non-object argument is simply int coerced, matches
  23907. * V8 behavior (except for "null", which we coerce to
  23908. * 0 but V8 TypeErrors).
  23909. */
  23910. elem_length_signed = duk_to_int(thr, 0);
  23911. copy_mode = 3;
  23912. }
  23913. if (elem_length_signed < 0) {
  23914. goto fail_arguments;
  23915. }
  23916. elem_length = (duk_uint_t) elem_length_signed;
  23917. byte_length = (duk_uint_t) (elem_length << shift);
  23918. if ((byte_length >> shift) != elem_length) {
  23919. /* Byte length would overflow. */
  23920. /* XXX: easier check with less code? */
  23921. goto fail_arguments;
  23922. }
  23923. DUK_DDD(DUK_DDDPRINT("elem_length=%ld, byte_length=%ld",
  23924. (long) elem_length, (long) byte_length));
  23925. /* ArrayBuffer argument is handled specially above; the rest of the
  23926. * argument variants are handled by shared code below.
  23927. *
  23928. * ArrayBuffer in h_bufobj->buf_prop is intentionally left unset.
  23929. * It will be automatically created by the .buffer accessor on
  23930. * first access.
  23931. */
  23932. /* Push the resulting view object on top of a plain fixed buffer. */
  23933. (void) duk_push_fixed_buffer(thr, byte_length);
  23934. h_val = duk_known_hbuffer(thr, -1);
  23935. DUK_ASSERT(h_val != NULL);
  23936. h_bufobj = duk_push_bufobj_raw(thr,
  23937. DUK_HOBJECT_FLAG_EXTENSIBLE |
  23938. DUK_HOBJECT_FLAG_BUFOBJ |
  23939. DUK_HOBJECT_CLASS_AS_FLAGS(class_num),
  23940. (duk_small_int_t) proto_bidx);
  23941. h_bufobj->buf = h_val;
  23942. DUK_HBUFFER_INCREF(thr, h_val);
  23943. DUK_ASSERT(h_bufobj->offset == 0);
  23944. h_bufobj->length = byte_length;
  23945. h_bufobj->shift = (duk_uint8_t) shift;
  23946. h_bufobj->elem_type = (duk_uint8_t) elem_type;
  23947. h_bufobj->is_typedarray = 1;
  23948. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  23949. /* Copy values, the copy method depends on the arguments.
  23950. *
  23951. * Copy mode decision may depend on the validity of the underlying
  23952. * buffer of the source argument; there must be no harmful side effects
  23953. * from there to here for copy_mode to still be valid.
  23954. */
  23955. DUK_DDD(DUK_DDDPRINT("copy mode: %d", (int) copy_mode));
  23956. switch (copy_mode) {
  23957. /* Copy modes 0 and 1 can be omitted in size optimized build,
  23958. * copy mode 2 handles them (but more slowly).
  23959. */
  23960. #if !defined(DUK_USE_PREFER_SIZE)
  23961. case 0: {
  23962. /* Use byte copy. */
  23963. duk_uint8_t *p_src;
  23964. duk_uint8_t *p_dst;
  23965. DUK_ASSERT(h_bufobj != NULL);
  23966. DUK_ASSERT(h_bufobj->buf != NULL);
  23967. DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufobj));
  23968. DUK_ASSERT(h_bufarg != NULL);
  23969. DUK_ASSERT(h_bufarg->buf != NULL);
  23970. DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufarg));
  23971. p_dst = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufobj);
  23972. p_src = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufarg);
  23973. DUK_DDD(DUK_DDDPRINT("using memcpy: p_src=%p, p_dst=%p, byte_length=%ld",
  23974. (void *) p_src, (void *) p_dst, (long) byte_length));
  23975. duk_memcpy_unsafe((void *) p_dst, (const void *) p_src, (size_t) byte_length);
  23976. break;
  23977. }
  23978. case 1: {
  23979. /* Copy values through direct validated reads and writes. */
  23980. duk_small_uint_t src_elem_size;
  23981. duk_small_uint_t dst_elem_size;
  23982. duk_uint8_t *p_src;
  23983. duk_uint8_t *p_src_end;
  23984. duk_uint8_t *p_dst;
  23985. DUK_ASSERT(h_bufobj != NULL);
  23986. DUK_ASSERT(h_bufobj->buf != NULL);
  23987. DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufobj));
  23988. DUK_ASSERT(h_bufarg != NULL);
  23989. DUK_ASSERT(h_bufarg->buf != NULL);
  23990. DUK_ASSERT(DUK_HBUFOBJ_VALID_SLICE(h_bufarg));
  23991. src_elem_size = (duk_small_uint_t) (1U << h_bufarg->shift);
  23992. dst_elem_size = elem_size;
  23993. p_src = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufarg);
  23994. p_dst = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufobj);
  23995. p_src_end = p_src + h_bufarg->length;
  23996. DUK_DDD(DUK_DDDPRINT("using fast copy: p_src=%p, p_src_end=%p, p_dst=%p, "
  23997. "src_elem_size=%d, dst_elem_size=%d",
  23998. (void *) p_src, (void *) p_src_end, (void *) p_dst,
  23999. (int) src_elem_size, (int) dst_elem_size));
  24000. while (p_src != p_src_end) {
  24001. DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
  24002. "p_src=%p, p_src_end=%p, p_dst=%p",
  24003. (void *) p_src, (void *) p_src_end, (void *) p_dst));
  24004. /* A validated read() is always a number, so it's write coercion
  24005. * is always side effect free an won't invalidate pointers etc.
  24006. */
  24007. duk_hbufobj_push_validated_read(thr, h_bufarg, p_src, src_elem_size);
  24008. duk_hbufobj_validated_write(thr, h_bufobj, p_dst, dst_elem_size);
  24009. duk_pop(thr);
  24010. p_src += src_elem_size;
  24011. p_dst += dst_elem_size;
  24012. }
  24013. break;
  24014. }
  24015. #endif /* !DUK_USE_PREFER_SIZE */
  24016. case 2: {
  24017. /* Copy values by index reads and writes. Let virtual
  24018. * property handling take care of coercion.
  24019. */
  24020. duk_uint_t i;
  24021. DUK_DDD(DUK_DDDPRINT("using slow copy"));
  24022. for (i = 0; i < elem_length; i++) {
  24023. duk_get_prop_index(thr, 0, (duk_uarridx_t) i);
  24024. duk_put_prop_index(thr, -2, (duk_uarridx_t) i);
  24025. }
  24026. break;
  24027. }
  24028. default:
  24029. case 3: {
  24030. /* No copy, leave zero bytes in the buffer. There's no
  24031. * ambiguity with Float32/Float64 because zero bytes also
  24032. * represent 0.0.
  24033. */
  24034. DUK_DDD(DUK_DDDPRINT("using no copy"));
  24035. break;
  24036. }
  24037. }
  24038. return 1;
  24039. fail_arguments:
  24040. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  24041. }
  24042. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24043. /* When bufferobject support is disabled, new Uint8Array() could still be
  24044. * supported to create a plain fixed buffer. Disabled for now.
  24045. */
  24046. #if 0
  24047. DUK_INTERNAL duk_ret_t duk_bi_typedarray_constructor(duk_hthread *thr) {
  24048. duk_int_t elem_length_signed;
  24049. duk_uint_t byte_length;
  24050. /* XXX: The same copy helpers could be shared with at least some
  24051. * buffer functions.
  24052. */
  24053. duk_require_constructor_call(thr);
  24054. elem_length_signed = duk_require_int(thr, 0);
  24055. if (elem_length_signed < 0) {
  24056. goto fail_arguments;
  24057. }
  24058. byte_length = (duk_uint_t) elem_length_signed;
  24059. (void) duk_push_fixed_buffer_zero(thr, (duk_size_t) byte_length);
  24060. return 1;
  24061. fail_arguments:
  24062. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  24063. }
  24064. #endif /* 0 */
  24065. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24066. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24067. DUK_INTERNAL duk_ret_t duk_bi_dataview_constructor(duk_hthread *thr) {
  24068. duk_hbufobj *h_bufarg;
  24069. duk_hbufobj *h_bufobj;
  24070. duk_hbuffer *h_val;
  24071. duk_uint_t offset;
  24072. duk_uint_t length;
  24073. duk_require_constructor_call(thr);
  24074. h_bufarg = duk__require_bufobj_value(thr, 0);
  24075. DUK_ASSERT(h_bufarg != NULL);
  24076. if (DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_bufarg) != DUK_HOBJECT_CLASS_ARRAYBUFFER) {
  24077. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24078. }
  24079. duk__resolve_offset_opt_length(thr, h_bufarg, 1, 2, &offset, &length, 1 /*throw_flag*/);
  24080. DUK_ASSERT(offset <= h_bufarg->length);
  24081. DUK_ASSERT(offset + length <= h_bufarg->length);
  24082. h_bufobj = duk_push_bufobj_raw(thr,
  24083. DUK_HOBJECT_FLAG_EXTENSIBLE |
  24084. DUK_HOBJECT_FLAG_BUFOBJ |
  24085. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATAVIEW),
  24086. DUK_BIDX_DATAVIEW_PROTOTYPE);
  24087. h_val = h_bufarg->buf;
  24088. if (h_val == NULL) {
  24089. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24090. }
  24091. h_bufobj->buf = h_val;
  24092. DUK_HBUFFER_INCREF(thr, h_val);
  24093. h_bufobj->offset = h_bufarg->offset + offset;
  24094. h_bufobj->length = length;
  24095. DUK_ASSERT(h_bufobj->shift == 0);
  24096. DUK_ASSERT(h_bufobj->elem_type == DUK_HBUFOBJ_ELEM_UINT8);
  24097. DUK_ASSERT(h_bufobj->is_typedarray == 0);
  24098. DUK_ASSERT(h_bufobj->buf_prop == NULL);
  24099. h_bufobj->buf_prop = (duk_hobject *) h_bufarg;
  24100. DUK_ASSERT(h_bufarg != NULL);
  24101. DUK_HBUFOBJ_INCREF(thr, h_bufarg);
  24102. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  24103. return 1;
  24104. }
  24105. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24106. /*
  24107. * ArrayBuffer.isView()
  24108. */
  24109. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24110. DUK_INTERNAL duk_ret_t duk_bi_arraybuffer_isview(duk_hthread *thr) {
  24111. duk_hobject *h_obj;
  24112. duk_bool_t ret = 0;
  24113. if (duk_is_buffer(thr, 0)) {
  24114. ret = 1;
  24115. } else {
  24116. h_obj = duk_get_hobject(thr, 0);
  24117. if (h_obj != NULL && DUK_HOBJECT_IS_BUFOBJ(h_obj)) {
  24118. /* DataView needs special casing: ArrayBuffer.isView() is
  24119. * true, but ->is_typedarray is 0.
  24120. */
  24121. ret = ((duk_hbufobj *) h_obj)->is_typedarray ||
  24122. (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_DATAVIEW);
  24123. }
  24124. }
  24125. duk_push_boolean(thr, ret);
  24126. return 1;
  24127. }
  24128. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24129. /*
  24130. * Uint8Array.allocPlain()
  24131. */
  24132. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24133. DUK_INTERNAL duk_ret_t duk_bi_uint8array_allocplain(duk_hthread *thr) {
  24134. duk__hbufobj_fixed_from_argvalue(thr);
  24135. return 1;
  24136. }
  24137. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24138. /*
  24139. * Uint8Array.plainOf()
  24140. */
  24141. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24142. DUK_INTERNAL duk_ret_t duk_bi_uint8array_plainof(duk_hthread *thr) {
  24143. duk_hbufobj *h_bufobj;
  24144. #if !defined(DUK_USE_PREFER_SIZE)
  24145. /* Avoid churn if argument is already a plain buffer. */
  24146. if (duk_is_buffer(thr, 0)) {
  24147. return 1;
  24148. }
  24149. #endif
  24150. /* Promotes plain buffers to ArrayBuffers, so for a plain buffer
  24151. * argument we'll create a pointless temporary (but still work
  24152. * correctly).
  24153. */
  24154. h_bufobj = duk__require_bufobj_value(thr, 0);
  24155. if (h_bufobj->buf == NULL) {
  24156. duk_push_undefined(thr);
  24157. } else {
  24158. duk_push_hbuffer(thr, h_bufobj->buf);
  24159. }
  24160. return 1;
  24161. }
  24162. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24163. /*
  24164. * Node.js Buffer: toString([encoding], [start], [end])
  24165. */
  24166. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24167. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_hthread *thr) {
  24168. duk_hbufobj *h_this;
  24169. duk_int_t start_offset, end_offset;
  24170. duk_uint8_t *buf_slice;
  24171. duk_size_t slice_length;
  24172. h_this = duk__get_bufobj_this(thr);
  24173. if (h_this == NULL) {
  24174. /* XXX: happens e.g. when evaluating: String(Buffer.prototype). */
  24175. duk_push_literal(thr, "[object Object]");
  24176. return 1;
  24177. }
  24178. DUK_ASSERT_HBUFOBJ_VALID(h_this);
  24179. /* Ignore encoding for now. */
  24180. duk__clamp_startend_nonegidx_noshift(thr,
  24181. (duk_int_t) h_this->length,
  24182. 1 /*idx_start*/,
  24183. 2 /*idx_end*/,
  24184. &start_offset,
  24185. &end_offset);
  24186. slice_length = (duk_size_t) (end_offset - start_offset);
  24187. buf_slice = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, slice_length); /* all bytes initialized below */
  24188. DUK_ASSERT(buf_slice != NULL);
  24189. /* Neutered or uncovered, TypeError. */
  24190. if (h_this->buf == NULL ||
  24191. !DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_this, (duk_size_t) start_offset + slice_length)) {
  24192. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24193. }
  24194. /* XXX: ideally we wouldn't make a copy but a view into the buffer for the
  24195. * decoding process. Or the decoding helper could be changed to accept
  24196. * the slice info (a buffer pointer is NOT a good approach because guaranteeing
  24197. * its stability is difficult).
  24198. */
  24199. DUK_ASSERT(DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_this, (duk_size_t) start_offset + slice_length));
  24200. duk_memcpy_unsafe((void *) buf_slice,
  24201. (const void *) (DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this) + start_offset),
  24202. (size_t) slice_length);
  24203. /* Use the equivalent of: new TextEncoder().encode(this) to convert the
  24204. * string. Result will be valid UTF-8; non-CESU-8 inputs are currently
  24205. * interpreted loosely. Value stack convention is a bit odd for now.
  24206. */
  24207. duk_replace(thr, 0);
  24208. duk_set_top(thr, 1);
  24209. return duk_textdecoder_decode_utf8_nodejs(thr);
  24210. }
  24211. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24212. /*
  24213. * Node.js Buffer.prototype: toJSON()
  24214. */
  24215. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24216. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tojson(duk_hthread *thr) {
  24217. duk_hbufobj *h_this;
  24218. duk_uint8_t *buf;
  24219. duk_uint_t i, n;
  24220. duk_tval *tv;
  24221. h_this = duk__require_bufobj_this(thr);
  24222. DUK_ASSERT(h_this != NULL);
  24223. if (h_this->buf == NULL || !DUK_HBUFOBJ_VALID_SLICE(h_this)) {
  24224. /* Serialize uncovered backing buffer as a null; doesn't
  24225. * really matter as long we're memory safe.
  24226. */
  24227. duk_push_null(thr);
  24228. return 1;
  24229. }
  24230. duk_push_object(thr);
  24231. duk_push_hstring_stridx(thr, DUK_STRIDX_UC_BUFFER);
  24232. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_TYPE);
  24233. /* XXX: uninitialized would be OK */
  24234. DUK_ASSERT_DISABLE((duk_size_t) h_this->length <= (duk_size_t) DUK_UINT32_MAX);
  24235. tv = duk_push_harray_with_size_outptr(thr, (duk_uint32_t) h_this->length); /* XXX: needs revision with >4G buffers */
  24236. DUK_ASSERT(h_this->buf != NULL);
  24237. buf = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this);
  24238. for (i = 0, n = h_this->length; i < n; i++) {
  24239. DUK_TVAL_SET_U32(tv + i, (duk_uint32_t) buf[i]); /* no need for decref or incref */
  24240. }
  24241. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_DATA);
  24242. return 1;
  24243. }
  24244. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24245. /*
  24246. * Node.js Buffer.prototype.equals()
  24247. * Node.js Buffer.prototype.compare()
  24248. * Node.js Buffer.compare()
  24249. */
  24250. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24251. DUK_INTERNAL duk_ret_t duk_bi_buffer_compare_shared(duk_hthread *thr) {
  24252. duk_small_uint_t magic;
  24253. duk_hbufobj *h_bufarg1;
  24254. duk_hbufobj *h_bufarg2;
  24255. duk_small_int_t comp_res;
  24256. /* XXX: keep support for plain buffers and non-Node.js buffers? */
  24257. magic = (duk_small_uint_t) duk_get_current_magic(thr);
  24258. if (magic & 0x02U) {
  24259. /* Static call style. */
  24260. h_bufarg1 = duk__require_bufobj_value(thr, 0);
  24261. h_bufarg2 = duk__require_bufobj_value(thr, 1);
  24262. } else {
  24263. h_bufarg1 = duk__require_bufobj_this(thr);
  24264. h_bufarg2 = duk__require_bufobj_value(thr, 0);
  24265. }
  24266. DUK_ASSERT(h_bufarg1 != NULL);
  24267. DUK_ASSERT(h_bufarg2 != NULL);
  24268. /* We want to compare the slice/view areas of the arguments.
  24269. * If either slice/view is invalid (underlying buffer is shorter)
  24270. * ensure equals() is false, but otherwise the only thing that
  24271. * matters is to be memory safe.
  24272. */
  24273. if (DUK_HBUFOBJ_VALID_SLICE(h_bufarg1) &&
  24274. DUK_HBUFOBJ_VALID_SLICE(h_bufarg2)) {
  24275. comp_res = duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg1->buf) + h_bufarg1->offset,
  24276. (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufarg2->buf) + h_bufarg2->offset,
  24277. (duk_size_t) h_bufarg1->length,
  24278. (duk_size_t) h_bufarg2->length);
  24279. } else {
  24280. comp_res = -1; /* either nonzero value is ok */
  24281. }
  24282. if (magic & 0x01U) {
  24283. /* compare: similar to string comparison but for buffer data. */
  24284. duk_push_int(thr, comp_res);
  24285. } else {
  24286. /* equals */
  24287. duk_push_boolean(thr, (comp_res == 0));
  24288. }
  24289. return 1;
  24290. }
  24291. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24292. /*
  24293. * Node.js Buffer.prototype.fill()
  24294. */
  24295. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24296. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_fill(duk_hthread *thr) {
  24297. duk_hbufobj *h_this;
  24298. const duk_uint8_t *fill_str_ptr;
  24299. duk_size_t fill_str_len;
  24300. duk_uint8_t fill_value;
  24301. duk_int_t fill_offset;
  24302. duk_int_t fill_end;
  24303. duk_size_t fill_length;
  24304. duk_uint8_t *p;
  24305. h_this = duk__require_bufobj_this(thr);
  24306. DUK_ASSERT(h_this != NULL);
  24307. if (h_this->buf == NULL) {
  24308. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24309. }
  24310. /* [ value offset end ] */
  24311. if (duk_is_string_notsymbol(thr, 0)) {
  24312. fill_str_ptr = (const duk_uint8_t *) duk_get_lstring(thr, 0, &fill_str_len);
  24313. DUK_ASSERT(fill_str_ptr != NULL);
  24314. } else {
  24315. /* Symbols get ToNumber() coerced and cause TypeError. */
  24316. fill_value = (duk_uint8_t) duk_to_uint32(thr, 0);
  24317. fill_str_ptr = (const duk_uint8_t *) &fill_value;
  24318. fill_str_len = 1;
  24319. }
  24320. /* Fill offset handling is more lenient than in Node.js. */
  24321. duk__clamp_startend_nonegidx_noshift(thr,
  24322. (duk_int_t) h_this->length,
  24323. 1 /*idx_start*/,
  24324. 2 /*idx_end*/,
  24325. &fill_offset,
  24326. &fill_end);
  24327. DUK_DDD(DUK_DDDPRINT("fill: fill_value=%02x, fill_offset=%ld, fill_end=%ld, view length=%ld",
  24328. (unsigned int) fill_value, (long) fill_offset, (long) fill_end, (long) h_this->length));
  24329. DUK_ASSERT(fill_end - fill_offset >= 0);
  24330. DUK_ASSERT(h_this->buf != NULL);
  24331. p = (DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this) + fill_offset);
  24332. fill_length = (duk_size_t) (fill_end - fill_offset);
  24333. if (fill_str_len == 1) {
  24334. /* Handle single character fills as memset() even when
  24335. * the fill data comes from a one-char argument.
  24336. */
  24337. duk_memset_unsafe((void *) p, (int) fill_str_ptr[0], (size_t) fill_length);
  24338. } else if (fill_str_len > 1) {
  24339. duk_size_t i, n, t;
  24340. for (i = 0, n = (duk_size_t) (fill_end - fill_offset), t = 0; i < n; i++) {
  24341. p[i] = fill_str_ptr[t++];
  24342. if (t >= fill_str_len) {
  24343. t = 0;
  24344. }
  24345. }
  24346. } else {
  24347. DUK_DDD(DUK_DDDPRINT("zero size fill pattern, ignore silently"));
  24348. }
  24349. /* Return the Buffer to allow chaining: b.fill(0x11).fill(0x22, 3, 5).toString() */
  24350. duk_push_this(thr);
  24351. return 1;
  24352. }
  24353. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24354. /*
  24355. * Node.js Buffer.prototype.write(string, [offset], [length], [encoding])
  24356. */
  24357. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24358. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_write(duk_hthread *thr) {
  24359. duk_hbufobj *h_this;
  24360. duk_uint_t offset;
  24361. duk_uint_t length;
  24362. const duk_uint8_t *str_data;
  24363. duk_size_t str_len;
  24364. /* XXX: very inefficient support for plain buffers */
  24365. h_this = duk__require_bufobj_this(thr);
  24366. DUK_ASSERT(h_this != NULL);
  24367. /* Argument must be a string, e.g. a buffer is not allowed. */
  24368. str_data = (const duk_uint8_t *) duk_require_lstring_notsymbol(thr, 0, &str_len);
  24369. duk__resolve_offset_opt_length(thr, h_this, 1, 2, &offset, &length, 0 /*throw_flag*/);
  24370. DUK_ASSERT(offset <= h_this->length);
  24371. DUK_ASSERT(offset + length <= h_this->length);
  24372. /* XXX: encoding is ignored now. */
  24373. if (length > str_len) {
  24374. length = (duk_uint_t) str_len;
  24375. }
  24376. if (DUK_HBUFOBJ_VALID_SLICE(h_this)) {
  24377. /* Cannot overlap. */
  24378. duk_memcpy_unsafe((void *) (DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this) + offset),
  24379. (const void *) str_data,
  24380. (size_t) length);
  24381. } else {
  24382. DUK_DDD(DUK_DDDPRINT("write() target buffer is not covered, silent ignore"));
  24383. }
  24384. duk_push_uint(thr, length);
  24385. return 1;
  24386. }
  24387. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24388. /*
  24389. * Node.js Buffer.prototype.copy()
  24390. */
  24391. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24392. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_copy(duk_hthread *thr) {
  24393. duk_hbufobj *h_this;
  24394. duk_hbufobj *h_bufarg;
  24395. duk_int_t source_length;
  24396. duk_int_t target_length;
  24397. duk_int_t target_start, source_start, source_end;
  24398. duk_uint_t target_ustart, source_ustart, source_uend;
  24399. duk_uint_t copy_size = 0;
  24400. /* [ targetBuffer targetStart sourceStart sourceEnd ] */
  24401. h_this = duk__require_bufobj_this(thr);
  24402. h_bufarg = duk__require_bufobj_value(thr, 0);
  24403. DUK_ASSERT(h_this != NULL);
  24404. DUK_ASSERT(h_bufarg != NULL);
  24405. source_length = (duk_int_t) h_this->length;
  24406. target_length = (duk_int_t) h_bufarg->length;
  24407. target_start = duk_to_int(thr, 1);
  24408. source_start = duk_to_int(thr, 2);
  24409. if (duk_is_undefined(thr, 3)) {
  24410. source_end = source_length;
  24411. } else {
  24412. source_end = duk_to_int(thr, 3);
  24413. }
  24414. DUK_DDD(DUK_DDDPRINT("checking copy args: target_start=%ld, target_length=%ld, "
  24415. "source_start=%ld, source_end=%ld, source_length=%ld",
  24416. (long) target_start, (long) h_bufarg->length,
  24417. (long) source_start, (long) source_end, (long) source_length));
  24418. /* This behavior mostly mimics Node.js now. */
  24419. if (source_start < 0 || source_end < 0 || target_start < 0) {
  24420. /* Negative offsets cause a RangeError. */
  24421. goto fail_bounds;
  24422. }
  24423. source_ustart = (duk_uint_t) source_start;
  24424. source_uend = (duk_uint_t) source_end;
  24425. target_ustart = (duk_uint_t) target_start;
  24426. if (source_ustart >= source_uend || /* crossed offsets or zero size */
  24427. source_ustart >= (duk_uint_t) source_length || /* source out-of-bounds (but positive) */
  24428. target_ustart >= (duk_uint_t) target_length) { /* target out-of-bounds (but positive) */
  24429. goto silent_ignore;
  24430. }
  24431. if (source_uend >= (duk_uint_t) source_length) {
  24432. /* Source end clamped silently to available length. */
  24433. source_uend = (duk_uint_t) source_length;
  24434. }
  24435. copy_size = source_uend - source_ustart;
  24436. if (target_ustart + copy_size > (duk_uint_t) target_length) {
  24437. /* Clamp to target's end if too long.
  24438. *
  24439. * NOTE: there's no overflow possibility in the comparison;
  24440. * both target_ustart and copy_size are >= 0 and based on
  24441. * values in duk_int_t range. Adding them as duk_uint_t
  24442. * values is then guaranteed not to overflow.
  24443. */
  24444. DUK_ASSERT(target_ustart + copy_size >= target_ustart); /* no overflow */
  24445. DUK_ASSERT(target_ustart + copy_size >= copy_size); /* no overflow */
  24446. copy_size = (duk_uint_t) target_length - target_ustart;
  24447. }
  24448. DUK_DDD(DUK_DDDPRINT("making copy: target_ustart=%lu source_ustart=%lu copy_size=%lu",
  24449. (unsigned long) target_ustart, (unsigned long) source_ustart,
  24450. (unsigned long) copy_size));
  24451. DUK_ASSERT(copy_size >= 1);
  24452. DUK_ASSERT(source_ustart <= (duk_uint_t) source_length);
  24453. DUK_ASSERT(source_ustart + copy_size <= (duk_uint_t) source_length);
  24454. DUK_ASSERT(target_ustart <= (duk_uint_t) target_length);
  24455. DUK_ASSERT(target_ustart + copy_size <= (duk_uint_t) target_length);
  24456. /* Ensure copy is covered by underlying buffers. */
  24457. DUK_ASSERT(h_bufarg->buf != NULL); /* length check */
  24458. DUK_ASSERT(h_this->buf != NULL); /* length check */
  24459. if (DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_bufarg, target_ustart + copy_size) &&
  24460. DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_this, source_ustart + copy_size)) {
  24461. /* Must use memmove() because copy area may overlap (source and target
  24462. * buffer may be the same, or from different slices.
  24463. */
  24464. duk_memmove_unsafe((void *) (DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufarg) + target_ustart),
  24465. (const void *) (DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this) + source_ustart),
  24466. (size_t) copy_size);
  24467. } else {
  24468. DUK_DDD(DUK_DDDPRINT("buffer copy not covered by underlying buffer(s), ignoring"));
  24469. }
  24470. silent_ignore:
  24471. /* Return value is like write(), number of bytes written.
  24472. * The return value matters because of code like:
  24473. * "off += buf.copy(...)".
  24474. */
  24475. duk_push_uint(thr, copy_size);
  24476. return 1;
  24477. fail_bounds:
  24478. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  24479. }
  24480. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24481. /*
  24482. * TypedArray.prototype.set()
  24483. *
  24484. * TypedArray set() is pretty interesting to implement because:
  24485. *
  24486. * - The source argument may be a plain array or a typedarray. If the
  24487. * source is a TypedArray, values are decoded and re-encoded into the
  24488. * target (not as a plain byte copy). This may happen even when the
  24489. * element byte size is the same, e.g. integer values may be re-encoded
  24490. * into floats.
  24491. *
  24492. * - Source and target may refer to the same underlying buffer, so that
  24493. * the set() operation may overlap. The specification requires that this
  24494. * must work as if a copy was made before the operation. Note that this
  24495. * is NOT a simple memmove() situation because the source and target
  24496. * byte sizes may be different -- e.g. a 4-byte source (Int8Array) may
  24497. * expand to a 16-byte target (Uint32Array) so that the target overlaps
  24498. * the source both from beginning and the end (unlike in typical memmove).
  24499. *
  24500. * - Even if 'buf' pointers of the source and target differ, there's no
  24501. * guarantee that their memory areas don't overlap. This may be the
  24502. * case with external buffers.
  24503. *
  24504. * Even so, it is nice to optimize for the common case:
  24505. *
  24506. * - Source and target separate buffers or non-overlapping.
  24507. *
  24508. * - Source and target have a compatible type so that a plain byte copy
  24509. * is possible. Note that while e.g. uint8 and int8 are compatible
  24510. * (coercion one way or another doesn't change the byte representation),
  24511. * e.g. int8 and uint8clamped are NOT compatible when writing int8
  24512. * values into uint8clamped typedarray (-1 would clamp to 0 for instance).
  24513. *
  24514. * See test-bi-typedarray-proto-set.js.
  24515. */
  24516. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24517. DUK_INTERNAL duk_ret_t duk_bi_typedarray_set(duk_hthread *thr) {
  24518. duk_hbufobj *h_this;
  24519. duk_hobject *h_obj;
  24520. duk_uarridx_t i, n;
  24521. duk_int_t offset_signed;
  24522. duk_uint_t offset_elems;
  24523. duk_uint_t offset_bytes;
  24524. h_this = duk__require_bufobj_this(thr);
  24525. DUK_ASSERT(h_this != NULL);
  24526. DUK_ASSERT_HBUFOBJ_VALID(h_this);
  24527. if (h_this->buf == NULL) {
  24528. DUK_DDD(DUK_DDDPRINT("source neutered, skip copy"));
  24529. return 0;
  24530. }
  24531. duk_hbufobj_promote_plain(thr, 0);
  24532. h_obj = duk_require_hobject(thr, 0);
  24533. /* XXX: V8 throws a TypeError for negative values. Would it
  24534. * be more useful to interpret negative offsets here from the
  24535. * end of the buffer too?
  24536. */
  24537. offset_signed = duk_to_int(thr, 1);
  24538. if (offset_signed < 0) {
  24539. /* For some reason this is a TypeError (at least in V8). */
  24540. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24541. }
  24542. offset_elems = (duk_uint_t) offset_signed;
  24543. offset_bytes = offset_elems << h_this->shift;
  24544. if ((offset_bytes >> h_this->shift) != offset_elems) {
  24545. /* Byte length would overflow. */
  24546. /* XXX: easier check with less code? */
  24547. goto fail_args;
  24548. }
  24549. if (offset_bytes > h_this->length) {
  24550. /* Equality may be OK but >length not. Checking
  24551. * this explicitly avoids some overflow cases
  24552. * below.
  24553. */
  24554. goto fail_args;
  24555. }
  24556. DUK_ASSERT(offset_bytes <= h_this->length);
  24557. /* Fast path: source is a TypedArray (or any bufobj). */
  24558. if (DUK_HOBJECT_IS_BUFOBJ(h_obj)) {
  24559. duk_hbufobj *h_bufarg;
  24560. #if !defined(DUK_USE_PREFER_SIZE)
  24561. duk_uint16_t comp_mask;
  24562. #endif
  24563. duk_small_int_t no_overlap = 0;
  24564. duk_uint_t src_length;
  24565. duk_uint_t dst_length;
  24566. duk_uint_t dst_length_elems;
  24567. duk_uint8_t *p_src_base;
  24568. duk_uint8_t *p_src_end;
  24569. duk_uint8_t *p_src;
  24570. duk_uint8_t *p_dst_base;
  24571. duk_uint8_t *p_dst;
  24572. duk_small_uint_t src_elem_size;
  24573. duk_small_uint_t dst_elem_size;
  24574. h_bufarg = (duk_hbufobj *) h_obj;
  24575. DUK_ASSERT_HBUFOBJ_VALID(h_bufarg);
  24576. if (h_bufarg->buf == NULL) {
  24577. DUK_DDD(DUK_DDDPRINT("target neutered, skip copy"));
  24578. return 0;
  24579. }
  24580. /* Nominal size check. */
  24581. src_length = h_bufarg->length; /* bytes in source */
  24582. dst_length_elems = (src_length >> h_bufarg->shift); /* elems in source and dest */
  24583. dst_length = dst_length_elems << h_this->shift; /* bytes in dest */
  24584. if ((dst_length >> h_this->shift) != dst_length_elems) {
  24585. /* Byte length would overflow. */
  24586. /* XXX: easier check with less code? */
  24587. goto fail_args;
  24588. }
  24589. DUK_DDD(DUK_DDDPRINT("nominal size check: src_length=%ld, dst_length=%ld",
  24590. (long) src_length, (long) dst_length));
  24591. DUK_ASSERT(offset_bytes <= h_this->length);
  24592. if (dst_length > h_this->length - offset_bytes) {
  24593. /* Overflow not an issue because subtraction is used on the right
  24594. * side and guaranteed to be >= 0.
  24595. */
  24596. DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
  24597. goto fail_args;
  24598. }
  24599. if (!DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_this, offset_bytes + dst_length)) {
  24600. DUK_DDD(DUK_DDDPRINT("copy not covered by underlying target buffer, ignore"));
  24601. return 0;
  24602. }
  24603. p_src_base = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufarg);
  24604. p_dst_base = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this) + offset_bytes;
  24605. /* Check actual underlying buffers for validity and that they
  24606. * cover the copy. No side effects are allowed after the check
  24607. * so that the validity status doesn't change.
  24608. */
  24609. if (!DUK_HBUFOBJ_VALID_SLICE(h_this) ||
  24610. !DUK_HBUFOBJ_VALID_SLICE(h_bufarg)) {
  24611. /* The condition could be more narrow and check for the
  24612. * copy area only, but there's no need for fine grained
  24613. * behavior when the underlying buffer is misconfigured.
  24614. */
  24615. DUK_DDD(DUK_DDDPRINT("source and/or target not covered by underlying buffer, skip copy"));
  24616. return 0;
  24617. }
  24618. /* We want to do a straight memory copy if possible: this is
  24619. * an important operation because .set() is the TypedArray
  24620. * way to copy chunks of memory. However, because set()
  24621. * conceptually works in terms of elements, not all views are
  24622. * compatible with direct byte copying.
  24623. *
  24624. * If we do manage a direct copy, the "overlap issue" handled
  24625. * below can just be solved using memmove() because the source
  24626. * and destination element sizes are necessarily equal.
  24627. */
  24628. #if !defined(DUK_USE_PREFER_SIZE)
  24629. DUK_ASSERT(h_this->elem_type < sizeof(duk__buffer_elemtype_copy_compatible) / sizeof(duk_uint16_t));
  24630. comp_mask = duk__buffer_elemtype_copy_compatible[h_this->elem_type];
  24631. if (comp_mask & (1 << h_bufarg->elem_type)) {
  24632. DUK_ASSERT(src_length == dst_length);
  24633. DUK_DDD(DUK_DDDPRINT("fast path: able to use memmove() because views are compatible"));
  24634. duk_memmove_unsafe((void *) p_dst_base, (const void *) p_src_base, (size_t) dst_length);
  24635. return 0;
  24636. }
  24637. DUK_DDD(DUK_DDDPRINT("fast path: views are not compatible with a byte copy, copy by item"));
  24638. #endif /* !DUK_USE_PREFER_SIZE */
  24639. /* We want to avoid making a copy to process set() but that's
  24640. * not always possible: the source and the target may overlap
  24641. * and because element sizes are different, the overlap cannot
  24642. * always be handled with a memmove() or choosing the copy
  24643. * direction in a certain way. For example, if source type is
  24644. * uint8 and target type is uint32, the target area may exceed
  24645. * the source area from both ends!
  24646. *
  24647. * Note that because external buffers may point to the same
  24648. * memory areas, we must ultimately make this check using
  24649. * pointers.
  24650. *
  24651. * NOTE: careful with side effects: any side effect may cause
  24652. * a buffer resize (or external buffer pointer/length update)!
  24653. */
  24654. DUK_DDD(DUK_DDDPRINT("overlap check: p_src_base=%p, src_length=%ld, "
  24655. "p_dst_base=%p, dst_length=%ld",
  24656. (void *) p_src_base, (long) src_length,
  24657. (void *) p_dst_base, (long) dst_length));
  24658. if (p_src_base >= p_dst_base + dst_length || /* source starts after dest ends */
  24659. p_src_base + src_length <= p_dst_base) { /* source ends before dest starts */
  24660. no_overlap = 1;
  24661. }
  24662. if (!no_overlap) {
  24663. /* There's overlap: the desired end result is that
  24664. * conceptually a copy is made to avoid "trampling"
  24665. * of source data by destination writes. We make
  24666. * an actual temporary copy to handle this case.
  24667. */
  24668. duk_uint8_t *p_src_copy;
  24669. DUK_DDD(DUK_DDDPRINT("there is overlap, make a copy of the source"));
  24670. p_src_copy = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, src_length);
  24671. DUK_ASSERT(p_src_copy != NULL);
  24672. duk_memcpy_unsafe((void *) p_src_copy, (const void *) p_src_base, (size_t) src_length);
  24673. p_src_base = p_src_copy; /* use p_src_base from now on */
  24674. }
  24675. /* Value stack intentionally mixed size here. */
  24676. DUK_DDD(DUK_DDDPRINT("after overlap check: p_src_base=%p, src_length=%ld, "
  24677. "p_dst_base=%p, dst_length=%ld, valstack top=%ld",
  24678. (void *) p_src_base, (long) src_length,
  24679. (void *) p_dst_base, (long) dst_length,
  24680. (long) duk_get_top(thr)));
  24681. /* Ready to make the copy. We must proceed element by element
  24682. * and must avoid any side effects that might cause the buffer
  24683. * validity check above to become invalid.
  24684. *
  24685. * Although we work through the value stack here, only plain
  24686. * numbers are handled which should be side effect safe.
  24687. */
  24688. src_elem_size = (duk_small_uint_t) (1U << h_bufarg->shift);
  24689. dst_elem_size = (duk_small_uint_t) (1U << h_this->shift);
  24690. p_src = p_src_base;
  24691. p_dst = p_dst_base;
  24692. p_src_end = p_src_base + src_length;
  24693. while (p_src != p_src_end) {
  24694. DUK_DDD(DUK_DDDPRINT("fast path per element copy loop: "
  24695. "p_src=%p, p_src_end=%p, p_dst=%p",
  24696. (void *) p_src, (void *) p_src_end, (void *) p_dst));
  24697. /* A validated read() is always a number, so it's write coercion
  24698. * is always side effect free an won't invalidate pointers etc.
  24699. */
  24700. duk_hbufobj_push_validated_read(thr, h_bufarg, p_src, src_elem_size);
  24701. duk_hbufobj_validated_write(thr, h_this, p_dst, dst_elem_size);
  24702. duk_pop(thr);
  24703. p_src += src_elem_size;
  24704. p_dst += dst_elem_size;
  24705. }
  24706. return 0;
  24707. } else {
  24708. /* Slow path: quite slow, but we save space by using the property code
  24709. * to write coerce target values. We don't need to worry about overlap
  24710. * here because the source is not a TypedArray.
  24711. *
  24712. * We could use the bufobj write coercion helper but since the
  24713. * property read may have arbitrary side effects, full validity checks
  24714. * would be needed for every element anyway.
  24715. */
  24716. n = (duk_uarridx_t) duk_get_length(thr, 0);
  24717. DUK_ASSERT(offset_bytes <= h_this->length);
  24718. if ((n << h_this->shift) > h_this->length - offset_bytes) {
  24719. /* Overflow not an issue because subtraction is used on the right
  24720. * side and guaranteed to be >= 0.
  24721. */
  24722. DUK_DDD(DUK_DDDPRINT("copy exceeds target buffer nominal length"));
  24723. goto fail_args;
  24724. }
  24725. /* There's no need to check for buffer validity status for the
  24726. * target here: the property access code will do that for each
  24727. * element. Moreover, if we did check the validity here, side
  24728. * effects from reading the source argument might invalidate
  24729. * the results anyway.
  24730. */
  24731. DUK_ASSERT_TOP(thr, 2);
  24732. duk_push_this(thr);
  24733. for (i = 0; i < n; i++) {
  24734. duk_get_prop_index(thr, 0, i);
  24735. duk_put_prop_index(thr, 2, offset_elems + i);
  24736. }
  24737. }
  24738. return 0;
  24739. fail_args:
  24740. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  24741. }
  24742. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24743. /*
  24744. * Node.js Buffer.prototype.slice([start], [end])
  24745. * ArrayBuffer.prototype.slice(begin, [end])
  24746. * TypedArray.prototype.subarray(begin, [end])
  24747. *
  24748. * The API calls are almost identical; negative indices are counted from end
  24749. * of buffer, and final indices are clamped (allowing crossed indices). Main
  24750. * differences:
  24751. *
  24752. * - Copy/view behavior; Node.js .slice() and TypedArray .subarray() create
  24753. * views, ArrayBuffer .slice() creates a copy
  24754. *
  24755. * - Resulting object has a different class and prototype depending on the
  24756. * call (or 'this' argument)
  24757. *
  24758. * - TypedArray .subarray() arguments are element indices, not byte offsets
  24759. *
  24760. * - Plain buffer argument creates a plain buffer slice
  24761. */
  24762. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24763. DUK_LOCAL void duk__arraybuffer_plain_slice(duk_hthread *thr, duk_hbuffer *h_val) {
  24764. duk_int_t start_offset, end_offset;
  24765. duk_uint_t slice_length;
  24766. duk_uint8_t *p_copy;
  24767. duk_size_t copy_length;
  24768. duk__clamp_startend_negidx_shifted(thr,
  24769. (duk_int_t) DUK_HBUFFER_GET_SIZE(h_val),
  24770. 0 /*buffer_shift*/,
  24771. 0 /*idx_start*/,
  24772. 1 /*idx_end*/,
  24773. &start_offset,
  24774. &end_offset);
  24775. DUK_ASSERT(end_offset <= (duk_int_t) DUK_HBUFFER_GET_SIZE(h_val));
  24776. DUK_ASSERT(start_offset >= 0);
  24777. DUK_ASSERT(end_offset >= start_offset);
  24778. slice_length = (duk_uint_t) (end_offset - start_offset);
  24779. p_copy = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, (duk_size_t) slice_length);
  24780. DUK_ASSERT(p_copy != NULL);
  24781. copy_length = slice_length;
  24782. duk_memcpy_unsafe((void *) p_copy,
  24783. (const void *) ((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_val) + start_offset),
  24784. copy_length);
  24785. }
  24786. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24787. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24788. /* Shared helper for slice/subarray operation.
  24789. * Magic: 0x01=isView, 0x02=copy, 0x04=Node.js Buffer special handling.
  24790. */
  24791. DUK_INTERNAL duk_ret_t duk_bi_buffer_slice_shared(duk_hthread *thr) {
  24792. duk_small_int_t magic;
  24793. duk_small_uint_t res_class_num;
  24794. duk_small_int_t res_proto_bidx;
  24795. duk_hbufobj *h_this;
  24796. duk_hbufobj *h_bufobj;
  24797. duk_hbuffer *h_val;
  24798. duk_int_t start_offset, end_offset;
  24799. duk_uint_t slice_length;
  24800. duk_tval *tv;
  24801. /* [ start end ] */
  24802. magic = duk_get_current_magic(thr);
  24803. tv = duk_get_borrowed_this_tval(thr);
  24804. DUK_ASSERT(tv != NULL);
  24805. if (DUK_TVAL_IS_BUFFER(tv)) {
  24806. /* For plain buffers return a plain buffer slice. */
  24807. h_val = DUK_TVAL_GET_BUFFER(tv);
  24808. DUK_ASSERT(h_val != NULL);
  24809. if (magic & 0x02) {
  24810. /* Make copy: ArrayBuffer.prototype.slice() uses this. */
  24811. duk__arraybuffer_plain_slice(thr, h_val);
  24812. return 1;
  24813. } else {
  24814. /* View into existing buffer: cannot be done if the
  24815. * result is a plain buffer because there's no slice
  24816. * info. So return an ArrayBuffer instance; coerce
  24817. * the 'this' binding into an object and behave as if
  24818. * the original call was for an Object-coerced plain
  24819. * buffer (handled automatically by duk__require_bufobj_this()).
  24820. */
  24821. DUK_DDD(DUK_DDDPRINT("slice() doesn't handle view into plain buffer, coerce 'this' to ArrayBuffer object"));
  24822. /* fall through */
  24823. }
  24824. }
  24825. tv = NULL; /* No longer valid nor needed. */
  24826. h_this = duk__require_bufobj_this(thr);
  24827. /* Slice offsets are element (not byte) offsets, which only matters
  24828. * for TypedArray views, Node.js Buffer and ArrayBuffer have shift
  24829. * zero so byte and element offsets are the same. Negative indices
  24830. * are counted from end of slice, crossed indices are allowed (and
  24831. * result in zero length result), and final values are clamped
  24832. * against the current slice. There's intentionally no check
  24833. * against the underlying buffer here.
  24834. */
  24835. duk__clamp_startend_negidx_shifted(thr,
  24836. (duk_int_t) h_this->length,
  24837. (duk_uint8_t) h_this->shift,
  24838. 0 /*idx_start*/,
  24839. 1 /*idx_end*/,
  24840. &start_offset,
  24841. &end_offset);
  24842. DUK_ASSERT(end_offset >= start_offset);
  24843. DUK_ASSERT(start_offset >= 0);
  24844. DUK_ASSERT(end_offset >= 0);
  24845. slice_length = (duk_uint_t) (end_offset - start_offset);
  24846. /* The resulting buffer object gets the same class and prototype as
  24847. * the buffer in 'this', e.g. if the input is a Uint8Array the
  24848. * result is a Uint8Array; if the input is a Float32Array, the
  24849. * result is a Float32Array. The result internal prototype should
  24850. * be the default prototype for the class (e.g. initial value of
  24851. * Uint8Array.prototype), not copied from the argument (Duktape 1.x
  24852. * did that).
  24853. *
  24854. * Node.js Buffers have special handling: they're Uint8Arrays as far
  24855. * as the internal class is concerned, so the new Buffer should also
  24856. * be an Uint8Array but inherit from Buffer.prototype.
  24857. */
  24858. res_class_num = DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_this);
  24859. DUK_ASSERT(res_class_num >= DUK_HOBJECT_CLASS_BUFOBJ_MIN); /* type check guarantees */
  24860. DUK_ASSERT(res_class_num <= DUK_HOBJECT_CLASS_BUFOBJ_MAX);
  24861. res_proto_bidx = duk__buffer_proto_from_classnum[res_class_num - DUK_HOBJECT_CLASS_BUFOBJ_MIN];
  24862. if (magic & 0x04) {
  24863. res_proto_bidx = DUK_BIDX_NODEJS_BUFFER_PROTOTYPE;
  24864. }
  24865. h_bufobj = duk_push_bufobj_raw(thr,
  24866. DUK_HOBJECT_FLAG_EXTENSIBLE |
  24867. DUK_HOBJECT_FLAG_BUFOBJ |
  24868. DUK_HOBJECT_CLASS_AS_FLAGS(res_class_num),
  24869. res_proto_bidx);
  24870. DUK_ASSERT(h_bufobj != NULL);
  24871. DUK_ASSERT(h_bufobj->length == 0);
  24872. h_bufobj->shift = h_this->shift; /* inherit */
  24873. h_bufobj->elem_type = h_this->elem_type; /* inherit */
  24874. h_bufobj->is_typedarray = magic & 0x01;
  24875. DUK_ASSERT(h_bufobj->is_typedarray == 0 || h_bufobj->is_typedarray == 1);
  24876. h_val = h_this->buf;
  24877. if (h_val == NULL) {
  24878. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24879. }
  24880. if (magic & 0x02) {
  24881. /* non-zero: make copy */
  24882. duk_uint8_t *p_copy;
  24883. duk_size_t copy_length;
  24884. p_copy = (duk_uint8_t *) duk_push_fixed_buffer_zero(thr, (duk_size_t) slice_length); /* must be zeroed, not all bytes always copied */
  24885. DUK_ASSERT(p_copy != NULL);
  24886. /* Copy slice, respecting underlying buffer limits; remainder
  24887. * is left as zero.
  24888. */
  24889. copy_length = DUK_HBUFOBJ_CLAMP_BYTELENGTH(h_this, slice_length);
  24890. duk_memcpy_unsafe((void *) p_copy,
  24891. (const void *) (DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this) + start_offset),
  24892. copy_length);
  24893. h_val = duk_known_hbuffer(thr, -1);
  24894. h_bufobj->buf = h_val;
  24895. DUK_HBUFFER_INCREF(thr, h_val);
  24896. h_bufobj->length = slice_length;
  24897. DUK_ASSERT(h_bufobj->offset == 0);
  24898. duk_pop(thr); /* reachable so pop OK */
  24899. } else {
  24900. h_bufobj->buf = h_val;
  24901. DUK_HBUFFER_INCREF(thr, h_val);
  24902. h_bufobj->length = slice_length;
  24903. h_bufobj->offset = h_this->offset + (duk_uint_t) start_offset;
  24904. /* Copy the .buffer property, needed for TypedArray.prototype.subarray().
  24905. *
  24906. * XXX: limit copy only for TypedArray classes specifically?
  24907. */
  24908. DUK_ASSERT(h_bufobj->buf_prop == NULL);
  24909. h_bufobj->buf_prop = h_this->buf_prop; /* may be NULL */
  24910. DUK_HOBJECT_INCREF_ALLOWNULL(thr, (duk_hobject *) h_bufobj->buf_prop);
  24911. }
  24912. /* unbalanced stack on purpose */
  24913. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  24914. return 1;
  24915. }
  24916. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24917. /*
  24918. * Node.js Buffer.isEncoding()
  24919. */
  24920. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24921. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_encoding(duk_hthread *thr) {
  24922. const char *encoding;
  24923. /* only accept lowercase 'utf8' now. */
  24924. encoding = duk_to_string(thr, 0);
  24925. DUK_ASSERT(duk_is_string(thr, 0)); /* guaranteed by duk_to_string() */
  24926. duk_push_boolean(thr, DUK_STRCMP(encoding, "utf8") == 0);
  24927. return 1;
  24928. }
  24929. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24930. /*
  24931. * Node.js Buffer.isBuffer()
  24932. */
  24933. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24934. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_is_buffer(duk_hthread *thr) {
  24935. duk_hobject *h;
  24936. duk_hobject *h_proto;
  24937. duk_bool_t ret = 0;
  24938. DUK_ASSERT(duk_get_top(thr) >= 1); /* nargs */
  24939. h = duk_get_hobject(thr, 0);
  24940. if (h != NULL) {
  24941. h_proto = thr->builtins[DUK_BIDX_NODEJS_BUFFER_PROTOTYPE];
  24942. DUK_ASSERT(h_proto != NULL);
  24943. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  24944. if (h != NULL) {
  24945. ret = duk_hobject_prototype_chain_contains(thr, h, h_proto, 0 /*ignore_loop*/);
  24946. }
  24947. }
  24948. duk_push_boolean(thr, ret);
  24949. return 1;
  24950. }
  24951. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24952. /*
  24953. * Node.js Buffer.byteLength()
  24954. */
  24955. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24956. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_byte_length(duk_hthread *thr) {
  24957. const char *str;
  24958. duk_size_t len;
  24959. /* At the moment Buffer(<str>) will just use the string bytes as
  24960. * is (ignoring encoding), so we return the string length here
  24961. * unconditionally.
  24962. */
  24963. /* XXX: to be revised; Old Node.js behavior just coerces any buffer
  24964. * values to string:
  24965. * $ node
  24966. * > Buffer.byteLength(new Uint32Array(10))
  24967. * 20
  24968. * > Buffer.byteLength(new Uint32Array(100))
  24969. * 20
  24970. * (The 20 comes from '[object Uint32Array]'.length
  24971. */
  24972. str = duk_to_lstring(thr, 0, &len);
  24973. DUK_UNREF(str);
  24974. duk_push_size_t(thr, len);
  24975. return 1;
  24976. }
  24977. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  24978. /*
  24979. * Node.js Buffer.concat()
  24980. */
  24981. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  24982. DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_concat(duk_hthread *thr) {
  24983. duk_hobject *h_arg;
  24984. duk_uint_t total_length;
  24985. duk_hbufobj *h_bufobj;
  24986. duk_hbufobj *h_bufres;
  24987. duk_hbuffer *h_val;
  24988. duk_uint_t i, n;
  24989. duk_uint8_t *p;
  24990. duk_size_t space_left;
  24991. duk_size_t copy_size;
  24992. /* Node.js accepts only actual Arrays. */
  24993. h_arg = duk_require_hobject(thr, 0);
  24994. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_arg) != DUK_HOBJECT_CLASS_ARRAY) {
  24995. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  24996. }
  24997. /* Compute result length and validate argument buffers. */
  24998. n = (duk_uint_t) duk_get_length(thr, 0);
  24999. total_length = 0;
  25000. for (i = 0; i < n; i++) {
  25001. /* Neutered checks not necessary here: neutered buffers have
  25002. * zero 'length' so we'll effectively skip them.
  25003. */
  25004. DUK_ASSERT_TOP(thr, 2); /* [ array totalLength ] */
  25005. duk_get_prop_index(thr, 0, (duk_uarridx_t) i); /* -> [ array totalLength buf ] */
  25006. h_bufobj = duk__require_bufobj_value(thr, 2);
  25007. DUK_ASSERT(h_bufobj != NULL);
  25008. total_length += h_bufobj->length;
  25009. if (DUK_UNLIKELY(total_length < h_bufobj->length)) {
  25010. DUK_DCERROR_RANGE_INVALID_ARGS(thr); /* Wrapped. */
  25011. }
  25012. duk_pop(thr);
  25013. }
  25014. /* In Node.js v0.12.1 a 1-element array is special and won't create a
  25015. * copy, this was fixed later so an explicit check no longer needed.
  25016. */
  25017. /* User totalLength overrides a computed length, but we'll check
  25018. * every copy in the copy loop. Note that duk_to_int() can
  25019. * technically have arbitrary side effects so we need to recheck
  25020. * the buffers in the copy loop.
  25021. */
  25022. if (!duk_is_undefined(thr, 1) && n > 0) {
  25023. /* For n == 0, Node.js ignores totalLength argument and
  25024. * returns a zero length buffer.
  25025. */
  25026. duk_int_t total_length_signed;
  25027. total_length_signed = duk_to_int(thr, 1);
  25028. if (total_length_signed < 0) {
  25029. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  25030. }
  25031. total_length = (duk_uint_t) total_length_signed;
  25032. }
  25033. h_bufres = duk_push_bufobj_raw(thr,
  25034. DUK_HOBJECT_FLAG_EXTENSIBLE |
  25035. DUK_HOBJECT_FLAG_BUFOBJ |
  25036. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_UINT8ARRAY),
  25037. DUK_BIDX_NODEJS_BUFFER_PROTOTYPE);
  25038. DUK_ASSERT(h_bufres != NULL);
  25039. p = (duk_uint8_t *) duk_push_fixed_buffer_zero(thr, total_length); /* must be zeroed, all bytes not necessarily written over */
  25040. DUK_ASSERT(p != NULL);
  25041. space_left = (duk_size_t) total_length;
  25042. for (i = 0; i < n; i++) {
  25043. DUK_ASSERT_TOP(thr, 4); /* [ array totalLength bufres buf ] */
  25044. duk_get_prop_index(thr, 0, (duk_uarridx_t) i);
  25045. h_bufobj = duk__require_bufobj_value(thr, 4);
  25046. DUK_ASSERT(h_bufobj != NULL);
  25047. copy_size = h_bufobj->length;
  25048. if (copy_size > space_left) {
  25049. copy_size = space_left;
  25050. }
  25051. if (h_bufobj->buf != NULL &&
  25052. DUK_HBUFOBJ_VALID_SLICE(h_bufobj)) {
  25053. duk_memcpy_unsafe((void *) p,
  25054. (const void *) DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_bufobj),
  25055. copy_size);
  25056. } else {
  25057. /* Just skip, leaving zeroes in the result. */
  25058. ;
  25059. }
  25060. p += copy_size;
  25061. space_left -= copy_size;
  25062. duk_pop(thr);
  25063. }
  25064. h_val = duk_known_hbuffer(thr, -1);
  25065. duk__set_bufobj_buffer(thr, h_bufres, h_val);
  25066. h_bufres->is_typedarray = 1;
  25067. DUK_ASSERT_HBUFOBJ_VALID(h_bufres);
  25068. duk_pop(thr); /* pop plain buffer, now reachable through h_bufres */
  25069. return 1; /* return h_bufres */
  25070. }
  25071. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  25072. /*
  25073. * Shared readfield and writefield methods
  25074. *
  25075. * The readfield/writefield methods need support for endianness and field
  25076. * types. All offsets are byte based so no offset shifting is needed.
  25077. */
  25078. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  25079. /* Format of magic, bits:
  25080. * 0...1: field type; 0=uint8, 1=uint16, 2=uint32, 3=float, 4=double, 5=unused, 6=unused, 7=unused
  25081. * 3: endianness: 0=little, 1=big
  25082. * 4: signed: 1=yes, 0=no
  25083. * 5: typedarray: 1=yes, 0=no
  25084. */
  25085. #define DUK__FLD_8BIT 0
  25086. #define DUK__FLD_16BIT 1
  25087. #define DUK__FLD_32BIT 2
  25088. #define DUK__FLD_FLOAT 3
  25089. #define DUK__FLD_DOUBLE 4
  25090. #define DUK__FLD_VARINT 5
  25091. #define DUK__FLD_BIGENDIAN (1 << 3)
  25092. #define DUK__FLD_SIGNED (1 << 4)
  25093. #define DUK__FLD_TYPEDARRAY (1 << 5)
  25094. /* XXX: split into separate functions for each field type? */
  25095. DUK_INTERNAL duk_ret_t duk_bi_buffer_readfield(duk_hthread *thr) {
  25096. duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(thr);
  25097. duk_small_int_t magic_ftype;
  25098. duk_small_int_t magic_bigendian;
  25099. duk_small_int_t magic_signed;
  25100. duk_small_int_t magic_typedarray;
  25101. duk_small_int_t endswap;
  25102. duk_hbufobj *h_this;
  25103. duk_bool_t no_assert;
  25104. duk_int_t offset_signed;
  25105. duk_uint_t offset;
  25106. duk_uint_t buffer_length;
  25107. duk_uint_t check_length;
  25108. duk_uint8_t *buf;
  25109. duk_double_union du;
  25110. magic_ftype = magic & 0x0007;
  25111. magic_bigendian = magic & 0x0008;
  25112. magic_signed = magic & 0x0010;
  25113. magic_typedarray = magic & 0x0020;
  25114. h_this = duk__require_bufobj_this(thr); /* XXX: very inefficient for plain buffers */
  25115. DUK_ASSERT(h_this != NULL);
  25116. buffer_length = h_this->length;
  25117. /* [ offset noAssert ], when ftype != DUK__FLD_VARINT */
  25118. /* [ offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */
  25119. /* [ offset littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */
  25120. /* Handle TypedArray vs. Node.js Buffer arg differences */
  25121. if (magic_typedarray) {
  25122. no_assert = 0;
  25123. #if defined(DUK_USE_INTEGER_LE)
  25124. endswap = !duk_to_boolean(thr, 1); /* 1=little endian */
  25125. #else
  25126. endswap = duk_to_boolean(thr, 1); /* 1=little endian */
  25127. #endif
  25128. } else {
  25129. no_assert = duk_to_boolean(thr, (magic_ftype == DUK__FLD_VARINT) ? 2 : 1);
  25130. #if defined(DUK_USE_INTEGER_LE)
  25131. endswap = magic_bigendian;
  25132. #else
  25133. endswap = !magic_bigendian;
  25134. #endif
  25135. }
  25136. /* Offset is coerced first to signed integer range and then to unsigned.
  25137. * This ensures we can add a small byte length (1-8) to the offset in
  25138. * bound checks and not wrap.
  25139. */
  25140. offset_signed = duk_to_int(thr, 0);
  25141. offset = (duk_uint_t) offset_signed;
  25142. if (offset_signed < 0) {
  25143. goto fail_bounds;
  25144. }
  25145. DUK_DDD(DUK_DDDPRINT("readfield, buffer_length=%ld, offset=%ld, no_assert=%d, "
  25146. "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, "
  25147. "endswap=%d",
  25148. (long) buffer_length, (long) offset, (int) no_assert,
  25149. (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3),
  25150. (int) (magic_signed >> 4), (int) endswap));
  25151. /* Update 'buffer_length' to be the effective, safe limit which
  25152. * takes into account the underlying buffer. This value will be
  25153. * potentially invalidated by any side effect.
  25154. */
  25155. check_length = DUK_HBUFOBJ_CLAMP_BYTELENGTH(h_this, buffer_length);
  25156. DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld",
  25157. (long) buffer_length, (long) check_length));
  25158. if (h_this->buf) {
  25159. buf = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this);
  25160. } else {
  25161. /* Neutered. We could go into the switch-case safely with
  25162. * buf == NULL because check_length == 0. To avoid scanbuild
  25163. * warnings, fail directly instead.
  25164. */
  25165. DUK_ASSERT(check_length == 0);
  25166. goto fail_neutered;
  25167. }
  25168. DUK_ASSERT(buf != NULL);
  25169. switch (magic_ftype) {
  25170. case DUK__FLD_8BIT: {
  25171. duk_uint8_t tmp;
  25172. if (offset + 1U > check_length) {
  25173. goto fail_bounds;
  25174. }
  25175. tmp = buf[offset];
  25176. if (magic_signed) {
  25177. duk_push_int(thr, (duk_int_t) ((duk_int8_t) tmp));
  25178. } else {
  25179. duk_push_uint(thr, (duk_uint_t) tmp);
  25180. }
  25181. break;
  25182. }
  25183. case DUK__FLD_16BIT: {
  25184. duk_uint16_t tmp;
  25185. if (offset + 2U > check_length) {
  25186. goto fail_bounds;
  25187. }
  25188. duk_memcpy((void *) du.uc, (const void *) (buf + offset), 2);
  25189. tmp = du.us[0];
  25190. if (endswap) {
  25191. tmp = DUK_BSWAP16(tmp);
  25192. }
  25193. if (magic_signed) {
  25194. duk_push_int(thr, (duk_int_t) ((duk_int16_t) tmp));
  25195. } else {
  25196. duk_push_uint(thr, (duk_uint_t) tmp);
  25197. }
  25198. break;
  25199. }
  25200. case DUK__FLD_32BIT: {
  25201. duk_uint32_t tmp;
  25202. if (offset + 4U > check_length) {
  25203. goto fail_bounds;
  25204. }
  25205. duk_memcpy((void *) du.uc, (const void *) (buf + offset), 4);
  25206. tmp = du.ui[0];
  25207. if (endswap) {
  25208. tmp = DUK_BSWAP32(tmp);
  25209. }
  25210. if (magic_signed) {
  25211. duk_push_int(thr, (duk_int_t) ((duk_int32_t) tmp));
  25212. } else {
  25213. duk_push_uint(thr, (duk_uint_t) tmp);
  25214. }
  25215. break;
  25216. }
  25217. case DUK__FLD_FLOAT: {
  25218. duk_uint32_t tmp;
  25219. if (offset + 4U > check_length) {
  25220. goto fail_bounds;
  25221. }
  25222. duk_memcpy((void *) du.uc, (const void *) (buf + offset), 4);
  25223. if (endswap) {
  25224. tmp = du.ui[0];
  25225. tmp = DUK_BSWAP32(tmp);
  25226. du.ui[0] = tmp;
  25227. }
  25228. duk_push_number(thr, (duk_double_t) du.f[0]);
  25229. break;
  25230. }
  25231. case DUK__FLD_DOUBLE: {
  25232. if (offset + 8U > check_length) {
  25233. goto fail_bounds;
  25234. }
  25235. duk_memcpy((void *) du.uc, (const void *) (buf + offset), 8);
  25236. if (endswap) {
  25237. DUK_DBLUNION_BSWAP64(&du);
  25238. }
  25239. duk_push_number(thr, (duk_double_t) du.d);
  25240. break;
  25241. }
  25242. case DUK__FLD_VARINT: {
  25243. /* Node.js Buffer variable width integer field. We don't really
  25244. * care about speed here, so aim for shortest algorithm.
  25245. */
  25246. duk_int_t field_bytelen;
  25247. duk_int_t i, i_step, i_end;
  25248. #if defined(DUK_USE_64BIT_OPS)
  25249. duk_int64_t tmp;
  25250. duk_small_uint_t shift_tmp;
  25251. #else
  25252. duk_double_t tmp;
  25253. duk_small_int_t highbyte;
  25254. #endif
  25255. const duk_uint8_t *p;
  25256. field_bytelen = duk_get_int(thr, 1); /* avoid side effects! */
  25257. if (field_bytelen < 1 || field_bytelen > 6) {
  25258. goto fail_field_length;
  25259. }
  25260. if (offset + (duk_uint_t) field_bytelen > check_length) {
  25261. goto fail_bounds;
  25262. }
  25263. p = (const duk_uint8_t *) (buf + offset);
  25264. /* Slow gathering of value using either 64-bit arithmetic
  25265. * or IEEE doubles if 64-bit types not available. Handling
  25266. * of negative numbers is a bit non-obvious in both cases.
  25267. */
  25268. if (magic_bigendian) {
  25269. /* Gather in big endian */
  25270. i = 0;
  25271. i_step = 1;
  25272. i_end = field_bytelen; /* one i_step over */
  25273. } else {
  25274. /* Gather in little endian */
  25275. i = field_bytelen - 1;
  25276. i_step = -1;
  25277. i_end = -1; /* one i_step over */
  25278. }
  25279. #if defined(DUK_USE_64BIT_OPS)
  25280. tmp = 0;
  25281. do {
  25282. DUK_ASSERT(i >= 0 && i < field_bytelen);
  25283. tmp = (tmp << 8) + (duk_int64_t) p[i];
  25284. i += i_step;
  25285. } while (i != i_end);
  25286. if (magic_signed) {
  25287. /* Shift to sign extend. Left shift must be unsigned
  25288. * to avoid undefined behavior; right shift must be
  25289. * signed to sign extend properly.
  25290. */
  25291. shift_tmp = (duk_small_uint_t) (64U - (duk_small_uint_t) field_bytelen * 8U);
  25292. tmp = (duk_int64_t) ((duk_uint64_t) tmp << shift_tmp) >> shift_tmp;
  25293. }
  25294. duk_push_i64(thr, tmp);
  25295. #else
  25296. highbyte = p[i];
  25297. if (magic_signed && (highbyte & 0x80) != 0) {
  25298. /* 0xff => 255 - 256 = -1; 0x80 => 128 - 256 = -128 */
  25299. tmp = (duk_double_t) (highbyte - 256);
  25300. } else {
  25301. tmp = (duk_double_t) highbyte;
  25302. }
  25303. for (;;) {
  25304. i += i_step;
  25305. if (i == i_end) {
  25306. break;
  25307. }
  25308. DUK_ASSERT(i >= 0 && i < field_bytelen);
  25309. tmp = (tmp * 256.0) + (duk_double_t) p[i];
  25310. }
  25311. duk_push_number(thr, tmp);
  25312. #endif
  25313. break;
  25314. }
  25315. default: { /* should never happen but default here */
  25316. goto fail_bounds;
  25317. }
  25318. }
  25319. return 1;
  25320. fail_neutered:
  25321. fail_field_length:
  25322. fail_bounds:
  25323. if (no_assert) {
  25324. /* Node.js return value for noAssert out-of-bounds reads is
  25325. * usually (but not always) NaN. Return NaN consistently.
  25326. */
  25327. duk_push_nan(thr);
  25328. return 1;
  25329. }
  25330. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  25331. }
  25332. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  25333. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  25334. /* XXX: split into separate functions for each field type? */
  25335. DUK_INTERNAL duk_ret_t duk_bi_buffer_writefield(duk_hthread *thr) {
  25336. duk_small_int_t magic = (duk_small_int_t) duk_get_current_magic(thr);
  25337. duk_small_int_t magic_ftype;
  25338. duk_small_int_t magic_bigendian;
  25339. duk_small_int_t magic_signed;
  25340. duk_small_int_t magic_typedarray;
  25341. duk_small_int_t endswap;
  25342. duk_hbufobj *h_this;
  25343. duk_bool_t no_assert;
  25344. duk_int_t offset_signed;
  25345. duk_uint_t offset;
  25346. duk_uint_t buffer_length;
  25347. duk_uint_t check_length;
  25348. duk_uint8_t *buf;
  25349. duk_double_union du;
  25350. duk_int_t nbytes = 0;
  25351. magic_ftype = magic & 0x0007;
  25352. magic_bigendian = magic & 0x0008;
  25353. magic_signed = magic & 0x0010;
  25354. magic_typedarray = magic & 0x0020;
  25355. DUK_UNREF(magic_signed);
  25356. h_this = duk__require_bufobj_this(thr); /* XXX: very inefficient for plain buffers */
  25357. DUK_ASSERT(h_this != NULL);
  25358. buffer_length = h_this->length;
  25359. /* [ value offset noAssert ], when ftype != DUK__FLD_VARINT */
  25360. /* [ value offset fieldByteLength noAssert ], when ftype == DUK__FLD_VARINT */
  25361. /* [ offset value littleEndian ], when DUK__FLD_TYPEDARRAY (regardless of ftype) */
  25362. /* Handle TypedArray vs. Node.js Buffer arg differences */
  25363. if (magic_typedarray) {
  25364. no_assert = 0;
  25365. #if defined(DUK_USE_INTEGER_LE)
  25366. endswap = !duk_to_boolean(thr, 2); /* 1=little endian */
  25367. #else
  25368. endswap = duk_to_boolean(thr, 2); /* 1=little endian */
  25369. #endif
  25370. duk_swap(thr, 0, 1); /* offset/value order different from Node.js */
  25371. } else {
  25372. no_assert = duk_to_boolean(thr, (magic_ftype == DUK__FLD_VARINT) ? 3 : 2);
  25373. #if defined(DUK_USE_INTEGER_LE)
  25374. endswap = magic_bigendian;
  25375. #else
  25376. endswap = !magic_bigendian;
  25377. #endif
  25378. }
  25379. /* Offset is coerced first to signed integer range and then to unsigned.
  25380. * This ensures we can add a small byte length (1-8) to the offset in
  25381. * bound checks and not wrap.
  25382. */
  25383. offset_signed = duk_to_int(thr, 1);
  25384. offset = (duk_uint_t) offset_signed;
  25385. /* We need 'nbytes' even for a failed offset; return value must be
  25386. * (offset + nbytes) even when write fails due to invalid offset.
  25387. */
  25388. if (magic_ftype != DUK__FLD_VARINT) {
  25389. DUK_ASSERT(magic_ftype >= 0 && magic_ftype < (duk_small_int_t) (sizeof(duk__buffer_nbytes_from_fldtype) / sizeof(duk_uint8_t)));
  25390. nbytes = duk__buffer_nbytes_from_fldtype[magic_ftype];
  25391. } else {
  25392. nbytes = duk_get_int(thr, 2);
  25393. if (nbytes < 1 || nbytes > 6) {
  25394. goto fail_field_length;
  25395. }
  25396. }
  25397. DUK_ASSERT(nbytes >= 1 && nbytes <= 8);
  25398. /* Now we can check offset validity. */
  25399. if (offset_signed < 0) {
  25400. goto fail_bounds;
  25401. }
  25402. DUK_DDD(DUK_DDDPRINT("writefield, value=%!T, buffer_length=%ld, offset=%ld, no_assert=%d, "
  25403. "magic=%04x, magic_fieldtype=%d, magic_bigendian=%d, magic_signed=%d, "
  25404. "endswap=%d",
  25405. duk_get_tval(thr, 0), (long) buffer_length, (long) offset, (int) no_assert,
  25406. (unsigned int) magic, (int) magic_ftype, (int) (magic_bigendian >> 3),
  25407. (int) (magic_signed >> 4), (int) endswap));
  25408. /* Coerce value to a number before computing check_length, so that
  25409. * the field type specific coercion below can't have side effects
  25410. * that would invalidate check_length.
  25411. */
  25412. duk_to_number(thr, 0);
  25413. /* Update 'buffer_length' to be the effective, safe limit which
  25414. * takes into account the underlying buffer. This value will be
  25415. * potentially invalidated by any side effect.
  25416. */
  25417. check_length = DUK_HBUFOBJ_CLAMP_BYTELENGTH(h_this, buffer_length);
  25418. DUK_DDD(DUK_DDDPRINT("buffer_length=%ld, check_length=%ld",
  25419. (long) buffer_length, (long) check_length));
  25420. if (h_this->buf) {
  25421. buf = DUK_HBUFOBJ_GET_SLICE_BASE(thr->heap, h_this);
  25422. } else {
  25423. /* Neutered. We could go into the switch-case safely with
  25424. * buf == NULL because check_length == 0. To avoid scanbuild
  25425. * warnings, fail directly instead.
  25426. */
  25427. DUK_ASSERT(check_length == 0);
  25428. goto fail_neutered;
  25429. }
  25430. DUK_ASSERT(buf != NULL);
  25431. switch (magic_ftype) {
  25432. case DUK__FLD_8BIT: {
  25433. if (offset + 1U > check_length) {
  25434. goto fail_bounds;
  25435. }
  25436. /* sign doesn't matter when writing */
  25437. buf[offset] = (duk_uint8_t) duk_to_uint32(thr, 0);
  25438. break;
  25439. }
  25440. case DUK__FLD_16BIT: {
  25441. duk_uint16_t tmp;
  25442. if (offset + 2U > check_length) {
  25443. goto fail_bounds;
  25444. }
  25445. tmp = (duk_uint16_t) duk_to_uint32(thr, 0);
  25446. if (endswap) {
  25447. tmp = DUK_BSWAP16(tmp);
  25448. }
  25449. du.us[0] = tmp;
  25450. /* sign doesn't matter when writing */
  25451. duk_memcpy((void *) (buf + offset), (const void *) du.uc, 2);
  25452. break;
  25453. }
  25454. case DUK__FLD_32BIT: {
  25455. duk_uint32_t tmp;
  25456. if (offset + 4U > check_length) {
  25457. goto fail_bounds;
  25458. }
  25459. tmp = (duk_uint32_t) duk_to_uint32(thr, 0);
  25460. if (endswap) {
  25461. tmp = DUK_BSWAP32(tmp);
  25462. }
  25463. du.ui[0] = tmp;
  25464. /* sign doesn't matter when writing */
  25465. duk_memcpy((void *) (buf + offset), (const void *) du.uc, 4);
  25466. break;
  25467. }
  25468. case DUK__FLD_FLOAT: {
  25469. duk_uint32_t tmp;
  25470. if (offset + 4U > check_length) {
  25471. goto fail_bounds;
  25472. }
  25473. du.f[0] = (duk_float_t) duk_to_number(thr, 0);
  25474. if (endswap) {
  25475. tmp = du.ui[0];
  25476. tmp = DUK_BSWAP32(tmp);
  25477. du.ui[0] = tmp;
  25478. }
  25479. /* sign doesn't matter when writing */
  25480. duk_memcpy((void *) (buf + offset), (const void *) du.uc, 4);
  25481. break;
  25482. }
  25483. case DUK__FLD_DOUBLE: {
  25484. if (offset + 8U > check_length) {
  25485. goto fail_bounds;
  25486. }
  25487. du.d = (duk_double_t) duk_to_number(thr, 0);
  25488. if (endswap) {
  25489. DUK_DBLUNION_BSWAP64(&du);
  25490. }
  25491. /* sign doesn't matter when writing */
  25492. duk_memcpy((void *) (buf + offset), (const void *) du.uc, 8);
  25493. break;
  25494. }
  25495. case DUK__FLD_VARINT: {
  25496. /* Node.js Buffer variable width integer field. We don't really
  25497. * care about speed here, so aim for shortest algorithm.
  25498. */
  25499. duk_int_t field_bytelen;
  25500. duk_int_t i, i_step, i_end;
  25501. #if defined(DUK_USE_64BIT_OPS)
  25502. duk_int64_t tmp;
  25503. #else
  25504. duk_double_t tmp;
  25505. #endif
  25506. duk_uint8_t *p;
  25507. field_bytelen = (duk_int_t) nbytes;
  25508. if (offset + (duk_uint_t) field_bytelen > check_length) {
  25509. goto fail_bounds;
  25510. }
  25511. /* Slow writing of value using either 64-bit arithmetic
  25512. * or IEEE doubles if 64-bit types not available. There's
  25513. * no special sign handling when writing varints.
  25514. */
  25515. if (magic_bigendian) {
  25516. /* Write in big endian */
  25517. i = field_bytelen; /* one i_step added at top of loop */
  25518. i_step = -1;
  25519. i_end = 0;
  25520. } else {
  25521. /* Write in little endian */
  25522. i = -1; /* one i_step added at top of loop */
  25523. i_step = 1;
  25524. i_end = field_bytelen - 1;
  25525. }
  25526. /* XXX: The duk_to_number() cast followed by integer coercion
  25527. * is platform specific so NaN, +/- Infinity, and out-of-bounds
  25528. * values result in platform specific output now.
  25529. * See: test-bi-nodejs-buffer-proto-varint-special.js
  25530. */
  25531. #if defined(DUK_USE_64BIT_OPS)
  25532. tmp = (duk_int64_t) duk_to_number(thr, 0);
  25533. p = (duk_uint8_t *) (buf + offset);
  25534. do {
  25535. i += i_step;
  25536. DUK_ASSERT(i >= 0 && i < field_bytelen);
  25537. p[i] = (duk_uint8_t) (tmp & 0xff);
  25538. tmp = tmp >> 8; /* unnecessary shift for last byte */
  25539. } while (i != i_end);
  25540. #else
  25541. tmp = duk_to_number(thr, 0);
  25542. p = (duk_uint8_t *) (buf + offset);
  25543. do {
  25544. i += i_step;
  25545. tmp = DUK_FLOOR(tmp);
  25546. DUK_ASSERT(i >= 0 && i < field_bytelen);
  25547. p[i] = (duk_uint8_t) (DUK_FMOD(tmp, 256.0));
  25548. tmp = tmp / 256.0; /* unnecessary div for last byte */
  25549. } while (i != i_end);
  25550. #endif
  25551. break;
  25552. }
  25553. default: { /* should never happen but default here */
  25554. goto fail_bounds;
  25555. }
  25556. }
  25557. /* Node.js Buffer: return offset + #bytes written (i.e. next
  25558. * write offset).
  25559. */
  25560. if (magic_typedarray) {
  25561. /* For TypedArrays 'undefined' return value is specified
  25562. * by ES2015 (matches V8).
  25563. */
  25564. return 0;
  25565. }
  25566. duk_push_uint(thr, offset + (duk_uint_t) nbytes);
  25567. return 1;
  25568. fail_neutered:
  25569. fail_field_length:
  25570. fail_bounds:
  25571. if (no_assert) {
  25572. /* Node.js return value for failed writes is offset + #bytes
  25573. * that would have been written.
  25574. */
  25575. /* XXX: for negative input offsets, 'offset' will be a large
  25576. * positive value so the result here is confusing.
  25577. */
  25578. if (magic_typedarray) {
  25579. return 0;
  25580. }
  25581. duk_push_uint(thr, offset + (duk_uint_t) nbytes);
  25582. return 1;
  25583. }
  25584. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  25585. }
  25586. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  25587. /*
  25588. * Accessors for .buffer, .byteLength, .byteOffset
  25589. */
  25590. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  25591. DUK_LOCAL duk_hbufobj *duk__autospawn_arraybuffer(duk_hthread *thr, duk_hbuffer *h_buf) {
  25592. duk_hbufobj *h_res;
  25593. h_res = duk_push_bufobj_raw(thr,
  25594. DUK_HOBJECT_FLAG_EXTENSIBLE |
  25595. DUK_HOBJECT_FLAG_BUFOBJ |
  25596. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARRAYBUFFER),
  25597. DUK_BIDX_ARRAYBUFFER_PROTOTYPE);
  25598. DUK_ASSERT(h_res != NULL);
  25599. DUK_UNREF(h_res);
  25600. duk__set_bufobj_buffer(thr, h_res, h_buf);
  25601. DUK_ASSERT_HBUFOBJ_VALID(h_res);
  25602. DUK_ASSERT(h_res->buf_prop == NULL);
  25603. return h_res;
  25604. }
  25605. DUK_INTERNAL duk_ret_t duk_bi_typedarray_buffer_getter(duk_hthread *thr) {
  25606. duk_hbufobj *h_bufobj;
  25607. h_bufobj = (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_THROW /*flags*/);
  25608. DUK_ASSERT(h_bufobj != NULL);
  25609. if (DUK_HEAPHDR_IS_BUFFER((duk_heaphdr *) h_bufobj)) {
  25610. DUK_DD(DUK_DDPRINT("autospawn ArrayBuffer for plain buffer"));
  25611. (void) duk__autospawn_arraybuffer(thr, (duk_hbuffer *) h_bufobj);
  25612. return 1;
  25613. } else {
  25614. if (h_bufobj->buf_prop == NULL &&
  25615. DUK_HOBJECT_GET_CLASS_NUMBER((duk_hobject *) h_bufobj) != DUK_HOBJECT_CLASS_ARRAYBUFFER &&
  25616. h_bufobj->buf != NULL) {
  25617. duk_hbufobj *h_arrbuf;
  25618. DUK_DD(DUK_DDPRINT("autospawn ArrayBuffer for typed array or DataView"));
  25619. h_arrbuf = duk__autospawn_arraybuffer(thr, h_bufobj->buf);
  25620. if (h_bufobj->buf_prop == NULL) {
  25621. /* Must recheck buf_prop, in case ArrayBuffer
  25622. * alloc had a side effect which already filled
  25623. * it!
  25624. */
  25625. /* Set ArrayBuffer's .byteOffset and .byteLength based
  25626. * on the view so that Arraybuffer[view.byteOffset]
  25627. * matches view[0].
  25628. */
  25629. h_arrbuf->offset = 0;
  25630. DUK_ASSERT(h_bufobj->offset + h_bufobj->length >= h_bufobj->offset); /* Wrap check on creation. */
  25631. h_arrbuf->length = h_bufobj->offset + h_bufobj->length;
  25632. DUK_ASSERT(h_arrbuf->buf_prop == NULL);
  25633. DUK_ASSERT(h_bufobj->buf_prop == NULL);
  25634. h_bufobj->buf_prop = (duk_hobject *) h_arrbuf;
  25635. DUK_HBUFOBJ_INCREF(thr, h_arrbuf); /* Now reachable and accounted for. */
  25636. }
  25637. /* Left on stack; pushed for the second time below (OK). */
  25638. }
  25639. if (h_bufobj->buf_prop) {
  25640. duk_push_hobject(thr, h_bufobj->buf_prop);
  25641. return 1;
  25642. }
  25643. }
  25644. return 0;
  25645. }
  25646. DUK_INTERNAL duk_ret_t duk_bi_typedarray_byteoffset_getter(duk_hthread *thr) {
  25647. duk_hbufobj *h_bufobj;
  25648. h_bufobj = (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_THROW /*flags*/);
  25649. DUK_ASSERT(h_bufobj != NULL);
  25650. if (DUK_HEAPHDR_IS_BUFFER((duk_heaphdr *) h_bufobj)) {
  25651. duk_push_uint(thr, 0);
  25652. } else {
  25653. /* If neutered must return 0; offset is zeroed during
  25654. * neutering.
  25655. */
  25656. duk_push_uint(thr, h_bufobj->offset);
  25657. }
  25658. return 1;
  25659. }
  25660. DUK_INTERNAL duk_ret_t duk_bi_typedarray_bytelength_getter(duk_hthread *thr) {
  25661. duk_hbufobj *h_bufobj;
  25662. h_bufobj = (duk_hbufobj *) duk__getrequire_bufobj_this(thr, DUK__BUFOBJ_FLAG_THROW /*flags*/);
  25663. DUK_ASSERT(h_bufobj != NULL);
  25664. if (DUK_HEAPHDR_IS_BUFFER((duk_heaphdr *) h_bufobj)) {
  25665. duk_hbuffer *h_buf;
  25666. h_buf = (duk_hbuffer *) h_bufobj;
  25667. DUK_ASSERT(DUK_HBUFFER_GET_SIZE(h_buf) <= DUK_UINT_MAX); /* Buffer limits. */
  25668. duk_push_uint(thr, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_buf));
  25669. } else {
  25670. /* If neutered must return 0; length is zeroed during
  25671. * neutering.
  25672. */
  25673. duk_push_uint(thr, h_bufobj->length);
  25674. }
  25675. return 1;
  25676. }
  25677. #else /* DUK_USE_BUFFEROBJECT_SUPPORT */
  25678. /* No .buffer getter without ArrayBuffer support. */
  25679. #if 0
  25680. DUK_INTERNAL duk_ret_t duk_bi_typedarray_buffer_getter(duk_hthread *thr) {
  25681. return 0;
  25682. }
  25683. #endif
  25684. DUK_INTERNAL duk_ret_t duk_bi_typedarray_byteoffset_getter(duk_hthread *thr) {
  25685. duk_push_uint(thr, 0);
  25686. return 1;
  25687. }
  25688. DUK_INTERNAL duk_ret_t duk_bi_typedarray_bytelength_getter(duk_hthread *thr) {
  25689. duk_hbuffer *h_buf;
  25690. /* XXX: helper? */
  25691. duk_push_this(thr);
  25692. h_buf = duk_require_hbuffer(thr, -1);
  25693. duk_push_uint(thr, DUK_HBUFFER_GET_SIZE(h_buf));
  25694. return 1;
  25695. }
  25696. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  25697. /* automatic undefs */
  25698. #undef DUK__BUFOBJ_FLAG_PROMOTE
  25699. #undef DUK__BUFOBJ_FLAG_THROW
  25700. #undef DUK__FLD_16BIT
  25701. #undef DUK__FLD_32BIT
  25702. #undef DUK__FLD_8BIT
  25703. #undef DUK__FLD_BIGENDIAN
  25704. #undef DUK__FLD_DOUBLE
  25705. #undef DUK__FLD_FLOAT
  25706. #undef DUK__FLD_SIGNED
  25707. #undef DUK__FLD_TYPEDARRAY
  25708. #undef DUK__FLD_VARINT
  25709. #line 1 "duk_bi_date.c"
  25710. /*
  25711. * Date built-ins
  25712. *
  25713. * Unlike most built-ins, Date has some platform dependencies for getting
  25714. * UTC time, converting between UTC and local time, and parsing and
  25715. * formatting time values. These are all abstracted behind DUK_USE_xxx
  25716. * config options. There are built-in platform specific providers for
  25717. * POSIX and Windows, but external providers can also be used.
  25718. *
  25719. * See doc/datetime.rst.
  25720. *
  25721. */
  25722. /* #include duk_internal.h -> already included */
  25723. /* XXX: currently defines unnecessary symbols when DUK_USE_DATE_BUILTIN is disabled. */
  25724. /*
  25725. * Forward declarations
  25726. */
  25727. DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval_tzoffset(duk_hthread *thr, duk_small_uint_t flags, duk_int_t *out_tzoffset);
  25728. DUK_LOCAL_DECL duk_double_t duk__push_this_get_timeval(duk_hthread *thr, duk_small_uint_t flags);
  25729. DUK_LOCAL_DECL void duk__twodigit_year_fixup(duk_hthread *thr, duk_idx_t idx_val);
  25730. DUK_LOCAL_DECL duk_ret_t duk__set_this_timeval_from_dparts(duk_hthread *thr, duk_double_t *dparts, duk_small_uint_t flags);
  25731. /*
  25732. * Other file level defines
  25733. */
  25734. /* Debug macro to print all parts and dparts (used manually because of debug level). */
  25735. #define DUK__DPRINT_PARTS_AND_DPARTS(parts,dparts) do { \
  25736. DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld, dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
  25737. (long) (parts)[0], (long) (parts)[1], \
  25738. (long) (parts)[2], (long) (parts)[3], \
  25739. (long) (parts)[4], (long) (parts)[5], \
  25740. (long) (parts)[6], (long) (parts)[7], \
  25741. (double) (dparts)[0], (double) (dparts)[1], \
  25742. (double) (dparts)[2], (double) (dparts)[3], \
  25743. (double) (dparts)[4], (double) (dparts)[5], \
  25744. (double) (dparts)[6], (double) (dparts)[7])); \
  25745. } while (0)
  25746. #define DUK__DPRINT_PARTS(parts) do { \
  25747. DUK_D(DUK_DPRINT("parts: %ld %ld %ld %ld %ld %ld %ld %ld", \
  25748. (long) (parts)[0], (long) (parts)[1], \
  25749. (long) (parts)[2], (long) (parts)[3], \
  25750. (long) (parts)[4], (long) (parts)[5], \
  25751. (long) (parts)[6], (long) (parts)[7])); \
  25752. } while (0)
  25753. #define DUK__DPRINT_DPARTS(dparts) do { \
  25754. DUK_D(DUK_DPRINT("dparts: %lf %lf %lf %lf %lf %lf %lf %lf", \
  25755. (double) (dparts)[0], (double) (dparts)[1], \
  25756. (double) (dparts)[2], (double) (dparts)[3], \
  25757. (double) (dparts)[4], (double) (dparts)[5], \
  25758. (double) (dparts)[6], (double) (dparts)[7])); \
  25759. } while (0)
  25760. /* Equivalent year for DST calculations outside [1970,2038[ range, see
  25761. * E5 Section 15.9.1.8. Equivalent year has the same leap-year-ness and
  25762. * starts with the same weekday on Jan 1.
  25763. * https://bugzilla.mozilla.org/show_bug.cgi?id=351066
  25764. */
  25765. #define DUK__YEAR(x) ((duk_uint8_t) ((x) - 1970))
  25766. DUK_LOCAL duk_uint8_t duk__date_equivyear[14] = {
  25767. #if 1
  25768. /* This is based on V8 EquivalentYear() algorithm (see util/genequivyear.py):
  25769. * http://code.google.com/p/v8/source/browse/trunk/src/date.h#146
  25770. */
  25771. /* non-leap year: sunday, monday, ... */
  25772. DUK__YEAR(2023), DUK__YEAR(2035), DUK__YEAR(2019), DUK__YEAR(2031),
  25773. DUK__YEAR(2015), DUK__YEAR(2027), DUK__YEAR(2011),
  25774. /* leap year: sunday, monday, ... */
  25775. DUK__YEAR(2012), DUK__YEAR(2024), DUK__YEAR(2008), DUK__YEAR(2020),
  25776. DUK__YEAR(2032), DUK__YEAR(2016), DUK__YEAR(2028)
  25777. #endif
  25778. #if 0
  25779. /* This is based on Rhino EquivalentYear() algorithm:
  25780. * https://github.com/mozilla/rhino/blob/f99cc11d616f0cdda2c42bde72b3484df6182947/src/org/mozilla/javascript/NativeDate.java
  25781. */
  25782. /* non-leap year: sunday, monday, ... */
  25783. DUK__YEAR(1978), DUK__YEAR(1973), DUK__YEAR(1985), DUK__YEAR(1986),
  25784. DUK__YEAR(1981), DUK__YEAR(1971), DUK__YEAR(1977),
  25785. /* leap year: sunday, monday, ... */
  25786. DUK__YEAR(1984), DUK__YEAR(1996), DUK__YEAR(1980), DUK__YEAR(1992),
  25787. DUK__YEAR(1976), DUK__YEAR(1988), DUK__YEAR(1972)
  25788. #endif
  25789. };
  25790. /*
  25791. * ISO 8601 subset parser.
  25792. */
  25793. /* Parser part count. */
  25794. #define DUK__NUM_ISO8601_PARSER_PARTS 9
  25795. /* Parser part indices. */
  25796. #define DUK__PI_YEAR 0
  25797. #define DUK__PI_MONTH 1
  25798. #define DUK__PI_DAY 2
  25799. #define DUK__PI_HOUR 3
  25800. #define DUK__PI_MINUTE 4
  25801. #define DUK__PI_SECOND 5
  25802. #define DUK__PI_MILLISECOND 6
  25803. #define DUK__PI_TZHOUR 7
  25804. #define DUK__PI_TZMINUTE 8
  25805. /* Parser part masks. */
  25806. #define DUK__PM_YEAR (1 << DUK__PI_YEAR)
  25807. #define DUK__PM_MONTH (1 << DUK__PI_MONTH)
  25808. #define DUK__PM_DAY (1 << DUK__PI_DAY)
  25809. #define DUK__PM_HOUR (1 << DUK__PI_HOUR)
  25810. #define DUK__PM_MINUTE (1 << DUK__PI_MINUTE)
  25811. #define DUK__PM_SECOND (1 << DUK__PI_SECOND)
  25812. #define DUK__PM_MILLISECOND (1 << DUK__PI_MILLISECOND)
  25813. #define DUK__PM_TZHOUR (1 << DUK__PI_TZHOUR)
  25814. #define DUK__PM_TZMINUTE (1 << DUK__PI_TZMINUTE)
  25815. /* Parser separator indices. */
  25816. #define DUK__SI_PLUS 0
  25817. #define DUK__SI_MINUS 1
  25818. #define DUK__SI_T 2
  25819. #define DUK__SI_SPACE 3
  25820. #define DUK__SI_COLON 4
  25821. #define DUK__SI_PERIOD 5
  25822. #define DUK__SI_Z 6
  25823. #define DUK__SI_NUL 7
  25824. /* Parser separator masks. */
  25825. #define DUK__SM_PLUS (1 << DUK__SI_PLUS)
  25826. #define DUK__SM_MINUS (1 << DUK__SI_MINUS)
  25827. #define DUK__SM_T (1 << DUK__SI_T)
  25828. #define DUK__SM_SPACE (1 << DUK__SI_SPACE)
  25829. #define DUK__SM_COLON (1 << DUK__SI_COLON)
  25830. #define DUK__SM_PERIOD (1 << DUK__SI_PERIOD)
  25831. #define DUK__SM_Z (1 << DUK__SI_Z)
  25832. #define DUK__SM_NUL (1 << DUK__SI_NUL)
  25833. /* Rule control flags. */
  25834. #define DUK__CF_NEG (1 << 0) /* continue matching, set neg_tzoffset flag */
  25835. #define DUK__CF_ACCEPT (1 << 1) /* accept string */
  25836. #define DUK__CF_ACCEPT_NUL (1 << 2) /* accept string if next char is NUL (otherwise reject) */
  25837. #define DUK__PACK_RULE(partmask,sepmask,nextpart,flags) \
  25838. ((duk_uint32_t) (partmask) + \
  25839. (((duk_uint32_t) (sepmask)) << 9) + \
  25840. (((duk_uint32_t) (nextpart)) << 17) + \
  25841. (((duk_uint32_t) (flags)) << 21))
  25842. #define DUK__UNPACK_RULE(rule,var_nextidx,var_flags) do { \
  25843. (var_nextidx) = (duk_small_uint_t) (((rule) >> 17) & 0x0f); \
  25844. (var_flags) = (duk_small_uint_t) ((rule) >> 21); \
  25845. } while (0)
  25846. #define DUK__RULE_MASK_PART_SEP 0x1ffffUL
  25847. /* Matching separator index is used in the control table */
  25848. DUK_LOCAL const duk_uint8_t duk__parse_iso8601_seps[] = {
  25849. DUK_ASC_PLUS /*0*/, DUK_ASC_MINUS /*1*/, DUK_ASC_UC_T /*2*/, DUK_ASC_SPACE /*3*/,
  25850. DUK_ASC_COLON /*4*/, DUK_ASC_PERIOD /*5*/, DUK_ASC_UC_Z /*6*/, DUK_ASC_NUL /*7*/
  25851. };
  25852. /* Rule table: first matching rule is used to determine what to do next. */
  25853. DUK_LOCAL const duk_uint32_t duk__parse_iso8601_control[] = {
  25854. DUK__PACK_RULE(DUK__PM_YEAR, DUK__SM_MINUS, DUK__PI_MONTH, 0),
  25855. DUK__PACK_RULE(DUK__PM_MONTH, DUK__SM_MINUS, DUK__PI_DAY, 0),
  25856. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY, DUK__SM_T | DUK__SM_SPACE, DUK__PI_HOUR, 0),
  25857. DUK__PACK_RULE(DUK__PM_HOUR, DUK__SM_COLON, DUK__PI_MINUTE, 0),
  25858. DUK__PACK_RULE(DUK__PM_MINUTE, DUK__SM_COLON, DUK__PI_SECOND, 0),
  25859. DUK__PACK_RULE(DUK__PM_SECOND, DUK__SM_PERIOD, DUK__PI_MILLISECOND, 0),
  25860. DUK__PACK_RULE(DUK__PM_TZHOUR, DUK__SM_COLON, DUK__PI_TZMINUTE, 0),
  25861. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_PLUS, DUK__PI_TZHOUR, 0),
  25862. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_MINUS, DUK__PI_TZHOUR, DUK__CF_NEG),
  25863. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND, DUK__SM_Z, 0, DUK__CF_ACCEPT_NUL),
  25864. DUK__PACK_RULE(DUK__PM_YEAR | DUK__PM_MONTH | DUK__PM_DAY | DUK__PM_HOUR /*Note1*/ | DUK__PM_MINUTE | DUK__PM_SECOND | DUK__PM_MILLISECOND | DUK__PM_TZHOUR /*Note2*/ | DUK__PM_TZMINUTE, DUK__SM_NUL, 0, DUK__CF_ACCEPT)
  25865. /* Note1: the specification doesn't require matching a time form with
  25866. * just hours ("HH"), but we accept it here, e.g. "2012-01-02T12Z".
  25867. *
  25868. * Note2: the specification doesn't require matching a timezone offset
  25869. * with just hours ("HH"), but accept it here, e.g. "2012-01-02T03:04:05+02"
  25870. */
  25871. };
  25872. DUK_LOCAL duk_bool_t duk__parse_string_iso8601_subset(duk_hthread *thr, const char *str) {
  25873. duk_int_t parts[DUK__NUM_ISO8601_PARSER_PARTS];
  25874. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  25875. duk_double_t d;
  25876. const duk_uint8_t *p;
  25877. duk_small_uint_t part_idx = 0;
  25878. duk_int_t accum = 0;
  25879. duk_small_uint_t ndigits = 0;
  25880. duk_bool_t neg_year = 0;
  25881. duk_bool_t neg_tzoffset = 0;
  25882. duk_uint_fast8_t ch;
  25883. duk_small_uint_t i;
  25884. /* During parsing, month and day are one-based; set defaults here. */
  25885. duk_memzero(parts, sizeof(parts));
  25886. DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] == 0); /* don't care value, year is mandatory */
  25887. parts[DUK_DATE_IDX_MONTH] = 1;
  25888. parts[DUK_DATE_IDX_DAY] = 1;
  25889. /* Special handling for year sign. */
  25890. p = (const duk_uint8_t *) str;
  25891. ch = p[0];
  25892. if (ch == DUK_ASC_PLUS) {
  25893. p++;
  25894. } else if (ch == DUK_ASC_MINUS) {
  25895. neg_year = 1;
  25896. p++;
  25897. }
  25898. for (;;) {
  25899. ch = *p++;
  25900. DUK_DDD(DUK_DDDPRINT("parsing, part_idx=%ld, char=%ld ('%c')",
  25901. (long) part_idx, (long) ch,
  25902. (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : DUK_ASC_QUESTION)));
  25903. if (ch >= DUK_ASC_0 && ch <= DUK_ASC_9) {
  25904. if (ndigits >= 9) {
  25905. DUK_DDD(DUK_DDDPRINT("too many digits -> reject"));
  25906. goto reject;
  25907. }
  25908. if (part_idx == DUK__PI_MILLISECOND && ndigits >= 3) {
  25909. /* ignore millisecond fractions after 3 */
  25910. } else {
  25911. accum = accum * 10 + ((duk_int_t) ch) - ((duk_int_t) DUK_ASC_0) + 0x00;
  25912. ndigits++;
  25913. }
  25914. } else {
  25915. duk_uint_fast32_t match_val;
  25916. duk_small_uint_t sep_idx;
  25917. if (ndigits <= 0) {
  25918. goto reject;
  25919. }
  25920. if (part_idx == DUK__PI_MILLISECOND) {
  25921. /* complete the millisecond field */
  25922. while (ndigits < 3) {
  25923. accum *= 10;
  25924. ndigits++;
  25925. }
  25926. }
  25927. parts[part_idx] = accum;
  25928. DUK_DDD(DUK_DDDPRINT("wrote part %ld -> value %ld", (long) part_idx, (long) accum));
  25929. accum = 0;
  25930. ndigits = 0;
  25931. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t)); i++) {
  25932. if (duk__parse_iso8601_seps[i] == ch) {
  25933. break;
  25934. }
  25935. }
  25936. if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_seps) / sizeof(duk_uint8_t))) {
  25937. DUK_DDD(DUK_DDDPRINT("separator character doesn't match -> reject"));
  25938. goto reject;
  25939. }
  25940. sep_idx = i;
  25941. match_val = (1UL << part_idx) + (1UL << (sep_idx + 9)); /* match against rule part/sep bits */
  25942. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t)); i++) {
  25943. duk_uint_fast32_t rule = duk__parse_iso8601_control[i];
  25944. duk_small_uint_t nextpart;
  25945. duk_small_uint_t cflags;
  25946. DUK_DDD(DUK_DDDPRINT("part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, considering rule=0x%08lx",
  25947. (long) part_idx, (long) sep_idx,
  25948. (unsigned long) match_val, (unsigned long) rule));
  25949. if ((rule & match_val) != match_val) {
  25950. continue;
  25951. }
  25952. DUK__UNPACK_RULE(rule, nextpart, cflags);
  25953. DUK_DDD(DUK_DDDPRINT("rule match -> part_idx=%ld, sep_idx=%ld, match_val=0x%08lx, "
  25954. "rule=0x%08lx -> nextpart=%ld, cflags=0x%02lx",
  25955. (long) part_idx, (long) sep_idx,
  25956. (unsigned long) match_val, (unsigned long) rule,
  25957. (long) nextpart, (unsigned long) cflags));
  25958. if (cflags & DUK__CF_NEG) {
  25959. neg_tzoffset = 1;
  25960. }
  25961. if (cflags & DUK__CF_ACCEPT) {
  25962. goto accept;
  25963. }
  25964. if (cflags & DUK__CF_ACCEPT_NUL) {
  25965. DUK_ASSERT(*(p - 1) != (char) 0);
  25966. if (*p == DUK_ASC_NUL) {
  25967. goto accept;
  25968. }
  25969. goto reject;
  25970. }
  25971. part_idx = nextpart;
  25972. break;
  25973. } /* rule match */
  25974. if (i == (duk_small_uint_t) (sizeof(duk__parse_iso8601_control) / sizeof(duk_uint32_t))) {
  25975. DUK_DDD(DUK_DDDPRINT("no rule matches -> reject"));
  25976. goto reject;
  25977. }
  25978. if (ch == 0) {
  25979. /* This shouldn't be necessary, but check just in case
  25980. * to avoid any chance of overruns.
  25981. */
  25982. DUK_DDD(DUK_DDDPRINT("NUL after rule matching (should not happen) -> reject"));
  25983. goto reject;
  25984. }
  25985. } /* if-digit-else-ctrl */
  25986. } /* char loop */
  25987. /* We should never exit the loop above. */
  25988. DUK_UNREACHABLE();
  25989. reject:
  25990. DUK_DDD(DUK_DDDPRINT("reject"));
  25991. return 0;
  25992. accept:
  25993. DUK_DDD(DUK_DDDPRINT("accept"));
  25994. /* Apply timezone offset to get the main parts in UTC */
  25995. if (neg_year) {
  25996. parts[DUK__PI_YEAR] = -parts[DUK__PI_YEAR];
  25997. }
  25998. if (neg_tzoffset) {
  25999. parts[DUK__PI_HOUR] += parts[DUK__PI_TZHOUR];
  26000. parts[DUK__PI_MINUTE] += parts[DUK__PI_TZMINUTE];
  26001. } else {
  26002. parts[DUK__PI_HOUR] -= parts[DUK__PI_TZHOUR];
  26003. parts[DUK__PI_MINUTE] -= parts[DUK__PI_TZMINUTE];
  26004. }
  26005. parts[DUK__PI_MONTH] -= 1; /* zero-based month */
  26006. parts[DUK__PI_DAY] -= 1; /* zero-based day */
  26007. /* Use double parts, they tolerate unnormalized time.
  26008. *
  26009. * Note: DUK_DATE_IDX_WEEKDAY is initialized with a bogus value (DUK__PI_TZHOUR)
  26010. * on purpose. It won't be actually used by duk_bi_date_get_timeval_from_dparts(),
  26011. * but will make the value initialized just in case, and avoid any
  26012. * potential for Valgrind issues.
  26013. */
  26014. for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) {
  26015. DUK_DDD(DUK_DDDPRINT("part[%ld] = %ld", (long) i, (long) parts[i]));
  26016. dparts[i] = parts[i];
  26017. }
  26018. d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
  26019. duk_push_number(thr, d);
  26020. return 1;
  26021. }
  26022. /*
  26023. * Date/time parsing helper.
  26024. *
  26025. * Parse a datetime string into a time value. We must first try to parse
  26026. * the input according to the standard format in E5.1 Section 15.9.1.15.
  26027. * If that fails, we can try to parse using custom parsing, which can
  26028. * either be platform neutral (custom code) or platform specific (using
  26029. * existing platform API calls).
  26030. *
  26031. * Note in particular that we must parse whatever toString(), toUTCString(),
  26032. * and toISOString() can produce; see E5.1 Section 15.9.4.2.
  26033. *
  26034. * Returns 1 to allow tail calling.
  26035. *
  26036. * There is much room for improvement here with respect to supporting
  26037. * alternative datetime formats. For instance, V8 parses '2012-01-01' as
  26038. * UTC and '2012/01/01' as local time.
  26039. */
  26040. DUK_LOCAL duk_ret_t duk__parse_string(duk_hthread *thr, const char *str) {
  26041. /* XXX: there is a small risk here: because the ISO 8601 parser is
  26042. * very loose, it may end up parsing some datetime values which
  26043. * would be better parsed with a platform specific parser.
  26044. */
  26045. DUK_ASSERT(str != NULL);
  26046. DUK_DDD(DUK_DDDPRINT("parse datetime from string '%s'", (const char *) str));
  26047. if (duk__parse_string_iso8601_subset(thr, str) != 0) {
  26048. return 1;
  26049. }
  26050. #if defined(DUK_USE_DATE_PARSE_STRING)
  26051. /* Contract, either:
  26052. * - Push value on stack and return 1
  26053. * - Don't push anything on stack and return 0
  26054. */
  26055. if (DUK_USE_DATE_PARSE_STRING(thr, str) != 0) {
  26056. return 1;
  26057. }
  26058. #else
  26059. /* No platform-specific parsing, this is not an error. */
  26060. #endif
  26061. duk_push_nan(thr);
  26062. return 1;
  26063. }
  26064. /*
  26065. * Calendar helpers
  26066. *
  26067. * Some helpers are used for getters and can operate on normalized values
  26068. * which can be represented with 32-bit signed integers. Other helpers are
  26069. * needed by setters and operate on un-normalized double values, must watch
  26070. * out for non-finite numbers etc.
  26071. */
  26072. DUK_LOCAL duk_uint8_t duk__days_in_month[12] = {
  26073. (duk_uint8_t) 31, (duk_uint8_t) 28, (duk_uint8_t) 31, (duk_uint8_t) 30,
  26074. (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 31,
  26075. (duk_uint8_t) 30, (duk_uint8_t) 31, (duk_uint8_t) 30, (duk_uint8_t) 31
  26076. };
  26077. /* Maximum iteration count for computing UTC-to-local time offset when
  26078. * creating an ECMAScript time value from local parts.
  26079. */
  26080. #define DUK__LOCAL_TZOFFSET_MAXITER 4
  26081. /* Because 'day since epoch' can be negative and is used to compute weekday
  26082. * using a modulo operation, add this multiple of 7 to avoid negative values
  26083. * when year is below 1970 epoch. ECMAScript time values are restricted to
  26084. * +/- 100 million days from epoch, so this adder fits nicely into 32 bits.
  26085. * Round to a multiple of 7 (= floor(100000000 / 7) * 7) and add margin.
  26086. */
  26087. #define DUK__WEEKDAY_MOD_ADDER (20000000 * 7) /* 0x08583b00 */
  26088. DUK_INTERNAL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year) {
  26089. if ((year % 4) != 0) {
  26090. return 0;
  26091. }
  26092. if ((year % 100) != 0) {
  26093. return 1;
  26094. }
  26095. if ((year % 400) != 0) {
  26096. return 0;
  26097. }
  26098. return 1;
  26099. }
  26100. DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x) {
  26101. return (x >= -DUK_DATE_MSEC_100M_DAYS && x <= DUK_DATE_MSEC_100M_DAYS);
  26102. }
  26103. DUK_INTERNAL duk_bool_t duk_bi_date_timeval_in_leeway_range(duk_double_t x) {
  26104. return (x >= -DUK_DATE_MSEC_100M_DAYS_LEEWAY && x <= DUK_DATE_MSEC_100M_DAYS_LEEWAY);
  26105. }
  26106. DUK_INTERNAL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t x) {
  26107. return (x >= DUK_DATE_MIN_ECMA_YEAR && x <= DUK_DATE_MAX_ECMA_YEAR);
  26108. }
  26109. DUK_LOCAL duk_double_t duk__timeclip(duk_double_t x) {
  26110. if (!DUK_ISFINITE(x)) {
  26111. return DUK_DOUBLE_NAN;
  26112. }
  26113. if (!duk_bi_date_timeval_in_valid_range(x)) {
  26114. return DUK_DOUBLE_NAN;
  26115. }
  26116. x = duk_js_tointeger_number(x);
  26117. /* Here we'd have the option to normalize -0 to +0. */
  26118. return x;
  26119. }
  26120. /* Integer division which floors also negative values correctly. */
  26121. DUK_LOCAL duk_int_t duk__div_floor(duk_int_t a, duk_int_t b) {
  26122. DUK_ASSERT(b > 0);
  26123. if (a >= 0) {
  26124. return a / b;
  26125. } else {
  26126. /* e.g. a = -4, b = 5 --> -4 - 5 + 1 / 5 --> -8 / 5 --> -1
  26127. * a = -5, b = 5 --> -5 - 5 + 1 / 5 --> -9 / 5 --> -1
  26128. * a = -6, b = 5 --> -6 - 5 + 1 / 5 --> -10 / 5 --> -2
  26129. */
  26130. return (a - b + 1) / b;
  26131. }
  26132. }
  26133. /* Compute day number of the first day of a given year. */
  26134. DUK_LOCAL duk_int_t duk__day_from_year(duk_int_t year) {
  26135. /* Note: in integer arithmetic, (x / 4) is same as floor(x / 4) for non-negative
  26136. * values, but is incorrect for negative ones.
  26137. */
  26138. return 365 * (year - 1970)
  26139. + duk__div_floor(year - 1969, 4)
  26140. - duk__div_floor(year - 1901, 100)
  26141. + duk__div_floor(year - 1601, 400);
  26142. }
  26143. /* Given a day number, determine year and day-within-year. */
  26144. DUK_LOCAL duk_int_t duk__year_from_day(duk_int_t day, duk_small_int_t *out_day_within_year) {
  26145. duk_int_t year;
  26146. duk_int_t diff_days;
  26147. /* estimate year upwards (towards positive infinity), then back down;
  26148. * two iterations should be enough
  26149. */
  26150. if (day >= 0) {
  26151. year = 1970 + day / 365;
  26152. } else {
  26153. year = 1970 + day / 366;
  26154. }
  26155. for (;;) {
  26156. diff_days = duk__day_from_year(year) - day;
  26157. DUK_DDD(DUK_DDDPRINT("year=%ld day=%ld, diff_days=%ld", (long) year, (long) day, (long) diff_days));
  26158. if (diff_days <= 0) {
  26159. DUK_ASSERT(-diff_days < 366); /* fits into duk_small_int_t */
  26160. *out_day_within_year = -diff_days;
  26161. DUK_DDD(DUK_DDDPRINT("--> year=%ld, day-within-year=%ld",
  26162. (long) year, (long) *out_day_within_year));
  26163. DUK_ASSERT(*out_day_within_year >= 0);
  26164. DUK_ASSERT(*out_day_within_year < (duk_bi_date_is_leap_year(year) ? 366 : 365));
  26165. return year;
  26166. }
  26167. /* Note: this is very tricky; we must never 'overshoot' the
  26168. * correction downwards.
  26169. */
  26170. year -= 1 + (diff_days - 1) / 366; /* conservative */
  26171. }
  26172. }
  26173. /* Given a (year, month, day-within-month) triple, compute day number.
  26174. * The input triple is un-normalized and may contain non-finite values.
  26175. */
  26176. DUK_LOCAL duk_double_t duk__make_day(duk_double_t year, duk_double_t month, duk_double_t day) {
  26177. duk_int_t day_num;
  26178. duk_bool_t is_leap;
  26179. duk_small_int_t i, n;
  26180. /* Assume that year, month, day are all coerced to whole numbers.
  26181. * They may also be NaN or infinity, in which case this function
  26182. * must return NaN or infinity to ensure time value becomes NaN.
  26183. * If 'day' is NaN, the final return will end up returning a NaN,
  26184. * so it doesn't need to be checked here.
  26185. */
  26186. if (!DUK_ISFINITE(year) || !DUK_ISFINITE(month)) {
  26187. return DUK_DOUBLE_NAN;
  26188. }
  26189. year += DUK_FLOOR(month / 12.0);
  26190. month = DUK_FMOD(month, 12.0);
  26191. if (month < 0.0) {
  26192. /* handle negative values */
  26193. month += 12.0;
  26194. }
  26195. /* The algorithm in E5.1 Section 15.9.1.12 normalizes month, but
  26196. * does not normalize the day-of-month (nor check whether or not
  26197. * it is finite) because it's not necessary for finding the day
  26198. * number which matches the (year,month) pair.
  26199. *
  26200. * We assume that duk__day_from_year() is exact here.
  26201. *
  26202. * Without an explicit infinity / NaN check in the beginning,
  26203. * day_num would be a bogus integer here.
  26204. *
  26205. * It's possible for 'year' to be out of integer range here.
  26206. * If so, we need to return NaN without integer overflow.
  26207. * This fixes test-bug-setyear-overflow.js.
  26208. */
  26209. if (!duk_bi_date_year_in_valid_range(year)) {
  26210. DUK_DD(DUK_DDPRINT("year not in ecmascript valid range, avoid integer overflow: %lf", (double) year));
  26211. return DUK_DOUBLE_NAN;
  26212. }
  26213. day_num = duk__day_from_year((duk_int_t) year);
  26214. is_leap = duk_bi_date_is_leap_year((duk_int_t) year);
  26215. n = (duk_small_int_t) month;
  26216. for (i = 0; i < n; i++) {
  26217. day_num += duk__days_in_month[i];
  26218. if (i == 1 && is_leap) {
  26219. day_num++;
  26220. }
  26221. }
  26222. /* If 'day' is NaN, returns NaN. */
  26223. return (duk_double_t) day_num + day;
  26224. }
  26225. /* Split time value into parts. The time value may contain fractions (it may
  26226. * come from duk_time_to_components() API call) which are truncated. Possible
  26227. * local time adjustment has already been applied when reading the time value.
  26228. */
  26229. DUK_INTERNAL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags) {
  26230. duk_double_t d1, d2;
  26231. duk_int_t t1, t2;
  26232. duk_int_t day_since_epoch;
  26233. duk_int_t year; /* does not fit into 16 bits */
  26234. duk_small_int_t day_in_year;
  26235. duk_small_int_t month;
  26236. duk_small_int_t day;
  26237. duk_small_int_t dim;
  26238. duk_int_t jan1_since_epoch;
  26239. duk_small_int_t jan1_weekday;
  26240. duk_int_t equiv_year;
  26241. duk_small_uint_t i;
  26242. duk_bool_t is_leap;
  26243. duk_small_int_t arridx;
  26244. DUK_ASSERT(DUK_ISFINITE(d)); /* caller checks */
  26245. d = DUK_FLOOR(d); /* remove fractions if present */
  26246. DUK_ASSERT(DUK_FLOOR(d) == d);
  26247. /* The timevalue must be in valid ECMAScript range, but since a local
  26248. * time offset can be applied, we need to allow a +/- 24h leeway to
  26249. * the value. In other words, although the UTC time is within the
  26250. * ECMAScript range, the local part values can be just outside of it.
  26251. */
  26252. DUK_UNREF(duk_bi_date_timeval_in_leeway_range);
  26253. DUK_ASSERT(duk_bi_date_timeval_in_leeway_range(d));
  26254. /* These computations are guaranteed to be exact for the valid
  26255. * E5 time value range, assuming milliseconds without fractions.
  26256. */
  26257. d1 = (duk_double_t) DUK_FMOD(d, (double) DUK_DATE_MSEC_DAY);
  26258. if (d1 < 0.0) {
  26259. /* deal with negative values */
  26260. d1 += (duk_double_t) DUK_DATE_MSEC_DAY;
  26261. }
  26262. d2 = DUK_FLOOR((double) (d / (duk_double_t) DUK_DATE_MSEC_DAY));
  26263. DUK_ASSERT(d2 * ((duk_double_t) DUK_DATE_MSEC_DAY) + d1 == d);
  26264. /* now expected to fit into a 32-bit integer */
  26265. t1 = (duk_int_t) d1;
  26266. t2 = (duk_int_t) d2;
  26267. day_since_epoch = t2;
  26268. DUK_ASSERT((duk_double_t) t1 == d1);
  26269. DUK_ASSERT((duk_double_t) t2 == d2);
  26270. /* t1 = milliseconds within day (fits 32 bit)
  26271. * t2 = day number from epoch (fits 32 bit, may be negative)
  26272. */
  26273. parts[DUK_DATE_IDX_MILLISECOND] = t1 % 1000; t1 /= 1000;
  26274. parts[DUK_DATE_IDX_SECOND] = t1 % 60; t1 /= 60;
  26275. parts[DUK_DATE_IDX_MINUTE] = t1 % 60; t1 /= 60;
  26276. parts[DUK_DATE_IDX_HOUR] = t1;
  26277. DUK_ASSERT(parts[DUK_DATE_IDX_MILLISECOND] >= 0 && parts[DUK_DATE_IDX_MILLISECOND] <= 999);
  26278. DUK_ASSERT(parts[DUK_DATE_IDX_SECOND] >= 0 && parts[DUK_DATE_IDX_SECOND] <= 59);
  26279. DUK_ASSERT(parts[DUK_DATE_IDX_MINUTE] >= 0 && parts[DUK_DATE_IDX_MINUTE] <= 59);
  26280. DUK_ASSERT(parts[DUK_DATE_IDX_HOUR] >= 0 && parts[DUK_DATE_IDX_HOUR] <= 23);
  26281. DUK_DDD(DUK_DDDPRINT("d=%lf, d1=%lf, d2=%lf, t1=%ld, t2=%ld, parts: hour=%ld min=%ld sec=%ld msec=%ld",
  26282. (double) d, (double) d1, (double) d2, (long) t1, (long) t2,
  26283. (long) parts[DUK_DATE_IDX_HOUR],
  26284. (long) parts[DUK_DATE_IDX_MINUTE],
  26285. (long) parts[DUK_DATE_IDX_SECOND],
  26286. (long) parts[DUK_DATE_IDX_MILLISECOND]));
  26287. /* This assert depends on the input parts representing time inside
  26288. * the ECMAScript range.
  26289. */
  26290. DUK_ASSERT(t2 + DUK__WEEKDAY_MOD_ADDER >= 0);
  26291. parts[DUK_DATE_IDX_WEEKDAY] = (t2 + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
  26292. DUK_ASSERT(parts[DUK_DATE_IDX_WEEKDAY] >= 0 && parts[DUK_DATE_IDX_WEEKDAY] <= 6);
  26293. year = duk__year_from_day(t2, &day_in_year);
  26294. day = day_in_year;
  26295. is_leap = duk_bi_date_is_leap_year(year);
  26296. for (month = 0; month < 12; month++) {
  26297. dim = duk__days_in_month[month];
  26298. if (month == 1 && is_leap) {
  26299. dim++;
  26300. }
  26301. DUK_DDD(DUK_DDDPRINT("month=%ld, dim=%ld, day=%ld",
  26302. (long) month, (long) dim, (long) day));
  26303. if (day < dim) {
  26304. break;
  26305. }
  26306. day -= dim;
  26307. }
  26308. DUK_DDD(DUK_DDDPRINT("final month=%ld", (long) month));
  26309. DUK_ASSERT(month >= 0 && month <= 11);
  26310. DUK_ASSERT(day >= 0 && day <= 31);
  26311. /* Equivalent year mapping, used to avoid DST trouble when platform
  26312. * may fail to provide reasonable DST answers for dates outside the
  26313. * ordinary range (e.g. 1970-2038). An equivalent year has the same
  26314. * leap-year-ness as the original year and begins on the same weekday
  26315. * (Jan 1).
  26316. *
  26317. * The year 2038 is avoided because there seem to be problems with it
  26318. * on some platforms. The year 1970 is also avoided as there were
  26319. * practical problems with it; an equivalent year is used for it too,
  26320. * which breaks some DST computations for 1970 right now, see e.g.
  26321. * test-bi-date-tzoffset-brute-fi.js.
  26322. */
  26323. if ((flags & DUK_DATE_FLAG_EQUIVYEAR) && (year < 1971 || year > 2037)) {
  26324. DUK_ASSERT(is_leap == 0 || is_leap == 1);
  26325. jan1_since_epoch = day_since_epoch - day_in_year; /* day number for Jan 1 since epoch */
  26326. DUK_ASSERT(jan1_since_epoch + DUK__WEEKDAY_MOD_ADDER >= 0);
  26327. jan1_weekday = (jan1_since_epoch + 4 + DUK__WEEKDAY_MOD_ADDER) % 7; /* E5.1 Section 15.9.1.6 */
  26328. DUK_ASSERT(jan1_weekday >= 0 && jan1_weekday <= 6);
  26329. arridx = jan1_weekday;
  26330. if (is_leap) {
  26331. arridx += 7;
  26332. }
  26333. DUK_ASSERT(arridx >= 0 && arridx < (duk_small_int_t) (sizeof(duk__date_equivyear) / sizeof(duk_uint8_t)));
  26334. equiv_year = (duk_int_t) duk__date_equivyear[arridx] + 1970;
  26335. year = equiv_year;
  26336. DUK_DDD(DUK_DDDPRINT("equiv year mapping, year=%ld, day_in_year=%ld, day_since_epoch=%ld, "
  26337. "jan1_since_epoch=%ld, jan1_weekday=%ld -> equiv year %ld",
  26338. (long) year, (long) day_in_year, (long) day_since_epoch,
  26339. (long) jan1_since_epoch, (long) jan1_weekday, (long) equiv_year));
  26340. }
  26341. parts[DUK_DATE_IDX_YEAR] = year;
  26342. parts[DUK_DATE_IDX_MONTH] = month;
  26343. parts[DUK_DATE_IDX_DAY] = day;
  26344. if (flags & DUK_DATE_FLAG_ONEBASED) {
  26345. parts[DUK_DATE_IDX_MONTH]++; /* zero-based -> one-based */
  26346. parts[DUK_DATE_IDX_DAY]++; /* -""- */
  26347. }
  26348. if (dparts != NULL) {
  26349. for (i = 0; i < DUK_DATE_IDX_NUM_PARTS; i++) {
  26350. dparts[i] = (duk_double_t) parts[i];
  26351. }
  26352. }
  26353. }
  26354. /* Compute time value from (double) parts. The parts can be either UTC
  26355. * or local time; if local, they need to be (conceptually) converted into
  26356. * UTC time. The parts may represent valid or invalid time, and may be
  26357. * wildly out of range (but may cancel each other and still come out in
  26358. * the valid Date range).
  26359. */
  26360. DUK_INTERNAL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags) {
  26361. #if defined(DUK_USE_PARANOID_DATE_COMPUTATION)
  26362. /* See comments below on MakeTime why these are volatile. */
  26363. volatile duk_double_t tmp_time;
  26364. volatile duk_double_t tmp_day;
  26365. volatile duk_double_t d;
  26366. #else
  26367. duk_double_t tmp_time;
  26368. duk_double_t tmp_day;
  26369. duk_double_t d;
  26370. #endif
  26371. duk_small_uint_t i;
  26372. duk_int_t tzoff, tzoffprev1, tzoffprev2;
  26373. /* Expects 'this' at top of stack on entry. */
  26374. /* Coerce all finite parts with ToInteger(). ToInteger() must not
  26375. * be called for NaN/Infinity because it will convert e.g. NaN to
  26376. * zero. If ToInteger() has already been called, this has no side
  26377. * effects and is idempotent.
  26378. *
  26379. * Don't read dparts[DUK_DATE_IDX_WEEKDAY]; it will cause Valgrind
  26380. * issues if the value is uninitialized.
  26381. */
  26382. for (i = 0; i <= DUK_DATE_IDX_MILLISECOND; i++) {
  26383. /* SCANBUILD: scan-build complains here about assigned value
  26384. * being garbage or undefined. This is correct but operating
  26385. * on undefined values has no ill effect and is ignored by the
  26386. * caller in the case where this happens.
  26387. */
  26388. d = dparts[i];
  26389. if (DUK_ISFINITE(d)) {
  26390. dparts[i] = duk_js_tointeger_number(d);
  26391. }
  26392. }
  26393. /* Use explicit steps in computation to try to ensure that
  26394. * computation happens with intermediate results coerced to
  26395. * double values (instead of using something more accurate).
  26396. * E.g. E5.1 Section 15.9.1.11 requires use of IEEE 754
  26397. * rules (= ECMAScript '+' and '*' operators).
  26398. *
  26399. * Without 'volatile' even this approach fails on some platform
  26400. * and compiler combinations. For instance, gcc 4.8.1 on Ubuntu
  26401. * 64-bit, with -m32 and without -std=c99, test-bi-date-canceling.js
  26402. * would fail because of some optimizations when computing tmp_time
  26403. * (MakeTime below). Adding 'volatile' to tmp_time solved this
  26404. * particular problem (annoyingly, also adding debug prints or
  26405. * running the executable under valgrind hides it).
  26406. */
  26407. /* MakeTime */
  26408. tmp_time = 0.0;
  26409. tmp_time += dparts[DUK_DATE_IDX_HOUR] * ((duk_double_t) DUK_DATE_MSEC_HOUR);
  26410. tmp_time += dparts[DUK_DATE_IDX_MINUTE] * ((duk_double_t) DUK_DATE_MSEC_MINUTE);
  26411. tmp_time += dparts[DUK_DATE_IDX_SECOND] * ((duk_double_t) DUK_DATE_MSEC_SECOND);
  26412. tmp_time += dparts[DUK_DATE_IDX_MILLISECOND];
  26413. /* MakeDay */
  26414. tmp_day = duk__make_day(dparts[DUK_DATE_IDX_YEAR], dparts[DUK_DATE_IDX_MONTH], dparts[DUK_DATE_IDX_DAY]);
  26415. /* MakeDate */
  26416. d = tmp_day * ((duk_double_t) DUK_DATE_MSEC_DAY) + tmp_time;
  26417. DUK_DDD(DUK_DDDPRINT("time=%lf day=%lf --> timeval=%lf",
  26418. (double) tmp_time, (double) tmp_day, (double) d));
  26419. /* Optional UTC conversion. */
  26420. if (flags & DUK_DATE_FLAG_LOCALTIME) {
  26421. /* DUK_USE_DATE_GET_LOCAL_TZOFFSET() needs to be called with a
  26422. * time value computed from UTC parts. At this point we only
  26423. * have 'd' which is a time value computed from local parts, so
  26424. * it is off by the UTC-to-local time offset which we don't know
  26425. * yet. The current solution for computing the UTC-to-local
  26426. * time offset is to iterate a few times and detect a fixed
  26427. * point or a two-cycle loop (or a sanity iteration limit),
  26428. * see test-bi-date-local-parts.js and test-bi-date-tzoffset-basic-fi.js.
  26429. *
  26430. * E5.1 Section 15.9.1.9:
  26431. * UTC(t) = t - LocalTZA - DaylightSavingTA(t - LocalTZA)
  26432. *
  26433. * For NaN/inf, DUK_USE_DATE_GET_LOCAL_TZOFFSET() returns 0.
  26434. */
  26435. #if 0
  26436. /* Old solution: don't iterate, incorrect */
  26437. tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d);
  26438. DUK_DDD(DUK_DDDPRINT("tzoffset w/o iteration, tzoff=%ld", (long) tzoff));
  26439. d -= tzoff * 1000L;
  26440. DUK_UNREF(tzoffprev1);
  26441. DUK_UNREF(tzoffprev2);
  26442. #endif
  26443. /* Iteration solution */
  26444. tzoff = 0;
  26445. tzoffprev1 = 999999999L; /* invalid value which never matches */
  26446. for (i = 0; i < DUK__LOCAL_TZOFFSET_MAXITER; i++) {
  26447. tzoffprev2 = tzoffprev1;
  26448. tzoffprev1 = tzoff;
  26449. tzoff = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d - tzoff * 1000L);
  26450. DUK_DDD(DUK_DDDPRINT("tzoffset iteration, i=%d, tzoff=%ld, tzoffprev1=%ld tzoffprev2=%ld",
  26451. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  26452. if (tzoff == tzoffprev1) {
  26453. DUK_DDD(DUK_DDDPRINT("tzoffset iteration finished, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
  26454. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  26455. break;
  26456. } else if (tzoff == tzoffprev2) {
  26457. /* Two value cycle, see e.g. test-bi-date-tzoffset-basic-fi.js.
  26458. * In these cases, favor a higher tzoffset to get a consistent
  26459. * result which is independent of iteration count. Not sure if
  26460. * this is a generically correct solution.
  26461. */
  26462. DUK_DDD(DUK_DDDPRINT("tzoffset iteration two-value cycle, i=%d, tzoff=%ld, tzoffprev1=%ld, tzoffprev2=%ld",
  26463. (int) i, (long) tzoff, (long) tzoffprev1, (long) tzoffprev2));
  26464. if (tzoffprev1 > tzoff) {
  26465. tzoff = tzoffprev1;
  26466. }
  26467. break;
  26468. }
  26469. }
  26470. DUK_DDD(DUK_DDDPRINT("tzoffset iteration, tzoff=%ld", (long) tzoff));
  26471. d -= tzoff * 1000L;
  26472. }
  26473. /* TimeClip(), which also handles Infinity -> NaN conversion */
  26474. d = duk__timeclip(d);
  26475. return d;
  26476. }
  26477. /*
  26478. * API oriented helpers
  26479. */
  26480. /* Push 'this' binding, check that it is a Date object; then push the
  26481. * internal time value. At the end, stack is: [ ... this timeval ].
  26482. * Returns the time value. Local time adjustment is done if requested.
  26483. */
  26484. DUK_LOCAL duk_double_t duk__push_this_get_timeval_tzoffset(duk_hthread *thr, duk_small_uint_t flags, duk_int_t *out_tzoffset) {
  26485. duk_hobject *h;
  26486. duk_double_t d;
  26487. duk_int_t tzoffset = 0;
  26488. duk_push_this(thr);
  26489. h = duk_get_hobject(thr, -1); /* XXX: getter with class check, useful in built-ins */
  26490. if (h == NULL || DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_DATE) {
  26491. DUK_ERROR_TYPE(thr, "expected Date");
  26492. DUK_WO_NORETURN(return 0.0;);
  26493. }
  26494. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
  26495. d = duk_to_number_m1(thr);
  26496. duk_pop(thr);
  26497. if (DUK_ISNAN(d)) {
  26498. if (flags & DUK_DATE_FLAG_NAN_TO_ZERO) {
  26499. d = 0.0;
  26500. }
  26501. if (flags & DUK_DATE_FLAG_NAN_TO_RANGE_ERROR) {
  26502. DUK_ERROR_RANGE(thr, "Invalid Date");
  26503. DUK_WO_NORETURN(return 0.0;);
  26504. }
  26505. }
  26506. /* if no NaN handling flag, may still be NaN here, but not Inf */
  26507. DUK_ASSERT(!DUK_ISINF(d));
  26508. if (flags & DUK_DATE_FLAG_LOCALTIME) {
  26509. /* Note: DST adjustment is determined using UTC time.
  26510. * If 'd' is NaN, tzoffset will be 0.
  26511. */
  26512. tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d); /* seconds */
  26513. d += tzoffset * 1000L;
  26514. }
  26515. if (out_tzoffset) {
  26516. *out_tzoffset = tzoffset;
  26517. }
  26518. /* [ ... this ] */
  26519. return d;
  26520. }
  26521. DUK_LOCAL duk_double_t duk__push_this_get_timeval(duk_hthread *thr, duk_small_uint_t flags) {
  26522. return duk__push_this_get_timeval_tzoffset(thr, flags, NULL);
  26523. }
  26524. /* Set timeval to 'this' from dparts, push the new time value onto the
  26525. * value stack and return 1 (caller can then tail call us). Expects
  26526. * the value stack to contain 'this' on the stack top.
  26527. */
  26528. DUK_LOCAL duk_ret_t duk__set_this_timeval_from_dparts(duk_hthread *thr, duk_double_t *dparts, duk_small_uint_t flags) {
  26529. duk_double_t d;
  26530. /* [ ... this ] */
  26531. d = duk_bi_date_get_timeval_from_dparts(dparts, flags);
  26532. duk_push_number(thr, d); /* -> [ ... this timeval_new ] */
  26533. duk_dup_top(thr); /* -> [ ... this timeval_new timeval_new ] */
  26534. duk_put_prop_stridx_short(thr, -3, DUK_STRIDX_INT_VALUE);
  26535. /* stack top: new time value, return 1 to allow tail calls */
  26536. return 1;
  26537. }
  26538. /* 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. */
  26539. DUK_LOCAL void duk__format_parts_iso8601(duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags, duk_uint8_t *out_buf) {
  26540. char yearstr[8]; /* "-123456\0" */
  26541. char tzstr[8]; /* "+11:22\0" */
  26542. char sep = (flags & DUK_DATE_FLAG_SEP_T) ? DUK_ASC_UC_T : DUK_ASC_SPACE;
  26543. DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12);
  26544. DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31);
  26545. DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= -999999 && parts[DUK_DATE_IDX_YEAR] <= 999999);
  26546. /* Note: %06d for positive value, %07d for negative value to include
  26547. * sign and 6 digits.
  26548. */
  26549. DUK_SNPRINTF(yearstr,
  26550. sizeof(yearstr),
  26551. (parts[DUK_DATE_IDX_YEAR] >= 0 && parts[DUK_DATE_IDX_YEAR] <= 9999) ? "%04ld" :
  26552. ((parts[DUK_DATE_IDX_YEAR] >= 0) ? "+%06ld" : "%07ld"),
  26553. (long) parts[DUK_DATE_IDX_YEAR]);
  26554. yearstr[sizeof(yearstr) - 1] = (char) 0;
  26555. if (flags & DUK_DATE_FLAG_LOCALTIME) {
  26556. /* tzoffset seconds are dropped; 16 bits suffice for
  26557. * time offset in minutes
  26558. */
  26559. const char *fmt;
  26560. duk_small_int_t tmp, arg_hours, arg_minutes;
  26561. if (tzoffset >= 0) {
  26562. tmp = tzoffset;
  26563. fmt = "+%02d:%02d";
  26564. } else {
  26565. tmp = -tzoffset;
  26566. fmt = "-%02d:%02d";
  26567. }
  26568. tmp = tmp / 60;
  26569. arg_hours = tmp / 60;
  26570. arg_minutes = tmp % 60;
  26571. DUK_ASSERT(arg_hours <= 24); /* Even less is actually guaranteed for a valid tzoffset. */
  26572. arg_hours = arg_hours & 0x3f; /* For [0,24] this is a no-op, but fixes GCC 7 warning, see https://github.com/svaarala/duktape/issues/1602. */
  26573. DUK_SNPRINTF(tzstr, sizeof(tzstr), fmt, (int) arg_hours, (int) arg_minutes);
  26574. tzstr[sizeof(tzstr) - 1] = (char) 0;
  26575. } else {
  26576. tzstr[0] = DUK_ASC_UC_Z;
  26577. tzstr[1] = (char) 0;
  26578. }
  26579. /* Unlike year, the other parts fit into 16 bits so %d format
  26580. * is portable.
  26581. */
  26582. if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) {
  26583. DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d%c%02d:%02d:%02d.%03d%s",
  26584. (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY], (int) sep,
  26585. (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE],
  26586. (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND], (const char *) tzstr);
  26587. } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) {
  26588. DUK_SPRINTF((char *) out_buf, "%s-%02d-%02d",
  26589. (const char *) yearstr, (int) parts[DUK_DATE_IDX_MONTH], (int) parts[DUK_DATE_IDX_DAY]);
  26590. } else {
  26591. DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME);
  26592. DUK_SPRINTF((char *) out_buf, "%02d:%02d:%02d.%03d%s",
  26593. (int) parts[DUK_DATE_IDX_HOUR], (int) parts[DUK_DATE_IDX_MINUTE],
  26594. (int) parts[DUK_DATE_IDX_SECOND], (int) parts[DUK_DATE_IDX_MILLISECOND],
  26595. (const char *) tzstr);
  26596. }
  26597. }
  26598. /* Helper for string conversion calls: check 'this' binding, get the
  26599. * internal time value, and format date and/or time in a few formats.
  26600. * Return value allows tail calls.
  26601. */
  26602. DUK_LOCAL duk_ret_t duk__to_string_helper(duk_hthread *thr, duk_small_uint_t flags) {
  26603. duk_double_t d;
  26604. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  26605. duk_int_t tzoffset; /* seconds, doesn't fit into 16 bits */
  26606. duk_bool_t rc;
  26607. duk_uint8_t buf[DUK_BI_DATE_ISO8601_BUFSIZE];
  26608. DUK_UNREF(rc); /* unreferenced with some options */
  26609. d = duk__push_this_get_timeval_tzoffset(thr, flags, &tzoffset);
  26610. if (DUK_ISNAN(d)) {
  26611. duk_push_hstring_stridx(thr, DUK_STRIDX_INVALID_DATE);
  26612. return 1;
  26613. }
  26614. DUK_ASSERT(DUK_ISFINITE(d));
  26615. /* formatters always get one-based month/day-of-month */
  26616. duk_bi_date_timeval_to_parts(d, parts, NULL, DUK_DATE_FLAG_ONEBASED);
  26617. DUK_ASSERT(parts[DUK_DATE_IDX_MONTH] >= 1 && parts[DUK_DATE_IDX_MONTH] <= 12);
  26618. DUK_ASSERT(parts[DUK_DATE_IDX_DAY] >= 1 && parts[DUK_DATE_IDX_DAY] <= 31);
  26619. if (flags & DUK_DATE_FLAG_TOSTRING_LOCALE) {
  26620. /* try locale specific formatter; if it refuses to format the
  26621. * string, fall back to an ISO 8601 formatted value in local
  26622. * time.
  26623. */
  26624. #if defined(DUK_USE_DATE_FORMAT_STRING)
  26625. /* Contract, either:
  26626. * - Push string to value stack and return 1
  26627. * - Don't push anything and return 0
  26628. */
  26629. rc = DUK_USE_DATE_FORMAT_STRING(thr, parts, tzoffset, flags);
  26630. if (rc != 0) {
  26631. return 1;
  26632. }
  26633. #else
  26634. /* No locale specific formatter; this is OK, we fall back
  26635. * to ISO 8601.
  26636. */
  26637. #endif
  26638. }
  26639. /* Different calling convention than above used because the helper
  26640. * is shared.
  26641. */
  26642. duk__format_parts_iso8601(parts, tzoffset, flags, buf);
  26643. duk_push_string(thr, (const char *) buf);
  26644. return 1;
  26645. }
  26646. /* Helper for component getter calls: check 'this' binding, get the
  26647. * internal time value, split it into parts (either as UTC time or
  26648. * local time), push a specified component as a return value to the
  26649. * value stack and return 1 (caller can then tail call us).
  26650. */
  26651. DUK_LOCAL duk_ret_t duk__get_part_helper(duk_hthread *thr, duk_small_uint_t flags_and_idx) {
  26652. duk_double_t d;
  26653. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  26654. duk_small_uint_t idx_part = (duk_small_uint_t) (flags_and_idx >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */
  26655. DUK_ASSERT_DISABLE(idx_part >= 0); /* unsigned */
  26656. DUK_ASSERT(idx_part < DUK_DATE_IDX_NUM_PARTS);
  26657. d = duk__push_this_get_timeval(thr, flags_and_idx);
  26658. if (DUK_ISNAN(d)) {
  26659. duk_push_nan(thr);
  26660. return 1;
  26661. }
  26662. DUK_ASSERT(DUK_ISFINITE(d));
  26663. duk_bi_date_timeval_to_parts(d, parts, NULL, flags_and_idx); /* no need to mask idx portion */
  26664. /* Setter APIs detect special year numbers (0...99) and apply a +1900
  26665. * only in certain cases. The legacy getYear() getter applies -1900
  26666. * unconditionally.
  26667. */
  26668. duk_push_int(thr, (flags_and_idx & DUK_DATE_FLAG_SUB1900) ? parts[idx_part] - 1900 : parts[idx_part]);
  26669. return 1;
  26670. }
  26671. /* Helper for component setter calls: check 'this' binding, get the
  26672. * internal time value, split it into parts (either as UTC time or
  26673. * local time), modify one or more components as specified, recompute
  26674. * the time value, set it as the internal value. Finally, push the
  26675. * new time value as a return value to the value stack and return 1
  26676. * (caller can then tail call us).
  26677. */
  26678. DUK_LOCAL duk_ret_t duk__set_part_helper(duk_hthread *thr, duk_small_uint_t flags_and_maxnargs) {
  26679. duk_double_t d;
  26680. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  26681. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  26682. duk_idx_t nargs;
  26683. duk_small_uint_t maxnargs = (duk_small_uint_t) (flags_and_maxnargs >> DUK_DATE_FLAG_VALUE_SHIFT); /* unpack args */
  26684. duk_small_uint_t idx_first, idx;
  26685. duk_small_uint_t i;
  26686. nargs = duk_get_top(thr);
  26687. d = duk__push_this_get_timeval(thr, flags_and_maxnargs);
  26688. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  26689. if (DUK_ISFINITE(d)) {
  26690. duk_bi_date_timeval_to_parts(d, parts, dparts, flags_and_maxnargs);
  26691. } else {
  26692. /* NaN timevalue: we need to coerce the arguments, but
  26693. * the resulting internal timestamp needs to remain NaN.
  26694. * This works but is not pretty: parts and dparts will
  26695. * be partially uninitialized, but we only write to them.
  26696. */
  26697. }
  26698. /*
  26699. * Determining which datetime components to overwrite based on
  26700. * stack arguments is a bit complicated, but important to factor
  26701. * out from setters themselves for compactness.
  26702. *
  26703. * If DUK_DATE_FLAG_TIMESETTER, maxnargs indicates setter type:
  26704. *
  26705. * 1 -> millisecond
  26706. * 2 -> second, [millisecond]
  26707. * 3 -> minute, [second], [millisecond]
  26708. * 4 -> hour, [minute], [second], [millisecond]
  26709. *
  26710. * Else:
  26711. *
  26712. * 1 -> date
  26713. * 2 -> month, [date]
  26714. * 3 -> year, [month], [date]
  26715. *
  26716. * By comparing nargs and maxnargs (and flags) we know which
  26717. * components to override. We rely on part index ordering.
  26718. */
  26719. if (flags_and_maxnargs & DUK_DATE_FLAG_TIMESETTER) {
  26720. DUK_ASSERT(maxnargs >= 1 && maxnargs <= 4);
  26721. idx_first = DUK_DATE_IDX_MILLISECOND - (maxnargs - 1);
  26722. } else {
  26723. DUK_ASSERT(maxnargs >= 1 && maxnargs <= 3);
  26724. idx_first = DUK_DATE_IDX_DAY - (maxnargs - 1);
  26725. }
  26726. DUK_ASSERT_DISABLE(idx_first >= 0); /* unsigned */
  26727. DUK_ASSERT(idx_first < DUK_DATE_IDX_NUM_PARTS);
  26728. for (i = 0; i < maxnargs; i++) {
  26729. if ((duk_idx_t) i >= nargs) {
  26730. /* no argument given -> leave components untouched */
  26731. break;
  26732. }
  26733. idx = idx_first + i;
  26734. DUK_ASSERT_DISABLE(idx >= 0); /* unsigned */
  26735. DUK_ASSERT(idx < DUK_DATE_IDX_NUM_PARTS);
  26736. if (idx == DUK_DATE_IDX_YEAR && (flags_and_maxnargs & DUK_DATE_FLAG_YEAR_FIXUP)) {
  26737. duk__twodigit_year_fixup(thr, (duk_idx_t) i);
  26738. }
  26739. dparts[idx] = duk_to_number(thr, (duk_idx_t) i);
  26740. if (idx == DUK_DATE_IDX_DAY) {
  26741. /* Day-of-month is one-based in the API, but zero-based
  26742. * internally, so fix here. Note that month is zero-based
  26743. * both in the API and internally.
  26744. */
  26745. /* SCANBUILD: complains about use of uninitialized values.
  26746. * The complaint is correct, but operating in undefined
  26747. * values here is intentional in some cases and the caller
  26748. * ignores the results.
  26749. */
  26750. dparts[idx] -= 1.0;
  26751. }
  26752. }
  26753. /* Leaves new timevalue on stack top and returns 1, which is correct
  26754. * for part setters.
  26755. */
  26756. if (DUK_ISFINITE(d)) {
  26757. return duk__set_this_timeval_from_dparts(thr, dparts, flags_and_maxnargs);
  26758. } else {
  26759. /* Internal timevalue is already NaN, so don't touch it. */
  26760. duk_push_nan(thr);
  26761. return 1;
  26762. }
  26763. }
  26764. /* Apply ToNumber() to specified index; if ToInteger(val) in [0,99], add
  26765. * 1900 and replace value at idx_val.
  26766. */
  26767. DUK_LOCAL void duk__twodigit_year_fixup(duk_hthread *thr, duk_idx_t idx_val) {
  26768. duk_double_t d;
  26769. /* XXX: idx_val would fit into 16 bits, but using duk_small_uint_t
  26770. * might not generate better code due to casting.
  26771. */
  26772. /* E5 Sections 15.9.3.1, B.2.4, B.2.5 */
  26773. duk_to_number(thr, idx_val);
  26774. if (duk_is_nan(thr, idx_val)) {
  26775. return;
  26776. }
  26777. duk_dup(thr, idx_val);
  26778. duk_to_int(thr, -1);
  26779. d = duk_get_number(thr, -1); /* get as double to handle huge numbers correctly */
  26780. if (d >= 0.0 && d <= 99.0) {
  26781. d += 1900.0;
  26782. duk_push_number(thr, d);
  26783. duk_replace(thr, idx_val);
  26784. }
  26785. duk_pop(thr);
  26786. }
  26787. /* Set datetime parts from stack arguments, defaulting any missing values.
  26788. * Day-of-week is not set; it is not required when setting the time value.
  26789. */
  26790. DUK_LOCAL void duk__set_parts_from_args(duk_hthread *thr, duk_double_t *dparts, duk_idx_t nargs) {
  26791. duk_double_t d;
  26792. duk_small_uint_t i;
  26793. duk_small_uint_t idx;
  26794. /* Causes a ToNumber() coercion, but doesn't break coercion order since
  26795. * year is coerced first anyway.
  26796. */
  26797. duk__twodigit_year_fixup(thr, 0);
  26798. /* There are at most 7 args, but we use 8 here so that also
  26799. * DUK_DATE_IDX_WEEKDAY gets initialized (to zero) to avoid the potential
  26800. * for any Valgrind gripes later.
  26801. */
  26802. for (i = 0; i < 8; i++) {
  26803. /* Note: rely on index ordering */
  26804. idx = DUK_DATE_IDX_YEAR + i;
  26805. if ((duk_idx_t) i < nargs) {
  26806. d = duk_to_number(thr, (duk_idx_t) i);
  26807. if (idx == DUK_DATE_IDX_DAY) {
  26808. /* Convert day from one-based to zero-based (internal). This may
  26809. * cause the day part to be negative, which is OK.
  26810. */
  26811. d -= 1.0;
  26812. }
  26813. } else {
  26814. /* All components default to 0 except day-of-month which defaults
  26815. * to 1. However, because our internal day-of-month is zero-based,
  26816. * it also defaults to zero here.
  26817. */
  26818. d = 0.0;
  26819. }
  26820. dparts[idx] = d;
  26821. }
  26822. DUK_DDD(DUK_DDDPRINT("parts from args -> %lf %lf %lf %lf %lf %lf %lf %lf",
  26823. (double) dparts[0], (double) dparts[1],
  26824. (double) dparts[2], (double) dparts[3],
  26825. (double) dparts[4], (double) dparts[5],
  26826. (double) dparts[6], (double) dparts[7]));
  26827. }
  26828. /*
  26829. * Indirect magic value lookup for Date methods.
  26830. *
  26831. * Date methods don't put their control flags into the function magic value
  26832. * because they wouldn't fit into a LIGHTFUNC's magic field. Instead, the
  26833. * magic value is set to an index pointing to the array of control flags
  26834. * below.
  26835. *
  26836. * This must be kept in strict sync with genbuiltins.py!
  26837. */
  26838. static duk_uint16_t duk__date_magics[] = {
  26839. /* 0: toString */
  26840. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME,
  26841. /* 1: toDateString */
  26842. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_LOCALTIME,
  26843. /* 2: toTimeString */
  26844. DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_LOCALTIME,
  26845. /* 3: toLocaleString */
  26846. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME,
  26847. /* 4: toLocaleDateString */
  26848. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME,
  26849. /* 5: toLocaleTimeString */
  26850. DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_TOSTRING_LOCALE + DUK_DATE_FLAG_LOCALTIME,
  26851. /* 6: toUTCString */
  26852. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME,
  26853. /* 7: toISOString */
  26854. DUK_DATE_FLAG_TOSTRING_DATE + DUK_DATE_FLAG_TOSTRING_TIME + DUK_DATE_FLAG_NAN_TO_RANGE_ERROR + DUK_DATE_FLAG_SEP_T,
  26855. /* 8: getFullYear */
  26856. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT),
  26857. /* 9: getUTCFullYear */
  26858. 0 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT),
  26859. /* 10: getMonth */
  26860. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT),
  26861. /* 11: getUTCMonth */
  26862. 0 + (DUK_DATE_IDX_MONTH << DUK_DATE_FLAG_VALUE_SHIFT),
  26863. /* 12: getDate */
  26864. DUK_DATE_FLAG_ONEBASED + DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT),
  26865. /* 13: getUTCDate */
  26866. DUK_DATE_FLAG_ONEBASED + (DUK_DATE_IDX_DAY << DUK_DATE_FLAG_VALUE_SHIFT),
  26867. /* 14: getDay */
  26868. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT),
  26869. /* 15: getUTCDay */
  26870. 0 + (DUK_DATE_IDX_WEEKDAY << DUK_DATE_FLAG_VALUE_SHIFT),
  26871. /* 16: getHours */
  26872. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT),
  26873. /* 17: getUTCHours */
  26874. 0 + (DUK_DATE_IDX_HOUR << DUK_DATE_FLAG_VALUE_SHIFT),
  26875. /* 18: getMinutes */
  26876. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT),
  26877. /* 19: getUTCMinutes */
  26878. 0 + (DUK_DATE_IDX_MINUTE << DUK_DATE_FLAG_VALUE_SHIFT),
  26879. /* 20: getSeconds */
  26880. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  26881. /* 21: getUTCSeconds */
  26882. 0 + (DUK_DATE_IDX_SECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  26883. /* 22: getMilliseconds */
  26884. DUK_DATE_FLAG_LOCALTIME + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  26885. /* 23: getUTCMilliseconds */
  26886. 0 + (DUK_DATE_IDX_MILLISECOND << DUK_DATE_FLAG_VALUE_SHIFT),
  26887. /* 24: setMilliseconds */
  26888. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  26889. /* 25: setUTCMilliseconds */
  26890. DUK_DATE_FLAG_TIMESETTER + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  26891. /* 26: setSeconds */
  26892. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  26893. /* 27: setUTCSeconds */
  26894. DUK_DATE_FLAG_TIMESETTER + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  26895. /* 28: setMinutes */
  26896. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  26897. /* 29: setUTCMinutes */
  26898. DUK_DATE_FLAG_TIMESETTER + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  26899. /* 30: setHours */
  26900. DUK_DATE_FLAG_TIMESETTER + DUK_DATE_FLAG_LOCALTIME + (4 << DUK_DATE_FLAG_VALUE_SHIFT),
  26901. /* 31: setUTCHours */
  26902. DUK_DATE_FLAG_TIMESETTER + (4 << DUK_DATE_FLAG_VALUE_SHIFT),
  26903. /* 32: setDate */
  26904. DUK_DATE_FLAG_LOCALTIME + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  26905. /* 33: setUTCDate */
  26906. 0 + (1 << DUK_DATE_FLAG_VALUE_SHIFT),
  26907. /* 34: setMonth */
  26908. DUK_DATE_FLAG_LOCALTIME + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  26909. /* 35: setUTCMonth */
  26910. 0 + (2 << DUK_DATE_FLAG_VALUE_SHIFT),
  26911. /* 36: setFullYear */
  26912. DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_LOCALTIME + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  26913. /* 37: setUTCFullYear */
  26914. DUK_DATE_FLAG_NAN_TO_ZERO + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  26915. /* 38: getYear */
  26916. DUK_DATE_FLAG_LOCALTIME + DUK_DATE_FLAG_SUB1900 + (DUK_DATE_IDX_YEAR << DUK_DATE_FLAG_VALUE_SHIFT),
  26917. /* 39: setYear */
  26918. DUK_DATE_FLAG_NAN_TO_ZERO + DUK_DATE_FLAG_YEAR_FIXUP + (3 << DUK_DATE_FLAG_VALUE_SHIFT),
  26919. };
  26920. DUK_LOCAL duk_small_uint_t duk__date_get_indirect_magic(duk_hthread *thr) {
  26921. duk_small_uint_t magicidx = (duk_small_uint_t) duk_get_current_magic(thr);
  26922. DUK_ASSERT(magicidx < (duk_small_int_t) (sizeof(duk__date_magics) / sizeof(duk_uint16_t)));
  26923. return (duk_small_uint_t) duk__date_magics[magicidx];
  26924. }
  26925. #if defined(DUK_USE_DATE_BUILTIN)
  26926. /*
  26927. * Constructor calls
  26928. */
  26929. DUK_INTERNAL duk_ret_t duk_bi_date_constructor(duk_hthread *thr) {
  26930. duk_idx_t nargs = duk_get_top(thr);
  26931. duk_bool_t is_cons = duk_is_constructor_call(thr);
  26932. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  26933. duk_double_t d;
  26934. DUK_DDD(DUK_DDDPRINT("Date constructor, nargs=%ld, is_cons=%ld", (long) nargs, (long) is_cons));
  26935. (void) duk_push_object_helper(thr,
  26936. DUK_HOBJECT_FLAG_EXTENSIBLE |
  26937. DUK_HOBJECT_FLAG_FASTREFS |
  26938. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DATE),
  26939. DUK_BIDX_DATE_PROTOTYPE);
  26940. /* Unlike most built-ins, the internal [[PrimitiveValue]] of a Date
  26941. * is mutable.
  26942. */
  26943. if (nargs == 0 || !is_cons) {
  26944. d = duk__timeclip(duk_time_get_ecmascript_time_nofrac(thr));
  26945. duk_push_number(thr, d);
  26946. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
  26947. if (!is_cons) {
  26948. /* called as a normal function: return new Date().toString() */
  26949. duk_to_string(thr, -1);
  26950. }
  26951. return 1;
  26952. } else if (nargs == 1) {
  26953. const char *str;
  26954. duk_to_primitive(thr, 0, DUK_HINT_NONE);
  26955. str = duk_get_string_notsymbol(thr, 0);
  26956. if (str) {
  26957. duk__parse_string(thr, str);
  26958. duk_replace(thr, 0); /* may be NaN */
  26959. }
  26960. d = duk__timeclip(duk_to_number(thr, 0)); /* symbols fail here */
  26961. duk_push_number(thr, d);
  26962. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_W);
  26963. return 1;
  26964. }
  26965. duk__set_parts_from_args(thr, dparts, nargs);
  26966. /* Parts are in local time, convert when setting. */
  26967. (void) duk__set_this_timeval_from_dparts(thr, dparts, DUK_DATE_FLAG_LOCALTIME /*flags*/); /* -> [ ... this timeval ] */
  26968. duk_pop(thr); /* -> [ ... this ] */
  26969. return 1;
  26970. }
  26971. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_parse(duk_hthread *thr) {
  26972. return duk__parse_string(thr, duk_to_string(thr, 0));
  26973. }
  26974. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_utc(duk_hthread *thr) {
  26975. duk_idx_t nargs = duk_get_top(thr);
  26976. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  26977. duk_double_t d;
  26978. /* Behavior for nargs < 2 is implementation dependent: currently we'll
  26979. * set a NaN time value (matching V8 behavior) in this case.
  26980. */
  26981. if (nargs < 2) {
  26982. duk_push_nan(thr);
  26983. } else {
  26984. duk__set_parts_from_args(thr, dparts, nargs);
  26985. d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
  26986. duk_push_number(thr, d);
  26987. }
  26988. return 1;
  26989. }
  26990. DUK_INTERNAL duk_ret_t duk_bi_date_constructor_now(duk_hthread *thr) {
  26991. duk_double_t d;
  26992. d = duk_time_get_ecmascript_time_nofrac(thr);
  26993. DUK_ASSERT(duk__timeclip(d) == d); /* TimeClip() should never be necessary */
  26994. duk_push_number(thr, d);
  26995. return 1;
  26996. }
  26997. /*
  26998. * String/JSON conversions
  26999. *
  27000. * Human readable conversions are now basically ISO 8601 with a space
  27001. * (instead of 'T') as the date/time separator. This is a good baseline
  27002. * and is platform independent.
  27003. *
  27004. * A shared native helper to provide many conversions. Magic value contains
  27005. * a set of flags. The helper provides:
  27006. *
  27007. * toString()
  27008. * toDateString()
  27009. * toTimeString()
  27010. * toLocaleString()
  27011. * toLocaleDateString()
  27012. * toLocaleTimeString()
  27013. * toUTCString()
  27014. * toISOString()
  27015. *
  27016. * Notes:
  27017. *
  27018. * - Date.prototype.toGMTString() and Date.prototype.toUTCString() are
  27019. * required to be the same ECMAScript function object (!), so it is
  27020. * omitted from here.
  27021. *
  27022. * - Date.prototype.toUTCString(): E5.1 specification does not require a
  27023. * specific format, but result should be human readable. The
  27024. * specification suggests using ISO 8601 format with a space (instead
  27025. * of 'T') separator if a more human readable format is not available.
  27026. *
  27027. * - Date.prototype.toISOString(): unlike other conversion functions,
  27028. * toISOString() requires a RangeError for invalid date values.
  27029. */
  27030. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_tostring_shared(duk_hthread *thr) {
  27031. duk_small_uint_t flags = duk__date_get_indirect_magic(thr);
  27032. return duk__to_string_helper(thr, flags);
  27033. }
  27034. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_value_of(duk_hthread *thr) {
  27035. /* This native function is also used for Date.prototype.getTime()
  27036. * as their behavior is identical.
  27037. */
  27038. duk_double_t d = duk__push_this_get_timeval(thr, 0 /*flags*/); /* -> [ this ] */
  27039. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  27040. duk_push_number(thr, d);
  27041. return 1;
  27042. }
  27043. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_to_json(duk_hthread *thr) {
  27044. /* Note: toJSON() is a generic function which works even if 'this'
  27045. * is not a Date. The sole argument is ignored.
  27046. */
  27047. duk_push_this(thr);
  27048. duk_to_object(thr, -1);
  27049. duk_dup_top(thr);
  27050. duk_to_primitive(thr, -1, DUK_HINT_NUMBER);
  27051. if (duk_is_number(thr, -1)) {
  27052. duk_double_t d = duk_get_number(thr, -1);
  27053. if (!DUK_ISFINITE(d)) {
  27054. duk_push_null(thr);
  27055. return 1;
  27056. }
  27057. }
  27058. duk_pop(thr);
  27059. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_TO_ISO_STRING);
  27060. duk_dup_m2(thr); /* -> [ O toIsoString O ] */
  27061. duk_call_method(thr, 0);
  27062. return 1;
  27063. }
  27064. /*
  27065. * Getters.
  27066. *
  27067. * Implementing getters is quite easy. The internal time value is either
  27068. * NaN, or represents milliseconds (without fractions) from Jan 1, 1970.
  27069. * The internal time value can be converted to integer parts, and each
  27070. * part will be normalized and will fit into a 32-bit signed integer.
  27071. *
  27072. * A shared native helper to provide all getters. Magic value contains
  27073. * a set of flags and also packs the date component index argument. The
  27074. * helper provides:
  27075. *
  27076. * getFullYear()
  27077. * getUTCFullYear()
  27078. * getMonth()
  27079. * getUTCMonth()
  27080. * getDate()
  27081. * getUTCDate()
  27082. * getDay()
  27083. * getUTCDay()
  27084. * getHours()
  27085. * getUTCHours()
  27086. * getMinutes()
  27087. * getUTCMinutes()
  27088. * getSeconds()
  27089. * getUTCSeconds()
  27090. * getMilliseconds()
  27091. * getUTCMilliseconds()
  27092. * getYear()
  27093. *
  27094. * Notes:
  27095. *
  27096. * - Date.prototype.getDate(): 'date' means day-of-month, and is
  27097. * zero-based in internal calculations but public API expects it to
  27098. * be one-based.
  27099. *
  27100. * - Date.prototype.getTime() and Date.prototype.valueOf() have identical
  27101. * behavior. They have separate function objects, but share the same C
  27102. * function (duk_bi_date_prototype_value_of).
  27103. */
  27104. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_shared(duk_hthread *thr) {
  27105. duk_small_uint_t flags_and_idx = duk__date_get_indirect_magic(thr);
  27106. return duk__get_part_helper(thr, flags_and_idx);
  27107. }
  27108. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_get_timezone_offset(duk_hthread *thr) {
  27109. /*
  27110. * Return (t - LocalTime(t)) in minutes:
  27111. *
  27112. * t - LocalTime(t) = t - (t + LocalTZA + DaylightSavingTA(t))
  27113. * = -(LocalTZA + DaylightSavingTA(t))
  27114. *
  27115. * where DaylightSavingTA() is checked for time 't'.
  27116. *
  27117. * Note that the sign of the result is opposite to common usage,
  27118. * e.g. for EE(S)T which normally is +2h or +3h from UTC, this
  27119. * function returns -120 or -180.
  27120. *
  27121. */
  27122. duk_double_t d;
  27123. duk_int_t tzoffset;
  27124. /* Note: DST adjustment is determined using UTC time. */
  27125. d = duk__push_this_get_timeval(thr, 0 /*flags*/);
  27126. DUK_ASSERT(DUK_ISFINITE(d) || DUK_ISNAN(d));
  27127. if (DUK_ISNAN(d)) {
  27128. duk_push_nan(thr);
  27129. } else {
  27130. DUK_ASSERT(DUK_ISFINITE(d));
  27131. tzoffset = DUK_USE_DATE_GET_LOCAL_TZOFFSET(d);
  27132. duk_push_int(thr, -tzoffset / 60);
  27133. }
  27134. return 1;
  27135. }
  27136. /*
  27137. * Setters.
  27138. *
  27139. * Setters are a bit more complicated than getters. Component setters
  27140. * break down the current time value into its (normalized) component
  27141. * parts, replace one or more components with -unnormalized- new values,
  27142. * and the components are then converted back into a time value. As an
  27143. * example of using unnormalized values:
  27144. *
  27145. * var d = new Date(1234567890);
  27146. *
  27147. * is equivalent to:
  27148. *
  27149. * var d = new Date(0);
  27150. * d.setUTCMilliseconds(1234567890);
  27151. *
  27152. * A shared native helper to provide almost all setters. Magic value
  27153. * contains a set of flags and also packs the "maxnargs" argument. The
  27154. * helper provides:
  27155. *
  27156. * setMilliseconds()
  27157. * setUTCMilliseconds()
  27158. * setSeconds()
  27159. * setUTCSeconds()
  27160. * setMinutes()
  27161. * setUTCMinutes()
  27162. * setHours()
  27163. * setUTCHours()
  27164. * setDate()
  27165. * setUTCDate()
  27166. * setMonth()
  27167. * setUTCMonth()
  27168. * setFullYear()
  27169. * setUTCFullYear()
  27170. * setYear()
  27171. *
  27172. * Notes:
  27173. *
  27174. * - Date.prototype.setYear() (Section B addition): special year check
  27175. * is omitted. NaN / Infinity will just flow through and ultimately
  27176. * result in a NaN internal time value.
  27177. *
  27178. * - Date.prototype.setYear() does not have optional arguments for
  27179. * setting month and day-in-month (like setFullYear()), but we indicate
  27180. * 'maxnargs' to be 3 to get the year written to the correct component
  27181. * index in duk__set_part_helper(). The function has nargs == 1, so only
  27182. * the year will be set regardless of actual argument count.
  27183. */
  27184. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_shared(duk_hthread *thr) {
  27185. duk_small_uint_t flags_and_maxnargs = duk__date_get_indirect_magic(thr);
  27186. return duk__set_part_helper(thr, flags_and_maxnargs);
  27187. }
  27188. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_set_time(duk_hthread *thr) {
  27189. duk_double_t d;
  27190. (void) duk__push_this_get_timeval(thr, 0 /*flags*/); /* -> [ timeval this ] */
  27191. d = duk__timeclip(duk_to_number(thr, 0));
  27192. duk_push_number(thr, d);
  27193. duk_dup_top(thr);
  27194. duk_put_prop_stridx_short(thr, -3, DUK_STRIDX_INT_VALUE); /* -> [ timeval this timeval ] */
  27195. return 1;
  27196. }
  27197. /*
  27198. * Misc.
  27199. */
  27200. #if defined(DUK_USE_SYMBOL_BUILTIN)
  27201. DUK_INTERNAL duk_ret_t duk_bi_date_prototype_toprimitive(duk_hthread *thr) {
  27202. duk_size_t hintlen;
  27203. const char *hintstr;
  27204. duk_int_t hint;
  27205. /* Invokes OrdinaryToPrimitive() with suitable hint. Note that the
  27206. * method is generic, and works on non-Date arguments too.
  27207. *
  27208. * https://www.ecma-international.org/ecma-262/6.0/#sec-date.prototype-@@toprimitive
  27209. */
  27210. duk_push_this(thr);
  27211. duk_require_object(thr, -1);
  27212. DUK_ASSERT_TOP(thr, 2);
  27213. hintstr = duk_require_lstring(thr, 0, &hintlen);
  27214. if ((hintlen == 6 && DUK_STRCMP(hintstr, "string") == 0) ||
  27215. (hintlen == 7 && DUK_STRCMP(hintstr, "default") == 0)) {
  27216. hint = DUK_HINT_STRING;
  27217. } else if (hintlen == 6 && DUK_STRCMP(hintstr, "number") == 0) {
  27218. hint = DUK_HINT_NUMBER;
  27219. } else {
  27220. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  27221. }
  27222. duk_to_primitive_ordinary(thr, -1, hint);
  27223. return 1;
  27224. }
  27225. #endif /* DUK_USE_SYMBOL_BUILTIN */
  27226. #endif /* DUK_USE_DATE_BUILTIN */
  27227. /* automatic undefs */
  27228. #undef DUK__CF_ACCEPT
  27229. #undef DUK__CF_ACCEPT_NUL
  27230. #undef DUK__CF_NEG
  27231. #undef DUK__DPRINT_DPARTS
  27232. #undef DUK__DPRINT_PARTS
  27233. #undef DUK__DPRINT_PARTS_AND_DPARTS
  27234. #undef DUK__LOCAL_TZOFFSET_MAXITER
  27235. #undef DUK__NUM_ISO8601_PARSER_PARTS
  27236. #undef DUK__PACK_RULE
  27237. #undef DUK__PI_DAY
  27238. #undef DUK__PI_HOUR
  27239. #undef DUK__PI_MILLISECOND
  27240. #undef DUK__PI_MINUTE
  27241. #undef DUK__PI_MONTH
  27242. #undef DUK__PI_SECOND
  27243. #undef DUK__PI_TZHOUR
  27244. #undef DUK__PI_TZMINUTE
  27245. #undef DUK__PI_YEAR
  27246. #undef DUK__PM_DAY
  27247. #undef DUK__PM_HOUR
  27248. #undef DUK__PM_MILLISECOND
  27249. #undef DUK__PM_MINUTE
  27250. #undef DUK__PM_MONTH
  27251. #undef DUK__PM_SECOND
  27252. #undef DUK__PM_TZHOUR
  27253. #undef DUK__PM_TZMINUTE
  27254. #undef DUK__PM_YEAR
  27255. #undef DUK__RULE_MASK_PART_SEP
  27256. #undef DUK__SI_COLON
  27257. #undef DUK__SI_MINUS
  27258. #undef DUK__SI_NUL
  27259. #undef DUK__SI_PERIOD
  27260. #undef DUK__SI_PLUS
  27261. #undef DUK__SI_SPACE
  27262. #undef DUK__SI_T
  27263. #undef DUK__SI_Z
  27264. #undef DUK__SM_COLON
  27265. #undef DUK__SM_MINUS
  27266. #undef DUK__SM_NUL
  27267. #undef DUK__SM_PERIOD
  27268. #undef DUK__SM_PLUS
  27269. #undef DUK__SM_SPACE
  27270. #undef DUK__SM_T
  27271. #undef DUK__SM_Z
  27272. #undef DUK__UNPACK_RULE
  27273. #undef DUK__WEEKDAY_MOD_ADDER
  27274. #undef DUK__YEAR
  27275. #line 1 "duk_bi_date_unix.c"
  27276. /*
  27277. * Unix-like Date providers
  27278. *
  27279. * Generally useful Unix / POSIX / ANSI Date providers.
  27280. */
  27281. /* #include duk_internal.h -> already included */
  27282. /* The necessary #includes are in place in duk_config.h. */
  27283. /* Buffer sizes for some UNIX calls. Larger than strictly necessary
  27284. * to avoid Valgrind errors.
  27285. */
  27286. #define DUK__STRPTIME_BUF_SIZE 64
  27287. #define DUK__STRFTIME_BUF_SIZE 64
  27288. #if defined(DUK_USE_DATE_NOW_GETTIMEOFDAY)
  27289. /* Get current ECMAScript time (= UNIX/Posix time, but in milliseconds). */
  27290. DUK_INTERNAL duk_double_t duk_bi_date_get_now_gettimeofday(void) {
  27291. struct timeval tv;
  27292. duk_double_t d;
  27293. if (gettimeofday(&tv, NULL) != 0) {
  27294. DUK_D(DUK_DPRINT("gettimeofday() failed"));
  27295. return 0.0;
  27296. }
  27297. /* As of Duktape 2.2.0 allow fractions. */
  27298. d = ((duk_double_t) tv.tv_sec) * 1000.0 +
  27299. ((duk_double_t) tv.tv_usec) / 1000.0;
  27300. return d;
  27301. }
  27302. #endif /* DUK_USE_DATE_NOW_GETTIMEOFDAY */
  27303. #if defined(DUK_USE_DATE_NOW_TIME)
  27304. /* Not a very good provider: only full seconds are available. */
  27305. DUK_INTERNAL duk_double_t duk_bi_date_get_now_time(void) {
  27306. time_t t;
  27307. t = time(NULL);
  27308. if (t == (time_t) -1) {
  27309. DUK_D(DUK_DPRINT("time() failed"));
  27310. return 0.0;
  27311. }
  27312. return ((duk_double_t) t) * 1000.0;
  27313. }
  27314. #endif /* DUK_USE_DATE_NOW_TIME */
  27315. #if defined(DUK_USE_DATE_TZO_GMTIME) || defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME_S)
  27316. /* Get local time offset (in seconds) for a certain (UTC) instant 'd'. */
  27317. DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d) {
  27318. time_t t, t1, t2;
  27319. duk_int_t parts[DUK_DATE_IDX_NUM_PARTS];
  27320. duk_double_t dparts[DUK_DATE_IDX_NUM_PARTS];
  27321. struct tm tms[2];
  27322. #if defined(DUK_USE_DATE_TZO_GMTIME)
  27323. struct tm *tm_ptr;
  27324. #endif
  27325. /* For NaN/inf, the return value doesn't matter. */
  27326. if (!DUK_ISFINITE(d)) {
  27327. return 0;
  27328. }
  27329. /* If not within ECMAScript range, some integer time calculations
  27330. * won't work correctly (and some asserts will fail), so bail out
  27331. * if so. This fixes test-bug-date-insane-setyear.js. There is
  27332. * a +/- 24h leeway in this range check to avoid a test262 corner
  27333. * case documented in test-bug-date-timeval-edges.js.
  27334. */
  27335. if (!duk_bi_date_timeval_in_leeway_range(d)) {
  27336. DUK_DD(DUK_DDPRINT("timeval not within valid range, skip tzoffset computation to avoid integer overflows"));
  27337. return 0;
  27338. }
  27339. /*
  27340. * This is a bit tricky to implement portably. The result depends
  27341. * on the timestamp (specifically, DST depends on the timestamp).
  27342. * If e.g. UNIX APIs are used, they'll have portability issues with
  27343. * very small and very large years.
  27344. *
  27345. * Current approach:
  27346. *
  27347. * - Stay within portable UNIX limits by using equivalent year mapping.
  27348. * Avoid year 1970 and 2038 as some conversions start to fail, at
  27349. * least on some platforms. Avoiding 1970 means that there are
  27350. * currently DST discrepancies for 1970.
  27351. *
  27352. * - Create a UTC and local time breakdowns from 't'. Then create
  27353. * a time_t using gmtime() and localtime() and compute the time
  27354. * difference between the two.
  27355. *
  27356. * Equivalent year mapping (E5 Section 15.9.1.8):
  27357. *
  27358. * If the host environment provides functionality for determining
  27359. * daylight saving time, the implementation of ECMAScript is free
  27360. * to map the year in question to an equivalent year (same
  27361. * leap-year-ness and same starting week day for the year) for which
  27362. * the host environment provides daylight saving time information.
  27363. * The only restriction is that all equivalent years should produce
  27364. * the same result.
  27365. *
  27366. * This approach is quite reasonable but not entirely correct, e.g.
  27367. * the specification also states (E5 Section 15.9.1.8):
  27368. *
  27369. * The implementation of ECMAScript should not try to determine
  27370. * whether the exact time was subject to daylight saving time, but
  27371. * just whether daylight saving time would have been in effect if
  27372. * the _current daylight saving time algorithm_ had been used at the
  27373. * time. This avoids complications such as taking into account the
  27374. * years that the locale observed daylight saving time year round.
  27375. *
  27376. * Since we rely on the platform APIs for conversions between local
  27377. * time and UTC, we can't guarantee the above. Rather, if the platform
  27378. * has historical DST rules they will be applied. This seems to be the
  27379. * general preferred direction in ECMAScript standardization (or at least
  27380. * implementations) anyway, and even the equivalent year mapping should
  27381. * be disabled if the platform is known to handle DST properly for the
  27382. * full ECMAScript range.
  27383. *
  27384. * The following has useful discussion and links:
  27385. *
  27386. * https://bugzilla.mozilla.org/show_bug.cgi?id=351066
  27387. */
  27388. duk_bi_date_timeval_to_parts(d, parts, dparts, DUK_DATE_FLAG_EQUIVYEAR /*flags*/);
  27389. DUK_ASSERT(parts[DUK_DATE_IDX_YEAR] >= 1970 && parts[DUK_DATE_IDX_YEAR] <= 2038);
  27390. d = duk_bi_date_get_timeval_from_dparts(dparts, 0 /*flags*/);
  27391. DUK_ASSERT(d >= 0 && d < 2147483648.0 * 1000.0); /* unsigned 31-bit range */
  27392. t = (time_t) (d / 1000.0);
  27393. DUK_DDD(DUK_DDDPRINT("timeval: %lf -> time_t %ld", (double) d, (long) t));
  27394. duk_memzero((void *) tms, sizeof(struct tm) * 2);
  27395. #if defined(DUK_USE_DATE_TZO_GMTIME_R)
  27396. (void) gmtime_r(&t, &tms[0]);
  27397. (void) localtime_r(&t, &tms[1]);
  27398. #elif defined(DUK_USE_DATE_TZO_GMTIME_S)
  27399. (void) gmtime_s(&t, &tms[0]);
  27400. (void) localtime_s(&t, &tms[1]);
  27401. #elif defined(DUK_USE_DATE_TZO_GMTIME)
  27402. tm_ptr = gmtime(&t);
  27403. duk_memcpy((void *) &tms[0], tm_ptr, sizeof(struct tm));
  27404. tm_ptr = localtime(&t);
  27405. duk_memcpy((void *) &tms[1], tm_ptr, sizeof(struct tm));
  27406. #else
  27407. #error internal error
  27408. #endif
  27409. DUK_DDD(DUK_DDDPRINT("gmtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  27410. "wday:%ld,yday:%ld,isdst:%ld}",
  27411. (long) tms[0].tm_sec, (long) tms[0].tm_min, (long) tms[0].tm_hour,
  27412. (long) tms[0].tm_mday, (long) tms[0].tm_mon, (long) tms[0].tm_year,
  27413. (long) tms[0].tm_wday, (long) tms[0].tm_yday, (long) tms[0].tm_isdst));
  27414. DUK_DDD(DUK_DDDPRINT("localtime result: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  27415. "wday:%ld,yday:%ld,isdst:%ld}",
  27416. (long) tms[1].tm_sec, (long) tms[1].tm_min, (long) tms[1].tm_hour,
  27417. (long) tms[1].tm_mday, (long) tms[1].tm_mon, (long) tms[1].tm_year,
  27418. (long) tms[1].tm_wday, (long) tms[1].tm_yday, (long) tms[1].tm_isdst));
  27419. /* tm_isdst is both an input and an output to mktime(), use 0 to
  27420. * avoid DST handling in mktime():
  27421. * - https://github.com/svaarala/duktape/issues/406
  27422. * - http://stackoverflow.com/questions/8558919/mktime-and-tm-isdst
  27423. */
  27424. tms[0].tm_isdst = 0;
  27425. tms[1].tm_isdst = 0;
  27426. t1 = mktime(&tms[0]); /* UTC */
  27427. t2 = mktime(&tms[1]); /* local */
  27428. if (t1 == (time_t) -1 || t2 == (time_t) -1) {
  27429. /* This check used to be for (t < 0) but on some platforms
  27430. * time_t is unsigned and apparently the proper way to detect
  27431. * an mktime() error return is the cast above. See e.g.:
  27432. * http://pubs.opengroup.org/onlinepubs/009695299/functions/mktime.html
  27433. */
  27434. goto mktime_error;
  27435. }
  27436. DUK_DDD(DUK_DDDPRINT("t1=%ld (utc), t2=%ld (local)", (long) t1, (long) t2));
  27437. /* Compute final offset in seconds, positive if local time ahead of
  27438. * UTC (returned value is UTC-to-local offset).
  27439. *
  27440. * difftime() returns a double, so coercion to int generates quite
  27441. * a lot of code. Direct subtraction is not portable, however.
  27442. * XXX: allow direct subtraction on known platforms.
  27443. */
  27444. #if 0
  27445. return (duk_int_t) (t2 - t1);
  27446. #endif
  27447. return (duk_int_t) difftime(t2, t1);
  27448. mktime_error:
  27449. /* XXX: return something more useful, so that caller can throw? */
  27450. DUK_D(DUK_DPRINT("mktime() failed, d=%lf", (double) d));
  27451. return 0;
  27452. }
  27453. #endif /* DUK_USE_DATE_TZO_GMTIME */
  27454. #if defined(DUK_USE_DATE_PRS_STRPTIME)
  27455. DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_strptime(duk_hthread *thr, const char *str) {
  27456. struct tm tm;
  27457. time_t t;
  27458. char buf[DUK__STRPTIME_BUF_SIZE];
  27459. /* Copy to buffer with slack to avoid Valgrind gripes from strptime. */
  27460. DUK_ASSERT(str != NULL);
  27461. duk_memzero(buf, sizeof(buf)); /* valgrind whine without this */
  27462. DUK_SNPRINTF(buf, sizeof(buf), "%s", (const char *) str);
  27463. buf[sizeof(buf) - 1] = (char) 0;
  27464. DUK_DDD(DUK_DDDPRINT("parsing: '%s'", (const char *) buf));
  27465. duk_memzero(&tm, sizeof(tm));
  27466. if (strptime((const char *) buf, "%c", &tm) != NULL) {
  27467. DUK_DDD(DUK_DDDPRINT("before mktime: tm={sec:%ld,min:%ld,hour:%ld,mday:%ld,mon:%ld,year:%ld,"
  27468. "wday:%ld,yday:%ld,isdst:%ld}",
  27469. (long) tm.tm_sec, (long) tm.tm_min, (long) tm.tm_hour,
  27470. (long) tm.tm_mday, (long) tm.tm_mon, (long) tm.tm_year,
  27471. (long) tm.tm_wday, (long) tm.tm_yday, (long) tm.tm_isdst));
  27472. tm.tm_isdst = -1; /* negative: dst info not available */
  27473. t = mktime(&tm);
  27474. DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t));
  27475. if (t >= 0) {
  27476. duk_push_number(thr, ((duk_double_t) t) * 1000.0);
  27477. return 1;
  27478. }
  27479. }
  27480. return 0;
  27481. }
  27482. #endif /* DUK_USE_DATE_PRS_STRPTIME */
  27483. #if defined(DUK_USE_DATE_PRS_GETDATE)
  27484. DUK_INTERNAL duk_bool_t duk_bi_date_parse_string_getdate(duk_hthread *thr, const char *str) {
  27485. struct tm tm;
  27486. duk_small_int_t rc;
  27487. time_t t;
  27488. /* For this to work, DATEMSK must be set, so this is not very
  27489. * convenient for an embeddable interpreter.
  27490. */
  27491. duk_memzero(&tm, sizeof(struct tm));
  27492. rc = (duk_small_int_t) getdate_r(str, &tm);
  27493. DUK_DDD(DUK_DDDPRINT("getdate_r() -> %ld", (long) rc));
  27494. if (rc == 0) {
  27495. t = mktime(&tm);
  27496. DUK_DDD(DUK_DDDPRINT("mktime() -> %ld", (long) t));
  27497. if (t >= 0) {
  27498. duk_push_number(thr, (duk_double_t) t);
  27499. return 1;
  27500. }
  27501. }
  27502. return 0;
  27503. }
  27504. #endif /* DUK_USE_DATE_PRS_GETDATE */
  27505. #if defined(DUK_USE_DATE_FMT_STRFTIME)
  27506. DUK_INTERNAL duk_bool_t duk_bi_date_format_parts_strftime(duk_hthread *thr, duk_int_t *parts, duk_int_t tzoffset, duk_small_uint_t flags) {
  27507. char buf[DUK__STRFTIME_BUF_SIZE];
  27508. struct tm tm;
  27509. const char *fmt;
  27510. DUK_UNREF(tzoffset);
  27511. /* If the platform doesn't support the entire ECMAScript range, we need
  27512. * to return 0 so that the caller can fall back to the default formatter.
  27513. *
  27514. * For now, assume that if time_t is 8 bytes or more, the whole ECMAScript
  27515. * range is supported. For smaller time_t values (4 bytes in practice),
  27516. * assumes that the signed 32-bit range is supported.
  27517. *
  27518. * XXX: detect this more correctly per platform. The size of time_t is
  27519. * probably not an accurate guarantee of strftime() supporting or not
  27520. * supporting a large time range (the full ECMAScript range).
  27521. */
  27522. if (sizeof(time_t) < 8 &&
  27523. (parts[DUK_DATE_IDX_YEAR] < 1970 || parts[DUK_DATE_IDX_YEAR] > 2037)) {
  27524. /* be paranoid for 32-bit time values (even avoiding negative ones) */
  27525. return 0;
  27526. }
  27527. duk_memzero(&tm, sizeof(tm));
  27528. tm.tm_sec = parts[DUK_DATE_IDX_SECOND];
  27529. tm.tm_min = parts[DUK_DATE_IDX_MINUTE];
  27530. tm.tm_hour = parts[DUK_DATE_IDX_HOUR];
  27531. tm.tm_mday = parts[DUK_DATE_IDX_DAY]; /* already one-based */
  27532. tm.tm_mon = parts[DUK_DATE_IDX_MONTH] - 1; /* one-based -> zero-based */
  27533. tm.tm_year = parts[DUK_DATE_IDX_YEAR] - 1900;
  27534. tm.tm_wday = parts[DUK_DATE_IDX_WEEKDAY];
  27535. tm.tm_isdst = 0;
  27536. duk_memzero(buf, sizeof(buf));
  27537. if ((flags & DUK_DATE_FLAG_TOSTRING_DATE) && (flags & DUK_DATE_FLAG_TOSTRING_TIME)) {
  27538. fmt = "%c";
  27539. } else if (flags & DUK_DATE_FLAG_TOSTRING_DATE) {
  27540. fmt = "%x";
  27541. } else {
  27542. DUK_ASSERT(flags & DUK_DATE_FLAG_TOSTRING_TIME);
  27543. fmt = "%X";
  27544. }
  27545. (void) strftime(buf, sizeof(buf) - 1, fmt, &tm);
  27546. DUK_ASSERT(buf[sizeof(buf) - 1] == 0);
  27547. duk_push_string(thr, buf);
  27548. return 1;
  27549. }
  27550. #endif /* DUK_USE_DATE_FMT_STRFTIME */
  27551. #if defined(DUK_USE_GET_MONOTONIC_TIME_CLOCK_GETTIME)
  27552. DUK_INTERNAL duk_double_t duk_bi_date_get_monotonic_time_clock_gettime(void) {
  27553. struct timespec ts;
  27554. if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
  27555. return (duk_double_t) ts.tv_sec * 1000.0 + (duk_double_t) ts.tv_nsec / 1000000.0;
  27556. } else {
  27557. DUK_D(DUK_DPRINT("clock_gettime(CLOCK_MONOTONIC) failed"));
  27558. return 0.0;
  27559. }
  27560. }
  27561. #endif
  27562. /* automatic undefs */
  27563. #undef DUK__STRFTIME_BUF_SIZE
  27564. #undef DUK__STRPTIME_BUF_SIZE
  27565. #line 1 "duk_bi_date_windows.c"
  27566. /*
  27567. * Windows Date providers
  27568. *
  27569. * Platform specific links:
  27570. *
  27571. * - http://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx
  27572. */
  27573. /* #include duk_internal.h -> already included */
  27574. /* The necessary #includes are in place in duk_config.h. */
  27575. #if defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS)
  27576. /* Shared Windows helpers. */
  27577. DUK_LOCAL void duk__convert_systime_to_ularge(const SYSTEMTIME *st, ULARGE_INTEGER *res) {
  27578. FILETIME ft;
  27579. if (SystemTimeToFileTime(st, &ft) == 0) {
  27580. DUK_D(DUK_DPRINT("SystemTimeToFileTime() failed, returning 0"));
  27581. res->QuadPart = 0;
  27582. } else {
  27583. res->LowPart = ft.dwLowDateTime;
  27584. res->HighPart = ft.dwHighDateTime;
  27585. }
  27586. }
  27587. #if defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS)
  27588. DUK_LOCAL void duk__convert_filetime_to_ularge(const FILETIME *ft, ULARGE_INTEGER *res) {
  27589. res->LowPart = ft->dwLowDateTime;
  27590. res->HighPart = ft->dwHighDateTime;
  27591. }
  27592. #endif /* DUK_USE_DATE_NOW_WINDOWS_SUBMS */
  27593. DUK_LOCAL void duk__set_systime_jan1970(SYSTEMTIME *st) {
  27594. duk_memzero((void *) st, sizeof(*st));
  27595. st->wYear = 1970;
  27596. st->wMonth = 1;
  27597. st->wDayOfWeek = 4; /* not sure whether or not needed; Thursday */
  27598. st->wDay = 1;
  27599. DUK_ASSERT(st->wHour == 0);
  27600. DUK_ASSERT(st->wMinute == 0);
  27601. DUK_ASSERT(st->wSecond == 0);
  27602. DUK_ASSERT(st->wMilliseconds == 0);
  27603. }
  27604. #endif /* defined(DUK_USE_DATE_NOW_WINDOWS) || defined(DUK_USE_DATE_TZO_WINDOWS) */
  27605. #if defined(DUK_USE_DATE_NOW_WINDOWS)
  27606. DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows(void) {
  27607. /* Suggested step-by-step method from documentation of RtlTimeToSecondsSince1970:
  27608. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724928(v=vs.85).aspx
  27609. */
  27610. SYSTEMTIME st1, st2;
  27611. ULARGE_INTEGER tmp1, tmp2;
  27612. GetSystemTime(&st1);
  27613. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  27614. duk__set_systime_jan1970(&st2);
  27615. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
  27616. /* Difference is in 100ns units, convert to milliseconds, keeping
  27617. * fractions since Duktape 2.2.0. This is only theoretical because
  27618. * SYSTEMTIME is limited to milliseconds.
  27619. */
  27620. return (duk_double_t) ((LONGLONG) tmp1.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000.0;
  27621. }
  27622. #endif /* DUK_USE_DATE_NOW_WINDOWS */
  27623. #if defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS)
  27624. DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows_subms(void) {
  27625. /* Variant of the basic algorithm using GetSystemTimePreciseAsFileTime()
  27626. * for more accuracy.
  27627. */
  27628. FILETIME ft1;
  27629. SYSTEMTIME st2;
  27630. ULARGE_INTEGER tmp1, tmp2;
  27631. GetSystemTimePreciseAsFileTime(&ft1);
  27632. duk__convert_filetime_to_ularge((const FILETIME *) &ft1, &tmp1);
  27633. duk__set_systime_jan1970(&st2);
  27634. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
  27635. /* Difference is in 100ns units, convert to milliseconds, keeping
  27636. * fractions since Duktape 2.2.0.
  27637. */
  27638. return (duk_double_t) ((LONGLONG) tmp1.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000.0;
  27639. }
  27640. #endif /* DUK_USE_DATE_NOW_WINDOWS */
  27641. #if defined(DUK_USE_DATE_TZO_WINDOWS)
  27642. DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d) {
  27643. SYSTEMTIME st1;
  27644. SYSTEMTIME st2;
  27645. SYSTEMTIME st3;
  27646. ULARGE_INTEGER tmp1;
  27647. ULARGE_INTEGER tmp2;
  27648. ULARGE_INTEGER tmp3;
  27649. FILETIME ft1;
  27650. BOOL ret;
  27651. /* XXX: handling of timestamps outside Windows supported range.
  27652. * How does Windows deal with dates before 1600? Does windows
  27653. * support all ECMAScript years (like -200000 and +200000)?
  27654. * Should equivalent year mapping be used here too? If so, use
  27655. * a shared helper (currently integrated into timeval-to-parts).
  27656. */
  27657. /* Use the approach described in "Remarks" of FileTimeToLocalFileTime:
  27658. * http://msdn.microsoft.com/en-us/library/windows/desktop/ms724277(v=vs.85).aspx
  27659. */
  27660. duk__set_systime_jan1970(&st1);
  27661. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  27662. tmp2.QuadPart = (ULONGLONG) (d * 10000.0); /* millisec -> 100ns units since jan 1, 1970 */
  27663. tmp2.QuadPart += tmp1.QuadPart; /* input 'd' in Windows UTC, 100ns units */
  27664. ft1.dwLowDateTime = tmp2.LowPart;
  27665. ft1.dwHighDateTime = tmp2.HighPart;
  27666. ret = FileTimeToSystemTime((const FILETIME *) &ft1, &st2);
  27667. if (!ret) {
  27668. DUK_D(DUK_DPRINT("FileTimeToSystemTime() failed, return tzoffset 0"));
  27669. return 0;
  27670. }
  27671. if (SystemTimeToTzSpecificLocalTime((LPTIME_ZONE_INFORMATION) NULL, &st2, &st3) == 0) {
  27672. DUK_D(DUK_DPRINT("SystemTimeToTzSpecificLocalTime() failed, return tzoffset 0"));
  27673. return 0;
  27674. }
  27675. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st3, &tmp3);
  27676. /* Positive if local time ahead of UTC. */
  27677. return (duk_int_t) (((LONGLONG) tmp3.QuadPart - (LONGLONG) tmp2.QuadPart) / DUK_I64_CONSTANT(10000000)); /* seconds */
  27678. }
  27679. #endif /* DUK_USE_DATE_TZO_WINDOWS */
  27680. #if defined(DUK_USE_DATE_TZO_WINDOWS_NO_DST)
  27681. DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_windows_no_dst(duk_double_t d) {
  27682. SYSTEMTIME st1;
  27683. SYSTEMTIME st2;
  27684. FILETIME ft1;
  27685. FILETIME ft2;
  27686. ULARGE_INTEGER tmp1;
  27687. ULARGE_INTEGER tmp2;
  27688. BOOL ret;
  27689. /* Do a similar computation to duk_bi_date_get_local_tzoffset_windows
  27690. * but without accounting for daylight savings time. Use this on
  27691. * Windows platforms (like Durango) that don't support the
  27692. * SystemTimeToTzSpecificLocalTime() call.
  27693. */
  27694. /* current time not needed for this computation */
  27695. DUK_UNREF(d);
  27696. duk__set_systime_jan1970(&st1);
  27697. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st1, &tmp1);
  27698. ft1.dwLowDateTime = tmp1.LowPart;
  27699. ft1.dwHighDateTime = tmp1.HighPart;
  27700. ret = FileTimeToLocalFileTime((const FILETIME *) &ft1, &ft2);
  27701. if (!ret) {
  27702. DUK_D(DUK_DPRINT("FileTimeToLocalFileTime() failed, return tzoffset 0"));
  27703. return 0;
  27704. }
  27705. ret = FileTimeToSystemTime((const FILETIME *) &ft2, &st2);
  27706. if (!ret) {
  27707. DUK_D(DUK_DPRINT("FileTimeToSystemTime() failed, return tzoffset 0"));
  27708. return 0;
  27709. }
  27710. duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
  27711. return (duk_int_t) (((LONGLONG) tmp2.QuadPart - (LONGLONG) tmp1.QuadPart) / DUK_I64_CONSTANT(10000000)); /* seconds */
  27712. }
  27713. #endif /* DUK_USE_DATE_TZO_WINDOWS_NO_DST */
  27714. #if defined(DUK_USE_GET_MONOTONIC_TIME_WINDOWS_QPC)
  27715. DUK_INTERNAL duk_double_t duk_bi_date_get_monotonic_time_windows_qpc(void) {
  27716. LARGE_INTEGER count, freq;
  27717. /* There are legacy issues with QueryPerformanceCounter():
  27718. * - Potential jumps: https://support.microsoft.com/en-us/help/274323/performance-counter-value-may-unexpectedly-leap-forward
  27719. * - Differences between cores (XP): https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx#qpc_support_in_windows_versions
  27720. *
  27721. * We avoid these by enabling QPC by default only for Vista or later.
  27722. */
  27723. if (QueryPerformanceCounter(&count) && QueryPerformanceFrequency(&freq)) {
  27724. /* XXX: QueryPerformanceFrequency() can be cached */
  27725. return (duk_double_t) count.QuadPart / (duk_double_t) freq.QuadPart * 1000.0;
  27726. } else {
  27727. /* MSDN: "On systems that run Windows XP or later, the function
  27728. * will always succeed and will thus never return zero."
  27729. * Provide minimal error path just in case user enables this
  27730. * feature in pre-XP Windows.
  27731. */
  27732. return 0.0;
  27733. }
  27734. }
  27735. #endif /* DUK_USE_GET_MONOTONIC_TIME_WINDOWS_QPC */
  27736. #line 1 "duk_bi_duktape.c"
  27737. /*
  27738. * Duktape built-ins
  27739. *
  27740. * Size optimization note: it might seem that vararg multipurpose functions
  27741. * like fin(), enc(), and dec() are not very size optimal, but using a single
  27742. * user-visible ECMAScript function saves a lot of run-time footprint; each
  27743. * Function instance takes >100 bytes. Using a shared native helper and a
  27744. * 'magic' value won't save much if there are multiple Function instances
  27745. * anyway.
  27746. */
  27747. /* #include duk_internal.h -> already included */
  27748. #if defined(DUK_USE_DUKTAPE_BUILTIN)
  27749. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_info(duk_hthread *thr) {
  27750. duk_inspect_value(thr, -1);
  27751. return 1;
  27752. }
  27753. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_act(duk_hthread *thr) {
  27754. duk_int_t level;
  27755. level = duk_to_int(thr, 0);
  27756. duk_inspect_callstack_entry(thr, level);
  27757. return 1;
  27758. }
  27759. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_gc(duk_hthread *thr) {
  27760. duk_small_uint_t flags;
  27761. flags = (duk_small_uint_t) duk_get_uint(thr, 0);
  27762. duk_heap_mark_and_sweep(thr->heap, flags);
  27763. /* XXX: Not sure what the best return value would be in the API.
  27764. * Return true for now.
  27765. */
  27766. duk_push_true(thr);
  27767. return 1;
  27768. }
  27769. #if defined(DUK_USE_FINALIZER_SUPPORT)
  27770. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_fin(duk_hthread *thr) {
  27771. (void) duk_require_hobject(thr, 0);
  27772. if (duk_get_top(thr) >= 2) {
  27773. /* Set: currently a finalizer is disabled by setting it to
  27774. * undefined; this does not remove the property at the moment.
  27775. * The value could be type checked to be either a function
  27776. * or something else; if something else, the property could
  27777. * be deleted. Must use duk_set_finalizer() to keep
  27778. * DUK_HOBJECT_FLAG_HAVE_FINALIZER in sync.
  27779. */
  27780. duk_set_top(thr, 2);
  27781. duk_set_finalizer(thr, 0);
  27782. return 0;
  27783. } else {
  27784. /* Get. */
  27785. DUK_ASSERT(duk_get_top(thr) == 1);
  27786. duk_get_finalizer(thr, 0);
  27787. return 1;
  27788. }
  27789. }
  27790. #endif /* DUK_USE_FINALIZER_SUPPORT */
  27791. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_enc(duk_hthread *thr) {
  27792. duk_hstring *h_str;
  27793. /* Vararg function: must be careful to check/require arguments.
  27794. * The JSON helpers accept invalid indices and treat them like
  27795. * non-existent optional parameters.
  27796. */
  27797. h_str = duk_require_hstring(thr, 0); /* Could reject symbols, but no point: won't match comparisons. */
  27798. duk_require_valid_index(thr, 1);
  27799. if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
  27800. duk_set_top(thr, 2);
  27801. duk_hex_encode(thr, 1);
  27802. DUK_ASSERT_TOP(thr, 2);
  27803. } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
  27804. duk_set_top(thr, 2);
  27805. duk_base64_encode(thr, 1);
  27806. DUK_ASSERT_TOP(thr, 2);
  27807. #if defined(DUK_USE_JSON_SUPPORT) && defined(DUK_USE_JX)
  27808. } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
  27809. duk_bi_json_stringify_helper(thr,
  27810. 1 /*idx_value*/,
  27811. 2 /*idx_replacer*/,
  27812. 3 /*idx_space*/,
  27813. DUK_JSON_FLAG_EXT_CUSTOM |
  27814. DUK_JSON_FLAG_ASCII_ONLY |
  27815. DUK_JSON_FLAG_AVOID_KEY_QUOTES /*flags*/);
  27816. #endif
  27817. #if defined(DUK_USE_JSON_SUPPORT) && defined(DUK_USE_JC)
  27818. } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
  27819. duk_bi_json_stringify_helper(thr,
  27820. 1 /*idx_value*/,
  27821. 2 /*idx_replacer*/,
  27822. 3 /*idx_space*/,
  27823. DUK_JSON_FLAG_EXT_COMPATIBLE |
  27824. DUK_JSON_FLAG_ASCII_ONLY /*flags*/);
  27825. #endif
  27826. } else {
  27827. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  27828. }
  27829. return 1;
  27830. }
  27831. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_dec(duk_hthread *thr) {
  27832. duk_hstring *h_str;
  27833. /* Vararg function: must be careful to check/require arguments.
  27834. * The JSON helpers accept invalid indices and treat them like
  27835. * non-existent optional parameters.
  27836. */
  27837. h_str = duk_require_hstring(thr, 0); /* Could reject symbols, but no point: won't match comparisons */
  27838. duk_require_valid_index(thr, 1);
  27839. if (h_str == DUK_HTHREAD_STRING_HEX(thr)) {
  27840. duk_set_top(thr, 2);
  27841. duk_hex_decode(thr, 1);
  27842. DUK_ASSERT_TOP(thr, 2);
  27843. } else if (h_str == DUK_HTHREAD_STRING_BASE64(thr)) {
  27844. duk_set_top(thr, 2);
  27845. duk_base64_decode(thr, 1);
  27846. DUK_ASSERT_TOP(thr, 2);
  27847. #if defined(DUK_USE_JSON_SUPPORT) && defined(DUK_USE_JX)
  27848. } else if (h_str == DUK_HTHREAD_STRING_JX(thr)) {
  27849. duk_bi_json_parse_helper(thr,
  27850. 1 /*idx_value*/,
  27851. 2 /*idx_replacer*/,
  27852. DUK_JSON_FLAG_EXT_CUSTOM /*flags*/);
  27853. #endif
  27854. #if defined(DUK_USE_JSON_SUPPORT) && defined(DUK_USE_JC)
  27855. } else if (h_str == DUK_HTHREAD_STRING_JC(thr)) {
  27856. duk_bi_json_parse_helper(thr,
  27857. 1 /*idx_value*/,
  27858. 2 /*idx_replacer*/,
  27859. DUK_JSON_FLAG_EXT_COMPATIBLE /*flags*/);
  27860. #endif
  27861. } else {
  27862. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  27863. }
  27864. return 1;
  27865. }
  27866. /*
  27867. * Compact an object
  27868. */
  27869. DUK_INTERNAL duk_ret_t duk_bi_duktape_object_compact(duk_hthread *thr) {
  27870. DUK_ASSERT_TOP(thr, 1);
  27871. duk_compact(thr, 0);
  27872. return 1; /* return the argument object */
  27873. }
  27874. #endif /* DUK_USE_DUKTAPE_BUILTIN */
  27875. #line 1 "duk_bi_encoding.c"
  27876. /*
  27877. * WHATWG Encoding API built-ins
  27878. *
  27879. * API specification: https://encoding.spec.whatwg.org/#api
  27880. * Web IDL: https://www.w3.org/TR/WebIDL/
  27881. */
  27882. /* #include duk_internal.h -> already included */
  27883. /*
  27884. * Data structures for encoding/decoding
  27885. */
  27886. typedef struct {
  27887. duk_uint8_t *out; /* where to write next byte(s) */
  27888. duk_codepoint_t lead; /* lead surrogate */
  27889. } duk__encode_context;
  27890. typedef struct {
  27891. /* UTF-8 decoding state */
  27892. duk_codepoint_t codepoint; /* built up incrementally */
  27893. duk_uint8_t upper; /* max value of next byte (decode error otherwise) */
  27894. duk_uint8_t lower; /* min value of next byte (ditto) */
  27895. duk_uint8_t needed; /* how many more bytes we need */
  27896. duk_uint8_t bom_handled; /* BOM seen or no longer expected */
  27897. /* Decoder configuration */
  27898. duk_uint8_t fatal;
  27899. duk_uint8_t ignore_bom;
  27900. } duk__decode_context;
  27901. /* The signed duk_codepoint_t type is used to signal a decoded codepoint
  27902. * (>= 0) or various other states using negative values.
  27903. */
  27904. #define DUK__CP_CONTINUE (-1) /* continue to next byte, no completed codepoint */
  27905. #define DUK__CP_ERROR (-2) /* decoding error */
  27906. #define DUK__CP_RETRY (-3) /* decoding error; retry last byte */
  27907. /*
  27908. * Raw helpers for encoding/decoding
  27909. */
  27910. /* Emit UTF-8 (= CESU-8) encoded U+FFFD (replacement char), i.e. ef bf bd. */
  27911. DUK_LOCAL duk_uint8_t *duk__utf8_emit_repl(duk_uint8_t *ptr) {
  27912. *ptr++ = 0xef;
  27913. *ptr++ = 0xbf;
  27914. *ptr++ = 0xbd;
  27915. return ptr;
  27916. }
  27917. DUK_LOCAL void duk__utf8_decode_init(duk__decode_context *dec_ctx) {
  27918. /* (Re)init the decoding state of 'dec_ctx' but leave decoder
  27919. * configuration fields untouched.
  27920. */
  27921. dec_ctx->codepoint = 0x0000L;
  27922. dec_ctx->upper = 0xbf;
  27923. dec_ctx->lower = 0x80;
  27924. dec_ctx->needed = 0;
  27925. dec_ctx->bom_handled = 0;
  27926. }
  27927. DUK_LOCAL duk_codepoint_t duk__utf8_decode_next(duk__decode_context *dec_ctx, duk_uint8_t x) {
  27928. /*
  27929. * UTF-8 algorithm based on the Encoding specification:
  27930. * https://encoding.spec.whatwg.org/#utf-8-decoder
  27931. *
  27932. * Two main states: decoding initial byte vs. decoding continuation
  27933. * bytes. Shortest length encoding is validated by restricting the
  27934. * allowed range of first continuation byte using 'lower' and 'upper'.
  27935. */
  27936. if (dec_ctx->needed == 0) {
  27937. /* process initial byte */
  27938. if (x <= 0x7f) {
  27939. /* U+0000-U+007F, 1 byte (ASCII) */
  27940. return (duk_codepoint_t) x;
  27941. } else if (x >= 0xc2 && x <= 0xdf) {
  27942. /* U+0080-U+07FF, 2 bytes */
  27943. dec_ctx->needed = 1;
  27944. dec_ctx->codepoint = x & 0x1f;
  27945. DUK_ASSERT(dec_ctx->lower == 0x80);
  27946. DUK_ASSERT(dec_ctx->upper == 0xbf);
  27947. return DUK__CP_CONTINUE;
  27948. } else if (x >= 0xe0 && x <= 0xef) {
  27949. /* U+0800-U+FFFF, 3 bytes */
  27950. if (x == 0xe0) {
  27951. dec_ctx->lower = 0xa0;
  27952. DUK_ASSERT(dec_ctx->upper == 0xbf);
  27953. } else if (x == 0xed) {
  27954. DUK_ASSERT(dec_ctx->lower == 0x80);
  27955. dec_ctx->upper = 0x9f;
  27956. }
  27957. dec_ctx->needed = 2;
  27958. dec_ctx->codepoint = x & 0x0f;
  27959. return DUK__CP_CONTINUE;
  27960. } else if (x >= 0xf0 && x <= 0xf4) {
  27961. /* U+010000-U+10FFFF, 4 bytes */
  27962. if (x == 0xf0) {
  27963. dec_ctx->lower = 0x90;
  27964. DUK_ASSERT(dec_ctx->upper == 0xbf);
  27965. } else if (x == 0xf4) {
  27966. DUK_ASSERT(dec_ctx->lower == 0x80);
  27967. dec_ctx->upper = 0x8f;
  27968. }
  27969. dec_ctx->needed = 3;
  27970. dec_ctx->codepoint = x & 0x07;
  27971. return DUK__CP_CONTINUE;
  27972. } else {
  27973. /* not a legal initial byte */
  27974. return DUK__CP_ERROR;
  27975. }
  27976. } else {
  27977. /* process continuation byte */
  27978. if (x >= dec_ctx->lower && x <= dec_ctx->upper) {
  27979. dec_ctx->lower = 0x80;
  27980. dec_ctx->upper = 0xbf;
  27981. dec_ctx->codepoint = (dec_ctx->codepoint << 6) | (x & 0x3f);
  27982. if (--dec_ctx->needed > 0) {
  27983. /* need more bytes */
  27984. return DUK__CP_CONTINUE;
  27985. } else {
  27986. /* got a codepoint */
  27987. duk_codepoint_t ret;
  27988. DUK_ASSERT(dec_ctx->codepoint <= 0x10ffffL); /* Decoding rules guarantee. */
  27989. ret = dec_ctx->codepoint;
  27990. dec_ctx->codepoint = 0x0000L;
  27991. dec_ctx->needed = 0;
  27992. return ret;
  27993. }
  27994. } else {
  27995. /* We just encountered an illegal UTF-8 continuation byte. This might
  27996. * be the initial byte of the next character; if we return a plain
  27997. * error status and the decoder is in replacement mode, the character
  27998. * will be masked. We still need to alert the caller to the error
  27999. * though.
  28000. */
  28001. dec_ctx->codepoint = 0x0000L;
  28002. dec_ctx->needed = 0;
  28003. dec_ctx->lower = 0x80;
  28004. dec_ctx->upper = 0xbf;
  28005. return DUK__CP_RETRY;
  28006. }
  28007. }
  28008. }
  28009. #if defined(DUK_USE_ENCODING_BUILTINS)
  28010. DUK_LOCAL void duk__utf8_encode_char(void *udata, duk_codepoint_t codepoint) {
  28011. duk__encode_context *enc_ctx;
  28012. DUK_ASSERT(codepoint >= 0);
  28013. enc_ctx = (duk__encode_context *) udata;
  28014. DUK_ASSERT(enc_ctx != NULL);
  28015. #if !defined(DUK_USE_PREFER_SIZE)
  28016. if (codepoint <= 0x7f && enc_ctx->lead == 0x0000L) {
  28017. /* Fast path for ASCII. */
  28018. *enc_ctx->out++ = (duk_uint8_t) codepoint;
  28019. return;
  28020. }
  28021. #endif
  28022. if (DUK_UNLIKELY(codepoint > 0x10ffffL)) {
  28023. /* cannot legally encode in UTF-8 */
  28024. codepoint = DUK_UNICODE_CP_REPLACEMENT_CHARACTER;
  28025. } else if (codepoint >= 0xd800L && codepoint <= 0xdfffL) {
  28026. if (codepoint <= 0xdbffL) {
  28027. /* high surrogate */
  28028. duk_codepoint_t prev_lead = enc_ctx->lead;
  28029. enc_ctx->lead = codepoint;
  28030. if (prev_lead == 0x0000L) {
  28031. /* high surrogate, no output */
  28032. return;
  28033. } else {
  28034. /* consecutive high surrogates, consider first one unpaired */
  28035. codepoint = DUK_UNICODE_CP_REPLACEMENT_CHARACTER;
  28036. }
  28037. } else {
  28038. /* low surrogate */
  28039. if (enc_ctx->lead != 0x0000L) {
  28040. codepoint = (duk_codepoint_t) (0x010000L + ((enc_ctx->lead - 0xd800L) << 10) + (codepoint - 0xdc00L));
  28041. enc_ctx->lead = 0x0000L;
  28042. } else {
  28043. /* unpaired low surrogate */
  28044. DUK_ASSERT(enc_ctx->lead == 0x0000L);
  28045. codepoint = DUK_UNICODE_CP_REPLACEMENT_CHARACTER;
  28046. }
  28047. }
  28048. } else {
  28049. if (enc_ctx->lead != 0x0000L) {
  28050. /* unpaired high surrogate: emit replacement character and the input codepoint */
  28051. enc_ctx->lead = 0x0000L;
  28052. enc_ctx->out = duk__utf8_emit_repl(enc_ctx->out);
  28053. }
  28054. }
  28055. /* Codepoint may be original input, a decoded surrogate pair, or may
  28056. * have been replaced with U+FFFD.
  28057. */
  28058. enc_ctx->out += duk_unicode_encode_xutf8((duk_ucodepoint_t) codepoint, enc_ctx->out);
  28059. }
  28060. #endif /* DUK_USE_ENCODING_BUILTINS */
  28061. /* Shared helper for buffer-to-string using a TextDecoder() compatible UTF-8
  28062. * decoder.
  28063. */
  28064. DUK_LOCAL duk_ret_t duk__decode_helper(duk_hthread *thr, duk__decode_context *dec_ctx) {
  28065. const duk_uint8_t *input;
  28066. duk_size_t len = 0;
  28067. duk_size_t len_tmp;
  28068. duk_bool_t stream = 0;
  28069. duk_codepoint_t codepoint;
  28070. duk_uint8_t *output;
  28071. const duk_uint8_t *in;
  28072. duk_uint8_t *out;
  28073. DUK_ASSERT(dec_ctx != NULL);
  28074. /* Careful with input buffer pointer: any side effects involving
  28075. * code execution (e.g. getters, coercion calls, and finalizers)
  28076. * may cause a resize and invalidate a pointer we've read. This
  28077. * is why the pointer is actually looked up at the last minute.
  28078. * Argument validation must still happen first to match WHATWG
  28079. * required side effect order.
  28080. */
  28081. if (duk_is_undefined(thr, 0)) {
  28082. duk_push_fixed_buffer_nozero(thr, 0);
  28083. duk_replace(thr, 0);
  28084. }
  28085. (void) duk_require_buffer_data(thr, 0, &len); /* Need 'len', avoid pointer. */
  28086. if (duk_check_type_mask(thr, 1, DUK_TYPE_MASK_UNDEFINED |
  28087. DUK_TYPE_MASK_NULL |
  28088. DUK_TYPE_MASK_NONE)) {
  28089. /* Use defaults, treat missing value like undefined. */
  28090. } else {
  28091. duk_require_type_mask(thr, 1, DUK_TYPE_MASK_UNDEFINED |
  28092. DUK_TYPE_MASK_NULL |
  28093. DUK_TYPE_MASK_LIGHTFUNC |
  28094. DUK_TYPE_MASK_BUFFER |
  28095. DUK_TYPE_MASK_OBJECT);
  28096. if (duk_get_prop_literal(thr, 1, "stream")) {
  28097. stream = duk_to_boolean(thr, -1);
  28098. }
  28099. }
  28100. /* Allowance is 3*len in the general case because all bytes may potentially
  28101. * become U+FFFD. If the first byte completes a non-BMP codepoint it will
  28102. * decode to a CESU-8 surrogate pair (6 bytes) so we allow 3 extra bytes to
  28103. * compensate: (1*3)+3 = 6. Non-BMP codepoints are safe otherwise because
  28104. * the 4->6 expansion is well under the 3x allowance.
  28105. *
  28106. * XXX: As with TextEncoder, need a better buffer allocation strategy here.
  28107. */
  28108. if (len >= (DUK_HBUFFER_MAX_BYTELEN / 3) - 3) {
  28109. DUK_ERROR_TYPE(thr, DUK_STR_RESULT_TOO_LONG);
  28110. DUK_WO_NORETURN(return 0;);
  28111. }
  28112. output = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, 3 + (3 * len)); /* used parts will be always manually written over */
  28113. input = (const duk_uint8_t *) duk_get_buffer_data(thr, 0, &len_tmp);
  28114. DUK_ASSERT(input != NULL || len == 0);
  28115. if (DUK_UNLIKELY(len != len_tmp)) {
  28116. /* Very unlikely but possible: source buffer was resized by
  28117. * a side effect when fixed buffer was pushed. Output buffer
  28118. * may not be large enough to hold output, so just fail if
  28119. * length has changed.
  28120. */
  28121. DUK_D(DUK_DPRINT("input buffer resized by side effect, fail"));
  28122. goto fail_type;
  28123. }
  28124. /* From this point onwards it's critical that no side effect occur
  28125. * which may disturb 'input': finalizer execution, property accesses,
  28126. * active coercions, etc. Even an allocation related mark-and-sweep
  28127. * may affect the pointer because it may trigger a pending finalizer.
  28128. */
  28129. in = input;
  28130. out = output;
  28131. while (in < input + len) {
  28132. codepoint = duk__utf8_decode_next(dec_ctx, *in++);
  28133. if (codepoint < 0) {
  28134. if (codepoint == DUK__CP_CONTINUE) {
  28135. continue;
  28136. }
  28137. /* Decoding error with or without retry. */
  28138. DUK_ASSERT(codepoint == DUK__CP_ERROR || codepoint == DUK__CP_RETRY);
  28139. if (codepoint == DUK__CP_RETRY) {
  28140. --in; /* retry last byte */
  28141. }
  28142. /* replacement mode: replace with U+FFFD */
  28143. codepoint = DUK_UNICODE_CP_REPLACEMENT_CHARACTER;
  28144. if (dec_ctx->fatal) {
  28145. /* fatal mode: throw a TypeError */
  28146. goto fail_type;
  28147. }
  28148. /* Continue with 'codepoint', Unicode replacement. */
  28149. }
  28150. DUK_ASSERT(codepoint >= 0x0000L && codepoint <= 0x10ffffL);
  28151. if (!dec_ctx->bom_handled) {
  28152. dec_ctx->bom_handled = 1;
  28153. if (codepoint == 0xfeffL && !dec_ctx->ignore_bom) {
  28154. continue;
  28155. }
  28156. }
  28157. out += duk_unicode_encode_cesu8((duk_ucodepoint_t) codepoint, out);
  28158. DUK_ASSERT(out <= output + (3 + (3 * len)));
  28159. }
  28160. if (!stream) {
  28161. if (dec_ctx->needed != 0) {
  28162. /* truncated sequence at end of buffer */
  28163. if (dec_ctx->fatal) {
  28164. goto fail_type;
  28165. } else {
  28166. out += duk_unicode_encode_cesu8(DUK_UNICODE_CP_REPLACEMENT_CHARACTER, out);
  28167. DUK_ASSERT(out <= output + (3 + (3 * len)));
  28168. }
  28169. }
  28170. duk__utf8_decode_init(dec_ctx); /* Initialize decoding state for potential reuse. */
  28171. }
  28172. /* Output buffer is fixed and thus stable even if there had been
  28173. * side effects (which there shouldn't be).
  28174. */
  28175. duk_push_lstring(thr, (const char *) output, (duk_size_t) (out - output));
  28176. return 1;
  28177. fail_type:
  28178. DUK_ERROR_TYPE(thr, DUK_STR_UTF8_DECODE_FAILED);
  28179. DUK_WO_NORETURN(return 0;);
  28180. }
  28181. /*
  28182. * Built-in bindings
  28183. */
  28184. #if defined(DUK_USE_ENCODING_BUILTINS)
  28185. DUK_INTERNAL duk_ret_t duk_bi_textencoder_constructor(duk_hthread *thr) {
  28186. /* TextEncoder currently requires no persistent state, so the constructor
  28187. * does nothing on purpose.
  28188. */
  28189. duk_require_constructor_call(thr);
  28190. return 0;
  28191. }
  28192. DUK_INTERNAL duk_ret_t duk_bi_textencoder_prototype_encoding_getter(duk_hthread *thr) {
  28193. duk_push_literal(thr, "utf-8");
  28194. return 1;
  28195. }
  28196. DUK_INTERNAL duk_ret_t duk_bi_textencoder_prototype_encode(duk_hthread *thr) {
  28197. duk__encode_context enc_ctx;
  28198. duk_size_t len;
  28199. duk_size_t final_len;
  28200. duk_uint8_t *output;
  28201. DUK_ASSERT_TOP(thr, 1);
  28202. if (duk_is_undefined(thr, 0)) {
  28203. len = 0;
  28204. } else {
  28205. duk_hstring *h_input;
  28206. h_input = duk_to_hstring(thr, 0);
  28207. DUK_ASSERT(h_input != NULL);
  28208. len = (duk_size_t) DUK_HSTRING_GET_CHARLEN(h_input);
  28209. if (len >= DUK_HBUFFER_MAX_BYTELEN / 3) {
  28210. DUK_ERROR_TYPE(thr, DUK_STR_RESULT_TOO_LONG);
  28211. DUK_WO_NORETURN(return 0;);
  28212. }
  28213. }
  28214. /* Allowance is 3*len because all bytes can potentially be replaced with
  28215. * U+FFFD -- which rather inconveniently encodes to 3 bytes in UTF-8.
  28216. * Rely on dynamic buffer data pointer stability: no other code has
  28217. * access to the data pointer.
  28218. *
  28219. * XXX: The buffer allocation strategy used here is rather inefficient.
  28220. * Maybe switch to a chunk-based strategy, or preprocess the string to
  28221. * figure out the space needed ahead of time?
  28222. */
  28223. DUK_ASSERT(3 * len >= len);
  28224. output = (duk_uint8_t *) duk_push_dynamic_buffer(thr, 3 * len);
  28225. if (len > 0) {
  28226. DUK_ASSERT(duk_is_string(thr, 0)); /* True if len > 0. */
  28227. /* XXX: duk_decode_string() is used to process the input
  28228. * string. For standard ECMAScript strings, represented
  28229. * internally as CESU-8, this is fine. However, behavior
  28230. * beyond CESU-8 is not very strict: codepoints using an
  28231. * extended form of UTF-8 are also accepted, and invalid
  28232. * codepoint sequences (which are allowed in Duktape strings)
  28233. * are not handled as well as they could (e.g. invalid
  28234. * continuation bytes may mask following codepoints).
  28235. * This is how ECMAScript code would also see such strings.
  28236. * Maybe replace duk_decode_string() with an explicit strict
  28237. * CESU-8 decoder here?
  28238. */
  28239. enc_ctx.lead = 0x0000L;
  28240. enc_ctx.out = output;
  28241. duk_decode_string(thr, 0, duk__utf8_encode_char, (void *) &enc_ctx);
  28242. if (enc_ctx.lead != 0x0000L) {
  28243. /* unpaired high surrogate at end of string */
  28244. enc_ctx.out = duk__utf8_emit_repl(enc_ctx.out);
  28245. DUK_ASSERT(enc_ctx.out <= output + (3 * len));
  28246. }
  28247. /* The output buffer is usually very much oversized, so shrink it to
  28248. * actually needed size. Pointer stability assumed up to this point.
  28249. */
  28250. DUK_ASSERT_TOP(thr, 2);
  28251. DUK_ASSERT(output == (duk_uint8_t *) duk_get_buffer_data(thr, -1, NULL));
  28252. final_len = (duk_size_t) (enc_ctx.out - output);
  28253. duk_resize_buffer(thr, -1, final_len);
  28254. /* 'output' and 'enc_ctx.out' are potentially invalidated by the resize. */
  28255. } else {
  28256. final_len = 0;
  28257. }
  28258. /* Standard WHATWG output is a Uint8Array. Here the Uint8Array will
  28259. * be backed by a dynamic buffer which differs from e.g. Uint8Arrays
  28260. * created as 'new Uint8Array(N)'. ECMAScript code won't see the
  28261. * difference but C code will. When bufferobjects are not supported,
  28262. * returns a plain dynamic buffer.
  28263. */
  28264. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  28265. duk_push_buffer_object(thr, -1, 0, final_len, DUK_BUFOBJ_UINT8ARRAY);
  28266. #endif
  28267. return 1;
  28268. }
  28269. DUK_INTERNAL duk_ret_t duk_bi_textdecoder_constructor(duk_hthread *thr) {
  28270. duk__decode_context *dec_ctx;
  28271. duk_bool_t fatal = 0;
  28272. duk_bool_t ignore_bom = 0;
  28273. DUK_ASSERT_TOP(thr, 2);
  28274. duk_require_constructor_call(thr);
  28275. if (!duk_is_undefined(thr, 0)) {
  28276. /* XXX: For now ignore 'label' (encoding identifier). */
  28277. duk_to_string(thr, 0);
  28278. }
  28279. if (!duk_is_null_or_undefined(thr, 1)) {
  28280. if (duk_get_prop_literal(thr, 1, "fatal")) {
  28281. fatal = duk_to_boolean(thr, -1);
  28282. }
  28283. if (duk_get_prop_literal(thr, 1, "ignoreBOM")) {
  28284. ignore_bom = duk_to_boolean(thr, -1);
  28285. }
  28286. }
  28287. duk_push_this(thr);
  28288. /* The decode context is not assumed to be zeroed; all fields are
  28289. * initialized explicitly.
  28290. */
  28291. dec_ctx = (duk__decode_context *) duk_push_fixed_buffer(thr, sizeof(duk__decode_context));
  28292. dec_ctx->fatal = (duk_uint8_t) fatal;
  28293. dec_ctx->ignore_bom = (duk_uint8_t) ignore_bom;
  28294. duk__utf8_decode_init(dec_ctx); /* Initializes remaining fields. */
  28295. duk_put_prop_literal(thr, -2, DUK_INTERNAL_SYMBOL("Context"));
  28296. return 0;
  28297. }
  28298. /* Get TextDecoder context from 'this'; leaves garbage on stack. */
  28299. DUK_LOCAL duk__decode_context *duk__get_textdecoder_context(duk_hthread *thr) {
  28300. duk__decode_context *dec_ctx;
  28301. duk_push_this(thr);
  28302. duk_get_prop_literal(thr, -1, DUK_INTERNAL_SYMBOL("Context"));
  28303. dec_ctx = (duk__decode_context *) duk_require_buffer(thr, -1, NULL);
  28304. DUK_ASSERT(dec_ctx != NULL);
  28305. return dec_ctx;
  28306. }
  28307. DUK_INTERNAL duk_ret_t duk_bi_textdecoder_prototype_shared_getter(duk_hthread *thr) {
  28308. duk__decode_context *dec_ctx;
  28309. duk_int_t magic;
  28310. dec_ctx = duk__get_textdecoder_context(thr);
  28311. magic = duk_get_current_magic(thr);
  28312. switch (magic) {
  28313. case 0:
  28314. /* Encoding is now fixed, so _Context lookup is only needed to
  28315. * validate the 'this' binding (TypeError if not TextDecoder-like).
  28316. */
  28317. duk_push_literal(thr, "utf-8");
  28318. break;
  28319. case 1:
  28320. duk_push_boolean(thr, dec_ctx->fatal);
  28321. break;
  28322. default:
  28323. duk_push_boolean(thr, dec_ctx->ignore_bom);
  28324. break;
  28325. }
  28326. return 1;
  28327. }
  28328. DUK_INTERNAL duk_ret_t duk_bi_textdecoder_prototype_decode(duk_hthread *thr) {
  28329. duk__decode_context *dec_ctx;
  28330. dec_ctx = duk__get_textdecoder_context(thr);
  28331. return duk__decode_helper(thr, dec_ctx);
  28332. }
  28333. #endif /* DUK_USE_ENCODING_BUILTINS */
  28334. /*
  28335. * Internal helper for Node.js Buffer
  28336. */
  28337. /* Internal helper used for Node.js Buffer .toString(). Value stack convention
  28338. * is currently odd: it mimics TextDecoder .decode() so that argument must be at
  28339. * index 0, and decode options (not present for Buffer) at index 1. Return value
  28340. * is a Duktape/C function return value.
  28341. */
  28342. DUK_INTERNAL duk_ret_t duk_textdecoder_decode_utf8_nodejs(duk_hthread *thr) {
  28343. duk__decode_context dec_ctx;
  28344. dec_ctx.fatal = 0; /* use replacement chars */
  28345. dec_ctx.ignore_bom = 1; /* ignore BOMs (matches Node.js Buffer .toString()) */
  28346. duk__utf8_decode_init(&dec_ctx);
  28347. return duk__decode_helper(thr, &dec_ctx);
  28348. }
  28349. /* automatic undefs */
  28350. #undef DUK__CP_CONTINUE
  28351. #undef DUK__CP_ERROR
  28352. #undef DUK__CP_RETRY
  28353. #line 1 "duk_bi_error.c"
  28354. /*
  28355. * Error built-ins
  28356. */
  28357. /* #include duk_internal.h -> already included */
  28358. DUK_INTERNAL duk_ret_t duk_bi_error_constructor_shared(duk_hthread *thr) {
  28359. /* Behavior for constructor and non-constructor call is
  28360. * the same except for augmenting the created error. When
  28361. * called as a constructor, the caller (duk_new()) will handle
  28362. * augmentation; when called as normal function, we need to do
  28363. * it here.
  28364. */
  28365. duk_small_int_t bidx_prototype = duk_get_current_magic(thr);
  28366. /* same for both error and each subclass like TypeError */
  28367. duk_uint_t flags_and_class = DUK_HOBJECT_FLAG_EXTENSIBLE |
  28368. DUK_HOBJECT_FLAG_FASTREFS |
  28369. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ERROR);
  28370. (void) duk_push_object_helper(thr, flags_and_class, bidx_prototype);
  28371. /* If message is undefined, the own property 'message' is not set at
  28372. * all to save property space. An empty message is inherited anyway.
  28373. */
  28374. if (!duk_is_undefined(thr, 0)) {
  28375. duk_to_string(thr, 0);
  28376. duk_dup_0(thr); /* [ message error message ] */
  28377. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE, DUK_PROPDESC_FLAGS_WC);
  28378. }
  28379. /* Augment the error if called as a normal function. __FILE__ and __LINE__
  28380. * are not desirable in this case.
  28381. */
  28382. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  28383. if (!duk_is_constructor_call(thr)) {
  28384. duk_err_augment_error_create(thr, thr, NULL, 0, DUK_AUGMENT_FLAG_NOBLAME_FILELINE);
  28385. }
  28386. #endif
  28387. return 1;
  28388. }
  28389. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_to_string(duk_hthread *thr) {
  28390. /* XXX: optimize with more direct internal access */
  28391. duk_push_this(thr);
  28392. (void) duk_require_hobject_promote_mask(thr, -1, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  28393. /* [ ... this ] */
  28394. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_NAME);
  28395. if (duk_is_undefined(thr, -1)) {
  28396. duk_pop(thr);
  28397. duk_push_literal(thr, "Error");
  28398. } else {
  28399. duk_to_string(thr, -1);
  28400. }
  28401. /* [ ... this name ] */
  28402. /* XXX: Are steps 6 and 7 in E5 Section 15.11.4.4 duplicated by
  28403. * accident or are they actually needed? The first ToString()
  28404. * could conceivably return 'undefined'.
  28405. */
  28406. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE);
  28407. if (duk_is_undefined(thr, -1)) {
  28408. duk_pop(thr);
  28409. duk_push_hstring_empty(thr);
  28410. } else {
  28411. duk_to_string(thr, -1);
  28412. }
  28413. /* [ ... this name message ] */
  28414. if (duk_get_length(thr, -2) == 0) {
  28415. /* name is empty -> return message */
  28416. return 1;
  28417. }
  28418. if (duk_get_length(thr, -1) == 0) {
  28419. /* message is empty -> return name */
  28420. duk_pop(thr);
  28421. return 1;
  28422. }
  28423. duk_push_literal(thr, ": ");
  28424. duk_insert(thr, -2); /* ... name ': ' message */
  28425. duk_concat(thr, 3);
  28426. return 1;
  28427. }
  28428. #if defined(DUK_USE_TRACEBACKS)
  28429. /*
  28430. * Traceback handling
  28431. *
  28432. * The unified helper decodes the traceback and produces various requested
  28433. * outputs. It should be optimized for size, and may leave garbage on stack,
  28434. * only the topmost return value matters. For instance, traceback separator
  28435. * and decoded strings are pushed even when looking for filename only.
  28436. *
  28437. * NOTE: although _Tracedata is an internal property, user code can currently
  28438. * write to the array (or replace it with something other than an array).
  28439. * The code below must tolerate arbitrary _Tracedata. It can throw errors
  28440. * etc, but cannot cause a segfault or memory unsafe behavior.
  28441. */
  28442. /* constants arbitrary, chosen for small loads */
  28443. #define DUK__OUTPUT_TYPE_TRACEBACK (-1)
  28444. #define DUK__OUTPUT_TYPE_FILENAME 0
  28445. #define DUK__OUTPUT_TYPE_LINENUMBER 1
  28446. DUK_LOCAL duk_ret_t duk__error_getter_helper(duk_hthread *thr, duk_small_int_t output_type) {
  28447. duk_idx_t idx_td;
  28448. duk_small_int_t i; /* traceback depth fits into 16 bits */
  28449. duk_small_int_t t; /* stack type fits into 16 bits */
  28450. duk_small_int_t count_func = 0; /* traceback depth ensures fits into 16 bits */
  28451. const char *str_tailcall = " tailcall";
  28452. const char *str_strict = " strict";
  28453. const char *str_construct = " construct";
  28454. const char *str_prevyield = " preventsyield";
  28455. const char *str_directeval = " directeval";
  28456. const char *str_empty = "";
  28457. DUK_ASSERT_TOP(thr, 0); /* fixed arg count */
  28458. duk_push_this(thr);
  28459. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_TRACEDATA);
  28460. idx_td = duk_get_top_index(thr);
  28461. duk_push_hstring_stridx(thr, DUK_STRIDX_NEWLINE_4SPACE);
  28462. duk_push_this(thr);
  28463. /* [ ... this tracedata sep this ] */
  28464. /* XXX: skip null filename? */
  28465. if (duk_check_type(thr, idx_td, DUK_TYPE_OBJECT)) {
  28466. /* Current tracedata contains 2 entries per callstack entry. */
  28467. for (i = 0; ; i += 2) {
  28468. duk_int_t pc;
  28469. duk_uint_t line;
  28470. duk_uint_t flags;
  28471. duk_double_t d;
  28472. const char *funcname;
  28473. const char *filename;
  28474. duk_hobject *h_func;
  28475. duk_hstring *h_name;
  28476. duk_require_stack(thr, 5);
  28477. duk_get_prop_index(thr, idx_td, (duk_uarridx_t) i);
  28478. duk_get_prop_index(thr, idx_td, (duk_uarridx_t) (i + 1));
  28479. d = duk_to_number_m1(thr);
  28480. pc = duk_double_to_int_t(DUK_FMOD(d, DUK_DOUBLE_2TO32));
  28481. flags = duk_double_to_uint_t(DUK_FLOOR(d / DUK_DOUBLE_2TO32));
  28482. t = (duk_small_int_t) duk_get_type(thr, -2);
  28483. if (t == DUK_TYPE_OBJECT || t == DUK_TYPE_LIGHTFUNC) {
  28484. /*
  28485. * ECMAScript/native function call or lightfunc call
  28486. */
  28487. count_func++;
  28488. /* [ ... v1(func) v2(pc+flags) ] */
  28489. /* These may be systematically omitted by Duktape
  28490. * with certain config options, but allow user to
  28491. * set them on a case-by-case basis.
  28492. */
  28493. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_NAME);
  28494. duk_get_prop_stridx_short(thr, -3, DUK_STRIDX_FILE_NAME);
  28495. #if defined(DUK_USE_PC2LINE)
  28496. line = (duk_uint_t) duk_hobject_pc2line_query(thr, -4, (duk_uint_fast32_t) pc);
  28497. #else
  28498. line = 0;
  28499. #endif
  28500. /* [ ... v1 v2 name filename ] */
  28501. /* When looking for .fileName/.lineNumber, blame first
  28502. * function which has a .fileName.
  28503. */
  28504. if (duk_is_string_notsymbol(thr, -1)) {
  28505. if (output_type == DUK__OUTPUT_TYPE_FILENAME) {
  28506. return 1;
  28507. } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) {
  28508. duk_push_uint(thr, line);
  28509. return 1;
  28510. }
  28511. }
  28512. /* XXX: Change 'anon' handling here too, to use empty string for anonymous functions? */
  28513. /* XXX: Could be improved by coercing to a readable duk_tval (especially string escaping) */
  28514. h_name = duk_get_hstring_notsymbol(thr, -2); /* may be NULL */
  28515. funcname = (h_name == NULL || h_name == DUK_HTHREAD_STRING_EMPTY_STRING(thr)) ?
  28516. "[anon]" : (const char *) DUK_HSTRING_GET_DATA(h_name);
  28517. filename = duk_get_string_notsymbol(thr, -1);
  28518. filename = filename ? filename : "";
  28519. DUK_ASSERT(funcname != NULL);
  28520. DUK_ASSERT(filename != NULL);
  28521. h_func = duk_get_hobject(thr, -4); /* NULL for lightfunc */
  28522. if (h_func == NULL) {
  28523. duk_push_sprintf(thr, "at %s light%s%s%s%s%s",
  28524. (const char *) funcname,
  28525. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  28526. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty),
  28527. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  28528. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  28529. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  28530. } else if (DUK_HOBJECT_HAS_NATFUNC(h_func)) {
  28531. duk_push_sprintf(thr, "at %s (%s) native%s%s%s%s%s",
  28532. (const char *) funcname,
  28533. (const char *) filename,
  28534. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  28535. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty),
  28536. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  28537. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  28538. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  28539. } else {
  28540. duk_push_sprintf(thr, "at %s (%s:%lu)%s%s%s%s%s",
  28541. (const char *) funcname,
  28542. (const char *) filename,
  28543. (unsigned long) line,
  28544. (const char *) ((flags & DUK_ACT_FLAG_STRICT) ? str_strict : str_empty),
  28545. (const char *) ((flags & DUK_ACT_FLAG_TAILCALLED) ? str_tailcall : str_empty),
  28546. (const char *) ((flags & DUK_ACT_FLAG_CONSTRUCT) ? str_construct : str_empty),
  28547. (const char *) ((flags & DUK_ACT_FLAG_DIRECT_EVAL) ? str_directeval : str_empty),
  28548. (const char *) ((flags & DUK_ACT_FLAG_PREVENT_YIELD) ? str_prevyield : str_empty));
  28549. }
  28550. duk_replace(thr, -5); /* [ ... v1 v2 name filename str ] -> [ ... str v2 name filename ] */
  28551. duk_pop_3(thr); /* -> [ ... str ] */
  28552. } else if (t == DUK_TYPE_STRING) {
  28553. const char *str_file;
  28554. /*
  28555. * __FILE__ / __LINE__ entry, here 'pc' is line number directly.
  28556. * Sometimes __FILE__ / __LINE__ is reported as the source for
  28557. * the error (fileName, lineNumber), sometimes not.
  28558. */
  28559. /* [ ... v1(filename) v2(line+flags) ] */
  28560. /* When looking for .fileName/.lineNumber, blame compilation
  28561. * or C call site unless flagged not to do so.
  28562. */
  28563. if (!(flags & DUK_TB_FLAG_NOBLAME_FILELINE)) {
  28564. if (output_type == DUK__OUTPUT_TYPE_FILENAME) {
  28565. duk_pop(thr);
  28566. return 1;
  28567. } else if (output_type == DUK__OUTPUT_TYPE_LINENUMBER) {
  28568. duk_push_int(thr, pc);
  28569. return 1;
  28570. }
  28571. }
  28572. /* Tracedata is trusted but avoid any risk of using a NULL
  28573. * for %s format because it has undefined behavior. Symbols
  28574. * don't need to be explicitly rejected as they pose no memory
  28575. * safety issues.
  28576. */
  28577. str_file = (const char *) duk_get_string(thr, -2);
  28578. duk_push_sprintf(thr, "at [anon] (%s:%ld) internal",
  28579. (const char *) (str_file ? str_file : "null"), (long) pc);
  28580. duk_replace(thr, -3); /* [ ... v1 v2 str ] -> [ ... str v2 ] */
  28581. duk_pop(thr); /* -> [ ... str ] */
  28582. } else {
  28583. /* unknown, ignore */
  28584. duk_pop_2(thr);
  28585. break;
  28586. }
  28587. }
  28588. if (count_func >= DUK_USE_TRACEBACK_DEPTH) {
  28589. /* Possibly truncated; there is no explicit truncation
  28590. * marker so this is the best we can do.
  28591. */
  28592. duk_push_hstring_stridx(thr, DUK_STRIDX_BRACKETED_ELLIPSIS);
  28593. }
  28594. }
  28595. /* [ ... this tracedata sep this str1 ... strN ] */
  28596. if (output_type != DUK__OUTPUT_TYPE_TRACEBACK) {
  28597. return 0;
  28598. } else {
  28599. /* The 'this' after 'sep' will get ToString() coerced by
  28600. * duk_join() automatically. We don't want to do that
  28601. * coercion when providing .fileName or .lineNumber (GH-254).
  28602. */
  28603. duk_join(thr, duk_get_top(thr) - (idx_td + 2) /*count, not including sep*/);
  28604. return 1;
  28605. }
  28606. }
  28607. /* XXX: Output type could be encoded into native function 'magic' value to
  28608. * save space. For setters the stridx could be encoded into 'magic'.
  28609. */
  28610. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_hthread *thr) {
  28611. return duk__error_getter_helper(thr, DUK__OUTPUT_TYPE_TRACEBACK);
  28612. }
  28613. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_hthread *thr) {
  28614. return duk__error_getter_helper(thr, DUK__OUTPUT_TYPE_FILENAME);
  28615. }
  28616. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_hthread *thr) {
  28617. return duk__error_getter_helper(thr, DUK__OUTPUT_TYPE_LINENUMBER);
  28618. }
  28619. #else /* DUK_USE_TRACEBACKS */
  28620. /*
  28621. * Traceback handling when tracebacks disabled.
  28622. *
  28623. * The fileName / lineNumber stubs are now necessary because built-in
  28624. * data will include the accessor properties in Error.prototype. If those
  28625. * are removed for builds without tracebacks, these can also be removed.
  28626. * 'stack' should still be present and produce a ToString() equivalent:
  28627. * this is useful for user code which prints a stacktrace and expects to
  28628. * see something useful. A normal stacktrace also begins with a ToString()
  28629. * of the error so this makes sense.
  28630. */
  28631. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_getter(duk_hthread *thr) {
  28632. /* XXX: remove this native function and map 'stack' accessor
  28633. * to the toString() implementation directly.
  28634. */
  28635. return duk_bi_error_prototype_to_string(thr);
  28636. }
  28637. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_getter(duk_hthread *thr) {
  28638. DUK_UNREF(thr);
  28639. return 0;
  28640. }
  28641. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_getter(duk_hthread *thr) {
  28642. DUK_UNREF(thr);
  28643. return 0;
  28644. }
  28645. #endif /* DUK_USE_TRACEBACKS */
  28646. DUK_LOCAL duk_ret_t duk__error_setter_helper(duk_hthread *thr, duk_small_uint_t stridx_key) {
  28647. /* Attempt to write 'stack', 'fileName', 'lineNumber' works as if
  28648. * user code called Object.defineProperty() to create an overriding
  28649. * own property. This allows user code to overwrite .fileName etc
  28650. * intuitively as e.g. "err.fileName = 'dummy'" as one might expect.
  28651. * See https://github.com/svaarala/duktape/issues/387.
  28652. */
  28653. DUK_ASSERT_TOP(thr, 1); /* fixed arg count: value */
  28654. duk_push_this(thr);
  28655. duk_push_hstring_stridx(thr, stridx_key);
  28656. duk_dup_0(thr);
  28657. /* [ ... obj key value ] */
  28658. DUK_DD(DUK_DDPRINT("error setter: %!T %!T %!T",
  28659. duk_get_tval(thr, -3), duk_get_tval(thr, -2), duk_get_tval(thr, -1)));
  28660. duk_def_prop(thr, -3, DUK_DEFPROP_HAVE_VALUE |
  28661. DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE |
  28662. DUK_DEFPROP_HAVE_ENUMERABLE | /*not enumerable*/
  28663. DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE);
  28664. return 0;
  28665. }
  28666. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_stack_setter(duk_hthread *thr) {
  28667. return duk__error_setter_helper(thr, DUK_STRIDX_STACK);
  28668. }
  28669. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_filename_setter(duk_hthread *thr) {
  28670. return duk__error_setter_helper(thr, DUK_STRIDX_FILE_NAME);
  28671. }
  28672. DUK_INTERNAL duk_ret_t duk_bi_error_prototype_linenumber_setter(duk_hthread *thr) {
  28673. return duk__error_setter_helper(thr, DUK_STRIDX_LINE_NUMBER);
  28674. }
  28675. /* automatic undefs */
  28676. #undef DUK__OUTPUT_TYPE_FILENAME
  28677. #undef DUK__OUTPUT_TYPE_LINENUMBER
  28678. #undef DUK__OUTPUT_TYPE_TRACEBACK
  28679. #line 1 "duk_bi_function.c"
  28680. /*
  28681. * Function built-ins
  28682. */
  28683. /* #include duk_internal.h -> already included */
  28684. /* Needed even when Function built-in is disabled. */
  28685. DUK_INTERNAL duk_ret_t duk_bi_function_prototype(duk_hthread *thr) {
  28686. /* ignore arguments, return undefined (E5 Section 15.3.4) */
  28687. DUK_UNREF(thr);
  28688. return 0;
  28689. }
  28690. #if defined(DUK_USE_FUNCTION_BUILTIN)
  28691. DUK_INTERNAL duk_ret_t duk_bi_function_constructor(duk_hthread *thr) {
  28692. duk_hstring *h_sourcecode;
  28693. duk_idx_t nargs;
  28694. duk_idx_t i;
  28695. duk_small_uint_t comp_flags;
  28696. duk_hcompfunc *func;
  28697. duk_hobject *outer_lex_env;
  28698. duk_hobject *outer_var_env;
  28699. /* normal and constructor calls have identical semantics */
  28700. nargs = duk_get_top(thr);
  28701. for (i = 0; i < nargs; i++) {
  28702. duk_to_string(thr, i); /* Rejects Symbols during coercion. */
  28703. }
  28704. if (nargs == 0) {
  28705. duk_push_hstring_empty(thr);
  28706. duk_push_hstring_empty(thr);
  28707. } else if (nargs == 1) {
  28708. /* XXX: cover this with the generic >1 case? */
  28709. duk_push_hstring_empty(thr);
  28710. } else {
  28711. duk_insert(thr, 0); /* [ arg1 ... argN-1 body] -> [body arg1 ... argN-1] */
  28712. duk_push_literal(thr, ",");
  28713. duk_insert(thr, 1);
  28714. duk_join(thr, nargs - 1);
  28715. }
  28716. /* [ body formals ], formals is comma separated list that needs to be parsed */
  28717. DUK_ASSERT_TOP(thr, 2);
  28718. /* XXX: this placeholder is not always correct, but use for now.
  28719. * It will fail in corner cases; see test-dev-func-cons-args.js.
  28720. */
  28721. duk_push_literal(thr, "function(");
  28722. duk_dup_1(thr);
  28723. duk_push_literal(thr, "){");
  28724. duk_dup_0(thr);
  28725. duk_push_literal(thr, "\n}"); /* Newline is important to handle trailing // comment. */
  28726. duk_concat(thr, 5);
  28727. /* [ body formals source ] */
  28728. DUK_ASSERT_TOP(thr, 3);
  28729. /* strictness is not inherited, intentional */
  28730. comp_flags = DUK_COMPILE_FUNCEXPR;
  28731. duk_push_hstring_stridx(thr, DUK_STRIDX_COMPILE); /* XXX: copy from caller? */ /* XXX: ignored now */
  28732. h_sourcecode = duk_require_hstring(thr, -2); /* no symbol check needed; -2 is concat'd code */
  28733. duk_js_compile(thr,
  28734. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_sourcecode),
  28735. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sourcecode),
  28736. comp_flags);
  28737. /* Force .name to 'anonymous' (ES2015). */
  28738. duk_push_literal(thr, "anonymous");
  28739. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C);
  28740. func = (duk_hcompfunc *) duk_known_hobject(thr, -1);
  28741. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) func));
  28742. DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE((duk_hobject *) func));
  28743. /* [ body formals source template ] */
  28744. /* only outer_lex_env matters, as functions always get a new
  28745. * variable declaration environment.
  28746. */
  28747. outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  28748. outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  28749. duk_js_push_closure(thr, func, outer_var_env, outer_lex_env, 1 /*add_auto_proto*/);
  28750. /* [ body formals source template closure ] */
  28751. return 1;
  28752. }
  28753. #endif /* DUK_USE_FUNCTION_BUILTIN */
  28754. #if defined(DUK_USE_FUNCTION_BUILTIN)
  28755. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_to_string(duk_hthread *thr) {
  28756. duk_tval *tv;
  28757. /*
  28758. * E5 Section 15.3.4.2 places few requirements on the output of
  28759. * this function: the result is implementation dependent, must
  28760. * follow FunctionDeclaration syntax (in particular, must have a
  28761. * name even for anonymous functions or functions with empty name).
  28762. * The output does NOT need to compile into anything useful.
  28763. *
  28764. * E6 Section 19.2.3.5 changes the requirements completely: the
  28765. * result must either eval() to a functionally equivalent object
  28766. * OR eval() to a SyntaxError.
  28767. *
  28768. * We opt for the SyntaxError approach for now, with a syntax that
  28769. * mimics V8's native function syntax:
  28770. *
  28771. * 'function cos() { [native code] }'
  28772. *
  28773. * but extended with [ecmascript code], [bound code], and
  28774. * [lightfunc code].
  28775. */
  28776. duk_push_this(thr);
  28777. tv = DUK_GET_TVAL_NEGIDX(thr, -1);
  28778. DUK_ASSERT(tv != NULL);
  28779. if (DUK_TVAL_IS_OBJECT(tv)) {
  28780. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv);
  28781. const char *func_name;
  28782. /* Function name: missing/undefined is mapped to empty string,
  28783. * otherwise coerce to string. No handling for invalid identifier
  28784. * characters or e.g. '{' in the function name. This doesn't
  28785. * really matter as long as a SyntaxError results. Technically
  28786. * if the name contained a suitable prefix followed by '//' it
  28787. * might cause the result to parse without error.
  28788. */
  28789. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_NAME);
  28790. if (duk_is_undefined(thr, -1)) {
  28791. func_name = "";
  28792. } else {
  28793. func_name = duk_to_string(thr, -1);
  28794. DUK_ASSERT(func_name != NULL);
  28795. }
  28796. if (DUK_HOBJECT_IS_COMPFUNC(obj)) {
  28797. duk_push_sprintf(thr, "function %s() { [ecmascript code] }", (const char *) func_name);
  28798. } else if (DUK_HOBJECT_IS_NATFUNC(obj)) {
  28799. duk_push_sprintf(thr, "function %s() { [native code] }", (const char *) func_name);
  28800. } else if (DUK_HOBJECT_IS_BOUNDFUNC(obj)) {
  28801. duk_push_sprintf(thr, "function %s() { [bound code] }", (const char *) func_name);
  28802. } else {
  28803. goto type_error;
  28804. }
  28805. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  28806. duk_push_lightfunc_tostring(thr, tv);
  28807. } else {
  28808. goto type_error;
  28809. }
  28810. return 1;
  28811. type_error:
  28812. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  28813. }
  28814. #endif
  28815. /* Always present because the native function pointer is needed in call
  28816. * handling.
  28817. */
  28818. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_call(duk_hthread *thr) {
  28819. /* .call() is dealt with in call handling by simulating its
  28820. * effects so this function is actually never called.
  28821. */
  28822. DUK_UNREF(thr);
  28823. return DUK_RET_TYPE_ERROR;
  28824. }
  28825. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_apply(duk_hthread *thr) {
  28826. /* Like .call(), never actually called. */
  28827. DUK_UNREF(thr);
  28828. return DUK_RET_TYPE_ERROR;
  28829. }
  28830. DUK_INTERNAL duk_ret_t duk_bi_reflect_apply(duk_hthread *thr) {
  28831. /* Like .call(), never actually called. */
  28832. DUK_UNREF(thr);
  28833. return DUK_RET_TYPE_ERROR;
  28834. }
  28835. DUK_INTERNAL duk_ret_t duk_bi_reflect_construct(duk_hthread *thr) {
  28836. /* Like .call(), never actually called. */
  28837. DUK_UNREF(thr);
  28838. return DUK_RET_TYPE_ERROR;
  28839. }
  28840. #if defined(DUK_USE_FUNCTION_BUILTIN)
  28841. /* Create a bound function which points to a target function which may
  28842. * be bound or non-bound. If the target is bound, the argument lists
  28843. * and 'this' binding of the functions are merged and the resulting
  28844. * function points directly to the non-bound target.
  28845. */
  28846. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_bind(duk_hthread *thr) {
  28847. duk_hboundfunc *h_bound;
  28848. duk_idx_t nargs; /* bound args, not counting 'this' binding */
  28849. duk_idx_t bound_nargs;
  28850. duk_int_t bound_len;
  28851. duk_tval *tv_prevbound;
  28852. duk_idx_t n_prevbound;
  28853. duk_tval *tv_res;
  28854. duk_tval *tv_tmp;
  28855. /* XXX: C API call, e.g. duk_push_bound_function(thr, target_idx, nargs); */
  28856. /* Vararg function, careful arg handling, e.g. thisArg may not
  28857. * be present.
  28858. */
  28859. nargs = duk_get_top(thr) - 1; /* actual args, not counting 'this' binding */
  28860. if (nargs < 0) {
  28861. nargs++;
  28862. duk_push_undefined(thr);
  28863. }
  28864. DUK_ASSERT(nargs >= 0);
  28865. /* Limit 'nargs' for bound functions to guarantee arithmetic
  28866. * below will never wrap.
  28867. */
  28868. if (nargs > (duk_idx_t) DUK_HBOUNDFUNC_MAX_ARGS) {
  28869. DUK_DCERROR_RANGE_INVALID_COUNT(thr);
  28870. }
  28871. duk_push_this(thr);
  28872. duk_require_callable(thr, -1);
  28873. /* [ thisArg arg1 ... argN func ] (thisArg+args == nargs+1 total) */
  28874. DUK_ASSERT_TOP(thr, nargs + 2);
  28875. /* Create bound function object. */
  28876. h_bound = duk_push_hboundfunc(thr);
  28877. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&h_bound->target));
  28878. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&h_bound->this_binding));
  28879. DUK_ASSERT(h_bound->args == NULL);
  28880. DUK_ASSERT(h_bound->nargs == 0);
  28881. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_bound) == NULL);
  28882. /* [ thisArg arg1 ... argN func boundFunc ] */
  28883. /* If the target is a bound function, argument lists must be
  28884. * merged. The 'this' binding closest to the target function
  28885. * wins because in call handling the 'this' gets replaced over
  28886. * and over again until we call the non-bound function.
  28887. */
  28888. tv_prevbound = NULL;
  28889. n_prevbound = 0;
  28890. tv_tmp = DUK_GET_TVAL_POSIDX(thr, 0);
  28891. DUK_TVAL_SET_TVAL(&h_bound->this_binding, tv_tmp);
  28892. tv_tmp = DUK_GET_TVAL_NEGIDX(thr, -2);
  28893. DUK_TVAL_SET_TVAL(&h_bound->target, tv_tmp);
  28894. if (DUK_TVAL_IS_OBJECT(tv_tmp)) {
  28895. duk_hobject *h_target;
  28896. duk_hobject *bound_proto;
  28897. h_target = DUK_TVAL_GET_OBJECT(tv_tmp);
  28898. DUK_ASSERT(DUK_HOBJECT_IS_CALLABLE(h_target));
  28899. /* Internal prototype must be copied from the target.
  28900. * For lightfuncs Function.prototype is used and is already
  28901. * in place.
  28902. */
  28903. bound_proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_target);
  28904. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) h_bound, bound_proto);
  28905. /* The 'strict' flag is copied to get the special [[Get]] of E5.1
  28906. * Section 15.3.5.4 to apply when a 'caller' value is a strict bound
  28907. * function. Not sure if this is correct, because the specification
  28908. * is a bit ambiguous on this point but it would make sense.
  28909. */
  28910. /* Strictness is inherited from target. */
  28911. if (DUK_HOBJECT_HAS_STRICT(h_target)) {
  28912. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_bound);
  28913. }
  28914. if (DUK_HOBJECT_HAS_BOUNDFUNC(h_target)) {
  28915. duk_hboundfunc *h_boundtarget;
  28916. h_boundtarget = (duk_hboundfunc *) (void *) h_target;
  28917. /* The final function should always be non-bound, unless
  28918. * there's a bug in the internals. Assert for it.
  28919. */
  28920. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(&h_boundtarget->target) ||
  28921. (DUK_TVAL_IS_OBJECT(&h_boundtarget->target) &&
  28922. DUK_HOBJECT_IS_CALLABLE(DUK_TVAL_GET_OBJECT(&h_boundtarget->target)) &&
  28923. !DUK_HOBJECT_IS_BOUNDFUNC(DUK_TVAL_GET_OBJECT(&h_boundtarget->target))));
  28924. DUK_TVAL_SET_TVAL(&h_bound->target, &h_boundtarget->target);
  28925. DUK_TVAL_SET_TVAL(&h_bound->this_binding, &h_boundtarget->this_binding);
  28926. tv_prevbound = h_boundtarget->args;
  28927. n_prevbound = h_boundtarget->nargs;
  28928. }
  28929. } else {
  28930. /* Lightfuncs are always strict. */
  28931. duk_hobject *bound_proto;
  28932. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_tmp));
  28933. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_bound);
  28934. bound_proto = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  28935. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) h_bound, bound_proto);
  28936. }
  28937. DUK_TVAL_INCREF(thr, &h_bound->target); /* old values undefined, no decref needed */
  28938. DUK_TVAL_INCREF(thr, &h_bound->this_binding);
  28939. bound_nargs = n_prevbound + nargs;
  28940. if (bound_nargs > (duk_idx_t) DUK_HBOUNDFUNC_MAX_ARGS) {
  28941. DUK_DCERROR_RANGE_INVALID_COUNT(thr);
  28942. }
  28943. tv_res = (duk_tval *) DUK_ALLOC_CHECKED(thr, ((duk_size_t) bound_nargs) * sizeof(duk_tval));
  28944. DUK_ASSERT(tv_res != NULL || bound_nargs == 0);
  28945. DUK_ASSERT(h_bound->args == NULL);
  28946. DUK_ASSERT(h_bound->nargs == 0);
  28947. h_bound->args = tv_res;
  28948. h_bound->nargs = bound_nargs;
  28949. DUK_ASSERT(n_prevbound >= 0);
  28950. duk_copy_tvals_incref(thr, tv_res, tv_prevbound, (duk_size_t) n_prevbound);
  28951. DUK_ASSERT(nargs >= 0);
  28952. duk_copy_tvals_incref(thr, tv_res + n_prevbound, DUK_GET_TVAL_POSIDX(thr, 1), (duk_size_t) nargs);
  28953. /* [ thisArg arg1 ... argN func boundFunc ] */
  28954. /* Bound function 'length' property is interesting.
  28955. * For lightfuncs, simply read the virtual property.
  28956. */
  28957. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH);
  28958. bound_len = duk_get_int(thr, -1); /* ES2015: no coercion */
  28959. if (bound_len < nargs) {
  28960. bound_len = 0;
  28961. } else {
  28962. bound_len -= nargs;
  28963. }
  28964. if (sizeof(duk_int_t) > 4 && bound_len > (duk_int_t) DUK_UINT32_MAX) {
  28965. bound_len = (duk_int_t) DUK_UINT32_MAX;
  28966. }
  28967. duk_pop(thr);
  28968. DUK_ASSERT(bound_len >= 0);
  28969. tv_tmp = thr->valstack_top++;
  28970. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv_tmp));
  28971. DUK_ASSERT(!DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv_tmp));
  28972. DUK_TVAL_SET_U32(tv_tmp, (duk_uint32_t) bound_len); /* in-place update, fastint */
  28973. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_C); /* attrs in E6 Section 9.2.4 */
  28974. /* XXX: could these be virtual? */
  28975. /* Caller and arguments must use the same thrower, [[ThrowTypeError]]. */
  28976. duk_xdef_prop_stridx_thrower(thr, -1, DUK_STRIDX_CALLER);
  28977. duk_xdef_prop_stridx_thrower(thr, -1, DUK_STRIDX_LC_ARGUMENTS);
  28978. /* Function name and fileName (non-standard). */
  28979. duk_push_literal(thr, "bound "); /* ES2015 19.2.3.2. */
  28980. duk_get_prop_stridx(thr, -3, DUK_STRIDX_NAME);
  28981. if (!duk_is_string_notsymbol(thr, -1)) {
  28982. /* ES2015 has requirement to check that .name of target is a string
  28983. * (also must check for Symbol); if not, targetName should be the
  28984. * empty string. ES2015 19.2.3.2.
  28985. */
  28986. duk_pop(thr);
  28987. duk_push_hstring_empty(thr);
  28988. }
  28989. duk_concat(thr, 2);
  28990. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C);
  28991. #if defined(DUK_USE_FUNC_FILENAME_PROPERTY)
  28992. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_FILE_NAME);
  28993. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_C);
  28994. #endif
  28995. DUK_DDD(DUK_DDDPRINT("created bound function: %!iT", (duk_tval *) duk_get_tval(thr, -1)));
  28996. return 1;
  28997. }
  28998. #endif /* DUK_USE_FUNCTION_BUILTIN */
  28999. /* %NativeFunctionPrototype% .length getter. */
  29000. DUK_INTERNAL duk_ret_t duk_bi_native_function_length(duk_hthread *thr) {
  29001. duk_tval *tv;
  29002. duk_hnatfunc *h;
  29003. duk_int16_t func_nargs;
  29004. tv = duk_get_borrowed_this_tval(thr);
  29005. DUK_ASSERT(tv != NULL);
  29006. if (DUK_TVAL_IS_OBJECT(tv)) {
  29007. h = (duk_hnatfunc *) DUK_TVAL_GET_OBJECT(tv);
  29008. DUK_ASSERT(h != NULL);
  29009. if (!DUK_HOBJECT_IS_NATFUNC((duk_hobject *) h)) {
  29010. goto fail_type;
  29011. }
  29012. func_nargs = h->nargs;
  29013. duk_push_int(thr, func_nargs == DUK_HNATFUNC_NARGS_VARARGS ? 0 : func_nargs);
  29014. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  29015. duk_small_uint_t lf_flags;
  29016. duk_small_uint_t lf_len;
  29017. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv);
  29018. lf_len = DUK_LFUNC_FLAGS_GET_LENGTH(lf_flags);
  29019. duk_push_uint(thr, lf_len);
  29020. } else {
  29021. goto fail_type;
  29022. }
  29023. return 1;
  29024. fail_type:
  29025. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  29026. }
  29027. /* %NativeFunctionPrototype% .name getter. */
  29028. DUK_INTERNAL duk_ret_t duk_bi_native_function_name(duk_hthread *thr) {
  29029. duk_tval *tv;
  29030. duk_hnatfunc *h;
  29031. tv = duk_get_borrowed_this_tval(thr);
  29032. DUK_ASSERT(tv != NULL);
  29033. if (DUK_TVAL_IS_OBJECT(tv)) {
  29034. h = (duk_hnatfunc *) DUK_TVAL_GET_OBJECT(tv);
  29035. DUK_ASSERT(h != NULL);
  29036. if (!DUK_HOBJECT_IS_NATFUNC((duk_hobject *) h)) {
  29037. goto fail_type;
  29038. }
  29039. #if 0
  29040. duk_push_hnatfunc_name(thr, h);
  29041. #endif
  29042. duk_push_hstring_empty(thr);
  29043. } else if (DUK_TVAL_IS_LIGHTFUNC(tv)) {
  29044. duk_push_lightfunc_name(thr, tv);
  29045. } else {
  29046. goto fail_type;
  29047. }
  29048. return 1;
  29049. fail_type:
  29050. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  29051. }
  29052. #if defined(DUK_USE_SYMBOL_BUILTIN)
  29053. DUK_INTERNAL duk_ret_t duk_bi_function_prototype_hasinstance(duk_hthread *thr) {
  29054. /* This binding: RHS, stack index 0: LHS. */
  29055. duk_bool_t ret;
  29056. ret = duk_js_instanceof_ordinary(thr, DUK_GET_TVAL_POSIDX(thr, 0), DUK_GET_THIS_TVAL_PTR(thr));
  29057. duk_push_boolean(thr, ret);
  29058. return 1;
  29059. }
  29060. #endif /* DUK_USE_SYMBOL_BUILTIN */
  29061. #line 1 "duk_bi_global.c"
  29062. /*
  29063. * Global object built-ins
  29064. */
  29065. /* #include duk_internal.h -> already included */
  29066. /*
  29067. * Encoding/decoding helpers
  29068. */
  29069. /* XXX: Could add fast path (for each transform callback) with direct byte
  29070. * lookups (no shifting) and no explicit check for x < 0x80 before table
  29071. * lookup.
  29072. */
  29073. /* Macros for creating and checking bitmasks for character encoding.
  29074. * Bit number is a bit counterintuitive, but minimizes code size.
  29075. */
  29076. #define DUK__MKBITS(a,b,c,d,e,f,g,h) ((duk_uint8_t) ( \
  29077. ((a) << 0) | ((b) << 1) | ((c) << 2) | ((d) << 3) | \
  29078. ((e) << 4) | ((f) << 5) | ((g) << 6) | ((h) << 7) \
  29079. ))
  29080. #define DUK__CHECK_BITMASK(table,cp) ((table)[(cp) >> 3] & (1 << ((cp) & 0x07)))
  29081. /* E5.1 Section 15.1.3.3: uriReserved + uriUnescaped + '#' */
  29082. DUK_LOCAL const duk_uint8_t duk__encode_uriunescaped_table[16] = {
  29083. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  29084. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  29085. DUK__MKBITS(0, 1, 0, 1, 1, 0, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x20-0x2f */
  29086. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */
  29087. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  29088. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  29089. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  29090. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */
  29091. };
  29092. /* E5.1 Section 15.1.3.4: uriUnescaped */
  29093. DUK_LOCAL const duk_uint8_t duk__encode_uricomponent_unescaped_table[16] = {
  29094. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  29095. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  29096. DUK__MKBITS(0, 1, 0, 0, 0, 0, 0, 1), DUK__MKBITS(1, 1, 1, 0, 0, 1, 1, 0), /* 0x20-0x2f */
  29097. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  29098. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  29099. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  29100. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  29101. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 1, 0), /* 0x70-0x7f */
  29102. };
  29103. /* E5.1 Section 15.1.3.1: uriReserved + '#' */
  29104. DUK_LOCAL const duk_uint8_t duk__decode_uri_reserved_table[16] = {
  29105. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  29106. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  29107. DUK__MKBITS(0, 0, 0, 1, 1, 0, 1, 0), DUK__MKBITS(0, 0, 0, 1, 1, 0, 0, 1), /* 0x20-0x2f */
  29108. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 0, 1), /* 0x30-0x3f */
  29109. DUK__MKBITS(1, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */
  29110. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */
  29111. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */
  29112. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */
  29113. };
  29114. /* E5.1 Section 15.1.3.2: empty */
  29115. DUK_LOCAL const duk_uint8_t duk__decode_uri_component_reserved_table[16] = {
  29116. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  29117. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  29118. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x20-0x2f */
  29119. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  29120. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x40-0x4f */
  29121. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x50-0x5f */
  29122. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x60-0x6f */
  29123. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x70-0x7f */
  29124. };
  29125. #if defined(DUK_USE_SECTION_B)
  29126. /* E5.1 Section B.2.2, step 7. */
  29127. DUK_LOCAL const duk_uint8_t duk__escape_unescaped_table[16] = {
  29128. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x00-0x0f */
  29129. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), /* 0x10-0x1f */
  29130. DUK__MKBITS(0, 0, 0, 0, 0, 0, 0, 0), DUK__MKBITS(0, 0, 1, 1, 0, 1, 1, 1), /* 0x20-0x2f */
  29131. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 0, 0, 0, 0, 0, 0), /* 0x30-0x3f */
  29132. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x40-0x4f */
  29133. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 1), /* 0x50-0x5f */
  29134. DUK__MKBITS(0, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), /* 0x60-0x6f */
  29135. DUK__MKBITS(1, 1, 1, 1, 1, 1, 1, 1), DUK__MKBITS(1, 1, 1, 0, 0, 0, 0, 0) /* 0x70-0x7f */
  29136. };
  29137. #endif /* DUK_USE_SECTION_B */
  29138. typedef struct {
  29139. duk_hthread *thr;
  29140. duk_hstring *h_str;
  29141. duk_bufwriter_ctx bw;
  29142. const duk_uint8_t *p;
  29143. const duk_uint8_t *p_start;
  29144. const duk_uint8_t *p_end;
  29145. } duk__transform_context;
  29146. typedef void (*duk__transform_callback)(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp);
  29147. /* XXX: refactor and share with other code */
  29148. DUK_LOCAL duk_small_int_t duk__decode_hex_escape(const duk_uint8_t *p, duk_small_int_t n) {
  29149. duk_small_int_t ch;
  29150. duk_small_int_t t = 0;
  29151. while (n > 0) {
  29152. t = t * 16;
  29153. ch = (duk_small_int_t) duk_hex_dectab[*p++];
  29154. if (DUK_LIKELY(ch >= 0)) {
  29155. t += ch;
  29156. } else {
  29157. return -1;
  29158. }
  29159. n--;
  29160. }
  29161. return t;
  29162. }
  29163. DUK_LOCAL int duk__transform_helper(duk_hthread *thr, duk__transform_callback callback, const void *udata) {
  29164. duk__transform_context tfm_ctx_alloc;
  29165. duk__transform_context *tfm_ctx = &tfm_ctx_alloc;
  29166. duk_codepoint_t cp;
  29167. tfm_ctx->thr = thr;
  29168. tfm_ctx->h_str = duk_to_hstring(thr, 0);
  29169. DUK_ASSERT(tfm_ctx->h_str != NULL);
  29170. DUK_BW_INIT_PUSHBUF(thr, &tfm_ctx->bw, DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str)); /* initial size guess */
  29171. tfm_ctx->p_start = DUK_HSTRING_GET_DATA(tfm_ctx->h_str);
  29172. tfm_ctx->p_end = tfm_ctx->p_start + DUK_HSTRING_GET_BYTELEN(tfm_ctx->h_str);
  29173. tfm_ctx->p = tfm_ctx->p_start;
  29174. while (tfm_ctx->p < tfm_ctx->p_end) {
  29175. cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end);
  29176. callback(tfm_ctx, udata, cp);
  29177. }
  29178. DUK_BW_COMPACT(thr, &tfm_ctx->bw);
  29179. (void) duk_buffer_to_string(thr, -1); /* Safe if transform is safe. */
  29180. return 1;
  29181. }
  29182. DUK_LOCAL void duk__transform_callback_encode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  29183. duk_uint8_t xutf8_buf[DUK_UNICODE_MAX_XUTF8_LENGTH];
  29184. duk_small_int_t len;
  29185. duk_codepoint_t cp1, cp2;
  29186. duk_small_int_t i, t;
  29187. const duk_uint8_t *unescaped_table = (const duk_uint8_t *) udata;
  29188. /* UTF-8 encoded bytes escaped as %xx%xx%xx... -> 3 * nbytes.
  29189. * Codepoint range is restricted so this is a slightly too large
  29190. * but doesn't matter.
  29191. */
  29192. DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 3 * DUK_UNICODE_MAX_XUTF8_LENGTH);
  29193. if (cp < 0) {
  29194. goto uri_error;
  29195. } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(unescaped_table, cp)) {
  29196. DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp);
  29197. return;
  29198. } else if (cp >= 0xdc00L && cp <= 0xdfffL) {
  29199. goto uri_error;
  29200. } else if (cp >= 0xd800L && cp <= 0xdbffL) {
  29201. /* Needs lookahead */
  29202. if (duk_unicode_decode_xutf8(tfm_ctx->thr, &tfm_ctx->p, tfm_ctx->p_start, tfm_ctx->p_end, (duk_ucodepoint_t *) &cp2) == 0) {
  29203. goto uri_error;
  29204. }
  29205. if (!(cp2 >= 0xdc00L && cp2 <= 0xdfffL)) {
  29206. goto uri_error;
  29207. }
  29208. cp1 = cp;
  29209. cp = (duk_codepoint_t) (((cp1 - 0xd800L) << 10) + (cp2 - 0xdc00L) + 0x10000L);
  29210. } else if (cp > 0x10ffffL) {
  29211. /* Although we can allow non-BMP characters (they'll decode
  29212. * back into surrogate pairs), we don't allow extended UTF-8
  29213. * characters; they would encode to URIs which won't decode
  29214. * back because of strict UTF-8 checks in URI decoding.
  29215. * (However, we could just as well allow them here.)
  29216. */
  29217. goto uri_error;
  29218. } else {
  29219. /* Non-BMP characters within valid UTF-8 range: encode as is.
  29220. * They'll decode back into surrogate pairs if the escaped
  29221. * output is decoded.
  29222. */
  29223. ;
  29224. }
  29225. len = duk_unicode_encode_xutf8((duk_ucodepoint_t) cp, xutf8_buf);
  29226. for (i = 0; i < len; i++) {
  29227. t = (duk_small_int_t) xutf8_buf[i];
  29228. DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr,
  29229. &tfm_ctx->bw,
  29230. DUK_ASC_PERCENT,
  29231. (duk_uint8_t) duk_uc_nybbles[t >> 4],
  29232. (duk_uint8_t) duk_uc_nybbles[t & 0x0f]);
  29233. }
  29234. return;
  29235. uri_error:
  29236. DUK_ERROR_URI(tfm_ctx->thr, DUK_STR_INVALID_INPUT);
  29237. DUK_WO_NORETURN(return;);
  29238. }
  29239. DUK_LOCAL void duk__transform_callback_decode_uri(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  29240. const duk_uint8_t *reserved_table = (const duk_uint8_t *) udata;
  29241. duk_small_uint_t utf8_blen;
  29242. duk_codepoint_t min_cp;
  29243. duk_small_int_t t; /* must be signed */
  29244. duk_small_uint_t i;
  29245. /* Maximum write size: XUTF8 path writes max DUK_UNICODE_MAX_XUTF8_LENGTH,
  29246. * percent escape path writes max two times CESU-8 encoded BMP length.
  29247. */
  29248. DUK_BW_ENSURE(tfm_ctx->thr,
  29249. &tfm_ctx->bw,
  29250. (DUK_UNICODE_MAX_XUTF8_LENGTH >= 2 * DUK_UNICODE_MAX_CESU8_BMP_LENGTH ?
  29251. DUK_UNICODE_MAX_XUTF8_LENGTH : DUK_UNICODE_MAX_CESU8_BMP_LENGTH));
  29252. if (cp == (duk_codepoint_t) '%') {
  29253. const duk_uint8_t *p = tfm_ctx->p;
  29254. duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */
  29255. DUK_DDD(DUK_DDDPRINT("percent encoding, left=%ld", (long) left));
  29256. if (left < 2) {
  29257. goto uri_error;
  29258. }
  29259. t = duk__decode_hex_escape(p, 2);
  29260. DUK_DDD(DUK_DDDPRINT("first byte: %ld", (long) t));
  29261. if (t < 0) {
  29262. goto uri_error;
  29263. }
  29264. if (t < 0x80) {
  29265. if (DUK__CHECK_BITMASK(reserved_table, t)) {
  29266. /* decode '%xx' to '%xx' if decoded char in reserved set */
  29267. DUK_ASSERT(tfm_ctx->p - 1 >= tfm_ctx->p_start);
  29268. DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr,
  29269. &tfm_ctx->bw,
  29270. DUK_ASC_PERCENT,
  29271. p[0],
  29272. p[1]);
  29273. } else {
  29274. DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) t);
  29275. }
  29276. tfm_ctx->p += 2;
  29277. return;
  29278. }
  29279. /* Decode UTF-8 codepoint from a sequence of hex escapes. The
  29280. * first byte of the sequence has been decoded to 't'.
  29281. *
  29282. * Note that UTF-8 validation must be strict according to the
  29283. * specification: E5.1 Section 15.1.3, decode algorithm step
  29284. * 4.d.vii.8. URIError from non-shortest encodings is also
  29285. * specifically noted in the spec.
  29286. */
  29287. DUK_ASSERT(t >= 0x80);
  29288. if (t < 0xc0) {
  29289. /* continuation byte */
  29290. goto uri_error;
  29291. } else if (t < 0xe0) {
  29292. /* 110x xxxx; 2 bytes */
  29293. utf8_blen = 2;
  29294. min_cp = 0x80L;
  29295. cp = t & 0x1f;
  29296. } else if (t < 0xf0) {
  29297. /* 1110 xxxx; 3 bytes */
  29298. utf8_blen = 3;
  29299. min_cp = 0x800L;
  29300. cp = t & 0x0f;
  29301. } else if (t < 0xf8) {
  29302. /* 1111 0xxx; 4 bytes */
  29303. utf8_blen = 4;
  29304. min_cp = 0x10000L;
  29305. cp = t & 0x07;
  29306. } else {
  29307. /* extended utf-8 not allowed for URIs */
  29308. goto uri_error;
  29309. }
  29310. if (left < utf8_blen * 3 - 1) {
  29311. /* '%xx%xx...%xx', p points to char after first '%' */
  29312. goto uri_error;
  29313. }
  29314. p += 3;
  29315. for (i = 1; i < utf8_blen; i++) {
  29316. /* p points to digit part ('%xy', p points to 'x') */
  29317. t = duk__decode_hex_escape(p, 2);
  29318. DUK_DDD(DUK_DDDPRINT("i=%ld utf8_blen=%ld cp=%ld t=0x%02lx",
  29319. (long) i, (long) utf8_blen, (long) cp, (unsigned long) t));
  29320. if (t < 0) {
  29321. goto uri_error;
  29322. }
  29323. if ((t & 0xc0) != 0x80) {
  29324. goto uri_error;
  29325. }
  29326. cp = (cp << 6) + (t & 0x3f);
  29327. p += 3;
  29328. }
  29329. p--; /* p overshoots */
  29330. tfm_ctx->p = p;
  29331. DUK_DDD(DUK_DDDPRINT("final cp=%ld, min_cp=%ld", (long) cp, (long) min_cp));
  29332. if (cp < min_cp || cp > 0x10ffffL || (cp >= 0xd800L && cp <= 0xdfffL)) {
  29333. goto uri_error;
  29334. }
  29335. /* The E5.1 algorithm checks whether or not a decoded codepoint
  29336. * is below 0x80 and perhaps may be in the "reserved" set.
  29337. * This seems pointless because the single byte UTF-8 case is
  29338. * handled separately, and non-shortest encodings are rejected.
  29339. * So, 'cp' cannot be below 0x80 here, and thus cannot be in
  29340. * the reserved set.
  29341. */
  29342. /* utf-8 validation ensures these */
  29343. DUK_ASSERT(cp >= 0x80L && cp <= 0x10ffffL);
  29344. if (cp >= 0x10000L) {
  29345. cp -= 0x10000L;
  29346. DUK_ASSERT(cp < 0x100000L);
  29347. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp >> 10) + 0xd800L));
  29348. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, ((cp & 0x03ffL) + 0xdc00L));
  29349. } else {
  29350. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp);
  29351. }
  29352. } else {
  29353. DUK_BW_WRITE_RAW_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp);
  29354. }
  29355. return;
  29356. uri_error:
  29357. DUK_ERROR_URI(tfm_ctx->thr, DUK_STR_INVALID_INPUT);
  29358. DUK_WO_NORETURN(return;);
  29359. }
  29360. #if defined(DUK_USE_SECTION_B)
  29361. DUK_LOCAL void duk__transform_callback_escape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  29362. DUK_UNREF(udata);
  29363. DUK_BW_ENSURE(tfm_ctx->thr, &tfm_ctx->bw, 6);
  29364. if (cp < 0) {
  29365. goto esc_error;
  29366. } else if ((cp < 0x80L) && DUK__CHECK_BITMASK(duk__escape_unescaped_table, cp)) {
  29367. DUK_BW_WRITE_RAW_U8(tfm_ctx->thr, &tfm_ctx->bw, (duk_uint8_t) cp);
  29368. } else if (cp < 0x100L) {
  29369. DUK_BW_WRITE_RAW_U8_3(tfm_ctx->thr,
  29370. &tfm_ctx->bw,
  29371. (duk_uint8_t) DUK_ASC_PERCENT,
  29372. (duk_uint8_t) duk_uc_nybbles[cp >> 4],
  29373. (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]);
  29374. } else if (cp < 0x10000L) {
  29375. DUK_BW_WRITE_RAW_U8_6(tfm_ctx->thr,
  29376. &tfm_ctx->bw,
  29377. (duk_uint8_t) DUK_ASC_PERCENT,
  29378. (duk_uint8_t) DUK_ASC_LC_U,
  29379. (duk_uint8_t) duk_uc_nybbles[cp >> 12],
  29380. (duk_uint8_t) duk_uc_nybbles[(cp >> 8) & 0x0f],
  29381. (duk_uint8_t) duk_uc_nybbles[(cp >> 4) & 0x0f],
  29382. (duk_uint8_t) duk_uc_nybbles[cp & 0x0f]);
  29383. } else {
  29384. /* Characters outside BMP cannot be escape()'d. We could
  29385. * encode them as surrogate pairs (for codepoints inside
  29386. * valid UTF-8 range, but not extended UTF-8). Because
  29387. * escape() and unescape() are legacy functions, we don't.
  29388. */
  29389. goto esc_error;
  29390. }
  29391. return;
  29392. esc_error:
  29393. DUK_ERROR_TYPE(tfm_ctx->thr, DUK_STR_INVALID_INPUT);
  29394. DUK_WO_NORETURN(return;);
  29395. }
  29396. DUK_LOCAL void duk__transform_callback_unescape(duk__transform_context *tfm_ctx, const void *udata, duk_codepoint_t cp) {
  29397. duk_small_int_t t;
  29398. DUK_UNREF(udata);
  29399. if (cp == (duk_codepoint_t) '%') {
  29400. const duk_uint8_t *p = tfm_ctx->p;
  29401. duk_size_t left = (duk_size_t) (tfm_ctx->p_end - p); /* bytes left */
  29402. if (left >= 5 && p[0] == 'u' &&
  29403. ((t = duk__decode_hex_escape(p + 1, 4)) >= 0)) {
  29404. cp = (duk_codepoint_t) t;
  29405. tfm_ctx->p += 5;
  29406. } else if (left >= 2 &&
  29407. ((t = duk__decode_hex_escape(p, 2)) >= 0)) {
  29408. cp = (duk_codepoint_t) t;
  29409. tfm_ctx->p += 2;
  29410. }
  29411. }
  29412. DUK_BW_WRITE_ENSURE_XUTF8(tfm_ctx->thr, &tfm_ctx->bw, cp);
  29413. }
  29414. #endif /* DUK_USE_SECTION_B */
  29415. /*
  29416. * Eval
  29417. *
  29418. * Eval needs to handle both a "direct eval" and an "indirect eval".
  29419. * Direct eval handling needs access to the caller's activation so that its
  29420. * lexical environment can be accessed. A direct eval is only possible from
  29421. * ECMAScript code; an indirect eval call is possible also from C code.
  29422. * When an indirect eval call is made from C code, there may not be a
  29423. * calling activation at all which needs careful handling.
  29424. */
  29425. DUK_INTERNAL duk_ret_t duk_bi_global_object_eval(duk_hthread *thr) {
  29426. duk_hstring *h;
  29427. duk_activation *act_caller;
  29428. duk_activation *act_eval;
  29429. duk_hcompfunc *func;
  29430. duk_hobject *outer_lex_env;
  29431. duk_hobject *outer_var_env;
  29432. duk_bool_t this_to_global = 1;
  29433. duk_small_uint_t comp_flags;
  29434. duk_int_t level = -2;
  29435. duk_small_uint_t call_flags;
  29436. DUK_ASSERT(duk_get_top(thr) == 1 || duk_get_top(thr) == 2); /* 2 when called by debugger */
  29437. DUK_ASSERT(thr->callstack_top >= 1); /* at least this function exists */
  29438. DUK_ASSERT(thr->callstack_curr != NULL);
  29439. DUK_ASSERT((thr->callstack_curr->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0 || /* indirect eval */
  29440. (thr->callstack_top >= 2)); /* if direct eval, calling activation must exist */
  29441. /*
  29442. * callstack_top - 1 --> this function
  29443. * callstack_top - 2 --> caller (may not exist)
  29444. *
  29445. * If called directly from C, callstack_top might be 1. If calling
  29446. * activation doesn't exist, call must be indirect.
  29447. */
  29448. h = duk_get_hstring_notsymbol(thr, 0);
  29449. if (!h) {
  29450. /* Symbol must be returned as is, like any non-string values. */
  29451. return 1; /* return arg as-is */
  29452. }
  29453. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  29454. /* NOTE: level is used only by the debugger and should never be present
  29455. * for an ECMAScript eval().
  29456. */
  29457. DUK_ASSERT(level == -2); /* by default, use caller's environment */
  29458. if (duk_get_top(thr) >= 2 && duk_is_number(thr, 1)) {
  29459. level = duk_get_int(thr, 1);
  29460. }
  29461. DUK_ASSERT(level <= -2); /* This is guaranteed by debugger code. */
  29462. #endif
  29463. /* [ source ] */
  29464. comp_flags = DUK_COMPILE_EVAL;
  29465. act_eval = thr->callstack_curr; /* this function */
  29466. DUK_ASSERT(act_eval != NULL);
  29467. act_caller = duk_hthread_get_activation_for_level(thr, level);
  29468. if (act_caller != NULL) {
  29469. /* Have a calling activation, check for direct eval (otherwise
  29470. * assume indirect eval.
  29471. */
  29472. if ((act_caller->flags & DUK_ACT_FLAG_STRICT) &&
  29473. (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL)) {
  29474. /* Only direct eval inherits strictness from calling code
  29475. * (E5.1 Section 10.1.1).
  29476. */
  29477. comp_flags |= DUK_COMPILE_STRICT;
  29478. }
  29479. } else {
  29480. DUK_ASSERT((act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) == 0);
  29481. }
  29482. duk_push_hstring_stridx(thr, DUK_STRIDX_INPUT); /* XXX: copy from caller? */
  29483. duk_js_compile(thr,
  29484. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h),
  29485. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h),
  29486. comp_flags);
  29487. func = (duk_hcompfunc *) duk_known_hobject(thr, -1);
  29488. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) func));
  29489. /* [ source template ] */
  29490. /* E5 Section 10.4.2 */
  29491. if (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) {
  29492. DUK_ASSERT(thr->callstack_top >= 2);
  29493. DUK_ASSERT(act_caller != NULL);
  29494. if (act_caller->lex_env == NULL) {
  29495. DUK_ASSERT(act_caller->var_env == NULL);
  29496. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  29497. /* this may have side effects, so re-lookup act */
  29498. duk_js_init_activation_environment_records_delayed(thr, act_caller);
  29499. }
  29500. DUK_ASSERT(act_caller->lex_env != NULL);
  29501. DUK_ASSERT(act_caller->var_env != NULL);
  29502. this_to_global = 0;
  29503. if (DUK_HOBJECT_HAS_STRICT((duk_hobject *) func)) {
  29504. duk_hdecenv *new_env;
  29505. duk_hobject *act_lex_env;
  29506. DUK_DDD(DUK_DDDPRINT("direct eval call to a strict function -> "
  29507. "var_env and lex_env to a fresh env, "
  29508. "this_binding to caller's this_binding"));
  29509. act_lex_env = act_caller->lex_env;
  29510. new_env = duk_hdecenv_alloc(thr,
  29511. DUK_HOBJECT_FLAG_EXTENSIBLE |
  29512. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV));
  29513. DUK_ASSERT(new_env != NULL);
  29514. duk_push_hobject(thr, (duk_hobject *) new_env);
  29515. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) new_env) == NULL);
  29516. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, (duk_hobject *) new_env, act_lex_env);
  29517. DUK_HOBJECT_INCREF_ALLOWNULL(thr, act_lex_env);
  29518. DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr *) new_env));
  29519. outer_lex_env = (duk_hobject *) new_env;
  29520. outer_var_env = (duk_hobject *) new_env;
  29521. duk_insert(thr, 0); /* stash to bottom of value stack to keep new_env reachable for duration of eval */
  29522. /* compiler's responsibility */
  29523. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func));
  29524. } else {
  29525. DUK_DDD(DUK_DDDPRINT("direct eval call to a non-strict function -> "
  29526. "var_env and lex_env to caller's envs, "
  29527. "this_binding to caller's this_binding"));
  29528. outer_lex_env = act_caller->lex_env;
  29529. outer_var_env = act_caller->var_env;
  29530. /* compiler's responsibility */
  29531. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) func));
  29532. }
  29533. } else {
  29534. DUK_DDD(DUK_DDDPRINT("indirect eval call -> var_env and lex_env to "
  29535. "global object, this_binding to global object"));
  29536. this_to_global = 1;
  29537. outer_lex_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  29538. outer_var_env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  29539. }
  29540. /* Eval code doesn't need an automatic .prototype object. */
  29541. duk_js_push_closure(thr, func, outer_var_env, outer_lex_env, 0 /*add_auto_proto*/);
  29542. /* [ env? source template closure ] */
  29543. if (this_to_global) {
  29544. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  29545. duk_push_hobject_bidx(thr, DUK_BIDX_GLOBAL);
  29546. } else {
  29547. duk_tval *tv;
  29548. DUK_ASSERT(thr->callstack_top >= 2);
  29549. DUK_ASSERT(act_caller != NULL);
  29550. tv = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + act_caller->bottom_byteoff - sizeof(duk_tval)); /* this is just beneath bottom */
  29551. DUK_ASSERT(tv >= thr->valstack);
  29552. duk_push_tval(thr, tv);
  29553. }
  29554. DUK_DDD(DUK_DDDPRINT("eval -> lex_env=%!iO, var_env=%!iO, this_binding=%!T",
  29555. (duk_heaphdr *) outer_lex_env,
  29556. (duk_heaphdr *) outer_var_env,
  29557. duk_get_tval(thr, -1)));
  29558. /* [ env? source template closure this ] */
  29559. call_flags = 0;
  29560. if (act_eval->flags & DUK_ACT_FLAG_DIRECT_EVAL) {
  29561. /* Set DIRECT_EVAL flag for the call; it's not strictly
  29562. * needed for the 'inner' eval call (the eval body) but
  29563. * current new.target implementation expects to find it
  29564. * so it can traverse direct eval chains up to the real
  29565. * calling function.
  29566. */
  29567. call_flags |= DUK_CALL_FLAG_DIRECT_EVAL;
  29568. }
  29569. duk_handle_call_unprotected_nargs(thr, 0, call_flags);
  29570. /* [ env? source template result ] */
  29571. return 1;
  29572. }
  29573. /*
  29574. * Parsing of ints and floats
  29575. */
  29576. #if defined(DUK_USE_GLOBAL_BUILTIN)
  29577. DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_int(duk_hthread *thr) {
  29578. duk_int32_t radix;
  29579. duk_small_uint_t s2n_flags;
  29580. DUK_ASSERT_TOP(thr, 2);
  29581. duk_to_string(thr, 0); /* Reject symbols. */
  29582. radix = duk_to_int32(thr, 1);
  29583. /* While parseInt() recognizes 0xdeadbeef, it doesn't recognize
  29584. * ES2015 0o123 or 0b10001.
  29585. */
  29586. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  29587. DUK_S2N_FLAG_ALLOW_GARBAGE |
  29588. DUK_S2N_FLAG_ALLOW_PLUS |
  29589. DUK_S2N_FLAG_ALLOW_MINUS |
  29590. DUK_S2N_FLAG_ALLOW_LEADING_ZERO |
  29591. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  29592. /* Specification stripPrefix maps to DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT.
  29593. *
  29594. * Don't autodetect octals (from leading zeroes), require user code to
  29595. * provide an explicit radix 8 for parsing octal. See write-up from Mozilla:
  29596. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#ECMAScript_5_Removes_Octal_Interpretation
  29597. */
  29598. if (radix != 0) {
  29599. if (radix < 2 || radix > 36) {
  29600. goto ret_nan;
  29601. }
  29602. if (radix != 16) {
  29603. s2n_flags &= ~DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT;
  29604. }
  29605. } else {
  29606. radix = 10;
  29607. }
  29608. duk_dup_0(thr);
  29609. duk_numconv_parse(thr, (duk_small_int_t) radix, s2n_flags);
  29610. return 1;
  29611. ret_nan:
  29612. duk_push_nan(thr);
  29613. return 1;
  29614. }
  29615. #endif /* DUK_USE_GLOBAL_BUILTIN */
  29616. #if defined(DUK_USE_GLOBAL_BUILTIN)
  29617. DUK_INTERNAL duk_ret_t duk_bi_global_object_parse_float(duk_hthread *thr) {
  29618. duk_small_uint_t s2n_flags;
  29619. DUK_ASSERT_TOP(thr, 1);
  29620. duk_to_string(thr, 0); /* Reject symbols. */
  29621. /* XXX: check flags */
  29622. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  29623. DUK_S2N_FLAG_ALLOW_EXP |
  29624. DUK_S2N_FLAG_ALLOW_GARBAGE |
  29625. DUK_S2N_FLAG_ALLOW_PLUS |
  29626. DUK_S2N_FLAG_ALLOW_MINUS |
  29627. DUK_S2N_FLAG_ALLOW_INF |
  29628. DUK_S2N_FLAG_ALLOW_FRAC |
  29629. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  29630. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  29631. DUK_S2N_FLAG_ALLOW_LEADING_ZERO;
  29632. duk_numconv_parse(thr, 10 /*radix*/, s2n_flags);
  29633. return 1;
  29634. }
  29635. #endif /* DUK_USE_GLOBAL_BUILTIN */
  29636. /*
  29637. * Number checkers
  29638. */
  29639. #if defined(DUK_USE_GLOBAL_BUILTIN)
  29640. DUK_INTERNAL duk_ret_t duk_bi_global_object_is_nan(duk_hthread *thr) {
  29641. duk_double_t d = duk_to_number(thr, 0);
  29642. duk_push_boolean(thr, (duk_bool_t) DUK_ISNAN(d));
  29643. return 1;
  29644. }
  29645. #endif /* DUK_USE_GLOBAL_BUILTIN */
  29646. #if defined(DUK_USE_GLOBAL_BUILTIN)
  29647. DUK_INTERNAL duk_ret_t duk_bi_global_object_is_finite(duk_hthread *thr) {
  29648. duk_double_t d = duk_to_number(thr, 0);
  29649. duk_push_boolean(thr, (duk_bool_t) DUK_ISFINITE(d));
  29650. return 1;
  29651. }
  29652. #endif /* DUK_USE_GLOBAL_BUILTIN */
  29653. /*
  29654. * URI handling
  29655. */
  29656. #if defined(DUK_USE_GLOBAL_BUILTIN)
  29657. DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri(duk_hthread *thr) {
  29658. return duk__transform_helper(thr, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_reserved_table);
  29659. }
  29660. DUK_INTERNAL duk_ret_t duk_bi_global_object_decode_uri_component(duk_hthread *thr) {
  29661. return duk__transform_helper(thr, duk__transform_callback_decode_uri, (const void *) duk__decode_uri_component_reserved_table);
  29662. }
  29663. DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri(duk_hthread *thr) {
  29664. return duk__transform_helper(thr, duk__transform_callback_encode_uri, (const void *) duk__encode_uriunescaped_table);
  29665. }
  29666. DUK_INTERNAL duk_ret_t duk_bi_global_object_encode_uri_component(duk_hthread *thr) {
  29667. return duk__transform_helper(thr, duk__transform_callback_encode_uri, (const void *) duk__encode_uricomponent_unescaped_table);
  29668. }
  29669. #if defined(DUK_USE_SECTION_B)
  29670. DUK_INTERNAL duk_ret_t duk_bi_global_object_escape(duk_hthread *thr) {
  29671. return duk__transform_helper(thr, duk__transform_callback_escape, (const void *) NULL);
  29672. }
  29673. DUK_INTERNAL duk_ret_t duk_bi_global_object_unescape(duk_hthread *thr) {
  29674. return duk__transform_helper(thr, duk__transform_callback_unescape, (const void *) NULL);
  29675. }
  29676. #endif /* DUK_USE_SECTION_B */
  29677. #endif /* DUK_USE_GLOBAL_BUILTIN */
  29678. /* automatic undefs */
  29679. #undef DUK__CHECK_BITMASK
  29680. #undef DUK__MKBITS
  29681. #line 1 "duk_bi_json.c"
  29682. /*
  29683. * JSON built-ins.
  29684. *
  29685. * See doc/json.rst.
  29686. *
  29687. * Codepoints are handled as duk_uint_fast32_t to ensure that the full
  29688. * unsigned 32-bit range is supported. This matters to e.g. JX.
  29689. *
  29690. * Input parsing doesn't do an explicit end-of-input check at all. This is
  29691. * safe: input string data is always NUL-terminated (0x00) and valid JSON
  29692. * inputs never contain plain NUL characters, so that as long as syntax checks
  29693. * are correct, we'll never read past the NUL. This approach reduces code size
  29694. * and improves parsing performance, but it's critical that syntax checks are
  29695. * indeed correct!
  29696. */
  29697. /* #include duk_internal.h -> already included */
  29698. #if defined(DUK_USE_JSON_SUPPORT)
  29699. /*
  29700. * Local defines and forward declarations.
  29701. */
  29702. #define DUK__JSON_DECSTR_BUFSIZE 128
  29703. #define DUK__JSON_DECSTR_CHUNKSIZE 64
  29704. #define DUK__JSON_ENCSTR_CHUNKSIZE 64
  29705. #define DUK__JSON_STRINGIFY_BUFSIZE 128
  29706. #define DUK__JSON_MAX_ESC_LEN 10 /* '\Udeadbeef' */
  29707. DUK_LOCAL_DECL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx);
  29708. DUK_LOCAL_DECL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx);
  29709. #if defined(DUK_USE_JX)
  29710. DUK_LOCAL_DECL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx);
  29711. #endif
  29712. DUK_LOCAL_DECL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx);
  29713. DUK_LOCAL_DECL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx);
  29714. DUK_LOCAL_DECL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n);
  29715. DUK_LOCAL_DECL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx);
  29716. DUK_LOCAL_DECL void duk__dec_string(duk_json_dec_ctx *js_ctx);
  29717. #if defined(DUK_USE_JX)
  29718. DUK_LOCAL_DECL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx);
  29719. DUK_LOCAL_DECL void duk__dec_pointer(duk_json_dec_ctx *js_ctx);
  29720. DUK_LOCAL_DECL void duk__dec_buffer(duk_json_dec_ctx *js_ctx);
  29721. #endif
  29722. DUK_LOCAL_DECL void duk__dec_number(duk_json_dec_ctx *js_ctx);
  29723. DUK_LOCAL_DECL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx);
  29724. DUK_LOCAL_DECL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx);
  29725. DUK_LOCAL_DECL void duk__dec_object(duk_json_dec_ctx *js_ctx);
  29726. DUK_LOCAL_DECL void duk__dec_array(duk_json_dec_ctx *js_ctx);
  29727. DUK_LOCAL_DECL void duk__dec_value(duk_json_dec_ctx *js_ctx);
  29728. DUK_LOCAL_DECL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx);
  29729. DUK_LOCAL_DECL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch);
  29730. DUK_LOCAL_DECL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2);
  29731. DUK_LOCAL_DECL void duk__unemit_1(duk_json_enc_ctx *js_ctx);
  29732. DUK_LOCAL_DECL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h);
  29733. #if defined(DUK_USE_FASTINT)
  29734. DUK_LOCAL_DECL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p);
  29735. #endif
  29736. DUK_LOCAL_DECL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx);
  29737. DUK_LOCAL_DECL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q);
  29738. DUK_LOCAL_DECL void duk__enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k);
  29739. DUK_LOCAL_DECL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str);
  29740. DUK_LOCAL_DECL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top);
  29741. DUK_LOCAL_DECL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top);
  29742. DUK_LOCAL_DECL void duk__enc_object(duk_json_enc_ctx *js_ctx);
  29743. DUK_LOCAL_DECL void duk__enc_array(duk_json_enc_ctx *js_ctx);
  29744. DUK_LOCAL_DECL duk_bool_t duk__enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder);
  29745. DUK_LOCAL_DECL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv);
  29746. DUK_LOCAL_DECL void duk__enc_double(duk_json_enc_ctx *js_ctx);
  29747. #if defined(DUK_USE_FASTINT)
  29748. DUK_LOCAL_DECL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv);
  29749. #endif
  29750. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  29751. DUK_LOCAL_DECL void duk__enc_buffer_jx_jc(duk_json_enc_ctx *js_ctx, duk_hbuffer *h);
  29752. DUK_LOCAL_DECL void duk__enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr);
  29753. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  29754. DUK_LOCAL_DECL void duk__enc_bufobj(duk_json_enc_ctx *js_ctx, duk_hbufobj *h_bufobj);
  29755. #endif
  29756. #endif
  29757. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  29758. DUK_LOCAL_DECL void duk__enc_buffer_json_fastpath(duk_json_enc_ctx *js_ctx, duk_hbuffer *h);
  29759. #endif
  29760. DUK_LOCAL_DECL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_uint_t depth);
  29761. /*
  29762. * Helper tables
  29763. */
  29764. #if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH)
  29765. DUK_LOCAL const duk_uint8_t duk__json_quotestr_lookup[256] = {
  29766. /* 0x00 ... 0x7f: as is
  29767. * 0x80: escape generically
  29768. * 0x81: slow path
  29769. * 0xa0 ... 0xff: backslash + one char
  29770. */
  29771. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xe2, 0xf4, 0xee, 0x80, 0xe6, 0xf2, 0x80, 0x80,
  29772. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
  29773. 0x20, 0x21, 0xa2, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  29774. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  29775. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  29776. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0xdc, 0x5d, 0x5e, 0x5f,
  29777. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  29778. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x81,
  29779. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29780. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29781. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29782. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29783. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29784. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29785. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
  29786. 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81
  29787. };
  29788. #else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  29789. DUK_LOCAL const duk_uint8_t duk__json_quotestr_esc[14] = {
  29790. DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
  29791. DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
  29792. DUK_ASC_LC_B, DUK_ASC_LC_T, DUK_ASC_LC_N, DUK_ASC_NUL,
  29793. DUK_ASC_LC_F, DUK_ASC_LC_R
  29794. };
  29795. #endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  29796. #if defined(DUK_USE_JSON_DECSTRING_FASTPATH)
  29797. DUK_LOCAL const duk_uint8_t duk__json_decstr_lookup[256] = {
  29798. /* 0x00: slow path
  29799. * other: as is
  29800. */
  29801. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29802. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29803. 0x20, 0x21, 0x00, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  29804. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  29805. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  29806. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x00, 0x5d, 0x5e, 0x5f,
  29807. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  29808. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
  29809. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  29810. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  29811. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
  29812. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
  29813. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  29814. 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
  29815. 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  29816. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
  29817. };
  29818. #endif /* DUK_USE_JSON_DECSTRING_FASTPATH */
  29819. #if defined(DUK_USE_JSON_EATWHITE_FASTPATH)
  29820. DUK_LOCAL const duk_uint8_t duk__json_eatwhite_lookup[256] = {
  29821. /* 0x00: finish (non-white)
  29822. * 0x01: continue
  29823. */
  29824. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
  29825. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29826. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29827. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29828. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29829. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29830. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29831. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29832. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29833. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29834. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29835. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29836. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29837. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29838. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29839. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  29840. };
  29841. #endif /* DUK_USE_JSON_EATWHITE_FASTPATH */
  29842. #if defined(DUK_USE_JSON_DECNUMBER_FASTPATH)
  29843. DUK_LOCAL const duk_uint8_t duk__json_decnumber_lookup[256] = {
  29844. /* 0x00: finish (not part of number)
  29845. * 0x01: continue
  29846. */
  29847. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29848. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29849. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
  29850. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29851. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29852. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29853. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29854. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29855. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29856. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29857. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29858. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29859. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29860. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29861. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29862. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  29863. };
  29864. #endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */
  29865. /*
  29866. * Parsing implementation.
  29867. *
  29868. * JSON lexer is now separate from duk_lexer.c because there are numerous
  29869. * small differences making it difficult to share the lexer.
  29870. *
  29871. * The parser here works with raw bytes directly; this works because all
  29872. * JSON delimiters are ASCII characters. Invalid xUTF-8 encoded values
  29873. * inside strings will be passed on without normalization; this is not a
  29874. * compliance concern because compliant inputs will always be valid
  29875. * CESU-8 encodings.
  29876. */
  29877. DUK_LOCAL void duk__dec_syntax_error(duk_json_dec_ctx *js_ctx) {
  29878. /* Shared handler to minimize parser size. Cause will be
  29879. * hidden, unfortunately, but we'll have an offset which
  29880. * is often quite enough.
  29881. */
  29882. DUK_ERROR_FMT1(js_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FMT_INVALID_JSON,
  29883. (long) (js_ctx->p - js_ctx->p_start));
  29884. DUK_WO_NORETURN(return;);
  29885. }
  29886. DUK_LOCAL void duk__dec_eat_white(duk_json_dec_ctx *js_ctx) {
  29887. const duk_uint8_t *p;
  29888. duk_uint8_t t;
  29889. p = js_ctx->p;
  29890. for (;;) {
  29891. DUK_ASSERT(p <= js_ctx->p_end);
  29892. t = *p;
  29893. #if defined(DUK_USE_JSON_EATWHITE_FASTPATH)
  29894. /* This fast path is pretty marginal in practice.
  29895. * XXX: candidate for removal.
  29896. */
  29897. DUK_ASSERT(duk__json_eatwhite_lookup[0x00] == 0x00); /* end-of-input breaks */
  29898. if (duk__json_eatwhite_lookup[t] == 0) {
  29899. break;
  29900. }
  29901. #else /* DUK_USE_JSON_EATWHITE_FASTPATH */
  29902. if (!(t == 0x20 || t == 0x0a || t == 0x0d || t == 0x09)) {
  29903. /* NUL also comes here. Comparison order matters, 0x20
  29904. * is most common whitespace.
  29905. */
  29906. break;
  29907. }
  29908. #endif /* DUK_USE_JSON_EATWHITE_FASTPATH */
  29909. p++;
  29910. }
  29911. js_ctx->p = p;
  29912. }
  29913. #if defined(DUK_USE_JX)
  29914. DUK_LOCAL duk_uint8_t duk__dec_peek(duk_json_dec_ctx *js_ctx) {
  29915. DUK_ASSERT(js_ctx->p <= js_ctx->p_end);
  29916. return *js_ctx->p;
  29917. }
  29918. #endif
  29919. DUK_LOCAL duk_uint8_t duk__dec_get(duk_json_dec_ctx *js_ctx) {
  29920. DUK_ASSERT(js_ctx->p <= js_ctx->p_end);
  29921. return *js_ctx->p++;
  29922. }
  29923. DUK_LOCAL duk_uint8_t duk__dec_get_nonwhite(duk_json_dec_ctx *js_ctx) {
  29924. duk__dec_eat_white(js_ctx);
  29925. return duk__dec_get(js_ctx);
  29926. }
  29927. /* For JX, expressing the whole unsigned 32-bit range matters. */
  29928. DUK_LOCAL duk_uint_fast32_t duk__dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n) {
  29929. duk_small_uint_t i;
  29930. duk_uint_fast32_t res = 0;
  29931. duk_uint8_t x;
  29932. duk_small_int_t t;
  29933. for (i = 0; i < n; i++) {
  29934. /* XXX: share helper from lexer; duk_lexer.c / hexval(). */
  29935. x = duk__dec_get(js_ctx);
  29936. DUK_DDD(DUK_DDDPRINT("decode_hex_escape: i=%ld, n=%ld, res=%ld, x=%ld",
  29937. (long) i, (long) n, (long) res, (long) x));
  29938. /* x == 0x00 (EOF) causes syntax_error */
  29939. DUK_ASSERT(duk_hex_dectab[0] == -1);
  29940. t = duk_hex_dectab[x & 0xff];
  29941. if (DUK_LIKELY(t >= 0)) {
  29942. res = (res * 16) + (duk_uint_fast32_t) t;
  29943. } else {
  29944. /* catches EOF and invalid digits */
  29945. goto syntax_error;
  29946. }
  29947. }
  29948. DUK_DDD(DUK_DDDPRINT("final hex decoded value: %ld", (long) res));
  29949. return res;
  29950. syntax_error:
  29951. duk__dec_syntax_error(js_ctx);
  29952. DUK_UNREACHABLE();
  29953. return 0;
  29954. }
  29955. DUK_LOCAL void duk__dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx) {
  29956. duk_hstring *h;
  29957. const duk_uint8_t *p;
  29958. duk_uint8_t x, y;
  29959. /* First character has already been eaten and checked by the caller.
  29960. * We can scan until a NUL in stridx string because no built-in strings
  29961. * have internal NULs.
  29962. */
  29963. DUK_ASSERT_STRIDX_VALID(stridx);
  29964. h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
  29965. DUK_ASSERT(h != NULL);
  29966. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h) + 1;
  29967. DUK_ASSERT(*(js_ctx->p - 1) == *(p - 1)); /* first character has been matched */
  29968. for (;;) {
  29969. x = *p;
  29970. if (x == 0) {
  29971. break;
  29972. }
  29973. y = duk__dec_get(js_ctx);
  29974. if (x != y) {
  29975. /* Catches EOF of JSON input. */
  29976. goto syntax_error;
  29977. }
  29978. p++;
  29979. }
  29980. return;
  29981. syntax_error:
  29982. duk__dec_syntax_error(js_ctx);
  29983. DUK_UNREACHABLE();
  29984. }
  29985. DUK_LOCAL duk_small_int_t duk__dec_string_escape(duk_json_dec_ctx *js_ctx, duk_uint8_t **ext_p) {
  29986. duk_uint_fast32_t cp;
  29987. /* EOF (-1) will be cast to an unsigned value first
  29988. * and then re-cast for the switch. In any case, it
  29989. * will match the default case (syntax error).
  29990. */
  29991. cp = (duk_uint_fast32_t) duk__dec_get(js_ctx);
  29992. switch (cp) {
  29993. case DUK_ASC_BACKSLASH: break;
  29994. case DUK_ASC_DOUBLEQUOTE: break;
  29995. case DUK_ASC_SLASH: break;
  29996. case DUK_ASC_LC_T: cp = 0x09; break;
  29997. case DUK_ASC_LC_N: cp = 0x0a; break;
  29998. case DUK_ASC_LC_R: cp = 0x0d; break;
  29999. case DUK_ASC_LC_F: cp = 0x0c; break;
  30000. case DUK_ASC_LC_B: cp = 0x08; break;
  30001. case DUK_ASC_LC_U: {
  30002. cp = duk__dec_decode_hex_escape(js_ctx, 4);
  30003. break;
  30004. }
  30005. #if defined(DUK_USE_JX)
  30006. case DUK_ASC_UC_U: {
  30007. if (js_ctx->flag_ext_custom) {
  30008. cp = duk__dec_decode_hex_escape(js_ctx, 8);
  30009. } else {
  30010. return 1; /* syntax error */
  30011. }
  30012. break;
  30013. }
  30014. case DUK_ASC_LC_X: {
  30015. if (js_ctx->flag_ext_custom) {
  30016. cp = duk__dec_decode_hex_escape(js_ctx, 2);
  30017. } else {
  30018. return 1; /* syntax error */
  30019. }
  30020. break;
  30021. }
  30022. #endif /* DUK_USE_JX */
  30023. default:
  30024. /* catches EOF (0x00) */
  30025. return 1; /* syntax error */
  30026. }
  30027. DUK_RAW_WRITE_XUTF8(*ext_p, cp);
  30028. return 0;
  30029. }
  30030. DUK_LOCAL void duk__dec_string(duk_json_dec_ctx *js_ctx) {
  30031. duk_hthread *thr = js_ctx->thr;
  30032. duk_bufwriter_ctx bw_alloc;
  30033. duk_bufwriter_ctx *bw;
  30034. duk_uint8_t *q;
  30035. /* '"' was eaten by caller */
  30036. /* Note that we currently parse -bytes-, not codepoints.
  30037. * All non-ASCII extended UTF-8 will encode to bytes >= 0x80,
  30038. * so they'll simply pass through (valid UTF-8 or not).
  30039. */
  30040. bw = &bw_alloc;
  30041. DUK_BW_INIT_PUSHBUF(js_ctx->thr, bw, DUK__JSON_DECSTR_BUFSIZE);
  30042. q = DUK_BW_GET_PTR(js_ctx->thr, bw);
  30043. #if defined(DUK_USE_JSON_DECSTRING_FASTPATH)
  30044. for (;;) {
  30045. duk_small_uint_t safe;
  30046. duk_uint8_t b, x;
  30047. const duk_uint8_t *p;
  30048. /* Select a safe loop count where no output checks are
  30049. * needed assuming we won't encounter escapes. Input
  30050. * bound checks are not necessary as a NUL (guaranteed)
  30051. * will cause a SyntaxError before we read out of bounds.
  30052. */
  30053. safe = DUK__JSON_DECSTR_CHUNKSIZE;
  30054. /* Ensure space for 1:1 output plus one escape. */
  30055. q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, safe + DUK_UNICODE_MAX_XUTF8_LENGTH, q);
  30056. p = js_ctx->p; /* temp copy, write back for next loop */
  30057. for (;;) {
  30058. if (safe == 0) {
  30059. js_ctx->p = p;
  30060. break;
  30061. }
  30062. safe--;
  30063. /* End of input (NUL) goes through slow path and causes SyntaxError. */
  30064. DUK_ASSERT(duk__json_decstr_lookup[0] == 0x00);
  30065. b = *p++;
  30066. x = (duk_small_int_t) duk__json_decstr_lookup[b];
  30067. if (DUK_LIKELY(x != 0)) {
  30068. /* Fast path, decode as is. */
  30069. *q++ = b;
  30070. } else if (b == DUK_ASC_DOUBLEQUOTE) {
  30071. js_ctx->p = p;
  30072. goto found_quote;
  30073. } else if (b == DUK_ASC_BACKSLASH) {
  30074. /* We've ensured space for one escaped input; then
  30075. * bail out and recheck (this makes escape handling
  30076. * quite slow but it's uncommon).
  30077. */
  30078. js_ctx->p = p;
  30079. if (duk__dec_string_escape(js_ctx, &q) != 0) {
  30080. goto syntax_error;
  30081. }
  30082. break;
  30083. } else {
  30084. js_ctx->p = p;
  30085. goto syntax_error;
  30086. }
  30087. }
  30088. }
  30089. found_quote:
  30090. #else /* DUK_USE_JSON_DECSTRING_FASTPATH */
  30091. for (;;) {
  30092. duk_uint8_t x;
  30093. q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, DUK_UNICODE_MAX_XUTF8_LENGTH, q);
  30094. x = duk__dec_get(js_ctx);
  30095. if (x == DUK_ASC_DOUBLEQUOTE) {
  30096. break;
  30097. } else if (x == DUK_ASC_BACKSLASH) {
  30098. if (duk__dec_string_escape(js_ctx, &q) != 0) {
  30099. goto syntax_error;
  30100. }
  30101. } else if (x < 0x20) {
  30102. /* catches EOF (NUL) */
  30103. goto syntax_error;
  30104. } else {
  30105. *q++ = (duk_uint8_t) x;
  30106. }
  30107. }
  30108. #endif /* DUK_USE_JSON_DECSTRING_FASTPATH */
  30109. DUK_BW_SETPTR_AND_COMPACT(js_ctx->thr, bw, q);
  30110. (void) duk_buffer_to_string(thr, -1); /* Safe if input string is safe. */
  30111. /* [ ... str ] */
  30112. return;
  30113. syntax_error:
  30114. duk__dec_syntax_error(js_ctx);
  30115. DUK_UNREACHABLE();
  30116. }
  30117. #if defined(DUK_USE_JX)
  30118. /* Decode a plain string consisting entirely of identifier characters.
  30119. * Used to parse plain keys (e.g. "foo: 123").
  30120. */
  30121. DUK_LOCAL void duk__dec_plain_string(duk_json_dec_ctx *js_ctx) {
  30122. duk_hthread *thr = js_ctx->thr;
  30123. const duk_uint8_t *p;
  30124. duk_small_int_t x;
  30125. /* Caller has already eaten the first char so backtrack one byte. */
  30126. js_ctx->p--; /* safe */
  30127. p = js_ctx->p;
  30128. /* Here again we parse bytes, and non-ASCII UTF-8 will cause end of
  30129. * parsing (which is correct except if there are non-shortest encodings).
  30130. * There is also no need to check explicitly for end of input buffer as
  30131. * the input is NUL padded and NUL will exit the parsing loop.
  30132. *
  30133. * Because no unescaping takes place, we can just scan to the end of the
  30134. * plain string and intern from the input buffer.
  30135. */
  30136. for (;;) {
  30137. x = *p;
  30138. /* There is no need to check the first character specially here
  30139. * (i.e. reject digits): the caller only accepts valid initial
  30140. * characters and won't call us if the first character is a digit.
  30141. * This also ensures that the plain string won't be empty.
  30142. */
  30143. if (!duk_unicode_is_identifier_part((duk_codepoint_t) x)) {
  30144. break;
  30145. }
  30146. p++;
  30147. }
  30148. duk_push_lstring(thr, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p));
  30149. js_ctx->p = p;
  30150. /* [ ... str ] */
  30151. }
  30152. #endif /* DUK_USE_JX */
  30153. #if defined(DUK_USE_JX)
  30154. DUK_LOCAL void duk__dec_pointer(duk_json_dec_ctx *js_ctx) {
  30155. duk_hthread *thr = js_ctx->thr;
  30156. const duk_uint8_t *p;
  30157. duk_small_int_t x;
  30158. void *voidptr;
  30159. /* Caller has already eaten the first character ('(') which we don't need. */
  30160. p = js_ctx->p;
  30161. for (;;) {
  30162. x = *p;
  30163. /* Assume that the native representation never contains a closing
  30164. * parenthesis.
  30165. */
  30166. if (x == DUK_ASC_RPAREN) {
  30167. break;
  30168. } else if (x <= 0) {
  30169. /* NUL term or -1 (EOF), NUL check would suffice */
  30170. goto syntax_error;
  30171. }
  30172. p++;
  30173. }
  30174. /* There is no need to NUL delimit the sscanf() call: trailing garbage is
  30175. * ignored and there is always a NUL terminator which will force an error
  30176. * if no error is encountered before it. It's possible that the scan
  30177. * would scan further than between [js_ctx->p,p[ though and we'd advance
  30178. * by less than the scanned value.
  30179. *
  30180. * Because pointers are platform specific, a failure to scan a pointer
  30181. * results in a null pointer which is a better placeholder than a missing
  30182. * value or an error.
  30183. */
  30184. voidptr = NULL;
  30185. (void) DUK_SSCANF((const char *) js_ctx->p, DUK_STR_FMT_PTR, &voidptr);
  30186. duk_push_pointer(thr, voidptr);
  30187. js_ctx->p = p + 1; /* skip ')' */
  30188. /* [ ... ptr ] */
  30189. return;
  30190. syntax_error:
  30191. duk__dec_syntax_error(js_ctx);
  30192. DUK_UNREACHABLE();
  30193. }
  30194. #endif /* DUK_USE_JX */
  30195. #if defined(DUK_USE_JX)
  30196. DUK_LOCAL void duk__dec_buffer(duk_json_dec_ctx *js_ctx) {
  30197. duk_hthread *thr = js_ctx->thr;
  30198. const duk_uint8_t *p;
  30199. duk_uint8_t *buf;
  30200. duk_size_t src_len;
  30201. duk_small_int_t x;
  30202. /* Caller has already eaten the first character ('|') which we don't need. */
  30203. p = js_ctx->p;
  30204. /* XXX: Would be nice to share the fast path loop from duk_hex_decode()
  30205. * and avoid creating a temporary buffer. However, there are some
  30206. * differences which prevent trivial sharing:
  30207. *
  30208. * - Pipe char detection
  30209. * - EOF detection
  30210. * - Unknown length of input and output
  30211. *
  30212. * The best approach here would be a bufwriter and a reasonaly sized
  30213. * safe inner loop (e.g. 64 output bytes at a time).
  30214. */
  30215. for (;;) {
  30216. x = *p;
  30217. /* This loop intentionally does not ensure characters are valid
  30218. * ([0-9a-fA-F]) because the hex decode call below will do that.
  30219. */
  30220. if (x == DUK_ASC_PIPE) {
  30221. break;
  30222. } else if (x <= 0) {
  30223. /* NUL term or -1 (EOF), NUL check would suffice */
  30224. goto syntax_error;
  30225. }
  30226. p++;
  30227. }
  30228. /* XXX: this is not very nice; unnecessary copy is made. */
  30229. src_len = (duk_size_t) (p - js_ctx->p);
  30230. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, src_len);
  30231. DUK_ASSERT(buf != NULL);
  30232. duk_memcpy((void *) buf, (const void *) js_ctx->p, src_len);
  30233. duk_hex_decode(thr, -1);
  30234. js_ctx->p = p + 1; /* skip '|' */
  30235. /* [ ... buf ] */
  30236. return;
  30237. syntax_error:
  30238. duk__dec_syntax_error(js_ctx);
  30239. DUK_UNREACHABLE();
  30240. }
  30241. #endif /* DUK_USE_JX */
  30242. /* Parse a number, other than NaN or +/- Infinity */
  30243. DUK_LOCAL void duk__dec_number(duk_json_dec_ctx *js_ctx) {
  30244. duk_hthread *thr = js_ctx->thr;
  30245. const duk_uint8_t *p_start;
  30246. const duk_uint8_t *p;
  30247. duk_uint8_t x;
  30248. duk_small_uint_t s2n_flags;
  30249. DUK_DDD(DUK_DDDPRINT("parse_number"));
  30250. p_start = js_ctx->p;
  30251. /* First pass parse is very lenient (e.g. allows '1.2.3') and extracts a
  30252. * string for strict number parsing.
  30253. */
  30254. p = js_ctx->p;
  30255. for (;;) {
  30256. x = *p;
  30257. DUK_DDD(DUK_DDDPRINT("parse_number: p_start=%p, p=%p, p_end=%p, x=%ld",
  30258. (const void *) p_start, (const void *) p,
  30259. (const void *) js_ctx->p_end, (long) x));
  30260. #if defined(DUK_USE_JSON_DECNUMBER_FASTPATH)
  30261. /* This fast path is pretty marginal in practice.
  30262. * XXX: candidate for removal.
  30263. */
  30264. DUK_ASSERT(duk__json_decnumber_lookup[0x00] == 0x00); /* end-of-input breaks */
  30265. if (duk__json_decnumber_lookup[x] == 0) {
  30266. break;
  30267. }
  30268. #else /* DUK_USE_JSON_DECNUMBER_FASTPATH */
  30269. if (!((x >= DUK_ASC_0 && x <= DUK_ASC_9) ||
  30270. (x == DUK_ASC_PERIOD || x == DUK_ASC_LC_E ||
  30271. x == DUK_ASC_UC_E || x == DUK_ASC_MINUS || x == DUK_ASC_PLUS))) {
  30272. /* Plus sign must be accepted for positive exponents
  30273. * (e.g. '1.5e+2'). This clause catches NULs.
  30274. */
  30275. break;
  30276. }
  30277. #endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */
  30278. p++; /* safe, because matched (NUL causes a break) */
  30279. }
  30280. js_ctx->p = p;
  30281. DUK_ASSERT(js_ctx->p > p_start);
  30282. duk_push_lstring(thr, (const char *) p_start, (duk_size_t) (p - p_start));
  30283. s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
  30284. DUK_S2N_FLAG_ALLOW_MINUS | /* but don't allow leading plus */
  30285. DUK_S2N_FLAG_ALLOW_FRAC;
  30286. DUK_DDD(DUK_DDDPRINT("parse_number: string before parsing: %!T",
  30287. (duk_tval *) duk_get_tval(thr, -1)));
  30288. duk_numconv_parse(thr, 10 /*radix*/, s2n_flags);
  30289. if (duk_is_nan(thr, -1)) {
  30290. duk__dec_syntax_error(js_ctx);
  30291. }
  30292. DUK_ASSERT(duk_is_number(thr, -1));
  30293. DUK_DDD(DUK_DDDPRINT("parse_number: final number: %!T",
  30294. (duk_tval *) duk_get_tval(thr, -1)));
  30295. /* [ ... num ] */
  30296. }
  30297. DUK_LOCAL void duk__dec_objarr_entry(duk_json_dec_ctx *js_ctx) {
  30298. duk_hthread *thr = js_ctx->thr;
  30299. duk_require_stack(thr, DUK_JSON_DEC_REQSTACK);
  30300. /* c recursion check */
  30301. DUK_ASSERT_DISABLE(js_ctx->recursion_depth >= 0); /* unsigned */
  30302. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  30303. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  30304. DUK_ERROR_RANGE(thr, DUK_STR_JSONDEC_RECLIMIT);
  30305. DUK_WO_NORETURN(return;);
  30306. }
  30307. js_ctx->recursion_depth++;
  30308. }
  30309. DUK_LOCAL void duk__dec_objarr_exit(duk_json_dec_ctx *js_ctx) {
  30310. /* c recursion check */
  30311. DUK_ASSERT(js_ctx->recursion_depth > 0);
  30312. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  30313. js_ctx->recursion_depth--;
  30314. }
  30315. DUK_LOCAL void duk__dec_object(duk_json_dec_ctx *js_ctx) {
  30316. duk_hthread *thr = js_ctx->thr;
  30317. duk_int_t key_count; /* XXX: a "first" flag would suffice */
  30318. duk_uint8_t x;
  30319. DUK_DDD(DUK_DDDPRINT("parse_object"));
  30320. duk__dec_objarr_entry(js_ctx);
  30321. duk_push_object(thr);
  30322. /* Initial '{' has been checked and eaten by caller. */
  30323. key_count = 0;
  30324. for (;;) {
  30325. x = duk__dec_get_nonwhite(js_ctx);
  30326. DUK_DDD(DUK_DDDPRINT("parse_object: obj=%!T, x=%ld, key_count=%ld",
  30327. (duk_tval *) duk_get_tval(thr, -1),
  30328. (long) x, (long) key_count));
  30329. /* handle comma and closing brace */
  30330. if (x == DUK_ASC_COMMA && key_count > 0) {
  30331. /* accept comma, expect new value */
  30332. x = duk__dec_get_nonwhite(js_ctx);
  30333. } else if (x == DUK_ASC_RCURLY) {
  30334. /* eat closing brace */
  30335. break;
  30336. } else if (key_count == 0) {
  30337. /* accept anything, expect first value (EOF will be
  30338. * caught by key parsing below.
  30339. */
  30340. ;
  30341. } else {
  30342. /* catches EOF (NUL) and initial comma */
  30343. goto syntax_error;
  30344. }
  30345. /* parse key and value */
  30346. if (x == DUK_ASC_DOUBLEQUOTE) {
  30347. duk__dec_string(js_ctx);
  30348. #if defined(DUK_USE_JX)
  30349. } else if (js_ctx->flag_ext_custom &&
  30350. duk_unicode_is_identifier_start((duk_codepoint_t) x)) {
  30351. duk__dec_plain_string(js_ctx);
  30352. #endif
  30353. } else {
  30354. goto syntax_error;
  30355. }
  30356. /* [ ... obj key ] */
  30357. x = duk__dec_get_nonwhite(js_ctx);
  30358. if (x != DUK_ASC_COLON) {
  30359. goto syntax_error;
  30360. }
  30361. duk__dec_value(js_ctx);
  30362. /* [ ... obj key val ] */
  30363. duk_xdef_prop_wec(thr, -3);
  30364. /* [ ... obj ] */
  30365. key_count++;
  30366. }
  30367. /* [ ... obj ] */
  30368. DUK_DDD(DUK_DDDPRINT("parse_object: final object is %!T",
  30369. (duk_tval *) duk_get_tval(thr, -1)));
  30370. duk__dec_objarr_exit(js_ctx);
  30371. return;
  30372. syntax_error:
  30373. duk__dec_syntax_error(js_ctx);
  30374. DUK_UNREACHABLE();
  30375. }
  30376. DUK_LOCAL void duk__dec_array(duk_json_dec_ctx *js_ctx) {
  30377. duk_hthread *thr = js_ctx->thr;
  30378. duk_uarridx_t arr_idx;
  30379. duk_uint8_t x;
  30380. DUK_DDD(DUK_DDDPRINT("parse_array"));
  30381. duk__dec_objarr_entry(js_ctx);
  30382. duk_push_array(thr);
  30383. /* Initial '[' has been checked and eaten by caller. */
  30384. arr_idx = 0;
  30385. for (;;) {
  30386. x = duk__dec_get_nonwhite(js_ctx);
  30387. DUK_DDD(DUK_DDDPRINT("parse_array: arr=%!T, x=%ld, arr_idx=%ld",
  30388. (duk_tval *) duk_get_tval(thr, -1),
  30389. (long) x, (long) arr_idx));
  30390. /* handle comma and closing bracket */
  30391. if ((x == DUK_ASC_COMMA) && (arr_idx != 0)) {
  30392. /* accept comma, expect new value */
  30393. ;
  30394. } else if (x == DUK_ASC_RBRACKET) {
  30395. /* eat closing bracket */
  30396. break;
  30397. } else if (arr_idx == 0) {
  30398. /* accept anything, expect first value (EOF will be
  30399. * caught by duk__dec_value() below.
  30400. */
  30401. js_ctx->p--; /* backtrack (safe) */
  30402. } else {
  30403. /* catches EOF (NUL) and initial comma */
  30404. goto syntax_error;
  30405. }
  30406. /* parse value */
  30407. duk__dec_value(js_ctx);
  30408. /* [ ... arr val ] */
  30409. duk_xdef_prop_index_wec(thr, -2, arr_idx);
  30410. arr_idx++;
  30411. }
  30412. /* Must set 'length' explicitly when using duk_xdef_prop_xxx() to
  30413. * set the values.
  30414. */
  30415. duk_set_length(thr, -1, arr_idx);
  30416. /* [ ... arr ] */
  30417. DUK_DDD(DUK_DDDPRINT("parse_array: final array is %!T",
  30418. (duk_tval *) duk_get_tval(thr, -1)));
  30419. duk__dec_objarr_exit(js_ctx);
  30420. return;
  30421. syntax_error:
  30422. duk__dec_syntax_error(js_ctx);
  30423. DUK_UNREACHABLE();
  30424. }
  30425. DUK_LOCAL void duk__dec_value(duk_json_dec_ctx *js_ctx) {
  30426. duk_hthread *thr = js_ctx->thr;
  30427. duk_uint8_t x;
  30428. x = duk__dec_get_nonwhite(js_ctx);
  30429. DUK_DDD(DUK_DDDPRINT("parse_value: initial x=%ld", (long) x));
  30430. /* Note: duk__dec_req_stridx() backtracks one char */
  30431. if (x == DUK_ASC_DOUBLEQUOTE) {
  30432. duk__dec_string(js_ctx);
  30433. } else if ((x >= DUK_ASC_0 && x <= DUK_ASC_9) || (x == DUK_ASC_MINUS)) {
  30434. #if defined(DUK_USE_JX)
  30435. if (js_ctx->flag_ext_custom && x == DUK_ASC_MINUS && duk__dec_peek(js_ctx) == DUK_ASC_UC_I) {
  30436. duk__dec_req_stridx(js_ctx, DUK_STRIDX_MINUS_INFINITY); /* "-Infinity", '-' has been eaten */
  30437. duk_push_number(thr, -DUK_DOUBLE_INFINITY);
  30438. } else {
  30439. #else
  30440. { /* unconditional block */
  30441. #endif
  30442. /* We already ate 'x', so backup one byte. */
  30443. js_ctx->p--; /* safe */
  30444. duk__dec_number(js_ctx);
  30445. }
  30446. } else if (x == DUK_ASC_LC_T) {
  30447. duk__dec_req_stridx(js_ctx, DUK_STRIDX_TRUE);
  30448. duk_push_true(thr);
  30449. } else if (x == DUK_ASC_LC_F) {
  30450. duk__dec_req_stridx(js_ctx, DUK_STRIDX_FALSE);
  30451. duk_push_false(thr);
  30452. } else if (x == DUK_ASC_LC_N) {
  30453. duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_NULL);
  30454. duk_push_null(thr);
  30455. #if defined(DUK_USE_JX)
  30456. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LC_U) {
  30457. duk__dec_req_stridx(js_ctx, DUK_STRIDX_LC_UNDEFINED);
  30458. duk_push_undefined(thr);
  30459. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_N) {
  30460. duk__dec_req_stridx(js_ctx, DUK_STRIDX_NAN);
  30461. duk_push_nan(thr);
  30462. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_I) {
  30463. duk__dec_req_stridx(js_ctx, DUK_STRIDX_INFINITY);
  30464. duk_push_number(thr, DUK_DOUBLE_INFINITY);
  30465. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_LPAREN) {
  30466. duk__dec_pointer(js_ctx);
  30467. } else if (js_ctx->flag_ext_custom && x == DUK_ASC_PIPE) {
  30468. duk__dec_buffer(js_ctx);
  30469. #endif
  30470. } else if (x == DUK_ASC_LCURLY) {
  30471. duk__dec_object(js_ctx);
  30472. } else if (x == DUK_ASC_LBRACKET) {
  30473. duk__dec_array(js_ctx);
  30474. } else {
  30475. /* catches EOF (NUL) */
  30476. goto syntax_error;
  30477. }
  30478. duk__dec_eat_white(js_ctx);
  30479. /* [ ... val ] */
  30480. return;
  30481. syntax_error:
  30482. duk__dec_syntax_error(js_ctx);
  30483. DUK_UNREACHABLE();
  30484. }
  30485. /* Recursive value reviver, implements the Walk() algorithm. No C recursion
  30486. * check is done here because the initial parsing step will already ensure
  30487. * there is a reasonable limit on C recursion depth and hence object depth.
  30488. */
  30489. DUK_LOCAL void duk__dec_reviver_walk(duk_json_dec_ctx *js_ctx) {
  30490. duk_hthread *thr = js_ctx->thr;
  30491. duk_hobject *h;
  30492. duk_uarridx_t i, arr_len;
  30493. DUK_DDD(DUK_DDDPRINT("walk: top=%ld, holder=%!T, name=%!T",
  30494. (long) duk_get_top(thr),
  30495. (duk_tval *) duk_get_tval(thr, -2),
  30496. (duk_tval *) duk_get_tval(thr, -1)));
  30497. duk_dup_top(thr);
  30498. duk_get_prop(thr, -3); /* -> [ ... holder name val ] */
  30499. h = duk_get_hobject(thr, -1);
  30500. if (h != NULL) {
  30501. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  30502. arr_len = (duk_uarridx_t) duk_get_length(thr, -1);
  30503. for (i = 0; i < arr_len; i++) {
  30504. /* [ ... holder name val ] */
  30505. DUK_DDD(DUK_DDDPRINT("walk: array, top=%ld, i=%ld, arr_len=%ld, holder=%!T, name=%!T, val=%!T",
  30506. (long) duk_get_top(thr), (long) i, (long) arr_len,
  30507. (duk_tval *) duk_get_tval(thr, -3), (duk_tval *) duk_get_tval(thr, -2),
  30508. (duk_tval *) duk_get_tval(thr, -1)));
  30509. duk_dup_top(thr);
  30510. (void) duk_push_uint_to_hstring(thr, (duk_uint_t) i); /* -> [ ... holder name val val ToString(i) ] */
  30511. duk__dec_reviver_walk(js_ctx); /* -> [ ... holder name val new_elem ] */
  30512. if (duk_is_undefined(thr, -1)) {
  30513. duk_pop(thr);
  30514. duk_del_prop_index(thr, -1, i);
  30515. } else {
  30516. /* XXX: duk_xdef_prop_index_wec() would be more appropriate
  30517. * here but it currently makes some assumptions that might
  30518. * not hold (e.g. that previous property is not an accessor).
  30519. */
  30520. duk_put_prop_index(thr, -2, i);
  30521. }
  30522. }
  30523. } else {
  30524. /* [ ... holder name val ] */
  30525. duk_enum(thr, -1, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/);
  30526. while (duk_next(thr, -1 /*enum_index*/, 0 /*get_value*/)) {
  30527. DUK_DDD(DUK_DDDPRINT("walk: object, top=%ld, holder=%!T, name=%!T, val=%!T, enum=%!iT, obj_key=%!T",
  30528. (long) duk_get_top(thr), (duk_tval *) duk_get_tval(thr, -5),
  30529. (duk_tval *) duk_get_tval(thr, -4), (duk_tval *) duk_get_tval(thr, -3),
  30530. (duk_tval *) duk_get_tval(thr, -2), (duk_tval *) duk_get_tval(thr, -1)));
  30531. /* [ ... holder name val enum obj_key ] */
  30532. duk_dup_m3(thr);
  30533. duk_dup_m2(thr);
  30534. /* [ ... holder name val enum obj_key val obj_key ] */
  30535. duk__dec_reviver_walk(js_ctx);
  30536. /* [ ... holder name val enum obj_key new_elem ] */
  30537. if (duk_is_undefined(thr, -1)) {
  30538. duk_pop(thr);
  30539. duk_del_prop(thr, -3);
  30540. } else {
  30541. /* XXX: duk_xdef_prop_index_wec() would be more appropriate
  30542. * here but it currently makes some assumptions that might
  30543. * not hold (e.g. that previous property is not an accessor).
  30544. *
  30545. * Using duk_put_prop() works incorrectly with '__proto__'
  30546. * if the own property with that name has been deleted. This
  30547. * does not happen normally, but a clever reviver can trigger
  30548. * that, see complex reviver case in: test-bug-json-parse-__proto__.js.
  30549. */
  30550. duk_put_prop(thr, -4);
  30551. }
  30552. }
  30553. duk_pop(thr); /* pop enum */
  30554. }
  30555. }
  30556. /* [ ... holder name val ] */
  30557. duk_dup(thr, js_ctx->idx_reviver);
  30558. duk_insert(thr, -4); /* -> [ ... reviver holder name val ] */
  30559. duk_call_method(thr, 2); /* -> [ ... res ] */
  30560. DUK_DDD(DUK_DDDPRINT("walk: top=%ld, result=%!T",
  30561. (long) duk_get_top(thr), (duk_tval *) duk_get_tval(thr, -1)));
  30562. }
  30563. /*
  30564. * Stringify implementation.
  30565. */
  30566. #define DUK__EMIT_1(js_ctx,ch) duk__emit_1((js_ctx), (duk_uint_fast8_t) (ch))
  30567. #define DUK__EMIT_2(js_ctx,ch1,ch2) duk__emit_2((js_ctx), (duk_uint_fast8_t) (ch1), (duk_uint_fast8_t) (ch2))
  30568. #define DUK__EMIT_HSTR(js_ctx,h) duk__emit_hstring((js_ctx), (h))
  30569. #if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30570. #define DUK__EMIT_CSTR(js_ctx,p) duk__emit_cstring((js_ctx), (p))
  30571. #endif
  30572. #define DUK__EMIT_STRIDX(js_ctx,i) duk__emit_stridx((js_ctx), (i))
  30573. #define DUK__UNEMIT_1(js_ctx) duk__unemit_1((js_ctx))
  30574. DUK_LOCAL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch) {
  30575. DUK_BW_WRITE_ENSURE_U8(js_ctx->thr, &js_ctx->bw, ch);
  30576. }
  30577. DUK_LOCAL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2) {
  30578. DUK_BW_WRITE_ENSURE_U8_2(js_ctx->thr, &js_ctx->bw, ch1, ch2);
  30579. }
  30580. DUK_LOCAL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h) {
  30581. DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h);
  30582. }
  30583. #if defined(DUK_USE_FASTINT) || defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30584. DUK_LOCAL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *str) {
  30585. DUK_BW_WRITE_ENSURE_CSTRING(js_ctx->thr, &js_ctx->bw, str);
  30586. }
  30587. #endif
  30588. DUK_LOCAL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx) {
  30589. duk_hstring *h;
  30590. DUK_ASSERT_STRIDX_VALID(stridx);
  30591. h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
  30592. DUK_ASSERT(h != NULL);
  30593. DUK_BW_WRITE_ENSURE_HSTRING(js_ctx->thr, &js_ctx->bw, h);
  30594. }
  30595. DUK_LOCAL void duk__unemit_1(duk_json_enc_ctx *js_ctx) {
  30596. DUK_ASSERT(DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw) >= 1);
  30597. DUK_BW_ADD_PTR(js_ctx->thr, &js_ctx->bw, -1);
  30598. }
  30599. #define DUK__MKESC(nybbles,esc1,esc2) \
  30600. (((duk_uint_fast32_t) (nybbles)) << 16) | \
  30601. (((duk_uint_fast32_t) (esc1)) << 8) | \
  30602. ((duk_uint_fast32_t) (esc2))
  30603. DUK_LOCAL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q) {
  30604. duk_uint_fast32_t tmp;
  30605. duk_small_uint_t dig;
  30606. DUK_UNREF(js_ctx);
  30607. /* Caller ensures space for at least DUK__JSON_MAX_ESC_LEN. */
  30608. /* Select appropriate escape format automatically, and set 'tmp' to a
  30609. * value encoding both the escape format character and the nybble count:
  30610. *
  30611. * (nybble_count << 16) | (escape_char1) | (escape_char2)
  30612. */
  30613. #if defined(DUK_USE_JX)
  30614. if (DUK_LIKELY(cp < 0x100UL)) {
  30615. if (DUK_UNLIKELY(js_ctx->flag_ext_custom != 0U)) {
  30616. tmp = DUK__MKESC(2, DUK_ASC_BACKSLASH, DUK_ASC_LC_X);
  30617. } else {
  30618. tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U);
  30619. }
  30620. } else
  30621. #endif
  30622. if (DUK_LIKELY(cp < 0x10000UL)) {
  30623. tmp = DUK__MKESC(4, DUK_ASC_BACKSLASH, DUK_ASC_LC_U);
  30624. } else {
  30625. #if defined(DUK_USE_JX)
  30626. if (DUK_LIKELY(js_ctx->flag_ext_custom != 0U)) {
  30627. tmp = DUK__MKESC(8, DUK_ASC_BACKSLASH, DUK_ASC_UC_U);
  30628. } else
  30629. #endif
  30630. {
  30631. /* In compatible mode and standard JSON mode, output
  30632. * something useful for non-BMP characters. This won't
  30633. * roundtrip but will still be more or less readable and
  30634. * more useful than an error.
  30635. */
  30636. tmp = DUK__MKESC(8, DUK_ASC_UC_U, DUK_ASC_PLUS);
  30637. }
  30638. }
  30639. *q++ = (duk_uint8_t) ((tmp >> 8) & 0xff);
  30640. *q++ = (duk_uint8_t) (tmp & 0xff);
  30641. tmp = tmp >> 16;
  30642. while (tmp > 0) {
  30643. tmp--;
  30644. dig = (duk_small_uint_t) ((cp >> (4 * tmp)) & 0x0f);
  30645. *q++ = duk_lc_digits[dig];
  30646. }
  30647. return q;
  30648. }
  30649. DUK_LOCAL void duk__enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k) {
  30650. const duk_int8_t *p, *p_start, *p_end; /* Note: intentionally signed. */
  30651. duk_size_t k_len;
  30652. duk_codepoint_t cp;
  30653. DUK_ASSERT(k != NULL);
  30654. /* Accept ASCII strings which conform to identifier requirements
  30655. * as being emitted without key quotes. Since we only accept ASCII
  30656. * there's no need for actual decoding: 'p' is intentionally signed
  30657. * so that bytes >= 0x80 extend to negative values and are rejected
  30658. * as invalid identifier codepoints.
  30659. */
  30660. if (js_ctx->flag_avoid_key_quotes) {
  30661. k_len = DUK_HSTRING_GET_BYTELEN(k);
  30662. p_start = (const duk_int8_t *) DUK_HSTRING_GET_DATA(k);
  30663. p_end = p_start + k_len;
  30664. p = p_start;
  30665. if (p == p_end) {
  30666. /* Zero length string is not accepted without quotes */
  30667. goto quote_normally;
  30668. }
  30669. cp = (duk_codepoint_t) (*p++);
  30670. if (DUK_UNLIKELY(!duk_unicode_is_identifier_start(cp))) {
  30671. goto quote_normally;
  30672. }
  30673. while (p < p_end) {
  30674. cp = (duk_codepoint_t) (*p++);
  30675. if (DUK_UNLIKELY(!duk_unicode_is_identifier_part(cp))) {
  30676. goto quote_normally;
  30677. }
  30678. }
  30679. /* This seems faster than emitting bytes one at a time and
  30680. * then potentially rewinding.
  30681. */
  30682. DUK__EMIT_HSTR(js_ctx, k);
  30683. return;
  30684. }
  30685. quote_normally:
  30686. duk__enc_quote_string(js_ctx, k);
  30687. }
  30688. /* The Quote(value) operation: quote a string.
  30689. *
  30690. * Stack policy: [ ] -> [ ].
  30691. */
  30692. DUK_LOCAL void duk__enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str) {
  30693. duk_hthread *thr = js_ctx->thr;
  30694. const duk_uint8_t *p, *p_start, *p_end, *p_now, *p_tmp;
  30695. duk_uint8_t *q;
  30696. duk_ucodepoint_t cp; /* typed for duk_unicode_decode_xutf8() */
  30697. DUK_DDD(DUK_DDDPRINT("duk__enc_quote_string: h_str=%!O", (duk_heaphdr *) h_str));
  30698. DUK_ASSERT(h_str != NULL);
  30699. p_start = DUK_HSTRING_GET_DATA(h_str);
  30700. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_str);
  30701. p = p_start;
  30702. DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE);
  30703. /* Encode string in small chunks, estimating the maximum expansion so that
  30704. * there's no need to ensure space while processing the chunk.
  30705. */
  30706. while (p < p_end) {
  30707. duk_size_t left, now, space;
  30708. left = (duk_size_t) (p_end - p);
  30709. now = (left > DUK__JSON_ENCSTR_CHUNKSIZE ?
  30710. DUK__JSON_ENCSTR_CHUNKSIZE : left);
  30711. /* Maximum expansion per input byte is 6:
  30712. * - invalid UTF-8 byte causes "\uXXXX" to be emitted (6/1 = 6).
  30713. * - 2-byte UTF-8 encodes as "\uXXXX" (6/2 = 3).
  30714. * - 4-byte UTF-8 encodes as "\Uxxxxxxxx" (10/4 = 2.5).
  30715. */
  30716. space = now * 6;
  30717. q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space);
  30718. p_now = p + now;
  30719. while (p < p_now) {
  30720. #if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH)
  30721. duk_uint8_t b;
  30722. b = duk__json_quotestr_lookup[*p++];
  30723. if (DUK_LIKELY(b < 0x80)) {
  30724. /* Most input bytes go through here. */
  30725. *q++ = b;
  30726. } else if (b >= 0xa0) {
  30727. *q++ = DUK_ASC_BACKSLASH;
  30728. *q++ = (duk_uint8_t) (b - 0x80);
  30729. } else if (b == 0x80) {
  30730. cp = (duk_ucodepoint_t) (*(p - 1));
  30731. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  30732. } else if (b == 0x7f && js_ctx->flag_ascii_only) {
  30733. /* 0x7F is special */
  30734. DUK_ASSERT(b == 0x81);
  30735. cp = (duk_ucodepoint_t) 0x7f;
  30736. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  30737. } else {
  30738. DUK_ASSERT(b == 0x81);
  30739. p--;
  30740. /* slow path is shared */
  30741. #else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  30742. cp = *p;
  30743. if (DUK_LIKELY(cp <= 0x7f)) {
  30744. /* ascii fast path: avoid decoding utf-8 */
  30745. p++;
  30746. if (cp == 0x22 || cp == 0x5c) {
  30747. /* double quote or backslash */
  30748. *q++ = DUK_ASC_BACKSLASH;
  30749. *q++ = (duk_uint8_t) cp;
  30750. } else if (cp < 0x20) {
  30751. duk_uint_fast8_t esc_char;
  30752. /* This approach is a bit shorter than a straight
  30753. * if-else-ladder and also a bit faster.
  30754. */
  30755. if (cp < (sizeof(duk__json_quotestr_esc) / sizeof(duk_uint8_t)) &&
  30756. (esc_char = duk__json_quotestr_esc[cp]) != 0) {
  30757. *q++ = DUK_ASC_BACKSLASH;
  30758. *q++ = (duk_uint8_t) esc_char;
  30759. } else {
  30760. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  30761. }
  30762. } else if (cp == 0x7f && js_ctx->flag_ascii_only) {
  30763. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  30764. } else {
  30765. /* any other printable -> as is */
  30766. *q++ = (duk_uint8_t) cp;
  30767. }
  30768. } else {
  30769. /* slow path is shared */
  30770. #endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
  30771. /* slow path decode */
  30772. /* If XUTF-8 decoding fails, treat the offending byte as a codepoint directly
  30773. * and go forward one byte. This is of course very lossy, but allows some kind
  30774. * of output to be produced even for internal strings which don't conform to
  30775. * XUTF-8. All standard ECMAScript strings are always CESU-8, so this behavior
  30776. * does not violate the ECMAScript specification. The behavior is applied to
  30777. * all modes, including ECMAScript standard JSON. Because the current XUTF-8
  30778. * decoding is not very strict, this behavior only really affects initial bytes
  30779. * and truncated codepoints.
  30780. *
  30781. * Another alternative would be to scan forwards to start of next codepoint
  30782. * (or end of input) and emit just one replacement codepoint.
  30783. */
  30784. p_tmp = p;
  30785. if (!duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp)) {
  30786. /* Decode failed. */
  30787. cp = *p_tmp;
  30788. p = p_tmp + 1;
  30789. }
  30790. #if defined(DUK_USE_NONSTD_JSON_ESC_U2028_U2029)
  30791. if (js_ctx->flag_ascii_only || cp == 0x2028 || cp == 0x2029) {
  30792. #else
  30793. if (js_ctx->flag_ascii_only) {
  30794. #endif
  30795. q = duk__emit_esc_auto_fast(js_ctx, cp, q);
  30796. } else {
  30797. /* as is */
  30798. DUK_RAW_WRITE_XUTF8(q, cp);
  30799. }
  30800. }
  30801. }
  30802. DUK_BW_SET_PTR(thr, &js_ctx->bw, q);
  30803. }
  30804. DUK__EMIT_1(js_ctx, DUK_ASC_DOUBLEQUOTE);
  30805. }
  30806. /* Encode a double (checked by caller) from stack top. Stack top may be
  30807. * replaced by serialized string but is not popped (caller does that).
  30808. */
  30809. DUK_LOCAL void duk__enc_double(duk_json_enc_ctx *js_ctx) {
  30810. duk_hthread *thr;
  30811. duk_tval *tv;
  30812. duk_double_t d;
  30813. duk_small_int_t c;
  30814. duk_small_int_t s;
  30815. duk_small_uint_t stridx;
  30816. duk_small_uint_t n2s_flags;
  30817. duk_hstring *h_str;
  30818. DUK_ASSERT(js_ctx != NULL);
  30819. thr = js_ctx->thr;
  30820. DUK_ASSERT(thr != NULL);
  30821. /* Caller must ensure 'tv' is indeed a double and not a fastint! */
  30822. tv = DUK_GET_TVAL_NEGIDX(thr, -1);
  30823. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  30824. d = DUK_TVAL_GET_DOUBLE(tv);
  30825. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  30826. s = (duk_small_int_t) DUK_SIGNBIT(d);
  30827. DUK_UNREF(s);
  30828. if (DUK_LIKELY(!(c == DUK_FP_INFINITE || c == DUK_FP_NAN))) {
  30829. DUK_ASSERT(DUK_ISFINITE(d));
  30830. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30831. /* Negative zero needs special handling in JX/JC because
  30832. * it would otherwise serialize to '0', not '-0'.
  30833. */
  30834. if (DUK_UNLIKELY(c == DUK_FP_ZERO && s != 0 &&
  30835. (js_ctx->flag_ext_custom_or_compatible))) {
  30836. duk_push_hstring_stridx(thr, DUK_STRIDX_MINUS_ZERO); /* '-0' */
  30837. } else
  30838. #endif /* DUK_USE_JX || DUK_USE_JC */
  30839. {
  30840. n2s_flags = 0;
  30841. /* [ ... number ] -> [ ... string ] */
  30842. duk_numconv_stringify(thr, 10 /*radix*/, 0 /*digits*/, n2s_flags);
  30843. }
  30844. h_str = duk_known_hstring(thr, -1);
  30845. DUK__EMIT_HSTR(js_ctx, h_str);
  30846. return;
  30847. }
  30848. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30849. if (!(js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  30850. DUK_JSON_FLAG_EXT_COMPATIBLE))) {
  30851. stridx = DUK_STRIDX_LC_NULL;
  30852. } else if (c == DUK_FP_NAN) {
  30853. stridx = js_ctx->stridx_custom_nan;
  30854. } else if (s == 0) {
  30855. stridx = js_ctx->stridx_custom_posinf;
  30856. } else {
  30857. stridx = js_ctx->stridx_custom_neginf;
  30858. }
  30859. #else
  30860. stridx = DUK_STRIDX_LC_NULL;
  30861. #endif
  30862. DUK__EMIT_STRIDX(js_ctx, stridx);
  30863. }
  30864. #if defined(DUK_USE_FASTINT)
  30865. /* Encode a fastint from duk_tval ptr, no value stack effects. */
  30866. DUK_LOCAL void duk__enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv) {
  30867. duk_int64_t v;
  30868. /* Fastint range is signed 48-bit so longest value is -2^47 = -140737488355328
  30869. * (16 chars long), longest signed 64-bit value is -2^63 = -9223372036854775808
  30870. * (20 chars long). Alloc space for 64-bit range to be safe.
  30871. */
  30872. duk_uint8_t buf[20 + 1];
  30873. /* Caller must ensure 'tv' is indeed a fastint! */
  30874. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  30875. v = DUK_TVAL_GET_FASTINT(tv);
  30876. /* XXX: There are no format strings in duk_config.h yet, could add
  30877. * one for formatting duk_int64_t. For now, assumes "%lld" and that
  30878. * "long long" type exists. Could also rely on C99 directly but that
  30879. * won't work for older MSVC.
  30880. */
  30881. DUK_SPRINTF((char *) buf, "%lld", (long long) v);
  30882. DUK__EMIT_CSTR(js_ctx, (const char *) buf);
  30883. }
  30884. #endif
  30885. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  30886. #if defined(DUK_USE_HEX_FASTPATH)
  30887. DUK_LOCAL duk_uint8_t *duk__enc_buffer_data_hex(const duk_uint8_t *src, duk_size_t src_len, duk_uint8_t *dst) {
  30888. duk_uint8_t *q;
  30889. duk_uint16_t *q16;
  30890. duk_small_uint_t x;
  30891. duk_size_t i, len_safe;
  30892. #if !defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE)
  30893. duk_bool_t shift_dst;
  30894. #endif
  30895. /* Unlike in duk_hex_encode() 'dst' is not necessarily aligned by 2.
  30896. * For platforms where unaligned accesses are not allowed, shift 'dst'
  30897. * ahead by 1 byte to get alignment and then duk_memmove() the result
  30898. * in place. The faster encoding loop makes up the difference.
  30899. * There's always space for one extra byte because a terminator always
  30900. * follows the hex data and that's been accounted for by the caller.
  30901. */
  30902. #if defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE)
  30903. q16 = (duk_uint16_t *) (void *) dst;
  30904. #else
  30905. shift_dst = (duk_bool_t) (((duk_size_t) dst) & 0x01U);
  30906. if (shift_dst) {
  30907. DUK_DD(DUK_DDPRINT("unaligned accesses not possible, dst not aligned -> step to dst + 1"));
  30908. q16 = (duk_uint16_t *) (void *) (dst + 1);
  30909. } else {
  30910. DUK_DD(DUK_DDPRINT("unaligned accesses not possible, dst is aligned"));
  30911. q16 = (duk_uint16_t *) (void *) dst;
  30912. }
  30913. DUK_ASSERT((((duk_size_t) q16) & 0x01U) == 0);
  30914. #endif
  30915. len_safe = src_len & ~0x03U;
  30916. for (i = 0; i < len_safe; i += 4) {
  30917. q16[0] = duk_hex_enctab[src[i]];
  30918. q16[1] = duk_hex_enctab[src[i + 1]];
  30919. q16[2] = duk_hex_enctab[src[i + 2]];
  30920. q16[3] = duk_hex_enctab[src[i + 3]];
  30921. q16 += 4;
  30922. }
  30923. q = (duk_uint8_t *) q16;
  30924. #if !defined(DUK_USE_UNALIGNED_ACCESSES_POSSIBLE)
  30925. if (shift_dst) {
  30926. q--;
  30927. duk_memmove((void *) dst, (const void *) (dst + 1), 2 * len_safe);
  30928. DUK_ASSERT(dst + 2 * len_safe == q);
  30929. }
  30930. #endif
  30931. for (; i < src_len; i++) {
  30932. x = src[i];
  30933. *q++ = duk_lc_digits[x >> 4];
  30934. *q++ = duk_lc_digits[x & 0x0f];
  30935. }
  30936. return q;
  30937. }
  30938. #else /* DUK_USE_HEX_FASTPATH */
  30939. DUK_LOCAL duk_uint8_t *duk__enc_buffer_data_hex(const duk_uint8_t *src, duk_size_t src_len, duk_uint8_t *dst) {
  30940. const duk_uint8_t *p;
  30941. const duk_uint8_t *p_end;
  30942. duk_uint8_t *q;
  30943. duk_small_uint_t x;
  30944. p = src;
  30945. p_end = src + src_len;
  30946. q = dst;
  30947. while (p != p_end) {
  30948. x = *p++;
  30949. *q++ = duk_lc_digits[x >> 4];
  30950. *q++ = duk_lc_digits[x & 0x0f];
  30951. }
  30952. return q;
  30953. }
  30954. #endif /* DUK_USE_HEX_FASTPATH */
  30955. DUK_LOCAL void duk__enc_buffer_data(duk_json_enc_ctx *js_ctx, duk_uint8_t *buf_data, duk_size_t buf_len) {
  30956. duk_hthread *thr;
  30957. duk_uint8_t *q;
  30958. duk_size_t space;
  30959. thr = js_ctx->thr;
  30960. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); /* caller checks */
  30961. DUK_ASSERT(js_ctx->flag_ext_custom_or_compatible);
  30962. /* Buffer values are encoded in (lowercase) hex to make the
  30963. * binary data readable. Base64 or similar would be more
  30964. * compact but less readable, and the point of JX/JC
  30965. * variants is to be as useful to a programmer as possible.
  30966. */
  30967. /* The #if defined() clutter here needs to handle the three
  30968. * cases: (1) JX+JC, (2) JX only, (3) JC only.
  30969. */
  30970. /* Note: space must cater for both JX and JC. */
  30971. space = 9 + buf_len * 2 + 2;
  30972. DUK_ASSERT(DUK_HBUFFER_MAX_BYTELEN <= 0x7ffffffeUL);
  30973. DUK_ASSERT((space - 2) / 2 >= buf_len); /* overflow not possible, buffer limits */
  30974. q = DUK_BW_ENSURE_GETPTR(thr, &js_ctx->bw, space);
  30975. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  30976. if (js_ctx->flag_ext_custom)
  30977. #endif
  30978. #if defined(DUK_USE_JX)
  30979. {
  30980. *q++ = DUK_ASC_PIPE;
  30981. q = duk__enc_buffer_data_hex(buf_data, buf_len, q);
  30982. *q++ = DUK_ASC_PIPE;
  30983. }
  30984. #endif
  30985. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  30986. else
  30987. #endif
  30988. #if defined(DUK_USE_JC)
  30989. {
  30990. DUK_ASSERT(js_ctx->flag_ext_compatible);
  30991. duk_memcpy((void *) q, (const void *) "{\"_buf\":\"", 9); /* len: 9 */
  30992. q += 9;
  30993. q = duk__enc_buffer_data_hex(buf_data, buf_len, q);
  30994. *q++ = DUK_ASC_DOUBLEQUOTE;
  30995. *q++ = DUK_ASC_RCURLY;
  30996. }
  30997. #endif
  30998. DUK_BW_SET_PTR(thr, &js_ctx->bw, q);
  30999. }
  31000. DUK_LOCAL void duk__enc_buffer_jx_jc(duk_json_enc_ctx *js_ctx, duk_hbuffer *h) {
  31001. duk__enc_buffer_data(js_ctx,
  31002. (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(js_ctx->thr->heap, h),
  31003. (duk_size_t) DUK_HBUFFER_GET_SIZE(h));
  31004. }
  31005. #endif /* DUK_USE_JX || DUK_USE_JC */
  31006. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  31007. DUK_LOCAL void duk__enc_buffer_json_fastpath(duk_json_enc_ctx *js_ctx, duk_hbuffer *h) {
  31008. duk_size_t i, n;
  31009. const duk_uint8_t *buf;
  31010. duk_uint8_t *q;
  31011. n = DUK_HBUFFER_GET_SIZE(h);
  31012. if (n == 0) {
  31013. DUK__EMIT_2(js_ctx, DUK_ASC_LCURLY, DUK_ASC_RCURLY);
  31014. return;
  31015. }
  31016. DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY);
  31017. /* Maximum encoded length with 32-bit index: 1 + 10 + 2 + 3 + 1 + 1 = 18,
  31018. * with 64-bit index: 1 + 20 + 2 + 3 + 1 + 1 = 28. 32 has some slack.
  31019. *
  31020. * Note that because the output buffer is reallocated from time to time,
  31021. * side effects (such as finalizers) affecting the buffer 'h' must be
  31022. * disabled. This is the case in the JSON.stringify() fast path.
  31023. */
  31024. buf = (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(js_ctx->thr->heap, h);
  31025. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31026. for (i = 0; i < n; i++) {
  31027. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth + 1);
  31028. q = DUK_BW_ENSURE_GETPTR(js_ctx->thr, &js_ctx->bw, 32);
  31029. q += DUK_SPRINTF((char *) q, "\"%lu\": %u,", (unsigned long) i, (unsigned int) buf[i]);
  31030. DUK_BW_SET_PTR(js_ctx->thr, &js_ctx->bw, q);
  31031. }
  31032. } else {
  31033. q = DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw);
  31034. for (i = 0; i < n; i++) {
  31035. q = DUK_BW_ENSURE_RAW(js_ctx->thr, &js_ctx->bw, 32, q);
  31036. q += DUK_SPRINTF((char *) q, "\"%lu\":%u,", (unsigned long) i, (unsigned int) buf[i]);
  31037. }
  31038. DUK_BW_SET_PTR(js_ctx->thr, &js_ctx->bw, q);
  31039. }
  31040. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  31041. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31042. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth);
  31043. }
  31044. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  31045. }
  31046. #endif /* DUK_USE_JSON_STRINGIFY_FASTPATH */
  31047. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31048. DUK_LOCAL void duk__enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr) {
  31049. char buf[64]; /* XXX: how to figure correct size? */
  31050. const char *fmt;
  31051. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible); /* caller checks */
  31052. DUK_ASSERT(js_ctx->flag_ext_custom_or_compatible);
  31053. duk_memzero(buf, sizeof(buf));
  31054. /* The #if defined() clutter here needs to handle the three
  31055. * cases: (1) JX+JC, (2) JX only, (3) JC only.
  31056. */
  31057. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  31058. if (js_ctx->flag_ext_custom)
  31059. #endif
  31060. #if defined(DUK_USE_JX)
  31061. {
  31062. fmt = ptr ? "(%p)" : "(null)";
  31063. }
  31064. #endif
  31065. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  31066. else
  31067. #endif
  31068. #if defined(DUK_USE_JC)
  31069. {
  31070. DUK_ASSERT(js_ctx->flag_ext_compatible);
  31071. fmt = ptr ? "{\"_ptr\":\"%p\"}" : "{\"_ptr\":\"null\"}";
  31072. }
  31073. #endif
  31074. /* When ptr == NULL, the format argument is unused. */
  31075. DUK_SNPRINTF(buf, sizeof(buf) - 1, fmt, ptr); /* must not truncate */
  31076. DUK__EMIT_CSTR(js_ctx, buf);
  31077. }
  31078. #endif /* DUK_USE_JX || DUK_USE_JC */
  31079. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  31080. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31081. DUK_LOCAL void duk__enc_bufobj(duk_json_enc_ctx *js_ctx, duk_hbufobj *h_bufobj) {
  31082. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  31083. if (h_bufobj->buf == NULL || !DUK_HBUFOBJ_VALID_SLICE(h_bufobj)) {
  31084. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  31085. } else {
  31086. /* Handle both full and partial slice (as long as covered). */
  31087. duk__enc_buffer_data(js_ctx,
  31088. (duk_uint8_t *) DUK_HBUFOBJ_GET_SLICE_BASE(js_ctx->thr->heap, h_bufobj),
  31089. (duk_size_t) h_bufobj->length);
  31090. }
  31091. }
  31092. #endif /* DUK_USE_JX || DUK_USE_JC */
  31093. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  31094. /* Indent helper. Calling code relies on js_ctx->recursion_depth also being
  31095. * directly related to indent depth.
  31096. */
  31097. #if defined(DUK_USE_PREFER_SIZE)
  31098. DUK_LOCAL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_uint_t depth) {
  31099. DUK_ASSERT(js_ctx->h_gap != NULL);
  31100. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) > 0); /* caller guarantees */
  31101. DUK__EMIT_1(js_ctx, 0x0a);
  31102. while (depth-- > 0) {
  31103. DUK__EMIT_HSTR(js_ctx, js_ctx->h_gap);
  31104. }
  31105. }
  31106. #else /* DUK_USE_PREFER_SIZE */
  31107. DUK_LOCAL void duk__enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_uint_t depth) {
  31108. const duk_uint8_t *gap_data;
  31109. duk_size_t gap_len;
  31110. duk_size_t avail_bytes; /* bytes of indent available for copying */
  31111. duk_size_t need_bytes; /* bytes of indent still needed */
  31112. duk_uint8_t *p_start;
  31113. duk_uint8_t *p;
  31114. DUK_ASSERT(js_ctx->h_gap != NULL);
  31115. DUK_ASSERT(DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) > 0); /* caller guarantees */
  31116. DUK__EMIT_1(js_ctx, 0x0a);
  31117. if (DUK_UNLIKELY(depth == 0)) {
  31118. return;
  31119. }
  31120. /* To handle deeper indents efficiently, make use of copies we've
  31121. * already emitted. In effect we can emit a sequence of 1, 2, 4,
  31122. * 8, etc copies, and then finish the last run. Byte counters
  31123. * avoid multiply with gap_len on every loop.
  31124. */
  31125. gap_data = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(js_ctx->h_gap);
  31126. gap_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap);
  31127. DUK_ASSERT(gap_len > 0);
  31128. need_bytes = gap_len * depth;
  31129. p = DUK_BW_ENSURE_GETPTR(js_ctx->thr, &js_ctx->bw, need_bytes);
  31130. p_start = p;
  31131. duk_memcpy((void *) p, (const void *) gap_data, (size_t) gap_len);
  31132. p += gap_len;
  31133. avail_bytes = gap_len;
  31134. DUK_ASSERT(need_bytes >= gap_len);
  31135. need_bytes -= gap_len;
  31136. while (need_bytes >= avail_bytes) {
  31137. duk_memcpy((void *) p, (const void *) p_start, (size_t) avail_bytes);
  31138. p += avail_bytes;
  31139. need_bytes -= avail_bytes;
  31140. avail_bytes <<= 1;
  31141. }
  31142. DUK_ASSERT(need_bytes < avail_bytes); /* need_bytes may be zero */
  31143. duk_memcpy((void *) p, (const void *) p_start, (size_t) need_bytes);
  31144. p += need_bytes;
  31145. /*avail_bytes += need_bytes*/
  31146. DUK_BW_SET_PTR(js_ctx->thr, &js_ctx->bw, p);
  31147. }
  31148. #endif /* DUK_USE_PREFER_SIZE */
  31149. /* Shared entry handling for object/array serialization. */
  31150. DUK_LOCAL void duk__enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top) {
  31151. duk_hthread *thr = js_ctx->thr;
  31152. duk_hobject *h_target;
  31153. duk_uint_fast32_t i, n;
  31154. *entry_top = duk_get_top(thr);
  31155. duk_require_stack(thr, DUK_JSON_ENC_REQSTACK);
  31156. /* Loop check using a hybrid approach: a fixed-size visited[] array
  31157. * with overflow in a loop check object.
  31158. */
  31159. h_target = duk_known_hobject(thr, -1); /* object or array */
  31160. n = js_ctx->recursion_depth;
  31161. if (DUK_UNLIKELY(n > DUK_JSON_ENC_LOOPARRAY)) {
  31162. n = DUK_JSON_ENC_LOOPARRAY;
  31163. }
  31164. for (i = 0; i < n; i++) {
  31165. if (DUK_UNLIKELY(js_ctx->visiting[i] == h_target)) {
  31166. DUK_DD(DUK_DDPRINT("slow path loop detect"));
  31167. DUK_ERROR_TYPE(thr, DUK_STR_CYCLIC_INPUT);
  31168. DUK_WO_NORETURN(return;);
  31169. }
  31170. }
  31171. if (js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY) {
  31172. js_ctx->visiting[js_ctx->recursion_depth] = h_target;
  31173. } else {
  31174. duk_push_sprintf(thr, DUK_STR_FMT_PTR, (void *) h_target);
  31175. duk_dup_top(thr); /* -> [ ... voidp voidp ] */
  31176. if (duk_has_prop(thr, js_ctx->idx_loop)) {
  31177. DUK_ERROR_TYPE(thr, DUK_STR_CYCLIC_INPUT);
  31178. DUK_WO_NORETURN(return;);
  31179. }
  31180. duk_push_true(thr); /* -> [ ... voidp true ] */
  31181. duk_put_prop(thr, js_ctx->idx_loop); /* -> [ ... ] */
  31182. }
  31183. /* C recursion check. */
  31184. DUK_ASSERT_DISABLE(js_ctx->recursion_depth >= 0); /* unsigned */
  31185. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  31186. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  31187. DUK_ERROR_RANGE(thr, DUK_STR_JSONENC_RECLIMIT);
  31188. DUK_WO_NORETURN(return;);
  31189. }
  31190. js_ctx->recursion_depth++;
  31191. DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T",
  31192. (long) duk_get_top(thr), (duk_tval *) duk_get_tval(thr, js_ctx->idx_loop)));
  31193. }
  31194. /* Shared exit handling for object/array serialization. */
  31195. DUK_LOCAL void duk__enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top) {
  31196. duk_hthread *thr = js_ctx->thr;
  31197. duk_hobject *h_target;
  31198. /* C recursion check. */
  31199. DUK_ASSERT(js_ctx->recursion_depth > 0);
  31200. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  31201. js_ctx->recursion_depth--;
  31202. /* Loop check. */
  31203. h_target = duk_known_hobject(thr, *entry_top - 1); /* original target at entry_top - 1 */
  31204. if (js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY) {
  31205. /* Previous entry was inside visited[], nothing to do. */
  31206. } else {
  31207. duk_push_sprintf(thr, DUK_STR_FMT_PTR, (void *) h_target);
  31208. duk_del_prop(thr, js_ctx->idx_loop); /* -> [ ... ] */
  31209. }
  31210. /* Restore stack top after unbalanced code paths. */
  31211. duk_set_top(thr, *entry_top);
  31212. DUK_DDD(DUK_DDDPRINT("shared entry finished: top=%ld, loop=%!T",
  31213. (long) duk_get_top(thr), (duk_tval *) duk_get_tval(thr, js_ctx->idx_loop)));
  31214. }
  31215. /* The JO(value) operation: encode object.
  31216. *
  31217. * Stack policy: [ object ] -> [ object ].
  31218. */
  31219. DUK_LOCAL void duk__enc_object(duk_json_enc_ctx *js_ctx) {
  31220. duk_hthread *thr = js_ctx->thr;
  31221. duk_hstring *h_key;
  31222. duk_idx_t entry_top;
  31223. duk_idx_t idx_obj;
  31224. duk_idx_t idx_keys;
  31225. duk_bool_t emitted;
  31226. duk_uarridx_t arr_len, i;
  31227. duk_size_t prev_size;
  31228. DUK_DDD(DUK_DDDPRINT("duk__enc_object: obj=%!T", (duk_tval *) duk_get_tval(thr, -1)));
  31229. duk__enc_objarr_entry(js_ctx, &entry_top);
  31230. idx_obj = entry_top - 1;
  31231. if (js_ctx->idx_proplist >= 0) {
  31232. idx_keys = js_ctx->idx_proplist;
  31233. } else {
  31234. /* XXX: would be nice to enumerate an object at specified index */
  31235. duk_dup(thr, idx_obj);
  31236. (void) duk_hobject_get_enumerated_keys(thr, DUK_ENUM_OWN_PROPERTIES_ONLY /*flags*/); /* [ ... target ] -> [ ... target keys ] */
  31237. idx_keys = duk_require_normalize_index(thr, -1);
  31238. /* leave stack unbalanced on purpose */
  31239. }
  31240. DUK_DDD(DUK_DDDPRINT("idx_keys=%ld, h_keys=%!T",
  31241. (long) idx_keys, (duk_tval *) duk_get_tval(thr, idx_keys)));
  31242. /* Steps 8-10 have been merged to avoid a "partial" variable. */
  31243. DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY);
  31244. /* XXX: keys is an internal object with all keys to be processed
  31245. * in its (gapless) array part. Because nobody can touch the keys
  31246. * object, we could iterate its array part directly (keeping in mind
  31247. * that it can be reallocated).
  31248. */
  31249. arr_len = (duk_uarridx_t) duk_get_length(thr, idx_keys);
  31250. emitted = 0;
  31251. for (i = 0; i < arr_len; i++) {
  31252. duk_get_prop_index(thr, idx_keys, i); /* -> [ ... key ] */
  31253. DUK_DDD(DUK_DDDPRINT("object property loop: holder=%!T, key=%!T",
  31254. (duk_tval *) duk_get_tval(thr, idx_obj),
  31255. (duk_tval *) duk_get_tval(thr, -1)));
  31256. h_key = duk_known_hstring(thr, -1);
  31257. DUK_ASSERT(h_key != NULL);
  31258. DUK_ASSERT(!DUK_HSTRING_HAS_SYMBOL(h_key)); /* proplist filtering; enum options */
  31259. prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw);
  31260. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31261. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth);
  31262. duk__enc_key_autoquote(js_ctx, h_key);
  31263. DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE);
  31264. } else {
  31265. duk__enc_key_autoquote(js_ctx, h_key);
  31266. DUK__EMIT_1(js_ctx, DUK_ASC_COLON);
  31267. }
  31268. /* [ ... key ] */
  31269. if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_obj) == 0)) {
  31270. /* Value would yield 'undefined', so skip key altogether.
  31271. * Side effects have already happened.
  31272. */
  31273. DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size);
  31274. } else {
  31275. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  31276. emitted = 1;
  31277. }
  31278. /* [ ... ] */
  31279. }
  31280. if (emitted) {
  31281. DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA);
  31282. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  31283. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31284. DUK_ASSERT(js_ctx->recursion_depth >= 1);
  31285. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1U);
  31286. }
  31287. }
  31288. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  31289. duk__enc_objarr_exit(js_ctx, &entry_top);
  31290. DUK_ASSERT_TOP(thr, entry_top);
  31291. }
  31292. /* The JA(value) operation: encode array.
  31293. *
  31294. * Stack policy: [ array ] -> [ array ].
  31295. */
  31296. DUK_LOCAL void duk__enc_array(duk_json_enc_ctx *js_ctx) {
  31297. duk_hthread *thr = js_ctx->thr;
  31298. duk_idx_t entry_top;
  31299. duk_idx_t idx_arr;
  31300. duk_bool_t emitted;
  31301. duk_uarridx_t i, arr_len;
  31302. DUK_DDD(DUK_DDDPRINT("duk__enc_array: array=%!T",
  31303. (duk_tval *) duk_get_tval(thr, -1)));
  31304. duk__enc_objarr_entry(js_ctx, &entry_top);
  31305. idx_arr = entry_top - 1;
  31306. /* Steps 8-10 have been merged to avoid a "partial" variable. */
  31307. DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET);
  31308. arr_len = (duk_uarridx_t) duk_get_length(thr, idx_arr);
  31309. emitted = 0;
  31310. for (i = 0; i < arr_len; i++) {
  31311. DUK_DDD(DUK_DDDPRINT("array entry loop: array=%!T, index=%ld, arr_len=%ld",
  31312. (duk_tval *) duk_get_tval(thr, idx_arr),
  31313. (long) i, (long) arr_len));
  31314. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31315. DUK_ASSERT(js_ctx->recursion_depth >= 1);
  31316. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth);
  31317. }
  31318. (void) duk_push_uint_to_hstring(thr, (duk_uint_t) i); /* -> [ ... key ] */
  31319. /* [ ... key ] */
  31320. if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_arr) == 0)) {
  31321. /* Value would normally be omitted, replace with 'null'. */
  31322. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  31323. } else {
  31324. ;
  31325. }
  31326. /* [ ... ] */
  31327. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  31328. emitted = 1;
  31329. }
  31330. if (emitted) {
  31331. DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA);
  31332. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  31333. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31334. DUK_ASSERT(js_ctx->recursion_depth >= 1);
  31335. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1U);
  31336. }
  31337. }
  31338. DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET);
  31339. duk__enc_objarr_exit(js_ctx, &entry_top);
  31340. DUK_ASSERT_TOP(thr, entry_top);
  31341. }
  31342. /* The Str(key, holder) operation.
  31343. *
  31344. * Stack policy: [ ... key ] -> [ ... ]
  31345. */
  31346. DUK_LOCAL duk_bool_t duk__enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder) {
  31347. duk_hthread *thr = js_ctx->thr;
  31348. duk_tval *tv;
  31349. duk_tval *tv_holder;
  31350. duk_tval *tv_key;
  31351. duk_small_int_t c;
  31352. DUK_DDD(DUK_DDDPRINT("duk__enc_value: idx_holder=%ld, holder=%!T, key=%!T",
  31353. (long) idx_holder, (duk_tval *) duk_get_tval(thr, idx_holder),
  31354. (duk_tval *) duk_get_tval(thr, -1)));
  31355. tv_holder = DUK_GET_TVAL_POSIDX(thr, idx_holder);
  31356. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_holder));
  31357. tv_key = DUK_GET_TVAL_NEGIDX(thr, -1);
  31358. DUK_ASSERT(DUK_TVAL_IS_STRING(tv_key));
  31359. DUK_ASSERT(!DUK_HSTRING_HAS_SYMBOL(DUK_TVAL_GET_STRING(tv_key))); /* Caller responsible. */
  31360. (void) duk_hobject_getprop(thr, tv_holder, tv_key);
  31361. /* -> [ ... key val ] */
  31362. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(thr, -1)));
  31363. /* Standard JSON checks for .toJSON() only for actual objects; for
  31364. * example, setting Number.prototype.toJSON and then serializing a
  31365. * number won't invoke the .toJSON() method. However, lightfuncs and
  31366. * plain buffers mimic objects so we check for their .toJSON() method.
  31367. */
  31368. if (duk_check_type_mask(thr, -1, DUK_TYPE_MASK_OBJECT |
  31369. DUK_TYPE_MASK_LIGHTFUNC |
  31370. DUK_TYPE_MASK_BUFFER)) {
  31371. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_TO_JSON);
  31372. if (duk_is_callable(thr, -1)) { /* toJSON() can also be a lightfunc */
  31373. DUK_DDD(DUK_DDDPRINT("value is object, has callable toJSON() -> call it"));
  31374. /* XXX: duk_dup_unvalidated(thr, -2) etc. */
  31375. duk_dup_m2(thr); /* -> [ ... key val toJSON val ] */
  31376. duk_dup_m4(thr); /* -> [ ... key val toJSON val key ] */
  31377. duk_call_method(thr, 1); /* -> [ ... key val val' ] */
  31378. duk_remove_m2(thr); /* -> [ ... key val' ] */
  31379. } else {
  31380. duk_pop(thr); /* -> [ ... key val ] */
  31381. }
  31382. }
  31383. /* [ ... key val ] */
  31384. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(thr, -1)));
  31385. if (js_ctx->h_replacer) {
  31386. /* XXX: Here a "slice copy" would be useful. */
  31387. DUK_DDD(DUK_DDDPRINT("replacer is set, call replacer"));
  31388. duk_push_hobject(thr, js_ctx->h_replacer); /* -> [ ... key val replacer ] */
  31389. duk_dup(thr, idx_holder); /* -> [ ... key val replacer holder ] */
  31390. duk_dup_m4(thr); /* -> [ ... key val replacer holder key ] */
  31391. duk_dup_m4(thr); /* -> [ ... key val replacer holder key val ] */
  31392. duk_call_method(thr, 2); /* -> [ ... key val val' ] */
  31393. duk_remove_m2(thr); /* -> [ ... key val' ] */
  31394. }
  31395. /* [ ... key val ] */
  31396. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(thr, -1)));
  31397. tv = DUK_GET_TVAL_NEGIDX(thr, -1);
  31398. if (DUK_TVAL_IS_OBJECT(tv)) {
  31399. duk_hobject *h;
  31400. h = DUK_TVAL_GET_OBJECT(tv);
  31401. DUK_ASSERT(h != NULL);
  31402. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  31403. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31404. if (DUK_HOBJECT_IS_BUFOBJ(h) &&
  31405. js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  31406. /* With JX/JC a bufferobject gets serialized specially. */
  31407. duk_hbufobj *h_bufobj;
  31408. h_bufobj = (duk_hbufobj *) h;
  31409. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  31410. duk__enc_bufobj(js_ctx, h_bufobj);
  31411. goto pop2_emitted;
  31412. }
  31413. /* Otherwise bufferobjects get serialized as normal objects. */
  31414. #endif /* JX || JC */
  31415. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  31416. c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h);
  31417. switch (c) {
  31418. case DUK_HOBJECT_CLASS_NUMBER: {
  31419. DUK_DDD(DUK_DDDPRINT("value is a Number object -> coerce with ToNumber()"));
  31420. duk_to_number_m1(thr);
  31421. /* The coercion potentially invokes user .valueOf() and .toString()
  31422. * but can't result in a function value because ToPrimitive() would
  31423. * reject such a result: test-dev-json-stringify-coercion-1.js.
  31424. */
  31425. DUK_ASSERT(!duk_is_callable(thr, -1));
  31426. break;
  31427. }
  31428. case DUK_HOBJECT_CLASS_STRING: {
  31429. DUK_DDD(DUK_DDDPRINT("value is a String object -> coerce with ToString()"));
  31430. duk_to_string(thr, -1);
  31431. /* Same coercion behavior as for Number. */
  31432. DUK_ASSERT(!duk_is_callable(thr, -1));
  31433. break;
  31434. }
  31435. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31436. case DUK_HOBJECT_CLASS_POINTER:
  31437. #endif
  31438. case DUK_HOBJECT_CLASS_BOOLEAN: {
  31439. DUK_DDD(DUK_DDDPRINT("value is a Boolean/Buffer/Pointer object -> get internal value"));
  31440. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
  31441. duk_remove_m2(thr);
  31442. break;
  31443. }
  31444. default: {
  31445. /* Normal object which doesn't get automatically coerced to a
  31446. * primitive value. Functions are checked for specially. The
  31447. * primitive value coercions for Number, String, Pointer, and
  31448. * Boolean can't result in functions so suffices to check here.
  31449. * Symbol objects are handled like plain objects (their primitive
  31450. * value is NOT looked up like for e.g. String objects).
  31451. */
  31452. DUK_ASSERT(h != NULL);
  31453. if (DUK_HOBJECT_IS_CALLABLE(h)) {
  31454. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31455. if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  31456. DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  31457. /* We only get here when doing non-standard JSON encoding */
  31458. DUK_DDD(DUK_DDDPRINT("-> function allowed, serialize to custom format"));
  31459. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible);
  31460. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  31461. goto pop2_emitted;
  31462. } else {
  31463. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)"));
  31464. goto pop2_undef;
  31465. }
  31466. #else /* DUK_USE_JX || DUK_USE_JC */
  31467. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (function)"));
  31468. goto pop2_undef;
  31469. #endif /* DUK_USE_JX || DUK_USE_JC */
  31470. }
  31471. }
  31472. } /* end switch */
  31473. }
  31474. /* [ ... key val ] */
  31475. DUK_DDD(DUK_DDDPRINT("value=%!T", (duk_tval *) duk_get_tval(thr, -1)));
  31476. if (duk_check_type_mask(thr, -1, js_ctx->mask_for_undefined)) {
  31477. /* will result in undefined */
  31478. DUK_DDD(DUK_DDDPRINT("-> will result in undefined (type mask check)"));
  31479. goto pop2_undef;
  31480. }
  31481. tv = DUK_GET_TVAL_NEGIDX(thr, -1);
  31482. switch (DUK_TVAL_GET_TAG(tv)) {
  31483. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31484. /* When JX/JC not in use, the type mask above will avoid this case if needed. */
  31485. case DUK_TAG_UNDEFINED: {
  31486. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined);
  31487. break;
  31488. }
  31489. #endif
  31490. case DUK_TAG_NULL: {
  31491. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  31492. break;
  31493. }
  31494. case DUK_TAG_BOOLEAN: {
  31495. DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ?
  31496. DUK_STRIDX_TRUE : DUK_STRIDX_FALSE);
  31497. break;
  31498. }
  31499. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31500. /* When JX/JC not in use, the type mask above will avoid this case if needed. */
  31501. case DUK_TAG_POINTER: {
  31502. duk__enc_pointer(js_ctx, DUK_TVAL_GET_POINTER(tv));
  31503. break;
  31504. }
  31505. #endif /* DUK_USE_JX || DUK_USE_JC */
  31506. case DUK_TAG_STRING: {
  31507. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  31508. DUK_ASSERT(h != NULL);
  31509. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  31510. goto pop2_undef;
  31511. }
  31512. duk__enc_quote_string(js_ctx, h);
  31513. break;
  31514. }
  31515. case DUK_TAG_OBJECT: {
  31516. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  31517. DUK_ASSERT(h != NULL);
  31518. /* Function values are handled completely above (including
  31519. * coercion results):
  31520. */
  31521. DUK_ASSERT(!DUK_HOBJECT_IS_CALLABLE(h));
  31522. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  31523. duk__enc_array(js_ctx);
  31524. } else {
  31525. duk__enc_object(js_ctx);
  31526. }
  31527. break;
  31528. }
  31529. /* Because plain buffers mimics Uint8Array, they have enumerable
  31530. * index properties [0,byteLength[. Because JSON only serializes
  31531. * enumerable own properties, no properties can be serialized for
  31532. * plain buffers (all virtual properties are non-enumerable). However,
  31533. * there may be a .toJSON() method which was already handled above.
  31534. */
  31535. case DUK_TAG_BUFFER: {
  31536. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31537. if (js_ctx->flag_ext_custom_or_compatible) {
  31538. duk__enc_buffer_jx_jc(js_ctx, DUK_TVAL_GET_BUFFER(tv));
  31539. break;
  31540. }
  31541. #endif
  31542. /* Could implement a fastpath, but the fast path would need
  31543. * to handle realloc side effects correctly.
  31544. */
  31545. duk_to_object(thr, -1);
  31546. duk__enc_object(js_ctx);
  31547. break;
  31548. }
  31549. case DUK_TAG_LIGHTFUNC: {
  31550. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31551. /* We only get here when doing non-standard JSON encoding */
  31552. DUK_ASSERT(js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible);
  31553. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  31554. #else
  31555. /* Standard JSON omits functions */
  31556. DUK_UNREACHABLE();
  31557. #endif
  31558. break;
  31559. }
  31560. #if defined(DUK_USE_FASTINT)
  31561. case DUK_TAG_FASTINT:
  31562. /* Number serialization has a significant impact relative to
  31563. * other fast path code, so careful fast path for fastints.
  31564. */
  31565. duk__enc_fastint_tval(js_ctx, tv);
  31566. break;
  31567. #endif
  31568. default: {
  31569. /* number */
  31570. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  31571. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  31572. /* XXX: A fast path for usual integers would be useful when
  31573. * fastint support is not enabled.
  31574. */
  31575. duk__enc_double(js_ctx);
  31576. break;
  31577. }
  31578. }
  31579. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31580. pop2_emitted:
  31581. #endif
  31582. duk_pop_2(thr); /* [ ... key val ] -> [ ... ] */
  31583. return 1; /* emitted */
  31584. pop2_undef:
  31585. duk_pop_2(thr); /* [ ... key val ] -> [ ... ] */
  31586. return 0; /* not emitted */
  31587. }
  31588. /* E5 Section 15.12.3, main algorithm, step 4.b.ii steps 1-4. */
  31589. DUK_LOCAL duk_bool_t duk__enc_allow_into_proplist(duk_tval *tv) {
  31590. duk_small_int_t c;
  31591. /* XXX: some kind of external internal type checker?
  31592. * - type mask; symbol flag; class mask
  31593. */
  31594. DUK_ASSERT(tv != NULL);
  31595. if (DUK_TVAL_IS_STRING(tv)) {
  31596. duk_hstring *h;
  31597. h = DUK_TVAL_GET_STRING(tv);
  31598. DUK_ASSERT(h != NULL);
  31599. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  31600. return 0;
  31601. }
  31602. return 1;
  31603. } else if (DUK_TVAL_IS_NUMBER(tv)) {
  31604. return 1;
  31605. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  31606. duk_hobject *h;
  31607. h = DUK_TVAL_GET_OBJECT(tv);
  31608. DUK_ASSERT(h != NULL);
  31609. c = (duk_small_int_t) DUK_HOBJECT_GET_CLASS_NUMBER(h);
  31610. if (c == DUK_HOBJECT_CLASS_STRING || c == DUK_HOBJECT_CLASS_NUMBER) {
  31611. return 1;
  31612. }
  31613. }
  31614. return 0;
  31615. }
  31616. /*
  31617. * JSON.stringify() fast path
  31618. *
  31619. * Otherwise supports full JSON, JX, and JC features, but bails out on any
  31620. * possible side effect which might change the value being serialized. The
  31621. * fast path can take advantage of the fact that the value being serialized
  31622. * is unchanged so that we can walk directly through property tables etc.
  31623. */
  31624. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  31625. DUK_LOCAL duk_bool_t duk__json_stringify_fast_value(duk_json_enc_ctx *js_ctx, duk_tval *tv) {
  31626. duk_uint_fast32_t i, n;
  31627. DUK_DDD(DUK_DDDPRINT("stringify fast: %!T", tv));
  31628. DUK_ASSERT(js_ctx != NULL);
  31629. DUK_ASSERT(js_ctx->thr != NULL);
  31630. #if 0 /* disabled for now */
  31631. restart_match:
  31632. #endif
  31633. DUK_ASSERT(tv != NULL);
  31634. switch (DUK_TVAL_GET_TAG(tv)) {
  31635. case DUK_TAG_UNDEFINED: {
  31636. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31637. if (js_ctx->flag_ext_custom || js_ctx->flag_ext_compatible) {
  31638. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined);
  31639. break;
  31640. } else {
  31641. goto emit_undefined;
  31642. }
  31643. #else
  31644. goto emit_undefined;
  31645. #endif
  31646. }
  31647. case DUK_TAG_NULL: {
  31648. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  31649. break;
  31650. }
  31651. case DUK_TAG_BOOLEAN: {
  31652. DUK__EMIT_STRIDX(js_ctx, DUK_TVAL_GET_BOOLEAN(tv) ?
  31653. DUK_STRIDX_TRUE : DUK_STRIDX_FALSE);
  31654. break;
  31655. }
  31656. case DUK_TAG_STRING: {
  31657. duk_hstring *h;
  31658. h = DUK_TVAL_GET_STRING(tv);
  31659. DUK_ASSERT(h != NULL);
  31660. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  31661. goto emit_undefined;
  31662. }
  31663. duk__enc_quote_string(js_ctx, h);
  31664. break;
  31665. }
  31666. case DUK_TAG_OBJECT: {
  31667. duk_hobject *obj;
  31668. duk_tval *tv_val;
  31669. duk_bool_t emitted = 0;
  31670. duk_uint32_t c_bit, c_all, c_array, c_unbox, c_undef,
  31671. c_func, c_bufobj, c_object, c_abort;
  31672. /* For objects JSON.stringify() only looks for own, enumerable
  31673. * properties which is nice for the fast path here.
  31674. *
  31675. * For arrays JSON.stringify() uses [[Get]] so it will actually
  31676. * inherit properties during serialization! This fast path
  31677. * supports gappy arrays as long as there's no actual inherited
  31678. * property (which might be a getter etc).
  31679. *
  31680. * Since recursion only happens for objects, we can have both
  31681. * recursion and loop checks here. We use a simple, depth-limited
  31682. * loop check in the fast path because the object-based tracking
  31683. * is very slow (when tested, it accounted for 50% of fast path
  31684. * execution time for input data with a lot of small objects!).
  31685. */
  31686. /* XXX: for real world code, could just ignore array inheritance
  31687. * and only look at array own properties.
  31688. */
  31689. /* We rely on a few object flag / class number relationships here,
  31690. * assert for them.
  31691. */
  31692. obj = DUK_TVAL_GET_OBJECT(tv);
  31693. DUK_ASSERT(obj != NULL);
  31694. DUK_ASSERT_HOBJECT_VALID(obj);
  31695. /* Once recursion depth is increased, exit path must decrease
  31696. * it (though it's OK to abort the fast path).
  31697. */
  31698. DUK_ASSERT_DISABLE(js_ctx->recursion_depth >= 0); /* unsigned */
  31699. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  31700. if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
  31701. DUK_DD(DUK_DDPRINT("fast path recursion limit"));
  31702. DUK_ERROR_RANGE(js_ctx->thr, DUK_STR_JSONDEC_RECLIMIT);
  31703. DUK_WO_NORETURN(return 0;);
  31704. }
  31705. for (i = 0, n = (duk_uint_fast32_t) js_ctx->recursion_depth; i < n; i++) {
  31706. if (DUK_UNLIKELY(js_ctx->visiting[i] == obj)) {
  31707. DUK_DD(DUK_DDPRINT("fast path loop detect"));
  31708. DUK_ERROR_TYPE(js_ctx->thr, DUK_STR_CYCLIC_INPUT);
  31709. DUK_WO_NORETURN(return 0;);
  31710. }
  31711. }
  31712. /* Guaranteed by recursion_limit setup so we don't have to
  31713. * check twice.
  31714. */
  31715. DUK_ASSERT(js_ctx->recursion_depth < DUK_JSON_ENC_LOOPARRAY);
  31716. js_ctx->visiting[js_ctx->recursion_depth] = obj;
  31717. js_ctx->recursion_depth++;
  31718. /* If object has a .toJSON() property, we can't be certain
  31719. * that it wouldn't mutate any value arbitrarily, so bail
  31720. * out of the fast path.
  31721. *
  31722. * If an object is a Proxy we also can't avoid side effects
  31723. * so abandon.
  31724. */
  31725. /* XXX: non-callable .toJSON() doesn't need to cause an abort
  31726. * but does at the moment, probably not worth fixing.
  31727. */
  31728. if (duk_hobject_hasprop_raw(js_ctx->thr, obj, DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr)) ||
  31729. DUK_HOBJECT_IS_PROXY(obj)) {
  31730. DUK_DD(DUK_DDPRINT("object has a .toJSON property or object is a Proxy, abort fast path"));
  31731. goto abort_fastpath;
  31732. }
  31733. /* We could use a switch-case for the class number but it turns out
  31734. * a small if-else ladder on class masks is better. The if-ladder
  31735. * should be in order of relevancy.
  31736. */
  31737. /* XXX: move masks to js_ctx? they don't change during one
  31738. * fast path invocation.
  31739. */
  31740. DUK_ASSERT(DUK_HOBJECT_CLASS_MAX <= 31);
  31741. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31742. if (js_ctx->flag_ext_custom_or_compatible) {
  31743. c_all = DUK_HOBJECT_CMASK_ALL;
  31744. c_array = DUK_HOBJECT_CMASK_ARRAY;
  31745. c_unbox = DUK_HOBJECT_CMASK_NUMBER |
  31746. DUK_HOBJECT_CMASK_STRING |
  31747. DUK_HOBJECT_CMASK_BOOLEAN |
  31748. DUK_HOBJECT_CMASK_POINTER; /* Symbols are not unboxed. */
  31749. c_func = DUK_HOBJECT_CMASK_FUNCTION;
  31750. c_bufobj = DUK_HOBJECT_CMASK_ALL_BUFOBJS;
  31751. c_undef = 0;
  31752. c_abort = 0;
  31753. c_object = c_all & ~(c_array | c_unbox | c_func | c_bufobj | c_undef | c_abort);
  31754. }
  31755. else
  31756. #endif
  31757. {
  31758. c_all = DUK_HOBJECT_CMASK_ALL;
  31759. c_array = DUK_HOBJECT_CMASK_ARRAY;
  31760. c_unbox = DUK_HOBJECT_CMASK_NUMBER |
  31761. DUK_HOBJECT_CMASK_STRING |
  31762. DUK_HOBJECT_CMASK_BOOLEAN; /* Symbols are not unboxed. */
  31763. c_func = 0;
  31764. c_bufobj = 0;
  31765. c_undef = DUK_HOBJECT_CMASK_FUNCTION |
  31766. DUK_HOBJECT_CMASK_POINTER;
  31767. /* As the fast path doesn't currently properly support
  31768. * duk_hbufobj virtual properties, abort fast path if
  31769. * we encounter them in plain JSON mode.
  31770. */
  31771. c_abort = DUK_HOBJECT_CMASK_ALL_BUFOBJS;
  31772. c_object = c_all & ~(c_array | c_unbox | c_func | c_bufobj | c_undef | c_abort);
  31773. }
  31774. c_bit = (duk_uint32_t) DUK_HOBJECT_GET_CLASS_MASK(obj);
  31775. if (c_bit & c_object) {
  31776. /* All other object types. */
  31777. DUK__EMIT_1(js_ctx, DUK_ASC_LCURLY);
  31778. /* A non-Array object should not have an array part in practice.
  31779. * But since it is supported internally (and perhaps used at some
  31780. * point), check and abandon if that's the case.
  31781. */
  31782. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  31783. DUK_DD(DUK_DDPRINT("non-Array object has array part, abort fast path"));
  31784. goto abort_fastpath;
  31785. }
  31786. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(obj); i++) {
  31787. duk_hstring *k;
  31788. duk_size_t prev_size;
  31789. k = DUK_HOBJECT_E_GET_KEY(js_ctx->thr->heap, obj, i);
  31790. if (!k) {
  31791. continue;
  31792. }
  31793. if (DUK_HSTRING_HAS_ARRIDX(k)) {
  31794. /* If an object has array index keys we would need
  31795. * to sort them into the ES2015 enumeration order to
  31796. * be consistent with the slow path. Abort the fast
  31797. * path and handle in the slow path for now.
  31798. */
  31799. DUK_DD(DUK_DDPRINT("property key is an array index, abort fast path"));
  31800. goto abort_fastpath;
  31801. }
  31802. if (!DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(js_ctx->thr->heap, obj, i)) {
  31803. continue;
  31804. }
  31805. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(js_ctx->thr->heap, obj, i)) {
  31806. /* Getter might have arbitrary side effects,
  31807. * so bail out.
  31808. */
  31809. DUK_DD(DUK_DDPRINT("property is an accessor, abort fast path"));
  31810. goto abort_fastpath;
  31811. }
  31812. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(k))) {
  31813. continue;
  31814. }
  31815. tv_val = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(js_ctx->thr->heap, obj, i);
  31816. prev_size = DUK_BW_GET_SIZE(js_ctx->thr, &js_ctx->bw);
  31817. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31818. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth);
  31819. duk__enc_key_autoquote(js_ctx, k);
  31820. DUK__EMIT_2(js_ctx, DUK_ASC_COLON, DUK_ASC_SPACE);
  31821. } else {
  31822. duk__enc_key_autoquote(js_ctx, k);
  31823. DUK__EMIT_1(js_ctx, DUK_ASC_COLON);
  31824. }
  31825. if (duk__json_stringify_fast_value(js_ctx, tv_val) == 0) {
  31826. DUK_DD(DUK_DDPRINT("prop value not supported, rewind key and colon"));
  31827. DUK_BW_SET_SIZE(js_ctx->thr, &js_ctx->bw, prev_size);
  31828. } else {
  31829. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  31830. emitted = 1;
  31831. }
  31832. }
  31833. /* If any non-Array value had enumerable virtual own
  31834. * properties, they should be serialized here (actually,
  31835. * before the explicit properties). Standard types don't.
  31836. */
  31837. if (emitted) {
  31838. DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA);
  31839. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  31840. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31841. DUK_ASSERT(js_ctx->recursion_depth >= 1);
  31842. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1U);
  31843. }
  31844. }
  31845. DUK__EMIT_1(js_ctx, DUK_ASC_RCURLY);
  31846. } else if (c_bit & c_array) {
  31847. duk_uint_fast32_t arr_len;
  31848. duk_uint_fast32_t asize;
  31849. DUK__EMIT_1(js_ctx, DUK_ASC_LBRACKET);
  31850. /* Assume arrays are dense in the fast path. */
  31851. if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  31852. DUK_DD(DUK_DDPRINT("Array object is sparse, abort fast path"));
  31853. goto abort_fastpath;
  31854. }
  31855. arr_len = (duk_uint_fast32_t) ((duk_harray *) obj)->length;
  31856. asize = (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(obj);
  31857. /* Array part may be larger than 'length'; if so, iterate
  31858. * only up to array 'length'. Array part may also be smaller
  31859. * than 'length' in some cases.
  31860. */
  31861. for (i = 0; i < arr_len; i++) {
  31862. duk_tval *tv_arrval;
  31863. duk_hstring *h_tmp;
  31864. duk_bool_t has_inherited;
  31865. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31866. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth);
  31867. }
  31868. if (DUK_LIKELY(i < asize)) {
  31869. tv_arrval = DUK_HOBJECT_A_GET_VALUE_PTR(js_ctx->thr->heap, obj, i);
  31870. if (DUK_LIKELY(!DUK_TVAL_IS_UNUSED(tv_arrval))) {
  31871. /* Expected case: element is present. */
  31872. if (duk__json_stringify_fast_value(js_ctx, tv_arrval) == 0) {
  31873. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  31874. }
  31875. goto elem_done;
  31876. }
  31877. }
  31878. /* Gap in array; check for inherited property,
  31879. * bail out if one exists. This should be enough
  31880. * to support gappy arrays for all practical code.
  31881. */
  31882. h_tmp = duk_push_uint_to_hstring(js_ctx->thr, (duk_uint_t) i);
  31883. has_inherited = duk_hobject_hasprop_raw(js_ctx->thr, obj, h_tmp);
  31884. duk_pop(js_ctx->thr);
  31885. if (has_inherited) {
  31886. DUK_D(DUK_DPRINT("gap in array, conflicting inherited property, abort fast path"));
  31887. goto abort_fastpath;
  31888. }
  31889. /* Ordinary gap, undefined encodes to 'null' in
  31890. * standard JSON, but JX/JC use their form for
  31891. * undefined to better preserve the typing.
  31892. */
  31893. DUK_D(DUK_DPRINT("gap in array, no conflicting inherited property, remain on fast path"));
  31894. #if defined(DUK_USE_JX)
  31895. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_undefined);
  31896. #else
  31897. DUK__EMIT_STRIDX(js_ctx, DUK_STRIDX_LC_NULL);
  31898. #endif
  31899. /* fall through */
  31900. elem_done:
  31901. DUK__EMIT_1(js_ctx, DUK_ASC_COMMA);
  31902. emitted = 1;
  31903. }
  31904. if (emitted) {
  31905. DUK_ASSERT(*((duk_uint8_t *) DUK_BW_GET_PTR(js_ctx->thr, &js_ctx->bw) - 1) == DUK_ASC_COMMA);
  31906. DUK__UNEMIT_1(js_ctx); /* eat trailing comma */
  31907. if (DUK_UNLIKELY(js_ctx->h_gap != NULL)) {
  31908. DUK_ASSERT(js_ctx->recursion_depth >= 1);
  31909. duk__enc_newline_indent(js_ctx, js_ctx->recursion_depth - 1U);
  31910. }
  31911. }
  31912. DUK__EMIT_1(js_ctx, DUK_ASC_RBRACKET);
  31913. } else if (c_bit & c_unbox) {
  31914. /* Certain boxed types are required to go through
  31915. * automatic unboxing. Rely on internal value being
  31916. * sane (to avoid infinite recursion).
  31917. */
  31918. DUK_ASSERT((c_bit & DUK_HOBJECT_CMASK_SYMBOL) == 0); /* Symbols are not unboxed. */
  31919. #if 1
  31920. /* The code below is incorrect if .toString() or .valueOf() have
  31921. * have been overridden. The correct approach would be to look up
  31922. * the method(s) and if they resolve to the built-in function we
  31923. * can safely bypass it and look up the internal value directly.
  31924. * Unimplemented for now, abort fast path for boxed values.
  31925. */
  31926. goto abort_fastpath;
  31927. #else /* disabled */
  31928. /* Disabled until fixed, see above. */
  31929. duk_tval *tv_internal;
  31930. DUK_DD(DUK_DDPRINT("auto unboxing in fast path"));
  31931. tv_internal = duk_hobject_get_internal_value_tval_ptr(js_ctx->thr->heap, obj);
  31932. DUK_ASSERT(tv_internal != NULL);
  31933. DUK_ASSERT(DUK_TVAL_IS_STRING(tv_internal) ||
  31934. DUK_TVAL_IS_NUMBER(tv_internal) ||
  31935. DUK_TVAL_IS_BOOLEAN(tv_internal) ||
  31936. DUK_TVAL_IS_POINTER(tv_internal));
  31937. tv = tv_internal;
  31938. DUK_ASSERT(js_ctx->recursion_depth > 0);
  31939. js_ctx->recursion_depth--; /* required to keep recursion depth correct */
  31940. goto restart_match;
  31941. #endif /* disabled */
  31942. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31943. } else if (c_bit & c_func) {
  31944. DUK__EMIT_STRIDX(js_ctx, js_ctx->stridx_custom_function);
  31945. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  31946. } else if (c_bit & c_bufobj) {
  31947. duk__enc_bufobj(js_ctx, (duk_hbufobj *) obj);
  31948. #endif
  31949. #endif
  31950. } else if (c_bit & c_abort) {
  31951. DUK_DD(DUK_DDPRINT("abort fast path for unsupported type"));
  31952. goto abort_fastpath;
  31953. } else {
  31954. DUK_ASSERT((c_bit & c_undef) != 0);
  31955. /* Must decrease recursion depth before returning. */
  31956. DUK_ASSERT(js_ctx->recursion_depth > 0);
  31957. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  31958. js_ctx->recursion_depth--;
  31959. goto emit_undefined;
  31960. }
  31961. DUK_ASSERT(js_ctx->recursion_depth > 0);
  31962. DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
  31963. js_ctx->recursion_depth--;
  31964. break;
  31965. }
  31966. case DUK_TAG_BUFFER: {
  31967. /* Plain buffers are treated like Uint8Arrays: they have
  31968. * enumerable indices. Other virtual properties are not
  31969. * enumerable, and inherited properties are not serialized.
  31970. * However, there can be a replacer (not relevant here) or
  31971. * a .toJSON() method (which we need to check for explicitly).
  31972. */
  31973. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  31974. if (duk_hobject_hasprop_raw(js_ctx->thr,
  31975. js_ctx->thr->builtins[DUK_BIDX_UINT8ARRAY_PROTOTYPE],
  31976. DUK_HTHREAD_STRING_TO_JSON(js_ctx->thr))) {
  31977. DUK_DD(DUK_DDPRINT("value is a plain buffer and there's an inherited .toJSON, abort fast path"));
  31978. goto abort_fastpath;
  31979. }
  31980. #endif
  31981. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31982. if (js_ctx->flag_ext_custom_or_compatible) {
  31983. duk__enc_buffer_jx_jc(js_ctx, DUK_TVAL_GET_BUFFER(tv));
  31984. break;
  31985. }
  31986. #endif
  31987. /* Plain buffers mimic Uint8Arrays, and have enumerable index
  31988. * properties.
  31989. */
  31990. duk__enc_buffer_json_fastpath(js_ctx, DUK_TVAL_GET_BUFFER(tv));
  31991. break;
  31992. }
  31993. case DUK_TAG_POINTER: {
  31994. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  31995. if (js_ctx->flag_ext_custom_or_compatible) {
  31996. duk__enc_pointer(js_ctx, DUK_TVAL_GET_POINTER(tv));
  31997. break;
  31998. } else {
  31999. goto emit_undefined;
  32000. }
  32001. #else
  32002. goto emit_undefined;
  32003. #endif
  32004. }
  32005. case DUK_TAG_LIGHTFUNC: {
  32006. /* A lightfunc might also inherit a .toJSON() so just bail out. */
  32007. /* XXX: Could just lookup .toJSON() and continue in fast path,
  32008. * as it would almost never be defined.
  32009. */
  32010. DUK_DD(DUK_DDPRINT("value is a lightfunc, abort fast path"));
  32011. goto abort_fastpath;
  32012. }
  32013. #if defined(DUK_USE_FASTINT)
  32014. case DUK_TAG_FASTINT: {
  32015. /* Number serialization has a significant impact relative to
  32016. * other fast path code, so careful fast path for fastints.
  32017. */
  32018. duk__enc_fastint_tval(js_ctx, tv);
  32019. break;
  32020. }
  32021. #endif
  32022. default: {
  32023. /* XXX: A fast path for usual integers would be useful when
  32024. * fastint support is not enabled.
  32025. */
  32026. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  32027. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  32028. /* XXX: Stack discipline is annoying, could be changed in numconv. */
  32029. duk_push_tval(js_ctx->thr, tv);
  32030. duk__enc_double(js_ctx);
  32031. duk_pop(js_ctx->thr);
  32032. #if 0
  32033. /* Could also rely on native sprintf(), but it will handle
  32034. * values like NaN, Infinity, -0, exponent notation etc in
  32035. * a JSON-incompatible way.
  32036. */
  32037. duk_double_t d;
  32038. char buf[64];
  32039. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  32040. d = DUK_TVAL_GET_DOUBLE(tv);
  32041. DUK_SPRINTF(buf, "%lg", d);
  32042. DUK__EMIT_CSTR(js_ctx, buf);
  32043. #endif
  32044. }
  32045. }
  32046. return 1; /* not undefined */
  32047. emit_undefined:
  32048. return 0; /* value was undefined/unsupported */
  32049. abort_fastpath:
  32050. /* Error message doesn't matter: the error is ignored anyway. */
  32051. DUK_DD(DUK_DDPRINT("aborting fast path"));
  32052. DUK_ERROR_INTERNAL(js_ctx->thr);
  32053. DUK_WO_NORETURN(return 0;);
  32054. }
  32055. DUK_LOCAL duk_ret_t duk__json_stringify_fast(duk_hthread *thr, void *udata) {
  32056. duk_json_enc_ctx *js_ctx;
  32057. duk_tval *tv;
  32058. DUK_ASSERT(thr != NULL);
  32059. DUK_ASSERT(udata != NULL);
  32060. js_ctx = (duk_json_enc_ctx *) udata;
  32061. DUK_ASSERT(js_ctx != NULL);
  32062. tv = DUK_GET_TVAL_NEGIDX(thr, -1);
  32063. if (duk__json_stringify_fast_value(js_ctx, tv) == 0) {
  32064. DUK_DD(DUK_DDPRINT("top level value not supported, fail fast path"));
  32065. DUK_DCERROR_TYPE_INVALID_ARGS(thr); /* Error message is ignored, so doesn't matter. */
  32066. }
  32067. return 0;
  32068. }
  32069. #endif /* DUK_USE_JSON_STRINGIFY_FASTPATH */
  32070. /*
  32071. * Top level wrappers
  32072. */
  32073. DUK_INTERNAL
  32074. void duk_bi_json_parse_helper(duk_hthread *thr,
  32075. duk_idx_t idx_value,
  32076. duk_idx_t idx_reviver,
  32077. duk_small_uint_t flags) {
  32078. duk_json_dec_ctx js_ctx_alloc;
  32079. duk_json_dec_ctx *js_ctx = &js_ctx_alloc;
  32080. duk_hstring *h_text;
  32081. #if defined(DUK_USE_ASSERTIONS)
  32082. duk_idx_t entry_top = duk_get_top(thr);
  32083. #endif
  32084. /* negative top-relative indices not allowed now */
  32085. DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0);
  32086. DUK_ASSERT(idx_reviver == DUK_INVALID_INDEX || idx_reviver >= 0);
  32087. DUK_DDD(DUK_DDDPRINT("JSON parse start: text=%!T, reviver=%!T, flags=0x%08lx, stack_top=%ld",
  32088. (duk_tval *) duk_get_tval(thr, idx_value),
  32089. (duk_tval *) duk_get_tval(thr, idx_reviver),
  32090. (unsigned long) flags,
  32091. (long) duk_get_top(thr)));
  32092. duk_memzero(&js_ctx_alloc, sizeof(js_ctx_alloc));
  32093. js_ctx->thr = thr;
  32094. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  32095. /* nothing now */
  32096. #endif
  32097. js_ctx->recursion_limit = DUK_USE_JSON_DEC_RECLIMIT;
  32098. DUK_ASSERT(js_ctx->recursion_depth == 0);
  32099. /* Flag handling currently assumes that flags are consistent. This is OK
  32100. * because the call sites are now strictly controlled.
  32101. */
  32102. js_ctx->flags = flags;
  32103. #if defined(DUK_USE_JX)
  32104. js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM;
  32105. #endif
  32106. #if defined(DUK_USE_JC)
  32107. js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE;
  32108. #endif
  32109. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  32110. js_ctx->flag_ext_custom_or_compatible = flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE);
  32111. #endif
  32112. h_text = duk_to_hstring(thr, idx_value); /* coerce in-place; rejects Symbols */
  32113. DUK_ASSERT(h_text != NULL);
  32114. /* JSON parsing code is allowed to read [p_start,p_end]: p_end is
  32115. * valid and points to the string NUL terminator (which is always
  32116. * guaranteed for duk_hstrings.
  32117. */
  32118. js_ctx->p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text);
  32119. js_ctx->p = js_ctx->p_start;
  32120. js_ctx->p_end = ((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_text)) +
  32121. DUK_HSTRING_GET_BYTELEN(h_text);
  32122. DUK_ASSERT(*(js_ctx->p_end) == 0x00);
  32123. duk__dec_value(js_ctx); /* -> [ ... value ] */
  32124. /* Trailing whitespace has been eaten by duk__dec_value(), so if
  32125. * we're not at end of input here, it's a SyntaxError.
  32126. */
  32127. if (js_ctx->p != js_ctx->p_end) {
  32128. duk__dec_syntax_error(js_ctx);
  32129. }
  32130. if (duk_is_callable(thr, idx_reviver)) {
  32131. DUK_DDD(DUK_DDDPRINT("applying reviver: %!T",
  32132. (duk_tval *) duk_get_tval(thr, idx_reviver)));
  32133. js_ctx->idx_reviver = idx_reviver;
  32134. duk_push_object(thr);
  32135. duk_dup_m2(thr); /* -> [ ... val root val ] */
  32136. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_EMPTY_STRING); /* default attrs ok */
  32137. duk_push_hstring_stridx(thr, DUK_STRIDX_EMPTY_STRING); /* -> [ ... val root "" ] */
  32138. DUK_DDD(DUK_DDDPRINT("start reviver walk, root=%!T, name=%!T",
  32139. (duk_tval *) duk_get_tval(thr, -2),
  32140. (duk_tval *) duk_get_tval(thr, -1)));
  32141. duk__dec_reviver_walk(js_ctx); /* [ ... val root "" ] -> [ ... val val' ] */
  32142. duk_remove_m2(thr); /* -> [ ... val' ] */
  32143. } else {
  32144. DUK_DDD(DUK_DDDPRINT("reviver does not exist or is not callable: %!T",
  32145. (duk_tval *) duk_get_tval(thr, idx_reviver)));
  32146. }
  32147. /* Final result is at stack top. */
  32148. DUK_DDD(DUK_DDDPRINT("JSON parse end: text=%!T, reviver=%!T, flags=0x%08lx, result=%!T, stack_top=%ld",
  32149. (duk_tval *) duk_get_tval(thr, idx_value),
  32150. (duk_tval *) duk_get_tval(thr, idx_reviver),
  32151. (unsigned long) flags,
  32152. (duk_tval *) duk_get_tval(thr, -1),
  32153. (long) duk_get_top(thr)));
  32154. DUK_ASSERT(duk_get_top(thr) == entry_top + 1);
  32155. }
  32156. DUK_INTERNAL
  32157. void duk_bi_json_stringify_helper(duk_hthread *thr,
  32158. duk_idx_t idx_value,
  32159. duk_idx_t idx_replacer,
  32160. duk_idx_t idx_space,
  32161. duk_small_uint_t flags) {
  32162. duk_json_enc_ctx js_ctx_alloc;
  32163. duk_json_enc_ctx *js_ctx = &js_ctx_alloc;
  32164. duk_hobject *h;
  32165. duk_idx_t idx_holder;
  32166. duk_idx_t entry_top;
  32167. /* negative top-relative indices not allowed now */
  32168. DUK_ASSERT(idx_value == DUK_INVALID_INDEX || idx_value >= 0);
  32169. DUK_ASSERT(idx_replacer == DUK_INVALID_INDEX || idx_replacer >= 0);
  32170. DUK_ASSERT(idx_space == DUK_INVALID_INDEX || idx_space >= 0);
  32171. DUK_DDD(DUK_DDDPRINT("JSON stringify start: value=%!T, replacer=%!T, space=%!T, flags=0x%08lx, stack_top=%ld",
  32172. (duk_tval *) duk_get_tval(thr, idx_value),
  32173. (duk_tval *) duk_get_tval(thr, idx_replacer),
  32174. (duk_tval *) duk_get_tval(thr, idx_space),
  32175. (unsigned long) flags,
  32176. (long) duk_get_top(thr)));
  32177. entry_top = duk_get_top(thr);
  32178. /*
  32179. * Context init
  32180. */
  32181. duk_memzero(&js_ctx_alloc, sizeof(js_ctx_alloc));
  32182. js_ctx->thr = thr;
  32183. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  32184. js_ctx->h_replacer = NULL;
  32185. js_ctx->h_gap = NULL;
  32186. #endif
  32187. js_ctx->idx_proplist = -1;
  32188. /* Flag handling currently assumes that flags are consistent. This is OK
  32189. * because the call sites are now strictly controlled.
  32190. */
  32191. js_ctx->flags = flags;
  32192. js_ctx->flag_ascii_only = flags & DUK_JSON_FLAG_ASCII_ONLY;
  32193. js_ctx->flag_avoid_key_quotes = flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES;
  32194. #if defined(DUK_USE_JX)
  32195. js_ctx->flag_ext_custom = flags & DUK_JSON_FLAG_EXT_CUSTOM;
  32196. #endif
  32197. #if defined(DUK_USE_JC)
  32198. js_ctx->flag_ext_compatible = flags & DUK_JSON_FLAG_EXT_COMPATIBLE;
  32199. #endif
  32200. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  32201. js_ctx->flag_ext_custom_or_compatible = flags & (DUK_JSON_FLAG_EXT_CUSTOM | DUK_JSON_FLAG_EXT_COMPATIBLE);
  32202. #endif
  32203. /* The #if defined() clutter here handles the JX/JC enable/disable
  32204. * combinations properly.
  32205. */
  32206. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  32207. js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_NULL; /* standard JSON; array gaps */
  32208. #if defined(DUK_USE_JX)
  32209. if (flags & DUK_JSON_FLAG_EXT_CUSTOM) {
  32210. js_ctx->stridx_custom_undefined = DUK_STRIDX_LC_UNDEFINED;
  32211. js_ctx->stridx_custom_nan = DUK_STRIDX_NAN;
  32212. js_ctx->stridx_custom_neginf = DUK_STRIDX_MINUS_INFINITY;
  32213. js_ctx->stridx_custom_posinf = DUK_STRIDX_INFINITY;
  32214. js_ctx->stridx_custom_function =
  32215. (flags & DUK_JSON_FLAG_AVOID_KEY_QUOTES) ?
  32216. DUK_STRIDX_JSON_EXT_FUNCTION2 :
  32217. DUK_STRIDX_JSON_EXT_FUNCTION1;
  32218. }
  32219. #endif /* DUK_USE_JX */
  32220. #if defined(DUK_USE_JX) && defined(DUK_USE_JC)
  32221. else
  32222. #endif /* DUK_USE_JX && DUK_USE_JC */
  32223. #if defined(DUK_USE_JC)
  32224. if (js_ctx->flags & DUK_JSON_FLAG_EXT_COMPATIBLE) {
  32225. js_ctx->stridx_custom_undefined = DUK_STRIDX_JSON_EXT_UNDEFINED;
  32226. js_ctx->stridx_custom_nan = DUK_STRIDX_JSON_EXT_NAN;
  32227. js_ctx->stridx_custom_neginf = DUK_STRIDX_JSON_EXT_NEGINF;
  32228. js_ctx->stridx_custom_posinf = DUK_STRIDX_JSON_EXT_POSINF;
  32229. js_ctx->stridx_custom_function = DUK_STRIDX_JSON_EXT_FUNCTION1;
  32230. }
  32231. #endif /* DUK_USE_JC */
  32232. #endif /* DUK_USE_JX || DUK_USE_JC */
  32233. #if defined(DUK_USE_JX) || defined(DUK_USE_JC)
  32234. if (js_ctx->flags & (DUK_JSON_FLAG_EXT_CUSTOM |
  32235. DUK_JSON_FLAG_EXT_COMPATIBLE)) {
  32236. DUK_ASSERT(js_ctx->mask_for_undefined == 0); /* already zero */
  32237. }
  32238. else
  32239. #endif /* DUK_USE_JX || DUK_USE_JC */
  32240. {
  32241. /* Plain buffer is treated like ArrayBuffer and serialized.
  32242. * Lightfuncs are treated like objects, but JSON explicitly
  32243. * skips serializing Function objects so we can just reject
  32244. * lightfuncs here.
  32245. */
  32246. js_ctx->mask_for_undefined = DUK_TYPE_MASK_UNDEFINED |
  32247. DUK_TYPE_MASK_POINTER |
  32248. DUK_TYPE_MASK_LIGHTFUNC;
  32249. }
  32250. DUK_BW_INIT_PUSHBUF(thr, &js_ctx->bw, DUK__JSON_STRINGIFY_BUFSIZE);
  32251. js_ctx->idx_loop = duk_push_bare_object(thr);
  32252. DUK_ASSERT(js_ctx->idx_loop >= 0);
  32253. /* [ ... buf loop ] */
  32254. /*
  32255. * Process replacer/proplist (2nd argument to JSON.stringify)
  32256. */
  32257. h = duk_get_hobject(thr, idx_replacer);
  32258. if (h != NULL) {
  32259. if (DUK_HOBJECT_IS_CALLABLE(h)) {
  32260. js_ctx->h_replacer = h;
  32261. } else if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_ARRAY) {
  32262. /* Here the specification requires correct array index enumeration
  32263. * which is a bit tricky for sparse arrays (it is handled by the
  32264. * enum setup code). We now enumerate ancestors too, although the
  32265. * specification is not very clear on whether that is required.
  32266. */
  32267. duk_uarridx_t plist_idx = 0;
  32268. duk_small_uint_t enum_flags;
  32269. js_ctx->idx_proplist = duk_push_array(thr); /* XXX: array internal? */
  32270. enum_flags = DUK_ENUM_ARRAY_INDICES_ONLY |
  32271. DUK_ENUM_SORT_ARRAY_INDICES; /* expensive flag */
  32272. duk_enum(thr, idx_replacer, enum_flags);
  32273. while (duk_next(thr, -1 /*enum_index*/, 1 /*get_value*/)) {
  32274. /* [ ... proplist enum_obj key val ] */
  32275. if (duk__enc_allow_into_proplist(duk_get_tval(thr, -1))) {
  32276. /* XXX: duplicates should be eliminated here */
  32277. DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> accept",
  32278. (duk_tval *) duk_get_tval(thr, -2),
  32279. (duk_tval *) duk_get_tval(thr, -1)));
  32280. duk_to_string(thr, -1); /* extra coercion of strings is OK */
  32281. duk_put_prop_index(thr, -4, plist_idx); /* -> [ ... proplist enum_obj key ] */
  32282. plist_idx++;
  32283. duk_pop(thr);
  32284. } else {
  32285. DUK_DDD(DUK_DDDPRINT("proplist enum: key=%!T, val=%!T --> reject",
  32286. (duk_tval *) duk_get_tval(thr, -2),
  32287. (duk_tval *) duk_get_tval(thr, -1)));
  32288. duk_pop_2(thr);
  32289. }
  32290. }
  32291. duk_pop(thr); /* pop enum */
  32292. /* [ ... proplist ] */
  32293. }
  32294. }
  32295. /* [ ... buf loop (proplist) ] */
  32296. /*
  32297. * Process space (3rd argument to JSON.stringify)
  32298. */
  32299. h = duk_get_hobject(thr, idx_space);
  32300. if (h != NULL) {
  32301. duk_small_uint_t c = DUK_HOBJECT_GET_CLASS_NUMBER(h);
  32302. if (c == DUK_HOBJECT_CLASS_NUMBER) {
  32303. duk_to_number(thr, idx_space);
  32304. } else if (c == DUK_HOBJECT_CLASS_STRING) {
  32305. duk_to_string(thr, idx_space);
  32306. }
  32307. }
  32308. if (duk_is_number(thr, idx_space)) {
  32309. duk_small_int_t nspace;
  32310. /* spaces[] must be static to allow initializer with old compilers like BCC */
  32311. static const char spaces[10] = {
  32312. DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE,
  32313. DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE, DUK_ASC_SPACE,
  32314. DUK_ASC_SPACE, DUK_ASC_SPACE
  32315. }; /* XXX: helper */
  32316. /* ToInteger() coercion; NaN -> 0, infinities are clamped to 0 and 10 */
  32317. nspace = (duk_small_int_t) duk_to_int_clamped(thr, idx_space, 0 /*minval*/, 10 /*maxval*/);
  32318. DUK_ASSERT(nspace >= 0 && nspace <= 10);
  32319. duk_push_lstring(thr, spaces, (duk_size_t) nspace);
  32320. js_ctx->h_gap = duk_known_hstring(thr, -1);
  32321. DUK_ASSERT(js_ctx->h_gap != NULL);
  32322. } else if (duk_is_string_notsymbol(thr, idx_space)) {
  32323. duk_dup(thr, idx_space);
  32324. duk_substring(thr, -1, 0, 10); /* clamp to 10 chars */
  32325. js_ctx->h_gap = duk_known_hstring(thr, -1);
  32326. } else {
  32327. /* nop */
  32328. }
  32329. if (js_ctx->h_gap != NULL) {
  32330. /* If gap is empty, behave as if not given at all. Check
  32331. * against byte length because character length is more
  32332. * expensive.
  32333. */
  32334. if (DUK_HSTRING_GET_BYTELEN(js_ctx->h_gap) == 0) {
  32335. js_ctx->h_gap = NULL;
  32336. }
  32337. }
  32338. /* [ ... buf loop (proplist) (gap) ] */
  32339. /*
  32340. * Fast path: assume no mutation, iterate object property tables
  32341. * directly; bail out if that assumption doesn't hold.
  32342. */
  32343. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  32344. if (js_ctx->h_replacer == NULL && /* replacer is a mutation risk */
  32345. js_ctx->idx_proplist == -1) { /* proplist is very rare */
  32346. duk_int_t pcall_rc;
  32347. duk_small_uint_t prev_ms_base_flags;
  32348. DUK_DD(DUK_DDPRINT("try JSON.stringify() fast path"));
  32349. /* Use recursion_limit to ensure we don't overwrite js_ctx->visiting[]
  32350. * array so we don't need two counter checks in the fast path. The
  32351. * slow path has a much larger recursion limit which we'll use if
  32352. * necessary.
  32353. */
  32354. DUK_ASSERT(DUK_USE_JSON_ENC_RECLIMIT >= DUK_JSON_ENC_LOOPARRAY);
  32355. js_ctx->recursion_limit = DUK_JSON_ENC_LOOPARRAY;
  32356. DUK_ASSERT(js_ctx->recursion_depth == 0);
  32357. /* Execute the fast path in a protected call. If any error is thrown,
  32358. * fall back to the slow path. This includes e.g. recursion limit
  32359. * because the fast path has a smaller recursion limit (and simpler,
  32360. * limited loop detection).
  32361. */
  32362. duk_dup(thr, idx_value);
  32363. /* Must prevent finalizers which may have arbitrary side effects. */
  32364. prev_ms_base_flags = thr->heap->ms_base_flags;
  32365. thr->heap->ms_base_flags |=
  32366. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* Avoid attempt to compact any objects. */
  32367. thr->heap->pf_prevent_count++; /* Prevent finalizers. */
  32368. DUK_ASSERT(thr->heap->pf_prevent_count != 0); /* Wrap. */
  32369. pcall_rc = duk_safe_call(thr, duk__json_stringify_fast, (void *) js_ctx /*udata*/, 1 /*nargs*/, 0 /*nret*/);
  32370. DUK_ASSERT(thr->heap->pf_prevent_count > 0);
  32371. thr->heap->pf_prevent_count--;
  32372. thr->heap->ms_base_flags = prev_ms_base_flags;
  32373. if (pcall_rc == DUK_EXEC_SUCCESS) {
  32374. DUK_DD(DUK_DDPRINT("fast path successful"));
  32375. DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw);
  32376. goto replace_finished;
  32377. }
  32378. /* We come here for actual aborts (like encountering .toJSON())
  32379. * but also for recursion/loop errors. Bufwriter size can be
  32380. * kept because we'll probably need at least as much as we've
  32381. * allocated so far.
  32382. */
  32383. DUK_D(DUK_DPRINT("fast path failed, serialize using slow path instead"));
  32384. DUK_BW_RESET_SIZE(thr, &js_ctx->bw);
  32385. js_ctx->recursion_depth = 0;
  32386. }
  32387. #endif
  32388. /*
  32389. * Create wrapper object and serialize
  32390. */
  32391. idx_holder = duk_push_object(thr);
  32392. duk_dup(thr, idx_value);
  32393. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_EMPTY_STRING);
  32394. DUK_DDD(DUK_DDDPRINT("before: flags=0x%08lx, loop=%!T, replacer=%!O, "
  32395. "proplist=%!T, gap=%!O, holder=%!T",
  32396. (unsigned long) js_ctx->flags,
  32397. (duk_tval *) duk_get_tval(thr, js_ctx->idx_loop),
  32398. (duk_heaphdr *) js_ctx->h_replacer,
  32399. (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(thr, js_ctx->idx_proplist) : NULL),
  32400. (duk_heaphdr *) js_ctx->h_gap,
  32401. (duk_tval *) duk_get_tval(thr, -1)));
  32402. /* serialize the wrapper with empty string key */
  32403. duk_push_hstring_empty(thr);
  32404. /* [ ... buf loop (proplist) (gap) holder "" ] */
  32405. js_ctx->recursion_limit = DUK_USE_JSON_ENC_RECLIMIT;
  32406. DUK_ASSERT(js_ctx->recursion_depth == 0);
  32407. if (DUK_UNLIKELY(duk__enc_value(js_ctx, idx_holder) == 0)) { /* [ ... holder key ] -> [ ... holder ] */
  32408. /* Result is undefined. */
  32409. duk_push_undefined(thr);
  32410. } else {
  32411. /* Convert buffer to result string. */
  32412. DUK_BW_PUSH_AS_STRING(thr, &js_ctx->bw);
  32413. }
  32414. DUK_DDD(DUK_DDDPRINT("after: flags=0x%08lx, loop=%!T, replacer=%!O, "
  32415. "proplist=%!T, gap=%!O, holder=%!T",
  32416. (unsigned long) js_ctx->flags,
  32417. (duk_tval *) duk_get_tval(thr, js_ctx->idx_loop),
  32418. (duk_heaphdr *) js_ctx->h_replacer,
  32419. (duk_tval *) (js_ctx->idx_proplist >= 0 ? duk_get_tval(thr, js_ctx->idx_proplist) : NULL),
  32420. (duk_heaphdr *) js_ctx->h_gap,
  32421. (duk_tval *) duk_get_tval(thr, idx_holder)));
  32422. /* The stack has a variable shape here, so force it to the
  32423. * desired one explicitly.
  32424. */
  32425. #if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
  32426. replace_finished:
  32427. #endif
  32428. duk_replace(thr, entry_top);
  32429. duk_set_top(thr, entry_top + 1);
  32430. DUK_DDD(DUK_DDDPRINT("JSON stringify end: value=%!T, replacer=%!T, space=%!T, "
  32431. "flags=0x%08lx, result=%!T, stack_top=%ld",
  32432. (duk_tval *) duk_get_tval(thr, idx_value),
  32433. (duk_tval *) duk_get_tval(thr, idx_replacer),
  32434. (duk_tval *) duk_get_tval(thr, idx_space),
  32435. (unsigned long) flags,
  32436. (duk_tval *) duk_get_tval(thr, -1),
  32437. (long) duk_get_top(thr)));
  32438. DUK_ASSERT(duk_get_top(thr) == entry_top + 1);
  32439. }
  32440. #if defined(DUK_USE_JSON_BUILTIN)
  32441. /*
  32442. * Entry points
  32443. */
  32444. DUK_INTERNAL duk_ret_t duk_bi_json_object_parse(duk_hthread *thr) {
  32445. duk_bi_json_parse_helper(thr,
  32446. 0 /*idx_value*/,
  32447. 1 /*idx_replacer*/,
  32448. 0 /*flags*/);
  32449. return 1;
  32450. }
  32451. DUK_INTERNAL duk_ret_t duk_bi_json_object_stringify(duk_hthread *thr) {
  32452. duk_bi_json_stringify_helper(thr,
  32453. 0 /*idx_value*/,
  32454. 1 /*idx_replacer*/,
  32455. 2 /*idx_space*/,
  32456. 0 /*flags*/);
  32457. return 1;
  32458. }
  32459. #endif /* DUK_USE_JSON_BUILTIN */
  32460. #endif /* DUK_USE_JSON_SUPPORT */
  32461. /* automatic undefs */
  32462. #undef DUK__EMIT_1
  32463. #undef DUK__EMIT_2
  32464. #undef DUK__EMIT_CSTR
  32465. #undef DUK__EMIT_HSTR
  32466. #undef DUK__EMIT_STRIDX
  32467. #undef DUK__JSON_DECSTR_BUFSIZE
  32468. #undef DUK__JSON_DECSTR_CHUNKSIZE
  32469. #undef DUK__JSON_ENCSTR_CHUNKSIZE
  32470. #undef DUK__JSON_MAX_ESC_LEN
  32471. #undef DUK__JSON_STRINGIFY_BUFSIZE
  32472. #undef DUK__MKESC
  32473. #undef DUK__UNEMIT_1
  32474. #line 1 "duk_bi_math.c"
  32475. /*
  32476. * Math built-ins
  32477. */
  32478. /* #include duk_internal.h -> already included */
  32479. #if defined(DUK_USE_MATH_BUILTIN)
  32480. /*
  32481. * Use static helpers which can work with math.h functions matching
  32482. * the following signatures. This is not portable if any of these math
  32483. * functions is actually a macro.
  32484. *
  32485. * Typing here is intentionally 'double' wherever values interact with
  32486. * the standard library APIs.
  32487. */
  32488. typedef double (*duk__one_arg_func)(double);
  32489. typedef double (*duk__two_arg_func)(double, double);
  32490. DUK_LOCAL duk_ret_t duk__math_minmax(duk_hthread *thr, duk_double_t initial, duk__two_arg_func min_max) {
  32491. duk_idx_t n = duk_get_top(thr);
  32492. duk_idx_t i;
  32493. duk_double_t res = initial;
  32494. duk_double_t t;
  32495. /*
  32496. * Note: fmax() does not match the E5 semantics. E5 requires
  32497. * that if -any- input to Math.max() is a NaN, the result is a
  32498. * NaN. fmax() will return a NaN only if -both- inputs are NaN.
  32499. * Same applies to fmin().
  32500. *
  32501. * Note: every input value must be coerced with ToNumber(), even
  32502. * if we know the result will be a NaN anyway: ToNumber() may have
  32503. * side effects for which even order of evaluation matters.
  32504. */
  32505. for (i = 0; i < n; i++) {
  32506. t = duk_to_number(thr, i);
  32507. if (DUK_FPCLASSIFY(t) == DUK_FP_NAN || DUK_FPCLASSIFY(res) == DUK_FP_NAN) {
  32508. /* Note: not normalized, but duk_push_number() will normalize */
  32509. res = (duk_double_t) DUK_DOUBLE_NAN;
  32510. } else {
  32511. res = (duk_double_t) min_max(res, (double) t);
  32512. }
  32513. }
  32514. duk_push_number(thr, res);
  32515. return 1;
  32516. }
  32517. DUK_LOCAL double duk__fmin_fixed(double x, double y) {
  32518. /* fmin() with args -0 and +0 is not guaranteed to return
  32519. * -0 as ECMAScript requires.
  32520. */
  32521. if (x == 0 && y == 0) {
  32522. duk_double_union du1, du2;
  32523. du1.d = x;
  32524. du2.d = y;
  32525. /* Already checked to be zero so these must hold, and allow us
  32526. * to check for "x is -0 or y is -0" by ORing the high parts
  32527. * for comparison.
  32528. */
  32529. DUK_ASSERT(du1.ui[DUK_DBL_IDX_UI0] == 0 || du1.ui[DUK_DBL_IDX_UI0] == 0x80000000UL);
  32530. DUK_ASSERT(du2.ui[DUK_DBL_IDX_UI0] == 0 || du2.ui[DUK_DBL_IDX_UI0] == 0x80000000UL);
  32531. /* XXX: what's the safest way of creating a negative zero? */
  32532. if ((du1.ui[DUK_DBL_IDX_UI0] | du2.ui[DUK_DBL_IDX_UI0]) != 0) {
  32533. /* Enter here if either x or y (or both) is -0. */
  32534. return -0.0;
  32535. } else {
  32536. return +0.0;
  32537. }
  32538. }
  32539. return duk_double_fmin(x, y);
  32540. }
  32541. DUK_LOCAL double duk__fmax_fixed(double x, double y) {
  32542. /* fmax() with args -0 and +0 is not guaranteed to return
  32543. * +0 as ECMAScript requires.
  32544. */
  32545. if (x == 0 && y == 0) {
  32546. if (DUK_SIGNBIT(x) == 0 || DUK_SIGNBIT(y) == 0) {
  32547. return +0.0;
  32548. } else {
  32549. return -0.0;
  32550. }
  32551. }
  32552. return duk_double_fmax(x, y);
  32553. }
  32554. #if defined(DUK_USE_ES6)
  32555. DUK_LOCAL double duk__cbrt(double x) {
  32556. /* cbrt() is C99. To avoid hassling embedders with the need to provide a
  32557. * cube root function, we can get by with pow(). The result is not
  32558. * identical, but that's OK: ES2015 says it's implementation-dependent.
  32559. */
  32560. #if defined(DUK_CBRT)
  32561. /* cbrt() matches ES2015 requirements. */
  32562. return DUK_CBRT(x);
  32563. #else
  32564. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  32565. /* pow() does not, however. */
  32566. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) {
  32567. return x;
  32568. }
  32569. if (DUK_SIGNBIT(x)) {
  32570. return -DUK_POW(-x, 1.0 / 3.0);
  32571. } else {
  32572. return DUK_POW(x, 1.0 / 3.0);
  32573. }
  32574. #endif
  32575. }
  32576. DUK_LOCAL double duk__log2(double x) {
  32577. #if defined(DUK_LOG2)
  32578. return DUK_LOG2(x);
  32579. #else
  32580. return DUK_LOG(x) * DUK_DOUBLE_LOG2E;
  32581. #endif
  32582. }
  32583. DUK_LOCAL double duk__log10(double x) {
  32584. #if defined(DUK_LOG10)
  32585. return DUK_LOG10(x);
  32586. #else
  32587. return DUK_LOG(x) * DUK_DOUBLE_LOG10E;
  32588. #endif
  32589. }
  32590. DUK_LOCAL double duk__trunc(double x) {
  32591. #if defined(DUK_TRUNC)
  32592. return DUK_TRUNC(x);
  32593. #else
  32594. /* Handles -0 correctly: -0.0 matches 'x >= 0.0' but floor()
  32595. * is required to return -0 when the argument is -0.
  32596. */
  32597. return x >= 0.0 ? DUK_FLOOR(x) : DUK_CEIL(x);
  32598. #endif
  32599. }
  32600. #endif /* DUK_USE_ES6 */
  32601. DUK_LOCAL double duk__round_fixed(double x) {
  32602. /* Numbers half-way between integers must be rounded towards +Infinity,
  32603. * e.g. -3.5 must be rounded to -3 (not -4). When rounded to zero, zero
  32604. * sign must be set appropriately. E5.1 Section 15.8.2.15.
  32605. *
  32606. * Note that ANSI C round() is "round to nearest integer, away from zero",
  32607. * which is incorrect for negative values. Here we make do with floor().
  32608. */
  32609. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  32610. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE || c == DUK_FP_ZERO) {
  32611. return x;
  32612. }
  32613. /*
  32614. * x is finite and non-zero
  32615. *
  32616. * -1.6 -> floor(-1.1) -> -2
  32617. * -1.5 -> floor(-1.0) -> -1 (towards +Inf)
  32618. * -1.4 -> floor(-0.9) -> -1
  32619. * -0.5 -> -0.0 (special case)
  32620. * -0.1 -> -0.0 (special case)
  32621. * +0.1 -> +0.0 (special case)
  32622. * +0.5 -> floor(+1.0) -> 1 (towards +Inf)
  32623. * +1.4 -> floor(+1.9) -> 1
  32624. * +1.5 -> floor(+2.0) -> 2 (towards +Inf)
  32625. * +1.6 -> floor(+2.1) -> 2
  32626. */
  32627. if (x >= -0.5 && x < 0.5) {
  32628. /* +0.5 is handled by floor, this is on purpose */
  32629. if (x < 0.0) {
  32630. return -0.0;
  32631. } else {
  32632. return +0.0;
  32633. }
  32634. }
  32635. return DUK_FLOOR(x + 0.5);
  32636. }
  32637. /* Wrappers for calling standard math library methods. These may be required
  32638. * on platforms where one or more of the math built-ins are defined as macros
  32639. * or inline functions and are thus not suitable to be used as function pointers.
  32640. */
  32641. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  32642. DUK_LOCAL double duk__fabs(double x) {
  32643. return DUK_FABS(x);
  32644. }
  32645. DUK_LOCAL double duk__acos(double x) {
  32646. return DUK_ACOS(x);
  32647. }
  32648. DUK_LOCAL double duk__asin(double x) {
  32649. return DUK_ASIN(x);
  32650. }
  32651. DUK_LOCAL double duk__atan(double x) {
  32652. return DUK_ATAN(x);
  32653. }
  32654. DUK_LOCAL double duk__ceil(double x) {
  32655. return DUK_CEIL(x);
  32656. }
  32657. DUK_LOCAL double duk__cos(double x) {
  32658. return DUK_COS(x);
  32659. }
  32660. DUK_LOCAL double duk__exp(double x) {
  32661. return DUK_EXP(x);
  32662. }
  32663. DUK_LOCAL double duk__floor(double x) {
  32664. return DUK_FLOOR(x);
  32665. }
  32666. DUK_LOCAL double duk__log(double x) {
  32667. return DUK_LOG(x);
  32668. }
  32669. DUK_LOCAL double duk__sin(double x) {
  32670. return DUK_SIN(x);
  32671. }
  32672. DUK_LOCAL double duk__sqrt(double x) {
  32673. return DUK_SQRT(x);
  32674. }
  32675. DUK_LOCAL double duk__tan(double x) {
  32676. return DUK_TAN(x);
  32677. }
  32678. DUK_LOCAL double duk__atan2_fixed(double x, double y) {
  32679. #if defined(DUK_USE_ATAN2_WORKAROUNDS)
  32680. /* Specific fixes to common atan2() implementation issues:
  32681. * - test-bug-mingw-math-issues.js
  32682. */
  32683. if (DUK_ISINF(x) && DUK_ISINF(y)) {
  32684. if (DUK_SIGNBIT(x)) {
  32685. if (DUK_SIGNBIT(y)) {
  32686. return -2.356194490192345;
  32687. } else {
  32688. return -0.7853981633974483;
  32689. }
  32690. } else {
  32691. if (DUK_SIGNBIT(y)) {
  32692. return 2.356194490192345;
  32693. } else {
  32694. return 0.7853981633974483;
  32695. }
  32696. }
  32697. }
  32698. #else
  32699. /* Some ISO C assumptions. */
  32700. DUK_ASSERT(DUK_ATAN2(DUK_DOUBLE_INFINITY, DUK_DOUBLE_INFINITY) == 0.7853981633974483);
  32701. DUK_ASSERT(DUK_ATAN2(-DUK_DOUBLE_INFINITY, DUK_DOUBLE_INFINITY) == -0.7853981633974483);
  32702. DUK_ASSERT(DUK_ATAN2(DUK_DOUBLE_INFINITY, -DUK_DOUBLE_INFINITY) == 2.356194490192345);
  32703. DUK_ASSERT(DUK_ATAN2(-DUK_DOUBLE_INFINITY, -DUK_DOUBLE_INFINITY) == -2.356194490192345);
  32704. #endif
  32705. return DUK_ATAN2(x, y);
  32706. }
  32707. #endif /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
  32708. /* order must match constants in genbuiltins.py */
  32709. DUK_LOCAL const duk__one_arg_func duk__one_arg_funcs[] = {
  32710. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  32711. duk__fabs,
  32712. duk__acos,
  32713. duk__asin,
  32714. duk__atan,
  32715. duk__ceil,
  32716. duk__cos,
  32717. duk__exp,
  32718. duk__floor,
  32719. duk__log,
  32720. duk__round_fixed,
  32721. duk__sin,
  32722. duk__sqrt,
  32723. duk__tan,
  32724. #if defined(DUK_USE_ES6)
  32725. duk__cbrt,
  32726. duk__log2,
  32727. duk__log10,
  32728. duk__trunc
  32729. #endif
  32730. #else /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
  32731. DUK_FABS,
  32732. DUK_ACOS,
  32733. DUK_ASIN,
  32734. DUK_ATAN,
  32735. DUK_CEIL,
  32736. DUK_COS,
  32737. DUK_EXP,
  32738. DUK_FLOOR,
  32739. DUK_LOG,
  32740. duk__round_fixed,
  32741. DUK_SIN,
  32742. DUK_SQRT,
  32743. DUK_TAN,
  32744. #if defined(DUK_USE_ES6)
  32745. duk__cbrt,
  32746. duk__log2,
  32747. duk__log10,
  32748. duk__trunc
  32749. #endif
  32750. #endif /* DUK_USE_AVOID_PLATFORM_FUNCPTRS */
  32751. };
  32752. /* order must match constants in genbuiltins.py */
  32753. DUK_LOCAL const duk__two_arg_func duk__two_arg_funcs[] = {
  32754. #if defined(DUK_USE_AVOID_PLATFORM_FUNCPTRS)
  32755. duk__atan2_fixed,
  32756. duk_js_arith_pow
  32757. #else
  32758. duk__atan2_fixed,
  32759. duk_js_arith_pow
  32760. #endif
  32761. };
  32762. DUK_INTERNAL duk_ret_t duk_bi_math_object_onearg_shared(duk_hthread *thr) {
  32763. duk_small_int_t fun_idx = duk_get_current_magic(thr);
  32764. duk__one_arg_func fun;
  32765. duk_double_t arg1;
  32766. DUK_ASSERT(fun_idx >= 0);
  32767. DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__one_arg_funcs) / sizeof(duk__one_arg_func)));
  32768. arg1 = duk_to_number(thr, 0);
  32769. fun = duk__one_arg_funcs[fun_idx];
  32770. duk_push_number(thr, (duk_double_t) fun((double) arg1));
  32771. return 1;
  32772. }
  32773. DUK_INTERNAL duk_ret_t duk_bi_math_object_twoarg_shared(duk_hthread *thr) {
  32774. duk_small_int_t fun_idx = duk_get_current_magic(thr);
  32775. duk__two_arg_func fun;
  32776. duk_double_t arg1;
  32777. duk_double_t arg2;
  32778. DUK_ASSERT(fun_idx >= 0);
  32779. DUK_ASSERT(fun_idx < (duk_small_int_t) (sizeof(duk__two_arg_funcs) / sizeof(duk__two_arg_func)));
  32780. arg1 = duk_to_number(thr, 0); /* explicit ordered evaluation to match coercion semantics */
  32781. arg2 = duk_to_number(thr, 1);
  32782. fun = duk__two_arg_funcs[fun_idx];
  32783. duk_push_number(thr, (duk_double_t) fun((double) arg1, (double) arg2));
  32784. return 1;
  32785. }
  32786. DUK_INTERNAL duk_ret_t duk_bi_math_object_max(duk_hthread *thr) {
  32787. return duk__math_minmax(thr, -DUK_DOUBLE_INFINITY, duk__fmax_fixed);
  32788. }
  32789. DUK_INTERNAL duk_ret_t duk_bi_math_object_min(duk_hthread *thr) {
  32790. return duk__math_minmax(thr, DUK_DOUBLE_INFINITY, duk__fmin_fixed);
  32791. }
  32792. DUK_INTERNAL duk_ret_t duk_bi_math_object_random(duk_hthread *thr) {
  32793. duk_push_number(thr, (duk_double_t) DUK_UTIL_GET_RANDOM_DOUBLE(thr));
  32794. return 1;
  32795. }
  32796. #if defined(DUK_USE_ES6)
  32797. DUK_INTERNAL duk_ret_t duk_bi_math_object_hypot(duk_hthread *thr) {
  32798. /*
  32799. * E6 Section 20.2.2.18: Math.hypot
  32800. *
  32801. * - If no arguments are passed, the result is +0.
  32802. * - If any argument is +inf, the result is +inf.
  32803. * - If any argument is -inf, the result is +inf.
  32804. * - If no argument is +inf or -inf, and any argument is NaN, the result is
  32805. * NaN.
  32806. * - If all arguments are either +0 or -0, the result is +0.
  32807. */
  32808. duk_idx_t nargs;
  32809. duk_idx_t i;
  32810. duk_bool_t found_nan;
  32811. duk_double_t max;
  32812. duk_double_t sum, summand;
  32813. duk_double_t comp, prelim;
  32814. duk_double_t t;
  32815. nargs = duk_get_top(thr);
  32816. /* Find the highest value. Also ToNumber() coerces. */
  32817. max = 0.0;
  32818. found_nan = 0;
  32819. for (i = 0; i < nargs; i++) {
  32820. t = DUK_FABS(duk_to_number(thr, i));
  32821. if (DUK_FPCLASSIFY(t) == DUK_FP_NAN) {
  32822. found_nan = 1;
  32823. } else {
  32824. max = duk_double_fmax(max, t);
  32825. }
  32826. }
  32827. /* Early return cases. */
  32828. if (max == DUK_DOUBLE_INFINITY) {
  32829. duk_push_number(thr, DUK_DOUBLE_INFINITY);
  32830. return 1;
  32831. } else if (found_nan) {
  32832. duk_push_number(thr, DUK_DOUBLE_NAN);
  32833. return 1;
  32834. } else if (max == 0.0) {
  32835. duk_push_number(thr, 0.0);
  32836. /* Otherwise we'd divide by zero. */
  32837. return 1;
  32838. }
  32839. /* Use Kahan summation and normalize to the highest value to minimize
  32840. * floating point rounding error and avoid overflow.
  32841. *
  32842. * https://en.wikipedia.org/wiki/Kahan_summation_algorithm
  32843. */
  32844. sum = 0.0;
  32845. comp = 0.0;
  32846. for (i = 0; i < nargs; i++) {
  32847. t = DUK_FABS(duk_get_number(thr, i)) / max;
  32848. summand = (t * t) - comp;
  32849. prelim = sum + summand;
  32850. comp = (prelim - sum) - summand;
  32851. sum = prelim;
  32852. }
  32853. duk_push_number(thr, (duk_double_t) DUK_SQRT(sum) * max);
  32854. return 1;
  32855. }
  32856. #endif /* DUK_USE_ES6 */
  32857. #if defined(DUK_USE_ES6)
  32858. DUK_INTERNAL duk_ret_t duk_bi_math_object_sign(duk_hthread *thr) {
  32859. duk_double_t d;
  32860. d = duk_to_number(thr, 0);
  32861. if (duk_double_is_nan(d)) {
  32862. DUK_ASSERT(duk_is_nan(thr, -1));
  32863. return 1; /* NaN input -> return NaN */
  32864. }
  32865. if (d == 0.0) {
  32866. /* Zero sign kept, i.e. -0 -> -0, +0 -> +0. */
  32867. return 1;
  32868. }
  32869. duk_push_int(thr, (d > 0.0 ? 1 : -1));
  32870. return 1;
  32871. }
  32872. #endif /* DUK_USE_ES6 */
  32873. #if defined(DUK_USE_ES6)
  32874. DUK_INTERNAL duk_ret_t duk_bi_math_object_clz32(duk_hthread *thr) {
  32875. duk_uint32_t x;
  32876. duk_small_uint_t i;
  32877. #if defined(DUK_USE_PREFER_SIZE)
  32878. duk_uint32_t mask;
  32879. x = duk_to_uint32(thr, 0);
  32880. for (i = 0, mask = 0x80000000UL; mask != 0; mask >>= 1) {
  32881. if (x & mask) {
  32882. break;
  32883. }
  32884. i++;
  32885. }
  32886. DUK_ASSERT(i <= 32);
  32887. duk_push_uint(thr, i);
  32888. return 1;
  32889. #else /* DUK_USE_PREFER_SIZE */
  32890. i = 0;
  32891. x = duk_to_uint32(thr, 0);
  32892. if (x & 0xffff0000UL) {
  32893. x >>= 16;
  32894. } else {
  32895. i += 16;
  32896. }
  32897. if (x & 0x0000ff00UL) {
  32898. x >>= 8;
  32899. } else {
  32900. i += 8;
  32901. }
  32902. if (x & 0x000000f0UL) {
  32903. x >>= 4;
  32904. } else {
  32905. i += 4;
  32906. }
  32907. if (x & 0x0000000cUL) {
  32908. x >>= 2;
  32909. } else {
  32910. i += 2;
  32911. }
  32912. if (x & 0x00000002UL) {
  32913. x >>= 1;
  32914. } else {
  32915. i += 1;
  32916. }
  32917. if (x & 0x00000001UL) {
  32918. ;
  32919. } else {
  32920. i += 1;
  32921. }
  32922. DUK_ASSERT(i <= 32);
  32923. duk_push_uint(thr, i);
  32924. return 1;
  32925. #endif /* DUK_USE_PREFER_SIZE */
  32926. }
  32927. #endif /* DUK_USE_ES6 */
  32928. #if defined(DUK_USE_ES6)
  32929. DUK_INTERNAL duk_ret_t duk_bi_math_object_imul(duk_hthread *thr) {
  32930. duk_uint32_t x, y, z;
  32931. x = duk_to_uint32(thr, 0);
  32932. y = duk_to_uint32(thr, 1);
  32933. z = x * y;
  32934. /* While arguments are ToUint32() coerced and the multiplication
  32935. * is unsigned as such, the final result is curiously interpreted
  32936. * as a signed 32-bit value.
  32937. */
  32938. duk_push_i32(thr, (duk_int32_t) z);
  32939. return 1;
  32940. }
  32941. #endif /* DUK_USE_ES6 */
  32942. #endif /* DUK_USE_MATH_BUILTIN */
  32943. #line 1 "duk_bi_number.c"
  32944. /*
  32945. * Number built-ins
  32946. */
  32947. /* #include duk_internal.h -> already included */
  32948. #if defined(DUK_USE_NUMBER_BUILTIN)
  32949. DUK_LOCAL duk_double_t duk__push_this_number_plain(duk_hthread *thr) {
  32950. duk_hobject *h;
  32951. /* Number built-in accepts a plain number or a Number object (whose
  32952. * internal value is operated on). Other types cause TypeError.
  32953. */
  32954. duk_push_this(thr);
  32955. if (duk_is_number(thr, -1)) {
  32956. DUK_DDD(DUK_DDDPRINT("plain number value: %!T", (duk_tval *) duk_get_tval(thr, -1)));
  32957. goto done;
  32958. }
  32959. h = duk_get_hobject(thr, -1);
  32960. if (!h ||
  32961. (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_NUMBER)) {
  32962. DUK_DDD(DUK_DDDPRINT("unacceptable this value: %!T", (duk_tval *) duk_get_tval(thr, -1)));
  32963. DUK_ERROR_TYPE(thr, "number expected");
  32964. DUK_WO_NORETURN(return 0.0;);
  32965. }
  32966. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
  32967. DUK_ASSERT(duk_is_number(thr, -1));
  32968. DUK_DDD(DUK_DDDPRINT("number object: %!T, internal value: %!T",
  32969. (duk_tval *) duk_get_tval(thr, -2), (duk_tval *) duk_get_tval(thr, -1)));
  32970. duk_remove_m2(thr);
  32971. done:
  32972. return duk_get_number(thr, -1);
  32973. }
  32974. DUK_INTERNAL duk_ret_t duk_bi_number_constructor(duk_hthread *thr) {
  32975. duk_idx_t nargs;
  32976. duk_hobject *h_this;
  32977. /*
  32978. * The Number constructor uses ToNumber(arg) for number coercion
  32979. * (coercing an undefined argument to NaN). However, if the
  32980. * argument is not given at all, +0 must be used instead. To do
  32981. * this, a vararg function is used.
  32982. */
  32983. nargs = duk_get_top(thr);
  32984. if (nargs == 0) {
  32985. duk_push_int(thr, 0);
  32986. }
  32987. duk_to_number(thr, 0);
  32988. duk_set_top(thr, 1);
  32989. DUK_ASSERT_TOP(thr, 1);
  32990. if (!duk_is_constructor_call(thr)) {
  32991. return 1;
  32992. }
  32993. /*
  32994. * E5 Section 15.7.2.1 requires that the constructed object
  32995. * must have the original Number.prototype as its internal
  32996. * prototype. However, since Number.prototype is non-writable
  32997. * and non-configurable, this doesn't have to be enforced here:
  32998. * The default object (bound to 'this') is OK, though we have
  32999. * to change its class.
  33000. *
  33001. * Internal value set to ToNumber(arg) or +0; if no arg given,
  33002. * ToNumber(undefined) = NaN, so special treatment is needed
  33003. * (above). String internal value is immutable.
  33004. */
  33005. /* XXX: helper */
  33006. duk_push_this(thr);
  33007. h_this = duk_known_hobject(thr, -1);
  33008. DUK_HOBJECT_SET_CLASS_NUMBER(h_this, DUK_HOBJECT_CLASS_NUMBER);
  33009. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_this) == thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE]);
  33010. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_this) == DUK_HOBJECT_CLASS_NUMBER);
  33011. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_this));
  33012. duk_dup_0(thr); /* -> [ val obj val ] */
  33013. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  33014. return 0; /* no return value -> don't replace created value */
  33015. }
  33016. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_value_of(duk_hthread *thr) {
  33017. (void) duk__push_this_number_plain(thr);
  33018. return 1;
  33019. }
  33020. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_string(duk_hthread *thr) {
  33021. duk_small_int_t radix;
  33022. duk_small_uint_t n2s_flags;
  33023. (void) duk__push_this_number_plain(thr);
  33024. if (duk_is_undefined(thr, 0)) {
  33025. radix = 10;
  33026. } else {
  33027. radix = (duk_small_int_t) duk_to_int_check_range(thr, 0, 2, 36);
  33028. }
  33029. DUK_DDD(DUK_DDDPRINT("radix=%ld", (long) radix));
  33030. n2s_flags = 0;
  33031. duk_numconv_stringify(thr,
  33032. radix /*radix*/,
  33033. 0 /*digits*/,
  33034. n2s_flags /*flags*/);
  33035. return 1;
  33036. }
  33037. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_locale_string(duk_hthread *thr) {
  33038. /* XXX: just use toString() for now; permitted although not recommended.
  33039. * nargs==1, so radix is passed to toString().
  33040. */
  33041. return duk_bi_number_prototype_to_string(thr);
  33042. }
  33043. /*
  33044. * toFixed(), toExponential(), toPrecision()
  33045. */
  33046. /* XXX: shared helper for toFixed(), toExponential(), toPrecision()? */
  33047. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_fixed(duk_hthread *thr) {
  33048. duk_small_int_t frac_digits;
  33049. duk_double_t d;
  33050. duk_small_int_t c;
  33051. duk_small_uint_t n2s_flags;
  33052. /* In ES5.1 frac_digits is coerced first; in ES2015 the 'this number
  33053. * value' check is done first.
  33054. */
  33055. d = duk__push_this_number_plain(thr);
  33056. frac_digits = (duk_small_int_t) duk_to_int_check_range(thr, 0, 0, 20);
  33057. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  33058. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  33059. goto use_to_string;
  33060. }
  33061. if (d >= 1.0e21 || d <= -1.0e21) {
  33062. goto use_to_string;
  33063. }
  33064. n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
  33065. DUK_N2S_FLAG_FRACTION_DIGITS;
  33066. duk_numconv_stringify(thr,
  33067. 10 /*radix*/,
  33068. frac_digits /*digits*/,
  33069. n2s_flags /*flags*/);
  33070. return 1;
  33071. use_to_string:
  33072. DUK_ASSERT_TOP(thr, 2);
  33073. duk_to_string(thr, -1);
  33074. return 1;
  33075. }
  33076. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_exponential(duk_hthread *thr) {
  33077. duk_bool_t frac_undefined;
  33078. duk_small_int_t frac_digits;
  33079. duk_double_t d;
  33080. duk_small_int_t c;
  33081. duk_small_uint_t n2s_flags;
  33082. d = duk__push_this_number_plain(thr);
  33083. frac_undefined = duk_is_undefined(thr, 0);
  33084. duk_to_int(thr, 0); /* for side effects */
  33085. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  33086. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  33087. goto use_to_string;
  33088. }
  33089. frac_digits = (duk_small_int_t) duk_to_int_check_range(thr, 0, 0, 20);
  33090. n2s_flags = DUK_N2S_FLAG_FORCE_EXP |
  33091. (frac_undefined ? 0 : DUK_N2S_FLAG_FIXED_FORMAT);
  33092. duk_numconv_stringify(thr,
  33093. 10 /*radix*/,
  33094. frac_digits + 1 /*leading digit + fractions*/,
  33095. n2s_flags /*flags*/);
  33096. return 1;
  33097. use_to_string:
  33098. DUK_ASSERT_TOP(thr, 2);
  33099. duk_to_string(thr, -1);
  33100. return 1;
  33101. }
  33102. DUK_INTERNAL duk_ret_t duk_bi_number_prototype_to_precision(duk_hthread *thr) {
  33103. /* The specification has quite awkward order of coercion and
  33104. * checks for toPrecision(). The operations below are a bit
  33105. * reordered, within constraints of observable side effects.
  33106. */
  33107. duk_double_t d;
  33108. duk_small_int_t prec;
  33109. duk_small_int_t c;
  33110. duk_small_uint_t n2s_flags;
  33111. DUK_ASSERT_TOP(thr, 1);
  33112. d = duk__push_this_number_plain(thr);
  33113. if (duk_is_undefined(thr, 0)) {
  33114. goto use_to_string;
  33115. }
  33116. DUK_ASSERT_TOP(thr, 2);
  33117. duk_to_int(thr, 0); /* for side effects */
  33118. c = (duk_small_int_t) DUK_FPCLASSIFY(d);
  33119. if (c == DUK_FP_NAN || c == DUK_FP_INFINITE) {
  33120. goto use_to_string;
  33121. }
  33122. prec = (duk_small_int_t) duk_to_int_check_range(thr, 0, 1, 21);
  33123. n2s_flags = DUK_N2S_FLAG_FIXED_FORMAT |
  33124. DUK_N2S_FLAG_NO_ZERO_PAD;
  33125. duk_numconv_stringify(thr,
  33126. 10 /*radix*/,
  33127. prec /*digits*/,
  33128. n2s_flags /*flags*/);
  33129. return 1;
  33130. use_to_string:
  33131. /* Used when precision is undefined; also used for NaN (-> "NaN"),
  33132. * and +/- infinity (-> "Infinity", "-Infinity").
  33133. */
  33134. DUK_ASSERT_TOP(thr, 2);
  33135. duk_to_string(thr, -1);
  33136. return 1;
  33137. }
  33138. /*
  33139. * ES2015 isFinite() etc
  33140. */
  33141. #if defined(DUK_USE_ES6)
  33142. DUK_INTERNAL duk_ret_t duk_bi_number_check_shared(duk_hthread *thr) {
  33143. duk_int_t magic;
  33144. duk_bool_t ret = 0;
  33145. if (duk_is_number(thr, 0)) {
  33146. duk_double_t d;
  33147. magic = duk_get_current_magic(thr);
  33148. d = duk_get_number(thr, 0);
  33149. switch (magic) {
  33150. case 0: /* isFinite() */
  33151. ret = duk_double_is_finite(d);
  33152. break;
  33153. case 1: /* isInteger() */
  33154. ret = duk_double_is_integer(d);
  33155. break;
  33156. case 2: /* isNaN() */
  33157. ret = duk_double_is_nan(d);
  33158. break;
  33159. default: /* isSafeInteger() */
  33160. DUK_ASSERT(magic == 3);
  33161. ret = duk_double_is_safe_integer(d);
  33162. }
  33163. }
  33164. duk_push_boolean(thr, ret);
  33165. return 1;
  33166. }
  33167. #endif /* DUK_USE_ES6 */
  33168. #endif /* DUK_USE_NUMBER_BUILTIN */
  33169. #line 1 "duk_bi_object.c"
  33170. /*
  33171. * Object built-ins
  33172. */
  33173. /* #include duk_internal.h -> already included */
  33174. /* Needed even when Object built-in disabled. */
  33175. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_to_string(duk_hthread *thr) {
  33176. duk_tval *tv;
  33177. tv = DUK_HTHREAD_THIS_PTR(thr);
  33178. duk_push_class_string_tval(thr, tv, 0 /*avoid_side_effects*/);
  33179. return 1;
  33180. }
  33181. #if defined(DUK_USE_OBJECT_BUILTIN)
  33182. DUK_INTERNAL duk_ret_t duk_bi_object_constructor(duk_hthread *thr) {
  33183. duk_uint_t arg_mask;
  33184. arg_mask = duk_get_type_mask(thr, 0);
  33185. if (!duk_is_constructor_call(thr) && /* not a constructor call */
  33186. ((arg_mask & (DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_UNDEFINED)) == 0)) { /* and argument not null or undefined */
  33187. duk_to_object(thr, 0);
  33188. return 1;
  33189. }
  33190. /* Pointer and buffer primitive values are treated like other
  33191. * primitives values which have a fully fledged object counterpart:
  33192. * promote to an object value. Lightfuncs and plain buffers are
  33193. * coerced with ToObject() even they could also be returned as is.
  33194. */
  33195. if (arg_mask & (DUK_TYPE_MASK_OBJECT |
  33196. DUK_TYPE_MASK_STRING |
  33197. DUK_TYPE_MASK_BOOLEAN |
  33198. DUK_TYPE_MASK_NUMBER |
  33199. DUK_TYPE_MASK_POINTER |
  33200. DUK_TYPE_MASK_BUFFER |
  33201. DUK_TYPE_MASK_LIGHTFUNC)) {
  33202. /* For DUK_TYPE_OBJECT the coercion is a no-op and could
  33203. * be checked for explicitly, but Object(obj) calls are
  33204. * not very common so opt for minimal footprint.
  33205. */
  33206. duk_to_object(thr, 0);
  33207. return 1;
  33208. }
  33209. (void) duk_push_object_helper(thr,
  33210. DUK_HOBJECT_FLAG_EXTENSIBLE |
  33211. DUK_HOBJECT_FLAG_FASTREFS |
  33212. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  33213. DUK_BIDX_OBJECT_PROTOTYPE);
  33214. return 1;
  33215. }
  33216. #endif /* DUK_USE_OBJECT_BUILTIN */
  33217. #if defined(DUK_USE_OBJECT_BUILTIN) && defined(DUK_USE_ES6)
  33218. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_assign(duk_hthread *thr) {
  33219. duk_idx_t nargs;
  33220. duk_int_t idx;
  33221. nargs = duk_get_top_require_min(thr, 1 /*min_top*/);
  33222. duk_to_object(thr, 0);
  33223. for (idx = 1; idx < nargs; idx++) {
  33224. /* E7 19.1.2.1 (step 4a) */
  33225. if (duk_is_null_or_undefined(thr, idx)) {
  33226. continue;
  33227. }
  33228. /* duk_enum() respects ES2015+ [[OwnPropertyKeys]] ordering, which is
  33229. * convenient here.
  33230. */
  33231. duk_to_object(thr, idx);
  33232. duk_enum(thr, idx, DUK_ENUM_OWN_PROPERTIES_ONLY);
  33233. while (duk_next(thr, -1, 1 /*get_value*/)) {
  33234. /* [ target ... enum key value ] */
  33235. duk_put_prop(thr, 0);
  33236. /* [ target ... enum ] */
  33237. }
  33238. /* Could pop enumerator, but unnecessary because of duk_set_top()
  33239. * below.
  33240. */
  33241. }
  33242. duk_set_top(thr, 1);
  33243. return 1;
  33244. }
  33245. #endif
  33246. #if defined(DUK_USE_OBJECT_BUILTIN) && defined(DUK_USE_ES6)
  33247. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is(duk_hthread *thr) {
  33248. DUK_ASSERT_TOP(thr, 2);
  33249. duk_push_boolean(thr, duk_samevalue(thr, 0, 1));
  33250. return 1;
  33251. }
  33252. #endif
  33253. #if defined(DUK_USE_OBJECT_BUILTIN)
  33254. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_create(duk_hthread *thr) {
  33255. duk_hobject *proto;
  33256. DUK_ASSERT_TOP(thr, 2);
  33257. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  33258. duk_hbufobj_promote_plain(thr, 0);
  33259. #endif
  33260. proto = duk_require_hobject_accept_mask(thr, 0, DUK_TYPE_MASK_NULL);
  33261. DUK_ASSERT(proto != NULL || duk_is_null(thr, 0));
  33262. (void) duk_push_object_helper_proto(thr,
  33263. DUK_HOBJECT_FLAG_EXTENSIBLE |
  33264. DUK_HOBJECT_FLAG_FASTREFS |
  33265. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  33266. proto);
  33267. if (!duk_is_undefined(thr, 1)) {
  33268. /* [ O Properties obj ] */
  33269. duk_replace(thr, 0);
  33270. /* [ obj Properties ] */
  33271. /* Just call the "original" Object.defineProperties() to
  33272. * finish up.
  33273. */
  33274. return duk_bi_object_constructor_define_properties(thr);
  33275. }
  33276. /* [ O Properties obj ] */
  33277. return 1;
  33278. }
  33279. #endif /* DUK_USE_OBJECT_BUILTIN */
  33280. #if defined(DUK_USE_OBJECT_BUILTIN)
  33281. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_properties(duk_hthread *thr) {
  33282. duk_small_uint_t pass;
  33283. duk_uint_t defprop_flags;
  33284. duk_hobject *obj;
  33285. duk_idx_t idx_value;
  33286. duk_hobject *get;
  33287. duk_hobject *set;
  33288. /* Lightfunc and plain buffer handling by ToObject() coercion. */
  33289. obj = duk_require_hobject_promote_mask(thr, 0, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  33290. DUK_ASSERT(obj != NULL);
  33291. duk_to_object(thr, 1); /* properties object */
  33292. DUK_DDD(DUK_DDDPRINT("target=%!iT, properties=%!iT",
  33293. (duk_tval *) duk_get_tval(thr, 0),
  33294. (duk_tval *) duk_get_tval(thr, 1)));
  33295. /*
  33296. * Two pass approach to processing the property descriptors.
  33297. * On first pass validate and normalize all descriptors before
  33298. * any changes are made to the target object. On second pass
  33299. * make the actual modifications to the target object.
  33300. *
  33301. * Right now we'll just use the same normalize/validate helper
  33302. * on both passes, ignoring its outputs on the first pass.
  33303. */
  33304. for (pass = 0; pass < 2; pass++) {
  33305. duk_set_top(thr, 2); /* -> [ hobject props ] */
  33306. duk_enum(thr, 1, DUK_ENUM_OWN_PROPERTIES_ONLY | DUK_ENUM_INCLUDE_SYMBOLS /*enum_flags*/);
  33307. for (;;) {
  33308. duk_hstring *key;
  33309. /* [ hobject props enum(props) ] */
  33310. duk_set_top(thr, 3);
  33311. if (!duk_next(thr, 2, 1 /*get_value*/)) {
  33312. break;
  33313. }
  33314. DUK_DDD(DUK_DDDPRINT("-> key=%!iT, desc=%!iT",
  33315. (duk_tval *) duk_get_tval(thr, -2),
  33316. (duk_tval *) duk_get_tval(thr, -1)));
  33317. /* [ hobject props enum(props) key desc ] */
  33318. duk_hobject_prepare_property_descriptor(thr,
  33319. 4 /*idx_desc*/,
  33320. &defprop_flags,
  33321. &idx_value,
  33322. &get,
  33323. &set);
  33324. /* [ hobject props enum(props) key desc [multiple values] ] */
  33325. if (pass == 0) {
  33326. continue;
  33327. }
  33328. /* This allows symbols on purpose. */
  33329. key = duk_known_hstring(thr, 3);
  33330. DUK_ASSERT(key != NULL);
  33331. duk_hobject_define_property_helper(thr,
  33332. defprop_flags,
  33333. obj,
  33334. key,
  33335. idx_value,
  33336. get,
  33337. set,
  33338. 1 /*throw_flag*/);
  33339. }
  33340. }
  33341. /*
  33342. * Return target object
  33343. */
  33344. duk_dup_0(thr);
  33345. return 1;
  33346. }
  33347. #endif /* DUK_USE_OBJECT_BUILTIN */
  33348. #if defined(DUK_USE_OBJECT_BUILTIN)
  33349. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_seal_freeze_shared(duk_hthread *thr) {
  33350. DUK_ASSERT_TOP(thr, 1);
  33351. duk_seal_freeze_raw(thr, 0, (duk_bool_t) duk_get_current_magic(thr) /*is_freeze*/);
  33352. return 1;
  33353. }
  33354. #endif /* DUK_USE_OBJECT_BUILTIN */
  33355. #if defined(DUK_USE_OBJECT_BUILTIN)
  33356. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_sealed_frozen_shared(duk_hthread *thr) {
  33357. duk_hobject *h;
  33358. duk_bool_t is_frozen;
  33359. duk_uint_t mask;
  33360. is_frozen = (duk_bool_t) duk_get_current_magic(thr);
  33361. mask = duk_get_type_mask(thr, 0);
  33362. if (mask & (DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER)) {
  33363. DUK_ASSERT(is_frozen == 0 || is_frozen == 1);
  33364. duk_push_boolean(thr, (mask & DUK_TYPE_MASK_LIGHTFUNC) ?
  33365. 1 : /* lightfunc always frozen and sealed */
  33366. (is_frozen ^ 1)); /* buffer sealed but not frozen (index props writable) */
  33367. } else {
  33368. /* ES2015 Sections 19.1.2.12, 19.1.2.13: anything other than an object
  33369. * is considered to be already sealed and frozen.
  33370. */
  33371. h = duk_get_hobject(thr, 0);
  33372. duk_push_boolean(thr, (h == NULL) ||
  33373. duk_hobject_object_is_sealed_frozen_helper(thr, h, is_frozen /*is_frozen*/));
  33374. }
  33375. return 1;
  33376. }
  33377. #endif /* DUK_USE_OBJECT_BUILTIN */
  33378. #if defined(DUK_USE_OBJECT_BUILTIN)
  33379. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_to_locale_string(duk_hthread *thr) {
  33380. DUK_ASSERT_TOP(thr, 0);
  33381. (void) duk_push_this_coercible_to_object(thr);
  33382. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_TO_STRING);
  33383. #if 0 /* This is mentioned explicitly in the E5.1 spec, but duk_call_method() checks for it in practice. */
  33384. duk_require_callable(thr, 1);
  33385. #endif
  33386. duk_dup_0(thr); /* -> [ O toString O ] */
  33387. duk_call_method(thr, 0); /* XXX: call method tail call? */
  33388. return 1;
  33389. }
  33390. #endif /* DUK_USE_OBJECT_BUILTIN */
  33391. #if defined(DUK_USE_OBJECT_BUILTIN)
  33392. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_value_of(duk_hthread *thr) {
  33393. /* For lightfuncs and plain buffers, returns Object() coerced. */
  33394. (void) duk_push_this_coercible_to_object(thr);
  33395. return 1;
  33396. }
  33397. #endif /* DUK_USE_OBJECT_BUILTIN */
  33398. #if defined(DUK_USE_OBJECT_BUILTIN)
  33399. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_is_prototype_of(duk_hthread *thr) {
  33400. duk_hobject *h_v;
  33401. duk_hobject *h_obj;
  33402. DUK_ASSERT_TOP(thr, 1);
  33403. h_v = duk_get_hobject(thr, 0);
  33404. if (!h_v) {
  33405. duk_push_false(thr); /* XXX: tail call: return duk_push_false(thr) */
  33406. return 1;
  33407. }
  33408. h_obj = duk_push_this_coercible_to_object(thr);
  33409. DUK_ASSERT(h_obj != NULL);
  33410. /* E5.1 Section 15.2.4.6, step 3.a, lookup proto once before compare.
  33411. * Prototype loops should cause an error to be thrown.
  33412. */
  33413. duk_push_boolean(thr, duk_hobject_prototype_chain_contains(thr, DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_v), h_obj, 0 /*ignore_loop*/));
  33414. return 1;
  33415. }
  33416. #endif /* DUK_USE_OBJECT_BUILTIN */
  33417. #if defined(DUK_USE_OBJECT_BUILTIN)
  33418. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_has_own_property(duk_hthread *thr) {
  33419. return (duk_ret_t) duk_hobject_object_ownprop_helper(thr, 0 /*required_desc_flags*/);
  33420. }
  33421. #endif /* DUK_USE_OBJECT_BUILTIN */
  33422. #if defined(DUK_USE_OBJECT_BUILTIN)
  33423. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_property_is_enumerable(duk_hthread *thr) {
  33424. return (duk_ret_t) duk_hobject_object_ownprop_helper(thr, DUK_PROPDESC_FLAG_ENUMERABLE /*required_desc_flags*/);
  33425. }
  33426. #endif /* DUK_USE_OBJECT_BUILTIN */
  33427. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33428. /* Shared helper to implement Object.getPrototypeOf,
  33429. * Object.prototype.__proto__ getter, and Reflect.getPrototypeOf.
  33430. *
  33431. * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-get-object.prototype.__proto__
  33432. */
  33433. DUK_INTERNAL duk_ret_t duk_bi_object_getprototype_shared(duk_hthread *thr) {
  33434. /*
  33435. * magic = 0: __proto__ getter
  33436. * magic = 1: Object.getPrototypeOf()
  33437. * magic = 2: Reflect.getPrototypeOf()
  33438. */
  33439. duk_hobject *h;
  33440. duk_hobject *proto;
  33441. duk_tval *tv;
  33442. duk_int_t magic;
  33443. magic = duk_get_current_magic(thr);
  33444. if (magic == 0) {
  33445. DUK_ASSERT_TOP(thr, 0);
  33446. duk_push_this_coercible_to_object(thr);
  33447. }
  33448. DUK_ASSERT(duk_get_top(thr) >= 1);
  33449. if (magic < 2) {
  33450. /* ES2015 Section 19.1.2.9, step 1 */
  33451. duk_to_object(thr, 0);
  33452. }
  33453. tv = DUK_GET_TVAL_POSIDX(thr, 0);
  33454. switch (DUK_TVAL_GET_TAG(tv)) {
  33455. case DUK_TAG_BUFFER:
  33456. proto = thr->builtins[DUK_BIDX_UINT8ARRAY_PROTOTYPE];
  33457. break;
  33458. case DUK_TAG_LIGHTFUNC:
  33459. proto = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  33460. break;
  33461. case DUK_TAG_OBJECT:
  33462. h = DUK_TVAL_GET_OBJECT(tv);
  33463. proto = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  33464. break;
  33465. default:
  33466. /* This implicitly handles CheckObjectCoercible() caused
  33467. * TypeError.
  33468. */
  33469. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  33470. }
  33471. if (proto != NULL) {
  33472. duk_push_hobject(thr, proto);
  33473. } else {
  33474. duk_push_null(thr);
  33475. }
  33476. return 1;
  33477. }
  33478. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33479. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33480. /* Shared helper to implement ES2015 Object.setPrototypeOf,
  33481. * Object.prototype.__proto__ setter, and Reflect.setPrototypeOf.
  33482. *
  33483. * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-get-object.prototype.__proto__
  33484. * http://www.ecma-international.org/ecma-262/6.0/index.html#sec-object.setprototypeof
  33485. */
  33486. DUK_INTERNAL duk_ret_t duk_bi_object_setprototype_shared(duk_hthread *thr) {
  33487. /*
  33488. * magic = 0: __proto__ setter
  33489. * magic = 1: Object.setPrototypeOf()
  33490. * magic = 2: Reflect.setPrototypeOf()
  33491. */
  33492. duk_hobject *h_obj;
  33493. duk_hobject *h_new_proto;
  33494. duk_hobject *h_curr;
  33495. duk_ret_t ret_success = 1; /* retval for success path */
  33496. duk_uint_t mask;
  33497. duk_int_t magic;
  33498. /* Preliminaries for __proto__ and setPrototypeOf (E6 19.1.2.18 steps 1-4). */
  33499. magic = duk_get_current_magic(thr);
  33500. if (magic == 0) {
  33501. duk_push_this_check_object_coercible(thr);
  33502. duk_insert(thr, 0);
  33503. if (!duk_check_type_mask(thr, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT)) {
  33504. return 0;
  33505. }
  33506. /* __proto__ setter returns 'undefined' on success unlike the
  33507. * setPrototypeOf() call which returns the target object.
  33508. */
  33509. ret_success = 0;
  33510. } else {
  33511. if (magic == 1) {
  33512. duk_require_object_coercible(thr, 0);
  33513. } else {
  33514. duk_require_hobject_accept_mask(thr, 0,
  33515. DUK_TYPE_MASK_LIGHTFUNC |
  33516. DUK_TYPE_MASK_BUFFER);
  33517. }
  33518. duk_require_type_mask(thr, 1, DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_OBJECT);
  33519. }
  33520. h_new_proto = duk_get_hobject(thr, 1);
  33521. /* h_new_proto may be NULL */
  33522. mask = duk_get_type_mask(thr, 0);
  33523. if (mask & (DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER)) {
  33524. duk_hobject *curr_proto;
  33525. curr_proto = thr->builtins[(mask & DUK_TYPE_MASK_LIGHTFUNC) ?
  33526. DUK_BIDX_FUNCTION_PROTOTYPE :
  33527. DUK_BIDX_UINT8ARRAY_PROTOTYPE];
  33528. if (h_new_proto == curr_proto) {
  33529. goto skip;
  33530. }
  33531. goto fail_nonextensible;
  33532. }
  33533. h_obj = duk_get_hobject(thr, 0);
  33534. if (h_obj == NULL) {
  33535. goto skip;
  33536. }
  33537. DUK_ASSERT(h_obj != NULL);
  33538. /* [[SetPrototypeOf]] standard behavior, E6 9.1.2. */
  33539. /* TODO: implement Proxy object support here */
  33540. if (h_new_proto == DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_obj)) {
  33541. goto skip;
  33542. }
  33543. if (!DUK_HOBJECT_HAS_EXTENSIBLE(h_obj)) {
  33544. goto fail_nonextensible;
  33545. }
  33546. for (h_curr = h_new_proto; h_curr != NULL; h_curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_curr)) {
  33547. /* Loop prevention. */
  33548. if (h_curr == h_obj) {
  33549. goto fail_loop;
  33550. }
  33551. }
  33552. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h_obj, h_new_proto);
  33553. /* fall thru */
  33554. skip:
  33555. duk_set_top(thr, 1);
  33556. if (magic == 2) {
  33557. duk_push_true(thr);
  33558. }
  33559. return ret_success;
  33560. fail_nonextensible:
  33561. fail_loop:
  33562. if (magic != 2) {
  33563. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  33564. } else {
  33565. duk_push_false(thr);
  33566. return 1;
  33567. }
  33568. }
  33569. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33570. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33571. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_define_property(duk_hthread *thr) {
  33572. /*
  33573. * magic = 0: Object.defineProperty()
  33574. * magic = 1: Reflect.defineProperty()
  33575. */
  33576. duk_hobject *obj;
  33577. duk_hstring *key;
  33578. duk_hobject *get;
  33579. duk_hobject *set;
  33580. duk_idx_t idx_value;
  33581. duk_uint_t defprop_flags;
  33582. duk_small_uint_t magic;
  33583. duk_bool_t throw_flag;
  33584. duk_bool_t ret;
  33585. DUK_ASSERT(thr != NULL);
  33586. DUK_DDD(DUK_DDDPRINT("Object.defineProperty(): ctx=%p obj=%!T key=%!T desc=%!T",
  33587. (void *) thr,
  33588. (duk_tval *) duk_get_tval(thr, 0),
  33589. (duk_tval *) duk_get_tval(thr, 1),
  33590. (duk_tval *) duk_get_tval(thr, 2)));
  33591. /* [ obj key desc ] */
  33592. magic = (duk_small_uint_t) duk_get_current_magic(thr);
  33593. /* Lightfuncs are currently supported by coercing to a temporary
  33594. * Function object; changes will be allowed (the coerced value is
  33595. * extensible) but will be lost. Same for plain buffers.
  33596. */
  33597. obj = duk_require_hobject_promote_mask(thr, 0, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  33598. DUK_ASSERT(obj != NULL);
  33599. key = duk_to_property_key_hstring(thr, 1);
  33600. (void) duk_require_hobject(thr, 2);
  33601. DUK_ASSERT(obj != NULL);
  33602. DUK_ASSERT(key != NULL);
  33603. DUK_ASSERT(duk_get_hobject(thr, 2) != NULL);
  33604. /*
  33605. * Validate and convert argument property descriptor (an ECMAScript
  33606. * object) into a set of defprop_flags and possibly property value,
  33607. * getter, and/or setter values on the value stack.
  33608. *
  33609. * Lightfunc set/get values are coerced to full Functions.
  33610. */
  33611. duk_hobject_prepare_property_descriptor(thr,
  33612. 2 /*idx_desc*/,
  33613. &defprop_flags,
  33614. &idx_value,
  33615. &get,
  33616. &set);
  33617. /*
  33618. * Use Object.defineProperty() helper for the actual operation.
  33619. */
  33620. DUK_ASSERT(magic == 0U || magic == 1U);
  33621. throw_flag = magic ^ 1U;
  33622. ret = duk_hobject_define_property_helper(thr,
  33623. defprop_flags,
  33624. obj,
  33625. key,
  33626. idx_value,
  33627. get,
  33628. set,
  33629. throw_flag);
  33630. /* Ignore the normalize/validate helper outputs on the value stack,
  33631. * they're popped automatically.
  33632. */
  33633. if (magic == 0U) {
  33634. /* Object.defineProperty(): return target object. */
  33635. duk_push_hobject(thr, obj);
  33636. } else {
  33637. /* Reflect.defineProperty(): return success/fail. */
  33638. duk_push_boolean(thr, ret);
  33639. }
  33640. return 1;
  33641. }
  33642. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33643. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33644. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_get_own_property_descriptor(duk_hthread *thr) {
  33645. DUK_ASSERT_TOP(thr, 2);
  33646. /* ES2015 Section 19.1.2.6, step 1 */
  33647. if (duk_get_current_magic(thr) == 0) {
  33648. duk_to_object(thr, 0);
  33649. }
  33650. /* [ obj key ] */
  33651. duk_hobject_object_get_own_property_descriptor(thr, -2);
  33652. return 1;
  33653. }
  33654. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33655. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33656. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_is_extensible(duk_hthread *thr) {
  33657. /*
  33658. * magic = 0: Object.isExtensible()
  33659. * magic = 1: Reflect.isExtensible()
  33660. */
  33661. duk_hobject *h;
  33662. if (duk_get_current_magic(thr) == 0) {
  33663. h = duk_get_hobject(thr, 0);
  33664. } else {
  33665. /* Reflect.isExtensible(): throw if non-object, but we accept lightfuncs
  33666. * and plain buffers here because they pretend to be objects.
  33667. */
  33668. h = duk_require_hobject_accept_mask(thr, 0, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  33669. }
  33670. duk_push_boolean(thr, (h != NULL) && DUK_HOBJECT_HAS_EXTENSIBLE(h));
  33671. return 1;
  33672. }
  33673. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33674. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33675. /* Shared helper for various key/symbol listings, magic:
  33676. * 0=Object.keys()
  33677. * 1=Object.getOwnPropertyNames(),
  33678. * 2=Object.getOwnPropertySymbols(),
  33679. * 3=Reflect.ownKeys()
  33680. */
  33681. DUK_LOCAL const duk_small_uint_t duk__object_keys_enum_flags[4] = {
  33682. /* Object.keys() */
  33683. DUK_ENUM_OWN_PROPERTIES_ONLY |
  33684. DUK_ENUM_NO_PROXY_BEHAVIOR,
  33685. /* Object.getOwnPropertyNames() */
  33686. DUK_ENUM_INCLUDE_NONENUMERABLE |
  33687. DUK_ENUM_OWN_PROPERTIES_ONLY |
  33688. DUK_ENUM_NO_PROXY_BEHAVIOR,
  33689. /* Object.getOwnPropertySymbols() */
  33690. DUK_ENUM_INCLUDE_SYMBOLS |
  33691. DUK_ENUM_OWN_PROPERTIES_ONLY |
  33692. DUK_ENUM_EXCLUDE_STRINGS |
  33693. DUK_ENUM_INCLUDE_NONENUMERABLE |
  33694. DUK_ENUM_NO_PROXY_BEHAVIOR,
  33695. /* Reflect.ownKeys() */
  33696. DUK_ENUM_INCLUDE_SYMBOLS |
  33697. DUK_ENUM_OWN_PROPERTIES_ONLY |
  33698. DUK_ENUM_INCLUDE_NONENUMERABLE |
  33699. DUK_ENUM_NO_PROXY_BEHAVIOR
  33700. };
  33701. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_keys_shared(duk_hthread *thr) {
  33702. duk_hobject *obj;
  33703. #if defined(DUK_USE_ES6_PROXY)
  33704. duk_hobject *h_proxy_target;
  33705. duk_hobject *h_proxy_handler;
  33706. duk_hobject *h_trap_result;
  33707. #endif
  33708. duk_small_uint_t enum_flags;
  33709. duk_int_t magic;
  33710. DUK_ASSERT_TOP(thr, 1);
  33711. magic = duk_get_current_magic(thr);
  33712. if (magic == 3) {
  33713. /* ES2015 Section 26.1.11 requires a TypeError for non-objects. Lightfuncs
  33714. * and plain buffers pretend to be objects, so accept those too.
  33715. */
  33716. obj = duk_require_hobject_promote_mask(thr, 0, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  33717. } else {
  33718. /* ES2015: ToObject coerce. */
  33719. obj = duk_to_hobject(thr, 0);
  33720. }
  33721. DUK_ASSERT(obj != NULL);
  33722. DUK_UNREF(obj);
  33723. /* XXX: proxy chains */
  33724. #if defined(DUK_USE_ES6_PROXY)
  33725. /* XXX: better sharing of code between proxy target call sites */
  33726. if (DUK_LIKELY(!duk_hobject_proxy_check(obj,
  33727. &h_proxy_target,
  33728. &h_proxy_handler))) {
  33729. goto skip_proxy;
  33730. }
  33731. duk_push_hobject(thr, h_proxy_handler);
  33732. if (!duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_OWN_KEYS)) {
  33733. /* Careful with reachability here: don't pop 'obj' before pushing
  33734. * proxy target.
  33735. */
  33736. DUK_DDD(DUK_DDDPRINT("no ownKeys trap, get keys of target instead"));
  33737. duk_pop_2(thr);
  33738. duk_push_hobject(thr, h_proxy_target);
  33739. duk_replace(thr, 0);
  33740. DUK_ASSERT_TOP(thr, 1);
  33741. goto skip_proxy;
  33742. }
  33743. /* [ obj handler trap ] */
  33744. duk_insert(thr, -2);
  33745. duk_push_hobject(thr, h_proxy_target); /* -> [ obj trap handler target ] */
  33746. duk_call_method(thr, 1 /*nargs*/); /* -> [ obj trap_result ] */
  33747. h_trap_result = duk_require_hobject(thr, -1);
  33748. DUK_UNREF(h_trap_result);
  33749. magic = duk_get_current_magic(thr);
  33750. DUK_ASSERT(magic >= 0 && magic < (duk_int_t) (sizeof(duk__object_keys_enum_flags) / sizeof(duk_small_uint_t)));
  33751. enum_flags = duk__object_keys_enum_flags[magic];
  33752. duk_proxy_ownkeys_postprocess(thr, h_proxy_target, enum_flags);
  33753. return 1;
  33754. skip_proxy:
  33755. #endif /* DUK_USE_ES6_PROXY */
  33756. DUK_ASSERT_TOP(thr, 1);
  33757. magic = duk_get_current_magic(thr);
  33758. DUK_ASSERT(magic >= 0 && magic < (duk_int_t) (sizeof(duk__object_keys_enum_flags) / sizeof(duk_small_uint_t)));
  33759. enum_flags = duk__object_keys_enum_flags[magic];
  33760. return duk_hobject_get_enumerated_keys(thr, enum_flags);
  33761. }
  33762. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33763. #if defined(DUK_USE_OBJECT_BUILTIN) || defined(DUK_USE_REFLECT_BUILTIN)
  33764. DUK_INTERNAL duk_ret_t duk_bi_object_constructor_prevent_extensions(duk_hthread *thr) {
  33765. /*
  33766. * magic = 0: Object.preventExtensions()
  33767. * magic = 1: Reflect.preventExtensions()
  33768. */
  33769. duk_hobject *h;
  33770. duk_uint_t mask;
  33771. duk_int_t magic;
  33772. magic = duk_get_current_magic(thr);
  33773. /* Silent success for lightfuncs and plain buffers always. */
  33774. mask = DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER;
  33775. /* Object.preventExtensions() silent success for non-object. */
  33776. if (magic == 0) {
  33777. mask |= DUK_TYPE_MASK_UNDEFINED |
  33778. DUK_TYPE_MASK_NULL |
  33779. DUK_TYPE_MASK_BOOLEAN |
  33780. DUK_TYPE_MASK_NUMBER |
  33781. DUK_TYPE_MASK_STRING |
  33782. DUK_TYPE_MASK_POINTER;
  33783. }
  33784. if (duk_check_type_mask(thr, 0, mask)) {
  33785. /* Not an object, already non-extensible so always success. */
  33786. goto done;
  33787. }
  33788. h = duk_require_hobject(thr, 0);
  33789. DUK_ASSERT(h != NULL);
  33790. DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
  33791. /* A non-extensible object cannot gain any more properties,
  33792. * so this is a good time to compact.
  33793. */
  33794. duk_hobject_compact_props(thr, h);
  33795. done:
  33796. if (magic == 1) {
  33797. duk_push_true(thr);
  33798. }
  33799. return 1;
  33800. }
  33801. #endif /* DUK_USE_OBJECT_BUILTIN || DUK_USE_REFLECT_BUILTIN */
  33802. /*
  33803. * __defineGetter__, __defineSetter__, __lookupGetter__, __lookupSetter__
  33804. */
  33805. #if defined(DUK_USE_ES8)
  33806. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_defineaccessor(duk_hthread *thr) {
  33807. duk_push_this(thr);
  33808. duk_insert(thr, 0);
  33809. duk_to_object(thr, 0);
  33810. duk_require_callable(thr, 2);
  33811. /* [ ToObject(this) key getter/setter ] */
  33812. /* ToPropertyKey() coercion is not needed, duk_def_prop() does it. */
  33813. duk_def_prop(thr, 0, DUK_DEFPROP_SET_ENUMERABLE |
  33814. DUK_DEFPROP_SET_CONFIGURABLE |
  33815. (duk_get_current_magic(thr) ? DUK_DEFPROP_HAVE_SETTER : DUK_DEFPROP_HAVE_GETTER));
  33816. return 0;
  33817. }
  33818. DUK_INTERNAL duk_ret_t duk_bi_object_prototype_lookupaccessor(duk_hthread *thr) {
  33819. duk_uint_t sanity;
  33820. duk_push_this(thr);
  33821. duk_to_object(thr, -1);
  33822. /* XXX: Prototype walk (with sanity) should be a core property
  33823. * operation, could add a flag to e.g. duk_get_prop_desc().
  33824. */
  33825. /* ToPropertyKey() coercion is not needed, duk_get_prop_desc() does it. */
  33826. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  33827. while (!duk_is_undefined(thr, -1)) {
  33828. /* [ key obj ] */
  33829. duk_dup(thr, 0);
  33830. duk_get_prop_desc(thr, 1, 0 /*flags*/);
  33831. if (!duk_is_undefined(thr, -1)) {
  33832. duk_get_prop_stridx(thr, -1, (duk_get_current_magic(thr) != 0 ? DUK_STRIDX_SET : DUK_STRIDX_GET));
  33833. return 1;
  33834. }
  33835. duk_pop(thr);
  33836. if (DUK_UNLIKELY(sanity-- == 0)) {
  33837. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  33838. DUK_WO_NORETURN(return 0;);
  33839. }
  33840. duk_get_prototype(thr, -1);
  33841. duk_remove(thr, -2);
  33842. }
  33843. return 1;
  33844. }
  33845. #endif /* DUK_USE_ES8 */
  33846. #line 1 "duk_bi_performance.c"
  33847. /*
  33848. * High resolution time API (performance.now() et al)
  33849. *
  33850. * API specification: https://encoding.spec.whatwg.org/#ap://www.w3.org/TR/hr-time/
  33851. */
  33852. /* #include duk_internal.h -> already included */
  33853. #if defined(DUK_USE_PERFORMANCE_BUILTIN)
  33854. DUK_INTERNAL duk_ret_t duk_bi_performance_now(duk_hthread *thr) {
  33855. /* From API spec:
  33856. * The DOMHighResTimeStamp type is used to store a time value in
  33857. * milliseconds, measured relative from the time origin, global
  33858. * monotonic clock, or a time value that represents a duration
  33859. * between two DOMHighResTimeStamp's.
  33860. */
  33861. duk_push_number(thr, duk_time_get_monotonic_time(thr));
  33862. return 1;
  33863. }
  33864. #if 0 /* Missing until semantics decided. */
  33865. DUK_INTERNAL duk_ret_t duk_bi_performance_timeorigin_getter(duk_hthread *thr) {
  33866. /* No decision yet how to handle timeOrigins, e.g. should one be
  33867. * initialized per heap, or per global object set. See
  33868. * https://www.w3.org/TR/hr-time/#time-origin.
  33869. */
  33870. duk_push_uint(thr, 0);
  33871. return 1;
  33872. }
  33873. #endif /* 0 */
  33874. #endif /* DUK_USE_PERFORMANCE_BUILTIN */
  33875. #line 1 "duk_bi_pointer.c"
  33876. /*
  33877. * Pointer built-ins
  33878. */
  33879. /* #include duk_internal.h -> already included */
  33880. /*
  33881. * Constructor
  33882. */
  33883. DUK_INTERNAL duk_ret_t duk_bi_pointer_constructor(duk_hthread *thr) {
  33884. /* XXX: this behavior is quite useless now; it would be nice to be able
  33885. * to create pointer values from e.g. numbers or strings. Numbers are
  33886. * problematic on 64-bit platforms though. Hex encoded strings?
  33887. */
  33888. if (duk_get_top(thr) == 0) {
  33889. duk_push_pointer(thr, NULL);
  33890. } else {
  33891. duk_to_pointer(thr, 0);
  33892. }
  33893. DUK_ASSERT(duk_is_pointer(thr, 0));
  33894. duk_set_top(thr, 1);
  33895. if (duk_is_constructor_call(thr)) {
  33896. (void) duk_push_object_helper(thr,
  33897. DUK_HOBJECT_FLAG_EXTENSIBLE |
  33898. DUK_HOBJECT_FLAG_FASTREFS |
  33899. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_POINTER),
  33900. DUK_BIDX_POINTER_PROTOTYPE);
  33901. /* Pointer object internal value is immutable */
  33902. duk_dup_0(thr);
  33903. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  33904. }
  33905. /* Note: unbalanced stack on purpose */
  33906. return 1;
  33907. }
  33908. /*
  33909. * toString(), valueOf()
  33910. */
  33911. DUK_INTERNAL duk_ret_t duk_bi_pointer_prototype_tostring_shared(duk_hthread *thr) {
  33912. duk_tval *tv;
  33913. duk_small_int_t to_string = duk_get_current_magic(thr);
  33914. duk_push_this(thr);
  33915. tv = duk_require_tval(thr, -1);
  33916. DUK_ASSERT(tv != NULL);
  33917. if (DUK_TVAL_IS_POINTER(tv)) {
  33918. /* nop */
  33919. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  33920. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  33921. DUK_ASSERT(h != NULL);
  33922. /* Must be a "pointer object", i.e. class "Pointer" */
  33923. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_POINTER) {
  33924. goto type_error;
  33925. }
  33926. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
  33927. } else {
  33928. goto type_error;
  33929. }
  33930. if (to_string) {
  33931. duk_to_string(thr, -1);
  33932. }
  33933. return 1;
  33934. type_error:
  33935. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  33936. }
  33937. #line 1 "duk_bi_promise.c"
  33938. /*
  33939. * Promise built-in
  33940. */
  33941. /* #include duk_internal.h -> already included */
  33942. #if defined(DUK_USE_PROMISE_BUILTIN)
  33943. DUK_INTERNAL duk_ret_t duk_bi_promise_constructor(duk_hthread *thr) {
  33944. DUK_ERROR_TYPE(thr, "unimplemented");
  33945. DUK_WO_NORETURN(return 0;);
  33946. }
  33947. DUK_INTERNAL duk_ret_t duk_bi_promise_all(duk_hthread *thr) {
  33948. DUK_ERROR_TYPE(thr, "unimplemented");
  33949. DUK_WO_NORETURN(return 0;);
  33950. }
  33951. DUK_INTERNAL duk_ret_t duk_bi_promise_race(duk_hthread *thr) {
  33952. DUK_ERROR_TYPE(thr, "unimplemented");
  33953. DUK_WO_NORETURN(return 0;);
  33954. }
  33955. DUK_INTERNAL duk_ret_t duk_bi_promise_reject(duk_hthread *thr) {
  33956. DUK_ERROR_TYPE(thr, "unimplemented");
  33957. DUK_WO_NORETURN(return 0;);
  33958. }
  33959. DUK_INTERNAL duk_ret_t duk_bi_promise_resolve(duk_hthread *thr) {
  33960. DUK_ERROR_TYPE(thr, "unimplemented");
  33961. DUK_WO_NORETURN(return 0;);
  33962. }
  33963. DUK_INTERNAL duk_ret_t duk_bi_promise_catch(duk_hthread *thr) {
  33964. DUK_ERROR_TYPE(thr, "unimplemented");
  33965. DUK_WO_NORETURN(return 0;);
  33966. }
  33967. DUK_INTERNAL duk_ret_t duk_bi_promise_then(duk_hthread *thr) {
  33968. DUK_ERROR_TYPE(thr, "unimplemented");
  33969. DUK_WO_NORETURN(return 0;);
  33970. }
  33971. #endif /* DUK_USE_PROMISE_BUILTIN */
  33972. #line 1 "duk_bi_proxy.c"
  33973. /*
  33974. * Proxy built-in (ES2015)
  33975. */
  33976. /* #include duk_internal.h -> already included */
  33977. #if defined(DUK_USE_ES6_PROXY)
  33978. /* Post-process a Proxy ownKeys() result at stack top. Push a cleaned up
  33979. * array of valid result keys (strings or symbols). TypeError for invalid
  33980. * values. Flags are shared with duk_enum().
  33981. */
  33982. DUK_INTERNAL void duk_proxy_ownkeys_postprocess(duk_hthread *thr, duk_hobject *h_proxy_target, duk_uint_t flags) {
  33983. duk_uarridx_t i, len, idx;
  33984. duk_propdesc desc;
  33985. DUK_ASSERT_CTX_VALID(thr);
  33986. DUK_ASSERT(h_proxy_target != NULL);
  33987. len = (duk_uarridx_t) duk_get_length(thr, -1);
  33988. idx = 0;
  33989. duk_push_array(thr);
  33990. /* XXX: preallocated dense array, fill in directly */
  33991. for (i = 0; i < len; i++) {
  33992. duk_hstring *h;
  33993. /* [ obj trap_result res_arr ] */
  33994. (void) duk_get_prop_index(thr, -2, i);
  33995. h = duk_get_hstring(thr, -1);
  33996. if (h == NULL) {
  33997. DUK_ERROR_TYPE_INVALID_TRAP_RESULT(thr);
  33998. DUK_WO_NORETURN(return;);
  33999. }
  34000. if (!(flags & DUK_ENUM_INCLUDE_NONENUMERABLE)) {
  34001. /* No support for 'getOwnPropertyDescriptor' trap yet,
  34002. * so check enumerability always from target object
  34003. * descriptor.
  34004. */
  34005. if (duk_hobject_get_own_propdesc(thr, h_proxy_target, duk_known_hstring(thr, -1), &desc, 0 /*flags*/)) {
  34006. if ((desc.flags & DUK_PROPDESC_FLAG_ENUMERABLE) == 0) {
  34007. DUK_DDD(DUK_DDDPRINT("ignore non-enumerable property: %!T", duk_get_tval(thr, -1)));
  34008. goto skip_key;
  34009. }
  34010. } else {
  34011. DUK_DDD(DUK_DDDPRINT("ignore non-existent property: %!T", duk_get_tval(thr, -1)));
  34012. goto skip_key;
  34013. }
  34014. }
  34015. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  34016. if (!(flags & DUK_ENUM_INCLUDE_SYMBOLS)) {
  34017. DUK_DDD(DUK_DDDPRINT("ignore symbol property: %!T", duk_get_tval(thr, -1)));
  34018. goto skip_key;
  34019. }
  34020. if (DUK_HSTRING_HAS_HIDDEN(h) && !(flags & DUK_ENUM_INCLUDE_HIDDEN)) {
  34021. DUK_DDD(DUK_DDDPRINT("ignore hidden symbol property: %!T", duk_get_tval(thr, -1)));
  34022. goto skip_key;
  34023. }
  34024. } else {
  34025. if (flags & DUK_ENUM_EXCLUDE_STRINGS) {
  34026. DUK_DDD(DUK_DDDPRINT("ignore string property: %!T", duk_get_tval(thr, -1)));
  34027. goto skip_key;
  34028. }
  34029. }
  34030. /* [ obj trap_result res_arr propname ] */
  34031. duk_put_prop_index(thr, -2, idx++);
  34032. continue;
  34033. skip_key:
  34034. duk_pop(thr);
  34035. continue;
  34036. }
  34037. /* XXX: Missing trap result validation for non-configurable target keys
  34038. * (must be present), for non-extensible target all target keys must be
  34039. * present and no extra keys can be present.
  34040. * http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
  34041. */
  34042. /* XXX: The key enumerability check should trigger the "getOwnPropertyDescriptor"
  34043. * trap which has not yet been implemented. In the absence of such a trap,
  34044. * the enumerability should be checked from the target object; this is
  34045. * handled above.
  34046. */
  34047. }
  34048. #endif /* DUK_USE_ES6_PROXY */
  34049. #if defined(DUK_USE_ES6_PROXY)
  34050. DUK_INTERNAL duk_ret_t duk_bi_proxy_constructor(duk_hthread *thr) {
  34051. DUK_ASSERT_TOP(thr, 2); /* [ target handler ] */
  34052. duk_require_constructor_call(thr);
  34053. duk_push_proxy(thr, 0 /*flags*/); /* [ target handler ] -> [ proxy ] */
  34054. return 1; /* replacement */
  34055. }
  34056. #endif /* DUK_USE_ES6_PROXY */
  34057. #line 1 "duk_bi_reflect.c"
  34058. /*
  34059. * 'Reflect' built-in (ES2016 Section 26.1)
  34060. * http://www.ecma-international.org/ecma-262/7.0/#sec-reflect-object
  34061. *
  34062. * Many Reflect built-in functions are provided by shared helpers in
  34063. * duk_bi_object.c or duk_bi_function.c.
  34064. */
  34065. /* #include duk_internal.h -> already included */
  34066. #if defined(DUK_USE_REFLECT_BUILTIN)
  34067. DUK_INTERNAL duk_ret_t duk_bi_reflect_object_delete_property(duk_hthread *thr) {
  34068. duk_tval *tv_obj;
  34069. duk_tval *tv_key;
  34070. duk_bool_t ret;
  34071. DUK_ASSERT_TOP(thr, 2);
  34072. (void) duk_require_hobject(thr, 0);
  34073. (void) duk_to_string(thr, 1);
  34074. /* [ target key ] */
  34075. DUK_ASSERT(thr != NULL);
  34076. tv_obj = DUK_GET_TVAL_POSIDX(thr, 0);
  34077. tv_key = DUK_GET_TVAL_POSIDX(thr, 1);
  34078. ret = duk_hobject_delprop(thr, tv_obj, tv_key, 0 /*throw_flag*/);
  34079. duk_push_boolean(thr, ret);
  34080. return 1;
  34081. }
  34082. DUK_INTERNAL duk_ret_t duk_bi_reflect_object_get(duk_hthread *thr) {
  34083. duk_tval *tv_obj;
  34084. duk_tval *tv_key;
  34085. duk_idx_t nargs;
  34086. DUK_ASSERT(thr != NULL);
  34087. nargs = duk_get_top_require_min(thr, 2 /*min_top*/);
  34088. (void) duk_require_hobject(thr, 0);
  34089. (void) duk_to_string(thr, 1);
  34090. if (nargs >= 3 && !duk_strict_equals(thr, 0, 2)) {
  34091. /* XXX: [[Get]] receiver currently unsupported */
  34092. DUK_ERROR_UNSUPPORTED(thr);
  34093. DUK_WO_NORETURN(return 0;);
  34094. }
  34095. /* [ target key receiver? ...? ] */
  34096. tv_obj = DUK_GET_TVAL_POSIDX(thr, 0);
  34097. tv_key = DUK_GET_TVAL_POSIDX(thr, 1);
  34098. (void) duk_hobject_getprop(thr, tv_obj, tv_key); /* This could also be a duk_get_prop(). */
  34099. return 1;
  34100. }
  34101. DUK_INTERNAL duk_ret_t duk_bi_reflect_object_has(duk_hthread *thr) {
  34102. duk_tval *tv_obj;
  34103. duk_tval *tv_key;
  34104. duk_bool_t ret;
  34105. DUK_ASSERT(thr != NULL);
  34106. DUK_ASSERT_TOP(thr, 2);
  34107. (void) duk_require_hobject(thr, 0);
  34108. (void) duk_to_string(thr, 1);
  34109. /* [ target key ] */
  34110. tv_obj = DUK_GET_TVAL_POSIDX(thr, 0);
  34111. tv_key = DUK_GET_TVAL_POSIDX(thr, 1);
  34112. ret = duk_hobject_hasprop(thr, tv_obj, tv_key);
  34113. duk_push_boolean(thr, ret);
  34114. return 1;
  34115. }
  34116. DUK_INTERNAL duk_ret_t duk_bi_reflect_object_set(duk_hthread *thr) {
  34117. duk_tval *tv_obj;
  34118. duk_tval *tv_key;
  34119. duk_tval *tv_val;
  34120. duk_idx_t nargs;
  34121. duk_bool_t ret;
  34122. DUK_ASSERT(thr != NULL);
  34123. nargs = duk_get_top_require_min(thr, 3 /*min_top*/);
  34124. (void) duk_require_hobject(thr, 0);
  34125. (void) duk_to_string(thr, 1);
  34126. if (nargs >= 4 && !duk_strict_equals(thr, 0, 3)) {
  34127. /* XXX: [[Set]] receiver currently unsupported */
  34128. DUK_ERROR_UNSUPPORTED(thr);
  34129. DUK_WO_NORETURN(return 0;);
  34130. }
  34131. /* [ target key value receiver? ...? ] */
  34132. tv_obj = DUK_GET_TVAL_POSIDX(thr, 0);
  34133. tv_key = DUK_GET_TVAL_POSIDX(thr, 1);
  34134. tv_val = DUK_GET_TVAL_POSIDX(thr, 2);
  34135. ret = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, 0 /*throw_flag*/);
  34136. duk_push_boolean(thr, ret);
  34137. return 1;
  34138. }
  34139. #endif /* DUK_USE_REFLECT_BUILTIN */
  34140. #line 1 "duk_bi_regexp.c"
  34141. /*
  34142. * RegExp built-ins
  34143. */
  34144. /* #include duk_internal.h -> already included */
  34145. #if defined(DUK_USE_REGEXP_SUPPORT)
  34146. DUK_LOCAL void duk__get_this_regexp(duk_hthread *thr) {
  34147. duk_hobject *h;
  34148. duk_push_this(thr);
  34149. h = duk_require_hobject_with_class(thr, -1, DUK_HOBJECT_CLASS_REGEXP);
  34150. DUK_ASSERT(h != NULL);
  34151. DUK_UNREF(h);
  34152. duk_insert(thr, 0); /* prepend regexp to valstack 0 index */
  34153. }
  34154. /* XXX: much to improve (code size) */
  34155. DUK_INTERNAL duk_ret_t duk_bi_regexp_constructor(duk_hthread *thr) {
  34156. duk_hobject *h_pattern;
  34157. DUK_ASSERT_TOP(thr, 2);
  34158. h_pattern = duk_get_hobject(thr, 0);
  34159. if (!duk_is_constructor_call(thr) &&
  34160. h_pattern != NULL &&
  34161. DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP &&
  34162. duk_is_undefined(thr, 1)) {
  34163. /* Called as a function, pattern has [[Class]] "RegExp" and
  34164. * flags is undefined -> return object as is.
  34165. */
  34166. /* XXX: ES2015 has a NewTarget SameValue() check which is not
  34167. * yet implemented.
  34168. */
  34169. duk_dup_0(thr);
  34170. return 1;
  34171. }
  34172. /* Else functionality is identical for function call and constructor
  34173. * call.
  34174. */
  34175. if (h_pattern != NULL &&
  34176. DUK_HOBJECT_GET_CLASS_NUMBER(h_pattern) == DUK_HOBJECT_CLASS_REGEXP) {
  34177. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_SOURCE);
  34178. if (duk_is_undefined(thr, 1)) {
  34179. /* In ES5 one would need to read the flags individually;
  34180. * in ES2015 just read .flags.
  34181. */
  34182. duk_get_prop_stridx(thr, 0, DUK_STRIDX_FLAGS);
  34183. } else {
  34184. /* In ES2015 allowed; overrides argument RegExp flags. */
  34185. duk_dup_1(thr);
  34186. }
  34187. } else {
  34188. if (duk_is_undefined(thr, 0)) {
  34189. duk_push_hstring_empty(thr);
  34190. } else {
  34191. duk_dup_0(thr);
  34192. duk_to_string(thr, -1); /* Rejects Symbols. */
  34193. }
  34194. if (duk_is_undefined(thr, 1)) {
  34195. duk_push_hstring_empty(thr);
  34196. } else {
  34197. duk_dup_1(thr);
  34198. duk_to_string(thr, -1); /* Rejects Symbols. */
  34199. }
  34200. /* [ ... pattern flags ] */
  34201. }
  34202. DUK_DDD(DUK_DDDPRINT("RegExp constructor/function call, pattern=%!T, flags=%!T",
  34203. (duk_tval *) duk_get_tval(thr, -2), (duk_tval *) duk_get_tval(thr, -1)));
  34204. /* [ ... pattern flags ] (both uncoerced) */
  34205. duk_to_string(thr, -2);
  34206. duk_to_string(thr, -1);
  34207. duk_regexp_compile(thr);
  34208. /* [ ... bytecode escaped_source ] */
  34209. duk_regexp_create_instance(thr);
  34210. /* [ ... RegExp ] */
  34211. return 1;
  34212. }
  34213. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_exec(duk_hthread *thr) {
  34214. duk__get_this_regexp(thr);
  34215. /* [ regexp input ] */
  34216. duk_regexp_match(thr);
  34217. /* [ result ] */
  34218. return 1;
  34219. }
  34220. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_test(duk_hthread *thr) {
  34221. duk__get_this_regexp(thr);
  34222. /* [ regexp input ] */
  34223. /* result object is created and discarded; wasteful but saves code space */
  34224. duk_regexp_match(thr);
  34225. /* [ result ] */
  34226. duk_push_boolean(thr, (duk_is_null(thr, -1) ? 0 : 1));
  34227. return 1;
  34228. }
  34229. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_tostring(duk_hthread *thr) {
  34230. /* This must be generic in ES2015 and later. */
  34231. DUK_ASSERT_TOP(thr, 0);
  34232. duk_push_this(thr);
  34233. duk_push_literal(thr, "/");
  34234. duk_get_prop_stridx(thr, 0, DUK_STRIDX_SOURCE);
  34235. duk_dup_m2(thr); /* another "/" */
  34236. duk_get_prop_stridx(thr, 0, DUK_STRIDX_FLAGS);
  34237. duk_concat(thr, 4);
  34238. return 1;
  34239. }
  34240. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_flags(duk_hthread *thr) {
  34241. /* .flags is ES2015 but present even when ES2015 bindings are
  34242. * disabled because the constructor relies on it.
  34243. */
  34244. duk_uint8_t buf[8]; /* enough for all flags + NUL */
  34245. duk_uint8_t *p = buf;
  34246. /* .flags is generic and works on any object. */
  34247. duk_push_this(thr);
  34248. (void) duk_require_hobject(thr, -1);
  34249. if (duk_get_prop_stridx_boolean(thr, 0, DUK_STRIDX_GLOBAL, NULL)) {
  34250. *p++ = DUK_ASC_LC_G;
  34251. }
  34252. if (duk_get_prop_stridx_boolean(thr, 0, DUK_STRIDX_IGNORE_CASE, NULL)) {
  34253. *p++ = DUK_ASC_LC_I;
  34254. }
  34255. if (duk_get_prop_stridx_boolean(thr, 0, DUK_STRIDX_MULTILINE, NULL)) {
  34256. *p++ = DUK_ASC_LC_M;
  34257. }
  34258. /* .unicode: to be added */
  34259. /* .sticky: to be added */
  34260. *p++ = DUK_ASC_NUL;
  34261. DUK_ASSERT((duk_size_t) (p - buf) <= sizeof(buf));
  34262. duk_push_string(thr, (const char *) buf);
  34263. return 1;
  34264. }
  34265. /* Shared helper for providing .source, .global, .multiline, etc getters. */
  34266. DUK_INTERNAL duk_ret_t duk_bi_regexp_prototype_shared_getter(duk_hthread *thr) {
  34267. duk_hstring *h_bc;
  34268. duk_small_uint_t re_flags;
  34269. duk_hobject *h;
  34270. duk_int_t magic;
  34271. DUK_ASSERT_TOP(thr, 0);
  34272. duk_push_this(thr);
  34273. h = duk_require_hobject(thr, -1);
  34274. magic = duk_get_current_magic(thr);
  34275. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) == DUK_HOBJECT_CLASS_REGEXP) {
  34276. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_INT_SOURCE);
  34277. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_INT_BYTECODE);
  34278. h_bc = duk_require_hstring(thr, -1);
  34279. re_flags = (duk_small_uint_t) DUK_HSTRING_GET_DATA(h_bc)[0]; /* Safe even if h_bc length is 0 (= NUL) */
  34280. duk_pop(thr);
  34281. } else if (h == thr->builtins[DUK_BIDX_REGEXP_PROTOTYPE]) {
  34282. /* In ES2015 and ES2016 a TypeError would be thrown here.
  34283. * However, this had real world issues so ES2017 draft
  34284. * allows RegExp.prototype specifically, returning '(?:)'
  34285. * for .source and undefined for all flags.
  34286. */
  34287. if (magic != 16 /* .source */) {
  34288. return 0;
  34289. }
  34290. duk_push_literal(thr, "(?:)"); /* .source handled by switch-case */
  34291. re_flags = 0;
  34292. } else {
  34293. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  34294. }
  34295. /* [ regexp source ] */
  34296. switch (magic) {
  34297. case 0: { /* global */
  34298. duk_push_boolean(thr, (re_flags & DUK_RE_FLAG_GLOBAL));
  34299. break;
  34300. }
  34301. case 1: { /* ignoreCase */
  34302. duk_push_boolean(thr, (re_flags & DUK_RE_FLAG_IGNORE_CASE));
  34303. break;
  34304. }
  34305. case 2: { /* multiline */
  34306. duk_push_boolean(thr, (re_flags & DUK_RE_FLAG_MULTILINE));
  34307. break;
  34308. }
  34309. #if 0
  34310. /* Don't provide until implemented to avoid interfering with feature
  34311. * detection in user code.
  34312. */
  34313. case 3: /* sticky */
  34314. case 4: { /* unicode */
  34315. duk_push_false(thr);
  34316. break;
  34317. }
  34318. #endif
  34319. default: { /* source */
  34320. /* leave 'source' on top */
  34321. break;
  34322. }
  34323. }
  34324. return 1;
  34325. }
  34326. #endif /* DUK_USE_REGEXP_SUPPORT */
  34327. #line 1 "duk_bi_string.c"
  34328. /*
  34329. * String built-ins
  34330. *
  34331. * Most String built-ins must only accept strings (or String objects).
  34332. * Symbols, represented internally as strings, must be generally rejected.
  34333. * The duk_push_this_coercible_to_string() helper does this automatically.
  34334. */
  34335. /* XXX: There are several limitations in the current implementation for
  34336. * strings with >= 0x80000000UL characters. In some cases one would need
  34337. * to be able to represent the range [-0xffffffff,0xffffffff] and so on.
  34338. * Generally character and byte length are assumed to fit into signed 32
  34339. * bits (< 0x80000000UL). Places with issues are not marked explicitly
  34340. * below in all cases, look for signed type usage (duk_int_t etc) for
  34341. * offsets/lengths.
  34342. */
  34343. /* #include duk_internal.h -> already included */
  34344. #if defined(DUK_USE_STRING_BUILTIN)
  34345. /*
  34346. * Helpers
  34347. */
  34348. DUK_LOCAL duk_hstring *duk__str_tostring_notregexp(duk_hthread *thr, duk_idx_t idx) {
  34349. duk_hstring *h;
  34350. if (duk_get_class_number(thr, idx) == DUK_HOBJECT_CLASS_REGEXP) {
  34351. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  34352. DUK_WO_NORETURN(return NULL;);
  34353. }
  34354. h = duk_to_hstring(thr, idx);
  34355. DUK_ASSERT(h != NULL);
  34356. return h;
  34357. }
  34358. DUK_LOCAL duk_int_t duk__str_search_shared(duk_hthread *thr, duk_hstring *h_this, duk_hstring *h_search, duk_int_t start_cpos, duk_bool_t backwards) {
  34359. duk_int_t cpos;
  34360. duk_int_t bpos;
  34361. const duk_uint8_t *p_start, *p_end, *p;
  34362. const duk_uint8_t *q_start;
  34363. duk_int_t q_blen;
  34364. duk_uint8_t firstbyte;
  34365. duk_uint8_t t;
  34366. cpos = start_cpos;
  34367. /* Empty searchstring always matches; cpos must be clamped here.
  34368. * (If q_blen were < 0 due to clamped coercion, it would also be
  34369. * caught here.)
  34370. */
  34371. q_start = DUK_HSTRING_GET_DATA(h_search);
  34372. q_blen = (duk_int_t) DUK_HSTRING_GET_BYTELEN(h_search);
  34373. if (q_blen <= 0) {
  34374. return cpos;
  34375. }
  34376. DUK_ASSERT(q_blen > 0);
  34377. bpos = (duk_int_t) duk_heap_strcache_offset_char2byte(thr, h_this, (duk_uint32_t) cpos);
  34378. p_start = DUK_HSTRING_GET_DATA(h_this);
  34379. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_this);
  34380. p = p_start + bpos;
  34381. /* This loop is optimized for size. For speed, there should be
  34382. * two separate loops, and we should ensure that memcmp() can be
  34383. * used without an extra "will searchstring fit" check. Doing
  34384. * the preconditioning for 'p' and 'p_end' is easy but cpos
  34385. * must be updated if 'p' is wound back (backward scanning).
  34386. */
  34387. firstbyte = q_start[0]; /* leading byte of match string */
  34388. while (p <= p_end && p >= p_start) {
  34389. t = *p;
  34390. /* For ECMAScript strings, this check can only match for
  34391. * initial UTF-8 bytes (not continuation bytes). For other
  34392. * strings all bets are off.
  34393. */
  34394. if ((t == firstbyte) && ((duk_size_t) (p_end - p) >= (duk_size_t) q_blen)) {
  34395. DUK_ASSERT(q_blen > 0);
  34396. if (duk_memcmp((const void *) p, (const void *) q_start, (size_t) q_blen) == 0) {
  34397. return cpos;
  34398. }
  34399. }
  34400. /* track cpos while scanning */
  34401. if (backwards) {
  34402. /* when going backwards, we decrement cpos 'early';
  34403. * 'p' may point to a continuation byte of the char
  34404. * at offset 'cpos', but that's OK because we'll
  34405. * backtrack all the way to the initial byte.
  34406. */
  34407. if ((t & 0xc0) != 0x80) {
  34408. cpos--;
  34409. }
  34410. p--;
  34411. } else {
  34412. if ((t & 0xc0) != 0x80) {
  34413. cpos++;
  34414. }
  34415. p++;
  34416. }
  34417. }
  34418. /* Not found. Empty string case is handled specially above. */
  34419. return -1;
  34420. }
  34421. /*
  34422. * Constructor
  34423. */
  34424. DUK_INTERNAL duk_ret_t duk_bi_string_constructor(duk_hthread *thr) {
  34425. duk_hstring *h;
  34426. duk_uint_t flags;
  34427. /* String constructor needs to distinguish between an argument not given at all
  34428. * vs. given as 'undefined'. We're a vararg function to handle this properly.
  34429. */
  34430. /* XXX: copy current activation flags to thr, including current magic,
  34431. * is_constructor_call etc. This takes a few bytes in duk_hthread but
  34432. * makes call sites smaller (there are >30 is_constructor_call and get
  34433. * current magic call sites.
  34434. */
  34435. if (duk_get_top(thr) == 0) {
  34436. duk_push_hstring_empty(thr);
  34437. } else {
  34438. h = duk_to_hstring_acceptsymbol(thr, 0);
  34439. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h) && !duk_is_constructor_call(thr))) {
  34440. duk_push_symbol_descriptive_string(thr, h);
  34441. duk_replace(thr, 0);
  34442. }
  34443. }
  34444. duk_to_string(thr, 0); /* catches symbol argument for constructor call */
  34445. DUK_ASSERT(duk_is_string(thr, 0));
  34446. duk_set_top(thr, 1); /* Top may be 1 or larger. */
  34447. if (duk_is_constructor_call(thr)) {
  34448. /* String object internal value is immutable */
  34449. flags = DUK_HOBJECT_FLAG_EXTENSIBLE |
  34450. DUK_HOBJECT_FLAG_FASTREFS |
  34451. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ |
  34452. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_STRING);
  34453. duk_push_object_helper(thr, flags, DUK_BIDX_STRING_PROTOTYPE);
  34454. duk_dup_0(thr);
  34455. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VALUE, DUK_PROPDESC_FLAGS_NONE);
  34456. }
  34457. /* Note: unbalanced stack on purpose */
  34458. return 1;
  34459. }
  34460. DUK_LOCAL duk_ret_t duk__construct_from_codepoints(duk_hthread *thr, duk_bool_t nonbmp) {
  34461. duk_bufwriter_ctx bw_alloc;
  34462. duk_bufwriter_ctx *bw;
  34463. duk_idx_t i, n;
  34464. duk_ucodepoint_t cp;
  34465. /* XXX: It would be nice to build the string directly but ToUint16()
  34466. * coercion is needed so a generic helper would not be very
  34467. * helpful (perhaps coerce the value stack first here and then
  34468. * build a string from a duk_tval number sequence in one go?).
  34469. */
  34470. n = duk_get_top(thr);
  34471. bw = &bw_alloc;
  34472. DUK_BW_INIT_PUSHBUF(thr, bw, (duk_size_t) n); /* initial estimate for ASCII only codepoints */
  34473. for (i = 0; i < n; i++) {
  34474. /* XXX: could improve bufwriter handling to write multiple codepoints
  34475. * with one ensure call but the relative benefit would be quite small.
  34476. */
  34477. if (nonbmp) {
  34478. /* ES2015 requires that (1) SameValue(cp, ToInteger(cp)) and
  34479. * (2) cp >= 0 and cp <= 0x10ffff. This check does not
  34480. * implement the steps exactly but the outcome should be
  34481. * the same.
  34482. */
  34483. duk_int32_t i32 = 0;
  34484. if (!duk_is_whole_get_int32(duk_to_number(thr, i), &i32) ||
  34485. i32 < 0 || i32 > 0x10ffffL) {
  34486. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  34487. }
  34488. DUK_ASSERT(i32 >= 0 && i32 <= 0x10ffffL);
  34489. cp = (duk_ucodepoint_t) i32;
  34490. DUK_BW_WRITE_ENSURE_CESU8(thr, bw, cp);
  34491. } else {
  34492. #if defined(DUK_USE_NONSTD_STRING_FROMCHARCODE_32BIT)
  34493. /* ToUint16() coercion is mandatory in the E5.1 specification, but
  34494. * this non-compliant behavior makes more sense because we support
  34495. * non-BMP codepoints. Don't use CESU-8 because that'd create
  34496. * surrogate pairs.
  34497. */
  34498. cp = (duk_ucodepoint_t) duk_to_uint32(thr, i);
  34499. DUK_BW_WRITE_ENSURE_XUTF8(thr, bw, cp);
  34500. #else
  34501. cp = (duk_ucodepoint_t) duk_to_uint16(thr, i);
  34502. DUK_ASSERT(cp >= 0 && cp <= 0x10ffffL);
  34503. DUK_BW_WRITE_ENSURE_CESU8(thr, bw, cp);
  34504. #endif
  34505. }
  34506. }
  34507. DUK_BW_COMPACT(thr, bw);
  34508. (void) duk_buffer_to_string(thr, -1); /* Safe, extended UTF-8 or CESU-8 encoded. */
  34509. return 1;
  34510. }
  34511. DUK_INTERNAL duk_ret_t duk_bi_string_constructor_from_char_code(duk_hthread *thr) {
  34512. return duk__construct_from_codepoints(thr, 0 /*nonbmp*/);
  34513. }
  34514. #if defined(DUK_USE_ES6)
  34515. DUK_INTERNAL duk_ret_t duk_bi_string_constructor_from_code_point(duk_hthread *thr) {
  34516. return duk__construct_from_codepoints(thr, 1 /*nonbmp*/);
  34517. }
  34518. #endif
  34519. /*
  34520. * toString(), valueOf()
  34521. */
  34522. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_to_string(duk_hthread *thr) {
  34523. duk_tval *tv;
  34524. duk_push_this(thr);
  34525. tv = duk_require_tval(thr, -1);
  34526. DUK_ASSERT(tv != NULL);
  34527. if (DUK_TVAL_IS_STRING(tv)) {
  34528. /* return as is */
  34529. } else if (DUK_TVAL_IS_OBJECT(tv)) {
  34530. duk_hobject *h = DUK_TVAL_GET_OBJECT(tv);
  34531. DUK_ASSERT(h != NULL);
  34532. /* Must be a "string object", i.e. class "String" */
  34533. if (DUK_HOBJECT_GET_CLASS_NUMBER(h) != DUK_HOBJECT_CLASS_STRING) {
  34534. goto type_error;
  34535. }
  34536. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VALUE);
  34537. DUK_ASSERT(duk_is_string(thr, -1));
  34538. } else {
  34539. goto type_error;
  34540. }
  34541. (void) duk_require_hstring_notsymbol(thr, -1); /* Reject symbols (and wrapped symbols). */
  34542. return 1;
  34543. type_error:
  34544. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  34545. }
  34546. /*
  34547. * Character and charcode access
  34548. */
  34549. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_at(duk_hthread *thr) {
  34550. duk_hstring *h;
  34551. duk_int_t pos;
  34552. /* XXX: faster implementation */
  34553. h = duk_push_this_coercible_to_string(thr);
  34554. DUK_ASSERT(h != NULL);
  34555. pos = duk_to_int(thr, 0);
  34556. if (sizeof(duk_size_t) >= sizeof(duk_uint_t)) {
  34557. /* Cast to duk_size_t works in this case:
  34558. * - If pos < 0, (duk_size_t) pos will always be
  34559. * >= max_charlen, and result will be the empty string
  34560. * (see duk_substring()).
  34561. * - If pos >= 0, pos + 1 cannot wrap.
  34562. */
  34563. DUK_ASSERT((duk_size_t) DUK_INT_MIN >= DUK_HSTRING_MAX_BYTELEN);
  34564. DUK_ASSERT((duk_size_t) DUK_INT_MAX + 1U > (duk_size_t) DUK_INT_MAX);
  34565. duk_substring(thr, -1, (duk_size_t) pos, (duk_size_t) pos + 1U);
  34566. } else {
  34567. /* If size_t is smaller than int, explicit bounds checks
  34568. * are needed because an int may wrap multiple times.
  34569. */
  34570. if (DUK_UNLIKELY(pos < 0 || (duk_uint_t) pos >= (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h))) {
  34571. duk_push_hstring_empty(thr);
  34572. } else {
  34573. duk_substring(thr, -1, (duk_size_t) pos, (duk_size_t) pos + 1U);
  34574. }
  34575. }
  34576. return 1;
  34577. }
  34578. /* Magic: 0=charCodeAt, 1=codePointAt */
  34579. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_char_code_at(duk_hthread *thr) {
  34580. duk_int_t pos;
  34581. duk_hstring *h;
  34582. duk_bool_t clamped;
  34583. duk_uint32_t cp;
  34584. duk_int_t magic;
  34585. /* XXX: faster implementation */
  34586. DUK_DDD(DUK_DDDPRINT("arg=%!T", (duk_tval *) duk_get_tval(thr, 0)));
  34587. h = duk_push_this_coercible_to_string(thr);
  34588. DUK_ASSERT(h != NULL);
  34589. pos = duk_to_int_clamped_raw(thr,
  34590. 0 /*index*/,
  34591. 0 /*min(incl)*/,
  34592. (duk_int_t) DUK_HSTRING_GET_CHARLEN(h) - 1 /*max(incl)*/,
  34593. &clamped /*out_clamped*/);
  34594. #if defined(DUK_USE_ES6)
  34595. magic = duk_get_current_magic(thr);
  34596. #else
  34597. DUK_ASSERT(duk_get_current_magic(thr) == 0);
  34598. magic = 0;
  34599. #endif
  34600. if (clamped) {
  34601. /* For out-of-bounds indices .charCodeAt() returns NaN and
  34602. * .codePointAt() returns undefined.
  34603. */
  34604. if (magic != 0) {
  34605. return 0;
  34606. }
  34607. duk_push_nan(thr);
  34608. } else {
  34609. DUK_ASSERT(pos >= 0);
  34610. cp = (duk_uint32_t) duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) pos, (duk_bool_t) magic /*surrogate_aware*/);
  34611. duk_push_u32(thr, cp);
  34612. }
  34613. return 1;
  34614. }
  34615. /*
  34616. * substring(), substr(), slice()
  34617. */
  34618. /* XXX: any chance of merging these three similar but still slightly
  34619. * different algorithms so that footprint would be reduced?
  34620. */
  34621. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substring(duk_hthread *thr) {
  34622. duk_hstring *h;
  34623. duk_int_t start_pos, end_pos;
  34624. duk_int_t len;
  34625. h = duk_push_this_coercible_to_string(thr);
  34626. DUK_ASSERT(h != NULL);
  34627. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  34628. /* [ start end str ] */
  34629. start_pos = duk_to_int_clamped(thr, 0, 0, len);
  34630. if (duk_is_undefined(thr, 1)) {
  34631. end_pos = len;
  34632. } else {
  34633. end_pos = duk_to_int_clamped(thr, 1, 0, len);
  34634. }
  34635. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  34636. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  34637. if (start_pos > end_pos) {
  34638. duk_int_t tmp = start_pos;
  34639. start_pos = end_pos;
  34640. end_pos = tmp;
  34641. }
  34642. DUK_ASSERT(end_pos >= start_pos);
  34643. duk_substring(thr, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  34644. return 1;
  34645. }
  34646. #if defined(DUK_USE_SECTION_B)
  34647. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_substr(duk_hthread *thr) {
  34648. duk_hstring *h;
  34649. duk_int_t start_pos, end_pos;
  34650. duk_int_t len;
  34651. /* Unlike non-obsolete String calls, substr() algorithm in E5.1
  34652. * specification will happily coerce undefined and null to strings
  34653. * ("undefined" and "null").
  34654. */
  34655. duk_push_this(thr);
  34656. h = duk_to_hstring_m1(thr); /* Reject Symbols. */
  34657. DUK_ASSERT(h != NULL);
  34658. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  34659. /* [ start length str ] */
  34660. /* The implementation for computing of start_pos and end_pos differs
  34661. * from the standard algorithm, but is intended to result in the exactly
  34662. * same behavior. This is not always obvious.
  34663. */
  34664. /* combines steps 2 and 5; -len ensures max() not needed for step 5 */
  34665. start_pos = duk_to_int_clamped(thr, 0, -len, len);
  34666. if (start_pos < 0) {
  34667. start_pos = len + start_pos;
  34668. }
  34669. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  34670. /* combines steps 3, 6; step 7 is not needed */
  34671. if (duk_is_undefined(thr, 1)) {
  34672. end_pos = len;
  34673. } else {
  34674. DUK_ASSERT(start_pos <= len);
  34675. end_pos = start_pos + duk_to_int_clamped(thr, 1, 0, len - start_pos);
  34676. }
  34677. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  34678. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  34679. DUK_ASSERT(end_pos >= start_pos);
  34680. duk_substring(thr, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  34681. return 1;
  34682. }
  34683. #endif /* DUK_USE_SECTION_B */
  34684. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_slice(duk_hthread *thr) {
  34685. duk_hstring *h;
  34686. duk_int_t start_pos, end_pos;
  34687. duk_int_t len;
  34688. h = duk_push_this_coercible_to_string(thr);
  34689. DUK_ASSERT(h != NULL);
  34690. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  34691. /* [ start end str ] */
  34692. start_pos = duk_to_int_clamped(thr, 0, -len, len);
  34693. if (start_pos < 0) {
  34694. start_pos = len + start_pos;
  34695. }
  34696. if (duk_is_undefined(thr, 1)) {
  34697. end_pos = len;
  34698. } else {
  34699. end_pos = duk_to_int_clamped(thr, 1, -len, len);
  34700. if (end_pos < 0) {
  34701. end_pos = len + end_pos;
  34702. }
  34703. }
  34704. DUK_ASSERT(start_pos >= 0 && start_pos <= len);
  34705. DUK_ASSERT(end_pos >= 0 && end_pos <= len);
  34706. if (end_pos < start_pos) {
  34707. end_pos = start_pos;
  34708. }
  34709. DUK_ASSERT(end_pos >= start_pos);
  34710. duk_substring(thr, -1, (duk_size_t) start_pos, (duk_size_t) end_pos);
  34711. return 1;
  34712. }
  34713. /*
  34714. * Case conversion
  34715. */
  34716. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_caseconv_shared(duk_hthread *thr) {
  34717. duk_small_int_t uppercase = duk_get_current_magic(thr);
  34718. (void) duk_push_this_coercible_to_string(thr);
  34719. duk_unicode_case_convert_string(thr, (duk_bool_t) uppercase);
  34720. return 1;
  34721. }
  34722. /*
  34723. * indexOf() and lastIndexOf()
  34724. */
  34725. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_indexof_shared(duk_hthread *thr) {
  34726. duk_hstring *h_this;
  34727. duk_hstring *h_search;
  34728. duk_int_t clen_this;
  34729. duk_int_t cpos;
  34730. duk_small_uint_t is_lastindexof = (duk_small_uint_t) duk_get_current_magic(thr); /* 0=indexOf, 1=lastIndexOf */
  34731. h_this = duk_push_this_coercible_to_string(thr);
  34732. DUK_ASSERT(h_this != NULL);
  34733. clen_this = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h_this);
  34734. h_search = duk_to_hstring(thr, 0);
  34735. DUK_ASSERT(h_search != NULL);
  34736. duk_to_number(thr, 1);
  34737. if (duk_is_nan(thr, 1) && is_lastindexof) {
  34738. /* indexOf: NaN should cause pos to be zero.
  34739. * lastIndexOf: NaN should cause pos to be +Infinity
  34740. * (and later be clamped to len).
  34741. */
  34742. cpos = clen_this;
  34743. } else {
  34744. cpos = duk_to_int_clamped(thr, 1, 0, clen_this);
  34745. }
  34746. cpos = duk__str_search_shared(thr, h_this, h_search, cpos, is_lastindexof /*backwards*/);
  34747. duk_push_int(thr, cpos);
  34748. return 1;
  34749. }
  34750. /*
  34751. * replace()
  34752. */
  34753. /* XXX: the current implementation works but is quite clunky; it compiles
  34754. * to almost 1,4kB of x86 code so it needs to be simplified (better approach,
  34755. * shared helpers, etc). Some ideas for refactoring:
  34756. *
  34757. * - a primitive to convert a string into a regexp matcher (reduces matching
  34758. * code at the cost of making matching much slower)
  34759. * - use replace() as a basic helper for match() and split(), which are both
  34760. * much simpler
  34761. * - API call to get_prop and to_boolean
  34762. */
  34763. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_replace(duk_hthread *thr) {
  34764. duk_hstring *h_input;
  34765. duk_hstring *h_match;
  34766. duk_hstring *h_search;
  34767. duk_hobject *h_re;
  34768. duk_bufwriter_ctx bw_alloc;
  34769. duk_bufwriter_ctx *bw;
  34770. #if defined(DUK_USE_REGEXP_SUPPORT)
  34771. duk_bool_t is_regexp;
  34772. duk_bool_t is_global;
  34773. #endif
  34774. duk_bool_t is_repl_func;
  34775. duk_uint32_t match_start_coff, match_start_boff;
  34776. #if defined(DUK_USE_REGEXP_SUPPORT)
  34777. duk_int_t match_caps;
  34778. #endif
  34779. duk_uint32_t prev_match_end_boff;
  34780. const duk_uint8_t *r_start, *r_end, *r; /* repl string scan */
  34781. duk_size_t tmp_sz;
  34782. DUK_ASSERT_TOP(thr, 2);
  34783. h_input = duk_push_this_coercible_to_string(thr);
  34784. DUK_ASSERT(h_input != NULL);
  34785. bw = &bw_alloc;
  34786. DUK_BW_INIT_PUSHBUF(thr, bw, DUK_HSTRING_GET_BYTELEN(h_input)); /* input size is good output starting point */
  34787. DUK_ASSERT_TOP(thr, 4);
  34788. /* stack[0] = search value
  34789. * stack[1] = replace value
  34790. * stack[2] = input string
  34791. * stack[3] = result buffer
  34792. */
  34793. h_re = duk_get_hobject_with_class(thr, 0, DUK_HOBJECT_CLASS_REGEXP);
  34794. if (h_re) {
  34795. #if defined(DUK_USE_REGEXP_SUPPORT)
  34796. is_regexp = 1;
  34797. is_global = duk_get_prop_stridx_boolean(thr, 0, DUK_STRIDX_GLOBAL, NULL);
  34798. if (is_global) {
  34799. /* start match from beginning */
  34800. duk_push_int(thr, 0);
  34801. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  34802. }
  34803. #else /* DUK_USE_REGEXP_SUPPORT */
  34804. DUK_DCERROR_UNSUPPORTED(thr);
  34805. #endif /* DUK_USE_REGEXP_SUPPORT */
  34806. } else {
  34807. duk_to_string(thr, 0); /* rejects symbols */
  34808. #if defined(DUK_USE_REGEXP_SUPPORT)
  34809. is_regexp = 0;
  34810. is_global = 0;
  34811. #endif
  34812. }
  34813. if (duk_is_function(thr, 1)) {
  34814. is_repl_func = 1;
  34815. r_start = NULL;
  34816. r_end = NULL;
  34817. } else {
  34818. duk_hstring *h_repl;
  34819. is_repl_func = 0;
  34820. h_repl = duk_to_hstring(thr, 1); /* reject symbols */
  34821. DUK_ASSERT(h_repl != NULL);
  34822. r_start = DUK_HSTRING_GET_DATA(h_repl);
  34823. r_end = r_start + DUK_HSTRING_GET_BYTELEN(h_repl);
  34824. }
  34825. prev_match_end_boff = 0;
  34826. for (;;) {
  34827. /*
  34828. * If matching with a regexp:
  34829. * - non-global RegExp: lastIndex not touched on a match, zeroed
  34830. * on a non-match
  34831. * - global RegExp: on match, lastIndex will be updated by regexp
  34832. * executor to point to next char after the matching part (so that
  34833. * characters in the matching part are not matched again)
  34834. *
  34835. * If matching with a string:
  34836. * - always non-global match, find first occurrence
  34837. *
  34838. * We need:
  34839. * - The character offset of start-of-match for the replacer function
  34840. * - The byte offsets for start-of-match and end-of-match to implement
  34841. * the replacement values $&, $`, and $', and to copy non-matching
  34842. * input string portions (including header and trailer) verbatim.
  34843. *
  34844. * NOTE: the E5.1 specification is a bit vague how the RegExp should
  34845. * behave in the replacement process; e.g. is matching done first for
  34846. * all matches (in the global RegExp case) before any replacer calls
  34847. * are made? See: test-bi-string-proto-replace.js for discussion.
  34848. */
  34849. DUK_ASSERT_TOP(thr, 4);
  34850. #if defined(DUK_USE_REGEXP_SUPPORT)
  34851. if (is_regexp) {
  34852. duk_dup_0(thr);
  34853. duk_dup_2(thr);
  34854. duk_regexp_match(thr); /* [ ... regexp input ] -> [ res_obj ] */
  34855. if (!duk_is_object(thr, -1)) {
  34856. duk_pop(thr);
  34857. break;
  34858. }
  34859. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INDEX);
  34860. DUK_ASSERT(duk_is_number(thr, -1));
  34861. match_start_coff = duk_get_uint(thr, -1);
  34862. duk_pop(thr);
  34863. duk_get_prop_index(thr, -1, 0);
  34864. DUK_ASSERT(duk_is_string(thr, -1));
  34865. h_match = duk_known_hstring(thr, -1);
  34866. duk_pop(thr); /* h_match is borrowed, remains reachable through match_obj */
  34867. if (DUK_HSTRING_GET_BYTELEN(h_match) == 0) {
  34868. /* This should be equivalent to match() algorithm step 8.f.iii.2:
  34869. * detect an empty match and allow it, but don't allow it twice.
  34870. */
  34871. duk_uint32_t last_index;
  34872. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  34873. last_index = (duk_uint32_t) duk_get_uint(thr, -1);
  34874. DUK_DDD(DUK_DDDPRINT("empty match, bump lastIndex: %ld -> %ld",
  34875. (long) last_index, (long) (last_index + 1)));
  34876. duk_pop(thr);
  34877. duk_push_uint(thr, (duk_uint_t) (last_index + 1));
  34878. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  34879. }
  34880. DUK_ASSERT(duk_get_length(thr, -1) <= DUK_INT_MAX); /* string limits */
  34881. match_caps = (duk_int_t) duk_get_length(thr, -1);
  34882. } else {
  34883. #else /* DUK_USE_REGEXP_SUPPORT */
  34884. { /* unconditionally */
  34885. #endif /* DUK_USE_REGEXP_SUPPORT */
  34886. const duk_uint8_t *p_start, *p_end, *p; /* input string scan */
  34887. const duk_uint8_t *q_start; /* match string */
  34888. duk_size_t q_blen;
  34889. #if defined(DUK_USE_REGEXP_SUPPORT)
  34890. DUK_ASSERT(!is_global); /* single match always */
  34891. #endif
  34892. p_start = DUK_HSTRING_GET_DATA(h_input);
  34893. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  34894. p = p_start;
  34895. h_search = duk_known_hstring(thr, 0);
  34896. q_start = DUK_HSTRING_GET_DATA(h_search);
  34897. q_blen = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_search);
  34898. p_end -= q_blen; /* ensure full memcmp() fits in while */
  34899. match_start_coff = 0;
  34900. while (p <= p_end) {
  34901. DUK_ASSERT(p + q_blen <= DUK_HSTRING_GET_DATA(h_input) + DUK_HSTRING_GET_BYTELEN(h_input));
  34902. if (duk_memcmp((const void *) p, (const void *) q_start, (size_t) q_blen) == 0) {
  34903. duk_dup_0(thr);
  34904. h_match = duk_known_hstring(thr, -1);
  34905. #if defined(DUK_USE_REGEXP_SUPPORT)
  34906. match_caps = 0;
  34907. #endif
  34908. goto found;
  34909. }
  34910. /* track utf-8 non-continuation bytes */
  34911. if ((p[0] & 0xc0) != 0x80) {
  34912. match_start_coff++;
  34913. }
  34914. p++;
  34915. }
  34916. /* not found */
  34917. break;
  34918. }
  34919. found:
  34920. /* stack[0] = search value
  34921. * stack[1] = replace value
  34922. * stack[2] = input string
  34923. * stack[3] = result buffer
  34924. * stack[4] = regexp match OR match string
  34925. */
  34926. match_start_boff = (duk_uint32_t) duk_heap_strcache_offset_char2byte(thr, h_input, match_start_coff);
  34927. tmp_sz = (duk_size_t) (match_start_boff - prev_match_end_boff);
  34928. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff, tmp_sz);
  34929. prev_match_end_boff = match_start_boff + DUK_HSTRING_GET_BYTELEN(h_match);
  34930. if (is_repl_func) {
  34931. duk_idx_t idx_args;
  34932. duk_hstring *h_repl;
  34933. /* regexp res_obj is at index 4 */
  34934. duk_dup_1(thr);
  34935. idx_args = duk_get_top(thr);
  34936. #if defined(DUK_USE_REGEXP_SUPPORT)
  34937. if (is_regexp) {
  34938. duk_int_t idx;
  34939. duk_require_stack(thr, match_caps + 2);
  34940. for (idx = 0; idx < match_caps; idx++) {
  34941. /* match followed by capture(s) */
  34942. duk_get_prop_index(thr, 4, (duk_uarridx_t) idx);
  34943. }
  34944. } else {
  34945. #else /* DUK_USE_REGEXP_SUPPORT */
  34946. { /* unconditionally */
  34947. #endif /* DUK_USE_REGEXP_SUPPORT */
  34948. /* match == search string, by definition */
  34949. duk_dup_0(thr);
  34950. }
  34951. duk_push_uint(thr, (duk_uint_t) match_start_coff);
  34952. duk_dup_2(thr);
  34953. /* [ ... replacer match [captures] match_char_offset input ] */
  34954. duk_call(thr, duk_get_top(thr) - idx_args);
  34955. h_repl = duk_to_hstring_m1(thr); /* -> [ ... repl_value ] */
  34956. DUK_ASSERT(h_repl != NULL);
  34957. DUK_BW_WRITE_ENSURE_HSTRING(thr, bw, h_repl);
  34958. duk_pop(thr); /* repl_value */
  34959. } else {
  34960. r = r_start;
  34961. while (r < r_end) {
  34962. duk_int_t ch1;
  34963. duk_int_t ch2;
  34964. #if defined(DUK_USE_REGEXP_SUPPORT)
  34965. duk_int_t ch3;
  34966. #endif
  34967. duk_size_t left;
  34968. ch1 = *r++;
  34969. if (ch1 != DUK_ASC_DOLLAR) {
  34970. goto repl_write;
  34971. }
  34972. DUK_ASSERT(r <= r_end);
  34973. left = (duk_size_t) (r_end - r);
  34974. if (left <= 0) {
  34975. goto repl_write;
  34976. }
  34977. ch2 = r[0];
  34978. switch (ch2) {
  34979. case DUK_ASC_DOLLAR: {
  34980. ch1 = (1 << 8) + DUK_ASC_DOLLAR;
  34981. goto repl_write;
  34982. }
  34983. case DUK_ASC_AMP: {
  34984. DUK_BW_WRITE_ENSURE_HSTRING(thr, bw, h_match);
  34985. r++;
  34986. continue;
  34987. }
  34988. case DUK_ASC_GRAVE: {
  34989. tmp_sz = (duk_size_t) match_start_boff;
  34990. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input), tmp_sz);
  34991. r++;
  34992. continue;
  34993. }
  34994. case DUK_ASC_SINGLEQUOTE: {
  34995. duk_uint32_t match_end_boff;
  34996. /* Use match charlen instead of bytelen, just in case the input and
  34997. * match codepoint encodings would have different lengths.
  34998. */
  34999. /* XXX: charlen computed here, and also in char2byte helper. */
  35000. match_end_boff = (duk_uint32_t) duk_heap_strcache_offset_char2byte(thr,
  35001. h_input,
  35002. match_start_coff + (duk_uint_fast32_t) DUK_HSTRING_GET_CHARLEN(h_match));
  35003. tmp_sz = (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - match_end_boff);
  35004. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input) + match_end_boff, tmp_sz);
  35005. r++;
  35006. continue;
  35007. }
  35008. default: {
  35009. #if defined(DUK_USE_REGEXP_SUPPORT)
  35010. duk_int_t capnum, captmp, capadv;
  35011. /* XXX: optional check, match_caps is zero if no regexp,
  35012. * so dollar will be interpreted literally anyway.
  35013. */
  35014. if (!is_regexp) {
  35015. goto repl_write;
  35016. }
  35017. if (!(ch2 >= DUK_ASC_0 && ch2 <= DUK_ASC_9)) {
  35018. goto repl_write;
  35019. }
  35020. capnum = ch2 - DUK_ASC_0;
  35021. capadv = 1;
  35022. if (left >= 2) {
  35023. ch3 = r[1];
  35024. if (ch3 >= DUK_ASC_0 && ch3 <= DUK_ASC_9) {
  35025. captmp = capnum * 10 + (ch3 - DUK_ASC_0);
  35026. if (captmp < match_caps) {
  35027. capnum = captmp;
  35028. capadv = 2;
  35029. }
  35030. }
  35031. }
  35032. if (capnum > 0 && capnum < match_caps) {
  35033. DUK_ASSERT(is_regexp != 0); /* match_caps == 0 without regexps */
  35034. /* regexp res_obj is at offset 4 */
  35035. duk_get_prop_index(thr, 4, (duk_uarridx_t) capnum);
  35036. if (duk_is_string(thr, -1)) {
  35037. duk_hstring *h_tmp_str;
  35038. h_tmp_str = duk_known_hstring(thr, -1);
  35039. DUK_BW_WRITE_ENSURE_HSTRING(thr, bw, h_tmp_str);
  35040. } else {
  35041. /* undefined -> skip (replaced with empty) */
  35042. }
  35043. duk_pop(thr);
  35044. r += capadv;
  35045. continue;
  35046. } else {
  35047. goto repl_write;
  35048. }
  35049. #else /* DUK_USE_REGEXP_SUPPORT */
  35050. goto repl_write; /* unconditionally */
  35051. #endif /* DUK_USE_REGEXP_SUPPORT */
  35052. } /* default case */
  35053. } /* switch (ch2) */
  35054. repl_write:
  35055. /* ch1 = (r_increment << 8) + byte */
  35056. DUK_BW_WRITE_ENSURE_U8(thr, bw, (duk_uint8_t) (ch1 & 0xff));
  35057. r += ch1 >> 8;
  35058. } /* while repl */
  35059. } /* if (is_repl_func) */
  35060. duk_pop(thr); /* pop regexp res_obj or match string */
  35061. #if defined(DUK_USE_REGEXP_SUPPORT)
  35062. if (!is_global) {
  35063. #else
  35064. { /* unconditionally; is_global==0 */
  35065. #endif
  35066. break;
  35067. }
  35068. }
  35069. /* trailer */
  35070. tmp_sz = (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - prev_match_end_boff);
  35071. DUK_BW_WRITE_ENSURE_BYTES(thr, bw, DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff, tmp_sz);
  35072. DUK_ASSERT_TOP(thr, 4);
  35073. DUK_BW_COMPACT(thr, bw);
  35074. (void) duk_buffer_to_string(thr, -1); /* Safe if inputs are safe. */
  35075. return 1;
  35076. }
  35077. /*
  35078. * split()
  35079. */
  35080. /* XXX: very messy now, but works; clean up, remove unused variables (nomimally
  35081. * used so compiler doesn't complain).
  35082. */
  35083. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_split(duk_hthread *thr) {
  35084. duk_hstring *h_input;
  35085. duk_hstring *h_sep;
  35086. duk_uint32_t limit;
  35087. duk_uint32_t arr_idx;
  35088. #if defined(DUK_USE_REGEXP_SUPPORT)
  35089. duk_bool_t is_regexp;
  35090. #endif
  35091. duk_bool_t matched; /* set to 1 if any match exists (needed for empty input special case) */
  35092. duk_uint32_t prev_match_end_coff, prev_match_end_boff;
  35093. duk_uint32_t match_start_boff, match_start_coff;
  35094. duk_uint32_t match_end_boff, match_end_coff;
  35095. h_input = duk_push_this_coercible_to_string(thr);
  35096. DUK_ASSERT(h_input != NULL);
  35097. duk_push_array(thr);
  35098. if (duk_is_undefined(thr, 1)) {
  35099. limit = 0xffffffffUL;
  35100. } else {
  35101. limit = duk_to_uint32(thr, 1);
  35102. }
  35103. if (limit == 0) {
  35104. return 1;
  35105. }
  35106. /* If the separator is a RegExp, make a "clone" of it. The specification
  35107. * algorithm calls [[Match]] directly for specific indices; we emulate this
  35108. * by tweaking lastIndex and using a "force global" variant of duk_regexp_match()
  35109. * which will use global-style matching even when the RegExp itself is non-global.
  35110. */
  35111. if (duk_is_undefined(thr, 0)) {
  35112. /* The spec algorithm first does "R = ToString(separator)" before checking
  35113. * whether separator is undefined. Since this is side effect free, we can
  35114. * skip the ToString() here.
  35115. */
  35116. duk_dup_2(thr);
  35117. duk_put_prop_index(thr, 3, 0);
  35118. return 1;
  35119. } else if (duk_get_hobject_with_class(thr, 0, DUK_HOBJECT_CLASS_REGEXP) != NULL) {
  35120. #if defined(DUK_USE_REGEXP_SUPPORT)
  35121. duk_push_hobject_bidx(thr, DUK_BIDX_REGEXP_CONSTRUCTOR);
  35122. duk_dup_0(thr);
  35123. duk_new(thr, 1); /* [ ... RegExp val ] -> [ ... res ] */
  35124. duk_replace(thr, 0);
  35125. /* lastIndex is initialized to zero by new RegExp() */
  35126. is_regexp = 1;
  35127. #else
  35128. DUK_DCERROR_UNSUPPORTED(thr);
  35129. #endif
  35130. } else {
  35131. duk_to_string(thr, 0);
  35132. #if defined(DUK_USE_REGEXP_SUPPORT)
  35133. is_regexp = 0;
  35134. #endif
  35135. }
  35136. /* stack[0] = separator (string or regexp)
  35137. * stack[1] = limit
  35138. * stack[2] = input string
  35139. * stack[3] = result array
  35140. */
  35141. prev_match_end_boff = 0;
  35142. prev_match_end_coff = 0;
  35143. arr_idx = 0;
  35144. matched = 0;
  35145. for (;;) {
  35146. /*
  35147. * The specification uses RegExp [[Match]] to attempt match at specific
  35148. * offsets. We don't have such a primitive, so we use an actual RegExp
  35149. * and tweak lastIndex. Since the RegExp may be non-global, we use a
  35150. * special variant which forces global-like behavior for matching.
  35151. */
  35152. DUK_ASSERT_TOP(thr, 4);
  35153. #if defined(DUK_USE_REGEXP_SUPPORT)
  35154. if (is_regexp) {
  35155. duk_dup_0(thr);
  35156. duk_dup_2(thr);
  35157. duk_regexp_match_force_global(thr); /* [ ... regexp input ] -> [ res_obj ] */
  35158. if (!duk_is_object(thr, -1)) {
  35159. duk_pop(thr);
  35160. break;
  35161. }
  35162. matched = 1;
  35163. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INDEX);
  35164. DUK_ASSERT(duk_is_number(thr, -1));
  35165. match_start_coff = duk_get_uint(thr, -1);
  35166. match_start_boff = (duk_uint32_t) duk_heap_strcache_offset_char2byte(thr, h_input, match_start_coff);
  35167. duk_pop(thr);
  35168. if (match_start_coff == DUK_HSTRING_GET_CHARLEN(h_input)) {
  35169. /* don't allow an empty match at the end of the string */
  35170. duk_pop(thr);
  35171. break;
  35172. }
  35173. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  35174. DUK_ASSERT(duk_is_number(thr, -1));
  35175. match_end_coff = duk_get_uint(thr, -1);
  35176. match_end_boff = (duk_uint32_t) duk_heap_strcache_offset_char2byte(thr, h_input, match_end_coff);
  35177. duk_pop(thr);
  35178. /* empty match -> bump and continue */
  35179. if (prev_match_end_boff == match_end_boff) {
  35180. duk_push_uint(thr, (duk_uint_t) (match_end_coff + 1));
  35181. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  35182. duk_pop(thr);
  35183. continue;
  35184. }
  35185. } else {
  35186. #else /* DUK_USE_REGEXP_SUPPORT */
  35187. { /* unconditionally */
  35188. #endif /* DUK_USE_REGEXP_SUPPORT */
  35189. const duk_uint8_t *p_start, *p_end, *p; /* input string scan */
  35190. const duk_uint8_t *q_start; /* match string */
  35191. duk_size_t q_blen, q_clen;
  35192. p_start = DUK_HSTRING_GET_DATA(h_input);
  35193. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
  35194. p = p_start + prev_match_end_boff;
  35195. h_sep = duk_known_hstring(thr, 0); /* symbol already rejected above */
  35196. q_start = DUK_HSTRING_GET_DATA(h_sep);
  35197. q_blen = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h_sep);
  35198. q_clen = (duk_size_t) DUK_HSTRING_GET_CHARLEN(h_sep);
  35199. p_end -= q_blen; /* ensure full memcmp() fits in while */
  35200. match_start_coff = prev_match_end_coff;
  35201. if (q_blen == 0) {
  35202. /* Handle empty separator case: it will always match, and always
  35203. * triggers the check in step 13.c.iii initially. Note that we
  35204. * must skip to either end of string or start of first codepoint,
  35205. * skipping over any continuation bytes!
  35206. *
  35207. * Don't allow an empty string to match at the end of the input.
  35208. */
  35209. matched = 1; /* empty separator can always match */
  35210. match_start_coff++;
  35211. p++;
  35212. while (p < p_end) {
  35213. if ((p[0] & 0xc0) != 0x80) {
  35214. goto found;
  35215. }
  35216. p++;
  35217. }
  35218. goto not_found;
  35219. }
  35220. DUK_ASSERT(q_blen > 0 && q_clen > 0);
  35221. while (p <= p_end) {
  35222. DUK_ASSERT(p + q_blen <= DUK_HSTRING_GET_DATA(h_input) + DUK_HSTRING_GET_BYTELEN(h_input));
  35223. DUK_ASSERT(q_blen > 0); /* no issues with empty memcmp() */
  35224. if (duk_memcmp((const void *) p, (const void *) q_start, (size_t) q_blen) == 0) {
  35225. /* never an empty match, so step 13.c.iii can't be triggered */
  35226. goto found;
  35227. }
  35228. /* track utf-8 non-continuation bytes */
  35229. if ((p[0] & 0xc0) != 0x80) {
  35230. match_start_coff++;
  35231. }
  35232. p++;
  35233. }
  35234. not_found:
  35235. /* not found */
  35236. break;
  35237. found:
  35238. matched = 1;
  35239. match_start_boff = (duk_uint32_t) (p - p_start);
  35240. match_end_coff = (duk_uint32_t) (match_start_coff + q_clen); /* constrained by string length */
  35241. match_end_boff = (duk_uint32_t) (match_start_boff + q_blen); /* ditto */
  35242. /* empty match (may happen with empty separator) -> bump and continue */
  35243. if (prev_match_end_boff == match_end_boff) {
  35244. prev_match_end_boff++;
  35245. prev_match_end_coff++;
  35246. continue;
  35247. }
  35248. } /* if (is_regexp) */
  35249. /* stack[0] = separator (string or regexp)
  35250. * stack[1] = limit
  35251. * stack[2] = input string
  35252. * stack[3] = result array
  35253. * stack[4] = regexp res_obj (if is_regexp)
  35254. */
  35255. DUK_DDD(DUK_DDDPRINT("split; match_start b=%ld,c=%ld, match_end b=%ld,c=%ld, prev_end b=%ld,c=%ld",
  35256. (long) match_start_boff, (long) match_start_coff,
  35257. (long) match_end_boff, (long) match_end_coff,
  35258. (long) prev_match_end_boff, (long) prev_match_end_coff));
  35259. duk_push_lstring(thr,
  35260. (const char *) (DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff),
  35261. (duk_size_t) (match_start_boff - prev_match_end_boff));
  35262. duk_put_prop_index(thr, 3, arr_idx);
  35263. arr_idx++;
  35264. if (arr_idx >= limit) {
  35265. goto hit_limit;
  35266. }
  35267. #if defined(DUK_USE_REGEXP_SUPPORT)
  35268. if (is_regexp) {
  35269. duk_size_t i, len;
  35270. len = duk_get_length(thr, 4);
  35271. for (i = 1; i < len; i++) {
  35272. DUK_ASSERT(i <= DUK_UARRIDX_MAX); /* cannot have >4G captures */
  35273. duk_get_prop_index(thr, 4, (duk_uarridx_t) i);
  35274. duk_put_prop_index(thr, 3, arr_idx);
  35275. arr_idx++;
  35276. if (arr_idx >= limit) {
  35277. goto hit_limit;
  35278. }
  35279. }
  35280. duk_pop(thr);
  35281. /* lastIndex already set up for next match */
  35282. } else {
  35283. #else /* DUK_USE_REGEXP_SUPPORT */
  35284. { /* unconditionally */
  35285. #endif /* DUK_USE_REGEXP_SUPPORT */
  35286. /* no action */
  35287. }
  35288. prev_match_end_boff = match_end_boff;
  35289. prev_match_end_coff = match_end_coff;
  35290. continue;
  35291. } /* for */
  35292. /* Combined step 11 (empty string special case) and 14-15. */
  35293. DUK_DDD(DUK_DDDPRINT("split trailer; prev_end b=%ld,c=%ld",
  35294. (long) prev_match_end_boff, (long) prev_match_end_coff));
  35295. if (DUK_HSTRING_GET_BYTELEN(h_input) > 0 || !matched) {
  35296. /* Add trailer if:
  35297. * a) non-empty input
  35298. * b) empty input and no (zero size) match found (step 11)
  35299. */
  35300. duk_push_lstring(thr,
  35301. (const char *) DUK_HSTRING_GET_DATA(h_input) + prev_match_end_boff,
  35302. (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h_input) - prev_match_end_boff));
  35303. duk_put_prop_index(thr, 3, arr_idx);
  35304. /* No arr_idx update or limit check */
  35305. }
  35306. return 1;
  35307. hit_limit:
  35308. #if defined(DUK_USE_REGEXP_SUPPORT)
  35309. if (is_regexp) {
  35310. duk_pop(thr);
  35311. }
  35312. #endif
  35313. return 1;
  35314. }
  35315. /*
  35316. * Various
  35317. */
  35318. #if defined(DUK_USE_REGEXP_SUPPORT)
  35319. DUK_LOCAL void duk__to_regexp_helper(duk_hthread *thr, duk_idx_t idx, duk_bool_t force_new) {
  35320. duk_hobject *h;
  35321. /* Shared helper for match() steps 3-4, search() steps 3-4. */
  35322. DUK_ASSERT(idx >= 0);
  35323. if (force_new) {
  35324. goto do_new;
  35325. }
  35326. h = duk_get_hobject_with_class(thr, idx, DUK_HOBJECT_CLASS_REGEXP);
  35327. if (!h) {
  35328. goto do_new;
  35329. }
  35330. return;
  35331. do_new:
  35332. duk_push_hobject_bidx(thr, DUK_BIDX_REGEXP_CONSTRUCTOR);
  35333. duk_dup(thr, idx);
  35334. duk_new(thr, 1); /* [ ... RegExp val ] -> [ ... res ] */
  35335. duk_replace(thr, idx);
  35336. }
  35337. #endif /* DUK_USE_REGEXP_SUPPORT */
  35338. #if defined(DUK_USE_REGEXP_SUPPORT)
  35339. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_search(duk_hthread *thr) {
  35340. /* Easiest way to implement the search required by the specification
  35341. * is to do a RegExp test() with lastIndex forced to zero. To avoid
  35342. * side effects on the argument, "clone" the RegExp if a RegExp was
  35343. * given as input.
  35344. *
  35345. * The global flag of the RegExp should be ignored; setting lastIndex
  35346. * to zero (which happens when "cloning" the RegExp) should have an
  35347. * equivalent effect.
  35348. */
  35349. DUK_ASSERT_TOP(thr, 1);
  35350. (void) duk_push_this_coercible_to_string(thr); /* at index 1 */
  35351. duk__to_regexp_helper(thr, 0 /*index*/, 1 /*force_new*/);
  35352. /* stack[0] = regexp
  35353. * stack[1] = string
  35354. */
  35355. /* Avoid using RegExp.prototype methods, as they're writable and
  35356. * configurable and may have been changed.
  35357. */
  35358. duk_dup_0(thr);
  35359. duk_dup_1(thr); /* [ ... re_obj input ] */
  35360. duk_regexp_match(thr); /* -> [ ... res_obj ] */
  35361. if (!duk_is_object(thr, -1)) {
  35362. duk_push_int(thr, -1);
  35363. return 1;
  35364. }
  35365. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INDEX);
  35366. DUK_ASSERT(duk_is_number(thr, -1));
  35367. return 1;
  35368. }
  35369. #endif /* DUK_USE_REGEXP_SUPPORT */
  35370. #if defined(DUK_USE_REGEXP_SUPPORT)
  35371. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_match(duk_hthread *thr) {
  35372. duk_bool_t global;
  35373. duk_int_t prev_last_index;
  35374. duk_int_t this_index;
  35375. duk_int_t arr_idx;
  35376. DUK_ASSERT_TOP(thr, 1);
  35377. (void) duk_push_this_coercible_to_string(thr);
  35378. duk__to_regexp_helper(thr, 0 /*index*/, 0 /*force_new*/);
  35379. global = duk_get_prop_stridx_boolean(thr, 0, DUK_STRIDX_GLOBAL, NULL);
  35380. DUK_ASSERT_TOP(thr, 2);
  35381. /* stack[0] = regexp
  35382. * stack[1] = string
  35383. */
  35384. if (!global) {
  35385. duk_regexp_match(thr); /* -> [ res_obj ] */
  35386. return 1; /* return 'res_obj' */
  35387. }
  35388. /* Global case is more complex. */
  35389. /* [ regexp string ] */
  35390. duk_push_int(thr, 0);
  35391. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  35392. duk_push_array(thr);
  35393. /* [ regexp string res_arr ] */
  35394. prev_last_index = 0;
  35395. arr_idx = 0;
  35396. for (;;) {
  35397. DUK_ASSERT_TOP(thr, 3);
  35398. duk_dup_0(thr);
  35399. duk_dup_1(thr);
  35400. duk_regexp_match(thr); /* -> [ ... regexp string ] -> [ ... res_obj ] */
  35401. if (!duk_is_object(thr, -1)) {
  35402. duk_pop(thr);
  35403. break;
  35404. }
  35405. duk_get_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  35406. DUK_ASSERT(duk_is_number(thr, -1));
  35407. this_index = duk_get_int(thr, -1);
  35408. duk_pop(thr);
  35409. if (this_index == prev_last_index) {
  35410. this_index++;
  35411. duk_push_int(thr, this_index);
  35412. duk_put_prop_stridx_short(thr, 0, DUK_STRIDX_LAST_INDEX);
  35413. }
  35414. prev_last_index = this_index;
  35415. duk_get_prop_index(thr, -1, 0); /* match string */
  35416. duk_put_prop_index(thr, 2, (duk_uarridx_t) arr_idx);
  35417. arr_idx++;
  35418. duk_pop(thr); /* res_obj */
  35419. }
  35420. if (arr_idx == 0) {
  35421. duk_push_null(thr);
  35422. }
  35423. return 1; /* return 'res_arr' or 'null' */
  35424. }
  35425. #endif /* DUK_USE_REGEXP_SUPPORT */
  35426. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_concat(duk_hthread *thr) {
  35427. /* duk_concat() coerces arguments with ToString() in correct order */
  35428. (void) duk_push_this_coercible_to_string(thr);
  35429. duk_insert(thr, 0); /* this is relatively expensive */
  35430. duk_concat(thr, duk_get_top(thr));
  35431. return 1;
  35432. }
  35433. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_trim(duk_hthread *thr) {
  35434. DUK_ASSERT_TOP(thr, 0);
  35435. (void) duk_push_this_coercible_to_string(thr);
  35436. duk_trim(thr, 0);
  35437. DUK_ASSERT_TOP(thr, 1);
  35438. return 1;
  35439. }
  35440. #if defined(DUK_USE_ES6)
  35441. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_repeat(duk_hthread *thr) {
  35442. duk_hstring *h_input;
  35443. duk_size_t input_blen;
  35444. duk_size_t result_len;
  35445. duk_int_t count_signed;
  35446. duk_uint_t count;
  35447. const duk_uint8_t *src;
  35448. duk_uint8_t *buf;
  35449. duk_uint8_t *p;
  35450. duk_double_t d;
  35451. #if !defined(DUK_USE_PREFER_SIZE)
  35452. duk_size_t copy_size;
  35453. duk_uint8_t *p_end;
  35454. #endif
  35455. DUK_ASSERT_TOP(thr, 1);
  35456. h_input = duk_push_this_coercible_to_string(thr);
  35457. DUK_ASSERT(h_input != NULL);
  35458. input_blen = DUK_HSTRING_GET_BYTELEN(h_input);
  35459. /* Count is ToNumber() coerced; +Infinity must be always rejected
  35460. * (even if input string is zero length), as well as negative values
  35461. * and -Infinity. -Infinity doesn't require an explicit check
  35462. * because duk_get_int() clamps it to DUK_INT_MIN which gets rejected
  35463. * as a negative value (regardless of input string length).
  35464. */
  35465. d = duk_to_number(thr, 0);
  35466. if (duk_double_is_posinf(d)) {
  35467. goto fail_range;
  35468. }
  35469. count_signed = duk_get_int(thr, 0);
  35470. if (count_signed < 0) {
  35471. goto fail_range;
  35472. }
  35473. count = (duk_uint_t) count_signed;
  35474. /* Overflow check for result length. */
  35475. result_len = count * input_blen;
  35476. if (count != 0 && result_len / count != input_blen) {
  35477. goto fail_range;
  35478. }
  35479. /* Temporary fixed buffer, later converted to string. */
  35480. buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, result_len);
  35481. DUK_ASSERT(buf != NULL);
  35482. src = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  35483. DUK_ASSERT(src != NULL);
  35484. #if defined(DUK_USE_PREFER_SIZE)
  35485. p = buf;
  35486. while (count-- > 0) {
  35487. duk_memcpy((void *) p, (const void *) src, input_blen); /* copy size may be zero, but pointers are valid */
  35488. p += input_blen;
  35489. }
  35490. #else /* DUK_USE_PREFER_SIZE */
  35491. /* Take advantage of already copied pieces to speed up the process
  35492. * especially for small repeated strings.
  35493. */
  35494. p = buf;
  35495. p_end = p + result_len;
  35496. copy_size = input_blen;
  35497. for (;;) {
  35498. duk_size_t remain = (duk_size_t) (p_end - p);
  35499. DUK_DDD(DUK_DDDPRINT("remain=%ld, copy_size=%ld, input_blen=%ld, result_len=%ld",
  35500. (long) remain, (long) copy_size, (long) input_blen,
  35501. (long) result_len));
  35502. if (remain <= copy_size) {
  35503. /* If result_len is zero, this case is taken and does
  35504. * a zero size copy (with valid pointers).
  35505. */
  35506. duk_memcpy((void *) p, (const void *) src, remain);
  35507. break;
  35508. } else {
  35509. duk_memcpy((void *) p, (const void *) src, copy_size);
  35510. p += copy_size;
  35511. }
  35512. src = (const duk_uint8_t *) buf; /* Use buf as source for larger copies. */
  35513. copy_size = (duk_size_t) (p - buf);
  35514. }
  35515. #endif /* DUK_USE_PREFER_SIZE */
  35516. /* XXX: It would be useful to be able to create a duk_hstring with
  35517. * a certain byte size whose data area wasn't initialized and which
  35518. * wasn't in the string table yet. This would allow a string to be
  35519. * constructed directly without a buffer temporary and when it was
  35520. * finished, it could be injected into the string table. Currently
  35521. * this isn't possible because duk_hstrings are only tracked by the
  35522. * intern table (they are not in heap_allocated).
  35523. */
  35524. duk_buffer_to_string(thr, -1); /* Safe if input is safe. */
  35525. return 1;
  35526. fail_range:
  35527. DUK_DCERROR_RANGE_INVALID_ARGS(thr);
  35528. }
  35529. #endif /* DUK_USE_ES6 */
  35530. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_locale_compare(duk_hthread *thr) {
  35531. duk_hstring *h1;
  35532. duk_hstring *h2;
  35533. duk_size_t h1_len, h2_len, prefix_len;
  35534. duk_small_int_t ret = 0;
  35535. duk_small_int_t rc;
  35536. /* The current implementation of localeCompare() is simply a codepoint
  35537. * by codepoint comparison, implemented with a simple string compare
  35538. * because UTF-8 should preserve codepoint ordering (assuming valid
  35539. * shortest UTF-8 encoding).
  35540. *
  35541. * The specification requires that the return value must be related
  35542. * to the sort order: e.g. negative means that 'this' comes before
  35543. * 'that' in sort order. We assume an ascending sort order.
  35544. */
  35545. /* XXX: could share code with duk_js_ops.c, duk_js_compare_helper */
  35546. h1 = duk_push_this_coercible_to_string(thr);
  35547. DUK_ASSERT(h1 != NULL);
  35548. h2 = duk_to_hstring(thr, 0);
  35549. DUK_ASSERT(h2 != NULL);
  35550. h1_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1);
  35551. h2_len = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2);
  35552. prefix_len = (h1_len <= h2_len ? h1_len : h2_len);
  35553. rc = (duk_small_int_t) duk_memcmp((const void *) DUK_HSTRING_GET_DATA(h1),
  35554. (const void *) DUK_HSTRING_GET_DATA(h2),
  35555. (size_t) prefix_len);
  35556. if (rc < 0) {
  35557. ret = -1;
  35558. goto done;
  35559. } else if (rc > 0) {
  35560. ret = 1;
  35561. goto done;
  35562. }
  35563. /* prefix matches, lengths matter now */
  35564. if (h1_len > h2_len) {
  35565. ret = 1;
  35566. goto done;
  35567. } else if (h1_len == h2_len) {
  35568. DUK_ASSERT(ret == 0);
  35569. goto done;
  35570. }
  35571. ret = -1;
  35572. goto done;
  35573. done:
  35574. duk_push_int(thr, (duk_int_t) ret);
  35575. return 1;
  35576. }
  35577. #if defined(DUK_USE_ES6)
  35578. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_startswith_endswith(duk_hthread *thr) {
  35579. duk_int_t magic;
  35580. duk_hstring *h;
  35581. duk_hstring *h_search;
  35582. duk_size_t blen_search;
  35583. const duk_uint8_t *p_cmp_start;
  35584. duk_bool_t result;
  35585. h = duk_push_this_coercible_to_string(thr);
  35586. DUK_ASSERT(h != NULL);
  35587. h_search = duk__str_tostring_notregexp(thr, 0);
  35588. DUK_ASSERT(h_search != NULL);
  35589. magic = duk_get_current_magic(thr);
  35590. p_cmp_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  35591. blen_search = DUK_HSTRING_GET_BYTELEN(h_search);
  35592. if (duk_is_undefined(thr, 1)) {
  35593. if (magic) {
  35594. p_cmp_start = p_cmp_start + DUK_HSTRING_GET_BYTELEN(h) - blen_search;
  35595. } else {
  35596. /* p_cmp_start already OK */
  35597. }
  35598. } else {
  35599. duk_int_t len;
  35600. duk_int_t pos;
  35601. DUK_ASSERT(DUK_HSTRING_MAX_BYTELEN <= DUK_INT_MAX);
  35602. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  35603. pos = duk_to_int_clamped(thr, 1, 0, len);
  35604. DUK_ASSERT(pos >= 0 && pos <= len);
  35605. if (magic) {
  35606. p_cmp_start -= blen_search; /* Conceptually subtracted last, but do already here. */
  35607. }
  35608. DUK_ASSERT(pos >= 0 && pos <= len);
  35609. p_cmp_start += duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) pos);
  35610. }
  35611. /* The main comparison can be done using a memcmp() rather than
  35612. * doing codepoint comparisons: for CESU-8 strings there is a
  35613. * canonical representation for every codepoint. But we do need
  35614. * to deal with the char/byte offset translation to find the
  35615. * comparison range.
  35616. */
  35617. result = 0;
  35618. if (p_cmp_start >= DUK_HSTRING_GET_DATA(h) &&
  35619. (duk_size_t) (p_cmp_start - (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h)) + blen_search <= DUK_HSTRING_GET_BYTELEN(h)) {
  35620. if (duk_memcmp((const void *) p_cmp_start,
  35621. (const void *) DUK_HSTRING_GET_DATA(h_search),
  35622. (size_t) blen_search) == 0) {
  35623. result = 1;
  35624. }
  35625. }
  35626. duk_push_boolean(thr, result);
  35627. return 1;
  35628. }
  35629. #endif /* DUK_USE_ES6 */
  35630. #if defined(DUK_USE_ES6)
  35631. DUK_INTERNAL duk_ret_t duk_bi_string_prototype_includes(duk_hthread *thr) {
  35632. duk_hstring *h;
  35633. duk_hstring *h_search;
  35634. duk_int_t len;
  35635. duk_int_t pos;
  35636. h = duk_push_this_coercible_to_string(thr);
  35637. DUK_ASSERT(h != NULL);
  35638. h_search = duk__str_tostring_notregexp(thr, 0);
  35639. DUK_ASSERT(h_search != NULL);
  35640. len = (duk_int_t) DUK_HSTRING_GET_CHARLEN(h);
  35641. pos = duk_to_int_clamped(thr, 1, 0, len);
  35642. DUK_ASSERT(pos >= 0 && pos <= len);
  35643. pos = duk__str_search_shared(thr, h, h_search, pos, 0 /*backwards*/);
  35644. duk_push_boolean(thr, pos >= 0);
  35645. return 1;
  35646. }
  35647. #endif /* DUK_USE_ES6 */
  35648. #endif /* DUK_USE_STRING_BUILTIN */
  35649. #line 1 "duk_bi_symbol.c"
  35650. /*
  35651. * Symbol built-in
  35652. */
  35653. /* #include duk_internal.h -> already included */
  35654. #if defined(DUK_USE_SYMBOL_BUILTIN)
  35655. /*
  35656. * Constructor
  35657. */
  35658. DUK_INTERNAL duk_ret_t duk_bi_symbol_constructor_shared(duk_hthread *thr) {
  35659. const duk_uint8_t *desc;
  35660. duk_size_t len;
  35661. duk_uint8_t *buf;
  35662. duk_uint8_t *p;
  35663. duk_int_t magic;
  35664. magic = duk_get_current_magic(thr);
  35665. if (duk_is_undefined(thr, 0) && (magic == 0)) {
  35666. /* Symbol() accepts undefined and empty string, but they are
  35667. * treated differently.
  35668. */
  35669. desc = NULL;
  35670. len = 0;
  35671. } else {
  35672. /* Symbol.for() coerces undefined to 'undefined' */
  35673. desc = (const duk_uint8_t *) duk_to_lstring(thr, 0, &len);
  35674. }
  35675. /* Maximum symbol data length:
  35676. * +1 initial byte (0x80 or 0x81)
  35677. * +len description
  35678. * +1 0xff after description, before unique suffix
  35679. * +17 autogenerated unique suffix: 'ffffffff-ffffffff' is longest
  35680. * +1 0xff after unique suffix for symbols with undefined description
  35681. */
  35682. buf = (duk_uint8_t *) duk_push_fixed_buffer(thr, 1 + len + 1 + 17 + 1);
  35683. DUK_ASSERT(buf != NULL);
  35684. p = buf + 1;
  35685. DUK_ASSERT(desc != NULL || len == 0); /* may be NULL if len is 0 */
  35686. duk_memcpy_unsafe((void *) p, (const void *) desc, len);
  35687. p += len;
  35688. if (magic == 0) {
  35689. /* Symbol(): create unique symbol. Use two 32-bit values
  35690. * to avoid dependency on 64-bit types and 64-bit integer
  35691. * formatting (at least for now).
  35692. */
  35693. if (++thr->heap->sym_counter[0] == 0) {
  35694. thr->heap->sym_counter[1]++;
  35695. }
  35696. p += DUK_SPRINTF((char *) p, "\xFF" "%lx-%lx",
  35697. (unsigned long) thr->heap->sym_counter[1],
  35698. (unsigned long) thr->heap->sym_counter[0]);
  35699. if (desc == NULL) {
  35700. /* Special case for 'undefined' description, trailing
  35701. * 0xff distinguishes from empty string description,
  35702. * but needs minimal special case handling elsewhere.
  35703. */
  35704. *p++ = 0xff;
  35705. }
  35706. buf[0] = 0x81;
  35707. } else {
  35708. /* Symbol.for(): create a global symbol */
  35709. buf[0] = 0x80;
  35710. }
  35711. duk_push_lstring(thr, (const char *) buf, (duk_size_t) (p - buf));
  35712. DUK_DDD(DUK_DDDPRINT("created symbol: %!T", duk_get_tval(thr, -1)));
  35713. return 1;
  35714. }
  35715. DUK_LOCAL duk_hstring *duk__auto_unbox_symbol(duk_hthread *thr, duk_tval *tv_arg) {
  35716. duk_tval *tv;
  35717. duk_tval tv_val;
  35718. duk_hobject *h_obj;
  35719. duk_hstring *h_str;
  35720. DUK_ASSERT(tv_arg != NULL);
  35721. /* XXX: add internal helper: duk_auto_unbox_tval(thr, tv, mask); */
  35722. /* XXX: add internal helper: duk_auto_unbox(thr, tv, idx); */
  35723. tv = tv_arg;
  35724. if (DUK_TVAL_IS_OBJECT(tv)) {
  35725. h_obj = DUK_TVAL_GET_OBJECT(tv);
  35726. DUK_ASSERT(h_obj != NULL);
  35727. if (DUK_HOBJECT_GET_CLASS_NUMBER(h_obj) == DUK_HOBJECT_CLASS_SYMBOL) {
  35728. if (!duk_hobject_get_internal_value(thr->heap, h_obj, &tv_val)) {
  35729. return NULL;
  35730. }
  35731. tv = &tv_val;
  35732. } else {
  35733. return NULL;
  35734. }
  35735. }
  35736. if (!DUK_TVAL_IS_STRING(tv)) {
  35737. return NULL;
  35738. }
  35739. h_str = DUK_TVAL_GET_STRING(tv);
  35740. DUK_ASSERT(h_str != NULL);
  35741. /* Here symbol is more expected than not. */
  35742. if (DUK_UNLIKELY(!DUK_HSTRING_HAS_SYMBOL(h_str))) {
  35743. return NULL;
  35744. }
  35745. return h_str;
  35746. }
  35747. DUK_INTERNAL duk_ret_t duk_bi_symbol_tostring_shared(duk_hthread *thr) {
  35748. duk_hstring *h_str;
  35749. h_str = duk__auto_unbox_symbol(thr, DUK_HTHREAD_THIS_PTR(thr));
  35750. if (h_str == NULL) {
  35751. return DUK_RET_TYPE_ERROR;
  35752. }
  35753. if (duk_get_current_magic(thr) == 0) {
  35754. /* .toString() */
  35755. duk_push_symbol_descriptive_string(thr, h_str);
  35756. } else {
  35757. /* .valueOf() */
  35758. duk_push_hstring(thr, h_str);
  35759. }
  35760. return 1;
  35761. }
  35762. DUK_INTERNAL duk_ret_t duk_bi_symbol_key_for(duk_hthread *thr) {
  35763. duk_hstring *h;
  35764. const duk_uint8_t *p;
  35765. /* Argument must be a symbol but not checked here. The initial byte
  35766. * check will catch non-symbol strings.
  35767. */
  35768. h = duk_require_hstring(thr, 0);
  35769. DUK_ASSERT(h != NULL);
  35770. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  35771. DUK_ASSERT(p != NULL);
  35772. /* Even for zero length strings there's at least one NUL byte so
  35773. * we can safely check the initial byte.
  35774. */
  35775. if (p[0] == 0x80) {
  35776. /* Global symbol, return its key (bytes just after the initial byte). */
  35777. duk_push_lstring(thr, (const char *) (p + 1), (duk_size_t) (DUK_HSTRING_GET_BYTELEN(h) - 1));
  35778. return 1;
  35779. } else if (p[0] == 0x81 || p[0] == 0x82 || p[0] == 0xff) {
  35780. /* Local symbol or hidden symbol, return undefined. */
  35781. return 0;
  35782. }
  35783. /* Covers normal strings and unknown initial bytes. */
  35784. return DUK_RET_TYPE_ERROR;
  35785. }
  35786. DUK_INTERNAL duk_ret_t duk_bi_symbol_toprimitive(duk_hthread *thr) {
  35787. duk_hstring *h_str;
  35788. h_str = duk__auto_unbox_symbol(thr, DUK_HTHREAD_THIS_PTR(thr));
  35789. if (h_str == NULL) {
  35790. return DUK_RET_TYPE_ERROR;
  35791. }
  35792. duk_push_hstring(thr, h_str);
  35793. return 1;
  35794. }
  35795. #endif /* DUK_USE_SYMBOL_BUILTIN */
  35796. #line 1 "duk_bi_thread.c"
  35797. /*
  35798. * Thread builtins
  35799. */
  35800. /* #include duk_internal.h -> already included */
  35801. /*
  35802. * Constructor
  35803. */
  35804. #if defined(DUK_USE_COROUTINE_SUPPORT)
  35805. DUK_INTERNAL duk_ret_t duk_bi_thread_constructor(duk_hthread *thr) {
  35806. duk_hthread *new_thr;
  35807. duk_hobject *func;
  35808. /* Check that the argument is callable; this is not 100% because we
  35809. * don't allow native functions to be a thread's initial function.
  35810. * Resume will reject such functions in any case.
  35811. */
  35812. /* XXX: need a duk_require_func_promote_lfunc() */
  35813. func = duk_require_hobject_promote_lfunc(thr, 0);
  35814. DUK_ASSERT(func != NULL);
  35815. duk_require_callable(thr, 0);
  35816. duk_push_thread(thr);
  35817. new_thr = (duk_hthread *) duk_known_hobject(thr, -1);
  35818. new_thr->state = DUK_HTHREAD_STATE_INACTIVE;
  35819. /* push initial function call to new thread stack; this is
  35820. * picked up by resume().
  35821. */
  35822. duk_push_hobject(new_thr, func);
  35823. return 1; /* return thread */
  35824. }
  35825. #endif
  35826. /*
  35827. * Resume a thread.
  35828. *
  35829. * The thread must be in resumable state, either (a) new thread which hasn't
  35830. * yet started, or (b) a thread which has previously yielded. This method
  35831. * must be called from an ECMAScript function.
  35832. *
  35833. * Args:
  35834. * - thread
  35835. * - value
  35836. * - isError (defaults to false)
  35837. *
  35838. * Note: yield and resume handling is currently asymmetric.
  35839. */
  35840. #if defined(DUK_USE_COROUTINE_SUPPORT)
  35841. DUK_INTERNAL duk_ret_t duk_bi_thread_resume(duk_hthread *ctx) {
  35842. duk_hthread *thr = (duk_hthread *) ctx;
  35843. duk_hthread *thr_resume;
  35844. duk_hobject *caller_func;
  35845. duk_small_uint_t is_error;
  35846. DUK_DDD(DUK_DDDPRINT("Duktape.Thread.resume(): thread=%!T, value=%!T, is_error=%!T",
  35847. (duk_tval *) duk_get_tval(thr, 0),
  35848. (duk_tval *) duk_get_tval(thr, 1),
  35849. (duk_tval *) duk_get_tval(thr, 2)));
  35850. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  35851. DUK_ASSERT(thr->heap->curr_thread == thr);
  35852. thr_resume = duk_require_hthread(thr, 0);
  35853. DUK_ASSERT(duk_get_top(thr) == 3);
  35854. is_error = (duk_small_uint_t) duk_to_boolean_top_pop(thr);
  35855. DUK_ASSERT(duk_get_top(thr) == 2);
  35856. /* [ thread value ] */
  35857. /*
  35858. * Thread state and calling context checks
  35859. */
  35860. if (thr->callstack_top < 2) {
  35861. DUK_DD(DUK_DDPRINT("resume state invalid: callstack should contain at least 2 entries (caller and Duktape.Thread.resume)"));
  35862. goto state_error;
  35863. }
  35864. DUK_ASSERT(thr->callstack_curr != NULL);
  35865. DUK_ASSERT(thr->callstack_curr->parent != NULL);
  35866. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL); /* us */
  35867. DUK_ASSERT(DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)));
  35868. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr->parent) != NULL); /* caller */
  35869. caller_func = DUK_ACT_GET_FUNC(thr->callstack_curr->parent);
  35870. if (!DUK_HOBJECT_IS_COMPFUNC(caller_func)) {
  35871. DUK_DD(DUK_DDPRINT("resume state invalid: caller must be ECMAScript code"));
  35872. goto state_error;
  35873. }
  35874. /* Note: there is no requirement that: 'thr->callstack_preventcount == 1'
  35875. * like for yield.
  35876. */
  35877. if (thr_resume->state != DUK_HTHREAD_STATE_INACTIVE &&
  35878. thr_resume->state != DUK_HTHREAD_STATE_YIELDED) {
  35879. DUK_DD(DUK_DDPRINT("resume state invalid: target thread must be INACTIVE or YIELDED"));
  35880. goto state_error;
  35881. }
  35882. DUK_ASSERT(thr_resume->state == DUK_HTHREAD_STATE_INACTIVE ||
  35883. thr_resume->state == DUK_HTHREAD_STATE_YIELDED);
  35884. /* Further state-dependent pre-checks */
  35885. if (thr_resume->state == DUK_HTHREAD_STATE_YIELDED) {
  35886. /* no pre-checks now, assume a previous yield() has left things in
  35887. * tip-top shape (longjmp handler will assert for these).
  35888. */
  35889. } else {
  35890. duk_hobject *h_fun;
  35891. DUK_ASSERT(thr_resume->state == DUK_HTHREAD_STATE_INACTIVE);
  35892. /* The initial function must be an ECMAScript function (but
  35893. * can be bound). We must make sure of that before we longjmp
  35894. * because an error in the RESUME handler call processing will
  35895. * not be handled very cleanly.
  35896. */
  35897. if ((thr_resume->callstack_top != 0) ||
  35898. (thr_resume->valstack_top - thr_resume->valstack != 1)) {
  35899. goto state_error;
  35900. }
  35901. duk_push_tval(thr, DUK_GET_TVAL_NEGIDX(thr_resume, -1));
  35902. duk_resolve_nonbound_function(thr);
  35903. h_fun = duk_require_hobject(thr, -1); /* reject lightfuncs on purpose */
  35904. if (!DUK_HOBJECT_IS_CALLABLE(h_fun) || !DUK_HOBJECT_IS_COMPFUNC(h_fun)) {
  35905. goto state_error;
  35906. }
  35907. duk_pop(thr);
  35908. }
  35909. /*
  35910. * The error object has been augmented with a traceback and other
  35911. * info from its creation point -- usually another thread. The
  35912. * error handler is called here right before throwing, but it also
  35913. * runs in the resumer's thread. It might be nice to get a traceback
  35914. * from the resumee but this is not the case now.
  35915. */
  35916. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  35917. if (is_error) {
  35918. DUK_ASSERT_TOP(thr, 2); /* value (error) is at stack top */
  35919. duk_err_augment_error_throw(thr); /* in resumer's context */
  35920. }
  35921. #endif
  35922. #if defined(DUK_USE_DEBUG)
  35923. if (is_error) {
  35924. DUK_DDD(DUK_DDDPRINT("RESUME ERROR: thread=%!T, value=%!T",
  35925. (duk_tval *) duk_get_tval(thr, 0),
  35926. (duk_tval *) duk_get_tval(thr, 1)));
  35927. } else if (thr_resume->state == DUK_HTHREAD_STATE_YIELDED) {
  35928. DUK_DDD(DUK_DDDPRINT("RESUME NORMAL: thread=%!T, value=%!T",
  35929. (duk_tval *) duk_get_tval(thr, 0),
  35930. (duk_tval *) duk_get_tval(thr, 1)));
  35931. } else {
  35932. DUK_DDD(DUK_DDDPRINT("RESUME INITIAL: thread=%!T, value=%!T",
  35933. (duk_tval *) duk_get_tval(thr, 0),
  35934. (duk_tval *) duk_get_tval(thr, 1)));
  35935. }
  35936. #endif
  35937. thr->heap->lj.type = DUK_LJ_TYPE_RESUME;
  35938. /* lj value2: thread */
  35939. DUK_ASSERT(thr->valstack_bottom < thr->valstack_top);
  35940. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value2, &thr->valstack_bottom[0]); /* side effects */
  35941. /* lj value1: value */
  35942. DUK_ASSERT(thr->valstack_bottom + 1 < thr->valstack_top);
  35943. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value1, &thr->valstack_bottom[1]); /* side effects */
  35944. DUK_TVAL_CHKFAST_INPLACE_SLOW(&thr->heap->lj.value1);
  35945. thr->heap->lj.iserror = is_error;
  35946. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* call is from executor, so we know we have a jmpbuf */
  35947. duk_err_longjmp(thr); /* execution resumes in bytecode executor */
  35948. DUK_UNREACHABLE();
  35949. /* Never here, fall through to error (from compiler point of view). */
  35950. state_error:
  35951. DUK_DCERROR_TYPE_INVALID_STATE(thr);
  35952. }
  35953. #endif
  35954. /*
  35955. * Yield the current thread.
  35956. *
  35957. * The thread must be in yieldable state: it must have a resumer, and there
  35958. * must not be any yield-preventing calls (native calls and constructor calls,
  35959. * currently) in the thread's call stack (otherwise a resume would not be
  35960. * possible later). This method must be called from an ECMAScript function.
  35961. *
  35962. * Args:
  35963. * - value
  35964. * - isError (defaults to false)
  35965. *
  35966. * Note: yield and resume handling is currently asymmetric.
  35967. */
  35968. #if defined(DUK_USE_COROUTINE_SUPPORT)
  35969. DUK_INTERNAL duk_ret_t duk_bi_thread_yield(duk_hthread *thr) {
  35970. duk_hobject *caller_func;
  35971. duk_small_uint_t is_error;
  35972. DUK_DDD(DUK_DDDPRINT("Duktape.Thread.yield(): value=%!T, is_error=%!T",
  35973. (duk_tval *) duk_get_tval(thr, 0),
  35974. (duk_tval *) duk_get_tval(thr, 1)));
  35975. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  35976. DUK_ASSERT(thr->heap->curr_thread == thr);
  35977. DUK_ASSERT(duk_get_top(thr) == 2);
  35978. is_error = (duk_small_uint_t) duk_to_boolean_top_pop(thr);
  35979. DUK_ASSERT(duk_get_top(thr) == 1);
  35980. /* [ value ] */
  35981. /*
  35982. * Thread state and calling context checks
  35983. */
  35984. if (!thr->resumer) {
  35985. DUK_DD(DUK_DDPRINT("yield state invalid: current thread must have a resumer"));
  35986. goto state_error;
  35987. }
  35988. DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED);
  35989. if (thr->callstack_top < 2) {
  35990. DUK_DD(DUK_DDPRINT("yield state invalid: callstack should contain at least 2 entries (caller and Duktape.Thread.yield)"));
  35991. goto state_error;
  35992. }
  35993. DUK_ASSERT(thr->callstack_curr != NULL);
  35994. DUK_ASSERT(thr->callstack_curr->parent != NULL);
  35995. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL); /* us */
  35996. DUK_ASSERT(DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)));
  35997. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr->parent) != NULL); /* caller */
  35998. caller_func = DUK_ACT_GET_FUNC(thr->callstack_curr->parent);
  35999. if (!DUK_HOBJECT_IS_COMPFUNC(caller_func)) {
  36000. DUK_DD(DUK_DDPRINT("yield state invalid: caller must be ECMAScript code"));
  36001. goto state_error;
  36002. }
  36003. DUK_ASSERT(thr->callstack_preventcount >= 1); /* should never be zero, because we (Duktape.Thread.yield) are on the stack */
  36004. if (thr->callstack_preventcount != 1) {
  36005. /* Note: the only yield-preventing call is Duktape.Thread.yield(), hence check for 1, not 0 */
  36006. DUK_DD(DUK_DDPRINT("yield state invalid: there must be no yield-preventing calls in current thread callstack (preventcount is %ld)",
  36007. (long) thr->callstack_preventcount));
  36008. goto state_error;
  36009. }
  36010. /*
  36011. * The error object has been augmented with a traceback and other
  36012. * info from its creation point -- usually the current thread.
  36013. * The error handler, however, is called right before throwing
  36014. * and runs in the yielder's thread.
  36015. */
  36016. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  36017. if (is_error) {
  36018. DUK_ASSERT_TOP(thr, 1); /* value (error) is at stack top */
  36019. duk_err_augment_error_throw(thr); /* in yielder's context */
  36020. }
  36021. #endif
  36022. #if defined(DUK_USE_DEBUG)
  36023. if (is_error) {
  36024. DUK_DDD(DUK_DDDPRINT("YIELD ERROR: value=%!T",
  36025. (duk_tval *) duk_get_tval(thr, 0)));
  36026. } else {
  36027. DUK_DDD(DUK_DDDPRINT("YIELD NORMAL: value=%!T",
  36028. (duk_tval *) duk_get_tval(thr, 0)));
  36029. }
  36030. #endif
  36031. /*
  36032. * Process yield
  36033. *
  36034. * After longjmp(), processing continues in bytecode executor longjmp
  36035. * handler, which will e.g. update thr->resumer to NULL.
  36036. */
  36037. thr->heap->lj.type = DUK_LJ_TYPE_YIELD;
  36038. /* lj value1: value */
  36039. DUK_ASSERT(thr->valstack_bottom < thr->valstack_top);
  36040. DUK_TVAL_SET_TVAL_UPDREF(thr, &thr->heap->lj.value1, &thr->valstack_bottom[0]); /* side effects */
  36041. DUK_TVAL_CHKFAST_INPLACE_SLOW(&thr->heap->lj.value1);
  36042. thr->heap->lj.iserror = is_error;
  36043. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* call is from executor, so we know we have a jmpbuf */
  36044. duk_err_longjmp(thr); /* execution resumes in bytecode executor */
  36045. DUK_UNREACHABLE();
  36046. /* Never here, fall through to error (from compiler point of view). */
  36047. state_error:
  36048. DUK_DCERROR_TYPE_INVALID_STATE(thr);
  36049. }
  36050. #endif
  36051. #if defined(DUK_USE_COROUTINE_SUPPORT)
  36052. DUK_INTERNAL duk_ret_t duk_bi_thread_current(duk_hthread *thr) {
  36053. duk_push_current_thread(thr);
  36054. return 1;
  36055. }
  36056. #endif
  36057. #line 1 "duk_bi_thrower.c"
  36058. /*
  36059. * Type error thrower, E5 Section 13.2.3.
  36060. */
  36061. /* #include duk_internal.h -> already included */
  36062. DUK_INTERNAL duk_ret_t duk_bi_type_error_thrower(duk_hthread *thr) {
  36063. DUK_DCERROR_TYPE_INVALID_ARGS(thr);
  36064. }
  36065. #line 1 "duk_debug_fixedbuffer.c"
  36066. /*
  36067. * Fixed buffer helper useful for debugging, requires no allocation
  36068. * which is critical for debugging.
  36069. */
  36070. /* #include duk_internal.h -> already included */
  36071. #if defined(DUK_USE_DEBUG)
  36072. DUK_INTERNAL void duk_fb_put_bytes(duk_fixedbuffer *fb, const duk_uint8_t *buffer, duk_size_t length) {
  36073. duk_size_t avail;
  36074. duk_size_t copylen;
  36075. avail = (fb->offset >= fb->length ? (duk_size_t) 0 : (duk_size_t) (fb->length - fb->offset));
  36076. if (length > avail) {
  36077. copylen = avail;
  36078. fb->truncated = 1;
  36079. } else {
  36080. copylen = length;
  36081. }
  36082. duk_memcpy_unsafe(fb->buffer + fb->offset, buffer, copylen);
  36083. fb->offset += copylen;
  36084. }
  36085. DUK_INTERNAL void duk_fb_put_byte(duk_fixedbuffer *fb, duk_uint8_t x) {
  36086. duk_fb_put_bytes(fb, (const duk_uint8_t *) &x, 1);
  36087. }
  36088. DUK_INTERNAL void duk_fb_put_cstring(duk_fixedbuffer *fb, const char *x) {
  36089. duk_fb_put_bytes(fb, (const duk_uint8_t *) x, (duk_size_t) DUK_STRLEN(x));
  36090. }
  36091. DUK_INTERNAL void duk_fb_sprintf(duk_fixedbuffer *fb, const char *fmt, ...) {
  36092. duk_size_t avail;
  36093. va_list ap;
  36094. va_start(ap, fmt);
  36095. avail = (fb->offset >= fb->length ? (duk_size_t) 0 : (duk_size_t) (fb->length - fb->offset));
  36096. if (avail > 0) {
  36097. duk_int_t res = (duk_int_t) DUK_VSNPRINTF((char *) (fb->buffer + fb->offset), avail, fmt, ap);
  36098. if (res < 0) {
  36099. /* error */
  36100. } else if ((duk_size_t) res >= avail) {
  36101. /* (maybe) truncated */
  36102. fb->offset += avail;
  36103. if ((duk_size_t) res > avail) {
  36104. /* actual chars dropped (not just NUL term) */
  36105. fb->truncated = 1;
  36106. }
  36107. } else {
  36108. /* normal */
  36109. fb->offset += (duk_size_t) res;
  36110. }
  36111. }
  36112. va_end(ap);
  36113. }
  36114. DUK_INTERNAL void duk_fb_put_funcptr(duk_fixedbuffer *fb, duk_uint8_t *fptr, duk_size_t fptr_size) {
  36115. char buf[64+1];
  36116. duk_debug_format_funcptr(buf, sizeof(buf), fptr, fptr_size);
  36117. buf[sizeof(buf) - 1] = (char) 0;
  36118. duk_fb_put_cstring(fb, buf);
  36119. }
  36120. DUK_INTERNAL duk_bool_t duk_fb_is_full(duk_fixedbuffer *fb) {
  36121. return (fb->offset >= fb->length);
  36122. }
  36123. #endif /* DUK_USE_DEBUG */
  36124. #line 1 "duk_debug_vsnprintf.c"
  36125. /*
  36126. * Custom formatter for debug printing, allowing Duktape specific data
  36127. * structures (such as tagged values and heap objects) to be printed with
  36128. * a nice format string. Because debug printing should not affect execution
  36129. * state, formatting here must be independent of execution (see implications
  36130. * below) and must not allocate memory.
  36131. *
  36132. * Custom format tags begin with a '%!' to safely distinguish them from
  36133. * standard format tags. The following conversions are supported:
  36134. *
  36135. * %!T tagged value (duk_tval *)
  36136. * %!O heap object (duk_heaphdr *)
  36137. * %!I decoded bytecode instruction
  36138. * %!C bytecode instruction opcode name (arg is long)
  36139. *
  36140. * Everything is serialized in a JSON-like manner. The default depth is one
  36141. * level, internal prototype is not followed, and internal properties are not
  36142. * serialized. The following modifiers change this behavior:
  36143. *
  36144. * @ print pointers
  36145. * # print binary representations (where applicable)
  36146. * d deep traversal of own properties (not prototype)
  36147. * p follow prototype chain (useless without 'd')
  36148. * i include internal properties (other than prototype)
  36149. * x hexdump buffers
  36150. * h heavy formatting
  36151. *
  36152. * For instance, the following serializes objects recursively, but does not
  36153. * follow the prototype chain nor print internal properties: "%!dO".
  36154. *
  36155. * Notes:
  36156. *
  36157. * * Standard snprintf return value semantics seem to vary. This
  36158. * implementation returns the number of bytes it actually wrote
  36159. * (excluding the null terminator). If retval == buffer size,
  36160. * output was truncated (except for corner cases).
  36161. *
  36162. * * Output format is intentionally different from ECMAScript
  36163. * formatting requirements, as formatting here serves debugging
  36164. * of internals.
  36165. *
  36166. * * Depth checking (and updating) is done in each type printer
  36167. * separately, to allow them to call each other freely.
  36168. *
  36169. * * Some pathological structures might take ages to print (e.g.
  36170. * self recursion with 100 properties pointing to the object
  36171. * itself). To guard against these, each printer also checks
  36172. * whether the output buffer is full; if so, early exit.
  36173. *
  36174. * * Reference loops are detected using a loop stack.
  36175. */
  36176. /* #include duk_internal.h -> already included */
  36177. #if defined(DUK_USE_DEBUG)
  36178. /* #include stdio.h -> already included */
  36179. /* #include stdarg.h -> already included */
  36180. #include <string.h>
  36181. /* list of conversion specifiers that terminate a format tag;
  36182. * this is unfortunately guesswork.
  36183. */
  36184. #define DUK__ALLOWED_STANDARD_SPECIFIERS "diouxXeEfFgGaAcsCSpnm"
  36185. /* maximum length of standard format tag that we support */
  36186. #define DUK__MAX_FORMAT_TAG_LENGTH 32
  36187. /* heapobj recursion depth when deep printing is selected */
  36188. #define DUK__DEEP_DEPTH_LIMIT 8
  36189. /* maximum recursion depth for loop detection stacks */
  36190. #define DUK__LOOP_STACK_DEPTH 256
  36191. /* must match bytecode defines now; build autogenerate? */
  36192. DUK_LOCAL const char * const duk__bc_optab[256] = {
  36193. "LDREG", "STREG", "JUMP", "LDCONST", "LDINT", "LDINTX", "LDTHIS", "LDUNDEF",
  36194. "LDNULL", "LDTRUE", "LDFALSE", "GETVAR", "BNOT", "LNOT", "UNM", "UNP",
  36195. "EQ_RR", "EQ_CR", "EQ_RC", "EQ_CC", "NEQ_RR", "NEQ_CR", "NEQ_RC", "NEQ_CC",
  36196. "SEQ_RR", "SEQ_CR", "SEQ_RC", "SEQ_CC", "SNEQ_RR", "SNEQ_CR", "SNEQ_RC", "SNEQ_CC",
  36197. "GT_RR", "GT_CR", "GT_RC", "GT_CC", "GE_RR", "GE_CR", "GE_RC", "GE_CC",
  36198. "LT_RR", "LT_CR", "LT_RC", "LT_CC", "LE_RR", "LE_CR", "LE_RC", "LE_CC",
  36199. "IFTRUE_R", "IFTRUE_C", "IFFALSE_R", "IFFALSE_C", "ADD_RR", "ADD_CR", "ADD_RC", "ADD_CC",
  36200. "SUB_RR", "SUB_CR", "SUB_RC", "SUB_CC", "MUL_RR", "MUL_CR", "MUL_RC", "MUL_CC",
  36201. "DIV_RR", "DIV_CR", "DIV_RC", "DIV_CC", "MOD_RR", "MOD_CR", "MOD_RC", "MOD_CC",
  36202. "EXP_RR", "EXP_CR", "EXP_RC", "EXP_CC", "BAND_RR", "BAND_CR", "BAND_RC", "BAND_CC",
  36203. "BOR_RR", "BOR_CR", "BOR_RC", "BOR_CC", "BXOR_RR", "BXOR_CR", "BXOR_RC", "BXOR_CC",
  36204. "BASL_RR", "BASL_CR", "BASL_RC", "BASL_CC", "BLSR_RR", "BLSR_CR", "BLSR_RC", "BLSR_CC",
  36205. "BASR_RR", "BASR_CR", "BASR_RC", "BASR_CC", "INSTOF_RR", "INSTOF_CR", "INSTOF_RC", "INSTOF_CC",
  36206. "IN_RR", "IN_CR", "IN_RC", "IN_CC", "GETPROP_RR", "GETPROP_CR", "GETPROP_RC", "GETPROP_CC",
  36207. "PUTPROP_RR", "PUTPROP_CR", "PUTPROP_RC", "PUTPROP_CC", "DELPROP_RR", "DELPROP_CR", "DELPROP_RC", "DELPROP_CC",
  36208. "PREINCR", "PREDECR", "POSTINCR", "POSTDECR", "PREINCV", "PREDECV", "POSTINCV", "POSTDECV",
  36209. "PREINCP_RR", "PREINCP_CR", "PREINCP_RC", "PREINCP_CC", "PREDECP_RR", "PREDECP_CR", "PREDECP_RC", "PREDECP_CC",
  36210. "POSTINCP_RR", "POSTINCP_CR", "POSTINCP_RC", "POSTINCP_CC", "POSTDECP_RR", "POSTDECP_CR", "POSTDECP_RC", "POSTDECP_CC",
  36211. "DECLVAR_RR", "DECLVAR_CR", "DECLVAR_RC", "DECLVAR_CC", "REGEXP_RR", "REGEXP_RC", "REGEXP_CR", "REGEXP_CC",
  36212. "CLOSURE", "TYPEOF", "TYPEOFID", "PUTVAR", "DELVAR", "RETREG", "RETUNDEF", "RETCONST",
  36213. "RETCONSTN", "LABEL", "ENDLABEL", "BREAK", "CONTINUE", "TRYCATCH", "ENDTRY", "ENDCATCH",
  36214. "ENDFIN", "THROW", "INVLHS", "CSREG", "CSVAR_RR", "CSVAR_CR", "CSVAR_RC", "CSVAR_CC",
  36215. "CALL0", "CALL1", "CALL2", "CALL3", "CALL4", "CALL5", "CALL6", "CALL7",
  36216. "CALL8", "CALL9", "CALL10", "CALL11", "CALL12", "CALL13", "CALL14", "CALL15",
  36217. "NEWOBJ", "NEWARR", "MPUTOBJ", "MPUTOBJI", "INITSET", "INITGET", "MPUTARR", "MPUTARRI",
  36218. "SETALEN", "INITENUM", "NEXTENUM", "NEWTARGET", "DEBUGGER", "NOP", "INVALID", "UNUSED207",
  36219. "GETPROPC_RR", "GETPROPC_CR", "GETPROPC_RC", "GETPROPC_CC", "UNUSED212", "UNUSED213", "UNUSED214", "UNUSED215",
  36220. "UNUSED216", "UNUSED217", "UNUSED218", "UNUSED219", "UNUSED220", "UNUSED221", "UNUSED222", "UNUSED223",
  36221. "UNUSED224", "UNUSED225", "UNUSED226", "UNUSED227", "UNUSED228", "UNUSED229", "UNUSED230", "UNUSED231",
  36222. "UNUSED232", "UNUSED233", "UNUSED234", "UNUSED235", "UNUSED236", "UNUSED237", "UNUSED238", "UNUSED239",
  36223. "UNUSED240", "UNUSED241", "UNUSED242", "UNUSED243", "UNUSED244", "UNUSED245", "UNUSED246", "UNUSED247",
  36224. "UNUSED248", "UNUSED249", "UNUSED250", "UNUSED251", "UNUSED252", "UNUSED253", "UNUSED254", "UNUSED255"
  36225. };
  36226. typedef struct duk__dprint_state duk__dprint_state;
  36227. struct duk__dprint_state {
  36228. duk_fixedbuffer *fb;
  36229. /* loop_stack_index could be perhaps be replaced by 'depth', but it's nice
  36230. * to not couple these two mechanisms unnecessarily.
  36231. */
  36232. duk_hobject *loop_stack[DUK__LOOP_STACK_DEPTH];
  36233. duk_int_t loop_stack_index;
  36234. duk_int_t loop_stack_limit;
  36235. duk_int_t depth;
  36236. duk_int_t depth_limit;
  36237. duk_bool_t pointer;
  36238. duk_bool_t heavy;
  36239. duk_bool_t binary;
  36240. duk_bool_t follow_proto;
  36241. duk_bool_t internal;
  36242. duk_bool_t hexdump;
  36243. };
  36244. /* helpers */
  36245. DUK_LOCAL_DECL void duk__print_hstring(duk__dprint_state *st, duk_hstring *k, duk_bool_t quotes);
  36246. DUK_LOCAL_DECL void duk__print_hobject(duk__dprint_state *st, duk_hobject *h);
  36247. DUK_LOCAL_DECL void duk__print_hbuffer(duk__dprint_state *st, duk_hbuffer *h);
  36248. DUK_LOCAL_DECL void duk__print_tval(duk__dprint_state *st, duk_tval *tv);
  36249. DUK_LOCAL_DECL void duk__print_instr(duk__dprint_state *st, duk_instr_t ins);
  36250. DUK_LOCAL_DECL void duk__print_heaphdr(duk__dprint_state *st, duk_heaphdr *h);
  36251. DUK_LOCAL_DECL void duk__print_shared_heaphdr(duk__dprint_state *st, duk_heaphdr *h);
  36252. DUK_LOCAL_DECL void duk__print_shared_heaphdr_string(duk__dprint_state *st, duk_heaphdr_string *h);
  36253. DUK_LOCAL void duk__print_shared_heaphdr(duk__dprint_state *st, duk_heaphdr *h) {
  36254. duk_fixedbuffer *fb = st->fb;
  36255. if (st->heavy) {
  36256. duk_fb_sprintf(fb, "(%p)", (void *) h);
  36257. }
  36258. if (!h) {
  36259. return;
  36260. }
  36261. if (st->binary) {
  36262. duk_size_t i;
  36263. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  36264. for (i = 0; i < (duk_size_t) sizeof(*h); i++) {
  36265. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)h)[i]);
  36266. }
  36267. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  36268. }
  36269. #if defined(DUK_USE_REFERENCE_COUNTING) /* currently implicitly also DUK_USE_DOUBLE_LINKED_HEAP */
  36270. if (st->heavy) {
  36271. duk_fb_sprintf(fb, "[h_next=%p,h_prev=%p,h_refcount=%lu,h_flags=%08lx,type=%ld,"
  36272. "reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  36273. (void *) DUK_HEAPHDR_GET_NEXT(NULL, h),
  36274. (void *) DUK_HEAPHDR_GET_PREV(NULL, h),
  36275. (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(h),
  36276. (unsigned long) DUK_HEAPHDR_GET_FLAGS(h),
  36277. (long) DUK_HEAPHDR_GET_TYPE(h),
  36278. (long) (DUK_HEAPHDR_HAS_REACHABLE(h) ? 1 : 0),
  36279. (long) (DUK_HEAPHDR_HAS_TEMPROOT(h) ? 1 : 0),
  36280. (long) (DUK_HEAPHDR_HAS_FINALIZABLE(h) ? 1 : 0),
  36281. (long) (DUK_HEAPHDR_HAS_FINALIZED(h) ? 1 : 0));
  36282. }
  36283. #else
  36284. if (st->heavy) {
  36285. duk_fb_sprintf(fb, "[h_next=%p,h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  36286. (void *) DUK_HEAPHDR_GET_NEXT(NULL, h),
  36287. (unsigned long) DUK_HEAPHDR_GET_FLAGS(h),
  36288. (long) DUK_HEAPHDR_GET_TYPE(h),
  36289. (long) (DUK_HEAPHDR_HAS_REACHABLE(h) ? 1 : 0),
  36290. (long) (DUK_HEAPHDR_HAS_TEMPROOT(h) ? 1 : 0),
  36291. (long) (DUK_HEAPHDR_HAS_FINALIZABLE(h) ? 1 : 0),
  36292. (long) (DUK_HEAPHDR_HAS_FINALIZED(h) ? 1 : 0));
  36293. }
  36294. #endif
  36295. }
  36296. DUK_LOCAL void duk__print_shared_heaphdr_string(duk__dprint_state *st, duk_heaphdr_string *h) {
  36297. duk_fixedbuffer *fb = st->fb;
  36298. if (st->heavy) {
  36299. duk_fb_sprintf(fb, "(%p)", (void *) h);
  36300. }
  36301. if (!h) {
  36302. return;
  36303. }
  36304. if (st->binary) {
  36305. duk_size_t i;
  36306. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  36307. for (i = 0; i < (duk_size_t) sizeof(*h); i++) {
  36308. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)h)[i]);
  36309. }
  36310. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  36311. }
  36312. #if defined(DUK_USE_REFERENCE_COUNTING)
  36313. if (st->heavy) {
  36314. duk_fb_sprintf(fb, "[h_refcount=%lu,h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  36315. (unsigned long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h),
  36316. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h),
  36317. (long) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h),
  36318. (long) (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h) ? 1 : 0),
  36319. (long) (DUK_HEAPHDR_HAS_TEMPROOT((duk_heaphdr *) h) ? 1 : 0),
  36320. (long) (DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) h) ? 1 : 0),
  36321. (long) (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h) ? 1 : 0));
  36322. }
  36323. #else
  36324. if (st->heavy) {
  36325. duk_fb_sprintf(fb, "[h_flags=%08lx,type=%ld,reachable=%ld,temproot=%ld,finalizable=%ld,finalized=%ld]",
  36326. (unsigned long) DUK_HEAPHDR_GET_FLAGS((duk_heaphdr *) h),
  36327. (long) DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h),
  36328. (long) (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h) ? 1 : 0),
  36329. (long) (DUK_HEAPHDR_HAS_TEMPROOT((duk_heaphdr *) h) ? 1 : 0),
  36330. (long) (DUK_HEAPHDR_HAS_FINALIZABLE((duk_heaphdr *) h) ? 1 : 0),
  36331. (long) (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) h) ? 1 : 0));
  36332. }
  36333. #endif
  36334. }
  36335. DUK_LOCAL void duk__print_hstring(duk__dprint_state *st, duk_hstring *h, duk_bool_t quotes) {
  36336. duk_fixedbuffer *fb = st->fb;
  36337. const duk_uint8_t *p;
  36338. const duk_uint8_t *p_end;
  36339. /* terminal type: no depth check */
  36340. if (duk_fb_is_full(fb)) {
  36341. return;
  36342. }
  36343. duk__print_shared_heaphdr_string(st, &h->hdr);
  36344. if (!h) {
  36345. duk_fb_put_cstring(fb, "NULL");
  36346. return;
  36347. }
  36348. p = DUK_HSTRING_GET_DATA(h);
  36349. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  36350. if (p_end > p && p[0] == DUK_ASC_UNDERSCORE) {
  36351. /* If property key begins with underscore, encode it with
  36352. * forced quotes (e.g. "_Foo") to distinguish it from encoded
  36353. * internal properties (e.g. \x82Bar -> _Bar).
  36354. */
  36355. quotes = 1;
  36356. }
  36357. if (quotes) {
  36358. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_DOUBLEQUOTE);
  36359. }
  36360. while (p < p_end) {
  36361. duk_uint8_t ch = *p++;
  36362. /* two special escapes: '\' and '"', other printables as is */
  36363. if (ch == '\\') {
  36364. duk_fb_sprintf(fb, "\\\\");
  36365. } else if (ch == '"') {
  36366. duk_fb_sprintf(fb, "\\\"");
  36367. } else if (ch >= 0x20 && ch <= 0x7e) {
  36368. duk_fb_put_byte(fb, ch);
  36369. } else if (ch == 0x82 && !quotes) {
  36370. /* encode \x82Bar as _Bar if no quotes are
  36371. * applied, this is for readable internal keys.
  36372. */
  36373. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_UNDERSCORE);
  36374. } else {
  36375. duk_fb_sprintf(fb, "\\x%02lx", (unsigned long) ch);
  36376. }
  36377. }
  36378. if (quotes) {
  36379. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_DOUBLEQUOTE);
  36380. }
  36381. #if defined(DUK_USE_REFERENCE_COUNTING)
  36382. /* XXX: limit to quoted strings only, to save keys from being cluttered? */
  36383. duk_fb_sprintf(fb, "/%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(&h->hdr));
  36384. #endif
  36385. }
  36386. #define DUK__COMMA() do { \
  36387. if (first) { \
  36388. first = 0; \
  36389. } else { \
  36390. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COMMA); \
  36391. } \
  36392. } while (0)
  36393. DUK_LOCAL void duk__print_hobject(duk__dprint_state *st, duk_hobject *h) {
  36394. duk_fixedbuffer *fb = st->fb;
  36395. duk_uint_fast32_t i;
  36396. duk_tval *tv;
  36397. duk_hstring *key;
  36398. duk_bool_t first = 1;
  36399. const char *brace1 = "{";
  36400. const char *brace2 = "}";
  36401. duk_bool_t pushed_loopstack = 0;
  36402. if (duk_fb_is_full(fb)) {
  36403. return;
  36404. }
  36405. duk__print_shared_heaphdr(st, &h->hdr);
  36406. if (h && DUK_HOBJECT_HAS_ARRAY_PART(h)) {
  36407. brace1 = "[";
  36408. brace2 = "]";
  36409. }
  36410. if (!h) {
  36411. duk_fb_put_cstring(fb, "NULL");
  36412. goto finished;
  36413. }
  36414. if (st->depth >= st->depth_limit) {
  36415. const char *subtype = "generic";
  36416. if (DUK_HOBJECT_IS_COMPFUNC(h)) {
  36417. subtype = "compfunc";
  36418. } else if (DUK_HOBJECT_IS_NATFUNC(h)) {
  36419. subtype = "natfunc";
  36420. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  36421. subtype = "thread";
  36422. } else if (DUK_HOBJECT_IS_BUFOBJ(h)) {
  36423. subtype = "bufobj";
  36424. } else if (DUK_HOBJECT_IS_ARRAY(h)) {
  36425. subtype = "array";
  36426. }
  36427. duk_fb_sprintf(fb, "%sobject/%s %p%s", (const char *) brace1, subtype, (void *) h, (const char *) brace2);
  36428. return;
  36429. }
  36430. for (i = 0; i < (duk_uint_fast32_t) st->loop_stack_index; i++) {
  36431. if (st->loop_stack[i] == h) {
  36432. duk_fb_sprintf(fb, "%sLOOP:%p%s", (const char *) brace1, (void *) h, (const char *) brace2);
  36433. return;
  36434. }
  36435. }
  36436. /* after this, return paths should 'goto finished' for decrement */
  36437. st->depth++;
  36438. if (st->loop_stack_index >= st->loop_stack_limit) {
  36439. duk_fb_sprintf(fb, "%sOUT-OF-LOOP-STACK%s", (const char *) brace1, (const char *) brace2);
  36440. goto finished;
  36441. }
  36442. st->loop_stack[st->loop_stack_index++] = h;
  36443. pushed_loopstack = 1;
  36444. /*
  36445. * Notation: double underscore used for internal properties which are not
  36446. * stored in the property allocation (e.g. '__valstack').
  36447. */
  36448. duk_fb_put_cstring(fb, brace1);
  36449. if (DUK_HOBJECT_GET_PROPS(NULL, h)) {
  36450. duk_uint32_t a_limit;
  36451. a_limit = DUK_HOBJECT_GET_ASIZE(h);
  36452. if (st->internal) {
  36453. /* dump all allocated entries, unused entries print as 'unused',
  36454. * note that these may extend beyond current 'length' and look
  36455. * a bit funny.
  36456. */
  36457. } else {
  36458. /* leave out trailing 'unused' elements */
  36459. while (a_limit > 0) {
  36460. tv = DUK_HOBJECT_A_GET_VALUE_PTR(NULL, h, a_limit - 1);
  36461. if (!DUK_TVAL_IS_UNUSED(tv)) {
  36462. break;
  36463. }
  36464. a_limit--;
  36465. }
  36466. }
  36467. for (i = 0; i < a_limit; i++) {
  36468. tv = DUK_HOBJECT_A_GET_VALUE_PTR(NULL, h, i);
  36469. DUK__COMMA();
  36470. duk__print_tval(st, tv);
  36471. }
  36472. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(h); i++) {
  36473. key = DUK_HOBJECT_E_GET_KEY(NULL, h, i);
  36474. if (!key) {
  36475. continue;
  36476. }
  36477. if (!st->internal && DUK_HSTRING_HAS_HIDDEN(key)) {
  36478. continue;
  36479. }
  36480. DUK__COMMA();
  36481. duk__print_hstring(st, key, 0);
  36482. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COLON);
  36483. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(NULL, h, i)) {
  36484. duk_fb_sprintf(fb, "[get:%p,set:%p]",
  36485. (void *) DUK_HOBJECT_E_GET_VALUE(NULL, h, i).a.get,
  36486. (void *) DUK_HOBJECT_E_GET_VALUE(NULL, h, i).a.set);
  36487. } else {
  36488. tv = &DUK_HOBJECT_E_GET_VALUE(NULL, h, i).v;
  36489. duk__print_tval(st, tv);
  36490. }
  36491. if (st->heavy) {
  36492. duk_fb_sprintf(fb, "<%02lx>", (unsigned long) DUK_HOBJECT_E_GET_FLAGS(NULL, h, i));
  36493. }
  36494. }
  36495. }
  36496. if (st->internal) {
  36497. if (DUK_HOBJECT_IS_ARRAY(h)) {
  36498. DUK__COMMA(); duk_fb_sprintf(fb, "__array:true");
  36499. }
  36500. if (DUK_HOBJECT_HAS_EXTENSIBLE(h)) {
  36501. DUK__COMMA(); duk_fb_sprintf(fb, "__extensible:true");
  36502. }
  36503. if (DUK_HOBJECT_HAS_CONSTRUCTABLE(h)) {
  36504. DUK__COMMA(); duk_fb_sprintf(fb, "__constructable:true");
  36505. }
  36506. if (DUK_HOBJECT_HAS_BOUNDFUNC(h)) {
  36507. DUK__COMMA(); duk_fb_sprintf(fb, "__boundfunc:true");
  36508. }
  36509. if (DUK_HOBJECT_HAS_COMPFUNC(h)) {
  36510. DUK__COMMA(); duk_fb_sprintf(fb, "__compfunc:true");
  36511. }
  36512. if (DUK_HOBJECT_HAS_NATFUNC(h)) {
  36513. DUK__COMMA(); duk_fb_sprintf(fb, "__natfunc:true");
  36514. }
  36515. if (DUK_HOBJECT_HAS_BUFOBJ(h)) {
  36516. DUK__COMMA(); duk_fb_sprintf(fb, "__bufobj:true");
  36517. }
  36518. if (DUK_HOBJECT_IS_THREAD(h)) {
  36519. DUK__COMMA(); duk_fb_sprintf(fb, "__thread:true");
  36520. }
  36521. if (DUK_HOBJECT_HAS_ARRAY_PART(h)) {
  36522. DUK__COMMA(); duk_fb_sprintf(fb, "__array_part:true");
  36523. }
  36524. if (DUK_HOBJECT_HAS_STRICT(h)) {
  36525. DUK__COMMA(); duk_fb_sprintf(fb, "__strict:true");
  36526. }
  36527. if (DUK_HOBJECT_HAS_NOTAIL(h)) {
  36528. DUK__COMMA(); duk_fb_sprintf(fb, "__notail:true");
  36529. }
  36530. if (DUK_HOBJECT_HAS_NEWENV(h)) {
  36531. DUK__COMMA(); duk_fb_sprintf(fb, "__newenv:true");
  36532. }
  36533. if (DUK_HOBJECT_HAS_NAMEBINDING(h)) {
  36534. DUK__COMMA(); duk_fb_sprintf(fb, "__namebinding:true");
  36535. }
  36536. if (DUK_HOBJECT_HAS_CREATEARGS(h)) {
  36537. DUK__COMMA(); duk_fb_sprintf(fb, "__createargs:true");
  36538. }
  36539. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(h)) {
  36540. DUK__COMMA(); duk_fb_sprintf(fb, "__exotic_array:true");
  36541. }
  36542. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(h)) {
  36543. DUK__COMMA(); duk_fb_sprintf(fb, "__exotic_stringobj:true");
  36544. }
  36545. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h)) {
  36546. DUK__COMMA(); duk_fb_sprintf(fb, "__exotic_arguments:true");
  36547. }
  36548. if (DUK_HOBJECT_IS_BUFOBJ(h)) {
  36549. DUK__COMMA(); duk_fb_sprintf(fb, "__exotic_bufobj:true");
  36550. }
  36551. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(h)) {
  36552. DUK__COMMA(); duk_fb_sprintf(fb, "__exotic_proxyobj:true");
  36553. }
  36554. }
  36555. if (st->internal && DUK_HOBJECT_IS_ARRAY(h)) {
  36556. duk_harray *a = (duk_harray *) h;
  36557. DUK__COMMA(); duk_fb_sprintf(fb, "__length:%ld", (long) a->length);
  36558. DUK__COMMA(); duk_fb_sprintf(fb, "__length_nonwritable:%ld", (long) a->length_nonwritable);
  36559. } else if (st->internal && DUK_HOBJECT_IS_COMPFUNC(h)) {
  36560. duk_hcompfunc *f = (duk_hcompfunc *) h;
  36561. DUK__COMMA(); duk_fb_put_cstring(fb, "__data:");
  36562. duk__print_hbuffer(st, (duk_hbuffer *) DUK_HCOMPFUNC_GET_DATA(NULL, f));
  36563. DUK__COMMA(); duk_fb_put_cstring(fb, "__lexenv:"); duk__print_hobject(st, DUK_HCOMPFUNC_GET_LEXENV(NULL, f));
  36564. DUK__COMMA(); duk_fb_put_cstring(fb, "__varenv:"); duk__print_hobject(st, DUK_HCOMPFUNC_GET_VARENV(NULL, f));
  36565. DUK__COMMA(); duk_fb_sprintf(fb, "__nregs:%ld", (long) f->nregs);
  36566. DUK__COMMA(); duk_fb_sprintf(fb, "__nargs:%ld", (long) f->nargs);
  36567. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  36568. DUK__COMMA(); duk_fb_sprintf(fb, "__start_line:%ld", (long) f->start_line);
  36569. DUK__COMMA(); duk_fb_sprintf(fb, "__end_line:%ld", (long) f->end_line);
  36570. #endif
  36571. DUK__COMMA(); duk_fb_put_cstring(fb, "__data:");
  36572. duk__print_hbuffer(st, (duk_hbuffer *) DUK_HCOMPFUNC_GET_DATA(NULL, f));
  36573. } else if (st->internal && DUK_HOBJECT_IS_NATFUNC(h)) {
  36574. duk_hnatfunc *f = (duk_hnatfunc *) h;
  36575. DUK__COMMA(); duk_fb_sprintf(fb, "__func:");
  36576. duk_fb_put_funcptr(fb, (duk_uint8_t *) &f->func, sizeof(f->func));
  36577. DUK__COMMA(); duk_fb_sprintf(fb, "__nargs:%ld", (long) f->nargs);
  36578. DUK__COMMA(); duk_fb_sprintf(fb, "__magic:%ld", (long) f->magic);
  36579. } else if (st->internal && DUK_HOBJECT_IS_DECENV(h)) {
  36580. duk_hdecenv *e = (duk_hdecenv *) h;
  36581. DUK__COMMA(); duk_fb_sprintf(fb, "__thread:"); duk__print_hobject(st, (duk_hobject *) e->thread);
  36582. DUK__COMMA(); duk_fb_sprintf(fb, "__varmap:"); duk__print_hobject(st, (duk_hobject *) e->varmap);
  36583. DUK__COMMA(); duk_fb_sprintf(fb, "__regbase_byteoff:%ld", (long) e->regbase_byteoff);
  36584. } else if (st->internal && DUK_HOBJECT_IS_OBJENV(h)) {
  36585. duk_hobjenv *e = (duk_hobjenv *) h;
  36586. DUK__COMMA(); duk_fb_sprintf(fb, "__target:"); duk__print_hobject(st, (duk_hobject *) e->target);
  36587. DUK__COMMA(); duk_fb_sprintf(fb, "__has_this:%ld", (long) e->has_this);
  36588. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  36589. } else if (st->internal && DUK_HOBJECT_IS_BUFOBJ(h)) {
  36590. duk_hbufobj *b = (duk_hbufobj *) h;
  36591. DUK__COMMA(); duk_fb_sprintf(fb, "__buf:");
  36592. duk__print_hbuffer(st, (duk_hbuffer *) b->buf);
  36593. DUK__COMMA(); duk_fb_sprintf(fb, "__buf_prop:");
  36594. duk__print_hobject(st, (duk_hobject *) b->buf_prop);
  36595. DUK__COMMA(); duk_fb_sprintf(fb, "__offset:%ld", (long) b->offset);
  36596. DUK__COMMA(); duk_fb_sprintf(fb, "__length:%ld", (long) b->length);
  36597. DUK__COMMA(); duk_fb_sprintf(fb, "__shift:%ld", (long) b->shift);
  36598. DUK__COMMA(); duk_fb_sprintf(fb, "__elemtype:%ld", (long) b->elem_type);
  36599. #endif
  36600. } else if (st->internal && DUK_HOBJECT_IS_PROXY(h)) {
  36601. duk_hproxy *p = (duk_hproxy *) h;
  36602. DUK__COMMA(); duk_fb_sprintf(fb, "__target:");
  36603. duk__print_hobject(st, p->target);
  36604. DUK__COMMA(); duk_fb_sprintf(fb, "__handler:");
  36605. duk__print_hobject(st, p->handler);
  36606. } else if (st->internal && DUK_HOBJECT_IS_THREAD(h)) {
  36607. duk_hthread *t = (duk_hthread *) h;
  36608. DUK__COMMA(); duk_fb_sprintf(fb, "__ptr_curr_pc:%p", (void *) t->ptr_curr_pc);
  36609. DUK__COMMA(); duk_fb_sprintf(fb, "__heap:%p", (void *) t->heap);
  36610. DUK__COMMA(); duk_fb_sprintf(fb, "__strict:%ld", (long) t->strict);
  36611. DUK__COMMA(); duk_fb_sprintf(fb, "__state:%ld", (long) t->state);
  36612. DUK__COMMA(); duk_fb_sprintf(fb, "__unused1:%ld", (long) t->unused1);
  36613. DUK__COMMA(); duk_fb_sprintf(fb, "__unused2:%ld", (long) t->unused2);
  36614. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack:%p", (void *) t->valstack);
  36615. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_end:%p/%ld", (void *) t->valstack_end, (long) (t->valstack_end - t->valstack));
  36616. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_alloc_end:%p/%ld", (void *) t->valstack_alloc_end, (long) (t->valstack_alloc_end - t->valstack));
  36617. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_bottom:%p/%ld", (void *) t->valstack_bottom, (long) (t->valstack_bottom - t->valstack));
  36618. DUK__COMMA(); duk_fb_sprintf(fb, "__valstack_top:%p/%ld", (void *) t->valstack_top, (long) (t->valstack_top - t->valstack));
  36619. DUK__COMMA(); duk_fb_sprintf(fb, "__callstack_curr:%p", (void *) t->callstack_curr);
  36620. DUK__COMMA(); duk_fb_sprintf(fb, "__callstack_top:%ld", (long) t->callstack_top);
  36621. DUK__COMMA(); duk_fb_sprintf(fb, "__callstack_preventcount:%ld", (long) t->callstack_preventcount);
  36622. DUK__COMMA(); duk_fb_sprintf(fb, "__resumer:"); duk__print_hobject(st, (duk_hobject *) t->resumer);
  36623. DUK__COMMA(); duk_fb_sprintf(fb, "__compile_ctx:%p", (void *) t->compile_ctx);
  36624. #if defined(DUK_USE_INTERRUPT_COUNTER)
  36625. DUK__COMMA(); duk_fb_sprintf(fb, "__interrupt_counter:%ld", (long) t->interrupt_counter);
  36626. DUK__COMMA(); duk_fb_sprintf(fb, "__interrupt_init:%ld", (long) t->interrupt_init);
  36627. #endif
  36628. /* XXX: print built-ins array? */
  36629. }
  36630. #if defined(DUK_USE_REFERENCE_COUNTING)
  36631. if (st->internal) {
  36632. DUK__COMMA(); duk_fb_sprintf(fb, "__refcount:%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h));
  36633. }
  36634. #endif
  36635. if (st->internal) {
  36636. DUK__COMMA(); duk_fb_sprintf(fb, "__class:%ld", (long) DUK_HOBJECT_GET_CLASS_NUMBER(h));
  36637. }
  36638. DUK__COMMA(); duk_fb_sprintf(fb, "__heapptr:%p", (void *) h); /* own pointer */
  36639. /* prototype should be last, for readability */
  36640. if (DUK_HOBJECT_GET_PROTOTYPE(NULL, h)) {
  36641. if (st->follow_proto) {
  36642. DUK__COMMA(); duk_fb_put_cstring(fb, "__prototype:"); duk__print_hobject(st, DUK_HOBJECT_GET_PROTOTYPE(NULL, h));
  36643. } else {
  36644. DUK__COMMA(); duk_fb_sprintf(fb, "__prototype:%p", (void *) DUK_HOBJECT_GET_PROTOTYPE(NULL, h));
  36645. }
  36646. }
  36647. duk_fb_put_cstring(fb, brace2);
  36648. #if defined(DUK_USE_HOBJECT_HASH_PART)
  36649. if (st->heavy && DUK_HOBJECT_GET_HSIZE(h) > 0) {
  36650. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LANGLE);
  36651. for (i = 0; i < DUK_HOBJECT_GET_HSIZE(h); i++) {
  36652. duk_uint_t h_idx = DUK_HOBJECT_H_GET_INDEX(NULL, h, i);
  36653. if (i > 0) {
  36654. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_COMMA);
  36655. }
  36656. if (h_idx == DUK_HOBJECT_HASHIDX_UNUSED) {
  36657. duk_fb_sprintf(fb, "u");
  36658. } else if (h_idx == DUK_HOBJECT_HASHIDX_DELETED) {
  36659. duk_fb_sprintf(fb, "d");
  36660. } else {
  36661. duk_fb_sprintf(fb, "%ld", (long) h_idx);
  36662. }
  36663. }
  36664. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RANGLE);
  36665. }
  36666. #endif
  36667. finished:
  36668. st->depth--;
  36669. if (pushed_loopstack) {
  36670. st->loop_stack_index--;
  36671. st->loop_stack[st->loop_stack_index] = NULL;
  36672. }
  36673. }
  36674. DUK_LOCAL void duk__print_hbuffer(duk__dprint_state *st, duk_hbuffer *h) {
  36675. duk_fixedbuffer *fb = st->fb;
  36676. duk_size_t i, n;
  36677. duk_uint8_t *p;
  36678. if (duk_fb_is_full(fb)) {
  36679. return;
  36680. }
  36681. /* terminal type: no depth check */
  36682. if (!h) {
  36683. duk_fb_put_cstring(fb, "NULL");
  36684. return;
  36685. }
  36686. if (DUK_HBUFFER_HAS_DYNAMIC(h)) {
  36687. if (DUK_HBUFFER_HAS_EXTERNAL(h)) {
  36688. duk_hbuffer_external *g = (duk_hbuffer_external *) h;
  36689. duk_fb_sprintf(fb, "buffer:external:%p:%ld",
  36690. (void *) DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(NULL, g),
  36691. (long) DUK_HBUFFER_EXTERNAL_GET_SIZE(g));
  36692. } else {
  36693. duk_hbuffer_dynamic *g = (duk_hbuffer_dynamic *) h;
  36694. duk_fb_sprintf(fb, "buffer:dynamic:%p:%ld",
  36695. (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(NULL, g),
  36696. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(g));
  36697. }
  36698. } else {
  36699. duk_fb_sprintf(fb, "buffer:fixed:%ld", (long) DUK_HBUFFER_GET_SIZE(h));
  36700. }
  36701. #if defined(DUK_USE_REFERENCE_COUNTING)
  36702. duk_fb_sprintf(fb, "/%lu", (unsigned long) DUK_HEAPHDR_GET_REFCOUNT(&h->hdr));
  36703. #endif
  36704. if (st->hexdump) {
  36705. duk_fb_sprintf(fb, "=[");
  36706. n = DUK_HBUFFER_GET_SIZE(h);
  36707. p = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(NULL, h);
  36708. for (i = 0; i < n; i++) {
  36709. duk_fb_sprintf(fb, "%02lx", (unsigned long) p[i]);
  36710. }
  36711. duk_fb_sprintf(fb, "]");
  36712. }
  36713. }
  36714. DUK_LOCAL void duk__print_heaphdr(duk__dprint_state *st, duk_heaphdr *h) {
  36715. duk_fixedbuffer *fb = st->fb;
  36716. if (duk_fb_is_full(fb)) {
  36717. return;
  36718. }
  36719. if (!h) {
  36720. duk_fb_put_cstring(fb, "NULL");
  36721. return;
  36722. }
  36723. switch (DUK_HEAPHDR_GET_TYPE(h)) {
  36724. case DUK_HTYPE_STRING:
  36725. duk__print_hstring(st, (duk_hstring *) h, 1);
  36726. break;
  36727. case DUK_HTYPE_OBJECT:
  36728. duk__print_hobject(st, (duk_hobject *) h);
  36729. break;
  36730. case DUK_HTYPE_BUFFER:
  36731. duk__print_hbuffer(st, (duk_hbuffer *) h);
  36732. break;
  36733. default:
  36734. duk_fb_sprintf(fb, "[unknown htype %ld]", (long) DUK_HEAPHDR_GET_TYPE(h));
  36735. break;
  36736. }
  36737. }
  36738. DUK_LOCAL void duk__print_tval(duk__dprint_state *st, duk_tval *tv) {
  36739. duk_fixedbuffer *fb = st->fb;
  36740. if (duk_fb_is_full(fb)) {
  36741. return;
  36742. }
  36743. /* depth check is done when printing an actual type */
  36744. if (st->heavy) {
  36745. duk_fb_sprintf(fb, "(%p)", (void *) tv);
  36746. }
  36747. if (!tv) {
  36748. duk_fb_put_cstring(fb, "NULL");
  36749. return;
  36750. }
  36751. if (st->binary) {
  36752. duk_size_t i;
  36753. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LBRACKET);
  36754. for (i = 0; i < (duk_size_t) sizeof(*tv); i++) {
  36755. duk_fb_sprintf(fb, "%02lx", (unsigned long) ((duk_uint8_t *)tv)[i]);
  36756. }
  36757. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RBRACKET);
  36758. }
  36759. if (st->heavy) {
  36760. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_LANGLE);
  36761. }
  36762. switch (DUK_TVAL_GET_TAG(tv)) {
  36763. case DUK_TAG_UNDEFINED: {
  36764. duk_fb_put_cstring(fb, "undefined");
  36765. break;
  36766. }
  36767. case DUK_TAG_UNUSED: {
  36768. duk_fb_put_cstring(fb, "unused");
  36769. break;
  36770. }
  36771. case DUK_TAG_NULL: {
  36772. duk_fb_put_cstring(fb, "null");
  36773. break;
  36774. }
  36775. case DUK_TAG_BOOLEAN: {
  36776. duk_fb_put_cstring(fb, DUK_TVAL_GET_BOOLEAN(tv) ? "true" : "false");
  36777. break;
  36778. }
  36779. case DUK_TAG_STRING: {
  36780. /* Note: string is a terminal heap object, so no depth check here */
  36781. duk__print_hstring(st, DUK_TVAL_GET_STRING(tv), 1);
  36782. break;
  36783. }
  36784. case DUK_TAG_OBJECT: {
  36785. duk__print_hobject(st, DUK_TVAL_GET_OBJECT(tv));
  36786. break;
  36787. }
  36788. case DUK_TAG_BUFFER: {
  36789. duk__print_hbuffer(st, DUK_TVAL_GET_BUFFER(tv));
  36790. break;
  36791. }
  36792. case DUK_TAG_POINTER: {
  36793. duk_fb_sprintf(fb, "pointer:%p", (void *) DUK_TVAL_GET_POINTER(tv));
  36794. break;
  36795. }
  36796. case DUK_TAG_LIGHTFUNC: {
  36797. duk_c_function func;
  36798. duk_small_uint_t lf_flags;
  36799. DUK_TVAL_GET_LIGHTFUNC(tv, func, lf_flags);
  36800. duk_fb_sprintf(fb, "lightfunc:");
  36801. duk_fb_put_funcptr(fb, (duk_uint8_t *) &func, sizeof(func));
  36802. duk_fb_sprintf(fb, ":%04lx", (long) lf_flags);
  36803. break;
  36804. }
  36805. #if defined(DUK_USE_FASTINT)
  36806. case DUK_TAG_FASTINT:
  36807. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  36808. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  36809. duk_fb_sprintf(fb, "%.18g_F", (double) DUK_TVAL_GET_NUMBER(tv));
  36810. break;
  36811. #endif
  36812. default: {
  36813. /* IEEE double is approximately 16 decimal digits; print a couple extra */
  36814. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  36815. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  36816. duk_fb_sprintf(fb, "%.18g", (double) DUK_TVAL_GET_NUMBER(tv));
  36817. break;
  36818. }
  36819. }
  36820. if (st->heavy) {
  36821. duk_fb_put_byte(fb, (duk_uint8_t) DUK_ASC_RANGLE);
  36822. }
  36823. }
  36824. DUK_LOCAL void duk__print_instr(duk__dprint_state *st, duk_instr_t ins) {
  36825. duk_fixedbuffer *fb = st->fb;
  36826. duk_small_int_t op;
  36827. const char *op_name;
  36828. op = (duk_small_int_t) DUK_DEC_OP(ins);
  36829. op_name = duk__bc_optab[op];
  36830. /* XXX: option to fix opcode length so it lines up nicely */
  36831. if (op == DUK_OP_JUMP) {
  36832. duk_int_t diff1 = (duk_int_t) (DUK_DEC_ABC(ins) - DUK_BC_JUMP_BIAS); /* from next pc */
  36833. duk_int_t diff2 = diff1 + 1; /* from curr pc */
  36834. duk_fb_sprintf(fb, "%s %ld (to pc%c%ld)",
  36835. (const char *) op_name, (long) diff1,
  36836. (int) (diff2 >= 0 ? '+' : '-'), /* char format: use int */
  36837. (long) (diff2 >= 0 ? diff2 : -diff2));
  36838. } else {
  36839. duk_fb_sprintf(fb, "%s %ld, %ld, %ld",
  36840. (const char *) op_name, (long) DUK_DEC_A(ins),
  36841. (long) DUK_DEC_B(ins), (long) DUK_DEC_C(ins));
  36842. }
  36843. }
  36844. DUK_LOCAL void duk__print_opcode(duk__dprint_state *st, duk_small_int_t opcode) {
  36845. duk_fixedbuffer *fb = st->fb;
  36846. if (opcode < DUK_BC_OP_MIN || opcode > DUK_BC_OP_MAX) {
  36847. duk_fb_sprintf(fb, "?(%ld)", (long) opcode);
  36848. } else {
  36849. duk_fb_sprintf(fb, "%s", (const char *) duk__bc_optab[opcode]);
  36850. }
  36851. }
  36852. DUK_INTERNAL duk_int_t duk_debug_vsnprintf(char *str, duk_size_t size, const char *format, va_list ap) {
  36853. duk_fixedbuffer fb;
  36854. const char *p = format;
  36855. const char *p_end = p + DUK_STRLEN(format);
  36856. duk_int_t retval;
  36857. duk_memzero(&fb, sizeof(fb));
  36858. fb.buffer = (duk_uint8_t *) str;
  36859. fb.length = size;
  36860. fb.offset = 0;
  36861. fb.truncated = 0;
  36862. while (p < p_end) {
  36863. char ch = *p++;
  36864. const char *p_begfmt = NULL;
  36865. duk_bool_t got_exclamation = 0;
  36866. duk_bool_t got_long = 0; /* %lf, %ld etc */
  36867. duk__dprint_state st;
  36868. if (ch != DUK_ASC_PERCENT) {
  36869. duk_fb_put_byte(&fb, (duk_uint8_t) ch);
  36870. continue;
  36871. }
  36872. /*
  36873. * Format tag parsing. Since we don't understand all the
  36874. * possible format tags allowed, we just scan for a terminating
  36875. * specifier and keep track of relevant modifiers that we do
  36876. * understand. See man 3 printf.
  36877. */
  36878. duk_memzero(&st, sizeof(st));
  36879. st.fb = &fb;
  36880. st.depth = 0;
  36881. st.depth_limit = 1;
  36882. st.loop_stack_index = 0;
  36883. st.loop_stack_limit = DUK__LOOP_STACK_DEPTH;
  36884. p_begfmt = p - 1;
  36885. while (p < p_end) {
  36886. ch = *p++;
  36887. if (ch == DUK_ASC_STAR) {
  36888. /* unsupported: would consume multiple args */
  36889. goto format_error;
  36890. } else if (ch == DUK_ASC_PERCENT) {
  36891. duk_fb_put_byte(&fb, (duk_uint8_t) DUK_ASC_PERCENT);
  36892. break;
  36893. } else if (ch == DUK_ASC_EXCLAMATION) {
  36894. got_exclamation = 1;
  36895. } else if (!got_exclamation && ch == DUK_ASC_LC_L) {
  36896. got_long = 1;
  36897. } else if (got_exclamation && ch == DUK_ASC_LC_D) {
  36898. st.depth_limit = DUK__DEEP_DEPTH_LIMIT;
  36899. } else if (got_exclamation && ch == DUK_ASC_LC_P) {
  36900. st.follow_proto = 1;
  36901. } else if (got_exclamation && ch == DUK_ASC_LC_I) {
  36902. st.internal = 1;
  36903. } else if (got_exclamation && ch == DUK_ASC_LC_X) {
  36904. st.hexdump = 1;
  36905. } else if (got_exclamation && ch == DUK_ASC_LC_H) {
  36906. st.heavy = 1;
  36907. } else if (got_exclamation && ch == DUK_ASC_ATSIGN) {
  36908. st.pointer = 1;
  36909. } else if (got_exclamation && ch == DUK_ASC_HASH) {
  36910. st.binary = 1;
  36911. } else if (got_exclamation && ch == DUK_ASC_UC_T) {
  36912. duk_tval *t = va_arg(ap, duk_tval *);
  36913. if (st.pointer && !st.heavy) {
  36914. duk_fb_sprintf(&fb, "(%p)", (void *) t);
  36915. }
  36916. duk__print_tval(&st, t);
  36917. break;
  36918. } else if (got_exclamation && ch == DUK_ASC_UC_O) {
  36919. duk_heaphdr *t = va_arg(ap, duk_heaphdr *);
  36920. if (st.pointer && !st.heavy) {
  36921. duk_fb_sprintf(&fb, "(%p)", (void *) t);
  36922. }
  36923. duk__print_heaphdr(&st, t);
  36924. break;
  36925. } else if (got_exclamation && ch == DUK_ASC_UC_I) {
  36926. duk_instr_t t = va_arg(ap, duk_instr_t);
  36927. duk__print_instr(&st, t);
  36928. break;
  36929. } else if (got_exclamation && ch == DUK_ASC_UC_C) {
  36930. long t = va_arg(ap, long);
  36931. duk__print_opcode(&st, (duk_small_int_t) t);
  36932. break;
  36933. } else if (!got_exclamation && strchr(DUK__ALLOWED_STANDARD_SPECIFIERS, (int) ch)) {
  36934. char fmtbuf[DUK__MAX_FORMAT_TAG_LENGTH];
  36935. duk_size_t fmtlen;
  36936. DUK_ASSERT(p >= p_begfmt);
  36937. fmtlen = (duk_size_t) (p - p_begfmt);
  36938. if (fmtlen >= sizeof(fmtbuf)) {
  36939. /* format is too large, abort */
  36940. goto format_error;
  36941. }
  36942. duk_memzero(fmtbuf, sizeof(fmtbuf));
  36943. duk_memcpy(fmtbuf, p_begfmt, fmtlen);
  36944. /* assume exactly 1 arg, which is why '*' is forbidden; arg size still
  36945. * depends on type though.
  36946. */
  36947. if (ch == DUK_ASC_LC_F || ch == DUK_ASC_LC_G || ch == DUK_ASC_LC_E) {
  36948. /* %f and %lf both consume a 'long' */
  36949. double arg = va_arg(ap, double);
  36950. duk_fb_sprintf(&fb, fmtbuf, arg);
  36951. } else if (ch == DUK_ASC_LC_D && got_long) {
  36952. /* %ld */
  36953. long arg = va_arg(ap, long);
  36954. duk_fb_sprintf(&fb, fmtbuf, arg);
  36955. } else if (ch == DUK_ASC_LC_D) {
  36956. /* %d; only 16 bits are guaranteed */
  36957. int arg = va_arg(ap, int);
  36958. duk_fb_sprintf(&fb, fmtbuf, arg);
  36959. } else if (ch == DUK_ASC_LC_U && got_long) {
  36960. /* %lu */
  36961. unsigned long arg = va_arg(ap, unsigned long);
  36962. duk_fb_sprintf(&fb, fmtbuf, arg);
  36963. } else if (ch == DUK_ASC_LC_U) {
  36964. /* %u; only 16 bits are guaranteed */
  36965. unsigned int arg = va_arg(ap, unsigned int);
  36966. duk_fb_sprintf(&fb, fmtbuf, arg);
  36967. } else if (ch == DUK_ASC_LC_X && got_long) {
  36968. /* %lx */
  36969. unsigned long arg = va_arg(ap, unsigned long);
  36970. duk_fb_sprintf(&fb, fmtbuf, arg);
  36971. } else if (ch == DUK_ASC_LC_X) {
  36972. /* %x; only 16 bits are guaranteed */
  36973. unsigned int arg = va_arg(ap, unsigned int);
  36974. duk_fb_sprintf(&fb, fmtbuf, arg);
  36975. } else if (ch == DUK_ASC_LC_S) {
  36976. /* %s */
  36977. const char *arg = va_arg(ap, const char *);
  36978. if (arg == NULL) {
  36979. /* '%s' and NULL is not portable, so special case
  36980. * it for debug printing.
  36981. */
  36982. duk_fb_sprintf(&fb, "NULL");
  36983. } else {
  36984. duk_fb_sprintf(&fb, fmtbuf, arg);
  36985. }
  36986. } else if (ch == DUK_ASC_LC_P) {
  36987. /* %p */
  36988. void *arg = va_arg(ap, void *);
  36989. if (arg == NULL) {
  36990. /* '%p' and NULL is portable, but special case it
  36991. * anyway to get a standard NULL marker in logs.
  36992. */
  36993. duk_fb_sprintf(&fb, "NULL");
  36994. } else {
  36995. duk_fb_sprintf(&fb, fmtbuf, arg);
  36996. }
  36997. } else if (ch == DUK_ASC_LC_C) {
  36998. /* '%c', passed concretely as int */
  36999. int arg = va_arg(ap, int);
  37000. duk_fb_sprintf(&fb, fmtbuf, arg);
  37001. } else {
  37002. /* Should not happen. */
  37003. duk_fb_sprintf(&fb, "INVALID-FORMAT(%s)", (const char *) fmtbuf);
  37004. }
  37005. break;
  37006. } else {
  37007. /* ignore */
  37008. }
  37009. }
  37010. }
  37011. goto done;
  37012. format_error:
  37013. duk_fb_put_cstring(&fb, "FMTERR");
  37014. /* fall through */
  37015. done:
  37016. retval = (duk_int_t) fb.offset;
  37017. duk_fb_put_byte(&fb, (duk_uint8_t) 0);
  37018. /* return total chars written excluding terminator */
  37019. return retval;
  37020. }
  37021. #if 0 /*unused*/
  37022. DUK_INTERNAL duk_int_t duk_debug_snprintf(char *str, duk_size_t size, const char *format, ...) {
  37023. duk_int_t retval;
  37024. va_list ap;
  37025. va_start(ap, format);
  37026. retval = duk_debug_vsnprintf(str, size, format, ap);
  37027. va_end(ap);
  37028. return retval;
  37029. }
  37030. #endif
  37031. /* Formatting function pointers is tricky: there is no standard pointer for
  37032. * function pointers and the size of a function pointer may depend on the
  37033. * specific pointer type. This helper formats a function pointer based on
  37034. * its memory layout to get something useful on most platforms.
  37035. */
  37036. DUK_INTERNAL void duk_debug_format_funcptr(char *buf, duk_size_t buf_size, duk_uint8_t *fptr, duk_size_t fptr_size) {
  37037. duk_size_t i;
  37038. duk_uint8_t *p = (duk_uint8_t *) buf;
  37039. duk_uint8_t *p_end = (duk_uint8_t *) (buf + buf_size - 1);
  37040. DUK_ASSERT(buf != NULL);
  37041. duk_memzero(buf, buf_size);
  37042. for (i = 0; i < fptr_size; i++) {
  37043. duk_int_t left = (duk_int_t) (p_end - p);
  37044. duk_uint8_t ch;
  37045. if (left <= 0) {
  37046. break;
  37047. }
  37048. /* Quite approximate but should be useful for little and big endian. */
  37049. #if defined(DUK_USE_INTEGER_BE)
  37050. ch = fptr[i];
  37051. #else
  37052. ch = fptr[fptr_size - 1 - i];
  37053. #endif
  37054. p += DUK_SNPRINTF((char *) p, (duk_size_t) left, "%02lx", (unsigned long) ch);
  37055. }
  37056. }
  37057. #endif /* DUK_USE_DEBUG */
  37058. /* automatic undefs */
  37059. #undef DUK__ALLOWED_STANDARD_SPECIFIERS
  37060. #undef DUK__COMMA
  37061. #undef DUK__DEEP_DEPTH_LIMIT
  37062. #undef DUK__LOOP_STACK_DEPTH
  37063. #undef DUK__MAX_FORMAT_TAG_LENGTH
  37064. #line 1 "duk_debugger.c"
  37065. /*
  37066. * Duktape debugger
  37067. */
  37068. /* #include duk_internal.h -> already included */
  37069. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  37070. /*
  37071. * Assert helpers
  37072. */
  37073. #if defined(DUK_USE_ASSERTIONS)
  37074. #define DUK__DBG_TPORT_ENTER() do { \
  37075. DUK_ASSERT(heap->dbg_calling_transport == 0); \
  37076. heap->dbg_calling_transport = 1; \
  37077. } while (0)
  37078. #define DUK__DBG_TPORT_EXIT() do { \
  37079. DUK_ASSERT(heap->dbg_calling_transport == 1); \
  37080. heap->dbg_calling_transport = 0; \
  37081. } while (0)
  37082. #else
  37083. #define DUK__DBG_TPORT_ENTER() do {} while (0)
  37084. #define DUK__DBG_TPORT_EXIT() do {} while (0)
  37085. #endif
  37086. /*
  37087. * Helper structs
  37088. */
  37089. typedef union {
  37090. void *p;
  37091. duk_uint_t b[1];
  37092. /* Use b[] to access the size of the union, which is strictly not
  37093. * correct. Can't use fixed size unless there's feature detection
  37094. * for pointer byte size.
  37095. */
  37096. } duk__ptr_union;
  37097. /*
  37098. * Detach handling
  37099. */
  37100. #define DUK__SET_CONN_BROKEN(thr,reason) do { \
  37101. /* For now shared handler is fine. */ \
  37102. duk__debug_do_detach1((thr)->heap, (reason)); \
  37103. } while (0)
  37104. DUK_LOCAL void duk__debug_do_detach1(duk_heap *heap, duk_int_t reason) {
  37105. /* Can be called multiple times with no harm. Mark the transport
  37106. * bad (dbg_read_cb == NULL) and clear state except for the detached
  37107. * callback and the udata field. The detached callback is delayed
  37108. * to the message loop so that it can be called between messages;
  37109. * this avoids corner cases related to immediate debugger reattach
  37110. * inside the detached callback.
  37111. */
  37112. if (heap->dbg_detaching) {
  37113. DUK_D(DUK_DPRINT("debugger already detaching, ignore detach1"));
  37114. return;
  37115. }
  37116. DUK_D(DUK_DPRINT("debugger transport detaching, marking transport broken"));
  37117. heap->dbg_detaching = 1; /* prevent multiple in-progress detaches */
  37118. if (heap->dbg_write_cb != NULL) {
  37119. duk_hthread *thr;
  37120. thr = heap->heap_thread;
  37121. DUK_ASSERT(thr != NULL);
  37122. duk_debug_write_notify(thr, DUK_DBG_CMD_DETACHING);
  37123. duk_debug_write_int(thr, reason);
  37124. duk_debug_write_eom(thr);
  37125. }
  37126. heap->dbg_read_cb = NULL;
  37127. heap->dbg_write_cb = NULL;
  37128. heap->dbg_peek_cb = NULL;
  37129. heap->dbg_read_flush_cb = NULL;
  37130. heap->dbg_write_flush_cb = NULL;
  37131. heap->dbg_request_cb = NULL;
  37132. /* heap->dbg_detached_cb: keep */
  37133. /* heap->dbg_udata: keep */
  37134. /* heap->dbg_processing: keep on purpose to avoid debugger re-entry in detaching state */
  37135. heap->dbg_state_dirty = 0;
  37136. heap->dbg_force_restart = 0;
  37137. heap->dbg_pause_flags = 0;
  37138. heap->dbg_pause_act = NULL;
  37139. heap->dbg_pause_startline = 0;
  37140. heap->dbg_have_next_byte = 0;
  37141. duk_debug_clear_paused(heap); /* XXX: some overlap with field inits above */
  37142. heap->dbg_state_dirty = 0; /* XXX: clear_paused sets dirty; rework? */
  37143. /* Ensure there are no stale active breakpoint pointers.
  37144. * Breakpoint list is currently kept - we could empty it
  37145. * here but we'd need to handle refcounts correctly, and
  37146. * we'd need a 'thr' reference for that.
  37147. *
  37148. * XXX: clear breakpoint on either attach or detach?
  37149. */
  37150. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  37151. }
  37152. DUK_LOCAL void duk__debug_do_detach2(duk_heap *heap) {
  37153. duk_debug_detached_function detached_cb;
  37154. void *detached_udata;
  37155. duk_hthread *thr;
  37156. thr = heap->heap_thread;
  37157. if (thr == NULL) {
  37158. DUK_ASSERT(heap->dbg_detached_cb == NULL);
  37159. return;
  37160. }
  37161. /* Safe to call multiple times. */
  37162. detached_cb = heap->dbg_detached_cb;
  37163. detached_udata = heap->dbg_udata;
  37164. heap->dbg_detached_cb = NULL;
  37165. heap->dbg_udata = NULL;
  37166. if (detached_cb) {
  37167. /* Careful here: state must be wiped before the call
  37168. * so that we can cleanly handle a re-attach from
  37169. * inside the callback.
  37170. */
  37171. DUK_D(DUK_DPRINT("detached during message loop, delayed call to detached_cb"));
  37172. detached_cb(thr, detached_udata);
  37173. }
  37174. heap->dbg_detaching = 0;
  37175. }
  37176. DUK_INTERNAL void duk_debug_do_detach(duk_heap *heap) {
  37177. duk__debug_do_detach1(heap, 0);
  37178. duk__debug_do_detach2(heap);
  37179. }
  37180. /* Called on a read/write error: NULL all callbacks except the detached
  37181. * callback so that we never accidentally call them after a read/write
  37182. * error has been indicated. This is especially important for the transport
  37183. * I/O callbacks to fulfill guaranteed callback semantics.
  37184. */
  37185. DUK_LOCAL void duk__debug_null_most_callbacks(duk_hthread *thr) {
  37186. duk_heap *heap;
  37187. DUK_ASSERT(thr != NULL);
  37188. heap = thr->heap;
  37189. DUK_D(DUK_DPRINT("transport read/write error, NULL all callbacks expected detached"));
  37190. heap->dbg_read_cb = NULL;
  37191. heap->dbg_write_cb = NULL; /* this is especially critical to avoid another write call in detach1() */
  37192. heap->dbg_peek_cb = NULL;
  37193. heap->dbg_read_flush_cb = NULL;
  37194. heap->dbg_write_flush_cb = NULL;
  37195. heap->dbg_request_cb = NULL;
  37196. /* keep heap->dbg_detached_cb */
  37197. }
  37198. /*
  37199. * Pause handling
  37200. */
  37201. DUK_LOCAL void duk__debug_set_pause_state(duk_hthread *thr, duk_heap *heap, duk_small_uint_t pause_flags) {
  37202. duk_uint_fast32_t line;
  37203. line = duk_debug_curr_line(thr);
  37204. if (line == 0) {
  37205. /* No line info for current function. */
  37206. duk_small_uint_t updated_flags;
  37207. updated_flags = pause_flags & ~(DUK_PAUSE_FLAG_LINE_CHANGE);
  37208. DUK_D(DUK_DPRINT("no line info for current activation, disable line-based pause flags: 0x%08lx -> 0x%08lx",
  37209. (long) pause_flags, (long) updated_flags));
  37210. pause_flags = updated_flags;
  37211. }
  37212. heap->dbg_pause_flags = pause_flags;
  37213. heap->dbg_pause_act = thr->callstack_curr;
  37214. heap->dbg_pause_startline = (duk_uint32_t) line;
  37215. heap->dbg_state_dirty = 1;
  37216. DUK_D(DUK_DPRINT("set state for automatic pause triggers, flags=0x%08lx, act=%p, startline=%ld",
  37217. (long) heap->dbg_pause_flags, (void *) heap->dbg_pause_act,
  37218. (long) heap->dbg_pause_startline));
  37219. }
  37220. /*
  37221. * Debug connection peek and flush primitives
  37222. */
  37223. DUK_INTERNAL duk_bool_t duk_debug_read_peek(duk_hthread *thr) {
  37224. duk_heap *heap;
  37225. duk_bool_t ret;
  37226. DUK_ASSERT(thr != NULL);
  37227. heap = thr->heap;
  37228. DUK_ASSERT(heap != NULL);
  37229. if (heap->dbg_read_cb == NULL) {
  37230. DUK_D(DUK_DPRINT("attempt to peek in detached state, return zero (= no data)"));
  37231. return 0;
  37232. }
  37233. if (heap->dbg_peek_cb == NULL) {
  37234. DUK_DD(DUK_DDPRINT("no peek callback, return zero (= no data)"));
  37235. return 0;
  37236. }
  37237. DUK__DBG_TPORT_ENTER();
  37238. ret = (duk_bool_t) (heap->dbg_peek_cb(heap->dbg_udata) > 0);
  37239. DUK__DBG_TPORT_EXIT();
  37240. return ret;
  37241. }
  37242. DUK_INTERNAL void duk_debug_read_flush(duk_hthread *thr) {
  37243. duk_heap *heap;
  37244. DUK_ASSERT(thr != NULL);
  37245. heap = thr->heap;
  37246. DUK_ASSERT(heap != NULL);
  37247. if (heap->dbg_read_cb == NULL) {
  37248. DUK_D(DUK_DPRINT("attempt to read flush in detached state, ignore"));
  37249. return;
  37250. }
  37251. if (heap->dbg_read_flush_cb == NULL) {
  37252. DUK_DD(DUK_DDPRINT("no read flush callback, ignore"));
  37253. return;
  37254. }
  37255. DUK__DBG_TPORT_ENTER();
  37256. heap->dbg_read_flush_cb(heap->dbg_udata);
  37257. DUK__DBG_TPORT_EXIT();
  37258. }
  37259. DUK_INTERNAL void duk_debug_write_flush(duk_hthread *thr) {
  37260. duk_heap *heap;
  37261. DUK_ASSERT(thr != NULL);
  37262. heap = thr->heap;
  37263. DUK_ASSERT(heap != NULL);
  37264. if (heap->dbg_read_cb == NULL) {
  37265. DUK_D(DUK_DPRINT("attempt to write flush in detached state, ignore"));
  37266. return;
  37267. }
  37268. if (heap->dbg_write_flush_cb == NULL) {
  37269. DUK_DD(DUK_DDPRINT("no write flush callback, ignore"));
  37270. return;
  37271. }
  37272. DUK__DBG_TPORT_ENTER();
  37273. heap->dbg_write_flush_cb(heap->dbg_udata);
  37274. DUK__DBG_TPORT_EXIT();
  37275. }
  37276. /*
  37277. * Debug connection skip primitives
  37278. */
  37279. /* Skip fully. */
  37280. DUK_INTERNAL void duk_debug_skip_bytes(duk_hthread *thr, duk_size_t length) {
  37281. duk_uint8_t dummy[64];
  37282. duk_size_t now;
  37283. DUK_ASSERT(thr != NULL);
  37284. while (length > 0) {
  37285. now = (length > sizeof(dummy) ? sizeof(dummy) : length);
  37286. duk_debug_read_bytes(thr, dummy, now);
  37287. length -= now;
  37288. }
  37289. }
  37290. DUK_INTERNAL void duk_debug_skip_byte(duk_hthread *thr) {
  37291. DUK_ASSERT(thr != NULL);
  37292. (void) duk_debug_read_byte(thr);
  37293. }
  37294. /*
  37295. * Debug connection read primitives
  37296. */
  37297. /* Peek ahead in the stream one byte. */
  37298. DUK_INTERNAL uint8_t duk_debug_peek_byte(duk_hthread *thr) {
  37299. /* It is important not to call this if the last byte read was an EOM.
  37300. * Reading ahead in this scenario would cause unnecessary blocking if
  37301. * another message is not available.
  37302. */
  37303. duk_uint8_t x;
  37304. x = duk_debug_read_byte(thr);
  37305. thr->heap->dbg_have_next_byte = 1;
  37306. thr->heap->dbg_next_byte = x;
  37307. return x;
  37308. }
  37309. /* Read fully. */
  37310. DUK_INTERNAL void duk_debug_read_bytes(duk_hthread *thr, duk_uint8_t *data, duk_size_t length) {
  37311. duk_heap *heap;
  37312. duk_uint8_t *p;
  37313. duk_size_t left;
  37314. duk_size_t got;
  37315. DUK_ASSERT(thr != NULL);
  37316. heap = thr->heap;
  37317. DUK_ASSERT(heap != NULL);
  37318. DUK_ASSERT(data != NULL);
  37319. if (heap->dbg_read_cb == NULL) {
  37320. DUK_D(DUK_DPRINT("attempt to read %ld bytes in detached state, return zero data", (long) length));
  37321. goto fail;
  37322. }
  37323. /* NOTE: length may be zero */
  37324. p = data;
  37325. if (length >= 1 && heap->dbg_have_next_byte) {
  37326. heap->dbg_have_next_byte = 0;
  37327. *p++ = heap->dbg_next_byte;
  37328. }
  37329. for (;;) {
  37330. left = (duk_size_t) ((data + length) - p);
  37331. if (left == 0) {
  37332. break;
  37333. }
  37334. DUK_ASSERT(heap->dbg_read_cb != NULL);
  37335. DUK_ASSERT(left >= 1);
  37336. #if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
  37337. left = 1;
  37338. #endif
  37339. DUK__DBG_TPORT_ENTER();
  37340. got = heap->dbg_read_cb(heap->dbg_udata, (char *) p, left);
  37341. DUK__DBG_TPORT_EXIT();
  37342. if (got == 0 || got > left) {
  37343. DUK_D(DUK_DPRINT("connection error during read, return zero data"));
  37344. duk__debug_null_most_callbacks(thr); /* avoid calling write callback in detach1() */
  37345. DUK__SET_CONN_BROKEN(thr, 1);
  37346. goto fail;
  37347. }
  37348. p += got;
  37349. }
  37350. return;
  37351. fail:
  37352. duk_memzero((void *) data, (size_t) length);
  37353. }
  37354. DUK_INTERNAL duk_uint8_t duk_debug_read_byte(duk_hthread *thr) {
  37355. duk_uint8_t x;
  37356. x = 0; /* just in case callback is broken and won't write 'x' */
  37357. duk_debug_read_bytes(thr, &x, 1);
  37358. return x;
  37359. }
  37360. DUK_LOCAL duk_uint32_t duk__debug_read_uint32_raw(duk_hthread *thr) {
  37361. duk_uint8_t buf[4];
  37362. DUK_ASSERT(thr != NULL);
  37363. duk_debug_read_bytes(thr, buf, 4);
  37364. return ((duk_uint32_t) buf[0] << 24) |
  37365. ((duk_uint32_t) buf[1] << 16) |
  37366. ((duk_uint32_t) buf[2] << 8) |
  37367. (duk_uint32_t) buf[3];
  37368. }
  37369. DUK_LOCAL duk_int32_t duk__debug_read_int32_raw(duk_hthread *thr) {
  37370. return (duk_int32_t) duk__debug_read_uint32_raw(thr);
  37371. }
  37372. DUK_LOCAL duk_uint16_t duk__debug_read_uint16_raw(duk_hthread *thr) {
  37373. duk_uint8_t buf[2];
  37374. DUK_ASSERT(thr != NULL);
  37375. duk_debug_read_bytes(thr, buf, 2);
  37376. return ((duk_uint16_t) buf[0] << 8) |
  37377. (duk_uint16_t) buf[1];
  37378. }
  37379. DUK_INTERNAL duk_int32_t duk_debug_read_int(duk_hthread *thr) {
  37380. duk_small_uint_t x;
  37381. duk_small_uint_t t;
  37382. DUK_ASSERT(thr != NULL);
  37383. x = duk_debug_read_byte(thr);
  37384. if (x >= 0xc0) {
  37385. t = duk_debug_read_byte(thr);
  37386. return (duk_int32_t) (((x - 0xc0) << 8) + t);
  37387. } else if (x >= 0x80) {
  37388. return (duk_int32_t) (x - 0x80);
  37389. } else if (x == DUK_DBG_IB_INT4) {
  37390. return (duk_int32_t) duk__debug_read_uint32_raw(thr);
  37391. }
  37392. DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
  37393. DUK__SET_CONN_BROKEN(thr, 1);
  37394. return 0;
  37395. }
  37396. DUK_LOCAL duk_hstring *duk__debug_read_hstring_raw(duk_hthread *thr, duk_uint32_t len) {
  37397. duk_uint8_t buf[31];
  37398. duk_uint8_t *p;
  37399. if (len <= sizeof(buf)) {
  37400. duk_debug_read_bytes(thr, buf, (duk_size_t) len);
  37401. duk_push_lstring(thr, (const char *) buf, (duk_size_t) len);
  37402. } else {
  37403. p = (duk_uint8_t *) duk_push_fixed_buffer(thr, (duk_size_t) len); /* zero for paranoia */
  37404. DUK_ASSERT(p != NULL);
  37405. duk_debug_read_bytes(thr, p, (duk_size_t) len);
  37406. (void) duk_buffer_to_string(thr, -1); /* Safety relies on debug client, which is OK. */
  37407. }
  37408. return duk_require_hstring(thr, -1);
  37409. }
  37410. DUK_INTERNAL duk_hstring *duk_debug_read_hstring(duk_hthread *thr) {
  37411. duk_small_uint_t x;
  37412. duk_uint32_t len;
  37413. DUK_ASSERT(thr != NULL);
  37414. x = duk_debug_read_byte(thr);
  37415. if (x >= 0x60 && x <= 0x7f) {
  37416. /* For short strings, use a fixed temp buffer. */
  37417. len = (duk_uint32_t) (x - 0x60);
  37418. } else if (x == DUK_DBG_IB_STR2) {
  37419. len = (duk_uint32_t) duk__debug_read_uint16_raw(thr);
  37420. } else if (x == DUK_DBG_IB_STR4) {
  37421. len = (duk_uint32_t) duk__debug_read_uint32_raw(thr);
  37422. } else {
  37423. goto fail;
  37424. }
  37425. return duk__debug_read_hstring_raw(thr, len);
  37426. fail:
  37427. DUK_D(DUK_DPRINT("debug connection error: failed to decode int"));
  37428. DUK__SET_CONN_BROKEN(thr, 1);
  37429. duk_push_hstring_empty(thr); /* always push some string */
  37430. return duk_require_hstring(thr, -1);
  37431. }
  37432. DUK_LOCAL duk_hbuffer *duk__debug_read_hbuffer_raw(duk_hthread *thr, duk_uint32_t len) {
  37433. duk_uint8_t *p;
  37434. p = (duk_uint8_t *) duk_push_fixed_buffer(thr, (duk_size_t) len); /* zero for paranoia */
  37435. DUK_ASSERT(p != NULL);
  37436. duk_debug_read_bytes(thr, p, (duk_size_t) len);
  37437. return duk_require_hbuffer(thr, -1);
  37438. }
  37439. DUK_LOCAL void *duk__debug_read_pointer_raw(duk_hthread *thr) {
  37440. duk_small_uint_t x;
  37441. duk__ptr_union pu;
  37442. DUK_ASSERT(thr != NULL);
  37443. x = duk_debug_read_byte(thr);
  37444. if (x != sizeof(pu)) {
  37445. goto fail;
  37446. }
  37447. duk_debug_read_bytes(thr, (duk_uint8_t *) &pu.p, sizeof(pu));
  37448. #if defined(DUK_USE_INTEGER_LE)
  37449. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  37450. #endif
  37451. return (void *) pu.p;
  37452. fail:
  37453. DUK_D(DUK_DPRINT("debug connection error: failed to decode pointer"));
  37454. DUK__SET_CONN_BROKEN(thr, 1);
  37455. return (void *) NULL;
  37456. }
  37457. DUK_LOCAL duk_double_t duk__debug_read_double_raw(duk_hthread *thr) {
  37458. duk_double_union du;
  37459. DUK_ASSERT(sizeof(du.uc) == 8);
  37460. duk_debug_read_bytes(thr, (duk_uint8_t *) du.uc, sizeof(du.uc));
  37461. DUK_DBLUNION_DOUBLE_NTOH(&du);
  37462. return du.d;
  37463. }
  37464. #if 0
  37465. DUK_INTERNAL duk_heaphdr *duk_debug_read_heapptr(duk_hthread *thr) {
  37466. duk_small_uint_t x;
  37467. DUK_ASSERT(thr != NULL);
  37468. x = duk_debug_read_byte(thr);
  37469. if (x != DUK_DBG_IB_HEAPPTR) {
  37470. goto fail;
  37471. }
  37472. return (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
  37473. fail:
  37474. DUK_D(DUK_DPRINT("debug connection error: failed to decode heapptr"));
  37475. DUK__SET_CONN_BROKEN(thr, 1);
  37476. return NULL;
  37477. }
  37478. #endif
  37479. DUK_INTERNAL duk_heaphdr *duk_debug_read_any_ptr(duk_hthread *thr) {
  37480. duk_small_uint_t x;
  37481. DUK_ASSERT(thr != NULL);
  37482. x = duk_debug_read_byte(thr);
  37483. switch (x) {
  37484. case DUK_DBG_IB_OBJECT:
  37485. case DUK_DBG_IB_POINTER:
  37486. case DUK_DBG_IB_HEAPPTR:
  37487. /* Accept any pointer-like value; for 'object' dvalue, read
  37488. * and ignore the class number.
  37489. */
  37490. if (x == DUK_DBG_IB_OBJECT) {
  37491. duk_debug_skip_byte(thr);
  37492. }
  37493. break;
  37494. default:
  37495. goto fail;
  37496. }
  37497. return (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
  37498. fail:
  37499. DUK_D(DUK_DPRINT("debug connection error: failed to decode any pointer (object, pointer, heapptr)"));
  37500. DUK__SET_CONN_BROKEN(thr, 1);
  37501. return NULL;
  37502. }
  37503. DUK_INTERNAL duk_tval *duk_debug_read_tval(duk_hthread *thr) {
  37504. duk_uint8_t x;
  37505. duk_uint_t t;
  37506. duk_uint32_t len;
  37507. DUK_ASSERT(thr != NULL);
  37508. x = duk_debug_read_byte(thr);
  37509. if (x >= 0xc0) {
  37510. t = (duk_uint_t) (x - 0xc0);
  37511. t = (t << 8) + duk_debug_read_byte(thr);
  37512. duk_push_uint(thr, (duk_uint_t) t);
  37513. goto return_ptr;
  37514. }
  37515. if (x >= 0x80) {
  37516. duk_push_uint(thr, (duk_uint_t) (x - 0x80));
  37517. goto return_ptr;
  37518. }
  37519. if (x >= 0x60) {
  37520. len = (duk_uint32_t) (x - 0x60);
  37521. duk__debug_read_hstring_raw(thr, len);
  37522. goto return_ptr;
  37523. }
  37524. switch (x) {
  37525. case DUK_DBG_IB_INT4: {
  37526. duk_int32_t i = duk__debug_read_int32_raw(thr);
  37527. duk_push_i32(thr, i);
  37528. break;
  37529. }
  37530. case DUK_DBG_IB_STR4: {
  37531. len = duk__debug_read_uint32_raw(thr);
  37532. duk__debug_read_hstring_raw(thr, len);
  37533. break;
  37534. }
  37535. case DUK_DBG_IB_STR2: {
  37536. len = duk__debug_read_uint16_raw(thr);
  37537. duk__debug_read_hstring_raw(thr, len);
  37538. break;
  37539. }
  37540. case DUK_DBG_IB_BUF4: {
  37541. len = duk__debug_read_uint32_raw(thr);
  37542. duk__debug_read_hbuffer_raw(thr, len);
  37543. break;
  37544. }
  37545. case DUK_DBG_IB_BUF2: {
  37546. len = duk__debug_read_uint16_raw(thr);
  37547. duk__debug_read_hbuffer_raw(thr, len);
  37548. break;
  37549. }
  37550. case DUK_DBG_IB_UNDEFINED: {
  37551. duk_push_undefined(thr);
  37552. break;
  37553. }
  37554. case DUK_DBG_IB_NULL: {
  37555. duk_push_null(thr);
  37556. break;
  37557. }
  37558. case DUK_DBG_IB_TRUE: {
  37559. duk_push_true(thr);
  37560. break;
  37561. }
  37562. case DUK_DBG_IB_FALSE: {
  37563. duk_push_false(thr);
  37564. break;
  37565. }
  37566. case DUK_DBG_IB_NUMBER: {
  37567. duk_double_t d;
  37568. d = duk__debug_read_double_raw(thr);
  37569. duk_push_number(thr, d);
  37570. break;
  37571. }
  37572. case DUK_DBG_IB_OBJECT: {
  37573. duk_heaphdr *h;
  37574. duk_debug_skip_byte(thr);
  37575. h = (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
  37576. duk_push_heapptr(thr, (void *) h);
  37577. break;
  37578. }
  37579. case DUK_DBG_IB_POINTER: {
  37580. void *ptr;
  37581. ptr = duk__debug_read_pointer_raw(thr);
  37582. duk_push_pointer(thr, ptr);
  37583. break;
  37584. }
  37585. case DUK_DBG_IB_LIGHTFUNC: {
  37586. /* XXX: Not needed for now, so not implemented. Note that
  37587. * function pointers may have different size/layout than
  37588. * a void pointer.
  37589. */
  37590. DUK_D(DUK_DPRINT("reading lightfunc values unimplemented"));
  37591. goto fail;
  37592. }
  37593. case DUK_DBG_IB_HEAPPTR: {
  37594. duk_heaphdr *h;
  37595. h = (duk_heaphdr *) duk__debug_read_pointer_raw(thr);
  37596. duk_push_heapptr(thr, (void *) h);
  37597. break;
  37598. }
  37599. case DUK_DBG_IB_UNUSED: /* unused: not accepted in inbound messages */
  37600. default:
  37601. goto fail;
  37602. }
  37603. return_ptr:
  37604. return DUK_GET_TVAL_NEGIDX(thr, -1);
  37605. fail:
  37606. DUK_D(DUK_DPRINT("debug connection error: failed to decode tval"));
  37607. DUK__SET_CONN_BROKEN(thr, 1);
  37608. return NULL;
  37609. }
  37610. /*
  37611. * Debug connection write primitives
  37612. */
  37613. /* Write fully. */
  37614. DUK_INTERNAL void duk_debug_write_bytes(duk_hthread *thr, const duk_uint8_t *data, duk_size_t length) {
  37615. duk_heap *heap;
  37616. const duk_uint8_t *p;
  37617. duk_size_t left;
  37618. duk_size_t got;
  37619. DUK_ASSERT(thr != NULL);
  37620. DUK_ASSERT(length == 0 || data != NULL);
  37621. heap = thr->heap;
  37622. DUK_ASSERT(heap != NULL);
  37623. if (heap->dbg_write_cb == NULL) {
  37624. DUK_D(DUK_DPRINT("attempt to write %ld bytes in detached state, ignore", (long) length));
  37625. return;
  37626. }
  37627. if (length == 0) {
  37628. /* Avoid doing an actual write callback with length == 0,
  37629. * because that's reserved for a write flush.
  37630. */
  37631. return;
  37632. }
  37633. DUK_ASSERT(data != NULL);
  37634. p = data;
  37635. for (;;) {
  37636. left = (duk_size_t) ((data + length) - p);
  37637. if (left == 0) {
  37638. break;
  37639. }
  37640. DUK_ASSERT(heap->dbg_write_cb != NULL);
  37641. DUK_ASSERT(left >= 1);
  37642. #if defined(DUK_USE_DEBUGGER_TRANSPORT_TORTURE)
  37643. left = 1;
  37644. #endif
  37645. DUK__DBG_TPORT_ENTER();
  37646. got = heap->dbg_write_cb(heap->dbg_udata, (const char *) p, left);
  37647. DUK__DBG_TPORT_EXIT();
  37648. if (got == 0 || got > left) {
  37649. duk__debug_null_most_callbacks(thr); /* avoid calling write callback in detach1() */
  37650. DUK_D(DUK_DPRINT("connection error during write"));
  37651. DUK__SET_CONN_BROKEN(thr, 1);
  37652. return;
  37653. }
  37654. p += got;
  37655. }
  37656. }
  37657. DUK_INTERNAL void duk_debug_write_byte(duk_hthread *thr, duk_uint8_t x) {
  37658. duk_debug_write_bytes(thr, (const duk_uint8_t *) &x, 1);
  37659. }
  37660. DUK_INTERNAL void duk_debug_write_unused(duk_hthread *thr) {
  37661. duk_debug_write_byte(thr, DUK_DBG_IB_UNUSED);
  37662. }
  37663. DUK_INTERNAL void duk_debug_write_undefined(duk_hthread *thr) {
  37664. duk_debug_write_byte(thr, DUK_DBG_IB_UNDEFINED);
  37665. }
  37666. #if defined(DUK_USE_DEBUGGER_INSPECT)
  37667. DUK_INTERNAL void duk_debug_write_null(duk_hthread *thr) {
  37668. duk_debug_write_byte(thr, DUK_DBG_IB_NULL);
  37669. }
  37670. #endif
  37671. DUK_INTERNAL void duk_debug_write_boolean(duk_hthread *thr, duk_uint_t val) {
  37672. duk_debug_write_byte(thr, val ? DUK_DBG_IB_TRUE : DUK_DBG_IB_FALSE);
  37673. }
  37674. /* Write signed 32-bit integer. */
  37675. DUK_INTERNAL void duk_debug_write_int(duk_hthread *thr, duk_int32_t x) {
  37676. duk_uint8_t buf[5];
  37677. duk_size_t len;
  37678. DUK_ASSERT(thr != NULL);
  37679. if (x >= 0 && x <= 0x3fL) {
  37680. buf[0] = (duk_uint8_t) (0x80 + x);
  37681. len = 1;
  37682. } else if (x >= 0 && x <= 0x3fffL) {
  37683. buf[0] = (duk_uint8_t) (0xc0 + (x >> 8));
  37684. buf[1] = (duk_uint8_t) (x & 0xff);
  37685. len = 2;
  37686. } else {
  37687. /* Signed integers always map to 4 bytes now. */
  37688. buf[0] = (duk_uint8_t) DUK_DBG_IB_INT4;
  37689. buf[1] = (duk_uint8_t) ((x >> 24) & 0xff);
  37690. buf[2] = (duk_uint8_t) ((x >> 16) & 0xff);
  37691. buf[3] = (duk_uint8_t) ((x >> 8) & 0xff);
  37692. buf[4] = (duk_uint8_t) (x & 0xff);
  37693. len = 5;
  37694. }
  37695. duk_debug_write_bytes(thr, buf, len);
  37696. }
  37697. /* Write unsigned 32-bit integer. */
  37698. DUK_INTERNAL void duk_debug_write_uint(duk_hthread *thr, duk_uint32_t x) {
  37699. /* The debugger protocol doesn't support a plain integer encoding for
  37700. * the full 32-bit unsigned range (only 32-bit signed). For now,
  37701. * unsigned 32-bit values simply written as signed ones. This is not
  37702. * a concrete issue except for 32-bit heaphdr fields. Proper solutions
  37703. * would be to (a) write such integers as IEEE doubles or (b) add an
  37704. * unsigned 32-bit dvalue.
  37705. */
  37706. if (x >= 0x80000000UL) {
  37707. DUK_D(DUK_DPRINT("writing unsigned integer 0x%08lx as signed integer",
  37708. (long) x));
  37709. }
  37710. duk_debug_write_int(thr, (duk_int32_t) x);
  37711. }
  37712. DUK_INTERNAL void duk_debug_write_strbuf(duk_hthread *thr, const char *data, duk_size_t length, duk_uint8_t marker_base) {
  37713. duk_uint8_t buf[5];
  37714. duk_size_t buflen;
  37715. DUK_ASSERT(thr != NULL);
  37716. DUK_ASSERT(length == 0 || data != NULL);
  37717. if (length <= 0x1fUL && marker_base == DUK_DBG_IB_STR4) {
  37718. /* For strings, special form for short lengths. */
  37719. buf[0] = (duk_uint8_t) (0x60 + length);
  37720. buflen = 1;
  37721. } else if (length <= 0xffffUL) {
  37722. buf[0] = (duk_uint8_t) (marker_base + 1);
  37723. buf[1] = (duk_uint8_t) (length >> 8);
  37724. buf[2] = (duk_uint8_t) (length & 0xff);
  37725. buflen = 3;
  37726. } else {
  37727. buf[0] = (duk_uint8_t) marker_base;
  37728. buf[1] = (duk_uint8_t) (length >> 24);
  37729. buf[2] = (duk_uint8_t) ((length >> 16) & 0xff);
  37730. buf[3] = (duk_uint8_t) ((length >> 8) & 0xff);
  37731. buf[4] = (duk_uint8_t) (length & 0xff);
  37732. buflen = 5;
  37733. }
  37734. duk_debug_write_bytes(thr, (const duk_uint8_t *) buf, buflen);
  37735. duk_debug_write_bytes(thr, (const duk_uint8_t *) data, length);
  37736. }
  37737. DUK_INTERNAL void duk_debug_write_string(duk_hthread *thr, const char *data, duk_size_t length) {
  37738. duk_debug_write_strbuf(thr, data, length, DUK_DBG_IB_STR4);
  37739. }
  37740. DUK_INTERNAL void duk_debug_write_cstring(duk_hthread *thr, const char *data) {
  37741. DUK_ASSERT(thr != NULL);
  37742. duk_debug_write_string(thr,
  37743. data,
  37744. data ? DUK_STRLEN(data) : 0);
  37745. }
  37746. DUK_INTERNAL void duk_debug_write_hstring(duk_hthread *thr, duk_hstring *h) {
  37747. DUK_ASSERT(thr != NULL);
  37748. /* XXX: differentiate null pointer from empty string? */
  37749. duk_debug_write_string(thr,
  37750. (h != NULL ? (const char *) DUK_HSTRING_GET_DATA(h) : NULL),
  37751. (h != NULL ? (duk_size_t) DUK_HSTRING_GET_BYTELEN(h) : 0));
  37752. }
  37753. DUK_LOCAL void duk__debug_write_hstring_safe_top(duk_hthread *thr) {
  37754. duk_debug_write_hstring(thr, duk_safe_to_hstring(thr, -1));
  37755. }
  37756. DUK_INTERNAL void duk_debug_write_buffer(duk_hthread *thr, const char *data, duk_size_t length) {
  37757. duk_debug_write_strbuf(thr, data, length, DUK_DBG_IB_BUF4);
  37758. }
  37759. DUK_INTERNAL void duk_debug_write_hbuffer(duk_hthread *thr, duk_hbuffer *h) {
  37760. DUK_ASSERT(thr != NULL);
  37761. duk_debug_write_buffer(thr,
  37762. (h != NULL ? (const char *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h) : NULL),
  37763. (h != NULL ? (duk_size_t) DUK_HBUFFER_GET_SIZE(h) : 0));
  37764. }
  37765. DUK_LOCAL void duk__debug_write_pointer_raw(duk_hthread *thr, void *ptr, duk_uint8_t ibyte) {
  37766. duk_uint8_t buf[2];
  37767. duk__ptr_union pu;
  37768. DUK_ASSERT(thr != NULL);
  37769. DUK_ASSERT(sizeof(ptr) >= 1 && sizeof(ptr) <= 16);
  37770. /* ptr may be NULL */
  37771. buf[0] = ibyte;
  37772. buf[1] = sizeof(pu);
  37773. duk_debug_write_bytes(thr, buf, 2);
  37774. pu.p = (void *) ptr;
  37775. #if defined(DUK_USE_INTEGER_LE)
  37776. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  37777. #endif
  37778. duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
  37779. }
  37780. DUK_INTERNAL void duk_debug_write_pointer(duk_hthread *thr, void *ptr) {
  37781. duk__debug_write_pointer_raw(thr, ptr, DUK_DBG_IB_POINTER);
  37782. }
  37783. #if defined(DUK_USE_DEBUGGER_DUMPHEAP) || defined(DUK_USE_DEBUGGER_INSPECT)
  37784. DUK_INTERNAL void duk_debug_write_heapptr(duk_hthread *thr, duk_heaphdr *h) {
  37785. duk__debug_write_pointer_raw(thr, (void *) h, DUK_DBG_IB_HEAPPTR);
  37786. }
  37787. #endif /* DUK_USE_DEBUGGER_DUMPHEAP || DUK_USE_DEBUGGER_INSPECT */
  37788. DUK_INTERNAL void duk_debug_write_hobject(duk_hthread *thr, duk_hobject *obj) {
  37789. duk_uint8_t buf[3];
  37790. duk__ptr_union pu;
  37791. DUK_ASSERT(thr != NULL);
  37792. DUK_ASSERT(sizeof(obj) >= 1 && sizeof(obj) <= 16);
  37793. DUK_ASSERT(obj != NULL);
  37794. buf[0] = DUK_DBG_IB_OBJECT;
  37795. buf[1] = (duk_uint8_t) DUK_HOBJECT_GET_CLASS_NUMBER(obj);
  37796. buf[2] = sizeof(pu);
  37797. duk_debug_write_bytes(thr, buf, 3);
  37798. pu.p = (void *) obj;
  37799. #if defined(DUK_USE_INTEGER_LE)
  37800. duk_byteswap_bytes((duk_uint8_t *) pu.b, sizeof(pu));
  37801. #endif
  37802. duk_debug_write_bytes(thr, (const duk_uint8_t *) &pu.p, (duk_size_t) sizeof(pu));
  37803. }
  37804. DUK_INTERNAL void duk_debug_write_tval(duk_hthread *thr, duk_tval *tv) {
  37805. duk_c_function lf_func;
  37806. duk_small_uint_t lf_flags;
  37807. duk_uint8_t buf[4];
  37808. duk_double_union du1;
  37809. duk_double_union du2;
  37810. duk_int32_t i32;
  37811. DUK_ASSERT(thr != NULL);
  37812. DUK_ASSERT(tv != NULL);
  37813. switch (DUK_TVAL_GET_TAG(tv)) {
  37814. case DUK_TAG_UNDEFINED:
  37815. duk_debug_write_byte(thr, DUK_DBG_IB_UNDEFINED);
  37816. break;
  37817. case DUK_TAG_UNUSED:
  37818. duk_debug_write_byte(thr, DUK_DBG_IB_UNUSED);
  37819. break;
  37820. case DUK_TAG_NULL:
  37821. duk_debug_write_byte(thr, DUK_DBG_IB_NULL);
  37822. break;
  37823. case DUK_TAG_BOOLEAN:
  37824. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv) == 0 ||
  37825. DUK_TVAL_GET_BOOLEAN(tv) == 1);
  37826. duk_debug_write_boolean(thr, DUK_TVAL_GET_BOOLEAN(tv));
  37827. break;
  37828. case DUK_TAG_POINTER:
  37829. duk_debug_write_pointer(thr, (void *) DUK_TVAL_GET_POINTER(tv));
  37830. break;
  37831. case DUK_TAG_LIGHTFUNC:
  37832. DUK_TVAL_GET_LIGHTFUNC(tv, lf_func, lf_flags);
  37833. buf[0] = DUK_DBG_IB_LIGHTFUNC;
  37834. buf[1] = (duk_uint8_t) (lf_flags >> 8);
  37835. buf[2] = (duk_uint8_t) (lf_flags & 0xff);
  37836. buf[3] = sizeof(lf_func);
  37837. duk_debug_write_bytes(thr, buf, 4);
  37838. duk_debug_write_bytes(thr, (const duk_uint8_t *) &lf_func, sizeof(lf_func));
  37839. break;
  37840. case DUK_TAG_STRING:
  37841. duk_debug_write_hstring(thr, DUK_TVAL_GET_STRING(tv));
  37842. break;
  37843. case DUK_TAG_OBJECT:
  37844. duk_debug_write_hobject(thr, DUK_TVAL_GET_OBJECT(tv));
  37845. break;
  37846. case DUK_TAG_BUFFER:
  37847. duk_debug_write_hbuffer(thr, DUK_TVAL_GET_BUFFER(tv));
  37848. break;
  37849. #if defined(DUK_USE_FASTINT)
  37850. case DUK_TAG_FASTINT:
  37851. #endif
  37852. default:
  37853. /* Numbers are normalized to big (network) endian. We can
  37854. * (but are not required) to use integer dvalues when there's
  37855. * no loss of precision.
  37856. *
  37857. * XXX: share check with other code; this check is slow but
  37858. * reliable and doesn't require careful exponent/mantissa
  37859. * mask tricks as in the fastint downgrade code.
  37860. */
  37861. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  37862. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  37863. du1.d = DUK_TVAL_GET_NUMBER(tv);
  37864. i32 = (duk_int32_t) du1.d;
  37865. du2.d = (duk_double_t) i32;
  37866. DUK_DD(DUK_DDPRINT("i32=%ld du1=%02x%02x%02x%02x%02x%02x%02x%02x "
  37867. "du2=%02x%02x%02x%02x%02x%02x%02x%02x",
  37868. (long) i32,
  37869. (unsigned int) du1.uc[0], (unsigned int) du1.uc[1],
  37870. (unsigned int) du1.uc[2], (unsigned int) du1.uc[3],
  37871. (unsigned int) du1.uc[4], (unsigned int) du1.uc[5],
  37872. (unsigned int) du1.uc[6], (unsigned int) du1.uc[7],
  37873. (unsigned int) du2.uc[0], (unsigned int) du2.uc[1],
  37874. (unsigned int) du2.uc[2], (unsigned int) du2.uc[3],
  37875. (unsigned int) du2.uc[4], (unsigned int) du2.uc[5],
  37876. (unsigned int) du2.uc[6], (unsigned int) du2.uc[7]));
  37877. if (duk_memcmp((const void *) du1.uc, (const void *) du2.uc, sizeof(du1.uc)) == 0) {
  37878. duk_debug_write_int(thr, i32);
  37879. } else {
  37880. DUK_DBLUNION_DOUBLE_HTON(&du1);
  37881. duk_debug_write_byte(thr, DUK_DBG_IB_NUMBER);
  37882. duk_debug_write_bytes(thr, (const duk_uint8_t *) du1.uc, sizeof(du1.uc));
  37883. }
  37884. }
  37885. }
  37886. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  37887. /* Variant for writing duk_tvals so that any heap allocated values are
  37888. * written out as tagged heap pointers.
  37889. */
  37890. DUK_LOCAL void duk__debug_write_tval_heapptr(duk_hthread *thr, duk_tval *tv) {
  37891. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  37892. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  37893. duk_debug_write_heapptr(thr, h);
  37894. } else {
  37895. duk_debug_write_tval(thr, tv);
  37896. }
  37897. }
  37898. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  37899. /*
  37900. * Debug connection message write helpers
  37901. */
  37902. #if 0 /* unused */
  37903. DUK_INTERNAL void duk_debug_write_request(duk_hthread *thr, duk_small_uint_t command) {
  37904. duk_debug_write_byte(thr, DUK_DBG_IB_REQUEST);
  37905. duk_debug_write_int(thr, command);
  37906. }
  37907. #endif
  37908. DUK_INTERNAL void duk_debug_write_reply(duk_hthread *thr) {
  37909. duk_debug_write_byte(thr, DUK_DBG_IB_REPLY);
  37910. }
  37911. DUK_INTERNAL void duk_debug_write_error_eom(duk_hthread *thr, duk_small_uint_t err_code, const char *msg) {
  37912. /* Allow NULL 'msg' */
  37913. duk_debug_write_byte(thr, DUK_DBG_IB_ERROR);
  37914. duk_debug_write_int(thr, (duk_int32_t) err_code);
  37915. duk_debug_write_cstring(thr, msg);
  37916. duk_debug_write_eom(thr);
  37917. }
  37918. DUK_INTERNAL void duk_debug_write_notify(duk_hthread *thr, duk_small_uint_t command) {
  37919. duk_debug_write_byte(thr, DUK_DBG_IB_NOTIFY);
  37920. duk_debug_write_int(thr, (duk_int32_t) command);
  37921. }
  37922. DUK_INTERNAL void duk_debug_write_eom(duk_hthread *thr) {
  37923. duk_debug_write_byte(thr, DUK_DBG_IB_EOM);
  37924. /* As an initial implementation, write flush after every EOM (and the
  37925. * version identifier). A better implementation would flush only when
  37926. * Duktape is finished processing messages so that a flush only happens
  37927. * after all outbound messages are finished on that occasion.
  37928. */
  37929. duk_debug_write_flush(thr);
  37930. }
  37931. /*
  37932. * Status message and helpers
  37933. */
  37934. DUK_INTERNAL duk_uint_fast32_t duk_debug_curr_line(duk_hthread *thr) {
  37935. duk_activation *act;
  37936. duk_uint_fast32_t line;
  37937. duk_uint_fast32_t pc;
  37938. act = thr->callstack_curr;
  37939. if (act == NULL) {
  37940. return 0;
  37941. }
  37942. /* We're conceptually between two opcodes; act->pc indicates the next
  37943. * instruction to be executed. This is usually the correct pc/line to
  37944. * indicate in Status. (For the 'debugger' statement this now reports
  37945. * the pc/line after the debugger statement because the debugger opcode
  37946. * has already been executed.)
  37947. */
  37948. pc = duk_hthread_get_act_curr_pc(thr, act);
  37949. /* XXX: this should be optimized to be a raw query and avoid valstack
  37950. * operations if possible.
  37951. */
  37952. duk_push_tval(thr, &act->tv_func);
  37953. line = duk_hobject_pc2line_query(thr, -1, pc);
  37954. duk_pop(thr);
  37955. return line;
  37956. }
  37957. DUK_INTERNAL void duk_debug_send_status(duk_hthread *thr) {
  37958. duk_activation *act;
  37959. duk_debug_write_notify(thr, DUK_DBG_CMD_STATUS);
  37960. duk_debug_write_int(thr, (DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap) ? 1 : 0));
  37961. act = thr->callstack_curr;
  37962. if (act == NULL) {
  37963. duk_debug_write_undefined(thr);
  37964. duk_debug_write_undefined(thr);
  37965. duk_debug_write_int(thr, 0);
  37966. duk_debug_write_int(thr, 0);
  37967. } else {
  37968. duk_push_tval(thr, &act->tv_func);
  37969. duk_get_prop_literal(thr, -1, "fileName");
  37970. duk__debug_write_hstring_safe_top(thr);
  37971. duk_get_prop_literal(thr, -2, "name");
  37972. duk__debug_write_hstring_safe_top(thr);
  37973. duk_pop_3(thr);
  37974. /* Report next pc/line to be executed. */
  37975. duk_debug_write_uint(thr, (duk_uint32_t) duk_debug_curr_line(thr));
  37976. duk_debug_write_uint(thr, (duk_uint32_t) duk_hthread_get_act_curr_pc(thr, act));
  37977. }
  37978. duk_debug_write_eom(thr);
  37979. }
  37980. #if defined(DUK_USE_DEBUGGER_THROW_NOTIFY)
  37981. DUK_INTERNAL void duk_debug_send_throw(duk_hthread *thr, duk_bool_t fatal) {
  37982. /*
  37983. * NFY <int: 5> <int: fatal> <str: msg> <str: filename> <int: linenumber> EOM
  37984. */
  37985. duk_activation *act;
  37986. duk_uint32_t pc;
  37987. DUK_ASSERT(thr->valstack_top > thr->valstack); /* At least: ... [err] */
  37988. duk_debug_write_notify(thr, DUK_DBG_CMD_THROW);
  37989. duk_debug_write_int(thr, (duk_int32_t) fatal);
  37990. /* Report thrown value to client coerced to string */
  37991. duk_dup_top(thr);
  37992. duk__debug_write_hstring_safe_top(thr);
  37993. duk_pop(thr);
  37994. if (duk_is_error(thr, -1)) {
  37995. /* Error instance, use augmented error data directly */
  37996. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_FILE_NAME);
  37997. duk__debug_write_hstring_safe_top(thr);
  37998. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_LINE_NUMBER);
  37999. duk_debug_write_uint(thr, duk_get_uint(thr, -1));
  38000. duk_pop_2(thr);
  38001. } else {
  38002. /* For anything other than an Error instance, we calculate the
  38003. * error location directly from the current activation if one
  38004. * exists.
  38005. */
  38006. act = thr->callstack_curr;
  38007. if (act != NULL) {
  38008. duk_push_tval(thr, &act->tv_func);
  38009. duk_get_prop_literal(thr, -1, "fileName");
  38010. duk__debug_write_hstring_safe_top(thr);
  38011. pc = (duk_uint32_t) duk_hthread_get_act_prev_pc(thr, act);
  38012. duk_debug_write_uint(thr, (duk_uint32_t) duk_hobject_pc2line_query(thr, -2, pc));
  38013. duk_pop_2(thr);
  38014. } else {
  38015. /* Can happen if duk_throw() is called on an empty
  38016. * callstack.
  38017. */
  38018. duk_debug_write_cstring(thr, "");
  38019. duk_debug_write_uint(thr, 0);
  38020. }
  38021. }
  38022. duk_debug_write_eom(thr);
  38023. }
  38024. #endif /* DUK_USE_DEBUGGER_THROW_NOTIFY */
  38025. /*
  38026. * Debug message processing
  38027. */
  38028. /* Skip dvalue. */
  38029. DUK_LOCAL duk_bool_t duk__debug_skip_dvalue(duk_hthread *thr) {
  38030. duk_uint8_t x;
  38031. duk_uint32_t len;
  38032. x = duk_debug_read_byte(thr);
  38033. if (x >= 0xc0) {
  38034. duk_debug_skip_byte(thr);
  38035. return 0;
  38036. }
  38037. if (x >= 0x80) {
  38038. return 0;
  38039. }
  38040. if (x >= 0x60) {
  38041. duk_debug_skip_bytes(thr, (duk_size_t) (x - 0x60));
  38042. return 0;
  38043. }
  38044. switch(x) {
  38045. case DUK_DBG_IB_EOM:
  38046. return 1; /* Return 1: got EOM */
  38047. case DUK_DBG_IB_REQUEST:
  38048. case DUK_DBG_IB_REPLY:
  38049. case DUK_DBG_IB_ERROR:
  38050. case DUK_DBG_IB_NOTIFY:
  38051. break;
  38052. case DUK_DBG_IB_INT4:
  38053. (void) duk__debug_read_uint32_raw(thr);
  38054. break;
  38055. case DUK_DBG_IB_STR4:
  38056. case DUK_DBG_IB_BUF4:
  38057. len = duk__debug_read_uint32_raw(thr);
  38058. duk_debug_skip_bytes(thr, len);
  38059. break;
  38060. case DUK_DBG_IB_STR2:
  38061. case DUK_DBG_IB_BUF2:
  38062. len = duk__debug_read_uint16_raw(thr);
  38063. duk_debug_skip_bytes(thr, len);
  38064. break;
  38065. case DUK_DBG_IB_UNUSED:
  38066. case DUK_DBG_IB_UNDEFINED:
  38067. case DUK_DBG_IB_NULL:
  38068. case DUK_DBG_IB_TRUE:
  38069. case DUK_DBG_IB_FALSE:
  38070. break;
  38071. case DUK_DBG_IB_NUMBER:
  38072. duk_debug_skip_bytes(thr, 8);
  38073. break;
  38074. case DUK_DBG_IB_OBJECT:
  38075. duk_debug_skip_byte(thr);
  38076. len = duk_debug_read_byte(thr);
  38077. duk_debug_skip_bytes(thr, len);
  38078. break;
  38079. case DUK_DBG_IB_POINTER:
  38080. case DUK_DBG_IB_HEAPPTR:
  38081. len = duk_debug_read_byte(thr);
  38082. duk_debug_skip_bytes(thr, len);
  38083. break;
  38084. case DUK_DBG_IB_LIGHTFUNC:
  38085. duk_debug_skip_bytes(thr, 2);
  38086. len = duk_debug_read_byte(thr);
  38087. duk_debug_skip_bytes(thr, len);
  38088. break;
  38089. default:
  38090. goto fail;
  38091. }
  38092. return 0;
  38093. fail:
  38094. DUK__SET_CONN_BROKEN(thr, 1);
  38095. return 1; /* Pretend like we got EOM */
  38096. }
  38097. /* Skip dvalues to EOM. */
  38098. DUK_LOCAL void duk__debug_skip_to_eom(duk_hthread *thr) {
  38099. for (;;) {
  38100. if (duk__debug_skip_dvalue(thr)) {
  38101. break;
  38102. }
  38103. }
  38104. }
  38105. /* Read and validate a call stack index. If index is invalid, write out an
  38106. * error message and return zero.
  38107. */
  38108. DUK_LOCAL duk_int32_t duk__debug_read_validate_csindex(duk_hthread *thr) {
  38109. duk_int32_t level;
  38110. level = duk_debug_read_int(thr);
  38111. if (level >= 0 || -level > (duk_int32_t) thr->callstack_top) {
  38112. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack index");
  38113. return 0; /* zero indicates failure */
  38114. }
  38115. return level;
  38116. }
  38117. /* Read a call stack index and lookup the corresponding duk_activation.
  38118. * If index is invalid, write out an error message and return NULL.
  38119. */
  38120. DUK_LOCAL duk_activation *duk__debug_read_level_get_activation(duk_hthread *thr) {
  38121. duk_activation *act;
  38122. duk_int32_t level;
  38123. level = duk_debug_read_int(thr);
  38124. act = duk_hthread_get_activation_for_level(thr, level);
  38125. if (act == NULL) {
  38126. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack index");
  38127. }
  38128. return act;
  38129. }
  38130. /*
  38131. * Simple commands
  38132. */
  38133. DUK_LOCAL void duk__debug_handle_basic_info(duk_hthread *thr, duk_heap *heap) {
  38134. DUK_UNREF(heap);
  38135. DUK_D(DUK_DPRINT("debug command Version"));
  38136. duk_debug_write_reply(thr);
  38137. duk_debug_write_int(thr, DUK_VERSION);
  38138. duk_debug_write_cstring(thr, DUK_GIT_DESCRIBE);
  38139. duk_debug_write_cstring(thr, DUK_USE_TARGET_INFO);
  38140. #if defined(DUK_USE_DOUBLE_LE)
  38141. duk_debug_write_int(thr, 1);
  38142. #elif defined(DUK_USE_DOUBLE_ME)
  38143. duk_debug_write_int(thr, 2);
  38144. #elif defined(DUK_USE_DOUBLE_BE)
  38145. duk_debug_write_int(thr, 3);
  38146. #else
  38147. duk_debug_write_int(thr, 0);
  38148. #endif
  38149. duk_debug_write_int(thr, (duk_int_t) sizeof(void *));
  38150. duk_debug_write_eom(thr);
  38151. }
  38152. DUK_LOCAL void duk__debug_handle_trigger_status(duk_hthread *thr, duk_heap *heap) {
  38153. DUK_UNREF(heap);
  38154. DUK_D(DUK_DPRINT("debug command TriggerStatus"));
  38155. duk_debug_write_reply(thr);
  38156. duk_debug_write_eom(thr);
  38157. heap->dbg_state_dirty = 1;
  38158. }
  38159. DUK_LOCAL void duk__debug_handle_pause(duk_hthread *thr, duk_heap *heap) {
  38160. DUK_D(DUK_DPRINT("debug command Pause"));
  38161. duk_debug_set_paused(heap);
  38162. duk_debug_write_reply(thr);
  38163. duk_debug_write_eom(thr);
  38164. }
  38165. DUK_LOCAL void duk__debug_handle_resume(duk_hthread *thr, duk_heap *heap) {
  38166. duk_small_uint_t pause_flags;
  38167. DUK_D(DUK_DPRINT("debug command Resume"));
  38168. duk_debug_clear_paused(heap);
  38169. pause_flags = 0;
  38170. #if 0 /* manual testing */
  38171. pause_flags |= DUK_PAUSE_FLAG_ONE_OPCODE;
  38172. pause_flags |= DUK_PAUSE_FLAG_CAUGHT_ERROR;
  38173. pause_flags |= DUK_PAUSE_FLAG_UNCAUGHT_ERROR;
  38174. #endif
  38175. #if defined(DUK_USE_DEBUGGER_PAUSE_UNCAUGHT)
  38176. pause_flags |= DUK_PAUSE_FLAG_UNCAUGHT_ERROR;
  38177. #endif
  38178. duk__debug_set_pause_state(thr, heap, pause_flags);
  38179. duk_debug_write_reply(thr);
  38180. duk_debug_write_eom(thr);
  38181. }
  38182. DUK_LOCAL void duk__debug_handle_step(duk_hthread *thr, duk_heap *heap, duk_int32_t cmd) {
  38183. duk_small_uint_t pause_flags;
  38184. DUK_D(DUK_DPRINT("debug command StepInto/StepOver/StepOut: %d", (int) cmd));
  38185. if (cmd == DUK_DBG_CMD_STEPINTO) {
  38186. pause_flags = DUK_PAUSE_FLAG_LINE_CHANGE |
  38187. DUK_PAUSE_FLAG_FUNC_ENTRY |
  38188. DUK_PAUSE_FLAG_FUNC_EXIT;
  38189. } else if (cmd == DUK_DBG_CMD_STEPOVER) {
  38190. pause_flags = DUK_PAUSE_FLAG_LINE_CHANGE |
  38191. DUK_PAUSE_FLAG_FUNC_EXIT;
  38192. } else {
  38193. DUK_ASSERT(cmd == DUK_DBG_CMD_STEPOUT);
  38194. pause_flags = DUK_PAUSE_FLAG_FUNC_EXIT;
  38195. }
  38196. #if defined(DUK_USE_DEBUGGER_PAUSE_UNCAUGHT)
  38197. pause_flags |= DUK_PAUSE_FLAG_UNCAUGHT_ERROR;
  38198. #endif
  38199. /* If current activation doesn't have line information, line-based
  38200. * pause flags are automatically disabled. As a result, e.g.
  38201. * StepInto will then pause on (native) function entry or exit.
  38202. */
  38203. duk_debug_clear_paused(heap);
  38204. duk__debug_set_pause_state(thr, heap, pause_flags);
  38205. duk_debug_write_reply(thr);
  38206. duk_debug_write_eom(thr);
  38207. }
  38208. DUK_LOCAL void duk__debug_handle_list_break(duk_hthread *thr, duk_heap *heap) {
  38209. duk_small_int_t i;
  38210. DUK_D(DUK_DPRINT("debug command ListBreak"));
  38211. duk_debug_write_reply(thr);
  38212. for (i = 0; i < (duk_small_int_t) heap->dbg_breakpoint_count; i++) {
  38213. duk_debug_write_hstring(thr, heap->dbg_breakpoints[i].filename);
  38214. duk_debug_write_uint(thr, (duk_uint32_t) heap->dbg_breakpoints[i].line);
  38215. }
  38216. duk_debug_write_eom(thr);
  38217. }
  38218. DUK_LOCAL void duk__debug_handle_add_break(duk_hthread *thr, duk_heap *heap) {
  38219. duk_hstring *filename;
  38220. duk_uint32_t linenumber;
  38221. duk_small_int_t idx;
  38222. DUK_UNREF(heap);
  38223. filename = duk_debug_read_hstring(thr);
  38224. linenumber = (duk_uint32_t) duk_debug_read_int(thr);
  38225. DUK_D(DUK_DPRINT("debug command AddBreak: %!O:%ld", (duk_hobject *) filename, (long) linenumber));
  38226. idx = duk_debug_add_breakpoint(thr, filename, linenumber);
  38227. if (idx >= 0) {
  38228. duk_debug_write_reply(thr);
  38229. duk_debug_write_int(thr, (duk_int32_t) idx);
  38230. duk_debug_write_eom(thr);
  38231. } else {
  38232. duk_debug_write_error_eom(thr, DUK_DBG_ERR_TOOMANY, "no space for breakpoint");
  38233. }
  38234. }
  38235. DUK_LOCAL void duk__debug_handle_del_break(duk_hthread *thr, duk_heap *heap) {
  38236. duk_small_uint_t idx;
  38237. DUK_UNREF(heap);
  38238. DUK_D(DUK_DPRINT("debug command DelBreak"));
  38239. idx = (duk_small_uint_t) duk_debug_read_int(thr);
  38240. if (duk_debug_remove_breakpoint(thr, idx)) {
  38241. duk_debug_write_reply(thr);
  38242. duk_debug_write_eom(thr);
  38243. } else {
  38244. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid breakpoint index");
  38245. }
  38246. }
  38247. DUK_LOCAL void duk__debug_handle_get_var(duk_hthread *thr, duk_heap *heap) {
  38248. duk_activation *act;
  38249. duk_hstring *str;
  38250. duk_bool_t rc;
  38251. DUK_UNREF(heap);
  38252. DUK_D(DUK_DPRINT("debug command GetVar"));
  38253. act = duk__debug_read_level_get_activation(thr);
  38254. if (act == NULL) {
  38255. return;
  38256. }
  38257. str = duk_debug_read_hstring(thr); /* push to stack */
  38258. DUK_ASSERT(str != NULL);
  38259. rc = duk_js_getvar_activation(thr, act, str, 0);
  38260. duk_debug_write_reply(thr);
  38261. if (rc) {
  38262. duk_debug_write_int(thr, 1);
  38263. DUK_ASSERT(duk_get_tval(thr, -2) != NULL);
  38264. duk_debug_write_tval(thr, duk_get_tval(thr, -2));
  38265. } else {
  38266. duk_debug_write_int(thr, 0);
  38267. duk_debug_write_unused(thr);
  38268. }
  38269. duk_debug_write_eom(thr);
  38270. }
  38271. DUK_LOCAL void duk__debug_handle_put_var(duk_hthread *thr, duk_heap *heap) {
  38272. duk_activation *act;
  38273. duk_hstring *str;
  38274. duk_tval *tv;
  38275. DUK_UNREF(heap);
  38276. DUK_D(DUK_DPRINT("debug command PutVar"));
  38277. act = duk__debug_read_level_get_activation(thr);
  38278. if (act == NULL) {
  38279. return;
  38280. }
  38281. str = duk_debug_read_hstring(thr); /* push to stack */
  38282. DUK_ASSERT(str != NULL);
  38283. tv = duk_debug_read_tval(thr);
  38284. if (tv == NULL) {
  38285. /* detached */
  38286. return;
  38287. }
  38288. duk_js_putvar_activation(thr, act, str, tv, 0);
  38289. /* XXX: Current putvar implementation doesn't have a success flag,
  38290. * add one and send to debug client?
  38291. */
  38292. duk_debug_write_reply(thr);
  38293. duk_debug_write_eom(thr);
  38294. }
  38295. DUK_LOCAL void duk__debug_handle_get_call_stack(duk_hthread *thr, duk_heap *heap) {
  38296. duk_hthread *curr_thr = thr;
  38297. duk_activation *curr_act;
  38298. duk_uint_fast32_t pc;
  38299. duk_uint_fast32_t line;
  38300. DUK_ASSERT(thr != NULL);
  38301. DUK_UNREF(heap);
  38302. duk_debug_write_reply(thr);
  38303. while (curr_thr != NULL) {
  38304. for (curr_act = curr_thr->callstack_curr; curr_act != NULL; curr_act = curr_act->parent) {
  38305. /* PC/line semantics here are:
  38306. * - For callstack top we're conceptually between two
  38307. * opcodes and current PC indicates next line to
  38308. * execute, so report that (matches Status).
  38309. * - For other activations we're conceptually still
  38310. * executing the instruction at PC-1, so report that
  38311. * (matches error stacktrace behavior).
  38312. * - See: https://github.com/svaarala/duktape/issues/281
  38313. */
  38314. /* XXX: optimize to use direct reads, i.e. avoid
  38315. * value stack operations.
  38316. */
  38317. duk_push_tval(thr, &curr_act->tv_func);
  38318. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_FILE_NAME);
  38319. duk__debug_write_hstring_safe_top(thr);
  38320. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_NAME);
  38321. duk__debug_write_hstring_safe_top(thr);
  38322. pc = duk_hthread_get_act_curr_pc(thr, curr_act);
  38323. if (curr_act != curr_thr->callstack_curr && pc > 0) {
  38324. pc--;
  38325. }
  38326. line = duk_hobject_pc2line_query(thr, -3, pc);
  38327. duk_debug_write_uint(thr, (duk_uint32_t) line);
  38328. duk_debug_write_uint(thr, (duk_uint32_t) pc);
  38329. duk_pop_3(thr);
  38330. }
  38331. curr_thr = curr_thr->resumer;
  38332. }
  38333. /* SCANBUILD: warning about 'thr' potentially being NULL here,
  38334. * warning is incorrect because thr != NULL always here.
  38335. */
  38336. duk_debug_write_eom(thr);
  38337. }
  38338. DUK_LOCAL void duk__debug_handle_get_locals(duk_hthread *thr, duk_heap *heap) {
  38339. duk_activation *act;
  38340. duk_hstring *varname;
  38341. DUK_UNREF(heap);
  38342. act = duk__debug_read_level_get_activation(thr);
  38343. if (act == NULL) {
  38344. return;
  38345. }
  38346. duk_debug_write_reply(thr);
  38347. /* XXX: several nice-to-have improvements here:
  38348. * - Use direct reads avoiding value stack operations
  38349. * - Avoid triggering getters, indicate getter values to debug client
  38350. * - If side effects are possible, add error catching
  38351. */
  38352. duk_push_tval(thr, &act->tv_func);
  38353. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_VARMAP);
  38354. if (duk_is_object(thr, -1)) {
  38355. duk_enum(thr, -1, 0 /*enum_flags*/);
  38356. while (duk_next(thr, -1 /*enum_index*/, 0 /*get_value*/)) {
  38357. varname = duk_known_hstring(thr, -1);
  38358. duk_js_getvar_activation(thr, act, varname, 0 /*throw_flag*/);
  38359. /* [ ... func varmap enum key value this ] */
  38360. duk_debug_write_hstring(thr, duk_get_hstring(thr, -3));
  38361. duk_debug_write_tval(thr, duk_get_tval(thr, -2));
  38362. duk_pop_3(thr); /* -> [ ... func varmap enum ] */
  38363. }
  38364. } else {
  38365. DUK_D(DUK_DPRINT("varmap is not an object in GetLocals, ignore"));
  38366. }
  38367. duk_debug_write_eom(thr);
  38368. }
  38369. DUK_LOCAL void duk__debug_handle_eval(duk_hthread *thr, duk_heap *heap) {
  38370. duk_small_uint_t call_flags;
  38371. duk_int_t call_ret;
  38372. duk_small_int_t eval_err;
  38373. duk_bool_t direct_eval;
  38374. duk_int32_t level;
  38375. duk_idx_t idx_func;
  38376. DUK_UNREF(heap);
  38377. DUK_D(DUK_DPRINT("debug command Eval"));
  38378. /* The eval code is executed within the lexical environment of a specified
  38379. * activation. For now, use global object eval() function, with the eval
  38380. * considered a 'direct call to eval'.
  38381. *
  38382. * Callstack index for debug commands only affects scope -- the callstack
  38383. * as seen by, e.g. Duktape.act() will be the same regardless.
  38384. */
  38385. /* nargs == 2 so we can pass a callstack index to eval(). */
  38386. idx_func = duk_get_top(thr);
  38387. duk_push_c_function(thr, duk_bi_global_object_eval, 2 /*nargs*/);
  38388. duk_push_undefined(thr); /* 'this' binding shouldn't matter here */
  38389. /* Read callstack index, if non-null. */
  38390. if (duk_debug_peek_byte(thr) == DUK_DBG_IB_NULL) {
  38391. direct_eval = 0;
  38392. level = -1; /* Not needed, but silences warning. */
  38393. (void) duk_debug_read_byte(thr);
  38394. } else {
  38395. direct_eval = 1;
  38396. level = duk__debug_read_validate_csindex(thr);
  38397. if (level == 0) {
  38398. return;
  38399. }
  38400. }
  38401. DUK_ASSERT(!direct_eval ||
  38402. (level < 0 && -level <= (duk_int32_t) thr->callstack_top));
  38403. (void) duk_debug_read_hstring(thr);
  38404. if (direct_eval) {
  38405. duk_push_int(thr, level - 1); /* compensate for eval() call */
  38406. }
  38407. /* [ ... eval "eval" eval_input level? ] */
  38408. call_flags = 0;
  38409. if (direct_eval) {
  38410. duk_activation *act;
  38411. duk_hobject *fun;
  38412. act = duk_hthread_get_activation_for_level(thr, level);
  38413. if (act != NULL) {
  38414. fun = DUK_ACT_GET_FUNC(act);
  38415. if (fun != NULL && DUK_HOBJECT_IS_COMPFUNC(fun)) {
  38416. /* Direct eval requires that there's a current
  38417. * activation and it is an ECMAScript function.
  38418. * When Eval is executed from e.g. cooperate API
  38419. * call we'll need to do an indirect eval instead.
  38420. */
  38421. call_flags |= DUK_CALL_FLAG_DIRECT_EVAL;
  38422. }
  38423. }
  38424. }
  38425. call_ret = duk_pcall_method_flags(thr, duk_get_top(thr) - (idx_func + 2), call_flags);
  38426. if (call_ret == DUK_EXEC_SUCCESS) {
  38427. eval_err = 0;
  38428. /* Use result value as is. */
  38429. } else {
  38430. /* For errors a string coerced result is most informative
  38431. * right now, as the debug client doesn't have the capability
  38432. * to traverse the error object.
  38433. */
  38434. eval_err = 1;
  38435. duk_safe_to_string(thr, -1);
  38436. }
  38437. /* [ ... result ] */
  38438. duk_debug_write_reply(thr);
  38439. duk_debug_write_int(thr, (duk_int32_t) eval_err);
  38440. DUK_ASSERT(duk_get_tval(thr, -1) != NULL);
  38441. duk_debug_write_tval(thr, duk_get_tval(thr, -1));
  38442. duk_debug_write_eom(thr);
  38443. }
  38444. DUK_LOCAL void duk__debug_handle_detach(duk_hthread *thr, duk_heap *heap) {
  38445. DUK_UNREF(heap);
  38446. DUK_D(DUK_DPRINT("debug command Detach"));
  38447. duk_debug_write_reply(thr);
  38448. duk_debug_write_eom(thr);
  38449. DUK_D(DUK_DPRINT("debug connection detached, mark broken"));
  38450. DUK__SET_CONN_BROKEN(thr, 0); /* not an error */
  38451. }
  38452. DUK_LOCAL void duk__debug_handle_apprequest(duk_hthread *thr, duk_heap *heap) {
  38453. duk_idx_t old_top;
  38454. DUK_D(DUK_DPRINT("debug command AppRequest"));
  38455. old_top = duk_get_top(thr); /* save stack top */
  38456. if (heap->dbg_request_cb != NULL) {
  38457. duk_idx_t nrets;
  38458. duk_idx_t nvalues = 0;
  38459. duk_idx_t top, idx;
  38460. /* Read tvals from the message and push them onto the valstack,
  38461. * then call the request callback to process the request.
  38462. */
  38463. while (duk_debug_peek_byte(thr) != DUK_DBG_IB_EOM) {
  38464. duk_tval *tv;
  38465. if (!duk_check_stack(thr, 1)) {
  38466. DUK_D(DUK_DPRINT("failed to allocate space for request dvalue(s)"));
  38467. goto fail;
  38468. }
  38469. tv = duk_debug_read_tval(thr); /* push to stack */
  38470. if (tv == NULL) {
  38471. /* detached */
  38472. return;
  38473. }
  38474. nvalues++;
  38475. }
  38476. DUK_ASSERT(duk_get_top(thr) == old_top + nvalues);
  38477. /* Request callback should push values for reply to client onto valstack */
  38478. DUK_D(DUK_DPRINT("calling into AppRequest request_cb with nvalues=%ld, old_top=%ld, top=%ld",
  38479. (long) nvalues, (long) old_top, (long) duk_get_top(thr)));
  38480. nrets = heap->dbg_request_cb(thr, heap->dbg_udata, nvalues);
  38481. DUK_D(DUK_DPRINT("returned from AppRequest request_cb; nvalues=%ld -> nrets=%ld, old_top=%ld, top=%ld",
  38482. (long) nvalues, (long) nrets, (long) old_top, (long) duk_get_top(thr)));
  38483. if (nrets >= 0) {
  38484. DUK_ASSERT(duk_get_top(thr) >= old_top + nrets);
  38485. if (duk_get_top(thr) < old_top + nrets) {
  38486. DUK_D(DUK_DPRINT("AppRequest callback doesn't match value stack configuration, "
  38487. "top=%ld < old_top=%ld + nrets=%ld; "
  38488. "this might mean it's unsafe to continue!",
  38489. (long) duk_get_top(thr), (long) old_top, (long) nrets));
  38490. goto fail;
  38491. }
  38492. /* Reply with tvals pushed by request callback */
  38493. duk_debug_write_byte(thr, DUK_DBG_IB_REPLY);
  38494. top = duk_get_top(thr);
  38495. for (idx = top - nrets; idx < top; idx++) {
  38496. duk_debug_write_tval(thr, DUK_GET_TVAL_POSIDX(thr, idx));
  38497. }
  38498. duk_debug_write_eom(thr);
  38499. } else {
  38500. DUK_ASSERT(duk_get_top(thr) >= old_top + 1);
  38501. if (duk_get_top(thr) < old_top + 1) {
  38502. DUK_D(DUK_DPRINT("request callback return value doesn't match value stack configuration"));
  38503. goto fail;
  38504. }
  38505. duk_debug_write_error_eom(thr, DUK_DBG_ERR_APPLICATION, duk_get_string(thr, -1));
  38506. }
  38507. duk_set_top(thr, old_top); /* restore stack top */
  38508. } else {
  38509. DUK_D(DUK_DPRINT("no request callback, treat AppRequest as unsupported"));
  38510. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNSUPPORTED, "AppRequest unsupported by target");
  38511. }
  38512. return;
  38513. fail:
  38514. duk_set_top(thr, old_top); /* restore stack top */
  38515. DUK__SET_CONN_BROKEN(thr, 1);
  38516. }
  38517. /*
  38518. * DumpHeap command
  38519. */
  38520. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  38521. /* XXX: this has some overlap with object inspection; remove this and make
  38522. * DumpHeap return lists of heapptrs instead?
  38523. */
  38524. DUK_LOCAL void duk__debug_dump_heaphdr(duk_hthread *thr, duk_heap *heap, duk_heaphdr *hdr) {
  38525. DUK_UNREF(heap);
  38526. duk_debug_write_heapptr(thr, hdr);
  38527. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_TYPE(hdr));
  38528. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_FLAGS_RAW(hdr));
  38529. #if defined(DUK_USE_REFERENCE_COUNTING)
  38530. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HEAPHDR_GET_REFCOUNT(hdr));
  38531. #else
  38532. duk_debug_write_int(thr, (duk_int32_t) -1);
  38533. #endif
  38534. switch (DUK_HEAPHDR_GET_TYPE(hdr)) {
  38535. case DUK_HTYPE_STRING: {
  38536. duk_hstring *h = (duk_hstring *) hdr;
  38537. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HSTRING_GET_BYTELEN(h));
  38538. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HSTRING_GET_CHARLEN(h));
  38539. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HSTRING_GET_HASH(h));
  38540. duk_debug_write_hstring(thr, h);
  38541. break;
  38542. }
  38543. case DUK_HTYPE_OBJECT: {
  38544. duk_hobject *h = (duk_hobject *) hdr;
  38545. duk_hstring *k;
  38546. duk_uint_fast32_t i;
  38547. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_CLASS_NUMBER(h));
  38548. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(heap, h));
  38549. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ESIZE(h));
  38550. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ENEXT(h));
  38551. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_ASIZE(h));
  38552. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_GET_HSIZE(h));
  38553. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  38554. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HOBJECT_E_GET_FLAGS(heap, h, i));
  38555. k = DUK_HOBJECT_E_GET_KEY(heap, h, i);
  38556. duk_debug_write_heapptr(thr, (duk_heaphdr *) k);
  38557. if (k == NULL) {
  38558. duk_debug_write_int(thr, 0); /* isAccessor */
  38559. duk_debug_write_unused(thr);
  38560. continue;
  38561. }
  38562. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, h, i)) {
  38563. duk_debug_write_int(thr, 1); /* isAccessor */
  38564. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.get);
  38565. duk_debug_write_heapptr(thr, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.set);
  38566. } else {
  38567. duk_debug_write_int(thr, 0); /* isAccessor */
  38568. duk__debug_write_tval_heapptr(thr, &DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->v);
  38569. }
  38570. }
  38571. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  38572. /* Note: array dump will include elements beyond
  38573. * 'length'.
  38574. */
  38575. duk__debug_write_tval_heapptr(thr, DUK_HOBJECT_A_GET_VALUE_PTR(heap, h, i));
  38576. }
  38577. break;
  38578. }
  38579. case DUK_HTYPE_BUFFER: {
  38580. duk_hbuffer *h = (duk_hbuffer *) hdr;
  38581. duk_debug_write_uint(thr, (duk_uint32_t) DUK_HBUFFER_GET_SIZE(h));
  38582. duk_debug_write_buffer(thr, (const char *) DUK_HBUFFER_GET_DATA_PTR(heap, h), (duk_size_t) DUK_HBUFFER_GET_SIZE(h));
  38583. break;
  38584. }
  38585. default: {
  38586. DUK_D(DUK_DPRINT("invalid htype: %d", (int) DUK_HEAPHDR_GET_TYPE(hdr)));
  38587. }
  38588. }
  38589. }
  38590. DUK_LOCAL void duk__debug_dump_heap_allocated(duk_hthread *thr, duk_heap *heap) {
  38591. duk_heaphdr *hdr;
  38592. hdr = heap->heap_allocated;
  38593. while (hdr != NULL) {
  38594. duk__debug_dump_heaphdr(thr, heap, hdr);
  38595. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  38596. }
  38597. }
  38598. DUK_LOCAL void duk__debug_dump_strtab(duk_hthread *thr, duk_heap *heap) {
  38599. duk_uint32_t i;
  38600. duk_hstring *h;
  38601. for (i = 0; i < heap->st_size; i++) {
  38602. #if defined(DUK_USE_STRTAB_PTRCOMP)
  38603. h = DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, heap->strtable16[i]);
  38604. #else
  38605. h = heap->strtable[i];
  38606. #endif
  38607. while (h != NULL) {
  38608. duk__debug_dump_heaphdr(thr, heap, (duk_heaphdr *) h);
  38609. h = h->hdr.h_next;
  38610. }
  38611. }
  38612. }
  38613. DUK_LOCAL void duk__debug_handle_dump_heap(duk_hthread *thr, duk_heap *heap) {
  38614. DUK_D(DUK_DPRINT("debug command DumpHeap"));
  38615. duk_debug_write_reply(thr);
  38616. duk__debug_dump_heap_allocated(thr, heap);
  38617. duk__debug_dump_strtab(thr, heap);
  38618. duk_debug_write_eom(thr);
  38619. }
  38620. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  38621. DUK_LOCAL void duk__debug_handle_get_bytecode(duk_hthread *thr, duk_heap *heap) {
  38622. duk_activation *act;
  38623. duk_hcompfunc *fun = NULL;
  38624. duk_size_t i, n;
  38625. duk_tval *tv;
  38626. duk_hobject **fn;
  38627. duk_int32_t level = -1;
  38628. duk_uint8_t ibyte;
  38629. DUK_UNREF(heap);
  38630. DUK_D(DUK_DPRINT("debug command GetBytecode"));
  38631. ibyte = duk_debug_peek_byte(thr);
  38632. if (ibyte != DUK_DBG_IB_EOM) {
  38633. tv = duk_debug_read_tval(thr);
  38634. if (tv == NULL) {
  38635. /* detached */
  38636. return;
  38637. }
  38638. if (DUK_TVAL_IS_OBJECT(tv)) {
  38639. /* tentative, checked later */
  38640. fun = (duk_hcompfunc *) DUK_TVAL_GET_OBJECT(tv);
  38641. DUK_ASSERT(fun != NULL);
  38642. } else if (DUK_TVAL_IS_NUMBER(tv)) {
  38643. level = (duk_int32_t) DUK_TVAL_GET_NUMBER(tv);
  38644. } else {
  38645. DUK_D(DUK_DPRINT("invalid argument to GetBytecode: %!T", tv));
  38646. goto fail_args;
  38647. }
  38648. }
  38649. if (fun == NULL) {
  38650. act = duk_hthread_get_activation_for_level(thr, level);
  38651. if (act == NULL) {
  38652. goto fail_index;
  38653. }
  38654. fun = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  38655. }
  38656. if (fun == NULL || !DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) fun)) {
  38657. DUK_D(DUK_DPRINT("invalid argument to GetBytecode: %!O", fun));
  38658. goto fail_args;
  38659. }
  38660. DUK_ASSERT(fun != NULL && DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) fun));
  38661. duk_debug_write_reply(thr);
  38662. n = DUK_HCOMPFUNC_GET_CONSTS_COUNT(heap, fun);
  38663. duk_debug_write_int(thr, (duk_int32_t) n);
  38664. tv = DUK_HCOMPFUNC_GET_CONSTS_BASE(heap, fun);
  38665. for (i = 0; i < n; i++) {
  38666. duk_debug_write_tval(thr, tv);
  38667. tv++;
  38668. }
  38669. n = DUK_HCOMPFUNC_GET_FUNCS_COUNT(heap, fun);
  38670. duk_debug_write_int(thr, (duk_int32_t) n);
  38671. fn = DUK_HCOMPFUNC_GET_FUNCS_BASE(heap, fun);
  38672. for (i = 0; i < n; i++) {
  38673. duk_debug_write_hobject(thr, *fn);
  38674. fn++;
  38675. }
  38676. duk_debug_write_string(thr,
  38677. (const char *) DUK_HCOMPFUNC_GET_CODE_BASE(heap, fun),
  38678. (duk_size_t) DUK_HCOMPFUNC_GET_CODE_SIZE(heap, fun));
  38679. duk_debug_write_eom(thr);
  38680. return;
  38681. fail_args:
  38682. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNKNOWN, "invalid argument");
  38683. return;
  38684. fail_index:
  38685. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "invalid callstack index");
  38686. return;
  38687. }
  38688. /*
  38689. * Object inspection commands: GetHeapObjInfo, GetObjPropDesc,
  38690. * GetObjPropDescRange
  38691. */
  38692. #if defined(DUK_USE_DEBUGGER_INSPECT)
  38693. #if 0 /* pruned */
  38694. DUK_LOCAL const char * const duk__debug_getinfo_heaphdr_keys[] = {
  38695. "reachable",
  38696. "temproot",
  38697. "finalizable",
  38698. "finalized",
  38699. "readonly"
  38700. /* NULL not needed here */
  38701. };
  38702. DUK_LOCAL duk_uint_t duk__debug_getinfo_heaphdr_masks[] = {
  38703. DUK_HEAPHDR_FLAG_REACHABLE,
  38704. DUK_HEAPHDR_FLAG_TEMPROOT,
  38705. DUK_HEAPHDR_FLAG_FINALIZABLE,
  38706. DUK_HEAPHDR_FLAG_FINALIZED,
  38707. DUK_HEAPHDR_FLAG_READONLY,
  38708. 0 /* terminator */
  38709. };
  38710. #endif
  38711. DUK_LOCAL const char * const duk__debug_getinfo_hstring_keys[] = {
  38712. #if 0
  38713. "arridx",
  38714. "symbol",
  38715. "hidden",
  38716. "reserved_word",
  38717. "strict_reserved_word",
  38718. "eval_or_arguments",
  38719. #endif
  38720. "extdata"
  38721. /* NULL not needed here */
  38722. };
  38723. DUK_LOCAL duk_uint_t duk__debug_getinfo_hstring_masks[] = {
  38724. #if 0
  38725. DUK_HSTRING_FLAG_ARRIDX,
  38726. DUK_HSTRING_FLAG_SYMBOL,
  38727. DUK_HSTRING_FLAG_HIDDEN,
  38728. DUK_HSTRING_FLAG_RESERVED_WORD,
  38729. DUK_HSTRING_FLAG_STRICT_RESERVED_WORD,
  38730. DUK_HSTRING_FLAG_EVAL_OR_ARGUMENTS,
  38731. #endif
  38732. DUK_HSTRING_FLAG_EXTDATA,
  38733. 0 /* terminator */
  38734. };
  38735. DUK_LOCAL const char * const duk__debug_getinfo_hobject_keys[] = {
  38736. "extensible",
  38737. "constructable",
  38738. "callable",
  38739. "boundfunc",
  38740. "compfunc",
  38741. "natfunc",
  38742. "bufobj",
  38743. "fastrefs",
  38744. "array_part",
  38745. "strict",
  38746. "notail",
  38747. "newenv",
  38748. "namebinding",
  38749. "createargs",
  38750. "have_finalizer",
  38751. "exotic_array",
  38752. "exotic_stringobj",
  38753. "exotic_arguments",
  38754. "exotic_proxyobj",
  38755. "special_call"
  38756. /* NULL not needed here */
  38757. };
  38758. DUK_LOCAL duk_uint_t duk__debug_getinfo_hobject_masks[] = {
  38759. DUK_HOBJECT_FLAG_EXTENSIBLE,
  38760. DUK_HOBJECT_FLAG_CONSTRUCTABLE,
  38761. DUK_HOBJECT_FLAG_CALLABLE,
  38762. DUK_HOBJECT_FLAG_BOUNDFUNC,
  38763. DUK_HOBJECT_FLAG_COMPFUNC,
  38764. DUK_HOBJECT_FLAG_NATFUNC,
  38765. DUK_HOBJECT_FLAG_BUFOBJ,
  38766. DUK_HOBJECT_FLAG_FASTREFS,
  38767. DUK_HOBJECT_FLAG_ARRAY_PART,
  38768. DUK_HOBJECT_FLAG_STRICT,
  38769. DUK_HOBJECT_FLAG_NOTAIL,
  38770. DUK_HOBJECT_FLAG_NEWENV,
  38771. DUK_HOBJECT_FLAG_NAMEBINDING,
  38772. DUK_HOBJECT_FLAG_CREATEARGS,
  38773. DUK_HOBJECT_FLAG_HAVE_FINALIZER,
  38774. DUK_HOBJECT_FLAG_EXOTIC_ARRAY,
  38775. DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ,
  38776. DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS,
  38777. DUK_HOBJECT_FLAG_EXOTIC_PROXYOBJ,
  38778. DUK_HOBJECT_FLAG_SPECIAL_CALL,
  38779. 0 /* terminator */
  38780. };
  38781. DUK_LOCAL const char * const duk__debug_getinfo_hbuffer_keys[] = {
  38782. "dynamic",
  38783. "external"
  38784. /* NULL not needed here */
  38785. };
  38786. DUK_LOCAL duk_uint_t duk__debug_getinfo_hbuffer_masks[] = {
  38787. DUK_HBUFFER_FLAG_DYNAMIC,
  38788. DUK_HBUFFER_FLAG_EXTERNAL,
  38789. 0 /* terminator */
  38790. };
  38791. DUK_LOCAL void duk__debug_getinfo_flags_key(duk_hthread *thr, const char *key) {
  38792. duk_debug_write_uint(thr, 0);
  38793. duk_debug_write_cstring(thr, key);
  38794. }
  38795. DUK_LOCAL void duk__debug_getinfo_prop_uint(duk_hthread *thr, const char *key, duk_uint_t val) {
  38796. duk_debug_write_uint(thr, 0);
  38797. duk_debug_write_cstring(thr, key);
  38798. duk_debug_write_uint(thr, val);
  38799. }
  38800. DUK_LOCAL void duk__debug_getinfo_prop_int(duk_hthread *thr, const char *key, duk_int_t val) {
  38801. duk_debug_write_uint(thr, 0);
  38802. duk_debug_write_cstring(thr, key);
  38803. duk_debug_write_int(thr, val);
  38804. }
  38805. DUK_LOCAL void duk__debug_getinfo_prop_bool(duk_hthread *thr, const char *key, duk_bool_t val) {
  38806. duk_debug_write_uint(thr, 0);
  38807. duk_debug_write_cstring(thr, key);
  38808. duk_debug_write_boolean(thr, val);
  38809. }
  38810. DUK_LOCAL void duk__debug_getinfo_bitmask(duk_hthread *thr, const char * const * keys, duk_uint_t *masks, duk_uint_t flags) {
  38811. const char *key;
  38812. duk_uint_t mask;
  38813. for (;;) {
  38814. mask = *masks++;
  38815. if (mask == 0) {
  38816. break;
  38817. }
  38818. key = *keys++;
  38819. DUK_ASSERT(key != NULL);
  38820. DUK_DD(DUK_DDPRINT("inspect bitmask: key=%s, mask=0x%08lx, flags=0x%08lx", key, (unsigned long) mask, (unsigned long) flags));
  38821. duk__debug_getinfo_prop_bool(thr, key, flags & mask);
  38822. }
  38823. }
  38824. /* Inspect a property using a virtual index into a conceptual property list
  38825. * consisting of (1) all array part items from [0,a_size[ (even when above
  38826. * .length) and (2) all entry part items from [0,e_next[. Unused slots are
  38827. * indicated using dvalue 'unused'.
  38828. */
  38829. DUK_LOCAL duk_bool_t duk__debug_getprop_index(duk_hthread *thr, duk_heap *heap, duk_hobject *h_obj, duk_uint_t idx) {
  38830. duk_uint_t a_size;
  38831. duk_tval *tv;
  38832. duk_hstring *h_key;
  38833. duk_hobject *h_getset;
  38834. duk_uint_t flags;
  38835. DUK_UNREF(heap);
  38836. a_size = DUK_HOBJECT_GET_ASIZE(h_obj);
  38837. if (idx < a_size) {
  38838. duk_debug_write_uint(thr, DUK_PROPDESC_FLAGS_WEC);
  38839. duk_debug_write_uint(thr, idx);
  38840. tv = DUK_HOBJECT_A_GET_VALUE_PTR(heap, h_obj, idx);
  38841. duk_debug_write_tval(thr, tv);
  38842. return 1;
  38843. }
  38844. idx -= a_size;
  38845. if (idx >= DUK_HOBJECT_GET_ENEXT(h_obj)) {
  38846. return 0;
  38847. }
  38848. h_key = DUK_HOBJECT_E_GET_KEY(heap, h_obj, idx);
  38849. if (h_key == NULL) {
  38850. duk_debug_write_uint(thr, 0);
  38851. duk_debug_write_null(thr);
  38852. duk_debug_write_unused(thr);
  38853. return 1;
  38854. }
  38855. flags = DUK_HOBJECT_E_GET_FLAGS(heap, h_obj, idx);
  38856. if (DUK_HSTRING_HAS_SYMBOL(h_key)) {
  38857. flags |= DUK_DBG_PROPFLAG_SYMBOL;
  38858. }
  38859. if (DUK_HSTRING_HAS_HIDDEN(h_key)) {
  38860. flags |= DUK_DBG_PROPFLAG_HIDDEN;
  38861. }
  38862. duk_debug_write_uint(thr, flags);
  38863. duk_debug_write_hstring(thr, h_key);
  38864. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  38865. h_getset = DUK_HOBJECT_E_GET_VALUE_GETTER(heap, h_obj, idx);
  38866. if (h_getset) {
  38867. duk_debug_write_hobject(thr, h_getset);
  38868. } else {
  38869. duk_debug_write_null(thr);
  38870. }
  38871. h_getset = DUK_HOBJECT_E_GET_VALUE_SETTER(heap, h_obj, idx);
  38872. if (h_getset) {
  38873. duk_debug_write_hobject(thr, h_getset);
  38874. } else {
  38875. duk_debug_write_null(thr);
  38876. }
  38877. } else {
  38878. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, h_obj, idx);
  38879. duk_debug_write_tval(thr, tv);
  38880. }
  38881. return 1;
  38882. }
  38883. DUK_LOCAL void duk__debug_handle_get_heap_obj_info(duk_hthread *thr, duk_heap *heap) {
  38884. duk_heaphdr *h;
  38885. DUK_D(DUK_DPRINT("debug command GetHeapObjInfo"));
  38886. DUK_UNREF(heap);
  38887. DUK_ASSERT(sizeof(duk__debug_getinfo_hstring_keys) / sizeof(const char *) == sizeof(duk__debug_getinfo_hstring_masks) / sizeof(duk_uint_t) - 1);
  38888. DUK_ASSERT(sizeof(duk__debug_getinfo_hobject_keys) / sizeof(const char *) == sizeof(duk__debug_getinfo_hobject_masks) / sizeof(duk_uint_t) - 1);
  38889. DUK_ASSERT(sizeof(duk__debug_getinfo_hbuffer_keys) / sizeof(const char *) == sizeof(duk__debug_getinfo_hbuffer_masks) / sizeof(duk_uint_t) - 1);
  38890. h = duk_debug_read_any_ptr(thr);
  38891. if (!h) {
  38892. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNKNOWN, "invalid target");
  38893. return;
  38894. }
  38895. duk_debug_write_reply(thr);
  38896. /* As with all inspection code, we rely on the debug client providing
  38897. * a valid, non-stale pointer: there's no portable way to safely
  38898. * validate the pointer here.
  38899. */
  38900. duk__debug_getinfo_flags_key(thr, "heapptr");
  38901. duk_debug_write_heapptr(thr, h);
  38902. /* XXX: comes out as signed now */
  38903. duk__debug_getinfo_prop_uint(thr, "heaphdr_flags", (duk_uint_t) DUK_HEAPHDR_GET_FLAGS(h));
  38904. duk__debug_getinfo_prop_uint(thr, "heaphdr_type", (duk_uint_t) DUK_HEAPHDR_GET_TYPE(h));
  38905. #if defined(DUK_USE_REFERENCE_COUNTING)
  38906. duk__debug_getinfo_prop_uint(thr, "refcount", (duk_uint_t) DUK_HEAPHDR_GET_REFCOUNT(h));
  38907. #endif
  38908. #if 0 /* pruned */
  38909. duk__debug_getinfo_bitmask(thr,
  38910. duk__debug_getinfo_heaphdr_keys,
  38911. duk__debug_getinfo_heaphdr_masks,
  38912. DUK_HEAPHDR_GET_FLAGS_RAW(h));
  38913. #endif
  38914. switch (DUK_HEAPHDR_GET_TYPE(h)) {
  38915. case DUK_HTYPE_STRING: {
  38916. duk_hstring *h_str;
  38917. h_str = (duk_hstring *) h;
  38918. duk__debug_getinfo_bitmask(thr,
  38919. duk__debug_getinfo_hstring_keys,
  38920. duk__debug_getinfo_hstring_masks,
  38921. DUK_HEAPHDR_GET_FLAGS_RAW(h));
  38922. duk__debug_getinfo_prop_uint(thr, "bytelen", (duk_uint_t) DUK_HSTRING_GET_BYTELEN(h_str));
  38923. duk__debug_getinfo_prop_uint(thr, "charlen", (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h_str));
  38924. duk__debug_getinfo_prop_uint(thr, "hash", (duk_uint_t) DUK_HSTRING_GET_HASH(h_str));
  38925. duk__debug_getinfo_flags_key(thr, "data");
  38926. duk_debug_write_hstring(thr, h_str);
  38927. break;
  38928. }
  38929. case DUK_HTYPE_OBJECT: {
  38930. duk_hobject *h_obj;
  38931. duk_hobject *h_proto;
  38932. h_obj = (duk_hobject *) h;
  38933. h_proto = DUK_HOBJECT_GET_PROTOTYPE(heap, h_obj);
  38934. /* duk_hobject specific fields. */
  38935. duk__debug_getinfo_bitmask(thr,
  38936. duk__debug_getinfo_hobject_keys,
  38937. duk__debug_getinfo_hobject_masks,
  38938. DUK_HEAPHDR_GET_FLAGS_RAW(h));
  38939. duk__debug_getinfo_prop_uint(thr, "class_number", DUK_HOBJECT_GET_CLASS_NUMBER(h_obj));
  38940. duk__debug_getinfo_flags_key(thr, "class_name");
  38941. duk_debug_write_hstring(thr, DUK_HOBJECT_GET_CLASS_STRING(heap, h_obj));
  38942. duk__debug_getinfo_flags_key(thr, "prototype");
  38943. if (h_proto != NULL) {
  38944. duk_debug_write_hobject(thr, h_proto);
  38945. } else {
  38946. duk_debug_write_null(thr);
  38947. }
  38948. duk__debug_getinfo_flags_key(thr, "props");
  38949. duk_debug_write_pointer(thr, (void *) DUK_HOBJECT_GET_PROPS(heap, h_obj));
  38950. duk__debug_getinfo_prop_uint(thr, "e_size", (duk_uint_t) DUK_HOBJECT_GET_ESIZE(h_obj));
  38951. duk__debug_getinfo_prop_uint(thr, "e_next", (duk_uint_t) DUK_HOBJECT_GET_ENEXT(h_obj));
  38952. duk__debug_getinfo_prop_uint(thr, "a_size", (duk_uint_t) DUK_HOBJECT_GET_ASIZE(h_obj));
  38953. duk__debug_getinfo_prop_uint(thr, "h_size", (duk_uint_t) DUK_HOBJECT_GET_HSIZE(h_obj));
  38954. if (DUK_HOBJECT_IS_ARRAY(h_obj)) {
  38955. duk_harray *h_arr;
  38956. h_arr = (duk_harray *) h_obj;
  38957. duk__debug_getinfo_prop_uint(thr, "length", (duk_uint_t) h_arr->length);
  38958. duk__debug_getinfo_prop_bool(thr, "length_nonwritable", h_arr->length_nonwritable);
  38959. }
  38960. if (DUK_HOBJECT_IS_NATFUNC(h_obj)) {
  38961. duk_hnatfunc *h_fun;
  38962. h_fun = (duk_hnatfunc *) h_obj;
  38963. duk__debug_getinfo_prop_int(thr, "nargs", h_fun->nargs);
  38964. duk__debug_getinfo_prop_int(thr, "magic", h_fun->magic);
  38965. duk__debug_getinfo_prop_bool(thr, "varargs", h_fun->magic == DUK_HNATFUNC_NARGS_VARARGS);
  38966. /* Native function pointer may be different from a void pointer,
  38967. * and we serialize it from memory directly now (no byte swapping etc).
  38968. */
  38969. duk__debug_getinfo_flags_key(thr, "funcptr");
  38970. duk_debug_write_buffer(thr, (const char *) &h_fun->func, sizeof(h_fun->func));
  38971. }
  38972. if (DUK_HOBJECT_IS_COMPFUNC(h_obj)) {
  38973. duk_hcompfunc *h_fun;
  38974. duk_hbuffer *h_buf;
  38975. duk_hobject *h_lexenv;
  38976. duk_hobject *h_varenv;
  38977. h_fun = (duk_hcompfunc *) h_obj;
  38978. duk__debug_getinfo_prop_int(thr, "nregs", h_fun->nregs);
  38979. duk__debug_getinfo_prop_int(thr, "nargs", h_fun->nargs);
  38980. duk__debug_getinfo_flags_key(thr, "lex_env");
  38981. h_lexenv = DUK_HCOMPFUNC_GET_LEXENV(thr->heap, h_fun);
  38982. if (h_lexenv != NULL) {
  38983. duk_debug_write_hobject(thr, h_lexenv);
  38984. } else {
  38985. duk_debug_write_null(thr);
  38986. }
  38987. duk__debug_getinfo_flags_key(thr, "var_env");
  38988. h_varenv = DUK_HCOMPFUNC_GET_VARENV(thr->heap, h_fun);
  38989. if (h_varenv != NULL) {
  38990. duk_debug_write_hobject(thr, h_varenv);
  38991. } else {
  38992. duk_debug_write_null(thr);
  38993. }
  38994. duk__debug_getinfo_prop_uint(thr, "start_line", h_fun->start_line);
  38995. duk__debug_getinfo_prop_uint(thr, "end_line", h_fun->end_line);
  38996. h_buf = (duk_hbuffer *) DUK_HCOMPFUNC_GET_DATA(thr->heap, h_fun);
  38997. if (h_buf != NULL) {
  38998. duk__debug_getinfo_flags_key(thr, "data");
  38999. duk_debug_write_heapptr(thr, (duk_heaphdr *) h_buf);
  39000. }
  39001. }
  39002. if (DUK_HOBJECT_IS_BOUNDFUNC(h_obj)) {
  39003. duk_hboundfunc *h_bfun;
  39004. h_bfun = (duk_hboundfunc *) (void *) h_obj;
  39005. duk__debug_getinfo_flags_key(thr, "target");
  39006. duk_debug_write_tval(thr, &h_bfun->target);
  39007. duk__debug_getinfo_flags_key(thr, "this_binding");
  39008. duk_debug_write_tval(thr, &h_bfun->this_binding);
  39009. duk__debug_getinfo_flags_key(thr, "nargs");
  39010. duk_debug_write_int(thr, h_bfun->nargs);
  39011. /* h_bfun->args not exposed now */
  39012. }
  39013. if (DUK_HOBJECT_IS_THREAD(h_obj)) {
  39014. /* XXX: Currently no inspection of threads, e.g. value stack, call
  39015. * stack, catch stack, etc.
  39016. */
  39017. duk_hthread *h_thr;
  39018. h_thr = (duk_hthread *) h_obj;
  39019. DUK_UNREF(h_thr);
  39020. }
  39021. if (DUK_HOBJECT_IS_DECENV(h_obj)) {
  39022. duk_hdecenv *h_env;
  39023. h_env = (duk_hdecenv *) h_obj;
  39024. duk__debug_getinfo_flags_key(thr, "thread");
  39025. duk_debug_write_heapptr(thr, (duk_heaphdr *) (h_env->thread));
  39026. duk__debug_getinfo_flags_key(thr, "varmap");
  39027. duk_debug_write_heapptr(thr, (duk_heaphdr *) (h_env->varmap));
  39028. duk__debug_getinfo_prop_uint(thr, "regbase", (duk_uint_t) h_env->regbase_byteoff);
  39029. }
  39030. if (DUK_HOBJECT_IS_OBJENV(h_obj)) {
  39031. duk_hobjenv *h_env;
  39032. h_env = (duk_hobjenv *) h_obj;
  39033. duk__debug_getinfo_flags_key(thr, "target");
  39034. duk_debug_write_heapptr(thr, (duk_heaphdr *) (h_env->target));
  39035. duk__debug_getinfo_prop_bool(thr, "has_this", h_env->has_this);
  39036. }
  39037. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  39038. if (DUK_HOBJECT_IS_BUFOBJ(h_obj)) {
  39039. duk_hbufobj *h_bufobj;
  39040. h_bufobj = (duk_hbufobj *) h_obj;
  39041. duk__debug_getinfo_prop_uint(thr, "slice_offset", h_bufobj->offset);
  39042. duk__debug_getinfo_prop_uint(thr, "slice_length", h_bufobj->length);
  39043. duk__debug_getinfo_prop_uint(thr, "elem_shift", (duk_uint_t) h_bufobj->shift);
  39044. duk__debug_getinfo_prop_uint(thr, "elem_type", (duk_uint_t) h_bufobj->elem_type);
  39045. duk__debug_getinfo_prop_bool(thr, "is_typedarray", (duk_uint_t) h_bufobj->is_typedarray);
  39046. if (h_bufobj->buf != NULL) {
  39047. duk__debug_getinfo_flags_key(thr, "buffer");
  39048. duk_debug_write_heapptr(thr, (duk_heaphdr *) h_bufobj->buf);
  39049. }
  39050. }
  39051. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  39052. break;
  39053. }
  39054. case DUK_HTYPE_BUFFER: {
  39055. duk_hbuffer *h_buf;
  39056. h_buf = (duk_hbuffer *) h;
  39057. duk__debug_getinfo_bitmask(thr,
  39058. duk__debug_getinfo_hbuffer_keys,
  39059. duk__debug_getinfo_hbuffer_masks,
  39060. DUK_HEAPHDR_GET_FLAGS_RAW(h));
  39061. duk__debug_getinfo_prop_uint(thr, "size", (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_buf));
  39062. duk__debug_getinfo_flags_key(thr, "dataptr");
  39063. duk_debug_write_pointer(thr, (void *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_buf));
  39064. duk__debug_getinfo_flags_key(thr, "data");
  39065. duk_debug_write_hbuffer(thr, h_buf); /* tolerates NULL h_buf */
  39066. break;
  39067. }
  39068. default: {
  39069. /* Since we already started writing the reply, just emit nothing. */
  39070. DUK_D(DUK_DPRINT("inspect target pointer has invalid heaphdr type"));
  39071. }
  39072. }
  39073. duk_debug_write_eom(thr);
  39074. }
  39075. DUK_LOCAL void duk__debug_handle_get_obj_prop_desc(duk_hthread *thr, duk_heap *heap) {
  39076. duk_heaphdr *h;
  39077. duk_hobject *h_obj;
  39078. duk_hstring *h_key;
  39079. duk_propdesc desc;
  39080. DUK_D(DUK_DPRINT("debug command GetObjPropDesc"));
  39081. DUK_UNREF(heap);
  39082. h = duk_debug_read_any_ptr(thr);
  39083. if (!h) {
  39084. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNKNOWN, "invalid target");
  39085. return;
  39086. }
  39087. h_key = duk_debug_read_hstring(thr);
  39088. if (h == NULL || DUK_HEAPHDR_GET_TYPE(h) != DUK_HTYPE_OBJECT || h_key == NULL) {
  39089. goto fail_args;
  39090. }
  39091. h_obj = (duk_hobject *) h;
  39092. if (duk_hobject_get_own_propdesc(thr, h_obj, h_key, &desc, 0 /*flags*/)) {
  39093. duk_int_t virtual_idx;
  39094. duk_bool_t rc;
  39095. /* To use the shared helper need the virtual index. */
  39096. DUK_ASSERT(desc.e_idx >= 0 || desc.a_idx >= 0);
  39097. virtual_idx = (desc.a_idx >= 0 ? desc.a_idx :
  39098. (duk_int_t) DUK_HOBJECT_GET_ASIZE(h_obj) + desc.e_idx);
  39099. duk_debug_write_reply(thr);
  39100. rc = duk__debug_getprop_index(thr, heap, h_obj, (duk_uint_t) virtual_idx);
  39101. DUK_ASSERT(rc == 1);
  39102. DUK_UNREF(rc);
  39103. duk_debug_write_eom(thr);
  39104. } else {
  39105. duk_debug_write_error_eom(thr, DUK_DBG_ERR_NOTFOUND, "not found");
  39106. }
  39107. return;
  39108. fail_args:
  39109. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNKNOWN, "invalid args");
  39110. }
  39111. DUK_LOCAL void duk__debug_handle_get_obj_prop_desc_range(duk_hthread *thr, duk_heap *heap) {
  39112. duk_heaphdr *h;
  39113. duk_hobject *h_obj;
  39114. duk_uint_t idx, idx_start, idx_end;
  39115. DUK_D(DUK_DPRINT("debug command GetObjPropDescRange"));
  39116. DUK_UNREF(heap);
  39117. h = duk_debug_read_any_ptr(thr);
  39118. idx_start = (duk_uint_t) duk_debug_read_int(thr);
  39119. idx_end = (duk_uint_t) duk_debug_read_int(thr);
  39120. if (h == NULL || DUK_HEAPHDR_GET_TYPE(h) != DUK_HTYPE_OBJECT) {
  39121. goto fail_args;
  39122. }
  39123. h_obj = (duk_hobject *) h;
  39124. /* The index range space is conceptually the array part followed by the
  39125. * entry part. Unlike normal enumeration all slots are exposed here as
  39126. * is and return 'unused' if the slots are not in active use. In particular
  39127. * the array part is included for the full a_size regardless of what the
  39128. * array .length is.
  39129. */
  39130. duk_debug_write_reply(thr);
  39131. for (idx = idx_start; idx < idx_end; idx++) {
  39132. if (!duk__debug_getprop_index(thr, heap, h_obj, idx)) {
  39133. break;
  39134. }
  39135. }
  39136. duk_debug_write_eom(thr);
  39137. return;
  39138. fail_args:
  39139. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNKNOWN, "invalid args");
  39140. }
  39141. #endif /* DUK_USE_DEBUGGER_INSPECT */
  39142. /*
  39143. * Process incoming debug requests
  39144. *
  39145. * Individual request handlers can push temporaries on the value stack and
  39146. * rely on duk__debug_process_message() to restore the value stack top
  39147. * automatically.
  39148. */
  39149. /* Process one debug message. Automatically restore value stack top to its
  39150. * entry value, so that individual message handlers don't need exact value
  39151. * stack handling which is convenient.
  39152. */
  39153. DUK_LOCAL void duk__debug_process_message(duk_hthread *thr) {
  39154. duk_heap *heap;
  39155. duk_uint8_t x;
  39156. duk_int32_t cmd;
  39157. duk_idx_t entry_top;
  39158. DUK_ASSERT(thr != NULL);
  39159. heap = thr->heap;
  39160. DUK_ASSERT(heap != NULL);
  39161. entry_top = duk_get_top(thr);
  39162. x = duk_debug_read_byte(thr);
  39163. switch (x) {
  39164. case DUK_DBG_IB_REQUEST: {
  39165. cmd = duk_debug_read_int(thr);
  39166. switch (cmd) {
  39167. case DUK_DBG_CMD_BASICINFO: {
  39168. duk__debug_handle_basic_info(thr, heap);
  39169. break;
  39170. }
  39171. case DUK_DBG_CMD_TRIGGERSTATUS: {
  39172. duk__debug_handle_trigger_status(thr, heap);
  39173. break;
  39174. }
  39175. case DUK_DBG_CMD_PAUSE: {
  39176. duk__debug_handle_pause(thr, heap);
  39177. break;
  39178. }
  39179. case DUK_DBG_CMD_RESUME: {
  39180. duk__debug_handle_resume(thr, heap);
  39181. break;
  39182. }
  39183. case DUK_DBG_CMD_STEPINTO:
  39184. case DUK_DBG_CMD_STEPOVER:
  39185. case DUK_DBG_CMD_STEPOUT: {
  39186. duk__debug_handle_step(thr, heap, cmd);
  39187. break;
  39188. }
  39189. case DUK_DBG_CMD_LISTBREAK: {
  39190. duk__debug_handle_list_break(thr, heap);
  39191. break;
  39192. }
  39193. case DUK_DBG_CMD_ADDBREAK: {
  39194. duk__debug_handle_add_break(thr, heap);
  39195. break;
  39196. }
  39197. case DUK_DBG_CMD_DELBREAK: {
  39198. duk__debug_handle_del_break(thr, heap);
  39199. break;
  39200. }
  39201. case DUK_DBG_CMD_GETVAR: {
  39202. duk__debug_handle_get_var(thr, heap);
  39203. break;
  39204. }
  39205. case DUK_DBG_CMD_PUTVAR: {
  39206. duk__debug_handle_put_var(thr, heap);
  39207. break;
  39208. }
  39209. case DUK_DBG_CMD_GETCALLSTACK: {
  39210. duk__debug_handle_get_call_stack(thr, heap);
  39211. break;
  39212. }
  39213. case DUK_DBG_CMD_GETLOCALS: {
  39214. duk__debug_handle_get_locals(thr, heap);
  39215. break;
  39216. }
  39217. case DUK_DBG_CMD_EVAL: {
  39218. duk__debug_handle_eval(thr, heap);
  39219. break;
  39220. }
  39221. case DUK_DBG_CMD_DETACH: {
  39222. /* The actual detached_cb call is postponed to message loop so
  39223. * we don't need any special precautions here (just skip to EOM
  39224. * on the already closed connection).
  39225. */
  39226. duk__debug_handle_detach(thr, heap);
  39227. break;
  39228. }
  39229. #if defined(DUK_USE_DEBUGGER_DUMPHEAP)
  39230. case DUK_DBG_CMD_DUMPHEAP: {
  39231. duk__debug_handle_dump_heap(thr, heap);
  39232. break;
  39233. }
  39234. #endif /* DUK_USE_DEBUGGER_DUMPHEAP */
  39235. case DUK_DBG_CMD_GETBYTECODE: {
  39236. duk__debug_handle_get_bytecode(thr, heap);
  39237. break;
  39238. }
  39239. case DUK_DBG_CMD_APPREQUEST: {
  39240. duk__debug_handle_apprequest(thr, heap);
  39241. break;
  39242. }
  39243. #if defined(DUK_USE_DEBUGGER_INSPECT)
  39244. case DUK_DBG_CMD_GETHEAPOBJINFO: {
  39245. duk__debug_handle_get_heap_obj_info(thr, heap);
  39246. break;
  39247. }
  39248. case DUK_DBG_CMD_GETOBJPROPDESC: {
  39249. duk__debug_handle_get_obj_prop_desc(thr, heap);
  39250. break;
  39251. }
  39252. case DUK_DBG_CMD_GETOBJPROPDESCRANGE: {
  39253. duk__debug_handle_get_obj_prop_desc_range(thr, heap);
  39254. break;
  39255. }
  39256. #endif /* DUK_USE_DEBUGGER_INSPECT */
  39257. default: {
  39258. DUK_D(DUK_DPRINT("debug command unsupported: %d", (int) cmd));
  39259. duk_debug_write_error_eom(thr, DUK_DBG_ERR_UNSUPPORTED, "unsupported command");
  39260. }
  39261. } /* switch cmd */
  39262. break;
  39263. }
  39264. case DUK_DBG_IB_REPLY: {
  39265. DUK_D(DUK_DPRINT("debug reply, skipping"));
  39266. break;
  39267. }
  39268. case DUK_DBG_IB_ERROR: {
  39269. DUK_D(DUK_DPRINT("debug error, skipping"));
  39270. break;
  39271. }
  39272. case DUK_DBG_IB_NOTIFY: {
  39273. DUK_D(DUK_DPRINT("debug notify, skipping"));
  39274. break;
  39275. }
  39276. default: {
  39277. DUK_D(DUK_DPRINT("invalid initial byte, drop connection: %d", (int) x));
  39278. goto fail;
  39279. }
  39280. } /* switch initial byte */
  39281. DUK_ASSERT(duk_get_top(thr) >= entry_top);
  39282. duk_set_top(thr, entry_top);
  39283. duk__debug_skip_to_eom(thr);
  39284. return;
  39285. fail:
  39286. DUK_ASSERT(duk_get_top(thr) >= entry_top);
  39287. duk_set_top(thr, entry_top);
  39288. DUK__SET_CONN_BROKEN(thr, 1);
  39289. return;
  39290. }
  39291. DUK_LOCAL void duk__check_resend_status(duk_hthread *thr) {
  39292. if (thr->heap->dbg_read_cb != NULL && thr->heap->dbg_state_dirty) {
  39293. duk_debug_send_status(thr);
  39294. thr->heap->dbg_state_dirty = 0;
  39295. }
  39296. }
  39297. DUK_INTERNAL duk_bool_t duk_debug_process_messages(duk_hthread *thr, duk_bool_t no_block) {
  39298. #if defined(DUK_USE_ASSERTIONS)
  39299. duk_idx_t entry_top;
  39300. #endif
  39301. duk_bool_t retval = 0;
  39302. DUK_ASSERT(thr != NULL);
  39303. DUK_ASSERT(thr->heap != NULL);
  39304. #if defined(DUK_USE_ASSERTIONS)
  39305. entry_top = duk_get_top(thr);
  39306. #endif
  39307. DUK_D(DUK_DPRINT("process debug messages: read_cb=%s, no_block=%ld, detaching=%ld, processing=%ld",
  39308. thr->heap->dbg_read_cb ? "not NULL" : "NULL", (long) no_block,
  39309. (long) thr->heap->dbg_detaching, (long) thr->heap->dbg_processing));
  39310. DUK_DD(DUK_DDPRINT("top at entry: %ld", (long) duk_get_top(thr)));
  39311. /* thr->heap->dbg_detaching may be != 0 if a debugger write outside
  39312. * the message loop caused a transport error and detach1() to run.
  39313. */
  39314. DUK_ASSERT(thr->heap->dbg_detaching == 0 || thr->heap->dbg_detaching == 1);
  39315. DUK_ASSERT(thr->heap->dbg_processing == 0);
  39316. thr->heap->dbg_processing = 1;
  39317. /* Ensure dirty state causes a Status even if never process any
  39318. * messages. This is expected by the bytecode executor when in
  39319. * the running state.
  39320. */
  39321. duk__check_resend_status(thr);
  39322. for (;;) {
  39323. /* Process messages until we're no longer paused or we peek
  39324. * and see there's nothing to read right now.
  39325. */
  39326. DUK_DD(DUK_DDPRINT("top at loop top: %ld", (long) duk_get_top(thr)));
  39327. DUK_ASSERT(thr->heap->dbg_processing == 1);
  39328. while (thr->heap->dbg_read_cb == NULL && thr->heap->dbg_detaching) {
  39329. /* Detach is pending; can be triggered from outside the
  39330. * debugger loop (e.g. Status notify write error) or by
  39331. * previous message handling. Call detached callback
  39332. * here, in a controlled state, to ensure a possible
  39333. * reattach inside the detached_cb is handled correctly.
  39334. *
  39335. * Recheck for detach in a while loop: an immediate
  39336. * reattach involves a call to duk_debugger_attach()
  39337. * which writes a debugger handshake line immediately
  39338. * inside the API call. If the transport write fails
  39339. * for that handshake, we can immediately end up in a
  39340. * "transport broken, detaching" case several times here.
  39341. * Loop back until we're either cleanly attached or
  39342. * fully detached.
  39343. *
  39344. * NOTE: Reset dbg_processing = 1 forcibly, in case we
  39345. * re-attached; duk_debugger_attach() sets dbg_processing
  39346. * to 0 at the moment.
  39347. */
  39348. DUK_D(DUK_DPRINT("detach pending (dbg_read_cb == NULL, dbg_detaching != 0), call detach2"));
  39349. duk__debug_do_detach2(thr->heap);
  39350. thr->heap->dbg_processing = 1; /* may be set to 0 by duk_debugger_attach() inside callback */
  39351. DUK_D(DUK_DPRINT("after detach2 (and possible reattach): dbg_read_cb=%s, dbg_detaching=%ld",
  39352. thr->heap->dbg_read_cb ? "not NULL" : "NULL", (long) thr->heap->dbg_detaching));
  39353. }
  39354. DUK_ASSERT(thr->heap->dbg_detaching == 0); /* true even with reattach */
  39355. DUK_ASSERT(thr->heap->dbg_processing == 1); /* even after a detach and possible reattach */
  39356. if (thr->heap->dbg_read_cb == NULL) {
  39357. DUK_D(DUK_DPRINT("debug connection broken (and not detaching), stop processing messages"));
  39358. break;
  39359. }
  39360. if (!DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap) || no_block) {
  39361. if (!duk_debug_read_peek(thr)) {
  39362. /* Note: peek cannot currently trigger a detach
  39363. * so the dbg_detaching == 0 assert outside the
  39364. * loop is correct.
  39365. */
  39366. DUK_D(DUK_DPRINT("processing debug message, peek indicated no data, stop processing messages"));
  39367. break;
  39368. }
  39369. DUK_D(DUK_DPRINT("processing debug message, peek indicated there is data, handle it"));
  39370. } else {
  39371. DUK_D(DUK_DPRINT("paused, process debug message, blocking if necessary"));
  39372. }
  39373. duk__check_resend_status(thr);
  39374. duk__debug_process_message(thr);
  39375. duk__check_resend_status(thr);
  39376. retval = 1; /* processed one or more messages */
  39377. }
  39378. DUK_ASSERT(thr->heap->dbg_detaching == 0);
  39379. DUK_ASSERT(thr->heap->dbg_processing == 1);
  39380. thr->heap->dbg_processing = 0;
  39381. /* As an initial implementation, read flush after exiting the message
  39382. * loop. If transport is broken, this is a no-op (with debug logs).
  39383. */
  39384. duk_debug_read_flush(thr); /* this cannot initiate a detach */
  39385. DUK_ASSERT(thr->heap->dbg_detaching == 0);
  39386. DUK_DD(DUK_DDPRINT("top at exit: %ld", (long) duk_get_top(thr)));
  39387. #if defined(DUK_USE_ASSERTIONS)
  39388. /* Easy to get wrong, so assert for it. */
  39389. DUK_ASSERT(entry_top == duk_get_top(thr));
  39390. #endif
  39391. return retval;
  39392. }
  39393. /*
  39394. * Halt execution helper
  39395. */
  39396. /* Halt execution and enter a debugger message loop until execution is resumed
  39397. * by the client. PC for the current activation may be temporarily decremented
  39398. * so that the "current" instruction will be shown by the client. This helper
  39399. * is callable from anywhere, also outside bytecode executor.
  39400. */
  39401. DUK_INTERNAL void duk_debug_halt_execution(duk_hthread *thr, duk_bool_t use_prev_pc) {
  39402. duk_activation *act;
  39403. duk_hcompfunc *fun;
  39404. duk_instr_t *old_pc = NULL;
  39405. DUK_ASSERT(thr != NULL);
  39406. DUK_ASSERT(thr->heap != NULL);
  39407. DUK_ASSERT(duk_debug_is_attached(thr->heap));
  39408. DUK_ASSERT(thr->heap->dbg_processing == 0);
  39409. DUK_ASSERT(!duk_debug_is_paused(thr->heap));
  39410. duk_debug_set_paused(thr->heap);
  39411. act = thr->callstack_curr;
  39412. /* NOTE: act may be NULL if an error is thrown outside of any activation,
  39413. * which may happen in the case of, e.g. syntax errors.
  39414. */
  39415. /* Decrement PC if that was requested, this requires a PC sync. */
  39416. if (act != NULL) {
  39417. duk_hthread_sync_currpc(thr);
  39418. old_pc = act->curr_pc;
  39419. fun = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  39420. /* Short circuit if is safe: if act->curr_pc != NULL, 'fun' is
  39421. * guaranteed to be a non-NULL ECMAScript function.
  39422. */
  39423. DUK_ASSERT(act->curr_pc == NULL ||
  39424. (fun != NULL && DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) fun)));
  39425. if (use_prev_pc &&
  39426. act->curr_pc != NULL &&
  39427. act->curr_pc > DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, fun)) {
  39428. act->curr_pc--;
  39429. }
  39430. }
  39431. /* Process debug messages until we are no longer paused. */
  39432. /* NOTE: This is a bit fragile. It's important to ensure that
  39433. * duk_debug_process_messages() never throws an error or
  39434. * act->curr_pc will never be reset.
  39435. */
  39436. thr->heap->dbg_state_dirty = 1;
  39437. while (DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap)) {
  39438. DUK_ASSERT(duk_debug_is_attached(thr->heap));
  39439. DUK_ASSERT(thr->heap->dbg_processing == 0);
  39440. duk_debug_process_messages(thr, 0 /*no_block*/);
  39441. }
  39442. /* XXX: Decrementing and restoring act->curr_pc works now, but if the
  39443. * debugger message loop gains the ability to adjust the current PC
  39444. * (e.g. a forced jump) restoring the PC here will break. Another
  39445. * approach would be to use a state flag for the "decrement 1 from
  39446. * topmost activation's PC" and take it into account whenever dealing
  39447. * with PC values.
  39448. */
  39449. if (act != NULL) {
  39450. act->curr_pc = old_pc; /* restore PC */
  39451. }
  39452. }
  39453. /*
  39454. * Breakpoint management
  39455. */
  39456. DUK_INTERNAL duk_small_int_t duk_debug_add_breakpoint(duk_hthread *thr, duk_hstring *filename, duk_uint32_t line) {
  39457. duk_heap *heap;
  39458. duk_breakpoint *b;
  39459. /* Caller must trigger recomputation of active breakpoint list. To
  39460. * ensure stale values are not used if that doesn't happen, clear the
  39461. * active breakpoint list here.
  39462. */
  39463. DUK_ASSERT(thr != NULL);
  39464. DUK_ASSERT(filename != NULL);
  39465. heap = thr->heap;
  39466. DUK_ASSERT(heap != NULL);
  39467. if (heap->dbg_breakpoint_count >= DUK_HEAP_MAX_BREAKPOINTS) {
  39468. DUK_D(DUK_DPRINT("failed to add breakpoint for %O:%ld, all breakpoint slots used",
  39469. (duk_heaphdr *) filename, (long) line));
  39470. return -1;
  39471. }
  39472. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  39473. b = heap->dbg_breakpoints + (heap->dbg_breakpoint_count++);
  39474. b->filename = filename;
  39475. b->line = line;
  39476. DUK_HSTRING_INCREF(thr, filename);
  39477. return (duk_small_int_t) (heap->dbg_breakpoint_count - 1); /* index */
  39478. }
  39479. DUK_INTERNAL duk_bool_t duk_debug_remove_breakpoint(duk_hthread *thr, duk_small_uint_t breakpoint_index) {
  39480. duk_heap *heap;
  39481. duk_hstring *h;
  39482. duk_breakpoint *b;
  39483. duk_size_t move_size;
  39484. /* Caller must trigger recomputation of active breakpoint list. To
  39485. * ensure stale values are not used if that doesn't happen, clear the
  39486. * active breakpoint list here.
  39487. */
  39488. DUK_ASSERT(thr != NULL);
  39489. heap = thr->heap;
  39490. DUK_ASSERT(heap != NULL);
  39491. DUK_ASSERT(duk_debug_is_attached(thr->heap));
  39492. DUK_ASSERT_DISABLE(breakpoint_index >= 0); /* unsigned */
  39493. if (breakpoint_index >= heap->dbg_breakpoint_count) {
  39494. DUK_D(DUK_DPRINT("invalid breakpoint index: %ld", (long) breakpoint_index));
  39495. return 0;
  39496. }
  39497. b = heap->dbg_breakpoints + breakpoint_index;
  39498. h = b->filename;
  39499. DUK_ASSERT(h != NULL);
  39500. move_size = sizeof(duk_breakpoint) * (heap->dbg_breakpoint_count - breakpoint_index - 1);
  39501. duk_memmove((void *) b,
  39502. (const void *) (b + 1),
  39503. (size_t) move_size);
  39504. heap->dbg_breakpoint_count--;
  39505. heap->dbg_breakpoints_active[0] = (duk_breakpoint *) NULL;
  39506. DUK_HSTRING_DECREF(thr, h); /* side effects */
  39507. DUK_UNREF(h); /* w/o refcounting */
  39508. /* Breakpoint entries above the used area are left as garbage. */
  39509. return 1;
  39510. }
  39511. /*
  39512. * Misc state management
  39513. */
  39514. DUK_INTERNAL duk_bool_t duk_debug_is_attached(duk_heap *heap) {
  39515. return (heap->dbg_read_cb != NULL);
  39516. }
  39517. DUK_INTERNAL duk_bool_t duk_debug_is_paused(duk_heap *heap) {
  39518. return (DUK_HEAP_HAS_DEBUGGER_PAUSED(heap) != 0);
  39519. }
  39520. DUK_INTERNAL void duk_debug_set_paused(duk_heap *heap) {
  39521. if (duk_debug_is_paused(heap)) {
  39522. DUK_D(DUK_DPRINT("trying to set paused state when already paused, ignoring"));
  39523. } else {
  39524. DUK_HEAP_SET_DEBUGGER_PAUSED(heap);
  39525. heap->dbg_state_dirty = 1;
  39526. duk_debug_clear_pause_state(heap);
  39527. DUK_ASSERT(heap->ms_running == 0); /* debugger can't be triggered within mark-and-sweep */
  39528. heap->ms_running = 1; /* prevent mark-and-sweep, prevent refzero queueing */
  39529. heap->ms_prevent_count++;
  39530. DUK_ASSERT(heap->ms_prevent_count != 0); /* Wrap. */
  39531. DUK_ASSERT(heap->heap_thread != NULL);
  39532. }
  39533. }
  39534. DUK_INTERNAL void duk_debug_clear_paused(duk_heap *heap) {
  39535. if (duk_debug_is_paused(heap)) {
  39536. DUK_HEAP_CLEAR_DEBUGGER_PAUSED(heap);
  39537. heap->dbg_state_dirty = 1;
  39538. duk_debug_clear_pause_state(heap);
  39539. DUK_ASSERT(heap->ms_running == 1);
  39540. DUK_ASSERT(heap->ms_prevent_count > 0);
  39541. heap->ms_prevent_count--;
  39542. heap->ms_running = 0;
  39543. DUK_ASSERT(heap->heap_thread != NULL);
  39544. } else {
  39545. DUK_D(DUK_DPRINT("trying to clear paused state when not paused, ignoring"));
  39546. }
  39547. }
  39548. DUK_INTERNAL void duk_debug_clear_pause_state(duk_heap *heap) {
  39549. heap->dbg_pause_flags = 0;
  39550. heap->dbg_pause_act = NULL;
  39551. heap->dbg_pause_startline = 0;
  39552. }
  39553. #else /* DUK_USE_DEBUGGER_SUPPORT */
  39554. /* No debugger support. */
  39555. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  39556. /* automatic undefs */
  39557. #undef DUK__DBG_TPORT_ENTER
  39558. #undef DUK__DBG_TPORT_EXIT
  39559. #undef DUK__SET_CONN_BROKEN
  39560. #line 1 "duk_error_augment.c"
  39561. /*
  39562. * Augmenting errors at their creation site and their throw site.
  39563. *
  39564. * When errors are created, traceback data is added by built-in code
  39565. * and a user error handler (if defined) can process or replace the
  39566. * error. Similarly, when errors are thrown, a user error handler
  39567. * (if defined) can process or replace the error.
  39568. *
  39569. * Augmentation and other processing at error creation time is nice
  39570. * because an error is only created once, but it may be thrown and
  39571. * rethrown multiple times. User error handler registered for processing
  39572. * an error at its throw site must be careful to handle rethrowing in
  39573. * a useful manner.
  39574. *
  39575. * Error augmentation may throw an internal error (e.g. alloc error).
  39576. *
  39577. * ECMAScript allows throwing any values, so all values cannot be
  39578. * augmented. Currently, the built-in augmentation at error creation
  39579. * only augments error values which are Error instances (= have the
  39580. * built-in Error.prototype in their prototype chain) and are also
  39581. * extensible. User error handlers have no limitations in this respect.
  39582. */
  39583. /* #include duk_internal.h -> already included */
  39584. /*
  39585. * Helper for calling a user error handler.
  39586. *
  39587. * 'thr' must be the currently active thread; the error handler is called
  39588. * in its context. The valstack of 'thr' must have the error value on
  39589. * top, and will be replaced by another error value based on the return
  39590. * value of the error handler.
  39591. *
  39592. * The helper calls duk_handle_call() recursively in protected mode.
  39593. * Before that call happens, no longjmps should happen; as a consequence,
  39594. * we must assume that the valstack contains enough temporary space for
  39595. * arguments and such.
  39596. *
  39597. * While the error handler runs, any errors thrown will not trigger a
  39598. * recursive error handler call (this is implemented using a heap level
  39599. * flag which will "follow" through any coroutines resumed inside the
  39600. * error handler). If the error handler is not callable or throws an
  39601. * error, the resulting error replaces the original error (for Duktape
  39602. * internal errors, duk_error_throw.c further substitutes this error with
  39603. * a DoubleError which is not ideal). This would be easy to change and
  39604. * even signal to the caller.
  39605. *
  39606. * The user error handler is stored in 'Duktape.errCreate' or
  39607. * 'Duktape.errThrow' depending on whether we're augmenting the error at
  39608. * creation or throw time. There are several alternatives to this approach,
  39609. * see doc/error-objects.rst for discussion.
  39610. *
  39611. * Note: since further longjmp()s may occur while calling the error handler
  39612. * (for many reasons, e.g. a labeled 'break' inside the handler), the
  39613. * caller can make no assumptions on the thr->heap->lj state after the
  39614. * call (this affects especially duk_error_throw.c). This is not an issue
  39615. * as long as the caller writes to the lj state only after the error handler
  39616. * finishes.
  39617. */
  39618. #if defined(DUK_USE_ERRTHROW) || defined(DUK_USE_ERRCREATE)
  39619. DUK_LOCAL void duk__err_augment_user(duk_hthread *thr, duk_small_uint_t stridx_cb) {
  39620. duk_tval *tv_hnd;
  39621. duk_int_t rc;
  39622. DUK_ASSERT(thr != NULL);
  39623. DUK_ASSERT(thr->heap != NULL);
  39624. DUK_ASSERT_STRIDX_VALID(stridx_cb);
  39625. if (thr->heap->augmenting_error) {
  39626. DUK_D(DUK_DPRINT("recursive call to error augmentation, ignore"));
  39627. return;
  39628. }
  39629. /*
  39630. * Check whether or not we have an error handler.
  39631. *
  39632. * We must be careful of not triggering an error when looking up the
  39633. * property. For instance, if the property is a getter, we don't want
  39634. * to call it, only plain values are allowed. The value, if it exists,
  39635. * is not checked. If the value is not a function, a TypeError happens
  39636. * when it is called and that error replaces the original one.
  39637. */
  39638. DUK_ASSERT_VALSTACK_SPACE(thr, 4); /* 3 entries actually needed below */
  39639. /* [ ... errval ] */
  39640. if (thr->builtins[DUK_BIDX_DUKTAPE] == NULL) {
  39641. /* When creating built-ins, some of the built-ins may not be set
  39642. * and we want to tolerate that when throwing errors.
  39643. */
  39644. DUK_DD(DUK_DDPRINT("error occurred when DUK_BIDX_DUKTAPE is NULL, ignoring"));
  39645. return;
  39646. }
  39647. tv_hnd = duk_hobject_find_existing_entry_tval_ptr(thr->heap,
  39648. thr->builtins[DUK_BIDX_DUKTAPE],
  39649. DUK_HTHREAD_GET_STRING(thr, stridx_cb));
  39650. if (tv_hnd == NULL) {
  39651. DUK_DD(DUK_DDPRINT("error handler does not exist or is not a plain value: %!T",
  39652. (duk_tval *) tv_hnd));
  39653. return;
  39654. }
  39655. DUK_DDD(DUK_DDDPRINT("error handler dump (callability not checked): %!T",
  39656. (duk_tval *) tv_hnd));
  39657. duk_push_tval(thr, tv_hnd);
  39658. /* [ ... errval errhandler ] */
  39659. duk_insert(thr, -2); /* -> [ ... errhandler errval ] */
  39660. duk_push_undefined(thr);
  39661. duk_insert(thr, -2); /* -> [ ... errhandler undefined(= this) errval ] */
  39662. /* [ ... errhandler undefined errval ] */
  39663. /*
  39664. * heap->augmenting_error prevents recursive re-entry and also causes
  39665. * call handling to use a larger (but not unbounded) call stack limit
  39666. * for the duration of error augmentation.
  39667. *
  39668. * We ignore errors now: a success return and an error value both
  39669. * replace the original error value. (This would be easy to change.)
  39670. */
  39671. DUK_ASSERT(thr->heap->augmenting_error == 0);
  39672. thr->heap->augmenting_error = 1;
  39673. rc = duk_pcall_method(thr, 1);
  39674. DUK_UNREF(rc); /* no need to check now: both success and error are OK */
  39675. DUK_ASSERT(thr->heap->augmenting_error == 1);
  39676. thr->heap->augmenting_error = 0;
  39677. /* [ ... errval ] */
  39678. }
  39679. #endif /* DUK_USE_ERRTHROW || DUK_USE_ERRCREATE */
  39680. /*
  39681. * Add ._Tracedata to an error on the stack top.
  39682. */
  39683. #if defined(DUK_USE_TRACEBACKS)
  39684. DUK_LOCAL void duk__add_traceback(duk_hthread *thr, duk_hthread *thr_callstack, const char *c_filename, duk_int_t c_line, duk_small_uint_t flags) {
  39685. duk_activation *act;
  39686. duk_int_t depth;
  39687. duk_int_t arr_size;
  39688. duk_tval *tv;
  39689. duk_hstring *s;
  39690. duk_uint32_t u32;
  39691. duk_double_t d;
  39692. DUK_ASSERT(thr != NULL);
  39693. DUK_ASSERT(thr_callstack != NULL);
  39694. /* [ ... error ] */
  39695. /*
  39696. * The traceback format is pretty arcane in an attempt to keep it compact
  39697. * and cheap to create. It may change arbitrarily from version to version.
  39698. * It should be decoded/accessed through version specific accessors only.
  39699. *
  39700. * See doc/error-objects.rst.
  39701. */
  39702. DUK_DDD(DUK_DDDPRINT("adding traceback to object: %!T",
  39703. (duk_tval *) duk_get_tval(thr, -1)));
  39704. /* Preallocate array to correct size, so that we can just write out
  39705. * the _Tracedata values into the array part.
  39706. */
  39707. act = thr->callstack_curr;
  39708. depth = DUK_USE_TRACEBACK_DEPTH;
  39709. DUK_ASSERT(thr_callstack->callstack_top <= DUK_INT_MAX); /* callstack limits */
  39710. if (depth > (duk_int_t) thr_callstack->callstack_top) {
  39711. depth = (duk_int_t) thr_callstack->callstack_top;
  39712. }
  39713. if (depth > 0) {
  39714. if (flags & DUK_AUGMENT_FLAG_SKIP_ONE) {
  39715. DUK_ASSERT(act != NULL);
  39716. act = act->parent;
  39717. depth--;
  39718. }
  39719. }
  39720. arr_size = depth * 2;
  39721. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  39722. arr_size += 2;
  39723. }
  39724. if (c_filename) {
  39725. /* We need the C filename to be interned before getting the
  39726. * array part pointer to avoid any GC interference while the
  39727. * array part is populated.
  39728. */
  39729. duk_push_string(thr, c_filename);
  39730. arr_size += 2;
  39731. }
  39732. /* XXX: uninitialized would be OK */
  39733. DUK_D(DUK_DPRINT("preallocated _Tracedata to %ld items", (long) arr_size));
  39734. tv = duk_push_harray_with_size_outptr(thr, (duk_uint32_t) arr_size);
  39735. DUK_ASSERT(arr_size == 0 || tv != NULL);
  39736. /* Compiler SyntaxErrors (and other errors) come first, and are
  39737. * blamed by default (not flagged "noblame").
  39738. */
  39739. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  39740. s = thr->compile_ctx->h_filename;
  39741. DUK_TVAL_SET_STRING(tv, s);
  39742. DUK_HSTRING_INCREF(thr, s);
  39743. tv++;
  39744. u32 = (duk_uint32_t) thr->compile_ctx->curr_token.start_line; /* (flags<<32) + (line), flags = 0 */
  39745. DUK_TVAL_SET_U32(tv, u32);
  39746. tv++;
  39747. }
  39748. /* Filename/line from C macros (__FILE__, __LINE__) are added as an
  39749. * entry with a special format: (string, number). The number contains
  39750. * the line and flags.
  39751. */
  39752. /* [ ... error c_filename? arr ] */
  39753. if (c_filename) {
  39754. DUK_ASSERT(DUK_TVAL_IS_STRING(thr->valstack_top - 2));
  39755. s = DUK_TVAL_GET_STRING(thr->valstack_top - 2); /* interned c_filename */
  39756. DUK_ASSERT(s != NULL);
  39757. DUK_TVAL_SET_STRING(tv, s);
  39758. DUK_HSTRING_INCREF(thr, s);
  39759. tv++;
  39760. d = ((flags & DUK_AUGMENT_FLAG_NOBLAME_FILELINE) ? ((duk_double_t) DUK_TB_FLAG_NOBLAME_FILELINE) * DUK_DOUBLE_2TO32 : 0.0) +
  39761. (duk_double_t) c_line;
  39762. DUK_TVAL_SET_DOUBLE(tv, d);
  39763. tv++;
  39764. }
  39765. /* Traceback depth doesn't take into account the filename/line
  39766. * special handling above (intentional).
  39767. */
  39768. for (; depth-- > 0; act = act->parent) {
  39769. duk_uint32_t pc;
  39770. duk_tval *tv_src;
  39771. /* [... arr] */
  39772. DUK_ASSERT(act != NULL); /* depth check above, assumes book-keeping is correct */
  39773. DUK_ASSERT_DISABLE(act->pc >= 0); /* unsigned */
  39774. /* Add function object. */
  39775. tv_src = &act->tv_func; /* object (function) or lightfunc */
  39776. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv_src) || DUK_TVAL_IS_LIGHTFUNC(tv_src));
  39777. DUK_TVAL_SET_TVAL(tv, tv_src);
  39778. DUK_TVAL_INCREF(thr, tv);
  39779. tv++;
  39780. /* Add a number containing: pc, activation flags.
  39781. *
  39782. * PC points to next instruction, find offending PC. Note that
  39783. * PC == 0 for native code.
  39784. */
  39785. pc = (duk_uint32_t) duk_hthread_get_act_prev_pc(thr_callstack, act);
  39786. DUK_ASSERT_DISABLE(pc >= 0); /* unsigned */
  39787. DUK_ASSERT((duk_double_t) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */
  39788. d = ((duk_double_t) act->flags) * DUK_DOUBLE_2TO32 + (duk_double_t) pc;
  39789. DUK_TVAL_SET_DOUBLE(tv, d);
  39790. tv++;
  39791. }
  39792. #if defined(DUK_USE_ASSERTIONS)
  39793. {
  39794. duk_harray *a;
  39795. a = (duk_harray *) duk_known_hobject(thr, -1);
  39796. DUK_ASSERT(a != NULL);
  39797. DUK_ASSERT((duk_uint32_t) (tv - DUK_HOBJECT_A_GET_BASE(thr->heap, (duk_hobject *) a)) == a->length);
  39798. DUK_ASSERT(a->length == (duk_uint32_t) arr_size);
  39799. }
  39800. #endif
  39801. /* [ ... error c_filename? arr ] */
  39802. if (c_filename) {
  39803. duk_remove_m2(thr);
  39804. }
  39805. /* [ ... error arr ] */
  39806. duk_xdef_prop_stridx_short_wec(thr, -2, DUK_STRIDX_INT_TRACEDATA); /* -> [ ... error ] */
  39807. }
  39808. #endif /* DUK_USE_TRACEBACKS */
  39809. /*
  39810. * Add .fileName and .lineNumber to an error on the stack top.
  39811. */
  39812. #if defined(DUK_USE_AUGMENT_ERROR_CREATE) && !defined(DUK_USE_TRACEBACKS)
  39813. DUK_LOCAL void duk__add_fileline(duk_hthread *thr, duk_hthread *thr_callstack, const char *c_filename, duk_int_t c_line, duk_small_uint_t flags) {
  39814. #if defined(DUK_USE_ASSERTIONS)
  39815. duk_int_t entry_top;
  39816. #endif
  39817. #if defined(DUK_USE_ASSERTIONS)
  39818. entry_top = duk_get_top(thr);
  39819. #endif
  39820. /*
  39821. * If tracebacks are disabled, 'fileName' and 'lineNumber' are added
  39822. * as plain own properties. Since Error.prototype has accessors of
  39823. * the same name, we need to define own properties directly (cannot
  39824. * just use e.g. duk_put_prop_stridx). Existing properties are not
  39825. * overwritten in case they already exist.
  39826. */
  39827. if (thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL) {
  39828. /* Compiler SyntaxError (or other error) gets the primary blame.
  39829. * Currently no flag to prevent blaming.
  39830. */
  39831. duk_push_uint(thr, (duk_uint_t) thr->compile_ctx->curr_token.start_line);
  39832. duk_push_hstring(thr, thr->compile_ctx->h_filename);
  39833. } else if (c_filename && (flags & DUK_AUGMENT_FLAG_NOBLAME_FILELINE) == 0) {
  39834. /* C call site gets blamed next, unless flagged not to do so.
  39835. * XXX: file/line is disabled in minimal builds, so disable this
  39836. * too when appropriate.
  39837. */
  39838. duk_push_int(thr, c_line);
  39839. duk_push_string(thr, c_filename);
  39840. } else {
  39841. /* Finally, blame the innermost callstack entry which has a
  39842. * .fileName property.
  39843. */
  39844. duk_small_uint_t depth;
  39845. duk_uint32_t ecma_line;
  39846. duk_activation *act;
  39847. DUK_ASSERT(thr_callstack->callstack_top <= DUK_INT_MAX); /* callstack limits */
  39848. depth = DUK_USE_TRACEBACK_DEPTH;
  39849. if (depth > thr_callstack->callstack_top) {
  39850. depth = thr_callstack->callstack_top;
  39851. }
  39852. for (act = thr_callstack->callstack_curr; depth-- > 0; act = act->parent) {
  39853. duk_hobject *func;
  39854. duk_uint32_t pc;
  39855. DUK_ASSERT(act != NULL);
  39856. func = DUK_ACT_GET_FUNC(act);
  39857. if (func == NULL) {
  39858. /* Lightfunc, not blamed now. */
  39859. continue;
  39860. }
  39861. /* PC points to next instruction, find offending PC,
  39862. * PC == 0 for native code.
  39863. */
  39864. pc = duk_hthread_get_act_prev_pc(thr, act); /* thr argument only used for thr->heap, so specific thread doesn't matter */
  39865. DUK_UNREF(pc);
  39866. DUK_ASSERT_DISABLE(pc >= 0); /* unsigned */
  39867. DUK_ASSERT((duk_double_t) pc < DUK_DOUBLE_2TO32); /* assume PC is at most 32 bits and non-negative */
  39868. duk_push_hobject(thr, func);
  39869. /* [ ... error func ] */
  39870. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_FILE_NAME);
  39871. if (!duk_is_string_notsymbol(thr, -1)) {
  39872. duk_pop_2(thr);
  39873. continue;
  39874. }
  39875. /* [ ... error func fileName ] */
  39876. ecma_line = 0;
  39877. #if defined(DUK_USE_PC2LINE)
  39878. if (DUK_HOBJECT_IS_COMPFUNC(func)) {
  39879. ecma_line = duk_hobject_pc2line_query(thr, -2, (duk_uint_fast32_t) pc);
  39880. } else {
  39881. /* Native function, no relevant lineNumber. */
  39882. }
  39883. #endif /* DUK_USE_PC2LINE */
  39884. duk_push_u32(thr, ecma_line);
  39885. /* [ ... error func fileName lineNumber ] */
  39886. duk_replace(thr, -3);
  39887. /* [ ... error lineNumber fileName ] */
  39888. goto define_props;
  39889. }
  39890. /* No activation matches, use undefined for both .fileName and
  39891. * .lineNumber (matches what we do with a _Tracedata based
  39892. * no-match lookup.
  39893. */
  39894. duk_push_undefined(thr);
  39895. duk_push_undefined(thr);
  39896. }
  39897. define_props:
  39898. /* [ ... error lineNumber fileName ] */
  39899. #if defined(DUK_USE_ASSERTIONS)
  39900. DUK_ASSERT(duk_get_top(thr) == entry_top + 2);
  39901. #endif
  39902. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_C | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  39903. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LINE_NUMBER, DUK_PROPDESC_FLAGS_C | DUK_PROPDESC_FLAG_NO_OVERWRITE);
  39904. }
  39905. #endif /* DUK_USE_AUGMENT_ERROR_CREATE && !DUK_USE_TRACEBACKS */
  39906. /*
  39907. * Add line number to a compiler error.
  39908. */
  39909. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  39910. DUK_LOCAL void duk__add_compiler_error_line(duk_hthread *thr) {
  39911. /* Append a "(line NNN)" to the "message" property of any error
  39912. * thrown during compilation. Usually compilation errors are
  39913. * SyntaxErrors but they can also be out-of-memory errors and
  39914. * the like.
  39915. */
  39916. /* [ ... error ] */
  39917. DUK_ASSERT(duk_is_object(thr, -1));
  39918. if (!(thr->compile_ctx != NULL && thr->compile_ctx->h_filename != NULL)) {
  39919. return;
  39920. }
  39921. DUK_DDD(DUK_DDDPRINT("compile error, before adding line info: %!T",
  39922. (duk_tval *) duk_get_tval(thr, -1)));
  39923. if (duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_MESSAGE)) {
  39924. duk_push_sprintf(thr, " (line %ld)", (long) thr->compile_ctx->curr_token.start_line);
  39925. duk_concat(thr, 2);
  39926. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_MESSAGE);
  39927. } else {
  39928. duk_pop(thr);
  39929. }
  39930. DUK_DDD(DUK_DDDPRINT("compile error, after adding line info: %!T",
  39931. (duk_tval *) duk_get_tval(thr, -1)));
  39932. }
  39933. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  39934. /*
  39935. * Augment an error being created using Duktape specific properties
  39936. * like _Tracedata or .fileName/.lineNumber.
  39937. */
  39938. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  39939. DUK_LOCAL void duk__err_augment_builtin_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *c_filename, duk_int_t c_line, duk_hobject *obj, duk_small_uint_t flags) {
  39940. #if defined(DUK_USE_ASSERTIONS)
  39941. duk_int_t entry_top;
  39942. #endif
  39943. #if defined(DUK_USE_ASSERTIONS)
  39944. entry_top = duk_get_top(thr);
  39945. #endif
  39946. DUK_ASSERT(obj != NULL);
  39947. DUK_UNREF(obj); /* unreferenced w/o tracebacks */
  39948. duk__add_compiler_error_line(thr);
  39949. #if defined(DUK_USE_TRACEBACKS)
  39950. /* If tracebacks are enabled, the '_Tracedata' property is the only
  39951. * thing we need: 'fileName' and 'lineNumber' are virtual properties
  39952. * which use '_Tracedata'.
  39953. */
  39954. if (duk_hobject_hasprop_raw(thr, obj, DUK_HTHREAD_STRING_INT_TRACEDATA(thr))) {
  39955. DUK_DDD(DUK_DDDPRINT("error value already has a '_Tracedata' property, not modifying it"));
  39956. } else {
  39957. duk__add_traceback(thr, thr_callstack, c_filename, c_line, flags);
  39958. }
  39959. #else
  39960. /* Without tracebacks the concrete .fileName and .lineNumber need
  39961. * to be added directly.
  39962. */
  39963. duk__add_fileline(thr, thr_callstack, c_filename, c_line, flags);
  39964. #endif
  39965. #if defined(DUK_USE_ASSERTIONS)
  39966. DUK_ASSERT(duk_get_top(thr) == entry_top);
  39967. #endif
  39968. }
  39969. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  39970. /*
  39971. * Augment an error at creation time with _Tracedata/fileName/lineNumber
  39972. * and allow a user error handler (if defined) to process/replace the error.
  39973. * The error to be augmented is at the stack top.
  39974. *
  39975. * thr: thread containing the error value
  39976. * thr_callstack: thread which should be used for generating callstack etc.
  39977. * c_filename: C __FILE__ related to the error
  39978. * c_line: C __LINE__ related to the error
  39979. * flags & DUK_AUGMENT_FLAG_NOBLAME_FILELINE:
  39980. * if true, don't fileName/line as error source, otherwise use traceback
  39981. * (needed because user code filename/line are reported but internal ones
  39982. * are not)
  39983. */
  39984. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  39985. DUK_INTERNAL void duk_err_augment_error_create(duk_hthread *thr, duk_hthread *thr_callstack, const char *c_filename, duk_int_t c_line, duk_small_uint_t flags) {
  39986. duk_hobject *obj;
  39987. DUK_ASSERT(thr != NULL);
  39988. DUK_ASSERT(thr_callstack != NULL);
  39989. /* [ ... error ] */
  39990. /*
  39991. * Criteria for augmenting:
  39992. *
  39993. * - augmentation enabled in build (naturally)
  39994. * - error value internal prototype chain contains the built-in
  39995. * Error prototype object (i.e. 'val instanceof Error')
  39996. *
  39997. * Additional criteria for built-in augmenting:
  39998. *
  39999. * - error value is an extensible object
  40000. */
  40001. obj = duk_get_hobject(thr, -1);
  40002. if (!obj) {
  40003. DUK_DDD(DUK_DDDPRINT("value is not an object, skip both built-in and user augment"));
  40004. return;
  40005. }
  40006. if (!duk_hobject_prototype_chain_contains(thr, obj, thr->builtins[DUK_BIDX_ERROR_PROTOTYPE], 1 /*ignore_loop*/)) {
  40007. /* If the value has a prototype loop, it's critical not to
  40008. * throw here. Instead, assume the value is not to be
  40009. * augmented.
  40010. */
  40011. DUK_DDD(DUK_DDDPRINT("value is not an error instance, skip both built-in and user augment"));
  40012. return;
  40013. }
  40014. if (DUK_HOBJECT_HAS_EXTENSIBLE(obj)) {
  40015. DUK_DDD(DUK_DDDPRINT("error meets criteria, built-in augment"));
  40016. duk__err_augment_builtin_create(thr, thr_callstack, c_filename, c_line, obj, flags);
  40017. } else {
  40018. DUK_DDD(DUK_DDDPRINT("error does not meet criteria, no built-in augment"));
  40019. }
  40020. /* [ ... error ] */
  40021. #if defined(DUK_USE_ERRCREATE)
  40022. duk__err_augment_user(thr, DUK_STRIDX_ERR_CREATE);
  40023. #endif
  40024. }
  40025. #endif /* DUK_USE_AUGMENT_ERROR_CREATE */
  40026. /*
  40027. * Augment an error at throw time; allow a user error handler (if defined)
  40028. * to process/replace the error. The error to be augmented is at the
  40029. * stack top.
  40030. */
  40031. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  40032. DUK_INTERNAL void duk_err_augment_error_throw(duk_hthread *thr) {
  40033. #if defined(DUK_USE_ERRTHROW)
  40034. duk__err_augment_user(thr, DUK_STRIDX_ERR_THROW);
  40035. #endif /* DUK_USE_ERRTHROW */
  40036. }
  40037. #endif /* DUK_USE_AUGMENT_ERROR_THROW */
  40038. #line 1 "duk_error_longjmp.c"
  40039. /*
  40040. * Do a longjmp call, calling the fatal error handler if no
  40041. * catchpoint exists.
  40042. */
  40043. /* #include duk_internal.h -> already included */
  40044. #if defined(DUK_USE_PREFER_SIZE)
  40045. DUK_NORETURN(DUK_LOCAL_DECL void duk__uncaught_minimal(duk_hthread *thr));
  40046. DUK_LOCAL void duk__uncaught_minimal(duk_hthread *thr) {
  40047. (void) duk_fatal(thr, "uncaught error");
  40048. DUK_WO_NORETURN(return;);
  40049. }
  40050. #endif
  40051. #if 0
  40052. DUK_NORETURN(DUK_LOCAL_DECL void duk__uncaught_readable(duk_hthread *thr));
  40053. DUK_LOCAL void duk__uncaught_readable(duk_hthread *thr) {
  40054. const char *summary;
  40055. char buf[DUK_USE_FATAL_MAXLEN];
  40056. summary = duk_push_string_tval_readable(thr, &thr->heap->lj.value1);
  40057. DUK_SNPRINTF(buf, sizeof(buf), "uncaught: %s", summary);
  40058. buf[sizeof(buf) - 1] = (char) 0;
  40059. (void) duk_fatal(thr, (const char *) buf);
  40060. DUK_WO_NORETURN(return;);
  40061. }
  40062. #endif
  40063. #if !defined(DUK_USE_PREFER_SIZE)
  40064. DUK_NORETURN(DUK_LOCAL_DECL void duk__uncaught_error_aware(duk_hthread *thr));
  40065. DUK_LOCAL void duk__uncaught_error_aware(duk_hthread *thr) {
  40066. const char *summary;
  40067. char buf[DUK_USE_FATAL_MAXLEN];
  40068. summary = duk_push_string_tval_readable_error(thr, &thr->heap->lj.value1);
  40069. DUK_ASSERT(summary != NULL);
  40070. DUK_SNPRINTF(buf, sizeof(buf), "uncaught: %s", summary);
  40071. buf[sizeof(buf) - 1] = (char) 0;
  40072. (void) duk_fatal(thr, (const char *) buf);
  40073. DUK_WO_NORETURN(return;);
  40074. }
  40075. #endif
  40076. DUK_INTERNAL void duk_err_longjmp(duk_hthread *thr) {
  40077. DUK_ASSERT(thr != NULL);
  40078. DUK_ASSERT(thr->heap != NULL);
  40079. DUK_DD(DUK_DDPRINT("longjmp error: type=%d iserror=%d value1=%!T value2=%!T",
  40080. (int) thr->heap->lj.type, (int) thr->heap->lj.iserror,
  40081. &thr->heap->lj.value1, &thr->heap->lj.value2));
  40082. /* Prevent finalizer execution during error handling. All error
  40083. * handling sites will process pending finalizers once error handling
  40084. * is complete and we're ready for the side effects. Does not prevent
  40085. * refzero freeing or mark-and-sweep during error handling.
  40086. *
  40087. * NOTE: when we come here some calling code may have used DECREF
  40088. * NORZ macros without an explicit DUK_REFZERO_CHECK_xxx() call.
  40089. * We don't want to do it here because it would just check for
  40090. * pending finalizers and we prevent that explicitly. Instead,
  40091. * the error catcher will run the finalizers once error handling
  40092. * is complete.
  40093. */
  40094. DUK_ASSERT_LJSTATE_SET(thr->heap);
  40095. thr->heap->pf_prevent_count++;
  40096. DUK_ASSERT(thr->heap->pf_prevent_count != 0); /* Wrap. */
  40097. #if defined(DUK_USE_ASSERTIONS)
  40098. /* XXX: set this immediately when longjmp state is set */
  40099. DUK_ASSERT(thr->heap->error_not_allowed == 0); /* Detect error within critical section. */
  40100. thr->heap->error_not_allowed = 1;
  40101. #endif
  40102. DUK_DD(DUK_DDPRINT("about to longjmp, pf_prevent_count=%ld", (long) thr->heap->pf_prevent_count));
  40103. /* If we don't have a jmpbuf_ptr, there is little we can do except
  40104. * cause a fatal error. The caller's expectation is that we never
  40105. * return.
  40106. */
  40107. if (!thr->heap->lj.jmpbuf_ptr) {
  40108. DUK_D(DUK_DPRINT("uncaught error: type=%d iserror=%d value1=%!T value2=%!T",
  40109. (int) thr->heap->lj.type, (int) thr->heap->lj.iserror,
  40110. &thr->heap->lj.value1, &thr->heap->lj.value2));
  40111. #if defined(DUK_USE_PREFER_SIZE)
  40112. duk__uncaught_minimal(thr);
  40113. #else
  40114. duk__uncaught_error_aware(thr);
  40115. #endif
  40116. DUK_UNREACHABLE();
  40117. }
  40118. #if defined(DUK_USE_CPP_EXCEPTIONS)
  40119. throw duk_internal_exception(); /* dummy */
  40120. #else
  40121. DUK_LONGJMP(thr->heap->lj.jmpbuf_ptr->jb);
  40122. #endif
  40123. DUK_UNREACHABLE();
  40124. }
  40125. #line 1 "duk_error_misc.c"
  40126. /*
  40127. * Error helpers
  40128. */
  40129. /* #include duk_internal.h -> already included */
  40130. /*
  40131. * Helper to walk the thread chain and see if there is an active error
  40132. * catcher. Protected calls or finally blocks aren't considered catching.
  40133. */
  40134. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  40135. DUK_LOCAL duk_bool_t duk__have_active_catcher(duk_hthread *thr) {
  40136. /* As noted above, a protected API call won't be counted as a
  40137. * catcher. This is usually convenient, e.g. in the case of a top-
  40138. * level duk_pcall(), but may not always be desirable. Perhaps add
  40139. * an argument to treat them as catchers?
  40140. */
  40141. duk_activation *act;
  40142. duk_catcher *cat;
  40143. DUK_ASSERT(thr != NULL);
  40144. for (; thr != NULL; thr = thr->resumer) {
  40145. for (act = thr->callstack_curr; act != NULL; act = act->parent) {
  40146. for (cat = act->cat; cat != NULL; cat = cat->parent) {
  40147. if (DUK_CAT_HAS_CATCH_ENABLED(cat)) {
  40148. return 1; /* all we need to know */
  40149. }
  40150. }
  40151. }
  40152. }
  40153. return 0;
  40154. }
  40155. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  40156. /*
  40157. * Get prototype object for an integer error code.
  40158. */
  40159. DUK_INTERNAL duk_hobject *duk_error_prototype_from_code(duk_hthread *thr, duk_errcode_t code) {
  40160. switch (code) {
  40161. case DUK_ERR_EVAL_ERROR:
  40162. return thr->builtins[DUK_BIDX_EVAL_ERROR_PROTOTYPE];
  40163. case DUK_ERR_RANGE_ERROR:
  40164. return thr->builtins[DUK_BIDX_RANGE_ERROR_PROTOTYPE];
  40165. case DUK_ERR_REFERENCE_ERROR:
  40166. return thr->builtins[DUK_BIDX_REFERENCE_ERROR_PROTOTYPE];
  40167. case DUK_ERR_SYNTAX_ERROR:
  40168. return thr->builtins[DUK_BIDX_SYNTAX_ERROR_PROTOTYPE];
  40169. case DUK_ERR_TYPE_ERROR:
  40170. return thr->builtins[DUK_BIDX_TYPE_ERROR_PROTOTYPE];
  40171. case DUK_ERR_URI_ERROR:
  40172. return thr->builtins[DUK_BIDX_URI_ERROR_PROTOTYPE];
  40173. case DUK_ERR_ERROR:
  40174. default:
  40175. return thr->builtins[DUK_BIDX_ERROR_PROTOTYPE];
  40176. }
  40177. }
  40178. /*
  40179. * Helper for debugger throw notify and pause-on-uncaught integration.
  40180. */
  40181. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  40182. DUK_INTERNAL void duk_err_check_debugger_integration(duk_hthread *thr) {
  40183. duk_bool_t uncaught;
  40184. duk_tval *tv_obj;
  40185. /* If something is thrown with the debugger attached and nobody will
  40186. * catch it, execution is paused before the longjmp, turning over
  40187. * control to the debug client. This allows local state to be examined
  40188. * before the stack is unwound. Errors are not intercepted when debug
  40189. * message loop is active (e.g. for Eval).
  40190. */
  40191. DUK_ASSERT(thr != NULL);
  40192. DUK_ASSERT(thr->heap != NULL);
  40193. /* XXX: Allow customizing the pause and notify behavior at runtime
  40194. * using debugger runtime flags. For now the behavior is fixed using
  40195. * config options.
  40196. */
  40197. if (!duk_debug_is_attached(thr->heap) ||
  40198. thr->heap->dbg_processing ||
  40199. thr->heap->lj.type != DUK_LJ_TYPE_THROW ||
  40200. thr->heap->creating_error) {
  40201. DUK_D(DUK_DPRINT("skip debugger error integration; not attached, debugger processing, not THROW, or error thrown while creating error"));
  40202. return;
  40203. }
  40204. /* Don't intercept a DoubleError, we may have caused the initial double
  40205. * fault and attempting to intercept it will cause us to be called
  40206. * recursively and exhaust the C stack. (This should no longer happen
  40207. * for the initial throw because DoubleError path doesn't do a debugger
  40208. * integration check, but it might happen for rethrows.)
  40209. */
  40210. tv_obj = &thr->heap->lj.value1;
  40211. if (DUK_TVAL_IS_OBJECT(tv_obj) && DUK_TVAL_GET_OBJECT(tv_obj) == thr->builtins[DUK_BIDX_DOUBLE_ERROR]) {
  40212. DUK_D(DUK_DPRINT("built-in DoubleError instance (re)thrown, not intercepting"));
  40213. return;
  40214. }
  40215. uncaught = !duk__have_active_catcher(thr);
  40216. /* Debugger code expects the value at stack top. This also serves
  40217. * as a backup: we need to store/restore the longjmp state because
  40218. * when the debugger is paused Eval commands may be executed and
  40219. * they can arbitrarily clobber the longjmp state.
  40220. */
  40221. duk_push_tval(thr, tv_obj);
  40222. /* Store and reset longjmp state. */
  40223. DUK_ASSERT_LJSTATE_SET(thr->heap);
  40224. DUK_TVAL_DECREF_NORZ(thr, tv_obj);
  40225. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&thr->heap->lj.value2)); /* Always for THROW type. */
  40226. DUK_TVAL_SET_UNDEFINED(tv_obj);
  40227. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  40228. DUK_ASSERT_LJSTATE_UNSET(thr->heap);
  40229. #if defined(DUK_USE_DEBUGGER_THROW_NOTIFY)
  40230. /* Report it to the debug client */
  40231. DUK_D(DUK_DPRINT("throw with debugger attached, report to client"));
  40232. duk_debug_send_throw(thr, uncaught);
  40233. #endif
  40234. if (uncaught) {
  40235. if (thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_UNCAUGHT_ERROR) {
  40236. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by uncaught error"));
  40237. duk_debug_halt_execution(thr, 1 /*use_prev_pc*/);
  40238. }
  40239. } else {
  40240. if (thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_CAUGHT_ERROR) {
  40241. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by caught error"));
  40242. duk_debug_halt_execution(thr, 1 /*use_prev_pc*/);
  40243. }
  40244. }
  40245. /* Restore longjmp state. */
  40246. DUK_ASSERT_LJSTATE_UNSET(thr->heap);
  40247. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  40248. tv_obj = DUK_GET_TVAL_NEGIDX(thr, -1);
  40249. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&thr->heap->lj.value1));
  40250. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(&thr->heap->lj.value2));
  40251. DUK_TVAL_SET_TVAL(&thr->heap->lj.value1, tv_obj);
  40252. DUK_TVAL_INCREF(thr, tv_obj);
  40253. DUK_ASSERT_LJSTATE_SET(thr->heap);
  40254. duk_pop(thr);
  40255. }
  40256. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  40257. /*
  40258. * Helpers for setting up heap longjmp state.
  40259. */
  40260. DUK_INTERNAL void duk_err_setup_ljstate1(duk_hthread *thr, duk_small_uint_t lj_type, duk_tval *tv_val) {
  40261. duk_heap *heap;
  40262. DUK_ASSERT(thr != NULL);
  40263. heap = thr->heap;
  40264. DUK_ASSERT(heap != NULL);
  40265. DUK_ASSERT(tv_val != NULL);
  40266. DUK_ASSERT_LJSTATE_UNSET(heap);
  40267. heap->lj.type = lj_type;
  40268. DUK_TVAL_SET_TVAL(&heap->lj.value1, tv_val);
  40269. DUK_TVAL_INCREF(thr, tv_val);
  40270. DUK_ASSERT_LJSTATE_SET(heap);
  40271. }
  40272. #line 1 "duk_error_throw.c"
  40273. /*
  40274. * Create and throw an ECMAScript error object based on a code and a message.
  40275. *
  40276. * Used when we throw errors internally. ECMAScript generated error objects
  40277. * are created by ECMAScript code, and the throwing is handled by the bytecode
  40278. * executor.
  40279. */
  40280. /* #include duk_internal.h -> already included */
  40281. /*
  40282. * Create and throw an error (originating from Duktape internally)
  40283. *
  40284. * Push an error object on top of the stack, possibly throw augmenting
  40285. * the error, and finally longjmp.
  40286. *
  40287. * If an error occurs while we're dealing with the current error, we might
  40288. * enter an infinite recursion loop. This is prevented by detecting a
  40289. * "double fault" through the heap->creating_error flag; the recursion
  40290. * then stops at the second level.
  40291. */
  40292. #if defined(DUK_USE_VERBOSE_ERRORS)
  40293. DUK_INTERNAL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code, const char *msg, const char *filename, duk_int_t line) {
  40294. #else
  40295. DUK_INTERNAL void duk_err_create_and_throw(duk_hthread *thr, duk_errcode_t code) {
  40296. #endif
  40297. #if defined(DUK_USE_VERBOSE_ERRORS)
  40298. DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%ld, msg=%s, filename=%s, line=%ld",
  40299. (long) code, (const char *) msg,
  40300. (const char *) filename, (long) line));
  40301. #else
  40302. DUK_DD(DUK_DDPRINT("duk_err_create_and_throw(): code=%ld", (long) code));
  40303. #endif
  40304. DUK_ASSERT(thr != NULL);
  40305. /* Even though nested call is possible because we throw an error when
  40306. * trying to create an error, the potential errors must happen before
  40307. * the longjmp state is configured.
  40308. */
  40309. DUK_ASSERT_LJSTATE_UNSET(thr->heap);
  40310. /* Sync so that augmentation sees up-to-date activations, NULL
  40311. * thr->ptr_curr_pc so that it's not used if side effects occur
  40312. * in augmentation or longjmp handling.
  40313. */
  40314. duk_hthread_sync_and_null_currpc(thr);
  40315. /*
  40316. * Create and push an error object onto the top of stack.
  40317. * The error is potentially augmented before throwing.
  40318. *
  40319. * If a "double error" occurs, use a fixed error instance
  40320. * to avoid further trouble.
  40321. */
  40322. if (thr->heap->creating_error) {
  40323. duk_tval tv_val;
  40324. duk_hobject *h_err;
  40325. thr->heap->creating_error = 0;
  40326. h_err = thr->builtins[DUK_BIDX_DOUBLE_ERROR];
  40327. if (h_err != NULL) {
  40328. DUK_D(DUK_DPRINT("double fault detected -> use built-in fixed 'double error' instance"));
  40329. DUK_TVAL_SET_OBJECT(&tv_val, h_err);
  40330. } else {
  40331. DUK_D(DUK_DPRINT("double fault detected; there is no built-in fixed 'double error' instance "
  40332. "-> use the error code as a number"));
  40333. DUK_TVAL_SET_I32(&tv_val, (duk_int32_t) code);
  40334. }
  40335. duk_err_setup_ljstate1(thr, DUK_LJ_TYPE_THROW, &tv_val);
  40336. /* No augmentation to avoid any allocations or side effects. */
  40337. } else {
  40338. /* Prevent infinite recursion. Extra call stack and C
  40339. * recursion headroom (see GH-191) is added for augmentation.
  40340. * That is now signalled by heap->augmenting error and taken
  40341. * into account in call handling without an explicit limit bump.
  40342. */
  40343. thr->heap->creating_error = 1;
  40344. duk_require_stack(thr, 1);
  40345. /* XXX: usually unnecessary '%s' formatting here, but cannot
  40346. * use 'msg' as a format string directly.
  40347. */
  40348. #if defined(DUK_USE_VERBOSE_ERRORS)
  40349. duk_push_error_object_raw(thr,
  40350. code | DUK_ERRCODE_FLAG_NOBLAME_FILELINE,
  40351. filename,
  40352. line,
  40353. "%s",
  40354. (const char *) msg);
  40355. #else
  40356. duk_push_error_object_raw(thr,
  40357. code | DUK_ERRCODE_FLAG_NOBLAME_FILELINE,
  40358. NULL,
  40359. 0,
  40360. NULL);
  40361. #endif
  40362. /* Note that an alloc error may happen during error augmentation.
  40363. * This may happen both when the original error is an alloc error
  40364. * and when it's something else. Because any error in augmentation
  40365. * must be handled correctly anyway, there's no special check for
  40366. * avoiding it for alloc errors (this differs from Duktape 1.x).
  40367. */
  40368. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  40369. DUK_DDD(DUK_DDDPRINT("THROW ERROR (INTERNAL): %!iT (before throw augment)",
  40370. (duk_tval *) duk_get_tval(thr, -1)));
  40371. duk_err_augment_error_throw(thr);
  40372. #endif
  40373. duk_err_setup_ljstate1(thr, DUK_LJ_TYPE_THROW, DUK_GET_TVAL_NEGIDX(thr, -1));
  40374. thr->heap->creating_error = 0;
  40375. /* Error is now created and we assume no errors can occur any
  40376. * more. Check for debugger Throw integration only when the
  40377. * error is complete. If we enter debugger message loop,
  40378. * creating_error must be 0 so that errors can be thrown in
  40379. * the paused state, e.g. in Eval commands.
  40380. */
  40381. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  40382. duk_err_check_debugger_integration(thr);
  40383. #endif
  40384. }
  40385. /*
  40386. * Finally, longjmp
  40387. */
  40388. DUK_DDD(DUK_DDDPRINT("THROW ERROR (INTERNAL): %!iT, %!iT (after throw augment)",
  40389. (duk_tval *) &thr->heap->lj.value1, (duk_tval *) &thr->heap->lj.value2));
  40390. duk_err_longjmp(thr);
  40391. DUK_UNREACHABLE();
  40392. }
  40393. /*
  40394. * Helper for C function call negative return values.
  40395. */
  40396. DUK_INTERNAL void duk_error_throw_from_negative_rc(duk_hthread *thr, duk_ret_t rc) {
  40397. DUK_ASSERT(thr != NULL);
  40398. DUK_ASSERT(rc < 0);
  40399. /*
  40400. * The __FILE__ and __LINE__ information is intentionally not used in the
  40401. * creation of the error object, as it isn't useful in the tracedata. The
  40402. * tracedata still contains the function which returned the negative return
  40403. * code, and having the file/line of this function isn't very useful.
  40404. *
  40405. * The error messages for DUK_RET_xxx shorthand are intentionally very
  40406. * minimal: they're only really useful for low memory targets.
  40407. */
  40408. duk_error_raw(thr, -rc, NULL, 0, "error (rc %ld)", (long) rc);
  40409. DUK_WO_NORETURN(return;);
  40410. }
  40411. #line 1 "duk_hbuffer_alloc.c"
  40412. /*
  40413. * duk_hbuffer allocation and freeing.
  40414. */
  40415. /* #include duk_internal.h -> already included */
  40416. /* Allocate a new duk_hbuffer of a certain type and return a pointer to it
  40417. * (NULL on error). Write buffer data pointer to 'out_bufdata' (only if
  40418. * allocation successful).
  40419. */
  40420. DUK_INTERNAL duk_hbuffer *duk_hbuffer_alloc(duk_heap *heap, duk_size_t size, duk_small_uint_t flags, void **out_bufdata) {
  40421. duk_hbuffer *res = NULL;
  40422. duk_size_t header_size;
  40423. duk_size_t alloc_size;
  40424. DUK_ASSERT(heap != NULL);
  40425. DUK_ASSERT(out_bufdata != NULL);
  40426. DUK_DDD(DUK_DDDPRINT("allocate hbuffer"));
  40427. /* Size sanity check. Should not be necessary because caller is
  40428. * required to check this, but we don't want to cause a segfault
  40429. * if the size wraps either in duk_size_t computation or when
  40430. * storing the size in a 16-bit field.
  40431. */
  40432. if (size > DUK_HBUFFER_MAX_BYTELEN) {
  40433. DUK_D(DUK_DPRINT("hbuffer alloc failed: size too large: %ld", (long) size));
  40434. return NULL; /* no need to write 'out_bufdata' */
  40435. }
  40436. if (flags & DUK_BUF_FLAG_EXTERNAL) {
  40437. header_size = sizeof(duk_hbuffer_external);
  40438. alloc_size = sizeof(duk_hbuffer_external);
  40439. } else if (flags & DUK_BUF_FLAG_DYNAMIC) {
  40440. header_size = sizeof(duk_hbuffer_dynamic);
  40441. alloc_size = sizeof(duk_hbuffer_dynamic);
  40442. } else {
  40443. header_size = sizeof(duk_hbuffer_fixed);
  40444. alloc_size = sizeof(duk_hbuffer_fixed) + size;
  40445. DUK_ASSERT(alloc_size >= sizeof(duk_hbuffer_fixed)); /* no wrapping */
  40446. }
  40447. res = (duk_hbuffer *) DUK_ALLOC(heap, alloc_size);
  40448. if (DUK_UNLIKELY(res == NULL)) {
  40449. goto alloc_error;
  40450. }
  40451. /* zero everything unless requested not to do so */
  40452. #if defined(DUK_USE_ZERO_BUFFER_DATA)
  40453. duk_memzero((void *) res,
  40454. (flags & DUK_BUF_FLAG_NOZERO) ? header_size : alloc_size);
  40455. #else
  40456. duk_memzero((void *) res, header_size);
  40457. #endif
  40458. if (flags & DUK_BUF_FLAG_EXTERNAL) {
  40459. duk_hbuffer_external *h;
  40460. h = (duk_hbuffer_external *) res;
  40461. DUK_UNREF(h);
  40462. *out_bufdata = NULL;
  40463. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  40464. #if defined(DUK_USE_HEAPPTR16)
  40465. /* the compressed pointer is zeroed which maps to NULL, so nothing to do. */
  40466. #else
  40467. DUK_HBUFFER_EXTERNAL_SET_DATA_PTR(heap, h, NULL);
  40468. #endif
  40469. #endif
  40470. DUK_ASSERT(DUK_HBUFFER_EXTERNAL_GET_DATA_PTR(heap, h) == NULL);
  40471. } else if (flags & DUK_BUF_FLAG_DYNAMIC) {
  40472. duk_hbuffer_dynamic *h = (duk_hbuffer_dynamic *) res;
  40473. void *ptr;
  40474. if (size > 0) {
  40475. DUK_ASSERT(!(flags & DUK_BUF_FLAG_EXTERNAL)); /* alloc external with size zero */
  40476. DUK_DDD(DUK_DDDPRINT("dynamic buffer with nonzero size, alloc actual buffer"));
  40477. #if defined(DUK_USE_ZERO_BUFFER_DATA)
  40478. ptr = DUK_ALLOC_ZEROED(heap, size);
  40479. #else
  40480. ptr = DUK_ALLOC(heap, size);
  40481. #endif
  40482. if (DUK_UNLIKELY(ptr == NULL)) {
  40483. /* Because size > 0, NULL check is correct */
  40484. goto alloc_error;
  40485. }
  40486. *out_bufdata = ptr;
  40487. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap, h, ptr);
  40488. } else {
  40489. *out_bufdata = NULL;
  40490. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  40491. #if defined(DUK_USE_HEAPPTR16)
  40492. /* the compressed pointer is zeroed which maps to NULL, so nothing to do. */
  40493. #else
  40494. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(heap, h, NULL);
  40495. #endif
  40496. #endif
  40497. DUK_ASSERT(DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, h) == NULL);
  40498. }
  40499. } else {
  40500. *out_bufdata = (void *) ((duk_hbuffer_fixed *) (void *) res + 1);
  40501. }
  40502. DUK_HBUFFER_SET_SIZE(res, size);
  40503. DUK_HEAPHDR_SET_TYPE(&res->hdr, DUK_HTYPE_BUFFER);
  40504. if (flags & DUK_BUF_FLAG_DYNAMIC) {
  40505. DUK_HBUFFER_SET_DYNAMIC(res);
  40506. if (flags & DUK_BUF_FLAG_EXTERNAL) {
  40507. DUK_HBUFFER_SET_EXTERNAL(res);
  40508. }
  40509. } else {
  40510. DUK_ASSERT(!(flags & DUK_BUF_FLAG_EXTERNAL));
  40511. }
  40512. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &res->hdr);
  40513. DUK_DDD(DUK_DDDPRINT("allocated hbuffer: %p", (void *) res));
  40514. return res;
  40515. alloc_error:
  40516. DUK_DD(DUK_DDPRINT("hbuffer allocation failed"));
  40517. DUK_FREE(heap, res);
  40518. return NULL; /* no need to write 'out_bufdata' */
  40519. }
  40520. /* For indirect allocs. */
  40521. DUK_INTERNAL void *duk_hbuffer_get_dynalloc_ptr(duk_heap *heap, void *ud) {
  40522. duk_hbuffer_dynamic *buf = (duk_hbuffer_dynamic *) ud;
  40523. DUK_UNREF(heap);
  40524. return (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, buf);
  40525. }
  40526. #line 1 "duk_hbuffer_ops.c"
  40527. /*
  40528. * duk_hbuffer operations such as resizing and inserting/appending data to
  40529. * a dynamic buffer.
  40530. */
  40531. /* #include duk_internal.h -> already included */
  40532. /*
  40533. * Resizing
  40534. */
  40535. DUK_INTERNAL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size) {
  40536. void *res;
  40537. duk_size_t prev_size;
  40538. DUK_ASSERT(thr != NULL);
  40539. DUK_ASSERT(buf != NULL);
  40540. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  40541. DUK_ASSERT(!DUK_HBUFFER_HAS_EXTERNAL(buf));
  40542. /*
  40543. * Maximum size check
  40544. */
  40545. if (new_size > DUK_HBUFFER_MAX_BYTELEN) {
  40546. DUK_ERROR_RANGE(thr, "buffer too long");
  40547. DUK_WO_NORETURN(return;);
  40548. }
  40549. /*
  40550. * Note: use indirect realloc variant just in case mark-and-sweep
  40551. * (finalizers) might resize this same buffer during garbage
  40552. * collection.
  40553. */
  40554. res = DUK_REALLOC_INDIRECT(thr->heap, duk_hbuffer_get_dynalloc_ptr, (void *) buf, new_size);
  40555. if (DUK_LIKELY(res != NULL || new_size == 0)) {
  40556. /* 'res' may be NULL if new allocation size is 0. */
  40557. DUK_DDD(DUK_DDDPRINT("resized dynamic buffer %p:%ld -> %p:%ld",
  40558. (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf),
  40559. (long) DUK_HBUFFER_DYNAMIC_GET_SIZE(buf),
  40560. (void *) res,
  40561. (long) new_size));
  40562. /*
  40563. * The entire allocated buffer area, regardless of actual used
  40564. * size, is kept zeroed in resizes for simplicity. If the buffer
  40565. * is grown, zero the new part.
  40566. */
  40567. prev_size = DUK_HBUFFER_DYNAMIC_GET_SIZE(buf);
  40568. if (new_size > prev_size) {
  40569. DUK_ASSERT(new_size - prev_size > 0);
  40570. #if defined(DUK_USE_ZERO_BUFFER_DATA)
  40571. duk_memzero((void *) ((char *) res + prev_size),
  40572. (duk_size_t) (new_size - prev_size));
  40573. #endif
  40574. }
  40575. DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, new_size);
  40576. DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(thr->heap, buf, res);
  40577. } else {
  40578. DUK_ERROR_ALLOC_FAILED(thr);
  40579. DUK_WO_NORETURN(return;);
  40580. }
  40581. DUK_ASSERT(res != NULL || new_size == 0);
  40582. }
  40583. DUK_INTERNAL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf) {
  40584. DUK_ASSERT(thr != NULL);
  40585. DUK_ASSERT(buf != NULL);
  40586. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
  40587. DUK_ASSERT(!DUK_HBUFFER_HAS_EXTERNAL(buf));
  40588. duk_hbuffer_resize(thr, buf, 0);
  40589. }
  40590. /* #include duk_internal.h -> already included */
  40591. #line 2 "duk_hbufobj_misc.c"
  40592. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  40593. DUK_INTERNAL duk_uint_t duk_hbufobj_clamp_bytelength(duk_hbufobj *h_bufobj, duk_uint_t len) {
  40594. duk_uint_t buf_size;
  40595. duk_uint_t buf_avail;
  40596. DUK_ASSERT(h_bufobj != NULL);
  40597. DUK_ASSERT(h_bufobj->buf != NULL);
  40598. buf_size = (duk_uint_t) DUK_HBUFFER_GET_SIZE(h_bufobj->buf);
  40599. if (h_bufobj->offset > buf_size) {
  40600. /* Slice starting point is beyond current length. */
  40601. return 0;
  40602. }
  40603. buf_avail = buf_size - h_bufobj->offset;
  40604. return buf_avail >= len ? len : buf_avail;
  40605. }
  40606. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  40607. #line 1 "duk_heap_alloc.c"
  40608. /*
  40609. * duk_heap allocation and freeing.
  40610. */
  40611. /* #include duk_internal.h -> already included */
  40612. #if defined(DUK_USE_ROM_STRINGS)
  40613. /* Fixed seed value used with ROM strings. */
  40614. #define DUK__FIXED_HASH_SEED 0xabcd1234
  40615. #endif
  40616. /*
  40617. * Free a heap object.
  40618. *
  40619. * Free heap object and its internal (non-heap) pointers. Assumes that
  40620. * caller has removed the object from heap allocated list or the string
  40621. * intern table, and any weak references (which strings may have) have
  40622. * been already dealt with.
  40623. */
  40624. DUK_INTERNAL void duk_free_hobject(duk_heap *heap, duk_hobject *h) {
  40625. DUK_ASSERT(heap != NULL);
  40626. DUK_ASSERT(h != NULL);
  40627. DUK_FREE(heap, DUK_HOBJECT_GET_PROPS(heap, h));
  40628. if (DUK_HOBJECT_IS_COMPFUNC(h)) {
  40629. duk_hcompfunc *f = (duk_hcompfunc *) h;
  40630. DUK_UNREF(f);
  40631. /* Currently nothing to free; 'data' is a heap object */
  40632. } else if (DUK_HOBJECT_IS_NATFUNC(h)) {
  40633. duk_hnatfunc *f = (duk_hnatfunc *) h;
  40634. DUK_UNREF(f);
  40635. /* Currently nothing to free */
  40636. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  40637. duk_hthread *t = (duk_hthread *) h;
  40638. duk_activation *act;
  40639. DUK_FREE(heap, t->valstack);
  40640. /* Don't free h->resumer because it exists in the heap.
  40641. * Callstack entries also contain function pointers which
  40642. * are not freed for the same reason. They are decref
  40643. * finalized and the targets are freed if necessary based
  40644. * on their refcount (or reachability).
  40645. */
  40646. for (act = t->callstack_curr; act != NULL;) {
  40647. duk_activation *act_next;
  40648. duk_catcher *cat;
  40649. for (cat = act->cat; cat != NULL;) {
  40650. duk_catcher *cat_next;
  40651. cat_next = cat->parent;
  40652. DUK_FREE(heap, (void *) cat);
  40653. cat = cat_next;
  40654. }
  40655. act_next = act->parent;
  40656. DUK_FREE(heap, (void *) act);
  40657. act = act_next;
  40658. }
  40659. /* XXX: with 'caller' property the callstack would need
  40660. * to be unwound to update the 'caller' properties of
  40661. * functions in the callstack.
  40662. */
  40663. } else if (DUK_HOBJECT_IS_BOUNDFUNC(h)) {
  40664. duk_hboundfunc *f = (duk_hboundfunc *) (void *) h;
  40665. DUK_FREE(heap, f->args);
  40666. }
  40667. DUK_FREE(heap, (void *) h);
  40668. }
  40669. DUK_INTERNAL void duk_free_hbuffer(duk_heap *heap, duk_hbuffer *h) {
  40670. DUK_ASSERT(heap != NULL);
  40671. DUK_ASSERT(h != NULL);
  40672. if (DUK_HBUFFER_HAS_DYNAMIC(h) && !DUK_HBUFFER_HAS_EXTERNAL(h)) {
  40673. duk_hbuffer_dynamic *g = (duk_hbuffer_dynamic *) h;
  40674. DUK_DDD(DUK_DDDPRINT("free dynamic buffer %p", (void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, g)));
  40675. DUK_FREE(heap, DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(heap, g));
  40676. }
  40677. DUK_FREE(heap, (void *) h);
  40678. }
  40679. DUK_INTERNAL void duk_free_hstring(duk_heap *heap, duk_hstring *h) {
  40680. DUK_ASSERT(heap != NULL);
  40681. DUK_ASSERT(h != NULL);
  40682. DUK_UNREF(heap);
  40683. DUK_UNREF(h);
  40684. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_FREE)
  40685. if (DUK_HSTRING_HAS_EXTDATA(h)) {
  40686. DUK_DDD(DUK_DDDPRINT("free extstr: hstring %!O, extdata: %p",
  40687. h, DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h)));
  40688. DUK_USE_EXTSTR_FREE(heap->heap_udata, (const void *) DUK_HSTRING_GET_EXTDATA((duk_hstring_external *) h));
  40689. }
  40690. #endif
  40691. DUK_FREE(heap, (void *) h);
  40692. }
  40693. DUK_INTERNAL void duk_heap_free_heaphdr_raw(duk_heap *heap, duk_heaphdr *hdr) {
  40694. DUK_ASSERT(heap);
  40695. DUK_ASSERT(hdr);
  40696. DUK_DDD(DUK_DDDPRINT("free heaphdr %p, htype %ld", (void *) hdr, (long) DUK_HEAPHDR_GET_TYPE(hdr)));
  40697. switch (DUK_HEAPHDR_GET_TYPE(hdr)) {
  40698. case DUK_HTYPE_STRING:
  40699. duk_free_hstring(heap, (duk_hstring *) hdr);
  40700. break;
  40701. case DUK_HTYPE_OBJECT:
  40702. duk_free_hobject(heap, (duk_hobject *) hdr);
  40703. break;
  40704. default:
  40705. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) == DUK_HTYPE_BUFFER);
  40706. duk_free_hbuffer(heap, (duk_hbuffer *) hdr);
  40707. }
  40708. }
  40709. /*
  40710. * Free the heap.
  40711. *
  40712. * Frees heap-related non-heap-tracked allocations such as the
  40713. * string intern table; then frees the heap allocated objects;
  40714. * and finally frees the heap structure itself. Reference counts
  40715. * and GC markers are ignored (and not updated) in this process,
  40716. * and finalizers won't be called.
  40717. *
  40718. * The heap pointer and heap object pointers must not be used
  40719. * after this call.
  40720. */
  40721. #if defined(DUK_USE_CACHE_ACTIVATION)
  40722. DUK_LOCAL duk_size_t duk__heap_free_activation_freelist(duk_heap *heap) {
  40723. duk_activation *act;
  40724. duk_activation *act_next;
  40725. duk_size_t count_act = 0;
  40726. for (act = heap->activation_free; act != NULL;) {
  40727. act_next = act->parent;
  40728. DUK_FREE(heap, (void *) act);
  40729. act = act_next;
  40730. #if defined(DUK_USE_DEBUG)
  40731. count_act++;
  40732. #endif
  40733. }
  40734. heap->activation_free = NULL; /* needed when called from mark-and-sweep */
  40735. return count_act;
  40736. }
  40737. #endif /* DUK_USE_CACHE_ACTIVATION */
  40738. #if defined(DUK_USE_CACHE_CATCHER)
  40739. DUK_LOCAL duk_size_t duk__heap_free_catcher_freelist(duk_heap *heap) {
  40740. duk_catcher *cat;
  40741. duk_catcher *cat_next;
  40742. duk_size_t count_cat = 0;
  40743. for (cat = heap->catcher_free; cat != NULL;) {
  40744. cat_next = cat->parent;
  40745. DUK_FREE(heap, (void *) cat);
  40746. cat = cat_next;
  40747. #if defined(DUK_USE_DEBUG)
  40748. count_cat++;
  40749. #endif
  40750. }
  40751. heap->catcher_free = NULL; /* needed when called from mark-and-sweep */
  40752. return count_cat;
  40753. }
  40754. #endif /* DUK_USE_CACHE_CATCHER */
  40755. DUK_INTERNAL void duk_heap_free_freelists(duk_heap *heap) {
  40756. duk_size_t count_act = 0;
  40757. duk_size_t count_cat = 0;
  40758. #if defined(DUK_USE_CACHE_ACTIVATION)
  40759. count_act = duk__heap_free_activation_freelist(heap);
  40760. #endif
  40761. #if defined(DUK_USE_CACHE_CATCHER)
  40762. count_cat = duk__heap_free_catcher_freelist(heap);
  40763. #endif
  40764. DUK_UNREF(heap);
  40765. DUK_UNREF(count_act);
  40766. DUK_UNREF(count_cat);
  40767. DUK_D(DUK_DPRINT("freed %ld activation freelist entries, %ld catcher freelist entries",
  40768. (long) count_act, (long) count_cat));
  40769. }
  40770. DUK_LOCAL void duk__free_allocated(duk_heap *heap) {
  40771. duk_heaphdr *curr;
  40772. duk_heaphdr *next;
  40773. curr = heap->heap_allocated;
  40774. while (curr) {
  40775. /* We don't log or warn about freeing zero refcount objects
  40776. * because they may happen with finalizer processing.
  40777. */
  40778. DUK_DDD(DUK_DDDPRINT("FINALFREE (allocated): %!iO",
  40779. (duk_heaphdr *) curr));
  40780. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  40781. duk_heap_free_heaphdr_raw(heap, curr);
  40782. curr = next;
  40783. }
  40784. }
  40785. #if defined(DUK_USE_FINALIZER_SUPPORT)
  40786. DUK_LOCAL void duk__free_finalize_list(duk_heap *heap) {
  40787. duk_heaphdr *curr;
  40788. duk_heaphdr *next;
  40789. curr = heap->finalize_list;
  40790. while (curr) {
  40791. DUK_DDD(DUK_DDDPRINT("FINALFREE (finalize_list): %!iO",
  40792. (duk_heaphdr *) curr));
  40793. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  40794. duk_heap_free_heaphdr_raw(heap, curr);
  40795. curr = next;
  40796. }
  40797. }
  40798. #endif /* DUK_USE_FINALIZER_SUPPORT */
  40799. DUK_LOCAL void duk__free_stringtable(duk_heap *heap) {
  40800. /* strings are only tracked by stringtable */
  40801. duk_heap_strtable_free(heap);
  40802. }
  40803. #if defined(DUK_USE_FINALIZER_SUPPORT)
  40804. DUK_LOCAL void duk__free_run_finalizers(duk_heap *heap) {
  40805. duk_heaphdr *curr;
  40806. duk_uint_t round_no;
  40807. duk_size_t count_all;
  40808. duk_size_t count_finalized;
  40809. duk_size_t curr_limit;
  40810. DUK_ASSERT(heap != NULL);
  40811. #if defined(DUK_USE_REFERENCE_COUNTING)
  40812. DUK_ASSERT(heap->refzero_list == NULL); /* refzero not running -> must be empty */
  40813. #endif
  40814. DUK_ASSERT(heap->finalize_list == NULL); /* mark-and-sweep last pass */
  40815. if (heap->heap_thread == NULL) {
  40816. /* May happen when heap allocation fails right off. There
  40817. * cannot be any finalizable objects in this case.
  40818. */
  40819. DUK_D(DUK_DPRINT("no heap_thread in heap destruct, assume no finalizable objects"));
  40820. return;
  40821. }
  40822. /* Prevent finalize_list processing and mark-and-sweep entirely.
  40823. * Setting ms_running = 1 also prevents refzero handling from moving
  40824. * objects away from the heap_allocated list (the flag name is a bit
  40825. * misleading here).
  40826. */
  40827. DUK_ASSERT(heap->pf_prevent_count == 0);
  40828. heap->pf_prevent_count = 1;
  40829. DUK_ASSERT(heap->ms_running == 0);
  40830. heap->ms_running = 1;
  40831. DUK_ASSERT(heap->ms_prevent_count == 0);
  40832. heap->ms_prevent_count = 1; /* Bump, because mark-and-sweep assumes it's bumped when ms_running is set. */
  40833. curr_limit = 0; /* suppress warning, not used */
  40834. for (round_no = 0; ; round_no++) {
  40835. curr = heap->heap_allocated;
  40836. count_all = 0;
  40837. count_finalized = 0;
  40838. while (curr) {
  40839. count_all++;
  40840. if (DUK_HEAPHDR_IS_OBJECT(curr)) {
  40841. /* Only objects in heap_allocated may have finalizers. Check that
  40842. * the object itself has a _Finalizer property (own or inherited)
  40843. * so that we don't execute finalizers for e.g. Proxy objects.
  40844. */
  40845. DUK_ASSERT(curr != NULL);
  40846. if (DUK_HOBJECT_HAS_FINALIZER_FAST(heap, (duk_hobject *) curr)) {
  40847. if (!DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) curr)) {
  40848. DUK_ASSERT(DUK_HEAP_HAS_FINALIZER_NORESCUE(heap)); /* maps to finalizer 2nd argument */
  40849. duk_heap_run_finalizer(heap, (duk_hobject *) curr);
  40850. count_finalized++;
  40851. }
  40852. }
  40853. }
  40854. curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
  40855. }
  40856. /* Each round of finalizer execution may spawn new finalizable objects
  40857. * which is normal behavior for some applications. Allow multiple
  40858. * rounds of finalization, but use a shrinking limit based on the
  40859. * first round to detect the case where a runaway finalizer creates
  40860. * an unbounded amount of new finalizable objects. Finalizer rescue
  40861. * is not supported: the semantics are unclear because most of the
  40862. * objects being finalized here are already reachable. The finalizer
  40863. * is given a boolean to indicate that rescue is not possible.
  40864. *
  40865. * See discussion in: https://github.com/svaarala/duktape/pull/473
  40866. */
  40867. if (round_no == 0) {
  40868. /* Cannot wrap: each object is at least 8 bytes so count is
  40869. * at most 1/8 of that.
  40870. */
  40871. curr_limit = count_all * 2;
  40872. } else {
  40873. curr_limit = (curr_limit * 3) / 4; /* Decrease by 25% every round */
  40874. }
  40875. DUK_D(DUK_DPRINT("finalizer round %ld complete, %ld objects, tried to execute %ld finalizers, current limit is %ld",
  40876. (long) round_no, (long) count_all, (long) count_finalized, (long) curr_limit));
  40877. if (count_finalized == 0) {
  40878. DUK_D(DUK_DPRINT("no more finalizable objects, forced finalization finished"));
  40879. break;
  40880. }
  40881. if (count_finalized >= curr_limit) {
  40882. DUK_D(DUK_DPRINT("finalizer count above limit, potentially runaway finalizer; skip remaining finalizers"));
  40883. break;
  40884. }
  40885. }
  40886. DUK_ASSERT(heap->ms_running == 1);
  40887. heap->ms_running = 0;
  40888. DUK_ASSERT(heap->pf_prevent_count == 1);
  40889. heap->pf_prevent_count = 0;
  40890. }
  40891. #endif /* DUK_USE_FINALIZER_SUPPORT */
  40892. DUK_INTERNAL void duk_heap_free(duk_heap *heap) {
  40893. DUK_D(DUK_DPRINT("free heap: %p", (void *) heap));
  40894. #if defined(DUK_USE_DEBUG)
  40895. duk_heap_strtable_dump(heap);
  40896. #endif
  40897. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  40898. /* Detach a debugger if attached (can be called multiple times)
  40899. * safely.
  40900. */
  40901. /* XXX: Add a flag to reject an attempt to re-attach? Otherwise
  40902. * the detached callback may immediately reattach.
  40903. */
  40904. duk_debug_do_detach(heap);
  40905. #endif
  40906. /* Execute finalizers before freeing the heap, even for reachable
  40907. * objects. This gives finalizers the chance to free any native
  40908. * resources like file handles, allocations made outside Duktape,
  40909. * etc. This is quite tricky to get right, so that all finalizer
  40910. * guarantees are honored.
  40911. *
  40912. * Run mark-and-sweep a few times just in case (unreachable object
  40913. * finalizers run already here). The last round must rescue objects
  40914. * from the previous round without running any more finalizers. This
  40915. * ensures rescued objects get their FINALIZED flag cleared so that
  40916. * their finalizer is called once more in forced finalization to
  40917. * satisfy finalizer guarantees. However, we don't want to run any
  40918. * more finalizers because that'd required one more loop, and so on.
  40919. *
  40920. * XXX: this perhaps requires an execution time limit.
  40921. */
  40922. DUK_D(DUK_DPRINT("execute finalizers before freeing heap"));
  40923. DUK_ASSERT(heap->pf_skip_finalizers == 0);
  40924. DUK_D(DUK_DPRINT("forced gc #1 in heap destruction"));
  40925. duk_heap_mark_and_sweep(heap, 0);
  40926. DUK_D(DUK_DPRINT("forced gc #2 in heap destruction"));
  40927. duk_heap_mark_and_sweep(heap, 0);
  40928. DUK_D(DUK_DPRINT("forced gc #3 in heap destruction (don't run finalizers)"));
  40929. heap->pf_skip_finalizers = 1;
  40930. duk_heap_mark_and_sweep(heap, 0); /* Skip finalizers; queue finalizable objects to heap_allocated. */
  40931. /* There are never objects in refzero_list at this point, or at any
  40932. * point beyond a DECREF (even a DECREF_NORZ). Since Duktape 2.1
  40933. * refzero_list processing is side effect free, so it is always
  40934. * processed to completion by a DECREF initially triggering a zero
  40935. * refcount.
  40936. */
  40937. #if defined(DUK_USE_REFERENCE_COUNTING)
  40938. DUK_ASSERT(heap->refzero_list == NULL); /* Always processed to completion inline. */
  40939. #endif
  40940. #if defined(DUK_USE_FINALIZER_SUPPORT)
  40941. DUK_ASSERT(heap->finalize_list == NULL); /* Last mark-and-sweep with skip_finalizers. */
  40942. #endif
  40943. #if defined(DUK_USE_FINALIZER_SUPPORT)
  40944. DUK_D(DUK_DPRINT("run finalizers for remaining finalizable objects"));
  40945. DUK_HEAP_SET_FINALIZER_NORESCUE(heap); /* Rescue no longer supported. */
  40946. duk__free_run_finalizers(heap);
  40947. #endif /* DUK_USE_FINALIZER_SUPPORT */
  40948. /* Note: heap->heap_thread, heap->curr_thread, and heap->heap_object
  40949. * are on the heap allocated list.
  40950. */
  40951. DUK_D(DUK_DPRINT("freeing temporary freelists"));
  40952. duk_heap_free_freelists(heap);
  40953. DUK_D(DUK_DPRINT("freeing heap_allocated of heap: %p", (void *) heap));
  40954. duk__free_allocated(heap);
  40955. #if defined(DUK_USE_REFERENCE_COUNTING)
  40956. DUK_ASSERT(heap->refzero_list == NULL); /* Always processed to completion inline. */
  40957. #endif
  40958. #if defined(DUK_USE_FINALIZER_SUPPORT)
  40959. DUK_D(DUK_DPRINT("freeing finalize_list of heap: %p", (void *) heap));
  40960. duk__free_finalize_list(heap);
  40961. #endif
  40962. DUK_D(DUK_DPRINT("freeing string table of heap: %p", (void *) heap));
  40963. duk__free_stringtable(heap);
  40964. DUK_D(DUK_DPRINT("freeing heap structure: %p", (void *) heap));
  40965. heap->free_func(heap->heap_udata, heap);
  40966. }
  40967. /*
  40968. * Allocate a heap.
  40969. *
  40970. * String table is initialized with built-in strings from genbuiltins.py,
  40971. * either by dynamically creating the strings or by referring to ROM strings.
  40972. */
  40973. #if defined(DUK_USE_ROM_STRINGS)
  40974. DUK_LOCAL duk_bool_t duk__init_heap_strings(duk_heap *heap) {
  40975. #if defined(DUK_USE_ASSERTIONS)
  40976. duk_small_uint_t i;
  40977. #endif
  40978. DUK_UNREF(heap);
  40979. /* With ROM-based strings, heap->strs[] and thr->strs[] are omitted
  40980. * so nothing to initialize for strs[].
  40981. */
  40982. #if defined(DUK_USE_ASSERTIONS)
  40983. for (i = 0; i < sizeof(duk_rom_strings_lookup) / sizeof(const duk_hstring *); i++) {
  40984. const duk_hstring *h;
  40985. duk_uint32_t hash;
  40986. h = duk_rom_strings_lookup[i];
  40987. while (h != NULL) {
  40988. hash = duk_heap_hashstring(heap, (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  40989. DUK_DD(DUK_DDPRINT("duk_rom_strings_lookup[%d] -> hash 0x%08lx, computed 0x%08lx",
  40990. (int) i, (unsigned long) DUK_HSTRING_GET_HASH(h), (unsigned long) hash));
  40991. DUK_ASSERT(hash == (duk_uint32_t) DUK_HSTRING_GET_HASH(h));
  40992. h = (const duk_hstring *) h->hdr.h_next;
  40993. }
  40994. }
  40995. #endif
  40996. return 1;
  40997. }
  40998. #else /* DUK_USE_ROM_STRINGS */
  40999. DUK_LOCAL duk_bool_t duk__init_heap_strings(duk_heap *heap) {
  41000. duk_bitdecoder_ctx bd_ctx;
  41001. duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
  41002. duk_small_uint_t i;
  41003. duk_memzero(&bd_ctx, sizeof(bd_ctx));
  41004. bd->data = (const duk_uint8_t *) duk_strings_data;
  41005. bd->length = (duk_size_t) DUK_STRDATA_DATA_LENGTH;
  41006. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  41007. duk_uint8_t tmp[DUK_STRDATA_MAX_STRLEN];
  41008. duk_small_uint_t len;
  41009. duk_hstring *h;
  41010. len = duk_bd_decode_bitpacked_string(bd, tmp);
  41011. /* No need to length check string: it will never exceed even
  41012. * the 16-bit length maximum.
  41013. */
  41014. DUK_ASSERT(len <= 0xffffUL);
  41015. DUK_DDD(DUK_DDDPRINT("intern built-in string %ld", (long) i));
  41016. h = duk_heap_strtable_intern(heap, tmp, len);
  41017. if (!h) {
  41018. goto failed;
  41019. }
  41020. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h));
  41021. /* Special flags checks. Since these strings are always
  41022. * reachable and a string cannot appear twice in the string
  41023. * table, there's no need to check/set these flags elsewhere.
  41024. * The 'internal' flag is set by string intern code.
  41025. */
  41026. if (i == DUK_STRIDX_EVAL || i == DUK_STRIDX_LC_ARGUMENTS) {
  41027. DUK_HSTRING_SET_EVAL_OR_ARGUMENTS(h);
  41028. }
  41029. if (i >= DUK_STRIDX_START_RESERVED && i < DUK_STRIDX_END_RESERVED) {
  41030. DUK_HSTRING_SET_RESERVED_WORD(h);
  41031. if (i >= DUK_STRIDX_START_STRICT_RESERVED) {
  41032. DUK_HSTRING_SET_STRICT_RESERVED_WORD(h);
  41033. }
  41034. }
  41035. DUK_DDD(DUK_DDDPRINT("interned: %!O", (duk_heaphdr *) h));
  41036. /* XXX: The incref macro takes a thread pointer but doesn't
  41037. * use it right now.
  41038. */
  41039. DUK_HSTRING_INCREF(_never_referenced_, h);
  41040. #if defined(DUK_USE_HEAPPTR16)
  41041. heap->strs16[i] = DUK_USE_HEAPPTR_ENC16(heap->heap_udata, (void *) h);
  41042. #else
  41043. heap->strs[i] = h;
  41044. #endif
  41045. }
  41046. return 1;
  41047. failed:
  41048. return 0;
  41049. }
  41050. #endif /* DUK_USE_ROM_STRINGS */
  41051. DUK_LOCAL duk_bool_t duk__init_heap_thread(duk_heap *heap) {
  41052. duk_hthread *thr;
  41053. DUK_D(DUK_DPRINT("heap init: alloc heap thread"));
  41054. thr = duk_hthread_alloc_unchecked(heap,
  41055. DUK_HOBJECT_FLAG_EXTENSIBLE |
  41056. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_THREAD));
  41057. if (thr == NULL) {
  41058. DUK_D(DUK_DPRINT("failed to alloc heap_thread"));
  41059. return 0;
  41060. }
  41061. thr->state = DUK_HTHREAD_STATE_INACTIVE;
  41062. #if defined(DUK_USE_ROM_STRINGS)
  41063. /* No strs[] pointer. */
  41064. #else /* DUK_USE_ROM_STRINGS */
  41065. #if defined(DUK_USE_HEAPPTR16)
  41066. thr->strs16 = heap->strs16;
  41067. #else
  41068. thr->strs = heap->strs;
  41069. #endif
  41070. #endif /* DUK_USE_ROM_STRINGS */
  41071. heap->heap_thread = thr;
  41072. DUK_HTHREAD_INCREF(thr, thr); /* Note: first argument not really used */
  41073. /* 'thr' is now reachable */
  41074. DUK_D(DUK_DPRINT("heap init: init heap thread stacks"));
  41075. if (!duk_hthread_init_stacks(heap, thr)) {
  41076. return 0;
  41077. }
  41078. /* XXX: this may now fail, and is not handled correctly */
  41079. duk_hthread_create_builtin_objects(thr);
  41080. /* default prototype */
  41081. DUK_HOBJECT_SET_PROTOTYPE_INIT_INCREF(thr, (duk_hobject *) thr, thr->builtins[DUK_BIDX_THREAD_PROTOTYPE]);
  41082. return 1;
  41083. }
  41084. #if defined(DUK_USE_DEBUG)
  41085. #define DUK__DUMPSZ(t) do { \
  41086. DUK_D(DUK_DPRINT("" #t "=%ld", (long) sizeof(t))); \
  41087. } while (0)
  41088. /* These is not 100% because format would need to be non-portable "long long".
  41089. * Also print out as doubles to catch cases where the "long" type is not wide
  41090. * enough; the limits will then not be printed accurately but the magnitude
  41091. * will be correct.
  41092. */
  41093. #define DUK__DUMPLM_SIGNED_RAW(t,a,b) do { \
  41094. DUK_D(DUK_DPRINT(t "=[%ld,%ld]=[%lf,%lf]", \
  41095. (long) (a), (long) (b), \
  41096. (double) (a), (double) (b))); \
  41097. } while (0)
  41098. #define DUK__DUMPLM_UNSIGNED_RAW(t,a,b) do { \
  41099. DUK_D(DUK_DPRINT(t "=[%lu,%lu]=[%lf,%lf]", \
  41100. (unsigned long) (a), (unsigned long) (b), \
  41101. (double) (a), (double) (b))); \
  41102. } while (0)
  41103. #define DUK__DUMPLM_SIGNED(t) do { \
  41104. DUK__DUMPLM_SIGNED_RAW("DUK_" #t "_{MIN,MAX}", DUK_##t##_MIN, DUK_##t##_MAX); \
  41105. } while (0)
  41106. #define DUK__DUMPLM_UNSIGNED(t) do { \
  41107. DUK__DUMPLM_UNSIGNED_RAW("DUK_" #t "_{MIN,MAX}", DUK_##t##_MIN, DUK_##t##_MAX); \
  41108. } while (0)
  41109. DUK_LOCAL void duk__dump_type_sizes(void) {
  41110. DUK_D(DUK_DPRINT("sizeof()"));
  41111. /* basic platform types */
  41112. DUK__DUMPSZ(char);
  41113. DUK__DUMPSZ(short);
  41114. DUK__DUMPSZ(int);
  41115. DUK__DUMPSZ(long);
  41116. DUK__DUMPSZ(double);
  41117. DUK__DUMPSZ(void *);
  41118. DUK__DUMPSZ(size_t);
  41119. /* basic types from duk_features.h */
  41120. DUK__DUMPSZ(duk_uint8_t);
  41121. DUK__DUMPSZ(duk_int8_t);
  41122. DUK__DUMPSZ(duk_uint16_t);
  41123. DUK__DUMPSZ(duk_int16_t);
  41124. DUK__DUMPSZ(duk_uint32_t);
  41125. DUK__DUMPSZ(duk_int32_t);
  41126. DUK__DUMPSZ(duk_uint64_t);
  41127. DUK__DUMPSZ(duk_int64_t);
  41128. DUK__DUMPSZ(duk_uint_least8_t);
  41129. DUK__DUMPSZ(duk_int_least8_t);
  41130. DUK__DUMPSZ(duk_uint_least16_t);
  41131. DUK__DUMPSZ(duk_int_least16_t);
  41132. DUK__DUMPSZ(duk_uint_least32_t);
  41133. DUK__DUMPSZ(duk_int_least32_t);
  41134. #if defined(DUK_USE_64BIT_OPS)
  41135. DUK__DUMPSZ(duk_uint_least64_t);
  41136. DUK__DUMPSZ(duk_int_least64_t);
  41137. #endif
  41138. DUK__DUMPSZ(duk_uint_fast8_t);
  41139. DUK__DUMPSZ(duk_int_fast8_t);
  41140. DUK__DUMPSZ(duk_uint_fast16_t);
  41141. DUK__DUMPSZ(duk_int_fast16_t);
  41142. DUK__DUMPSZ(duk_uint_fast32_t);
  41143. DUK__DUMPSZ(duk_int_fast32_t);
  41144. #if defined(DUK_USE_64BIT_OPS)
  41145. DUK__DUMPSZ(duk_uint_fast64_t);
  41146. DUK__DUMPSZ(duk_int_fast64_t);
  41147. #endif
  41148. DUK__DUMPSZ(duk_uintptr_t);
  41149. DUK__DUMPSZ(duk_intptr_t);
  41150. DUK__DUMPSZ(duk_uintmax_t);
  41151. DUK__DUMPSZ(duk_intmax_t);
  41152. DUK__DUMPSZ(duk_double_t);
  41153. /* important chosen base types */
  41154. DUK__DUMPSZ(duk_int_t);
  41155. DUK__DUMPSZ(duk_uint_t);
  41156. DUK__DUMPSZ(duk_int_fast_t);
  41157. DUK__DUMPSZ(duk_uint_fast_t);
  41158. DUK__DUMPSZ(duk_small_int_t);
  41159. DUK__DUMPSZ(duk_small_uint_t);
  41160. DUK__DUMPSZ(duk_small_int_fast_t);
  41161. DUK__DUMPSZ(duk_small_uint_fast_t);
  41162. /* some derived types */
  41163. DUK__DUMPSZ(duk_codepoint_t);
  41164. DUK__DUMPSZ(duk_ucodepoint_t);
  41165. DUK__DUMPSZ(duk_idx_t);
  41166. DUK__DUMPSZ(duk_errcode_t);
  41167. DUK__DUMPSZ(duk_uarridx_t);
  41168. /* tval */
  41169. DUK__DUMPSZ(duk_double_union);
  41170. DUK__DUMPSZ(duk_tval);
  41171. /* structs from duk_forwdecl.h */
  41172. DUK__DUMPSZ(duk_jmpbuf); /* just one 'int' for C++ exceptions */
  41173. DUK__DUMPSZ(duk_heaphdr);
  41174. DUK__DUMPSZ(duk_heaphdr_string);
  41175. DUK__DUMPSZ(duk_hstring);
  41176. DUK__DUMPSZ(duk_hstring_external);
  41177. DUK__DUMPSZ(duk_hobject);
  41178. DUK__DUMPSZ(duk_harray);
  41179. DUK__DUMPSZ(duk_hcompfunc);
  41180. DUK__DUMPSZ(duk_hnatfunc);
  41181. DUK__DUMPSZ(duk_hdecenv);
  41182. DUK__DUMPSZ(duk_hobjenv);
  41183. DUK__DUMPSZ(duk_hthread);
  41184. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  41185. DUK__DUMPSZ(duk_hbufobj);
  41186. #endif
  41187. DUK__DUMPSZ(duk_hproxy);
  41188. DUK__DUMPSZ(duk_hbuffer);
  41189. DUK__DUMPSZ(duk_hbuffer_fixed);
  41190. DUK__DUMPSZ(duk_hbuffer_dynamic);
  41191. DUK__DUMPSZ(duk_hbuffer_external);
  41192. DUK__DUMPSZ(duk_propaccessor);
  41193. DUK__DUMPSZ(duk_propvalue);
  41194. DUK__DUMPSZ(duk_propdesc);
  41195. DUK__DUMPSZ(duk_heap);
  41196. DUK__DUMPSZ(duk_activation);
  41197. DUK__DUMPSZ(duk_catcher);
  41198. DUK__DUMPSZ(duk_strcache_entry);
  41199. DUK__DUMPSZ(duk_litcache_entry);
  41200. DUK__DUMPSZ(duk_ljstate);
  41201. DUK__DUMPSZ(duk_fixedbuffer);
  41202. DUK__DUMPSZ(duk_bitdecoder_ctx);
  41203. DUK__DUMPSZ(duk_bitencoder_ctx);
  41204. DUK__DUMPSZ(duk_token);
  41205. DUK__DUMPSZ(duk_re_token);
  41206. DUK__DUMPSZ(duk_lexer_point);
  41207. DUK__DUMPSZ(duk_lexer_ctx);
  41208. DUK__DUMPSZ(duk_compiler_instr);
  41209. DUK__DUMPSZ(duk_compiler_func);
  41210. DUK__DUMPSZ(duk_compiler_ctx);
  41211. DUK__DUMPSZ(duk_re_matcher_ctx);
  41212. DUK__DUMPSZ(duk_re_compiler_ctx);
  41213. }
  41214. DUK_LOCAL void duk__dump_type_limits(void) {
  41215. DUK_D(DUK_DPRINT("limits"));
  41216. /* basic types */
  41217. DUK__DUMPLM_SIGNED(INT8);
  41218. DUK__DUMPLM_UNSIGNED(UINT8);
  41219. DUK__DUMPLM_SIGNED(INT_FAST8);
  41220. DUK__DUMPLM_UNSIGNED(UINT_FAST8);
  41221. DUK__DUMPLM_SIGNED(INT_LEAST8);
  41222. DUK__DUMPLM_UNSIGNED(UINT_LEAST8);
  41223. DUK__DUMPLM_SIGNED(INT16);
  41224. DUK__DUMPLM_UNSIGNED(UINT16);
  41225. DUK__DUMPLM_SIGNED(INT_FAST16);
  41226. DUK__DUMPLM_UNSIGNED(UINT_FAST16);
  41227. DUK__DUMPLM_SIGNED(INT_LEAST16);
  41228. DUK__DUMPLM_UNSIGNED(UINT_LEAST16);
  41229. DUK__DUMPLM_SIGNED(INT32);
  41230. DUK__DUMPLM_UNSIGNED(UINT32);
  41231. DUK__DUMPLM_SIGNED(INT_FAST32);
  41232. DUK__DUMPLM_UNSIGNED(UINT_FAST32);
  41233. DUK__DUMPLM_SIGNED(INT_LEAST32);
  41234. DUK__DUMPLM_UNSIGNED(UINT_LEAST32);
  41235. #if defined(DUK_USE_64BIT_OPS)
  41236. DUK__DUMPLM_SIGNED(INT64);
  41237. DUK__DUMPLM_UNSIGNED(UINT64);
  41238. DUK__DUMPLM_SIGNED(INT_FAST64);
  41239. DUK__DUMPLM_UNSIGNED(UINT_FAST64);
  41240. DUK__DUMPLM_SIGNED(INT_LEAST64);
  41241. DUK__DUMPLM_UNSIGNED(UINT_LEAST64);
  41242. #endif
  41243. DUK__DUMPLM_SIGNED(INTPTR);
  41244. DUK__DUMPLM_UNSIGNED(UINTPTR);
  41245. DUK__DUMPLM_SIGNED(INTMAX);
  41246. DUK__DUMPLM_UNSIGNED(UINTMAX);
  41247. /* derived types */
  41248. DUK__DUMPLM_SIGNED(INT);
  41249. DUK__DUMPLM_UNSIGNED(UINT);
  41250. DUK__DUMPLM_SIGNED(INT_FAST);
  41251. DUK__DUMPLM_UNSIGNED(UINT_FAST);
  41252. DUK__DUMPLM_SIGNED(SMALL_INT);
  41253. DUK__DUMPLM_UNSIGNED(SMALL_UINT);
  41254. DUK__DUMPLM_SIGNED(SMALL_INT_FAST);
  41255. DUK__DUMPLM_UNSIGNED(SMALL_UINT_FAST);
  41256. }
  41257. DUK_LOCAL void duk__dump_misc_options(void) {
  41258. DUK_D(DUK_DPRINT("DUK_VERSION: %ld", (long) DUK_VERSION));
  41259. DUK_D(DUK_DPRINT("DUK_GIT_DESCRIBE: %s", DUK_GIT_DESCRIBE));
  41260. DUK_D(DUK_DPRINT("OS string: %s", DUK_USE_OS_STRING));
  41261. DUK_D(DUK_DPRINT("architecture string: %s", DUK_USE_ARCH_STRING));
  41262. DUK_D(DUK_DPRINT("compiler string: %s", DUK_USE_COMPILER_STRING));
  41263. DUK_D(DUK_DPRINT("debug level: %ld", (long) DUK_USE_DEBUG_LEVEL));
  41264. #if defined(DUK_USE_PACKED_TVAL)
  41265. DUK_D(DUK_DPRINT("DUK_USE_PACKED_TVAL: yes"));
  41266. #else
  41267. DUK_D(DUK_DPRINT("DUK_USE_PACKED_TVAL: no"));
  41268. #endif
  41269. #if defined(DUK_USE_VARIADIC_MACROS)
  41270. DUK_D(DUK_DPRINT("DUK_USE_VARIADIC_MACROS: yes"));
  41271. #else
  41272. DUK_D(DUK_DPRINT("DUK_USE_VARIADIC_MACROS: no"));
  41273. #endif
  41274. #if defined(DUK_USE_INTEGER_LE)
  41275. DUK_D(DUK_DPRINT("integer endianness: little"));
  41276. #elif defined(DUK_USE_INTEGER_ME)
  41277. DUK_D(DUK_DPRINT("integer endianness: mixed"));
  41278. #elif defined(DUK_USE_INTEGER_BE)
  41279. DUK_D(DUK_DPRINT("integer endianness: big"));
  41280. #else
  41281. DUK_D(DUK_DPRINT("integer endianness: ???"));
  41282. #endif
  41283. #if defined(DUK_USE_DOUBLE_LE)
  41284. DUK_D(DUK_DPRINT("IEEE double endianness: little"));
  41285. #elif defined(DUK_USE_DOUBLE_ME)
  41286. DUK_D(DUK_DPRINT("IEEE double endianness: mixed"));
  41287. #elif defined(DUK_USE_DOUBLE_BE)
  41288. DUK_D(DUK_DPRINT("IEEE double endianness: big"));
  41289. #else
  41290. DUK_D(DUK_DPRINT("IEEE double endianness: ???"));
  41291. #endif
  41292. }
  41293. #endif /* DUK_USE_DEBUG */
  41294. DUK_INTERNAL
  41295. duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
  41296. duk_realloc_function realloc_func,
  41297. duk_free_function free_func,
  41298. void *heap_udata,
  41299. duk_fatal_function fatal_func) {
  41300. duk_heap *res = NULL;
  41301. duk_uint32_t st_initsize;
  41302. DUK_D(DUK_DPRINT("allocate heap"));
  41303. /*
  41304. * Random config sanity asserts
  41305. */
  41306. DUK_ASSERT(DUK_USE_STRTAB_MINSIZE >= 64);
  41307. DUK_ASSERT((DUK_HTYPE_STRING & 0x01U) == 0);
  41308. DUK_ASSERT((DUK_HTYPE_BUFFER & 0x01U) == 0);
  41309. DUK_ASSERT((DUK_HTYPE_OBJECT & 0x01U) == 1); /* DUK_HEAPHDR_IS_OBJECT() relies ont his. */
  41310. /*
  41311. * Debug dump type sizes
  41312. */
  41313. #if defined(DUK_USE_DEBUG)
  41314. duk__dump_misc_options();
  41315. duk__dump_type_sizes();
  41316. duk__dump_type_limits();
  41317. #endif
  41318. /*
  41319. * If selftests enabled, run them as early as possible.
  41320. */
  41321. #if defined(DUK_USE_SELF_TESTS)
  41322. DUK_D(DUK_DPRINT("run self tests"));
  41323. if (duk_selftest_run_tests(alloc_func, realloc_func, free_func, heap_udata) > 0) {
  41324. fatal_func(heap_udata, "self test(s) failed");
  41325. }
  41326. DUK_D(DUK_DPRINT("self tests passed"));
  41327. #endif
  41328. /*
  41329. * Important assert-like checks that should be enabled even
  41330. * when assertions are otherwise not enabled.
  41331. */
  41332. #if defined(DUK_USE_EXEC_REGCONST_OPTIMIZE)
  41333. /* Can't check sizeof() using preprocessor so explicit check.
  41334. * This will be optimized away in practice; unfortunately a
  41335. * warning is generated on some compilers as a result.
  41336. */
  41337. #if defined(DUK_USE_PACKED_TVAL)
  41338. if (sizeof(duk_tval) != 8) {
  41339. #else
  41340. if (sizeof(duk_tval) != 16) {
  41341. #endif
  41342. fatal_func(heap_udata, "sizeof(duk_tval) not 8 or 16, cannot use DUK_USE_EXEC_REGCONST_OPTIMIZE option");
  41343. }
  41344. #endif /* DUK_USE_EXEC_REGCONST_OPTIMIZE */
  41345. /*
  41346. * Computed values (e.g. INFINITY)
  41347. */
  41348. #if defined(DUK_USE_COMPUTED_NAN)
  41349. do {
  41350. /* Workaround for some exotic platforms where NAN is missing
  41351. * and the expression (0.0 / 0.0) does NOT result in a NaN.
  41352. * Such platforms use the global 'duk_computed_nan' which must
  41353. * be initialized at runtime. Use 'volatile' to ensure that
  41354. * the compiler will actually do the computation and not try
  41355. * to do constant folding which might result in the original
  41356. * problem.
  41357. */
  41358. volatile double dbl1 = 0.0;
  41359. volatile double dbl2 = 0.0;
  41360. duk_computed_nan = dbl1 / dbl2;
  41361. } while (0);
  41362. #endif
  41363. #if defined(DUK_USE_COMPUTED_INFINITY)
  41364. do {
  41365. /* Similar workaround for INFINITY. */
  41366. volatile double dbl1 = 1.0;
  41367. volatile double dbl2 = 0.0;
  41368. duk_computed_infinity = dbl1 / dbl2;
  41369. } while (0);
  41370. #endif
  41371. /*
  41372. * Allocate heap struct
  41373. *
  41374. * Use a raw call, all macros expect the heap to be initialized
  41375. */
  41376. #if defined(DUK_USE_INJECT_HEAP_ALLOC_ERROR) && (DUK_USE_INJECT_HEAP_ALLOC_ERROR == 1)
  41377. goto failed;
  41378. #endif
  41379. DUK_D(DUK_DPRINT("alloc duk_heap object"));
  41380. res = (duk_heap *) alloc_func(heap_udata, sizeof(duk_heap));
  41381. if (!res) {
  41382. goto failed;
  41383. }
  41384. /*
  41385. * Zero the struct, and start initializing roughly in order
  41386. */
  41387. duk_memzero(res, sizeof(*res));
  41388. #if defined(DUK_USE_ASSERTIONS)
  41389. res->heap_initializing = 1;
  41390. #endif
  41391. /* explicit NULL inits */
  41392. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  41393. res->heap_udata = NULL;
  41394. res->heap_allocated = NULL;
  41395. #if defined(DUK_USE_REFERENCE_COUNTING)
  41396. res->refzero_list = NULL;
  41397. #endif
  41398. #if defined(DUK_USE_FINALIZER_SUPPORT)
  41399. res->finalize_list = NULL;
  41400. #if defined(DUK_USE_ASSERTIONS)
  41401. res->currently_finalizing = NULL;
  41402. #endif
  41403. #endif
  41404. #if defined(DUK_USE_CACHE_ACTIVATION)
  41405. res->activation_free = NULL;
  41406. #endif
  41407. #if defined(DUK_USE_CACHE_CATCHER)
  41408. res->catcher_free = NULL;
  41409. #endif
  41410. res->heap_thread = NULL;
  41411. res->curr_thread = NULL;
  41412. res->heap_object = NULL;
  41413. #if defined(DUK_USE_STRTAB_PTRCOMP)
  41414. res->strtable16 = NULL;
  41415. #else
  41416. res->strtable = NULL;
  41417. #endif
  41418. #if defined(DUK_USE_ROM_STRINGS)
  41419. /* no res->strs[] */
  41420. #else /* DUK_USE_ROM_STRINGS */
  41421. #if defined(DUK_USE_HEAPPTR16)
  41422. /* res->strs16[] is zeroed and zero decodes to NULL, so no NULL inits. */
  41423. #else
  41424. {
  41425. duk_small_uint_t i;
  41426. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  41427. res->strs[i] = NULL;
  41428. }
  41429. }
  41430. #endif
  41431. #endif /* DUK_USE_ROM_STRINGS */
  41432. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  41433. res->dbg_read_cb = NULL;
  41434. res->dbg_write_cb = NULL;
  41435. res->dbg_peek_cb = NULL;
  41436. res->dbg_read_flush_cb = NULL;
  41437. res->dbg_write_flush_cb = NULL;
  41438. res->dbg_request_cb = NULL;
  41439. res->dbg_udata = NULL;
  41440. res->dbg_pause_act = NULL;
  41441. #endif
  41442. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  41443. res->alloc_func = alloc_func;
  41444. res->realloc_func = realloc_func;
  41445. res->free_func = free_func;
  41446. res->heap_udata = heap_udata;
  41447. res->fatal_func = fatal_func;
  41448. /* XXX: for now there's a pointer packing zero assumption, i.e.
  41449. * NULL <=> compressed pointer 0. If this is removed, may need
  41450. * to precompute e.g. null16 here.
  41451. */
  41452. /* res->ms_trigger_counter == 0 -> now causes immediate GC; which is OK */
  41453. /* Prevent mark-and-sweep and finalizer execution until heap is completely
  41454. * initialized.
  41455. */
  41456. DUK_ASSERT(res->ms_prevent_count == 0);
  41457. DUK_ASSERT(res->pf_prevent_count == 0);
  41458. res->ms_prevent_count = 1;
  41459. res->pf_prevent_count = 1;
  41460. DUK_ASSERT(res->ms_running == 0);
  41461. res->call_recursion_depth = 0;
  41462. res->call_recursion_limit = DUK_USE_NATIVE_CALL_RECLIMIT;
  41463. /* XXX: use the pointer as a seed for now: mix in time at least */
  41464. /* The casts through duk_uintptr_t is to avoid the following GCC warning:
  41465. *
  41466. * warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  41467. *
  41468. * This still generates a /Wp64 warning on VS2010 when compiling for x86.
  41469. */
  41470. #if defined(DUK_USE_ROM_STRINGS)
  41471. /* XXX: make a common DUK_USE_ option, and allow custom fixed seed? */
  41472. DUK_D(DUK_DPRINT("using rom strings, force heap hash_seed to fixed value 0x%08lx", (long) DUK__FIXED_HASH_SEED));
  41473. res->hash_seed = (duk_uint32_t) DUK__FIXED_HASH_SEED;
  41474. #else /* DUK_USE_ROM_STRINGS */
  41475. res->hash_seed = (duk_uint32_t) (duk_uintptr_t) res;
  41476. #if !defined(DUK_USE_STRHASH_DENSE)
  41477. res->hash_seed ^= 5381; /* Bernstein hash init value is normally 5381; XOR it in in case pointer low bits are 0 */
  41478. #endif
  41479. #endif /* DUK_USE_ROM_STRINGS */
  41480. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  41481. res->lj.jmpbuf_ptr = NULL;
  41482. #endif
  41483. DUK_ASSERT(res->lj.type == DUK_LJ_TYPE_UNKNOWN); /* zero */
  41484. DUK_ASSERT(res->lj.iserror == 0);
  41485. DUK_TVAL_SET_UNDEFINED(&res->lj.value1);
  41486. DUK_TVAL_SET_UNDEFINED(&res->lj.value2);
  41487. DUK_ASSERT_LJSTATE_UNSET(res);
  41488. /*
  41489. * Init stringtable: fixed variant
  41490. */
  41491. st_initsize = DUK_USE_STRTAB_MINSIZE;
  41492. #if defined(DUK_USE_STRTAB_PTRCOMP)
  41493. res->strtable16 = (duk_uint16_t *) alloc_func(heap_udata, sizeof(duk_uint16_t) * st_initsize);
  41494. if (res->strtable16 == NULL) {
  41495. goto failed;
  41496. }
  41497. #else
  41498. res->strtable = (duk_hstring **) alloc_func(heap_udata, sizeof(duk_hstring *) * st_initsize);
  41499. if (res->strtable == NULL) {
  41500. goto failed;
  41501. }
  41502. #endif
  41503. res->st_size = st_initsize;
  41504. res->st_mask = st_initsize - 1;
  41505. #if (DUK_USE_STRTAB_MINSIZE != DUK_USE_STRTAB_MAXSIZE)
  41506. DUK_ASSERT(res->st_count == 0);
  41507. #endif
  41508. #if defined(DUK_USE_STRTAB_PTRCOMP)
  41509. /* zero assumption */
  41510. duk_memzero(res->strtable16, sizeof(duk_uint16_t) * st_initsize);
  41511. #else
  41512. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  41513. {
  41514. duk_uint32_t i;
  41515. for (i = 0; i < st_initsize; i++) {
  41516. res->strtable[i] = NULL;
  41517. }
  41518. }
  41519. #else
  41520. duk_memzero(res->strtable, sizeof(duk_hstring *) * st_initsize);
  41521. #endif /* DUK_USE_EXPLICIT_NULL_INIT */
  41522. #endif /* DUK_USE_STRTAB_PTRCOMP */
  41523. /*
  41524. * Init stringcache
  41525. */
  41526. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  41527. {
  41528. duk_uint_t i;
  41529. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  41530. res->strcache[i].h = NULL;
  41531. }
  41532. }
  41533. #endif
  41534. /*
  41535. * Init litcache
  41536. */
  41537. #if defined(DUK_USE_LITCACHE_SIZE)
  41538. DUK_ASSERT(DUK_USE_LITCACHE_SIZE > 0);
  41539. DUK_ASSERT(DUK_IS_POWER_OF_TWO((duk_uint_t) DUK_USE_LITCACHE_SIZE));
  41540. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  41541. {
  41542. duk_uint_t i;
  41543. for (i = 0; i < DUK_USE_LITCACHE_SIZE; i++) {
  41544. res->litcache[i].addr = NULL;
  41545. res->litcache[i].h = NULL;
  41546. }
  41547. }
  41548. #endif
  41549. #endif /* DUK_USE_LITCACHE_SIZE */
  41550. /* XXX: error handling is incomplete. It would be cleanest if
  41551. * there was a setjmp catchpoint, so that all init code could
  41552. * freely throw errors. If that were the case, the return code
  41553. * passing here could be removed.
  41554. */
  41555. /*
  41556. * Init built-in strings
  41557. */
  41558. #if defined(DUK_USE_INJECT_HEAP_ALLOC_ERROR) && (DUK_USE_INJECT_HEAP_ALLOC_ERROR == 2)
  41559. goto failed;
  41560. #endif
  41561. DUK_D(DUK_DPRINT("heap init: initialize heap strings"));
  41562. if (!duk__init_heap_strings(res)) {
  41563. goto failed;
  41564. }
  41565. /*
  41566. * Init the heap thread
  41567. */
  41568. #if defined(DUK_USE_INJECT_HEAP_ALLOC_ERROR) && (DUK_USE_INJECT_HEAP_ALLOC_ERROR == 3)
  41569. goto failed;
  41570. #endif
  41571. DUK_D(DUK_DPRINT("heap init: initialize heap thread"));
  41572. if (!duk__init_heap_thread(res)) {
  41573. goto failed;
  41574. }
  41575. /*
  41576. * Init the heap object
  41577. */
  41578. #if defined(DUK_USE_INJECT_HEAP_ALLOC_ERROR) && (DUK_USE_INJECT_HEAP_ALLOC_ERROR == 4)
  41579. goto failed;
  41580. #endif
  41581. DUK_D(DUK_DPRINT("heap init: initialize heap object"));
  41582. DUK_ASSERT(res->heap_thread != NULL);
  41583. res->heap_object = duk_hobject_alloc_unchecked(res, DUK_HOBJECT_FLAG_EXTENSIBLE |
  41584. DUK_HOBJECT_FLAG_FASTREFS |
  41585. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT));
  41586. if (res->heap_object == NULL) {
  41587. goto failed;
  41588. }
  41589. DUK_HOBJECT_INCREF(res->heap_thread, res->heap_object);
  41590. /*
  41591. * Odds and ends depending on the heap thread
  41592. */
  41593. #if !defined(DUK_USE_GET_RANDOM_DOUBLE)
  41594. #if defined(DUK_USE_PREFER_SIZE) || !defined(DUK_USE_64BIT_OPS)
  41595. res->rnd_state = (duk_uint32_t) duk_time_get_ecmascript_time(res->heap_thread);
  41596. duk_util_tinyrandom_prepare_seed(res->heap_thread);
  41597. #else
  41598. res->rnd_state[0] = (duk_uint64_t) duk_time_get_ecmascript_time(res->heap_thread);
  41599. DUK_ASSERT(res->rnd_state[1] == 0); /* Not filled here, filled in by seed preparation. */
  41600. #if 0 /* Manual test values matching misc/xoroshiro128plus_test.c. */
  41601. res->rnd_state[0] = DUK_U64_CONSTANT(0xdeadbeef12345678);
  41602. res->rnd_state[1] = DUK_U64_CONSTANT(0xcafed00d12345678);
  41603. #endif
  41604. duk_util_tinyrandom_prepare_seed(res->heap_thread);
  41605. /* Mix in heap pointer: this ensures that if two Duktape heaps are
  41606. * created on the same millisecond, they get a different PRNG
  41607. * sequence (unless e.g. virtual memory addresses cause also the
  41608. * heap object pointer to be the same).
  41609. */
  41610. {
  41611. duk_uint64_t tmp_u64;
  41612. tmp_u64 = 0;
  41613. duk_memcpy((void *) &tmp_u64,
  41614. (const void *) &res,
  41615. (size_t) (sizeof(void *) >= sizeof(duk_uint64_t) ? sizeof(duk_uint64_t) : sizeof(void *)));
  41616. res->rnd_state[1] ^= tmp_u64;
  41617. }
  41618. do {
  41619. duk_small_uint_t i;
  41620. for (i = 0; i < 10; i++) {
  41621. /* Throw away a few initial random numbers just in
  41622. * case. Probably unnecessary due to SplitMix64
  41623. * preparation.
  41624. */
  41625. (void) duk_util_tinyrandom_get_double(res->heap_thread);
  41626. }
  41627. } while (0);
  41628. #endif
  41629. #endif
  41630. /*
  41631. * Allow finalizer and mark-and-sweep processing.
  41632. */
  41633. DUK_D(DUK_DPRINT("heap init: allow finalizer/mark-and-sweep processing"));
  41634. DUK_ASSERT(res->ms_prevent_count == 1);
  41635. DUK_ASSERT(res->pf_prevent_count == 1);
  41636. res->ms_prevent_count = 0;
  41637. res->pf_prevent_count = 0;
  41638. DUK_ASSERT(res->ms_running == 0);
  41639. #if defined(DUK_USE_ASSERTIONS)
  41640. res->heap_initializing = 0;
  41641. #endif
  41642. /*
  41643. * All done.
  41644. */
  41645. DUK_D(DUK_DPRINT("allocated heap: %p", (void *) res));
  41646. return res;
  41647. failed:
  41648. DUK_D(DUK_DPRINT("heap allocation failed"));
  41649. if (res != NULL) {
  41650. /* Assumes that allocated pointers and alloc funcs are valid
  41651. * if res exists.
  41652. */
  41653. DUK_ASSERT(res->ms_prevent_count == 1);
  41654. DUK_ASSERT(res->pf_prevent_count == 1);
  41655. DUK_ASSERT(res->ms_running == 0);
  41656. if (res->heap_thread != NULL) {
  41657. res->ms_prevent_count = 0;
  41658. res->pf_prevent_count = 0;
  41659. }
  41660. #if defined(DUK_USE_ASSERTIONS)
  41661. res->heap_initializing = 0;
  41662. #endif
  41663. DUK_ASSERT(res->alloc_func != NULL);
  41664. DUK_ASSERT(res->realloc_func != NULL);
  41665. DUK_ASSERT(res->free_func != NULL);
  41666. duk_heap_free(res);
  41667. }
  41668. return NULL;
  41669. }
  41670. /* automatic undefs */
  41671. #undef DUK__DUMPLM_SIGNED
  41672. #undef DUK__DUMPLM_SIGNED_RAW
  41673. #undef DUK__DUMPLM_UNSIGNED
  41674. #undef DUK__DUMPLM_UNSIGNED_RAW
  41675. #undef DUK__DUMPSZ
  41676. #undef DUK__FIXED_HASH_SEED
  41677. #line 1 "duk_heap_finalize.c"
  41678. /*
  41679. * Finalizer handling.
  41680. */
  41681. /* #include duk_internal.h -> already included */
  41682. #if defined(DUK_USE_FINALIZER_SUPPORT)
  41683. /*
  41684. * Fake torture finalizer.
  41685. */
  41686. #if defined(DUK_USE_FINALIZER_TORTURE)
  41687. DUK_LOCAL duk_ret_t duk__fake_global_finalizer(duk_hthread *thr) {
  41688. DUK_DD(DUK_DDPRINT("fake global torture finalizer executed"));
  41689. /* Require a lot of stack to force a value stack grow/shrink. */
  41690. duk_require_stack(thr, 100000);
  41691. /* Force a reallocation with pointer change for value stack
  41692. * to maximize side effects.
  41693. */
  41694. duk_hthread_valstack_torture_realloc(thr);
  41695. /* Inner function call, error throw. */
  41696. duk_eval_string_noresult(thr,
  41697. "(function dummy() {\n"
  41698. " dummy.prototype = null; /* break reference loop */\n"
  41699. " try {\n"
  41700. " throw 'fake-finalizer-dummy-error';\n"
  41701. " } catch (e) {\n"
  41702. " void e;\n"
  41703. " }\n"
  41704. "})()");
  41705. /* The above creates garbage (e.g. a function instance). Because
  41706. * the function/prototype reference loop is broken, it gets collected
  41707. * immediately by DECREF. If Function.prototype has a _Finalizer
  41708. * property (happens in some test cases), the garbage gets queued to
  41709. * finalize_list. This still won't cause an infinite loop because
  41710. * the torture finalizer is called once per finalize_list run and
  41711. * the garbage gets handled in the same run. (If the garbage needs
  41712. * mark-and-sweep collection, an infinite loop might ensue.)
  41713. */
  41714. return 0;
  41715. }
  41716. DUK_LOCAL void duk__run_global_torture_finalizer(duk_hthread *thr) {
  41717. DUK_ASSERT(thr != NULL);
  41718. /* Avoid fake finalization when callstack limit is near. Otherwise
  41719. * a callstack limit error will be created, then refzero'ed. The
  41720. * +5 headroom is conservative.
  41721. */
  41722. if (thr->heap->call_recursion_depth + 5 >= thr->heap->call_recursion_limit ||
  41723. thr->callstack_top + 5 >= DUK_USE_CALLSTACK_LIMIT) {
  41724. DUK_D(DUK_DPRINT("skip global torture finalizer, too little headroom for call recursion or call stack size"));
  41725. return;
  41726. }
  41727. /* Run fake finalizer. Avoid creating unnecessary garbage. */
  41728. duk_push_c_function(thr, duk__fake_global_finalizer, 0 /*nargs*/);
  41729. (void) duk_pcall(thr, 0 /*nargs*/);
  41730. duk_pop(thr);
  41731. }
  41732. #endif /* DUK_USE_FINALIZER_TORTURE */
  41733. /*
  41734. * Process the finalize_list to completion.
  41735. *
  41736. * An object may be placed on finalize_list by either refcounting or
  41737. * mark-and-sweep. The refcount of objects placed by refcounting will be
  41738. * zero; the refcount of objects placed by mark-and-sweep is > 0. In both
  41739. * cases the refcount is bumped by 1 artificially so that a REFZERO event
  41740. * can never happen while an object is waiting for finalization. Without
  41741. * this bump a REFZERO could now happen because user code may call
  41742. * duk_push_heapptr() and then pop a value even when it's on finalize_list.
  41743. *
  41744. * List processing assumes refcounts are kept up-to-date at all times, so
  41745. * that once the finalizer returns, a zero refcount is a reliable reason to
  41746. * free the object immediately rather than place it back to the heap. This
  41747. * is the case because we run outside of refzero_list processing so that
  41748. * DECREF cascades are handled fully inline.
  41749. *
  41750. * For mark-and-sweep queued objects (had_zero_refcount false) the object
  41751. * may be freed immediately if its refcount is zero after the finalizer call
  41752. * (i.e. finalizer removed the reference loop for the object). If not, the
  41753. * next mark-and-sweep will collect the object unless it has become reachable
  41754. * (i.e. rescued) by that time and its refcount hasn't fallen to zero before
  41755. * that. Mark-and-sweep detects these objects because their FINALIZED flag
  41756. * is set.
  41757. *
  41758. * There's an inherent limitation for mark-and-sweep finalizer rescuing: an
  41759. * object won't get refinalized if (1) it's rescued, but (2) becomes
  41760. * unreachable before mark-and-sweep has had time to notice it. The next
  41761. * mark-and-sweep round simply doesn't have any information of whether the
  41762. * object has been unreachable the whole time or not (the only way to get
  41763. * that information would be a mark-and-sweep pass for *every finalized
  41764. * object*). This is awkward for the application because the mark-and-sweep
  41765. * round is not generally visible or under full application control.
  41766. *
  41767. * For refcount queued objects (had_zero_refcount true) the object is either
  41768. * immediately freed or rescued, and waiting for a mark-and-sweep round is not
  41769. * necessary (or desirable); FINALIZED is cleared when a rescued object is
  41770. * queued back to heap_allocated. The object is eligible for finalization
  41771. * again (either via refcounting or mark-and-sweep) immediately after being
  41772. * rescued. If a refcount finalized object is placed into an unreachable
  41773. * reference loop by its finalizer, it will get collected by mark-and-sweep
  41774. * and currently the finalizer will execute again.
  41775. *
  41776. * There's a special case where:
  41777. *
  41778. * - Mark-and-sweep queues an object to finalize_list for finalization.
  41779. * - The finalizer is executed, FINALIZED is set, and object is queued
  41780. * back to heap_allocated, waiting for a new mark-and-sweep round.
  41781. * - The object's refcount drops to zero before mark-and-sweep has a
  41782. * chance to run another round and make a rescue/free decision.
  41783. *
  41784. * This is now handled by refzero code: if an object has a finalizer but
  41785. * FINALIZED is already set, the object is freed without finalizer processing.
  41786. * The outcome is the same as if mark-and-sweep was executed at that point;
  41787. * mark-and-sweep would also free the object without another finalizer run.
  41788. * This could also be changed so that the refzero-triggered finalizer *IS*
  41789. * executed: being refzero collected implies someone has operated on the
  41790. * object so it hasn't been totally unreachable the whole time. This would
  41791. * risk a finalizer loop however.
  41792. */
  41793. DUK_INTERNAL void duk_heap_process_finalize_list(duk_heap *heap) {
  41794. duk_heaphdr *curr;
  41795. #if defined(DUK_USE_DEBUG)
  41796. duk_size_t count = 0;
  41797. #endif
  41798. DUK_DDD(DUK_DDDPRINT("duk_heap_process_finalize_list: %p", (void *) heap));
  41799. if (heap->pf_prevent_count != 0) {
  41800. DUK_DDD(DUK_DDDPRINT("skip finalize_list processing: pf_prevent_count != 0"));
  41801. return;
  41802. }
  41803. /* Heap alloc prevents mark-and-sweep before heap_thread is ready. */
  41804. DUK_ASSERT(heap != NULL);
  41805. DUK_ASSERT(heap->heap_thread != NULL);
  41806. DUK_ASSERT(heap->heap_thread->valstack != NULL);
  41807. #if defined(DUK_USE_REFERENCE_COUNTING)
  41808. DUK_ASSERT(heap->refzero_list == NULL);
  41809. #endif
  41810. DUK_ASSERT(heap->pf_prevent_count == 0);
  41811. heap->pf_prevent_count = 1;
  41812. /* Mark-and-sweep no longer needs to be prevented when running
  41813. * finalizers: mark-and-sweep skips any rescue decisions if there
  41814. * are any objects in finalize_list when mark-and-sweep is entered.
  41815. * This protects finalized objects from incorrect rescue decisions
  41816. * caused by finalize_list being a reachability root and only
  41817. * partially processed. Freeing decisions are not postponed.
  41818. */
  41819. /* When finalizer torture is enabled, make a fake finalizer call with
  41820. * maximum side effects regardless of whether finalize_list is empty.
  41821. */
  41822. #if defined(DUK_USE_FINALIZER_TORTURE)
  41823. duk__run_global_torture_finalizer(heap->heap_thread);
  41824. #endif
  41825. /* Process finalize_list until it becomes empty. There's currently no
  41826. * protection against a finalizer always creating more garbage.
  41827. */
  41828. while ((curr = heap->finalize_list) != NULL) {
  41829. #if defined(DUK_USE_REFERENCE_COUNTING)
  41830. duk_bool_t queue_back;
  41831. #endif
  41832. DUK_DD(DUK_DDPRINT("processing finalize_list entry: %p -> %!iO", (void *) curr, curr));
  41833. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT); /* Only objects have finalizers. */
  41834. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(curr));
  41835. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(curr));
  41836. DUK_ASSERT(DUK_HEAPHDR_HAS_FINALIZABLE(curr)); /* All objects on finalize_list will have this flag (except object being finalized right now). */
  41837. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr)); /* Queueing code ensures. */
  41838. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY(curr)); /* ROM objects never get freed (or finalized). */
  41839. #if defined(DUK_USE_ASSERTIONS)
  41840. DUK_ASSERT(heap->currently_finalizing == NULL);
  41841. heap->currently_finalizing = curr;
  41842. #endif
  41843. /* Clear FINALIZABLE for object being finalized, so that
  41844. * duk_push_heapptr() can properly ignore the object.
  41845. */
  41846. DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
  41847. if (DUK_LIKELY(!heap->pf_skip_finalizers)) {
  41848. /* Run the finalizer, duk_heap_run_finalizer() sets
  41849. * and checks for FINALIZED to prevent the finalizer
  41850. * from executing multiple times per finalization cycle.
  41851. * (This safeguard shouldn't be actually needed anymore).
  41852. */
  41853. #if defined(DUK_USE_REFERENCE_COUNTING)
  41854. duk_bool_t had_zero_refcount;
  41855. #endif
  41856. /* The object's refcount is >0 throughout so it won't be
  41857. * refzero processed prematurely.
  41858. */
  41859. #if defined(DUK_USE_REFERENCE_COUNTING)
  41860. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) >= 1);
  41861. had_zero_refcount = (DUK_HEAPHDR_GET_REFCOUNT(curr) == 1); /* Preincremented on finalize_list insert. */
  41862. #endif
  41863. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  41864. duk_heap_run_finalizer(heap, (duk_hobject *) curr); /* must never longjmp */
  41865. DUK_ASSERT(DUK_HEAPHDR_HAS_FINALIZED(curr));
  41866. /* XXX: assert that object is still in finalize_list
  41867. * when duk_push_heapptr() allows automatic rescue.
  41868. */
  41869. #if defined(DUK_USE_REFERENCE_COUNTING)
  41870. DUK_DD(DUK_DDPRINT("refcount after finalizer (includes bump): %ld", (long) DUK_HEAPHDR_GET_REFCOUNT(curr)));
  41871. if (DUK_HEAPHDR_GET_REFCOUNT(curr) == 1) { /* Only artificial bump in refcount? */
  41872. #if defined(DUK_USE_DEBUG)
  41873. if (had_zero_refcount) {
  41874. DUK_DD(DUK_DDPRINT("finalized object's refcount is zero -> free immediately (refcount queued)"));
  41875. } else {
  41876. DUK_DD(DUK_DDPRINT("finalized object's refcount is zero -> free immediately (mark-and-sweep queued)"));
  41877. }
  41878. #endif
  41879. queue_back = 0;
  41880. } else
  41881. #endif
  41882. {
  41883. #if defined(DUK_USE_REFERENCE_COUNTING)
  41884. queue_back = 1;
  41885. if (had_zero_refcount) {
  41886. /* When finalization is triggered
  41887. * by refzero and we queue the object
  41888. * back, clear FINALIZED right away
  41889. * so that the object can be refinalized
  41890. * immediately if necessary.
  41891. */
  41892. DUK_HEAPHDR_CLEAR_FINALIZED(curr);
  41893. }
  41894. #endif
  41895. }
  41896. } else {
  41897. /* Used during heap destruction: don't actually run finalizers
  41898. * because we're heading into forced finalization. Instead,
  41899. * queue finalizable objects back to the heap_allocated list.
  41900. */
  41901. DUK_D(DUK_DPRINT("skip finalizers flag set, queue object to heap_allocated without finalizing"));
  41902. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  41903. #if defined(DUK_USE_REFERENCE_COUNTING)
  41904. queue_back = 1;
  41905. #endif
  41906. }
  41907. /* Dequeue object from finalize_list. Note that 'curr' may no
  41908. * longer be finalize_list head because new objects may have
  41909. * been queued to the list. As a result we can't optimize for
  41910. * the single-linked heap case and must scan the list for
  41911. * removal, typically the scan is very short however.
  41912. */
  41913. DUK_HEAP_REMOVE_FROM_FINALIZE_LIST(heap, curr);
  41914. /* Queue back to heap_allocated or free immediately. */
  41915. #if defined(DUK_USE_REFERENCE_COUNTING)
  41916. if (queue_back) {
  41917. /* FINALIZED is only cleared if object originally
  41918. * queued for finalization by refcounting. For
  41919. * mark-and-sweep FINALIZED is left set, so that
  41920. * next mark-and-sweep round can make a rescue/free
  41921. * decision.
  41922. */
  41923. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) >= 1);
  41924. DUK_HEAPHDR_PREDEC_REFCOUNT(curr); /* Remove artificial refcount bump. */
  41925. DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
  41926. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, curr);
  41927. } else {
  41928. /* No need to remove the refcount bump here. */
  41929. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT); /* currently, always the case */
  41930. DUK_DD(DUK_DDPRINT("refcount finalize after finalizer call: %!O", curr));
  41931. duk_hobject_refcount_finalize_norz(heap, (duk_hobject *) curr);
  41932. duk_free_hobject(heap, (duk_hobject *) curr);
  41933. DUK_DD(DUK_DDPRINT("freed hobject after finalization: %p", (void *) curr));
  41934. }
  41935. #else /* DUK_USE_REFERENCE_COUNTING */
  41936. DUK_HEAPHDR_CLEAR_FINALIZABLE(curr);
  41937. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, curr);
  41938. #endif /* DUK_USE_REFERENCE_COUNTING */
  41939. #if defined(DUK_USE_DEBUG)
  41940. count++;
  41941. #endif
  41942. #if defined(DUK_USE_ASSERTIONS)
  41943. DUK_ASSERT(heap->currently_finalizing != NULL);
  41944. heap->currently_finalizing = NULL;
  41945. #endif
  41946. }
  41947. /* finalize_list will always be processed completely. */
  41948. DUK_ASSERT(heap->finalize_list == NULL);
  41949. #if 0
  41950. /* While NORZ macros are used above, this is unnecessary because the
  41951. * only pending side effects are now finalizers, and finalize_list is
  41952. * empty.
  41953. */
  41954. DUK_REFZERO_CHECK_SLOW(heap->heap_thread);
  41955. #endif
  41956. /* Prevent count may be bumped while finalizers run, but should always
  41957. * be reliably unbumped by the time we get here.
  41958. */
  41959. DUK_ASSERT(heap->pf_prevent_count == 1);
  41960. heap->pf_prevent_count = 0;
  41961. #if defined(DUK_USE_DEBUG)
  41962. DUK_DD(DUK_DDPRINT("duk_heap_process_finalize_list: %ld finalizers called", (long) count));
  41963. #endif
  41964. }
  41965. /*
  41966. * Run an duk_hobject finalizer. Must never throw an uncaught error
  41967. * (but may throw caught errors).
  41968. *
  41969. * There is no return value. Any return value or error thrown by
  41970. * the finalizer is ignored (although errors are debug logged).
  41971. *
  41972. * Notes:
  41973. *
  41974. * - The finalizer thread 'top' assertions are there because it is
  41975. * critical that strict stack policy is observed (i.e. no cruft
  41976. * left on the finalizer stack).
  41977. */
  41978. DUK_LOCAL duk_ret_t duk__finalize_helper(duk_hthread *thr, void *udata) {
  41979. DUK_ASSERT(thr != NULL);
  41980. DUK_UNREF(udata);
  41981. DUK_DDD(DUK_DDDPRINT("protected finalization helper running"));
  41982. /* [... obj] */
  41983. /* _Finalizer property is read without checking if the value is
  41984. * callable or even exists. This is intentional, and handled
  41985. * by throwing an error which is caught by the safe call wrapper.
  41986. *
  41987. * XXX: Finalizer lookup should traverse the prototype chain (to allow
  41988. * inherited finalizers) but should not invoke accessors or proxy object
  41989. * behavior. At the moment this lookup will invoke proxy behavior, so
  41990. * caller must ensure that this function is not called if the target is
  41991. * a Proxy.
  41992. */
  41993. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_FINALIZER); /* -> [... obj finalizer] */
  41994. duk_dup_m2(thr);
  41995. duk_push_boolean(thr, DUK_HEAP_HAS_FINALIZER_NORESCUE(thr->heap));
  41996. DUK_DDD(DUK_DDDPRINT("calling finalizer"));
  41997. duk_call(thr, 2); /* [ ... obj finalizer obj heapDestruct ] -> [ ... obj retval ] */
  41998. DUK_DDD(DUK_DDDPRINT("finalizer returned successfully"));
  41999. return 0;
  42000. /* Note: we rely on duk_safe_call() to fix up the stack for the caller,
  42001. * so we don't need to pop stuff here. There is no return value;
  42002. * caller determines rescued status based on object refcount.
  42003. */
  42004. }
  42005. DUK_INTERNAL void duk_heap_run_finalizer(duk_heap *heap, duk_hobject *obj) {
  42006. duk_hthread *thr;
  42007. duk_ret_t rc;
  42008. #if defined(DUK_USE_ASSERTIONS)
  42009. duk_idx_t entry_top;
  42010. #endif
  42011. DUK_DD(DUK_DDPRINT("running duk_hobject finalizer for object: %p", (void *) obj));
  42012. DUK_ASSERT(heap != NULL);
  42013. DUK_ASSERT(heap->heap_thread != NULL);
  42014. thr = heap->heap_thread;
  42015. DUK_ASSERT(obj != NULL);
  42016. DUK_ASSERT_VALSTACK_SPACE(heap->heap_thread, 1);
  42017. #if defined(DUK_USE_ASSERTIONS)
  42018. entry_top = duk_get_top(thr);
  42019. #endif
  42020. /*
  42021. * Get and call the finalizer. All of this must be wrapped
  42022. * in a protected call, because even getting the finalizer
  42023. * may trigger an error (getter may throw one, for instance).
  42024. */
  42025. /* ROM objects could inherit a finalizer, but they are never deemed
  42026. * unreachable by mark-and-sweep, and their refcount never falls to 0.
  42027. */
  42028. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj));
  42029. /* Duktape 2.1: finalize_list never contains objects with FINALIZED
  42030. * set, so no need to check here.
  42031. */
  42032. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) obj));
  42033. #if 0
  42034. if (DUK_HEAPHDR_HAS_FINALIZED((duk_heaphdr *) obj)) {
  42035. DUK_D(DUK_DPRINT("object already finalized, avoid running finalizer twice: %!O", obj));
  42036. return;
  42037. }
  42038. #endif
  42039. DUK_HEAPHDR_SET_FINALIZED((duk_heaphdr *) obj); /* ensure never re-entered until rescue cycle complete */
  42040. #if defined(DUK_USE_ES6_PROXY)
  42041. if (DUK_HOBJECT_IS_PROXY(obj)) {
  42042. /* This may happen if duk_set_finalizer() or Duktape.fin() is
  42043. * called for a Proxy object. In such cases the fast finalizer
  42044. * flag will be set on the Proxy, not the target, and neither
  42045. * will be finalized.
  42046. */
  42047. DUK_D(DUK_DPRINT("object is a Proxy, skip finalizer call"));
  42048. return;
  42049. }
  42050. #endif /* DUK_USE_ES6_PROXY */
  42051. duk_push_hobject(thr, obj); /* this also increases refcount by one */
  42052. rc = duk_safe_call(thr, duk__finalize_helper, NULL /*udata*/, 0 /*nargs*/, 1 /*nrets*/); /* -> [... obj retval/error] */
  42053. DUK_ASSERT_TOP(thr, entry_top + 2); /* duk_safe_call discipline */
  42054. if (rc != DUK_EXEC_SUCCESS) {
  42055. /* Note: we ask for one return value from duk_safe_call to get this
  42056. * error debugging here.
  42057. */
  42058. DUK_D(DUK_DPRINT("wrapped finalizer call failed for object %p (ignored); error: %!T",
  42059. (void *) obj, (duk_tval *) duk_get_tval(thr, -1)));
  42060. }
  42061. duk_pop_2(thr); /* -> [...] */
  42062. DUK_ASSERT_TOP(thr, entry_top);
  42063. }
  42064. #else /* DUK_USE_FINALIZER_SUPPORT */
  42065. /* nothing */
  42066. #endif /* DUK_USE_FINALIZER_SUPPORT */
  42067. #line 1 "duk_heap_hashstring.c"
  42068. /*
  42069. * String hash computation (interning).
  42070. *
  42071. * String hashing is performance critical because a string hash is computed
  42072. * for all new strings which are candidates to be added to the string table.
  42073. * However, strings actually added to the string table go through a codepoint
  42074. * length calculation which dominates performance because it goes through
  42075. * every byte of the input string (but only for strings added).
  42076. *
  42077. * The string hash algorithm should be fast, but on the other hand provide
  42078. * good enough hashes to ensure both string table and object property table
  42079. * hash tables work reasonably well (i.e., there aren't too many collisions
  42080. * with real world inputs). Unless the hash is cryptographic, it's always
  42081. * possible to craft inputs with maximal hash collisions.
  42082. *
  42083. * NOTE: The hash algorithms must match tools/dukutil.py:duk_heap_hashstring()
  42084. * for ROM string support!
  42085. */
  42086. /* #include duk_internal.h -> already included */
  42087. #if defined(DUK_USE_STRHASH_DENSE)
  42088. /* Constants for duk_hashstring(). */
  42089. #define DUK__STRHASH_SHORTSTRING 4096L
  42090. #define DUK__STRHASH_MEDIUMSTRING (256L * 1024L)
  42091. #define DUK__STRHASH_BLOCKSIZE 256L
  42092. DUK_INTERNAL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len) {
  42093. duk_uint32_t hash;
  42094. /* Use Murmurhash2 directly for short strings, and use "block skipping"
  42095. * for long strings: hash an initial part and then sample the rest of
  42096. * the string with reasonably sized chunks. An initial offset for the
  42097. * sampling is computed based on a hash of the initial part of the string;
  42098. * this is done to (usually) avoid the case where all long strings have
  42099. * certain offset ranges which are never sampled.
  42100. *
  42101. * Skip should depend on length and bound the total time to roughly
  42102. * logarithmic. With current values:
  42103. *
  42104. * 1M string => 256 * 241 = 61696 bytes (0.06M) of hashing
  42105. * 1G string => 256 * 16321 = 4178176 bytes (3.98M) of hashing
  42106. *
  42107. * XXX: It would be better to compute the skip offset more "smoothly"
  42108. * instead of having a few boundary values.
  42109. */
  42110. /* note: mixing len into seed improves hashing when skipping */
  42111. duk_uint32_t str_seed = heap->hash_seed ^ ((duk_uint32_t) len);
  42112. if (len <= DUK__STRHASH_SHORTSTRING) {
  42113. hash = duk_util_hashbytes(str, len, str_seed);
  42114. } else {
  42115. duk_size_t off;
  42116. duk_size_t skip;
  42117. if (len <= DUK__STRHASH_MEDIUMSTRING) {
  42118. skip = (duk_size_t) (16 * DUK__STRHASH_BLOCKSIZE + DUK__STRHASH_BLOCKSIZE);
  42119. } else {
  42120. skip = (duk_size_t) (256 * DUK__STRHASH_BLOCKSIZE + DUK__STRHASH_BLOCKSIZE);
  42121. }
  42122. hash = duk_util_hashbytes(str, (duk_size_t) DUK__STRHASH_SHORTSTRING, str_seed);
  42123. off = DUK__STRHASH_SHORTSTRING + (skip * (hash % 256)) / 256;
  42124. /* XXX: inefficient loop */
  42125. while (off < len) {
  42126. duk_size_t left = len - off;
  42127. duk_size_t now = (duk_size_t) (left > DUK__STRHASH_BLOCKSIZE ? DUK__STRHASH_BLOCKSIZE : left);
  42128. hash ^= duk_util_hashbytes(str + off, now, str_seed);
  42129. off += skip;
  42130. }
  42131. }
  42132. #if defined(DUK_USE_STRHASH16)
  42133. /* Truncate to 16 bits here, so that a computed hash can be compared
  42134. * against a hash stored in a 16-bit field.
  42135. */
  42136. hash &= 0x0000ffffUL;
  42137. #endif
  42138. return hash;
  42139. }
  42140. #else /* DUK_USE_STRHASH_DENSE */
  42141. DUK_INTERNAL duk_uint32_t duk_heap_hashstring(duk_heap *heap, const duk_uint8_t *str, duk_size_t len) {
  42142. duk_uint32_t hash;
  42143. duk_size_t step;
  42144. duk_size_t off;
  42145. /* Slightly modified "Bernstein hash" from:
  42146. *
  42147. * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
  42148. *
  42149. * Modifications: string skipping and reverse direction similar to
  42150. * Lua 5.1.5, and different hash initializer.
  42151. *
  42152. * The reverse direction ensures last byte it always included in the
  42153. * hash which is a good default as changing parts of the string are
  42154. * more often in the suffix than in the prefix.
  42155. */
  42156. hash = heap->hash_seed ^ ((duk_uint32_t) len); /* Bernstein hash init value is normally 5381 */
  42157. step = (len >> DUK_USE_STRHASH_SKIP_SHIFT) + 1;
  42158. for (off = len; off >= step; off -= step) {
  42159. DUK_ASSERT(off >= 1); /* off >= step, and step >= 1 */
  42160. hash = (hash * 33) + str[off - 1];
  42161. }
  42162. #if defined(DUK_USE_STRHASH16)
  42163. /* Truncate to 16 bits here, so that a computed hash can be compared
  42164. * against a hash stored in a 16-bit field.
  42165. */
  42166. hash &= 0x0000ffffUL;
  42167. #endif
  42168. return hash;
  42169. }
  42170. #endif /* DUK_USE_STRHASH_DENSE */
  42171. /* automatic undefs */
  42172. #undef DUK__STRHASH_BLOCKSIZE
  42173. #undef DUK__STRHASH_MEDIUMSTRING
  42174. #undef DUK__STRHASH_SHORTSTRING
  42175. #line 1 "duk_heap_markandsweep.c"
  42176. /*
  42177. * Mark-and-sweep garbage collection.
  42178. */
  42179. /* #include duk_internal.h -> already included */
  42180. DUK_LOCAL_DECL void duk__mark_heaphdr(duk_heap *heap, duk_heaphdr *h);
  42181. DUK_LOCAL_DECL void duk__mark_heaphdr_nonnull(duk_heap *heap, duk_heaphdr *h);
  42182. DUK_LOCAL_DECL void duk__mark_tval(duk_heap *heap, duk_tval *tv);
  42183. DUK_LOCAL_DECL void duk__mark_tvals(duk_heap *heap, duk_tval *tv, duk_idx_t count);
  42184. /*
  42185. * Marking functions for heap types: mark children recursively.
  42186. */
  42187. DUK_LOCAL void duk__mark_hstring(duk_heap *heap, duk_hstring *h) {
  42188. DUK_UNREF(heap);
  42189. DUK_UNREF(h);
  42190. DUK_DDD(DUK_DDDPRINT("duk__mark_hstring: %p", (void *) h));
  42191. DUK_ASSERT(h);
  42192. /* nothing to process */
  42193. }
  42194. DUK_LOCAL void duk__mark_hobject(duk_heap *heap, duk_hobject *h) {
  42195. duk_uint_fast32_t i;
  42196. DUK_DDD(DUK_DDDPRINT("duk__mark_hobject: %p", (void *) h));
  42197. DUK_ASSERT(h);
  42198. /* XXX: use advancing pointers instead of index macros -> faster and smaller? */
  42199. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(h); i++) {
  42200. duk_hstring *key = DUK_HOBJECT_E_GET_KEY(heap, h, i);
  42201. if (key == NULL) {
  42202. continue;
  42203. }
  42204. duk__mark_heaphdr_nonnull(heap, (duk_heaphdr *) key);
  42205. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, h, i)) {
  42206. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.get);
  42207. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->a.set);
  42208. } else {
  42209. duk__mark_tval(heap, &DUK_HOBJECT_E_GET_VALUE_PTR(heap, h, i)->v);
  42210. }
  42211. }
  42212. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(h); i++) {
  42213. duk__mark_tval(heap, DUK_HOBJECT_A_GET_VALUE_PTR(heap, h, i));
  42214. }
  42215. /* Hash part is a 'weak reference' and does not contribute. */
  42216. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HOBJECT_GET_PROTOTYPE(heap, h));
  42217. /* Fast path for objects which don't have a subclass struct, or have a
  42218. * subclass struct but nothing that needs marking in the subclass struct.
  42219. */
  42220. if (DUK_HOBJECT_HAS_FASTREFS(h)) {
  42221. DUK_ASSERT(DUK_HOBJECT_ALLOWS_FASTREFS(h));
  42222. return;
  42223. }
  42224. DUK_ASSERT(DUK_HOBJECT_PROHIBITS_FASTREFS(h));
  42225. /* XXX: reorg, more common first */
  42226. if (DUK_HOBJECT_IS_COMPFUNC(h)) {
  42227. duk_hcompfunc *f = (duk_hcompfunc *) h;
  42228. duk_tval *tv, *tv_end;
  42229. duk_hobject **fn, **fn_end;
  42230. DUK_ASSERT_HCOMPFUNC_VALID(f);
  42231. /* 'data' is reachable through every compiled function which
  42232. * contains a reference.
  42233. */
  42234. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HCOMPFUNC_GET_DATA(heap, f));
  42235. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HCOMPFUNC_GET_LEXENV(heap, f));
  42236. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_HCOMPFUNC_GET_VARENV(heap, f));
  42237. if (DUK_HCOMPFUNC_GET_DATA(heap, f) != NULL) {
  42238. tv = DUK_HCOMPFUNC_GET_CONSTS_BASE(heap, f);
  42239. tv_end = DUK_HCOMPFUNC_GET_CONSTS_END(heap, f);
  42240. while (tv < tv_end) {
  42241. duk__mark_tval(heap, tv);
  42242. tv++;
  42243. }
  42244. fn = DUK_HCOMPFUNC_GET_FUNCS_BASE(heap, f);
  42245. fn_end = DUK_HCOMPFUNC_GET_FUNCS_END(heap, f);
  42246. while (fn < fn_end) {
  42247. duk__mark_heaphdr_nonnull(heap, (duk_heaphdr *) *fn);
  42248. fn++;
  42249. }
  42250. } else {
  42251. /* May happen in some out-of-memory corner cases. */
  42252. DUK_D(DUK_DPRINT("duk_hcompfunc 'data' is NULL, skipping marking"));
  42253. }
  42254. } else if (DUK_HOBJECT_IS_DECENV(h)) {
  42255. duk_hdecenv *e = (duk_hdecenv *) h;
  42256. DUK_ASSERT_HDECENV_VALID(e);
  42257. duk__mark_heaphdr(heap, (duk_heaphdr *) e->thread);
  42258. duk__mark_heaphdr(heap, (duk_heaphdr *) e->varmap);
  42259. } else if (DUK_HOBJECT_IS_OBJENV(h)) {
  42260. duk_hobjenv *e = (duk_hobjenv *) h;
  42261. DUK_ASSERT_HOBJENV_VALID(e);
  42262. duk__mark_heaphdr_nonnull(heap, (duk_heaphdr *) e->target);
  42263. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  42264. } else if (DUK_HOBJECT_IS_BUFOBJ(h)) {
  42265. duk_hbufobj *b = (duk_hbufobj *) h;
  42266. DUK_ASSERT_HBUFOBJ_VALID(b);
  42267. duk__mark_heaphdr(heap, (duk_heaphdr *) b->buf);
  42268. duk__mark_heaphdr(heap, (duk_heaphdr *) b->buf_prop);
  42269. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  42270. } else if (DUK_HOBJECT_IS_BOUNDFUNC(h)) {
  42271. duk_hboundfunc *f = (duk_hboundfunc *) (void *) h;
  42272. DUK_ASSERT_HBOUNDFUNC_VALID(f);
  42273. duk__mark_tval(heap, &f->target);
  42274. duk__mark_tval(heap, &f->this_binding);
  42275. duk__mark_tvals(heap, f->args, f->nargs);
  42276. #if defined(DUK_USE_ES6_PROXY)
  42277. } else if (DUK_HOBJECT_IS_PROXY(h)) {
  42278. duk_hproxy *p = (duk_hproxy *) h;
  42279. DUK_ASSERT_HPROXY_VALID(p);
  42280. duk__mark_heaphdr_nonnull(heap, (duk_heaphdr *) p->target);
  42281. duk__mark_heaphdr_nonnull(heap, (duk_heaphdr *) p->handler);
  42282. #endif /* DUK_USE_ES6_PROXY */
  42283. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  42284. duk_hthread *t = (duk_hthread *) h;
  42285. duk_activation *act;
  42286. duk_tval *tv;
  42287. DUK_ASSERT_HTHREAD_VALID(t);
  42288. tv = t->valstack;
  42289. while (tv < t->valstack_top) {
  42290. duk__mark_tval(heap, tv);
  42291. tv++;
  42292. }
  42293. for (act = t->callstack_curr; act != NULL; act = act->parent) {
  42294. duk__mark_heaphdr(heap, (duk_heaphdr *) DUK_ACT_GET_FUNC(act));
  42295. duk__mark_heaphdr(heap, (duk_heaphdr *) act->var_env);
  42296. duk__mark_heaphdr(heap, (duk_heaphdr *) act->lex_env);
  42297. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  42298. duk__mark_heaphdr(heap, (duk_heaphdr *) act->prev_caller);
  42299. #endif
  42300. #if 0 /* nothing now */
  42301. for (cat = act->cat; cat != NULL; cat = cat->parent) {
  42302. }
  42303. #endif
  42304. }
  42305. duk__mark_heaphdr(heap, (duk_heaphdr *) t->resumer);
  42306. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  42307. duk__mark_heaphdr(heap, (duk_heaphdr *) t->builtins[i]);
  42308. }
  42309. } else {
  42310. /* We may come here if the object should have a FASTREFS flag
  42311. * but it's missing for some reason. Assert for never getting
  42312. * here; however, other than performance, this is harmless.
  42313. */
  42314. DUK_D(DUK_DPRINT("missing FASTREFS flag for: %!iO", h));
  42315. DUK_ASSERT(0);
  42316. }
  42317. }
  42318. /* Mark any duk_heaphdr type. Recursion tracking happens only here. */
  42319. DUK_LOCAL void duk__mark_heaphdr(duk_heap *heap, duk_heaphdr *h) {
  42320. DUK_DDD(DUK_DDDPRINT("duk__mark_heaphdr %p, type %ld",
  42321. (void *) h,
  42322. (h != NULL ? (long) DUK_HEAPHDR_GET_TYPE(h) : (long) -1)));
  42323. /* XXX: add non-null variant? */
  42324. if (h == NULL) {
  42325. return;
  42326. }
  42327. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY(h) || DUK_HEAPHDR_HAS_REACHABLE(h));
  42328. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  42329. if (!DUK_HEAPHDR_HAS_READONLY(h)) {
  42330. h->h_assert_refcount++; /* Comparison refcount: bump even if already reachable. */
  42331. }
  42332. #endif
  42333. if (DUK_HEAPHDR_HAS_REACHABLE(h)) {
  42334. DUK_DDD(DUK_DDDPRINT("already marked reachable, skip"));
  42335. return;
  42336. }
  42337. #if defined(DUK_USE_ROM_OBJECTS)
  42338. /* READONLY objects always have REACHABLE set, so the check above
  42339. * will prevent READONLY objects from being marked here.
  42340. */
  42341. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY(h));
  42342. #endif
  42343. DUK_HEAPHDR_SET_REACHABLE(h);
  42344. if (heap->ms_recursion_depth >= DUK_USE_MARK_AND_SWEEP_RECLIMIT) {
  42345. DUK_D(DUK_DPRINT("mark-and-sweep recursion limit reached, marking as temproot: %p", (void *) h));
  42346. DUK_HEAP_SET_MARKANDSWEEP_RECLIMIT_REACHED(heap);
  42347. DUK_HEAPHDR_SET_TEMPROOT(h);
  42348. return;
  42349. }
  42350. heap->ms_recursion_depth++;
  42351. DUK_ASSERT(heap->ms_recursion_depth != 0); /* Wrap. */
  42352. switch (DUK_HEAPHDR_GET_TYPE(h)) {
  42353. case DUK_HTYPE_STRING:
  42354. duk__mark_hstring(heap, (duk_hstring *) h);
  42355. break;
  42356. case DUK_HTYPE_OBJECT:
  42357. duk__mark_hobject(heap, (duk_hobject *) h);
  42358. break;
  42359. case DUK_HTYPE_BUFFER:
  42360. /* nothing to mark */
  42361. break;
  42362. default:
  42363. DUK_D(DUK_DPRINT("attempt to mark heaphdr %p with invalid htype %ld", (void *) h, (long) DUK_HEAPHDR_GET_TYPE(h)));
  42364. DUK_UNREACHABLE();
  42365. }
  42366. DUK_ASSERT(heap->ms_recursion_depth > 0);
  42367. heap->ms_recursion_depth--;
  42368. }
  42369. DUK_LOCAL void duk__mark_tval(duk_heap *heap, duk_tval *tv) {
  42370. DUK_DDD(DUK_DDDPRINT("duk__mark_tval %p", (void *) tv));
  42371. if (tv == NULL) {
  42372. return;
  42373. }
  42374. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  42375. duk_heaphdr *h;
  42376. h = DUK_TVAL_GET_HEAPHDR(tv);
  42377. DUK_ASSERT(h != NULL);
  42378. duk__mark_heaphdr_nonnull(heap, h);
  42379. }
  42380. }
  42381. DUK_LOCAL void duk__mark_tvals(duk_heap *heap, duk_tval *tv, duk_idx_t count) {
  42382. DUK_ASSERT(count == 0 || tv != NULL);
  42383. while (count-- > 0) {
  42384. if (DUK_TVAL_IS_HEAP_ALLOCATED(tv)) {
  42385. duk_heaphdr *h;
  42386. h = DUK_TVAL_GET_HEAPHDR(tv);
  42387. DUK_ASSERT(h != NULL);
  42388. duk__mark_heaphdr_nonnull(heap, h);
  42389. }
  42390. tv++;
  42391. }
  42392. }
  42393. /* Mark any duk_heaphdr type, caller guarantees a non-NULL pointer. */
  42394. DUK_LOCAL void duk__mark_heaphdr_nonnull(duk_heap *heap, duk_heaphdr *h) {
  42395. /* For now, just call the generic handler. Change when call sites
  42396. * are changed too.
  42397. */
  42398. duk__mark_heaphdr(heap, h);
  42399. }
  42400. /*
  42401. * Mark the heap.
  42402. */
  42403. DUK_LOCAL void duk__mark_roots_heap(duk_heap *heap) {
  42404. duk_small_uint_t i;
  42405. DUK_DD(DUK_DDPRINT("duk__mark_roots_heap: %p", (void *) heap));
  42406. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->heap_thread);
  42407. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->heap_object);
  42408. for (i = 0; i < DUK_HEAP_NUM_STRINGS; i++) {
  42409. duk_hstring *h = DUK_HEAP_GET_STRING(heap, i);
  42410. duk__mark_heaphdr(heap, (duk_heaphdr *) h);
  42411. }
  42412. duk__mark_tval(heap, &heap->lj.value1);
  42413. duk__mark_tval(heap, &heap->lj.value2);
  42414. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  42415. for (i = 0; i < heap->dbg_breakpoint_count; i++) {
  42416. duk__mark_heaphdr(heap, (duk_heaphdr *) heap->dbg_breakpoints[i].filename);
  42417. }
  42418. #endif
  42419. }
  42420. /*
  42421. * Mark unreachable, finalizable objects.
  42422. *
  42423. * Such objects will be moved aside and their finalizers run later. They
  42424. * have to be treated as reachability roots for their properties etc to
  42425. * remain allocated. This marking is only done for unreachable values which
  42426. * would be swept later.
  42427. *
  42428. * Objects are first marked FINALIZABLE and only then marked as reachability
  42429. * roots; otherwise circular references might be handled inconsistently.
  42430. */
  42431. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42432. DUK_LOCAL void duk__mark_finalizable(duk_heap *heap) {
  42433. duk_heaphdr *hdr;
  42434. duk_size_t count_finalizable = 0;
  42435. DUK_DD(DUK_DDPRINT("duk__mark_finalizable: %p", (void *) heap));
  42436. DUK_ASSERT(heap->heap_thread != NULL);
  42437. hdr = heap->heap_allocated;
  42438. while (hdr != NULL) {
  42439. /* A finalizer is looked up from the object and up its
  42440. * prototype chain (which allows inherited finalizers).
  42441. * The finalizer is checked for using a duk_hobject flag
  42442. * which is kept in sync with the presence and callability
  42443. * of a _Finalizer hidden symbol.
  42444. */
  42445. if (!DUK_HEAPHDR_HAS_REACHABLE(hdr) &&
  42446. DUK_HEAPHDR_IS_OBJECT(hdr) &&
  42447. !DUK_HEAPHDR_HAS_FINALIZED(hdr) &&
  42448. DUK_HOBJECT_HAS_FINALIZER_FAST(heap, (duk_hobject *) hdr)) {
  42449. /* heaphdr:
  42450. * - is not reachable
  42451. * - is an object
  42452. * - is not a finalized object waiting for rescue/keep decision
  42453. * - has a finalizer
  42454. */
  42455. DUK_DD(DUK_DDPRINT("unreachable heap object will be "
  42456. "finalized -> mark as finalizable "
  42457. "and treat as a reachability root: %p",
  42458. (void *) hdr));
  42459. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY(hdr));
  42460. DUK_HEAPHDR_SET_FINALIZABLE(hdr);
  42461. count_finalizable++;
  42462. }
  42463. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42464. }
  42465. if (count_finalizable == 0) {
  42466. return;
  42467. }
  42468. DUK_DD(DUK_DDPRINT("marked %ld heap objects as finalizable, now mark them reachable",
  42469. (long) count_finalizable));
  42470. hdr = heap->heap_allocated;
  42471. while (hdr != NULL) {
  42472. if (DUK_HEAPHDR_HAS_FINALIZABLE(hdr)) {
  42473. duk__mark_heaphdr_nonnull(heap, hdr);
  42474. }
  42475. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42476. }
  42477. /* Caller will finish the marking process if we hit a recursion limit. */
  42478. }
  42479. #endif /* DUK_USE_FINALIZER_SUPPORT */
  42480. /*
  42481. * Mark objects on finalize_list.
  42482. */
  42483. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42484. DUK_LOCAL void duk__mark_finalize_list(duk_heap *heap) {
  42485. duk_heaphdr *hdr;
  42486. #if defined(DUK_USE_DEBUG)
  42487. duk_size_t count_finalize_list = 0;
  42488. #endif
  42489. DUK_DD(DUK_DDPRINT("duk__mark_finalize_list: %p", (void *) heap));
  42490. hdr = heap->finalize_list;
  42491. while (hdr != NULL) {
  42492. duk__mark_heaphdr_nonnull(heap, hdr);
  42493. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42494. #if defined(DUK_USE_DEBUG)
  42495. count_finalize_list++;
  42496. #endif
  42497. }
  42498. #if defined(DUK_USE_DEBUG)
  42499. if (count_finalize_list > 0) {
  42500. DUK_D(DUK_DPRINT("marked %ld objects on the finalize_list as reachable (previous finalizer run skipped)",
  42501. (long) count_finalize_list));
  42502. }
  42503. #endif
  42504. }
  42505. #endif /* DUK_USE_FINALIZER_SUPPORT */
  42506. /*
  42507. * Fallback marking handler if recursion limit is reached.
  42508. *
  42509. * Iterates 'temproots' until recursion limit is no longer hit. Temproots
  42510. * can be in heap_allocated or finalize_list; refzero_list is now always
  42511. * empty for mark-and-sweep. A temproot may occur in finalize_list now if
  42512. * there are objects on the finalize_list and user code creates a reference
  42513. * from an object in heap_allocated to the object in finalize_list (which is
  42514. * now allowed), and it happened to coincide with the recursion depth limit.
  42515. *
  42516. * This is a slow scan, but guarantees that we finish with a bounded C stack.
  42517. *
  42518. * Note that nodes may have been marked as temproots before this scan begun,
  42519. * OR they may have been marked during the scan (as we process nodes
  42520. * recursively also during the scan). This is intended behavior.
  42521. */
  42522. #if defined(DUK_USE_DEBUG)
  42523. DUK_LOCAL void duk__handle_temproot(duk_heap *heap, duk_heaphdr *hdr, duk_size_t *count) {
  42524. #else
  42525. DUK_LOCAL void duk__handle_temproot(duk_heap *heap, duk_heaphdr *hdr) {
  42526. #endif
  42527. DUK_ASSERT(hdr != NULL);
  42528. if (!DUK_HEAPHDR_HAS_TEMPROOT(hdr)) {
  42529. DUK_DDD(DUK_DDDPRINT("not a temp root: %p", (void *) hdr));
  42530. return;
  42531. }
  42532. DUK_DDD(DUK_DDDPRINT("found a temp root: %p", (void *) hdr));
  42533. DUK_HEAPHDR_CLEAR_TEMPROOT(hdr);
  42534. DUK_HEAPHDR_CLEAR_REACHABLE(hdr); /* Done so that duk__mark_heaphdr() works correctly. */
  42535. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  42536. hdr->h_assert_refcount--; /* Same node visited twice. */
  42537. #endif
  42538. duk__mark_heaphdr_nonnull(heap, hdr);
  42539. #if defined(DUK_USE_DEBUG)
  42540. (*count)++;
  42541. #endif
  42542. }
  42543. DUK_LOCAL void duk__mark_temproots_by_heap_scan(duk_heap *heap) {
  42544. duk_heaphdr *hdr;
  42545. #if defined(DUK_USE_DEBUG)
  42546. duk_size_t count;
  42547. #endif
  42548. DUK_DD(DUK_DDPRINT("duk__mark_temproots_by_heap_scan: %p", (void *) heap));
  42549. while (DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap)) {
  42550. DUK_DD(DUK_DDPRINT("recursion limit reached, doing heap scan to continue from temproots"));
  42551. #if defined(DUK_USE_DEBUG)
  42552. count = 0;
  42553. #endif
  42554. DUK_HEAP_CLEAR_MARKANDSWEEP_RECLIMIT_REACHED(heap);
  42555. hdr = heap->heap_allocated;
  42556. while (hdr) {
  42557. #if defined(DUK_USE_DEBUG)
  42558. duk__handle_temproot(heap, hdr, &count);
  42559. #else
  42560. duk__handle_temproot(heap, hdr);
  42561. #endif
  42562. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42563. }
  42564. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42565. hdr = heap->finalize_list;
  42566. while (hdr) {
  42567. #if defined(DUK_USE_DEBUG)
  42568. duk__handle_temproot(heap, hdr, &count);
  42569. #else
  42570. duk__handle_temproot(heap, hdr);
  42571. #endif
  42572. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42573. }
  42574. #endif
  42575. #if defined(DUK_USE_DEBUG)
  42576. DUK_DD(DUK_DDPRINT("temproot mark heap scan processed %ld temp roots", (long) count));
  42577. #endif
  42578. }
  42579. }
  42580. /*
  42581. * Finalize refcounts for heap elements just about to be freed.
  42582. * This must be done for all objects before freeing to avoid any
  42583. * stale pointer dereferences.
  42584. *
  42585. * Note that this must deduce the set of objects to be freed
  42586. * identically to duk__sweep_heap().
  42587. */
  42588. #if defined(DUK_USE_REFERENCE_COUNTING)
  42589. DUK_LOCAL void duk__finalize_refcounts(duk_heap *heap) {
  42590. duk_heaphdr *hdr;
  42591. DUK_ASSERT(heap->heap_thread != NULL);
  42592. DUK_DD(DUK_DDPRINT("duk__finalize_refcounts: heap=%p", (void *) heap));
  42593. hdr = heap->heap_allocated;
  42594. while (hdr) {
  42595. if (!DUK_HEAPHDR_HAS_REACHABLE(hdr)) {
  42596. /*
  42597. * Unreachable object about to be swept. Finalize target refcounts
  42598. * (objects which the unreachable object points to) without doing
  42599. * refzero processing. Recursive decrefs are also prevented when
  42600. * refzero processing is disabled.
  42601. *
  42602. * Value cannot be a finalizable object, as they have been made
  42603. * temporarily reachable for this round.
  42604. */
  42605. DUK_DDD(DUK_DDDPRINT("unreachable object, refcount finalize before sweeping: %p", (void *) hdr));
  42606. /* Finalize using heap->heap_thread; DECREF has a
  42607. * suppress check for mark-and-sweep which is based
  42608. * on heap->ms_running.
  42609. */
  42610. duk_heaphdr_refcount_finalize_norz(heap, hdr);
  42611. }
  42612. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42613. }
  42614. }
  42615. #endif /* DUK_USE_REFERENCE_COUNTING */
  42616. /*
  42617. * Clear (reachable) flags of finalize_list.
  42618. *
  42619. * We could mostly do in the sweep phase when we move objects from the
  42620. * heap into the finalize_list. However, if a finalizer run is skipped
  42621. * during a mark-and-sweep, the objects on the finalize_list will be marked
  42622. * reachable during the next mark-and-sweep. Since they're already on the
  42623. * finalize_list, no-one will be clearing their REACHABLE flag so we do it
  42624. * here. (This now overlaps with the sweep handling in a harmless way.)
  42625. */
  42626. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42627. DUK_LOCAL void duk__clear_finalize_list_flags(duk_heap *heap) {
  42628. duk_heaphdr *hdr;
  42629. DUK_DD(DUK_DDPRINT("duk__clear_finalize_list_flags: %p", (void *) heap));
  42630. hdr = heap->finalize_list;
  42631. while (hdr) {
  42632. DUK_HEAPHDR_CLEAR_REACHABLE(hdr);
  42633. #if defined(DUK_USE_ASSERTIONS)
  42634. DUK_ASSERT(DUK_HEAPHDR_HAS_FINALIZABLE(hdr) || \
  42635. (heap->currently_finalizing == hdr));
  42636. #endif
  42637. /* DUK_HEAPHDR_FLAG_FINALIZED may be set. */
  42638. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  42639. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42640. }
  42641. }
  42642. #endif /* DUK_USE_FINALIZER_SUPPORT */
  42643. /*
  42644. * Sweep stringtable.
  42645. */
  42646. DUK_LOCAL void duk__sweep_stringtable(duk_heap *heap, duk_size_t *out_count_keep) {
  42647. duk_hstring *h;
  42648. duk_hstring *prev;
  42649. duk_uint32_t i;
  42650. #if defined(DUK_USE_DEBUG)
  42651. duk_size_t count_free = 0;
  42652. #endif
  42653. duk_size_t count_keep = 0;
  42654. DUK_DD(DUK_DDPRINT("duk__sweep_stringtable: %p", (void *) heap));
  42655. #if defined(DUK_USE_STRTAB_PTRCOMP)
  42656. if (heap->strtable16 == NULL) {
  42657. #else
  42658. if (heap->strtable == NULL) {
  42659. #endif
  42660. goto done;
  42661. }
  42662. for (i = 0; i < heap->st_size; i++) {
  42663. #if defined(DUK_USE_STRTAB_PTRCOMP)
  42664. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, heap->strtable16[i]);
  42665. #else
  42666. h = heap->strtable[i];
  42667. #endif
  42668. prev = NULL;
  42669. while (h != NULL) {
  42670. duk_hstring *next;
  42671. next = h->hdr.h_next;
  42672. if (DUK_HEAPHDR_HAS_REACHABLE((duk_heaphdr *) h))
  42673. {
  42674. DUK_HEAPHDR_CLEAR_REACHABLE((duk_heaphdr *) h);
  42675. count_keep++;
  42676. prev = h;
  42677. } else {
  42678. #if defined(DUK_USE_DEBUG)
  42679. count_free++;
  42680. #endif
  42681. /* For pinned strings the refcount has been
  42682. * bumped. We could unbump it here before
  42683. * freeing, but that's actually not necessary
  42684. * except for assertions.
  42685. */
  42686. #if 0
  42687. if (DUK_HSTRING_HAS_PINNED_LITERAL(h)) {
  42688. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) > 0U);
  42689. DUK_HSTRING_DECREF_NORZ(heap->heap_thread, h);
  42690. DUK_HSTRING_CLEAR_PINNED_LITERAL(h);
  42691. }
  42692. #endif
  42693. #if defined(DUK_USE_REFERENCE_COUNTING)
  42694. /* Non-zero refcounts should not happen for unreachable strings,
  42695. * because we refcount finalize all unreachable objects which
  42696. * should have decreased unreachable string refcounts to zero
  42697. * (even for cycles). However, pinned strings have a +1 bump.
  42698. */
  42699. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) ==
  42700. DUK_HSTRING_HAS_PINNED_LITERAL(h) ? 1U : 0U);
  42701. #endif
  42702. /* Deal with weak references first. */
  42703. duk_heap_strcache_string_remove(heap, (duk_hstring *) h);
  42704. /* Remove the string from the string table. */
  42705. duk_heap_strtable_unlink_prev(heap, (duk_hstring *) h, (duk_hstring *) prev);
  42706. /* Free inner references (these exist e.g. when external
  42707. * strings are enabled) and the struct itself.
  42708. */
  42709. duk_free_hstring(heap, (duk_hstring *) h);
  42710. /* Don't update 'prev'; it should be last string kept. */
  42711. }
  42712. h = next;
  42713. }
  42714. }
  42715. done:
  42716. #if defined(DUK_USE_DEBUG)
  42717. DUK_D(DUK_DPRINT("mark-and-sweep sweep stringtable: %ld freed, %ld kept",
  42718. (long) count_free, (long) count_keep));
  42719. #endif
  42720. *out_count_keep = count_keep;
  42721. }
  42722. /*
  42723. * Sweep heap.
  42724. */
  42725. DUK_LOCAL void duk__sweep_heap(duk_heap *heap, duk_small_uint_t flags, duk_size_t *out_count_keep) {
  42726. duk_heaphdr *prev; /* last element that was left in the heap */
  42727. duk_heaphdr *curr;
  42728. duk_heaphdr *next;
  42729. #if defined(DUK_USE_DEBUG)
  42730. duk_size_t count_free = 0;
  42731. duk_size_t count_finalize = 0;
  42732. duk_size_t count_rescue = 0;
  42733. #endif
  42734. duk_size_t count_keep = 0;
  42735. DUK_DD(DUK_DDPRINT("duk__sweep_heap: %p", (void *) heap));
  42736. prev = NULL;
  42737. curr = heap->heap_allocated;
  42738. heap->heap_allocated = NULL;
  42739. while (curr) {
  42740. /* Strings and ROM objects are never placed on the heap allocated list. */
  42741. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) != DUK_HTYPE_STRING);
  42742. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY(curr));
  42743. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  42744. if (DUK_HEAPHDR_HAS_REACHABLE(curr)) {
  42745. /*
  42746. * Reachable object:
  42747. * - If FINALIZABLE -> actually unreachable (but marked
  42748. * artificially reachable), queue to finalize_list.
  42749. * - If !FINALIZABLE but FINALIZED -> rescued after
  42750. * finalizer execution.
  42751. * - Otherwise just a normal, reachable object.
  42752. *
  42753. * Objects which are kept are queued to heap_allocated
  42754. * tail (we're essentially filtering heap_allocated in
  42755. * practice).
  42756. */
  42757. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42758. if (DUK_UNLIKELY(DUK_HEAPHDR_HAS_FINALIZABLE(curr))) {
  42759. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(curr));
  42760. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT);
  42761. DUK_DD(DUK_DDPRINT("sweep; reachable, finalizable --> move to finalize_list: %p", (void *) curr));
  42762. #if defined(DUK_USE_REFERENCE_COUNTING)
  42763. DUK_HEAPHDR_PREINC_REFCOUNT(curr); /* Bump refcount so that refzero never occurs when pending a finalizer call. */
  42764. #endif
  42765. DUK_HEAP_INSERT_INTO_FINALIZE_LIST(heap, curr);
  42766. #if defined(DUK_USE_DEBUG)
  42767. count_finalize++;
  42768. #endif
  42769. }
  42770. else
  42771. #endif /* DUK_USE_FINALIZER_SUPPORT */
  42772. {
  42773. if (DUK_UNLIKELY(DUK_HEAPHDR_HAS_FINALIZED(curr))) {
  42774. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  42775. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT);
  42776. if (flags & DUK_MS_FLAG_POSTPONE_RESCUE) {
  42777. DUK_DD(DUK_DDPRINT("sweep; reachable, finalized, but postponing rescue decisions --> keep object (with FINALIZED set): %!iO", curr));
  42778. count_keep++;
  42779. } else {
  42780. DUK_DD(DUK_DDPRINT("sweep; reachable, finalized --> rescued after finalization: %p", (void *) curr));
  42781. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42782. DUK_HEAPHDR_CLEAR_FINALIZED(curr);
  42783. #endif
  42784. #if defined(DUK_USE_DEBUG)
  42785. count_rescue++;
  42786. #endif
  42787. }
  42788. } else {
  42789. DUK_DD(DUK_DDPRINT("sweep; reachable --> keep: %!iO", curr));
  42790. count_keep++;
  42791. }
  42792. if (prev != NULL) {
  42793. DUK_ASSERT(heap->heap_allocated != NULL);
  42794. DUK_HEAPHDR_SET_NEXT(heap, prev, curr);
  42795. } else {
  42796. DUK_ASSERT(heap->heap_allocated == NULL);
  42797. heap->heap_allocated = curr;
  42798. }
  42799. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  42800. DUK_HEAPHDR_SET_PREV(heap, curr, prev);
  42801. #endif
  42802. DUK_ASSERT_HEAPHDR_LINKS(heap, prev);
  42803. DUK_ASSERT_HEAPHDR_LINKS(heap, curr);
  42804. prev = curr;
  42805. }
  42806. /*
  42807. * Shrink check for value stacks here. We're inside
  42808. * ms_prevent_count protection which prevents recursive
  42809. * mark-and-sweep and refzero finalizers, so there are
  42810. * no side effects that would affect the heap lists.
  42811. */
  42812. if (DUK_HEAPHDR_IS_OBJECT(curr) && DUK_HOBJECT_IS_THREAD((duk_hobject *) curr)) {
  42813. duk_hthread *thr_curr = (duk_hthread *) curr;
  42814. DUK_DD(DUK_DDPRINT("value stack shrink check for thread: %!O", curr));
  42815. duk_valstack_shrink_check_nothrow(thr_curr, flags & DUK_MS_FLAG_EMERGENCY /*snug*/);
  42816. }
  42817. DUK_HEAPHDR_CLEAR_REACHABLE(curr);
  42818. /* Keep FINALIZED if set, used if rescue decisions are postponed. */
  42819. /* Keep FINALIZABLE for objects on finalize_list. */
  42820. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(curr));
  42821. } else {
  42822. /*
  42823. * Unreachable object:
  42824. * - If FINALIZED, object was finalized but not
  42825. * rescued. This doesn't affect freeing.
  42826. * - Otherwise normal unreachable object.
  42827. *
  42828. * There's no guard preventing a FINALIZED object
  42829. * from being freed while finalizers execute: the
  42830. * artificial finalize_list reachability roots can't
  42831. * cause an incorrect free decision (but can cause
  42832. * an incorrect rescue decision).
  42833. */
  42834. #if defined(DUK_USE_REFERENCE_COUNTING)
  42835. /* Non-zero refcounts should not happen because we refcount
  42836. * finalize all unreachable objects which should cancel out
  42837. * refcounts (even for cycles).
  42838. */
  42839. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(curr) == 0);
  42840. #endif
  42841. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(curr));
  42842. #if defined(DUK_USE_DEBUG)
  42843. if (DUK_HEAPHDR_HAS_FINALIZED(curr)) {
  42844. DUK_DD(DUK_DDPRINT("sweep; unreachable, finalized --> finalized object not rescued: %p", (void *) curr));
  42845. } else {
  42846. DUK_DD(DUK_DDPRINT("sweep; not reachable --> free: %p", (void *) curr));
  42847. }
  42848. #endif
  42849. /* Note: object cannot be a finalizable unreachable object, as
  42850. * they have been marked temporarily reachable for this round,
  42851. * and are handled above.
  42852. */
  42853. #if defined(DUK_USE_DEBUG)
  42854. count_free++;
  42855. #endif
  42856. /* Weak refs should be handled here, but no weak refs for
  42857. * any non-string objects exist right now.
  42858. */
  42859. /* Free object and all auxiliary (non-heap) allocs. */
  42860. duk_heap_free_heaphdr_raw(heap, curr);
  42861. }
  42862. curr = next;
  42863. }
  42864. if (prev != NULL) {
  42865. DUK_HEAPHDR_SET_NEXT(heap, prev, NULL);
  42866. }
  42867. DUK_ASSERT_HEAPHDR_LINKS(heap, prev);
  42868. #if defined(DUK_USE_DEBUG)
  42869. DUK_D(DUK_DPRINT("mark-and-sweep sweep objects (non-string): %ld freed, %ld kept, %ld rescued, %ld queued for finalization",
  42870. (long) count_free, (long) count_keep, (long) count_rescue, (long) count_finalize));
  42871. #endif
  42872. *out_count_keep = count_keep;
  42873. }
  42874. /*
  42875. * Litcache helpers.
  42876. */
  42877. #if defined(DUK_USE_LITCACHE_SIZE)
  42878. DUK_LOCAL void duk__wipe_litcache(duk_heap *heap) {
  42879. duk_uint_t i;
  42880. duk_litcache_entry *e;
  42881. e = heap->litcache;
  42882. for (i = 0; i < DUK_USE_LITCACHE_SIZE; i++) {
  42883. e->addr = NULL;
  42884. /* e->h does not need to be invalidated: when e->addr is
  42885. * NULL, e->h is considered garbage.
  42886. */
  42887. e++;
  42888. }
  42889. }
  42890. #endif /* DUK_USE_LITCACHE_SIZE */
  42891. /*
  42892. * Object compaction.
  42893. *
  42894. * Compaction is assumed to never throw an error.
  42895. */
  42896. DUK_LOCAL int duk__protected_compact_object(duk_hthread *thr, void *udata) {
  42897. duk_hobject *obj;
  42898. /* XXX: for threads, compact stacks? */
  42899. DUK_UNREF(udata);
  42900. obj = duk_known_hobject(thr, -1);
  42901. duk_hobject_compact_props(thr, obj);
  42902. return 0;
  42903. }
  42904. #if defined(DUK_USE_DEBUG)
  42905. DUK_LOCAL void duk__compact_object_list(duk_heap *heap, duk_hthread *thr, duk_heaphdr *start, duk_size_t *p_count_check, duk_size_t *p_count_compact, duk_size_t *p_count_bytes_saved) {
  42906. #else
  42907. DUK_LOCAL void duk__compact_object_list(duk_heap *heap, duk_hthread *thr, duk_heaphdr *start) {
  42908. #endif
  42909. duk_heaphdr *curr;
  42910. #if defined(DUK_USE_DEBUG)
  42911. duk_size_t old_size, new_size;
  42912. #endif
  42913. duk_hobject *obj;
  42914. DUK_UNREF(heap);
  42915. curr = start;
  42916. while (curr) {
  42917. DUK_DDD(DUK_DDDPRINT("mark-and-sweep compact: %p", (void *) curr));
  42918. if (DUK_HEAPHDR_GET_TYPE(curr) != DUK_HTYPE_OBJECT) {
  42919. goto next;
  42920. }
  42921. obj = (duk_hobject *) curr;
  42922. #if defined(DUK_USE_DEBUG)
  42923. old_size = DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  42924. DUK_HOBJECT_GET_ASIZE(obj),
  42925. DUK_HOBJECT_GET_HSIZE(obj));
  42926. #endif
  42927. DUK_DD(DUK_DDPRINT("compact object: %p", (void *) obj));
  42928. duk_push_hobject(thr, obj);
  42929. /* XXX: disable error handlers for duration of compaction? */
  42930. duk_safe_call(thr, duk__protected_compact_object, NULL, 1, 0);
  42931. #if defined(DUK_USE_DEBUG)
  42932. new_size = DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  42933. DUK_HOBJECT_GET_ASIZE(obj),
  42934. DUK_HOBJECT_GET_HSIZE(obj));
  42935. #endif
  42936. #if defined(DUK_USE_DEBUG)
  42937. (*p_count_compact)++;
  42938. (*p_count_bytes_saved) += (duk_size_t) (old_size - new_size);
  42939. #endif
  42940. next:
  42941. curr = DUK_HEAPHDR_GET_NEXT(heap, curr);
  42942. #if defined(DUK_USE_DEBUG)
  42943. (*p_count_check)++;
  42944. #endif
  42945. }
  42946. }
  42947. DUK_LOCAL void duk__compact_objects(duk_heap *heap) {
  42948. /* XXX: which lists should participate? to be finalized? */
  42949. #if defined(DUK_USE_DEBUG)
  42950. duk_size_t count_check = 0;
  42951. duk_size_t count_compact = 0;
  42952. duk_size_t count_bytes_saved = 0;
  42953. #endif
  42954. DUK_DD(DUK_DDPRINT("duk__compact_objects: %p", (void *) heap));
  42955. DUK_ASSERT(heap->heap_thread != NULL);
  42956. #if defined(DUK_USE_DEBUG)
  42957. duk__compact_object_list(heap, heap->heap_thread, heap->heap_allocated, &count_check, &count_compact, &count_bytes_saved);
  42958. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42959. duk__compact_object_list(heap, heap->heap_thread, heap->finalize_list, &count_check, &count_compact, &count_bytes_saved);
  42960. #endif
  42961. #else
  42962. duk__compact_object_list(heap, heap->heap_thread, heap->heap_allocated);
  42963. #if defined(DUK_USE_FINALIZER_SUPPORT)
  42964. duk__compact_object_list(heap, heap->heap_thread, heap->finalize_list);
  42965. #endif
  42966. #endif
  42967. #if defined(DUK_USE_REFERENCE_COUNTING)
  42968. DUK_ASSERT(heap->refzero_list == NULL); /* Always handled to completion inline in DECREF. */
  42969. #endif
  42970. #if defined(DUK_USE_DEBUG)
  42971. DUK_D(DUK_DPRINT("mark-and-sweep compact objects: %ld checked, %ld compaction attempts, %ld bytes saved by compaction",
  42972. (long) count_check, (long) count_compact, (long) count_bytes_saved));
  42973. #endif
  42974. }
  42975. /*
  42976. * Assertion helpers.
  42977. */
  42978. #if defined(DUK_USE_ASSERTIONS)
  42979. DUK_LOCAL void duk__assert_heaphdr_flags(duk_heap *heap) {
  42980. duk_heaphdr *hdr;
  42981. hdr = heap->heap_allocated;
  42982. while (hdr) {
  42983. DUK_ASSERT(!DUK_HEAPHDR_HAS_REACHABLE(hdr));
  42984. DUK_ASSERT(!DUK_HEAPHDR_HAS_TEMPROOT(hdr));
  42985. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZABLE(hdr));
  42986. /* may have FINALIZED */
  42987. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  42988. }
  42989. #if defined(DUK_USE_REFERENCE_COUNTING)
  42990. DUK_ASSERT(heap->refzero_list == NULL); /* Always handled to completion inline in DECREF. */
  42991. #endif
  42992. }
  42993. #if defined(DUK_USE_REFERENCE_COUNTING)
  42994. DUK_LOCAL void duk__assert_valid_refcounts(duk_heap *heap) {
  42995. duk_heaphdr *hdr = heap->heap_allocated;
  42996. while (hdr) {
  42997. /* Cannot really assert much w.r.t. refcounts now. */
  42998. if (DUK_HEAPHDR_GET_REFCOUNT(hdr) == 0 &&
  42999. DUK_HEAPHDR_HAS_FINALIZED(hdr)) {
  43000. /* An object may be in heap_allocated list with a zero
  43001. * refcount if it has just been finalized and is waiting
  43002. * to be collected by the next cycle.
  43003. * (This doesn't currently happen however.)
  43004. */
  43005. } else if (DUK_HEAPHDR_GET_REFCOUNT(hdr) == 0) {
  43006. /* An object may be in heap_allocated list with a zero
  43007. * refcount also if it is a temporary object created
  43008. * during debugger paused state. It will get collected
  43009. * by mark-and-sweep based on its reachability status
  43010. * (presumably not reachable because refcount is 0).
  43011. */
  43012. }
  43013. DUK_ASSERT_DISABLE(DUK_HEAPHDR_GET_REFCOUNT(hdr) >= 0); /* Unsigned. */
  43014. hdr = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  43015. }
  43016. }
  43017. DUK_LOCAL void duk__clear_assert_refcounts(duk_heap *heap) {
  43018. duk_heaphdr *curr;
  43019. duk_uint32_t i;
  43020. for (curr = heap->heap_allocated; curr != NULL; curr = DUK_HEAPHDR_GET_NEXT(heap, curr)) {
  43021. curr->h_assert_refcount = 0;
  43022. }
  43023. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43024. for (curr = heap->finalize_list; curr != NULL; curr = DUK_HEAPHDR_GET_NEXT(heap, curr)) {
  43025. curr->h_assert_refcount = 0;
  43026. }
  43027. #endif
  43028. #if defined(DUK_USE_REFERENCE_COUNTING)
  43029. for (curr = heap->refzero_list; curr != NULL; curr = DUK_HEAPHDR_GET_NEXT(heap, curr)) {
  43030. curr->h_assert_refcount = 0;
  43031. }
  43032. #endif
  43033. for (i = 0; i < heap->st_size; i++) {
  43034. duk_hstring *h;
  43035. #if defined(DUK_USE_STRTAB_PTRCOMP)
  43036. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, heap->strtable16[i]);
  43037. #else
  43038. h = heap->strtable[i];
  43039. #endif
  43040. while (h != NULL) {
  43041. ((duk_heaphdr *) h)->h_assert_refcount = 0;
  43042. h = h->hdr.h_next;
  43043. }
  43044. }
  43045. }
  43046. DUK_LOCAL void duk__check_refcount_heaphdr(duk_heaphdr *hdr) {
  43047. duk_bool_t count_ok;
  43048. duk_size_t expect_refc;
  43049. /* The refcount check only makes sense for reachable objects on
  43050. * heap_allocated or string table, after the sweep phase. Prior to
  43051. * sweep phase refcounts will include references that are not visible
  43052. * via reachability roots.
  43053. *
  43054. * Because we're called after the sweep phase, all heap objects on
  43055. * heap_allocated are reachable. REACHABLE flags have already been
  43056. * cleared so we can't check them.
  43057. */
  43058. /* ROM objects have intentionally incorrect refcount (1), but we won't
  43059. * check them.
  43060. */
  43061. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY(hdr));
  43062. expect_refc = hdr->h_assert_refcount;
  43063. if (DUK_HEAPHDR_IS_STRING(hdr) && DUK_HSTRING_HAS_PINNED_LITERAL((duk_hstring *) hdr)) {
  43064. expect_refc++;
  43065. }
  43066. count_ok = ((duk_size_t) DUK_HEAPHDR_GET_REFCOUNT(hdr) == expect_refc);
  43067. if (!count_ok) {
  43068. DUK_D(DUK_DPRINT("refcount mismatch for: %p: header=%ld counted=%ld --> %!iO",
  43069. (void *) hdr, (long) DUK_HEAPHDR_GET_REFCOUNT(hdr),
  43070. (long) hdr->h_assert_refcount, hdr));
  43071. DUK_ASSERT(0);
  43072. }
  43073. }
  43074. DUK_LOCAL void duk__check_assert_refcounts(duk_heap *heap) {
  43075. duk_heaphdr *curr;
  43076. duk_uint32_t i;
  43077. for (curr = heap->heap_allocated; curr != NULL; curr = DUK_HEAPHDR_GET_NEXT(heap, curr)) {
  43078. duk__check_refcount_heaphdr(curr);
  43079. }
  43080. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43081. for (curr = heap->finalize_list; curr != NULL; curr = DUK_HEAPHDR_GET_NEXT(heap, curr)) {
  43082. duk__check_refcount_heaphdr(curr);
  43083. }
  43084. #endif
  43085. for (i = 0; i < heap->st_size; i++) {
  43086. duk_hstring *h;
  43087. #if defined(DUK_USE_STRTAB_PTRCOMP)
  43088. h = DUK_USE_HEAPPTR_DEC16(heap->heap_udata, heap->strtable16[i]);
  43089. #else
  43090. h = heap->strtable[i];
  43091. #endif
  43092. while (h != NULL) {
  43093. duk__check_refcount_heaphdr((duk_heaphdr *) h);
  43094. h = h->hdr.h_next;
  43095. }
  43096. }
  43097. }
  43098. #endif /* DUK_USE_REFERENCE_COUNTING */
  43099. #if defined(DUK_USE_LITCACHE_SIZE)
  43100. DUK_LOCAL void duk__assert_litcache_nulls(duk_heap *heap) {
  43101. duk_uint_t i;
  43102. duk_litcache_entry *e;
  43103. e = heap->litcache;
  43104. for (i = 0; i < DUK_USE_LITCACHE_SIZE; i++) {
  43105. /* Entry addresses were NULLed before mark-and-sweep, check
  43106. * that they're still NULL afterwards to ensure no pointers
  43107. * were recorded through any side effects.
  43108. */
  43109. DUK_ASSERT(e->addr == NULL);
  43110. }
  43111. }
  43112. #endif /* DUK_USE_LITCACHE_SIZE */
  43113. #endif /* DUK_USE_ASSERTIONS */
  43114. /*
  43115. * Stats dump.
  43116. */
  43117. #if defined(DUK_USE_DEBUG)
  43118. DUK_LOCAL void duk__dump_stats(duk_heap *heap) {
  43119. DUK_D(DUK_DPRINT("stats executor: opcodes=%ld, interrupt=%ld, throw=%ld",
  43120. (long) heap->stats_exec_opcodes, (long) heap->stats_exec_interrupt,
  43121. (long) heap->stats_exec_throw));
  43122. DUK_D(DUK_DPRINT("stats call: all=%ld, tailcall=%ld, ecmatoecma=%ld",
  43123. (long) heap->stats_call_all, (long) heap->stats_call_tailcall,
  43124. (long) heap->stats_call_ecmatoecma));
  43125. DUK_D(DUK_DPRINT("stats safecall: all=%ld, nothrow=%ld, throw=%ld",
  43126. (long) heap->stats_safecall_all, (long) heap->stats_safecall_nothrow,
  43127. (long) heap->stats_safecall_throw));
  43128. DUK_D(DUK_DPRINT("stats mark-and-sweep: try_count=%ld, skip_count=%ld, emergency_count=%ld",
  43129. (long) heap->stats_ms_try_count, (long) heap->stats_ms_skip_count,
  43130. (long) heap->stats_ms_emergency_count));
  43131. DUK_D(DUK_DPRINT("stats stringtable: intern_hit=%ld, intern_miss=%ld, "
  43132. "resize_check=%ld, resize_grow=%ld, resize_shrink=%ld, "
  43133. "litcache_hit=%ld, litcache_miss=%ld, litcache_pin=%ld",
  43134. (long) heap->stats_strtab_intern_hit, (long) heap->stats_strtab_intern_miss,
  43135. (long) heap->stats_strtab_resize_check, (long) heap->stats_strtab_resize_grow,
  43136. (long) heap->stats_strtab_resize_shrink, (long) heap->stats_strtab_litcache_hit,
  43137. (long) heap->stats_strtab_litcache_miss, (long) heap->stats_strtab_litcache_pin));
  43138. DUK_D(DUK_DPRINT("stats object: realloc_props=%ld, abandon_array=%ld",
  43139. (long) heap->stats_object_realloc_props, (long) heap->stats_object_abandon_array));
  43140. DUK_D(DUK_DPRINT("stats getownpropdesc: count=%ld, hit=%ld, miss=%ld",
  43141. (long) heap->stats_getownpropdesc_count, (long) heap->stats_getownpropdesc_hit,
  43142. (long) heap->stats_getownpropdesc_miss));
  43143. DUK_D(DUK_DPRINT("stats getpropdesc: count=%ld, hit=%ld, miss=%ld",
  43144. (long) heap->stats_getpropdesc_count, (long) heap->stats_getpropdesc_hit,
  43145. (long) heap->stats_getpropdesc_miss));
  43146. DUK_D(DUK_DPRINT("stats getprop: all=%ld, arrayidx=%ld, bufobjidx=%ld, "
  43147. "bufferidx=%ld, bufferlen=%ld, stringidx=%ld, stringlen=%ld, "
  43148. "proxy=%ld, arguments=%ld",
  43149. (long) heap->stats_getprop_all, (long) heap->stats_getprop_arrayidx,
  43150. (long) heap->stats_getprop_bufobjidx, (long) heap->stats_getprop_bufferidx,
  43151. (long) heap->stats_getprop_bufferlen, (long) heap->stats_getprop_stringidx,
  43152. (long) heap->stats_getprop_stringlen, (long) heap->stats_getprop_proxy,
  43153. (long) heap->stats_getprop_arguments));
  43154. DUK_D(DUK_DPRINT("stats putprop: all=%ld, arrayidx=%ld, bufobjidx=%ld, "
  43155. "bufferidx=%ld, proxy=%ld",
  43156. (long) heap->stats_putprop_all, (long) heap->stats_putprop_arrayidx,
  43157. (long) heap->stats_putprop_bufobjidx, (long) heap->stats_putprop_bufferidx,
  43158. (long) heap->stats_putprop_proxy));
  43159. DUK_D(DUK_DPRINT("stats getvar: all=%ld",
  43160. (long) heap->stats_getvar_all));
  43161. DUK_D(DUK_DPRINT("stats putvar: all=%ld",
  43162. (long) heap->stats_putvar_all));
  43163. }
  43164. #endif /* DUK_USE_DEBUG */
  43165. /*
  43166. * Main mark-and-sweep function.
  43167. *
  43168. * 'flags' represents the features requested by the caller. The current
  43169. * heap->ms_base_flags is ORed automatically into the flags; the base flags
  43170. * mask typically prevents certain mark-and-sweep operation to avoid trouble.
  43171. */
  43172. DUK_INTERNAL void duk_heap_mark_and_sweep(duk_heap *heap, duk_small_uint_t flags) {
  43173. duk_size_t count_keep_obj;
  43174. duk_size_t count_keep_str;
  43175. #if defined(DUK_USE_VOLUNTARY_GC)
  43176. duk_size_t tmp;
  43177. #endif
  43178. DUK_STATS_INC(heap, stats_ms_try_count);
  43179. #if defined(DUK_USE_DEBUG)
  43180. if (flags & DUK_MS_FLAG_EMERGENCY) {
  43181. DUK_STATS_INC(heap, stats_ms_emergency_count);
  43182. }
  43183. #endif
  43184. /* If debugger is paused, garbage collection is disabled by default.
  43185. * This is achieved by bumping ms_prevent_count when becoming paused.
  43186. */
  43187. DUK_ASSERT(!DUK_HEAP_HAS_DEBUGGER_PAUSED(heap) || heap->ms_prevent_count > 0);
  43188. /* Prevention/recursion check as soon as possible because we may
  43189. * be called a number of times when voluntary mark-and-sweep is
  43190. * pending.
  43191. */
  43192. if (heap->ms_prevent_count != 0) {
  43193. DUK_DD(DUK_DDPRINT("reject recursive mark-and-sweep"));
  43194. DUK_STATS_INC(heap, stats_ms_skip_count);
  43195. return;
  43196. }
  43197. DUK_ASSERT(heap->ms_running == 0); /* ms_prevent_count is bumped when ms_running is set */
  43198. /* Heap_thread is used during mark-and-sweep for refcount finalization
  43199. * (it's also used for finalizer execution once mark-and-sweep is
  43200. * complete). Heap allocation code ensures heap_thread is set and
  43201. * properly initialized before setting ms_prevent_count to 0.
  43202. */
  43203. DUK_ASSERT(heap->heap_thread != NULL);
  43204. DUK_ASSERT(heap->heap_thread->valstack != NULL);
  43205. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) starting, requested flags: 0x%08lx, effective flags: 0x%08lx",
  43206. (unsigned long) flags, (unsigned long) (flags | heap->ms_base_flags)));
  43207. flags |= heap->ms_base_flags;
  43208. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43209. if (heap->finalize_list != NULL) {
  43210. flags |= DUK_MS_FLAG_POSTPONE_RESCUE;
  43211. }
  43212. #endif
  43213. /*
  43214. * Assertions before
  43215. */
  43216. #if defined(DUK_USE_ASSERTIONS)
  43217. DUK_ASSERT(heap->ms_prevent_count == 0);
  43218. DUK_ASSERT(heap->ms_running == 0);
  43219. DUK_ASSERT(!DUK_HEAP_HAS_DEBUGGER_PAUSED(heap));
  43220. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap));
  43221. DUK_ASSERT(heap->ms_recursion_depth == 0);
  43222. duk__assert_heaphdr_flags(heap);
  43223. #if defined(DUK_USE_REFERENCE_COUNTING)
  43224. /* Note: heap->refzero_free_running may be true; a refcount
  43225. * finalizer may trigger a mark-and-sweep.
  43226. */
  43227. duk__assert_valid_refcounts(heap);
  43228. #endif /* DUK_USE_REFERENCE_COUNTING */
  43229. #endif /* DUK_USE_ASSERTIONS */
  43230. /*
  43231. * Begin
  43232. */
  43233. DUK_ASSERT(heap->ms_prevent_count == 0);
  43234. DUK_ASSERT(heap->ms_running == 0);
  43235. heap->ms_prevent_count = 1;
  43236. heap->ms_running = 1;
  43237. /*
  43238. * Free activation/catcher freelists on every mark-and-sweep for now.
  43239. * This is an initial rough draft; ideally we'd keep count of the
  43240. * freelist size and free only excess entries.
  43241. */
  43242. DUK_D(DUK_DPRINT("freeing temporary freelists"));
  43243. duk_heap_free_freelists(heap);
  43244. /*
  43245. * Mark roots, hoping that recursion limit is not normally hit.
  43246. * If recursion limit is hit, run additional reachability rounds
  43247. * starting from "temproots" until marking is complete.
  43248. *
  43249. * Marking happens in two phases: first we mark actual reachability
  43250. * roots (and run "temproots" to complete the process). Then we
  43251. * check which objects are unreachable and are finalizable; such
  43252. * objects are marked as FINALIZABLE and marked as reachability
  43253. * (and "temproots" is run again to complete the process).
  43254. *
  43255. * The heap finalize_list must also be marked as a reachability root.
  43256. * There may be objects on the list from a previous round if the
  43257. * previous run had finalizer skip flag.
  43258. */
  43259. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  43260. duk__clear_assert_refcounts(heap);
  43261. #endif
  43262. #if defined(DUK_USE_LITCACHE_SIZE)
  43263. duk__wipe_litcache(heap);
  43264. #endif
  43265. duk__mark_roots_heap(heap); /* Mark main reachability roots. */
  43266. #if defined(DUK_USE_REFERENCE_COUNTING)
  43267. DUK_ASSERT(heap->refzero_list == NULL); /* Always handled to completion inline in DECREF. */
  43268. #endif
  43269. duk__mark_temproots_by_heap_scan(heap); /* Temproots. */
  43270. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43271. duk__mark_finalizable(heap); /* Mark finalizable as reachability roots. */
  43272. duk__mark_finalize_list(heap); /* Mark finalizer work list as reachability roots. */
  43273. #endif
  43274. duk__mark_temproots_by_heap_scan(heap); /* Temproots. */
  43275. /*
  43276. * Sweep garbage and remove marking flags, and move objects with
  43277. * finalizers to the finalizer work list.
  43278. *
  43279. * Objects to be swept need to get their refcounts finalized before
  43280. * they are swept. In other words, their target object refcounts
  43281. * need to be decreased. This has to be done before freeing any
  43282. * objects to avoid decref'ing dangling pointers (which may happen
  43283. * even without bugs, e.g. with reference loops)
  43284. *
  43285. * Because strings don't point to other heap objects, similar
  43286. * finalization is not necessary for strings.
  43287. */
  43288. /* XXX: more emergency behavior, e.g. find smaller hash sizes etc */
  43289. #if defined(DUK_USE_REFERENCE_COUNTING)
  43290. duk__finalize_refcounts(heap);
  43291. #endif
  43292. duk__sweep_heap(heap, flags, &count_keep_obj);
  43293. duk__sweep_stringtable(heap, &count_keep_str);
  43294. #if defined(DUK_USE_ASSERTIONS) && defined(DUK_USE_REFERENCE_COUNTING)
  43295. duk__check_assert_refcounts(heap);
  43296. #endif
  43297. #if defined(DUK_USE_REFERENCE_COUNTING)
  43298. DUK_ASSERT(heap->refzero_list == NULL); /* Always handled to completion inline in DECREF. */
  43299. #endif
  43300. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43301. duk__clear_finalize_list_flags(heap);
  43302. #endif
  43303. /*
  43304. * Object compaction (emergency only).
  43305. *
  43306. * Object compaction is a separate step after sweeping, as there is
  43307. * more free memory for it to work with. Also, currently compaction
  43308. * may insert new objects into the heap allocated list and the string
  43309. * table which we don't want to do during a sweep (the reachability
  43310. * flags of such objects would be incorrect). The objects inserted
  43311. * are currently:
  43312. *
  43313. * - a temporary duk_hbuffer for a new properties allocation
  43314. * - if array part is abandoned, string keys are interned
  43315. *
  43316. * The object insertions go to the front of the list, so they do not
  43317. * cause an infinite loop (they are not compacted).
  43318. */
  43319. if ((flags & DUK_MS_FLAG_EMERGENCY) &&
  43320. !(flags & DUK_MS_FLAG_NO_OBJECT_COMPACTION)) {
  43321. duk__compact_objects(heap);
  43322. }
  43323. /*
  43324. * String table resize check.
  43325. *
  43326. * This is mainly useful in emergency GC: if the string table load
  43327. * factor is really low for some reason, we can shrink the string
  43328. * table to a smaller size and free some memory in the process.
  43329. * Only execute in emergency GC. String table has internal flags
  43330. * to protect against recursive resizing if this mark-and-sweep pass
  43331. * was triggered by a string table resize.
  43332. */
  43333. if (flags & DUK_MS_FLAG_EMERGENCY) {
  43334. DUK_D(DUK_DPRINT("stringtable resize check in emergency gc"));
  43335. duk_heap_strtable_force_resize(heap);
  43336. }
  43337. /*
  43338. * Finish
  43339. */
  43340. DUK_ASSERT(heap->ms_prevent_count == 1);
  43341. heap->ms_prevent_count = 0;
  43342. DUK_ASSERT(heap->ms_running == 1);
  43343. heap->ms_running = 0;
  43344. /*
  43345. * Assertions after
  43346. */
  43347. #if defined(DUK_USE_ASSERTIONS)
  43348. DUK_ASSERT(heap->ms_prevent_count == 0);
  43349. DUK_ASSERT(!DUK_HEAP_HAS_MARKANDSWEEP_RECLIMIT_REACHED(heap));
  43350. DUK_ASSERT(heap->ms_recursion_depth == 0);
  43351. duk__assert_heaphdr_flags(heap);
  43352. #if defined(DUK_USE_REFERENCE_COUNTING)
  43353. /* Note: heap->refzero_free_running may be true; a refcount
  43354. * finalizer may trigger a mark-and-sweep.
  43355. */
  43356. duk__assert_valid_refcounts(heap);
  43357. #endif /* DUK_USE_REFERENCE_COUNTING */
  43358. #if defined(DUK_USE_LITCACHE_SIZE)
  43359. duk__assert_litcache_nulls(heap);
  43360. #endif /* DUK_USE_LITCACHE_SIZE */
  43361. #endif /* DUK_USE_ASSERTIONS */
  43362. /*
  43363. * Reset trigger counter
  43364. */
  43365. #if defined(DUK_USE_VOLUNTARY_GC)
  43366. tmp = (count_keep_obj + count_keep_str) / 256;
  43367. heap->ms_trigger_counter = (duk_int_t) (
  43368. (tmp * DUK_HEAP_MARK_AND_SWEEP_TRIGGER_MULT) +
  43369. DUK_HEAP_MARK_AND_SWEEP_TRIGGER_ADD);
  43370. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) finished: %ld objects kept, %ld strings kept, trigger reset to %ld",
  43371. (long) count_keep_obj, (long) count_keep_str, (long) heap->ms_trigger_counter));
  43372. #else
  43373. DUK_D(DUK_DPRINT("garbage collect (mark-and-sweep) finished: %ld objects kept, %ld strings kept, no voluntary trigger",
  43374. (long) count_keep_obj, (long) count_keep_str));
  43375. #endif
  43376. /*
  43377. * Stats dump
  43378. */
  43379. #if defined(DUK_USE_DEBUG)
  43380. duk__dump_stats(heap);
  43381. #endif
  43382. /*
  43383. * Finalize objects in the finalization work list. Finalized
  43384. * objects are queued back to heap_allocated with FINALIZED set.
  43385. *
  43386. * Since finalizers may cause arbitrary side effects, they are
  43387. * prevented e.g. during string table and object property allocation
  43388. * resizing using heap->pf_prevent_count. In this case the objects
  43389. * remain in the finalization work list after mark-and-sweep exits
  43390. * and they may be finalized on the next pass or any DECREF checking
  43391. * for finalize_list.
  43392. *
  43393. * As of Duktape 2.1 finalization happens outside mark-and-sweep
  43394. * protection. Mark-and-sweep is allowed while the finalize_list
  43395. * is being processed, but no rescue decisions are done while the
  43396. * process is on-going. This avoids incorrect rescue decisions
  43397. * if an object is considered reachable (and thus rescued) because
  43398. * of a reference via finalize_list (which is considered a reachability
  43399. * root). When finalize_list is being processed, reachable objects
  43400. * with FINALIZED set will just keep their FINALIZED flag for later
  43401. * mark-and-sweep processing.
  43402. *
  43403. * This could also be handled (a bit better) by having a more refined
  43404. * notion of reachability for rescue/free decisions.
  43405. *
  43406. * XXX: avoid finalizer execution when doing emergency GC?
  43407. */
  43408. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43409. /* Attempt to process finalize_list, pf_prevent_count check
  43410. * is inside the target.
  43411. */
  43412. duk_heap_process_finalize_list(heap);
  43413. #endif /* DUK_USE_FINALIZER_SUPPORT */
  43414. }
  43415. #line 1 "duk_heap_memory.c"
  43416. /*
  43417. * Memory allocation handling.
  43418. */
  43419. /* #include duk_internal.h -> already included */
  43420. /*
  43421. * Voluntary GC check
  43422. */
  43423. #if defined(DUK_USE_VOLUNTARY_GC)
  43424. DUK_LOCAL DUK_INLINE void duk__check_voluntary_gc(duk_heap *heap) {
  43425. if (DUK_UNLIKELY(--(heap)->ms_trigger_counter < 0)) {
  43426. #if defined(DUK_USE_DEBUG)
  43427. if (heap->ms_prevent_count == 0) {
  43428. DUK_D(DUK_DPRINT("triggering voluntary mark-and-sweep"));
  43429. } else {
  43430. DUK_DD(DUK_DDPRINT("gc blocked -> skip voluntary mark-and-sweep now"));
  43431. }
  43432. #endif
  43433. /* Prevention checks in the call target handle cases where
  43434. * voluntary GC is not allowed. The voluntary GC trigger
  43435. * counter is only rewritten if mark-and-sweep actually runs.
  43436. */
  43437. duk_heap_mark_and_sweep(heap, DUK_MS_FLAG_VOLUNTARY /*flags*/);
  43438. }
  43439. }
  43440. #define DUK__VOLUNTARY_PERIODIC_GC(heap) do { duk__check_voluntary_gc((heap)); } while (0)
  43441. #else
  43442. #define DUK__VOLUNTARY_PERIODIC_GC(heap) /* no voluntary gc */
  43443. #endif /* DUK_USE_VOLUNTARY_GC */
  43444. /*
  43445. * Allocate memory with garbage collection
  43446. */
  43447. DUK_INTERNAL void *duk_heap_mem_alloc(duk_heap *heap, duk_size_t size) {
  43448. void *res;
  43449. duk_small_int_t i;
  43450. DUK_ASSERT(heap != NULL);
  43451. DUK_ASSERT_DISABLE(size >= 0);
  43452. /*
  43453. * Voluntary periodic GC (if enabled)
  43454. */
  43455. DUK__VOLUNTARY_PERIODIC_GC(heap);
  43456. /*
  43457. * First attempt
  43458. */
  43459. #if defined(DUK_USE_GC_TORTURE)
  43460. /* Simulate alloc failure on every alloc, except when mark-and-sweep
  43461. * is running.
  43462. */
  43463. if (heap->ms_prevent_count == 0) {
  43464. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first alloc attempt fails"));
  43465. res = NULL;
  43466. DUK_UNREF(res);
  43467. goto skip_attempt;
  43468. }
  43469. #endif
  43470. res = heap->alloc_func(heap->heap_udata, size);
  43471. if (DUK_LIKELY(res || size == 0)) {
  43472. /* For zero size allocations NULL is allowed. */
  43473. return res;
  43474. }
  43475. #if defined(DUK_USE_GC_TORTURE)
  43476. skip_attempt:
  43477. #endif
  43478. DUK_D(DUK_DPRINT("first alloc attempt failed, attempt to gc and retry"));
  43479. #if 0
  43480. /*
  43481. * Avoid a GC if GC is already running. This can happen at a late
  43482. * stage in a GC when we try to e.g. resize the stringtable
  43483. * or compact objects.
  43484. *
  43485. * NOTE: explicit handling isn't actually be needed: if the GC is
  43486. * not allowed, duk_heap_mark_and_sweep() will reject it for every
  43487. * attempt in the loop below, resulting in a NULL same as here.
  43488. */
  43489. if (heap->ms_prevent_count != 0) {
  43490. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed, gc in progress (gc skipped), alloc size %ld", (long) size));
  43491. return NULL;
  43492. }
  43493. #endif
  43494. /*
  43495. * Retry with several GC attempts. Initial attempts are made without
  43496. * emergency mode; later attempts use emergency mode which minimizes
  43497. * memory allocations forcibly.
  43498. */
  43499. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  43500. duk_small_uint_t flags;
  43501. flags = 0;
  43502. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  43503. flags |= DUK_MS_FLAG_EMERGENCY;
  43504. }
  43505. duk_heap_mark_and_sweep(heap, flags);
  43506. res = heap->alloc_func(heap->heap_udata, size);
  43507. if (res) {
  43508. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() succeeded after gc (pass %ld), alloc size %ld",
  43509. (long) (i + 1), (long) size));
  43510. return res;
  43511. }
  43512. }
  43513. DUK_D(DUK_DPRINT("duk_heap_mem_alloc() failed even after gc, alloc size %ld", (long) size));
  43514. return NULL;
  43515. }
  43516. DUK_INTERNAL void *duk_heap_mem_alloc_zeroed(duk_heap *heap, duk_size_t size) {
  43517. void *res;
  43518. DUK_ASSERT(heap != NULL);
  43519. DUK_ASSERT_DISABLE(size >= 0);
  43520. res = DUK_ALLOC(heap, size);
  43521. if (DUK_LIKELY(res != NULL)) {
  43522. duk_memzero(res, size);
  43523. }
  43524. return res;
  43525. }
  43526. DUK_INTERNAL void *duk_heap_mem_alloc_checked(duk_hthread *thr, duk_size_t size) {
  43527. void *res;
  43528. DUK_ASSERT(thr != NULL);
  43529. res = duk_heap_mem_alloc(thr->heap, size);
  43530. if (DUK_LIKELY(res != NULL || size == 0)) {
  43531. return res;
  43532. }
  43533. DUK_ERROR_ALLOC_FAILED(thr);
  43534. DUK_WO_NORETURN(return NULL;);
  43535. }
  43536. DUK_INTERNAL void *duk_heap_mem_alloc_checked_zeroed(duk_hthread *thr, duk_size_t size) {
  43537. void *res;
  43538. DUK_ASSERT(thr != NULL);
  43539. res = duk_heap_mem_alloc_zeroed(thr->heap, size);
  43540. if (DUK_LIKELY(res != NULL || size == 0)) {
  43541. return res;
  43542. }
  43543. DUK_ERROR_ALLOC_FAILED(thr);
  43544. DUK_WO_NORETURN(return NULL;);
  43545. }
  43546. /*
  43547. * Reallocate memory with garbage collection
  43548. */
  43549. DUK_INTERNAL void *duk_heap_mem_realloc(duk_heap *heap, void *ptr, duk_size_t newsize) {
  43550. void *res;
  43551. duk_small_int_t i;
  43552. DUK_ASSERT(heap != NULL);
  43553. /* ptr may be NULL */
  43554. DUK_ASSERT_DISABLE(newsize >= 0);
  43555. /*
  43556. * Voluntary periodic GC (if enabled)
  43557. */
  43558. DUK__VOLUNTARY_PERIODIC_GC(heap);
  43559. /*
  43560. * First attempt
  43561. */
  43562. #if defined(DUK_USE_GC_TORTURE)
  43563. /* Simulate alloc failure on every realloc, except when mark-and-sweep
  43564. * is running.
  43565. */
  43566. if (heap->ms_prevent_count == 0) {
  43567. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first realloc attempt fails"));
  43568. res = NULL;
  43569. DUK_UNREF(res);
  43570. goto skip_attempt;
  43571. }
  43572. #endif
  43573. res = heap->realloc_func(heap->heap_udata, ptr, newsize);
  43574. if (DUK_LIKELY(res || newsize == 0)) {
  43575. /* For zero size allocations NULL is allowed. */
  43576. return res;
  43577. }
  43578. #if defined(DUK_USE_GC_TORTURE)
  43579. skip_attempt:
  43580. #endif
  43581. DUK_D(DUK_DPRINT("first realloc attempt failed, attempt to gc and retry"));
  43582. #if 0
  43583. /*
  43584. * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
  43585. */
  43586. if (heap->ms_prevent_count != 0) {
  43587. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
  43588. return NULL;
  43589. }
  43590. #endif
  43591. /*
  43592. * Retry with several GC attempts. Initial attempts are made without
  43593. * emergency mode; later attempts use emergency mode which minimizes
  43594. * memory allocations forcibly.
  43595. */
  43596. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  43597. duk_small_uint_t flags;
  43598. flags = 0;
  43599. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  43600. flags |= DUK_MS_FLAG_EMERGENCY;
  43601. }
  43602. duk_heap_mark_and_sweep(heap, flags);
  43603. res = heap->realloc_func(heap->heap_udata, ptr, newsize);
  43604. if (res || newsize == 0) {
  43605. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() succeeded after gc (pass %ld), alloc size %ld",
  43606. (long) (i + 1), (long) newsize));
  43607. return res;
  43608. }
  43609. }
  43610. DUK_D(DUK_DPRINT("duk_heap_mem_realloc() failed even after gc, alloc size %ld", (long) newsize));
  43611. return NULL;
  43612. }
  43613. /*
  43614. * Reallocate memory with garbage collection, using a callback to provide
  43615. * the current allocated pointer. This variant is used when a mark-and-sweep
  43616. * (e.g. finalizers) might change the original pointer.
  43617. */
  43618. DUK_INTERNAL void *duk_heap_mem_realloc_indirect(duk_heap *heap, duk_mem_getptr cb, void *ud, duk_size_t newsize) {
  43619. void *res;
  43620. duk_small_int_t i;
  43621. DUK_ASSERT(heap != NULL);
  43622. DUK_ASSERT_DISABLE(newsize >= 0);
  43623. /*
  43624. * Voluntary periodic GC (if enabled)
  43625. */
  43626. DUK__VOLUNTARY_PERIODIC_GC(heap);
  43627. /*
  43628. * First attempt
  43629. */
  43630. #if defined(DUK_USE_GC_TORTURE)
  43631. /* Simulate alloc failure on every realloc, except when mark-and-sweep
  43632. * is running.
  43633. */
  43634. if (heap->ms_prevent_count == 0) {
  43635. DUK_DDD(DUK_DDDPRINT("gc torture enabled, pretend that first indirect realloc attempt fails"));
  43636. res = NULL;
  43637. DUK_UNREF(res);
  43638. goto skip_attempt;
  43639. }
  43640. #endif
  43641. res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  43642. if (DUK_LIKELY(res || newsize == 0)) {
  43643. /* For zero size allocations NULL is allowed. */
  43644. return res;
  43645. }
  43646. #if defined(DUK_USE_GC_TORTURE)
  43647. skip_attempt:
  43648. #endif
  43649. DUK_D(DUK_DPRINT("first indirect realloc attempt failed, attempt to gc and retry"));
  43650. #if 0
  43651. /*
  43652. * Avoid a GC if GC is already running. See duk_heap_mem_alloc().
  43653. */
  43654. if (heap->ms_prevent_count != 0) {
  43655. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed, gc in progress (gc skipped), alloc size %ld", (long) newsize));
  43656. return NULL;
  43657. }
  43658. #endif
  43659. /*
  43660. * Retry with several GC attempts. Initial attempts are made without
  43661. * emergency mode; later attempts use emergency mode which minimizes
  43662. * memory allocations forcibly.
  43663. */
  43664. for (i = 0; i < DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_LIMIT; i++) {
  43665. duk_small_uint_t flags;
  43666. #if defined(DUK_USE_DEBUG)
  43667. void *ptr_pre;
  43668. void *ptr_post;
  43669. #endif
  43670. #if defined(DUK_USE_DEBUG)
  43671. ptr_pre = cb(heap, ud);
  43672. #endif
  43673. flags = 0;
  43674. if (i >= DUK_HEAP_ALLOC_FAIL_MARKANDSWEEP_EMERGENCY_LIMIT - 1) {
  43675. flags |= DUK_MS_FLAG_EMERGENCY;
  43676. }
  43677. duk_heap_mark_and_sweep(heap, flags);
  43678. #if defined(DUK_USE_DEBUG)
  43679. ptr_post = cb(heap, ud);
  43680. if (ptr_pre != ptr_post) {
  43681. DUK_DD(DUK_DDPRINT("realloc base pointer changed by mark-and-sweep: %p -> %p",
  43682. (void *) ptr_pre, (void *) ptr_post));
  43683. }
  43684. #endif
  43685. /* Note: key issue here is to re-lookup the base pointer on every attempt.
  43686. * The pointer being reallocated may change after every mark-and-sweep.
  43687. */
  43688. res = heap->realloc_func(heap->heap_udata, cb(heap, ud), newsize);
  43689. if (res || newsize == 0) {
  43690. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() succeeded after gc (pass %ld), alloc size %ld",
  43691. (long) (i + 1), (long) newsize));
  43692. return res;
  43693. }
  43694. }
  43695. DUK_D(DUK_DPRINT("duk_heap_mem_realloc_indirect() failed even after gc, alloc size %ld", (long) newsize));
  43696. return NULL;
  43697. }
  43698. /*
  43699. * Free memory
  43700. */
  43701. DUK_INTERNAL void duk_heap_mem_free(duk_heap *heap, void *ptr) {
  43702. DUK_ASSERT(heap != NULL);
  43703. /* ptr may be NULL */
  43704. /* Must behave like a no-op with NULL and any pointer returned from
  43705. * malloc/realloc with zero size.
  43706. */
  43707. heap->free_func(heap->heap_udata, ptr);
  43708. /* Never perform a GC (even voluntary) in a memory free, otherwise
  43709. * all call sites doing frees would need to deal with the side effects.
  43710. * No need to update voluntary GC counter either.
  43711. */
  43712. }
  43713. /* automatic undefs */
  43714. #undef DUK__VOLUNTARY_PERIODIC_GC
  43715. #line 1 "duk_heap_misc.c"
  43716. /*
  43717. * Support functions for duk_heap.
  43718. */
  43719. /* #include duk_internal.h -> already included */
  43720. DUK_INTERNAL void duk_heap_insert_into_heap_allocated(duk_heap *heap, duk_heaphdr *hdr) {
  43721. duk_heaphdr *root;
  43722. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) != DUK_HTYPE_STRING);
  43723. root = heap->heap_allocated;
  43724. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  43725. if (root != NULL) {
  43726. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, root) == NULL);
  43727. DUK_HEAPHDR_SET_PREV(heap, root, hdr);
  43728. }
  43729. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  43730. #endif
  43731. DUK_HEAPHDR_SET_NEXT(heap, hdr, root);
  43732. DUK_ASSERT_HEAPHDR_LINKS(heap, hdr);
  43733. DUK_ASSERT_HEAPHDR_LINKS(heap, root);
  43734. heap->heap_allocated = hdr;
  43735. }
  43736. #if defined(DUK_USE_REFERENCE_COUNTING)
  43737. DUK_INTERNAL void duk_heap_remove_from_heap_allocated(duk_heap *heap, duk_heaphdr *hdr) {
  43738. duk_heaphdr *prev;
  43739. duk_heaphdr *next;
  43740. /* Strings are in string table. */
  43741. DUK_ASSERT(hdr != NULL);
  43742. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(hdr) != DUK_HTYPE_STRING);
  43743. /* Target 'hdr' must be in heap_allocated (not e.g. finalize_list).
  43744. * If not, heap lists will become corrupted so assert early for it.
  43745. */
  43746. #if defined(DUK_USE_ASSERTIONS)
  43747. {
  43748. duk_heaphdr *tmp;
  43749. for (tmp = heap->heap_allocated; tmp != NULL; tmp = DUK_HEAPHDR_GET_NEXT(heap, tmp)) {
  43750. if (tmp == hdr) {
  43751. break;
  43752. }
  43753. }
  43754. DUK_ASSERT(tmp == hdr);
  43755. }
  43756. #endif
  43757. /* Read/write only once to minimize pointer compression calls. */
  43758. prev = DUK_HEAPHDR_GET_PREV(heap, hdr);
  43759. next = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  43760. if (prev != NULL) {
  43761. DUK_ASSERT(heap->heap_allocated != hdr);
  43762. DUK_HEAPHDR_SET_NEXT(heap, prev, next);
  43763. } else {
  43764. DUK_ASSERT(heap->heap_allocated == hdr);
  43765. heap->heap_allocated = next;
  43766. }
  43767. if (next != NULL) {
  43768. DUK_HEAPHDR_SET_PREV(heap, next, prev);
  43769. } else {
  43770. ;
  43771. }
  43772. }
  43773. #endif /* DUK_USE_REFERENCE_COUNTING */
  43774. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43775. DUK_INTERNAL void duk_heap_insert_into_finalize_list(duk_heap *heap, duk_heaphdr *hdr) {
  43776. duk_heaphdr *root;
  43777. root = heap->finalize_list;
  43778. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  43779. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  43780. if (root != NULL) {
  43781. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, root) == NULL);
  43782. DUK_HEAPHDR_SET_PREV(heap, root, hdr);
  43783. }
  43784. #endif
  43785. DUK_HEAPHDR_SET_NEXT(heap, hdr, root);
  43786. DUK_ASSERT_HEAPHDR_LINKS(heap, hdr);
  43787. DUK_ASSERT_HEAPHDR_LINKS(heap, root);
  43788. heap->finalize_list = hdr;
  43789. }
  43790. #endif /* DUK_USE_FINALIZER_SUPPORT */
  43791. #if defined(DUK_USE_FINALIZER_SUPPORT)
  43792. DUK_INTERNAL void duk_heap_remove_from_finalize_list(duk_heap *heap, duk_heaphdr *hdr) {
  43793. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  43794. duk_heaphdr *next;
  43795. duk_heaphdr *prev;
  43796. next = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  43797. prev = DUK_HEAPHDR_GET_PREV(heap, hdr);
  43798. if (next != NULL) {
  43799. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, next) == hdr);
  43800. DUK_HEAPHDR_SET_PREV(heap, next, prev);
  43801. }
  43802. if (prev == NULL) {
  43803. DUK_ASSERT(hdr == heap->finalize_list);
  43804. heap->finalize_list = next;
  43805. } else {
  43806. DUK_ASSERT(hdr != heap->finalize_list);
  43807. DUK_HEAPHDR_SET_NEXT(heap, prev, next);
  43808. }
  43809. #else
  43810. duk_heaphdr *next;
  43811. duk_heaphdr *curr;
  43812. /* Random removal is expensive: we need to locate the previous element
  43813. * because we don't have a 'prev' pointer.
  43814. */
  43815. curr = heap->finalize_list;
  43816. if (curr == hdr) {
  43817. heap->finalize_list = DUK_HEAPHDR_GET_NEXT(heap, curr);
  43818. } else {
  43819. DUK_ASSERT(hdr != heap->finalize_list);
  43820. for (;;) {
  43821. DUK_ASSERT(curr != NULL); /* Caller responsibility. */
  43822. next = DUK_HEAPHDR_GET_NEXT(heap, curr);
  43823. if (next == hdr) {
  43824. next = DUK_HEAPHDR_GET_NEXT(heap, hdr);
  43825. DUK_HEAPHDR_SET_NEXT(heap, curr, next);
  43826. break;
  43827. }
  43828. }
  43829. }
  43830. #endif
  43831. }
  43832. #endif /* DUK_USE_FINALIZER_SUPPORT */
  43833. #if defined(DUK_USE_ASSERTIONS)
  43834. DUK_INTERNAL duk_bool_t duk_heap_in_heap_allocated(duk_heap *heap, duk_heaphdr *ptr) {
  43835. duk_heaphdr *curr;
  43836. DUK_ASSERT(heap != NULL);
  43837. for (curr = heap->heap_allocated; curr != NULL; curr = DUK_HEAPHDR_GET_NEXT(heap, curr)) {
  43838. if (curr == ptr) {
  43839. return 1;
  43840. }
  43841. }
  43842. return 0;
  43843. }
  43844. #endif /* DUK_USE_ASSERTIONS */
  43845. #if defined(DUK_USE_INTERRUPT_COUNTER)
  43846. DUK_INTERNAL void duk_heap_switch_thread(duk_heap *heap, duk_hthread *new_thr) {
  43847. duk_hthread *curr_thr;
  43848. DUK_ASSERT(heap != NULL);
  43849. if (new_thr != NULL) {
  43850. curr_thr = heap->curr_thread;
  43851. if (curr_thr == NULL) {
  43852. /* For initial entry use default value; zero forces an
  43853. * interrupt before executing the first insturction.
  43854. */
  43855. DUK_DD(DUK_DDPRINT("switch thread, initial entry, init default interrupt counter"));
  43856. new_thr->interrupt_counter = 0;
  43857. new_thr->interrupt_init = 0;
  43858. } else {
  43859. /* Copy interrupt counter/init value state to new thread (if any).
  43860. * It's OK for new_thr to be the same as curr_thr.
  43861. */
  43862. #if defined(DUK_USE_DEBUG)
  43863. if (new_thr != curr_thr) {
  43864. DUK_DD(DUK_DDPRINT("switch thread, not initial entry, copy interrupt counter"));
  43865. }
  43866. #endif
  43867. new_thr->interrupt_counter = curr_thr->interrupt_counter;
  43868. new_thr->interrupt_init = curr_thr->interrupt_init;
  43869. }
  43870. } else {
  43871. DUK_DD(DUK_DDPRINT("switch thread, new thread is NULL, no interrupt counter changes"));
  43872. }
  43873. heap->curr_thread = new_thr; /* may be NULL */
  43874. }
  43875. #endif /* DUK_USE_INTERRUPT_COUNTER */
  43876. #line 1 "duk_heap_refcount.c"
  43877. /*
  43878. * Reference counting implementation.
  43879. *
  43880. * INCREF/DECREF, finalization and freeing of objects whose refcount reaches
  43881. * zero (refzero). These operations are very performance sensitive, so
  43882. * various small tricks are used in an attempt to maximize speed.
  43883. */
  43884. /* #include duk_internal.h -> already included */
  43885. #if defined(DUK_USE_REFERENCE_COUNTING)
  43886. #if !defined(DUK_USE_DOUBLE_LINKED_HEAP)
  43887. #error internal error, reference counting requires a double linked heap
  43888. #endif
  43889. /*
  43890. * Heap object refcount finalization.
  43891. *
  43892. * When an object is about to be freed, all other objects it refers to must
  43893. * be decref'd. Refcount finalization does NOT free the object or its inner
  43894. * allocations (mark-and-sweep shares these helpers), it just manipulates
  43895. * the refcounts.
  43896. *
  43897. * Note that any of the DECREFs may cause a refcount to drop to zero. If so,
  43898. * the object won't be refzero processed inline, but will just be queued to
  43899. * refzero_list and processed by an earlier caller working on refzero_list,
  43900. * eliminating C recursion from even long refzero cascades. If refzero
  43901. * finalization is triggered by mark-and-sweep, refzero conditions are ignored
  43902. * (objects are not even queued to refzero_list) because mark-and-sweep deals
  43903. * with them; refcounts are still updated so that they remain in sync with
  43904. * actual references.
  43905. */
  43906. DUK_LOCAL void duk__decref_tvals_norz(duk_hthread *thr, duk_tval *tv, duk_idx_t count) {
  43907. DUK_ASSERT(count == 0 || tv != NULL);
  43908. while (count-- > 0) {
  43909. DUK_TVAL_DECREF_NORZ(thr, tv);
  43910. tv++;
  43911. }
  43912. }
  43913. DUK_INTERNAL void duk_hobject_refcount_finalize_norz(duk_heap *heap, duk_hobject *h) {
  43914. duk_hthread *thr;
  43915. duk_uint_fast32_t i;
  43916. duk_uint_fast32_t n;
  43917. duk_propvalue *p_val;
  43918. duk_tval *p_tv;
  43919. duk_hstring **p_key;
  43920. duk_uint8_t *p_flag;
  43921. duk_hobject *h_proto;
  43922. DUK_ASSERT(heap != NULL);
  43923. DUK_ASSERT(heap->heap_thread != NULL);
  43924. DUK_ASSERT(h);
  43925. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) h) == DUK_HTYPE_OBJECT);
  43926. thr = heap->heap_thread;
  43927. DUK_ASSERT(thr != NULL);
  43928. p_key = DUK_HOBJECT_E_GET_KEY_BASE(heap, h);
  43929. p_val = DUK_HOBJECT_E_GET_VALUE_BASE(heap, h);
  43930. p_flag = DUK_HOBJECT_E_GET_FLAGS_BASE(heap, h);
  43931. n = DUK_HOBJECT_GET_ENEXT(h);
  43932. while (n-- > 0) {
  43933. duk_hstring *key;
  43934. key = p_key[n];
  43935. if (DUK_UNLIKELY(key == NULL)) {
  43936. continue;
  43937. }
  43938. DUK_HSTRING_DECREF_NORZ(thr, key);
  43939. if (DUK_UNLIKELY(p_flag[n] & DUK_PROPDESC_FLAG_ACCESSOR)) {
  43940. duk_hobject *h_getset;
  43941. h_getset = p_val[n].a.get;
  43942. DUK_ASSERT(h_getset == NULL || DUK_HEAPHDR_IS_OBJECT((duk_heaphdr *) h_getset));
  43943. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, h_getset);
  43944. h_getset = p_val[n].a.set;
  43945. DUK_ASSERT(h_getset == NULL || DUK_HEAPHDR_IS_OBJECT((duk_heaphdr *) h_getset));
  43946. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, h_getset);
  43947. } else {
  43948. duk_tval *tv_val;
  43949. tv_val = &p_val[n].v;
  43950. DUK_TVAL_DECREF_NORZ(thr, tv_val);
  43951. }
  43952. }
  43953. p_tv = DUK_HOBJECT_A_GET_BASE(heap, h);
  43954. n = DUK_HOBJECT_GET_ASIZE(h);
  43955. while (n-- > 0) {
  43956. duk_tval *tv_val;
  43957. tv_val = p_tv + n;
  43958. DUK_TVAL_DECREF_NORZ(thr, tv_val);
  43959. }
  43960. /* Hash part is a 'weak reference' and doesn't contribute to refcounts. */
  43961. h_proto = (duk_hobject *) DUK_HOBJECT_GET_PROTOTYPE(heap, h);
  43962. DUK_ASSERT(h_proto == NULL || DUK_HEAPHDR_IS_OBJECT((duk_heaphdr *) h_proto));
  43963. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, h_proto);
  43964. /* XXX: Object subclass tests are quite awkward at present, ideally
  43965. * we should be able to switch-case here with a dense index (subtype
  43966. * number or something). For now, fast path plain objects and arrays
  43967. * and bit test the rest individually.
  43968. */
  43969. if (DUK_HOBJECT_HAS_FASTREFS(h)) {
  43970. /* Plain object or array, nothing more to do. While a
  43971. * duk_harray has additional fields, none of them need
  43972. * DECREF updates.
  43973. */
  43974. DUK_ASSERT(DUK_HOBJECT_ALLOWS_FASTREFS(h));
  43975. return;
  43976. }
  43977. DUK_ASSERT(DUK_HOBJECT_PROHIBITS_FASTREFS(h));
  43978. /* Slow path: special object, start bit checks from most likely. */
  43979. /* XXX: reorg, more common first */
  43980. if (DUK_HOBJECT_IS_COMPFUNC(h)) {
  43981. duk_hcompfunc *f = (duk_hcompfunc *) h;
  43982. duk_tval *tv, *tv_end;
  43983. duk_hobject **funcs, **funcs_end;
  43984. DUK_ASSERT_HCOMPFUNC_VALID(f);
  43985. if (DUK_LIKELY(DUK_HCOMPFUNC_GET_DATA(heap, f) != NULL)) {
  43986. tv = DUK_HCOMPFUNC_GET_CONSTS_BASE(heap, f);
  43987. tv_end = DUK_HCOMPFUNC_GET_CONSTS_END(heap, f);
  43988. while (tv < tv_end) {
  43989. DUK_TVAL_DECREF_NORZ(thr, tv);
  43990. tv++;
  43991. }
  43992. funcs = DUK_HCOMPFUNC_GET_FUNCS_BASE(heap, f);
  43993. funcs_end = DUK_HCOMPFUNC_GET_FUNCS_END(heap, f);
  43994. while (funcs < funcs_end) {
  43995. duk_hobject *h_func;
  43996. h_func = *funcs;
  43997. DUK_ASSERT(h_func != NULL);
  43998. DUK_ASSERT(DUK_HEAPHDR_IS_OBJECT((duk_heaphdr *) h_func));
  43999. DUK_HCOMPFUNC_DECREF_NORZ(thr, (duk_hcompfunc *) h_func);
  44000. funcs++;
  44001. }
  44002. } else {
  44003. /* May happen in some out-of-memory corner cases. */
  44004. DUK_D(DUK_DPRINT("duk_hcompfunc 'data' is NULL, skipping decref"));
  44005. }
  44006. DUK_HEAPHDR_DECREF_ALLOWNULL(thr, (duk_heaphdr *) DUK_HCOMPFUNC_GET_LEXENV(heap, f));
  44007. DUK_HEAPHDR_DECREF_ALLOWNULL(thr, (duk_heaphdr *) DUK_HCOMPFUNC_GET_VARENV(heap, f));
  44008. DUK_HEAPHDR_DECREF_ALLOWNULL(thr, (duk_hbuffer *) DUK_HCOMPFUNC_GET_DATA(heap, f));
  44009. } else if (DUK_HOBJECT_IS_DECENV(h)) {
  44010. duk_hdecenv *e = (duk_hdecenv *) h;
  44011. DUK_ASSERT_HDECENV_VALID(e);
  44012. DUK_HTHREAD_DECREF_NORZ_ALLOWNULL(thr, e->thread);
  44013. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, e->varmap);
  44014. } else if (DUK_HOBJECT_IS_OBJENV(h)) {
  44015. duk_hobjenv *e = (duk_hobjenv *) h;
  44016. DUK_ASSERT_HOBJENV_VALID(e);
  44017. DUK_ASSERT(e->target != NULL); /* Required for object environments. */
  44018. DUK_HOBJECT_DECREF_NORZ(thr, e->target);
  44019. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  44020. } else if (DUK_HOBJECT_IS_BUFOBJ(h)) {
  44021. duk_hbufobj *b = (duk_hbufobj *) h;
  44022. DUK_ASSERT_HBUFOBJ_VALID(b);
  44023. DUK_HBUFFER_DECREF_NORZ_ALLOWNULL(thr, (duk_hbuffer *) b->buf);
  44024. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, (duk_hobject *) b->buf_prop);
  44025. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  44026. } else if (DUK_HOBJECT_IS_BOUNDFUNC(h)) {
  44027. duk_hboundfunc *f = (duk_hboundfunc *) (void *) h;
  44028. DUK_ASSERT_HBOUNDFUNC_VALID(f);
  44029. DUK_TVAL_DECREF_NORZ(thr, &f->target);
  44030. DUK_TVAL_DECREF_NORZ(thr, &f->this_binding);
  44031. duk__decref_tvals_norz(thr, f->args, f->nargs);
  44032. #if defined(DUK_USE_ES6_PROXY)
  44033. } else if (DUK_HOBJECT_IS_PROXY(h)) {
  44034. duk_hproxy *p = (duk_hproxy *) h;
  44035. DUK_ASSERT_HPROXY_VALID(p);
  44036. DUK_HOBJECT_DECREF_NORZ(thr, p->target);
  44037. DUK_HOBJECT_DECREF_NORZ(thr, p->handler);
  44038. #endif /* DUK_USE_ES6_PROXY */
  44039. } else if (DUK_HOBJECT_IS_THREAD(h)) {
  44040. duk_hthread *t = (duk_hthread *) h;
  44041. duk_activation *act;
  44042. duk_tval *tv;
  44043. DUK_ASSERT_HTHREAD_VALID(t);
  44044. tv = t->valstack;
  44045. while (tv < t->valstack_top) {
  44046. DUK_TVAL_DECREF_NORZ(thr, tv);
  44047. tv++;
  44048. }
  44049. for (act = t->callstack_curr; act != NULL; act = act->parent) {
  44050. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, (duk_hobject *) DUK_ACT_GET_FUNC(act));
  44051. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, (duk_hobject *) act->var_env);
  44052. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, (duk_hobject *) act->lex_env);
  44053. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  44054. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, (duk_hobject *) act->prev_caller);
  44055. #endif
  44056. #if 0 /* nothing now */
  44057. for (cat = act->cat; cat != NULL; cat = cat->parent) {
  44058. }
  44059. #endif
  44060. }
  44061. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  44062. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, (duk_hobject *) t->builtins[i]);
  44063. }
  44064. DUK_HTHREAD_DECREF_NORZ_ALLOWNULL(thr, (duk_hthread *) t->resumer);
  44065. } else {
  44066. /* We may come here if the object should have a FASTREFS flag
  44067. * but it's missing for some reason. Assert for never getting
  44068. * here; however, other than performance, this is harmless.
  44069. */
  44070. DUK_D(DUK_DPRINT("missing FASTREFS flag for: %!iO", h));
  44071. DUK_ASSERT(0);
  44072. }
  44073. }
  44074. DUK_INTERNAL void duk_heaphdr_refcount_finalize_norz(duk_heap *heap, duk_heaphdr *hdr) {
  44075. DUK_ASSERT(heap != NULL);
  44076. DUK_ASSERT(heap->heap_thread != NULL);
  44077. DUK_ASSERT(hdr != NULL);
  44078. if (DUK_HEAPHDR_IS_OBJECT(hdr)) {
  44079. duk_hobject_refcount_finalize_norz(heap, (duk_hobject *) hdr);
  44080. }
  44081. /* DUK_HTYPE_BUFFER: nothing to finalize */
  44082. /* DUK_HTYPE_STRING: nothing to finalize */
  44083. }
  44084. /*
  44085. * Refzero processing for duk_hobject: queue a refzero'ed object to either
  44086. * finalize_list or refzero_list and process the relevent list(s) if
  44087. * necessary.
  44088. *
  44089. * Refzero_list is single linked, with only 'prev' pointers set and valid.
  44090. * All 'next' pointers are intentionally left as garbage. This doesn't
  44091. * matter because refzero_list is processed to completion before any other
  44092. * code (like mark-and-sweep) might walk the list.
  44093. *
  44094. * In more detail:
  44095. *
  44096. * - On first insert refzero_list is NULL and the new object becomes the
  44097. * first and only element on the list; duk__refcount_free_pending() is
  44098. * called and it starts processing the list from the initial element,
  44099. * i.e. the list tail.
  44100. *
  44101. * - As each object is refcount finalized, new objects may be queued to
  44102. * refzero_list head. Their 'next' pointers are left as garbage, but
  44103. * 'prev' points are set correctly, with the element at refzero_list
  44104. * having a NULL 'prev' pointer. The fact that refzero_list is non-NULL
  44105. * is used to reject (1) recursive duk__refcount_free_pending() and
  44106. * (2) finalize_list processing calls.
  44107. *
  44108. * - When we're done with the current object, read its 'prev' pointer and
  44109. * free the object. If 'prev' is NULL, we've reached head of list and are
  44110. * done: set refzero_list to NULL and process pending finalizers. Otherwise
  44111. * continue processing the list.
  44112. *
  44113. * A refzero cascade is free of side effects because it only involves
  44114. * queueing more objects and freeing memory; finalizer execution is blocked
  44115. * in the code path queueing objects to finalize_list. As a result the
  44116. * initial refzero call (which triggers duk__refcount_free_pending()) must
  44117. * check finalize_list so that finalizers are executed snappily.
  44118. *
  44119. * If finalize_list processing starts first, refzero may occur while we're
  44120. * processing finalizers. That's fine: that particular refzero cascade is
  44121. * handled to completion without side effects. Once the cascade is complete,
  44122. * we'll run pending finalizers but notice that we're already doing that and
  44123. * return.
  44124. *
  44125. * This could be expanded to allow incremental freeing: just bail out
  44126. * early and resume at a future alloc/decref/refzero. However, if that
  44127. * were done, the list structure would need to be kept consistent at all
  44128. * times, mark-and-sweep would need to handle refzero_list, etc.
  44129. */
  44130. DUK_LOCAL void duk__refcount_free_pending(duk_heap *heap) {
  44131. duk_heaphdr *curr;
  44132. #if defined(DUK_USE_DEBUG)
  44133. duk_int_t count = 0;
  44134. #endif
  44135. DUK_ASSERT(heap != NULL);
  44136. curr = heap->refzero_list;
  44137. DUK_ASSERT(curr != NULL);
  44138. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, curr) == NULL); /* We're called on initial insert only. */
  44139. /* curr->next is GARBAGE. */
  44140. do {
  44141. duk_heaphdr *prev;
  44142. DUK_DDD(DUK_DDDPRINT("refzero processing %p: %!O", (void *) curr, (duk_heaphdr *) curr));
  44143. #if defined(DUK_USE_DEBUG)
  44144. count++;
  44145. #endif
  44146. DUK_ASSERT(curr != NULL);
  44147. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(curr) == DUK_HTYPE_OBJECT); /* currently, always the case */
  44148. /* FINALIZED may be set; don't care about flags here. */
  44149. /* Refcount finalize 'curr'. Refzero_list must be non-NULL
  44150. * here to prevent recursive entry to duk__refcount_free_pending().
  44151. */
  44152. DUK_ASSERT(heap->refzero_list != NULL);
  44153. duk_hobject_refcount_finalize_norz(heap, (duk_hobject *) curr);
  44154. prev = DUK_HEAPHDR_GET_PREV(heap, curr);
  44155. DUK_ASSERT((prev == NULL && heap->refzero_list == curr) || \
  44156. (prev != NULL && heap->refzero_list != curr));
  44157. /* prev->next is intentionally not updated and is garbage. */
  44158. duk_free_hobject(heap, (duk_hobject *) curr); /* Invalidates 'curr'. */
  44159. curr = prev;
  44160. } while (curr != NULL);
  44161. heap->refzero_list = NULL;
  44162. DUK_DD(DUK_DDPRINT("refzero processed %ld objects", (long) count));
  44163. }
  44164. DUK_LOCAL DUK_INLINE void duk__refcount_refzero_hobject(duk_heap *heap, duk_hobject *obj, duk_bool_t skip_free_pending) {
  44165. duk_heaphdr *hdr;
  44166. duk_heaphdr *root;
  44167. DUK_ASSERT(heap != NULL);
  44168. DUK_ASSERT(heap->heap_thread != NULL);
  44169. DUK_ASSERT(obj != NULL);
  44170. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) obj) == DUK_HTYPE_OBJECT);
  44171. hdr = (duk_heaphdr *) obj;
  44172. /* Refzero'd objects must be in heap_allocated. They can't be in
  44173. * finalize_list because all objects on finalize_list have an
  44174. * artificial +1 refcount bump.
  44175. */
  44176. #if defined(DUK_USE_ASSERTIONS)
  44177. DUK_ASSERT(duk_heap_in_heap_allocated(heap, (duk_heaphdr *) obj));
  44178. #endif
  44179. DUK_HEAP_REMOVE_FROM_HEAP_ALLOCATED(heap, hdr);
  44180. #if defined(DUK_USE_FINALIZER_SUPPORT)
  44181. /* This finalizer check MUST BE side effect free. It should also be
  44182. * as fast as possible because it's applied to every object freed.
  44183. */
  44184. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_FINALIZER_FAST(heap, (duk_hobject *) hdr) != 0U)) {
  44185. /* Special case: FINALIZED may be set if mark-and-sweep queued
  44186. * object for finalization, the finalizer was executed (and
  44187. * FINALIZED set), mark-and-sweep hasn't yet processed the
  44188. * object again, but its refcount drops to zero. Free without
  44189. * running the finalizer again.
  44190. */
  44191. if (DUK_HEAPHDR_HAS_FINALIZED(hdr)) {
  44192. DUK_D(DUK_DPRINT("refzero'd object has finalizer and FINALIZED is set -> free"));
  44193. } else {
  44194. /* Set FINALIZABLE flag so that all objects on finalize_list
  44195. * will have it set and are thus detectable based on the
  44196. * flag alone.
  44197. */
  44198. DUK_HEAPHDR_SET_FINALIZABLE(hdr);
  44199. DUK_ASSERT(!DUK_HEAPHDR_HAS_FINALIZED(hdr));
  44200. #if defined(DUK_USE_REFERENCE_COUNTING)
  44201. /* Bump refcount on finalize_list insert so that a
  44202. * refzero can never occur when an object is waiting
  44203. * for its finalizer call. Refzero might otherwise
  44204. * now happen because we allow duk_push_heapptr() for
  44205. * objects pending finalization.
  44206. */
  44207. DUK_HEAPHDR_PREINC_REFCOUNT(hdr);
  44208. #endif
  44209. DUK_HEAP_INSERT_INTO_FINALIZE_LIST(heap, hdr);
  44210. /* Process finalizers unless skipping is explicitly
  44211. * requested (NORZ) or refzero_list is being processed
  44212. * (avoids side effects during a refzero cascade).
  44213. * If refzero_list is processed, the initial refzero
  44214. * call will run pending finalizers when refzero_list
  44215. * is done.
  44216. */
  44217. if (!skip_free_pending && heap->refzero_list == NULL) {
  44218. duk_heap_process_finalize_list(heap);
  44219. }
  44220. return;
  44221. }
  44222. }
  44223. #endif /* DUK_USE_FINALIZER_SUPPORT */
  44224. /* No need to finalize, free object via refzero_list. */
  44225. root = heap->refzero_list;
  44226. DUK_HEAPHDR_SET_PREV(heap, hdr, NULL);
  44227. /* 'next' is left as GARBAGE. */
  44228. heap->refzero_list = hdr;
  44229. if (root == NULL) {
  44230. /* Object is now queued. Refzero_list was NULL so
  44231. * no-one is currently processing it; do it here.
  44232. * With refzero processing just doing a cascade of
  44233. * free calls, we can process it directly even when
  44234. * NORZ macros are used: there are no side effects.
  44235. */
  44236. duk__refcount_free_pending(heap);
  44237. DUK_ASSERT(heap->refzero_list == NULL);
  44238. /* Process finalizers only after the entire cascade
  44239. * is finished. In most cases there's nothing to
  44240. * finalize, so fast path check to avoid a call.
  44241. */
  44242. #if defined(DUK_USE_FINALIZER_SUPPORT)
  44243. if (!skip_free_pending && DUK_UNLIKELY(heap->finalize_list != NULL)) {
  44244. duk_heap_process_finalize_list(heap);
  44245. }
  44246. #endif
  44247. } else {
  44248. DUK_ASSERT(DUK_HEAPHDR_GET_PREV(heap, root) == NULL);
  44249. DUK_HEAPHDR_SET_PREV(heap, root, hdr);
  44250. /* Object is now queued. Because refzero_list was
  44251. * non-NULL, it's already being processed by someone
  44252. * in the C call stack, so we're done.
  44253. */
  44254. }
  44255. }
  44256. #if defined(DUK_USE_FINALIZER_SUPPORT)
  44257. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_refzero_check_fast(duk_hthread *thr) {
  44258. DUK_ASSERT(thr != NULL);
  44259. DUK_ASSERT(thr->heap != NULL);
  44260. DUK_ASSERT(thr->heap->refzero_list == NULL); /* Processed to completion inline. */
  44261. if (DUK_UNLIKELY(thr->heap->finalize_list != NULL)) {
  44262. duk_heap_process_finalize_list(thr->heap);
  44263. }
  44264. }
  44265. DUK_INTERNAL void duk_refzero_check_slow(duk_hthread *thr) {
  44266. DUK_ASSERT(thr != NULL);
  44267. DUK_ASSERT(thr->heap != NULL);
  44268. DUK_ASSERT(thr->heap->refzero_list == NULL); /* Processed to completion inline. */
  44269. if (DUK_UNLIKELY(thr->heap->finalize_list != NULL)) {
  44270. duk_heap_process_finalize_list(thr->heap);
  44271. }
  44272. }
  44273. #endif /* DUK_USE_FINALIZER_SUPPORT */
  44274. /*
  44275. * Refzero processing for duk_hstring.
  44276. */
  44277. DUK_LOCAL DUK_INLINE void duk__refcount_refzero_hstring(duk_heap *heap, duk_hstring *str) {
  44278. DUK_ASSERT(heap != NULL);
  44279. DUK_ASSERT(heap->heap_thread != NULL);
  44280. DUK_ASSERT(str != NULL);
  44281. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) str) == DUK_HTYPE_STRING);
  44282. duk_heap_strcache_string_remove(heap, str);
  44283. duk_heap_strtable_unlink(heap, str);
  44284. duk_free_hstring(heap, str);
  44285. }
  44286. /*
  44287. * Refzero processing for duk_hbuffer.
  44288. */
  44289. DUK_LOCAL DUK_INLINE void duk__refcount_refzero_hbuffer(duk_heap *heap, duk_hbuffer *buf) {
  44290. DUK_ASSERT(heap != NULL);
  44291. DUK_ASSERT(heap->heap_thread != NULL);
  44292. DUK_ASSERT(buf != NULL);
  44293. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE((duk_heaphdr *) buf) == DUK_HTYPE_BUFFER);
  44294. DUK_HEAP_REMOVE_FROM_HEAP_ALLOCATED(heap, (duk_heaphdr *) buf);
  44295. duk_free_hbuffer(heap, buf);
  44296. }
  44297. /*
  44298. * Incref and decref functions.
  44299. *
  44300. * Decref may trigger immediate refzero handling, which may free and finalize
  44301. * an arbitrary number of objects (a "DECREF cascade").
  44302. *
  44303. * Refzero handling is skipped entirely if (1) mark-and-sweep is running or
  44304. * (2) execution is paused in the debugger. The objects are left in the heap,
  44305. * and will be freed by mark-and-sweep or eventual heap destruction.
  44306. *
  44307. * This is necessary during mark-and-sweep because refcounts are also updated
  44308. * during the sweep phase (otherwise objects referenced by a swept object
  44309. * would have incorrect refcounts) which then calls here. This could be
  44310. * avoided by using separate decref macros in mark-and-sweep; however,
  44311. * mark-and-sweep also calls finalizers which would use the ordinary decref
  44312. * macros anyway.
  44313. *
  44314. * We can't process refzeros (= free objects) when the debugger is running
  44315. * as the debugger might make an object unreachable but still continue
  44316. * inspecting it (or even cause it to be pushed back). So we must rely on
  44317. * mark-and-sweep to collect them.
  44318. *
  44319. * The DUK__RZ_SUPPRESS_CHECK() condition is also used in heap destruction
  44320. * when running finalizers for remaining objects: the flag prevents objects
  44321. * from being moved around in heap linked lists while that's being done.
  44322. *
  44323. * The suppress condition is important to performance.
  44324. */
  44325. #define DUK__RZ_SUPPRESS_ASSERT1() do { \
  44326. DUK_ASSERT(thr != NULL); \
  44327. DUK_ASSERT(thr->heap != NULL); \
  44328. /* When mark-and-sweep runs, heap_thread must exist. */ \
  44329. DUK_ASSERT(thr->heap->ms_running == 0 || thr->heap->heap_thread != NULL); \
  44330. /* When mark-and-sweep runs, the 'thr' argument always matches heap_thread. \
  44331. * This could be used to e.g. suppress check against 'thr' directly (and \
  44332. * knowing it would be heap_thread); not really used now. \
  44333. */ \
  44334. DUK_ASSERT(thr->heap->ms_running == 0 || thr == thr->heap->heap_thread); \
  44335. /* We may be called when the heap is initializing and we process \
  44336. * refzeros normally, but mark-and-sweep and finalizers are prevented \
  44337. * if that's the case. \
  44338. */ \
  44339. DUK_ASSERT(thr->heap->heap_initializing == 0 || thr->heap->ms_prevent_count > 0); \
  44340. DUK_ASSERT(thr->heap->heap_initializing == 0 || thr->heap->pf_prevent_count > 0); \
  44341. } while (0)
  44342. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  44343. #define DUK__RZ_SUPPRESS_ASSERT2() do { \
  44344. /* When debugger is paused, ms_running is set. */ \
  44345. DUK_ASSERT(!DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap) || thr->heap->ms_running != 0); \
  44346. } while (0)
  44347. #define DUK__RZ_SUPPRESS_COND() (heap->ms_running != 0)
  44348. #else
  44349. #define DUK__RZ_SUPPRESS_ASSERT2() do { } while (0)
  44350. #define DUK__RZ_SUPPRESS_COND() (heap->ms_running != 0)
  44351. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  44352. #define DUK__RZ_SUPPRESS_CHECK() do { \
  44353. DUK__RZ_SUPPRESS_ASSERT1(); \
  44354. DUK__RZ_SUPPRESS_ASSERT2(); \
  44355. if (DUK_UNLIKELY(DUK__RZ_SUPPRESS_COND())) { \
  44356. DUK_DDD(DUK_DDDPRINT("refzero handling suppressed (not even queued) when mark-and-sweep running, object: %p", (void *) h)); \
  44357. return; \
  44358. } \
  44359. } while (0)
  44360. #define DUK__RZ_STRING() do { \
  44361. duk__refcount_refzero_hstring(heap, (duk_hstring *) h); \
  44362. } while (0)
  44363. #define DUK__RZ_BUFFER() do { \
  44364. duk__refcount_refzero_hbuffer(heap, (duk_hbuffer *) h); \
  44365. } while (0)
  44366. #define DUK__RZ_OBJECT() do { \
  44367. duk__refcount_refzero_hobject(heap, (duk_hobject *) h, skip_free_pending); \
  44368. } while (0)
  44369. /* XXX: test the effect of inlining here vs. NOINLINE in refzero helpers */
  44370. #if defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  44371. #define DUK__RZ_INLINE DUK_ALWAYS_INLINE
  44372. #else
  44373. #define DUK__RZ_INLINE /*nop*/
  44374. #endif
  44375. DUK_LOCAL DUK__RZ_INLINE void duk__hstring_refzero_helper(duk_hthread *thr, duk_hstring *h) {
  44376. duk_heap *heap;
  44377. DUK_ASSERT(thr != NULL);
  44378. DUK_ASSERT(h != NULL);
  44379. heap = thr->heap;
  44380. DUK__RZ_SUPPRESS_CHECK();
  44381. DUK__RZ_STRING();
  44382. }
  44383. DUK_LOCAL DUK__RZ_INLINE void duk__hbuffer_refzero_helper(duk_hthread *thr, duk_hbuffer *h) {
  44384. duk_heap *heap;
  44385. DUK_ASSERT(thr != NULL);
  44386. DUK_ASSERT(h != NULL);
  44387. heap = thr->heap;
  44388. DUK__RZ_SUPPRESS_CHECK();
  44389. DUK__RZ_BUFFER();
  44390. }
  44391. DUK_LOCAL DUK__RZ_INLINE void duk__hobject_refzero_helper(duk_hthread *thr, duk_hobject *h, duk_bool_t skip_free_pending) {
  44392. duk_heap *heap;
  44393. DUK_ASSERT(thr != NULL);
  44394. DUK_ASSERT(h != NULL);
  44395. heap = thr->heap;
  44396. DUK__RZ_SUPPRESS_CHECK();
  44397. DUK__RZ_OBJECT();
  44398. }
  44399. DUK_LOCAL DUK__RZ_INLINE void duk__heaphdr_refzero_helper(duk_hthread *thr, duk_heaphdr *h, duk_bool_t skip_free_pending) {
  44400. duk_heap *heap;
  44401. duk_small_uint_t htype;
  44402. DUK_ASSERT(thr != NULL);
  44403. DUK_ASSERT(h != NULL);
  44404. heap = thr->heap;
  44405. htype = (duk_small_uint_t) DUK_HEAPHDR_GET_TYPE(h);
  44406. DUK__RZ_SUPPRESS_CHECK();
  44407. switch (htype) {
  44408. case DUK_HTYPE_STRING:
  44409. /* Strings have no internal references but do have "weak"
  44410. * references in the string cache. Also note that strings
  44411. * are not on the heap_allocated list like other heap
  44412. * elements.
  44413. */
  44414. DUK__RZ_STRING();
  44415. break;
  44416. case DUK_HTYPE_OBJECT:
  44417. /* Objects have internal references. Must finalize through
  44418. * the "refzero" work list.
  44419. */
  44420. DUK__RZ_OBJECT();
  44421. break;
  44422. default:
  44423. /* Buffers have no internal references. However, a dynamic
  44424. * buffer has a separate allocation for the buffer. This is
  44425. * freed by duk_heap_free_heaphdr_raw().
  44426. */
  44427. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(h) == DUK_HTYPE_BUFFER);
  44428. DUK__RZ_BUFFER();
  44429. break;
  44430. }
  44431. }
  44432. DUK_INTERNAL DUK_NOINLINE void duk_heaphdr_refzero(duk_hthread *thr, duk_heaphdr *h) {
  44433. duk__heaphdr_refzero_helper(thr, h, 0 /*skip_free_pending*/);
  44434. }
  44435. DUK_INTERNAL DUK_NOINLINE void duk_heaphdr_refzero_norz(duk_hthread *thr, duk_heaphdr *h) {
  44436. duk__heaphdr_refzero_helper(thr, h, 1 /*skip_free_pending*/);
  44437. }
  44438. DUK_INTERNAL DUK_NOINLINE void duk_hstring_refzero(duk_hthread *thr, duk_hstring *h) {
  44439. duk__hstring_refzero_helper(thr, h);
  44440. }
  44441. DUK_INTERNAL DUK_NOINLINE void duk_hbuffer_refzero(duk_hthread *thr, duk_hbuffer *h) {
  44442. duk__hbuffer_refzero_helper(thr, h);
  44443. }
  44444. DUK_INTERNAL DUK_NOINLINE void duk_hobject_refzero(duk_hthread *thr, duk_hobject *h) {
  44445. duk__hobject_refzero_helper(thr, h, 0 /*skip_free_pending*/);
  44446. }
  44447. DUK_INTERNAL DUK_NOINLINE void duk_hobject_refzero_norz(duk_hthread *thr, duk_hobject *h) {
  44448. duk__hobject_refzero_helper(thr, h, 1 /*skip_free_pending*/);
  44449. }
  44450. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  44451. DUK_INTERNAL void duk_tval_incref(duk_tval *tv) {
  44452. DUK_ASSERT(tv != NULL);
  44453. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv)) {
  44454. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  44455. DUK_ASSERT(h != NULL);
  44456. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  44457. DUK_ASSERT_DISABLE(h->h_refcount >= 0);
  44458. DUK_HEAPHDR_PREINC_REFCOUNT(h);
  44459. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) != 0); /* No wrapping. */
  44460. }
  44461. }
  44462. DUK_INTERNAL void duk_tval_decref(duk_hthread *thr, duk_tval *tv) {
  44463. DUK_ASSERT(thr != NULL);
  44464. DUK_ASSERT(tv != NULL);
  44465. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv)) {
  44466. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  44467. DUK_ASSERT(h != NULL);
  44468. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  44469. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) >= 1);
  44470. #if 0
  44471. if (DUK_HEAPHDR_PREDEC_REFCOUNT(h) != 0) {
  44472. return;
  44473. }
  44474. duk_heaphdr_refzero(thr, h);
  44475. #else
  44476. duk_heaphdr_decref(thr, h);
  44477. #endif
  44478. }
  44479. }
  44480. DUK_INTERNAL void duk_tval_decref_norz(duk_hthread *thr, duk_tval *tv) {
  44481. DUK_ASSERT(thr != NULL);
  44482. DUK_ASSERT(tv != NULL);
  44483. if (DUK_TVAL_NEEDS_REFCOUNT_UPDATE(tv)) {
  44484. duk_heaphdr *h = DUK_TVAL_GET_HEAPHDR(tv);
  44485. DUK_ASSERT(h != NULL);
  44486. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  44487. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT(h) >= 1);
  44488. #if 0
  44489. if (DUK_HEAPHDR_PREDEC_REFCOUNT(h) != 0) {
  44490. return;
  44491. }
  44492. duk_heaphdr_refzero_norz(thr, h);
  44493. #else
  44494. duk_heaphdr_decref_norz(thr, h);
  44495. #endif
  44496. }
  44497. }
  44498. #endif /* !DUK_USE_FAST_REFCOUNT_DEFAULT */
  44499. #define DUK__DECREF_ASSERTS() do { \
  44500. DUK_ASSERT(thr != NULL); \
  44501. DUK_ASSERT(thr->heap != NULL); \
  44502. DUK_ASSERT(h != NULL); \
  44503. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID((duk_heaphdr *) h)); \
  44504. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) >= 1); \
  44505. } while (0)
  44506. #if defined(DUK_USE_ROM_OBJECTS)
  44507. #define DUK__INCREF_SHARED() do { \
  44508. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h)) { \
  44509. return; \
  44510. } \
  44511. DUK_HEAPHDR_PREINC_REFCOUNT((duk_heaphdr *) h); \
  44512. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) != 0); /* No wrapping. */ \
  44513. } while (0)
  44514. #define DUK__DECREF_SHARED() do { \
  44515. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h)) { \
  44516. return; \
  44517. } \
  44518. if (DUK_HEAPHDR_PREDEC_REFCOUNT((duk_heaphdr *) h) != 0) { \
  44519. return; \
  44520. } \
  44521. } while (0)
  44522. #else
  44523. #define DUK__INCREF_SHARED() do { \
  44524. DUK_HEAPHDR_PREINC_REFCOUNT((duk_heaphdr *) h); \
  44525. DUK_ASSERT(DUK_HEAPHDR_GET_REFCOUNT((duk_heaphdr *) h) != 0); /* No wrapping. */ \
  44526. } while (0)
  44527. #define DUK__DECREF_SHARED() do { \
  44528. if (DUK_HEAPHDR_PREDEC_REFCOUNT((duk_heaphdr *) h) != 0) { \
  44529. return; \
  44530. } \
  44531. } while (0)
  44532. #endif
  44533. #if !defined(DUK_USE_FAST_REFCOUNT_DEFAULT)
  44534. /* This will in practice be inlined because it's just an INC instructions
  44535. * and a bit test + INC when ROM objects are enabled.
  44536. */
  44537. DUK_INTERNAL void duk_heaphdr_incref(duk_heaphdr *h) {
  44538. DUK_ASSERT(h != NULL);
  44539. DUK_ASSERT(DUK_HEAPHDR_HTYPE_VALID(h));
  44540. DUK_ASSERT_DISABLE(DUK_HEAPHDR_GET_REFCOUNT(h) >= 0);
  44541. DUK__INCREF_SHARED();
  44542. }
  44543. DUK_INTERNAL void duk_heaphdr_decref(duk_hthread *thr, duk_heaphdr *h) {
  44544. DUK__DECREF_ASSERTS();
  44545. DUK__DECREF_SHARED();
  44546. duk_heaphdr_refzero(thr, h);
  44547. /* Forced mark-and-sweep when GC torture enabled; this could happen
  44548. * on any DECREF (but not DECREF_NORZ).
  44549. */
  44550. DUK_GC_TORTURE(thr->heap);
  44551. }
  44552. DUK_INTERNAL void duk_heaphdr_decref_norz(duk_hthread *thr, duk_heaphdr *h) {
  44553. DUK__DECREF_ASSERTS();
  44554. DUK__DECREF_SHARED();
  44555. duk_heaphdr_refzero_norz(thr, h);
  44556. }
  44557. #endif /* !DUK_USE_FAST_REFCOUNT_DEFAULT */
  44558. #if 0 /* Not needed. */
  44559. DUK_INTERNAL void duk_hstring_decref(duk_hthread *thr, duk_hstring *h) {
  44560. DUK__DECREF_ASSERTS();
  44561. DUK__DECREF_SHARED();
  44562. duk_hstring_refzero(thr, h);
  44563. }
  44564. DUK_INTERNAL void duk_hstring_decref_norz(duk_hthread *thr, duk_hstring *h) {
  44565. DUK__DECREF_ASSERTS();
  44566. DUK__DECREF_SHARED();
  44567. duk_hstring_refzero_norz(thr, h);
  44568. }
  44569. DUK_INTERNAL void duk_hbuffer_decref(duk_hthread *thr, duk_hbuffer *h) {
  44570. DUK__DECREF_ASSERTS();
  44571. DUK__DECREF_SHARED();
  44572. duk_hbuffer_refzero(thr, h);
  44573. }
  44574. DUK_INTERNAL void duk_hbuffer_decref_norz(duk_hthread *thr, duk_hbuffer *h) {
  44575. DUK__DECREF_ASSERTS();
  44576. DUK__DECREF_SHARED();
  44577. duk_hbuffer_refzero_norz(thr, h);
  44578. }
  44579. DUK_INTERNAL void duk_hobject_decref(duk_hthread *thr, duk_hobject *h) {
  44580. DUK__DECREF_ASSERTS();
  44581. DUK__DECREF_SHARED();
  44582. duk_hobject_refzero(thr, h);
  44583. }
  44584. DUK_INTERNAL void duk_hobject_decref_norz(duk_hthread *thr, duk_hobject *h) {
  44585. DUK__DECREF_ASSERTS();
  44586. DUK__DECREF_SHARED();
  44587. duk_hobject_refzero_norz(thr, h);
  44588. }
  44589. #endif
  44590. #else /* DUK_USE_REFERENCE_COUNTING */
  44591. /* no refcounting */
  44592. #endif /* DUK_USE_REFERENCE_COUNTING */
  44593. /* automatic undefs */
  44594. #undef DUK__DECREF_ASSERTS
  44595. #undef DUK__DECREF_SHARED
  44596. #undef DUK__INCREF_SHARED
  44597. #undef DUK__RZ_BUFFER
  44598. #undef DUK__RZ_INLINE
  44599. #undef DUK__RZ_OBJECT
  44600. #undef DUK__RZ_STRING
  44601. #undef DUK__RZ_SUPPRESS_ASSERT1
  44602. #undef DUK__RZ_SUPPRESS_ASSERT2
  44603. #undef DUK__RZ_SUPPRESS_CHECK
  44604. #undef DUK__RZ_SUPPRESS_COND
  44605. #line 1 "duk_heap_stringcache.c"
  44606. /*
  44607. * String cache.
  44608. *
  44609. * Provides a cache to optimize indexed string lookups. The cache keeps
  44610. * track of (byte offset, char offset) states for a fixed number of strings.
  44611. * Otherwise we'd need to scan from either end of the string, as we store
  44612. * strings in (extended) UTF-8.
  44613. */
  44614. /* #include duk_internal.h -> already included */
  44615. /*
  44616. * Delete references to given hstring from the heap string cache.
  44617. *
  44618. * String cache references are 'weak': they are not counted towards
  44619. * reference counts, nor serve as roots for mark-and-sweep. When an
  44620. * object is about to be freed, such references need to be removed.
  44621. */
  44622. DUK_INTERNAL void duk_heap_strcache_string_remove(duk_heap *heap, duk_hstring *h) {
  44623. duk_uint_t i;
  44624. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  44625. duk_strcache_entry *c = heap->strcache + i;
  44626. if (c->h == h) {
  44627. DUK_DD(DUK_DDPRINT("deleting weak strcache reference to hstring %p from heap %p",
  44628. (void *) h, (void *) heap));
  44629. c->h = NULL;
  44630. /* XXX: the string shouldn't appear twice, but we now loop to the
  44631. * end anyway; if fixed, add a looping assertion to ensure there
  44632. * is no duplicate.
  44633. */
  44634. }
  44635. }
  44636. }
  44637. /*
  44638. * String scanning helpers
  44639. *
  44640. * All bytes other than UTF-8 continuation bytes ([0x80,0xbf]) are
  44641. * considered to contribute a character. This must match how string
  44642. * character length is computed.
  44643. */
  44644. DUK_LOCAL const duk_uint8_t *duk__scan_forwards(const duk_uint8_t *p, const duk_uint8_t *q, duk_uint_fast32_t n) {
  44645. while (n > 0) {
  44646. for (;;) {
  44647. p++;
  44648. if (p >= q) {
  44649. return NULL;
  44650. }
  44651. if ((*p & 0xc0) != 0x80) {
  44652. break;
  44653. }
  44654. }
  44655. n--;
  44656. }
  44657. return p;
  44658. }
  44659. DUK_LOCAL const duk_uint8_t *duk__scan_backwards(const duk_uint8_t *p, const duk_uint8_t *q, duk_uint_fast32_t n) {
  44660. while (n > 0) {
  44661. for (;;) {
  44662. p--;
  44663. if (p < q) {
  44664. return NULL;
  44665. }
  44666. if ((*p & 0xc0) != 0x80) {
  44667. break;
  44668. }
  44669. }
  44670. n--;
  44671. }
  44672. return p;
  44673. }
  44674. /*
  44675. * Convert char offset to byte offset
  44676. *
  44677. * Avoid using the string cache if possible: for ASCII strings byte and
  44678. * char offsets are equal and for short strings direct scanning may be
  44679. * better than using the string cache (which may evict a more important
  44680. * entry).
  44681. *
  44682. * Typing now assumes 32-bit string byte/char offsets (duk_uint_fast32_t).
  44683. * Better typing might be to use duk_size_t.
  44684. *
  44685. * Caller should ensure 'char_offset' is within the string bounds [0,charlen]
  44686. * (endpoint is inclusive). If this is not the case, no memory unsafe
  44687. * behavior will happen but an error will be thrown.
  44688. */
  44689. DUK_INTERNAL duk_uint_fast32_t duk_heap_strcache_offset_char2byte(duk_hthread *thr, duk_hstring *h, duk_uint_fast32_t char_offset) {
  44690. duk_heap *heap;
  44691. duk_strcache_entry *sce;
  44692. duk_uint_fast32_t byte_offset;
  44693. duk_uint_t i;
  44694. duk_bool_t use_cache;
  44695. duk_uint_fast32_t dist_start, dist_end, dist_sce;
  44696. duk_uint_fast32_t char_length;
  44697. const duk_uint8_t *p_start;
  44698. const duk_uint8_t *p_end;
  44699. const duk_uint8_t *p_found;
  44700. /*
  44701. * For ASCII strings, the answer is simple.
  44702. */
  44703. if (DUK_LIKELY(DUK_HSTRING_IS_ASCII(h))) {
  44704. return char_offset;
  44705. }
  44706. char_length = (duk_uint_fast32_t) DUK_HSTRING_GET_CHARLEN(h);
  44707. DUK_ASSERT(char_offset <= char_length);
  44708. if (DUK_LIKELY(DUK_HSTRING_IS_ASCII(h))) {
  44709. /* Must recheck because the 'is ascii' flag may be set
  44710. * lazily. Alternatively, we could just compare charlen
  44711. * to bytelen.
  44712. */
  44713. return char_offset;
  44714. }
  44715. /*
  44716. * For non-ASCII strings, we need to scan forwards or backwards
  44717. * from some starting point. The starting point may be the start
  44718. * or end of the string, or some cached midpoint in the string
  44719. * cache.
  44720. *
  44721. * For "short" strings we simply scan without checking or updating
  44722. * the cache. For longer strings we check and update the cache as
  44723. * necessary, inserting a new cache entry if none exists.
  44724. */
  44725. DUK_DDD(DUK_DDDPRINT("non-ascii string %p, char_offset=%ld, clen=%ld, blen=%ld",
  44726. (void *) h, (long) char_offset,
  44727. (long) DUK_HSTRING_GET_CHARLEN(h),
  44728. (long) DUK_HSTRING_GET_BYTELEN(h)));
  44729. heap = thr->heap;
  44730. sce = NULL;
  44731. use_cache = (char_length > DUK_HEAP_STRINGCACHE_NOCACHE_LIMIT);
  44732. if (use_cache) {
  44733. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  44734. DUK_DDD(DUK_DDDPRINT("stringcache before char2byte (using cache):"));
  44735. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  44736. duk_strcache_entry *c = heap->strcache + i;
  44737. DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
  44738. (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
  44739. }
  44740. #endif
  44741. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  44742. duk_strcache_entry *c = heap->strcache + i;
  44743. if (c->h == h) {
  44744. sce = c;
  44745. break;
  44746. }
  44747. }
  44748. }
  44749. /*
  44750. * Scan from shortest distance:
  44751. * - start of string
  44752. * - end of string
  44753. * - cache entry (if exists)
  44754. */
  44755. DUK_ASSERT(DUK_HSTRING_GET_CHARLEN(h) >= char_offset);
  44756. dist_start = char_offset;
  44757. dist_end = char_length - char_offset;
  44758. dist_sce = 0; DUK_UNREF(dist_sce); /* initialize for debug prints, needed if sce==NULL */
  44759. p_start = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  44760. p_end = (const duk_uint8_t *) (p_start + DUK_HSTRING_GET_BYTELEN(h));
  44761. p_found = NULL;
  44762. if (sce) {
  44763. if (char_offset >= sce->cidx) {
  44764. dist_sce = char_offset - sce->cidx;
  44765. if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
  44766. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  44767. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  44768. "scan forwards from sce",
  44769. (long) use_cache, (void *) (sce ? sce->h : NULL),
  44770. (sce ? (long) sce->cidx : (long) -1),
  44771. (sce ? (long) sce->bidx : (long) -1),
  44772. (long) dist_start, (long) dist_end, (long) dist_sce));
  44773. p_found = duk__scan_forwards(p_start + sce->bidx,
  44774. p_end,
  44775. dist_sce);
  44776. goto scan_done;
  44777. }
  44778. } else {
  44779. dist_sce = sce->cidx - char_offset;
  44780. if ((dist_sce <= dist_start) && (dist_sce <= dist_end)) {
  44781. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  44782. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  44783. "scan backwards from sce",
  44784. (long) use_cache, (void *) (sce ? sce->h : NULL),
  44785. (sce ? (long) sce->cidx : (long) -1),
  44786. (sce ? (long) sce->bidx : (long) -1),
  44787. (long) dist_start, (long) dist_end, (long) dist_sce));
  44788. p_found = duk__scan_backwards(p_start + sce->bidx,
  44789. p_start,
  44790. dist_sce);
  44791. goto scan_done;
  44792. }
  44793. }
  44794. }
  44795. /* no sce, or sce scan not best */
  44796. if (dist_start <= dist_end) {
  44797. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  44798. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  44799. "scan forwards from string start",
  44800. (long) use_cache, (void *) (sce ? sce->h : NULL),
  44801. (sce ? (long) sce->cidx : (long) -1),
  44802. (sce ? (long) sce->bidx : (long) -1),
  44803. (long) dist_start, (long) dist_end, (long) dist_sce));
  44804. p_found = duk__scan_forwards(p_start,
  44805. p_end,
  44806. dist_start);
  44807. } else {
  44808. DUK_DDD(DUK_DDDPRINT("non-ascii string, use_cache=%ld, sce=%p:%ld:%ld, "
  44809. "dist_start=%ld, dist_end=%ld, dist_sce=%ld => "
  44810. "scan backwards from string end",
  44811. (long) use_cache, (void *) (sce ? sce->h : NULL),
  44812. (sce ? (long) sce->cidx : (long) -1),
  44813. (sce ? (long) sce->bidx : (long) -1),
  44814. (long) dist_start, (long) dist_end, (long) dist_sce));
  44815. p_found = duk__scan_backwards(p_end,
  44816. p_start,
  44817. dist_end);
  44818. }
  44819. scan_done:
  44820. if (DUK_UNLIKELY(p_found == NULL)) {
  44821. /* Scan error: this shouldn't normally happen; it could happen if
  44822. * string is not valid UTF-8 data, and clen/blen are not consistent
  44823. * with the scanning algorithm.
  44824. */
  44825. goto scan_error;
  44826. }
  44827. DUK_ASSERT(p_found >= p_start);
  44828. DUK_ASSERT(p_found <= p_end); /* may be equal */
  44829. byte_offset = (duk_uint32_t) (p_found - p_start);
  44830. DUK_DDD(DUK_DDDPRINT("-> string %p, cidx %ld -> bidx %ld",
  44831. (void *) h, (long) char_offset, (long) byte_offset));
  44832. /*
  44833. * Update cache entry (allocating if necessary), and move the
  44834. * cache entry to the first place (in an "LRU" policy).
  44835. */
  44836. if (use_cache) {
  44837. /* update entry, allocating if necessary */
  44838. if (!sce) {
  44839. sce = heap->strcache + DUK_HEAP_STRCACHE_SIZE - 1; /* take last entry */
  44840. sce->h = h;
  44841. }
  44842. DUK_ASSERT(sce != NULL);
  44843. sce->bidx = (duk_uint32_t) (p_found - p_start);
  44844. sce->cidx = (duk_uint32_t) char_offset;
  44845. /* LRU: move our entry to first */
  44846. if (sce > &heap->strcache[0]) {
  44847. /*
  44848. * A C
  44849. * B A
  44850. * C <- sce ==> B
  44851. * D D
  44852. */
  44853. duk_strcache_entry tmp;
  44854. tmp = *sce;
  44855. duk_memmove((void *) (&heap->strcache[1]),
  44856. (const void *) (&heap->strcache[0]),
  44857. (size_t) (((char *) sce) - ((char *) &heap->strcache[0])));
  44858. heap->strcache[0] = tmp;
  44859. /* 'sce' points to the wrong entry here, but is no longer used */
  44860. }
  44861. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  44862. DUK_DDD(DUK_DDDPRINT("stringcache after char2byte (using cache):"));
  44863. for (i = 0; i < DUK_HEAP_STRCACHE_SIZE; i++) {
  44864. duk_strcache_entry *c = heap->strcache + i;
  44865. DUK_DDD(DUK_DDDPRINT(" [%ld] -> h=%p, cidx=%ld, bidx=%ld",
  44866. (long) i, (void *) c->h, (long) c->cidx, (long) c->bidx));
  44867. }
  44868. #endif
  44869. }
  44870. return byte_offset;
  44871. scan_error:
  44872. DUK_ERROR_INTERNAL(thr);
  44873. DUK_WO_NORETURN(return 0;);
  44874. }
  44875. #line 1 "duk_heap_stringtable.c"
  44876. /*
  44877. * Heap string table handling, string interning.
  44878. */
  44879. /* #include duk_internal.h -> already included */
  44880. /* Resize checks not needed if minsize == maxsize, typical for low memory
  44881. * targets.
  44882. */
  44883. #define DUK__STRTAB_RESIZE_CHECK
  44884. #if (DUK_USE_STRTAB_MINSIZE == DUK_USE_STRTAB_MAXSIZE)
  44885. #undef DUK__STRTAB_RESIZE_CHECK
  44886. #endif
  44887. #if defined(DUK_USE_STRTAB_PTRCOMP)
  44888. #define DUK__HEAPPTR_ENC16(heap,ptr) DUK_USE_HEAPPTR_ENC16((heap)->heap_udata, (ptr))
  44889. #define DUK__HEAPPTR_DEC16(heap,val) DUK_USE_HEAPPTR_DEC16((heap)->heap_udata, (val))
  44890. #define DUK__GET_STRTABLE(heap) ((heap)->strtable16)
  44891. #else
  44892. #define DUK__HEAPPTR_ENC16(heap,ptr) (ptr)
  44893. #define DUK__HEAPPTR_DEC16(heap,val) (val)
  44894. #define DUK__GET_STRTABLE(heap) ((heap)->strtable)
  44895. #endif
  44896. #define DUK__STRTAB_U32_MAX_STRLEN 10 /* 4'294'967'295 */
  44897. /*
  44898. * Debug dump stringtable.
  44899. */
  44900. #if defined(DUK_USE_DEBUG)
  44901. DUK_INTERNAL void duk_heap_strtable_dump(duk_heap *heap) {
  44902. #if defined(DUK_USE_STRTAB_PTRCOMP)
  44903. duk_uint16_t *strtable;
  44904. #else
  44905. duk_hstring **strtable;
  44906. #endif
  44907. duk_uint32_t i;
  44908. duk_hstring *h;
  44909. duk_size_t count_total = 0;
  44910. duk_size_t count_chain;
  44911. duk_size_t count_chain_min = DUK_SIZE_MAX;
  44912. duk_size_t count_chain_max = 0;
  44913. duk_size_t count_len[8]; /* chain lengths from 0 to 7 */
  44914. if (heap == NULL) {
  44915. DUK_D(DUK_DPRINT("string table, heap=NULL"));
  44916. return;
  44917. }
  44918. strtable = DUK__GET_STRTABLE(heap);
  44919. if (strtable == NULL) {
  44920. DUK_D(DUK_DPRINT("string table, strtab=NULL"));
  44921. return;
  44922. }
  44923. duk_memzero((void *) count_len, sizeof(count_len));
  44924. for (i = 0; i < heap->st_size; i++) {
  44925. h = DUK__HEAPPTR_DEC16(heap, strtable[i]);
  44926. count_chain = 0;
  44927. while (h != NULL) {
  44928. count_chain++;
  44929. h = h->hdr.h_next;
  44930. }
  44931. if (count_chain < sizeof(count_len) / sizeof(duk_size_t)) {
  44932. count_len[count_chain]++;
  44933. }
  44934. count_chain_max = (count_chain > count_chain_max ? count_chain : count_chain_max);
  44935. count_chain_min = (count_chain < count_chain_min ? count_chain : count_chain_min);
  44936. count_total += count_chain;
  44937. }
  44938. DUK_D(DUK_DPRINT("string table, strtab=%p, count=%lu, chain min=%lu max=%lu avg=%lf: "
  44939. "counts: %lu %lu %lu %lu %lu %lu %lu %lu ...",
  44940. (void *) heap->strtable, (unsigned long) count_total,
  44941. (unsigned long) count_chain_min, (unsigned long) count_chain_max,
  44942. (double) count_total / (double) heap->st_size,
  44943. (unsigned long) count_len[0], (unsigned long) count_len[1],
  44944. (unsigned long) count_len[2], (unsigned long) count_len[3],
  44945. (unsigned long) count_len[4], (unsigned long) count_len[5],
  44946. (unsigned long) count_len[6], (unsigned long) count_len[7]));
  44947. }
  44948. #endif /* DUK_USE_DEBUG */
  44949. /*
  44950. * Assertion helper to ensure strtable is populated correctly.
  44951. */
  44952. #if defined(DUK_USE_ASSERTIONS)
  44953. DUK_LOCAL void duk__strtable_assert_checks(duk_heap *heap) {
  44954. #if defined(DUK_USE_STRTAB_PTRCOMP)
  44955. duk_uint16_t *strtable;
  44956. #else
  44957. duk_hstring **strtable;
  44958. #endif
  44959. duk_uint32_t i;
  44960. duk_hstring *h;
  44961. duk_size_t count = 0;
  44962. DUK_ASSERT(heap != NULL);
  44963. strtable = DUK__GET_STRTABLE(heap);
  44964. if (strtable != NULL) {
  44965. DUK_ASSERT(heap->st_size != 0);
  44966. DUK_ASSERT(heap->st_mask == heap->st_size - 1);
  44967. for (i = 0; i < heap->st_size; i++) {
  44968. h = DUK__HEAPPTR_DEC16(heap, strtable[i]);
  44969. while (h != NULL) {
  44970. DUK_ASSERT((DUK_HSTRING_GET_HASH(h) & heap->st_mask) == i);
  44971. count++;
  44972. h = h->hdr.h_next;
  44973. }
  44974. }
  44975. } else {
  44976. DUK_ASSERT(heap->st_size == 0);
  44977. DUK_ASSERT(heap->st_mask == 0);
  44978. }
  44979. #if defined(DUK__STRTAB_RESIZE_CHECK)
  44980. DUK_ASSERT(count == (duk_size_t) heap->st_count);
  44981. #endif
  44982. }
  44983. #endif /* DUK_USE_ASSERTIONS */
  44984. /*
  44985. * Allocate and initialize a duk_hstring.
  44986. *
  44987. * Returns a NULL if allocation or initialization fails for some reason.
  44988. *
  44989. * The string won't be inserted into the string table and isn't tracked in
  44990. * any way (link pointers will be NULL). The caller must place the string
  44991. * into the string table without any risk of a longjmp, otherwise the string
  44992. * is leaked.
  44993. */
  44994. DUK_LOCAL duk_hstring *duk__strtable_alloc_hstring(duk_heap *heap,
  44995. const duk_uint8_t *str,
  44996. duk_uint32_t blen,
  44997. duk_uint32_t strhash,
  44998. const duk_uint8_t *extdata) {
  44999. duk_hstring *res;
  45000. const duk_uint8_t *data;
  45001. #if !defined(DUK_USE_HSTRING_ARRIDX)
  45002. duk_uarridx_t dummy;
  45003. #endif
  45004. DUK_ASSERT(heap != NULL);
  45005. DUK_UNREF(extdata);
  45006. #if defined(DUK_USE_STRLEN16)
  45007. /* If blen <= 0xffffUL, clen is also guaranteed to be <= 0xffffUL. */
  45008. if (blen > 0xffffUL) {
  45009. DUK_D(DUK_DPRINT("16-bit string blen/clen active and blen over 16 bits, reject intern"));
  45010. goto alloc_error;
  45011. }
  45012. #endif
  45013. /* XXX: Memzeroing the allocated structure is not really necessary
  45014. * because we could just initialize all fields explicitly (almost
  45015. * all fields are initialized explicitly anyway).
  45016. */
  45017. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_INTERN_CHECK)
  45018. if (extdata) {
  45019. res = (duk_hstring *) DUK_ALLOC(heap, sizeof(duk_hstring_external));
  45020. if (DUK_UNLIKELY(res == NULL)) {
  45021. goto alloc_error;
  45022. }
  45023. duk_memzero(res, sizeof(duk_hstring_external));
  45024. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45025. DUK_HEAPHDR_STRING_INIT_NULLS(&res->hdr);
  45026. #endif
  45027. DUK_HEAPHDR_SET_TYPE_AND_FLAGS(&res->hdr, DUK_HTYPE_STRING, DUK_HSTRING_FLAG_EXTDATA);
  45028. DUK_ASSERT(extdata[blen] == 0); /* Application responsibility. */
  45029. data = extdata;
  45030. ((duk_hstring_external *) res)->extdata = extdata;
  45031. } else
  45032. #endif /* DUK_USE_HSTRING_EXTDATA && DUK_USE_EXTSTR_INTERN_CHECK */
  45033. {
  45034. duk_uint8_t *data_tmp;
  45035. /* NUL terminate for convenient C access */
  45036. DUK_ASSERT(sizeof(duk_hstring) + blen + 1 > blen); /* No wrap, limits ensure. */
  45037. res = (duk_hstring *) DUK_ALLOC(heap, sizeof(duk_hstring) + blen + 1);
  45038. if (DUK_UNLIKELY(res == NULL)) {
  45039. goto alloc_error;
  45040. }
  45041. duk_memzero(res, sizeof(duk_hstring));
  45042. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45043. DUK_HEAPHDR_STRING_INIT_NULLS(&res->hdr);
  45044. #endif
  45045. DUK_HEAPHDR_SET_TYPE_AND_FLAGS(&res->hdr, DUK_HTYPE_STRING, 0);
  45046. data_tmp = (duk_uint8_t *) (res + 1);
  45047. duk_memcpy(data_tmp, str, blen);
  45048. data_tmp[blen] = (duk_uint8_t) 0;
  45049. data = (const duk_uint8_t *) data_tmp;
  45050. }
  45051. DUK_HSTRING_SET_BYTELEN(res, blen);
  45052. DUK_HSTRING_SET_HASH(res, strhash);
  45053. DUK_ASSERT(!DUK_HSTRING_HAS_ARRIDX(res));
  45054. #if defined(DUK_USE_HSTRING_ARRIDX)
  45055. res->arridx = duk_js_to_arrayindex_string(data, blen);
  45056. if (res->arridx != DUK_HSTRING_NO_ARRAY_INDEX) {
  45057. #else
  45058. dummy = duk_js_to_arrayindex_string(data, blen);
  45059. if (dummy != DUK_HSTRING_NO_ARRAY_INDEX) {
  45060. #endif
  45061. /* Array index strings cannot be symbol strings,
  45062. * and they're always pure ASCII so blen == clen.
  45063. */
  45064. DUK_HSTRING_SET_ARRIDX(res);
  45065. DUK_HSTRING_SET_ASCII(res);
  45066. DUK_ASSERT(duk_unicode_unvalidated_utf8_length(data, (duk_size_t) blen) == blen);
  45067. } else {
  45068. /* Because 'data' is NUL-terminated, we don't need a
  45069. * blen > 0 check here. For NUL (0x00) the symbol
  45070. * checks will be false.
  45071. */
  45072. if (DUK_UNLIKELY(data[0] >= 0x80U)) {
  45073. if (data[0] <= 0x81) {
  45074. DUK_HSTRING_SET_SYMBOL(res);
  45075. } else if (data[0] == 0x82U || data[0] == 0xffU) {
  45076. DUK_HSTRING_SET_HIDDEN(res);
  45077. DUK_HSTRING_SET_SYMBOL(res);
  45078. }
  45079. }
  45080. /* Using an explicit 'ASCII' flag has larger footprint (one call site
  45081. * only) but is quite useful for the case when there's no explicit
  45082. * 'clen' in duk_hstring.
  45083. *
  45084. * The flag is set lazily for RAM strings.
  45085. */
  45086. DUK_ASSERT(!DUK_HSTRING_HAS_ASCII(res));
  45087. #if defined(DUK_USE_HSTRING_LAZY_CLEN)
  45088. /* Charlen initialized to 0, updated on-the-fly. */
  45089. #else
  45090. duk_hstring_init_charlen(res); /* Also sets ASCII flag. */
  45091. #endif
  45092. }
  45093. DUK_DDD(DUK_DDDPRINT("interned string, hash=0x%08lx, blen=%ld, has_arridx=%ld, has_extdata=%ld",
  45094. (unsigned long) DUK_HSTRING_GET_HASH(res),
  45095. (long) DUK_HSTRING_GET_BYTELEN(res),
  45096. (long) (DUK_HSTRING_HAS_ARRIDX(res) ? 1 : 0),
  45097. (long) (DUK_HSTRING_HAS_EXTDATA(res) ? 1 : 0)));
  45098. DUK_ASSERT(res != NULL);
  45099. return res;
  45100. alloc_error:
  45101. return NULL;
  45102. }
  45103. /*
  45104. * Grow strtable allocation in-place.
  45105. */
  45106. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45107. DUK_LOCAL void duk__strtable_grow_inplace(duk_heap *heap) {
  45108. duk_uint32_t new_st_size;
  45109. duk_uint32_t old_st_size;
  45110. duk_uint32_t i;
  45111. duk_hstring *h;
  45112. duk_hstring *next;
  45113. duk_hstring *prev;
  45114. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45115. duk_uint16_t *new_ptr;
  45116. duk_uint16_t *new_ptr_high;
  45117. #else
  45118. duk_hstring **new_ptr;
  45119. duk_hstring **new_ptr_high;
  45120. #endif
  45121. DUK_DD(DUK_DDPRINT("grow in-place: %lu -> %lu", (unsigned long) heap->st_size, (unsigned long) heap->st_size * 2));
  45122. DUK_ASSERT(heap != NULL);
  45123. DUK_ASSERT(heap->st_resizing == 1);
  45124. DUK_ASSERT(heap->st_size >= 2);
  45125. DUK_ASSERT((heap->st_size & (heap->st_size - 1)) == 0); /* 2^N */
  45126. DUK_ASSERT(DUK__GET_STRTABLE(heap) != NULL);
  45127. DUK_STATS_INC(heap, stats_strtab_resize_grow);
  45128. new_st_size = heap->st_size << 1U;
  45129. DUK_ASSERT(new_st_size > heap->st_size); /* No overflow. */
  45130. /* Reallocate the strtable first and then work in-place to rehash
  45131. * strings. We don't need an indirect allocation here: even if GC
  45132. * is triggered to satisfy the allocation, recursive strtable resize
  45133. * is prevented by flags. This is also why we don't need to use
  45134. * DUK_REALLOC_INDIRECT().
  45135. */
  45136. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45137. new_ptr = (duk_uint16_t *) DUK_REALLOC(heap, heap->strtable16, sizeof(duk_uint16_t) * new_st_size);
  45138. #else
  45139. new_ptr = (duk_hstring **) DUK_REALLOC(heap, heap->strtable, sizeof(duk_hstring *) * new_st_size);
  45140. #endif
  45141. if (DUK_UNLIKELY(new_ptr == NULL)) {
  45142. /* If realloc fails we can continue normally: the string table
  45143. * won't "fill up" although chains will gradually get longer.
  45144. * When string insertions continue, we'll quite soon try again
  45145. * with no special handling.
  45146. */
  45147. DUK_D(DUK_DPRINT("string table grow failed, ignoring"));
  45148. return;
  45149. }
  45150. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45151. heap->strtable16 = new_ptr;
  45152. #else
  45153. heap->strtable = new_ptr;
  45154. #endif
  45155. /* Rehash a single bucket into two separate ones. When we grow
  45156. * by x2 the highest 'new' bit determines whether a string remains
  45157. * in its old position (bit is 0) or goes to a new one (bit is 1).
  45158. */
  45159. old_st_size = heap->st_size;
  45160. new_ptr_high = new_ptr + old_st_size;
  45161. for (i = 0; i < old_st_size; i++) {
  45162. duk_hstring *new_root;
  45163. duk_hstring *new_root_high;
  45164. h = DUK__HEAPPTR_DEC16(heap, new_ptr[i]);
  45165. new_root = h;
  45166. new_root_high = NULL;
  45167. prev = NULL;
  45168. while (h != NULL) {
  45169. duk_uint32_t mask;
  45170. DUK_ASSERT((DUK_HSTRING_GET_HASH(h) & heap->st_mask) == i);
  45171. next = h->hdr.h_next;
  45172. /* Example: if previous size was 256, previous mask is 0xFF
  45173. * and size is 0x100 which corresponds to the new bit that
  45174. * comes into play.
  45175. */
  45176. DUK_ASSERT(heap->st_mask == old_st_size - 1);
  45177. mask = old_st_size;
  45178. if (DUK_HSTRING_GET_HASH(h) & mask) {
  45179. if (prev != NULL) {
  45180. prev->hdr.h_next = h->hdr.h_next;
  45181. } else {
  45182. DUK_ASSERT(h == new_root);
  45183. new_root = h->hdr.h_next;
  45184. }
  45185. h->hdr.h_next = new_root_high;
  45186. new_root_high = h;
  45187. } else {
  45188. prev = h;
  45189. }
  45190. h = next;
  45191. }
  45192. new_ptr[i] = DUK__HEAPPTR_ENC16(heap, new_root);
  45193. new_ptr_high[i] = DUK__HEAPPTR_ENC16(heap, new_root_high);
  45194. }
  45195. heap->st_size = new_st_size;
  45196. heap->st_mask = new_st_size - 1;
  45197. #if defined(DUK_USE_ASSERTIONS)
  45198. duk__strtable_assert_checks(heap);
  45199. #endif
  45200. }
  45201. #endif /* DUK__STRTAB_RESIZE_CHECK */
  45202. /*
  45203. * Shrink strtable allocation in-place.
  45204. */
  45205. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45206. DUK_LOCAL void duk__strtable_shrink_inplace(duk_heap *heap) {
  45207. duk_uint32_t new_st_size;
  45208. duk_uint32_t i;
  45209. duk_hstring *h;
  45210. duk_hstring *other;
  45211. duk_hstring *root;
  45212. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45213. duk_uint16_t *old_ptr;
  45214. duk_uint16_t *old_ptr_high;
  45215. duk_uint16_t *new_ptr;
  45216. #else
  45217. duk_hstring **old_ptr;
  45218. duk_hstring **old_ptr_high;
  45219. duk_hstring **new_ptr;
  45220. #endif
  45221. DUK_DD(DUK_DDPRINT("shrink in-place: %lu -> %lu", (unsigned long) heap->st_size, (unsigned long) heap->st_size / 2));
  45222. DUK_ASSERT(heap != NULL);
  45223. DUK_ASSERT(heap->st_resizing == 1);
  45224. DUK_ASSERT(heap->st_size >= 2);
  45225. DUK_ASSERT((heap->st_size & (heap->st_size - 1)) == 0); /* 2^N */
  45226. DUK_ASSERT(DUK__GET_STRTABLE(heap) != NULL);
  45227. DUK_STATS_INC(heap, stats_strtab_resize_shrink);
  45228. new_st_size = heap->st_size >> 1U;
  45229. /* Combine two buckets into a single one. When we shrink, one hash
  45230. * bit (highest) disappears.
  45231. */
  45232. old_ptr = DUK__GET_STRTABLE(heap);
  45233. old_ptr_high = old_ptr + new_st_size;
  45234. for (i = 0; i < new_st_size; i++) {
  45235. h = DUK__HEAPPTR_DEC16(heap, old_ptr[i]);
  45236. other = DUK__HEAPPTR_DEC16(heap, old_ptr_high[i]);
  45237. if (h == NULL) {
  45238. /* First chain is empty, so use second one as is. */
  45239. root = other;
  45240. } else {
  45241. /* Find end of first chain, and link in the second. */
  45242. root = h;
  45243. while (h->hdr.h_next != NULL) {
  45244. h = h->hdr.h_next;
  45245. }
  45246. h->hdr.h_next = other;
  45247. }
  45248. old_ptr[i] = DUK__HEAPPTR_ENC16(heap, root);
  45249. }
  45250. heap->st_size = new_st_size;
  45251. heap->st_mask = new_st_size - 1;
  45252. /* The strtable is now consistent and we can realloc safely. Even
  45253. * if side effects cause string interning or removal the strtable
  45254. * updates are safe. Recursive resize has been prevented by caller.
  45255. * This is also why we don't need to use DUK_REALLOC_INDIRECT().
  45256. *
  45257. * We assume a realloc() to a smaller size is guaranteed to succeed.
  45258. * It would be relatively straightforward to handle the error by
  45259. * essentially performing a "grow" step to recover.
  45260. */
  45261. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45262. new_ptr = (duk_uint16_t *) DUK_REALLOC(heap, heap->strtable16, sizeof(duk_uint16_t) * new_st_size);
  45263. DUK_ASSERT(new_ptr != NULL);
  45264. heap->strtable16 = new_ptr;
  45265. #else
  45266. new_ptr = (duk_hstring **) DUK_REALLOC(heap, heap->strtable, sizeof(duk_hstring *) * new_st_size);
  45267. DUK_ASSERT(new_ptr != NULL);
  45268. heap->strtable = new_ptr;
  45269. #endif
  45270. #if defined(DUK_USE_ASSERTIONS)
  45271. duk__strtable_assert_checks(heap);
  45272. #endif
  45273. }
  45274. #endif /* DUK__STRTAB_RESIZE_CHECK */
  45275. /*
  45276. * Grow/shrink check.
  45277. */
  45278. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45279. DUK_LOCAL DUK_COLD DUK_NOINLINE void duk__strtable_resize_check(duk_heap *heap) {
  45280. duk_uint32_t load_factor; /* fixed point */
  45281. DUK_ASSERT(heap != NULL);
  45282. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45283. DUK_ASSERT(heap->strtable16 != NULL);
  45284. #else
  45285. DUK_ASSERT(heap->strtable != NULL);
  45286. #endif
  45287. DUK_STATS_INC(heap, stats_strtab_resize_check);
  45288. /* Prevent recursive resizing. */
  45289. if (DUK_UNLIKELY(heap->st_resizing != 0U)) {
  45290. DUK_D(DUK_DPRINT("prevent recursive strtable resize"));
  45291. return;
  45292. }
  45293. heap->st_resizing = 1;
  45294. DUK_ASSERT(heap->st_size >= 16U);
  45295. DUK_ASSERT((heap->st_size >> 4U) >= 1);
  45296. load_factor = heap->st_count / (heap->st_size >> 4U);
  45297. DUK_DD(DUK_DDPRINT("resize check string table: size=%lu, count=%lu, load_factor=%lu (fixed point .4; float %lf)",
  45298. (unsigned long) heap->st_size, (unsigned long) heap->st_count,
  45299. (unsigned long) load_factor,
  45300. (double) heap->st_count / (double) heap->st_size));
  45301. if (load_factor >= DUK_USE_STRTAB_GROW_LIMIT) {
  45302. if (heap->st_size >= DUK_USE_STRTAB_MAXSIZE) {
  45303. DUK_DD(DUK_DDPRINT("want to grow strtable (based on load factor) but already maximum size"));
  45304. } else {
  45305. DUK_D(DUK_DPRINT("grow string table: %lu -> %lu", (unsigned long) heap->st_size, (unsigned long) heap->st_size * 2));
  45306. #if defined(DUK_USE_DEBUG)
  45307. duk_heap_strtable_dump(heap);
  45308. #endif
  45309. duk__strtable_grow_inplace(heap);
  45310. }
  45311. } else if (load_factor <= DUK_USE_STRTAB_SHRINK_LIMIT) {
  45312. if (heap->st_size <= DUK_USE_STRTAB_MINSIZE) {
  45313. DUK_DD(DUK_DDPRINT("want to shrink strtable (based on load factor) but already minimum size"));
  45314. } else {
  45315. DUK_D(DUK_DPRINT("shrink string table: %lu -> %lu", (unsigned long) heap->st_size, (unsigned long) heap->st_size / 2));
  45316. #if defined(DUK_USE_DEBUG)
  45317. duk_heap_strtable_dump(heap);
  45318. #endif
  45319. duk__strtable_shrink_inplace(heap);
  45320. }
  45321. } else {
  45322. DUK_DD(DUK_DDPRINT("no need for strtable resize"));
  45323. }
  45324. heap->st_resizing = 0;
  45325. }
  45326. #endif /* DUK__STRTAB_RESIZE_CHECK */
  45327. /*
  45328. * Torture grow/shrink: unconditionally grow and shrink back.
  45329. */
  45330. #if defined(DUK_USE_STRTAB_TORTURE) && defined(DUK__STRTAB_RESIZE_CHECK)
  45331. DUK_LOCAL void duk__strtable_resize_torture(duk_heap *heap) {
  45332. duk_uint32_t old_st_size;
  45333. DUK_ASSERT(heap != NULL);
  45334. old_st_size = heap->st_size;
  45335. if (old_st_size >= DUK_USE_STRTAB_MAXSIZE) {
  45336. return;
  45337. }
  45338. heap->st_resizing = 1;
  45339. duk__strtable_grow_inplace(heap);
  45340. if (heap->st_size > old_st_size) {
  45341. duk__strtable_shrink_inplace(heap);
  45342. }
  45343. heap->st_resizing = 0;
  45344. }
  45345. #endif /* DUK_USE_STRTAB_TORTURE && DUK__STRTAB_RESIZE_CHECK */
  45346. /*
  45347. * Raw intern; string already checked not to be present.
  45348. */
  45349. DUK_LOCAL duk_hstring *duk__strtable_do_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen, duk_uint32_t strhash) {
  45350. duk_hstring *res;
  45351. const duk_uint8_t *extdata;
  45352. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45353. duk_uint16_t *slot;
  45354. #else
  45355. duk_hstring **slot;
  45356. #endif
  45357. DUK_DDD(DUK_DDDPRINT("do_intern: heap=%p, str=%p, blen=%lu, strhash=%lx, st_size=%lu, st_count=%lu, load=%lf",
  45358. (void *) heap, (const void *) str, (unsigned long) blen, (unsigned long) strhash,
  45359. (unsigned long) heap->st_size, (unsigned long) heap->st_count,
  45360. (double) heap->st_count / (double) heap->st_size));
  45361. DUK_ASSERT(heap != NULL);
  45362. /* Prevent any side effects on the string table and the caller provided
  45363. * str/blen arguments while interning is in progress. For example, if
  45364. * the caller provided str/blen from a dynamic buffer, a finalizer
  45365. * might resize or modify that dynamic buffer, invalidating the call
  45366. * arguments.
  45367. *
  45368. * While finalizers must be prevented, mark-and-sweep itself is fine.
  45369. * Recursive string table resize is prevented explicitly here.
  45370. */
  45371. heap->pf_prevent_count++;
  45372. DUK_ASSERT(heap->pf_prevent_count != 0); /* Wrap. */
  45373. #if defined(DUK_USE_STRTAB_TORTURE) && defined(DUK__STRTAB_RESIZE_CHECK)
  45374. duk__strtable_resize_torture(heap);
  45375. #endif
  45376. /* String table grow/shrink check. Because of chaining (and no
  45377. * accumulation issues as with hash probe chains and DELETED
  45378. * markers) there's never a mandatory need to resize right now.
  45379. * Check for the resize only periodically, based on st_count
  45380. * bit pattern. Because string table removal doesn't do a shrink
  45381. * check, we do that also here.
  45382. *
  45383. * Do the resize and possible grow/shrink before the new duk_hstring
  45384. * has been allocated. Otherwise we may trigger a GC when the result
  45385. * duk_hstring is not yet strongly referenced.
  45386. */
  45387. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45388. if (DUK_UNLIKELY((heap->st_count & DUK_USE_STRTAB_RESIZE_CHECK_MASK) == 0)) {
  45389. duk__strtable_resize_check(heap);
  45390. }
  45391. #endif
  45392. /* External string check (low memory optimization). */
  45393. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_INTERN_CHECK)
  45394. extdata = (const duk_uint8_t *) DUK_USE_EXTSTR_INTERN_CHECK(heap->heap_udata, (void *) DUK_LOSE_CONST(str), (duk_size_t) blen);
  45395. #else
  45396. extdata = (const duk_uint8_t *) NULL;
  45397. #endif
  45398. /* Allocate and initialize string, not yet linked. This may cause a
  45399. * GC which may cause other strings to be interned and inserted into
  45400. * the string table before we insert our string. Finalizer execution
  45401. * is disabled intentionally to avoid a finalizer from e.g. resizing
  45402. * a buffer used as a data area for 'str'.
  45403. */
  45404. res = duk__strtable_alloc_hstring(heap, str, blen, strhash, extdata);
  45405. /* Allow side effects again: GC must be avoided until duk_hstring
  45406. * result (if successful) has been INCREF'd.
  45407. */
  45408. DUK_ASSERT(heap->pf_prevent_count > 0);
  45409. heap->pf_prevent_count--;
  45410. /* Alloc error handling. */
  45411. if (DUK_UNLIKELY(res == NULL)) {
  45412. #if defined(DUK_USE_HSTRING_EXTDATA) && defined(DUK_USE_EXTSTR_INTERN_CHECK)
  45413. if (extdata != NULL) {
  45414. DUK_USE_EXTSTR_FREE(heap->heap_udata, (const void *) extdata);
  45415. }
  45416. #endif
  45417. return NULL;
  45418. }
  45419. /* Insert into string table. */
  45420. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45421. slot = heap->strtable16 + (strhash & heap->st_mask);
  45422. #else
  45423. slot = heap->strtable + (strhash & heap->st_mask);
  45424. #endif
  45425. DUK_ASSERT(res->hdr.h_next == NULL); /* This is the case now, but unnecessary zeroing/NULLing. */
  45426. res->hdr.h_next = DUK__HEAPPTR_DEC16(heap, *slot);
  45427. *slot = DUK__HEAPPTR_ENC16(heap, res);
  45428. /* Update string count only for successful inserts. */
  45429. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45430. heap->st_count++;
  45431. #endif
  45432. /* The duk_hstring is in the string table but is not yet strongly
  45433. * reachable. Calling code MUST NOT make any allocations or other
  45434. * side effects before the duk_hstring has been INCREF'd and made
  45435. * reachable.
  45436. */
  45437. return res;
  45438. }
  45439. /*
  45440. * Intern a string from str/blen, returning either an existing duk_hstring
  45441. * or adding a new one into the string table. The input string does -not-
  45442. * need to be NUL terminated.
  45443. *
  45444. * The input 'str' argument may point to a Duktape managed data area such as
  45445. * the data area of a dynamic buffer. It's crucial to avoid any side effects
  45446. * that might affect the data area (e.g. resize the dynamic buffer, or write
  45447. * to the buffer) before the string is fully interned.
  45448. */
  45449. #if defined(DUK_USE_ROM_STRINGS)
  45450. DUK_LOCAL duk_hstring *duk__strtab_romstring_lookup(duk_heap *heap, const duk_uint8_t *str, duk_size_t blen, duk_uint32_t strhash) {
  45451. duk_size_t lookup_hash;
  45452. duk_hstring *curr;
  45453. DUK_ASSERT(heap != NULL);
  45454. DUK_UNREF(heap);
  45455. lookup_hash = (blen << 4);
  45456. if (blen > 0) {
  45457. lookup_hash += str[0];
  45458. }
  45459. lookup_hash &= 0xff;
  45460. curr = (duk_hstring *) DUK_LOSE_CONST(duk_rom_strings_lookup[lookup_hash]);
  45461. while (curr != NULL) {
  45462. /* Unsafe memcmp() because for zero blen, str may be NULL. */
  45463. if (strhash == DUK_HSTRING_GET_HASH(curr) &&
  45464. blen == DUK_HSTRING_GET_BYTELEN(curr) &&
  45465. duk_memcmp_unsafe((const void *) str, (const void *) DUK_HSTRING_GET_DATA(curr), blen) == 0) {
  45466. DUK_DDD(DUK_DDDPRINT("intern check: rom string: %!O, computed hash 0x%08lx, rom hash 0x%08lx",
  45467. curr, (unsigned long) strhash, (unsigned long) DUK_HSTRING_GET_HASH(curr)));
  45468. return curr;
  45469. }
  45470. curr = curr->hdr.h_next;
  45471. }
  45472. return NULL;
  45473. }
  45474. #endif /* DUK_USE_ROM_STRINGS */
  45475. DUK_INTERNAL duk_hstring *duk_heap_strtable_intern(duk_heap *heap, const duk_uint8_t *str, duk_uint32_t blen) {
  45476. duk_uint32_t strhash;
  45477. duk_hstring *h;
  45478. DUK_DDD(DUK_DDDPRINT("intern check: heap=%p, str=%p, blen=%lu", (void *) heap, (const void *) str, (unsigned long) blen));
  45479. /* Preliminaries. */
  45480. /* XXX: maybe just require 'str != NULL' even for zero size? */
  45481. DUK_ASSERT(heap != NULL);
  45482. DUK_ASSERT(blen == 0 || str != NULL);
  45483. DUK_ASSERT(blen <= DUK_HSTRING_MAX_BYTELEN); /* Caller is responsible for ensuring this. */
  45484. strhash = duk_heap_hashstring(heap, str, (duk_size_t) blen);
  45485. /* String table lookup. */
  45486. DUK_ASSERT(DUK__GET_STRTABLE(heap) != NULL);
  45487. DUK_ASSERT(heap->st_size > 0);
  45488. DUK_ASSERT(heap->st_size == heap->st_mask + 1);
  45489. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45490. h = DUK__HEAPPTR_DEC16(heap, heap->strtable16[strhash & heap->st_mask]);
  45491. #else
  45492. h = heap->strtable[strhash & heap->st_mask];
  45493. #endif
  45494. while (h != NULL) {
  45495. if (DUK_HSTRING_GET_HASH(h) == strhash &&
  45496. DUK_HSTRING_GET_BYTELEN(h) == blen &&
  45497. duk_memcmp_unsafe((const void *) str, (const void *) DUK_HSTRING_GET_DATA(h), (size_t) blen) == 0) {
  45498. /* Found existing entry. */
  45499. DUK_STATS_INC(heap, stats_strtab_intern_hit);
  45500. return h;
  45501. }
  45502. h = h->hdr.h_next;
  45503. }
  45504. /* ROM table lookup. Because this lookup is slower, do it only after
  45505. * RAM lookup. This works because no ROM string is ever interned into
  45506. * the RAM string table.
  45507. */
  45508. #if defined(DUK_USE_ROM_STRINGS)
  45509. h = duk__strtab_romstring_lookup(heap, str, blen, strhash);
  45510. if (h != NULL) {
  45511. DUK_STATS_INC(heap, stats_strtab_intern_hit);
  45512. return h;
  45513. }
  45514. #endif
  45515. /* Not found in string table; insert. */
  45516. DUK_STATS_INC(heap, stats_strtab_intern_miss);
  45517. h = duk__strtable_do_intern(heap, str, blen, strhash);
  45518. return h; /* may be NULL */
  45519. }
  45520. /*
  45521. * Intern a string from u32.
  45522. */
  45523. /* XXX: Could arrange some special handling because we know that the result
  45524. * will have an arridx flag and an ASCII flag, won't need a clen check, etc.
  45525. */
  45526. DUK_INTERNAL duk_hstring *duk_heap_strtable_intern_u32(duk_heap *heap, duk_uint32_t val) {
  45527. duk_uint8_t buf[DUK__STRTAB_U32_MAX_STRLEN];
  45528. duk_uint8_t *p;
  45529. DUK_ASSERT(heap != NULL);
  45530. /* This is smaller and faster than a %lu sprintf. */
  45531. p = buf + sizeof(buf);
  45532. do {
  45533. p--;
  45534. *p = duk_lc_digits[val % 10];
  45535. val = val / 10;
  45536. } while (val != 0); /* For val == 0, emit exactly one '0'. */
  45537. DUK_ASSERT(p >= buf);
  45538. return duk_heap_strtable_intern(heap, (const duk_uint8_t *) p, (duk_uint32_t) ((buf + sizeof(buf)) - p));
  45539. }
  45540. /*
  45541. * Checked convenience variants.
  45542. *
  45543. * XXX: Because the main use case is for the checked variants, make them the
  45544. * main functionality and provide a safe variant separately (it is only needed
  45545. * during heap init). The problem with that is that longjmp state and error
  45546. * creation must already be possible to throw.
  45547. */
  45548. DUK_INTERNAL duk_hstring *duk_heap_strtable_intern_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t blen) {
  45549. duk_hstring *res;
  45550. DUK_ASSERT(thr != NULL);
  45551. DUK_ASSERT(thr->heap != NULL);
  45552. DUK_ASSERT(blen == 0 || str != NULL);
  45553. res = duk_heap_strtable_intern(thr->heap, str, blen);
  45554. if (DUK_UNLIKELY(res == NULL)) {
  45555. DUK_ERROR_ALLOC_FAILED(thr);
  45556. DUK_WO_NORETURN(return NULL;);
  45557. }
  45558. return res;
  45559. }
  45560. #if defined(DUK_USE_LITCACHE_SIZE)
  45561. DUK_LOCAL duk_uint_t duk__strtable_litcache_key(const duk_uint8_t *str, duk_uint32_t blen) {
  45562. duk_uintptr_t key;
  45563. DUK_ASSERT(DUK_USE_LITCACHE_SIZE > 0);
  45564. DUK_ASSERT(DUK_IS_POWER_OF_TWO((duk_uint_t) DUK_USE_LITCACHE_SIZE));
  45565. key = (duk_uintptr_t) blen ^ (duk_uintptr_t) str;
  45566. key &= (duk_uintptr_t) (DUK_USE_LITCACHE_SIZE - 1); /* Assumes size is power of 2. */
  45567. /* Due to masking, cast is in 32-bit range. */
  45568. DUK_ASSERT(key <= DUK_UINT_MAX);
  45569. return (duk_uint_t) key;
  45570. }
  45571. DUK_INTERNAL duk_hstring *duk_heap_strtable_intern_literal_checked(duk_hthread *thr, const duk_uint8_t *str, duk_uint32_t blen) {
  45572. duk_uint_t key;
  45573. duk_litcache_entry *ent;
  45574. duk_hstring *h;
  45575. /* Fast path check: literal exists in literal cache. */
  45576. key = duk__strtable_litcache_key(str, blen);
  45577. ent = thr->heap->litcache + key;
  45578. if (ent->addr == str) {
  45579. DUK_DD(DUK_DDPRINT("intern check for cached, pinned literal: str=%p, blen=%ld -> duk_hstring %!O",
  45580. (const void *) str, (long) blen, (duk_heaphdr *) ent->h));
  45581. DUK_ASSERT(ent->h != NULL);
  45582. DUK_ASSERT(DUK_HSTRING_HAS_PINNED_LITERAL(ent->h));
  45583. DUK_STATS_INC(thr->heap, stats_strtab_litcache_hit);
  45584. return ent->h;
  45585. }
  45586. /* Intern and update (overwrite) cache entry. */
  45587. h = duk_heap_strtable_intern_checked(thr, str, blen);
  45588. ent->addr = str;
  45589. ent->h = h;
  45590. DUK_STATS_INC(thr->heap, stats_strtab_litcache_miss);
  45591. /* Pin the duk_hstring until the next mark-and-sweep. This means
  45592. * litcache entries don't need to be invalidated until the next
  45593. * mark-and-sweep as their target duk_hstring is not freed before
  45594. * the mark-and-sweep happens. The pin remains even if the literal
  45595. * cache entry is overwritten, and is still useful to avoid string
  45596. * table traffic.
  45597. */
  45598. if (!DUK_HSTRING_HAS_PINNED_LITERAL(h)) {
  45599. DUK_DD(DUK_DDPRINT("pin duk_hstring because it is a literal: %!O", (duk_heaphdr *) h));
  45600. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h));
  45601. DUK_HSTRING_INCREF(thr, h);
  45602. DUK_HSTRING_SET_PINNED_LITERAL(h);
  45603. DUK_STATS_INC(thr->heap, stats_strtab_litcache_pin);
  45604. }
  45605. return h;
  45606. }
  45607. #endif /* DUK_USE_LITCACHE_SIZE */
  45608. DUK_INTERNAL duk_hstring *duk_heap_strtable_intern_u32_checked(duk_hthread *thr, duk_uint32_t val) {
  45609. duk_hstring *res;
  45610. DUK_ASSERT(thr != NULL);
  45611. DUK_ASSERT(thr->heap != NULL);
  45612. res = duk_heap_strtable_intern_u32(thr->heap, val);
  45613. if (DUK_UNLIKELY(res == NULL)) {
  45614. DUK_ERROR_ALLOC_FAILED(thr);
  45615. DUK_WO_NORETURN(return NULL;);
  45616. }
  45617. return res;
  45618. }
  45619. /*
  45620. * Remove (unlink) a string from the string table.
  45621. *
  45622. * Just unlinks the duk_hstring, leaving link pointers as garbage.
  45623. * Caller must free the string itself.
  45624. */
  45625. #if defined(DUK_USE_REFERENCE_COUNTING)
  45626. /* Unlink without a 'prev' pointer. */
  45627. DUK_INTERNAL void duk_heap_strtable_unlink(duk_heap *heap, duk_hstring *h) {
  45628. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45629. duk_uint16_t *slot;
  45630. #else
  45631. duk_hstring **slot;
  45632. #endif
  45633. duk_hstring *other;
  45634. duk_hstring *prev;
  45635. DUK_DDD(DUK_DDDPRINT("remove: heap=%p, h=%p, blen=%lu, strhash=%lx",
  45636. (void *) heap, (void *) h,
  45637. (unsigned long) (h != NULL ? DUK_HSTRING_GET_BYTELEN(h) : 0),
  45638. (unsigned long) (h != NULL ? DUK_HSTRING_GET_HASH(h) : 0)));
  45639. DUK_ASSERT(heap != NULL);
  45640. DUK_ASSERT(h != NULL);
  45641. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45642. DUK_ASSERT(heap->st_count > 0);
  45643. heap->st_count--;
  45644. #endif
  45645. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45646. slot = heap->strtable16 + (DUK_HSTRING_GET_HASH(h) & heap->st_mask);
  45647. #else
  45648. slot = heap->strtable + (DUK_HSTRING_GET_HASH(h) & heap->st_mask);
  45649. #endif
  45650. other = DUK__HEAPPTR_DEC16(heap, *slot);
  45651. DUK_ASSERT(other != NULL); /* At least argument string is in the chain. */
  45652. prev = NULL;
  45653. while (other != h) {
  45654. prev = other;
  45655. other = other->hdr.h_next;
  45656. DUK_ASSERT(other != NULL); /* We'll eventually find 'h'. */
  45657. }
  45658. if (prev != NULL) {
  45659. /* Middle of list. */
  45660. prev->hdr.h_next = h->hdr.h_next;
  45661. } else {
  45662. /* Head of list. */
  45663. *slot = DUK__HEAPPTR_ENC16(heap, h->hdr.h_next);
  45664. }
  45665. /* There's no resize check on a string free. The next string
  45666. * intern will do one.
  45667. */
  45668. }
  45669. #endif /* DUK_USE_REFERENCE_COUNTING */
  45670. /* Unlink with a 'prev' pointer. */
  45671. DUK_INTERNAL void duk_heap_strtable_unlink_prev(duk_heap *heap, duk_hstring *h, duk_hstring *prev) {
  45672. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45673. duk_uint16_t *slot;
  45674. #else
  45675. duk_hstring **slot;
  45676. #endif
  45677. DUK_DDD(DUK_DDDPRINT("remove: heap=%p, prev=%p, h=%p, blen=%lu, strhash=%lx",
  45678. (void *) heap, (void *) prev, (void *) h,
  45679. (unsigned long) (h != NULL ? DUK_HSTRING_GET_BYTELEN(h) : 0),
  45680. (unsigned long) (h != NULL ? DUK_HSTRING_GET_HASH(h) : 0)));
  45681. DUK_ASSERT(heap != NULL);
  45682. DUK_ASSERT(h != NULL);
  45683. DUK_ASSERT(prev == NULL || prev->hdr.h_next == h);
  45684. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45685. DUK_ASSERT(heap->st_count > 0);
  45686. heap->st_count--;
  45687. #endif
  45688. if (prev != NULL) {
  45689. /* Middle of list. */
  45690. prev->hdr.h_next = h->hdr.h_next;
  45691. } else {
  45692. /* Head of list. */
  45693. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45694. slot = heap->strtable16 + (DUK_HSTRING_GET_HASH(h) & heap->st_mask);
  45695. #else
  45696. slot = heap->strtable + (DUK_HSTRING_GET_HASH(h) & heap->st_mask);
  45697. #endif
  45698. DUK_ASSERT(DUK__HEAPPTR_DEC16(heap, *slot) == h);
  45699. *slot = DUK__HEAPPTR_ENC16(heap, h->hdr.h_next);
  45700. }
  45701. }
  45702. /*
  45703. * Force string table resize check in mark-and-sweep.
  45704. */
  45705. DUK_INTERNAL void duk_heap_strtable_force_resize(duk_heap *heap) {
  45706. /* Does only one grow/shrink step if needed. The heap->st_resizing
  45707. * flag protects against recursive resizing.
  45708. */
  45709. DUK_ASSERT(heap != NULL);
  45710. DUK_UNREF(heap);
  45711. #if defined(DUK__STRTAB_RESIZE_CHECK)
  45712. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45713. if (heap->strtable16 != NULL) {
  45714. #else
  45715. if (heap->strtable != NULL) {
  45716. #endif
  45717. duk__strtable_resize_check(heap);
  45718. }
  45719. #endif
  45720. }
  45721. /*
  45722. * Free strings in the string table and the string table itself.
  45723. */
  45724. DUK_INTERNAL void duk_heap_strtable_free(duk_heap *heap) {
  45725. #if defined(DUK_USE_STRTAB_PTRCOMP)
  45726. duk_uint16_t *strtable;
  45727. duk_uint16_t *st;
  45728. #else
  45729. duk_hstring **strtable;
  45730. duk_hstring **st;
  45731. #endif
  45732. duk_hstring *h;
  45733. DUK_ASSERT(heap != NULL);
  45734. #if defined(DUK_USE_ASSERTIONS)
  45735. duk__strtable_assert_checks(heap);
  45736. #endif
  45737. /* Strtable can be NULL if heap init fails. However, in that case
  45738. * heap->st_size is 0, so strtable == strtable_end and we skip the
  45739. * loop without a special check.
  45740. */
  45741. strtable = DUK__GET_STRTABLE(heap);
  45742. st = strtable + heap->st_size;
  45743. DUK_ASSERT(strtable != NULL || heap->st_size == 0);
  45744. while (strtable != st) {
  45745. --st;
  45746. h = DUK__HEAPPTR_DEC16(heap, *st);
  45747. while (h) {
  45748. duk_hstring *h_next;
  45749. h_next = h->hdr.h_next;
  45750. /* Strings may have inner refs (extdata) in some cases. */
  45751. duk_free_hstring(heap, h);
  45752. h = h_next;
  45753. }
  45754. }
  45755. DUK_FREE(heap, strtable);
  45756. }
  45757. /* automatic undefs */
  45758. #undef DUK__GET_STRTABLE
  45759. #undef DUK__HEAPPTR_DEC16
  45760. #undef DUK__HEAPPTR_ENC16
  45761. #undef DUK__STRTAB_U32_MAX_STRLEN
  45762. #line 1 "duk_hobject_alloc.c"
  45763. /*
  45764. * Hobject allocation.
  45765. *
  45766. * Provides primitive allocation functions for all object types (plain object,
  45767. * compiled function, native function, thread). The object return is not yet
  45768. * in "heap allocated" list and has a refcount of zero, so caller must careful.
  45769. */
  45770. /* XXX: In most cases there's no need for plain allocation without pushing
  45771. * to the value stack. Maybe rework contract?
  45772. */
  45773. /* #include duk_internal.h -> already included */
  45774. /*
  45775. * Helpers.
  45776. */
  45777. DUK_LOCAL void duk__init_object_parts(duk_heap *heap, duk_uint_t hobject_flags, duk_hobject *obj) {
  45778. DUK_ASSERT(obj != NULL);
  45779. /* Zeroed by caller. */
  45780. obj->hdr.h_flags = hobject_flags | DUK_HTYPE_OBJECT;
  45781. DUK_ASSERT(DUK_HEAPHDR_GET_TYPE(&obj->hdr) == DUK_HTYPE_OBJECT); /* Assume zero shift. */
  45782. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45783. DUK_HOBJECT_SET_PROTOTYPE(heap, obj, NULL);
  45784. DUK_HOBJECT_SET_PROPS(heap, obj, NULL);
  45785. #endif
  45786. #if defined(DUK_USE_HEAPPTR16)
  45787. /* Zero encoded pointer is required to match NULL. */
  45788. DUK_HEAPHDR_SET_NEXT(heap, &obj->hdr, NULL);
  45789. #if defined(DUK_USE_DOUBLE_LINKED_HEAP)
  45790. DUK_HEAPHDR_SET_PREV(heap, &obj->hdr, NULL);
  45791. #endif
  45792. #endif
  45793. DUK_ASSERT_HEAPHDR_LINKS(heap, &obj->hdr);
  45794. DUK_HEAP_INSERT_INTO_HEAP_ALLOCATED(heap, &obj->hdr);
  45795. /* obj->props is intentionally left as NULL, and duk_hobject_props.c must deal
  45796. * with this properly. This is intentional: empty objects consume a minimum
  45797. * amount of memory. Further, an initial allocation might fail and cause
  45798. * 'obj' to "leak" (require a mark-and-sweep) since it is not reachable yet.
  45799. */
  45800. }
  45801. DUK_LOCAL void *duk__hobject_alloc_init(duk_hthread *thr, duk_uint_t hobject_flags, duk_size_t size) {
  45802. void *res;
  45803. res = (void *) DUK_ALLOC_CHECKED_ZEROED(thr, size);
  45804. DUK_ASSERT(res != NULL);
  45805. duk__init_object_parts(thr->heap, hobject_flags, (duk_hobject *) res);
  45806. return res;
  45807. }
  45808. /*
  45809. * Allocate an duk_hobject.
  45810. *
  45811. * The allocated object has no allocation for properties; the caller may
  45812. * want to force a resize if a desired size is known.
  45813. *
  45814. * The allocated object has zero reference count and is not reachable.
  45815. * The caller MUST make the object reachable and increase its reference
  45816. * count before invoking any operation that might require memory allocation.
  45817. */
  45818. DUK_INTERNAL duk_hobject *duk_hobject_alloc_unchecked(duk_heap *heap, duk_uint_t hobject_flags) {
  45819. duk_hobject *res;
  45820. DUK_ASSERT(heap != NULL);
  45821. /* different memory layout, alloc size, and init */
  45822. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_COMPFUNC) == 0);
  45823. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_NATFUNC) == 0);
  45824. DUK_ASSERT((hobject_flags & DUK_HOBJECT_FLAG_BOUNDFUNC) == 0);
  45825. res = (duk_hobject *) DUK_ALLOC_ZEROED(heap, sizeof(duk_hobject));
  45826. if (DUK_UNLIKELY(res == NULL)) {
  45827. return NULL;
  45828. }
  45829. DUK_ASSERT(!DUK_HOBJECT_IS_THREAD(res));
  45830. duk__init_object_parts(heap, hobject_flags, res);
  45831. DUK_ASSERT(!DUK_HOBJECT_IS_THREAD(res));
  45832. return res;
  45833. }
  45834. DUK_INTERNAL duk_hobject *duk_hobject_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45835. duk_hobject *res;
  45836. res = (duk_hobject *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hobject));
  45837. return res;
  45838. }
  45839. DUK_INTERNAL duk_hcompfunc *duk_hcompfunc_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45840. duk_hcompfunc *res;
  45841. res = (duk_hcompfunc *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hcompfunc));
  45842. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45843. #if defined(DUK_USE_HEAPPTR16)
  45844. /* NULL pointer is required to encode to zero, so memset is enough. */
  45845. #else
  45846. res->data = NULL;
  45847. res->funcs = NULL;
  45848. res->bytecode = NULL;
  45849. #endif
  45850. res->lex_env = NULL;
  45851. res->var_env = NULL;
  45852. #endif
  45853. return res;
  45854. }
  45855. DUK_INTERNAL duk_hnatfunc *duk_hnatfunc_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45856. duk_hnatfunc *res;
  45857. res = (duk_hnatfunc *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hnatfunc));
  45858. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45859. res->func = NULL;
  45860. #endif
  45861. return res;
  45862. }
  45863. DUK_INTERNAL duk_hboundfunc *duk_hboundfunc_alloc(duk_heap *heap, duk_uint_t hobject_flags) {
  45864. duk_hboundfunc *res;
  45865. res = (duk_hboundfunc *) DUK_ALLOC(heap, sizeof(duk_hboundfunc));
  45866. if (!res) {
  45867. return NULL;
  45868. }
  45869. duk_memzero(res, sizeof(duk_hboundfunc));
  45870. duk__init_object_parts(heap, hobject_flags, &res->obj);
  45871. DUK_TVAL_SET_UNDEFINED(&res->target);
  45872. DUK_TVAL_SET_UNDEFINED(&res->this_binding);
  45873. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45874. res->args = NULL;
  45875. #endif
  45876. return res;
  45877. }
  45878. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  45879. DUK_INTERNAL duk_hbufobj *duk_hbufobj_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45880. duk_hbufobj *res;
  45881. res = (duk_hbufobj *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hbufobj));
  45882. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45883. res->buf = NULL;
  45884. res->buf_prop = NULL;
  45885. #endif
  45886. DUK_ASSERT_HBUFOBJ_VALID(res);
  45887. return res;
  45888. }
  45889. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  45890. /* Allocate a new thread.
  45891. *
  45892. * Leaves the built-ins array uninitialized. The caller must either
  45893. * initialize a new global context or share existing built-ins from
  45894. * another thread.
  45895. */
  45896. DUK_INTERNAL duk_hthread *duk_hthread_alloc_unchecked(duk_heap *heap, duk_uint_t hobject_flags) {
  45897. duk_hthread *res;
  45898. res = (duk_hthread *) DUK_ALLOC(heap, sizeof(duk_hthread));
  45899. if (DUK_UNLIKELY(res == NULL)) {
  45900. return NULL;
  45901. }
  45902. duk_memzero(res, sizeof(duk_hthread));
  45903. duk__init_object_parts(heap, hobject_flags, &res->obj);
  45904. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45905. res->ptr_curr_pc = NULL;
  45906. res->heap = NULL;
  45907. res->valstack = NULL;
  45908. res->valstack_end = NULL;
  45909. res->valstack_alloc_end = NULL;
  45910. res->valstack_bottom = NULL;
  45911. res->valstack_top = NULL;
  45912. res->callstack_curr = NULL;
  45913. res->resumer = NULL;
  45914. res->compile_ctx = NULL,
  45915. #if defined(DUK_USE_HEAPPTR16)
  45916. res->strs16 = NULL;
  45917. #else
  45918. res->strs = NULL;
  45919. #endif
  45920. {
  45921. duk_small_uint_t i;
  45922. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  45923. res->builtins[i] = NULL;
  45924. }
  45925. }
  45926. #endif
  45927. /* When nothing is running, API calls are in non-strict mode. */
  45928. DUK_ASSERT(res->strict == 0);
  45929. res->heap = heap;
  45930. /* XXX: Any reason not to merge duk_hthread_alloc.c here? */
  45931. return res;
  45932. }
  45933. DUK_INTERNAL duk_hthread *duk_hthread_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45934. duk_hthread *res;
  45935. res = duk_hthread_alloc_unchecked(thr->heap, hobject_flags);
  45936. if (res == NULL) {
  45937. DUK_ERROR_ALLOC_FAILED(thr);
  45938. DUK_WO_NORETURN(return NULL;);
  45939. }
  45940. return res;
  45941. }
  45942. DUK_INTERNAL duk_harray *duk_harray_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45943. duk_harray *res;
  45944. res = (duk_harray *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_harray));
  45945. DUK_ASSERT(res->length == 0);
  45946. return res;
  45947. }
  45948. DUK_INTERNAL duk_hdecenv *duk_hdecenv_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45949. duk_hdecenv *res;
  45950. res = (duk_hdecenv *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hdecenv));
  45951. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45952. res->thread = NULL;
  45953. res->varmap = NULL;
  45954. #endif
  45955. DUK_ASSERT(res->thread == NULL);
  45956. DUK_ASSERT(res->varmap == NULL);
  45957. DUK_ASSERT(res->regbase_byteoff == 0);
  45958. return res;
  45959. }
  45960. DUK_INTERNAL duk_hobjenv *duk_hobjenv_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45961. duk_hobjenv *res;
  45962. res = (duk_hobjenv *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hobjenv));
  45963. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  45964. res->target = NULL;
  45965. #endif
  45966. DUK_ASSERT(res->target == NULL);
  45967. return res;
  45968. }
  45969. DUK_INTERNAL duk_hproxy *duk_hproxy_alloc(duk_hthread *thr, duk_uint_t hobject_flags) {
  45970. duk_hproxy *res;
  45971. res = (duk_hproxy *) duk__hobject_alloc_init(thr, hobject_flags, sizeof(duk_hproxy));
  45972. /* Leave ->target and ->handler uninitialized, as caller will always
  45973. * explicitly initialize them before any side effects are possible.
  45974. */
  45975. return res;
  45976. }
  45977. #line 1 "duk_hobject_enum.c"
  45978. /*
  45979. * Object enumeration support.
  45980. *
  45981. * Creates an internal enumeration state object to be used e.g. with for-in
  45982. * enumeration. The state object contains a snapshot of target object keys
  45983. * and internal control state for enumeration. Enumerator flags allow caller
  45984. * to e.g. request internal/non-enumerable properties, and to enumerate only
  45985. * "own" properties.
  45986. *
  45987. * Also creates the result value for e.g. Object.keys() based on the same
  45988. * internal structure.
  45989. *
  45990. * This snapshot-based enumeration approach is used to simplify enumeration:
  45991. * non-snapshot-based approaches are difficult to reconcile with mutating
  45992. * the enumeration target, running multiple long-lived enumerators at the
  45993. * same time, garbage collection details, etc. The downside is that the
  45994. * enumerator object is memory inefficient especially for iterating arrays.
  45995. */
  45996. /* #include duk_internal.h -> already included */
  45997. /* XXX: identify enumeration target with an object index (not top of stack) */
  45998. /* First enumerated key index in enumerator object, must match exactly the
  45999. * number of control properties inserted to the enumerator.
  46000. */
  46001. #define DUK__ENUM_START_INDEX 2
  46002. /* Current implementation suffices for ES2015 for now because there's no symbol
  46003. * sorting, so commented out for now.
  46004. */
  46005. /*
  46006. * Helper to sort enumeration keys using a callback for pairwise duk_hstring
  46007. * comparisons. The keys are in the enumeration object entry part, starting
  46008. * from DUK__ENUM_START_INDEX, and the entry part is dense. Entry part values
  46009. * are all "true", e.g. "1" -> true, "3" -> true, "foo" -> true, "2" -> true,
  46010. * so it suffices to just switch keys without switching values.
  46011. *
  46012. * ES2015 [[OwnPropertyKeys]] enumeration order for ordinary objects:
  46013. * (1) array indices in ascending order,
  46014. * (2) non-array-index keys in insertion order, and
  46015. * (3) symbols in insertion order.
  46016. * http://www.ecma-international.org/ecma-262/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys.
  46017. *
  46018. * This rule is applied to "own properties" at each inheritance level;
  46019. * non-duplicate parent keys always follow child keys. For example,
  46020. * an inherited array index will enumerate -after- a symbol in the
  46021. * child.
  46022. *
  46023. * Insertion sort is used because (1) it's simple and compact, (2) works
  46024. * in-place, (3) minimizes operations if data is already nearly sorted,
  46025. * (4) doesn't reorder elements considered equal.
  46026. * http://en.wikipedia.org/wiki/Insertion_sort
  46027. */
  46028. /* Sort key, must hold array indices, "not array index" marker, and one more
  46029. * higher value for symbols.
  46030. */
  46031. #if !defined(DUK_USE_SYMBOL_BUILTIN)
  46032. typedef duk_uint32_t duk__sort_key_t;
  46033. #elif defined(DUK_USE_64BIT_OPS)
  46034. typedef duk_uint64_t duk__sort_key_t;
  46035. #else
  46036. typedef duk_double_t duk__sort_key_t;
  46037. #endif
  46038. /* Get sort key for a duk_hstring. */
  46039. DUK_LOCAL duk__sort_key_t duk__hstring_sort_key(duk_hstring *x) {
  46040. duk__sort_key_t val;
  46041. /* For array indices [0,0xfffffffe] use the array index as is.
  46042. * For strings, use 0xffffffff, the marker 'arridx' already in
  46043. * duk_hstring. For symbols, any value above 0xffffffff works,
  46044. * as long as it is the same for all symbols; currently just add
  46045. * the masked flag field into the arridx temporary.
  46046. */
  46047. DUK_ASSERT(x != NULL);
  46048. DUK_ASSERT(!DUK_HSTRING_HAS_SYMBOL(x) || DUK_HSTRING_GET_ARRIDX_FAST(x) == DUK_HSTRING_NO_ARRAY_INDEX);
  46049. val = (duk__sort_key_t) DUK_HSTRING_GET_ARRIDX_FAST(x);
  46050. #if defined(DUK_USE_SYMBOL_BUILTIN)
  46051. val = val + (duk__sort_key_t) (DUK_HEAPHDR_GET_FLAGS_RAW((duk_heaphdr *) x) & DUK_HSTRING_FLAG_SYMBOL);
  46052. #endif
  46053. return (duk__sort_key_t) val;
  46054. }
  46055. /* Insert element 'b' after element 'a'? */
  46056. DUK_LOCAL duk_bool_t duk__sort_compare_es6(duk_hstring *a, duk_hstring *b, duk__sort_key_t val_b) {
  46057. duk__sort_key_t val_a;
  46058. DUK_ASSERT(a != NULL);
  46059. DUK_ASSERT(b != NULL);
  46060. DUK_UNREF(b); /* Not actually needed now, val_b suffices. */
  46061. val_a = duk__hstring_sort_key(a);
  46062. if (val_a > val_b) {
  46063. return 0;
  46064. } else {
  46065. return 1;
  46066. }
  46067. }
  46068. DUK_LOCAL void duk__sort_enum_keys_es6(duk_hthread *thr, duk_hobject *h_obj, duk_int_fast32_t idx_start, duk_int_fast32_t idx_end) {
  46069. duk_hstring **keys;
  46070. duk_int_fast32_t idx;
  46071. DUK_ASSERT(h_obj != NULL);
  46072. DUK_ASSERT(idx_start >= DUK__ENUM_START_INDEX);
  46073. DUK_ASSERT(idx_end >= idx_start);
  46074. DUK_UNREF(thr);
  46075. if (idx_end <= idx_start + 1) {
  46076. return; /* Zero or one element(s). */
  46077. }
  46078. keys = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, h_obj);
  46079. for (idx = idx_start + 1; idx < idx_end; idx++) {
  46080. duk_hstring *h_curr;
  46081. duk_int_fast32_t idx_insert;
  46082. duk__sort_key_t val_curr;
  46083. h_curr = keys[idx];
  46084. DUK_ASSERT(h_curr != NULL);
  46085. /* Scan backwards for insertion place. This works very well
  46086. * when the elements are nearly in order which is the common
  46087. * (and optimized for) case.
  46088. */
  46089. val_curr = duk__hstring_sort_key(h_curr); /* Remains same during scanning. */
  46090. for (idx_insert = idx - 1; idx_insert >= idx_start; idx_insert--) {
  46091. duk_hstring *h_insert;
  46092. h_insert = keys[idx_insert];
  46093. DUK_ASSERT(h_insert != NULL);
  46094. if (duk__sort_compare_es6(h_insert, h_curr, val_curr)) {
  46095. break;
  46096. }
  46097. }
  46098. /* If we're out of indices, idx_insert == idx_start - 1 and idx_insert++
  46099. * brings us back to idx_start.
  46100. */
  46101. idx_insert++;
  46102. DUK_ASSERT(idx_insert >= 0 && idx_insert <= idx);
  46103. /* .-- p_insert .-- p_curr
  46104. * v v
  46105. * | ... | insert | ... | curr
  46106. */
  46107. /* This could also done when the keys are in order, i.e.
  46108. * idx_insert == idx. The result would be an unnecessary
  46109. * memmove() but we use an explicit check because the keys
  46110. * are very often in order already.
  46111. */
  46112. if (idx != idx_insert) {
  46113. duk_memmove((void *) (keys + idx_insert + 1),
  46114. (const void *) (keys + idx_insert),
  46115. ((size_t) (idx - idx_insert) * sizeof(duk_hstring *)));
  46116. keys[idx_insert] = h_curr;
  46117. }
  46118. }
  46119. }
  46120. /*
  46121. * Create an internal enumerator object E, which has its keys ordered
  46122. * to match desired enumeration ordering. Also initialize internal control
  46123. * properties for enumeration.
  46124. *
  46125. * Note: if an array was used to hold enumeration keys instead, an array
  46126. * scan would be needed to eliminate duplicates found in the prototype chain.
  46127. */
  46128. DUK_LOCAL void duk__add_enum_key(duk_hthread *thr, duk_hstring *k) {
  46129. /* 'k' may be unreachable on entry so must push without any
  46130. * potential for GC.
  46131. */
  46132. duk_push_hstring(thr, k);
  46133. duk_push_true(thr);
  46134. duk_put_prop(thr, -3);
  46135. }
  46136. DUK_LOCAL void duk__add_enum_key_stridx(duk_hthread *thr, duk_small_uint_t stridx) {
  46137. duk__add_enum_key(thr, DUK_HTHREAD_GET_STRING(thr, stridx));
  46138. }
  46139. DUK_INTERNAL void duk_hobject_enumerator_create(duk_hthread *thr, duk_small_uint_t enum_flags) {
  46140. duk_hobject *enum_target;
  46141. duk_hobject *curr;
  46142. duk_hobject *res;
  46143. #if defined(DUK_USE_ES6_PROXY)
  46144. duk_hobject *h_proxy_target;
  46145. duk_hobject *h_proxy_handler;
  46146. duk_hobject *h_trap_result;
  46147. #endif
  46148. duk_uint_fast32_t i, len; /* used for array, stack, and entry indices */
  46149. duk_uint_fast32_t sort_start_index;
  46150. DUK_ASSERT(thr != NULL);
  46151. enum_target = duk_require_hobject(thr, -1);
  46152. DUK_ASSERT(enum_target != NULL);
  46153. duk_push_bare_object(thr);
  46154. res = duk_known_hobject(thr, -1);
  46155. /* [enum_target res] */
  46156. /* Target must be stored so that we can recheck whether or not
  46157. * keys still exist when we enumerate. This is not done if the
  46158. * enumeration result comes from a proxy trap as there is no
  46159. * real object to check against.
  46160. */
  46161. duk_push_hobject(thr, enum_target);
  46162. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_INT_TARGET);
  46163. /* Initialize index so that we skip internal control keys. */
  46164. duk_push_int(thr, DUK__ENUM_START_INDEX);
  46165. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_INT_NEXT);
  46166. /*
  46167. * Proxy object handling
  46168. */
  46169. #if defined(DUK_USE_ES6_PROXY)
  46170. if (DUK_LIKELY((enum_flags & DUK_ENUM_NO_PROXY_BEHAVIOR) != 0)) {
  46171. goto skip_proxy;
  46172. }
  46173. if (DUK_LIKELY(!duk_hobject_proxy_check(enum_target,
  46174. &h_proxy_target,
  46175. &h_proxy_handler))) {
  46176. goto skip_proxy;
  46177. }
  46178. /* XXX: share code with Object.keys() Proxy handling */
  46179. /* In ES2015 for-in invoked the "enumerate" trap; in ES2016 "enumerate"
  46180. * has been obsoleted and "ownKeys" is used instead.
  46181. */
  46182. DUK_DDD(DUK_DDDPRINT("proxy enumeration"));
  46183. duk_push_hobject(thr, h_proxy_handler);
  46184. if (!duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_OWN_KEYS)) {
  46185. /* No need to replace the 'enum_target' value in stack, only the
  46186. * enum_target reference. This also ensures that the original
  46187. * enum target is reachable, which keeps the proxy and the proxy
  46188. * target reachable. We do need to replace the internal _Target.
  46189. */
  46190. DUK_DDD(DUK_DDDPRINT("no ownKeys trap, enumerate proxy target instead"));
  46191. DUK_DDD(DUK_DDDPRINT("h_proxy_target=%!O", (duk_heaphdr *) h_proxy_target));
  46192. enum_target = h_proxy_target;
  46193. duk_push_hobject(thr, enum_target); /* -> [ ... enum_target res handler undefined target ] */
  46194. duk_put_prop_stridx_short(thr, -4, DUK_STRIDX_INT_TARGET);
  46195. duk_pop_2(thr); /* -> [ ... enum_target res ] */
  46196. goto skip_proxy;
  46197. }
  46198. /* [ ... enum_target res handler trap ] */
  46199. duk_insert(thr, -2);
  46200. duk_push_hobject(thr, h_proxy_target); /* -> [ ... enum_target res trap handler target ] */
  46201. duk_call_method(thr, 1 /*nargs*/); /* -> [ ... enum_target res trap_result ] */
  46202. h_trap_result = duk_require_hobject(thr, -1);
  46203. DUK_UNREF(h_trap_result);
  46204. duk_proxy_ownkeys_postprocess(thr, h_proxy_target, enum_flags);
  46205. /* -> [ ... enum_target res trap_result keys_array ] */
  46206. /* Copy cleaned up trap result keys into the enumerator object. */
  46207. /* XXX: result is a dense array; could make use of that. */
  46208. DUK_ASSERT(duk_is_array(thr, -1));
  46209. len = (duk_uint_fast32_t) duk_get_length(thr, -1);
  46210. for (i = 0; i < len; i++) {
  46211. (void) duk_get_prop_index(thr, -1, (duk_uarridx_t) i);
  46212. DUK_ASSERT(duk_is_string(thr, -1)); /* postprocess cleaned up */
  46213. /* [ ... enum_target res trap_result keys_array val ] */
  46214. duk_push_true(thr);
  46215. /* [ ... enum_target res trap_result keys_array val true ] */
  46216. duk_put_prop(thr, -5);
  46217. }
  46218. /* [ ... enum_target res trap_result keys_array ] */
  46219. duk_pop_2(thr);
  46220. duk_remove_m2(thr);
  46221. /* [ ... res ] */
  46222. /* The internal _Target property is kept pointing to the original
  46223. * enumeration target (the proxy object), so that the enumerator
  46224. * 'next' operation can read property values if so requested. The
  46225. * fact that the _Target is a proxy disables key existence check
  46226. * during enumeration.
  46227. */
  46228. DUK_DDD(DUK_DDDPRINT("proxy enumeration, final res: %!O", (duk_heaphdr *) res));
  46229. goto compact_and_return;
  46230. skip_proxy:
  46231. #endif /* DUK_USE_ES6_PROXY */
  46232. curr = enum_target;
  46233. sort_start_index = DUK__ENUM_START_INDEX;
  46234. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(res) == DUK__ENUM_START_INDEX);
  46235. while (curr) {
  46236. duk_uint_fast32_t sort_end_index;
  46237. #if !defined(DUK_USE_PREFER_SIZE)
  46238. duk_bool_t need_sort = 0;
  46239. #endif
  46240. /* Enumeration proceeds by inheritance level. Virtual
  46241. * properties need to be handled specially, followed by
  46242. * array part, and finally entry part.
  46243. *
  46244. * If there are array index keys in the entry part or any
  46245. * other risk of the ES2015 [[OwnPropertyKeys]] order being
  46246. * violated, need_sort is set and an explicit ES2015 sort is
  46247. * done for the inheritance level.
  46248. */
  46249. /* XXX: inheriting from proxy */
  46250. /*
  46251. * Virtual properties.
  46252. *
  46253. * String and buffer indices are virtual and always enumerable,
  46254. * 'length' is virtual and non-enumerable. Array and arguments
  46255. * object props have special behavior but are concrete.
  46256. *
  46257. * String and buffer objects don't have an array part so as long
  46258. * as virtual array index keys are enumerated first, we don't
  46259. * need to set need_sort.
  46260. */
  46261. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  46262. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr) || DUK_HOBJECT_IS_BUFOBJ(curr)) {
  46263. #else
  46264. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr)) {
  46265. #endif
  46266. duk_bool_t have_length = 1;
  46267. /* String and buffer enumeration behavior is identical now,
  46268. * so use shared handler.
  46269. */
  46270. if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(curr)) {
  46271. duk_hstring *h_val;
  46272. h_val = duk_hobject_get_internal_value_string(thr->heap, curr);
  46273. DUK_ASSERT(h_val != NULL); /* string objects must not created without internal value */
  46274. len = (duk_uint_fast32_t) DUK_HSTRING_GET_CHARLEN(h_val);
  46275. }
  46276. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  46277. else {
  46278. duk_hbufobj *h_bufobj;
  46279. DUK_ASSERT(DUK_HOBJECT_IS_BUFOBJ(curr));
  46280. h_bufobj = (duk_hbufobj *) curr;
  46281. if (h_bufobj == NULL || !h_bufobj->is_typedarray) {
  46282. /* Zero length seems like a good behavior for neutered buffers.
  46283. * ArrayBuffer (non-view) and DataView don't have index properties
  46284. * or .length property.
  46285. */
  46286. len = 0;
  46287. have_length = 0;
  46288. } else {
  46289. /* There's intentionally no check for
  46290. * current underlying buffer length.
  46291. */
  46292. len = (duk_uint_fast32_t) (h_bufobj->length >> h_bufobj->shift);
  46293. }
  46294. }
  46295. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  46296. for (i = 0; i < len; i++) {
  46297. duk_hstring *k;
  46298. /* This is a bit fragile: the string is not
  46299. * reachable until it is pushed by the helper.
  46300. */
  46301. k = duk_heap_strtable_intern_u32_checked(thr, (duk_uint32_t) i);
  46302. DUK_ASSERT(k);
  46303. duk__add_enum_key(thr, k);
  46304. /* [enum_target res] */
  46305. }
  46306. /* 'length' and other virtual properties are not
  46307. * enumerable, but are included if non-enumerable
  46308. * properties are requested.
  46309. */
  46310. if (have_length && (enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE)) {
  46311. duk__add_enum_key_stridx(thr, DUK_STRIDX_LENGTH);
  46312. }
  46313. }
  46314. /*
  46315. * Array part
  46316. */
  46317. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ASIZE(curr); i++) {
  46318. duk_hstring *k;
  46319. duk_tval *tv;
  46320. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, curr, i);
  46321. if (DUK_TVAL_IS_UNUSED(tv)) {
  46322. continue;
  46323. }
  46324. k = duk_heap_strtable_intern_u32_checked(thr, (duk_uint32_t) i); /* Fragile reachability. */
  46325. DUK_ASSERT(k);
  46326. duk__add_enum_key(thr, k);
  46327. /* [enum_target res] */
  46328. }
  46329. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(curr)) {
  46330. /* Array .length comes after numeric indices. */
  46331. if (enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE) {
  46332. duk__add_enum_key_stridx(thr, DUK_STRIDX_LENGTH);
  46333. }
  46334. }
  46335. /*
  46336. * Entries part
  46337. */
  46338. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(curr); i++) {
  46339. duk_hstring *k;
  46340. k = DUK_HOBJECT_E_GET_KEY(thr->heap, curr, i);
  46341. if (!k) {
  46342. continue;
  46343. }
  46344. if (!(enum_flags & DUK_ENUM_INCLUDE_NONENUMERABLE) &&
  46345. !DUK_HOBJECT_E_SLOT_IS_ENUMERABLE(thr->heap, curr, i)) {
  46346. continue;
  46347. }
  46348. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(k))) {
  46349. if (!(enum_flags & DUK_ENUM_INCLUDE_HIDDEN) &&
  46350. DUK_HSTRING_HAS_HIDDEN(k)) {
  46351. continue;
  46352. }
  46353. if (!(enum_flags & DUK_ENUM_INCLUDE_SYMBOLS)) {
  46354. continue;
  46355. }
  46356. #if !defined(DUK_USE_PREFER_SIZE)
  46357. need_sort = 1;
  46358. #endif
  46359. } else {
  46360. DUK_ASSERT(!DUK_HSTRING_HAS_HIDDEN(k)); /* would also have symbol flag */
  46361. if (enum_flags & DUK_ENUM_EXCLUDE_STRINGS) {
  46362. continue;
  46363. }
  46364. }
  46365. if (DUK_HSTRING_HAS_ARRIDX(k)) {
  46366. /* This in currently only possible if the
  46367. * object has no array part: the array part
  46368. * is exhaustive when it is present.
  46369. */
  46370. #if !defined(DUK_USE_PREFER_SIZE)
  46371. need_sort = 1;
  46372. #endif
  46373. } else {
  46374. if (enum_flags & DUK_ENUM_ARRAY_INDICES_ONLY) {
  46375. continue;
  46376. }
  46377. }
  46378. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, curr, i) ||
  46379. !DUK_TVAL_IS_UNUSED(&DUK_HOBJECT_E_GET_VALUE_PTR(thr->heap, curr, i)->v));
  46380. duk__add_enum_key(thr, k);
  46381. /* [enum_target res] */
  46382. }
  46383. /* Sort enumerated keys according to ES2015 requirements for
  46384. * the "inheritance level" just processed. This is far from
  46385. * optimal, ES2015 semantics could be achieved more efficiently
  46386. * by handling array index string keys (and symbol keys)
  46387. * specially above in effect doing the sort inline.
  46388. *
  46389. * Skip the sort if array index sorting is requested because
  46390. * we must consider all keys, also inherited, so an explicit
  46391. * sort is done for the whole result after we're done with the
  46392. * prototype chain.
  46393. *
  46394. * Also skip the sort if need_sort == 0, i.e. we know for
  46395. * certain that the enumerated order is already correct.
  46396. */
  46397. sort_end_index = DUK_HOBJECT_GET_ENEXT(res);
  46398. if (!(enum_flags & DUK_ENUM_SORT_ARRAY_INDICES)) {
  46399. #if defined(DUK_USE_PREFER_SIZE)
  46400. duk__sort_enum_keys_es6(thr, res, (duk_int_fast32_t) sort_start_index, (duk_int_fast32_t) sort_end_index);
  46401. #else
  46402. if (need_sort) {
  46403. DUK_DDD(DUK_DDDPRINT("need to sort"));
  46404. duk__sort_enum_keys_es6(thr, res, (duk_int_fast32_t) sort_start_index, (duk_int_fast32_t) sort_end_index);
  46405. } else {
  46406. DUK_DDD(DUK_DDDPRINT("no need to sort"));
  46407. }
  46408. #endif
  46409. }
  46410. sort_start_index = sort_end_index;
  46411. if (enum_flags & DUK_ENUM_OWN_PROPERTIES_ONLY) {
  46412. break;
  46413. }
  46414. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  46415. }
  46416. /* [enum_target res] */
  46417. duk_remove_m2(thr);
  46418. /* [res] */
  46419. if (enum_flags & DUK_ENUM_SORT_ARRAY_INDICES) {
  46420. /* Some E5/E5.1 algorithms require that array indices are iterated
  46421. * in a strictly ascending order. This is the case for e.g.
  46422. * Array.prototype.forEach() and JSON.stringify() PropertyList
  46423. * handling. The caller can request an explicit sort in these
  46424. * cases.
  46425. */
  46426. /* Sort to ES2015 order which works for pure array incides but
  46427. * also for mixed keys.
  46428. */
  46429. duk__sort_enum_keys_es6(thr, res, (duk_int_fast32_t) DUK__ENUM_START_INDEX, (duk_int_fast32_t) DUK_HOBJECT_GET_ENEXT(res));
  46430. }
  46431. #if defined(DUK_USE_ES6_PROXY)
  46432. compact_and_return:
  46433. #endif
  46434. /* compact; no need to seal because object is internal */
  46435. duk_hobject_compact_props(thr, res);
  46436. DUK_DDD(DUK_DDDPRINT("created enumerator object: %!iT", (duk_tval *) duk_get_tval(thr, -1)));
  46437. }
  46438. /*
  46439. * Returns non-zero if a key and/or value was enumerated, and:
  46440. *
  46441. * [enum] -> [key] (get_value == 0)
  46442. * [enum] -> [key value] (get_value == 1)
  46443. *
  46444. * Returns zero without pushing anything on the stack otherwise.
  46445. */
  46446. DUK_INTERNAL duk_bool_t duk_hobject_enumerator_next(duk_hthread *thr, duk_bool_t get_value) {
  46447. duk_hobject *e;
  46448. duk_hobject *enum_target;
  46449. duk_hstring *res = NULL;
  46450. duk_uint_fast32_t idx;
  46451. duk_bool_t check_existence;
  46452. DUK_ASSERT(thr != NULL);
  46453. /* [... enum] */
  46454. e = duk_require_hobject(thr, -1);
  46455. /* XXX use get tval ptr, more efficient */
  46456. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_NEXT);
  46457. idx = (duk_uint_fast32_t) duk_require_uint(thr, -1);
  46458. duk_pop(thr);
  46459. DUK_DDD(DUK_DDDPRINT("enumeration: index is: %ld", (long) idx));
  46460. /* Enumeration keys are checked against the enumeration target (to see
  46461. * that they still exist). In the proxy enumeration case _Target will
  46462. * be the proxy, and checking key existence against the proxy is not
  46463. * required (or sensible, as the keys may be fully virtual).
  46464. */
  46465. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_TARGET);
  46466. enum_target = duk_require_hobject(thr, -1);
  46467. DUK_ASSERT(enum_target != NULL);
  46468. #if defined(DUK_USE_ES6_PROXY)
  46469. check_existence = (!DUK_HOBJECT_IS_PROXY(enum_target));
  46470. #else
  46471. check_existence = 1;
  46472. #endif
  46473. duk_pop(thr); /* still reachable */
  46474. DUK_DDD(DUK_DDDPRINT("getting next enum value, enum_target=%!iO, enumerator=%!iT",
  46475. (duk_heaphdr *) enum_target, (duk_tval *) duk_get_tval(thr, -1)));
  46476. /* no array part */
  46477. for (;;) {
  46478. duk_hstring *k;
  46479. if (idx >= DUK_HOBJECT_GET_ENEXT(e)) {
  46480. DUK_DDD(DUK_DDDPRINT("enumeration: ran out of elements"));
  46481. break;
  46482. }
  46483. /* we know these because enum objects are internally created */
  46484. k = DUK_HOBJECT_E_GET_KEY(thr->heap, e, idx);
  46485. DUK_ASSERT(k != NULL);
  46486. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, e, idx));
  46487. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(&DUK_HOBJECT_E_GET_VALUE(thr->heap, e, idx).v));
  46488. idx++;
  46489. /* recheck that the property still exists */
  46490. if (check_existence && !duk_hobject_hasprop_raw(thr, enum_target, k)) {
  46491. DUK_DDD(DUK_DDDPRINT("property deleted during enumeration, skip"));
  46492. continue;
  46493. }
  46494. DUK_DDD(DUK_DDDPRINT("enumeration: found element, key: %!O", (duk_heaphdr *) k));
  46495. res = k;
  46496. break;
  46497. }
  46498. DUK_DDD(DUK_DDDPRINT("enumeration: updating next index to %ld", (long) idx));
  46499. duk_push_u32(thr, (duk_uint32_t) idx);
  46500. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_INT_NEXT);
  46501. /* [... enum] */
  46502. if (res) {
  46503. duk_push_hstring(thr, res);
  46504. if (get_value) {
  46505. duk_push_hobject(thr, enum_target);
  46506. duk_dup_m2(thr); /* -> [... enum key enum_target key] */
  46507. duk_get_prop(thr, -2); /* -> [... enum key enum_target val] */
  46508. duk_remove_m2(thr); /* -> [... enum key val] */
  46509. duk_remove(thr, -3); /* -> [... key val] */
  46510. } else {
  46511. duk_remove_m2(thr); /* -> [... key] */
  46512. }
  46513. return 1;
  46514. } else {
  46515. duk_pop(thr); /* -> [...] */
  46516. return 0;
  46517. }
  46518. }
  46519. /*
  46520. * Get enumerated keys in an ECMAScript array. Matches Object.keys() behavior
  46521. * described in E5 Section 15.2.3.14.
  46522. */
  46523. DUK_INTERNAL duk_ret_t duk_hobject_get_enumerated_keys(duk_hthread *thr, duk_small_uint_t enum_flags) {
  46524. duk_hobject *e;
  46525. duk_hstring **keys;
  46526. duk_tval *tv;
  46527. duk_uint_fast32_t count;
  46528. DUK_ASSERT(thr != NULL);
  46529. DUK_ASSERT(duk_get_hobject(thr, -1) != NULL);
  46530. /* Create a temporary enumerator to get the (non-duplicated) key list;
  46531. * the enumerator state is initialized without being needed, but that
  46532. * has little impact.
  46533. */
  46534. duk_hobject_enumerator_create(thr, enum_flags);
  46535. e = duk_known_hobject(thr, -1);
  46536. /* [enum_target enum res] */
  46537. /* Create dense result array to exact size. */
  46538. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(e) >= DUK__ENUM_START_INDEX);
  46539. count = (duk_uint32_t) (DUK_HOBJECT_GET_ENEXT(e) - DUK__ENUM_START_INDEX);
  46540. /* XXX: uninit would be OK */
  46541. tv = duk_push_harray_with_size_outptr(thr, (duk_uint32_t) count);
  46542. DUK_ASSERT(count == 0 || tv != NULL);
  46543. /* Fill result array, no side effects. */
  46544. keys = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, e);
  46545. keys += DUK__ENUM_START_INDEX;
  46546. while (count-- > 0) {
  46547. duk_hstring *k;
  46548. k = *keys++;
  46549. DUK_ASSERT(k != NULL); /* enumerator must have no keys deleted */
  46550. DUK_TVAL_SET_STRING(tv, k);
  46551. tv++;
  46552. DUK_HSTRING_INCREF(thr, k);
  46553. }
  46554. /* [enum_target enum res] */
  46555. duk_remove_m2(thr);
  46556. /* [enum_target res] */
  46557. return 1; /* return 1 to allow callers to tail call */
  46558. }
  46559. /* automatic undefs */
  46560. #undef DUK__ENUM_START_INDEX
  46561. #line 1 "duk_hobject_misc.c"
  46562. /*
  46563. * Misc support functions
  46564. */
  46565. /* #include duk_internal.h -> already included */
  46566. DUK_INTERNAL duk_bool_t duk_hobject_prototype_chain_contains(duk_hthread *thr, duk_hobject *h, duk_hobject *p, duk_bool_t ignore_loop) {
  46567. duk_uint_t sanity;
  46568. DUK_ASSERT(thr != NULL);
  46569. /* False if the object is NULL or the prototype 'p' is NULL.
  46570. * In particular, false if both are NULL (don't compare equal).
  46571. */
  46572. if (h == NULL || p == NULL) {
  46573. return 0;
  46574. }
  46575. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  46576. do {
  46577. if (h == p) {
  46578. return 1;
  46579. }
  46580. if (sanity-- == 0) {
  46581. if (ignore_loop) {
  46582. break;
  46583. } else {
  46584. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  46585. DUK_WO_NORETURN(return 0;);
  46586. }
  46587. }
  46588. h = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  46589. } while (h);
  46590. return 0;
  46591. }
  46592. DUK_INTERNAL void duk_hobject_set_prototype_updref(duk_hthread *thr, duk_hobject *h, duk_hobject *p) {
  46593. #if defined(DUK_USE_REFERENCE_COUNTING)
  46594. duk_hobject *tmp;
  46595. DUK_ASSERT(h);
  46596. tmp = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h);
  46597. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p);
  46598. DUK_HOBJECT_INCREF_ALLOWNULL(thr, p); /* avoid problems if p == h->prototype */
  46599. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  46600. #else
  46601. DUK_ASSERT(h);
  46602. DUK_UNREF(thr);
  46603. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, h, p);
  46604. #endif
  46605. }
  46606. #line 1 "duk_hobject_pc2line.c"
  46607. /*
  46608. * Helpers for creating and querying pc2line debug data, which
  46609. * converts a bytecode program counter to a source line number.
  46610. *
  46611. * The run-time pc2line data is bit-packed, and documented in:
  46612. *
  46613. * doc/function-objects.rst
  46614. */
  46615. /* #include duk_internal.h -> already included */
  46616. #if defined(DUK_USE_PC2LINE)
  46617. /* Generate pc2line data for an instruction sequence, leaving a buffer on stack top. */
  46618. DUK_INTERNAL void duk_hobject_pc2line_pack(duk_hthread *thr, duk_compiler_instr *instrs, duk_uint_fast32_t length) {
  46619. duk_hbuffer_dynamic *h_buf;
  46620. duk_bitencoder_ctx be_ctx_alloc;
  46621. duk_bitencoder_ctx *be_ctx = &be_ctx_alloc;
  46622. duk_uint32_t *hdr;
  46623. duk_size_t new_size;
  46624. duk_uint_fast32_t num_header_entries;
  46625. duk_uint_fast32_t curr_offset;
  46626. duk_int_fast32_t curr_line, next_line, diff_line;
  46627. duk_uint_fast32_t curr_pc;
  46628. duk_uint_fast32_t hdr_index;
  46629. DUK_ASSERT(length <= DUK_COMPILER_MAX_BYTECODE_LENGTH);
  46630. num_header_entries = (length + DUK_PC2LINE_SKIP - 1) / DUK_PC2LINE_SKIP;
  46631. curr_offset = (duk_uint_fast32_t) (sizeof(duk_uint32_t) + num_header_entries * sizeof(duk_uint32_t) * 2);
  46632. duk_push_dynamic_buffer(thr, (duk_size_t) curr_offset);
  46633. h_buf = (duk_hbuffer_dynamic *) duk_known_hbuffer(thr, -1);
  46634. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf) && !DUK_HBUFFER_HAS_EXTERNAL(h_buf));
  46635. hdr = (duk_uint32_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  46636. DUK_ASSERT(hdr != NULL);
  46637. hdr[0] = (duk_uint32_t) length; /* valid pc range is [0, length[ */
  46638. curr_pc = 0U;
  46639. while (curr_pc < length) {
  46640. new_size = (duk_size_t) (curr_offset + DUK_PC2LINE_MAX_DIFF_LENGTH);
  46641. duk_hbuffer_resize(thr, h_buf, new_size);
  46642. hdr = (duk_uint32_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, h_buf);
  46643. DUK_ASSERT(hdr != NULL);
  46644. DUK_ASSERT(curr_pc < length);
  46645. hdr_index = 1 + (curr_pc / DUK_PC2LINE_SKIP) * 2;
  46646. curr_line = (duk_int_fast32_t) instrs[curr_pc].line;
  46647. hdr[hdr_index + 0] = (duk_uint32_t) curr_line;
  46648. hdr[hdr_index + 1] = (duk_uint32_t) curr_offset;
  46649. #if 0
  46650. DUK_DDD(DUK_DDDPRINT("hdr[%ld]: pc=%ld line=%ld offset=%ld",
  46651. (long) (curr_pc / DUK_PC2LINE_SKIP),
  46652. (long) curr_pc,
  46653. (long) hdr[hdr_index + 0],
  46654. (long) hdr[hdr_index + 1]));
  46655. #endif
  46656. duk_memzero(be_ctx, sizeof(*be_ctx));
  46657. be_ctx->data = ((duk_uint8_t *) hdr) + curr_offset;
  46658. be_ctx->length = (duk_size_t) DUK_PC2LINE_MAX_DIFF_LENGTH;
  46659. for (;;) {
  46660. curr_pc++;
  46661. if ( ((curr_pc % DUK_PC2LINE_SKIP) == 0) || /* end of diff run */
  46662. (curr_pc >= length) ) { /* end of bytecode */
  46663. break;
  46664. }
  46665. DUK_ASSERT(curr_pc < length);
  46666. next_line = (duk_int32_t) instrs[curr_pc].line;
  46667. diff_line = next_line - curr_line;
  46668. #if 0
  46669. DUK_DDD(DUK_DDDPRINT("curr_line=%ld, next_line=%ld -> diff_line=%ld",
  46670. (long) curr_line, (long) next_line, (long) diff_line));
  46671. #endif
  46672. if (diff_line == 0) {
  46673. /* 0 */
  46674. duk_be_encode(be_ctx, 0, 1);
  46675. } else if (diff_line >= 1 && diff_line <= 4) {
  46676. /* 1 0 <2 bits> */
  46677. duk_be_encode(be_ctx, (duk_uint32_t) ((0x02 << 2) + (diff_line - 1)), 4);
  46678. } else if (diff_line >= -0x80 && diff_line <= 0x7f) {
  46679. /* 1 1 0 <8 bits> */
  46680. DUK_ASSERT(diff_line + 0x80 >= 0 && diff_line + 0x80 <= 0xff);
  46681. duk_be_encode(be_ctx, (duk_uint32_t) ((0x06 << 8) + (diff_line + 0x80)), 11);
  46682. } else {
  46683. /* 1 1 1 <32 bits>
  46684. * Encode in two parts to avoid bitencode 24-bit limitation
  46685. */
  46686. duk_be_encode(be_ctx, (duk_uint32_t) ((0x07 << 16) + ((next_line >> 16) & 0xffff)), 19);
  46687. duk_be_encode(be_ctx, (duk_uint32_t) (next_line & 0xffff), 16);
  46688. }
  46689. curr_line = next_line;
  46690. }
  46691. duk_be_finish(be_ctx);
  46692. DUK_ASSERT(!be_ctx->truncated);
  46693. /* be_ctx->offset == length of encoded bitstream */
  46694. curr_offset += (duk_uint_fast32_t) be_ctx->offset;
  46695. }
  46696. /* compact */
  46697. new_size = (duk_size_t) curr_offset;
  46698. duk_hbuffer_resize(thr, h_buf, new_size);
  46699. (void) duk_to_fixed_buffer(thr, -1, NULL);
  46700. DUK_DDD(DUK_DDDPRINT("final pc2line data: pc_limit=%ld, length=%ld, %lf bits/opcode --> %!ixT",
  46701. (long) length, (long) new_size, (double) new_size * 8.0 / (double) length,
  46702. (duk_tval *) duk_get_tval(thr, -1)));
  46703. }
  46704. /* PC is unsigned. If caller does PC arithmetic and gets a negative result,
  46705. * it will map to a large PC which is out of bounds and causes a zero to be
  46706. * returned.
  46707. */
  46708. DUK_LOCAL duk_uint_fast32_t duk__hobject_pc2line_query_raw(duk_hthread *thr, duk_hbuffer_fixed *buf, duk_uint_fast32_t pc) {
  46709. duk_bitdecoder_ctx bd_ctx_alloc;
  46710. duk_bitdecoder_ctx *bd_ctx = &bd_ctx_alloc;
  46711. duk_uint32_t *hdr;
  46712. duk_uint_fast32_t start_offset;
  46713. duk_uint_fast32_t pc_limit;
  46714. duk_uint_fast32_t hdr_index;
  46715. duk_uint_fast32_t pc_base;
  46716. duk_uint_fast32_t n;
  46717. duk_uint_fast32_t curr_line;
  46718. DUK_ASSERT(buf != NULL);
  46719. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) buf) && !DUK_HBUFFER_HAS_EXTERNAL((duk_hbuffer *) buf));
  46720. DUK_UNREF(thr);
  46721. /*
  46722. * Use the index in the header to find the right starting point
  46723. */
  46724. hdr_index = pc / DUK_PC2LINE_SKIP;
  46725. pc_base = hdr_index * DUK_PC2LINE_SKIP;
  46726. n = pc - pc_base;
  46727. if (DUK_HBUFFER_FIXED_GET_SIZE(buf) <= sizeof(duk_uint32_t)) {
  46728. DUK_DD(DUK_DDPRINT("pc2line lookup failed: buffer is smaller than minimal header"));
  46729. goto pc2line_error;
  46730. }
  46731. hdr = (duk_uint32_t *) (void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, buf);
  46732. pc_limit = hdr[0];
  46733. if (pc >= pc_limit) {
  46734. /* Note: pc is unsigned and cannot be negative */
  46735. DUK_DD(DUK_DDPRINT("pc2line lookup failed: pc out of bounds (pc=%ld, limit=%ld)",
  46736. (long) pc, (long) pc_limit));
  46737. goto pc2line_error;
  46738. }
  46739. curr_line = hdr[1 + hdr_index * 2];
  46740. start_offset = hdr[1 + hdr_index * 2 + 1];
  46741. if ((duk_size_t) start_offset > DUK_HBUFFER_FIXED_GET_SIZE(buf)) {
  46742. DUK_DD(DUK_DDPRINT("pc2line lookup failed: start_offset out of bounds (start_offset=%ld, buffer_size=%ld)",
  46743. (long) start_offset, (long) DUK_HBUFFER_GET_SIZE((duk_hbuffer *) buf)));
  46744. goto pc2line_error;
  46745. }
  46746. /*
  46747. * Iterate the bitstream (line diffs) until PC is reached
  46748. */
  46749. duk_memzero(bd_ctx, sizeof(*bd_ctx));
  46750. bd_ctx->data = ((duk_uint8_t *) hdr) + start_offset;
  46751. bd_ctx->length = (duk_size_t) (DUK_HBUFFER_FIXED_GET_SIZE(buf) - start_offset);
  46752. #if 0
  46753. DUK_DDD(DUK_DDDPRINT("pc2line lookup: pc=%ld -> hdr_index=%ld, pc_base=%ld, n=%ld, start_offset=%ld",
  46754. (long) pc, (long) hdr_index, (long) pc_base, (long) n, (long) start_offset));
  46755. #endif
  46756. while (n > 0) {
  46757. #if 0
  46758. DUK_DDD(DUK_DDDPRINT("lookup: n=%ld, curr_line=%ld", (long) n, (long) curr_line));
  46759. #endif
  46760. if (duk_bd_decode_flag(bd_ctx)) {
  46761. if (duk_bd_decode_flag(bd_ctx)) {
  46762. if (duk_bd_decode_flag(bd_ctx)) {
  46763. /* 1 1 1 <32 bits> */
  46764. duk_uint_fast32_t t;
  46765. t = duk_bd_decode(bd_ctx, 16); /* workaround: max nbits = 24 now */
  46766. t = (t << 16) + duk_bd_decode(bd_ctx, 16);
  46767. curr_line = t;
  46768. } else {
  46769. /* 1 1 0 <8 bits> */
  46770. duk_uint_fast32_t t;
  46771. t = duk_bd_decode(bd_ctx, 8);
  46772. curr_line = curr_line + t - 0x80;
  46773. }
  46774. } else {
  46775. /* 1 0 <2 bits> */
  46776. duk_uint_fast32_t t;
  46777. t = duk_bd_decode(bd_ctx, 2);
  46778. curr_line = curr_line + t + 1;
  46779. }
  46780. } else {
  46781. /* 0: no change */
  46782. }
  46783. n--;
  46784. }
  46785. DUK_DDD(DUK_DDDPRINT("pc2line lookup result: pc %ld -> line %ld", (long) pc, (long) curr_line));
  46786. return curr_line;
  46787. pc2line_error:
  46788. DUK_D(DUK_DPRINT("pc2line conversion failed for pc=%ld", (long) pc));
  46789. return 0;
  46790. }
  46791. DUK_INTERNAL duk_uint_fast32_t duk_hobject_pc2line_query(duk_hthread *thr, duk_idx_t idx_func, duk_uint_fast32_t pc) {
  46792. duk_hbuffer_fixed *pc2line;
  46793. duk_uint_fast32_t line;
  46794. /* XXX: now that pc2line is used by the debugger quite heavily in
  46795. * checked execution, this should be optimized to avoid value stack
  46796. * and perhaps also implement some form of pc2line caching (see
  46797. * future work in debugger.rst).
  46798. */
  46799. duk_get_prop_stridx(thr, idx_func, DUK_STRIDX_INT_PC2LINE);
  46800. pc2line = (duk_hbuffer_fixed *) (void *) duk_get_hbuffer(thr, -1);
  46801. if (pc2line != NULL) {
  46802. DUK_ASSERT(!DUK_HBUFFER_HAS_DYNAMIC((duk_hbuffer *) pc2line) && !DUK_HBUFFER_HAS_EXTERNAL((duk_hbuffer *) pc2line));
  46803. line = duk__hobject_pc2line_query_raw(thr, pc2line, (duk_uint_fast32_t) pc);
  46804. } else {
  46805. line = 0;
  46806. }
  46807. duk_pop(thr);
  46808. return line;
  46809. }
  46810. #endif /* DUK_USE_PC2LINE */
  46811. #line 1 "duk_hobject_props.c"
  46812. /*
  46813. * duk_hobject property access functionality.
  46814. *
  46815. * This is very central functionality for size, performance, and compliance.
  46816. * It is also rather intricate; see hobject-algorithms.rst for discussion on
  46817. * the algorithms and memory-management.rst for discussion on refcounts and
  46818. * side effect issues.
  46819. *
  46820. * Notes:
  46821. *
  46822. * - It might be tempting to assert "refcount nonzero" for objects
  46823. * being operated on, but that's not always correct: objects with
  46824. * a zero refcount may be operated on by the refcount implementation
  46825. * (finalization) for instance. Hence, no refcount assertions are made.
  46826. *
  46827. * - Many operations (memory allocation, identifier operations, etc)
  46828. * may cause arbitrary side effects (e.g. through GC and finalization).
  46829. * These side effects may invalidate duk_tval pointers which point to
  46830. * areas subject to reallocation (like value stack). Heap objects
  46831. * themselves have stable pointers. Holding heap object pointers or
  46832. * duk_tval copies is not problematic with respect to side effects;
  46833. * care must be taken when holding and using argument duk_tval pointers.
  46834. *
  46835. * - If a finalizer is executed, it may operate on the the same object
  46836. * we're currently dealing with. For instance, the finalizer might
  46837. * delete a certain property which has already been looked up and
  46838. * confirmed to exist. Ideally finalizers would be disabled if GC
  46839. * happens during property access. At the moment property table realloc
  46840. * disables finalizers, and all DECREFs may cause arbitrary changes so
  46841. * handle DECREF carefully.
  46842. *
  46843. * - The order of operations for a DECREF matters. When DECREF is executed,
  46844. * the entire object graph must be consistent; note that a refzero may
  46845. * lead to a mark-and-sweep through a refcount finalizer. Use NORZ macros
  46846. * and an explicit DUK_REFZERO_CHECK_xxx() if achieving correct order is hard.
  46847. */
  46848. /*
  46849. * XXX: array indices are mostly typed as duk_uint32_t here; duk_uarridx_t
  46850. * might be more appropriate.
  46851. */
  46852. /* #include duk_internal.h -> already included */
  46853. /*
  46854. * Local defines
  46855. */
  46856. #define DUK__NO_ARRAY_INDEX DUK_HSTRING_NO_ARRAY_INDEX
  46857. /* Marker values for hash part. */
  46858. #define DUK__HASH_UNUSED DUK_HOBJECT_HASHIDX_UNUSED
  46859. #define DUK__HASH_DELETED DUK_HOBJECT_HASHIDX_DELETED
  46860. /* Valstack space that suffices for all local calls, excluding any recursion
  46861. * into ECMAScript or Duktape/C calls (Proxy, getters, etc).
  46862. */
  46863. #define DUK__VALSTACK_SPACE 10
  46864. /* Valstack space allocated especially for proxy lookup which does a
  46865. * recursive property lookup.
  46866. */
  46867. #define DUK__VALSTACK_PROXY_LOOKUP 20
  46868. /*
  46869. * Local prototypes
  46870. */
  46871. DUK_LOCAL_DECL duk_bool_t duk__check_arguments_map_for_get(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc);
  46872. DUK_LOCAL_DECL void duk__check_arguments_map_for_put(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc, duk_bool_t throw_flag);
  46873. DUK_LOCAL_DECL void duk__check_arguments_map_for_delete(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc);
  46874. DUK_LOCAL_DECL duk_bool_t duk__handle_put_array_length_smaller(duk_hthread *thr, duk_hobject *obj, duk_uint32_t old_len, duk_uint32_t new_len, duk_bool_t force_flag, duk_uint32_t *out_result_len);
  46875. DUK_LOCAL_DECL duk_bool_t duk__handle_put_array_length(duk_hthread *thr, duk_hobject *obj);
  46876. DUK_LOCAL_DECL duk_bool_t duk__get_propdesc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags);
  46877. DUK_LOCAL_DECL duk_bool_t duk__get_own_propdesc_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_uint32_t arr_idx, duk_propdesc *out_desc, duk_small_uint_t flags);
  46878. /*
  46879. * Misc helpers
  46880. */
  46881. /* Convert a duk_tval number (caller checks) to a 32-bit index. Returns
  46882. * DUK__NO_ARRAY_INDEX if the number is not whole or not a valid array
  46883. * index.
  46884. */
  46885. /* XXX: for fastints, could use a variant which assumes a double duk_tval
  46886. * (and doesn't need to check for fastint again).
  46887. */
  46888. DUK_LOCAL duk_uint32_t duk__tval_number_to_arr_idx(duk_tval *tv) {
  46889. duk_double_t dbl;
  46890. duk_uint32_t idx;
  46891. DUK_ASSERT(tv != NULL);
  46892. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  46893. /* -0 is accepted here as index 0 because ToString(-0) == "0" which is
  46894. * in canonical form and thus an array index.
  46895. */
  46896. dbl = DUK_TVAL_GET_NUMBER(tv);
  46897. idx = (duk_uint32_t) dbl;
  46898. if ((duk_double_t) idx == dbl) {
  46899. /* Is whole and within 32 bit range. If the value happens to be 0xFFFFFFFF,
  46900. * it's not a valid array index but will then match DUK__NO_ARRAY_INDEX.
  46901. */
  46902. return idx;
  46903. }
  46904. return DUK__NO_ARRAY_INDEX;
  46905. }
  46906. #if defined(DUK_USE_FASTINT)
  46907. /* Convert a duk_tval fastint (caller checks) to a 32-bit index. */
  46908. DUK_LOCAL duk_uint32_t duk__tval_fastint_to_arr_idx(duk_tval *tv) {
  46909. duk_int64_t t;
  46910. DUK_ASSERT(tv != NULL);
  46911. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  46912. t = DUK_TVAL_GET_FASTINT(tv);
  46913. if (((duk_uint64_t) t & ~DUK_U64_CONSTANT(0xffffffff)) != 0) {
  46914. /* Catches >0x100000000 and negative values. */
  46915. return DUK__NO_ARRAY_INDEX;
  46916. }
  46917. /* If the value happens to be 0xFFFFFFFF, it's not a valid array index
  46918. * but will then match DUK__NO_ARRAY_INDEX.
  46919. */
  46920. return (duk_uint32_t) t;
  46921. }
  46922. #endif /* DUK_USE_FASTINT */
  46923. /* Convert a duk_tval on the value stack (in a trusted index we don't validate)
  46924. * to a string or symbol using ES2015 ToPropertyKey():
  46925. * http://www.ecma-international.org/ecma-262/6.0/#sec-topropertykey.
  46926. *
  46927. * Also check if it's a valid array index and return that (or DUK__NO_ARRAY_INDEX
  46928. * if not).
  46929. */
  46930. DUK_LOCAL duk_uint32_t duk__to_property_key(duk_hthread *thr, duk_idx_t idx, duk_hstring **out_h) {
  46931. duk_uint32_t arr_idx;
  46932. duk_hstring *h;
  46933. duk_tval *tv_dst;
  46934. DUK_ASSERT(thr != NULL);
  46935. DUK_ASSERT(out_h != NULL);
  46936. DUK_ASSERT(duk_is_valid_index(thr, idx));
  46937. DUK_ASSERT(idx < 0);
  46938. /* XXX: The revised ES2015 ToPropertyKey() handling (ES5.1 was just
  46939. * ToString()) involves a ToPrimitive(), a symbol check, and finally
  46940. * a ToString(). Figure out the best way to have a good fast path
  46941. * but still be compliant and share code.
  46942. */
  46943. tv_dst = DUK_GET_TVAL_NEGIDX(thr, idx); /* intentionally unvalidated */
  46944. if (DUK_TVAL_IS_STRING(tv_dst)) {
  46945. /* Most important path: strings and plain symbols are used as
  46946. * is. For symbols the array index check below is unnecessary
  46947. * (they're never valid array indices) but checking that the
  46948. * string is a symbol would make the plain string path slower
  46949. * unnecessarily.
  46950. */
  46951. h = DUK_TVAL_GET_STRING(tv_dst);
  46952. } else {
  46953. h = duk_to_property_key_hstring(thr, idx);
  46954. }
  46955. DUK_ASSERT(h != NULL);
  46956. *out_h = h;
  46957. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(h);
  46958. return arr_idx;
  46959. }
  46960. DUK_LOCAL duk_uint32_t duk__push_tval_to_property_key(duk_hthread *thr, duk_tval *tv_key, duk_hstring **out_h) {
  46961. duk_push_tval(thr, tv_key); /* XXX: could use an unsafe push here */
  46962. return duk__to_property_key(thr, -1, out_h);
  46963. }
  46964. /* String is an own (virtual) property of a plain buffer. */
  46965. DUK_LOCAL duk_bool_t duk__key_is_plain_buf_ownprop(duk_hthread *thr, duk_hbuffer *buf, duk_hstring *key, duk_uint32_t arr_idx) {
  46966. DUK_UNREF(thr);
  46967. /* Virtual index properties. Checking explicitly for
  46968. * 'arr_idx != DUK__NO_ARRAY_INDEX' is not necessary
  46969. * because DUK__NO_ARRAY_INDEXi is always larger than
  46970. * maximum allowed buffer size.
  46971. */
  46972. DUK_ASSERT(DUK__NO_ARRAY_INDEX >= DUK_HBUFFER_GET_SIZE(buf));
  46973. if (arr_idx < DUK_HBUFFER_GET_SIZE(buf)) {
  46974. return 1;
  46975. }
  46976. /* Other virtual properties. */
  46977. return (key == DUK_HTHREAD_STRING_LENGTH(thr));
  46978. }
  46979. /*
  46980. * Helpers for managing property storage size
  46981. */
  46982. /* Get default hash part size for a certain entry part size. */
  46983. #if defined(DUK_USE_HOBJECT_HASH_PART)
  46984. DUK_LOCAL duk_uint32_t duk__get_default_h_size(duk_uint32_t e_size) {
  46985. DUK_ASSERT(e_size <= DUK_HOBJECT_MAX_PROPERTIES);
  46986. if (e_size >= DUK_USE_HOBJECT_HASH_PROP_LIMIT) {
  46987. duk_uint32_t res;
  46988. duk_uint32_t tmp;
  46989. /* Hash size should be 2^N where N is chosen so that 2^N is
  46990. * larger than e_size. Extra shifting is used to ensure hash
  46991. * is relatively sparse.
  46992. */
  46993. tmp = e_size;
  46994. res = 2; /* Result will be 2 ** (N + 1). */
  46995. while (tmp >= 0x40) {
  46996. tmp >>= 6;
  46997. res <<= 6;
  46998. }
  46999. while (tmp != 0) {
  47000. tmp >>= 1;
  47001. res <<= 1;
  47002. }
  47003. DUK_ASSERT((DUK_HOBJECT_MAX_PROPERTIES << 2U) > DUK_HOBJECT_MAX_PROPERTIES); /* Won't wrap, even shifted by 2. */
  47004. DUK_ASSERT(res > e_size);
  47005. return res;
  47006. } else {
  47007. return 0;
  47008. }
  47009. }
  47010. #endif /* USE_PROP_HASH_PART */
  47011. /* Get minimum entry part growth for a certain size. */
  47012. DUK_LOCAL duk_uint32_t duk__get_min_grow_e(duk_uint32_t e_size) {
  47013. duk_uint32_t res;
  47014. DUK_ASSERT(e_size <= DUK_HOBJECT_MAX_PROPERTIES);
  47015. res = (e_size + DUK_USE_HOBJECT_ENTRY_MINGROW_ADD) / DUK_USE_HOBJECT_ENTRY_MINGROW_DIVISOR;
  47016. DUK_ASSERT(res >= 1); /* important for callers */
  47017. return res;
  47018. }
  47019. /* Get minimum array part growth for a certain size. */
  47020. DUK_LOCAL duk_uint32_t duk__get_min_grow_a(duk_uint32_t a_size) {
  47021. duk_uint32_t res;
  47022. DUK_ASSERT((duk_size_t) a_size <= DUK_HOBJECT_MAX_PROPERTIES);
  47023. res = (a_size + DUK_USE_HOBJECT_ARRAY_MINGROW_ADD) / DUK_USE_HOBJECT_ARRAY_MINGROW_DIVISOR;
  47024. DUK_ASSERT(res >= 1); /* important for callers */
  47025. return res;
  47026. }
  47027. /* Count actually used entry part entries (non-NULL keys). */
  47028. DUK_LOCAL duk_uint32_t duk__count_used_e_keys(duk_hthread *thr, duk_hobject *obj) {
  47029. duk_uint_fast32_t i;
  47030. duk_uint_fast32_t n = 0;
  47031. duk_hstring **e;
  47032. DUK_ASSERT(obj != NULL);
  47033. DUK_UNREF(thr);
  47034. e = DUK_HOBJECT_E_GET_KEY_BASE(thr->heap, obj);
  47035. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  47036. if (*e++) {
  47037. n++;
  47038. }
  47039. }
  47040. return (duk_uint32_t) n;
  47041. }
  47042. /* Count actually used array part entries and array minimum size.
  47043. * NOTE: 'out_min_size' can be computed much faster by starting from the
  47044. * end and breaking out early when finding first used entry, but this is
  47045. * not needed now.
  47046. */
  47047. DUK_LOCAL void duk__compute_a_stats(duk_hthread *thr, duk_hobject *obj, duk_uint32_t *out_used, duk_uint32_t *out_min_size) {
  47048. duk_uint_fast32_t i;
  47049. duk_uint_fast32_t used = 0;
  47050. duk_uint_fast32_t highest_idx = (duk_uint_fast32_t) -1; /* see below */
  47051. duk_tval *a;
  47052. DUK_ASSERT(obj != NULL);
  47053. DUK_ASSERT(out_used != NULL);
  47054. DUK_ASSERT(out_min_size != NULL);
  47055. DUK_UNREF(thr);
  47056. a = DUK_HOBJECT_A_GET_BASE(thr->heap, obj);
  47057. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  47058. duk_tval *tv = a++;
  47059. if (!DUK_TVAL_IS_UNUSED(tv)) {
  47060. used++;
  47061. highest_idx = i;
  47062. }
  47063. }
  47064. /* Initial value for highest_idx is -1 coerced to unsigned. This
  47065. * is a bit odd, but (highest_idx + 1) will then wrap to 0 below
  47066. * for out_min_size as intended.
  47067. */
  47068. *out_used = (duk_uint32_t) used;
  47069. *out_min_size = (duk_uint32_t) (highest_idx + 1); /* 0 if no used entries */
  47070. }
  47071. /* Check array density and indicate whether or not the array part should be abandoned. */
  47072. DUK_LOCAL duk_bool_t duk__abandon_array_density_check(duk_uint32_t a_used, duk_uint32_t a_size) {
  47073. /*
  47074. * Array abandon check; abandon if:
  47075. *
  47076. * new_used / new_size < limit
  47077. * new_used < limit * new_size || limit is 3 bits fixed point
  47078. * new_used < limit' / 8 * new_size || *8
  47079. * 8*new_used < limit' * new_size || :8
  47080. * new_used < limit' * (new_size / 8)
  47081. *
  47082. * Here, new_used = a_used, new_size = a_size.
  47083. *
  47084. * Note: some callers use approximate values for a_used and/or a_size
  47085. * (e.g. dropping a '+1' term). This doesn't affect the usefulness
  47086. * of the check, but may confuse debugging.
  47087. */
  47088. return (a_used < DUK_USE_HOBJECT_ARRAY_ABANDON_LIMIT * (a_size >> 3));
  47089. }
  47090. /* Fast check for extending array: check whether or not a slow density check is required. */
  47091. DUK_LOCAL duk_bool_t duk__abandon_array_slow_check_required(duk_uint32_t arr_idx, duk_uint32_t old_size) {
  47092. /*
  47093. * In a fast check we assume old_size equals old_used (i.e., existing
  47094. * array is fully dense).
  47095. *
  47096. * Slow check if:
  47097. *
  47098. * (new_size - old_size) / old_size > limit
  47099. * new_size - old_size > limit * old_size
  47100. * new_size > (1 + limit) * old_size || limit' is 3 bits fixed point
  47101. * new_size > (1 + (limit' / 8)) * old_size || * 8
  47102. * 8 * new_size > (8 + limit') * old_size || : 8
  47103. * new_size > (8 + limit') * (old_size / 8)
  47104. * new_size > limit'' * (old_size / 8) || limit'' = 9 -> max 25% increase
  47105. * arr_idx + 1 > limit'' * (old_size / 8)
  47106. *
  47107. * This check doesn't work well for small values, so old_size is rounded
  47108. * up for the check (and the '+ 1' of arr_idx can be ignored in practice):
  47109. *
  47110. * arr_idx > limit'' * ((old_size + 7) / 8)
  47111. */
  47112. return (arr_idx > DUK_USE_HOBJECT_ARRAY_FAST_RESIZE_LIMIT * ((old_size + 7) >> 3));
  47113. }
  47114. /*
  47115. * Proxy helpers
  47116. */
  47117. #if defined(DUK_USE_ES6_PROXY)
  47118. DUK_INTERNAL duk_bool_t duk_hobject_proxy_check(duk_hobject *obj, duk_hobject **out_target, duk_hobject **out_handler) {
  47119. duk_hproxy *h_proxy;
  47120. DUK_ASSERT(obj != NULL);
  47121. DUK_ASSERT(out_target != NULL);
  47122. DUK_ASSERT(out_handler != NULL);
  47123. /* Caller doesn't need to check exotic proxy behavior (but does so for
  47124. * some fast paths).
  47125. */
  47126. if (DUK_LIKELY(!DUK_HOBJECT_IS_PROXY(obj))) {
  47127. return 0;
  47128. }
  47129. h_proxy = (duk_hproxy *) obj;
  47130. DUK_ASSERT_HPROXY_VALID(h_proxy);
  47131. DUK_ASSERT(h_proxy->handler != NULL);
  47132. DUK_ASSERT(h_proxy->target != NULL);
  47133. *out_handler = h_proxy->handler;
  47134. *out_target = h_proxy->target;
  47135. return 1;
  47136. }
  47137. #endif /* DUK_USE_ES6_PROXY */
  47138. /* Get Proxy target object. If the argument is not a Proxy, return it as is.
  47139. * If a Proxy is revoked, an error is thrown.
  47140. */
  47141. #if defined(DUK_USE_ES6_PROXY)
  47142. DUK_INTERNAL duk_hobject *duk_hobject_resolve_proxy_target(duk_hobject *obj) {
  47143. DUK_ASSERT(obj != NULL);
  47144. /* Resolve Proxy targets until Proxy chain ends. No explicit check for
  47145. * a Proxy loop: user code cannot create such a loop (it would only be
  47146. * possible by editing duk_hproxy references directly).
  47147. */
  47148. while (DUK_HOBJECT_IS_PROXY(obj)) {
  47149. duk_hproxy *h_proxy;
  47150. h_proxy = (duk_hproxy *) obj;
  47151. DUK_ASSERT_HPROXY_VALID(h_proxy);
  47152. obj = h_proxy->target;
  47153. DUK_ASSERT(obj != NULL);
  47154. }
  47155. DUK_ASSERT(obj != NULL);
  47156. return obj;
  47157. }
  47158. #endif /* DUK_USE_ES6_PROXY */
  47159. #if defined(DUK_USE_ES6_PROXY)
  47160. DUK_LOCAL duk_bool_t duk__proxy_check_prop(duk_hthread *thr, duk_hobject *obj, duk_small_uint_t stridx_trap, duk_tval *tv_key, duk_hobject **out_target) {
  47161. duk_hobject *h_handler;
  47162. DUK_ASSERT(thr != NULL);
  47163. DUK_ASSERT(obj != NULL);
  47164. DUK_ASSERT(tv_key != NULL);
  47165. DUK_ASSERT(out_target != NULL);
  47166. if (!duk_hobject_proxy_check(obj, out_target, &h_handler)) {
  47167. return 0;
  47168. }
  47169. DUK_ASSERT(*out_target != NULL);
  47170. DUK_ASSERT(h_handler != NULL);
  47171. /* XXX: At the moment Duktape accesses internal keys like _Finalizer using a
  47172. * normal property set/get which would allow a proxy handler to interfere with
  47173. * such behavior and to get access to internal key strings. This is not a problem
  47174. * as such because internal key strings can be created in other ways too (e.g.
  47175. * through buffers). The best fix is to change Duktape internal lookups to
  47176. * skip proxy behavior. Until that, internal property accesses bypass the
  47177. * proxy and are applied to the target (as if the handler did not exist).
  47178. * This has some side effects, see test-bi-proxy-internal-keys.js.
  47179. */
  47180. if (DUK_TVAL_IS_STRING(tv_key)) {
  47181. duk_hstring *h_key = (duk_hstring *) DUK_TVAL_GET_STRING(tv_key);
  47182. DUK_ASSERT(h_key != NULL);
  47183. if (DUK_HSTRING_HAS_HIDDEN(h_key)) {
  47184. /* Symbol accesses must go through proxy lookup in ES2015.
  47185. * Hidden symbols behave like Duktape 1.x internal keys
  47186. * and currently won't.
  47187. */
  47188. DUK_DDD(DUK_DDDPRINT("hidden key, skip proxy handler and apply to target"));
  47189. return 0;
  47190. }
  47191. }
  47192. /* The handler is looked up with a normal property lookup; it may be an
  47193. * accessor or the handler object itself may be a proxy object. If the
  47194. * handler is a proxy, we need to extend the valstack as we make a
  47195. * recursive proxy check without a function call in between (in fact
  47196. * there is no limit to the potential recursion here).
  47197. *
  47198. * (For sanity, proxy creation rejects another proxy object as either
  47199. * the handler or the target at the moment so recursive proxy cases
  47200. * are not realized now.)
  47201. */
  47202. /* XXX: C recursion limit if proxies are allowed as handler/target values */
  47203. duk_require_stack(thr, DUK__VALSTACK_PROXY_LOOKUP);
  47204. duk_push_hobject(thr, h_handler);
  47205. if (duk_get_prop_stridx_short(thr, -1, stridx_trap)) {
  47206. /* -> [ ... handler trap ] */
  47207. duk_insert(thr, -2); /* -> [ ... trap handler ] */
  47208. /* stack prepped for func call: [ ... trap handler ] */
  47209. return 1;
  47210. } else {
  47211. duk_pop_2_unsafe(thr);
  47212. return 0;
  47213. }
  47214. }
  47215. #endif /* DUK_USE_ES6_PROXY */
  47216. /*
  47217. * Reallocate property allocation, moving properties to the new allocation.
  47218. *
  47219. * Includes key compaction, rehashing, and can also optionally abandon
  47220. * the array part, 'migrating' array entries into the beginning of the
  47221. * new entry part.
  47222. *
  47223. * There is no support for in-place reallocation or just compacting keys
  47224. * without resizing the property allocation. This is intentional to keep
  47225. * code size minimal, but would be useful future work.
  47226. *
  47227. * The implementation is relatively straightforward, except for the array
  47228. * abandonment process. Array abandonment requires that new string keys
  47229. * are interned, which may trigger GC. All keys interned so far must be
  47230. * reachable for GC at all times and correctly refcounted for; valstack is
  47231. * used for that now.
  47232. *
  47233. * Also, a GC triggered during this reallocation process must not interfere
  47234. * with the object being resized. This is currently controlled by preventing
  47235. * finalizers (as they may affect ANY object) and object compaction in
  47236. * mark-and-sweep. It would suffice to protect only this particular object
  47237. * from compaction, however. DECREF refzero cascades are side effect free
  47238. * and OK.
  47239. *
  47240. * Note: because we need to potentially resize the valstack (as part
  47241. * of abandoning the array part), any tval pointers to the valstack
  47242. * will become invalid after this call.
  47243. */
  47244. DUK_INTERNAL void duk_hobject_realloc_props(duk_hthread *thr,
  47245. duk_hobject *obj,
  47246. duk_uint32_t new_e_size,
  47247. duk_uint32_t new_a_size,
  47248. duk_uint32_t new_h_size,
  47249. duk_bool_t abandon_array) {
  47250. duk_small_uint_t prev_ms_base_flags;
  47251. duk_uint32_t new_alloc_size;
  47252. duk_uint32_t new_e_size_adjusted;
  47253. duk_uint8_t *new_p;
  47254. duk_hstring **new_e_k;
  47255. duk_propvalue *new_e_pv;
  47256. duk_uint8_t *new_e_f;
  47257. duk_tval *new_a;
  47258. duk_uint32_t *new_h;
  47259. duk_uint32_t new_e_next;
  47260. duk_uint_fast32_t i;
  47261. duk_size_t array_copy_size;
  47262. #if defined(DUK_USE_ASSERTIONS)
  47263. duk_bool_t prev_error_not_allowed;
  47264. #endif
  47265. DUK_ASSERT(thr != NULL);
  47266. DUK_ASSERT(obj != NULL);
  47267. DUK_ASSERT(!abandon_array || new_a_size == 0); /* if abandon_array, new_a_size must be 0 */
  47268. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL || (DUK_HOBJECT_GET_ESIZE(obj) == 0 && DUK_HOBJECT_GET_ASIZE(obj) == 0));
  47269. DUK_ASSERT(new_h_size == 0 || new_h_size >= new_e_size); /* required to guarantee success of rehashing,
  47270. * intentionally use unadjusted new_e_size
  47271. */
  47272. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj));
  47273. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  47274. DUK_STATS_INC(thr->heap, stats_object_realloc_props);
  47275. /*
  47276. * Pre resize assertions.
  47277. */
  47278. #if defined(DUK_USE_ASSERTIONS)
  47279. /* XXX: pre-checks (such as no duplicate keys) */
  47280. #endif
  47281. /*
  47282. * For property layout 1, tweak e_size to ensure that the whole entry
  47283. * part (key + val + flags) is a suitable multiple for alignment
  47284. * (platform specific).
  47285. *
  47286. * Property layout 2 does not require this tweaking and is preferred
  47287. * on low RAM platforms requiring alignment.
  47288. */
  47289. #if defined(DUK_USE_HOBJECT_LAYOUT_2) || defined(DUK_USE_HOBJECT_LAYOUT_3)
  47290. DUK_DDD(DUK_DDDPRINT("using layout 2 or 3, no need to pad e_size: %ld", (long) new_e_size));
  47291. new_e_size_adjusted = new_e_size;
  47292. #elif defined(DUK_USE_HOBJECT_LAYOUT_1) && (DUK_HOBJECT_ALIGN_TARGET == 1)
  47293. DUK_DDD(DUK_DDDPRINT("using layout 1, but no need to pad e_size: %ld", (long) new_e_size));
  47294. new_e_size_adjusted = new_e_size;
  47295. #elif defined(DUK_USE_HOBJECT_LAYOUT_1) && ((DUK_HOBJECT_ALIGN_TARGET == 4) || (DUK_HOBJECT_ALIGN_TARGET == 8))
  47296. new_e_size_adjusted = (new_e_size + (duk_uint32_t) DUK_HOBJECT_ALIGN_TARGET - 1U) &
  47297. (~((duk_uint32_t) DUK_HOBJECT_ALIGN_TARGET - 1U));
  47298. DUK_DDD(DUK_DDDPRINT("using layout 1, and alignment target is %ld, adjusted e_size: %ld -> %ld",
  47299. (long) DUK_HOBJECT_ALIGN_TARGET, (long) new_e_size, (long) new_e_size_adjusted));
  47300. DUK_ASSERT(new_e_size_adjusted >= new_e_size);
  47301. #else
  47302. #error invalid hobject layout defines
  47303. #endif
  47304. /*
  47305. * Debug logging after adjustment.
  47306. */
  47307. DUK_DDD(DUK_DDDPRINT("attempt to resize hobject %p props (%ld -> %ld bytes), from {p=%p,e_size=%ld,e_next=%ld,a_size=%ld,h_size=%ld} to "
  47308. "{e_size=%ld,a_size=%ld,h_size=%ld}, abandon_array=%ld, unadjusted new_e_size=%ld",
  47309. (void *) obj,
  47310. (long) DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  47311. DUK_HOBJECT_GET_ASIZE(obj),
  47312. DUK_HOBJECT_GET_HSIZE(obj)),
  47313. (long) DUK_HOBJECT_P_COMPUTE_SIZE(new_e_size_adjusted, new_a_size, new_h_size),
  47314. (void *) DUK_HOBJECT_GET_PROPS(thr->heap, obj),
  47315. (long) DUK_HOBJECT_GET_ESIZE(obj),
  47316. (long) DUK_HOBJECT_GET_ENEXT(obj),
  47317. (long) DUK_HOBJECT_GET_ASIZE(obj),
  47318. (long) DUK_HOBJECT_GET_HSIZE(obj),
  47319. (long) new_e_size_adjusted,
  47320. (long) new_a_size,
  47321. (long) new_h_size,
  47322. (long) abandon_array,
  47323. (long) new_e_size));
  47324. /*
  47325. * Property count check. This is the only point where we ensure that
  47326. * we don't get more (allocated) property space that we can handle.
  47327. * There aren't hard limits as such, but some algorithms may fail
  47328. * if we get too close to the 4G property limit.
  47329. *
  47330. * Since this works based on allocation size (not actually used size),
  47331. * the limit is a bit approximate but good enough in practice.
  47332. */
  47333. if (new_e_size_adjusted + new_a_size > DUK_HOBJECT_MAX_PROPERTIES) {
  47334. DUK_ERROR_ALLOC_FAILED(thr);
  47335. DUK_WO_NORETURN(return;);
  47336. }
  47337. /*
  47338. * Compute new alloc size and alloc new area.
  47339. *
  47340. * The new area is not tracked in the heap at all, so it's critical
  47341. * we get to free/keep it in a controlled manner.
  47342. */
  47343. #if defined(DUK_USE_ASSERTIONS)
  47344. /* Whole path must be error throw free, but we may be called from
  47345. * within error handling so can't assert for error_not_allowed == 0.
  47346. */
  47347. prev_error_not_allowed = thr->heap->error_not_allowed;
  47348. thr->heap->error_not_allowed = 1;
  47349. #endif
  47350. prev_ms_base_flags = thr->heap->ms_base_flags;
  47351. thr->heap->ms_base_flags |=
  47352. DUK_MS_FLAG_NO_OBJECT_COMPACTION; /* Avoid attempt to compact the current object (all objects really). */
  47353. thr->heap->pf_prevent_count++; /* Avoid finalizers. */
  47354. DUK_ASSERT(thr->heap->pf_prevent_count != 0); /* Wrap. */
  47355. new_alloc_size = DUK_HOBJECT_P_COMPUTE_SIZE(new_e_size_adjusted, new_a_size, new_h_size);
  47356. DUK_DDD(DUK_DDDPRINT("new hobject allocation size is %ld", (long) new_alloc_size));
  47357. if (new_alloc_size == 0) {
  47358. DUK_ASSERT(new_e_size_adjusted == 0);
  47359. DUK_ASSERT(new_a_size == 0);
  47360. DUK_ASSERT(new_h_size == 0);
  47361. new_p = NULL;
  47362. } else {
  47363. /* Alloc may trigger mark-and-sweep but no compaction, and
  47364. * cannot throw.
  47365. */
  47366. #if 0 /* XXX: inject test */
  47367. if (1) {
  47368. new_p = NULL;
  47369. goto alloc_failed;
  47370. }
  47371. #endif
  47372. new_p = (duk_uint8_t *) DUK_ALLOC(thr->heap, new_alloc_size);
  47373. if (new_p == NULL) {
  47374. /* NULL always indicates alloc failure because
  47375. * new_alloc_size > 0.
  47376. */
  47377. goto alloc_failed;
  47378. }
  47379. }
  47380. /* Set up pointers to the new property area: this is hidden behind a macro
  47381. * because it is memory layout specific.
  47382. */
  47383. DUK_HOBJECT_P_SET_REALLOC_PTRS(new_p, new_e_k, new_e_pv, new_e_f, new_a, new_h,
  47384. new_e_size_adjusted, new_a_size, new_h_size);
  47385. DUK_UNREF(new_h); /* happens when hash part dropped */
  47386. new_e_next = 0;
  47387. /* if new_p == NULL, all of these pointers are NULL */
  47388. DUK_ASSERT((new_p != NULL) ||
  47389. (new_e_k == NULL && new_e_pv == NULL && new_e_f == NULL &&
  47390. new_a == NULL && new_h == NULL));
  47391. DUK_DDD(DUK_DDDPRINT("new alloc size %ld, new_e_k=%p, new_e_pv=%p, new_e_f=%p, new_a=%p, new_h=%p",
  47392. (long) new_alloc_size, (void *) new_e_k, (void *) new_e_pv, (void *) new_e_f,
  47393. (void *) new_a, (void *) new_h));
  47394. /*
  47395. * Migrate array part to start of entries if requested.
  47396. *
  47397. * Note: from an enumeration perspective the order of entry keys matters.
  47398. * Array keys should appear wherever they appeared before the array abandon
  47399. * operation. (This no longer matters much because keys are ES2015 sorted.)
  47400. */
  47401. if (abandon_array) {
  47402. /* Assuming new_a_size == 0, and that entry part contains
  47403. * no conflicting keys, refcounts do not need to be adjusted for
  47404. * the values, as they remain exactly the same.
  47405. *
  47406. * The keys, however, need to be interned, incref'd, and be
  47407. * reachable for GC. Any intern attempt may trigger a GC and
  47408. * claim any non-reachable strings, so every key must be reachable
  47409. * at all times. Refcounts must be correct to satisfy refcount
  47410. * assertions.
  47411. *
  47412. * A longjmp must not occur here, as the new_p allocation would
  47413. * leak. Refcounts would come out correctly as the interned
  47414. * strings are valstack tracked.
  47415. */
  47416. DUK_ASSERT(new_a_size == 0);
  47417. DUK_STATS_INC(thr->heap, stats_object_abandon_array);
  47418. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  47419. duk_tval *tv1;
  47420. duk_tval *tv2;
  47421. duk_hstring *key;
  47422. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  47423. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  47424. if (DUK_TVAL_IS_UNUSED(tv1)) {
  47425. continue;
  47426. }
  47427. DUK_ASSERT(new_p != NULL && new_e_k != NULL &&
  47428. new_e_pv != NULL && new_e_f != NULL);
  47429. /*
  47430. * Intern key via the valstack to ensure reachability behaves
  47431. * properly. We must avoid longjmp's here so use non-checked
  47432. * primitives.
  47433. *
  47434. * Note: duk_check_stack() potentially reallocs the valstack,
  47435. * invalidating any duk_tval pointers to valstack. Callers
  47436. * must be careful.
  47437. */
  47438. #if 0 /* XXX: inject test */
  47439. if (1) {
  47440. goto abandon_error;
  47441. }
  47442. #endif
  47443. /* Never shrinks; auto-adds DUK_VALSTACK_INTERNAL_EXTRA, which
  47444. * is generous.
  47445. */
  47446. if (!duk_check_stack(thr, 1)) {
  47447. goto abandon_error;
  47448. }
  47449. DUK_ASSERT_VALSTACK_SPACE(thr, 1);
  47450. key = duk_heap_strtable_intern_u32(thr->heap, (duk_uint32_t) i);
  47451. if (key == NULL) {
  47452. goto abandon_error;
  47453. }
  47454. duk_push_hstring(thr, key); /* keep key reachable for GC etc; guaranteed not to fail */
  47455. /* Key is now reachable in the valstack, don't INCREF
  47456. * the new allocation yet (we'll steal the refcounts
  47457. * from the value stack once all keys are done).
  47458. */
  47459. new_e_k[new_e_next] = key;
  47460. tv2 = &new_e_pv[new_e_next].v; /* array entries are all plain values */
  47461. DUK_TVAL_SET_TVAL(tv2, tv1);
  47462. new_e_f[new_e_next] = DUK_PROPDESC_FLAG_WRITABLE |
  47463. DUK_PROPDESC_FLAG_ENUMERABLE |
  47464. DUK_PROPDESC_FLAG_CONFIGURABLE;
  47465. new_e_next++;
  47466. /* Note: new_e_next matches pushed temp key count, and nothing can
  47467. * fail above between the push and this point.
  47468. */
  47469. }
  47470. /* Steal refcounts from value stack. */
  47471. DUK_DDD(DUK_DDDPRINT("abandon array: pop %ld key temps from valstack", (long) new_e_next));
  47472. duk_pop_n_nodecref_unsafe(thr, (duk_idx_t) new_e_next);
  47473. }
  47474. /*
  47475. * Copy keys and values in the entry part (compacting them at the same time).
  47476. */
  47477. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  47478. duk_hstring *key;
  47479. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL);
  47480. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  47481. if (key == NULL) {
  47482. continue;
  47483. }
  47484. DUK_ASSERT(new_p != NULL && new_e_k != NULL &&
  47485. new_e_pv != NULL && new_e_f != NULL);
  47486. new_e_k[new_e_next] = key;
  47487. new_e_pv[new_e_next] = DUK_HOBJECT_E_GET_VALUE(thr->heap, obj, i);
  47488. new_e_f[new_e_next] = DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, i);
  47489. new_e_next++;
  47490. }
  47491. /* the entries [new_e_next, new_e_size_adjusted[ are left uninitialized on purpose (ok, not gc reachable) */
  47492. /*
  47493. * Copy array elements to new array part. If the new array part is
  47494. * larger, initialize the unused entries as UNUSED because they are
  47495. * GC reachable.
  47496. */
  47497. #if defined(DUK_USE_ASSERTIONS)
  47498. /* Caller must have decref'd values above new_a_size (if that is necessary). */
  47499. if (!abandon_array) {
  47500. for (i = new_a_size; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  47501. duk_tval *tv;
  47502. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  47503. DUK_ASSERT(DUK_TVAL_IS_UNUSED(tv));
  47504. }
  47505. }
  47506. #endif
  47507. if (new_a_size > DUK_HOBJECT_GET_ASIZE(obj)) {
  47508. array_copy_size = sizeof(duk_tval) * DUK_HOBJECT_GET_ASIZE(obj);
  47509. } else {
  47510. array_copy_size = sizeof(duk_tval) * new_a_size;
  47511. }
  47512. DUK_ASSERT(new_a != NULL || array_copy_size == 0U);
  47513. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, obj) != NULL || array_copy_size == 0U);
  47514. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(obj) > 0 || array_copy_size == 0U);
  47515. duk_memcpy_unsafe((void *) new_a,
  47516. (const void *) DUK_HOBJECT_A_GET_BASE(thr->heap, obj),
  47517. array_copy_size);
  47518. for (i = DUK_HOBJECT_GET_ASIZE(obj); i < new_a_size; i++) {
  47519. duk_tval *tv = &new_a[i];
  47520. DUK_TVAL_SET_UNUSED(tv);
  47521. }
  47522. /*
  47523. * Rebuild the hash part always from scratch (guaranteed to finish
  47524. * as long as caller gave consistent parameters).
  47525. *
  47526. * Any resize of hash part requires rehashing. In addition, by rehashing
  47527. * get rid of any elements marked deleted (DUK__HASH_DELETED) which is critical
  47528. * to ensuring the hash part never fills up.
  47529. */
  47530. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47531. if (new_h_size == 0) {
  47532. DUK_DDD(DUK_DDDPRINT("no hash part, no rehash"));
  47533. } else {
  47534. duk_uint32_t mask;
  47535. DUK_ASSERT(new_h != NULL);
  47536. /* fill new_h with u32 0xff = UNUSED */
  47537. DUK_ASSERT(new_h_size > 0);
  47538. duk_memset(new_h, 0xff, sizeof(duk_uint32_t) * new_h_size);
  47539. DUK_ASSERT(new_e_next <= new_h_size); /* equality not actually possible */
  47540. mask = new_h_size - 1;
  47541. for (i = 0; i < new_e_next; i++) {
  47542. duk_hstring *key = new_e_k[i];
  47543. duk_uint32_t j, step;
  47544. DUK_ASSERT(key != NULL);
  47545. j = DUK_HSTRING_GET_HASH(key) & mask;
  47546. step = 1; /* Cache friendly but clustering prone. */
  47547. for (;;) {
  47548. DUK_ASSERT(new_h[j] != DUK__HASH_DELETED); /* should never happen */
  47549. if (new_h[j] == DUK__HASH_UNUSED) {
  47550. DUK_DDD(DUK_DDDPRINT("rebuild hit %ld -> %ld", (long) j, (long) i));
  47551. new_h[j] = (duk_uint32_t) i;
  47552. break;
  47553. }
  47554. DUK_DDD(DUK_DDDPRINT("rebuild miss %ld, step %ld", (long) j, (long) step));
  47555. j = (j + step) & mask;
  47556. /* Guaranteed to finish (hash is larger than #props). */
  47557. }
  47558. }
  47559. }
  47560. #endif /* DUK_USE_HOBJECT_HASH_PART */
  47561. /*
  47562. * Nice debug log.
  47563. */
  47564. DUK_DD(DUK_DDPRINT("resized hobject %p props (%ld -> %ld bytes), from {p=%p,e_size=%ld,e_next=%ld,a_size=%ld,h_size=%ld} to "
  47565. "{p=%p,e_size=%ld,e_next=%ld,a_size=%ld,h_size=%ld}, abandon_array=%ld, unadjusted new_e_size=%ld",
  47566. (void *) obj,
  47567. (long) DUK_HOBJECT_P_COMPUTE_SIZE(DUK_HOBJECT_GET_ESIZE(obj),
  47568. DUK_HOBJECT_GET_ASIZE(obj),
  47569. DUK_HOBJECT_GET_HSIZE(obj)),
  47570. (long) new_alloc_size,
  47571. (void *) DUK_HOBJECT_GET_PROPS(thr->heap, obj),
  47572. (long) DUK_HOBJECT_GET_ESIZE(obj),
  47573. (long) DUK_HOBJECT_GET_ENEXT(obj),
  47574. (long) DUK_HOBJECT_GET_ASIZE(obj),
  47575. (long) DUK_HOBJECT_GET_HSIZE(obj),
  47576. (void *) new_p,
  47577. (long) new_e_size_adjusted,
  47578. (long) new_e_next,
  47579. (long) new_a_size,
  47580. (long) new_h_size,
  47581. (long) abandon_array,
  47582. (long) new_e_size));
  47583. /*
  47584. * All done, switch properties ('p') allocation to new one.
  47585. */
  47586. DUK_FREE_CHECKED(thr, DUK_HOBJECT_GET_PROPS(thr->heap, obj)); /* NULL obj->p is OK */
  47587. DUK_HOBJECT_SET_PROPS(thr->heap, obj, new_p);
  47588. DUK_HOBJECT_SET_ESIZE(obj, new_e_size_adjusted);
  47589. DUK_HOBJECT_SET_ENEXT(obj, new_e_next);
  47590. DUK_HOBJECT_SET_ASIZE(obj, new_a_size);
  47591. DUK_HOBJECT_SET_HSIZE(obj, new_h_size);
  47592. /* Clear array part flag only after switching. */
  47593. if (abandon_array) {
  47594. DUK_HOBJECT_CLEAR_ARRAY_PART(obj);
  47595. }
  47596. DUK_DDD(DUK_DDDPRINT("resize result: %!O", (duk_heaphdr *) obj));
  47597. DUK_ASSERT(thr->heap->pf_prevent_count > 0);
  47598. thr->heap->pf_prevent_count--;
  47599. thr->heap->ms_base_flags = prev_ms_base_flags;
  47600. #if defined(DUK_USE_ASSERTIONS)
  47601. DUK_ASSERT(thr->heap->error_not_allowed == 1);
  47602. thr->heap->error_not_allowed = prev_error_not_allowed;
  47603. #endif
  47604. /*
  47605. * Post resize assertions.
  47606. */
  47607. #if defined(DUK_USE_ASSERTIONS)
  47608. /* XXX: post-checks (such as no duplicate keys) */
  47609. #endif
  47610. return;
  47611. /*
  47612. * Abandon array failed. We don't need to DECREF anything
  47613. * because the references in the new allocation are not
  47614. * INCREF'd until abandon is complete. The string interned
  47615. * keys are on the value stack and are handled normally by
  47616. * unwind.
  47617. */
  47618. abandon_error:
  47619. alloc_failed:
  47620. DUK_D(DUK_DPRINT("object property table resize failed"));
  47621. DUK_FREE_CHECKED(thr, new_p); /* OK for NULL. */
  47622. thr->heap->pf_prevent_count--;
  47623. thr->heap->ms_base_flags = prev_ms_base_flags;
  47624. #if defined(DUK_USE_ASSERTIONS)
  47625. DUK_ASSERT(thr->heap->error_not_allowed == 1);
  47626. thr->heap->error_not_allowed = prev_error_not_allowed;
  47627. #endif
  47628. DUK_ERROR_ALLOC_FAILED(thr);
  47629. DUK_WO_NORETURN(return;);
  47630. }
  47631. /*
  47632. * Helpers to resize properties allocation on specific needs.
  47633. */
  47634. DUK_INTERNAL void duk_hobject_resize_entrypart(duk_hthread *thr,
  47635. duk_hobject *obj,
  47636. duk_uint32_t new_e_size) {
  47637. duk_uint32_t old_e_size;
  47638. duk_uint32_t new_a_size;
  47639. duk_uint32_t new_h_size;
  47640. DUK_ASSERT(thr != NULL);
  47641. DUK_ASSERT(obj != NULL);
  47642. old_e_size = DUK_HOBJECT_GET_ESIZE(obj);
  47643. if (old_e_size > new_e_size) {
  47644. new_e_size = old_e_size;
  47645. }
  47646. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47647. new_h_size = duk__get_default_h_size(new_e_size);
  47648. #else
  47649. new_h_size = 0;
  47650. #endif
  47651. new_a_size = DUK_HOBJECT_GET_ASIZE(obj);
  47652. duk_hobject_realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  47653. }
  47654. #if 0 /*unused */
  47655. DUK_INTERNAL void duk_hobject_resize_arraypart(duk_hthread *thr,
  47656. duk_hobject *obj,
  47657. duk_uint32_t new_a_size) {
  47658. duk_uint32_t old_a_size;
  47659. duk_uint32_t new_e_size;
  47660. duk_uint32_t new_h_size;
  47661. DUK_ASSERT(thr != NULL);
  47662. DUK_ASSERT(obj != NULL);
  47663. if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  47664. return;
  47665. }
  47666. old_a_size = DUK_HOBJECT_GET_ASIZE(obj);
  47667. if (old_a_size > new_a_size) {
  47668. new_a_size = old_a_size;
  47669. }
  47670. new_e_size = DUK_HOBJECT_GET_ESIZE(obj);
  47671. new_h_size = DUK_HOBJECT_GET_HSIZE(obj);
  47672. duk_hobject_realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  47673. }
  47674. #endif
  47675. /* Grow entry part allocation for one additional entry. */
  47676. DUK_LOCAL void duk__grow_props_for_new_entry_item(duk_hthread *thr, duk_hobject *obj) {
  47677. duk_uint32_t old_e_used; /* actually used, non-NULL entries */
  47678. duk_uint32_t new_e_size;
  47679. duk_uint32_t new_a_size;
  47680. duk_uint32_t new_h_size;
  47681. DUK_ASSERT(thr != NULL);
  47682. DUK_ASSERT(obj != NULL);
  47683. /* Duktape 0.11.0 and prior tried to optimize the resize by not
  47684. * counting the number of actually used keys prior to the resize.
  47685. * This worked mostly well but also caused weird leak-like behavior
  47686. * as in: test-bug-object-prop-alloc-unbounded.js. So, now we count
  47687. * the keys explicitly to compute the new entry part size.
  47688. */
  47689. old_e_used = duk__count_used_e_keys(thr, obj);
  47690. new_e_size = old_e_used + duk__get_min_grow_e(old_e_used);
  47691. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47692. new_h_size = duk__get_default_h_size(new_e_size);
  47693. #else
  47694. new_h_size = 0;
  47695. #endif
  47696. new_a_size = DUK_HOBJECT_GET_ASIZE(obj);
  47697. DUK_ASSERT(new_e_size >= old_e_used + 1); /* duk__get_min_grow_e() is always >= 1 */
  47698. duk_hobject_realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  47699. }
  47700. /* Grow array part for a new highest array index. */
  47701. DUK_LOCAL void duk__grow_props_for_array_item(duk_hthread *thr, duk_hobject *obj, duk_uint32_t highest_arr_idx) {
  47702. duk_uint32_t new_e_size;
  47703. duk_uint32_t new_a_size;
  47704. duk_uint32_t new_h_size;
  47705. DUK_ASSERT(thr != NULL);
  47706. DUK_ASSERT(obj != NULL);
  47707. DUK_ASSERT(highest_arr_idx >= DUK_HOBJECT_GET_ASIZE(obj));
  47708. /* minimum new length is highest_arr_idx + 1 */
  47709. new_e_size = DUK_HOBJECT_GET_ESIZE(obj);
  47710. new_h_size = DUK_HOBJECT_GET_HSIZE(obj);
  47711. new_a_size = highest_arr_idx + duk__get_min_grow_a(highest_arr_idx);
  47712. DUK_ASSERT(new_a_size >= highest_arr_idx + 1); /* duk__get_min_grow_a() is always >= 1 */
  47713. duk_hobject_realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 0);
  47714. }
  47715. /* Abandon array part, moving array entries into entries part.
  47716. * This requires a props resize, which is a heavy operation.
  47717. * We also compact the entries part while we're at it, although
  47718. * this is not strictly required.
  47719. */
  47720. DUK_LOCAL void duk__abandon_array_checked(duk_hthread *thr, duk_hobject *obj) {
  47721. duk_uint32_t new_e_size;
  47722. duk_uint32_t new_a_size;
  47723. duk_uint32_t new_h_size;
  47724. duk_uint32_t e_used; /* actually used, non-NULL keys */
  47725. duk_uint32_t a_used;
  47726. duk_uint32_t a_size;
  47727. DUK_ASSERT(thr != NULL);
  47728. DUK_ASSERT(obj != NULL);
  47729. e_used = duk__count_used_e_keys(thr, obj);
  47730. duk__compute_a_stats(thr, obj, &a_used, &a_size);
  47731. /*
  47732. * Must guarantee all actually used array entries will fit into
  47733. * new entry part. Add one growth step to ensure we don't run out
  47734. * of space right away.
  47735. */
  47736. new_e_size = e_used + a_used;
  47737. new_e_size = new_e_size + duk__get_min_grow_e(new_e_size);
  47738. new_a_size = 0;
  47739. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47740. new_h_size = duk__get_default_h_size(new_e_size);
  47741. #else
  47742. new_h_size = 0;
  47743. #endif
  47744. DUK_DD(DUK_DDPRINT("abandon array part for hobject %p, "
  47745. "array stats before: e_used=%ld, a_used=%ld, a_size=%ld; "
  47746. "resize to e_size=%ld, a_size=%ld, h_size=%ld",
  47747. (void *) obj, (long) e_used, (long) a_used, (long) a_size,
  47748. (long) new_e_size, (long) new_a_size, (long) new_h_size));
  47749. duk_hobject_realloc_props(thr, obj, new_e_size, new_a_size, new_h_size, 1);
  47750. }
  47751. /*
  47752. * Compact an object. Minimizes allocation size for objects which are
  47753. * not likely to be extended. This is useful for internal and non-
  47754. * extensible objects, but can also be called for non-extensible objects.
  47755. * May abandon the array part if it is computed to be too sparse.
  47756. *
  47757. * This call is relatively expensive, as it needs to scan both the
  47758. * entries and the array part.
  47759. *
  47760. * The call may fail due to allocation error.
  47761. */
  47762. DUK_INTERNAL void duk_hobject_compact_props(duk_hthread *thr, duk_hobject *obj) {
  47763. duk_uint32_t e_size; /* currently used -> new size */
  47764. duk_uint32_t a_size; /* currently required */
  47765. duk_uint32_t a_used; /* actually used */
  47766. duk_uint32_t h_size;
  47767. duk_bool_t abandon_array;
  47768. DUK_ASSERT(thr != NULL);
  47769. DUK_ASSERT(obj != NULL);
  47770. #if defined(DUK_USE_ROM_OBJECTS)
  47771. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) {
  47772. DUK_DD(DUK_DDPRINT("ignore attempt to compact a rom object"));
  47773. return;
  47774. }
  47775. #endif
  47776. e_size = duk__count_used_e_keys(thr, obj);
  47777. duk__compute_a_stats(thr, obj, &a_used, &a_size);
  47778. DUK_DD(DUK_DDPRINT("compacting hobject, used e keys %ld, used a keys %ld, min a size %ld, "
  47779. "resized array density would be: %ld/%ld = %lf",
  47780. (long) e_size, (long) a_used, (long) a_size,
  47781. (long) a_used, (long) a_size,
  47782. (double) a_used / (double) a_size));
  47783. if (duk__abandon_array_density_check(a_used, a_size)) {
  47784. DUK_DD(DUK_DDPRINT("decided to abandon array during compaction, a_used=%ld, a_size=%ld",
  47785. (long) a_used, (long) a_size));
  47786. abandon_array = 1;
  47787. e_size += a_used;
  47788. a_size = 0;
  47789. } else {
  47790. DUK_DD(DUK_DDPRINT("decided to keep array during compaction"));
  47791. abandon_array = 0;
  47792. }
  47793. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47794. if (e_size >= DUK_USE_HOBJECT_HASH_PROP_LIMIT) {
  47795. h_size = duk__get_default_h_size(e_size);
  47796. } else {
  47797. h_size = 0;
  47798. }
  47799. #else
  47800. h_size = 0;
  47801. #endif
  47802. DUK_DD(DUK_DDPRINT("compacting hobject -> new e_size %ld, new a_size=%ld, new h_size=%ld, abandon_array=%ld",
  47803. (long) e_size, (long) a_size, (long) h_size, (long) abandon_array));
  47804. duk_hobject_realloc_props(thr, obj, e_size, a_size, h_size, abandon_array);
  47805. }
  47806. /*
  47807. * Find an existing key from entry part either by linear scan or by
  47808. * using the hash index (if it exists).
  47809. *
  47810. * Sets entry index (and possibly the hash index) to output variables,
  47811. * which allows the caller to update the entry and hash entries in-place.
  47812. * If entry is not found, both values are set to -1. If entry is found
  47813. * but there is no hash part, h_idx is set to -1.
  47814. */
  47815. DUK_INTERNAL duk_bool_t duk_hobject_find_existing_entry(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_int_t *e_idx, duk_int_t *h_idx) {
  47816. DUK_ASSERT(obj != NULL);
  47817. DUK_ASSERT(key != NULL);
  47818. DUK_ASSERT(e_idx != NULL);
  47819. DUK_ASSERT(h_idx != NULL);
  47820. DUK_UNREF(heap);
  47821. if (DUK_LIKELY(DUK_HOBJECT_GET_HSIZE(obj) == 0))
  47822. {
  47823. /* Linear scan: more likely because most objects are small.
  47824. * This is an important fast path.
  47825. *
  47826. * XXX: this might be worth inlining for property lookups.
  47827. */
  47828. duk_uint_fast32_t i;
  47829. duk_uint_fast32_t n;
  47830. duk_hstring **h_keys_base;
  47831. DUK_DDD(DUK_DDDPRINT("duk_hobject_find_existing_entry() using linear scan for lookup"));
  47832. h_keys_base = DUK_HOBJECT_E_GET_KEY_BASE(heap, obj);
  47833. n = DUK_HOBJECT_GET_ENEXT(obj);
  47834. for (i = 0; i < n; i++) {
  47835. if (h_keys_base[i] == key) {
  47836. *e_idx = (duk_int_t) i;
  47837. *h_idx = -1;
  47838. return 1;
  47839. }
  47840. }
  47841. }
  47842. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47843. else
  47844. {
  47845. /* hash lookup */
  47846. duk_uint32_t n;
  47847. duk_uint32_t i, step;
  47848. duk_uint32_t *h_base;
  47849. duk_uint32_t mask;
  47850. DUK_DDD(DUK_DDDPRINT("duk_hobject_find_existing_entry() using hash part for lookup"));
  47851. h_base = DUK_HOBJECT_H_GET_BASE(heap, obj);
  47852. n = DUK_HOBJECT_GET_HSIZE(obj);
  47853. mask = n - 1;
  47854. i = DUK_HSTRING_GET_HASH(key) & mask;
  47855. step = 1; /* Cache friendly but clustering prone. */
  47856. for (;;) {
  47857. duk_uint32_t t;
  47858. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  47859. DUK_ASSERT(i < DUK_HOBJECT_GET_HSIZE(obj));
  47860. t = h_base[i];
  47861. DUK_ASSERT(t == DUK__HASH_UNUSED || t == DUK__HASH_DELETED ||
  47862. (t < DUK_HOBJECT_GET_ESIZE(obj))); /* t >= 0 always true, unsigned */
  47863. if (t == DUK__HASH_UNUSED) {
  47864. break;
  47865. } else if (t == DUK__HASH_DELETED) {
  47866. DUK_DDD(DUK_DDDPRINT("lookup miss (deleted) i=%ld, t=%ld",
  47867. (long) i, (long) t));
  47868. } else {
  47869. DUK_ASSERT(t < DUK_HOBJECT_GET_ESIZE(obj));
  47870. if (DUK_HOBJECT_E_GET_KEY(heap, obj, t) == key) {
  47871. DUK_DDD(DUK_DDDPRINT("lookup hit i=%ld, t=%ld -> key %p",
  47872. (long) i, (long) t, (void *) key));
  47873. *e_idx = (duk_int_t) t;
  47874. *h_idx = (duk_int_t) i;
  47875. return 1;
  47876. }
  47877. DUK_DDD(DUK_DDDPRINT("lookup miss i=%ld, t=%ld",
  47878. (long) i, (long) t));
  47879. }
  47880. i = (i + step) & mask;
  47881. /* Guaranteed to finish (hash is larger than #props). */
  47882. }
  47883. }
  47884. #endif /* DUK_USE_HOBJECT_HASH_PART */
  47885. /* Not found, leave e_idx and h_idx unset. */
  47886. return 0;
  47887. }
  47888. /* For internal use: get non-accessor entry value */
  47889. DUK_INTERNAL duk_tval *duk_hobject_find_existing_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_hstring *key) {
  47890. duk_int_t e_idx;
  47891. duk_int_t h_idx;
  47892. DUK_ASSERT(obj != NULL);
  47893. DUK_ASSERT(key != NULL);
  47894. DUK_UNREF(heap);
  47895. if (duk_hobject_find_existing_entry(heap, obj, key, &e_idx, &h_idx)) {
  47896. DUK_ASSERT(e_idx >= 0);
  47897. if (!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx)) {
  47898. return DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx);
  47899. }
  47900. }
  47901. return NULL;
  47902. }
  47903. /* For internal use: get non-accessor entry value and attributes */
  47904. DUK_INTERNAL duk_tval *duk_hobject_find_existing_entry_tval_ptr_and_attrs(duk_heap *heap, duk_hobject *obj, duk_hstring *key, duk_uint_t *out_attrs) {
  47905. duk_int_t e_idx;
  47906. duk_int_t h_idx;
  47907. DUK_ASSERT(obj != NULL);
  47908. DUK_ASSERT(key != NULL);
  47909. DUK_ASSERT(out_attrs != NULL);
  47910. DUK_UNREF(heap);
  47911. if (duk_hobject_find_existing_entry(heap, obj, key, &e_idx, &h_idx)) {
  47912. DUK_ASSERT(e_idx >= 0);
  47913. if (!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx)) {
  47914. *out_attrs = DUK_HOBJECT_E_GET_FLAGS(heap, obj, e_idx);
  47915. return DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx);
  47916. }
  47917. }
  47918. /* If not found, out_attrs is left unset. */
  47919. return NULL;
  47920. }
  47921. /* For internal use: get array part value */
  47922. DUK_INTERNAL duk_tval *duk_hobject_find_existing_array_entry_tval_ptr(duk_heap *heap, duk_hobject *obj, duk_uarridx_t i) {
  47923. duk_tval *tv;
  47924. DUK_ASSERT(obj != NULL);
  47925. DUK_UNREF(heap);
  47926. if (!DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  47927. return NULL;
  47928. }
  47929. if (i >= DUK_HOBJECT_GET_ASIZE(obj)) {
  47930. return NULL;
  47931. }
  47932. tv = DUK_HOBJECT_A_GET_VALUE_PTR(heap, obj, i);
  47933. return tv;
  47934. }
  47935. /*
  47936. * Allocate and initialize a new entry, resizing the properties allocation
  47937. * if necessary. Returns entry index (e_idx) or throws an error if alloc fails.
  47938. *
  47939. * Sets the key of the entry (increasing the key's refcount), and updates
  47940. * the hash part if it exists. Caller must set value and flags, and update
  47941. * the entry value refcount. A decref for the previous value is not necessary.
  47942. */
  47943. DUK_LOCAL duk_int_t duk__hobject_alloc_entry_checked(duk_hthread *thr, duk_hobject *obj, duk_hstring *key) {
  47944. duk_uint32_t idx;
  47945. DUK_ASSERT(thr != NULL);
  47946. DUK_ASSERT(obj != NULL);
  47947. DUK_ASSERT(key != NULL);
  47948. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(obj) <= DUK_HOBJECT_GET_ESIZE(obj));
  47949. #if defined(DUK_USE_ASSERTIONS)
  47950. /* key must not already exist in entry part */
  47951. {
  47952. duk_uint_fast32_t i;
  47953. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  47954. DUK_ASSERT(DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i) != key);
  47955. }
  47956. }
  47957. #endif
  47958. if (DUK_HOBJECT_GET_ENEXT(obj) >= DUK_HOBJECT_GET_ESIZE(obj)) {
  47959. /* only need to guarantee 1 more slot, but allocation growth is in chunks */
  47960. DUK_DDD(DUK_DDDPRINT("entry part full, allocate space for one more entry"));
  47961. duk__grow_props_for_new_entry_item(thr, obj);
  47962. }
  47963. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(obj) < DUK_HOBJECT_GET_ESIZE(obj));
  47964. idx = DUK_HOBJECT_POSTINC_ENEXT(obj);
  47965. /* previous value is assumed to be garbage, so don't touch it */
  47966. DUK_HOBJECT_E_SET_KEY(thr->heap, obj, idx, key);
  47967. DUK_HSTRING_INCREF(thr, key);
  47968. #if defined(DUK_USE_HOBJECT_HASH_PART)
  47969. if (DUK_UNLIKELY(DUK_HOBJECT_GET_HSIZE(obj) > 0)) {
  47970. duk_uint32_t n, mask;
  47971. duk_uint32_t i, step;
  47972. duk_uint32_t *h_base = DUK_HOBJECT_H_GET_BASE(thr->heap, obj);
  47973. n = DUK_HOBJECT_GET_HSIZE(obj);
  47974. mask = n - 1;
  47975. i = DUK_HSTRING_GET_HASH(key) & mask;
  47976. step = 1; /* Cache friendly but clustering prone. */
  47977. for (;;) {
  47978. duk_uint32_t t = h_base[i];
  47979. if (t == DUK__HASH_UNUSED || t == DUK__HASH_DELETED) {
  47980. DUK_DDD(DUK_DDDPRINT("duk__hobject_alloc_entry_checked() inserted key into hash part, %ld -> %ld",
  47981. (long) i, (long) idx));
  47982. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  47983. DUK_ASSERT(i < DUK_HOBJECT_GET_HSIZE(obj));
  47984. DUK_ASSERT_DISABLE(idx >= 0);
  47985. DUK_ASSERT(idx < DUK_HOBJECT_GET_ESIZE(obj));
  47986. h_base[i] = idx;
  47987. break;
  47988. }
  47989. DUK_DDD(DUK_DDDPRINT("duk__hobject_alloc_entry_checked() miss %ld", (long) i));
  47990. i = (i + step) & mask;
  47991. /* Guaranteed to finish (hash is larger than #props). */
  47992. }
  47993. }
  47994. #endif /* DUK_USE_HOBJECT_HASH_PART */
  47995. /* Note: we could return the hash index here too, but it's not
  47996. * needed right now.
  47997. */
  47998. DUK_ASSERT_DISABLE(idx >= 0);
  47999. DUK_ASSERT(idx < DUK_HOBJECT_GET_ESIZE(obj));
  48000. DUK_ASSERT(idx < DUK_HOBJECT_GET_ENEXT(obj));
  48001. return (duk_int_t) idx;
  48002. }
  48003. /*
  48004. * Object internal value
  48005. *
  48006. * Returned value is guaranteed to be reachable / incref'd, caller does not need
  48007. * to incref OR decref. No proxies or accessors are invoked, no prototype walk.
  48008. */
  48009. DUK_INTERNAL duk_bool_t duk_hobject_get_internal_value(duk_heap *heap, duk_hobject *obj, duk_tval *tv_out) {
  48010. duk_int_t e_idx;
  48011. duk_int_t h_idx;
  48012. DUK_ASSERT(heap != NULL);
  48013. DUK_ASSERT(obj != NULL);
  48014. DUK_ASSERT(tv_out != NULL);
  48015. /* Always in entry part, no need to look up parents etc. */
  48016. if (duk_hobject_find_existing_entry(heap, obj, DUK_HEAP_STRING_INT_VALUE(heap), &e_idx, &h_idx)) {
  48017. DUK_ASSERT(e_idx >= 0);
  48018. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(heap, obj, e_idx));
  48019. DUK_TVAL_SET_TVAL(tv_out, DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(heap, obj, e_idx));
  48020. return 1;
  48021. }
  48022. DUK_TVAL_SET_UNDEFINED(tv_out);
  48023. return 0;
  48024. }
  48025. DUK_INTERNAL duk_hstring *duk_hobject_get_internal_value_string(duk_heap *heap, duk_hobject *obj) {
  48026. duk_tval tv;
  48027. DUK_ASSERT(heap != NULL);
  48028. DUK_ASSERT(obj != NULL);
  48029. /* This is not strictly necessary, but avoids compiler warnings; e.g.
  48030. * gcc won't reliably detect that no uninitialized data is read below.
  48031. */
  48032. duk_memzero((void *) &tv, sizeof(duk_tval));
  48033. if (duk_hobject_get_internal_value(heap, obj, &tv)) {
  48034. duk_hstring *h;
  48035. DUK_ASSERT(DUK_TVAL_IS_STRING(&tv));
  48036. h = DUK_TVAL_GET_STRING(&tv);
  48037. /* No explicit check for string vs. symbol, accept both. */
  48038. return h;
  48039. }
  48040. return NULL;
  48041. }
  48042. /*
  48043. * Arguments handling helpers (argument map mainly).
  48044. *
  48045. * An arguments object has exotic behavior for some numeric indices.
  48046. * Accesses may translate to identifier operations which may have
  48047. * arbitrary side effects (potentially invalidating any duk_tval
  48048. * pointers).
  48049. */
  48050. /* Lookup 'key' from arguments internal 'map', perform a variable lookup
  48051. * if mapped, and leave the result on top of stack (and return non-zero).
  48052. * Used in E5 Section 10.6 algorithms [[Get]] and [[GetOwnProperty]].
  48053. */
  48054. DUK_LOCAL
  48055. duk_bool_t duk__lookup_arguments_map(duk_hthread *thr,
  48056. duk_hobject *obj,
  48057. duk_hstring *key,
  48058. duk_propdesc *temp_desc,
  48059. duk_hobject **out_map,
  48060. duk_hobject **out_varenv) {
  48061. duk_hobject *map;
  48062. duk_hobject *varenv;
  48063. duk_bool_t rc;
  48064. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48065. DUK_DDD(DUK_DDDPRINT("arguments map lookup: thr=%p, obj=%p, key=%p, temp_desc=%p "
  48066. "(obj -> %!O, key -> %!O)",
  48067. (void *) thr, (void *) obj, (void *) key, (void *) temp_desc,
  48068. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  48069. if (!duk_hobject_get_own_propdesc(thr, obj, DUK_HTHREAD_STRING_INT_MAP(thr), temp_desc, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  48070. DUK_DDD(DUK_DDDPRINT("-> no 'map'"));
  48071. return 0;
  48072. }
  48073. map = duk_require_hobject(thr, -1);
  48074. DUK_ASSERT(map != NULL);
  48075. duk_pop_unsafe(thr); /* map is reachable through obj */
  48076. if (!duk_hobject_get_own_propdesc(thr, map, key, temp_desc, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  48077. DUK_DDD(DUK_DDDPRINT("-> 'map' exists, but key not in map"));
  48078. return 0;
  48079. }
  48080. /* [... varname] */
  48081. DUK_DDD(DUK_DDDPRINT("-> 'map' exists, and contains key, key is mapped to argument/variable binding %!T",
  48082. (duk_tval *) duk_get_tval(thr, -1)));
  48083. DUK_ASSERT(duk_is_string(thr, -1)); /* guaranteed when building arguments */
  48084. /* get varenv for varname (callee's declarative lexical environment) */
  48085. rc = duk_hobject_get_own_propdesc(thr, obj, DUK_HTHREAD_STRING_INT_VARENV(thr), temp_desc, DUK_GETDESC_FLAG_PUSH_VALUE);
  48086. DUK_UNREF(rc);
  48087. DUK_ASSERT(rc != 0); /* arguments MUST have an initialized lexical environment reference */
  48088. varenv = duk_require_hobject(thr, -1);
  48089. DUK_ASSERT(varenv != NULL);
  48090. duk_pop_unsafe(thr); /* varenv remains reachable through 'obj' */
  48091. DUK_DDD(DUK_DDDPRINT("arguments varenv is: %!dO", (duk_heaphdr *) varenv));
  48092. /* success: leave varname in stack */
  48093. *out_map = map;
  48094. *out_varenv = varenv;
  48095. return 1; /* [... varname] */
  48096. }
  48097. /* Lookup 'key' from arguments internal 'map', and leave replacement value
  48098. * on stack top if mapped (and return non-zero).
  48099. * Used in E5 Section 10.6 algorithm for [[GetOwnProperty]] (used by [[Get]]).
  48100. */
  48101. DUK_LOCAL duk_bool_t duk__check_arguments_map_for_get(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc) {
  48102. duk_hobject *map;
  48103. duk_hobject *varenv;
  48104. duk_hstring *varname;
  48105. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48106. if (!duk__lookup_arguments_map(thr, obj, key, temp_desc, &map, &varenv)) {
  48107. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic get behavior"));
  48108. return 0;
  48109. }
  48110. /* [... varname] */
  48111. varname = duk_require_hstring(thr, -1);
  48112. DUK_ASSERT(varname != NULL);
  48113. duk_pop_unsafe(thr); /* varname is still reachable */
  48114. DUK_DDD(DUK_DDDPRINT("arguments object automatic getvar for a bound variable; "
  48115. "key=%!O, varname=%!O",
  48116. (duk_heaphdr *) key,
  48117. (duk_heaphdr *) varname));
  48118. (void) duk_js_getvar_envrec(thr, varenv, varname, 1 /*throw*/);
  48119. /* [... value this_binding] */
  48120. duk_pop_unsafe(thr);
  48121. /* leave result on stack top */
  48122. return 1;
  48123. }
  48124. /* Lookup 'key' from arguments internal 'map', perform a variable write if mapped.
  48125. * Used in E5 Section 10.6 algorithm for [[DefineOwnProperty]] (used by [[Put]]).
  48126. * Assumes stack top contains 'put' value (which is NOT popped).
  48127. */
  48128. DUK_LOCAL void duk__check_arguments_map_for_put(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc, duk_bool_t throw_flag) {
  48129. duk_hobject *map;
  48130. duk_hobject *varenv;
  48131. duk_hstring *varname;
  48132. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48133. if (!duk__lookup_arguments_map(thr, obj, key, temp_desc, &map, &varenv)) {
  48134. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic put behavior"));
  48135. return;
  48136. }
  48137. /* [... put_value varname] */
  48138. varname = duk_require_hstring(thr, -1);
  48139. DUK_ASSERT(varname != NULL);
  48140. duk_pop_unsafe(thr); /* varname is still reachable */
  48141. DUK_DDD(DUK_DDDPRINT("arguments object automatic putvar for a bound variable; "
  48142. "key=%!O, varname=%!O, value=%!T",
  48143. (duk_heaphdr *) key,
  48144. (duk_heaphdr *) varname,
  48145. (duk_tval *) duk_require_tval(thr, -1)));
  48146. /* [... put_value] */
  48147. /*
  48148. * Note: although arguments object variable mappings are only established
  48149. * for non-strict functions (and a call to a non-strict function created
  48150. * the arguments object in question), an inner strict function may be doing
  48151. * the actual property write. Hence the throw_flag applied here comes from
  48152. * the property write call.
  48153. */
  48154. duk_js_putvar_envrec(thr, varenv, varname, duk_require_tval(thr, -1), throw_flag);
  48155. /* [... put_value] */
  48156. }
  48157. /* Lookup 'key' from arguments internal 'map', delete mapping if found.
  48158. * Used in E5 Section 10.6 algorithm for [[Delete]]. Note that the
  48159. * variable/argument itself (where the map points) is not deleted.
  48160. */
  48161. DUK_LOCAL void duk__check_arguments_map_for_delete(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *temp_desc) {
  48162. duk_hobject *map;
  48163. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48164. if (!duk_hobject_get_own_propdesc(thr, obj, DUK_HTHREAD_STRING_INT_MAP(thr), temp_desc, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  48165. DUK_DDD(DUK_DDDPRINT("arguments: key not mapped, no exotic delete behavior"));
  48166. return;
  48167. }
  48168. map = duk_require_hobject(thr, -1);
  48169. DUK_ASSERT(map != NULL);
  48170. duk_pop_unsafe(thr); /* map is reachable through obj */
  48171. DUK_DDD(DUK_DDDPRINT("-> have 'map', delete key %!O from map (if exists)); ignore result",
  48172. (duk_heaphdr *) key));
  48173. /* Note: no recursion issue, we can trust 'map' to behave */
  48174. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_BEHAVIOR(map));
  48175. DUK_DDD(DUK_DDDPRINT("map before deletion: %!O", (duk_heaphdr *) map));
  48176. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  48177. DUK_DDD(DUK_DDDPRINT("map after deletion: %!O", (duk_heaphdr *) map));
  48178. }
  48179. /*
  48180. * ECMAScript compliant [[GetOwnProperty]](P), for internal use only.
  48181. *
  48182. * If property is found:
  48183. * - Fills descriptor fields to 'out_desc'
  48184. * - If DUK_GETDESC_FLAG_PUSH_VALUE is set, pushes a value related to the
  48185. * property onto the stack ('undefined' for accessor properties).
  48186. * - Returns non-zero
  48187. *
  48188. * If property is not found:
  48189. * - 'out_desc' is left in untouched state (possibly garbage)
  48190. * - Nothing is pushed onto the stack (not even with DUK_GETDESC_FLAG_PUSH_VALUE
  48191. * set)
  48192. * - Returns zero
  48193. *
  48194. * Notes:
  48195. *
  48196. * - Getting a property descriptor may cause an allocation (and hence
  48197. * GC) to take place, hence reachability and refcount of all related
  48198. * values matter. Reallocation of value stack, properties, etc may
  48199. * invalidate many duk_tval pointers (concretely, those which reside
  48200. * in memory areas subject to reallocation). However, heap object
  48201. * pointers are never affected (heap objects have stable pointers).
  48202. *
  48203. * - The value of a plain property is always reachable and has a non-zero
  48204. * reference count.
  48205. *
  48206. * - The value of a virtual property is not necessarily reachable from
  48207. * elsewhere and may have a refcount of zero. Hence we push it onto
  48208. * the valstack for the caller, which ensures it remains reachable
  48209. * while it is needed.
  48210. *
  48211. * - There are no virtual accessor properties. Hence, all getters and
  48212. * setters are always related to concretely stored properties, which
  48213. * ensures that the get/set functions in the resulting descriptor are
  48214. * reachable and have non-zero refcounts. Should there be virtual
  48215. * accessor properties later, this would need to change.
  48216. */
  48217. DUK_LOCAL duk_bool_t duk__get_own_propdesc_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_uint32_t arr_idx, duk_propdesc *out_desc, duk_small_uint_t flags) {
  48218. duk_tval *tv;
  48219. DUK_DDD(DUK_DDDPRINT("duk_hobject_get_own_propdesc: thr=%p, obj=%p, key=%p, out_desc=%p, flags=%lx, "
  48220. "arr_idx=%ld (obj -> %!O, key -> %!O)",
  48221. (void *) thr, (void *) obj, (void *) key, (void *) out_desc,
  48222. (long) flags, (long) arr_idx,
  48223. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  48224. DUK_ASSERT(thr != NULL);
  48225. DUK_ASSERT(thr->heap != NULL);
  48226. DUK_ASSERT(obj != NULL);
  48227. DUK_ASSERT(key != NULL);
  48228. DUK_ASSERT(out_desc != NULL);
  48229. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48230. DUK_STATS_INC(thr->heap, stats_getownpropdesc_count);
  48231. /* Each code path returning 1 (= found) must fill in all the output
  48232. * descriptor fields. We don't do it beforehand because it'd be
  48233. * unnecessary work if the property isn't found and would happen
  48234. * multiple times for an inheritance chain.
  48235. */
  48236. DUK_ASSERT_SET_GARBAGE(out_desc, sizeof(*out_desc));
  48237. #if 0
  48238. out_desc->flags = 0;
  48239. out_desc->get = NULL;
  48240. out_desc->set = NULL;
  48241. out_desc->e_idx = -1;
  48242. out_desc->h_idx = -1;
  48243. out_desc->a_idx = -1;
  48244. #endif
  48245. /*
  48246. * Try entries part first because it's the common case.
  48247. *
  48248. * Array part lookups are usually handled by the array fast path, and
  48249. * are not usually inherited. Array and entry parts never contain the
  48250. * same keys so the entry part vs. array part order doesn't matter.
  48251. */
  48252. if (duk_hobject_find_existing_entry(thr->heap, obj, key, &out_desc->e_idx, &out_desc->h_idx)) {
  48253. duk_int_t e_idx = out_desc->e_idx;
  48254. DUK_ASSERT(out_desc->e_idx >= 0);
  48255. out_desc->a_idx = -1;
  48256. out_desc->flags = DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, e_idx);
  48257. out_desc->get = NULL;
  48258. out_desc->set = NULL;
  48259. if (DUK_UNLIKELY(out_desc->flags & DUK_PROPDESC_FLAG_ACCESSOR)) {
  48260. DUK_DDD(DUK_DDDPRINT("-> found accessor property in entry part"));
  48261. out_desc->get = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, e_idx);
  48262. out_desc->set = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, e_idx);
  48263. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48264. /* a dummy undefined value is pushed to make valstack
  48265. * behavior uniform for caller
  48266. */
  48267. duk_push_undefined(thr);
  48268. }
  48269. } else {
  48270. DUK_DDD(DUK_DDDPRINT("-> found plain property in entry part"));
  48271. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  48272. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48273. duk_push_tval(thr, tv);
  48274. }
  48275. }
  48276. goto prop_found;
  48277. }
  48278. /*
  48279. * Try array part.
  48280. */
  48281. if (DUK_HOBJECT_HAS_ARRAY_PART(obj) && arr_idx != DUK__NO_ARRAY_INDEX) {
  48282. if (arr_idx < DUK_HOBJECT_GET_ASIZE(obj)) {
  48283. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  48284. if (!DUK_TVAL_IS_UNUSED(tv)) {
  48285. DUK_DDD(DUK_DDDPRINT("-> found in array part"));
  48286. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48287. duk_push_tval(thr, tv);
  48288. }
  48289. /* implicit attributes */
  48290. out_desc->flags = DUK_PROPDESC_FLAG_WRITABLE |
  48291. DUK_PROPDESC_FLAG_CONFIGURABLE |
  48292. DUK_PROPDESC_FLAG_ENUMERABLE;
  48293. out_desc->get = NULL;
  48294. out_desc->set = NULL;
  48295. out_desc->e_idx = -1;
  48296. out_desc->h_idx = -1;
  48297. out_desc->a_idx = (duk_int_t) arr_idx; /* XXX: limit 2G due to being signed */
  48298. goto prop_found;
  48299. }
  48300. }
  48301. }
  48302. DUK_DDD(DUK_DDDPRINT("-> not found as a concrete property"));
  48303. /*
  48304. * Not found as a concrete property, check for virtual properties.
  48305. */
  48306. if (!DUK_HOBJECT_HAS_VIRTUAL_PROPERTIES(obj)) {
  48307. /* Quick skip. */
  48308. goto prop_not_found;
  48309. }
  48310. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  48311. duk_harray *a;
  48312. DUK_DDD(DUK_DDDPRINT("array object exotic property get for key: %!O, arr_idx: %ld",
  48313. (duk_heaphdr *) key, (long) arr_idx));
  48314. a = (duk_harray *) obj;
  48315. DUK_ASSERT_HARRAY_VALID(a);
  48316. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  48317. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  48318. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48319. duk_push_uint(thr, (duk_uint_t) a->length);
  48320. }
  48321. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  48322. if (DUK_HARRAY_LENGTH_WRITABLE(a)) {
  48323. out_desc->flags |= DUK_PROPDESC_FLAG_WRITABLE;
  48324. }
  48325. out_desc->get = NULL;
  48326. out_desc->set = NULL;
  48327. out_desc->e_idx = -1;
  48328. out_desc->h_idx = -1;
  48329. out_desc->a_idx = -1;
  48330. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  48331. goto prop_found_noexotic; /* cannot be arguments exotic */
  48332. }
  48333. } else if (DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(obj)) {
  48334. DUK_DDD(DUK_DDDPRINT("string object exotic property get for key: %!O, arr_idx: %ld",
  48335. (duk_heaphdr *) key, (long) arr_idx));
  48336. /* XXX: charlen; avoid multiple lookups? */
  48337. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  48338. duk_hstring *h_val;
  48339. DUK_DDD(DUK_DDDPRINT("array index exists"));
  48340. h_val = duk_hobject_get_internal_value_string(thr->heap, obj);
  48341. DUK_ASSERT(h_val);
  48342. if (arr_idx < DUK_HSTRING_GET_CHARLEN(h_val)) {
  48343. DUK_DDD(DUK_DDDPRINT("-> found, array index inside string"));
  48344. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48345. duk_push_hstring(thr, h_val);
  48346. duk_substring(thr, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
  48347. }
  48348. out_desc->flags = DUK_PROPDESC_FLAG_ENUMERABLE | /* E5 Section 15.5.5.2 */
  48349. DUK_PROPDESC_FLAG_VIRTUAL;
  48350. out_desc->get = NULL;
  48351. out_desc->set = NULL;
  48352. out_desc->e_idx = -1;
  48353. out_desc->h_idx = -1;
  48354. out_desc->a_idx = -1;
  48355. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  48356. goto prop_found_noexotic; /* cannot be arguments exotic */
  48357. } else {
  48358. /* index is above internal string length -> property is fully normal */
  48359. DUK_DDD(DUK_DDDPRINT("array index outside string -> normal property"));
  48360. }
  48361. } else if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  48362. duk_hstring *h_val;
  48363. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  48364. h_val = duk_hobject_get_internal_value_string(thr->heap, obj);
  48365. DUK_ASSERT(h_val != NULL);
  48366. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48367. duk_push_uint(thr, (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h_val));
  48368. }
  48369. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL; /* E5 Section 15.5.5.1 */
  48370. out_desc->get = NULL;
  48371. out_desc->set = NULL;
  48372. out_desc->e_idx = -1;
  48373. out_desc->h_idx = -1;
  48374. out_desc->a_idx = -1;
  48375. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  48376. goto prop_found_noexotic; /* cannot be arguments exotic */
  48377. }
  48378. }
  48379. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  48380. else if (DUK_HOBJECT_IS_BUFOBJ(obj)) {
  48381. duk_hbufobj *h_bufobj;
  48382. duk_uint_t byte_off;
  48383. duk_small_uint_t elem_size;
  48384. h_bufobj = (duk_hbufobj *) obj;
  48385. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  48386. DUK_DDD(DUK_DDDPRINT("bufobj property get for key: %!O, arr_idx: %ld",
  48387. (duk_heaphdr *) key, (long) arr_idx));
  48388. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HBUFOBJ_HAS_VIRTUAL_INDICES(h_bufobj)) {
  48389. DUK_DDD(DUK_DDDPRINT("array index exists"));
  48390. /* Careful with wrapping: arr_idx upshift may easily wrap, whereas
  48391. * length downshift won't.
  48392. */
  48393. if (arr_idx < (h_bufobj->length >> h_bufobj->shift)) {
  48394. byte_off = arr_idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  48395. elem_size = (duk_small_uint_t) (1U << h_bufobj->shift);
  48396. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48397. duk_uint8_t *data;
  48398. if (h_bufobj->buf != NULL && DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  48399. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  48400. duk_hbufobj_push_validated_read(thr, h_bufobj, data, elem_size);
  48401. } else {
  48402. DUK_D(DUK_DPRINT("bufobj access out of underlying buffer, ignoring (read zero)"));
  48403. duk_push_uint(thr, 0);
  48404. }
  48405. }
  48406. out_desc->flags = DUK_PROPDESC_FLAG_WRITABLE |
  48407. DUK_PROPDESC_FLAG_VIRTUAL;
  48408. if (DUK_HOBJECT_GET_CLASS_NUMBER(obj) != DUK_HOBJECT_CLASS_ARRAYBUFFER) {
  48409. /* ArrayBuffer indices are non-standard and are
  48410. * non-enumerable to avoid their serialization.
  48411. */
  48412. out_desc->flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  48413. }
  48414. out_desc->get = NULL;
  48415. out_desc->set = NULL;
  48416. out_desc->e_idx = -1;
  48417. out_desc->h_idx = -1;
  48418. out_desc->a_idx = -1;
  48419. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  48420. goto prop_found_noexotic; /* cannot be e.g. arguments exotic, since exotic 'traits' are mutually exclusive */
  48421. } else {
  48422. /* index is above internal buffer length -> property is fully normal */
  48423. DUK_DDD(DUK_DDDPRINT("array index outside buffer -> normal property"));
  48424. }
  48425. } else if (key == DUK_HTHREAD_STRING_LENGTH(thr) && DUK_HBUFOBJ_HAS_VIRTUAL_INDICES(h_bufobj)) {
  48426. DUK_DDD(DUK_DDDPRINT("-> found, key is 'length', length exotic behavior"));
  48427. if (flags & DUK_GETDESC_FLAG_PUSH_VALUE) {
  48428. /* Length in elements: take into account shift, but
  48429. * intentionally don't check the underlying buffer here.
  48430. */
  48431. duk_push_uint(thr, h_bufobj->length >> h_bufobj->shift);
  48432. }
  48433. out_desc->flags = DUK_PROPDESC_FLAG_VIRTUAL;
  48434. out_desc->get = NULL;
  48435. out_desc->set = NULL;
  48436. out_desc->e_idx = -1;
  48437. out_desc->h_idx = -1;
  48438. out_desc->a_idx = -1;
  48439. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj));
  48440. goto prop_found_noexotic; /* cannot be arguments exotic */
  48441. }
  48442. }
  48443. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  48444. /* Array properties have exotic behavior but they are concrete,
  48445. * so no special handling here.
  48446. *
  48447. * Arguments exotic behavior (E5 Section 10.6, [[GetOwnProperty]]
  48448. * is only relevant as a post-check implemented below; hence no
  48449. * check here.
  48450. */
  48451. /*
  48452. * Not found as concrete or virtual.
  48453. */
  48454. prop_not_found:
  48455. DUK_DDD(DUK_DDDPRINT("-> not found (virtual, entry part, or array part)"));
  48456. DUK_STATS_INC(thr->heap, stats_getownpropdesc_miss);
  48457. return 0;
  48458. /*
  48459. * Found.
  48460. *
  48461. * Arguments object has exotic post-processing, see E5 Section 10.6,
  48462. * description of [[GetOwnProperty]] variant for arguments.
  48463. */
  48464. prop_found:
  48465. DUK_DDD(DUK_DDDPRINT("-> property found, checking for arguments exotic post-behavior"));
  48466. /* Notes:
  48467. * - Only numbered indices are relevant, so arr_idx fast reject is good
  48468. * (this is valid unless there are more than 4**32-1 arguments).
  48469. * - Since variable lookup has no side effects, this can be skipped if
  48470. * DUK_GETDESC_FLAG_PUSH_VALUE is not set.
  48471. */
  48472. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj) &&
  48473. arr_idx != DUK__NO_ARRAY_INDEX &&
  48474. (flags & DUK_GETDESC_FLAG_PUSH_VALUE))) {
  48475. duk_propdesc temp_desc;
  48476. /* Magically bound variable cannot be an accessor. However,
  48477. * there may be an accessor property (or a plain property) in
  48478. * place with magic behavior removed. This happens e.g. when
  48479. * a magic property is redefined with defineProperty().
  48480. * Cannot assert for "not accessor" here.
  48481. */
  48482. /* replaces top of stack with new value if necessary */
  48483. DUK_ASSERT((flags & DUK_GETDESC_FLAG_PUSH_VALUE) != 0);
  48484. /* This can perform a variable lookup but only into a declarative
  48485. * environment which has no side effects.
  48486. */
  48487. if (duk__check_arguments_map_for_get(thr, obj, key, &temp_desc)) {
  48488. DUK_DDD(DUK_DDDPRINT("-> arguments exotic behavior overrides result: %!T -> %!T",
  48489. (duk_tval *) duk_get_tval(thr, -2),
  48490. (duk_tval *) duk_get_tval(thr, -1)));
  48491. /* [... old_result result] -> [... result] */
  48492. duk_remove_m2(thr);
  48493. }
  48494. }
  48495. prop_found_noexotic:
  48496. DUK_STATS_INC(thr->heap, stats_getownpropdesc_hit);
  48497. return 1;
  48498. }
  48499. DUK_INTERNAL duk_bool_t duk_hobject_get_own_propdesc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags) {
  48500. DUK_ASSERT(thr != NULL);
  48501. DUK_ASSERT(obj != NULL);
  48502. DUK_ASSERT(key != NULL);
  48503. DUK_ASSERT(out_desc != NULL);
  48504. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48505. return duk__get_own_propdesc_raw(thr, obj, key, DUK_HSTRING_GET_ARRIDX_SLOW(key), out_desc, flags);
  48506. }
  48507. /*
  48508. * ECMAScript compliant [[GetProperty]](P), for internal use only.
  48509. *
  48510. * If property is found:
  48511. * - Fills descriptor fields to 'out_desc'
  48512. * - If DUK_GETDESC_FLAG_PUSH_VALUE is set, pushes a value related to the
  48513. * property onto the stack ('undefined' for accessor properties).
  48514. * - Returns non-zero
  48515. *
  48516. * If property is not found:
  48517. * - 'out_desc' is left in untouched state (possibly garbage)
  48518. * - Nothing is pushed onto the stack (not even with DUK_GETDESC_FLAG_PUSH_VALUE
  48519. * set)
  48520. * - Returns zero
  48521. *
  48522. * May cause arbitrary side effects and invalidate (most) duk_tval
  48523. * pointers.
  48524. */
  48525. DUK_LOCAL duk_bool_t duk__get_propdesc(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_propdesc *out_desc, duk_small_uint_t flags) {
  48526. duk_hobject *curr;
  48527. duk_uint32_t arr_idx;
  48528. duk_uint_t sanity;
  48529. DUK_ASSERT(thr != NULL);
  48530. DUK_ASSERT(thr->heap != NULL);
  48531. DUK_ASSERT(obj != NULL);
  48532. DUK_ASSERT(key != NULL);
  48533. DUK_ASSERT(out_desc != NULL);
  48534. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48535. DUK_STATS_INC(thr->heap, stats_getpropdesc_count);
  48536. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  48537. DUK_DDD(DUK_DDDPRINT("duk__get_propdesc: thr=%p, obj=%p, key=%p, out_desc=%p, flags=%lx, "
  48538. "arr_idx=%ld (obj -> %!O, key -> %!O)",
  48539. (void *) thr, (void *) obj, (void *) key, (void *) out_desc,
  48540. (long) flags, (long) arr_idx,
  48541. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  48542. curr = obj;
  48543. DUK_ASSERT(curr != NULL);
  48544. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  48545. do {
  48546. if (duk__get_own_propdesc_raw(thr, curr, key, arr_idx, out_desc, flags)) {
  48547. /* stack contains value (if requested), 'out_desc' is set */
  48548. DUK_STATS_INC(thr->heap, stats_getpropdesc_hit);
  48549. return 1;
  48550. }
  48551. /* not found in 'curr', next in prototype chain; impose max depth */
  48552. if (DUK_UNLIKELY(sanity-- == 0)) {
  48553. if (flags & DUK_GETDESC_FLAG_IGNORE_PROTOLOOP) {
  48554. /* treat like property not found */
  48555. break;
  48556. } else {
  48557. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  48558. DUK_WO_NORETURN(return 0;);
  48559. }
  48560. }
  48561. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  48562. } while (curr != NULL);
  48563. /* out_desc is left untouched (possibly garbage), caller must use return
  48564. * value to determine whether out_desc can be looked up
  48565. */
  48566. DUK_STATS_INC(thr->heap, stats_getpropdesc_miss);
  48567. return 0;
  48568. }
  48569. /*
  48570. * Shallow fast path checks for accessing array elements with numeric
  48571. * indices. The goal is to try to avoid coercing an array index to an
  48572. * (interned) string for the most common lookups, in particular, for
  48573. * standard Array objects.
  48574. *
  48575. * Interning is avoided but only for a very narrow set of cases:
  48576. * - Object has array part, index is within array allocation, and
  48577. * value is not unused (= key exists)
  48578. * - Object has no interfering exotic behavior (e.g. arguments or
  48579. * string object exotic behaviors interfere, array exotic
  48580. * behavior does not).
  48581. *
  48582. * Current shortcoming: if key does not exist (even if it is within
  48583. * the array allocation range) a slow path lookup with interning is
  48584. * always required. This can probably be fixed so that there is a
  48585. * quick fast path for non-existent elements as well, at least for
  48586. * standard Array objects.
  48587. */
  48588. #if defined(DUK_USE_ARRAY_PROP_FASTPATH)
  48589. DUK_LOCAL duk_tval *duk__getprop_shallow_fastpath_array_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key) {
  48590. duk_tval *tv;
  48591. duk_uint32_t idx;
  48592. DUK_UNREF(thr);
  48593. if (!(DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  48594. !DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj) &&
  48595. !DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(obj) &&
  48596. !DUK_HOBJECT_IS_BUFOBJ(obj) &&
  48597. !DUK_HOBJECT_IS_PROXY(obj))) {
  48598. /* Must have array part and no conflicting exotic behaviors.
  48599. * Doesn't need to have array special behavior, e.g. Arguments
  48600. * object has array part.
  48601. */
  48602. return NULL;
  48603. }
  48604. /* Arrays never have other exotic behaviors. */
  48605. DUK_DDD(DUK_DDDPRINT("fast path attempt (no exotic string/arguments/buffer "
  48606. "behavior, object has array part)"));
  48607. #if defined(DUK_USE_FASTINT)
  48608. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  48609. idx = duk__tval_fastint_to_arr_idx(tv_key);
  48610. } else
  48611. #endif
  48612. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  48613. idx = duk__tval_number_to_arr_idx(tv_key);
  48614. } else {
  48615. DUK_DDD(DUK_DDDPRINT("key is not a number"));
  48616. return NULL;
  48617. }
  48618. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  48619. * is 0xffffffffUL. We don't need to check for that explicitly
  48620. * because 0xffffffffUL will never be inside object 'a_size'.
  48621. */
  48622. if (idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  48623. DUK_DDD(DUK_DDDPRINT("key is not an array index or outside array part"));
  48624. return NULL;
  48625. }
  48626. DUK_ASSERT(idx != 0xffffffffUL);
  48627. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  48628. /* XXX: for array instances we could take a shortcut here and assume
  48629. * Array.prototype doesn't contain an array index property.
  48630. */
  48631. DUK_DDD(DUK_DDDPRINT("key is a valid array index and inside array part"));
  48632. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, idx);
  48633. if (!DUK_TVAL_IS_UNUSED(tv)) {
  48634. DUK_DDD(DUK_DDDPRINT("-> fast path successful"));
  48635. return tv;
  48636. }
  48637. DUK_DDD(DUK_DDDPRINT("fast path attempt failed, fall back to slow path"));
  48638. return NULL;
  48639. }
  48640. DUK_LOCAL duk_bool_t duk__putprop_shallow_fastpath_array_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key, duk_tval *tv_val) {
  48641. duk_tval *tv;
  48642. duk_harray *a;
  48643. duk_uint32_t idx;
  48644. duk_uint32_t old_len, new_len;
  48645. if (!(DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj) &&
  48646. DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  48647. DUK_HOBJECT_HAS_EXTENSIBLE(obj))) {
  48648. return 0;
  48649. }
  48650. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)); /* caller ensures */
  48651. a = (duk_harray *) obj;
  48652. DUK_ASSERT_HARRAY_VALID(a);
  48653. #if defined(DUK_USE_FASTINT)
  48654. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  48655. idx = duk__tval_fastint_to_arr_idx(tv_key);
  48656. } else
  48657. #endif
  48658. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  48659. idx = duk__tval_number_to_arr_idx(tv_key);
  48660. } else {
  48661. DUK_DDD(DUK_DDDPRINT("key is not a number"));
  48662. return 0;
  48663. }
  48664. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  48665. * is 0xffffffffUL. We don't need to check for that explicitly
  48666. * because 0xffffffffUL will never be inside object 'a_size'.
  48667. */
  48668. if (idx >= DUK_HOBJECT_GET_ASIZE(obj)) { /* for resizing of array part, use slow path */
  48669. return 0;
  48670. }
  48671. DUK_ASSERT(idx != 0xffffffffUL);
  48672. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  48673. old_len = a->length;
  48674. if (idx >= old_len) {
  48675. DUK_DDD(DUK_DDDPRINT("write new array entry requires length update "
  48676. "(arr_idx=%ld, old_len=%ld)",
  48677. (long) idx, (long) old_len));
  48678. if (DUK_HARRAY_LENGTH_NONWRITABLE(a)) {
  48679. /* The correct behavior here is either a silent error
  48680. * or a TypeError, depending on strictness. Fall back
  48681. * to the slow path to handle the situation.
  48682. */
  48683. return 0;
  48684. }
  48685. new_len = idx + 1;
  48686. ((duk_harray *) obj)->length = new_len;
  48687. }
  48688. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, idx);
  48689. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv_val); /* side effects */
  48690. DUK_DDD(DUK_DDDPRINT("array fast path success for index %ld", (long) idx));
  48691. return 1;
  48692. }
  48693. #endif /* DUK_USE_ARRAY_PROP_FASTPATH */
  48694. /*
  48695. * Fast path for bufobj getprop/putprop
  48696. */
  48697. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  48698. DUK_LOCAL duk_bool_t duk__getprop_fastpath_bufobj_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key) {
  48699. duk_uint32_t idx;
  48700. duk_hbufobj *h_bufobj;
  48701. duk_uint_t byte_off;
  48702. duk_small_uint_t elem_size;
  48703. duk_uint8_t *data;
  48704. if (!DUK_HOBJECT_IS_BUFOBJ(obj)) {
  48705. return 0;
  48706. }
  48707. h_bufobj = (duk_hbufobj *) obj;
  48708. if (!DUK_HBUFOBJ_HAS_VIRTUAL_INDICES(h_bufobj)) {
  48709. return 0;
  48710. }
  48711. #if defined(DUK_USE_FASTINT)
  48712. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  48713. idx = duk__tval_fastint_to_arr_idx(tv_key);
  48714. } else
  48715. #endif
  48716. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  48717. idx = duk__tval_number_to_arr_idx(tv_key);
  48718. } else {
  48719. return 0;
  48720. }
  48721. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  48722. * is 0xffffffffUL. We don't need to check for that explicitly
  48723. * because 0xffffffffUL will never be inside bufobj length.
  48724. */
  48725. /* Careful with wrapping (left shifting idx would be unsafe). */
  48726. if (idx >= (h_bufobj->length >> h_bufobj->shift)) {
  48727. return 0;
  48728. }
  48729. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  48730. byte_off = idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  48731. elem_size = (duk_small_uint_t) (1U << h_bufobj->shift);
  48732. if (h_bufobj->buf != NULL && DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  48733. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  48734. duk_hbufobj_push_validated_read(thr, h_bufobj, data, elem_size);
  48735. } else {
  48736. DUK_D(DUK_DPRINT("bufobj access out of underlying buffer, ignoring (read zero)"));
  48737. duk_push_uint(thr, 0);
  48738. }
  48739. return 1;
  48740. }
  48741. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  48742. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  48743. DUK_LOCAL duk_bool_t duk__putprop_fastpath_bufobj_tval(duk_hthread *thr, duk_hobject *obj, duk_tval *tv_key, duk_tval *tv_val) {
  48744. duk_uint32_t idx;
  48745. duk_hbufobj *h_bufobj;
  48746. duk_uint_t byte_off;
  48747. duk_small_uint_t elem_size;
  48748. duk_uint8_t *data;
  48749. if (!(DUK_HOBJECT_IS_BUFOBJ(obj) &&
  48750. DUK_TVAL_IS_NUMBER(tv_val))) {
  48751. return 0;
  48752. }
  48753. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)); /* caller ensures; rom objects are never bufobjs now */
  48754. h_bufobj = (duk_hbufobj *) obj;
  48755. if (!DUK_HBUFOBJ_HAS_VIRTUAL_INDICES(h_bufobj)) {
  48756. return 0;
  48757. }
  48758. #if defined(DUK_USE_FASTINT)
  48759. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  48760. idx = duk__tval_fastint_to_arr_idx(tv_key);
  48761. } else
  48762. #endif
  48763. if (DUK_TVAL_IS_DOUBLE(tv_key)) {
  48764. idx = duk__tval_number_to_arr_idx(tv_key);
  48765. } else {
  48766. return 0;
  48767. }
  48768. /* If index is not valid, idx will be DUK__NO_ARRAY_INDEX which
  48769. * is 0xffffffffUL. We don't need to check for that explicitly
  48770. * because 0xffffffffUL will never be inside bufobj length.
  48771. */
  48772. /* Careful with wrapping (left shifting idx would be unsafe). */
  48773. if (idx >= (h_bufobj->length >> h_bufobj->shift)) {
  48774. return 0;
  48775. }
  48776. DUK_ASSERT(idx != DUK__NO_ARRAY_INDEX);
  48777. byte_off = idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  48778. elem_size = (duk_small_uint_t) (1U << h_bufobj->shift);
  48779. /* Value is required to be a number in the fast path so there
  48780. * are no side effects in write coercion.
  48781. */
  48782. duk_push_tval(thr, tv_val);
  48783. DUK_ASSERT(duk_is_number(thr, -1));
  48784. if (h_bufobj->buf != NULL && DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  48785. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  48786. duk_hbufobj_validated_write(thr, h_bufobj, data, elem_size);
  48787. } else {
  48788. DUK_D(DUK_DPRINT("bufobj access out of underlying buffer, ignoring (write skipped)"));
  48789. }
  48790. duk_pop_unsafe(thr);
  48791. return 1;
  48792. }
  48793. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  48794. /*
  48795. * GETPROP: ECMAScript property read.
  48796. */
  48797. DUK_INTERNAL duk_bool_t duk_hobject_getprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
  48798. duk_tval tv_obj_copy;
  48799. duk_tval tv_key_copy;
  48800. duk_hobject *curr = NULL;
  48801. duk_hstring *key = NULL;
  48802. duk_uint32_t arr_idx = DUK__NO_ARRAY_INDEX;
  48803. duk_propdesc desc;
  48804. duk_uint_t sanity;
  48805. DUK_DDD(DUK_DDDPRINT("getprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  48806. (void *) thr, (void *) tv_obj, (void *) tv_key,
  48807. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  48808. DUK_ASSERT(thr != NULL);
  48809. DUK_ASSERT(thr->heap != NULL);
  48810. DUK_ASSERT(tv_obj != NULL);
  48811. DUK_ASSERT(tv_key != NULL);
  48812. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  48813. DUK_STATS_INC(thr->heap, stats_getprop_all);
  48814. /*
  48815. * Make a copy of tv_obj, tv_key, and tv_val to avoid any issues of
  48816. * them being invalidated by a valstack resize.
  48817. *
  48818. * XXX: this is now an overkill for many fast paths. Rework this
  48819. * to be faster (although switching to a valstack discipline might
  48820. * be a better solution overall).
  48821. */
  48822. DUK_TVAL_SET_TVAL(&tv_obj_copy, tv_obj);
  48823. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  48824. tv_obj = &tv_obj_copy;
  48825. tv_key = &tv_key_copy;
  48826. /*
  48827. * Coercion and fast path processing
  48828. */
  48829. switch (DUK_TVAL_GET_TAG(tv_obj)) {
  48830. case DUK_TAG_UNDEFINED:
  48831. case DUK_TAG_NULL: {
  48832. /* Note: unconditional throw */
  48833. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject"));
  48834. #if defined(DUK_USE_PARANOID_ERRORS)
  48835. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_BASE);
  48836. #else
  48837. DUK_ERROR_FMT2(thr, DUK_ERR_TYPE_ERROR, "cannot read property %s of %s",
  48838. duk_push_string_tval_readable(thr, tv_key), duk_push_string_tval_readable(thr, tv_obj));
  48839. #endif
  48840. DUK_WO_NORETURN(return 0;);
  48841. break;
  48842. }
  48843. case DUK_TAG_BOOLEAN: {
  48844. DUK_DDD(DUK_DDDPRINT("base object is a boolean, start lookup from boolean prototype"));
  48845. curr = thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE];
  48846. break;
  48847. }
  48848. case DUK_TAG_STRING: {
  48849. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  48850. duk_int_t pop_count;
  48851. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  48852. /* Symbols (ES2015 or hidden) don't have virtual properties. */
  48853. DUK_DDD(DUK_DDDPRINT("base object is a symbol, start lookup from symbol prototype"));
  48854. curr = thr->builtins[DUK_BIDX_SYMBOL_PROTOTYPE];
  48855. break;
  48856. }
  48857. #if defined(DUK_USE_FASTINT)
  48858. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  48859. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  48860. DUK_DDD(DUK_DDDPRINT("base object string, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  48861. pop_count = 0;
  48862. } else
  48863. #endif
  48864. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  48865. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  48866. DUK_DDD(DUK_DDDPRINT("base object string, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  48867. pop_count = 0;
  48868. } else {
  48869. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  48870. DUK_ASSERT(key != NULL);
  48871. DUK_DDD(DUK_DDDPRINT("base object string, key is a non-fast-path number; after "
  48872. "coercion key is %!T, arr_idx %ld",
  48873. (duk_tval *) duk_get_tval(thr, -1), (long) arr_idx));
  48874. pop_count = 1;
  48875. }
  48876. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  48877. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  48878. duk_pop_n_unsafe(thr, pop_count);
  48879. duk_push_hstring(thr, h);
  48880. duk_substring(thr, -1, arr_idx, arr_idx + 1); /* [str] -> [substr] */
  48881. DUK_STATS_INC(thr->heap, stats_getprop_stringidx);
  48882. DUK_DDD(DUK_DDDPRINT("-> %!T (base is string, key is an index inside string length "
  48883. "after coercion -> return char)",
  48884. (duk_tval *) duk_get_tval(thr, -1)));
  48885. return 1;
  48886. }
  48887. if (pop_count == 0) {
  48888. /* This is a pretty awkward control flow, but we need to recheck the
  48889. * key coercion here.
  48890. */
  48891. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  48892. DUK_ASSERT(key != NULL);
  48893. DUK_DDD(DUK_DDDPRINT("base object string, key is a non-fast-path number; after "
  48894. "coercion key is %!T, arr_idx %ld",
  48895. (duk_tval *) duk_get_tval(thr, -1), (long) arr_idx));
  48896. }
  48897. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  48898. duk_pop_unsafe(thr); /* [key] -> [] */
  48899. duk_push_uint(thr, (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h)); /* [] -> [res] */
  48900. DUK_STATS_INC(thr->heap, stats_getprop_stringlen);
  48901. DUK_DDD(DUK_DDDPRINT("-> %!T (base is string, key is 'length' after coercion -> "
  48902. "return string length)",
  48903. (duk_tval *) duk_get_tval(thr, -1)));
  48904. return 1;
  48905. }
  48906. DUK_DDD(DUK_DDDPRINT("base object is a string, start lookup from string prototype"));
  48907. curr = thr->builtins[DUK_BIDX_STRING_PROTOTYPE];
  48908. goto lookup; /* avoid double coercion */
  48909. }
  48910. case DUK_TAG_OBJECT: {
  48911. #if defined(DUK_USE_ARRAY_PROP_FASTPATH)
  48912. duk_tval *tmp;
  48913. #endif
  48914. curr = DUK_TVAL_GET_OBJECT(tv_obj);
  48915. DUK_ASSERT(curr != NULL);
  48916. /* XXX: array .length fast path (important in e.g. loops)? */
  48917. #if defined(DUK_USE_ARRAY_PROP_FASTPATH)
  48918. tmp = duk__getprop_shallow_fastpath_array_tval(thr, curr, tv_key);
  48919. if (tmp) {
  48920. duk_push_tval(thr, tmp);
  48921. DUK_DDD(DUK_DDDPRINT("-> %!T (base is object, key is a number, array part "
  48922. "fast path)",
  48923. (duk_tval *) duk_get_tval(thr, -1)));
  48924. DUK_STATS_INC(thr->heap, stats_getprop_arrayidx);
  48925. return 1;
  48926. }
  48927. #endif
  48928. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  48929. if (duk__getprop_fastpath_bufobj_tval(thr, curr, tv_key) != 0) {
  48930. /* Read value pushed on stack. */
  48931. DUK_DDD(DUK_DDDPRINT("-> %!T (base is bufobj, key is a number, bufobj "
  48932. "fast path)",
  48933. (duk_tval *) duk_get_tval(thr, -1)));
  48934. DUK_STATS_INC(thr->heap, stats_getprop_bufobjidx);
  48935. return 1;
  48936. }
  48937. #endif
  48938. #if defined(DUK_USE_ES6_PROXY)
  48939. if (DUK_UNLIKELY(DUK_HOBJECT_IS_PROXY(curr))) {
  48940. duk_hobject *h_target;
  48941. if (duk__proxy_check_prop(thr, curr, DUK_STRIDX_GET, tv_key, &h_target)) {
  48942. /* -> [ ... trap handler ] */
  48943. DUK_DDD(DUK_DDDPRINT("-> proxy object 'get' for key %!T", (duk_tval *) tv_key));
  48944. DUK_STATS_INC(thr->heap, stats_getprop_proxy);
  48945. duk_push_hobject(thr, h_target); /* target */
  48946. duk_push_tval(thr, tv_key); /* P */
  48947. duk_push_tval(thr, tv_obj); /* Receiver: Proxy object */
  48948. duk_call_method(thr, 3 /*nargs*/);
  48949. /* Target object must be checked for a conflicting
  48950. * non-configurable property.
  48951. */
  48952. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  48953. DUK_ASSERT(key != NULL);
  48954. if (duk__get_own_propdesc_raw(thr, h_target, key, arr_idx, &desc, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  48955. duk_tval *tv_hook = duk_require_tval(thr, -3); /* value from hook */
  48956. duk_tval *tv_targ = duk_require_tval(thr, -1); /* value from target */
  48957. duk_bool_t datadesc_reject;
  48958. duk_bool_t accdesc_reject;
  48959. DUK_DDD(DUK_DDDPRINT("proxy 'get': target has matching property %!O, check for "
  48960. "conflicting property; tv_hook=%!T, tv_targ=%!T, desc.flags=0x%08lx, "
  48961. "desc.get=%p, desc.set=%p",
  48962. (duk_heaphdr *) key, (duk_tval *) tv_hook, (duk_tval *) tv_targ,
  48963. (unsigned long) desc.flags,
  48964. (void *) desc.get, (void *) desc.set));
  48965. datadesc_reject = !(desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  48966. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  48967. !(desc.flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  48968. !duk_js_samevalue(tv_hook, tv_targ);
  48969. accdesc_reject = (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  48970. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  48971. (desc.get == NULL) &&
  48972. !DUK_TVAL_IS_UNDEFINED(tv_hook);
  48973. if (datadesc_reject || accdesc_reject) {
  48974. DUK_ERROR_TYPE(thr, DUK_STR_PROXY_REJECTED);
  48975. DUK_WO_NORETURN(return 0;);
  48976. }
  48977. duk_pop_2_unsafe(thr);
  48978. } else {
  48979. duk_pop_unsafe(thr);
  48980. }
  48981. return 1; /* return value */
  48982. }
  48983. curr = h_target; /* resume lookup from target */
  48984. DUK_TVAL_SET_OBJECT(tv_obj, curr);
  48985. }
  48986. #endif /* DUK_USE_ES6_PROXY */
  48987. if (DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(curr)) {
  48988. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  48989. DUK_ASSERT(key != NULL);
  48990. DUK_STATS_INC(thr->heap, stats_getprop_arguments);
  48991. if (duk__check_arguments_map_for_get(thr, curr, key, &desc)) {
  48992. DUK_DDD(DUK_DDDPRINT("-> %!T (base is object with arguments exotic behavior, "
  48993. "key matches magically bound property -> skip standard "
  48994. "Get with replacement value)",
  48995. (duk_tval *) duk_get_tval(thr, -1)));
  48996. /* no need for 'caller' post-check, because 'key' must be an array index */
  48997. duk_remove_m2(thr); /* [key result] -> [result] */
  48998. return 1;
  48999. }
  49000. goto lookup; /* avoid double coercion */
  49001. }
  49002. break;
  49003. }
  49004. /* Buffer has virtual properties similar to string, but indexed values
  49005. * are numbers, not 1-byte buffers/strings which would perform badly.
  49006. */
  49007. case DUK_TAG_BUFFER: {
  49008. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  49009. duk_int_t pop_count;
  49010. /*
  49011. * Because buffer values are often looped over, a number fast path
  49012. * is important.
  49013. */
  49014. #if defined(DUK_USE_FASTINT)
  49015. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  49016. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  49017. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  49018. pop_count = 0;
  49019. }
  49020. else
  49021. #endif
  49022. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  49023. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  49024. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  49025. pop_count = 0;
  49026. } else {
  49027. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49028. DUK_ASSERT(key != NULL);
  49029. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  49030. "coercion key is %!T, arr_idx %ld",
  49031. (duk_tval *) duk_get_tval(thr, -1), (long) arr_idx));
  49032. pop_count = 1;
  49033. }
  49034. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  49035. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  49036. duk_pop_n_unsafe(thr, pop_count);
  49037. duk_push_uint(thr, ((duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h))[arr_idx]);
  49038. DUK_STATS_INC(thr->heap, stats_getprop_bufferidx);
  49039. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is an index inside buffer length "
  49040. "after coercion -> return byte as number)",
  49041. (duk_tval *) duk_get_tval(thr, -1)));
  49042. return 1;
  49043. }
  49044. if (pop_count == 0) {
  49045. /* This is a pretty awkward control flow, but we need to recheck the
  49046. * key coercion here.
  49047. */
  49048. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49049. DUK_ASSERT(key != NULL);
  49050. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  49051. "coercion key is %!T, arr_idx %ld",
  49052. (duk_tval *) duk_get_tval(thr, -1), (long) arr_idx));
  49053. }
  49054. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  49055. duk_pop_unsafe(thr); /* [key] -> [] */
  49056. duk_push_uint(thr, (duk_uint_t) DUK_HBUFFER_GET_SIZE(h)); /* [] -> [res] */
  49057. DUK_STATS_INC(thr->heap, stats_getprop_bufferlen);
  49058. DUK_DDD(DUK_DDDPRINT("-> %!T (base is buffer, key is 'length' "
  49059. "after coercion -> return buffer length)",
  49060. (duk_tval *) duk_get_tval(thr, -1)));
  49061. return 1;
  49062. }
  49063. DUK_DDD(DUK_DDDPRINT("base object is a buffer, start lookup from Uint8Array prototype"));
  49064. curr = thr->builtins[DUK_BIDX_UINT8ARRAY_PROTOTYPE];
  49065. goto lookup; /* avoid double coercion */
  49066. }
  49067. case DUK_TAG_POINTER: {
  49068. DUK_DDD(DUK_DDDPRINT("base object is a pointer, start lookup from pointer prototype"));
  49069. curr = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  49070. break;
  49071. }
  49072. case DUK_TAG_LIGHTFUNC: {
  49073. /* Lightfuncs inherit getter .name and .length from %NativeFunctionPrototype%. */
  49074. DUK_DDD(DUK_DDDPRINT("base object is a lightfunc, start lookup from function prototype"));
  49075. curr = thr->builtins[DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE];
  49076. break;
  49077. }
  49078. #if defined(DUK_USE_FASTINT)
  49079. case DUK_TAG_FASTINT:
  49080. #endif
  49081. default: {
  49082. /* number */
  49083. DUK_DDD(DUK_DDDPRINT("base object is a number, start lookup from number prototype"));
  49084. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_obj));
  49085. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_obj));
  49086. curr = thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE];
  49087. break;
  49088. }
  49089. }
  49090. /* key coercion (unless already coerced above) */
  49091. DUK_ASSERT(key == NULL);
  49092. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49093. DUK_ASSERT(key != NULL);
  49094. /*
  49095. * Property lookup
  49096. */
  49097. lookup:
  49098. /* [key] (coerced) */
  49099. DUK_ASSERT(curr != NULL);
  49100. DUK_ASSERT(key != NULL);
  49101. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  49102. do {
  49103. if (!duk__get_own_propdesc_raw(thr, curr, key, arr_idx, &desc, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  49104. goto next_in_chain;
  49105. }
  49106. if (desc.get != NULL) {
  49107. /* accessor with defined getter */
  49108. DUK_ASSERT((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) != 0);
  49109. duk_pop_unsafe(thr); /* [key undefined] -> [key] */
  49110. duk_push_hobject(thr, desc.get);
  49111. duk_push_tval(thr, tv_obj); /* note: original, uncoerced base */
  49112. #if defined(DUK_USE_NONSTD_GETTER_KEY_ARGUMENT)
  49113. duk_dup_m3(thr);
  49114. duk_call_method(thr, 1); /* [key getter this key] -> [key retval] */
  49115. #else
  49116. duk_call_method(thr, 0); /* [key getter this] -> [key retval] */
  49117. #endif
  49118. } else {
  49119. /* [key value] or [key undefined] */
  49120. /* data property or accessor without getter */
  49121. DUK_ASSERT(((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0) ||
  49122. (desc.get == NULL));
  49123. /* if accessor without getter, return value is undefined */
  49124. DUK_ASSERT(((desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) == 0) ||
  49125. duk_is_undefined(thr, -1));
  49126. /* Note: for an accessor without getter, falling through to
  49127. * check for "caller" exotic behavior is unnecessary as
  49128. * "undefined" will never activate the behavior. But it does
  49129. * no harm, so we'll do it anyway.
  49130. */
  49131. }
  49132. goto found; /* [key result] */
  49133. next_in_chain:
  49134. /* XXX: option to pretend property doesn't exist if sanity limit is
  49135. * hit might be useful.
  49136. */
  49137. if (DUK_UNLIKELY(sanity-- == 0)) {
  49138. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  49139. DUK_WO_NORETURN(return 0;);
  49140. }
  49141. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  49142. } while (curr != NULL);
  49143. /*
  49144. * Not found
  49145. */
  49146. duk_to_undefined(thr, -1); /* [key] -> [undefined] (default value) */
  49147. DUK_DDD(DUK_DDDPRINT("-> %!T (not found)", (duk_tval *) duk_get_tval(thr, -1)));
  49148. return 0;
  49149. /*
  49150. * Found; post-processing (Function and arguments objects)
  49151. */
  49152. found:
  49153. /* [key result] */
  49154. #if !defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  49155. /* Special behavior for 'caller' property of (non-bound) function objects
  49156. * and non-strict Arguments objects: if 'caller' -value- (!) is a strict
  49157. * mode function, throw a TypeError (E5 Sections 15.3.5.4, 10.6).
  49158. * Quite interestingly, a non-strict function with no formal arguments
  49159. * will get an arguments object -without- special 'caller' behavior!
  49160. *
  49161. * The E5.1 spec is a bit ambiguous if this special behavior applies when
  49162. * a bound function is the base value (not the 'caller' value): Section
  49163. * 15.3.4.5 (describing bind()) states that [[Get]] for bound functions
  49164. * matches that of Section 15.3.5.4 ([[Get]] for Function instances).
  49165. * However, Section 13.3.5.4 has "NOTE: Function objects created using
  49166. * Function.prototype.bind use the default [[Get]] internal method."
  49167. * The current implementation assumes this means that bound functions
  49168. * should not have the special [[Get]] behavior.
  49169. *
  49170. * The E5.1 spec is also a bit unclear if the TypeError throwing is
  49171. * applied if the 'caller' value is a strict bound function. The
  49172. * current implementation will throw even for both strict non-bound
  49173. * and strict bound functions.
  49174. *
  49175. * See test-dev-strict-func-as-caller-prop-value.js for quite extensive
  49176. * tests.
  49177. *
  49178. * This exotic behavior is disabled when the non-standard 'caller' property
  49179. * is enabled, as it conflicts with the free use of 'caller'.
  49180. */
  49181. if (key == DUK_HTHREAD_STRING_CALLER(thr) &&
  49182. DUK_TVAL_IS_OBJECT(tv_obj)) {
  49183. duk_hobject *orig = DUK_TVAL_GET_OBJECT(tv_obj);
  49184. DUK_ASSERT(orig != NULL);
  49185. if (DUK_HOBJECT_IS_NONBOUND_FUNCTION(orig) ||
  49186. DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(orig)) {
  49187. duk_hobject *h;
  49188. /* XXX: The TypeError is currently not applied to bound
  49189. * functions because the 'strict' flag is not copied by
  49190. * bind(). This may or may not be correct, the specification
  49191. * only refers to the value being a "strict mode Function
  49192. * object" which is ambiguous.
  49193. */
  49194. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(orig));
  49195. h = duk_get_hobject(thr, -1); /* NULL if not an object */
  49196. if (h &&
  49197. DUK_HOBJECT_IS_FUNCTION(h) &&
  49198. DUK_HOBJECT_HAS_STRICT(h)) {
  49199. /* XXX: sufficient to check 'strict', assert for 'is function' */
  49200. DUK_ERROR_TYPE(thr, DUK_STR_STRICT_CALLER_READ);
  49201. DUK_WO_NORETURN(return 0;);
  49202. }
  49203. }
  49204. }
  49205. #endif /* !DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
  49206. duk_remove_m2(thr); /* [key result] -> [result] */
  49207. DUK_DDD(DUK_DDDPRINT("-> %!T (found)", (duk_tval *) duk_get_tval(thr, -1)));
  49208. return 1;
  49209. }
  49210. /*
  49211. * HASPROP: ECMAScript property existence check ("in" operator).
  49212. *
  49213. * Interestingly, the 'in' operator does not do any coercion of
  49214. * the target object.
  49215. */
  49216. DUK_INTERNAL duk_bool_t duk_hobject_hasprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key) {
  49217. duk_tval tv_key_copy;
  49218. duk_hobject *obj;
  49219. duk_hstring *key;
  49220. duk_uint32_t arr_idx;
  49221. duk_bool_t rc;
  49222. duk_propdesc desc;
  49223. DUK_DDD(DUK_DDDPRINT("hasprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  49224. (void *) thr, (void *) tv_obj, (void *) tv_key,
  49225. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  49226. DUK_ASSERT(thr != NULL);
  49227. DUK_ASSERT(thr->heap != NULL);
  49228. DUK_ASSERT(tv_obj != NULL);
  49229. DUK_ASSERT(tv_key != NULL);
  49230. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  49231. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  49232. tv_key = &tv_key_copy;
  49233. /*
  49234. * The 'in' operator requires an object as its right hand side,
  49235. * throwing a TypeError unconditionally if this is not the case.
  49236. *
  49237. * However, lightfuncs need to behave like fully fledged objects
  49238. * here to be maximally transparent, so we need to handle them
  49239. * here. Same goes for plain buffers which behave like ArrayBuffers.
  49240. */
  49241. /* XXX: Refactor key coercion so that it's only called once. It can't
  49242. * be trivially lifted here because the object must be type checked
  49243. * first.
  49244. */
  49245. if (DUK_TVAL_IS_OBJECT(tv_obj)) {
  49246. obj = DUK_TVAL_GET_OBJECT(tv_obj);
  49247. DUK_ASSERT(obj != NULL);
  49248. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49249. } else if (DUK_TVAL_IS_BUFFER(tv_obj)) {
  49250. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49251. if (duk__key_is_plain_buf_ownprop(thr, DUK_TVAL_GET_BUFFER(tv_obj), key, arr_idx)) {
  49252. rc = 1;
  49253. goto pop_and_return;
  49254. }
  49255. obj = thr->builtins[DUK_BIDX_UINT8ARRAY_PROTOTYPE];
  49256. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_obj)) {
  49257. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49258. /* If not found, resume existence check from %NativeFunctionPrototype%.
  49259. * We can just substitute the value in this case; nothing will
  49260. * need the original base value (as would be the case with e.g.
  49261. * setters/getters.
  49262. */
  49263. obj = thr->builtins[DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE];
  49264. } else {
  49265. /* Note: unconditional throw */
  49266. DUK_DDD(DUK_DDDPRINT("base object is not an object -> reject"));
  49267. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_BASE);
  49268. DUK_WO_NORETURN(return 0;);
  49269. }
  49270. /* XXX: fast path for arrays? */
  49271. DUK_ASSERT(key != NULL);
  49272. DUK_ASSERT(obj != NULL);
  49273. DUK_UNREF(arr_idx);
  49274. #if defined(DUK_USE_ES6_PROXY)
  49275. if (DUK_UNLIKELY(DUK_HOBJECT_IS_PROXY(obj))) {
  49276. duk_hobject *h_target;
  49277. duk_bool_t tmp_bool;
  49278. /* XXX: the key in 'key in obj' is string coerced before we're called
  49279. * (which is the required behavior in E5/E5.1/E6) so the key is a string
  49280. * here already.
  49281. */
  49282. if (duk__proxy_check_prop(thr, obj, DUK_STRIDX_HAS, tv_key, &h_target)) {
  49283. /* [ ... key trap handler ] */
  49284. DUK_DDD(DUK_DDDPRINT("-> proxy object 'has' for key %!T", (duk_tval *) tv_key));
  49285. duk_push_hobject(thr, h_target); /* target */
  49286. duk_push_tval(thr, tv_key); /* P */
  49287. duk_call_method(thr, 2 /*nargs*/);
  49288. tmp_bool = duk_to_boolean_top_pop(thr);
  49289. if (!tmp_bool) {
  49290. /* Target object must be checked for a conflicting
  49291. * non-configurable property.
  49292. */
  49293. if (duk__get_own_propdesc_raw(thr, h_target, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  49294. DUK_DDD(DUK_DDDPRINT("proxy 'has': target has matching property %!O, check for "
  49295. "conflicting property; desc.flags=0x%08lx, "
  49296. "desc.get=%p, desc.set=%p",
  49297. (duk_heaphdr *) key, (unsigned long) desc.flags,
  49298. (void *) desc.get, (void *) desc.set));
  49299. /* XXX: Extensibility check for target uses IsExtensible(). If we
  49300. * implemented the isExtensible trap and didn't reject proxies as
  49301. * proxy targets, it should be respected here.
  49302. */
  49303. if (!((desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && /* property is configurable and */
  49304. DUK_HOBJECT_HAS_EXTENSIBLE(h_target))) { /* ... target is extensible */
  49305. DUK_ERROR_TYPE(thr, DUK_STR_PROXY_REJECTED);
  49306. DUK_WO_NORETURN(return 0;);
  49307. }
  49308. }
  49309. }
  49310. duk_pop_unsafe(thr); /* [ key ] -> [] */
  49311. return tmp_bool;
  49312. }
  49313. obj = h_target; /* resume check from proxy target */
  49314. }
  49315. #endif /* DUK_USE_ES6_PROXY */
  49316. /* XXX: inline into a prototype walking loop? */
  49317. rc = duk__get_propdesc(thr, obj, key, &desc, 0 /*flags*/); /* don't push value */
  49318. /* fall through */
  49319. pop_and_return:
  49320. duk_pop_unsafe(thr); /* [ key ] -> [] */
  49321. return rc;
  49322. }
  49323. /*
  49324. * HASPROP variant used internally.
  49325. *
  49326. * This primitive must never throw an error, callers rely on this.
  49327. * In particular, don't throw an error for prototype loops; instead,
  49328. * pretend like the property doesn't exist if a prototype sanity limit
  49329. * is reached.
  49330. *
  49331. * Does not implement proxy behavior: if applied to a proxy object,
  49332. * returns key existence on the proxy object itself.
  49333. */
  49334. DUK_INTERNAL duk_bool_t duk_hobject_hasprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key) {
  49335. duk_propdesc dummy;
  49336. DUK_ASSERT(thr != NULL);
  49337. DUK_ASSERT(thr->heap != NULL);
  49338. DUK_ASSERT(obj != NULL);
  49339. DUK_ASSERT(key != NULL);
  49340. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  49341. return duk__get_propdesc(thr, obj, key, &dummy, DUK_GETDESC_FLAG_IGNORE_PROTOLOOP); /* don't push value */
  49342. }
  49343. /*
  49344. * Helper: handle Array object 'length' write which automatically
  49345. * deletes properties, see E5 Section 15.4.5.1, step 3. This is
  49346. * quite tricky to get right.
  49347. *
  49348. * Used by duk_hobject_putprop().
  49349. */
  49350. /* Coerce a new .length candidate to a number and check that it's a valid
  49351. * .length.
  49352. */
  49353. DUK_LOCAL duk_uint32_t duk__to_new_array_length_checked(duk_hthread *thr, duk_tval *tv) {
  49354. duk_uint32_t res;
  49355. duk_double_t d;
  49356. #if !defined(DUK_USE_PREFER_SIZE)
  49357. #if defined(DUK_USE_FASTINT)
  49358. /* When fastints are enabled, the most interesting case is assigning
  49359. * a fastint to .length (e.g. arr.length = 0).
  49360. */
  49361. if (DUK_TVAL_IS_FASTINT(tv)) {
  49362. /* Very common case. */
  49363. duk_int64_t fi;
  49364. fi = DUK_TVAL_GET_FASTINT(tv);
  49365. if (fi < 0 || fi > DUK_I64_CONSTANT(0xffffffff)) {
  49366. goto fail_range;
  49367. }
  49368. return (duk_uint32_t) fi;
  49369. }
  49370. #else /* DUK_USE_FASTINT */
  49371. /* When fastints are not enabled, the most interesting case is any
  49372. * number.
  49373. */
  49374. if (DUK_TVAL_IS_DOUBLE(tv)) {
  49375. d = DUK_TVAL_GET_NUMBER(tv);
  49376. }
  49377. #endif /* DUK_USE_FASTINT */
  49378. else
  49379. #endif /* !DUK_USE_PREFER_SIZE */
  49380. {
  49381. /* In all other cases, and when doing a size optimized build,
  49382. * fall back to the comprehensive handler.
  49383. */
  49384. d = duk_js_tonumber(thr, tv);
  49385. }
  49386. /* Refuse to update an Array's 'length' to a value outside the
  49387. * 32-bit range. Negative zero is accepted as zero.
  49388. */
  49389. res = duk_double_to_uint32_t(d);
  49390. if ((duk_double_t) res != d) {
  49391. goto fail_range;
  49392. }
  49393. return res;
  49394. fail_range:
  49395. DUK_ERROR_RANGE(thr, DUK_STR_INVALID_ARRAY_LENGTH);
  49396. DUK_WO_NORETURN(return 0;);
  49397. }
  49398. /* Delete elements required by a smaller length, taking into account
  49399. * potentially non-configurable elements. Returns non-zero if all
  49400. * elements could be deleted, and zero if all or some elements could
  49401. * not be deleted. Also writes final "target length" to 'out_result_len'.
  49402. * This is the length value that should go into the 'length' property
  49403. * (must be set by the caller). Never throws an error.
  49404. */
  49405. DUK_LOCAL
  49406. duk_bool_t duk__handle_put_array_length_smaller(duk_hthread *thr,
  49407. duk_hobject *obj,
  49408. duk_uint32_t old_len,
  49409. duk_uint32_t new_len,
  49410. duk_bool_t force_flag,
  49411. duk_uint32_t *out_result_len) {
  49412. duk_uint32_t target_len;
  49413. duk_uint_fast32_t i;
  49414. duk_uint32_t arr_idx;
  49415. duk_hstring *key;
  49416. duk_tval *tv;
  49417. duk_bool_t rc;
  49418. DUK_DDD(DUK_DDDPRINT("new array length smaller than old (%ld -> %ld), "
  49419. "probably need to remove elements",
  49420. (long) old_len, (long) new_len));
  49421. /*
  49422. * New length is smaller than old length, need to delete properties above
  49423. * the new length.
  49424. *
  49425. * If array part exists, this is straightforward: array entries cannot
  49426. * be non-configurable so this is guaranteed to work.
  49427. *
  49428. * If array part does not exist, array-indexed values are scattered
  49429. * in the entry part, and some may not be configurable (preventing length
  49430. * from becoming lower than their index + 1). To handle the algorithm
  49431. * in E5 Section 15.4.5.1, step l correctly, we scan the entire property
  49432. * set twice.
  49433. */
  49434. DUK_ASSERT(thr != NULL);
  49435. DUK_ASSERT(obj != NULL);
  49436. DUK_ASSERT(new_len < old_len);
  49437. DUK_ASSERT(out_result_len != NULL);
  49438. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  49439. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj));
  49440. DUK_ASSERT(DUK_HOBJECT_IS_ARRAY(obj));
  49441. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  49442. /*
  49443. * All defined array-indexed properties are in the array part
  49444. * (we assume the array part is comprehensive), and all array
  49445. * entries are writable, configurable, and enumerable. Thus,
  49446. * nothing can prevent array entries from being deleted.
  49447. */
  49448. DUK_DDD(DUK_DDDPRINT("have array part, easy case"));
  49449. if (old_len < DUK_HOBJECT_GET_ASIZE(obj)) {
  49450. /* XXX: assertion that entries >= old_len are already unused */
  49451. i = old_len;
  49452. } else {
  49453. i = DUK_HOBJECT_GET_ASIZE(obj);
  49454. }
  49455. DUK_ASSERT(i <= DUK_HOBJECT_GET_ASIZE(obj));
  49456. while (i > new_len) {
  49457. i--;
  49458. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  49459. DUK_TVAL_SET_UNUSED_UPDREF(thr, tv); /* side effects */
  49460. }
  49461. *out_result_len = new_len;
  49462. return 1;
  49463. } else {
  49464. /*
  49465. * Entries part is a bit more complex.
  49466. */
  49467. /* Stage 1: find highest preventing non-configurable entry (if any).
  49468. * When forcing, ignore non-configurability.
  49469. */
  49470. DUK_DDD(DUK_DDDPRINT("no array part, slow case"));
  49471. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 1: find target_len "
  49472. "(highest preventing non-configurable entry (if any))"));
  49473. target_len = new_len;
  49474. if (force_flag) {
  49475. DUK_DDD(DUK_DDDPRINT("array length write, no array part; force flag -> skip stage 1"));
  49476. goto skip_stage1;
  49477. }
  49478. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  49479. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  49480. if (!key) {
  49481. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: null key", (long) i));
  49482. continue;
  49483. }
  49484. if (!DUK_HSTRING_HAS_ARRIDX(key)) {
  49485. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key not an array index", (long) i));
  49486. continue;
  49487. }
  49488. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(key)); /* XXX: macro checks for array index flag, which is unnecessary here */
  49489. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  49490. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX);
  49491. DUK_ASSERT(arr_idx < old_len); /* consistency requires this */
  49492. if (arr_idx < new_len) {
  49493. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is array index %ld, below new_len",
  49494. (long) i, (long) arr_idx));
  49495. continue;
  49496. }
  49497. if (DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(thr->heap, obj, i)) {
  49498. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is a relevant array index %ld, but configurable",
  49499. (long) i, (long) arr_idx));
  49500. continue;
  49501. }
  49502. /* relevant array index is non-configurable, blocks write */
  49503. if (arr_idx >= target_len) {
  49504. DUK_DDD(DUK_DDDPRINT("entry at index %ld has arr_idx %ld, is not configurable, "
  49505. "update target_len %ld -> %ld",
  49506. (long) i, (long) arr_idx, (long) target_len,
  49507. (long) (arr_idx + 1)));
  49508. target_len = arr_idx + 1;
  49509. }
  49510. }
  49511. skip_stage1:
  49512. /* stage 2: delete configurable entries above target length */
  49513. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld, target_len=%ld",
  49514. (long) old_len, (long) new_len, (long) target_len));
  49515. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 2: remove "
  49516. "entries >= target_len"));
  49517. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  49518. key = DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i);
  49519. if (!key) {
  49520. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: null key", (long) i));
  49521. continue;
  49522. }
  49523. if (!DUK_HSTRING_HAS_ARRIDX(key)) {
  49524. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key not an array index", (long) i));
  49525. continue;
  49526. }
  49527. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(key)); /* XXX: macro checks for array index flag, which is unnecessary here */
  49528. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  49529. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX);
  49530. DUK_ASSERT(arr_idx < old_len); /* consistency requires this */
  49531. if (arr_idx < target_len) {
  49532. DUK_DDD(DUK_DDDPRINT("skip entry index %ld: key is array index %ld, below target_len",
  49533. (long) i, (long) arr_idx));
  49534. continue;
  49535. }
  49536. DUK_ASSERT(force_flag || DUK_HOBJECT_E_SLOT_IS_CONFIGURABLE(thr->heap, obj, i)); /* stage 1 guarantees */
  49537. DUK_DDD(DUK_DDDPRINT("delete entry index %ld: key is array index %ld",
  49538. (long) i, (long) arr_idx));
  49539. /*
  49540. * Slow delete, but we don't care as we're already in a very slow path.
  49541. * The delete always succeeds: key has no exotic behavior, property
  49542. * is configurable, and no resize occurs.
  49543. */
  49544. rc = duk_hobject_delprop_raw(thr, obj, key, force_flag ? DUK_DELPROP_FLAG_FORCE : 0);
  49545. DUK_UNREF(rc);
  49546. DUK_ASSERT(rc != 0);
  49547. }
  49548. /* stage 3: update length (done by caller), decide return code */
  49549. DUK_DDD(DUK_DDDPRINT("array length write, no array part, stage 3: update length (done by caller)"));
  49550. *out_result_len = target_len;
  49551. if (target_len == new_len) {
  49552. DUK_DDD(DUK_DDDPRINT("target_len matches new_len, return success"));
  49553. return 1;
  49554. }
  49555. DUK_DDD(DUK_DDDPRINT("target_len does not match new_len (some entry prevented "
  49556. "full length adjustment), return error"));
  49557. return 0;
  49558. }
  49559. DUK_UNREACHABLE();
  49560. }
  49561. /* XXX: is valstack top best place for argument? */
  49562. DUK_LOCAL duk_bool_t duk__handle_put_array_length(duk_hthread *thr, duk_hobject *obj) {
  49563. duk_harray *a;
  49564. duk_uint32_t old_len;
  49565. duk_uint32_t new_len;
  49566. duk_uint32_t result_len;
  49567. duk_bool_t rc;
  49568. DUK_DDD(DUK_DDDPRINT("handling a put operation to array 'length' exotic property, "
  49569. "new val: %!T",
  49570. (duk_tval *) duk_get_tval(thr, -1)));
  49571. DUK_ASSERT(thr != NULL);
  49572. DUK_ASSERT(obj != NULL);
  49573. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  49574. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj));
  49575. DUK_ASSERT(DUK_HOBJECT_IS_ARRAY(obj));
  49576. a = (duk_harray *) obj;
  49577. DUK_ASSERT_HARRAY_VALID(a);
  49578. DUK_ASSERT(duk_is_valid_index(thr, -1));
  49579. /*
  49580. * Get old and new length
  49581. */
  49582. old_len = a->length;
  49583. new_len = duk__to_new_array_length_checked(thr, DUK_GET_TVAL_NEGIDX(thr, -1));
  49584. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld", (long) old_len, (long) new_len));
  49585. /*
  49586. * Writability check
  49587. */
  49588. if (DUK_HARRAY_LENGTH_NONWRITABLE(a)) {
  49589. DUK_DDD(DUK_DDDPRINT("length is not writable, fail"));
  49590. return 0;
  49591. }
  49592. /*
  49593. * New length not lower than old length => no changes needed
  49594. * (not even array allocation).
  49595. */
  49596. if (new_len >= old_len) {
  49597. DUK_DDD(DUK_DDDPRINT("new length is same or higher than old length, just update length, no deletions"));
  49598. a->length = new_len;
  49599. return 1;
  49600. }
  49601. DUK_DDD(DUK_DDDPRINT("new length is lower than old length, probably must delete entries"));
  49602. /*
  49603. * New length lower than old length => delete elements, then
  49604. * update length.
  49605. *
  49606. * Note: even though a bunch of elements have been deleted, the 'desc' is
  49607. * still valid as properties haven't been resized (and entries compacted).
  49608. */
  49609. rc = duk__handle_put_array_length_smaller(thr, obj, old_len, new_len, 0 /*force_flag*/, &result_len);
  49610. DUK_ASSERT(result_len >= new_len && result_len <= old_len);
  49611. a->length = result_len;
  49612. /* XXX: shrink array allocation or entries compaction here? */
  49613. return rc;
  49614. }
  49615. /*
  49616. * PUTPROP: ECMAScript property write.
  49617. *
  49618. * Unlike ECMAScript primitive which returns nothing, returns 1 to indicate
  49619. * success and 0 to indicate failure (assuming throw is not set).
  49620. *
  49621. * This is an extremely tricky function. Some examples:
  49622. *
  49623. * * Currently a decref may trigger a GC, which may compact an object's
  49624. * property allocation. Consequently, any entry indices (e_idx) will
  49625. * be potentially invalidated by a decref.
  49626. *
  49627. * * Exotic behaviors (strings, arrays, arguments object) require,
  49628. * among other things:
  49629. *
  49630. * - Preprocessing before and postprocessing after an actual property
  49631. * write. For example, array index write requires pre-checking the
  49632. * array 'length' property for access control, and may require an
  49633. * array 'length' update after the actual write has succeeded (but
  49634. * not if it fails).
  49635. *
  49636. * - Deletion of multiple entries, as a result of array 'length' write.
  49637. *
  49638. * * Input values are taken as pointers which may point to the valstack.
  49639. * If valstack is resized because of the put (this may happen at least
  49640. * when the array part is abandoned), the pointers can be invalidated.
  49641. * (We currently make a copy of all of the input values to avoid issues.)
  49642. */
  49643. DUK_INTERNAL duk_bool_t duk_hobject_putprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_tval *tv_val, duk_bool_t throw_flag) {
  49644. duk_tval tv_obj_copy;
  49645. duk_tval tv_key_copy;
  49646. duk_tval tv_val_copy;
  49647. duk_hobject *orig = NULL; /* NULL if tv_obj is primitive */
  49648. duk_hobject *curr;
  49649. duk_hstring *key = NULL;
  49650. duk_propdesc desc;
  49651. duk_tval *tv;
  49652. duk_uint32_t arr_idx;
  49653. duk_bool_t rc;
  49654. duk_int_t e_idx;
  49655. duk_uint_t sanity;
  49656. duk_uint32_t new_array_length = 0; /* 0 = no update */
  49657. DUK_DDD(DUK_DDDPRINT("putprop: thr=%p, obj=%p, key=%p, val=%p, throw=%ld "
  49658. "(obj -> %!T, key -> %!T, val -> %!T)",
  49659. (void *) thr, (void *) tv_obj, (void *) tv_key, (void *) tv_val,
  49660. (long) throw_flag, (duk_tval *) tv_obj, (duk_tval *) tv_key, (duk_tval *) tv_val));
  49661. DUK_ASSERT(thr != NULL);
  49662. DUK_ASSERT(thr->heap != NULL);
  49663. DUK_ASSERT(tv_obj != NULL);
  49664. DUK_ASSERT(tv_key != NULL);
  49665. DUK_ASSERT(tv_val != NULL);
  49666. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  49667. DUK_STATS_INC(thr->heap, stats_putprop_all);
  49668. /*
  49669. * Make a copy of tv_obj, tv_key, and tv_val to avoid any issues of
  49670. * them being invalidated by a valstack resize.
  49671. *
  49672. * XXX: this is an overkill for some paths, so optimize this later
  49673. * (or maybe switch to a stack arguments model entirely).
  49674. */
  49675. DUK_TVAL_SET_TVAL(&tv_obj_copy, tv_obj);
  49676. DUK_TVAL_SET_TVAL(&tv_key_copy, tv_key);
  49677. DUK_TVAL_SET_TVAL(&tv_val_copy, tv_val);
  49678. tv_obj = &tv_obj_copy;
  49679. tv_key = &tv_key_copy;
  49680. tv_val = &tv_val_copy;
  49681. /*
  49682. * Coercion and fast path processing.
  49683. */
  49684. switch (DUK_TVAL_GET_TAG(tv_obj)) {
  49685. case DUK_TAG_UNDEFINED:
  49686. case DUK_TAG_NULL: {
  49687. /* Note: unconditional throw */
  49688. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject (object=%!iT)",
  49689. (duk_tval *) tv_obj));
  49690. #if defined(DUK_USE_PARANOID_ERRORS)
  49691. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_BASE);
  49692. #else
  49693. DUK_ERROR_FMT2(thr, DUK_ERR_TYPE_ERROR, "cannot write property %s of %s",
  49694. duk_push_string_tval_readable(thr, tv_key), duk_push_string_tval_readable(thr, tv_obj));
  49695. #endif
  49696. DUK_WO_NORETURN(return 0;);
  49697. break;
  49698. }
  49699. case DUK_TAG_BOOLEAN: {
  49700. DUK_DDD(DUK_DDDPRINT("base object is a boolean, start lookup from boolean prototype"));
  49701. curr = thr->builtins[DUK_BIDX_BOOLEAN_PROTOTYPE];
  49702. break;
  49703. }
  49704. case DUK_TAG_STRING: {
  49705. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  49706. /*
  49707. * Note: currently no fast path for array index writes.
  49708. * They won't be possible anyway as strings are immutable.
  49709. */
  49710. DUK_ASSERT(key == NULL);
  49711. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49712. DUK_ASSERT(key != NULL);
  49713. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  49714. /* Symbols (ES2015 or hidden) don't have virtual properties. */
  49715. curr = thr->builtins[DUK_BIDX_SYMBOL_PROTOTYPE];
  49716. goto lookup;
  49717. }
  49718. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  49719. goto fail_not_writable;
  49720. }
  49721. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  49722. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  49723. goto fail_not_writable;
  49724. }
  49725. DUK_DDD(DUK_DDDPRINT("base object is a string, start lookup from string prototype"));
  49726. curr = thr->builtins[DUK_BIDX_STRING_PROTOTYPE];
  49727. goto lookup; /* avoid double coercion */
  49728. }
  49729. case DUK_TAG_OBJECT: {
  49730. orig = DUK_TVAL_GET_OBJECT(tv_obj);
  49731. DUK_ASSERT(orig != NULL);
  49732. #if defined(DUK_USE_ROM_OBJECTS)
  49733. /* With this check in place fast paths won't need read-only
  49734. * object checks. This is technically incorrect if there are
  49735. * setters that cause no writes to ROM objects, but current
  49736. * built-ins don't have such setters.
  49737. */
  49738. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) orig)) {
  49739. DUK_DD(DUK_DDPRINT("attempt to putprop on read-only target object"));
  49740. goto fail_not_writable_no_pop; /* Must avoid duk_pop() in exit path */
  49741. }
  49742. #endif
  49743. /* The fast path for array property put is not fully compliant:
  49744. * If one places conflicting number-indexed properties into
  49745. * Array.prototype (for example, a non-writable Array.prototype[7])
  49746. * the fast path will incorrectly ignore them.
  49747. *
  49748. * This fast path could be made compliant by falling through
  49749. * to the slow path if the previous value was UNUSED. This would
  49750. * also remove the need to check for extensibility. Right now a
  49751. * non-extensible array is slower than an extensible one as far
  49752. * as writes are concerned.
  49753. *
  49754. * The fast path behavior is documented in more detail here:
  49755. * tests/ecmascript/test-misc-array-fast-write.js
  49756. */
  49757. /* XXX: array .length? */
  49758. #if defined(DUK_USE_ARRAY_PROP_FASTPATH)
  49759. if (duk__putprop_shallow_fastpath_array_tval(thr, orig, tv_key, tv_val) != 0) {
  49760. DUK_DDD(DUK_DDDPRINT("array fast path success"));
  49761. DUK_STATS_INC(thr->heap, stats_putprop_arrayidx);
  49762. return 1;
  49763. }
  49764. #endif
  49765. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  49766. if (duk__putprop_fastpath_bufobj_tval(thr, orig, tv_key, tv_val) != 0) {
  49767. DUK_DDD(DUK_DDDPRINT("base is bufobj, key is a number, bufobj fast path"));
  49768. DUK_STATS_INC(thr->heap, stats_putprop_bufobjidx);
  49769. return 1;
  49770. }
  49771. #endif
  49772. #if defined(DUK_USE_ES6_PROXY)
  49773. if (DUK_UNLIKELY(DUK_HOBJECT_IS_PROXY(orig))) {
  49774. duk_hobject *h_target;
  49775. duk_bool_t tmp_bool;
  49776. if (duk__proxy_check_prop(thr, orig, DUK_STRIDX_SET, tv_key, &h_target)) {
  49777. /* -> [ ... trap handler ] */
  49778. DUK_DDD(DUK_DDDPRINT("-> proxy object 'set' for key %!T", (duk_tval *) tv_key));
  49779. DUK_STATS_INC(thr->heap, stats_putprop_proxy);
  49780. duk_push_hobject(thr, h_target); /* target */
  49781. duk_push_tval(thr, tv_key); /* P */
  49782. duk_push_tval(thr, tv_val); /* V */
  49783. duk_push_tval(thr, tv_obj); /* Receiver: Proxy object */
  49784. duk_call_method(thr, 4 /*nargs*/);
  49785. tmp_bool = duk_to_boolean_top_pop(thr);
  49786. if (!tmp_bool) {
  49787. goto fail_proxy_rejected;
  49788. }
  49789. /* Target object must be checked for a conflicting
  49790. * non-configurable property.
  49791. */
  49792. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49793. DUK_ASSERT(key != NULL);
  49794. if (duk__get_own_propdesc_raw(thr, h_target, key, arr_idx, &desc, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  49795. duk_tval *tv_targ = duk_require_tval(thr, -1);
  49796. duk_bool_t datadesc_reject;
  49797. duk_bool_t accdesc_reject;
  49798. DUK_DDD(DUK_DDDPRINT("proxy 'set': target has matching property %!O, check for "
  49799. "conflicting property; tv_val=%!T, tv_targ=%!T, desc.flags=0x%08lx, "
  49800. "desc.get=%p, desc.set=%p",
  49801. (duk_heaphdr *) key, (duk_tval *) tv_val, (duk_tval *) tv_targ,
  49802. (unsigned long) desc.flags,
  49803. (void *) desc.get, (void *) desc.set));
  49804. datadesc_reject = !(desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  49805. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  49806. !(desc.flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  49807. !duk_js_samevalue(tv_val, tv_targ);
  49808. accdesc_reject = (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  49809. !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) &&
  49810. (desc.set == NULL);
  49811. if (datadesc_reject || accdesc_reject) {
  49812. DUK_ERROR_TYPE(thr, DUK_STR_PROXY_REJECTED);
  49813. DUK_WO_NORETURN(return 0;);
  49814. }
  49815. duk_pop_2_unsafe(thr);
  49816. } else {
  49817. duk_pop_unsafe(thr);
  49818. }
  49819. return 1; /* success */
  49820. }
  49821. orig = h_target; /* resume write to target */
  49822. DUK_TVAL_SET_OBJECT(tv_obj, orig);
  49823. }
  49824. #endif /* DUK_USE_ES6_PROXY */
  49825. curr = orig;
  49826. break;
  49827. }
  49828. case DUK_TAG_BUFFER: {
  49829. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  49830. duk_int_t pop_count = 0;
  49831. /*
  49832. * Because buffer values may be looped over and read/written
  49833. * from, an array index fast path is important.
  49834. */
  49835. #if defined(DUK_USE_FASTINT)
  49836. if (DUK_TVAL_IS_FASTINT(tv_key)) {
  49837. arr_idx = duk__tval_fastint_to_arr_idx(tv_key);
  49838. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path fastint; arr_idx %ld", (long) arr_idx));
  49839. pop_count = 0;
  49840. } else
  49841. #endif
  49842. if (DUK_TVAL_IS_NUMBER(tv_key)) {
  49843. arr_idx = duk__tval_number_to_arr_idx(tv_key);
  49844. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a fast-path number; arr_idx %ld", (long) arr_idx));
  49845. pop_count = 0;
  49846. } else {
  49847. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49848. DUK_ASSERT(key != NULL);
  49849. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  49850. "coercion key is %!T, arr_idx %ld",
  49851. (duk_tval *) duk_get_tval(thr, -1), (long) arr_idx));
  49852. pop_count = 1;
  49853. }
  49854. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  49855. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  49856. duk_uint8_t *data;
  49857. DUK_DDD(DUK_DDDPRINT("writing to buffer data at index %ld", (long) arr_idx));
  49858. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h);
  49859. /* XXX: duk_to_int() ensures we'll get 8 lowest bits as
  49860. * as input is within duk_int_t range (capped outside it).
  49861. */
  49862. #if defined(DUK_USE_FASTINT)
  49863. /* Buffer writes are often integers. */
  49864. if (DUK_TVAL_IS_FASTINT(tv_val)) {
  49865. data[arr_idx] = (duk_uint8_t) DUK_TVAL_GET_FASTINT_U32(tv_val);
  49866. }
  49867. else
  49868. #endif
  49869. {
  49870. duk_push_tval(thr, tv_val);
  49871. data[arr_idx] = (duk_uint8_t) duk_to_uint32(thr, -1);
  49872. pop_count++;
  49873. }
  49874. duk_pop_n_unsafe(thr, pop_count);
  49875. DUK_DDD(DUK_DDDPRINT("result: success (buffer data write)"));
  49876. DUK_STATS_INC(thr->heap, stats_putprop_bufferidx);
  49877. return 1;
  49878. }
  49879. if (pop_count == 0) {
  49880. /* This is a pretty awkward control flow, but we need to recheck the
  49881. * key coercion here.
  49882. */
  49883. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49884. DUK_ASSERT(key != NULL);
  49885. DUK_DDD(DUK_DDDPRINT("base object buffer, key is a non-fast-path number; after "
  49886. "coercion key is %!T, arr_idx %ld",
  49887. (duk_tval *) duk_get_tval(thr, -1), (long) arr_idx));
  49888. }
  49889. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  49890. goto fail_not_writable;
  49891. }
  49892. DUK_DDD(DUK_DDDPRINT("base object is a buffer, start lookup from Uint8Array prototype"));
  49893. curr = thr->builtins[DUK_BIDX_UINT8ARRAY_PROTOTYPE];
  49894. goto lookup; /* avoid double coercion */
  49895. }
  49896. case DUK_TAG_POINTER: {
  49897. DUK_DDD(DUK_DDDPRINT("base object is a pointer, start lookup from pointer prototype"));
  49898. curr = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  49899. break;
  49900. }
  49901. case DUK_TAG_LIGHTFUNC: {
  49902. /* Lightfuncs have no own properties and are considered non-extensible.
  49903. * However, the write may be captured by an inherited setter which
  49904. * means we can't stop the lookup here.
  49905. */
  49906. DUK_DDD(DUK_DDDPRINT("base object is a lightfunc, start lookup from function prototype"));
  49907. curr = thr->builtins[DUK_BIDX_NATIVE_FUNCTION_PROTOTYPE];
  49908. break;
  49909. }
  49910. #if defined(DUK_USE_FASTINT)
  49911. case DUK_TAG_FASTINT:
  49912. #endif
  49913. default: {
  49914. /* number */
  49915. DUK_DDD(DUK_DDDPRINT("base object is a number, start lookup from number prototype"));
  49916. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_obj));
  49917. curr = thr->builtins[DUK_BIDX_NUMBER_PROTOTYPE];
  49918. break;
  49919. }
  49920. }
  49921. DUK_ASSERT(key == NULL);
  49922. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  49923. DUK_ASSERT(key != NULL);
  49924. lookup:
  49925. /*
  49926. * Check whether the property already exists in the prototype chain.
  49927. * Note that the actual write goes into the original base object
  49928. * (except if an accessor property captures the write).
  49929. */
  49930. /* [key] */
  49931. DUK_ASSERT(curr != NULL);
  49932. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  49933. do {
  49934. if (!duk__get_own_propdesc_raw(thr, curr, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  49935. goto next_in_chain;
  49936. }
  49937. if (desc.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  49938. /*
  49939. * Found existing accessor property (own or inherited).
  49940. * Call setter with 'this' set to orig, and value as the only argument.
  49941. * Setter calls are OK even for ROM objects.
  49942. *
  49943. * Note: no exotic arguments object behavior, because [[Put]] never
  49944. * calls [[DefineOwnProperty]] (E5 Section 8.12.5, step 5.b).
  49945. */
  49946. duk_hobject *setter;
  49947. DUK_DD(DUK_DDPRINT("put to an own or inherited accessor, calling setter"));
  49948. setter = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, curr, desc.e_idx);
  49949. if (!setter) {
  49950. goto fail_no_setter;
  49951. }
  49952. duk_push_hobject(thr, setter);
  49953. duk_push_tval(thr, tv_obj); /* note: original, uncoerced base */
  49954. duk_push_tval(thr, tv_val); /* [key setter this val] */
  49955. #if defined(DUK_USE_NONSTD_SETTER_KEY_ARGUMENT)
  49956. duk_dup_m4(thr);
  49957. duk_call_method(thr, 2); /* [key setter this val key] -> [key retval] */
  49958. #else
  49959. duk_call_method(thr, 1); /* [key setter this val] -> [key retval] */
  49960. #endif
  49961. duk_pop_unsafe(thr); /* ignore retval -> [key] */
  49962. goto success_no_arguments_exotic;
  49963. }
  49964. if (orig == NULL) {
  49965. /*
  49966. * Found existing own or inherited plain property, but original
  49967. * base is a primitive value.
  49968. */
  49969. DUK_DD(DUK_DDPRINT("attempt to create a new property in a primitive base object"));
  49970. goto fail_base_primitive;
  49971. }
  49972. if (curr != orig) {
  49973. /*
  49974. * Found existing inherited plain property.
  49975. * Do an access control check, and if OK, write
  49976. * new property to 'orig'.
  49977. */
  49978. if (!DUK_HOBJECT_HAS_EXTENSIBLE(orig)) {
  49979. DUK_DD(DUK_DDPRINT("found existing inherited plain property, but original object is not extensible"));
  49980. goto fail_not_extensible;
  49981. }
  49982. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  49983. DUK_DD(DUK_DDPRINT("found existing inherited plain property, original object is extensible, but inherited property is not writable"));
  49984. goto fail_not_writable;
  49985. }
  49986. DUK_DD(DUK_DDPRINT("put to new property, object extensible, inherited property found and is writable"));
  49987. goto create_new;
  49988. } else {
  49989. /*
  49990. * Found existing own (non-inherited) plain property.
  49991. * Do an access control check and update in place.
  49992. */
  49993. if (!(desc.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  49994. DUK_DD(DUK_DDPRINT("found existing own (non-inherited) plain property, but property is not writable"));
  49995. goto fail_not_writable;
  49996. }
  49997. if (desc.flags & DUK_PROPDESC_FLAG_VIRTUAL) {
  49998. DUK_DD(DUK_DDPRINT("found existing own (non-inherited) virtual property, property is writable"));
  49999. if (DUK_HOBJECT_IS_ARRAY(curr)) {
  50000. /*
  50001. * Write to 'length' of an array is a very complex case
  50002. * handled in a helper which updates both the array elements
  50003. * and writes the new 'length'. The write may result in an
  50004. * unconditional RangeError or a partial write (indicated
  50005. * by a return code).
  50006. *
  50007. * Note: the helper has an unnecessary writability check
  50008. * for 'length', we already know it is writable.
  50009. */
  50010. DUK_ASSERT(key == DUK_HTHREAD_STRING_LENGTH(thr)); /* only virtual array property */
  50011. DUK_DDD(DUK_DDDPRINT("writing existing 'length' property to array exotic, invoke complex helper"));
  50012. /* XXX: the helper currently assumes stack top contains new
  50013. * 'length' value and the whole calling convention is not very
  50014. * compatible with what we need.
  50015. */
  50016. duk_push_tval(thr, tv_val); /* [key val] */
  50017. rc = duk__handle_put_array_length(thr, orig);
  50018. duk_pop_unsafe(thr); /* [key val] -> [key] */
  50019. if (!rc) {
  50020. goto fail_array_length_partial;
  50021. }
  50022. /* key is 'length', cannot match argument exotic behavior */
  50023. goto success_no_arguments_exotic;
  50024. }
  50025. #if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
  50026. else if (DUK_HOBJECT_IS_BUFOBJ(curr)) {
  50027. duk_hbufobj *h_bufobj;
  50028. duk_uint_t byte_off;
  50029. duk_small_uint_t elem_size;
  50030. h_bufobj = (duk_hbufobj *) curr;
  50031. DUK_ASSERT_HBUFOBJ_VALID(h_bufobj);
  50032. DUK_DD(DUK_DDPRINT("writable virtual property is in buffer object"));
  50033. /* Careful with wrapping: arr_idx upshift may easily wrap, whereas
  50034. * length downshift won't.
  50035. */
  50036. if (arr_idx < (h_bufobj->length >> h_bufobj->shift) && DUK_HBUFOBJ_HAS_VIRTUAL_INDICES(h_bufobj)) {
  50037. duk_uint8_t *data;
  50038. DUK_DDD(DUK_DDDPRINT("writing to buffer data at index %ld", (long) arr_idx));
  50039. DUK_ASSERT(arr_idx != DUK__NO_ARRAY_INDEX); /* index/length check guarantees */
  50040. byte_off = arr_idx << h_bufobj->shift; /* no wrap assuming h_bufobj->length is valid */
  50041. elem_size = (duk_small_uint_t) (1U << h_bufobj->shift);
  50042. /* Coerce to number before validating pointers etc so that the
  50043. * number coercions in duk_hbufobj_validated_write() are
  50044. * guaranteed to be side effect free and not invalidate the
  50045. * pointer checks we do here.
  50046. */
  50047. duk_push_tval(thr, tv_val);
  50048. (void) duk_to_number_m1(thr);
  50049. if (h_bufobj->buf != NULL && DUK_HBUFOBJ_VALID_BYTEOFFSET_EXCL(h_bufobj, byte_off + elem_size)) {
  50050. data = (duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(thr->heap, h_bufobj->buf) + h_bufobj->offset + byte_off;
  50051. duk_hbufobj_validated_write(thr, h_bufobj, data, elem_size);
  50052. } else {
  50053. DUK_D(DUK_DPRINT("bufobj access out of underlying buffer, ignoring (write skipped)"));
  50054. }
  50055. duk_pop_unsafe(thr);
  50056. goto success_no_arguments_exotic;
  50057. }
  50058. }
  50059. #endif /* DUK_USE_BUFFEROBJECT_SUPPORT */
  50060. DUK_D(DUK_DPRINT("should not happen, key %!O", key));
  50061. goto fail_internal; /* should not happen */
  50062. }
  50063. DUK_DD(DUK_DDPRINT("put to existing own plain property, property is writable"));
  50064. goto update_old;
  50065. }
  50066. DUK_UNREACHABLE();
  50067. next_in_chain:
  50068. /* XXX: option to pretend property doesn't exist if sanity limit is
  50069. * hit might be useful.
  50070. */
  50071. if (DUK_UNLIKELY(sanity-- == 0)) {
  50072. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  50073. DUK_WO_NORETURN(return 0;);
  50074. }
  50075. curr = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, curr);
  50076. } while (curr != NULL);
  50077. /*
  50078. * Property not found in prototype chain.
  50079. */
  50080. DUK_DDD(DUK_DDDPRINT("property not found in prototype chain"));
  50081. if (orig == NULL) {
  50082. DUK_DD(DUK_DDPRINT("attempt to create a new property in a primitive base object"));
  50083. goto fail_base_primitive;
  50084. }
  50085. if (!DUK_HOBJECT_HAS_EXTENSIBLE(orig)) {
  50086. DUK_DD(DUK_DDPRINT("put to a new property (not found in prototype chain), but original object not extensible"));
  50087. goto fail_not_extensible;
  50088. }
  50089. goto create_new;
  50090. update_old:
  50091. /*
  50092. * Update an existing property of the base object.
  50093. */
  50094. /* [key] */
  50095. DUK_DDD(DUK_DDDPRINT("update an existing property of the original object"));
  50096. DUK_ASSERT(orig != NULL);
  50097. #if defined(DUK_USE_ROM_OBJECTS)
  50098. /* This should not happen because DUK_TAG_OBJECT case checks
  50099. * for this already, but check just in case.
  50100. */
  50101. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) orig)) {
  50102. goto fail_not_writable;
  50103. }
  50104. #endif
  50105. /* Although there are writable virtual properties (e.g. plain buffer
  50106. * and buffer object number indices), they are handled before we come
  50107. * here.
  50108. */
  50109. DUK_ASSERT((desc.flags & DUK_PROPDESC_FLAG_VIRTUAL) == 0);
  50110. DUK_ASSERT(desc.a_idx >= 0 || desc.e_idx >= 0);
  50111. /* Array own property .length is handled above. */
  50112. DUK_ASSERT(!(DUK_HOBJECT_IS_ARRAY(orig) && key == DUK_HTHREAD_STRING_LENGTH(thr)));
  50113. if (desc.e_idx >= 0) {
  50114. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, desc.e_idx);
  50115. DUK_DDD(DUK_DDDPRINT("previous entry value: %!iT", (duk_tval *) tv));
  50116. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv_val); /* side effects; e_idx may be invalidated */
  50117. /* don't touch property attributes or hash part */
  50118. DUK_DD(DUK_DDPRINT("put to an existing entry at index %ld -> new value %!iT",
  50119. (long) desc.e_idx, (duk_tval *) tv));
  50120. } else {
  50121. /* Note: array entries are always writable, so the writability check
  50122. * above is pointless for them. The check could be avoided with some
  50123. * refactoring but is probably not worth it.
  50124. */
  50125. DUK_ASSERT(desc.a_idx >= 0);
  50126. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, orig, desc.a_idx);
  50127. DUK_DDD(DUK_DDDPRINT("previous array value: %!iT", (duk_tval *) tv));
  50128. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv_val); /* side effects; a_idx may be invalidated */
  50129. DUK_DD(DUK_DDPRINT("put to an existing array entry at index %ld -> new value %!iT",
  50130. (long) desc.a_idx, (duk_tval *) tv));
  50131. }
  50132. /* Regardless of whether property is found in entry or array part,
  50133. * it may have arguments exotic behavior (array indices may reside
  50134. * in entry part for abandoned / non-existent array parts).
  50135. */
  50136. goto success_with_arguments_exotic;
  50137. create_new:
  50138. /*
  50139. * Create a new property in the original object.
  50140. *
  50141. * Exotic properties need to be reconsidered here from a write
  50142. * perspective (not just property attributes perspective).
  50143. * However, the property does not exist in the object already,
  50144. * so this limits the kind of exotic properties that apply.
  50145. */
  50146. /* [key] */
  50147. DUK_DDD(DUK_DDDPRINT("create new property to original object"));
  50148. DUK_ASSERT(orig != NULL);
  50149. /* Array own property .length is handled above. */
  50150. DUK_ASSERT(!(DUK_HOBJECT_IS_ARRAY(orig) && key == DUK_HTHREAD_STRING_LENGTH(thr)));
  50151. #if defined(DUK_USE_ROM_OBJECTS)
  50152. /* This should not happen because DUK_TAG_OBJECT case checks
  50153. * for this already, but check just in case.
  50154. */
  50155. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) orig)) {
  50156. goto fail_not_writable;
  50157. }
  50158. #endif
  50159. /* Not possible because array object 'length' is present
  50160. * from its creation and cannot be deleted, and is thus
  50161. * caught as an existing property above.
  50162. */
  50163. DUK_ASSERT(!(DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  50164. key == DUK_HTHREAD_STRING_LENGTH(thr)));
  50165. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig) &&
  50166. arr_idx != DUK__NO_ARRAY_INDEX) {
  50167. /* automatic length update */
  50168. duk_uint32_t old_len;
  50169. duk_harray *a;
  50170. a = (duk_harray *) orig;
  50171. DUK_ASSERT_HARRAY_VALID(a);
  50172. old_len = a->length;
  50173. if (arr_idx >= old_len) {
  50174. DUK_DDD(DUK_DDDPRINT("write new array entry requires length update "
  50175. "(arr_idx=%ld, old_len=%ld)",
  50176. (long) arr_idx, (long) old_len));
  50177. if (DUK_HARRAY_LENGTH_NONWRITABLE(a)) {
  50178. DUK_DD(DUK_DDPRINT("attempt to extend array, but array 'length' is not writable"));
  50179. goto fail_not_writable;
  50180. }
  50181. /* Note: actual update happens once write has been completed
  50182. * without error below. The write should always succeed
  50183. * from a specification viewpoint, but we may e.g. run out
  50184. * of memory. It's safer in this order.
  50185. */
  50186. DUK_ASSERT(arr_idx != 0xffffffffUL);
  50187. new_array_length = arr_idx + 1; /* flag for later write */
  50188. } else {
  50189. DUK_DDD(DUK_DDDPRINT("write new array entry does not require length update "
  50190. "(arr_idx=%ld, old_len=%ld)",
  50191. (long) arr_idx, (long) old_len));
  50192. }
  50193. }
  50194. /* write_to_array_part: */
  50195. /*
  50196. * Write to array part?
  50197. *
  50198. * Note: array abandonding requires a property resize which uses
  50199. * 'rechecks' valstack for temporaries and may cause any existing
  50200. * valstack pointers to be invalidated. To protect against this,
  50201. * tv_obj, tv_key, and tv_val are copies of the original inputs.
  50202. */
  50203. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  50204. DUK_HOBJECT_HAS_ARRAY_PART(orig)) {
  50205. if (arr_idx < DUK_HOBJECT_GET_ASIZE(orig)) {
  50206. goto no_array_growth;
  50207. }
  50208. /*
  50209. * Array needs to grow, but we don't want it becoming too sparse.
  50210. * If it were to become sparse, abandon array part, moving all
  50211. * array entries into the entries part (for good).
  50212. *
  50213. * Since we don't keep track of actual density (used vs. size) of
  50214. * the array part, we need to estimate somehow. The check is made
  50215. * in two parts:
  50216. *
  50217. * - Check whether the resize need is small compared to the
  50218. * current size (relatively); if so, resize without further
  50219. * checking (essentially we assume that the original part is
  50220. * "dense" so that the result would be dense enough).
  50221. *
  50222. * - Otherwise, compute the resize using an actual density
  50223. * measurement based on counting the used array entries.
  50224. */
  50225. DUK_DDD(DUK_DDDPRINT("write to new array requires array resize, decide whether to do a "
  50226. "fast resize without abandon check (arr_idx=%ld, old_size=%ld)",
  50227. (long) arr_idx, (long) DUK_HOBJECT_GET_ASIZE(orig)));
  50228. if (duk__abandon_array_slow_check_required(arr_idx, DUK_HOBJECT_GET_ASIZE(orig))) {
  50229. duk_uint32_t old_used;
  50230. duk_uint32_t old_size;
  50231. DUK_DDD(DUK_DDDPRINT("=> fast check is NOT OK, do slow check for array abandon"));
  50232. duk__compute_a_stats(thr, orig, &old_used, &old_size);
  50233. DUK_DDD(DUK_DDDPRINT("abandon check, array stats: old_used=%ld, old_size=%ld, arr_idx=%ld",
  50234. (long) old_used, (long) old_size, (long) arr_idx));
  50235. /* Note: intentionally use approximations to shave a few instructions:
  50236. * a_used = old_used (accurate: old_used + 1)
  50237. * a_size = arr_idx (accurate: arr_idx + 1)
  50238. */
  50239. if (duk__abandon_array_density_check(old_used, arr_idx)) {
  50240. DUK_DD(DUK_DDPRINT("write to new array entry beyond current length, "
  50241. "decided to abandon array part (would become too sparse)"));
  50242. /* abandoning requires a props allocation resize and
  50243. * 'rechecks' the valstack, invalidating any existing
  50244. * valstack value pointers!
  50245. */
  50246. duk__abandon_array_checked(thr, orig);
  50247. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(orig));
  50248. goto write_to_entry_part;
  50249. }
  50250. DUK_DDD(DUK_DDDPRINT("=> decided to keep array part"));
  50251. } else {
  50252. DUK_DDD(DUK_DDDPRINT("=> fast resize is OK"));
  50253. }
  50254. DUK_DD(DUK_DDPRINT("write to new array entry beyond current length, "
  50255. "decided to extend current allocation"));
  50256. duk__grow_props_for_array_item(thr, orig, arr_idx);
  50257. no_array_growth:
  50258. /* Note: assume array part is comprehensive, so that either
  50259. * the write goes to the array part, or we've abandoned the
  50260. * array above (and will not come here).
  50261. */
  50262. DUK_ASSERT(DUK_HOBJECT_HAS_ARRAY_PART(orig));
  50263. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(orig));
  50264. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, orig, arr_idx);
  50265. /* prev value must be unused, no decref */
  50266. DUK_ASSERT(DUK_TVAL_IS_UNUSED(tv));
  50267. DUK_TVAL_SET_TVAL(tv, tv_val);
  50268. DUK_TVAL_INCREF(thr, tv);
  50269. DUK_DD(DUK_DDPRINT("put to new array entry: %ld -> %!T",
  50270. (long) arr_idx, (duk_tval *) tv));
  50271. /* Note: array part values are [[Writable]], [[Enumerable]],
  50272. * and [[Configurable]] which matches the required attributes
  50273. * here.
  50274. */
  50275. goto entry_updated;
  50276. }
  50277. write_to_entry_part:
  50278. /*
  50279. * Write to entry part
  50280. */
  50281. /* entry allocation updates hash part and increases the key
  50282. * refcount; may need a props allocation resize but doesn't
  50283. * 'recheck' the valstack.
  50284. */
  50285. e_idx = duk__hobject_alloc_entry_checked(thr, orig, key);
  50286. DUK_ASSERT(e_idx >= 0);
  50287. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, orig, e_idx);
  50288. /* prev value can be garbage, no decref */
  50289. DUK_TVAL_SET_TVAL(tv, tv_val);
  50290. DUK_TVAL_INCREF(thr, tv);
  50291. DUK_HOBJECT_E_SET_FLAGS(thr->heap, orig, e_idx, DUK_PROPDESC_FLAGS_WEC);
  50292. goto entry_updated;
  50293. entry_updated:
  50294. /*
  50295. * Possible pending array length update, which must only be done
  50296. * if the actual entry write succeeded.
  50297. */
  50298. if (new_array_length > 0) {
  50299. /* Note: zero works as a "no update" marker because the new length
  50300. * can never be zero after a new property is written.
  50301. */
  50302. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(orig));
  50303. DUK_DDD(DUK_DDDPRINT("write successful, pending array length update to: %ld",
  50304. (long) new_array_length));
  50305. ((duk_harray *) orig)->length = new_array_length;
  50306. }
  50307. /*
  50308. * Arguments exotic behavior not possible for new properties: all
  50309. * magically bound properties are initially present in the arguments
  50310. * object, and if they are deleted, the binding is also removed from
  50311. * parameter map.
  50312. */
  50313. goto success_no_arguments_exotic;
  50314. success_with_arguments_exotic:
  50315. /*
  50316. * Arguments objects have exotic [[DefineOwnProperty]] which updates
  50317. * the internal 'map' of arguments for writes to currently mapped
  50318. * arguments. More conretely, writes to mapped arguments generate
  50319. * a write to a bound variable.
  50320. *
  50321. * The [[Put]] algorithm invokes [[DefineOwnProperty]] for existing
  50322. * data properties and new properties, but not for existing accessors.
  50323. * Hence, in E5 Section 10.6 ([[DefinedOwnProperty]] algorithm), we
  50324. * have a Desc with 'Value' (and possibly other properties too), and
  50325. * we end up in step 5.b.i.
  50326. */
  50327. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  50328. DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(orig)) {
  50329. /* Note: only numbered indices are relevant, so arr_idx fast reject
  50330. * is good (this is valid unless there are more than 4**32-1 arguments).
  50331. */
  50332. DUK_DDD(DUK_DDDPRINT("putprop successful, arguments exotic behavior needed"));
  50333. /* Note: we can reuse 'desc' here */
  50334. /* XXX: top of stack must contain value, which helper doesn't touch,
  50335. * rework to use tv_val directly?
  50336. */
  50337. duk_push_tval(thr, tv_val);
  50338. (void) duk__check_arguments_map_for_put(thr, orig, key, &desc, throw_flag);
  50339. duk_pop_unsafe(thr);
  50340. }
  50341. /* fall thru */
  50342. success_no_arguments_exotic:
  50343. /* shared exit path now */
  50344. DUK_DDD(DUK_DDDPRINT("result: success"));
  50345. duk_pop_unsafe(thr); /* remove key */
  50346. return 1;
  50347. #if defined(DUK_USE_ES6_PROXY)
  50348. fail_proxy_rejected:
  50349. DUK_DDD(DUK_DDDPRINT("result: error, proxy rejects"));
  50350. if (throw_flag) {
  50351. DUK_ERROR_TYPE(thr, DUK_STR_PROXY_REJECTED);
  50352. DUK_WO_NORETURN(return 0;);
  50353. }
  50354. /* Note: no key on stack */
  50355. return 0;
  50356. #endif
  50357. fail_base_primitive:
  50358. DUK_DDD(DUK_DDDPRINT("result: error, base primitive"));
  50359. if (throw_flag) {
  50360. #if defined(DUK_USE_PARANOID_ERRORS)
  50361. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_BASE);
  50362. #else
  50363. DUK_ERROR_FMT2(thr, DUK_ERR_TYPE_ERROR, "cannot write property %s of %s",
  50364. duk_push_string_tval_readable(thr, tv_key), duk_push_string_tval_readable(thr, tv_obj));
  50365. #endif
  50366. DUK_WO_NORETURN(return 0;);
  50367. }
  50368. duk_pop_unsafe(thr); /* remove key */
  50369. return 0;
  50370. fail_not_extensible:
  50371. DUK_DDD(DUK_DDDPRINT("result: error, not extensible"));
  50372. if (throw_flag) {
  50373. DUK_ERROR_TYPE(thr, DUK_STR_NOT_EXTENSIBLE);
  50374. DUK_WO_NORETURN(return 0;);
  50375. }
  50376. duk_pop_unsafe(thr); /* remove key */
  50377. return 0;
  50378. fail_not_writable:
  50379. DUK_DDD(DUK_DDDPRINT("result: error, not writable"));
  50380. if (throw_flag) {
  50381. DUK_ERROR_TYPE(thr, DUK_STR_NOT_WRITABLE);
  50382. DUK_WO_NORETURN(return 0;);
  50383. }
  50384. duk_pop_unsafe(thr); /* remove key */
  50385. return 0;
  50386. #if defined(DUK_USE_ROM_OBJECTS)
  50387. fail_not_writable_no_pop:
  50388. DUK_DDD(DUK_DDDPRINT("result: error, not writable"));
  50389. if (throw_flag) {
  50390. DUK_ERROR_TYPE(thr, DUK_STR_NOT_WRITABLE);
  50391. DUK_WO_NORETURN(return 0;);
  50392. }
  50393. return 0;
  50394. #endif
  50395. fail_array_length_partial:
  50396. DUK_DD(DUK_DDPRINT("result: error, array length write only partially successful"));
  50397. if (throw_flag) {
  50398. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE);
  50399. DUK_WO_NORETURN(return 0;);
  50400. }
  50401. duk_pop_unsafe(thr); /* remove key */
  50402. return 0;
  50403. fail_no_setter:
  50404. DUK_DDD(DUK_DDDPRINT("result: error, accessor property without setter"));
  50405. if (throw_flag) {
  50406. DUK_ERROR_TYPE(thr, DUK_STR_SETTER_UNDEFINED);
  50407. DUK_WO_NORETURN(return 0;);
  50408. }
  50409. duk_pop_unsafe(thr); /* remove key */
  50410. return 0;
  50411. fail_internal:
  50412. DUK_DDD(DUK_DDDPRINT("result: error, internal"));
  50413. if (throw_flag) {
  50414. DUK_ERROR_INTERNAL(thr);
  50415. DUK_WO_NORETURN(return 0;);
  50416. }
  50417. duk_pop_unsafe(thr); /* remove key */
  50418. return 0;
  50419. }
  50420. /*
  50421. * ECMAScript compliant [[Delete]](P, Throw).
  50422. */
  50423. DUK_INTERNAL duk_bool_t duk_hobject_delprop_raw(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags) {
  50424. duk_propdesc desc;
  50425. duk_tval *tv;
  50426. duk_uint32_t arr_idx;
  50427. duk_bool_t throw_flag;
  50428. duk_bool_t force_flag;
  50429. throw_flag = (flags & DUK_DELPROP_FLAG_THROW);
  50430. force_flag = (flags & DUK_DELPROP_FLAG_FORCE);
  50431. DUK_DDD(DUK_DDDPRINT("delprop_raw: thr=%p, obj=%p, key=%p, throw=%ld, force=%ld (obj -> %!O, key -> %!O)",
  50432. (void *) thr, (void *) obj, (void *) key, (long) throw_flag, (long) force_flag,
  50433. (duk_heaphdr *) obj, (duk_heaphdr *) key));
  50434. DUK_ASSERT(thr != NULL);
  50435. DUK_ASSERT(thr->heap != NULL);
  50436. DUK_ASSERT(obj != NULL);
  50437. DUK_ASSERT(key != NULL);
  50438. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  50439. arr_idx = DUK_HSTRING_GET_ARRIDX_FAST(key);
  50440. /* 0 = don't push current value */
  50441. if (!duk__get_own_propdesc_raw(thr, obj, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  50442. DUK_DDD(DUK_DDDPRINT("property not found, succeed always"));
  50443. goto success;
  50444. }
  50445. #if defined(DUK_USE_ROM_OBJECTS)
  50446. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) {
  50447. DUK_DD(DUK_DDPRINT("attempt to delprop on read-only target object"));
  50448. goto fail_not_configurable;
  50449. }
  50450. #endif
  50451. if ((desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) == 0 && !force_flag) {
  50452. goto fail_not_configurable;
  50453. }
  50454. if (desc.a_idx < 0 && desc.e_idx < 0) {
  50455. /* Currently there are no deletable virtual properties, but
  50456. * with force_flag we might attempt to delete one.
  50457. */
  50458. DUK_DD(DUK_DDPRINT("delete failed: property found, force flag, but virtual (and implicitly non-configurable)"));
  50459. goto fail_virtual;
  50460. }
  50461. if (desc.a_idx >= 0) {
  50462. DUK_ASSERT(desc.e_idx < 0);
  50463. tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, desc.a_idx);
  50464. DUK_TVAL_SET_UNUSED_UPDREF(thr, tv); /* side effects */
  50465. goto success;
  50466. } else {
  50467. DUK_ASSERT(desc.a_idx < 0);
  50468. /* remove hash entry (no decref) */
  50469. #if defined(DUK_USE_HOBJECT_HASH_PART)
  50470. if (desc.h_idx >= 0) {
  50471. duk_uint32_t *h_base = DUK_HOBJECT_H_GET_BASE(thr->heap, obj);
  50472. DUK_DDD(DUK_DDDPRINT("removing hash entry at h_idx %ld", (long) desc.h_idx));
  50473. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) > 0);
  50474. DUK_ASSERT((duk_uint32_t) desc.h_idx < DUK_HOBJECT_GET_HSIZE(obj));
  50475. h_base[desc.h_idx] = DUK__HASH_DELETED;
  50476. } else {
  50477. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) == 0);
  50478. }
  50479. #else
  50480. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(obj) == 0);
  50481. #endif
  50482. /* Remove value. This requires multiple writes so avoid side
  50483. * effects via no-refzero macros so that e_idx is not
  50484. * invalidated.
  50485. */
  50486. DUK_DDD(DUK_DDDPRINT("before removing value, e_idx %ld, key %p, key at slot %p",
  50487. (long) desc.e_idx, (void *) key, (void *) DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx)));
  50488. DUK_DDD(DUK_DDDPRINT("removing value at e_idx %ld", (long) desc.e_idx));
  50489. if (DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx)) {
  50490. duk_hobject *tmp;
  50491. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, desc.e_idx);
  50492. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, desc.e_idx, NULL);
  50493. DUK_UNREF(tmp);
  50494. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, tmp);
  50495. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, desc.e_idx);
  50496. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, desc.e_idx, NULL);
  50497. DUK_UNREF(tmp);
  50498. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, tmp);
  50499. } else {
  50500. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  50501. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv);
  50502. }
  50503. #if 0
  50504. /* Not strictly necessary because if key == NULL, flag MUST be ignored. */
  50505. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, desc.e_idx, 0);
  50506. #endif
  50507. /* Remove key. */
  50508. DUK_DDD(DUK_DDDPRINT("before removing key, e_idx %ld, key %p, key at slot %p",
  50509. (long) desc.e_idx, (void *) key, (void *) DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx)));
  50510. DUK_DDD(DUK_DDDPRINT("removing key at e_idx %ld", (long) desc.e_idx));
  50511. DUK_ASSERT(key == DUK_HOBJECT_E_GET_KEY(thr->heap, obj, desc.e_idx));
  50512. DUK_HOBJECT_E_SET_KEY(thr->heap, obj, desc.e_idx, NULL);
  50513. DUK_HSTRING_DECREF_NORZ(thr, key);
  50514. /* Trigger refzero side effects only when we're done as a
  50515. * finalizer might operate on the object and affect the
  50516. * e_idx we're supposed to use.
  50517. */
  50518. DUK_REFZERO_CHECK_SLOW(thr);
  50519. goto success;
  50520. }
  50521. DUK_UNREACHABLE();
  50522. success:
  50523. /*
  50524. * Argument exotic [[Delete]] behavior (E5 Section 10.6) is
  50525. * a post-check, keeping arguments internal 'map' in sync with
  50526. * any successful deletes (note that property does not need to
  50527. * exist for delete to 'succeed').
  50528. *
  50529. * Delete key from 'map'. Since 'map' only contains array index
  50530. * keys, we can use arr_idx for a fast skip.
  50531. */
  50532. DUK_DDD(DUK_DDDPRINT("delete successful, check for arguments exotic behavior"));
  50533. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj)) {
  50534. /* Note: only numbered indices are relevant, so arr_idx fast reject
  50535. * is good (this is valid unless there are more than 4**32-1 arguments).
  50536. */
  50537. DUK_DDD(DUK_DDDPRINT("delete successful, arguments exotic behavior needed"));
  50538. /* Note: we can reuse 'desc' here */
  50539. (void) duk__check_arguments_map_for_delete(thr, obj, key, &desc);
  50540. }
  50541. DUK_DDD(DUK_DDDPRINT("delete successful"));
  50542. return 1;
  50543. fail_virtual: /* just use the same "not configurable" error message */
  50544. fail_not_configurable:
  50545. DUK_DDD(DUK_DDDPRINT("delete failed: property found, not configurable"));
  50546. if (throw_flag) {
  50547. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE);
  50548. DUK_WO_NORETURN(return 0;);
  50549. }
  50550. return 0;
  50551. }
  50552. /*
  50553. * DELPROP: ECMAScript property deletion.
  50554. */
  50555. DUK_INTERNAL duk_bool_t duk_hobject_delprop(duk_hthread *thr, duk_tval *tv_obj, duk_tval *tv_key, duk_bool_t throw_flag) {
  50556. duk_hstring *key = NULL;
  50557. #if defined(DUK_USE_ES6_PROXY)
  50558. duk_propdesc desc;
  50559. #endif
  50560. duk_int_t entry_top;
  50561. duk_uint32_t arr_idx = DUK__NO_ARRAY_INDEX;
  50562. duk_bool_t rc;
  50563. DUK_DDD(DUK_DDDPRINT("delprop: thr=%p, obj=%p, key=%p (obj -> %!T, key -> %!T)",
  50564. (void *) thr, (void *) tv_obj, (void *) tv_key,
  50565. (duk_tval *) tv_obj, (duk_tval *) tv_key));
  50566. DUK_ASSERT(thr != NULL);
  50567. DUK_ASSERT(thr->heap != NULL);
  50568. DUK_ASSERT(tv_obj != NULL);
  50569. DUK_ASSERT(tv_key != NULL);
  50570. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  50571. /* Storing the entry top is cheaper here to ensure stack is correct at exit,
  50572. * as there are several paths out.
  50573. */
  50574. entry_top = duk_get_top(thr);
  50575. if (DUK_TVAL_IS_UNDEFINED(tv_obj) ||
  50576. DUK_TVAL_IS_NULL(tv_obj)) {
  50577. DUK_DDD(DUK_DDDPRINT("base object is undefined or null -> reject"));
  50578. goto fail_invalid_base_uncond;
  50579. }
  50580. duk_push_tval(thr, tv_obj);
  50581. duk_push_tval(thr, tv_key);
  50582. tv_obj = DUK_GET_TVAL_NEGIDX(thr, -2);
  50583. if (DUK_TVAL_IS_OBJECT(tv_obj)) {
  50584. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv_obj);
  50585. DUK_ASSERT(obj != NULL);
  50586. #if defined(DUK_USE_ES6_PROXY)
  50587. if (DUK_UNLIKELY(DUK_HOBJECT_IS_PROXY(obj))) {
  50588. duk_hobject *h_target;
  50589. duk_bool_t tmp_bool;
  50590. /* Note: proxy handling must happen before key is string coerced. */
  50591. if (duk__proxy_check_prop(thr, obj, DUK_STRIDX_DELETE_PROPERTY, tv_key, &h_target)) {
  50592. /* -> [ ... obj key trap handler ] */
  50593. DUK_DDD(DUK_DDDPRINT("-> proxy object 'deleteProperty' for key %!T", (duk_tval *) tv_key));
  50594. duk_push_hobject(thr, h_target); /* target */
  50595. duk_dup_m4(thr); /* P */
  50596. duk_call_method(thr, 2 /*nargs*/);
  50597. tmp_bool = duk_to_boolean_top_pop(thr);
  50598. if (!tmp_bool) {
  50599. goto fail_proxy_rejected; /* retval indicates delete failed */
  50600. }
  50601. /* Target object must be checked for a conflicting
  50602. * non-configurable property.
  50603. */
  50604. tv_key = DUK_GET_TVAL_NEGIDX(thr, -1);
  50605. arr_idx = duk__push_tval_to_property_key(thr, tv_key, &key);
  50606. DUK_ASSERT(key != NULL);
  50607. if (duk__get_own_propdesc_raw(thr, h_target, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  50608. duk_small_int_t desc_reject;
  50609. DUK_DDD(DUK_DDDPRINT("proxy 'deleteProperty': target has matching property %!O, check for "
  50610. "conflicting property; desc.flags=0x%08lx, "
  50611. "desc.get=%p, desc.set=%p",
  50612. (duk_heaphdr *) key, (unsigned long) desc.flags,
  50613. (void *) desc.get, (void *) desc.set));
  50614. desc_reject = !(desc.flags & DUK_PROPDESC_FLAG_CONFIGURABLE);
  50615. if (desc_reject) {
  50616. /* unconditional */
  50617. DUK_ERROR_TYPE(thr, DUK_STR_PROXY_REJECTED);
  50618. DUK_WO_NORETURN(return 0;);
  50619. }
  50620. }
  50621. rc = 1; /* success */
  50622. goto done_rc;
  50623. }
  50624. obj = h_target; /* resume delete to target */
  50625. }
  50626. #endif /* DUK_USE_ES6_PROXY */
  50627. arr_idx = duk__to_property_key(thr, -1, &key);
  50628. DUK_ASSERT(key != NULL);
  50629. rc = duk_hobject_delprop_raw(thr, obj, key, throw_flag ? DUK_DELPROP_FLAG_THROW : 0);
  50630. goto done_rc;
  50631. } else if (DUK_TVAL_IS_STRING(tv_obj)) {
  50632. /* String has .length and array index virtual properties
  50633. * which can't be deleted. No need for a symbol check;
  50634. * no offending virtual symbols exist.
  50635. */
  50636. /* XXX: unnecessary string coercion for array indices,
  50637. * intentional to keep small.
  50638. */
  50639. duk_hstring *h = DUK_TVAL_GET_STRING(tv_obj);
  50640. DUK_ASSERT(h != NULL);
  50641. arr_idx = duk__to_property_key(thr, -1, &key);
  50642. DUK_ASSERT(key != NULL);
  50643. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  50644. goto fail_not_configurable;
  50645. }
  50646. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  50647. arr_idx < DUK_HSTRING_GET_CHARLEN(h)) {
  50648. goto fail_not_configurable;
  50649. }
  50650. } else if (DUK_TVAL_IS_BUFFER(tv_obj)) {
  50651. /* XXX: unnecessary string coercion for array indices,
  50652. * intentional to keep small; some overlap with string
  50653. * handling.
  50654. */
  50655. duk_hbuffer *h = DUK_TVAL_GET_BUFFER(tv_obj);
  50656. DUK_ASSERT(h != NULL);
  50657. arr_idx = duk__to_property_key(thr, -1, &key);
  50658. DUK_ASSERT(key != NULL);
  50659. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  50660. goto fail_not_configurable;
  50661. }
  50662. if (arr_idx != DUK__NO_ARRAY_INDEX &&
  50663. arr_idx < DUK_HBUFFER_GET_SIZE(h)) {
  50664. goto fail_not_configurable;
  50665. }
  50666. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_obj)) {
  50667. /* Lightfunc has no virtual properties since Duktape 2.2
  50668. * so success. Still must coerce key for side effects.
  50669. */
  50670. arr_idx = duk__to_property_key(thr, -1, &key);
  50671. DUK_ASSERT(key != NULL);
  50672. DUK_UNREF(key);
  50673. }
  50674. /* non-object base, no offending virtual property */
  50675. rc = 1;
  50676. goto done_rc;
  50677. done_rc:
  50678. duk_set_top_unsafe(thr, entry_top);
  50679. return rc;
  50680. fail_invalid_base_uncond:
  50681. /* Note: unconditional throw */
  50682. DUK_ASSERT(duk_get_top(thr) == entry_top);
  50683. #if defined(DUK_USE_PARANOID_ERRORS)
  50684. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_BASE);
  50685. #else
  50686. DUK_ERROR_FMT2(thr, DUK_ERR_TYPE_ERROR, "cannot delete property %s of %s",
  50687. duk_push_string_tval_readable(thr, tv_key), duk_push_string_tval_readable(thr, tv_obj));
  50688. #endif
  50689. DUK_WO_NORETURN(return 0;);
  50690. #if defined(DUK_USE_ES6_PROXY)
  50691. fail_proxy_rejected:
  50692. if (throw_flag) {
  50693. DUK_ERROR_TYPE(thr, DUK_STR_PROXY_REJECTED);
  50694. DUK_WO_NORETURN(return 0;);
  50695. }
  50696. duk_set_top_unsafe(thr, entry_top);
  50697. return 0;
  50698. #endif
  50699. fail_not_configurable:
  50700. if (throw_flag) {
  50701. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE);
  50702. DUK_WO_NORETURN(return 0;);
  50703. }
  50704. duk_set_top_unsafe(thr, entry_top);
  50705. return 0;
  50706. }
  50707. /*
  50708. * Internal helper to define a property with specific flags, ignoring
  50709. * normal semantics such as extensibility, write protection etc.
  50710. * Overwrites any existing value and attributes unless caller requests
  50711. * that value only be updated if it doesn't already exists.
  50712. *
  50713. * Does not support:
  50714. * - virtual properties (error if write attempted)
  50715. * - getter/setter properties (error if write attempted)
  50716. * - non-default (!= WEC) attributes for array entries (error if attempted)
  50717. * - array abandoning: if array part exists, it is always extended
  50718. * - array 'length' updating
  50719. *
  50720. * Stack: [... in_val] -> []
  50721. *
  50722. * Used for e.g. built-in initialization and environment record
  50723. * operations.
  50724. */
  50725. DUK_INTERNAL void duk_hobject_define_property_internal(duk_hthread *thr, duk_hobject *obj, duk_hstring *key, duk_small_uint_t flags) {
  50726. duk_propdesc desc;
  50727. duk_uint32_t arr_idx;
  50728. duk_int_t e_idx;
  50729. duk_tval *tv1 = NULL;
  50730. duk_tval *tv2 = NULL;
  50731. duk_small_uint_t propflags = flags & DUK_PROPDESC_FLAGS_MASK; /* mask out flags not actually stored */
  50732. DUK_DDD(DUK_DDDPRINT("define new property (internal): thr=%p, obj=%!O, key=%!O, flags=0x%02lx, val=%!T",
  50733. (void *) thr, (duk_heaphdr *) obj, (duk_heaphdr *) key,
  50734. (unsigned long) flags, (duk_tval *) duk_get_tval(thr, -1)));
  50735. DUK_ASSERT(thr != NULL);
  50736. DUK_ASSERT(thr->heap != NULL);
  50737. DUK_ASSERT(obj != NULL);
  50738. DUK_ASSERT(key != NULL);
  50739. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj));
  50740. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  50741. DUK_ASSERT(duk_is_valid_index(thr, -1)); /* contains value */
  50742. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  50743. if (duk__get_own_propdesc_raw(thr, obj, key, arr_idx, &desc, 0 /*flags*/)) { /* don't push value */
  50744. if (desc.e_idx >= 0) {
  50745. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  50746. DUK_DDD(DUK_DDDPRINT("property already exists in the entry part -> skip as requested"));
  50747. goto pop_exit;
  50748. }
  50749. DUK_DDD(DUK_DDDPRINT("property already exists in the entry part -> update value and attributes"));
  50750. if (DUK_UNLIKELY(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, desc.e_idx))) {
  50751. DUK_D(DUK_DPRINT("existing property is an accessor, not supported"));
  50752. goto error_internal;
  50753. }
  50754. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, desc.e_idx, propflags);
  50755. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, desc.e_idx);
  50756. } else if (desc.a_idx >= 0) {
  50757. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  50758. DUK_DDD(DUK_DDDPRINT("property already exists in the array part -> skip as requested"));
  50759. goto pop_exit;
  50760. }
  50761. DUK_DDD(DUK_DDDPRINT("property already exists in the array part -> update value (assert attributes)"));
  50762. if (propflags != DUK_PROPDESC_FLAGS_WEC) {
  50763. DUK_D(DUK_DPRINT("existing property in array part, but propflags not WEC (0x%02lx)",
  50764. (unsigned long) propflags));
  50765. goto error_internal;
  50766. }
  50767. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, desc.a_idx);
  50768. } else {
  50769. if (flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) {
  50770. DUK_DDD(DUK_DDDPRINT("property already exists but is virtual -> skip as requested"));
  50771. goto pop_exit;
  50772. }
  50773. if (key == DUK_HTHREAD_STRING_LENGTH(thr) && DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  50774. duk_uint32_t new_len;
  50775. #if defined(DUK_USE_DEBUG)
  50776. duk_uint32_t prev_len;
  50777. prev_len = ((duk_harray *) obj)->length;
  50778. #endif
  50779. new_len = duk__to_new_array_length_checked(thr, DUK_GET_TVAL_NEGIDX(thr, -1));
  50780. ((duk_harray *) obj)->length = new_len;
  50781. DUK_D(DUK_DPRINT("internal define property for array .length: %ld -> %ld",
  50782. (long) prev_len, (long) ((duk_harray *) obj)->length));
  50783. goto pop_exit;
  50784. }
  50785. DUK_DD(DUK_DDPRINT("property already exists but is virtual -> failure"));
  50786. goto error_virtual;
  50787. }
  50788. goto write_value;
  50789. }
  50790. if (DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  50791. if (arr_idx != DUK__NO_ARRAY_INDEX) {
  50792. DUK_DDD(DUK_DDDPRINT("property does not exist, object has array part -> possibly extend array part and write value (assert attributes)"));
  50793. DUK_ASSERT(propflags == DUK_PROPDESC_FLAGS_WEC);
  50794. /* always grow the array, no sparse / abandon support here */
  50795. if (arr_idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  50796. duk__grow_props_for_array_item(thr, obj, arr_idx);
  50797. }
  50798. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(obj));
  50799. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  50800. goto write_value;
  50801. }
  50802. }
  50803. DUK_DDD(DUK_DDDPRINT("property does not exist, object belongs in entry part -> allocate new entry and write value and attributes"));
  50804. e_idx = duk__hobject_alloc_entry_checked(thr, obj, key); /* increases key refcount */
  50805. DUK_ASSERT(e_idx >= 0);
  50806. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, propflags);
  50807. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  50808. /* new entry: previous value is garbage; set to undefined to share write_value */
  50809. DUK_TVAL_SET_UNDEFINED(tv1);
  50810. goto write_value;
  50811. write_value:
  50812. /* tv1 points to value storage */
  50813. tv2 = duk_require_tval(thr, -1); /* late lookup, avoid side effects */
  50814. DUK_DDD(DUK_DDDPRINT("writing/updating value: %!T -> %!T",
  50815. (duk_tval *) tv1, (duk_tval *) tv2));
  50816. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  50817. goto pop_exit;
  50818. pop_exit:
  50819. duk_pop_unsafe(thr); /* remove in_val */
  50820. return;
  50821. error_virtual: /* share error message */
  50822. error_internal:
  50823. DUK_ERROR_INTERNAL(thr);
  50824. DUK_WO_NORETURN(return;);
  50825. }
  50826. /*
  50827. * Fast path for defining array indexed values without interning the key.
  50828. * This is used by e.g. code for Array prototype and traceback creation so
  50829. * must avoid interning.
  50830. */
  50831. DUK_INTERNAL void duk_hobject_define_property_internal_arridx(duk_hthread *thr, duk_hobject *obj, duk_uarridx_t arr_idx, duk_small_uint_t flags) {
  50832. duk_hstring *key;
  50833. duk_tval *tv1, *tv2;
  50834. DUK_DDD(DUK_DDDPRINT("define new property (internal) arr_idx fast path: thr=%p, obj=%!O, "
  50835. "arr_idx=%ld, flags=0x%02lx, val=%!T",
  50836. (void *) thr, obj, (long) arr_idx, (unsigned long) flags,
  50837. (duk_tval *) duk_get_tval(thr, -1)));
  50838. DUK_ASSERT(thr != NULL);
  50839. DUK_ASSERT(thr->heap != NULL);
  50840. DUK_ASSERT(obj != NULL);
  50841. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj));
  50842. if (DUK_HOBJECT_HAS_ARRAY_PART(obj) &&
  50843. arr_idx != DUK__NO_ARRAY_INDEX &&
  50844. flags == DUK_PROPDESC_FLAGS_WEC) {
  50845. DUK_ASSERT((flags & DUK_PROPDESC_FLAG_NO_OVERWRITE) == 0); /* covered by comparison */
  50846. DUK_DDD(DUK_DDDPRINT("define property to array part (property may or may not exist yet)"));
  50847. /* always grow the array, no sparse / abandon support here */
  50848. if (arr_idx >= DUK_HOBJECT_GET_ASIZE(obj)) {
  50849. duk__grow_props_for_array_item(thr, obj, arr_idx);
  50850. }
  50851. DUK_ASSERT(arr_idx < DUK_HOBJECT_GET_ASIZE(obj));
  50852. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, arr_idx);
  50853. tv2 = duk_require_tval(thr, -1);
  50854. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  50855. duk_pop_unsafe(thr); /* [ ...val ] -> [ ... ] */
  50856. return;
  50857. }
  50858. DUK_DDD(DUK_DDDPRINT("define property fast path didn't work, use slow path"));
  50859. key = duk_push_uint_to_hstring(thr, (duk_uint_t) arr_idx);
  50860. DUK_ASSERT(key != NULL);
  50861. duk_insert(thr, -2); /* [ ... val key ] -> [ ... key val ] */
  50862. duk_hobject_define_property_internal(thr, obj, key, flags);
  50863. duk_pop_unsafe(thr); /* [ ... key ] -> [ ... ] */
  50864. }
  50865. /*
  50866. * Internal helpers for managing object 'length'
  50867. */
  50868. DUK_INTERNAL duk_size_t duk_hobject_get_length(duk_hthread *thr, duk_hobject *obj) {
  50869. duk_double_t val;
  50870. DUK_ASSERT_CTX_VALID(thr);
  50871. DUK_ASSERT(obj != NULL);
  50872. /* Fast path for Arrays. */
  50873. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  50874. return ((duk_harray *) obj)->length;
  50875. }
  50876. /* Slow path, .length can be e.g. accessor, obj can be a Proxy, etc. */
  50877. duk_push_hobject(thr, obj);
  50878. duk_push_hstring_stridx(thr, DUK_STRIDX_LENGTH);
  50879. (void) duk_hobject_getprop(thr,
  50880. DUK_GET_TVAL_NEGIDX(thr, -2),
  50881. DUK_GET_TVAL_NEGIDX(thr, -1));
  50882. val = duk_to_number_m1(thr);
  50883. duk_pop_3_unsafe(thr);
  50884. /* This isn't part of ECMAScript semantics; return a value within
  50885. * duk_size_t range, or 0 otherwise.
  50886. */
  50887. if (val >= 0.0 && val <= (duk_double_t) DUK_SIZE_MAX) {
  50888. return (duk_size_t) val;
  50889. }
  50890. return 0;
  50891. }
  50892. /*
  50893. * Fast finalizer check for an object. Walks the prototype chain, checking
  50894. * for finalizer presence using DUK_HOBJECT_FLAG_HAVE_FINALIZER which is kept
  50895. * in sync with the actual property when setting/removing the finalizer.
  50896. */
  50897. #if defined(DUK_USE_HEAPPTR16)
  50898. DUK_INTERNAL duk_bool_t duk_hobject_has_finalizer_fast_raw(duk_heap *heap, duk_hobject *obj) {
  50899. #else
  50900. DUK_INTERNAL duk_bool_t duk_hobject_has_finalizer_fast_raw(duk_hobject *obj) {
  50901. #endif
  50902. duk_uint_t sanity;
  50903. DUK_ASSERT(obj != NULL);
  50904. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  50905. do {
  50906. if (DUK_UNLIKELY(DUK_HOBJECT_HAS_HAVE_FINALIZER(obj))) {
  50907. return 1;
  50908. }
  50909. if (DUK_UNLIKELY(sanity-- == 0)) {
  50910. DUK_D(DUK_DPRINT("prototype loop when checking for finalizer existence; returning false"));
  50911. return 0;
  50912. }
  50913. #if defined(DUK_USE_HEAPPTR16)
  50914. DUK_ASSERT(heap != NULL);
  50915. obj = DUK_HOBJECT_GET_PROTOTYPE(heap, obj);
  50916. #else
  50917. obj = DUK_HOBJECT_GET_PROTOTYPE(NULL, obj); /* 'heap' arg ignored */
  50918. #endif
  50919. } while (obj != NULL);
  50920. return 0;
  50921. }
  50922. /*
  50923. * Object.getOwnPropertyDescriptor() (E5 Sections 15.2.3.3, 8.10.4)
  50924. *
  50925. * [ ... key ] -> [ ... desc/undefined ]
  50926. */
  50927. DUK_INTERNAL void duk_hobject_object_get_own_property_descriptor(duk_hthread *thr, duk_idx_t obj_idx) {
  50928. duk_hobject *obj;
  50929. duk_hstring *key;
  50930. duk_propdesc pd;
  50931. DUK_ASSERT(thr != NULL);
  50932. DUK_ASSERT(thr->heap != NULL);
  50933. obj = duk_require_hobject_promote_mask(thr, obj_idx, DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  50934. key = duk_to_property_key_hstring(thr, -1);
  50935. DUK_ASSERT(key != NULL);
  50936. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  50937. if (!duk_hobject_get_own_propdesc(thr, obj, key, &pd, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  50938. duk_push_undefined(thr);
  50939. duk_remove_m2(thr);
  50940. return;
  50941. }
  50942. duk_push_object(thr);
  50943. /* [ ... key value desc ] */
  50944. if (DUK_PROPDESC_IS_ACCESSOR(&pd)) {
  50945. /* If a setter/getter is missing (undefined), the descriptor must
  50946. * still have the property present with the value 'undefined'.
  50947. */
  50948. if (pd.get) {
  50949. duk_push_hobject(thr, pd.get);
  50950. } else {
  50951. duk_push_undefined(thr);
  50952. }
  50953. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_GET);
  50954. if (pd.set) {
  50955. duk_push_hobject(thr, pd.set);
  50956. } else {
  50957. duk_push_undefined(thr);
  50958. }
  50959. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_SET);
  50960. } else {
  50961. duk_dup_m2(thr);
  50962. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_VALUE);
  50963. duk_push_boolean(thr, DUK_PROPDESC_IS_WRITABLE(&pd));
  50964. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_WRITABLE);
  50965. }
  50966. duk_push_boolean(thr, DUK_PROPDESC_IS_ENUMERABLE(&pd));
  50967. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_ENUMERABLE);
  50968. duk_push_boolean(thr, DUK_PROPDESC_IS_CONFIGURABLE(&pd));
  50969. duk_put_prop_stridx_short(thr, -2, DUK_STRIDX_CONFIGURABLE);
  50970. /* [ ... key value desc ] */
  50971. duk_replace(thr, -3);
  50972. duk_pop_unsafe(thr); /* -> [ ... desc ] */
  50973. }
  50974. /*
  50975. * NormalizePropertyDescriptor() related helper.
  50976. *
  50977. * Internal helper which validates and normalizes a property descriptor
  50978. * represented as an ECMAScript object (e.g. argument to defineProperty()).
  50979. * The output of this conversion is a set of defprop_flags and possibly
  50980. * some values pushed on the value stack to (1) ensure borrowed pointers
  50981. * remain valid, and (2) avoid unnecessary pops for footprint reasons.
  50982. * Caller must manage stack top carefully because the number of values
  50983. * pushed depends on the input property descriptor.
  50984. *
  50985. * The original descriptor object must not be altered in the process.
  50986. */
  50987. /* XXX: very basic optimization -> duk_get_prop_stridx_top */
  50988. DUK_INTERNAL
  50989. void duk_hobject_prepare_property_descriptor(duk_hthread *thr,
  50990. duk_idx_t idx_in,
  50991. duk_uint_t *out_defprop_flags,
  50992. duk_idx_t *out_idx_value,
  50993. duk_hobject **out_getter,
  50994. duk_hobject **out_setter) {
  50995. duk_idx_t idx_value = -1;
  50996. duk_hobject *getter = NULL;
  50997. duk_hobject *setter = NULL;
  50998. duk_bool_t is_data_desc = 0;
  50999. duk_bool_t is_acc_desc = 0;
  51000. duk_uint_t defprop_flags = 0;
  51001. DUK_ASSERT(out_defprop_flags != NULL);
  51002. DUK_ASSERT(out_idx_value != NULL);
  51003. DUK_ASSERT(out_getter != NULL);
  51004. DUK_ASSERT(out_setter != NULL);
  51005. DUK_ASSERT(idx_in <= 0x7fffL); /* short variants would be OK, but not used to avoid shifts */
  51006. /* Must be an object, otherwise TypeError (E5.1 Section 8.10.5, step 1). */
  51007. idx_in = duk_require_normalize_index(thr, idx_in);
  51008. (void) duk_require_hobject(thr, idx_in);
  51009. /* The coercion order must match the ToPropertyDescriptor() algorithm
  51010. * so that side effects in coercion happen in the correct order.
  51011. * (This order also happens to be compatible with duk_def_prop(),
  51012. * although it doesn't matter in practice.)
  51013. */
  51014. if (duk_get_prop_stridx(thr, idx_in, DUK_STRIDX_VALUE)) {
  51015. is_data_desc = 1;
  51016. defprop_flags |= DUK_DEFPROP_HAVE_VALUE;
  51017. idx_value = duk_get_top_index(thr);
  51018. }
  51019. if (duk_get_prop_stridx(thr, idx_in, DUK_STRIDX_WRITABLE)) {
  51020. is_data_desc = 1;
  51021. if (duk_to_boolean_top_pop(thr)) {
  51022. defprop_flags |= DUK_DEFPROP_HAVE_WRITABLE | DUK_DEFPROP_WRITABLE;
  51023. } else {
  51024. defprop_flags |= DUK_DEFPROP_HAVE_WRITABLE;
  51025. }
  51026. }
  51027. if (duk_get_prop_stridx(thr, idx_in, DUK_STRIDX_GET)) {
  51028. duk_tval *tv = duk_require_tval(thr, -1);
  51029. duk_hobject *h_get;
  51030. if (DUK_TVAL_IS_UNDEFINED(tv)) {
  51031. /* undefined is accepted */
  51032. DUK_ASSERT(getter == NULL);
  51033. } else {
  51034. /* NOTE: lightfuncs are coerced to full functions because
  51035. * lightfuncs don't fit into a property value slot. This
  51036. * has some side effects, see test-dev-lightfunc-accessor.js.
  51037. */
  51038. h_get = duk_get_hobject_promote_lfunc(thr, -1);
  51039. if (h_get == NULL || !DUK_HOBJECT_IS_CALLABLE(h_get)) {
  51040. goto type_error;
  51041. }
  51042. getter = h_get;
  51043. }
  51044. is_acc_desc = 1;
  51045. defprop_flags |= DUK_DEFPROP_HAVE_GETTER;
  51046. }
  51047. if (duk_get_prop_stridx(thr, idx_in, DUK_STRIDX_SET)) {
  51048. duk_tval *tv = duk_require_tval(thr, -1);
  51049. duk_hobject *h_set;
  51050. if (DUK_TVAL_IS_UNDEFINED(tv)) {
  51051. /* undefined is accepted */
  51052. DUK_ASSERT(setter == NULL);
  51053. } else {
  51054. /* NOTE: lightfuncs are coerced to full functions because
  51055. * lightfuncs don't fit into a property value slot. This
  51056. * has some side effects, see test-dev-lightfunc-accessor.js.
  51057. */
  51058. h_set = duk_get_hobject_promote_lfunc(thr, -1);
  51059. if (h_set == NULL || !DUK_HOBJECT_IS_CALLABLE(h_set)) {
  51060. goto type_error;
  51061. }
  51062. setter = h_set;
  51063. }
  51064. is_acc_desc = 1;
  51065. defprop_flags |= DUK_DEFPROP_HAVE_SETTER;
  51066. }
  51067. if (duk_get_prop_stridx(thr, idx_in, DUK_STRIDX_ENUMERABLE)) {
  51068. if (duk_to_boolean_top_pop(thr)) {
  51069. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE;
  51070. } else {
  51071. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE;
  51072. }
  51073. }
  51074. if (duk_get_prop_stridx(thr, idx_in, DUK_STRIDX_CONFIGURABLE)) {
  51075. if (duk_to_boolean_top_pop(thr)) {
  51076. defprop_flags |= DUK_DEFPROP_HAVE_CONFIGURABLE | DUK_DEFPROP_CONFIGURABLE;
  51077. } else {
  51078. defprop_flags |= DUK_DEFPROP_HAVE_CONFIGURABLE;
  51079. }
  51080. }
  51081. if (is_data_desc && is_acc_desc) {
  51082. goto type_error;
  51083. }
  51084. *out_defprop_flags = defprop_flags;
  51085. *out_idx_value = idx_value;
  51086. *out_getter = getter;
  51087. *out_setter = setter;
  51088. /* [ ... [multiple values] ] */
  51089. return;
  51090. type_error:
  51091. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_DESCRIPTOR);
  51092. DUK_WO_NORETURN(return;);
  51093. }
  51094. /*
  51095. * Object.defineProperty() related helper (E5 Section 15.2.3.6).
  51096. * Also handles ES2015 Reflect.defineProperty().
  51097. *
  51098. * Inlines all [[DefineOwnProperty]] exotic behaviors.
  51099. *
  51100. * Note: ECMAScript compliant [[DefineOwnProperty]](P, Desc, Throw) is not
  51101. * implemented directly, but Object.defineProperty() serves its purpose.
  51102. * We don't need the [[DefineOwnProperty]] internally and we don't have a
  51103. * property descriptor with 'missing values' so it's easier to avoid it
  51104. * entirely.
  51105. *
  51106. * Note: this is only called for actual objects, not primitive values.
  51107. * This must support virtual properties for full objects (e.g. Strings)
  51108. * but not for plain values (e.g. strings). Lightfuncs, even though
  51109. * primitive in a sense, are treated like objects and accepted as target
  51110. * values.
  51111. */
  51112. /* XXX: this is a major target for size optimization */
  51113. DUK_INTERNAL
  51114. duk_bool_t duk_hobject_define_property_helper(duk_hthread *thr,
  51115. duk_uint_t defprop_flags,
  51116. duk_hobject *obj,
  51117. duk_hstring *key,
  51118. duk_idx_t idx_value,
  51119. duk_hobject *get,
  51120. duk_hobject *set,
  51121. duk_bool_t throw_flag) {
  51122. duk_uint32_t arr_idx;
  51123. duk_tval tv;
  51124. duk_bool_t has_enumerable;
  51125. duk_bool_t has_configurable;
  51126. duk_bool_t has_writable;
  51127. duk_bool_t has_value;
  51128. duk_bool_t has_get;
  51129. duk_bool_t has_set;
  51130. duk_bool_t is_enumerable;
  51131. duk_bool_t is_configurable;
  51132. duk_bool_t is_writable;
  51133. duk_bool_t force_flag;
  51134. duk_small_uint_t new_flags;
  51135. duk_propdesc curr;
  51136. duk_uint32_t arridx_new_array_length; /* != 0 => post-update for array 'length' (used when key is an array index) */
  51137. duk_uint32_t arrlen_old_len;
  51138. duk_uint32_t arrlen_new_len;
  51139. duk_bool_t pending_write_protect;
  51140. DUK_ASSERT(thr != NULL);
  51141. DUK_ASSERT(thr->heap != NULL);
  51142. DUK_ASSERT(obj != NULL);
  51143. DUK_ASSERT(key != NULL);
  51144. /* idx_value may be < 0 (no value), set and get may be NULL */
  51145. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  51146. /* All the flags fit in 16 bits, so will fit into duk_bool_t. */
  51147. has_writable = (defprop_flags & DUK_DEFPROP_HAVE_WRITABLE);
  51148. has_enumerable = (defprop_flags & DUK_DEFPROP_HAVE_ENUMERABLE);
  51149. has_configurable = (defprop_flags & DUK_DEFPROP_HAVE_CONFIGURABLE);
  51150. has_value = (defprop_flags & DUK_DEFPROP_HAVE_VALUE);
  51151. has_get = (defprop_flags & DUK_DEFPROP_HAVE_GETTER);
  51152. has_set = (defprop_flags & DUK_DEFPROP_HAVE_SETTER);
  51153. is_writable = (defprop_flags & DUK_DEFPROP_WRITABLE);
  51154. is_enumerable = (defprop_flags & DUK_DEFPROP_ENUMERABLE);
  51155. is_configurable = (defprop_flags & DUK_DEFPROP_CONFIGURABLE);
  51156. force_flag = (defprop_flags & DUK_DEFPROP_FORCE);
  51157. arr_idx = DUK_HSTRING_GET_ARRIDX_SLOW(key);
  51158. arridx_new_array_length = 0;
  51159. pending_write_protect = 0;
  51160. arrlen_old_len = 0;
  51161. arrlen_new_len = 0;
  51162. DUK_DDD(DUK_DDDPRINT("has_enumerable=%ld is_enumerable=%ld "
  51163. "has_configurable=%ld is_configurable=%ld "
  51164. "has_writable=%ld is_writable=%ld "
  51165. "has_value=%ld value=%!T "
  51166. "has_get=%ld get=%p=%!O "
  51167. "has_set=%ld set=%p=%!O "
  51168. "arr_idx=%ld throw_flag=!%ld",
  51169. (long) has_enumerable, (long) is_enumerable,
  51170. (long) has_configurable, (long) is_configurable,
  51171. (long) has_writable, (long) is_writable,
  51172. (long) has_value, (duk_tval *) (idx_value >= 0 ? duk_get_tval(thr, idx_value) : NULL),
  51173. (long) has_get, (void *) get, (duk_heaphdr *) get,
  51174. (long) has_set, (void *) set, (duk_heaphdr *) set,
  51175. (long) arr_idx, (long) throw_flag));
  51176. /*
  51177. * Array exotic behaviors can be implemented at this point. The local variables
  51178. * are essentially a 'value copy' of the input descriptor (Desc), which is modified
  51179. * by the Array [[DefineOwnProperty]] (E5 Section 15.4.5.1).
  51180. */
  51181. if (!DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  51182. goto skip_array_exotic;
  51183. }
  51184. if (key == DUK_HTHREAD_STRING_LENGTH(thr)) {
  51185. duk_harray *a;
  51186. /* E5 Section 15.4.5.1, step 3, steps a - i are implemented here, j - n at the end */
  51187. if (!has_value) {
  51188. DUK_DDD(DUK_DDDPRINT("exotic array behavior for 'length', but no value in descriptor -> normal behavior"));
  51189. goto skip_array_exotic;
  51190. }
  51191. DUK_DDD(DUK_DDDPRINT("exotic array behavior for 'length', value present in descriptor -> exotic behavior"));
  51192. /*
  51193. * Get old and new length
  51194. */
  51195. a = (duk_harray *) obj;
  51196. DUK_ASSERT_HARRAY_VALID(a);
  51197. arrlen_old_len = a->length;
  51198. DUK_ASSERT(idx_value >= 0);
  51199. arrlen_new_len = duk__to_new_array_length_checked(thr, DUK_GET_TVAL_POSIDX(thr, idx_value));
  51200. duk_push_u32(thr, arrlen_new_len);
  51201. duk_replace(thr, idx_value); /* step 3.e: replace 'Desc.[[Value]]' */
  51202. DUK_DDD(DUK_DDDPRINT("old_len=%ld, new_len=%ld", (long) arrlen_old_len, (long) arrlen_new_len));
  51203. if (arrlen_new_len >= arrlen_old_len) {
  51204. /* standard behavior, step 3.f.i */
  51205. DUK_DDD(DUK_DDDPRINT("new length is same or higher as previous => standard behavior"));
  51206. goto skip_array_exotic;
  51207. }
  51208. DUK_DDD(DUK_DDDPRINT("new length is smaller than previous => exotic post behavior"));
  51209. /* XXX: consolidated algorithm step 15.f -> redundant? */
  51210. if (DUK_HARRAY_LENGTH_NONWRITABLE(a) && !force_flag) {
  51211. /* Array .length is always non-configurable; if it's also
  51212. * non-writable, don't allow it to be written.
  51213. */
  51214. goto fail_not_configurable;
  51215. }
  51216. /* steps 3.h and 3.i */
  51217. if (has_writable && !is_writable) {
  51218. DUK_DDD(DUK_DDDPRINT("desc writable is false, force it back to true, and flag pending write protect"));
  51219. is_writable = 1;
  51220. pending_write_protect = 1;
  51221. }
  51222. /* remaining actual steps are carried out if standard DefineOwnProperty succeeds */
  51223. } else if (arr_idx != DUK__NO_ARRAY_INDEX) {
  51224. /* XXX: any chance of unifying this with the 'length' key handling? */
  51225. /* E5 Section 15.4.5.1, step 4 */
  51226. duk_uint32_t old_len;
  51227. duk_harray *a;
  51228. a = (duk_harray *) obj;
  51229. DUK_ASSERT_HARRAY_VALID(a);
  51230. old_len = a->length;
  51231. if (arr_idx >= old_len) {
  51232. DUK_DDD(DUK_DDDPRINT("defineProperty requires array length update "
  51233. "(arr_idx=%ld, old_len=%ld)",
  51234. (long) arr_idx, (long) old_len));
  51235. if (DUK_HARRAY_LENGTH_NONWRITABLE(a) && !force_flag) {
  51236. /* Array .length is always non-configurable, so
  51237. * if it's also non-writable, don't allow a value
  51238. * write. With force flag allow writing.
  51239. */
  51240. goto fail_not_configurable;
  51241. }
  51242. /* actual update happens once write has been completed without
  51243. * error below.
  51244. */
  51245. DUK_ASSERT(arr_idx != 0xffffffffUL);
  51246. arridx_new_array_length = arr_idx + 1;
  51247. } else {
  51248. DUK_DDD(DUK_DDDPRINT("defineProperty does not require length update "
  51249. "(arr_idx=%ld, old_len=%ld) -> standard behavior",
  51250. (long) arr_idx, (long) old_len));
  51251. }
  51252. }
  51253. skip_array_exotic:
  51254. /* XXX: There is currently no support for writing buffer object
  51255. * indexed elements here. Attempt to do so will succeed and
  51256. * write a concrete property into the buffer object. This should
  51257. * be fixed at some point but because buffers are a custom feature
  51258. * anyway, this is relatively unimportant.
  51259. */
  51260. /*
  51261. * Actual Object.defineProperty() default algorithm.
  51262. */
  51263. /*
  51264. * First check whether property exists; if not, simple case. This covers
  51265. * steps 1-4.
  51266. */
  51267. if (!duk__get_own_propdesc_raw(thr, obj, key, arr_idx, &curr, DUK_GETDESC_FLAG_PUSH_VALUE)) {
  51268. DUK_DDD(DUK_DDDPRINT("property does not exist"));
  51269. if (!DUK_HOBJECT_HAS_EXTENSIBLE(obj) && !force_flag) {
  51270. goto fail_not_extensible;
  51271. }
  51272. #if defined(DUK_USE_ROM_OBJECTS)
  51273. /* ROM objects are never extensible but force flag may
  51274. * allow us to come here anyway.
  51275. */
  51276. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj) || !DUK_HOBJECT_HAS_EXTENSIBLE(obj));
  51277. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) {
  51278. DUK_D(DUK_DPRINT("attempt to define property on a read-only target object"));
  51279. goto fail_not_configurable;
  51280. }
  51281. #endif
  51282. /* XXX: share final setting code for value and flags? difficult because
  51283. * refcount code is different. Share entry allocation? But can't allocate
  51284. * until array index checked.
  51285. */
  51286. /* steps 4.a and 4.b are tricky */
  51287. if (has_set || has_get) {
  51288. duk_int_t e_idx;
  51289. DUK_DDD(DUK_DDDPRINT("create new accessor property"));
  51290. DUK_ASSERT(has_set || set == NULL);
  51291. DUK_ASSERT(has_get || get == NULL);
  51292. DUK_ASSERT(!has_value);
  51293. DUK_ASSERT(!has_writable);
  51294. new_flags = DUK_PROPDESC_FLAG_ACCESSOR; /* defaults, E5 Section 8.6.1, Table 7 */
  51295. if (has_enumerable && is_enumerable) {
  51296. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  51297. }
  51298. if (has_configurable && is_configurable) {
  51299. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  51300. }
  51301. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  51302. DUK_DDD(DUK_DDDPRINT("accessor cannot go to array part, abandon array"));
  51303. duk__abandon_array_checked(thr, obj);
  51304. }
  51305. /* write to entry part */
  51306. e_idx = duk__hobject_alloc_entry_checked(thr, obj, key);
  51307. DUK_ASSERT(e_idx >= 0);
  51308. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, e_idx, get);
  51309. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, e_idx, set);
  51310. DUK_HOBJECT_INCREF_ALLOWNULL(thr, get);
  51311. DUK_HOBJECT_INCREF_ALLOWNULL(thr, set);
  51312. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, new_flags);
  51313. goto success_exotics;
  51314. } else {
  51315. duk_int_t e_idx;
  51316. duk_tval *tv2;
  51317. DUK_DDD(DUK_DDDPRINT("create new data property"));
  51318. DUK_ASSERT(!has_set);
  51319. DUK_ASSERT(!has_get);
  51320. new_flags = 0; /* defaults, E5 Section 8.6.1, Table 7 */
  51321. if (has_writable && is_writable) {
  51322. new_flags |= DUK_PROPDESC_FLAG_WRITABLE;
  51323. }
  51324. if (has_enumerable && is_enumerable) {
  51325. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  51326. }
  51327. if (has_configurable && is_configurable) {
  51328. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  51329. }
  51330. if (has_value) {
  51331. duk_tval *tv_tmp = duk_require_tval(thr, idx_value);
  51332. DUK_TVAL_SET_TVAL(&tv, tv_tmp);
  51333. } else {
  51334. DUK_TVAL_SET_UNDEFINED(&tv); /* default value */
  51335. }
  51336. if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_ARRAY_PART(obj)) {
  51337. if (new_flags == DUK_PROPDESC_FLAGS_WEC) {
  51338. #if 0
  51339. DUK_DDD(DUK_DDDPRINT("new data property attributes match array defaults, attempt to write to array part"));
  51340. /* may become sparse...*/
  51341. #endif
  51342. /* XXX: handling for array part missing now; this doesn't affect
  51343. * compliance but causes array entry writes using defineProperty()
  51344. * to always abandon array part.
  51345. */
  51346. }
  51347. DUK_DDD(DUK_DDDPRINT("new data property cannot go to array part, abandon array"));
  51348. duk__abandon_array_checked(thr, obj);
  51349. /* fall through */
  51350. }
  51351. /* write to entry part */
  51352. e_idx = duk__hobject_alloc_entry_checked(thr, obj, key);
  51353. DUK_ASSERT(e_idx >= 0);
  51354. tv2 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, e_idx);
  51355. DUK_TVAL_SET_TVAL(tv2, &tv);
  51356. DUK_TVAL_INCREF(thr, tv2);
  51357. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, e_idx, new_flags);
  51358. goto success_exotics;
  51359. }
  51360. DUK_UNREACHABLE();
  51361. }
  51362. /* we currently assume virtual properties are not configurable (as none of them are) */
  51363. DUK_ASSERT((curr.e_idx >= 0 || curr.a_idx >= 0) || !(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE));
  51364. /* [obj key desc value get set curr_value] */
  51365. /*
  51366. * Property already exists. Steps 5-6 detect whether any changes need
  51367. * to be made.
  51368. */
  51369. if (has_enumerable) {
  51370. if (is_enumerable) {
  51371. if (!(curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE)) {
  51372. goto need_check;
  51373. }
  51374. } else {
  51375. if (curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE) {
  51376. goto need_check;
  51377. }
  51378. }
  51379. }
  51380. if (has_configurable) {
  51381. if (is_configurable) {
  51382. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  51383. goto need_check;
  51384. }
  51385. } else {
  51386. if (curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) {
  51387. goto need_check;
  51388. }
  51389. }
  51390. }
  51391. if (has_value) {
  51392. duk_tval *tmp1;
  51393. duk_tval *tmp2;
  51394. /* attempt to change from accessor to data property */
  51395. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  51396. goto need_check;
  51397. }
  51398. tmp1 = duk_require_tval(thr, -1); /* curr value */
  51399. tmp2 = duk_require_tval(thr, idx_value); /* new value */
  51400. if (!duk_js_samevalue(tmp1, tmp2)) {
  51401. goto need_check;
  51402. }
  51403. }
  51404. if (has_writable) {
  51405. /* attempt to change from accessor to data property */
  51406. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  51407. goto need_check;
  51408. }
  51409. if (is_writable) {
  51410. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  51411. goto need_check;
  51412. }
  51413. } else {
  51414. if (curr.flags & DUK_PROPDESC_FLAG_WRITABLE) {
  51415. goto need_check;
  51416. }
  51417. }
  51418. }
  51419. if (has_set) {
  51420. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  51421. if (set != curr.set) {
  51422. goto need_check;
  51423. }
  51424. } else {
  51425. goto need_check;
  51426. }
  51427. }
  51428. if (has_get) {
  51429. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  51430. if (get != curr.get) {
  51431. goto need_check;
  51432. }
  51433. } else {
  51434. goto need_check;
  51435. }
  51436. }
  51437. /* property exists, either 'desc' is empty, or all values
  51438. * match (SameValue)
  51439. */
  51440. goto success_no_exotics;
  51441. need_check:
  51442. /*
  51443. * Some change(s) need to be made. Steps 7-11.
  51444. */
  51445. /* shared checks for all descriptor types */
  51446. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  51447. if (has_configurable && is_configurable) {
  51448. goto fail_not_configurable;
  51449. }
  51450. if (has_enumerable) {
  51451. if (curr.flags & DUK_PROPDESC_FLAG_ENUMERABLE) {
  51452. if (!is_enumerable) {
  51453. goto fail_not_configurable;
  51454. }
  51455. } else {
  51456. if (is_enumerable) {
  51457. goto fail_not_configurable;
  51458. }
  51459. }
  51460. }
  51461. }
  51462. /* Virtual properties don't have backing so they can't mostly be
  51463. * edited. Some virtual properties are, however, writable: for
  51464. * example, virtual index properties of buffer objects and Array
  51465. * instance .length. These are not configurable so the checks
  51466. * above mostly cover attempts to change them, except when the
  51467. * duk_def_prop() call is used with DUK_DEFPROP_FORCE; even in
  51468. * that case we can't forcibly change the property attributes
  51469. * because they don't have concrete backing.
  51470. */
  51471. /* XXX: for ROM objects too it'd be best if value modify was
  51472. * allowed if the value matches SameValue.
  51473. */
  51474. /* Reject attempt to change a read-only object. */
  51475. #if defined(DUK_USE_ROM_OBJECTS)
  51476. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) {
  51477. DUK_DD(DUK_DDPRINT("attempt to define property on read-only target object"));
  51478. goto fail_not_configurable;
  51479. }
  51480. #endif
  51481. /* descriptor type specific checks */
  51482. if (has_set || has_get) {
  51483. /* IsAccessorDescriptor(desc) == true */
  51484. DUK_ASSERT(!has_writable);
  51485. DUK_ASSERT(!has_value);
  51486. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  51487. /* curr and desc are accessors */
  51488. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  51489. if (has_set && set != curr.set) {
  51490. goto fail_not_configurable;
  51491. }
  51492. if (has_get && get != curr.get) {
  51493. goto fail_not_configurable;
  51494. }
  51495. }
  51496. } else {
  51497. duk_bool_t rc;
  51498. duk_tval *tv1;
  51499. /* curr is data, desc is accessor */
  51500. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  51501. goto fail_not_configurable;
  51502. }
  51503. DUK_DDD(DUK_DDDPRINT("convert property to accessor property"));
  51504. if (curr.a_idx >= 0) {
  51505. DUK_DDD(DUK_DDDPRINT("property to convert is stored in an array entry, abandon array and re-lookup"));
  51506. duk__abandon_array_checked(thr, obj);
  51507. duk_pop_unsafe(thr); /* remove old value */
  51508. rc = duk__get_own_propdesc_raw(thr, obj, key, arr_idx, &curr, DUK_GETDESC_FLAG_PUSH_VALUE);
  51509. DUK_UNREF(rc);
  51510. DUK_ASSERT(rc != 0);
  51511. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  51512. }
  51513. if (curr.e_idx < 0) {
  51514. DUK_ASSERT(curr.a_idx < 0 && curr.e_idx < 0);
  51515. goto fail_virtual; /* safeguard for virtual property */
  51516. }
  51517. DUK_ASSERT(curr.e_idx >= 0);
  51518. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  51519. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  51520. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, tv1); /* XXX: just decref */
  51521. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, NULL);
  51522. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, NULL);
  51523. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  51524. DUK_HOBJECT_E_SLOT_SET_ACCESSOR(thr->heap, obj, curr.e_idx);
  51525. DUK_DDD(DUK_DDDPRINT("flags after data->accessor conversion: 0x%02lx",
  51526. (unsigned long) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, curr.e_idx)));
  51527. /* Update curr.flags; faster than a re-lookup. */
  51528. curr.flags &= ~DUK_PROPDESC_FLAG_WRITABLE;
  51529. curr.flags |= DUK_PROPDESC_FLAG_ACCESSOR;
  51530. }
  51531. } else if (has_value || has_writable) {
  51532. /* IsDataDescriptor(desc) == true */
  51533. DUK_ASSERT(!has_set);
  51534. DUK_ASSERT(!has_get);
  51535. if (curr.flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  51536. duk_hobject *tmp;
  51537. /* curr is accessor, desc is data */
  51538. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  51539. goto fail_not_configurable;
  51540. }
  51541. /* curr is accessor -> cannot be in array part. */
  51542. DUK_ASSERT(curr.a_idx < 0);
  51543. if (curr.e_idx < 0) {
  51544. goto fail_virtual; /* safeguard; no virtual accessors now */
  51545. }
  51546. DUK_DDD(DUK_DDDPRINT("convert property to data property"));
  51547. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  51548. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, curr.e_idx);
  51549. DUK_UNREF(tmp);
  51550. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, NULL);
  51551. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, tmp);
  51552. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, curr.e_idx);
  51553. DUK_UNREF(tmp);
  51554. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, NULL);
  51555. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, tmp);
  51556. DUK_TVAL_SET_UNDEFINED(DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx));
  51557. DUK_HOBJECT_E_SLOT_CLEAR_WRITABLE(thr->heap, obj, curr.e_idx);
  51558. DUK_HOBJECT_E_SLOT_CLEAR_ACCESSOR(thr->heap, obj, curr.e_idx);
  51559. DUK_DDD(DUK_DDDPRINT("flags after accessor->data conversion: 0x%02lx",
  51560. (unsigned long) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, curr.e_idx)));
  51561. /* Update curr.flags; faster than a re-lookup. */
  51562. curr.flags &= ~(DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_ACCESSOR);
  51563. } else {
  51564. /* curr and desc are data */
  51565. if (!(curr.flags & DUK_PROPDESC_FLAG_CONFIGURABLE) && !force_flag) {
  51566. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && has_writable && is_writable) {
  51567. goto fail_not_configurable;
  51568. }
  51569. /* Note: changing from writable to non-writable is OK */
  51570. if (!(curr.flags & DUK_PROPDESC_FLAG_WRITABLE) && has_value) {
  51571. duk_tval *tmp1 = duk_require_tval(thr, -1); /* curr value */
  51572. duk_tval *tmp2 = duk_require_tval(thr, idx_value); /* new value */
  51573. if (!duk_js_samevalue(tmp1, tmp2)) {
  51574. goto fail_not_configurable;
  51575. }
  51576. }
  51577. }
  51578. }
  51579. } else {
  51580. /* IsGenericDescriptor(desc) == true; this means in practice that 'desc'
  51581. * only has [[Enumerable]] or [[Configurable]] flag updates, which are
  51582. * allowed at this point.
  51583. */
  51584. DUK_ASSERT(!has_value && !has_writable && !has_get && !has_set);
  51585. }
  51586. /*
  51587. * Start doing property attributes updates. Steps 12-13.
  51588. *
  51589. * Start by computing new attribute flags without writing yet.
  51590. * Property type conversion is done above if necessary.
  51591. */
  51592. new_flags = curr.flags;
  51593. if (has_enumerable) {
  51594. if (is_enumerable) {
  51595. new_flags |= DUK_PROPDESC_FLAG_ENUMERABLE;
  51596. } else {
  51597. new_flags &= ~DUK_PROPDESC_FLAG_ENUMERABLE;
  51598. }
  51599. }
  51600. if (has_configurable) {
  51601. if (is_configurable) {
  51602. new_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  51603. } else {
  51604. new_flags &= ~DUK_PROPDESC_FLAG_CONFIGURABLE;
  51605. }
  51606. }
  51607. if (has_writable) {
  51608. if (is_writable) {
  51609. new_flags |= DUK_PROPDESC_FLAG_WRITABLE;
  51610. } else {
  51611. new_flags &= ~DUK_PROPDESC_FLAG_WRITABLE;
  51612. }
  51613. }
  51614. /* XXX: write protect after flag? -> any chance of handling it here? */
  51615. DUK_DDD(DUK_DDDPRINT("new flags that we want to write: 0x%02lx",
  51616. (unsigned long) new_flags));
  51617. /*
  51618. * Check whether we need to abandon an array part (if it exists)
  51619. */
  51620. if (curr.a_idx >= 0) {
  51621. duk_bool_t rc;
  51622. DUK_ASSERT(curr.e_idx < 0);
  51623. if (new_flags == DUK_PROPDESC_FLAGS_WEC) {
  51624. duk_tval *tv1, *tv2;
  51625. DUK_DDD(DUK_DDDPRINT("array index, new property attributes match array defaults, update in-place"));
  51626. DUK_ASSERT(curr.flags == DUK_PROPDESC_FLAGS_WEC); /* must have been, since in array part */
  51627. DUK_ASSERT(!has_set);
  51628. DUK_ASSERT(!has_get);
  51629. DUK_ASSERT(idx_value >= 0); /* must be: if attributes match and we get here the value must differ (otherwise no change) */
  51630. tv2 = duk_require_tval(thr, idx_value);
  51631. tv1 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, curr.a_idx);
  51632. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects; may invalidate a_idx */
  51633. goto success_exotics;
  51634. }
  51635. DUK_DDD(DUK_DDDPRINT("array index, new property attributes do not match array defaults, abandon array and re-lookup"));
  51636. duk__abandon_array_checked(thr, obj);
  51637. duk_pop_unsafe(thr); /* remove old value */
  51638. rc = duk__get_own_propdesc_raw(thr, obj, key, arr_idx, &curr, DUK_GETDESC_FLAG_PUSH_VALUE);
  51639. DUK_UNREF(rc);
  51640. DUK_ASSERT(rc != 0);
  51641. DUK_ASSERT(curr.e_idx >= 0 && curr.a_idx < 0);
  51642. }
  51643. DUK_DDD(DUK_DDDPRINT("updating existing property in entry part"));
  51644. /* Array case is handled comprehensively above: either in entry
  51645. * part or a virtual property.
  51646. */
  51647. DUK_ASSERT(curr.a_idx < 0);
  51648. DUK_DDD(DUK_DDDPRINT("update existing property attributes"));
  51649. if (curr.e_idx >= 0) {
  51650. DUK_HOBJECT_E_SET_FLAGS(thr->heap, obj, curr.e_idx, new_flags);
  51651. } else {
  51652. /* For Array .length the only allowed transition is for .length
  51653. * to become non-writable.
  51654. */
  51655. if (key == DUK_HTHREAD_STRING_LENGTH(thr) && DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  51656. duk_harray *a;
  51657. a = (duk_harray *) obj;
  51658. DUK_DD(DUK_DDPRINT("Object.defineProperty() attribute update for duk_harray .length -> %02lx", (unsigned long) new_flags));
  51659. DUK_ASSERT_HARRAY_VALID(a);
  51660. if ((new_flags & DUK_PROPDESC_FLAGS_EC) != (curr.flags & DUK_PROPDESC_FLAGS_EC)) {
  51661. DUK_D(DUK_DPRINT("Object.defineProperty() attempt to change virtual array .length enumerable or configurable attribute, fail"));
  51662. goto fail_virtual;
  51663. }
  51664. if (new_flags & DUK_PROPDESC_FLAG_WRITABLE) {
  51665. DUK_HARRAY_SET_LENGTH_WRITABLE(a);
  51666. } else {
  51667. DUK_HARRAY_SET_LENGTH_NONWRITABLE(a);
  51668. }
  51669. }
  51670. }
  51671. if (has_set) {
  51672. duk_hobject *tmp;
  51673. /* Virtual properties are non-configurable but with a 'force'
  51674. * flag we might come here so check explicitly for virtual.
  51675. */
  51676. if (curr.e_idx < 0) {
  51677. goto fail_virtual;
  51678. }
  51679. DUK_DDD(DUK_DDDPRINT("update existing property setter"));
  51680. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  51681. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, obj, curr.e_idx);
  51682. DUK_UNREF(tmp);
  51683. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, obj, curr.e_idx, set);
  51684. DUK_HOBJECT_INCREF_ALLOWNULL(thr, set);
  51685. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects; may invalidate e_idx */
  51686. }
  51687. if (has_get) {
  51688. duk_hobject *tmp;
  51689. if (curr.e_idx < 0) {
  51690. goto fail_virtual;
  51691. }
  51692. DUK_DDD(DUK_DDDPRINT("update existing property getter"));
  51693. DUK_ASSERT(DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  51694. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, obj, curr.e_idx);
  51695. DUK_UNREF(tmp);
  51696. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, obj, curr.e_idx, get);
  51697. DUK_HOBJECT_INCREF_ALLOWNULL(thr, get);
  51698. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp); /* side effects; may invalidate e_idx */
  51699. }
  51700. if (has_value) {
  51701. duk_tval *tv1, *tv2;
  51702. DUK_DDD(DUK_DDDPRINT("update existing property value"));
  51703. if (curr.e_idx >= 0) {
  51704. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, obj, curr.e_idx));
  51705. tv2 = duk_require_tval(thr, idx_value);
  51706. tv1 = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, obj, curr.e_idx);
  51707. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects; may invalidate e_idx */
  51708. } else {
  51709. DUK_ASSERT(curr.a_idx < 0); /* array part case handled comprehensively previously */
  51710. DUK_DD(DUK_DDPRINT("Object.defineProperty(), value update for virtual property"));
  51711. /* XXX: Uint8Array and other typed array virtual writes not currently
  51712. * handled.
  51713. */
  51714. if (key == DUK_HTHREAD_STRING_LENGTH(thr) && DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  51715. duk_harray *a;
  51716. a = (duk_harray *) obj;
  51717. DUK_DD(DUK_DDPRINT("Object.defineProperty() value update for duk_harray .length -> %ld", (long) arrlen_new_len));
  51718. DUK_ASSERT_HARRAY_VALID(a);
  51719. a->length = arrlen_new_len;
  51720. } else {
  51721. goto fail_virtual; /* should not happen */
  51722. }
  51723. }
  51724. }
  51725. /*
  51726. * Standard algorithm succeeded without errors, check for exotic post-behaviors.
  51727. *
  51728. * Arguments exotic behavior in E5 Section 10.6 occurs after the standard
  51729. * [[DefineOwnProperty]] has completed successfully.
  51730. *
  51731. * Array exotic behavior in E5 Section 15.4.5.1 is implemented partly
  51732. * prior to the default [[DefineOwnProperty]], but:
  51733. * - for an array index key (e.g. "10") the final 'length' update occurs here
  51734. * - for 'length' key the element deletion and 'length' update occurs here
  51735. */
  51736. success_exotics:
  51737. /* curr.a_idx or curr.e_idx may have been invalidated by side effects
  51738. * above.
  51739. */
  51740. /* [obj key desc value get set curr_value] */
  51741. if (DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)) {
  51742. duk_harray *a;
  51743. a = (duk_harray *) obj;
  51744. DUK_ASSERT_HARRAY_VALID(a);
  51745. if (arridx_new_array_length > 0) {
  51746. /*
  51747. * Note: zero works as a "no update" marker because the new length
  51748. * can never be zero after a new property is written.
  51749. */
  51750. /* E5 Section 15.4.5.1, steps 4.e.i - 4.e.ii */
  51751. DUK_DDD(DUK_DDDPRINT("defineProperty successful, pending array length update to: %ld",
  51752. (long) arridx_new_array_length));
  51753. a->length = arridx_new_array_length;
  51754. }
  51755. if (key == DUK_HTHREAD_STRING_LENGTH(thr) && arrlen_new_len < arrlen_old_len) {
  51756. /*
  51757. * E5 Section 15.4.5.1, steps 3.k - 3.n. The order at the end combines
  51758. * the error case 3.l.iii and the success case 3.m-3.n.
  51759. */
  51760. /* XXX: investigate whether write protect can be handled above, if we
  51761. * just update length here while ignoring its protected status
  51762. */
  51763. duk_uint32_t result_len;
  51764. duk_bool_t rc;
  51765. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key is 'length', exotic array behavior, "
  51766. "doing array element deletion and length update"));
  51767. rc = duk__handle_put_array_length_smaller(thr, obj, arrlen_old_len, arrlen_new_len, force_flag, &result_len);
  51768. /* update length (curr points to length, and we assume it's still valid) */
  51769. DUK_ASSERT(result_len >= arrlen_new_len && result_len <= arrlen_old_len);
  51770. a->length = result_len;
  51771. if (pending_write_protect) {
  51772. DUK_DDD(DUK_DDDPRINT("setting array length non-writable (pending writability update)"));
  51773. DUK_HARRAY_SET_LENGTH_NONWRITABLE(a);
  51774. }
  51775. /* XXX: shrink array allocation or entries compaction here? */
  51776. if (!rc) {
  51777. DUK_DD(DUK_DDPRINT("array length write only partially successful"));
  51778. goto fail_not_configurable;
  51779. }
  51780. }
  51781. } else if (arr_idx != DUK__NO_ARRAY_INDEX && DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(obj)) {
  51782. duk_hobject *map;
  51783. duk_hobject *varenv;
  51784. DUK_ASSERT(arridx_new_array_length == 0);
  51785. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(obj)); /* traits are separate; in particular, arguments not an array */
  51786. map = NULL;
  51787. varenv = NULL;
  51788. if (!duk__lookup_arguments_map(thr, obj, key, &curr, &map, &varenv)) {
  51789. goto success_no_exotics;
  51790. }
  51791. DUK_ASSERT(map != NULL);
  51792. DUK_ASSERT(varenv != NULL);
  51793. /* [obj key desc value get set curr_value varname] */
  51794. if (has_set || has_get) {
  51795. /* = IsAccessorDescriptor(Desc) */
  51796. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map' "
  51797. "changed to an accessor, delete arguments binding"));
  51798. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  51799. } else {
  51800. /* Note: this order matters (final value before deleting map entry must be done) */
  51801. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  51802. "check for value update / binding deletion"));
  51803. if (has_value) {
  51804. duk_hstring *varname;
  51805. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  51806. "update bound value (variable/argument)"));
  51807. varname = duk_require_hstring(thr, -1);
  51808. DUK_ASSERT(varname != NULL);
  51809. DUK_DDD(DUK_DDDPRINT("arguments object automatic putvar for a bound variable; "
  51810. "key=%!O, varname=%!O, value=%!T",
  51811. (duk_heaphdr *) key,
  51812. (duk_heaphdr *) varname,
  51813. (duk_tval *) duk_require_tval(thr, idx_value)));
  51814. /* strict flag for putvar comes from our caller (currently: fixed) */
  51815. duk_js_putvar_envrec(thr, varenv, varname, duk_require_tval(thr, idx_value), 1 /*throw_flag*/);
  51816. }
  51817. if (has_writable && !is_writable) {
  51818. DUK_DDD(DUK_DDDPRINT("defineProperty successful, key mapped to arguments 'map', "
  51819. "changed to non-writable, delete arguments binding"));
  51820. (void) duk_hobject_delprop_raw(thr, map, key, 0); /* ignore result */
  51821. }
  51822. }
  51823. /* 'varname' is in stack in this else branch, leaving an unbalanced stack below,
  51824. * but this doesn't matter now.
  51825. */
  51826. }
  51827. success_no_exotics:
  51828. /* Some code paths use NORZ macros for simplicity, ensure refzero
  51829. * handling is completed.
  51830. */
  51831. DUK_REFZERO_CHECK_SLOW(thr);
  51832. return 1;
  51833. fail_not_extensible:
  51834. if (throw_flag) {
  51835. DUK_ERROR_TYPE(thr, DUK_STR_NOT_EXTENSIBLE);
  51836. DUK_WO_NORETURN(return 0;);
  51837. }
  51838. return 0;
  51839. fail_virtual: /* just use the same "not configurable" error message" */
  51840. fail_not_configurable:
  51841. if (throw_flag) {
  51842. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE);
  51843. DUK_WO_NORETURN(return 0;);
  51844. }
  51845. return 0;
  51846. }
  51847. /*
  51848. * Object.prototype.hasOwnProperty() and Object.prototype.propertyIsEnumerable().
  51849. */
  51850. DUK_INTERNAL duk_bool_t duk_hobject_object_ownprop_helper(duk_hthread *thr, duk_small_uint_t required_desc_flags) {
  51851. duk_hstring *h_v;
  51852. duk_hobject *h_obj;
  51853. duk_propdesc desc;
  51854. duk_bool_t ret;
  51855. /* coercion order matters */
  51856. h_v = duk_to_hstring_acceptsymbol(thr, 0);
  51857. DUK_ASSERT(h_v != NULL);
  51858. h_obj = duk_push_this_coercible_to_object(thr);
  51859. DUK_ASSERT(h_obj != NULL);
  51860. ret = duk_hobject_get_own_propdesc(thr, h_obj, h_v, &desc, 0 /*flags*/); /* don't push value */
  51861. duk_push_boolean(thr, ret && ((desc.flags & required_desc_flags) == required_desc_flags));
  51862. return 1;
  51863. }
  51864. /*
  51865. * Object.seal() and Object.freeze() (E5 Sections 15.2.3.8 and 15.2.3.9)
  51866. *
  51867. * Since the algorithms are similar, a helper provides both functions.
  51868. * Freezing is essentially sealing + making plain properties non-writable.
  51869. *
  51870. * Note: virtual (non-concrete) properties which are non-configurable but
  51871. * writable would pose some problems, but such properties do not currently
  51872. * exist (all virtual properties are non-configurable and non-writable).
  51873. * If they did exist, the non-configurability does NOT prevent them from
  51874. * becoming non-writable. However, this change should be recorded somehow
  51875. * so that it would turn up (e.g. when getting the property descriptor),
  51876. * requiring some additional flags in the object.
  51877. */
  51878. DUK_INTERNAL void duk_hobject_object_seal_freeze_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_freeze) {
  51879. duk_uint_fast32_t i;
  51880. DUK_ASSERT(thr != NULL);
  51881. DUK_ASSERT(thr->heap != NULL);
  51882. DUK_ASSERT(obj != NULL);
  51883. DUK_ASSERT_VALSTACK_SPACE(thr, DUK__VALSTACK_SPACE);
  51884. #if defined(DUK_USE_ROM_OBJECTS)
  51885. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) obj)) {
  51886. DUK_DD(DUK_DDPRINT("attempt to seal/freeze a readonly object, reject"));
  51887. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONFIGURABLE);
  51888. DUK_WO_NORETURN(return;);
  51889. }
  51890. #endif
  51891. /*
  51892. * Abandon array part because all properties must become non-configurable.
  51893. * Note that this is now done regardless of whether this is always the case
  51894. * (skips check, but performance problem if caller would do this many times
  51895. * for the same object; not likely).
  51896. */
  51897. duk__abandon_array_checked(thr, obj);
  51898. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(obj) == 0);
  51899. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  51900. duk_uint8_t *fp;
  51901. /* since duk__abandon_array_checked() causes a resize, there should be no gaps in keys */
  51902. DUK_ASSERT(DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i) != NULL);
  51903. /* avoid multiple computations of flags address; bypasses macros */
  51904. fp = DUK_HOBJECT_E_GET_FLAGS_PTR(thr->heap, obj, i);
  51905. if (is_freeze && !((*fp) & DUK_PROPDESC_FLAG_ACCESSOR)) {
  51906. *fp &= ~(DUK_PROPDESC_FLAG_WRITABLE | DUK_PROPDESC_FLAG_CONFIGURABLE);
  51907. } else {
  51908. *fp &= ~DUK_PROPDESC_FLAG_CONFIGURABLE;
  51909. }
  51910. }
  51911. DUK_HOBJECT_CLEAR_EXTENSIBLE(obj);
  51912. /* no need to compact since we already did that in duk__abandon_array_checked()
  51913. * (regardless of whether an array part existed or not.
  51914. */
  51915. return;
  51916. }
  51917. /*
  51918. * Object.isSealed() and Object.isFrozen() (E5 Sections 15.2.3.11, 15.2.3.13)
  51919. *
  51920. * Since the algorithms are similar, a helper provides both functions.
  51921. * Freezing is essentially sealing + making plain properties non-writable.
  51922. *
  51923. * Note: all virtual (non-concrete) properties are currently non-configurable
  51924. * and non-writable (and there are no accessor virtual properties), so they don't
  51925. * need to be considered here now.
  51926. */
  51927. DUK_INTERNAL duk_bool_t duk_hobject_object_is_sealed_frozen_helper(duk_hthread *thr, duk_hobject *obj, duk_bool_t is_frozen) {
  51928. duk_uint_fast32_t i;
  51929. DUK_ASSERT(obj != NULL);
  51930. DUK_UNREF(thr);
  51931. /* Note: no allocation pressure, no need to check refcounts etc */
  51932. /* must not be extensible */
  51933. if (DUK_HOBJECT_HAS_EXTENSIBLE(obj)) {
  51934. return 0;
  51935. }
  51936. /* all virtual properties are non-configurable and non-writable */
  51937. /* entry part must not contain any configurable properties, or
  51938. * writable properties (if is_frozen).
  51939. */
  51940. for (i = 0; i < DUK_HOBJECT_GET_ENEXT(obj); i++) {
  51941. duk_small_uint_t flags;
  51942. if (!DUK_HOBJECT_E_GET_KEY(thr->heap, obj, i)) {
  51943. continue;
  51944. }
  51945. /* avoid multiple computations of flags address; bypasses macros */
  51946. flags = (duk_small_uint_t) DUK_HOBJECT_E_GET_FLAGS(thr->heap, obj, i);
  51947. if (flags & DUK_PROPDESC_FLAG_CONFIGURABLE) {
  51948. return 0;
  51949. }
  51950. if (is_frozen &&
  51951. !(flags & DUK_PROPDESC_FLAG_ACCESSOR) &&
  51952. (flags & DUK_PROPDESC_FLAG_WRITABLE)) {
  51953. return 0;
  51954. }
  51955. }
  51956. /* array part must not contain any non-unused properties, as they would
  51957. * be configurable and writable.
  51958. */
  51959. for (i = 0; i < DUK_HOBJECT_GET_ASIZE(obj); i++) {
  51960. duk_tval *tv = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, obj, i);
  51961. if (!DUK_TVAL_IS_UNUSED(tv)) {
  51962. return 0;
  51963. }
  51964. }
  51965. return 1;
  51966. }
  51967. /*
  51968. * Object.preventExtensions() and Object.isExtensible() (E5 Sections 15.2.3.10, 15.2.3.13)
  51969. *
  51970. * Not needed, implemented by macros DUK_HOBJECT_{HAS,CLEAR,SET}_EXTENSIBLE
  51971. * and the Object built-in bindings.
  51972. */
  51973. /* automatic undefs */
  51974. #undef DUK__HASH_DELETED
  51975. #undef DUK__HASH_UNUSED
  51976. #undef DUK__NO_ARRAY_INDEX
  51977. #undef DUK__VALSTACK_PROXY_LOOKUP
  51978. #undef DUK__VALSTACK_SPACE
  51979. #line 1 "duk_hstring_misc.c"
  51980. /*
  51981. * Misc support functions
  51982. */
  51983. /* #include duk_internal.h -> already included */
  51984. /*
  51985. * duk_hstring charCodeAt, with and without surrogate awareness
  51986. */
  51987. DUK_INTERNAL duk_ucodepoint_t duk_hstring_char_code_at_raw(duk_hthread *thr, duk_hstring *h, duk_uint_t pos, duk_bool_t surrogate_aware) {
  51988. duk_uint32_t boff;
  51989. const duk_uint8_t *p, *p_start, *p_end;
  51990. duk_ucodepoint_t cp1;
  51991. duk_ucodepoint_t cp2;
  51992. /* Caller must check character offset to be inside the string. */
  51993. DUK_ASSERT(thr != NULL);
  51994. DUK_ASSERT(h != NULL);
  51995. DUK_ASSERT_DISABLE(pos >= 0); /* unsigned */
  51996. DUK_ASSERT(pos < (duk_uint_t) DUK_HSTRING_GET_CHARLEN(h));
  51997. boff = (duk_uint32_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint32_t) pos);
  51998. DUK_DDD(DUK_DDDPRINT("charCodeAt: pos=%ld -> boff=%ld, str=%!O",
  51999. (long) pos, (long) boff, (duk_heaphdr *) h));
  52000. DUK_ASSERT_DISABLE(boff >= 0);
  52001. DUK_ASSERT(boff < DUK_HSTRING_GET_BYTELEN(h));
  52002. p_start = DUK_HSTRING_GET_DATA(h);
  52003. p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
  52004. p = p_start + boff;
  52005. DUK_DDD(DUK_DDDPRINT("p_start=%p, p_end=%p, p=%p",
  52006. (const void *) p_start, (const void *) p_end,
  52007. (const void *) p));
  52008. /* For invalid UTF-8 (never happens for standard ECMAScript strings)
  52009. * return U+FFFD replacement character.
  52010. */
  52011. if (duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp1)) {
  52012. if (surrogate_aware && cp1 >= 0xd800UL && cp1 <= 0xdbffUL) {
  52013. /* The decode helper is memory safe even if 'cp1' was
  52014. * decoded at the end of the string and 'p' is no longer
  52015. * within string memory range.
  52016. */
  52017. cp2 = 0; /* If call fails, this is left untouched and won't match cp2 check. */
  52018. (void) duk_unicode_decode_xutf8(thr, &p, p_start, p_end, &cp2);
  52019. if (cp2 >= 0xdc00UL && cp2 <= 0xdfffUL) {
  52020. cp1 = (duk_ucodepoint_t) (((cp1 - 0xd800UL) << 10) + (cp2 - 0xdc00UL) + 0x10000UL);
  52021. }
  52022. }
  52023. } else {
  52024. cp1 = DUK_UNICODE_CP_REPLACEMENT_CHARACTER;
  52025. }
  52026. return cp1;
  52027. }
  52028. /*
  52029. * duk_hstring charlen, when lazy charlen disabled
  52030. */
  52031. #if !defined(DUK_USE_HSTRING_LAZY_CLEN)
  52032. #if !defined(DUK_USE_HSTRING_CLEN)
  52033. #error non-lazy duk_hstring charlen but DUK_USE_HSTRING_CLEN not set
  52034. #endif
  52035. DUK_INTERNAL void duk_hstring_init_charlen(duk_hstring *h) {
  52036. duk_uint32_t clen;
  52037. DUK_ASSERT(h != NULL);
  52038. DUK_ASSERT(!DUK_HSTRING_HAS_ASCII(h));
  52039. DUK_ASSERT(!DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h));
  52040. clen = duk_unicode_unvalidated_utf8_length(DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  52041. #if defined(DUK_USE_STRLEN16)
  52042. DUK_ASSERT(clen <= 0xffffUL); /* Bytelength checked during interning. */
  52043. h->clen16 = (duk_uint16_t) clen;
  52044. #else
  52045. h->clen = (duk_uint32_t) clen;
  52046. #endif
  52047. if (DUK_LIKELY(clen == DUK_HSTRING_GET_BYTELEN(h))) {
  52048. DUK_HSTRING_SET_ASCII(h);
  52049. }
  52050. }
  52051. DUK_INTERNAL DUK_HOT duk_size_t duk_hstring_get_charlen(duk_hstring *h) {
  52052. #if defined(DUK_USE_STRLEN16)
  52053. return h->clen16;
  52054. #else
  52055. return h->clen;
  52056. #endif
  52057. }
  52058. #endif /* !DUK_USE_HSTRING_LAZY_CLEN */
  52059. /*
  52060. * duk_hstring charlen, when lazy charlen enabled
  52061. */
  52062. #if defined(DUK_USE_HSTRING_LAZY_CLEN)
  52063. #if defined(DUK_USE_HSTRING_CLEN)
  52064. DUK_LOCAL DUK_COLD duk_size_t duk__hstring_get_charlen_slowpath(duk_hstring *h) {
  52065. duk_size_t res;
  52066. DUK_ASSERT(h->clen == 0); /* Checked by caller. */
  52067. #if defined(DUK_USE_ROM_STRINGS)
  52068. /* ROM strings have precomputed clen, but if the computed clen is zero
  52069. * we can still come here and can't write anything.
  52070. */
  52071. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h)) {
  52072. return 0;
  52073. }
  52074. #endif
  52075. res = duk_unicode_unvalidated_utf8_length(DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  52076. #if defined(DUK_USE_STRLEN16)
  52077. DUK_ASSERT(res <= 0xffffUL); /* Bytelength checked during interning. */
  52078. h->clen16 = (duk_uint16_t) res;
  52079. #else
  52080. h->clen = (duk_uint32_t) res;
  52081. #endif
  52082. if (DUK_LIKELY(res == DUK_HSTRING_GET_BYTELEN(h))) {
  52083. DUK_HSTRING_SET_ASCII(h);
  52084. }
  52085. return res;
  52086. }
  52087. #else /* DUK_USE_HSTRING_CLEN */
  52088. DUK_LOCAL duk_size_t duk__hstring_get_charlen_slowpath(duk_hstring *h) {
  52089. if (DUK_LIKELY(DUK_HSTRING_HAS_ASCII(h))) {
  52090. /* Most practical strings will go here. */
  52091. return DUK_HSTRING_GET_BYTELEN(h);
  52092. } else {
  52093. /* ASCII flag is lazy, so set it here. */
  52094. duk_size_t res;
  52095. /* XXX: here we could use the strcache to speed up the
  52096. * computation (matters for 'i < str.length' loops).
  52097. */
  52098. res = duk_unicode_unvalidated_utf8_length(DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
  52099. #if defined(DUK_USE_ROM_STRINGS)
  52100. if (DUK_HEAPHDR_HAS_READONLY((duk_heaphdr *) h)) {
  52101. /* For ROM strings, can't write anything; ASCII flag
  52102. * is preset so we don't need to update it.
  52103. */
  52104. return res;
  52105. }
  52106. #endif
  52107. if (DUK_LIKELY(res == DUK_HSTRING_GET_BYTELEN(h))) {
  52108. DUK_HSTRING_SET_ASCII(h);
  52109. }
  52110. return res;
  52111. }
  52112. }
  52113. #endif /* DUK_USE_HSTRING_CLEN */
  52114. #if defined(DUK_USE_HSTRING_CLEN)
  52115. DUK_INTERNAL DUK_HOT duk_size_t duk_hstring_get_charlen(duk_hstring *h) {
  52116. #if defined(DUK_USE_STRLEN16)
  52117. if (DUK_LIKELY(h->clen16 != 0)) {
  52118. return h->clen16;
  52119. }
  52120. #else
  52121. if (DUK_LIKELY(h->clen != 0)) {
  52122. return h->clen;
  52123. }
  52124. #endif
  52125. return duk__hstring_get_charlen_slowpath(h);
  52126. }
  52127. #else /* DUK_USE_HSTRING_CLEN */
  52128. DUK_INTERNAL DUK_HOT duk_size_t duk_hstring_get_charlen(duk_hstring *h) {
  52129. /* Always use slow path. */
  52130. return duk__hstring_get_charlen_slowpath(h);
  52131. }
  52132. #endif /* DUK_USE_HSTRING_CLEN */
  52133. #endif /* DUK_USE_HSTRING_LAZY_CLEN */
  52134. /*
  52135. * Compare duk_hstring to an ASCII cstring.
  52136. */
  52137. DUK_INTERNAL duk_bool_t duk_hstring_equals_ascii_cstring(duk_hstring *h, const char *cstr) {
  52138. duk_size_t len;
  52139. DUK_ASSERT(h != NULL);
  52140. DUK_ASSERT(cstr != NULL);
  52141. len = DUK_STRLEN(cstr);
  52142. if (len != DUK_HSTRING_GET_BYTELEN(h)) {
  52143. return 0;
  52144. }
  52145. if (duk_memcmp((const void *) cstr, (const void *) DUK_HSTRING_GET_DATA(h), len) == 0) {
  52146. return 1;
  52147. }
  52148. return 0;
  52149. }
  52150. #line 1 "duk_hthread_alloc.c"
  52151. /*
  52152. * duk_hthread allocation and freeing.
  52153. */
  52154. /* #include duk_internal.h -> already included */
  52155. /*
  52156. * Allocate initial stacks for a thread. Note that 'thr' must be reachable
  52157. * as a garbage collection may be triggered by the allocation attempts.
  52158. * Returns zero (without leaking memory) if init fails.
  52159. */
  52160. DUK_INTERNAL duk_bool_t duk_hthread_init_stacks(duk_heap *heap, duk_hthread *thr) {
  52161. duk_size_t alloc_size;
  52162. duk_size_t i;
  52163. DUK_ASSERT(heap != NULL);
  52164. DUK_ASSERT(thr != NULL);
  52165. DUK_ASSERT(thr->valstack == NULL);
  52166. DUK_ASSERT(thr->valstack_end == NULL);
  52167. DUK_ASSERT(thr->valstack_alloc_end == NULL);
  52168. DUK_ASSERT(thr->valstack_bottom == NULL);
  52169. DUK_ASSERT(thr->valstack_top == NULL);
  52170. DUK_ASSERT(thr->callstack_curr == NULL);
  52171. /* valstack */
  52172. DUK_ASSERT(DUK_VALSTACK_API_ENTRY_MINIMUM <= DUK_VALSTACK_INITIAL_SIZE);
  52173. alloc_size = sizeof(duk_tval) * DUK_VALSTACK_INITIAL_SIZE;
  52174. thr->valstack = (duk_tval *) DUK_ALLOC(heap, alloc_size);
  52175. if (!thr->valstack) {
  52176. goto fail;
  52177. }
  52178. duk_memzero(thr->valstack, alloc_size);
  52179. thr->valstack_end = thr->valstack + DUK_VALSTACK_API_ENTRY_MINIMUM;
  52180. thr->valstack_alloc_end = thr->valstack + DUK_VALSTACK_INITIAL_SIZE;
  52181. thr->valstack_bottom = thr->valstack;
  52182. thr->valstack_top = thr->valstack;
  52183. for (i = 0; i < DUK_VALSTACK_INITIAL_SIZE; i++) {
  52184. DUK_TVAL_SET_UNDEFINED(&thr->valstack[i]);
  52185. }
  52186. return 1;
  52187. fail:
  52188. DUK_FREE(heap, thr->valstack);
  52189. DUK_ASSERT(thr->callstack_curr == NULL);
  52190. thr->valstack = NULL;
  52191. return 0;
  52192. }
  52193. /* For indirect allocs. */
  52194. DUK_INTERNAL void *duk_hthread_get_valstack_ptr(duk_heap *heap, void *ud) {
  52195. duk_hthread *thr = (duk_hthread *) ud;
  52196. DUK_UNREF(heap);
  52197. return (void *) thr->valstack;
  52198. }
  52199. #line 1 "duk_hthread_builtins.c"
  52200. /*
  52201. * Initialize built-in objects. Current thread must have a valstack
  52202. * and initialization errors may longjmp, so a setjmp() catch point
  52203. * must exist.
  52204. */
  52205. /* #include duk_internal.h -> already included */
  52206. /*
  52207. * Encoding constants, must match genbuiltins.py
  52208. */
  52209. #define DUK__PROP_FLAGS_BITS 3
  52210. #define DUK__LENGTH_PROP_BITS 3
  52211. #define DUK__NARGS_BITS 3
  52212. #define DUK__PROP_TYPE_BITS 3
  52213. #define DUK__NARGS_VARARGS_MARKER 0x07
  52214. #define DUK__PROP_TYPE_DOUBLE 0
  52215. #define DUK__PROP_TYPE_STRING 1
  52216. #define DUK__PROP_TYPE_STRIDX 2
  52217. #define DUK__PROP_TYPE_BUILTIN 3
  52218. #define DUK__PROP_TYPE_UNDEFINED 4
  52219. #define DUK__PROP_TYPE_BOOLEAN_TRUE 5
  52220. #define DUK__PROP_TYPE_BOOLEAN_FALSE 6
  52221. #define DUK__PROP_TYPE_ACCESSOR 7
  52222. /*
  52223. * Create built-in objects by parsing an init bitstream generated
  52224. * by genbuiltins.py.
  52225. */
  52226. #if defined(DUK_USE_ROM_OBJECTS)
  52227. #if defined(DUK_USE_ROM_GLOBAL_CLONE) || defined(DUK_USE_ROM_GLOBAL_INHERIT)
  52228. DUK_LOCAL void duk__duplicate_ram_global_object(duk_hthread *thr) {
  52229. duk_hobject *h_global;
  52230. #if defined(DUK_USE_ROM_GLOBAL_CLONE)
  52231. duk_hobject *h_oldglobal;
  52232. duk_uint8_t *props;
  52233. duk_size_t alloc_size;
  52234. #endif
  52235. duk_hobject *h_objenv;
  52236. /* XXX: refactor into internal helper, duk_clone_hobject() */
  52237. #if defined(DUK_USE_ROM_GLOBAL_INHERIT)
  52238. /* Inherit from ROM-based global object: less RAM usage, less transparent. */
  52239. h_global = duk_push_object_helper(thr,
  52240. DUK_HOBJECT_FLAG_EXTENSIBLE |
  52241. DUK_HOBJECT_FLAG_FASTREFS |
  52242. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_GLOBAL),
  52243. DUK_BIDX_GLOBAL);
  52244. DUK_ASSERT(h_global != NULL);
  52245. #elif defined(DUK_USE_ROM_GLOBAL_CLONE)
  52246. /* Clone the properties of the ROM-based global object to create a
  52247. * fully RAM-based global object. Uses more memory than the inherit
  52248. * model but more compliant.
  52249. */
  52250. h_global = duk_push_object_helper(thr,
  52251. DUK_HOBJECT_FLAG_EXTENSIBLE |
  52252. DUK_HOBJECT_FLAG_FASTREFS |
  52253. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_GLOBAL),
  52254. DUK_BIDX_OBJECT_PROTOTYPE);
  52255. DUK_ASSERT(h_global != NULL);
  52256. h_oldglobal = thr->builtins[DUK_BIDX_GLOBAL];
  52257. DUK_ASSERT(h_oldglobal != NULL);
  52258. /* Copy the property table verbatim; this handles attributes etc.
  52259. * For ROM objects it's not necessary (or possible) to update
  52260. * refcounts so leave them as is.
  52261. */
  52262. alloc_size = DUK_HOBJECT_P_ALLOC_SIZE(h_oldglobal);
  52263. DUK_ASSERT(alloc_size > 0);
  52264. props = DUK_ALLOC_CHECKED(thr, alloc_size);
  52265. DUK_ASSERT(props != NULL);
  52266. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, h_oldglobal) != NULL);
  52267. duk_memcpy((void *) props, (const void *) DUK_HOBJECT_GET_PROPS(thr->heap, h_oldglobal), alloc_size);
  52268. /* XXX: keep property attributes or tweak them here?
  52269. * Properties will now be non-configurable even when they're
  52270. * normally configurable for the global object.
  52271. */
  52272. DUK_ASSERT(DUK_HOBJECT_GET_PROPS(thr->heap, h_global) == NULL);
  52273. DUK_HOBJECT_SET_PROPS(thr->heap, h_global, props);
  52274. DUK_HOBJECT_SET_ESIZE(h_global, DUK_HOBJECT_GET_ESIZE(h_oldglobal));
  52275. DUK_HOBJECT_SET_ENEXT(h_global, DUK_HOBJECT_GET_ENEXT(h_oldglobal));
  52276. DUK_HOBJECT_SET_ASIZE(h_global, DUK_HOBJECT_GET_ASIZE(h_oldglobal));
  52277. DUK_HOBJECT_SET_HSIZE(h_global, DUK_HOBJECT_GET_HSIZE(h_oldglobal));
  52278. #else
  52279. #error internal error in config defines
  52280. #endif
  52281. duk_hobject_compact_props(thr, h_global);
  52282. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL] != NULL);
  52283. DUK_ASSERT(!DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE((duk_heaphdr *) thr->builtins[DUK_BIDX_GLOBAL])); /* no need to decref: ROM object */
  52284. thr->builtins[DUK_BIDX_GLOBAL] = h_global;
  52285. DUK_HOBJECT_INCREF(thr, h_global);
  52286. DUK_D(DUK_DPRINT("duplicated global object: %!O", h_global));
  52287. /* Create a fresh object environment for the global scope. This is
  52288. * needed so that the global scope points to the newly created RAM-based
  52289. * global object.
  52290. */
  52291. h_objenv = (duk_hobject *) duk_hobjenv_alloc(thr,
  52292. DUK_HOBJECT_FLAG_EXTENSIBLE |
  52293. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV));
  52294. DUK_ASSERT(h_objenv != NULL);
  52295. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, h_objenv) == NULL);
  52296. duk_push_hobject(thr, h_objenv);
  52297. DUK_ASSERT(h_global != NULL);
  52298. ((duk_hobjenv *) h_objenv)->target = h_global;
  52299. DUK_HOBJECT_INCREF(thr, h_global);
  52300. DUK_ASSERT(((duk_hobjenv *) h_objenv)->has_this == 0);
  52301. DUK_ASSERT(thr->builtins[DUK_BIDX_GLOBAL_ENV] != NULL);
  52302. DUK_ASSERT(!DUK_HEAPHDR_NEEDS_REFCOUNT_UPDATE((duk_heaphdr *) thr->builtins[DUK_BIDX_GLOBAL_ENV])); /* no need to decref: ROM object */
  52303. thr->builtins[DUK_BIDX_GLOBAL_ENV] = h_objenv;
  52304. DUK_HOBJECT_INCREF(thr, h_objenv);
  52305. DUK_D(DUK_DPRINT("duplicated global env: %!O", h_objenv));
  52306. DUK_ASSERT_HOBJENV_VALID((duk_hobjenv *) h_objenv);
  52307. duk_pop_2(thr); /* Pop global object and global env. */
  52308. }
  52309. #endif /* DUK_USE_ROM_GLOBAL_CLONE || DUK_USE_ROM_GLOBAL_INHERIT */
  52310. DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
  52311. /* Setup builtins from ROM objects. All heaps/threads will share
  52312. * the same readonly objects.
  52313. */
  52314. duk_small_uint_t i;
  52315. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  52316. duk_hobject *h;
  52317. h = (duk_hobject *) DUK_LOSE_CONST(duk_rom_builtins_bidx[i]);
  52318. DUK_ASSERT(h != NULL);
  52319. thr->builtins[i] = h;
  52320. }
  52321. #if defined(DUK_USE_ROM_GLOBAL_CLONE) || defined(DUK_USE_ROM_GLOBAL_INHERIT)
  52322. /* By default the global object is read-only which is often much
  52323. * more of an issue than having read-only built-in objects (like
  52324. * RegExp, Date, etc). Use a RAM-based copy of the global object
  52325. * and the global environment object for convenience.
  52326. */
  52327. duk__duplicate_ram_global_object(thr);
  52328. #endif
  52329. }
  52330. #else /* DUK_USE_ROM_OBJECTS */
  52331. DUK_LOCAL void duk__push_stridx(duk_hthread *thr, duk_bitdecoder_ctx *bd) {
  52332. duk_small_uint_t n;
  52333. n = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52334. DUK_ASSERT_DISABLE(n >= 0); /* unsigned */
  52335. DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
  52336. duk_push_hstring_stridx(thr, n);
  52337. }
  52338. DUK_LOCAL void duk__push_string(duk_hthread *thr, duk_bitdecoder_ctx *bd) {
  52339. /* XXX: built-ins data could provide a maximum length that is
  52340. * actually needed; bitpacked max length is now 256 bytes.
  52341. */
  52342. duk_uint8_t tmp[DUK_BD_BITPACKED_STRING_MAXLEN];
  52343. duk_small_uint_t len;
  52344. len = duk_bd_decode_bitpacked_string(bd, tmp);
  52345. duk_push_lstring(thr, (const char *) tmp, (duk_size_t) len);
  52346. }
  52347. DUK_LOCAL void duk__push_stridx_or_string(duk_hthread *thr, duk_bitdecoder_ctx *bd) {
  52348. duk_small_uint_t n;
  52349. n = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52350. if (n == 0) {
  52351. duk__push_string(thr, bd);
  52352. } else {
  52353. n--;
  52354. DUK_ASSERT(n < DUK_HEAP_NUM_STRINGS);
  52355. duk_push_hstring_stridx(thr, n);
  52356. }
  52357. }
  52358. DUK_LOCAL void duk__push_double(duk_hthread *thr, duk_bitdecoder_ctx *bd) {
  52359. duk_double_union du;
  52360. duk_small_uint_t i;
  52361. for (i = 0; i < 8; i++) {
  52362. /* Encoding endianness must match target memory layout,
  52363. * build scripts and genbuiltins.py must ensure this.
  52364. */
  52365. du.uc[i] = (duk_uint8_t) duk_bd_decode(bd, 8);
  52366. }
  52367. duk_push_number(thr, du.d); /* push operation normalizes NaNs */
  52368. }
  52369. DUK_INTERNAL void duk_hthread_create_builtin_objects(duk_hthread *thr) {
  52370. duk_bitdecoder_ctx bd_ctx;
  52371. duk_bitdecoder_ctx *bd = &bd_ctx; /* convenience */
  52372. duk_hobject *h;
  52373. duk_small_uint_t i, j;
  52374. DUK_D(DUK_DPRINT("INITBUILTINS BEGIN: DUK_NUM_BUILTINS=%d, DUK_NUM_BUILTINS_ALL=%d", (int) DUK_NUM_BUILTINS, (int) DUK_NUM_ALL_BUILTINS));
  52375. duk_memzero(&bd_ctx, sizeof(bd_ctx));
  52376. bd->data = (const duk_uint8_t *) duk_builtins_data;
  52377. bd->length = (duk_size_t) DUK_BUILTINS_DATA_LENGTH;
  52378. /*
  52379. * First create all built-in bare objects on the empty valstack.
  52380. *
  52381. * Built-ins in the index range [0,DUK_NUM_BUILTINS-1] have value
  52382. * stack indices matching their eventual thr->builtins[] index.
  52383. *
  52384. * Built-ins in the index range [DUK_NUM_BUILTINS,DUK_NUM_ALL_BUILTINS]
  52385. * will exist on the value stack during init but won't be placed
  52386. * into thr->builtins[]. These are objects referenced in some way
  52387. * from thr->builtins[] roots but which don't need to be indexed by
  52388. * Duktape through thr->builtins[] (e.g. user custom objects).
  52389. *
  52390. * Internal prototypes will be incorrect (NULL) at this stage.
  52391. */
  52392. duk_require_stack(thr, DUK_NUM_ALL_BUILTINS);
  52393. DUK_DD(DUK_DDPRINT("create empty built-ins"));
  52394. DUK_ASSERT_TOP(thr, 0);
  52395. for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
  52396. duk_small_uint_t class_num;
  52397. duk_small_int_t len = -1; /* must be signed */
  52398. class_num = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52399. len = (duk_small_int_t) duk_bd_decode_flagged_signed(bd, DUK__LENGTH_PROP_BITS, (duk_int32_t) -1 /*def_value*/);
  52400. if (class_num == DUK_HOBJECT_CLASS_FUNCTION) {
  52401. duk_small_uint_t natidx;
  52402. duk_small_int_t c_nargs; /* must hold DUK_VARARGS */
  52403. duk_c_function c_func;
  52404. duk_int16_t magic;
  52405. DUK_DDD(DUK_DDDPRINT("len=%ld", (long) len));
  52406. DUK_ASSERT(len >= 0);
  52407. natidx = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52408. DUK_ASSERT(natidx != 0);
  52409. c_func = duk_bi_native_functions[natidx];
  52410. DUK_ASSERT(c_func != NULL);
  52411. c_nargs = (duk_small_int_t) duk_bd_decode_flagged_signed(bd, DUK__NARGS_BITS, len /*def_value*/);
  52412. if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
  52413. c_nargs = DUK_VARARGS;
  52414. }
  52415. /* XXX: set magic directly here? (it could share the c_nargs arg) */
  52416. (void) duk_push_c_function_builtin(thr, c_func, c_nargs);
  52417. h = duk_known_hobject(thr, -1);
  52418. /* Currently all built-in native functions are strict.
  52419. * duk_push_c_function() now sets strict flag, so
  52420. * assert for it.
  52421. */
  52422. DUK_ASSERT(DUK_HOBJECT_HAS_STRICT(h));
  52423. /* XXX: function properties */
  52424. duk__push_stridx_or_string(thr, bd);
  52425. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  52426. duk_xdef_prop_stridx_short(thr,
  52427. -2,
  52428. DUK_STRIDX_NAME,
  52429. DUK_PROPDESC_FLAGS_C);
  52430. #else
  52431. duk_pop(thr); /* Not very ideal but good enough for now. */
  52432. #endif
  52433. /* Almost all global level Function objects are constructable
  52434. * but not all: Function.prototype is a non-constructable,
  52435. * callable Function.
  52436. */
  52437. if (duk_bd_decode_flag(bd)) {
  52438. DUK_ASSERT(DUK_HOBJECT_HAS_CONSTRUCTABLE(h));
  52439. } else {
  52440. DUK_HOBJECT_CLEAR_CONSTRUCTABLE(h);
  52441. }
  52442. /* Cast converts magic to 16-bit signed value */
  52443. magic = (duk_int16_t) duk_bd_decode_varuint(bd);
  52444. ((duk_hnatfunc *) h)->magic = magic;
  52445. } else if (class_num == DUK_HOBJECT_CLASS_ARRAY) {
  52446. duk_push_array(thr);
  52447. } else if (class_num == DUK_HOBJECT_CLASS_OBJENV) {
  52448. duk_hobjenv *env;
  52449. duk_hobject *global;
  52450. DUK_ASSERT(i == DUK_BIDX_GLOBAL_ENV);
  52451. DUK_ASSERT(DUK_BIDX_GLOBAL_ENV > DUK_BIDX_GLOBAL);
  52452. env = duk_hobjenv_alloc(thr,
  52453. DUK_HOBJECT_FLAG_EXTENSIBLE |
  52454. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV));
  52455. DUK_ASSERT(env->target == NULL);
  52456. duk_push_hobject(thr, (duk_hobject *) env);
  52457. global = duk_known_hobject(thr, DUK_BIDX_GLOBAL);
  52458. DUK_ASSERT(global != NULL);
  52459. env->target = global;
  52460. DUK_HOBJECT_INCREF(thr, global);
  52461. DUK_ASSERT(env->has_this == 0);
  52462. DUK_ASSERT_HOBJENV_VALID(env);
  52463. } else {
  52464. DUK_ASSERT(class_num != DUK_HOBJECT_CLASS_DECENV);
  52465. (void) duk_push_object_helper(thr,
  52466. DUK_HOBJECT_FLAG_FASTREFS |
  52467. DUK_HOBJECT_FLAG_EXTENSIBLE,
  52468. -1); /* no prototype or class yet */
  52469. }
  52470. h = duk_known_hobject(thr, -1);
  52471. DUK_HOBJECT_SET_CLASS_NUMBER(h, class_num);
  52472. if (i < DUK_NUM_BUILTINS) {
  52473. thr->builtins[i] = h;
  52474. DUK_HOBJECT_INCREF(thr, &h->hdr);
  52475. }
  52476. if (len >= 0) {
  52477. /* In ES2015+ built-in function object .length property
  52478. * has property attributes C (configurable only):
  52479. * http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-standard-built-in-objects
  52480. *
  52481. * Array.prototype remains an Array instance in ES2015+
  52482. * and its length has attributes W (writable only).
  52483. * Because .length is now virtual for duk_harray, it is
  52484. * not encoded explicitly in init data.
  52485. */
  52486. DUK_ASSERT(class_num != DUK_HOBJECT_CLASS_ARRAY); /* .length is virtual */
  52487. duk_push_int(thr, len);
  52488. duk_xdef_prop_stridx_short(thr,
  52489. -2,
  52490. DUK_STRIDX_LENGTH,
  52491. DUK_PROPDESC_FLAGS_C);
  52492. }
  52493. /* enable exotic behaviors last */
  52494. if (class_num == DUK_HOBJECT_CLASS_ARRAY) {
  52495. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(h)); /* set by duk_push_array() */
  52496. }
  52497. if (class_num == DUK_HOBJECT_CLASS_STRING) {
  52498. DUK_HOBJECT_SET_EXOTIC_STRINGOBJ(h);
  52499. }
  52500. /* some assertions */
  52501. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h));
  52502. /* DUK_HOBJECT_FLAG_CONSTRUCTABLE varies */
  52503. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(h));
  52504. DUK_ASSERT(!DUK_HOBJECT_HAS_COMPFUNC(h));
  52505. /* DUK_HOBJECT_FLAG_NATFUNC varies */
  52506. DUK_ASSERT(!DUK_HOBJECT_IS_THREAD(h));
  52507. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(h) || class_num == DUK_HOBJECT_CLASS_ARRAY);
  52508. /* DUK_HOBJECT_FLAG_STRICT varies */
  52509. DUK_ASSERT(!DUK_HOBJECT_HAS_NATFUNC(h) || /* all native functions have NEWENV */
  52510. DUK_HOBJECT_HAS_NEWENV(h));
  52511. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(h));
  52512. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(h));
  52513. /* DUK_HOBJECT_FLAG_EXOTIC_ARRAY varies */
  52514. /* DUK_HOBJECT_FLAG_EXOTIC_STRINGOBJ varies */
  52515. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(h));
  52516. DUK_DDD(DUK_DDDPRINT("created built-in %ld, class=%ld, length=%ld", (long) i, (long) class_num, (long) len));
  52517. }
  52518. /*
  52519. * Then decode the builtins init data (see genbuiltins.py) to
  52520. * init objects. Internal prototypes are set at this stage,
  52521. * with thr->builtins[] populated.
  52522. */
  52523. DUK_DD(DUK_DDPRINT("initialize built-in object properties"));
  52524. for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
  52525. duk_small_uint_t t;
  52526. duk_small_uint_t num;
  52527. DUK_DDD(DUK_DDDPRINT("initializing built-in object at index %ld", (long) i));
  52528. h = duk_known_hobject(thr, (duk_idx_t) i);
  52529. t = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52530. if (t > 0) {
  52531. t--;
  52532. DUK_DDD(DUK_DDDPRINT("set internal prototype: built-in %ld", (long) t));
  52533. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, duk_known_hobject(thr, (duk_idx_t) t));
  52534. } else if (DUK_HOBJECT_IS_NATFUNC(h)) {
  52535. /* Standard native built-ins cannot inherit from
  52536. * %NativeFunctionPrototype%, they are required to
  52537. * inherit from Function.prototype directly.
  52538. */
  52539. DUK_ASSERT(thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE] != NULL);
  52540. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  52541. }
  52542. t = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52543. if (t > 0) {
  52544. /* 'prototype' property for all built-in objects (which have it) has attributes:
  52545. * [[Writable]] = false,
  52546. * [[Enumerable]] = false,
  52547. * [[Configurable]] = false
  52548. */
  52549. t--;
  52550. DUK_DDD(DUK_DDDPRINT("set external prototype: built-in %ld", (long) t));
  52551. duk_dup(thr, (duk_idx_t) t);
  52552. duk_xdef_prop_stridx(thr, (duk_idx_t) i, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_NONE);
  52553. }
  52554. t = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52555. if (t > 0) {
  52556. /* 'constructor' property for all built-in objects (which have it) has attributes:
  52557. * [[Writable]] = true,
  52558. * [[Enumerable]] = false,
  52559. * [[Configurable]] = true
  52560. */
  52561. t--;
  52562. DUK_DDD(DUK_DDDPRINT("set external constructor: built-in %ld", (long) t));
  52563. duk_dup(thr, (duk_idx_t) t);
  52564. duk_xdef_prop_stridx(thr, (duk_idx_t) i, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC);
  52565. }
  52566. /* normal valued properties */
  52567. num = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52568. DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld normal valued properties", (long) i, (long) num));
  52569. for (j = 0; j < num; j++) {
  52570. duk_small_uint_t defprop_flags;
  52571. duk__push_stridx_or_string(thr, bd);
  52572. /*
  52573. * Property attribute defaults are defined in E5 Section 15 (first
  52574. * few pages); there is a default for all properties and a special
  52575. * default for 'length' properties. Variation from the defaults is
  52576. * signaled using a single flag bit in the bitstream.
  52577. */
  52578. defprop_flags = (duk_small_uint_t) duk_bd_decode_flagged(bd,
  52579. DUK__PROP_FLAGS_BITS,
  52580. (duk_uint32_t) DUK_PROPDESC_FLAGS_WC);
  52581. defprop_flags |= DUK_DEFPROP_FORCE |
  52582. DUK_DEFPROP_HAVE_VALUE |
  52583. DUK_DEFPROP_HAVE_WRITABLE |
  52584. DUK_DEFPROP_HAVE_ENUMERABLE |
  52585. DUK_DEFPROP_HAVE_CONFIGURABLE; /* Defaults for data properties. */
  52586. /* The writable, enumerable, configurable flags in prop_flags
  52587. * match both duk_def_prop() and internal property flags.
  52588. */
  52589. DUK_ASSERT(DUK_PROPDESC_FLAG_WRITABLE == DUK_DEFPROP_WRITABLE);
  52590. DUK_ASSERT(DUK_PROPDESC_FLAG_ENUMERABLE == DUK_DEFPROP_ENUMERABLE);
  52591. DUK_ASSERT(DUK_PROPDESC_FLAG_CONFIGURABLE == DUK_DEFPROP_CONFIGURABLE);
  52592. t = (duk_small_uint_t) duk_bd_decode(bd, DUK__PROP_TYPE_BITS);
  52593. DUK_DDD(DUK_DDDPRINT("built-in %ld, normal-valued property %ld, key %!T, flags 0x%02lx, type %ld",
  52594. (long) i, (long) j, duk_get_tval(thr, -1), (unsigned long) defprop_flags, (long) t));
  52595. switch (t) {
  52596. case DUK__PROP_TYPE_DOUBLE: {
  52597. duk__push_double(thr, bd);
  52598. break;
  52599. }
  52600. case DUK__PROP_TYPE_STRING: {
  52601. duk__push_string(thr, bd);
  52602. break;
  52603. }
  52604. case DUK__PROP_TYPE_STRIDX: {
  52605. duk__push_stridx(thr, bd);
  52606. break;
  52607. }
  52608. case DUK__PROP_TYPE_BUILTIN: {
  52609. duk_small_uint_t bidx;
  52610. bidx = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52611. duk_dup(thr, (duk_idx_t) bidx);
  52612. break;
  52613. }
  52614. case DUK__PROP_TYPE_UNDEFINED: {
  52615. duk_push_undefined(thr);
  52616. break;
  52617. }
  52618. case DUK__PROP_TYPE_BOOLEAN_TRUE: {
  52619. duk_push_true(thr);
  52620. break;
  52621. }
  52622. case DUK__PROP_TYPE_BOOLEAN_FALSE: {
  52623. duk_push_false(thr);
  52624. break;
  52625. }
  52626. case DUK__PROP_TYPE_ACCESSOR: {
  52627. duk_small_uint_t natidx_getter = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52628. duk_small_uint_t natidx_setter = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52629. duk_small_uint_t accessor_magic = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52630. duk_c_function c_func_getter;
  52631. duk_c_function c_func_setter;
  52632. DUK_DDD(DUK_DDDPRINT("built-in accessor property: objidx=%ld, key=%!T, getteridx=%ld, setteridx=%ld, flags=0x%04lx",
  52633. (long) i, duk_get_tval(thr, -1), (long) natidx_getter, (long) natidx_setter, (unsigned long) defprop_flags));
  52634. c_func_getter = duk_bi_native_functions[natidx_getter];
  52635. if (c_func_getter != NULL) {
  52636. duk_push_c_function_builtin_noconstruct(thr, c_func_getter, 0); /* always 0 args */
  52637. duk_set_magic(thr, -1, (duk_int_t) accessor_magic);
  52638. defprop_flags |= DUK_DEFPROP_HAVE_GETTER;
  52639. }
  52640. c_func_setter = duk_bi_native_functions[natidx_setter];
  52641. if (c_func_setter != NULL) {
  52642. duk_push_c_function_builtin_noconstruct(thr, c_func_setter, 1); /* always 1 arg */
  52643. duk_set_magic(thr, -1, (duk_int_t) accessor_magic);
  52644. defprop_flags |= DUK_DEFPROP_HAVE_SETTER;
  52645. }
  52646. /* Writable flag doesn't make sense for an accessor. */
  52647. DUK_ASSERT((defprop_flags & DUK_PROPDESC_FLAG_WRITABLE) == 0); /* genbuiltins.py ensures */
  52648. defprop_flags &= ~(DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE);
  52649. defprop_flags |= DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_HAVE_CONFIGURABLE;
  52650. break;
  52651. }
  52652. default: {
  52653. /* exhaustive */
  52654. DUK_UNREACHABLE();
  52655. }
  52656. }
  52657. duk_def_prop(thr, (duk_idx_t) i, defprop_flags);
  52658. DUK_ASSERT_TOP(thr, DUK_NUM_ALL_BUILTINS);
  52659. }
  52660. /* native function properties */
  52661. num = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52662. DUK_DDD(DUK_DDDPRINT("built-in object %ld, %ld function valued properties", (long) i, (long) num));
  52663. for (j = 0; j < num; j++) {
  52664. duk_hstring *h_key;
  52665. duk_small_uint_t natidx;
  52666. duk_int_t c_nargs; /* must hold DUK_VARARGS */
  52667. duk_small_uint_t c_length;
  52668. duk_int16_t magic;
  52669. duk_c_function c_func;
  52670. duk_hnatfunc *h_func;
  52671. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  52672. duk_small_int_t lightfunc_eligible;
  52673. #endif
  52674. duk_small_uint_t defprop_flags;
  52675. duk__push_stridx_or_string(thr, bd);
  52676. h_key = duk_known_hstring(thr, -1);
  52677. DUK_UNREF(h_key);
  52678. natidx = (duk_small_uint_t) duk_bd_decode_varuint(bd);
  52679. c_length = (duk_small_uint_t) duk_bd_decode(bd, DUK__LENGTH_PROP_BITS);
  52680. c_nargs = (duk_int_t) duk_bd_decode_flagged(bd, DUK__NARGS_BITS, (duk_uint32_t) c_length /*def_value*/);
  52681. if (c_nargs == DUK__NARGS_VARARGS_MARKER) {
  52682. c_nargs = DUK_VARARGS;
  52683. }
  52684. c_func = duk_bi_native_functions[natidx];
  52685. DUK_DDD(DUK_DDDPRINT("built-in %ld, function-valued property %ld, key %!O, natidx %ld, length %ld, nargs %ld",
  52686. (long) i, (long) j, (duk_heaphdr *) h_key, (long) natidx, (long) c_length,
  52687. (c_nargs == DUK_VARARGS ? (long) -1 : (long) c_nargs)));
  52688. /* Cast converts magic to 16-bit signed value */
  52689. magic = (duk_int16_t) duk_bd_decode_varuint(bd);
  52690. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  52691. lightfunc_eligible =
  52692. ((c_nargs >= DUK_LFUNC_NARGS_MIN && c_nargs <= DUK_LFUNC_NARGS_MAX) || (c_nargs == DUK_VARARGS)) &&
  52693. (c_length <= DUK_LFUNC_LENGTH_MAX) &&
  52694. (magic >= DUK_LFUNC_MAGIC_MIN && magic <= DUK_LFUNC_MAGIC_MAX);
  52695. /* These functions have trouble working as lightfuncs.
  52696. * Some of them have specific asserts and some may have
  52697. * additional properties (e.g. 'require.id' may be written).
  52698. */
  52699. if (c_func == duk_bi_global_object_eval) {
  52700. lightfunc_eligible = 0;
  52701. }
  52702. #if defined(DUK_USE_COROUTINE_SUPPORT)
  52703. if (c_func == duk_bi_thread_yield ||
  52704. c_func == duk_bi_thread_resume) {
  52705. lightfunc_eligible = 0;
  52706. }
  52707. #endif
  52708. if (c_func == duk_bi_function_prototype_call ||
  52709. c_func == duk_bi_function_prototype_apply ||
  52710. c_func == duk_bi_reflect_apply ||
  52711. c_func == duk_bi_reflect_construct) {
  52712. lightfunc_eligible = 0;
  52713. }
  52714. if (lightfunc_eligible) {
  52715. duk_tval tv_lfunc;
  52716. duk_small_uint_t lf_nargs = (duk_small_uint_t) (c_nargs == DUK_VARARGS ? DUK_LFUNC_NARGS_VARARGS : c_nargs);
  52717. duk_small_uint_t lf_flags = DUK_LFUNC_FLAGS_PACK(magic, c_length, lf_nargs);
  52718. DUK_TVAL_SET_LIGHTFUNC(&tv_lfunc, c_func, lf_flags);
  52719. duk_push_tval(thr, &tv_lfunc);
  52720. DUK_D(DUK_DPRINT("built-in function eligible as light function: i=%d, j=%d c_length=%ld, c_nargs=%ld, magic=%ld -> %!iT", (int) i, (int) j, (long) c_length, (long) c_nargs, (long) magic, duk_get_tval(thr, -1)));
  52721. goto lightfunc_skip;
  52722. }
  52723. DUK_D(DUK_DPRINT("built-in function NOT ELIGIBLE as light function: i=%d, j=%d c_length=%ld, c_nargs=%ld, magic=%ld", (int) i, (int) j, (long) c_length, (long) c_nargs, (long) magic));
  52724. #endif /* DUK_USE_LIGHTFUNC_BUILTINS */
  52725. /* [ (builtin objects) name ] */
  52726. duk_push_c_function_builtin_noconstruct(thr, c_func, c_nargs);
  52727. h_func = duk_known_hnatfunc(thr, -1);
  52728. DUK_UNREF(h_func);
  52729. /* XXX: add into init data? */
  52730. /* Special call handling, not described in init data. */
  52731. if (c_func == duk_bi_global_object_eval ||
  52732. c_func == duk_bi_function_prototype_call ||
  52733. c_func == duk_bi_function_prototype_apply ||
  52734. c_func == duk_bi_reflect_apply ||
  52735. c_func == duk_bi_reflect_construct) {
  52736. DUK_HOBJECT_SET_SPECIAL_CALL((duk_hobject *) h_func);
  52737. }
  52738. /* Currently all built-in native functions are strict.
  52739. * This doesn't matter for many functions, but e.g.
  52740. * String.prototype.charAt (and other string functions)
  52741. * rely on being strict so that their 'this' binding is
  52742. * not automatically coerced.
  52743. */
  52744. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_func);
  52745. /* No built-in functions are constructable except the top
  52746. * level ones (Number, etc).
  52747. */
  52748. DUK_ASSERT(!DUK_HOBJECT_HAS_CONSTRUCTABLE((duk_hobject *) h_func));
  52749. /* XXX: any way to avoid decoding magic bit; there are quite
  52750. * many function properties and relatively few with magic values.
  52751. */
  52752. h_func->magic = magic;
  52753. /* [ (builtin objects) name func ] */
  52754. duk_push_uint(thr, c_length);
  52755. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_C);
  52756. duk_dup_m2(thr);
  52757. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C);
  52758. /* XXX: other properties of function instances; 'arguments', 'caller'. */
  52759. DUK_DD(DUK_DDPRINT("built-in object %ld, function property %ld -> %!T",
  52760. (long) i, (long) j, (duk_tval *) duk_get_tval(thr, -1)));
  52761. /* [ (builtin objects) name func ] */
  52762. /*
  52763. * The default property attributes are correct for all
  52764. * function valued properties of built-in objects now.
  52765. */
  52766. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  52767. lightfunc_skip:
  52768. #endif
  52769. defprop_flags = (duk_small_uint_t) duk_bd_decode_flagged(bd,
  52770. DUK__PROP_FLAGS_BITS,
  52771. (duk_uint32_t) DUK_PROPDESC_FLAGS_WC);
  52772. defprop_flags |= DUK_DEFPROP_FORCE |
  52773. DUK_DEFPROP_HAVE_VALUE |
  52774. DUK_DEFPROP_HAVE_WRITABLE |
  52775. DUK_DEFPROP_HAVE_ENUMERABLE |
  52776. DUK_DEFPROP_HAVE_CONFIGURABLE;
  52777. DUK_ASSERT(DUK_PROPDESC_FLAG_WRITABLE == DUK_DEFPROP_WRITABLE);
  52778. DUK_ASSERT(DUK_PROPDESC_FLAG_ENUMERABLE == DUK_DEFPROP_ENUMERABLE);
  52779. DUK_ASSERT(DUK_PROPDESC_FLAG_CONFIGURABLE == DUK_DEFPROP_CONFIGURABLE);
  52780. duk_def_prop(thr, (duk_idx_t) i, defprop_flags);
  52781. /* [ (builtin objects) ] */
  52782. }
  52783. }
  52784. /*
  52785. * Special post-tweaks, for cases not covered by the init data format.
  52786. *
  52787. * - Set Date.prototype.toGMTString to Date.prototype.toUTCString.
  52788. * toGMTString is required to have the same Function object as
  52789. * toUTCString in E5 Section B.2.6. Note that while Smjs respects
  52790. * this, V8 does not (the Function objects are distinct).
  52791. *
  52792. * - Make DoubleError non-extensible.
  52793. *
  52794. * - Add info about most important effective compile options to Duktape.
  52795. *
  52796. * - Possibly remove some properties (values or methods) which are not
  52797. * desirable with current feature options but are not currently
  52798. * conditional in init data.
  52799. */
  52800. #if defined(DUK_USE_DATE_BUILTIN)
  52801. duk_get_prop_stridx_short(thr, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_UTC_STRING);
  52802. duk_xdef_prop_stridx_short(thr, DUK_BIDX_DATE_PROTOTYPE, DUK_STRIDX_TO_GMT_STRING, DUK_PROPDESC_FLAGS_WC);
  52803. #endif
  52804. h = duk_known_hobject(thr, DUK_BIDX_DOUBLE_ERROR);
  52805. DUK_HOBJECT_CLEAR_EXTENSIBLE(h);
  52806. #if !defined(DUK_USE_ES6_OBJECT_PROTO_PROPERTY)
  52807. DUK_DD(DUK_DDPRINT("delete Object.prototype.__proto__ built-in which is not enabled in features"));
  52808. (void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_PROTOTYPE], DUK_HTHREAD_STRING___PROTO__(thr), DUK_DELPROP_FLAG_THROW);
  52809. #endif
  52810. #if !defined(DUK_USE_ES6_OBJECT_SETPROTOTYPEOF)
  52811. DUK_DD(DUK_DDPRINT("delete Object.setPrototypeOf built-in which is not enabled in features"));
  52812. (void) duk_hobject_delprop_raw(thr, thr->builtins[DUK_BIDX_OBJECT_CONSTRUCTOR], DUK_HTHREAD_STRING_SET_PROTOTYPE_OF(thr), DUK_DELPROP_FLAG_THROW);
  52813. #endif
  52814. /* XXX: relocate */
  52815. duk_push_string(thr,
  52816. /* Endianness indicator */
  52817. #if defined(DUK_USE_INTEGER_LE)
  52818. "l"
  52819. #elif defined(DUK_USE_INTEGER_BE)
  52820. "b"
  52821. #elif defined(DUK_USE_INTEGER_ME) /* integer mixed endian not really used now */
  52822. "m"
  52823. #else
  52824. "?"
  52825. #endif
  52826. #if defined(DUK_USE_DOUBLE_LE)
  52827. "l"
  52828. #elif defined(DUK_USE_DOUBLE_BE)
  52829. "b"
  52830. #elif defined(DUK_USE_DOUBLE_ME)
  52831. "m"
  52832. #else
  52833. "?"
  52834. #endif
  52835. " "
  52836. /* Packed or unpacked tval */
  52837. #if defined(DUK_USE_PACKED_TVAL)
  52838. "p"
  52839. #else
  52840. "u"
  52841. #endif
  52842. #if defined(DUK_USE_FASTINT)
  52843. "f"
  52844. #endif
  52845. " "
  52846. /* Low memory/performance options */
  52847. #if defined(DUK_USE_STRTAB_PTRCOMP)
  52848. "s"
  52849. #endif
  52850. #if !defined(DUK_USE_HEAPPTR16) && !defined(DUK_DATAPTR16) && !defined(DUK_FUNCPTR16)
  52851. "n"
  52852. #endif
  52853. #if defined(DUK_USE_HEAPPTR16)
  52854. "h"
  52855. #endif
  52856. #if defined(DUK_USE_DATAPTR16)
  52857. "d"
  52858. #endif
  52859. #if defined(DUK_USE_FUNCPTR16)
  52860. "f"
  52861. #endif
  52862. #if defined(DUK_USE_REFCOUNT16)
  52863. "R"
  52864. #endif
  52865. #if defined(DUK_USE_STRHASH16)
  52866. "H"
  52867. #endif
  52868. #if defined(DUK_USE_STRLEN16)
  52869. "S"
  52870. #endif
  52871. #if defined(DUK_USE_BUFLEN16)
  52872. "B"
  52873. #endif
  52874. #if defined(DUK_USE_OBJSIZES16)
  52875. "O"
  52876. #endif
  52877. #if defined(DUK_USE_LIGHTFUNC_BUILTINS)
  52878. "L"
  52879. #endif
  52880. #if defined(DUK_USE_ROM_STRINGS) || defined(DUK_USE_ROM_OBJECTS)
  52881. /* XXX: This won't be shown in practice now
  52882. * because this code is not run when builtins
  52883. * are in ROM.
  52884. */
  52885. "Z"
  52886. #endif
  52887. #if defined(DUK_USE_LITCACHE_SIZE)
  52888. "l"
  52889. #endif
  52890. " "
  52891. /* Object property allocation layout */
  52892. #if defined(DUK_USE_HOBJECT_LAYOUT_1)
  52893. "p1"
  52894. #elif defined(DUK_USE_HOBJECT_LAYOUT_2)
  52895. "p2"
  52896. #elif defined(DUK_USE_HOBJECT_LAYOUT_3)
  52897. "p3"
  52898. #else
  52899. "p?"
  52900. #endif
  52901. " "
  52902. /* Alignment guarantee */
  52903. #if (DUK_USE_ALIGN_BY == 4)
  52904. "a4"
  52905. #elif (DUK_USE_ALIGN_BY == 8)
  52906. "a8"
  52907. #elif (DUK_USE_ALIGN_BY == 1)
  52908. "a1"
  52909. #else
  52910. #error invalid DUK_USE_ALIGN_BY
  52911. #endif
  52912. " "
  52913. /* Architecture, OS, and compiler strings */
  52914. DUK_USE_ARCH_STRING
  52915. " "
  52916. DUK_USE_OS_STRING
  52917. " "
  52918. DUK_USE_COMPILER_STRING);
  52919. duk_xdef_prop_stridx_short(thr, DUK_BIDX_DUKTAPE, DUK_STRIDX_ENV, DUK_PROPDESC_FLAGS_WC);
  52920. /*
  52921. * Since built-ins are not often extended, compact them.
  52922. */
  52923. DUK_DD(DUK_DDPRINT("compact built-ins"));
  52924. for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
  52925. duk_hobject_compact_props(thr, duk_known_hobject(thr, (duk_idx_t) i));
  52926. }
  52927. DUK_D(DUK_DPRINT("INITBUILTINS END"));
  52928. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 1)
  52929. for (i = 0; i < DUK_NUM_ALL_BUILTINS; i++) {
  52930. DUK_DD(DUK_DDPRINT("built-in object %ld after initialization and compacting: %!@iO",
  52931. (long) i, (duk_heaphdr *) duk_require_hobject(thr, i)));
  52932. }
  52933. #endif
  52934. /*
  52935. * Pop built-ins from stack: they are now INCREF'd and
  52936. * reachable from the builtins[] array or indirectly
  52937. * through builtins[].
  52938. */
  52939. duk_set_top(thr, 0);
  52940. DUK_ASSERT_TOP(thr, 0);
  52941. }
  52942. #endif /* DUK_USE_ROM_OBJECTS */
  52943. DUK_INTERNAL void duk_hthread_copy_builtin_objects(duk_hthread *thr_from, duk_hthread *thr_to) {
  52944. duk_small_uint_t i;
  52945. for (i = 0; i < DUK_NUM_BUILTINS; i++) {
  52946. thr_to->builtins[i] = thr_from->builtins[i];
  52947. DUK_HOBJECT_INCREF_ALLOWNULL(thr_to, thr_to->builtins[i]); /* side effect free */
  52948. }
  52949. }
  52950. /* automatic undefs */
  52951. #undef DUK__LENGTH_PROP_BITS
  52952. #undef DUK__NARGS_BITS
  52953. #undef DUK__NARGS_VARARGS_MARKER
  52954. #undef DUK__PROP_FLAGS_BITS
  52955. #undef DUK__PROP_TYPE_ACCESSOR
  52956. #undef DUK__PROP_TYPE_BITS
  52957. #undef DUK__PROP_TYPE_BOOLEAN_FALSE
  52958. #undef DUK__PROP_TYPE_BOOLEAN_TRUE
  52959. #undef DUK__PROP_TYPE_BUILTIN
  52960. #undef DUK__PROP_TYPE_DOUBLE
  52961. #undef DUK__PROP_TYPE_STRIDX
  52962. #undef DUK__PROP_TYPE_STRING
  52963. #undef DUK__PROP_TYPE_UNDEFINED
  52964. #line 1 "duk_hthread_misc.c"
  52965. /*
  52966. * Thread support.
  52967. */
  52968. /* #include duk_internal.h -> already included */
  52969. DUK_INTERNAL void duk_hthread_terminate(duk_hthread *thr) {
  52970. DUK_ASSERT(thr != NULL);
  52971. while (thr->callstack_curr != NULL) {
  52972. duk_hthread_activation_unwind_norz(thr);
  52973. }
  52974. thr->valstack_bottom = thr->valstack;
  52975. duk_set_top(thr, 0); /* unwinds valstack, updating refcounts */
  52976. thr->state = DUK_HTHREAD_STATE_TERMINATED;
  52977. /* Here we could remove references to built-ins, but it may not be
  52978. * worth the effort because built-ins are quite likely to be shared
  52979. * with another (unterminated) thread, and terminated threads are also
  52980. * usually garbage collected quite quickly.
  52981. *
  52982. * We could also shrink the value stack here, but that also may not
  52983. * be worth the effort for the same reason.
  52984. */
  52985. DUK_REFZERO_CHECK_SLOW(thr);
  52986. }
  52987. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  52988. DUK_INTERNAL duk_uint_fast32_t duk_hthread_get_act_curr_pc(duk_hthread *thr, duk_activation *act) {
  52989. duk_instr_t *bcode;
  52990. DUK_ASSERT(thr != NULL);
  52991. DUK_ASSERT(act != NULL);
  52992. DUK_UNREF(thr);
  52993. /* XXX: store 'bcode' pointer to activation for faster lookup? */
  52994. if (act->func && DUK_HOBJECT_IS_COMPFUNC(act->func)) {
  52995. bcode = DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, (duk_hcompfunc *) (act->func));
  52996. return (duk_uint_fast32_t) (act->curr_pc - bcode);
  52997. }
  52998. return 0;
  52999. }
  53000. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  53001. DUK_INTERNAL duk_uint_fast32_t duk_hthread_get_act_prev_pc(duk_hthread *thr, duk_activation *act) {
  53002. duk_instr_t *bcode;
  53003. duk_uint_fast32_t ret;
  53004. DUK_ASSERT(thr != NULL);
  53005. DUK_ASSERT(act != NULL);
  53006. DUK_UNREF(thr);
  53007. if (act->func && DUK_HOBJECT_IS_COMPFUNC(act->func)) {
  53008. bcode = DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, (duk_hcompfunc *) (act->func));
  53009. ret = (duk_uint_fast32_t) (act->curr_pc - bcode);
  53010. if (ret > 0) {
  53011. ret--;
  53012. }
  53013. return ret;
  53014. }
  53015. return 0;
  53016. }
  53017. /* Write bytecode executor's curr_pc back to topmost activation (if any). */
  53018. DUK_INTERNAL void duk_hthread_sync_currpc(duk_hthread *thr) {
  53019. duk_activation *act;
  53020. DUK_ASSERT(thr != NULL);
  53021. if (thr->ptr_curr_pc != NULL) {
  53022. /* ptr_curr_pc != NULL only when bytecode executor is active. */
  53023. DUK_ASSERT(thr->callstack_top > 0);
  53024. DUK_ASSERT(thr->callstack_curr != NULL);
  53025. act = thr->callstack_curr;
  53026. DUK_ASSERT(act != NULL);
  53027. act->curr_pc = *thr->ptr_curr_pc;
  53028. }
  53029. }
  53030. DUK_INTERNAL void duk_hthread_sync_and_null_currpc(duk_hthread *thr) {
  53031. duk_activation *act;
  53032. DUK_ASSERT(thr != NULL);
  53033. if (thr->ptr_curr_pc != NULL) {
  53034. /* ptr_curr_pc != NULL only when bytecode executor is active. */
  53035. DUK_ASSERT(thr->callstack_top > 0);
  53036. DUK_ASSERT(thr->callstack_curr != NULL);
  53037. act = thr->callstack_curr;
  53038. DUK_ASSERT(act != NULL);
  53039. act->curr_pc = *thr->ptr_curr_pc;
  53040. thr->ptr_curr_pc = NULL;
  53041. }
  53042. }
  53043. #line 1 "duk_hthread_stacks.c"
  53044. /*
  53045. * Thread stack (mainly call stack) primitives: allocation of activations,
  53046. * unwinding catchers and activations, etc.
  53047. *
  53048. * Value stack handling is a part of the API implementation.
  53049. */
  53050. /* #include duk_internal.h -> already included */
  53051. /* Unwind the topmost catcher of the current activation (caller must check that
  53052. * both exist) without side effects.
  53053. */
  53054. DUK_INTERNAL void duk_hthread_catcher_unwind_norz(duk_hthread *thr, duk_activation *act) {
  53055. duk_catcher *cat;
  53056. DUK_ASSERT(thr != NULL);
  53057. DUK_ASSERT(act != NULL);
  53058. DUK_ASSERT(act->cat != NULL); /* caller must check */
  53059. cat = act->cat;
  53060. DUK_ASSERT(cat != NULL);
  53061. DUK_DDD(DUK_DDDPRINT("unwinding catch stack entry %p (lexenv check is done)", (void *) cat));
  53062. if (DUK_CAT_HAS_LEXENV_ACTIVE(cat)) {
  53063. duk_hobject *env;
  53064. env = act->lex_env; /* current lex_env of the activation (created for catcher) */
  53065. DUK_ASSERT(env != NULL); /* must be, since env was created when catcher was created */
  53066. act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, env); /* prototype is lex_env before catcher created */
  53067. DUK_HOBJECT_INCREF(thr, act->lex_env);
  53068. DUK_HOBJECT_DECREF_NORZ(thr, env);
  53069. /* There is no need to decref anything else than 'env': if 'env'
  53070. * becomes unreachable, refzero will handle decref'ing its prototype.
  53071. */
  53072. }
  53073. act->cat = cat->parent;
  53074. duk_hthread_catcher_free(thr, cat);
  53075. }
  53076. /* Same as above, but caller is certain no catcher-related lexenv may exist. */
  53077. DUK_INTERNAL void duk_hthread_catcher_unwind_nolexenv_norz(duk_hthread *thr, duk_activation *act) {
  53078. duk_catcher *cat;
  53079. DUK_ASSERT(thr != NULL);
  53080. DUK_ASSERT(act != NULL);
  53081. DUK_ASSERT(act->cat != NULL); /* caller must check */
  53082. cat = act->cat;
  53083. DUK_ASSERT(cat != NULL);
  53084. DUK_DDD(DUK_DDDPRINT("unwinding catch stack entry %p (lexenv check is not done)", (void *) cat));
  53085. DUK_ASSERT(!DUK_CAT_HAS_LEXENV_ACTIVE(cat));
  53086. act->cat = cat->parent;
  53087. duk_hthread_catcher_free(thr, cat);
  53088. }
  53089. DUK_LOCAL
  53090. #if defined(DUK_USE_CACHE_CATCHER)
  53091. DUK_NOINLINE
  53092. #endif
  53093. duk_catcher *duk__hthread_catcher_alloc_slow(duk_hthread *thr) {
  53094. duk_catcher *cat;
  53095. cat = (duk_catcher *) DUK_ALLOC_CHECKED(thr, sizeof(duk_catcher));
  53096. DUK_ASSERT(cat != NULL);
  53097. return cat;
  53098. }
  53099. #if defined(DUK_USE_CACHE_CATCHER)
  53100. DUK_INTERNAL DUK_INLINE duk_catcher *duk_hthread_catcher_alloc(duk_hthread *thr) {
  53101. duk_catcher *cat;
  53102. DUK_ASSERT(thr != NULL);
  53103. cat = thr->heap->catcher_free;
  53104. if (DUK_LIKELY(cat != NULL)) {
  53105. thr->heap->catcher_free = cat->parent;
  53106. return cat;
  53107. }
  53108. return duk__hthread_catcher_alloc_slow(thr);
  53109. }
  53110. #else /* DUK_USE_CACHE_CATCHER */
  53111. DUK_INTERNAL duk_catcher *duk_hthread_catcher_alloc(duk_hthread *thr) {
  53112. return duk__hthread_catcher_alloc_slow(thr);
  53113. }
  53114. #endif /* DUK_USE_CACHE_CATCHER */
  53115. DUK_INTERNAL void duk_hthread_catcher_free(duk_hthread *thr, duk_catcher *cat) {
  53116. DUK_ASSERT(thr != NULL);
  53117. DUK_ASSERT(cat != NULL);
  53118. #if defined(DUK_USE_CACHE_CATCHER)
  53119. /* Unconditional caching for now; freed in mark-and-sweep. */
  53120. cat->parent = thr->heap->catcher_free;
  53121. thr->heap->catcher_free = cat;
  53122. #else
  53123. DUK_FREE_CHECKED(thr, (void *) cat);
  53124. #endif
  53125. }
  53126. DUK_LOCAL
  53127. #if defined(DUK_USE_CACHE_ACTIVATION)
  53128. DUK_NOINLINE
  53129. #endif
  53130. duk_activation *duk__hthread_activation_alloc_slow(duk_hthread *thr) {
  53131. duk_activation *act;
  53132. act = (duk_activation *) DUK_ALLOC_CHECKED(thr, sizeof(duk_activation));
  53133. DUK_ASSERT(act != NULL);
  53134. return act;
  53135. }
  53136. #if defined(DUK_USE_CACHE_ACTIVATION)
  53137. DUK_INTERNAL DUK_INLINE duk_activation *duk_hthread_activation_alloc(duk_hthread *thr) {
  53138. duk_activation *act;
  53139. DUK_ASSERT(thr != NULL);
  53140. act = thr->heap->activation_free;
  53141. if (DUK_LIKELY(act != NULL)) {
  53142. thr->heap->activation_free = act->parent;
  53143. return act;
  53144. }
  53145. return duk__hthread_activation_alloc_slow(thr);
  53146. }
  53147. #else /* DUK_USE_CACHE_ACTIVATION */
  53148. DUK_INTERNAL duk_activation *duk_hthread_activation_alloc(duk_hthread *thr) {
  53149. return duk__hthread_activation_alloc_slow(thr);
  53150. }
  53151. #endif /* DUK_USE_CACHE_ACTIVATION */
  53152. DUK_INTERNAL void duk_hthread_activation_free(duk_hthread *thr, duk_activation *act) {
  53153. DUK_ASSERT(thr != NULL);
  53154. DUK_ASSERT(act != NULL);
  53155. #if defined(DUK_USE_CACHE_ACTIVATION)
  53156. /* Unconditional caching for now; freed in mark-and-sweep. */
  53157. act->parent = thr->heap->activation_free;
  53158. thr->heap->activation_free = act;
  53159. #else
  53160. DUK_FREE_CHECKED(thr, (void *) act);
  53161. #endif
  53162. }
  53163. /* Internal helper: process the unwind for the topmost activation of a thread,
  53164. * but leave the duk_activation in place for possible tailcall reuse.
  53165. */
  53166. DUK_LOCAL void duk__activation_unwind_nofree_norz(duk_hthread *thr) {
  53167. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  53168. duk_heap *heap;
  53169. #endif
  53170. duk_activation *act;
  53171. duk_hobject *func;
  53172. duk_hobject *tmp;
  53173. DUK_ASSERT(thr != NULL);
  53174. DUK_ASSERT(thr->callstack_curr != NULL); /* caller must check */
  53175. DUK_ASSERT(thr->callstack_top > 0);
  53176. act = thr->callstack_curr;
  53177. DUK_ASSERT(act != NULL);
  53178. /* With lightfuncs, act 'func' may be NULL. */
  53179. /* With duk_activation records allocated separately, 'act' is a stable
  53180. * pointer and not affected by side effects.
  53181. */
  53182. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  53183. /*
  53184. * Restore 'caller' property for non-strict callee functions.
  53185. */
  53186. func = DUK_ACT_GET_FUNC(act);
  53187. if (func != NULL && !DUK_HOBJECT_HAS_STRICT(func)) {
  53188. duk_tval *tv_caller;
  53189. duk_tval tv_tmp;
  53190. duk_hobject *h_tmp;
  53191. tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
  53192. /* The act->prev_caller should only be set if the entry for 'caller'
  53193. * exists (as it is only set in that case, and the property is not
  53194. * configurable), but handle all the cases anyway.
  53195. */
  53196. if (tv_caller) {
  53197. DUK_TVAL_SET_TVAL(&tv_tmp, tv_caller);
  53198. if (act->prev_caller) {
  53199. /* Just transfer the refcount from act->prev_caller to tv_caller,
  53200. * so no need for a refcount update. This is the expected case.
  53201. */
  53202. DUK_TVAL_SET_OBJECT(tv_caller, act->prev_caller);
  53203. act->prev_caller = NULL;
  53204. } else {
  53205. DUK_TVAL_SET_NULL(tv_caller); /* no incref needed */
  53206. DUK_ASSERT(act->prev_caller == NULL);
  53207. }
  53208. DUK_TVAL_DECREF_NORZ(thr, &tv_tmp);
  53209. } else {
  53210. h_tmp = act->prev_caller;
  53211. if (h_tmp) {
  53212. act->prev_caller = NULL;
  53213. DUK_HOBJECT_DECREF_NORZ(thr, h_tmp);
  53214. }
  53215. }
  53216. DUK_ASSERT(act->prev_caller == NULL);
  53217. }
  53218. #endif
  53219. /*
  53220. * Unwind debugger state. If we unwind while stepping
  53221. * (for any step type), pause execution. This is the
  53222. * only place explicitly handling a step out.
  53223. */
  53224. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  53225. heap = thr->heap;
  53226. if (heap->dbg_pause_act == thr->callstack_curr) {
  53227. if (heap->dbg_pause_flags & DUK_PAUSE_FLAG_FUNC_EXIT) {
  53228. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by function exit"));
  53229. duk_debug_set_paused(heap);
  53230. } else {
  53231. DUK_D(DUK_DPRINT("unwound past dbg_pause_act, set to NULL"));
  53232. heap->dbg_pause_act = NULL; /* avoid stale pointers */
  53233. }
  53234. DUK_ASSERT(heap->dbg_pause_act == NULL);
  53235. }
  53236. #endif
  53237. /*
  53238. * Unwind catchers.
  53239. *
  53240. * Since there are no references in the catcher structure,
  53241. * unwinding is quite simple. The only thing we need to
  53242. * look out for is popping a possible lexical environment
  53243. * established for an active catch clause.
  53244. */
  53245. while (act->cat != NULL) {
  53246. duk_hthread_catcher_unwind_norz(thr, act);
  53247. }
  53248. /*
  53249. * Close environment record(s) if they exist.
  53250. *
  53251. * Only variable environments are closed. If lex_env != var_env, it
  53252. * cannot currently contain any register bound declarations.
  53253. *
  53254. * Only environments created for a NEWENV function are closed. If an
  53255. * environment is created for e.g. an eval call, it must not be closed.
  53256. */
  53257. func = DUK_ACT_GET_FUNC(act);
  53258. if (func != NULL && !DUK_HOBJECT_HAS_NEWENV(func)) {
  53259. DUK_DDD(DUK_DDDPRINT("skip closing environments, envs not owned by this activation"));
  53260. goto skip_env_close;
  53261. }
  53262. /* func is NULL for lightfunc */
  53263. /* Catch sites are required to clean up their environments
  53264. * in FINALLY part before propagating, so this should
  53265. * always hold here.
  53266. */
  53267. DUK_ASSERT(act->lex_env == act->var_env);
  53268. /* XXX: Closing the environment record copies values from registers
  53269. * into the scope object. It's side effect free as such, but may
  53270. * currently run out of memory which causes an error throw. This is
  53271. * an actual sandboxing problem for error unwinds, and needs to be
  53272. * fixed e.g. by preallocating the scope property slots.
  53273. */
  53274. if (act->var_env != NULL) {
  53275. DUK_DDD(DUK_DDDPRINT("closing var_env record %p -> %!O",
  53276. (void *) act->var_env, (duk_heaphdr *) act->var_env));
  53277. duk_js_close_environment_record(thr, act->var_env);
  53278. }
  53279. skip_env_close:
  53280. /*
  53281. * Update preventcount
  53282. */
  53283. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  53284. DUK_ASSERT(thr->callstack_preventcount >= 1);
  53285. thr->callstack_preventcount--;
  53286. }
  53287. /*
  53288. * Reference count updates, using NORZ macros so we don't
  53289. * need to handle side effects.
  53290. *
  53291. * duk_activation pointers like act->var_env are intentionally
  53292. * left as garbage and not NULLed. Without side effects they
  53293. * can't be used when the values are dangling/garbage.
  53294. */
  53295. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, act->var_env);
  53296. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, act->lex_env);
  53297. tmp = DUK_ACT_GET_FUNC(act);
  53298. DUK_HOBJECT_DECREF_NORZ_ALLOWNULL(thr, tmp);
  53299. DUK_UNREF(tmp);
  53300. }
  53301. /* Unwind topmost duk_activation of a thread, caller must ensure that an
  53302. * activation exists. The call is side effect free, except that scope
  53303. * closure may currently throw an out-of-memory error.
  53304. */
  53305. DUK_INTERNAL void duk_hthread_activation_unwind_norz(duk_hthread *thr) {
  53306. duk_activation *act;
  53307. duk__activation_unwind_nofree_norz(thr);
  53308. DUK_ASSERT(thr->callstack_curr != NULL);
  53309. DUK_ASSERT(thr->callstack_top > 0);
  53310. act = thr->callstack_curr;
  53311. thr->callstack_curr = act->parent;
  53312. thr->callstack_top--;
  53313. /* Ideally we'd restore value stack reserve here to caller's value.
  53314. * This doesn't work for current unwind call sites however, because
  53315. * the current (unwound) value stack top may be above the reserve.
  53316. * Thus value stack reserve is restored by the call sites.
  53317. */
  53318. /* XXX: inline for performance builds? */
  53319. duk_hthread_activation_free(thr, act);
  53320. /* We could clear the book-keeping variables like retval_byteoff for
  53321. * the topmost activation, but don't do so now as it's not necessary.
  53322. */
  53323. }
  53324. DUK_INTERNAL void duk_hthread_activation_unwind_reuse_norz(duk_hthread *thr) {
  53325. duk__activation_unwind_nofree_norz(thr);
  53326. }
  53327. /* Get duk_activation for given callstack level or NULL if level is invalid
  53328. * or deeper than the call stack. Level -1 refers to current activation, -2
  53329. * to its caller, etc. Starting from Duktape 2.2 finding the activation is
  53330. * a linked list scan which gets more expensive the deeper the lookup is.
  53331. */
  53332. DUK_INTERNAL duk_activation *duk_hthread_get_activation_for_level(duk_hthread *thr, duk_int_t level) {
  53333. duk_activation *act;
  53334. if (level >= 0) {
  53335. return NULL;
  53336. }
  53337. act = thr->callstack_curr;
  53338. for (;;) {
  53339. if (act == NULL) {
  53340. return act;
  53341. }
  53342. if (level == -1) {
  53343. return act;
  53344. }
  53345. level++;
  53346. act = act->parent;
  53347. }
  53348. /* never here */
  53349. }
  53350. #if defined(DUK_USE_FINALIZER_TORTURE)
  53351. DUK_INTERNAL void duk_hthread_valstack_torture_realloc(duk_hthread *thr) {
  53352. duk_size_t alloc_size;
  53353. duk_tval *new_ptr;
  53354. duk_ptrdiff_t alloc_end_off;
  53355. duk_ptrdiff_t end_off;
  53356. duk_ptrdiff_t bottom_off;
  53357. duk_ptrdiff_t top_off;
  53358. if (thr->valstack == NULL) {
  53359. DUK_D(DUK_DPRINT("skip valstack torture realloc, valstack is NULL"));
  53360. return;
  53361. }
  53362. alloc_end_off = (duk_ptrdiff_t) ((duk_uint8_t *) thr->valstack_alloc_end - (duk_uint8_t *) thr->valstack);
  53363. end_off = (duk_ptrdiff_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack);
  53364. bottom_off = (duk_ptrdiff_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack);
  53365. top_off = (duk_ptrdiff_t) ((duk_uint8_t *) thr->valstack_top - (duk_uint8_t *) thr->valstack);
  53366. alloc_size = (duk_size_t) alloc_end_off;
  53367. if (alloc_size == 0) {
  53368. DUK_D(DUK_DPRINT("skip valstack torture realloc, alloc_size is zero"));
  53369. return;
  53370. }
  53371. /* Use DUK_ALLOC_RAW() to avoid side effects. */
  53372. new_ptr = (duk_tval *) DUK_ALLOC_RAW(thr->heap, alloc_size);
  53373. if (new_ptr != NULL) {
  53374. duk_memcpy((void *) new_ptr, (const void *) thr->valstack, alloc_size);
  53375. duk_memset((void *) thr->valstack, 0x55, alloc_size);
  53376. DUK_FREE_CHECKED(thr, (void *) thr->valstack);
  53377. thr->valstack = new_ptr;
  53378. thr->valstack_alloc_end = (duk_tval *) ((duk_uint8_t *) new_ptr + alloc_end_off);
  53379. thr->valstack_end = (duk_tval *) ((duk_uint8_t *) new_ptr + end_off);
  53380. thr->valstack_bottom = (duk_tval *) ((duk_uint8_t *) new_ptr + bottom_off);
  53381. thr->valstack_top = (duk_tval *) ((duk_uint8_t *) new_ptr + top_off);
  53382. } else {
  53383. DUK_D(DUK_DPRINT("failed to realloc valstack for torture, ignore"));
  53384. }
  53385. }
  53386. #endif /* DUK_USE_FINALIZER_TORTURE */
  53387. #line 1 "duk_js_arith.c"
  53388. /*
  53389. * Shared helpers for arithmetic operations
  53390. */
  53391. /* #include duk_internal.h -> already included */
  53392. /* ECMAScript modulus ('%') does not match IEEE 754 "remainder" operation
  53393. * (implemented by remainder() in C99) but does seem to match ANSI C fmod().
  53394. * Compare E5 Section 11.5.3 and "man fmod".
  53395. */
  53396. DUK_INTERNAL double duk_js_arith_mod(double d1, double d2) {
  53397. #if defined(DUK_USE_POW_WORKAROUNDS)
  53398. /* Specific fixes to common fmod() implementation issues:
  53399. * - test-bug-mingw-math-issues.js
  53400. */
  53401. if (DUK_ISINF(d2)) {
  53402. if (DUK_ISINF(d1)) {
  53403. return DUK_DOUBLE_NAN;
  53404. } else {
  53405. return d1;
  53406. }
  53407. } else if (d1 == 0.0) {
  53408. /* d1 +/-0 is returned as is (preserving sign) except when
  53409. * d2 is zero or NaN.
  53410. */
  53411. if (d2 == 0.0 || DUK_ISNAN(d2)) {
  53412. return DUK_DOUBLE_NAN;
  53413. } else {
  53414. return d1;
  53415. }
  53416. }
  53417. #else
  53418. /* Some ISO C assumptions. */
  53419. DUK_ASSERT(DUK_FMOD(1.0, DUK_DOUBLE_INFINITY) == 1.0);
  53420. DUK_ASSERT(DUK_FMOD(-1.0, DUK_DOUBLE_INFINITY) == -1.0);
  53421. DUK_ASSERT(DUK_FMOD(1.0, -DUK_DOUBLE_INFINITY) == 1.0);
  53422. DUK_ASSERT(DUK_FMOD(-1.0, -DUK_DOUBLE_INFINITY) == -1.0);
  53423. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(DUK_DOUBLE_INFINITY, DUK_DOUBLE_INFINITY)));
  53424. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(DUK_DOUBLE_INFINITY, -DUK_DOUBLE_INFINITY)));
  53425. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(-DUK_DOUBLE_INFINITY, DUK_DOUBLE_INFINITY)));
  53426. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(-DUK_DOUBLE_INFINITY, -DUK_DOUBLE_INFINITY)));
  53427. DUK_ASSERT(DUK_FMOD(0.0, 1.0) == 0.0 && DUK_SIGNBIT(DUK_FMOD(0.0, 1.0)) == 0);
  53428. DUK_ASSERT(DUK_FMOD(-0.0, 1.0) == 0.0 && DUK_SIGNBIT(DUK_FMOD(-0.0, 1.0)) != 0);
  53429. DUK_ASSERT(DUK_FMOD(0.0, DUK_DOUBLE_INFINITY) == 0.0 && DUK_SIGNBIT(DUK_FMOD(0.0, DUK_DOUBLE_INFINITY)) == 0);
  53430. DUK_ASSERT(DUK_FMOD(-0.0, DUK_DOUBLE_INFINITY) == 0.0 && DUK_SIGNBIT(DUK_FMOD(-0.0, DUK_DOUBLE_INFINITY)) != 0);
  53431. DUK_ASSERT(DUK_FMOD(0.0, -DUK_DOUBLE_INFINITY) == 0.0 && DUK_SIGNBIT(DUK_FMOD(0.0, DUK_DOUBLE_INFINITY)) == 0);
  53432. DUK_ASSERT(DUK_FMOD(-0.0, -DUK_DOUBLE_INFINITY) == 0.0 && DUK_SIGNBIT(DUK_FMOD(-0.0, -DUK_DOUBLE_INFINITY)) != 0);
  53433. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(0.0, 0.0)));
  53434. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(-0.0, 0.0)));
  53435. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(0.0, -0.0)));
  53436. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(-0.0, -0.0)));
  53437. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(0.0, DUK_DOUBLE_NAN)));
  53438. DUK_ASSERT(DUK_ISNAN(DUK_FMOD(-0.0, DUK_DOUBLE_NAN)));
  53439. #endif
  53440. return (duk_double_t) DUK_FMOD((double) d1, (double) d2);
  53441. }
  53442. /* Shared helper for Math.pow() and exponentiation operator. */
  53443. DUK_INTERNAL double duk_js_arith_pow(double x, double y) {
  53444. /* The ANSI C pow() semantics differ from ECMAScript.
  53445. *
  53446. * E.g. when x==1 and y is +/- infinite, the ECMAScript required
  53447. * result is NaN, while at least Linux pow() returns 1.
  53448. */
  53449. duk_small_int_t cx, cy, sx;
  53450. DUK_UNREF(cx);
  53451. DUK_UNREF(sx);
  53452. cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  53453. if (cy == DUK_FP_NAN) {
  53454. goto ret_nan;
  53455. }
  53456. if (DUK_FABS(x) == 1.0 && cy == DUK_FP_INFINITE) {
  53457. goto ret_nan;
  53458. }
  53459. #if defined(DUK_USE_POW_WORKAROUNDS)
  53460. /* Specific fixes to common pow() implementation issues:
  53461. * - test-bug-netbsd-math-pow.js: NetBSD 6.0 on x86 (at least)
  53462. * - test-bug-mingw-math-issues.js
  53463. */
  53464. cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  53465. if (cx == DUK_FP_ZERO && y < 0.0) {
  53466. sx = (duk_small_int_t) DUK_SIGNBIT(x);
  53467. if (sx == 0) {
  53468. /* Math.pow(+0,y) should be Infinity when y<0. NetBSD pow()
  53469. * returns -Infinity instead when y is <0 and finite. The
  53470. * if-clause also catches y == -Infinity (which works even
  53471. * without the fix).
  53472. */
  53473. return DUK_DOUBLE_INFINITY;
  53474. } else {
  53475. /* Math.pow(-0,y) where y<0 should be:
  53476. * - -Infinity if y<0 and an odd integer
  53477. * - Infinity if y<0 but not an odd integer
  53478. * NetBSD pow() returns -Infinity for all finite y<0. The
  53479. * if-clause also catches y == -Infinity (which works even
  53480. * without the fix).
  53481. */
  53482. /* fmod() return value has same sign as input (negative) so
  53483. * the result here will be in the range ]-2,0], -1 indicates
  53484. * odd. If x is -Infinity, NaN is returned and the odd check
  53485. * always concludes "not odd" which results in desired outcome.
  53486. */
  53487. double tmp = DUK_FMOD(y, 2);
  53488. if (tmp == -1.0) {
  53489. return -DUK_DOUBLE_INFINITY;
  53490. } else {
  53491. /* Not odd, or y == -Infinity */
  53492. return DUK_DOUBLE_INFINITY;
  53493. }
  53494. }
  53495. } else if (cx == DUK_FP_NAN) {
  53496. if (y == 0.0) {
  53497. /* NaN ** +/- 0 should always be 1, but is NaN on
  53498. * at least some Cygwin/MinGW versions.
  53499. */
  53500. return 1.0;
  53501. }
  53502. }
  53503. #else
  53504. /* Some ISO C assumptions. */
  53505. DUK_ASSERT(DUK_POW(DUK_DOUBLE_NAN, 0.0) == 1.0);
  53506. DUK_ASSERT(DUK_ISINF(DUK_POW(0.0, -1.0)) && DUK_SIGNBIT(DUK_POW(0.0, -1.0)) == 0);
  53507. DUK_ASSERT(DUK_ISINF(DUK_POW(-0.0, -2.0)) && DUK_SIGNBIT(DUK_POW(-0.0, -2.0)) == 0);
  53508. DUK_ASSERT(DUK_ISINF(DUK_POW(-0.0, -3.0)) && DUK_SIGNBIT(DUK_POW(-0.0, -3.0)) != 0);
  53509. #endif
  53510. return DUK_POW(x, y);
  53511. ret_nan:
  53512. return DUK_DOUBLE_NAN;
  53513. }
  53514. #line 1 "duk_js_call.c"
  53515. /*
  53516. * Call handling.
  53517. *
  53518. * duk_handle_call_unprotected():
  53519. *
  53520. * - Unprotected call to ECMAScript or Duktape/C function, from native
  53521. * code or bytecode executor.
  53522. *
  53523. * - Also handles Ecma-to-Ecma calls which reuses a currently running
  53524. * executor instance to avoid native recursion. Call setup is done
  53525. * normally, but just before calling the bytecode executor a special
  53526. * return code is used to indicate that a calling executor is reused.
  53527. *
  53528. * - Also handles tailcalls, i.e. reuse of current duk_activation.
  53529. *
  53530. * - Also handles setup for initial Duktape.Thread.resume().
  53531. *
  53532. * duk_handle_safe_call():
  53533. *
  53534. * - Protected C call within current activation.
  53535. *
  53536. * setjmp() and local variables have a nasty interaction, see execution.rst;
  53537. * non-volatile locals modified after setjmp() call are not guaranteed to
  53538. * keep their value and can cause compiler or compiler version specific
  53539. * difficult to replicate issues.
  53540. *
  53541. * See 'execution.rst'.
  53542. */
  53543. /* #include duk_internal.h -> already included */
  53544. /* XXX: heap->error_not_allowed for success path too? */
  53545. /*
  53546. * Limit check helpers.
  53547. */
  53548. /* Allow headroom for calls during error augmentation (see GH-191).
  53549. * We allow space for 10 additional recursions, with one extra
  53550. * for, e.g. a print() call at the deepest level, and an extra
  53551. * +1 for protected call wrapping.
  53552. */
  53553. #define DUK__AUGMENT_CALL_RELAX_COUNT (10 + 2)
  53554. DUK_LOCAL DUK_NOINLINE void duk__call_c_recursion_limit_check_slowpath(duk_hthread *thr) {
  53555. /* When augmenting an error, the effective limit is a bit higher.
  53556. * Check for it only if the fast path check fails.
  53557. */
  53558. #if defined(DUK_USE_AUGMENT_ERROR_THROW) || defined(DUK_USE_AUGMENT_ERROR_CREATE)
  53559. if (thr->heap->augmenting_error) {
  53560. if (thr->heap->call_recursion_depth < thr->heap->call_recursion_limit + DUK__AUGMENT_CALL_RELAX_COUNT) {
  53561. DUK_D(DUK_DPRINT("C recursion limit reached but augmenting error and within relaxed limit"));
  53562. return;
  53563. }
  53564. }
  53565. #endif
  53566. DUK_D(DUK_DPRINT("call prevented because C recursion limit reached"));
  53567. DUK_ERROR_RANGE(thr, DUK_STR_C_CALLSTACK_LIMIT);
  53568. DUK_WO_NORETURN(return;);
  53569. }
  53570. DUK_LOCAL DUK_ALWAYS_INLINE void duk__call_c_recursion_limit_check(duk_hthread *thr) {
  53571. DUK_ASSERT(thr->heap->call_recursion_depth >= 0);
  53572. DUK_ASSERT(thr->heap->call_recursion_depth <= thr->heap->call_recursion_limit);
  53573. /* This check is forcibly inlined because it's very cheap and almost
  53574. * always passes. The slow path is forcibly noinline.
  53575. */
  53576. if (DUK_LIKELY(thr->heap->call_recursion_depth < thr->heap->call_recursion_limit)) {
  53577. return;
  53578. }
  53579. duk__call_c_recursion_limit_check_slowpath(thr);
  53580. }
  53581. DUK_LOCAL DUK_NOINLINE void duk__call_callstack_limit_check_slowpath(duk_hthread *thr) {
  53582. /* When augmenting an error, the effective limit is a bit higher.
  53583. * Check for it only if the fast path check fails.
  53584. */
  53585. #if defined(DUK_USE_AUGMENT_ERROR_THROW) || defined(DUK_USE_AUGMENT_ERROR_CREATE)
  53586. if (thr->heap->augmenting_error) {
  53587. if (thr->callstack_top < DUK_USE_CALLSTACK_LIMIT + DUK__AUGMENT_CALL_RELAX_COUNT) {
  53588. DUK_D(DUK_DPRINT("call stack limit reached but augmenting error and within relaxed limit"));
  53589. return;
  53590. }
  53591. }
  53592. #endif
  53593. /* XXX: error message is a bit misleading: we reached a recursion
  53594. * limit which is also essentially the same as a C callstack limit
  53595. * (except perhaps with some relaxed threading assumptions).
  53596. */
  53597. DUK_D(DUK_DPRINT("call prevented because call stack limit reached"));
  53598. DUK_ERROR_RANGE(thr, DUK_STR_CALLSTACK_LIMIT);
  53599. DUK_WO_NORETURN(return;);
  53600. }
  53601. DUK_LOCAL DUK_ALWAYS_INLINE void duk__call_callstack_limit_check(duk_hthread *thr) {
  53602. /* This check is forcibly inlined because it's very cheap and almost
  53603. * always passes. The slow path is forcibly noinline.
  53604. */
  53605. if (DUK_LIKELY(thr->callstack_top < DUK_USE_CALLSTACK_LIMIT)) {
  53606. return;
  53607. }
  53608. duk__call_callstack_limit_check_slowpath(thr);
  53609. }
  53610. /*
  53611. * Interrupt counter fixup (for development only).
  53612. */
  53613. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  53614. DUK_LOCAL void duk__interrupt_fixup(duk_hthread *thr, duk_hthread *entry_curr_thread) {
  53615. /* Currently the bytecode executor and executor interrupt
  53616. * instruction counts are off because we don't execute the
  53617. * interrupt handler when we're about to exit from the initial
  53618. * user call into Duktape.
  53619. *
  53620. * If we were to execute the interrupt handler here, the counts
  53621. * would match. You can enable this block manually to check
  53622. * that this is the case.
  53623. */
  53624. DUK_ASSERT(thr != NULL);
  53625. DUK_ASSERT(thr->heap != NULL);
  53626. #if defined(DUK_USE_INTERRUPT_DEBUG_FIXUP)
  53627. if (entry_curr_thread == NULL) {
  53628. thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter;
  53629. thr->heap->inst_count_interrupt += thr->interrupt_init;
  53630. DUK_DD(DUK_DDPRINT("debug test: updated interrupt count on exit to "
  53631. "user code, instruction counts: executor=%ld, interrupt=%ld",
  53632. (long) thr->heap->inst_count_exec, (long) thr->heap->inst_count_interrupt));
  53633. DUK_ASSERT(thr->heap->inst_count_exec == thr->heap->inst_count_interrupt);
  53634. }
  53635. #else
  53636. DUK_UNREF(thr);
  53637. DUK_UNREF(entry_curr_thread);
  53638. #endif
  53639. }
  53640. #endif
  53641. /*
  53642. * Arguments object creation.
  53643. *
  53644. * Creating arguments objects involves many small details, see E5 Section
  53645. * 10.6 for the specific requirements. Much of the arguments object exotic
  53646. * behavior is implemented in duk_hobject_props.c, and is enabled by the
  53647. * object flag DUK_HOBJECT_FLAG_EXOTIC_ARGUMENTS.
  53648. */
  53649. DUK_LOCAL void duk__create_arguments_object(duk_hthread *thr,
  53650. duk_hobject *func,
  53651. duk_hobject *varenv,
  53652. duk_idx_t idx_args) {
  53653. duk_hobject *arg; /* 'arguments' */
  53654. duk_hobject *formals; /* formals for 'func' (may be NULL if func is a C function) */
  53655. duk_idx_t i_arg;
  53656. duk_idx_t i_map;
  53657. duk_idx_t i_mappednames;
  53658. duk_idx_t i_formals;
  53659. duk_idx_t i_argbase;
  53660. duk_idx_t n_formals;
  53661. duk_idx_t idx;
  53662. duk_idx_t num_stack_args;
  53663. duk_bool_t need_map;
  53664. DUK_ASSERT(thr != NULL);
  53665. DUK_ASSERT(func != NULL);
  53666. DUK_ASSERT(DUK_HOBJECT_IS_NONBOUND_FUNCTION(func));
  53667. DUK_ASSERT(varenv != NULL);
  53668. /* [ ... func this arg1(@idx_args) ... argN envobj ]
  53669. * [ arg1(@idx_args) ... argN envobj ] (for tailcalls)
  53670. */
  53671. need_map = 0;
  53672. i_argbase = idx_args;
  53673. num_stack_args = duk_get_top(thr) - i_argbase - 1;
  53674. DUK_ASSERT(i_argbase >= 0);
  53675. DUK_ASSERT(num_stack_args >= 0);
  53676. duk_push_hobject(thr, func);
  53677. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_FORMALS);
  53678. formals = duk_get_hobject(thr, -1);
  53679. if (formals) {
  53680. n_formals = (duk_idx_t) duk_get_length(thr, -1);
  53681. } else {
  53682. /* This shouldn't happen without tampering of internal
  53683. * properties: if a function accesses 'arguments', _Formals
  53684. * is kept. Check for the case anyway in case internal
  53685. * properties have been modified manually.
  53686. */
  53687. DUK_D(DUK_DPRINT("_Formals is undefined when creating arguments, use n_formals == 0"));
  53688. n_formals = 0;
  53689. }
  53690. duk_remove_m2(thr); /* leave formals on stack for later use */
  53691. i_formals = duk_require_top_index(thr);
  53692. DUK_ASSERT(n_formals >= 0);
  53693. DUK_ASSERT(formals != NULL || n_formals == 0);
  53694. DUK_DDD(DUK_DDDPRINT("func=%!O, formals=%!O, n_formals=%ld",
  53695. (duk_heaphdr *) func, (duk_heaphdr *) formals,
  53696. (long) n_formals));
  53697. /* [ ... formals ] */
  53698. /*
  53699. * Create required objects:
  53700. * - 'arguments' object: array-like, but not an array
  53701. * - 'map' object: internal object, tied to 'arguments'
  53702. * - 'mappedNames' object: temporary value used during construction
  53703. */
  53704. arg = duk_push_object_helper(thr,
  53705. DUK_HOBJECT_FLAG_EXTENSIBLE |
  53706. DUK_HOBJECT_FLAG_FASTREFS |
  53707. DUK_HOBJECT_FLAG_ARRAY_PART |
  53708. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_ARGUMENTS),
  53709. DUK_BIDX_OBJECT_PROTOTYPE);
  53710. DUK_ASSERT(arg != NULL);
  53711. (void) duk_push_object_helper(thr,
  53712. DUK_HOBJECT_FLAG_EXTENSIBLE |
  53713. DUK_HOBJECT_FLAG_FASTREFS |
  53714. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  53715. -1); /* no prototype */
  53716. (void) duk_push_object_helper(thr,
  53717. DUK_HOBJECT_FLAG_EXTENSIBLE |
  53718. DUK_HOBJECT_FLAG_FASTREFS |
  53719. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJECT),
  53720. -1); /* no prototype */
  53721. i_arg = duk_get_top(thr) - 3;
  53722. i_map = i_arg + 1;
  53723. i_mappednames = i_arg + 2;
  53724. /* [ ... formals arguments map mappedNames ] */
  53725. DUK_DDD(DUK_DDDPRINT("created arguments related objects: "
  53726. "arguments at index %ld -> %!O "
  53727. "map at index %ld -> %!O "
  53728. "mappednames at index %ld -> %!O",
  53729. (long) i_arg, (duk_heaphdr *) duk_get_hobject(thr, i_arg),
  53730. (long) i_map, (duk_heaphdr *) duk_get_hobject(thr, i_map),
  53731. (long) i_mappednames, (duk_heaphdr *) duk_get_hobject(thr, i_mappednames)));
  53732. /*
  53733. * Init arguments properties, map, etc.
  53734. */
  53735. duk_push_int(thr, num_stack_args);
  53736. duk_xdef_prop_stridx(thr, i_arg, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_WC);
  53737. /*
  53738. * Init argument related properties.
  53739. */
  53740. /* step 11 */
  53741. idx = num_stack_args - 1;
  53742. while (idx >= 0) {
  53743. DUK_DDD(DUK_DDDPRINT("arg idx %ld, argbase=%ld, argidx=%ld",
  53744. (long) idx, (long) i_argbase, (long) (i_argbase + idx)));
  53745. DUK_DDD(DUK_DDDPRINT("define arguments[%ld]=arg", (long) idx));
  53746. duk_dup(thr, i_argbase + idx);
  53747. duk_xdef_prop_index_wec(thr, i_arg, (duk_uarridx_t) idx);
  53748. DUK_DDD(DUK_DDDPRINT("defined arguments[%ld]=arg", (long) idx));
  53749. /* step 11.c is relevant only if non-strict (checked in 11.c.ii) */
  53750. if (!DUK_HOBJECT_HAS_STRICT(func) && idx < n_formals) {
  53751. DUK_ASSERT(formals != NULL);
  53752. DUK_DDD(DUK_DDDPRINT("strict function, index within formals (%ld < %ld)",
  53753. (long) idx, (long) n_formals));
  53754. duk_get_prop_index(thr, i_formals, (duk_uarridx_t) idx);
  53755. DUK_ASSERT(duk_is_string(thr, -1));
  53756. duk_dup_top(thr); /* [ ... name name ] */
  53757. if (!duk_has_prop(thr, i_mappednames)) {
  53758. /* steps 11.c.ii.1 - 11.c.ii.4, but our internal book-keeping
  53759. * differs from the reference model
  53760. */
  53761. /* [ ... name ] */
  53762. need_map = 1;
  53763. DUK_DDD(DUK_DDDPRINT("set mappednames[%s]=%ld",
  53764. (const char *) duk_get_string(thr, -1),
  53765. (long) idx));
  53766. duk_dup_top(thr); /* name */
  53767. (void) duk_push_uint_to_hstring(thr, (duk_uint_t) idx); /* index */
  53768. duk_xdef_prop_wec(thr, i_mappednames); /* out of spec, must be configurable */
  53769. DUK_DDD(DUK_DDDPRINT("set map[%ld]=%s",
  53770. (long) idx,
  53771. duk_get_string(thr, -1)));
  53772. duk_dup_top(thr); /* name */
  53773. duk_xdef_prop_index_wec(thr, i_map, (duk_uarridx_t) idx); /* out of spec, must be configurable */
  53774. } else {
  53775. /* duk_has_prop() popped the second 'name' */
  53776. }
  53777. /* [ ... name ] */
  53778. duk_pop(thr); /* pop 'name' */
  53779. }
  53780. idx--;
  53781. }
  53782. DUK_DDD(DUK_DDDPRINT("actual arguments processed"));
  53783. /* step 12 */
  53784. if (need_map) {
  53785. DUK_DDD(DUK_DDDPRINT("adding 'map' and 'varenv' to arguments object"));
  53786. /* should never happen for a strict callee */
  53787. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
  53788. duk_dup(thr, i_map);
  53789. duk_xdef_prop_stridx(thr, i_arg, DUK_STRIDX_INT_MAP, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
  53790. /* The variable environment for magic variable bindings needs to be
  53791. * given by the caller and recorded in the arguments object.
  53792. *
  53793. * See E5 Section 10.6, the creation of setters/getters.
  53794. *
  53795. * The variable environment also provides access to the callee, so
  53796. * an explicit (internal) callee property is not needed.
  53797. */
  53798. duk_push_hobject(thr, varenv);
  53799. duk_xdef_prop_stridx(thr, i_arg, DUK_STRIDX_INT_VARENV, DUK_PROPDESC_FLAGS_NONE); /* out of spec, don't care */
  53800. }
  53801. /* steps 13-14 */
  53802. if (DUK_HOBJECT_HAS_STRICT(func)) {
  53803. /* Callee/caller are throwers and are not deletable etc. They
  53804. * could be implemented as virtual properties, but currently
  53805. * there is no support for virtual properties which are accessors
  53806. * (only plain virtual properties). This would not be difficult
  53807. * to change in duk_hobject_props, but we can make the throwers
  53808. * normal, concrete properties just as easily.
  53809. *
  53810. * Note that the specification requires that the *same* thrower
  53811. * built-in object is used here! See E5 Section 10.6 main
  53812. * algoritm, step 14, and Section 13.2.3 which describes the
  53813. * thrower. See test case test-arguments-throwers.js.
  53814. */
  53815. DUK_DDD(DUK_DDDPRINT("strict function, setting caller/callee to throwers"));
  53816. duk_xdef_prop_stridx_thrower(thr, i_arg, DUK_STRIDX_CALLER);
  53817. duk_xdef_prop_stridx_thrower(thr, i_arg, DUK_STRIDX_CALLEE);
  53818. } else {
  53819. DUK_DDD(DUK_DDDPRINT("non-strict function, setting callee to actual value"));
  53820. duk_push_hobject(thr, func);
  53821. duk_xdef_prop_stridx(thr, i_arg, DUK_STRIDX_CALLEE, DUK_PROPDESC_FLAGS_WC);
  53822. }
  53823. /* set exotic behavior only after we're done */
  53824. if (need_map) {
  53825. /* Exotic behaviors are only enabled for arguments objects
  53826. * which have a parameter map (see E5 Section 10.6 main
  53827. * algorithm, step 12).
  53828. *
  53829. * In particular, a non-strict arguments object with no
  53830. * mapped formals does *NOT* get exotic behavior, even
  53831. * for e.g. "caller" property. This seems counterintuitive
  53832. * but seems to be the case.
  53833. */
  53834. /* cannot be strict (never mapped variables) */
  53835. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(func));
  53836. DUK_DDD(DUK_DDDPRINT("enabling exotic behavior for arguments object"));
  53837. DUK_HOBJECT_SET_EXOTIC_ARGUMENTS(arg);
  53838. } else {
  53839. DUK_DDD(DUK_DDDPRINT("not enabling exotic behavior for arguments object"));
  53840. }
  53841. DUK_DDD(DUK_DDDPRINT("final arguments related objects: "
  53842. "arguments at index %ld -> %!O "
  53843. "map at index %ld -> %!O "
  53844. "mappednames at index %ld -> %!O",
  53845. (long) i_arg, (duk_heaphdr *) duk_get_hobject(thr, i_arg),
  53846. (long) i_map, (duk_heaphdr *) duk_get_hobject(thr, i_map),
  53847. (long) i_mappednames, (duk_heaphdr *) duk_get_hobject(thr, i_mappednames)));
  53848. /* [ args(n) envobj formals arguments map mappednames ] */
  53849. duk_pop_2(thr);
  53850. duk_remove_m2(thr);
  53851. /* [ args(n) envobj arguments ] */
  53852. }
  53853. /* Helper for creating the arguments object and adding it to the env record
  53854. * on top of the value stack.
  53855. */
  53856. DUK_LOCAL void duk__handle_createargs_for_call(duk_hthread *thr,
  53857. duk_hobject *func,
  53858. duk_hobject *env,
  53859. duk_idx_t idx_args) {
  53860. DUK_DDD(DUK_DDDPRINT("creating arguments object for function call"));
  53861. DUK_ASSERT(thr != NULL);
  53862. DUK_ASSERT(func != NULL);
  53863. DUK_ASSERT(env != NULL);
  53864. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  53865. /* [ ... arg1 ... argN envobj ] */
  53866. duk__create_arguments_object(thr,
  53867. func,
  53868. env,
  53869. idx_args);
  53870. /* [ ... arg1 ... argN envobj argobj ] */
  53871. duk_xdef_prop_stridx_short(thr,
  53872. -2,
  53873. DUK_STRIDX_LC_ARGUMENTS,
  53874. DUK_HOBJECT_HAS_STRICT(func) ? DUK_PROPDESC_FLAGS_E : /* strict: non-deletable, non-writable */
  53875. DUK_PROPDESC_FLAGS_WE); /* non-strict: non-deletable, writable */
  53876. /* [ ... arg1 ... argN envobj ] */
  53877. }
  53878. /*
  53879. * Helpers for constructor call handling.
  53880. *
  53881. * There are two [[Construct]] operations in the specification:
  53882. *
  53883. * - E5 Section 13.2.2: for Function objects
  53884. * - E5 Section 15.3.4.5.2: for "bound" Function objects
  53885. *
  53886. * The chain of bound functions is resolved in Section 15.3.4.5.2,
  53887. * with arguments "piling up" until the [[Construct]] internal
  53888. * method is called on the final, actual Function object. Note
  53889. * that the "prototype" property is looked up *only* from the
  53890. * final object, *before* calling the constructor.
  53891. *
  53892. * Since Duktape 2.2 bound functions are represented with the
  53893. * duk_hboundfunc internal type, and bound function chains are
  53894. * collapsed when a bound function is created. As a result, the
  53895. * direct target of a duk_hboundfunc is always non-bound and the
  53896. * this/argument lists have been resolved.
  53897. *
  53898. * When constructing new Array instances, an unnecessary object is
  53899. * created and discarded now: the standard [[Construct]] creates an
  53900. * object, and calls the Array constructor. The Array constructor
  53901. * returns an Array instance, which is used as the result value for
  53902. * the "new" operation; the object created before the Array constructor
  53903. * call is discarded.
  53904. *
  53905. * This would be easy to fix, e.g. by knowing that the Array constructor
  53906. * will always create a replacement object and skip creating the fallback
  53907. * object in that case.
  53908. */
  53909. /* Update default instance prototype for constructor call. */
  53910. DUK_LOCAL void duk__update_default_instance_proto(duk_hthread *thr, duk_idx_t idx_func) {
  53911. duk_hobject *proto;
  53912. duk_hobject *fallback;
  53913. DUK_ASSERT(duk_is_constructable(thr, idx_func));
  53914. duk_get_prop_stridx_short(thr, idx_func, DUK_STRIDX_PROTOTYPE);
  53915. proto = duk_get_hobject(thr, -1);
  53916. if (proto == NULL) {
  53917. DUK_DDD(DUK_DDDPRINT("constructor has no 'prototype' property, or value not an object "
  53918. "-> leave standard Object prototype as fallback prototype"));
  53919. } else {
  53920. DUK_DDD(DUK_DDDPRINT("constructor has 'prototype' property with object value "
  53921. "-> set fallback prototype to that value: %!iO", (duk_heaphdr *) proto));
  53922. /* Original fallback (default instance) is untouched when
  53923. * resolving bound functions etc.
  53924. */
  53925. fallback = duk_known_hobject(thr, idx_func + 1);
  53926. DUK_ASSERT(fallback != NULL);
  53927. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, fallback, proto);
  53928. }
  53929. duk_pop(thr);
  53930. }
  53931. /* Postprocess: return value special handling, error augmentation. */
  53932. DUK_INTERNAL void duk_call_construct_postprocess(duk_hthread *thr, duk_small_uint_t proxy_invariant) {
  53933. /* Use either fallback (default instance) or retval depending
  53934. * on retval type. Needs to be called before unwind because
  53935. * the default instance is read from the current (immutable)
  53936. * 'this' binding.
  53937. *
  53938. * For Proxy 'construct' calls the return value must be an
  53939. * Object (we accept object-like values like buffers and
  53940. * lightfuncs too). If not, TypeError.
  53941. */
  53942. if (duk_check_type_mask(thr, -1, DUK_TYPE_MASK_OBJECT |
  53943. DUK_TYPE_MASK_BUFFER |
  53944. DUK_TYPE_MASK_LIGHTFUNC)) {
  53945. DUK_DDD(DUK_DDDPRINT("replacement value"));
  53946. } else {
  53947. if (DUK_UNLIKELY(proxy_invariant != 0U)) {
  53948. /* Proxy 'construct' return value invariant violated. */
  53949. DUK_ERROR_TYPE_INVALID_TRAP_RESULT(thr);
  53950. DUK_WO_NORETURN(return;);
  53951. }
  53952. /* XXX: direct value stack access */
  53953. duk_pop(thr);
  53954. duk_push_this(thr);
  53955. }
  53956. #if defined(DUK_USE_AUGMENT_ERROR_CREATE)
  53957. /* Augment created errors upon creation, not when they are thrown or
  53958. * rethrown. __FILE__ and __LINE__ are not desirable here; the call
  53959. * stack reflects the caller which is correct. Skip topmost, unwound
  53960. * activation when creating a traceback. If thr->ptr_curr_pc was !=
  53961. * NULL we'd need to sync the current PC so that the traceback comes
  53962. * out right; however it is always synced here so just assert for it.
  53963. */
  53964. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  53965. duk_err_augment_error_create(thr, thr, NULL, 0, DUK_AUGMENT_FLAG_NOBLAME_FILELINE |
  53966. DUK_AUGMENT_FLAG_SKIP_ONE);
  53967. #endif
  53968. }
  53969. /*
  53970. * Helper for handling a bound function when a call is being made.
  53971. *
  53972. * Assumes that bound function chains have been "collapsed" so that either
  53973. * the target is non-bound or there is one bound function that points to a
  53974. * nonbound target.
  53975. *
  53976. * Prepends the bound arguments to the value stack (at idx_func + 2).
  53977. * The 'this' binding is also updated if necessary (at idx_func + 1).
  53978. * Note that for constructor calls the 'this' binding is never updated by
  53979. * [[BoundThis]].
  53980. */
  53981. DUK_LOCAL void duk__handle_bound_chain_for_call(duk_hthread *thr,
  53982. duk_idx_t idx_func,
  53983. duk_bool_t is_constructor_call) {
  53984. duk_tval *tv_func;
  53985. duk_hobject *func;
  53986. duk_idx_t len;
  53987. DUK_ASSERT(thr != NULL);
  53988. /* On entry, item at idx_func is a bound, non-lightweight function,
  53989. * but we don't rely on that below.
  53990. */
  53991. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  53992. tv_func = duk_require_tval(thr, idx_func);
  53993. DUK_ASSERT(tv_func != NULL);
  53994. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  53995. func = DUK_TVAL_GET_OBJECT(tv_func);
  53996. /* XXX: separate helper function, out of fast path? */
  53997. if (DUK_HOBJECT_HAS_BOUNDFUNC(func)) {
  53998. duk_hboundfunc *h_bound;
  53999. duk_tval *tv_args;
  54000. duk_tval *tv_gap;
  54001. h_bound = (duk_hboundfunc *) (void *) func;
  54002. tv_args = h_bound->args;
  54003. len = h_bound->nargs;
  54004. DUK_ASSERT(len == 0 || tv_args != NULL);
  54005. DUK_DDD(DUK_DDDPRINT("bound function encountered, ptr=%p: %!T",
  54006. (void *) DUK_TVAL_GET_OBJECT(tv_func), tv_func));
  54007. /* [ ... func this arg1 ... argN ] */
  54008. if (is_constructor_call) {
  54009. /* See: tests/ecmascript/test-spec-bound-constructor.js */
  54010. DUK_DDD(DUK_DDDPRINT("constructor call: don't update this binding"));
  54011. } else {
  54012. /* XXX: duk_replace_tval */
  54013. duk_push_tval(thr, &h_bound->this_binding);
  54014. duk_replace(thr, idx_func + 1); /* idx_this = idx_func + 1 */
  54015. }
  54016. /* [ ... func this arg1 ... argN ] */
  54017. duk_require_stack(thr, len);
  54018. tv_gap = duk_reserve_gap(thr, idx_func + 2, len);
  54019. duk_copy_tvals_incref(thr, tv_gap, tv_args, (duk_size_t) len);
  54020. /* [ ... func this <bound args> arg1 ... argN ] */
  54021. duk_push_tval(thr, &h_bound->target);
  54022. duk_replace(thr, idx_func); /* replace in stack */
  54023. DUK_DDD(DUK_DDDPRINT("bound function handled, idx_func=%ld, curr func=%!T",
  54024. (long) idx_func, duk_get_tval(thr, idx_func)));
  54025. }
  54026. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  54027. /* Lightweight function: never bound, so terminate. */
  54028. ;
  54029. } else {
  54030. /* Shouldn't happen, so ugly error is enough. */
  54031. DUK_ERROR_INTERNAL(thr);
  54032. DUK_WO_NORETURN(return;);
  54033. }
  54034. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54035. DUK_DDD(DUK_DDDPRINT("final non-bound function is: %!T", duk_get_tval(thr, idx_func)));
  54036. #if defined(DUK_USE_ASSERTIONS)
  54037. tv_func = duk_require_tval(thr, idx_func);
  54038. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func) || DUK_TVAL_IS_OBJECT(tv_func));
  54039. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  54040. func = DUK_TVAL_GET_OBJECT(tv_func);
  54041. DUK_ASSERT(func != NULL);
  54042. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(func));
  54043. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(func) ||
  54044. DUK_HOBJECT_HAS_NATFUNC(func));
  54045. }
  54046. #endif
  54047. }
  54048. /*
  54049. * Helper for inline handling of .call(), .apply(), and .construct().
  54050. */
  54051. DUK_LOCAL duk_bool_t duk__handle_specialfuncs_for_call(duk_hthread *thr, duk_idx_t idx_func, duk_hobject *func, duk_small_uint_t *call_flags, duk_bool_t first) {
  54052. #if defined(DUK_USE_ASSERTIONS)
  54053. duk_c_function natfunc;
  54054. #endif
  54055. duk_tval *tv_args;
  54056. DUK_ASSERT(func != NULL);
  54057. DUK_ASSERT((*call_flags & DUK_CALL_FLAG_CONSTRUCT) == 0); /* Caller. */
  54058. #if defined(DUK_USE_ASSERTIONS)
  54059. natfunc = ((duk_hnatfunc *) func)->func;
  54060. DUK_ASSERT(natfunc != NULL);
  54061. #endif
  54062. /* On every round of function resolution at least target function and
  54063. * 'this' binding are set. We can assume that here, and must guarantee
  54064. * it on exit. Value stack reserve is extended for bound function and
  54065. * .apply() unpacking so we don't need to extend it here when we need a
  54066. * few slots.
  54067. */
  54068. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54069. /* Handle native 'eval' specially. A direct eval check is only made
  54070. * for the first resolution attempt; e.g. a bound eval call is -not-
  54071. * a direct eval call.
  54072. */
  54073. if (DUK_UNLIKELY(((duk_hnatfunc *) func)->magic == 15)) {
  54074. /* For now no special handling except for direct eval
  54075. * detection.
  54076. */
  54077. DUK_ASSERT(((duk_hnatfunc *) func)->func == duk_bi_global_object_eval);
  54078. if (first && (*call_flags & DUK_CALL_FLAG_CALLED_AS_EVAL)) {
  54079. *call_flags = (*call_flags & ~DUK_CALL_FLAG_CALLED_AS_EVAL) | DUK_CALL_FLAG_DIRECT_EVAL;
  54080. }
  54081. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54082. return 1; /* stop resolving */
  54083. }
  54084. /* Handle special functions based on the DUK_HOBJECT_FLAG_SPECIAL_CALL
  54085. * flag; their magic value is used for switch-case.
  54086. *
  54087. * NOTE: duk_unpack_array_like() reserves value stack space
  54088. * for the result values (unlike most other value stack calls).
  54089. */
  54090. switch (((duk_hnatfunc *) func)->magic) {
  54091. case 0: { /* 0=Function.prototype.call() */
  54092. /* Value stack:
  54093. * idx_func + 0: Function.prototype.call() [removed]
  54094. * idx_func + 1: this binding for .call (target function)
  54095. * idx_func + 2: 1st argument to .call, desired 'this' binding
  54096. * idx_func + 3: 2nd argument to .call, desired 1st argument for ultimate target
  54097. * ...
  54098. *
  54099. * Remove idx_func + 0 to get:
  54100. * idx_func + 0: target function
  54101. * idx_func + 1: this binding
  54102. * idx_func + 2: call arguments
  54103. * ...
  54104. */
  54105. DUK_ASSERT(natfunc == duk_bi_function_prototype_call);
  54106. duk_remove_unsafe(thr, idx_func);
  54107. tv_args = thr->valstack_bottom + idx_func + 2;
  54108. if (thr->valstack_top < tv_args) {
  54109. DUK_ASSERT(tv_args <= thr->valstack_end);
  54110. thr->valstack_top = tv_args; /* at least target function and 'this' binding present */
  54111. }
  54112. break;
  54113. }
  54114. case 1: { /* 1=Function.prototype.apply() */
  54115. /* Value stack:
  54116. * idx_func + 0: Function.prototype.apply() [removed]
  54117. * idx_func + 1: this binding for .apply (target function)
  54118. * idx_func + 2: 1st argument to .apply, desired 'this' binding
  54119. * idx_func + 3: 2nd argument to .apply, argArray
  54120. * [anything after this MUST be ignored]
  54121. *
  54122. * Remove idx_func + 0 and unpack the argArray to get:
  54123. * idx_func + 0: target function
  54124. * idx_func + 1: this binding
  54125. * idx_func + 2: call arguments
  54126. * ...
  54127. */
  54128. DUK_ASSERT(natfunc == duk_bi_function_prototype_apply);
  54129. duk_remove_unsafe(thr, idx_func);
  54130. goto apply_shared;
  54131. }
  54132. #if defined(DUK_USE_REFLECT_BUILTIN)
  54133. case 2: { /* 2=Reflect.apply() */
  54134. /* Value stack:
  54135. * idx_func + 0: Reflect.apply() [removed]
  54136. * idx_func + 1: this binding for .apply (ignored, usually Reflect) [removed]
  54137. * idx_func + 2: 1st argument to .apply, target function
  54138. * idx_func + 3: 2nd argument to .apply, desired 'this' binding
  54139. * idx_func + 4: 3rd argument to .apply, argArray
  54140. * [anything after this MUST be ignored]
  54141. *
  54142. * Remove idx_func + 0 and idx_func + 1, and unpack the argArray to get:
  54143. * idx_func + 0: target function
  54144. * idx_func + 1: this binding
  54145. * idx_func + 2: call arguments
  54146. * ...
  54147. */
  54148. DUK_ASSERT(natfunc == duk_bi_reflect_apply);
  54149. duk_remove_n_unsafe(thr, idx_func, 2);
  54150. goto apply_shared;
  54151. }
  54152. case 3: { /* 3=Reflect.construct() */
  54153. /* Value stack:
  54154. * idx_func + 0: Reflect.construct() [removed]
  54155. * idx_func + 1: this binding for .construct (ignored, usually Reflect) [removed]
  54156. * idx_func + 2: 1st argument to .construct, target function
  54157. * idx_func + 3: 2nd argument to .construct, argArray
  54158. * idx_func + 4: 3rd argument to .construct, newTarget
  54159. * [anything after this MUST be ignored]
  54160. *
  54161. * Remove idx_func + 0 and idx_func + 1, unpack the argArray,
  54162. * and insert default instance (prototype not yet updated), to get:
  54163. * idx_func + 0: target function
  54164. * idx_func + 1: this binding (default instance)
  54165. * idx_func + 2: constructor call arguments
  54166. * ...
  54167. *
  54168. * Call flags must be updated to reflect the fact that we're
  54169. * now dealing with a constructor call, and e.g. the 'this'
  54170. * binding cannot be overwritten if the target is bound.
  54171. *
  54172. * newTarget is checked but not yet passed onwards.
  54173. */
  54174. duk_idx_t top;
  54175. DUK_ASSERT(natfunc == duk_bi_reflect_construct);
  54176. *call_flags |= DUK_CALL_FLAG_CONSTRUCT;
  54177. duk_remove_n_unsafe(thr, idx_func, 2);
  54178. top = duk_get_top(thr);
  54179. if (!duk_is_constructable(thr, idx_func)) {
  54180. /* Target constructability must be checked before
  54181. * unpacking argArray (which may cause side effects).
  54182. * Just return; caller will throw the error.
  54183. */
  54184. duk_set_top_unsafe(thr, idx_func + 2); /* satisfy asserts */
  54185. break;
  54186. }
  54187. duk_push_object(thr);
  54188. duk_insert(thr, idx_func + 1); /* default instance */
  54189. /* [ ... func default_instance argArray newTarget? ] */
  54190. top = duk_get_top(thr);
  54191. if (top < idx_func + 3) {
  54192. /* argArray is a mandatory argument for Reflect.construct(). */
  54193. DUK_ERROR_TYPE_INVALID_ARGS(thr);
  54194. DUK_WO_NORETURN(return 0;);
  54195. }
  54196. if (top > idx_func + 3) {
  54197. if (!duk_strict_equals(thr, idx_func, idx_func + 3)) {
  54198. /* XXX: [[Construct]] newTarget currently unsupported */
  54199. DUK_ERROR_UNSUPPORTED(thr);
  54200. DUK_WO_NORETURN(return 0;);
  54201. }
  54202. duk_set_top_unsafe(thr, idx_func + 3); /* remove any args beyond argArray */
  54203. }
  54204. DUK_ASSERT(duk_get_top(thr) == idx_func + 3);
  54205. DUK_ASSERT(duk_is_valid_index(thr, idx_func + 2));
  54206. (void) duk_unpack_array_like(thr, idx_func + 2); /* XXX: should also remove target to be symmetric with duk_pack()? */
  54207. duk_remove(thr, idx_func + 2);
  54208. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54209. break;
  54210. }
  54211. #endif /* DUK_USE_REFLECT_BUILTIN */
  54212. default: {
  54213. DUK_ASSERT(0);
  54214. DUK_UNREACHABLE();
  54215. }
  54216. }
  54217. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54218. return 0; /* keep resolving */
  54219. apply_shared:
  54220. tv_args = thr->valstack_bottom + idx_func + 2;
  54221. if (thr->valstack_top <= tv_args) {
  54222. DUK_ASSERT(tv_args <= thr->valstack_end);
  54223. thr->valstack_top = tv_args; /* at least target func and 'this' binding present */
  54224. /* No need to check for argArray. */
  54225. } else {
  54226. DUK_ASSERT(duk_get_top(thr) >= idx_func + 3); /* idx_func + 2 covered above */
  54227. if (thr->valstack_top > tv_args + 1) {
  54228. duk_set_top_unsafe(thr, idx_func + 3); /* remove any args beyond argArray */
  54229. }
  54230. DUK_ASSERT(duk_is_valid_index(thr, idx_func + 2));
  54231. if (!duk_is_callable(thr, idx_func)) {
  54232. /* Avoid unpack side effects if the target isn't callable.
  54233. * Calling code will throw the actual error.
  54234. */
  54235. } else {
  54236. (void) duk_unpack_array_like(thr, idx_func + 2);
  54237. duk_remove(thr, idx_func + 2);
  54238. }
  54239. }
  54240. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54241. return 0; /* keep resolving */
  54242. }
  54243. /*
  54244. * Helper for Proxy handling.
  54245. */
  54246. #if defined(DUK_USE_ES6_PROXY)
  54247. DUK_LOCAL void duk__handle_proxy_for_call(duk_hthread *thr, duk_idx_t idx_func, duk_hproxy *h_proxy, duk_small_uint_t *call_flags) {
  54248. duk_bool_t rc;
  54249. /* Value stack:
  54250. * idx_func + 0: Proxy object
  54251. * idx_func + 1: this binding for call
  54252. * idx_func + 2: 1st argument for call
  54253. * idx_func + 3: 2nd argument for call
  54254. * ...
  54255. *
  54256. * If Proxy doesn't have a trap for the call ('apply' or 'construct'),
  54257. * replace Proxy object with target object.
  54258. *
  54259. * If we're dealing with a normal call and the Proxy has an 'apply'
  54260. * trap, manipulate value stack to:
  54261. *
  54262. * idx_func + 0: trap
  54263. * idx_func + 1: Proxy's handler
  54264. * idx_func + 2: Proxy's target
  54265. * idx_func + 3: this binding for call (from idx_func + 1)
  54266. * idx_func + 4: call arguments packed to an array
  54267. *
  54268. * If we're dealing with a constructor call and the Proxy has a
  54269. * 'construct' trap, manipulate value stack to:
  54270. *
  54271. * idx_func + 0: trap
  54272. * idx_func + 1: Proxy's handler
  54273. * idx_func + 2: Proxy's target
  54274. * idx_func + 3: call arguments packed to an array
  54275. * idx_func + 4: newTarget == Proxy object here
  54276. *
  54277. * As we don't yet have proper newTarget support, the newTarget at
  54278. * idx_func + 3 is just the original constructor being called, i.e.
  54279. * the Proxy object (not the target). Note that the default instance
  54280. * (original 'this' binding) is dropped and ignored.
  54281. */
  54282. duk_push_hobject(thr, h_proxy->handler);
  54283. rc = duk_get_prop_stridx_short(thr, -1, (*call_flags & DUK_CALL_FLAG_CONSTRUCT) ? DUK_STRIDX_CONSTRUCT : DUK_STRIDX_APPLY);
  54284. if (rc == 0) {
  54285. /* Not found, continue to target. If this is a construct
  54286. * call, update default instance prototype using the Proxy,
  54287. * not the target.
  54288. */
  54289. if (*call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  54290. if (!(*call_flags & DUK_CALL_FLAG_DEFAULT_INSTANCE_UPDATED)) {
  54291. *call_flags |= DUK_CALL_FLAG_DEFAULT_INSTANCE_UPDATED;
  54292. duk__update_default_instance_proto(thr, idx_func);
  54293. }
  54294. }
  54295. duk_pop_2(thr);
  54296. duk_push_hobject(thr, h_proxy->target);
  54297. duk_replace(thr, idx_func);
  54298. return;
  54299. }
  54300. /* Here we must be careful not to replace idx_func while
  54301. * h_proxy is still needed, otherwise h_proxy may become
  54302. * dangling. This could be improved e.g. using a
  54303. * duk_pack_slice() with a freeform slice.
  54304. */
  54305. /* Here:
  54306. * idx_func + 0: Proxy object
  54307. * idx_func + 1: this binding for call
  54308. * idx_func + 2: 1st argument for call
  54309. * idx_func + 3: 2nd argument for call
  54310. * ...
  54311. * idx_func + N: handler
  54312. * idx_func + N + 1: trap
  54313. */
  54314. duk_insert(thr, idx_func + 1);
  54315. duk_insert(thr, idx_func + 2);
  54316. duk_push_hobject(thr, h_proxy->target);
  54317. duk_insert(thr, idx_func + 3);
  54318. duk_pack(thr, duk_get_top(thr) - (idx_func + 5));
  54319. /* Here:
  54320. * idx_func + 0: Proxy object
  54321. * idx_func + 1: trap
  54322. * idx_func + 2: Proxy's handler
  54323. * idx_func + 3: Proxy's target
  54324. * idx_func + 4: this binding for call
  54325. * idx_func + 5: arguments array
  54326. */
  54327. DUK_ASSERT(duk_get_top(thr) == idx_func + 6);
  54328. if (*call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  54329. *call_flags |= DUK_CALL_FLAG_CONSTRUCT_PROXY; /* Enable 'construct' trap return invariant check. */
  54330. *call_flags &= ~(DUK_CALL_FLAG_CONSTRUCT); /* Resume as non-constructor call to the trap. */
  54331. /* 'apply' args: target, thisArg, argArray
  54332. * 'construct' args: target, argArray, newTarget
  54333. */
  54334. duk_remove(thr, idx_func + 4);
  54335. duk_push_hobject(thr, (duk_hobject *) h_proxy);
  54336. }
  54337. /* Finalize value stack layout by removing Proxy reference. */
  54338. duk_remove(thr, idx_func);
  54339. h_proxy = NULL; /* invalidated */
  54340. DUK_ASSERT(duk_get_top(thr) == idx_func + 5);
  54341. }
  54342. #endif /* DUK_USE_ES6_PROXY */
  54343. /*
  54344. * Helper for setting up var_env and lex_env of an activation,
  54345. * assuming it does NOT have the DUK_HOBJECT_FLAG_NEWENV flag.
  54346. */
  54347. DUK_LOCAL void duk__handle_oldenv_for_call(duk_hthread *thr,
  54348. duk_hobject *func,
  54349. duk_activation *act) {
  54350. duk_hcompfunc *f;
  54351. duk_hobject *h_lex;
  54352. duk_hobject *h_var;
  54353. DUK_ASSERT(thr != NULL);
  54354. DUK_ASSERT(func != NULL);
  54355. DUK_ASSERT(act != NULL);
  54356. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV(func));
  54357. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
  54358. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(func));
  54359. DUK_UNREF(thr);
  54360. f = (duk_hcompfunc *) func;
  54361. h_lex = DUK_HCOMPFUNC_GET_LEXENV(thr->heap, f);
  54362. h_var = DUK_HCOMPFUNC_GET_VARENV(thr->heap, f);
  54363. DUK_ASSERT(h_lex != NULL); /* Always true for closures (not for templates) */
  54364. DUK_ASSERT(h_var != NULL);
  54365. act->lex_env = h_lex;
  54366. act->var_env = h_var;
  54367. DUK_HOBJECT_INCREF(thr, h_lex);
  54368. DUK_HOBJECT_INCREF(thr, h_var);
  54369. }
  54370. /*
  54371. * Helper for updating callee 'caller' property.
  54372. */
  54373. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  54374. DUK_LOCAL void duk__update_func_caller_prop(duk_hthread *thr, duk_hobject *func) {
  54375. duk_tval *tv_caller;
  54376. duk_hobject *h_tmp;
  54377. duk_activation *act_callee;
  54378. duk_activation *act_caller;
  54379. DUK_ASSERT(thr != NULL);
  54380. DUK_ASSERT(func != NULL);
  54381. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(func)); /* bound chain resolved */
  54382. DUK_ASSERT(thr->callstack_top >= 1);
  54383. if (DUK_HOBJECT_HAS_STRICT(func)) {
  54384. /* Strict functions don't get their 'caller' updated. */
  54385. return;
  54386. }
  54387. DUK_ASSERT(thr->callstack_top > 0);
  54388. act_callee = thr->callstack_curr;
  54389. DUK_ASSERT(act_callee != NULL);
  54390. act_caller = (thr->callstack_top >= 2 ? act_callee->parent : NULL);
  54391. /* XXX: check .caller writability? */
  54392. /* Backup 'caller' property and update its value. */
  54393. tv_caller = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_CALLER(thr));
  54394. if (tv_caller) {
  54395. /* If caller is global/eval code, 'caller' should be set to
  54396. * 'null'.
  54397. *
  54398. * XXX: there is no exotic flag to infer this correctly now.
  54399. * The NEWENV flag is used now which works as intended for
  54400. * everything (global code, non-strict eval code, and functions)
  54401. * except strict eval code. Bound functions are never an issue
  54402. * because 'func' has been resolved to a non-bound function.
  54403. */
  54404. if (act_caller != NULL) {
  54405. /* act_caller->func may be NULL in some finalization cases,
  54406. * just treat like we don't know the caller.
  54407. */
  54408. if (act_caller->func && !DUK_HOBJECT_HAS_NEWENV(act_caller->func)) {
  54409. /* Setting to NULL causes 'caller' to be set to
  54410. * 'null' as desired.
  54411. */
  54412. act_caller = NULL;
  54413. }
  54414. }
  54415. if (DUK_TVAL_IS_OBJECT(tv_caller)) {
  54416. h_tmp = DUK_TVAL_GET_OBJECT(tv_caller);
  54417. DUK_ASSERT(h_tmp != NULL);
  54418. act_callee->prev_caller = h_tmp;
  54419. /* Previous value doesn't need refcount changes because its ownership
  54420. * is transferred to prev_caller.
  54421. */
  54422. if (act_caller != NULL) {
  54423. DUK_ASSERT(act_caller->func != NULL);
  54424. DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
  54425. DUK_TVAL_INCREF(thr, tv_caller);
  54426. } else {
  54427. DUK_TVAL_SET_NULL(tv_caller); /* no incref */
  54428. }
  54429. } else {
  54430. /* 'caller' must only take on 'null' or function value */
  54431. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_caller));
  54432. DUK_ASSERT(act_callee->prev_caller == NULL);
  54433. if (act_caller != NULL && act_caller->func) {
  54434. /* Tolerate act_caller->func == NULL which happens in
  54435. * some finalization cases; treat like unknown caller.
  54436. */
  54437. DUK_TVAL_SET_OBJECT(tv_caller, act_caller->func);
  54438. DUK_TVAL_INCREF(thr, tv_caller);
  54439. } else {
  54440. DUK_TVAL_SET_NULL(tv_caller); /* no incref */
  54441. }
  54442. }
  54443. }
  54444. }
  54445. #endif /* DUK_USE_NONSTD_FUNC_CALLER_PROPERTY */
  54446. /*
  54447. * Shared helpers for resolving the final, non-bound target function of the
  54448. * call and the effective 'this' binding. Resolves bound functions and
  54449. * applies .call(), .apply(), and .construct() inline.
  54450. *
  54451. * Proxy traps are also handled inline so that if the target is a Proxy with
  54452. * a 'call' or 'construct' trap, the trap handler is called with a modified
  54453. * argument list.
  54454. *
  54455. * Once the bound function / .call() / .apply() / .construct() sequence has
  54456. * been resolved, the value at idx_func + 1 may need coercion described in
  54457. * E5 Section 10.4.3.
  54458. *
  54459. * A call that begins as a non-constructor call may be converted into a
  54460. * constructor call during the resolution process if Reflect.construct()
  54461. * is invoked. This is handled by updating the caller's call_flags.
  54462. *
  54463. * For global and eval code (E5 Sections 10.4.1 and 10.4.2), we assume
  54464. * that the caller has provided the correct 'this' binding explicitly
  54465. * when calling, i.e.:
  54466. *
  54467. * - global code: this=global object
  54468. * - direct eval: this=copy from eval() caller's this binding
  54469. * - other eval: this=global object
  54470. *
  54471. * The 'this' coercion may cause a recursive function call with arbitrary
  54472. * side effects, because ToObject() may be called.
  54473. */
  54474. DUK_LOCAL DUK_INLINE void duk__coerce_nonstrict_this_binding(duk_hthread *thr, duk_idx_t idx_this) {
  54475. duk_tval *tv_this;
  54476. duk_hobject *obj_global;
  54477. tv_this = thr->valstack_bottom + idx_this;
  54478. switch (DUK_TVAL_GET_TAG(tv_this)) {
  54479. case DUK_TAG_OBJECT:
  54480. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, object -> use directly"));
  54481. break;
  54482. case DUK_TAG_UNDEFINED:
  54483. case DUK_TAG_NULL:
  54484. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, undefined/null -> use global object"));
  54485. obj_global = thr->builtins[DUK_BIDX_GLOBAL];
  54486. /* XXX: avoid this check somehow */
  54487. if (DUK_LIKELY(obj_global != NULL)) {
  54488. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_this)); /* no need to decref previous value */
  54489. DUK_TVAL_SET_OBJECT(tv_this, obj_global);
  54490. DUK_HOBJECT_INCREF(thr, obj_global);
  54491. } else {
  54492. /* This may only happen if built-ins are being "torn down".
  54493. * This behavior is out of specification scope.
  54494. */
  54495. DUK_D(DUK_DPRINT("this binding: wanted to use global object, but it is NULL -> using undefined instead"));
  54496. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv_this)); /* no need to decref previous value */
  54497. DUK_TVAL_SET_UNDEFINED(tv_this); /* nothing to incref */
  54498. }
  54499. break;
  54500. default:
  54501. /* Plain buffers and lightfuncs are object coerced. Lightfuncs
  54502. * very rarely come here however, because the call target would
  54503. * need to be a non-strict non-lightfunc (lightfuncs are considered
  54504. * strict) with an explicit lightfunc 'this' binding.
  54505. */
  54506. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_this));
  54507. DUK_DDD(DUK_DDDPRINT("this binding: non-strict, not object/undefined/null -> use ToObject(value)"));
  54508. duk_to_object(thr, idx_this); /* may have side effects */
  54509. break;
  54510. }
  54511. }
  54512. DUK_LOCAL DUK_ALWAYS_INLINE duk_bool_t duk__resolve_target_fastpath_check(duk_hthread *thr, duk_idx_t idx_func, duk_hobject **out_func, duk_small_uint_t call_flags) {
  54513. #if defined(DUK_USE_PREFER_SIZE)
  54514. DUK_UNREF(thr);
  54515. DUK_UNREF(idx_func);
  54516. DUK_UNREF(out_func);
  54517. DUK_UNREF(call_flags);
  54518. #else /* DUK_USE_PREFER_SIZE */
  54519. duk_tval *tv_func;
  54520. duk_hobject *func;
  54521. if (DUK_UNLIKELY(call_flags & DUK_CALL_FLAG_CONSTRUCT)) {
  54522. return 0;
  54523. }
  54524. tv_func = DUK_GET_TVAL_POSIDX(thr, idx_func);
  54525. DUK_ASSERT(tv_func != NULL);
  54526. if (DUK_LIKELY(DUK_TVAL_IS_OBJECT(tv_func))) {
  54527. func = DUK_TVAL_GET_OBJECT(tv_func);
  54528. if (DUK_HOBJECT_IS_CALLABLE(func) &&
  54529. !DUK_HOBJECT_HAS_BOUNDFUNC(func) &&
  54530. !DUK_HOBJECT_HAS_SPECIAL_CALL(func)) {
  54531. *out_func = func;
  54532. if (DUK_HOBJECT_HAS_STRICT(func)) {
  54533. /* Strict function: no 'this' coercion. */
  54534. return 1;
  54535. }
  54536. duk__coerce_nonstrict_this_binding(thr, idx_func + 1);
  54537. return 1;
  54538. }
  54539. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  54540. *out_func = NULL;
  54541. /* Lightfuncs are considered strict, so 'this' binding is
  54542. * used as is. They're never bound, always constructable,
  54543. * and never special functions.
  54544. */
  54545. return 1;
  54546. }
  54547. #endif /* DUK_USE_PREFER_SIZE */
  54548. return 0; /* let slow path deal with it */
  54549. }
  54550. DUK_LOCAL duk_hobject *duk__resolve_target_func_and_this_binding(duk_hthread *thr,
  54551. duk_idx_t idx_func,
  54552. duk_small_uint_t *call_flags) {
  54553. duk_tval *tv_func;
  54554. duk_hobject *func;
  54555. duk_bool_t first;
  54556. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54557. for (first = 1;; first = 0) {
  54558. DUK_ASSERT(duk_get_top(thr) >= idx_func + 2);
  54559. tv_func = DUK_GET_TVAL_POSIDX(thr, idx_func);
  54560. DUK_ASSERT(tv_func != NULL);
  54561. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  54562. func = DUK_TVAL_GET_OBJECT(tv_func);
  54563. if (*call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  54564. if (DUK_UNLIKELY(!DUK_HOBJECT_HAS_CONSTRUCTABLE(func))) {
  54565. goto not_constructable;
  54566. }
  54567. } else {
  54568. if (DUK_UNLIKELY(!DUK_HOBJECT_IS_CALLABLE(func))) {
  54569. goto not_callable;
  54570. }
  54571. }
  54572. if (DUK_LIKELY(!DUK_HOBJECT_HAS_BOUNDFUNC(func) &&
  54573. !DUK_HOBJECT_HAS_SPECIAL_CALL(func) &&
  54574. !DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(func))) {
  54575. /* Common case, so test for using a single bitfield test.
  54576. * Break out to handle this coercion etc.
  54577. */
  54578. break;
  54579. }
  54580. /* XXX: could set specialcall for boundfuncs too, simplify check above */
  54581. if (DUK_HOBJECT_HAS_BOUNDFUNC(func)) {
  54582. DUK_ASSERT(!DUK_HOBJECT_HAS_SPECIAL_CALL(func));
  54583. DUK_ASSERT(!DUK_HOBJECT_IS_NATFUNC(func));
  54584. /* Callable/constructable flags are the same
  54585. * for the bound function and its target, so
  54586. * we don't need to check them here, we can
  54587. * check them from the target only.
  54588. */
  54589. duk__handle_bound_chain_for_call(thr, idx_func, *call_flags & DUK_CALL_FLAG_CONSTRUCT);
  54590. DUK_ASSERT(DUK_TVAL_IS_OBJECT(duk_require_tval(thr, idx_func)) ||
  54591. DUK_TVAL_IS_LIGHTFUNC(duk_require_tval(thr, idx_func)));
  54592. } else {
  54593. DUK_ASSERT(DUK_HOBJECT_HAS_SPECIAL_CALL(func));
  54594. #if defined(DUK_USE_ES6_PROXY)
  54595. if (DUK_HOBJECT_HAS_EXOTIC_PROXYOBJ(func)) {
  54596. /* If no trap, resume processing from Proxy trap.
  54597. * If trap exists, helper converts call into a trap
  54598. * call; this may change a constructor call into a
  54599. * normal (non-constructor) trap call. We must
  54600. * continue processing even when a trap is found as
  54601. * the trap may be bound.
  54602. */
  54603. duk__handle_proxy_for_call(thr, idx_func, (duk_hproxy *) func, call_flags);
  54604. }
  54605. else
  54606. #endif
  54607. {
  54608. DUK_ASSERT(DUK_HOBJECT_IS_NATFUNC(func));
  54609. DUK_ASSERT(DUK_HOBJECT_HAS_CALLABLE(func));
  54610. DUK_ASSERT(!DUK_HOBJECT_HAS_CONSTRUCTABLE(func));
  54611. /* Constructable check already done above. */
  54612. if (duk__handle_specialfuncs_for_call(thr, idx_func, func, call_flags, first) != 0) {
  54613. /* Encountered native eval call, normal call
  54614. * context. Break out, handle this coercion etc.
  54615. */
  54616. break;
  54617. }
  54618. }
  54619. }
  54620. /* Retry loop. */
  54621. } else if (DUK_TVAL_IS_LIGHTFUNC(tv_func)) {
  54622. /* Lightfuncs are:
  54623. * - Always strict, so no 'this' coercion.
  54624. * - Always callable.
  54625. * - Always constructable.
  54626. * - Never specialfuncs.
  54627. */
  54628. func = NULL;
  54629. goto finished;
  54630. } else {
  54631. goto not_callable;
  54632. }
  54633. }
  54634. DUK_ASSERT(func != NULL);
  54635. if (!DUK_HOBJECT_HAS_STRICT(func)) {
  54636. /* Non-strict target needs 'this' coercion.
  54637. * This has potential side effects invalidating
  54638. * 'tv_func'.
  54639. */
  54640. duk__coerce_nonstrict_this_binding(thr, idx_func + 1);
  54641. }
  54642. if (*call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  54643. if (!(*call_flags & DUK_CALL_FLAG_DEFAULT_INSTANCE_UPDATED)) {
  54644. *call_flags |= DUK_CALL_FLAG_DEFAULT_INSTANCE_UPDATED;
  54645. duk__update_default_instance_proto(thr, idx_func);
  54646. }
  54647. }
  54648. finished:
  54649. #if defined(DUK_USE_ASSERTIONS)
  54650. {
  54651. duk_tval *tv_tmp;
  54652. tv_tmp = duk_get_tval(thr, idx_func);
  54653. DUK_ASSERT(tv_tmp != NULL);
  54654. DUK_ASSERT((DUK_TVAL_IS_OBJECT(tv_tmp) && DUK_HOBJECT_IS_CALLABLE(DUK_TVAL_GET_OBJECT(tv_tmp))) ||
  54655. DUK_TVAL_IS_LIGHTFUNC(tv_tmp));
  54656. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUNDFUNC(func));
  54657. DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPFUNC(func) ||
  54658. DUK_HOBJECT_IS_NATFUNC(func)));
  54659. DUK_ASSERT(func == NULL || (DUK_HOBJECT_HAS_CONSTRUCTABLE(func) ||
  54660. (*call_flags & DUK_CALL_FLAG_CONSTRUCT) == 0));
  54661. }
  54662. #endif
  54663. return func;
  54664. not_callable:
  54665. DUK_ASSERT(tv_func != NULL);
  54666. #if defined(DUK_USE_VERBOSE_ERRORS)
  54667. /* GETPROPC delayed error handling: when target is not callable,
  54668. * GETPROPC replaces idx_func+0 with an Error (non-callable) with
  54669. * a hidden Symbol to signify it's to be thrown as is here. The
  54670. * hidden Symbol is only checked as an own property, not inherited
  54671. * (which would be dangerous).
  54672. */
  54673. if (DUK_TVAL_IS_OBJECT(tv_func)) {
  54674. if (duk_hobject_find_existing_entry_tval_ptr(thr->heap, DUK_TVAL_GET_OBJECT(tv_func), DUK_HTHREAD_STRING_INT_TARGET(thr)) != NULL) {
  54675. duk_push_tval(thr, tv_func);
  54676. (void) duk_throw(thr);
  54677. DUK_WO_NORETURN(return NULL;);
  54678. }
  54679. }
  54680. #endif
  54681. #if defined(DUK_USE_VERBOSE_ERRORS)
  54682. #if defined(DUK_USE_PARANOID_ERRORS)
  54683. DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "%s not callable", duk_get_type_name(thr, idx_func));
  54684. #else
  54685. DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "%s not callable", duk_push_string_tval_readable(thr, tv_func));
  54686. #endif
  54687. #else
  54688. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CALLABLE);
  54689. #endif
  54690. DUK_WO_NORETURN(return NULL;);
  54691. not_constructable:
  54692. /* For now GETPROPC delayed error not needed for constructor calls. */
  54693. #if defined(DUK_USE_VERBOSE_ERRORS)
  54694. #if defined(DUK_USE_PARANOID_ERRORS)
  54695. DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "%s not constructable", duk_get_type_name(thr, idx_func));
  54696. #else
  54697. DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "%s not constructable", duk_push_string_tval_readable(thr, tv_func));
  54698. #endif
  54699. #else
  54700. DUK_ERROR_TYPE(thr, DUK_STR_NOT_CONSTRUCTABLE);
  54701. #endif
  54702. DUK_WO_NORETURN(return NULL;);
  54703. }
  54704. /*
  54705. * Manipulate value stack so that exactly 'num_stack_rets' return
  54706. * values are at 'idx_retbase' in every case, assuming there are
  54707. * 'rc' return values on top of stack.
  54708. *
  54709. * This is a bit tricky, because the called C function operates in
  54710. * the same activation record and may have e.g. popped the stack
  54711. * empty (below idx_retbase).
  54712. */
  54713. DUK_LOCAL void duk__safe_call_adjust_valstack(duk_hthread *thr, duk_idx_t idx_retbase, duk_idx_t num_stack_rets, duk_idx_t num_actual_rets) {
  54714. duk_idx_t idx_rcbase;
  54715. DUK_ASSERT(thr != NULL);
  54716. DUK_ASSERT(idx_retbase >= 0);
  54717. DUK_ASSERT(num_stack_rets >= 0);
  54718. DUK_ASSERT(num_actual_rets >= 0);
  54719. idx_rcbase = duk_get_top(thr) - num_actual_rets; /* base of known return values */
  54720. if (DUK_UNLIKELY(idx_rcbase < 0)) {
  54721. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_CFUNC_RC);
  54722. DUK_WO_NORETURN(return;);
  54723. }
  54724. DUK_DDD(DUK_DDDPRINT("adjust valstack after func call: "
  54725. "num_stack_rets=%ld, num_actual_rets=%ld, stack_top=%ld, idx_retbase=%ld, idx_rcbase=%ld",
  54726. (long) num_stack_rets, (long) num_actual_rets, (long) duk_get_top(thr),
  54727. (long) idx_retbase, (long) idx_rcbase));
  54728. DUK_ASSERT(idx_rcbase >= 0); /* caller must check */
  54729. /* Space for num_stack_rets was reserved before the safe call.
  54730. * Because value stack reserve cannot shrink except in call returns,
  54731. * the reserve is still in place. Adjust valstack, carefully
  54732. * ensuring we don't overstep the reserve.
  54733. */
  54734. /* Match idx_rcbase with idx_retbase so that the return values
  54735. * start at the correct index.
  54736. */
  54737. if (idx_rcbase > idx_retbase) {
  54738. duk_idx_t count = idx_rcbase - idx_retbase;
  54739. DUK_DDD(DUK_DDDPRINT("elements at/after idx_retbase have enough to cover func retvals "
  54740. "(idx_retbase=%ld, idx_rcbase=%ld)", (long) idx_retbase, (long) idx_rcbase));
  54741. /* Remove values between irc_rcbase (start of intended return
  54742. * values) and idx_retbase to lower return values to idx_retbase.
  54743. */
  54744. DUK_ASSERT(count > 0);
  54745. duk_remove_n(thr, idx_retbase, count); /* may be NORZ */
  54746. } else {
  54747. duk_idx_t count = idx_retbase - idx_rcbase;
  54748. DUK_DDD(DUK_DDDPRINT("not enough elements at/after idx_retbase to cover func retvals "
  54749. "(idx_retbase=%ld, idx_rcbase=%ld)", (long) idx_retbase, (long) idx_rcbase));
  54750. /* Insert 'undefined' at idx_rcbase (start of intended return
  54751. * values) to lift return values to idx_retbase.
  54752. */
  54753. DUK_ASSERT(count >= 0);
  54754. DUK_ASSERT(thr->valstack_end - thr->valstack_top >= count); /* reserve cannot shrink */
  54755. duk_insert_undefined_n(thr, idx_rcbase, count);
  54756. }
  54757. /* Chop extra retvals away / extend with undefined. */
  54758. duk_set_top_unsafe(thr, idx_retbase + num_stack_rets);
  54759. }
  54760. /*
  54761. * Activation setup for tailcalls and non-tailcalls.
  54762. */
  54763. #if defined(DUK_USE_TAILCALL)
  54764. DUK_LOCAL duk_small_uint_t duk__call_setup_act_attempt_tailcall(duk_hthread *thr,
  54765. duk_small_uint_t call_flags,
  54766. duk_idx_t idx_func,
  54767. duk_hobject *func,
  54768. duk_size_t entry_valstack_bottom_byteoff,
  54769. duk_size_t entry_valstack_end_byteoff,
  54770. duk_idx_t *out_nargs,
  54771. duk_idx_t *out_nregs,
  54772. duk_size_t *out_vs_min_bytes,
  54773. duk_activation **out_act) {
  54774. duk_activation *act;
  54775. duk_tval *tv1, *tv2;
  54776. duk_idx_t idx_args;
  54777. duk_small_uint_t flags1, flags2;
  54778. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  54779. duk_activation *prev_pause_act;
  54780. #endif
  54781. DUK_UNREF(entry_valstack_end_byteoff);
  54782. /* Tailcall cannot be flagged to resume calls, and a
  54783. * previous frame must exist.
  54784. */
  54785. DUK_ASSERT(thr->callstack_top >= 1);
  54786. act = thr->callstack_curr;
  54787. DUK_ASSERT(act != NULL);
  54788. *out_act = act;
  54789. if (func == NULL || !DUK_HOBJECT_IS_COMPFUNC(func)) {
  54790. DUK_DDD(DUK_DDDPRINT("tail call prevented by target not being ecma function"));
  54791. return 0;
  54792. }
  54793. if (act->flags & DUK_ACT_FLAG_PREVENT_YIELD) {
  54794. DUK_DDD(DUK_DDDPRINT("tail call prevented by current activation having DUK_ACT_FLAG_PREVENT_YIELD"));
  54795. return 0;
  54796. }
  54797. /* Tailcall is only allowed if current and candidate
  54798. * function have identical return value handling. There
  54799. * are three possible return value handling cases:
  54800. * 1. Normal function call, no special return value handling.
  54801. * 2. Constructor call, return value replacement object check.
  54802. * 3. Proxy 'construct' trap call, return value invariant check.
  54803. */
  54804. flags1 = (duk_small_uint_t) ((act->flags & DUK_ACT_FLAG_CONSTRUCT) ? 1 : 0)
  54805. #if defined(DUK_USE_ES6_PROXY)
  54806. | (duk_small_uint_t) ((act->flags & DUK_ACT_FLAG_CONSTRUCT_PROXY) ? 2 : 0)
  54807. #endif
  54808. ;
  54809. flags2 = (duk_small_uint_t) ((call_flags & DUK_CALL_FLAG_CONSTRUCT) ? 1 : 0)
  54810. #if defined(DUK_USE_ES6_PROXY)
  54811. | (duk_small_uint_t) ((call_flags & DUK_CALL_FLAG_CONSTRUCT_PROXY) ? 2 : 0);
  54812. #endif
  54813. ;
  54814. if (flags1 != flags2) {
  54815. DUK_DDD(DUK_DDDPRINT("tail call prevented by incompatible return value handling"));
  54816. return 0;
  54817. }
  54818. DUK_ASSERT(((act->flags & DUK_ACT_FLAG_CONSTRUCT) && (call_flags & DUK_CALL_FLAG_CONSTRUCT)) ||
  54819. (!(act->flags & DUK_ACT_FLAG_CONSTRUCT) && !(call_flags & DUK_CALL_FLAG_CONSTRUCT)));
  54820. DUK_ASSERT(((act->flags & DUK_ACT_FLAG_CONSTRUCT_PROXY) && (call_flags & DUK_CALL_FLAG_CONSTRUCT_PROXY)) ||
  54821. (!(act->flags & DUK_ACT_FLAG_CONSTRUCT_PROXY) && !(call_flags & DUK_CALL_FLAG_CONSTRUCT_PROXY)));
  54822. if (DUK_HOBJECT_HAS_NOTAIL(func)) {
  54823. /* See: test-bug-tailcall-preventyield-assert.c. */
  54824. DUK_DDD(DUK_DDDPRINT("tail call prevented by function having a notail flag"));
  54825. return 0;
  54826. }
  54827. /*
  54828. * Tailcall handling
  54829. *
  54830. * Although the callstack entry is reused, we need to explicitly unwind
  54831. * the current activation (or simulate an unwind). In particular, the
  54832. * current activation must be closed, otherwise something like
  54833. * test-bug-reduce-judofyr.js results. Also catchers need to be unwound
  54834. * because there may be non-error-catching label entries in valid tail calls.
  54835. *
  54836. * Special attention is needed for debugger and pause behavior when
  54837. * reusing an activation.
  54838. * - Disable StepOut processing for the activation unwind because
  54839. * we reuse the activation, see:
  54840. * https://github.com/svaarala/duktape/issues/1684.
  54841. * - Disable line change pause flag permanently if act == dbg_pause_act
  54842. * (if set) because it would no longer be relevant, see:
  54843. * https://github.com/svaarala/duktape/issues/1726,
  54844. * https://github.com/svaarala/duktape/issues/1786.
  54845. * - Check for function entry (e.g. StepInto) pause flag here, because
  54846. * the executor pause check won't trigger due to shared activation, see:
  54847. * https://github.com/svaarala/duktape/issues/1726.
  54848. */
  54849. DUK_DDD(DUK_DDDPRINT("is tail call, reusing activation at callstack top, at index %ld",
  54850. (long) (thr->callstack_top - 1)));
  54851. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(func));
  54852. DUK_ASSERT(!DUK_HOBJECT_HAS_NATFUNC(func));
  54853. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(func));
  54854. DUK_ASSERT((act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0);
  54855. DUK_ASSERT(call_flags & DUK_CALL_FLAG_ALLOW_ECMATOECMA);
  54856. /* Unwind the topmost callstack entry before reusing it. This
  54857. * also unwinds the catchers related to the topmost entry.
  54858. */
  54859. DUK_ASSERT(thr->callstack_top > 0);
  54860. DUK_ASSERT(thr->callstack_curr != NULL);
  54861. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  54862. if (act == thr->heap->dbg_pause_act) {
  54863. thr->heap->dbg_pause_flags &= ~DUK_PAUSE_FLAG_LINE_CHANGE;
  54864. }
  54865. prev_pause_act = thr->heap->dbg_pause_act;
  54866. thr->heap->dbg_pause_act = NULL;
  54867. if (thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_FUNC_ENTRY) {
  54868. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by function entry (tailcall)"));
  54869. duk_debug_set_paused(thr->heap);
  54870. }
  54871. #endif
  54872. duk_hthread_activation_unwind_reuse_norz(thr);
  54873. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  54874. thr->heap->dbg_pause_act = prev_pause_act;
  54875. #endif
  54876. DUK_ASSERT(act == thr->callstack_curr);
  54877. /* XXX: We could restore the caller's value stack reserve
  54878. * here, as if we did an actual unwind-and-call. Without
  54879. * the restoration, value stack reserve may remain higher
  54880. * than would otherwise be possible until we return to a
  54881. * non-tailcall.
  54882. */
  54883. /* Then reuse the unwound activation. */
  54884. act->cat = NULL;
  54885. act->var_env = NULL;
  54886. act->lex_env = NULL;
  54887. DUK_ASSERT(func != NULL);
  54888. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(func));
  54889. act->func = func; /* don't want an intermediate exposed state with func == NULL */
  54890. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  54891. act->prev_caller = NULL;
  54892. #endif
  54893. /* don't want an intermediate exposed state with invalid pc */
  54894. act->curr_pc = DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, (duk_hcompfunc *) func);
  54895. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  54896. act->prev_line = 0;
  54897. #endif
  54898. DUK_TVAL_SET_OBJECT(&act->tv_func, func); /* borrowed, no refcount */
  54899. DUK_HOBJECT_INCREF(thr, func);
  54900. act->flags = DUK_ACT_FLAG_TAILCALLED;
  54901. if (DUK_HOBJECT_HAS_STRICT(func)) {
  54902. act->flags |= DUK_ACT_FLAG_STRICT;
  54903. }
  54904. if (call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  54905. act->flags |= DUK_ACT_FLAG_CONSTRUCT;
  54906. }
  54907. #if defined(DUK_USE_ES6_PROXY)
  54908. if (call_flags & DUK_CALL_FLAG_CONSTRUCT_PROXY) {
  54909. act->flags |= DUK_ACT_FLAG_CONSTRUCT_PROXY;
  54910. }
  54911. #endif
  54912. DUK_ASSERT(DUK_ACT_GET_FUNC(act) == func); /* already updated */
  54913. DUK_ASSERT(act->var_env == NULL);
  54914. DUK_ASSERT(act->lex_env == NULL);
  54915. act->bottom_byteoff = entry_valstack_bottom_byteoff; /* tail call -> reuse current "frame" */
  54916. #if 0
  54917. /* Topmost activation retval_byteoff is considered garbage, no need to init. */
  54918. act->retval_byteoff = 0;
  54919. #endif
  54920. /* Filled in when final reserve is known, dummy value doesn't matter
  54921. * even in error unwind because reserve_byteoff is only used when
  54922. * returning to -this- activation.
  54923. */
  54924. act->reserve_byteoff = 0;
  54925. /*
  54926. * Manipulate valstack so that args are on the current bottom and the
  54927. * previous caller's 'this' binding (which is the value preceding the
  54928. * current bottom) is replaced with the new 'this' binding:
  54929. *
  54930. * [ ... this_old | (crud) func this_new arg1 ... argN ]
  54931. * --> [ ... this_new | arg1 ... argN ]
  54932. *
  54933. * For tail calling to work properly, the valstack bottom must not grow
  54934. * here; otherwise crud would accumulate on the valstack.
  54935. */
  54936. tv1 = thr->valstack_bottom - 1;
  54937. tv2 = thr->valstack_bottom + idx_func + 1;
  54938. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top); /* tv1 is -below- valstack_bottom */
  54939. DUK_ASSERT(tv2 >= thr->valstack_bottom && tv2 < thr->valstack_top);
  54940. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  54941. idx_args = idx_func + 2;
  54942. duk_remove_n(thr, 0, idx_args); /* may be NORZ */
  54943. idx_func = 0; DUK_UNREF(idx_func); /* really 'not applicable' anymore, should not be referenced after this */
  54944. idx_args = 0;
  54945. *out_nargs = ((duk_hcompfunc *) func)->nargs;
  54946. *out_nregs = ((duk_hcompfunc *) func)->nregs;
  54947. DUK_ASSERT(*out_nregs >= 0);
  54948. DUK_ASSERT(*out_nregs >= *out_nargs);
  54949. *out_vs_min_bytes = entry_valstack_bottom_byteoff + sizeof(duk_tval) * ((duk_size_t) *out_nregs + DUK_VALSTACK_INTERNAL_EXTRA);
  54950. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  54951. #if defined(DUK_USE_TAILCALL)
  54952. #error incorrect options: tail calls enabled with function caller property
  54953. #endif
  54954. /* XXX: This doesn't actually work properly for tail calls, so
  54955. * tail calls are disabled when DUK_USE_NONSTD_FUNC_CALLER_PROPERTY
  54956. * is in use.
  54957. */
  54958. duk__update_func_caller_prop(thr, func);
  54959. #endif
  54960. /* [ ... this_new | arg1 ... argN ] */
  54961. return 1;
  54962. }
  54963. #endif /* DUK_USE_TAILCALL */
  54964. DUK_LOCAL void duk__call_setup_act_not_tailcall(duk_hthread *thr,
  54965. duk_small_uint_t call_flags,
  54966. duk_idx_t idx_func,
  54967. duk_hobject *func,
  54968. duk_size_t entry_valstack_bottom_byteoff,
  54969. duk_size_t entry_valstack_end_byteoff,
  54970. duk_idx_t *out_nargs,
  54971. duk_idx_t *out_nregs,
  54972. duk_size_t *out_vs_min_bytes,
  54973. duk_activation **out_act) {
  54974. duk_activation *act;
  54975. duk_activation *new_act;
  54976. DUK_UNREF(entry_valstack_end_byteoff);
  54977. DUK_DDD(DUK_DDDPRINT("not a tail call, pushing a new activation to callstack, to index %ld",
  54978. (long) (thr->callstack_top)));
  54979. duk__call_callstack_limit_check(thr);
  54980. new_act = duk_hthread_activation_alloc(thr);
  54981. DUK_ASSERT(new_act != NULL);
  54982. act = thr->callstack_curr;
  54983. if (act != NULL) {
  54984. /*
  54985. * Update return value stack index of current activation (if any).
  54986. *
  54987. * Although it might seem this is not necessary (bytecode executor
  54988. * does this for ECMAScript-to-ECMAScript calls; other calls are
  54989. * handled here), this turns out to be necessary for handling yield
  54990. * and resume. For them, an ECMAScript-to-native call happens, and
  54991. * the ECMAScript call's retval_byteoff must be set for things to work.
  54992. */
  54993. act->retval_byteoff = entry_valstack_bottom_byteoff + (duk_size_t) idx_func * sizeof(duk_tval);
  54994. }
  54995. new_act->parent = act;
  54996. thr->callstack_curr = new_act;
  54997. thr->callstack_top++;
  54998. act = new_act;
  54999. *out_act = act;
  55000. DUK_ASSERT(thr->valstack_top > thr->valstack_bottom); /* at least effective 'this' */
  55001. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUNDFUNC(func));
  55002. act->cat = NULL;
  55003. act->flags = 0;
  55004. if (call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  55005. act->flags |= DUK_ACT_FLAG_CONSTRUCT;
  55006. }
  55007. #if defined(DUK_USE_ES6_PROXY)
  55008. if (call_flags & DUK_CALL_FLAG_CONSTRUCT_PROXY) {
  55009. act->flags |= DUK_ACT_FLAG_CONSTRUCT_PROXY;
  55010. }
  55011. #endif
  55012. if (call_flags & DUK_CALL_FLAG_DIRECT_EVAL) {
  55013. act->flags |= DUK_ACT_FLAG_DIRECT_EVAL;
  55014. }
  55015. /* start of arguments: idx_func + 2. */
  55016. act->func = func; /* NULL for lightfunc */
  55017. if (DUK_LIKELY(func != NULL)) {
  55018. DUK_TVAL_SET_OBJECT(&act->tv_func, func); /* borrowed, no refcount */
  55019. if (DUK_HOBJECT_HAS_STRICT(func)) {
  55020. act->flags |= DUK_ACT_FLAG_STRICT;
  55021. }
  55022. if (DUK_HOBJECT_IS_COMPFUNC(func)) {
  55023. *out_nargs = ((duk_hcompfunc *) func)->nargs;
  55024. *out_nregs = ((duk_hcompfunc *) func)->nregs;
  55025. DUK_ASSERT(*out_nregs >= 0);
  55026. DUK_ASSERT(*out_nregs >= *out_nargs);
  55027. *out_vs_min_bytes = entry_valstack_bottom_byteoff +
  55028. sizeof(duk_tval) * ((duk_size_t) idx_func + 2U + (duk_size_t) *out_nregs + DUK_VALSTACK_INTERNAL_EXTRA);
  55029. } else {
  55030. /* True because of call target lookup checks. */
  55031. DUK_ASSERT(DUK_HOBJECT_IS_NATFUNC(func));
  55032. *out_nargs = ((duk_hnatfunc *) func)->nargs;
  55033. *out_nregs = *out_nargs;
  55034. if (*out_nargs >= 0) {
  55035. *out_vs_min_bytes = entry_valstack_bottom_byteoff +
  55036. sizeof(duk_tval) * ((duk_size_t) idx_func + 2U + (duk_size_t) *out_nregs + DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  55037. } else {
  55038. /* Vararg function. */
  55039. duk_size_t valstack_top_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - ((duk_uint8_t *) thr->valstack));
  55040. *out_vs_min_bytes = valstack_top_byteoff +
  55041. sizeof(duk_tval) * (DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  55042. }
  55043. }
  55044. } else {
  55045. duk_small_uint_t lf_flags;
  55046. duk_tval *tv_func;
  55047. act->flags |= DUK_ACT_FLAG_STRICT;
  55048. tv_func = DUK_GET_TVAL_POSIDX(thr, idx_func);
  55049. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func));
  55050. DUK_TVAL_SET_TVAL(&act->tv_func, tv_func); /* borrowed, no refcount */
  55051. lf_flags = DUK_TVAL_GET_LIGHTFUNC_FLAGS(tv_func);
  55052. *out_nargs = DUK_LFUNC_FLAGS_GET_NARGS(lf_flags);
  55053. if (*out_nargs != DUK_LFUNC_NARGS_VARARGS) {
  55054. *out_vs_min_bytes = entry_valstack_bottom_byteoff +
  55055. sizeof(duk_tval) * ((duk_size_t) idx_func + 2U + (duk_size_t) *out_nargs + DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  55056. } else {
  55057. duk_size_t valstack_top_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_top - ((duk_uint8_t *) thr->valstack));
  55058. *out_vs_min_bytes = valstack_top_byteoff +
  55059. sizeof(duk_tval) * (DUK_VALSTACK_API_ENTRY_MINIMUM + DUK_VALSTACK_INTERNAL_EXTRA);
  55060. *out_nargs = -1; /* vararg */
  55061. }
  55062. *out_nregs = *out_nargs;
  55063. }
  55064. act->var_env = NULL;
  55065. act->lex_env = NULL;
  55066. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  55067. act->prev_caller = NULL;
  55068. #endif
  55069. act->curr_pc = NULL;
  55070. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  55071. act->prev_line = 0;
  55072. #endif
  55073. act->bottom_byteoff = entry_valstack_bottom_byteoff + sizeof(duk_tval) * ((duk_size_t) idx_func + 2U);
  55074. #if 0
  55075. act->retval_byteoff = 0; /* topmost activation retval_byteoff is considered garbage, no need to init */
  55076. #endif
  55077. /* Filled in when final reserve is known, dummy value doesn't matter
  55078. * even in error unwind because reserve_byteoff is only used when
  55079. * returning to -this- activation.
  55080. */
  55081. act->reserve_byteoff = 0; /* filled in by caller */
  55082. /* XXX: Is this INCREF necessary? 'func' is always a borrowed
  55083. * reference reachable through the value stack? If changed, stack
  55084. * unwind code also needs to be fixed to match.
  55085. */
  55086. DUK_HOBJECT_INCREF_ALLOWNULL(thr, func); /* act->func */
  55087. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  55088. if (func) {
  55089. duk__update_func_caller_prop(thr, func);
  55090. }
  55091. #endif
  55092. }
  55093. /*
  55094. * Environment setup.
  55095. */
  55096. DUK_LOCAL void duk__call_env_setup(duk_hthread *thr, duk_hobject *func, duk_activation *act, duk_idx_t idx_args) {
  55097. duk_hobject *env;
  55098. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUNDFUNC(func)); /* bound function has already been resolved */
  55099. if (DUK_LIKELY(func != NULL)) {
  55100. if (DUK_LIKELY(DUK_HOBJECT_HAS_NEWENV(func))) {
  55101. if (DUK_LIKELY(!DUK_HOBJECT_HAS_CREATEARGS(func))) {
  55102. /* Use a new environment but there's no 'arguments' object;
  55103. * delayed environment initialization. This is the most
  55104. * common case.
  55105. */
  55106. DUK_ASSERT(act->lex_env == NULL);
  55107. DUK_ASSERT(act->var_env == NULL);
  55108. } else {
  55109. /* Use a new environment and there's an 'arguments' object.
  55110. * We need to initialize it right now.
  55111. */
  55112. /* third arg: absolute index (to entire valstack) of bottom_byteoff of new activation */
  55113. env = duk_create_activation_environment_record(thr, func, act->bottom_byteoff);
  55114. DUK_ASSERT(env != NULL);
  55115. /* [ ... func this arg1 ... argN envobj ] */
  55116. DUK_ASSERT(DUK_HOBJECT_HAS_CREATEARGS(func));
  55117. duk__handle_createargs_for_call(thr, func, env, idx_args);
  55118. /* [ ... func this arg1 ... argN envobj ] */
  55119. act->lex_env = env;
  55120. act->var_env = env;
  55121. DUK_HOBJECT_INCREF(thr, env);
  55122. DUK_HOBJECT_INCREF(thr, env); /* XXX: incref by count (2) directly */
  55123. duk_pop(thr);
  55124. }
  55125. } else {
  55126. /* Use existing env (e.g. for non-strict eval); cannot have
  55127. * an own 'arguments' object (but can refer to an existing one).
  55128. */
  55129. DUK_ASSERT(!DUK_HOBJECT_HAS_CREATEARGS(func));
  55130. duk__handle_oldenv_for_call(thr, func, act);
  55131. DUK_ASSERT(act->lex_env != NULL);
  55132. DUK_ASSERT(act->var_env != NULL);
  55133. }
  55134. } else {
  55135. /* Lightfuncs are always native functions and have "newenv". */
  55136. DUK_ASSERT(act->lex_env == NULL);
  55137. DUK_ASSERT(act->var_env == NULL);
  55138. }
  55139. }
  55140. /*
  55141. * Misc shared helpers.
  55142. */
  55143. /* Check thread state, update current thread. */
  55144. DUK_LOCAL void duk__call_thread_state_update(duk_hthread *thr) {
  55145. DUK_ASSERT(thr != NULL);
  55146. if (DUK_LIKELY(thr == thr->heap->curr_thread)) {
  55147. if (DUK_UNLIKELY(thr->state != DUK_HTHREAD_STATE_RUNNING)) {
  55148. /* Should actually never happen, but check anyway. */
  55149. goto thread_state_error;
  55150. }
  55151. } else {
  55152. DUK_ASSERT(thr->heap->curr_thread == NULL ||
  55153. thr->heap->curr_thread->state == DUK_HTHREAD_STATE_RUNNING);
  55154. if (DUK_UNLIKELY(thr->state != DUK_HTHREAD_STATE_INACTIVE)) {
  55155. goto thread_state_error;
  55156. }
  55157. DUK_HEAP_SWITCH_THREAD(thr->heap, thr);
  55158. thr->state = DUK_HTHREAD_STATE_RUNNING;
  55159. /* Multiple threads may be simultaneously in the RUNNING
  55160. * state, but not in the same "resume chain".
  55161. */
  55162. }
  55163. DUK_ASSERT(thr->heap->curr_thread == thr);
  55164. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  55165. return;
  55166. thread_state_error:
  55167. DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "invalid thread state (%ld)", (long) thr->state);
  55168. DUK_WO_NORETURN(return;);
  55169. }
  55170. /*
  55171. * Main unprotected call handler, handles:
  55172. *
  55173. * - All combinations of native/ECMAScript caller and native/ECMAScript
  55174. * target.
  55175. *
  55176. * - Optimized ECMAScript-to-ECMAScript call where call handling only
  55177. * sets up a new duk_activation but reuses an existing bytecode executor
  55178. * (the caller) without native recursion.
  55179. *
  55180. * - Tailcalls, where an activation is reused without increasing call
  55181. * stack (duk_activation) depth.
  55182. *
  55183. * - Setup for an initial Duktape.Thread.resume().
  55184. *
  55185. * The call handler doesn't provide any protection guarantees, protected calls
  55186. * must be implemented e.g. by wrapping the call in a duk_safe_call().
  55187. * Call setup may fail at any stage, even when the new activation is in
  55188. * place; the only guarantee is that the state is consistent for unwinding.
  55189. */
  55190. DUK_LOCAL duk_int_t duk__handle_call_raw(duk_hthread *thr,
  55191. duk_idx_t idx_func,
  55192. duk_small_uint_t call_flags) {
  55193. #if defined(DUK_USE_ASSERTIONS)
  55194. duk_activation *entry_act;
  55195. duk_size_t entry_callstack_top;
  55196. #endif
  55197. duk_size_t entry_valstack_bottom_byteoff;
  55198. duk_size_t entry_valstack_end_byteoff;
  55199. duk_int_t entry_call_recursion_depth;
  55200. duk_hthread *entry_curr_thread;
  55201. duk_uint_fast8_t entry_thread_state;
  55202. duk_instr_t **entry_ptr_curr_pc;
  55203. duk_idx_t idx_args;
  55204. duk_idx_t nargs; /* # argument registers target function wants (< 0 => "as is") */
  55205. duk_idx_t nregs; /* # total registers target function wants on entry (< 0 => "as is") */
  55206. duk_size_t vs_min_bytes; /* minimum value stack size (bytes) for handling call */
  55207. duk_hobject *func; /* 'func' on stack (borrowed reference) */
  55208. duk_activation *act;
  55209. duk_ret_t rc;
  55210. duk_small_uint_t use_tailcall;
  55211. DUK_ASSERT(thr != NULL);
  55212. DUK_ASSERT(thr->heap != NULL);
  55213. /* Asserts for heap->curr_thread omitted: it may be NULL, 'thr', or
  55214. * any other thread (e.g. when heap thread is used to run finalizers).
  55215. */
  55216. DUK_ASSERT_CTX_VALID(thr);
  55217. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  55218. DUK_ASSERT(idx_func >= 0);
  55219. DUK_STATS_INC(thr->heap, stats_call_all);
  55220. /* If a tail call:
  55221. * - an ECMAScript activation must be on top of the callstack
  55222. * - there cannot be any catch stack entries that would catch
  55223. * a return
  55224. */
  55225. #if defined(DUK_USE_ASSERTIONS)
  55226. if (call_flags & DUK_CALL_FLAG_TAILCALL) {
  55227. duk_activation *tmp_act;
  55228. duk_catcher *tmp_cat;
  55229. DUK_ASSERT(thr->callstack_top >= 1);
  55230. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL);
  55231. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)));
  55232. /* No entry in the catch stack which would actually catch a
  55233. * throw can refer to the callstack entry being reused.
  55234. * There *can* be catch stack entries referring to the current
  55235. * callstack entry as long as they don't catch (e.g. label sites).
  55236. */
  55237. tmp_act = thr->callstack_curr;
  55238. for (tmp_cat = tmp_act->cat; tmp_cat != NULL; tmp_cat = tmp_cat->parent) {
  55239. DUK_ASSERT(DUK_CAT_GET_TYPE(tmp_cat) == DUK_CAT_TYPE_LABEL); /* a non-catching entry */
  55240. }
  55241. }
  55242. #endif /* DUK_USE_ASSERTIONS */
  55243. /*
  55244. * Store entry state.
  55245. */
  55246. #if defined(DUK_USE_ASSERTIONS)
  55247. entry_act = thr->callstack_curr;
  55248. entry_callstack_top = thr->callstack_top;
  55249. #endif
  55250. entry_valstack_bottom_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack);
  55251. entry_valstack_end_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack);
  55252. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  55253. entry_curr_thread = thr->heap->curr_thread; /* may be NULL if first call */
  55254. entry_thread_state = thr->state;
  55255. entry_ptr_curr_pc = thr->ptr_curr_pc; /* may be NULL */
  55256. /* If thr->ptr_curr_pc is set, sync curr_pc to act->pc. Then NULL
  55257. * thr->ptr_curr_pc so that it's not accidentally used with an incorrect
  55258. * activation when side effects occur.
  55259. */
  55260. duk_hthread_sync_and_null_currpc(thr);
  55261. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  55262. DUK_DD(DUK_DDPRINT("duk__handle_call_raw: thr=%p, idx_func=%ld, "
  55263. "call_flags=0x%08lx (constructor=%ld), "
  55264. "valstack_top=%ld, idx_func=%ld, idx_args=%ld, rec_depth=%ld/%ld, "
  55265. "entry_valstack_bottom_byteoff=%ld, entry_valstack_end_byteoff=%ld, "
  55266. "entry_call_recursion_depth=%ld, "
  55267. "entry_curr_thread=%p, entry_thread_state=%ld",
  55268. (void *) thr,
  55269. (long) idx_func,
  55270. (unsigned long) call_flags,
  55271. (long) ((call_flags & DUK_CALL_FLAG_CONSTRUCT) != 0 ? 1 : 0),
  55272. (long) duk_get_top(thr),
  55273. (long) idx_func,
  55274. (long) (idx_func + 2),
  55275. (long) thr->heap->call_recursion_depth,
  55276. (long) thr->heap->call_recursion_limit,
  55277. (long) entry_valstack_bottom_byteoff,
  55278. (long) entry_valstack_end_byteoff,
  55279. (long) entry_call_recursion_depth,
  55280. (void *) entry_curr_thread,
  55281. (long) entry_thread_state));
  55282. /*
  55283. * Thread state check and book-keeping.
  55284. */
  55285. duk__call_thread_state_update(thr);
  55286. /*
  55287. * Resolve final target function; handle bound functions and special
  55288. * functions like .call() and .apply(). Also figure out the effective
  55289. * 'this' binding, which replaces the current value at idx_func + 1.
  55290. */
  55291. if (DUK_LIKELY(duk__resolve_target_fastpath_check(thr, idx_func, &func, call_flags) != 0U)) {
  55292. DUK_DDD(DUK_DDDPRINT("fast path target resolve"));
  55293. } else {
  55294. DUK_DDD(DUK_DDDPRINT("slow path target resolve"));
  55295. func = duk__resolve_target_func_and_this_binding(thr, idx_func, &call_flags);
  55296. }
  55297. DUK_ASSERT(duk_get_top(thr) - idx_func >= 2); /* at least func and this present */
  55298. DUK_ASSERT(func == NULL || !DUK_HOBJECT_HAS_BOUNDFUNC(func));
  55299. DUK_ASSERT(func == NULL || (DUK_HOBJECT_IS_COMPFUNC(func) ||
  55300. DUK_HOBJECT_IS_NATFUNC(func)));
  55301. /* [ ... func this arg1 ... argN ] */
  55302. /*
  55303. * Setup a preliminary activation and figure out nargs/nregs and
  55304. * value stack minimum size.
  55305. *
  55306. * Don't touch valstack_bottom or valstack_top yet so that Duktape API
  55307. * calls work normally.
  55308. *
  55309. * Because 'act' is not zeroed, all fields must be filled in.
  55310. */
  55311. /* Should not be necessary, but initialize to silence warnings. */
  55312. act = NULL;
  55313. nargs = 0;
  55314. nregs = 0;
  55315. vs_min_bytes = 0;
  55316. #if defined(DUK_USE_TAILCALL)
  55317. use_tailcall = (call_flags & DUK_CALL_FLAG_TAILCALL);
  55318. if (use_tailcall) {
  55319. use_tailcall = duk__call_setup_act_attempt_tailcall(thr,
  55320. call_flags,
  55321. idx_func,
  55322. func,
  55323. entry_valstack_bottom_byteoff,
  55324. entry_valstack_end_byteoff,
  55325. &nargs,
  55326. &nregs,
  55327. &vs_min_bytes,
  55328. &act);
  55329. }
  55330. #else
  55331. DUK_ASSERT((call_flags & DUK_CALL_FLAG_TAILCALL) == 0); /* compiler ensures this */
  55332. use_tailcall = 0;
  55333. #endif
  55334. if (use_tailcall) {
  55335. idx_args = 0;
  55336. DUK_STATS_INC(thr->heap, stats_call_tailcall);
  55337. } else {
  55338. duk__call_setup_act_not_tailcall(thr,
  55339. call_flags,
  55340. idx_func,
  55341. func,
  55342. entry_valstack_bottom_byteoff,
  55343. entry_valstack_end_byteoff,
  55344. &nargs,
  55345. &nregs,
  55346. &vs_min_bytes,
  55347. &act);
  55348. idx_args = idx_func + 2;
  55349. }
  55350. /* After this point idx_func is no longer valid for tailcalls. */
  55351. DUK_ASSERT(act != NULL);
  55352. /* [ ... func this arg1 ... argN ] */
  55353. /*
  55354. * Environment record creation and 'arguments' object creation.
  55355. * Named function expression name binding is handled by the
  55356. * compiler; the compiled function's parent env will contain
  55357. * the (immutable) binding already.
  55358. *
  55359. * This handling is now identical for C and ECMAScript functions.
  55360. * C functions always have the 'NEWENV' flag set, so their
  55361. * environment record initialization is delayed (which is good).
  55362. *
  55363. * Delayed creation (on demand) is handled in duk_js_var.c.
  55364. */
  55365. duk__call_env_setup(thr, func, act, idx_args);
  55366. /* [ ... func this arg1 ... argN ] */
  55367. /*
  55368. * Setup value stack: clamp to 'nargs', fill up to 'nregs',
  55369. * ensure value stack size matches target requirements, and
  55370. * switch value stack bottom. Valstack top is kept.
  55371. *
  55372. * Value stack can only grow here.
  55373. */
  55374. duk_valstack_grow_check_throw(thr, vs_min_bytes);
  55375. act->reserve_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack);
  55376. if (use_tailcall) {
  55377. DUK_ASSERT(nregs >= 0);
  55378. DUK_ASSERT(nregs >= nargs);
  55379. duk_set_top_and_wipe(thr, nregs, nargs);
  55380. } else {
  55381. if (nregs >= 0) {
  55382. DUK_ASSERT(nregs >= nargs);
  55383. duk_set_top_and_wipe(thr, idx_func + 2 + nregs, idx_func + 2 + nargs);
  55384. } else {
  55385. ;
  55386. }
  55387. thr->valstack_bottom = thr->valstack_bottom + idx_func + 2;
  55388. }
  55389. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  55390. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  55391. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  55392. /*
  55393. * Make the actual call. For Ecma-to-Ecma calls detect that
  55394. * setup is complete, then return with a status code that allows
  55395. * the caller to reuse the running executor.
  55396. */
  55397. if (func != NULL && DUK_HOBJECT_IS_COMPFUNC(func)) {
  55398. /*
  55399. * ECMAScript call.
  55400. */
  55401. DUK_ASSERT(func != NULL);
  55402. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(func));
  55403. act->curr_pc = DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, (duk_hcompfunc *) func);
  55404. if (call_flags & DUK_CALL_FLAG_ALLOW_ECMATOECMA) {
  55405. DUK_DD(DUK_DDPRINT("avoid native call, use existing executor"));
  55406. DUK_STATS_INC(thr->heap, stats_call_ecmatoecma);
  55407. DUK_ASSERT((act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0);
  55408. DUK_REFZERO_CHECK_FAST(thr);
  55409. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  55410. return 1; /* 1=reuse executor */
  55411. }
  55412. DUK_ASSERT(use_tailcall == 0);
  55413. /* duk_hthread_activation_unwind_norz() will decrease this on unwind */
  55414. DUK_ASSERT((act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0);
  55415. act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;
  55416. thr->callstack_preventcount++;
  55417. /* XXX: we could just do this on entry regardless of reuse, as long
  55418. * as recursion depth is decreased for e2e case.
  55419. */
  55420. duk__call_c_recursion_limit_check(thr);
  55421. thr->heap->call_recursion_depth++;
  55422. /* [ ... func this | arg1 ... argN ] ('this' must precede new bottom) */
  55423. /*
  55424. * Bytecode executor call.
  55425. *
  55426. * Execute bytecode, handling any recursive function calls and
  55427. * thread resumptions. Returns when execution would return from
  55428. * the entry level activation. When the executor returns, a
  55429. * single return value is left on the stack top.
  55430. *
  55431. * The only possible longjmp() is an error (DUK_LJ_TYPE_THROW),
  55432. * other types are handled internally by the executor.
  55433. */
  55434. /* thr->ptr_curr_pc is set by bytecode executor early on entry */
  55435. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  55436. DUK_DDD(DUK_DDDPRINT("entering bytecode execution"));
  55437. duk_js_execute_bytecode(thr);
  55438. DUK_DDD(DUK_DDDPRINT("returned from bytecode execution"));
  55439. } else {
  55440. /*
  55441. * Native call.
  55442. */
  55443. DUK_ASSERT(func == NULL || ((duk_hnatfunc *) func)->func != NULL);
  55444. DUK_ASSERT(use_tailcall == 0);
  55445. /* [ ... func this | arg1 ... argN ] ('this' must precede new bottom) */
  55446. /* duk_hthread_activation_unwind_norz() will decrease this on unwind */
  55447. DUK_ASSERT((act->flags & DUK_ACT_FLAG_PREVENT_YIELD) == 0);
  55448. act->flags |= DUK_ACT_FLAG_PREVENT_YIELD;
  55449. thr->callstack_preventcount++;
  55450. /* XXX: we could just do this on entry regardless of reuse, as long
  55451. * as recursion depth is decreased for e2e case.
  55452. */
  55453. duk__call_c_recursion_limit_check(thr);
  55454. thr->heap->call_recursion_depth++;
  55455. /* For native calls must be NULL so we don't sync back */
  55456. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  55457. /* XXX: native funcptr could come out of call setup. */
  55458. if (func) {
  55459. rc = ((duk_hnatfunc *) func)->func(thr);
  55460. } else {
  55461. duk_tval *tv_func;
  55462. duk_c_function funcptr;
  55463. tv_func = &act->tv_func;
  55464. DUK_ASSERT(DUK_TVAL_IS_LIGHTFUNC(tv_func));
  55465. funcptr = DUK_TVAL_GET_LIGHTFUNC_FUNCPTR(tv_func);
  55466. rc = funcptr(thr);
  55467. }
  55468. /* Automatic error throwing, retval check. */
  55469. if (rc == 0) {
  55470. DUK_ASSERT(thr->valstack < thr->valstack_end);
  55471. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top));
  55472. thr->valstack_top++;
  55473. } else if (rc == 1) {
  55474. ;
  55475. } else if (rc < 0) {
  55476. duk_error_throw_from_negative_rc(thr, rc);
  55477. DUK_WO_NORETURN(return 0;);
  55478. } else {
  55479. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_CFUNC_RC);
  55480. DUK_WO_NORETURN(return 0;);
  55481. }
  55482. }
  55483. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  55484. DUK_ASSERT(use_tailcall == 0);
  55485. /*
  55486. * Constructor call post processing.
  55487. */
  55488. #if defined(DUK_USE_ES6_PROXY)
  55489. if (call_flags & (DUK_CALL_FLAG_CONSTRUCT | DUK_CALL_FLAG_CONSTRUCT_PROXY)) {
  55490. duk_call_construct_postprocess(thr, call_flags & DUK_CALL_FLAG_CONSTRUCT_PROXY);
  55491. }
  55492. #else
  55493. if (call_flags & DUK_CALL_FLAG_CONSTRUCT) {
  55494. duk_call_construct_postprocess(thr, 0);
  55495. }
  55496. #endif
  55497. /*
  55498. * Unwind, restore valstack bottom and other book-keeping.
  55499. */
  55500. DUK_ASSERT(thr->callstack_curr != NULL);
  55501. DUK_ASSERT(thr->callstack_curr->parent == entry_act);
  55502. DUK_ASSERT(thr->callstack_top == entry_callstack_top + 1);
  55503. duk_hthread_activation_unwind_norz(thr);
  55504. DUK_ASSERT(thr->callstack_curr == entry_act);
  55505. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  55506. thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + entry_valstack_bottom_byteoff);
  55507. /* keep current valstack_top */
  55508. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  55509. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  55510. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  55511. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom >= idx_func + 1);
  55512. /* Return value handling. */
  55513. /* [ ... func this (crud) retval ] */
  55514. {
  55515. duk_tval *tv_ret;
  55516. duk_tval *tv_funret;
  55517. tv_ret = thr->valstack_bottom + idx_func;
  55518. tv_funret = thr->valstack_top - 1;
  55519. #if defined(DUK_USE_FASTINT)
  55520. /* Explicit check for fastint downgrade. */
  55521. DUK_TVAL_CHKFAST_INPLACE_FAST(tv_funret);
  55522. #endif
  55523. DUK_TVAL_SET_TVAL_UPDREF(thr, tv_ret, tv_funret); /* side effects */
  55524. }
  55525. duk_set_top_unsafe(thr, idx_func + 1);
  55526. /* [ ... retval ] */
  55527. /* Restore caller's value stack reserve (cannot fail). */
  55528. DUK_ASSERT((duk_uint8_t *) thr->valstack + entry_valstack_end_byteoff >= (duk_uint8_t *) thr->valstack_top);
  55529. DUK_ASSERT((duk_uint8_t *) thr->valstack + entry_valstack_end_byteoff <= (duk_uint8_t *) thr->valstack_alloc_end);
  55530. thr->valstack_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + entry_valstack_end_byteoff);
  55531. /* XXX: Trial value stack shrink would be OK here, but we'd need
  55532. * to prevent side effects of the potential realloc.
  55533. */
  55534. /* Restore entry thread executor curr_pc stack frame pointer. */
  55535. thr->ptr_curr_pc = entry_ptr_curr_pc;
  55536. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  55537. thr->state = (duk_uint8_t) entry_thread_state;
  55538. /* Disabled assert: triggered with some torture tests. */
  55539. #if 0
  55540. DUK_ASSERT((thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread == NULL) || /* first call */
  55541. (thr->state == DUK_HTHREAD_STATE_INACTIVE && thr->heap->curr_thread != NULL) || /* other call */
  55542. (thr->state == DUK_HTHREAD_STATE_RUNNING && thr->heap->curr_thread == thr)); /* current thread */
  55543. #endif
  55544. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  55545. /* If the debugger is active we need to force an interrupt so that
  55546. * debugger breakpoints are rechecked. This is important for function
  55547. * calls caused by side effects (e.g. when doing a DUK_OP_GETPROP), see
  55548. * GH-303. Only needed for success path, error path always causes a
  55549. * breakpoint recheck in the executor. It would be enough to set this
  55550. * only when returning to an ECMAScript activation, but setting the flag
  55551. * on every return should have no ill effect.
  55552. */
  55553. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  55554. if (duk_debug_is_attached(thr->heap)) {
  55555. DUK_DD(DUK_DDPRINT("returning with debugger enabled, force interrupt"));
  55556. DUK_ASSERT(thr->interrupt_counter <= thr->interrupt_init);
  55557. thr->interrupt_init -= thr->interrupt_counter;
  55558. thr->interrupt_counter = 0;
  55559. thr->heap->dbg_force_restart = 1;
  55560. }
  55561. #endif
  55562. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  55563. duk__interrupt_fixup(thr, entry_curr_thread);
  55564. #endif
  55565. /* Restored by success path. */
  55566. DUK_ASSERT(thr->heap->call_recursion_depth == entry_call_recursion_depth);
  55567. DUK_ASSERT(thr->ptr_curr_pc == entry_ptr_curr_pc);
  55568. DUK_ASSERT_LJSTATE_UNSET(thr->heap);
  55569. DUK_REFZERO_CHECK_FAST(thr);
  55570. return 0; /* 0=call handled inline */
  55571. }
  55572. DUK_INTERNAL duk_int_t duk_handle_call_unprotected_nargs(duk_hthread *thr,
  55573. duk_idx_t nargs,
  55574. duk_small_uint_t call_flags) {
  55575. duk_idx_t idx_func;
  55576. DUK_ASSERT(duk_get_top(thr) >= nargs + 2);
  55577. idx_func = duk_get_top(thr) - (nargs + 2);
  55578. DUK_ASSERT(idx_func >= 0);
  55579. return duk_handle_call_unprotected(thr, idx_func, call_flags);
  55580. }
  55581. DUK_INTERNAL duk_int_t duk_handle_call_unprotected(duk_hthread *thr,
  55582. duk_idx_t idx_func,
  55583. duk_small_uint_t call_flags) {
  55584. DUK_ASSERT(duk_is_valid_index(thr, idx_func));
  55585. DUK_ASSERT(idx_func >= 0);
  55586. return duk__handle_call_raw(thr, idx_func, call_flags);
  55587. }
  55588. /*
  55589. * duk_handle_safe_call(): make a "C protected call" within the
  55590. * current activation.
  55591. *
  55592. * The allowed thread states for making a call are the same as for
  55593. * duk_handle_call_protected().
  55594. *
  55595. * Even though this call is protected, errors are thrown for insane arguments
  55596. * and may result in a fatal error unless there's another protected call which
  55597. * catches such errors.
  55598. *
  55599. * The error handling path should be error free, even for out-of-memory
  55600. * errors, to ensure safe sandboxing. (As of Duktape 2.2.0 this is not
  55601. * yet the case for environment closing which may run out of memory, see
  55602. * XXX notes below.)
  55603. */
  55604. DUK_LOCAL void duk__handle_safe_call_inner(duk_hthread *thr,
  55605. duk_safe_call_function func,
  55606. void *udata,
  55607. #if defined(DUK_USE_ASSERTIONS)
  55608. duk_size_t entry_valstack_bottom_byteoff,
  55609. duk_size_t entry_callstack_top,
  55610. #endif
  55611. duk_hthread *entry_curr_thread,
  55612. duk_uint_fast8_t entry_thread_state,
  55613. duk_idx_t idx_retbase,
  55614. duk_idx_t num_stack_rets) {
  55615. duk_ret_t rc;
  55616. DUK_ASSERT(thr != NULL);
  55617. DUK_ASSERT_CTX_VALID(thr);
  55618. /*
  55619. * Thread state check and book-keeping.
  55620. */
  55621. duk__call_thread_state_update(thr);
  55622. /*
  55623. * Recursion limit check.
  55624. */
  55625. duk__call_c_recursion_limit_check(thr);
  55626. thr->heap->call_recursion_depth++;
  55627. /*
  55628. * Make the C call.
  55629. */
  55630. rc = func(thr, udata);
  55631. DUK_DDD(DUK_DDDPRINT("safe_call, func rc=%ld", (long) rc));
  55632. /*
  55633. * Valstack manipulation for results.
  55634. */
  55635. /* we're running inside the caller's activation, so no change in call/catch stack or valstack bottom */
  55636. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  55637. DUK_ASSERT(thr->valstack_bottom >= thr->valstack);
  55638. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack) == entry_valstack_bottom_byteoff);
  55639. DUK_ASSERT(thr->valstack_top >= thr->valstack_bottom);
  55640. DUK_ASSERT(thr->valstack_end >= thr->valstack_top);
  55641. if (DUK_UNLIKELY(rc < 0)) {
  55642. duk_error_throw_from_negative_rc(thr, rc);
  55643. DUK_WO_NORETURN(return;);
  55644. }
  55645. DUK_ASSERT(rc >= 0);
  55646. duk__safe_call_adjust_valstack(thr, idx_retbase, num_stack_rets, rc); /* throws for insane rc */
  55647. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  55648. thr->state = (duk_uint8_t) entry_thread_state;
  55649. }
  55650. DUK_LOCAL void duk__handle_safe_call_error(duk_hthread *thr,
  55651. duk_activation *entry_act,
  55652. #if defined(DUK_USE_ASSERTIONS)
  55653. duk_size_t entry_callstack_top,
  55654. #endif
  55655. duk_hthread *entry_curr_thread,
  55656. duk_uint_fast8_t entry_thread_state,
  55657. duk_idx_t idx_retbase,
  55658. duk_idx_t num_stack_rets,
  55659. duk_size_t entry_valstack_bottom_byteoff,
  55660. duk_jmpbuf *old_jmpbuf_ptr) {
  55661. DUK_ASSERT(thr != NULL);
  55662. DUK_ASSERT_CTX_VALID(thr);
  55663. /*
  55664. * Error during call. The error value is at heap->lj.value1.
  55665. *
  55666. * The very first thing we do is restore the previous setjmp catcher.
  55667. * This means that any error in error handling will propagate outwards
  55668. * instead of causing a setjmp() re-entry above.
  55669. */
  55670. DUK_DDD(DUK_DDDPRINT("error caught during protected duk_handle_safe_call()"));
  55671. /* Other longjmp types are handled by executor before propagating
  55672. * the error here.
  55673. */
  55674. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW);
  55675. DUK_ASSERT_LJSTATE_SET(thr->heap);
  55676. /* Either pointer may be NULL (at entry), so don't assert. */
  55677. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  55678. /* XXX: callstack unwind may now throw an error when closing
  55679. * scopes; this is a sandboxing issue, described in:
  55680. * https://github.com/svaarala/duktape/issues/476
  55681. */
  55682. /* XXX: "unwind to" primitive? */
  55683. DUK_ASSERT(thr->callstack_top >= entry_callstack_top);
  55684. while (thr->callstack_curr != entry_act) {
  55685. DUK_ASSERT(thr->callstack_curr != NULL);
  55686. duk_hthread_activation_unwind_norz(thr);
  55687. }
  55688. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  55689. /* Switch active thread before any side effects to avoid a
  55690. * dangling curr_thread pointer.
  55691. */
  55692. DUK_HEAP_SWITCH_THREAD(thr->heap, entry_curr_thread); /* may be NULL */
  55693. thr->state = (duk_uint8_t) entry_thread_state;
  55694. DUK_ASSERT(thr->heap->curr_thread == entry_curr_thread);
  55695. DUK_ASSERT(thr->state == entry_thread_state);
  55696. /* Restore valstack bottom. */
  55697. thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + entry_valstack_bottom_byteoff);
  55698. /* [ ... | (crud) ] */
  55699. /* XXX: ensure space in valstack (now relies on internal reserve)? */
  55700. duk_push_tval(thr, &thr->heap->lj.value1);
  55701. /* [ ... | (crud) errobj ] */
  55702. DUK_ASSERT(duk_get_top(thr) >= 1); /* at least errobj must be on stack */
  55703. duk__safe_call_adjust_valstack(thr, idx_retbase, num_stack_rets, 1); /* 1 = num actual 'return values' */
  55704. /* [ ... | ] or [ ... | errobj (M * undefined)] where M = num_stack_rets - 1 */
  55705. /* Reset longjmp state. */
  55706. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  55707. thr->heap->lj.iserror = 0;
  55708. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, &thr->heap->lj.value1);
  55709. DUK_TVAL_SET_UNDEFINED_UPDREF_NORZ(thr, &thr->heap->lj.value2);
  55710. /* Error handling complete, remove side effect protections. Caller
  55711. * will process pending finalizers.
  55712. */
  55713. #if defined(DUK_USE_ASSERTIONS)
  55714. DUK_ASSERT(thr->heap->error_not_allowed == 1);
  55715. thr->heap->error_not_allowed = 0;
  55716. #endif
  55717. DUK_ASSERT(thr->heap->pf_prevent_count > 0);
  55718. thr->heap->pf_prevent_count--;
  55719. DUK_DD(DUK_DDPRINT("safe call error handled, pf_prevent_count updated to %ld", (long) thr->heap->pf_prevent_count));
  55720. /* thr->ptr_curr_pc is restored by
  55721. * duk__handle_safe_call_shared_unwind() which is also used for
  55722. * success path.
  55723. */
  55724. }
  55725. DUK_LOCAL void duk__handle_safe_call_shared_unwind(duk_hthread *thr,
  55726. duk_idx_t idx_retbase,
  55727. duk_idx_t num_stack_rets,
  55728. #if defined(DUK_USE_ASSERTIONS)
  55729. duk_size_t entry_callstack_top,
  55730. #endif
  55731. duk_int_t entry_call_recursion_depth,
  55732. duk_hthread *entry_curr_thread,
  55733. duk_instr_t **entry_ptr_curr_pc) {
  55734. DUK_ASSERT(thr != NULL);
  55735. DUK_ASSERT_CTX_VALID(thr);
  55736. DUK_UNREF(idx_retbase);
  55737. DUK_UNREF(num_stack_rets);
  55738. DUK_UNREF(entry_curr_thread);
  55739. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  55740. /* Restore entry thread executor curr_pc stack frame pointer.
  55741. * XXX: would be enough to do in error path only, should nest
  55742. * cleanly in success path.
  55743. */
  55744. thr->ptr_curr_pc = entry_ptr_curr_pc;
  55745. thr->heap->call_recursion_depth = entry_call_recursion_depth;
  55746. /* stack discipline consistency check */
  55747. DUK_ASSERT(duk_get_top(thr) == idx_retbase + num_stack_rets);
  55748. /* A debugger forced interrupt check is not needed here, as
  55749. * problematic safe calls are not caused by side effects.
  55750. */
  55751. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  55752. duk__interrupt_fixup(thr, entry_curr_thread);
  55753. #endif
  55754. }
  55755. DUK_INTERNAL duk_int_t duk_handle_safe_call(duk_hthread *thr,
  55756. duk_safe_call_function func,
  55757. void *udata,
  55758. duk_idx_t num_stack_args,
  55759. duk_idx_t num_stack_rets) {
  55760. duk_activation *entry_act;
  55761. duk_size_t entry_valstack_bottom_byteoff;
  55762. #if defined(DUK_USE_ASSERTIONS)
  55763. duk_size_t entry_valstack_end_byteoff;
  55764. duk_size_t entry_callstack_top;
  55765. duk_size_t entry_callstack_preventcount;
  55766. #endif
  55767. duk_int_t entry_call_recursion_depth;
  55768. duk_hthread *entry_curr_thread;
  55769. duk_uint_fast8_t entry_thread_state;
  55770. duk_instr_t **entry_ptr_curr_pc;
  55771. duk_jmpbuf *old_jmpbuf_ptr = NULL;
  55772. duk_jmpbuf our_jmpbuf;
  55773. duk_idx_t idx_retbase;
  55774. duk_int_t retval;
  55775. DUK_ASSERT(thr != NULL);
  55776. DUK_ASSERT(duk_get_top(thr) >= num_stack_args); /* Caller ensures. */
  55777. DUK_STATS_INC(thr->heap, stats_safecall_all);
  55778. /* Value stack reserve handling: safe call assumes caller has reserved
  55779. * space for nrets (assuming optimal unwind processing). Value stack
  55780. * reserve is not stored/restored as for normal calls because a safe
  55781. * call conceptually happens in the same activation.
  55782. */
  55783. /* Careful with indices like '-x'; if 'x' is zero, it refers to bottom */
  55784. entry_act = thr->callstack_curr;
  55785. entry_valstack_bottom_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack);
  55786. #if defined(DUK_USE_ASSERTIONS)
  55787. entry_valstack_end_byteoff = (duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack);
  55788. entry_callstack_top = thr->callstack_top;
  55789. entry_callstack_preventcount = thr->callstack_preventcount;
  55790. #endif
  55791. entry_call_recursion_depth = thr->heap->call_recursion_depth;
  55792. entry_curr_thread = thr->heap->curr_thread; /* may be NULL if first call */
  55793. entry_thread_state = thr->state;
  55794. entry_ptr_curr_pc = thr->ptr_curr_pc; /* may be NULL */
  55795. idx_retbase = duk_get_top(thr) - num_stack_args; /* not a valid stack index if num_stack_args == 0 */
  55796. DUK_ASSERT(idx_retbase >= 0);
  55797. DUK_ASSERT((duk_idx_t) (thr->valstack_top - thr->valstack_bottom) >= num_stack_args); /* Caller ensures. */
  55798. DUK_ASSERT((duk_idx_t) (thr->valstack_end - (thr->valstack_bottom + idx_retbase)) >= num_stack_rets); /* Caller ensures. */
  55799. /* Cannot portably debug print a function pointer, hence 'func' not printed! */
  55800. DUK_DD(DUK_DDPRINT("duk_handle_safe_call: thr=%p, num_stack_args=%ld, num_stack_rets=%ld, "
  55801. "valstack_top=%ld, idx_retbase=%ld, rec_depth=%ld/%ld, "
  55802. "entry_act=%p, entry_valstack_bottom_byteoff=%ld, entry_call_recursion_depth=%ld, "
  55803. "entry_curr_thread=%p, entry_thread_state=%ld",
  55804. (void *) thr,
  55805. (long) num_stack_args,
  55806. (long) num_stack_rets,
  55807. (long) duk_get_top(thr),
  55808. (long) idx_retbase,
  55809. (long) thr->heap->call_recursion_depth,
  55810. (long) thr->heap->call_recursion_limit,
  55811. (void *) entry_act,
  55812. (long) entry_valstack_bottom_byteoff,
  55813. (long) entry_call_recursion_depth,
  55814. (void *) entry_curr_thread,
  55815. (long) entry_thread_state));
  55816. /* Setjmp catchpoint setup. */
  55817. old_jmpbuf_ptr = thr->heap->lj.jmpbuf_ptr;
  55818. thr->heap->lj.jmpbuf_ptr = &our_jmpbuf;
  55819. /* Prevent yields for the duration of the safe call. This only
  55820. * matters if the executor makes safe calls to functions that
  55821. * yield, this doesn't currently happen.
  55822. */
  55823. thr->callstack_preventcount++;
  55824. #if defined(DUK_USE_CPP_EXCEPTIONS)
  55825. try {
  55826. #else
  55827. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr == &our_jmpbuf);
  55828. if (DUK_SETJMP(our_jmpbuf.jb) == 0) {
  55829. /* Success path. */
  55830. #endif
  55831. DUK_DDD(DUK_DDDPRINT("safe_call setjmp catchpoint setup complete"));
  55832. duk__handle_safe_call_inner(thr,
  55833. func,
  55834. udata,
  55835. #if defined(DUK_USE_ASSERTIONS)
  55836. entry_valstack_bottom_byteoff,
  55837. entry_callstack_top,
  55838. #endif
  55839. entry_curr_thread,
  55840. entry_thread_state,
  55841. idx_retbase,
  55842. num_stack_rets);
  55843. DUK_STATS_INC(thr->heap, stats_safecall_nothrow);
  55844. /* Either pointer may be NULL (at entry), so don't assert */
  55845. thr->heap->lj.jmpbuf_ptr = old_jmpbuf_ptr;
  55846. /* If calls happen inside the safe call, these are restored by
  55847. * whatever calls are made. Reserve cannot decrease.
  55848. */
  55849. DUK_ASSERT(thr->callstack_curr == entry_act);
  55850. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack) >= entry_valstack_end_byteoff);
  55851. retval = DUK_EXEC_SUCCESS;
  55852. #if defined(DUK_USE_CPP_EXCEPTIONS)
  55853. } catch (duk_internal_exception &exc) {
  55854. DUK_UNREF(exc);
  55855. #else
  55856. } else {
  55857. /* Error path. */
  55858. #endif
  55859. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack) >= entry_valstack_end_byteoff);
  55860. DUK_STATS_INC(thr->heap, stats_safecall_throw);
  55861. duk__handle_safe_call_error(thr,
  55862. entry_act,
  55863. #if defined(DUK_USE_ASSERTIONS)
  55864. entry_callstack_top,
  55865. #endif
  55866. entry_curr_thread,
  55867. entry_thread_state,
  55868. idx_retbase,
  55869. num_stack_rets,
  55870. entry_valstack_bottom_byteoff,
  55871. old_jmpbuf_ptr);
  55872. retval = DUK_EXEC_ERROR;
  55873. }
  55874. #if defined(DUK_USE_CPP_EXCEPTIONS)
  55875. catch (duk_fatal_exception &exc) {
  55876. DUK_D(DUK_DPRINT("rethrow duk_fatal_exception"));
  55877. throw;
  55878. } catch (std::exception &exc) {
  55879. const char *what = exc.what();
  55880. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack) >= entry_valstack_end_byteoff);
  55881. DUK_STATS_INC(thr->heap, stats_safecall_throw);
  55882. if (!what) {
  55883. what = "unknown";
  55884. }
  55885. DUK_D(DUK_DPRINT("unexpected c++ std::exception (perhaps thrown by user code)"));
  55886. try {
  55887. DUK_ERROR_FMT1(thr, DUK_ERR_TYPE_ERROR, "caught invalid c++ std::exception '%s' (perhaps thrown by user code)", what);
  55888. DUK_WO_NORETURN(return 0;);
  55889. } catch (duk_internal_exception exc) {
  55890. DUK_D(DUK_DPRINT("caught api error thrown from unexpected c++ std::exception"));
  55891. DUK_UNREF(exc);
  55892. duk__handle_safe_call_error(thr,
  55893. entry_act,
  55894. #if defined(DUK_USE_ASSERTIONS)
  55895. entry_callstack_top,
  55896. #endif
  55897. entry_curr_thread,
  55898. entry_thread_state,
  55899. idx_retbase,
  55900. num_stack_rets,
  55901. entry_valstack_bottom_byteoff,
  55902. old_jmpbuf_ptr);
  55903. retval = DUK_EXEC_ERROR;
  55904. }
  55905. } catch (...) {
  55906. DUK_D(DUK_DPRINT("unexpected c++ exception (perhaps thrown by user code)"));
  55907. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack) >= entry_valstack_end_byteoff);
  55908. DUK_STATS_INC(thr->heap, stats_safecall_throw);
  55909. try {
  55910. DUK_ERROR_TYPE(thr, "caught invalid c++ exception (perhaps thrown by user code)");
  55911. DUK_WO_NORETURN(return 0;);
  55912. } catch (duk_internal_exception exc) {
  55913. DUK_D(DUK_DPRINT("caught api error thrown from unexpected c++ exception"));
  55914. DUK_UNREF(exc);
  55915. duk__handle_safe_call_error(thr,
  55916. entry_act,
  55917. #if defined(DUK_USE_ASSERTIONS)
  55918. entry_callstack_top,
  55919. #endif
  55920. entry_curr_thread,
  55921. entry_thread_state,
  55922. idx_retbase,
  55923. num_stack_rets,
  55924. entry_valstack_bottom_byteoff,
  55925. old_jmpbuf_ptr);
  55926. retval = DUK_EXEC_ERROR;
  55927. }
  55928. }
  55929. #endif
  55930. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr == old_jmpbuf_ptr); /* success/error path both do this */
  55931. DUK_ASSERT_LJSTATE_UNSET(thr->heap);
  55932. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack) >= entry_valstack_end_byteoff);
  55933. duk__handle_safe_call_shared_unwind(thr,
  55934. idx_retbase,
  55935. num_stack_rets,
  55936. #if defined(DUK_USE_ASSERTIONS)
  55937. entry_callstack_top,
  55938. #endif
  55939. entry_call_recursion_depth,
  55940. entry_curr_thread,
  55941. entry_ptr_curr_pc);
  55942. /* Restore preventcount. */
  55943. thr->callstack_preventcount--;
  55944. DUK_ASSERT(thr->callstack_preventcount == entry_callstack_preventcount);
  55945. /* Final asserts. */
  55946. DUK_ASSERT(thr->callstack_curr == entry_act);
  55947. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_bottom - (duk_uint8_t *) thr->valstack) == entry_valstack_bottom_byteoff);
  55948. DUK_ASSERT((duk_size_t) ((duk_uint8_t *) thr->valstack_end - (duk_uint8_t *) thr->valstack) >= entry_valstack_end_byteoff);
  55949. DUK_ASSERT(thr->callstack_top == entry_callstack_top);
  55950. DUK_ASSERT(thr->heap->call_recursion_depth == entry_call_recursion_depth);
  55951. DUK_ASSERT(thr->heap->curr_thread == entry_curr_thread);
  55952. DUK_ASSERT(thr->state == entry_thread_state);
  55953. DUK_ASSERT(thr->ptr_curr_pc == entry_ptr_curr_pc);
  55954. DUK_ASSERT(duk_get_top(thr) == idx_retbase + num_stack_rets);
  55955. DUK_ASSERT_LJSTATE_UNSET(thr->heap);
  55956. /* Pending side effects. */
  55957. DUK_REFZERO_CHECK_FAST(thr);
  55958. return retval;
  55959. }
  55960. /*
  55961. * Property-based call (foo.noSuch()) error setup: replace target function
  55962. * on stack top with a specially tagged (hidden Symbol) error which gets
  55963. * thrown in call handling at the proper spot to follow ECMAScript semantics.
  55964. */
  55965. #if defined(DUK_USE_VERBOSE_ERRORS)
  55966. DUK_INTERNAL DUK_NOINLINE DUK_COLD void duk_call_setup_propcall_error(duk_hthread *thr, duk_tval *tv_targ, duk_tval *tv_base, duk_tval *tv_key) {
  55967. const char *str1, *str2, *str3;
  55968. duk_idx_t entry_top;
  55969. entry_top = duk_get_top(thr);
  55970. /* Must stabilize pointers first. Argument convention is a bit awkward,
  55971. * it comes from the executor call site where some arguments may not be
  55972. * on the value stack (consts).
  55973. */
  55974. duk_push_tval(thr, tv_base);
  55975. duk_push_tval(thr, tv_key);
  55976. duk_push_tval(thr, tv_targ);
  55977. DUK_GC_TORTURE(thr->heap);
  55978. /* We only push an error, replacing the call target (at idx_func)
  55979. * with the error to ensure side effects come out correctly:
  55980. * - Property read
  55981. * - Call argument evaluation
  55982. * - Callability check and error thrown.
  55983. *
  55984. * A hidden Symbol on the error object pushed here is used by
  55985. * call handling to figure out the error is to be thrown as is.
  55986. * It is CRITICAL that the hidden Symbol can never occur on a
  55987. * user visible object that may get thrown.
  55988. */
  55989. #if defined(DUK_USE_PARANOID_ERRORS)
  55990. str1 = duk_get_type_name(thr, -1);
  55991. str2 = duk_get_type_name(thr, -2);
  55992. str3 = duk_get_type_name(thr, -3);
  55993. duk_push_error_object(thr, DUK_ERR_TYPE_ERROR | DUK_ERRCODE_FLAG_NOBLAME_FILELINE, "%s not callable (property %s of %s)", str1, str2, str3);
  55994. #else
  55995. str1 = duk_push_string_readable(thr, -1);
  55996. str2 = duk_push_string_readable(thr, -3);
  55997. str3 = duk_push_string_readable(thr, -5);
  55998. duk_push_error_object(thr, DUK_ERR_TYPE_ERROR | DUK_ERRCODE_FLAG_NOBLAME_FILELINE, "%s not callable (property %s of %s)", str1, str2, str3);
  55999. #endif
  56000. duk_push_true(thr);
  56001. duk_put_prop_stridx(thr, -2, DUK_STRIDX_INT_TARGET); /* Marker property, reuse _Target. */
  56002. /* [ <nregs> propValue <variable> error ] */
  56003. duk_replace(thr, entry_top - 1);
  56004. duk_set_top(thr, entry_top);
  56005. DUK_ASSERT(!duk_is_callable(thr, -1)); /* Critical so that call handling will throw the error. */
  56006. }
  56007. #endif /* DUK_USE_VERBOSE_ERRORS */
  56008. /* automatic undefs */
  56009. #undef DUK__AUGMENT_CALL_RELAX_COUNT
  56010. #line 1 "duk_js_compiler.c"
  56011. /*
  56012. * ECMAScript compiler.
  56013. *
  56014. * Parses an input string and generates a function template result.
  56015. * Compilation may happen in multiple contexts (global code, eval
  56016. * code, function code).
  56017. *
  56018. * The parser uses a traditional top-down recursive parsing for the
  56019. * statement level, and an operator precedence based top-down approach
  56020. * for the expression level. The attempt is to minimize the C stack
  56021. * depth. Bytecode is generated directly without an intermediate
  56022. * representation (tree), at the cost of needing two (and sometimes
  56023. * three) passes over each function.
  56024. *
  56025. * The top-down recursive parser functions are named "duk__parse_XXX".
  56026. *
  56027. * Recursion limits are in key functions to prevent arbitrary C recursion:
  56028. * function body parsing, statement parsing, and expression parsing.
  56029. *
  56030. * See doc/compiler.rst for discussion on the design.
  56031. *
  56032. * A few typing notes:
  56033. *
  56034. * - duk_regconst_t: signed, highest bit set (< 0) means constant,
  56035. * some call sites use -1 for "none" (equivalent to constant 0x7fffffff)
  56036. * - PC values: duk_int_t, negative values used as markers
  56037. */
  56038. /* #include duk_internal.h -> already included */
  56039. /* If highest bit of a register number is set, it refers to a constant instead.
  56040. * When interpreted as a signed value, this means const values are always
  56041. * negative (when interpreted as two's complement). For example DUK__ISREG_TEMP()
  56042. * uses this approach to avoid an explicit DUK__ISREG() check (the condition is
  56043. * logically "'x' is a register AND 'x' >= temp_first").
  56044. */
  56045. #define DUK__CONST_MARKER DUK_REGCONST_CONST_MARKER
  56046. #define DUK__REMOVECONST(x) ((x) & ~DUK__CONST_MARKER)
  56047. #define DUK__ISREG(x) ((x) >= 0)
  56048. #define DUK__ISCONST(x) ((x) < 0)
  56049. #define DUK__ISREG_TEMP(comp_ctx,x) ((duk_int32_t) (x) >= (duk_int32_t) ((comp_ctx)->curr_func.temp_first)) /* Check for x >= temp_first && x >= 0 by comparing as signed. */
  56050. #define DUK__ISREG_NOTTEMP(comp_ctx,x) ((duk_uint32_t) (x) < (duk_uint32_t) ((comp_ctx)->curr_func.temp_first)) /* Check for x >= 0 && x < temp_first by interpreting as unsigned. */
  56051. #define DUK__GETTEMP(comp_ctx) ((comp_ctx)->curr_func.temp_next)
  56052. #define DUK__SETTEMP(comp_ctx,x) ((comp_ctx)->curr_func.temp_next = (x)) /* dangerous: must only lower (temp_max not updated) */
  56053. #define DUK__SETTEMP_CHECKMAX(comp_ctx,x) duk__settemp_checkmax((comp_ctx),(x))
  56054. #define DUK__ALLOCTEMP(comp_ctx) duk__alloctemp((comp_ctx))
  56055. #define DUK__ALLOCTEMPS(comp_ctx,count) duk__alloctemps((comp_ctx),(count))
  56056. /* Init value set size for array and object literals. */
  56057. #define DUK__MAX_ARRAY_INIT_VALUES 20
  56058. #define DUK__MAX_OBJECT_INIT_PAIRS 10
  56059. /* XXX: hack, remove when const lookup is not O(n) */
  56060. #define DUK__GETCONST_MAX_CONSTS_CHECK 256
  56061. /* These limits are based on bytecode limits. Max temps is limited
  56062. * by duk_hcompfunc nargs/nregs fields being 16 bits.
  56063. */
  56064. #define DUK__MAX_CONSTS DUK_BC_BC_MAX
  56065. #define DUK__MAX_FUNCS DUK_BC_BC_MAX
  56066. #define DUK__MAX_TEMPS 0xffffL
  56067. /* Initial bytecode size allocation. */
  56068. #if defined(DUK_USE_PREFER_SIZE)
  56069. #define DUK__BC_INITIAL_INSTS 16
  56070. #else
  56071. #define DUK__BC_INITIAL_INSTS 256
  56072. #endif
  56073. #define DUK__RECURSION_INCREASE(comp_ctx,thr) do { \
  56074. DUK_DDD(DUK_DDDPRINT("RECURSION INCREASE: %s:%ld", (const char *) DUK_FILE_MACRO, (long) DUK_LINE_MACRO)); \
  56075. duk__comp_recursion_increase((comp_ctx)); \
  56076. } while (0)
  56077. #define DUK__RECURSION_DECREASE(comp_ctx,thr) do { \
  56078. DUK_DDD(DUK_DDDPRINT("RECURSION DECREASE: %s:%ld", (const char *) DUK_FILE_MACRO, (long) DUK_LINE_MACRO)); \
  56079. duk__comp_recursion_decrease((comp_ctx)); \
  56080. } while (0)
  56081. /* Value stack slot limits: these are quite approximate right now, and
  56082. * because they overlap in control flow, some could be eliminated.
  56083. */
  56084. #define DUK__COMPILE_ENTRY_SLOTS 8
  56085. #define DUK__FUNCTION_INIT_REQUIRE_SLOTS 16
  56086. #define DUK__FUNCTION_BODY_REQUIRE_SLOTS 16
  56087. #define DUK__PARSE_STATEMENTS_SLOTS 16
  56088. #define DUK__PARSE_EXPR_SLOTS 16
  56089. /* Temporary structure used to pass a stack allocated region through
  56090. * duk_safe_call().
  56091. */
  56092. typedef struct {
  56093. duk_small_uint_t flags;
  56094. duk_compiler_ctx comp_ctx_alloc;
  56095. duk_lexer_point lex_pt_alloc;
  56096. } duk__compiler_stkstate;
  56097. /*
  56098. * Prototypes
  56099. */
  56100. /* lexing */
  56101. DUK_LOCAL_DECL void duk__advance_helper(duk_compiler_ctx *comp_ctx, duk_small_int_t expect);
  56102. DUK_LOCAL_DECL void duk__advance_expect(duk_compiler_ctx *comp_ctx, duk_small_int_t expect);
  56103. DUK_LOCAL_DECL void duk__advance(duk_compiler_ctx *ctx);
  56104. /* function helpers */
  56105. DUK_LOCAL_DECL void duk__init_func_valstack_slots(duk_compiler_ctx *comp_ctx);
  56106. DUK_LOCAL_DECL void duk__reset_func_for_pass2(duk_compiler_ctx *comp_ctx);
  56107. DUK_LOCAL_DECL void duk__init_varmap_and_prologue_for_pass2(duk_compiler_ctx *comp_ctx, duk_regconst_t *out_stmt_value_reg);
  56108. DUK_LOCAL_DECL void duk__convert_to_func_template(duk_compiler_ctx *comp_ctx);
  56109. DUK_LOCAL_DECL duk_int_t duk__cleanup_varmap(duk_compiler_ctx *comp_ctx);
  56110. /* code emission */
  56111. DUK_LOCAL_DECL duk_int_t duk__get_current_pc(duk_compiler_ctx *comp_ctx);
  56112. DUK_LOCAL_DECL duk_compiler_instr *duk__get_instr_ptr(duk_compiler_ctx *comp_ctx, duk_int_t pc);
  56113. DUK_LOCAL_DECL void duk__emit(duk_compiler_ctx *comp_ctx, duk_instr_t ins);
  56114. DUK_LOCAL_DECL void duk__emit_op_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t op);
  56115. DUK_LOCAL_DECL void duk__emit_a_b_c(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a, duk_regconst_t b, duk_regconst_t c);
  56116. DUK_LOCAL_DECL void duk__emit_a_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a, duk_regconst_t b);
  56117. DUK_LOCAL_DECL void duk__emit_b_c(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t b, duk_regconst_t c);
  56118. #if 0 /* unused */
  56119. DUK_LOCAL_DECL void duk__emit_a(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a);
  56120. DUK_LOCAL_DECL void duk__emit_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t b);
  56121. #endif
  56122. DUK_LOCAL_DECL void duk__emit_a_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a, duk_regconst_t bc);
  56123. DUK_LOCAL_DECL void duk__emit_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t bc);
  56124. DUK_LOCAL_DECL void duk__emit_abc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t abc);
  56125. DUK_LOCAL_DECL void duk__emit_load_int32(duk_compiler_ctx *comp_ctx, duk_regconst_t reg, duk_int32_t val);
  56126. DUK_LOCAL_DECL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_regconst_t reg, duk_int32_t val);
  56127. DUK_LOCAL_DECL void duk__emit_jump(duk_compiler_ctx *comp_ctx, duk_int_t target_pc);
  56128. DUK_LOCAL_DECL duk_int_t duk__emit_jump_empty(duk_compiler_ctx *comp_ctx);
  56129. DUK_LOCAL_DECL void duk__insert_jump_entry(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc);
  56130. DUK_LOCAL_DECL void duk__patch_jump(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc, duk_int_t target_pc);
  56131. DUK_LOCAL_DECL void duk__patch_jump_here(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc);
  56132. DUK_LOCAL_DECL void duk__patch_trycatch(duk_compiler_ctx *comp_ctx, duk_int_t ldconst_pc, duk_int_t trycatch_pc, duk_regconst_t reg_catch, duk_regconst_t const_varname, duk_small_uint_t flags);
  56133. DUK_LOCAL_DECL void duk__emit_if_false_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst);
  56134. DUK_LOCAL_DECL void duk__emit_if_true_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst);
  56135. DUK_LOCAL_DECL void duk__emit_invalid(duk_compiler_ctx *comp_ctx);
  56136. /* ivalue/ispec helpers */
  56137. DUK_LOCAL_DECL void duk__ivalue_regconst(duk_ivalue *x, duk_regconst_t regconst);
  56138. DUK_LOCAL_DECL void duk__ivalue_plain_fromstack(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56139. DUK_LOCAL_DECL void duk__ivalue_var_fromstack(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56140. DUK_LOCAL_DECL void duk__ivalue_var_hstring(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_hstring *h);
  56141. DUK_LOCAL_DECL void duk__copy_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *src, duk_ispec *dst);
  56142. DUK_LOCAL_DECL void duk__copy_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *src, duk_ivalue *dst);
  56143. DUK_LOCAL_DECL duk_regconst_t duk__alloctemps(duk_compiler_ctx *comp_ctx, duk_small_int_t num);
  56144. DUK_LOCAL_DECL duk_regconst_t duk__alloctemp(duk_compiler_ctx *comp_ctx);
  56145. DUK_LOCAL_DECL void duk__settemp_checkmax(duk_compiler_ctx *comp_ctx, duk_regconst_t temp_next);
  56146. DUK_LOCAL_DECL duk_regconst_t duk__getconst(duk_compiler_ctx *comp_ctx);
  56147. DUK_LOCAL_DECL
  56148. duk_regconst_t duk__ispec_toregconst_raw(duk_compiler_ctx *comp_ctx,
  56149. duk_ispec *x,
  56150. duk_regconst_t forced_reg,
  56151. duk_small_uint_t flags);
  56152. DUK_LOCAL_DECL void duk__ispec_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ispec *x, duk_regconst_t forced_reg);
  56153. DUK_LOCAL_DECL void duk__ivalue_toplain_raw(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_regconst_t forced_reg);
  56154. DUK_LOCAL_DECL void duk__ivalue_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56155. DUK_LOCAL_DECL void duk__ivalue_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56156. DUK_LOCAL_DECL
  56157. duk_regconst_t duk__ivalue_toregconst_raw(duk_compiler_ctx *comp_ctx,
  56158. duk_ivalue *x,
  56159. duk_regconst_t forced_reg,
  56160. duk_small_uint_t flags);
  56161. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56162. #if 0 /* unused */
  56163. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56164. #endif
  56165. DUK_LOCAL_DECL void duk__ivalue_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_int_t forced_reg);
  56166. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56167. DUK_LOCAL_DECL duk_regconst_t duk__ivalue_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x);
  56168. /* identifier handling */
  56169. DUK_LOCAL_DECL duk_regconst_t duk__lookup_active_register_binding(duk_compiler_ctx *comp_ctx);
  56170. DUK_LOCAL_DECL duk_bool_t duk__lookup_lhs(duk_compiler_ctx *ctx, duk_regconst_t *out_reg_varbind, duk_regconst_t *out_rc_varname);
  56171. /* label handling */
  56172. DUK_LOCAL_DECL void duk__add_label(duk_compiler_ctx *comp_ctx, duk_hstring *h_label, duk_int_t pc_label, duk_int_t label_id);
  56173. DUK_LOCAL_DECL void duk__update_label_flags(duk_compiler_ctx *comp_ctx, duk_int_t label_id, duk_small_uint_t flags);
  56174. DUK_LOCAL_DECL void duk__lookup_active_label(duk_compiler_ctx *comp_ctx, duk_hstring *h_label, duk_bool_t is_break, duk_int_t *out_label_id, duk_int_t *out_label_catch_depth, duk_int_t *out_label_pc, duk_bool_t *out_is_closest);
  56175. DUK_LOCAL_DECL void duk__reset_labels_to_length(duk_compiler_ctx *comp_ctx, duk_size_t len);
  56176. /* top-down expression parser */
  56177. DUK_LOCAL_DECL void duk__expr_nud(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56178. DUK_LOCAL_DECL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_ivalue *res);
  56179. DUK_LOCAL_DECL duk_small_uint_t duk__expr_lbp(duk_compiler_ctx *comp_ctx);
  56180. DUK_LOCAL_DECL duk_bool_t duk__expr_is_empty(duk_compiler_ctx *comp_ctx);
  56181. /* exprtop is the top level variant which resets nud/led counts */
  56182. DUK_LOCAL_DECL void duk__expr(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56183. DUK_LOCAL_DECL void duk__exprtop(duk_compiler_ctx *ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56184. /* convenience helpers */
  56185. #if 0 /* unused */
  56186. DUK_LOCAL_DECL duk_regconst_t duk__expr_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56187. #endif
  56188. #if 0 /* unused */
  56189. DUK_LOCAL_DECL duk_regconst_t duk__expr_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56190. #endif
  56191. DUK_LOCAL_DECL void duk__expr_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_regconst_t forced_reg);
  56192. DUK_LOCAL_DECL duk_regconst_t duk__expr_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56193. #if 0 /* unused */
  56194. DUK_LOCAL_DECL duk_regconst_t duk__expr_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56195. #endif
  56196. DUK_LOCAL_DECL void duk__expr_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56197. DUK_LOCAL_DECL void duk__expr_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56198. DUK_LOCAL_DECL duk_regconst_t duk__exprtop_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56199. #if 0 /* unused */
  56200. DUK_LOCAL_DECL duk_regconst_t duk__exprtop_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56201. #endif
  56202. DUK_LOCAL_DECL void duk__exprtop_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_regconst_t forced_reg);
  56203. DUK_LOCAL_DECL duk_regconst_t duk__exprtop_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56204. #if 0 /* unused */
  56205. DUK_LOCAL_DECL void duk__exprtop_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags);
  56206. #endif
  56207. /* expression parsing helpers */
  56208. DUK_LOCAL_DECL duk_int_t duk__parse_arguments(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56209. DUK_LOCAL_DECL void duk__nud_array_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56210. DUK_LOCAL_DECL void duk__nud_object_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56211. /* statement parsing */
  56212. DUK_LOCAL_DECL void duk__parse_var_decl(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags, duk_regconst_t *out_reg_varbind, duk_regconst_t *out_rc_varname);
  56213. DUK_LOCAL_DECL void duk__parse_var_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags);
  56214. DUK_LOCAL_DECL void duk__parse_for_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  56215. DUK_LOCAL_DECL void duk__parse_switch_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  56216. DUK_LOCAL_DECL void duk__parse_if_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56217. DUK_LOCAL_DECL void duk__parse_do_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  56218. DUK_LOCAL_DECL void duk__parse_while_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site);
  56219. DUK_LOCAL_DECL void duk__parse_break_or_continue_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56220. DUK_LOCAL_DECL void duk__parse_return_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56221. DUK_LOCAL_DECL void duk__parse_throw_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56222. DUK_LOCAL_DECL void duk__parse_try_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56223. DUK_LOCAL_DECL void duk__parse_with_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res);
  56224. DUK_LOCAL_DECL void duk__parse_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_bool_t allow_source_elem);
  56225. DUK_LOCAL_DECL duk_int_t duk__stmt_label_site(duk_compiler_ctx *comp_ctx, duk_int_t label_id);
  56226. DUK_LOCAL_DECL void duk__parse_stmts(duk_compiler_ctx *comp_ctx, duk_bool_t allow_source_elem, duk_bool_t expect_eof, duk_bool_t regexp_after);
  56227. DUK_LOCAL_DECL void duk__parse_func_body(duk_compiler_ctx *comp_ctx, duk_bool_t expect_eof, duk_bool_t implicit_return_value, duk_bool_t regexp_after, duk_small_int_t expect_token);
  56228. DUK_LOCAL_DECL void duk__parse_func_formals(duk_compiler_ctx *comp_ctx);
  56229. DUK_LOCAL_DECL void duk__parse_func_like_raw(duk_compiler_ctx *comp_ctx, duk_small_uint_t flags);
  56230. DUK_LOCAL_DECL duk_int_t duk__parse_func_like_fnum(duk_compiler_ctx *comp_ctx, duk_small_uint_t flags);
  56231. #define DUK__FUNC_FLAG_DECL (1 << 0) /* Parsing a function declaration. */
  56232. #define DUK__FUNC_FLAG_GETSET (1 << 1) /* Parsing an object literal getter/setter. */
  56233. #define DUK__FUNC_FLAG_METDEF (1 << 2) /* Parsing an object literal method definition shorthand. */
  56234. #define DUK__FUNC_FLAG_PUSHNAME_PASS1 (1 << 3) /* Push function name when creating template (first pass only). */
  56235. #define DUK__FUNC_FLAG_USE_PREVTOKEN (1 << 4) /* Use prev_token to start function parsing (workaround for object literal). */
  56236. /*
  56237. * Parser control values for tokens. The token table is ordered by the
  56238. * DUK_TOK_XXX defines.
  56239. *
  56240. * The binding powers are for lbp() use (i.e. for use in led() context).
  56241. * Binding powers are positive for typing convenience, and bits at the
  56242. * top should be reserved for flags. Binding power step must be higher
  56243. * than 1 so that binding power "lbp - 1" can be used for right associative
  56244. * operators. Currently a step of 2 is used (which frees one more bit for
  56245. * flags).
  56246. */
  56247. /* XXX: actually single step levels would work just fine, clean up */
  56248. /* binding power "levels" (see doc/compiler.rst) */
  56249. #define DUK__BP_INVALID 0 /* always terminates led() */
  56250. #define DUK__BP_EOF 2
  56251. #define DUK__BP_CLOSING 4 /* token closes expression, e.g. ')', ']' */
  56252. #define DUK__BP_FOR_EXPR DUK__BP_CLOSING /* bp to use when parsing a top level Expression */
  56253. #define DUK__BP_COMMA 6
  56254. #define DUK__BP_ASSIGNMENT 8
  56255. #define DUK__BP_CONDITIONAL 10
  56256. #define DUK__BP_LOR 12
  56257. #define DUK__BP_LAND 14
  56258. #define DUK__BP_BOR 16
  56259. #define DUK__BP_BXOR 18
  56260. #define DUK__BP_BAND 20
  56261. #define DUK__BP_EQUALITY 22
  56262. #define DUK__BP_RELATIONAL 24
  56263. #define DUK__BP_SHIFT 26
  56264. #define DUK__BP_ADDITIVE 28
  56265. #define DUK__BP_MULTIPLICATIVE 30
  56266. #define DUK__BP_EXPONENTIATION 32
  56267. #define DUK__BP_POSTFIX 34
  56268. #define DUK__BP_CALL 36
  56269. #define DUK__BP_MEMBER 38
  56270. #define DUK__TOKEN_LBP_BP_MASK 0x1f
  56271. #define DUK__TOKEN_LBP_FLAG_NO_REGEXP (1 << 5) /* regexp literal must not follow this token */
  56272. #define DUK__TOKEN_LBP_FLAG_TERMINATES (1 << 6) /* terminates expression; e.g. post-increment/-decrement */
  56273. #define DUK__TOKEN_LBP_FLAG_UNUSED (1 << 7) /* unused */
  56274. #define DUK__TOKEN_LBP_GET_BP(x) ((duk_small_uint_t) (((x) & DUK__TOKEN_LBP_BP_MASK) * 2))
  56275. #define DUK__MK_LBP(bp) ((bp) >> 1) /* bp is assumed to be even */
  56276. #define DUK__MK_LBP_FLAGS(bp,flags) (((bp) >> 1) | (flags))
  56277. DUK_LOCAL const duk_uint8_t duk__token_lbp[] = {
  56278. DUK__MK_LBP(DUK__BP_EOF), /* DUK_TOK_EOF */
  56279. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_IDENTIFIER */
  56280. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_BREAK */
  56281. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CASE */
  56282. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CATCH */
  56283. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CONTINUE */
  56284. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DEBUGGER */
  56285. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DEFAULT */
  56286. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DELETE */
  56287. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_DO */
  56288. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_ELSE */
  56289. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FINALLY */
  56290. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FOR */
  56291. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_FUNCTION */
  56292. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IF */
  56293. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_IN */
  56294. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_INSTANCEOF */
  56295. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_NEW */
  56296. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_RETURN */
  56297. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SWITCH */
  56298. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_THIS */
  56299. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_THROW */
  56300. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_TRY */
  56301. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_TYPEOF */
  56302. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_VAR */
  56303. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CONST */
  56304. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_VOID */
  56305. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_WHILE */
  56306. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_WITH */
  56307. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_CLASS */
  56308. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_ENUM */
  56309. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_EXPORT */
  56310. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_EXTENDS */
  56311. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IMPORT */
  56312. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SUPER */
  56313. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_NULL */
  56314. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_TRUE */
  56315. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_FALSE */
  56316. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_GET */
  56317. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SET */
  56318. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_IMPLEMENTS */
  56319. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_INTERFACE */
  56320. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LET */
  56321. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PACKAGE */
  56322. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PRIVATE */
  56323. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PROTECTED */
  56324. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_PUBLIC */
  56325. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_STATIC */
  56326. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_YIELD */
  56327. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LCURLY */
  56328. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RCURLY */
  56329. DUK__MK_LBP(DUK__BP_MEMBER), /* DUK_TOK_LBRACKET */
  56330. DUK__MK_LBP_FLAGS(DUK__BP_CLOSING, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RBRACKET */
  56331. DUK__MK_LBP(DUK__BP_CALL), /* DUK_TOK_LPAREN */
  56332. DUK__MK_LBP_FLAGS(DUK__BP_CLOSING, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_RPAREN */
  56333. DUK__MK_LBP(DUK__BP_MEMBER), /* DUK_TOK_PERIOD */
  56334. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_SEMICOLON */
  56335. DUK__MK_LBP(DUK__BP_COMMA), /* DUK_TOK_COMMA */
  56336. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_LT */
  56337. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_GT */
  56338. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_LE */
  56339. DUK__MK_LBP(DUK__BP_RELATIONAL), /* DUK_TOK_GE */
  56340. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_EQ */
  56341. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_NEQ */
  56342. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_SEQ */
  56343. DUK__MK_LBP(DUK__BP_EQUALITY), /* DUK_TOK_SNEQ */
  56344. DUK__MK_LBP(DUK__BP_ADDITIVE), /* DUK_TOK_ADD */
  56345. DUK__MK_LBP(DUK__BP_ADDITIVE), /* DUK_TOK_SUB */
  56346. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_MUL */
  56347. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_DIV */
  56348. DUK__MK_LBP(DUK__BP_MULTIPLICATIVE), /* DUK_TOK_MOD */
  56349. DUK__MK_LBP(DUK__BP_EXPONENTIATION), /* DUK_TOK_EXP */
  56350. DUK__MK_LBP(DUK__BP_POSTFIX), /* DUK_TOK_INCREMENT */
  56351. DUK__MK_LBP(DUK__BP_POSTFIX), /* DUK_TOK_DECREMENT */
  56352. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_ALSHIFT */
  56353. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_ARSHIFT */
  56354. DUK__MK_LBP(DUK__BP_SHIFT), /* DUK_TOK_RSHIFT */
  56355. DUK__MK_LBP(DUK__BP_BAND), /* DUK_TOK_BAND */
  56356. DUK__MK_LBP(DUK__BP_BOR), /* DUK_TOK_BOR */
  56357. DUK__MK_LBP(DUK__BP_BXOR), /* DUK_TOK_BXOR */
  56358. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_LNOT */
  56359. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_BNOT */
  56360. DUK__MK_LBP(DUK__BP_LAND), /* DUK_TOK_LAND */
  56361. DUK__MK_LBP(DUK__BP_LOR), /* DUK_TOK_LOR */
  56362. DUK__MK_LBP(DUK__BP_CONDITIONAL), /* DUK_TOK_QUESTION */
  56363. DUK__MK_LBP(DUK__BP_INVALID), /* DUK_TOK_COLON */
  56364. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_EQUALSIGN */
  56365. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ADD_EQ */
  56366. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_SUB_EQ */
  56367. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_MUL_EQ */
  56368. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_DIV_EQ */
  56369. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_MOD_EQ */
  56370. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_EXP_EQ */
  56371. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ALSHIFT_EQ */
  56372. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_ARSHIFT_EQ */
  56373. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_RSHIFT_EQ */
  56374. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BAND_EQ */
  56375. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BOR_EQ */
  56376. DUK__MK_LBP(DUK__BP_ASSIGNMENT), /* DUK_TOK_BXOR_EQ */
  56377. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_NUMBER */
  56378. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_STRING */
  56379. DUK__MK_LBP_FLAGS(DUK__BP_INVALID, DUK__TOKEN_LBP_FLAG_NO_REGEXP), /* DUK_TOK_REGEXP */
  56380. };
  56381. /*
  56382. * Misc helpers
  56383. */
  56384. DUK_LOCAL void duk__comp_recursion_increase(duk_compiler_ctx *comp_ctx) {
  56385. DUK_ASSERT(comp_ctx != NULL);
  56386. DUK_ASSERT(comp_ctx->recursion_depth >= 0);
  56387. if (comp_ctx->recursion_depth >= comp_ctx->recursion_limit) {
  56388. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_COMPILER_RECURSION_LIMIT);
  56389. DUK_WO_NORETURN(return;);
  56390. }
  56391. comp_ctx->recursion_depth++;
  56392. }
  56393. DUK_LOCAL void duk__comp_recursion_decrease(duk_compiler_ctx *comp_ctx) {
  56394. DUK_ASSERT(comp_ctx != NULL);
  56395. DUK_ASSERT(comp_ctx->recursion_depth > 0);
  56396. comp_ctx->recursion_depth--;
  56397. }
  56398. DUK_LOCAL duk_bool_t duk__hstring_is_eval_or_arguments(duk_compiler_ctx *comp_ctx, duk_hstring *h) {
  56399. DUK_UNREF(comp_ctx);
  56400. DUK_ASSERT(h != NULL);
  56401. return DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(h);
  56402. }
  56403. DUK_LOCAL duk_bool_t duk__hstring_is_eval_or_arguments_in_strict_mode(duk_compiler_ctx *comp_ctx, duk_hstring *h) {
  56404. DUK_ASSERT(h != NULL);
  56405. return (comp_ctx->curr_func.is_strict &&
  56406. DUK_HSTRING_HAS_EVAL_OR_ARGUMENTS(h));
  56407. }
  56408. /*
  56409. * Parser duk__advance() token eating functions
  56410. */
  56411. /* XXX: valstack handling is awkward. Add a valstack helper which
  56412. * avoids dup():ing; valstack_copy(src, dst)?
  56413. */
  56414. DUK_LOCAL void duk__advance_helper(duk_compiler_ctx *comp_ctx, duk_small_int_t expect) {
  56415. duk_hthread *thr = comp_ctx->thr;
  56416. duk_bool_t regexp;
  56417. DUK_ASSERT_DISABLE(comp_ctx->curr_token.t >= 0); /* unsigned */
  56418. DUK_ASSERT(comp_ctx->curr_token.t <= DUK_TOK_MAXVAL); /* MAXVAL is inclusive */
  56419. /*
  56420. * Use current token to decide whether a RegExp can follow.
  56421. *
  56422. * We can use either 't' or 't_nores'; the latter would not
  56423. * recognize keywords. Some keywords can be followed by a
  56424. * RegExp (e.g. "return"), so using 't' is better. This is
  56425. * not trivial, see doc/compiler.rst.
  56426. */
  56427. regexp = 1;
  56428. if (duk__token_lbp[comp_ctx->curr_token.t] & DUK__TOKEN_LBP_FLAG_NO_REGEXP) {
  56429. regexp = 0;
  56430. }
  56431. if (comp_ctx->curr_func.reject_regexp_in_adv) {
  56432. comp_ctx->curr_func.reject_regexp_in_adv = 0;
  56433. regexp = 0;
  56434. }
  56435. if (comp_ctx->curr_func.allow_regexp_in_adv) {
  56436. comp_ctx->curr_func.allow_regexp_in_adv = 0;
  56437. regexp = 1;
  56438. }
  56439. if (expect >= 0 && comp_ctx->curr_token.t != (duk_small_uint_t) expect) {
  56440. DUK_D(DUK_DPRINT("parse error: expect=%ld, got=%ld",
  56441. (long) expect, (long) comp_ctx->curr_token.t));
  56442. DUK_ERROR_SYNTAX(thr, DUK_STR_PARSE_ERROR);
  56443. DUK_WO_NORETURN(return;);
  56444. }
  56445. /* make current token the previous; need to fiddle with valstack "backing store" */
  56446. duk_memcpy(&comp_ctx->prev_token, &comp_ctx->curr_token, sizeof(duk_token));
  56447. duk_copy(thr, comp_ctx->tok11_idx, comp_ctx->tok21_idx);
  56448. duk_copy(thr, comp_ctx->tok12_idx, comp_ctx->tok22_idx);
  56449. /* parse new token */
  56450. duk_lexer_parse_js_input_element(&comp_ctx->lex,
  56451. &comp_ctx->curr_token,
  56452. comp_ctx->curr_func.is_strict,
  56453. regexp);
  56454. DUK_DDD(DUK_DDDPRINT("advance: curr: tok=%ld/%ld,%ld,term=%ld,%!T,%!T "
  56455. "prev: tok=%ld/%ld,%ld,term=%ld,%!T,%!T",
  56456. (long) comp_ctx->curr_token.t,
  56457. (long) comp_ctx->curr_token.t_nores,
  56458. (long) comp_ctx->curr_token.start_line,
  56459. (long) comp_ctx->curr_token.lineterm,
  56460. (duk_tval *) duk_get_tval(thr, comp_ctx->tok11_idx),
  56461. (duk_tval *) duk_get_tval(thr, comp_ctx->tok12_idx),
  56462. (long) comp_ctx->prev_token.t,
  56463. (long) comp_ctx->prev_token.t_nores,
  56464. (long) comp_ctx->prev_token.start_line,
  56465. (long) comp_ctx->prev_token.lineterm,
  56466. (duk_tval *) duk_get_tval(thr, comp_ctx->tok21_idx),
  56467. (duk_tval *) duk_get_tval(thr, comp_ctx->tok22_idx)));
  56468. }
  56469. /* advance, expecting current token to be a specific token; parse next token in regexp context */
  56470. DUK_LOCAL void duk__advance_expect(duk_compiler_ctx *comp_ctx, duk_small_int_t expect) {
  56471. duk__advance_helper(comp_ctx, expect);
  56472. }
  56473. /* advance, whatever the current token is; parse next token in regexp context */
  56474. DUK_LOCAL void duk__advance(duk_compiler_ctx *comp_ctx) {
  56475. duk__advance_helper(comp_ctx, -1);
  56476. }
  56477. /*
  56478. * Helpers for duk_compiler_func.
  56479. */
  56480. /* init function state: inits valstack allocations */
  56481. DUK_LOCAL void duk__init_func_valstack_slots(duk_compiler_ctx *comp_ctx) {
  56482. duk_compiler_func *func = &comp_ctx->curr_func;
  56483. duk_hthread *thr = comp_ctx->thr;
  56484. duk_idx_t entry_top;
  56485. entry_top = duk_get_top(thr);
  56486. duk_memzero(func, sizeof(*func)); /* intentional overlap with earlier memzero */
  56487. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  56488. func->h_name = NULL;
  56489. func->h_consts = NULL;
  56490. func->h_funcs = NULL;
  56491. func->h_decls = NULL;
  56492. func->h_labelnames = NULL;
  56493. func->h_labelinfos = NULL;
  56494. func->h_argnames = NULL;
  56495. func->h_varmap = NULL;
  56496. #endif
  56497. duk_require_stack(thr, DUK__FUNCTION_INIT_REQUIRE_SLOTS);
  56498. DUK_BW_INIT_PUSHBUF(thr, &func->bw_code, DUK__BC_INITIAL_INSTS * sizeof(duk_compiler_instr));
  56499. /* code_idx = entry_top + 0 */
  56500. duk_push_array(thr);
  56501. func->consts_idx = entry_top + 1;
  56502. func->h_consts = DUK_GET_HOBJECT_POSIDX(thr, entry_top + 1);
  56503. DUK_ASSERT(func->h_consts != NULL);
  56504. duk_push_array(thr);
  56505. func->funcs_idx = entry_top + 2;
  56506. func->h_funcs = DUK_GET_HOBJECT_POSIDX(thr, entry_top + 2);
  56507. DUK_ASSERT(func->h_funcs != NULL);
  56508. DUK_ASSERT(func->fnum_next == 0);
  56509. duk_push_array(thr);
  56510. func->decls_idx = entry_top + 3;
  56511. func->h_decls = DUK_GET_HOBJECT_POSIDX(thr, entry_top + 3);
  56512. DUK_ASSERT(func->h_decls != NULL);
  56513. duk_push_array(thr);
  56514. func->labelnames_idx = entry_top + 4;
  56515. func->h_labelnames = DUK_GET_HOBJECT_POSIDX(thr, entry_top + 4);
  56516. DUK_ASSERT(func->h_labelnames != NULL);
  56517. duk_push_dynamic_buffer(thr, 0);
  56518. func->labelinfos_idx = entry_top + 5;
  56519. func->h_labelinfos = (duk_hbuffer_dynamic *) duk_known_hbuffer(thr, entry_top + 5);
  56520. DUK_ASSERT(func->h_labelinfos != NULL);
  56521. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(func->h_labelinfos) && !DUK_HBUFFER_HAS_EXTERNAL(func->h_labelinfos));
  56522. duk_push_array(thr);
  56523. func->argnames_idx = entry_top + 6;
  56524. func->h_argnames = DUK_GET_HOBJECT_POSIDX(thr, entry_top + 6);
  56525. DUK_ASSERT(func->h_argnames != NULL);
  56526. duk_push_bare_object(thr);
  56527. func->varmap_idx = entry_top + 7;
  56528. func->h_varmap = DUK_GET_HOBJECT_POSIDX(thr, entry_top + 7);
  56529. DUK_ASSERT(func->h_varmap != NULL);
  56530. }
  56531. /* reset function state (prepare for pass 2) */
  56532. DUK_LOCAL void duk__reset_func_for_pass2(duk_compiler_ctx *comp_ctx) {
  56533. duk_compiler_func *func = &comp_ctx->curr_func;
  56534. duk_hthread *thr = comp_ctx->thr;
  56535. /* reset bytecode buffer but keep current size; pass 2 will
  56536. * require same amount or more.
  56537. */
  56538. DUK_BW_RESET_SIZE(thr, &func->bw_code);
  56539. duk_set_length(thr, func->consts_idx, 0);
  56540. /* keep func->h_funcs; inner functions are not reparsed to avoid O(depth^2) parsing */
  56541. func->fnum_next = 0;
  56542. /* duk_set_length(thr, func->funcs_idx, 0); */
  56543. duk_set_length(thr, func->labelnames_idx, 0);
  56544. duk_hbuffer_reset(thr, func->h_labelinfos);
  56545. /* keep func->h_argnames; it is fixed for all passes */
  56546. /* truncated in case pass 3 needed */
  56547. duk_push_bare_object(thr);
  56548. duk_replace(thr, func->varmap_idx);
  56549. func->h_varmap = DUK_GET_HOBJECT_POSIDX(thr, func->varmap_idx);
  56550. DUK_ASSERT(func->h_varmap != NULL);
  56551. }
  56552. /* cleanup varmap from any null entries, compact it, etc; returns number
  56553. * of final entries after cleanup.
  56554. */
  56555. DUK_LOCAL duk_int_t duk__cleanup_varmap(duk_compiler_ctx *comp_ctx) {
  56556. duk_hthread *thr = comp_ctx->thr;
  56557. duk_hobject *h_varmap;
  56558. duk_hstring *h_key;
  56559. duk_tval *tv;
  56560. duk_uint32_t i, e_next;
  56561. duk_int_t ret;
  56562. /* [ ... varmap ] */
  56563. h_varmap = DUK_GET_HOBJECT_NEGIDX(thr, -1);
  56564. DUK_ASSERT(h_varmap != NULL);
  56565. ret = 0;
  56566. e_next = DUK_HOBJECT_GET_ENEXT(h_varmap);
  56567. for (i = 0; i < e_next; i++) {
  56568. h_key = DUK_HOBJECT_E_GET_KEY(thr->heap, h_varmap, i);
  56569. if (!h_key) {
  56570. continue;
  56571. }
  56572. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, h_varmap, i));
  56573. /* The entries can either be register numbers or 'null' values.
  56574. * Thus, no need to DECREF them and get side effects. DECREF'ing
  56575. * the keys (strings) can cause memory to be freed but no side
  56576. * effects as strings don't have finalizers. This is why we can
  56577. * rely on the object properties not changing from underneath us.
  56578. */
  56579. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, h_varmap, i);
  56580. if (!DUK_TVAL_IS_NUMBER(tv)) {
  56581. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv));
  56582. DUK_HOBJECT_E_SET_KEY(thr->heap, h_varmap, i, NULL);
  56583. DUK_HSTRING_DECREF(thr, h_key);
  56584. /* when key is NULL, value is garbage so no need to set */
  56585. } else {
  56586. ret++;
  56587. }
  56588. }
  56589. duk_compact_m1(thr);
  56590. return ret;
  56591. }
  56592. /* Convert duk_compiler_func into a function template, leaving the result
  56593. * on top of stack.
  56594. */
  56595. /* XXX: awkward and bloated asm -- use faster internal accesses */
  56596. DUK_LOCAL void duk__convert_to_func_template(duk_compiler_ctx *comp_ctx) {
  56597. duk_compiler_func *func = &comp_ctx->curr_func;
  56598. duk_hthread *thr = comp_ctx->thr;
  56599. duk_hcompfunc *h_res;
  56600. duk_hbuffer_fixed *h_data;
  56601. duk_size_t consts_count;
  56602. duk_size_t funcs_count;
  56603. duk_size_t code_count;
  56604. duk_size_t code_size;
  56605. duk_size_t data_size;
  56606. duk_size_t i;
  56607. duk_tval *p_const;
  56608. duk_hobject **p_func;
  56609. duk_instr_t *p_instr;
  56610. duk_compiler_instr *q_instr;
  56611. duk_tval *tv;
  56612. duk_bool_t keep_varmap;
  56613. duk_bool_t keep_formals;
  56614. #if !defined(DUK_USE_DEBUGGER_SUPPORT)
  56615. duk_size_t formals_length;
  56616. #endif
  56617. DUK_DDD(DUK_DDDPRINT("converting duk_compiler_func to function/template"));
  56618. /*
  56619. * Push result object and init its flags
  56620. */
  56621. /* Valstack should suffice here, required on function valstack init */
  56622. h_res = duk_push_hcompfunc(thr);
  56623. DUK_ASSERT(h_res != NULL);
  56624. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) h_res) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  56625. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, (duk_hobject *) h_res, NULL); /* Function templates are "bare objects". */
  56626. if (func->is_function) {
  56627. DUK_DDD(DUK_DDDPRINT("function -> set NEWENV"));
  56628. DUK_HOBJECT_SET_NEWENV((duk_hobject *) h_res);
  56629. if (!func->is_arguments_shadowed) {
  56630. /* arguments object would be accessible; note that shadowing
  56631. * bindings are arguments or function declarations, neither
  56632. * of which are deletable, so this is safe.
  56633. */
  56634. if (func->id_access_arguments || func->may_direct_eval) {
  56635. DUK_DDD(DUK_DDDPRINT("function may access 'arguments' object directly or "
  56636. "indirectly -> set CREATEARGS"));
  56637. DUK_HOBJECT_SET_CREATEARGS((duk_hobject *) h_res);
  56638. }
  56639. }
  56640. } else if (func->is_eval && func->is_strict) {
  56641. DUK_DDD(DUK_DDDPRINT("strict eval code -> set NEWENV"));
  56642. DUK_HOBJECT_SET_NEWENV((duk_hobject *) h_res);
  56643. } else {
  56644. /* non-strict eval: env is caller's env or global env (direct vs. indirect call)
  56645. * global code: env is is global env
  56646. */
  56647. DUK_DDD(DUK_DDDPRINT("non-strict eval code or global code -> no NEWENV"));
  56648. DUK_ASSERT(!DUK_HOBJECT_HAS_NEWENV((duk_hobject *) h_res));
  56649. }
  56650. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  56651. if (func->is_function && func->is_namebinding && func->h_name != NULL) {
  56652. /* Object literal set/get functions have a name (property
  56653. * name) but must not have a lexical name binding, see
  56654. * test-bug-getset-func-name.js.
  56655. */
  56656. DUK_DDD(DUK_DDDPRINT("function expression with a name -> set NAMEBINDING"));
  56657. DUK_HOBJECT_SET_NAMEBINDING((duk_hobject *) h_res);
  56658. }
  56659. #endif
  56660. if (func->is_strict) {
  56661. DUK_DDD(DUK_DDDPRINT("function is strict -> set STRICT"));
  56662. DUK_HOBJECT_SET_STRICT((duk_hobject *) h_res);
  56663. }
  56664. if (func->is_notail) {
  56665. DUK_DDD(DUK_DDDPRINT("function is notail -> set NOTAIL"));
  56666. DUK_HOBJECT_SET_NOTAIL((duk_hobject *) h_res);
  56667. }
  56668. if (func->is_constructable) {
  56669. DUK_DDD(DUK_DDDPRINT("function is constructable -> set CONSTRUCTABLE"));
  56670. DUK_HOBJECT_SET_CONSTRUCTABLE((duk_hobject *) h_res);
  56671. }
  56672. /*
  56673. * Build function fixed size 'data' buffer, which contains bytecode,
  56674. * constants, and inner function references.
  56675. *
  56676. * During the building phase 'data' is reachable but incomplete.
  56677. * Only incref's occur during building (no refzero or GC happens),
  56678. * so the building process is atomic.
  56679. */
  56680. consts_count = duk_hobject_get_length(thr, func->h_consts);
  56681. funcs_count = duk_hobject_get_length(thr, func->h_funcs) / 3;
  56682. code_count = DUK_BW_GET_SIZE(thr, &func->bw_code) / sizeof(duk_compiler_instr);
  56683. code_size = code_count * sizeof(duk_instr_t);
  56684. data_size = consts_count * sizeof(duk_tval) +
  56685. funcs_count * sizeof(duk_hobject *) +
  56686. code_size;
  56687. DUK_DDD(DUK_DDDPRINT("consts_count=%ld, funcs_count=%ld, code_size=%ld -> "
  56688. "data_size=%ld*%ld + %ld*%ld + %ld = %ld",
  56689. (long) consts_count, (long) funcs_count, (long) code_size,
  56690. (long) consts_count, (long) sizeof(duk_tval),
  56691. (long) funcs_count, (long) sizeof(duk_hobject *),
  56692. (long) code_size, (long) data_size));
  56693. duk_push_fixed_buffer_nozero(thr, data_size);
  56694. h_data = (duk_hbuffer_fixed *) (void *) duk_known_hbuffer(thr, -1);
  56695. DUK_HCOMPFUNC_SET_DATA(thr->heap, h_res, (duk_hbuffer *) h_data);
  56696. DUK_HEAPHDR_INCREF(thr, h_data);
  56697. p_const = (duk_tval *) (void *) DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_data);
  56698. for (i = 0; i < consts_count; i++) {
  56699. DUK_ASSERT(i <= DUK_UARRIDX_MAX); /* const limits */
  56700. tv = duk_hobject_find_existing_array_entry_tval_ptr(thr->heap, func->h_consts, (duk_uarridx_t) i);
  56701. DUK_ASSERT(tv != NULL);
  56702. DUK_TVAL_SET_TVAL(p_const, tv);
  56703. p_const++;
  56704. DUK_TVAL_INCREF(thr, tv); /* may be a string constant */
  56705. DUK_DDD(DUK_DDDPRINT("constant: %!T", (duk_tval *) tv));
  56706. }
  56707. p_func = (duk_hobject **) p_const;
  56708. DUK_HCOMPFUNC_SET_FUNCS(thr->heap, h_res, p_func);
  56709. for (i = 0; i < funcs_count; i++) {
  56710. duk_hobject *h;
  56711. DUK_ASSERT(i * 3 <= DUK_UARRIDX_MAX); /* func limits */
  56712. tv = duk_hobject_find_existing_array_entry_tval_ptr(thr->heap, func->h_funcs, (duk_uarridx_t) (i * 3));
  56713. DUK_ASSERT(tv != NULL);
  56714. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  56715. h = DUK_TVAL_GET_OBJECT(tv);
  56716. DUK_ASSERT(h != NULL);
  56717. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(h));
  56718. *p_func++ = h;
  56719. DUK_HOBJECT_INCREF(thr, h);
  56720. DUK_DDD(DUK_DDDPRINT("inner function: %p -> %!iO",
  56721. (void *) h, (duk_heaphdr *) h));
  56722. }
  56723. p_instr = (duk_instr_t *) p_func;
  56724. DUK_HCOMPFUNC_SET_BYTECODE(thr->heap, h_res, p_instr);
  56725. /* copy bytecode instructions one at a time */
  56726. q_instr = (duk_compiler_instr *) (void *) DUK_BW_GET_BASEPTR(thr, &func->bw_code);
  56727. for (i = 0; i < code_count; i++) {
  56728. p_instr[i] = q_instr[i].ins;
  56729. }
  56730. /* Note: 'q_instr' is still used below */
  56731. DUK_ASSERT((duk_uint8_t *) (p_instr + code_count) == DUK_HBUFFER_FIXED_GET_DATA_PTR(thr->heap, h_data) + data_size);
  56732. duk_pop(thr); /* 'data' (and everything in it) is reachable through h_res now */
  56733. /*
  56734. * Init non-property result fields
  56735. *
  56736. * 'nregs' controls how large a register frame is allocated.
  56737. *
  56738. * 'nargs' controls how many formal arguments are written to registers:
  56739. * r0, ... r(nargs-1). The remaining registers are initialized to
  56740. * undefined.
  56741. */
  56742. DUK_ASSERT(func->temp_max >= 0);
  56743. h_res->nregs = (duk_uint16_t) func->temp_max;
  56744. h_res->nargs = (duk_uint16_t) duk_hobject_get_length(thr, func->h_argnames);
  56745. DUK_ASSERT(h_res->nregs >= h_res->nargs); /* pass2 allocation handles this */
  56746. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  56747. h_res->start_line = (duk_uint32_t) func->min_line;
  56748. h_res->end_line = (duk_uint32_t) func->max_line;
  56749. #endif
  56750. /*
  56751. * Init object properties
  56752. *
  56753. * Properties should be added in decreasing order of access frequency.
  56754. * (Not very critical for function templates.)
  56755. */
  56756. DUK_DDD(DUK_DDDPRINT("init function properties"));
  56757. /* [ ... res ] */
  56758. /* _Varmap: omitted if function is guaranteed not to do a slow path
  56759. * identifier access that might be caught by locally declared variables.
  56760. * The varmap can also be omitted if it turns out empty of actual
  56761. * register mappings after a cleanup. When debugging is enabled, we
  56762. * always need the varmap to be able to lookup variables at any point.
  56763. */
  56764. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  56765. DUK_DD(DUK_DDPRINT("keeping _Varmap because debugger support is enabled"));
  56766. keep_varmap = 1;
  56767. #else
  56768. if (func->id_access_slow_own || /* directly uses slow accesses that may match own variables */
  56769. func->id_access_arguments || /* accesses 'arguments' directly */
  56770. func->may_direct_eval || /* may indirectly slow access through a direct eval */
  56771. funcs_count > 0) { /* has inner functions which may slow access (XXX: this can be optimized by looking at the inner functions) */
  56772. DUK_DD(DUK_DDPRINT("keeping _Varmap because of direct eval, slow path access that may match local variables, or presence of inner functions"));
  56773. keep_varmap = 1;
  56774. } else {
  56775. DUK_DD(DUK_DDPRINT("dropping _Varmap"));
  56776. keep_varmap = 0;
  56777. }
  56778. #endif
  56779. if (keep_varmap) {
  56780. duk_int_t num_used;
  56781. duk_dup(thr, func->varmap_idx);
  56782. num_used = duk__cleanup_varmap(comp_ctx);
  56783. DUK_DDD(DUK_DDDPRINT("cleaned up varmap: %!T (num_used=%ld)",
  56784. (duk_tval *) duk_get_tval(thr, -1), (long) num_used));
  56785. if (num_used > 0) {
  56786. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_VARMAP, DUK_PROPDESC_FLAGS_NONE);
  56787. } else {
  56788. DUK_DD(DUK_DDPRINT("varmap is empty after cleanup -> no need to add"));
  56789. duk_pop(thr);
  56790. }
  56791. }
  56792. /* _Formals: omitted if function is guaranteed not to need a (non-strict)
  56793. * arguments object, and _Formals.length matches nargs exactly.
  56794. *
  56795. * Non-arrow functions can't see an outer function's 'argument' binding
  56796. * (because they have their own), but arrow functions can. When arrow
  56797. * functions are added, this condition would need to be added:
  56798. * inner_arrow_funcs_count > 0 inner arrow functions may access 'arguments'
  56799. */
  56800. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  56801. DUK_DD(DUK_DDPRINT("keeping _Formals because debugger support is enabled"));
  56802. keep_formals = 1;
  56803. #else
  56804. formals_length = duk_get_length(thr, func->argnames_idx);
  56805. if (formals_length != (duk_size_t) h_res->nargs) {
  56806. /* Nargs not enough for closure .length: keep _Formals regardless
  56807. * of its length. Shouldn't happen in practice at the moment.
  56808. */
  56809. DUK_DD(DUK_DDPRINT("keeping _Formals because _Formals.length != nargs"));
  56810. keep_formals = 1;
  56811. } else if ((func->id_access_arguments || func->may_direct_eval) &&
  56812. (formals_length > 0)) {
  56813. /* Direct eval (may access 'arguments') or accesses 'arguments'
  56814. * explicitly: keep _Formals unless it is zero length.
  56815. */
  56816. DUK_DD(DUK_DDPRINT("keeping _Formals because of direct eval or explicit access to 'arguments', and _Formals.length != 0"));
  56817. keep_formals = 1;
  56818. } else {
  56819. DUK_DD(DUK_DDPRINT("omitting _Formals, nargs matches _Formals.length, so no properties added"));
  56820. keep_formals = 0;
  56821. }
  56822. #endif
  56823. if (keep_formals) {
  56824. duk_dup(thr, func->argnames_idx);
  56825. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_FORMALS, DUK_PROPDESC_FLAGS_NONE);
  56826. }
  56827. /* name */
  56828. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  56829. if (func->h_name) {
  56830. duk_push_hstring(thr, func->h_name);
  56831. DUK_DD(DUK_DDPRINT("setting function template .name to %!T", duk_get_tval(thr, -1)));
  56832. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_NONE);
  56833. }
  56834. #endif /* DUK_USE_FUNC_NAME_PROPERTY */
  56835. /* _Source */
  56836. #if defined(DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY)
  56837. if (0) {
  56838. /* XXX: Currently function source code is not stored, as it is not
  56839. * required by the standard. Source code should not be stored by
  56840. * default (user should enable it explicitly), and the source should
  56841. * probably be compressed with a trivial text compressor; average
  56842. * compression of 20-30% is quite easy to achieve even with a trivial
  56843. * compressor (RLE + backwards lookup).
  56844. *
  56845. * Debugging needs source code to be useful: sometimes input code is
  56846. * not found in files as it may be generated and then eval()'d, given
  56847. * by dynamic C code, etc.
  56848. *
  56849. * Other issues:
  56850. *
  56851. * - Need tokenizer indices for start and end to substring
  56852. * - Always normalize function declaration part?
  56853. * - If we keep _Formals, only need to store body
  56854. */
  56855. /*
  56856. * For global or eval code this is straightforward. For functions
  56857. * created with the Function constructor we only get the source for
  56858. * the body and must manufacture the "function ..." part.
  56859. *
  56860. * For instance, for constructed functions (v8):
  56861. *
  56862. * > a = new Function("foo", "bar", "print(foo)");
  56863. * [Function]
  56864. * > a.toString()
  56865. * 'function anonymous(foo,bar) {\nprint(foo)\n}'
  56866. *
  56867. * Similarly for e.g. getters (v8):
  56868. *
  56869. * > x = { get a(foo,bar) { print(foo); } }
  56870. * { a: [Getter] }
  56871. * > Object.getOwnPropertyDescriptor(x, 'a').get.toString()
  56872. * 'function a(foo,bar) { print(foo); }'
  56873. */
  56874. #if 0
  56875. duk_push_literal(thr, "XXX");
  56876. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_SOURCE, DUK_PROPDESC_FLAGS_NONE);
  56877. #endif
  56878. }
  56879. #endif /* DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY */
  56880. /* _Pc2line */
  56881. #if defined(DUK_USE_PC2LINE)
  56882. if (1) {
  56883. /*
  56884. * Size-optimized pc->line mapping.
  56885. */
  56886. DUK_ASSERT(code_count <= DUK_COMPILER_MAX_BYTECODE_LENGTH);
  56887. duk_hobject_pc2line_pack(thr, q_instr, (duk_uint_fast32_t) code_count); /* -> pushes fixed buffer */
  56888. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_PC2LINE, DUK_PROPDESC_FLAGS_NONE);
  56889. /* XXX: if assertions enabled, walk through all valid PCs
  56890. * and check line mapping.
  56891. */
  56892. }
  56893. #endif /* DUK_USE_PC2LINE */
  56894. /* fileName */
  56895. #if defined(DUK_USE_FUNC_FILENAME_PROPERTY)
  56896. if (comp_ctx->h_filename) {
  56897. /*
  56898. * Source filename (or equivalent), for identifying thrown errors.
  56899. */
  56900. duk_push_hstring(thr, comp_ctx->h_filename);
  56901. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_FILE_NAME, DUK_PROPDESC_FLAGS_NONE);
  56902. }
  56903. #endif
  56904. DUK_DD(DUK_DDPRINT("converted function: %!ixT",
  56905. (duk_tval *) duk_get_tval(thr, -1)));
  56906. /*
  56907. * Compact the function template.
  56908. */
  56909. duk_compact_m1(thr);
  56910. /*
  56911. * Debug dumping
  56912. */
  56913. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  56914. {
  56915. duk_hcompfunc *h;
  56916. duk_instr_t *p, *p_start, *p_end;
  56917. h = (duk_hcompfunc *) duk_get_hobject(thr, -1);
  56918. p_start = (duk_instr_t *) DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, h);
  56919. p_end = (duk_instr_t *) DUK_HCOMPFUNC_GET_CODE_END(thr->heap, h);
  56920. p = p_start;
  56921. while (p < p_end) {
  56922. DUK_DDD(DUK_DDDPRINT("BC %04ld: %!I ; 0x%08lx op=%ld (%!C) a=%ld b=%ld c=%ld",
  56923. (long) (p - p_start),
  56924. (duk_instr_t) (*p),
  56925. (unsigned long) (*p),
  56926. (long) DUK_DEC_OP(*p),
  56927. (long) DUK_DEC_OP(*p),
  56928. (long) DUK_DEC_A(*p),
  56929. (long) DUK_DEC_B(*p),
  56930. (long) DUK_DEC_C(*p)));
  56931. p++;
  56932. }
  56933. }
  56934. #endif
  56935. }
  56936. /*
  56937. * Code emission helpers
  56938. *
  56939. * Some emission helpers understand the range of target and source reg/const
  56940. * values and automatically emit shuffling code if necessary. This is the
  56941. * case when the slot in question (A, B, C) is used in the standard way and
  56942. * for opcodes the emission helpers explicitly understand (like DUK_OP_MPUTOBJ).
  56943. *
  56944. * The standard way is that:
  56945. * - slot A is a target register
  56946. * - slot B is a source register/constant
  56947. * - slot C is a source register/constant
  56948. *
  56949. * If a slot is used in a non-standard way the caller must indicate this
  56950. * somehow. If a slot is used as a target instead of a source (or vice
  56951. * versa), this can be indicated with a flag to trigger proper shuffling
  56952. * (e.g. DUK__EMIT_FLAG_B_IS_TARGET). If the value in the slot is not
  56953. * register/const related at all, the caller must ensure that the raw value
  56954. * fits into the corresponding slot so as to not trigger shuffling. The
  56955. * caller must set a "no shuffle" flag to ensure compilation fails if
  56956. * shuffling were to be triggered because of an internal error.
  56957. *
  56958. * For slots B and C the raw slot size is 9 bits but one bit is reserved for
  56959. * the reg/const indicator. To use the full 9-bit range for a raw value,
  56960. * shuffling must be disabled with the DUK__EMIT_FLAG_NO_SHUFFLE_{B,C} flag.
  56961. * Shuffling is only done for A, B, and C slots, not the larger BC or ABC slots.
  56962. *
  56963. * There is call handling specific understanding in the A-B-C emitter to
  56964. * convert call setup and call instructions into indirect ones if necessary.
  56965. */
  56966. /* Code emission flags, passed in the 'opcode' field. Opcode + flags
  56967. * fit into 16 bits for now, so use duk_small_uint_t.
  56968. */
  56969. #define DUK__EMIT_FLAG_NO_SHUFFLE_A (1 << 8)
  56970. #define DUK__EMIT_FLAG_NO_SHUFFLE_B (1 << 9)
  56971. #define DUK__EMIT_FLAG_NO_SHUFFLE_C (1 << 10)
  56972. #define DUK__EMIT_FLAG_A_IS_SOURCE (1 << 11) /* slot A is a source (default: target) */
  56973. #define DUK__EMIT_FLAG_B_IS_TARGET (1 << 12) /* slot B is a target (default: source) */
  56974. #define DUK__EMIT_FLAG_C_IS_TARGET (1 << 13) /* slot C is a target (default: source) */
  56975. #define DUK__EMIT_FLAG_BC_REGCONST (1 << 14) /* slots B and C are reg/const */
  56976. #define DUK__EMIT_FLAG_RESERVE_JUMPSLOT (1 << 15) /* reserve a jumpslot after instr before target spilling, used for NEXTENUM */
  56977. /* XXX: macro smaller than call? */
  56978. DUK_LOCAL duk_int_t duk__get_current_pc(duk_compiler_ctx *comp_ctx) {
  56979. duk_compiler_func *func;
  56980. func = &comp_ctx->curr_func;
  56981. return (duk_int_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &func->bw_code) / sizeof(duk_compiler_instr));
  56982. }
  56983. DUK_LOCAL duk_compiler_instr *duk__get_instr_ptr(duk_compiler_ctx *comp_ctx, duk_int_t pc) {
  56984. DUK_ASSERT(pc >= 0);
  56985. DUK_ASSERT((duk_size_t) pc < (duk_size_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) / sizeof(duk_compiler_instr)));
  56986. return ((duk_compiler_instr *) (void *) DUK_BW_GET_BASEPTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code)) + pc;
  56987. }
  56988. /* emit instruction; could return PC but that's not needed in the majority
  56989. * of cases.
  56990. */
  56991. DUK_LOCAL void duk__emit(duk_compiler_ctx *comp_ctx, duk_instr_t ins) {
  56992. #if defined(DUK_USE_PC2LINE)
  56993. duk_int_t line;
  56994. #endif
  56995. duk_compiler_instr *instr;
  56996. DUK_DDD(DUK_DDDPRINT("duk__emit: 0x%08lx curr_token.start_line=%ld prev_token.start_line=%ld pc=%ld --> %!I",
  56997. (unsigned long) ins,
  56998. (long) comp_ctx->curr_token.start_line,
  56999. (long) comp_ctx->prev_token.start_line,
  57000. (long) duk__get_current_pc(comp_ctx),
  57001. (duk_instr_t) ins));
  57002. instr = (duk_compiler_instr *) (void *) DUK_BW_ENSURE_GETPTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, sizeof(duk_compiler_instr));
  57003. DUK_BW_ADD_PTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, sizeof(duk_compiler_instr));
  57004. #if defined(DUK_USE_PC2LINE)
  57005. /* The line number tracking is a bit inconsistent right now, which
  57006. * affects debugger accuracy. Mostly call sites emit opcodes when
  57007. * they have parsed a token (say a terminating semicolon) and called
  57008. * duk__advance(). In this case the line number of the previous
  57009. * token is the most accurate one (except in prologue where
  57010. * prev_token.start_line is 0). This is probably not 100% correct
  57011. * right now.
  57012. */
  57013. /* approximation, close enough */
  57014. line = comp_ctx->prev_token.start_line;
  57015. if (line == 0) {
  57016. line = comp_ctx->curr_token.start_line;
  57017. }
  57018. #endif
  57019. instr->ins = ins;
  57020. #if defined(DUK_USE_PC2LINE)
  57021. instr->line = (duk_uint32_t) line;
  57022. #endif
  57023. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  57024. if (line < comp_ctx->curr_func.min_line) {
  57025. comp_ctx->curr_func.min_line = line;
  57026. }
  57027. if (line > comp_ctx->curr_func.max_line) {
  57028. comp_ctx->curr_func.max_line = line;
  57029. }
  57030. #endif
  57031. /* Limit checks for bytecode byte size and line number. */
  57032. if (DUK_UNLIKELY(DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) > DUK_USE_ESBC_MAX_BYTES)) {
  57033. goto fail_bc_limit;
  57034. }
  57035. #if defined(DUK_USE_PC2LINE) && defined(DUK_USE_ESBC_LIMITS)
  57036. #if defined(DUK_USE_BUFLEN16)
  57037. /* Buffer length is bounded to 0xffff automatically, avoid compile warning. */
  57038. if (DUK_UNLIKELY(line > DUK_USE_ESBC_MAX_LINENUMBER)) {
  57039. goto fail_bc_limit;
  57040. }
  57041. #else
  57042. if (DUK_UNLIKELY(line > DUK_USE_ESBC_MAX_LINENUMBER)) {
  57043. goto fail_bc_limit;
  57044. }
  57045. #endif
  57046. #endif
  57047. return;
  57048. fail_bc_limit:
  57049. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_BYTECODE_LIMIT);
  57050. DUK_WO_NORETURN(return;);
  57051. }
  57052. /* Update function min/max line from current token. Needed to improve
  57053. * function line range information for debugging, so that e.g. opening
  57054. * curly brace is covered by line range even when no opcodes are emitted
  57055. * for the line containing the brace.
  57056. */
  57057. DUK_LOCAL void duk__update_lineinfo_currtoken(duk_compiler_ctx *comp_ctx) {
  57058. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  57059. duk_int_t line;
  57060. line = comp_ctx->curr_token.start_line;
  57061. if (line == 0) {
  57062. return;
  57063. }
  57064. if (line < comp_ctx->curr_func.min_line) {
  57065. comp_ctx->curr_func.min_line = line;
  57066. }
  57067. if (line > comp_ctx->curr_func.max_line) {
  57068. comp_ctx->curr_func.max_line = line;
  57069. }
  57070. #else
  57071. DUK_UNREF(comp_ctx);
  57072. #endif
  57073. }
  57074. DUK_LOCAL void duk__emit_op_only(duk_compiler_ctx *comp_ctx, duk_small_uint_t op) {
  57075. duk__emit(comp_ctx, DUK_ENC_OP_ABC(op, 0));
  57076. }
  57077. /* Important main primitive. */
  57078. DUK_LOCAL void duk__emit_a_b_c(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a, duk_regconst_t b, duk_regconst_t c) {
  57079. duk_instr_t ins = 0;
  57080. duk_int_t a_out = -1;
  57081. duk_int_t b_out = -1;
  57082. duk_int_t c_out = -1;
  57083. duk_int_t tmp;
  57084. duk_small_uint_t op = op_flags & 0xffU;
  57085. DUK_DDD(DUK_DDDPRINT("emit: op_flags=%04lx, a=%ld, b=%ld, c=%ld",
  57086. (unsigned long) op_flags, (long) a, (long) b, (long) c));
  57087. /* We could rely on max temp/const checks: if they don't exceed BC
  57088. * limit, nothing here can either (just asserts would be enough).
  57089. * Currently we check for the limits, which provides additional
  57090. * protection against creating invalid bytecode due to compiler
  57091. * bugs.
  57092. */
  57093. DUK_ASSERT_DISABLE((op_flags & 0xff) >= DUK_BC_OP_MIN); /* unsigned */
  57094. DUK_ASSERT((op_flags & 0xff) <= DUK_BC_OP_MAX);
  57095. DUK_ASSERT(DUK__ISREG(a));
  57096. DUK_ASSERT(b != -1); /* Not 'none'. */
  57097. DUK_ASSERT(c != -1); /* Not 'none'. */
  57098. /* Input shuffling happens before the actual operation, while output
  57099. * shuffling happens afterwards. Output shuffling decisions are still
  57100. * made at the same time to reduce branch clutter; output shuffle decisions
  57101. * are recorded into X_out variables.
  57102. */
  57103. /* Slot A: currently no support for reg/const. */
  57104. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57105. if (a <= DUK_BC_A_MAX && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A)) {
  57106. #else
  57107. if (a <= DUK_BC_A_MAX) {
  57108. #endif
  57109. ;
  57110. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A) {
  57111. DUK_D(DUK_DPRINT("out of regs: 'a' (reg) needs shuffling but shuffle prohibited, a: %ld", (long) a));
  57112. goto error_outofregs;
  57113. } else if (a <= DUK_BC_BC_MAX) {
  57114. comp_ctx->curr_func.needs_shuffle = 1;
  57115. tmp = comp_ctx->curr_func.shuffle1;
  57116. if (op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) {
  57117. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, a));
  57118. } else {
  57119. /* Output shuffle needed after main operation */
  57120. a_out = a;
  57121. /* The DUK_OP_CSVAR output shuffle assumes shuffle registers are
  57122. * consecutive.
  57123. */
  57124. DUK_ASSERT((comp_ctx->curr_func.shuffle1 == 0 && comp_ctx->curr_func.shuffle2 == 0) ||
  57125. (comp_ctx->curr_func.shuffle2 == comp_ctx->curr_func.shuffle1 + 1));
  57126. if (op == DUK_OP_CSVAR) {
  57127. /* For CSVAR the limit is one smaller because output shuffle
  57128. * must be able to express 'a + 1' in BC.
  57129. */
  57130. if (a + 1 > DUK_BC_BC_MAX) {
  57131. goto error_outofregs;
  57132. }
  57133. }
  57134. }
  57135. a = tmp;
  57136. } else {
  57137. DUK_D(DUK_DPRINT("out of regs: 'a' (reg) needs shuffling but does not fit into BC, a: %ld", (long) a));
  57138. goto error_outofregs;
  57139. }
  57140. /* Slot B: reg/const support, mapped to bit 0 of opcode. */
  57141. if ((b & DUK__CONST_MARKER) != 0) {
  57142. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B) == 0);
  57143. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_B_IS_TARGET) == 0);
  57144. b = b & ~DUK__CONST_MARKER;
  57145. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57146. if (0) {
  57147. #else
  57148. if (b <= 0xff) {
  57149. #endif
  57150. if (op_flags & DUK__EMIT_FLAG_BC_REGCONST) {
  57151. /* Opcode follows B/C reg/const convention. */
  57152. DUK_ASSERT((op & 0x01) == 0);
  57153. ins |= DUK_ENC_OP_A_B_C(0x01, 0, 0, 0); /* const flag for B */
  57154. } else {
  57155. DUK_D(DUK_DPRINT("B is const, opcode is not B/C reg/const: %x", op_flags));
  57156. }
  57157. } else if (b <= DUK_BC_BC_MAX) {
  57158. comp_ctx->curr_func.needs_shuffle = 1;
  57159. tmp = comp_ctx->curr_func.shuffle2;
  57160. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDCONST, tmp, b));
  57161. b = tmp;
  57162. } else {
  57163. DUK_D(DUK_DPRINT("out of regs: 'b' (const) needs shuffling but does not fit into BC, b: %ld", (long) b));
  57164. goto error_outofregs;
  57165. }
  57166. } else {
  57167. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57168. if (b <= 0xff && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B)) {
  57169. #else
  57170. if (b <= 0xff) {
  57171. #endif
  57172. ;
  57173. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_B) {
  57174. if (b > DUK_BC_B_MAX) {
  57175. /* Note: 0xff != DUK_BC_B_MAX */
  57176. DUK_D(DUK_DPRINT("out of regs: 'b' (reg) needs shuffling but shuffle prohibited, b: %ld", (long) b));
  57177. goto error_outofregs;
  57178. }
  57179. } else if (b <= DUK_BC_BC_MAX) {
  57180. comp_ctx->curr_func.needs_shuffle = 1;
  57181. tmp = comp_ctx->curr_func.shuffle2;
  57182. if (op_flags & DUK__EMIT_FLAG_B_IS_TARGET) {
  57183. /* Output shuffle needed after main operation */
  57184. b_out = b;
  57185. }
  57186. if (!(op_flags & DUK__EMIT_FLAG_B_IS_TARGET)) {
  57187. if (op == DUK_OP_MPUTOBJ || op == DUK_OP_MPUTARR) {
  57188. /* Special handling for MPUTOBJ/MPUTARR shuffling.
  57189. * For each, slot B identifies the first register of a range
  57190. * of registers, so normal shuffling won't work. Instead,
  57191. * an indirect version of the opcode is used.
  57192. */
  57193. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_B_IS_TARGET) == 0);
  57194. duk__emit_load_int32_noshuffle(comp_ctx, tmp, b);
  57195. DUK_ASSERT(DUK_OP_MPUTOBJI == DUK_OP_MPUTOBJ + 1);
  57196. DUK_ASSERT(DUK_OP_MPUTARRI == DUK_OP_MPUTARR + 1);
  57197. op_flags++; /* indirect opcode follows direct */
  57198. } else {
  57199. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, b));
  57200. }
  57201. }
  57202. b = tmp;
  57203. } else {
  57204. DUK_D(DUK_DPRINT("out of regs: 'b' (reg) needs shuffling but does not fit into BC, b: %ld", (long) b));
  57205. goto error_outofregs;
  57206. }
  57207. }
  57208. /* Slot C: reg/const support, mapped to bit 1 of opcode. */
  57209. if ((c & DUK__CONST_MARKER) != 0) {
  57210. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C) == 0);
  57211. DUK_ASSERT((op_flags & DUK__EMIT_FLAG_C_IS_TARGET) == 0);
  57212. c = c & ~DUK__CONST_MARKER;
  57213. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57214. if (0) {
  57215. #else
  57216. if (c <= 0xff) {
  57217. #endif
  57218. if (op_flags & DUK__EMIT_FLAG_BC_REGCONST) {
  57219. /* Opcode follows B/C reg/const convention. */
  57220. DUK_ASSERT((op & 0x02) == 0);
  57221. ins |= DUK_ENC_OP_A_B_C(0x02, 0, 0, 0); /* const flag for C */
  57222. } else {
  57223. DUK_D(DUK_DPRINT("C is const, opcode is not B/C reg/const: %x", op_flags));
  57224. }
  57225. } else if (c <= DUK_BC_BC_MAX) {
  57226. comp_ctx->curr_func.needs_shuffle = 1;
  57227. tmp = comp_ctx->curr_func.shuffle3;
  57228. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDCONST, tmp, c));
  57229. c = tmp;
  57230. } else {
  57231. DUK_D(DUK_DPRINT("out of regs: 'c' (const) needs shuffling but does not fit into BC, c: %ld", (long) c));
  57232. goto error_outofregs;
  57233. }
  57234. } else {
  57235. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57236. if (c <= 0xff && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C)) {
  57237. #else
  57238. if (c <= 0xff) {
  57239. #endif
  57240. ;
  57241. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_C) {
  57242. if (c > DUK_BC_C_MAX) {
  57243. /* Note: 0xff != DUK_BC_C_MAX */
  57244. DUK_D(DUK_DPRINT("out of regs: 'c' (reg) needs shuffling but shuffle prohibited, c: %ld", (long) c));
  57245. goto error_outofregs;
  57246. }
  57247. } else if (c <= DUK_BC_BC_MAX) {
  57248. comp_ctx->curr_func.needs_shuffle = 1;
  57249. tmp = comp_ctx->curr_func.shuffle3;
  57250. if (op_flags & DUK__EMIT_FLAG_C_IS_TARGET) {
  57251. /* Output shuffle needed after main operation */
  57252. c_out = c;
  57253. } else {
  57254. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, c));
  57255. }
  57256. c = tmp;
  57257. } else {
  57258. DUK_D(DUK_DPRINT("out of regs: 'c' (reg) needs shuffling but does not fit into BC, c: %ld", (long) c));
  57259. goto error_outofregs;
  57260. }
  57261. }
  57262. /* Main operation */
  57263. DUK_ASSERT(a >= DUK_BC_A_MIN);
  57264. DUK_ASSERT(a <= DUK_BC_A_MAX);
  57265. DUK_ASSERT(b >= DUK_BC_B_MIN);
  57266. DUK_ASSERT(b <= DUK_BC_B_MAX);
  57267. DUK_ASSERT(c >= DUK_BC_C_MIN);
  57268. DUK_ASSERT(c <= DUK_BC_C_MAX);
  57269. ins |= DUK_ENC_OP_A_B_C(op_flags & 0xff, a, b, c);
  57270. duk__emit(comp_ctx, ins);
  57271. /* NEXTENUM needs a jump slot right after the main instruction.
  57272. * When the JUMP is taken, output spilling is not needed so this
  57273. * workaround is possible. The jump slot PC is exceptionally
  57274. * plumbed through comp_ctx to minimize call sites.
  57275. */
  57276. if (op_flags & DUK__EMIT_FLAG_RESERVE_JUMPSLOT) {
  57277. comp_ctx->emit_jumpslot_pc = duk__get_current_pc(comp_ctx);
  57278. duk__emit_abc(comp_ctx, DUK_OP_JUMP, 0);
  57279. }
  57280. /* Output shuffling: only one output register is realistically possible.
  57281. *
  57282. * (Zero would normally be an OK marker value: if the target register
  57283. * was zero, it would never be shuffled. But with DUK_USE_SHUFFLE_TORTURE
  57284. * this is no longer true, so use -1 as a marker instead.)
  57285. */
  57286. if (a_out >= 0) {
  57287. DUK_ASSERT(b_out < 0);
  57288. DUK_ASSERT(c_out < 0);
  57289. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, a, a_out));
  57290. if (op == DUK_OP_CSVAR) {
  57291. /* Special handling for CSVAR shuffling. The variable lookup
  57292. * results in a <value, this binding> pair in successive
  57293. * registers so use two shuffle registers and two output
  57294. * loads. (In practice this is dead code because temp/const
  57295. * limit is reached first.)
  57296. */
  57297. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, a + 1, a_out + 1));
  57298. }
  57299. } else if (b_out >= 0) {
  57300. DUK_ASSERT(a_out < 0);
  57301. DUK_ASSERT(c_out < 0);
  57302. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, b, b_out));
  57303. } else if (c_out >= 0) {
  57304. DUK_ASSERT(b_out < 0);
  57305. DUK_ASSERT(c_out < 0);
  57306. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, c, c_out));
  57307. }
  57308. return;
  57309. error_outofregs:
  57310. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_REG_LIMIT);
  57311. DUK_WO_NORETURN(return;);
  57312. }
  57313. /* For many of the helpers below it'd be technically correct to add
  57314. * "no shuffle" flags for parameters passed in as zero. For example,
  57315. * duk__emit_a_b() should call duk__emit_a_b_c() with C set to 0, and
  57316. * DUK__EMIT_FLAG_NO_SHUFFLE_C added to op_flags. However, since the
  57317. * C value is 0, it'll never get shuffled so adding the flag is just
  57318. * unnecessary additional code. This is unfortunately not true for
  57319. * "shuffle torture" mode which needs special handling.
  57320. */
  57321. DUK_LOCAL void duk__emit_a_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a, duk_regconst_t b) {
  57322. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57323. op_flags |= DUK__EMIT_FLAG_NO_SHUFFLE_C;
  57324. #endif
  57325. duk__emit_a_b_c(comp_ctx, op_flags, a, b, 0);
  57326. }
  57327. DUK_LOCAL void duk__emit_b_c(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t b, duk_regconst_t c) {
  57328. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57329. op_flags |= DUK__EMIT_FLAG_NO_SHUFFLE_A;
  57330. #endif
  57331. duk__emit_a_b_c(comp_ctx, op_flags, 0, b, c);
  57332. }
  57333. #if 0 /* unused */
  57334. DUK_LOCAL void duk__emit_a(duk_compiler_ctx *comp_ctx, int op_flags, int a) {
  57335. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57336. op_flags |= DUK__EMIT_FLAG_NO_SHUFFLE_B | DUK__EMIT_FLAG_NO_SHUFFLE_C;
  57337. #endif
  57338. duk__emit_a_b_c(comp_ctx, op_flags, a, 0, 0);
  57339. }
  57340. #endif
  57341. #if 0 /* unused */
  57342. DUK_LOCAL void duk__emit_b(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t b) {
  57343. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57344. op_flags |= DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_NO_SHUFFLE_C;
  57345. #endif
  57346. duk__emit_a_b_c(comp_ctx, op_flags, 0, b, 0);
  57347. }
  57348. #endif
  57349. DUK_LOCAL void duk__emit_a_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op_flags, duk_regconst_t a, duk_regconst_t bc) {
  57350. duk_instr_t ins;
  57351. duk_int_t tmp;
  57352. /* allow caller to give a const number with the DUK__CONST_MARKER */
  57353. DUK_ASSERT(bc != -1); /* Not 'none'. */
  57354. bc = bc & (~DUK__CONST_MARKER);
  57355. DUK_ASSERT_DISABLE((op_flags & 0xff) >= DUK_BC_OP_MIN); /* unsigned */
  57356. DUK_ASSERT((op_flags & 0xff) <= DUK_BC_OP_MAX);
  57357. DUK_ASSERT(bc >= DUK_BC_BC_MIN);
  57358. DUK_ASSERT(bc <= DUK_BC_BC_MAX);
  57359. DUK_ASSERT((bc & DUK__CONST_MARKER) == 0);
  57360. if (bc <= DUK_BC_BC_MAX) {
  57361. ;
  57362. } else {
  57363. /* No BC shuffling now. */
  57364. goto error_outofregs;
  57365. }
  57366. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57367. if (a <= DUK_BC_A_MAX && (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A)) {
  57368. #else
  57369. if (a <= DUK_BC_A_MAX) {
  57370. #endif
  57371. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, a, bc);
  57372. duk__emit(comp_ctx, ins);
  57373. } else if (op_flags & DUK__EMIT_FLAG_NO_SHUFFLE_A) {
  57374. goto error_outofregs;
  57375. } else if ((op_flags & 0xf0U) == DUK_OP_CALL0) {
  57376. comp_ctx->curr_func.needs_shuffle = 1;
  57377. tmp = comp_ctx->curr_func.shuffle1;
  57378. duk__emit_load_int32_noshuffle(comp_ctx, tmp, a);
  57379. op_flags |= DUK_BC_CALL_FLAG_INDIRECT;
  57380. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, tmp, bc);
  57381. duk__emit(comp_ctx, ins);
  57382. } else if (a <= DUK_BC_BC_MAX) {
  57383. comp_ctx->curr_func.needs_shuffle = 1;
  57384. tmp = comp_ctx->curr_func.shuffle1;
  57385. ins = DUK_ENC_OP_A_BC(op_flags & 0xff, tmp, bc);
  57386. if (op_flags & DUK__EMIT_FLAG_A_IS_SOURCE) {
  57387. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_LDREG, tmp, a));
  57388. duk__emit(comp_ctx, ins);
  57389. } else {
  57390. duk__emit(comp_ctx, ins);
  57391. duk__emit(comp_ctx, DUK_ENC_OP_A_BC(DUK_OP_STREG, tmp, a));
  57392. }
  57393. } else {
  57394. goto error_outofregs;
  57395. }
  57396. return;
  57397. error_outofregs:
  57398. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_REG_LIMIT);
  57399. DUK_WO_NORETURN(return;);
  57400. }
  57401. DUK_LOCAL void duk__emit_bc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t bc) {
  57402. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57403. op |= DUK__EMIT_FLAG_NO_SHUFFLE_A;
  57404. #endif
  57405. duk__emit_a_bc(comp_ctx, op, 0, bc);
  57406. }
  57407. DUK_LOCAL void duk__emit_abc(duk_compiler_ctx *comp_ctx, duk_small_uint_t op, duk_regconst_t abc) {
  57408. duk_instr_t ins;
  57409. DUK_ASSERT_DISABLE(op >= DUK_BC_OP_MIN); /* unsigned */
  57410. DUK_ASSERT(op <= DUK_BC_OP_MAX);
  57411. DUK_ASSERT_DISABLE(abc >= DUK_BC_ABC_MIN); /* unsigned */
  57412. DUK_ASSERT(abc <= DUK_BC_ABC_MAX);
  57413. DUK_ASSERT((abc & DUK__CONST_MARKER) == 0);
  57414. DUK_ASSERT(abc != -1); /* Not 'none'. */
  57415. if (abc <= DUK_BC_ABC_MAX) {
  57416. ;
  57417. } else {
  57418. goto error_outofregs;
  57419. }
  57420. ins = DUK_ENC_OP_ABC(op, abc);
  57421. DUK_DDD(DUK_DDDPRINT("duk__emit_abc: 0x%08lx line=%ld pc=%ld op=%ld (%!C) abc=%ld (%!I)",
  57422. (unsigned long) ins, (long) comp_ctx->curr_token.start_line,
  57423. (long) duk__get_current_pc(comp_ctx), (long) op, (long) op,
  57424. (long) abc, (duk_instr_t) ins));
  57425. duk__emit(comp_ctx, ins);
  57426. return;
  57427. error_outofregs:
  57428. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_REG_LIMIT);
  57429. DUK_WO_NORETURN(return;);
  57430. }
  57431. DUK_LOCAL void duk__emit_load_int32_raw(duk_compiler_ctx *comp_ctx, duk_regconst_t reg, duk_int32_t val, duk_small_uint_t op_flags) {
  57432. /* XXX: Shuffling support could be implemented here so that LDINT+LDINTX
  57433. * would only shuffle once (instead of twice). The current code works
  57434. * though, and has a smaller compiler footprint.
  57435. */
  57436. if ((val >= (duk_int32_t) DUK_BC_BC_MIN - (duk_int32_t) DUK_BC_LDINT_BIAS) &&
  57437. (val <= (duk_int32_t) DUK_BC_BC_MAX - (duk_int32_t) DUK_BC_LDINT_BIAS)) {
  57438. DUK_DDD(DUK_DDDPRINT("emit LDINT to reg %ld for %ld", (long) reg, (long) val));
  57439. duk__emit_a_bc(comp_ctx, DUK_OP_LDINT | op_flags, reg, (duk_regconst_t) (val + (duk_int32_t) DUK_BC_LDINT_BIAS));
  57440. } else {
  57441. duk_int32_t hi = val >> DUK_BC_LDINTX_SHIFT;
  57442. duk_int32_t lo = val & ((((duk_int32_t) 1) << DUK_BC_LDINTX_SHIFT) - 1);
  57443. DUK_ASSERT(lo >= 0);
  57444. DUK_DDD(DUK_DDDPRINT("emit LDINT+LDINTX to reg %ld for %ld -> hi %ld, lo %ld",
  57445. (long) reg, (long) val, (long) hi, (long) lo));
  57446. duk__emit_a_bc(comp_ctx, DUK_OP_LDINT | op_flags, reg, (duk_regconst_t) (hi + (duk_int32_t) DUK_BC_LDINT_BIAS));
  57447. duk__emit_a_bc(comp_ctx, DUK_OP_LDINTX | op_flags, reg, (duk_regconst_t) lo);
  57448. }
  57449. }
  57450. DUK_LOCAL void duk__emit_load_int32(duk_compiler_ctx *comp_ctx, duk_regconst_t reg, duk_int32_t val) {
  57451. duk__emit_load_int32_raw(comp_ctx, reg, val, 0 /*op_flags*/);
  57452. }
  57453. #if defined(DUK_USE_SHUFFLE_TORTURE)
  57454. /* Used by duk__emit*() calls so that we don't shuffle the loadints that
  57455. * are needed to handle indirect opcodes.
  57456. */
  57457. DUK_LOCAL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_regconst_t reg, duk_int32_t val) {
  57458. duk__emit_load_int32_raw(comp_ctx, reg, val, DUK__EMIT_FLAG_NO_SHUFFLE_A /*op_flags*/);
  57459. }
  57460. #else
  57461. DUK_LOCAL void duk__emit_load_int32_noshuffle(duk_compiler_ctx *comp_ctx, duk_regconst_t reg, duk_int32_t val) {
  57462. /* When torture not enabled, can just use the same helper because
  57463. * 'reg' won't get spilled.
  57464. */
  57465. DUK_ASSERT(reg <= DUK_BC_A_MAX);
  57466. duk__emit_load_int32(comp_ctx, reg, val);
  57467. }
  57468. #endif
  57469. DUK_LOCAL void duk__emit_jump(duk_compiler_ctx *comp_ctx, duk_int_t target_pc) {
  57470. duk_int_t curr_pc;
  57471. duk_int_t offset;
  57472. curr_pc = (duk_int_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) / sizeof(duk_compiler_instr));
  57473. offset = (duk_int_t) target_pc - (duk_int_t) curr_pc - 1;
  57474. DUK_ASSERT(offset + DUK_BC_JUMP_BIAS >= DUK_BC_ABC_MIN);
  57475. DUK_ASSERT(offset + DUK_BC_JUMP_BIAS <= DUK_BC_ABC_MAX);
  57476. duk__emit_abc(comp_ctx, DUK_OP_JUMP, (duk_regconst_t) (offset + DUK_BC_JUMP_BIAS));
  57477. }
  57478. DUK_LOCAL duk_int_t duk__emit_jump_empty(duk_compiler_ctx *comp_ctx) {
  57479. duk_int_t ret;
  57480. ret = duk__get_current_pc(comp_ctx); /* useful for patching jumps later */
  57481. duk__emit_op_only(comp_ctx, DUK_OP_JUMP);
  57482. return ret;
  57483. }
  57484. /* Insert an empty jump in the middle of code emitted earlier. This is
  57485. * currently needed for compiling for-in.
  57486. */
  57487. DUK_LOCAL void duk__insert_jump_entry(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc) {
  57488. #if defined(DUK_USE_PC2LINE)
  57489. duk_int_t line;
  57490. #endif
  57491. duk_compiler_instr *instr;
  57492. duk_size_t offset;
  57493. DUK_ASSERT(jump_pc >= 0);
  57494. offset = (duk_size_t) jump_pc * sizeof(duk_compiler_instr);
  57495. instr = (duk_compiler_instr *) (void *)
  57496. DUK_BW_INSERT_ENSURE_AREA(comp_ctx->thr,
  57497. &comp_ctx->curr_func.bw_code,
  57498. offset,
  57499. sizeof(duk_compiler_instr));
  57500. #if defined(DUK_USE_PC2LINE)
  57501. line = comp_ctx->curr_token.start_line; /* approximation, close enough */
  57502. #endif
  57503. instr->ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, 0);
  57504. #if defined(DUK_USE_PC2LINE)
  57505. instr->line = (duk_uint32_t) line;
  57506. #endif
  57507. DUK_BW_ADD_PTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, sizeof(duk_compiler_instr));
  57508. if (DUK_UNLIKELY(DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) > DUK_USE_ESBC_MAX_BYTES)) {
  57509. goto fail_bc_limit;
  57510. }
  57511. return;
  57512. fail_bc_limit:
  57513. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_BYTECODE_LIMIT);
  57514. DUK_WO_NORETURN(return;);
  57515. }
  57516. /* Does not assume that jump_pc contains a DUK_OP_JUMP previously; this is intentional
  57517. * to allow e.g. an INVALID opcode be overwritten with a JUMP (label management uses this).
  57518. */
  57519. DUK_LOCAL void duk__patch_jump(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc, duk_int_t target_pc) {
  57520. duk_compiler_instr *instr;
  57521. duk_int_t offset;
  57522. /* allow negative PCs, behave as a no-op */
  57523. if (jump_pc < 0) {
  57524. DUK_DDD(DUK_DDDPRINT("duk__patch_jump(): nop call, jump_pc=%ld (<0), target_pc=%ld",
  57525. (long) jump_pc, (long) target_pc));
  57526. return;
  57527. }
  57528. DUK_ASSERT(jump_pc >= 0);
  57529. /* XXX: range assert */
  57530. instr = duk__get_instr_ptr(comp_ctx, jump_pc);
  57531. DUK_ASSERT(instr != NULL);
  57532. /* XXX: range assert */
  57533. offset = target_pc - jump_pc - 1;
  57534. instr->ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, offset + DUK_BC_JUMP_BIAS);
  57535. DUK_DDD(DUK_DDDPRINT("duk__patch_jump(): jump_pc=%ld, target_pc=%ld, offset=%ld",
  57536. (long) jump_pc, (long) target_pc, (long) offset));
  57537. }
  57538. DUK_LOCAL void duk__patch_jump_here(duk_compiler_ctx *comp_ctx, duk_int_t jump_pc) {
  57539. duk__patch_jump(comp_ctx, jump_pc, duk__get_current_pc(comp_ctx));
  57540. }
  57541. DUK_LOCAL void duk__patch_trycatch(duk_compiler_ctx *comp_ctx, duk_int_t ldconst_pc, duk_int_t trycatch_pc, duk_regconst_t reg_catch, duk_regconst_t const_varname, duk_small_uint_t flags) {
  57542. duk_compiler_instr *instr;
  57543. DUK_ASSERT(DUK__ISREG(reg_catch));
  57544. instr = duk__get_instr_ptr(comp_ctx, ldconst_pc);
  57545. DUK_ASSERT(DUK_DEC_OP(instr->ins) == DUK_OP_LDCONST);
  57546. DUK_ASSERT(instr != NULL);
  57547. if (const_varname & DUK__CONST_MARKER) {
  57548. /* Have a catch variable. */
  57549. const_varname = const_varname & (~DUK__CONST_MARKER);
  57550. if (reg_catch > DUK_BC_BC_MAX || const_varname > DUK_BC_BC_MAX) {
  57551. /* Catch attempts to use out-of-range reg/const. Without this
  57552. * check Duktape 0.12.0 could generate invalid code which caused
  57553. * an assert failure on execution. This error is triggered e.g.
  57554. * for functions with a lot of constants and a try-catch statement.
  57555. * Shuffling or opcode semantics change is needed to fix the issue.
  57556. * See: test-bug-trycatch-many-constants.js.
  57557. */
  57558. DUK_D(DUK_DPRINT("failed to patch trycatch: flags=%ld, reg_catch=%ld, const_varname=%ld (0x%08lx)",
  57559. (long) flags, (long) reg_catch, (long) const_varname, (long) const_varname));
  57560. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_REG_LIMIT);
  57561. DUK_WO_NORETURN(return;);
  57562. }
  57563. instr->ins |= DUK_ENC_OP_A_BC(0, 0, const_varname);
  57564. } else {
  57565. /* No catch variable, e.g. a try-finally; replace LDCONST with
  57566. * NOP to avoid a bogus LDCONST.
  57567. */
  57568. instr->ins = DUK_ENC_OP(DUK_OP_NOP);
  57569. }
  57570. instr = duk__get_instr_ptr(comp_ctx, trycatch_pc);
  57571. DUK_ASSERT(instr != NULL);
  57572. DUK_ASSERT_DISABLE(flags >= DUK_BC_A_MIN);
  57573. DUK_ASSERT(flags <= DUK_BC_A_MAX);
  57574. instr->ins = DUK_ENC_OP_A_BC(DUK_OP_TRYCATCH, flags, reg_catch);
  57575. }
  57576. DUK_LOCAL void duk__emit_if_false_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst) {
  57577. duk_small_uint_t op;
  57578. op = DUK__ISREG(regconst) ? DUK_OP_IFFALSE_R : DUK_OP_IFFALSE_C;
  57579. duk__emit_bc(comp_ctx, op, regconst); /* helper will remove const flag */
  57580. }
  57581. DUK_LOCAL void duk__emit_if_true_skip(duk_compiler_ctx *comp_ctx, duk_regconst_t regconst) {
  57582. duk_small_uint_t op;
  57583. op = DUK__ISREG(regconst) ? DUK_OP_IFTRUE_R : DUK_OP_IFTRUE_C;
  57584. duk__emit_bc(comp_ctx, op, regconst); /* helper will remove const flag */
  57585. }
  57586. DUK_LOCAL void duk__emit_invalid(duk_compiler_ctx *comp_ctx) {
  57587. duk__emit_op_only(comp_ctx, DUK_OP_INVALID);
  57588. }
  57589. /*
  57590. * Peephole optimizer for finished bytecode.
  57591. *
  57592. * Does not remove opcodes; currently only straightens out unconditional
  57593. * jump chains which are generated by several control structures.
  57594. */
  57595. DUK_LOCAL void duk__peephole_optimize_bytecode(duk_compiler_ctx *comp_ctx) {
  57596. duk_compiler_instr *bc;
  57597. duk_small_uint_t iter;
  57598. duk_int_t i, n;
  57599. duk_int_t count_opt;
  57600. bc = (duk_compiler_instr *) (void *) DUK_BW_GET_BASEPTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code);
  57601. #if defined(DUK_USE_BUFLEN16)
  57602. /* No need to assert, buffer size maximum is 0xffff. */
  57603. #else
  57604. DUK_ASSERT((duk_size_t) DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) / sizeof(duk_compiler_instr) <= (duk_size_t) DUK_INT_MAX); /* bytecode limits */
  57605. #endif
  57606. n = (duk_int_t) (DUK_BW_GET_SIZE(comp_ctx->thr, &comp_ctx->curr_func.bw_code) / sizeof(duk_compiler_instr));
  57607. for (iter = 0; iter < DUK_COMPILER_PEEPHOLE_MAXITER; iter++) {
  57608. count_opt = 0;
  57609. for (i = 0; i < n; i++) {
  57610. duk_instr_t ins;
  57611. duk_int_t target_pc1;
  57612. duk_int_t target_pc2;
  57613. ins = bc[i].ins;
  57614. if (DUK_DEC_OP(ins) != DUK_OP_JUMP) {
  57615. continue;
  57616. }
  57617. target_pc1 = i + 1 + (duk_int_t) DUK_DEC_ABC(ins) - (duk_int_t) DUK_BC_JUMP_BIAS;
  57618. DUK_DDD(DUK_DDDPRINT("consider jump at pc %ld; target_pc=%ld", (long) i, (long) target_pc1));
  57619. DUK_ASSERT(target_pc1 >= 0);
  57620. DUK_ASSERT(target_pc1 < n);
  57621. /* Note: if target_pc1 == i, we'll optimize a jump to itself.
  57622. * This does not need to be checked for explicitly; the case
  57623. * is rare and max iter breaks us out.
  57624. */
  57625. ins = bc[target_pc1].ins;
  57626. if (DUK_DEC_OP(ins) != DUK_OP_JUMP) {
  57627. continue;
  57628. }
  57629. target_pc2 = target_pc1 + 1 + (duk_int_t) DUK_DEC_ABC(ins) - (duk_int_t) DUK_BC_JUMP_BIAS;
  57630. DUK_DDD(DUK_DDDPRINT("optimizing jump at pc %ld; old target is %ld -> new target is %ld",
  57631. (long) i, (long) target_pc1, (long) target_pc2));
  57632. bc[i].ins = DUK_ENC_OP_ABC(DUK_OP_JUMP, target_pc2 - (i + 1) + DUK_BC_JUMP_BIAS);
  57633. count_opt++;
  57634. }
  57635. DUK_DD(DUK_DDPRINT("optimized %ld jumps on peephole round %ld", (long) count_opt, (long) (iter + 1)));
  57636. if (count_opt == 0) {
  57637. break;
  57638. }
  57639. }
  57640. }
  57641. /*
  57642. * Intermediate value helpers
  57643. */
  57644. /* Flags for intermediate value coercions. A flag for using a forced reg
  57645. * is not needed, the forced_reg argument suffices and generates better
  57646. * code (it is checked as it is used).
  57647. */
  57648. /* XXX: DUK__IVAL_FLAG_REQUIRE_SHORT is passed but not currently implemented
  57649. * by ispec/ivalue operations.
  57650. */
  57651. #define DUK__IVAL_FLAG_ALLOW_CONST (1 << 0) /* allow a constant to be returned */
  57652. #define DUK__IVAL_FLAG_REQUIRE_TEMP (1 << 1) /* require a (mutable) temporary as a result (or a const if allowed) */
  57653. #define DUK__IVAL_FLAG_REQUIRE_SHORT (1 << 2) /* require a short (8-bit) reg/const which fits into bytecode B/C slot */
  57654. /* XXX: some code might benefit from DUK__SETTEMP_IFTEMP(thr,x) */
  57655. #if 0 /* enable manually for dumping */
  57656. #define DUK__DUMP_ISPEC(compctx,ispec) do { duk__dump_ispec((compctx), (ispec)); } while (0)
  57657. #define DUK__DUMP_IVALUE(compctx,ivalue) do { duk__dump_ivalue((compctx), (ivalue)); } while (0)
  57658. DUK_LOCAL void duk__dump_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *x) {
  57659. DUK_D(DUK_DPRINT("ispec dump: t=%ld regconst=0x%08lx, valstack_idx=%ld, value=%!T",
  57660. (long) x->t, (unsigned long) x->regconst, (long) x->valstack_idx,
  57661. duk_get_tval(comp_ctx->thr, x->valstack_idx)));
  57662. }
  57663. DUK_LOCAL void duk__dump_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  57664. DUK_D(DUK_DPRINT("ivalue dump: t=%ld op=%ld "
  57665. "x1={t=%ld regconst=0x%08lx valstack_idx=%ld value=%!T} "
  57666. "x2={t=%ld regconst=0x%08lx valstack_idx=%ld value=%!T}",
  57667. (long) x->t, (long) x->op,
  57668. (long) x->x1.t, (unsigned long) x->x1.regconst, (long) x->x1.valstack_idx,
  57669. duk_get_tval(comp_ctx->thr, x->x1.valstack_idx),
  57670. (long) x->x2.t, (unsigned long) x->x2.regconst, (long) x->x2.valstack_idx,
  57671. duk_get_tval(comp_ctx->thr, x->x2.valstack_idx)));
  57672. }
  57673. #else
  57674. #define DUK__DUMP_ISPEC(comp_ctx,x) do {} while (0)
  57675. #define DUK__DUMP_IVALUE(comp_ctx,x) do {} while (0)
  57676. #endif
  57677. DUK_LOCAL void duk__ivalue_regconst(duk_ivalue *x, duk_regconst_t regconst) {
  57678. x->t = DUK_IVAL_PLAIN;
  57679. x->x1.t = DUK_ISPEC_REGCONST;
  57680. x->x1.regconst = regconst;
  57681. }
  57682. DUK_LOCAL void duk__ivalue_plain_fromstack(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  57683. x->t = DUK_IVAL_PLAIN;
  57684. x->x1.t = DUK_ISPEC_VALUE;
  57685. duk_replace(comp_ctx->thr, x->x1.valstack_idx);
  57686. }
  57687. DUK_LOCAL void duk__ivalue_var_fromstack(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  57688. x->t = DUK_IVAL_VAR;
  57689. x->x1.t = DUK_ISPEC_VALUE;
  57690. duk_replace(comp_ctx->thr, x->x1.valstack_idx);
  57691. }
  57692. DUK_LOCAL_DECL void duk__ivalue_var_hstring(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_hstring *h) {
  57693. DUK_ASSERT(h != NULL);
  57694. duk_push_hstring(comp_ctx->thr, h);
  57695. duk__ivalue_var_fromstack(comp_ctx, x);
  57696. }
  57697. DUK_LOCAL void duk__copy_ispec(duk_compiler_ctx *comp_ctx, duk_ispec *src, duk_ispec *dst) {
  57698. dst->t = src->t;
  57699. dst->regconst = src->regconst;
  57700. duk_copy(comp_ctx->thr, src->valstack_idx, dst->valstack_idx);
  57701. }
  57702. DUK_LOCAL void duk__copy_ivalue(duk_compiler_ctx *comp_ctx, duk_ivalue *src, duk_ivalue *dst) {
  57703. dst->t = src->t;
  57704. dst->op = src->op;
  57705. dst->x1.t = src->x1.t;
  57706. dst->x1.regconst = src->x1.regconst;
  57707. dst->x2.t = src->x2.t;
  57708. dst->x2.regconst = src->x2.regconst;
  57709. duk_copy(comp_ctx->thr, src->x1.valstack_idx, dst->x1.valstack_idx);
  57710. duk_copy(comp_ctx->thr, src->x2.valstack_idx, dst->x2.valstack_idx);
  57711. }
  57712. DUK_LOCAL duk_regconst_t duk__alloctemps(duk_compiler_ctx *comp_ctx, duk_small_int_t num) {
  57713. duk_regconst_t res;
  57714. res = comp_ctx->curr_func.temp_next;
  57715. comp_ctx->curr_func.temp_next += num;
  57716. if (comp_ctx->curr_func.temp_next > DUK__MAX_TEMPS) { /* == DUK__MAX_TEMPS is OK */
  57717. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_TEMP_LIMIT);
  57718. DUK_WO_NORETURN(return 0;);
  57719. }
  57720. /* maintain highest 'used' temporary, needed to figure out nregs of function */
  57721. if (comp_ctx->curr_func.temp_next > comp_ctx->curr_func.temp_max) {
  57722. comp_ctx->curr_func.temp_max = comp_ctx->curr_func.temp_next;
  57723. }
  57724. return res;
  57725. }
  57726. DUK_LOCAL duk_regconst_t duk__alloctemp(duk_compiler_ctx *comp_ctx) {
  57727. return duk__alloctemps(comp_ctx, 1);
  57728. }
  57729. DUK_LOCAL void duk__settemp_checkmax(duk_compiler_ctx *comp_ctx, duk_regconst_t temp_next) {
  57730. comp_ctx->curr_func.temp_next = temp_next;
  57731. if (temp_next > comp_ctx->curr_func.temp_max) {
  57732. comp_ctx->curr_func.temp_max = temp_next;
  57733. }
  57734. }
  57735. /* get const for value at valstack top */
  57736. DUK_LOCAL duk_regconst_t duk__getconst(duk_compiler_ctx *comp_ctx) {
  57737. duk_hthread *thr = comp_ctx->thr;
  57738. duk_compiler_func *f = &comp_ctx->curr_func;
  57739. duk_tval *tv1;
  57740. duk_int_t i, n, n_check;
  57741. n = (duk_int_t) duk_get_length(thr, f->consts_idx);
  57742. tv1 = DUK_GET_TVAL_NEGIDX(thr, -1);
  57743. DUK_ASSERT(tv1 != NULL);
  57744. #if defined(DUK_USE_FASTINT)
  57745. /* Explicit check for fastint downgrade. */
  57746. DUK_TVAL_CHKFAST_INPLACE_SLOW(tv1);
  57747. #endif
  57748. /* Sanity workaround for handling functions with a large number of
  57749. * constants at least somewhat reasonably. Otherwise checking whether
  57750. * we already have the constant would grow very slow (as it is O(N^2)).
  57751. */
  57752. n_check = (n > DUK__GETCONST_MAX_CONSTS_CHECK ? DUK__GETCONST_MAX_CONSTS_CHECK : n);
  57753. for (i = 0; i < n_check; i++) {
  57754. duk_tval *tv2 = DUK_HOBJECT_A_GET_VALUE_PTR(thr->heap, f->h_consts, i);
  57755. /* Strict equality is NOT enough, because we cannot use the same
  57756. * constant for e.g. +0 and -0.
  57757. */
  57758. if (duk_js_samevalue(tv1, tv2)) {
  57759. DUK_DDD(DUK_DDDPRINT("reused existing constant for %!T -> const index %ld",
  57760. (duk_tval *) tv1, (long) i));
  57761. duk_pop(thr);
  57762. return (duk_regconst_t) i | (duk_regconst_t) DUK__CONST_MARKER;
  57763. }
  57764. }
  57765. if (n > DUK__MAX_CONSTS) {
  57766. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_CONST_LIMIT);
  57767. DUK_WO_NORETURN(return 0;);
  57768. }
  57769. DUK_DDD(DUK_DDDPRINT("allocating new constant for %!T -> const index %ld",
  57770. (duk_tval *) tv1, (long) n));
  57771. (void) duk_put_prop_index(thr, f->consts_idx, (duk_uarridx_t) n); /* invalidates tv1, tv2 */
  57772. return (duk_regconst_t) n | (duk_regconst_t) DUK__CONST_MARKER;
  57773. }
  57774. DUK_LOCAL duk_bool_t duk__const_needs_refcount(duk_compiler_ctx *comp_ctx, duk_regconst_t rc) {
  57775. #if defined(DUK_USE_REFERENCE_COUNTING)
  57776. duk_compiler_func *f = &comp_ctx->curr_func;
  57777. duk_bool_t ret;
  57778. DUK_ASSERT((rc & DUK__CONST_MARKER) == 0); /* caller removes const marker */
  57779. (void) duk_get_prop_index(comp_ctx->thr, f->consts_idx, (duk_uarridx_t) rc);
  57780. ret = !duk_is_number(comp_ctx->thr, -1); /* now only number/string, so conservative check */
  57781. duk_pop(comp_ctx->thr);
  57782. return ret;
  57783. #else
  57784. DUK_UNREF(comp_ctx);
  57785. DUK_UNREF(rc);
  57786. DUK_ASSERT((rc & DUK__CONST_MARKER) == 0); /* caller removes const marker */
  57787. return 0;
  57788. #endif
  57789. }
  57790. /* Get the value represented by an duk_ispec to a register or constant.
  57791. * The caller can control the result by indicating whether or not:
  57792. *
  57793. * (1) a constant is allowed (sometimes the caller needs the result to
  57794. * be in a register)
  57795. *
  57796. * (2) a temporary register is required (usually when caller requires
  57797. * the register to be safely mutable; normally either a bound
  57798. * register or a temporary register are both OK)
  57799. *
  57800. * (3) a forced register target needs to be used
  57801. *
  57802. * Bytecode may be emitted to generate the necessary value. The return
  57803. * value is either a register or a constant.
  57804. */
  57805. DUK_LOCAL
  57806. duk_regconst_t duk__ispec_toregconst_raw(duk_compiler_ctx *comp_ctx,
  57807. duk_ispec *x,
  57808. duk_regconst_t forced_reg,
  57809. duk_small_uint_t flags) {
  57810. duk_hthread *thr = comp_ctx->thr;
  57811. DUK_DDD(DUK_DDDPRINT("duk__ispec_toregconst_raw(): x={%ld:%ld:%!T}, "
  57812. "forced_reg=%ld, flags 0x%08lx: allow_const=%ld require_temp=%ld require_short=%ld",
  57813. (long) x->t,
  57814. (long) x->regconst,
  57815. (duk_tval *) duk_get_tval(thr, x->valstack_idx),
  57816. (long) forced_reg,
  57817. (unsigned long) flags,
  57818. (long) ((flags & DUK__IVAL_FLAG_ALLOW_CONST) ? 1 : 0),
  57819. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) ? 1 : 0),
  57820. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_SHORT) ? 1 : 0)));
  57821. switch (x->t) {
  57822. case DUK_ISPEC_VALUE: {
  57823. duk_tval *tv;
  57824. tv = DUK_GET_TVAL_POSIDX(thr, x->valstack_idx);
  57825. DUK_ASSERT(tv != NULL);
  57826. switch (DUK_TVAL_GET_TAG(tv)) {
  57827. case DUK_TAG_UNDEFINED: {
  57828. /* Note: although there is no 'undefined' literal, undefined
  57829. * values can occur during compilation as a result of e.g.
  57830. * the 'void' operator.
  57831. */
  57832. duk_regconst_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  57833. duk__emit_bc(comp_ctx, DUK_OP_LDUNDEF, dest);
  57834. return dest;
  57835. }
  57836. case DUK_TAG_NULL: {
  57837. duk_regconst_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  57838. duk__emit_bc(comp_ctx, DUK_OP_LDNULL, dest);
  57839. return dest;
  57840. }
  57841. case DUK_TAG_BOOLEAN: {
  57842. duk_regconst_t dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  57843. duk__emit_bc(comp_ctx,
  57844. (DUK_TVAL_GET_BOOLEAN(tv) ? DUK_OP_LDTRUE : DUK_OP_LDFALSE),
  57845. dest);
  57846. return dest;
  57847. }
  57848. case DUK_TAG_POINTER: {
  57849. DUK_UNREACHABLE();
  57850. break;
  57851. }
  57852. case DUK_TAG_STRING: {
  57853. duk_hstring *h;
  57854. duk_regconst_t dest;
  57855. duk_regconst_t constidx;
  57856. h = DUK_TVAL_GET_STRING(tv);
  57857. DUK_UNREF(h);
  57858. DUK_ASSERT(h != NULL);
  57859. #if 0 /* XXX: to be implemented? */
  57860. /* Use special opcodes to load short strings */
  57861. if (DUK_HSTRING_GET_BYTELEN(h) <= 2) {
  57862. /* Encode into a single opcode (18 bits can encode 1-2 bytes + length indicator) */
  57863. } else if (DUK_HSTRING_GET_BYTELEN(h) <= 6) {
  57864. /* Encode into a double constant (53 bits can encode 6*8 = 48 bits + 3-bit length */
  57865. }
  57866. #endif
  57867. duk_dup(thr, x->valstack_idx);
  57868. constidx = duk__getconst(comp_ctx);
  57869. if (flags & DUK__IVAL_FLAG_ALLOW_CONST) {
  57870. return constidx;
  57871. }
  57872. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  57873. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, dest, constidx);
  57874. return dest;
  57875. }
  57876. case DUK_TAG_OBJECT: {
  57877. DUK_UNREACHABLE();
  57878. break;
  57879. }
  57880. case DUK_TAG_BUFFER: {
  57881. DUK_UNREACHABLE();
  57882. break;
  57883. }
  57884. case DUK_TAG_LIGHTFUNC: {
  57885. DUK_UNREACHABLE();
  57886. break;
  57887. }
  57888. #if defined(DUK_USE_FASTINT)
  57889. case DUK_TAG_FASTINT:
  57890. #endif
  57891. default: {
  57892. /* number */
  57893. duk_regconst_t dest;
  57894. duk_regconst_t constidx;
  57895. duk_double_t dval;
  57896. duk_int32_t ival;
  57897. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  57898. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  57899. dval = DUK_TVAL_GET_NUMBER(tv);
  57900. if (!(flags & DUK__IVAL_FLAG_ALLOW_CONST)) {
  57901. /* A number can be loaded either through a constant, using
  57902. * LDINT, or using LDINT+LDINTX. LDINT is always a size win,
  57903. * LDINT+LDINTX is not if the constant is used multiple times.
  57904. * Currently always prefer LDINT+LDINTX over a double constant.
  57905. */
  57906. if (duk_is_whole_get_int32_nonegzero(dval, &ival)) {
  57907. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  57908. duk__emit_load_int32(comp_ctx, dest, ival);
  57909. return dest;
  57910. }
  57911. }
  57912. duk_dup(thr, x->valstack_idx);
  57913. constidx = duk__getconst(comp_ctx);
  57914. if (flags & DUK__IVAL_FLAG_ALLOW_CONST) {
  57915. return constidx;
  57916. } else {
  57917. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  57918. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, dest, constidx);
  57919. return dest;
  57920. }
  57921. }
  57922. } /* end switch */
  57923. goto fail_internal; /* never here */
  57924. }
  57925. case DUK_ISPEC_REGCONST: {
  57926. if (forced_reg >= 0) {
  57927. if (DUK__ISCONST(x->regconst)) {
  57928. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, forced_reg, x->regconst);
  57929. } else if (x->regconst != forced_reg) {
  57930. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, forced_reg, x->regconst);
  57931. } else {
  57932. ; /* already in correct reg */
  57933. }
  57934. return forced_reg;
  57935. }
  57936. DUK_ASSERT(forced_reg < 0);
  57937. if (DUK__ISCONST(x->regconst)) {
  57938. if (!(flags & DUK__IVAL_FLAG_ALLOW_CONST)) {
  57939. duk_regconst_t dest = DUK__ALLOCTEMP(comp_ctx);
  57940. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, dest, x->regconst);
  57941. return dest;
  57942. }
  57943. return x->regconst;
  57944. }
  57945. DUK_ASSERT(forced_reg < 0 && !DUK__ISCONST(x->regconst));
  57946. if ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) && !DUK__ISREG_TEMP(comp_ctx, x->regconst)) {
  57947. duk_regconst_t dest = DUK__ALLOCTEMP(comp_ctx);
  57948. duk__emit_a_bc(comp_ctx, DUK_OP_LDREG, dest, x->regconst);
  57949. return dest;
  57950. }
  57951. return x->regconst;
  57952. }
  57953. default: {
  57954. break; /* never here */
  57955. }
  57956. }
  57957. fail_internal:
  57958. DUK_ERROR_INTERNAL(thr);
  57959. DUK_WO_NORETURN(return 0;);
  57960. }
  57961. DUK_LOCAL void duk__ispec_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ispec *x, duk_regconst_t forced_reg) {
  57962. DUK_ASSERT(forced_reg >= 0);
  57963. (void) duk__ispec_toregconst_raw(comp_ctx, x, forced_reg, 0 /*flags*/);
  57964. }
  57965. /* Coerce an duk_ivalue to a 'plain' value by generating the necessary
  57966. * arithmetic operations, property access, or variable access bytecode.
  57967. * The duk_ivalue argument ('x') is converted into a plain value as a
  57968. * side effect.
  57969. */
  57970. DUK_LOCAL void duk__ivalue_toplain_raw(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_regconst_t forced_reg) {
  57971. duk_hthread *thr = comp_ctx->thr;
  57972. DUK_DDD(DUK_DDDPRINT("duk__ivalue_toplain_raw(): x={t=%ld,op=%ld,x1={%ld:%ld:%!T},x2={%ld:%ld:%!T}}, "
  57973. "forced_reg=%ld",
  57974. (long) x->t, (long) x->op,
  57975. (long) x->x1.t, (long) x->x1.regconst,
  57976. (duk_tval *) duk_get_tval(thr, x->x1.valstack_idx),
  57977. (long) x->x2.t, (long) x->x2.regconst,
  57978. (duk_tval *) duk_get_tval(thr, x->x2.valstack_idx),
  57979. (long) forced_reg));
  57980. switch (x->t) {
  57981. case DUK_IVAL_PLAIN: {
  57982. return;
  57983. }
  57984. /* XXX: support unary arithmetic ivalues (useful?) */
  57985. case DUK_IVAL_ARITH: {
  57986. duk_regconst_t arg1;
  57987. duk_regconst_t arg2;
  57988. duk_regconst_t dest;
  57989. duk_tval *tv1;
  57990. duk_tval *tv2;
  57991. DUK_DDD(DUK_DDDPRINT("arith to plain conversion"));
  57992. /* inline arithmetic check for constant values */
  57993. /* XXX: use the exactly same arithmetic function here as in executor */
  57994. if (x->x1.t == DUK_ISPEC_VALUE && x->x2.t == DUK_ISPEC_VALUE && x->t == DUK_IVAL_ARITH) {
  57995. tv1 = DUK_GET_TVAL_POSIDX(thr, x->x1.valstack_idx);
  57996. tv2 = DUK_GET_TVAL_POSIDX(thr, x->x2.valstack_idx);
  57997. DUK_ASSERT(tv1 != NULL);
  57998. DUK_ASSERT(tv2 != NULL);
  57999. DUK_DDD(DUK_DDDPRINT("arith: tv1=%!T, tv2=%!T",
  58000. (duk_tval *) tv1,
  58001. (duk_tval *) tv2));
  58002. if (DUK_TVAL_IS_NUMBER(tv1) && DUK_TVAL_IS_NUMBER(tv2)) {
  58003. duk_double_t d1 = DUK_TVAL_GET_NUMBER(tv1);
  58004. duk_double_t d2 = DUK_TVAL_GET_NUMBER(tv2);
  58005. duk_double_t d3;
  58006. duk_bool_t accept_fold = 1;
  58007. DUK_DDD(DUK_DDDPRINT("arith inline check: d1=%lf, d2=%lf, op=%ld",
  58008. (double) d1, (double) d2, (long) x->op));
  58009. switch (x->op) {
  58010. case DUK_OP_ADD: {
  58011. d3 = d1 + d2;
  58012. break;
  58013. }
  58014. case DUK_OP_SUB: {
  58015. d3 = d1 - d2;
  58016. break;
  58017. }
  58018. case DUK_OP_MUL: {
  58019. d3 = d1 * d2;
  58020. break;
  58021. }
  58022. case DUK_OP_DIV: {
  58023. /* Division-by-zero is undefined
  58024. * behavior, so rely on a helper.
  58025. */
  58026. d3 = duk_double_div(d1, d2);
  58027. break;
  58028. }
  58029. case DUK_OP_EXP: {
  58030. d3 = (duk_double_t) duk_js_arith_pow((double) d1, (double) d2);
  58031. break;
  58032. }
  58033. default: {
  58034. d3 = 0.0; /* Won't be used, but silence MSVC /W4 warning. */
  58035. accept_fold = 0;
  58036. break;
  58037. }
  58038. }
  58039. if (accept_fold) {
  58040. duk_double_union du;
  58041. du.d = d3;
  58042. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  58043. d3 = du.d;
  58044. x->t = DUK_IVAL_PLAIN;
  58045. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  58046. DUK_TVAL_SET_NUMBER(tv1, d3); /* old value is number: no refcount */
  58047. return;
  58048. }
  58049. } else if (x->op == DUK_OP_ADD && DUK_TVAL_IS_STRING(tv1) && DUK_TVAL_IS_STRING(tv2)) {
  58050. /* Inline string concatenation. No need to check for
  58051. * symbols, as all inputs are valid ECMAScript strings.
  58052. */
  58053. duk_dup(thr, x->x1.valstack_idx);
  58054. duk_dup(thr, x->x2.valstack_idx);
  58055. duk_concat(thr, 2);
  58056. duk_replace(thr, x->x1.valstack_idx);
  58057. x->t = DUK_IVAL_PLAIN;
  58058. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  58059. return;
  58060. }
  58061. }
  58062. arg1 = duk__ispec_toregconst_raw(comp_ctx, &x->x1, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  58063. arg2 = duk__ispec_toregconst_raw(comp_ctx, &x->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  58064. /* If forced reg, use it as destination. Otherwise try to
  58065. * use either coerced ispec if it is a temporary.
  58066. */
  58067. if (forced_reg >= 0) {
  58068. dest = forced_reg;
  58069. } else if (DUK__ISREG_TEMP(comp_ctx, arg1)) {
  58070. dest = arg1;
  58071. } else if (DUK__ISREG_TEMP(comp_ctx, arg2)) {
  58072. dest = arg2;
  58073. } else {
  58074. dest = DUK__ALLOCTEMP(comp_ctx);
  58075. }
  58076. DUK_ASSERT(DUK__ISREG(dest));
  58077. duk__emit_a_b_c(comp_ctx, x->op | DUK__EMIT_FLAG_BC_REGCONST, dest, arg1, arg2);
  58078. duk__ivalue_regconst(x, dest);
  58079. return;
  58080. }
  58081. case DUK_IVAL_PROP: {
  58082. /* XXX: very similar to DUK_IVAL_ARITH - merge? */
  58083. duk_regconst_t arg1;
  58084. duk_regconst_t arg2;
  58085. duk_regconst_t dest;
  58086. /* Need a short reg/const, does not have to be a mutable temp. */
  58087. arg1 = duk__ispec_toregconst_raw(comp_ctx, &x->x1, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  58088. arg2 = duk__ispec_toregconst_raw(comp_ctx, &x->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_SHORT /*flags*/);
  58089. /* Pick a destination register. If either base value or key
  58090. * happens to be a temp value, reuse it as the destination.
  58091. *
  58092. * XXX: The temp must be a "mutable" one, i.e. such that no
  58093. * other expression is using it anymore. Here this should be
  58094. * the case because the value of a property access expression
  58095. * is neither the base nor the key, but the lookup result.
  58096. */
  58097. if (forced_reg >= 0) {
  58098. dest = forced_reg;
  58099. } else if (DUK__ISREG_TEMP(comp_ctx, arg1)) {
  58100. dest = arg1;
  58101. } else if (DUK__ISREG_TEMP(comp_ctx, arg2)) {
  58102. dest = arg2;
  58103. } else {
  58104. dest = DUK__ALLOCTEMP(comp_ctx);
  58105. }
  58106. duk__emit_a_b_c(comp_ctx,
  58107. DUK_OP_GETPROP | DUK__EMIT_FLAG_BC_REGCONST,
  58108. dest,
  58109. arg1,
  58110. arg2);
  58111. duk__ivalue_regconst(x, dest);
  58112. return;
  58113. }
  58114. case DUK_IVAL_VAR: {
  58115. /* x1 must be a string */
  58116. duk_regconst_t dest;
  58117. duk_regconst_t reg_varbind;
  58118. duk_regconst_t rc_varname;
  58119. DUK_ASSERT(x->x1.t == DUK_ISPEC_VALUE);
  58120. duk_dup(thr, x->x1.valstack_idx);
  58121. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  58122. duk__ivalue_regconst(x, reg_varbind);
  58123. } else {
  58124. dest = (forced_reg >= 0 ? forced_reg : DUK__ALLOCTEMP(comp_ctx));
  58125. duk__emit_a_bc(comp_ctx, DUK_OP_GETVAR, dest, rc_varname);
  58126. duk__ivalue_regconst(x, dest);
  58127. }
  58128. return;
  58129. }
  58130. case DUK_IVAL_NONE:
  58131. default: {
  58132. DUK_D(DUK_DPRINT("invalid ivalue type: %ld", (long) x->t));
  58133. break;
  58134. }
  58135. }
  58136. DUK_ERROR_INTERNAL(thr);
  58137. DUK_WO_NORETURN(return;);
  58138. }
  58139. /* evaluate to plain value, no forced register (temp/bound reg both ok) */
  58140. DUK_LOCAL void duk__ivalue_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  58141. duk__ivalue_toplain_raw(comp_ctx, x, -1 /*forced_reg*/);
  58142. }
  58143. /* evaluate to final form (e.g. coerce GETPROP to code), throw away temp */
  58144. DUK_LOCAL void duk__ivalue_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  58145. duk_regconst_t temp;
  58146. /* If duk__ivalue_toplain_raw() allocates a temp, forget it and
  58147. * restore next temp state.
  58148. */
  58149. temp = DUK__GETTEMP(comp_ctx);
  58150. duk__ivalue_toplain_raw(comp_ctx, x, -1 /*forced_reg*/);
  58151. DUK__SETTEMP(comp_ctx, temp);
  58152. }
  58153. /* Coerce an duk_ivalue to a register or constant; result register may
  58154. * be a temp or a bound register.
  58155. *
  58156. * The duk_ivalue argument ('x') is converted into a regconst as a
  58157. * side effect.
  58158. */
  58159. DUK_LOCAL
  58160. duk_regconst_t duk__ivalue_toregconst_raw(duk_compiler_ctx *comp_ctx,
  58161. duk_ivalue *x,
  58162. duk_regconst_t forced_reg,
  58163. duk_small_uint_t flags) {
  58164. duk_hthread *thr = comp_ctx->thr;
  58165. duk_regconst_t reg;
  58166. DUK_UNREF(thr);
  58167. DUK_DDD(DUK_DDDPRINT("duk__ivalue_toregconst_raw(): x={t=%ld,op=%ld,x1={%ld:%ld:%!T},x2={%ld:%ld:%!T}}, "
  58168. "forced_reg=%ld, flags 0x%08lx: allow_const=%ld require_temp=%ld require_short=%ld",
  58169. (long) x->t, (long) x->op,
  58170. (long) x->x1.t, (long) x->x1.regconst,
  58171. (duk_tval *) duk_get_tval(thr, x->x1.valstack_idx),
  58172. (long) x->x2.t, (long) x->x2.regconst,
  58173. (duk_tval *) duk_get_tval(thr, x->x2.valstack_idx),
  58174. (long) forced_reg,
  58175. (unsigned long) flags,
  58176. (long) ((flags & DUK__IVAL_FLAG_ALLOW_CONST) ? 1 : 0),
  58177. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_TEMP) ? 1 : 0),
  58178. (long) ((flags & DUK__IVAL_FLAG_REQUIRE_SHORT) ? 1 : 0)));
  58179. /* first coerce to a plain value */
  58180. duk__ivalue_toplain_raw(comp_ctx, x, forced_reg);
  58181. DUK_ASSERT(x->t == DUK_IVAL_PLAIN);
  58182. /* then to a register */
  58183. reg = duk__ispec_toregconst_raw(comp_ctx, &x->x1, forced_reg, flags);
  58184. duk__ivalue_regconst(x, reg);
  58185. return reg;
  58186. }
  58187. DUK_LOCAL duk_regconst_t duk__ivalue_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  58188. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, 0 /*flags*/);
  58189. }
  58190. #if 0 /* unused */
  58191. DUK_LOCAL duk_regconst_t duk__ivalue_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  58192. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  58193. }
  58194. #endif
  58195. DUK_LOCAL void duk__ivalue_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *x, duk_int_t forced_reg) {
  58196. DUK_ASSERT(forced_reg >= 0);
  58197. (void) duk__ivalue_toregconst_raw(comp_ctx, x, forced_reg, 0 /*flags*/);
  58198. }
  58199. DUK_LOCAL duk_regconst_t duk__ivalue_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  58200. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  58201. }
  58202. DUK_LOCAL duk_regconst_t duk__ivalue_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *x) {
  58203. return duk__ivalue_toregconst_raw(comp_ctx, x, -1, DUK__IVAL_FLAG_ALLOW_CONST | DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  58204. }
  58205. /* The issues below can be solved with better flags */
  58206. /* XXX: many operations actually want toforcedtemp() -- brand new temp? */
  58207. /* XXX: need a toplain_ignore() which will only coerce a value to a temp
  58208. * register if it might have a side effect. Side-effect free values do not
  58209. * need to be coerced.
  58210. */
  58211. /*
  58212. * Identifier handling
  58213. */
  58214. DUK_LOCAL duk_regconst_t duk__lookup_active_register_binding(duk_compiler_ctx *comp_ctx) {
  58215. duk_hthread *thr = comp_ctx->thr;
  58216. duk_hstring *h_varname;
  58217. duk_regconst_t ret;
  58218. DUK_DDD(DUK_DDDPRINT("resolving identifier reference to '%!T'",
  58219. (duk_tval *) duk_get_tval(thr, -1)));
  58220. /*
  58221. * Special name handling
  58222. */
  58223. h_varname = duk_known_hstring(thr, -1);
  58224. if (h_varname == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)) {
  58225. DUK_DDD(DUK_DDDPRINT("flagging function as accessing 'arguments'"));
  58226. comp_ctx->curr_func.id_access_arguments = 1;
  58227. }
  58228. /*
  58229. * Inside one or more 'with' statements fall back to slow path always.
  58230. * (See e.g. test-stmt-with.js.)
  58231. */
  58232. if (comp_ctx->curr_func.with_depth > 0) {
  58233. DUK_DDD(DUK_DDDPRINT("identifier lookup inside a 'with' -> fall back to slow path"));
  58234. goto slow_path_own;
  58235. }
  58236. /*
  58237. * Any catch bindings ("catch (e)") also affect identifier binding.
  58238. *
  58239. * Currently, the varmap is modified for the duration of the catch
  58240. * clause to ensure any identifier accesses with the catch variable
  58241. * name will use slow path.
  58242. */
  58243. duk_get_prop(thr, comp_ctx->curr_func.varmap_idx);
  58244. if (duk_is_number(thr, -1)) {
  58245. ret = duk_to_int(thr, -1);
  58246. duk_pop(thr);
  58247. } else {
  58248. duk_pop(thr);
  58249. if (comp_ctx->curr_func.catch_depth > 0 || comp_ctx->curr_func.with_depth > 0) {
  58250. DUK_DDD(DUK_DDDPRINT("slow path access from inside a try-catch or with needs _Varmap"));
  58251. goto slow_path_own;
  58252. } else {
  58253. /* In this case we're doing a variable lookup that doesn't
  58254. * match our own variables, so _Varmap won't be needed at
  58255. * run time.
  58256. */
  58257. DUK_DDD(DUK_DDDPRINT("slow path access outside of try-catch and with, no need for _Varmap"));
  58258. goto slow_path_notown;
  58259. }
  58260. }
  58261. DUK_DDD(DUK_DDDPRINT("identifier lookup -> reg %ld", (long) ret));
  58262. return ret;
  58263. slow_path_notown:
  58264. DUK_DDD(DUK_DDDPRINT("identifier lookup -> slow path, not own variable"));
  58265. comp_ctx->curr_func.id_access_slow = 1;
  58266. return (duk_regconst_t) -1;
  58267. slow_path_own:
  58268. DUK_DDD(DUK_DDDPRINT("identifier lookup -> slow path, may be own variable"));
  58269. comp_ctx->curr_func.id_access_slow = 1;
  58270. comp_ctx->curr_func.id_access_slow_own = 1;
  58271. return (duk_regconst_t) -1;
  58272. }
  58273. /* Lookup an identifier name in the current varmap, indicating whether the
  58274. * identifier is register-bound and if not, allocating a constant for the
  58275. * identifier name. Returns 1 if register-bound, 0 otherwise. Caller can
  58276. * also check (out_reg_varbind >= 0) to check whether or not identifier is
  58277. * register bound. The caller must NOT use out_rc_varname at all unless
  58278. * return code is 0 or out_reg_varbind is < 0; this is becuase out_rc_varname
  58279. * is unsigned and doesn't have a "unused" / none value.
  58280. */
  58281. DUK_LOCAL duk_bool_t duk__lookup_lhs(duk_compiler_ctx *comp_ctx, duk_regconst_t *out_reg_varbind, duk_regconst_t *out_rc_varname) {
  58282. duk_hthread *thr = comp_ctx->thr;
  58283. duk_regconst_t reg_varbind;
  58284. duk_regconst_t rc_varname;
  58285. /* [ ... varname ] */
  58286. duk_dup_top(thr);
  58287. reg_varbind = duk__lookup_active_register_binding(comp_ctx);
  58288. if (reg_varbind >= 0) {
  58289. *out_reg_varbind = reg_varbind;
  58290. *out_rc_varname = 0; /* duk_regconst_t is unsigned, so use 0 as dummy value (ignored by caller) */
  58291. duk_pop(thr);
  58292. return 1;
  58293. } else {
  58294. rc_varname = duk__getconst(comp_ctx);
  58295. *out_reg_varbind = -1;
  58296. *out_rc_varname = rc_varname;
  58297. return 0;
  58298. }
  58299. }
  58300. /*
  58301. * Label handling
  58302. *
  58303. * Labels are initially added with flags prohibiting both break and continue.
  58304. * When the statement type is finally uncovered (after potentially multiple
  58305. * labels), all the labels are updated to allow/prohibit break and continue.
  58306. */
  58307. DUK_LOCAL void duk__add_label(duk_compiler_ctx *comp_ctx, duk_hstring *h_label, duk_int_t pc_label, duk_int_t label_id) {
  58308. duk_hthread *thr = comp_ctx->thr;
  58309. duk_size_t n;
  58310. duk_size_t new_size;
  58311. duk_uint8_t *p;
  58312. duk_labelinfo *li_start, *li;
  58313. /* Duplicate (shadowing) labels are not allowed, except for the empty
  58314. * labels (which are used as default labels for switch and iteration
  58315. * statements).
  58316. *
  58317. * We could also allow shadowing of non-empty pending labels without any
  58318. * other issues than breaking the required label shadowing requirements
  58319. * of the E5 specification, see Section 12.12.
  58320. */
  58321. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  58322. li_start = (duk_labelinfo *) (void *) p;
  58323. li = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  58324. n = (duk_size_t) (li - li_start);
  58325. while (li > li_start) {
  58326. li--;
  58327. if (li->h_label == h_label && h_label != DUK_HTHREAD_STRING_EMPTY_STRING(thr)) {
  58328. DUK_ERROR_SYNTAX(thr, DUK_STR_DUPLICATE_LABEL);
  58329. DUK_WO_NORETURN(return;);
  58330. }
  58331. }
  58332. duk_push_hstring(thr, h_label);
  58333. DUK_ASSERT(n <= DUK_UARRIDX_MAX); /* label limits */
  58334. (void) duk_put_prop_index(thr, comp_ctx->curr_func.labelnames_idx, (duk_uarridx_t) n);
  58335. new_size = (n + 1) * sizeof(duk_labelinfo);
  58336. duk_hbuffer_resize(thr, comp_ctx->curr_func.h_labelinfos, new_size);
  58337. /* XXX: slack handling, slow now */
  58338. /* relookup after possible realloc */
  58339. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  58340. li_start = (duk_labelinfo *) (void *) p;
  58341. DUK_UNREF(li_start); /* silence scan-build warning */
  58342. li = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  58343. li--;
  58344. /* Labels can be used for iteration statements but also for other statements,
  58345. * in particular a label can be used for a block statement. All cases of a
  58346. * named label accept a 'break' so that flag is set here. Iteration statements
  58347. * also allow 'continue', so that flag is updated when we figure out the
  58348. * statement type.
  58349. */
  58350. li->flags = DUK_LABEL_FLAG_ALLOW_BREAK;
  58351. li->label_id = label_id;
  58352. li->h_label = h_label;
  58353. li->catch_depth = comp_ctx->curr_func.catch_depth; /* catch depth from current func */
  58354. li->pc_label = pc_label;
  58355. DUK_DDD(DUK_DDDPRINT("registered label: flags=0x%08lx, id=%ld, name=%!O, catch_depth=%ld, pc_label=%ld",
  58356. (unsigned long) li->flags, (long) li->label_id, (duk_heaphdr *) li->h_label,
  58357. (long) li->catch_depth, (long) li->pc_label));
  58358. }
  58359. /* Update all labels with matching label_id. */
  58360. DUK_LOCAL void duk__update_label_flags(duk_compiler_ctx *comp_ctx, duk_int_t label_id, duk_small_uint_t flags) {
  58361. duk_uint8_t *p;
  58362. duk_labelinfo *li_start, *li;
  58363. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(comp_ctx->thr->heap, comp_ctx->curr_func.h_labelinfos);
  58364. li_start = (duk_labelinfo *) (void *) p;
  58365. li = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  58366. /* Match labels starting from latest; once label_id no longer matches, we can
  58367. * safely exit without checking the rest of the labels (only the topmost labels
  58368. * are ever updated).
  58369. */
  58370. while (li > li_start) {
  58371. li--;
  58372. if (li->label_id != label_id) {
  58373. break;
  58374. }
  58375. DUK_DDD(DUK_DDDPRINT("updating (overwriting) label flags for li=%p, label_id=%ld, flags=%ld",
  58376. (void *) li, (long) label_id, (long) flags));
  58377. li->flags = flags;
  58378. }
  58379. }
  58380. /* Lookup active label information. Break/continue distinction is necessary to handle switch
  58381. * statement related labels correctly: a switch will only catch a 'break', not a 'continue'.
  58382. *
  58383. * An explicit label cannot appear multiple times in the active set, but empty labels (unlabelled
  58384. * iteration and switch statements) can. A break will match the closest unlabelled or labelled
  58385. * statement. A continue will match the closest unlabelled or labelled iteration statement. It is
  58386. * a syntax error if a continue matches a labelled switch statement; because an explicit label cannot
  58387. * be duplicated, the continue cannot match any valid label outside the switch.
  58388. *
  58389. * A side effect of these rules is that a LABEL statement related to a switch should never actually
  58390. * catch a continue abrupt completion at run-time. Hence an INVALID opcode can be placed in the
  58391. * continue slot of the switch's LABEL statement.
  58392. */
  58393. /* XXX: awkward, especially the bunch of separate output values -> output struct? */
  58394. DUK_LOCAL void duk__lookup_active_label(duk_compiler_ctx *comp_ctx, duk_hstring *h_label, duk_bool_t is_break, duk_int_t *out_label_id, duk_int_t *out_label_catch_depth, duk_int_t *out_label_pc, duk_bool_t *out_is_closest) {
  58395. duk_hthread *thr = comp_ctx->thr;
  58396. duk_uint8_t *p;
  58397. duk_labelinfo *li_start, *li_end, *li;
  58398. duk_bool_t match = 0;
  58399. DUK_DDD(DUK_DDDPRINT("looking up active label: label='%!O', is_break=%ld",
  58400. (duk_heaphdr *) h_label, (long) is_break));
  58401. DUK_UNREF(thr);
  58402. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, comp_ctx->curr_func.h_labelinfos);
  58403. li_start = (duk_labelinfo *) (void *) p;
  58404. li_end = (duk_labelinfo *) (void *) (p + DUK_HBUFFER_GET_SIZE(comp_ctx->curr_func.h_labelinfos));
  58405. li = li_end;
  58406. /* Match labels starting from latest label because there can be duplicate empty
  58407. * labels in the label set.
  58408. */
  58409. while (li > li_start) {
  58410. li--;
  58411. if (li->h_label != h_label) {
  58412. DUK_DDD(DUK_DDDPRINT("labelinfo[%ld] ->'%!O' != %!O",
  58413. (long) (li - li_start),
  58414. (duk_heaphdr *) li->h_label,
  58415. (duk_heaphdr *) h_label));
  58416. continue;
  58417. }
  58418. DUK_DDD(DUK_DDDPRINT("labelinfo[%ld] -> '%!O' label name matches (still need to check type)",
  58419. (long) (li - li_start), (duk_heaphdr *) h_label));
  58420. /* currently all labels accept a break, so no explicit check for it now */
  58421. DUK_ASSERT(li->flags & DUK_LABEL_FLAG_ALLOW_BREAK);
  58422. if (is_break) {
  58423. /* break matches always */
  58424. match = 1;
  58425. break;
  58426. } else if (li->flags & DUK_LABEL_FLAG_ALLOW_CONTINUE) {
  58427. /* iteration statements allow continue */
  58428. match = 1;
  58429. break;
  58430. } else {
  58431. /* continue matched this label -- we can only continue if this is the empty
  58432. * label, for which duplication is allowed, and thus there is hope of
  58433. * finding a match deeper in the label stack.
  58434. */
  58435. if (h_label != DUK_HTHREAD_STRING_EMPTY_STRING(thr)) {
  58436. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_LABEL);
  58437. DUK_WO_NORETURN(return;);
  58438. } else {
  58439. DUK_DDD(DUK_DDDPRINT("continue matched an empty label which does not "
  58440. "allow a continue -> continue lookup deeper in label stack"));
  58441. }
  58442. }
  58443. }
  58444. /* XXX: match flag is awkward, rework */
  58445. if (!match) {
  58446. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_LABEL);
  58447. DUK_WO_NORETURN(return;);
  58448. }
  58449. DUK_DDD(DUK_DDDPRINT("label match: %!O -> label_id %ld, catch_depth=%ld, pc_label=%ld",
  58450. (duk_heaphdr *) h_label, (long) li->label_id,
  58451. (long) li->catch_depth, (long) li->pc_label));
  58452. *out_label_id = li->label_id;
  58453. *out_label_catch_depth = li->catch_depth;
  58454. *out_label_pc = li->pc_label;
  58455. *out_is_closest = (li == li_end - 1);
  58456. }
  58457. DUK_LOCAL void duk__reset_labels_to_length(duk_compiler_ctx *comp_ctx, duk_size_t len) {
  58458. duk_hthread *thr = comp_ctx->thr;
  58459. duk_set_length(thr, comp_ctx->curr_func.labelnames_idx, len);
  58460. duk_hbuffer_resize(thr, comp_ctx->curr_func.h_labelinfos, sizeof(duk_labelinfo) * len);
  58461. }
  58462. /*
  58463. * Expression parsing: duk__expr_nud(), duk__expr_led(), duk__expr_lbp(), and helpers.
  58464. *
  58465. * - duk__expr_nud(): ("null denotation"): process prev_token as a "start" of an expression (e.g. literal)
  58466. * - duk__expr_led(): ("left denotation"): process prev_token in the "middle" of an expression (e.g. operator)
  58467. * - duk__expr_lbp(): ("left-binding power"): return left-binding power of curr_token
  58468. */
  58469. /* object literal key tracking flags */
  58470. #define DUK__OBJ_LIT_KEY_PLAIN (1 << 0) /* key encountered as a plain property */
  58471. #define DUK__OBJ_LIT_KEY_GET (1 << 1) /* key encountered as a getter */
  58472. #define DUK__OBJ_LIT_KEY_SET (1 << 2) /* key encountered as a setter */
  58473. DUK_LOCAL void duk__nud_array_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  58474. duk_hthread *thr = comp_ctx->thr;
  58475. duk_regconst_t reg_obj; /* result reg */
  58476. duk_regconst_t reg_temp; /* temp reg */
  58477. duk_regconst_t temp_start; /* temp reg value for start of loop */
  58478. duk_small_uint_t max_init_values; /* max # of values initialized in one MPUTARR set */
  58479. duk_small_uint_t num_values; /* number of values in current MPUTARR set */
  58480. duk_uarridx_t curr_idx; /* current (next) array index */
  58481. duk_uarridx_t start_idx; /* start array index of current MPUTARR set */
  58482. duk_uarridx_t init_idx; /* last array index explicitly initialized, +1 */
  58483. duk_bool_t require_comma; /* next loop requires a comma */
  58484. #if !defined(DUK_USE_PREFER_SIZE)
  58485. duk_int_t pc_newarr;
  58486. duk_compiler_instr *instr;
  58487. #endif
  58488. /* DUK_TOK_LBRACKET already eaten, current token is right after that */
  58489. DUK_ASSERT(comp_ctx->prev_token.t == DUK_TOK_LBRACKET);
  58490. max_init_values = DUK__MAX_ARRAY_INIT_VALUES; /* XXX: depend on available temps? */
  58491. reg_obj = DUK__ALLOCTEMP(comp_ctx);
  58492. #if !defined(DUK_USE_PREFER_SIZE)
  58493. pc_newarr = duk__get_current_pc(comp_ctx);
  58494. #endif
  58495. duk__emit_bc(comp_ctx, DUK_OP_NEWARR, reg_obj); /* XXX: patch initial size hint afterwards? */
  58496. temp_start = DUK__GETTEMP(comp_ctx);
  58497. /*
  58498. * Emit initializers in sets of maximum max_init_values.
  58499. * Corner cases such as single value initializers do not have
  58500. * special handling now.
  58501. *
  58502. * Elided elements must not be emitted as 'undefined' values,
  58503. * because such values would be enumerable (which is incorrect).
  58504. * Also note that trailing elisions must be reflected in the
  58505. * length of the final array but cause no elements to be actually
  58506. * inserted.
  58507. */
  58508. curr_idx = 0;
  58509. init_idx = 0; /* tracks maximum initialized index + 1 */
  58510. start_idx = 0;
  58511. require_comma = 0;
  58512. for (;;) {
  58513. num_values = 0;
  58514. DUK__SETTEMP(comp_ctx, temp_start);
  58515. if (comp_ctx->curr_token.t == DUK_TOK_RBRACKET) {
  58516. break;
  58517. }
  58518. for (;;) {
  58519. if (comp_ctx->curr_token.t == DUK_TOK_RBRACKET) {
  58520. /* the outer loop will recheck and exit */
  58521. break;
  58522. }
  58523. /* comma check */
  58524. if (require_comma) {
  58525. if (comp_ctx->curr_token.t == DUK_TOK_COMMA) {
  58526. /* comma after a value, expected */
  58527. duk__advance(comp_ctx);
  58528. require_comma = 0;
  58529. continue;
  58530. } else {
  58531. goto syntax_error;
  58532. }
  58533. } else {
  58534. if (comp_ctx->curr_token.t == DUK_TOK_COMMA) {
  58535. /* elision - flush */
  58536. curr_idx++;
  58537. duk__advance(comp_ctx);
  58538. /* if num_values > 0, MPUTARR emitted by outer loop after break */
  58539. break;
  58540. }
  58541. }
  58542. /* else an array initializer element */
  58543. /* initial index */
  58544. if (num_values == 0) {
  58545. start_idx = curr_idx;
  58546. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  58547. duk__emit_load_int32(comp_ctx, reg_temp, (duk_int32_t) start_idx);
  58548. }
  58549. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* alloc temp just in case, to update max temp */
  58550. DUK__SETTEMP(comp_ctx, reg_temp);
  58551. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/);
  58552. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  58553. num_values++;
  58554. curr_idx++;
  58555. require_comma = 1;
  58556. if (num_values >= max_init_values) {
  58557. /* MPUTARR emitted by outer loop */
  58558. break;
  58559. }
  58560. }
  58561. if (num_values > 0) {
  58562. /* - A is a source register (it's not a write target, but used
  58563. * to identify the target object) but can be shuffled.
  58564. * - B cannot be shuffled normally because it identifies a range
  58565. * of registers, the emitter has special handling for this
  58566. * (the "no shuffle" flag must not be set).
  58567. * - C is a non-register number and cannot be shuffled, but
  58568. * never needs to be.
  58569. */
  58570. duk__emit_a_b_c(comp_ctx,
  58571. DUK_OP_MPUTARR |
  58572. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  58573. DUK__EMIT_FLAG_A_IS_SOURCE,
  58574. reg_obj,
  58575. temp_start,
  58576. (duk_regconst_t) (num_values + 1));
  58577. init_idx = start_idx + num_values;
  58578. /* num_values and temp_start reset at top of outer loop */
  58579. }
  58580. }
  58581. /* Update initil size for NEWARR, doesn't need to be exact and is
  58582. * capped at A field limit.
  58583. */
  58584. #if !defined(DUK_USE_PREFER_SIZE)
  58585. instr = duk__get_instr_ptr(comp_ctx, pc_newarr);
  58586. instr->ins |= DUK_ENC_OP_A(0, curr_idx > DUK_BC_A_MAX ? DUK_BC_A_MAX : curr_idx);
  58587. #endif
  58588. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RBRACKET);
  58589. duk__advance(comp_ctx);
  58590. DUK_DDD(DUK_DDDPRINT("array literal done, curridx=%ld, initidx=%ld",
  58591. (long) curr_idx, (long) init_idx));
  58592. /* trailing elisions? */
  58593. if (curr_idx > init_idx) {
  58594. /* yes, must set array length explicitly */
  58595. DUK_DDD(DUK_DDDPRINT("array literal has trailing elisions which affect its length"));
  58596. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  58597. duk__emit_load_int32(comp_ctx, reg_temp, (duk_int_t) curr_idx);
  58598. duk__emit_a_bc(comp_ctx,
  58599. DUK_OP_SETALEN | DUK__EMIT_FLAG_A_IS_SOURCE,
  58600. reg_obj,
  58601. reg_temp);
  58602. }
  58603. DUK__SETTEMP(comp_ctx, temp_start);
  58604. duk__ivalue_regconst(res, reg_obj);
  58605. return;
  58606. syntax_error:
  58607. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_ARRAY_LITERAL);
  58608. DUK_WO_NORETURN(return;);
  58609. }
  58610. typedef struct {
  58611. duk_regconst_t reg_obj;
  58612. duk_regconst_t temp_start;
  58613. duk_small_uint_t num_pairs;
  58614. duk_small_uint_t num_total_pairs;
  58615. } duk__objlit_state;
  58616. DUK_LOCAL void duk__objlit_flush_keys(duk_compiler_ctx *comp_ctx, duk__objlit_state *st) {
  58617. if (st->num_pairs > 0) {
  58618. /* - A is a source register (it's not a write target, but used
  58619. * to identify the target object) but can be shuffled.
  58620. * - B cannot be shuffled normally because it identifies a range
  58621. * of registers, the emitter has special handling for this
  58622. * (the "no shuffle" flag must not be set).
  58623. * - C is a non-register number and cannot be shuffled, but
  58624. * never needs to be.
  58625. */
  58626. DUK_ASSERT(st->num_pairs > 0);
  58627. duk__emit_a_b_c(comp_ctx,
  58628. DUK_OP_MPUTOBJ |
  58629. DUK__EMIT_FLAG_NO_SHUFFLE_C |
  58630. DUK__EMIT_FLAG_A_IS_SOURCE,
  58631. st->reg_obj,
  58632. st->temp_start,
  58633. (duk_regconst_t) (st->num_pairs * 2));
  58634. st->num_total_pairs += st->num_pairs;
  58635. st->num_pairs = 0;
  58636. }
  58637. DUK__SETTEMP(comp_ctx, st->temp_start);
  58638. }
  58639. DUK_LOCAL duk_bool_t duk__objlit_load_key(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_token *tok, duk_regconst_t reg_temp) {
  58640. if (tok->t_nores == DUK_TOK_IDENTIFIER || tok->t_nores == DUK_TOK_STRING) {
  58641. /* same handling for identifiers and strings */
  58642. DUK_ASSERT(tok->str1 != NULL);
  58643. duk_push_hstring(comp_ctx->thr, tok->str1);
  58644. } else if (tok->t == DUK_TOK_NUMBER) {
  58645. /* numbers can be loaded as numbers and coerced on the fly */
  58646. duk_push_number(comp_ctx->thr, tok->num);
  58647. } else {
  58648. return 1; /* error */
  58649. }
  58650. duk__ivalue_plain_fromstack(comp_ctx, res);
  58651. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  58652. duk__ivalue_toforcedreg(comp_ctx, res, reg_temp);
  58653. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  58654. return 0;
  58655. }
  58656. DUK_LOCAL void duk__nud_object_literal(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  58657. duk_hthread *thr = comp_ctx->thr;
  58658. duk__objlit_state st;
  58659. duk_regconst_t reg_temp; /* temp reg */
  58660. duk_small_uint_t max_init_pairs; /* max # of key-value pairs initialized in one MPUTOBJ set */
  58661. duk_bool_t first; /* first value: comma must not precede the value */
  58662. duk_bool_t is_set, is_get; /* temps */
  58663. #if !defined(DUK_USE_PREFER_SIZE)
  58664. duk_int_t pc_newobj;
  58665. duk_compiler_instr *instr;
  58666. #endif
  58667. DUK_ASSERT(comp_ctx->prev_token.t == DUK_TOK_LCURLY);
  58668. max_init_pairs = DUK__MAX_OBJECT_INIT_PAIRS; /* XXX: depend on available temps? */
  58669. st.reg_obj = DUK__ALLOCTEMP(comp_ctx); /* target object */
  58670. st.temp_start = DUK__GETTEMP(comp_ctx); /* start of MPUTOBJ argument list */
  58671. st.num_pairs = 0; /* number of key/value pairs emitted for current MPUTOBJ set */
  58672. st.num_total_pairs = 0; /* number of key/value pairs emitted overall */
  58673. #if !defined(DUK_USE_PREFER_SIZE)
  58674. pc_newobj = duk__get_current_pc(comp_ctx);
  58675. #endif
  58676. duk__emit_bc(comp_ctx, DUK_OP_NEWOBJ, st.reg_obj);
  58677. /*
  58678. * Emit initializers in sets of maximum max_init_pairs keys.
  58679. * Setter/getter is handled separately and terminates the
  58680. * current set of initializer values. Corner cases such as
  58681. * single value initializers do not have special handling now.
  58682. */
  58683. first = 1;
  58684. for (;;) {
  58685. /*
  58686. * ES5 and ES2015+ provide a lot of different PropertyDefinition
  58687. * formats, see http://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer.
  58688. *
  58689. * PropertyName can be IdentifierName (includes reserved words), a string
  58690. * literal, or a number literal. Note that IdentifierName allows 'get' and
  58691. * 'set' too, so we need to look ahead to the next token to distinguish:
  58692. *
  58693. * { get : 1 }
  58694. *
  58695. * and
  58696. *
  58697. * { get foo() { return 1 } }
  58698. * { get get() { return 1 } } // 'get' as getter propertyname
  58699. *
  58700. * Finally, a trailing comma is allowed.
  58701. *
  58702. * Key name is coerced to string at compile time (and ends up as a
  58703. * a string constant) even for numeric keys (e.g. "{1:'foo'}").
  58704. * These could be emitted using e.g. LDINT, but that seems hardly
  58705. * worth the effort and would increase code size.
  58706. */
  58707. DUK_DDD(DUK_DDDPRINT("object literal loop, curr_token->t = %ld",
  58708. (long) comp_ctx->curr_token.t));
  58709. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  58710. break;
  58711. }
  58712. if (first) {
  58713. first = 0;
  58714. } else {
  58715. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  58716. goto syntax_error;
  58717. }
  58718. duk__advance(comp_ctx);
  58719. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  58720. /* trailing comma followed by rcurly */
  58721. break;
  58722. }
  58723. }
  58724. /* Advance to get one step of lookup. */
  58725. duk__advance(comp_ctx);
  58726. /* Flush current MPUTOBJ if enough many pairs gathered. */
  58727. if (st.num_pairs >= max_init_pairs) {
  58728. duk__objlit_flush_keys(comp_ctx, &st);
  58729. DUK_ASSERT(st.num_pairs == 0);
  58730. }
  58731. /* Reset temp register state and reserve reg_temp and
  58732. * reg_temp + 1 for handling the current property.
  58733. */
  58734. DUK__SETTEMP(comp_ctx, st.temp_start + 2 * (duk_regconst_t) st.num_pairs);
  58735. reg_temp = DUK__ALLOCTEMPS(comp_ctx, 2);
  58736. /* NOTE: "get" and "set" are not officially ReservedWords and the lexer
  58737. * currently treats them always like ordinary identifiers (DUK_TOK_GET
  58738. * and DUK_TOK_SET are unused). They need to be detected based on the
  58739. * identifier string content.
  58740. */
  58741. is_get = (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  58742. comp_ctx->prev_token.str1 == DUK_HTHREAD_STRING_GET(thr));
  58743. is_set = (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  58744. comp_ctx->prev_token.str1 == DUK_HTHREAD_STRING_SET(thr));
  58745. if ((is_get || is_set) && comp_ctx->curr_token.t != DUK_TOK_COLON) {
  58746. /* getter/setter */
  58747. duk_int_t fnum;
  58748. duk__objlit_flush_keys(comp_ctx, &st);
  58749. DUK_ASSERT(DUK__GETTEMP(comp_ctx) == st.temp_start); /* 2 regs are guaranteed to be allocated w.r.t. temp_max */
  58750. reg_temp = DUK__ALLOCTEMPS(comp_ctx, 2);
  58751. if (duk__objlit_load_key(comp_ctx, res, &comp_ctx->curr_token, reg_temp) != 0) {
  58752. goto syntax_error;
  58753. }
  58754. /* curr_token = get/set name */
  58755. fnum = duk__parse_func_like_fnum(comp_ctx, DUK__FUNC_FLAG_GETSET);
  58756. duk__emit_a_bc(comp_ctx,
  58757. DUK_OP_CLOSURE,
  58758. st.temp_start + 1,
  58759. (duk_regconst_t) fnum);
  58760. /* Slot C is used in a non-standard fashion (range of regs),
  58761. * emitter code has special handling for it (must not set the
  58762. * "no shuffle" flag).
  58763. */
  58764. duk__emit_a_bc(comp_ctx,
  58765. (is_get ? DUK_OP_INITGET : DUK_OP_INITSET) | DUK__EMIT_FLAG_A_IS_SOURCE,
  58766. st.reg_obj,
  58767. st.temp_start); /* temp_start+0 = key, temp_start+1 = closure */
  58768. DUK_ASSERT(st.num_pairs == 0); /* temp state is reset on next loop */
  58769. #if defined(DUK_USE_ES6)
  58770. } else if (comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  58771. (comp_ctx->curr_token.t == DUK_TOK_COMMA || comp_ctx->curr_token.t == DUK_TOK_RCURLY)) {
  58772. duk_bool_t load_rc;
  58773. load_rc = duk__objlit_load_key(comp_ctx, res, &comp_ctx->prev_token, reg_temp);
  58774. DUK_UNREF(load_rc);
  58775. DUK_ASSERT(load_rc == 0); /* always succeeds because token is identifier */
  58776. duk__ivalue_var_hstring(comp_ctx, res, comp_ctx->prev_token.str1);
  58777. DUK_ASSERT(DUK__GETTEMP(comp_ctx) == reg_temp + 1);
  58778. duk__ivalue_toforcedreg(comp_ctx, res, reg_temp + 1);
  58779. st.num_pairs++;
  58780. } else if ((comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER ||
  58781. comp_ctx->prev_token.t == DUK_TOK_STRING ||
  58782. comp_ctx->prev_token.t == DUK_TOK_NUMBER) &&
  58783. comp_ctx->curr_token.t == DUK_TOK_LPAREN) {
  58784. duk_int_t fnum;
  58785. /* Parsing-wise there's a small hickup here: the token parsing
  58786. * state is one step too advanced for the function parse helper
  58787. * compared to other cases. The current solution is an extra
  58788. * flag to indicate whether function parsing should use the
  58789. * current or the previous token to starting parsing from.
  58790. */
  58791. if (duk__objlit_load_key(comp_ctx, res, &comp_ctx->prev_token, reg_temp) != 0) {
  58792. goto syntax_error;
  58793. }
  58794. fnum = duk__parse_func_like_fnum(comp_ctx, DUK__FUNC_FLAG_USE_PREVTOKEN | DUK__FUNC_FLAG_METDEF);
  58795. duk__emit_a_bc(comp_ctx,
  58796. DUK_OP_CLOSURE,
  58797. reg_temp + 1,
  58798. (duk_regconst_t) fnum);
  58799. st.num_pairs++;
  58800. #endif /* DUK_USE_ES6 */
  58801. } else {
  58802. #if defined(DUK_USE_ES6)
  58803. if (comp_ctx->prev_token.t == DUK_TOK_LBRACKET) {
  58804. /* ES2015 computed property name. Executor ToPropertyKey()
  58805. * coerces the key at runtime.
  58806. */
  58807. DUK__SETTEMP(comp_ctx, reg_temp);
  58808. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_FOR_EXPR, reg_temp);
  58809. duk__advance_expect(comp_ctx, DUK_TOK_RBRACKET);
  58810. /* XXX: If next token is '(' we're dealing with
  58811. * the method shorthand with a computed name,
  58812. * e.g. { [Symbol.for('foo')](a,b) {} }. This
  58813. * form is not yet supported and causes a
  58814. * SyntaxError on the DUK_TOK_COLON check below.
  58815. */
  58816. }
  58817. else
  58818. #endif /* DUK_USE_ES6 */
  58819. {
  58820. if (duk__objlit_load_key(comp_ctx, res, &comp_ctx->prev_token, reg_temp) != 0) {
  58821. goto syntax_error;
  58822. }
  58823. }
  58824. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  58825. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  58826. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp + 1 /*forced_reg*/);
  58827. st.num_pairs++;
  58828. }
  58829. } /* property loop */
  58830. /* Flush remaining properties. */
  58831. duk__objlit_flush_keys(comp_ctx, &st);
  58832. DUK_ASSERT(st.num_pairs == 0);
  58833. DUK_ASSERT(DUK__GETTEMP(comp_ctx) == st.temp_start);
  58834. /* Update initial size for NEWOBJ. The init size doesn't need to be
  58835. * exact as the purpose is just to avoid object resizes in common
  58836. * cases. The size is capped to field A limit, and will be too high
  58837. * if the object literal contains duplicate keys (this is harmless but
  58838. * increases memory traffic if the object is compacted later on).
  58839. */
  58840. #if !defined(DUK_USE_PREFER_SIZE)
  58841. instr = duk__get_instr_ptr(comp_ctx, pc_newobj);
  58842. instr->ins |= DUK_ENC_OP_A(0, st.num_total_pairs > DUK_BC_A_MAX ? DUK_BC_A_MAX : st.num_total_pairs);
  58843. #endif
  58844. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RCURLY);
  58845. duk__advance(comp_ctx); /* No RegExp after object literal. */
  58846. duk__ivalue_regconst(res, st.reg_obj);
  58847. return;
  58848. syntax_error:
  58849. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_OBJECT_LITERAL);
  58850. DUK_WO_NORETURN(return;);
  58851. }
  58852. /* Parse argument list. Arguments are written to temps starting from
  58853. * "next temp". Returns number of arguments parsed. Expects left paren
  58854. * to be already eaten, and eats the right paren before returning.
  58855. */
  58856. DUK_LOCAL duk_int_t duk__parse_arguments(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  58857. duk_int_t nargs = 0;
  58858. duk_regconst_t reg_temp;
  58859. /* Note: expect that caller has already eaten the left paren */
  58860. DUK_DDD(DUK_DDDPRINT("start parsing arguments, prev_token.t=%ld, curr_token.t=%ld",
  58861. (long) comp_ctx->prev_token.t, (long) comp_ctx->curr_token.t));
  58862. for (;;) {
  58863. if (comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  58864. break;
  58865. }
  58866. if (nargs > 0) {
  58867. duk__advance_expect(comp_ctx, DUK_TOK_COMMA);
  58868. }
  58869. /* We want the argument expression value to go to "next temp"
  58870. * without additional moves. That should almost always be the
  58871. * case, but we double check after expression parsing.
  58872. *
  58873. * This is not the cleanest possible approach.
  58874. */
  58875. reg_temp = DUK__ALLOCTEMP(comp_ctx); /* bump up "allocated" reg count, just in case */
  58876. DUK__SETTEMP(comp_ctx, reg_temp);
  58877. /* binding power must be high enough to NOT allow comma expressions directly */
  58878. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp); /* always allow 'in', coerce to 'tr' just in case */
  58879. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  58880. nargs++;
  58881. DUK_DDD(DUK_DDDPRINT("argument #%ld written into reg %ld", (long) nargs, (long) reg_temp));
  58882. }
  58883. /* eat the right paren */
  58884. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* RegExp mode does not matter. */
  58885. DUK_DDD(DUK_DDDPRINT("end parsing arguments"));
  58886. return nargs;
  58887. }
  58888. DUK_LOCAL duk_bool_t duk__expr_is_empty(duk_compiler_ctx *comp_ctx) {
  58889. /* empty expressions can be detected conveniently with nud/led counts */
  58890. return (comp_ctx->curr_func.nud_count == 0) &&
  58891. (comp_ctx->curr_func.led_count == 0);
  58892. }
  58893. DUK_LOCAL void duk__expr_nud(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  58894. duk_hthread *thr = comp_ctx->thr;
  58895. duk_token *tk;
  58896. duk_regconst_t temp_at_entry;
  58897. duk_small_uint_t tok;
  58898. duk_uint32_t args; /* temp variable to pass constants and flags to shared code */
  58899. /*
  58900. * ctx->prev_token token to process with duk__expr_nud()
  58901. * ctx->curr_token updated by caller
  58902. *
  58903. * Note: the token in the switch below has already been eaten.
  58904. */
  58905. temp_at_entry = DUK__GETTEMP(comp_ctx);
  58906. comp_ctx->curr_func.nud_count++;
  58907. tk = &comp_ctx->prev_token;
  58908. tok = tk->t;
  58909. res->t = DUK_IVAL_NONE;
  58910. DUK_DDD(DUK_DDDPRINT("duk__expr_nud(), prev_token.t=%ld, allow_in=%ld, paren_level=%ld",
  58911. (long) tk->t, (long) comp_ctx->curr_func.allow_in, (long) comp_ctx->curr_func.paren_level));
  58912. switch (tok) {
  58913. /* PRIMARY EXPRESSIONS */
  58914. case DUK_TOK_THIS: {
  58915. duk_regconst_t reg_temp;
  58916. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  58917. duk__emit_bc(comp_ctx,
  58918. DUK_OP_LDTHIS,
  58919. reg_temp);
  58920. duk__ivalue_regconst(res, reg_temp);
  58921. return;
  58922. }
  58923. case DUK_TOK_IDENTIFIER: {
  58924. duk__ivalue_var_hstring(comp_ctx, res, tk->str1);
  58925. return;
  58926. }
  58927. case DUK_TOK_NULL: {
  58928. duk_push_null(thr);
  58929. goto plain_value;
  58930. }
  58931. case DUK_TOK_TRUE: {
  58932. duk_push_true(thr);
  58933. goto plain_value;
  58934. }
  58935. case DUK_TOK_FALSE: {
  58936. duk_push_false(thr);
  58937. goto plain_value;
  58938. }
  58939. case DUK_TOK_NUMBER: {
  58940. duk_push_number(thr, tk->num);
  58941. goto plain_value;
  58942. }
  58943. case DUK_TOK_STRING: {
  58944. DUK_ASSERT(tk->str1 != NULL);
  58945. duk_push_hstring(thr, tk->str1);
  58946. goto plain_value;
  58947. }
  58948. case DUK_TOK_REGEXP: {
  58949. #if defined(DUK_USE_REGEXP_SUPPORT)
  58950. duk_regconst_t reg_temp;
  58951. duk_regconst_t rc_re_bytecode; /* const */
  58952. duk_regconst_t rc_re_source; /* const */
  58953. DUK_ASSERT(tk->str1 != NULL);
  58954. DUK_ASSERT(tk->str2 != NULL);
  58955. DUK_DDD(DUK_DDDPRINT("emitting regexp op, str1=%!O, str2=%!O",
  58956. (duk_heaphdr *) tk->str1,
  58957. (duk_heaphdr *) tk->str2));
  58958. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  58959. duk_push_hstring(thr, tk->str1);
  58960. duk_push_hstring(thr, tk->str2);
  58961. /* [ ... pattern flags ] */
  58962. duk_regexp_compile(thr);
  58963. /* [ ... escaped_source bytecode ] */
  58964. rc_re_bytecode = duk__getconst(comp_ctx);
  58965. rc_re_source = duk__getconst(comp_ctx);
  58966. duk__emit_a_b_c(comp_ctx,
  58967. DUK_OP_REGEXP | DUK__EMIT_FLAG_BC_REGCONST,
  58968. reg_temp /*a*/,
  58969. rc_re_bytecode /*b*/,
  58970. rc_re_source /*c*/);
  58971. duk__ivalue_regconst(res, reg_temp);
  58972. return;
  58973. #else /* DUK_USE_REGEXP_SUPPORT */
  58974. goto syntax_error;
  58975. #endif /* DUK_USE_REGEXP_SUPPORT */
  58976. }
  58977. case DUK_TOK_LBRACKET: {
  58978. DUK_DDD(DUK_DDDPRINT("parsing array literal"));
  58979. duk__nud_array_literal(comp_ctx, res);
  58980. return;
  58981. }
  58982. case DUK_TOK_LCURLY: {
  58983. DUK_DDD(DUK_DDDPRINT("parsing object literal"));
  58984. duk__nud_object_literal(comp_ctx, res);
  58985. return;
  58986. }
  58987. case DUK_TOK_LPAREN: {
  58988. duk_bool_t prev_allow_in;
  58989. comp_ctx->curr_func.paren_level++;
  58990. prev_allow_in = comp_ctx->curr_func.allow_in;
  58991. comp_ctx->curr_func.allow_in = 1; /* reset 'allow_in' for parenthesized expression */
  58992. duk__expr(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression, terminates at a ')' */
  58993. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* No RegExp after parenthesized expression. */
  58994. comp_ctx->curr_func.allow_in = prev_allow_in;
  58995. comp_ctx->curr_func.paren_level--;
  58996. return;
  58997. }
  58998. /* MEMBER/NEW/CALL EXPRESSIONS */
  58999. case DUK_TOK_NEW: {
  59000. /*
  59001. * Parsing an expression starting with 'new' is tricky because
  59002. * there are multiple possible productions deriving from
  59003. * LeftHandSideExpression which begin with 'new'.
  59004. *
  59005. * We currently resort to one-token lookahead to distinguish the
  59006. * cases. Hopefully this is correct. The binding power must be
  59007. * such that parsing ends at an LPAREN (CallExpression) but not at
  59008. * a PERIOD or LBRACKET (MemberExpression).
  59009. *
  59010. * See doc/compiler.rst for discussion on the parsing approach,
  59011. * and testcases/test-dev-new.js for a bunch of documented tests.
  59012. */
  59013. duk_regconst_t reg_target;
  59014. duk_int_t nargs;
  59015. DUK_DDD(DUK_DDDPRINT("begin parsing new expression"));
  59016. reg_target = DUK__ALLOCTEMPS(comp_ctx, 2);
  59017. #if defined(DUK_USE_ES6)
  59018. if (comp_ctx->curr_token.t == DUK_TOK_PERIOD) {
  59019. /* new.target */
  59020. DUK_DDD(DUK_DDDPRINT("new.target"));
  59021. duk__advance(comp_ctx);
  59022. if (comp_ctx->curr_token.t_nores != DUK_TOK_IDENTIFIER ||
  59023. !duk_hstring_equals_ascii_cstring(comp_ctx->curr_token.str1, "target")) {
  59024. goto syntax_error_newtarget;
  59025. }
  59026. if (comp_ctx->curr_func.is_global) {
  59027. goto syntax_error_newtarget;
  59028. }
  59029. duk__advance(comp_ctx);
  59030. duk__emit_bc(comp_ctx,
  59031. DUK_OP_NEWTARGET,
  59032. reg_target);
  59033. duk__ivalue_regconst(res, reg_target);
  59034. return;
  59035. }
  59036. #endif /* DUK_USE_ES6 */
  59037. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_CALL /*rbp_flags*/, reg_target /*forced_reg*/);
  59038. duk__emit_bc(comp_ctx, DUK_OP_NEWOBJ, reg_target + 1); /* default instance */
  59039. DUK__SETTEMP(comp_ctx, reg_target + 2);
  59040. /* XXX: 'new obj.noSuch()' doesn't use GETPROPC now which
  59041. * makes the error message worse than for obj.noSuch().
  59042. */
  59043. if (comp_ctx->curr_token.t == DUK_TOK_LPAREN) {
  59044. /* 'new' MemberExpression Arguments */
  59045. DUK_DDD(DUK_DDDPRINT("new expression has argument list"));
  59046. duk__advance(comp_ctx);
  59047. nargs = duk__parse_arguments(comp_ctx, res); /* parse args starting from "next temp", reg_target + 1 */
  59048. /* right paren eaten */
  59049. } else {
  59050. /* 'new' MemberExpression */
  59051. DUK_DDD(DUK_DDDPRINT("new expression has no argument list"));
  59052. nargs = 0;
  59053. }
  59054. duk__emit_a_bc(comp_ctx,
  59055. DUK_OP_CALL0 | DUK_BC_CALL_FLAG_CONSTRUCT,
  59056. nargs /*num_args*/,
  59057. reg_target /*target*/);
  59058. DUK_DDD(DUK_DDDPRINT("end parsing new expression"));
  59059. duk__ivalue_regconst(res, reg_target);
  59060. return;
  59061. }
  59062. /* FUNCTION EXPRESSIONS */
  59063. case DUK_TOK_FUNCTION: {
  59064. /* Function expression. Note that any statement beginning with 'function'
  59065. * is handled by the statement parser as a function declaration, or a
  59066. * non-standard function expression/statement (or a SyntaxError). We only
  59067. * handle actual function expressions (occurring inside an expression) here.
  59068. *
  59069. * O(depth^2) parse count for inner functions is handled by recording a
  59070. * lexer offset on the first compilation pass, so that the function can
  59071. * be efficiently skipped on the second pass. This is encapsulated into
  59072. * duk__parse_func_like_fnum().
  59073. */
  59074. duk_regconst_t reg_temp;
  59075. duk_int_t fnum;
  59076. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59077. /* curr_token follows 'function' */
  59078. fnum = duk__parse_func_like_fnum(comp_ctx, 0 /*flags*/);
  59079. DUK_DDD(DUK_DDDPRINT("parsed inner function -> fnum %ld", (long) fnum));
  59080. duk__emit_a_bc(comp_ctx,
  59081. DUK_OP_CLOSURE,
  59082. reg_temp /*a*/,
  59083. (duk_regconst_t) fnum /*bc*/);
  59084. duk__ivalue_regconst(res, reg_temp);
  59085. return;
  59086. }
  59087. /* UNARY EXPRESSIONS */
  59088. case DUK_TOK_DELETE: {
  59089. /* Delete semantics are a bit tricky. The description in E5 specification
  59090. * is kind of confusing, because it distinguishes between resolvability of
  59091. * a reference (which is only known at runtime) seemingly at compile time
  59092. * (= SyntaxError throwing).
  59093. */
  59094. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59095. if (res->t == DUK_IVAL_VAR) {
  59096. /* not allowed in strict mode, regardless of whether resolves;
  59097. * in non-strict mode DELVAR handles both non-resolving and
  59098. * resolving cases (the specification description is a bit confusing).
  59099. */
  59100. duk_regconst_t reg_temp;
  59101. duk_regconst_t reg_varbind;
  59102. duk_regconst_t rc_varname;
  59103. if (comp_ctx->curr_func.is_strict) {
  59104. DUK_ERROR_SYNTAX(thr, DUK_STR_CANNOT_DELETE_IDENTIFIER);
  59105. DUK_WO_NORETURN(return;);
  59106. }
  59107. DUK__SETTEMP(comp_ctx, temp_at_entry);
  59108. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59109. duk_dup(thr, res->x1.valstack_idx);
  59110. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  59111. /* register bound variables are non-configurable -> always false */
  59112. duk__emit_bc(comp_ctx,
  59113. DUK_OP_LDFALSE,
  59114. reg_temp);
  59115. } else {
  59116. duk_dup(thr, res->x1.valstack_idx);
  59117. rc_varname = duk__getconst(comp_ctx);
  59118. duk__emit_a_bc(comp_ctx,
  59119. DUK_OP_DELVAR,
  59120. reg_temp,
  59121. rc_varname);
  59122. }
  59123. duk__ivalue_regconst(res, reg_temp);
  59124. } else if (res->t == DUK_IVAL_PROP) {
  59125. duk_regconst_t reg_temp;
  59126. duk_regconst_t reg_obj;
  59127. duk_regconst_t rc_key;
  59128. DUK__SETTEMP(comp_ctx, temp_at_entry);
  59129. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59130. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  59131. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  59132. duk__emit_a_b_c(comp_ctx,
  59133. DUK_OP_DELPROP | DUK__EMIT_FLAG_BC_REGCONST,
  59134. reg_temp,
  59135. reg_obj,
  59136. rc_key);
  59137. duk__ivalue_regconst(res, reg_temp);
  59138. } else {
  59139. /* non-Reference deletion is always 'true', even in strict mode */
  59140. duk_push_true(thr);
  59141. goto plain_value;
  59142. }
  59143. return;
  59144. }
  59145. case DUK_TOK_VOID: {
  59146. duk__expr_toplain_ignore(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59147. duk_push_undefined(thr);
  59148. goto plain_value;
  59149. }
  59150. case DUK_TOK_TYPEOF: {
  59151. /* 'typeof' must handle unresolvable references without throwing
  59152. * a ReferenceError (E5 Section 11.4.3). Register mapped values
  59153. * will never be unresolvable so special handling is only required
  59154. * when an identifier is a "slow path" one.
  59155. */
  59156. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59157. if (res->t == DUK_IVAL_VAR) {
  59158. duk_regconst_t reg_varbind;
  59159. duk_regconst_t rc_varname;
  59160. duk_regconst_t reg_temp;
  59161. duk_dup(thr, res->x1.valstack_idx);
  59162. if (!duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  59163. DUK_DDD(DUK_DDDPRINT("typeof for an identifier name which could not be resolved "
  59164. "at compile time, need to use special run-time handling"));
  59165. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59166. duk__emit_a_bc(comp_ctx,
  59167. DUK_OP_TYPEOFID,
  59168. reg_temp,
  59169. rc_varname);
  59170. duk__ivalue_regconst(res, reg_temp);
  59171. return;
  59172. }
  59173. }
  59174. args = DUK_OP_TYPEOF;
  59175. goto unary;
  59176. }
  59177. case DUK_TOK_INCREMENT: {
  59178. args = (DUK_OP_PREINCP << 8) + DUK_OP_PREINCR;
  59179. goto preincdec;
  59180. }
  59181. case DUK_TOK_DECREMENT: {
  59182. args = (DUK_OP_PREDECP << 8) + DUK_OP_PREDECR;
  59183. goto preincdec;
  59184. }
  59185. case DUK_TOK_ADD: {
  59186. /* unary plus */
  59187. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59188. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE &&
  59189. duk_is_number(thr, res->x1.valstack_idx)) {
  59190. /* unary plus of a number is identity */
  59191. return;
  59192. }
  59193. args = DUK_OP_UNP;
  59194. goto unary;
  59195. }
  59196. case DUK_TOK_SUB: {
  59197. /* unary minus */
  59198. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59199. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE &&
  59200. duk_is_number(thr, res->x1.valstack_idx)) {
  59201. /* this optimization is important to handle negative literals
  59202. * (which are not directly provided by the lexical grammar)
  59203. */
  59204. duk_tval *tv_num;
  59205. duk_double_union du;
  59206. tv_num = DUK_GET_TVAL_POSIDX(thr, res->x1.valstack_idx);
  59207. DUK_ASSERT(tv_num != NULL);
  59208. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_num));
  59209. du.d = DUK_TVAL_GET_NUMBER(tv_num);
  59210. du.d = -du.d;
  59211. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  59212. DUK_TVAL_SET_NUMBER(tv_num, du.d);
  59213. return;
  59214. }
  59215. args = DUK_OP_UNM;
  59216. goto unary;
  59217. }
  59218. case DUK_TOK_BNOT: {
  59219. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59220. args = DUK_OP_BNOT;
  59221. goto unary;
  59222. }
  59223. case DUK_TOK_LNOT: {
  59224. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59225. if (res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_VALUE) {
  59226. /* Very minimal inlining to handle common idioms '!0' and '!1',
  59227. * and also boolean arguments like '!false' and '!true'.
  59228. */
  59229. duk_tval *tv_val;
  59230. tv_val = DUK_GET_TVAL_POSIDX(thr, res->x1.valstack_idx);
  59231. DUK_ASSERT(tv_val != NULL);
  59232. if (DUK_TVAL_IS_NUMBER(tv_val)) {
  59233. duk_double_t d;
  59234. d = DUK_TVAL_GET_NUMBER(tv_val);
  59235. if (d == 0.0) {
  59236. /* Matches both +0 and -0 on purpose. */
  59237. DUK_DDD(DUK_DDDPRINT("inlined lnot: !0 -> true"));
  59238. DUK_TVAL_SET_BOOLEAN_TRUE(tv_val);
  59239. return;
  59240. } else if (d == 1.0) {
  59241. DUK_DDD(DUK_DDDPRINT("inlined lnot: !1 -> false"));
  59242. DUK_TVAL_SET_BOOLEAN_FALSE(tv_val);
  59243. return;
  59244. }
  59245. } else if (DUK_TVAL_IS_BOOLEAN(tv_val)) {
  59246. duk_small_uint_t v;
  59247. v = DUK_TVAL_GET_BOOLEAN(tv_val);
  59248. DUK_DDD(DUK_DDDPRINT("inlined lnot boolean: %ld", (long) v));
  59249. DUK_ASSERT(v == 0 || v == 1);
  59250. DUK_TVAL_SET_BOOLEAN(tv_val, v ^ 0x01);
  59251. return;
  59252. }
  59253. }
  59254. args = DUK_OP_LNOT;
  59255. goto unary;
  59256. }
  59257. } /* end switch */
  59258. DUK_ERROR_SYNTAX(thr, DUK_STR_PARSE_ERROR);
  59259. DUK_WO_NORETURN(return;);
  59260. unary:
  59261. {
  59262. /* Unary opcodes use just the 'BC' register source because it
  59263. * matches current shuffle limits, and maps cleanly to 16 high
  59264. * bits of the opcode.
  59265. */
  59266. duk_regconst_t reg_src, reg_res;
  59267. reg_src = duk__ivalue_toregconst_raw(comp_ctx, res, -1 /*forced_reg*/, 0 /*flags*/);
  59268. if (DUK__ISREG_TEMP(comp_ctx, reg_src)) {
  59269. reg_res = reg_src;
  59270. } else {
  59271. reg_res = DUK__ALLOCTEMP(comp_ctx);
  59272. }
  59273. duk__emit_a_bc(comp_ctx,
  59274. args,
  59275. reg_res,
  59276. reg_src);
  59277. duk__ivalue_regconst(res, reg_res);
  59278. return;
  59279. }
  59280. preincdec:
  59281. {
  59282. /* preincrement and predecrement */
  59283. duk_regconst_t reg_res;
  59284. duk_small_uint_t args_op1 = args & 0xff; /* DUK_OP_PREINCR/DUK_OP_PREDECR */
  59285. duk_small_uint_t args_op2 = args >> 8; /* DUK_OP_PREINCP_RR/DUK_OP_PREDECP_RR */
  59286. /* Specific assumptions for opcode numbering. */
  59287. DUK_ASSERT(DUK_OP_PREINCR + 4 == DUK_OP_PREINCV);
  59288. DUK_ASSERT(DUK_OP_PREDECR + 4 == DUK_OP_PREDECV);
  59289. reg_res = DUK__ALLOCTEMP(comp_ctx);
  59290. duk__expr(comp_ctx, res, DUK__BP_MULTIPLICATIVE /*rbp_flags*/); /* UnaryExpression */
  59291. if (res->t == DUK_IVAL_VAR) {
  59292. duk_hstring *h_varname;
  59293. duk_regconst_t reg_varbind;
  59294. duk_regconst_t rc_varname;
  59295. h_varname = duk_known_hstring(thr, res->x1.valstack_idx);
  59296. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  59297. goto syntax_error;
  59298. }
  59299. duk_dup(thr, res->x1.valstack_idx);
  59300. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  59301. duk__emit_a_bc(comp_ctx,
  59302. args_op1, /* e.g. DUK_OP_PREINCR */
  59303. reg_res,
  59304. reg_varbind);
  59305. } else {
  59306. duk__emit_a_bc(comp_ctx,
  59307. args_op1 + 4, /* e.g. DUK_OP_PREINCV */
  59308. reg_res,
  59309. rc_varname);
  59310. }
  59311. DUK_DDD(DUK_DDDPRINT("preincdec to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  59312. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  59313. } else if (res->t == DUK_IVAL_PROP) {
  59314. duk_regconst_t reg_obj; /* allocate to reg only (not const) */
  59315. duk_regconst_t rc_key;
  59316. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  59317. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  59318. duk__emit_a_b_c(comp_ctx,
  59319. args_op2 | DUK__EMIT_FLAG_BC_REGCONST, /* e.g. DUK_OP_PREINCP */
  59320. reg_res,
  59321. reg_obj,
  59322. rc_key);
  59323. } else {
  59324. /* Technically return value is not needed because INVLHS will
  59325. * unconditially throw a ReferenceError. Coercion is necessary
  59326. * for proper semantics (consider ToNumber() called for an object).
  59327. * Use DUK_OP_UNP with a dummy register to get ToNumber().
  59328. */
  59329. duk__ivalue_toforcedreg(comp_ctx, res, reg_res);
  59330. duk__emit_bc(comp_ctx,
  59331. DUK_OP_UNP,
  59332. reg_res); /* for side effects, result ignored */
  59333. duk__emit_op_only(comp_ctx,
  59334. DUK_OP_INVLHS);
  59335. }
  59336. DUK__SETTEMP(comp_ctx, reg_res + 1);
  59337. duk__ivalue_regconst(res, reg_res);
  59338. return;
  59339. }
  59340. plain_value:
  59341. {
  59342. /* Stack top contains plain value */
  59343. duk__ivalue_plain_fromstack(comp_ctx, res);
  59344. return;
  59345. }
  59346. #if defined(DUK_USE_ES6)
  59347. syntax_error_newtarget:
  59348. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_NEWTARGET);
  59349. DUK_WO_NORETURN(return;);
  59350. #endif
  59351. syntax_error:
  59352. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_EXPRESSION);
  59353. DUK_WO_NORETURN(return;);
  59354. }
  59355. /* XXX: add flag to indicate whether caller cares about return value; this
  59356. * affects e.g. handling of assignment expressions. This change needs API
  59357. * changes elsewhere too.
  59358. */
  59359. DUK_LOCAL void duk__expr_led(duk_compiler_ctx *comp_ctx, duk_ivalue *left, duk_ivalue *res) {
  59360. duk_hthread *thr = comp_ctx->thr;
  59361. duk_token *tk;
  59362. duk_small_uint_t tok;
  59363. duk_uint32_t args; /* temp variable to pass constants and flags to shared code */
  59364. /*
  59365. * ctx->prev_token token to process with duk__expr_led()
  59366. * ctx->curr_token updated by caller
  59367. */
  59368. comp_ctx->curr_func.led_count++;
  59369. /* The token in the switch has already been eaten here */
  59370. tk = &comp_ctx->prev_token;
  59371. tok = tk->t;
  59372. DUK_DDD(DUK_DDDPRINT("duk__expr_led(), prev_token.t=%ld, allow_in=%ld, paren_level=%ld",
  59373. (long) tk->t, (long) comp_ctx->curr_func.allow_in, (long) comp_ctx->curr_func.paren_level));
  59374. /* XXX: default priority for infix operators is duk__expr_lbp(tok) -> get it here? */
  59375. switch (tok) {
  59376. /* PRIMARY EXPRESSIONS */
  59377. case DUK_TOK_PERIOD: {
  59378. /* Property access expressions are critical for correct LHS ordering,
  59379. * see comments in duk__expr()!
  59380. *
  59381. * A conservative approach would be to use duk__ivalue_totempconst()
  59382. * for 'left'. However, allowing a reg-bound variable seems safe here
  59383. * and is nice because "foo.bar" is a common expression. If the ivalue
  59384. * is used in an expression a GETPROP will occur before any changes to
  59385. * the base value can occur. If the ivalue is used as an assignment
  59386. * LHS, the assignment code will ensure the base value is safe from
  59387. * RHS mutation.
  59388. */
  59389. /* XXX: This now coerces an identifier into a GETVAR to a temp, which
  59390. * causes an extra LDREG in call setup. It's sufficient to coerce to a
  59391. * unary ivalue?
  59392. */
  59393. duk__ivalue_toplain(comp_ctx, left);
  59394. /* NB: must accept reserved words as property name */
  59395. if (comp_ctx->curr_token.t_nores != DUK_TOK_IDENTIFIER) {
  59396. DUK_ERROR_SYNTAX(thr, DUK_STR_EXPECTED_IDENTIFIER);
  59397. DUK_WO_NORETURN(return;);
  59398. }
  59399. res->t = DUK_IVAL_PROP;
  59400. duk__copy_ispec(comp_ctx, &left->x1, &res->x1); /* left.x1 -> res.x1 */
  59401. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  59402. duk_push_hstring(thr, comp_ctx->curr_token.str1);
  59403. duk_replace(thr, res->x2.valstack_idx);
  59404. res->x2.t = DUK_ISPEC_VALUE;
  59405. /* special RegExp literal handling after IdentifierName */
  59406. comp_ctx->curr_func.reject_regexp_in_adv = 1;
  59407. duk__advance(comp_ctx);
  59408. return;
  59409. }
  59410. case DUK_TOK_LBRACKET: {
  59411. /* Property access expressions are critical for correct LHS ordering,
  59412. * see comments in duk__expr()!
  59413. */
  59414. /* XXX: optimize temp reg use */
  59415. /* XXX: similar coercion issue as in DUK_TOK_PERIOD */
  59416. /* XXX: coerce to regs? it might be better for enumeration use, where the
  59417. * same PROP ivalue is used multiple times. Or perhaps coerce PROP further
  59418. * there?
  59419. */
  59420. /* XXX: for simple cases like x['y'] an unnecessary LDREG is
  59421. * emitted for the base value; could avoid it if we knew that
  59422. * the key expression is safe (e.g. just a single literal).
  59423. */
  59424. /* The 'left' value must not be a register bound variable
  59425. * because it may be mutated during the rest of the expression
  59426. * and E5.1 Section 11.2.1 specifies the order of evaluation
  59427. * so that the base value is evaluated first.
  59428. * See: test-bug-nested-prop-mutate.js.
  59429. */
  59430. duk__ivalue_totempconst(comp_ctx, left);
  59431. duk__expr_toplain(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression, ']' terminates */
  59432. duk__advance_expect(comp_ctx, DUK_TOK_RBRACKET);
  59433. res->t = DUK_IVAL_PROP;
  59434. duk__copy_ispec(comp_ctx, &res->x1, &res->x2); /* res.x1 -> res.x2 */
  59435. duk__copy_ispec(comp_ctx, &left->x1, &res->x1); /* left.x1 -> res.x1 */
  59436. return;
  59437. }
  59438. case DUK_TOK_LPAREN: {
  59439. /* function call */
  59440. duk_regconst_t reg_cs = DUK__ALLOCTEMPS(comp_ctx, 2);
  59441. duk_int_t nargs;
  59442. duk_small_uint_t call_op = DUK_OP_CALL0;
  59443. /* XXX: attempt to get the call result to "next temp" whenever
  59444. * possible to avoid unnecessary register shuffles.
  59445. */
  59446. /*
  59447. * Setup call: target and 'this' binding. Three cases:
  59448. *
  59449. * 1. Identifier base (e.g. "foo()")
  59450. * 2. Property base (e.g. "foo.bar()")
  59451. * 3. Register base (e.g. "foo()()"; i.e. when a return value is a function)
  59452. */
  59453. if (left->t == DUK_IVAL_VAR) {
  59454. duk_hstring *h_varname;
  59455. duk_regconst_t reg_varbind;
  59456. duk_regconst_t rc_varname;
  59457. DUK_DDD(DUK_DDDPRINT("function call with identifier base"));
  59458. h_varname = duk_known_hstring(thr, left->x1.valstack_idx);
  59459. if (h_varname == DUK_HTHREAD_STRING_EVAL(thr)) {
  59460. /* Potential direct eval call detected, flag the CALL
  59461. * so that a run-time "direct eval" check is made and
  59462. * special behavior may be triggered. Note that this
  59463. * does not prevent 'eval' from being register bound.
  59464. */
  59465. DUK_DDD(DUK_DDDPRINT("function call with identifier 'eval' "
  59466. "-> using EVALCALL, marking function "
  59467. "as may_direct_eval"));
  59468. call_op |= DUK_BC_CALL_FLAG_CALLED_AS_EVAL;
  59469. comp_ctx->curr_func.may_direct_eval = 1;
  59470. }
  59471. duk_dup(thr, left->x1.valstack_idx);
  59472. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  59473. duk__emit_a_bc(comp_ctx,
  59474. DUK_OP_CSREG | DUK__EMIT_FLAG_A_IS_SOURCE,
  59475. reg_varbind,
  59476. reg_cs + 0);
  59477. } else {
  59478. /* XXX: expand target register or constant field to
  59479. * reduce shuffling.
  59480. */
  59481. DUK_ASSERT(DUK__ISCONST(rc_varname));
  59482. duk__emit_a_b(comp_ctx,
  59483. DUK_OP_CSVAR | DUK__EMIT_FLAG_BC_REGCONST,
  59484. reg_cs + 0,
  59485. rc_varname);
  59486. }
  59487. } else if (left->t == DUK_IVAL_PROP) {
  59488. /* Call through a property lookup, E5 Section 11.2.3, step 6.a.i,
  59489. * E5 Section 10.4.3. There used to be a separate CSPROP opcode
  59490. * but a typical call setup took 3 opcodes (e.g. LDREG, LDCONST,
  59491. * CSPROP) and the same can be achieved with ordinary loads.
  59492. */
  59493. #if defined(DUK_USE_VERBOSE_ERRORS)
  59494. duk_regconst_t reg_key;
  59495. #endif
  59496. DUK_DDD(DUK_DDDPRINT("function call with property base"));
  59497. /* XXX: For Math.sin() this generates: LDCONST + LDREG +
  59498. * GETPROPC + call. The LDREG is unnecessary because LDCONST
  59499. * could be loaded directly into reg_cs + 1. This doesn't
  59500. * happen now because a variable cannot be in left->x1 of a
  59501. * DUK_IVAL_PROP. We could notice that left->x1 is a temp
  59502. * and reuse, but it would still be in the wrong position
  59503. * (reg_cs + 0 rather than reg_cs + 1).
  59504. */
  59505. duk__ispec_toforcedreg(comp_ctx, &left->x1, reg_cs + 1); /* base */
  59506. #if defined(DUK_USE_VERBOSE_ERRORS)
  59507. reg_key = duk__ispec_toregconst_raw(comp_ctx, &left->x2, -1, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  59508. duk__emit_a_b_c(comp_ctx,
  59509. DUK_OP_GETPROPC | DUK__EMIT_FLAG_BC_REGCONST,
  59510. reg_cs + 0,
  59511. reg_cs + 1,
  59512. reg_key);
  59513. #else
  59514. duk__ivalue_toforcedreg(comp_ctx, left, reg_cs + 0); /* base[key] */
  59515. #endif
  59516. } else {
  59517. DUK_DDD(DUK_DDDPRINT("function call with register base"));
  59518. duk__ivalue_toforcedreg(comp_ctx, left, reg_cs + 0);
  59519. #if 0
  59520. duk__emit_a_bc(comp_ctx,
  59521. DUK_OP_CSREG | DUK__EMIT_FLAG_A_IS_SOURCE,
  59522. reg_cs + 0,
  59523. reg_cs + 0); /* in-place setup */
  59524. #endif
  59525. /* Because of in-place setup, REGCS is equivalent to
  59526. * just this LDUNDEF.
  59527. */
  59528. duk__emit_bc(comp_ctx, DUK_OP_LDUNDEF, reg_cs + 1);
  59529. }
  59530. DUK__SETTEMP(comp_ctx, reg_cs + 2);
  59531. nargs = duk__parse_arguments(comp_ctx, res); /* parse args starting from "next temp" */
  59532. /* Tailcalls are handled by back-patching the already emitted opcode
  59533. * later in return statement parser.
  59534. */
  59535. duk__emit_a_bc(comp_ctx,
  59536. call_op,
  59537. (duk_regconst_t) nargs /*numargs*/,
  59538. reg_cs /*basereg*/);
  59539. DUK__SETTEMP(comp_ctx, reg_cs + 1); /* result in csreg */
  59540. duk__ivalue_regconst(res, reg_cs);
  59541. return;
  59542. }
  59543. /* POSTFIX EXPRESSION */
  59544. case DUK_TOK_INCREMENT: {
  59545. args = (DUK_OP_POSTINCP_RR << 16) + (DUK_OP_POSTINCR << 8) + 0;
  59546. goto postincdec;
  59547. }
  59548. case DUK_TOK_DECREMENT: {
  59549. args = (DUK_OP_POSTDECP_RR << 16) + (DUK_OP_POSTDECR << 8) + 0;
  59550. goto postincdec;
  59551. }
  59552. /* EXPONENTIATION EXPRESSION */
  59553. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  59554. case DUK_TOK_EXP: {
  59555. args = (DUK_OP_EXP << 8) + DUK__BP_EXPONENTIATION - 1; /* UnaryExpression */
  59556. goto binary;
  59557. }
  59558. #endif
  59559. /* MULTIPLICATIVE EXPRESSION */
  59560. case DUK_TOK_MUL: {
  59561. args = (DUK_OP_MUL << 8) + DUK__BP_MULTIPLICATIVE; /* ExponentiationExpression */
  59562. goto binary;
  59563. }
  59564. case DUK_TOK_DIV: {
  59565. args = (DUK_OP_DIV << 8) + DUK__BP_MULTIPLICATIVE; /* ExponentiationExpression */
  59566. goto binary;
  59567. }
  59568. case DUK_TOK_MOD: {
  59569. args = (DUK_OP_MOD << 8) + DUK__BP_MULTIPLICATIVE; /* ExponentiationExpression */
  59570. goto binary;
  59571. }
  59572. /* ADDITIVE EXPRESSION */
  59573. case DUK_TOK_ADD: {
  59574. args = (DUK_OP_ADD << 8) + DUK__BP_ADDITIVE; /* MultiplicativeExpression */
  59575. goto binary;
  59576. }
  59577. case DUK_TOK_SUB: {
  59578. args = (DUK_OP_SUB << 8) + DUK__BP_ADDITIVE; /* MultiplicativeExpression */
  59579. goto binary;
  59580. }
  59581. /* SHIFT EXPRESSION */
  59582. case DUK_TOK_ALSHIFT: {
  59583. /* << */
  59584. args = (DUK_OP_BASL << 8) + DUK__BP_SHIFT;
  59585. goto binary;
  59586. }
  59587. case DUK_TOK_ARSHIFT: {
  59588. /* >> */
  59589. args = (DUK_OP_BASR << 8) + DUK__BP_SHIFT;
  59590. goto binary;
  59591. }
  59592. case DUK_TOK_RSHIFT: {
  59593. /* >>> */
  59594. args = (DUK_OP_BLSR << 8) + DUK__BP_SHIFT;
  59595. goto binary;
  59596. }
  59597. /* RELATIONAL EXPRESSION */
  59598. case DUK_TOK_LT: {
  59599. /* < */
  59600. args = (DUK_OP_LT << 8) + DUK__BP_RELATIONAL;
  59601. goto binary;
  59602. }
  59603. case DUK_TOK_GT: {
  59604. args = (DUK_OP_GT << 8) + DUK__BP_RELATIONAL;
  59605. goto binary;
  59606. }
  59607. case DUK_TOK_LE: {
  59608. args = (DUK_OP_LE << 8) + DUK__BP_RELATIONAL;
  59609. goto binary;
  59610. }
  59611. case DUK_TOK_GE: {
  59612. args = (DUK_OP_GE << 8) + DUK__BP_RELATIONAL;
  59613. goto binary;
  59614. }
  59615. case DUK_TOK_INSTANCEOF: {
  59616. args = (DUK_OP_INSTOF << 8) + DUK__BP_RELATIONAL;
  59617. goto binary;
  59618. }
  59619. case DUK_TOK_IN: {
  59620. args = (DUK_OP_IN << 8) + DUK__BP_RELATIONAL;
  59621. goto binary;
  59622. }
  59623. /* EQUALITY EXPRESSION */
  59624. case DUK_TOK_EQ: {
  59625. args = (DUK_OP_EQ << 8) + DUK__BP_EQUALITY;
  59626. goto binary;
  59627. }
  59628. case DUK_TOK_NEQ: {
  59629. args = (DUK_OP_NEQ << 8) + DUK__BP_EQUALITY;
  59630. goto binary;
  59631. }
  59632. case DUK_TOK_SEQ: {
  59633. args = (DUK_OP_SEQ << 8) + DUK__BP_EQUALITY;
  59634. goto binary;
  59635. }
  59636. case DUK_TOK_SNEQ: {
  59637. args = (DUK_OP_SNEQ << 8) + DUK__BP_EQUALITY;
  59638. goto binary;
  59639. }
  59640. /* BITWISE EXPRESSIONS */
  59641. case DUK_TOK_BAND: {
  59642. args = (DUK_OP_BAND << 8) + DUK__BP_BAND;
  59643. goto binary;
  59644. }
  59645. case DUK_TOK_BXOR: {
  59646. args = (DUK_OP_BXOR << 8) + DUK__BP_BXOR;
  59647. goto binary;
  59648. }
  59649. case DUK_TOK_BOR: {
  59650. args = (DUK_OP_BOR << 8) + DUK__BP_BOR;
  59651. goto binary;
  59652. }
  59653. /* LOGICAL EXPRESSIONS */
  59654. case DUK_TOK_LAND: {
  59655. /* syntactically left-associative but parsed as right-associative */
  59656. args = (1 << 8) + DUK__BP_LAND - 1;
  59657. goto binary_logical;
  59658. }
  59659. case DUK_TOK_LOR: {
  59660. /* syntactically left-associative but parsed as right-associative */
  59661. args = (0 << 8) + DUK__BP_LOR - 1;
  59662. goto binary_logical;
  59663. }
  59664. /* CONDITIONAL EXPRESSION */
  59665. case DUK_TOK_QUESTION: {
  59666. /* XXX: common reg allocation need is to reuse a sub-expression's temp reg,
  59667. * but only if it really is a temp. Nothing fancy here now.
  59668. */
  59669. duk_regconst_t reg_temp;
  59670. duk_int_t pc_jump1;
  59671. duk_int_t pc_jump2;
  59672. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59673. duk__ivalue_toforcedreg(comp_ctx, left, reg_temp);
  59674. duk__emit_if_true_skip(comp_ctx, reg_temp);
  59675. pc_jump1 = duk__emit_jump_empty(comp_ctx); /* jump to false */
  59676. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/); /* AssignmentExpression */
  59677. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  59678. pc_jump2 = duk__emit_jump_empty(comp_ctx); /* jump to end */
  59679. duk__patch_jump_here(comp_ctx, pc_jump1);
  59680. duk__expr_toforcedreg(comp_ctx, res, DUK__BP_COMMA /*rbp_flags*/, reg_temp /*forced_reg*/); /* AssignmentExpression */
  59681. duk__patch_jump_here(comp_ctx, pc_jump2);
  59682. DUK__SETTEMP(comp_ctx, reg_temp + 1);
  59683. duk__ivalue_regconst(res, reg_temp);
  59684. return;
  59685. }
  59686. /* ASSIGNMENT EXPRESSION */
  59687. case DUK_TOK_EQUALSIGN: {
  59688. /*
  59689. * Assignments are right associative, allows e.g.
  59690. * a = 5;
  59691. * a += b = 9; // same as a += (b = 9)
  59692. * -> expression value 14, a = 14, b = 9
  59693. *
  59694. * Right associativiness is reflected in the BP for recursion,
  59695. * "-1" ensures assignment operations are allowed.
  59696. *
  59697. * XXX: just use DUK__BP_COMMA (i.e. no need for 2-step bp levels)?
  59698. */
  59699. args = (DUK_OP_NONE << 8) + DUK__BP_ASSIGNMENT - 1; /* DUK_OP_NONE marks a 'plain' assignment */
  59700. goto assign;
  59701. }
  59702. case DUK_TOK_ADD_EQ: {
  59703. /* right associative */
  59704. args = (DUK_OP_ADD << 8) + DUK__BP_ASSIGNMENT - 1;
  59705. goto assign;
  59706. }
  59707. case DUK_TOK_SUB_EQ: {
  59708. /* right associative */
  59709. args = (DUK_OP_SUB << 8) + DUK__BP_ASSIGNMENT - 1;
  59710. goto assign;
  59711. }
  59712. case DUK_TOK_MUL_EQ: {
  59713. /* right associative */
  59714. args = (DUK_OP_MUL << 8) + DUK__BP_ASSIGNMENT - 1;
  59715. goto assign;
  59716. }
  59717. case DUK_TOK_DIV_EQ: {
  59718. /* right associative */
  59719. args = (DUK_OP_DIV << 8) + DUK__BP_ASSIGNMENT - 1;
  59720. goto assign;
  59721. }
  59722. case DUK_TOK_MOD_EQ: {
  59723. /* right associative */
  59724. args = (DUK_OP_MOD << 8) + DUK__BP_ASSIGNMENT - 1;
  59725. goto assign;
  59726. }
  59727. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  59728. case DUK_TOK_EXP_EQ: {
  59729. /* right associative */
  59730. args = (DUK_OP_EXP << 8) + DUK__BP_ASSIGNMENT - 1;
  59731. goto assign;
  59732. }
  59733. #endif
  59734. case DUK_TOK_ALSHIFT_EQ: {
  59735. /* right associative */
  59736. args = (DUK_OP_BASL << 8) + DUK__BP_ASSIGNMENT - 1;
  59737. goto assign;
  59738. }
  59739. case DUK_TOK_ARSHIFT_EQ: {
  59740. /* right associative */
  59741. args = (DUK_OP_BASR << 8) + DUK__BP_ASSIGNMENT - 1;
  59742. goto assign;
  59743. }
  59744. case DUK_TOK_RSHIFT_EQ: {
  59745. /* right associative */
  59746. args = (DUK_OP_BLSR << 8) + DUK__BP_ASSIGNMENT - 1;
  59747. goto assign;
  59748. }
  59749. case DUK_TOK_BAND_EQ: {
  59750. /* right associative */
  59751. args = (DUK_OP_BAND << 8) + DUK__BP_ASSIGNMENT - 1;
  59752. goto assign;
  59753. }
  59754. case DUK_TOK_BOR_EQ: {
  59755. /* right associative */
  59756. args = (DUK_OP_BOR << 8) + DUK__BP_ASSIGNMENT - 1;
  59757. goto assign;
  59758. }
  59759. case DUK_TOK_BXOR_EQ: {
  59760. /* right associative */
  59761. args = (DUK_OP_BXOR << 8) + DUK__BP_ASSIGNMENT - 1;
  59762. goto assign;
  59763. }
  59764. /* COMMA */
  59765. case DUK_TOK_COMMA: {
  59766. /* right associative */
  59767. duk__ivalue_toplain_ignore(comp_ctx, left); /* need side effects, not value */
  59768. duk__expr_toplain(comp_ctx, res, DUK__BP_COMMA - 1 /*rbp_flags*/);
  59769. /* return 'res' (of right part) as our result */
  59770. return;
  59771. }
  59772. default: {
  59773. break;
  59774. }
  59775. }
  59776. DUK_D(DUK_DPRINT("parse error: unexpected token: %ld", (long) tok));
  59777. DUK_ERROR_SYNTAX(thr, DUK_STR_PARSE_ERROR);
  59778. DUK_WO_NORETURN(return;);
  59779. #if 0
  59780. /* XXX: shared handling for 'duk__expr_lhs'? */
  59781. if (comp_ctx->curr_func.paren_level == 0 && XXX) {
  59782. comp_ctx->curr_func.duk__expr_lhs = 0;
  59783. }
  59784. #endif
  59785. binary:
  59786. /*
  59787. * Shared handling of binary operations
  59788. *
  59789. * args = (opcode << 8) + rbp
  59790. */
  59791. {
  59792. duk__ivalue_toplain(comp_ctx, left);
  59793. duk__expr_toplain(comp_ctx, res, args & 0xff /*rbp_flags*/);
  59794. /* combine left->x1 and res->x1 (right->x1, really) -> (left->x1 OP res->x1) */
  59795. DUK_ASSERT(left->t == DUK_IVAL_PLAIN);
  59796. DUK_ASSERT(res->t == DUK_IVAL_PLAIN);
  59797. res->t = DUK_IVAL_ARITH;
  59798. res->op = (args >> 8) & 0xff;
  59799. res->x2.t = res->x1.t;
  59800. res->x2.regconst = res->x1.regconst;
  59801. duk_copy(thr, res->x1.valstack_idx, res->x2.valstack_idx);
  59802. res->x1.t = left->x1.t;
  59803. res->x1.regconst = left->x1.regconst;
  59804. duk_copy(thr, left->x1.valstack_idx, res->x1.valstack_idx);
  59805. DUK_DDD(DUK_DDDPRINT("binary op, res: t=%ld, x1.t=%ld, x1.regconst=0x%08lx, x2.t=%ld, x2.regconst=0x%08lx",
  59806. (long) res->t, (long) res->x1.t, (unsigned long) res->x1.regconst, (long) res->x2.t, (unsigned long) res->x2.regconst));
  59807. return;
  59808. }
  59809. binary_logical:
  59810. /*
  59811. * Shared handling for logical AND and logical OR.
  59812. *
  59813. * args = (truthval << 8) + rbp
  59814. *
  59815. * Truthval determines when to skip right-hand-side.
  59816. * For logical AND truthval=1, for logical OR truthval=0.
  59817. *
  59818. * See doc/compiler.rst for discussion on compiling logical
  59819. * AND and OR expressions. The approach here is very simplistic,
  59820. * generating extra jumps and multiple evaluations of truth values,
  59821. * but generates code on-the-fly with only local back-patching.
  59822. *
  59823. * Both logical AND and OR are syntactically left-associated.
  59824. * However, logical ANDs are compiled as right associative
  59825. * expressions, i.e. "A && B && C" as "A && (B && C)", to allow
  59826. * skip jumps to skip over the entire tail. Similarly for logical OR.
  59827. */
  59828. {
  59829. duk_regconst_t reg_temp;
  59830. duk_int_t pc_jump;
  59831. duk_small_uint_t args_truthval = args >> 8;
  59832. duk_small_uint_t args_rbp = args & 0xff;
  59833. /* XXX: unoptimal use of temps, resetting */
  59834. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59835. duk__ivalue_toforcedreg(comp_ctx, left, reg_temp);
  59836. DUK_ASSERT(DUK__ISREG(reg_temp));
  59837. duk__emit_bc(comp_ctx,
  59838. (args_truthval ? DUK_OP_IFTRUE_R : DUK_OP_IFFALSE_R),
  59839. reg_temp); /* skip jump conditionally */
  59840. pc_jump = duk__emit_jump_empty(comp_ctx);
  59841. duk__expr_toforcedreg(comp_ctx, res, args_rbp /*rbp_flags*/, reg_temp /*forced_reg*/);
  59842. duk__patch_jump_here(comp_ctx, pc_jump);
  59843. duk__ivalue_regconst(res, reg_temp);
  59844. return;
  59845. }
  59846. assign:
  59847. /*
  59848. * Shared assignment expression handling
  59849. *
  59850. * args = (opcode << 8) + rbp
  59851. *
  59852. * If 'opcode' is DUK_OP_NONE, plain assignment without arithmetic.
  59853. * Syntactically valid left-hand-side forms which are not accepted as
  59854. * left-hand-side values (e.g. as in "f() = 1") must NOT cause a
  59855. * SyntaxError, but rather a run-time ReferenceError.
  59856. *
  59857. * When evaluating X <op>= Y, the LHS (X) is conceptually evaluated
  59858. * to a temporary first. The RHS is then evaluated. Finally, the
  59859. * <op> is applied to the initial value of RHS (not the value after
  59860. * RHS evaluation), and written to X. Doing so concretely generates
  59861. * inefficient code so we'd like to avoid the temporary when possible.
  59862. * See: https://github.com/svaarala/duktape/pull/992.
  59863. *
  59864. * The expression value (final LHS value, written to RHS) is
  59865. * conceptually copied into a fresh temporary so that it won't
  59866. * change even if the LHS/RHS values change in outer expressions.
  59867. * For example, it'd be generally incorrect for the expression value
  59868. * to be the RHS register binding, unless there's a guarantee that it
  59869. * won't change during further expression evaluation. Using the
  59870. * temporary concretely produces inefficient bytecode, so we try to
  59871. * avoid the extra temporary for some known-to-be-safe cases.
  59872. * Currently the only safe case we detect is a "top level assignment",
  59873. * for example "x = y + z;", where the assignment expression value is
  59874. * ignored.
  59875. * See: test-dev-assign-expr.js and test-bug-assign-mutate-gh381.js.
  59876. */
  59877. {
  59878. duk_small_uint_t args_op = args >> 8;
  59879. duk_small_uint_t args_rbp = args & 0xff;
  59880. duk_bool_t toplevel_assign;
  59881. /* XXX: here we need to know if 'left' is left-hand-side compatible.
  59882. * That information is no longer available from current expr parsing
  59883. * state; it would need to be carried into the 'left' ivalue or by
  59884. * some other means.
  59885. */
  59886. /* A top-level assignment is e.g. "x = y;". For these it's safe
  59887. * to use the RHS as-is as the expression value, even if the RHS
  59888. * is a reg-bound identifier. The RHS ('res') is right associative
  59889. * so it has consumed all other assignment level operations; the
  59890. * only relevant lower binding power construct is comma operator
  59891. * which will ignore the expression value provided here. Usually
  59892. * the top level assignment expression value is ignored, but it
  59893. * is relevant for e.g. eval code.
  59894. */
  59895. toplevel_assign = (comp_ctx->curr_func.nud_count == 1 && /* one token before */
  59896. comp_ctx->curr_func.led_count == 1); /* one operator (= assign) */
  59897. DUK_DDD(DUK_DDDPRINT("assignment: nud_count=%ld, led_count=%ld, toplevel_assign=%ld",
  59898. (long) comp_ctx->curr_func.nud_count,
  59899. (long) comp_ctx->curr_func.led_count,
  59900. (long) toplevel_assign));
  59901. if (left->t == DUK_IVAL_VAR) {
  59902. duk_hstring *h_varname;
  59903. duk_regconst_t reg_varbind;
  59904. duk_regconst_t rc_varname;
  59905. DUK_ASSERT(left->x1.t == DUK_ISPEC_VALUE); /* LHS is already side effect free */
  59906. h_varname = duk_known_hstring(thr, left->x1.valstack_idx);
  59907. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  59908. /* E5 Section 11.13.1 (and others for other assignments), step 4. */
  59909. goto syntax_error_lvalue;
  59910. }
  59911. duk_dup(thr, left->x1.valstack_idx);
  59912. (void) duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname);
  59913. if (args_op == DUK_OP_NONE) {
  59914. duk__expr(comp_ctx, res, args_rbp /*rbp_flags*/);
  59915. if (toplevel_assign) {
  59916. /* Any 'res' will do. */
  59917. DUK_DDD(DUK_DDDPRINT("plain assignment, toplevel assign, use as is"));
  59918. } else {
  59919. /* 'res' must be a plain ivalue, and not register-bound variable. */
  59920. DUK_DDD(DUK_DDDPRINT("plain assignment, not toplevel assign, ensure not a reg-bound identifier"));
  59921. if (res->t != DUK_IVAL_PLAIN || (res->x1.t == DUK_ISPEC_REGCONST &&
  59922. DUK__ISREG_NOTTEMP(comp_ctx, res->x1.regconst))) {
  59923. duk__ivalue_totempconst(comp_ctx, res);
  59924. }
  59925. }
  59926. } else {
  59927. /* For X <op>= Y we need to evaluate the pre-op
  59928. * value of X before evaluating the RHS: the RHS
  59929. * can change X, but when we do <op> we must use
  59930. * the pre-op value.
  59931. */
  59932. duk_regconst_t reg_temp;
  59933. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59934. if (reg_varbind >= 0) {
  59935. duk_regconst_t reg_res;
  59936. duk_regconst_t reg_src;
  59937. duk_int_t pc_temp_load;
  59938. duk_int_t pc_before_rhs;
  59939. duk_int_t pc_after_rhs;
  59940. if (toplevel_assign) {
  59941. /* 'reg_varbind' is the operation result and can also
  59942. * become the expression value for top level assignments
  59943. * such as: "var x; x += y;".
  59944. */
  59945. DUK_DD(DUK_DDPRINT("<op>= expression is top level, write directly to reg_varbind"));
  59946. reg_res = reg_varbind;
  59947. } else {
  59948. /* Not safe to use 'reg_varbind' as assignment expression
  59949. * value, so go through a temp.
  59950. */
  59951. DUK_DD(DUK_DDPRINT("<op>= expression is not top level, write to reg_temp"));
  59952. reg_res = reg_temp; /* reg_res should be smallest possible */
  59953. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  59954. }
  59955. /* Try to optimize X <op>= Y for reg-bound
  59956. * variables. Detect side-effect free RHS
  59957. * narrowly by seeing whether it emits code.
  59958. * If not, rewind the code emitter and overwrite
  59959. * the unnecessary temp reg load.
  59960. */
  59961. pc_temp_load = duk__get_current_pc(comp_ctx);
  59962. duk__emit_a_bc(comp_ctx,
  59963. DUK_OP_LDREG,
  59964. reg_temp,
  59965. reg_varbind);
  59966. pc_before_rhs = duk__get_current_pc(comp_ctx);
  59967. duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  59968. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  59969. pc_after_rhs = duk__get_current_pc(comp_ctx);
  59970. DUK_DD(DUK_DDPRINT("pc_temp_load=%ld, pc_before_rhs=%ld, pc_after_rhs=%ld",
  59971. (long) pc_temp_load, (long) pc_before_rhs,
  59972. (long) pc_after_rhs));
  59973. if (pc_after_rhs == pc_before_rhs) {
  59974. /* Note: if the reg_temp load generated shuffling
  59975. * instructions, we may need to rewind more than
  59976. * one instruction, so use explicit PC computation.
  59977. */
  59978. DUK_DD(DUK_DDPRINT("rhs is side effect free, rewind and avoid unnecessary temp for reg-based <op>="));
  59979. DUK_BW_ADD_PTR(comp_ctx->thr, &comp_ctx->curr_func.bw_code, (pc_temp_load - pc_before_rhs) * (duk_int_t) sizeof(duk_compiler_instr));
  59980. reg_src = reg_varbind;
  59981. } else {
  59982. DUK_DD(DUK_DDPRINT("rhs evaluation emitted code, not sure if rhs is side effect free; use temp reg for LHS"));
  59983. reg_src = reg_temp;
  59984. }
  59985. duk__emit_a_b_c(comp_ctx,
  59986. args_op | DUK__EMIT_FLAG_BC_REGCONST,
  59987. reg_res,
  59988. reg_src,
  59989. res->x1.regconst);
  59990. res->x1.regconst = reg_res;
  59991. /* Ensure compact use of temps. */
  59992. if (DUK__ISREG_TEMP(comp_ctx, reg_res)) {
  59993. DUK__SETTEMP(comp_ctx, reg_res + 1);
  59994. }
  59995. } else {
  59996. /* When LHS is not register bound, always go through a
  59997. * temporary. No optimization for top level assignment.
  59998. */
  59999. duk__emit_a_bc(comp_ctx,
  60000. DUK_OP_GETVAR,
  60001. reg_temp,
  60002. rc_varname);
  60003. duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  60004. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  60005. duk__emit_a_b_c(comp_ctx,
  60006. args_op | DUK__EMIT_FLAG_BC_REGCONST,
  60007. reg_temp,
  60008. reg_temp,
  60009. res->x1.regconst);
  60010. res->x1.regconst = reg_temp;
  60011. }
  60012. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  60013. }
  60014. /* At this point 'res' holds the potential expression value.
  60015. * It can be basically any ivalue here, including a reg-bound
  60016. * identifier (if code above deems it safe) or a unary/binary
  60017. * operation. Operations must be resolved to a side effect free
  60018. * plain value, and the side effects must happen exactly once.
  60019. */
  60020. if (reg_varbind >= 0) {
  60021. if (res->t != DUK_IVAL_PLAIN) {
  60022. /* Resolve 'res' directly into the LHS binding, and use
  60023. * that as the expression value if safe. If not safe,
  60024. * resolve to a temp/const and copy to LHS.
  60025. */
  60026. if (toplevel_assign) {
  60027. duk__ivalue_toforcedreg(comp_ctx, res, (duk_int_t) reg_varbind);
  60028. } else {
  60029. duk__ivalue_totempconst(comp_ctx, res);
  60030. duk__copy_ivalue(comp_ctx, res, left); /* use 'left' as a temp */
  60031. duk__ivalue_toforcedreg(comp_ctx, left, (duk_int_t) reg_varbind);
  60032. }
  60033. } else {
  60034. /* Use 'res' as the expression value (it's side effect
  60035. * free and may be a plain value, a register, or a
  60036. * constant) and write it to the LHS binding too.
  60037. */
  60038. duk__copy_ivalue(comp_ctx, res, left); /* use 'left' as a temp */
  60039. duk__ivalue_toforcedreg(comp_ctx, left, (duk_int_t) reg_varbind);
  60040. }
  60041. } else {
  60042. /* Only a reg fits into 'A' so coerce 'res' into a register
  60043. * for PUTVAR.
  60044. *
  60045. * XXX: here the current A/B/C split is suboptimal: we could
  60046. * just use 9 bits for reg_res (and support constants) and 17
  60047. * instead of 18 bits for the varname const index.
  60048. */
  60049. duk__ivalue_toreg(comp_ctx, res);
  60050. duk__emit_a_bc(comp_ctx,
  60051. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  60052. res->x1.regconst,
  60053. rc_varname);
  60054. }
  60055. /* 'res' contains expression value */
  60056. } else if (left->t == DUK_IVAL_PROP) {
  60057. /* E5 Section 11.13.1 (and others) step 4 never matches for prop writes -> no check */
  60058. duk_regconst_t reg_obj;
  60059. duk_regconst_t rc_key;
  60060. duk_regconst_t rc_res;
  60061. duk_regconst_t reg_temp;
  60062. /* Property access expressions ('a[b]') are critical to correct
  60063. * LHS evaluation ordering, see test-dev-assign-eval-order*.js.
  60064. * We must make sure that the LHS target slot (base object and
  60065. * key) don't change during RHS evaluation. The only concrete
  60066. * problem is a register reference to a variable-bound register
  60067. * (i.e., non-temp). Require temp regs for both key and base.
  60068. *
  60069. * Don't allow a constant for the object (even for a number
  60070. * etc), as it goes into the 'A' field of the opcode.
  60071. */
  60072. reg_obj = duk__ispec_toregconst_raw(comp_ctx,
  60073. &left->x1,
  60074. -1 /*forced_reg*/,
  60075. DUK__IVAL_FLAG_REQUIRE_TEMP /*flags*/);
  60076. rc_key = duk__ispec_toregconst_raw(comp_ctx,
  60077. &left->x2,
  60078. -1 /*forced_reg*/,
  60079. DUK__IVAL_FLAG_REQUIRE_TEMP | DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  60080. /* Evaluate RHS only when LHS is safe. */
  60081. if (args_op == DUK_OP_NONE) {
  60082. duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  60083. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  60084. rc_res = res->x1.regconst;
  60085. } else {
  60086. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  60087. duk__emit_a_b_c(comp_ctx,
  60088. DUK_OP_GETPROP | DUK__EMIT_FLAG_BC_REGCONST,
  60089. reg_temp,
  60090. reg_obj,
  60091. rc_key);
  60092. duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  60093. DUK_ASSERT(res->t == DUK_IVAL_PLAIN && res->x1.t == DUK_ISPEC_REGCONST);
  60094. duk__emit_a_b_c(comp_ctx,
  60095. args_op | DUK__EMIT_FLAG_BC_REGCONST,
  60096. reg_temp,
  60097. reg_temp,
  60098. res->x1.regconst);
  60099. rc_res = reg_temp;
  60100. }
  60101. duk__emit_a_b_c(comp_ctx,
  60102. DUK_OP_PUTPROP | DUK__EMIT_FLAG_A_IS_SOURCE | DUK__EMIT_FLAG_BC_REGCONST,
  60103. reg_obj,
  60104. rc_key,
  60105. rc_res);
  60106. duk__ivalue_regconst(res, rc_res);
  60107. } else {
  60108. /* No support for lvalues returned from new or function call expressions.
  60109. * However, these must NOT cause compile-time SyntaxErrors, but run-time
  60110. * ReferenceErrors. Both left and right sides of the assignment must be
  60111. * evaluated before throwing a ReferenceError. For instance:
  60112. *
  60113. * f() = g();
  60114. *
  60115. * must result in f() being evaluated, then g() being evaluated, and
  60116. * finally, a ReferenceError being thrown. See E5 Section 11.13.1.
  60117. */
  60118. duk_regconst_t rc_res;
  60119. /* First evaluate LHS fully to ensure all side effects are out. */
  60120. duk__ivalue_toplain_ignore(comp_ctx, left);
  60121. /* Then evaluate RHS fully (its value becomes the expression value too).
  60122. * Technically we'd need the side effect safety check here too, but because
  60123. * we always throw using INVLHS the result doesn't matter.
  60124. */
  60125. rc_res = duk__expr_toregconst(comp_ctx, res, args_rbp /*rbp_flags*/);
  60126. duk__emit_op_only(comp_ctx, DUK_OP_INVLHS);
  60127. duk__ivalue_regconst(res, rc_res);
  60128. }
  60129. return;
  60130. }
  60131. postincdec:
  60132. {
  60133. /*
  60134. * Post-increment/decrement will return the original value as its
  60135. * result value. However, even that value will be coerced using
  60136. * ToNumber() which is quite awkward. Specific bytecode opcodes
  60137. * are used to handle these semantics.
  60138. *
  60139. * Note that post increment/decrement has a "no LineTerminator here"
  60140. * restriction. This is handled by duk__expr_lbp(), which forcibly terminates
  60141. * the previous expression if a LineTerminator occurs before '++'/'--'.
  60142. */
  60143. duk_regconst_t reg_res;
  60144. duk_small_uint_t args_op1 = (args >> 8) & 0xff; /* DUK_OP_POSTINCR/DUK_OP_POSTDECR */
  60145. duk_small_uint_t args_op2 = args >> 16; /* DUK_OP_POSTINCP_RR/DUK_OP_POSTDECP_RR */
  60146. /* Specific assumptions for opcode numbering. */
  60147. DUK_ASSERT(DUK_OP_POSTINCR + 4 == DUK_OP_POSTINCV);
  60148. DUK_ASSERT(DUK_OP_POSTDECR + 4 == DUK_OP_POSTDECV);
  60149. reg_res = DUK__ALLOCTEMP(comp_ctx);
  60150. if (left->t == DUK_IVAL_VAR) {
  60151. duk_hstring *h_varname;
  60152. duk_regconst_t reg_varbind;
  60153. duk_regconst_t rc_varname;
  60154. h_varname = duk_known_hstring(thr, left->x1.valstack_idx);
  60155. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  60156. goto syntax_error;
  60157. }
  60158. duk_dup(thr, left->x1.valstack_idx);
  60159. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  60160. duk__emit_a_bc(comp_ctx,
  60161. args_op1, /* e.g. DUK_OP_POSTINCR */
  60162. reg_res,
  60163. reg_varbind);
  60164. } else {
  60165. duk__emit_a_bc(comp_ctx,
  60166. args_op1 + 4, /* e.g. DUK_OP_POSTINCV */
  60167. reg_res,
  60168. rc_varname);
  60169. }
  60170. DUK_DDD(DUK_DDDPRINT("postincdec to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  60171. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  60172. } else if (left->t == DUK_IVAL_PROP) {
  60173. duk_regconst_t reg_obj; /* allocate to reg only (not const) */
  60174. duk_regconst_t rc_key;
  60175. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &left->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  60176. rc_key = duk__ispec_toregconst_raw(comp_ctx, &left->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  60177. duk__emit_a_b_c(comp_ctx,
  60178. args_op2 | DUK__EMIT_FLAG_BC_REGCONST, /* e.g. DUK_OP_POSTINCP */
  60179. reg_res,
  60180. reg_obj,
  60181. rc_key);
  60182. } else {
  60183. /* Technically return value is not needed because INVLHS will
  60184. * unconditially throw a ReferenceError. Coercion is necessary
  60185. * for proper semantics (consider ToNumber() called for an object).
  60186. * Use DUK_OP_UNP with a dummy register to get ToNumber().
  60187. */
  60188. duk__ivalue_toforcedreg(comp_ctx, left, reg_res);
  60189. duk__emit_bc(comp_ctx,
  60190. DUK_OP_UNP,
  60191. reg_res); /* for side effects, result ignored */
  60192. duk__emit_op_only(comp_ctx,
  60193. DUK_OP_INVLHS);
  60194. }
  60195. DUK__SETTEMP(comp_ctx, reg_res + 1);
  60196. duk__ivalue_regconst(res, reg_res);
  60197. return;
  60198. }
  60199. syntax_error:
  60200. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_EXPRESSION);
  60201. DUK_WO_NORETURN(return;);
  60202. syntax_error_lvalue:
  60203. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_LVALUE);
  60204. DUK_WO_NORETURN(return;);
  60205. }
  60206. DUK_LOCAL duk_small_uint_t duk__expr_lbp(duk_compiler_ctx *comp_ctx) {
  60207. duk_small_uint_t tok = comp_ctx->curr_token.t;
  60208. DUK_ASSERT_DISABLE(tok >= DUK_TOK_MINVAL); /* unsigned */
  60209. DUK_ASSERT(tok <= DUK_TOK_MAXVAL);
  60210. DUK_ASSERT(sizeof(duk__token_lbp) == DUK_TOK_MAXVAL + 1);
  60211. /* XXX: integrate support for this into led() instead?
  60212. * Similar issue as post-increment/post-decrement.
  60213. */
  60214. /* prevent duk__expr_led() by using a binding power less than anything valid */
  60215. if (tok == DUK_TOK_IN && !comp_ctx->curr_func.allow_in) {
  60216. return 0;
  60217. }
  60218. if ((tok == DUK_TOK_DECREMENT || tok == DUK_TOK_INCREMENT) &&
  60219. (comp_ctx->curr_token.lineterm)) {
  60220. /* '++' or '--' in a post-increment/decrement position,
  60221. * and a LineTerminator occurs between the operator and
  60222. * the preceding expression. Force the previous expr
  60223. * to terminate, in effect treating e.g. "a,b\n++" as
  60224. * "a,b;++" (= SyntaxError).
  60225. */
  60226. return 0;
  60227. }
  60228. return DUK__TOKEN_LBP_GET_BP(duk__token_lbp[tok]); /* format is bit packed */
  60229. }
  60230. /*
  60231. * Expression parsing.
  60232. *
  60233. * Upon entry to 'expr' and its variants, 'curr_tok' is assumed to be the
  60234. * first token of the expression. Upon exit, 'curr_tok' will be the first
  60235. * token not part of the expression (e.g. semicolon terminating an expression
  60236. * statement).
  60237. */
  60238. #define DUK__EXPR_RBP_MASK 0xff
  60239. #define DUK__EXPR_FLAG_REJECT_IN (1 << 8) /* reject 'in' token (used for for-in) */
  60240. #define DUK__EXPR_FLAG_ALLOW_EMPTY (1 << 9) /* allow empty expression */
  60241. #define DUK__EXPR_FLAG_REQUIRE_INIT (1 << 10) /* require initializer for var/const */
  60242. /* main expression parser function */
  60243. DUK_LOCAL void duk__expr(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60244. duk_hthread *thr = comp_ctx->thr;
  60245. duk_ivalue tmp_alloc; /* 'res' is used for "left", and 'tmp' for "right" */
  60246. duk_ivalue *tmp = &tmp_alloc;
  60247. duk_small_uint_t rbp;
  60248. DUK__RECURSION_INCREASE(comp_ctx, thr);
  60249. duk_require_stack(thr, DUK__PARSE_EXPR_SLOTS);
  60250. /* filter out flags from exprtop rbp_flags here to save space */
  60251. rbp = rbp_flags & DUK__EXPR_RBP_MASK;
  60252. DUK_DDD(DUK_DDDPRINT("duk__expr(), rbp_flags=%ld, rbp=%ld, allow_in=%ld, paren_level=%ld",
  60253. (long) rbp_flags, (long) rbp, (long) comp_ctx->curr_func.allow_in,
  60254. (long) comp_ctx->curr_func.paren_level));
  60255. duk_memzero(&tmp_alloc, sizeof(tmp_alloc));
  60256. tmp->x1.valstack_idx = duk_get_top(thr);
  60257. tmp->x2.valstack_idx = tmp->x1.valstack_idx + 1;
  60258. duk_push_undefined(thr);
  60259. duk_push_undefined(thr);
  60260. /* XXX: where to release temp regs in intermediate expressions?
  60261. * e.g. 1+2+3 -> don't inflate temp register count when parsing this.
  60262. * that particular expression temp regs can be forced here.
  60263. */
  60264. /* XXX: increase ctx->expr_tokens here for every consumed token
  60265. * (this would be a nice statistic)?
  60266. */
  60267. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  60268. /* XXX: possibly incorrect handling of empty expression */
  60269. DUK_DDD(DUK_DDDPRINT("empty expression"));
  60270. if (!(rbp_flags & DUK__EXPR_FLAG_ALLOW_EMPTY)) {
  60271. DUK_ERROR_SYNTAX(thr, DUK_STR_EMPTY_EXPR_NOT_ALLOWED);
  60272. DUK_WO_NORETURN(return;);
  60273. }
  60274. duk_push_undefined(thr);
  60275. duk__ivalue_plain_fromstack(comp_ctx, res);
  60276. goto cleanup;
  60277. }
  60278. duk__advance(comp_ctx);
  60279. duk__expr_nud(comp_ctx, res); /* reuse 'res' as 'left' */
  60280. while (rbp < duk__expr_lbp(comp_ctx)) {
  60281. duk__advance(comp_ctx);
  60282. duk__expr_led(comp_ctx, res, tmp);
  60283. duk__copy_ivalue(comp_ctx, tmp, res); /* tmp -> res */
  60284. }
  60285. cleanup:
  60286. /* final result is already in 'res' */
  60287. duk_pop_2(thr);
  60288. DUK__RECURSION_DECREASE(comp_ctx, thr);
  60289. }
  60290. DUK_LOCAL void duk__exprtop(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60291. duk_hthread *thr = comp_ctx->thr;
  60292. /* Note: these variables must reside in 'curr_func' instead of the global
  60293. * context: when parsing function expressions, expression parsing is nested.
  60294. */
  60295. comp_ctx->curr_func.nud_count = 0;
  60296. comp_ctx->curr_func.led_count = 0;
  60297. comp_ctx->curr_func.paren_level = 0;
  60298. comp_ctx->curr_func.expr_lhs = 1;
  60299. comp_ctx->curr_func.allow_in = (rbp_flags & DUK__EXPR_FLAG_REJECT_IN ? 0 : 1);
  60300. duk__expr(comp_ctx, res, rbp_flags);
  60301. if (!(rbp_flags & DUK__EXPR_FLAG_ALLOW_EMPTY) && duk__expr_is_empty(comp_ctx)) {
  60302. DUK_ERROR_SYNTAX(thr, DUK_STR_EMPTY_EXPR_NOT_ALLOWED);
  60303. DUK_WO_NORETURN(return;);
  60304. }
  60305. }
  60306. /* A bunch of helpers (for size optimization) that combine duk__expr()/duk__exprtop()
  60307. * and result conversions.
  60308. *
  60309. * Each helper needs at least 2-3 calls to make it worth while to wrap.
  60310. */
  60311. #if 0 /* unused */
  60312. DUK_LOCAL duk_regconst_t duk__expr_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60313. duk__expr(comp_ctx, res, rbp_flags);
  60314. return duk__ivalue_toreg(comp_ctx, res);
  60315. }
  60316. #endif
  60317. #if 0 /* unused */
  60318. DUK_LOCAL duk_regconst_t duk__expr_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60319. duk__expr(comp_ctx, res, rbp_flags);
  60320. return duk__ivalue_totemp(comp_ctx, res);
  60321. }
  60322. #endif
  60323. DUK_LOCAL void duk__expr_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_regconst_t forced_reg) {
  60324. DUK_ASSERT(forced_reg >= 0);
  60325. duk__expr(comp_ctx, res, rbp_flags);
  60326. duk__ivalue_toforcedreg(comp_ctx, res, forced_reg);
  60327. }
  60328. DUK_LOCAL duk_regconst_t duk__expr_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60329. duk__expr(comp_ctx, res, rbp_flags);
  60330. return duk__ivalue_toregconst(comp_ctx, res);
  60331. }
  60332. #if 0 /* unused */
  60333. DUK_LOCAL duk_regconst_t duk__expr_totempconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60334. duk__expr(comp_ctx, res, rbp_flags);
  60335. return duk__ivalue_totempconst(comp_ctx, res);
  60336. }
  60337. #endif
  60338. DUK_LOCAL void duk__expr_toplain(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60339. duk__expr(comp_ctx, res, rbp_flags);
  60340. duk__ivalue_toplain(comp_ctx, res);
  60341. }
  60342. DUK_LOCAL void duk__expr_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60343. duk__expr(comp_ctx, res, rbp_flags);
  60344. duk__ivalue_toplain_ignore(comp_ctx, res);
  60345. }
  60346. DUK_LOCAL duk_regconst_t duk__exprtop_toreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60347. duk__exprtop(comp_ctx, res, rbp_flags);
  60348. return duk__ivalue_toreg(comp_ctx, res);
  60349. }
  60350. #if 0 /* unused */
  60351. DUK_LOCAL duk_regconst_t duk__exprtop_totemp(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60352. duk__exprtop(comp_ctx, res, rbp_flags);
  60353. return duk__ivalue_totemp(comp_ctx, res);
  60354. }
  60355. #endif
  60356. DUK_LOCAL void duk__exprtop_toforcedreg(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags, duk_regconst_t forced_reg) {
  60357. DUK_ASSERT(forced_reg >= 0);
  60358. duk__exprtop(comp_ctx, res, rbp_flags);
  60359. duk__ivalue_toforcedreg(comp_ctx, res, forced_reg);
  60360. }
  60361. DUK_LOCAL duk_regconst_t duk__exprtop_toregconst(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t rbp_flags) {
  60362. duk__exprtop(comp_ctx, res, rbp_flags);
  60363. return duk__ivalue_toregconst(comp_ctx, res);
  60364. }
  60365. #if 0 /* unused */
  60366. DUK_LOCAL void duk__exprtop_toplain_ignore(duk_compiler_ctx *comp_ctx, duk_ivalue *res, int rbp_flags) {
  60367. duk__exprtop(comp_ctx, res, rbp_flags);
  60368. duk__ivalue_toplain_ignore(comp_ctx, res);
  60369. }
  60370. #endif
  60371. /*
  60372. * Parse an individual source element (top level statement) or a statement.
  60373. *
  60374. * Handles labeled statements automatically (peeling away labels before
  60375. * parsing an expression that follows the label(s)).
  60376. *
  60377. * Upon entry, 'curr_tok' contains the first token of the statement (parsed
  60378. * in "allow regexp literal" mode). Upon exit, 'curr_tok' contains the first
  60379. * token following the statement (if the statement has a terminator, this is
  60380. * the token after the terminator).
  60381. */
  60382. #define DUK__HAS_VAL (1 << 0) /* stmt has non-empty value */
  60383. #define DUK__HAS_TERM (1 << 1) /* stmt has explicit/implicit semicolon terminator */
  60384. #define DUK__ALLOW_AUTO_SEMI_ALWAYS (1 << 2) /* allow automatic semicolon even without lineterm (compatibility) */
  60385. #define DUK__STILL_PROLOGUE (1 << 3) /* statement does not terminate directive prologue */
  60386. #define DUK__IS_TERMINAL (1 << 4) /* statement is guaranteed to be terminal (control doesn't flow to next statement) */
  60387. /* Parse a single variable declaration (e.g. "i" or "i=10"). A leading 'var'
  60388. * has already been eaten. These is no return value in 'res', it is used only
  60389. * as a temporary.
  60390. *
  60391. * When called from 'for-in' statement parser, the initializer expression must
  60392. * not allow the 'in' token. The caller supply additional expression parsing
  60393. * flags (like DUK__EXPR_FLAG_REJECT_IN) in 'expr_flags'.
  60394. *
  60395. * Finally, out_rc_varname and out_reg_varbind are updated to reflect where
  60396. * the identifier is bound:
  60397. *
  60398. * If register bound: out_reg_varbind >= 0, out_rc_varname == 0 (ignore)
  60399. * If not register bound: out_reg_varbind < 0, out_rc_varname >= 0
  60400. *
  60401. * These allow the caller to use the variable for further assignment, e.g.
  60402. * as is done in 'for-in' parsing.
  60403. */
  60404. DUK_LOCAL void duk__parse_var_decl(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags, duk_regconst_t *out_reg_varbind, duk_regconst_t *out_rc_varname) {
  60405. duk_hthread *thr = comp_ctx->thr;
  60406. duk_hstring *h_varname;
  60407. duk_regconst_t reg_varbind;
  60408. duk_regconst_t rc_varname;
  60409. /* assume 'var' has been eaten */
  60410. /* Note: Identifier rejects reserved words */
  60411. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  60412. goto syntax_error;
  60413. }
  60414. h_varname = comp_ctx->curr_token.str1;
  60415. DUK_ASSERT(h_varname != NULL);
  60416. /* strict mode restrictions (E5 Section 12.2.1) */
  60417. if (duk__hstring_is_eval_or_arguments_in_strict_mode(comp_ctx, h_varname)) {
  60418. goto syntax_error;
  60419. }
  60420. /* register declarations in first pass */
  60421. if (comp_ctx->curr_func.in_scanning) {
  60422. duk_uarridx_t n;
  60423. DUK_DDD(DUK_DDDPRINT("register variable declaration %!O in pass 1",
  60424. (duk_heaphdr *) h_varname));
  60425. n = (duk_uarridx_t) duk_get_length(thr, comp_ctx->curr_func.decls_idx);
  60426. duk_push_hstring(thr, h_varname);
  60427. duk_put_prop_index(thr, comp_ctx->curr_func.decls_idx, n);
  60428. duk_push_int(thr, DUK_DECL_TYPE_VAR + (0 << 8));
  60429. duk_put_prop_index(thr, comp_ctx->curr_func.decls_idx, n + 1);
  60430. }
  60431. duk_push_hstring(thr, h_varname); /* push before advancing to keep reachable */
  60432. /* register binding lookup is based on varmap (even in first pass) */
  60433. duk_dup_top(thr);
  60434. (void) duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname);
  60435. duk__advance(comp_ctx); /* eat identifier */
  60436. if (comp_ctx->curr_token.t == DUK_TOK_EQUALSIGN) {
  60437. duk__advance(comp_ctx);
  60438. DUK_DDD(DUK_DDDPRINT("vardecl, assign to '%!O' -> reg_varbind=%ld, rc_varname=%ld",
  60439. (duk_heaphdr *) h_varname, (long) reg_varbind, (long) rc_varname));
  60440. duk__exprtop(comp_ctx, res, DUK__BP_COMMA | expr_flags /*rbp_flags*/); /* AssignmentExpression */
  60441. if (reg_varbind >= 0) {
  60442. duk__ivalue_toforcedreg(comp_ctx, res, reg_varbind);
  60443. } else {
  60444. duk_regconst_t reg_val;
  60445. reg_val = duk__ivalue_toreg(comp_ctx, res);
  60446. duk__emit_a_bc(comp_ctx,
  60447. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  60448. reg_val,
  60449. rc_varname);
  60450. }
  60451. } else {
  60452. if (expr_flags & DUK__EXPR_FLAG_REQUIRE_INIT) {
  60453. /* Used for minimal 'const': initializer required. */
  60454. goto syntax_error;
  60455. }
  60456. }
  60457. duk_pop(thr); /* pop varname */
  60458. *out_rc_varname = rc_varname;
  60459. *out_reg_varbind = reg_varbind;
  60460. return;
  60461. syntax_error:
  60462. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_VAR_DECLARATION);
  60463. DUK_WO_NORETURN(return;);
  60464. }
  60465. DUK_LOCAL void duk__parse_var_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_small_uint_t expr_flags) {
  60466. duk_regconst_t reg_varbind;
  60467. duk_regconst_t rc_varname;
  60468. duk__advance(comp_ctx); /* eat 'var' */
  60469. for (;;) {
  60470. /* rc_varname and reg_varbind are ignored here */
  60471. duk__parse_var_decl(comp_ctx, res, 0 | expr_flags, &reg_varbind, &rc_varname);
  60472. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  60473. break;
  60474. }
  60475. duk__advance(comp_ctx);
  60476. }
  60477. }
  60478. DUK_LOCAL void duk__parse_for_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  60479. duk_hthread *thr = comp_ctx->thr;
  60480. duk_int_t pc_v34_lhs; /* start variant 3/4 left-hand-side code (L1 in doc/compiler.rst example) */
  60481. duk_regconst_t temp_reset; /* knock back "next temp" to this whenever possible */
  60482. duk_regconst_t reg_temps; /* preallocated temporaries (2) for variants 3 and 4 */
  60483. DUK_DDD(DUK_DDDPRINT("start parsing a for/for-in statement"));
  60484. /* Two temporaries are preallocated here for variants 3 and 4 which need
  60485. * registers which are never clobbered by expressions in the loop
  60486. * (concretely: for the enumerator object and the next enumerated value).
  60487. * Variants 1 and 2 "release" these temps.
  60488. */
  60489. reg_temps = DUK__ALLOCTEMPS(comp_ctx, 2);
  60490. temp_reset = DUK__GETTEMP(comp_ctx);
  60491. /*
  60492. * For/for-in main variants are:
  60493. *
  60494. * 1. for (ExpressionNoIn_opt; Expression_opt; Expression_opt) Statement
  60495. * 2. for (var VariableDeclarationNoIn; Expression_opt; Expression_opt) Statement
  60496. * 3. for (LeftHandSideExpression in Expression) Statement
  60497. * 4. for (var VariableDeclarationNoIn in Expression) Statement
  60498. *
  60499. * Parsing these without arbitrary lookahead or backtracking is relatively
  60500. * tricky but we manage to do so for now.
  60501. *
  60502. * See doc/compiler.rst for a detailed discussion of control flow
  60503. * issues, evaluation order issues, etc.
  60504. */
  60505. duk__advance(comp_ctx); /* eat 'for' */
  60506. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  60507. DUK_DDD(DUK_DDDPRINT("detecting for/for-in loop variant, pc=%ld", (long) duk__get_current_pc(comp_ctx)));
  60508. /* a label site has been emitted by duk__parse_stmt() automatically
  60509. * (it will also emit the ENDLABEL).
  60510. */
  60511. if (comp_ctx->curr_token.t == DUK_TOK_VAR) {
  60512. /*
  60513. * Variant 2 or 4
  60514. */
  60515. duk_regconst_t reg_varbind; /* variable binding register if register-bound (otherwise < 0) */
  60516. duk_regconst_t rc_varname; /* variable name reg/const, if variable not register-bound */
  60517. duk__advance(comp_ctx); /* eat 'var' */
  60518. duk__parse_var_decl(comp_ctx, res, DUK__EXPR_FLAG_REJECT_IN, &reg_varbind, &rc_varname);
  60519. DUK__SETTEMP(comp_ctx, temp_reset);
  60520. if (comp_ctx->curr_token.t == DUK_TOK_IN) {
  60521. /*
  60522. * Variant 4
  60523. */
  60524. DUK_DDD(DUK_DDDPRINT("detected for variant 4: for (var VariableDeclarationNoIn in Expression) Statement"));
  60525. pc_v34_lhs = duk__get_current_pc(comp_ctx); /* jump is inserted here */
  60526. if (reg_varbind >= 0) {
  60527. duk__emit_a_bc(comp_ctx,
  60528. DUK_OP_LDREG,
  60529. reg_varbind,
  60530. reg_temps + 0);
  60531. } else {
  60532. duk__emit_a_bc(comp_ctx,
  60533. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  60534. reg_temps + 0,
  60535. rc_varname);
  60536. }
  60537. goto parse_3_or_4;
  60538. } else {
  60539. /*
  60540. * Variant 2
  60541. */
  60542. DUK_DDD(DUK_DDDPRINT("detected for variant 2: for (var VariableDeclarationNoIn; Expression_opt; Expression_opt) Statement"));
  60543. for (;;) {
  60544. /* more initializers */
  60545. if (comp_ctx->curr_token.t != DUK_TOK_COMMA) {
  60546. break;
  60547. }
  60548. DUK_DDD(DUK_DDDPRINT("variant 2 has another variable initializer"));
  60549. duk__advance(comp_ctx); /* eat comma */
  60550. duk__parse_var_decl(comp_ctx, res, DUK__EXPR_FLAG_REJECT_IN, &reg_varbind, &rc_varname);
  60551. }
  60552. goto parse_1_or_2;
  60553. }
  60554. } else {
  60555. /*
  60556. * Variant 1 or 3
  60557. */
  60558. pc_v34_lhs = duk__get_current_pc(comp_ctx); /* jump is inserted here (variant 3) */
  60559. /* Note that duk__exprtop() here can clobber any reg above current temp_next,
  60560. * so any loop variables (e.g. enumerator) must be "preallocated".
  60561. */
  60562. /* don't coerce yet to a plain value (variant 3 needs special handling) */
  60563. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_REJECT_IN | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression */
  60564. if (comp_ctx->curr_token.t == DUK_TOK_IN) {
  60565. /*
  60566. * Variant 3
  60567. */
  60568. /* XXX: need to determine LHS type, and check that it is LHS compatible */
  60569. DUK_DDD(DUK_DDDPRINT("detected for variant 3: for (LeftHandSideExpression in Expression) Statement"));
  60570. if (duk__expr_is_empty(comp_ctx)) {
  60571. goto syntax_error; /* LeftHandSideExpression does not allow empty expression */
  60572. }
  60573. if (res->t == DUK_IVAL_VAR) {
  60574. duk_regconst_t reg_varbind;
  60575. duk_regconst_t rc_varname;
  60576. duk_dup(thr, res->x1.valstack_idx);
  60577. if (duk__lookup_lhs(comp_ctx, &reg_varbind, &rc_varname)) {
  60578. duk__emit_a_bc(comp_ctx,
  60579. DUK_OP_LDREG,
  60580. reg_varbind,
  60581. reg_temps + 0);
  60582. } else {
  60583. duk__emit_a_bc(comp_ctx,
  60584. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  60585. reg_temps + 0,
  60586. rc_varname);
  60587. }
  60588. } else if (res->t == DUK_IVAL_PROP) {
  60589. /* Don't allow a constant for the object (even for a number etc), as
  60590. * it goes into the 'A' field of the opcode.
  60591. */
  60592. duk_regconst_t reg_obj;
  60593. duk_regconst_t rc_key;
  60594. reg_obj = duk__ispec_toregconst_raw(comp_ctx, &res->x1, -1 /*forced_reg*/, 0 /*flags*/); /* don't allow const */
  60595. rc_key = duk__ispec_toregconst_raw(comp_ctx, &res->x2, -1 /*forced_reg*/, DUK__IVAL_FLAG_ALLOW_CONST /*flags*/);
  60596. duk__emit_a_b_c(comp_ctx,
  60597. DUK_OP_PUTPROP | DUK__EMIT_FLAG_A_IS_SOURCE | DUK__EMIT_FLAG_BC_REGCONST,
  60598. reg_obj,
  60599. rc_key,
  60600. reg_temps + 0);
  60601. } else {
  60602. duk__ivalue_toplain_ignore(comp_ctx, res); /* just in case */
  60603. duk__emit_op_only(comp_ctx,
  60604. DUK_OP_INVLHS);
  60605. }
  60606. goto parse_3_or_4;
  60607. } else {
  60608. /*
  60609. * Variant 1
  60610. */
  60611. DUK_DDD(DUK_DDDPRINT("detected for variant 1: for (ExpressionNoIn_opt; Expression_opt; Expression_opt) Statement"));
  60612. duk__ivalue_toplain_ignore(comp_ctx, res);
  60613. goto parse_1_or_2;
  60614. }
  60615. }
  60616. parse_1_or_2:
  60617. /*
  60618. * Parse variant 1 or 2. The first part expression (which differs
  60619. * in the variants) has already been parsed and its code emitted.
  60620. *
  60621. * reg_temps + 0: unused
  60622. * reg_temps + 1: unused
  60623. */
  60624. {
  60625. duk_regconst_t rc_cond;
  60626. duk_int_t pc_l1, pc_l2, pc_l3, pc_l4;
  60627. duk_int_t pc_jumpto_l3, pc_jumpto_l4;
  60628. duk_bool_t expr_c_empty;
  60629. DUK_DDD(DUK_DDDPRINT("shared code for parsing variants 1 and 2"));
  60630. /* "release" preallocated temps since we won't need them */
  60631. temp_reset = reg_temps + 0;
  60632. DUK__SETTEMP(comp_ctx, temp_reset);
  60633. duk__advance_expect(comp_ctx, DUK_TOK_SEMICOLON);
  60634. pc_l1 = duk__get_current_pc(comp_ctx);
  60635. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression_opt */
  60636. if (duk__expr_is_empty(comp_ctx)) {
  60637. /* no need to coerce */
  60638. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* to body */
  60639. pc_jumpto_l4 = -1; /* omitted */
  60640. } else {
  60641. rc_cond = duk__ivalue_toregconst(comp_ctx, res);
  60642. duk__emit_if_false_skip(comp_ctx, rc_cond);
  60643. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* to body */
  60644. pc_jumpto_l4 = duk__emit_jump_empty(comp_ctx); /* to exit */
  60645. }
  60646. DUK__SETTEMP(comp_ctx, temp_reset);
  60647. duk__advance_expect(comp_ctx, DUK_TOK_SEMICOLON);
  60648. pc_l2 = duk__get_current_pc(comp_ctx);
  60649. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR | DUK__EXPR_FLAG_ALLOW_EMPTY /*rbp_flags*/); /* Expression_opt */
  60650. if (duk__expr_is_empty(comp_ctx)) {
  60651. /* no need to coerce */
  60652. expr_c_empty = 1;
  60653. /* JUMP L1 omitted */
  60654. } else {
  60655. duk__ivalue_toplain_ignore(comp_ctx, res);
  60656. expr_c_empty = 0;
  60657. duk__emit_jump(comp_ctx, pc_l1);
  60658. }
  60659. DUK__SETTEMP(comp_ctx, temp_reset);
  60660. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  60661. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* Allow RegExp as part of next stmt. */
  60662. pc_l3 = duk__get_current_pc(comp_ctx);
  60663. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  60664. if (expr_c_empty) {
  60665. duk__emit_jump(comp_ctx, pc_l1);
  60666. } else {
  60667. duk__emit_jump(comp_ctx, pc_l2);
  60668. }
  60669. /* temp reset is not necessary after duk__parse_stmt(), which already does it */
  60670. pc_l4 = duk__get_current_pc(comp_ctx);
  60671. DUK_DDD(DUK_DDDPRINT("patching jumps: jumpto_l3: %ld->%ld, jumpto_l4: %ld->%ld, "
  60672. "break: %ld->%ld, continue: %ld->%ld",
  60673. (long) pc_jumpto_l3, (long) pc_l3, (long) pc_jumpto_l4, (long) pc_l4,
  60674. (long) (pc_label_site + 1), (long) pc_l4, (long) (pc_label_site + 2), (long) pc_l2));
  60675. duk__patch_jump(comp_ctx, pc_jumpto_l3, pc_l3);
  60676. duk__patch_jump(comp_ctx, pc_jumpto_l4, pc_l4);
  60677. duk__patch_jump(comp_ctx,
  60678. pc_label_site + 1,
  60679. pc_l4); /* break jump */
  60680. duk__patch_jump(comp_ctx,
  60681. pc_label_site + 2,
  60682. expr_c_empty ? pc_l1 : pc_l2); /* continue jump */
  60683. }
  60684. goto finished;
  60685. parse_3_or_4:
  60686. /*
  60687. * Parse variant 3 or 4.
  60688. *
  60689. * For variant 3 (e.g. "for (A in C) D;") the code for A (except the
  60690. * final property/variable write) has already been emitted. The first
  60691. * instruction of that code is at pc_v34_lhs; a JUMP needs to be inserted
  60692. * there to satisfy control flow needs.
  60693. *
  60694. * For variant 4, if the variable declaration had an initializer
  60695. * (e.g. "for (var A = B in C) D;") the code for the assignment
  60696. * (B) has already been emitted.
  60697. *
  60698. * Variables set before entering here:
  60699. *
  60700. * pc_v34_lhs: insert a "JUMP L2" here (see doc/compiler.rst example).
  60701. * reg_temps + 0: iteration target value (written to LHS)
  60702. * reg_temps + 1: enumerator object
  60703. */
  60704. {
  60705. duk_int_t pc_l1, pc_l2, pc_l3, pc_l4, pc_l5;
  60706. duk_int_t pc_jumpto_l2, pc_jumpto_l3, pc_jumpto_l4, pc_jumpto_l5;
  60707. duk_regconst_t reg_target;
  60708. DUK_DDD(DUK_DDDPRINT("shared code for parsing variants 3 and 4, pc_v34_lhs=%ld", (long) pc_v34_lhs));
  60709. DUK__SETTEMP(comp_ctx, temp_reset);
  60710. /* First we need to insert a jump in the middle of previously
  60711. * emitted code to get the control flow right. No jumps can
  60712. * cross the position where the jump is inserted. See doc/compiler.rst
  60713. * for discussion on the intricacies of control flow and side effects
  60714. * for variants 3 and 4.
  60715. */
  60716. duk__insert_jump_entry(comp_ctx, pc_v34_lhs);
  60717. pc_jumpto_l2 = pc_v34_lhs; /* inserted jump */
  60718. pc_l1 = pc_v34_lhs + 1; /* +1, right after inserted jump */
  60719. /* The code for writing reg_temps + 0 to the left hand side has already
  60720. * been emitted.
  60721. */
  60722. pc_jumpto_l3 = duk__emit_jump_empty(comp_ctx); /* -> loop body */
  60723. duk__advance(comp_ctx); /* eat 'in' */
  60724. /* Parse enumeration target and initialize enumerator. For 'null' and 'undefined',
  60725. * INITENUM will creates a 'null' enumerator which works like an empty enumerator
  60726. * (E5 Section 12.6.4, step 3). Note that INITENUM requires the value to be in a
  60727. * register (constant not allowed).
  60728. */
  60729. pc_l2 = duk__get_current_pc(comp_ctx);
  60730. reg_target = duk__exprtop_toreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/); /* Expression */
  60731. duk__emit_b_c(comp_ctx,
  60732. DUK_OP_INITENUM | DUK__EMIT_FLAG_B_IS_TARGET,
  60733. reg_temps + 1,
  60734. reg_target);
  60735. pc_jumpto_l4 = duk__emit_jump_empty(comp_ctx);
  60736. DUK__SETTEMP(comp_ctx, temp_reset);
  60737. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  60738. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* Allow RegExp as part of next stmt. */
  60739. pc_l3 = duk__get_current_pc(comp_ctx);
  60740. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  60741. /* temp reset is not necessary after duk__parse_stmt(), which already does it */
  60742. /* NEXTENUM needs a jump slot right after the main opcode.
  60743. * We need the code emitter to reserve the slot: if there's
  60744. * target shuffling, the target shuffle opcodes must happen
  60745. * after the jump slot (for NEXTENUM the shuffle opcodes are
  60746. * not needed if the enum is finished).
  60747. */
  60748. pc_l4 = duk__get_current_pc(comp_ctx);
  60749. duk__emit_b_c(comp_ctx,
  60750. DUK_OP_NEXTENUM | DUK__EMIT_FLAG_B_IS_TARGET | DUK__EMIT_FLAG_RESERVE_JUMPSLOT,
  60751. reg_temps + 0,
  60752. reg_temps + 1);
  60753. pc_jumpto_l5 = comp_ctx->emit_jumpslot_pc; /* NEXTENUM jump slot: executed when enum finished */
  60754. duk__emit_jump(comp_ctx, pc_l1); /* jump to next loop, using reg_v34_iter as iterated value */
  60755. pc_l5 = duk__get_current_pc(comp_ctx);
  60756. /* XXX: since the enumerator may be a memory expensive object,
  60757. * perhaps clear it explicitly here? If so, break jump must
  60758. * go through this clearing operation.
  60759. */
  60760. DUK_DDD(DUK_DDDPRINT("patching jumps: jumpto_l2: %ld->%ld, jumpto_l3: %ld->%ld, "
  60761. "jumpto_l4: %ld->%ld, jumpto_l5: %ld->%ld, "
  60762. "break: %ld->%ld, continue: %ld->%ld",
  60763. (long) pc_jumpto_l2, (long) pc_l2, (long) pc_jumpto_l3, (long) pc_l3,
  60764. (long) pc_jumpto_l4, (long) pc_l4, (long) pc_jumpto_l5, (long) pc_l5,
  60765. (long) (pc_label_site + 1), (long) pc_l5, (long) (pc_label_site + 2), (long) pc_l4));
  60766. duk__patch_jump(comp_ctx, pc_jumpto_l2, pc_l2);
  60767. duk__patch_jump(comp_ctx, pc_jumpto_l3, pc_l3);
  60768. duk__patch_jump(comp_ctx, pc_jumpto_l4, pc_l4);
  60769. duk__patch_jump(comp_ctx, pc_jumpto_l5, pc_l5);
  60770. duk__patch_jump(comp_ctx, pc_label_site + 1, pc_l5); /* break jump */
  60771. duk__patch_jump(comp_ctx, pc_label_site + 2, pc_l4); /* continue jump */
  60772. }
  60773. goto finished;
  60774. finished:
  60775. DUK_DDD(DUK_DDDPRINT("end parsing a for/for-in statement"));
  60776. return;
  60777. syntax_error:
  60778. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_FOR);
  60779. DUK_WO_NORETURN(return;);
  60780. }
  60781. DUK_LOCAL void duk__parse_switch_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  60782. duk_hthread *thr = comp_ctx->thr;
  60783. duk_regconst_t temp_at_loop;
  60784. duk_regconst_t rc_switch; /* reg/const for switch value */
  60785. duk_regconst_t rc_case; /* reg/const for case value */
  60786. duk_regconst_t reg_temp; /* general temp register */
  60787. duk_int_t pc_prevcase = -1;
  60788. duk_int_t pc_prevstmt = -1;
  60789. duk_int_t pc_default = -1; /* -1 == not set, -2 == pending (next statement list) */
  60790. /* Note: negative pc values are ignored when patching jumps, so no explicit checks needed */
  60791. /*
  60792. * Switch is pretty complicated because of several conflicting concerns:
  60793. *
  60794. * - Want to generate code without an intermediate representation,
  60795. * i.e., in one go
  60796. *
  60797. * - Case selectors are expressions, not values, and may thus e.g. throw
  60798. * exceptions (which causes evaluation order concerns)
  60799. *
  60800. * - Evaluation semantics of case selectors and default clause need to be
  60801. * carefully implemented to provide correct behavior even with case value
  60802. * side effects
  60803. *
  60804. * - Fall through case and default clauses; avoiding dead JUMPs if case
  60805. * ends with an unconditional jump (a break or a continue)
  60806. *
  60807. * - The same case value may occur multiple times, but evaluation rules
  60808. * only process the first match before switching to a "propagation" mode
  60809. * where case values are no longer evaluated
  60810. *
  60811. * See E5 Section 12.11. Also see doc/compiler.rst for compilation
  60812. * discussion.
  60813. */
  60814. duk__advance(comp_ctx);
  60815. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  60816. rc_switch = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  60817. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* RegExp mode does not matter. */
  60818. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  60819. DUK_DDD(DUK_DDDPRINT("switch value in register %ld", (long) rc_switch));
  60820. temp_at_loop = DUK__GETTEMP(comp_ctx);
  60821. for (;;) {
  60822. duk_int_t num_stmts;
  60823. duk_small_uint_t tok;
  60824. /* sufficient for keeping temp reg numbers in check */
  60825. DUK__SETTEMP(comp_ctx, temp_at_loop);
  60826. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  60827. break;
  60828. }
  60829. /*
  60830. * Parse a case or default clause.
  60831. */
  60832. if (comp_ctx->curr_token.t == DUK_TOK_CASE) {
  60833. /*
  60834. * Case clause.
  60835. *
  60836. * Note: cannot use reg_case as a temp register (for SEQ target)
  60837. * because it may be a constant.
  60838. */
  60839. duk__patch_jump_here(comp_ctx, pc_prevcase); /* chain jumps for case
  60840. * evaluation and checking
  60841. */
  60842. duk__advance(comp_ctx);
  60843. rc_case = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  60844. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  60845. reg_temp = DUK__ALLOCTEMP(comp_ctx);
  60846. duk__emit_a_b_c(comp_ctx,
  60847. DUK_OP_SEQ | DUK__EMIT_FLAG_BC_REGCONST,
  60848. reg_temp,
  60849. rc_switch,
  60850. rc_case);
  60851. duk__emit_if_true_skip(comp_ctx, reg_temp);
  60852. /* jump to next case clause */
  60853. pc_prevcase = duk__emit_jump_empty(comp_ctx); /* no match, next case */
  60854. /* statements go here (if any) on next loop */
  60855. } else if (comp_ctx->curr_token.t == DUK_TOK_DEFAULT) {
  60856. /*
  60857. * Default clause.
  60858. */
  60859. if (pc_default >= 0) {
  60860. goto syntax_error;
  60861. }
  60862. duk__advance(comp_ctx);
  60863. duk__advance_expect(comp_ctx, DUK_TOK_COLON);
  60864. /* Fix for https://github.com/svaarala/duktape/issues/155:
  60865. * If 'default' is first clause (detected by pc_prevcase < 0)
  60866. * we need to ensure we stay in the matching chain.
  60867. */
  60868. if (pc_prevcase < 0) {
  60869. DUK_DD(DUK_DDPRINT("default clause is first, emit prevcase jump"));
  60870. pc_prevcase = duk__emit_jump_empty(comp_ctx);
  60871. }
  60872. /* default clause matches next statement list (if any) */
  60873. pc_default = -2;
  60874. } else {
  60875. /* Code is not accepted before the first case/default clause */
  60876. goto syntax_error;
  60877. }
  60878. /*
  60879. * Parse code after the clause. Possible terminators are
  60880. * 'case', 'default', and '}'.
  60881. *
  60882. * Note that there may be no code at all, not even an empty statement,
  60883. * between case clauses. This must be handled just like an empty statement
  60884. * (omitting seemingly pointless JUMPs), to avoid situations like
  60885. * test-bug-case-fallthrough.js.
  60886. */
  60887. num_stmts = 0;
  60888. if (pc_default == -2) {
  60889. pc_default = duk__get_current_pc(comp_ctx);
  60890. }
  60891. /* Note: this is correct even for default clause statements:
  60892. * they participate in 'fall-through' behavior even if the
  60893. * default clause is in the middle.
  60894. */
  60895. duk__patch_jump_here(comp_ctx, pc_prevstmt); /* chain jumps for 'fall-through'
  60896. * after a case matches.
  60897. */
  60898. for (;;) {
  60899. tok = comp_ctx->curr_token.t;
  60900. if (tok == DUK_TOK_CASE || tok == DUK_TOK_DEFAULT ||
  60901. tok == DUK_TOK_RCURLY) {
  60902. break;
  60903. }
  60904. num_stmts++;
  60905. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  60906. }
  60907. /* fall-through jump to next code of next case (backpatched) */
  60908. pc_prevstmt = duk__emit_jump_empty(comp_ctx);
  60909. /* XXX: would be nice to omit this jump when the jump is not
  60910. * reachable, at least in the obvious cases (such as the case
  60911. * ending with a 'break'.
  60912. *
  60913. * Perhaps duk__parse_stmt() could provide some info on whether
  60914. * the statement is a "dead end"?
  60915. *
  60916. * If implemented, just set pc_prevstmt to -1 when not needed.
  60917. */
  60918. }
  60919. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RCURLY);
  60920. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  60921. duk__advance(comp_ctx); /* Allow RegExp as part of next stmt. */
  60922. /* default case control flow patchup; note that if pc_prevcase < 0
  60923. * (i.e. no case clauses), control enters default case automatically.
  60924. */
  60925. if (pc_default >= 0) {
  60926. /* default case exists: go there if no case matches */
  60927. duk__patch_jump(comp_ctx, pc_prevcase, pc_default);
  60928. } else {
  60929. /* default case does not exist, or no statements present
  60930. * after default case: finish case evaluation
  60931. */
  60932. duk__patch_jump_here(comp_ctx, pc_prevcase);
  60933. }
  60934. /* fall-through control flow patchup; note that pc_prevstmt may be
  60935. * < 0 (i.e. no case clauses), in which case this is a no-op.
  60936. */
  60937. duk__patch_jump_here(comp_ctx, pc_prevstmt);
  60938. /* continue jump not patched, an INVALID opcode remains there */
  60939. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  60940. /* Note: 'fast' breaks will jump to pc_label_site + 1, which will
  60941. * then jump here. The double jump will be eliminated by a
  60942. * peephole pass, resulting in an optimal jump here. The label
  60943. * site jumps will remain in bytecode and will waste code size.
  60944. */
  60945. return;
  60946. syntax_error:
  60947. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_SWITCH);
  60948. DUK_WO_NORETURN(return;);
  60949. }
  60950. DUK_LOCAL void duk__parse_if_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  60951. duk_regconst_t temp_reset;
  60952. duk_regconst_t rc_cond;
  60953. duk_int_t pc_jump_false;
  60954. DUK_DDD(DUK_DDDPRINT("begin parsing if statement"));
  60955. temp_reset = DUK__GETTEMP(comp_ctx);
  60956. duk__advance(comp_ctx); /* eat 'if' */
  60957. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  60958. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  60959. duk__emit_if_true_skip(comp_ctx, rc_cond);
  60960. pc_jump_false = duk__emit_jump_empty(comp_ctx); /* jump to end or else part */
  60961. DUK__SETTEMP(comp_ctx, temp_reset);
  60962. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  60963. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* Allow RegExp as part of next stmt. */
  60964. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  60965. /* The 'else' ambiguity is resolved by 'else' binding to the innermost
  60966. * construct, so greedy matching is correct here.
  60967. */
  60968. if (comp_ctx->curr_token.t == DUK_TOK_ELSE) {
  60969. duk_int_t pc_jump_end;
  60970. DUK_DDD(DUK_DDDPRINT("if has else part"));
  60971. duk__advance(comp_ctx);
  60972. pc_jump_end = duk__emit_jump_empty(comp_ctx); /* jump from true part to end */
  60973. duk__patch_jump_here(comp_ctx, pc_jump_false);
  60974. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  60975. duk__patch_jump_here(comp_ctx, pc_jump_end);
  60976. } else {
  60977. DUK_DDD(DUK_DDDPRINT("if does not have else part"));
  60978. duk__patch_jump_here(comp_ctx, pc_jump_false);
  60979. }
  60980. DUK_DDD(DUK_DDDPRINT("end parsing if statement"));
  60981. }
  60982. DUK_LOCAL void duk__parse_do_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  60983. duk_regconst_t rc_cond;
  60984. duk_int_t pc_start;
  60985. DUK_DDD(DUK_DDDPRINT("begin parsing do statement"));
  60986. duk__advance(comp_ctx); /* Eat 'do'; allow RegExp as part of next stmt. */
  60987. pc_start = duk__get_current_pc(comp_ctx);
  60988. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  60989. duk__patch_jump_here(comp_ctx, pc_label_site + 2); /* continue jump */
  60990. duk__advance_expect(comp_ctx, DUK_TOK_WHILE);
  60991. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  60992. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  60993. duk__emit_if_false_skip(comp_ctx, rc_cond);
  60994. duk__emit_jump(comp_ctx, pc_start);
  60995. /* no need to reset temps, as we're finished emitting code */
  60996. comp_ctx->curr_func.allow_regexp_in_adv = 1; /* Allow RegExp as part of next stmt. */
  60997. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  60998. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  60999. DUK_DDD(DUK_DDDPRINT("end parsing do statement"));
  61000. }
  61001. DUK_LOCAL void duk__parse_while_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_int_t pc_label_site) {
  61002. duk_regconst_t temp_reset;
  61003. duk_regconst_t rc_cond;
  61004. duk_int_t pc_start;
  61005. duk_int_t pc_jump_false;
  61006. DUK_DDD(DUK_DDDPRINT("begin parsing while statement"));
  61007. temp_reset = DUK__GETTEMP(comp_ctx);
  61008. duk__advance(comp_ctx); /* eat 'while' */
  61009. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  61010. pc_start = duk__get_current_pc(comp_ctx);
  61011. duk__patch_jump_here(comp_ctx, pc_label_site + 2); /* continue jump */
  61012. rc_cond = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  61013. duk__emit_if_true_skip(comp_ctx, rc_cond);
  61014. pc_jump_false = duk__emit_jump_empty(comp_ctx);
  61015. DUK__SETTEMP(comp_ctx, temp_reset);
  61016. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  61017. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* Allow RegExp as part of next stmt. */
  61018. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  61019. duk__emit_jump(comp_ctx, pc_start);
  61020. duk__patch_jump_here(comp_ctx, pc_jump_false);
  61021. duk__patch_jump_here(comp_ctx, pc_label_site + 1); /* break jump */
  61022. DUK_DDD(DUK_DDDPRINT("end parsing while statement"));
  61023. }
  61024. DUK_LOCAL void duk__parse_break_or_continue_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  61025. duk_hthread *thr = comp_ctx->thr;
  61026. duk_bool_t is_break = (comp_ctx->curr_token.t == DUK_TOK_BREAK);
  61027. duk_int_t label_id;
  61028. duk_int_t label_catch_depth;
  61029. duk_int_t label_pc; /* points to LABEL; pc+1 = jump site for break; pc+2 = jump site for continue */
  61030. duk_bool_t label_is_closest;
  61031. DUK_UNREF(res);
  61032. duk__advance(comp_ctx); /* eat 'break' or 'continue' */
  61033. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || /* explicit semi follows */
  61034. comp_ctx->curr_token.lineterm || /* automatic semi will be inserted */
  61035. comp_ctx->curr_token.allow_auto_semi) { /* automatic semi will be inserted */
  61036. /* break/continue without label */
  61037. duk__lookup_active_label(comp_ctx, DUK_HTHREAD_STRING_EMPTY_STRING(thr), is_break, &label_id, &label_catch_depth, &label_pc, &label_is_closest);
  61038. } else if (comp_ctx->curr_token.t == DUK_TOK_IDENTIFIER) {
  61039. /* break/continue with label (label cannot be a reserved word, production is 'Identifier' */
  61040. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  61041. duk__lookup_active_label(comp_ctx, comp_ctx->curr_token.str1, is_break, &label_id, &label_catch_depth, &label_pc, &label_is_closest);
  61042. duk__advance(comp_ctx);
  61043. } else {
  61044. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_BREAK_CONT_LABEL);
  61045. DUK_WO_NORETURN(return;);
  61046. }
  61047. /* Use a fast break/continue when possible. A fast break/continue is
  61048. * just a jump to the LABEL break/continue jump slot, which then jumps
  61049. * to an appropriate place (for break, going through ENDLABEL correctly).
  61050. * The peephole optimizer will optimize the jump to a direct one.
  61051. */
  61052. if (label_catch_depth == comp_ctx->curr_func.catch_depth &&
  61053. label_is_closest) {
  61054. DUK_DDD(DUK_DDDPRINT("break/continue: is_break=%ld, label_id=%ld, label_is_closest=%ld, "
  61055. "label_catch_depth=%ld, catch_depth=%ld "
  61056. "-> use fast variant (direct jump)",
  61057. (long) is_break, (long) label_id, (long) label_is_closest,
  61058. (long) label_catch_depth, (long) comp_ctx->curr_func.catch_depth));
  61059. duk__emit_jump(comp_ctx, label_pc + (is_break ? 1 : 2));
  61060. } else {
  61061. DUK_DDD(DUK_DDDPRINT("break/continue: is_break=%ld, label_id=%ld, label_is_closest=%ld, "
  61062. "label_catch_depth=%ld, catch_depth=%ld "
  61063. "-> use slow variant (longjmp)",
  61064. (long) is_break, (long) label_id, (long) label_is_closest,
  61065. (long) label_catch_depth, (long) comp_ctx->curr_func.catch_depth));
  61066. duk__emit_bc(comp_ctx,
  61067. is_break ? DUK_OP_BREAK : DUK_OP_CONTINUE,
  61068. (duk_regconst_t) label_id);
  61069. }
  61070. }
  61071. DUK_LOCAL void duk__parse_return_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  61072. duk_hthread *thr = comp_ctx->thr;
  61073. duk_regconst_t rc_val;
  61074. duk__advance(comp_ctx); /* eat 'return' */
  61075. /* A 'return' statement is only allowed inside an actual function body,
  61076. * not as part of eval or global code.
  61077. */
  61078. if (!comp_ctx->curr_func.is_function) {
  61079. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_RETURN);
  61080. DUK_WO_NORETURN(return;);
  61081. }
  61082. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON || /* explicit semi follows */
  61083. comp_ctx->curr_token.lineterm || /* automatic semi will be inserted */
  61084. comp_ctx->curr_token.allow_auto_semi) { /* automatic semi will be inserted */
  61085. DUK_DDD(DUK_DDDPRINT("empty return value -> undefined"));
  61086. duk__emit_op_only(comp_ctx, DUK_OP_RETUNDEF);
  61087. } else {
  61088. duk_int_t pc_before_expr;
  61089. duk_int_t pc_after_expr;
  61090. DUK_DDD(DUK_DDDPRINT("return with a value"));
  61091. DUK_UNREF(pc_before_expr);
  61092. DUK_UNREF(pc_after_expr);
  61093. pc_before_expr = duk__get_current_pc(comp_ctx);
  61094. rc_val = duk__exprtop_toregconst(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  61095. pc_after_expr = duk__get_current_pc(comp_ctx);
  61096. /* Tail call check: if last opcode emitted was CALL, and
  61097. * the context allows it, add a tailcall flag to the CALL.
  61098. * This doesn't guarantee that a tail call will be allowed at
  61099. * runtime, so the RETURN must still be emitted. (Duktape
  61100. * 0.10.0 avoided this and simulated a RETURN if a tail call
  61101. * couldn't be used at runtime; but this didn't work
  61102. * correctly with a thread yield/resume, see
  61103. * test-bug-tailcall-thread-yield-resume.js for discussion.)
  61104. *
  61105. * In addition to the last opcode being CALL, we also need to
  61106. * be sure that 'rc_val' is the result register of the CALL.
  61107. * For instance, for the expression 'return 0, (function ()
  61108. * { return 1; }), 2' the last opcode emitted is CALL (no
  61109. * bytecode is emitted for '2') but 'rc_val' indicates
  61110. * constant '2'. Similarly if '2' is replaced by a register
  61111. * bound variable, no opcodes are emitted but tail call would
  61112. * be incorrect.
  61113. *
  61114. * This is tricky and easy to get wrong. It would be best to
  61115. * track enough expression metadata to check that 'rc_val' came
  61116. * from that last CALL instruction. We don't have that metadata
  61117. * now, so we check that 'rc_val' is a temporary register result
  61118. * (not a constant or a register bound variable). There should
  61119. * be no way currently for 'rc_val' to be a temporary for an
  61120. * expression following the CALL instruction without emitting
  61121. * some opcodes following the CALL. This proxy check is used
  61122. * below.
  61123. *
  61124. * See: test-bug-comma-expr-gh131.js.
  61125. *
  61126. * The non-standard 'caller' property disables tail calls
  61127. * because they pose some special cases which haven't been
  61128. * fixed yet.
  61129. */
  61130. #if defined(DUK_USE_TAILCALL)
  61131. if (comp_ctx->curr_func.catch_depth == 0 && /* no catchers */
  61132. pc_after_expr > pc_before_expr) { /* at least one opcode emitted */
  61133. duk_compiler_instr *instr;
  61134. duk_instr_t ins;
  61135. duk_small_uint_t op;
  61136. instr = duk__get_instr_ptr(comp_ctx, pc_after_expr - 1);
  61137. DUK_ASSERT(instr != NULL);
  61138. ins = instr->ins;
  61139. op = (duk_small_uint_t) DUK_DEC_OP(ins);
  61140. if ((op & ~0x0fU) == DUK_OP_CALL0 &&
  61141. DUK__ISREG_TEMP(comp_ctx, rc_val) /* see above */) {
  61142. DUK_DDD(DUK_DDDPRINT("return statement detected a tail call opportunity: "
  61143. "catch depth is 0, duk__exprtop() emitted >= 1 instructions, "
  61144. "and last instruction is a CALL "
  61145. "-> change to TAILCALL"));
  61146. ins |= DUK_ENC_OP(DUK_BC_CALL_FLAG_TAILCALL);
  61147. instr->ins = ins;
  61148. }
  61149. }
  61150. #endif /* DUK_USE_TAILCALL */
  61151. if (DUK__ISREG(rc_val)) {
  61152. duk__emit_bc(comp_ctx, DUK_OP_RETREG, rc_val);
  61153. } else {
  61154. rc_val = DUK__REMOVECONST(rc_val);
  61155. if (duk__const_needs_refcount(comp_ctx, rc_val)) {
  61156. duk__emit_bc(comp_ctx, DUK_OP_RETCONST, rc_val);
  61157. } else {
  61158. duk__emit_bc(comp_ctx, DUK_OP_RETCONSTN, rc_val);
  61159. }
  61160. }
  61161. }
  61162. }
  61163. DUK_LOCAL void duk__parse_throw_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  61164. duk_regconst_t reg_val;
  61165. duk__advance(comp_ctx); /* eat 'throw' */
  61166. /* Unlike break/continue, throw statement does not allow an empty value. */
  61167. if (comp_ctx->curr_token.lineterm) {
  61168. DUK_ERROR_SYNTAX(comp_ctx->thr, DUK_STR_INVALID_THROW);
  61169. DUK_WO_NORETURN(return;);
  61170. }
  61171. reg_val = duk__exprtop_toreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  61172. duk__emit_bc(comp_ctx,
  61173. DUK_OP_THROW,
  61174. reg_val);
  61175. }
  61176. DUK_LOCAL void duk__parse_try_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  61177. duk_hthread *thr = comp_ctx->thr;
  61178. duk_regconst_t reg_catch; /* reg_catch+0 and reg_catch+1 are reserved for TRYCATCH */
  61179. duk_regconst_t rc_varname = 0;
  61180. duk_small_uint_t trycatch_flags = 0;
  61181. duk_int_t pc_ldconst = -1;
  61182. duk_int_t pc_trycatch = -1;
  61183. duk_int_t pc_catch = -1;
  61184. duk_int_t pc_finally = -1;
  61185. DUK_UNREF(res);
  61186. /*
  61187. * See the following documentation for discussion:
  61188. *
  61189. * doc/execution.rst: control flow details
  61190. *
  61191. * Try, catch, and finally "parts" are Blocks, not Statements, so
  61192. * they must always be delimited by curly braces. This is unlike e.g.
  61193. * the if statement, which accepts any Statement. This eliminates any
  61194. * questions of matching parts of nested try statements. The Block
  61195. * parsing is implemented inline here (instead of calling out).
  61196. *
  61197. * Finally part has a 'let scoped' variable, which requires a few kinks
  61198. * here.
  61199. */
  61200. comp_ctx->curr_func.catch_depth++;
  61201. duk__advance(comp_ctx); /* eat 'try' */
  61202. reg_catch = DUK__ALLOCTEMPS(comp_ctx, 2);
  61203. /* The target for this LDCONST may need output shuffling, but we assume
  61204. * that 'pc_ldconst' will be the LDCONST that we can patch later. This
  61205. * should be the case because there's no input shuffling. (If there's
  61206. * no catch clause, this LDCONST will be replaced with a NOP.)
  61207. */
  61208. pc_ldconst = duk__get_current_pc(comp_ctx);
  61209. duk__emit_a_bc(comp_ctx, DUK_OP_LDCONST, reg_catch, 0 /*patched later*/);
  61210. pc_trycatch = duk__get_current_pc(comp_ctx);
  61211. duk__emit_invalid(comp_ctx); /* TRYCATCH, cannot emit now (not enough info) */
  61212. duk__emit_invalid(comp_ctx); /* jump for 'catch' case */
  61213. duk__emit_invalid(comp_ctx); /* jump for 'finally' case or end (if no finally) */
  61214. /* try part */
  61215. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  61216. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/, 1 /*regexp_after*/);
  61217. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  61218. duk__emit_op_only(comp_ctx, DUK_OP_ENDTRY);
  61219. if (comp_ctx->curr_token.t == DUK_TOK_CATCH) {
  61220. /*
  61221. * The catch variable must be updated to reflect the new allocated
  61222. * register for the duration of the catch clause. We need to store
  61223. * and restore the original value for the varmap entry (if any).
  61224. */
  61225. /*
  61226. * Note: currently register bindings must be fixed for the entire
  61227. * function. So, even though the catch variable is in a register
  61228. * we know, we must use an explicit environment record and slow path
  61229. * accesses to read/write the catch binding to make closures created
  61230. * within the catch clause work correctly. This restriction should
  61231. * be fixable (at least in common cases) later.
  61232. *
  61233. * See: test-bug-catch-binding-2.js.
  61234. *
  61235. * XXX: improve to get fast path access to most catch clauses.
  61236. */
  61237. duk_hstring *h_var;
  61238. duk_int_t varmap_value; /* for storing/restoring the varmap binding for catch variable */
  61239. DUK_DDD(DUK_DDDPRINT("stack top at start of catch clause: %ld", (long) duk_get_top(thr)));
  61240. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_HAVE_CATCH;
  61241. pc_catch = duk__get_current_pc(comp_ctx);
  61242. duk__advance(comp_ctx);
  61243. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  61244. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  61245. /* Identifier, i.e. don't allow reserved words */
  61246. goto syntax_error;
  61247. }
  61248. h_var = comp_ctx->curr_token.str1;
  61249. DUK_ASSERT(h_var != NULL);
  61250. duk_push_hstring(thr, h_var); /* keep in on valstack, use borrowed ref below */
  61251. if (comp_ctx->curr_func.is_strict &&
  61252. ((h_var == DUK_HTHREAD_STRING_EVAL(thr)) ||
  61253. (h_var == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)))) {
  61254. DUK_DDD(DUK_DDDPRINT("catch identifier 'eval' or 'arguments' in strict mode -> SyntaxError"));
  61255. goto syntax_error;
  61256. }
  61257. duk_dup_top(thr);
  61258. rc_varname = duk__getconst(comp_ctx);
  61259. DUK_DDD(DUK_DDDPRINT("catch clause, rc_varname=0x%08lx (%ld)",
  61260. (unsigned long) rc_varname, (long) rc_varname));
  61261. duk__advance(comp_ctx);
  61262. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN);
  61263. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  61264. DUK_DDD(DUK_DDDPRINT("varmap before modifying for catch clause: %!iT",
  61265. (duk_tval *) duk_get_tval(thr, comp_ctx->curr_func.varmap_idx)));
  61266. duk_dup_top(thr);
  61267. duk_get_prop(thr, comp_ctx->curr_func.varmap_idx);
  61268. if (duk_is_undefined(thr, -1)) {
  61269. varmap_value = -2;
  61270. } else if (duk_is_null(thr, -1)) {
  61271. varmap_value = -1;
  61272. } else {
  61273. DUK_ASSERT(duk_is_number(thr, -1));
  61274. varmap_value = duk_get_int(thr, -1);
  61275. DUK_ASSERT(varmap_value >= 0);
  61276. }
  61277. duk_pop(thr);
  61278. #if 0
  61279. /* It'd be nice to do something like this - but it doesn't
  61280. * work for closures created inside the catch clause.
  61281. */
  61282. duk_dup_top(thr);
  61283. duk_push_int(thr, (duk_int_t) (reg_catch + 0));
  61284. duk_put_prop(thr, comp_ctx->curr_func.varmap_idx);
  61285. #endif
  61286. duk_dup_top(thr);
  61287. duk_push_null(thr);
  61288. duk_put_prop(thr, comp_ctx->curr_func.varmap_idx);
  61289. duk__emit_a_bc(comp_ctx,
  61290. DUK_OP_PUTVAR | DUK__EMIT_FLAG_A_IS_SOURCE,
  61291. reg_catch + 0 /*value*/,
  61292. rc_varname /*varname*/);
  61293. DUK_DDD(DUK_DDDPRINT("varmap before parsing catch clause: %!iT",
  61294. (duk_tval *) duk_get_tval(thr, comp_ctx->curr_func.varmap_idx)));
  61295. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/, 1 /*regexp_after*/);
  61296. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  61297. if (varmap_value == -2) {
  61298. /* not present */
  61299. duk_del_prop(thr, comp_ctx->curr_func.varmap_idx);
  61300. } else {
  61301. if (varmap_value == -1) {
  61302. duk_push_null(thr);
  61303. } else {
  61304. DUK_ASSERT(varmap_value >= 0);
  61305. duk_push_int(thr, varmap_value);
  61306. }
  61307. duk_put_prop(thr, comp_ctx->curr_func.varmap_idx);
  61308. }
  61309. /* varname is popped by above code */
  61310. DUK_DDD(DUK_DDDPRINT("varmap after restore catch clause: %!iT",
  61311. (duk_tval *) duk_get_tval(thr, comp_ctx->curr_func.varmap_idx)));
  61312. duk__emit_op_only(comp_ctx,
  61313. DUK_OP_ENDCATCH);
  61314. /*
  61315. * XXX: for now, indicate that an expensive catch binding
  61316. * declarative environment is always needed. If we don't
  61317. * need it, we don't need the const_varname either.
  61318. */
  61319. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_CATCH_BINDING;
  61320. DUK_DDD(DUK_DDDPRINT("stack top at end of catch clause: %ld", (long) duk_get_top(thr)));
  61321. }
  61322. if (comp_ctx->curr_token.t == DUK_TOK_FINALLY) {
  61323. trycatch_flags |= DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY;
  61324. pc_finally = duk__get_current_pc(comp_ctx);
  61325. duk__advance(comp_ctx);
  61326. duk__advance_expect(comp_ctx, DUK_TOK_LCURLY);
  61327. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/, 1 /*regexp_after*/);
  61328. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  61329. duk__emit_abc(comp_ctx,
  61330. DUK_OP_ENDFIN,
  61331. reg_catch); /* rethrow */
  61332. }
  61333. if (!(trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) &&
  61334. !(trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY)) {
  61335. /* must have catch and/or finally */
  61336. goto syntax_error;
  61337. }
  61338. /* If there's no catch block, rc_varname will be 0 and duk__patch_trycatch()
  61339. * will replace the LDCONST with a NOP. For any actual constant (including
  61340. * constant 0) the DUK__CONST_MARKER flag will be set in rc_varname.
  61341. */
  61342. duk__patch_trycatch(comp_ctx,
  61343. pc_ldconst,
  61344. pc_trycatch,
  61345. reg_catch,
  61346. rc_varname,
  61347. trycatch_flags);
  61348. if (trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
  61349. DUK_ASSERT(pc_catch >= 0);
  61350. duk__patch_jump(comp_ctx, pc_trycatch + 1, pc_catch);
  61351. }
  61352. if (trycatch_flags & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
  61353. DUK_ASSERT(pc_finally >= 0);
  61354. duk__patch_jump(comp_ctx, pc_trycatch + 2, pc_finally);
  61355. } else {
  61356. /* without finally, the second jump slot is used to jump to end of stmt */
  61357. duk__patch_jump_here(comp_ctx, pc_trycatch + 2);
  61358. }
  61359. comp_ctx->curr_func.catch_depth--;
  61360. return;
  61361. syntax_error:
  61362. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_TRY);
  61363. DUK_WO_NORETURN(return;);
  61364. }
  61365. DUK_LOCAL void duk__parse_with_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res) {
  61366. duk_int_t pc_trycatch;
  61367. duk_int_t pc_finished;
  61368. duk_regconst_t reg_catch;
  61369. duk_small_uint_t trycatch_flags;
  61370. if (comp_ctx->curr_func.is_strict) {
  61371. DUK_ERROR_SYNTAX(comp_ctx->thr, DUK_STR_WITH_IN_STRICT_MODE);
  61372. DUK_WO_NORETURN(return;);
  61373. }
  61374. comp_ctx->curr_func.catch_depth++;
  61375. duk__advance(comp_ctx); /* eat 'with' */
  61376. reg_catch = DUK__ALLOCTEMPS(comp_ctx, 2);
  61377. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  61378. duk__exprtop_toforcedreg(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/, reg_catch);
  61379. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  61380. duk__advance_expect(comp_ctx, DUK_TOK_RPAREN); /* Allow RegExp as part of next stmt. */
  61381. pc_trycatch = duk__get_current_pc(comp_ctx);
  61382. trycatch_flags = DUK_BC_TRYCATCH_FLAG_WITH_BINDING;
  61383. duk__emit_a_bc(comp_ctx,
  61384. DUK_OP_TRYCATCH | DUK__EMIT_FLAG_NO_SHUFFLE_A,
  61385. (duk_regconst_t) trycatch_flags /*a*/,
  61386. reg_catch /*bc*/);
  61387. duk__emit_invalid(comp_ctx); /* catch jump */
  61388. duk__emit_invalid(comp_ctx); /* finished jump */
  61389. duk__parse_stmt(comp_ctx, res, 0 /*allow_source_elem*/);
  61390. duk__emit_op_only(comp_ctx, DUK_OP_ENDTRY);
  61391. pc_finished = duk__get_current_pc(comp_ctx);
  61392. duk__patch_jump(comp_ctx, pc_trycatch + 2, pc_finished);
  61393. comp_ctx->curr_func.catch_depth--;
  61394. }
  61395. DUK_LOCAL duk_int_t duk__stmt_label_site(duk_compiler_ctx *comp_ctx, duk_int_t label_id) {
  61396. /* if a site already exists, nop: max one label site per statement */
  61397. if (label_id >= 0) {
  61398. return label_id;
  61399. }
  61400. label_id = comp_ctx->curr_func.label_next++;
  61401. DUK_DDD(DUK_DDDPRINT("allocated new label id for label site: %ld", (long) label_id));
  61402. duk__emit_bc(comp_ctx,
  61403. DUK_OP_LABEL,
  61404. (duk_regconst_t) label_id);
  61405. duk__emit_invalid(comp_ctx);
  61406. duk__emit_invalid(comp_ctx);
  61407. return label_id;
  61408. }
  61409. /* Parse a single statement.
  61410. *
  61411. * Creates a label site (with an empty label) automatically for iteration
  61412. * statements. Also "peels off" any label statements for explicit labels.
  61413. */
  61414. DUK_LOCAL void duk__parse_stmt(duk_compiler_ctx *comp_ctx, duk_ivalue *res, duk_bool_t allow_source_elem) {
  61415. duk_hthread *thr = comp_ctx->thr;
  61416. duk_bool_t dir_prol_at_entry; /* directive prologue status at entry */
  61417. duk_regconst_t temp_at_entry;
  61418. duk_size_t labels_len_at_entry;
  61419. duk_int_t pc_at_entry; /* assumed to also be PC of "LABEL" */
  61420. duk_int_t stmt_id;
  61421. duk_small_uint_t stmt_flags = 0;
  61422. duk_int_t label_id = -1;
  61423. duk_small_uint_t tok;
  61424. duk_bool_t test_func_decl;
  61425. DUK__RECURSION_INCREASE(comp_ctx, thr);
  61426. temp_at_entry = DUK__GETTEMP(comp_ctx);
  61427. pc_at_entry = duk__get_current_pc(comp_ctx);
  61428. labels_len_at_entry = duk_get_length(thr, comp_ctx->curr_func.labelnames_idx);
  61429. stmt_id = comp_ctx->curr_func.stmt_next++;
  61430. dir_prol_at_entry = comp_ctx->curr_func.in_directive_prologue;
  61431. DUK_UNREF(stmt_id);
  61432. DUK_DDD(DUK_DDDPRINT("parsing a statement, stmt_id=%ld, temp_at_entry=%ld, labels_len_at_entry=%ld, "
  61433. "is_strict=%ld, in_directive_prologue=%ld, catch_depth=%ld",
  61434. (long) stmt_id, (long) temp_at_entry, (long) labels_len_at_entry,
  61435. (long) comp_ctx->curr_func.is_strict, (long) comp_ctx->curr_func.in_directive_prologue,
  61436. (long) comp_ctx->curr_func.catch_depth));
  61437. /* The directive prologue flag is cleared by default so that it is
  61438. * unset for any recursive statement parsing. It is only "revived"
  61439. * if a directive is detected. (We could also make directives only
  61440. * allowed if 'allow_source_elem' was true.)
  61441. */
  61442. comp_ctx->curr_func.in_directive_prologue = 0;
  61443. retry_parse:
  61444. DUK_DDD(DUK_DDDPRINT("try stmt parse, stmt_id=%ld, label_id=%ld, allow_source_elem=%ld, catch_depth=%ld",
  61445. (long) stmt_id, (long) label_id, (long) allow_source_elem,
  61446. (long) comp_ctx->curr_func.catch_depth));
  61447. /*
  61448. * Detect iteration statements; if encountered, establish an
  61449. * empty label.
  61450. */
  61451. tok = comp_ctx->curr_token.t;
  61452. if (tok == DUK_TOK_FOR || tok == DUK_TOK_DO || tok == DUK_TOK_WHILE ||
  61453. tok == DUK_TOK_SWITCH) {
  61454. DUK_DDD(DUK_DDDPRINT("iteration/switch statement -> add empty label"));
  61455. label_id = duk__stmt_label_site(comp_ctx, label_id);
  61456. duk__add_label(comp_ctx,
  61457. DUK_HTHREAD_STRING_EMPTY_STRING(thr),
  61458. pc_at_entry /*pc_label*/,
  61459. label_id);
  61460. }
  61461. /*
  61462. * Main switch for statement / source element type.
  61463. */
  61464. switch (comp_ctx->curr_token.t) {
  61465. case DUK_TOK_FUNCTION: {
  61466. /*
  61467. * Function declaration, function expression, or (non-standard)
  61468. * function statement.
  61469. *
  61470. * The E5 specification only allows function declarations at
  61471. * the top level (in "source elements"). An ExpressionStatement
  61472. * is explicitly not allowed to begin with a "function" keyword
  61473. * (E5 Section 12.4). Hence any non-error semantics for such
  61474. * non-top-level statements are non-standard. Duktape semantics
  61475. * for function statements are modelled after V8, see
  61476. * test-dev-func-decl-outside-top.js.
  61477. */
  61478. test_func_decl = allow_source_elem;
  61479. #if defined(DUK_USE_NONSTD_FUNC_STMT)
  61480. /* Lenient: allow function declarations outside top level in
  61481. * non-strict mode but reject them in strict mode.
  61482. */
  61483. test_func_decl = test_func_decl || !comp_ctx->curr_func.is_strict;
  61484. #endif /* DUK_USE_NONSTD_FUNC_STMT */
  61485. /* Strict: never allow function declarations outside top level. */
  61486. if (test_func_decl) {
  61487. /* FunctionDeclaration: not strictly a statement but handled as such.
  61488. *
  61489. * O(depth^2) parse count for inner functions is handled by recording a
  61490. * lexer offset on the first compilation pass, so that the function can
  61491. * be efficiently skipped on the second pass. This is encapsulated into
  61492. * duk__parse_func_like_fnum().
  61493. */
  61494. duk_int_t fnum;
  61495. #if defined(DUK_USE_ASSERTIONS)
  61496. duk_idx_t top_before;
  61497. #endif
  61498. DUK_DDD(DUK_DDDPRINT("function declaration statement"));
  61499. #if defined(DUK_USE_ASSERTIONS)
  61500. top_before = duk_get_top(thr);
  61501. #endif
  61502. duk__advance(comp_ctx); /* eat 'function' */
  61503. fnum = duk__parse_func_like_fnum(comp_ctx, DUK__FUNC_FLAG_DECL | DUK__FUNC_FLAG_PUSHNAME_PASS1);
  61504. /* The value stack convention here is a bit odd: the function
  61505. * name is only pushed on pass 1 (in_scanning), and is needed
  61506. * to process function declarations.
  61507. */
  61508. if (comp_ctx->curr_func.in_scanning) {
  61509. duk_uarridx_t n;
  61510. #if defined(DUK_USE_ASSERTIONS)
  61511. DUK_ASSERT(duk_get_top(thr) == top_before + 1);
  61512. #endif
  61513. DUK_DDD(DUK_DDDPRINT("register function declaration %!T in pass 1, fnum %ld",
  61514. duk_get_tval(thr, -1), (long) fnum));
  61515. n = (duk_uarridx_t) duk_get_length(thr, comp_ctx->curr_func.decls_idx);
  61516. /* funcname is at index -1 */
  61517. duk_put_prop_index(thr, comp_ctx->curr_func.decls_idx, n);
  61518. duk_push_int(thr, (duk_int_t) (DUK_DECL_TYPE_FUNC + (fnum << 8)));
  61519. duk_put_prop_index(thr, comp_ctx->curr_func.decls_idx, n + 1);
  61520. } else {
  61521. #if defined(DUK_USE_ASSERTIONS)
  61522. DUK_ASSERT(duk_get_top(thr) == top_before);
  61523. #endif
  61524. }
  61525. /* no statement value (unlike function expression) */
  61526. stmt_flags = 0;
  61527. break;
  61528. } else {
  61529. DUK_ERROR_SYNTAX(thr, DUK_STR_FUNC_STMT_NOT_ALLOWED);
  61530. DUK_WO_NORETURN(return;);
  61531. }
  61532. break;
  61533. }
  61534. case DUK_TOK_LCURLY: {
  61535. DUK_DDD(DUK_DDDPRINT("block statement"));
  61536. duk__advance(comp_ctx);
  61537. duk__parse_stmts(comp_ctx, 0 /*allow_source_elem*/, 0 /*expect_eof*/, 1 /*regexp_after*/);
  61538. /* the DUK_TOK_RCURLY is eaten by duk__parse_stmts() */
  61539. if (label_id >= 0) {
  61540. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  61541. }
  61542. stmt_flags = 0;
  61543. break;
  61544. }
  61545. case DUK_TOK_CONST: {
  61546. DUK_DDD(DUK_DDDPRINT("constant declaration statement"));
  61547. duk__parse_var_stmt(comp_ctx, res, DUK__EXPR_FLAG_REQUIRE_INIT /*expr_flags*/);
  61548. stmt_flags = DUK__HAS_TERM;
  61549. break;
  61550. }
  61551. case DUK_TOK_VAR: {
  61552. DUK_DDD(DUK_DDDPRINT("variable declaration statement"));
  61553. duk__parse_var_stmt(comp_ctx, res, 0 /*expr_flags*/);
  61554. stmt_flags = DUK__HAS_TERM;
  61555. break;
  61556. }
  61557. case DUK_TOK_SEMICOLON: {
  61558. /* empty statement with an explicit semicolon */
  61559. DUK_DDD(DUK_DDDPRINT("empty statement"));
  61560. stmt_flags = DUK__HAS_TERM;
  61561. break;
  61562. }
  61563. case DUK_TOK_IF: {
  61564. DUK_DDD(DUK_DDDPRINT("if statement"));
  61565. duk__parse_if_stmt(comp_ctx, res);
  61566. if (label_id >= 0) {
  61567. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  61568. }
  61569. stmt_flags = 0;
  61570. break;
  61571. }
  61572. case DUK_TOK_DO: {
  61573. /*
  61574. * Do-while statement is mostly trivial, but there is special
  61575. * handling for automatic semicolon handling (triggered by the
  61576. * DUK__ALLOW_AUTO_SEMI_ALWAYS) flag related to a bug filed at:
  61577. *
  61578. * https://bugs.ecmascript.org/show_bug.cgi?id=8
  61579. *
  61580. * See doc/compiler.rst for details.
  61581. */
  61582. DUK_DDD(DUK_DDDPRINT("do statement"));
  61583. DUK_ASSERT(label_id >= 0);
  61584. duk__update_label_flags(comp_ctx,
  61585. label_id,
  61586. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  61587. duk__parse_do_stmt(comp_ctx, res, pc_at_entry);
  61588. stmt_flags = DUK__HAS_TERM | DUK__ALLOW_AUTO_SEMI_ALWAYS; /* DUK__ALLOW_AUTO_SEMI_ALWAYS workaround */
  61589. break;
  61590. }
  61591. case DUK_TOK_WHILE: {
  61592. DUK_DDD(DUK_DDDPRINT("while statement"));
  61593. DUK_ASSERT(label_id >= 0);
  61594. duk__update_label_flags(comp_ctx,
  61595. label_id,
  61596. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  61597. duk__parse_while_stmt(comp_ctx, res, pc_at_entry);
  61598. stmt_flags = 0;
  61599. break;
  61600. }
  61601. case DUK_TOK_FOR: {
  61602. /*
  61603. * For/for-in statement is complicated to parse because
  61604. * determining the statement type (three-part for vs. a
  61605. * for-in) requires potential backtracking.
  61606. *
  61607. * See the helper for the messy stuff.
  61608. */
  61609. DUK_DDD(DUK_DDDPRINT("for/for-in statement"));
  61610. DUK_ASSERT(label_id >= 0);
  61611. duk__update_label_flags(comp_ctx,
  61612. label_id,
  61613. DUK_LABEL_FLAG_ALLOW_BREAK | DUK_LABEL_FLAG_ALLOW_CONTINUE);
  61614. duk__parse_for_stmt(comp_ctx, res, pc_at_entry);
  61615. stmt_flags = 0;
  61616. break;
  61617. }
  61618. case DUK_TOK_CONTINUE:
  61619. case DUK_TOK_BREAK: {
  61620. DUK_DDD(DUK_DDDPRINT("break/continue statement"));
  61621. duk__parse_break_or_continue_stmt(comp_ctx, res);
  61622. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  61623. break;
  61624. }
  61625. case DUK_TOK_RETURN: {
  61626. DUK_DDD(DUK_DDDPRINT("return statement"));
  61627. duk__parse_return_stmt(comp_ctx, res);
  61628. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  61629. break;
  61630. }
  61631. case DUK_TOK_WITH: {
  61632. DUK_DDD(DUK_DDDPRINT("with statement"));
  61633. comp_ctx->curr_func.with_depth++;
  61634. duk__parse_with_stmt(comp_ctx, res);
  61635. if (label_id >= 0) {
  61636. duk__patch_jump_here(comp_ctx, pc_at_entry + 1); /* break jump */
  61637. }
  61638. comp_ctx->curr_func.with_depth--;
  61639. stmt_flags = 0;
  61640. break;
  61641. }
  61642. case DUK_TOK_SWITCH: {
  61643. /*
  61644. * The switch statement is pretty messy to compile.
  61645. * See the helper for details.
  61646. */
  61647. DUK_DDD(DUK_DDDPRINT("switch statement"));
  61648. DUK_ASSERT(label_id >= 0);
  61649. duk__update_label_flags(comp_ctx,
  61650. label_id,
  61651. DUK_LABEL_FLAG_ALLOW_BREAK); /* don't allow continue */
  61652. duk__parse_switch_stmt(comp_ctx, res, pc_at_entry);
  61653. stmt_flags = 0;
  61654. break;
  61655. }
  61656. case DUK_TOK_THROW: {
  61657. DUK_DDD(DUK_DDDPRINT("throw statement"));
  61658. duk__parse_throw_stmt(comp_ctx, res);
  61659. stmt_flags = DUK__HAS_TERM | DUK__IS_TERMINAL;
  61660. break;
  61661. }
  61662. case DUK_TOK_TRY: {
  61663. DUK_DDD(DUK_DDDPRINT("try statement"));
  61664. duk__parse_try_stmt(comp_ctx, res);
  61665. stmt_flags = 0;
  61666. break;
  61667. }
  61668. case DUK_TOK_DEBUGGER: {
  61669. duk__advance(comp_ctx);
  61670. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  61671. DUK_DDD(DUK_DDDPRINT("debugger statement: debugging enabled, emit debugger opcode"));
  61672. duk__emit_op_only(comp_ctx, DUK_OP_DEBUGGER);
  61673. #else
  61674. DUK_DDD(DUK_DDDPRINT("debugger statement: ignored"));
  61675. #endif
  61676. stmt_flags = DUK__HAS_TERM;
  61677. break;
  61678. }
  61679. default: {
  61680. /*
  61681. * Else, must be one of:
  61682. * - ExpressionStatement, possibly a directive (String)
  61683. * - LabelledStatement (Identifier followed by ':')
  61684. *
  61685. * Expressions beginning with 'function' keyword are covered by a case
  61686. * above (such expressions are not allowed in standard E5 anyway).
  61687. * Also expressions starting with '{' are interpreted as block
  61688. * statements. See E5 Section 12.4.
  61689. *
  61690. * Directive detection is tricky; see E5 Section 14.1 on directive
  61691. * prologue. A directive is an expression statement with a single
  61692. * string literal and an explicit or automatic semicolon. Escape
  61693. * characters are significant and no parens etc are allowed:
  61694. *
  61695. * 'use strict'; // valid 'use strict' directive
  61696. * 'use\u0020strict'; // valid directive, not a 'use strict' directive
  61697. * ('use strict'); // not a valid directive
  61698. *
  61699. * The expression is determined to consist of a single string literal
  61700. * based on duk__expr_nud() and duk__expr_led() call counts. The string literal
  61701. * of a 'use strict' directive is determined to lack any escapes based
  61702. * num_escapes count from the lexer. Note that other directives may be
  61703. * allowed to contain escapes, so a directive with escapes does not
  61704. * terminate a directive prologue.
  61705. *
  61706. * We rely on the fact that the expression parser will not emit any
  61707. * code for a single token expression. However, it will generate an
  61708. * intermediate value which we will then successfully ignore.
  61709. *
  61710. * A similar approach is used for labels.
  61711. */
  61712. duk_bool_t single_token;
  61713. DUK_DDD(DUK_DDDPRINT("expression statement"));
  61714. duk__exprtop(comp_ctx, res, DUK__BP_FOR_EXPR /*rbp_flags*/);
  61715. single_token = (comp_ctx->curr_func.nud_count == 1 && /* one token */
  61716. comp_ctx->curr_func.led_count == 0); /* no operators */
  61717. if (single_token &&
  61718. comp_ctx->prev_token.t == DUK_TOK_IDENTIFIER &&
  61719. comp_ctx->curr_token.t == DUK_TOK_COLON) {
  61720. /*
  61721. * Detected label
  61722. */
  61723. duk_hstring *h_lab;
  61724. /* expected ival */
  61725. DUK_ASSERT(res->t == DUK_IVAL_VAR);
  61726. DUK_ASSERT(res->x1.t == DUK_ISPEC_VALUE);
  61727. DUK_ASSERT(DUK_TVAL_IS_STRING(duk_get_tval(thr, res->x1.valstack_idx)));
  61728. h_lab = comp_ctx->prev_token.str1;
  61729. DUK_ASSERT(h_lab != NULL);
  61730. DUK_DDD(DUK_DDDPRINT("explicit label site for label '%!O'",
  61731. (duk_heaphdr *) h_lab));
  61732. duk__advance(comp_ctx); /* eat colon */
  61733. label_id = duk__stmt_label_site(comp_ctx, label_id);
  61734. duk__add_label(comp_ctx,
  61735. h_lab,
  61736. pc_at_entry /*pc_label*/,
  61737. label_id);
  61738. /* a statement following a label cannot be a source element
  61739. * (a function declaration).
  61740. */
  61741. allow_source_elem = 0;
  61742. DUK_DDD(DUK_DDDPRINT("label handled, retry statement parsing"));
  61743. goto retry_parse;
  61744. }
  61745. stmt_flags = 0;
  61746. if (dir_prol_at_entry && /* still in prologue */
  61747. single_token && /* single string token */
  61748. comp_ctx->prev_token.t == DUK_TOK_STRING) {
  61749. /*
  61750. * Detected a directive
  61751. */
  61752. duk_hstring *h_dir;
  61753. /* expected ival */
  61754. DUK_ASSERT(res->t == DUK_IVAL_PLAIN);
  61755. DUK_ASSERT(res->x1.t == DUK_ISPEC_VALUE);
  61756. DUK_ASSERT(DUK_TVAL_IS_STRING(duk_get_tval(thr, res->x1.valstack_idx)));
  61757. h_dir = comp_ctx->prev_token.str1;
  61758. DUK_ASSERT(h_dir != NULL);
  61759. DUK_DDD(DUK_DDDPRINT("potential directive: %!O", h_dir));
  61760. stmt_flags |= DUK__STILL_PROLOGUE;
  61761. /* Note: escaped characters differentiate directives */
  61762. if (comp_ctx->prev_token.num_escapes > 0) {
  61763. DUK_DDD(DUK_DDDPRINT("directive contains escapes: valid directive "
  61764. "but we ignore such directives"));
  61765. } else {
  61766. /*
  61767. * The length comparisons are present to handle
  61768. * strings like "use strict\u0000foo" as required.
  61769. */
  61770. if (DUK_HSTRING_GET_BYTELEN(h_dir) == 10 &&
  61771. DUK_STRCMP((const char *) DUK_HSTRING_GET_DATA(h_dir), "use strict") == 0) {
  61772. #if defined(DUK_USE_STRICT_DECL)
  61773. DUK_DDD(DUK_DDDPRINT("use strict directive detected: strict flag %ld -> %ld",
  61774. (long) comp_ctx->curr_func.is_strict, (long) 1));
  61775. comp_ctx->curr_func.is_strict = 1;
  61776. #else
  61777. DUK_DDD(DUK_DDDPRINT("use strict detected but strict declarations disabled, ignoring"));
  61778. #endif
  61779. } else if (DUK_HSTRING_GET_BYTELEN(h_dir) == 14 &&
  61780. DUK_STRCMP((const char *) DUK_HSTRING_GET_DATA(h_dir), "use duk notail") == 0) {
  61781. DUK_DDD(DUK_DDDPRINT("use duk notail directive detected: notail flag %ld -> %ld",
  61782. (long) comp_ctx->curr_func.is_notail, (long) 1));
  61783. comp_ctx->curr_func.is_notail = 1;
  61784. } else {
  61785. DUK_DD(DUK_DDPRINT("unknown directive: '%!O', ignoring but not terminating "
  61786. "directive prologue", (duk_hobject *) h_dir));
  61787. }
  61788. }
  61789. } else {
  61790. DUK_DDD(DUK_DDDPRINT("non-directive expression statement or no longer in prologue; "
  61791. "prologue terminated if still active"));
  61792. }
  61793. stmt_flags |= DUK__HAS_VAL | DUK__HAS_TERM;
  61794. }
  61795. } /* end switch (tok) */
  61796. /*
  61797. * Statement value handling.
  61798. *
  61799. * Global code and eval code has an implicit return value
  61800. * which comes from the last statement with a value
  61801. * (technically a non-"empty" continuation, which is
  61802. * different from an empty statement).
  61803. *
  61804. * Since we don't know whether a later statement will
  61805. * override the value of the current statement, we need
  61806. * to coerce the statement value to a register allocated
  61807. * for implicit return values. In other cases we need
  61808. * to coerce the statement value to a plain value to get
  61809. * any side effects out (consider e.g. "foo.bar;").
  61810. */
  61811. /* XXX: what about statements which leave a half-cooked value in 'res'
  61812. * but have no stmt value? Any such statements?
  61813. */
  61814. if (stmt_flags & DUK__HAS_VAL) {
  61815. duk_regconst_t reg_stmt_value = comp_ctx->curr_func.reg_stmt_value;
  61816. if (reg_stmt_value >= 0) {
  61817. duk__ivalue_toforcedreg(comp_ctx, res, reg_stmt_value);
  61818. } else {
  61819. duk__ivalue_toplain_ignore(comp_ctx, res);
  61820. }
  61821. } else {
  61822. ;
  61823. }
  61824. /*
  61825. * Statement terminator check, including automatic semicolon
  61826. * handling. After this step, 'curr_tok' should be the first
  61827. * token after a possible statement terminator.
  61828. */
  61829. if (stmt_flags & DUK__HAS_TERM) {
  61830. if (comp_ctx->curr_token.t == DUK_TOK_SEMICOLON) {
  61831. DUK_DDD(DUK_DDDPRINT("explicit semicolon terminates statement"));
  61832. duk__advance(comp_ctx);
  61833. } else {
  61834. if (comp_ctx->curr_token.allow_auto_semi) {
  61835. DUK_DDD(DUK_DDDPRINT("automatic semicolon terminates statement"));
  61836. } else if (stmt_flags & DUK__ALLOW_AUTO_SEMI_ALWAYS) {
  61837. /* XXX: make this lenience dependent on flags or strictness? */
  61838. DUK_DDD(DUK_DDDPRINT("automatic semicolon terminates statement (allowed for compatibility "
  61839. "even though no lineterm present before next token)"));
  61840. } else {
  61841. DUK_ERROR_SYNTAX(thr, DUK_STR_UNTERMINATED_STMT);
  61842. DUK_WO_NORETURN(return;);
  61843. }
  61844. }
  61845. } else {
  61846. DUK_DDD(DUK_DDDPRINT("statement has no terminator"));
  61847. }
  61848. /*
  61849. * Directive prologue tracking.
  61850. */
  61851. if (stmt_flags & DUK__STILL_PROLOGUE) {
  61852. DUK_DDD(DUK_DDDPRINT("setting in_directive_prologue"));
  61853. comp_ctx->curr_func.in_directive_prologue = 1;
  61854. }
  61855. /*
  61856. * Cleanups (all statement parsing flows through here).
  61857. *
  61858. * Pop label site and reset labels. Reset 'next temp' to value at
  61859. * entry to reuse temps.
  61860. */
  61861. if (label_id >= 0) {
  61862. duk__emit_bc(comp_ctx,
  61863. DUK_OP_ENDLABEL,
  61864. (duk_regconst_t) label_id);
  61865. }
  61866. DUK__SETTEMP(comp_ctx, temp_at_entry);
  61867. duk__reset_labels_to_length(comp_ctx, labels_len_at_entry);
  61868. /* XXX: return indication of "terminalness" (e.g. a 'throw' is terminal) */
  61869. DUK__RECURSION_DECREASE(comp_ctx, thr);
  61870. }
  61871. /*
  61872. * Parse a statement list.
  61873. *
  61874. * Handles automatic semicolon insertion and implicit return value.
  61875. *
  61876. * Upon entry, 'curr_tok' should contain the first token of the first
  61877. * statement (parsed in the "allow regexp literal" mode). Upon exit,
  61878. * 'curr_tok' contains the token following the statement list terminator
  61879. * (EOF or closing brace).
  61880. */
  61881. DUK_LOCAL void duk__parse_stmts(duk_compiler_ctx *comp_ctx, duk_bool_t allow_source_elem, duk_bool_t expect_eof, duk_bool_t regexp_after) {
  61882. duk_hthread *thr = comp_ctx->thr;
  61883. duk_ivalue res_alloc;
  61884. duk_ivalue *res = &res_alloc;
  61885. /* Setup state. Initial ivalue is 'undefined'. */
  61886. duk_require_stack(thr, DUK__PARSE_STATEMENTS_SLOTS);
  61887. /* XXX: 'res' setup can be moved to function body level; in fact, two 'res'
  61888. * intermediate values suffice for parsing of each function. Nesting is needed
  61889. * for nested functions (which may occur inside expressions).
  61890. */
  61891. duk_memzero(&res_alloc, sizeof(res_alloc));
  61892. res->t = DUK_IVAL_PLAIN;
  61893. res->x1.t = DUK_ISPEC_VALUE;
  61894. res->x1.valstack_idx = duk_get_top(thr);
  61895. res->x2.valstack_idx = res->x1.valstack_idx + 1;
  61896. duk_push_undefined(thr);
  61897. duk_push_undefined(thr);
  61898. /* Parse statements until a closing token (EOF or '}') is found. */
  61899. for (;;) {
  61900. /* Check whether statement list ends. */
  61901. if (expect_eof) {
  61902. if (comp_ctx->curr_token.t == DUK_TOK_EOF) {
  61903. break;
  61904. }
  61905. } else {
  61906. if (comp_ctx->curr_token.t == DUK_TOK_RCURLY) {
  61907. break;
  61908. }
  61909. }
  61910. /* Check statement type based on the first token type.
  61911. *
  61912. * Note: expression parsing helpers expect 'curr_tok' to
  61913. * contain the first token of the expression upon entry.
  61914. */
  61915. DUK_DDD(DUK_DDDPRINT("TOKEN %ld (non-whitespace, non-comment)", (long) comp_ctx->curr_token.t));
  61916. duk__parse_stmt(comp_ctx, res, allow_source_elem);
  61917. }
  61918. /* RegExp is allowed / not allowed depending on context. For function
  61919. * declarations RegExp is allowed because it follows a function
  61920. * declaration statement and may appear as part of the next statement.
  61921. * For function expressions RegExp is not allowed, and it's possible
  61922. * to do something like '(function () {} / 123)'.
  61923. */
  61924. if (regexp_after) {
  61925. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  61926. }
  61927. duk__advance(comp_ctx);
  61928. /* Tear down state. */
  61929. duk_pop_2(thr);
  61930. }
  61931. /*
  61932. * Declaration binding instantiation conceptually happens when calling a
  61933. * function; for us it essentially means that function prologue. The
  61934. * conceptual process is described in E5 Section 10.5.
  61935. *
  61936. * We need to keep track of all encountered identifiers to (1) create an
  61937. * identifier-to-register map ("varmap"); and (2) detect duplicate
  61938. * declarations. Identifiers which are not bound to registers still need
  61939. * to be tracked for detecting duplicates. Currently such identifiers
  61940. * are put into the varmap with a 'null' value, which is later cleaned up.
  61941. *
  61942. * To support functions with a large number of variable and function
  61943. * declarations, registers are not allocated beyond a certain limit;
  61944. * after that limit, variables and functions need slow path access.
  61945. * Arguments are currently always register bound, which imposes a hard
  61946. * (and relatively small) argument count limit.
  61947. *
  61948. * Some bindings in E5 are not configurable (= deletable) and almost all
  61949. * are mutable (writable). Exceptions are:
  61950. *
  61951. * - The 'arguments' binding, established only if no shadowing argument
  61952. * or function declaration exists. We handle 'arguments' creation
  61953. * and binding through an explicit slow path environment record.
  61954. *
  61955. * - The "name" binding for a named function expression. This is also
  61956. * handled through an explicit slow path environment record.
  61957. */
  61958. /* XXX: add support for variables to not be register bound always, to
  61959. * handle cases with a very large number of variables?
  61960. */
  61961. DUK_LOCAL void duk__init_varmap_and_prologue_for_pass2(duk_compiler_ctx *comp_ctx, duk_regconst_t *out_stmt_value_reg) {
  61962. duk_hthread *thr = comp_ctx->thr;
  61963. duk_hstring *h_name;
  61964. duk_bool_t configurable_bindings;
  61965. duk_uarridx_t num_args;
  61966. duk_uarridx_t num_decls;
  61967. duk_regconst_t rc_name;
  61968. duk_small_uint_t declvar_flags;
  61969. duk_uarridx_t i;
  61970. #if defined(DUK_USE_ASSERTIONS)
  61971. duk_idx_t entry_top;
  61972. #endif
  61973. #if defined(DUK_USE_ASSERTIONS)
  61974. entry_top = duk_get_top(thr);
  61975. #endif
  61976. /*
  61977. * Preliminaries
  61978. */
  61979. configurable_bindings = comp_ctx->curr_func.is_eval;
  61980. DUK_DDD(DUK_DDDPRINT("configurable_bindings=%ld", (long) configurable_bindings));
  61981. /* varmap is already in comp_ctx->curr_func.varmap_idx */
  61982. /*
  61983. * Function formal arguments, always bound to registers
  61984. * (there's no support for shuffling them now).
  61985. */
  61986. num_args = (duk_uarridx_t) duk_get_length(thr, comp_ctx->curr_func.argnames_idx);
  61987. DUK_DDD(DUK_DDDPRINT("num_args=%ld", (long) num_args));
  61988. /* XXX: check num_args */
  61989. for (i = 0; i < num_args; i++) {
  61990. duk_get_prop_index(thr, comp_ctx->curr_func.argnames_idx, i);
  61991. h_name = duk_known_hstring(thr, -1);
  61992. if (comp_ctx->curr_func.is_strict) {
  61993. if (duk__hstring_is_eval_or_arguments(comp_ctx, h_name)) {
  61994. DUK_DDD(DUK_DDDPRINT("arg named 'eval' or 'arguments' in strict mode -> SyntaxError"));
  61995. goto error_argname;
  61996. }
  61997. duk_dup_top(thr);
  61998. if (duk_has_prop(thr, comp_ctx->curr_func.varmap_idx)) {
  61999. DUK_DDD(DUK_DDDPRINT("duplicate arg name in strict mode -> SyntaxError"));
  62000. goto error_argname;
  62001. }
  62002. /* Ensure argument name is not a reserved word in current
  62003. * (final) strictness. Formal argument parsing may not
  62004. * catch reserved names if strictness changes during
  62005. * parsing.
  62006. *
  62007. * We only need to do this in strict mode because non-strict
  62008. * keyword are always detected in formal argument parsing.
  62009. */
  62010. if (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(h_name)) {
  62011. goto error_argname;
  62012. }
  62013. }
  62014. /* overwrite any previous binding of the same name; the effect is
  62015. * that last argument of a certain name wins.
  62016. */
  62017. /* only functions can have arguments */
  62018. DUK_ASSERT(comp_ctx->curr_func.is_function);
  62019. duk_push_uarridx(thr, i); /* -> [ ... name index ] */
  62020. duk_put_prop(thr, comp_ctx->curr_func.varmap_idx); /* -> [ ... ] */
  62021. /* no code needs to be emitted, the regs already have values */
  62022. }
  62023. /* use temp_next for tracking register allocations */
  62024. DUK__SETTEMP_CHECKMAX(comp_ctx, (duk_regconst_t) num_args);
  62025. /*
  62026. * After arguments, allocate special registers (like shuffling temps)
  62027. */
  62028. if (out_stmt_value_reg) {
  62029. *out_stmt_value_reg = DUK__ALLOCTEMP(comp_ctx);
  62030. }
  62031. if (comp_ctx->curr_func.needs_shuffle) {
  62032. duk_regconst_t shuffle_base = DUK__ALLOCTEMPS(comp_ctx, 3);
  62033. comp_ctx->curr_func.shuffle1 = shuffle_base;
  62034. comp_ctx->curr_func.shuffle2 = shuffle_base + 1;
  62035. comp_ctx->curr_func.shuffle3 = shuffle_base + 2;
  62036. DUK_D(DUK_DPRINT("shuffle registers needed by function, allocated: %ld %ld %ld",
  62037. (long) comp_ctx->curr_func.shuffle1,
  62038. (long) comp_ctx->curr_func.shuffle2,
  62039. (long) comp_ctx->curr_func.shuffle3));
  62040. }
  62041. if (comp_ctx->curr_func.temp_next > 0x100) {
  62042. DUK_D(DUK_DPRINT("not enough 8-bit regs: temp_next=%ld", (long) comp_ctx->curr_func.temp_next));
  62043. goto error_outofregs;
  62044. }
  62045. /*
  62046. * Function declarations
  62047. */
  62048. num_decls = (duk_uarridx_t) duk_get_length(thr, comp_ctx->curr_func.decls_idx);
  62049. DUK_DDD(DUK_DDDPRINT("num_decls=%ld -> %!T",
  62050. (long) num_decls,
  62051. (duk_tval *) duk_get_tval(thr, comp_ctx->curr_func.decls_idx)));
  62052. for (i = 0; i < num_decls; i += 2) {
  62053. duk_int_t decl_type;
  62054. duk_int_t fnum;
  62055. duk_get_prop_index(thr, comp_ctx->curr_func.decls_idx, i + 1); /* decl type */
  62056. decl_type = duk_to_int(thr, -1);
  62057. fnum = decl_type >> 8; /* XXX: macros */
  62058. decl_type = decl_type & 0xff;
  62059. duk_pop(thr);
  62060. if (decl_type != DUK_DECL_TYPE_FUNC) {
  62061. continue;
  62062. }
  62063. duk_get_prop_index(thr, comp_ctx->curr_func.decls_idx, i); /* decl name */
  62064. /* XXX: spilling */
  62065. if (comp_ctx->curr_func.is_function) {
  62066. duk_regconst_t reg_bind;
  62067. duk_dup_top(thr);
  62068. if (duk_has_prop(thr, comp_ctx->curr_func.varmap_idx)) {
  62069. /* shadowed; update value */
  62070. duk_dup_top(thr);
  62071. duk_get_prop(thr, comp_ctx->curr_func.varmap_idx);
  62072. reg_bind = duk_to_int(thr, -1); /* [ ... name reg_bind ] */
  62073. duk__emit_a_bc(comp_ctx,
  62074. DUK_OP_CLOSURE,
  62075. reg_bind,
  62076. (duk_regconst_t) fnum);
  62077. } else {
  62078. /* function: always register bound */
  62079. reg_bind = DUK__ALLOCTEMP(comp_ctx);
  62080. duk__emit_a_bc(comp_ctx,
  62081. DUK_OP_CLOSURE,
  62082. reg_bind,
  62083. (duk_regconst_t) fnum);
  62084. duk_push_int(thr, (duk_int_t) reg_bind);
  62085. }
  62086. } else {
  62087. /* Function declaration for global/eval code is emitted even
  62088. * for duplicates, because of E5 Section 10.5, step 5.e of
  62089. * E5.1 (special behavior for variable bound to global object).
  62090. *
  62091. * DECLVAR will not re-declare a variable as such, but will
  62092. * update the binding value.
  62093. */
  62094. duk_regconst_t reg_temp = DUK__ALLOCTEMP(comp_ctx);
  62095. duk_dup_top(thr);
  62096. rc_name = duk__getconst(comp_ctx);
  62097. duk_push_null(thr);
  62098. duk__emit_a_bc(comp_ctx,
  62099. DUK_OP_CLOSURE,
  62100. reg_temp,
  62101. (duk_regconst_t) fnum);
  62102. declvar_flags = DUK_PROPDESC_FLAG_WRITABLE |
  62103. DUK_PROPDESC_FLAG_ENUMERABLE |
  62104. DUK_BC_DECLVAR_FLAG_FUNC_DECL;
  62105. if (configurable_bindings) {
  62106. declvar_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  62107. }
  62108. duk__emit_a_b_c(comp_ctx,
  62109. DUK_OP_DECLVAR | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_BC_REGCONST,
  62110. (duk_regconst_t) declvar_flags /*flags*/,
  62111. rc_name /*name*/,
  62112. reg_temp /*value*/);
  62113. DUK__SETTEMP(comp_ctx, reg_temp); /* forget temp */
  62114. }
  62115. DUK_DDD(DUK_DDDPRINT("function declaration to varmap: %!T -> %!T",
  62116. (duk_tval *) duk_get_tval(thr, -2),
  62117. (duk_tval *) duk_get_tval(thr, -1)));
  62118. #if defined(DUK_USE_FASTINT)
  62119. DUK_ASSERT(DUK_TVAL_IS_NULL(duk_get_tval(thr, -1)) || DUK_TVAL_IS_FASTINT(duk_get_tval(thr, -1)));
  62120. #endif
  62121. duk_put_prop(thr, comp_ctx->curr_func.varmap_idx); /* [ ... name reg/null ] -> [ ... ] */
  62122. }
  62123. /*
  62124. * 'arguments' binding is special; if a shadowing argument or
  62125. * function declaration exists, an arguments object will
  62126. * definitely not be needed, regardless of whether the identifier
  62127. * 'arguments' is referenced inside the function body.
  62128. */
  62129. if (duk_has_prop_stridx(thr, comp_ctx->curr_func.varmap_idx, DUK_STRIDX_LC_ARGUMENTS)) {
  62130. DUK_DDD(DUK_DDDPRINT("'arguments' is shadowed by argument or function declaration "
  62131. "-> arguments object creation can be skipped"));
  62132. comp_ctx->curr_func.is_arguments_shadowed = 1;
  62133. }
  62134. /*
  62135. * Variable declarations.
  62136. *
  62137. * Unlike function declarations, variable declaration values don't get
  62138. * assigned on entry. If a binding of the same name already exists, just
  62139. * ignore it silently.
  62140. */
  62141. for (i = 0; i < num_decls; i += 2) {
  62142. duk_int_t decl_type;
  62143. duk_get_prop_index(thr, comp_ctx->curr_func.decls_idx, i + 1); /* decl type */
  62144. decl_type = duk_to_int(thr, -1);
  62145. decl_type = decl_type & 0xff;
  62146. duk_pop(thr);
  62147. if (decl_type != DUK_DECL_TYPE_VAR) {
  62148. continue;
  62149. }
  62150. duk_get_prop_index(thr, comp_ctx->curr_func.decls_idx, i); /* decl name */
  62151. if (duk_has_prop(thr, comp_ctx->curr_func.varmap_idx)) {
  62152. /* shadowed, ignore */
  62153. } else {
  62154. duk_get_prop_index(thr, comp_ctx->curr_func.decls_idx, i); /* decl name */
  62155. h_name = duk_known_hstring(thr, -1);
  62156. if (h_name == DUK_HTHREAD_STRING_LC_ARGUMENTS(thr) &&
  62157. !comp_ctx->curr_func.is_arguments_shadowed) {
  62158. /* E5 Section steps 7-8 */
  62159. DUK_DDD(DUK_DDDPRINT("'arguments' not shadowed by a function declaration, "
  62160. "but appears as a variable declaration -> treat as "
  62161. "a no-op for variable declaration purposes"));
  62162. duk_pop(thr);
  62163. continue;
  62164. }
  62165. /* XXX: spilling */
  62166. if (comp_ctx->curr_func.is_function) {
  62167. duk_regconst_t reg_bind = DUK__ALLOCTEMP(comp_ctx);
  62168. /* no need to init reg, it will be undefined on entry */
  62169. duk_push_int(thr, (duk_int_t) reg_bind);
  62170. } else {
  62171. duk_dup_top(thr);
  62172. rc_name = duk__getconst(comp_ctx);
  62173. duk_push_null(thr);
  62174. declvar_flags = DUK_PROPDESC_FLAG_WRITABLE |
  62175. DUK_PROPDESC_FLAG_ENUMERABLE;
  62176. if (configurable_bindings) {
  62177. declvar_flags |= DUK_PROPDESC_FLAG_CONFIGURABLE;
  62178. }
  62179. duk__emit_a_b_c(comp_ctx,
  62180. DUK_OP_DECLVAR | DUK__EMIT_FLAG_NO_SHUFFLE_A | DUK__EMIT_FLAG_BC_REGCONST,
  62181. (duk_regconst_t) declvar_flags /*flags*/,
  62182. rc_name /*name*/,
  62183. 0 /*value*/);
  62184. }
  62185. duk_put_prop(thr, comp_ctx->curr_func.varmap_idx); /* [ ... name reg/null ] -> [ ... ] */
  62186. }
  62187. }
  62188. /*
  62189. * Wrap up
  62190. */
  62191. DUK_DDD(DUK_DDDPRINT("varmap: %!T, is_arguments_shadowed=%ld",
  62192. (duk_tval *) duk_get_tval(thr, comp_ctx->curr_func.varmap_idx),
  62193. (long) comp_ctx->curr_func.is_arguments_shadowed));
  62194. DUK_ASSERT_TOP(thr, entry_top);
  62195. return;
  62196. error_outofregs:
  62197. DUK_ERROR_RANGE(thr, DUK_STR_REG_LIMIT);
  62198. DUK_WO_NORETURN(return;);
  62199. error_argname:
  62200. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_ARG_NAME);
  62201. DUK_WO_NORETURN(return;);
  62202. }
  62203. /*
  62204. * Parse a function-body-like expression (FunctionBody or Program
  62205. * in E5 grammar) using a two-pass parse. The productions appear
  62206. * in the following contexts:
  62207. *
  62208. * - function expression
  62209. * - function statement
  62210. * - function declaration
  62211. * - getter in object literal
  62212. * - setter in object literal
  62213. * - global code
  62214. * - eval code
  62215. * - Function constructor body
  62216. *
  62217. * This function only parses the statement list of the body; the argument
  62218. * list and possible function name must be initialized by the caller.
  62219. * For instance, for Function constructor, the argument names are originally
  62220. * on the value stack. The parsing of statements ends either at an EOF or
  62221. * a closing brace; this is controlled by an input flag.
  62222. *
  62223. * Note that there are many differences affecting parsing and even code
  62224. * generation:
  62225. *
  62226. * - Global and eval code have an implicit return value generated
  62227. * by the last statement; function code does not
  62228. *
  62229. * - Global code, eval code, and Function constructor body end in
  62230. * an EOF, other bodies in a closing brace ('}')
  62231. *
  62232. * Upon entry, 'curr_tok' is ignored and the function will pull in the
  62233. * first token on its own. Upon exit, 'curr_tok' is the terminating
  62234. * token (EOF or closing brace).
  62235. */
  62236. DUK_LOCAL void duk__parse_func_body(duk_compiler_ctx *comp_ctx, duk_bool_t expect_eof, duk_bool_t implicit_return_value, duk_bool_t regexp_after, duk_small_int_t expect_token) {
  62237. duk_compiler_func *func;
  62238. duk_hthread *thr;
  62239. duk_regconst_t reg_stmt_value = -1;
  62240. duk_lexer_point lex_pt;
  62241. duk_regconst_t temp_first;
  62242. duk_small_int_t compile_round = 1;
  62243. DUK_ASSERT(comp_ctx != NULL);
  62244. thr = comp_ctx->thr;
  62245. DUK_ASSERT(thr != NULL);
  62246. func = &comp_ctx->curr_func;
  62247. DUK_ASSERT(func != NULL);
  62248. DUK__RECURSION_INCREASE(comp_ctx, thr);
  62249. duk_require_stack(thr, DUK__FUNCTION_BODY_REQUIRE_SLOTS);
  62250. /*
  62251. * Store lexer position for a later rewind
  62252. */
  62253. DUK_LEXER_GETPOINT(&comp_ctx->lex, &lex_pt);
  62254. /*
  62255. * Program code (global and eval code) has an implicit return value
  62256. * from the last statement value (e.g. eval("1; 2+3;") returns 3).
  62257. * This is not the case with functions. If implicit statement return
  62258. * value is requested, all statements are coerced to a register
  62259. * allocated here, and used in the implicit return statement below.
  62260. */
  62261. /* XXX: this is pointless here because pass 1 is throw-away */
  62262. if (implicit_return_value) {
  62263. reg_stmt_value = DUK__ALLOCTEMP(comp_ctx);
  62264. /* If an implicit return value is needed by caller, it must be
  62265. * initialized to 'undefined' because we don't know whether any
  62266. * non-empty (where "empty" is a continuation type, and different
  62267. * from an empty statement) statements will be executed.
  62268. *
  62269. * However, since 1st pass is a throwaway one, no need to emit
  62270. * it here.
  62271. */
  62272. #if 0
  62273. duk__emit_bc(comp_ctx,
  62274. DUK_OP_LDUNDEF,
  62275. 0);
  62276. #endif
  62277. }
  62278. /*
  62279. * First pass.
  62280. *
  62281. * Gather variable/function declarations needed for second pass.
  62282. * Code generated is dummy and discarded.
  62283. */
  62284. func->in_directive_prologue = 1;
  62285. func->in_scanning = 1;
  62286. func->may_direct_eval = 0;
  62287. func->id_access_arguments = 0;
  62288. func->id_access_slow = 0;
  62289. func->id_access_slow_own = 0;
  62290. func->reg_stmt_value = reg_stmt_value;
  62291. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  62292. func->min_line = DUK_INT_MAX;
  62293. func->max_line = 0;
  62294. #endif
  62295. /* duk__parse_stmts() expects curr_tok to be set; parse in "allow
  62296. * regexp literal" mode with current strictness.
  62297. */
  62298. if (expect_token >= 0) {
  62299. /* Eating a left curly; regexp mode is allowed by left curly
  62300. * based on duk__token_lbp[] automatically.
  62301. */
  62302. DUK_ASSERT(expect_token == DUK_TOK_LCURLY);
  62303. duk__update_lineinfo_currtoken(comp_ctx);
  62304. duk__advance_expect(comp_ctx, expect_token);
  62305. } else {
  62306. /* Need to set curr_token.t because lexing regexp mode depends on current
  62307. * token type. Zero value causes "allow regexp" mode.
  62308. */
  62309. comp_ctx->curr_token.t = 0;
  62310. duk__advance(comp_ctx);
  62311. }
  62312. DUK_DDD(DUK_DDDPRINT("begin 1st pass"));
  62313. duk__parse_stmts(comp_ctx,
  62314. 1, /* allow source elements */
  62315. expect_eof, /* expect EOF instead of } */
  62316. regexp_after); /* regexp after */
  62317. DUK_DDD(DUK_DDDPRINT("end 1st pass"));
  62318. /*
  62319. * Second (and possibly third) pass.
  62320. *
  62321. * Generate actual code. In most cases the need for shuffle
  62322. * registers is detected during pass 1, but in some corner cases
  62323. * we'll only detect it during pass 2 and a third pass is then
  62324. * needed (see GH-115).
  62325. */
  62326. for (;;) {
  62327. duk_bool_t needs_shuffle_before = comp_ctx->curr_func.needs_shuffle;
  62328. compile_round++;
  62329. /*
  62330. * Rewind lexer.
  62331. *
  62332. * duk__parse_stmts() expects curr_tok to be set; parse in "allow regexp
  62333. * literal" mode with current strictness.
  62334. *
  62335. * curr_token line number info should be initialized for pass 2 before
  62336. * generating prologue, to ensure prologue bytecode gets nice line numbers.
  62337. */
  62338. DUK_DDD(DUK_DDDPRINT("rewind lexer"));
  62339. DUK_LEXER_SETPOINT(&comp_ctx->lex, &lex_pt);
  62340. comp_ctx->curr_token.t = 0; /* this is needed for regexp mode */
  62341. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  62342. duk__advance(comp_ctx);
  62343. /*
  62344. * Reset function state and perform register allocation, which creates
  62345. * 'varmap' for second pass. Function prologue for variable declarations,
  62346. * binding value initializations etc is emitted as a by-product.
  62347. *
  62348. * Strict mode restrictions for duplicate and invalid argument
  62349. * names are checked here now that we know whether the function
  62350. * is actually strict. See: test-dev-strict-mode-boundary.js.
  62351. *
  62352. * Inner functions are compiled during pass 1 and are not reset.
  62353. */
  62354. duk__reset_func_for_pass2(comp_ctx);
  62355. func->in_directive_prologue = 1;
  62356. func->in_scanning = 0;
  62357. /* must be able to emit code, alloc consts, etc. */
  62358. duk__init_varmap_and_prologue_for_pass2(comp_ctx,
  62359. (implicit_return_value ? &reg_stmt_value : NULL));
  62360. func->reg_stmt_value = reg_stmt_value;
  62361. temp_first = DUK__GETTEMP(comp_ctx);
  62362. func->temp_first = temp_first;
  62363. func->temp_next = temp_first;
  62364. func->stmt_next = 0;
  62365. func->label_next = 0;
  62366. /* XXX: init or assert catch depth etc -- all values */
  62367. func->id_access_arguments = 0;
  62368. func->id_access_slow = 0;
  62369. func->id_access_slow_own = 0;
  62370. /*
  62371. * Check function name validity now that we know strictness.
  62372. * This only applies to function declarations and expressions,
  62373. * not setter/getter name.
  62374. *
  62375. * See: test-dev-strict-mode-boundary.js
  62376. */
  62377. if (func->is_function && !func->is_setget && func->h_name != NULL) {
  62378. if (func->is_strict) {
  62379. if (duk__hstring_is_eval_or_arguments(comp_ctx, func->h_name)) {
  62380. DUK_DDD(DUK_DDDPRINT("func name is 'eval' or 'arguments' in strict mode"));
  62381. goto error_funcname;
  62382. }
  62383. if (DUK_HSTRING_HAS_STRICT_RESERVED_WORD(func->h_name)) {
  62384. DUK_DDD(DUK_DDDPRINT("func name is a reserved word in strict mode"));
  62385. goto error_funcname;
  62386. }
  62387. } else {
  62388. if (DUK_HSTRING_HAS_RESERVED_WORD(func->h_name) &&
  62389. !DUK_HSTRING_HAS_STRICT_RESERVED_WORD(func->h_name)) {
  62390. DUK_DDD(DUK_DDDPRINT("func name is a reserved word in non-strict mode"));
  62391. goto error_funcname;
  62392. }
  62393. }
  62394. }
  62395. /*
  62396. * Second pass parsing.
  62397. */
  62398. if (implicit_return_value) {
  62399. /* Default implicit return value. */
  62400. duk__emit_bc(comp_ctx,
  62401. DUK_OP_LDUNDEF,
  62402. 0);
  62403. }
  62404. DUK_DDD(DUK_DDDPRINT("begin 2nd pass"));
  62405. duk__parse_stmts(comp_ctx,
  62406. 1, /* allow source elements */
  62407. expect_eof, /* expect EOF instead of } */
  62408. regexp_after); /* regexp after */
  62409. DUK_DDD(DUK_DDDPRINT("end 2nd pass"));
  62410. duk__update_lineinfo_currtoken(comp_ctx);
  62411. if (needs_shuffle_before == comp_ctx->curr_func.needs_shuffle) {
  62412. /* Shuffle decision not changed. */
  62413. break;
  62414. }
  62415. if (compile_round >= 3) {
  62416. /* Should never happen but avoid infinite loop just in case. */
  62417. DUK_D(DUK_DPRINT("more than 3 compile passes needed, should never happen"));
  62418. DUK_ERROR_INTERNAL(thr);
  62419. DUK_WO_NORETURN(return;);
  62420. }
  62421. DUK_D(DUK_DPRINT("need additional round to compile function, round now %d", (int) compile_round));
  62422. }
  62423. /*
  62424. * Emit a final RETURN.
  62425. *
  62426. * It would be nice to avoid emitting an unnecessary "return" opcode
  62427. * if the current PC is not reachable. However, this cannot be reliably
  62428. * detected; even if the previous instruction is an unconditional jump,
  62429. * there may be a previous jump which jumps to current PC (which is the
  62430. * case for iteration and conditional statements, for instance).
  62431. */
  62432. /* XXX: request a "last statement is terminal" from duk__parse_stmt() and duk__parse_stmts();
  62433. * we could avoid the last RETURN if we could ensure there is no way to get here
  62434. * (directly or via a jump)
  62435. */
  62436. DUK_ASSERT(comp_ctx->curr_func.catch_depth == 0);
  62437. if (reg_stmt_value >= 0) {
  62438. DUK_ASSERT(DUK__ISREG(reg_stmt_value));
  62439. duk__emit_bc(comp_ctx, DUK_OP_RETREG, reg_stmt_value /*reg*/);
  62440. } else {
  62441. duk__emit_op_only(comp_ctx, DUK_OP_RETUNDEF);
  62442. }
  62443. /*
  62444. * Peephole optimize JUMP chains.
  62445. */
  62446. duk__peephole_optimize_bytecode(comp_ctx);
  62447. /*
  62448. * comp_ctx->curr_func is now ready to be converted into an actual
  62449. * function template.
  62450. */
  62451. DUK__RECURSION_DECREASE(comp_ctx, thr);
  62452. return;
  62453. error_funcname:
  62454. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_FUNC_NAME);
  62455. DUK_WO_NORETURN(return;);
  62456. }
  62457. /*
  62458. * Parse a function-like expression:
  62459. *
  62460. * - function expression
  62461. * - function declaration
  62462. * - function statement (non-standard)
  62463. * - setter/getter
  62464. *
  62465. * Adds the function to comp_ctx->curr_func function table and returns the
  62466. * function number.
  62467. *
  62468. * On entry, curr_token points to:
  62469. *
  62470. * - the token after 'function' for function expression/declaration/statement
  62471. * - the token after 'set' or 'get' for setter/getter
  62472. */
  62473. /* Parse formals. */
  62474. DUK_LOCAL void duk__parse_func_formals(duk_compiler_ctx *comp_ctx) {
  62475. duk_hthread *thr = comp_ctx->thr;
  62476. duk_bool_t first = 1;
  62477. duk_uarridx_t n;
  62478. for (;;) {
  62479. if (comp_ctx->curr_token.t == DUK_TOK_RPAREN) {
  62480. break;
  62481. }
  62482. if (first) {
  62483. /* no comma */
  62484. first = 0;
  62485. } else {
  62486. duk__advance_expect(comp_ctx, DUK_TOK_COMMA);
  62487. }
  62488. /* Note: when parsing a formal list in non-strict context, e.g.
  62489. * "implements" is parsed as an identifier. When the function is
  62490. * later detected to be strict, the argument list must be rechecked
  62491. * against a larger set of reserved words (that of strict mode).
  62492. * This is handled by duk__parse_func_body(). Here we recognize
  62493. * whatever tokens are considered reserved in current strictness
  62494. * (which is not always enough).
  62495. */
  62496. if (comp_ctx->curr_token.t != DUK_TOK_IDENTIFIER) {
  62497. DUK_ERROR_SYNTAX(thr, DUK_STR_EXPECTED_IDENTIFIER);
  62498. DUK_WO_NORETURN(return;);
  62499. }
  62500. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_IDENTIFIER);
  62501. DUK_ASSERT(comp_ctx->curr_token.str1 != NULL);
  62502. DUK_DDD(DUK_DDDPRINT("formal argument: %!O",
  62503. (duk_heaphdr *) comp_ctx->curr_token.str1));
  62504. /* XXX: append primitive */
  62505. duk_push_hstring(thr, comp_ctx->curr_token.str1);
  62506. n = (duk_uarridx_t) duk_get_length(thr, comp_ctx->curr_func.argnames_idx);
  62507. duk_put_prop_index(thr, comp_ctx->curr_func.argnames_idx, n);
  62508. duk__advance(comp_ctx); /* eat identifier */
  62509. }
  62510. }
  62511. /* Parse a function-like expression, assuming that 'comp_ctx->curr_func' is
  62512. * correctly set up. Assumes that curr_token is just after 'function' (or
  62513. * 'set'/'get' etc).
  62514. */
  62515. DUK_LOCAL void duk__parse_func_like_raw(duk_compiler_ctx *comp_ctx, duk_small_uint_t flags) {
  62516. duk_hthread *thr = comp_ctx->thr;
  62517. duk_token *tok;
  62518. duk_bool_t no_advance;
  62519. DUK_ASSERT(comp_ctx->curr_func.num_formals == 0);
  62520. DUK_ASSERT(comp_ctx->curr_func.is_function == 1);
  62521. DUK_ASSERT(comp_ctx->curr_func.is_eval == 0);
  62522. DUK_ASSERT(comp_ctx->curr_func.is_global == 0);
  62523. DUK_ASSERT(comp_ctx->curr_func.is_setget == ((flags & DUK__FUNC_FLAG_GETSET) != 0));
  62524. duk__update_lineinfo_currtoken(comp_ctx);
  62525. /*
  62526. * Function name (if any)
  62527. *
  62528. * We don't check for prohibited names here, because we don't
  62529. * yet know whether the function will be strict. Function body
  62530. * parsing handles this retroactively.
  62531. *
  62532. * For function expressions and declarations function name must
  62533. * be an Identifer (excludes reserved words). For setter/getter
  62534. * it is a PropertyName which allows reserved words and also
  62535. * strings and numbers (e.g. "{ get 1() { ... } }").
  62536. *
  62537. * Function parsing may start either from prev_token or curr_token
  62538. * (object literal method definition uses prev_token for example).
  62539. * This is dealt with for the initial token.
  62540. */
  62541. no_advance = (flags & DUK__FUNC_FLAG_USE_PREVTOKEN);
  62542. if (no_advance) {
  62543. tok = &comp_ctx->prev_token;
  62544. } else {
  62545. tok = &comp_ctx->curr_token;
  62546. }
  62547. if (flags & DUK__FUNC_FLAG_GETSET) {
  62548. /* PropertyName -> IdentifierName | StringLiteral | NumericLiteral */
  62549. if (tok->t_nores == DUK_TOK_IDENTIFIER || tok->t == DUK_TOK_STRING) {
  62550. duk_push_hstring(thr, tok->str1); /* keep in valstack */
  62551. } else if (tok->t == DUK_TOK_NUMBER) {
  62552. duk_push_number(thr, tok->num);
  62553. duk_to_string(thr, -1);
  62554. } else {
  62555. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_GETSET_NAME);
  62556. DUK_WO_NORETURN(return;);
  62557. }
  62558. comp_ctx->curr_func.h_name = duk_known_hstring(thr, -1); /* borrowed reference */
  62559. } else {
  62560. /* Function name is an Identifier (not IdentifierName), but we get
  62561. * the raw name (not recognizing keywords) here and perform the name
  62562. * checks only after pass 1.
  62563. */
  62564. if (tok->t_nores == DUK_TOK_IDENTIFIER) {
  62565. duk_push_hstring(thr, tok->str1); /* keep in valstack */
  62566. comp_ctx->curr_func.h_name = duk_known_hstring(thr, -1); /* borrowed reference */
  62567. } else {
  62568. /* valstack will be unbalanced, which is OK */
  62569. DUK_ASSERT((flags & DUK__FUNC_FLAG_GETSET) == 0);
  62570. DUK_ASSERT(comp_ctx->curr_func.h_name == NULL);
  62571. no_advance = 1;
  62572. if (flags & DUK__FUNC_FLAG_DECL) {
  62573. DUK_ERROR_SYNTAX(thr, DUK_STR_FUNC_NAME_REQUIRED);
  62574. DUK_WO_NORETURN(return;);
  62575. }
  62576. }
  62577. }
  62578. DUK_DD(DUK_DDPRINT("function name: %!O",
  62579. (duk_heaphdr *) comp_ctx->curr_func.h_name));
  62580. if (!no_advance) {
  62581. duk__advance(comp_ctx);
  62582. }
  62583. /*
  62584. * Formal argument list
  62585. *
  62586. * We don't check for prohibited names or for duplicate argument
  62587. * names here, becase we don't yet know whether the function will
  62588. * be strict. Function body parsing handles this retroactively.
  62589. */
  62590. duk__advance_expect(comp_ctx, DUK_TOK_LPAREN);
  62591. duk__parse_func_formals(comp_ctx);
  62592. DUK_ASSERT(comp_ctx->curr_token.t == DUK_TOK_RPAREN);
  62593. duk__advance(comp_ctx);
  62594. /*
  62595. * Parse function body
  62596. */
  62597. duk__parse_func_body(comp_ctx,
  62598. 0, /* expect_eof */
  62599. 0, /* implicit_return_value */
  62600. flags & DUK__FUNC_FLAG_DECL, /* regexp_after */
  62601. DUK_TOK_LCURLY); /* expect_token */
  62602. /*
  62603. * Convert duk_compiler_func to a function template and add it
  62604. * to the parent function table.
  62605. */
  62606. duk__convert_to_func_template(comp_ctx); /* -> [ ... func ] */
  62607. }
  62608. /* Parse an inner function, adding the function template to the current function's
  62609. * function table. Return a function number to be used by the outer function.
  62610. *
  62611. * Avoiding O(depth^2) inner function parsing is handled here. On the first pass,
  62612. * compile and register the function normally into the 'funcs' array, also recording
  62613. * a lexer point (offset/line) to the closing brace of the function. On the second
  62614. * pass, skip the function and return the same 'fnum' as on the first pass by using
  62615. * a running counter.
  62616. *
  62617. * An unfortunate side effect of this is that when parsing the inner function, almost
  62618. * nothing is known of the outer function, i.e. the inner function's scope. We don't
  62619. * need that information at the moment, but it would allow some optimizations if it
  62620. * were used.
  62621. */
  62622. DUK_LOCAL duk_int_t duk__parse_func_like_fnum(duk_compiler_ctx *comp_ctx, duk_small_uint_t flags) {
  62623. duk_hthread *thr = comp_ctx->thr;
  62624. duk_compiler_func old_func;
  62625. duk_idx_t entry_top;
  62626. duk_int_t fnum;
  62627. /*
  62628. * On second pass, skip the function.
  62629. */
  62630. if (!comp_ctx->curr_func.in_scanning) {
  62631. duk_lexer_point lex_pt;
  62632. fnum = comp_ctx->curr_func.fnum_next++;
  62633. duk_get_prop_index(thr, comp_ctx->curr_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 1));
  62634. lex_pt.offset = (duk_size_t) duk_to_uint(thr, -1);
  62635. duk_pop(thr);
  62636. duk_get_prop_index(thr, comp_ctx->curr_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 2));
  62637. lex_pt.line = duk_to_int(thr, -1);
  62638. duk_pop(thr);
  62639. DUK_DDD(DUK_DDDPRINT("second pass of an inner func, skip the function, reparse closing brace; lex offset=%ld, line=%ld",
  62640. (long) lex_pt.offset, (long) lex_pt.line));
  62641. DUK_LEXER_SETPOINT(&comp_ctx->lex, &lex_pt);
  62642. comp_ctx->curr_token.t = 0; /* this is needed for regexp mode */
  62643. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  62644. duk__advance(comp_ctx);
  62645. /* RegExp is not allowed after a function expression, e.g. in
  62646. * (function () {} / 123). A RegExp *is* allowed after a
  62647. * function declaration!
  62648. */
  62649. if (flags & DUK__FUNC_FLAG_DECL) {
  62650. comp_ctx->curr_func.allow_regexp_in_adv = 1;
  62651. }
  62652. duk__advance_expect(comp_ctx, DUK_TOK_RCURLY);
  62653. return fnum;
  62654. }
  62655. /*
  62656. * On first pass, perform actual parsing. Remember valstack top on entry
  62657. * to restore it later, and switch to using a new function in comp_ctx.
  62658. */
  62659. entry_top = duk_get_top(thr);
  62660. DUK_DDD(DUK_DDDPRINT("before func: entry_top=%ld, curr_tok.start_offset=%ld",
  62661. (long) entry_top, (long) comp_ctx->curr_token.start_offset));
  62662. duk_memcpy(&old_func, &comp_ctx->curr_func, sizeof(duk_compiler_func));
  62663. duk_memzero(&comp_ctx->curr_func, sizeof(duk_compiler_func));
  62664. duk__init_func_valstack_slots(comp_ctx);
  62665. DUK_ASSERT(comp_ctx->curr_func.num_formals == 0);
  62666. /* inherit initial strictness from parent */
  62667. comp_ctx->curr_func.is_strict = old_func.is_strict;
  62668. /* XXX: It might be better to just store the flags into the curr_func
  62669. * struct and use them as is without this flag interpretation step
  62670. * here.
  62671. */
  62672. DUK_ASSERT(comp_ctx->curr_func.is_notail == 0);
  62673. comp_ctx->curr_func.is_function = 1;
  62674. DUK_ASSERT(comp_ctx->curr_func.is_eval == 0);
  62675. DUK_ASSERT(comp_ctx->curr_func.is_global == 0);
  62676. comp_ctx->curr_func.is_setget = ((flags & DUK__FUNC_FLAG_GETSET) != 0);
  62677. comp_ctx->curr_func.is_namebinding = !(flags & (DUK__FUNC_FLAG_GETSET |
  62678. DUK__FUNC_FLAG_METDEF |
  62679. DUK__FUNC_FLAG_DECL)); /* no name binding for: declarations, objlit getset, objlit method def */
  62680. comp_ctx->curr_func.is_constructable = !(flags & (DUK__FUNC_FLAG_GETSET |
  62681. DUK__FUNC_FLAG_METDEF)); /* not constructable: objlit getset, objlit method def */
  62682. /*
  62683. * Parse inner function
  62684. */
  62685. duk__parse_func_like_raw(comp_ctx, flags); /* pushes function template */
  62686. /* prev_token.start_offset points to the closing brace here; when skipping
  62687. * we're going to reparse the closing brace to ensure semicolon insertion
  62688. * etc work as expected.
  62689. */
  62690. DUK_DDD(DUK_DDDPRINT("after func: prev_tok.start_offset=%ld, curr_tok.start_offset=%ld",
  62691. (long) comp_ctx->prev_token.start_offset, (long) comp_ctx->curr_token.start_offset));
  62692. DUK_ASSERT(comp_ctx->lex.input[comp_ctx->prev_token.start_offset] == (duk_uint8_t) DUK_ASC_RCURLY);
  62693. /* XXX: append primitive */
  62694. DUK_ASSERT(duk_get_length(thr, old_func.funcs_idx) == (duk_size_t) (old_func.fnum_next * 3));
  62695. fnum = old_func.fnum_next++;
  62696. if (fnum > DUK__MAX_FUNCS) {
  62697. DUK_ERROR_RANGE(comp_ctx->thr, DUK_STR_FUNC_LIMIT);
  62698. DUK_WO_NORETURN(return 0;);
  62699. }
  62700. /* array writes autoincrement length */
  62701. (void) duk_put_prop_index(thr, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3));
  62702. duk_push_size_t(thr, comp_ctx->prev_token.start_offset);
  62703. (void) duk_put_prop_index(thr, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 1));
  62704. duk_push_int(thr, comp_ctx->prev_token.start_line);
  62705. (void) duk_put_prop_index(thr, old_func.funcs_idx, (duk_uarridx_t) (fnum * 3 + 2));
  62706. /*
  62707. * Cleanup: restore original function, restore valstack state.
  62708. *
  62709. * Function declaration handling needs the function name to be pushed
  62710. * on the value stack.
  62711. */
  62712. if (flags & DUK__FUNC_FLAG_PUSHNAME_PASS1) {
  62713. DUK_ASSERT(comp_ctx->curr_func.h_name != NULL);
  62714. duk_push_hstring(thr, comp_ctx->curr_func.h_name);
  62715. duk_replace(thr, entry_top);
  62716. duk_set_top(thr, entry_top + 1);
  62717. } else {
  62718. duk_set_top(thr, entry_top);
  62719. }
  62720. duk_memcpy((void *) &comp_ctx->curr_func, (void *) &old_func, sizeof(duk_compiler_func));
  62721. return fnum;
  62722. }
  62723. /*
  62724. * Compile input string into an executable function template without
  62725. * arguments.
  62726. *
  62727. * The string is parsed as the "Program" production of ECMAScript E5.
  62728. * Compilation context can be either global code or eval code (see E5
  62729. * Sections 14 and 15.1.2.1).
  62730. *
  62731. * Input stack: [ ... filename ]
  62732. * Output stack: [ ... func_template ]
  62733. */
  62734. /* XXX: source code property */
  62735. DUK_LOCAL duk_ret_t duk__js_compile_raw(duk_hthread *thr, void *udata) {
  62736. duk_hstring *h_filename;
  62737. duk__compiler_stkstate *comp_stk;
  62738. duk_compiler_ctx *comp_ctx;
  62739. duk_lexer_point *lex_pt;
  62740. duk_compiler_func *func;
  62741. duk_idx_t entry_top;
  62742. duk_bool_t is_strict;
  62743. duk_bool_t is_eval;
  62744. duk_bool_t is_funcexpr;
  62745. duk_small_uint_t flags;
  62746. DUK_ASSERT(thr != NULL);
  62747. DUK_ASSERT(udata != NULL);
  62748. /*
  62749. * Arguments check
  62750. */
  62751. entry_top = duk_get_top(thr);
  62752. DUK_ASSERT(entry_top >= 1);
  62753. comp_stk = (duk__compiler_stkstate *) udata;
  62754. comp_ctx = &comp_stk->comp_ctx_alloc;
  62755. lex_pt = &comp_stk->lex_pt_alloc;
  62756. DUK_ASSERT(comp_ctx != NULL);
  62757. DUK_ASSERT(lex_pt != NULL);
  62758. flags = comp_stk->flags;
  62759. is_eval = (flags & DUK_COMPILE_EVAL ? 1 : 0);
  62760. is_strict = (flags & DUK_COMPILE_STRICT ? 1 : 0);
  62761. is_funcexpr = (flags & DUK_COMPILE_FUNCEXPR ? 1 : 0);
  62762. h_filename = duk_get_hstring(thr, -1); /* may be undefined */
  62763. /*
  62764. * Init compiler and lexer contexts
  62765. */
  62766. func = &comp_ctx->curr_func;
  62767. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  62768. comp_ctx->thr = NULL;
  62769. comp_ctx->h_filename = NULL;
  62770. comp_ctx->prev_token.str1 = NULL;
  62771. comp_ctx->prev_token.str2 = NULL;
  62772. comp_ctx->curr_token.str1 = NULL;
  62773. comp_ctx->curr_token.str2 = NULL;
  62774. #endif
  62775. duk_require_stack(thr, DUK__COMPILE_ENTRY_SLOTS);
  62776. duk_push_dynamic_buffer(thr, 0); /* entry_top + 0 */
  62777. duk_push_undefined(thr); /* entry_top + 1 */
  62778. duk_push_undefined(thr); /* entry_top + 2 */
  62779. duk_push_undefined(thr); /* entry_top + 3 */
  62780. duk_push_undefined(thr); /* entry_top + 4 */
  62781. comp_ctx->thr = thr;
  62782. comp_ctx->h_filename = h_filename;
  62783. comp_ctx->tok11_idx = entry_top + 1;
  62784. comp_ctx->tok12_idx = entry_top + 2;
  62785. comp_ctx->tok21_idx = entry_top + 3;
  62786. comp_ctx->tok22_idx = entry_top + 4;
  62787. comp_ctx->recursion_limit = DUK_USE_COMPILER_RECLIMIT;
  62788. /* comp_ctx->lex has been pre-initialized by caller: it has been
  62789. * zeroed and input/input_length has been set.
  62790. */
  62791. comp_ctx->lex.thr = thr;
  62792. /* comp_ctx->lex.input and comp_ctx->lex.input_length filled by caller */
  62793. comp_ctx->lex.slot1_idx = comp_ctx->tok11_idx;
  62794. comp_ctx->lex.slot2_idx = comp_ctx->tok12_idx;
  62795. comp_ctx->lex.buf_idx = entry_top + 0;
  62796. comp_ctx->lex.buf = (duk_hbuffer_dynamic *) duk_known_hbuffer(thr, entry_top + 0);
  62797. DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(comp_ctx->lex.buf) && !DUK_HBUFFER_HAS_EXTERNAL(comp_ctx->lex.buf));
  62798. comp_ctx->lex.token_limit = DUK_COMPILER_TOKEN_LIMIT;
  62799. lex_pt->offset = 0;
  62800. lex_pt->line = 1;
  62801. DUK_LEXER_SETPOINT(&comp_ctx->lex, lex_pt); /* fills window */
  62802. comp_ctx->curr_token.start_line = 0; /* needed for line number tracking (becomes prev_token.start_line) */
  62803. /*
  62804. * Initialize function state for a zero-argument function
  62805. */
  62806. duk__init_func_valstack_slots(comp_ctx);
  62807. DUK_ASSERT(func->num_formals == 0);
  62808. if (is_funcexpr) {
  62809. /* Name will be filled from function expression, not by caller.
  62810. * This case is used by Function constructor and duk_compile()
  62811. * API with the DUK_COMPILE_FUNCTION option.
  62812. */
  62813. DUK_ASSERT(func->h_name == NULL);
  62814. } else {
  62815. duk_push_hstring_stridx(thr, (is_eval ? DUK_STRIDX_EVAL :
  62816. DUK_STRIDX_GLOBAL));
  62817. func->h_name = duk_get_hstring(thr, -1);
  62818. }
  62819. /*
  62820. * Parse a function body or a function-like expression, depending
  62821. * on flags.
  62822. */
  62823. DUK_ASSERT(func->is_setget == 0);
  62824. func->is_strict = (duk_uint8_t) is_strict;
  62825. DUK_ASSERT(func->is_notail == 0);
  62826. if (is_funcexpr) {
  62827. func->is_function = 1;
  62828. DUK_ASSERT(func->is_eval == 0);
  62829. DUK_ASSERT(func->is_global == 0);
  62830. func->is_namebinding = 1;
  62831. func->is_constructable = 1;
  62832. duk__advance(comp_ctx); /* init 'curr_token' */
  62833. duk__advance_expect(comp_ctx, DUK_TOK_FUNCTION);
  62834. (void) duk__parse_func_like_raw(comp_ctx, 0 /*flags*/);
  62835. } else {
  62836. DUK_ASSERT(func->is_function == 0);
  62837. DUK_ASSERT(is_eval == 0 || is_eval == 1);
  62838. func->is_eval = (duk_uint8_t) is_eval;
  62839. func->is_global = (duk_uint8_t) !is_eval;
  62840. DUK_ASSERT(func->is_namebinding == 0);
  62841. DUK_ASSERT(func->is_constructable == 0);
  62842. duk__parse_func_body(comp_ctx,
  62843. 1, /* expect_eof */
  62844. 1, /* implicit_return_value */
  62845. 1, /* regexp_after (does not matter) */
  62846. -1); /* expect_token */
  62847. }
  62848. /*
  62849. * Convert duk_compiler_func to a function template
  62850. */
  62851. duk__convert_to_func_template(comp_ctx);
  62852. /*
  62853. * Wrapping duk_safe_call() will mangle the stack, just return stack top
  62854. */
  62855. /* [ ... filename (temps) func ] */
  62856. return 1;
  62857. }
  62858. DUK_INTERNAL void duk_js_compile(duk_hthread *thr, const duk_uint8_t *src_buffer, duk_size_t src_length, duk_small_uint_t flags) {
  62859. duk__compiler_stkstate comp_stk;
  62860. duk_compiler_ctx *prev_ctx;
  62861. duk_ret_t safe_rc;
  62862. DUK_ASSERT(thr != NULL);
  62863. DUK_ASSERT(src_buffer != NULL);
  62864. /* preinitialize lexer state partially */
  62865. duk_memzero(&comp_stk, sizeof(comp_stk));
  62866. comp_stk.flags = flags;
  62867. DUK_LEXER_INITCTX(&comp_stk.comp_ctx_alloc.lex);
  62868. comp_stk.comp_ctx_alloc.lex.input = src_buffer;
  62869. comp_stk.comp_ctx_alloc.lex.input_length = src_length;
  62870. comp_stk.comp_ctx_alloc.lex.flags = flags; /* Forward flags directly for now. */
  62871. /* [ ... filename ] */
  62872. prev_ctx = thr->compile_ctx;
  62873. thr->compile_ctx = &comp_stk.comp_ctx_alloc; /* for duk_error_augment.c */
  62874. safe_rc = duk_safe_call(thr, duk__js_compile_raw, (void *) &comp_stk /*udata*/, 1 /*nargs*/, 1 /*nrets*/);
  62875. thr->compile_ctx = prev_ctx; /* must restore reliably before returning */
  62876. if (safe_rc != DUK_EXEC_SUCCESS) {
  62877. DUK_D(DUK_DPRINT("compilation failed: %!T", duk_get_tval(thr, -1)));
  62878. (void) duk_throw(thr);
  62879. DUK_WO_NORETURN(return;);
  62880. }
  62881. /* [ ... template ] */
  62882. }
  62883. /* automatic undefs */
  62884. #undef DUK__ALLOCTEMP
  62885. #undef DUK__ALLOCTEMPS
  62886. #undef DUK__ALLOW_AUTO_SEMI_ALWAYS
  62887. #undef DUK__BC_INITIAL_INSTS
  62888. #undef DUK__BP_ADDITIVE
  62889. #undef DUK__BP_ASSIGNMENT
  62890. #undef DUK__BP_BAND
  62891. #undef DUK__BP_BOR
  62892. #undef DUK__BP_BXOR
  62893. #undef DUK__BP_CALL
  62894. #undef DUK__BP_CLOSING
  62895. #undef DUK__BP_COMMA
  62896. #undef DUK__BP_CONDITIONAL
  62897. #undef DUK__BP_EOF
  62898. #undef DUK__BP_EQUALITY
  62899. #undef DUK__BP_EXPONENTIATION
  62900. #undef DUK__BP_FOR_EXPR
  62901. #undef DUK__BP_INVALID
  62902. #undef DUK__BP_LAND
  62903. #undef DUK__BP_LOR
  62904. #undef DUK__BP_MEMBER
  62905. #undef DUK__BP_MULTIPLICATIVE
  62906. #undef DUK__BP_POSTFIX
  62907. #undef DUK__BP_RELATIONAL
  62908. #undef DUK__BP_SHIFT
  62909. #undef DUK__COMPILE_ENTRY_SLOTS
  62910. #undef DUK__CONST_MARKER
  62911. #undef DUK__DUMP_ISPEC
  62912. #undef DUK__DUMP_IVALUE
  62913. #undef DUK__EMIT_FLAG_A_IS_SOURCE
  62914. #undef DUK__EMIT_FLAG_BC_REGCONST
  62915. #undef DUK__EMIT_FLAG_B_IS_TARGET
  62916. #undef DUK__EMIT_FLAG_C_IS_TARGET
  62917. #undef DUK__EMIT_FLAG_NO_SHUFFLE_A
  62918. #undef DUK__EMIT_FLAG_NO_SHUFFLE_B
  62919. #undef DUK__EMIT_FLAG_NO_SHUFFLE_C
  62920. #undef DUK__EMIT_FLAG_RESERVE_JUMPSLOT
  62921. #undef DUK__EXPR_FLAG_ALLOW_EMPTY
  62922. #undef DUK__EXPR_FLAG_REJECT_IN
  62923. #undef DUK__EXPR_FLAG_REQUIRE_INIT
  62924. #undef DUK__EXPR_RBP_MASK
  62925. #undef DUK__FUNCTION_BODY_REQUIRE_SLOTS
  62926. #undef DUK__FUNCTION_INIT_REQUIRE_SLOTS
  62927. #undef DUK__FUNC_FLAG_DECL
  62928. #undef DUK__FUNC_FLAG_GETSET
  62929. #undef DUK__FUNC_FLAG_METDEF
  62930. #undef DUK__FUNC_FLAG_PUSHNAME_PASS1
  62931. #undef DUK__FUNC_FLAG_USE_PREVTOKEN
  62932. #undef DUK__GETCONST_MAX_CONSTS_CHECK
  62933. #undef DUK__GETTEMP
  62934. #undef DUK__HAS_TERM
  62935. #undef DUK__HAS_VAL
  62936. #undef DUK__ISCONST
  62937. #undef DUK__ISREG
  62938. #undef DUK__ISREG_NOTTEMP
  62939. #undef DUK__ISREG_TEMP
  62940. #undef DUK__IS_TERMINAL
  62941. #undef DUK__IVAL_FLAG_ALLOW_CONST
  62942. #undef DUK__IVAL_FLAG_REQUIRE_SHORT
  62943. #undef DUK__IVAL_FLAG_REQUIRE_TEMP
  62944. #undef DUK__MAX_ARRAY_INIT_VALUES
  62945. #undef DUK__MAX_CONSTS
  62946. #undef DUK__MAX_FUNCS
  62947. #undef DUK__MAX_OBJECT_INIT_PAIRS
  62948. #undef DUK__MAX_TEMPS
  62949. #undef DUK__MK_LBP
  62950. #undef DUK__MK_LBP_FLAGS
  62951. #undef DUK__OBJ_LIT_KEY_GET
  62952. #undef DUK__OBJ_LIT_KEY_PLAIN
  62953. #undef DUK__OBJ_LIT_KEY_SET
  62954. #undef DUK__PARSE_EXPR_SLOTS
  62955. #undef DUK__PARSE_STATEMENTS_SLOTS
  62956. #undef DUK__RECURSION_DECREASE
  62957. #undef DUK__RECURSION_INCREASE
  62958. #undef DUK__REMOVECONST
  62959. #undef DUK__SETTEMP
  62960. #undef DUK__SETTEMP_CHECKMAX
  62961. #undef DUK__STILL_PROLOGUE
  62962. #undef DUK__TOKEN_LBP_BP_MASK
  62963. #undef DUK__TOKEN_LBP_FLAG_NO_REGEXP
  62964. #undef DUK__TOKEN_LBP_FLAG_TERMINATES
  62965. #undef DUK__TOKEN_LBP_FLAG_UNUSED
  62966. #undef DUK__TOKEN_LBP_GET_BP
  62967. #line 1 "duk_js_executor.c"
  62968. /*
  62969. * ECMAScript bytecode executor.
  62970. */
  62971. /* #include duk_internal.h -> already included */
  62972. /*
  62973. * Local declarations.
  62974. */
  62975. DUK_LOCAL_DECL void duk__js_execute_bytecode_inner(duk_hthread *entry_thread, duk_activation *entry_act);
  62976. /*
  62977. * Misc helpers.
  62978. */
  62979. /* Forced inline declaration, only applied for performance oriented build. */
  62980. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  62981. #define DUK__INLINE_PERF
  62982. #define DUK__NOINLINE_PERF
  62983. #else
  62984. #define DUK__INLINE_PERF DUK_ALWAYS_INLINE
  62985. #define DUK__NOINLINE_PERF DUK_NOINLINE
  62986. #endif
  62987. /* Replace value stack top to value at 'tv_ptr'. Optimize for
  62988. * performance by only applying the net refcount change.
  62989. */
  62990. #define DUK__REPLACE_TO_TVPTR(thr,tv_ptr) do { \
  62991. duk_hthread *duk__thr; \
  62992. duk_tval *duk__tvsrc; \
  62993. duk_tval *duk__tvdst; \
  62994. duk_tval duk__tvtmp; \
  62995. duk__thr = (thr); \
  62996. duk__tvsrc = DUK_GET_TVAL_NEGIDX(duk__thr, -1); \
  62997. duk__tvdst = (tv_ptr); \
  62998. DUK_TVAL_SET_TVAL(&duk__tvtmp, duk__tvdst); \
  62999. DUK_TVAL_SET_TVAL(duk__tvdst, duk__tvsrc); \
  63000. DUK_TVAL_SET_UNDEFINED(duk__tvsrc); /* value stack init policy */ \
  63001. duk__thr->valstack_top = duk__tvsrc; \
  63002. DUK_TVAL_DECREF(duk__thr, &duk__tvtmp); \
  63003. } while (0)
  63004. /* XXX: candidate of being an internal shared API call */
  63005. #if 0 /* unused */
  63006. DUK_LOCAL void duk__push_tvals_incref_only(duk_hthread *thr, duk_tval *tv_src, duk_small_uint_fast_t count) {
  63007. duk_tval *tv_dst;
  63008. duk_size_t copy_size;
  63009. duk_size_t i;
  63010. tv_dst = thr->valstack_top;
  63011. copy_size = sizeof(duk_tval) * count;
  63012. duk_memcpy((void *) tv_dst, (const void *) tv_src, copy_size);
  63013. for (i = 0; i < count; i++) {
  63014. DUK_TVAL_INCREF(thr, tv_dst);
  63015. tv_dst++;
  63016. }
  63017. thr->valstack_top = tv_dst;
  63018. }
  63019. #endif
  63020. /*
  63021. * Arithmetic, binary, and logical helpers.
  63022. *
  63023. * Note: there is no opcode for logical AND or logical OR; this is on
  63024. * purpose, because the evalution order semantics for them make such
  63025. * opcodes pretty pointless: short circuiting means they are most
  63026. * comfortably implemented as jumps. However, a logical NOT opcode
  63027. * is useful.
  63028. *
  63029. * Note: careful with duk_tval pointers here: they are potentially
  63030. * invalidated by any DECREF and almost any API call. It's still
  63031. * preferable to work without making a copy but that's not always
  63032. * possible.
  63033. */
  63034. DUK_LOCAL DUK__INLINE_PERF duk_double_t duk__compute_mod(duk_double_t d1, duk_double_t d2) {
  63035. return (duk_double_t) duk_js_arith_mod((double) d1, (double) d2);
  63036. }
  63037. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  63038. DUK_LOCAL DUK__INLINE_PERF duk_double_t duk__compute_exp(duk_double_t d1, duk_double_t d2) {
  63039. return (duk_double_t) duk_js_arith_pow((double) d1, (double) d2);
  63040. }
  63041. #endif
  63042. DUK_LOCAL DUK__INLINE_PERF void duk__vm_arith_add(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_fast_t idx_z) {
  63043. /*
  63044. * Addition operator is different from other arithmetic
  63045. * operations in that it also provides string concatenation.
  63046. * Hence it is implemented separately.
  63047. *
  63048. * There is a fast path for number addition. Other cases go
  63049. * through potentially multiple coercions as described in the
  63050. * E5 specification. It may be possible to reduce the number
  63051. * of coercions, but this must be done carefully to preserve
  63052. * the exact semantics.
  63053. *
  63054. * E5 Section 11.6.1.
  63055. *
  63056. * Custom types also have special behavior implemented here.
  63057. */
  63058. duk_double_union du;
  63059. DUK_ASSERT(thr != NULL);
  63060. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  63061. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  63062. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  63063. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(thr));
  63064. /*
  63065. * Fast paths
  63066. */
  63067. #if defined(DUK_USE_FASTINT)
  63068. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  63069. duk_int64_t v1, v2, v3;
  63070. duk_int32_t v3_hi;
  63071. duk_tval *tv_z;
  63072. /* Input values are signed 48-bit so we can detect overflow
  63073. * reliably from high bits or just a comparison.
  63074. */
  63075. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  63076. v2 = DUK_TVAL_GET_FASTINT(tv_y);
  63077. v3 = v1 + v2;
  63078. v3_hi = (duk_int32_t) (v3 >> 32);
  63079. if (DUK_LIKELY(v3_hi >= DUK_I64_CONSTANT(-0x8000) && v3_hi <= DUK_I64_CONSTANT(0x7fff))) {
  63080. tv_z = thr->valstack_bottom + idx_z;
  63081. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, v3); /* side effects */
  63082. return;
  63083. } else {
  63084. /* overflow, fall through */
  63085. ;
  63086. }
  63087. }
  63088. #endif /* DUK_USE_FASTINT */
  63089. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  63090. #if !defined(DUK_USE_EXEC_PREFER_SIZE)
  63091. duk_tval *tv_z;
  63092. #endif
  63093. du.d = DUK_TVAL_GET_NUMBER(tv_x) + DUK_TVAL_GET_NUMBER(tv_y);
  63094. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  63095. duk_push_number(thr, du.d); /* will NaN normalize result */
  63096. duk_replace(thr, (duk_idx_t) idx_z);
  63097. #else /* DUK_USE_EXEC_PREFER_SIZE */
  63098. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  63099. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  63100. tv_z = thr->valstack_bottom + idx_z;
  63101. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, du.d); /* side effects */
  63102. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  63103. return;
  63104. }
  63105. /*
  63106. * Slow path: potentially requires function calls for coercion
  63107. */
  63108. duk_push_tval(thr, tv_x);
  63109. duk_push_tval(thr, tv_y);
  63110. duk_to_primitive(thr, -2, DUK_HINT_NONE); /* side effects -> don't use tv_x, tv_y after */
  63111. duk_to_primitive(thr, -1, DUK_HINT_NONE);
  63112. /* Since Duktape 2.x plain buffers are treated like ArrayBuffer. */
  63113. if (duk_is_string(thr, -2) || duk_is_string(thr, -1)) {
  63114. /* Symbols shouldn't technically be handled here, but should
  63115. * go into the default ToNumber() coercion path instead and
  63116. * fail there with a TypeError. However, there's a ToString()
  63117. * in duk_concat_2() which also fails with TypeError so no
  63118. * explicit check is needed.
  63119. */
  63120. duk_concat_2(thr); /* [... s1 s2] -> [... s1+s2] */
  63121. } else {
  63122. duk_double_t d1, d2;
  63123. d1 = duk_to_number_m2(thr);
  63124. d2 = duk_to_number_m1(thr);
  63125. DUK_ASSERT(duk_is_number(thr, -2));
  63126. DUK_ASSERT(duk_is_number(thr, -1));
  63127. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1);
  63128. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);
  63129. du.d = d1 + d2;
  63130. duk_pop_2_unsafe(thr);
  63131. duk_push_number(thr, du.d); /* will NaN normalize result */
  63132. }
  63133. duk_replace(thr, (duk_idx_t) idx_z); /* side effects */
  63134. }
  63135. DUK_LOCAL DUK__INLINE_PERF void duk__vm_arith_binary_op(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_uint_fast_t idx_z, duk_small_uint_fast_t opcode) {
  63136. /*
  63137. * Arithmetic operations other than '+' have number-only semantics
  63138. * and are implemented here. The separate switch-case here means a
  63139. * "double dispatch" of the arithmetic opcode, but saves code space.
  63140. *
  63141. * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
  63142. */
  63143. duk_double_t d1, d2;
  63144. duk_double_union du;
  63145. duk_small_uint_fast_t opcode_shifted;
  63146. #if defined(DUK_USE_FASTINT) || !defined(DUK_USE_EXEC_PREFER_SIZE)
  63147. duk_tval *tv_z;
  63148. #endif
  63149. DUK_ASSERT(thr != NULL);
  63150. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  63151. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  63152. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  63153. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(thr));
  63154. opcode_shifted = opcode >> 2; /* Get base opcode without reg/const modifiers. */
  63155. #if defined(DUK_USE_FASTINT)
  63156. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  63157. duk_int64_t v1, v2, v3;
  63158. duk_int32_t v3_hi;
  63159. v1 = DUK_TVAL_GET_FASTINT(tv_x);
  63160. v2 = DUK_TVAL_GET_FASTINT(tv_y);
  63161. switch (opcode_shifted) {
  63162. case DUK_OP_SUB >> 2: {
  63163. v3 = v1 - v2;
  63164. break;
  63165. }
  63166. case DUK_OP_MUL >> 2: {
  63167. /* Must ensure result is 64-bit (no overflow); a
  63168. * simple and sufficient fast path is to allow only
  63169. * 32-bit inputs. Avoid zero inputs to avoid
  63170. * negative zero issues (-1 * 0 = -0, for instance).
  63171. */
  63172. if (v1 >= DUK_I64_CONSTANT(-0x80000000) && v1 <= DUK_I64_CONSTANT(0x7fffffff) && v1 != 0 &&
  63173. v2 >= DUK_I64_CONSTANT(-0x80000000) && v2 <= DUK_I64_CONSTANT(0x7fffffff) && v2 != 0) {
  63174. v3 = v1 * v2;
  63175. } else {
  63176. goto skip_fastint;
  63177. }
  63178. break;
  63179. }
  63180. case DUK_OP_DIV >> 2: {
  63181. /* Don't allow a zero divisor. Fast path check by
  63182. * "verifying" with multiplication. Also avoid zero
  63183. * dividend to avoid negative zero issues (0 / -1 = -0
  63184. * for instance).
  63185. */
  63186. if (v1 == 0 || v2 == 0) {
  63187. goto skip_fastint;
  63188. }
  63189. v3 = v1 / v2;
  63190. if (v3 * v2 != v1) {
  63191. goto skip_fastint;
  63192. }
  63193. break;
  63194. }
  63195. case DUK_OP_MOD >> 2: {
  63196. /* Don't allow a zero divisor. Restrict both v1 and
  63197. * v2 to positive values to avoid compiler specific
  63198. * behavior.
  63199. */
  63200. if (v1 < 1 || v2 < 1) {
  63201. goto skip_fastint;
  63202. }
  63203. v3 = v1 % v2;
  63204. DUK_ASSERT(v3 >= 0);
  63205. DUK_ASSERT(v3 < v2);
  63206. DUK_ASSERT(v1 - (v1 / v2) * v2 == v3);
  63207. break;
  63208. }
  63209. default: {
  63210. /* Possible with DUK_OP_EXP. */
  63211. goto skip_fastint;
  63212. }
  63213. }
  63214. v3_hi = (duk_int32_t) (v3 >> 32);
  63215. if (DUK_LIKELY(v3_hi >= DUK_I64_CONSTANT(-0x8000) && v3_hi <= DUK_I64_CONSTANT(0x7fff))) {
  63216. tv_z = thr->valstack_bottom + idx_z;
  63217. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, v3); /* side effects */
  63218. return;
  63219. }
  63220. /* fall through if overflow etc */
  63221. }
  63222. skip_fastint:
  63223. #endif /* DUK_USE_FASTINT */
  63224. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  63225. /* fast path */
  63226. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  63227. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  63228. } else {
  63229. duk_push_tval(thr, tv_x);
  63230. duk_push_tval(thr, tv_y);
  63231. d1 = duk_to_number_m2(thr); /* side effects */
  63232. d2 = duk_to_number_m1(thr);
  63233. DUK_ASSERT(duk_is_number(thr, -2));
  63234. DUK_ASSERT(duk_is_number(thr, -1));
  63235. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d1);
  63236. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d2);
  63237. duk_pop_2_unsafe(thr);
  63238. }
  63239. switch (opcode_shifted) {
  63240. case DUK_OP_SUB >> 2: {
  63241. du.d = d1 - d2;
  63242. break;
  63243. }
  63244. case DUK_OP_MUL >> 2: {
  63245. du.d = d1 * d2;
  63246. break;
  63247. }
  63248. case DUK_OP_DIV >> 2: {
  63249. /* Division-by-zero is undefined behavior, so
  63250. * rely on a helper.
  63251. */
  63252. du.d = duk_double_div(d1, d2);
  63253. break;
  63254. }
  63255. case DUK_OP_MOD >> 2: {
  63256. du.d = duk__compute_mod(d1, d2);
  63257. break;
  63258. }
  63259. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  63260. case DUK_OP_EXP >> 2: {
  63261. du.d = duk__compute_exp(d1, d2);
  63262. break;
  63263. }
  63264. #endif
  63265. default: {
  63266. DUK_UNREACHABLE();
  63267. du.d = DUK_DOUBLE_NAN; /* should not happen */
  63268. break;
  63269. }
  63270. }
  63271. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  63272. duk_push_number(thr, du.d); /* will NaN normalize result */
  63273. duk_replace(thr, (duk_idx_t) idx_z);
  63274. #else /* DUK_USE_EXEC_PREFER_SIZE */
  63275. /* important to use normalized NaN with 8-byte tagged types */
  63276. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du);
  63277. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  63278. tv_z = thr->valstack_bottom + idx_z;
  63279. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, du.d); /* side effects */
  63280. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  63281. }
  63282. DUK_LOCAL DUK__INLINE_PERF void duk__vm_bitwise_binary_op(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_fast_t idx_z, duk_small_uint_fast_t opcode) {
  63283. /*
  63284. * Binary bitwise operations use different coercions (ToInt32, ToUint32)
  63285. * depending on the operation. We coerce the arguments first using
  63286. * ToInt32(), and then cast to an 32-bit value if necessary. Note that
  63287. * such casts must be correct even if there is no native 32-bit type
  63288. * (e.g., duk_int32_t and duk_uint32_t are 64-bit).
  63289. *
  63290. * E5 Sections 11.10, 11.7.1, 11.7.2, 11.7.3
  63291. */
  63292. duk_int32_t i1, i2, i3;
  63293. duk_uint32_t u1, u2, u3;
  63294. #if defined(DUK_USE_FASTINT)
  63295. duk_int64_t fi3;
  63296. #else
  63297. duk_double_t d3;
  63298. #endif
  63299. duk_small_uint_fast_t opcode_shifted;
  63300. #if defined(DUK_USE_FASTINT) || !defined(DUK_USE_EXEC_PREFER_SIZE)
  63301. duk_tval *tv_z;
  63302. #endif
  63303. DUK_ASSERT(thr != NULL);
  63304. DUK_ASSERT(tv_x != NULL); /* may be reg or const */
  63305. DUK_ASSERT(tv_y != NULL); /* may be reg or const */
  63306. DUK_ASSERT_DISABLE(idx_z >= 0); /* unsigned */
  63307. DUK_ASSERT((duk_uint_t) idx_z < (duk_uint_t) duk_get_top(thr));
  63308. opcode_shifted = opcode >> 2; /* Get base opcode without reg/const modifiers. */
  63309. #if defined(DUK_USE_FASTINT)
  63310. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  63311. i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_x);
  63312. i2 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv_y);
  63313. }
  63314. else
  63315. #endif /* DUK_USE_FASTINT */
  63316. {
  63317. duk_push_tval(thr, tv_x);
  63318. duk_push_tval(thr, tv_y);
  63319. i1 = duk_to_int32(thr, -2);
  63320. i2 = duk_to_int32(thr, -1);
  63321. duk_pop_2_unsafe(thr);
  63322. }
  63323. switch (opcode_shifted) {
  63324. case DUK_OP_BAND >> 2: {
  63325. i3 = i1 & i2;
  63326. break;
  63327. }
  63328. case DUK_OP_BOR >> 2: {
  63329. i3 = i1 | i2;
  63330. break;
  63331. }
  63332. case DUK_OP_BXOR >> 2: {
  63333. i3 = i1 ^ i2;
  63334. break;
  63335. }
  63336. case DUK_OP_BASL >> 2: {
  63337. /* Signed shift, named "arithmetic" (asl) because the result
  63338. * is signed, e.g. 4294967295 << 1 -> -2. Note that result
  63339. * must be masked.
  63340. */
  63341. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  63342. i3 = (duk_int32_t) (((duk_uint32_t) i1) << (u2 & 0x1fUL)); /* E5 Section 11.7.1, steps 7 and 8 */
  63343. i3 = i3 & ((duk_int32_t) 0xffffffffUL); /* Note: left shift, should mask */
  63344. break;
  63345. }
  63346. case DUK_OP_BASR >> 2: {
  63347. /* signed shift */
  63348. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  63349. i3 = i1 >> (u2 & 0x1fUL); /* E5 Section 11.7.2, steps 7 and 8 */
  63350. break;
  63351. }
  63352. case DUK_OP_BLSR >> 2: {
  63353. /* unsigned shift */
  63354. u1 = ((duk_uint32_t) i1) & 0xffffffffUL;
  63355. u2 = ((duk_uint32_t) i2) & 0xffffffffUL;
  63356. /* special result value handling */
  63357. u3 = u1 >> (u2 & 0x1fUL); /* E5 Section 11.7.2, steps 7 and 8 */
  63358. #if defined(DUK_USE_FASTINT)
  63359. fi3 = (duk_int64_t) u3;
  63360. goto fastint_result_set;
  63361. #else
  63362. d3 = (duk_double_t) u3;
  63363. goto result_set;
  63364. #endif
  63365. }
  63366. default: {
  63367. DUK_UNREACHABLE();
  63368. i3 = 0; /* should not happen */
  63369. break;
  63370. }
  63371. }
  63372. #if defined(DUK_USE_FASTINT)
  63373. /* Result is always fastint compatible. */
  63374. /* XXX: Set 32-bit result (but must then handle signed and
  63375. * unsigned results separately).
  63376. */
  63377. fi3 = (duk_int64_t) i3;
  63378. fastint_result_set:
  63379. tv_z = thr->valstack_bottom + idx_z;
  63380. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, fi3); /* side effects */
  63381. #else /* DUK_USE_FASTINT */
  63382. d3 = (duk_double_t) i3;
  63383. result_set:
  63384. DUK_ASSERT(!DUK_ISNAN(d3)); /* 'd3' is never NaN, so no need to normalize */
  63385. DUK_ASSERT_DOUBLE_IS_NORMALIZED(d3); /* always normalized */
  63386. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  63387. duk_push_number(thr, d3); /* would NaN normalize result, but unnecessary */
  63388. duk_replace(thr, (duk_idx_t) idx_z);
  63389. #else /* DUK_USE_EXEC_PREFER_SIZE */
  63390. tv_z = thr->valstack_bottom + idx_z;
  63391. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_z, d3); /* side effects */
  63392. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  63393. #endif /* DUK_USE_FASTINT */
  63394. }
  63395. /* In-place unary operation. */
  63396. DUK_LOCAL DUK__INLINE_PERF void duk__vm_arith_unary_op(duk_hthread *thr, duk_uint_fast_t idx_src, duk_uint_fast_t idx_dst, duk_small_uint_fast_t opcode) {
  63397. /*
  63398. * Arithmetic operations other than '+' have number-only semantics
  63399. * and are implemented here. The separate switch-case here means a
  63400. * "double dispatch" of the arithmetic opcode, but saves code space.
  63401. *
  63402. * E5 Sections 11.5, 11.5.1, 11.5.2, 11.5.3, 11.6, 11.6.1, 11.6.2, 11.6.3.
  63403. */
  63404. duk_tval *tv;
  63405. duk_double_t d1;
  63406. duk_double_union du;
  63407. DUK_ASSERT(thr != NULL);
  63408. DUK_ASSERT(opcode == DUK_OP_UNM || opcode == DUK_OP_UNP);
  63409. DUK_ASSERT_DISABLE(idx_src >= 0);
  63410. DUK_ASSERT_DISABLE(idx_dst >= 0);
  63411. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_src);
  63412. #if defined(DUK_USE_FASTINT)
  63413. if (DUK_TVAL_IS_FASTINT(tv)) {
  63414. duk_int64_t v1, v2;
  63415. v1 = DUK_TVAL_GET_FASTINT(tv);
  63416. if (opcode == DUK_OP_UNM) {
  63417. /* The smallest fastint is no longer 48-bit when
  63418. * negated. Positive zero becames negative zero
  63419. * (cannot be represented) when negated.
  63420. */
  63421. if (DUK_LIKELY(v1 != DUK_FASTINT_MIN && v1 != 0)) {
  63422. v2 = -v1;
  63423. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst);
  63424. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv, v2);
  63425. return;
  63426. }
  63427. } else {
  63428. /* ToNumber() for a fastint is a no-op. */
  63429. DUK_ASSERT(opcode == DUK_OP_UNP);
  63430. v2 = v1;
  63431. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst);
  63432. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv, v2);
  63433. return;
  63434. }
  63435. /* fall through if overflow etc */
  63436. }
  63437. #endif /* DUK_USE_FASTINT */
  63438. if (DUK_TVAL_IS_NUMBER(tv)) {
  63439. d1 = DUK_TVAL_GET_NUMBER(tv);
  63440. } else {
  63441. d1 = duk_to_number_tval(thr, tv); /* side effects */
  63442. }
  63443. if (opcode == DUK_OP_UNP) {
  63444. /* ToNumber() for a double is a no-op, but unary plus is
  63445. * used to force a fastint check so do that here.
  63446. */
  63447. du.d = d1;
  63448. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  63449. #if defined(DUK_USE_FASTINT)
  63450. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst);
  63451. DUK_TVAL_SET_NUMBER_CHKFAST_UPDREF(thr, tv, du.d); /* always 'fast', i.e. inlined */
  63452. return;
  63453. #endif
  63454. } else {
  63455. DUK_ASSERT(opcode == DUK_OP_UNM);
  63456. du.d = -d1;
  63457. DUK_DBLUNION_NORMALIZE_NAN_CHECK(&du); /* mandatory if du.d is a NaN */
  63458. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  63459. }
  63460. /* XXX: size optimize: push+replace? */
  63461. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst);
  63462. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv, du.d);
  63463. }
  63464. DUK_LOCAL DUK__INLINE_PERF void duk__vm_bitwise_not(duk_hthread *thr, duk_uint_fast_t idx_src, duk_uint_fast_t idx_dst) {
  63465. /*
  63466. * E5 Section 11.4.8
  63467. */
  63468. duk_tval *tv;
  63469. duk_int32_t i1, i2;
  63470. DUK_ASSERT(thr != NULL);
  63471. DUK_ASSERT_DISABLE(idx_src >= 0);
  63472. DUK_ASSERT_DISABLE(idx_dst >= 0);
  63473. DUK_ASSERT((duk_uint_t) idx_src < (duk_uint_t) duk_get_top(thr));
  63474. DUK_ASSERT((duk_uint_t) idx_dst < (duk_uint_t) duk_get_top(thr));
  63475. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_src);
  63476. #if defined(DUK_USE_FASTINT)
  63477. if (DUK_TVAL_IS_FASTINT(tv)) {
  63478. i1 = (duk_int32_t) DUK_TVAL_GET_FASTINT_I32(tv);
  63479. }
  63480. else
  63481. #endif /* DUK_USE_FASTINT */
  63482. {
  63483. duk_push_tval(thr, tv);
  63484. i1 = duk_to_int32(thr, -1); /* side effects */
  63485. duk_pop_unsafe(thr);
  63486. }
  63487. /* Result is always fastint compatible. */
  63488. i2 = ~i1;
  63489. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst);
  63490. DUK_TVAL_SET_I32_UPDREF(thr, tv, i2); /* side effects */
  63491. }
  63492. DUK_LOCAL DUK__INLINE_PERF void duk__vm_logical_not(duk_hthread *thr, duk_uint_fast_t idx_src, duk_uint_fast_t idx_dst) {
  63493. /*
  63494. * E5 Section 11.4.9
  63495. */
  63496. duk_tval *tv;
  63497. duk_bool_t res;
  63498. DUK_ASSERT(thr != NULL);
  63499. DUK_ASSERT_DISABLE(idx_src >= 0);
  63500. DUK_ASSERT_DISABLE(idx_dst >= 0);
  63501. DUK_ASSERT((duk_uint_t) idx_src < (duk_uint_t) duk_get_top(thr));
  63502. DUK_ASSERT((duk_uint_t) idx_dst < (duk_uint_t) duk_get_top(thr));
  63503. /* ToBoolean() does not require any operations with side effects so
  63504. * we can do it efficiently. For footprint it would be better to use
  63505. * duk_js_toboolean() and then push+replace to the result slot.
  63506. */
  63507. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_src);
  63508. res = duk_js_toboolean(tv); /* does not modify 'tv' */
  63509. DUK_ASSERT(res == 0 || res == 1);
  63510. res ^= 1;
  63511. tv = DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst);
  63512. /* XXX: size optimize: push+replace? */
  63513. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv, res); /* side effects */
  63514. }
  63515. /* XXX: size optimized variant */
  63516. DUK_LOCAL DUK__INLINE_PERF void duk__prepost_incdec_reg_helper(duk_hthread *thr, duk_tval *tv_dst, duk_tval *tv_src, duk_small_uint_t op) {
  63517. duk_double_t x, y, z;
  63518. /* Two lowest bits of opcode are used to distinguish
  63519. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  63520. */
  63521. DUK_ASSERT((DUK_OP_PREINCR & 0x03) == 0x00);
  63522. DUK_ASSERT((DUK_OP_PREDECR & 0x03) == 0x01);
  63523. DUK_ASSERT((DUK_OP_POSTINCR & 0x03) == 0x02);
  63524. DUK_ASSERT((DUK_OP_POSTDECR & 0x03) == 0x03);
  63525. #if defined(DUK_USE_FASTINT)
  63526. if (DUK_TVAL_IS_FASTINT(tv_src)) {
  63527. duk_int64_t x_fi, y_fi, z_fi;
  63528. x_fi = DUK_TVAL_GET_FASTINT(tv_src);
  63529. if (op & 0x01) {
  63530. if (DUK_UNLIKELY(x_fi == DUK_FASTINT_MIN)) {
  63531. goto skip_fastint;
  63532. }
  63533. y_fi = x_fi - 1;
  63534. } else {
  63535. if (DUK_UNLIKELY(x_fi == DUK_FASTINT_MAX)) {
  63536. goto skip_fastint;
  63537. }
  63538. y_fi = x_fi + 1;
  63539. }
  63540. DUK_TVAL_SET_FASTINT(tv_src, y_fi); /* no need for refcount update */
  63541. z_fi = (op & 0x02) ? x_fi : y_fi;
  63542. DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_dst, z_fi); /* side effects */
  63543. return;
  63544. }
  63545. skip_fastint:
  63546. #endif
  63547. if (DUK_TVAL_IS_NUMBER(tv_src)) {
  63548. /* Fast path for the case where the register
  63549. * is a number (e.g. loop counter).
  63550. */
  63551. x = DUK_TVAL_GET_NUMBER(tv_src);
  63552. if (op & 0x01) {
  63553. y = x - 1.0;
  63554. } else {
  63555. y = x + 1.0;
  63556. }
  63557. DUK_TVAL_SET_NUMBER(tv_src, y); /* no need for refcount update */
  63558. } else {
  63559. /* Preserve duk_tval pointer(s) across a potential valstack
  63560. * resize by converting them into offsets temporarily.
  63561. */
  63562. duk_idx_t bc;
  63563. duk_size_t off_dst;
  63564. off_dst = (duk_size_t) ((duk_uint8_t *) tv_dst - (duk_uint8_t *) thr->valstack_bottom);
  63565. bc = (duk_idx_t) (tv_src - thr->valstack_bottom); /* XXX: pass index explicitly? */
  63566. tv_src = NULL; /* no longer referenced */
  63567. x = duk_to_number(thr, bc);
  63568. if (op & 0x01) {
  63569. y = x - 1.0;
  63570. } else {
  63571. y = x + 1.0;
  63572. }
  63573. duk_push_number(thr, y);
  63574. duk_replace(thr, bc);
  63575. tv_dst = (duk_tval *) (void *) (((duk_uint8_t *) thr->valstack_bottom) + off_dst);
  63576. }
  63577. z = (op & 0x02) ? x : y;
  63578. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_dst, z); /* side effects */
  63579. }
  63580. DUK_LOCAL DUK__INLINE_PERF void duk__prepost_incdec_var_helper(duk_hthread *thr, duk_small_uint_t idx_dst, duk_tval *tv_id, duk_small_uint_t op, duk_small_uint_t is_strict) {
  63581. duk_activation *act;
  63582. duk_double_t x, y;
  63583. duk_hstring *name;
  63584. /* XXX: The pre/post inc/dec for an identifier lookup is
  63585. * missing the important fast path where the identifier
  63586. * has a storage location e.g. in a scope object so that
  63587. * it can be updated in-place. In particular, the case
  63588. * where the identifier has a storage location AND the
  63589. * previous value is a number should be optimized because
  63590. * it's side effect free.
  63591. */
  63592. /* Two lowest bits of opcode are used to distinguish
  63593. * variants. Bit 0 = inc(0)/dec(1), bit 1 = pre(0)/post(1).
  63594. */
  63595. DUK_ASSERT((DUK_OP_PREINCV & 0x03) == 0x00);
  63596. DUK_ASSERT((DUK_OP_PREDECV & 0x03) == 0x01);
  63597. DUK_ASSERT((DUK_OP_POSTINCV & 0x03) == 0x02);
  63598. DUK_ASSERT((DUK_OP_POSTDECV & 0x03) == 0x03);
  63599. DUK_ASSERT(DUK_TVAL_IS_STRING(tv_id));
  63600. name = DUK_TVAL_GET_STRING(tv_id);
  63601. DUK_ASSERT(name != NULL);
  63602. act = thr->callstack_curr;
  63603. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [ ... val this ] */
  63604. /* XXX: Fastint fast path would be useful here. Also fastints
  63605. * now lose their fastint status in current handling which is
  63606. * not intuitive.
  63607. */
  63608. x = duk_to_number_m2(thr);
  63609. if (op & 0x01) {
  63610. y = x - 1.0;
  63611. } else {
  63612. y = x + 1.0;
  63613. }
  63614. /* [... x this] */
  63615. if (op & 0x02) {
  63616. duk_push_number(thr, y); /* -> [ ... x this y ] */
  63617. DUK_ASSERT(act == thr->callstack_curr);
  63618. duk_js_putvar_activation(thr, act, name, DUK_GET_TVAL_NEGIDX(thr, -1), is_strict);
  63619. duk_pop_2_unsafe(thr); /* -> [ ... x ] */
  63620. } else {
  63621. duk_pop_2_unsafe(thr); /* -> [ ... ] */
  63622. duk_push_number(thr, y); /* -> [ ... y ] */
  63623. DUK_ASSERT(act == thr->callstack_curr);
  63624. duk_js_putvar_activation(thr, act, name, DUK_GET_TVAL_NEGIDX(thr, -1), is_strict);
  63625. }
  63626. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  63627. duk_replace(thr, (duk_idx_t) idx_dst);
  63628. #else /* DUK_USE_EXEC_PREFER_SIZE */
  63629. DUK__REPLACE_TO_TVPTR(thr, DUK_GET_TVAL_POSIDX(thr, (duk_idx_t) idx_dst));
  63630. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  63631. }
  63632. /*
  63633. * Longjmp and other control flow transfer for the bytecode executor.
  63634. *
  63635. * The longjmp handler can handle all longjmp types: error, yield, and
  63636. * resume (pseudotypes are never actually thrown).
  63637. *
  63638. * Error policy for longjmp: should not ordinarily throw errors; if errors
  63639. * occur (e.g. due to out-of-memory) they bubble outwards rather than being
  63640. * handled recursively.
  63641. */
  63642. #define DUK__LONGJMP_RESTART 0 /* state updated, restart bytecode execution */
  63643. #define DUK__LONGJMP_RETHROW 1 /* exit bytecode executor by rethrowing an error to caller */
  63644. #define DUK__RETHAND_RESTART 0 /* state updated, restart bytecode execution */
  63645. #define DUK__RETHAND_FINISHED 1 /* exit bytecode execution with return value */
  63646. /* XXX: optimize reconfig valstack operations so that resize, clamp, and setting
  63647. * top are combined into one pass.
  63648. */
  63649. /* Reconfigure value stack for return to an ECMAScript function at
  63650. * callstack top (caller unwinds).
  63651. */
  63652. DUK_LOCAL void duk__reconfig_valstack_ecma_return(duk_hthread *thr) {
  63653. duk_activation *act;
  63654. duk_hcompfunc *h_func;
  63655. duk_idx_t clamp_top;
  63656. DUK_ASSERT(thr != NULL);
  63657. act = thr->callstack_curr;
  63658. DUK_ASSERT(act != NULL);
  63659. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  63660. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(act)));
  63661. /* Clamp so that values at 'clamp_top' and above are wiped and won't
  63662. * retain reachable garbage. Then extend to 'nregs' because we're
  63663. * returning to an ECMAScript function.
  63664. */
  63665. h_func = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  63666. thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + act->bottom_byteoff);
  63667. DUK_ASSERT(act->retval_byteoff >= act->bottom_byteoff);
  63668. clamp_top = (duk_idx_t) ((act->retval_byteoff - act->bottom_byteoff + sizeof(duk_tval)) / sizeof(duk_tval)); /* +1 = one retval */
  63669. duk_set_top_and_wipe(thr, h_func->nregs, clamp_top);
  63670. DUK_ASSERT((duk_uint8_t *) thr->valstack_end >= (duk_uint8_t *) thr->valstack + act->reserve_byteoff);
  63671. thr->valstack_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + act->reserve_byteoff);
  63672. /* XXX: a best effort shrink check would be OK here */
  63673. }
  63674. /* Reconfigure value stack for an ECMAScript catcher. Use topmost catcher
  63675. * in 'act'.
  63676. */
  63677. DUK_LOCAL void duk__reconfig_valstack_ecma_catcher(duk_hthread *thr, duk_activation *act) {
  63678. duk_catcher *cat;
  63679. duk_hcompfunc *h_func;
  63680. duk_size_t idx_bottom;
  63681. duk_idx_t clamp_top;
  63682. DUK_ASSERT(thr != NULL);
  63683. DUK_ASSERT(act != NULL);
  63684. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  63685. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(act)));
  63686. cat = act->cat;
  63687. DUK_ASSERT(cat != NULL);
  63688. h_func = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  63689. thr->valstack_bottom = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + act->bottom_byteoff);
  63690. idx_bottom = (duk_size_t) (thr->valstack_bottom - thr->valstack);
  63691. DUK_ASSERT(cat->idx_base >= idx_bottom);
  63692. clamp_top = (duk_idx_t) (cat->idx_base - idx_bottom + 2); /* +2 = catcher value, catcher lj_type */
  63693. duk_set_top_and_wipe(thr, h_func->nregs, clamp_top);
  63694. DUK_ASSERT((duk_uint8_t *) thr->valstack_end >= (duk_uint8_t *) thr->valstack + act->reserve_byteoff);
  63695. thr->valstack_end = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + act->reserve_byteoff);
  63696. /* XXX: a best effort shrink check would be OK here */
  63697. }
  63698. /* Set catcher regs: idx_base+0 = value, idx_base+1 = lj_type.
  63699. * No side effects.
  63700. */
  63701. DUK_LOCAL void duk__set_catcher_regs_norz(duk_hthread *thr, duk_catcher *cat, duk_tval *tv_val_unstable, duk_small_uint_t lj_type) {
  63702. duk_tval *tv1;
  63703. DUK_ASSERT(thr != NULL);
  63704. DUK_ASSERT(tv_val_unstable != NULL);
  63705. tv1 = thr->valstack + cat->idx_base;
  63706. DUK_ASSERT(tv1 < thr->valstack_top);
  63707. DUK_TVAL_SET_TVAL_UPDREF_NORZ(thr, tv1, tv_val_unstable);
  63708. tv1++;
  63709. DUK_ASSERT(tv1 == thr->valstack + cat->idx_base + 1);
  63710. DUK_ASSERT(tv1 < thr->valstack_top);
  63711. DUK_TVAL_SET_U32_UPDREF_NORZ(thr, tv1, (duk_uint32_t) lj_type);
  63712. }
  63713. DUK_LOCAL void duk__handle_catch(duk_hthread *thr, duk_tval *tv_val_unstable, duk_small_uint_t lj_type) {
  63714. duk_activation *act;
  63715. duk_catcher *cat;
  63716. DUK_ASSERT(thr != NULL);
  63717. DUK_ASSERT(tv_val_unstable != NULL);
  63718. act = thr->callstack_curr;
  63719. DUK_ASSERT(act != NULL);
  63720. DUK_ASSERT(act->cat != NULL);
  63721. DUK_ASSERT(DUK_CAT_GET_TYPE(act->cat) == DUK_CAT_TYPE_TCF);
  63722. duk__set_catcher_regs_norz(thr, act->cat, tv_val_unstable, lj_type);
  63723. DUK_ASSERT(thr->callstack_top >= 1);
  63724. DUK_ASSERT(thr->callstack_curr != NULL);
  63725. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL);
  63726. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)));
  63727. DUK_ASSERT(thr->callstack_top >= 1);
  63728. DUK_ASSERT(act == thr->callstack_curr);
  63729. DUK_ASSERT(act != NULL);
  63730. duk__reconfig_valstack_ecma_catcher(thr, act);
  63731. DUK_ASSERT(thr->callstack_top >= 1);
  63732. DUK_ASSERT(act == thr->callstack_curr);
  63733. DUK_ASSERT(act != NULL);
  63734. cat = act->cat;
  63735. DUK_ASSERT(cat != NULL);
  63736. act->curr_pc = cat->pc_base + 0; /* +0 = catch */
  63737. /*
  63738. * If entering a 'catch' block which requires an automatic
  63739. * catch variable binding, create the lexical environment.
  63740. *
  63741. * The binding is mutable (= writable) but not deletable.
  63742. * Step 4 for the catch production in E5 Section 12.14;
  63743. * no value is given for CreateMutableBinding 'D' argument,
  63744. * which implies the binding is not deletable.
  63745. */
  63746. if (DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat)) {
  63747. duk_hdecenv *new_env;
  63748. DUK_DDD(DUK_DDDPRINT("catcher has an automatic catch binding"));
  63749. DUK_ASSERT(thr->callstack_top >= 1);
  63750. DUK_ASSERT(act == thr->callstack_curr);
  63751. DUK_ASSERT(act != NULL);
  63752. if (act->lex_env == NULL) {
  63753. DUK_ASSERT(act->var_env == NULL);
  63754. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  63755. duk_js_init_activation_environment_records_delayed(thr, act);
  63756. DUK_ASSERT(act == thr->callstack_curr);
  63757. DUK_ASSERT(act != NULL);
  63758. }
  63759. DUK_ASSERT(act->lex_env != NULL);
  63760. DUK_ASSERT(act->var_env != NULL);
  63761. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  63762. /* XXX: If an out-of-memory happens here, longjmp state asserts
  63763. * will be triggered at present and a try-catch fails to catch.
  63764. * That's not sandboxing fatal (C API protected calls are what
  63765. * matters), and script catch code can immediately throw anyway
  63766. * for almost any operation.
  63767. */
  63768. new_env = duk_hdecenv_alloc(thr,
  63769. DUK_HOBJECT_FLAG_EXTENSIBLE |
  63770. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV));
  63771. DUK_ASSERT(new_env != NULL);
  63772. duk_push_hobject(thr, (duk_hobject *) new_env);
  63773. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) new_env) == NULL);
  63774. DUK_DDD(DUK_DDDPRINT("new_env allocated: %!iO", (duk_heaphdr *) new_env));
  63775. /* Note: currently the catch binding is handled without a register
  63776. * binding because we don't support dynamic register bindings (they
  63777. * must be fixed for an entire function). So, there is no need to
  63778. * record regbases etc.
  63779. */
  63780. /* XXX: duk_xdef_prop() may cause an out-of-memory, see above. */
  63781. DUK_ASSERT(cat->h_varname != NULL);
  63782. duk_push_hstring(thr, cat->h_varname);
  63783. duk_push_tval(thr, thr->valstack + cat->idx_base);
  63784. duk_xdef_prop(thr, -3, DUK_PROPDESC_FLAGS_W); /* writable, not configurable */
  63785. DUK_ASSERT(act == thr->callstack_curr);
  63786. DUK_ASSERT(act != NULL);
  63787. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, (duk_hobject *) new_env, act->lex_env);
  63788. act->lex_env = (duk_hobject *) new_env;
  63789. DUK_HOBJECT_INCREF(thr, (duk_hobject *) new_env); /* reachable through activation */
  63790. /* Net refcount change to act->lex_env is 0: incref for new_env's
  63791. * prototype, decref for act->lex_env overwrite.
  63792. */
  63793. DUK_CAT_SET_LEXENV_ACTIVE(cat);
  63794. duk_pop_unsafe(thr);
  63795. DUK_DDD(DUK_DDDPRINT("new_env finished: %!iO", (duk_heaphdr *) new_env));
  63796. }
  63797. DUK_CAT_CLEAR_CATCH_ENABLED(cat);
  63798. }
  63799. DUK_LOCAL void duk__handle_finally(duk_hthread *thr, duk_tval *tv_val_unstable, duk_small_uint_t lj_type) {
  63800. duk_activation *act;
  63801. duk_catcher *cat;
  63802. DUK_ASSERT(thr != NULL);
  63803. DUK_ASSERT(tv_val_unstable != NULL);
  63804. act = thr->callstack_curr;
  63805. DUK_ASSERT(act != NULL);
  63806. DUK_ASSERT(act->cat != NULL);
  63807. DUK_ASSERT(DUK_CAT_GET_TYPE(act->cat) == DUK_CAT_TYPE_TCF);
  63808. duk__set_catcher_regs_norz(thr, act->cat, tv_val_unstable, lj_type);
  63809. DUK_ASSERT(thr->callstack_top >= 1);
  63810. DUK_ASSERT(thr->callstack_curr != NULL);
  63811. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL);
  63812. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)));
  63813. DUK_ASSERT(thr->callstack_top >= 1);
  63814. DUK_ASSERT(act == thr->callstack_curr);
  63815. DUK_ASSERT(act != NULL);
  63816. duk__reconfig_valstack_ecma_catcher(thr, act);
  63817. DUK_ASSERT(thr->callstack_top >= 1);
  63818. DUK_ASSERT(act == thr->callstack_curr);
  63819. DUK_ASSERT(act != NULL);
  63820. cat = act->cat;
  63821. DUK_ASSERT(cat != NULL);
  63822. act->curr_pc = cat->pc_base + 1; /* +1 = finally */
  63823. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  63824. }
  63825. DUK_LOCAL void duk__handle_label(duk_hthread *thr, duk_small_uint_t lj_type) {
  63826. duk_activation *act;
  63827. duk_catcher *cat;
  63828. DUK_ASSERT(thr != NULL);
  63829. DUK_ASSERT(thr->callstack_top >= 1);
  63830. act = thr->callstack_curr;
  63831. DUK_ASSERT(act != NULL);
  63832. DUK_ASSERT(DUK_ACT_GET_FUNC(act) != NULL);
  63833. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(DUK_ACT_GET_FUNC(act)));
  63834. /* +0 = break, +1 = continue */
  63835. cat = act->cat;
  63836. DUK_ASSERT(cat != NULL);
  63837. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL);
  63838. act->curr_pc = cat->pc_base + (lj_type == DUK_LJ_TYPE_CONTINUE ? 1 : 0);
  63839. /* valstack should not need changes */
  63840. #if defined(DUK_USE_ASSERTIONS)
  63841. DUK_ASSERT(thr->callstack_top >= 1);
  63842. DUK_ASSERT(act == thr->callstack_curr);
  63843. DUK_ASSERT(act != NULL);
  63844. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack_bottom) ==
  63845. (duk_size_t) ((duk_hcompfunc *) DUK_ACT_GET_FUNC(act))->nregs);
  63846. #endif
  63847. }
  63848. /* Called for handling both a longjmp() with type DUK_LJ_TYPE_YIELD and
  63849. * when a RETURN opcode terminates a thread and yields to the resumer.
  63850. * Caller unwinds so that top of callstack is the activation we return to.
  63851. */
  63852. #if defined(DUK_USE_COROUTINE_SUPPORT)
  63853. DUK_LOCAL void duk__handle_yield(duk_hthread *thr, duk_hthread *resumer, duk_tval *tv_val_unstable) {
  63854. duk_activation *act_resumer;
  63855. duk_tval *tv1;
  63856. DUK_ASSERT(thr != NULL);
  63857. DUK_ASSERT(resumer != NULL);
  63858. DUK_ASSERT(tv_val_unstable != NULL);
  63859. act_resumer = resumer->callstack_curr;
  63860. DUK_ASSERT(act_resumer != NULL);
  63861. DUK_ASSERT(DUK_ACT_GET_FUNC(act_resumer) != NULL);
  63862. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(act_resumer))); /* resume caller must be an ECMAScript func */
  63863. tv1 = (duk_tval *) (void *) ((duk_uint8_t *) resumer->valstack + act_resumer->retval_byteoff); /* return value from Duktape.Thread.resume() */
  63864. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv_val_unstable); /* side effects */ /* XXX: avoid side effects */
  63865. duk__reconfig_valstack_ecma_return(resumer);
  63866. /* caller must change active thread, and set thr->resumer to NULL */
  63867. }
  63868. #endif /* DUK_USE_COROUTINE_SUPPORT */
  63869. DUK_LOCAL duk_small_uint_t duk__handle_longjmp(duk_hthread *thr, duk_activation *entry_act) {
  63870. duk_small_uint_t retval = DUK__LONGJMP_RESTART;
  63871. DUK_ASSERT(thr != NULL);
  63872. DUK_ASSERT(entry_act != NULL);
  63873. /* 'thr' is the current thread, as no-one resumes except us and we
  63874. * switch 'thr' in that case.
  63875. */
  63876. DUK_ASSERT(thr == thr->heap->curr_thread);
  63877. /*
  63878. * (Re)try handling the longjmp.
  63879. *
  63880. * A longjmp handler may convert the longjmp to a different type and
  63881. * "virtually" rethrow by goto'ing to 'check_longjmp'. Before the goto,
  63882. * the following must be updated:
  63883. * - the heap 'lj' state
  63884. * - 'thr' must reflect the "throwing" thread
  63885. */
  63886. check_longjmp:
  63887. DUK_DD(DUK_DDPRINT("handling longjmp: type=%ld, value1=%!T, value2=%!T, iserror=%ld",
  63888. (long) thr->heap->lj.type,
  63889. (duk_tval *) &thr->heap->lj.value1,
  63890. (duk_tval *) &thr->heap->lj.value2,
  63891. (long) thr->heap->lj.iserror));
  63892. switch (thr->heap->lj.type) {
  63893. #if defined(DUK_USE_COROUTINE_SUPPORT)
  63894. case DUK_LJ_TYPE_RESUME: {
  63895. /*
  63896. * Note: lj.value1 is 'value', lj.value2 is 'resumee'.
  63897. * This differs from YIELD.
  63898. */
  63899. duk_tval *tv;
  63900. duk_tval *tv2;
  63901. duk_hthread *resumee;
  63902. /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
  63903. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged by Duktape.Thread.resume() */
  63904. DUK_ASSERT(thr->callstack_top >= 2); /* ECMAScript activation + Duktape.Thread.resume() activation */
  63905. DUK_ASSERT(thr->callstack_curr != NULL);
  63906. DUK_ASSERT(thr->callstack_curr->parent != NULL);
  63907. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL &&
  63908. DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)) &&
  63909. ((duk_hnatfunc *) DUK_ACT_GET_FUNC(thr->callstack_curr))->func == duk_bi_thread_resume);
  63910. tv = &thr->heap->lj.value2; /* resumee */
  63911. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  63912. DUK_ASSERT(DUK_TVAL_GET_OBJECT(tv) != NULL);
  63913. DUK_ASSERT(DUK_HOBJECT_IS_THREAD(DUK_TVAL_GET_OBJECT(tv)));
  63914. resumee = (duk_hthread *) DUK_TVAL_GET_OBJECT(tv);
  63915. DUK_ASSERT(resumee != NULL);
  63916. DUK_ASSERT(resumee->resumer == NULL);
  63917. DUK_ASSERT(resumee->state == DUK_HTHREAD_STATE_INACTIVE ||
  63918. resumee->state == DUK_HTHREAD_STATE_YIELDED); /* checked by Duktape.Thread.resume() */
  63919. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  63920. resumee->callstack_top >= 2); /* YIELDED: ECMAScript activation + Duktape.Thread.yield() activation */
  63921. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_YIELDED ||
  63922. (DUK_ACT_GET_FUNC(resumee->callstack_curr) != NULL &&
  63923. DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(resumee->callstack_curr)) &&
  63924. ((duk_hnatfunc *) DUK_ACT_GET_FUNC(resumee->callstack_curr))->func == duk_bi_thread_yield));
  63925. DUK_ASSERT(resumee->state != DUK_HTHREAD_STATE_INACTIVE ||
  63926. resumee->callstack_top == 0); /* INACTIVE: no activation, single function value on valstack */
  63927. if (thr->heap->lj.iserror) {
  63928. /*
  63929. * Throw the error in the resumed thread's context; the
  63930. * error value is pushed onto the resumee valstack.
  63931. *
  63932. * Note: the callstack of the target may empty in this case
  63933. * too (i.e. the target thread has never been resumed). The
  63934. * value stack will contain the initial function in that case,
  63935. * which we simply ignore.
  63936. */
  63937. DUK_ASSERT(resumee->resumer == NULL);
  63938. resumee->resumer = thr;
  63939. DUK_HTHREAD_INCREF(thr, thr);
  63940. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  63941. thr->state = DUK_HTHREAD_STATE_RESUMED;
  63942. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  63943. thr = resumee;
  63944. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  63945. /* thr->heap->lj.value1 is already the value to throw */
  63946. /* thr->heap->lj.value2 is 'thread', will be wiped out at the end */
  63947. DUK_ASSERT(thr->heap->lj.iserror); /* already set */
  63948. DUK_DD(DUK_DDPRINT("-> resume with an error, converted to a throw in the resumee, propagate"));
  63949. goto check_longjmp;
  63950. } else if (resumee->state == DUK_HTHREAD_STATE_YIELDED) {
  63951. /* Unwind previous Duktape.Thread.yield() call. The
  63952. * activation remaining must always be an ECMAScript
  63953. * call now (yield() accepts calls from ECMAScript
  63954. * only).
  63955. */
  63956. duk_activation *act_resumee;
  63957. DUK_ASSERT(resumee->callstack_top >= 2);
  63958. act_resumee = resumee->callstack_curr; /* Duktape.Thread.yield() */
  63959. DUK_ASSERT(act_resumee != NULL);
  63960. act_resumee = act_resumee->parent; /* ECMAScript call site for yield() */
  63961. DUK_ASSERT(act_resumee != NULL);
  63962. tv = (duk_tval *) (void *) ((duk_uint8_t *) resumee->valstack + act_resumee->retval_byteoff); /* return value from Duktape.Thread.yield() */
  63963. DUK_ASSERT(tv >= resumee->valstack && tv < resumee->valstack_top);
  63964. tv2 = &thr->heap->lj.value1;
  63965. DUK_TVAL_SET_TVAL_UPDREF(thr, tv, tv2); /* side effects */ /* XXX: avoid side effects */
  63966. duk_hthread_activation_unwind_norz(resumee); /* unwind to 'yield' caller */
  63967. /* no need to unwind catch stack */
  63968. duk__reconfig_valstack_ecma_return(resumee);
  63969. DUK_ASSERT(resumee->resumer == NULL);
  63970. resumee->resumer = thr;
  63971. DUK_HTHREAD_INCREF(thr, thr);
  63972. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  63973. thr->state = DUK_HTHREAD_STATE_RESUMED;
  63974. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  63975. #if 0
  63976. thr = resumee; /* not needed, as we exit right away */
  63977. #endif
  63978. DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
  63979. retval = DUK__LONGJMP_RESTART;
  63980. goto wipe_and_return;
  63981. } else {
  63982. /* Initial resume call. */
  63983. duk_small_uint_t call_flags;
  63984. duk_int_t setup_rc;
  63985. /* resumee: [... initial_func] (currently actually: [initial_func]) */
  63986. duk_push_undefined(resumee);
  63987. tv = &thr->heap->lj.value1;
  63988. duk_push_tval(resumee, tv);
  63989. /* resumee: [... initial_func undefined(= this) resume_value ] */
  63990. call_flags = DUK_CALL_FLAG_ALLOW_ECMATOECMA; /* not tailcall, ecma-to-ecma (assumed to succeed) */
  63991. setup_rc = duk_handle_call_unprotected_nargs(resumee, 1 /*nargs*/, call_flags);
  63992. if (setup_rc == 0) {
  63993. /* This shouldn't happen; Duktape.Thread.resume()
  63994. * should make sure of that. If it does happen
  63995. * this internal error will propagate out of the
  63996. * executor which can be quite misleading.
  63997. */
  63998. DUK_ERROR_INTERNAL(thr);
  63999. DUK_WO_NORETURN(return 0;);
  64000. }
  64001. DUK_ASSERT(resumee->resumer == NULL);
  64002. resumee->resumer = thr;
  64003. DUK_HTHREAD_INCREF(thr, thr);
  64004. resumee->state = DUK_HTHREAD_STATE_RUNNING;
  64005. thr->state = DUK_HTHREAD_STATE_RESUMED;
  64006. DUK_HEAP_SWITCH_THREAD(thr->heap, resumee);
  64007. #if 0
  64008. thr = resumee; /* not needed, as we exit right away */
  64009. #endif
  64010. DUK_DD(DUK_DDPRINT("-> resume with a value, restart execution in resumee"));
  64011. retval = DUK__LONGJMP_RESTART;
  64012. goto wipe_and_return;
  64013. }
  64014. DUK_UNREACHABLE();
  64015. break; /* never here */
  64016. }
  64017. case DUK_LJ_TYPE_YIELD: {
  64018. /*
  64019. * Currently only allowed only if yielding thread has only
  64020. * ECMAScript activations (except for the Duktape.Thread.yield()
  64021. * call at the callstack top) and none of them constructor
  64022. * calls.
  64023. *
  64024. * This excludes the 'entry' thread which will always have
  64025. * a preventcount > 0.
  64026. */
  64027. duk_hthread *resumer;
  64028. /* duk_bi_duk_object_yield() and duk_bi_duk_object_resume() ensure all of these are met */
  64029. #if 0 /* entry_thread not available for assert */
  64030. DUK_ASSERT(thr != entry_thread); /* Duktape.Thread.yield() should prevent */
  64031. #endif
  64032. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING); /* unchanged from Duktape.Thread.yield() */
  64033. DUK_ASSERT(thr->callstack_top >= 2); /* ECMAScript activation + Duktape.Thread.yield() activation */
  64034. DUK_ASSERT(thr->callstack_curr != NULL);
  64035. DUK_ASSERT(thr->callstack_curr->parent != NULL);
  64036. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL &&
  64037. DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)) &&
  64038. ((duk_hnatfunc *) DUK_ACT_GET_FUNC(thr->callstack_curr))->func == duk_bi_thread_yield);
  64039. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr->parent) != NULL &&
  64040. DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr->parent))); /* an ECMAScript function */
  64041. resumer = thr->resumer;
  64042. DUK_ASSERT(resumer != NULL);
  64043. DUK_ASSERT(resumer->state == DUK_HTHREAD_STATE_RESUMED); /* written by a previous RESUME handling */
  64044. DUK_ASSERT(resumer->callstack_top >= 2); /* ECMAScript activation + Duktape.Thread.resume() activation */
  64045. DUK_ASSERT(resumer->callstack_curr != NULL);
  64046. DUK_ASSERT(resumer->callstack_curr->parent != NULL);
  64047. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack_curr) != NULL &&
  64048. DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(resumer->callstack_curr)) &&
  64049. ((duk_hnatfunc *) DUK_ACT_GET_FUNC(resumer->callstack_curr))->func == duk_bi_thread_resume);
  64050. DUK_ASSERT(DUK_ACT_GET_FUNC(resumer->callstack_curr->parent) != NULL &&
  64051. DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(resumer->callstack_curr->parent))); /* an ECMAScript function */
  64052. if (thr->heap->lj.iserror) {
  64053. thr->state = DUK_HTHREAD_STATE_YIELDED;
  64054. thr->resumer = NULL;
  64055. DUK_HTHREAD_DECREF_NORZ(thr, resumer);
  64056. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  64057. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  64058. thr = resumer;
  64059. thr->heap->lj.type = DUK_LJ_TYPE_THROW;
  64060. /* lj.value1 is already set */
  64061. DUK_ASSERT(thr->heap->lj.iserror); /* already set */
  64062. DUK_DD(DUK_DDPRINT("-> yield an error, converted to a throw in the resumer, propagate"));
  64063. goto check_longjmp;
  64064. } else {
  64065. duk_hthread_activation_unwind_norz(resumer);
  64066. duk__handle_yield(thr, resumer, &thr->heap->lj.value1);
  64067. thr->state = DUK_HTHREAD_STATE_YIELDED;
  64068. thr->resumer = NULL;
  64069. DUK_HTHREAD_DECREF_NORZ(thr, resumer);
  64070. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  64071. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  64072. #if 0
  64073. thr = resumer; /* not needed, as we exit right away */
  64074. #endif
  64075. DUK_DD(DUK_DDPRINT("-> yield a value, restart execution in resumer"));
  64076. retval = DUK__LONGJMP_RESTART;
  64077. goto wipe_and_return;
  64078. }
  64079. DUK_UNREACHABLE();
  64080. break; /* never here */
  64081. }
  64082. #endif /* DUK_USE_COROUTINE_SUPPORT */
  64083. case DUK_LJ_TYPE_THROW: {
  64084. /*
  64085. * Three possible outcomes:
  64086. * * A try or finally catcher is found => resume there.
  64087. * (or)
  64088. * * The error propagates to the bytecode executor entry
  64089. * level (and we're in the entry thread) => rethrow
  64090. * with a new longjmp(), after restoring the previous
  64091. * catchpoint.
  64092. * * The error is not caught in the current thread, so
  64093. * the thread finishes with an error. This works like
  64094. * a yielded error, except that the thread is finished
  64095. * and can no longer be resumed. (There is always a
  64096. * resumer in this case.)
  64097. *
  64098. * Note: until we hit the entry level, there can only be
  64099. * ECMAScript activations.
  64100. */
  64101. duk_activation *act;
  64102. duk_catcher *cat;
  64103. duk_hthread *resumer;
  64104. for (;;) {
  64105. act = thr->callstack_curr;
  64106. if (act == NULL) {
  64107. break;
  64108. }
  64109. for (;;) {
  64110. cat = act->cat;
  64111. if (cat == NULL) {
  64112. break;
  64113. }
  64114. if (DUK_CAT_HAS_CATCH_ENABLED(cat)) {
  64115. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
  64116. duk__handle_catch(thr,
  64117. &thr->heap->lj.value1,
  64118. DUK_LJ_TYPE_THROW);
  64119. DUK_DD(DUK_DDPRINT("-> throw caught by a 'catch' clause, restart execution"));
  64120. retval = DUK__LONGJMP_RESTART;
  64121. goto wipe_and_return;
  64122. }
  64123. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  64124. DUK_ASSERT(DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF);
  64125. DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat));
  64126. duk__handle_finally(thr,
  64127. &thr->heap->lj.value1,
  64128. DUK_LJ_TYPE_THROW);
  64129. DUK_DD(DUK_DDPRINT("-> throw caught by a 'finally' clause, restart execution"));
  64130. retval = DUK__LONGJMP_RESTART;
  64131. goto wipe_and_return;
  64132. }
  64133. duk_hthread_catcher_unwind_norz(thr, act);
  64134. }
  64135. if (act == entry_act) {
  64136. /* Not caught by anything before entry level; rethrow and let the
  64137. * final catcher finish unwinding (esp. value stack).
  64138. */
  64139. DUK_D(DUK_DPRINT("-> throw propagated up to entry level, rethrow and exit bytecode executor"));
  64140. retval = DUK__LONGJMP_RETHROW;
  64141. goto just_return;
  64142. }
  64143. duk_hthread_activation_unwind_norz(thr);
  64144. }
  64145. DUK_DD(DUK_DDPRINT("-> throw not caught by current thread, yield error to resumer and recheck longjmp"));
  64146. /* Not caught by current thread, thread terminates (yield error to resumer);
  64147. * note that this may cause a cascade if the resumer terminates with an uncaught
  64148. * exception etc (this is OK, but needs careful testing).
  64149. */
  64150. DUK_ASSERT(thr->resumer != NULL);
  64151. DUK_ASSERT(thr->resumer->callstack_top >= 2); /* ECMAScript activation + Duktape.Thread.resume() activation */
  64152. DUK_ASSERT(thr->resumer->callstack_curr != NULL);
  64153. DUK_ASSERT(thr->resumer->callstack_curr->parent != NULL);
  64154. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack_curr->parent) != NULL &&
  64155. DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->resumer->callstack_curr->parent))); /* an ECMAScript function */
  64156. resumer = thr->resumer;
  64157. /* reset longjmp */
  64158. DUK_ASSERT(thr->heap->lj.type == DUK_LJ_TYPE_THROW); /* already set */
  64159. /* lj.value1 already set */
  64160. duk_hthread_terminate(thr); /* updates thread state, minimizes its allocations */
  64161. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
  64162. thr->resumer = NULL;
  64163. DUK_HTHREAD_DECREF_NORZ(thr, resumer);
  64164. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  64165. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  64166. thr = resumer;
  64167. goto check_longjmp;
  64168. }
  64169. case DUK_LJ_TYPE_BREAK: /* pseudotypes, not used in actual longjmps */
  64170. case DUK_LJ_TYPE_CONTINUE:
  64171. case DUK_LJ_TYPE_RETURN:
  64172. case DUK_LJ_TYPE_NORMAL:
  64173. default: {
  64174. /* should never happen, but be robust */
  64175. DUK_D(DUK_DPRINT("caught unknown longjmp type %ld, treat as internal error", (long) thr->heap->lj.type));
  64176. goto convert_to_internal_error;
  64177. }
  64178. } /* end switch */
  64179. DUK_UNREACHABLE();
  64180. wipe_and_return:
  64181. /* this is not strictly necessary, but helps debugging */
  64182. thr->heap->lj.type = DUK_LJ_TYPE_UNKNOWN;
  64183. thr->heap->lj.iserror = 0;
  64184. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value1); /* side effects */
  64185. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, &thr->heap->lj.value2); /* side effects */
  64186. DUK_GC_TORTURE(thr->heap);
  64187. just_return:
  64188. return retval;
  64189. convert_to_internal_error:
  64190. /* This could also be thrown internally (set the error, goto check_longjmp),
  64191. * but it's better for internal errors to bubble outwards so that we won't
  64192. * infinite loop in this catchpoint.
  64193. */
  64194. DUK_ERROR_INTERNAL(thr);
  64195. DUK_WO_NORETURN(return 0;);
  64196. }
  64197. /* Handle a BREAK/CONTINUE opcode. Avoid using longjmp() for BREAK/CONTINUE
  64198. * handling because it has a measurable performance impact in ordinary
  64199. * environments and an extreme impact in Emscripten (GH-342).
  64200. */
  64201. DUK_LOCAL DUK__NOINLINE_PERF void duk__handle_break_or_continue(duk_hthread *thr,
  64202. duk_uint_t label_id,
  64203. duk_small_uint_t lj_type) {
  64204. duk_activation *act;
  64205. duk_catcher *cat;
  64206. DUK_ASSERT(thr != NULL);
  64207. /* Find a matching label catcher or 'finally' catcher in
  64208. * the same function, unwinding catchers as we go.
  64209. *
  64210. * A label catcher must always exist and will match unless
  64211. * a 'finally' captures the break/continue first. It is the
  64212. * compiler's responsibility to ensure that labels are used
  64213. * correctly.
  64214. */
  64215. act = thr->callstack_curr;
  64216. DUK_ASSERT(act != NULL);
  64217. for (;;) {
  64218. cat = act->cat;
  64219. if (cat == NULL) {
  64220. break;
  64221. }
  64222. DUK_DDD(DUK_DDDPRINT("considering catcher %p: type=%ld label=%ld",
  64223. (void *) cat,
  64224. (long) DUK_CAT_GET_TYPE(cat),
  64225. (long) DUK_CAT_GET_LABEL(cat)));
  64226. /* XXX: bit mask test; FINALLY <-> TCF, single bit mask would suffice? */
  64227. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
  64228. DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  64229. duk_tval tv_tmp;
  64230. DUK_TVAL_SET_U32(&tv_tmp, (duk_uint32_t) label_id);
  64231. duk__handle_finally(thr, &tv_tmp, lj_type);
  64232. DUK_DD(DUK_DDPRINT("-> break/continue caught by 'finally', restart execution"));
  64233. return;
  64234. }
  64235. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_LABEL &&
  64236. (duk_uint_t) DUK_CAT_GET_LABEL(cat) == label_id) {
  64237. duk__handle_label(thr, lj_type);
  64238. DUK_DD(DUK_DDPRINT("-> break/continue caught by a label catcher (in the same function), restart execution"));
  64239. return;
  64240. }
  64241. duk_hthread_catcher_unwind_norz(thr, act);
  64242. }
  64243. /* Should never happen, but be robust. */
  64244. DUK_D(DUK_DPRINT("-> break/continue not caught by anything in the current function (should never happen), throw internal error"));
  64245. DUK_ERROR_INTERNAL(thr);
  64246. DUK_WO_NORETURN(return;);
  64247. }
  64248. /* Handle a RETURN opcode. Avoid using longjmp() for return handling because
  64249. * it has a measurable performance impact in ordinary environments and an extreme
  64250. * impact in Emscripten (GH-342). Return value is on value stack top.
  64251. */
  64252. DUK_LOCAL duk_small_uint_t duk__handle_return(duk_hthread *thr, duk_activation *entry_act) {
  64253. duk_tval *tv1;
  64254. duk_tval *tv2;
  64255. #if defined(DUK_USE_COROUTINE_SUPPORT)
  64256. duk_hthread *resumer;
  64257. #endif
  64258. duk_activation *act;
  64259. duk_catcher *cat;
  64260. /* We can directly access value stack here. */
  64261. DUK_ASSERT(thr != NULL);
  64262. DUK_ASSERT(entry_act != NULL);
  64263. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  64264. tv1 = thr->valstack_top - 1;
  64265. DUK_TVAL_CHKFAST_INPLACE_FAST(tv1); /* fastint downgrade check for return values */
  64266. /*
  64267. * Four possible outcomes:
  64268. *
  64269. * 1. A 'finally' in the same function catches the 'return'.
  64270. * It may continue to propagate when 'finally' is finished,
  64271. * or it may be neutralized by 'finally' (both handled by
  64272. * ENDFIN).
  64273. *
  64274. * 2. The return happens at the entry level of the bytecode
  64275. * executor, so return from the executor (in C stack).
  64276. *
  64277. * 3. There is a calling (ECMAScript) activation in the call
  64278. * stack => return to it, in the same executor instance.
  64279. *
  64280. * 4. There is no calling activation, and the thread is
  64281. * terminated. There is always a resumer in this case,
  64282. * which gets the return value similarly to a 'yield'
  64283. * (except that the current thread can no longer be
  64284. * resumed).
  64285. */
  64286. DUK_ASSERT(thr != NULL);
  64287. DUK_ASSERT(thr->callstack_top >= 1);
  64288. act = thr->callstack_curr;
  64289. DUK_ASSERT(act != NULL);
  64290. for (;;) {
  64291. cat = act->cat;
  64292. if (cat == NULL) {
  64293. break;
  64294. }
  64295. if (DUK_CAT_GET_TYPE(cat) == DUK_CAT_TYPE_TCF &&
  64296. DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  64297. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  64298. duk__handle_finally(thr, thr->valstack_top - 1, DUK_LJ_TYPE_RETURN);
  64299. DUK_DD(DUK_DDPRINT("-> return caught by 'finally', restart execution"));
  64300. return DUK__RETHAND_RESTART;
  64301. }
  64302. duk_hthread_catcher_unwind_norz(thr, act);
  64303. }
  64304. if (act == entry_act) {
  64305. /* Return to the bytecode executor caller who will unwind stacks
  64306. * and handle constructor post-processing.
  64307. * Return value is already on the stack top: [ ... retval ].
  64308. */
  64309. DUK_DDD(DUK_DDDPRINT("-> return propagated up to entry level, exit bytecode executor"));
  64310. return DUK__RETHAND_FINISHED;
  64311. }
  64312. if (thr->callstack_top >= 2) {
  64313. /* There is a caller; it MUST be an ECMAScript caller (otherwise it would
  64314. * match entry_act check).
  64315. */
  64316. DUK_DDD(DUK_DDDPRINT("return to ECMAScript caller, retval_byteoff=%ld, lj_value1=%!T",
  64317. (long) (thr->callstack_curr->parent->retval_byteoff),
  64318. (duk_tval *) &thr->heap->lj.value1));
  64319. DUK_ASSERT(thr->callstack_curr != NULL);
  64320. DUK_ASSERT(thr->callstack_curr->parent != NULL);
  64321. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr->parent))); /* must be ECMAScript */
  64322. #if defined(DUK_USE_ES6_PROXY)
  64323. if (thr->callstack_curr->flags & (DUK_ACT_FLAG_CONSTRUCT | DUK_ACT_FLAG_CONSTRUCT_PROXY)) {
  64324. duk_call_construct_postprocess(thr, thr->callstack_curr->flags & DUK_ACT_FLAG_CONSTRUCT_PROXY); /* side effects */
  64325. }
  64326. #else
  64327. if (thr->callstack_curr->flags & DUK_ACT_FLAG_CONSTRUCT) {
  64328. duk_call_construct_postprocess(thr, 0); /* side effects */
  64329. }
  64330. #endif
  64331. tv1 = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + thr->callstack_curr->parent->retval_byteoff);
  64332. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  64333. tv2 = thr->valstack_top - 1;
  64334. DUK_TVAL_SET_TVAL_UPDREF(thr, tv1, tv2); /* side effects */
  64335. /* Catch stack unwind happens inline in callstack unwind. */
  64336. duk_hthread_activation_unwind_norz(thr);
  64337. duk__reconfig_valstack_ecma_return(thr);
  64338. DUK_DD(DUK_DDPRINT("-> return not intercepted, restart execution in caller"));
  64339. return DUK__RETHAND_RESTART;
  64340. }
  64341. #if defined(DUK_USE_COROUTINE_SUPPORT)
  64342. DUK_DD(DUK_DDPRINT("no calling activation, thread finishes (similar to yield)"));
  64343. DUK_ASSERT(thr->resumer != NULL);
  64344. DUK_ASSERT(thr->resumer->callstack_top >= 2); /* ECMAScript activation + Duktape.Thread.resume() activation */
  64345. DUK_ASSERT(thr->resumer->callstack_curr != NULL);
  64346. DUK_ASSERT(thr->resumer->callstack_curr->parent != NULL);
  64347. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack_curr) != NULL &&
  64348. DUK_HOBJECT_IS_NATFUNC(DUK_ACT_GET_FUNC(thr->resumer->callstack_curr)) &&
  64349. ((duk_hnatfunc *) DUK_ACT_GET_FUNC(thr->resumer->callstack_curr))->func == duk_bi_thread_resume); /* Duktape.Thread.resume() */
  64350. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->resumer->callstack_curr->parent) != NULL &&
  64351. DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->resumer->callstack_curr->parent))); /* an ECMAScript function */
  64352. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_RUNNING);
  64353. DUK_ASSERT(thr->resumer->state == DUK_HTHREAD_STATE_RESUMED);
  64354. resumer = thr->resumer;
  64355. /* Share yield longjmp handler.
  64356. *
  64357. * This sequence of steps is a bit fragile (see GH-1845):
  64358. * - We need the return value from 'thr' (resumed thread) value stack.
  64359. * The termination unwinds its value stack, losing the value.
  64360. * - We need a refcounted reference for 'thr', which may only exist
  64361. * in the caller value stack. We can't unwind or reconfigure the
  64362. * caller's value stack without potentially freeing 'thr'.
  64363. *
  64364. * Current approach is to capture the 'thr' return value and store
  64365. * a reference to 'thr' in the caller value stack temporarily. This
  64366. * keeps 'thr' reachable until final yield/return handling which
  64367. * removes the references atomatically.
  64368. */
  64369. DUK_ASSERT(thr->valstack_top - 1 >= thr->valstack_bottom);
  64370. duk_hthread_activation_unwind_norz(resumer); /* May remove last reference to 'thr', but is NORZ. */
  64371. duk_push_tval(resumer, thr->valstack_top - 1); /* Capture return value, side effect free. */
  64372. duk_push_hthread(resumer, thr); /* Make 'thr' reachable again, before side effects. */
  64373. duk_hthread_terminate(thr); /* Updates thread state, minimizes its allocations. */
  64374. thr->resumer = NULL;
  64375. DUK_HTHREAD_DECREF(thr, resumer);
  64376. DUK_ASSERT(thr->state == DUK_HTHREAD_STATE_TERMINATED);
  64377. resumer->state = DUK_HTHREAD_STATE_RUNNING;
  64378. DUK_HEAP_SWITCH_THREAD(thr->heap, resumer);
  64379. DUK_ASSERT(resumer->valstack_top - 2 >= resumer->valstack_bottom);
  64380. duk__handle_yield(thr, resumer, resumer->valstack_top - 2);
  64381. thr = NULL; /* 'thr' invalidated by call */
  64382. #if 0
  64383. thr = resumer; /* not needed */
  64384. #endif
  64385. DUK_DD(DUK_DDPRINT("-> return not caught, thread terminated; handle like yield, restart execution in resumer"));
  64386. return DUK__RETHAND_RESTART;
  64387. #else
  64388. /* Without coroutine support this case should never happen. */
  64389. DUK_ERROR_INTERNAL(thr);
  64390. DUK_WO_NORETURN(return 0;);
  64391. #endif
  64392. }
  64393. /*
  64394. * Executor interrupt handling
  64395. *
  64396. * The handler is called whenever the interrupt countdown reaches zero
  64397. * (or below). The handler must perform whatever checks are activated,
  64398. * e.g. check for cumulative step count to impose an execution step
  64399. * limit or check for breakpoints or other debugger interaction.
  64400. *
  64401. * When the actions are done, the handler must reinit the interrupt
  64402. * init and counter values. The 'init' value must indicate how many
  64403. * bytecode instructions are executed before the next interrupt. The
  64404. * counter must interface with the bytecode executor loop. Concretely,
  64405. * the new init value is normally one higher than the new counter value.
  64406. * For instance, to execute exactly one bytecode instruction the init
  64407. * value is set to 1 and the counter to 0. If an error is thrown by the
  64408. * interrupt handler, the counters are set to the same value (e.g. both
  64409. * to 0 to cause an interrupt when the next bytecode instruction is about
  64410. * to be executed after error handling).
  64411. *
  64412. * Maintaining the init/counter value properly is important for accurate
  64413. * behavior. For instance, executor step limit needs a cumulative step
  64414. * count which is simply computed as a sum of 'init' values. This must
  64415. * work accurately even when single stepping.
  64416. */
  64417. #if defined(DUK_USE_INTERRUPT_COUNTER)
  64418. #define DUK__INT_NOACTION 0 /* no specific action, resume normal execution */
  64419. #define DUK__INT_RESTART 1 /* must "goto restart_execution", e.g. breakpoints changed */
  64420. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  64421. DUK_LOCAL void duk__interrupt_handle_debugger(duk_hthread *thr, duk_bool_t *out_immediate, duk_small_uint_t *out_interrupt_retval) {
  64422. duk_activation *act;
  64423. duk_breakpoint *bp;
  64424. duk_breakpoint **bp_active;
  64425. duk_uint_fast32_t line = 0;
  64426. duk_bool_t process_messages;
  64427. duk_bool_t processed_messages = 0;
  64428. DUK_ASSERT(thr->heap->dbg_processing == 0); /* don't re-enter e.g. during Eval */
  64429. act = thr->callstack_curr;
  64430. DUK_ASSERT(act != NULL);
  64431. /* It might seem that replacing 'thr->heap' with just 'heap' below
  64432. * might be a good idea, but it increases code size slightly
  64433. * (probably due to unnecessary spilling) at least on x64.
  64434. */
  64435. /*
  64436. * Single opcode step check
  64437. */
  64438. if (thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_ONE_OPCODE_ACTIVE) {
  64439. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by one opcode step"));
  64440. duk_debug_set_paused(thr->heap);
  64441. }
  64442. /*
  64443. * Breakpoint and step state checks
  64444. */
  64445. if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE ||
  64446. (thr->heap->dbg_pause_act == thr->callstack_curr)) {
  64447. line = duk_debug_curr_line(thr);
  64448. if (act->prev_line != line) {
  64449. /* Stepped? Step out is handled by callstack unwind. */
  64450. if ((thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_LINE_CHANGE) &&
  64451. (thr->heap->dbg_pause_act == thr->callstack_curr) &&
  64452. (line != thr->heap->dbg_pause_startline)) {
  64453. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by line change, at line %ld",
  64454. (long) line));
  64455. duk_debug_set_paused(thr->heap);
  64456. }
  64457. /* Check for breakpoints only on line transition.
  64458. * Breakpoint is triggered when we enter the target
  64459. * line from a different line, and the previous line
  64460. * was within the same function.
  64461. *
  64462. * This condition is tricky: the condition used to be
  64463. * that transition to -or across- the breakpoint line
  64464. * triggered the breakpoint. This seems intuitively
  64465. * better because it handles breakpoints on lines with
  64466. * no emitted opcodes; but this leads to the issue
  64467. * described in: https://github.com/svaarala/duktape/issues/263.
  64468. */
  64469. bp_active = thr->heap->dbg_breakpoints_active;
  64470. for (;;) {
  64471. bp = *bp_active++;
  64472. if (bp == NULL) {
  64473. break;
  64474. }
  64475. DUK_ASSERT(bp->filename != NULL);
  64476. if (act->prev_line != bp->line && line == bp->line) {
  64477. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by breakpoint at %!O:%ld",
  64478. (duk_heaphdr *) bp->filename, (long) bp->line));
  64479. duk_debug_set_paused(thr->heap);
  64480. }
  64481. }
  64482. } else {
  64483. ;
  64484. }
  64485. act->prev_line = (duk_uint32_t) line;
  64486. }
  64487. /*
  64488. * Rate limit check for sending status update or peeking into
  64489. * the debug transport. Both can be expensive operations that
  64490. * we don't want to do on every opcode.
  64491. *
  64492. * Making sure the interval remains reasonable on a wide variety
  64493. * of targets and bytecode is difficult without a timestamp, so
  64494. * we use a Date-provided timestamp for the rate limit check.
  64495. * But since it's also expensive to get a timestamp, a bytecode
  64496. * counter is used to rate limit getting timestamps.
  64497. */
  64498. process_messages = 0;
  64499. if (thr->heap->dbg_state_dirty || DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap) || thr->heap->dbg_detaching) {
  64500. /* Enter message processing loop for sending Status notifys and
  64501. * to finish a pending detach.
  64502. */
  64503. process_messages = 1;
  64504. }
  64505. /* XXX: remove heap->dbg_exec_counter, use heap->inst_count_interrupt instead? */
  64506. DUK_ASSERT(thr->interrupt_init >= 0);
  64507. thr->heap->dbg_exec_counter += (duk_uint_t) thr->interrupt_init;
  64508. if (thr->heap->dbg_exec_counter - thr->heap->dbg_last_counter >= DUK_HEAP_DBG_RATELIMIT_OPCODES) {
  64509. /* Overflow of the execution counter is fine and doesn't break
  64510. * anything here.
  64511. */
  64512. duk_double_t now, diff_last;
  64513. thr->heap->dbg_last_counter = thr->heap->dbg_exec_counter;
  64514. now = duk_time_get_monotonic_time(thr);
  64515. diff_last = now - thr->heap->dbg_last_time;
  64516. if (diff_last < 0.0 || diff_last >= (duk_double_t) DUK_HEAP_DBG_RATELIMIT_MILLISECS) {
  64517. /* Monotonic time should not experience time jumps,
  64518. * but the provider may be missing and we're actually
  64519. * using ECMAScript time. So, tolerate negative values
  64520. * so that a time jump works reasonably.
  64521. *
  64522. * Same interval is now used for status sending and
  64523. * peeking.
  64524. */
  64525. thr->heap->dbg_last_time = now;
  64526. thr->heap->dbg_state_dirty = 1;
  64527. process_messages = 1;
  64528. }
  64529. }
  64530. /*
  64531. * Process messages and send status if necessary.
  64532. *
  64533. * If we're paused, we'll block for new messages. If we're not
  64534. * paused, we'll process anything we can peek but won't block
  64535. * for more. Detach (and re-attach) handling is all localized
  64536. * to duk_debug_process_messages() too.
  64537. *
  64538. * Debugger writes outside the message loop may cause debugger
  64539. * detach1 phase to run, after which dbg_read_cb == NULL and
  64540. * dbg_detaching != 0. The message loop will finish the detach
  64541. * by running detach2 phase, so enter the message loop also when
  64542. * detaching.
  64543. */
  64544. if (process_messages) {
  64545. DUK_ASSERT(thr->heap->dbg_processing == 0);
  64546. processed_messages = duk_debug_process_messages(thr, 0 /*no_block*/);
  64547. DUK_ASSERT(thr->heap->dbg_processing == 0);
  64548. }
  64549. /* Continue checked execution if there are breakpoints or we're stepping.
  64550. * Also use checked execution if paused flag is active - it shouldn't be
  64551. * because the debug message loop shouldn't terminate if it was. Step out
  64552. * is handled by callstack unwind and doesn't need checked execution.
  64553. * Note that debugger may have detached due to error or explicit request
  64554. * above, so we must recheck attach status.
  64555. */
  64556. if (duk_debug_is_attached(thr->heap)) {
  64557. DUK_ASSERT(act == thr->callstack_curr);
  64558. DUK_ASSERT(act != NULL);
  64559. if (act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE ||
  64560. (thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_ONE_OPCODE) ||
  64561. ((thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_LINE_CHANGE) &&
  64562. thr->heap->dbg_pause_act == thr->callstack_curr) ||
  64563. DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap)) {
  64564. *out_immediate = 1;
  64565. }
  64566. /* If we processed any debug messages breakpoints may have
  64567. * changed; restart execution to re-check active breakpoints.
  64568. */
  64569. if (processed_messages) {
  64570. DUK_D(DUK_DPRINT("processed debug messages, restart execution to recheck possibly changed breakpoints"));
  64571. *out_interrupt_retval = DUK__INT_RESTART;
  64572. } else {
  64573. if (thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_ONE_OPCODE) {
  64574. /* Set 'pause after one opcode' active only when we're
  64575. * actually just about to execute code.
  64576. */
  64577. thr->heap->dbg_pause_flags |= DUK_PAUSE_FLAG_ONE_OPCODE_ACTIVE;
  64578. }
  64579. }
  64580. } else {
  64581. DUK_D(DUK_DPRINT("debugger became detached, resume normal execution"));
  64582. }
  64583. }
  64584. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  64585. DUK_LOCAL DUK__NOINLINE_PERF DUK_COLD duk_small_uint_t duk__executor_interrupt(duk_hthread *thr) {
  64586. duk_int_t ctr;
  64587. duk_activation *act;
  64588. duk_hcompfunc *fun;
  64589. duk_bool_t immediate = 0;
  64590. duk_small_uint_t retval;
  64591. DUK_ASSERT(thr != NULL);
  64592. DUK_ASSERT(thr->heap != NULL);
  64593. DUK_ASSERT(thr->callstack_top > 0);
  64594. #if defined(DUK_USE_DEBUG)
  64595. thr->heap->inst_count_interrupt += thr->interrupt_init;
  64596. DUK_DD(DUK_DDPRINT("execution interrupt, counter=%ld, init=%ld, "
  64597. "instruction counts: executor=%ld, interrupt=%ld",
  64598. (long) thr->interrupt_counter, (long) thr->interrupt_init,
  64599. (long) thr->heap->inst_count_exec, (long) thr->heap->inst_count_interrupt));
  64600. #endif
  64601. retval = DUK__INT_NOACTION;
  64602. ctr = DUK_HTHREAD_INTCTR_DEFAULT;
  64603. /*
  64604. * Avoid nested calls. Concretely this happens during debugging, e.g.
  64605. * when we eval() an expression.
  64606. *
  64607. * Also don't interrupt if we're currently doing debug processing
  64608. * (which can be initiated outside the bytecode executor) as this
  64609. * may cause the debugger to be called recursively. Check required
  64610. * for correct operation of throw intercept and other "exotic" halting
  64611. * scenarios.
  64612. */
  64613. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  64614. if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap) || thr->heap->dbg_processing) {
  64615. #else
  64616. if (DUK_HEAP_HAS_INTERRUPT_RUNNING(thr->heap)) {
  64617. #endif
  64618. DUK_DD(DUK_DDPRINT("nested executor interrupt, ignoring"));
  64619. /* Set a high interrupt counter; the original executor
  64620. * interrupt invocation will rewrite before exiting.
  64621. */
  64622. thr->interrupt_init = ctr;
  64623. thr->interrupt_counter = ctr - 1;
  64624. return DUK__INT_NOACTION;
  64625. }
  64626. DUK_HEAP_SET_INTERRUPT_RUNNING(thr->heap);
  64627. act = thr->callstack_curr;
  64628. DUK_ASSERT(act != NULL);
  64629. fun = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  64630. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC((duk_hobject *) fun));
  64631. DUK_UNREF(fun);
  64632. #if defined(DUK_USE_EXEC_TIMEOUT_CHECK)
  64633. /*
  64634. * Execution timeout check
  64635. */
  64636. if (DUK_USE_EXEC_TIMEOUT_CHECK(thr->heap->heap_udata)) {
  64637. /* Keep throwing an error whenever we get here. The unusual values
  64638. * are set this way because no instruction is ever executed, we just
  64639. * throw an error until all try/catch/finally and other catchpoints
  64640. * have been exhausted. Duktape/C code gets control at each protected
  64641. * call but whenever it enters back into Duktape the RangeError gets
  64642. * raised. User exec timeout check must consistently indicate a timeout
  64643. * until we've fully bubbled out of Duktape.
  64644. */
  64645. DUK_D(DUK_DPRINT("execution timeout, throwing a RangeError"));
  64646. thr->interrupt_init = 0;
  64647. thr->interrupt_counter = 0;
  64648. DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap);
  64649. DUK_ERROR_RANGE(thr, "execution timeout");
  64650. DUK_WO_NORETURN(return 0;);
  64651. }
  64652. #endif /* DUK_USE_EXEC_TIMEOUT_CHECK */
  64653. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  64654. if (!thr->heap->dbg_processing &&
  64655. (thr->heap->dbg_read_cb != NULL || thr->heap->dbg_detaching)) {
  64656. /* Avoid recursive re-entry; enter when we're attached or
  64657. * detaching (to finish off the pending detach).
  64658. */
  64659. duk__interrupt_handle_debugger(thr, &immediate, &retval);
  64660. DUK_ASSERT(act == thr->callstack_curr);
  64661. }
  64662. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  64663. /*
  64664. * Update the interrupt counter
  64665. */
  64666. if (immediate) {
  64667. /* Cause an interrupt after executing one instruction. */
  64668. ctr = 1;
  64669. }
  64670. /* The counter value is one less than the init value: init value should
  64671. * indicate how many instructions are executed before interrupt. To
  64672. * execute 1 instruction (after interrupt handler return), counter must
  64673. * be 0.
  64674. */
  64675. DUK_ASSERT(ctr >= 1);
  64676. thr->interrupt_init = ctr;
  64677. thr->interrupt_counter = ctr - 1;
  64678. DUK_HEAP_CLEAR_INTERRUPT_RUNNING(thr->heap);
  64679. return retval;
  64680. }
  64681. #endif /* DUK_USE_INTERRUPT_COUNTER */
  64682. /*
  64683. * Debugger handling for executor restart
  64684. *
  64685. * Check for breakpoints, stepping, etc, and figure out if we should execute
  64686. * in checked or normal mode. Note that we can't do this when an activation
  64687. * is created, because breakpoint status (and stepping status) may change
  64688. * later, so we must recheck every time we're executing an activation.
  64689. * This primitive should be side effect free to avoid changes during check.
  64690. */
  64691. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  64692. DUK_LOCAL void duk__executor_recheck_debugger(duk_hthread *thr, duk_activation *act, duk_hcompfunc *fun) {
  64693. duk_heap *heap;
  64694. duk_tval *tv_tmp;
  64695. duk_hstring *filename;
  64696. duk_small_uint_t bp_idx;
  64697. duk_breakpoint **bp_active;
  64698. DUK_ASSERT(thr != NULL);
  64699. DUK_ASSERT(act != NULL);
  64700. DUK_ASSERT(fun != NULL);
  64701. heap = thr->heap;
  64702. bp_active = heap->dbg_breakpoints_active;
  64703. act->flags &= ~DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
  64704. tv_tmp = duk_hobject_find_existing_entry_tval_ptr(thr->heap, (duk_hobject *) fun, DUK_HTHREAD_STRING_FILE_NAME(thr));
  64705. if (tv_tmp && DUK_TVAL_IS_STRING(tv_tmp)) {
  64706. filename = DUK_TVAL_GET_STRING(tv_tmp);
  64707. /* Figure out all active breakpoints. A breakpoint is
  64708. * considered active if the current function's fileName
  64709. * matches the breakpoint's fileName, AND there is no
  64710. * inner function that has matching line numbers
  64711. * (otherwise a breakpoint would be triggered both
  64712. * inside and outside of the inner function which would
  64713. * be confusing). Example:
  64714. *
  64715. * function foo() {
  64716. * print('foo');
  64717. * function bar() { <-. breakpoints in these
  64718. * print('bar'); | lines should not affect
  64719. * } <-' foo() execution
  64720. * bar();
  64721. * }
  64722. *
  64723. * We need a few things that are only available when
  64724. * debugger support is enabled: (1) a line range for
  64725. * each function, and (2) access to the function
  64726. * template to access the inner functions (and their
  64727. * line ranges).
  64728. *
  64729. * It's important to have a narrow match for active
  64730. * breakpoints so that we don't enter checked execution
  64731. * when that's not necessary. For instance, if we're
  64732. * running inside a certain function and there's
  64733. * breakpoint outside in (after the call site), we
  64734. * don't want to slow down execution of the function.
  64735. */
  64736. for (bp_idx = 0; bp_idx < heap->dbg_breakpoint_count; bp_idx++) {
  64737. duk_breakpoint *bp = heap->dbg_breakpoints + bp_idx;
  64738. duk_hobject **funcs, **funcs_end;
  64739. duk_hcompfunc *inner_fun;
  64740. duk_bool_t bp_match;
  64741. if (bp->filename == filename &&
  64742. bp->line >= fun->start_line && bp->line <= fun->end_line) {
  64743. bp_match = 1;
  64744. DUK_DD(DUK_DDPRINT("breakpoint filename and line match: "
  64745. "%s:%ld vs. %s (line %ld vs. %ld-%ld)",
  64746. DUK_HSTRING_GET_DATA(bp->filename),
  64747. (long) bp->line,
  64748. DUK_HSTRING_GET_DATA(filename),
  64749. (long) bp->line,
  64750. (long) fun->start_line,
  64751. (long) fun->end_line));
  64752. funcs = DUK_HCOMPFUNC_GET_FUNCS_BASE(thr->heap, fun);
  64753. funcs_end = DUK_HCOMPFUNC_GET_FUNCS_END(thr->heap, fun);
  64754. while (funcs != funcs_end) {
  64755. inner_fun = (duk_hcompfunc *) *funcs;
  64756. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) inner_fun));
  64757. if (bp->line >= inner_fun->start_line && bp->line <= inner_fun->end_line) {
  64758. DUK_DD(DUK_DDPRINT("inner function masks ('captures') breakpoint"));
  64759. bp_match = 0;
  64760. break;
  64761. }
  64762. funcs++;
  64763. }
  64764. if (bp_match) {
  64765. /* No need to check for size of bp_active list,
  64766. * it's always larger than maximum number of
  64767. * breakpoints.
  64768. */
  64769. act->flags |= DUK_ACT_FLAG_BREAKPOINT_ACTIVE;
  64770. *bp_active = heap->dbg_breakpoints + bp_idx;
  64771. bp_active++;
  64772. }
  64773. }
  64774. }
  64775. }
  64776. *bp_active = NULL; /* terminate */
  64777. DUK_DD(DUK_DDPRINT("ACTIVE BREAKPOINTS: %ld", (long) (bp_active - thr->heap->dbg_breakpoints_active)));
  64778. /* Force pause if we were doing "step into" in another activation. */
  64779. if ((thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_FUNC_ENTRY) &&
  64780. thr->heap->dbg_pause_act != thr->callstack_curr) {
  64781. DUK_D(DUK_DPRINT("PAUSE TRIGGERED by function entry"));
  64782. duk_debug_set_paused(thr->heap);
  64783. }
  64784. /* Force interrupt right away if we're paused or in "checked mode".
  64785. * Step out is handled by callstack unwind.
  64786. */
  64787. if ((act->flags & DUK_ACT_FLAG_BREAKPOINT_ACTIVE) ||
  64788. DUK_HEAP_HAS_DEBUGGER_PAUSED(thr->heap) ||
  64789. ((thr->heap->dbg_pause_flags & DUK_PAUSE_FLAG_LINE_CHANGE) &&
  64790. thr->heap->dbg_pause_act == thr->callstack_curr)) {
  64791. /* We'll need to interrupt early so recompute the init
  64792. * counter to reflect the number of bytecode instructions
  64793. * executed so that step counts for e.g. debugger rate
  64794. * limiting are accurate.
  64795. */
  64796. DUK_ASSERT(thr->interrupt_counter <= thr->interrupt_init);
  64797. thr->interrupt_init = thr->interrupt_init - thr->interrupt_counter;
  64798. thr->interrupt_counter = 0;
  64799. }
  64800. }
  64801. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  64802. /*
  64803. * Opcode handlers for opcodes with a lot of code and which are relatively
  64804. * rare; NOINLINE to reduce amount of code in main bytecode dispatcher.
  64805. */
  64806. DUK_LOCAL DUK__NOINLINE_PERF void duk__handle_op_initset_initget(duk_hthread *thr, duk_uint_fast32_t ins) {
  64807. duk_bool_t is_set = (DUK_DEC_OP(ins) == DUK_OP_INITSET);
  64808. duk_uint_fast_t idx;
  64809. duk_uint_t defprop_flags;
  64810. /* A -> object register (acts as a source)
  64811. * BC -> BC+0 contains key, BC+1 closure (value)
  64812. */
  64813. /* INITSET/INITGET are only used to initialize object literal keys.
  64814. * There may be a previous propery in ES2015 because duplicate property
  64815. * names are allowed.
  64816. */
  64817. /* This could be made more optimal by accessing internals directly. */
  64818. idx = (duk_uint_fast_t) DUK_DEC_BC(ins);
  64819. duk_dup(thr, (duk_idx_t) (idx + 0)); /* key */
  64820. duk_dup(thr, (duk_idx_t) (idx + 1)); /* getter/setter */
  64821. if (is_set) {
  64822. defprop_flags = DUK_DEFPROP_HAVE_SETTER |
  64823. DUK_DEFPROP_FORCE |
  64824. DUK_DEFPROP_SET_ENUMERABLE |
  64825. DUK_DEFPROP_SET_CONFIGURABLE;
  64826. } else {
  64827. defprop_flags = DUK_DEFPROP_HAVE_GETTER |
  64828. DUK_DEFPROP_FORCE |
  64829. DUK_DEFPROP_SET_ENUMERABLE |
  64830. DUK_DEFPROP_SET_CONFIGURABLE;
  64831. }
  64832. duk_def_prop(thr, (duk_idx_t) DUK_DEC_A(ins), defprop_flags);
  64833. }
  64834. DUK_LOCAL DUK__NOINLINE_PERF void duk__handle_op_trycatch(duk_hthread *thr, duk_uint_fast32_t ins, duk_instr_t *curr_pc) {
  64835. duk_activation *act;
  64836. duk_catcher *cat;
  64837. duk_tval *tv1;
  64838. duk_small_uint_fast_t a;
  64839. duk_small_uint_fast_t bc;
  64840. /* A -> flags
  64841. * BC -> reg_catch; base register for two registers used both during
  64842. * trycatch setup and when catch is triggered
  64843. *
  64844. * If DUK_BC_TRYCATCH_FLAG_CATCH_BINDING set:
  64845. * reg_catch + 0: catch binding variable name (string).
  64846. * Automatic declarative environment is established for
  64847. * the duration of the 'catch' clause.
  64848. *
  64849. * If DUK_BC_TRYCATCH_FLAG_WITH_BINDING set:
  64850. * reg_catch + 0: with 'target value', which is coerced to
  64851. * an object and then used as a bindind object for an
  64852. * environment record. The binding is initialized here, for
  64853. * the 'try' clause.
  64854. *
  64855. * Note that a TRYCATCH generated for a 'with' statement has no
  64856. * catch or finally parts.
  64857. */
  64858. /* XXX: TRYCATCH handling should be reworked to avoid creating
  64859. * an explicit scope unless it is actually needed (e.g. function
  64860. * instances or eval is executed inside the catch block). This
  64861. * rework is not trivial because the compiler doesn't have an
  64862. * intermediate representation. When the rework is done, the
  64863. * opcode format can also be made more straightforward.
  64864. */
  64865. /* XXX: side effect handling is quite awkward here */
  64866. DUK_DDD(DUK_DDDPRINT("TRYCATCH: reg_catch=%ld, have_catch=%ld, "
  64867. "have_finally=%ld, catch_binding=%ld, with_binding=%ld (flags=0x%02lx)",
  64868. (long) DUK_DEC_BC(ins),
  64869. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH ? 1 : 0),
  64870. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY ? 1 : 0),
  64871. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING ? 1 : 0),
  64872. (long) (DUK_DEC_A(ins) & DUK_BC_TRYCATCH_FLAG_WITH_BINDING ? 1 : 0),
  64873. (unsigned long) DUK_DEC_A(ins)));
  64874. a = DUK_DEC_A(ins);
  64875. bc = DUK_DEC_BC(ins);
  64876. /* Registers 'bc' and 'bc + 1' are written in longjmp handling
  64877. * and if their previous values (which are temporaries) become
  64878. * unreachable -and- have a finalizer, there'll be a function
  64879. * call during error handling which is not supported now (GH-287).
  64880. * Ensure that both 'bc' and 'bc + 1' have primitive values to
  64881. * guarantee no finalizer calls in error handling. Scrubbing also
  64882. * ensures finalizers for the previous values run here rather than
  64883. * later. Error handling related values are also written to 'bc'
  64884. * and 'bc + 1' but those values never become unreachable during
  64885. * error handling, so there's no side effect problem even if the
  64886. * error value has a finalizer.
  64887. */
  64888. duk_dup(thr, (duk_idx_t) bc); /* Stabilize value. */
  64889. duk_to_undefined(thr, (duk_idx_t) bc);
  64890. duk_to_undefined(thr, (duk_idx_t) (bc + 1));
  64891. /* Allocate catcher and populate it. Doesn't have to
  64892. * be fully atomic, but the catcher must be in a
  64893. * consistent state if side effects (such as finalizer
  64894. * calls) occur.
  64895. */
  64896. cat = duk_hthread_catcher_alloc(thr);
  64897. DUK_ASSERT(cat != NULL);
  64898. cat->flags = DUK_CAT_TYPE_TCF;
  64899. cat->h_varname = NULL;
  64900. cat->pc_base = (duk_instr_t *) curr_pc; /* pre-incremented, points to first jump slot */
  64901. cat->idx_base = (duk_size_t) (thr->valstack_bottom - thr->valstack) + bc;
  64902. act = thr->callstack_curr;
  64903. DUK_ASSERT(act != NULL);
  64904. cat->parent = act->cat;
  64905. act->cat = cat;
  64906. if (a & DUK_BC_TRYCATCH_FLAG_HAVE_CATCH) {
  64907. cat->flags |= DUK_CAT_FLAG_CATCH_ENABLED;
  64908. }
  64909. if (a & DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY) {
  64910. cat->flags |= DUK_CAT_FLAG_FINALLY_ENABLED;
  64911. }
  64912. if (a & DUK_BC_TRYCATCH_FLAG_CATCH_BINDING) {
  64913. DUK_DDD(DUK_DDDPRINT("catch binding flag set to catcher"));
  64914. cat->flags |= DUK_CAT_FLAG_CATCH_BINDING_ENABLED;
  64915. tv1 = DUK_GET_TVAL_NEGIDX(thr, -1);
  64916. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  64917. /* borrowed reference; although 'tv1' comes from a register,
  64918. * its value was loaded using LDCONST so the constant will
  64919. * also exist and be reachable.
  64920. */
  64921. cat->h_varname = DUK_TVAL_GET_STRING(tv1);
  64922. } else if (a & DUK_BC_TRYCATCH_FLAG_WITH_BINDING) {
  64923. duk_hobjenv *env;
  64924. duk_hobject *target;
  64925. /* Delayed env initialization for activation (if needed). */
  64926. DUK_ASSERT(thr->callstack_top >= 1);
  64927. DUK_ASSERT(act == thr->callstack_curr);
  64928. DUK_ASSERT(act != NULL);
  64929. if (act->lex_env == NULL) {
  64930. DUK_DDD(DUK_DDDPRINT("delayed environment initialization"));
  64931. DUK_ASSERT(act->var_env == NULL);
  64932. duk_js_init_activation_environment_records_delayed(thr, act);
  64933. DUK_ASSERT(act == thr->callstack_curr);
  64934. DUK_UNREF(act); /* 'act' is no longer accessed, scanbuild fix */
  64935. }
  64936. DUK_ASSERT(act->lex_env != NULL);
  64937. DUK_ASSERT(act->var_env != NULL);
  64938. /* Coerce 'with' target. */
  64939. target = duk_to_hobject(thr, -1);
  64940. DUK_ASSERT(target != NULL);
  64941. /* Create an object environment; it is not pushed
  64942. * so avoid side effects very carefully until it is
  64943. * referenced.
  64944. */
  64945. env = duk_hobjenv_alloc(thr,
  64946. DUK_HOBJECT_FLAG_EXTENSIBLE |
  64947. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_OBJENV));
  64948. DUK_ASSERT(env != NULL);
  64949. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) env) == NULL);
  64950. env->target = target; /* always provideThis=true */
  64951. DUK_HOBJECT_INCREF(thr, target);
  64952. env->has_this = 1;
  64953. DUK_ASSERT_HOBJENV_VALID(env);
  64954. DUK_DDD(DUK_DDDPRINT("environment for with binding: %!iO", env));
  64955. DUK_ASSERT(act == thr->callstack_curr);
  64956. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) env) == NULL);
  64957. DUK_ASSERT(act->lex_env != NULL);
  64958. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, (duk_hobject *) env, act->lex_env);
  64959. act->lex_env = (duk_hobject *) env; /* Now reachable. */
  64960. DUK_HOBJECT_INCREF(thr, (duk_hobject *) env);
  64961. /* Net refcount change to act->lex_env is 0: incref for env's
  64962. * prototype, decref for act->lex_env overwrite.
  64963. */
  64964. /* Set catcher lex_env active (affects unwind)
  64965. * only when the whole setup is complete.
  64966. */
  64967. cat = act->cat; /* XXX: better to relookup? not mandatory because 'cat' is stable */
  64968. cat->flags |= DUK_CAT_FLAG_LEXENV_ACTIVE;
  64969. } else {
  64970. ;
  64971. }
  64972. DUK_DDD(DUK_DDDPRINT("TRYCATCH catcher: flags=0x%08lx, pc_base=%ld, "
  64973. "idx_base=%ld, h_varname=%!O",
  64974. (unsigned long) cat->flags,
  64975. (long) cat->pc_base, (long) cat->idx_base, (duk_heaphdr *) cat->h_varname));
  64976. duk_pop_unsafe(thr);
  64977. }
  64978. DUK_LOCAL DUK__NOINLINE_PERF duk_instr_t *duk__handle_op_endtry(duk_hthread *thr, duk_uint_fast32_t ins) {
  64979. duk_activation *act;
  64980. duk_catcher *cat;
  64981. duk_tval *tv1;
  64982. duk_instr_t *pc_base;
  64983. DUK_UNREF(ins);
  64984. DUK_ASSERT(thr->callstack_top >= 1);
  64985. act = thr->callstack_curr;
  64986. DUK_ASSERT(act != NULL);
  64987. cat = act->cat;
  64988. DUK_ASSERT(cat != NULL);
  64989. DUK_ASSERT(DUK_CAT_GET_TYPE(act->cat) == DUK_CAT_TYPE_TCF);
  64990. DUK_DDD(DUK_DDDPRINT("ENDTRY: clearing catch active flag (regardless of whether it was set or not)"));
  64991. DUK_CAT_CLEAR_CATCH_ENABLED(cat);
  64992. pc_base = cat->pc_base;
  64993. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  64994. DUK_DDD(DUK_DDDPRINT("ENDTRY: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
  64995. tv1 = thr->valstack + cat->idx_base;
  64996. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  64997. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  64998. tv1 = NULL;
  64999. tv1 = thr->valstack + cat->idx_base + 1;
  65000. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  65001. DUK_TVAL_SET_U32_UPDREF(thr, tv1, (duk_uint32_t) DUK_LJ_TYPE_NORMAL); /* side effects */
  65002. tv1 = NULL;
  65003. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  65004. } else {
  65005. DUK_DDD(DUK_DDDPRINT("ENDTRY: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
  65006. duk_hthread_catcher_unwind_norz(thr, act); /* lexenv may be set for 'with' binding */
  65007. /* no need to unwind callstack */
  65008. }
  65009. return pc_base + 1; /* new curr_pc value */
  65010. }
  65011. DUK_LOCAL DUK__NOINLINE_PERF duk_instr_t *duk__handle_op_endcatch(duk_hthread *thr, duk_uint_fast32_t ins) {
  65012. duk_activation *act;
  65013. duk_catcher *cat;
  65014. duk_tval *tv1;
  65015. duk_instr_t *pc_base;
  65016. DUK_UNREF(ins);
  65017. DUK_ASSERT(thr->callstack_top >= 1);
  65018. act = thr->callstack_curr;
  65019. DUK_ASSERT(act != NULL);
  65020. cat = act->cat;
  65021. DUK_ASSERT(cat != NULL);
  65022. DUK_ASSERT(!DUK_CAT_HAS_CATCH_ENABLED(cat)); /* cleared before entering catch part */
  65023. if (DUK_CAT_HAS_LEXENV_ACTIVE(cat)) {
  65024. duk_hobject *prev_env;
  65025. /* 'with' binding has no catch clause, so can't be here unless a normal try-catch */
  65026. DUK_ASSERT(DUK_CAT_HAS_CATCH_BINDING_ENABLED(cat));
  65027. DUK_ASSERT(act->lex_env != NULL);
  65028. DUK_DDD(DUK_DDDPRINT("ENDCATCH: popping catcher part lexical environment"));
  65029. prev_env = act->lex_env;
  65030. DUK_ASSERT(prev_env != NULL);
  65031. act->lex_env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, prev_env);
  65032. DUK_CAT_CLEAR_LEXENV_ACTIVE(cat);
  65033. DUK_HOBJECT_INCREF(thr, act->lex_env);
  65034. DUK_HOBJECT_DECREF(thr, prev_env); /* side effects */
  65035. DUK_ASSERT(act == thr->callstack_curr);
  65036. DUK_ASSERT(act != NULL);
  65037. }
  65038. pc_base = cat->pc_base;
  65039. if (DUK_CAT_HAS_FINALLY_ENABLED(cat)) {
  65040. DUK_DDD(DUK_DDDPRINT("ENDCATCH: finally part is active, jump through 2nd jump slot with 'normal continuation'"));
  65041. tv1 = thr->valstack + cat->idx_base;
  65042. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  65043. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  65044. tv1 = NULL;
  65045. tv1 = thr->valstack + cat->idx_base + 1;
  65046. DUK_ASSERT(tv1 >= thr->valstack && tv1 < thr->valstack_top);
  65047. DUK_TVAL_SET_U32_UPDREF(thr, tv1, (duk_uint32_t) DUK_LJ_TYPE_NORMAL); /* side effects */
  65048. tv1 = NULL;
  65049. DUK_CAT_CLEAR_FINALLY_ENABLED(cat);
  65050. } else {
  65051. DUK_DDD(DUK_DDDPRINT("ENDCATCH: no finally part, dismantle catcher, jump through 2nd jump slot (to end of statement)"));
  65052. duk_hthread_catcher_unwind_norz(thr, act);
  65053. /* no need to unwind callstack */
  65054. }
  65055. return pc_base + 1; /* new curr_pc value */
  65056. }
  65057. DUK_LOCAL DUK__NOINLINE_PERF duk_small_uint_t duk__handle_op_endfin(duk_hthread *thr, duk_uint_fast32_t ins, duk_activation *entry_act) {
  65058. duk_activation *act;
  65059. duk_tval *tv1;
  65060. duk_uint_t reg_catch;
  65061. duk_small_uint_t cont_type;
  65062. duk_small_uint_t ret_result;
  65063. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  65064. DUK_ASSERT(thr->callstack_top >= 1);
  65065. act = thr->callstack_curr;
  65066. DUK_ASSERT(act != NULL);
  65067. reg_catch = DUK_DEC_ABC(ins);
  65068. /* CATCH flag may be enabled or disabled here; it may be enabled if
  65069. * the statement has a catch block but the try block does not throw
  65070. * an error.
  65071. */
  65072. DUK_DDD(DUK_DDDPRINT("ENDFIN: completion value=%!T, type=%!T",
  65073. (duk_tval *) (thr->valstack_bottom + reg_catch + 0),
  65074. (duk_tval *) (thr->valstack_bottom + reg_catch + 1)));
  65075. tv1 = thr->valstack_bottom + reg_catch + 1; /* type */
  65076. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  65077. #if defined(DUK_USE_FASTINT)
  65078. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1));
  65079. cont_type = (duk_small_uint_t) DUK_TVAL_GET_FASTINT_U32(tv1);
  65080. #else
  65081. cont_type = (duk_small_uint_t) DUK_TVAL_GET_NUMBER(tv1);
  65082. #endif
  65083. tv1--; /* value */
  65084. switch (cont_type) {
  65085. case DUK_LJ_TYPE_NORMAL: {
  65086. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'normal' (non-abrupt) completion -> "
  65087. "dismantle catcher, resume execution after ENDFIN"));
  65088. duk_hthread_catcher_unwind_norz(thr, act);
  65089. /* no need to unwind callstack */
  65090. return 0; /* restart execution */
  65091. }
  65092. case DUK_LJ_TYPE_RETURN: {
  65093. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with 'return' complation -> dismantle "
  65094. "catcher, handle return, lj.value1=%!T", tv1));
  65095. /* Not necessary to unwind catch stack: return handling will
  65096. * do it. The finally flag of 'cat' is no longer set. The
  65097. * catch flag may be set, but it's not checked by return handling.
  65098. */
  65099. duk_push_tval(thr, tv1);
  65100. ret_result = duk__handle_return(thr, entry_act);
  65101. if (ret_result == DUK__RETHAND_RESTART) {
  65102. return 0; /* restart execution */
  65103. }
  65104. DUK_ASSERT(ret_result == DUK__RETHAND_FINISHED);
  65105. DUK_DDD(DUK_DDDPRINT("exiting executor after ENDFIN and RETURN (pseudo) longjmp type"));
  65106. return 1; /* exit executor */
  65107. }
  65108. case DUK_LJ_TYPE_BREAK:
  65109. case DUK_LJ_TYPE_CONTINUE: {
  65110. duk_uint_t label_id;
  65111. duk_small_uint_t lj_type;
  65112. /* Not necessary to unwind catch stack: break/continue
  65113. * handling will do it. The finally flag of 'cat' is
  65114. * no longer set. The catch flag may be set, but it's
  65115. * not checked by break/continue handling.
  65116. */
  65117. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  65118. #if defined(DUK_USE_FASTINT)
  65119. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1));
  65120. label_id = (duk_small_uint_t) DUK_TVAL_GET_FASTINT_U32(tv1);
  65121. #else
  65122. label_id = (duk_small_uint_t) DUK_TVAL_GET_NUMBER(tv1);
  65123. #endif
  65124. lj_type = cont_type;
  65125. duk__handle_break_or_continue(thr, label_id, lj_type);
  65126. return 0; /* restart execution */
  65127. }
  65128. default: {
  65129. DUK_DDD(DUK_DDDPRINT("ENDFIN: finally part finishing with abrupt completion, lj_type=%ld -> "
  65130. "dismantle catcher, re-throw error",
  65131. (long) cont_type));
  65132. duk_err_setup_ljstate1(thr, (duk_small_uint_t) cont_type, tv1);
  65133. /* No debugger Throw notify check on purpose (rethrow). */
  65134. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  65135. duk_err_longjmp(thr);
  65136. DUK_UNREACHABLE();
  65137. }
  65138. }
  65139. DUK_UNREACHABLE();
  65140. return 0;
  65141. }
  65142. DUK_LOCAL DUK__NOINLINE_PERF void duk__handle_op_initenum(duk_hthread *thr, duk_uint_fast32_t ins) {
  65143. duk_small_uint_t b;
  65144. duk_small_uint_t c;
  65145. /*
  65146. * Enumeration semantics come from for-in statement, E5 Section 12.6.4.
  65147. * If called with 'null' or 'undefined', this opcode returns 'null' as
  65148. * the enumerator, which is special cased in NEXTENUM. This simplifies
  65149. * the compiler part
  65150. */
  65151. /* B -> register for writing enumerator object
  65152. * C -> value to be enumerated (register)
  65153. */
  65154. b = DUK_DEC_B(ins);
  65155. c = DUK_DEC_C(ins);
  65156. if (duk_is_null_or_undefined(thr, (duk_idx_t) c)) {
  65157. duk_push_null(thr);
  65158. duk_replace(thr, (duk_idx_t) b);
  65159. } else {
  65160. duk_dup(thr, (duk_idx_t) c);
  65161. duk_to_object(thr, -1);
  65162. duk_hobject_enumerator_create(thr, 0 /*enum_flags*/); /* [ ... val ] --> [ ... enum ] */
  65163. duk_replace(thr, (duk_idx_t) b);
  65164. }
  65165. }
  65166. DUK_LOCAL DUK__NOINLINE_PERF duk_small_uint_t duk__handle_op_nextenum(duk_hthread *thr, duk_uint_fast32_t ins) {
  65167. duk_small_uint_t b;
  65168. duk_small_uint_t c;
  65169. duk_small_uint_t pc_skip = 0;
  65170. /*
  65171. * NEXTENUM checks whether the enumerator still has unenumerated
  65172. * keys. If so, the next key is loaded to the target register
  65173. * and the next instruction is skipped. Otherwise the next instruction
  65174. * will be executed, jumping out of the enumeration loop.
  65175. */
  65176. /* B -> target register for next key
  65177. * C -> enum register
  65178. */
  65179. b = DUK_DEC_B(ins);
  65180. c = DUK_DEC_C(ins);
  65181. DUK_DDD(DUK_DDDPRINT("NEXTENUM: b->%!T, c->%!T",
  65182. (duk_tval *) duk_get_tval(thr, (duk_idx_t) b),
  65183. (duk_tval *) duk_get_tval(thr, (duk_idx_t) c)));
  65184. if (duk_is_object(thr, (duk_idx_t) c)) {
  65185. /* XXX: assert 'c' is an enumerator */
  65186. duk_dup(thr, (duk_idx_t) c);
  65187. if (duk_hobject_enumerator_next(thr, 0 /*get_value*/)) {
  65188. /* [ ... enum ] -> [ ... next_key ] */
  65189. DUK_DDD(DUK_DDDPRINT("enum active, next key is %!T, skip jump slot ",
  65190. (duk_tval *) duk_get_tval(thr, -1)));
  65191. pc_skip = 1;
  65192. } else {
  65193. /* [ ... enum ] -> [ ... ] */
  65194. DUK_DDD(DUK_DDDPRINT("enum finished, execute jump slot"));
  65195. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* valstack policy */
  65196. thr->valstack_top++;
  65197. }
  65198. duk_replace(thr, (duk_idx_t) b);
  65199. } else {
  65200. /* 'null' enumerator case -> behave as with an empty enumerator */
  65201. DUK_ASSERT(duk_is_null(thr, (duk_idx_t) c));
  65202. DUK_DDD(DUK_DDDPRINT("enum is null, execute jump slot"));
  65203. }
  65204. return pc_skip;
  65205. }
  65206. /*
  65207. * Call handling helpers.
  65208. */
  65209. DUK_LOCAL duk_bool_t duk__executor_handle_call(duk_hthread *thr, duk_idx_t idx, duk_idx_t nargs, duk_small_uint_t call_flags) {
  65210. duk_bool_t rc;
  65211. duk_set_top_unsafe(thr, (duk_idx_t) (idx + nargs + 2)); /* [ ... func this arg1 ... argN ] */
  65212. /* Attempt an Ecma-to-Ecma call setup. If the call
  65213. * target is (directly or indirectly) Reflect.construct(),
  65214. * the call may change into a constructor call on the fly.
  65215. */
  65216. rc = (duk_bool_t) duk_handle_call_unprotected(thr, idx, call_flags);
  65217. if (rc != 0) {
  65218. /* Ecma-to-ecma call possible, may or may not
  65219. * be a tail call. Avoid C recursion by
  65220. * reusing current executor instance.
  65221. */
  65222. DUK_DDD(DUK_DDDPRINT("ecma-to-ecma call setup possible, restart execution"));
  65223. /* curr_pc synced by duk_handle_call_unprotected() */
  65224. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  65225. return rc;
  65226. } else {
  65227. /* Call was handled inline. */
  65228. }
  65229. DUK_ASSERT(thr->ptr_curr_pc != NULL);
  65230. return rc;
  65231. }
  65232. /*
  65233. * ECMAScript bytecode executor.
  65234. *
  65235. * Resume execution for the current thread from its current activation.
  65236. * Returns when execution would return from the entry level activation,
  65237. * leaving a single return value on top of the stack. Function calls
  65238. * and thread resumptions are handled internally. If an error occurs,
  65239. * a longjmp() with type DUK_LJ_TYPE_THROW is called on the entry level
  65240. * setjmp() jmpbuf.
  65241. *
  65242. * ECMAScript function calls and coroutine resumptions are handled
  65243. * internally (by the outer executor function) without recursive C calls.
  65244. * Other function calls are handled using duk_handle_call(), increasing
  65245. * C recursion depth.
  65246. *
  65247. * Abrupt completions (= long control tranfers) are handled either
  65248. * directly by reconfiguring relevant stacks and restarting execution,
  65249. * or via a longjmp. Longjmp-free handling is preferable for performance
  65250. * (especially Emscripten performance), and is used for: break, continue,
  65251. * and return.
  65252. *
  65253. * For more detailed notes, see doc/execution.rst.
  65254. *
  65255. * Also see doc/code-issues.rst for discussion of setjmp(), longjmp(),
  65256. * and volatile.
  65257. */
  65258. /* Presence of 'fun' is config based, there's a marginal performance
  65259. * difference and the best option is architecture dependent.
  65260. */
  65261. #if defined(DUK_USE_EXEC_FUN_LOCAL)
  65262. #define DUK__FUN() fun
  65263. #else
  65264. #define DUK__FUN() ((duk_hcompfunc *) DUK_ACT_GET_FUNC((thr)->callstack_curr))
  65265. #endif
  65266. /* Strict flag. */
  65267. #define DUK__STRICT() ((duk_small_uint_t) DUK_HOBJECT_HAS_STRICT((duk_hobject *) DUK__FUN()))
  65268. /* Reg/const access macros: these are very footprint and performance sensitive
  65269. * so modify with care. Arguments are sometimes evaluated multiple times which
  65270. * is not ideal.
  65271. */
  65272. #define DUK__REG(x) (*(thr->valstack_bottom + (x)))
  65273. #define DUK__REGP(x) (thr->valstack_bottom + (x))
  65274. #define DUK__CONST(x) (*(consts + (x)))
  65275. #define DUK__CONSTP(x) (consts + (x))
  65276. /* Reg/const access macros which take the 32-bit instruction and avoid an
  65277. * explicit field decoding step by using shifts and masks. These must be
  65278. * kept in sync with duk_js_bytecode.h. The shift/mask values are chosen
  65279. * so that 'ins' can be shifted and masked and used as a -byte- offset
  65280. * instead of a duk_tval offset which needs further shifting (which is an
  65281. * issue on some, but not all, CPUs).
  65282. */
  65283. #define DUK__RCBIT_B DUK_BC_REGCONST_B
  65284. #define DUK__RCBIT_C DUK_BC_REGCONST_C
  65285. #if defined(DUK_USE_EXEC_REGCONST_OPTIMIZE)
  65286. #if defined(DUK_USE_PACKED_TVAL)
  65287. #define DUK__TVAL_SHIFT 3 /* sizeof(duk_tval) == 8 */
  65288. #else
  65289. #define DUK__TVAL_SHIFT 4 /* sizeof(duk_tval) == 16; not always the case so also asserted for */
  65290. #endif
  65291. #define DUK__SHIFT_A (DUK_BC_SHIFT_A - DUK__TVAL_SHIFT)
  65292. #define DUK__SHIFT_B (DUK_BC_SHIFT_B - DUK__TVAL_SHIFT)
  65293. #define DUK__SHIFT_C (DUK_BC_SHIFT_C - DUK__TVAL_SHIFT)
  65294. #define DUK__SHIFT_BC (DUK_BC_SHIFT_BC - DUK__TVAL_SHIFT)
  65295. #define DUK__MASK_A (DUK_BC_UNSHIFTED_MASK_A << DUK__TVAL_SHIFT)
  65296. #define DUK__MASK_B (DUK_BC_UNSHIFTED_MASK_B << DUK__TVAL_SHIFT)
  65297. #define DUK__MASK_C (DUK_BC_UNSHIFTED_MASK_C << DUK__TVAL_SHIFT)
  65298. #define DUK__MASK_BC (DUK_BC_UNSHIFTED_MASK_BC << DUK__TVAL_SHIFT)
  65299. #define DUK__BYTEOFF_A(ins) (((ins) >> DUK__SHIFT_A) & DUK__MASK_A)
  65300. #define DUK__BYTEOFF_B(ins) (((ins) >> DUK__SHIFT_B) & DUK__MASK_B)
  65301. #define DUK__BYTEOFF_C(ins) (((ins) >> DUK__SHIFT_C) & DUK__MASK_C)
  65302. #define DUK__BYTEOFF_BC(ins) (((ins) >> DUK__SHIFT_BC) & DUK__MASK_BC)
  65303. #define DUK__REGP_A(ins) ((duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_bottom + DUK__BYTEOFF_A((ins))))
  65304. #define DUK__REGP_B(ins) ((duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_bottom + DUK__BYTEOFF_B((ins))))
  65305. #define DUK__REGP_C(ins) ((duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_bottom + DUK__BYTEOFF_C((ins))))
  65306. #define DUK__REGP_BC(ins) ((duk_tval *) (void *) ((duk_uint8_t *) thr->valstack_bottom + DUK__BYTEOFF_BC((ins))))
  65307. #define DUK__CONSTP_A(ins) ((duk_tval *) (void *) ((duk_uint8_t *) consts + DUK__BYTEOFF_A((ins))))
  65308. #define DUK__CONSTP_B(ins) ((duk_tval *) (void *) ((duk_uint8_t *) consts + DUK__BYTEOFF_B((ins))))
  65309. #define DUK__CONSTP_C(ins) ((duk_tval *) (void *) ((duk_uint8_t *) consts + DUK__BYTEOFF_C((ins))))
  65310. #define DUK__CONSTP_BC(ins) ((duk_tval *) (void *) ((duk_uint8_t *) consts + DUK__BYTEOFF_BC((ins))))
  65311. #define DUK__REGCONSTP_B(ins) ((duk_tval *) (void *) ((duk_uint8_t *) (((ins) & DUK__RCBIT_B) ? consts : thr->valstack_bottom) + DUK__BYTEOFF_B((ins))))
  65312. #define DUK__REGCONSTP_C(ins) ((duk_tval *) (void *) ((duk_uint8_t *) (((ins) & DUK__RCBIT_C) ? consts : thr->valstack_bottom) + DUK__BYTEOFF_C((ins))))
  65313. #else /* DUK_USE_EXEC_REGCONST_OPTIMIZE */
  65314. /* Safe alternatives, no assumption about duk_tval size. */
  65315. #define DUK__REGP_A(ins) DUK__REGP(DUK_DEC_A((ins)))
  65316. #define DUK__REGP_B(ins) DUK__REGP(DUK_DEC_B((ins)))
  65317. #define DUK__REGP_C(ins) DUK__REGP(DUK_DEC_C((ins)))
  65318. #define DUK__REGP_BC(ins) DUK__REGP(DUK_DEC_BC((ins)))
  65319. #define DUK__CONSTP_A(ins) DUK__CONSTP(DUK_DEC_A((ins)))
  65320. #define DUK__CONSTP_B(ins) DUK__CONSTP(DUK_DEC_B((ins)))
  65321. #define DUK__CONSTP_C(ins) DUK__CONSTP(DUK_DEC_C((ins)))
  65322. #define DUK__CONSTP_BC(ins) DUK__CONSTP(DUK_DEC_BC((ins)))
  65323. #define DUK__REGCONSTP_B(ins) ((((ins) & DUK__RCBIT_B) ? consts : thr->valstack_bottom) + DUK_DEC_B((ins)))
  65324. #define DUK__REGCONSTP_C(ins) ((((ins) & DUK__RCBIT_C) ? consts : thr->valstack_bottom) + DUK_DEC_C((ins)))
  65325. #endif /* DUK_USE_EXEC_REGCONST_OPTIMIZE */
  65326. #if defined(DUK_USE_VERBOSE_EXECUTOR_ERRORS)
  65327. #define DUK__INTERNAL_ERROR(msg) do { \
  65328. DUK_ERROR_ERROR(thr, (msg)); \
  65329. DUK_WO_NORETURN(return;); \
  65330. } while (0)
  65331. #else
  65332. #define DUK__INTERNAL_ERROR(msg) do { \
  65333. goto internal_error; \
  65334. } while (0)
  65335. #endif
  65336. #define DUK__SYNC_CURR_PC() do { \
  65337. duk_activation *duk__act; \
  65338. duk__act = thr->callstack_curr; \
  65339. duk__act->curr_pc = curr_pc; \
  65340. } while (0)
  65341. #define DUK__SYNC_AND_NULL_CURR_PC() do { \
  65342. duk_activation *duk__act; \
  65343. duk__act = thr->callstack_curr; \
  65344. duk__act->curr_pc = curr_pc; \
  65345. thr->ptr_curr_pc = NULL; \
  65346. } while (0)
  65347. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65348. #define DUK__LOOKUP_INDIRECT(idx) do { \
  65349. (idx) = (duk_uint_fast_t) duk_get_uint(thr, (duk_idx_t) (idx)); \
  65350. } while (0)
  65351. #elif defined(DUK_USE_FASTINT)
  65352. #define DUK__LOOKUP_INDIRECT(idx) do { \
  65353. duk_tval *tv_ind; \
  65354. tv_ind = DUK__REGP((idx)); \
  65355. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); \
  65356. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv_ind)); /* compiler guarantees */ \
  65357. (idx) = (duk_uint_fast_t) DUK_TVAL_GET_FASTINT_U32(tv_ind); \
  65358. } while (0)
  65359. #else
  65360. #define DUK__LOOKUP_INDIRECT(idx) do { \
  65361. duk_tval *tv_ind; \
  65362. tv_ind = DUK__REGP(idx); \
  65363. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_ind)); \
  65364. idx = (duk_uint_fast_t) DUK_TVAL_GET_NUMBER(tv_ind); \
  65365. } while (0)
  65366. #endif
  65367. DUK_LOCAL void duk__handle_executor_error(duk_heap *heap,
  65368. duk_activation *entry_act,
  65369. duk_int_t entry_call_recursion_depth,
  65370. duk_jmpbuf *entry_jmpbuf_ptr) {
  65371. duk_small_uint_t lj_ret;
  65372. /* Longjmp callers are required to sync-and-null thr->ptr_curr_pc
  65373. * before longjmp.
  65374. */
  65375. DUK_ASSERT(heap->curr_thread != NULL);
  65376. DUK_ASSERT(heap->curr_thread->ptr_curr_pc == NULL);
  65377. /* XXX: signalling the need to shrink check (only if unwound) */
  65378. /* Must be restored here to handle e.g. yields properly. */
  65379. heap->call_recursion_depth = entry_call_recursion_depth;
  65380. /* Switch to caller's setjmp() catcher so that if an error occurs
  65381. * during error handling, it is always propagated outwards instead
  65382. * of causing an infinite loop in our own handler.
  65383. */
  65384. heap->lj.jmpbuf_ptr = (duk_jmpbuf *) entry_jmpbuf_ptr;
  65385. lj_ret = duk__handle_longjmp(heap->curr_thread, entry_act);
  65386. /* Error handling complete, remove side effect protections.
  65387. */
  65388. #if defined(DUK_USE_ASSERTIONS)
  65389. DUK_ASSERT(heap->error_not_allowed == 1);
  65390. heap->error_not_allowed = 0;
  65391. #endif
  65392. DUK_ASSERT(heap->pf_prevent_count > 0);
  65393. heap->pf_prevent_count--;
  65394. DUK_DD(DUK_DDPRINT("executor error handled, pf_prevent_count updated to %ld", (long) heap->pf_prevent_count));
  65395. if (lj_ret == DUK__LONGJMP_RESTART) {
  65396. /* Restart bytecode execution, possibly with a changed thread. */
  65397. DUK_REFZERO_CHECK_SLOW(heap->curr_thread);
  65398. } else {
  65399. /* If an error is propagated, don't run refzero checks here.
  65400. * The next catcher will deal with that. Pf_prevent_count
  65401. * will be re-bumped by the longjmp.
  65402. */
  65403. DUK_ASSERT(lj_ret == DUK__LONGJMP_RETHROW); /* Rethrow error to calling state. */
  65404. DUK_ASSERT(heap->lj.jmpbuf_ptr == entry_jmpbuf_ptr); /* Longjmp handling has restored jmpbuf_ptr. */
  65405. /* Thread may have changed, e.g. YIELD converted to THROW. */
  65406. duk_err_longjmp(heap->curr_thread);
  65407. DUK_UNREACHABLE();
  65408. }
  65409. }
  65410. /* Outer executor with setjmp/longjmp handling. */
  65411. DUK_INTERNAL void duk_js_execute_bytecode(duk_hthread *exec_thr) {
  65412. /* Entry level info. */
  65413. duk_hthread *entry_thread;
  65414. duk_activation *entry_act;
  65415. duk_int_t entry_call_recursion_depth;
  65416. duk_jmpbuf *entry_jmpbuf_ptr;
  65417. duk_jmpbuf our_jmpbuf;
  65418. duk_heap *heap;
  65419. DUK_ASSERT(exec_thr != NULL);
  65420. DUK_ASSERT(exec_thr->heap != NULL);
  65421. DUK_ASSERT(exec_thr->heap->curr_thread != NULL);
  65422. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR((duk_heaphdr *) exec_thr);
  65423. DUK_ASSERT(exec_thr->callstack_top >= 1); /* at least one activation, ours */
  65424. DUK_ASSERT(exec_thr->callstack_curr != NULL);
  65425. DUK_ASSERT(DUK_ACT_GET_FUNC(exec_thr->callstack_curr) != NULL);
  65426. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(exec_thr->callstack_curr)));
  65427. DUK_GC_TORTURE(exec_thr->heap);
  65428. entry_thread = exec_thr;
  65429. heap = entry_thread->heap;
  65430. entry_act = entry_thread->callstack_curr;
  65431. DUK_ASSERT(entry_act != NULL);
  65432. entry_call_recursion_depth = entry_thread->heap->call_recursion_depth;
  65433. entry_jmpbuf_ptr = entry_thread->heap->lj.jmpbuf_ptr;
  65434. /*
  65435. * Note: we currently assume that the setjmp() catchpoint is
  65436. * not re-entrant (longjmp() cannot be called more than once
  65437. * for a single setjmp()).
  65438. *
  65439. * See doc/code-issues.rst for notes on variable assignment
  65440. * before and after setjmp().
  65441. */
  65442. for (;;) {
  65443. heap->lj.jmpbuf_ptr = &our_jmpbuf;
  65444. DUK_ASSERT(heap->lj.jmpbuf_ptr != NULL);
  65445. #if defined(DUK_USE_CPP_EXCEPTIONS)
  65446. try {
  65447. #else
  65448. DUK_ASSERT(heap->lj.jmpbuf_ptr == &our_jmpbuf);
  65449. if (DUK_SETJMP(our_jmpbuf.jb) == 0) {
  65450. #endif
  65451. /* Execute bytecode until returned or longjmp(). */
  65452. duk__js_execute_bytecode_inner(entry_thread, entry_act);
  65453. /* Successful return: restore jmpbuf and return to caller. */
  65454. heap->lj.jmpbuf_ptr = entry_jmpbuf_ptr;
  65455. return;
  65456. #if defined(DUK_USE_CPP_EXCEPTIONS)
  65457. } catch (duk_internal_exception &exc) {
  65458. #else
  65459. } else {
  65460. #endif
  65461. #if defined(DUK_USE_CPP_EXCEPTIONS)
  65462. DUK_UNREF(exc);
  65463. #endif
  65464. DUK_DDD(DUK_DDDPRINT("longjmp caught by bytecode executor"));
  65465. DUK_STATS_INC(exec_thr->heap, stats_exec_throw);
  65466. duk__handle_executor_error(heap,
  65467. entry_act,
  65468. entry_call_recursion_depth,
  65469. entry_jmpbuf_ptr);
  65470. }
  65471. #if defined(DUK_USE_CPP_EXCEPTIONS)
  65472. catch (duk_fatal_exception &exc) {
  65473. DUK_D(DUK_DPRINT("rethrow duk_fatal_exception"));
  65474. throw;
  65475. } catch (std::exception &exc) {
  65476. const char *what = exc.what();
  65477. if (!what) {
  65478. what = "unknown";
  65479. }
  65480. DUK_D(DUK_DPRINT("unexpected c++ std::exception (perhaps thrown by user code)"));
  65481. DUK_STATS_INC(exec_thr->heap, stats_exec_throw);
  65482. try {
  65483. DUK_ASSERT(heap->curr_thread != NULL);
  65484. DUK_ERROR_FMT1(heap->curr_thread, DUK_ERR_TYPE_ERROR, "caught invalid c++ std::exception '%s' (perhaps thrown by user code)", what);
  65485. DUK_WO_NORETURN(return;);
  65486. } catch (duk_internal_exception exc) {
  65487. DUK_D(DUK_DPRINT("caught api error thrown from unexpected c++ std::exception"));
  65488. DUK_UNREF(exc);
  65489. duk__handle_executor_error(heap,
  65490. entry_act,
  65491. entry_call_recursion_depth,
  65492. entry_jmpbuf_ptr);
  65493. }
  65494. } catch (...) {
  65495. DUK_D(DUK_DPRINT("unexpected c++ exception (perhaps thrown by user code)"));
  65496. DUK_STATS_INC(exec_thr->heap, stats_exec_throw);
  65497. try {
  65498. DUK_ASSERT(heap->curr_thread != NULL);
  65499. DUK_ERROR_TYPE(heap->curr_thread, "caught invalid c++ exception (perhaps thrown by user code)");
  65500. DUK_WO_NORETURN(return;);
  65501. } catch (duk_internal_exception exc) {
  65502. DUK_D(DUK_DPRINT("caught api error thrown from unexpected c++ exception"));
  65503. DUK_UNREF(exc);
  65504. duk__handle_executor_error(heap,
  65505. entry_act,
  65506. entry_call_recursion_depth,
  65507. entry_jmpbuf_ptr);
  65508. }
  65509. }
  65510. #endif
  65511. }
  65512. DUK_WO_NORETURN(return;);
  65513. }
  65514. /* Inner executor, performance critical. */
  65515. DUK_LOCAL DUK_NOINLINE DUK_HOT void duk__js_execute_bytecode_inner(duk_hthread *entry_thread, duk_activation *entry_act) {
  65516. /* Current PC, accessed by other functions through thr->ptr_to_curr_pc.
  65517. * Critical for performance. It would be safest to make this volatile,
  65518. * but that eliminates performance benefits; aliasing guarantees
  65519. * should be enough though.
  65520. */
  65521. duk_instr_t *curr_pc; /* bytecode has a stable pointer */
  65522. /* Hot variables for interpretation. Critical for performance,
  65523. * but must add sparingly to minimize register shuffling.
  65524. */
  65525. duk_hthread *thr; /* stable */
  65526. duk_tval *consts; /* stable */
  65527. duk_uint_fast32_t ins;
  65528. /* 'funcs' is quite rarely used, so no local for it */
  65529. #if defined(DUK_USE_EXEC_FUN_LOCAL)
  65530. duk_hcompfunc *fun;
  65531. #else
  65532. /* 'fun' is quite rarely used, so no local for it */
  65533. #endif
  65534. #if defined(DUK_USE_INTERRUPT_COUNTER)
  65535. duk_int_t int_ctr;
  65536. #endif
  65537. #if defined(DUK_USE_ASSERTIONS)
  65538. duk_size_t valstack_top_base; /* valstack top, should match before interpreting each op (no leftovers) */
  65539. #endif
  65540. /* Optimized reg/const access macros assume sizeof(duk_tval) to be
  65541. * either 8 or 16. Heap allocation checks this even without asserts
  65542. * enabled now because it can't be autodetected in duk_config.h.
  65543. */
  65544. #if 1
  65545. #if defined(DUK_USE_PACKED_TVAL)
  65546. DUK_ASSERT(sizeof(duk_tval) == 8);
  65547. #else
  65548. DUK_ASSERT(sizeof(duk_tval) == 16);
  65549. #endif
  65550. #endif
  65551. DUK_GC_TORTURE(entry_thread->heap);
  65552. /*
  65553. * Restart execution by reloading thread state.
  65554. *
  65555. * Note that 'thr' and any thread configuration may have changed,
  65556. * so all local variables are suspect and we need to reinitialize.
  65557. *
  65558. * The number of local variables should be kept to a minimum: if
  65559. * the variables are spilled, they will need to be loaded from
  65560. * memory anyway.
  65561. *
  65562. * Any 'goto restart_execution;' code path in opcode dispatch must
  65563. * ensure 'curr_pc' is synced back to act->curr_pc before the goto
  65564. * takes place.
  65565. *
  65566. * The interpreter must be very careful with memory pointers, as
  65567. * many pointers are not guaranteed to be 'stable' and may be
  65568. * reallocated and relocated on-the-fly quite easily (e.g. by a
  65569. * memory allocation or a property access).
  65570. *
  65571. * The following are assumed to have stable pointers:
  65572. * - the current thread
  65573. * - the current function
  65574. * - the bytecode, constant table, inner function table of the
  65575. * current function (as they are a part of the function allocation)
  65576. *
  65577. * The following are assumed to have semi-stable pointers:
  65578. * - the current activation entry: stable as long as callstack
  65579. * is not changed (reallocated by growing or shrinking), or
  65580. * by any garbage collection invocation (through finalizers)
  65581. * - Note in particular that ANY DECREF can invalidate the
  65582. * activation pointer, so for the most part a fresh lookup
  65583. * is required
  65584. *
  65585. * The following are not assumed to have stable pointers at all:
  65586. * - the value stack (registers) of the current thread
  65587. *
  65588. * See execution.rst for discussion.
  65589. */
  65590. restart_execution:
  65591. /* Lookup current thread; use the stable 'entry_thread' for this to
  65592. * avoid clobber warnings. Any valid, reachable 'thr' value would be
  65593. * fine for this, so using 'entry_thread' is just to silence warnings.
  65594. */
  65595. thr = entry_thread->heap->curr_thread;
  65596. DUK_ASSERT(thr != NULL);
  65597. DUK_ASSERT(thr->callstack_top >= 1);
  65598. DUK_ASSERT(thr->callstack_curr != NULL);
  65599. DUK_ASSERT(DUK_ACT_GET_FUNC(thr->callstack_curr) != NULL);
  65600. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(DUK_ACT_GET_FUNC(thr->callstack_curr)));
  65601. DUK_GC_TORTURE(thr->heap);
  65602. thr->ptr_curr_pc = &curr_pc;
  65603. /* Relookup and initialize dispatch loop variables. Debugger check. */
  65604. {
  65605. duk_activation *act;
  65606. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  65607. duk_hcompfunc *fun;
  65608. #endif
  65609. /* Assume interrupt init/counter are properly initialized here. */
  65610. /* Assume that thr->valstack_bottom has been set-up before getting here. */
  65611. act = thr->callstack_curr;
  65612. DUK_ASSERT(act != NULL);
  65613. fun = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  65614. DUK_ASSERT(fun != NULL);
  65615. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == fun->nregs);
  65616. consts = DUK_HCOMPFUNC_GET_CONSTS_BASE(thr->heap, fun);
  65617. DUK_ASSERT(consts != NULL);
  65618. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  65619. if (DUK_UNLIKELY(duk_debug_is_attached(thr->heap) && !thr->heap->dbg_processing)) {
  65620. duk__executor_recheck_debugger(thr, act, fun);
  65621. DUK_ASSERT(act == thr->callstack_curr);
  65622. DUK_ASSERT(act != NULL);
  65623. }
  65624. #endif /* DUK_USE_DEBUGGER_SUPPORT */
  65625. #if defined(DUK_USE_ASSERTIONS)
  65626. valstack_top_base = (duk_size_t) (thr->valstack_top - thr->valstack);
  65627. #endif
  65628. /* Set up curr_pc for opcode dispatch. */
  65629. curr_pc = act->curr_pc;
  65630. }
  65631. DUK_DD(DUK_DDPRINT("restarting execution, thr %p, act idx %ld, fun %p,"
  65632. "consts %p, funcs %p, lev %ld, regbot %ld, regtop %ld, "
  65633. "preventcount=%ld",
  65634. (void *) thr,
  65635. (long) (thr->callstack_top - 1),
  65636. (void *) DUK__FUN(),
  65637. (void *) DUK_HCOMPFUNC_GET_CONSTS_BASE(thr->heap, DUK__FUN()),
  65638. (void *) DUK_HCOMPFUNC_GET_FUNCS_BASE(thr->heap, DUK__FUN()),
  65639. (long) (thr->callstack_top - 1),
  65640. (long) (thr->valstack_bottom - thr->valstack),
  65641. (long) (thr->valstack_top - thr->valstack),
  65642. (long) thr->callstack_preventcount));
  65643. /* Dispatch loop. */
  65644. for (;;) {
  65645. duk_uint8_t op;
  65646. DUK_ASSERT(thr->callstack_top >= 1);
  65647. DUK_ASSERT(thr->valstack_top - thr->valstack_bottom == DUK__FUN()->nregs);
  65648. DUK_ASSERT((duk_size_t) (thr->valstack_top - thr->valstack) == valstack_top_base);
  65649. /* Executor interrupt counter check, used to implement breakpoints,
  65650. * debugging interface, execution timeouts, etc. The counter is heap
  65651. * specific but is maintained in the current thread to make the check
  65652. * as fast as possible. The counter is copied back to the heap struct
  65653. * whenever a thread switch occurs by the DUK_HEAP_SWITCH_THREAD() macro.
  65654. */
  65655. #if defined(DUK_USE_INTERRUPT_COUNTER)
  65656. int_ctr = thr->interrupt_counter;
  65657. if (DUK_LIKELY(int_ctr > 0)) {
  65658. thr->interrupt_counter = int_ctr - 1;
  65659. } else {
  65660. /* Trigger at zero or below */
  65661. duk_small_uint_t exec_int_ret;
  65662. DUK_STATS_INC(thr->heap, stats_exec_interrupt);
  65663. /* Write curr_pc back for the debugger. */
  65664. {
  65665. duk_activation *act;
  65666. DUK_ASSERT(thr->callstack_top > 0);
  65667. act = thr->callstack_curr;
  65668. DUK_ASSERT(act != NULL);
  65669. act->curr_pc = (duk_instr_t *) curr_pc;
  65670. }
  65671. /* Forced restart caused by a function return; must recheck
  65672. * debugger breakpoints before checking line transitions,
  65673. * see GH-303. Restart and then handle interrupt_counter
  65674. * zero again.
  65675. */
  65676. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  65677. if (thr->heap->dbg_force_restart) {
  65678. DUK_DD(DUK_DDPRINT("dbg_force_restart flag forced restart execution")); /* GH-303 */
  65679. thr->heap->dbg_force_restart = 0;
  65680. goto restart_execution;
  65681. }
  65682. #endif
  65683. exec_int_ret = duk__executor_interrupt(thr);
  65684. if (exec_int_ret == DUK__INT_RESTART) {
  65685. /* curr_pc synced back above */
  65686. goto restart_execution;
  65687. }
  65688. }
  65689. #endif /* DUK_USE_INTERRUPT_COUNTER */
  65690. #if defined(DUK_USE_INTERRUPT_COUNTER) && defined(DUK_USE_DEBUG)
  65691. /* For cross-checking during development: ensure dispatch count
  65692. * matches cumulative interrupt counter init value sums.
  65693. */
  65694. thr->heap->inst_count_exec++;
  65695. #endif
  65696. #if defined(DUK_USE_ASSERTIONS) || defined(DUK_USE_DEBUG)
  65697. {
  65698. duk_activation *act;
  65699. act = thr->callstack_curr;
  65700. DUK_ASSERT(curr_pc >= DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, DUK__FUN()));
  65701. DUK_ASSERT(curr_pc < DUK_HCOMPFUNC_GET_CODE_END(thr->heap, DUK__FUN()));
  65702. DUK_UNREF(act); /* if debugging disabled */
  65703. DUK_DDD(DUK_DDDPRINT("executing bytecode: pc=%ld, ins=0x%08lx, op=%ld, valstack_top=%ld/%ld, nregs=%ld --> %!I",
  65704. (long) (curr_pc - DUK_HCOMPFUNC_GET_CODE_BASE(thr->heap, DUK__FUN())),
  65705. (unsigned long) *curr_pc,
  65706. (long) DUK_DEC_OP(*curr_pc),
  65707. (long) (thr->valstack_top - thr->valstack),
  65708. (long) (thr->valstack_end - thr->valstack),
  65709. (long) (DUK__FUN() ? DUK__FUN()->nregs : -1),
  65710. (duk_instr_t) *curr_pc));
  65711. }
  65712. #endif
  65713. #if defined(DUK_USE_ASSERTIONS)
  65714. /* Quite heavy assert: check valstack policy. Improper
  65715. * shuffle instructions can write beyond valstack_top/end
  65716. * so this check catches them in the act.
  65717. */
  65718. {
  65719. duk_tval *tv;
  65720. tv = thr->valstack_top;
  65721. while (tv != thr->valstack_end) {
  65722. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(tv));
  65723. tv++;
  65724. }
  65725. }
  65726. #endif
  65727. ins = *curr_pc++;
  65728. DUK_STATS_INC(thr->heap, stats_exec_opcodes);
  65729. /* Typing: use duk_small_(u)int_fast_t when decoding small
  65730. * opcode fields (op, A, B, C, BC) which fit into 16 bits
  65731. * and duk_(u)int_fast_t when decoding larger fields (e.g.
  65732. * ABC). Use unsigned variant by default, signed when the
  65733. * value is used in signed arithmetic. Using variable names
  65734. * such as 'a', 'b', 'c', 'bc', etc makes it easier to spot
  65735. * typing mismatches.
  65736. */
  65737. /* Switch based on opcode. Cast to 8-bit unsigned value and
  65738. * use a fully populated case clauses so that the compiler
  65739. * will (at least usually) omit a bounds check.
  65740. */
  65741. op = (duk_uint8_t) DUK_DEC_OP(ins);
  65742. switch (op) {
  65743. /* Some useful macros. These access inner executor variables
  65744. * directly so they only apply within the executor.
  65745. */
  65746. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65747. #define DUK__REPLACE_TOP_A_BREAK() { goto replace_top_a; }
  65748. #define DUK__REPLACE_TOP_BC_BREAK() { goto replace_top_bc; }
  65749. #define DUK__REPLACE_BOOL_A_BREAK(bval) { \
  65750. duk_bool_t duk__bval; \
  65751. duk__bval = (bval); \
  65752. DUK_ASSERT(duk__bval == 0 || duk__bval == 1); \
  65753. duk_push_boolean(thr, duk__bval); \
  65754. DUK__REPLACE_TOP_A_BREAK(); \
  65755. }
  65756. #else
  65757. #define DUK__REPLACE_TOP_A_BREAK() { DUK__REPLACE_TO_TVPTR(thr, DUK__REGP_A(ins)); break; }
  65758. #define DUK__REPLACE_TOP_BC_BREAK() { DUK__REPLACE_TO_TVPTR(thr, DUK__REGP_BC(ins)); break; }
  65759. #define DUK__REPLACE_BOOL_A_BREAK(bval) { \
  65760. duk_bool_t duk__bval; \
  65761. duk_tval *duk__tvdst; \
  65762. duk__bval = (bval); \
  65763. DUK_ASSERT(duk__bval == 0 || duk__bval == 1); \
  65764. duk__tvdst = DUK__REGP_A(ins); \
  65765. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, duk__tvdst, duk__bval); \
  65766. break; \
  65767. }
  65768. #endif
  65769. /* XXX: 12 + 12 bit variant might make sense too, for both reg and
  65770. * const loads.
  65771. */
  65772. /* For LDREG, STREG, LDCONST footprint optimized variants would just
  65773. * duk_dup() + duk_replace(), but because they're used quite a lot
  65774. * they're currently intentionally not size optimized.
  65775. */
  65776. case DUK_OP_LDREG: {
  65777. duk_tval *tv1, *tv2;
  65778. tv1 = DUK__REGP_A(ins);
  65779. tv2 = DUK__REGP_BC(ins);
  65780. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2); /* side effects */
  65781. break;
  65782. }
  65783. case DUK_OP_STREG: {
  65784. duk_tval *tv1, *tv2;
  65785. tv1 = DUK__REGP_A(ins);
  65786. tv2 = DUK__REGP_BC(ins);
  65787. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv2, tv1); /* side effects */
  65788. break;
  65789. }
  65790. case DUK_OP_LDCONST: {
  65791. duk_tval *tv1, *tv2;
  65792. tv1 = DUK__REGP_A(ins);
  65793. tv2 = DUK__CONSTP_BC(ins);
  65794. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2); /* side effects */
  65795. break;
  65796. }
  65797. /* LDINT and LDINTX are intended to load an arbitrary signed
  65798. * 32-bit value. Only an LDINT+LDINTX sequence is supported.
  65799. * This also guarantees all values remain fastints.
  65800. */
  65801. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65802. case DUK_OP_LDINT: {
  65803. duk_int32_t val;
  65804. val = (duk_int32_t) DUK_DEC_BC(ins) - (duk_int32_t) DUK_BC_LDINT_BIAS;
  65805. duk_push_int(thr, val);
  65806. DUK__REPLACE_TOP_A_BREAK();
  65807. }
  65808. case DUK_OP_LDINTX: {
  65809. duk_int32_t val;
  65810. val = (duk_int32_t) duk_get_int(thr, DUK_DEC_A(ins));
  65811. val = (val << DUK_BC_LDINTX_SHIFT) + (duk_int32_t) DUK_DEC_BC(ins); /* no bias */
  65812. duk_push_int(thr, val);
  65813. DUK__REPLACE_TOP_A_BREAK();
  65814. }
  65815. #else /* DUK_USE_EXEC_PREFER_SIZE */
  65816. case DUK_OP_LDINT: {
  65817. duk_tval *tv1;
  65818. duk_int32_t val;
  65819. val = (duk_int32_t) DUK_DEC_BC(ins) - (duk_int32_t) DUK_BC_LDINT_BIAS;
  65820. tv1 = DUK__REGP_A(ins);
  65821. DUK_TVAL_SET_I32_UPDREF(thr, tv1, val); /* side effects */
  65822. break;
  65823. }
  65824. case DUK_OP_LDINTX: {
  65825. duk_tval *tv1;
  65826. duk_int32_t val;
  65827. tv1 = DUK__REGP_A(ins);
  65828. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  65829. #if defined(DUK_USE_FASTINT)
  65830. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1));
  65831. val = DUK_TVAL_GET_FASTINT_I32(tv1);
  65832. #else
  65833. /* XXX: fast double-to-int conversion, we know number is integer in [-0x80000000,0xffffffff]. */
  65834. val = (duk_int32_t) DUK_TVAL_GET_NUMBER(tv1);
  65835. #endif
  65836. val = (duk_int32_t) ((duk_uint32_t) val << DUK_BC_LDINTX_SHIFT) + (duk_int32_t) DUK_DEC_BC(ins); /* no bias */
  65837. DUK_TVAL_SET_I32_UPDREF(thr, tv1, val); /* side effects */
  65838. break;
  65839. }
  65840. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  65841. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65842. case DUK_OP_LDTHIS: {
  65843. duk_push_this(thr);
  65844. DUK__REPLACE_TOP_BC_BREAK();
  65845. }
  65846. case DUK_OP_LDUNDEF: {
  65847. duk_to_undefined(thr, (duk_idx_t) DUK_DEC_BC(ins));
  65848. break;
  65849. }
  65850. case DUK_OP_LDNULL: {
  65851. duk_to_null(thr, (duk_idx_t) DUK_DEC_BC(ins));
  65852. break;
  65853. }
  65854. case DUK_OP_LDTRUE: {
  65855. duk_push_true(thr);
  65856. DUK__REPLACE_TOP_BC_BREAK();
  65857. }
  65858. case DUK_OP_LDFALSE: {
  65859. duk_push_false(thr);
  65860. DUK__REPLACE_TOP_BC_BREAK();
  65861. }
  65862. #else /* DUK_USE_EXEC_PREFER_SIZE */
  65863. case DUK_OP_LDTHIS: {
  65864. /* Note: 'this' may be bound to any value, not just an object */
  65865. duk_tval *tv1, *tv2;
  65866. tv1 = DUK__REGP_BC(ins);
  65867. tv2 = thr->valstack_bottom - 1; /* 'this binding' is just under bottom */
  65868. DUK_ASSERT(tv2 >= thr->valstack);
  65869. DUK_TVAL_SET_TVAL_UPDREF_FAST(thr, tv1, tv2); /* side effects */
  65870. break;
  65871. }
  65872. case DUK_OP_LDUNDEF: {
  65873. duk_tval *tv1;
  65874. tv1 = DUK__REGP_BC(ins);
  65875. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv1); /* side effects */
  65876. break;
  65877. }
  65878. case DUK_OP_LDNULL: {
  65879. duk_tval *tv1;
  65880. tv1 = DUK__REGP_BC(ins);
  65881. DUK_TVAL_SET_NULL_UPDREF(thr, tv1); /* side effects */
  65882. break;
  65883. }
  65884. case DUK_OP_LDTRUE: {
  65885. duk_tval *tv1;
  65886. tv1 = DUK__REGP_BC(ins);
  65887. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv1, 1); /* side effects */
  65888. break;
  65889. }
  65890. case DUK_OP_LDFALSE: {
  65891. duk_tval *tv1;
  65892. tv1 = DUK__REGP_BC(ins);
  65893. DUK_TVAL_SET_BOOLEAN_UPDREF(thr, tv1, 0); /* side effects */
  65894. break;
  65895. }
  65896. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  65897. case DUK_OP_BNOT: {
  65898. duk__vm_bitwise_not(thr, DUK_DEC_BC(ins), DUK_DEC_A(ins));
  65899. break;
  65900. }
  65901. case DUK_OP_LNOT: {
  65902. duk__vm_logical_not(thr, DUK_DEC_BC(ins), DUK_DEC_A(ins));
  65903. break;
  65904. }
  65905. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65906. case DUK_OP_UNM:
  65907. case DUK_OP_UNP: {
  65908. duk__vm_arith_unary_op(thr, DUK_DEC_BC(ins), DUK_DEC_A(ins), op);
  65909. break;
  65910. }
  65911. #else /* DUK_USE_EXEC_PREFER_SIZE */
  65912. case DUK_OP_UNM: {
  65913. duk__vm_arith_unary_op(thr, DUK_DEC_BC(ins), DUK_DEC_A(ins), DUK_OP_UNM);
  65914. break;
  65915. }
  65916. case DUK_OP_UNP: {
  65917. duk__vm_arith_unary_op(thr, DUK_DEC_BC(ins), DUK_DEC_A(ins), DUK_OP_UNP);
  65918. break;
  65919. }
  65920. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  65921. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65922. case DUK_OP_TYPEOF: {
  65923. duk_small_uint_t stridx;
  65924. stridx = duk_js_typeof_stridx(DUK__REGP_BC(ins));
  65925. DUK_ASSERT_STRIDX_VALID(stridx);
  65926. duk_push_hstring_stridx(thr, stridx);
  65927. DUK__REPLACE_TOP_A_BREAK();
  65928. }
  65929. #else /* DUK_USE_EXEC_PREFER_SIZE */
  65930. case DUK_OP_TYPEOF: {
  65931. duk_tval *tv;
  65932. duk_small_uint_t stridx;
  65933. duk_hstring *h_str;
  65934. tv = DUK__REGP_BC(ins);
  65935. stridx = duk_js_typeof_stridx(tv);
  65936. DUK_ASSERT_STRIDX_VALID(stridx);
  65937. h_str = DUK_HTHREAD_GET_STRING(thr, stridx);
  65938. tv = DUK__REGP_A(ins);
  65939. DUK_TVAL_SET_STRING_UPDREF(thr, tv, h_str);
  65940. break;
  65941. }
  65942. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  65943. case DUK_OP_TYPEOFID: {
  65944. duk_small_uint_t stridx;
  65945. #if !defined(DUK_USE_EXEC_PREFER_SIZE)
  65946. duk_hstring *h_str;
  65947. #endif
  65948. duk_activation *act;
  65949. duk_hstring *name;
  65950. duk_tval *tv;
  65951. /* A -> target register
  65952. * BC -> constant index of identifier name
  65953. */
  65954. tv = DUK__CONSTP_BC(ins);
  65955. DUK_ASSERT(DUK_TVAL_IS_STRING(tv));
  65956. name = DUK_TVAL_GET_STRING(tv);
  65957. tv = NULL; /* lookup has side effects */
  65958. act = thr->callstack_curr;
  65959. if (duk_js_getvar_activation(thr, act, name, 0 /*throw*/)) {
  65960. /* -> [... val this] */
  65961. tv = DUK_GET_TVAL_NEGIDX(thr, -2);
  65962. stridx = duk_js_typeof_stridx(tv);
  65963. tv = NULL; /* no longer needed */
  65964. duk_pop_2_unsafe(thr);
  65965. } else {
  65966. /* unresolvable, no stack changes */
  65967. stridx = DUK_STRIDX_LC_UNDEFINED;
  65968. }
  65969. DUK_ASSERT_STRIDX_VALID(stridx);
  65970. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  65971. duk_push_hstring_stridx(thr, stridx);
  65972. DUK__REPLACE_TOP_A_BREAK();
  65973. #else /* DUK_USE_EXEC_PREFER_SIZE */
  65974. h_str = DUK_HTHREAD_GET_STRING(thr, stridx);
  65975. tv = DUK__REGP_A(ins);
  65976. DUK_TVAL_SET_STRING_UPDREF(thr, tv, h_str);
  65977. break;
  65978. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  65979. }
  65980. /* Equality: E5 Sections 11.9.1, 11.9.3 */
  65981. #define DUK__EQ_BODY(barg,carg) { \
  65982. duk_bool_t tmp; \
  65983. tmp = duk_js_equals(thr, (barg), (carg)); \
  65984. DUK_ASSERT(tmp == 0 || tmp == 1); \
  65985. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  65986. }
  65987. #define DUK__NEQ_BODY(barg,carg) { \
  65988. duk_bool_t tmp; \
  65989. tmp = duk_js_equals(thr, (barg), (carg)); \
  65990. DUK_ASSERT(tmp == 0 || tmp == 1); \
  65991. tmp ^= 1; \
  65992. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  65993. }
  65994. #define DUK__SEQ_BODY(barg,carg) { \
  65995. duk_bool_t tmp; \
  65996. tmp = duk_js_strict_equals((barg), (carg)); \
  65997. DUK_ASSERT(tmp == 0 || tmp == 1); \
  65998. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  65999. }
  66000. #define DUK__SNEQ_BODY(barg,carg) { \
  66001. duk_bool_t tmp; \
  66002. tmp = duk_js_strict_equals((barg), (carg)); \
  66003. DUK_ASSERT(tmp == 0 || tmp == 1); \
  66004. tmp ^= 1; \
  66005. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  66006. }
  66007. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66008. case DUK_OP_EQ_RR:
  66009. case DUK_OP_EQ_CR:
  66010. case DUK_OP_EQ_RC:
  66011. case DUK_OP_EQ_CC:
  66012. DUK__EQ_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66013. case DUK_OP_NEQ_RR:
  66014. case DUK_OP_NEQ_CR:
  66015. case DUK_OP_NEQ_RC:
  66016. case DUK_OP_NEQ_CC:
  66017. DUK__NEQ_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66018. case DUK_OP_SEQ_RR:
  66019. case DUK_OP_SEQ_CR:
  66020. case DUK_OP_SEQ_RC:
  66021. case DUK_OP_SEQ_CC:
  66022. DUK__SEQ_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66023. case DUK_OP_SNEQ_RR:
  66024. case DUK_OP_SNEQ_CR:
  66025. case DUK_OP_SNEQ_RC:
  66026. case DUK_OP_SNEQ_CC:
  66027. DUK__SNEQ_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66028. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66029. case DUK_OP_EQ_RR:
  66030. DUK__EQ_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66031. case DUK_OP_EQ_CR:
  66032. DUK__EQ_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66033. case DUK_OP_EQ_RC:
  66034. DUK__EQ_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66035. case DUK_OP_EQ_CC:
  66036. DUK__EQ_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66037. case DUK_OP_NEQ_RR:
  66038. DUK__NEQ_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66039. case DUK_OP_NEQ_CR:
  66040. DUK__NEQ_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66041. case DUK_OP_NEQ_RC:
  66042. DUK__NEQ_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66043. case DUK_OP_NEQ_CC:
  66044. DUK__NEQ_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66045. case DUK_OP_SEQ_RR:
  66046. DUK__SEQ_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66047. case DUK_OP_SEQ_CR:
  66048. DUK__SEQ_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66049. case DUK_OP_SEQ_RC:
  66050. DUK__SEQ_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66051. case DUK_OP_SEQ_CC:
  66052. DUK__SEQ_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66053. case DUK_OP_SNEQ_RR:
  66054. DUK__SNEQ_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66055. case DUK_OP_SNEQ_CR:
  66056. DUK__SNEQ_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66057. case DUK_OP_SNEQ_RC:
  66058. DUK__SNEQ_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66059. case DUK_OP_SNEQ_CC:
  66060. DUK__SNEQ_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66061. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66062. #define DUK__COMPARE_BODY(arg1,arg2,flags) { \
  66063. duk_bool_t tmp; \
  66064. tmp = duk_js_compare_helper(thr, (arg1), (arg2), (flags)); \
  66065. DUK_ASSERT(tmp == 0 || tmp == 1); \
  66066. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  66067. }
  66068. #define DUK__GT_BODY(barg,carg) DUK__COMPARE_BODY((carg), (barg), 0)
  66069. #define DUK__GE_BODY(barg,carg) DUK__COMPARE_BODY((barg), (carg), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST | DUK_COMPARE_FLAG_NEGATE)
  66070. #define DUK__LT_BODY(barg,carg) DUK__COMPARE_BODY((barg), (carg), DUK_COMPARE_FLAG_EVAL_LEFT_FIRST)
  66071. #define DUK__LE_BODY(barg,carg) DUK__COMPARE_BODY((carg), (barg), DUK_COMPARE_FLAG_NEGATE)
  66072. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66073. case DUK_OP_GT_RR:
  66074. case DUK_OP_GT_CR:
  66075. case DUK_OP_GT_RC:
  66076. case DUK_OP_GT_CC:
  66077. DUK__GT_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66078. case DUK_OP_GE_RR:
  66079. case DUK_OP_GE_CR:
  66080. case DUK_OP_GE_RC:
  66081. case DUK_OP_GE_CC:
  66082. DUK__GE_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66083. case DUK_OP_LT_RR:
  66084. case DUK_OP_LT_CR:
  66085. case DUK_OP_LT_RC:
  66086. case DUK_OP_LT_CC:
  66087. DUK__LT_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66088. case DUK_OP_LE_RR:
  66089. case DUK_OP_LE_CR:
  66090. case DUK_OP_LE_RC:
  66091. case DUK_OP_LE_CC:
  66092. DUK__LE_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66093. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66094. case DUK_OP_GT_RR:
  66095. DUK__GT_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66096. case DUK_OP_GT_CR:
  66097. DUK__GT_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66098. case DUK_OP_GT_RC:
  66099. DUK__GT_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66100. case DUK_OP_GT_CC:
  66101. DUK__GT_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66102. case DUK_OP_GE_RR:
  66103. DUK__GE_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66104. case DUK_OP_GE_CR:
  66105. DUK__GE_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66106. case DUK_OP_GE_RC:
  66107. DUK__GE_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66108. case DUK_OP_GE_CC:
  66109. DUK__GE_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66110. case DUK_OP_LT_RR:
  66111. DUK__LT_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66112. case DUK_OP_LT_CR:
  66113. DUK__LT_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66114. case DUK_OP_LT_RC:
  66115. DUK__LT_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66116. case DUK_OP_LT_CC:
  66117. DUK__LT_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66118. case DUK_OP_LE_RR:
  66119. DUK__LE_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66120. case DUK_OP_LE_CR:
  66121. DUK__LE_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66122. case DUK_OP_LE_RC:
  66123. DUK__LE_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66124. case DUK_OP_LE_CC:
  66125. DUK__LE_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66126. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66127. /* No size optimized variant at present for IF. */
  66128. case DUK_OP_IFTRUE_R: {
  66129. if (duk_js_toboolean(DUK__REGP_BC(ins)) != 0) {
  66130. curr_pc++;
  66131. }
  66132. break;
  66133. }
  66134. case DUK_OP_IFTRUE_C: {
  66135. if (duk_js_toboolean(DUK__CONSTP_BC(ins)) != 0) {
  66136. curr_pc++;
  66137. }
  66138. break;
  66139. }
  66140. case DUK_OP_IFFALSE_R: {
  66141. if (duk_js_toboolean(DUK__REGP_BC(ins)) == 0) {
  66142. curr_pc++;
  66143. }
  66144. break;
  66145. }
  66146. case DUK_OP_IFFALSE_C: {
  66147. if (duk_js_toboolean(DUK__CONSTP_BC(ins)) == 0) {
  66148. curr_pc++;
  66149. }
  66150. break;
  66151. }
  66152. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66153. case DUK_OP_ADD_RR:
  66154. case DUK_OP_ADD_CR:
  66155. case DUK_OP_ADD_RC:
  66156. case DUK_OP_ADD_CC: {
  66157. /* XXX: could leave value on stack top and goto replace_top_a; */
  66158. duk__vm_arith_add(thr, DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins), DUK_DEC_A(ins));
  66159. break;
  66160. }
  66161. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66162. case DUK_OP_ADD_RR: {
  66163. duk__vm_arith_add(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins));
  66164. break;
  66165. }
  66166. case DUK_OP_ADD_CR: {
  66167. duk__vm_arith_add(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins));
  66168. break;
  66169. }
  66170. case DUK_OP_ADD_RC: {
  66171. duk__vm_arith_add(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins));
  66172. break;
  66173. }
  66174. case DUK_OP_ADD_CC: {
  66175. duk__vm_arith_add(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins));
  66176. break;
  66177. }
  66178. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66179. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66180. case DUK_OP_SUB_RR:
  66181. case DUK_OP_SUB_CR:
  66182. case DUK_OP_SUB_RC:
  66183. case DUK_OP_SUB_CC:
  66184. case DUK_OP_MUL_RR:
  66185. case DUK_OP_MUL_CR:
  66186. case DUK_OP_MUL_RC:
  66187. case DUK_OP_MUL_CC:
  66188. case DUK_OP_DIV_RR:
  66189. case DUK_OP_DIV_CR:
  66190. case DUK_OP_DIV_RC:
  66191. case DUK_OP_DIV_CC:
  66192. case DUK_OP_MOD_RR:
  66193. case DUK_OP_MOD_CR:
  66194. case DUK_OP_MOD_RC:
  66195. case DUK_OP_MOD_CC:
  66196. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  66197. case DUK_OP_EXP_RR:
  66198. case DUK_OP_EXP_CR:
  66199. case DUK_OP_EXP_RC:
  66200. case DUK_OP_EXP_CC:
  66201. #endif /* DUK_USE_ES7_EXP_OPERATOR */
  66202. {
  66203. /* XXX: could leave value on stack top and goto replace_top_a; */
  66204. duk__vm_arith_binary_op(thr, DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins), DUK_DEC_A(ins), op);
  66205. break;
  66206. }
  66207. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66208. case DUK_OP_SUB_RR: {
  66209. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_SUB);
  66210. break;
  66211. }
  66212. case DUK_OP_SUB_CR: {
  66213. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_SUB);
  66214. break;
  66215. }
  66216. case DUK_OP_SUB_RC: {
  66217. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_SUB);
  66218. break;
  66219. }
  66220. case DUK_OP_SUB_CC: {
  66221. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_SUB);
  66222. break;
  66223. }
  66224. case DUK_OP_MUL_RR: {
  66225. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_MUL);
  66226. break;
  66227. }
  66228. case DUK_OP_MUL_CR: {
  66229. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_MUL);
  66230. break;
  66231. }
  66232. case DUK_OP_MUL_RC: {
  66233. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_MUL);
  66234. break;
  66235. }
  66236. case DUK_OP_MUL_CC: {
  66237. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_MUL);
  66238. break;
  66239. }
  66240. case DUK_OP_DIV_RR: {
  66241. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_DIV);
  66242. break;
  66243. }
  66244. case DUK_OP_DIV_CR: {
  66245. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_DIV);
  66246. break;
  66247. }
  66248. case DUK_OP_DIV_RC: {
  66249. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_DIV);
  66250. break;
  66251. }
  66252. case DUK_OP_DIV_CC: {
  66253. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_DIV);
  66254. break;
  66255. }
  66256. case DUK_OP_MOD_RR: {
  66257. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_MOD);
  66258. break;
  66259. }
  66260. case DUK_OP_MOD_CR: {
  66261. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_MOD);
  66262. break;
  66263. }
  66264. case DUK_OP_MOD_RC: {
  66265. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_MOD);
  66266. break;
  66267. }
  66268. case DUK_OP_MOD_CC: {
  66269. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_MOD);
  66270. break;
  66271. }
  66272. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  66273. case DUK_OP_EXP_RR: {
  66274. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_EXP);
  66275. break;
  66276. }
  66277. case DUK_OP_EXP_CR: {
  66278. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_EXP);
  66279. break;
  66280. }
  66281. case DUK_OP_EXP_RC: {
  66282. duk__vm_arith_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_EXP);
  66283. break;
  66284. }
  66285. case DUK_OP_EXP_CC: {
  66286. duk__vm_arith_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_EXP);
  66287. break;
  66288. }
  66289. #endif /* DUK_USE_ES7_EXP_OPERATOR */
  66290. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66291. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66292. case DUK_OP_BAND_RR:
  66293. case DUK_OP_BAND_CR:
  66294. case DUK_OP_BAND_RC:
  66295. case DUK_OP_BAND_CC:
  66296. case DUK_OP_BOR_RR:
  66297. case DUK_OP_BOR_CR:
  66298. case DUK_OP_BOR_RC:
  66299. case DUK_OP_BOR_CC:
  66300. case DUK_OP_BXOR_RR:
  66301. case DUK_OP_BXOR_CR:
  66302. case DUK_OP_BXOR_RC:
  66303. case DUK_OP_BXOR_CC:
  66304. case DUK_OP_BASL_RR:
  66305. case DUK_OP_BASL_CR:
  66306. case DUK_OP_BASL_RC:
  66307. case DUK_OP_BASL_CC:
  66308. case DUK_OP_BLSR_RR:
  66309. case DUK_OP_BLSR_CR:
  66310. case DUK_OP_BLSR_RC:
  66311. case DUK_OP_BLSR_CC:
  66312. case DUK_OP_BASR_RR:
  66313. case DUK_OP_BASR_CR:
  66314. case DUK_OP_BASR_RC:
  66315. case DUK_OP_BASR_CC: {
  66316. /* XXX: could leave value on stack top and goto replace_top_a; */
  66317. duk__vm_bitwise_binary_op(thr, DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins), DUK_DEC_A(ins), op);
  66318. break;
  66319. }
  66320. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66321. case DUK_OP_BAND_RR: {
  66322. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BAND);
  66323. break;
  66324. }
  66325. case DUK_OP_BAND_CR: {
  66326. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BAND);
  66327. break;
  66328. }
  66329. case DUK_OP_BAND_RC: {
  66330. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BAND);
  66331. break;
  66332. }
  66333. case DUK_OP_BAND_CC: {
  66334. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BAND);
  66335. break;
  66336. }
  66337. case DUK_OP_BOR_RR: {
  66338. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BOR);
  66339. break;
  66340. }
  66341. case DUK_OP_BOR_CR: {
  66342. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BOR);
  66343. break;
  66344. }
  66345. case DUK_OP_BOR_RC: {
  66346. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BOR);
  66347. break;
  66348. }
  66349. case DUK_OP_BOR_CC: {
  66350. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BOR);
  66351. break;
  66352. }
  66353. case DUK_OP_BXOR_RR: {
  66354. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BXOR);
  66355. break;
  66356. }
  66357. case DUK_OP_BXOR_CR: {
  66358. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BXOR);
  66359. break;
  66360. }
  66361. case DUK_OP_BXOR_RC: {
  66362. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BXOR);
  66363. break;
  66364. }
  66365. case DUK_OP_BXOR_CC: {
  66366. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BXOR);
  66367. break;
  66368. }
  66369. case DUK_OP_BASL_RR: {
  66370. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BASL);
  66371. break;
  66372. }
  66373. case DUK_OP_BASL_CR: {
  66374. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BASL);
  66375. break;
  66376. }
  66377. case DUK_OP_BASL_RC: {
  66378. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BASL);
  66379. break;
  66380. }
  66381. case DUK_OP_BASL_CC: {
  66382. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BASL);
  66383. break;
  66384. }
  66385. case DUK_OP_BLSR_RR: {
  66386. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BLSR);
  66387. break;
  66388. }
  66389. case DUK_OP_BLSR_CR: {
  66390. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BLSR);
  66391. break;
  66392. }
  66393. case DUK_OP_BLSR_RC: {
  66394. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BLSR);
  66395. break;
  66396. }
  66397. case DUK_OP_BLSR_CC: {
  66398. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BLSR);
  66399. break;
  66400. }
  66401. case DUK_OP_BASR_RR: {
  66402. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BASR);
  66403. break;
  66404. }
  66405. case DUK_OP_BASR_CR: {
  66406. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__REGP_C(ins), DUK_DEC_A(ins), DUK_OP_BASR);
  66407. break;
  66408. }
  66409. case DUK_OP_BASR_RC: {
  66410. duk__vm_bitwise_binary_op(thr, DUK__REGP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BASR);
  66411. break;
  66412. }
  66413. case DUK_OP_BASR_CC: {
  66414. duk__vm_bitwise_binary_op(thr, DUK__CONSTP_B(ins), DUK__CONSTP_C(ins), DUK_DEC_A(ins), DUK_OP_BASR);
  66415. break;
  66416. }
  66417. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66418. /* For INSTOF and IN, B is always a register. */
  66419. #define DUK__INSTOF_BODY(barg,carg) { \
  66420. duk_bool_t tmp; \
  66421. tmp = duk_js_instanceof(thr, (barg), (carg)); \
  66422. DUK_ASSERT(tmp == 0 || tmp == 1); \
  66423. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  66424. }
  66425. #define DUK__IN_BODY(barg,carg) { \
  66426. duk_bool_t tmp; \
  66427. tmp = duk_js_in(thr, (barg), (carg)); \
  66428. DUK_ASSERT(tmp == 0 || tmp == 1); \
  66429. DUK__REPLACE_BOOL_A_BREAK(tmp); \
  66430. }
  66431. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66432. case DUK_OP_INSTOF_RR:
  66433. case DUK_OP_INSTOF_CR:
  66434. case DUK_OP_INSTOF_RC:
  66435. case DUK_OP_INSTOF_CC:
  66436. DUK__INSTOF_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66437. case DUK_OP_IN_RR:
  66438. case DUK_OP_IN_CR:
  66439. case DUK_OP_IN_RC:
  66440. case DUK_OP_IN_CC:
  66441. DUK__IN_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66442. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66443. case DUK_OP_INSTOF_RR:
  66444. DUK__INSTOF_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66445. case DUK_OP_INSTOF_CR:
  66446. DUK__INSTOF_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66447. case DUK_OP_INSTOF_RC:
  66448. DUK__INSTOF_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66449. case DUK_OP_INSTOF_CC:
  66450. DUK__INSTOF_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66451. case DUK_OP_IN_RR:
  66452. DUK__IN_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66453. case DUK_OP_IN_CR:
  66454. DUK__IN_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66455. case DUK_OP_IN_RC:
  66456. DUK__IN_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66457. case DUK_OP_IN_CC:
  66458. DUK__IN_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66459. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66460. /* Pre/post inc/dec for register variables, important for loops. */
  66461. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66462. case DUK_OP_PREINCR:
  66463. case DUK_OP_PREDECR:
  66464. case DUK_OP_POSTINCR:
  66465. case DUK_OP_POSTDECR: {
  66466. duk__prepost_incdec_reg_helper(thr, DUK__REGP_A(ins), DUK__REGP_BC(ins), op);
  66467. break;
  66468. }
  66469. case DUK_OP_PREINCV:
  66470. case DUK_OP_PREDECV:
  66471. case DUK_OP_POSTINCV:
  66472. case DUK_OP_POSTDECV: {
  66473. duk__prepost_incdec_var_helper(thr, DUK_DEC_A(ins), DUK__CONSTP_BC(ins), op, DUK__STRICT());
  66474. break;
  66475. }
  66476. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66477. case DUK_OP_PREINCR: {
  66478. duk__prepost_incdec_reg_helper(thr, DUK__REGP_A(ins), DUK__REGP_BC(ins), DUK_OP_PREINCR);
  66479. break;
  66480. }
  66481. case DUK_OP_PREDECR: {
  66482. duk__prepost_incdec_reg_helper(thr, DUK__REGP_A(ins), DUK__REGP_BC(ins), DUK_OP_PREDECR);
  66483. break;
  66484. }
  66485. case DUK_OP_POSTINCR: {
  66486. duk__prepost_incdec_reg_helper(thr, DUK__REGP_A(ins), DUK__REGP_BC(ins), DUK_OP_POSTINCR);
  66487. break;
  66488. }
  66489. case DUK_OP_POSTDECR: {
  66490. duk__prepost_incdec_reg_helper(thr, DUK__REGP_A(ins), DUK__REGP_BC(ins), DUK_OP_POSTDECR);
  66491. break;
  66492. }
  66493. case DUK_OP_PREINCV: {
  66494. duk__prepost_incdec_var_helper(thr, DUK_DEC_A(ins), DUK__CONSTP_BC(ins), DUK_OP_PREINCV, DUK__STRICT());
  66495. break;
  66496. }
  66497. case DUK_OP_PREDECV: {
  66498. duk__prepost_incdec_var_helper(thr, DUK_DEC_A(ins), DUK__CONSTP_BC(ins), DUK_OP_PREDECV, DUK__STRICT());
  66499. break;
  66500. }
  66501. case DUK_OP_POSTINCV: {
  66502. duk__prepost_incdec_var_helper(thr, DUK_DEC_A(ins), DUK__CONSTP_BC(ins), DUK_OP_POSTINCV, DUK__STRICT());
  66503. break;
  66504. }
  66505. case DUK_OP_POSTDECV: {
  66506. duk__prepost_incdec_var_helper(thr, DUK_DEC_A(ins), DUK__CONSTP_BC(ins), DUK_OP_POSTDECV, DUK__STRICT());
  66507. break;
  66508. }
  66509. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66510. /* XXX: Move to separate helper, optimize for perf/size separately. */
  66511. /* Preinc/predec for object properties. */
  66512. case DUK_OP_PREINCP_RR:
  66513. case DUK_OP_PREINCP_CR:
  66514. case DUK_OP_PREINCP_RC:
  66515. case DUK_OP_PREINCP_CC:
  66516. case DUK_OP_PREDECP_RR:
  66517. case DUK_OP_PREDECP_CR:
  66518. case DUK_OP_PREDECP_RC:
  66519. case DUK_OP_PREDECP_CC:
  66520. case DUK_OP_POSTINCP_RR:
  66521. case DUK_OP_POSTINCP_CR:
  66522. case DUK_OP_POSTINCP_RC:
  66523. case DUK_OP_POSTINCP_CC:
  66524. case DUK_OP_POSTDECP_RR:
  66525. case DUK_OP_POSTDECP_CR:
  66526. case DUK_OP_POSTDECP_RC:
  66527. case DUK_OP_POSTDECP_CC: {
  66528. duk_tval *tv_obj;
  66529. duk_tval *tv_key;
  66530. duk_tval *tv_val;
  66531. duk_bool_t rc;
  66532. duk_double_t x, y, z;
  66533. #if !defined(DUK_USE_EXEC_PREFER_SIZE)
  66534. duk_tval *tv_dst;
  66535. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66536. /* A -> target reg
  66537. * B -> object reg/const (may be const e.g. in "'foo'[1]")
  66538. * C -> key reg/const
  66539. */
  66540. /* Opcode bits 0-1 are used to distinguish reg/const variants.
  66541. * Opcode bits 2-3 are used to distinguish inc/dec variants:
  66542. * Bit 2 = inc(0)/dec(1), bit 3 = pre(0)/post(1).
  66543. */
  66544. DUK_ASSERT((DUK_OP_PREINCP_RR & 0x0c) == 0x00);
  66545. DUK_ASSERT((DUK_OP_PREDECP_RR & 0x0c) == 0x04);
  66546. DUK_ASSERT((DUK_OP_POSTINCP_RR & 0x0c) == 0x08);
  66547. DUK_ASSERT((DUK_OP_POSTDECP_RR & 0x0c) == 0x0c);
  66548. tv_obj = DUK__REGCONSTP_B(ins);
  66549. tv_key = DUK__REGCONSTP_C(ins);
  66550. rc = duk_hobject_getprop(thr, tv_obj, tv_key); /* -> [val] */
  66551. DUK_UNREF(rc); /* ignore */
  66552. tv_obj = NULL; /* invalidated */
  66553. tv_key = NULL; /* invalidated */
  66554. /* XXX: Fastint fast path would be useful here. Also fastints
  66555. * now lose their fastint status in current handling which is
  66556. * not intuitive.
  66557. */
  66558. x = duk_to_number_m1(thr);
  66559. duk_pop_unsafe(thr);
  66560. if (ins & DUK_BC_INCDECP_FLAG_DEC) {
  66561. y = x - 1.0;
  66562. } else {
  66563. y = x + 1.0;
  66564. }
  66565. duk_push_number(thr, y);
  66566. tv_val = DUK_GET_TVAL_NEGIDX(thr, -1);
  66567. DUK_ASSERT(tv_val != NULL);
  66568. tv_obj = DUK__REGCONSTP_B(ins);
  66569. tv_key = DUK__REGCONSTP_C(ins);
  66570. rc = duk_hobject_putprop(thr, tv_obj, tv_key, tv_val, DUK__STRICT());
  66571. DUK_UNREF(rc); /* ignore */
  66572. tv_obj = NULL; /* invalidated */
  66573. tv_key = NULL; /* invalidated */
  66574. duk_pop_unsafe(thr);
  66575. z = (ins & DUK_BC_INCDECP_FLAG_POST) ? x : y;
  66576. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66577. duk_push_number(thr, z);
  66578. DUK__REPLACE_TOP_A_BREAK();
  66579. #else
  66580. tv_dst = DUK__REGP_A(ins);
  66581. DUK_TVAL_SET_NUMBER_UPDREF(thr, tv_dst, z);
  66582. break;
  66583. #endif
  66584. }
  66585. /* XXX: GETPROP where object is 'this', GETPROPT?
  66586. * Occurs relatively often in object oriented code.
  66587. */
  66588. #define DUK__GETPROP_BODY(barg,carg) { \
  66589. /* A -> target reg \
  66590. * B -> object reg/const (may be const e.g. in "'foo'[1]") \
  66591. * C -> key reg/const \
  66592. */ \
  66593. (void) duk_hobject_getprop(thr, (barg), (carg)); \
  66594. DUK__REPLACE_TOP_A_BREAK(); \
  66595. }
  66596. #define DUK__GETPROPC_BODY(barg,carg) { \
  66597. /* Same as GETPROP but callability check for property-based calls. */ \
  66598. duk_tval *tv__targ; \
  66599. (void) duk_hobject_getprop(thr, (barg), (carg)); \
  66600. DUK_GC_TORTURE(thr->heap); \
  66601. tv__targ = DUK_GET_TVAL_NEGIDX(thr, -1); \
  66602. if (DUK_UNLIKELY(!duk_is_callable_tval(thr, tv__targ))) { \
  66603. /* Here we intentionally re-evaluate the macro \
  66604. * arguments to deal with potentially changed \
  66605. * valstack base pointer! \
  66606. */ \
  66607. duk_call_setup_propcall_error(thr, tv__targ, (barg), (carg)); \
  66608. } \
  66609. DUK__REPLACE_TOP_A_BREAK(); \
  66610. }
  66611. #define DUK__PUTPROP_BODY(aarg,barg,carg) { \
  66612. /* A -> object reg \
  66613. * B -> key reg/const \
  66614. * C -> value reg/const \
  66615. * \
  66616. * Note: intentional difference to register arrangement \
  66617. * of e.g. GETPROP; 'A' must contain a register-only value. \
  66618. */ \
  66619. (void) duk_hobject_putprop(thr, (aarg), (barg), (carg), DUK__STRICT()); \
  66620. break; \
  66621. }
  66622. #define DUK__DELPROP_BODY(barg,carg) { \
  66623. /* A -> result reg \
  66624. * B -> object reg \
  66625. * C -> key reg/const \
  66626. */ \
  66627. duk_bool_t rc; \
  66628. rc = duk_hobject_delprop(thr, (barg), (carg), DUK__STRICT()); \
  66629. DUK_ASSERT(rc == 0 || rc == 1); \
  66630. DUK__REPLACE_BOOL_A_BREAK(rc); \
  66631. }
  66632. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66633. case DUK_OP_GETPROP_RR:
  66634. case DUK_OP_GETPROP_CR:
  66635. case DUK_OP_GETPROP_RC:
  66636. case DUK_OP_GETPROP_CC:
  66637. DUK__GETPROP_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66638. #if defined(DUK_USE_VERBOSE_ERRORS)
  66639. case DUK_OP_GETPROPC_RR:
  66640. case DUK_OP_GETPROPC_CR:
  66641. case DUK_OP_GETPROPC_RC:
  66642. case DUK_OP_GETPROPC_CC:
  66643. DUK__GETPROPC_BODY(DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66644. #endif
  66645. case DUK_OP_PUTPROP_RR:
  66646. case DUK_OP_PUTPROP_CR:
  66647. case DUK_OP_PUTPROP_RC:
  66648. case DUK_OP_PUTPROP_CC:
  66649. DUK__PUTPROP_BODY(DUK__REGP_A(ins), DUK__REGCONSTP_B(ins), DUK__REGCONSTP_C(ins));
  66650. case DUK_OP_DELPROP_RR:
  66651. case DUK_OP_DELPROP_RC: /* B is always reg */
  66652. DUK__DELPROP_BODY(DUK__REGP_B(ins), DUK__REGCONSTP_C(ins));
  66653. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66654. case DUK_OP_GETPROP_RR:
  66655. DUK__GETPROP_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66656. case DUK_OP_GETPROP_CR:
  66657. DUK__GETPROP_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66658. case DUK_OP_GETPROP_RC:
  66659. DUK__GETPROP_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66660. case DUK_OP_GETPROP_CC:
  66661. DUK__GETPROP_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66662. #if defined(DUK_USE_VERBOSE_ERRORS)
  66663. case DUK_OP_GETPROPC_RR:
  66664. DUK__GETPROPC_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66665. case DUK_OP_GETPROPC_CR:
  66666. DUK__GETPROPC_BODY(DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66667. case DUK_OP_GETPROPC_RC:
  66668. DUK__GETPROPC_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66669. case DUK_OP_GETPROPC_CC:
  66670. DUK__GETPROPC_BODY(DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66671. #endif
  66672. case DUK_OP_PUTPROP_RR:
  66673. DUK__PUTPROP_BODY(DUK__REGP_A(ins), DUK__REGP_B(ins), DUK__REGP_C(ins));
  66674. case DUK_OP_PUTPROP_CR:
  66675. DUK__PUTPROP_BODY(DUK__REGP_A(ins), DUK__CONSTP_B(ins), DUK__REGP_C(ins));
  66676. case DUK_OP_PUTPROP_RC:
  66677. DUK__PUTPROP_BODY(DUK__REGP_A(ins), DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66678. case DUK_OP_PUTPROP_CC:
  66679. DUK__PUTPROP_BODY(DUK__REGP_A(ins), DUK__CONSTP_B(ins), DUK__CONSTP_C(ins));
  66680. case DUK_OP_DELPROP_RR: /* B is always reg */
  66681. DUK__DELPROP_BODY(DUK__REGP_B(ins), DUK__REGP_C(ins));
  66682. case DUK_OP_DELPROP_RC:
  66683. DUK__DELPROP_BODY(DUK__REGP_B(ins), DUK__CONSTP_C(ins));
  66684. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66685. /* No fast path for DECLVAR now, it's quite a rare instruction. */
  66686. case DUK_OP_DECLVAR_RR:
  66687. case DUK_OP_DECLVAR_CR:
  66688. case DUK_OP_DECLVAR_RC:
  66689. case DUK_OP_DECLVAR_CC: {
  66690. duk_activation *act;
  66691. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  66692. duk_tval *tv1;
  66693. duk_hstring *name;
  66694. duk_small_uint_t prop_flags;
  66695. duk_bool_t is_func_decl;
  66696. tv1 = DUK__REGCONSTP_B(ins);
  66697. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  66698. name = DUK_TVAL_GET_STRING(tv1);
  66699. DUK_ASSERT(name != NULL);
  66700. is_func_decl = ((a & DUK_BC_DECLVAR_FLAG_FUNC_DECL) != 0);
  66701. /* XXX: declvar takes an duk_tval pointer, which is awkward and
  66702. * should be reworked.
  66703. */
  66704. /* Compiler is responsible for selecting property flags (configurability,
  66705. * writability, etc).
  66706. */
  66707. prop_flags = a & DUK_PROPDESC_FLAGS_MASK;
  66708. if (is_func_decl) {
  66709. duk_push_tval(thr, DUK__REGCONSTP_C(ins));
  66710. } else {
  66711. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* valstack policy */
  66712. thr->valstack_top++;
  66713. }
  66714. tv1 = DUK_GET_TVAL_NEGIDX(thr, -1);
  66715. act = thr->callstack_curr;
  66716. if (duk_js_declvar_activation(thr, act, name, tv1, prop_flags, is_func_decl)) {
  66717. if (is_func_decl) {
  66718. /* Already declared, update value. */
  66719. tv1 = DUK_GET_TVAL_NEGIDX(thr, -1);
  66720. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  66721. } else {
  66722. /* Already declared but no initializer value
  66723. * (e.g. 'var xyz;'), no-op.
  66724. */
  66725. }
  66726. }
  66727. duk_pop_unsafe(thr);
  66728. break;
  66729. }
  66730. #if defined(DUK_USE_REGEXP_SUPPORT)
  66731. /* The compiler should never emit DUK_OP_REGEXP if there is no
  66732. * regexp support.
  66733. */
  66734. case DUK_OP_REGEXP_RR:
  66735. case DUK_OP_REGEXP_CR:
  66736. case DUK_OP_REGEXP_RC:
  66737. case DUK_OP_REGEXP_CC: {
  66738. /* A -> target register
  66739. * B -> bytecode (also contains flags)
  66740. * C -> escaped source
  66741. */
  66742. duk_push_tval(thr, DUK__REGCONSTP_C(ins));
  66743. duk_push_tval(thr, DUK__REGCONSTP_B(ins)); /* -> [ ... escaped_source bytecode ] */
  66744. duk_regexp_create_instance(thr); /* -> [ ... regexp_instance ] */
  66745. DUK__REPLACE_TOP_A_BREAK();
  66746. }
  66747. #endif /* DUK_USE_REGEXP_SUPPORT */
  66748. /* XXX: 'c' is unused, use whole BC, etc. */
  66749. case DUK_OP_CSVAR_RR:
  66750. case DUK_OP_CSVAR_CR:
  66751. case DUK_OP_CSVAR_RC:
  66752. case DUK_OP_CSVAR_CC: {
  66753. /* The speciality of calling through a variable binding is that the
  66754. * 'this' value may be provided by the variable lookup: E5 Section 6.b.i.
  66755. *
  66756. * The only (standard) case where the 'this' binding is non-null is when
  66757. * (1) the variable is found in an object environment record, and
  66758. * (2) that object environment record is a 'with' block.
  66759. */
  66760. duk_activation *act;
  66761. duk_uint_fast_t idx;
  66762. duk_tval *tv1;
  66763. duk_hstring *name;
  66764. /* A -> target registers (A, A + 1) for call setup
  66765. * B -> identifier name, usually constant but can be a register due to shuffling
  66766. */
  66767. tv1 = DUK__REGCONSTP_B(ins);
  66768. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  66769. name = DUK_TVAL_GET_STRING(tv1);
  66770. DUK_ASSERT(name != NULL);
  66771. act = thr->callstack_curr;
  66772. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  66773. idx = (duk_uint_fast_t) DUK_DEC_A(ins);
  66774. /* Could add direct value stack handling. */
  66775. duk_replace(thr, (duk_idx_t) (idx + 1)); /* 'this' binding */
  66776. duk_replace(thr, (duk_idx_t) idx); /* variable value (function, we hope, not checked here) */
  66777. break;
  66778. }
  66779. case DUK_OP_CLOSURE: {
  66780. duk_activation *act;
  66781. duk_hcompfunc *fun_act;
  66782. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  66783. duk_hobject *fun_temp;
  66784. /* A -> target reg
  66785. * BC -> inner function index
  66786. */
  66787. DUK_DDD(DUK_DDDPRINT("CLOSURE to target register %ld, fnum %ld (count %ld)",
  66788. (long) DUK_DEC_A(ins), (long) DUK_DEC_BC(ins), (long) DUK_HCOMPFUNC_GET_FUNCS_COUNT(thr->heap, DUK__FUN())));
  66789. DUK_ASSERT_DISABLE(bc >= 0); /* unsigned */
  66790. DUK_ASSERT((duk_uint_t) bc < (duk_uint_t) DUK_HCOMPFUNC_GET_FUNCS_COUNT(thr->heap, DUK__FUN()));
  66791. act = thr->callstack_curr;
  66792. fun_act = (duk_hcompfunc *) DUK_ACT_GET_FUNC(act);
  66793. fun_temp = DUK_HCOMPFUNC_GET_FUNCS_BASE(thr->heap, fun_act)[bc];
  66794. DUK_ASSERT(fun_temp != NULL);
  66795. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC(fun_temp));
  66796. DUK_DDD(DUK_DDDPRINT("CLOSURE: function template is: %p -> %!O",
  66797. (void *) fun_temp, (duk_heaphdr *) fun_temp));
  66798. if (act->lex_env == NULL) {
  66799. DUK_ASSERT(act->var_env == NULL);
  66800. duk_js_init_activation_environment_records_delayed(thr, act);
  66801. act = thr->callstack_curr;
  66802. }
  66803. DUK_ASSERT(act->lex_env != NULL);
  66804. DUK_ASSERT(act->var_env != NULL);
  66805. /* functions always have a NEWENV flag, i.e. they get a
  66806. * new variable declaration environment, so only lex_env
  66807. * matters here.
  66808. */
  66809. duk_js_push_closure(thr,
  66810. (duk_hcompfunc *) fun_temp,
  66811. act->var_env,
  66812. act->lex_env,
  66813. 1 /*add_auto_proto*/);
  66814. DUK__REPLACE_TOP_A_BREAK();
  66815. }
  66816. case DUK_OP_GETVAR: {
  66817. duk_activation *act;
  66818. duk_tval *tv1;
  66819. duk_hstring *name;
  66820. tv1 = DUK__CONSTP_BC(ins);
  66821. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  66822. name = DUK_TVAL_GET_STRING(tv1);
  66823. DUK_ASSERT(name != NULL);
  66824. act = thr->callstack_curr;
  66825. DUK_ASSERT(act != NULL);
  66826. (void) duk_js_getvar_activation(thr, act, name, 1 /*throw*/); /* -> [... val this] */
  66827. duk_pop_unsafe(thr); /* 'this' binding is not needed here */
  66828. DUK__REPLACE_TOP_A_BREAK();
  66829. }
  66830. case DUK_OP_PUTVAR: {
  66831. duk_activation *act;
  66832. duk_tval *tv1;
  66833. duk_hstring *name;
  66834. tv1 = DUK__CONSTP_BC(ins);
  66835. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  66836. name = DUK_TVAL_GET_STRING(tv1);
  66837. DUK_ASSERT(name != NULL);
  66838. /* XXX: putvar takes a duk_tval pointer, which is awkward and
  66839. * should be reworked.
  66840. */
  66841. tv1 = DUK__REGP_A(ins); /* val */
  66842. act = thr->callstack_curr;
  66843. duk_js_putvar_activation(thr, act, name, tv1, DUK__STRICT());
  66844. break;
  66845. }
  66846. case DUK_OP_DELVAR: {
  66847. duk_activation *act;
  66848. duk_tval *tv1;
  66849. duk_hstring *name;
  66850. duk_bool_t rc;
  66851. tv1 = DUK__CONSTP_BC(ins);
  66852. DUK_ASSERT(DUK_TVAL_IS_STRING(tv1));
  66853. name = DUK_TVAL_GET_STRING(tv1);
  66854. DUK_ASSERT(name != NULL);
  66855. act = thr->callstack_curr;
  66856. rc = duk_js_delvar_activation(thr, act, name);
  66857. DUK__REPLACE_BOOL_A_BREAK(rc);
  66858. }
  66859. case DUK_OP_JUMP: {
  66860. /* Note: without explicit cast to signed, MSVC will
  66861. * apparently generate a large positive jump when the
  66862. * bias-corrected value would normally be negative.
  66863. */
  66864. curr_pc += (duk_int_fast_t) DUK_DEC_ABC(ins) - (duk_int_fast_t) DUK_BC_JUMP_BIAS;
  66865. break;
  66866. }
  66867. #define DUK__RETURN_SHARED() do { \
  66868. duk_small_uint_t ret_result; \
  66869. /* duk__handle_return() is guaranteed never to throw, except \
  66870. * for potential out-of-memory situations which will then \
  66871. * propagate out of the executor longjmp handler. \
  66872. */ \
  66873. DUK_ASSERT(thr->ptr_curr_pc == NULL); \
  66874. ret_result = duk__handle_return(thr, entry_act); \
  66875. if (ret_result == DUK__RETHAND_RESTART) { \
  66876. goto restart_execution; \
  66877. } \
  66878. DUK_ASSERT(ret_result == DUK__RETHAND_FINISHED); \
  66879. return; \
  66880. } while (0)
  66881. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  66882. case DUK_OP_RETREG:
  66883. case DUK_OP_RETCONST:
  66884. case DUK_OP_RETCONSTN:
  66885. case DUK_OP_RETUNDEF: {
  66886. /* BC -> return value reg/const */
  66887. DUK__SYNC_AND_NULL_CURR_PC();
  66888. if (op == DUK_OP_RETREG) {
  66889. duk_push_tval(thr, DUK__REGP_BC(ins));
  66890. } else if (op == DUK_OP_RETUNDEF) {
  66891. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top)); /* valstack policy */
  66892. thr->valstack_top++;
  66893. } else {
  66894. DUK_ASSERT(op == DUK_OP_RETCONST || op == DUK_OP_RETCONSTN);
  66895. duk_push_tval(thr, DUK__CONSTP_BC(ins));
  66896. }
  66897. DUK__RETURN_SHARED();
  66898. }
  66899. #else /* DUK_USE_EXEC_PREFER_SIZE */
  66900. case DUK_OP_RETREG: {
  66901. duk_tval *tv;
  66902. DUK__SYNC_AND_NULL_CURR_PC();
  66903. tv = DUK__REGP_BC(ins);
  66904. DUK_TVAL_SET_TVAL(thr->valstack_top, tv);
  66905. DUK_TVAL_INCREF(thr, tv);
  66906. thr->valstack_top++;
  66907. DUK__RETURN_SHARED();
  66908. }
  66909. /* This will be unused without refcounting. */
  66910. case DUK_OP_RETCONST: {
  66911. duk_tval *tv;
  66912. DUK__SYNC_AND_NULL_CURR_PC();
  66913. tv = DUK__CONSTP_BC(ins);
  66914. DUK_TVAL_SET_TVAL(thr->valstack_top, tv);
  66915. DUK_TVAL_INCREF(thr, tv);
  66916. thr->valstack_top++;
  66917. DUK__RETURN_SHARED();
  66918. }
  66919. case DUK_OP_RETCONSTN: {
  66920. duk_tval *tv;
  66921. DUK__SYNC_AND_NULL_CURR_PC();
  66922. tv = DUK__CONSTP_BC(ins);
  66923. DUK_TVAL_SET_TVAL(thr->valstack_top, tv);
  66924. #if defined(DUK_USE_REFERENCE_COUNTING)
  66925. /* Without refcounting only RETCONSTN is used. */
  66926. DUK_ASSERT(!DUK_TVAL_IS_HEAP_ALLOCATED(tv)); /* no INCREF for this constant */
  66927. #endif
  66928. thr->valstack_top++;
  66929. DUK__RETURN_SHARED();
  66930. }
  66931. case DUK_OP_RETUNDEF: {
  66932. DUK__SYNC_AND_NULL_CURR_PC();
  66933. thr->valstack_top++; /* value at valstack top is already undefined by valstack policy */
  66934. DUK_ASSERT(DUK_TVAL_IS_UNDEFINED(thr->valstack_top));
  66935. DUK__RETURN_SHARED();
  66936. }
  66937. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  66938. case DUK_OP_LABEL: {
  66939. duk_activation *act;
  66940. duk_catcher *cat;
  66941. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  66942. /* Allocate catcher and populate it (must be atomic). */
  66943. cat = duk_hthread_catcher_alloc(thr);
  66944. DUK_ASSERT(cat != NULL);
  66945. cat->flags = (duk_uint32_t) (DUK_CAT_TYPE_LABEL | (bc << DUK_CAT_LABEL_SHIFT));
  66946. cat->pc_base = (duk_instr_t *) curr_pc; /* pre-incremented, points to first jump slot */
  66947. cat->idx_base = 0; /* unused for label */
  66948. cat->h_varname = NULL;
  66949. act = thr->callstack_curr;
  66950. DUK_ASSERT(act != NULL);
  66951. cat->parent = act->cat;
  66952. act->cat = cat;
  66953. DUK_DDD(DUK_DDDPRINT("LABEL catcher: flags=0x%08lx, pc_base=%ld, "
  66954. "idx_base=%ld, h_varname=%!O, label_id=%ld",
  66955. (long) cat->flags, (long) cat->pc_base,
  66956. (long) cat->idx_base, (duk_heaphdr *) cat->h_varname, (long) DUK_CAT_GET_LABEL(cat)));
  66957. curr_pc += 2; /* skip jump slots */
  66958. break;
  66959. }
  66960. case DUK_OP_ENDLABEL: {
  66961. duk_activation *act;
  66962. #if (defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)) || defined(DUK_USE_ASSERTIONS)
  66963. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  66964. #endif
  66965. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  66966. DUK_DDD(DUK_DDDPRINT("ENDLABEL %ld", (long) bc));
  66967. #endif
  66968. act = thr->callstack_curr;
  66969. DUK_ASSERT(act->cat != NULL);
  66970. DUK_ASSERT(DUK_CAT_GET_TYPE(act->cat) == DUK_CAT_TYPE_LABEL);
  66971. DUK_ASSERT((duk_uint_fast_t) DUK_CAT_GET_LABEL(act->cat) == bc);
  66972. duk_hthread_catcher_unwind_nolexenv_norz(thr, act);
  66973. /* no need to unwind callstack */
  66974. break;
  66975. }
  66976. case DUK_OP_BREAK: {
  66977. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  66978. DUK__SYNC_AND_NULL_CURR_PC();
  66979. duk__handle_break_or_continue(thr, (duk_uint_t) bc, DUK_LJ_TYPE_BREAK);
  66980. goto restart_execution;
  66981. }
  66982. case DUK_OP_CONTINUE: {
  66983. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  66984. DUK__SYNC_AND_NULL_CURR_PC();
  66985. duk__handle_break_or_continue(thr, (duk_uint_t) bc, DUK_LJ_TYPE_CONTINUE);
  66986. goto restart_execution;
  66987. }
  66988. /* XXX: move to helper, too large to be inline here */
  66989. case DUK_OP_TRYCATCH: {
  66990. duk__handle_op_trycatch(thr, ins, curr_pc);
  66991. curr_pc += 2; /* skip jump slots */
  66992. break;
  66993. }
  66994. case DUK_OP_ENDTRY: {
  66995. curr_pc = duk__handle_op_endtry(thr, ins);
  66996. break;
  66997. }
  66998. case DUK_OP_ENDCATCH: {
  66999. duk__handle_op_endcatch(thr, ins);
  67000. break;
  67001. }
  67002. case DUK_OP_ENDFIN: {
  67003. /* Sync and NULL early. */
  67004. DUK__SYNC_AND_NULL_CURR_PC();
  67005. if (duk__handle_op_endfin(thr, ins, entry_act) != 0) {
  67006. return;
  67007. }
  67008. /* Must restart because we NULLed out curr_pc. */
  67009. goto restart_execution;
  67010. }
  67011. case DUK_OP_THROW: {
  67012. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  67013. /* Note: errors are augmented when they are created, not
  67014. * when they are thrown. So, don't augment here, it would
  67015. * break re-throwing for instance.
  67016. */
  67017. /* Sync so that augmentation sees up-to-date activations, NULL
  67018. * thr->ptr_curr_pc so that it's not used if side effects occur
  67019. * in augmentation or longjmp handling.
  67020. */
  67021. DUK__SYNC_AND_NULL_CURR_PC();
  67022. duk_dup(thr, (duk_idx_t) bc);
  67023. DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (before throw augment)",
  67024. (duk_tval *) duk_get_tval(thr, -1)));
  67025. #if defined(DUK_USE_AUGMENT_ERROR_THROW)
  67026. duk_err_augment_error_throw(thr);
  67027. DUK_DDD(DUK_DDDPRINT("THROW ERROR (BYTECODE): %!dT (after throw augment)",
  67028. (duk_tval *) duk_get_tval(thr, -1)));
  67029. #endif
  67030. duk_err_setup_ljstate1(thr, DUK_LJ_TYPE_THROW, DUK_GET_TVAL_NEGIDX(thr, -1));
  67031. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  67032. duk_err_check_debugger_integration(thr);
  67033. #endif
  67034. DUK_ASSERT(thr->heap->lj.jmpbuf_ptr != NULL); /* always in executor */
  67035. duk_err_longjmp(thr);
  67036. DUK_UNREACHABLE();
  67037. break;
  67038. }
  67039. case DUK_OP_CSREG: {
  67040. /*
  67041. * Assuming a register binds to a variable declared within this
  67042. * function (a declarative binding), the 'this' for the call
  67043. * setup is always 'undefined'. E5 Section 10.2.1.1.6.
  67044. */
  67045. duk_small_uint_fast_t a = DUK_DEC_A(ins);
  67046. duk_small_uint_fast_t bc = DUK_DEC_BC(ins);
  67047. /* A -> register containing target function (not type checked here)
  67048. * BC -> target registers (BC, BC + 1) for call setup
  67049. */
  67050. #if defined(DUK_USE_PREFER_SIZE)
  67051. duk_dup(thr, (duk_idx_t) a);
  67052. duk_replace(thr, (duk_idx_t) bc);
  67053. duk_to_undefined(thr, (duk_idx_t) (bc + 1));
  67054. #else
  67055. duk_tval *tv1;
  67056. duk_tval *tv2;
  67057. duk_tval *tv3;
  67058. duk_tval tv_tmp1;
  67059. duk_tval tv_tmp2;
  67060. tv1 = DUK__REGP(bc);
  67061. tv2 = tv1 + 1;
  67062. DUK_TVAL_SET_TVAL(&tv_tmp1, tv1);
  67063. DUK_TVAL_SET_TVAL(&tv_tmp2, tv2);
  67064. tv3 = DUK__REGP(a);
  67065. DUK_TVAL_SET_TVAL(tv1, tv3);
  67066. DUK_TVAL_INCREF(thr, tv1); /* no side effects */
  67067. DUK_TVAL_SET_UNDEFINED(tv2); /* no need for incref */
  67068. DUK_TVAL_DECREF(thr, &tv_tmp1);
  67069. DUK_TVAL_DECREF(thr, &tv_tmp2);
  67070. #endif
  67071. break;
  67072. }
  67073. /* XXX: in some cases it's faster NOT to reuse the value
  67074. * stack but rather copy the arguments on top of the stack
  67075. * (mainly when the calling value stack is large and the value
  67076. * stack resize would be large).
  67077. */
  67078. case DUK_OP_CALL0:
  67079. case DUK_OP_CALL1:
  67080. case DUK_OP_CALL2:
  67081. case DUK_OP_CALL3:
  67082. case DUK_OP_CALL4:
  67083. case DUK_OP_CALL5:
  67084. case DUK_OP_CALL6:
  67085. case DUK_OP_CALL7: {
  67086. /* Opcode packs 4 flag bits: 1 for indirect, 3 map
  67087. * 1:1 to three lowest call handling flags.
  67088. *
  67089. * A -> nargs or register with nargs (indirect)
  67090. * BC -> base register for call (base -> func, base+1 -> this, base+2 -> arg1 ... base+2+N-1 -> argN)
  67091. */
  67092. duk_idx_t nargs;
  67093. duk_idx_t idx;
  67094. duk_small_uint_t call_flags;
  67095. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  67096. duk_hcompfunc *fun;
  67097. #endif
  67098. DUK_ASSERT((DUK_OP_CALL0 & 0x0fU) == 0);
  67099. DUK_ASSERT((ins & DUK_BC_CALL_FLAG_INDIRECT) == 0);
  67100. nargs = (duk_idx_t) DUK_DEC_A(ins);
  67101. call_flags = (ins & 0x07U) | DUK_CALL_FLAG_ALLOW_ECMATOECMA;
  67102. idx = (duk_idx_t) DUK_DEC_BC(ins);
  67103. if (duk__executor_handle_call(thr, idx, nargs, call_flags)) {
  67104. /* curr_pc synced by duk_handle_call_unprotected() */
  67105. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  67106. goto restart_execution;
  67107. }
  67108. DUK_ASSERT(thr->ptr_curr_pc != NULL);
  67109. /* duk_js_call.c is required to restore the stack reserve
  67110. * so we only need to reset the top.
  67111. */
  67112. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  67113. fun = DUK__FUN();
  67114. #endif
  67115. duk_set_top_unsafe(thr, (duk_idx_t) fun->nregs);
  67116. /* No need to reinit setjmp() catchpoint, as call handling
  67117. * will store and restore our state.
  67118. *
  67119. * When debugger is enabled, we need to recheck the activation
  67120. * status after returning. This is now handled by call handling
  67121. * and heap->dbg_force_restart.
  67122. */
  67123. break;
  67124. }
  67125. case DUK_OP_CALL8:
  67126. case DUK_OP_CALL9:
  67127. case DUK_OP_CALL10:
  67128. case DUK_OP_CALL11:
  67129. case DUK_OP_CALL12:
  67130. case DUK_OP_CALL13:
  67131. case DUK_OP_CALL14:
  67132. case DUK_OP_CALL15: {
  67133. /* Indirect variant. */
  67134. duk_uint_fast_t nargs;
  67135. duk_idx_t idx;
  67136. duk_small_uint_t call_flags;
  67137. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  67138. duk_hcompfunc *fun;
  67139. #endif
  67140. DUK_ASSERT((DUK_OP_CALL0 & 0x0fU) == 0);
  67141. DUK_ASSERT((ins & DUK_BC_CALL_FLAG_INDIRECT) != 0);
  67142. nargs = (duk_uint_fast_t) DUK_DEC_A(ins);
  67143. DUK__LOOKUP_INDIRECT(nargs);
  67144. call_flags = (ins & 0x07U) | DUK_CALL_FLAG_ALLOW_ECMATOECMA;
  67145. idx = (duk_idx_t) DUK_DEC_BC(ins);
  67146. if (duk__executor_handle_call(thr, idx, (duk_idx_t) nargs, call_flags)) {
  67147. DUK_ASSERT(thr->ptr_curr_pc == NULL);
  67148. goto restart_execution;
  67149. }
  67150. DUK_ASSERT(thr->ptr_curr_pc != NULL);
  67151. #if !defined(DUK_USE_EXEC_FUN_LOCAL)
  67152. fun = DUK__FUN();
  67153. #endif
  67154. duk_set_top_unsafe(thr, (duk_idx_t) fun->nregs);
  67155. break;
  67156. }
  67157. case DUK_OP_NEWOBJ: {
  67158. duk_push_object(thr);
  67159. #if defined(DUK_USE_ASSERTIONS)
  67160. {
  67161. duk_hobject *h;
  67162. h = duk_require_hobject(thr, -1);
  67163. DUK_ASSERT(DUK_HOBJECT_GET_ESIZE(h) == 0);
  67164. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(h) == 0);
  67165. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(h) == 0);
  67166. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(h) == 0);
  67167. }
  67168. #endif
  67169. #if !defined(DUK_USE_PREFER_SIZE)
  67170. /* XXX: could do a direct props realloc, but need hash size */
  67171. duk_hobject_resize_entrypart(thr, duk_known_hobject(thr, -1), DUK_DEC_A(ins));
  67172. #endif
  67173. DUK__REPLACE_TOP_BC_BREAK();
  67174. }
  67175. case DUK_OP_NEWARR: {
  67176. duk_push_array(thr);
  67177. #if defined(DUK_USE_ASSERTIONS)
  67178. {
  67179. duk_hobject *h;
  67180. h = duk_require_hobject(thr, -1);
  67181. DUK_ASSERT(DUK_HOBJECT_GET_ESIZE(h) == 0);
  67182. DUK_ASSERT(DUK_HOBJECT_GET_ENEXT(h) == 0);
  67183. DUK_ASSERT(DUK_HOBJECT_GET_ASIZE(h) == 0);
  67184. DUK_ASSERT(DUK_HOBJECT_GET_HSIZE(h) == 0);
  67185. DUK_ASSERT(DUK_HOBJECT_HAS_ARRAY_PART(h));
  67186. }
  67187. #endif
  67188. #if !defined(DUK_USE_PREFER_SIZE)
  67189. duk_hobject_realloc_props(thr,
  67190. duk_known_hobject(thr, -1),
  67191. 0 /*new_e_size*/,
  67192. DUK_DEC_A(ins) /*new_a_size*/,
  67193. 0 /*new_h_size*/,
  67194. 0 /*abandon_array*/);
  67195. #if 0
  67196. duk_hobject_resize_arraypart(thr, duk_known_hobject(thr, -1), DUK_DEC_A(ins));
  67197. #endif
  67198. #endif
  67199. DUK__REPLACE_TOP_BC_BREAK();
  67200. }
  67201. case DUK_OP_MPUTOBJ:
  67202. case DUK_OP_MPUTOBJI: {
  67203. duk_idx_t obj_idx;
  67204. duk_uint_fast_t idx, idx_end;
  67205. duk_small_uint_fast_t count;
  67206. /* A -> register of target object
  67207. * B -> first register of key/value pair list
  67208. * or register containing first register number if indirect
  67209. * C -> number of key/value pairs * 2
  67210. * (= number of value stack indices used starting from 'B')
  67211. */
  67212. obj_idx = DUK_DEC_A(ins);
  67213. DUK_ASSERT(duk_is_object(thr, obj_idx));
  67214. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  67215. if (DUK_DEC_OP(ins) == DUK_OP_MPUTOBJI) {
  67216. DUK__LOOKUP_INDIRECT(idx);
  67217. }
  67218. count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
  67219. DUK_ASSERT(count > 0); /* compiler guarantees */
  67220. idx_end = idx + count;
  67221. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  67222. if (DUK_UNLIKELY(idx_end > (duk_uint_fast_t) duk_get_top(thr))) {
  67223. /* XXX: use duk_is_valid_index() instead? */
  67224. /* XXX: improve check; check against nregs, not against top */
  67225. DUK__INTERNAL_ERROR("MPUTOBJ out of bounds");
  67226. }
  67227. #endif
  67228. /* Use 'force' flag to duk_def_prop() to ensure that any
  67229. * inherited properties don't prevent the operation.
  67230. * With ES2015 duplicate properties are allowed, so that we
  67231. * must overwrite any previous data or accessor property.
  67232. *
  67233. * With ES2015 computed property names the literal keys
  67234. * may be arbitrary values and need to be ToPropertyKey()
  67235. * coerced at runtime.
  67236. */
  67237. do {
  67238. /* XXX: faster initialization (direct access or better primitives) */
  67239. duk_dup(thr, (duk_idx_t) idx);
  67240. duk_dup(thr, (duk_idx_t) (idx + 1));
  67241. duk_def_prop(thr, obj_idx, DUK_DEFPROP_HAVE_VALUE |
  67242. DUK_DEFPROP_FORCE |
  67243. DUK_DEFPROP_SET_WRITABLE |
  67244. DUK_DEFPROP_SET_ENUMERABLE |
  67245. DUK_DEFPROP_SET_CONFIGURABLE);
  67246. idx += 2;
  67247. } while (idx < idx_end);
  67248. break;
  67249. }
  67250. case DUK_OP_INITSET:
  67251. case DUK_OP_INITGET: {
  67252. duk__handle_op_initset_initget(thr, ins);
  67253. break;
  67254. }
  67255. case DUK_OP_MPUTARR:
  67256. case DUK_OP_MPUTARRI: {
  67257. duk_idx_t obj_idx;
  67258. duk_uint_fast_t idx, idx_end;
  67259. duk_small_uint_fast_t count;
  67260. duk_tval *tv1;
  67261. duk_uint32_t arr_idx;
  67262. /* A -> register of target object
  67263. * B -> first register of value data (start_index, value1, value2, ..., valueN)
  67264. * or register containing first register number if indirect
  67265. * C -> number of key/value pairs (N)
  67266. */
  67267. obj_idx = DUK_DEC_A(ins);
  67268. DUK_ASSERT(duk_is_object(thr, obj_idx));
  67269. idx = (duk_uint_fast_t) DUK_DEC_B(ins);
  67270. if (DUK_DEC_OP(ins) == DUK_OP_MPUTARRI) {
  67271. DUK__LOOKUP_INDIRECT(idx);
  67272. }
  67273. count = (duk_small_uint_fast_t) DUK_DEC_C(ins);
  67274. DUK_ASSERT(count > 0 + 1); /* compiler guarantees */
  67275. idx_end = idx + count;
  67276. #if defined(DUK_USE_EXEC_INDIRECT_BOUND_CHECK)
  67277. if (idx_end > (duk_uint_fast_t) duk_get_top(thr)) {
  67278. /* XXX: use duk_is_valid_index() instead? */
  67279. /* XXX: improve check; check against nregs, not against top */
  67280. DUK__INTERNAL_ERROR("MPUTARR out of bounds");
  67281. }
  67282. #endif
  67283. tv1 = DUK__REGP(idx);
  67284. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  67285. #if defined(DUK_USE_FASTINT)
  67286. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1));
  67287. arr_idx = (duk_uint32_t) DUK_TVAL_GET_FASTINT_U32(tv1);
  67288. #else
  67289. arr_idx = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
  67290. #endif
  67291. idx++;
  67292. do {
  67293. /* duk_xdef_prop() will define an own property without any array
  67294. * special behaviors. We'll need to set the array length explicitly
  67295. * in the end. For arrays with elisions, the compiler will emit an
  67296. * explicit SETALEN which will update the length.
  67297. */
  67298. /* XXX: because we're dealing with 'own' properties of a fresh array,
  67299. * the array initializer should just ensure that the array has a large
  67300. * enough array part and write the values directly into array part,
  67301. * and finally set 'length' manually in the end (as already happens now).
  67302. */
  67303. duk_dup(thr, (duk_idx_t) idx);
  67304. duk_xdef_prop_index_wec(thr, obj_idx, arr_idx);
  67305. idx++;
  67306. arr_idx++;
  67307. } while (idx < idx_end);
  67308. /* XXX: E5.1 Section 11.1.4 coerces the final length through
  67309. * ToUint32() which is odd but happens now as a side effect of
  67310. * 'arr_idx' type.
  67311. */
  67312. duk_set_length(thr, obj_idx, (duk_size_t) (duk_uarridx_t) arr_idx);
  67313. break;
  67314. }
  67315. case DUK_OP_SETALEN: {
  67316. duk_tval *tv1;
  67317. duk_hobject *h;
  67318. duk_uint32_t len;
  67319. tv1 = DUK__REGP_A(ins);
  67320. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv1));
  67321. h = DUK_TVAL_GET_OBJECT(tv1);
  67322. DUK_ASSERT(DUK_HOBJECT_IS_ARRAY(h));
  67323. tv1 = DUK__REGP_BC(ins);
  67324. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv1));
  67325. #if defined(DUK_USE_FASTINT)
  67326. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv1));
  67327. len = (duk_uint32_t) DUK_TVAL_GET_FASTINT_U32(tv1);
  67328. #else
  67329. len = (duk_uint32_t) DUK_TVAL_GET_NUMBER(tv1);
  67330. #endif
  67331. ((duk_harray *) h)->length = len;
  67332. break;
  67333. }
  67334. case DUK_OP_INITENUM: {
  67335. duk__handle_op_initenum(thr, ins);
  67336. break;
  67337. }
  67338. case DUK_OP_NEXTENUM: {
  67339. curr_pc += duk__handle_op_nextenum(thr, ins);
  67340. break;
  67341. }
  67342. case DUK_OP_INVLHS: {
  67343. DUK_ERROR_REFERENCE(thr, DUK_STR_INVALID_LVALUE);
  67344. DUK_WO_NORETURN(return;);
  67345. break;
  67346. }
  67347. case DUK_OP_DEBUGGER: {
  67348. /* Opcode only emitted by compiler when debugger
  67349. * support is enabled. Ignore it silently without
  67350. * debugger support, in case it has been loaded
  67351. * from precompiled bytecode.
  67352. */
  67353. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  67354. if (duk_debug_is_attached(thr->heap)) {
  67355. DUK_D(DUK_DPRINT("DEBUGGER statement encountered, halt execution"));
  67356. DUK__SYNC_AND_NULL_CURR_PC();
  67357. duk_debug_halt_execution(thr, 1 /*use_prev_pc*/);
  67358. DUK_D(DUK_DPRINT("DEBUGGER statement finished, resume execution"));
  67359. goto restart_execution;
  67360. } else {
  67361. DUK_D(DUK_DPRINT("DEBUGGER statement ignored, debugger not attached"));
  67362. }
  67363. #else
  67364. DUK_D(DUK_DPRINT("DEBUGGER statement ignored, no debugger support"));
  67365. #endif
  67366. break;
  67367. }
  67368. case DUK_OP_NOP: {
  67369. /* Nop, ignored, but ABC fields may carry a value e.g.
  67370. * for indirect opcode handling.
  67371. */
  67372. break;
  67373. }
  67374. case DUK_OP_INVALID: {
  67375. DUK_ERROR_FMT1(thr, DUK_ERR_ERROR, "INVALID opcode (%ld)", (long) DUK_DEC_ABC(ins));
  67376. DUK_WO_NORETURN(return;);
  67377. break;
  67378. }
  67379. #if defined(DUK_USE_ES6)
  67380. case DUK_OP_NEWTARGET: {
  67381. duk_push_new_target(thr);
  67382. DUK__REPLACE_TOP_BC_BREAK();
  67383. }
  67384. #endif /* DUK_USE_ES6 */
  67385. #if !defined(DUK_USE_EXEC_PREFER_SIZE)
  67386. #if !defined(DUK_USE_ES7_EXP_OPERATOR)
  67387. case DUK_OP_EXP_RR:
  67388. case DUK_OP_EXP_CR:
  67389. case DUK_OP_EXP_RC:
  67390. case DUK_OP_EXP_CC:
  67391. #endif
  67392. #if !defined(DUK_USE_ES6)
  67393. case DUK_OP_NEWTARGET:
  67394. #endif
  67395. #if !defined(DUK_USE_VERBOSE_ERRORS)
  67396. case DUK_OP_GETPROPC_RR:
  67397. case DUK_OP_GETPROPC_CR:
  67398. case DUK_OP_GETPROPC_RC:
  67399. case DUK_OP_GETPROPC_CC:
  67400. #endif
  67401. case DUK_OP_UNUSED207:
  67402. case DUK_OP_UNUSED212:
  67403. case DUK_OP_UNUSED213:
  67404. case DUK_OP_UNUSED214:
  67405. case DUK_OP_UNUSED215:
  67406. case DUK_OP_UNUSED216:
  67407. case DUK_OP_UNUSED217:
  67408. case DUK_OP_UNUSED218:
  67409. case DUK_OP_UNUSED219:
  67410. case DUK_OP_UNUSED220:
  67411. case DUK_OP_UNUSED221:
  67412. case DUK_OP_UNUSED222:
  67413. case DUK_OP_UNUSED223:
  67414. case DUK_OP_UNUSED224:
  67415. case DUK_OP_UNUSED225:
  67416. case DUK_OP_UNUSED226:
  67417. case DUK_OP_UNUSED227:
  67418. case DUK_OP_UNUSED228:
  67419. case DUK_OP_UNUSED229:
  67420. case DUK_OP_UNUSED230:
  67421. case DUK_OP_UNUSED231:
  67422. case DUK_OP_UNUSED232:
  67423. case DUK_OP_UNUSED233:
  67424. case DUK_OP_UNUSED234:
  67425. case DUK_OP_UNUSED235:
  67426. case DUK_OP_UNUSED236:
  67427. case DUK_OP_UNUSED237:
  67428. case DUK_OP_UNUSED238:
  67429. case DUK_OP_UNUSED239:
  67430. case DUK_OP_UNUSED240:
  67431. case DUK_OP_UNUSED241:
  67432. case DUK_OP_UNUSED242:
  67433. case DUK_OP_UNUSED243:
  67434. case DUK_OP_UNUSED244:
  67435. case DUK_OP_UNUSED245:
  67436. case DUK_OP_UNUSED246:
  67437. case DUK_OP_UNUSED247:
  67438. case DUK_OP_UNUSED248:
  67439. case DUK_OP_UNUSED249:
  67440. case DUK_OP_UNUSED250:
  67441. case DUK_OP_UNUSED251:
  67442. case DUK_OP_UNUSED252:
  67443. case DUK_OP_UNUSED253:
  67444. case DUK_OP_UNUSED254:
  67445. case DUK_OP_UNUSED255:
  67446. /* Force all case clauses to map to an actual handler
  67447. * so that the compiler can emit a jump without a bounds
  67448. * check: the switch argument is a duk_uint8_t so that
  67449. * the compiler may be able to figure it out. This is
  67450. * a small detail and obviously compiler dependent.
  67451. */
  67452. /* default: clause omitted on purpose */
  67453. #else /* DUK_USE_EXEC_PREFER_SIZE */
  67454. default:
  67455. #endif /* DUK_USE_EXEC_PREFER_SIZE */
  67456. {
  67457. /* Default case catches invalid/unsupported opcodes. */
  67458. DUK_D(DUK_DPRINT("invalid opcode: %ld - %!I", (long) op, ins));
  67459. DUK__INTERNAL_ERROR("invalid opcode");
  67460. break;
  67461. }
  67462. } /* end switch */
  67463. continue;
  67464. /* Some shared exit paths for opcode handling below. These
  67465. * are mostly useful to reduce code footprint when multiple
  67466. * opcodes have a similar epilogue (like replacing stack top
  67467. * with index 'a').
  67468. */
  67469. #if defined(DUK_USE_EXEC_PREFER_SIZE)
  67470. replace_top_a:
  67471. DUK__REPLACE_TO_TVPTR(thr, DUK__REGP_A(ins));
  67472. continue;
  67473. replace_top_bc:
  67474. DUK__REPLACE_TO_TVPTR(thr, DUK__REGP_BC(ins));
  67475. continue;
  67476. #endif
  67477. }
  67478. DUK_WO_NORETURN(return;);
  67479. #if !defined(DUK_USE_VERBOSE_EXECUTOR_ERRORS)
  67480. internal_error:
  67481. DUK_ERROR_INTERNAL(thr);
  67482. DUK_WO_NORETURN(return;);
  67483. #endif
  67484. }
  67485. /* automatic undefs */
  67486. #undef DUK__BYTEOFF_A
  67487. #undef DUK__BYTEOFF_B
  67488. #undef DUK__BYTEOFF_BC
  67489. #undef DUK__BYTEOFF_C
  67490. #undef DUK__COMPARE_BODY
  67491. #undef DUK__CONST
  67492. #undef DUK__CONSTP
  67493. #undef DUK__CONSTP_A
  67494. #undef DUK__CONSTP_B
  67495. #undef DUK__CONSTP_BC
  67496. #undef DUK__CONSTP_C
  67497. #undef DUK__DELPROP_BODY
  67498. #undef DUK__EQ_BODY
  67499. #undef DUK__FUN
  67500. #undef DUK__GETPROPC_BODY
  67501. #undef DUK__GETPROP_BODY
  67502. #undef DUK__GE_BODY
  67503. #undef DUK__GT_BODY
  67504. #undef DUK__INLINE_PERF
  67505. #undef DUK__INSTOF_BODY
  67506. #undef DUK__INTERNAL_ERROR
  67507. #undef DUK__INT_NOACTION
  67508. #undef DUK__INT_RESTART
  67509. #undef DUK__IN_BODY
  67510. #undef DUK__LE_BODY
  67511. #undef DUK__LONGJMP_RESTART
  67512. #undef DUK__LONGJMP_RETHROW
  67513. #undef DUK__LOOKUP_INDIRECT
  67514. #undef DUK__LT_BODY
  67515. #undef DUK__MASK_A
  67516. #undef DUK__MASK_B
  67517. #undef DUK__MASK_BC
  67518. #undef DUK__MASK_C
  67519. #undef DUK__NEQ_BODY
  67520. #undef DUK__NOINLINE_PERF
  67521. #undef DUK__PUTPROP_BODY
  67522. #undef DUK__RCBIT_B
  67523. #undef DUK__RCBIT_C
  67524. #undef DUK__REG
  67525. #undef DUK__REGCONSTP_B
  67526. #undef DUK__REGCONSTP_C
  67527. #undef DUK__REGP
  67528. #undef DUK__REGP_A
  67529. #undef DUK__REGP_B
  67530. #undef DUK__REGP_BC
  67531. #undef DUK__REGP_C
  67532. #undef DUK__REPLACE_BOOL_A_BREAK
  67533. #undef DUK__REPLACE_TOP_A_BREAK
  67534. #undef DUK__REPLACE_TOP_BC_BREAK
  67535. #undef DUK__REPLACE_TO_TVPTR
  67536. #undef DUK__RETHAND_FINISHED
  67537. #undef DUK__RETHAND_RESTART
  67538. #undef DUK__RETURN_SHARED
  67539. #undef DUK__SEQ_BODY
  67540. #undef DUK__SHIFT_A
  67541. #undef DUK__SHIFT_B
  67542. #undef DUK__SHIFT_BC
  67543. #undef DUK__SHIFT_C
  67544. #undef DUK__SNEQ_BODY
  67545. #undef DUK__STRICT
  67546. #undef DUK__SYNC_AND_NULL_CURR_PC
  67547. #undef DUK__SYNC_CURR_PC
  67548. #undef DUK__TVAL_SHIFT
  67549. #line 1 "duk_js_ops.c"
  67550. /*
  67551. * ECMAScript specification algorithm and conversion helpers.
  67552. *
  67553. * These helpers encapsulate the primitive ECMAScript operation semantics,
  67554. * and are used by the bytecode executor and the API (among other places).
  67555. * Some primitives are only implemented as part of the API and have no
  67556. * "internal" helper. This is the case when an internal helper would not
  67557. * really be useful; e.g. the operation is rare, uses value stack heavily,
  67558. * etc.
  67559. *
  67560. * The operation arguments depend on what is required to implement
  67561. * the operation:
  67562. *
  67563. * - If an operation is simple and stateless, and has no side
  67564. * effects, it won't take an duk_hthread argument and its
  67565. * arguments may be duk_tval pointers (which are safe as long
  67566. * as no side effects take place).
  67567. *
  67568. * - If complex coercions are required (e.g. a "ToNumber" coercion)
  67569. * or errors may be thrown, the operation takes an duk_hthread
  67570. * argument. This also implies that the operation may have
  67571. * arbitrary side effects, invalidating any duk_tval pointers.
  67572. *
  67573. * - For operations with potential side effects, arguments can be
  67574. * taken in several ways:
  67575. *
  67576. * a) as duk_tval pointers, which makes sense if the "common case"
  67577. * can be resolved without side effects (e.g. coercion); the
  67578. * arguments are pushed to the valstack for coercion if
  67579. * necessary
  67580. *
  67581. * b) as duk_tval values
  67582. *
  67583. * c) implicitly on value stack top
  67584. *
  67585. * d) as indices to the value stack
  67586. *
  67587. * Future work:
  67588. *
  67589. * - Argument styles may not be the most sensible in every case now.
  67590. *
  67591. * - In-place coercions might be useful for several operations, if
  67592. * in-place coercion is OK for the bytecode executor and the API.
  67593. */
  67594. /* #include duk_internal.h -> already included */
  67595. /*
  67596. * ToPrimitive() (E5 Section 9.1)
  67597. *
  67598. * ==> implemented in the API.
  67599. */
  67600. /*
  67601. * ToBoolean() (E5 Section 9.2)
  67602. */
  67603. DUK_INTERNAL duk_bool_t duk_js_toboolean(duk_tval *tv) {
  67604. switch (DUK_TVAL_GET_TAG(tv)) {
  67605. case DUK_TAG_UNDEFINED:
  67606. case DUK_TAG_NULL:
  67607. return 0;
  67608. case DUK_TAG_BOOLEAN:
  67609. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv) == 0 || DUK_TVAL_GET_BOOLEAN(tv) == 1);
  67610. return DUK_TVAL_GET_BOOLEAN(tv);
  67611. case DUK_TAG_STRING: {
  67612. /* Symbols ToBoolean() coerce to true, regardless of their
  67613. * description. This happens with no explicit check because
  67614. * of the symbol representation byte prefix.
  67615. */
  67616. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  67617. DUK_ASSERT(h != NULL);
  67618. return (DUK_HSTRING_GET_BYTELEN(h) > 0 ? 1 : 0);
  67619. }
  67620. case DUK_TAG_OBJECT: {
  67621. return 1;
  67622. }
  67623. case DUK_TAG_BUFFER: {
  67624. /* Mimic Uint8Array semantics: objects coerce true, regardless
  67625. * of buffer length (zero or not) or context.
  67626. */
  67627. return 1;
  67628. }
  67629. case DUK_TAG_POINTER: {
  67630. void *p = DUK_TVAL_GET_POINTER(tv);
  67631. return (p != NULL ? 1 : 0);
  67632. }
  67633. case DUK_TAG_LIGHTFUNC: {
  67634. return 1;
  67635. }
  67636. #if defined(DUK_USE_FASTINT)
  67637. case DUK_TAG_FASTINT:
  67638. if (DUK_TVAL_GET_FASTINT(tv) != 0) {
  67639. return 1;
  67640. } else {
  67641. return 0;
  67642. }
  67643. #endif
  67644. default: {
  67645. /* number */
  67646. duk_double_t d;
  67647. #if defined(DUK_USE_PREFER_SIZE)
  67648. int c;
  67649. #endif
  67650. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  67651. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  67652. d = DUK_TVAL_GET_DOUBLE(tv);
  67653. #if defined(DUK_USE_PREFER_SIZE)
  67654. c = DUK_FPCLASSIFY((double) d);
  67655. if (c == DUK_FP_ZERO || c == DUK_FP_NAN) {
  67656. return 0;
  67657. } else {
  67658. return 1;
  67659. }
  67660. #else
  67661. DUK_ASSERT(duk_double_is_nan_or_zero(d) == 0 || duk_double_is_nan_or_zero(d) == 1);
  67662. return duk_double_is_nan_or_zero(d) ^ 1;
  67663. #endif
  67664. }
  67665. }
  67666. DUK_UNREACHABLE();
  67667. }
  67668. /*
  67669. * ToNumber() (E5 Section 9.3)
  67670. *
  67671. * Value to convert must be on stack top, and is popped before exit.
  67672. *
  67673. * See: http://www.cs.indiana.edu/~burger/FP-Printing-PLDI96.pdf
  67674. * http://www.cs.indiana.edu/~burger/fp/index.html
  67675. *
  67676. * Notes on the conversion:
  67677. *
  67678. * - There are specific requirements on the accuracy of the conversion
  67679. * through a "Mathematical Value" (MV), so this conversion is not
  67680. * trivial.
  67681. *
  67682. * - Quick rejects (e.g. based on first char) are difficult because
  67683. * the grammar allows leading and trailing white space.
  67684. *
  67685. * - Quick reject based on string length is difficult even after
  67686. * accounting for white space; there may be arbitrarily many
  67687. * decimal digits.
  67688. *
  67689. * - Standard grammar allows decimal values ("123"), hex values
  67690. * ("0x123") and infinities
  67691. *
  67692. * - Unlike source code literals, ToNumber() coerces empty strings
  67693. * and strings with only whitespace to zero (not NaN).
  67694. */
  67695. /* E5 Section 9.3.1 */
  67696. DUK_LOCAL duk_double_t duk__tonumber_string_raw(duk_hthread *thr) {
  67697. duk_small_uint_t s2n_flags;
  67698. duk_double_t d;
  67699. DUK_ASSERT(duk_is_string(thr, -1));
  67700. /* Quite lenient, e.g. allow empty as zero, but don't allow trailing
  67701. * garbage.
  67702. */
  67703. s2n_flags = DUK_S2N_FLAG_TRIM_WHITE |
  67704. DUK_S2N_FLAG_ALLOW_EXP |
  67705. DUK_S2N_FLAG_ALLOW_PLUS |
  67706. DUK_S2N_FLAG_ALLOW_MINUS |
  67707. DUK_S2N_FLAG_ALLOW_INF |
  67708. DUK_S2N_FLAG_ALLOW_FRAC |
  67709. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  67710. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  67711. DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO |
  67712. DUK_S2N_FLAG_ALLOW_LEADING_ZERO |
  67713. DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT |
  67714. DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT |
  67715. DUK_S2N_FLAG_ALLOW_AUTO_BIN_INT;
  67716. duk_numconv_parse(thr, 10 /*radix*/, s2n_flags);
  67717. #if defined(DUK_USE_PREFER_SIZE)
  67718. d = duk_get_number(thr, -1);
  67719. duk_pop_unsafe(thr);
  67720. #else
  67721. thr->valstack_top--;
  67722. DUK_ASSERT(DUK_TVAL_IS_NUMBER(thr->valstack_top));
  67723. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(thr->valstack_top)); /* no fastint conversion in numconv now */
  67724. DUK_ASSERT(!DUK_TVAL_NEEDS_REFCOUNT_UPDATE(thr->valstack_top));
  67725. d = DUK_TVAL_GET_DOUBLE(thr->valstack_top); /* assumes not a fastint */
  67726. DUK_TVAL_SET_UNDEFINED(thr->valstack_top);
  67727. #endif
  67728. return d;
  67729. }
  67730. DUK_INTERNAL duk_double_t duk_js_tonumber(duk_hthread *thr, duk_tval *tv) {
  67731. DUK_ASSERT(thr != NULL);
  67732. DUK_ASSERT(tv != NULL);
  67733. switch (DUK_TVAL_GET_TAG(tv)) {
  67734. case DUK_TAG_UNDEFINED: {
  67735. /* return a specific NaN (although not strictly necessary) */
  67736. duk_double_union du;
  67737. DUK_DBLUNION_SET_NAN(&du);
  67738. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  67739. return du.d;
  67740. }
  67741. case DUK_TAG_NULL: {
  67742. /* +0.0 */
  67743. return 0.0;
  67744. }
  67745. case DUK_TAG_BOOLEAN: {
  67746. if (DUK_TVAL_IS_BOOLEAN_TRUE(tv)) {
  67747. return 1.0;
  67748. }
  67749. return 0.0;
  67750. }
  67751. case DUK_TAG_STRING: {
  67752. /* For Symbols ToNumber() is always a TypeError. */
  67753. duk_hstring *h = DUK_TVAL_GET_STRING(tv);
  67754. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(h))) {
  67755. DUK_ERROR_TYPE(thr, DUK_STR_CANNOT_NUMBER_COERCE_SYMBOL);
  67756. DUK_WO_NORETURN(return 0.0;);
  67757. }
  67758. duk_push_hstring(thr, h);
  67759. return duk__tonumber_string_raw(thr);
  67760. }
  67761. case DUK_TAG_BUFFER: /* plain buffer treated like object */
  67762. case DUK_TAG_OBJECT: {
  67763. duk_double_t d;
  67764. duk_push_tval(thr, tv);
  67765. duk_to_primitive(thr, -1, DUK_HINT_NUMBER); /* 'tv' becomes invalid */
  67766. /* recursive call for a primitive value (guaranteed not to cause second
  67767. * recursion).
  67768. */
  67769. DUK_ASSERT(duk_get_tval(thr, -1) != NULL);
  67770. d = duk_js_tonumber(thr, duk_get_tval(thr, -1));
  67771. duk_pop_unsafe(thr);
  67772. return d;
  67773. }
  67774. case DUK_TAG_POINTER: {
  67775. /* Coerce like boolean */
  67776. void *p = DUK_TVAL_GET_POINTER(tv);
  67777. return (p != NULL ? 1.0 : 0.0);
  67778. }
  67779. case DUK_TAG_LIGHTFUNC: {
  67780. /* +(function(){}) -> NaN */
  67781. return DUK_DOUBLE_NAN;
  67782. }
  67783. #if defined(DUK_USE_FASTINT)
  67784. case DUK_TAG_FASTINT:
  67785. return (duk_double_t) DUK_TVAL_GET_FASTINT(tv);
  67786. #endif
  67787. default: {
  67788. /* number */
  67789. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv));
  67790. DUK_ASSERT(DUK_TVAL_IS_DOUBLE(tv));
  67791. return DUK_TVAL_GET_DOUBLE(tv);
  67792. }
  67793. }
  67794. DUK_UNREACHABLE();
  67795. }
  67796. /*
  67797. * ToInteger() (E5 Section 9.4)
  67798. */
  67799. /* exposed, used by e.g. duk_bi_date.c */
  67800. DUK_INTERNAL duk_double_t duk_js_tointeger_number(duk_double_t x) {
  67801. #if defined(DUK_USE_PREFER_SIZE)
  67802. duk_small_int_t c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  67803. if (DUK_UNLIKELY(c == DUK_FP_NAN)) {
  67804. return 0.0;
  67805. } else if (DUK_UNLIKELY(c == DUK_FP_INFINITE)) {
  67806. return x;
  67807. } else {
  67808. /* Finite, including neg/pos zero. Neg zero sign must be
  67809. * preserved.
  67810. */
  67811. return duk_double_trunc_towards_zero(x);
  67812. }
  67813. #else /* DUK_USE_PREFER_SIZE */
  67814. /* NaN and Infinity have the same exponent so it's a cheap
  67815. * initial check for the rare path.
  67816. */
  67817. if (DUK_UNLIKELY(duk_double_is_nan_or_inf(x) != 0U)) {
  67818. if (duk_double_is_nan(x)) {
  67819. return 0.0;
  67820. } else {
  67821. return x;
  67822. }
  67823. } else {
  67824. return duk_double_trunc_towards_zero(x);
  67825. }
  67826. #endif /* DUK_USE_PREFER_SIZE */
  67827. }
  67828. DUK_INTERNAL duk_double_t duk_js_tointeger(duk_hthread *thr, duk_tval *tv) {
  67829. /* XXX: fastint */
  67830. duk_double_t d = duk_js_tonumber(thr, tv); /* invalidates tv */
  67831. return duk_js_tointeger_number(d);
  67832. }
  67833. /*
  67834. * ToInt32(), ToUint32(), ToUint16() (E5 Sections 9.5, 9.6, 9.7)
  67835. */
  67836. /* combined algorithm matching E5 Sections 9.5 and 9.6 */
  67837. DUK_LOCAL duk_double_t duk__toint32_touint32_helper(duk_double_t x, duk_bool_t is_toint32) {
  67838. #if defined (DUK_USE_PREFER_SIZE)
  67839. duk_small_int_t c;
  67840. #endif
  67841. #if defined (DUK_USE_PREFER_SIZE)
  67842. c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  67843. if (c == DUK_FP_NAN || c == DUK_FP_ZERO || c == DUK_FP_INFINITE) {
  67844. return 0.0;
  67845. }
  67846. #else
  67847. if (duk_double_is_nan_zero_inf(x)) {
  67848. return 0.0;
  67849. }
  67850. #endif
  67851. /* x = sign(x) * floor(abs(x)), i.e. truncate towards zero, keep sign */
  67852. x = duk_double_trunc_towards_zero(x);
  67853. /* NOTE: fmod(x) result sign is same as sign of x, which
  67854. * differs from what Javascript wants (see Section 9.6).
  67855. */
  67856. x = DUK_FMOD(x, DUK_DOUBLE_2TO32); /* -> x in ]-2**32, 2**32[ */
  67857. if (x < 0.0) {
  67858. x += DUK_DOUBLE_2TO32;
  67859. }
  67860. DUK_ASSERT(x >= 0 && x < DUK_DOUBLE_2TO32); /* -> x in [0, 2**32[ */
  67861. if (is_toint32) {
  67862. if (x >= DUK_DOUBLE_2TO31) {
  67863. /* x in [2**31, 2**32[ */
  67864. x -= DUK_DOUBLE_2TO32; /* -> x in [-2**31,2**31[ */
  67865. }
  67866. }
  67867. return x;
  67868. }
  67869. DUK_INTERNAL duk_int32_t duk_js_toint32(duk_hthread *thr, duk_tval *tv) {
  67870. duk_double_t d;
  67871. #if defined(DUK_USE_FASTINT)
  67872. if (DUK_TVAL_IS_FASTINT(tv)) {
  67873. return DUK_TVAL_GET_FASTINT_I32(tv);
  67874. }
  67875. #endif
  67876. d = duk_js_tonumber(thr, tv); /* invalidates tv */
  67877. d = duk__toint32_touint32_helper(d, 1);
  67878. DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL);
  67879. DUK_ASSERT(d >= -2147483648.0 && d <= 2147483647.0); /* [-0x80000000,0x7fffffff] */
  67880. DUK_ASSERT(d == ((duk_double_t) ((duk_int32_t) d))); /* whole, won't clip */
  67881. return (duk_int32_t) d;
  67882. }
  67883. DUK_INTERNAL duk_uint32_t duk_js_touint32(duk_hthread *thr, duk_tval *tv) {
  67884. duk_double_t d;
  67885. #if defined(DUK_USE_FASTINT)
  67886. if (DUK_TVAL_IS_FASTINT(tv)) {
  67887. return DUK_TVAL_GET_FASTINT_U32(tv);
  67888. }
  67889. #endif
  67890. d = duk_js_tonumber(thr, tv); /* invalidates tv */
  67891. d = duk__toint32_touint32_helper(d, 0);
  67892. DUK_ASSERT(DUK_FPCLASSIFY(d) == DUK_FP_ZERO || DUK_FPCLASSIFY(d) == DUK_FP_NORMAL);
  67893. DUK_ASSERT(d >= 0.0 && d <= 4294967295.0); /* [0x00000000, 0xffffffff] */
  67894. DUK_ASSERT(d == ((duk_double_t) ((duk_uint32_t) d))); /* whole, won't clip */
  67895. return (duk_uint32_t) d;
  67896. }
  67897. DUK_INTERNAL duk_uint16_t duk_js_touint16(duk_hthread *thr, duk_tval *tv) {
  67898. /* should be a safe way to compute this */
  67899. return (duk_uint16_t) (duk_js_touint32(thr, tv) & 0x0000ffffU);
  67900. }
  67901. /*
  67902. * ToString() (E5 Section 9.8)
  67903. * ToObject() (E5 Section 9.9)
  67904. * CheckObjectCoercible() (E5 Section 9.10)
  67905. * IsCallable() (E5 Section 9.11)
  67906. *
  67907. * ==> implemented in the API.
  67908. */
  67909. /*
  67910. * Loose equality, strict equality, and SameValue (E5 Sections 11.9.1, 11.9.4,
  67911. * 9.12). These have much in common so they can share some helpers.
  67912. *
  67913. * Future work notes:
  67914. *
  67915. * - Current implementation (and spec definition) has recursion; this should
  67916. * be fixed if possible.
  67917. *
  67918. * - String-to-number coercion should be possible without going through the
  67919. * value stack (and be more compact) if a shared helper is invoked.
  67920. */
  67921. /* Note that this is the same operation for strict and loose equality:
  67922. * - E5 Section 11.9.3, step 1.c (loose)
  67923. * - E5 Section 11.9.6, step 4 (strict)
  67924. */
  67925. DUK_LOCAL duk_bool_t duk__js_equals_number(duk_double_t x, duk_double_t y) {
  67926. #if defined(DUK_USE_PARANOID_MATH)
  67927. /* Straightforward algorithm, makes fewer compiler assumptions. */
  67928. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  67929. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  67930. if (cx == DUK_FP_NAN || cy == DUK_FP_NAN) {
  67931. return 0;
  67932. }
  67933. if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) {
  67934. return 1;
  67935. }
  67936. if (x == y) {
  67937. return 1;
  67938. }
  67939. return 0;
  67940. #else /* DUK_USE_PARANOID_MATH */
  67941. /* Better equivalent algorithm. If the compiler is compliant, C and
  67942. * ECMAScript semantics are identical for this particular comparison.
  67943. * In particular, NaNs must never compare equal and zeroes must compare
  67944. * equal regardless of sign. Could also use a macro, but this inlines
  67945. * already nicely (no difference on gcc, for instance).
  67946. */
  67947. if (x == y) {
  67948. /* IEEE requires that NaNs compare false */
  67949. DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN);
  67950. DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN);
  67951. return 1;
  67952. } else {
  67953. /* IEEE requires that zeros compare the same regardless
  67954. * of their signed, so if both x and y are zeroes, they
  67955. * are caught above.
  67956. */
  67957. DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO));
  67958. return 0;
  67959. }
  67960. #endif /* DUK_USE_PARANOID_MATH */
  67961. }
  67962. DUK_LOCAL duk_bool_t duk__js_samevalue_number(duk_double_t x, duk_double_t y) {
  67963. #if defined(DUK_USE_PARANOID_MATH)
  67964. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  67965. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  67966. if (cx == DUK_FP_NAN && cy == DUK_FP_NAN) {
  67967. /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */
  67968. return 1;
  67969. }
  67970. if (cx == DUK_FP_ZERO && cy == DUK_FP_ZERO) {
  67971. /* Note: cannot assume that a non-zero return value of signbit() would
  67972. * always be the same -- hence cannot (portably) use something like:
  67973. *
  67974. * signbit(x) == signbit(y)
  67975. */
  67976. duk_small_int_t sx = DUK_SIGNBIT(x) ? 1 : 0;
  67977. duk_small_int_t sy = DUK_SIGNBIT(y) ? 1 : 0;
  67978. return (sx == sy);
  67979. }
  67980. /* normal comparison; known:
  67981. * - both x and y are not NaNs (but one of them can be)
  67982. * - both x and y are not zero (but one of them can be)
  67983. * - x and y may be denormal or infinite
  67984. */
  67985. return (x == y);
  67986. #else /* DUK_USE_PARANOID_MATH */
  67987. duk_small_int_t cx = (duk_small_int_t) DUK_FPCLASSIFY(x);
  67988. duk_small_int_t cy = (duk_small_int_t) DUK_FPCLASSIFY(y);
  67989. if (x == y) {
  67990. /* IEEE requires that NaNs compare false */
  67991. DUK_ASSERT(DUK_FPCLASSIFY(x) != DUK_FP_NAN);
  67992. DUK_ASSERT(DUK_FPCLASSIFY(y) != DUK_FP_NAN);
  67993. /* Using classification has smaller footprint than direct comparison. */
  67994. if (DUK_UNLIKELY(cx == DUK_FP_ZERO && cy == DUK_FP_ZERO)) {
  67995. /* Note: cannot assume that a non-zero return value of signbit() would
  67996. * always be the same -- hence cannot (portably) use something like:
  67997. *
  67998. * signbit(x) == signbit(y)
  67999. */
  68000. return duk_double_same_sign(x, y);
  68001. }
  68002. return 1;
  68003. } else {
  68004. /* IEEE requires that zeros compare the same regardless
  68005. * of their sign, so if both x and y are zeroes, they
  68006. * are caught above.
  68007. */
  68008. DUK_ASSERT(!(DUK_FPCLASSIFY(x) == DUK_FP_ZERO && DUK_FPCLASSIFY(y) == DUK_FP_ZERO));
  68009. /* Difference to non-strict/strict comparison is that NaNs compare
  68010. * equal and signed zero signs matter.
  68011. */
  68012. if (DUK_UNLIKELY(cx == DUK_FP_NAN && cy == DUK_FP_NAN)) {
  68013. /* SameValue(NaN, NaN) = true, regardless of NaN sign or extra bits */
  68014. return 1;
  68015. }
  68016. return 0;
  68017. }
  68018. #endif /* DUK_USE_PARANOID_MATH */
  68019. }
  68020. DUK_INTERNAL duk_bool_t duk_js_equals_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_t flags) {
  68021. duk_uint_t type_mask_x;
  68022. duk_uint_t type_mask_y;
  68023. /* If flags != 0 (strict or SameValue), thr can be NULL. For loose
  68024. * equals comparison it must be != NULL.
  68025. */
  68026. DUK_ASSERT(flags != 0 || thr != NULL);
  68027. /*
  68028. * Same type?
  68029. *
  68030. * Note: since number values have no explicit tag in the 8-byte
  68031. * representation, need the awkward if + switch.
  68032. */
  68033. #if defined(DUK_USE_FASTINT)
  68034. if (DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y)) {
  68035. if (DUK_TVAL_GET_FASTINT(tv_x) == DUK_TVAL_GET_FASTINT(tv_y)) {
  68036. return 1;
  68037. } else {
  68038. return 0;
  68039. }
  68040. }
  68041. else
  68042. #endif
  68043. if (DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y)) {
  68044. duk_double_t d1, d2;
  68045. /* Catches both doubles and cases where only one argument is
  68046. * a fastint so can't assume a double.
  68047. */
  68048. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  68049. d2 = DUK_TVAL_GET_NUMBER(tv_y);
  68050. if (DUK_UNLIKELY((flags & DUK_EQUALS_FLAG_SAMEVALUE) != 0)) {
  68051. /* SameValue */
  68052. return duk__js_samevalue_number(d1, d2);
  68053. } else {
  68054. /* equals and strict equals */
  68055. return duk__js_equals_number(d1, d2);
  68056. }
  68057. } else if (DUK_TVAL_GET_TAG(tv_x) == DUK_TVAL_GET_TAG(tv_y)) {
  68058. switch (DUK_TVAL_GET_TAG(tv_x)) {
  68059. case DUK_TAG_UNDEFINED:
  68060. case DUK_TAG_NULL: {
  68061. return 1;
  68062. }
  68063. case DUK_TAG_BOOLEAN: {
  68064. return DUK_TVAL_GET_BOOLEAN(tv_x) == DUK_TVAL_GET_BOOLEAN(tv_y);
  68065. }
  68066. case DUK_TAG_POINTER: {
  68067. return DUK_TVAL_GET_POINTER(tv_x) == DUK_TVAL_GET_POINTER(tv_y);
  68068. }
  68069. case DUK_TAG_STRING:
  68070. case DUK_TAG_OBJECT: {
  68071. /* Heap pointer comparison suffices for strings and objects.
  68072. * Symbols compare equal if they have the same internal
  68073. * representation; again heap pointer comparison suffices.
  68074. */
  68075. return DUK_TVAL_GET_HEAPHDR(tv_x) == DUK_TVAL_GET_HEAPHDR(tv_y);
  68076. }
  68077. case DUK_TAG_BUFFER: {
  68078. /* In Duktape 2.x plain buffers mimic Uint8Array objects
  68079. * so always compare by heap pointer. In Duktape 1.x
  68080. * strict comparison would compare heap pointers and
  68081. * non-strict would compare contents.
  68082. */
  68083. return DUK_TVAL_GET_HEAPHDR(tv_x) == DUK_TVAL_GET_HEAPHDR(tv_y);
  68084. }
  68085. case DUK_TAG_LIGHTFUNC: {
  68086. /* At least 'magic' has a significant impact on function
  68087. * identity.
  68088. */
  68089. duk_small_uint_t lf_flags_x;
  68090. duk_small_uint_t lf_flags_y;
  68091. duk_c_function func_x;
  68092. duk_c_function func_y;
  68093. DUK_TVAL_GET_LIGHTFUNC(tv_x, func_x, lf_flags_x);
  68094. DUK_TVAL_GET_LIGHTFUNC(tv_y, func_y, lf_flags_y);
  68095. return ((func_x == func_y) && (lf_flags_x == lf_flags_y)) ? 1 : 0;
  68096. }
  68097. #if defined(DUK_USE_FASTINT)
  68098. case DUK_TAG_FASTINT:
  68099. #endif
  68100. default: {
  68101. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_x));
  68102. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_y));
  68103. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  68104. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_y));
  68105. DUK_UNREACHABLE();
  68106. return 0;
  68107. }
  68108. }
  68109. }
  68110. if ((flags & (DUK_EQUALS_FLAG_STRICT | DUK_EQUALS_FLAG_SAMEVALUE)) != 0) {
  68111. return 0;
  68112. }
  68113. DUK_ASSERT(flags == 0); /* non-strict equality from here on */
  68114. /*
  68115. * Types are different; various cases for non-strict comparison
  68116. *
  68117. * Since comparison is symmetric, we use a "swap trick" to reduce
  68118. * code size.
  68119. */
  68120. type_mask_x = duk_get_type_mask_tval(tv_x);
  68121. type_mask_y = duk_get_type_mask_tval(tv_y);
  68122. /* Undefined/null are considered equal (e.g. "null == undefined" -> true). */
  68123. if ((type_mask_x & (DUK_TYPE_MASK_UNDEFINED | DUK_TYPE_MASK_NULL)) &&
  68124. (type_mask_y & (DUK_TYPE_MASK_NULL | DUK_TYPE_MASK_UNDEFINED))) {
  68125. return 1;
  68126. }
  68127. /* Number/string -> coerce string to number (e.g. "'1.5' == 1.5" -> true). */
  68128. if ((type_mask_x & DUK_TYPE_MASK_NUMBER) && (type_mask_y & DUK_TYPE_MASK_STRING)) {
  68129. if (!DUK_TVAL_STRING_IS_SYMBOL(tv_y)) {
  68130. duk_double_t d1, d2;
  68131. d1 = DUK_TVAL_GET_NUMBER(tv_x);
  68132. d2 = duk_to_number_tval(thr, tv_y);
  68133. return duk__js_equals_number(d1, d2);
  68134. }
  68135. }
  68136. if ((type_mask_x & DUK_TYPE_MASK_STRING) && (type_mask_y & DUK_TYPE_MASK_NUMBER)) {
  68137. if (!DUK_TVAL_STRING_IS_SYMBOL(tv_x)) {
  68138. duk_double_t d1, d2;
  68139. d1 = DUK_TVAL_GET_NUMBER(tv_y);
  68140. d2 = duk_to_number_tval(thr, tv_x);
  68141. return duk__js_equals_number(d1, d2);
  68142. }
  68143. }
  68144. /* Boolean/any -> coerce boolean to number and try again. If boolean is
  68145. * compared to a pointer, the final comparison after coercion now always
  68146. * yields false (as pointer vs. number compares to false), but this is
  68147. * not special cased.
  68148. *
  68149. * ToNumber(bool) is +1.0 or 0.0. Tagged boolean value is always 0 or 1.
  68150. */
  68151. if (type_mask_x & DUK_TYPE_MASK_BOOLEAN) {
  68152. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv_x) == 0 || DUK_TVAL_GET_BOOLEAN(tv_x) == 1);
  68153. duk_push_uint(thr, DUK_TVAL_GET_BOOLEAN(tv_x));
  68154. duk_push_tval(thr, tv_y);
  68155. goto recursive_call;
  68156. }
  68157. if (type_mask_y & DUK_TYPE_MASK_BOOLEAN) {
  68158. DUK_ASSERT(DUK_TVAL_GET_BOOLEAN(tv_y) == 0 || DUK_TVAL_GET_BOOLEAN(tv_y) == 1);
  68159. duk_push_tval(thr, tv_x);
  68160. duk_push_uint(thr, DUK_TVAL_GET_BOOLEAN(tv_y));
  68161. goto recursive_call;
  68162. }
  68163. /* String-number-symbol/object -> coerce object to primitive (apparently without hint), then try again. */
  68164. if ((type_mask_x & (DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_NUMBER)) &&
  68165. (type_mask_y & DUK_TYPE_MASK_OBJECT)) {
  68166. /* No symbol check needed because symbols and strings are accepted. */
  68167. duk_push_tval(thr, tv_x);
  68168. duk_push_tval(thr, tv_y);
  68169. duk_to_primitive(thr, -1, DUK_HINT_NONE); /* apparently no hint? */
  68170. goto recursive_call;
  68171. }
  68172. if ((type_mask_x & DUK_TYPE_MASK_OBJECT) &&
  68173. (type_mask_y & (DUK_TYPE_MASK_STRING | DUK_TYPE_MASK_NUMBER))) {
  68174. /* No symbol check needed because symbols and strings are accepted. */
  68175. duk_push_tval(thr, tv_x);
  68176. duk_push_tval(thr, tv_y);
  68177. duk_to_primitive(thr, -2, DUK_HINT_NONE); /* apparently no hint? */
  68178. goto recursive_call;
  68179. }
  68180. /* Nothing worked -> not equal. */
  68181. return 0;
  68182. recursive_call:
  68183. /* Shared code path to call the helper again with arguments on stack top. */
  68184. {
  68185. duk_bool_t rc;
  68186. rc = duk_js_equals_helper(thr,
  68187. DUK_GET_TVAL_NEGIDX(thr, -2),
  68188. DUK_GET_TVAL_NEGIDX(thr, -1),
  68189. 0 /*flags:nonstrict*/);
  68190. duk_pop_2_unsafe(thr);
  68191. return rc;
  68192. }
  68193. }
  68194. /*
  68195. * Comparisons (x >= y, x > y, x <= y, x < y)
  68196. *
  68197. * E5 Section 11.8.5: implement 'x < y' and then use negate and eval_left_first
  68198. * flags to get the rest.
  68199. */
  68200. /* XXX: this should probably just operate on the stack top, because it
  68201. * needs to push stuff on the stack anyway...
  68202. */
  68203. DUK_INTERNAL duk_small_int_t duk_js_data_compare(const duk_uint8_t *buf1, const duk_uint8_t *buf2, duk_size_t len1, duk_size_t len2) {
  68204. duk_size_t prefix_len;
  68205. duk_small_int_t rc;
  68206. prefix_len = (len1 <= len2 ? len1 : len2);
  68207. /* duk_memcmp() is guaranteed to return zero (equal) for zero length
  68208. * inputs.
  68209. */
  68210. rc = duk_memcmp_unsafe((const void *) buf1,
  68211. (const void *) buf2,
  68212. (size_t) prefix_len);
  68213. if (rc < 0) {
  68214. return -1;
  68215. } else if (rc > 0) {
  68216. return 1;
  68217. }
  68218. /* prefix matches, lengths matter now */
  68219. if (len1 < len2) {
  68220. /* e.g. "x" < "xx" */
  68221. return -1;
  68222. } else if (len1 > len2) {
  68223. return 1;
  68224. }
  68225. return 0;
  68226. }
  68227. DUK_INTERNAL duk_small_int_t duk_js_string_compare(duk_hstring *h1, duk_hstring *h2) {
  68228. /*
  68229. * String comparison (E5 Section 11.8.5, step 4), which
  68230. * needs to compare codepoint by codepoint.
  68231. *
  68232. * However, UTF-8 allows us to use strcmp directly: the shared
  68233. * prefix will be encoded identically (UTF-8 has unique encoding)
  68234. * and the first differing character can be compared with a simple
  68235. * unsigned byte comparison (which strcmp does).
  68236. *
  68237. * This will not work properly for non-xutf-8 strings, but this
  68238. * is not an issue for compliance.
  68239. */
  68240. DUK_ASSERT(h1 != NULL);
  68241. DUK_ASSERT(h2 != NULL);
  68242. return duk_js_data_compare((const duk_uint8_t *) DUK_HSTRING_GET_DATA(h1),
  68243. (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h2),
  68244. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h1),
  68245. (duk_size_t) DUK_HSTRING_GET_BYTELEN(h2));
  68246. }
  68247. #if 0 /* unused */
  68248. DUK_INTERNAL duk_small_int_t duk_js_buffer_compare(duk_heap *heap, duk_hbuffer *h1, duk_hbuffer *h2) {
  68249. /* Similar to String comparison. */
  68250. DUK_ASSERT(h1 != NULL);
  68251. DUK_ASSERT(h2 != NULL);
  68252. DUK_UNREF(heap);
  68253. return duk_js_data_compare((const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(heap, h1),
  68254. (const duk_uint8_t *) DUK_HBUFFER_GET_DATA_PTR(heap, h2),
  68255. (duk_size_t) DUK_HBUFFER_GET_SIZE(h1),
  68256. (duk_size_t) DUK_HBUFFER_GET_SIZE(h2));
  68257. }
  68258. #endif
  68259. #if defined(DUK_USE_FASTINT)
  68260. DUK_LOCAL duk_bool_t duk__compare_fastint(duk_bool_t retval, duk_int64_t v1, duk_int64_t v2) {
  68261. DUK_ASSERT(retval == 0 || retval == 1);
  68262. if (v1 < v2) {
  68263. return retval ^ 1;
  68264. } else {
  68265. return retval;
  68266. }
  68267. }
  68268. #endif
  68269. #if defined(DUK_USE_PARANOID_MATH)
  68270. DUK_LOCAL duk_bool_t duk__compare_number(duk_bool_t retval, duk_double_t d1, duk_double_t d2) {
  68271. duk_small_int_t c1, s1, c2, s2;
  68272. DUK_ASSERT(retval == 0 || retval == 1);
  68273. c1 = (duk_small_int_t) DUK_FPCLASSIFY(d1);
  68274. s1 = (duk_small_int_t) DUK_SIGNBIT(d1);
  68275. c2 = (duk_small_int_t) DUK_FPCLASSIFY(d2);
  68276. s2 = (duk_small_int_t) DUK_SIGNBIT(d2);
  68277. if (c1 == DUK_FP_NAN || c2 == DUK_FP_NAN) {
  68278. return 0; /* Always false, regardless of negation. */
  68279. }
  68280. if (c1 == DUK_FP_ZERO && c2 == DUK_FP_ZERO) {
  68281. /* For all combinations: +0 < +0, +0 < -0, -0 < +0, -0 < -0,
  68282. * steps e, f, and g.
  68283. */
  68284. return retval; /* false */
  68285. }
  68286. if (d1 == d2) {
  68287. return retval; /* false */
  68288. }
  68289. if (c1 == DUK_FP_INFINITE && s1 == 0) {
  68290. /* x == +Infinity */
  68291. return retval; /* false */
  68292. }
  68293. if (c2 == DUK_FP_INFINITE && s2 == 0) {
  68294. /* y == +Infinity */
  68295. return retval ^ 1; /* true */
  68296. }
  68297. if (c2 == DUK_FP_INFINITE && s2 != 0) {
  68298. /* y == -Infinity */
  68299. return retval; /* false */
  68300. }
  68301. if (c1 == DUK_FP_INFINITE && s1 != 0) {
  68302. /* x == -Infinity */
  68303. return retval ^ 1; /* true */
  68304. }
  68305. if (d1 < d2) {
  68306. return retval ^ 1; /* true */
  68307. }
  68308. return retval; /* false */
  68309. }
  68310. #else /* DUK_USE_PARANOID_MATH */
  68311. DUK_LOCAL duk_bool_t duk__compare_number(duk_bool_t retval, duk_double_t d1, duk_double_t d2) {
  68312. /* This comparison tree relies doesn't match the exact steps in
  68313. * E5 Section 11.8.5 but should produce the same results. The
  68314. * steps rely on exact IEEE semantics for NaNs, etc.
  68315. */
  68316. DUK_ASSERT(retval == 0 || retval == 1);
  68317. if (d1 < d2) {
  68318. /* In no case should both (d1 < d2) and (d2 < d1) be true.
  68319. * It's possible that neither is true though, and that's
  68320. * handled below.
  68321. */
  68322. DUK_ASSERT(!(d2 < d1));
  68323. /* - d1 < d2, both d1/d2 are normals (not Infinity, not NaN)
  68324. * - d2 is +Infinity, d1 != +Infinity and NaN
  68325. * - d1 is -Infinity, d2 != -Infinity and NaN
  68326. */
  68327. return retval ^ 1;
  68328. } else {
  68329. if (d2 < d1) {
  68330. /* - !(d1 < d2), both d1/d2 are normals (not Infinity, not NaN)
  68331. * - d1 is +Infinity, d2 != +Infinity and NaN
  68332. * - d2 is -Infinity, d1 != -Infinity and NaN
  68333. */
  68334. return retval;
  68335. } else {
  68336. /* - d1 and/or d2 is NaN
  68337. * - d1 and d2 are both +/- 0
  68338. * - d1 == d2 (including infinities)
  68339. */
  68340. if (duk_double_is_nan(d1) || duk_double_is_nan(d2)) {
  68341. /* Note: undefined from Section 11.8.5 always
  68342. * results in false return (see e.g. Section
  68343. * 11.8.3) - hence special treatment here.
  68344. */
  68345. return 0; /* zero regardless of negation */
  68346. } else {
  68347. return retval;
  68348. }
  68349. }
  68350. }
  68351. }
  68352. #endif /* DUK_USE_PARANOID_MATH */
  68353. DUK_INTERNAL duk_bool_t duk_js_compare_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_small_uint_t flags) {
  68354. duk_double_t d1, d2;
  68355. duk_small_int_t rc;
  68356. duk_bool_t retval;
  68357. DUK_ASSERT(DUK_COMPARE_FLAG_NEGATE == 1); /* Rely on this flag being lowest. */
  68358. retval = flags & DUK_COMPARE_FLAG_NEGATE;
  68359. DUK_ASSERT(retval == 0 || retval == 1);
  68360. /* Fast path for fastints */
  68361. #if defined(DUK_USE_FASTINT)
  68362. if (DUK_LIKELY(DUK_TVAL_IS_FASTINT(tv_x) && DUK_TVAL_IS_FASTINT(tv_y))) {
  68363. return duk__compare_fastint(retval,
  68364. DUK_TVAL_GET_FASTINT(tv_x),
  68365. DUK_TVAL_GET_FASTINT(tv_y));
  68366. }
  68367. #endif /* DUK_USE_FASTINT */
  68368. /* Fast path for numbers (one of which may be a fastint) */
  68369. #if !defined(DUK_USE_PREFER_SIZE)
  68370. if (DUK_LIKELY(DUK_TVAL_IS_NUMBER(tv_x) && DUK_TVAL_IS_NUMBER(tv_y))) {
  68371. return duk__compare_number(retval,
  68372. DUK_TVAL_GET_NUMBER(tv_x),
  68373. DUK_TVAL_GET_NUMBER(tv_y));
  68374. }
  68375. #endif
  68376. /* Slow path */
  68377. duk_push_tval(thr, tv_x);
  68378. duk_push_tval(thr, tv_y);
  68379. if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
  68380. duk_to_primitive(thr, -2, DUK_HINT_NUMBER);
  68381. duk_to_primitive(thr, -1, DUK_HINT_NUMBER);
  68382. } else {
  68383. duk_to_primitive(thr, -1, DUK_HINT_NUMBER);
  68384. duk_to_primitive(thr, -2, DUK_HINT_NUMBER);
  68385. }
  68386. /* Note: reuse variables */
  68387. tv_x = DUK_GET_TVAL_NEGIDX(thr, -2);
  68388. tv_y = DUK_GET_TVAL_NEGIDX(thr, -1);
  68389. if (DUK_TVAL_IS_STRING(tv_x) && DUK_TVAL_IS_STRING(tv_y)) {
  68390. duk_hstring *h1 = DUK_TVAL_GET_STRING(tv_x);
  68391. duk_hstring *h2 = DUK_TVAL_GET_STRING(tv_y);
  68392. DUK_ASSERT(h1 != NULL);
  68393. DUK_ASSERT(h2 != NULL);
  68394. if (DUK_LIKELY(!DUK_HSTRING_HAS_SYMBOL(h1) && !DUK_HSTRING_HAS_SYMBOL(h2))) {
  68395. rc = duk_js_string_compare(h1, h2);
  68396. duk_pop_2_unsafe(thr);
  68397. if (rc < 0) {
  68398. return retval ^ 1;
  68399. } else {
  68400. return retval;
  68401. }
  68402. }
  68403. /* One or both are Symbols: fall through to handle in the
  68404. * generic path. Concretely, ToNumber() will fail.
  68405. */
  68406. }
  68407. /* Ordering should not matter (E5 Section 11.8.5, step 3.a). */
  68408. #if 0
  68409. if (flags & DUK_COMPARE_FLAG_EVAL_LEFT_FIRST) {
  68410. d1 = duk_to_number_m2(thr);
  68411. d2 = duk_to_number_m1(thr);
  68412. } else {
  68413. d2 = duk_to_number_m1(thr);
  68414. d1 = duk_to_number_m2(thr);
  68415. }
  68416. #endif
  68417. d1 = duk_to_number_m2(thr);
  68418. d2 = duk_to_number_m1(thr);
  68419. /* We want to duk_pop_2_unsafe(thr); because the values are numbers
  68420. * no decref check is needed.
  68421. */
  68422. #if defined(DUK_USE_PREFER_SIZE)
  68423. duk_pop_2_nodecref_unsafe(thr);
  68424. #else
  68425. DUK_ASSERT(!DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk_get_tval(thr, -2)));
  68426. DUK_ASSERT(!DUK_TVAL_NEEDS_REFCOUNT_UPDATE(duk_get_tval(thr, -1)));
  68427. DUK_ASSERT(duk_get_top(thr) >= 2);
  68428. thr->valstack_top -= 2;
  68429. tv_x = thr->valstack_top;
  68430. tv_y = tv_x + 1;
  68431. DUK_TVAL_SET_UNDEFINED(tv_x); /* Value stack policy */
  68432. DUK_TVAL_SET_UNDEFINED(tv_y);
  68433. #endif
  68434. return duk__compare_number(retval, d1, d2);
  68435. }
  68436. /*
  68437. * instanceof
  68438. */
  68439. /*
  68440. * ES2015 Section 7.3.19 describes the OrdinaryHasInstance() algorithm
  68441. * which covers both bound and non-bound functions; in effect the algorithm
  68442. * includes E5 Sections 11.8.6, 15.3.5.3, and 15.3.4.5.3.
  68443. *
  68444. * ES2015 Section 12.9.4 describes the instanceof operator which first
  68445. * checks @@hasInstance well-known symbol and falls back to
  68446. * OrdinaryHasInstance().
  68447. *
  68448. * Limited Proxy support: don't support 'getPrototypeOf' trap but
  68449. * continue lookup in Proxy target if the value is a Proxy.
  68450. */
  68451. DUK_LOCAL duk_bool_t duk__js_instanceof_helper(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y, duk_bool_t skip_sym_check) {
  68452. duk_hobject *func;
  68453. duk_hobject *val;
  68454. duk_hobject *proto;
  68455. duk_tval *tv;
  68456. duk_bool_t skip_first;
  68457. duk_uint_t sanity;
  68458. /*
  68459. * Get the values onto the stack first. It would be possible to cover
  68460. * some normal cases without resorting to the value stack.
  68461. *
  68462. * The right hand side could be a light function (as they generally
  68463. * behave like objects). Light functions never have a 'prototype'
  68464. * property so E5.1 Section 15.3.5.3 step 3 always throws a TypeError.
  68465. * Using duk_require_hobject() is thus correct (except for error msg).
  68466. */
  68467. duk_push_tval(thr, tv_x);
  68468. duk_push_tval(thr, tv_y);
  68469. func = duk_require_hobject(thr, -1);
  68470. DUK_ASSERT(func != NULL);
  68471. #if defined(DUK_USE_SYMBOL_BUILTIN)
  68472. /*
  68473. * @@hasInstance check, ES2015 Section 12.9.4, Steps 2-4.
  68474. */
  68475. if (!skip_sym_check) {
  68476. if (duk_get_method_stridx(thr, -1, DUK_STRIDX_WELLKNOWN_SYMBOL_HAS_INSTANCE)) {
  68477. /* [ ... lhs rhs func ] */
  68478. duk_insert(thr, -3); /* -> [ ... func lhs rhs ] */
  68479. duk_swap_top(thr, -2); /* -> [ ... func rhs(this) lhs ] */
  68480. duk_call_method(thr, 1);
  68481. return duk_to_boolean_top_pop(thr);
  68482. }
  68483. }
  68484. #else
  68485. DUK_UNREF(skip_sym_check);
  68486. #endif
  68487. /*
  68488. * For bound objects, [[HasInstance]] just calls the target function
  68489. * [[HasInstance]]. If that is again a bound object, repeat until
  68490. * we find a non-bound Function object.
  68491. *
  68492. * The bound function chain is now "collapsed" so there can be only
  68493. * one bound function in the chain.
  68494. */
  68495. if (!DUK_HOBJECT_IS_CALLABLE(func)) {
  68496. /*
  68497. * Note: of native ECMAScript objects, only Function instances
  68498. * have a [[HasInstance]] internal property. Custom objects might
  68499. * also have it, but not in current implementation.
  68500. *
  68501. * XXX: add a separate flag, DUK_HOBJECT_FLAG_ALLOW_INSTANCEOF?
  68502. */
  68503. goto error_invalid_rval;
  68504. }
  68505. if (DUK_HOBJECT_HAS_BOUNDFUNC(func)) {
  68506. duk_push_tval(thr, &((duk_hboundfunc *) (void *) func)->target);
  68507. duk_replace(thr, -2);
  68508. func = duk_require_hobject(thr, -1); /* lightfunc throws */
  68509. /* Rely on Function.prototype.bind() never creating bound
  68510. * functions whose target is not proper.
  68511. */
  68512. DUK_ASSERT(func != NULL);
  68513. DUK_ASSERT(DUK_HOBJECT_IS_CALLABLE(func));
  68514. }
  68515. /*
  68516. * 'func' is now a non-bound object which supports [[HasInstance]]
  68517. * (which here just means DUK_HOBJECT_FLAG_CALLABLE). Move on
  68518. * to execute E5 Section 15.3.5.3.
  68519. */
  68520. DUK_ASSERT(func != NULL);
  68521. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(func));
  68522. DUK_ASSERT(DUK_HOBJECT_IS_CALLABLE(func));
  68523. /* [ ... lval rval(func) ] */
  68524. /* For lightfuncs, buffers, and pointers start the comparison directly
  68525. * from the virtual prototype object.
  68526. */
  68527. skip_first = 0;
  68528. tv = DUK_GET_TVAL_NEGIDX(thr, -2);
  68529. switch (DUK_TVAL_GET_TAG(tv)) {
  68530. case DUK_TAG_LIGHTFUNC:
  68531. val = thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE];
  68532. DUK_ASSERT(val != NULL);
  68533. break;
  68534. case DUK_TAG_BUFFER:
  68535. val = thr->builtins[DUK_BIDX_UINT8ARRAY_PROTOTYPE];
  68536. DUK_ASSERT(val != NULL);
  68537. break;
  68538. case DUK_TAG_POINTER:
  68539. val = thr->builtins[DUK_BIDX_POINTER_PROTOTYPE];
  68540. DUK_ASSERT(val != NULL);
  68541. break;
  68542. case DUK_TAG_OBJECT:
  68543. skip_first = 1; /* Ignore object itself on first round. */
  68544. val = DUK_TVAL_GET_OBJECT(tv);
  68545. DUK_ASSERT(val != NULL);
  68546. break;
  68547. default:
  68548. goto pop2_and_false;
  68549. }
  68550. DUK_ASSERT(val != NULL); /* Loop doesn't actually rely on this. */
  68551. /* Look up .prototype of rval. Leave it on the value stack in case it
  68552. * has been virtualized (e.g. getter, Proxy trap).
  68553. */
  68554. duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_PROTOTYPE); /* -> [ ... lval rval rval.prototype ] */
  68555. #if defined(DUK_USE_VERBOSE_ERRORS)
  68556. proto = duk_get_hobject(thr, -1);
  68557. if (proto == NULL) {
  68558. goto error_invalid_rval_noproto;
  68559. }
  68560. #else
  68561. proto = duk_require_hobject(thr, -1);
  68562. #endif
  68563. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  68564. do {
  68565. /*
  68566. * Note: prototype chain is followed BEFORE first comparison. This
  68567. * means that the instanceof lval is never itself compared to the
  68568. * rval.prototype property. This is apparently intentional, see E5
  68569. * Section 15.3.5.3, step 4.a.
  68570. *
  68571. * Also note:
  68572. *
  68573. * js> (function() {}) instanceof Function
  68574. * true
  68575. * js> Function instanceof Function
  68576. * true
  68577. *
  68578. * For the latter, h_proto will be Function.prototype, which is the
  68579. * built-in Function prototype. Because Function.[[Prototype]] is
  68580. * also the built-in Function prototype, the result is true.
  68581. */
  68582. if (!val) {
  68583. goto pop3_and_false;
  68584. }
  68585. DUK_ASSERT(val != NULL);
  68586. #if defined(DUK_USE_ES6_PROXY)
  68587. val = duk_hobject_resolve_proxy_target(val);
  68588. #endif
  68589. if (skip_first) {
  68590. skip_first = 0;
  68591. } else if (val == proto) {
  68592. goto pop3_and_true;
  68593. }
  68594. DUK_ASSERT(val != NULL);
  68595. val = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, val);
  68596. } while (--sanity > 0);
  68597. if (DUK_UNLIKELY(sanity == 0)) {
  68598. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  68599. DUK_WO_NORETURN(return 0;);
  68600. }
  68601. DUK_UNREACHABLE();
  68602. pop2_and_false:
  68603. duk_pop_2_unsafe(thr);
  68604. return 0;
  68605. pop3_and_false:
  68606. duk_pop_3_unsafe(thr);
  68607. return 0;
  68608. pop3_and_true:
  68609. duk_pop_3_unsafe(thr);
  68610. return 1;
  68611. error_invalid_rval:
  68612. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_INSTANCEOF_RVAL);
  68613. DUK_WO_NORETURN(return 0;);
  68614. #if defined(DUK_USE_VERBOSE_ERRORS)
  68615. error_invalid_rval_noproto:
  68616. DUK_ERROR_TYPE(thr, DUK_STR_INVALID_INSTANCEOF_RVAL_NOPROTO);
  68617. DUK_WO_NORETURN(return 0;);
  68618. #endif
  68619. }
  68620. #if defined(DUK_USE_SYMBOL_BUILTIN)
  68621. DUK_INTERNAL duk_bool_t duk_js_instanceof_ordinary(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  68622. return duk__js_instanceof_helper(thr, tv_x, tv_y, 1 /*skip_sym_check*/);
  68623. }
  68624. #endif
  68625. DUK_INTERNAL duk_bool_t duk_js_instanceof(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  68626. return duk__js_instanceof_helper(thr, tv_x, tv_y, 0 /*skip_sym_check*/);
  68627. }
  68628. /*
  68629. * in
  68630. */
  68631. /*
  68632. * E5 Sections 11.8.7, 8.12.6.
  68633. *
  68634. * Basically just a property existence check using [[HasProperty]].
  68635. */
  68636. DUK_INTERNAL duk_bool_t duk_js_in(duk_hthread *thr, duk_tval *tv_x, duk_tval *tv_y) {
  68637. duk_bool_t retval;
  68638. /*
  68639. * Get the values onto the stack first. It would be possible to cover
  68640. * some normal cases without resorting to the value stack (e.g. if
  68641. * lval is already a string).
  68642. */
  68643. /* XXX: The ES5/5.1/6 specifications require that the key in 'key in obj'
  68644. * must be string coerced before the internal HasProperty() algorithm is
  68645. * invoked. A fast path skipping coercion could be safely implemented for
  68646. * numbers (as number-to-string coercion has no side effects). For ES2015
  68647. * proxy behavior, the trap 'key' argument must be in a string coerced
  68648. * form (which is a shame).
  68649. */
  68650. /* TypeError if rval is not an object or object like (e.g. lightfunc
  68651. * or plain buffer).
  68652. */
  68653. duk_push_tval(thr, tv_x);
  68654. duk_push_tval(thr, tv_y);
  68655. duk_require_type_mask(thr, -1, DUK_TYPE_MASK_OBJECT | DUK_TYPE_MASK_LIGHTFUNC | DUK_TYPE_MASK_BUFFER);
  68656. (void) duk_to_property_key_hstring(thr, -2);
  68657. retval = duk_hobject_hasprop(thr,
  68658. DUK_GET_TVAL_NEGIDX(thr, -1),
  68659. DUK_GET_TVAL_NEGIDX(thr, -2));
  68660. duk_pop_2_unsafe(thr);
  68661. return retval;
  68662. }
  68663. /*
  68664. * typeof
  68665. *
  68666. * E5 Section 11.4.3.
  68667. *
  68668. * Very straightforward. The only question is what to return for our
  68669. * non-standard tag / object types.
  68670. *
  68671. * There is an unfortunate string constant define naming problem with
  68672. * typeof return values for e.g. "Object" and "object"; careful with
  68673. * the built-in string defines. The LC_XXX defines are used for the
  68674. * lowercase variants now.
  68675. */
  68676. DUK_INTERNAL duk_small_uint_t duk_js_typeof_stridx(duk_tval *tv_x) {
  68677. duk_small_uint_t stridx = 0;
  68678. switch (DUK_TVAL_GET_TAG(tv_x)) {
  68679. case DUK_TAG_UNDEFINED: {
  68680. stridx = DUK_STRIDX_LC_UNDEFINED;
  68681. break;
  68682. }
  68683. case DUK_TAG_NULL: {
  68684. /* Note: not a typo, "object" is returned for a null value. */
  68685. stridx = DUK_STRIDX_LC_OBJECT;
  68686. break;
  68687. }
  68688. case DUK_TAG_BOOLEAN: {
  68689. stridx = DUK_STRIDX_LC_BOOLEAN;
  68690. break;
  68691. }
  68692. case DUK_TAG_POINTER: {
  68693. /* Implementation specific. */
  68694. stridx = DUK_STRIDX_LC_POINTER;
  68695. break;
  68696. }
  68697. case DUK_TAG_STRING: {
  68698. duk_hstring *str;
  68699. /* All internal keys are identified as Symbols. */
  68700. str = DUK_TVAL_GET_STRING(tv_x);
  68701. DUK_ASSERT(str != NULL);
  68702. if (DUK_UNLIKELY(DUK_HSTRING_HAS_SYMBOL(str))) {
  68703. stridx = DUK_STRIDX_LC_SYMBOL;
  68704. } else {
  68705. stridx = DUK_STRIDX_LC_STRING;
  68706. }
  68707. break;
  68708. }
  68709. case DUK_TAG_OBJECT: {
  68710. duk_hobject *obj = DUK_TVAL_GET_OBJECT(tv_x);
  68711. DUK_ASSERT(obj != NULL);
  68712. if (DUK_HOBJECT_IS_CALLABLE(obj)) {
  68713. stridx = DUK_STRIDX_LC_FUNCTION;
  68714. } else {
  68715. stridx = DUK_STRIDX_LC_OBJECT;
  68716. }
  68717. break;
  68718. }
  68719. case DUK_TAG_BUFFER: {
  68720. /* Implementation specific. In Duktape 1.x this would be
  68721. * 'buffer', in Duktape 2.x changed to 'object' because plain
  68722. * buffers now mimic Uint8Array objects.
  68723. */
  68724. stridx = DUK_STRIDX_LC_OBJECT;
  68725. break;
  68726. }
  68727. case DUK_TAG_LIGHTFUNC: {
  68728. stridx = DUK_STRIDX_LC_FUNCTION;
  68729. break;
  68730. }
  68731. #if defined(DUK_USE_FASTINT)
  68732. case DUK_TAG_FASTINT:
  68733. #endif
  68734. default: {
  68735. /* number */
  68736. DUK_ASSERT(!DUK_TVAL_IS_UNUSED(tv_x));
  68737. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv_x));
  68738. stridx = DUK_STRIDX_LC_NUMBER;
  68739. break;
  68740. }
  68741. }
  68742. DUK_ASSERT_STRIDX_VALID(stridx);
  68743. return stridx;
  68744. }
  68745. /*
  68746. * Array index and length
  68747. *
  68748. * Array index: E5 Section 15.4
  68749. * Array length: E5 Section 15.4.5.1 steps 3.c - 3.d (array length write)
  68750. */
  68751. /* Compure array index from string context, or return a "not array index"
  68752. * indicator.
  68753. */
  68754. DUK_INTERNAL duk_uarridx_t duk_js_to_arrayindex_string(const duk_uint8_t *str, duk_uint32_t blen) {
  68755. duk_uarridx_t res;
  68756. /* Only strings with byte length 1-10 can be 32-bit array indices.
  68757. * Leading zeroes (except '0' alone), plus/minus signs are not allowed.
  68758. * We could do a lot of prechecks here, but since most strings won't
  68759. * start with any digits, it's simpler to just parse the number and
  68760. * fail quickly.
  68761. */
  68762. res = 0;
  68763. if (blen == 0) {
  68764. goto parse_fail;
  68765. }
  68766. do {
  68767. duk_uarridx_t dig;
  68768. dig = (duk_uarridx_t) (*str++) - DUK_ASC_0;
  68769. if (dig <= 9U) {
  68770. /* Careful overflow handling. When multiplying by 10:
  68771. * - 0x19999998 x 10 = 0xfffffff0: no overflow, and adding
  68772. * 0...9 is safe.
  68773. * - 0x19999999 x 10 = 0xfffffffa: no overflow, adding
  68774. * 0...5 is safe, 6...9 overflows.
  68775. * - 0x1999999a x 10 = 0x100000004: always overflow.
  68776. */
  68777. if (DUK_UNLIKELY(res >= 0x19999999UL)) {
  68778. if (res >= 0x1999999aUL) {
  68779. /* Always overflow. */
  68780. goto parse_fail;
  68781. }
  68782. DUK_ASSERT(res == 0x19999999UL);
  68783. if (dig >= 6U) {
  68784. goto parse_fail;
  68785. }
  68786. res = 0xfffffffaUL + dig;
  68787. DUK_ASSERT(res >= 0xfffffffaUL);
  68788. DUK_ASSERT_DISABLE(res <= 0xffffffffUL); /* range */
  68789. } else {
  68790. res = res * 10U + dig;
  68791. if (DUK_UNLIKELY(res == 0)) {
  68792. /* If 'res' is 0, previous 'res' must
  68793. * have been 0 and we scanned in a zero.
  68794. * This is only allowed if blen == 1,
  68795. * i.e. the exact string '0'.
  68796. */
  68797. if (blen == (duk_uint32_t) 1) {
  68798. return 0;
  68799. }
  68800. goto parse_fail;
  68801. }
  68802. }
  68803. } else {
  68804. /* Because 'dig' is unsigned, catches both values
  68805. * above '9' and below '0'.
  68806. */
  68807. goto parse_fail;
  68808. }
  68809. } while (--blen > 0);
  68810. return res;
  68811. parse_fail:
  68812. return DUK_HSTRING_NO_ARRAY_INDEX;
  68813. }
  68814. #if !defined(DUK_USE_HSTRING_ARRIDX)
  68815. /* Get array index for a string which is known to be an array index. This helper
  68816. * is needed when duk_hstring doesn't concretely store the array index, but strings
  68817. * are flagged as array indices at intern time.
  68818. */
  68819. DUK_INTERNAL duk_uarridx_t duk_js_to_arrayindex_hstring_fast_known(duk_hstring *h) {
  68820. const duk_uint8_t *p;
  68821. duk_uarridx_t res;
  68822. duk_uint8_t t;
  68823. DUK_ASSERT(h != NULL);
  68824. DUK_ASSERT(DUK_HSTRING_HAS_ARRIDX(h));
  68825. p = DUK_HSTRING_GET_DATA(h);
  68826. res = 0;
  68827. for (;;) {
  68828. t = *p++;
  68829. if (DUK_UNLIKELY(t == 0)) {
  68830. /* Scanning to NUL is always safe for interned strings. */
  68831. break;
  68832. }
  68833. DUK_ASSERT(t >= (duk_uint8_t) DUK_ASC_0 && t <= (duk_uint8_t) DUK_ASC_9);
  68834. res = res * 10U + (duk_uarridx_t) t - (duk_uarridx_t) DUK_ASC_0;
  68835. }
  68836. return res;
  68837. }
  68838. DUK_INTERNAL duk_uarridx_t duk_js_to_arrayindex_hstring_fast(duk_hstring *h) {
  68839. DUK_ASSERT(h != NULL);
  68840. if (!DUK_HSTRING_HAS_ARRIDX(h)) {
  68841. return DUK_HSTRING_NO_ARRAY_INDEX;
  68842. }
  68843. return duk_js_to_arrayindex_hstring_fast_known(h);
  68844. }
  68845. #endif /* DUK_USE_HSTRING_ARRIDX */
  68846. #line 1 "duk_js_var.c"
  68847. /*
  68848. * Identifier access and function closure handling.
  68849. *
  68850. * Provides the primitives for slow path identifier accesses: GETVAR,
  68851. * PUTVAR, DELVAR, etc. The fast path, direct register accesses, should
  68852. * be used for most identifier accesses. Consequently, these slow path
  68853. * primitives should be optimized for maximum compactness.
  68854. *
  68855. * ECMAScript environment records (declarative and object) are represented
  68856. * as internal objects with control keys. Environment records have a
  68857. * parent record ("outer environment reference") which is represented by
  68858. * the implicit prototype for technical reasons (in other words, it is a
  68859. * convenient field). The prototype chain is not followed in the ordinary
  68860. * sense for variable lookups.
  68861. *
  68862. * See identifier-handling.rst for more details on the identifier algorithms
  68863. * and the internal representation. See function-objects.rst for details on
  68864. * what function templates and instances are expected to look like.
  68865. *
  68866. * Care must be taken to avoid duk_tval pointer invalidation caused by
  68867. * e.g. value stack or object resizing.
  68868. *
  68869. * TODO: properties for function instances could be initialized much more
  68870. * efficiently by creating a property allocation for a certain size and
  68871. * filling in keys and values directly (and INCREFing both with "bulk incref"
  68872. * primitives.
  68873. *
  68874. * XXX: duk_hobject_getprop() and duk_hobject_putprop() calls are a bit
  68875. * awkward (especially because they follow the prototype chain); rework
  68876. * if "raw" own property helpers are added.
  68877. */
  68878. /* #include duk_internal.h -> already included */
  68879. /*
  68880. * Local result type for duk__get_identifier_reference() lookup.
  68881. */
  68882. typedef struct {
  68883. duk_hobject *env;
  68884. duk_hobject *holder; /* for object-bound identifiers */
  68885. duk_tval *value; /* for register-bound and declarative env identifiers */
  68886. duk_uint_t attrs; /* property attributes for identifier (relevant if value != NULL) */
  68887. duk_bool_t has_this; /* for object-bound identifiers: provide 'this' binding */
  68888. } duk__id_lookup_result;
  68889. /*
  68890. * Create a new function object based on a "template function" which contains
  68891. * compiled bytecode, constants, etc, but lacks a lexical environment.
  68892. *
  68893. * ECMAScript requires that each created closure is a separate object, with
  68894. * its own set of editable properties. However, structured property values
  68895. * (such as the formal arguments list and the variable map) are shared.
  68896. * Also the bytecode, constants, and inner functions are shared.
  68897. *
  68898. * See E5 Section 13.2 for detailed requirements on the function objects;
  68899. * there are no similar requirements for function "templates" which are an
  68900. * implementation dependent internal feature. Also see function-objects.rst
  68901. * for a discussion on the function instance properties provided by this
  68902. * implementation.
  68903. *
  68904. * Notes:
  68905. *
  68906. * * Order of internal properties should match frequency of use, since the
  68907. * properties will be linearly scanned on lookup (functions usually don't
  68908. * have enough properties to warrant a hash part).
  68909. *
  68910. * * The created closure is independent of its template; they do share the
  68911. * same 'data' buffer object, but the template object itself can be freed
  68912. * even if the closure object remains reachable.
  68913. */
  68914. DUK_LOCAL void duk__inc_data_inner_refcounts(duk_hthread *thr, duk_hcompfunc *f) {
  68915. duk_tval *tv, *tv_end;
  68916. duk_hobject **funcs, **funcs_end;
  68917. DUK_UNREF(thr);
  68918. /* If function creation fails due to out-of-memory, the data buffer
  68919. * pointer may be NULL in some cases. That's actually possible for
  68920. * GC code, but shouldn't be possible here because the incomplete
  68921. * function will be unwound from the value stack and never instantiated.
  68922. */
  68923. DUK_ASSERT(DUK_HCOMPFUNC_GET_DATA(thr->heap, f) != NULL);
  68924. tv = DUK_HCOMPFUNC_GET_CONSTS_BASE(thr->heap, f);
  68925. tv_end = DUK_HCOMPFUNC_GET_CONSTS_END(thr->heap, f);
  68926. while (tv < tv_end) {
  68927. DUK_TVAL_INCREF(thr, tv);
  68928. tv++;
  68929. }
  68930. funcs = DUK_HCOMPFUNC_GET_FUNCS_BASE(thr->heap, f);
  68931. funcs_end = DUK_HCOMPFUNC_GET_FUNCS_END(thr->heap, f);
  68932. while (funcs < funcs_end) {
  68933. DUK_HEAPHDR_INCREF(thr, (duk_heaphdr *) *funcs);
  68934. funcs++;
  68935. }
  68936. }
  68937. /* Push a new closure on the stack.
  68938. *
  68939. * Note: if fun_temp has NEWENV, i.e. a new lexical and variable declaration
  68940. * is created when the function is called, only outer_lex_env matters
  68941. * (outer_var_env is ignored and may or may not be same as outer_lex_env).
  68942. */
  68943. DUK_LOCAL const duk_uint16_t duk__closure_copy_proplist[] = {
  68944. /* order: most frequent to least frequent */
  68945. DUK_STRIDX_INT_VARMAP,
  68946. DUK_STRIDX_INT_FORMALS,
  68947. #if defined(DUK_USE_PC2LINE)
  68948. DUK_STRIDX_INT_PC2LINE,
  68949. #endif
  68950. #if defined(DUK_USE_FUNC_FILENAME_PROPERTY)
  68951. DUK_STRIDX_FILE_NAME,
  68952. #endif
  68953. #if defined(DUK_USE_NONSTD_FUNC_SOURCE_PROPERTY)
  68954. DUK_STRIDX_INT_SOURCE
  68955. #endif
  68956. };
  68957. DUK_INTERNAL
  68958. void duk_js_push_closure(duk_hthread *thr,
  68959. duk_hcompfunc *fun_temp,
  68960. duk_hobject *outer_var_env,
  68961. duk_hobject *outer_lex_env,
  68962. duk_bool_t add_auto_proto) {
  68963. duk_hcompfunc *fun_clos;
  68964. duk_small_uint_t i;
  68965. duk_uint_t len_value;
  68966. DUK_ASSERT(fun_temp != NULL);
  68967. DUK_ASSERT(DUK_HCOMPFUNC_GET_DATA(thr->heap, fun_temp) != NULL);
  68968. DUK_ASSERT(DUK_HCOMPFUNC_GET_FUNCS(thr->heap, fun_temp) != NULL);
  68969. DUK_ASSERT(DUK_HCOMPFUNC_GET_BYTECODE(thr->heap, fun_temp) != NULL);
  68970. DUK_ASSERT(outer_var_env != NULL);
  68971. DUK_ASSERT(outer_lex_env != NULL);
  68972. DUK_UNREF(len_value);
  68973. fun_clos = duk_push_hcompfunc(thr);
  68974. DUK_ASSERT(fun_clos != NULL);
  68975. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) fun_clos) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  68976. duk_push_hobject(thr, &fun_temp->obj); /* -> [ ... closure template ] */
  68977. DUK_ASSERT(DUK_HOBJECT_IS_COMPFUNC((duk_hobject *) fun_clos));
  68978. DUK_ASSERT(DUK_HCOMPFUNC_GET_DATA(thr->heap, fun_clos) == NULL);
  68979. DUK_ASSERT(DUK_HCOMPFUNC_GET_FUNCS(thr->heap, fun_clos) == NULL);
  68980. DUK_ASSERT(DUK_HCOMPFUNC_GET_BYTECODE(thr->heap, fun_clos) == NULL);
  68981. DUK_HCOMPFUNC_SET_DATA(thr->heap, fun_clos, DUK_HCOMPFUNC_GET_DATA(thr->heap, fun_temp));
  68982. DUK_HCOMPFUNC_SET_FUNCS(thr->heap, fun_clos, DUK_HCOMPFUNC_GET_FUNCS(thr->heap, fun_temp));
  68983. DUK_HCOMPFUNC_SET_BYTECODE(thr->heap, fun_clos, DUK_HCOMPFUNC_GET_BYTECODE(thr->heap, fun_temp));
  68984. /* Note: all references inside 'data' need to get their refcounts
  68985. * upped too. This is the case because refcounts are decreased
  68986. * through every function referencing 'data' independently.
  68987. */
  68988. DUK_HBUFFER_INCREF(thr, DUK_HCOMPFUNC_GET_DATA(thr->heap, fun_clos));
  68989. duk__inc_data_inner_refcounts(thr, fun_temp);
  68990. fun_clos->nregs = fun_temp->nregs;
  68991. fun_clos->nargs = fun_temp->nargs;
  68992. #if defined(DUK_USE_DEBUGGER_SUPPORT)
  68993. fun_clos->start_line = fun_temp->start_line;
  68994. fun_clos->end_line = fun_temp->end_line;
  68995. #endif
  68996. DUK_ASSERT(DUK_HCOMPFUNC_GET_DATA(thr->heap, fun_clos) != NULL);
  68997. DUK_ASSERT(DUK_HCOMPFUNC_GET_FUNCS(thr->heap, fun_clos) != NULL);
  68998. DUK_ASSERT(DUK_HCOMPFUNC_GET_BYTECODE(thr->heap, fun_clos) != NULL);
  68999. /* XXX: Could also copy from template, but there's no way to have any
  69000. * other value here now (used code has no access to the template).
  69001. * Prototype is set by duk_push_hcompfunc().
  69002. */
  69003. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, &fun_clos->obj) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  69004. #if 0
  69005. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, &fun_clos->obj, thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  69006. #endif
  69007. /* Copy duk_hobject flags as is from the template using a mask.
  69008. * Leave out duk_heaphdr owned flags just in case (e.g. if there's
  69009. * some GC flag or similar). Some flags can then be adjusted
  69010. * separately if necessary.
  69011. */
  69012. /* DUK_HEAPHDR_SET_FLAGS() masks changes to non-duk_heaphdr flags only. */
  69013. DUK_HEAPHDR_SET_FLAGS((duk_heaphdr *) fun_clos, DUK_HEAPHDR_GET_FLAGS_RAW((duk_heaphdr *) fun_temp));
  69014. DUK_DD(DUK_DDPRINT("fun_temp heaphdr flags: 0x%08lx, fun_clos heaphdr flags: 0x%08lx",
  69015. (unsigned long) DUK_HEAPHDR_GET_FLAGS_RAW((duk_heaphdr *) fun_temp),
  69016. (unsigned long) DUK_HEAPHDR_GET_FLAGS_RAW((duk_heaphdr *) fun_clos)));
  69017. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(&fun_clos->obj));
  69018. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(&fun_clos->obj));
  69019. DUK_ASSERT(DUK_HOBJECT_HAS_COMPFUNC(&fun_clos->obj));
  69020. DUK_ASSERT(!DUK_HOBJECT_HAS_NATFUNC(&fun_clos->obj));
  69021. DUK_ASSERT(!DUK_HOBJECT_IS_THREAD(&fun_clos->obj));
  69022. /* DUK_HOBJECT_FLAG_ARRAY_PART: don't care */
  69023. /* DUK_HOBJECT_FLAG_NEWENV: handled below */
  69024. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(&fun_clos->obj));
  69025. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_STRINGOBJ(&fun_clos->obj));
  69026. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARGUMENTS(&fun_clos->obj));
  69027. if (!DUK_HOBJECT_HAS_CONSTRUCTABLE(&fun_clos->obj)) {
  69028. /* If the template is not constructable don't add an automatic
  69029. * .prototype property. This is the case for e.g. ES2015 object
  69030. * literal getters/setters and method definitions.
  69031. */
  69032. add_auto_proto = 0;
  69033. }
  69034. /*
  69035. * Setup environment record properties based on the template and
  69036. * its flags.
  69037. *
  69038. * If DUK_HOBJECT_HAS_NEWENV(fun_temp) is true, the environment
  69039. * records represent identifiers "outside" the function; the
  69040. * "inner" environment records are created on demand. Otherwise,
  69041. * the environment records are those that will be directly used
  69042. * (e.g. for declarations).
  69043. *
  69044. * _Lexenv is always set; _Varenv defaults to _Lexenv if missing,
  69045. * so _Varenv is only set if _Lexenv != _Varenv.
  69046. *
  69047. * This is relatively complex, see doc/identifier-handling.rst.
  69048. */
  69049. if (DUK_HOBJECT_HAS_NEWENV(&fun_clos->obj)) {
  69050. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  69051. if (DUK_HOBJECT_HAS_NAMEBINDING(&fun_clos->obj)) {
  69052. duk_hobject *proto;
  69053. duk_hdecenv *new_env;
  69054. /*
  69055. * Named function expression, name needs to be bound
  69056. * in an intermediate environment record. The "outer"
  69057. * lexical/variable environment will thus be:
  69058. *
  69059. * a) { funcname: <func>, __prototype: outer_lex_env }
  69060. * b) { funcname: <func>, __prototype: <globalenv> } (if outer_lex_env missing)
  69061. */
  69062. if (outer_lex_env) {
  69063. proto = outer_lex_env;
  69064. } else {
  69065. proto = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  69066. }
  69067. /* -> [ ... closure template env ] */
  69068. new_env = duk_hdecenv_alloc(thr,
  69069. DUK_HOBJECT_FLAG_EXTENSIBLE |
  69070. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV));
  69071. DUK_ASSERT(new_env != NULL);
  69072. duk_push_hobject(thr, (duk_hobject *) new_env);
  69073. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) new_env) == NULL);
  69074. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, (duk_hobject *) new_env, proto);
  69075. DUK_HOBJECT_INCREF_ALLOWNULL(thr, proto);
  69076. DUK_ASSERT(new_env->thread == NULL); /* Closed. */
  69077. DUK_ASSERT(new_env->varmap == NULL);
  69078. /* It's important that duk_xdef_prop() is a 'raw define' so that any
  69079. * properties in an ancestor are never an issue (they should never be
  69080. * e.g. non-writable, but just in case).
  69081. *
  69082. * Because template objects are not visible to user code, the case
  69083. * where .name is missing shouldn't happen in practice. It it does,
  69084. * the name 'undefined' gets bound and maps to the closure (which is
  69085. * a bit odd, but safe).
  69086. */
  69087. (void) duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_NAME);
  69088. /* -> [ ... closure template env funcname ] */
  69089. duk_dup_m4(thr); /* -> [ ... closure template env funcname closure ] */
  69090. duk_xdef_prop(thr, -3, DUK_PROPDESC_FLAGS_NONE); /* -> [ ... closure template env ] */
  69091. /* env[funcname] = closure */
  69092. /* [ ... closure template env ] */
  69093. DUK_HCOMPFUNC_SET_LEXENV(thr->heap, fun_clos, (duk_hobject *) new_env);
  69094. DUK_HCOMPFUNC_SET_VARENV(thr->heap, fun_clos, (duk_hobject *) new_env);
  69095. DUK_HOBJECT_INCREF(thr, (duk_hobject *) new_env);
  69096. DUK_HOBJECT_INCREF(thr, (duk_hobject *) new_env);
  69097. duk_pop_unsafe(thr);
  69098. /* [ ... closure template ] */
  69099. }
  69100. else
  69101. #endif /* DUK_USE_FUNC_NAME_PROPERTY */
  69102. {
  69103. /*
  69104. * Other cases (function declaration, anonymous function expression,
  69105. * strict direct eval code). The "outer" environment will be whatever
  69106. * the caller gave us.
  69107. */
  69108. DUK_HCOMPFUNC_SET_LEXENV(thr->heap, fun_clos, outer_lex_env);
  69109. DUK_HCOMPFUNC_SET_VARENV(thr->heap, fun_clos, outer_lex_env);
  69110. DUK_HOBJECT_INCREF(thr, outer_lex_env);
  69111. DUK_HOBJECT_INCREF(thr, outer_lex_env);
  69112. /* [ ... closure template ] */
  69113. }
  69114. } else {
  69115. /*
  69116. * Function gets no new environment when called. This is the
  69117. * case for global code, indirect eval code, and non-strict
  69118. * direct eval code. There is no direct correspondence to the
  69119. * E5 specification, as global/eval code is not exposed as a
  69120. * function.
  69121. */
  69122. DUK_ASSERT(!DUK_HOBJECT_HAS_NAMEBINDING(&fun_temp->obj));
  69123. DUK_HCOMPFUNC_SET_LEXENV(thr->heap, fun_clos, outer_lex_env);
  69124. DUK_HCOMPFUNC_SET_VARENV(thr->heap, fun_clos, outer_var_env);
  69125. DUK_HOBJECT_INCREF(thr, outer_lex_env); /* NULLs not allowed; asserted on entry */
  69126. DUK_HOBJECT_INCREF(thr, outer_var_env);
  69127. }
  69128. DUK_DDD(DUK_DDDPRINT("closure varenv -> %!ipO, lexenv -> %!ipO",
  69129. (duk_heaphdr *) fun_clos->var_env,
  69130. (duk_heaphdr *) fun_clos->lex_env));
  69131. /* Call handling assumes this for all callable closures. */
  69132. DUK_ASSERT(DUK_HCOMPFUNC_GET_LEXENV(thr->heap, fun_clos) != NULL);
  69133. DUK_ASSERT(DUK_HCOMPFUNC_GET_VARENV(thr->heap, fun_clos) != NULL);
  69134. /*
  69135. * Copy some internal properties directly
  69136. *
  69137. * The properties will be non-writable and non-enumerable, but
  69138. * configurable.
  69139. */
  69140. /* [ ... closure template ] */
  69141. DUK_DDD(DUK_DDDPRINT("copying properties: closure=%!iT, template=%!iT",
  69142. (duk_tval *) duk_get_tval(thr, -2),
  69143. (duk_tval *) duk_get_tval(thr, -1)));
  69144. for (i = 0; i < (duk_small_uint_t) (sizeof(duk__closure_copy_proplist) / sizeof(duk_uint16_t)); i++) {
  69145. duk_small_int_t stridx = (duk_small_int_t) duk__closure_copy_proplist[i];
  69146. if (duk_get_prop_stridx_short(thr, -1, stridx)) {
  69147. /* [ ... closure template val ] */
  69148. DUK_DDD(DUK_DDDPRINT("copying property, stridx=%ld -> found", (long) stridx));
  69149. duk_xdef_prop_stridx_short(thr, -3, stridx, DUK_PROPDESC_FLAGS_C);
  69150. } else {
  69151. DUK_DDD(DUK_DDDPRINT("copying property, stridx=%ld -> not found", (long) stridx));
  69152. duk_pop_unsafe(thr);
  69153. }
  69154. }
  69155. /*
  69156. * "length" maps to number of formals (E5 Section 13.2) for function
  69157. * declarations/expressions (non-bound functions). Note that 'nargs'
  69158. * is NOT necessarily equal to the number of arguments. Use length
  69159. * of _Formals; if missing, assume nargs matches .length.
  69160. */
  69161. /* [ ... closure template ] */
  69162. /* XXX: these lookups should be just own property lookups instead of
  69163. * looking up the inheritance chain.
  69164. */
  69165. if (duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_INT_FORMALS)) {
  69166. /* [ ... closure template formals ] */
  69167. len_value = (duk_uint_t) duk_get_length(thr, -1); /* could access duk_harray directly, not important */
  69168. DUK_DD(DUK_DDPRINT("closure length from _Formals -> %ld", (long) len_value));
  69169. } else {
  69170. len_value = fun_temp->nargs;
  69171. DUK_DD(DUK_DDPRINT("closure length defaulted from nargs -> %ld", (long) len_value));
  69172. }
  69173. duk_pop_unsafe(thr);
  69174. duk_push_uint(thr, len_value); /* [ ... closure template len_value ] */
  69175. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_LENGTH, DUK_PROPDESC_FLAGS_C);
  69176. /*
  69177. * "prototype" is, by default, a fresh object with the "constructor"
  69178. * property.
  69179. *
  69180. * Note that this creates a circular reference for every function
  69181. * instance (closure) which prevents refcount-based collection of
  69182. * function instances.
  69183. *
  69184. * XXX: Try to avoid creating the default prototype object, because
  69185. * many functions are not used as constructors and the default
  69186. * prototype is unnecessary. Perhaps it could be created on-demand
  69187. * when it is first accessed?
  69188. */
  69189. /* [ ... closure template ] */
  69190. if (add_auto_proto) {
  69191. duk_push_object(thr); /* -> [ ... closure template newobj ] */
  69192. duk_dup_m3(thr); /* -> [ ... closure template newobj closure ] */
  69193. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_CONSTRUCTOR, DUK_PROPDESC_FLAGS_WC); /* -> [ ... closure template newobj ] */
  69194. duk_compact(thr, -1); /* compact the prototype */
  69195. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_PROTOTYPE, DUK_PROPDESC_FLAGS_W); /* -> [ ... closure template ] */
  69196. }
  69197. /*
  69198. * "arguments" and "caller" must be mapped to throwers for strict
  69199. * mode and bound functions (E5 Section 15.3.5).
  69200. *
  69201. * XXX: This is expensive to have for every strict function instance.
  69202. * Try to implement as virtual properties or on-demand created properties.
  69203. */
  69204. /* [ ... closure template ] */
  69205. if (DUK_HOBJECT_HAS_STRICT(&fun_clos->obj)) {
  69206. duk_xdef_prop_stridx_thrower(thr, -2, DUK_STRIDX_CALLER);
  69207. duk_xdef_prop_stridx_thrower(thr, -2, DUK_STRIDX_LC_ARGUMENTS);
  69208. } else {
  69209. #if defined(DUK_USE_NONSTD_FUNC_CALLER_PROPERTY)
  69210. DUK_DDD(DUK_DDDPRINT("function is non-strict and non-standard 'caller' property in use, add initial 'null' value"));
  69211. duk_push_null(thr);
  69212. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_CALLER, DUK_PROPDESC_FLAGS_NONE);
  69213. #else
  69214. DUK_DDD(DUK_DDDPRINT("function is non-strict and non-standard 'caller' property not used"));
  69215. #endif
  69216. }
  69217. /*
  69218. * "name" used to be non-standard but is now defined by ES2015.
  69219. * In ES2015/ES2016 the .name property is configurable.
  69220. */
  69221. /* [ ... closure template ] */
  69222. #if defined(DUK_USE_FUNC_NAME_PROPERTY)
  69223. /* XXX: Look for own property only; doesn't matter much because
  69224. * templates are bare objects.
  69225. */
  69226. if (duk_get_prop_stridx_short(thr, -1, DUK_STRIDX_NAME)) {
  69227. /* [ ... closure template name ] */
  69228. DUK_ASSERT(duk_is_string(thr, -1));
  69229. DUK_DD(DUK_DDPRINT("setting function instance name to %!T", duk_get_tval(thr, -1)));
  69230. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_NAME, DUK_PROPDESC_FLAGS_C); /* -> [ ... closure template ] */
  69231. } else {
  69232. /* Anonymous functions don't have a .name in ES2015, so don't set
  69233. * it on the instance either. The instance will then inherit
  69234. * it from Function.prototype.name.
  69235. */
  69236. DUK_DD(DUK_DDPRINT("not setting function instance .name"));
  69237. duk_pop_unsafe(thr);
  69238. }
  69239. #endif
  69240. /*
  69241. * Compact the closure, in most cases no properties will be added later.
  69242. * Also, without this the closures end up having unused property slots
  69243. * (e.g. in Duktape 0.9.0, 8 slots would be allocated and only 7 used).
  69244. * A better future solution would be to allocate the closure directly
  69245. * to correct size (and setup the properties directly without going
  69246. * through the API).
  69247. */
  69248. duk_compact(thr, -2);
  69249. /*
  69250. * Some assertions (E5 Section 13.2).
  69251. */
  69252. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(&fun_clos->obj) == DUK_HOBJECT_CLASS_FUNCTION);
  69253. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, &fun_clos->obj) == thr->builtins[DUK_BIDX_FUNCTION_PROTOTYPE]);
  69254. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(&fun_clos->obj));
  69255. DUK_ASSERT(duk_has_prop_stridx(thr, -2, DUK_STRIDX_LENGTH) != 0);
  69256. DUK_ASSERT(add_auto_proto == 0 || duk_has_prop_stridx(thr, -2, DUK_STRIDX_PROTOTYPE) != 0);
  69257. /* May be missing .name */
  69258. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(&fun_clos->obj) ||
  69259. duk_has_prop_stridx(thr, -2, DUK_STRIDX_CALLER) != 0);
  69260. DUK_ASSERT(!DUK_HOBJECT_HAS_STRICT(&fun_clos->obj) ||
  69261. duk_has_prop_stridx(thr, -2, DUK_STRIDX_LC_ARGUMENTS) != 0);
  69262. /*
  69263. * Finish
  69264. */
  69265. /* [ ... closure template ] */
  69266. DUK_DDD(DUK_DDDPRINT("created function instance: template=%!iT -> closure=%!iT",
  69267. (duk_tval *) duk_get_tval(thr, -1),
  69268. (duk_tval *) duk_get_tval(thr, -2)));
  69269. duk_pop_unsafe(thr);
  69270. /* [ ... closure ] */
  69271. }
  69272. /*
  69273. * Delayed activation environment record initialization (for functions
  69274. * with NEWENV).
  69275. *
  69276. * The non-delayed initialization is handled by duk_handle_call().
  69277. */
  69278. /* shared helper */
  69279. DUK_INTERNAL
  69280. duk_hobject *duk_create_activation_environment_record(duk_hthread *thr,
  69281. duk_hobject *func,
  69282. duk_size_t bottom_byteoff) {
  69283. duk_hdecenv *env;
  69284. duk_hobject *parent;
  69285. duk_hcompfunc *f;
  69286. DUK_ASSERT(thr != NULL);
  69287. DUK_ASSERT(func != NULL);
  69288. f = (duk_hcompfunc *) func;
  69289. parent = DUK_HCOMPFUNC_GET_LEXENV(thr->heap, f);
  69290. if (!parent) {
  69291. parent = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  69292. }
  69293. env = duk_hdecenv_alloc(thr,
  69294. DUK_HOBJECT_FLAG_EXTENSIBLE |
  69295. DUK_HOBJECT_CLASS_AS_FLAGS(DUK_HOBJECT_CLASS_DECENV));
  69296. DUK_ASSERT(env != NULL);
  69297. duk_push_hobject(thr, (duk_hobject *) env);
  69298. DUK_ASSERT(DUK_HOBJECT_GET_PROTOTYPE(thr->heap, (duk_hobject *) env) == NULL);
  69299. DUK_HOBJECT_SET_PROTOTYPE(thr->heap, (duk_hobject *) env, parent);
  69300. DUK_HOBJECT_INCREF_ALLOWNULL(thr, parent); /* parent env is the prototype */
  69301. /* open scope information, for compiled functions only */
  69302. DUK_ASSERT(env->thread == NULL);
  69303. DUK_ASSERT(env->varmap == NULL);
  69304. DUK_ASSERT(env->regbase_byteoff == 0);
  69305. if (DUK_HOBJECT_IS_COMPFUNC(func)) {
  69306. duk_hobject *varmap;
  69307. duk_tval *tv;
  69308. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  69309. if (tv != NULL && DUK_TVAL_IS_OBJECT(tv)) {
  69310. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  69311. varmap = DUK_TVAL_GET_OBJECT(tv);
  69312. DUK_ASSERT(varmap != NULL);
  69313. env->varmap = varmap;
  69314. DUK_HOBJECT_INCREF(thr, varmap);
  69315. env->thread = thr;
  69316. DUK_HTHREAD_INCREF(thr, thr);
  69317. env->regbase_byteoff = bottom_byteoff;
  69318. } else {
  69319. /* If function has no _Varmap, leave the environment closed. */
  69320. DUK_ASSERT(env->thread == NULL);
  69321. DUK_ASSERT(env->varmap == NULL);
  69322. DUK_ASSERT(env->regbase_byteoff == 0);
  69323. }
  69324. }
  69325. return (duk_hobject *) env;
  69326. }
  69327. DUK_INTERNAL
  69328. void duk_js_init_activation_environment_records_delayed(duk_hthread *thr,
  69329. duk_activation *act) {
  69330. duk_hobject *func;
  69331. duk_hobject *env;
  69332. DUK_ASSERT(thr != NULL);
  69333. func = DUK_ACT_GET_FUNC(act);
  69334. DUK_ASSERT(func != NULL);
  69335. DUK_ASSERT(!DUK_HOBJECT_HAS_BOUNDFUNC(func)); /* bound functions are never in act 'func' */
  69336. /*
  69337. * Delayed initialization only occurs for 'NEWENV' functions.
  69338. */
  69339. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  69340. DUK_ASSERT(act->lex_env == NULL);
  69341. DUK_ASSERT(act->var_env == NULL);
  69342. env = duk_create_activation_environment_record(thr, func, act->bottom_byteoff);
  69343. DUK_ASSERT(env != NULL);
  69344. /* 'act' is a stable pointer, so still OK. */
  69345. DUK_DDD(DUK_DDDPRINT("created delayed fresh env: %!ipO", (duk_heaphdr *) env));
  69346. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  69347. {
  69348. duk_hobject *p = env;
  69349. while (p) {
  69350. DUK_DDD(DUK_DDDPRINT(" -> %!ipO", (duk_heaphdr *) p));
  69351. p = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, p);
  69352. }
  69353. }
  69354. #endif
  69355. act->lex_env = env;
  69356. act->var_env = env;
  69357. DUK_HOBJECT_INCREF(thr, env); /* XXX: incref by count (here 2 times) */
  69358. DUK_HOBJECT_INCREF(thr, env);
  69359. duk_pop_unsafe(thr);
  69360. }
  69361. /*
  69362. * Closing environment records.
  69363. *
  69364. * The environment record MUST be closed with the thread where its activation
  69365. * is; i.e. if 'env' is open, 'thr' must match env->thread, and the regbase
  69366. * and varmap must still be valid. On entry, 'env' must be reachable.
  69367. */
  69368. DUK_INTERNAL void duk_js_close_environment_record(duk_hthread *thr, duk_hobject *env) {
  69369. duk_uint_fast32_t i;
  69370. duk_hobject *varmap;
  69371. duk_hstring *key;
  69372. duk_tval *tv;
  69373. duk_uint_t regnum;
  69374. DUK_ASSERT(thr != NULL);
  69375. DUK_ASSERT(env != NULL);
  69376. if (DUK_UNLIKELY(!DUK_HOBJECT_IS_DECENV(env))) {
  69377. DUK_DDD(DUK_DDDPRINT("env not a declarative record: %!iO", (duk_heaphdr *) env));
  69378. return;
  69379. }
  69380. varmap = ((duk_hdecenv *) env)->varmap;
  69381. if (varmap == NULL) {
  69382. DUK_DDD(DUK_DDDPRINT("env already closed: %!iO", (duk_heaphdr *) env));
  69383. return;
  69384. }
  69385. DUK_ASSERT(((duk_hdecenv *) env)->thread != NULL);
  69386. DUK_ASSERT_HDECENV_VALID((duk_hdecenv *) env);
  69387. DUK_DDD(DUK_DDDPRINT("closing env: %!iO", (duk_heaphdr *) env));
  69388. DUK_DDD(DUK_DDDPRINT("varmap: %!O", (duk_heaphdr *) varmap));
  69389. /* Env must be closed in the same thread as where it runs. */
  69390. DUK_ASSERT(((duk_hdecenv *) env)->thread == thr);
  69391. /* XXX: additional conditions when to close variables? we don't want to do it
  69392. * unless the environment may have "escaped" (referenced in a function closure).
  69393. * With delayed environments, the existence is probably good enough of a check.
  69394. */
  69395. /* Note: we rely on the _Varmap having a bunch of nice properties, like:
  69396. * - being compacted and unmodified during this process
  69397. * - not containing an array part
  69398. * - having correct value types
  69399. */
  69400. DUK_DDD(DUK_DDDPRINT("copying bound register values, %ld bound regs", (long) DUK_HOBJECT_GET_ENEXT(varmap)));
  69401. /* Copy over current variable values from value stack to the
  69402. * environment record. The scope object is empty but may
  69403. * inherit from another scope which has conflicting names.
  69404. */
  69405. /* XXX: Do this using a once allocated entry area, no side effects.
  69406. * Hash part would need special treatment however (maybe copy, and
  69407. * then realloc with hash part if large enough).
  69408. */
  69409. for (i = 0; i < (duk_uint_fast32_t) DUK_HOBJECT_GET_ENEXT(varmap); i++) {
  69410. duk_size_t regbase_byteoff;
  69411. key = DUK_HOBJECT_E_GET_KEY(thr->heap, varmap, i);
  69412. DUK_ASSERT(key != NULL); /* assume keys are compact in _Varmap */
  69413. DUK_ASSERT(!DUK_HOBJECT_E_SLOT_IS_ACCESSOR(thr->heap, varmap, i)); /* assume plain values */
  69414. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, varmap, i);
  69415. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  69416. DUK_ASSERT(DUK_TVAL_GET_NUMBER(tv) <= (duk_double_t) DUK_UINT32_MAX); /* limits */
  69417. #if defined(DUK_USE_FASTINT)
  69418. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  69419. regnum = (duk_uint_t) DUK_TVAL_GET_FASTINT_U32(tv);
  69420. #else
  69421. regnum = (duk_uint_t) DUK_TVAL_GET_NUMBER(tv);
  69422. #endif
  69423. regbase_byteoff = ((duk_hdecenv *) env)->regbase_byteoff;
  69424. DUK_ASSERT((duk_uint8_t *) thr->valstack + regbase_byteoff + sizeof(duk_tval) * regnum >= (duk_uint8_t *) thr->valstack);
  69425. DUK_ASSERT((duk_uint8_t *) thr->valstack + regbase_byteoff + sizeof(duk_tval) * regnum < (duk_uint8_t *) thr->valstack_top);
  69426. /* If property already exists, overwrites silently.
  69427. * Property is writable, but not deletable (not configurable
  69428. * in terms of property attributes).
  69429. */
  69430. duk_push_tval(thr, (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + regbase_byteoff + sizeof(duk_tval) * regnum));
  69431. DUK_DDD(DUK_DDDPRINT("closing identifier %!O -> reg %ld, value %!T",
  69432. (duk_heaphdr *) key,
  69433. (long) regnum,
  69434. (duk_tval *) duk_get_tval(thr, -1)));
  69435. duk_hobject_define_property_internal(thr, env, key, DUK_PROPDESC_FLAGS_WE);
  69436. }
  69437. /* NULL atomically to avoid inconsistent state + side effects. */
  69438. DUK_HOBJECT_DECREF_NORZ(thr, ((duk_hdecenv *) env)->thread);
  69439. DUK_HOBJECT_DECREF_NORZ(thr, ((duk_hdecenv *) env)->varmap);
  69440. ((duk_hdecenv *) env)->thread = NULL;
  69441. ((duk_hdecenv *) env)->varmap = NULL;
  69442. DUK_DDD(DUK_DDDPRINT("env after closing: %!O", (duk_heaphdr *) env));
  69443. }
  69444. /*
  69445. * GETIDREF: a GetIdentifierReference-like helper.
  69446. *
  69447. * Provides a parent traversing lookup and a single level lookup
  69448. * (for HasBinding).
  69449. *
  69450. * Instead of returning the value, returns a bunch of values allowing
  69451. * the caller to read, write, or delete the binding. Value pointers
  69452. * are duk_tval pointers which can be mutated directly as long as
  69453. * refcounts are properly updated. Note that any operation which may
  69454. * reallocate valstacks or compact objects may invalidate the returned
  69455. * duk_tval (but not object) pointers, so caller must be very careful.
  69456. *
  69457. * If starting environment record 'env' is given, 'act' is ignored.
  69458. * However, if 'env' is NULL, the caller may identify, in 'act', an
  69459. * activation which hasn't had its declarative environment initialized
  69460. * yet. The activation registers are then looked up, and its parent
  69461. * traversed normally.
  69462. *
  69463. * The 'out' structure values are only valid if the function returns
  69464. * success (non-zero).
  69465. */
  69466. /* lookup name from an open declarative record's registers */
  69467. DUK_LOCAL
  69468. duk_bool_t duk__getid_open_decl_env_regs(duk_hthread *thr,
  69469. duk_hstring *name,
  69470. duk_hdecenv *env,
  69471. duk__id_lookup_result *out) {
  69472. duk_tval *tv;
  69473. duk_size_t reg_rel;
  69474. DUK_ASSERT(thr != NULL);
  69475. DUK_ASSERT(name != NULL);
  69476. DUK_ASSERT(env != NULL);
  69477. DUK_ASSERT(out != NULL);
  69478. DUK_ASSERT(DUK_HOBJECT_IS_DECENV((duk_hobject *) env));
  69479. DUK_ASSERT_HDECENV_VALID(env);
  69480. if (env->thread == NULL) {
  69481. /* already closed */
  69482. return 0;
  69483. }
  69484. DUK_ASSERT(env->varmap != NULL);
  69485. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, env->varmap, name);
  69486. if (DUK_UNLIKELY(tv == NULL)) {
  69487. return 0;
  69488. }
  69489. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  69490. DUK_ASSERT(DUK_TVAL_GET_NUMBER(tv) <= (duk_double_t) DUK_UINT32_MAX); /* limits */
  69491. #if defined(DUK_USE_FASTINT)
  69492. DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));
  69493. reg_rel = (duk_size_t) DUK_TVAL_GET_FASTINT_U32(tv);
  69494. #else
  69495. reg_rel = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  69496. #endif
  69497. DUK_ASSERT_DISABLE(reg_rel >= 0); /* unsigned */
  69498. tv = (duk_tval *) (void *) ((duk_uint8_t *) env->thread->valstack + env->regbase_byteoff + sizeof(duk_tval) * reg_rel);
  69499. DUK_ASSERT(tv >= env->thread->valstack && tv < env->thread->valstack_end); /* XXX: more accurate? */
  69500. out->value = tv;
  69501. out->attrs = DUK_PROPDESC_FLAGS_W; /* registers are mutable, non-deletable */
  69502. out->env = (duk_hobject *) env;
  69503. out->holder = NULL;
  69504. out->has_this = 0;
  69505. return 1;
  69506. }
  69507. /* lookup name from current activation record's functions' registers */
  69508. DUK_LOCAL
  69509. duk_bool_t duk__getid_activation_regs(duk_hthread *thr,
  69510. duk_hstring *name,
  69511. duk_activation *act,
  69512. duk__id_lookup_result *out) {
  69513. duk_tval *tv;
  69514. duk_hobject *func;
  69515. duk_hobject *varmap;
  69516. duk_size_t reg_rel;
  69517. DUK_ASSERT(thr != NULL);
  69518. DUK_ASSERT(name != NULL);
  69519. DUK_ASSERT(act != NULL);
  69520. DUK_ASSERT(out != NULL);
  69521. func = DUK_ACT_GET_FUNC(act);
  69522. DUK_ASSERT(func != NULL);
  69523. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  69524. if (!DUK_HOBJECT_IS_COMPFUNC(func)) {
  69525. return 0;
  69526. }
  69527. /* XXX: move varmap to duk_hcompfunc struct field. */
  69528. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, func, DUK_HTHREAD_STRING_INT_VARMAP(thr));
  69529. if (!tv) {
  69530. return 0;
  69531. }
  69532. DUK_ASSERT(DUK_TVAL_IS_OBJECT(tv));
  69533. varmap = DUK_TVAL_GET_OBJECT(tv);
  69534. DUK_ASSERT(varmap != NULL);
  69535. tv = duk_hobject_find_existing_entry_tval_ptr(thr->heap, varmap, name);
  69536. if (!tv) {
  69537. return 0;
  69538. }
  69539. DUK_ASSERT(DUK_TVAL_IS_NUMBER(tv));
  69540. reg_rel = (duk_size_t) DUK_TVAL_GET_NUMBER(tv);
  69541. DUK_ASSERT_DISABLE(reg_rel >= 0);
  69542. DUK_ASSERT(reg_rel < ((duk_hcompfunc *) func)->nregs);
  69543. tv = (duk_tval *) (void *) ((duk_uint8_t *) thr->valstack + act->bottom_byteoff);
  69544. tv += reg_rel;
  69545. out->value = tv;
  69546. out->attrs = DUK_PROPDESC_FLAGS_W; /* registers are mutable, non-deletable */
  69547. out->env = NULL;
  69548. out->holder = NULL;
  69549. out->has_this = 0;
  69550. return 1;
  69551. }
  69552. DUK_LOCAL
  69553. duk_bool_t duk__get_identifier_reference(duk_hthread *thr,
  69554. duk_hobject *env,
  69555. duk_hstring *name,
  69556. duk_activation *act,
  69557. duk_bool_t parents,
  69558. duk__id_lookup_result *out) {
  69559. duk_tval *tv;
  69560. duk_uint_t sanity;
  69561. DUK_ASSERT(thr != NULL);
  69562. DUK_ASSERT(env != NULL || act != NULL);
  69563. DUK_ASSERT(name != NULL);
  69564. DUK_ASSERT(out != NULL);
  69565. DUK_ASSERT(!env || DUK_HOBJECT_IS_ENV(env));
  69566. DUK_ASSERT(!env || !DUK_HOBJECT_HAS_ARRAY_PART(env));
  69567. /*
  69568. * Conceptually, we look for the identifier binding by starting from
  69569. * 'env' and following to chain of environment records (represented
  69570. * by the prototype chain).
  69571. *
  69572. * If 'env' is NULL, the current activation does not yet have an
  69573. * allocated declarative environment record; this should be treated
  69574. * exactly as if the environment record existed but had no bindings
  69575. * other than register bindings.
  69576. *
  69577. * Note: we assume that with the DUK_HOBJECT_FLAG_NEWENV cleared
  69578. * the environment will always be initialized immediately; hence
  69579. * a NULL 'env' should only happen with the flag set. This is the
  69580. * case for: (1) function calls, and (2) strict, direct eval calls.
  69581. */
  69582. if (env == NULL && act != NULL) {
  69583. duk_hobject *func;
  69584. duk_hcompfunc *f;
  69585. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference: env is NULL, activation is non-NULL -> "
  69586. "delayed env case, look up activation regs first"));
  69587. /*
  69588. * Try registers
  69589. */
  69590. if (duk__getid_activation_regs(thr, name, act, out)) {
  69591. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  69592. "name=%!O -> value=%!T, attrs=%ld, has_this=%ld, env=%!O, holder=%!O "
  69593. "(found from register bindings when env=NULL)",
  69594. (duk_heaphdr *) name, (duk_tval *) out->value,
  69595. (long) out->attrs, (long) out->has_this,
  69596. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  69597. return 1;
  69598. }
  69599. DUK_DDD(DUK_DDDPRINT("not found in current activation regs"));
  69600. /*
  69601. * Not found in registers, proceed to the parent record.
  69602. * Here we need to determine what the parent would be,
  69603. * if 'env' was not NULL (i.e. same logic as when initializing
  69604. * the record).
  69605. *
  69606. * Note that environment initialization is only deferred when
  69607. * DUK_HOBJECT_HAS_NEWENV is set, and this only happens for:
  69608. * - Function code
  69609. * - Strict eval code
  69610. *
  69611. * We only need to check _Lexenv here; _Varenv exists only if it
  69612. * differs from _Lexenv (and thus _Lexenv will also be present).
  69613. */
  69614. if (!parents) {
  69615. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference failed, no parent traversal "
  69616. "(not found from register bindings when env=NULL)"));
  69617. goto fail_not_found;
  69618. }
  69619. func = DUK_ACT_GET_FUNC(act);
  69620. DUK_ASSERT(func != NULL);
  69621. DUK_ASSERT(DUK_HOBJECT_HAS_NEWENV(func));
  69622. f = (duk_hcompfunc *) func;
  69623. env = DUK_HCOMPFUNC_GET_LEXENV(thr->heap, f);
  69624. if (!env) {
  69625. env = thr->builtins[DUK_BIDX_GLOBAL_ENV];
  69626. }
  69627. DUK_DDD(DUK_DDDPRINT("continue lookup from env: %!iO",
  69628. (duk_heaphdr *) env));
  69629. }
  69630. /*
  69631. * Prototype walking starting from 'env'.
  69632. *
  69633. * ('act' is not needed anywhere here.)
  69634. */
  69635. sanity = DUK_HOBJECT_PROTOTYPE_CHAIN_SANITY;
  69636. while (env != NULL) {
  69637. duk_small_uint_t cl;
  69638. duk_uint_t attrs;
  69639. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference, name=%!O, considering env=%p -> %!iO",
  69640. (duk_heaphdr *) name,
  69641. (void *) env,
  69642. (duk_heaphdr *) env));
  69643. DUK_ASSERT(env != NULL);
  69644. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  69645. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(env));
  69646. cl = DUK_HOBJECT_GET_CLASS_NUMBER(env);
  69647. DUK_ASSERT(cl == DUK_HOBJECT_CLASS_OBJENV || cl == DUK_HOBJECT_CLASS_DECENV);
  69648. if (cl == DUK_HOBJECT_CLASS_DECENV) {
  69649. /*
  69650. * Declarative environment record.
  69651. *
  69652. * Identifiers can never be stored in ancestors and are
  69653. * always plain values, so we can use an internal helper
  69654. * and access the value directly with an duk_tval ptr.
  69655. *
  69656. * A closed environment is only indicated by it missing
  69657. * the "book-keeping" properties required for accessing
  69658. * register-bound variables.
  69659. */
  69660. DUK_ASSERT_HDECENV_VALID((duk_hdecenv *) env);
  69661. if (duk__getid_open_decl_env_regs(thr, name, (duk_hdecenv *) env, out)) {
  69662. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  69663. "name=%!O -> value=%!T, attrs=%ld, has_this=%ld, env=%!O, holder=%!O "
  69664. "(declarative environment record, scope open, found in regs)",
  69665. (duk_heaphdr *) name, (duk_tval *) out->value,
  69666. (long) out->attrs, (long) out->has_this,
  69667. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  69668. return 1;
  69669. }
  69670. tv = duk_hobject_find_existing_entry_tval_ptr_and_attrs(thr->heap, env, name, &attrs);
  69671. if (tv) {
  69672. out->value = tv;
  69673. out->attrs = attrs;
  69674. out->env = env;
  69675. out->holder = env;
  69676. out->has_this = 0;
  69677. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  69678. "name=%!O -> value=%!T, attrs=%ld, has_this=%ld, env=%!O, holder=%!O "
  69679. "(declarative environment record, found in properties)",
  69680. (duk_heaphdr *) name, (duk_tval *) out->value,
  69681. (long) out->attrs, (long) out->has_this,
  69682. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  69683. return 1;
  69684. }
  69685. } else {
  69686. /*
  69687. * Object environment record.
  69688. *
  69689. * Binding (target) object is an external, uncontrolled object.
  69690. * Identifier may be bound in an ancestor property, and may be
  69691. * an accessor. Target can also be a Proxy which we must support
  69692. * here.
  69693. */
  69694. /* XXX: we could save space by using _Target OR _This. If _Target, assume
  69695. * this binding is undefined. If _This, assumes this binding is _This, and
  69696. * target is also _This. One property would then be enough.
  69697. */
  69698. duk_hobject *target;
  69699. duk_bool_t found;
  69700. DUK_ASSERT(cl == DUK_HOBJECT_CLASS_OBJENV);
  69701. DUK_ASSERT_HOBJENV_VALID((duk_hobjenv *) env);
  69702. target = ((duk_hobjenv *) env)->target;
  69703. DUK_ASSERT(target != NULL);
  69704. /* Target may be a Proxy or property may be an accessor, so we must
  69705. * use an actual, Proxy-aware hasprop check here.
  69706. *
  69707. * out->holder is NOT set to the actual duk_hobject where the
  69708. * property is found, but rather the object binding target object.
  69709. */
  69710. #if defined(DUK_USE_ES6_PROXY)
  69711. if (DUK_UNLIKELY(DUK_HOBJECT_IS_PROXY(target))) {
  69712. duk_tval tv_name;
  69713. duk_tval tv_target_tmp;
  69714. DUK_ASSERT(name != NULL);
  69715. DUK_TVAL_SET_STRING(&tv_name, name);
  69716. DUK_TVAL_SET_OBJECT(&tv_target_tmp, target);
  69717. found = duk_hobject_hasprop(thr, &tv_target_tmp, &tv_name);
  69718. } else
  69719. #endif /* DUK_USE_ES6_PROXY */
  69720. {
  69721. /* XXX: duk_hobject_hasprop() would be correct for
  69722. * non-Proxy objects too, but it is about ~20-25%
  69723. * slower at present so separate code paths for
  69724. * Proxy and non-Proxy now.
  69725. */
  69726. found = duk_hobject_hasprop_raw(thr, target, name);
  69727. }
  69728. if (found) {
  69729. out->value = NULL; /* can't get value, may be accessor */
  69730. out->attrs = 0; /* irrelevant when out->value == NULL */
  69731. out->env = env;
  69732. out->holder = target;
  69733. out->has_this = ((duk_hobjenv *) env)->has_this;
  69734. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference successful: "
  69735. "name=%!O -> value=%!T, attrs=%ld, has_this=%ld, env=%!O, holder=%!O "
  69736. "(object environment record)",
  69737. (duk_heaphdr *) name, (duk_tval *) out->value,
  69738. (long) out->attrs, (long) out->has_this,
  69739. (duk_heaphdr *) out->env, (duk_heaphdr *) out->holder));
  69740. return 1;
  69741. }
  69742. }
  69743. if (!parents) {
  69744. DUK_DDD(DUK_DDDPRINT("duk__get_identifier_reference failed, no parent traversal "
  69745. "(not found from first traversed env)"));
  69746. goto fail_not_found;
  69747. }
  69748. if (DUK_UNLIKELY(sanity-- == 0)) {
  69749. DUK_ERROR_RANGE(thr, DUK_STR_PROTOTYPE_CHAIN_LIMIT);
  69750. DUK_WO_NORETURN(return 0;);
  69751. }
  69752. env = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, env);
  69753. }
  69754. /*
  69755. * Not found (even in global object)
  69756. */
  69757. fail_not_found:
  69758. return 0;
  69759. }
  69760. /*
  69761. * HASVAR: check identifier binding from a given environment record
  69762. * without traversing its parents.
  69763. *
  69764. * This primitive is not exposed to user code as such, but is used
  69765. * internally for e.g. declaration binding instantiation.
  69766. *
  69767. * See E5 Sections:
  69768. * 10.2.1.1.1 HasBinding(N)
  69769. * 10.2.1.2.1 HasBinding(N)
  69770. *
  69771. * Note: strictness has no bearing on this check. Hence we don't take
  69772. * a 'strict' parameter.
  69773. */
  69774. #if 0 /*unused*/
  69775. DUK_INTERNAL
  69776. duk_bool_t duk_js_hasvar_envrec(duk_hthread *thr,
  69777. duk_hobject *env,
  69778. duk_hstring *name) {
  69779. duk__id_lookup_result ref;
  69780. duk_bool_t parents;
  69781. DUK_DDD(DUK_DDDPRINT("hasvar: thr=%p, env=%p, name=%!O "
  69782. "(env -> %!dO)",
  69783. (void *) thr, (void *) env, (duk_heaphdr *) name,
  69784. (duk_heaphdr *) env));
  69785. DUK_ASSERT(thr != NULL);
  69786. DUK_ASSERT(env != NULL);
  69787. DUK_ASSERT(name != NULL);
  69788. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  69789. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  69790. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  69791. DUK_ASSERT(!DUK_HOBJECT_HAS_ARRAY_PART(env));
  69792. /* lookup results is ignored */
  69793. parents = 0;
  69794. return duk__get_identifier_reference(thr, env, name, NULL, parents, &ref);
  69795. }
  69796. #endif
  69797. /*
  69798. * GETVAR
  69799. *
  69800. * See E5 Sections:
  69801. * 11.1.2 Identifier Reference
  69802. * 10.3.1 Identifier Resolution
  69803. * 11.13.1 Simple Assignment [example of where the Reference is GetValue'd]
  69804. * 8.7.1 GetValue (V)
  69805. * 8.12.1 [[GetOwnProperty]] (P)
  69806. * 8.12.2 [[GetProperty]] (P)
  69807. * 8.12.3 [[Get]] (P)
  69808. *
  69809. * If 'throw' is true, always leaves two values on top of stack: [val this].
  69810. *
  69811. * If 'throw' is false, returns 0 if identifier cannot be resolved, and the
  69812. * stack will be unaffected in this case. If identifier is resolved, returns
  69813. * 1 and leaves [val this] on top of stack.
  69814. *
  69815. * Note: the 'strict' flag of a reference returned by GetIdentifierReference
  69816. * is ignored by GetValue. Hence we don't take a 'strict' parameter.
  69817. *
  69818. * The 'throw' flag is needed for implementing 'typeof' for an unreferenced
  69819. * identifier. An unreference identifier in other contexts generates a
  69820. * ReferenceError.
  69821. */
  69822. DUK_LOCAL
  69823. duk_bool_t duk__getvar_helper(duk_hthread *thr,
  69824. duk_hobject *env,
  69825. duk_activation *act,
  69826. duk_hstring *name,
  69827. duk_bool_t throw_flag) {
  69828. duk__id_lookup_result ref;
  69829. duk_tval tv_tmp_obj;
  69830. duk_tval tv_tmp_key;
  69831. duk_bool_t parents;
  69832. DUK_DDD(DUK_DDDPRINT("getvar: thr=%p, env=%p, act=%p, name=%!O "
  69833. "(env -> %!dO)",
  69834. (void *) thr, (void *) env, (void *) act,
  69835. (duk_heaphdr *) name, (duk_heaphdr *) env));
  69836. DUK_ASSERT(thr != NULL);
  69837. DUK_ASSERT(name != NULL);
  69838. /* env and act may be NULL */
  69839. DUK_STATS_INC(thr->heap, stats_getvar_all);
  69840. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  69841. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  69842. parents = 1; /* follow parent chain */
  69843. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  69844. if (ref.value) {
  69845. duk_push_tval(thr, ref.value);
  69846. duk_push_undefined(thr);
  69847. } else {
  69848. DUK_ASSERT(ref.holder != NULL);
  69849. /* ref.holder is safe across the getprop call (even
  69850. * with side effects) because 'env' is reachable and
  69851. * ref.holder is a direct heap pointer.
  69852. */
  69853. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, ref.holder);
  69854. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  69855. (void) duk_hobject_getprop(thr, &tv_tmp_obj, &tv_tmp_key); /* [value] */
  69856. if (ref.has_this) {
  69857. duk_push_hobject(thr, ref.holder);
  69858. } else {
  69859. duk_push_undefined(thr);
  69860. }
  69861. /* [value this] */
  69862. }
  69863. return 1;
  69864. } else {
  69865. if (throw_flag) {
  69866. DUK_ERROR_FMT1(thr, DUK_ERR_REFERENCE_ERROR,
  69867. "identifier '%s' undefined",
  69868. (const char *) DUK_HSTRING_GET_DATA(name));
  69869. DUK_WO_NORETURN(return 0;);
  69870. }
  69871. return 0;
  69872. }
  69873. }
  69874. DUK_INTERNAL
  69875. duk_bool_t duk_js_getvar_envrec(duk_hthread *thr,
  69876. duk_hobject *env,
  69877. duk_hstring *name,
  69878. duk_bool_t throw_flag) {
  69879. return duk__getvar_helper(thr, env, NULL, name, throw_flag);
  69880. }
  69881. DUK_INTERNAL
  69882. duk_bool_t duk_js_getvar_activation(duk_hthread *thr,
  69883. duk_activation *act,
  69884. duk_hstring *name,
  69885. duk_bool_t throw_flag) {
  69886. DUK_ASSERT(act != NULL);
  69887. return duk__getvar_helper(thr, act->lex_env, act, name, throw_flag);
  69888. }
  69889. /*
  69890. * PUTVAR
  69891. *
  69892. * See E5 Sections:
  69893. * 11.1.2 Identifier Reference
  69894. * 10.3.1 Identifier Resolution
  69895. * 11.13.1 Simple Assignment [example of where the Reference is PutValue'd]
  69896. * 8.7.2 PutValue (V,W) [see especially step 3.b, undefined -> automatic global in non-strict mode]
  69897. * 8.12.4 [[CanPut]] (P)
  69898. * 8.12.5 [[Put]] (P)
  69899. *
  69900. * Note: may invalidate any valstack (or object) duk_tval pointers because
  69901. * putting a value may reallocate any object or any valstack. Caller beware.
  69902. */
  69903. DUK_LOCAL
  69904. void duk__putvar_helper(duk_hthread *thr,
  69905. duk_hobject *env,
  69906. duk_activation *act,
  69907. duk_hstring *name,
  69908. duk_tval *val,
  69909. duk_bool_t strict) {
  69910. duk__id_lookup_result ref;
  69911. duk_tval tv_tmp_obj;
  69912. duk_tval tv_tmp_key;
  69913. duk_bool_t parents;
  69914. DUK_STATS_INC(thr->heap, stats_putvar_all);
  69915. DUK_DDD(DUK_DDDPRINT("putvar: thr=%p, env=%p, act=%p, name=%!O, val=%p, strict=%ld "
  69916. "(env -> %!dO, val -> %!T)",
  69917. (void *) thr, (void *) env, (void *) act,
  69918. (duk_heaphdr *) name, (void *) val, (long) strict,
  69919. (duk_heaphdr *) env, (duk_tval *) val));
  69920. DUK_ASSERT(thr != NULL);
  69921. DUK_ASSERT(name != NULL);
  69922. DUK_ASSERT(val != NULL);
  69923. /* env and act may be NULL */
  69924. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(env);
  69925. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  69926. DUK_ASSERT_REFCOUNT_NONZERO_TVAL(val);
  69927. /*
  69928. * In strict mode E5 protects 'eval' and 'arguments' from being
  69929. * assigned to (or even declared anywhere). Attempt to do so
  69930. * should result in a compile time SyntaxError. See the internal
  69931. * design documentation for details.
  69932. *
  69933. * Thus, we should never come here, run-time, for strict code,
  69934. * and name 'eval' or 'arguments'.
  69935. */
  69936. DUK_ASSERT(!strict ||
  69937. (name != DUK_HTHREAD_STRING_EVAL(thr) &&
  69938. name != DUK_HTHREAD_STRING_LC_ARGUMENTS(thr)));
  69939. /*
  69940. * Lookup variable and update in-place if found.
  69941. */
  69942. parents = 1; /* follow parent chain */
  69943. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  69944. if (ref.value && (ref.attrs & DUK_PROPDESC_FLAG_WRITABLE)) {
  69945. /* Update duk_tval in-place if pointer provided and the
  69946. * property is writable. If the property is not writable
  69947. * (immutable binding), use duk_hobject_putprop() which
  69948. * will respect mutability.
  69949. */
  69950. duk_tval *tv_val;
  69951. tv_val = ref.value;
  69952. DUK_ASSERT(tv_val != NULL);
  69953. DUK_TVAL_SET_TVAL_UPDREF(thr, tv_val, val); /* side effects */
  69954. /* ref.value invalidated here */
  69955. } else {
  69956. DUK_ASSERT(ref.holder != NULL);
  69957. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, ref.holder);
  69958. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  69959. (void) duk_hobject_putprop(thr, &tv_tmp_obj, &tv_tmp_key, val, strict);
  69960. /* ref.value invalidated here */
  69961. }
  69962. return;
  69963. }
  69964. /*
  69965. * Not found: write to global object (non-strict) or ReferenceError
  69966. * (strict); see E5 Section 8.7.2, step 3.
  69967. */
  69968. if (strict) {
  69969. DUK_DDD(DUK_DDDPRINT("identifier binding not found, strict => reference error"));
  69970. DUK_ERROR_FMT1(thr, DUK_ERR_REFERENCE_ERROR,
  69971. "identifier '%s' undefined",
  69972. (const char *) DUK_HSTRING_GET_DATA(name));
  69973. DUK_WO_NORETURN(return;);
  69974. }
  69975. DUK_DDD(DUK_DDDPRINT("identifier binding not found, not strict => set to global"));
  69976. DUK_TVAL_SET_OBJECT(&tv_tmp_obj, thr->builtins[DUK_BIDX_GLOBAL]);
  69977. DUK_TVAL_SET_STRING(&tv_tmp_key, name);
  69978. (void) duk_hobject_putprop(thr, &tv_tmp_obj, &tv_tmp_key, val, 0); /* 0 = no throw */
  69979. /* NB: 'val' may be invalidated here because put_value may realloc valstack,
  69980. * caller beware.
  69981. */
  69982. }
  69983. DUK_INTERNAL
  69984. void duk_js_putvar_envrec(duk_hthread *thr,
  69985. duk_hobject *env,
  69986. duk_hstring *name,
  69987. duk_tval *val,
  69988. duk_bool_t strict) {
  69989. duk__putvar_helper(thr, env, NULL, name, val, strict);
  69990. }
  69991. DUK_INTERNAL
  69992. void duk_js_putvar_activation(duk_hthread *thr,
  69993. duk_activation *act,
  69994. duk_hstring *name,
  69995. duk_tval *val,
  69996. duk_bool_t strict) {
  69997. DUK_ASSERT(act != NULL);
  69998. duk__putvar_helper(thr, act->lex_env, act, name, val, strict);
  69999. }
  70000. /*
  70001. * DELVAR
  70002. *
  70003. * See E5 Sections:
  70004. * 11.4.1 The delete operator
  70005. * 10.2.1.1.5 DeleteBinding (N) [declarative environment record]
  70006. * 10.2.1.2.5 DeleteBinding (N) [object environment record]
  70007. *
  70008. * Variable bindings established inside eval() are deletable (configurable),
  70009. * other bindings are not, including variables declared in global level.
  70010. * Registers are always non-deletable, and the deletion of other bindings
  70011. * is controlled by the configurable flag.
  70012. *
  70013. * For strict mode code, the 'delete' operator should fail with a compile
  70014. * time SyntaxError if applied to identifiers. Hence, no strict mode
  70015. * run-time deletion of identifiers should ever happen. This function
  70016. * should never be called from strict mode code!
  70017. */
  70018. DUK_LOCAL
  70019. duk_bool_t duk__delvar_helper(duk_hthread *thr,
  70020. duk_hobject *env,
  70021. duk_activation *act,
  70022. duk_hstring *name) {
  70023. duk__id_lookup_result ref;
  70024. duk_bool_t parents;
  70025. DUK_DDD(DUK_DDDPRINT("delvar: thr=%p, env=%p, act=%p, name=%!O "
  70026. "(env -> %!dO)",
  70027. (void *) thr, (void *) env, (void *) act,
  70028. (duk_heaphdr *) name, (duk_heaphdr *) env));
  70029. DUK_ASSERT(thr != NULL);
  70030. DUK_ASSERT(name != NULL);
  70031. /* env and act may be NULL */
  70032. DUK_ASSERT_REFCOUNT_NONZERO_HEAPHDR(name);
  70033. parents = 1; /* follow parent chain */
  70034. if (duk__get_identifier_reference(thr, env, name, act, parents, &ref)) {
  70035. if (ref.value && !(ref.attrs & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  70036. /* Identifier found in registers (always non-deletable)
  70037. * or declarative environment record and non-configurable.
  70038. */
  70039. return 0;
  70040. }
  70041. DUK_ASSERT(ref.holder != NULL);
  70042. return duk_hobject_delprop_raw(thr, ref.holder, name, 0);
  70043. }
  70044. /*
  70045. * Not found (even in global object).
  70046. *
  70047. * In non-strict mode this is a silent SUCCESS (!), see E5 Section 11.4.1,
  70048. * step 3.b. In strict mode this case is a compile time SyntaxError so
  70049. * we should not come here.
  70050. */
  70051. DUK_DDD(DUK_DDDPRINT("identifier to be deleted not found: name=%!O "
  70052. "(treated as silent success)",
  70053. (duk_heaphdr *) name));
  70054. return 1;
  70055. }
  70056. #if 0 /*unused*/
  70057. DUK_INTERNAL
  70058. duk_bool_t duk_js_delvar_envrec(duk_hthread *thr,
  70059. duk_hobject *env,
  70060. duk_hstring *name) {
  70061. return duk__delvar_helper(thr, env, NULL, name);
  70062. }
  70063. #endif
  70064. DUK_INTERNAL
  70065. duk_bool_t duk_js_delvar_activation(duk_hthread *thr,
  70066. duk_activation *act,
  70067. duk_hstring *name) {
  70068. DUK_ASSERT(act != NULL);
  70069. return duk__delvar_helper(thr, act->lex_env, act, name);
  70070. }
  70071. /*
  70072. * DECLVAR
  70073. *
  70074. * See E5 Sections:
  70075. * 10.4.3 Entering Function Code
  70076. * 10.5 Declaration Binding Instantion
  70077. * 12.2 Variable Statement
  70078. * 11.1.2 Identifier Reference
  70079. * 10.3.1 Identifier Resolution
  70080. *
  70081. * Variable declaration behavior is mainly discussed in Section 10.5,
  70082. * and is not discussed in the execution semantics (Sections 11-13).
  70083. *
  70084. * Conceptually declarations happen when code (global, eval, function)
  70085. * is entered, before any user code is executed. In practice, register-
  70086. * bound identifiers are 'declared' automatically (by virtue of being
  70087. * allocated to registers with the initial value 'undefined'). Other
  70088. * identifiers are declared in the function prologue with this primitive.
  70089. *
  70090. * Since non-register bindings eventually back to an internal object's
  70091. * properties, the 'prop_flags' argument is used to specify binding
  70092. * type:
  70093. *
  70094. * - Immutable binding: set DUK_PROPDESC_FLAG_WRITABLE to false
  70095. * - Non-deletable binding: set DUK_PROPDESC_FLAG_CONFIGURABLE to false
  70096. * - The flag DUK_PROPDESC_FLAG_ENUMERABLE should be set, although it
  70097. * doesn't really matter for internal objects
  70098. *
  70099. * All bindings are non-deletable mutable bindings except:
  70100. *
  70101. * - Declarations in eval code (mutable, deletable)
  70102. * - 'arguments' binding in strict function code (immutable)
  70103. * - Function name binding of a function expression (immutable)
  70104. *
  70105. * Declarations may go to declarative environment records (always
  70106. * so for functions), but may also go to object environment records
  70107. * (e.g. global code). The global object environment has special
  70108. * behavior when re-declaring a function (but not a variable); see
  70109. * E5.1 specification, Section 10.5, step 5.e.
  70110. *
  70111. * Declarations always go to the 'top-most' environment record, i.e.
  70112. * we never check the record chain. It's not an error even if a
  70113. * property (even an immutable or non-deletable one) of the same name
  70114. * already exists.
  70115. *
  70116. * If a declared variable already exists, its value needs to be updated
  70117. * (if possible). Returns 1 if a PUTVAR needs to be done by the caller;
  70118. * otherwise returns 0.
  70119. */
  70120. DUK_LOCAL
  70121. duk_bool_t duk__declvar_helper(duk_hthread *thr,
  70122. duk_hobject *env,
  70123. duk_hstring *name,
  70124. duk_tval *val,
  70125. duk_small_uint_t prop_flags,
  70126. duk_bool_t is_func_decl) {
  70127. duk_hobject *holder;
  70128. duk_bool_t parents;
  70129. duk__id_lookup_result ref;
  70130. duk_tval *tv;
  70131. DUK_DDD(DUK_DDDPRINT("declvar: thr=%p, env=%p, name=%!O, val=%!T, prop_flags=0x%08lx, is_func_decl=%ld "
  70132. "(env -> %!iO)",
  70133. (void *) thr, (void *) env, (duk_heaphdr *) name,
  70134. (duk_tval *) val, (unsigned long) prop_flags,
  70135. (unsigned int) is_func_decl, (duk_heaphdr *) env));
  70136. DUK_ASSERT(thr != NULL);
  70137. DUK_ASSERT(env != NULL);
  70138. DUK_ASSERT(name != NULL);
  70139. DUK_ASSERT(val != NULL);
  70140. /* Note: in strict mode the compiler should reject explicit
  70141. * declaration of 'eval' or 'arguments'. However, internal
  70142. * bytecode may declare 'arguments' in the function prologue.
  70143. * We don't bother checking (or asserting) for these now.
  70144. */
  70145. /* Note: val is a stable duk_tval pointer. The caller makes
  70146. * a value copy into its stack frame, so 'tv_val' is not subject
  70147. * to side effects here.
  70148. */
  70149. /*
  70150. * Check whether already declared.
  70151. *
  70152. * We need to check whether the binding exists in the environment
  70153. * without walking its parents. However, we still need to check
  70154. * register-bound identifiers and the prototype chain of an object
  70155. * environment target object.
  70156. */
  70157. parents = 0; /* just check 'env' */
  70158. if (duk__get_identifier_reference(thr, env, name, NULL, parents, &ref)) {
  70159. duk_int_t e_idx;
  70160. duk_int_t h_idx;
  70161. duk_small_uint_t flags;
  70162. /*
  70163. * Variable already declared, ignore re-declaration.
  70164. * The only exception is the updated behavior of E5.1 for
  70165. * global function declarations, E5.1 Section 10.5, step 5.e.
  70166. * This behavior does not apply to global variable declarations.
  70167. */
  70168. if (!(is_func_decl && env == thr->builtins[DUK_BIDX_GLOBAL_ENV])) {
  70169. DUK_DDD(DUK_DDDPRINT("re-declare a binding, ignoring"));
  70170. return 1; /* 1 -> needs a PUTVAR */
  70171. }
  70172. /*
  70173. * Special behavior in E5.1.
  70174. *
  70175. * Note that even though parents == 0, the conflicting property
  70176. * may be an inherited property (currently our global object's
  70177. * prototype is Object.prototype). Step 5.e first operates on
  70178. * the existing property (which is potentially in an ancestor)
  70179. * and then defines a new property in the global object (and
  70180. * never modifies the ancestor).
  70181. *
  70182. * Also note that this logic would become even more complicated
  70183. * if the conflicting property might be a virtual one. Object
  70184. * prototype has no virtual properties, though.
  70185. *
  70186. * XXX: this is now very awkward, rework.
  70187. */
  70188. DUK_DDD(DUK_DDDPRINT("re-declare a function binding in global object, "
  70189. "updated E5.1 processing"));
  70190. DUK_ASSERT(ref.holder != NULL);
  70191. holder = ref.holder;
  70192. /* holder will be set to the target object, not the actual object
  70193. * where the property was found (see duk__get_identifier_reference()).
  70194. */
  70195. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(holder) == DUK_HOBJECT_CLASS_GLOBAL);
  70196. DUK_ASSERT(!DUK_HOBJECT_HAS_EXOTIC_ARRAY(holder)); /* global object doesn't have array part */
  70197. /* XXX: use a helper for prototype traversal; no loop check here */
  70198. /* must be found: was found earlier, and cannot be inherited */
  70199. for (;;) {
  70200. DUK_ASSERT(holder != NULL);
  70201. if (duk_hobject_find_existing_entry(thr->heap, holder, name, &e_idx, &h_idx)) {
  70202. DUK_ASSERT(e_idx >= 0);
  70203. break;
  70204. }
  70205. /* SCANBUILD: NULL pointer dereference, doesn't actually trigger,
  70206. * asserted above.
  70207. */
  70208. holder = DUK_HOBJECT_GET_PROTOTYPE(thr->heap, holder);
  70209. }
  70210. DUK_ASSERT(holder != NULL);
  70211. DUK_ASSERT(e_idx >= 0);
  70212. /* SCANBUILD: scan-build produces a NULL pointer dereference warning
  70213. * below; it never actually triggers because holder is actually never
  70214. * NULL.
  70215. */
  70216. /* ref.holder is global object, holder is the object with the
  70217. * conflicting property.
  70218. */
  70219. flags = DUK_HOBJECT_E_GET_FLAGS(thr->heap, holder, e_idx);
  70220. if (!(flags & DUK_PROPDESC_FLAG_CONFIGURABLE)) {
  70221. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  70222. DUK_DDD(DUK_DDDPRINT("existing property is a non-configurable "
  70223. "accessor -> reject"));
  70224. goto fail_existing_attributes;
  70225. }
  70226. if (!((flags & DUK_PROPDESC_FLAG_WRITABLE) &&
  70227. (flags & DUK_PROPDESC_FLAG_ENUMERABLE))) {
  70228. DUK_DDD(DUK_DDDPRINT("existing property is a non-configurable "
  70229. "plain property which is not writable and "
  70230. "enumerable -> reject"));
  70231. goto fail_existing_attributes;
  70232. }
  70233. DUK_DDD(DUK_DDDPRINT("existing property is not configurable but "
  70234. "is plain, enumerable, and writable -> "
  70235. "allow redeclaration"));
  70236. }
  70237. if (holder == ref.holder) {
  70238. /* XXX: if duk_hobject_define_property_internal() was updated
  70239. * to handle a pre-existing accessor property, this would be
  70240. * a simple call (like for the ancestor case).
  70241. */
  70242. DUK_DDD(DUK_DDDPRINT("redefine, offending property in global object itself"));
  70243. if (flags & DUK_PROPDESC_FLAG_ACCESSOR) {
  70244. duk_hobject *tmp;
  70245. tmp = DUK_HOBJECT_E_GET_VALUE_GETTER(thr->heap, holder, e_idx);
  70246. DUK_HOBJECT_E_SET_VALUE_GETTER(thr->heap, holder, e_idx, NULL);
  70247. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  70248. DUK_UNREF(tmp);
  70249. tmp = DUK_HOBJECT_E_GET_VALUE_SETTER(thr->heap, holder, e_idx);
  70250. DUK_HOBJECT_E_SET_VALUE_SETTER(thr->heap, holder, e_idx, NULL);
  70251. DUK_HOBJECT_DECREF_ALLOWNULL(thr, tmp);
  70252. DUK_UNREF(tmp);
  70253. } else {
  70254. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx);
  70255. DUK_TVAL_SET_UNDEFINED_UPDREF(thr, tv);
  70256. }
  70257. /* Here val would be potentially invalid if we didn't make
  70258. * a value copy at the caller.
  70259. */
  70260. tv = DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx);
  70261. DUK_TVAL_SET_TVAL(tv, val);
  70262. DUK_TVAL_INCREF(thr, tv);
  70263. DUK_HOBJECT_E_SET_FLAGS(thr->heap, holder, e_idx, prop_flags);
  70264. DUK_DDD(DUK_DDDPRINT("updated global binding, final result: "
  70265. "value -> %!T, prop_flags=0x%08lx",
  70266. (duk_tval *) DUK_HOBJECT_E_GET_VALUE_TVAL_PTR(thr->heap, holder, e_idx),
  70267. (unsigned long) prop_flags));
  70268. } else {
  70269. DUK_DDD(DUK_DDDPRINT("redefine, offending property in ancestor"));
  70270. DUK_ASSERT(ref.holder == thr->builtins[DUK_BIDX_GLOBAL]);
  70271. duk_push_tval(thr, val);
  70272. duk_hobject_define_property_internal(thr, ref.holder, name, prop_flags);
  70273. }
  70274. return 0;
  70275. }
  70276. /*
  70277. * Not found (in registers or record objects). Declare
  70278. * to current variable environment.
  70279. */
  70280. /*
  70281. * Get holder object
  70282. */
  70283. if (DUK_HOBJECT_IS_DECENV(env)) {
  70284. DUK_ASSERT_HDECENV_VALID((duk_hdecenv *) env);
  70285. holder = env;
  70286. } else {
  70287. DUK_ASSERT_HOBJENV_VALID((duk_hobjenv *) env);
  70288. holder = ((duk_hobjenv *) env)->target;
  70289. DUK_ASSERT(holder != NULL);
  70290. }
  70291. /*
  70292. * Define new property
  70293. *
  70294. * Note: this may fail if the holder is not extensible.
  70295. */
  70296. /* XXX: this is awkward as we use an internal method which doesn't handle
  70297. * extensibility etc correctly. Basically we'd want to do a [[DefineOwnProperty]]
  70298. * or Object.defineProperty() here.
  70299. */
  70300. if (!DUK_HOBJECT_HAS_EXTENSIBLE(holder)) {
  70301. goto fail_not_extensible;
  70302. }
  70303. duk_push_hobject(thr, holder);
  70304. duk_push_hstring(thr, name);
  70305. duk_push_tval(thr, val);
  70306. duk_xdef_prop(thr, -3, prop_flags); /* [holder name val] -> [holder] */
  70307. duk_pop_unsafe(thr);
  70308. return 0;
  70309. fail_existing_attributes:
  70310. fail_not_extensible:
  70311. DUK_ERROR_TYPE(thr, "declaration failed");
  70312. DUK_WO_NORETURN(return 0;);
  70313. }
  70314. DUK_INTERNAL
  70315. duk_bool_t duk_js_declvar_activation(duk_hthread *thr,
  70316. duk_activation *act,
  70317. duk_hstring *name,
  70318. duk_tval *val,
  70319. duk_small_uint_t prop_flags,
  70320. duk_bool_t is_func_decl) {
  70321. duk_hobject *env;
  70322. duk_tval tv_val_copy;
  70323. DUK_ASSERT(act != NULL);
  70324. /*
  70325. * Make a value copy of the input val. This ensures that
  70326. * side effects cannot invalidate the pointer.
  70327. */
  70328. DUK_TVAL_SET_TVAL(&tv_val_copy, val);
  70329. val = &tv_val_copy;
  70330. /*
  70331. * Delayed env creation check
  70332. */
  70333. if (!act->var_env) {
  70334. DUK_ASSERT(act->lex_env == NULL);
  70335. duk_js_init_activation_environment_records_delayed(thr, act);
  70336. /* 'act' is a stable pointer, so still OK. */
  70337. }
  70338. DUK_ASSERT(act->lex_env != NULL);
  70339. DUK_ASSERT(act->var_env != NULL);
  70340. env = act->var_env;
  70341. DUK_ASSERT(env != NULL);
  70342. DUK_ASSERT(DUK_HOBJECT_IS_ENV(env));
  70343. return duk__declvar_helper(thr, env, name, val, prop_flags, is_func_decl);
  70344. }
  70345. #line 1 "duk_lexer.c"
  70346. /*
  70347. * Lexer for source files, ToNumber() string conversions, RegExp expressions,
  70348. * and JSON.
  70349. *
  70350. * Provides a stream of ECMAScript tokens from an UTF-8/CESU-8 buffer. The
  70351. * caller can also rewind the token stream into a certain position which is
  70352. * needed by the compiler part for multi-pass scanning. Tokens are
  70353. * represented as duk_token structures, and contain line number information.
  70354. * Token types are identified with DUK_TOK_* defines.
  70355. *
  70356. * Characters are decoded into a fixed size lookup window consisting of
  70357. * decoded Unicode code points, with window positions past the end of the
  70358. * input filled with an invalid codepoint (-1). The tokenizer can thus
  70359. * perform multiple character lookups efficiently and with few sanity
  70360. * checks (such as access outside the end of the input), which keeps the
  70361. * tokenization code small at the cost of performance.
  70362. *
  70363. * Character data in tokens, such as identifier names and string literals,
  70364. * is encoded into CESU-8 format on-the-fly while parsing the token in
  70365. * question. The string data is made reachable to garbage collection by
  70366. * placing the token-related values in value stack entries allocated for
  70367. * this purpose by the caller. The characters exist in Unicode code point
  70368. * form only in the fixed size lookup window, which keeps character data
  70369. * expansion (of especially ASCII data) low.
  70370. *
  70371. * Token parsing supports the full range of Unicode characters as described
  70372. * in the E5 specification. Parsing has been optimized for ASCII characters
  70373. * because ordinary ECMAScript code consists almost entirely of ASCII
  70374. * characters. Matching of complex Unicode codepoint sets (such as in the
  70375. * IdentifierStart and IdentifierPart productions) is optimized for size,
  70376. * and is done using a linear scan of a bit-packed list of ranges. This is
  70377. * very slow, but should never be entered unless the source code actually
  70378. * contains Unicode characters.
  70379. *
  70380. * ECMAScript tokenization is partially context sensitive. First,
  70381. * additional future reserved words are recognized in strict mode (see E5
  70382. * Section 7.6.1.2). Second, a forward slash character ('/') can be
  70383. * recognized either as starting a RegExp literal or as a division operator,
  70384. * depending on context. The caller must provide necessary context flags
  70385. * when requesting a new token.
  70386. *
  70387. * Future work:
  70388. *
  70389. * * Make line number tracking optional, as it consumes space.
  70390. *
  70391. * * Add a feature flag for disabling UTF-8 decoding of input, as most
  70392. * source code is ASCII. Because of Unicode escapes written in ASCII,
  70393. * this does not allow Unicode support to be removed from e.g.
  70394. * duk_unicode_is_identifier_start() nor does it allow removal of CESU-8
  70395. * encoding of e.g. string literals.
  70396. *
  70397. * * Add a feature flag for disabling Unicode compliance of e.g. identifier
  70398. * names. This allows for a build more than a kilobyte smaller, because
  70399. * Unicode ranges needed by duk_unicode_is_identifier_start() and
  70400. * duk_unicode_is_identifier_part() can be dropped. String literals
  70401. * should still be allowed to contain escaped Unicode, so this still does
  70402. * not allow removal of CESU-8 encoding of e.g. string literals.
  70403. *
  70404. * * Character lookup tables for codepoints above BMP could be stripped.
  70405. *
  70406. * * Strictly speaking, E5 specification requires that source code consists
  70407. * of 16-bit code units, and if not, must be conceptually converted to
  70408. * that format first. The current lexer processes Unicode code points
  70409. * and allows characters outside the BMP. These should be converted to
  70410. * surrogate pairs while reading the source characters into the window,
  70411. * not after tokens have been formed (as is done now). However, the fix
  70412. * is not trivial because two characters are decoded from one codepoint.
  70413. *
  70414. * * Optimize for speed as well as size. Large if-else ladders are (at
  70415. * least potentially) slow.
  70416. */
  70417. /* #include duk_internal.h -> already included */
  70418. /*
  70419. * Various defines and file specific helper macros
  70420. */
  70421. #define DUK__MAX_RE_DECESC_DIGITS 9
  70422. #define DUK__MAX_RE_QUANT_DIGITS 9 /* Does not allow e.g. 2**31-1, but one more would allow overflows of u32. */
  70423. /* whether to use macros or helper function depends on call count */
  70424. #define DUK__ISDIGIT(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_9)
  70425. #define DUK__ISHEXDIGIT(x) duk__is_hex_digit((x))
  70426. #define DUK__ISOCTDIGIT(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_7)
  70427. #define DUK__ISDIGIT03(x) ((x) >= DUK_ASC_0 && (x) <= DUK_ASC_3)
  70428. #define DUK__ISDIGIT47(x) ((x) >= DUK_ASC_4 && (x) <= DUK_ASC_7)
  70429. /* lexer character window helpers */
  70430. #define DUK__LOOKUP(lex_ctx,idx) ((lex_ctx)->window[(idx)].codepoint)
  70431. #define DUK__ADVANCECHARS(lex_ctx,count) duk__advance_chars((lex_ctx), (count))
  70432. #define DUK__ADVANCEBYTES(lex_ctx,count) duk__advance_bytes((lex_ctx), (count))
  70433. #define DUK__INITBUFFER(lex_ctx) duk__initbuffer((lex_ctx))
  70434. #define DUK__APPENDBUFFER(lex_ctx,x) duk__appendbuffer((lex_ctx), (duk_codepoint_t) (x))
  70435. #define DUK__APPENDBUFFER_ASCII(lex_ctx,x) duk__appendbuffer_ascii((lex_ctx), (duk_codepoint_t) (x))
  70436. /* lookup shorthands (note: assume context variable is named 'lex_ctx') */
  70437. #define DUK__L0() DUK__LOOKUP(lex_ctx, 0)
  70438. #define DUK__L1() DUK__LOOKUP(lex_ctx, 1)
  70439. #define DUK__L2() DUK__LOOKUP(lex_ctx, 2)
  70440. #define DUK__L3() DUK__LOOKUP(lex_ctx, 3)
  70441. #define DUK__L4() DUK__LOOKUP(lex_ctx, 4)
  70442. #define DUK__L5() DUK__LOOKUP(lex_ctx, 5)
  70443. /* packed advance/token number macro used by multiple functions */
  70444. #define DUK__ADVTOK(advbytes,tok) ((((advbytes) * sizeof(duk_lexer_codepoint)) << 8) + (tok))
  70445. /*
  70446. * Advance lookup window by N characters, filling in new characters as
  70447. * necessary. After returning caller is guaranteed a character window of
  70448. * at least DUK_LEXER_WINDOW_SIZE characters.
  70449. *
  70450. * The main function duk__advance_bytes() is called at least once per every
  70451. * token so it has a major lexer/compiler performance impact. There are two
  70452. * variants for the main duk__advance_bytes() algorithm: a sliding window
  70453. * approach which is slightly faster at the cost of larger code footprint,
  70454. * and a simple copying one.
  70455. *
  70456. * Decoding directly from the source string would be another lexing option.
  70457. * But the lookup window based approach has the advantage of hiding the
  70458. * source string and its encoding effectively which gives more flexibility
  70459. * going forward to e.g. support chunked streaming of source from flash.
  70460. *
  70461. * Decodes UTF-8/CESU-8 leniently with support for code points from U+0000 to
  70462. * U+10FFFF, causing an error if the input is unparseable. Leniency means:
  70463. *
  70464. * * Unicode code point validation is intentionally not performed,
  70465. * except to check that the codepoint does not exceed 0x10ffff.
  70466. *
  70467. * * In particular, surrogate pairs are allowed and not combined, which
  70468. * allows source files to represent all SourceCharacters with CESU-8.
  70469. * Broken surrogate pairs are allowed, as ECMAScript does not mandate
  70470. * their validation.
  70471. *
  70472. * * Allow non-shortest UTF-8 encodings.
  70473. *
  70474. * Leniency here causes few security concerns because all character data is
  70475. * decoded into Unicode codepoints before lexer processing, and is then
  70476. * re-encoded into CESU-8. The source can be parsed as strict UTF-8 with
  70477. * a compiler option. However, ECMAScript source characters include -all-
  70478. * 16-bit unsigned integer codepoints, so leniency seems to be appropriate.
  70479. *
  70480. * Note that codepoints above the BMP are not strictly SourceCharacters,
  70481. * but the lexer still accepts them as such. Before ending up in a string
  70482. * or an identifier name, codepoints above BMP are converted into surrogate
  70483. * pairs and then CESU-8 encoded, resulting in 16-bit Unicode data as
  70484. * expected by ECMAScript.
  70485. *
  70486. * An alternative approach to dealing with invalid or partial sequences
  70487. * would be to skip them and replace them with e.g. the Unicode replacement
  70488. * character U+FFFD. This has limited utility because a replacement character
  70489. * will most likely cause a parse error, unless it occurs inside a string.
  70490. * Further, ECMAScript source is typically pure ASCII.
  70491. *
  70492. * See:
  70493. *
  70494. * http://en.wikipedia.org/wiki/UTF-8
  70495. * http://en.wikipedia.org/wiki/CESU-8
  70496. * http://tools.ietf.org/html/rfc3629
  70497. * http://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences
  70498. *
  70499. * Future work:
  70500. *
  70501. * * Reject other invalid Unicode sequences (see Wikipedia entry for examples)
  70502. * in strict UTF-8 mode.
  70503. *
  70504. * * Size optimize. An attempt to use a 16-byte lookup table for the first
  70505. * byte resulted in a code increase though.
  70506. *
  70507. * * Is checking against maximum 0x10ffff really useful? 4-byte encoding
  70508. * imposes a certain limit anyway.
  70509. *
  70510. * * Support chunked streaming of source code. Can be implemented either
  70511. * by streaming chunks of bytes or chunks of codepoints.
  70512. */
  70513. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  70514. DUK_LOCAL void duk__fill_lexer_buffer(duk_lexer_ctx *lex_ctx, duk_small_uint_t start_offset_bytes) {
  70515. duk_lexer_codepoint *cp, *cp_end;
  70516. duk_ucodepoint_t x;
  70517. duk_small_uint_t contlen;
  70518. const duk_uint8_t *p, *p_end;
  70519. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70520. duk_ucodepoint_t mincp;
  70521. #endif
  70522. duk_int_t input_line;
  70523. /* Use temporaries and update lex_ctx only when finished. */
  70524. input_line = lex_ctx->input_line;
  70525. p = lex_ctx->input + lex_ctx->input_offset;
  70526. p_end = lex_ctx->input + lex_ctx->input_length;
  70527. cp = (duk_lexer_codepoint *) (void *) ((duk_uint8_t *) lex_ctx->buffer + start_offset_bytes);
  70528. cp_end = lex_ctx->buffer + DUK_LEXER_BUFFER_SIZE;
  70529. for (; cp != cp_end; cp++) {
  70530. cp->offset = (duk_size_t) (p - lex_ctx->input);
  70531. cp->line = input_line;
  70532. /* XXX: potential issue with signed pointers, p_end < p. */
  70533. if (DUK_UNLIKELY(p >= p_end)) {
  70534. /* If input_offset were assigned a negative value, it would
  70535. * result in a large positive value. Most likely it would be
  70536. * larger than input_length and be caught here. In any case
  70537. * no memory unsafe behavior would happen.
  70538. */
  70539. cp->codepoint = -1;
  70540. continue;
  70541. }
  70542. x = (duk_ucodepoint_t) (*p++);
  70543. /* Fast path. */
  70544. if (DUK_LIKELY(x < 0x80UL)) {
  70545. DUK_ASSERT(x != 0x2028UL && x != 0x2029UL); /* not LS/PS */
  70546. if (DUK_UNLIKELY(x <= 0x000dUL)) {
  70547. if ((x == 0x000aUL) ||
  70548. ((x == 0x000dUL) && (p >= p_end || *p != 0x000aUL))) {
  70549. /* lookup for 0x000a above assumes shortest encoding now */
  70550. /* E5 Section 7.3, treat the following as newlines:
  70551. * LF
  70552. * CR [not followed by LF]
  70553. * LS
  70554. * PS
  70555. *
  70556. * For CR LF, CR is ignored if it is followed by LF, and the LF will bump
  70557. * the line number.
  70558. */
  70559. input_line++;
  70560. }
  70561. }
  70562. cp->codepoint = (duk_codepoint_t) x;
  70563. continue;
  70564. }
  70565. /* Slow path. */
  70566. if (x < 0xc0UL) {
  70567. /* 10xx xxxx -> invalid */
  70568. goto error_encoding;
  70569. } else if (x < 0xe0UL) {
  70570. /* 110x xxxx 10xx xxxx */
  70571. contlen = 1;
  70572. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70573. mincp = 0x80UL;
  70574. #endif
  70575. x = x & 0x1fUL;
  70576. } else if (x < 0xf0UL) {
  70577. /* 1110 xxxx 10xx xxxx 10xx xxxx */
  70578. contlen = 2;
  70579. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70580. mincp = 0x800UL;
  70581. #endif
  70582. x = x & 0x0fUL;
  70583. } else if (x < 0xf8UL) {
  70584. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx */
  70585. contlen = 3;
  70586. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70587. mincp = 0x10000UL;
  70588. #endif
  70589. x = x & 0x07UL;
  70590. } else {
  70591. /* no point in supporting encodings of 5 or more bytes */
  70592. goto error_encoding;
  70593. }
  70594. DUK_ASSERT(p_end >= p);
  70595. if ((duk_size_t) contlen > (duk_size_t) (p_end - p)) {
  70596. goto error_clipped;
  70597. }
  70598. while (contlen > 0) {
  70599. duk_small_uint_t y;
  70600. y = *p++;
  70601. if ((y & 0xc0U) != 0x80U) {
  70602. /* check that byte has the form 10xx xxxx */
  70603. goto error_encoding;
  70604. }
  70605. x = x << 6;
  70606. x += y & 0x3fUL;
  70607. contlen--;
  70608. }
  70609. /* check final character validity */
  70610. if (x > 0x10ffffUL) {
  70611. goto error_encoding;
  70612. }
  70613. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70614. if (x < mincp || (x >= 0xd800UL && x <= 0xdfffUL) || x == 0xfffeUL) {
  70615. goto error_encoding;
  70616. }
  70617. #endif
  70618. DUK_ASSERT(x != 0x000aUL && x != 0x000dUL);
  70619. if ((x == 0x2028UL) || (x == 0x2029UL)) {
  70620. input_line++;
  70621. }
  70622. cp->codepoint = (duk_codepoint_t) x;
  70623. }
  70624. lex_ctx->input_offset = (duk_size_t) (p - lex_ctx->input);
  70625. lex_ctx->input_line = input_line;
  70626. return;
  70627. error_clipped: /* clipped codepoint */
  70628. error_encoding: /* invalid codepoint encoding or codepoint */
  70629. lex_ctx->input_offset = (duk_size_t) (p - lex_ctx->input);
  70630. lex_ctx->input_line = input_line;
  70631. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_SOURCE_DECODE_FAILED);
  70632. DUK_WO_NORETURN(return;);
  70633. }
  70634. DUK_LOCAL void duk__advance_bytes(duk_lexer_ctx *lex_ctx, duk_small_uint_t count_bytes) {
  70635. duk_small_uint_t used_bytes, avail_bytes;
  70636. DUK_ASSERT_DISABLE(count_bytes >= 0); /* unsigned */
  70637. DUK_ASSERT(count_bytes <= (duk_small_uint_t) (DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint)));
  70638. DUK_ASSERT(lex_ctx->window >= lex_ctx->buffer);
  70639. DUK_ASSERT(lex_ctx->window < lex_ctx->buffer + DUK_LEXER_BUFFER_SIZE);
  70640. DUK_ASSERT((duk_uint8_t *) lex_ctx->window + count_bytes <= (duk_uint8_t *) lex_ctx->buffer + DUK_LEXER_BUFFER_SIZE * sizeof(duk_lexer_codepoint));
  70641. /* Zero 'count' is also allowed to make call sites easier.
  70642. * Arithmetic in bytes generates better code in GCC.
  70643. */
  70644. lex_ctx->window = (duk_lexer_codepoint *) (void *) ((duk_uint8_t *) lex_ctx->window + count_bytes); /* avoid multiply */
  70645. used_bytes = (duk_small_uint_t) ((duk_uint8_t *) lex_ctx->window - (duk_uint8_t *) lex_ctx->buffer);
  70646. avail_bytes = DUK_LEXER_BUFFER_SIZE * sizeof(duk_lexer_codepoint) - used_bytes;
  70647. if (avail_bytes < (duk_small_uint_t) (DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint))) {
  70648. /* Not enough data to provide a full window, so "scroll" window to
  70649. * start of buffer and fill up the rest.
  70650. */
  70651. duk_memmove((void *) lex_ctx->buffer,
  70652. (const void *) lex_ctx->window,
  70653. (size_t) avail_bytes);
  70654. lex_ctx->window = lex_ctx->buffer;
  70655. duk__fill_lexer_buffer(lex_ctx, avail_bytes);
  70656. }
  70657. }
  70658. DUK_LOCAL void duk__init_lexer_window(duk_lexer_ctx *lex_ctx) {
  70659. lex_ctx->window = lex_ctx->buffer;
  70660. duk__fill_lexer_buffer(lex_ctx, 0);
  70661. }
  70662. #else /* DUK_USE_LEXER_SLIDING_WINDOW */
  70663. DUK_LOCAL duk_codepoint_t duk__read_char(duk_lexer_ctx *lex_ctx) {
  70664. duk_ucodepoint_t x;
  70665. duk_small_uint_t len;
  70666. duk_small_uint_t i;
  70667. const duk_uint8_t *p;
  70668. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70669. duk_ucodepoint_t mincp;
  70670. #endif
  70671. duk_size_t input_offset;
  70672. input_offset = lex_ctx->input_offset;
  70673. if (DUK_UNLIKELY(input_offset >= lex_ctx->input_length)) {
  70674. /* If input_offset were assigned a negative value, it would
  70675. * result in a large positive value. Most likely it would be
  70676. * larger than input_length and be caught here. In any case
  70677. * no memory unsafe behavior would happen.
  70678. */
  70679. return -1;
  70680. }
  70681. p = lex_ctx->input + input_offset;
  70682. x = (duk_ucodepoint_t) (*p);
  70683. if (DUK_LIKELY(x < 0x80UL)) {
  70684. /* 0xxx xxxx -> fast path */
  70685. /* input offset tracking */
  70686. lex_ctx->input_offset++;
  70687. DUK_ASSERT(x != 0x2028UL && x != 0x2029UL); /* not LS/PS */
  70688. if (DUK_UNLIKELY(x <= 0x000dUL)) {
  70689. if ((x == 0x000aUL) ||
  70690. ((x == 0x000dUL) && (lex_ctx->input_offset >= lex_ctx->input_length ||
  70691. lex_ctx->input[lex_ctx->input_offset] != 0x000aUL))) {
  70692. /* lookup for 0x000a above assumes shortest encoding now */
  70693. /* E5 Section 7.3, treat the following as newlines:
  70694. * LF
  70695. * CR [not followed by LF]
  70696. * LS
  70697. * PS
  70698. *
  70699. * For CR LF, CR is ignored if it is followed by LF, and the LF will bump
  70700. * the line number.
  70701. */
  70702. lex_ctx->input_line++;
  70703. }
  70704. }
  70705. return (duk_codepoint_t) x;
  70706. }
  70707. /* Slow path. */
  70708. if (x < 0xc0UL) {
  70709. /* 10xx xxxx -> invalid */
  70710. goto error_encoding;
  70711. } else if (x < 0xe0UL) {
  70712. /* 110x xxxx 10xx xxxx */
  70713. len = 2;
  70714. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70715. mincp = 0x80UL;
  70716. #endif
  70717. x = x & 0x1fUL;
  70718. } else if (x < 0xf0UL) {
  70719. /* 1110 xxxx 10xx xxxx 10xx xxxx */
  70720. len = 3;
  70721. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70722. mincp = 0x800UL;
  70723. #endif
  70724. x = x & 0x0fUL;
  70725. } else if (x < 0xf8UL) {
  70726. /* 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx */
  70727. len = 4;
  70728. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70729. mincp = 0x10000UL;
  70730. #endif
  70731. x = x & 0x07UL;
  70732. } else {
  70733. /* no point in supporting encodings of 5 or more bytes */
  70734. goto error_encoding;
  70735. }
  70736. DUK_ASSERT(lex_ctx->input_length >= lex_ctx->input_offset);
  70737. if ((duk_size_t) len > (duk_size_t) (lex_ctx->input_length - lex_ctx->input_offset)) {
  70738. goto error_clipped;
  70739. }
  70740. p++;
  70741. for (i = 1; i < len; i++) {
  70742. duk_small_uint_t y;
  70743. y = *p++;
  70744. if ((y & 0xc0U) != 0x80U) {
  70745. /* check that byte has the form 10xx xxxx */
  70746. goto error_encoding;
  70747. }
  70748. x = x << 6;
  70749. x += y & 0x3fUL;
  70750. }
  70751. /* check final character validity */
  70752. if (x > 0x10ffffUL) {
  70753. goto error_encoding;
  70754. }
  70755. #if defined(DUK_USE_STRICT_UTF8_SOURCE)
  70756. if (x < mincp || (x >= 0xd800UL && x <= 0xdfffUL) || x == 0xfffeUL) {
  70757. goto error_encoding;
  70758. }
  70759. #endif
  70760. /* input offset tracking */
  70761. lex_ctx->input_offset += len;
  70762. /* line tracking */
  70763. DUK_ASSERT(x != 0x000aUL && x != 0x000dUL);
  70764. if ((x == 0x2028UL) || (x == 0x2029UL)) {
  70765. lex_ctx->input_line++;
  70766. }
  70767. return (duk_codepoint_t) x;
  70768. error_clipped: /* clipped codepoint */
  70769. error_encoding: /* invalid codepoint encoding or codepoint */
  70770. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_SOURCE_DECODE_FAILED);
  70771. DUK_WO_NORETURN(return 0;);
  70772. }
  70773. DUK_LOCAL void duk__advance_bytes(duk_lexer_ctx *lex_ctx, duk_small_uint_t count_bytes) {
  70774. duk_small_uint_t keep_bytes;
  70775. duk_lexer_codepoint *cp, *cp_end;
  70776. DUK_ASSERT_DISABLE(count_bytes >= 0); /* unsigned */
  70777. DUK_ASSERT(count_bytes <= (duk_small_uint_t) (DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint)));
  70778. /* Zero 'count' is also allowed to make call sites easier. */
  70779. keep_bytes = DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint) - count_bytes;
  70780. duk_memmove((void *) lex_ctx->window,
  70781. (const void *) ((duk_uint8_t *) lex_ctx->window + count_bytes),
  70782. (size_t) keep_bytes);
  70783. cp = (duk_lexer_codepoint *) ((duk_uint8_t *) lex_ctx->window + keep_bytes);
  70784. cp_end = lex_ctx->window + DUK_LEXER_WINDOW_SIZE;
  70785. for (; cp != cp_end; cp++) {
  70786. cp->offset = lex_ctx->input_offset;
  70787. cp->line = lex_ctx->input_line;
  70788. cp->codepoint = duk__read_char(lex_ctx);
  70789. }
  70790. }
  70791. DUK_LOCAL void duk__init_lexer_window(duk_lexer_ctx *lex_ctx) {
  70792. /* Call with count == DUK_LEXER_WINDOW_SIZE to fill buffer initially. */
  70793. duk__advance_bytes(lex_ctx, DUK_LEXER_WINDOW_SIZE * sizeof(duk_lexer_codepoint)); /* fill window */
  70794. }
  70795. #endif /* DUK_USE_LEXER_SLIDING_WINDOW */
  70796. DUK_LOCAL void duk__advance_chars(duk_lexer_ctx *lex_ctx, duk_small_uint_t count_chars) {
  70797. duk__advance_bytes(lex_ctx, count_chars * sizeof(duk_lexer_codepoint));
  70798. }
  70799. /*
  70800. * (Re)initialize the temporary byte buffer. May be called extra times
  70801. * with little impact.
  70802. */
  70803. DUK_LOCAL void duk__initbuffer(duk_lexer_ctx *lex_ctx) {
  70804. /* Reuse buffer as is unless buffer has grown large. */
  70805. if (DUK_HBUFFER_DYNAMIC_GET_SIZE(lex_ctx->buf) < DUK_LEXER_TEMP_BUF_LIMIT) {
  70806. /* Keep current size */
  70807. } else {
  70808. duk_hbuffer_resize(lex_ctx->thr, lex_ctx->buf, DUK_LEXER_TEMP_BUF_LIMIT);
  70809. }
  70810. DUK_BW_INIT_WITHBUF(lex_ctx->thr, &lex_ctx->bw, lex_ctx->buf);
  70811. }
  70812. /*
  70813. * Append a Unicode codepoint to the temporary byte buffer. Performs
  70814. * CESU-8 surrogate pair encoding for codepoints above the BMP.
  70815. * Existing surrogate pairs are allowed and also encoded into CESU-8.
  70816. */
  70817. DUK_LOCAL void duk__appendbuffer(duk_lexer_ctx *lex_ctx, duk_codepoint_t x) {
  70818. /*
  70819. * Since character data is only generated by decoding the source or by
  70820. * the compiler itself, we rely on the input codepoints being correct
  70821. * and avoid a check here.
  70822. *
  70823. * Character data can also come here through decoding of Unicode
  70824. * escapes ("\udead\ubeef") so all 16-but unsigned values can be
  70825. * present, even when the source file itself is strict UTF-8.
  70826. */
  70827. DUK_ASSERT(x >= 0 && x <= 0x10ffffL);
  70828. DUK_BW_WRITE_ENSURE_CESU8(lex_ctx->thr, &lex_ctx->bw, (duk_ucodepoint_t) x);
  70829. }
  70830. DUK_LOCAL void duk__appendbuffer_ascii(duk_lexer_ctx *lex_ctx, duk_codepoint_t x) {
  70831. /* ASCII characters can be emitted as a single byte without encoding
  70832. * which matters for some fast paths.
  70833. */
  70834. DUK_ASSERT(x >= 0 && x <= 0x7f);
  70835. DUK_BW_WRITE_ENSURE_U8(lex_ctx->thr, &lex_ctx->bw, (duk_uint8_t) x);
  70836. }
  70837. /*
  70838. * Intern the temporary byte buffer into a valstack slot
  70839. * (in practice, slot1 or slot2).
  70840. */
  70841. DUK_LOCAL duk_hstring *duk__internbuffer(duk_lexer_ctx *lex_ctx, duk_idx_t valstack_idx) {
  70842. DUK_ASSERT(valstack_idx == lex_ctx->slot1_idx || valstack_idx == lex_ctx->slot2_idx);
  70843. DUK_BW_PUSH_AS_STRING(lex_ctx->thr, &lex_ctx->bw);
  70844. duk_replace(lex_ctx->thr, valstack_idx);
  70845. return duk_known_hstring(lex_ctx->thr, valstack_idx);
  70846. }
  70847. /*
  70848. * Init lexer context
  70849. */
  70850. DUK_INTERNAL void duk_lexer_initctx(duk_lexer_ctx *lex_ctx) {
  70851. DUK_ASSERT(lex_ctx != NULL);
  70852. duk_memzero(lex_ctx, sizeof(*lex_ctx));
  70853. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  70854. #if defined(DUK_USE_LEXER_SLIDING_WINDOW)
  70855. lex_ctx->window = NULL;
  70856. #endif
  70857. lex_ctx->thr = NULL;
  70858. lex_ctx->input = NULL;
  70859. lex_ctx->buf = NULL;
  70860. #endif
  70861. }
  70862. /*
  70863. * Set lexer input position and reinitialize lookup window.
  70864. */
  70865. DUK_INTERNAL void duk_lexer_getpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt) {
  70866. pt->offset = lex_ctx->window[0].offset;
  70867. pt->line = lex_ctx->window[0].line;
  70868. }
  70869. DUK_INTERNAL void duk_lexer_setpoint(duk_lexer_ctx *lex_ctx, duk_lexer_point *pt) {
  70870. DUK_ASSERT_DISABLE(pt->offset >= 0); /* unsigned */
  70871. DUK_ASSERT(pt->line >= 1);
  70872. lex_ctx->input_offset = pt->offset;
  70873. lex_ctx->input_line = pt->line;
  70874. duk__init_lexer_window(lex_ctx);
  70875. }
  70876. /*
  70877. * Lexing helpers
  70878. */
  70879. /* Numeric value of a hex digit (also covers octal and decimal digits) or
  70880. * -1 if not a valid hex digit.
  70881. */
  70882. DUK_LOCAL duk_codepoint_t duk__hexval_validate(duk_codepoint_t x) {
  70883. duk_small_int_t t;
  70884. /* Here 'x' is a Unicode codepoint */
  70885. if (DUK_LIKELY(x >= 0 && x <= 0xff)) {
  70886. t = duk_hex_dectab[x];
  70887. if (DUK_LIKELY(t >= 0)) {
  70888. return t;
  70889. }
  70890. }
  70891. return -1;
  70892. }
  70893. /* Just a wrapper for call sites where 'x' is known to be valid so
  70894. * we assert for it before decoding.
  70895. */
  70896. DUK_LOCAL duk_codepoint_t duk__hexval(duk_codepoint_t x) {
  70897. duk_codepoint_t ret;
  70898. DUK_ASSERT((x >= DUK_ASC_0 && x <= DUK_ASC_9) ||
  70899. (x >= DUK_ASC_LC_A && x <= DUK_ASC_LC_F) ||
  70900. (x >= DUK_ASC_UC_A && x <= DUK_ASC_UC_F));
  70901. ret = duk__hexval_validate(x);
  70902. DUK_ASSERT(ret >= 0 && ret <= 15);
  70903. return ret;
  70904. }
  70905. /* having this as a separate function provided a size benefit */
  70906. DUK_LOCAL duk_bool_t duk__is_hex_digit(duk_codepoint_t x) {
  70907. if (DUK_LIKELY(x >= 0 && x <= 0xff)) {
  70908. return (duk_hex_dectab[x] >= 0);
  70909. }
  70910. return 0;
  70911. }
  70912. /* Parse a Unicode escape of the form \xHH, \uHHHH, or \u{H+}. Shared by
  70913. * source and RegExp parsing.
  70914. */
  70915. DUK_LOCAL duk_codepoint_t duk__lexer_parse_escape(duk_lexer_ctx *lex_ctx, duk_bool_t allow_es6) {
  70916. duk_small_int_t digits; /* Initial value 2 or 4 for fixed length escapes, 0 for ES2015 \u{H+}. */
  70917. duk_codepoint_t escval;
  70918. duk_codepoint_t x;
  70919. duk_small_uint_t adv;
  70920. DUK_ASSERT(DUK__L0() == DUK_ASC_BACKSLASH); /* caller responsibilities */
  70921. DUK_ASSERT(DUK__L1() == DUK_ASC_LC_X || DUK__L1() == DUK_ASC_LC_U);
  70922. DUK_UNREF(allow_es6);
  70923. adv = 2;
  70924. digits = 2;
  70925. if (DUK__L1() == DUK_ASC_LC_U) {
  70926. digits = 4;
  70927. #if defined(DUK_USE_ES6_UNICODE_ESCAPE)
  70928. if (DUK__L2() == DUK_ASC_LCURLY && allow_es6) {
  70929. digits = 0;
  70930. adv = 3;
  70931. }
  70932. #endif
  70933. }
  70934. DUK__ADVANCECHARS(lex_ctx, adv);
  70935. escval = 0;
  70936. for (;;) {
  70937. /* One of the escape forms: \xHH, \uHHHH, \u{H+}.
  70938. * The 'digits' variable tracks parsing state and is
  70939. * initialized to:
  70940. *
  70941. * \xHH 2
  70942. * \uHH 4
  70943. * \u{H+} 0 first time, updated to -1 to indicate
  70944. * at least one digit has been parsed
  70945. *
  70946. * Octal parsing is handled separately because it can be
  70947. * done with fixed lookahead and also has validation
  70948. * rules which depend on the escape length (which is
  70949. * variable).
  70950. *
  70951. * We don't need a specific check for x < 0 (end of
  70952. * input) or duk_unicode_is_line_terminator(x)
  70953. * because the 'dig' decode will fail and lead to a
  70954. * SyntaxError.
  70955. */
  70956. duk_codepoint_t dig;
  70957. x = DUK__L0();
  70958. DUK__ADVANCECHARS(lex_ctx, 1);
  70959. dig = duk__hexval_validate(x);
  70960. if (digits > 0) {
  70961. digits--;
  70962. if (dig < 0) {
  70963. goto fail_escape;
  70964. }
  70965. DUK_ASSERT(dig >= 0x00 && dig <= 0x0f);
  70966. escval = (escval << 4) + dig;
  70967. if (digits == 0) {
  70968. DUK_ASSERT(escval >= 0 && escval <= 0xffffL);
  70969. break;
  70970. }
  70971. } else {
  70972. #if defined(DUK_USE_ES6_UNICODE_ESCAPE)
  70973. DUK_ASSERT(digits == 0 /* first time */ || digits == -1 /* others */);
  70974. if (dig >= 0) {
  70975. DUK_ASSERT(dig >= 0x00 && dig <= 0x0f);
  70976. escval = (escval << 4) + dig;
  70977. if (escval > 0x10ffffL) {
  70978. goto fail_escape;
  70979. }
  70980. } else if (x == DUK_ASC_RCURLY) {
  70981. if (digits == 0) {
  70982. /* Empty escape, \u{}. */
  70983. goto fail_escape;
  70984. }
  70985. DUK_ASSERT(escval >= 0 && escval <= 0x10ffffL);
  70986. break;
  70987. } else {
  70988. goto fail_escape;
  70989. }
  70990. digits = -1; /* Indicate we have at least one digit. */
  70991. #else /* DUK_USE_ES6_UNICODE_ESCAPE */
  70992. DUK_ASSERT(0); /* Never happens if \u{H+} support disabled. */
  70993. #endif /* DUK_USE_ES6_UNICODE_ESCAPE */
  70994. }
  70995. }
  70996. return escval;
  70997. fail_escape:
  70998. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_ESCAPE);
  70999. DUK_WO_NORETURN(return 0;);
  71000. }
  71001. /* Parse legacy octal escape of the form \N{1,3}, e.g. \0, \5, \0377. Maximum
  71002. * allowed value is \0377 (U+00FF), longest match is used. Used for both string
  71003. * RegExp octal escape parsing. Window[0] must be the slash '\' and the first
  71004. * digit must already be validated to be in [0-9] by the caller.
  71005. */
  71006. DUK_LOCAL duk_codepoint_t duk__lexer_parse_legacy_octal(duk_lexer_ctx *lex_ctx, duk_small_uint_t *out_adv, duk_bool_t reject_annex_b) {
  71007. duk_codepoint_t cp;
  71008. duk_small_uint_t lookup_idx;
  71009. duk_small_uint_t adv;
  71010. duk_codepoint_t tmp;
  71011. DUK_ASSERT(out_adv != NULL);
  71012. DUK_ASSERT(DUK__LOOKUP(lex_ctx, 0) == DUK_ASC_BACKSLASH);
  71013. DUK_ASSERT(DUK__LOOKUP(lex_ctx, 1) >= DUK_ASC_0 && DUK__LOOKUP(lex_ctx, 1) <= DUK_ASC_9);
  71014. cp = 0;
  71015. tmp = 0;
  71016. for (lookup_idx = 1; lookup_idx <= 3; lookup_idx++) {
  71017. DUK_DDD(DUK_DDDPRINT("lookup_idx=%ld, cp=%ld", (long) lookup_idx, (long) cp));
  71018. tmp = DUK__LOOKUP(lex_ctx, lookup_idx);
  71019. if (tmp < DUK_ASC_0 || tmp > DUK_ASC_7) {
  71020. /* No more valid digits. */
  71021. break;
  71022. }
  71023. tmp = (cp << 3) + (tmp - DUK_ASC_0);
  71024. if (tmp > 0xff) {
  71025. /* Three digit octal escapes above \377 (= 0xff)
  71026. * are not allowed.
  71027. */
  71028. break;
  71029. }
  71030. cp = tmp;
  71031. }
  71032. DUK_DDD(DUK_DDDPRINT("final lookup_idx=%ld, cp=%ld", (long) lookup_idx, (long) cp));
  71033. adv = lookup_idx;
  71034. if (lookup_idx == 1) {
  71035. DUK_DDD(DUK_DDDPRINT("\\8 or \\9 -> treat as literal, accept in strict mode too"));
  71036. DUK_ASSERT(tmp == DUK_ASC_8 || tmp == DUK_ASC_9);
  71037. cp = tmp;
  71038. adv++; /* correction to above, eat offending character */
  71039. } else if (lookup_idx == 2 && cp == 0) {
  71040. /* Note: 'foo\0bar' is OK in strict mode, but 'foo\00bar' is not.
  71041. * It won't be interpreted as 'foo\u{0}0bar' but as a SyntaxError.
  71042. */
  71043. DUK_DDD(DUK_DDDPRINT("\\0 -> accept in strict mode too"));
  71044. } else {
  71045. /* This clause also handles non-shortest zero, e.g. \00. */
  71046. if (reject_annex_b) {
  71047. DUK_DDD(DUK_DDDPRINT("non-zero octal literal %ld -> reject in strict-mode", (long) cp));
  71048. cp = -1;
  71049. } else {
  71050. DUK_DDD(DUK_DDDPRINT("non-zero octal literal %ld -> accepted", (long) cp));
  71051. DUK_ASSERT(cp >= 0 && cp <= 0xff);
  71052. }
  71053. }
  71054. *out_adv = adv;
  71055. DUK_ASSERT((cp >= 0 && cp <= 0xff) || (cp == -1 && reject_annex_b));
  71056. return cp;
  71057. }
  71058. /* XXX: move strict mode to lex_ctx? */
  71059. DUK_LOCAL void duk__lexer_parse_string_literal(duk_lexer_ctx *lex_ctx, duk_token *out_token, duk_small_int_t quote, duk_bool_t strict_mode) {
  71060. duk_small_uint_t adv;
  71061. for (adv = 1 /* initial quote */ ;;) {
  71062. duk_codepoint_t x;
  71063. DUK__ADVANCECHARS(lex_ctx, adv); /* eat opening quote on first loop */
  71064. x = DUK__L0();
  71065. adv = 1;
  71066. if (x == quote) {
  71067. DUK__ADVANCECHARS(lex_ctx, 1); /* eat closing quote */
  71068. break;
  71069. } else if (x == '\\') {
  71070. /* DUK__L0 -> '\' char
  71071. * DUK__L1 ... DUK__L5 -> more lookup
  71072. */
  71073. duk_small_int_t emitcp = -1;
  71074. x = DUK__L1();
  71075. /* How much to advance before next loop. */
  71076. adv = 2; /* note: long live range */
  71077. switch (x) {
  71078. case '\'':
  71079. emitcp = 0x0027;
  71080. break;
  71081. case '"':
  71082. emitcp = 0x0022;
  71083. break;
  71084. case '\\':
  71085. emitcp = 0x005c;
  71086. break;
  71087. case 'b':
  71088. emitcp = 0x0008;
  71089. break;
  71090. case 'f':
  71091. emitcp = 0x000c;
  71092. break;
  71093. case 'n':
  71094. emitcp = 0x000a;
  71095. break;
  71096. case 'r':
  71097. emitcp = 0x000d;
  71098. break;
  71099. case 't':
  71100. emitcp = 0x0009;
  71101. break;
  71102. case 'v':
  71103. emitcp = 0x000b;
  71104. break;
  71105. case 'x':
  71106. case 'u': {
  71107. duk_codepoint_t esc_cp;
  71108. esc_cp = duk__lexer_parse_escape(lex_ctx, 1 /*allow_es6*/);
  71109. DUK__APPENDBUFFER(lex_ctx, esc_cp);
  71110. adv = 0;
  71111. break;
  71112. }
  71113. default: {
  71114. if (duk_unicode_is_line_terminator(x)) {
  71115. /* line continuation */
  71116. if (x == 0x000d && DUK__L2() == 0x000a) {
  71117. /* CR LF again a special case */
  71118. adv = 3; /* line terminator, CR, LF */
  71119. }
  71120. } else if (DUK__ISDIGIT(x)) {
  71121. /*
  71122. * Octal escape or zero escape:
  71123. * \0 (lookahead not OctalDigit)
  71124. * \1 ... \7 (lookahead not OctalDigit)
  71125. * \ZeroToThree OctalDigit (lookahead not OctalDigit)
  71126. * \FourToSeven OctalDigit (no lookahead restrictions)
  71127. * \ZeroToThree OctalDigit OctalDigit (no lookahead restrictions)
  71128. *
  71129. * Zero escape is part of the standard syntax. Octal escapes are
  71130. * defined in E5 Section B.1.2, and are only allowed in non-strict mode.
  71131. * Any other productions starting with a decimal digit are invalid
  71132. * but are in practice treated like identity escapes.
  71133. *
  71134. * Parse octal (up to 3 digits) from the lookup window.
  71135. */
  71136. emitcp = duk__lexer_parse_legacy_octal(lex_ctx, &adv, strict_mode /*reject_annex_b*/);
  71137. if (emitcp < 0) {
  71138. goto fail_escape;
  71139. }
  71140. } else if (x < 0) {
  71141. goto fail_unterminated;
  71142. } else {
  71143. /* escaped NonEscapeCharacter */
  71144. DUK__APPENDBUFFER(lex_ctx, x);
  71145. }
  71146. } /* end default clause */
  71147. } /* end switch */
  71148. /* Shared handling for single codepoint escapes. */
  71149. if (emitcp >= 0) {
  71150. DUK__APPENDBUFFER(lex_ctx, emitcp);
  71151. }
  71152. /* Track number of escapes; count not really needed but directive
  71153. * prologues need to detect whether there were any escapes or line
  71154. * continuations or not.
  71155. */
  71156. out_token->num_escapes++;
  71157. } else if (x >= 0x20 && x <= 0x7f) {
  71158. /* Fast path for ASCII case, avoids line terminator
  71159. * check and CESU-8 encoding.
  71160. */
  71161. DUK_ASSERT(x >= 0);
  71162. DUK_ASSERT(!duk_unicode_is_line_terminator(x));
  71163. DUK_ASSERT(x != quote);
  71164. DUK_ASSERT(x != DUK_ASC_BACKSLASH);
  71165. DUK__APPENDBUFFER_ASCII(lex_ctx, x);
  71166. } else if (x < 0 || duk_unicode_is_line_terminator(x)) {
  71167. goto fail_unterminated;
  71168. } else {
  71169. /* Character which is part of the string but wasn't handled
  71170. * by the fast path.
  71171. */
  71172. DUK__APPENDBUFFER(lex_ctx, x);
  71173. }
  71174. } /* string parse loop */
  71175. return;
  71176. fail_escape:
  71177. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_ESCAPE);
  71178. DUK_WO_NORETURN(return;);
  71179. fail_unterminated:
  71180. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_UNTERMINATED_STRING);
  71181. DUK_WO_NORETURN(return;);
  71182. }
  71183. /* Skip to end-of-line (or end-of-file), used for single line comments. */
  71184. DUK_LOCAL void duk__lexer_skip_to_endofline(duk_lexer_ctx *lex_ctx) {
  71185. for (;;) {
  71186. duk_codepoint_t x;
  71187. x = DUK__L0();
  71188. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  71189. break;
  71190. }
  71191. DUK__ADVANCECHARS(lex_ctx, 1);
  71192. }
  71193. }
  71194. /*
  71195. * Parse ECMAScript source InputElementDiv or InputElementRegExp
  71196. * (E5 Section 7), skipping whitespace, comments, and line terminators.
  71197. *
  71198. * Possible results are:
  71199. * (1) a token
  71200. * (2) a line terminator (skipped)
  71201. * (3) a comment (skipped)
  71202. * (4) EOF
  71203. *
  71204. * White space is automatically skipped from the current position (but
  71205. * not after the input element). If input has already ended, returns
  71206. * DUK_TOK_EOF indefinitely. If a parse error occurs, uses an DUK_ERROR()
  71207. * macro call (and hence a longjmp through current heap longjmp context).
  71208. * Comments and line terminator tokens are automatically skipped.
  71209. *
  71210. * The input element being matched is determined by regexp_mode; if set,
  71211. * parses a InputElementRegExp, otherwise a InputElementDiv. The
  71212. * difference between these are handling of productions starting with a
  71213. * forward slash.
  71214. *
  71215. * If strict_mode is set, recognizes additional future reserved words
  71216. * specific to strict mode, and refuses to parse octal literals.
  71217. *
  71218. * The matching strategy below is to (currently) use a six character
  71219. * lookup window to quickly determine which production is the -longest-
  71220. * matching one, and then parse that. The top-level if-else clauses
  71221. * match the first character, and the code blocks for each clause
  71222. * handle -all- alternatives for that first character. ECMAScript
  71223. * specification uses the "longest match wins" semantics, so the order
  71224. * of the if-clauses matters.
  71225. *
  71226. * Misc notes:
  71227. *
  71228. * * ECMAScript numeric literals do not accept a sign character.
  71229. * Consequently e.g. "-1.0" is parsed as two tokens: a negative
  71230. * sign and a positive numeric literal. The compiler performs
  71231. * the negation during compilation, so this has no adverse impact.
  71232. *
  71233. * * There is no token for "undefined": it is just a value available
  71234. * from the global object (or simply established by doing a reference
  71235. * to an undefined value).
  71236. *
  71237. * * Some contexts want Identifier tokens, which are IdentifierNames
  71238. * excluding reserved words, while some contexts want IdentifierNames
  71239. * directly. In the latter case e.g. "while" is interpreted as an
  71240. * identifier name, not a DUK_TOK_WHILE token. The solution here is
  71241. * to provide both token types: DUK_TOK_WHILE goes to 't' while
  71242. * DUK_TOK_IDENTIFIER goes to 't_nores', and 'slot1' always contains
  71243. * the identifier / keyword name.
  71244. *
  71245. * * Directive prologue needs to identify string literals such as
  71246. * "use strict" and 'use strict', which are sensitive to line
  71247. * continuations and escape sequences. For instance, "use\u0020strict"
  71248. * is a valid directive but is distinct from "use strict". The solution
  71249. * here is to decode escapes while tokenizing, but to keep track of the
  71250. * number of escapes. Directive detection can then check that the
  71251. * number of escapes is zero.
  71252. *
  71253. * * Multi-line comments with one or more internal LineTerminator are
  71254. * treated like a line terminator to comply with automatic semicolon
  71255. * insertion.
  71256. */
  71257. DUK_INTERNAL
  71258. void duk_lexer_parse_js_input_element(duk_lexer_ctx *lex_ctx,
  71259. duk_token *out_token,
  71260. duk_bool_t strict_mode,
  71261. duk_bool_t regexp_mode) {
  71262. duk_codepoint_t x; /* temporary, must be signed and 32-bit to hold Unicode code points */
  71263. duk_small_uint_t advtok = 0; /* (advance << 8) + token_type, updated at function end,
  71264. * init is unnecessary but suppresses "may be used uninitialized" warnings.
  71265. */
  71266. duk_bool_t got_lineterm = 0; /* got lineterm preceding non-whitespace, non-lineterm token */
  71267. if (++lex_ctx->token_count >= lex_ctx->token_limit) {
  71268. goto fail_token_limit;
  71269. }
  71270. out_token->t = DUK_TOK_EOF;
  71271. out_token->t_nores = DUK_TOK_INVALID; /* marker: copy t if not changed */
  71272. #if 0 /* not necessary to init, disabled for faster parsing */
  71273. out_token->num = DUK_DOUBLE_NAN;
  71274. out_token->str1 = NULL;
  71275. out_token->str2 = NULL;
  71276. #endif
  71277. out_token->num_escapes = 0;
  71278. /* out_token->lineterm set by caller */
  71279. /* This would be nice, but parsing is faster without resetting the
  71280. * value slots. The only side effect is that references to temporary
  71281. * string values may linger until lexing is finished; they're then
  71282. * freed normally.
  71283. */
  71284. #if 0
  71285. duk_to_undefined(lex_ctx->thr, lex_ctx->slot1_idx);
  71286. duk_to_undefined(lex_ctx->thr, lex_ctx->slot2_idx);
  71287. #endif
  71288. /* 'advtok' indicates how much to advance and which token id to assign
  71289. * at the end. This shared functionality minimizes code size. All
  71290. * code paths are required to set 'advtok' to some value, so no default
  71291. * init value is used. Code paths calling DUK_ERROR() never return so
  71292. * they don't need to set advtok.
  71293. */
  71294. /*
  71295. * Matching order:
  71296. *
  71297. * Punctuator first chars, also covers comments, regexps
  71298. * LineTerminator
  71299. * Identifier or reserved word, also covers null/true/false literals
  71300. * NumericLiteral
  71301. * StringLiteral
  71302. * EOF
  71303. *
  71304. * The order does not matter as long as the longest match is
  71305. * always correctly identified. There are order dependencies
  71306. * in the clauses, so it's not trivial to convert to a switch.
  71307. */
  71308. restart_lineupdate:
  71309. out_token->start_line = lex_ctx->window[0].line;
  71310. restart:
  71311. out_token->start_offset = lex_ctx->window[0].offset;
  71312. x = DUK__L0();
  71313. switch (x) {
  71314. case DUK_ASC_SPACE:
  71315. case DUK_ASC_HT: /* fast paths for space and tab */
  71316. DUK__ADVANCECHARS(lex_ctx, 1);
  71317. goto restart;
  71318. case DUK_ASC_LF: /* LF line terminator; CR LF and Unicode lineterms are handled in slow path */
  71319. DUK__ADVANCECHARS(lex_ctx, 1);
  71320. got_lineterm = 1;
  71321. goto restart_lineupdate;
  71322. #if defined(DUK_USE_SHEBANG_COMMENTS)
  71323. case DUK_ASC_HASH: /* '#' */
  71324. if (DUK__L1() == DUK_ASC_EXCLAMATION && lex_ctx->window[0].offset == 0 &&
  71325. (lex_ctx->flags & DUK_COMPILE_SHEBANG)) {
  71326. /* "Shebang" comment ('#! ...') on first line. */
  71327. /* DUK__ADVANCECHARS(lex_ctx, 2) would be correct here, but not necessary */
  71328. duk__lexer_skip_to_endofline(lex_ctx);
  71329. goto restart; /* line terminator will be handled on next round */
  71330. }
  71331. goto fail_token;
  71332. #endif /* DUK_USE_SHEBANG_COMMENTS */
  71333. case DUK_ASC_SLASH: /* '/' */
  71334. if (DUK__L1() == DUK_ASC_SLASH) {
  71335. /*
  71336. * E5 Section 7.4, allow SourceCharacter (which is any 16-bit
  71337. * code point).
  71338. */
  71339. /* DUK__ADVANCECHARS(lex_ctx, 2) would be correct here, but not necessary */
  71340. duk__lexer_skip_to_endofline(lex_ctx);
  71341. goto restart; /* line terminator will be handled on next round */
  71342. } else if (DUK__L1() == DUK_ASC_STAR) {
  71343. /*
  71344. * E5 Section 7.4. If the multi-line comment contains a newline,
  71345. * it is treated like a single line terminator for automatic
  71346. * semicolon insertion.
  71347. */
  71348. duk_bool_t last_asterisk = 0;
  71349. DUK__ADVANCECHARS(lex_ctx, 2);
  71350. for (;;) {
  71351. x = DUK__L0();
  71352. if (x < 0) {
  71353. goto fail_unterm_comment;
  71354. }
  71355. DUK__ADVANCECHARS(lex_ctx, 1);
  71356. if (last_asterisk && x == DUK_ASC_SLASH) {
  71357. break;
  71358. }
  71359. if (duk_unicode_is_line_terminator(x)) {
  71360. got_lineterm = 1;
  71361. }
  71362. last_asterisk = (x == DUK_ASC_STAR);
  71363. }
  71364. goto restart_lineupdate;
  71365. } else if (regexp_mode) {
  71366. #if defined(DUK_USE_REGEXP_SUPPORT)
  71367. /*
  71368. * "/" followed by something in regexp mode. See E5 Section 7.8.5.
  71369. *
  71370. * RegExp parsing is a bit complex. First, the regexp body is delimited
  71371. * by forward slashes, but the body may also contain forward slashes as
  71372. * part of an escape sequence or inside a character class (delimited by
  71373. * square brackets). A mini state machine is used to implement these.
  71374. *
  71375. * Further, an early (parse time) error must be thrown if the regexp
  71376. * would cause a run-time error when used in the expression new RegExp(...).
  71377. * Parsing here simply extracts the (candidate) regexp, and also accepts
  71378. * invalid regular expressions (which are delimited properly). The caller
  71379. * (compiler) must perform final validation and regexp compilation.
  71380. *
  71381. * RegExp first char may not be '/' (single line comment) or '*' (multi-
  71382. * line comment). These have already been checked above, so there is no
  71383. * need below for special handling of the first regexp character as in
  71384. * the E5 productions.
  71385. *
  71386. * About unicode escapes within regexp literals:
  71387. *
  71388. * E5 Section 7.8.5 grammar does NOT accept \uHHHH escapes.
  71389. * However, Section 6 states that regexps accept the escapes,
  71390. * see paragraph starting with "In string literals...".
  71391. * The regexp grammar, which sees the decoded regexp literal
  71392. * (after lexical parsing) DOES have a \uHHHH unicode escape.
  71393. * So, for instance:
  71394. *
  71395. * /\u1234/
  71396. *
  71397. * should first be parsed by the lexical grammar as:
  71398. *
  71399. * '\' 'u' RegularExpressionBackslashSequence
  71400. * '1' RegularExpressionNonTerminator
  71401. * '2' RegularExpressionNonTerminator
  71402. * '3' RegularExpressionNonTerminator
  71403. * '4' RegularExpressionNonTerminator
  71404. *
  71405. * and the escape itself is then parsed by the regexp engine.
  71406. * This is the current implementation.
  71407. *
  71408. * Minor spec inconsistency:
  71409. *
  71410. * E5 Section 7.8.5 RegularExpressionBackslashSequence is:
  71411. *
  71412. * \ RegularExpressionNonTerminator
  71413. *
  71414. * while Section A.1 RegularExpressionBackslashSequence is:
  71415. *
  71416. * \ NonTerminator
  71417. *
  71418. * The latter is not normative and a typo.
  71419. *
  71420. */
  71421. /* first, parse regexp body roughly */
  71422. duk_small_int_t state = 0; /* 0=base, 1=esc, 2=class, 3=class+esc */
  71423. DUK__INITBUFFER(lex_ctx);
  71424. for (;;) {
  71425. DUK__ADVANCECHARS(lex_ctx, 1); /* skip opening slash on first loop */
  71426. x = DUK__L0();
  71427. if (x < 0 || duk_unicode_is_line_terminator(x)) {
  71428. goto fail_unterm_regexp;
  71429. }
  71430. x = DUK__L0(); /* re-read to avoid spill / fetch */
  71431. if (state == 0) {
  71432. if (x == DUK_ASC_SLASH) {
  71433. DUK__ADVANCECHARS(lex_ctx, 1); /* eat closing slash */
  71434. break;
  71435. } else if (x == DUK_ASC_BACKSLASH) {
  71436. state = 1;
  71437. } else if (x == DUK_ASC_LBRACKET) {
  71438. state = 2;
  71439. }
  71440. } else if (state == 1) {
  71441. state = 0;
  71442. } else if (state == 2) {
  71443. if (x == DUK_ASC_RBRACKET) {
  71444. state = 0;
  71445. } else if (x == DUK_ASC_BACKSLASH) {
  71446. state = 3;
  71447. }
  71448. } else { /* state == 3 */
  71449. state = 2;
  71450. }
  71451. DUK__APPENDBUFFER(lex_ctx, x);
  71452. }
  71453. out_token->str1 = duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  71454. /* second, parse flags */
  71455. DUK__INITBUFFER(lex_ctx);
  71456. for (;;) {
  71457. x = DUK__L0();
  71458. if (!duk_unicode_is_identifier_part(x)) {
  71459. break;
  71460. }
  71461. x = DUK__L0(); /* re-read to avoid spill / fetch */
  71462. DUK__APPENDBUFFER(lex_ctx, x);
  71463. DUK__ADVANCECHARS(lex_ctx, 1);
  71464. }
  71465. out_token->str2 = duk__internbuffer(lex_ctx, lex_ctx->slot2_idx);
  71466. DUK__INITBUFFER(lex_ctx); /* free some memory */
  71467. /* validation of the regexp is caller's responsibility */
  71468. advtok = DUK__ADVTOK(0, DUK_TOK_REGEXP);
  71469. #else /* DUK_USE_REGEXP_SUPPORT */
  71470. goto fail_regexp_support;
  71471. #endif /* DUK_USE_REGEXP_SUPPORT */
  71472. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71473. /* "/=" and not in regexp mode */
  71474. advtok = DUK__ADVTOK(2, DUK_TOK_DIV_EQ);
  71475. } else {
  71476. /* "/" and not in regexp mode */
  71477. advtok = DUK__ADVTOK(1, DUK_TOK_DIV);
  71478. }
  71479. break;
  71480. case DUK_ASC_LCURLY: /* '{' */
  71481. advtok = DUK__ADVTOK(1, DUK_TOK_LCURLY);
  71482. break;
  71483. case DUK_ASC_RCURLY: /* '}' */
  71484. advtok = DUK__ADVTOK(1, DUK_TOK_RCURLY);
  71485. break;
  71486. case DUK_ASC_LPAREN: /* '(' */
  71487. advtok = DUK__ADVTOK(1, DUK_TOK_LPAREN);
  71488. break;
  71489. case DUK_ASC_RPAREN: /* ')' */
  71490. advtok = DUK__ADVTOK(1, DUK_TOK_RPAREN);
  71491. break;
  71492. case DUK_ASC_LBRACKET: /* '[' */
  71493. advtok = DUK__ADVTOK(1, DUK_TOK_LBRACKET);
  71494. break;
  71495. case DUK_ASC_RBRACKET: /* ']' */
  71496. advtok = DUK__ADVTOK(1, DUK_TOK_RBRACKET);
  71497. break;
  71498. case DUK_ASC_PERIOD: /* '.' */
  71499. if (DUK__ISDIGIT(DUK__L1())) {
  71500. /* Period followed by a digit can only start DecimalLiteral
  71501. * (handled in slow path). We could jump straight into the
  71502. * DecimalLiteral handling but should avoid goto to inside
  71503. * a block.
  71504. */
  71505. goto slow_path;
  71506. }
  71507. advtok = DUK__ADVTOK(1, DUK_TOK_PERIOD);
  71508. break;
  71509. case DUK_ASC_SEMICOLON: /* ';' */
  71510. advtok = DUK__ADVTOK(1, DUK_TOK_SEMICOLON);
  71511. break;
  71512. case DUK_ASC_COMMA: /* ',' */
  71513. advtok = DUK__ADVTOK(1, DUK_TOK_COMMA);
  71514. break;
  71515. case DUK_ASC_LANGLE: /* '<' */
  71516. #if defined(DUK_USE_HTML_COMMENTS)
  71517. if (DUK__L1() == DUK_ASC_EXCLAMATION && DUK__L2() == DUK_ASC_MINUS && DUK__L3() == DUK_ASC_MINUS) {
  71518. /*
  71519. * ES2015: B.1.3, handle "<!--" SingleLineHTMLOpenComment
  71520. */
  71521. /* DUK__ADVANCECHARS(lex_ctx, 4) would be correct here, but not necessary */
  71522. duk__lexer_skip_to_endofline(lex_ctx);
  71523. goto restart; /* line terminator will be handled on next round */
  71524. }
  71525. else
  71526. #endif /* DUK_USE_HTML_COMMENTS */
  71527. if (DUK__L1() == DUK_ASC_LANGLE && DUK__L2() == DUK_ASC_EQUALS) {
  71528. advtok = DUK__ADVTOK(3, DUK_TOK_ALSHIFT_EQ);
  71529. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71530. advtok = DUK__ADVTOK(2, DUK_TOK_LE);
  71531. } else if (DUK__L1() == DUK_ASC_LANGLE) {
  71532. advtok = DUK__ADVTOK(2, DUK_TOK_ALSHIFT);
  71533. } else {
  71534. advtok = DUK__ADVTOK(1, DUK_TOK_LT);
  71535. }
  71536. break;
  71537. case DUK_ASC_RANGLE: /* '>' */
  71538. if (DUK__L1() == DUK_ASC_RANGLE && DUK__L2() == DUK_ASC_RANGLE && DUK__L3() == DUK_ASC_EQUALS) {
  71539. advtok = DUK__ADVTOK(4, DUK_TOK_RSHIFT_EQ);
  71540. } else if (DUK__L1() == DUK_ASC_RANGLE && DUK__L2() == DUK_ASC_RANGLE) {
  71541. advtok = DUK__ADVTOK(3, DUK_TOK_RSHIFT);
  71542. } else if (DUK__L1() == DUK_ASC_RANGLE && DUK__L2() == DUK_ASC_EQUALS) {
  71543. advtok = DUK__ADVTOK(3, DUK_TOK_ARSHIFT_EQ);
  71544. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71545. advtok = DUK__ADVTOK(2, DUK_TOK_GE);
  71546. } else if (DUK__L1() == DUK_ASC_RANGLE) {
  71547. advtok = DUK__ADVTOK(2, DUK_TOK_ARSHIFT);
  71548. } else {
  71549. advtok = DUK__ADVTOK(1, DUK_TOK_GT);
  71550. }
  71551. break;
  71552. case DUK_ASC_EQUALS: /* '=' */
  71553. if (DUK__L1() == DUK_ASC_EQUALS && DUK__L2() == DUK_ASC_EQUALS) {
  71554. advtok = DUK__ADVTOK(3, DUK_TOK_SEQ);
  71555. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71556. advtok = DUK__ADVTOK(2, DUK_TOK_EQ);
  71557. } else {
  71558. advtok = DUK__ADVTOK(1, DUK_TOK_EQUALSIGN);
  71559. }
  71560. break;
  71561. case DUK_ASC_EXCLAMATION: /* '!' */
  71562. if (DUK__L1() == DUK_ASC_EQUALS && DUK__L2() == DUK_ASC_EQUALS) {
  71563. advtok = DUK__ADVTOK(3, DUK_TOK_SNEQ);
  71564. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71565. advtok = DUK__ADVTOK(2, DUK_TOK_NEQ);
  71566. } else {
  71567. advtok = DUK__ADVTOK(1, DUK_TOK_LNOT);
  71568. }
  71569. break;
  71570. case DUK_ASC_PLUS: /* '+' */
  71571. if (DUK__L1() == DUK_ASC_PLUS) {
  71572. advtok = DUK__ADVTOK(2, DUK_TOK_INCREMENT);
  71573. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71574. advtok = DUK__ADVTOK(2, DUK_TOK_ADD_EQ);
  71575. } else {
  71576. advtok = DUK__ADVTOK(1, DUK_TOK_ADD);
  71577. }
  71578. break;
  71579. case DUK_ASC_MINUS: /* '-' */
  71580. #if defined(DUK_USE_HTML_COMMENTS)
  71581. if (got_lineterm && DUK__L1() == DUK_ASC_MINUS && DUK__L2() == DUK_ASC_RANGLE) {
  71582. /*
  71583. * ES2015: B.1.3, handle "-->" SingleLineHTMLCloseComment
  71584. * Only allowed:
  71585. * - on new line
  71586. * - preceded only by whitespace
  71587. * - preceded by end of multiline comment and optional whitespace
  71588. *
  71589. * Since whitespace generates no tokens, and multiline comments
  71590. * are treated as a line ending, consulting `got_lineterm` is
  71591. * sufficient to test for these three options.
  71592. */
  71593. /* DUK__ADVANCECHARS(lex_ctx, 3) would be correct here, but not necessary */
  71594. duk__lexer_skip_to_endofline(lex_ctx);
  71595. goto restart; /* line terminator will be handled on next round */
  71596. } else
  71597. #endif /* DUK_USE_HTML_COMMENTS */
  71598. if (DUK__L1() == DUK_ASC_MINUS) {
  71599. advtok = DUK__ADVTOK(2, DUK_TOK_DECREMENT);
  71600. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71601. advtok = DUK__ADVTOK(2, DUK_TOK_SUB_EQ);
  71602. } else {
  71603. advtok = DUK__ADVTOK(1, DUK_TOK_SUB);
  71604. }
  71605. break;
  71606. case DUK_ASC_STAR: /* '*' */
  71607. #if defined(DUK_USE_ES7_EXP_OPERATOR)
  71608. if (DUK__L1() == DUK_ASC_STAR && DUK__L2() == DUK_ASC_EQUALS) {
  71609. advtok = DUK__ADVTOK(3, DUK_TOK_EXP_EQ);
  71610. } else if (DUK__L1() == DUK_ASC_STAR) {
  71611. advtok = DUK__ADVTOK(2, DUK_TOK_EXP);
  71612. } else
  71613. #endif
  71614. if (DUK__L1() == DUK_ASC_EQUALS) {
  71615. advtok = DUK__ADVTOK(2, DUK_TOK_MUL_EQ);
  71616. } else {
  71617. advtok = DUK__ADVTOK(1, DUK_TOK_MUL);
  71618. }
  71619. break;
  71620. case DUK_ASC_PERCENT: /* '%' */
  71621. if (DUK__L1() == DUK_ASC_EQUALS) {
  71622. advtok = DUK__ADVTOK(2, DUK_TOK_MOD_EQ);
  71623. } else {
  71624. advtok = DUK__ADVTOK(1, DUK_TOK_MOD);
  71625. }
  71626. break;
  71627. case DUK_ASC_AMP: /* '&' */
  71628. if (DUK__L1() == DUK_ASC_AMP) {
  71629. advtok = DUK__ADVTOK(2, DUK_TOK_LAND);
  71630. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71631. advtok = DUK__ADVTOK(2, DUK_TOK_BAND_EQ);
  71632. } else {
  71633. advtok = DUK__ADVTOK(1, DUK_TOK_BAND);
  71634. }
  71635. break;
  71636. case DUK_ASC_PIPE: /* '|' */
  71637. if (DUK__L1() == DUK_ASC_PIPE) {
  71638. advtok = DUK__ADVTOK(2, DUK_TOK_LOR);
  71639. } else if (DUK__L1() == DUK_ASC_EQUALS) {
  71640. advtok = DUK__ADVTOK(2, DUK_TOK_BOR_EQ);
  71641. } else {
  71642. advtok = DUK__ADVTOK(1, DUK_TOK_BOR);
  71643. }
  71644. break;
  71645. case DUK_ASC_CARET: /* '^' */
  71646. if (DUK__L1() == DUK_ASC_EQUALS) {
  71647. advtok = DUK__ADVTOK(2, DUK_TOK_BXOR_EQ);
  71648. } else {
  71649. advtok = DUK__ADVTOK(1, DUK_TOK_BXOR);
  71650. }
  71651. break;
  71652. case DUK_ASC_TILDE: /* '~' */
  71653. advtok = DUK__ADVTOK(1, DUK_TOK_BNOT);
  71654. break;
  71655. case DUK_ASC_QUESTION: /* '?' */
  71656. advtok = DUK__ADVTOK(1, DUK_TOK_QUESTION);
  71657. break;
  71658. case DUK_ASC_COLON: /* ':' */
  71659. advtok = DUK__ADVTOK(1, DUK_TOK_COLON);
  71660. break;
  71661. case DUK_ASC_DOUBLEQUOTE: /* '"' */
  71662. case DUK_ASC_SINGLEQUOTE: { /* '\'' */
  71663. DUK__INITBUFFER(lex_ctx);
  71664. duk__lexer_parse_string_literal(lex_ctx, out_token, x /*quote*/, strict_mode);
  71665. duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  71666. out_token->str1 = duk_known_hstring(lex_ctx->thr, lex_ctx->slot1_idx);
  71667. DUK__INITBUFFER(lex_ctx); /* free some memory */
  71668. advtok = DUK__ADVTOK(0, DUK_TOK_STRING);
  71669. break;
  71670. }
  71671. default:
  71672. goto slow_path;
  71673. } /* switch */
  71674. goto skip_slow_path;
  71675. slow_path:
  71676. if (duk_unicode_is_line_terminator(x)) {
  71677. if (x == 0x000d && DUK__L1() == 0x000a) {
  71678. /*
  71679. * E5 Section 7.3: CR LF is detected as a single line terminator for
  71680. * line numbers. Here we also detect it as a single line terminator
  71681. * token.
  71682. */
  71683. DUK__ADVANCECHARS(lex_ctx, 2);
  71684. } else {
  71685. DUK__ADVANCECHARS(lex_ctx, 1);
  71686. }
  71687. got_lineterm = 1;
  71688. goto restart_lineupdate;
  71689. } else if (duk_unicode_is_identifier_start(x) || x == DUK_ASC_BACKSLASH) {
  71690. /*
  71691. * Parse an identifier and then check whether it is:
  71692. * - reserved word (keyword or other reserved word)
  71693. * - "null" (NullLiteral)
  71694. * - "true" (BooleanLiteral)
  71695. * - "false" (BooleanLiteral)
  71696. * - anything else => identifier
  71697. *
  71698. * This does not follow the E5 productions cleanly, but is
  71699. * useful and compact.
  71700. *
  71701. * Note that identifiers may contain Unicode escapes,
  71702. * see E5 Sections 6 and 7.6. They must be decoded first,
  71703. * and the result checked against allowed characters.
  71704. * The above if-clause accepts an identifier start and an
  71705. * '\' character -- no other token can begin with a '\'.
  71706. *
  71707. * Note that "get" and "set" are not reserved words in E5
  71708. * specification so they are recognized as plain identifiers
  71709. * (the tokens DUK_TOK_GET and DUK_TOK_SET are actually not
  71710. * used now). The compiler needs to work around this.
  71711. *
  71712. * Strictly speaking, following ECMAScript longest match
  71713. * specification, an invalid escape for the first character
  71714. * should cause a syntax error. However, an invalid escape
  71715. * for IdentifierParts should just terminate the identifier
  71716. * early (longest match), and let the next tokenization
  71717. * fail. For instance Rhino croaks with 'foo\z' when
  71718. * parsing the identifier. This has little practical impact.
  71719. */
  71720. duk_small_uint_t i, i_end;
  71721. duk_bool_t first = 1;
  71722. duk_hstring *str;
  71723. DUK__INITBUFFER(lex_ctx);
  71724. for (;;) {
  71725. /* re-lookup first char on first loop */
  71726. if (DUK__L0() == DUK_ASC_BACKSLASH) {
  71727. duk_codepoint_t esc_cp;
  71728. if (DUK__L1() != DUK_ASC_LC_U) {
  71729. goto fail_escape;
  71730. }
  71731. esc_cp = duk__lexer_parse_escape(lex_ctx, 1 /*allow_es6*/);
  71732. DUK__APPENDBUFFER(lex_ctx, esc_cp);
  71733. /* IdentifierStart is stricter than IdentifierPart, so if the first
  71734. * character is escaped, must have a stricter check here.
  71735. */
  71736. if (!(first ? duk_unicode_is_identifier_start(esc_cp) : duk_unicode_is_identifier_part(esc_cp))) {
  71737. goto fail_escape;
  71738. }
  71739. /* Track number of escapes: necessary for proper keyword
  71740. * detection.
  71741. */
  71742. out_token->num_escapes++;
  71743. } else {
  71744. /* Note: first character is checked against this. But because
  71745. * IdentifierPart includes all IdentifierStart characters, and
  71746. * the first character (if unescaped) has already been checked
  71747. * in the if condition, this is OK.
  71748. */
  71749. if (!duk_unicode_is_identifier_part(DUK__L0())) {
  71750. break;
  71751. }
  71752. DUK__APPENDBUFFER(lex_ctx, DUK__L0());
  71753. DUK__ADVANCECHARS(lex_ctx, 1);
  71754. }
  71755. first = 0;
  71756. }
  71757. out_token->str1 = duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  71758. str = out_token->str1;
  71759. out_token->t_nores = DUK_TOK_IDENTIFIER;
  71760. DUK__INITBUFFER(lex_ctx); /* free some memory */
  71761. /*
  71762. * Interned identifier is compared against reserved words, which are
  71763. * currently interned into the heap context. See genbuiltins.py.
  71764. *
  71765. * Note that an escape in the identifier disables recognition of
  71766. * keywords; e.g. "\u0069f = 1;" is a valid statement (assigns to
  71767. * identifier named "if"). This is not necessarily compliant,
  71768. * see test-dec-escaped-char-in-keyword.js.
  71769. *
  71770. * Note: "get" and "set" are awkward. They are not officially
  71771. * ReservedWords (and indeed e.g. "var set = 1;" is valid), and
  71772. * must come out as DUK_TOK_IDENTIFIER. The compiler needs to
  71773. * work around this a bit.
  71774. */
  71775. /* XXX: optimize by adding the token numbers directly into the
  71776. * always interned duk_hstring objects (there should be enough
  71777. * flag bits free for that)?
  71778. */
  71779. i_end = (strict_mode ? DUK_STRIDX_END_RESERVED : DUK_STRIDX_START_STRICT_RESERVED);
  71780. advtok = DUK__ADVTOK(0, DUK_TOK_IDENTIFIER);
  71781. if (out_token->num_escapes == 0) {
  71782. for (i = DUK_STRIDX_START_RESERVED; i < i_end; i++) {
  71783. DUK_ASSERT_DISABLE(i >= 0); /* unsigned */
  71784. DUK_ASSERT(i < DUK_HEAP_NUM_STRINGS);
  71785. if (DUK_HTHREAD_GET_STRING(lex_ctx->thr, i) == str) {
  71786. advtok = DUK__ADVTOK(0, DUK_STRIDX_TO_TOK(i));
  71787. break;
  71788. }
  71789. }
  71790. }
  71791. } else if (DUK__ISDIGIT(x) || (x == DUK_ASC_PERIOD)) {
  71792. /* Note: decimal number may start with a period, but must be followed by a digit */
  71793. /*
  71794. * Pre-parsing for decimal, hex, octal (both legacy and ES2015),
  71795. * and binary literals, followed by an actual parser step
  71796. * provided by numconv.
  71797. *
  71798. * Note: the leading sign character ('+' or '-') is -not- part of
  71799. * the production in E5 grammar, and that the a DecimalLiteral
  71800. * starting with a '0' must be followed by a non-digit.
  71801. *
  71802. * XXX: the two step parsing process is quite awkward, it would
  71803. * be more straightforward to allow numconv to parse the longest
  71804. * valid prefix (it already does that, it only needs to indicate
  71805. * where the input ended). However, the lexer decodes characters
  71806. * using a limited lookup window, so this is not a trivial change.
  71807. */
  71808. /* XXX: because of the final check below (that the literal is not
  71809. * followed by a digit), this could maybe be simplified, if we bail
  71810. * out early from a leading zero (and if there are no periods etc).
  71811. * Maybe too complex.
  71812. */
  71813. duk_double_t val;
  71814. duk_bool_t legacy_oct = 0;
  71815. duk_small_int_t state; /* 0=before period/exp,
  71816. * 1=after period, before exp
  71817. * 2=after exp, allow '+' or '-'
  71818. * 3=after exp and exp sign
  71819. */
  71820. duk_small_uint_t s2n_flags;
  71821. duk_codepoint_t y, z;
  71822. duk_small_int_t s2n_radix = 10;
  71823. duk_small_uint_t pre_adv = 0;
  71824. DUK__INITBUFFER(lex_ctx);
  71825. y = DUK__L1();
  71826. if (x == DUK_ASC_0) {
  71827. z = DUK_LOWERCASE_CHAR_ASCII(y);
  71828. pre_adv = 2; /* default for 0xNNN, 0oNNN, 0bNNN. */
  71829. if (z == DUK_ASC_LC_X) {
  71830. s2n_radix = 16;
  71831. } else if (z == DUK_ASC_LC_O) {
  71832. s2n_radix = 8;
  71833. } else if (z == DUK_ASC_LC_B) {
  71834. s2n_radix = 2;
  71835. } else {
  71836. pre_adv = 0;
  71837. if (DUK__ISDIGIT(y)) {
  71838. if (strict_mode) {
  71839. /* Reject octal like \07 but also octal-lookalike
  71840. * decimal like \08 in strict mode.
  71841. */
  71842. goto fail_number_literal;
  71843. } else {
  71844. /* Legacy OctalIntegerLiteral or octal-lookalice
  71845. * decimal. Deciding between the two happens below
  71846. * in digit scanning.
  71847. */
  71848. DUK__APPENDBUFFER(lex_ctx, x);
  71849. pre_adv = 1;
  71850. legacy_oct = 1;
  71851. s2n_radix = 8; /* tentative unless conflicting digits found */
  71852. }
  71853. }
  71854. }
  71855. }
  71856. DUK__ADVANCECHARS(lex_ctx, pre_adv);
  71857. /* XXX: we could parse integers here directly, and fall back
  71858. * to numconv only when encountering a fractional expression
  71859. * or when an octal literal turned out to be decimal (0778 etc).
  71860. */
  71861. state = 0;
  71862. for (;;) {
  71863. x = DUK__L0(); /* re-lookup curr char on first round */
  71864. if (DUK__ISDIGIT(x)) {
  71865. /* Note: intentionally allow leading zeroes here, as the
  71866. * actual parser will check for them.
  71867. */
  71868. if (state == 0 && legacy_oct && (x == DUK_ASC_8 || x == DUK_ASC_9)) {
  71869. /* Started out as an octal-lookalike
  71870. * but interpreted as decimal, e.g.
  71871. * '0779' -> 779. This also means
  71872. * that fractions are allowed, e.g.
  71873. * '0779.123' is allowed but '0777.123'
  71874. * is not!
  71875. */
  71876. s2n_radix = 10;
  71877. }
  71878. if (state == 2) {
  71879. state = 3;
  71880. }
  71881. } else if (s2n_radix == 16 && DUK__ISHEXDIGIT(x)) {
  71882. /* Note: 'e' and 'E' are also accepted here. */
  71883. ;
  71884. } else if (x == DUK_ASC_PERIOD) {
  71885. if (state >= 1 || s2n_radix != 10) {
  71886. break;
  71887. } else {
  71888. state = 1;
  71889. }
  71890. } else if (x == DUK_ASC_LC_E || x == DUK_ASC_UC_E) {
  71891. if (state >= 2 || s2n_radix != 10) {
  71892. break;
  71893. } else {
  71894. state = 2;
  71895. }
  71896. } else if (x == DUK_ASC_MINUS || x == DUK_ASC_PLUS) {
  71897. if (state != 2) {
  71898. break;
  71899. } else {
  71900. state = 3;
  71901. }
  71902. } else {
  71903. break;
  71904. }
  71905. DUK__APPENDBUFFER(lex_ctx, x);
  71906. DUK__ADVANCECHARS(lex_ctx, 1);
  71907. }
  71908. /* XXX: better coercion */
  71909. (void) duk__internbuffer(lex_ctx, lex_ctx->slot1_idx);
  71910. if (s2n_radix != 10) {
  71911. /* For bases other than 10, integer only. */
  71912. s2n_flags = DUK_S2N_FLAG_ALLOW_LEADING_ZERO;
  71913. } else {
  71914. s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
  71915. DUK_S2N_FLAG_ALLOW_FRAC |
  71916. DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  71917. DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  71918. DUK_S2N_FLAG_ALLOW_LEADING_ZERO;
  71919. }
  71920. duk_dup(lex_ctx->thr, lex_ctx->slot1_idx);
  71921. duk_numconv_parse(lex_ctx->thr, s2n_radix, s2n_flags);
  71922. val = duk_to_number_m1(lex_ctx->thr);
  71923. if (DUK_ISNAN(val)) {
  71924. goto fail_number_literal;
  71925. }
  71926. duk_replace(lex_ctx->thr, lex_ctx->slot1_idx); /* could also just pop? */
  71927. DUK__INITBUFFER(lex_ctx); /* free some memory */
  71928. /* Section 7.8.3 (note): NumericLiteral must be followed by something other than
  71929. * IdentifierStart or DecimalDigit.
  71930. */
  71931. if (DUK__ISDIGIT(DUK__L0()) || duk_unicode_is_identifier_start(DUK__L0())) {
  71932. goto fail_number_literal;
  71933. }
  71934. out_token->num = val;
  71935. advtok = DUK__ADVTOK(0, DUK_TOK_NUMBER);
  71936. } else if (duk_unicode_is_whitespace(DUK__LOOKUP(lex_ctx, 0))) {
  71937. DUK__ADVANCECHARS(lex_ctx, 1);
  71938. goto restart;
  71939. } else if (x < 0) {
  71940. advtok = DUK__ADVTOK(0, DUK_TOK_EOF);
  71941. } else {
  71942. goto fail_token;
  71943. }
  71944. skip_slow_path:
  71945. /*
  71946. * Shared exit path
  71947. */
  71948. DUK__ADVANCEBYTES(lex_ctx, advtok >> 8);
  71949. out_token->t = advtok & 0xff;
  71950. if (out_token->t_nores == DUK_TOK_INVALID) {
  71951. out_token->t_nores = out_token->t;
  71952. }
  71953. out_token->lineterm = got_lineterm;
  71954. /* Automatic semicolon insertion is allowed if a token is preceded
  71955. * by line terminator(s), or terminates a statement list (right curly
  71956. * or EOF).
  71957. */
  71958. if (got_lineterm || out_token->t == DUK_TOK_RCURLY || out_token->t == DUK_TOK_EOF) {
  71959. out_token->allow_auto_semi = 1;
  71960. } else {
  71961. out_token->allow_auto_semi = 0;
  71962. }
  71963. return;
  71964. fail_token_limit:
  71965. DUK_ERROR_RANGE(lex_ctx->thr, DUK_STR_TOKEN_LIMIT);
  71966. DUK_WO_NORETURN(return;);
  71967. fail_token:
  71968. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_TOKEN);
  71969. DUK_WO_NORETURN(return;);
  71970. fail_number_literal:
  71971. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_NUMBER_LITERAL);
  71972. DUK_WO_NORETURN(return;);
  71973. fail_escape:
  71974. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_ESCAPE);
  71975. DUK_WO_NORETURN(return;);
  71976. fail_unterm_regexp:
  71977. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_UNTERMINATED_REGEXP);
  71978. DUK_WO_NORETURN(return;);
  71979. fail_unterm_comment:
  71980. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_UNTERMINATED_COMMENT);
  71981. DUK_WO_NORETURN(return;);
  71982. #if !defined(DUK_USE_REGEXP_SUPPORT)
  71983. fail_regexp_support:
  71984. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_REGEXP_SUPPORT_DISABLED);
  71985. DUK_WO_NORETURN(return;);
  71986. #endif
  71987. }
  71988. #if defined(DUK_USE_REGEXP_SUPPORT)
  71989. /*
  71990. * Parse a RegExp token. The grammar is described in E5 Section 15.10.
  71991. * Terminal constructions (such as quantifiers) are parsed directly here.
  71992. *
  71993. * 0xffffffffU is used as a marker for "infinity" in quantifiers. Further,
  71994. * DUK__MAX_RE_QUANT_DIGITS limits the maximum number of digits that
  71995. * will be accepted for a quantifier.
  71996. */
  71997. DUK_INTERNAL void duk_lexer_parse_re_token(duk_lexer_ctx *lex_ctx, duk_re_token *out_token) {
  71998. duk_small_uint_t advtok = 0; /* init is unnecessary but suppresses "may be used uninitialized" warnings */
  71999. duk_codepoint_t x, y;
  72000. if (++lex_ctx->token_count >= lex_ctx->token_limit) {
  72001. goto fail_token_limit;
  72002. }
  72003. duk_memzero(out_token, sizeof(*out_token));
  72004. x = DUK__L0();
  72005. y = DUK__L1();
  72006. DUK_DDD(DUK_DDDPRINT("parsing regexp token, L0=%ld, L1=%ld", (long) x, (long) y));
  72007. switch (x) {
  72008. case DUK_ASC_PIPE: {
  72009. advtok = DUK__ADVTOK(1, DUK_RETOK_DISJUNCTION);
  72010. break;
  72011. }
  72012. case DUK_ASC_CARET: {
  72013. advtok = DUK__ADVTOK(1, DUK_RETOK_ASSERT_START);
  72014. break;
  72015. }
  72016. case DUK_ASC_DOLLAR: {
  72017. advtok = DUK__ADVTOK(1, DUK_RETOK_ASSERT_END);
  72018. break;
  72019. }
  72020. case DUK_ASC_QUESTION: {
  72021. out_token->qmin = 0;
  72022. out_token->qmax = 1;
  72023. if (y == DUK_ASC_QUESTION) {
  72024. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  72025. out_token->greedy = 0;
  72026. } else {
  72027. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  72028. out_token->greedy = 1;
  72029. }
  72030. break;
  72031. }
  72032. case DUK_ASC_STAR: {
  72033. out_token->qmin = 0;
  72034. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  72035. if (y == DUK_ASC_QUESTION) {
  72036. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  72037. out_token->greedy = 0;
  72038. } else {
  72039. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  72040. out_token->greedy = 1;
  72041. }
  72042. break;
  72043. }
  72044. case DUK_ASC_PLUS: {
  72045. out_token->qmin = 1;
  72046. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  72047. if (y == DUK_ASC_QUESTION) {
  72048. advtok = DUK__ADVTOK(2, DUK_RETOK_QUANTIFIER);
  72049. out_token->greedy = 0;
  72050. } else {
  72051. advtok = DUK__ADVTOK(1, DUK_RETOK_QUANTIFIER);
  72052. out_token->greedy = 1;
  72053. }
  72054. break;
  72055. }
  72056. case DUK_ASC_LCURLY: {
  72057. /* Production allows 'DecimalDigits', including leading zeroes */
  72058. duk_uint32_t val1 = 0;
  72059. duk_uint32_t val2 = DUK_RE_QUANTIFIER_INFINITE;
  72060. duk_small_int_t digits = 0;
  72061. #if defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72062. duk_lexer_point lex_pt;
  72063. #endif
  72064. #if defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72065. /* Store lexer position, restoring if quantifier is invalid. */
  72066. DUK_LEXER_GETPOINT(lex_ctx, &lex_pt);
  72067. #endif
  72068. for (;;) {
  72069. DUK__ADVANCECHARS(lex_ctx, 1); /* eat '{' on entry */
  72070. x = DUK__L0();
  72071. if (DUK__ISDIGIT(x)) {
  72072. digits++;
  72073. val1 = val1 * 10 + (duk_uint32_t) duk__hexval(x);
  72074. } else if (x == DUK_ASC_COMMA) {
  72075. if (digits > DUK__MAX_RE_QUANT_DIGITS) {
  72076. goto invalid_quantifier;
  72077. }
  72078. if (val2 != DUK_RE_QUANTIFIER_INFINITE) {
  72079. goto invalid_quantifier;
  72080. }
  72081. if (DUK__L1() == DUK_ASC_RCURLY) {
  72082. /* form: { DecimalDigits , }, val1 = min count */
  72083. if (digits == 0) {
  72084. goto invalid_quantifier;
  72085. }
  72086. out_token->qmin = val1;
  72087. out_token->qmax = DUK_RE_QUANTIFIER_INFINITE;
  72088. DUK__ADVANCECHARS(lex_ctx, 2);
  72089. break;
  72090. }
  72091. val2 = val1;
  72092. val1 = 0;
  72093. digits = 0; /* not strictly necessary because of lookahead '}' above */
  72094. } else if (x == DUK_ASC_RCURLY) {
  72095. if (digits > DUK__MAX_RE_QUANT_DIGITS) {
  72096. goto invalid_quantifier;
  72097. }
  72098. if (digits == 0) {
  72099. goto invalid_quantifier;
  72100. }
  72101. if (val2 != DUK_RE_QUANTIFIER_INFINITE) {
  72102. /* val2 = min count, val1 = max count */
  72103. out_token->qmin = val2;
  72104. out_token->qmax = val1;
  72105. } else {
  72106. /* val1 = count */
  72107. out_token->qmin = val1;
  72108. out_token->qmax = val1;
  72109. }
  72110. DUK__ADVANCECHARS(lex_ctx, 1);
  72111. break;
  72112. } else {
  72113. goto invalid_quantifier;
  72114. }
  72115. }
  72116. if (DUK__L0() == DUK_ASC_QUESTION) {
  72117. out_token->greedy = 0;
  72118. DUK__ADVANCECHARS(lex_ctx, 1);
  72119. } else {
  72120. out_token->greedy = 1;
  72121. }
  72122. advtok = DUK__ADVTOK(0, DUK_RETOK_QUANTIFIER);
  72123. break;
  72124. invalid_quantifier:
  72125. #if defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72126. /* Failed to match the quantifier, restore lexer and parse
  72127. * opening brace as a literal.
  72128. */
  72129. DUK_LEXER_SETPOINT(lex_ctx, &lex_pt);
  72130. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_CHAR);
  72131. out_token->num = DUK_ASC_LCURLY;
  72132. #else
  72133. goto fail_quantifier;
  72134. #endif
  72135. break;
  72136. }
  72137. case DUK_ASC_PERIOD: {
  72138. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_PERIOD);
  72139. break;
  72140. }
  72141. case DUK_ASC_BACKSLASH: {
  72142. /* The E5.1 specification does not seem to allow IdentifierPart characters
  72143. * to be used as identity escapes. Unfortunately this includes '$', which
  72144. * cannot be escaped as '\$'; it needs to be escaped e.g. as '\u0024'.
  72145. * Many other implementations (including V8 and Rhino, for instance) do
  72146. * accept '\$' as a valid identity escape, which is quite pragmatic, and
  72147. * ES2015 Annex B relaxes the rules to allow these (and other) real world forms.
  72148. */
  72149. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_CHAR); /* default: char escape (two chars) */
  72150. if (y == DUK_ASC_LC_B) {
  72151. advtok = DUK__ADVTOK(2, DUK_RETOK_ASSERT_WORD_BOUNDARY);
  72152. } else if (y == DUK_ASC_UC_B) {
  72153. advtok = DUK__ADVTOK(2, DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY);
  72154. } else if (y == DUK_ASC_LC_F) {
  72155. out_token->num = 0x000c;
  72156. } else if (y == DUK_ASC_LC_N) {
  72157. out_token->num = 0x000a;
  72158. } else if (y == DUK_ASC_LC_T) {
  72159. out_token->num = 0x0009;
  72160. } else if (y == DUK_ASC_LC_R) {
  72161. out_token->num = 0x000d;
  72162. } else if (y == DUK_ASC_LC_V) {
  72163. out_token->num = 0x000b;
  72164. } else if (y == DUK_ASC_LC_C) {
  72165. x = DUK__L2();
  72166. if ((x >= DUK_ASC_LC_A && x <= DUK_ASC_LC_Z) ||
  72167. (x >= DUK_ASC_UC_A && x <= DUK_ASC_UC_Z)) {
  72168. out_token->num = (duk_uint32_t) (x % 32);
  72169. advtok = DUK__ADVTOK(3, DUK_RETOK_ATOM_CHAR);
  72170. } else {
  72171. goto fail_escape;
  72172. }
  72173. } else if (y == DUK_ASC_LC_X || y == DUK_ASC_LC_U) {
  72174. /* The token value is the Unicode codepoint without
  72175. * it being decode into surrogate pair characters
  72176. * here. The \u{H+} is only allowed in Unicode mode
  72177. * which we don't support yet.
  72178. */
  72179. out_token->num = (duk_uint32_t) duk__lexer_parse_escape(lex_ctx, 0 /*allow_es6*/);
  72180. advtok = DUK__ADVTOK(0, DUK_RETOK_ATOM_CHAR);
  72181. } else if (y == DUK_ASC_LC_D) {
  72182. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_DIGIT);
  72183. } else if (y == DUK_ASC_UC_D) {
  72184. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_DIGIT);
  72185. } else if (y == DUK_ASC_LC_S) {
  72186. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_WHITE);
  72187. } else if (y == DUK_ASC_UC_S) {
  72188. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_WHITE);
  72189. } else if (y == DUK_ASC_LC_W) {
  72190. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_WORD_CHAR);
  72191. } else if (y == DUK_ASC_UC_W) {
  72192. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_NOT_WORD_CHAR);
  72193. } else if (DUK__ISDIGIT(y)) {
  72194. /* E5 Section 15.10.2.11 */
  72195. if (y == DUK_ASC_0) {
  72196. if (DUK__ISDIGIT(DUK__L2())) {
  72197. goto fail_escape;
  72198. }
  72199. out_token->num = 0x0000;
  72200. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_CHAR);
  72201. } else {
  72202. /* XXX: shared parsing? */
  72203. duk_uint32_t val = 0;
  72204. duk_small_int_t i;
  72205. for (i = 0; ; i++) {
  72206. if (i >= DUK__MAX_RE_DECESC_DIGITS) {
  72207. goto fail_escape;
  72208. }
  72209. DUK__ADVANCECHARS(lex_ctx, 1); /* eat backslash on entry */
  72210. x = DUK__L0();
  72211. if (!DUK__ISDIGIT(x)) {
  72212. break;
  72213. }
  72214. val = val * 10 + (duk_uint32_t) duk__hexval(x);
  72215. }
  72216. /* DUK__L0() cannot be a digit, because the loop doesn't terminate if it is */
  72217. advtok = DUK__ADVTOK(0, DUK_RETOK_ATOM_BACKREFERENCE);
  72218. out_token->num = val;
  72219. }
  72220. #if defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72221. } else if (y >= 0) {
  72222. /* For ES2015 Annex B, accept any source character as identity
  72223. * escape except 'c' which is used for control characters.
  72224. * http://www.ecma-international.org/ecma-262/6.0/#sec-regular-expressions-patterns
  72225. * Careful not to match end-of-buffer (<0) here.
  72226. * This is not yet full ES2015 Annex B because cases above
  72227. * (like hex escape) won't backtrack.
  72228. */
  72229. DUK_ASSERT(y != DUK_ASC_LC_C); /* covered above */
  72230. #else /* DUK_USE_ES6_REGEXP_SYNTAX */
  72231. } else if ((y >= 0 && !duk_unicode_is_identifier_part(y)) ||
  72232. y == DUK_UNICODE_CP_ZWNJ ||
  72233. y == DUK_UNICODE_CP_ZWJ) {
  72234. /* For ES5.1 identity escapes are not allowed for identifier
  72235. * parts. This conflicts with a lot of real world code as this
  72236. * doesn't e.g. allow escaping a dollar sign as /\$/, see
  72237. * test-regexp-identity-escape-dollar.js.
  72238. */
  72239. #endif /* DUK_USE_ES6_REGEXP_SYNTAX */
  72240. out_token->num = (duk_uint32_t) y;
  72241. } else {
  72242. goto fail_escape;
  72243. }
  72244. break;
  72245. }
  72246. case DUK_ASC_LPAREN: {
  72247. /* XXX: naming is inconsistent: ATOM_END_GROUP ends an ASSERT_START_LOOKAHEAD */
  72248. if (y == DUK_ASC_QUESTION) {
  72249. if (DUK__L2() == DUK_ASC_EQUALS) {
  72250. /* (?= */
  72251. advtok = DUK__ADVTOK(3, DUK_RETOK_ASSERT_START_POS_LOOKAHEAD);
  72252. } else if (DUK__L2() == DUK_ASC_EXCLAMATION) {
  72253. /* (?! */
  72254. advtok = DUK__ADVTOK(3, DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD);
  72255. } else if (DUK__L2() == DUK_ASC_COLON) {
  72256. /* (?: */
  72257. advtok = DUK__ADVTOK(3, DUK_RETOK_ATOM_START_NONCAPTURE_GROUP);
  72258. } else {
  72259. goto fail_group;
  72260. }
  72261. } else {
  72262. /* ( */
  72263. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_START_CAPTURE_GROUP);
  72264. }
  72265. break;
  72266. }
  72267. case DUK_ASC_RPAREN: {
  72268. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_END_GROUP);
  72269. break;
  72270. }
  72271. case DUK_ASC_LBRACKET: {
  72272. /*
  72273. * To avoid creating a heavy intermediate value for the list of ranges,
  72274. * only the start token ('[' or '[^') is parsed here. The regexp
  72275. * compiler parses the ranges itself.
  72276. */
  72277. /* XXX: with DUK_USE_ES6_REGEXP_SYNTAX we should allow left bracket
  72278. * literal too, but it's not easy to parse without backtracking.
  72279. */
  72280. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_START_CHARCLASS);
  72281. if (y == DUK_ASC_CARET) {
  72282. advtok = DUK__ADVTOK(2, DUK_RETOK_ATOM_START_CHARCLASS_INVERTED);
  72283. }
  72284. break;
  72285. }
  72286. #if !defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72287. case DUK_ASC_RCURLY:
  72288. case DUK_ASC_RBRACKET: {
  72289. /* Although these could be parsed as PatternCharacters unambiguously (here),
  72290. * E5 Section 15.10.1 grammar explicitly forbids these as PatternCharacters.
  72291. */
  72292. goto fail_invalid_char;
  72293. break;
  72294. }
  72295. #endif
  72296. case -1: {
  72297. /* EOF */
  72298. advtok = DUK__ADVTOK(0, DUK_TOK_EOF);
  72299. break;
  72300. }
  72301. default: {
  72302. /* PatternCharacter, all excluded characters are matched by cases above */
  72303. advtok = DUK__ADVTOK(1, DUK_RETOK_ATOM_CHAR);
  72304. out_token->num = (duk_uint32_t) x;
  72305. break;
  72306. }
  72307. }
  72308. /*
  72309. * Shared exit path
  72310. */
  72311. DUK__ADVANCEBYTES(lex_ctx, advtok >> 8);
  72312. out_token->t = advtok & 0xff;
  72313. return;
  72314. fail_token_limit:
  72315. DUK_ERROR_RANGE(lex_ctx->thr, DUK_STR_TOKEN_LIMIT);
  72316. DUK_WO_NORETURN(return;);
  72317. fail_escape:
  72318. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_REGEXP_ESCAPE);
  72319. DUK_WO_NORETURN(return;);
  72320. fail_group:
  72321. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_REGEXP_GROUP);
  72322. DUK_WO_NORETURN(return;);
  72323. #if !defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72324. fail_invalid_char:
  72325. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_REGEXP_CHARACTER);
  72326. DUK_WO_NORETURN(return;);
  72327. fail_quantifier:
  72328. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_QUANTIFIER);
  72329. DUK_WO_NORETURN(return;);
  72330. #endif
  72331. }
  72332. /*
  72333. * Special parser for character classes; calls callback for every
  72334. * range parsed and returns the number of ranges present.
  72335. */
  72336. /* XXX: this duplicates functionality in duk_regexp.c where a similar loop is
  72337. * required anyway. We could use that BUT we need to update the regexp compiler
  72338. * 'nranges' too. Work this out a bit more cleanly to save space.
  72339. */
  72340. /* XXX: the handling of character range detection is a bit convoluted.
  72341. * Try to simplify and make smaller.
  72342. */
  72343. /* XXX: logic for handling character ranges is now incorrect, it will accept
  72344. * e.g. [\d-z] whereas it should croak from it? SMJS accepts this too, though.
  72345. *
  72346. * Needs a read through and a lot of additional tests.
  72347. */
  72348. DUK_LOCAL
  72349. void duk__emit_u16_direct_ranges(duk_lexer_ctx *lex_ctx,
  72350. duk_re_range_callback gen_range,
  72351. void *userdata,
  72352. const duk_uint16_t *ranges,
  72353. duk_small_int_t num) {
  72354. const duk_uint16_t *ranges_end;
  72355. DUK_UNREF(lex_ctx);
  72356. ranges_end = ranges + num;
  72357. while (ranges < ranges_end) {
  72358. /* mark range 'direct', bypass canonicalization (see Wiki) */
  72359. gen_range(userdata, (duk_codepoint_t) ranges[0], (duk_codepoint_t) ranges[1], 1);
  72360. ranges += 2;
  72361. }
  72362. }
  72363. DUK_INTERNAL void duk_lexer_parse_re_ranges(duk_lexer_ctx *lex_ctx, duk_re_range_callback gen_range, void *userdata) {
  72364. duk_codepoint_t start = -1;
  72365. duk_codepoint_t ch;
  72366. duk_codepoint_t x;
  72367. duk_bool_t dash = 0;
  72368. duk_small_uint_t adv = 0;
  72369. DUK_DD(DUK_DDPRINT("parsing regexp ranges"));
  72370. for (;;) {
  72371. DUK__ADVANCECHARS(lex_ctx, adv);
  72372. adv = 1;
  72373. x = DUK__L0();
  72374. ch = -1; /* not strictly necessary, but avoids "uninitialized variable" warnings */
  72375. DUK_UNREF(ch);
  72376. if (x < 0) {
  72377. goto fail_unterm_charclass;
  72378. } else if (x == DUK_ASC_RBRACKET) {
  72379. if (start >= 0) {
  72380. gen_range(userdata, start, start, 0);
  72381. }
  72382. DUK__ADVANCECHARS(lex_ctx, 1); /* eat ']' before finishing */
  72383. break;
  72384. } else if (x == DUK_ASC_MINUS) {
  72385. if (start >= 0 && !dash && DUK__L1() != DUK_ASC_RBRACKET) {
  72386. /* '-' as a range indicator */
  72387. dash = 1;
  72388. continue;
  72389. } else {
  72390. /* '-' verbatim */
  72391. ch = x;
  72392. }
  72393. } else if (x == DUK_ASC_BACKSLASH) {
  72394. /*
  72395. * The escapes are same as outside a character class, except that \b has a
  72396. * different meaning, and \B and backreferences are prohibited (see E5
  72397. * Section 15.10.2.19). However, it's difficult to share code because we
  72398. * handle e.g. "\n" very differently: here we generate a single character
  72399. * range for it.
  72400. */
  72401. /* XXX: ES2015 surrogate pair handling. */
  72402. x = DUK__L1();
  72403. adv = 2;
  72404. if (x == DUK_ASC_LC_B) {
  72405. /* Note: '\b' in char class is different than outside (assertion),
  72406. * '\B' is not allowed and is caught by the duk_unicode_is_identifier_part()
  72407. * check below.
  72408. */
  72409. ch = 0x0008;
  72410. } else if (x == DUK_ASC_LC_F) {
  72411. ch = 0x000c;
  72412. } else if (x == DUK_ASC_LC_N) {
  72413. ch = 0x000a;
  72414. } else if (x == DUK_ASC_LC_T) {
  72415. ch = 0x0009;
  72416. } else if (x == DUK_ASC_LC_R) {
  72417. ch = 0x000d;
  72418. } else if (x == DUK_ASC_LC_V) {
  72419. ch = 0x000b;
  72420. } else if (x == DUK_ASC_LC_C) {
  72421. x = DUK__L2();
  72422. adv = 3;
  72423. if ((x >= DUK_ASC_LC_A && x <= DUK_ASC_LC_Z) ||
  72424. (x >= DUK_ASC_UC_A && x <= DUK_ASC_UC_Z)) {
  72425. ch = (x % 32);
  72426. } else {
  72427. goto fail_escape;
  72428. }
  72429. } else if (x == DUK_ASC_LC_X || x == DUK_ASC_LC_U) {
  72430. /* The \u{H+} form is only allowed in Unicode mode which
  72431. * we don't support yet.
  72432. */
  72433. ch = duk__lexer_parse_escape(lex_ctx, 0 /*allow_es6*/);
  72434. adv = 0;
  72435. } else if (x == DUK_ASC_LC_D) {
  72436. duk__emit_u16_direct_ranges(lex_ctx,
  72437. gen_range,
  72438. userdata,
  72439. duk_unicode_re_ranges_digit,
  72440. sizeof(duk_unicode_re_ranges_digit) / sizeof(duk_uint16_t));
  72441. ch = -1;
  72442. } else if (x == DUK_ASC_UC_D) {
  72443. duk__emit_u16_direct_ranges(lex_ctx,
  72444. gen_range,
  72445. userdata,
  72446. duk_unicode_re_ranges_not_digit,
  72447. sizeof(duk_unicode_re_ranges_not_digit) / sizeof(duk_uint16_t));
  72448. ch = -1;
  72449. } else if (x == DUK_ASC_LC_S) {
  72450. duk__emit_u16_direct_ranges(lex_ctx,
  72451. gen_range,
  72452. userdata,
  72453. duk_unicode_re_ranges_white,
  72454. sizeof(duk_unicode_re_ranges_white) / sizeof(duk_uint16_t));
  72455. ch = -1;
  72456. } else if (x == DUK_ASC_UC_S) {
  72457. duk__emit_u16_direct_ranges(lex_ctx,
  72458. gen_range,
  72459. userdata,
  72460. duk_unicode_re_ranges_not_white,
  72461. sizeof(duk_unicode_re_ranges_not_white) / sizeof(duk_uint16_t));
  72462. ch = -1;
  72463. } else if (x == DUK_ASC_LC_W) {
  72464. duk__emit_u16_direct_ranges(lex_ctx,
  72465. gen_range,
  72466. userdata,
  72467. duk_unicode_re_ranges_wordchar,
  72468. sizeof(duk_unicode_re_ranges_wordchar) / sizeof(duk_uint16_t));
  72469. ch = -1;
  72470. } else if (x == DUK_ASC_UC_W) {
  72471. duk__emit_u16_direct_ranges(lex_ctx,
  72472. gen_range,
  72473. userdata,
  72474. duk_unicode_re_ranges_not_wordchar,
  72475. sizeof(duk_unicode_re_ranges_not_wordchar) / sizeof(duk_uint16_t));
  72476. ch = -1;
  72477. } else if (DUK__ISDIGIT(x)) {
  72478. /* DecimalEscape, only \0 is allowed, no leading
  72479. * zeroes are allowed.
  72480. *
  72481. * ES2015 Annex B also allows (maximal match) legacy
  72482. * octal escapes up to \377 and \8 and \9 are
  72483. * accepted as literal '8' and '9', also in strict mode.
  72484. */
  72485. #if defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72486. ch = duk__lexer_parse_legacy_octal(lex_ctx, &adv, 0 /*reject_annex_b*/);
  72487. DUK_ASSERT(ch >= 0); /* no rejections */
  72488. #else
  72489. if (x == DUK_ASC_0 && !DUK__ISDIGIT(DUK__L2())) {
  72490. ch = 0x0000;
  72491. } else {
  72492. goto fail_escape;
  72493. }
  72494. #endif
  72495. #if defined(DUK_USE_ES6_REGEXP_SYNTAX)
  72496. } else if (x >= 0) {
  72497. /* IdentityEscape: ES2015 Annex B allows almost all
  72498. * source characters here. Match anything except
  72499. * EOF here.
  72500. */
  72501. ch = x;
  72502. #else /* DUK_USE_ES6_REGEXP_SYNTAX */
  72503. } else if (!duk_unicode_is_identifier_part(x)) {
  72504. /* IdentityEscape: ES5.1 doesn't allow identity escape
  72505. * for identifier part characters, which conflicts with
  72506. * some real world code. For example, it doesn't allow
  72507. * /[\$]/ which is awkward.
  72508. */
  72509. ch = x;
  72510. #endif /* DUK_USE_ES6_REGEXP_SYNTAX */
  72511. } else {
  72512. goto fail_escape;
  72513. }
  72514. } else {
  72515. /* character represents itself */
  72516. ch = x;
  72517. }
  72518. /* ch is a literal character here or -1 if parsed entity was
  72519. * an escape such as "\s".
  72520. */
  72521. if (ch < 0) {
  72522. /* multi-character sets not allowed as part of ranges, see
  72523. * E5 Section 15.10.2.15, abstract operation CharacterRange.
  72524. */
  72525. if (start >= 0) {
  72526. if (dash) {
  72527. goto fail_range;
  72528. } else {
  72529. gen_range(userdata, start, start, 0);
  72530. start = -1;
  72531. /* dash is already 0 */
  72532. }
  72533. }
  72534. } else {
  72535. if (start >= 0) {
  72536. if (dash) {
  72537. if (start > ch) {
  72538. goto fail_range;
  72539. }
  72540. gen_range(userdata, start, ch, 0);
  72541. start = -1;
  72542. dash = 0;
  72543. } else {
  72544. gen_range(userdata, start, start, 0);
  72545. start = ch;
  72546. /* dash is already 0 */
  72547. }
  72548. } else {
  72549. start = ch;
  72550. }
  72551. }
  72552. }
  72553. return;
  72554. fail_escape:
  72555. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_REGEXP_ESCAPE);
  72556. DUK_WO_NORETURN(return;);
  72557. fail_range:
  72558. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_INVALID_RANGE);
  72559. DUK_WO_NORETURN(return;);
  72560. fail_unterm_charclass:
  72561. DUK_ERROR_SYNTAX(lex_ctx->thr, DUK_STR_UNTERMINATED_CHARCLASS);
  72562. DUK_WO_NORETURN(return;);
  72563. }
  72564. #endif /* DUK_USE_REGEXP_SUPPORT */
  72565. /* automatic undefs */
  72566. #undef DUK__ADVANCEBYTES
  72567. #undef DUK__ADVANCECHARS
  72568. #undef DUK__ADVTOK
  72569. #undef DUK__APPENDBUFFER
  72570. #undef DUK__APPENDBUFFER_ASCII
  72571. #undef DUK__INITBUFFER
  72572. #undef DUK__ISDIGIT
  72573. #undef DUK__ISDIGIT03
  72574. #undef DUK__ISDIGIT47
  72575. #undef DUK__ISHEXDIGIT
  72576. #undef DUK__ISOCTDIGIT
  72577. #undef DUK__L0
  72578. #undef DUK__L1
  72579. #undef DUK__L2
  72580. #undef DUK__L3
  72581. #undef DUK__L4
  72582. #undef DUK__L5
  72583. #undef DUK__LOOKUP
  72584. #undef DUK__MAX_RE_DECESC_DIGITS
  72585. #undef DUK__MAX_RE_QUANT_DIGITS
  72586. #line 1 "duk_numconv.c"
  72587. /*
  72588. * Number-to-string and string-to-number conversions.
  72589. *
  72590. * Slow path number-to-string and string-to-number conversion is based on
  72591. * a Dragon4 variant, with fast paths for small integers. Big integer
  72592. * arithmetic is needed for guaranteeing that the conversion is correct
  72593. * and uses a minimum number of digits. The big number arithmetic has a
  72594. * fixed maximum size and does not require dynamic allocations.
  72595. *
  72596. * See: doc/number-conversion.rst.
  72597. */
  72598. /* #include duk_internal.h -> already included */
  72599. #define DUK__IEEE_DOUBLE_EXP_BIAS 1023
  72600. #define DUK__IEEE_DOUBLE_EXP_MIN (-1022) /* biased exp == 0 -> denormal, exp -1022 */
  72601. #define DUK__DIGITCHAR(x) duk_lc_digits[(x)]
  72602. /*
  72603. * Tables generated with util/gennumdigits.py.
  72604. *
  72605. * duk__str2num_digits_for_radix indicates, for each radix, how many input
  72606. * digits should be considered significant for string-to-number conversion.
  72607. * The input is also padded to this many digits to give the Dragon4
  72608. * conversion enough (apparent) precision to work with.
  72609. *
  72610. * duk__str2num_exp_limits indicates, for each radix, the radix-specific
  72611. * minimum/maximum exponent values (for a Dragon4 integer mantissa)
  72612. * below and above which the number is guaranteed to underflow to zero
  72613. * or overflow to Infinity. This allows parsing to keep bigint values
  72614. * bounded.
  72615. */
  72616. DUK_LOCAL const duk_uint8_t duk__str2num_digits_for_radix[] = {
  72617. 69, 44, 35, 30, 27, 25, 23, 22, 20, 20, /* 2 to 11 */
  72618. 20, 19, 19, 18, 18, 17, 17, 17, 16, 16, /* 12 to 21 */
  72619. 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, /* 22 to 31 */
  72620. 14, 14, 14, 14, 14 /* 31 to 36 */
  72621. };
  72622. typedef struct {
  72623. duk_int16_t upper;
  72624. duk_int16_t lower;
  72625. } duk__exp_limits;
  72626. DUK_LOCAL const duk__exp_limits duk__str2num_exp_limits[] = {
  72627. { 957, -1147 }, { 605, -725 }, { 479, -575 }, { 414, -496 },
  72628. { 372, -446 }, { 342, -411 }, { 321, -384 }, { 304, -364 },
  72629. { 291, -346 }, { 279, -334 }, { 268, -323 }, { 260, -312 },
  72630. { 252, -304 }, { 247, -296 }, { 240, -289 }, { 236, -283 },
  72631. { 231, -278 }, { 227, -273 }, { 223, -267 }, { 220, -263 },
  72632. { 216, -260 }, { 213, -256 }, { 210, -253 }, { 208, -249 },
  72633. { 205, -246 }, { 203, -244 }, { 201, -241 }, { 198, -239 },
  72634. { 196, -237 }, { 195, -234 }, { 193, -232 }, { 191, -230 },
  72635. { 190, -228 }, { 188, -226 }, { 187, -225 },
  72636. };
  72637. /*
  72638. * Limited functionality bigint implementation.
  72639. *
  72640. * Restricted to non-negative numbers with less than 32 * DUK__BI_MAX_PARTS bits,
  72641. * with the caller responsible for ensuring this is never exceeded. No memory
  72642. * allocation (except stack) is needed for bigint computation. Operations
  72643. * have been tailored for number conversion needs.
  72644. *
  72645. * Argument order is "assignment order", i.e. target first, then arguments:
  72646. * x <- y * z --> duk__bi_mul(x, y, z);
  72647. */
  72648. /* This upper value has been experimentally determined; debug build will check
  72649. * bigint size with assertions.
  72650. */
  72651. #define DUK__BI_MAX_PARTS 37 /* 37x32 = 1184 bits */
  72652. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  72653. #define DUK__BI_PRINT(name,x) duk__bi_print((name),(x))
  72654. #else
  72655. #define DUK__BI_PRINT(name,x)
  72656. #endif
  72657. /* Current size is about 152 bytes. */
  72658. typedef struct {
  72659. duk_small_int_t n;
  72660. duk_uint32_t v[DUK__BI_MAX_PARTS]; /* low to high */
  72661. } duk__bigint;
  72662. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  72663. DUK_LOCAL void duk__bi_print(const char *name, duk__bigint *x) {
  72664. /* Overestimate required size; debug code so not critical to be tight. */
  72665. char buf[DUK__BI_MAX_PARTS * 9 + 64];
  72666. char *p = buf;
  72667. duk_small_int_t i;
  72668. /* No NUL term checks in this debug code. */
  72669. p += DUK_SPRINTF(p, "%p n=%ld", (void *) x, (long) x->n);
  72670. if (x->n == 0) {
  72671. p += DUK_SPRINTF(p, " 0");
  72672. }
  72673. for (i = x->n - 1; i >= 0; i--) {
  72674. p += DUK_SPRINTF(p, " %08lx", (unsigned long) x->v[i]);
  72675. }
  72676. DUK_DDD(DUK_DDDPRINT("%s: %s", (const char *) name, (const char *) buf));
  72677. }
  72678. #endif
  72679. #if defined(DUK_USE_ASSERTIONS)
  72680. DUK_LOCAL duk_small_int_t duk__bi_is_valid(duk__bigint *x) {
  72681. return (duk_small_int_t)
  72682. ( ((x->n >= 0) && (x->n <= DUK__BI_MAX_PARTS)) /* is valid size */ &&
  72683. ((x->n == 0) || (x->v[x->n - 1] != 0)) /* is normalized */ );
  72684. }
  72685. #endif
  72686. DUK_LOCAL void duk__bi_normalize(duk__bigint *x) {
  72687. duk_small_int_t i;
  72688. for (i = x->n - 1; i >= 0; i--) {
  72689. if (x->v[i] != 0) {
  72690. break;
  72691. }
  72692. }
  72693. /* Note: if 'x' is zero, x->n becomes 0 here */
  72694. x->n = i + 1;
  72695. DUK_ASSERT(duk__bi_is_valid(x));
  72696. }
  72697. /* x <- y */
  72698. DUK_LOCAL void duk__bi_copy(duk__bigint *x, duk__bigint *y) {
  72699. duk_small_int_t n;
  72700. n = y->n;
  72701. x->n = n;
  72702. /* No need to special case n == 0. */
  72703. duk_memcpy((void *) x->v, (const void *) y->v, (size_t) (sizeof(duk_uint32_t) * (size_t) n));
  72704. }
  72705. DUK_LOCAL void duk__bi_set_small(duk__bigint *x, duk_uint32_t v) {
  72706. if (v == 0U) {
  72707. x->n = 0;
  72708. } else {
  72709. x->n = 1;
  72710. x->v[0] = v;
  72711. }
  72712. DUK_ASSERT(duk__bi_is_valid(x));
  72713. }
  72714. /* Return value: <0 <=> x < y
  72715. * 0 <=> x == y
  72716. * >0 <=> x > y
  72717. */
  72718. DUK_LOCAL int duk__bi_compare(duk__bigint *x, duk__bigint *y) {
  72719. duk_small_int_t i, nx, ny;
  72720. duk_uint32_t tx, ty;
  72721. DUK_ASSERT(duk__bi_is_valid(x));
  72722. DUK_ASSERT(duk__bi_is_valid(y));
  72723. nx = x->n;
  72724. ny = y->n;
  72725. if (nx > ny) {
  72726. goto ret_gt;
  72727. }
  72728. if (nx < ny) {
  72729. goto ret_lt;
  72730. }
  72731. for (i = nx - 1; i >= 0; i--) {
  72732. tx = x->v[i];
  72733. ty = y->v[i];
  72734. if (tx > ty) {
  72735. goto ret_gt;
  72736. }
  72737. if (tx < ty) {
  72738. goto ret_lt;
  72739. }
  72740. }
  72741. return 0;
  72742. ret_gt:
  72743. return 1;
  72744. ret_lt:
  72745. return -1;
  72746. }
  72747. /* x <- y + z */
  72748. #if defined(DUK_USE_64BIT_OPS)
  72749. DUK_LOCAL void duk__bi_add(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  72750. duk_uint64_t tmp;
  72751. duk_small_int_t i, ny, nz;
  72752. DUK_ASSERT(duk__bi_is_valid(y));
  72753. DUK_ASSERT(duk__bi_is_valid(z));
  72754. if (z->n > y->n) {
  72755. duk__bigint *t;
  72756. t = y; y = z; z = t;
  72757. }
  72758. DUK_ASSERT(y->n >= z->n);
  72759. ny = y->n; nz = z->n;
  72760. tmp = 0U;
  72761. for (i = 0; i < ny; i++) {
  72762. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  72763. tmp += y->v[i];
  72764. if (i < nz) {
  72765. tmp += z->v[i];
  72766. }
  72767. x->v[i] = (duk_uint32_t) (tmp & 0xffffffffUL);
  72768. tmp = tmp >> 32;
  72769. }
  72770. if (tmp != 0U) {
  72771. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  72772. x->v[i++] = (duk_uint32_t) tmp;
  72773. }
  72774. x->n = i;
  72775. DUK_ASSERT(x->n <= DUK__BI_MAX_PARTS);
  72776. /* no need to normalize */
  72777. DUK_ASSERT(duk__bi_is_valid(x));
  72778. }
  72779. #else /* DUK_USE_64BIT_OPS */
  72780. DUK_LOCAL void duk__bi_add(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  72781. duk_uint32_t carry, tmp1, tmp2;
  72782. duk_small_int_t i, ny, nz;
  72783. DUK_ASSERT(duk__bi_is_valid(y));
  72784. DUK_ASSERT(duk__bi_is_valid(z));
  72785. if (z->n > y->n) {
  72786. duk__bigint *t;
  72787. t = y; y = z; z = t;
  72788. }
  72789. DUK_ASSERT(y->n >= z->n);
  72790. ny = y->n; nz = z->n;
  72791. carry = 0U;
  72792. for (i = 0; i < ny; i++) {
  72793. /* Carry is detected based on wrapping which relies on exact 32-bit
  72794. * types.
  72795. */
  72796. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  72797. tmp1 = y->v[i];
  72798. tmp2 = tmp1;
  72799. if (i < nz) {
  72800. tmp2 += z->v[i];
  72801. }
  72802. /* Careful with carry condition:
  72803. * - If carry not added: 0x12345678 + 0 + 0xffffffff = 0x12345677 (< 0x12345678)
  72804. * - If carry added: 0x12345678 + 1 + 0xffffffff = 0x12345678 (== 0x12345678)
  72805. */
  72806. if (carry) {
  72807. tmp2++;
  72808. carry = (tmp2 <= tmp1 ? 1U : 0U);
  72809. } else {
  72810. carry = (tmp2 < tmp1 ? 1U : 0U);
  72811. }
  72812. x->v[i] = tmp2;
  72813. }
  72814. if (carry) {
  72815. DUK_ASSERT(i < DUK__BI_MAX_PARTS);
  72816. DUK_ASSERT(carry == 1U);
  72817. x->v[i++] = carry;
  72818. }
  72819. x->n = i;
  72820. DUK_ASSERT(x->n <= DUK__BI_MAX_PARTS);
  72821. /* no need to normalize */
  72822. DUK_ASSERT(duk__bi_is_valid(x));
  72823. }
  72824. #endif /* DUK_USE_64BIT_OPS */
  72825. /* x <- y + z */
  72826. DUK_LOCAL void duk__bi_add_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  72827. duk__bigint tmp;
  72828. DUK_ASSERT(duk__bi_is_valid(y));
  72829. /* XXX: this could be optimized; there is only one call site now though */
  72830. duk__bi_set_small(&tmp, z);
  72831. duk__bi_add(x, y, &tmp);
  72832. DUK_ASSERT(duk__bi_is_valid(x));
  72833. }
  72834. #if 0 /* unused */
  72835. /* x <- x + y, use t as temp */
  72836. DUK_LOCAL void duk__bi_add_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  72837. duk__bi_add(t, x, y);
  72838. duk__bi_copy(x, t);
  72839. }
  72840. #endif
  72841. /* x <- y - z, require x >= y => z >= 0, i.e. y >= z */
  72842. #if defined(DUK_USE_64BIT_OPS)
  72843. DUK_LOCAL void duk__bi_sub(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  72844. duk_small_int_t i, ny, nz;
  72845. duk_uint32_t ty, tz;
  72846. duk_int64_t tmp;
  72847. DUK_ASSERT(duk__bi_is_valid(y));
  72848. DUK_ASSERT(duk__bi_is_valid(z));
  72849. DUK_ASSERT(duk__bi_compare(y, z) >= 0);
  72850. DUK_ASSERT(y->n >= z->n);
  72851. ny = y->n; nz = z->n;
  72852. tmp = 0;
  72853. for (i = 0; i < ny; i++) {
  72854. ty = y->v[i];
  72855. if (i < nz) {
  72856. tz = z->v[i];
  72857. } else {
  72858. tz = 0;
  72859. }
  72860. tmp = (duk_int64_t) ty - (duk_int64_t) tz + tmp;
  72861. x->v[i] = (duk_uint32_t) ((duk_uint64_t) tmp & 0xffffffffUL);
  72862. tmp = tmp >> 32; /* 0 or -1 */
  72863. }
  72864. DUK_ASSERT(tmp == 0);
  72865. x->n = i;
  72866. duk__bi_normalize(x); /* need to normalize, may even cancel to 0 */
  72867. DUK_ASSERT(duk__bi_is_valid(x));
  72868. }
  72869. #else
  72870. DUK_LOCAL void duk__bi_sub(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  72871. duk_small_int_t i, ny, nz;
  72872. duk_uint32_t tmp1, tmp2, borrow;
  72873. DUK_ASSERT(duk__bi_is_valid(y));
  72874. DUK_ASSERT(duk__bi_is_valid(z));
  72875. DUK_ASSERT(duk__bi_compare(y, z) >= 0);
  72876. DUK_ASSERT(y->n >= z->n);
  72877. ny = y->n; nz = z->n;
  72878. borrow = 0U;
  72879. for (i = 0; i < ny; i++) {
  72880. /* Borrow is detected based on wrapping which relies on exact 32-bit
  72881. * types.
  72882. */
  72883. tmp1 = y->v[i];
  72884. tmp2 = tmp1;
  72885. if (i < nz) {
  72886. tmp2 -= z->v[i];
  72887. }
  72888. /* Careful with borrow condition:
  72889. * - If borrow not subtracted: 0x12345678 - 0 - 0xffffffff = 0x12345679 (> 0x12345678)
  72890. * - If borrow subtracted: 0x12345678 - 1 - 0xffffffff = 0x12345678 (== 0x12345678)
  72891. */
  72892. if (borrow) {
  72893. tmp2--;
  72894. borrow = (tmp2 >= tmp1 ? 1U : 0U);
  72895. } else {
  72896. borrow = (tmp2 > tmp1 ? 1U : 0U);
  72897. }
  72898. x->v[i] = tmp2;
  72899. }
  72900. DUK_ASSERT(borrow == 0U);
  72901. x->n = i;
  72902. duk__bi_normalize(x); /* need to normalize, may even cancel to 0 */
  72903. DUK_ASSERT(duk__bi_is_valid(x));
  72904. }
  72905. #endif
  72906. #if 0 /* unused */
  72907. /* x <- y - z */
  72908. DUK_LOCAL void duk__bi_sub_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  72909. duk__bigint tmp;
  72910. DUK_ASSERT(duk__bi_is_valid(y));
  72911. /* XXX: this could be optimized */
  72912. duk__bi_set_small(&tmp, z);
  72913. duk__bi_sub(x, y, &tmp);
  72914. DUK_ASSERT(duk__bi_is_valid(x));
  72915. }
  72916. #endif
  72917. /* x <- x - y, use t as temp */
  72918. DUK_LOCAL void duk__bi_sub_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  72919. duk__bi_sub(t, x, y);
  72920. duk__bi_copy(x, t);
  72921. }
  72922. /* x <- y * z */
  72923. DUK_LOCAL void duk__bi_mul(duk__bigint *x, duk__bigint *y, duk__bigint *z) {
  72924. duk_small_int_t i, j, nx, nz;
  72925. DUK_ASSERT(duk__bi_is_valid(y));
  72926. DUK_ASSERT(duk__bi_is_valid(z));
  72927. nx = y->n + z->n; /* max possible */
  72928. DUK_ASSERT(nx <= DUK__BI_MAX_PARTS);
  72929. if (nx == 0) {
  72930. /* Both inputs are zero; cases where only one is zero can go
  72931. * through main algorithm.
  72932. */
  72933. x->n = 0;
  72934. return;
  72935. }
  72936. duk_memzero((void *) x->v, (size_t) (sizeof(duk_uint32_t) * (size_t) nx));
  72937. x->n = nx;
  72938. nz = z->n;
  72939. for (i = 0; i < y->n; i++) {
  72940. #if defined(DUK_USE_64BIT_OPS)
  72941. duk_uint64_t tmp = 0U;
  72942. for (j = 0; j < nz; j++) {
  72943. tmp += (duk_uint64_t) y->v[i] * (duk_uint64_t) z->v[j] + x->v[i+j];
  72944. x->v[i+j] = (duk_uint32_t) (tmp & 0xffffffffUL);
  72945. tmp = tmp >> 32;
  72946. }
  72947. if (tmp > 0) {
  72948. DUK_ASSERT(i + j < nx);
  72949. DUK_ASSERT(i + j < DUK__BI_MAX_PARTS);
  72950. DUK_ASSERT(x->v[i+j] == 0U);
  72951. x->v[i+j] = (duk_uint32_t) tmp;
  72952. }
  72953. #else
  72954. /*
  72955. * Multiply + add + carry for 32-bit components using only 16x16->32
  72956. * multiplies and carry detection based on unsigned overflow.
  72957. *
  72958. * 1st mult, 32-bit: (A*2^16 + B)
  72959. * 2nd mult, 32-bit: (C*2^16 + D)
  72960. * 3rd add, 32-bit: E
  72961. * 4th add, 32-bit: F
  72962. *
  72963. * (AC*2^16 + B) * (C*2^16 + D) + E + F
  72964. * = AC*2^32 + AD*2^16 + BC*2^16 + BD + E + F
  72965. * = AC*2^32 + (AD + BC)*2^16 + (BD + E + F)
  72966. * = AC*2^32 + AD*2^16 + BC*2^16 + (BD + E + F)
  72967. */
  72968. duk_uint32_t a, b, c, d, e, f;
  72969. duk_uint32_t r, s, t;
  72970. a = y->v[i]; b = a & 0xffffUL; a = a >> 16;
  72971. f = 0;
  72972. for (j = 0; j < nz; j++) {
  72973. c = z->v[j]; d = c & 0xffffUL; c = c >> 16;
  72974. e = x->v[i+j];
  72975. /* build result as: (r << 32) + s: start with (BD + E + F) */
  72976. r = 0;
  72977. s = b * d;
  72978. /* add E */
  72979. t = s + e;
  72980. if (t < s) { r++; } /* carry */
  72981. s = t;
  72982. /* add F */
  72983. t = s + f;
  72984. if (t < s) { r++; } /* carry */
  72985. s = t;
  72986. /* add BC*2^16 */
  72987. t = b * c;
  72988. r += (t >> 16);
  72989. t = s + ((t & 0xffffUL) << 16);
  72990. if (t < s) { r++; } /* carry */
  72991. s = t;
  72992. /* add AD*2^16 */
  72993. t = a * d;
  72994. r += (t >> 16);
  72995. t = s + ((t & 0xffffUL) << 16);
  72996. if (t < s) { r++; } /* carry */
  72997. s = t;
  72998. /* add AC*2^32 */
  72999. t = a * c;
  73000. r += t;
  73001. DUK_DDD(DUK_DDDPRINT("ab=%08lx cd=%08lx ef=%08lx -> rs=%08lx %08lx",
  73002. (unsigned long) y->v[i], (unsigned long) z->v[j],
  73003. (unsigned long) x->v[i+j], (unsigned long) r,
  73004. (unsigned long) s));
  73005. x->v[i+j] = s;
  73006. f = r;
  73007. }
  73008. if (f > 0U) {
  73009. DUK_ASSERT(i + j < nx);
  73010. DUK_ASSERT(i + j < DUK__BI_MAX_PARTS);
  73011. DUK_ASSERT(x->v[i+j] == 0U);
  73012. x->v[i+j] = (duk_uint32_t) f;
  73013. }
  73014. #endif /* DUK_USE_64BIT_OPS */
  73015. }
  73016. duk__bi_normalize(x);
  73017. DUK_ASSERT(duk__bi_is_valid(x));
  73018. }
  73019. /* x <- y * z */
  73020. DUK_LOCAL void duk__bi_mul_small(duk__bigint *x, duk__bigint *y, duk_uint32_t z) {
  73021. duk__bigint tmp;
  73022. DUK_ASSERT(duk__bi_is_valid(y));
  73023. /* XXX: this could be optimized */
  73024. duk__bi_set_small(&tmp, z);
  73025. duk__bi_mul(x, y, &tmp);
  73026. DUK_ASSERT(duk__bi_is_valid(x));
  73027. }
  73028. /* x <- x * y, use t as temp */
  73029. DUK_LOCAL void duk__bi_mul_copy(duk__bigint *x, duk__bigint *y, duk__bigint *t) {
  73030. duk__bi_mul(t, x, y);
  73031. duk__bi_copy(x, t);
  73032. }
  73033. /* x <- x * y, use t as temp */
  73034. DUK_LOCAL void duk__bi_mul_small_copy(duk__bigint *x, duk_uint32_t y, duk__bigint *t) {
  73035. duk__bi_mul_small(t, x, y);
  73036. duk__bi_copy(x, t);
  73037. }
  73038. DUK_LOCAL int duk__bi_is_even(duk__bigint *x) {
  73039. DUK_ASSERT(duk__bi_is_valid(x));
  73040. return (x->n == 0) || ((x->v[0] & 0x01) == 0);
  73041. }
  73042. DUK_LOCAL int duk__bi_is_zero(duk__bigint *x) {
  73043. DUK_ASSERT(duk__bi_is_valid(x));
  73044. return (x->n == 0); /* this is the case for normalized numbers */
  73045. }
  73046. /* Bigint is 2^52. Used to detect normalized IEEE double mantissa values
  73047. * which are at the lowest edge (next floating point value downwards has
  73048. * a different exponent). The lowest mantissa has the form:
  73049. *
  73050. * 1000........000 (52 zeroes; only "hidden bit" is set)
  73051. */
  73052. DUK_LOCAL duk_small_int_t duk__bi_is_2to52(duk__bigint *x) {
  73053. DUK_ASSERT(duk__bi_is_valid(x));
  73054. return (duk_small_int_t)
  73055. (x->n == 2) && (x->v[0] == 0U) && (x->v[1] == (1U << (52-32)));
  73056. }
  73057. /* x <- (1<<y) */
  73058. DUK_LOCAL void duk__bi_twoexp(duk__bigint *x, duk_small_int_t y) {
  73059. duk_small_int_t n, r;
  73060. n = (y / 32) + 1;
  73061. DUK_ASSERT(n > 0);
  73062. r = y % 32;
  73063. duk_memzero((void *) x->v, sizeof(duk_uint32_t) * (size_t) n);
  73064. x->n = n;
  73065. x->v[n - 1] = (((duk_uint32_t) 1) << r);
  73066. }
  73067. /* x <- b^y; use t1 and t2 as temps */
  73068. DUK_LOCAL void duk__bi_exp_small(duk__bigint *x, duk_small_int_t b, duk_small_int_t y, duk__bigint *t1, duk__bigint *t2) {
  73069. /* Fast path the binary case */
  73070. DUK_ASSERT(x != t1 && x != t2 && t1 != t2); /* distinct bignums, easy mistake to make */
  73071. DUK_ASSERT(b >= 0);
  73072. DUK_ASSERT(y >= 0);
  73073. if (b == 2) {
  73074. duk__bi_twoexp(x, y);
  73075. return;
  73076. }
  73077. /* http://en.wikipedia.org/wiki/Exponentiation_by_squaring */
  73078. DUK_DDD(DUK_DDDPRINT("exp_small: b=%ld, y=%ld", (long) b, (long) y));
  73079. duk__bi_set_small(x, 1);
  73080. duk__bi_set_small(t1, (duk_uint32_t) b);
  73081. for (;;) {
  73082. /* Loop structure ensures that we don't compute t1^2 unnecessarily
  73083. * on the final round, as that might create a bignum exceeding the
  73084. * current DUK__BI_MAX_PARTS limit.
  73085. */
  73086. if (y & 0x01) {
  73087. duk__bi_mul_copy(x, t1, t2);
  73088. }
  73089. y = y >> 1;
  73090. if (y == 0) {
  73091. break;
  73092. }
  73093. duk__bi_mul_copy(t1, t1, t2);
  73094. }
  73095. DUK__BI_PRINT("exp_small result", x);
  73096. }
  73097. /*
  73098. * A Dragon4 number-to-string variant, based on:
  73099. *
  73100. * Guy L. Steele Jr., Jon L. White: "How to Print Floating-Point Numbers
  73101. * Accurately"
  73102. *
  73103. * Robert G. Burger, R. Kent Dybvig: "Printing Floating-Point Numbers
  73104. * Quickly and Accurately"
  73105. *
  73106. * The current algorithm is based on Figure 1 of the Burger-Dybvig paper,
  73107. * i.e. the base implementation without logarithm estimation speedups
  73108. * (these would increase code footprint considerably). Fixed-format output
  73109. * does not follow the suggestions in the paper; instead, we generate an
  73110. * extra digit and round-with-carry.
  73111. *
  73112. * The same algorithm is used for number parsing (with b=10 and B=2)
  73113. * by generating one extra digit and doing rounding manually.
  73114. *
  73115. * See doc/number-conversion.rst for limitations.
  73116. */
  73117. /* Maximum number of digits generated. */
  73118. #define DUK__MAX_OUTPUT_DIGITS 1040 /* (Number.MAX_VALUE).toString(2).length == 1024, + slack */
  73119. /* Maximum number of characters in formatted value. */
  73120. #define DUK__MAX_FORMATTED_LENGTH 1040 /* (-Number.MAX_VALUE).toString(2).length == 1025, + slack */
  73121. /* Number and (minimum) size of bigints in the nc_ctx structure. */
  73122. #define DUK__NUMCONV_CTX_NUM_BIGINTS 7
  73123. #define DUK__NUMCONV_CTX_BIGINTS_SIZE (sizeof(duk__bigint) * DUK__NUMCONV_CTX_NUM_BIGINTS)
  73124. typedef struct {
  73125. /* Currently about 7*152 = 1064 bytes. The space for these
  73126. * duk__bigints is used also as a temporary buffer for generating
  73127. * the final string. This is a bit awkard; a union would be
  73128. * more correct.
  73129. */
  73130. duk__bigint f, r, s, mp, mm, t1, t2;
  73131. duk_small_int_t is_s2n; /* if 1, doing a string-to-number; else doing a number-to-string */
  73132. duk_small_int_t is_fixed; /* if 1, doing a fixed format output (not free format) */
  73133. duk_small_int_t req_digits; /* requested number of output digits; 0 = free-format */
  73134. duk_small_int_t abs_pos; /* digit position is absolute, not relative */
  73135. duk_small_int_t e; /* exponent for 'f' */
  73136. duk_small_int_t b; /* input radix */
  73137. duk_small_int_t B; /* output radix */
  73138. duk_small_int_t k; /* see algorithm */
  73139. duk_small_int_t low_ok; /* see algorithm */
  73140. duk_small_int_t high_ok; /* see algorithm */
  73141. duk_small_int_t unequal_gaps; /* m+ != m- (very rarely) */
  73142. /* Buffer used for generated digits, values are in the range [0,B-1]. */
  73143. duk_uint8_t digits[DUK__MAX_OUTPUT_DIGITS];
  73144. duk_small_int_t count; /* digit count */
  73145. } duk__numconv_stringify_ctx;
  73146. /* Note: computes with 'idx' in assertions, so caller beware.
  73147. * 'idx' is preincremented, i.e. '1' on first call, because it
  73148. * is more convenient for the caller.
  73149. */
  73150. #define DUK__DRAGON4_OUTPUT_PREINC(nc_ctx,preinc_idx,x) do { \
  73151. DUK_ASSERT((preinc_idx) - 1 >= 0); \
  73152. DUK_ASSERT((preinc_idx) - 1 < DUK__MAX_OUTPUT_DIGITS); \
  73153. ((nc_ctx)->digits[(preinc_idx) - 1]) = (duk_uint8_t) (x); \
  73154. } while (0)
  73155. DUK_LOCAL duk_size_t duk__dragon4_format_uint32(duk_uint8_t *buf, duk_uint32_t x, duk_small_int_t radix) {
  73156. duk_uint8_t *p;
  73157. duk_size_t len;
  73158. duk_small_int_t dig;
  73159. duk_uint32_t t;
  73160. DUK_ASSERT(buf != NULL);
  73161. DUK_ASSERT(radix >= 2 && radix <= 36);
  73162. /* A 32-bit unsigned integer formats to at most 32 digits (the
  73163. * worst case happens with radix == 2). Output the digits backwards,
  73164. * and use a memmove() to get them in the right place.
  73165. */
  73166. p = buf + 32;
  73167. for (;;) {
  73168. t = x / (duk_uint32_t) radix;
  73169. dig = (duk_small_int_t) (x - t * (duk_uint32_t) radix);
  73170. x = t;
  73171. DUK_ASSERT(dig >= 0 && dig < 36);
  73172. *(--p) = DUK__DIGITCHAR(dig);
  73173. if (x == 0) {
  73174. break;
  73175. }
  73176. }
  73177. len = (duk_size_t) ((buf + 32) - p);
  73178. duk_memmove((void *) buf, (const void *) p, (size_t) len);
  73179. return len;
  73180. }
  73181. DUK_LOCAL void duk__dragon4_prepare(duk__numconv_stringify_ctx *nc_ctx) {
  73182. duk_small_int_t lowest_mantissa;
  73183. #if 1
  73184. /* Assume IEEE round-to-even, so that shorter encoding can be used
  73185. * when round-to-even would produce correct result. By removing
  73186. * this check (and having low_ok == high_ok == 0) the results would
  73187. * still be accurate but in some cases longer than necessary.
  73188. */
  73189. if (duk__bi_is_even(&nc_ctx->f)) {
  73190. DUK_DDD(DUK_DDDPRINT("f is even"));
  73191. nc_ctx->low_ok = 1;
  73192. nc_ctx->high_ok = 1;
  73193. } else {
  73194. DUK_DDD(DUK_DDDPRINT("f is odd"));
  73195. nc_ctx->low_ok = 0;
  73196. nc_ctx->high_ok = 0;
  73197. }
  73198. #else
  73199. /* Note: not honoring round-to-even should work but now generates incorrect
  73200. * results. For instance, 1e23 serializes to "a000...", i.e. the first digit
  73201. * equals the radix (10). Scaling stops one step too early in this case.
  73202. * Don't know why this is the case, but since this code path is unused, it
  73203. * doesn't matter.
  73204. */
  73205. nc_ctx->low_ok = 0;
  73206. nc_ctx->high_ok = 0;
  73207. #endif
  73208. /* For string-to-number, pretend we never have the lowest mantissa as there
  73209. * is no natural "precision" for inputs. Having lowest_mantissa == 0, we'll
  73210. * fall into the base cases for both e >= 0 and e < 0.
  73211. */
  73212. if (nc_ctx->is_s2n) {
  73213. lowest_mantissa = 0;
  73214. } else {
  73215. lowest_mantissa = duk__bi_is_2to52(&nc_ctx->f);
  73216. }
  73217. nc_ctx->unequal_gaps = 0;
  73218. if (nc_ctx->e >= 0) {
  73219. /* exponent non-negative (and thus not minimum exponent) */
  73220. if (lowest_mantissa) {
  73221. /* (>= e 0) AND (= f (expt b (- p 1)))
  73222. *
  73223. * be <- (expt b e) == b^e
  73224. * be1 <- (* be b) == (expt b (+ e 1)) == b^(e+1)
  73225. * r <- (* f be1 2) == 2 * f * b^(e+1) [if b==2 -> f * b^(e+2)]
  73226. * s <- (* b 2) [if b==2 -> 4]
  73227. * m+ <- be1 == b^(e+1)
  73228. * m- <- be == b^e
  73229. * k <- 0
  73230. * B <- B
  73231. * low_ok <- round
  73232. * high_ok <- round
  73233. */
  73234. DUK_DDD(DUK_DDDPRINT("non-negative exponent (not smallest exponent); "
  73235. "lowest mantissa value for this exponent -> "
  73236. "unequal gaps"));
  73237. duk__bi_exp_small(&nc_ctx->mm, nc_ctx->b, nc_ctx->e, &nc_ctx->t1, &nc_ctx->t2); /* mm <- b^e */
  73238. duk__bi_mul_small(&nc_ctx->mp, &nc_ctx->mm, (duk_uint32_t) nc_ctx->b); /* mp <- b^(e+1) */
  73239. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, 2);
  73240. duk__bi_mul(&nc_ctx->r, &nc_ctx->t1, &nc_ctx->mp); /* r <- (2 * f) * b^(e+1) */
  73241. duk__bi_set_small(&nc_ctx->s, (duk_uint32_t) (nc_ctx->b * 2)); /* s <- 2 * b */
  73242. nc_ctx->unequal_gaps = 1;
  73243. } else {
  73244. /* (>= e 0) AND (not (= f (expt b (- p 1))))
  73245. *
  73246. * be <- (expt b e) == b^e
  73247. * r <- (* f be 2) == 2 * f * b^e [if b==2 -> f * b^(e+1)]
  73248. * s <- 2
  73249. * m+ <- be == b^e
  73250. * m- <- be == b^e
  73251. * k <- 0
  73252. * B <- B
  73253. * low_ok <- round
  73254. * high_ok <- round
  73255. */
  73256. DUK_DDD(DUK_DDDPRINT("non-negative exponent (not smallest exponent); "
  73257. "not lowest mantissa for this exponent -> "
  73258. "equal gaps"));
  73259. duk__bi_exp_small(&nc_ctx->mm, nc_ctx->b, nc_ctx->e, &nc_ctx->t1, &nc_ctx->t2); /* mm <- b^e */
  73260. duk__bi_copy(&nc_ctx->mp, &nc_ctx->mm); /* mp <- b^e */
  73261. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, 2);
  73262. duk__bi_mul(&nc_ctx->r, &nc_ctx->t1, &nc_ctx->mp); /* r <- (2 * f) * b^e */
  73263. duk__bi_set_small(&nc_ctx->s, 2); /* s <- 2 */
  73264. }
  73265. } else {
  73266. /* When doing string-to-number, lowest_mantissa is always 0 so
  73267. * the exponent check, while incorrect, won't matter.
  73268. */
  73269. if (nc_ctx->e > DUK__IEEE_DOUBLE_EXP_MIN /*not minimum exponent*/ &&
  73270. lowest_mantissa /* lowest mantissa for this exponent*/) {
  73271. /* r <- (* f b 2) [if b==2 -> (* f 4)]
  73272. * s <- (* (expt b (- 1 e)) 2) == b^(1-e) * 2 [if b==2 -> b^(2-e)]
  73273. * m+ <- b == 2
  73274. * m- <- 1
  73275. * k <- 0
  73276. * B <- B
  73277. * low_ok <- round
  73278. * high_ok <- round
  73279. */
  73280. DUK_DDD(DUK_DDDPRINT("negative exponent; not minimum exponent and "
  73281. "lowest mantissa for this exponent -> "
  73282. "unequal gaps"));
  73283. duk__bi_mul_small(&nc_ctx->r, &nc_ctx->f, (duk_uint32_t) (nc_ctx->b * 2)); /* r <- (2 * b) * f */
  73284. duk__bi_exp_small(&nc_ctx->t1, nc_ctx->b, 1 - nc_ctx->e, &nc_ctx->s, &nc_ctx->t2); /* NB: use 's' as temp on purpose */
  73285. duk__bi_mul_small(&nc_ctx->s, &nc_ctx->t1, 2); /* s <- b^(1-e) * 2 */
  73286. duk__bi_set_small(&nc_ctx->mp, 2);
  73287. duk__bi_set_small(&nc_ctx->mm, 1);
  73288. nc_ctx->unequal_gaps = 1;
  73289. } else {
  73290. /* r <- (* f 2)
  73291. * s <- (* (expt b (- e)) 2) == b^(-e) * 2 [if b==2 -> b^(1-e)]
  73292. * m+ <- 1
  73293. * m- <- 1
  73294. * k <- 0
  73295. * B <- B
  73296. * low_ok <- round
  73297. * high_ok <- round
  73298. */
  73299. DUK_DDD(DUK_DDDPRINT("negative exponent; minimum exponent or not "
  73300. "lowest mantissa for this exponent -> "
  73301. "equal gaps"));
  73302. duk__bi_mul_small(&nc_ctx->r, &nc_ctx->f, 2); /* r <- 2 * f */
  73303. duk__bi_exp_small(&nc_ctx->t1, nc_ctx->b, -nc_ctx->e, &nc_ctx->s, &nc_ctx->t2); /* NB: use 's' as temp on purpose */
  73304. duk__bi_mul_small(&nc_ctx->s, &nc_ctx->t1, 2); /* s <- b^(-e) * 2 */
  73305. duk__bi_set_small(&nc_ctx->mp, 1);
  73306. duk__bi_set_small(&nc_ctx->mm, 1);
  73307. }
  73308. }
  73309. }
  73310. DUK_LOCAL void duk__dragon4_scale(duk__numconv_stringify_ctx *nc_ctx) {
  73311. duk_small_int_t k = 0;
  73312. /* This is essentially the 'scale' algorithm, with recursion removed.
  73313. * Note that 'k' is either correct immediately, or will move in one
  73314. * direction in the loop. There's no need to do the low/high checks
  73315. * on every round (like the Scheme algorithm does).
  73316. *
  73317. * The scheme algorithm finds 'k' and updates 's' simultaneously,
  73318. * while the logical algorithm finds 'k' with 's' having its initial
  73319. * value, after which 's' is updated separately (see the Burger-Dybvig
  73320. * paper, Section 3.1, steps 2 and 3).
  73321. *
  73322. * The case where m+ == m- (almost always) is optimized for, because
  73323. * it reduces the bigint operations considerably and almost always
  73324. * applies. The scale loop only needs to work with m+, so this works.
  73325. */
  73326. /* XXX: this algorithm could be optimized quite a lot by using e.g.
  73327. * a logarithm based estimator for 'k' and performing B^n multiplication
  73328. * using a lookup table or using some bit-representation based exp
  73329. * algorithm. Currently we just loop, with significant performance
  73330. * impact for very large and very small numbers.
  73331. */
  73332. DUK_DDD(DUK_DDDPRINT("scale: B=%ld, low_ok=%ld, high_ok=%ld",
  73333. (long) nc_ctx->B, (long) nc_ctx->low_ok, (long) nc_ctx->high_ok));
  73334. DUK__BI_PRINT("r(init)", &nc_ctx->r);
  73335. DUK__BI_PRINT("s(init)", &nc_ctx->s);
  73336. DUK__BI_PRINT("mp(init)", &nc_ctx->mp);
  73337. DUK__BI_PRINT("mm(init)", &nc_ctx->mm);
  73338. for (;;) {
  73339. DUK_DDD(DUK_DDDPRINT("scale loop (inc k), k=%ld", (long) k));
  73340. DUK__BI_PRINT("r", &nc_ctx->r);
  73341. DUK__BI_PRINT("s", &nc_ctx->s);
  73342. DUK__BI_PRINT("m+", &nc_ctx->mp);
  73343. DUK__BI_PRINT("m-", &nc_ctx->mm);
  73344. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 = (+ r m+) */
  73345. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) >= (nc_ctx->high_ok ? 0 : 1)) {
  73346. DUK_DDD(DUK_DDDPRINT("k is too low"));
  73347. /* r <- r
  73348. * s <- (* s B)
  73349. * m+ <- m+
  73350. * m- <- m-
  73351. * k <- (+ k 1)
  73352. */
  73353. duk__bi_mul_small_copy(&nc_ctx->s, (duk_uint32_t) nc_ctx->B, &nc_ctx->t1);
  73354. k++;
  73355. } else {
  73356. break;
  73357. }
  73358. }
  73359. /* k > 0 -> k was too low, and cannot be too high */
  73360. if (k > 0) {
  73361. goto skip_dec_k;
  73362. }
  73363. for (;;) {
  73364. DUK_DDD(DUK_DDDPRINT("scale loop (dec k), k=%ld", (long) k));
  73365. DUK__BI_PRINT("r", &nc_ctx->r);
  73366. DUK__BI_PRINT("s", &nc_ctx->s);
  73367. DUK__BI_PRINT("m+", &nc_ctx->mp);
  73368. DUK__BI_PRINT("m-", &nc_ctx->mm);
  73369. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 = (+ r m+) */
  73370. duk__bi_mul_small(&nc_ctx->t2, &nc_ctx->t1, (duk_uint32_t) nc_ctx->B); /* t2 = (* (+ r m+) B) */
  73371. if (duk__bi_compare(&nc_ctx->t2, &nc_ctx->s) <= (nc_ctx->high_ok ? -1 : 0)) {
  73372. DUK_DDD(DUK_DDDPRINT("k is too high"));
  73373. /* r <- (* r B)
  73374. * s <- s
  73375. * m+ <- (* m+ B)
  73376. * m- <- (* m- B)
  73377. * k <- (- k 1)
  73378. */
  73379. duk__bi_mul_small_copy(&nc_ctx->r, (duk_uint32_t) nc_ctx->B, &nc_ctx->t1);
  73380. duk__bi_mul_small_copy(&nc_ctx->mp, (duk_uint32_t) nc_ctx->B, &nc_ctx->t1);
  73381. if (nc_ctx->unequal_gaps) {
  73382. DUK_DDD(DUK_DDDPRINT("m+ != m- -> need to update m- too"));
  73383. duk__bi_mul_small_copy(&nc_ctx->mm, (duk_uint32_t) nc_ctx->B, &nc_ctx->t1);
  73384. }
  73385. k--;
  73386. } else {
  73387. break;
  73388. }
  73389. }
  73390. skip_dec_k:
  73391. if (!nc_ctx->unequal_gaps) {
  73392. DUK_DDD(DUK_DDDPRINT("equal gaps, copy m- from m+"));
  73393. duk__bi_copy(&nc_ctx->mm, &nc_ctx->mp); /* mm <- mp */
  73394. }
  73395. nc_ctx->k = k;
  73396. DUK_DDD(DUK_DDDPRINT("final k: %ld", (long) k));
  73397. DUK__BI_PRINT("r(final)", &nc_ctx->r);
  73398. DUK__BI_PRINT("s(final)", &nc_ctx->s);
  73399. DUK__BI_PRINT("mp(final)", &nc_ctx->mp);
  73400. DUK__BI_PRINT("mm(final)", &nc_ctx->mm);
  73401. }
  73402. DUK_LOCAL void duk__dragon4_generate(duk__numconv_stringify_ctx *nc_ctx) {
  73403. duk_small_int_t tc1, tc2; /* terminating conditions */
  73404. duk_small_int_t d; /* current digit */
  73405. duk_small_int_t count = 0; /* digit count */
  73406. /*
  73407. * Digit generation loop.
  73408. *
  73409. * Different termination conditions:
  73410. *
  73411. * 1. Free format output. Terminate when shortest accurate
  73412. * representation found.
  73413. *
  73414. * 2. Fixed format output, with specific number of digits.
  73415. * Ignore termination conditions, terminate when digits
  73416. * generated. Caller requests an extra digit and rounds.
  73417. *
  73418. * 3. Fixed format output, with a specific absolute cut-off
  73419. * position (e.g. 10 digits after decimal point). Note
  73420. * that we always generate at least one digit, even if
  73421. * the digit is below the cut-off point already.
  73422. */
  73423. for (;;) {
  73424. DUK_DDD(DUK_DDDPRINT("generate loop, count=%ld, k=%ld, B=%ld, low_ok=%ld, high_ok=%ld",
  73425. (long) count, (long) nc_ctx->k, (long) nc_ctx->B,
  73426. (long) nc_ctx->low_ok, (long) nc_ctx->high_ok));
  73427. DUK__BI_PRINT("r", &nc_ctx->r);
  73428. DUK__BI_PRINT("s", &nc_ctx->s);
  73429. DUK__BI_PRINT("m+", &nc_ctx->mp);
  73430. DUK__BI_PRINT("m-", &nc_ctx->mm);
  73431. /* (quotient-remainder (* r B) s) using a dummy subtraction loop */
  73432. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->r, (duk_uint32_t) nc_ctx->B); /* t1 <- (* r B) */
  73433. d = 0;
  73434. for (;;) {
  73435. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) < 0) {
  73436. break;
  73437. }
  73438. duk__bi_sub_copy(&nc_ctx->t1, &nc_ctx->s, &nc_ctx->t2); /* t1 <- t1 - s */
  73439. d++;
  73440. }
  73441. duk__bi_copy(&nc_ctx->r, &nc_ctx->t1); /* r <- (remainder (* r B) s) */
  73442. /* d <- (quotient (* r B) s) (in range 0...B-1) */
  73443. DUK_DDD(DUK_DDDPRINT("-> d(quot)=%ld", (long) d));
  73444. DUK__BI_PRINT("r(rem)", &nc_ctx->r);
  73445. duk__bi_mul_small_copy(&nc_ctx->mp, (duk_uint32_t) nc_ctx->B, &nc_ctx->t2); /* m+ <- (* m+ B) */
  73446. duk__bi_mul_small_copy(&nc_ctx->mm, (duk_uint32_t) nc_ctx->B, &nc_ctx->t2); /* m- <- (* m- B) */
  73447. DUK__BI_PRINT("mp(upd)", &nc_ctx->mp);
  73448. DUK__BI_PRINT("mm(upd)", &nc_ctx->mm);
  73449. /* Terminating conditions. For fixed width output, we just ignore the
  73450. * terminating conditions (and pretend that tc1 == tc2 == false). The
  73451. * the current shortcut for fixed-format output is to generate a few
  73452. * extra digits and use rounding (with carry) to finish the output.
  73453. */
  73454. if (nc_ctx->is_fixed == 0) {
  73455. /* free-form */
  73456. tc1 = (duk__bi_compare(&nc_ctx->r, &nc_ctx->mm) <= (nc_ctx->low_ok ? 0 : -1));
  73457. duk__bi_add(&nc_ctx->t1, &nc_ctx->r, &nc_ctx->mp); /* t1 <- (+ r m+) */
  73458. tc2 = (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) >= (nc_ctx->high_ok ? 0 : 1));
  73459. DUK_DDD(DUK_DDDPRINT("tc1=%ld, tc2=%ld", (long) tc1, (long) tc2));
  73460. } else {
  73461. /* fixed-format */
  73462. tc1 = 0;
  73463. tc2 = 0;
  73464. }
  73465. /* Count is incremented before DUK__DRAGON4_OUTPUT_PREINC() call
  73466. * on purpose, which is taken into account by the macro.
  73467. */
  73468. count++;
  73469. if (tc1) {
  73470. if (tc2) {
  73471. /* tc1 = true, tc2 = true */
  73472. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->r, 2);
  73473. if (duk__bi_compare(&nc_ctx->t1, &nc_ctx->s) < 0) { /* (< (* r 2) s) */
  73474. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=true, 2r > s: output d --> %ld (k=%ld)",
  73475. (long) d, (long) nc_ctx->k));
  73476. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  73477. } else {
  73478. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=true, 2r <= s: output d+1 --> %ld (k=%ld)",
  73479. (long) (d + 1), (long) nc_ctx->k));
  73480. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d + 1);
  73481. }
  73482. break;
  73483. } else {
  73484. /* tc1 = true, tc2 = false */
  73485. DUK_DDD(DUK_DDDPRINT("tc1=true, tc2=false: output d --> %ld (k=%ld)",
  73486. (long) d, (long) nc_ctx->k));
  73487. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  73488. break;
  73489. }
  73490. } else {
  73491. if (tc2) {
  73492. /* tc1 = false, tc2 = true */
  73493. DUK_DDD(DUK_DDDPRINT("tc1=false, tc2=true: output d+1 --> %ld (k=%ld)",
  73494. (long) (d + 1), (long) nc_ctx->k));
  73495. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d + 1);
  73496. break;
  73497. } else {
  73498. /* tc1 = false, tc2 = false */
  73499. DUK_DDD(DUK_DDDPRINT("tc1=false, tc2=false: output d --> %ld (k=%ld)",
  73500. (long) d, (long) nc_ctx->k));
  73501. DUK__DRAGON4_OUTPUT_PREINC(nc_ctx, count, d);
  73502. /* r <- r (updated above: r <- (remainder (* r B) s)
  73503. * s <- s
  73504. * m+ <- m+ (updated above: m+ <- (* m+ B)
  73505. * m- <- m- (updated above: m- <- (* m- B)
  73506. * B, low_ok, high_ok are fixed
  73507. */
  73508. /* fall through and continue for-loop */
  73509. }
  73510. }
  73511. /* fixed-format termination conditions */
  73512. if (nc_ctx->is_fixed) {
  73513. if (nc_ctx->abs_pos) {
  73514. int pos = nc_ctx->k - count + 1; /* count is already incremented, take into account */
  73515. DUK_DDD(DUK_DDDPRINT("fixed format, absolute: abs pos=%ld, k=%ld, count=%ld, req=%ld",
  73516. (long) pos, (long) nc_ctx->k, (long) count, (long) nc_ctx->req_digits));
  73517. if (pos <= nc_ctx->req_digits) {
  73518. DUK_DDD(DUK_DDDPRINT("digit position reached req_digits, end generate loop"));
  73519. break;
  73520. }
  73521. } else {
  73522. DUK_DDD(DUK_DDDPRINT("fixed format, relative: k=%ld, count=%ld, req=%ld",
  73523. (long) nc_ctx->k, (long) count, (long) nc_ctx->req_digits));
  73524. if (count >= nc_ctx->req_digits) {
  73525. DUK_DDD(DUK_DDDPRINT("digit count reached req_digits, end generate loop"));
  73526. break;
  73527. }
  73528. }
  73529. }
  73530. } /* for */
  73531. nc_ctx->count = count;
  73532. DUK_DDD(DUK_DDDPRINT("generate finished"));
  73533. #if defined(DUK_USE_DEBUG_LEVEL) && (DUK_USE_DEBUG_LEVEL >= 2)
  73534. {
  73535. duk_uint8_t buf[2048];
  73536. duk_small_int_t i, t;
  73537. duk_memzero(buf, sizeof(buf));
  73538. for (i = 0; i < nc_ctx->count; i++) {
  73539. t = nc_ctx->digits[i];
  73540. if (t < 0 || t > 36) {
  73541. buf[i] = (duk_uint8_t) '?';
  73542. } else {
  73543. buf[i] = (duk_uint8_t) DUK__DIGITCHAR(t);
  73544. }
  73545. }
  73546. DUK_DDD(DUK_DDDPRINT("-> generated digits; k=%ld, digits='%s'",
  73547. (long) nc_ctx->k, (const char *) buf));
  73548. }
  73549. #endif
  73550. }
  73551. /* Round up digits to a given position. If position is out-of-bounds,
  73552. * does nothing. If carry propagates over the first digit, a '1' is
  73553. * prepended to digits and 'k' will be updated. Return value indicates
  73554. * whether carry propagated over the first digit.
  73555. *
  73556. * Note that nc_ctx->count is NOT updated based on the rounding position
  73557. * (it is updated only if carry overflows over the first digit and an
  73558. * extra digit is prepended).
  73559. */
  73560. DUK_LOCAL duk_small_int_t duk__dragon4_fixed_format_round(duk__numconv_stringify_ctx *nc_ctx, duk_small_int_t round_idx) {
  73561. duk_small_int_t t;
  73562. duk_uint8_t *p;
  73563. duk_uint8_t roundup_limit;
  73564. duk_small_int_t ret = 0;
  73565. /*
  73566. * round_idx points to the digit which is considered for rounding; the
  73567. * digit to its left is the final digit of the rounded value. If round_idx
  73568. * is zero, rounding will be performed; the result will either be an empty
  73569. * rounded value or if carry happens a '1' digit is generated.
  73570. */
  73571. if (round_idx >= nc_ctx->count) {
  73572. DUK_DDD(DUK_DDDPRINT("round_idx out of bounds (%ld >= %ld (count)) -> no rounding",
  73573. (long) round_idx, (long) nc_ctx->count));
  73574. return 0;
  73575. } else if (round_idx < 0) {
  73576. DUK_DDD(DUK_DDDPRINT("round_idx out of bounds (%ld < 0) -> no rounding",
  73577. (long) round_idx));
  73578. return 0;
  73579. }
  73580. /*
  73581. * Round-up limit.
  73582. *
  73583. * For even values, divides evenly, e.g. 10 -> roundup_limit=5.
  73584. *
  73585. * For odd values, rounds up, e.g. 3 -> roundup_limit=2.
  73586. * If radix is 3, 0/3 -> down, 1/3 -> down, 2/3 -> up.
  73587. */
  73588. roundup_limit = (duk_uint8_t) ((nc_ctx->B + 1) / 2);
  73589. p = &nc_ctx->digits[round_idx];
  73590. if (*p >= roundup_limit) {
  73591. DUK_DDD(DUK_DDDPRINT("fixed-format rounding carry required"));
  73592. /* carry */
  73593. for (;;) {
  73594. *p = 0;
  73595. if (p == &nc_ctx->digits[0]) {
  73596. DUK_DDD(DUK_DDDPRINT("carry propagated to first digit -> special case handling"));
  73597. duk_memmove((void *) (&nc_ctx->digits[1]),
  73598. (const void *) (&nc_ctx->digits[0]),
  73599. (size_t) (sizeof(char) * (size_t) nc_ctx->count));
  73600. nc_ctx->digits[0] = 1; /* don't increase 'count' */
  73601. nc_ctx->k++; /* position of highest digit changed */
  73602. nc_ctx->count++; /* number of digits changed */
  73603. ret = 1;
  73604. break;
  73605. }
  73606. DUK_DDD(DUK_DDDPRINT("fixed-format rounding carry: B=%ld, roundup_limit=%ld, p=%p, digits=%p",
  73607. (long) nc_ctx->B, (long) roundup_limit, (void *) p, (void *) nc_ctx->digits));
  73608. p--;
  73609. t = *p;
  73610. DUK_DDD(DUK_DDDPRINT("digit before carry: %ld", (long) t));
  73611. if (++t < nc_ctx->B) {
  73612. DUK_DDD(DUK_DDDPRINT("rounding carry terminated"));
  73613. *p = (duk_uint8_t) t;
  73614. break;
  73615. }
  73616. DUK_DDD(DUK_DDDPRINT("wraps, carry to next digit"));
  73617. }
  73618. }
  73619. return ret;
  73620. }
  73621. #define DUK__NO_EXP (65536) /* arbitrary marker, outside valid exp range */
  73622. DUK_LOCAL void duk__dragon4_convert_and_push(duk__numconv_stringify_ctx *nc_ctx,
  73623. duk_hthread *thr,
  73624. duk_small_int_t radix,
  73625. duk_small_int_t digits,
  73626. duk_small_uint_t flags,
  73627. duk_small_int_t neg) {
  73628. duk_small_int_t k;
  73629. duk_small_int_t pos, pos_end;
  73630. duk_small_int_t expt;
  73631. duk_small_int_t dig;
  73632. duk_uint8_t *q;
  73633. duk_uint8_t *buf;
  73634. /*
  73635. * The string conversion here incorporates all the necessary ECMAScript
  73636. * semantics without attempting to be generic. nc_ctx->digits contains
  73637. * nc_ctx->count digits (>= 1), with the topmost digit's 'position'
  73638. * indicated by nc_ctx->k as follows:
  73639. *
  73640. * digits="123" count=3 k=0 --> 0.123
  73641. * digits="123" count=3 k=1 --> 1.23
  73642. * digits="123" count=3 k=5 --> 12300
  73643. * digits="123" count=3 k=-1 --> 0.0123
  73644. *
  73645. * Note that the identifier names used for format selection are different
  73646. * in Burger-Dybvig paper and ECMAScript specification (quite confusingly
  73647. * so, because e.g. 'k' has a totally different meaning in each). See
  73648. * documentation for discussion.
  73649. *
  73650. * ECMAScript doesn't specify any specific behavior for format selection
  73651. * (e.g. when to use exponent notation) for non-base-10 numbers.
  73652. *
  73653. * The bigint space in the context is reused for string output, as there
  73654. * is more than enough space for that (>1kB at the moment), and we avoid
  73655. * allocating even more stack.
  73656. */
  73657. DUK_ASSERT(DUK__NUMCONV_CTX_BIGINTS_SIZE >= DUK__MAX_FORMATTED_LENGTH);
  73658. DUK_ASSERT(nc_ctx->count >= 1);
  73659. k = nc_ctx->k;
  73660. buf = (duk_uint8_t *) &nc_ctx->f; /* XXX: union would be more correct */
  73661. q = buf;
  73662. /* Exponent handling: if exponent format is used, record exponent value and
  73663. * fake k such that one leading digit is generated (e.g. digits=123 -> "1.23").
  73664. *
  73665. * toFixed() prevents exponent use; otherwise apply a set of criteria to
  73666. * match the other API calls (toString(), toPrecision, etc).
  73667. */
  73668. expt = DUK__NO_EXP;
  73669. if (!nc_ctx->abs_pos /* toFixed() */) {
  73670. if ((flags & DUK_N2S_FLAG_FORCE_EXP) || /* exponential notation forced */
  73671. ((flags & DUK_N2S_FLAG_NO_ZERO_PAD) && /* fixed precision and zero padding would be required */
  73672. (k - digits >= 1)) || /* (e.g. k=3, digits=2 -> "12X") */
  73673. ((k > 21 || k <= -6) && (radix == 10))) { /* toString() conditions */
  73674. DUK_DDD(DUK_DDDPRINT("use exponential notation: k=%ld -> expt=%ld",
  73675. (long) k, (long) (k - 1)));
  73676. expt = k - 1; /* e.g. 12.3 -> digits="123" k=2 -> 1.23e1 */
  73677. k = 1; /* generate mantissa with a single leading whole number digit */
  73678. }
  73679. }
  73680. if (neg) {
  73681. *q++ = '-';
  73682. }
  73683. /* Start position (inclusive) and end position (exclusive) */
  73684. pos = (k >= 1 ? k : 1);
  73685. if (nc_ctx->is_fixed) {
  73686. if (nc_ctx->abs_pos) {
  73687. /* toFixed() */
  73688. pos_end = -digits;
  73689. } else {
  73690. pos_end = k - digits;
  73691. }
  73692. } else {
  73693. pos_end = k - nc_ctx->count;
  73694. }
  73695. if (pos_end > 0) {
  73696. pos_end = 0;
  73697. }
  73698. DUK_DDD(DUK_DDDPRINT("expt=%ld, k=%ld, count=%ld, pos=%ld, pos_end=%ld, is_fixed=%ld, "
  73699. "digits=%ld, abs_pos=%ld",
  73700. (long) expt, (long) k, (long) nc_ctx->count, (long) pos, (long) pos_end,
  73701. (long) nc_ctx->is_fixed, (long) digits, (long) nc_ctx->abs_pos));
  73702. /* Digit generation */
  73703. while (pos > pos_end) {
  73704. DUK_DDD(DUK_DDDPRINT("digit generation: pos=%ld, pos_end=%ld",
  73705. (long) pos, (long) pos_end));
  73706. if (pos == 0) {
  73707. *q++ = (duk_uint8_t) '.';
  73708. }
  73709. if (pos > k) {
  73710. *q++ = (duk_uint8_t) '0';
  73711. } else if (pos <= k - nc_ctx->count) {
  73712. *q++ = (duk_uint8_t) '0';
  73713. } else {
  73714. dig = nc_ctx->digits[k - pos];
  73715. DUK_ASSERT(dig >= 0 && dig < nc_ctx->B);
  73716. *q++ = (duk_uint8_t) DUK__DIGITCHAR(dig);
  73717. }
  73718. pos--;
  73719. }
  73720. DUK_ASSERT(pos <= 1);
  73721. /* Exponent */
  73722. if (expt != DUK__NO_EXP) {
  73723. /*
  73724. * Exponent notation for non-base-10 numbers isn't specified in ECMAScript
  73725. * specification, as it never explicitly turns up: non-decimal numbers can
  73726. * only be formatted with Number.prototype.toString([radix]) and for that,
  73727. * behavior is not explicitly specified.
  73728. *
  73729. * Logical choices include formatting the exponent as decimal (e.g. binary
  73730. * 100000 as 1e+5) or in current radix (e.g. binary 100000 as 1e+101).
  73731. * The Dragon4 algorithm (in the original paper) prints the exponent value
  73732. * in the target radix B. However, for radix values 15 and above, the
  73733. * exponent separator 'e' is no longer easily parseable. Consider, for
  73734. * instance, the number "1.faecee+1c".
  73735. */
  73736. duk_size_t len;
  73737. char expt_sign;
  73738. *q++ = 'e';
  73739. if (expt >= 0) {
  73740. expt_sign = '+';
  73741. } else {
  73742. expt_sign = '-';
  73743. expt = -expt;
  73744. }
  73745. *q++ = (duk_uint8_t) expt_sign;
  73746. len = duk__dragon4_format_uint32(q, (duk_uint32_t) expt, radix);
  73747. q += len;
  73748. }
  73749. duk_push_lstring(thr, (const char *) buf, (size_t) (q - buf));
  73750. }
  73751. /*
  73752. * Conversion helpers
  73753. */
  73754. DUK_LOCAL void duk__dragon4_double_to_ctx(duk__numconv_stringify_ctx *nc_ctx, duk_double_t x) {
  73755. duk_double_union u;
  73756. duk_uint32_t tmp;
  73757. duk_small_int_t expt;
  73758. /*
  73759. * seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
  73760. * A B C D E F G H
  73761. *
  73762. * s sign bit
  73763. * eee... exponent field
  73764. * fff... fraction
  73765. *
  73766. * ieee value = 1.ffff... * 2^(e - 1023) (normal)
  73767. * = 0.ffff... * 2^(-1022) (denormal)
  73768. *
  73769. * algorithm v = f * b^e
  73770. */
  73771. DUK_DBLUNION_SET_DOUBLE(&u, x);
  73772. nc_ctx->f.n = 2;
  73773. tmp = DUK_DBLUNION_GET_LOW32(&u);
  73774. nc_ctx->f.v[0] = tmp;
  73775. tmp = DUK_DBLUNION_GET_HIGH32(&u);
  73776. nc_ctx->f.v[1] = tmp & 0x000fffffUL;
  73777. expt = (duk_small_int_t) ((tmp >> 20) & 0x07ffUL);
  73778. if (expt == 0) {
  73779. /* denormal */
  73780. expt = DUK__IEEE_DOUBLE_EXP_MIN - 52;
  73781. duk__bi_normalize(&nc_ctx->f);
  73782. } else {
  73783. /* normal: implicit leading 1-bit */
  73784. nc_ctx->f.v[1] |= 0x00100000UL;
  73785. expt = expt - DUK__IEEE_DOUBLE_EXP_BIAS - 52;
  73786. DUK_ASSERT(duk__bi_is_valid(&nc_ctx->f)); /* true, because v[1] has at least one bit set */
  73787. }
  73788. DUK_ASSERT(duk__bi_is_valid(&nc_ctx->f));
  73789. nc_ctx->e = expt;
  73790. }
  73791. DUK_LOCAL void duk__dragon4_ctx_to_double(duk__numconv_stringify_ctx *nc_ctx, duk_double_t *x) {
  73792. duk_double_union u;
  73793. duk_small_int_t expt;
  73794. duk_small_int_t i;
  73795. duk_small_int_t bitstart;
  73796. duk_small_int_t bitround;
  73797. duk_small_int_t bitidx;
  73798. duk_small_int_t skip_round;
  73799. duk_uint32_t t, v;
  73800. DUK_ASSERT(nc_ctx->count == 53 + 1);
  73801. /* Sometimes this assert is not true right now; it will be true after
  73802. * rounding. See: test-bug-numconv-mantissa-assert.js.
  73803. */
  73804. DUK_ASSERT_DISABLE(nc_ctx->digits[0] == 1); /* zero handled by caller */
  73805. /* Should not be required because the code below always sets both high
  73806. * and low parts, but at least gcc-4.4.5 fails to deduce this correctly
  73807. * (perhaps because the low part is set (seemingly) conditionally in a
  73808. * loop), so this is here to avoid the bogus warning.
  73809. */
  73810. duk_memzero((void *) &u, sizeof(u));
  73811. /*
  73812. * Figure out how generated digits match up with the mantissa,
  73813. * and then perform rounding. If mantissa overflows, need to
  73814. * recompute the exponent (it is bumped and may overflow to
  73815. * infinity).
  73816. *
  73817. * For normal numbers the leading '1' is hidden and ignored,
  73818. * and the last bit is used for rounding:
  73819. *
  73820. * rounding pt
  73821. * <--------52------->|
  73822. * 1 x x x x ... x x x x|y ==> x x x x ... x x x x
  73823. *
  73824. * For denormals, the leading '1' is included in the number,
  73825. * and the rounding point is different:
  73826. *
  73827. * rounding pt
  73828. * <--52 or less--->|
  73829. * 1 x x x x ... x x|x x y ==> 0 0 ... 1 x x ... x x
  73830. *
  73831. * The largest denormals will have a mantissa beginning with
  73832. * a '1' (the explicit leading bit); smaller denormals will
  73833. * have leading zero bits.
  73834. *
  73835. * If the exponent would become too high, the result becomes
  73836. * Infinity. If the exponent is so small that the entire
  73837. * mantissa becomes zero, the result becomes zero.
  73838. *
  73839. * Note: the Dragon4 'k' is off-by-one with respect to the IEEE
  73840. * exponent. For instance, k==0 indicates that the leading '1'
  73841. * digit is at the first binary fraction position (0.1xxx...);
  73842. * the corresponding IEEE exponent would be -1.
  73843. */
  73844. skip_round = 0;
  73845. recheck_exp:
  73846. expt = nc_ctx->k - 1; /* IEEE exp without bias */
  73847. if (expt > 1023) {
  73848. /* Infinity */
  73849. bitstart = -255; /* needed for inf: causes mantissa to become zero,
  73850. * and rounding to be skipped.
  73851. */
  73852. expt = 2047;
  73853. } else if (expt >= -1022) {
  73854. /* normal */
  73855. bitstart = 1; /* skip leading digit */
  73856. expt += DUK__IEEE_DOUBLE_EXP_BIAS;
  73857. DUK_ASSERT(expt >= 1 && expt <= 2046);
  73858. } else {
  73859. /* denormal or zero */
  73860. bitstart = 1023 + expt; /* expt==-1023 -> bitstart=0 (leading 1);
  73861. * expt==-1024 -> bitstart=-1 (one left of leading 1), etc
  73862. */
  73863. expt = 0;
  73864. }
  73865. bitround = bitstart + 52;
  73866. DUK_DDD(DUK_DDDPRINT("ieee expt=%ld, bitstart=%ld, bitround=%ld",
  73867. (long) expt, (long) bitstart, (long) bitround));
  73868. if (!skip_round) {
  73869. if (duk__dragon4_fixed_format_round(nc_ctx, bitround)) {
  73870. /* Corner case: see test-numconv-parse-mant-carry.js. We could
  73871. * just bump the exponent and update bitstart, but it's more robust
  73872. * to recompute (but avoid rounding twice).
  73873. */
  73874. DUK_DDD(DUK_DDDPRINT("rounding caused exponent to be bumped, recheck exponent"));
  73875. skip_round = 1;
  73876. goto recheck_exp;
  73877. }
  73878. }
  73879. /*
  73880. * Create mantissa
  73881. */
  73882. t = 0;
  73883. for (i = 0; i < 52; i++) {
  73884. bitidx = bitstart + 52 - 1 - i;
  73885. if (bitidx >= nc_ctx->count) {
  73886. v = 0;
  73887. } else if (bitidx < 0) {
  73888. v = 0;
  73889. } else {
  73890. v = nc_ctx->digits[bitidx];
  73891. }
  73892. DUK_ASSERT(v == 0 || v == 1);
  73893. t += v << (i % 32);
  73894. if (i == 31) {
  73895. /* low 32 bits is complete */
  73896. DUK_DBLUNION_SET_LOW32(&u, t);
  73897. t = 0;
  73898. }
  73899. }
  73900. /* t has high mantissa */
  73901. DUK_DDD(DUK_DDDPRINT("mantissa is complete: %08lx %08lx",
  73902. (unsigned long) t,
  73903. (unsigned long) DUK_DBLUNION_GET_LOW32(&u)));
  73904. DUK_ASSERT(expt >= 0 && expt <= 0x7ffL);
  73905. t += ((duk_uint32_t) expt) << 20;
  73906. #if 0 /* caller handles sign change */
  73907. if (negative) {
  73908. t |= 0x80000000U;
  73909. }
  73910. #endif
  73911. DUK_DBLUNION_SET_HIGH32(&u, t);
  73912. DUK_DDD(DUK_DDDPRINT("number is complete: %08lx %08lx",
  73913. (unsigned long) DUK_DBLUNION_GET_HIGH32(&u),
  73914. (unsigned long) DUK_DBLUNION_GET_LOW32(&u)));
  73915. *x = DUK_DBLUNION_GET_DOUBLE(&u);
  73916. }
  73917. /*
  73918. * Exposed number-to-string API
  73919. *
  73920. * Input: [ number ]
  73921. * Output: [ string ]
  73922. */
  73923. DUK_INTERNAL void duk_numconv_stringify(duk_hthread *thr, duk_small_int_t radix, duk_small_int_t digits, duk_small_uint_t flags) {
  73924. duk_double_t x;
  73925. duk_small_int_t c;
  73926. duk_small_int_t neg;
  73927. duk_uint32_t uval;
  73928. duk__numconv_stringify_ctx nc_ctx_alloc; /* large context; around 2kB now */
  73929. duk__numconv_stringify_ctx *nc_ctx = &nc_ctx_alloc;
  73930. x = (duk_double_t) duk_require_number(thr, -1);
  73931. duk_pop(thr);
  73932. /*
  73933. * Handle special cases (NaN, infinity, zero).
  73934. */
  73935. c = (duk_small_int_t) DUK_FPCLASSIFY(x);
  73936. if (DUK_SIGNBIT((double) x)) {
  73937. x = -x;
  73938. neg = 1;
  73939. } else {
  73940. neg = 0;
  73941. }
  73942. /* NaN sign bit is platform specific with unpacked, un-normalized NaNs */
  73943. DUK_ASSERT(c == DUK_FP_NAN || DUK_SIGNBIT((double) x) == 0);
  73944. if (c == DUK_FP_NAN) {
  73945. duk_push_hstring_stridx(thr, DUK_STRIDX_NAN);
  73946. return;
  73947. } else if (c == DUK_FP_INFINITE) {
  73948. if (neg) {
  73949. /* -Infinity */
  73950. duk_push_hstring_stridx(thr, DUK_STRIDX_MINUS_INFINITY);
  73951. } else {
  73952. /* Infinity */
  73953. duk_push_hstring_stridx(thr, DUK_STRIDX_INFINITY);
  73954. }
  73955. return;
  73956. } else if (c == DUK_FP_ZERO) {
  73957. /* We can't shortcut zero here if it goes through special formatting
  73958. * (such as forced exponential notation).
  73959. */
  73960. ;
  73961. }
  73962. /*
  73963. * Handle integers in 32-bit range (that is, [-(2**32-1),2**32-1])
  73964. * specially, as they're very likely for embedded programs. This
  73965. * is now done for all radix values. We must be careful not to use
  73966. * the fast path when special formatting (e.g. forced exponential)
  73967. * is in force.
  73968. *
  73969. * XXX: could save space by supporting radix 10 only and using
  73970. * sprintf "%lu" for the fast path and for exponent formatting.
  73971. */
  73972. uval = duk_double_to_uint32_t(x);
  73973. if (((double) uval) == x && /* integer number in range */
  73974. flags == 0) { /* no special formatting */
  73975. /* use bigint area as a temp */
  73976. duk_uint8_t *buf = (duk_uint8_t *) (&nc_ctx->f);
  73977. duk_uint8_t *p = buf;
  73978. DUK_ASSERT(DUK__NUMCONV_CTX_BIGINTS_SIZE >= 32 + 1); /* max size: radix=2 + sign */
  73979. if (neg && uval != 0) {
  73980. /* no negative sign for zero */
  73981. *p++ = (duk_uint8_t) '-';
  73982. }
  73983. p += duk__dragon4_format_uint32(p, uval, radix);
  73984. duk_push_lstring(thr, (const char *) buf, (duk_size_t) (p - buf));
  73985. return;
  73986. }
  73987. /*
  73988. * Dragon4 setup.
  73989. *
  73990. * Convert double from IEEE representation for conversion;
  73991. * normal finite values have an implicit leading 1-bit. The
  73992. * slow path algorithm doesn't handle zero, so zero is special
  73993. * cased here but still creates a valid nc_ctx, and goes
  73994. * through normal formatting in case special formatting has
  73995. * been requested (e.g. forced exponential format: 0 -> "0e+0").
  73996. */
  73997. /* Would be nice to bulk clear the allocation, but the context
  73998. * is 1-2 kilobytes and nothing should rely on it being zeroed.
  73999. */
  74000. #if 0
  74001. duk_memzero((void *) nc_ctx, sizeof(*nc_ctx)); /* slow init, do only for slow path cases */
  74002. #endif
  74003. nc_ctx->is_s2n = 0;
  74004. nc_ctx->b = 2;
  74005. nc_ctx->B = radix;
  74006. nc_ctx->abs_pos = 0;
  74007. if (flags & DUK_N2S_FLAG_FIXED_FORMAT) {
  74008. nc_ctx->is_fixed = 1;
  74009. if (flags & DUK_N2S_FLAG_FRACTION_DIGITS) {
  74010. /* absolute req_digits; e.g. digits = 1 -> last digit is 0,
  74011. * but add an extra digit for rounding.
  74012. */
  74013. nc_ctx->abs_pos = 1;
  74014. nc_ctx->req_digits = (-digits + 1) - 1;
  74015. } else {
  74016. nc_ctx->req_digits = digits + 1;
  74017. }
  74018. } else {
  74019. nc_ctx->is_fixed = 0;
  74020. nc_ctx->req_digits = 0;
  74021. }
  74022. if (c == DUK_FP_ZERO) {
  74023. /* Zero special case: fake requested number of zero digits; ensure
  74024. * no sign bit is printed. Relative and absolute fixed format
  74025. * require separate handling.
  74026. */
  74027. duk_small_int_t count;
  74028. if (nc_ctx->is_fixed) {
  74029. if (nc_ctx->abs_pos) {
  74030. count = digits + 2; /* lead zero + 'digits' fractions + 1 for rounding */
  74031. } else {
  74032. count = digits + 1; /* + 1 for rounding */
  74033. }
  74034. } else {
  74035. count = 1;
  74036. }
  74037. DUK_DDD(DUK_DDDPRINT("count=%ld", (long) count));
  74038. DUK_ASSERT(count >= 1);
  74039. duk_memzero((void *) nc_ctx->digits, (size_t) count);
  74040. nc_ctx->count = count;
  74041. nc_ctx->k = 1; /* 0.000... */
  74042. neg = 0;
  74043. goto zero_skip;
  74044. }
  74045. duk__dragon4_double_to_ctx(nc_ctx, x); /* -> sets 'f' and 'e' */
  74046. DUK__BI_PRINT("f", &nc_ctx->f);
  74047. DUK_DDD(DUK_DDDPRINT("e=%ld", (long) nc_ctx->e));
  74048. /*
  74049. * Dragon4 slow path digit generation.
  74050. */
  74051. duk__dragon4_prepare(nc_ctx); /* setup many variables in nc_ctx */
  74052. DUK_DDD(DUK_DDDPRINT("after prepare:"));
  74053. DUK__BI_PRINT("r", &nc_ctx->r);
  74054. DUK__BI_PRINT("s", &nc_ctx->s);
  74055. DUK__BI_PRINT("mp", &nc_ctx->mp);
  74056. DUK__BI_PRINT("mm", &nc_ctx->mm);
  74057. duk__dragon4_scale(nc_ctx);
  74058. DUK_DDD(DUK_DDDPRINT("after scale; k=%ld", (long) nc_ctx->k));
  74059. DUK__BI_PRINT("r", &nc_ctx->r);
  74060. DUK__BI_PRINT("s", &nc_ctx->s);
  74061. DUK__BI_PRINT("mp", &nc_ctx->mp);
  74062. DUK__BI_PRINT("mm", &nc_ctx->mm);
  74063. duk__dragon4_generate(nc_ctx);
  74064. /*
  74065. * Convert and push final string.
  74066. */
  74067. zero_skip:
  74068. if (flags & DUK_N2S_FLAG_FIXED_FORMAT) {
  74069. /* Perform fixed-format rounding. */
  74070. duk_small_int_t roundpos;
  74071. if (flags & DUK_N2S_FLAG_FRACTION_DIGITS) {
  74072. /* 'roundpos' is relative to nc_ctx->k and increases to the right
  74073. * (opposite of how 'k' changes).
  74074. */
  74075. roundpos = -digits; /* absolute position for digit considered for rounding */
  74076. roundpos = nc_ctx->k - roundpos;
  74077. } else {
  74078. roundpos = digits;
  74079. }
  74080. DUK_DDD(DUK_DDDPRINT("rounding: k=%ld, count=%ld, digits=%ld, roundpos=%ld",
  74081. (long) nc_ctx->k, (long) nc_ctx->count, (long) digits, (long) roundpos));
  74082. (void) duk__dragon4_fixed_format_round(nc_ctx, roundpos);
  74083. /* Note: 'count' is currently not adjusted by rounding (i.e. the
  74084. * digits are not "chopped off". That shouldn't matter because
  74085. * the digit position (absolute or relative) is passed on to the
  74086. * convert-and-push function.
  74087. */
  74088. }
  74089. duk__dragon4_convert_and_push(nc_ctx, thr, radix, digits, flags, neg);
  74090. }
  74091. /*
  74092. * Exposed string-to-number API
  74093. *
  74094. * Input: [ string ]
  74095. * Output: [ number ]
  74096. *
  74097. * If number parsing fails, a NaN is pushed as the result. If number parsing
  74098. * fails due to an internal error, an InternalError is thrown.
  74099. */
  74100. DUK_INTERNAL void duk_numconv_parse(duk_hthread *thr, duk_small_int_t radix, duk_small_uint_t flags) {
  74101. duk__numconv_stringify_ctx nc_ctx_alloc; /* large context; around 2kB now */
  74102. duk__numconv_stringify_ctx *nc_ctx = &nc_ctx_alloc;
  74103. duk_double_t res;
  74104. duk_hstring *h_str;
  74105. duk_int_t expt;
  74106. duk_bool_t expt_neg;
  74107. duk_small_int_t expt_adj;
  74108. duk_small_int_t neg;
  74109. duk_small_int_t dig;
  74110. duk_small_int_t dig_whole;
  74111. duk_small_int_t dig_lzero;
  74112. duk_small_int_t dig_frac;
  74113. duk_small_int_t dig_expt;
  74114. duk_small_int_t dig_prec;
  74115. const duk__exp_limits *explim;
  74116. const duk_uint8_t *p;
  74117. duk_small_int_t ch;
  74118. DUK_DDD(DUK_DDDPRINT("parse number: %!T, radix=%ld, flags=0x%08lx",
  74119. (duk_tval *) duk_get_tval(thr, -1),
  74120. (long) radix, (unsigned long) flags));
  74121. DUK_ASSERT(radix >= 2 && radix <= 36);
  74122. DUK_ASSERT(radix - 2 < (duk_small_int_t) sizeof(duk__str2num_digits_for_radix));
  74123. /*
  74124. * Preliminaries: trim, sign, Infinity check
  74125. *
  74126. * We rely on the interned string having a NUL terminator, which will
  74127. * cause a parse failure wherever it is encountered. As a result, we
  74128. * don't need separate pointer checks.
  74129. *
  74130. * There is no special parsing for 'NaN' in the specification although
  74131. * 'Infinity' (with an optional sign) is allowed in some contexts.
  74132. * Some contexts allow plus/minus sign, while others only allow the
  74133. * minus sign (like JSON.parse()).
  74134. *
  74135. * Automatic hex number detection (leading '0x' or '0X') and octal
  74136. * number detection (leading '0' followed by at least one octal digit)
  74137. * is done here too.
  74138. *
  74139. * Symbols are not explicitly rejected here (that's up to the caller).
  74140. * If a symbol were passed here, it should ultimately safely fail
  74141. * parsing due to a syntax error.
  74142. */
  74143. if (flags & DUK_S2N_FLAG_TRIM_WHITE) {
  74144. /* Leading / trailing whitespace is sometimes accepted and
  74145. * sometimes not. After white space trimming, all valid input
  74146. * characters are pure ASCII.
  74147. */
  74148. duk_trim(thr, -1);
  74149. }
  74150. h_str = duk_require_hstring(thr, -1);
  74151. DUK_ASSERT(h_str != NULL);
  74152. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_str);
  74153. neg = 0;
  74154. ch = *p;
  74155. if (ch == (duk_small_int_t) '+') {
  74156. if ((flags & DUK_S2N_FLAG_ALLOW_PLUS) == 0) {
  74157. DUK_DDD(DUK_DDDPRINT("parse failed: leading plus sign not allowed"));
  74158. goto parse_fail;
  74159. }
  74160. p++;
  74161. } else if (ch == (duk_small_int_t) '-') {
  74162. if ((flags & DUK_S2N_FLAG_ALLOW_MINUS) == 0) {
  74163. DUK_DDD(DUK_DDDPRINT("parse failed: leading minus sign not allowed"));
  74164. goto parse_fail;
  74165. }
  74166. p++;
  74167. neg = 1;
  74168. }
  74169. if ((flags & DUK_S2N_FLAG_ALLOW_INF) && DUK_STRNCMP((const char *) p, "Infinity", 8) == 0) {
  74170. /* Don't check for Infinity unless the context allows it.
  74171. * 'Infinity' is a valid integer literal in e.g. base-36:
  74172. *
  74173. * parseInt('Infinity', 36)
  74174. * 1461559270678
  74175. */
  74176. if ((flags & DUK_S2N_FLAG_ALLOW_GARBAGE) == 0 && p[8] != DUK_ASC_NUL) {
  74177. DUK_DDD(DUK_DDDPRINT("parse failed: trailing garbage after matching 'Infinity' not allowed"));
  74178. goto parse_fail;
  74179. } else {
  74180. res = DUK_DOUBLE_INFINITY;
  74181. goto negcheck_and_ret;
  74182. }
  74183. }
  74184. ch = *p;
  74185. if (ch == (duk_small_int_t) '0') {
  74186. duk_small_int_t detect_radix = 0;
  74187. ch = DUK_LOWERCASE_CHAR_ASCII(p[1]); /* 'x' or 'X' -> 'x' */
  74188. if ((flags & DUK_S2N_FLAG_ALLOW_AUTO_HEX_INT) && ch == DUK_ASC_LC_X) {
  74189. DUK_DDD(DUK_DDDPRINT("detected 0x/0X hex prefix, changing radix and preventing fractions and exponent"));
  74190. detect_radix = 16;
  74191. #if 0
  74192. } else if ((flags & DUK_S2N_FLAG_ALLOW_AUTO_LEGACY_OCT_INT) &&
  74193. (ch >= (duk_small_int_t) '0' && ch <= (duk_small_int_t) '9')) {
  74194. DUK_DDD(DUK_DDDPRINT("detected 0n oct prefix, changing radix and preventing fractions and exponent"));
  74195. detect_radix = 8;
  74196. /* NOTE: if this legacy octal case is added back, it has
  74197. * different flags and 'p' advance so this needs to be
  74198. * reworked.
  74199. */
  74200. flags |= DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO; /* interpret e.g. '09' as '0', not NaN */
  74201. p += 1;
  74202. #endif
  74203. } else if ((flags & DUK_S2N_FLAG_ALLOW_AUTO_OCT_INT) && ch == DUK_ASC_LC_O) {
  74204. DUK_DDD(DUK_DDDPRINT("detected 0o oct prefix, changing radix and preventing fractions and exponent"));
  74205. detect_radix = 8;
  74206. } else if ((flags & DUK_S2N_FLAG_ALLOW_AUTO_BIN_INT) && ch == DUK_ASC_LC_B) {
  74207. DUK_DDD(DUK_DDDPRINT("detected 0b bin prefix, changing radix and preventing fractions and exponent"));
  74208. detect_radix = 2;
  74209. }
  74210. if (detect_radix > 0) {
  74211. radix = detect_radix;
  74212. /* Clear empty as zero flag: interpret e.g. '0x' and '0xg' as a NaN (= parse error) */
  74213. flags &= ~(DUK_S2N_FLAG_ALLOW_EXP | DUK_S2N_FLAG_ALLOW_EMPTY_FRAC |
  74214. DUK_S2N_FLAG_ALLOW_FRAC | DUK_S2N_FLAG_ALLOW_NAKED_FRAC |
  74215. DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO);
  74216. flags |= DUK_S2N_FLAG_ALLOW_LEADING_ZERO; /* allow e.g. '0x0009' and '0b00010001' */
  74217. p += 2;
  74218. }
  74219. }
  74220. /*
  74221. * Scan number and setup for Dragon4.
  74222. *
  74223. * The fast path case is detected during setup: an integer which
  74224. * can be converted without rounding, no net exponent. The fast
  74225. * path could be implemented as a separate scan, but may not really
  74226. * be worth it: the multiplications for building 'f' are not
  74227. * expensive when 'f' is small.
  74228. *
  74229. * The significand ('f') must contain enough bits of (apparent)
  74230. * accuracy, so that Dragon4 will generate enough binary output digits.
  74231. * For decimal numbers, this means generating a 20-digit significand,
  74232. * which should yield enough practical accuracy to parse IEEE doubles.
  74233. * In fact, the ECMAScript specification explicitly allows an
  74234. * implementation to treat digits beyond 20 as zeroes (and even
  74235. * to round the 20th digit upwards). For non-decimal numbers, the
  74236. * appropriate number of digits has been precomputed for comparable
  74237. * accuracy.
  74238. *
  74239. * Digit counts:
  74240. *
  74241. * [ dig_lzero ]
  74242. * |
  74243. * .+-..---[ dig_prec ]----.
  74244. * | || |
  74245. * 0000123.456789012345678901234567890e+123456
  74246. * | | | | | |
  74247. * `--+--' `------[ dig_frac ]-------' `-+--'
  74248. * | |
  74249. * [ dig_whole ] [ dig_expt ]
  74250. *
  74251. * dig_frac and dig_expt are -1 if not present
  74252. * dig_lzero is only computed for whole number part
  74253. *
  74254. * Parsing state
  74255. *
  74256. * Parsing whole part dig_frac < 0 AND dig_expt < 0
  74257. * Parsing fraction part dig_frac >= 0 AND dig_expt < 0
  74258. * Parsing exponent part dig_expt >= 0 (dig_frac may be < 0 or >= 0)
  74259. *
  74260. * Note: in case we hit an implementation limit (like exponent range),
  74261. * we should throw an error, NOT return NaN or Infinity. Even with
  74262. * very large exponent (or significand) values the final result may be
  74263. * finite, so NaN/Infinity would be incorrect.
  74264. */
  74265. duk__bi_set_small(&nc_ctx->f, 0);
  74266. dig_prec = 0;
  74267. dig_lzero = 0;
  74268. dig_whole = 0;
  74269. dig_frac = -1;
  74270. dig_expt = -1;
  74271. expt = 0;
  74272. expt_adj = 0; /* essentially tracks digit position of lowest 'f' digit */
  74273. expt_neg = 0;
  74274. for (;;) {
  74275. ch = *p++;
  74276. DUK_DDD(DUK_DDDPRINT("parse digits: p=%p, ch='%c' (%ld), expt=%ld, expt_adj=%ld, "
  74277. "dig_whole=%ld, dig_frac=%ld, dig_expt=%ld, dig_lzero=%ld, dig_prec=%ld",
  74278. (const void *) p, (int) ((ch >= 0x20 && ch <= 0x7e) ? ch : '?'), (long) ch,
  74279. (long) expt, (long) expt_adj, (long) dig_whole, (long) dig_frac,
  74280. (long) dig_expt, (long) dig_lzero, (long) dig_prec));
  74281. DUK__BI_PRINT("f", &nc_ctx->f);
  74282. /* Most common cases first. */
  74283. if (ch >= (duk_small_int_t) '0' && ch <= (duk_small_int_t) '9') {
  74284. dig = (duk_small_int_t) ch - '0' + 0;
  74285. } else if (ch == (duk_small_int_t) '.') {
  74286. /* A leading digit is not required in some cases, e.g. accept ".123".
  74287. * In other cases (JSON.parse()) a leading digit is required. This
  74288. * is checked for after the loop.
  74289. */
  74290. if (dig_frac >= 0 || dig_expt >= 0) {
  74291. if (flags & DUK_S2N_FLAG_ALLOW_GARBAGE) {
  74292. DUK_DDD(DUK_DDDPRINT("garbage termination (invalid period)"));
  74293. break;
  74294. } else {
  74295. DUK_DDD(DUK_DDDPRINT("parse failed: period not allowed"));
  74296. goto parse_fail;
  74297. }
  74298. }
  74299. if ((flags & DUK_S2N_FLAG_ALLOW_FRAC) == 0) {
  74300. /* Some contexts don't allow fractions at all; this can't be a
  74301. * post-check because the state ('f' and expt) would be incorrect.
  74302. */
  74303. if (flags & DUK_S2N_FLAG_ALLOW_GARBAGE) {
  74304. DUK_DDD(DUK_DDDPRINT("garbage termination (invalid first period)"));
  74305. break;
  74306. } else {
  74307. DUK_DDD(DUK_DDDPRINT("parse failed: fraction part not allowed"));
  74308. }
  74309. }
  74310. DUK_DDD(DUK_DDDPRINT("start fraction part"));
  74311. dig_frac = 0;
  74312. continue;
  74313. } else if (ch == (duk_small_int_t) 0) {
  74314. DUK_DDD(DUK_DDDPRINT("NUL termination"));
  74315. break;
  74316. } else if ((flags & DUK_S2N_FLAG_ALLOW_EXP) &&
  74317. dig_expt < 0 && (ch == (duk_small_int_t) 'e' || ch == (duk_small_int_t) 'E')) {
  74318. /* Note: we don't parse back exponent notation for anything else
  74319. * than radix 10, so this is not an ambiguous check (e.g. hex
  74320. * exponent values may have 'e' either as a significand digit
  74321. * or as an exponent separator).
  74322. *
  74323. * If the exponent separator occurs twice, 'e' will be interpreted
  74324. * as a digit (= 14) and will be rejected as an invalid decimal
  74325. * digit.
  74326. */
  74327. DUK_DDD(DUK_DDDPRINT("start exponent part"));
  74328. /* Exponent without a sign or with a +/- sign is accepted
  74329. * by all call sites (even JSON.parse()).
  74330. */
  74331. ch = *p;
  74332. if (ch == (duk_small_int_t) '-') {
  74333. expt_neg = 1;
  74334. p++;
  74335. } else if (ch == (duk_small_int_t) '+') {
  74336. p++;
  74337. }
  74338. dig_expt = 0;
  74339. continue;
  74340. } else if (ch >= (duk_small_int_t) 'a' && ch <= (duk_small_int_t) 'z') {
  74341. dig = (duk_small_int_t) (ch - (duk_small_int_t) 'a' + 0x0a);
  74342. } else if (ch >= (duk_small_int_t) 'A' && ch <= (duk_small_int_t) 'Z') {
  74343. dig = (duk_small_int_t) (ch - (duk_small_int_t) 'A' + 0x0a);
  74344. } else {
  74345. dig = 255; /* triggers garbage digit check below */
  74346. }
  74347. DUK_ASSERT((dig >= 0 && dig <= 35) || dig == 255);
  74348. if (dig >= radix) {
  74349. if (flags & DUK_S2N_FLAG_ALLOW_GARBAGE) {
  74350. DUK_DDD(DUK_DDDPRINT("garbage termination"));
  74351. break;
  74352. } else {
  74353. DUK_DDD(DUK_DDDPRINT("parse failed: trailing garbage or invalid digit"));
  74354. goto parse_fail;
  74355. }
  74356. }
  74357. if (dig_expt < 0) {
  74358. /* whole or fraction digit */
  74359. if (dig_prec < duk__str2num_digits_for_radix[radix - 2]) {
  74360. /* significant from precision perspective */
  74361. duk_small_int_t f_zero = duk__bi_is_zero(&nc_ctx->f);
  74362. if (f_zero && dig == 0) {
  74363. /* Leading zero is not counted towards precision digits; not
  74364. * in the integer part, nor in the fraction part.
  74365. */
  74366. if (dig_frac < 0) {
  74367. dig_lzero++;
  74368. }
  74369. } else {
  74370. /* XXX: join these ops (multiply-accumulate), but only if
  74371. * code footprint decreases.
  74372. */
  74373. duk__bi_mul_small(&nc_ctx->t1, &nc_ctx->f, (duk_uint32_t) radix);
  74374. duk__bi_add_small(&nc_ctx->f, &nc_ctx->t1, (duk_uint32_t) dig);
  74375. dig_prec++;
  74376. }
  74377. } else {
  74378. /* Ignore digits beyond a radix-specific limit, but note them
  74379. * in expt_adj.
  74380. */
  74381. expt_adj++;
  74382. }
  74383. if (dig_frac >= 0) {
  74384. dig_frac++;
  74385. expt_adj--;
  74386. } else {
  74387. dig_whole++;
  74388. }
  74389. } else {
  74390. /* exponent digit */
  74391. DUK_ASSERT(radix == 10);
  74392. expt = expt * radix + dig;
  74393. if (expt > DUK_S2N_MAX_EXPONENT) {
  74394. /* Impose a reasonable exponent limit, so that exp
  74395. * doesn't need to get tracked using a bigint.
  74396. */
  74397. DUK_DDD(DUK_DDDPRINT("parse failed: exponent too large"));
  74398. goto parse_explimit_error;
  74399. }
  74400. dig_expt++;
  74401. }
  74402. }
  74403. /* Leading zero. */
  74404. if (dig_lzero > 0 && dig_whole > 1) {
  74405. if ((flags & DUK_S2N_FLAG_ALLOW_LEADING_ZERO) == 0) {
  74406. DUK_DDD(DUK_DDDPRINT("parse failed: leading zeroes not allowed in integer part"));
  74407. goto parse_fail;
  74408. }
  74409. }
  74410. /* Validity checks for various fraction formats ("0.1", ".1", "1.", "."). */
  74411. if (dig_whole == 0) {
  74412. if (dig_frac == 0) {
  74413. /* "." is not accepted in any format */
  74414. DUK_DDD(DUK_DDDPRINT("parse failed: plain period without leading or trailing digits"));
  74415. goto parse_fail;
  74416. } else if (dig_frac > 0) {
  74417. /* ".123" */
  74418. if ((flags & DUK_S2N_FLAG_ALLOW_NAKED_FRAC) == 0) {
  74419. DUK_DDD(DUK_DDDPRINT("parse failed: fraction part not allowed without "
  74420. "leading integer digit(s)"));
  74421. goto parse_fail;
  74422. }
  74423. } else {
  74424. /* empty ("") is allowed in some formats (e.g. Number(''), as zero */
  74425. if ((flags & DUK_S2N_FLAG_ALLOW_EMPTY_AS_ZERO) == 0) {
  74426. DUK_DDD(DUK_DDDPRINT("parse failed: empty string not allowed (as zero)"));
  74427. goto parse_fail;
  74428. }
  74429. }
  74430. } else {
  74431. if (dig_frac == 0) {
  74432. /* "123." is allowed in some formats */
  74433. if ((flags & DUK_S2N_FLAG_ALLOW_EMPTY_FRAC) == 0) {
  74434. DUK_DDD(DUK_DDDPRINT("parse failed: empty fractions"));
  74435. goto parse_fail;
  74436. }
  74437. } else if (dig_frac > 0) {
  74438. /* "123.456" */
  74439. ;
  74440. } else {
  74441. /* "123" */
  74442. ;
  74443. }
  74444. }
  74445. /* Exponent without digits (e.g. "1e" or "1e+"). If trailing garbage is
  74446. * allowed, ignore exponent part as garbage (= parse as "1", i.e. exp 0).
  74447. */
  74448. if (dig_expt == 0) {
  74449. if ((flags & DUK_S2N_FLAG_ALLOW_GARBAGE) == 0) {
  74450. DUK_DDD(DUK_DDDPRINT("parse failed: empty exponent"));
  74451. goto parse_fail;
  74452. }
  74453. DUK_ASSERT(expt == 0);
  74454. }
  74455. if (expt_neg) {
  74456. expt = -expt;
  74457. }
  74458. DUK_DDD(DUK_DDDPRINT("expt=%ld, expt_adj=%ld, net exponent -> %ld",
  74459. (long) expt, (long) expt_adj, (long) (expt + expt_adj)));
  74460. expt += expt_adj;
  74461. /* Fast path check. */
  74462. if (nc_ctx->f.n <= 1 && /* 32-bit value */
  74463. expt == 0 /* no net exponent */) {
  74464. /* Fast path is triggered for no exponent and also for balanced exponent
  74465. * and fraction parts, e.g. for "1.23e2" == "123". Remember to respect
  74466. * zero sign.
  74467. */
  74468. /* XXX: could accept numbers larger than 32 bits, e.g. up to 53 bits? */
  74469. DUK_DDD(DUK_DDDPRINT("fast path number parse"));
  74470. if (nc_ctx->f.n == 1) {
  74471. res = (double) nc_ctx->f.v[0];
  74472. } else {
  74473. res = 0.0;
  74474. }
  74475. goto negcheck_and_ret;
  74476. }
  74477. /* Significand ('f') padding. */
  74478. while (dig_prec < duk__str2num_digits_for_radix[radix - 2]) {
  74479. /* Pad significand with "virtual" zero digits so that Dragon4 will
  74480. * have enough (apparent) precision to work with.
  74481. */
  74482. DUK_DDD(DUK_DDDPRINT("dig_prec=%ld, pad significand with zero", (long) dig_prec));
  74483. duk__bi_mul_small_copy(&nc_ctx->f, (duk_uint32_t) radix, &nc_ctx->t1);
  74484. DUK__BI_PRINT("f", &nc_ctx->f);
  74485. expt--;
  74486. dig_prec++;
  74487. }
  74488. DUK_DDD(DUK_DDDPRINT("final exponent: %ld", (long) expt));
  74489. /* Detect zero special case. */
  74490. if (nc_ctx->f.n == 0) {
  74491. /* This may happen even after the fast path check, if exponent is
  74492. * not balanced (e.g. "0e1"). Remember to respect zero sign.
  74493. */
  74494. DUK_DDD(DUK_DDDPRINT("significand is zero"));
  74495. res = 0.0;
  74496. goto negcheck_and_ret;
  74497. }
  74498. /* Quick reject of too large or too small exponents. This check
  74499. * would be incorrect for zero (e.g. "0e1000" is zero, not Infinity)
  74500. * so zero check must be above.
  74501. */
  74502. explim = &duk__str2num_exp_limits[radix - 2];
  74503. if (expt > explim->upper) {
  74504. DUK_DDD(DUK_DDDPRINT("exponent too large -> infinite"));
  74505. res = (duk_double_t) DUK_DOUBLE_INFINITY;
  74506. goto negcheck_and_ret;
  74507. } else if (expt < explim->lower) {
  74508. DUK_DDD(DUK_DDDPRINT("exponent too small -> zero"));
  74509. res = (duk_double_t) 0.0;
  74510. goto negcheck_and_ret;
  74511. }
  74512. nc_ctx->is_s2n = 1;
  74513. nc_ctx->e = expt;
  74514. nc_ctx->b = radix;
  74515. nc_ctx->B = 2;
  74516. nc_ctx->is_fixed = 1;
  74517. nc_ctx->abs_pos = 0;
  74518. nc_ctx->req_digits = 53 + 1;
  74519. DUK__BI_PRINT("f", &nc_ctx->f);
  74520. DUK_DDD(DUK_DDDPRINT("e=%ld", (long) nc_ctx->e));
  74521. /*
  74522. * Dragon4 slow path (binary) digit generation.
  74523. * An extra digit is generated for rounding.
  74524. */
  74525. duk__dragon4_prepare(nc_ctx); /* setup many variables in nc_ctx */
  74526. DUK_DDD(DUK_DDDPRINT("after prepare:"));
  74527. DUK__BI_PRINT("r", &nc_ctx->r);
  74528. DUK__BI_PRINT("s", &nc_ctx->s);
  74529. DUK__BI_PRINT("mp", &nc_ctx->mp);
  74530. DUK__BI_PRINT("mm", &nc_ctx->mm);
  74531. duk__dragon4_scale(nc_ctx);
  74532. DUK_DDD(DUK_DDDPRINT("after scale; k=%ld", (long) nc_ctx->k));
  74533. DUK__BI_PRINT("r", &nc_ctx->r);
  74534. DUK__BI_PRINT("s", &nc_ctx->s);
  74535. DUK__BI_PRINT("mp", &nc_ctx->mp);
  74536. DUK__BI_PRINT("mm", &nc_ctx->mm);
  74537. duk__dragon4_generate(nc_ctx);
  74538. DUK_ASSERT(nc_ctx->count == 53 + 1);
  74539. /*
  74540. * Convert binary digits into an IEEE double. Need to handle
  74541. * denormals and rounding correctly.
  74542. *
  74543. * Some call sites currently assume the result is always a
  74544. * non-fastint double. If this is changed, check all call
  74545. * sites.
  74546. */
  74547. duk__dragon4_ctx_to_double(nc_ctx, &res);
  74548. goto negcheck_and_ret;
  74549. negcheck_and_ret:
  74550. if (neg) {
  74551. res = -res;
  74552. }
  74553. duk_pop(thr);
  74554. duk_push_number(thr, (double) res);
  74555. DUK_DDD(DUK_DDDPRINT("result: %!T", (duk_tval *) duk_get_tval(thr, -1)));
  74556. return;
  74557. parse_fail:
  74558. DUK_DDD(DUK_DDDPRINT("parse failed"));
  74559. duk_pop(thr);
  74560. duk_push_nan(thr);
  74561. return;
  74562. parse_explimit_error:
  74563. DUK_DDD(DUK_DDDPRINT("parse failed, internal error, can't return a value"));
  74564. DUK_ERROR_RANGE(thr, "exponent too large");
  74565. DUK_WO_NORETURN(return;);
  74566. }
  74567. /* automatic undefs */
  74568. #undef DUK__BI_MAX_PARTS
  74569. #undef DUK__BI_PRINT
  74570. #undef DUK__DIGITCHAR
  74571. #undef DUK__DRAGON4_OUTPUT_PREINC
  74572. #undef DUK__IEEE_DOUBLE_EXP_BIAS
  74573. #undef DUK__IEEE_DOUBLE_EXP_MIN
  74574. #undef DUK__MAX_FORMATTED_LENGTH
  74575. #undef DUK__MAX_OUTPUT_DIGITS
  74576. #undef DUK__NO_EXP
  74577. #undef DUK__NUMCONV_CTX_BIGINTS_SIZE
  74578. #undef DUK__NUMCONV_CTX_NUM_BIGINTS
  74579. #line 1 "duk_regexp_compiler.c"
  74580. /*
  74581. * Regexp compilation.
  74582. *
  74583. * See doc/regexp.rst for a discussion of the compilation approach and
  74584. * current limitations.
  74585. *
  74586. * Regexp bytecode assumes jumps can be expressed with signed 32-bit
  74587. * integers. Consequently the bytecode size must not exceed 0x7fffffffL.
  74588. * The implementation casts duk_size_t (buffer size) to duk_(u)int32_t
  74589. * in many places. Although this could be changed, the bytecode format
  74590. * limit would still prevent regexps exceeding the signed 32-bit limit
  74591. * from working.
  74592. *
  74593. * XXX: The implementation does not prevent bytecode from exceeding the
  74594. * maximum supported size. This could be done by limiting the maximum
  74595. * input string size (assuming an upper bound can be computed for number
  74596. * of bytecode bytes emitted per input byte) or checking buffer maximum
  74597. * size when emitting bytecode (slower).
  74598. */
  74599. /* #include duk_internal.h -> already included */
  74600. #if defined(DUK_USE_REGEXP_SUPPORT)
  74601. /*
  74602. * Helper macros
  74603. */
  74604. #define DUK__RE_INITIAL_BUFSIZE 64
  74605. #define DUK__RE_BUFLEN(re_ctx) \
  74606. DUK_BW_GET_SIZE(re_ctx->thr, &re_ctx->bw)
  74607. /*
  74608. * Disjunction struct: result of parsing a disjunction
  74609. */
  74610. typedef struct {
  74611. /* Number of characters that the atom matches (e.g. 3 for 'abc'),
  74612. * -1 if atom is complex and number of matched characters either
  74613. * varies or is not known.
  74614. */
  74615. duk_int32_t charlen;
  74616. #if 0
  74617. /* These are not needed to implement quantifier capture handling,
  74618. * but might be needed at some point.
  74619. */
  74620. /* re_ctx->captures at start and end of atom parsing.
  74621. * Since 'captures' indicates highest capture number emitted
  74622. * so far in a DUK_REOP_SAVE, the captures numbers saved by
  74623. * the atom are: ]start_captures,end_captures].
  74624. */
  74625. duk_uint32_t start_captures;
  74626. duk_uint32_t end_captures;
  74627. #endif
  74628. } duk__re_disjunction_info;
  74629. /*
  74630. * Encoding helpers
  74631. *
  74632. * Some of the typing is bytecode based, e.g. slice sizes are unsigned 32-bit
  74633. * even though the buffer operations will use duk_size_t.
  74634. */
  74635. /* XXX: the insert helpers should ensure that the bytecode result is not
  74636. * larger than expected (or at least assert for it). Many things in the
  74637. * bytecode, like skip offsets, won't work correctly if the bytecode is
  74638. * larger than say 2G.
  74639. */
  74640. DUK_LOCAL duk_uint32_t duk__encode_i32(duk_int32_t x) {
  74641. if (x < 0) {
  74642. return ((duk_uint32_t) (-x)) * 2 + 1;
  74643. } else {
  74644. return ((duk_uint32_t) x) * 2;
  74645. }
  74646. }
  74647. /* XXX: return type should probably be duk_size_t, or explicit checks are needed for
  74648. * maximum size.
  74649. */
  74650. DUK_LOCAL duk_uint32_t duk__insert_u32(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_uint32_t x) {
  74651. duk_uint8_t buf[DUK_UNICODE_MAX_XUTF8_LENGTH];
  74652. duk_small_int_t len;
  74653. len = duk_unicode_encode_xutf8((duk_ucodepoint_t) x, buf);
  74654. DUK_ASSERT(len >= 0);
  74655. DUK_BW_INSERT_ENSURE_BYTES(re_ctx->thr, &re_ctx->bw, offset, buf, (duk_size_t) len);
  74656. return (duk_uint32_t) len;
  74657. }
  74658. DUK_LOCAL void duk__append_u32(duk_re_compiler_ctx *re_ctx, duk_uint32_t x) {
  74659. DUK_BW_WRITE_ENSURE_XUTF8(re_ctx->thr, &re_ctx->bw, x);
  74660. }
  74661. DUK_LOCAL void duk__append_7bit(duk_re_compiler_ctx *re_ctx, duk_uint32_t x) {
  74662. #if defined(DUK_USE_PREFER_SIZE)
  74663. duk__append_u32(re_ctx, x);
  74664. #else
  74665. DUK_ASSERT(x <= 0x7fU);
  74666. DUK_BW_WRITE_ENSURE_U8(re_ctx->thr, &re_ctx->bw, (duk_uint8_t) x);
  74667. #endif
  74668. }
  74669. #if 0
  74670. DUK_LOCAL void duk__append_2bytes(duk_re_compiler_ctx *re_ctx, duk_uint8_t x, duk_uint8_t y) {
  74671. DUK_BW_WRITE_ENSURE_U8_2(re_ctx->thr, &re_ctx->bw, x, y);
  74672. }
  74673. #endif
  74674. DUK_LOCAL duk_uint32_t duk__insert_i32(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_int32_t x) {
  74675. return duk__insert_u32(re_ctx, offset, duk__encode_i32(x));
  74676. }
  74677. DUK_LOCAL void duk__append_reop(duk_re_compiler_ctx *re_ctx, duk_uint32_t reop) {
  74678. DUK_ASSERT(reop <= 0x7fU);
  74679. (void) duk__append_7bit(re_ctx, reop);
  74680. }
  74681. #if 0 /* unused */
  74682. DUK_LOCAL void duk__append_i32(duk_re_compiler_ctx *re_ctx, duk_int32_t x) {
  74683. duk__append_u32(re_ctx, duk__encode_i32(x));
  74684. }
  74685. #endif
  74686. /* special helper for emitting u16 lists (used for character ranges for built-in char classes) */
  74687. DUK_LOCAL void duk__append_u16_list(duk_re_compiler_ctx *re_ctx, const duk_uint16_t *values, duk_uint32_t count) {
  74688. /* Call sites don't need the result length so it's not accumulated. */
  74689. while (count-- > 0) {
  74690. duk__append_u32(re_ctx, (duk_uint32_t) (*values++));
  74691. }
  74692. }
  74693. DUK_LOCAL void duk__insert_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_uint32_t data_offset, duk_uint32_t data_length) {
  74694. DUK_BW_INSERT_ENSURE_SLICE(re_ctx->thr, &re_ctx->bw, offset, data_offset, data_length);
  74695. }
  74696. DUK_LOCAL void duk__append_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t data_offset, duk_uint32_t data_length) {
  74697. DUK_BW_WRITE_ENSURE_SLICE(re_ctx->thr, &re_ctx->bw, data_offset, data_length);
  74698. }
  74699. DUK_LOCAL void duk__remove_slice(duk_re_compiler_ctx *re_ctx, duk_uint32_t data_offset, duk_uint32_t data_length) {
  74700. DUK_BW_REMOVE_ENSURE_SLICE(re_ctx->thr, &re_ctx->bw, data_offset, data_length);
  74701. }
  74702. /*
  74703. * Insert a jump offset at 'offset' to complete an instruction
  74704. * (the jump offset is always the last component of an instruction).
  74705. * The 'skip' argument must be computed relative to 'offset',
  74706. * -without- taking into account the skip field being inserted.
  74707. *
  74708. * ... A B C ins X Y Z ... (ins may be a JUMP, SPLIT1/SPLIT2, etc)
  74709. * => ... A B C ins SKIP X Y Z
  74710. *
  74711. * Computing the final (adjusted) skip value, which is relative to the
  74712. * first byte of the next instruction, is a bit tricky because of the
  74713. * variable length UTF-8 encoding. See doc/regexp.rst for discussion.
  74714. */
  74715. DUK_LOCAL duk_uint32_t duk__insert_jump_offset(duk_re_compiler_ctx *re_ctx, duk_uint32_t offset, duk_int32_t skip) {
  74716. #if 0
  74717. /* Iterative solution. */
  74718. if (skip < 0) {
  74719. duk_small_int_t len;
  74720. /* two encoding attempts suffices */
  74721. len = duk_unicode_get_xutf8_length((duk_codepoint_t) duk__encode_i32(skip));
  74722. len = duk_unicode_get_xutf8_length((duk_codepoint_t) duk__encode_i32(skip - (duk_int32_t) len));
  74723. DUK_ASSERT(duk_unicode_get_xutf8_length(duk__encode_i32(skip - (duk_int32_t) len)) == len); /* no change */
  74724. skip -= (duk_int32_t) len;
  74725. }
  74726. #endif
  74727. #if defined(DUK_USE_PREFER_SIZE)
  74728. /* Closed form solution, this produces smallest code.
  74729. * See re_neg_jump_offset (closed2).
  74730. */
  74731. if (skip < 0) {
  74732. skip--;
  74733. if (skip < -0x3fL) {
  74734. skip--;
  74735. }
  74736. if (skip < -0x3ffL) {
  74737. skip--;
  74738. }
  74739. if (skip < -0x7fffL) {
  74740. skip--;
  74741. }
  74742. if (skip < -0xfffffL) {
  74743. skip--;
  74744. }
  74745. if (skip < -0x1ffffffL) {
  74746. skip--;
  74747. }
  74748. if (skip < -0x3fffffffL) {
  74749. skip--;
  74750. }
  74751. }
  74752. #else /* DUK_USE_PREFER_SIZE */
  74753. /* Closed form solution, this produces fastest code.
  74754. * See re_neg_jump_offset (closed1).
  74755. */
  74756. if (skip < 0) {
  74757. if (skip >= -0x3eL) {
  74758. skip -= 1;
  74759. } else if (skip >= -0x3fdL) {
  74760. skip -= 2;
  74761. } else if (skip >= -0x7ffcL) {
  74762. skip -= 3;
  74763. } else if (skip >= -0xffffbL) {
  74764. skip -= 4;
  74765. } else if (skip >= -0x1fffffaL) {
  74766. skip -= 5;
  74767. } else if (skip >= -0x3ffffff9L) {
  74768. skip -= 6;
  74769. } else {
  74770. skip -= 7;
  74771. }
  74772. }
  74773. #endif /* DUK_USE_PREFER_SIZE */
  74774. return duk__insert_i32(re_ctx, offset, skip);
  74775. }
  74776. DUK_LOCAL duk_uint32_t duk__append_jump_offset(duk_re_compiler_ctx *re_ctx, duk_int32_t skip) {
  74777. return (duk_uint32_t) duk__insert_jump_offset(re_ctx, (duk_uint32_t) DUK__RE_BUFLEN(re_ctx), skip);
  74778. }
  74779. /*
  74780. * duk_re_range_callback for generating character class ranges.
  74781. *
  74782. * When ignoreCase is false, the range is simply emitted as is. We don't,
  74783. * for instance, eliminate duplicates or overlapping ranges in a character
  74784. * class.
  74785. *
  74786. * When ignoreCase is true but the 'direct' flag is set, the caller knows
  74787. * that the range canonicalizes to itself for case insensitive matching,
  74788. * so the range is emitted as is. This is mainly useful for built-in ranges
  74789. * like \W.
  74790. *
  74791. * Otherwise, when ignoreCase is true, the range needs to be normalized
  74792. * through canonicalization. Unfortunately a canonicalized version of a
  74793. * continuous range is not necessarily continuous (e.g. [x-{] is continuous
  74794. * but [X-{] is not). As a result, a single input range may expand to a lot
  74795. * of output ranges. The current algorithm creates the canonicalized ranges
  74796. * footprint efficiently at the cost of compile time execution time; see
  74797. * doc/regexp.rst for discussion, and some more details below.
  74798. *
  74799. * Note that the ctx->nranges is a context-wide temporary value. This is OK
  74800. * because there cannot be multiple character classes being parsed
  74801. * simultaneously.
  74802. *
  74803. * More detail on canonicalization:
  74804. *
  74805. * Conceptually, a range is canonicalized by scanning the entire range,
  74806. * normalizing each codepoint by converting it to uppercase, and generating
  74807. * a set of result ranges.
  74808. *
  74809. * Ideally a minimal set of output ranges would be emitted by merging all
  74810. * possible ranges even if they're emitted out of sequence. Because the
  74811. * input string is also case normalized during matching, some codepoints
  74812. * never occur at runtime; these "don't care" codepoints can be included or
  74813. * excluded from ranges when merging/optimizing ranges.
  74814. *
  74815. * The current algorithm does not do optimal range merging. Rather, output
  74816. * codepoints are generated in sequence, and when the output codepoints are
  74817. * continuous (CP, CP+1, CP+2, ...), they are merged locally into as large a
  74818. * range as possible. A small canonicalization bitmap is used to reduce
  74819. * actual codepoint canonicalizations which are quite slow at present. The
  74820. * bitmap provides a "codepoint block is continuous with respect to
  74821. * canonicalization" for N-codepoint blocks. This allows blocks to be
  74822. * skipped quickly.
  74823. *
  74824. * There are a number of shortcomings and future work here:
  74825. *
  74826. * - Individual codepoint normalizations are slow because they involve
  74827. * walking bit-packed rules without a lookup index.
  74828. *
  74829. * - The conceptual algorithm needs to canonicalize every codepoint in the
  74830. * input range to figure out the output range(s). Even with the small
  74831. * canonicalization bitmap the algorithm runs quite slowly for worst case
  74832. * inputs. There are many data structure alternatives to improve this.
  74833. *
  74834. * - While the current algorithm generates maximal output ranges when the
  74835. * output codepoints are emitted linearly, output ranges are not sorted or
  74836. * merged otherwise. In the worst case a lot of ranges are emitted when
  74837. * most of the ranges could be merged. In this process one could take
  74838. * advantage of "don't care" codepoints, which are never matched against at
  74839. * runtime due to canonicalization of input codepoints before comparison,
  74840. * to merge otherwise discontinuous output ranges.
  74841. *
  74842. * - The runtime data structure is just a linear list of ranges to match
  74843. * against. This can be quite slow if there are a lot of output ranges.
  74844. * There are various ways to make matching against the ranges faster,
  74845. * e.g. sorting the ranges and using a binary search; skip lists; tree
  74846. * based representations; full or approximate codepoint bitmaps, etc.
  74847. *
  74848. * - Only BMP is supported, codepoints above BMP are assumed to canonicalize
  74849. * to themselves. For now this is one place where we don't want to
  74850. * support chars outside the BMP, because the exhaustive search would be
  74851. * massively larger. It would be possible to support non-BMP with a
  74852. * different algorithm, or perhaps doing case normalization only at match
  74853. * time.
  74854. */
  74855. DUK_LOCAL void duk__regexp_emit_range(duk_re_compiler_ctx *re_ctx, duk_codepoint_t r1, duk_codepoint_t r2) {
  74856. DUK_ASSERT(r2 >= r1);
  74857. duk__append_u32(re_ctx, (duk_uint32_t) r1);
  74858. duk__append_u32(re_ctx, (duk_uint32_t) r2);
  74859. re_ctx->nranges++;
  74860. }
  74861. #if defined(DUK_USE_REGEXP_CANON_BITMAP)
  74862. /* Find next canonicalization discontinuity (conservative estimate) starting
  74863. * from 'start', not exceeding 'end'. If continuity is fine up to 'end'
  74864. * inclusive, returns end. Minimum possible return value is start.
  74865. */
  74866. DUK_LOCAL duk_codepoint_t duk__re_canon_next_discontinuity(duk_codepoint_t start, duk_codepoint_t end) {
  74867. duk_uint_t start_blk;
  74868. duk_uint_t end_blk;
  74869. duk_uint_t blk;
  74870. duk_uint_t offset;
  74871. duk_uint8_t mask;
  74872. /* Inclusive block range. */
  74873. DUK_ASSERT(start >= 0);
  74874. DUK_ASSERT(end >= 0);
  74875. DUK_ASSERT(end >= start);
  74876. start_blk = (duk_uint_t) (start >> DUK_CANON_BITMAP_BLKSHIFT);
  74877. end_blk = (duk_uint_t) (end >> DUK_CANON_BITMAP_BLKSHIFT);
  74878. for (blk = start_blk; blk <= end_blk; blk++) {
  74879. offset = blk >> 3;
  74880. mask = 1U << (blk & 0x07);
  74881. if (offset >= sizeof(duk_unicode_re_canon_bitmap)) {
  74882. /* Reached non-BMP range which is assumed continuous. */
  74883. return end;
  74884. }
  74885. DUK_ASSERT(offset < sizeof(duk_unicode_re_canon_bitmap));
  74886. if ((duk_unicode_re_canon_bitmap[offset] & mask) == 0) {
  74887. /* Block is discontinuous, continuity is guaranteed
  74888. * only up to end of previous block (+1 for exclusive
  74889. * return value => start of current block). Start
  74890. * block requires special handling.
  74891. */
  74892. if (blk > start_blk) {
  74893. return (duk_codepoint_t) (blk << DUK_CANON_BITMAP_BLKSHIFT);
  74894. } else {
  74895. return start;
  74896. }
  74897. }
  74898. }
  74899. DUK_ASSERT(blk == end_blk + 1); /* Reached end block which is continuous. */
  74900. return end;
  74901. }
  74902. #else /* DUK_USE_REGEXP_CANON_BITMAP */
  74903. DUK_LOCAL duk_codepoint_t duk__re_canon_next_discontinuity(duk_codepoint_t start, duk_codepoint_t end) {
  74904. DUK_ASSERT(start >= 0);
  74905. DUK_ASSERT(end >= 0);
  74906. DUK_ASSERT(end >= start);
  74907. if (start >= 0x10000) {
  74908. /* Even without the bitmap, treat non-BMP as continuous. */
  74909. return end;
  74910. }
  74911. return start;
  74912. }
  74913. #endif /* DUK_USE_REGEXP_CANON_BITMAP */
  74914. DUK_LOCAL void duk__regexp_generate_ranges(void *userdata, duk_codepoint_t r1, duk_codepoint_t r2, duk_bool_t direct) {
  74915. duk_re_compiler_ctx *re_ctx = (duk_re_compiler_ctx *) userdata;
  74916. duk_codepoint_t r_start;
  74917. duk_codepoint_t r_end;
  74918. duk_codepoint_t i;
  74919. duk_codepoint_t t;
  74920. duk_codepoint_t r_disc;
  74921. DUK_DD(DUK_DDPRINT("duk__regexp_generate_ranges(): re_ctx=%p, range=[%ld,%ld] direct=%ld",
  74922. (void *) re_ctx, (long) r1, (long) r2, (long) direct));
  74923. DUK_ASSERT(r2 >= r1); /* SyntaxError for out of order range. */
  74924. if (direct || (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) == 0) {
  74925. DUK_DD(DUK_DDPRINT("direct or not case sensitive, emit range: [%ld,%ld]", (long) r1, (long) r2));
  74926. duk__regexp_emit_range(re_ctx, r1, r2);
  74927. return;
  74928. }
  74929. DUK_DD(DUK_DDPRINT("case sensitive, process range: [%ld,%ld]", (long) r1, (long) r2));
  74930. r_start = duk_unicode_re_canonicalize_char(re_ctx->thr, r1);
  74931. r_end = r_start;
  74932. for (i = r1 + 1; i <= r2;) {
  74933. /* Input codepoint space processed up to i-1, and
  74934. * current range in r_{start,end} is up-to-date
  74935. * (inclusive) and may either break or continue.
  74936. */
  74937. r_disc = duk__re_canon_next_discontinuity(i, r2);
  74938. DUK_ASSERT(r_disc >= i);
  74939. DUK_ASSERT(r_disc <= r2);
  74940. r_end += r_disc - i; /* May be zero. */
  74941. t = duk_unicode_re_canonicalize_char(re_ctx->thr, r_disc);
  74942. if (t == r_end + 1) {
  74943. /* Not actually a discontinuity, continue range
  74944. * to r_disc and recheck.
  74945. */
  74946. r_end = t;
  74947. } else {
  74948. duk__regexp_emit_range(re_ctx, r_start, r_end);
  74949. r_start = t;
  74950. r_end = t;
  74951. }
  74952. i = r_disc + 1; /* Guarantees progress. */
  74953. }
  74954. duk__regexp_emit_range(re_ctx, r_start, r_end);
  74955. #if 0 /* Exhaustive search, very slow. */
  74956. r_start = duk_unicode_re_canonicalize_char(re_ctx->thr, r1);
  74957. r_end = r_start;
  74958. for (i = r1 + 1; i <= r2; i++) {
  74959. t = duk_unicode_re_canonicalize_char(re_ctx->thr, i);
  74960. if (t == r_end + 1) {
  74961. r_end = t;
  74962. } else {
  74963. DUK_DD(DUK_DDPRINT("canonicalized, emit range: [%ld,%ld]", (long) r_start, (long) r_end));
  74964. duk__append_u32(re_ctx, (duk_uint32_t) r_start);
  74965. duk__append_u32(re_ctx, (duk_uint32_t) r_end);
  74966. re_ctx->nranges++;
  74967. r_start = t;
  74968. r_end = t;
  74969. }
  74970. }
  74971. DUK_DD(DUK_DDPRINT("canonicalized, emit range: [%ld,%ld]", (long) r_start, (long) r_end));
  74972. duk__append_u32(re_ctx, (duk_uint32_t) r_start);
  74973. duk__append_u32(re_ctx, (duk_uint32_t) r_end);
  74974. re_ctx->nranges++;
  74975. #endif
  74976. }
  74977. /*
  74978. * Parse regexp Disjunction. Most of regexp compilation happens here.
  74979. *
  74980. * Handles Disjunction, Alternative, and Term productions directly without
  74981. * recursion. The only constructs requiring recursion are positive/negative
  74982. * lookaheads, capturing parentheses, and non-capturing parentheses.
  74983. *
  74984. * The function determines whether the entire disjunction is a 'simple atom'
  74985. * (see doc/regexp.rst discussion on 'simple quantifiers') and if so,
  74986. * returns the atom character length which is needed by the caller to keep
  74987. * track of its own atom character length. A disjunction with more than one
  74988. * alternative is never considered a simple atom (although in some cases
  74989. * that might be the case).
  74990. *
  74991. * Return value: simple atom character length or < 0 if not a simple atom.
  74992. * Appends the bytecode for the disjunction matcher to the end of the temp
  74993. * buffer.
  74994. *
  74995. * Regexp top level structure is:
  74996. *
  74997. * Disjunction = Term*
  74998. * | Term* | Disjunction
  74999. *
  75000. * Term = Assertion
  75001. * | Atom
  75002. * | Atom Quantifier
  75003. *
  75004. * An empty Term sequence is a valid disjunction alternative (e.g. /|||c||/).
  75005. *
  75006. * Notes:
  75007. *
  75008. * * Tracking of the 'simple-ness' of the current atom vs. the entire
  75009. * disjunction are separate matters. For instance, the disjunction
  75010. * may be complex, but individual atoms may be simple. Furthermore,
  75011. * simple quantifiers are used whenever possible, even if the
  75012. * disjunction as a whole is complex.
  75013. *
  75014. * * The estimate of whether an atom is simple is conservative now,
  75015. * and it would be possible to expand it. For instance, captures
  75016. * cause the disjunction to be marked complex, even though captures
  75017. * -can- be handled by simple quantifiers with some minor modifications.
  75018. *
  75019. * * Disjunction 'tainting' as 'complex' is handled at the end of the
  75020. * main for loop collectively for atoms. Assertions, quantifiers,
  75021. * and '|' tokens need to taint the result manually if necessary.
  75022. * Assertions cannot add to result char length, only atoms (and
  75023. * quantifiers) can; currently quantifiers will taint the result
  75024. * as complex though.
  75025. */
  75026. DUK_LOCAL const duk_uint16_t * const duk__re_range_lookup1[3] = {
  75027. duk_unicode_re_ranges_digit,
  75028. duk_unicode_re_ranges_white,
  75029. duk_unicode_re_ranges_wordchar
  75030. };
  75031. DUK_LOCAL const duk_uint8_t duk__re_range_lookup2[3] = {
  75032. sizeof(duk_unicode_re_ranges_digit) / (2 * sizeof(duk_uint16_t)),
  75033. sizeof(duk_unicode_re_ranges_white) / (2 * sizeof(duk_uint16_t)),
  75034. sizeof(duk_unicode_re_ranges_wordchar) / (2 * sizeof(duk_uint16_t))
  75035. };
  75036. DUK_LOCAL void duk__append_range_atom_matcher(duk_re_compiler_ctx *re_ctx, duk_small_uint_t re_op, const duk_uint16_t *ranges, duk_small_uint_t count) {
  75037. #if 0
  75038. DUK_ASSERT(re_op <= 0x7fUL);
  75039. DUK_ASSERT(count <= 0x7fUL);
  75040. duk__append_2bytes(re_ctx, (duk_uint8_t) re_op, (duk_uint8_t) count);
  75041. #endif
  75042. duk__append_reop(re_ctx, re_op);
  75043. duk__append_7bit(re_ctx, count);
  75044. duk__append_u16_list(re_ctx, ranges, count * 2);
  75045. }
  75046. DUK_LOCAL void duk__parse_disjunction(duk_re_compiler_ctx *re_ctx, duk_bool_t expect_eof, duk__re_disjunction_info *out_atom_info) {
  75047. duk_int32_t atom_start_offset = -1; /* negative -> no atom matched on previous round */
  75048. duk_int32_t atom_char_length = 0; /* negative -> complex atom */
  75049. duk_uint32_t atom_start_captures = re_ctx->captures; /* value of re_ctx->captures at start of atom */
  75050. duk_int32_t unpatched_disjunction_split = -1;
  75051. duk_int32_t unpatched_disjunction_jump = -1;
  75052. duk_uint32_t entry_offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx);
  75053. duk_int32_t res_charlen = 0; /* -1 if disjunction is complex, char length if simple */
  75054. duk__re_disjunction_info tmp_disj;
  75055. DUK_ASSERT(out_atom_info != NULL);
  75056. if (re_ctx->recursion_depth >= re_ctx->recursion_limit) {
  75057. DUK_ERROR_RANGE(re_ctx->thr, DUK_STR_REGEXP_COMPILER_RECURSION_LIMIT);
  75058. DUK_WO_NORETURN(return;);
  75059. }
  75060. re_ctx->recursion_depth++;
  75061. #if 0
  75062. out_atom_info->start_captures = re_ctx->captures;
  75063. #endif
  75064. for (;;) {
  75065. /* atom_char_length, atom_start_offset, atom_start_offset reflect the
  75066. * atom matched on the previous loop. If a quantifier is encountered
  75067. * on this loop, these are needed to handle the quantifier correctly.
  75068. * new_atom_char_length etc are for the atom parsed on this round;
  75069. * they're written to atom_char_length etc at the end of the round.
  75070. */
  75071. duk_int32_t new_atom_char_length; /* char length of the atom parsed in this loop */
  75072. duk_int32_t new_atom_start_offset; /* bytecode start offset of the atom parsed in this loop
  75073. * (allows quantifiers to copy the atom bytecode)
  75074. */
  75075. duk_uint32_t new_atom_start_captures; /* re_ctx->captures at the start of the atom parsed in this loop */
  75076. duk_lexer_parse_re_token(&re_ctx->lex, &re_ctx->curr_token);
  75077. DUK_DD(DUK_DDPRINT("re token: %ld (num=%ld, char=%c)",
  75078. (long) re_ctx->curr_token.t,
  75079. (long) re_ctx->curr_token.num,
  75080. (re_ctx->curr_token.num >= 0x20 && re_ctx->curr_token.num <= 0x7e) ?
  75081. (int) re_ctx->curr_token.num : (int) '?'));
  75082. /* set by atom case clauses */
  75083. new_atom_start_offset = -1;
  75084. new_atom_char_length = -1;
  75085. new_atom_start_captures = re_ctx->captures;
  75086. switch (re_ctx->curr_token.t) {
  75087. case DUK_RETOK_DISJUNCTION: {
  75088. /*
  75089. * The handling here is a bit tricky. If a previous '|' has been processed,
  75090. * we have a pending split1 and a pending jump (for a previous match). These
  75091. * need to be back-patched carefully. See docs for a detailed example.
  75092. */
  75093. /* patch pending jump and split */
  75094. if (unpatched_disjunction_jump >= 0) {
  75095. duk_uint32_t offset;
  75096. DUK_ASSERT(unpatched_disjunction_split >= 0);
  75097. offset = (duk_uint32_t) unpatched_disjunction_jump;
  75098. offset += duk__insert_jump_offset(re_ctx,
  75099. offset,
  75100. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - offset));
  75101. /* offset is now target of the pending split (right after jump) */
  75102. duk__insert_jump_offset(re_ctx,
  75103. (duk_uint32_t) unpatched_disjunction_split,
  75104. (duk_int32_t) offset - unpatched_disjunction_split);
  75105. }
  75106. /* add a new pending split to the beginning of the entire disjunction */
  75107. (void) duk__insert_u32(re_ctx,
  75108. entry_offset,
  75109. DUK_REOP_SPLIT1); /* prefer direct execution */
  75110. unpatched_disjunction_split = (duk_int32_t) (entry_offset + 1); /* +1 for opcode */
  75111. /* add a new pending match jump for latest finished alternative */
  75112. duk__append_reop(re_ctx, DUK_REOP_JUMP);
  75113. unpatched_disjunction_jump = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75114. /* 'taint' result as complex */
  75115. res_charlen = -1;
  75116. break;
  75117. }
  75118. case DUK_RETOK_QUANTIFIER: {
  75119. if (atom_start_offset < 0) {
  75120. DUK_ERROR_SYNTAX(re_ctx->thr, DUK_STR_INVALID_QUANTIFIER_NO_ATOM);
  75121. DUK_WO_NORETURN(return;);
  75122. }
  75123. if (re_ctx->curr_token.qmin > re_ctx->curr_token.qmax) {
  75124. DUK_ERROR_SYNTAX(re_ctx->thr, DUK_STR_INVALID_QUANTIFIER_VALUES);
  75125. DUK_WO_NORETURN(return;);
  75126. }
  75127. if (atom_char_length >= 0) {
  75128. /*
  75129. * Simple atom
  75130. *
  75131. * If atom_char_length is zero, we'll have unbounded execution time for e.g.
  75132. * /()*x/.exec('x'). We can't just skip the match because it might have some
  75133. * side effects (for instance, if we allowed captures in simple atoms, the
  75134. * capture needs to happen). The simple solution below is to force the
  75135. * quantifier to match at most once, since the additional matches have no effect.
  75136. *
  75137. * With a simple atom there can be no capture groups, so no captures need
  75138. * to be reset.
  75139. */
  75140. duk_int32_t atom_code_length;
  75141. duk_uint32_t offset;
  75142. duk_uint32_t qmin, qmax;
  75143. qmin = re_ctx->curr_token.qmin;
  75144. qmax = re_ctx->curr_token.qmax;
  75145. if (atom_char_length == 0) {
  75146. /* qmin and qmax will be 0 or 1 */
  75147. if (qmin > 1) {
  75148. qmin = 1;
  75149. }
  75150. if (qmax > 1) {
  75151. qmax = 1;
  75152. }
  75153. }
  75154. duk__append_reop(re_ctx, DUK_REOP_MATCH); /* complete 'sub atom' */
  75155. atom_code_length = (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - (duk_size_t) atom_start_offset);
  75156. offset = (duk_uint32_t) atom_start_offset;
  75157. if (re_ctx->curr_token.greedy) {
  75158. offset += duk__insert_u32(re_ctx, offset, DUK_REOP_SQGREEDY);
  75159. offset += duk__insert_u32(re_ctx, offset, qmin);
  75160. offset += duk__insert_u32(re_ctx, offset, qmax);
  75161. offset += duk__insert_u32(re_ctx, offset, (duk_uint32_t) atom_char_length);
  75162. offset += duk__insert_jump_offset(re_ctx, offset, atom_code_length);
  75163. } else {
  75164. offset += duk__insert_u32(re_ctx, offset, DUK_REOP_SQMINIMAL);
  75165. offset += duk__insert_u32(re_ctx, offset, qmin);
  75166. offset += duk__insert_u32(re_ctx, offset, qmax);
  75167. offset += duk__insert_jump_offset(re_ctx, offset, atom_code_length);
  75168. }
  75169. DUK_UNREF(offset); /* silence scan-build warning */
  75170. } else {
  75171. /*
  75172. * Complex atom
  75173. *
  75174. * The original code is used as a template, and removed at the end
  75175. * (this differs from the handling of simple quantifiers).
  75176. *
  75177. * NOTE: there is no current solution for empty atoms in complex
  75178. * quantifiers. This would need some sort of a 'progress' instruction.
  75179. *
  75180. * XXX: impose limit on maximum result size, i.e. atom_code_len * atom_copies?
  75181. */
  75182. duk_int32_t atom_code_length;
  75183. duk_uint32_t atom_copies;
  75184. duk_uint32_t tmp_qmin, tmp_qmax;
  75185. /* pre-check how many atom copies we're willing to make (atom_copies not needed below) */
  75186. atom_copies = (re_ctx->curr_token.qmax == DUK_RE_QUANTIFIER_INFINITE) ?
  75187. re_ctx->curr_token.qmin : re_ctx->curr_token.qmax;
  75188. if (atom_copies > DUK_RE_MAX_ATOM_COPIES) {
  75189. DUK_ERROR_RANGE(re_ctx->thr, DUK_STR_QUANTIFIER_TOO_MANY_COPIES);
  75190. DUK_WO_NORETURN(return;);
  75191. }
  75192. /* wipe the capture range made by the atom (if any) */
  75193. DUK_ASSERT(atom_start_captures <= re_ctx->captures);
  75194. if (atom_start_captures != re_ctx->captures) {
  75195. DUK_ASSERT(atom_start_captures < re_ctx->captures);
  75196. DUK_DDD(DUK_DDDPRINT("must wipe ]atom_start_captures,re_ctx->captures]: ]%ld,%ld]",
  75197. (long) atom_start_captures, (long) re_ctx->captures));
  75198. /* insert (DUK_REOP_WIPERANGE, start, count) in reverse order so the order ends up right */
  75199. duk__insert_u32(re_ctx, (duk_uint32_t) atom_start_offset, (re_ctx->captures - atom_start_captures) * 2U);
  75200. duk__insert_u32(re_ctx, (duk_uint32_t) atom_start_offset, (atom_start_captures + 1) * 2);
  75201. duk__insert_u32(re_ctx, (duk_uint32_t) atom_start_offset, DUK_REOP_WIPERANGE);
  75202. } else {
  75203. DUK_DDD(DUK_DDDPRINT("no need to wipe captures: atom_start_captures == re_ctx->captures == %ld",
  75204. (long) atom_start_captures));
  75205. }
  75206. atom_code_length = (duk_int32_t) DUK__RE_BUFLEN(re_ctx) - atom_start_offset;
  75207. /* insert the required matches (qmin) by copying the atom */
  75208. tmp_qmin = re_ctx->curr_token.qmin;
  75209. tmp_qmax = re_ctx->curr_token.qmax;
  75210. while (tmp_qmin > 0) {
  75211. duk__append_slice(re_ctx, (duk_uint32_t) atom_start_offset, (duk_uint32_t) atom_code_length);
  75212. tmp_qmin--;
  75213. if (tmp_qmax != DUK_RE_QUANTIFIER_INFINITE) {
  75214. tmp_qmax--;
  75215. }
  75216. }
  75217. DUK_ASSERT(tmp_qmin == 0);
  75218. /* insert code for matching the remainder - infinite or finite */
  75219. if (tmp_qmax == DUK_RE_QUANTIFIER_INFINITE) {
  75220. /* reuse last emitted atom for remaining 'infinite' quantifier */
  75221. if (re_ctx->curr_token.qmin == 0) {
  75222. /* Special case: original qmin was zero so there is nothing
  75223. * to repeat. Emit an atom copy but jump over it here.
  75224. */
  75225. duk__append_reop(re_ctx, DUK_REOP_JUMP);
  75226. duk__append_jump_offset(re_ctx, atom_code_length);
  75227. duk__append_slice(re_ctx, (duk_uint32_t) atom_start_offset, (duk_uint32_t) atom_code_length);
  75228. }
  75229. if (re_ctx->curr_token.greedy) {
  75230. duk__append_reop(re_ctx, DUK_REOP_SPLIT2); /* prefer jump */
  75231. } else {
  75232. duk__append_reop(re_ctx, DUK_REOP_SPLIT1); /* prefer direct */
  75233. }
  75234. duk__append_jump_offset(re_ctx, -atom_code_length - 1); /* -1 for opcode */
  75235. } else {
  75236. /*
  75237. * The remaining matches are emitted as sequence of SPLITs and atom
  75238. * copies; the SPLITs skip the remaining copies and match the sequel.
  75239. * This sequence needs to be emitted starting from the last copy
  75240. * because the SPLITs are variable length due to the variable length
  75241. * skip offset. This causes a lot of memory copying now.
  75242. *
  75243. * Example structure (greedy, match maximum # atoms):
  75244. *
  75245. * SPLIT1 LSEQ
  75246. * (atom)
  75247. * SPLIT1 LSEQ ; <- the byte length of this instruction is needed
  75248. * (atom) ; to encode the above SPLIT1 correctly
  75249. * ...
  75250. * LSEQ:
  75251. */
  75252. duk_uint32_t offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx);
  75253. while (tmp_qmax > 0) {
  75254. duk__insert_slice(re_ctx, offset, (duk_uint32_t) atom_start_offset, (duk_uint32_t) atom_code_length);
  75255. if (re_ctx->curr_token.greedy) {
  75256. duk__insert_u32(re_ctx, offset, DUK_REOP_SPLIT1); /* prefer direct */
  75257. } else {
  75258. duk__insert_u32(re_ctx, offset, DUK_REOP_SPLIT2); /* prefer jump */
  75259. }
  75260. duk__insert_jump_offset(re_ctx,
  75261. offset + 1, /* +1 for opcode */
  75262. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - (offset + 1)));
  75263. tmp_qmax--;
  75264. }
  75265. }
  75266. /* remove the original 'template' atom */
  75267. duk__remove_slice(re_ctx, (duk_uint32_t) atom_start_offset, (duk_uint32_t) atom_code_length);
  75268. }
  75269. /* 'taint' result as complex */
  75270. res_charlen = -1;
  75271. break;
  75272. }
  75273. case DUK_RETOK_ASSERT_START: {
  75274. duk__append_reop(re_ctx, DUK_REOP_ASSERT_START);
  75275. break;
  75276. }
  75277. case DUK_RETOK_ASSERT_END: {
  75278. duk__append_reop(re_ctx, DUK_REOP_ASSERT_END);
  75279. break;
  75280. }
  75281. case DUK_RETOK_ASSERT_WORD_BOUNDARY: {
  75282. duk__append_reop(re_ctx, DUK_REOP_ASSERT_WORD_BOUNDARY);
  75283. break;
  75284. }
  75285. case DUK_RETOK_ASSERT_NOT_WORD_BOUNDARY: {
  75286. duk__append_reop(re_ctx, DUK_REOP_ASSERT_NOT_WORD_BOUNDARY);
  75287. break;
  75288. }
  75289. case DUK_RETOK_ASSERT_START_POS_LOOKAHEAD:
  75290. case DUK_RETOK_ASSERT_START_NEG_LOOKAHEAD: {
  75291. duk_uint32_t offset;
  75292. duk_uint32_t opcode = (re_ctx->curr_token.t == DUK_RETOK_ASSERT_START_POS_LOOKAHEAD) ?
  75293. DUK_REOP_LOOKPOS : DUK_REOP_LOOKNEG;
  75294. offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx);
  75295. duk__parse_disjunction(re_ctx, 0, &tmp_disj);
  75296. duk__append_reop(re_ctx, DUK_REOP_MATCH);
  75297. (void) duk__insert_u32(re_ctx, offset, opcode);
  75298. (void) duk__insert_jump_offset(re_ctx,
  75299. offset + 1, /* +1 for opcode */
  75300. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - (offset + 1)));
  75301. /* 'taint' result as complex -- this is conservative,
  75302. * as lookaheads do not backtrack.
  75303. */
  75304. res_charlen = -1;
  75305. break;
  75306. }
  75307. case DUK_RETOK_ATOM_PERIOD: {
  75308. new_atom_char_length = 1;
  75309. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75310. duk__append_reop(re_ctx, DUK_REOP_PERIOD);
  75311. break;
  75312. }
  75313. case DUK_RETOK_ATOM_CHAR: {
  75314. /* Note: successive characters could be joined into string matches
  75315. * but this is not trivial (consider e.g. '/xyz+/); see docs for
  75316. * more discussion.
  75317. *
  75318. * No support for \u{H+} yet. While only BMP Unicode escapes are
  75319. * supported for RegExps at present, 'ch' may still be a non-BMP
  75320. * codepoint if it is decoded straight from source text UTF-8.
  75321. * There's no non-BMP support yet so this is handled simply by
  75322. * matching the non-BMP character (which is custom behavior).
  75323. */
  75324. duk_uint32_t ch;
  75325. new_atom_char_length = 1;
  75326. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75327. duk__append_reop(re_ctx, DUK_REOP_CHAR);
  75328. ch = re_ctx->curr_token.num;
  75329. if (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) {
  75330. ch = (duk_uint32_t) duk_unicode_re_canonicalize_char(re_ctx->thr, (duk_codepoint_t) ch);
  75331. }
  75332. duk__append_u32(re_ctx, ch);
  75333. break;
  75334. }
  75335. case DUK_RETOK_ATOM_DIGIT:
  75336. case DUK_RETOK_ATOM_NOT_DIGIT:
  75337. case DUK_RETOK_ATOM_WHITE:
  75338. case DUK_RETOK_ATOM_NOT_WHITE:
  75339. case DUK_RETOK_ATOM_WORD_CHAR:
  75340. case DUK_RETOK_ATOM_NOT_WORD_CHAR: {
  75341. duk_small_uint_t re_op;
  75342. duk_small_uint_t idx;
  75343. new_atom_char_length = 1;
  75344. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75345. DUK_ASSERT((DUK_RETOK_ATOM_DIGIT & 0x01) != 0);
  75346. DUK_ASSERT((DUK_RETOK_ATOM_WHITE & 0x01) != 0);
  75347. DUK_ASSERT((DUK_RETOK_ATOM_WORD_CHAR & 0x01) != 0);
  75348. DUK_ASSERT((DUK_RETOK_ATOM_NOT_DIGIT & 0x01) == 0);
  75349. DUK_ASSERT((DUK_RETOK_ATOM_NOT_WHITE & 0x01) == 0);
  75350. DUK_ASSERT((DUK_RETOK_ATOM_NOT_WORD_CHAR & 0x01) == 0);
  75351. re_op = (re_ctx->curr_token.t & 0x01) ? DUK_REOP_RANGES : DUK_REOP_INVRANGES;
  75352. DUK_ASSERT(DUK_RETOK_ATOM_WHITE == DUK_RETOK_ATOM_DIGIT + 2);
  75353. DUK_ASSERT(DUK_RETOK_ATOM_WORD_CHAR == DUK_RETOK_ATOM_DIGIT + 4);
  75354. idx = (duk_small_uint_t) ((re_ctx->curr_token.t - DUK_RETOK_ATOM_DIGIT) >> 1U);
  75355. DUK_ASSERT(idx <= 2U); /* Assume continuous token numbers; also checks negative underflow. */
  75356. duk__append_range_atom_matcher(re_ctx, re_op, duk__re_range_lookup1[idx], duk__re_range_lookup2[idx]);
  75357. break;
  75358. }
  75359. case DUK_RETOK_ATOM_BACKREFERENCE: {
  75360. duk_uint32_t backref = (duk_uint32_t) re_ctx->curr_token.num;
  75361. if (backref > re_ctx->highest_backref) {
  75362. re_ctx->highest_backref = backref;
  75363. }
  75364. new_atom_char_length = -1; /* mark as complex */
  75365. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75366. duk__append_reop(re_ctx, DUK_REOP_BACKREFERENCE);
  75367. duk__append_u32(re_ctx, backref);
  75368. break;
  75369. }
  75370. case DUK_RETOK_ATOM_START_CAPTURE_GROUP: {
  75371. duk_uint32_t cap;
  75372. new_atom_char_length = -1; /* mark as complex (capture handling) */
  75373. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75374. cap = ++re_ctx->captures;
  75375. duk__append_reop(re_ctx, DUK_REOP_SAVE);
  75376. duk__append_u32(re_ctx, cap * 2);
  75377. duk__parse_disjunction(re_ctx, 0, &tmp_disj); /* retval (sub-atom char length) unused, tainted as complex above */
  75378. duk__append_reop(re_ctx, DUK_REOP_SAVE);
  75379. duk__append_u32(re_ctx, cap * 2 + 1);
  75380. break;
  75381. }
  75382. case DUK_RETOK_ATOM_START_NONCAPTURE_GROUP: {
  75383. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75384. duk__parse_disjunction(re_ctx, 0, &tmp_disj);
  75385. new_atom_char_length = tmp_disj.charlen;
  75386. break;
  75387. }
  75388. case DUK_RETOK_ATOM_START_CHARCLASS:
  75389. case DUK_RETOK_ATOM_START_CHARCLASS_INVERTED: {
  75390. /*
  75391. * Range parsing is done with a special lexer function which calls
  75392. * us for every range parsed. This is different from how rest of
  75393. * the parsing works, but avoids a heavy, arbitrary size intermediate
  75394. * value type to hold the ranges.
  75395. *
  75396. * Another complication is the handling of character ranges when
  75397. * case insensitive matching is used (see docs for discussion).
  75398. * The range handler callback given to the lexer takes care of this
  75399. * as well.
  75400. *
  75401. * Note that duplicate ranges are not eliminated when parsing character
  75402. * classes, so that canonicalization of
  75403. *
  75404. * [0-9a-fA-Fx-{]
  75405. *
  75406. * creates the result (note the duplicate ranges):
  75407. *
  75408. * [0-9A-FA-FX-Z{-{]
  75409. *
  75410. * where [x-{] is split as a result of canonicalization. The duplicate
  75411. * ranges are not a semantics issue: they work correctly.
  75412. */
  75413. duk_uint32_t offset;
  75414. DUK_DD(DUK_DDPRINT("character class"));
  75415. /* insert ranges instruction, range count patched in later */
  75416. new_atom_char_length = 1;
  75417. new_atom_start_offset = (duk_int32_t) DUK__RE_BUFLEN(re_ctx);
  75418. duk__append_reop(re_ctx,
  75419. (re_ctx->curr_token.t == DUK_RETOK_ATOM_START_CHARCLASS) ?
  75420. DUK_REOP_RANGES : DUK_REOP_INVRANGES);
  75421. offset = (duk_uint32_t) DUK__RE_BUFLEN(re_ctx); /* patch in range count later */
  75422. /* parse ranges until character class ends */
  75423. re_ctx->nranges = 0; /* note: ctx-wide temporary */
  75424. duk_lexer_parse_re_ranges(&re_ctx->lex, duk__regexp_generate_ranges, (void *) re_ctx);
  75425. /* insert range count */
  75426. duk__insert_u32(re_ctx, offset, re_ctx->nranges);
  75427. break;
  75428. }
  75429. case DUK_RETOK_ATOM_END_GROUP: {
  75430. if (expect_eof) {
  75431. DUK_ERROR_SYNTAX(re_ctx->thr, DUK_STR_UNEXPECTED_CLOSING_PAREN);
  75432. DUK_WO_NORETURN(return;);
  75433. }
  75434. goto done;
  75435. }
  75436. case DUK_RETOK_EOF: {
  75437. if (!expect_eof) {
  75438. DUK_ERROR_SYNTAX(re_ctx->thr, DUK_STR_UNEXPECTED_END_OF_PATTERN);
  75439. DUK_WO_NORETURN(return;);
  75440. }
  75441. goto done;
  75442. }
  75443. default: {
  75444. DUK_ERROR_SYNTAX(re_ctx->thr, DUK_STR_UNEXPECTED_REGEXP_TOKEN);
  75445. DUK_WO_NORETURN(return;);
  75446. }
  75447. }
  75448. /* a complex (new) atom taints the result */
  75449. if (new_atom_start_offset >= 0) {
  75450. if (new_atom_char_length < 0) {
  75451. res_charlen = -1;
  75452. } else if (res_charlen >= 0) {
  75453. /* only advance if not tainted */
  75454. res_charlen += new_atom_char_length;
  75455. }
  75456. }
  75457. /* record previous atom info in case next token is a quantifier */
  75458. atom_start_offset = new_atom_start_offset;
  75459. atom_char_length = new_atom_char_length;
  75460. atom_start_captures = new_atom_start_captures;
  75461. }
  75462. done:
  75463. /* finish up pending jump and split for last alternative */
  75464. if (unpatched_disjunction_jump >= 0) {
  75465. duk_uint32_t offset;
  75466. DUK_ASSERT(unpatched_disjunction_split >= 0);
  75467. offset = (duk_uint32_t) unpatched_disjunction_jump;
  75468. offset += duk__insert_jump_offset(re_ctx,
  75469. offset,
  75470. (duk_int32_t) (DUK__RE_BUFLEN(re_ctx) - offset));
  75471. /* offset is now target of the pending split (right after jump) */
  75472. duk__insert_jump_offset(re_ctx,
  75473. (duk_uint32_t) unpatched_disjunction_split,
  75474. (duk_int32_t) offset - unpatched_disjunction_split);
  75475. }
  75476. #if 0
  75477. out_atom_info->end_captures = re_ctx->captures;
  75478. #endif
  75479. out_atom_info->charlen = res_charlen;
  75480. DUK_DDD(DUK_DDDPRINT("parse disjunction finished: charlen=%ld",
  75481. (long) out_atom_info->charlen));
  75482. re_ctx->recursion_depth--;
  75483. }
  75484. /*
  75485. * Flags parsing (see E5 Section 15.10.4.1).
  75486. */
  75487. DUK_LOCAL duk_uint32_t duk__parse_regexp_flags(duk_hthread *thr, duk_hstring *h) {
  75488. const duk_uint8_t *p;
  75489. const duk_uint8_t *p_end;
  75490. duk_uint32_t flags = 0;
  75491. p = DUK_HSTRING_GET_DATA(h);
  75492. p_end = p + DUK_HSTRING_GET_BYTELEN(h);
  75493. /* Note: can be safely scanned as bytes (undecoded) */
  75494. while (p < p_end) {
  75495. duk_uint8_t c = *p++;
  75496. switch (c) {
  75497. case (duk_uint8_t) 'g': {
  75498. if (flags & DUK_RE_FLAG_GLOBAL) {
  75499. goto flags_error;
  75500. }
  75501. flags |= DUK_RE_FLAG_GLOBAL;
  75502. break;
  75503. }
  75504. case (duk_uint8_t) 'i': {
  75505. if (flags & DUK_RE_FLAG_IGNORE_CASE) {
  75506. goto flags_error;
  75507. }
  75508. flags |= DUK_RE_FLAG_IGNORE_CASE;
  75509. break;
  75510. }
  75511. case (duk_uint8_t) 'm': {
  75512. if (flags & DUK_RE_FLAG_MULTILINE) {
  75513. goto flags_error;
  75514. }
  75515. flags |= DUK_RE_FLAG_MULTILINE;
  75516. break;
  75517. }
  75518. default: {
  75519. goto flags_error;
  75520. }
  75521. }
  75522. }
  75523. return flags;
  75524. flags_error:
  75525. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_REGEXP_FLAGS);
  75526. DUK_WO_NORETURN(return 0U;);
  75527. }
  75528. /*
  75529. * Create escaped RegExp source (E5 Section 15.10.3).
  75530. *
  75531. * The current approach is to special case the empty RegExp
  75532. * ('' -> '(?:)') and otherwise replace unescaped '/' characters
  75533. * with '\/' regardless of where they occur in the regexp.
  75534. *
  75535. * Note that normalization does not seem to be necessary for
  75536. * RegExp literals (e.g. '/foo/') because to be acceptable as
  75537. * a RegExp literal, the text between forward slashes must
  75538. * already match the escaping requirements (e.g. must not contain
  75539. * unescaped forward slashes or be empty). Escaping IS needed
  75540. * for expressions like 'new Regexp("...", "")' however.
  75541. * Currently, we re-escape in either case.
  75542. *
  75543. * Also note that we process the source here in UTF-8 encoded
  75544. * form. This is correct, because any non-ASCII characters are
  75545. * passed through without change.
  75546. */
  75547. DUK_LOCAL void duk__create_escaped_source(duk_hthread *thr, int idx_pattern) {
  75548. duk_hstring *h;
  75549. const duk_uint8_t *p;
  75550. duk_bufwriter_ctx bw_alloc;
  75551. duk_bufwriter_ctx *bw;
  75552. duk_uint8_t *q;
  75553. duk_size_t i, n;
  75554. duk_uint_fast8_t c_prev, c;
  75555. h = duk_known_hstring(thr, idx_pattern);
  75556. p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h);
  75557. n = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
  75558. if (n == 0) {
  75559. duk_push_literal(thr, "(?:)");
  75560. return;
  75561. }
  75562. bw = &bw_alloc;
  75563. DUK_BW_INIT_PUSHBUF(thr, bw, n);
  75564. q = DUK_BW_GET_PTR(thr, bw);
  75565. c_prev = (duk_uint_fast8_t) 0;
  75566. for (i = 0; i < n; i++) {
  75567. c = p[i];
  75568. q = DUK_BW_ENSURE_RAW(thr, bw, 2, q);
  75569. if (c == (duk_uint_fast8_t) '/' && c_prev != (duk_uint_fast8_t) '\\') {
  75570. /* Unescaped '/' ANYWHERE in the regexp (in disjunction,
  75571. * inside a character class, ...) => same escape works.
  75572. */
  75573. *q++ = DUK_ASC_BACKSLASH;
  75574. }
  75575. *q++ = (duk_uint8_t) c;
  75576. c_prev = c;
  75577. }
  75578. DUK_BW_SETPTR_AND_COMPACT(thr, bw, q);
  75579. (void) duk_buffer_to_string(thr, -1); /* Safe if input is safe. */
  75580. /* [ ... escaped_source ] */
  75581. }
  75582. /*
  75583. * Exposed regexp compilation primitive.
  75584. *
  75585. * Sets up a regexp compilation context, and calls duk__parse_disjunction() to do the
  75586. * actual parsing. Handles generation of the compiled regexp header and the
  75587. * "boilerplate" capture of the matching substring (save 0 and 1). Also does some
  75588. * global level regexp checks after recursive compilation has finished.
  75589. *
  75590. * An escaped version of the regexp source, suitable for use as a RegExp instance
  75591. * 'source' property (see E5 Section 15.10.3), is also left on the stack.
  75592. *
  75593. * Input stack: [ pattern flags ]
  75594. * Output stack: [ bytecode escaped_source ] (both as strings)
  75595. */
  75596. DUK_INTERNAL void duk_regexp_compile(duk_hthread *thr) {
  75597. duk_re_compiler_ctx re_ctx;
  75598. duk_lexer_point lex_point;
  75599. duk_hstring *h_pattern;
  75600. duk_hstring *h_flags;
  75601. duk__re_disjunction_info ign_disj;
  75602. DUK_ASSERT(thr != NULL);
  75603. /*
  75604. * Args validation
  75605. */
  75606. /* TypeError if fails */
  75607. h_pattern = duk_require_hstring_notsymbol(thr, -2);
  75608. h_flags = duk_require_hstring_notsymbol(thr, -1);
  75609. /*
  75610. * Create normalized 'source' property (E5 Section 15.10.3).
  75611. */
  75612. /* [ ... pattern flags ] */
  75613. duk__create_escaped_source(thr, -2);
  75614. /* [ ... pattern flags escaped_source ] */
  75615. /*
  75616. * Init compilation context
  75617. */
  75618. /* [ ... pattern flags escaped_source buffer ] */
  75619. duk_memzero(&re_ctx, sizeof(re_ctx));
  75620. DUK_LEXER_INITCTX(&re_ctx.lex); /* duplicate zeroing, expect for (possible) NULL inits */
  75621. re_ctx.thr = thr;
  75622. re_ctx.lex.thr = thr;
  75623. re_ctx.lex.input = DUK_HSTRING_GET_DATA(h_pattern);
  75624. re_ctx.lex.input_length = DUK_HSTRING_GET_BYTELEN(h_pattern);
  75625. re_ctx.lex.token_limit = DUK_RE_COMPILE_TOKEN_LIMIT;
  75626. re_ctx.recursion_limit = DUK_USE_REGEXP_COMPILER_RECLIMIT;
  75627. re_ctx.re_flags = duk__parse_regexp_flags(thr, h_flags);
  75628. DUK_BW_INIT_PUSHBUF(thr, &re_ctx.bw, DUK__RE_INITIAL_BUFSIZE);
  75629. DUK_DD(DUK_DDPRINT("regexp compiler ctx initialized, flags=0x%08lx, recursion_limit=%ld",
  75630. (unsigned long) re_ctx.re_flags, (long) re_ctx.recursion_limit));
  75631. /*
  75632. * Init lexer
  75633. */
  75634. lex_point.offset = 0; /* expensive init, just want to fill window */
  75635. lex_point.line = 1;
  75636. DUK_LEXER_SETPOINT(&re_ctx.lex, &lex_point);
  75637. /*
  75638. * Compilation
  75639. */
  75640. DUK_DD(DUK_DDPRINT("starting regexp compilation"));
  75641. duk__append_reop(&re_ctx, DUK_REOP_SAVE);
  75642. duk__append_7bit(&re_ctx, 0);
  75643. duk__parse_disjunction(&re_ctx, 1 /*expect_eof*/, &ign_disj);
  75644. duk__append_reop(&re_ctx, DUK_REOP_SAVE);
  75645. duk__append_7bit(&re_ctx, 1);
  75646. duk__append_reop(&re_ctx, DUK_REOP_MATCH);
  75647. /*
  75648. * Check for invalid backreferences; note that it is NOT an error
  75649. * to back-reference a capture group which has not yet been introduced
  75650. * in the pattern (as in /\1(foo)/); in fact, the backreference will
  75651. * always match! It IS an error to back-reference a capture group
  75652. * which will never be introduced in the pattern. Thus, we can check
  75653. * for such references only after parsing is complete.
  75654. */
  75655. if (re_ctx.highest_backref > re_ctx.captures) {
  75656. DUK_ERROR_SYNTAX(thr, DUK_STR_INVALID_BACKREFS);
  75657. DUK_WO_NORETURN(return;);
  75658. }
  75659. /*
  75660. * Emit compiled regexp header: flags, ncaptures
  75661. * (insertion order inverted on purpose)
  75662. */
  75663. duk__insert_u32(&re_ctx, 0, (re_ctx.captures + 1) * 2);
  75664. duk__insert_u32(&re_ctx, 0, re_ctx.re_flags);
  75665. /* [ ... pattern flags escaped_source buffer ] */
  75666. DUK_BW_COMPACT(thr, &re_ctx.bw);
  75667. (void) duk_buffer_to_string(thr, -1); /* Safe because flags is at most 7 bit. */
  75668. /* [ ... pattern flags escaped_source bytecode ] */
  75669. /*
  75670. * Finalize stack
  75671. */
  75672. duk_remove(thr, -4); /* -> [ ... flags escaped_source bytecode ] */
  75673. duk_remove(thr, -3); /* -> [ ... escaped_source bytecode ] */
  75674. DUK_DD(DUK_DDPRINT("regexp compilation successful, bytecode: %!T, escaped source: %!T",
  75675. (duk_tval *) duk_get_tval(thr, -1), (duk_tval *) duk_get_tval(thr, -2)));
  75676. }
  75677. /*
  75678. * Create a RegExp instance (E5 Section 15.10.7).
  75679. *
  75680. * Note: the output stack left by duk_regexp_compile() is directly compatible
  75681. * with the input here.
  75682. *
  75683. * Input stack: [ escaped_source bytecode ] (both as strings)
  75684. * Output stack: [ RegExp ]
  75685. */
  75686. DUK_INTERNAL void duk_regexp_create_instance(duk_hthread *thr) {
  75687. duk_hobject *h;
  75688. /* [ ... escaped_source bytecode ] */
  75689. duk_push_object(thr);
  75690. h = duk_known_hobject(thr, -1);
  75691. duk_insert(thr, -3);
  75692. /* [ ... regexp_object escaped_source bytecode ] */
  75693. DUK_HOBJECT_SET_CLASS_NUMBER(h, DUK_HOBJECT_CLASS_REGEXP);
  75694. DUK_HOBJECT_SET_PROTOTYPE_UPDREF(thr, h, thr->builtins[DUK_BIDX_REGEXP_PROTOTYPE]);
  75695. duk_xdef_prop_stridx_short(thr, -3, DUK_STRIDX_INT_BYTECODE, DUK_PROPDESC_FLAGS_NONE);
  75696. /* [ ... regexp_object escaped_source ] */
  75697. /* In ES2015 .source, and the .global, .multiline, etc flags are
  75698. * inherited getters. Store the escaped source as an internal
  75699. * property for the getter.
  75700. */
  75701. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_INT_SOURCE, DUK_PROPDESC_FLAGS_NONE);
  75702. /* [ ... regexp_object ] */
  75703. duk_push_int(thr, 0);
  75704. duk_xdef_prop_stridx_short(thr, -2, DUK_STRIDX_LAST_INDEX, DUK_PROPDESC_FLAGS_W);
  75705. /* [ ... regexp_object ] */
  75706. }
  75707. #else /* DUK_USE_REGEXP_SUPPORT */
  75708. /* regexp support disabled */
  75709. #endif /* DUK_USE_REGEXP_SUPPORT */
  75710. /* automatic undefs */
  75711. #undef DUK__RE_BUFLEN
  75712. #undef DUK__RE_INITIAL_BUFSIZE
  75713. #line 1 "duk_regexp_executor.c"
  75714. /*
  75715. * Regexp executor.
  75716. *
  75717. * Safety: the ECMAScript executor should prevent user from reading and
  75718. * replacing regexp bytecode. Even so, the executor must validate all
  75719. * memory accesses etc. When an invalid access is detected (e.g. a 'save'
  75720. * opcode to invalid, unallocated index) it should fail with an internal
  75721. * error but not cause a segmentation fault.
  75722. *
  75723. * Notes:
  75724. *
  75725. * - Backtrack counts are limited to unsigned 32 bits but should
  75726. * technically be duk_size_t for strings longer than 4G chars.
  75727. * This also requires a regexp bytecode change.
  75728. */
  75729. /* #include duk_internal.h -> already included */
  75730. #if defined(DUK_USE_REGEXP_SUPPORT)
  75731. /*
  75732. * Helpers for UTF-8 handling
  75733. *
  75734. * For bytecode readers the duk_uint32_t and duk_int32_t types are correct
  75735. * because they're used for more than just codepoints.
  75736. */
  75737. DUK_LOCAL duk_uint32_t duk__bc_get_u32(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **pc) {
  75738. return (duk_uint32_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, pc, re_ctx->bytecode, re_ctx->bytecode_end);
  75739. }
  75740. DUK_LOCAL duk_int32_t duk__bc_get_i32(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **pc) {
  75741. duk_uint32_t t;
  75742. /* signed integer encoding needed to work with UTF-8 */
  75743. t = (duk_uint32_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, pc, re_ctx->bytecode, re_ctx->bytecode_end);
  75744. if (t & 1) {
  75745. return -((duk_int32_t) (t >> 1));
  75746. } else {
  75747. return (duk_int32_t) (t >> 1);
  75748. }
  75749. }
  75750. DUK_LOCAL const duk_uint8_t *duk__utf8_backtrack(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_uint_fast32_t count) {
  75751. const duk_uint8_t *p;
  75752. /* Note: allow backtracking from p == ptr_end */
  75753. p = *ptr;
  75754. if (p < ptr_start || p > ptr_end) {
  75755. goto fail;
  75756. }
  75757. while (count > 0) {
  75758. for (;;) {
  75759. p--;
  75760. if (p < ptr_start) {
  75761. goto fail;
  75762. }
  75763. if ((*p & 0xc0) != 0x80) {
  75764. /* utf-8 continuation bytes have the form 10xx xxxx */
  75765. break;
  75766. }
  75767. }
  75768. count--;
  75769. }
  75770. *ptr = p;
  75771. return p;
  75772. fail:
  75773. DUK_ERROR_INTERNAL(thr);
  75774. DUK_WO_NORETURN(return NULL;);
  75775. }
  75776. DUK_LOCAL const duk_uint8_t *duk__utf8_advance(duk_hthread *thr, const duk_uint8_t **ptr, const duk_uint8_t *ptr_start, const duk_uint8_t *ptr_end, duk_uint_fast32_t count) {
  75777. const duk_uint8_t *p;
  75778. p = *ptr;
  75779. if (p < ptr_start || p >= ptr_end) {
  75780. goto fail;
  75781. }
  75782. while (count > 0) {
  75783. for (;;) {
  75784. p++;
  75785. /* Note: if encoding ends by hitting end of input, we don't check that
  75786. * the encoding is valid, we just assume it is.
  75787. */
  75788. if (p >= ptr_end || ((*p & 0xc0) != 0x80)) {
  75789. /* utf-8 continuation bytes have the form 10xx xxxx */
  75790. break;
  75791. }
  75792. }
  75793. count--;
  75794. }
  75795. *ptr = p;
  75796. return p;
  75797. fail:
  75798. DUK_ERROR_INTERNAL(thr);
  75799. DUK_WO_NORETURN(return NULL;);
  75800. }
  75801. /*
  75802. * Helpers for dealing with the input string
  75803. */
  75804. /* Get a (possibly canonicalized) input character from current sp. The input
  75805. * itself is never modified, and captures always record non-canonicalized
  75806. * characters even in case-insensitive matching. Return <0 if out of input.
  75807. */
  75808. DUK_LOCAL duk_codepoint_t duk__inp_get_cp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **sp) {
  75809. duk_codepoint_t res;
  75810. if (*sp >= re_ctx->input_end) {
  75811. return -1;
  75812. }
  75813. res = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(re_ctx->thr, sp, re_ctx->input, re_ctx->input_end);
  75814. if (re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) {
  75815. res = duk_unicode_re_canonicalize_char(re_ctx->thr, res);
  75816. }
  75817. return res;
  75818. }
  75819. DUK_LOCAL const duk_uint8_t *duk__inp_backtrack(duk_re_matcher_ctx *re_ctx, const duk_uint8_t **sp, duk_uint_fast32_t count) {
  75820. return duk__utf8_backtrack(re_ctx->thr, sp, re_ctx->input, re_ctx->input_end, count);
  75821. }
  75822. /* Backtrack utf-8 input and return a (possibly canonicalized) input character. */
  75823. DUK_LOCAL duk_codepoint_t duk__inp_get_prev_cp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t *sp) {
  75824. /* note: caller 'sp' is intentionally not updated here */
  75825. (void) duk__inp_backtrack(re_ctx, &sp, (duk_uint_fast32_t) 1);
  75826. return duk__inp_get_cp(re_ctx, &sp);
  75827. }
  75828. /*
  75829. * Regexp recursive matching function.
  75830. *
  75831. * Returns 'sp' on successful match (points to character after last matched one),
  75832. * NULL otherwise.
  75833. *
  75834. * The C recursion depth limit check is only performed in this function, this
  75835. * suffices because the function is present in all true recursion required by
  75836. * regexp execution.
  75837. */
  75838. DUK_LOCAL const duk_uint8_t *duk__match_regexp(duk_re_matcher_ctx *re_ctx, const duk_uint8_t *pc, const duk_uint8_t *sp) {
  75839. if (re_ctx->recursion_depth >= re_ctx->recursion_limit) {
  75840. DUK_ERROR_RANGE(re_ctx->thr, DUK_STR_REGEXP_EXECUTOR_RECURSION_LIMIT);
  75841. DUK_WO_NORETURN(return NULL;);
  75842. }
  75843. re_ctx->recursion_depth++;
  75844. for (;;) {
  75845. duk_small_int_t op;
  75846. if (re_ctx->steps_count >= re_ctx->steps_limit) {
  75847. DUK_ERROR_RANGE(re_ctx->thr, DUK_STR_REGEXP_EXECUTOR_STEP_LIMIT);
  75848. DUK_WO_NORETURN(return NULL;);
  75849. }
  75850. re_ctx->steps_count++;
  75851. /* Opcodes are at most 7 bits now so they encode to one byte. If this
  75852. * were not the case or 'pc' is invalid here (due to a bug etc) we'll
  75853. * still fail safely through the switch default case.
  75854. */
  75855. DUK_ASSERT(pc[0] <= 0x7fU);
  75856. #if 0
  75857. op = (duk_small_int_t) duk__bc_get_u32(re_ctx, &pc);
  75858. #endif
  75859. op = *pc++;
  75860. DUK_DDD(DUK_DDDPRINT("match: rec=%ld, steps=%ld, pc (after op)=%ld, sp=%ld, op=%ld",
  75861. (long) re_ctx->recursion_depth,
  75862. (long) re_ctx->steps_count,
  75863. (long) (pc - re_ctx->bytecode),
  75864. (long) (sp - re_ctx->input),
  75865. (long) op));
  75866. switch (op) {
  75867. case DUK_REOP_MATCH: {
  75868. goto match;
  75869. }
  75870. case DUK_REOP_CHAR: {
  75871. /*
  75872. * Byte-based matching would be possible for case-sensitive
  75873. * matching but not for case-insensitive matching. So, we
  75874. * match by decoding the input and bytecode character normally.
  75875. *
  75876. * Bytecode characters are assumed to be already canonicalized.
  75877. * Input characters are canonicalized automatically by
  75878. * duk__inp_get_cp() if necessary.
  75879. *
  75880. * There is no opcode for matching multiple characters. The
  75881. * regexp compiler has trouble joining strings efficiently
  75882. * during compilation. See doc/regexp.rst for more discussion.
  75883. */
  75884. duk_codepoint_t c1, c2;
  75885. c1 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  75886. DUK_ASSERT(!(re_ctx->re_flags & DUK_RE_FLAG_IGNORE_CASE) ||
  75887. c1 == duk_unicode_re_canonicalize_char(re_ctx->thr, c1)); /* canonicalized by compiler */
  75888. c2 = duk__inp_get_cp(re_ctx, &sp);
  75889. /* No need to check for c2 < 0 (end of input): because c1 >= 0, it
  75890. * will fail the match below automatically and cause goto fail.
  75891. */
  75892. #if 0
  75893. if (c2 < 0) {
  75894. goto fail;
  75895. }
  75896. #endif
  75897. DUK_ASSERT(c1 >= 0);
  75898. DUK_DDD(DUK_DDDPRINT("char match, c1=%ld, c2=%ld", (long) c1, (long) c2));
  75899. if (c1 != c2) {
  75900. goto fail;
  75901. }
  75902. break;
  75903. }
  75904. case DUK_REOP_PERIOD: {
  75905. duk_codepoint_t c;
  75906. c = duk__inp_get_cp(re_ctx, &sp);
  75907. if (c < 0 || duk_unicode_is_line_terminator(c)) {
  75908. /* E5 Sections 15.10.2.8, 7.3 */
  75909. goto fail;
  75910. }
  75911. break;
  75912. }
  75913. case DUK_REOP_RANGES:
  75914. case DUK_REOP_INVRANGES: {
  75915. duk_uint32_t n;
  75916. duk_codepoint_t c;
  75917. duk_small_int_t match;
  75918. n = duk__bc_get_u32(re_ctx, &pc);
  75919. c = duk__inp_get_cp(re_ctx, &sp);
  75920. if (c < 0) {
  75921. goto fail;
  75922. }
  75923. match = 0;
  75924. while (n) {
  75925. duk_codepoint_t r1, r2;
  75926. r1 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  75927. r2 = (duk_codepoint_t) duk__bc_get_u32(re_ctx, &pc);
  75928. DUK_DDD(DUK_DDDPRINT("matching ranges/invranges, n=%ld, r1=%ld, r2=%ld, c=%ld",
  75929. (long) n, (long) r1, (long) r2, (long) c));
  75930. if (c >= r1 && c <= r2) {
  75931. /* Note: don't bail out early, we must read all the ranges from
  75932. * bytecode. Another option is to skip them efficiently after
  75933. * breaking out of here. Prefer smallest code.
  75934. */
  75935. match = 1;
  75936. }
  75937. n--;
  75938. }
  75939. if (op == DUK_REOP_RANGES) {
  75940. if (!match) {
  75941. goto fail;
  75942. }
  75943. } else {
  75944. DUK_ASSERT(op == DUK_REOP_INVRANGES);
  75945. if (match) {
  75946. goto fail;
  75947. }
  75948. }
  75949. break;
  75950. }
  75951. case DUK_REOP_ASSERT_START: {
  75952. duk_codepoint_t c;
  75953. if (sp <= re_ctx->input) {
  75954. break;
  75955. }
  75956. if (!(re_ctx->re_flags & DUK_RE_FLAG_MULTILINE)) {
  75957. goto fail;
  75958. }
  75959. c = duk__inp_get_prev_cp(re_ctx, sp);
  75960. if (duk_unicode_is_line_terminator(c)) {
  75961. /* E5 Sections 15.10.2.8, 7.3 */
  75962. break;
  75963. }
  75964. goto fail;
  75965. }
  75966. case DUK_REOP_ASSERT_END: {
  75967. duk_codepoint_t c;
  75968. const duk_uint8_t *tmp_sp;
  75969. tmp_sp = sp;
  75970. c = duk__inp_get_cp(re_ctx, &tmp_sp);
  75971. if (c < 0) {
  75972. break;
  75973. }
  75974. if (!(re_ctx->re_flags & DUK_RE_FLAG_MULTILINE)) {
  75975. goto fail;
  75976. }
  75977. if (duk_unicode_is_line_terminator(c)) {
  75978. /* E5 Sections 15.10.2.8, 7.3 */
  75979. break;
  75980. }
  75981. goto fail;
  75982. }
  75983. case DUK_REOP_ASSERT_WORD_BOUNDARY:
  75984. case DUK_REOP_ASSERT_NOT_WORD_BOUNDARY: {
  75985. /*
  75986. * E5 Section 15.10.2.6. The previous and current character
  75987. * should -not- be canonicalized as they are now. However,
  75988. * canonicalization does not affect the result of IsWordChar()
  75989. * (which depends on Unicode characters never canonicalizing
  75990. * into ASCII characters) so this does not matter.
  75991. */
  75992. duk_small_int_t w1, w2;
  75993. if (sp <= re_ctx->input) {
  75994. w1 = 0; /* not a wordchar */
  75995. } else {
  75996. duk_codepoint_t c;
  75997. c = duk__inp_get_prev_cp(re_ctx, sp);
  75998. w1 = duk_unicode_re_is_wordchar(c);
  75999. }
  76000. if (sp >= re_ctx->input_end) {
  76001. w2 = 0; /* not a wordchar */
  76002. } else {
  76003. const duk_uint8_t *tmp_sp = sp; /* dummy so sp won't get updated */
  76004. duk_codepoint_t c;
  76005. c = duk__inp_get_cp(re_ctx, &tmp_sp);
  76006. w2 = duk_unicode_re_is_wordchar(c);
  76007. }
  76008. if (op == DUK_REOP_ASSERT_WORD_BOUNDARY) {
  76009. if (w1 == w2) {
  76010. goto fail;
  76011. }
  76012. } else {
  76013. DUK_ASSERT(op == DUK_REOP_ASSERT_NOT_WORD_BOUNDARY);
  76014. if (w1 != w2) {
  76015. goto fail;
  76016. }
  76017. }
  76018. break;
  76019. }
  76020. case DUK_REOP_JUMP: {
  76021. duk_int32_t skip;
  76022. skip = duk__bc_get_i32(re_ctx, &pc);
  76023. pc += skip;
  76024. break;
  76025. }
  76026. case DUK_REOP_SPLIT1: {
  76027. /* split1: prefer direct execution (no jump) */
  76028. const duk_uint8_t *sub_sp;
  76029. duk_int32_t skip;
  76030. skip = duk__bc_get_i32(re_ctx, &pc);
  76031. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  76032. if (sub_sp) {
  76033. sp = sub_sp;
  76034. goto match;
  76035. }
  76036. pc += skip;
  76037. break;
  76038. }
  76039. case DUK_REOP_SPLIT2: {
  76040. /* split2: prefer jump execution (not direct) */
  76041. const duk_uint8_t *sub_sp;
  76042. duk_int32_t skip;
  76043. skip = duk__bc_get_i32(re_ctx, &pc);
  76044. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  76045. if (sub_sp) {
  76046. sp = sub_sp;
  76047. goto match;
  76048. }
  76049. break;
  76050. }
  76051. case DUK_REOP_SQMINIMAL: {
  76052. duk_uint32_t q, qmin, qmax;
  76053. duk_int32_t skip;
  76054. const duk_uint8_t *sub_sp;
  76055. qmin = duk__bc_get_u32(re_ctx, &pc);
  76056. qmax = duk__bc_get_u32(re_ctx, &pc);
  76057. skip = duk__bc_get_i32(re_ctx, &pc);
  76058. DUK_DDD(DUK_DDDPRINT("minimal quantifier, qmin=%lu, qmax=%lu, skip=%ld",
  76059. (unsigned long) qmin, (unsigned long) qmax, (long) skip));
  76060. q = 0;
  76061. while (q <= qmax) {
  76062. if (q >= qmin) {
  76063. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  76064. if (sub_sp) {
  76065. sp = sub_sp;
  76066. goto match;
  76067. }
  76068. }
  76069. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  76070. if (!sub_sp) {
  76071. break;
  76072. }
  76073. sp = sub_sp;
  76074. q++;
  76075. }
  76076. goto fail;
  76077. }
  76078. case DUK_REOP_SQGREEDY: {
  76079. duk_uint32_t q, qmin, qmax, atomlen;
  76080. duk_int32_t skip;
  76081. const duk_uint8_t *sub_sp;
  76082. qmin = duk__bc_get_u32(re_ctx, &pc);
  76083. qmax = duk__bc_get_u32(re_ctx, &pc);
  76084. atomlen = duk__bc_get_u32(re_ctx, &pc);
  76085. skip = duk__bc_get_i32(re_ctx, &pc);
  76086. DUK_DDD(DUK_DDDPRINT("greedy quantifier, qmin=%lu, qmax=%lu, atomlen=%lu, skip=%ld",
  76087. (unsigned long) qmin, (unsigned long) qmax, (unsigned long) atomlen, (long) skip));
  76088. q = 0;
  76089. while (q < qmax) {
  76090. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  76091. if (!sub_sp) {
  76092. break;
  76093. }
  76094. sp = sub_sp;
  76095. q++;
  76096. }
  76097. while (q >= qmin) {
  76098. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  76099. if (sub_sp) {
  76100. sp = sub_sp;
  76101. goto match;
  76102. }
  76103. if (q == qmin) {
  76104. break;
  76105. }
  76106. /* Note: if atom were to contain e.g. captures, we would need to
  76107. * re-match the atom to get correct captures. Simply quantifiers
  76108. * do not allow captures in their atom now, so this is not an issue.
  76109. */
  76110. DUK_DDD(DUK_DDDPRINT("greedy quantifier, backtrack %ld characters (atomlen)",
  76111. (long) atomlen));
  76112. sp = duk__inp_backtrack(re_ctx, &sp, (duk_uint_fast32_t) atomlen);
  76113. q--;
  76114. }
  76115. goto fail;
  76116. }
  76117. case DUK_REOP_SAVE: {
  76118. duk_uint32_t idx;
  76119. const duk_uint8_t *old;
  76120. const duk_uint8_t *sub_sp;
  76121. idx = duk__bc_get_u32(re_ctx, &pc);
  76122. if (idx >= re_ctx->nsaved) {
  76123. /* idx is unsigned, < 0 check is not necessary */
  76124. DUK_D(DUK_DPRINT("internal error, regexp save index insane: idx=%ld", (long) idx));
  76125. goto internal_error;
  76126. }
  76127. old = re_ctx->saved[idx];
  76128. re_ctx->saved[idx] = sp;
  76129. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  76130. if (sub_sp) {
  76131. sp = sub_sp;
  76132. goto match;
  76133. }
  76134. re_ctx->saved[idx] = old;
  76135. goto fail;
  76136. }
  76137. case DUK_REOP_WIPERANGE: {
  76138. /* Wipe capture range and save old values for backtracking.
  76139. *
  76140. * XXX: this typically happens with a relatively small idx_count.
  76141. * It might be useful to handle cases where the count is small
  76142. * (say <= 8) by saving the values in stack instead. This would
  76143. * reduce memory churn and improve performance, at the cost of a
  76144. * slightly higher code footprint.
  76145. */
  76146. duk_uint32_t idx_start, idx_count;
  76147. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  76148. duk_uint32_t idx_end, idx;
  76149. #endif
  76150. duk_uint8_t **range_save;
  76151. const duk_uint8_t *sub_sp;
  76152. idx_start = duk__bc_get_u32(re_ctx, &pc);
  76153. idx_count = duk__bc_get_u32(re_ctx, &pc);
  76154. DUK_DDD(DUK_DDDPRINT("wipe saved range: start=%ld, count=%ld -> [%ld,%ld] (captures [%ld,%ld])",
  76155. (long) idx_start, (long) idx_count,
  76156. (long) idx_start, (long) (idx_start + idx_count - 1),
  76157. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  76158. if (idx_start + idx_count > re_ctx->nsaved || idx_count == 0) {
  76159. /* idx is unsigned, < 0 check is not necessary */
  76160. DUK_D(DUK_DPRINT("internal error, regexp wipe indices insane: idx_start=%ld, idx_count=%ld",
  76161. (long) idx_start, (long) idx_count));
  76162. goto internal_error;
  76163. }
  76164. DUK_ASSERT(idx_count > 0);
  76165. duk_require_stack(re_ctx->thr, 1);
  76166. range_save = (duk_uint8_t **) duk_push_fixed_buffer_nozero(re_ctx->thr,
  76167. sizeof(duk_uint8_t *) * idx_count);
  76168. DUK_ASSERT(range_save != NULL);
  76169. duk_memcpy(range_save, re_ctx->saved + idx_start, sizeof(duk_uint8_t *) * idx_count);
  76170. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  76171. idx_end = idx_start + idx_count;
  76172. for (idx = idx_start; idx < idx_end; idx++) {
  76173. re_ctx->saved[idx] = NULL;
  76174. }
  76175. #else
  76176. duk_memzero((void *) (re_ctx->saved + idx_start), sizeof(duk_uint8_t *) * idx_count);
  76177. #endif
  76178. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  76179. if (sub_sp) {
  76180. /* match: keep wiped/resaved values */
  76181. DUK_DDD(DUK_DDDPRINT("match: keep wiped/resaved values [%ld,%ld] (captures [%ld,%ld])",
  76182. (long) idx_start, (long) (idx_start + idx_count - 1),
  76183. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  76184. duk_pop_unsafe(re_ctx->thr);
  76185. sp = sub_sp;
  76186. goto match;
  76187. }
  76188. /* fail: restore saves */
  76189. DUK_DDD(DUK_DDDPRINT("fail: restore wiped/resaved values [%ld,%ld] (captures [%ld,%ld])",
  76190. (long) idx_start, (long) (idx_start + idx_count - 1),
  76191. (long) (idx_start / 2), (long) ((idx_start + idx_count - 1) / 2)));
  76192. duk_memcpy((void *) (re_ctx->saved + idx_start),
  76193. (const void *) range_save,
  76194. sizeof(duk_uint8_t *) * idx_count);
  76195. duk_pop_unsafe(re_ctx->thr);
  76196. goto fail;
  76197. }
  76198. case DUK_REOP_LOOKPOS:
  76199. case DUK_REOP_LOOKNEG: {
  76200. /*
  76201. * Needs a save of multiple saved[] entries depending on what range
  76202. * may be overwritten. Because the regexp parser does no such analysis,
  76203. * we currently save the entire saved array here. Lookaheads are thus
  76204. * a bit expensive. Note that the saved array is not needed for just
  76205. * the lookahead sub-match, but for the matching of the entire sequel.
  76206. *
  76207. * The temporary save buffer is pushed on to the valstack to handle
  76208. * errors correctly. Each lookahead causes a C recursion and pushes
  76209. * more stuff on the value stack. If the C recursion limit is less
  76210. * than the value stack slack, there is no need to check the stack.
  76211. * We do so regardless, just in case.
  76212. */
  76213. duk_int32_t skip;
  76214. duk_uint8_t **full_save;
  76215. const duk_uint8_t *sub_sp;
  76216. DUK_ASSERT(re_ctx->nsaved > 0);
  76217. duk_require_stack(re_ctx->thr, 1);
  76218. full_save = (duk_uint8_t **) duk_push_fixed_buffer_nozero(re_ctx->thr,
  76219. sizeof(duk_uint8_t *) * re_ctx->nsaved);
  76220. DUK_ASSERT(full_save != NULL);
  76221. duk_memcpy(full_save, re_ctx->saved, sizeof(duk_uint8_t *) * re_ctx->nsaved);
  76222. skip = duk__bc_get_i32(re_ctx, &pc);
  76223. sub_sp = duk__match_regexp(re_ctx, pc, sp);
  76224. if (op == DUK_REOP_LOOKPOS) {
  76225. if (!sub_sp) {
  76226. goto lookahead_fail;
  76227. }
  76228. } else {
  76229. if (sub_sp) {
  76230. goto lookahead_fail;
  76231. }
  76232. }
  76233. sub_sp = duk__match_regexp(re_ctx, pc + skip, sp);
  76234. if (sub_sp) {
  76235. /* match: keep saves */
  76236. duk_pop_unsafe(re_ctx->thr);
  76237. sp = sub_sp;
  76238. goto match;
  76239. }
  76240. /* fall through */
  76241. lookahead_fail:
  76242. /* fail: restore saves */
  76243. duk_memcpy((void *) re_ctx->saved,
  76244. (const void *) full_save,
  76245. sizeof(duk_uint8_t *) * re_ctx->nsaved);
  76246. duk_pop_unsafe(re_ctx->thr);
  76247. goto fail;
  76248. }
  76249. case DUK_REOP_BACKREFERENCE: {
  76250. /*
  76251. * Byte matching for back-references would be OK in case-
  76252. * sensitive matching. In case-insensitive matching we need
  76253. * to canonicalize characters, so back-reference matching needs
  76254. * to be done with codepoints instead. So, we just decode
  76255. * everything normally here, too.
  76256. *
  76257. * Note: back-reference index which is 0 or higher than
  76258. * NCapturingParens (= number of capturing parens in the
  76259. * -entire- regexp) is a compile time error. However, a
  76260. * backreference referring to a valid capture which has
  76261. * not matched anything always succeeds! See E5 Section
  76262. * 15.10.2.9, step 5, sub-step 3.
  76263. */
  76264. duk_uint32_t idx;
  76265. const duk_uint8_t *p;
  76266. idx = duk__bc_get_u32(re_ctx, &pc);
  76267. idx = idx << 1; /* backref n -> saved indices [n*2, n*2+1] */
  76268. if (idx < 2 || idx + 1 >= re_ctx->nsaved) {
  76269. /* regexp compiler should catch these */
  76270. DUK_D(DUK_DPRINT("internal error, backreference index insane"));
  76271. goto internal_error;
  76272. }
  76273. if (!re_ctx->saved[idx] || !re_ctx->saved[idx+1]) {
  76274. /* capture is 'undefined', always matches! */
  76275. DUK_DDD(DUK_DDDPRINT("backreference: saved[%ld,%ld] not complete, always match",
  76276. (long) idx, (long) (idx + 1)));
  76277. break;
  76278. }
  76279. DUK_DDD(DUK_DDDPRINT("backreference: match saved[%ld,%ld]", (long) idx, (long) (idx + 1)));
  76280. p = re_ctx->saved[idx];
  76281. while (p < re_ctx->saved[idx+1]) {
  76282. duk_codepoint_t c1, c2;
  76283. /* Note: not necessary to check p against re_ctx->input_end:
  76284. * the memory access is checked by duk__inp_get_cp(), while
  76285. * valid compiled regexps cannot write a saved[] entry
  76286. * which points to outside the string.
  76287. */
  76288. c1 = duk__inp_get_cp(re_ctx, &p);
  76289. DUK_ASSERT(c1 >= 0);
  76290. c2 = duk__inp_get_cp(re_ctx, &sp);
  76291. /* No need for an explicit c2 < 0 check: because c1 >= 0,
  76292. * the comparison will always fail if c2 < 0.
  76293. */
  76294. #if 0
  76295. if (c2 < 0) {
  76296. goto fail;
  76297. }
  76298. #endif
  76299. if (c1 != c2) {
  76300. goto fail;
  76301. }
  76302. }
  76303. break;
  76304. }
  76305. default: {
  76306. DUK_D(DUK_DPRINT("internal error, regexp opcode error: %ld", (long) op));
  76307. goto internal_error;
  76308. }
  76309. }
  76310. }
  76311. match:
  76312. re_ctx->recursion_depth--;
  76313. return sp;
  76314. fail:
  76315. re_ctx->recursion_depth--;
  76316. return NULL;
  76317. internal_error:
  76318. DUK_ERROR_INTERNAL(re_ctx->thr);
  76319. DUK_WO_NORETURN(return NULL;);
  76320. }
  76321. /*
  76322. * Exposed matcher function which provides the semantics of RegExp.prototype.exec().
  76323. *
  76324. * RegExp.prototype.test() has the same semantics as exec() but does not return the
  76325. * result object (which contains the matching string and capture groups). Currently
  76326. * there is no separate test() helper, so a temporary result object is created and
  76327. * discarded if test() is needed. This is intentional, to save code space.
  76328. *
  76329. * Input stack: [ ... re_obj input ]
  76330. * Output stack: [ ... result ]
  76331. */
  76332. DUK_LOCAL void duk__regexp_match_helper(duk_hthread *thr, duk_small_int_t force_global) {
  76333. duk_re_matcher_ctx re_ctx;
  76334. duk_hobject *h_regexp;
  76335. duk_hstring *h_bytecode;
  76336. duk_hstring *h_input;
  76337. duk_uint8_t *p_buf;
  76338. const duk_uint8_t *pc;
  76339. const duk_uint8_t *sp;
  76340. duk_small_int_t match = 0;
  76341. duk_small_int_t global;
  76342. duk_uint_fast32_t i;
  76343. double d;
  76344. duk_uint32_t char_offset;
  76345. DUK_ASSERT(thr != NULL);
  76346. DUK_DD(DUK_DDPRINT("regexp match: regexp=%!T, input=%!T",
  76347. (duk_tval *) duk_get_tval(thr, -2),
  76348. (duk_tval *) duk_get_tval(thr, -1)));
  76349. /*
  76350. * Regexp instance check, bytecode check, input coercion.
  76351. *
  76352. * See E5 Section 15.10.6.
  76353. */
  76354. /* TypeError if wrong; class check, see E5 Section 15.10.6 */
  76355. h_regexp = duk_require_hobject_with_class(thr, -2, DUK_HOBJECT_CLASS_REGEXP);
  76356. DUK_ASSERT(h_regexp != NULL);
  76357. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_regexp) == DUK_HOBJECT_CLASS_REGEXP);
  76358. DUK_UNREF(h_regexp);
  76359. h_input = duk_to_hstring(thr, -1);
  76360. DUK_ASSERT(h_input != NULL);
  76361. duk_get_prop_stridx_short(thr, -2, DUK_STRIDX_INT_BYTECODE); /* [ ... re_obj input ] -> [ ... re_obj input bc ] */
  76362. h_bytecode = duk_require_hstring(thr, -1); /* no regexp instance should exist without a non-configurable bytecode property */
  76363. DUK_ASSERT(h_bytecode != NULL);
  76364. /*
  76365. * Basic context initialization.
  76366. *
  76367. * Some init values are read from the bytecode header
  76368. * whose format is (UTF-8 codepoints):
  76369. *
  76370. * uint flags
  76371. * uint nsaved (even, 2n+2 where n = num captures)
  76372. */
  76373. /* [ ... re_obj input bc ] */
  76374. duk_memzero(&re_ctx, sizeof(re_ctx));
  76375. re_ctx.thr = thr;
  76376. re_ctx.input = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
  76377. re_ctx.input_end = re_ctx.input + DUK_HSTRING_GET_BYTELEN(h_input);
  76378. re_ctx.bytecode = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h_bytecode);
  76379. re_ctx.bytecode_end = re_ctx.bytecode + DUK_HSTRING_GET_BYTELEN(h_bytecode);
  76380. re_ctx.saved = NULL;
  76381. re_ctx.recursion_limit = DUK_USE_REGEXP_EXECUTOR_RECLIMIT;
  76382. re_ctx.steps_limit = DUK_RE_EXECUTE_STEPS_LIMIT;
  76383. /* read header */
  76384. pc = re_ctx.bytecode;
  76385. re_ctx.re_flags = duk__bc_get_u32(&re_ctx, &pc);
  76386. re_ctx.nsaved = duk__bc_get_u32(&re_ctx, &pc);
  76387. re_ctx.bytecode = pc;
  76388. DUK_ASSERT(DUK_RE_FLAG_GLOBAL < 0x10000UL); /* must fit into duk_small_int_t */
  76389. global = (duk_small_int_t) (force_global | (duk_small_int_t) (re_ctx.re_flags & DUK_RE_FLAG_GLOBAL));
  76390. DUK_ASSERT(re_ctx.nsaved >= 2);
  76391. DUK_ASSERT((re_ctx.nsaved % 2) == 0);
  76392. p_buf = (duk_uint8_t *) duk_push_fixed_buffer(thr, sizeof(duk_uint8_t *) * re_ctx.nsaved); /* rely on zeroing */
  76393. DUK_UNREF(p_buf);
  76394. re_ctx.saved = (const duk_uint8_t **) duk_get_buffer(thr, -1, NULL);
  76395. DUK_ASSERT(re_ctx.saved != NULL);
  76396. /* [ ... re_obj input bc saved_buf ] */
  76397. #if defined(DUK_USE_EXPLICIT_NULL_INIT)
  76398. for (i = 0; i < re_ctx.nsaved; i++) {
  76399. re_ctx.saved[i] = (duk_uint8_t *) NULL;
  76400. }
  76401. #elif defined(DUK_USE_ZERO_BUFFER_DATA)
  76402. /* buffer is automatically zeroed */
  76403. #else
  76404. duk_memzero((void *) p_buf, sizeof(duk_uint8_t *) * re_ctx.nsaved);
  76405. #endif
  76406. DUK_DDD(DUK_DDDPRINT("regexp ctx initialized, flags=0x%08lx, nsaved=%ld, recursion_limit=%ld, steps_limit=%ld",
  76407. (unsigned long) re_ctx.re_flags, (long) re_ctx.nsaved, (long) re_ctx.recursion_limit,
  76408. (long) re_ctx.steps_limit));
  76409. /*
  76410. * Get starting character offset for match, and initialize 'sp' based on it.
  76411. *
  76412. * Note: lastIndex is non-configurable so it must be present (we check the
  76413. * internal class of the object above, so we know it is). User code can set
  76414. * its value to an arbitrary (garbage) value though; E5 requires that lastIndex
  76415. * be coerced to a number before using. The code below works even if the
  76416. * property is missing: the value will then be coerced to zero.
  76417. *
  76418. * Note: lastIndex may be outside Uint32 range even after ToInteger() coercion.
  76419. * For instance, ToInteger(+Infinity) = +Infinity. We track the match offset
  76420. * as an integer, but pre-check it to be inside the 32-bit range before the loop.
  76421. * If not, the check in E5 Section 15.10.6.2, step 9.a applies.
  76422. */
  76423. /* XXX: lastIndex handling produces a lot of asm */
  76424. /* [ ... re_obj input bc saved_buf ] */
  76425. duk_get_prop_stridx_short(thr, -4, DUK_STRIDX_LAST_INDEX); /* -> [ ... re_obj input bc saved_buf lastIndex ] */
  76426. (void) duk_to_int(thr, -1); /* ToInteger(lastIndex) */
  76427. d = duk_get_number(thr, -1); /* integer, but may be +/- Infinite, +/- zero (not NaN, though) */
  76428. duk_pop_nodecref_unsafe(thr);
  76429. if (global) {
  76430. if (d < 0.0 || d > (double) DUK_HSTRING_GET_CHARLEN(h_input)) {
  76431. /* match fail */
  76432. char_offset = 0; /* not really necessary */
  76433. DUK_ASSERT(match == 0);
  76434. goto match_over;
  76435. }
  76436. char_offset = (duk_uint32_t) d;
  76437. } else {
  76438. /* lastIndex must be ignored for non-global regexps, but get the
  76439. * value for (theoretical) side effects. No side effects can
  76440. * really occur, because lastIndex is a normal property and is
  76441. * always non-configurable for RegExp instances.
  76442. */
  76443. char_offset = (duk_uint32_t) 0;
  76444. }
  76445. DUK_ASSERT(char_offset <= DUK_HSTRING_GET_CHARLEN(h_input));
  76446. sp = re_ctx.input + duk_heap_strcache_offset_char2byte(thr, h_input, char_offset);
  76447. /*
  76448. * Match loop.
  76449. *
  76450. * Try matching at different offsets until match found or input exhausted.
  76451. */
  76452. /* [ ... re_obj input bc saved_buf ] */
  76453. DUK_ASSERT(match == 0);
  76454. for (;;) {
  76455. /* char offset in [0, h_input->clen] (both ends inclusive), checked before entry */
  76456. DUK_ASSERT_DISABLE(char_offset >= 0);
  76457. DUK_ASSERT(char_offset <= DUK_HSTRING_GET_CHARLEN(h_input));
  76458. /* Note: re_ctx.steps is intentionally not reset, it applies to the entire unanchored match */
  76459. DUK_ASSERT(re_ctx.recursion_depth == 0);
  76460. DUK_DDD(DUK_DDDPRINT("attempt match at char offset %ld; %p [%p,%p]",
  76461. (long) char_offset, (const void *) sp,
  76462. (const void *) re_ctx.input, (const void *) re_ctx.input_end));
  76463. /*
  76464. * Note:
  76465. *
  76466. * - duk__match_regexp() is required not to longjmp() in ordinary "non-match"
  76467. * conditions; a longjmp() will terminate the entire matching process.
  76468. *
  76469. * - Clearing saved[] is not necessary because backtracking does it
  76470. *
  76471. * - Backtracking also rewinds re_ctx.recursion back to zero, unless an
  76472. * internal/limit error occurs (which causes a longjmp())
  76473. *
  76474. * - If we supported anchored matches, we would break out here
  76475. * unconditionally; however, ECMAScript regexps don't have anchored
  76476. * matches. It might make sense to implement a fast bail-out if
  76477. * the regexp begins with '^' and sp is not 0: currently we'll just
  76478. * run through the entire input string, trivially failing the match
  76479. * at every non-zero offset.
  76480. */
  76481. if (duk__match_regexp(&re_ctx, re_ctx.bytecode, sp) != NULL) {
  76482. DUK_DDD(DUK_DDDPRINT("match at offset %ld", (long) char_offset));
  76483. match = 1;
  76484. break;
  76485. }
  76486. /* advance by one character (code point) and one char_offset */
  76487. char_offset++;
  76488. if (char_offset > DUK_HSTRING_GET_CHARLEN(h_input)) {
  76489. /*
  76490. * Note:
  76491. *
  76492. * - Intentionally attempt (empty) match at char_offset == k_input->clen
  76493. *
  76494. * - Negative char_offsets have been eliminated and char_offset is duk_uint32_t
  76495. * -> no need or use for a negative check
  76496. */
  76497. DUK_DDD(DUK_DDDPRINT("no match after trying all sp offsets"));
  76498. break;
  76499. }
  76500. /* avoid calling at end of input, will DUK_ERROR (above check suffices to avoid this) */
  76501. (void) duk__utf8_advance(thr, &sp, re_ctx.input, re_ctx.input_end, (duk_uint_fast32_t) 1);
  76502. }
  76503. match_over:
  76504. /*
  76505. * Matching complete, create result array or return a 'null'. Update lastIndex
  76506. * if necessary. See E5 Section 15.10.6.2.
  76507. *
  76508. * Because lastIndex is a character (not byte) offset, we need the character
  76509. * length of the match which we conveniently get as a side effect of interning
  76510. * the matching substring (0th index of result array).
  76511. *
  76512. * saved[0] start pointer (~ byte offset) of current match
  76513. * saved[1] end pointer (~ byte offset) of current match (exclusive)
  76514. * char_offset start character offset of current match (-> .index of result)
  76515. * char_end_offset end character offset (computed below)
  76516. */
  76517. /* [ ... re_obj input bc saved_buf ] */
  76518. if (match) {
  76519. #if defined(DUK_USE_ASSERTIONS)
  76520. duk_hobject *h_res;
  76521. #endif
  76522. duk_uint32_t char_end_offset = 0;
  76523. DUK_DDD(DUK_DDDPRINT("regexp matches at char_offset %ld", (long) char_offset));
  76524. DUK_ASSERT(re_ctx.nsaved >= 2); /* must have start and end */
  76525. DUK_ASSERT((re_ctx.nsaved % 2) == 0); /* and even number */
  76526. /* XXX: Array size is known before and (2 * re_ctx.nsaved) but not taken
  76527. * advantage of now. The array is not compacted either, as regexp match
  76528. * objects are usually short lived.
  76529. */
  76530. duk_push_array(thr);
  76531. #if defined(DUK_USE_ASSERTIONS)
  76532. h_res = duk_require_hobject(thr, -1);
  76533. DUK_ASSERT(DUK_HOBJECT_HAS_EXTENSIBLE(h_res));
  76534. DUK_ASSERT(DUK_HOBJECT_HAS_EXOTIC_ARRAY(h_res));
  76535. DUK_ASSERT(DUK_HOBJECT_GET_CLASS_NUMBER(h_res) == DUK_HOBJECT_CLASS_ARRAY);
  76536. #endif
  76537. /* [ ... re_obj input bc saved_buf res_obj ] */
  76538. duk_push_u32(thr, char_offset);
  76539. duk_xdef_prop_stridx_short_wec(thr, -2, DUK_STRIDX_INDEX);
  76540. duk_dup_m4(thr);
  76541. duk_xdef_prop_stridx_short_wec(thr, -2, DUK_STRIDX_INPUT);
  76542. for (i = 0; i < re_ctx.nsaved; i += 2) {
  76543. /* Captures which are undefined have NULL pointers and are returned
  76544. * as 'undefined'. The same is done when saved[] pointers are insane
  76545. * (this should, of course, never happen in practice).
  76546. */
  76547. if (re_ctx.saved[i] && re_ctx.saved[i + 1] && re_ctx.saved[i + 1] >= re_ctx.saved[i]) {
  76548. duk_push_lstring(thr,
  76549. (const char *) re_ctx.saved[i],
  76550. (duk_size_t) (re_ctx.saved[i+1] - re_ctx.saved[i]));
  76551. if (i == 0) {
  76552. /* Assumes that saved[0] and saved[1] are always
  76553. * set by regexp bytecode (if not, char_end_offset
  76554. * will be zero). Also assumes clen reflects the
  76555. * correct char length.
  76556. */
  76557. char_end_offset = char_offset + (duk_uint32_t) duk_get_length(thr, -1); /* add charlen */
  76558. }
  76559. } else {
  76560. duk_push_undefined(thr);
  76561. }
  76562. /* [ ... re_obj input bc saved_buf res_obj val ] */
  76563. duk_put_prop_index(thr, -2, (duk_uarridx_t) (i / 2));
  76564. }
  76565. /* [ ... re_obj input bc saved_buf res_obj ] */
  76566. /* NB: 'length' property is automatically updated by the array setup loop */
  76567. if (global) {
  76568. /* global regexp: lastIndex updated on match */
  76569. duk_push_u32(thr, char_end_offset);
  76570. duk_put_prop_stridx_short(thr, -6, DUK_STRIDX_LAST_INDEX);
  76571. } else {
  76572. /* non-global regexp: lastIndex never updated on match */
  76573. ;
  76574. }
  76575. } else {
  76576. /*
  76577. * No match, E5 Section 15.10.6.2, step 9.a.i - 9.a.ii apply, regardless
  76578. * of 'global' flag of the RegExp. In particular, if lastIndex is invalid
  76579. * initially, it is reset to zero.
  76580. */
  76581. DUK_DDD(DUK_DDDPRINT("regexp does not match"));
  76582. duk_push_null(thr);
  76583. /* [ ... re_obj input bc saved_buf res_obj ] */
  76584. duk_push_int(thr, 0);
  76585. duk_put_prop_stridx_short(thr, -6, DUK_STRIDX_LAST_INDEX);
  76586. }
  76587. /* [ ... re_obj input bc saved_buf res_obj ] */
  76588. duk_insert(thr, -5);
  76589. /* [ ... res_obj re_obj input bc saved_buf ] */
  76590. duk_pop_n_unsafe(thr, 4);
  76591. /* [ ... res_obj ] */
  76592. /* XXX: these last tricks are unnecessary if the function is made
  76593. * a genuine native function.
  76594. */
  76595. }
  76596. DUK_INTERNAL void duk_regexp_match(duk_hthread *thr) {
  76597. duk__regexp_match_helper(thr, 0 /*force_global*/);
  76598. }
  76599. /* This variant is needed by String.prototype.split(); it needs to perform
  76600. * global-style matching on a cloned RegExp which is potentially non-global.
  76601. */
  76602. DUK_INTERNAL void duk_regexp_match_force_global(duk_hthread *thr) {
  76603. duk__regexp_match_helper(thr, 1 /*force_global*/);
  76604. }
  76605. #else /* DUK_USE_REGEXP_SUPPORT */
  76606. /* regexp support disabled */
  76607. #endif /* DUK_USE_REGEXP_SUPPORT */
  76608. #line 1 "duk_selftest.c"
  76609. /*
  76610. * Self tests to ensure execution environment is sane. Intended to catch
  76611. * compiler/platform problems which cannot be detected at compile time.
  76612. */
  76613. /* #include duk_internal.h -> already included */
  76614. #if defined(DUK_USE_SELF_TESTS)
  76615. /*
  76616. * Unions and structs for self tests
  76617. */
  76618. typedef union {
  76619. double d;
  76620. duk_uint8_t x[8];
  76621. } duk__test_double_union;
  76622. /* Self test failed. Expects a local variable 'error_count' to exist. */
  76623. #define DUK__FAILED(msg) do { \
  76624. DUK_D(DUK_DPRINT("self test failed: " #msg " at " DUK_FILE_MACRO ":" DUK_MACRO_STRINGIFY(DUK_LINE_MACRO))); \
  76625. error_count++; \
  76626. } while (0)
  76627. #define DUK__DBLUNION_CMP_TRUE(a,b) do { \
  76628. if (duk_memcmp((const void *) (a), (const void *) (b), sizeof(duk__test_double_union)) != 0) { \
  76629. DUK__FAILED("double union compares false (expected true)"); \
  76630. } \
  76631. } while (0)
  76632. #define DUK__DBLUNION_CMP_FALSE(a,b) do { \
  76633. if (duk_memcmp((const void *) (a), (const void *) (b), sizeof(duk__test_double_union)) == 0) { \
  76634. DUK__FAILED("double union compares true (expected false)"); \
  76635. } \
  76636. } while (0)
  76637. typedef union {
  76638. duk_uint32_t i;
  76639. duk_uint8_t x[8];
  76640. } duk__test_u32_union;
  76641. #if defined(DUK_USE_INTEGER_LE)
  76642. #define DUK__U32_INIT(u, a, b, c, d) do { \
  76643. (u)->x[0] = (d); (u)->x[1] = (c); (u)->x[2] = (b); (u)->x[3] = (a); \
  76644. } while (0)
  76645. #elif defined(DUK_USE_INTEGER_ME)
  76646. #error integer mixed endian not supported now
  76647. #elif defined(DUK_USE_INTEGER_BE)
  76648. #define DUK__U32_INIT(u, a, b, c, d) do { \
  76649. (u)->x[0] = (a); (u)->x[1] = (b); (u)->x[2] = (c); (u)->x[3] = (d); \
  76650. } while (0)
  76651. #else
  76652. #error unknown integer endianness
  76653. #endif
  76654. #if defined(DUK_USE_DOUBLE_LE)
  76655. #define DUK__DOUBLE_INIT(u, a, b, c, d, e, f, g, h) do { \
  76656. (u)->x[0] = (h); (u)->x[1] = (g); (u)->x[2] = (f); (u)->x[3] = (e); \
  76657. (u)->x[4] = (d); (u)->x[5] = (c); (u)->x[6] = (b); (u)->x[7] = (a); \
  76658. } while (0)
  76659. #define DUK__DOUBLE_COMPARE(u, a, b, c, d, e, f, g, h) \
  76660. ((u)->x[0] == (h) && (u)->x[1] == (g) && (u)->x[2] == (f) && (u)->x[3] == (e) && \
  76661. (u)->x[4] == (d) && (u)->x[5] == (c) && (u)->x[6] == (b) && (u)->x[7] == (a))
  76662. #elif defined(DUK_USE_DOUBLE_ME)
  76663. #define DUK__DOUBLE_INIT(u, a, b, c, d, e, f, g, h) do { \
  76664. (u)->x[0] = (d); (u)->x[1] = (c); (u)->x[2] = (b); (u)->x[3] = (a); \
  76665. (u)->x[4] = (h); (u)->x[5] = (g); (u)->x[6] = (f); (u)->x[7] = (e); \
  76666. } while (0)
  76667. #define DUK__DOUBLE_COMPARE(u, a, b, c, d, e, f, g, h) \
  76668. ((u)->x[0] == (d) && (u)->x[1] == (c) && (u)->x[2] == (b) && (u)->x[3] == (a) && \
  76669. (u)->x[4] == (h) && (u)->x[5] == (g) && (u)->x[6] == (f) && (u)->x[7] == (e))
  76670. #elif defined(DUK_USE_DOUBLE_BE)
  76671. #define DUK__DOUBLE_INIT(u, a, b, c, d, e, f, g, h) do { \
  76672. (u)->x[0] = (a); (u)->x[1] = (b); (u)->x[2] = (c); (u)->x[3] = (d); \
  76673. (u)->x[4] = (e); (u)->x[5] = (f); (u)->x[6] = (g); (u)->x[7] = (h); \
  76674. } while (0)
  76675. #define DUK__DOUBLE_COMPARE(u, a, b, c, d, e, f, g, h) \
  76676. ((u)->x[0] == (a) && (u)->x[1] == (b) && (u)->x[2] == (c) && (u)->x[3] == (d) && \
  76677. (u)->x[4] == (e) && (u)->x[5] == (f) && (u)->x[6] == (g) && (u)->x[7] == (h))
  76678. #else
  76679. #error unknown double endianness
  76680. #endif
  76681. /*
  76682. * Various sanity checks for typing
  76683. */
  76684. DUK_LOCAL duk_uint_t duk__selftest_types(void) {
  76685. duk_uint_t error_count = 0;
  76686. if (!(sizeof(duk_int8_t) == 1 &&
  76687. sizeof(duk_uint8_t) == 1 &&
  76688. sizeof(duk_int16_t) == 2 &&
  76689. sizeof(duk_uint16_t) == 2 &&
  76690. sizeof(duk_int32_t) == 4 &&
  76691. sizeof(duk_uint32_t) == 4)) {
  76692. DUK__FAILED("duk_(u)int{8,16,32}_t size");
  76693. }
  76694. #if defined(DUK_USE_64BIT_OPS)
  76695. if (!(sizeof(duk_int64_t) == 8 &&
  76696. sizeof(duk_uint64_t) == 8)) {
  76697. DUK__FAILED("duk_(u)int64_t size");
  76698. }
  76699. #endif
  76700. if (!(sizeof(duk_size_t) >= sizeof(duk_uint_t))) {
  76701. /* Some internal code now assumes that all duk_uint_t values
  76702. * can be expressed with a duk_size_t.
  76703. */
  76704. DUK__FAILED("duk_size_t is smaller than duk_uint_t");
  76705. }
  76706. if (!(sizeof(duk_int_t) >= 4)) {
  76707. DUK__FAILED("duk_int_t is not 32 bits");
  76708. }
  76709. return error_count;
  76710. }
  76711. /*
  76712. * Packed tval sanity
  76713. */
  76714. DUK_LOCAL duk_uint_t duk__selftest_packed_tval(void) {
  76715. duk_uint_t error_count = 0;
  76716. #if defined(DUK_USE_PACKED_TVAL)
  76717. if (sizeof(void *) > 4) {
  76718. DUK__FAILED("packed duk_tval in use but sizeof(void *) > 4");
  76719. }
  76720. #endif
  76721. return error_count;
  76722. }
  76723. /*
  76724. * Two's complement arithmetic.
  76725. */
  76726. DUK_LOCAL duk_uint_t duk__selftest_twos_complement(void) {
  76727. duk_uint_t error_count = 0;
  76728. volatile int test;
  76729. test = -1;
  76730. /* Note that byte order doesn't affect this test: all bytes in
  76731. * 'test' will be 0xFF for two's complement.
  76732. */
  76733. if (((volatile duk_uint8_t *) &test)[0] != (duk_uint8_t) 0xff) {
  76734. DUK__FAILED("two's complement arithmetic");
  76735. }
  76736. return error_count;
  76737. }
  76738. /*
  76739. * Byte order. Important to self check, because on some exotic platforms
  76740. * there is no actual detection but rather assumption based on platform
  76741. * defines.
  76742. */
  76743. DUK_LOCAL duk_uint_t duk__selftest_byte_order(void) {
  76744. duk_uint_t error_count = 0;
  76745. duk__test_u32_union u1;
  76746. duk__test_double_union u2;
  76747. /*
  76748. * >>> struct.pack('>d', 102030405060).encode('hex')
  76749. * '4237c17c6dc40000'
  76750. */
  76751. DUK__U32_INIT(&u1, 0xde, 0xad, 0xbe, 0xef);
  76752. DUK__DOUBLE_INIT(&u2, 0x42, 0x37, 0xc1, 0x7c, 0x6d, 0xc4, 0x00, 0x00);
  76753. if (u1.i != (duk_uint32_t) 0xdeadbeefUL) {
  76754. DUK__FAILED("duk_uint32_t byte order");
  76755. }
  76756. if (u2.d != (double) 102030405060.0) {
  76757. DUK__FAILED("double byte order");
  76758. }
  76759. return error_count;
  76760. }
  76761. /*
  76762. * DUK_BSWAP macros
  76763. */
  76764. DUK_LOCAL duk_uint_t duk__selftest_bswap_macros(void) {
  76765. duk_uint_t error_count = 0;
  76766. duk_uint32_t x32;
  76767. duk_uint16_t x16;
  76768. duk_double_union du;
  76769. duk_double_t du_diff;
  76770. x16 = 0xbeefUL;
  76771. x16 = DUK_BSWAP16(x16);
  76772. if (x16 != (duk_uint16_t) 0xefbeUL) {
  76773. DUK__FAILED("DUK_BSWAP16");
  76774. }
  76775. x32 = 0xdeadbeefUL;
  76776. x32 = DUK_BSWAP32(x32);
  76777. if (x32 != (duk_uint32_t) 0xefbeaddeUL) {
  76778. DUK__FAILED("DUK_BSWAP32");
  76779. }
  76780. /* >>> struct.unpack('>d', '4000112233445566'.decode('hex'))
  76781. * (2.008366013071895,)
  76782. */
  76783. du.uc[0] = 0x40; du.uc[1] = 0x00; du.uc[2] = 0x11; du.uc[3] = 0x22;
  76784. du.uc[4] = 0x33; du.uc[5] = 0x44; du.uc[6] = 0x55; du.uc[7] = 0x66;
  76785. DUK_DBLUNION_DOUBLE_NTOH(&du);
  76786. du_diff = du.d - 2.008366013071895;
  76787. #if 0
  76788. DUK_D(DUK_DPRINT("du_diff: %lg\n", (double) du_diff));
  76789. #endif
  76790. if (du_diff > 1e-15) {
  76791. /* Allow very small lenience because some compilers won't parse
  76792. * exact IEEE double constants (happened in matrix testing with
  76793. * Linux gcc-4.8 -m32 at least).
  76794. */
  76795. #if 0
  76796. DUK_D(DUK_DPRINT("Result of DUK_DBLUNION_DOUBLE_NTOH: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  76797. (unsigned int) du.uc[0], (unsigned int) du.uc[1],
  76798. (unsigned int) du.uc[2], (unsigned int) du.uc[3],
  76799. (unsigned int) du.uc[4], (unsigned int) du.uc[5],
  76800. (unsigned int) du.uc[6], (unsigned int) du.uc[7]));
  76801. #endif
  76802. DUK__FAILED("DUK_DBLUNION_DOUBLE_NTOH");
  76803. }
  76804. return error_count;
  76805. }
  76806. /*
  76807. * Basic double / byte union memory layout.
  76808. */
  76809. DUK_LOCAL duk_uint_t duk__selftest_double_union_size(void) {
  76810. duk_uint_t error_count = 0;
  76811. if (sizeof(duk__test_double_union) != 8) {
  76812. DUK__FAILED("invalid union size");
  76813. }
  76814. return error_count;
  76815. }
  76816. /*
  76817. * Union aliasing, see misc/clang_aliasing.c.
  76818. */
  76819. DUK_LOCAL duk_uint_t duk__selftest_double_aliasing(void) {
  76820. /* This testcase fails when Emscripten-generated code runs on Firefox.
  76821. * It's not an issue because the failure should only affect packed
  76822. * duk_tval representation, which is not used with Emscripten.
  76823. */
  76824. #if defined(DUK_USE_PACKED_TVAL)
  76825. duk_uint_t error_count = 0;
  76826. duk__test_double_union a, b;
  76827. /* Test signaling NaN and alias assignment in all endianness combinations.
  76828. */
  76829. /* little endian */
  76830. a.x[0] = 0x11; a.x[1] = 0x22; a.x[2] = 0x33; a.x[3] = 0x44;
  76831. a.x[4] = 0x00; a.x[5] = 0x00; a.x[6] = 0xf1; a.x[7] = 0xff;
  76832. b = a;
  76833. DUK__DBLUNION_CMP_TRUE(&a, &b);
  76834. /* big endian */
  76835. a.x[0] = 0xff; a.x[1] = 0xf1; a.x[2] = 0x00; a.x[3] = 0x00;
  76836. a.x[4] = 0x44; a.x[5] = 0x33; a.x[6] = 0x22; a.x[7] = 0x11;
  76837. b = a;
  76838. DUK__DBLUNION_CMP_TRUE(&a, &b);
  76839. /* mixed endian */
  76840. a.x[0] = 0x00; a.x[1] = 0x00; a.x[2] = 0xf1; a.x[3] = 0xff;
  76841. a.x[4] = 0x11; a.x[5] = 0x22; a.x[6] = 0x33; a.x[7] = 0x44;
  76842. b = a;
  76843. DUK__DBLUNION_CMP_TRUE(&a, &b);
  76844. return error_count;
  76845. #else
  76846. DUK_D(DUK_DPRINT("skip double aliasing self test when duk_tval is not packed"));
  76847. return 0;
  76848. #endif
  76849. }
  76850. /*
  76851. * Zero sign, see misc/tcc_zerosign2.c.
  76852. */
  76853. DUK_LOCAL duk_uint_t duk__selftest_double_zero_sign(void) {
  76854. duk_uint_t error_count = 0;
  76855. duk__test_double_union a, b;
  76856. a.d = 0.0;
  76857. b.d = -a.d;
  76858. DUK__DBLUNION_CMP_FALSE(&a, &b);
  76859. return error_count;
  76860. }
  76861. /*
  76862. * Rounding mode: Duktape assumes round-to-nearest, check that this is true.
  76863. * If we had C99 fenv.h we could check that fegetround() == FE_TONEAREST,
  76864. * but we don't want to rely on that header; and even if we did, it's good
  76865. * to ensure the rounding actually works.
  76866. */
  76867. DUK_LOCAL duk_uint_t duk__selftest_double_rounding(void) {
  76868. duk_uint_t error_count = 0;
  76869. duk__test_double_union a, b, c;
  76870. #if 0
  76871. /* Include <fenv.h> and test manually; these trigger failures: */
  76872. fesetround(FE_UPWARD);
  76873. fesetround(FE_DOWNWARD);
  76874. fesetround(FE_TOWARDZERO);
  76875. /* This is the default and passes. */
  76876. fesetround(FE_TONEAREST);
  76877. #endif
  76878. /* Rounding tests check that none of the other modes (round to
  76879. * +Inf, round to -Inf, round to zero) can be active:
  76880. * http://www.gnu.org/software/libc/manual/html_node/Rounding.html
  76881. */
  76882. /* 1.0 + 2^(-53): result is midway between 1.0 and 1.0 + ulp.
  76883. * Round to nearest: 1.0
  76884. * Round to +Inf: 1.0 + ulp
  76885. * Round to -Inf: 1.0
  76886. * Round to zero: 1.0
  76887. * => Correct result eliminates round to +Inf.
  76888. */
  76889. DUK__DOUBLE_INIT(&a, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  76890. DUK__DOUBLE_INIT(&b, 0x3c, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  76891. duk_memset((void *) &c, 0, sizeof(c));
  76892. c.d = a.d + b.d;
  76893. if (!DUK__DOUBLE_COMPARE(&c, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)) {
  76894. DUK_D(DUK_DPRINT("broken result (native endiannesss): %02x %02x %02x %02x %02x %02x %02x %02x",
  76895. (unsigned int) c.x[0], (unsigned int) c.x[1],
  76896. (unsigned int) c.x[2], (unsigned int) c.x[3],
  76897. (unsigned int) c.x[4], (unsigned int) c.x[5],
  76898. (unsigned int) c.x[6], (unsigned int) c.x[7]));
  76899. DUK__FAILED("invalid result from 1.0 + 0.5ulp");
  76900. }
  76901. /* (1.0 + ulp) + 2^(-53): result is midway between 1.0 + ulp and 1.0 + 2*ulp.
  76902. * Round to nearest: 1.0 + 2*ulp (round to even mantissa)
  76903. * Round to +Inf: 1.0 + 2*ulp
  76904. * Round to -Inf: 1.0 + ulp
  76905. * Round to zero: 1.0 + ulp
  76906. * => Correct result eliminates round to -Inf and round to zero.
  76907. */
  76908. DUK__DOUBLE_INIT(&a, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
  76909. DUK__DOUBLE_INIT(&b, 0x3c, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
  76910. duk_memset((void *) &c, 0, sizeof(c));
  76911. c.d = a.d + b.d;
  76912. if (!DUK__DOUBLE_COMPARE(&c, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02)) {
  76913. DUK_D(DUK_DPRINT("broken result (native endiannesss): %02x %02x %02x %02x %02x %02x %02x %02x",
  76914. (unsigned int) c.x[0], (unsigned int) c.x[1],
  76915. (unsigned int) c.x[2], (unsigned int) c.x[3],
  76916. (unsigned int) c.x[4], (unsigned int) c.x[5],
  76917. (unsigned int) c.x[6], (unsigned int) c.x[7]));
  76918. DUK__FAILED("invalid result from (1.0 + ulp) + 0.5ulp");
  76919. }
  76920. /* Could do negative number testing too, but the tests above should
  76921. * differentiate between IEEE 754 rounding modes.
  76922. */
  76923. return error_count;
  76924. }
  76925. /*
  76926. * fmod(): often a portability issue in embedded or bare platform targets.
  76927. * Check for at least minimally correct behavior. Unlike some other math
  76928. * functions (like cos()) Duktape relies on fmod() internally too.
  76929. */
  76930. DUK_LOCAL duk_uint_t duk__selftest_fmod(void) {
  76931. duk_uint_t error_count = 0;
  76932. duk__test_double_union u1, u2;
  76933. volatile duk_double_t t1, t2, t3;
  76934. /* fmod() with integer argument and exponent 2^32 is used by e.g.
  76935. * ToUint32() and some Duktape internals.
  76936. */
  76937. u1.d = DUK_FMOD(10.0, 4294967296.0);
  76938. u2.d = 10.0;
  76939. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76940. u1.d = DUK_FMOD(4294967306.0, 4294967296.0);
  76941. u2.d = 10.0;
  76942. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76943. u1.d = DUK_FMOD(73014444042.0, 4294967296.0);
  76944. u2.d = 10.0;
  76945. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76946. /* 52-bit integer split into two parts:
  76947. * >>> 0x1fedcba9876543
  76948. * 8987183256397123
  76949. * >>> float(0x1fedcba9876543) / float(2**53)
  76950. * 0.9977777777777778
  76951. */
  76952. u1.d = DUK_FMOD(8987183256397123.0, 4294967296.0);
  76953. u2.d = (duk_double_t) 0xa9876543UL;
  76954. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76955. t1 = 8987183256397123.0;
  76956. t2 = 4294967296.0;
  76957. t3 = t1 / t2;
  76958. u1.d = DUK_FLOOR(t3);
  76959. u2.d = (duk_double_t) 0x1fedcbUL;
  76960. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76961. /* C99 behavior is for fmod() result sign to mathc argument sign. */
  76962. u1.d = DUK_FMOD(-10.0, 4294967296.0);
  76963. u2.d = -10.0;
  76964. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76965. u1.d = DUK_FMOD(-4294967306.0, 4294967296.0);
  76966. u2.d = -10.0;
  76967. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76968. u1.d = DUK_FMOD(-73014444042.0, 4294967296.0);
  76969. u2.d = -10.0;
  76970. DUK__DBLUNION_CMP_TRUE(&u1, &u2);
  76971. return error_count;
  76972. }
  76973. /*
  76974. * Struct size/alignment if platform requires it
  76975. *
  76976. * There are some compiler specific struct padding pragmas etc in use, this
  76977. * selftest ensures they're correctly detected and used.
  76978. */
  76979. DUK_LOCAL duk_uint_t duk__selftest_struct_align(void) {
  76980. duk_uint_t error_count = 0;
  76981. #if (DUK_USE_ALIGN_BY == 4)
  76982. if ((sizeof(duk_hbuffer_fixed) % 4) != 0) {
  76983. DUK__FAILED("sizeof(duk_hbuffer_fixed) not aligned to 4");
  76984. }
  76985. #elif (DUK_USE_ALIGN_BY == 8)
  76986. if ((sizeof(duk_hbuffer_fixed) % 8) != 0) {
  76987. DUK__FAILED("sizeof(duk_hbuffer_fixed) not aligned to 8");
  76988. }
  76989. #elif (DUK_USE_ALIGN_BY == 1)
  76990. /* no check */
  76991. #else
  76992. #error invalid DUK_USE_ALIGN_BY
  76993. #endif
  76994. return error_count;
  76995. }
  76996. /*
  76997. * 64-bit arithmetic
  76998. *
  76999. * There are some platforms/compilers where 64-bit types are available
  77000. * but don't work correctly. Test for known cases.
  77001. */
  77002. DUK_LOCAL duk_uint_t duk__selftest_64bit_arithmetic(void) {
  77003. duk_uint_t error_count = 0;
  77004. #if defined(DUK_USE_64BIT_OPS)
  77005. volatile duk_int64_t i;
  77006. volatile duk_double_t d;
  77007. /* Catch a double-to-int64 cast issue encountered in practice. */
  77008. d = 2147483648.0;
  77009. i = (duk_int64_t) d;
  77010. if (i != DUK_I64_CONSTANT(0x80000000)) {
  77011. DUK__FAILED("casting 2147483648.0 to duk_int64_t failed");
  77012. }
  77013. #else
  77014. /* nop */
  77015. #endif
  77016. return error_count;
  77017. }
  77018. /*
  77019. * Casting
  77020. */
  77021. DUK_LOCAL duk_uint_t duk__selftest_cast_double_to_small_uint(void) {
  77022. /*
  77023. * https://github.com/svaarala/duktape/issues/127#issuecomment-77863473
  77024. */
  77025. duk_uint_t error_count = 0;
  77026. duk_double_t d1, d2;
  77027. duk_small_uint_t u;
  77028. duk_double_t d1v, d2v;
  77029. duk_small_uint_t uv;
  77030. /* Test without volatiles */
  77031. d1 = 1.0;
  77032. u = (duk_small_uint_t) d1;
  77033. d2 = (duk_double_t) u;
  77034. if (!(d1 == 1.0 && u == 1 && d2 == 1.0 && d1 == d2)) {
  77035. DUK__FAILED("double to duk_small_uint_t cast failed");
  77036. }
  77037. /* Same test with volatiles */
  77038. d1v = 1.0;
  77039. uv = (duk_small_uint_t) d1v;
  77040. d2v = (duk_double_t) uv;
  77041. if (!(d1v == 1.0 && uv == 1 && d2v == 1.0 && d1v == d2v)) {
  77042. DUK__FAILED("double to duk_small_uint_t cast failed");
  77043. }
  77044. return error_count;
  77045. }
  77046. DUK_LOCAL duk_uint_t duk__selftest_cast_double_to_uint32(void) {
  77047. /*
  77048. * This test fails on an exotic ARM target; double-to-uint
  77049. * cast is incorrectly clamped to -signed- int highest value.
  77050. *
  77051. * https://github.com/svaarala/duktape/issues/336
  77052. */
  77053. duk_uint_t error_count = 0;
  77054. duk_double_t dv;
  77055. duk_uint32_t uv;
  77056. dv = 3735928559.0; /* 0xdeadbeef in decimal */
  77057. uv = (duk_uint32_t) dv;
  77058. if (uv != 0xdeadbeefUL) {
  77059. DUK__FAILED("double to duk_uint32_t cast failed");
  77060. }
  77061. return error_count;
  77062. }
  77063. /*
  77064. * Minimal test of user supplied allocation functions
  77065. *
  77066. * - Basic alloc + realloc + free cycle
  77067. *
  77068. * - Realloc to significantly larger size to (hopefully) trigger a
  77069. * relocation and check that relocation copying works
  77070. */
  77071. DUK_LOCAL duk_uint_t duk__selftest_alloc_funcs(duk_alloc_function alloc_func,
  77072. duk_realloc_function realloc_func,
  77073. duk_free_function free_func,
  77074. void *udata) {
  77075. duk_uint_t error_count = 0;
  77076. void *ptr;
  77077. void *new_ptr;
  77078. duk_small_int_t i, j;
  77079. unsigned char x;
  77080. if (alloc_func == NULL || realloc_func == NULL || free_func == NULL) {
  77081. return 0;
  77082. }
  77083. for (i = 1; i <= 256; i++) {
  77084. ptr = alloc_func(udata, (duk_size_t) i);
  77085. if (ptr == NULL) {
  77086. DUK_D(DUK_DPRINT("alloc failed, ignore"));
  77087. continue; /* alloc failed, ignore */
  77088. }
  77089. for (j = 0; j < i; j++) {
  77090. ((unsigned char *) ptr)[j] = (unsigned char) (0x80 + j);
  77091. }
  77092. new_ptr = realloc_func(udata, ptr, 1024);
  77093. if (new_ptr == NULL) {
  77094. DUK_D(DUK_DPRINT("realloc failed, ignore"));
  77095. free_func(udata, ptr);
  77096. continue; /* realloc failed, ignore */
  77097. }
  77098. ptr = new_ptr;
  77099. for (j = 0; j < i; j++) {
  77100. x = ((unsigned char *) ptr)[j];
  77101. if (x != (unsigned char) (0x80 + j)) {
  77102. DUK_D(DUK_DPRINT("byte at index %ld doesn't match after realloc: %02lx",
  77103. (long) j, (unsigned long) x));
  77104. DUK__FAILED("byte compare after realloc");
  77105. break;
  77106. }
  77107. }
  77108. free_func(udata, ptr);
  77109. }
  77110. return error_count;
  77111. }
  77112. /*
  77113. * Self test main
  77114. */
  77115. DUK_INTERNAL duk_uint_t duk_selftest_run_tests(duk_alloc_function alloc_func,
  77116. duk_realloc_function realloc_func,
  77117. duk_free_function free_func,
  77118. void *udata) {
  77119. duk_uint_t error_count = 0;
  77120. DUK_D(DUK_DPRINT("self test starting"));
  77121. error_count += duk__selftest_types();
  77122. error_count += duk__selftest_packed_tval();
  77123. error_count += duk__selftest_twos_complement();
  77124. error_count += duk__selftest_byte_order();
  77125. error_count += duk__selftest_bswap_macros();
  77126. error_count += duk__selftest_double_union_size();
  77127. error_count += duk__selftest_double_aliasing();
  77128. error_count += duk__selftest_double_zero_sign();
  77129. error_count += duk__selftest_double_rounding();
  77130. error_count += duk__selftest_fmod();
  77131. error_count += duk__selftest_struct_align();
  77132. error_count += duk__selftest_64bit_arithmetic();
  77133. error_count += duk__selftest_cast_double_to_small_uint();
  77134. error_count += duk__selftest_cast_double_to_uint32();
  77135. error_count += duk__selftest_alloc_funcs(alloc_func, realloc_func, free_func, udata);
  77136. DUK_D(DUK_DPRINT("self test complete, total error count: %ld", (long) error_count));
  77137. return error_count;
  77138. }
  77139. #endif /* DUK_USE_SELF_TESTS */
  77140. /* automatic undefs */
  77141. #undef DUK__DBLUNION_CMP_FALSE
  77142. #undef DUK__DBLUNION_CMP_TRUE
  77143. #undef DUK__DOUBLE_COMPARE
  77144. #undef DUK__DOUBLE_INIT
  77145. #undef DUK__FAILED
  77146. #undef DUK__U32_INIT
  77147. /* #include duk_internal.h -> already included */
  77148. #line 2 "duk_tval.c"
  77149. #if defined(DUK_USE_FASTINT)
  77150. /*
  77151. * Manually optimized double-to-fastint downgrade check.
  77152. *
  77153. * This check has a large impact on performance, especially for fastint
  77154. * slow paths, so must be changed carefully. The code should probably be
  77155. * optimized for the case where the result does not fit into a fastint,
  77156. * to minimize the penalty for "slow path code" dealing with fractions etc.
  77157. *
  77158. * At least on one tested soft float ARM platform double-to-int64 coercion
  77159. * is very slow (and sometimes produces incorrect results, see self tests).
  77160. * This algorithm combines a fastint compatibility check and extracting the
  77161. * integer value from an IEEE double for setting the tagged fastint. For
  77162. * other platforms a more naive approach might be better.
  77163. *
  77164. * See doc/fastint.rst for details.
  77165. */
  77166. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_tval_set_number_chkfast_fast(duk_tval *tv, duk_double_t x) {
  77167. duk_double_union du;
  77168. duk_int64_t i;
  77169. duk_small_int_t expt;
  77170. duk_small_int_t shift;
  77171. /* XXX: optimize for packed duk_tval directly? */
  77172. du.d = x;
  77173. i = (duk_int64_t) DUK_DBLUNION_GET_INT64(&du);
  77174. expt = (duk_small_int_t) ((i >> 52) & 0x07ff);
  77175. shift = expt - 1023;
  77176. if (shift >= 0 && shift <= 46) { /* exponents 1023 to 1069 */
  77177. duk_int64_t t;
  77178. if (((DUK_I64_CONSTANT(0x000fffffffffffff) >> shift) & i) == 0) {
  77179. t = i | DUK_I64_CONSTANT(0x0010000000000000); /* implicit leading one */
  77180. t = t & DUK_I64_CONSTANT(0x001fffffffffffff);
  77181. t = t >> (52 - shift);
  77182. if (i < 0) {
  77183. t = -t;
  77184. }
  77185. DUK_TVAL_SET_FASTINT(tv, t);
  77186. return;
  77187. }
  77188. } else if (shift == -1023) { /* exponent 0 */
  77189. if (i >= 0 && (i & DUK_I64_CONSTANT(0x000fffffffffffff)) == 0) {
  77190. /* Note: reject negative zero. */
  77191. DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) 0);
  77192. return;
  77193. }
  77194. } else if (shift == 47) { /* exponent 1070 */
  77195. if (i < 0 && (i & DUK_I64_CONSTANT(0x000fffffffffffff)) == 0) {
  77196. DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) DUK_FASTINT_MIN);
  77197. return;
  77198. }
  77199. }
  77200. DUK_TVAL_SET_DOUBLE(tv, x);
  77201. return;
  77202. }
  77203. DUK_INTERNAL DUK_NOINLINE void duk_tval_set_number_chkfast_slow(duk_tval *tv, duk_double_t x) {
  77204. duk_tval_set_number_chkfast_fast(tv, x);
  77205. }
  77206. /*
  77207. * Manually optimized number-to-double conversion
  77208. */
  77209. #if defined(DUK_USE_FASTINT) && defined(DUK_USE_PACKED_TVAL)
  77210. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_packed(duk_tval *tv) {
  77211. duk_double_union du;
  77212. duk_uint64_t t;
  77213. t = (duk_uint64_t) DUK_DBLUNION_GET_UINT64(tv);
  77214. if ((t >> 48) != DUK_TAG_FASTINT) {
  77215. return tv->d;
  77216. } else if (t & DUK_U64_CONSTANT(0x0000800000000000)) {
  77217. t = (duk_uint64_t) (-((duk_int64_t) t)); /* avoid unary minus on unsigned */
  77218. t = t & DUK_U64_CONSTANT(0x0000ffffffffffff); /* negative */
  77219. t |= DUK_U64_CONSTANT(0xc330000000000000);
  77220. DUK_DBLUNION_SET_UINT64(&du, t);
  77221. return du.d + 4503599627370496.0; /* 1 << 52 */
  77222. } else if (t != 0) {
  77223. t &= DUK_U64_CONSTANT(0x0000ffffffffffff); /* positive */
  77224. t |= DUK_U64_CONSTANT(0x4330000000000000);
  77225. DUK_DBLUNION_SET_UINT64(&du, t);
  77226. return du.d - 4503599627370496.0; /* 1 << 52 */
  77227. } else {
  77228. return 0.0; /* zero */
  77229. }
  77230. }
  77231. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  77232. #if 0 /* unused */
  77233. #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
  77234. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked(duk_tval *tv) {
  77235. duk_double_union du;
  77236. duk_uint64_t t;
  77237. DUK_ASSERT(tv->t == DUK_TAG_NUMBER || tv->t == DUK_TAG_FASTINT);
  77238. if (tv->t == DUK_TAG_FASTINT) {
  77239. if (tv->v.fi >= 0) {
  77240. t = DUK_U64_CONSTANT(0x4330000000000000) | (duk_uint64_t) tv->v.fi;
  77241. DUK_DBLUNION_SET_UINT64(&du, t);
  77242. return du.d - 4503599627370496.0; /* 1 << 52 */
  77243. } else {
  77244. t = DUK_U64_CONSTANT(0xc330000000000000) | (duk_uint64_t) (-tv->v.fi);
  77245. DUK_DBLUNION_SET_UINT64(&du, t);
  77246. return du.d + 4503599627370496.0; /* 1 << 52 */
  77247. }
  77248. } else {
  77249. return tv->v.d;
  77250. }
  77251. }
  77252. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  77253. #endif /* 0 */
  77254. #if defined(DUK_USE_FASTINT) && !defined(DUK_USE_PACKED_TVAL)
  77255. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv) {
  77256. duk_double_union du;
  77257. duk_uint64_t t;
  77258. DUK_ASSERT(tv->t == DUK_TAG_FASTINT);
  77259. if (tv->v.fi >= 0) {
  77260. t = DUK_U64_CONSTANT(0x4330000000000000) | (duk_uint64_t) tv->v.fi;
  77261. DUK_DBLUNION_SET_UINT64(&du, t);
  77262. return du.d - 4503599627370496.0; /* 1 << 52 */
  77263. } else {
  77264. t = DUK_U64_CONSTANT(0xc330000000000000) | (duk_uint64_t) (-tv->v.fi);
  77265. DUK_DBLUNION_SET_UINT64(&du, t);
  77266. return du.d + 4503599627370496.0; /* 1 << 52 */
  77267. }
  77268. }
  77269. #endif /* DUK_USE_FASTINT && DUK_USE_PACKED_TVAL */
  77270. #endif /* DUK_USE_FASTINT */
  77271. #line 1 "duk_unicode_tables.c"
  77272. /*
  77273. * Unicode support tables automatically generated during build.
  77274. */
  77275. /* #include duk_internal.h -> already included */
  77276. /*
  77277. * Unicode tables containing ranges of Unicode characters in a
  77278. * packed format. These tables are used to match non-ASCII
  77279. * characters of complex productions by resorting to a linear
  77280. * range-by-range comparison. This is very slow, but is expected
  77281. * to be very rare in practical ECMAScript source code, and thus
  77282. * compactness is most important.
  77283. *
  77284. * The tables are matched using uni_range_match() and the format
  77285. * is described in tools/extract_chars.py.
  77286. */
  77287. #if defined(DUK_USE_SOURCE_NONBMP)
  77288. /* IdentifierStart production with ASCII excluded */
  77289. /* duk_unicode_ids_noa[] */
  77290. /*
  77291. * Automatically generated by extract_chars.py, do not edit!
  77292. */
  77293. const duk_uint8_t duk_unicode_ids_noa[1063] = {
  77294. 249,176,176,80,111,7,47,15,47,254,11,197,191,0,72,2,15,115,66,19,50,7,2,34,
  77295. 2,240,66,244,50,247,185,249,98,241,99,8,241,127,58,240,182,47,31,241,191,
  77296. 21,18,245,50,15,1,24,27,35,15,2,2,240,239,15,244,156,15,10,241,26,21,6,240,
  77297. 101,10,4,15,9,240,152,175,39,240,82,127,56,242,100,15,4,8,159,1,240,5,115,
  77298. 19,240,98,98,4,52,15,2,14,18,47,0,27,9,85,19,240,98,98,18,18,31,17,50,15,5,
  77299. 47,2,130,34,240,98,98,18,68,15,4,15,1,31,9,12,115,19,240,98,98,18,68,15,16,
  77300. 18,47,1,15,3,2,84,34,52,18,2,20,20,36,191,8,15,38,114,34,240,114,240,4,15,
  77301. 12,38,31,16,5,114,34,240,114,146,68,15,18,2,31,1,31,4,114,34,241,147,15,2,
  77302. 6,41,47,10,86,240,36,240,130,130,3,111,44,242,2,29,111,44,18,3,18,3,7,50,
  77303. 98,34,2,3,18,50,26,3,66,15,7,63,18,15,49,114,241,79,13,79,101,241,191,6,15,
  77304. 2,85,52,4,24,37,205,15,3,241,98,6,3,241,178,255,224,63,35,54,32,35,63,25,
  77305. 35,63,17,35,54,32,35,62,47,41,35,63,51,241,127,0,240,47,70,53,79,254,21,
  77306. 227,240,18,240,166,243,180,168,194,63,0,240,47,0,240,47,0,194,47,1,242,79,
  77307. 21,5,15,53,244,137,67,241,34,6,243,107,240,255,35,240,227,76,241,197,240,
  77308. 175,40,240,122,242,95,68,15,79,241,255,3,111,41,240,238,27,241,207,12,241,
  77309. 79,27,43,241,67,143,82,50,52,26,251,15,50,255,224,8,53,63,22,53,55,32,32,
  77310. 32,47,15,63,37,38,32,66,38,67,53,92,98,38,246,96,224,240,44,245,112,80,57,
  77311. 32,68,112,32,32,35,42,51,100,80,240,63,25,255,233,107,241,242,241,242,247,
  77312. 87,52,29,241,98,6,3,242,136,15,2,240,122,98,98,98,98,98,98,98,111,66,15,
  77313. 254,12,146,240,184,132,52,95,70,114,47,74,35,111,26,63,78,240,63,11,242,
  77314. 127,0,255,224,244,255,240,0,138,143,60,255,240,4,13,223,7,255,227,127,243,
  77315. 95,30,63,253,79,0,177,240,111,31,240,47,15,63,64,241,152,63,87,63,20,39,
  77316. 243,26,34,35,47,7,240,255,36,240,15,34,243,5,64,32,223,12,191,7,240,191,13,
  77317. 143,31,240,224,240,36,41,180,47,25,240,146,39,240,111,7,64,79,34,32,65,52,
  77318. 48,32,240,162,58,130,213,53,53,166,38,47,27,41,191,99,240,255,255,0,26,150,
  77319. 223,7,95,33,255,240,0,255,143,254,6,3,245,175,24,109,70,2,146,194,66,2,18,
  77320. 18,245,207,19,255,224,93,240,79,48,63,38,241,171,246,100,47,119,241,111,10,
  77321. 127,10,207,73,69,53,53,50,241,91,47,10,47,3,33,46,61,241,79,107,243,127,37,
  77322. 255,223,13,79,33,242,31,16,239,14,111,22,191,14,63,20,87,36,241,207,142,
  77323. 240,79,20,95,20,95,24,159,36,248,239,254,2,154,240,107,127,138,83,2,241,
  77324. 194,20,3,240,123,240,122,240,255,51,240,50,27,240,107,240,175,56,242,135,
  77325. 31,50,15,1,50,34,240,191,30,240,212,240,223,21,114,240,207,13,242,107,240,
  77326. 107,240,62,240,47,96,243,159,41,242,62,242,63,254,32,79,37,243,223,29,241,
  77327. 47,9,240,207,20,241,191,19,64,223,32,240,3,240,112,32,241,95,2,47,9,244,
  77328. 102,32,35,46,41,143,31,241,135,49,63,6,38,33,36,64,240,64,212,249,15,37,
  77329. 240,67,242,127,32,240,97,32,250,175,31,241,179,241,111,32,240,96,242,223,
  77330. 27,244,127,10,255,224,122,243,15,17,15,242,11,241,136,15,7,12,241,131,63,
  77331. 40,242,159,249,130,241,95,3,15,35,240,239,98,98,18,241,111,7,15,254,26,223,
  77332. 254,40,207,88,245,255,3,251,79,254,155,15,254,50,31,254,236,95,254,19,159,
  77333. 255,0,16,173,255,225,43,143,15,246,63,14,240,79,32,240,35,241,31,5,111,3,
  77334. 255,226,100,243,92,15,52,207,50,31,16,255,240,0,109,255,5,255,225,229,255,
  77335. 240,1,64,31,254,1,31,67,255,224,126,255,231,248,245,182,196,136,159,255,0,
  77336. 6,90,244,82,243,114,19,3,19,50,178,2,98,243,18,51,114,98,240,194,50,66,4,
  77337. 98,255,224,70,63,9,47,9,47,15,47,9,47,15,47,9,47,15,47,9,47,15,47,9,39,255,
  77338. 239,40,251,95,45,243,79,254,59,3,47,11,33,32,48,41,35,32,32,112,80,32,32,
  77339. 34,33,32,48,32,32,32,32,33,32,51,38,35,35,32,41,47,1,98,36,47,1,255,240,0,
  77340. 3,143,255,0,149,201,241,191,254,242,124,252,227,255,240,0,87,79,0,255,240,
  77341. 0,194,63,254,177,63,254,17,0,
  77342. };
  77343. #else
  77344. /* IdentifierStart production with ASCII and non-BMP excluded */
  77345. /* duk_unicode_ids_noabmp[] */
  77346. /*
  77347. * Automatically generated by extract_chars.py, do not edit!
  77348. */
  77349. const duk_uint8_t duk_unicode_ids_noabmp[626] = {
  77350. 249,176,176,80,111,7,47,15,47,254,11,197,191,0,72,2,15,115,66,19,50,7,2,34,
  77351. 2,240,66,244,50,247,185,249,98,241,99,8,241,127,58,240,182,47,31,241,191,
  77352. 21,18,245,50,15,1,24,27,35,15,2,2,240,239,15,244,156,15,10,241,26,21,6,240,
  77353. 101,10,4,15,9,240,152,175,39,240,82,127,56,242,100,15,4,8,159,1,240,5,115,
  77354. 19,240,98,98,4,52,15,2,14,18,47,0,27,9,85,19,240,98,98,18,18,31,17,50,15,5,
  77355. 47,2,130,34,240,98,98,18,68,15,4,15,1,31,9,12,115,19,240,98,98,18,68,15,16,
  77356. 18,47,1,15,3,2,84,34,52,18,2,20,20,36,191,8,15,38,114,34,240,114,240,4,15,
  77357. 12,38,31,16,5,114,34,240,114,146,68,15,18,2,31,1,31,4,114,34,241,147,15,2,
  77358. 6,41,47,10,86,240,36,240,130,130,3,111,44,242,2,29,111,44,18,3,18,3,7,50,
  77359. 98,34,2,3,18,50,26,3,66,15,7,63,18,15,49,114,241,79,13,79,101,241,191,6,15,
  77360. 2,85,52,4,24,37,205,15,3,241,98,6,3,241,178,255,224,63,35,54,32,35,63,25,
  77361. 35,63,17,35,54,32,35,62,47,41,35,63,51,241,127,0,240,47,70,53,79,254,21,
  77362. 227,240,18,240,166,243,180,168,194,63,0,240,47,0,240,47,0,194,47,1,242,79,
  77363. 21,5,15,53,244,137,67,241,34,6,243,107,240,255,35,240,227,76,241,197,240,
  77364. 175,40,240,122,242,95,68,15,79,241,255,3,111,41,240,238,27,241,207,12,241,
  77365. 79,27,43,241,67,143,82,50,52,26,251,15,50,255,224,8,53,63,22,53,55,32,32,
  77366. 32,47,15,63,37,38,32,66,38,67,53,92,98,38,246,96,224,240,44,245,112,80,57,
  77367. 32,68,112,32,32,35,42,51,100,80,240,63,25,255,233,107,241,242,241,242,247,
  77368. 87,52,29,241,98,6,3,242,136,15,2,240,122,98,98,98,98,98,98,98,111,66,15,
  77369. 254,12,146,240,184,132,52,95,70,114,47,74,35,111,26,63,78,240,63,11,242,
  77370. 127,0,255,224,244,255,240,0,138,143,60,255,240,4,13,223,7,255,227,127,243,
  77371. 95,30,63,253,79,0,177,240,111,31,240,47,15,63,64,241,152,63,87,63,20,39,
  77372. 243,26,34,35,47,7,240,255,36,240,15,34,243,5,64,32,223,12,191,7,240,191,13,
  77373. 143,31,240,224,240,36,41,180,47,25,240,146,39,240,111,7,64,79,34,32,65,52,
  77374. 48,32,240,162,58,130,213,53,53,166,38,47,27,41,191,99,240,255,255,0,26,150,
  77375. 223,7,95,33,255,240,0,255,143,254,6,3,245,175,24,109,70,2,146,194,66,2,18,
  77376. 18,245,207,19,255,224,93,240,79,48,63,38,241,171,246,100,47,119,241,111,10,
  77377. 127,10,207,73,69,53,53,50,0,
  77378. };
  77379. #endif
  77380. #if defined(DUK_USE_SOURCE_NONBMP)
  77381. /* IdentifierStart production with Letter and ASCII excluded */
  77382. /* duk_unicode_ids_m_let_noa[] */
  77383. /*
  77384. * Automatically generated by extract_chars.py, do not edit!
  77385. */
  77386. const duk_uint8_t duk_unicode_ids_m_let_noa[42] = {
  77387. 255,240,0,94,18,255,233,99,241,51,63,254,215,32,240,184,240,2,255,240,6,89,
  77388. 249,255,240,4,148,79,37,255,224,192,9,15,120,79,255,0,15,30,245,240,
  77389. };
  77390. #else
  77391. /* IdentifierStart production with Letter, ASCII, and non-BMP excluded */
  77392. /* duk_unicode_ids_m_let_noabmp[] */
  77393. /*
  77394. * Automatically generated by extract_chars.py, do not edit!
  77395. */
  77396. const duk_uint8_t duk_unicode_ids_m_let_noabmp[24] = {
  77397. 255,240,0,94,18,255,233,99,241,51,63,254,215,32,240,184,240,2,255,240,6,89,
  77398. 249,0,
  77399. };
  77400. #endif
  77401. #if defined(DUK_USE_SOURCE_NONBMP)
  77402. /* IdentifierPart production with IdentifierStart and ASCII excluded */
  77403. /* duk_unicode_idp_m_ids_noa[] */
  77404. /*
  77405. * Automatically generated by extract_chars.py, do not edit!
  77406. */
  77407. const duk_uint8_t duk_unicode_idp_m_ids_noa[549] = {
  77408. 255,225,243,246,15,254,0,116,255,191,29,32,33,33,32,243,170,242,47,15,112,
  77409. 245,118,53,49,35,57,240,144,241,15,11,244,218,240,25,241,56,241,67,40,34,
  77410. 36,241,210,246,173,47,17,242,130,47,2,38,177,57,240,50,242,160,38,49,50,
  77411. 160,177,57,240,50,242,160,36,81,50,64,240,107,64,194,242,160,39,34,34,240,
  77412. 97,57,181,34,242,160,38,49,50,145,177,57,240,64,242,212,66,35,160,240,9,
  77413. 240,35,242,198,34,35,129,193,57,240,50,242,160,38,34,35,129,193,57,240,35,
  77414. 242,145,38,34,35,160,177,57,240,65,243,128,85,32,39,121,49,242,240,54,215,
  77415. 41,244,144,53,33,197,57,243,1,121,192,32,32,81,242,63,4,33,106,47,20,160,
  77416. 245,111,4,41,211,82,34,54,67,235,46,255,225,179,47,254,42,98,240,242,240,
  77417. 241,241,1,243,47,16,160,57,241,50,57,245,209,241,64,246,139,91,185,247,41,
  77418. 242,244,242,185,47,13,58,121,240,141,243,68,242,31,1,201,240,56,210,241,12,
  77419. 57,241,237,242,47,4,153,121,246,130,47,5,80,82,50,251,143,42,36,255,225,0,
  77420. 31,35,31,5,15,109,197,4,191,254,175,34,247,240,245,47,16,255,225,30,95,91,
  77421. 31,255,0,100,121,159,55,5,159,18,31,66,31,254,0,64,64,80,240,148,244,161,
  77422. 242,79,2,185,127,2,240,9,240,231,240,188,241,227,242,29,240,25,192,185,242,
  77423. 29,208,145,57,241,50,242,64,34,49,97,32,241,180,97,253,231,33,57,255,240,3,
  77424. 225,128,255,225,213,240,15,2,240,4,31,10,47,178,159,23,15,254,27,16,253,64,
  77425. 248,116,255,224,25,159,254,68,178,33,99,241,162,80,249,113,255,228,13,47,
  77426. 39,239,17,159,1,63,31,175,39,151,47,22,210,159,37,13,47,34,218,36,159,68,
  77427. 183,15,146,182,151,63,42,2,99,19,42,11,19,100,79,178,240,42,159,72,240,77,
  77428. 159,199,99,143,13,31,68,240,31,1,159,67,201,159,69,229,159,254,9,169,255,
  77429. 224,11,159,26,98,57,10,175,32,240,15,254,8,151,39,240,41,242,175,6,45,246,
  77430. 197,64,33,38,32,153,255,240,3,191,169,247,132,242,214,240,185,255,226,235,
  77431. 241,239,2,63,255,0,59,254,31,255,0,3,186,68,89,115,111,16,63,134,47,254,71,
  77432. 223,34,255,224,244,242,117,242,41,15,0,15,8,66,239,254,68,70,47,1,54,33,36,
  77433. 255,231,153,111,95,102,159,255,12,6,154,254,0,
  77434. };
  77435. #else
  77436. /* IdentifierPart production with IdentifierStart, ASCII, and non-BMP excluded */
  77437. /* duk_unicode_idp_m_ids_noabmp[] */
  77438. /*
  77439. * Automatically generated by extract_chars.py, do not edit!
  77440. */
  77441. const duk_uint8_t duk_unicode_idp_m_ids_noabmp[358] = {
  77442. 255,225,243,246,15,254,0,116,255,191,29,32,33,33,32,243,170,242,47,15,112,
  77443. 245,118,53,49,35,57,240,144,241,15,11,244,218,240,25,241,56,241,67,40,34,
  77444. 36,241,210,246,173,47,17,242,130,47,2,38,177,57,240,50,242,160,38,49,50,
  77445. 160,177,57,240,50,242,160,36,81,50,64,240,107,64,194,242,160,39,34,34,240,
  77446. 97,57,181,34,242,160,38,49,50,145,177,57,240,64,242,212,66,35,160,240,9,
  77447. 240,35,242,198,34,35,129,193,57,240,50,242,160,38,34,35,129,193,57,240,35,
  77448. 242,145,38,34,35,160,177,57,240,65,243,128,85,32,39,121,49,242,240,54,215,
  77449. 41,244,144,53,33,197,57,243,1,121,192,32,32,81,242,63,4,33,106,47,20,160,
  77450. 245,111,4,41,211,82,34,54,67,235,46,255,225,179,47,254,42,98,240,242,240,
  77451. 241,241,1,243,47,16,160,57,241,50,57,245,209,241,64,246,139,91,185,247,41,
  77452. 242,244,242,185,47,13,58,121,240,141,243,68,242,31,1,201,240,56,210,241,12,
  77453. 57,241,237,242,47,4,153,121,246,130,47,5,80,82,50,251,143,42,36,255,225,0,
  77454. 31,35,31,5,15,109,197,4,191,254,175,34,247,240,245,47,16,255,225,30,95,91,
  77455. 31,255,0,100,121,159,55,5,159,18,31,66,31,254,0,64,64,80,240,148,244,161,
  77456. 242,79,2,185,127,2,240,9,240,231,240,188,241,227,242,29,240,25,192,185,242,
  77457. 29,208,145,57,241,50,242,64,34,49,97,32,241,180,97,253,231,33,57,255,240,3,
  77458. 225,128,255,225,213,240,15,2,240,4,31,10,47,178,159,23,0,
  77459. };
  77460. #endif
  77461. /*
  77462. * Case conversion tables generated using tools/extract_caseconv.py.
  77463. */
  77464. /* duk_unicode_caseconv_uc[] */
  77465. /* duk_unicode_caseconv_lc[] */
  77466. /*
  77467. * Automatically generated by extract_caseconv.py, do not edit!
  77468. */
  77469. const duk_uint8_t duk_unicode_caseconv_uc[1386] = {
  77470. 144,3,128,3,0,184,7,192,6,192,112,35,242,199,224,64,74,192,49,32,128,162,
  77471. 128,108,65,1,189,129,254,131,3,173,3,136,6,7,98,7,34,68,15,12,14,140,72,30,
  77472. 104,28,112,32,67,0,65,4,0,138,0,128,4,1,88,65,76,83,9,252,9,248,6,28,131,4,
  77473. 33,4,62,0,62,16,32,124,64,124,96,48,249,0,249,64,129,243,1,243,129,3,232,3,
  77474. 233,1,135,216,7,218,4,15,184,15,221,2,31,114,31,200,8,62,236,63,180,8,125,
  77475. 224,127,224,16,251,208,255,80,33,247,193,255,160,67,246,3,247,0,135,244,7,
  77476. 246,1,15,240,15,244,2,33,112,33,96,32,73,160,73,108,104,176,192,176,1,121,
  77477. 104,0,133,2,106,183,1,58,10,31,232,63,228,38,162,1,1,1,0,48,2,102,2,100,12,
  77478. 4,232,4,228,64,10,88,10,81,112,23,160,23,144,96,48,96,48,64,128,104,64,104,
  77479. 1,128,218,0,217,130,1,206,1,205,16,3,190,3,188,36,7,228,7,224,160,17,24,17,
  77480. 16,144,36,112,36,96,160,110,32,110,0,128,246,64,246,6,2,48,130,48,17,4,139,
  77481. 4,138,54,9,132,9,130,28,19,68,19,65,128,240,8,240,4,177,234,17,234,6,3,234,
  77482. 35,235,33,11,26,11,25,193,150,64,150,64,50,44,236,44,235,5,76,131,76,128,
  77483. 94,154,6,154,0,117,57,29,57,16,122,115,58,115,35,244,239,84,239,32,169,223,
  77484. 233,223,130,211,200,211,200,2,167,151,167,150,21,79,107,79,104,8,112,26,
  77485. 208,26,192,64,56,160,56,128,192,113,128,113,1,128,249,0,248,130,2,128,1,
  77486. 166,4,7,240,7,238,8,177,204,177,200,16,96,49,0,48,224,128,110,64,110,1,1,
  77487. 51,83,213,2,0,48,35,192,35,176,64,77,32,50,192,139,73,196,49,193,127,48,2,
  77488. 212,14,112,3,252,5,224,4,196,1,36,5,252,1,76,6,0,9,12,6,72,6,68,6,84,7,216,
  77489. 6,100,6,96,6,104,8,244,6,120,8,128,6,160,6,156,6,252,7,220,7,116,6,56,7,
  77490. 204,7,196,9,64,177,188,9,68,177,180,9,72,177,192,9,76,6,4,9,80,6,24,9,100,
  77491. 6,60,9,108,6,64,9,114,158,172,9,128,6,76,9,134,158,176,9,140,6,80,9,150,
  77492. 158,52,9,160,6,92,9,172,177,136,9,178,158,180,9,196,177,184,9,200,6,116,9,
  77493. 212,6,124,9,244,177,144,10,30,158,196,10,32,6,184,10,36,9,16,10,48,9,20,10,
  77494. 72,6,220,10,118,158,200,10,122,158,192,13,20,14,100,13,220,13,216,14,176,
  77495. 14,24,15,8,14,140,15,48,14,48,15,64,14,72,15,68,14,96,15,84,14,152,15,88,
  77496. 14,128,15,92,15,60,15,192,14,104,15,196,14,132,15,200,15,228,15,204,13,252,
  77497. 15,212,14,84,19,60,19,0,114,0,16,72,114,4,16,80,114,8,16,120,114,20,16,136,
  77498. 114,24,16,168,114,28,17,136,114,34,153,40,117,230,157,244,117,244,177,140,
  77499. 122,108,121,128,126,248,14,100,127,148,127,176,133,56,132,200,134,16,134,
  77500. 12,177,132,177,128,177,148,8,232,177,152,8,248,179,204,179,202,158,50,158,
  77501. 46,173,78,158,207,48,6,252,0,166,0,166,2,147,1,94,0,39,0,248,64,9,64,97,
  77502. 128,114,24,28,200,24,64,24,8,29,134,7,74,6,16,6,2,11,15,2,154,130,169,15,
  77503. 75,64,9,0,102,35,210,240,2,160,24,64,244,196,0,174,6,20,61,51,0,44,129,133,
  77504. 15,77,64,8,32,87,195,234,16,29,40,24,152,250,150,7,74,6,38,6,0,62,169,129,
  77505. 210,129,137,129,128,143,171,96,116,160,98,96,104,67,240,16,248,64,28,200,
  77506. 252,12,62,18,7,50,63,5,15,133,1,204,143,193,195,225,96,115,35,240,144,248,
  77507. 96,28,200,252,44,62,26,7,50,63,13,15,135,1,204,143,195,195,225,224,115,35,
  77508. 241,16,248,64,28,200,252,76,62,18,7,50,63,21,15,133,1,204,143,197,195,225,
  77509. 96,115,35,241,144,248,96,28,200,252,108,62,26,7,50,63,29,15,135,1,204,143,
  77510. 199,195,225,224,115,35,242,16,249,64,28,200,252,140,62,82,7,50,63,37,15,
  77511. 149,1,204,143,201,195,229,96,115,35,242,144,249,96,28,200,252,172,62,90,7,
  77512. 50,63,45,15,151,1,204,143,203,195,229,224,115,35,243,16,249,64,28,200,252,
  77513. 204,62,82,7,50,63,53,15,149,1,204,143,205,195,229,96,115,35,243,144,249,96,
  77514. 28,200,252,236,62,90,7,50,63,61,15,151,1,204,143,207,195,229,224,115,35,
  77515. 244,16,251,64,28,200,253,12,62,210,7,50,63,69,15,181,1,204,143,209,195,237,
  77516. 96,115,35,244,144,251,96,28,200,253,44,62,218,7,50,63,77,15,183,1,204,143,
  77517. 211,195,237,224,115,35,245,16,251,64,28,200,253,76,62,210,7,50,63,85,15,
  77518. 181,1,204,143,213,195,237,96,115,35,245,144,251,96,28,200,253,108,62,218,7,
  77519. 50,63,93,15,183,1,204,143,215,195,237,224,115,35,246,80,253,208,28,200,253,
  77520. 156,7,34,7,50,63,105,1,195,1,204,143,219,64,114,32,104,67,246,248,28,136,
  77521. 26,16,28,200,253,228,7,34,7,50,63,133,15,229,1,204,143,225,192,114,224,115,
  77522. 35,248,144,28,72,28,200,254,52,7,46,6,132,63,143,129,203,129,161,1,204,143,
  77523. 230,64,114,224,115,35,250,88,28,200,24,64,24,0,254,158,7,50,6,16,6,2,63,
  77524. 173,1,204,129,161,15,235,224,115,32,97,0,104,67,252,88,29,40,24,64,24,0,
  77525. 255,30,7,74,6,16,6,2,63,201,1,208,129,137,143,243,64,116,160,104,67,252,
  77526. 248,29,40,24,64,26,16,255,148,63,244,7,50,63,231,1,212,129,204,143,250,64,
  77527. 113,224,115,35,254,208,29,72,26,16,255,190,7,82,6,132,7,50,63,249,1,212,
  77528. 129,204,253,128,64,8,192,8,223,96,48,2,48,2,79,216,20,0,140,0,153,246,7,
  77529. 128,35,0,35,0,36,253,130,96,8,192,8,192,9,159,96,176,2,152,2,167,216,52,0,
  77530. 166,0,169,246,39,2,162,2,163,125,138,64,168,128,166,191,98,176,42,32,41,
  77531. 223,216,180,10,156,10,141,246,47,2,162,2,158,128,
  77532. };
  77533. const duk_uint8_t duk_unicode_caseconv_lc[680] = {
  77534. 152,3,0,3,128,184,6,192,7,192,112,24,144,37,96,64,54,32,81,64,128,226,0,
  77535. 235,65,129,199,1,230,130,3,145,3,177,34,7,70,7,134,36,15,244,13,236,24,32,
  77536. 0,34,129,0,65,0,67,4,0,166,32,172,41,132,40,11,64,19,9,208,85,184,80,19,
  77537. 240,19,248,12,62,16,62,0,32,124,96,124,64,48,249,64,249,0,129,243,129,243,
  77538. 1,3,233,3,232,1,135,218,7,216,4,15,196,15,192,8,31,152,31,144,16,63,80,63,
  77539. 64,32,126,224,126,192,16,253,208,251,128,33,252,129,247,32,131,251,3,250,0,
  77540. 135,246,135,221,129,15,244,15,240,2,31,234,31,122,4,63,240,62,240,8,127,
  77541. 232,125,240,17,11,1,11,129,2,75,98,77,3,69,128,5,134,11,203,31,128,143,193,
  77542. 127,144,255,160,154,140,4,0,4,4,192,9,144,9,152,48,19,144,19,161,0,41,64,
  77543. 41,101,192,94,64,94,129,128,193,0,193,130,1,160,1,161,6,3,102,3,104,8,7,44,
  77544. 7,48,72,14,240,14,248,144,31,32,31,48,64,63,0,63,37,0,136,128,136,196,129,
  77545. 35,1,35,133,3,112,3,113,4,7,176,7,178,48,17,128,17,132,136,36,80,36,89,176,
  77546. 76,16,76,32,224,154,0,154,44,7,128,7,128,101,143,80,15,80,176,31,89,31,81,
  77547. 8,88,206,88,208,12,178,0,178,5,145,103,89,103,96,42,100,10,100,18,244,208,
  77548. 20,208,35,169,200,169,200,195,211,153,83,153,159,167,121,167,122,5,78,253,
  77549. 78,254,22,158,66,158,68,21,60,181,60,184,170,123,74,123,80,67,0,211,1,64,2,
  77550. 1,172,1,173,4,3,136,3,140,12,7,20,7,24,16,31,184,31,192,34,199,34,199,48,
  77551. 65,128,195,128,196,2,1,184,1,185,5,79,84,4,204,8,0,192,101,128,154,65,1,29,
  77552. 129,30,2,16,199,45,39,5,251,240,23,128,15,240,24,16,37,48,24,96,37,64,24,
  77553. 224,29,208,24,240,37,144,25,0,37,176,25,16,25,32,25,48,38,0,25,64,38,48,25,
  77554. 112,38,128,25,128,25,144,25,208,39,32,25,240,39,80,26,112,26,128,26,224,40,
  77555. 128,27,112,41,32,31,16,31,48,31,96,25,80,31,112,27,240,34,0,25,224,35,162,
  77556. 198,80,35,208,25,160,35,226,198,96,36,48,24,0,36,64,40,144,36,80,40,192,55,
  77557. 96,55,112,55,240,63,48,56,96,58,192,56,192,60,192,60,240,61,112,63,64,59,
  77558. 128,63,144,63,32,76,0,76,241,233,224,13,241,251,193,251,49,252,193,252,49,
  77559. 254,193,254,81,255,193,255,50,18,96,60,146,18,160,6,178,18,176,14,82,19,34,
  77560. 20,226,24,50,24,66,198,2,198,18,198,32,38,178,198,49,215,210,198,64,39,210,
  77561. 198,208,37,18,198,224,39,18,198,240,37,2,199,0,37,34,207,34,207,58,119,209,
  77562. 215,154,120,186,120,202,120,208,38,90,122,176,37,202,122,192,38,26,122,208,
  77563. 38,202,123,0,41,234,123,16,40,122,123,32,41,218,123,58,181,48,32,38,16,3,
  77564. 72,24,56,
  77565. };
  77566. #if defined(DUK_USE_REGEXP_CANON_WORKAROUND)
  77567. /*
  77568. * Automatically generated by extract_caseconv.py, do not edit!
  77569. */
  77570. const duk_uint16_t duk_unicode_re_canon_lookup[65536] = {
  77571. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
  77572. 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,
  77573. 53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,
  77574. 78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,65,66,67,68,69,70,
  77575. 71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,123,124,125,
  77576. 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  77577. 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
  77578. 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
  77579. 180,924,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
  77580. 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
  77581. 216,217,218,219,220,221,222,223,192,193,194,195,196,197,198,199,200,201,
  77582. 202,203,204,205,206,207,208,209,210,211,212,213,214,247,216,217,218,219,
  77583. 220,221,222,376,256,256,258,258,260,260,262,262,264,264,266,266,268,268,
  77584. 270,270,272,272,274,274,276,276,278,278,280,280,282,282,284,284,286,286,
  77585. 288,288,290,290,292,292,294,294,296,296,298,298,300,300,302,302,304,305,
  77586. 306,306,308,308,310,310,312,313,313,315,315,317,317,319,319,321,321,323,
  77587. 323,325,325,327,327,329,330,330,332,332,334,334,336,336,338,338,340,340,
  77588. 342,342,344,344,346,346,348,348,350,350,352,352,354,354,356,356,358,358,
  77589. 360,360,362,362,364,364,366,366,368,368,370,370,372,372,374,374,376,377,
  77590. 377,379,379,381,381,383,579,385,386,386,388,388,390,391,391,393,394,395,
  77591. 395,397,398,399,400,401,401,403,404,502,406,407,408,408,573,411,412,413,
  77592. 544,415,416,416,418,418,420,420,422,423,423,425,426,427,428,428,430,431,
  77593. 431,433,434,435,435,437,437,439,440,440,442,443,444,444,446,503,448,449,
  77594. 450,451,452,452,452,455,455,455,458,458,458,461,461,463,463,465,465,467,
  77595. 467,469,469,471,471,473,473,475,475,398,478,478,480,480,482,482,484,484,
  77596. 486,486,488,488,490,490,492,492,494,494,496,497,497,497,500,500,502,503,
  77597. 504,504,506,506,508,508,510,510,512,512,514,514,516,516,518,518,520,520,
  77598. 522,522,524,524,526,526,528,528,530,530,532,532,534,534,536,536,538,538,
  77599. 540,540,542,542,544,545,546,546,548,548,550,550,552,552,554,554,556,556,
  77600. 558,558,560,560,562,562,564,565,566,567,568,569,570,571,571,573,574,11390,
  77601. 11391,577,577,579,580,581,582,582,584,584,586,586,588,588,590,590,11375,
  77602. 11373,11376,385,390,597,393,394,600,399,602,400,42923L,605,606,607,403,
  77603. 42924L,610,404,612,42893L,42922L,615,407,406,42926L,11362,42925L,621,622,
  77604. 412,624,11374,413,627,628,415,630,631,632,633,634,635,636,11364,638,639,
  77605. 422,641,642,425,644,645,646,42929L,430,580,433,434,581,653,654,655,656,657,
  77606. 439,659,660,661,662,663,664,665,666,667,668,42930L,42928L,671,672,673,674,
  77607. 675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,
  77608. 693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,
  77609. 711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,
  77610. 729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,
  77611. 747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,
  77612. 765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,
  77613. 783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,
  77614. 801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,
  77615. 819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,
  77616. 921,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,
  77617. 855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,
  77618. 873,874,875,876,877,878,879,880,880,882,882,884,885,886,886,888,889,890,
  77619. 1021,1022,1023,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,
  77620. 909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,
  77621. 927,928,929,930,931,932,933,934,935,936,937,938,939,902,904,905,906,944,
  77622. 913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,931,
  77623. 931,932,933,934,935,936,937,938,939,908,910,911,975,914,920,978,979,980,
  77624. 934,928,975,984,984,986,986,988,988,990,990,992,992,994,994,996,996,998,
  77625. 998,1000,1000,1002,1002,1004,1004,1006,1006,922,929,1017,895,1012,917,1014,
  77626. 1015,1015,1017,1018,1018,1020,1021,1022,1023,1024,1025,1026,1027,1028,1029,
  77627. 1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,1040,1041,1042,1043,1044,
  77628. 1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,1058,1059,
  77629. 1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1040,1041,1042,
  77630. 1043,1044,1045,1046,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1057,
  77631. 1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1069,1070,1071,1024,
  77632. 1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,1037,1038,1039,
  77633. 1120,1120,1122,1122,1124,1124,1126,1126,1128,1128,1130,1130,1132,1132,1134,
  77634. 1134,1136,1136,1138,1138,1140,1140,1142,1142,1144,1144,1146,1146,1148,1148,
  77635. 1150,1150,1152,1152,1154,1155,1156,1157,1158,1159,1160,1161,1162,1162,1164,
  77636. 1164,1166,1166,1168,1168,1170,1170,1172,1172,1174,1174,1176,1176,1178,1178,
  77637. 1180,1180,1182,1182,1184,1184,1186,1186,1188,1188,1190,1190,1192,1192,1194,
  77638. 1194,1196,1196,1198,1198,1200,1200,1202,1202,1204,1204,1206,1206,1208,1208,
  77639. 1210,1210,1212,1212,1214,1214,1216,1217,1217,1219,1219,1221,1221,1223,1223,
  77640. 1225,1225,1227,1227,1229,1229,1216,1232,1232,1234,1234,1236,1236,1238,1238,
  77641. 1240,1240,1242,1242,1244,1244,1246,1246,1248,1248,1250,1250,1252,1252,1254,
  77642. 1254,1256,1256,1258,1258,1260,1260,1262,1262,1264,1264,1266,1266,1268,1268,
  77643. 1270,1270,1272,1272,1274,1274,1276,1276,1278,1278,1280,1280,1282,1282,1284,
  77644. 1284,1286,1286,1288,1288,1290,1290,1292,1292,1294,1294,1296,1296,1298,1298,
  77645. 1300,1300,1302,1302,1304,1304,1306,1306,1308,1308,1310,1310,1312,1312,1314,
  77646. 1314,1316,1316,1318,1318,1320,1320,1322,1322,1324,1324,1326,1326,1328,1329,
  77647. 1330,1331,1332,1333,1334,1335,1336,1337,1338,1339,1340,1341,1342,1343,1344,
  77648. 1345,1346,1347,1348,1349,1350,1351,1352,1353,1354,1355,1356,1357,1358,1359,
  77649. 1360,1361,1362,1363,1364,1365,1366,1367,1368,1369,1370,1371,1372,1373,1374,
  77650. 1375,1376,1329,1330,1331,1332,1333,1334,1335,1336,1337,1338,1339,1340,1341,
  77651. 1342,1343,1344,1345,1346,1347,1348,1349,1350,1351,1352,1353,1354,1355,1356,
  77652. 1357,1358,1359,1360,1361,1362,1363,1364,1365,1366,1415,1416,1417,1418,1419,
  77653. 1420,1421,1422,1423,1424,1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,
  77654. 1435,1436,1437,1438,1439,1440,1441,1442,1443,1444,1445,1446,1447,1448,1449,
  77655. 1450,1451,1452,1453,1454,1455,1456,1457,1458,1459,1460,1461,1462,1463,1464,
  77656. 1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477,1478,1479,
  77657. 1480,1481,1482,1483,1484,1485,1486,1487,1488,1489,1490,1491,1492,1493,1494,
  77658. 1495,1496,1497,1498,1499,1500,1501,1502,1503,1504,1505,1506,1507,1508,1509,
  77659. 1510,1511,1512,1513,1514,1515,1516,1517,1518,1519,1520,1521,1522,1523,1524,
  77660. 1525,1526,1527,1528,1529,1530,1531,1532,1533,1534,1535,1536,1537,1538,1539,
  77661. 1540,1541,1542,1543,1544,1545,1546,1547,1548,1549,1550,1551,1552,1553,1554,
  77662. 1555,1556,1557,1558,1559,1560,1561,1562,1563,1564,1565,1566,1567,1568,1569,
  77663. 1570,1571,1572,1573,1574,1575,1576,1577,1578,1579,1580,1581,1582,1583,1584,
  77664. 1585,1586,1587,1588,1589,1590,1591,1592,1593,1594,1595,1596,1597,1598,1599,
  77665. 1600,1601,1602,1603,1604,1605,1606,1607,1608,1609,1610,1611,1612,1613,1614,
  77666. 1615,1616,1617,1618,1619,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,
  77667. 1630,1631,1632,1633,1634,1635,1636,1637,1638,1639,1640,1641,1642,1643,1644,
  77668. 1645,1646,1647,1648,1649,1650,1651,1652,1653,1654,1655,1656,1657,1658,1659,
  77669. 1660,1661,1662,1663,1664,1665,1666,1667,1668,1669,1670,1671,1672,1673,1674,
  77670. 1675,1676,1677,1678,1679,1680,1681,1682,1683,1684,1685,1686,1687,1688,1689,
  77671. 1690,1691,1692,1693,1694,1695,1696,1697,1698,1699,1700,1701,1702,1703,1704,
  77672. 1705,1706,1707,1708,1709,1710,1711,1712,1713,1714,1715,1716,1717,1718,1719,
  77673. 1720,1721,1722,1723,1724,1725,1726,1727,1728,1729,1730,1731,1732,1733,1734,
  77674. 1735,1736,1737,1738,1739,1740,1741,1742,1743,1744,1745,1746,1747,1748,1749,
  77675. 1750,1751,1752,1753,1754,1755,1756,1757,1758,1759,1760,1761,1762,1763,1764,
  77676. 1765,1766,1767,1768,1769,1770,1771,1772,1773,1774,1775,1776,1777,1778,1779,
  77677. 1780,1781,1782,1783,1784,1785,1786,1787,1788,1789,1790,1791,1792,1793,1794,
  77678. 1795,1796,1797,1798,1799,1800,1801,1802,1803,1804,1805,1806,1807,1808,1809,
  77679. 1810,1811,1812,1813,1814,1815,1816,1817,1818,1819,1820,1821,1822,1823,1824,
  77680. 1825,1826,1827,1828,1829,1830,1831,1832,1833,1834,1835,1836,1837,1838,1839,
  77681. 1840,1841,1842,1843,1844,1845,1846,1847,1848,1849,1850,1851,1852,1853,1854,
  77682. 1855,1856,1857,1858,1859,1860,1861,1862,1863,1864,1865,1866,1867,1868,1869,
  77683. 1870,1871,1872,1873,1874,1875,1876,1877,1878,1879,1880,1881,1882,1883,1884,
  77684. 1885,1886,1887,1888,1889,1890,1891,1892,1893,1894,1895,1896,1897,1898,1899,
  77685. 1900,1901,1902,1903,1904,1905,1906,1907,1908,1909,1910,1911,1912,1913,1914,
  77686. 1915,1916,1917,1918,1919,1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,
  77687. 1930,1931,1932,1933,1934,1935,1936,1937,1938,1939,1940,1941,1942,1943,1944,
  77688. 1945,1946,1947,1948,1949,1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,
  77689. 1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,
  77690. 1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,
  77691. 1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,
  77692. 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,
  77693. 2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030,2031,2032,2033,2034,
  77694. 2035,2036,2037,2038,2039,2040,2041,2042,2043,2044,2045,2046,2047,2048,2049,
  77695. 2050,2051,2052,2053,2054,2055,2056,2057,2058,2059,2060,2061,2062,2063,2064,
  77696. 2065,2066,2067,2068,2069,2070,2071,2072,2073,2074,2075,2076,2077,2078,2079,
  77697. 2080,2081,2082,2083,2084,2085,2086,2087,2088,2089,2090,2091,2092,2093,2094,
  77698. 2095,2096,2097,2098,2099,2100,2101,2102,2103,2104,2105,2106,2107,2108,2109,
  77699. 2110,2111,2112,2113,2114,2115,2116,2117,2118,2119,2120,2121,2122,2123,2124,
  77700. 2125,2126,2127,2128,2129,2130,2131,2132,2133,2134,2135,2136,2137,2138,2139,
  77701. 2140,2141,2142,2143,2144,2145,2146,2147,2148,2149,2150,2151,2152,2153,2154,
  77702. 2155,2156,2157,2158,2159,2160,2161,2162,2163,2164,2165,2166,2167,2168,2169,
  77703. 2170,2171,2172,2173,2174,2175,2176,2177,2178,2179,2180,2181,2182,2183,2184,
  77704. 2185,2186,2187,2188,2189,2190,2191,2192,2193,2194,2195,2196,2197,2198,2199,
  77705. 2200,2201,2202,2203,2204,2205,2206,2207,2208,2209,2210,2211,2212,2213,2214,
  77706. 2215,2216,2217,2218,2219,2220,2221,2222,2223,2224,2225,2226,2227,2228,2229,
  77707. 2230,2231,2232,2233,2234,2235,2236,2237,2238,2239,2240,2241,2242,2243,2244,
  77708. 2245,2246,2247,2248,2249,2250,2251,2252,2253,2254,2255,2256,2257,2258,2259,
  77709. 2260,2261,2262,2263,2264,2265,2266,2267,2268,2269,2270,2271,2272,2273,2274,
  77710. 2275,2276,2277,2278,2279,2280,2281,2282,2283,2284,2285,2286,2287,2288,2289,
  77711. 2290,2291,2292,2293,2294,2295,2296,2297,2298,2299,2300,2301,2302,2303,2304,
  77712. 2305,2306,2307,2308,2309,2310,2311,2312,2313,2314,2315,2316,2317,2318,2319,
  77713. 2320,2321,2322,2323,2324,2325,2326,2327,2328,2329,2330,2331,2332,2333,2334,
  77714. 2335,2336,2337,2338,2339,2340,2341,2342,2343,2344,2345,2346,2347,2348,2349,
  77715. 2350,2351,2352,2353,2354,2355,2356,2357,2358,2359,2360,2361,2362,2363,2364,
  77716. 2365,2366,2367,2368,2369,2370,2371,2372,2373,2374,2375,2376,2377,2378,2379,
  77717. 2380,2381,2382,2383,2384,2385,2386,2387,2388,2389,2390,2391,2392,2393,2394,
  77718. 2395,2396,2397,2398,2399,2400,2401,2402,2403,2404,2405,2406,2407,2408,2409,
  77719. 2410,2411,2412,2413,2414,2415,2416,2417,2418,2419,2420,2421,2422,2423,2424,
  77720. 2425,2426,2427,2428,2429,2430,2431,2432,2433,2434,2435,2436,2437,2438,2439,
  77721. 2440,2441,2442,2443,2444,2445,2446,2447,2448,2449,2450,2451,2452,2453,2454,
  77722. 2455,2456,2457,2458,2459,2460,2461,2462,2463,2464,2465,2466,2467,2468,2469,
  77723. 2470,2471,2472,2473,2474,2475,2476,2477,2478,2479,2480,2481,2482,2483,2484,
  77724. 2485,2486,2487,2488,2489,2490,2491,2492,2493,2494,2495,2496,2497,2498,2499,
  77725. 2500,2501,2502,2503,2504,2505,2506,2507,2508,2509,2510,2511,2512,2513,2514,
  77726. 2515,2516,2517,2518,2519,2520,2521,2522,2523,2524,2525,2526,2527,2528,2529,
  77727. 2530,2531,2532,2533,2534,2535,2536,2537,2538,2539,2540,2541,2542,2543,2544,
  77728. 2545,2546,2547,2548,2549,2550,2551,2552,2553,2554,2555,2556,2557,2558,2559,
  77729. 2560,2561,2562,2563,2564,2565,2566,2567,2568,2569,2570,2571,2572,2573,2574,
  77730. 2575,2576,2577,2578,2579,2580,2581,2582,2583,2584,2585,2586,2587,2588,2589,
  77731. 2590,2591,2592,2593,2594,2595,2596,2597,2598,2599,2600,2601,2602,2603,2604,
  77732. 2605,2606,2607,2608,2609,2610,2611,2612,2613,2614,2615,2616,2617,2618,2619,
  77733. 2620,2621,2622,2623,2624,2625,2626,2627,2628,2629,2630,2631,2632,2633,2634,
  77734. 2635,2636,2637,2638,2639,2640,2641,2642,2643,2644,2645,2646,2647,2648,2649,
  77735. 2650,2651,2652,2653,2654,2655,2656,2657,2658,2659,2660,2661,2662,2663,2664,
  77736. 2665,2666,2667,2668,2669,2670,2671,2672,2673,2674,2675,2676,2677,2678,2679,
  77737. 2680,2681,2682,2683,2684,2685,2686,2687,2688,2689,2690,2691,2692,2693,2694,
  77738. 2695,2696,2697,2698,2699,2700,2701,2702,2703,2704,2705,2706,2707,2708,2709,
  77739. 2710,2711,2712,2713,2714,2715,2716,2717,2718,2719,2720,2721,2722,2723,2724,
  77740. 2725,2726,2727,2728,2729,2730,2731,2732,2733,2734,2735,2736,2737,2738,2739,
  77741. 2740,2741,2742,2743,2744,2745,2746,2747,2748,2749,2750,2751,2752,2753,2754,
  77742. 2755,2756,2757,2758,2759,2760,2761,2762,2763,2764,2765,2766,2767,2768,2769,
  77743. 2770,2771,2772,2773,2774,2775,2776,2777,2778,2779,2780,2781,2782,2783,2784,
  77744. 2785,2786,2787,2788,2789,2790,2791,2792,2793,2794,2795,2796,2797,2798,2799,
  77745. 2800,2801,2802,2803,2804,2805,2806,2807,2808,2809,2810,2811,2812,2813,2814,
  77746. 2815,2816,2817,2818,2819,2820,2821,2822,2823,2824,2825,2826,2827,2828,2829,
  77747. 2830,2831,2832,2833,2834,2835,2836,2837,2838,2839,2840,2841,2842,2843,2844,
  77748. 2845,2846,2847,2848,2849,2850,2851,2852,2853,2854,2855,2856,2857,2858,2859,
  77749. 2860,2861,2862,2863,2864,2865,2866,2867,2868,2869,2870,2871,2872,2873,2874,
  77750. 2875,2876,2877,2878,2879,2880,2881,2882,2883,2884,2885,2886,2887,2888,2889,
  77751. 2890,2891,2892,2893,2894,2895,2896,2897,2898,2899,2900,2901,2902,2903,2904,
  77752. 2905,2906,2907,2908,2909,2910,2911,2912,2913,2914,2915,2916,2917,2918,2919,
  77753. 2920,2921,2922,2923,2924,2925,2926,2927,2928,2929,2930,2931,2932,2933,2934,
  77754. 2935,2936,2937,2938,2939,2940,2941,2942,2943,2944,2945,2946,2947,2948,2949,
  77755. 2950,2951,2952,2953,2954,2955,2956,2957,2958,2959,2960,2961,2962,2963,2964,
  77756. 2965,2966,2967,2968,2969,2970,2971,2972,2973,2974,2975,2976,2977,2978,2979,
  77757. 2980,2981,2982,2983,2984,2985,2986,2987,2988,2989,2990,2991,2992,2993,2994,
  77758. 2995,2996,2997,2998,2999,3000,3001,3002,3003,3004,3005,3006,3007,3008,3009,
  77759. 3010,3011,3012,3013,3014,3015,3016,3017,3018,3019,3020,3021,3022,3023,3024,
  77760. 3025,3026,3027,3028,3029,3030,3031,3032,3033,3034,3035,3036,3037,3038,3039,
  77761. 3040,3041,3042,3043,3044,3045,3046,3047,3048,3049,3050,3051,3052,3053,3054,
  77762. 3055,3056,3057,3058,3059,3060,3061,3062,3063,3064,3065,3066,3067,3068,3069,
  77763. 3070,3071,3072,3073,3074,3075,3076,3077,3078,3079,3080,3081,3082,3083,3084,
  77764. 3085,3086,3087,3088,3089,3090,3091,3092,3093,3094,3095,3096,3097,3098,3099,
  77765. 3100,3101,3102,3103,3104,3105,3106,3107,3108,3109,3110,3111,3112,3113,3114,
  77766. 3115,3116,3117,3118,3119,3120,3121,3122,3123,3124,3125,3126,3127,3128,3129,
  77767. 3130,3131,3132,3133,3134,3135,3136,3137,3138,3139,3140,3141,3142,3143,3144,
  77768. 3145,3146,3147,3148,3149,3150,3151,3152,3153,3154,3155,3156,3157,3158,3159,
  77769. 3160,3161,3162,3163,3164,3165,3166,3167,3168,3169,3170,3171,3172,3173,3174,
  77770. 3175,3176,3177,3178,3179,3180,3181,3182,3183,3184,3185,3186,3187,3188,3189,
  77771. 3190,3191,3192,3193,3194,3195,3196,3197,3198,3199,3200,3201,3202,3203,3204,
  77772. 3205,3206,3207,3208,3209,3210,3211,3212,3213,3214,3215,3216,3217,3218,3219,
  77773. 3220,3221,3222,3223,3224,3225,3226,3227,3228,3229,3230,3231,3232,3233,3234,
  77774. 3235,3236,3237,3238,3239,3240,3241,3242,3243,3244,3245,3246,3247,3248,3249,
  77775. 3250,3251,3252,3253,3254,3255,3256,3257,3258,3259,3260,3261,3262,3263,3264,
  77776. 3265,3266,3267,3268,3269,3270,3271,3272,3273,3274,3275,3276,3277,3278,3279,
  77777. 3280,3281,3282,3283,3284,3285,3286,3287,3288,3289,3290,3291,3292,3293,3294,
  77778. 3295,3296,3297,3298,3299,3300,3301,3302,3303,3304,3305,3306,3307,3308,3309,
  77779. 3310,3311,3312,3313,3314,3315,3316,3317,3318,3319,3320,3321,3322,3323,3324,
  77780. 3325,3326,3327,3328,3329,3330,3331,3332,3333,3334,3335,3336,3337,3338,3339,
  77781. 3340,3341,3342,3343,3344,3345,3346,3347,3348,3349,3350,3351,3352,3353,3354,
  77782. 3355,3356,3357,3358,3359,3360,3361,3362,3363,3364,3365,3366,3367,3368,3369,
  77783. 3370,3371,3372,3373,3374,3375,3376,3377,3378,3379,3380,3381,3382,3383,3384,
  77784. 3385,3386,3387,3388,3389,3390,3391,3392,3393,3394,3395,3396,3397,3398,3399,
  77785. 3400,3401,3402,3403,3404,3405,3406,3407,3408,3409,3410,3411,3412,3413,3414,
  77786. 3415,3416,3417,3418,3419,3420,3421,3422,3423,3424,3425,3426,3427,3428,3429,
  77787. 3430,3431,3432,3433,3434,3435,3436,3437,3438,3439,3440,3441,3442,3443,3444,
  77788. 3445,3446,3447,3448,3449,3450,3451,3452,3453,3454,3455,3456,3457,3458,3459,
  77789. 3460,3461,3462,3463,3464,3465,3466,3467,3468,3469,3470,3471,3472,3473,3474,
  77790. 3475,3476,3477,3478,3479,3480,3481,3482,3483,3484,3485,3486,3487,3488,3489,
  77791. 3490,3491,3492,3493,3494,3495,3496,3497,3498,3499,3500,3501,3502,3503,3504,
  77792. 3505,3506,3507,3508,3509,3510,3511,3512,3513,3514,3515,3516,3517,3518,3519,
  77793. 3520,3521,3522,3523,3524,3525,3526,3527,3528,3529,3530,3531,3532,3533,3534,
  77794. 3535,3536,3537,3538,3539,3540,3541,3542,3543,3544,3545,3546,3547,3548,3549,
  77795. 3550,3551,3552,3553,3554,3555,3556,3557,3558,3559,3560,3561,3562,3563,3564,
  77796. 3565,3566,3567,3568,3569,3570,3571,3572,3573,3574,3575,3576,3577,3578,3579,
  77797. 3580,3581,3582,3583,3584,3585,3586,3587,3588,3589,3590,3591,3592,3593,3594,
  77798. 3595,3596,3597,3598,3599,3600,3601,3602,3603,3604,3605,3606,3607,3608,3609,
  77799. 3610,3611,3612,3613,3614,3615,3616,3617,3618,3619,3620,3621,3622,3623,3624,
  77800. 3625,3626,3627,3628,3629,3630,3631,3632,3633,3634,3635,3636,3637,3638,3639,
  77801. 3640,3641,3642,3643,3644,3645,3646,3647,3648,3649,3650,3651,3652,3653,3654,
  77802. 3655,3656,3657,3658,3659,3660,3661,3662,3663,3664,3665,3666,3667,3668,3669,
  77803. 3670,3671,3672,3673,3674,3675,3676,3677,3678,3679,3680,3681,3682,3683,3684,
  77804. 3685,3686,3687,3688,3689,3690,3691,3692,3693,3694,3695,3696,3697,3698,3699,
  77805. 3700,3701,3702,3703,3704,3705,3706,3707,3708,3709,3710,3711,3712,3713,3714,
  77806. 3715,3716,3717,3718,3719,3720,3721,3722,3723,3724,3725,3726,3727,3728,3729,
  77807. 3730,3731,3732,3733,3734,3735,3736,3737,3738,3739,3740,3741,3742,3743,3744,
  77808. 3745,3746,3747,3748,3749,3750,3751,3752,3753,3754,3755,3756,3757,3758,3759,
  77809. 3760,3761,3762,3763,3764,3765,3766,3767,3768,3769,3770,3771,3772,3773,3774,
  77810. 3775,3776,3777,3778,3779,3780,3781,3782,3783,3784,3785,3786,3787,3788,3789,
  77811. 3790,3791,3792,3793,3794,3795,3796,3797,3798,3799,3800,3801,3802,3803,3804,
  77812. 3805,3806,3807,3808,3809,3810,3811,3812,3813,3814,3815,3816,3817,3818,3819,
  77813. 3820,3821,3822,3823,3824,3825,3826,3827,3828,3829,3830,3831,3832,3833,3834,
  77814. 3835,3836,3837,3838,3839,3840,3841,3842,3843,3844,3845,3846,3847,3848,3849,
  77815. 3850,3851,3852,3853,3854,3855,3856,3857,3858,3859,3860,3861,3862,3863,3864,
  77816. 3865,3866,3867,3868,3869,3870,3871,3872,3873,3874,3875,3876,3877,3878,3879,
  77817. 3880,3881,3882,3883,3884,3885,3886,3887,3888,3889,3890,3891,3892,3893,3894,
  77818. 3895,3896,3897,3898,3899,3900,3901,3902,3903,3904,3905,3906,3907,3908,3909,
  77819. 3910,3911,3912,3913,3914,3915,3916,3917,3918,3919,3920,3921,3922,3923,3924,
  77820. 3925,3926,3927,3928,3929,3930,3931,3932,3933,3934,3935,3936,3937,3938,3939,
  77821. 3940,3941,3942,3943,3944,3945,3946,3947,3948,3949,3950,3951,3952,3953,3954,
  77822. 3955,3956,3957,3958,3959,3960,3961,3962,3963,3964,3965,3966,3967,3968,3969,
  77823. 3970,3971,3972,3973,3974,3975,3976,3977,3978,3979,3980,3981,3982,3983,3984,
  77824. 3985,3986,3987,3988,3989,3990,3991,3992,3993,3994,3995,3996,3997,3998,3999,
  77825. 4000,4001,4002,4003,4004,4005,4006,4007,4008,4009,4010,4011,4012,4013,4014,
  77826. 4015,4016,4017,4018,4019,4020,4021,4022,4023,4024,4025,4026,4027,4028,4029,
  77827. 4030,4031,4032,4033,4034,4035,4036,4037,4038,4039,4040,4041,4042,4043,4044,
  77828. 4045,4046,4047,4048,4049,4050,4051,4052,4053,4054,4055,4056,4057,4058,4059,
  77829. 4060,4061,4062,4063,4064,4065,4066,4067,4068,4069,4070,4071,4072,4073,4074,
  77830. 4075,4076,4077,4078,4079,4080,4081,4082,4083,4084,4085,4086,4087,4088,4089,
  77831. 4090,4091,4092,4093,4094,4095,4096,4097,4098,4099,4100,4101,4102,4103,4104,
  77832. 4105,4106,4107,4108,4109,4110,4111,4112,4113,4114,4115,4116,4117,4118,4119,
  77833. 4120,4121,4122,4123,4124,4125,4126,4127,4128,4129,4130,4131,4132,4133,4134,
  77834. 4135,4136,4137,4138,4139,4140,4141,4142,4143,4144,4145,4146,4147,4148,4149,
  77835. 4150,4151,4152,4153,4154,4155,4156,4157,4158,4159,4160,4161,4162,4163,4164,
  77836. 4165,4166,4167,4168,4169,4170,4171,4172,4173,4174,4175,4176,4177,4178,4179,
  77837. 4180,4181,4182,4183,4184,4185,4186,4187,4188,4189,4190,4191,4192,4193,4194,
  77838. 4195,4196,4197,4198,4199,4200,4201,4202,4203,4204,4205,4206,4207,4208,4209,
  77839. 4210,4211,4212,4213,4214,4215,4216,4217,4218,4219,4220,4221,4222,4223,4224,
  77840. 4225,4226,4227,4228,4229,4230,4231,4232,4233,4234,4235,4236,4237,4238,4239,
  77841. 4240,4241,4242,4243,4244,4245,4246,4247,4248,4249,4250,4251,4252,4253,4254,
  77842. 4255,4256,4257,4258,4259,4260,4261,4262,4263,4264,4265,4266,4267,4268,4269,
  77843. 4270,4271,4272,4273,4274,4275,4276,4277,4278,4279,4280,4281,4282,4283,4284,
  77844. 4285,4286,4287,4288,4289,4290,4291,4292,4293,4294,4295,4296,4297,4298,4299,
  77845. 4300,4301,4302,4303,4304,4305,4306,4307,4308,4309,4310,4311,4312,4313,4314,
  77846. 4315,4316,4317,4318,4319,4320,4321,4322,4323,4324,4325,4326,4327,4328,4329,
  77847. 4330,4331,4332,4333,4334,4335,4336,4337,4338,4339,4340,4341,4342,4343,4344,
  77848. 4345,4346,4347,4348,4349,4350,4351,4352,4353,4354,4355,4356,4357,4358,4359,
  77849. 4360,4361,4362,4363,4364,4365,4366,4367,4368,4369,4370,4371,4372,4373,4374,
  77850. 4375,4376,4377,4378,4379,4380,4381,4382,4383,4384,4385,4386,4387,4388,4389,
  77851. 4390,4391,4392,4393,4394,4395,4396,4397,4398,4399,4400,4401,4402,4403,4404,
  77852. 4405,4406,4407,4408,4409,4410,4411,4412,4413,4414,4415,4416,4417,4418,4419,
  77853. 4420,4421,4422,4423,4424,4425,4426,4427,4428,4429,4430,4431,4432,4433,4434,
  77854. 4435,4436,4437,4438,4439,4440,4441,4442,4443,4444,4445,4446,4447,4448,4449,
  77855. 4450,4451,4452,4453,4454,4455,4456,4457,4458,4459,4460,4461,4462,4463,4464,
  77856. 4465,4466,4467,4468,4469,4470,4471,4472,4473,4474,4475,4476,4477,4478,4479,
  77857. 4480,4481,4482,4483,4484,4485,4486,4487,4488,4489,4490,4491,4492,4493,4494,
  77858. 4495,4496,4497,4498,4499,4500,4501,4502,4503,4504,4505,4506,4507,4508,4509,
  77859. 4510,4511,4512,4513,4514,4515,4516,4517,4518,4519,4520,4521,4522,4523,4524,
  77860. 4525,4526,4527,4528,4529,4530,4531,4532,4533,4534,4535,4536,4537,4538,4539,
  77861. 4540,4541,4542,4543,4544,4545,4546,4547,4548,4549,4550,4551,4552,4553,4554,
  77862. 4555,4556,4557,4558,4559,4560,4561,4562,4563,4564,4565,4566,4567,4568,4569,
  77863. 4570,4571,4572,4573,4574,4575,4576,4577,4578,4579,4580,4581,4582,4583,4584,
  77864. 4585,4586,4587,4588,4589,4590,4591,4592,4593,4594,4595,4596,4597,4598,4599,
  77865. 4600,4601,4602,4603,4604,4605,4606,4607,4608,4609,4610,4611,4612,4613,4614,
  77866. 4615,4616,4617,4618,4619,4620,4621,4622,4623,4624,4625,4626,4627,4628,4629,
  77867. 4630,4631,4632,4633,4634,4635,4636,4637,4638,4639,4640,4641,4642,4643,4644,
  77868. 4645,4646,4647,4648,4649,4650,4651,4652,4653,4654,4655,4656,4657,4658,4659,
  77869. 4660,4661,4662,4663,4664,4665,4666,4667,4668,4669,4670,4671,4672,4673,4674,
  77870. 4675,4676,4677,4678,4679,4680,4681,4682,4683,4684,4685,4686,4687,4688,4689,
  77871. 4690,4691,4692,4693,4694,4695,4696,4697,4698,4699,4700,4701,4702,4703,4704,
  77872. 4705,4706,4707,4708,4709,4710,4711,4712,4713,4714,4715,4716,4717,4718,4719,
  77873. 4720,4721,4722,4723,4724,4725,4726,4727,4728,4729,4730,4731,4732,4733,4734,
  77874. 4735,4736,4737,4738,4739,4740,4741,4742,4743,4744,4745,4746,4747,4748,4749,
  77875. 4750,4751,4752,4753,4754,4755,4756,4757,4758,4759,4760,4761,4762,4763,4764,
  77876. 4765,4766,4767,4768,4769,4770,4771,4772,4773,4774,4775,4776,4777,4778,4779,
  77877. 4780,4781,4782,4783,4784,4785,4786,4787,4788,4789,4790,4791,4792,4793,4794,
  77878. 4795,4796,4797,4798,4799,4800,4801,4802,4803,4804,4805,4806,4807,4808,4809,
  77879. 4810,4811,4812,4813,4814,4815,4816,4817,4818,4819,4820,4821,4822,4823,4824,
  77880. 4825,4826,4827,4828,4829,4830,4831,4832,4833,4834,4835,4836,4837,4838,4839,
  77881. 4840,4841,4842,4843,4844,4845,4846,4847,4848,4849,4850,4851,4852,4853,4854,
  77882. 4855,4856,4857,4858,4859,4860,4861,4862,4863,4864,4865,4866,4867,4868,4869,
  77883. 4870,4871,4872,4873,4874,4875,4876,4877,4878,4879,4880,4881,4882,4883,4884,
  77884. 4885,4886,4887,4888,4889,4890,4891,4892,4893,4894,4895,4896,4897,4898,4899,
  77885. 4900,4901,4902,4903,4904,4905,4906,4907,4908,4909,4910,4911,4912,4913,4914,
  77886. 4915,4916,4917,4918,4919,4920,4921,4922,4923,4924,4925,4926,4927,4928,4929,
  77887. 4930,4931,4932,4933,4934,4935,4936,4937,4938,4939,4940,4941,4942,4943,4944,
  77888. 4945,4946,4947,4948,4949,4950,4951,4952,4953,4954,4955,4956,4957,4958,4959,
  77889. 4960,4961,4962,4963,4964,4965,4966,4967,4968,4969,4970,4971,4972,4973,4974,
  77890. 4975,4976,4977,4978,4979,4980,4981,4982,4983,4984,4985,4986,4987,4988,4989,
  77891. 4990,4991,4992,4993,4994,4995,4996,4997,4998,4999,5000,5001,5002,5003,5004,
  77892. 5005,5006,5007,5008,5009,5010,5011,5012,5013,5014,5015,5016,5017,5018,5019,
  77893. 5020,5021,5022,5023,5024,5025,5026,5027,5028,5029,5030,5031,5032,5033,5034,
  77894. 5035,5036,5037,5038,5039,5040,5041,5042,5043,5044,5045,5046,5047,5048,5049,
  77895. 5050,5051,5052,5053,5054,5055,5056,5057,5058,5059,5060,5061,5062,5063,5064,
  77896. 5065,5066,5067,5068,5069,5070,5071,5072,5073,5074,5075,5076,5077,5078,5079,
  77897. 5080,5081,5082,5083,5084,5085,5086,5087,5088,5089,5090,5091,5092,5093,5094,
  77898. 5095,5096,5097,5098,5099,5100,5101,5102,5103,5104,5105,5106,5107,5108,5109,
  77899. 5110,5111,5104,5105,5106,5107,5108,5109,5118,5119,5120,5121,5122,5123,5124,
  77900. 5125,5126,5127,5128,5129,5130,5131,5132,5133,5134,5135,5136,5137,5138,5139,
  77901. 5140,5141,5142,5143,5144,5145,5146,5147,5148,5149,5150,5151,5152,5153,5154,
  77902. 5155,5156,5157,5158,5159,5160,5161,5162,5163,5164,5165,5166,5167,5168,5169,
  77903. 5170,5171,5172,5173,5174,5175,5176,5177,5178,5179,5180,5181,5182,5183,5184,
  77904. 5185,5186,5187,5188,5189,5190,5191,5192,5193,5194,5195,5196,5197,5198,5199,
  77905. 5200,5201,5202,5203,5204,5205,5206,5207,5208,5209,5210,5211,5212,5213,5214,
  77906. 5215,5216,5217,5218,5219,5220,5221,5222,5223,5224,5225,5226,5227,5228,5229,
  77907. 5230,5231,5232,5233,5234,5235,5236,5237,5238,5239,5240,5241,5242,5243,5244,
  77908. 5245,5246,5247,5248,5249,5250,5251,5252,5253,5254,5255,5256,5257,5258,5259,
  77909. 5260,5261,5262,5263,5264,5265,5266,5267,5268,5269,5270,5271,5272,5273,5274,
  77910. 5275,5276,5277,5278,5279,5280,5281,5282,5283,5284,5285,5286,5287,5288,5289,
  77911. 5290,5291,5292,5293,5294,5295,5296,5297,5298,5299,5300,5301,5302,5303,5304,
  77912. 5305,5306,5307,5308,5309,5310,5311,5312,5313,5314,5315,5316,5317,5318,5319,
  77913. 5320,5321,5322,5323,5324,5325,5326,5327,5328,5329,5330,5331,5332,5333,5334,
  77914. 5335,5336,5337,5338,5339,5340,5341,5342,5343,5344,5345,5346,5347,5348,5349,
  77915. 5350,5351,5352,5353,5354,5355,5356,5357,5358,5359,5360,5361,5362,5363,5364,
  77916. 5365,5366,5367,5368,5369,5370,5371,5372,5373,5374,5375,5376,5377,5378,5379,
  77917. 5380,5381,5382,5383,5384,5385,5386,5387,5388,5389,5390,5391,5392,5393,5394,
  77918. 5395,5396,5397,5398,5399,5400,5401,5402,5403,5404,5405,5406,5407,5408,5409,
  77919. 5410,5411,5412,5413,5414,5415,5416,5417,5418,5419,5420,5421,5422,5423,5424,
  77920. 5425,5426,5427,5428,5429,5430,5431,5432,5433,5434,5435,5436,5437,5438,5439,
  77921. 5440,5441,5442,5443,5444,5445,5446,5447,5448,5449,5450,5451,5452,5453,5454,
  77922. 5455,5456,5457,5458,5459,5460,5461,5462,5463,5464,5465,5466,5467,5468,5469,
  77923. 5470,5471,5472,5473,5474,5475,5476,5477,5478,5479,5480,5481,5482,5483,5484,
  77924. 5485,5486,5487,5488,5489,5490,5491,5492,5493,5494,5495,5496,5497,5498,5499,
  77925. 5500,5501,5502,5503,5504,5505,5506,5507,5508,5509,5510,5511,5512,5513,5514,
  77926. 5515,5516,5517,5518,5519,5520,5521,5522,5523,5524,5525,5526,5527,5528,5529,
  77927. 5530,5531,5532,5533,5534,5535,5536,5537,5538,5539,5540,5541,5542,5543,5544,
  77928. 5545,5546,5547,5548,5549,5550,5551,5552,5553,5554,5555,5556,5557,5558,5559,
  77929. 5560,5561,5562,5563,5564,5565,5566,5567,5568,5569,5570,5571,5572,5573,5574,
  77930. 5575,5576,5577,5578,5579,5580,5581,5582,5583,5584,5585,5586,5587,5588,5589,
  77931. 5590,5591,5592,5593,5594,5595,5596,5597,5598,5599,5600,5601,5602,5603,5604,
  77932. 5605,5606,5607,5608,5609,5610,5611,5612,5613,5614,5615,5616,5617,5618,5619,
  77933. 5620,5621,5622,5623,5624,5625,5626,5627,5628,5629,5630,5631,5632,5633,5634,
  77934. 5635,5636,5637,5638,5639,5640,5641,5642,5643,5644,5645,5646,5647,5648,5649,
  77935. 5650,5651,5652,5653,5654,5655,5656,5657,5658,5659,5660,5661,5662,5663,5664,
  77936. 5665,5666,5667,5668,5669,5670,5671,5672,5673,5674,5675,5676,5677,5678,5679,
  77937. 5680,5681,5682,5683,5684,5685,5686,5687,5688,5689,5690,5691,5692,5693,5694,
  77938. 5695,5696,5697,5698,5699,5700,5701,5702,5703,5704,5705,5706,5707,5708,5709,
  77939. 5710,5711,5712,5713,5714,5715,5716,5717,5718,5719,5720,5721,5722,5723,5724,
  77940. 5725,5726,5727,5728,5729,5730,5731,5732,5733,5734,5735,5736,5737,5738,5739,
  77941. 5740,5741,5742,5743,5744,5745,5746,5747,5748,5749,5750,5751,5752,5753,5754,
  77942. 5755,5756,5757,5758,5759,5760,5761,5762,5763,5764,5765,5766,5767,5768,5769,
  77943. 5770,5771,5772,5773,5774,5775,5776,5777,5778,5779,5780,5781,5782,5783,5784,
  77944. 5785,5786,5787,5788,5789,5790,5791,5792,5793,5794,5795,5796,5797,5798,5799,
  77945. 5800,5801,5802,5803,5804,5805,5806,5807,5808,5809,5810,5811,5812,5813,5814,
  77946. 5815,5816,5817,5818,5819,5820,5821,5822,5823,5824,5825,5826,5827,5828,5829,
  77947. 5830,5831,5832,5833,5834,5835,5836,5837,5838,5839,5840,5841,5842,5843,5844,
  77948. 5845,5846,5847,5848,5849,5850,5851,5852,5853,5854,5855,5856,5857,5858,5859,
  77949. 5860,5861,5862,5863,5864,5865,5866,5867,5868,5869,5870,5871,5872,5873,5874,
  77950. 5875,5876,5877,5878,5879,5880,5881,5882,5883,5884,5885,5886,5887,5888,5889,
  77951. 5890,5891,5892,5893,5894,5895,5896,5897,5898,5899,5900,5901,5902,5903,5904,
  77952. 5905,5906,5907,5908,5909,5910,5911,5912,5913,5914,5915,5916,5917,5918,5919,
  77953. 5920,5921,5922,5923,5924,5925,5926,5927,5928,5929,5930,5931,5932,5933,5934,
  77954. 5935,5936,5937,5938,5939,5940,5941,5942,5943,5944,5945,5946,5947,5948,5949,
  77955. 5950,5951,5952,5953,5954,5955,5956,5957,5958,5959,5960,5961,5962,5963,5964,
  77956. 5965,5966,5967,5968,5969,5970,5971,5972,5973,5974,5975,5976,5977,5978,5979,
  77957. 5980,5981,5982,5983,5984,5985,5986,5987,5988,5989,5990,5991,5992,5993,5994,
  77958. 5995,5996,5997,5998,5999,6000,6001,6002,6003,6004,6005,6006,6007,6008,6009,
  77959. 6010,6011,6012,6013,6014,6015,6016,6017,6018,6019,6020,6021,6022,6023,6024,
  77960. 6025,6026,6027,6028,6029,6030,6031,6032,6033,6034,6035,6036,6037,6038,6039,
  77961. 6040,6041,6042,6043,6044,6045,6046,6047,6048,6049,6050,6051,6052,6053,6054,
  77962. 6055,6056,6057,6058,6059,6060,6061,6062,6063,6064,6065,6066,6067,6068,6069,
  77963. 6070,6071,6072,6073,6074,6075,6076,6077,6078,6079,6080,6081,6082,6083,6084,
  77964. 6085,6086,6087,6088,6089,6090,6091,6092,6093,6094,6095,6096,6097,6098,6099,
  77965. 6100,6101,6102,6103,6104,6105,6106,6107,6108,6109,6110,6111,6112,6113,6114,
  77966. 6115,6116,6117,6118,6119,6120,6121,6122,6123,6124,6125,6126,6127,6128,6129,
  77967. 6130,6131,6132,6133,6134,6135,6136,6137,6138,6139,6140,6141,6142,6143,6144,
  77968. 6145,6146,6147,6148,6149,6150,6151,6152,6153,6154,6155,6156,6157,6158,6159,
  77969. 6160,6161,6162,6163,6164,6165,6166,6167,6168,6169,6170,6171,6172,6173,6174,
  77970. 6175,6176,6177,6178,6179,6180,6181,6182,6183,6184,6185,6186,6187,6188,6189,
  77971. 6190,6191,6192,6193,6194,6195,6196,6197,6198,6199,6200,6201,6202,6203,6204,
  77972. 6205,6206,6207,6208,6209,6210,6211,6212,6213,6214,6215,6216,6217,6218,6219,
  77973. 6220,6221,6222,6223,6224,6225,6226,6227,6228,6229,6230,6231,6232,6233,6234,
  77974. 6235,6236,6237,6238,6239,6240,6241,6242,6243,6244,6245,6246,6247,6248,6249,
  77975. 6250,6251,6252,6253,6254,6255,6256,6257,6258,6259,6260,6261,6262,6263,6264,
  77976. 6265,6266,6267,6268,6269,6270,6271,6272,6273,6274,6275,6276,6277,6278,6279,
  77977. 6280,6281,6282,6283,6284,6285,6286,6287,6288,6289,6290,6291,6292,6293,6294,
  77978. 6295,6296,6297,6298,6299,6300,6301,6302,6303,6304,6305,6306,6307,6308,6309,
  77979. 6310,6311,6312,6313,6314,6315,6316,6317,6318,6319,6320,6321,6322,6323,6324,
  77980. 6325,6326,6327,6328,6329,6330,6331,6332,6333,6334,6335,6336,6337,6338,6339,
  77981. 6340,6341,6342,6343,6344,6345,6346,6347,6348,6349,6350,6351,6352,6353,6354,
  77982. 6355,6356,6357,6358,6359,6360,6361,6362,6363,6364,6365,6366,6367,6368,6369,
  77983. 6370,6371,6372,6373,6374,6375,6376,6377,6378,6379,6380,6381,6382,6383,6384,
  77984. 6385,6386,6387,6388,6389,6390,6391,6392,6393,6394,6395,6396,6397,6398,6399,
  77985. 6400,6401,6402,6403,6404,6405,6406,6407,6408,6409,6410,6411,6412,6413,6414,
  77986. 6415,6416,6417,6418,6419,6420,6421,6422,6423,6424,6425,6426,6427,6428,6429,
  77987. 6430,6431,6432,6433,6434,6435,6436,6437,6438,6439,6440,6441,6442,6443,6444,
  77988. 6445,6446,6447,6448,6449,6450,6451,6452,6453,6454,6455,6456,6457,6458,6459,
  77989. 6460,6461,6462,6463,6464,6465,6466,6467,6468,6469,6470,6471,6472,6473,6474,
  77990. 6475,6476,6477,6478,6479,6480,6481,6482,6483,6484,6485,6486,6487,6488,6489,
  77991. 6490,6491,6492,6493,6494,6495,6496,6497,6498,6499,6500,6501,6502,6503,6504,
  77992. 6505,6506,6507,6508,6509,6510,6511,6512,6513,6514,6515,6516,6517,6518,6519,
  77993. 6520,6521,6522,6523,6524,6525,6526,6527,6528,6529,6530,6531,6532,6533,6534,
  77994. 6535,6536,6537,6538,6539,6540,6541,6542,6543,6544,6545,6546,6547,6548,6549,
  77995. 6550,6551,6552,6553,6554,6555,6556,6557,6558,6559,6560,6561,6562,6563,6564,
  77996. 6565,6566,6567,6568,6569,6570,6571,6572,6573,6574,6575,6576,6577,6578,6579,
  77997. 6580,6581,6582,6583,6584,6585,6586,6587,6588,6589,6590,6591,6592,6593,6594,
  77998. 6595,6596,6597,6598,6599,6600,6601,6602,6603,6604,6605,6606,6607,6608,6609,
  77999. 6610,6611,6612,6613,6614,6615,6616,6617,6618,6619,6620,6621,6622,6623,6624,
  78000. 6625,6626,6627,6628,6629,6630,6631,6632,6633,6634,6635,6636,6637,6638,6639,
  78001. 6640,6641,6642,6643,6644,6645,6646,6647,6648,6649,6650,6651,6652,6653,6654,
  78002. 6655,6656,6657,6658,6659,6660,6661,6662,6663,6664,6665,6666,6667,6668,6669,
  78003. 6670,6671,6672,6673,6674,6675,6676,6677,6678,6679,6680,6681,6682,6683,6684,
  78004. 6685,6686,6687,6688,6689,6690,6691,6692,6693,6694,6695,6696,6697,6698,6699,
  78005. 6700,6701,6702,6703,6704,6705,6706,6707,6708,6709,6710,6711,6712,6713,6714,
  78006. 6715,6716,6717,6718,6719,6720,6721,6722,6723,6724,6725,6726,6727,6728,6729,
  78007. 6730,6731,6732,6733,6734,6735,6736,6737,6738,6739,6740,6741,6742,6743,6744,
  78008. 6745,6746,6747,6748,6749,6750,6751,6752,6753,6754,6755,6756,6757,6758,6759,
  78009. 6760,6761,6762,6763,6764,6765,6766,6767,6768,6769,6770,6771,6772,6773,6774,
  78010. 6775,6776,6777,6778,6779,6780,6781,6782,6783,6784,6785,6786,6787,6788,6789,
  78011. 6790,6791,6792,6793,6794,6795,6796,6797,6798,6799,6800,6801,6802,6803,6804,
  78012. 6805,6806,6807,6808,6809,6810,6811,6812,6813,6814,6815,6816,6817,6818,6819,
  78013. 6820,6821,6822,6823,6824,6825,6826,6827,6828,6829,6830,6831,6832,6833,6834,
  78014. 6835,6836,6837,6838,6839,6840,6841,6842,6843,6844,6845,6846,6847,6848,6849,
  78015. 6850,6851,6852,6853,6854,6855,6856,6857,6858,6859,6860,6861,6862,6863,6864,
  78016. 6865,6866,6867,6868,6869,6870,6871,6872,6873,6874,6875,6876,6877,6878,6879,
  78017. 6880,6881,6882,6883,6884,6885,6886,6887,6888,6889,6890,6891,6892,6893,6894,
  78018. 6895,6896,6897,6898,6899,6900,6901,6902,6903,6904,6905,6906,6907,6908,6909,
  78019. 6910,6911,6912,6913,6914,6915,6916,6917,6918,6919,6920,6921,6922,6923,6924,
  78020. 6925,6926,6927,6928,6929,6930,6931,6932,6933,6934,6935,6936,6937,6938,6939,
  78021. 6940,6941,6942,6943,6944,6945,6946,6947,6948,6949,6950,6951,6952,6953,6954,
  78022. 6955,6956,6957,6958,6959,6960,6961,6962,6963,6964,6965,6966,6967,6968,6969,
  78023. 6970,6971,6972,6973,6974,6975,6976,6977,6978,6979,6980,6981,6982,6983,6984,
  78024. 6985,6986,6987,6988,6989,6990,6991,6992,6993,6994,6995,6996,6997,6998,6999,
  78025. 7000,7001,7002,7003,7004,7005,7006,7007,7008,7009,7010,7011,7012,7013,7014,
  78026. 7015,7016,7017,7018,7019,7020,7021,7022,7023,7024,7025,7026,7027,7028,7029,
  78027. 7030,7031,7032,7033,7034,7035,7036,7037,7038,7039,7040,7041,7042,7043,7044,
  78028. 7045,7046,7047,7048,7049,7050,7051,7052,7053,7054,7055,7056,7057,7058,7059,
  78029. 7060,7061,7062,7063,7064,7065,7066,7067,7068,7069,7070,7071,7072,7073,7074,
  78030. 7075,7076,7077,7078,7079,7080,7081,7082,7083,7084,7085,7086,7087,7088,7089,
  78031. 7090,7091,7092,7093,7094,7095,7096,7097,7098,7099,7100,7101,7102,7103,7104,
  78032. 7105,7106,7107,7108,7109,7110,7111,7112,7113,7114,7115,7116,7117,7118,7119,
  78033. 7120,7121,7122,7123,7124,7125,7126,7127,7128,7129,7130,7131,7132,7133,7134,
  78034. 7135,7136,7137,7138,7139,7140,7141,7142,7143,7144,7145,7146,7147,7148,7149,
  78035. 7150,7151,7152,7153,7154,7155,7156,7157,7158,7159,7160,7161,7162,7163,7164,
  78036. 7165,7166,7167,7168,7169,7170,7171,7172,7173,7174,7175,7176,7177,7178,7179,
  78037. 7180,7181,7182,7183,7184,7185,7186,7187,7188,7189,7190,7191,7192,7193,7194,
  78038. 7195,7196,7197,7198,7199,7200,7201,7202,7203,7204,7205,7206,7207,7208,7209,
  78039. 7210,7211,7212,7213,7214,7215,7216,7217,7218,7219,7220,7221,7222,7223,7224,
  78040. 7225,7226,7227,7228,7229,7230,7231,7232,7233,7234,7235,7236,7237,7238,7239,
  78041. 7240,7241,7242,7243,7244,7245,7246,7247,7248,7249,7250,7251,7252,7253,7254,
  78042. 7255,7256,7257,7258,7259,7260,7261,7262,7263,7264,7265,7266,7267,7268,7269,
  78043. 7270,7271,7272,7273,7274,7275,7276,7277,7278,7279,7280,7281,7282,7283,7284,
  78044. 7285,7286,7287,7288,7289,7290,7291,7292,7293,7294,7295,1042,1044,1054,1057,
  78045. 1058,1058,1066,1122,42570L,7305,7306,7307,7308,7309,7310,7311,7312,7313,
  78046. 7314,7315,7316,7317,7318,7319,7320,7321,7322,7323,7324,7325,7326,7327,7328,
  78047. 7329,7330,7331,7332,7333,7334,7335,7336,7337,7338,7339,7340,7341,7342,7343,
  78048. 7344,7345,7346,7347,7348,7349,7350,7351,7352,7353,7354,7355,7356,7357,7358,
  78049. 7359,7360,7361,7362,7363,7364,7365,7366,7367,7368,7369,7370,7371,7372,7373,
  78050. 7374,7375,7376,7377,7378,7379,7380,7381,7382,7383,7384,7385,7386,7387,7388,
  78051. 7389,7390,7391,7392,7393,7394,7395,7396,7397,7398,7399,7400,7401,7402,7403,
  78052. 7404,7405,7406,7407,7408,7409,7410,7411,7412,7413,7414,7415,7416,7417,7418,
  78053. 7419,7420,7421,7422,7423,7424,7425,7426,7427,7428,7429,7430,7431,7432,7433,
  78054. 7434,7435,7436,7437,7438,7439,7440,7441,7442,7443,7444,7445,7446,7447,7448,
  78055. 7449,7450,7451,7452,7453,7454,7455,7456,7457,7458,7459,7460,7461,7462,7463,
  78056. 7464,7465,7466,7467,7468,7469,7470,7471,7472,7473,7474,7475,7476,7477,7478,
  78057. 7479,7480,7481,7482,7483,7484,7485,7486,7487,7488,7489,7490,7491,7492,7493,
  78058. 7494,7495,7496,7497,7498,7499,7500,7501,7502,7503,7504,7505,7506,7507,7508,
  78059. 7509,7510,7511,7512,7513,7514,7515,7516,7517,7518,7519,7520,7521,7522,7523,
  78060. 7524,7525,7526,7527,7528,7529,7530,7531,7532,7533,7534,7535,7536,7537,7538,
  78061. 7539,7540,7541,7542,7543,7544,42877L,7546,7547,7548,11363,7550,7551,7552,
  78062. 7553,7554,7555,7556,7557,7558,7559,7560,7561,7562,7563,7564,7565,7566,7567,
  78063. 7568,7569,7570,7571,7572,7573,7574,7575,7576,7577,7578,7579,7580,7581,7582,
  78064. 7583,7584,7585,7586,7587,7588,7589,7590,7591,7592,7593,7594,7595,7596,7597,
  78065. 7598,7599,7600,7601,7602,7603,7604,7605,7606,7607,7608,7609,7610,7611,7612,
  78066. 7613,7614,7615,7616,7617,7618,7619,7620,7621,7622,7623,7624,7625,7626,7627,
  78067. 7628,7629,7630,7631,7632,7633,7634,7635,7636,7637,7638,7639,7640,7641,7642,
  78068. 7643,7644,7645,7646,7647,7648,7649,7650,7651,7652,7653,7654,7655,7656,7657,
  78069. 7658,7659,7660,7661,7662,7663,7664,7665,7666,7667,7668,7669,7670,7671,7672,
  78070. 7673,7674,7675,7676,7677,7678,7679,7680,7680,7682,7682,7684,7684,7686,7686,
  78071. 7688,7688,7690,7690,7692,7692,7694,7694,7696,7696,7698,7698,7700,7700,7702,
  78072. 7702,7704,7704,7706,7706,7708,7708,7710,7710,7712,7712,7714,7714,7716,7716,
  78073. 7718,7718,7720,7720,7722,7722,7724,7724,7726,7726,7728,7728,7730,7730,7732,
  78074. 7732,7734,7734,7736,7736,7738,7738,7740,7740,7742,7742,7744,7744,7746,7746,
  78075. 7748,7748,7750,7750,7752,7752,7754,7754,7756,7756,7758,7758,7760,7760,7762,
  78076. 7762,7764,7764,7766,7766,7768,7768,7770,7770,7772,7772,7774,7774,7776,7776,
  78077. 7778,7778,7780,7780,7782,7782,7784,7784,7786,7786,7788,7788,7790,7790,7792,
  78078. 7792,7794,7794,7796,7796,7798,7798,7800,7800,7802,7802,7804,7804,7806,7806,
  78079. 7808,7808,7810,7810,7812,7812,7814,7814,7816,7816,7818,7818,7820,7820,7822,
  78080. 7822,7824,7824,7826,7826,7828,7828,7830,7831,7832,7833,7834,7776,7836,7837,
  78081. 7838,7839,7840,7840,7842,7842,7844,7844,7846,7846,7848,7848,7850,7850,7852,
  78082. 7852,7854,7854,7856,7856,7858,7858,7860,7860,7862,7862,7864,7864,7866,7866,
  78083. 7868,7868,7870,7870,7872,7872,7874,7874,7876,7876,7878,7878,7880,7880,7882,
  78084. 7882,7884,7884,7886,7886,7888,7888,7890,7890,7892,7892,7894,7894,7896,7896,
  78085. 7898,7898,7900,7900,7902,7902,7904,7904,7906,7906,7908,7908,7910,7910,7912,
  78086. 7912,7914,7914,7916,7916,7918,7918,7920,7920,7922,7922,7924,7924,7926,7926,
  78087. 7928,7928,7930,7930,7932,7932,7934,7934,7944,7945,7946,7947,7948,7949,7950,
  78088. 7951,7944,7945,7946,7947,7948,7949,7950,7951,7960,7961,7962,7963,7964,7965,
  78089. 7958,7959,7960,7961,7962,7963,7964,7965,7966,7967,7976,7977,7978,7979,7980,
  78090. 7981,7982,7983,7976,7977,7978,7979,7980,7981,7982,7983,7992,7993,7994,7995,
  78091. 7996,7997,7998,7999,7992,7993,7994,7995,7996,7997,7998,7999,8008,8009,8010,
  78092. 8011,8012,8013,8006,8007,8008,8009,8010,8011,8012,8013,8014,8015,8016,8025,
  78093. 8018,8027,8020,8029,8022,8031,8024,8025,8026,8027,8028,8029,8030,8031,8040,
  78094. 8041,8042,8043,8044,8045,8046,8047,8040,8041,8042,8043,8044,8045,8046,8047,
  78095. 8122,8123,8136,8137,8138,8139,8154,8155,8184,8185,8170,8171,8186,8187,8062,
  78096. 8063,8064,8065,8066,8067,8068,8069,8070,8071,8072,8073,8074,8075,8076,8077,
  78097. 8078,8079,8080,8081,8082,8083,8084,8085,8086,8087,8088,8089,8090,8091,8092,
  78098. 8093,8094,8095,8096,8097,8098,8099,8100,8101,8102,8103,8104,8105,8106,8107,
  78099. 8108,8109,8110,8111,8120,8121,8114,8115,8116,8117,8118,8119,8120,8121,8122,
  78100. 8123,8124,8125,921,8127,8128,8129,8130,8131,8132,8133,8134,8135,8136,8137,
  78101. 8138,8139,8140,8141,8142,8143,8152,8153,8146,8147,8148,8149,8150,8151,8152,
  78102. 8153,8154,8155,8156,8157,8158,8159,8168,8169,8162,8163,8164,8172,8166,8167,
  78103. 8168,8169,8170,8171,8172,8173,8174,8175,8176,8177,8178,8179,8180,8181,8182,
  78104. 8183,8184,8185,8186,8187,8188,8189,8190,8191,8192,8193,8194,8195,8196,8197,
  78105. 8198,8199,8200,8201,8202,8203,8204,8205,8206,8207,8208,8209,8210,8211,8212,
  78106. 8213,8214,8215,8216,8217,8218,8219,8220,8221,8222,8223,8224,8225,8226,8227,
  78107. 8228,8229,8230,8231,8232,8233,8234,8235,8236,8237,8238,8239,8240,8241,8242,
  78108. 8243,8244,8245,8246,8247,8248,8249,8250,8251,8252,8253,8254,8255,8256,8257,
  78109. 8258,8259,8260,8261,8262,8263,8264,8265,8266,8267,8268,8269,8270,8271,8272,
  78110. 8273,8274,8275,8276,8277,8278,8279,8280,8281,8282,8283,8284,8285,8286,8287,
  78111. 8288,8289,8290,8291,8292,8293,8294,8295,8296,8297,8298,8299,8300,8301,8302,
  78112. 8303,8304,8305,8306,8307,8308,8309,8310,8311,8312,8313,8314,8315,8316,8317,
  78113. 8318,8319,8320,8321,8322,8323,8324,8325,8326,8327,8328,8329,8330,8331,8332,
  78114. 8333,8334,8335,8336,8337,8338,8339,8340,8341,8342,8343,8344,8345,8346,8347,
  78115. 8348,8349,8350,8351,8352,8353,8354,8355,8356,8357,8358,8359,8360,8361,8362,
  78116. 8363,8364,8365,8366,8367,8368,8369,8370,8371,8372,8373,8374,8375,8376,8377,
  78117. 8378,8379,8380,8381,8382,8383,8384,8385,8386,8387,8388,8389,8390,8391,8392,
  78118. 8393,8394,8395,8396,8397,8398,8399,8400,8401,8402,8403,8404,8405,8406,8407,
  78119. 8408,8409,8410,8411,8412,8413,8414,8415,8416,8417,8418,8419,8420,8421,8422,
  78120. 8423,8424,8425,8426,8427,8428,8429,8430,8431,8432,8433,8434,8435,8436,8437,
  78121. 8438,8439,8440,8441,8442,8443,8444,8445,8446,8447,8448,8449,8450,8451,8452,
  78122. 8453,8454,8455,8456,8457,8458,8459,8460,8461,8462,8463,8464,8465,8466,8467,
  78123. 8468,8469,8470,8471,8472,8473,8474,8475,8476,8477,8478,8479,8480,8481,8482,
  78124. 8483,8484,8485,8486,8487,8488,8489,8490,8491,8492,8493,8494,8495,8496,8497,
  78125. 8498,8499,8500,8501,8502,8503,8504,8505,8506,8507,8508,8509,8510,8511,8512,
  78126. 8513,8514,8515,8516,8517,8518,8519,8520,8521,8522,8523,8524,8525,8498,8527,
  78127. 8528,8529,8530,8531,8532,8533,8534,8535,8536,8537,8538,8539,8540,8541,8542,
  78128. 8543,8544,8545,8546,8547,8548,8549,8550,8551,8552,8553,8554,8555,8556,8557,
  78129. 8558,8559,8544,8545,8546,8547,8548,8549,8550,8551,8552,8553,8554,8555,8556,
  78130. 8557,8558,8559,8576,8577,8578,8579,8579,8581,8582,8583,8584,8585,8586,8587,
  78131. 8588,8589,8590,8591,8592,8593,8594,8595,8596,8597,8598,8599,8600,8601,8602,
  78132. 8603,8604,8605,8606,8607,8608,8609,8610,8611,8612,8613,8614,8615,8616,8617,
  78133. 8618,8619,8620,8621,8622,8623,8624,8625,8626,8627,8628,8629,8630,8631,8632,
  78134. 8633,8634,8635,8636,8637,8638,8639,8640,8641,8642,8643,8644,8645,8646,8647,
  78135. 8648,8649,8650,8651,8652,8653,8654,8655,8656,8657,8658,8659,8660,8661,8662,
  78136. 8663,8664,8665,8666,8667,8668,8669,8670,8671,8672,8673,8674,8675,8676,8677,
  78137. 8678,8679,8680,8681,8682,8683,8684,8685,8686,8687,8688,8689,8690,8691,8692,
  78138. 8693,8694,8695,8696,8697,8698,8699,8700,8701,8702,8703,8704,8705,8706,8707,
  78139. 8708,8709,8710,8711,8712,8713,8714,8715,8716,8717,8718,8719,8720,8721,8722,
  78140. 8723,8724,8725,8726,8727,8728,8729,8730,8731,8732,8733,8734,8735,8736,8737,
  78141. 8738,8739,8740,8741,8742,8743,8744,8745,8746,8747,8748,8749,8750,8751,8752,
  78142. 8753,8754,8755,8756,8757,8758,8759,8760,8761,8762,8763,8764,8765,8766,8767,
  78143. 8768,8769,8770,8771,8772,8773,8774,8775,8776,8777,8778,8779,8780,8781,8782,
  78144. 8783,8784,8785,8786,8787,8788,8789,8790,8791,8792,8793,8794,8795,8796,8797,
  78145. 8798,8799,8800,8801,8802,8803,8804,8805,8806,8807,8808,8809,8810,8811,8812,
  78146. 8813,8814,8815,8816,8817,8818,8819,8820,8821,8822,8823,8824,8825,8826,8827,
  78147. 8828,8829,8830,8831,8832,8833,8834,8835,8836,8837,8838,8839,8840,8841,8842,
  78148. 8843,8844,8845,8846,8847,8848,8849,8850,8851,8852,8853,8854,8855,8856,8857,
  78149. 8858,8859,8860,8861,8862,8863,8864,8865,8866,8867,8868,8869,8870,8871,8872,
  78150. 8873,8874,8875,8876,8877,8878,8879,8880,8881,8882,8883,8884,8885,8886,8887,
  78151. 8888,8889,8890,8891,8892,8893,8894,8895,8896,8897,8898,8899,8900,8901,8902,
  78152. 8903,8904,8905,8906,8907,8908,8909,8910,8911,8912,8913,8914,8915,8916,8917,
  78153. 8918,8919,8920,8921,8922,8923,8924,8925,8926,8927,8928,8929,8930,8931,8932,
  78154. 8933,8934,8935,8936,8937,8938,8939,8940,8941,8942,8943,8944,8945,8946,8947,
  78155. 8948,8949,8950,8951,8952,8953,8954,8955,8956,8957,8958,8959,8960,8961,8962,
  78156. 8963,8964,8965,8966,8967,8968,8969,8970,8971,8972,8973,8974,8975,8976,8977,
  78157. 8978,8979,8980,8981,8982,8983,8984,8985,8986,8987,8988,8989,8990,8991,8992,
  78158. 8993,8994,8995,8996,8997,8998,8999,9000,9001,9002,9003,9004,9005,9006,9007,
  78159. 9008,9009,9010,9011,9012,9013,9014,9015,9016,9017,9018,9019,9020,9021,9022,
  78160. 9023,9024,9025,9026,9027,9028,9029,9030,9031,9032,9033,9034,9035,9036,9037,
  78161. 9038,9039,9040,9041,9042,9043,9044,9045,9046,9047,9048,9049,9050,9051,9052,
  78162. 9053,9054,9055,9056,9057,9058,9059,9060,9061,9062,9063,9064,9065,9066,9067,
  78163. 9068,9069,9070,9071,9072,9073,9074,9075,9076,9077,9078,9079,9080,9081,9082,
  78164. 9083,9084,9085,9086,9087,9088,9089,9090,9091,9092,9093,9094,9095,9096,9097,
  78165. 9098,9099,9100,9101,9102,9103,9104,9105,9106,9107,9108,9109,9110,9111,9112,
  78166. 9113,9114,9115,9116,9117,9118,9119,9120,9121,9122,9123,9124,9125,9126,9127,
  78167. 9128,9129,9130,9131,9132,9133,9134,9135,9136,9137,9138,9139,9140,9141,9142,
  78168. 9143,9144,9145,9146,9147,9148,9149,9150,9151,9152,9153,9154,9155,9156,9157,
  78169. 9158,9159,9160,9161,9162,9163,9164,9165,9166,9167,9168,9169,9170,9171,9172,
  78170. 9173,9174,9175,9176,9177,9178,9179,9180,9181,9182,9183,9184,9185,9186,9187,
  78171. 9188,9189,9190,9191,9192,9193,9194,9195,9196,9197,9198,9199,9200,9201,9202,
  78172. 9203,9204,9205,9206,9207,9208,9209,9210,9211,9212,9213,9214,9215,9216,9217,
  78173. 9218,9219,9220,9221,9222,9223,9224,9225,9226,9227,9228,9229,9230,9231,9232,
  78174. 9233,9234,9235,9236,9237,9238,9239,9240,9241,9242,9243,9244,9245,9246,9247,
  78175. 9248,9249,9250,9251,9252,9253,9254,9255,9256,9257,9258,9259,9260,9261,9262,
  78176. 9263,9264,9265,9266,9267,9268,9269,9270,9271,9272,9273,9274,9275,9276,9277,
  78177. 9278,9279,9280,9281,9282,9283,9284,9285,9286,9287,9288,9289,9290,9291,9292,
  78178. 9293,9294,9295,9296,9297,9298,9299,9300,9301,9302,9303,9304,9305,9306,9307,
  78179. 9308,9309,9310,9311,9312,9313,9314,9315,9316,9317,9318,9319,9320,9321,9322,
  78180. 9323,9324,9325,9326,9327,9328,9329,9330,9331,9332,9333,9334,9335,9336,9337,
  78181. 9338,9339,9340,9341,9342,9343,9344,9345,9346,9347,9348,9349,9350,9351,9352,
  78182. 9353,9354,9355,9356,9357,9358,9359,9360,9361,9362,9363,9364,9365,9366,9367,
  78183. 9368,9369,9370,9371,9372,9373,9374,9375,9376,9377,9378,9379,9380,9381,9382,
  78184. 9383,9384,9385,9386,9387,9388,9389,9390,9391,9392,9393,9394,9395,9396,9397,
  78185. 9398,9399,9400,9401,9402,9403,9404,9405,9406,9407,9408,9409,9410,9411,9412,
  78186. 9413,9414,9415,9416,9417,9418,9419,9420,9421,9422,9423,9398,9399,9400,9401,
  78187. 9402,9403,9404,9405,9406,9407,9408,9409,9410,9411,9412,9413,9414,9415,9416,
  78188. 9417,9418,9419,9420,9421,9422,9423,9450,9451,9452,9453,9454,9455,9456,9457,
  78189. 9458,9459,9460,9461,9462,9463,9464,9465,9466,9467,9468,9469,9470,9471,9472,
  78190. 9473,9474,9475,9476,9477,9478,9479,9480,9481,9482,9483,9484,9485,9486,9487,
  78191. 9488,9489,9490,9491,9492,9493,9494,9495,9496,9497,9498,9499,9500,9501,9502,
  78192. 9503,9504,9505,9506,9507,9508,9509,9510,9511,9512,9513,9514,9515,9516,9517,
  78193. 9518,9519,9520,9521,9522,9523,9524,9525,9526,9527,9528,9529,9530,9531,9532,
  78194. 9533,9534,9535,9536,9537,9538,9539,9540,9541,9542,9543,9544,9545,9546,9547,
  78195. 9548,9549,9550,9551,9552,9553,9554,9555,9556,9557,9558,9559,9560,9561,9562,
  78196. 9563,9564,9565,9566,9567,9568,9569,9570,9571,9572,9573,9574,9575,9576,9577,
  78197. 9578,9579,9580,9581,9582,9583,9584,9585,9586,9587,9588,9589,9590,9591,9592,
  78198. 9593,9594,9595,9596,9597,9598,9599,9600,9601,9602,9603,9604,9605,9606,9607,
  78199. 9608,9609,9610,9611,9612,9613,9614,9615,9616,9617,9618,9619,9620,9621,9622,
  78200. 9623,9624,9625,9626,9627,9628,9629,9630,9631,9632,9633,9634,9635,9636,9637,
  78201. 9638,9639,9640,9641,9642,9643,9644,9645,9646,9647,9648,9649,9650,9651,9652,
  78202. 9653,9654,9655,9656,9657,9658,9659,9660,9661,9662,9663,9664,9665,9666,9667,
  78203. 9668,9669,9670,9671,9672,9673,9674,9675,9676,9677,9678,9679,9680,9681,9682,
  78204. 9683,9684,9685,9686,9687,9688,9689,9690,9691,9692,9693,9694,9695,9696,9697,
  78205. 9698,9699,9700,9701,9702,9703,9704,9705,9706,9707,9708,9709,9710,9711,9712,
  78206. 9713,9714,9715,9716,9717,9718,9719,9720,9721,9722,9723,9724,9725,9726,9727,
  78207. 9728,9729,9730,9731,9732,9733,9734,9735,9736,9737,9738,9739,9740,9741,9742,
  78208. 9743,9744,9745,9746,9747,9748,9749,9750,9751,9752,9753,9754,9755,9756,9757,
  78209. 9758,9759,9760,9761,9762,9763,9764,9765,9766,9767,9768,9769,9770,9771,9772,
  78210. 9773,9774,9775,9776,9777,9778,9779,9780,9781,9782,9783,9784,9785,9786,9787,
  78211. 9788,9789,9790,9791,9792,9793,9794,9795,9796,9797,9798,9799,9800,9801,9802,
  78212. 9803,9804,9805,9806,9807,9808,9809,9810,9811,9812,9813,9814,9815,9816,9817,
  78213. 9818,9819,9820,9821,9822,9823,9824,9825,9826,9827,9828,9829,9830,9831,9832,
  78214. 9833,9834,9835,9836,9837,9838,9839,9840,9841,9842,9843,9844,9845,9846,9847,
  78215. 9848,9849,9850,9851,9852,9853,9854,9855,9856,9857,9858,9859,9860,9861,9862,
  78216. 9863,9864,9865,9866,9867,9868,9869,9870,9871,9872,9873,9874,9875,9876,9877,
  78217. 9878,9879,9880,9881,9882,9883,9884,9885,9886,9887,9888,9889,9890,9891,9892,
  78218. 9893,9894,9895,9896,9897,9898,9899,9900,9901,9902,9903,9904,9905,9906,9907,
  78219. 9908,9909,9910,9911,9912,9913,9914,9915,9916,9917,9918,9919,9920,9921,9922,
  78220. 9923,9924,9925,9926,9927,9928,9929,9930,9931,9932,9933,9934,9935,9936,9937,
  78221. 9938,9939,9940,9941,9942,9943,9944,9945,9946,9947,9948,9949,9950,9951,9952,
  78222. 9953,9954,9955,9956,9957,9958,9959,9960,9961,9962,9963,9964,9965,9966,9967,
  78223. 9968,9969,9970,9971,9972,9973,9974,9975,9976,9977,9978,9979,9980,9981,9982,
  78224. 9983,9984,9985,9986,9987,9988,9989,9990,9991,9992,9993,9994,9995,9996,9997,
  78225. 9998,9999,10000,10001,10002,10003,10004,10005,10006,10007,10008,10009,
  78226. 10010,10011,10012,10013,10014,10015,10016,10017,10018,10019,10020,10021,
  78227. 10022,10023,10024,10025,10026,10027,10028,10029,10030,10031,10032,10033,
  78228. 10034,10035,10036,10037,10038,10039,10040,10041,10042,10043,10044,10045,
  78229. 10046,10047,10048,10049,10050,10051,10052,10053,10054,10055,10056,10057,
  78230. 10058,10059,10060,10061,10062,10063,10064,10065,10066,10067,10068,10069,
  78231. 10070,10071,10072,10073,10074,10075,10076,10077,10078,10079,10080,10081,
  78232. 10082,10083,10084,10085,10086,10087,10088,10089,10090,10091,10092,10093,
  78233. 10094,10095,10096,10097,10098,10099,10100,10101,10102,10103,10104,10105,
  78234. 10106,10107,10108,10109,10110,10111,10112,10113,10114,10115,10116,10117,
  78235. 10118,10119,10120,10121,10122,10123,10124,10125,10126,10127,10128,10129,
  78236. 10130,10131,10132,10133,10134,10135,10136,10137,10138,10139,10140,10141,
  78237. 10142,10143,10144,10145,10146,10147,10148,10149,10150,10151,10152,10153,
  78238. 10154,10155,10156,10157,10158,10159,10160,10161,10162,10163,10164,10165,
  78239. 10166,10167,10168,10169,10170,10171,10172,10173,10174,10175,10176,10177,
  78240. 10178,10179,10180,10181,10182,10183,10184,10185,10186,10187,10188,10189,
  78241. 10190,10191,10192,10193,10194,10195,10196,10197,10198,10199,10200,10201,
  78242. 10202,10203,10204,10205,10206,10207,10208,10209,10210,10211,10212,10213,
  78243. 10214,10215,10216,10217,10218,10219,10220,10221,10222,10223,10224,10225,
  78244. 10226,10227,10228,10229,10230,10231,10232,10233,10234,10235,10236,10237,
  78245. 10238,10239,10240,10241,10242,10243,10244,10245,10246,10247,10248,10249,
  78246. 10250,10251,10252,10253,10254,10255,10256,10257,10258,10259,10260,10261,
  78247. 10262,10263,10264,10265,10266,10267,10268,10269,10270,10271,10272,10273,
  78248. 10274,10275,10276,10277,10278,10279,10280,10281,10282,10283,10284,10285,
  78249. 10286,10287,10288,10289,10290,10291,10292,10293,10294,10295,10296,10297,
  78250. 10298,10299,10300,10301,10302,10303,10304,10305,10306,10307,10308,10309,
  78251. 10310,10311,10312,10313,10314,10315,10316,10317,10318,10319,10320,10321,
  78252. 10322,10323,10324,10325,10326,10327,10328,10329,10330,10331,10332,10333,
  78253. 10334,10335,10336,10337,10338,10339,10340,10341,10342,10343,10344,10345,
  78254. 10346,10347,10348,10349,10350,10351,10352,10353,10354,10355,10356,10357,
  78255. 10358,10359,10360,10361,10362,10363,10364,10365,10366,10367,10368,10369,
  78256. 10370,10371,10372,10373,10374,10375,10376,10377,10378,10379,10380,10381,
  78257. 10382,10383,10384,10385,10386,10387,10388,10389,10390,10391,10392,10393,
  78258. 10394,10395,10396,10397,10398,10399,10400,10401,10402,10403,10404,10405,
  78259. 10406,10407,10408,10409,10410,10411,10412,10413,10414,10415,10416,10417,
  78260. 10418,10419,10420,10421,10422,10423,10424,10425,10426,10427,10428,10429,
  78261. 10430,10431,10432,10433,10434,10435,10436,10437,10438,10439,10440,10441,
  78262. 10442,10443,10444,10445,10446,10447,10448,10449,10450,10451,10452,10453,
  78263. 10454,10455,10456,10457,10458,10459,10460,10461,10462,10463,10464,10465,
  78264. 10466,10467,10468,10469,10470,10471,10472,10473,10474,10475,10476,10477,
  78265. 10478,10479,10480,10481,10482,10483,10484,10485,10486,10487,10488,10489,
  78266. 10490,10491,10492,10493,10494,10495,10496,10497,10498,10499,10500,10501,
  78267. 10502,10503,10504,10505,10506,10507,10508,10509,10510,10511,10512,10513,
  78268. 10514,10515,10516,10517,10518,10519,10520,10521,10522,10523,10524,10525,
  78269. 10526,10527,10528,10529,10530,10531,10532,10533,10534,10535,10536,10537,
  78270. 10538,10539,10540,10541,10542,10543,10544,10545,10546,10547,10548,10549,
  78271. 10550,10551,10552,10553,10554,10555,10556,10557,10558,10559,10560,10561,
  78272. 10562,10563,10564,10565,10566,10567,10568,10569,10570,10571,10572,10573,
  78273. 10574,10575,10576,10577,10578,10579,10580,10581,10582,10583,10584,10585,
  78274. 10586,10587,10588,10589,10590,10591,10592,10593,10594,10595,10596,10597,
  78275. 10598,10599,10600,10601,10602,10603,10604,10605,10606,10607,10608,10609,
  78276. 10610,10611,10612,10613,10614,10615,10616,10617,10618,10619,10620,10621,
  78277. 10622,10623,10624,10625,10626,10627,10628,10629,10630,10631,10632,10633,
  78278. 10634,10635,10636,10637,10638,10639,10640,10641,10642,10643,10644,10645,
  78279. 10646,10647,10648,10649,10650,10651,10652,10653,10654,10655,10656,10657,
  78280. 10658,10659,10660,10661,10662,10663,10664,10665,10666,10667,10668,10669,
  78281. 10670,10671,10672,10673,10674,10675,10676,10677,10678,10679,10680,10681,
  78282. 10682,10683,10684,10685,10686,10687,10688,10689,10690,10691,10692,10693,
  78283. 10694,10695,10696,10697,10698,10699,10700,10701,10702,10703,10704,10705,
  78284. 10706,10707,10708,10709,10710,10711,10712,10713,10714,10715,10716,10717,
  78285. 10718,10719,10720,10721,10722,10723,10724,10725,10726,10727,10728,10729,
  78286. 10730,10731,10732,10733,10734,10735,10736,10737,10738,10739,10740,10741,
  78287. 10742,10743,10744,10745,10746,10747,10748,10749,10750,10751,10752,10753,
  78288. 10754,10755,10756,10757,10758,10759,10760,10761,10762,10763,10764,10765,
  78289. 10766,10767,10768,10769,10770,10771,10772,10773,10774,10775,10776,10777,
  78290. 10778,10779,10780,10781,10782,10783,10784,10785,10786,10787,10788,10789,
  78291. 10790,10791,10792,10793,10794,10795,10796,10797,10798,10799,10800,10801,
  78292. 10802,10803,10804,10805,10806,10807,10808,10809,10810,10811,10812,10813,
  78293. 10814,10815,10816,10817,10818,10819,10820,10821,10822,10823,10824,10825,
  78294. 10826,10827,10828,10829,10830,10831,10832,10833,10834,10835,10836,10837,
  78295. 10838,10839,10840,10841,10842,10843,10844,10845,10846,10847,10848,10849,
  78296. 10850,10851,10852,10853,10854,10855,10856,10857,10858,10859,10860,10861,
  78297. 10862,10863,10864,10865,10866,10867,10868,10869,10870,10871,10872,10873,
  78298. 10874,10875,10876,10877,10878,10879,10880,10881,10882,10883,10884,10885,
  78299. 10886,10887,10888,10889,10890,10891,10892,10893,10894,10895,10896,10897,
  78300. 10898,10899,10900,10901,10902,10903,10904,10905,10906,10907,10908,10909,
  78301. 10910,10911,10912,10913,10914,10915,10916,10917,10918,10919,10920,10921,
  78302. 10922,10923,10924,10925,10926,10927,10928,10929,10930,10931,10932,10933,
  78303. 10934,10935,10936,10937,10938,10939,10940,10941,10942,10943,10944,10945,
  78304. 10946,10947,10948,10949,10950,10951,10952,10953,10954,10955,10956,10957,
  78305. 10958,10959,10960,10961,10962,10963,10964,10965,10966,10967,10968,10969,
  78306. 10970,10971,10972,10973,10974,10975,10976,10977,10978,10979,10980,10981,
  78307. 10982,10983,10984,10985,10986,10987,10988,10989,10990,10991,10992,10993,
  78308. 10994,10995,10996,10997,10998,10999,11000,11001,11002,11003,11004,11005,
  78309. 11006,11007,11008,11009,11010,11011,11012,11013,11014,11015,11016,11017,
  78310. 11018,11019,11020,11021,11022,11023,11024,11025,11026,11027,11028,11029,
  78311. 11030,11031,11032,11033,11034,11035,11036,11037,11038,11039,11040,11041,
  78312. 11042,11043,11044,11045,11046,11047,11048,11049,11050,11051,11052,11053,
  78313. 11054,11055,11056,11057,11058,11059,11060,11061,11062,11063,11064,11065,
  78314. 11066,11067,11068,11069,11070,11071,11072,11073,11074,11075,11076,11077,
  78315. 11078,11079,11080,11081,11082,11083,11084,11085,11086,11087,11088,11089,
  78316. 11090,11091,11092,11093,11094,11095,11096,11097,11098,11099,11100,11101,
  78317. 11102,11103,11104,11105,11106,11107,11108,11109,11110,11111,11112,11113,
  78318. 11114,11115,11116,11117,11118,11119,11120,11121,11122,11123,11124,11125,
  78319. 11126,11127,11128,11129,11130,11131,11132,11133,11134,11135,11136,11137,
  78320. 11138,11139,11140,11141,11142,11143,11144,11145,11146,11147,11148,11149,
  78321. 11150,11151,11152,11153,11154,11155,11156,11157,11158,11159,11160,11161,
  78322. 11162,11163,11164,11165,11166,11167,11168,11169,11170,11171,11172,11173,
  78323. 11174,11175,11176,11177,11178,11179,11180,11181,11182,11183,11184,11185,
  78324. 11186,11187,11188,11189,11190,11191,11192,11193,11194,11195,11196,11197,
  78325. 11198,11199,11200,11201,11202,11203,11204,11205,11206,11207,11208,11209,
  78326. 11210,11211,11212,11213,11214,11215,11216,11217,11218,11219,11220,11221,
  78327. 11222,11223,11224,11225,11226,11227,11228,11229,11230,11231,11232,11233,
  78328. 11234,11235,11236,11237,11238,11239,11240,11241,11242,11243,11244,11245,
  78329. 11246,11247,11248,11249,11250,11251,11252,11253,11254,11255,11256,11257,
  78330. 11258,11259,11260,11261,11262,11263,11264,11265,11266,11267,11268,11269,
  78331. 11270,11271,11272,11273,11274,11275,11276,11277,11278,11279,11280,11281,
  78332. 11282,11283,11284,11285,11286,11287,11288,11289,11290,11291,11292,11293,
  78333. 11294,11295,11296,11297,11298,11299,11300,11301,11302,11303,11304,11305,
  78334. 11306,11307,11308,11309,11310,11311,11264,11265,11266,11267,11268,11269,
  78335. 11270,11271,11272,11273,11274,11275,11276,11277,11278,11279,11280,11281,
  78336. 11282,11283,11284,11285,11286,11287,11288,11289,11290,11291,11292,11293,
  78337. 11294,11295,11296,11297,11298,11299,11300,11301,11302,11303,11304,11305,
  78338. 11306,11307,11308,11309,11310,11359,11360,11360,11362,11363,11364,570,574,
  78339. 11367,11367,11369,11369,11371,11371,11373,11374,11375,11376,11377,11378,
  78340. 11378,11380,11381,11381,11383,11384,11385,11386,11387,11388,11389,11390,
  78341. 11391,11392,11392,11394,11394,11396,11396,11398,11398,11400,11400,11402,
  78342. 11402,11404,11404,11406,11406,11408,11408,11410,11410,11412,11412,11414,
  78343. 11414,11416,11416,11418,11418,11420,11420,11422,11422,11424,11424,11426,
  78344. 11426,11428,11428,11430,11430,11432,11432,11434,11434,11436,11436,11438,
  78345. 11438,11440,11440,11442,11442,11444,11444,11446,11446,11448,11448,11450,
  78346. 11450,11452,11452,11454,11454,11456,11456,11458,11458,11460,11460,11462,
  78347. 11462,11464,11464,11466,11466,11468,11468,11470,11470,11472,11472,11474,
  78348. 11474,11476,11476,11478,11478,11480,11480,11482,11482,11484,11484,11486,
  78349. 11486,11488,11488,11490,11490,11492,11493,11494,11495,11496,11497,11498,
  78350. 11499,11499,11501,11501,11503,11504,11505,11506,11506,11508,11509,11510,
  78351. 11511,11512,11513,11514,11515,11516,11517,11518,11519,4256,4257,4258,4259,
  78352. 4260,4261,4262,4263,4264,4265,4266,4267,4268,4269,4270,4271,4272,4273,4274,
  78353. 4275,4276,4277,4278,4279,4280,4281,4282,4283,4284,4285,4286,4287,4288,4289,
  78354. 4290,4291,4292,4293,11558,4295,11560,11561,11562,11563,11564,4301,11566,
  78355. 11567,11568,11569,11570,11571,11572,11573,11574,11575,11576,11577,11578,
  78356. 11579,11580,11581,11582,11583,11584,11585,11586,11587,11588,11589,11590,
  78357. 11591,11592,11593,11594,11595,11596,11597,11598,11599,11600,11601,11602,
  78358. 11603,11604,11605,11606,11607,11608,11609,11610,11611,11612,11613,11614,
  78359. 11615,11616,11617,11618,11619,11620,11621,11622,11623,11624,11625,11626,
  78360. 11627,11628,11629,11630,11631,11632,11633,11634,11635,11636,11637,11638,
  78361. 11639,11640,11641,11642,11643,11644,11645,11646,11647,11648,11649,11650,
  78362. 11651,11652,11653,11654,11655,11656,11657,11658,11659,11660,11661,11662,
  78363. 11663,11664,11665,11666,11667,11668,11669,11670,11671,11672,11673,11674,
  78364. 11675,11676,11677,11678,11679,11680,11681,11682,11683,11684,11685,11686,
  78365. 11687,11688,11689,11690,11691,11692,11693,11694,11695,11696,11697,11698,
  78366. 11699,11700,11701,11702,11703,11704,11705,11706,11707,11708,11709,11710,
  78367. 11711,11712,11713,11714,11715,11716,11717,11718,11719,11720,11721,11722,
  78368. 11723,11724,11725,11726,11727,11728,11729,11730,11731,11732,11733,11734,
  78369. 11735,11736,11737,11738,11739,11740,11741,11742,11743,11744,11745,11746,
  78370. 11747,11748,11749,11750,11751,11752,11753,11754,11755,11756,11757,11758,
  78371. 11759,11760,11761,11762,11763,11764,11765,11766,11767,11768,11769,11770,
  78372. 11771,11772,11773,11774,11775,11776,11777,11778,11779,11780,11781,11782,
  78373. 11783,11784,11785,11786,11787,11788,11789,11790,11791,11792,11793,11794,
  78374. 11795,11796,11797,11798,11799,11800,11801,11802,11803,11804,11805,11806,
  78375. 11807,11808,11809,11810,11811,11812,11813,11814,11815,11816,11817,11818,
  78376. 11819,11820,11821,11822,11823,11824,11825,11826,11827,11828,11829,11830,
  78377. 11831,11832,11833,11834,11835,11836,11837,11838,11839,11840,11841,11842,
  78378. 11843,11844,11845,11846,11847,11848,11849,11850,11851,11852,11853,11854,
  78379. 11855,11856,11857,11858,11859,11860,11861,11862,11863,11864,11865,11866,
  78380. 11867,11868,11869,11870,11871,11872,11873,11874,11875,11876,11877,11878,
  78381. 11879,11880,11881,11882,11883,11884,11885,11886,11887,11888,11889,11890,
  78382. 11891,11892,11893,11894,11895,11896,11897,11898,11899,11900,11901,11902,
  78383. 11903,11904,11905,11906,11907,11908,11909,11910,11911,11912,11913,11914,
  78384. 11915,11916,11917,11918,11919,11920,11921,11922,11923,11924,11925,11926,
  78385. 11927,11928,11929,11930,11931,11932,11933,11934,11935,11936,11937,11938,
  78386. 11939,11940,11941,11942,11943,11944,11945,11946,11947,11948,11949,11950,
  78387. 11951,11952,11953,11954,11955,11956,11957,11958,11959,11960,11961,11962,
  78388. 11963,11964,11965,11966,11967,11968,11969,11970,11971,11972,11973,11974,
  78389. 11975,11976,11977,11978,11979,11980,11981,11982,11983,11984,11985,11986,
  78390. 11987,11988,11989,11990,11991,11992,11993,11994,11995,11996,11997,11998,
  78391. 11999,12000,12001,12002,12003,12004,12005,12006,12007,12008,12009,12010,
  78392. 12011,12012,12013,12014,12015,12016,12017,12018,12019,12020,12021,12022,
  78393. 12023,12024,12025,12026,12027,12028,12029,12030,12031,12032,12033,12034,
  78394. 12035,12036,12037,12038,12039,12040,12041,12042,12043,12044,12045,12046,
  78395. 12047,12048,12049,12050,12051,12052,12053,12054,12055,12056,12057,12058,
  78396. 12059,12060,12061,12062,12063,12064,12065,12066,12067,12068,12069,12070,
  78397. 12071,12072,12073,12074,12075,12076,12077,12078,12079,12080,12081,12082,
  78398. 12083,12084,12085,12086,12087,12088,12089,12090,12091,12092,12093,12094,
  78399. 12095,12096,12097,12098,12099,12100,12101,12102,12103,12104,12105,12106,
  78400. 12107,12108,12109,12110,12111,12112,12113,12114,12115,12116,12117,12118,
  78401. 12119,12120,12121,12122,12123,12124,12125,12126,12127,12128,12129,12130,
  78402. 12131,12132,12133,12134,12135,12136,12137,12138,12139,12140,12141,12142,
  78403. 12143,12144,12145,12146,12147,12148,12149,12150,12151,12152,12153,12154,
  78404. 12155,12156,12157,12158,12159,12160,12161,12162,12163,12164,12165,12166,
  78405. 12167,12168,12169,12170,12171,12172,12173,12174,12175,12176,12177,12178,
  78406. 12179,12180,12181,12182,12183,12184,12185,12186,12187,12188,12189,12190,
  78407. 12191,12192,12193,12194,12195,12196,12197,12198,12199,12200,12201,12202,
  78408. 12203,12204,12205,12206,12207,12208,12209,12210,12211,12212,12213,12214,
  78409. 12215,12216,12217,12218,12219,12220,12221,12222,12223,12224,12225,12226,
  78410. 12227,12228,12229,12230,12231,12232,12233,12234,12235,12236,12237,12238,
  78411. 12239,12240,12241,12242,12243,12244,12245,12246,12247,12248,12249,12250,
  78412. 12251,12252,12253,12254,12255,12256,12257,12258,12259,12260,12261,12262,
  78413. 12263,12264,12265,12266,12267,12268,12269,12270,12271,12272,12273,12274,
  78414. 12275,12276,12277,12278,12279,12280,12281,12282,12283,12284,12285,12286,
  78415. 12287,12288,12289,12290,12291,12292,12293,12294,12295,12296,12297,12298,
  78416. 12299,12300,12301,12302,12303,12304,12305,12306,12307,12308,12309,12310,
  78417. 12311,12312,12313,12314,12315,12316,12317,12318,12319,12320,12321,12322,
  78418. 12323,12324,12325,12326,12327,12328,12329,12330,12331,12332,12333,12334,
  78419. 12335,12336,12337,12338,12339,12340,12341,12342,12343,12344,12345,12346,
  78420. 12347,12348,12349,12350,12351,12352,12353,12354,12355,12356,12357,12358,
  78421. 12359,12360,12361,12362,12363,12364,12365,12366,12367,12368,12369,12370,
  78422. 12371,12372,12373,12374,12375,12376,12377,12378,12379,12380,12381,12382,
  78423. 12383,12384,12385,12386,12387,12388,12389,12390,12391,12392,12393,12394,
  78424. 12395,12396,12397,12398,12399,12400,12401,12402,12403,12404,12405,12406,
  78425. 12407,12408,12409,12410,12411,12412,12413,12414,12415,12416,12417,12418,
  78426. 12419,12420,12421,12422,12423,12424,12425,12426,12427,12428,12429,12430,
  78427. 12431,12432,12433,12434,12435,12436,12437,12438,12439,12440,12441,12442,
  78428. 12443,12444,12445,12446,12447,12448,12449,12450,12451,12452,12453,12454,
  78429. 12455,12456,12457,12458,12459,12460,12461,12462,12463,12464,12465,12466,
  78430. 12467,12468,12469,12470,12471,12472,12473,12474,12475,12476,12477,12478,
  78431. 12479,12480,12481,12482,12483,12484,12485,12486,12487,12488,12489,12490,
  78432. 12491,12492,12493,12494,12495,12496,12497,12498,12499,12500,12501,12502,
  78433. 12503,12504,12505,12506,12507,12508,12509,12510,12511,12512,12513,12514,
  78434. 12515,12516,12517,12518,12519,12520,12521,12522,12523,12524,12525,12526,
  78435. 12527,12528,12529,12530,12531,12532,12533,12534,12535,12536,12537,12538,
  78436. 12539,12540,12541,12542,12543,12544,12545,12546,12547,12548,12549,12550,
  78437. 12551,12552,12553,12554,12555,12556,12557,12558,12559,12560,12561,12562,
  78438. 12563,12564,12565,12566,12567,12568,12569,12570,12571,12572,12573,12574,
  78439. 12575,12576,12577,12578,12579,12580,12581,12582,12583,12584,12585,12586,
  78440. 12587,12588,12589,12590,12591,12592,12593,12594,12595,12596,12597,12598,
  78441. 12599,12600,12601,12602,12603,12604,12605,12606,12607,12608,12609,12610,
  78442. 12611,12612,12613,12614,12615,12616,12617,12618,12619,12620,12621,12622,
  78443. 12623,12624,12625,12626,12627,12628,12629,12630,12631,12632,12633,12634,
  78444. 12635,12636,12637,12638,12639,12640,12641,12642,12643,12644,12645,12646,
  78445. 12647,12648,12649,12650,12651,12652,12653,12654,12655,12656,12657,12658,
  78446. 12659,12660,12661,12662,12663,12664,12665,12666,12667,12668,12669,12670,
  78447. 12671,12672,12673,12674,12675,12676,12677,12678,12679,12680,12681,12682,
  78448. 12683,12684,12685,12686,12687,12688,12689,12690,12691,12692,12693,12694,
  78449. 12695,12696,12697,12698,12699,12700,12701,12702,12703,12704,12705,12706,
  78450. 12707,12708,12709,12710,12711,12712,12713,12714,12715,12716,12717,12718,
  78451. 12719,12720,12721,12722,12723,12724,12725,12726,12727,12728,12729,12730,
  78452. 12731,12732,12733,12734,12735,12736,12737,12738,12739,12740,12741,12742,
  78453. 12743,12744,12745,12746,12747,12748,12749,12750,12751,12752,12753,12754,
  78454. 12755,12756,12757,12758,12759,12760,12761,12762,12763,12764,12765,12766,
  78455. 12767,12768,12769,12770,12771,12772,12773,12774,12775,12776,12777,12778,
  78456. 12779,12780,12781,12782,12783,12784,12785,12786,12787,12788,12789,12790,
  78457. 12791,12792,12793,12794,12795,12796,12797,12798,12799,12800,12801,12802,
  78458. 12803,12804,12805,12806,12807,12808,12809,12810,12811,12812,12813,12814,
  78459. 12815,12816,12817,12818,12819,12820,12821,12822,12823,12824,12825,12826,
  78460. 12827,12828,12829,12830,12831,12832,12833,12834,12835,12836,12837,12838,
  78461. 12839,12840,12841,12842,12843,12844,12845,12846,12847,12848,12849,12850,
  78462. 12851,12852,12853,12854,12855,12856,12857,12858,12859,12860,12861,12862,
  78463. 12863,12864,12865,12866,12867,12868,12869,12870,12871,12872,12873,12874,
  78464. 12875,12876,12877,12878,12879,12880,12881,12882,12883,12884,12885,12886,
  78465. 12887,12888,12889,12890,12891,12892,12893,12894,12895,12896,12897,12898,
  78466. 12899,12900,12901,12902,12903,12904,12905,12906,12907,12908,12909,12910,
  78467. 12911,12912,12913,12914,12915,12916,12917,12918,12919,12920,12921,12922,
  78468. 12923,12924,12925,12926,12927,12928,12929,12930,12931,12932,12933,12934,
  78469. 12935,12936,12937,12938,12939,12940,12941,12942,12943,12944,12945,12946,
  78470. 12947,12948,12949,12950,12951,12952,12953,12954,12955,12956,12957,12958,
  78471. 12959,12960,12961,12962,12963,12964,12965,12966,12967,12968,12969,12970,
  78472. 12971,12972,12973,12974,12975,12976,12977,12978,12979,12980,12981,12982,
  78473. 12983,12984,12985,12986,12987,12988,12989,12990,12991,12992,12993,12994,
  78474. 12995,12996,12997,12998,12999,13000,13001,13002,13003,13004,13005,13006,
  78475. 13007,13008,13009,13010,13011,13012,13013,13014,13015,13016,13017,13018,
  78476. 13019,13020,13021,13022,13023,13024,13025,13026,13027,13028,13029,13030,
  78477. 13031,13032,13033,13034,13035,13036,13037,13038,13039,13040,13041,13042,
  78478. 13043,13044,13045,13046,13047,13048,13049,13050,13051,13052,13053,13054,
  78479. 13055,13056,13057,13058,13059,13060,13061,13062,13063,13064,13065,13066,
  78480. 13067,13068,13069,13070,13071,13072,13073,13074,13075,13076,13077,13078,
  78481. 13079,13080,13081,13082,13083,13084,13085,13086,13087,13088,13089,13090,
  78482. 13091,13092,13093,13094,13095,13096,13097,13098,13099,13100,13101,13102,
  78483. 13103,13104,13105,13106,13107,13108,13109,13110,13111,13112,13113,13114,
  78484. 13115,13116,13117,13118,13119,13120,13121,13122,13123,13124,13125,13126,
  78485. 13127,13128,13129,13130,13131,13132,13133,13134,13135,13136,13137,13138,
  78486. 13139,13140,13141,13142,13143,13144,13145,13146,13147,13148,13149,13150,
  78487. 13151,13152,13153,13154,13155,13156,13157,13158,13159,13160,13161,13162,
  78488. 13163,13164,13165,13166,13167,13168,13169,13170,13171,13172,13173,13174,
  78489. 13175,13176,13177,13178,13179,13180,13181,13182,13183,13184,13185,13186,
  78490. 13187,13188,13189,13190,13191,13192,13193,13194,13195,13196,13197,13198,
  78491. 13199,13200,13201,13202,13203,13204,13205,13206,13207,13208,13209,13210,
  78492. 13211,13212,13213,13214,13215,13216,13217,13218,13219,13220,13221,13222,
  78493. 13223,13224,13225,13226,13227,13228,13229,13230,13231,13232,13233,13234,
  78494. 13235,13236,13237,13238,13239,13240,13241,13242,13243,13244,13245,13246,
  78495. 13247,13248,13249,13250,13251,13252,13253,13254,13255,13256,13257,13258,
  78496. 13259,13260,13261,13262,13263,13264,13265,13266,13267,13268,13269,13270,
  78497. 13271,13272,13273,13274,13275,13276,13277,13278,13279,13280,13281,13282,
  78498. 13283,13284,13285,13286,13287,13288,13289,13290,13291,13292,13293,13294,
  78499. 13295,13296,13297,13298,13299,13300,13301,13302,13303,13304,13305,13306,
  78500. 13307,13308,13309,13310,13311,13312,13313,13314,13315,13316,13317,13318,
  78501. 13319,13320,13321,13322,13323,13324,13325,13326,13327,13328,13329,13330,
  78502. 13331,13332,13333,13334,13335,13336,13337,13338,13339,13340,13341,13342,
  78503. 13343,13344,13345,13346,13347,13348,13349,13350,13351,13352,13353,13354,
  78504. 13355,13356,13357,13358,13359,13360,13361,13362,13363,13364,13365,13366,
  78505. 13367,13368,13369,13370,13371,13372,13373,13374,13375,13376,13377,13378,
  78506. 13379,13380,13381,13382,13383,13384,13385,13386,13387,13388,13389,13390,
  78507. 13391,13392,13393,13394,13395,13396,13397,13398,13399,13400,13401,13402,
  78508. 13403,13404,13405,13406,13407,13408,13409,13410,13411,13412,13413,13414,
  78509. 13415,13416,13417,13418,13419,13420,13421,13422,13423,13424,13425,13426,
  78510. 13427,13428,13429,13430,13431,13432,13433,13434,13435,13436,13437,13438,
  78511. 13439,13440,13441,13442,13443,13444,13445,13446,13447,13448,13449,13450,
  78512. 13451,13452,13453,13454,13455,13456,13457,13458,13459,13460,13461,13462,
  78513. 13463,13464,13465,13466,13467,13468,13469,13470,13471,13472,13473,13474,
  78514. 13475,13476,13477,13478,13479,13480,13481,13482,13483,13484,13485,13486,
  78515. 13487,13488,13489,13490,13491,13492,13493,13494,13495,13496,13497,13498,
  78516. 13499,13500,13501,13502,13503,13504,13505,13506,13507,13508,13509,13510,
  78517. 13511,13512,13513,13514,13515,13516,13517,13518,13519,13520,13521,13522,
  78518. 13523,13524,13525,13526,13527,13528,13529,13530,13531,13532,13533,13534,
  78519. 13535,13536,13537,13538,13539,13540,13541,13542,13543,13544,13545,13546,
  78520. 13547,13548,13549,13550,13551,13552,13553,13554,13555,13556,13557,13558,
  78521. 13559,13560,13561,13562,13563,13564,13565,13566,13567,13568,13569,13570,
  78522. 13571,13572,13573,13574,13575,13576,13577,13578,13579,13580,13581,13582,
  78523. 13583,13584,13585,13586,13587,13588,13589,13590,13591,13592,13593,13594,
  78524. 13595,13596,13597,13598,13599,13600,13601,13602,13603,13604,13605,13606,
  78525. 13607,13608,13609,13610,13611,13612,13613,13614,13615,13616,13617,13618,
  78526. 13619,13620,13621,13622,13623,13624,13625,13626,13627,13628,13629,13630,
  78527. 13631,13632,13633,13634,13635,13636,13637,13638,13639,13640,13641,13642,
  78528. 13643,13644,13645,13646,13647,13648,13649,13650,13651,13652,13653,13654,
  78529. 13655,13656,13657,13658,13659,13660,13661,13662,13663,13664,13665,13666,
  78530. 13667,13668,13669,13670,13671,13672,13673,13674,13675,13676,13677,13678,
  78531. 13679,13680,13681,13682,13683,13684,13685,13686,13687,13688,13689,13690,
  78532. 13691,13692,13693,13694,13695,13696,13697,13698,13699,13700,13701,13702,
  78533. 13703,13704,13705,13706,13707,13708,13709,13710,13711,13712,13713,13714,
  78534. 13715,13716,13717,13718,13719,13720,13721,13722,13723,13724,13725,13726,
  78535. 13727,13728,13729,13730,13731,13732,13733,13734,13735,13736,13737,13738,
  78536. 13739,13740,13741,13742,13743,13744,13745,13746,13747,13748,13749,13750,
  78537. 13751,13752,13753,13754,13755,13756,13757,13758,13759,13760,13761,13762,
  78538. 13763,13764,13765,13766,13767,13768,13769,13770,13771,13772,13773,13774,
  78539. 13775,13776,13777,13778,13779,13780,13781,13782,13783,13784,13785,13786,
  78540. 13787,13788,13789,13790,13791,13792,13793,13794,13795,13796,13797,13798,
  78541. 13799,13800,13801,13802,13803,13804,13805,13806,13807,13808,13809,13810,
  78542. 13811,13812,13813,13814,13815,13816,13817,13818,13819,13820,13821,13822,
  78543. 13823,13824,13825,13826,13827,13828,13829,13830,13831,13832,13833,13834,
  78544. 13835,13836,13837,13838,13839,13840,13841,13842,13843,13844,13845,13846,
  78545. 13847,13848,13849,13850,13851,13852,13853,13854,13855,13856,13857,13858,
  78546. 13859,13860,13861,13862,13863,13864,13865,13866,13867,13868,13869,13870,
  78547. 13871,13872,13873,13874,13875,13876,13877,13878,13879,13880,13881,13882,
  78548. 13883,13884,13885,13886,13887,13888,13889,13890,13891,13892,13893,13894,
  78549. 13895,13896,13897,13898,13899,13900,13901,13902,13903,13904,13905,13906,
  78550. 13907,13908,13909,13910,13911,13912,13913,13914,13915,13916,13917,13918,
  78551. 13919,13920,13921,13922,13923,13924,13925,13926,13927,13928,13929,13930,
  78552. 13931,13932,13933,13934,13935,13936,13937,13938,13939,13940,13941,13942,
  78553. 13943,13944,13945,13946,13947,13948,13949,13950,13951,13952,13953,13954,
  78554. 13955,13956,13957,13958,13959,13960,13961,13962,13963,13964,13965,13966,
  78555. 13967,13968,13969,13970,13971,13972,13973,13974,13975,13976,13977,13978,
  78556. 13979,13980,13981,13982,13983,13984,13985,13986,13987,13988,13989,13990,
  78557. 13991,13992,13993,13994,13995,13996,13997,13998,13999,14000,14001,14002,
  78558. 14003,14004,14005,14006,14007,14008,14009,14010,14011,14012,14013,14014,
  78559. 14015,14016,14017,14018,14019,14020,14021,14022,14023,14024,14025,14026,
  78560. 14027,14028,14029,14030,14031,14032,14033,14034,14035,14036,14037,14038,
  78561. 14039,14040,14041,14042,14043,14044,14045,14046,14047,14048,14049,14050,
  78562. 14051,14052,14053,14054,14055,14056,14057,14058,14059,14060,14061,14062,
  78563. 14063,14064,14065,14066,14067,14068,14069,14070,14071,14072,14073,14074,
  78564. 14075,14076,14077,14078,14079,14080,14081,14082,14083,14084,14085,14086,
  78565. 14087,14088,14089,14090,14091,14092,14093,14094,14095,14096,14097,14098,
  78566. 14099,14100,14101,14102,14103,14104,14105,14106,14107,14108,14109,14110,
  78567. 14111,14112,14113,14114,14115,14116,14117,14118,14119,14120,14121,14122,
  78568. 14123,14124,14125,14126,14127,14128,14129,14130,14131,14132,14133,14134,
  78569. 14135,14136,14137,14138,14139,14140,14141,14142,14143,14144,14145,14146,
  78570. 14147,14148,14149,14150,14151,14152,14153,14154,14155,14156,14157,14158,
  78571. 14159,14160,14161,14162,14163,14164,14165,14166,14167,14168,14169,14170,
  78572. 14171,14172,14173,14174,14175,14176,14177,14178,14179,14180,14181,14182,
  78573. 14183,14184,14185,14186,14187,14188,14189,14190,14191,14192,14193,14194,
  78574. 14195,14196,14197,14198,14199,14200,14201,14202,14203,14204,14205,14206,
  78575. 14207,14208,14209,14210,14211,14212,14213,14214,14215,14216,14217,14218,
  78576. 14219,14220,14221,14222,14223,14224,14225,14226,14227,14228,14229,14230,
  78577. 14231,14232,14233,14234,14235,14236,14237,14238,14239,14240,14241,14242,
  78578. 14243,14244,14245,14246,14247,14248,14249,14250,14251,14252,14253,14254,
  78579. 14255,14256,14257,14258,14259,14260,14261,14262,14263,14264,14265,14266,
  78580. 14267,14268,14269,14270,14271,14272,14273,14274,14275,14276,14277,14278,
  78581. 14279,14280,14281,14282,14283,14284,14285,14286,14287,14288,14289,14290,
  78582. 14291,14292,14293,14294,14295,14296,14297,14298,14299,14300,14301,14302,
  78583. 14303,14304,14305,14306,14307,14308,14309,14310,14311,14312,14313,14314,
  78584. 14315,14316,14317,14318,14319,14320,14321,14322,14323,14324,14325,14326,
  78585. 14327,14328,14329,14330,14331,14332,14333,14334,14335,14336,14337,14338,
  78586. 14339,14340,14341,14342,14343,14344,14345,14346,14347,14348,14349,14350,
  78587. 14351,14352,14353,14354,14355,14356,14357,14358,14359,14360,14361,14362,
  78588. 14363,14364,14365,14366,14367,14368,14369,14370,14371,14372,14373,14374,
  78589. 14375,14376,14377,14378,14379,14380,14381,14382,14383,14384,14385,14386,
  78590. 14387,14388,14389,14390,14391,14392,14393,14394,14395,14396,14397,14398,
  78591. 14399,14400,14401,14402,14403,14404,14405,14406,14407,14408,14409,14410,
  78592. 14411,14412,14413,14414,14415,14416,14417,14418,14419,14420,14421,14422,
  78593. 14423,14424,14425,14426,14427,14428,14429,14430,14431,14432,14433,14434,
  78594. 14435,14436,14437,14438,14439,14440,14441,14442,14443,14444,14445,14446,
  78595. 14447,14448,14449,14450,14451,14452,14453,14454,14455,14456,14457,14458,
  78596. 14459,14460,14461,14462,14463,14464,14465,14466,14467,14468,14469,14470,
  78597. 14471,14472,14473,14474,14475,14476,14477,14478,14479,14480,14481,14482,
  78598. 14483,14484,14485,14486,14487,14488,14489,14490,14491,14492,14493,14494,
  78599. 14495,14496,14497,14498,14499,14500,14501,14502,14503,14504,14505,14506,
  78600. 14507,14508,14509,14510,14511,14512,14513,14514,14515,14516,14517,14518,
  78601. 14519,14520,14521,14522,14523,14524,14525,14526,14527,14528,14529,14530,
  78602. 14531,14532,14533,14534,14535,14536,14537,14538,14539,14540,14541,14542,
  78603. 14543,14544,14545,14546,14547,14548,14549,14550,14551,14552,14553,14554,
  78604. 14555,14556,14557,14558,14559,14560,14561,14562,14563,14564,14565,14566,
  78605. 14567,14568,14569,14570,14571,14572,14573,14574,14575,14576,14577,14578,
  78606. 14579,14580,14581,14582,14583,14584,14585,14586,14587,14588,14589,14590,
  78607. 14591,14592,14593,14594,14595,14596,14597,14598,14599,14600,14601,14602,
  78608. 14603,14604,14605,14606,14607,14608,14609,14610,14611,14612,14613,14614,
  78609. 14615,14616,14617,14618,14619,14620,14621,14622,14623,14624,14625,14626,
  78610. 14627,14628,14629,14630,14631,14632,14633,14634,14635,14636,14637,14638,
  78611. 14639,14640,14641,14642,14643,14644,14645,14646,14647,14648,14649,14650,
  78612. 14651,14652,14653,14654,14655,14656,14657,14658,14659,14660,14661,14662,
  78613. 14663,14664,14665,14666,14667,14668,14669,14670,14671,14672,14673,14674,
  78614. 14675,14676,14677,14678,14679,14680,14681,14682,14683,14684,14685,14686,
  78615. 14687,14688,14689,14690,14691,14692,14693,14694,14695,14696,14697,14698,
  78616. 14699,14700,14701,14702,14703,14704,14705,14706,14707,14708,14709,14710,
  78617. 14711,14712,14713,14714,14715,14716,14717,14718,14719,14720,14721,14722,
  78618. 14723,14724,14725,14726,14727,14728,14729,14730,14731,14732,14733,14734,
  78619. 14735,14736,14737,14738,14739,14740,14741,14742,14743,14744,14745,14746,
  78620. 14747,14748,14749,14750,14751,14752,14753,14754,14755,14756,14757,14758,
  78621. 14759,14760,14761,14762,14763,14764,14765,14766,14767,14768,14769,14770,
  78622. 14771,14772,14773,14774,14775,14776,14777,14778,14779,14780,14781,14782,
  78623. 14783,14784,14785,14786,14787,14788,14789,14790,14791,14792,14793,14794,
  78624. 14795,14796,14797,14798,14799,14800,14801,14802,14803,14804,14805,14806,
  78625. 14807,14808,14809,14810,14811,14812,14813,14814,14815,14816,14817,14818,
  78626. 14819,14820,14821,14822,14823,14824,14825,14826,14827,14828,14829,14830,
  78627. 14831,14832,14833,14834,14835,14836,14837,14838,14839,14840,14841,14842,
  78628. 14843,14844,14845,14846,14847,14848,14849,14850,14851,14852,14853,14854,
  78629. 14855,14856,14857,14858,14859,14860,14861,14862,14863,14864,14865,14866,
  78630. 14867,14868,14869,14870,14871,14872,14873,14874,14875,14876,14877,14878,
  78631. 14879,14880,14881,14882,14883,14884,14885,14886,14887,14888,14889,14890,
  78632. 14891,14892,14893,14894,14895,14896,14897,14898,14899,14900,14901,14902,
  78633. 14903,14904,14905,14906,14907,14908,14909,14910,14911,14912,14913,14914,
  78634. 14915,14916,14917,14918,14919,14920,14921,14922,14923,14924,14925,14926,
  78635. 14927,14928,14929,14930,14931,14932,14933,14934,14935,14936,14937,14938,
  78636. 14939,14940,14941,14942,14943,14944,14945,14946,14947,14948,14949,14950,
  78637. 14951,14952,14953,14954,14955,14956,14957,14958,14959,14960,14961,14962,
  78638. 14963,14964,14965,14966,14967,14968,14969,14970,14971,14972,14973,14974,
  78639. 14975,14976,14977,14978,14979,14980,14981,14982,14983,14984,14985,14986,
  78640. 14987,14988,14989,14990,14991,14992,14993,14994,14995,14996,14997,14998,
  78641. 14999,15000,15001,15002,15003,15004,15005,15006,15007,15008,15009,15010,
  78642. 15011,15012,15013,15014,15015,15016,15017,15018,15019,15020,15021,15022,
  78643. 15023,15024,15025,15026,15027,15028,15029,15030,15031,15032,15033,15034,
  78644. 15035,15036,15037,15038,15039,15040,15041,15042,15043,15044,15045,15046,
  78645. 15047,15048,15049,15050,15051,15052,15053,15054,15055,15056,15057,15058,
  78646. 15059,15060,15061,15062,15063,15064,15065,15066,15067,15068,15069,15070,
  78647. 15071,15072,15073,15074,15075,15076,15077,15078,15079,15080,15081,15082,
  78648. 15083,15084,15085,15086,15087,15088,15089,15090,15091,15092,15093,15094,
  78649. 15095,15096,15097,15098,15099,15100,15101,15102,15103,15104,15105,15106,
  78650. 15107,15108,15109,15110,15111,15112,15113,15114,15115,15116,15117,15118,
  78651. 15119,15120,15121,15122,15123,15124,15125,15126,15127,15128,15129,15130,
  78652. 15131,15132,15133,15134,15135,15136,15137,15138,15139,15140,15141,15142,
  78653. 15143,15144,15145,15146,15147,15148,15149,15150,15151,15152,15153,15154,
  78654. 15155,15156,15157,15158,15159,15160,15161,15162,15163,15164,15165,15166,
  78655. 15167,15168,15169,15170,15171,15172,15173,15174,15175,15176,15177,15178,
  78656. 15179,15180,15181,15182,15183,15184,15185,15186,15187,15188,15189,15190,
  78657. 15191,15192,15193,15194,15195,15196,15197,15198,15199,15200,15201,15202,
  78658. 15203,15204,15205,15206,15207,15208,15209,15210,15211,15212,15213,15214,
  78659. 15215,15216,15217,15218,15219,15220,15221,15222,15223,15224,15225,15226,
  78660. 15227,15228,15229,15230,15231,15232,15233,15234,15235,15236,15237,15238,
  78661. 15239,15240,15241,15242,15243,15244,15245,15246,15247,15248,15249,15250,
  78662. 15251,15252,15253,15254,15255,15256,15257,15258,15259,15260,15261,15262,
  78663. 15263,15264,15265,15266,15267,15268,15269,15270,15271,15272,15273,15274,
  78664. 15275,15276,15277,15278,15279,15280,15281,15282,15283,15284,15285,15286,
  78665. 15287,15288,15289,15290,15291,15292,15293,15294,15295,15296,15297,15298,
  78666. 15299,15300,15301,15302,15303,15304,15305,15306,15307,15308,15309,15310,
  78667. 15311,15312,15313,15314,15315,15316,15317,15318,15319,15320,15321,15322,
  78668. 15323,15324,15325,15326,15327,15328,15329,15330,15331,15332,15333,15334,
  78669. 15335,15336,15337,15338,15339,15340,15341,15342,15343,15344,15345,15346,
  78670. 15347,15348,15349,15350,15351,15352,15353,15354,15355,15356,15357,15358,
  78671. 15359,15360,15361,15362,15363,15364,15365,15366,15367,15368,15369,15370,
  78672. 15371,15372,15373,15374,15375,15376,15377,15378,15379,15380,15381,15382,
  78673. 15383,15384,15385,15386,15387,15388,15389,15390,15391,15392,15393,15394,
  78674. 15395,15396,15397,15398,15399,15400,15401,15402,15403,15404,15405,15406,
  78675. 15407,15408,15409,15410,15411,15412,15413,15414,15415,15416,15417,15418,
  78676. 15419,15420,15421,15422,15423,15424,15425,15426,15427,15428,15429,15430,
  78677. 15431,15432,15433,15434,15435,15436,15437,15438,15439,15440,15441,15442,
  78678. 15443,15444,15445,15446,15447,15448,15449,15450,15451,15452,15453,15454,
  78679. 15455,15456,15457,15458,15459,15460,15461,15462,15463,15464,15465,15466,
  78680. 15467,15468,15469,15470,15471,15472,15473,15474,15475,15476,15477,15478,
  78681. 15479,15480,15481,15482,15483,15484,15485,15486,15487,15488,15489,15490,
  78682. 15491,15492,15493,15494,15495,15496,15497,15498,15499,15500,15501,15502,
  78683. 15503,15504,15505,15506,15507,15508,15509,15510,15511,15512,15513,15514,
  78684. 15515,15516,15517,15518,15519,15520,15521,15522,15523,15524,15525,15526,
  78685. 15527,15528,15529,15530,15531,15532,15533,15534,15535,15536,15537,15538,
  78686. 15539,15540,15541,15542,15543,15544,15545,15546,15547,15548,15549,15550,
  78687. 15551,15552,15553,15554,15555,15556,15557,15558,15559,15560,15561,15562,
  78688. 15563,15564,15565,15566,15567,15568,15569,15570,15571,15572,15573,15574,
  78689. 15575,15576,15577,15578,15579,15580,15581,15582,15583,15584,15585,15586,
  78690. 15587,15588,15589,15590,15591,15592,15593,15594,15595,15596,15597,15598,
  78691. 15599,15600,15601,15602,15603,15604,15605,15606,15607,15608,15609,15610,
  78692. 15611,15612,15613,15614,15615,15616,15617,15618,15619,15620,15621,15622,
  78693. 15623,15624,15625,15626,15627,15628,15629,15630,15631,15632,15633,15634,
  78694. 15635,15636,15637,15638,15639,15640,15641,15642,15643,15644,15645,15646,
  78695. 15647,15648,15649,15650,15651,15652,15653,15654,15655,15656,15657,15658,
  78696. 15659,15660,15661,15662,15663,15664,15665,15666,15667,15668,15669,15670,
  78697. 15671,15672,15673,15674,15675,15676,15677,15678,15679,15680,15681,15682,
  78698. 15683,15684,15685,15686,15687,15688,15689,15690,15691,15692,15693,15694,
  78699. 15695,15696,15697,15698,15699,15700,15701,15702,15703,15704,15705,15706,
  78700. 15707,15708,15709,15710,15711,15712,15713,15714,15715,15716,15717,15718,
  78701. 15719,15720,15721,15722,15723,15724,15725,15726,15727,15728,15729,15730,
  78702. 15731,15732,15733,15734,15735,15736,15737,15738,15739,15740,15741,15742,
  78703. 15743,15744,15745,15746,15747,15748,15749,15750,15751,15752,15753,15754,
  78704. 15755,15756,15757,15758,15759,15760,15761,15762,15763,15764,15765,15766,
  78705. 15767,15768,15769,15770,15771,15772,15773,15774,15775,15776,15777,15778,
  78706. 15779,15780,15781,15782,15783,15784,15785,15786,15787,15788,15789,15790,
  78707. 15791,15792,15793,15794,15795,15796,15797,15798,15799,15800,15801,15802,
  78708. 15803,15804,15805,15806,15807,15808,15809,15810,15811,15812,15813,15814,
  78709. 15815,15816,15817,15818,15819,15820,15821,15822,15823,15824,15825,15826,
  78710. 15827,15828,15829,15830,15831,15832,15833,15834,15835,15836,15837,15838,
  78711. 15839,15840,15841,15842,15843,15844,15845,15846,15847,15848,15849,15850,
  78712. 15851,15852,15853,15854,15855,15856,15857,15858,15859,15860,15861,15862,
  78713. 15863,15864,15865,15866,15867,15868,15869,15870,15871,15872,15873,15874,
  78714. 15875,15876,15877,15878,15879,15880,15881,15882,15883,15884,15885,15886,
  78715. 15887,15888,15889,15890,15891,15892,15893,15894,15895,15896,15897,15898,
  78716. 15899,15900,15901,15902,15903,15904,15905,15906,15907,15908,15909,15910,
  78717. 15911,15912,15913,15914,15915,15916,15917,15918,15919,15920,15921,15922,
  78718. 15923,15924,15925,15926,15927,15928,15929,15930,15931,15932,15933,15934,
  78719. 15935,15936,15937,15938,15939,15940,15941,15942,15943,15944,15945,15946,
  78720. 15947,15948,15949,15950,15951,15952,15953,15954,15955,15956,15957,15958,
  78721. 15959,15960,15961,15962,15963,15964,15965,15966,15967,15968,15969,15970,
  78722. 15971,15972,15973,15974,15975,15976,15977,15978,15979,15980,15981,15982,
  78723. 15983,15984,15985,15986,15987,15988,15989,15990,15991,15992,15993,15994,
  78724. 15995,15996,15997,15998,15999,16000,16001,16002,16003,16004,16005,16006,
  78725. 16007,16008,16009,16010,16011,16012,16013,16014,16015,16016,16017,16018,
  78726. 16019,16020,16021,16022,16023,16024,16025,16026,16027,16028,16029,16030,
  78727. 16031,16032,16033,16034,16035,16036,16037,16038,16039,16040,16041,16042,
  78728. 16043,16044,16045,16046,16047,16048,16049,16050,16051,16052,16053,16054,
  78729. 16055,16056,16057,16058,16059,16060,16061,16062,16063,16064,16065,16066,
  78730. 16067,16068,16069,16070,16071,16072,16073,16074,16075,16076,16077,16078,
  78731. 16079,16080,16081,16082,16083,16084,16085,16086,16087,16088,16089,16090,
  78732. 16091,16092,16093,16094,16095,16096,16097,16098,16099,16100,16101,16102,
  78733. 16103,16104,16105,16106,16107,16108,16109,16110,16111,16112,16113,16114,
  78734. 16115,16116,16117,16118,16119,16120,16121,16122,16123,16124,16125,16126,
  78735. 16127,16128,16129,16130,16131,16132,16133,16134,16135,16136,16137,16138,
  78736. 16139,16140,16141,16142,16143,16144,16145,16146,16147,16148,16149,16150,
  78737. 16151,16152,16153,16154,16155,16156,16157,16158,16159,16160,16161,16162,
  78738. 16163,16164,16165,16166,16167,16168,16169,16170,16171,16172,16173,16174,
  78739. 16175,16176,16177,16178,16179,16180,16181,16182,16183,16184,16185,16186,
  78740. 16187,16188,16189,16190,16191,16192,16193,16194,16195,16196,16197,16198,
  78741. 16199,16200,16201,16202,16203,16204,16205,16206,16207,16208,16209,16210,
  78742. 16211,16212,16213,16214,16215,16216,16217,16218,16219,16220,16221,16222,
  78743. 16223,16224,16225,16226,16227,16228,16229,16230,16231,16232,16233,16234,
  78744. 16235,16236,16237,16238,16239,16240,16241,16242,16243,16244,16245,16246,
  78745. 16247,16248,16249,16250,16251,16252,16253,16254,16255,16256,16257,16258,
  78746. 16259,16260,16261,16262,16263,16264,16265,16266,16267,16268,16269,16270,
  78747. 16271,16272,16273,16274,16275,16276,16277,16278,16279,16280,16281,16282,
  78748. 16283,16284,16285,16286,16287,16288,16289,16290,16291,16292,16293,16294,
  78749. 16295,16296,16297,16298,16299,16300,16301,16302,16303,16304,16305,16306,
  78750. 16307,16308,16309,16310,16311,16312,16313,16314,16315,16316,16317,16318,
  78751. 16319,16320,16321,16322,16323,16324,16325,16326,16327,16328,16329,16330,
  78752. 16331,16332,16333,16334,16335,16336,16337,16338,16339,16340,16341,16342,
  78753. 16343,16344,16345,16346,16347,16348,16349,16350,16351,16352,16353,16354,
  78754. 16355,16356,16357,16358,16359,16360,16361,16362,16363,16364,16365,16366,
  78755. 16367,16368,16369,16370,16371,16372,16373,16374,16375,16376,16377,16378,
  78756. 16379,16380,16381,16382,16383,16384,16385,16386,16387,16388,16389,16390,
  78757. 16391,16392,16393,16394,16395,16396,16397,16398,16399,16400,16401,16402,
  78758. 16403,16404,16405,16406,16407,16408,16409,16410,16411,16412,16413,16414,
  78759. 16415,16416,16417,16418,16419,16420,16421,16422,16423,16424,16425,16426,
  78760. 16427,16428,16429,16430,16431,16432,16433,16434,16435,16436,16437,16438,
  78761. 16439,16440,16441,16442,16443,16444,16445,16446,16447,16448,16449,16450,
  78762. 16451,16452,16453,16454,16455,16456,16457,16458,16459,16460,16461,16462,
  78763. 16463,16464,16465,16466,16467,16468,16469,16470,16471,16472,16473,16474,
  78764. 16475,16476,16477,16478,16479,16480,16481,16482,16483,16484,16485,16486,
  78765. 16487,16488,16489,16490,16491,16492,16493,16494,16495,16496,16497,16498,
  78766. 16499,16500,16501,16502,16503,16504,16505,16506,16507,16508,16509,16510,
  78767. 16511,16512,16513,16514,16515,16516,16517,16518,16519,16520,16521,16522,
  78768. 16523,16524,16525,16526,16527,16528,16529,16530,16531,16532,16533,16534,
  78769. 16535,16536,16537,16538,16539,16540,16541,16542,16543,16544,16545,16546,
  78770. 16547,16548,16549,16550,16551,16552,16553,16554,16555,16556,16557,16558,
  78771. 16559,16560,16561,16562,16563,16564,16565,16566,16567,16568,16569,16570,
  78772. 16571,16572,16573,16574,16575,16576,16577,16578,16579,16580,16581,16582,
  78773. 16583,16584,16585,16586,16587,16588,16589,16590,16591,16592,16593,16594,
  78774. 16595,16596,16597,16598,16599,16600,16601,16602,16603,16604,16605,16606,
  78775. 16607,16608,16609,16610,16611,16612,16613,16614,16615,16616,16617,16618,
  78776. 16619,16620,16621,16622,16623,16624,16625,16626,16627,16628,16629,16630,
  78777. 16631,16632,16633,16634,16635,16636,16637,16638,16639,16640,16641,16642,
  78778. 16643,16644,16645,16646,16647,16648,16649,16650,16651,16652,16653,16654,
  78779. 16655,16656,16657,16658,16659,16660,16661,16662,16663,16664,16665,16666,
  78780. 16667,16668,16669,16670,16671,16672,16673,16674,16675,16676,16677,16678,
  78781. 16679,16680,16681,16682,16683,16684,16685,16686,16687,16688,16689,16690,
  78782. 16691,16692,16693,16694,16695,16696,16697,16698,16699,16700,16701,16702,
  78783. 16703,16704,16705,16706,16707,16708,16709,16710,16711,16712,16713,16714,
  78784. 16715,16716,16717,16718,16719,16720,16721,16722,16723,16724,16725,16726,
  78785. 16727,16728,16729,16730,16731,16732,16733,16734,16735,16736,16737,16738,
  78786. 16739,16740,16741,16742,16743,16744,16745,16746,16747,16748,16749,16750,
  78787. 16751,16752,16753,16754,16755,16756,16757,16758,16759,16760,16761,16762,
  78788. 16763,16764,16765,16766,16767,16768,16769,16770,16771,16772,16773,16774,
  78789. 16775,16776,16777,16778,16779,16780,16781,16782,16783,16784,16785,16786,
  78790. 16787,16788,16789,16790,16791,16792,16793,16794,16795,16796,16797,16798,
  78791. 16799,16800,16801,16802,16803,16804,16805,16806,16807,16808,16809,16810,
  78792. 16811,16812,16813,16814,16815,16816,16817,16818,16819,16820,16821,16822,
  78793. 16823,16824,16825,16826,16827,16828,16829,16830,16831,16832,16833,16834,
  78794. 16835,16836,16837,16838,16839,16840,16841,16842,16843,16844,16845,16846,
  78795. 16847,16848,16849,16850,16851,16852,16853,16854,16855,16856,16857,16858,
  78796. 16859,16860,16861,16862,16863,16864,16865,16866,16867,16868,16869,16870,
  78797. 16871,16872,16873,16874,16875,16876,16877,16878,16879,16880,16881,16882,
  78798. 16883,16884,16885,16886,16887,16888,16889,16890,16891,16892,16893,16894,
  78799. 16895,16896,16897,16898,16899,16900,16901,16902,16903,16904,16905,16906,
  78800. 16907,16908,16909,16910,16911,16912,16913,16914,16915,16916,16917,16918,
  78801. 16919,16920,16921,16922,16923,16924,16925,16926,16927,16928,16929,16930,
  78802. 16931,16932,16933,16934,16935,16936,16937,16938,16939,16940,16941,16942,
  78803. 16943,16944,16945,16946,16947,16948,16949,16950,16951,16952,16953,16954,
  78804. 16955,16956,16957,16958,16959,16960,16961,16962,16963,16964,16965,16966,
  78805. 16967,16968,16969,16970,16971,16972,16973,16974,16975,16976,16977,16978,
  78806. 16979,16980,16981,16982,16983,16984,16985,16986,16987,16988,16989,16990,
  78807. 16991,16992,16993,16994,16995,16996,16997,16998,16999,17000,17001,17002,
  78808. 17003,17004,17005,17006,17007,17008,17009,17010,17011,17012,17013,17014,
  78809. 17015,17016,17017,17018,17019,17020,17021,17022,17023,17024,17025,17026,
  78810. 17027,17028,17029,17030,17031,17032,17033,17034,17035,17036,17037,17038,
  78811. 17039,17040,17041,17042,17043,17044,17045,17046,17047,17048,17049,17050,
  78812. 17051,17052,17053,17054,17055,17056,17057,17058,17059,17060,17061,17062,
  78813. 17063,17064,17065,17066,17067,17068,17069,17070,17071,17072,17073,17074,
  78814. 17075,17076,17077,17078,17079,17080,17081,17082,17083,17084,17085,17086,
  78815. 17087,17088,17089,17090,17091,17092,17093,17094,17095,17096,17097,17098,
  78816. 17099,17100,17101,17102,17103,17104,17105,17106,17107,17108,17109,17110,
  78817. 17111,17112,17113,17114,17115,17116,17117,17118,17119,17120,17121,17122,
  78818. 17123,17124,17125,17126,17127,17128,17129,17130,17131,17132,17133,17134,
  78819. 17135,17136,17137,17138,17139,17140,17141,17142,17143,17144,17145,17146,
  78820. 17147,17148,17149,17150,17151,17152,17153,17154,17155,17156,17157,17158,
  78821. 17159,17160,17161,17162,17163,17164,17165,17166,17167,17168,17169,17170,
  78822. 17171,17172,17173,17174,17175,17176,17177,17178,17179,17180,17181,17182,
  78823. 17183,17184,17185,17186,17187,17188,17189,17190,17191,17192,17193,17194,
  78824. 17195,17196,17197,17198,17199,17200,17201,17202,17203,17204,17205,17206,
  78825. 17207,17208,17209,17210,17211,17212,17213,17214,17215,17216,17217,17218,
  78826. 17219,17220,17221,17222,17223,17224,17225,17226,17227,17228,17229,17230,
  78827. 17231,17232,17233,17234,17235,17236,17237,17238,17239,17240,17241,17242,
  78828. 17243,17244,17245,17246,17247,17248,17249,17250,17251,17252,17253,17254,
  78829. 17255,17256,17257,17258,17259,17260,17261,17262,17263,17264,17265,17266,
  78830. 17267,17268,17269,17270,17271,17272,17273,17274,17275,17276,17277,17278,
  78831. 17279,17280,17281,17282,17283,17284,17285,17286,17287,17288,17289,17290,
  78832. 17291,17292,17293,17294,17295,17296,17297,17298,17299,17300,17301,17302,
  78833. 17303,17304,17305,17306,17307,17308,17309,17310,17311,17312,17313,17314,
  78834. 17315,17316,17317,17318,17319,17320,17321,17322,17323,17324,17325,17326,
  78835. 17327,17328,17329,17330,17331,17332,17333,17334,17335,17336,17337,17338,
  78836. 17339,17340,17341,17342,17343,17344,17345,17346,17347,17348,17349,17350,
  78837. 17351,17352,17353,17354,17355,17356,17357,17358,17359,17360,17361,17362,
  78838. 17363,17364,17365,17366,17367,17368,17369,17370,17371,17372,17373,17374,
  78839. 17375,17376,17377,17378,17379,17380,17381,17382,17383,17384,17385,17386,
  78840. 17387,17388,17389,17390,17391,17392,17393,17394,17395,17396,17397,17398,
  78841. 17399,17400,17401,17402,17403,17404,17405,17406,17407,17408,17409,17410,
  78842. 17411,17412,17413,17414,17415,17416,17417,17418,17419,17420,17421,17422,
  78843. 17423,17424,17425,17426,17427,17428,17429,17430,17431,17432,17433,17434,
  78844. 17435,17436,17437,17438,17439,17440,17441,17442,17443,17444,17445,17446,
  78845. 17447,17448,17449,17450,17451,17452,17453,17454,17455,17456,17457,17458,
  78846. 17459,17460,17461,17462,17463,17464,17465,17466,17467,17468,17469,17470,
  78847. 17471,17472,17473,17474,17475,17476,17477,17478,17479,17480,17481,17482,
  78848. 17483,17484,17485,17486,17487,17488,17489,17490,17491,17492,17493,17494,
  78849. 17495,17496,17497,17498,17499,17500,17501,17502,17503,17504,17505,17506,
  78850. 17507,17508,17509,17510,17511,17512,17513,17514,17515,17516,17517,17518,
  78851. 17519,17520,17521,17522,17523,17524,17525,17526,17527,17528,17529,17530,
  78852. 17531,17532,17533,17534,17535,17536,17537,17538,17539,17540,17541,17542,
  78853. 17543,17544,17545,17546,17547,17548,17549,17550,17551,17552,17553,17554,
  78854. 17555,17556,17557,17558,17559,17560,17561,17562,17563,17564,17565,17566,
  78855. 17567,17568,17569,17570,17571,17572,17573,17574,17575,17576,17577,17578,
  78856. 17579,17580,17581,17582,17583,17584,17585,17586,17587,17588,17589,17590,
  78857. 17591,17592,17593,17594,17595,17596,17597,17598,17599,17600,17601,17602,
  78858. 17603,17604,17605,17606,17607,17608,17609,17610,17611,17612,17613,17614,
  78859. 17615,17616,17617,17618,17619,17620,17621,17622,17623,17624,17625,17626,
  78860. 17627,17628,17629,17630,17631,17632,17633,17634,17635,17636,17637,17638,
  78861. 17639,17640,17641,17642,17643,17644,17645,17646,17647,17648,17649,17650,
  78862. 17651,17652,17653,17654,17655,17656,17657,17658,17659,17660,17661,17662,
  78863. 17663,17664,17665,17666,17667,17668,17669,17670,17671,17672,17673,17674,
  78864. 17675,17676,17677,17678,17679,17680,17681,17682,17683,17684,17685,17686,
  78865. 17687,17688,17689,17690,17691,17692,17693,17694,17695,17696,17697,17698,
  78866. 17699,17700,17701,17702,17703,17704,17705,17706,17707,17708,17709,17710,
  78867. 17711,17712,17713,17714,17715,17716,17717,17718,17719,17720,17721,17722,
  78868. 17723,17724,17725,17726,17727,17728,17729,17730,17731,17732,17733,17734,
  78869. 17735,17736,17737,17738,17739,17740,17741,17742,17743,17744,17745,17746,
  78870. 17747,17748,17749,17750,17751,17752,17753,17754,17755,17756,17757,17758,
  78871. 17759,17760,17761,17762,17763,17764,17765,17766,17767,17768,17769,17770,
  78872. 17771,17772,17773,17774,17775,17776,17777,17778,17779,17780,17781,17782,
  78873. 17783,17784,17785,17786,17787,17788,17789,17790,17791,17792,17793,17794,
  78874. 17795,17796,17797,17798,17799,17800,17801,17802,17803,17804,17805,17806,
  78875. 17807,17808,17809,17810,17811,17812,17813,17814,17815,17816,17817,17818,
  78876. 17819,17820,17821,17822,17823,17824,17825,17826,17827,17828,17829,17830,
  78877. 17831,17832,17833,17834,17835,17836,17837,17838,17839,17840,17841,17842,
  78878. 17843,17844,17845,17846,17847,17848,17849,17850,17851,17852,17853,17854,
  78879. 17855,17856,17857,17858,17859,17860,17861,17862,17863,17864,17865,17866,
  78880. 17867,17868,17869,17870,17871,17872,17873,17874,17875,17876,17877,17878,
  78881. 17879,17880,17881,17882,17883,17884,17885,17886,17887,17888,17889,17890,
  78882. 17891,17892,17893,17894,17895,17896,17897,17898,17899,17900,17901,17902,
  78883. 17903,17904,17905,17906,17907,17908,17909,17910,17911,17912,17913,17914,
  78884. 17915,17916,17917,17918,17919,17920,17921,17922,17923,17924,17925,17926,
  78885. 17927,17928,17929,17930,17931,17932,17933,17934,17935,17936,17937,17938,
  78886. 17939,17940,17941,17942,17943,17944,17945,17946,17947,17948,17949,17950,
  78887. 17951,17952,17953,17954,17955,17956,17957,17958,17959,17960,17961,17962,
  78888. 17963,17964,17965,17966,17967,17968,17969,17970,17971,17972,17973,17974,
  78889. 17975,17976,17977,17978,17979,17980,17981,17982,17983,17984,17985,17986,
  78890. 17987,17988,17989,17990,17991,17992,17993,17994,17995,17996,17997,17998,
  78891. 17999,18000,18001,18002,18003,18004,18005,18006,18007,18008,18009,18010,
  78892. 18011,18012,18013,18014,18015,18016,18017,18018,18019,18020,18021,18022,
  78893. 18023,18024,18025,18026,18027,18028,18029,18030,18031,18032,18033,18034,
  78894. 18035,18036,18037,18038,18039,18040,18041,18042,18043,18044,18045,18046,
  78895. 18047,18048,18049,18050,18051,18052,18053,18054,18055,18056,18057,18058,
  78896. 18059,18060,18061,18062,18063,18064,18065,18066,18067,18068,18069,18070,
  78897. 18071,18072,18073,18074,18075,18076,18077,18078,18079,18080,18081,18082,
  78898. 18083,18084,18085,18086,18087,18088,18089,18090,18091,18092,18093,18094,
  78899. 18095,18096,18097,18098,18099,18100,18101,18102,18103,18104,18105,18106,
  78900. 18107,18108,18109,18110,18111,18112,18113,18114,18115,18116,18117,18118,
  78901. 18119,18120,18121,18122,18123,18124,18125,18126,18127,18128,18129,18130,
  78902. 18131,18132,18133,18134,18135,18136,18137,18138,18139,18140,18141,18142,
  78903. 18143,18144,18145,18146,18147,18148,18149,18150,18151,18152,18153,18154,
  78904. 18155,18156,18157,18158,18159,18160,18161,18162,18163,18164,18165,18166,
  78905. 18167,18168,18169,18170,18171,18172,18173,18174,18175,18176,18177,18178,
  78906. 18179,18180,18181,18182,18183,18184,18185,18186,18187,18188,18189,18190,
  78907. 18191,18192,18193,18194,18195,18196,18197,18198,18199,18200,18201,18202,
  78908. 18203,18204,18205,18206,18207,18208,18209,18210,18211,18212,18213,18214,
  78909. 18215,18216,18217,18218,18219,18220,18221,18222,18223,18224,18225,18226,
  78910. 18227,18228,18229,18230,18231,18232,18233,18234,18235,18236,18237,18238,
  78911. 18239,18240,18241,18242,18243,18244,18245,18246,18247,18248,18249,18250,
  78912. 18251,18252,18253,18254,18255,18256,18257,18258,18259,18260,18261,18262,
  78913. 18263,18264,18265,18266,18267,18268,18269,18270,18271,18272,18273,18274,
  78914. 18275,18276,18277,18278,18279,18280,18281,18282,18283,18284,18285,18286,
  78915. 18287,18288,18289,18290,18291,18292,18293,18294,18295,18296,18297,18298,
  78916. 18299,18300,18301,18302,18303,18304,18305,18306,18307,18308,18309,18310,
  78917. 18311,18312,18313,18314,18315,18316,18317,18318,18319,18320,18321,18322,
  78918. 18323,18324,18325,18326,18327,18328,18329,18330,18331,18332,18333,18334,
  78919. 18335,18336,18337,18338,18339,18340,18341,18342,18343,18344,18345,18346,
  78920. 18347,18348,18349,18350,18351,18352,18353,18354,18355,18356,18357,18358,
  78921. 18359,18360,18361,18362,18363,18364,18365,18366,18367,18368,18369,18370,
  78922. 18371,18372,18373,18374,18375,18376,18377,18378,18379,18380,18381,18382,
  78923. 18383,18384,18385,18386,18387,18388,18389,18390,18391,18392,18393,18394,
  78924. 18395,18396,18397,18398,18399,18400,18401,18402,18403,18404,18405,18406,
  78925. 18407,18408,18409,18410,18411,18412,18413,18414,18415,18416,18417,18418,
  78926. 18419,18420,18421,18422,18423,18424,18425,18426,18427,18428,18429,18430,
  78927. 18431,18432,18433,18434,18435,18436,18437,18438,18439,18440,18441,18442,
  78928. 18443,18444,18445,18446,18447,18448,18449,18450,18451,18452,18453,18454,
  78929. 18455,18456,18457,18458,18459,18460,18461,18462,18463,18464,18465,18466,
  78930. 18467,18468,18469,18470,18471,18472,18473,18474,18475,18476,18477,18478,
  78931. 18479,18480,18481,18482,18483,18484,18485,18486,18487,18488,18489,18490,
  78932. 18491,18492,18493,18494,18495,18496,18497,18498,18499,18500,18501,18502,
  78933. 18503,18504,18505,18506,18507,18508,18509,18510,18511,18512,18513,18514,
  78934. 18515,18516,18517,18518,18519,18520,18521,18522,18523,18524,18525,18526,
  78935. 18527,18528,18529,18530,18531,18532,18533,18534,18535,18536,18537,18538,
  78936. 18539,18540,18541,18542,18543,18544,18545,18546,18547,18548,18549,18550,
  78937. 18551,18552,18553,18554,18555,18556,18557,18558,18559,18560,18561,18562,
  78938. 18563,18564,18565,18566,18567,18568,18569,18570,18571,18572,18573,18574,
  78939. 18575,18576,18577,18578,18579,18580,18581,18582,18583,18584,18585,18586,
  78940. 18587,18588,18589,18590,18591,18592,18593,18594,18595,18596,18597,18598,
  78941. 18599,18600,18601,18602,18603,18604,18605,18606,18607,18608,18609,18610,
  78942. 18611,18612,18613,18614,18615,18616,18617,18618,18619,18620,18621,18622,
  78943. 18623,18624,18625,18626,18627,18628,18629,18630,18631,18632,18633,18634,
  78944. 18635,18636,18637,18638,18639,18640,18641,18642,18643,18644,18645,18646,
  78945. 18647,18648,18649,18650,18651,18652,18653,18654,18655,18656,18657,18658,
  78946. 18659,18660,18661,18662,18663,18664,18665,18666,18667,18668,18669,18670,
  78947. 18671,18672,18673,18674,18675,18676,18677,18678,18679,18680,18681,18682,
  78948. 18683,18684,18685,18686,18687,18688,18689,18690,18691,18692,18693,18694,
  78949. 18695,18696,18697,18698,18699,18700,18701,18702,18703,18704,18705,18706,
  78950. 18707,18708,18709,18710,18711,18712,18713,18714,18715,18716,18717,18718,
  78951. 18719,18720,18721,18722,18723,18724,18725,18726,18727,18728,18729,18730,
  78952. 18731,18732,18733,18734,18735,18736,18737,18738,18739,18740,18741,18742,
  78953. 18743,18744,18745,18746,18747,18748,18749,18750,18751,18752,18753,18754,
  78954. 18755,18756,18757,18758,18759,18760,18761,18762,18763,18764,18765,18766,
  78955. 18767,18768,18769,18770,18771,18772,18773,18774,18775,18776,18777,18778,
  78956. 18779,18780,18781,18782,18783,18784,18785,18786,18787,18788,18789,18790,
  78957. 18791,18792,18793,18794,18795,18796,18797,18798,18799,18800,18801,18802,
  78958. 18803,18804,18805,18806,18807,18808,18809,18810,18811,18812,18813,18814,
  78959. 18815,18816,18817,18818,18819,18820,18821,18822,18823,18824,18825,18826,
  78960. 18827,18828,18829,18830,18831,18832,18833,18834,18835,18836,18837,18838,
  78961. 18839,18840,18841,18842,18843,18844,18845,18846,18847,18848,18849,18850,
  78962. 18851,18852,18853,18854,18855,18856,18857,18858,18859,18860,18861,18862,
  78963. 18863,18864,18865,18866,18867,18868,18869,18870,18871,18872,18873,18874,
  78964. 18875,18876,18877,18878,18879,18880,18881,18882,18883,18884,18885,18886,
  78965. 18887,18888,18889,18890,18891,18892,18893,18894,18895,18896,18897,18898,
  78966. 18899,18900,18901,18902,18903,18904,18905,18906,18907,18908,18909,18910,
  78967. 18911,18912,18913,18914,18915,18916,18917,18918,18919,18920,18921,18922,
  78968. 18923,18924,18925,18926,18927,18928,18929,18930,18931,18932,18933,18934,
  78969. 18935,18936,18937,18938,18939,18940,18941,18942,18943,18944,18945,18946,
  78970. 18947,18948,18949,18950,18951,18952,18953,18954,18955,18956,18957,18958,
  78971. 18959,18960,18961,18962,18963,18964,18965,18966,18967,18968,18969,18970,
  78972. 18971,18972,18973,18974,18975,18976,18977,18978,18979,18980,18981,18982,
  78973. 18983,18984,18985,18986,18987,18988,18989,18990,18991,18992,18993,18994,
  78974. 18995,18996,18997,18998,18999,19000,19001,19002,19003,19004,19005,19006,
  78975. 19007,19008,19009,19010,19011,19012,19013,19014,19015,19016,19017,19018,
  78976. 19019,19020,19021,19022,19023,19024,19025,19026,19027,19028,19029,19030,
  78977. 19031,19032,19033,19034,19035,19036,19037,19038,19039,19040,19041,19042,
  78978. 19043,19044,19045,19046,19047,19048,19049,19050,19051,19052,19053,19054,
  78979. 19055,19056,19057,19058,19059,19060,19061,19062,19063,19064,19065,19066,
  78980. 19067,19068,19069,19070,19071,19072,19073,19074,19075,19076,19077,19078,
  78981. 19079,19080,19081,19082,19083,19084,19085,19086,19087,19088,19089,19090,
  78982. 19091,19092,19093,19094,19095,19096,19097,19098,19099,19100,19101,19102,
  78983. 19103,19104,19105,19106,19107,19108,19109,19110,19111,19112,19113,19114,
  78984. 19115,19116,19117,19118,19119,19120,19121,19122,19123,19124,19125,19126,
  78985. 19127,19128,19129,19130,19131,19132,19133,19134,19135,19136,19137,19138,
  78986. 19139,19140,19141,19142,19143,19144,19145,19146,19147,19148,19149,19150,
  78987. 19151,19152,19153,19154,19155,19156,19157,19158,19159,19160,19161,19162,
  78988. 19163,19164,19165,19166,19167,19168,19169,19170,19171,19172,19173,19174,
  78989. 19175,19176,19177,19178,19179,19180,19181,19182,19183,19184,19185,19186,
  78990. 19187,19188,19189,19190,19191,19192,19193,19194,19195,19196,19197,19198,
  78991. 19199,19200,19201,19202,19203,19204,19205,19206,19207,19208,19209,19210,
  78992. 19211,19212,19213,19214,19215,19216,19217,19218,19219,19220,19221,19222,
  78993. 19223,19224,19225,19226,19227,19228,19229,19230,19231,19232,19233,19234,
  78994. 19235,19236,19237,19238,19239,19240,19241,19242,19243,19244,19245,19246,
  78995. 19247,19248,19249,19250,19251,19252,19253,19254,19255,19256,19257,19258,
  78996. 19259,19260,19261,19262,19263,19264,19265,19266,19267,19268,19269,19270,
  78997. 19271,19272,19273,19274,19275,19276,19277,19278,19279,19280,19281,19282,
  78998. 19283,19284,19285,19286,19287,19288,19289,19290,19291,19292,19293,19294,
  78999. 19295,19296,19297,19298,19299,19300,19301,19302,19303,19304,19305,19306,
  79000. 19307,19308,19309,19310,19311,19312,19313,19314,19315,19316,19317,19318,
  79001. 19319,19320,19321,19322,19323,19324,19325,19326,19327,19328,19329,19330,
  79002. 19331,19332,19333,19334,19335,19336,19337,19338,19339,19340,19341,19342,
  79003. 19343,19344,19345,19346,19347,19348,19349,19350,19351,19352,19353,19354,
  79004. 19355,19356,19357,19358,19359,19360,19361,19362,19363,19364,19365,19366,
  79005. 19367,19368,19369,19370,19371,19372,19373,19374,19375,19376,19377,19378,
  79006. 19379,19380,19381,19382,19383,19384,19385,19386,19387,19388,19389,19390,
  79007. 19391,19392,19393,19394,19395,19396,19397,19398,19399,19400,19401,19402,
  79008. 19403,19404,19405,19406,19407,19408,19409,19410,19411,19412,19413,19414,
  79009. 19415,19416,19417,19418,19419,19420,19421,19422,19423,19424,19425,19426,
  79010. 19427,19428,19429,19430,19431,19432,19433,19434,19435,19436,19437,19438,
  79011. 19439,19440,19441,19442,19443,19444,19445,19446,19447,19448,19449,19450,
  79012. 19451,19452,19453,19454,19455,19456,19457,19458,19459,19460,19461,19462,
  79013. 19463,19464,19465,19466,19467,19468,19469,19470,19471,19472,19473,19474,
  79014. 19475,19476,19477,19478,19479,19480,19481,19482,19483,19484,19485,19486,
  79015. 19487,19488,19489,19490,19491,19492,19493,19494,19495,19496,19497,19498,
  79016. 19499,19500,19501,19502,19503,19504,19505,19506,19507,19508,19509,19510,
  79017. 19511,19512,19513,19514,19515,19516,19517,19518,19519,19520,19521,19522,
  79018. 19523,19524,19525,19526,19527,19528,19529,19530,19531,19532,19533,19534,
  79019. 19535,19536,19537,19538,19539,19540,19541,19542,19543,19544,19545,19546,
  79020. 19547,19548,19549,19550,19551,19552,19553,19554,19555,19556,19557,19558,
  79021. 19559,19560,19561,19562,19563,19564,19565,19566,19567,19568,19569,19570,
  79022. 19571,19572,19573,19574,19575,19576,19577,19578,19579,19580,19581,19582,
  79023. 19583,19584,19585,19586,19587,19588,19589,19590,19591,19592,19593,19594,
  79024. 19595,19596,19597,19598,19599,19600,19601,19602,19603,19604,19605,19606,
  79025. 19607,19608,19609,19610,19611,19612,19613,19614,19615,19616,19617,19618,
  79026. 19619,19620,19621,19622,19623,19624,19625,19626,19627,19628,19629,19630,
  79027. 19631,19632,19633,19634,19635,19636,19637,19638,19639,19640,19641,19642,
  79028. 19643,19644,19645,19646,19647,19648,19649,19650,19651,19652,19653,19654,
  79029. 19655,19656,19657,19658,19659,19660,19661,19662,19663,19664,19665,19666,
  79030. 19667,19668,19669,19670,19671,19672,19673,19674,19675,19676,19677,19678,
  79031. 19679,19680,19681,19682,19683,19684,19685,19686,19687,19688,19689,19690,
  79032. 19691,19692,19693,19694,19695,19696,19697,19698,19699,19700,19701,19702,
  79033. 19703,19704,19705,19706,19707,19708,19709,19710,19711,19712,19713,19714,
  79034. 19715,19716,19717,19718,19719,19720,19721,19722,19723,19724,19725,19726,
  79035. 19727,19728,19729,19730,19731,19732,19733,19734,19735,19736,19737,19738,
  79036. 19739,19740,19741,19742,19743,19744,19745,19746,19747,19748,19749,19750,
  79037. 19751,19752,19753,19754,19755,19756,19757,19758,19759,19760,19761,19762,
  79038. 19763,19764,19765,19766,19767,19768,19769,19770,19771,19772,19773,19774,
  79039. 19775,19776,19777,19778,19779,19780,19781,19782,19783,19784,19785,19786,
  79040. 19787,19788,19789,19790,19791,19792,19793,19794,19795,19796,19797,19798,
  79041. 19799,19800,19801,19802,19803,19804,19805,19806,19807,19808,19809,19810,
  79042. 19811,19812,19813,19814,19815,19816,19817,19818,19819,19820,19821,19822,
  79043. 19823,19824,19825,19826,19827,19828,19829,19830,19831,19832,19833,19834,
  79044. 19835,19836,19837,19838,19839,19840,19841,19842,19843,19844,19845,19846,
  79045. 19847,19848,19849,19850,19851,19852,19853,19854,19855,19856,19857,19858,
  79046. 19859,19860,19861,19862,19863,19864,19865,19866,19867,19868,19869,19870,
  79047. 19871,19872,19873,19874,19875,19876,19877,19878,19879,19880,19881,19882,
  79048. 19883,19884,19885,19886,19887,19888,19889,19890,19891,19892,19893,19894,
  79049. 19895,19896,19897,19898,19899,19900,19901,19902,19903,19904,19905,19906,
  79050. 19907,19908,19909,19910,19911,19912,19913,19914,19915,19916,19917,19918,
  79051. 19919,19920,19921,19922,19923,19924,19925,19926,19927,19928,19929,19930,
  79052. 19931,19932,19933,19934,19935,19936,19937,19938,19939,19940,19941,19942,
  79053. 19943,19944,19945,19946,19947,19948,19949,19950,19951,19952,19953,19954,
  79054. 19955,19956,19957,19958,19959,19960,19961,19962,19963,19964,19965,19966,
  79055. 19967,19968,19969,19970,19971,19972,19973,19974,19975,19976,19977,19978,
  79056. 19979,19980,19981,19982,19983,19984,19985,19986,19987,19988,19989,19990,
  79057. 19991,19992,19993,19994,19995,19996,19997,19998,19999,20000,20001,20002,
  79058. 20003,20004,20005,20006,20007,20008,20009,20010,20011,20012,20013,20014,
  79059. 20015,20016,20017,20018,20019,20020,20021,20022,20023,20024,20025,20026,
  79060. 20027,20028,20029,20030,20031,20032,20033,20034,20035,20036,20037,20038,
  79061. 20039,20040,20041,20042,20043,20044,20045,20046,20047,20048,20049,20050,
  79062. 20051,20052,20053,20054,20055,20056,20057,20058,20059,20060,20061,20062,
  79063. 20063,20064,20065,20066,20067,20068,20069,20070,20071,20072,20073,20074,
  79064. 20075,20076,20077,20078,20079,20080,20081,20082,20083,20084,20085,20086,
  79065. 20087,20088,20089,20090,20091,20092,20093,20094,20095,20096,20097,20098,
  79066. 20099,20100,20101,20102,20103,20104,20105,20106,20107,20108,20109,20110,
  79067. 20111,20112,20113,20114,20115,20116,20117,20118,20119,20120,20121,20122,
  79068. 20123,20124,20125,20126,20127,20128,20129,20130,20131,20132,20133,20134,
  79069. 20135,20136,20137,20138,20139,20140,20141,20142,20143,20144,20145,20146,
  79070. 20147,20148,20149,20150,20151,20152,20153,20154,20155,20156,20157,20158,
  79071. 20159,20160,20161,20162,20163,20164,20165,20166,20167,20168,20169,20170,
  79072. 20171,20172,20173,20174,20175,20176,20177,20178,20179,20180,20181,20182,
  79073. 20183,20184,20185,20186,20187,20188,20189,20190,20191,20192,20193,20194,
  79074. 20195,20196,20197,20198,20199,20200,20201,20202,20203,20204,20205,20206,
  79075. 20207,20208,20209,20210,20211,20212,20213,20214,20215,20216,20217,20218,
  79076. 20219,20220,20221,20222,20223,20224,20225,20226,20227,20228,20229,20230,
  79077. 20231,20232,20233,20234,20235,20236,20237,20238,20239,20240,20241,20242,
  79078. 20243,20244,20245,20246,20247,20248,20249,20250,20251,20252,20253,20254,
  79079. 20255,20256,20257,20258,20259,20260,20261,20262,20263,20264,20265,20266,
  79080. 20267,20268,20269,20270,20271,20272,20273,20274,20275,20276,20277,20278,
  79081. 20279,20280,20281,20282,20283,20284,20285,20286,20287,20288,20289,20290,
  79082. 20291,20292,20293,20294,20295,20296,20297,20298,20299,20300,20301,20302,
  79083. 20303,20304,20305,20306,20307,20308,20309,20310,20311,20312,20313,20314,
  79084. 20315,20316,20317,20318,20319,20320,20321,20322,20323,20324,20325,20326,
  79085. 20327,20328,20329,20330,20331,20332,20333,20334,20335,20336,20337,20338,
  79086. 20339,20340,20341,20342,20343,20344,20345,20346,20347,20348,20349,20350,
  79087. 20351,20352,20353,20354,20355,20356,20357,20358,20359,20360,20361,20362,
  79088. 20363,20364,20365,20366,20367,20368,20369,20370,20371,20372,20373,20374,
  79089. 20375,20376,20377,20378,20379,20380,20381,20382,20383,20384,20385,20386,
  79090. 20387,20388,20389,20390,20391,20392,20393,20394,20395,20396,20397,20398,
  79091. 20399,20400,20401,20402,20403,20404,20405,20406,20407,20408,20409,20410,
  79092. 20411,20412,20413,20414,20415,20416,20417,20418,20419,20420,20421,20422,
  79093. 20423,20424,20425,20426,20427,20428,20429,20430,20431,20432,20433,20434,
  79094. 20435,20436,20437,20438,20439,20440,20441,20442,20443,20444,20445,20446,
  79095. 20447,20448,20449,20450,20451,20452,20453,20454,20455,20456,20457,20458,
  79096. 20459,20460,20461,20462,20463,20464,20465,20466,20467,20468,20469,20470,
  79097. 20471,20472,20473,20474,20475,20476,20477,20478,20479,20480,20481,20482,
  79098. 20483,20484,20485,20486,20487,20488,20489,20490,20491,20492,20493,20494,
  79099. 20495,20496,20497,20498,20499,20500,20501,20502,20503,20504,20505,20506,
  79100. 20507,20508,20509,20510,20511,20512,20513,20514,20515,20516,20517,20518,
  79101. 20519,20520,20521,20522,20523,20524,20525,20526,20527,20528,20529,20530,
  79102. 20531,20532,20533,20534,20535,20536,20537,20538,20539,20540,20541,20542,
  79103. 20543,20544,20545,20546,20547,20548,20549,20550,20551,20552,20553,20554,
  79104. 20555,20556,20557,20558,20559,20560,20561,20562,20563,20564,20565,20566,
  79105. 20567,20568,20569,20570,20571,20572,20573,20574,20575,20576,20577,20578,
  79106. 20579,20580,20581,20582,20583,20584,20585,20586,20587,20588,20589,20590,
  79107. 20591,20592,20593,20594,20595,20596,20597,20598,20599,20600,20601,20602,
  79108. 20603,20604,20605,20606,20607,20608,20609,20610,20611,20612,20613,20614,
  79109. 20615,20616,20617,20618,20619,20620,20621,20622,20623,20624,20625,20626,
  79110. 20627,20628,20629,20630,20631,20632,20633,20634,20635,20636,20637,20638,
  79111. 20639,20640,20641,20642,20643,20644,20645,20646,20647,20648,20649,20650,
  79112. 20651,20652,20653,20654,20655,20656,20657,20658,20659,20660,20661,20662,
  79113. 20663,20664,20665,20666,20667,20668,20669,20670,20671,20672,20673,20674,
  79114. 20675,20676,20677,20678,20679,20680,20681,20682,20683,20684,20685,20686,
  79115. 20687,20688,20689,20690,20691,20692,20693,20694,20695,20696,20697,20698,
  79116. 20699,20700,20701,20702,20703,20704,20705,20706,20707,20708,20709,20710,
  79117. 20711,20712,20713,20714,20715,20716,20717,20718,20719,20720,20721,20722,
  79118. 20723,20724,20725,20726,20727,20728,20729,20730,20731,20732,20733,20734,
  79119. 20735,20736,20737,20738,20739,20740,20741,20742,20743,20744,20745,20746,
  79120. 20747,20748,20749,20750,20751,20752,20753,20754,20755,20756,20757,20758,
  79121. 20759,20760,20761,20762,20763,20764,20765,20766,20767,20768,20769,20770,
  79122. 20771,20772,20773,20774,20775,20776,20777,20778,20779,20780,20781,20782,
  79123. 20783,20784,20785,20786,20787,20788,20789,20790,20791,20792,20793,20794,
  79124. 20795,20796,20797,20798,20799,20800,20801,20802,20803,20804,20805,20806,
  79125. 20807,20808,20809,20810,20811,20812,20813,20814,20815,20816,20817,20818,
  79126. 20819,20820,20821,20822,20823,20824,20825,20826,20827,20828,20829,20830,
  79127. 20831,20832,20833,20834,20835,20836,20837,20838,20839,20840,20841,20842,
  79128. 20843,20844,20845,20846,20847,20848,20849,20850,20851,20852,20853,20854,
  79129. 20855,20856,20857,20858,20859,20860,20861,20862,20863,20864,20865,20866,
  79130. 20867,20868,20869,20870,20871,20872,20873,20874,20875,20876,20877,20878,
  79131. 20879,20880,20881,20882,20883,20884,20885,20886,20887,20888,20889,20890,
  79132. 20891,20892,20893,20894,20895,20896,20897,20898,20899,20900,20901,20902,
  79133. 20903,20904,20905,20906,20907,20908,20909,20910,20911,20912,20913,20914,
  79134. 20915,20916,20917,20918,20919,20920,20921,20922,20923,20924,20925,20926,
  79135. 20927,20928,20929,20930,20931,20932,20933,20934,20935,20936,20937,20938,
  79136. 20939,20940,20941,20942,20943,20944,20945,20946,20947,20948,20949,20950,
  79137. 20951,20952,20953,20954,20955,20956,20957,20958,20959,20960,20961,20962,
  79138. 20963,20964,20965,20966,20967,20968,20969,20970,20971,20972,20973,20974,
  79139. 20975,20976,20977,20978,20979,20980,20981,20982,20983,20984,20985,20986,
  79140. 20987,20988,20989,20990,20991,20992,20993,20994,20995,20996,20997,20998,
  79141. 20999,21000,21001,21002,21003,21004,21005,21006,21007,21008,21009,21010,
  79142. 21011,21012,21013,21014,21015,21016,21017,21018,21019,21020,21021,21022,
  79143. 21023,21024,21025,21026,21027,21028,21029,21030,21031,21032,21033,21034,
  79144. 21035,21036,21037,21038,21039,21040,21041,21042,21043,21044,21045,21046,
  79145. 21047,21048,21049,21050,21051,21052,21053,21054,21055,21056,21057,21058,
  79146. 21059,21060,21061,21062,21063,21064,21065,21066,21067,21068,21069,21070,
  79147. 21071,21072,21073,21074,21075,21076,21077,21078,21079,21080,21081,21082,
  79148. 21083,21084,21085,21086,21087,21088,21089,21090,21091,21092,21093,21094,
  79149. 21095,21096,21097,21098,21099,21100,21101,21102,21103,21104,21105,21106,
  79150. 21107,21108,21109,21110,21111,21112,21113,21114,21115,21116,21117,21118,
  79151. 21119,21120,21121,21122,21123,21124,21125,21126,21127,21128,21129,21130,
  79152. 21131,21132,21133,21134,21135,21136,21137,21138,21139,21140,21141,21142,
  79153. 21143,21144,21145,21146,21147,21148,21149,21150,21151,21152,21153,21154,
  79154. 21155,21156,21157,21158,21159,21160,21161,21162,21163,21164,21165,21166,
  79155. 21167,21168,21169,21170,21171,21172,21173,21174,21175,21176,21177,21178,
  79156. 21179,21180,21181,21182,21183,21184,21185,21186,21187,21188,21189,21190,
  79157. 21191,21192,21193,21194,21195,21196,21197,21198,21199,21200,21201,21202,
  79158. 21203,21204,21205,21206,21207,21208,21209,21210,21211,21212,21213,21214,
  79159. 21215,21216,21217,21218,21219,21220,21221,21222,21223,21224,21225,21226,
  79160. 21227,21228,21229,21230,21231,21232,21233,21234,21235,21236,21237,21238,
  79161. 21239,21240,21241,21242,21243,21244,21245,21246,21247,21248,21249,21250,
  79162. 21251,21252,21253,21254,21255,21256,21257,21258,21259,21260,21261,21262,
  79163. 21263,21264,21265,21266,21267,21268,21269,21270,21271,21272,21273,21274,
  79164. 21275,21276,21277,21278,21279,21280,21281,21282,21283,21284,21285,21286,
  79165. 21287,21288,21289,21290,21291,21292,21293,21294,21295,21296,21297,21298,
  79166. 21299,21300,21301,21302,21303,21304,21305,21306,21307,21308,21309,21310,
  79167. 21311,21312,21313,21314,21315,21316,21317,21318,21319,21320,21321,21322,
  79168. 21323,21324,21325,21326,21327,21328,21329,21330,21331,21332,21333,21334,
  79169. 21335,21336,21337,21338,21339,21340,21341,21342,21343,21344,21345,21346,
  79170. 21347,21348,21349,21350,21351,21352,21353,21354,21355,21356,21357,21358,
  79171. 21359,21360,21361,21362,21363,21364,21365,21366,21367,21368,21369,21370,
  79172. 21371,21372,21373,21374,21375,21376,21377,21378,21379,21380,21381,21382,
  79173. 21383,21384,21385,21386,21387,21388,21389,21390,21391,21392,21393,21394,
  79174. 21395,21396,21397,21398,21399,21400,21401,21402,21403,21404,21405,21406,
  79175. 21407,21408,21409,21410,21411,21412,21413,21414,21415,21416,21417,21418,
  79176. 21419,21420,21421,21422,21423,21424,21425,21426,21427,21428,21429,21430,
  79177. 21431,21432,21433,21434,21435,21436,21437,21438,21439,21440,21441,21442,
  79178. 21443,21444,21445,21446,21447,21448,21449,21450,21451,21452,21453,21454,
  79179. 21455,21456,21457,21458,21459,21460,21461,21462,21463,21464,21465,21466,
  79180. 21467,21468,21469,21470,21471,21472,21473,21474,21475,21476,21477,21478,
  79181. 21479,21480,21481,21482,21483,21484,21485,21486,21487,21488,21489,21490,
  79182. 21491,21492,21493,21494,21495,21496,21497,21498,21499,21500,21501,21502,
  79183. 21503,21504,21505,21506,21507,21508,21509,21510,21511,21512,21513,21514,
  79184. 21515,21516,21517,21518,21519,21520,21521,21522,21523,21524,21525,21526,
  79185. 21527,21528,21529,21530,21531,21532,21533,21534,21535,21536,21537,21538,
  79186. 21539,21540,21541,21542,21543,21544,21545,21546,21547,21548,21549,21550,
  79187. 21551,21552,21553,21554,21555,21556,21557,21558,21559,21560,21561,21562,
  79188. 21563,21564,21565,21566,21567,21568,21569,21570,21571,21572,21573,21574,
  79189. 21575,21576,21577,21578,21579,21580,21581,21582,21583,21584,21585,21586,
  79190. 21587,21588,21589,21590,21591,21592,21593,21594,21595,21596,21597,21598,
  79191. 21599,21600,21601,21602,21603,21604,21605,21606,21607,21608,21609,21610,
  79192. 21611,21612,21613,21614,21615,21616,21617,21618,21619,21620,21621,21622,
  79193. 21623,21624,21625,21626,21627,21628,21629,21630,21631,21632,21633,21634,
  79194. 21635,21636,21637,21638,21639,21640,21641,21642,21643,21644,21645,21646,
  79195. 21647,21648,21649,21650,21651,21652,21653,21654,21655,21656,21657,21658,
  79196. 21659,21660,21661,21662,21663,21664,21665,21666,21667,21668,21669,21670,
  79197. 21671,21672,21673,21674,21675,21676,21677,21678,21679,21680,21681,21682,
  79198. 21683,21684,21685,21686,21687,21688,21689,21690,21691,21692,21693,21694,
  79199. 21695,21696,21697,21698,21699,21700,21701,21702,21703,21704,21705,21706,
  79200. 21707,21708,21709,21710,21711,21712,21713,21714,21715,21716,21717,21718,
  79201. 21719,21720,21721,21722,21723,21724,21725,21726,21727,21728,21729,21730,
  79202. 21731,21732,21733,21734,21735,21736,21737,21738,21739,21740,21741,21742,
  79203. 21743,21744,21745,21746,21747,21748,21749,21750,21751,21752,21753,21754,
  79204. 21755,21756,21757,21758,21759,21760,21761,21762,21763,21764,21765,21766,
  79205. 21767,21768,21769,21770,21771,21772,21773,21774,21775,21776,21777,21778,
  79206. 21779,21780,21781,21782,21783,21784,21785,21786,21787,21788,21789,21790,
  79207. 21791,21792,21793,21794,21795,21796,21797,21798,21799,21800,21801,21802,
  79208. 21803,21804,21805,21806,21807,21808,21809,21810,21811,21812,21813,21814,
  79209. 21815,21816,21817,21818,21819,21820,21821,21822,21823,21824,21825,21826,
  79210. 21827,21828,21829,21830,21831,21832,21833,21834,21835,21836,21837,21838,
  79211. 21839,21840,21841,21842,21843,21844,21845,21846,21847,21848,21849,21850,
  79212. 21851,21852,21853,21854,21855,21856,21857,21858,21859,21860,21861,21862,
  79213. 21863,21864,21865,21866,21867,21868,21869,21870,21871,21872,21873,21874,
  79214. 21875,21876,21877,21878,21879,21880,21881,21882,21883,21884,21885,21886,
  79215. 21887,21888,21889,21890,21891,21892,21893,21894,21895,21896,21897,21898,
  79216. 21899,21900,21901,21902,21903,21904,21905,21906,21907,21908,21909,21910,
  79217. 21911,21912,21913,21914,21915,21916,21917,21918,21919,21920,21921,21922,
  79218. 21923,21924,21925,21926,21927,21928,21929,21930,21931,21932,21933,21934,
  79219. 21935,21936,21937,21938,21939,21940,21941,21942,21943,21944,21945,21946,
  79220. 21947,21948,21949,21950,21951,21952,21953,21954,21955,21956,21957,21958,
  79221. 21959,21960,21961,21962,21963,21964,21965,21966,21967,21968,21969,21970,
  79222. 21971,21972,21973,21974,21975,21976,21977,21978,21979,21980,21981,21982,
  79223. 21983,21984,21985,21986,21987,21988,21989,21990,21991,21992,21993,21994,
  79224. 21995,21996,21997,21998,21999,22000,22001,22002,22003,22004,22005,22006,
  79225. 22007,22008,22009,22010,22011,22012,22013,22014,22015,22016,22017,22018,
  79226. 22019,22020,22021,22022,22023,22024,22025,22026,22027,22028,22029,22030,
  79227. 22031,22032,22033,22034,22035,22036,22037,22038,22039,22040,22041,22042,
  79228. 22043,22044,22045,22046,22047,22048,22049,22050,22051,22052,22053,22054,
  79229. 22055,22056,22057,22058,22059,22060,22061,22062,22063,22064,22065,22066,
  79230. 22067,22068,22069,22070,22071,22072,22073,22074,22075,22076,22077,22078,
  79231. 22079,22080,22081,22082,22083,22084,22085,22086,22087,22088,22089,22090,
  79232. 22091,22092,22093,22094,22095,22096,22097,22098,22099,22100,22101,22102,
  79233. 22103,22104,22105,22106,22107,22108,22109,22110,22111,22112,22113,22114,
  79234. 22115,22116,22117,22118,22119,22120,22121,22122,22123,22124,22125,22126,
  79235. 22127,22128,22129,22130,22131,22132,22133,22134,22135,22136,22137,22138,
  79236. 22139,22140,22141,22142,22143,22144,22145,22146,22147,22148,22149,22150,
  79237. 22151,22152,22153,22154,22155,22156,22157,22158,22159,22160,22161,22162,
  79238. 22163,22164,22165,22166,22167,22168,22169,22170,22171,22172,22173,22174,
  79239. 22175,22176,22177,22178,22179,22180,22181,22182,22183,22184,22185,22186,
  79240. 22187,22188,22189,22190,22191,22192,22193,22194,22195,22196,22197,22198,
  79241. 22199,22200,22201,22202,22203,22204,22205,22206,22207,22208,22209,22210,
  79242. 22211,22212,22213,22214,22215,22216,22217,22218,22219,22220,22221,22222,
  79243. 22223,22224,22225,22226,22227,22228,22229,22230,22231,22232,22233,22234,
  79244. 22235,22236,22237,22238,22239,22240,22241,22242,22243,22244,22245,22246,
  79245. 22247,22248,22249,22250,22251,22252,22253,22254,22255,22256,22257,22258,
  79246. 22259,22260,22261,22262,22263,22264,22265,22266,22267,22268,22269,22270,
  79247. 22271,22272,22273,22274,22275,22276,22277,22278,22279,22280,22281,22282,
  79248. 22283,22284,22285,22286,22287,22288,22289,22290,22291,22292,22293,22294,
  79249. 22295,22296,22297,22298,22299,22300,22301,22302,22303,22304,22305,22306,
  79250. 22307,22308,22309,22310,22311,22312,22313,22314,22315,22316,22317,22318,
  79251. 22319,22320,22321,22322,22323,22324,22325,22326,22327,22328,22329,22330,
  79252. 22331,22332,22333,22334,22335,22336,22337,22338,22339,22340,22341,22342,
  79253. 22343,22344,22345,22346,22347,22348,22349,22350,22351,22352,22353,22354,
  79254. 22355,22356,22357,22358,22359,22360,22361,22362,22363,22364,22365,22366,
  79255. 22367,22368,22369,22370,22371,22372,22373,22374,22375,22376,22377,22378,
  79256. 22379,22380,22381,22382,22383,22384,22385,22386,22387,22388,22389,22390,
  79257. 22391,22392,22393,22394,22395,22396,22397,22398,22399,22400,22401,22402,
  79258. 22403,22404,22405,22406,22407,22408,22409,22410,22411,22412,22413,22414,
  79259. 22415,22416,22417,22418,22419,22420,22421,22422,22423,22424,22425,22426,
  79260. 22427,22428,22429,22430,22431,22432,22433,22434,22435,22436,22437,22438,
  79261. 22439,22440,22441,22442,22443,22444,22445,22446,22447,22448,22449,22450,
  79262. 22451,22452,22453,22454,22455,22456,22457,22458,22459,22460,22461,22462,
  79263. 22463,22464,22465,22466,22467,22468,22469,22470,22471,22472,22473,22474,
  79264. 22475,22476,22477,22478,22479,22480,22481,22482,22483,22484,22485,22486,
  79265. 22487,22488,22489,22490,22491,22492,22493,22494,22495,22496,22497,22498,
  79266. 22499,22500,22501,22502,22503,22504,22505,22506,22507,22508,22509,22510,
  79267. 22511,22512,22513,22514,22515,22516,22517,22518,22519,22520,22521,22522,
  79268. 22523,22524,22525,22526,22527,22528,22529,22530,22531,22532,22533,22534,
  79269. 22535,22536,22537,22538,22539,22540,22541,22542,22543,22544,22545,22546,
  79270. 22547,22548,22549,22550,22551,22552,22553,22554,22555,22556,22557,22558,
  79271. 22559,22560,22561,22562,22563,22564,22565,22566,22567,22568,22569,22570,
  79272. 22571,22572,22573,22574,22575,22576,22577,22578,22579,22580,22581,22582,
  79273. 22583,22584,22585,22586,22587,22588,22589,22590,22591,22592,22593,22594,
  79274. 22595,22596,22597,22598,22599,22600,22601,22602,22603,22604,22605,22606,
  79275. 22607,22608,22609,22610,22611,22612,22613,22614,22615,22616,22617,22618,
  79276. 22619,22620,22621,22622,22623,22624,22625,22626,22627,22628,22629,22630,
  79277. 22631,22632,22633,22634,22635,22636,22637,22638,22639,22640,22641,22642,
  79278. 22643,22644,22645,22646,22647,22648,22649,22650,22651,22652,22653,22654,
  79279. 22655,22656,22657,22658,22659,22660,22661,22662,22663,22664,22665,22666,
  79280. 22667,22668,22669,22670,22671,22672,22673,22674,22675,22676,22677,22678,
  79281. 22679,22680,22681,22682,22683,22684,22685,22686,22687,22688,22689,22690,
  79282. 22691,22692,22693,22694,22695,22696,22697,22698,22699,22700,22701,22702,
  79283. 22703,22704,22705,22706,22707,22708,22709,22710,22711,22712,22713,22714,
  79284. 22715,22716,22717,22718,22719,22720,22721,22722,22723,22724,22725,22726,
  79285. 22727,22728,22729,22730,22731,22732,22733,22734,22735,22736,22737,22738,
  79286. 22739,22740,22741,22742,22743,22744,22745,22746,22747,22748,22749,22750,
  79287. 22751,22752,22753,22754,22755,22756,22757,22758,22759,22760,22761,22762,
  79288. 22763,22764,22765,22766,22767,22768,22769,22770,22771,22772,22773,22774,
  79289. 22775,22776,22777,22778,22779,22780,22781,22782,22783,22784,22785,22786,
  79290. 22787,22788,22789,22790,22791,22792,22793,22794,22795,22796,22797,22798,
  79291. 22799,22800,22801,22802,22803,22804,22805,22806,22807,22808,22809,22810,
  79292. 22811,22812,22813,22814,22815,22816,22817,22818,22819,22820,22821,22822,
  79293. 22823,22824,22825,22826,22827,22828,22829,22830,22831,22832,22833,22834,
  79294. 22835,22836,22837,22838,22839,22840,22841,22842,22843,22844,22845,22846,
  79295. 22847,22848,22849,22850,22851,22852,22853,22854,22855,22856,22857,22858,
  79296. 22859,22860,22861,22862,22863,22864,22865,22866,22867,22868,22869,22870,
  79297. 22871,22872,22873,22874,22875,22876,22877,22878,22879,22880,22881,22882,
  79298. 22883,22884,22885,22886,22887,22888,22889,22890,22891,22892,22893,22894,
  79299. 22895,22896,22897,22898,22899,22900,22901,22902,22903,22904,22905,22906,
  79300. 22907,22908,22909,22910,22911,22912,22913,22914,22915,22916,22917,22918,
  79301. 22919,22920,22921,22922,22923,22924,22925,22926,22927,22928,22929,22930,
  79302. 22931,22932,22933,22934,22935,22936,22937,22938,22939,22940,22941,22942,
  79303. 22943,22944,22945,22946,22947,22948,22949,22950,22951,22952,22953,22954,
  79304. 22955,22956,22957,22958,22959,22960,22961,22962,22963,22964,22965,22966,
  79305. 22967,22968,22969,22970,22971,22972,22973,22974,22975,22976,22977,22978,
  79306. 22979,22980,22981,22982,22983,22984,22985,22986,22987,22988,22989,22990,
  79307. 22991,22992,22993,22994,22995,22996,22997,22998,22999,23000,23001,23002,
  79308. 23003,23004,23005,23006,23007,23008,23009,23010,23011,23012,23013,23014,
  79309. 23015,23016,23017,23018,23019,23020,23021,23022,23023,23024,23025,23026,
  79310. 23027,23028,23029,23030,23031,23032,23033,23034,23035,23036,23037,23038,
  79311. 23039,23040,23041,23042,23043,23044,23045,23046,23047,23048,23049,23050,
  79312. 23051,23052,23053,23054,23055,23056,23057,23058,23059,23060,23061,23062,
  79313. 23063,23064,23065,23066,23067,23068,23069,23070,23071,23072,23073,23074,
  79314. 23075,23076,23077,23078,23079,23080,23081,23082,23083,23084,23085,23086,
  79315. 23087,23088,23089,23090,23091,23092,23093,23094,23095,23096,23097,23098,
  79316. 23099,23100,23101,23102,23103,23104,23105,23106,23107,23108,23109,23110,
  79317. 23111,23112,23113,23114,23115,23116,23117,23118,23119,23120,23121,23122,
  79318. 23123,23124,23125,23126,23127,23128,23129,23130,23131,23132,23133,23134,
  79319. 23135,23136,23137,23138,23139,23140,23141,23142,23143,23144,23145,23146,
  79320. 23147,23148,23149,23150,23151,23152,23153,23154,23155,23156,23157,23158,
  79321. 23159,23160,23161,23162,23163,23164,23165,23166,23167,23168,23169,23170,
  79322. 23171,23172,23173,23174,23175,23176,23177,23178,23179,23180,23181,23182,
  79323. 23183,23184,23185,23186,23187,23188,23189,23190,23191,23192,23193,23194,
  79324. 23195,23196,23197,23198,23199,23200,23201,23202,23203,23204,23205,23206,
  79325. 23207,23208,23209,23210,23211,23212,23213,23214,23215,23216,23217,23218,
  79326. 23219,23220,23221,23222,23223,23224,23225,23226,23227,23228,23229,23230,
  79327. 23231,23232,23233,23234,23235,23236,23237,23238,23239,23240,23241,23242,
  79328. 23243,23244,23245,23246,23247,23248,23249,23250,23251,23252,23253,23254,
  79329. 23255,23256,23257,23258,23259,23260,23261,23262,23263,23264,23265,23266,
  79330. 23267,23268,23269,23270,23271,23272,23273,23274,23275,23276,23277,23278,
  79331. 23279,23280,23281,23282,23283,23284,23285,23286,23287,23288,23289,23290,
  79332. 23291,23292,23293,23294,23295,23296,23297,23298,23299,23300,23301,23302,
  79333. 23303,23304,23305,23306,23307,23308,23309,23310,23311,23312,23313,23314,
  79334. 23315,23316,23317,23318,23319,23320,23321,23322,23323,23324,23325,23326,
  79335. 23327,23328,23329,23330,23331,23332,23333,23334,23335,23336,23337,23338,
  79336. 23339,23340,23341,23342,23343,23344,23345,23346,23347,23348,23349,23350,
  79337. 23351,23352,23353,23354,23355,23356,23357,23358,23359,23360,23361,23362,
  79338. 23363,23364,23365,23366,23367,23368,23369,23370,23371,23372,23373,23374,
  79339. 23375,23376,23377,23378,23379,23380,23381,23382,23383,23384,23385,23386,
  79340. 23387,23388,23389,23390,23391,23392,23393,23394,23395,23396,23397,23398,
  79341. 23399,23400,23401,23402,23403,23404,23405,23406,23407,23408,23409,23410,
  79342. 23411,23412,23413,23414,23415,23416,23417,23418,23419,23420,23421,23422,
  79343. 23423,23424,23425,23426,23427,23428,23429,23430,23431,23432,23433,23434,
  79344. 23435,23436,23437,23438,23439,23440,23441,23442,23443,23444,23445,23446,
  79345. 23447,23448,23449,23450,23451,23452,23453,23454,23455,23456,23457,23458,
  79346. 23459,23460,23461,23462,23463,23464,23465,23466,23467,23468,23469,23470,
  79347. 23471,23472,23473,23474,23475,23476,23477,23478,23479,23480,23481,23482,
  79348. 23483,23484,23485,23486,23487,23488,23489,23490,23491,23492,23493,23494,
  79349. 23495,23496,23497,23498,23499,23500,23501,23502,23503,23504,23505,23506,
  79350. 23507,23508,23509,23510,23511,23512,23513,23514,23515,23516,23517,23518,
  79351. 23519,23520,23521,23522,23523,23524,23525,23526,23527,23528,23529,23530,
  79352. 23531,23532,23533,23534,23535,23536,23537,23538,23539,23540,23541,23542,
  79353. 23543,23544,23545,23546,23547,23548,23549,23550,23551,23552,23553,23554,
  79354. 23555,23556,23557,23558,23559,23560,23561,23562,23563,23564,23565,23566,
  79355. 23567,23568,23569,23570,23571,23572,23573,23574,23575,23576,23577,23578,
  79356. 23579,23580,23581,23582,23583,23584,23585,23586,23587,23588,23589,23590,
  79357. 23591,23592,23593,23594,23595,23596,23597,23598,23599,23600,23601,23602,
  79358. 23603,23604,23605,23606,23607,23608,23609,23610,23611,23612,23613,23614,
  79359. 23615,23616,23617,23618,23619,23620,23621,23622,23623,23624,23625,23626,
  79360. 23627,23628,23629,23630,23631,23632,23633,23634,23635,23636,23637,23638,
  79361. 23639,23640,23641,23642,23643,23644,23645,23646,23647,23648,23649,23650,
  79362. 23651,23652,23653,23654,23655,23656,23657,23658,23659,23660,23661,23662,
  79363. 23663,23664,23665,23666,23667,23668,23669,23670,23671,23672,23673,23674,
  79364. 23675,23676,23677,23678,23679,23680,23681,23682,23683,23684,23685,23686,
  79365. 23687,23688,23689,23690,23691,23692,23693,23694,23695,23696,23697,23698,
  79366. 23699,23700,23701,23702,23703,23704,23705,23706,23707,23708,23709,23710,
  79367. 23711,23712,23713,23714,23715,23716,23717,23718,23719,23720,23721,23722,
  79368. 23723,23724,23725,23726,23727,23728,23729,23730,23731,23732,23733,23734,
  79369. 23735,23736,23737,23738,23739,23740,23741,23742,23743,23744,23745,23746,
  79370. 23747,23748,23749,23750,23751,23752,23753,23754,23755,23756,23757,23758,
  79371. 23759,23760,23761,23762,23763,23764,23765,23766,23767,23768,23769,23770,
  79372. 23771,23772,23773,23774,23775,23776,23777,23778,23779,23780,23781,23782,
  79373. 23783,23784,23785,23786,23787,23788,23789,23790,23791,23792,23793,23794,
  79374. 23795,23796,23797,23798,23799,23800,23801,23802,23803,23804,23805,23806,
  79375. 23807,23808,23809,23810,23811,23812,23813,23814,23815,23816,23817,23818,
  79376. 23819,23820,23821,23822,23823,23824,23825,23826,23827,23828,23829,23830,
  79377. 23831,23832,23833,23834,23835,23836,23837,23838,23839,23840,23841,23842,
  79378. 23843,23844,23845,23846,23847,23848,23849,23850,23851,23852,23853,23854,
  79379. 23855,23856,23857,23858,23859,23860,23861,23862,23863,23864,23865,23866,
  79380. 23867,23868,23869,23870,23871,23872,23873,23874,23875,23876,23877,23878,
  79381. 23879,23880,23881,23882,23883,23884,23885,23886,23887,23888,23889,23890,
  79382. 23891,23892,23893,23894,23895,23896,23897,23898,23899,23900,23901,23902,
  79383. 23903,23904,23905,23906,23907,23908,23909,23910,23911,23912,23913,23914,
  79384. 23915,23916,23917,23918,23919,23920,23921,23922,23923,23924,23925,23926,
  79385. 23927,23928,23929,23930,23931,23932,23933,23934,23935,23936,23937,23938,
  79386. 23939,23940,23941,23942,23943,23944,23945,23946,23947,23948,23949,23950,
  79387. 23951,23952,23953,23954,23955,23956,23957,23958,23959,23960,23961,23962,
  79388. 23963,23964,23965,23966,23967,23968,23969,23970,23971,23972,23973,23974,
  79389. 23975,23976,23977,23978,23979,23980,23981,23982,23983,23984,23985,23986,
  79390. 23987,23988,23989,23990,23991,23992,23993,23994,23995,23996,23997,23998,
  79391. 23999,24000,24001,24002,24003,24004,24005,24006,24007,24008,24009,24010,
  79392. 24011,24012,24013,24014,24015,24016,24017,24018,24019,24020,24021,24022,
  79393. 24023,24024,24025,24026,24027,24028,24029,24030,24031,24032,24033,24034,
  79394. 24035,24036,24037,24038,24039,24040,24041,24042,24043,24044,24045,24046,
  79395. 24047,24048,24049,24050,24051,24052,24053,24054,24055,24056,24057,24058,
  79396. 24059,24060,24061,24062,24063,24064,24065,24066,24067,24068,24069,24070,
  79397. 24071,24072,24073,24074,24075,24076,24077,24078,24079,24080,24081,24082,
  79398. 24083,24084,24085,24086,24087,24088,24089,24090,24091,24092,24093,24094,
  79399. 24095,24096,24097,24098,24099,24100,24101,24102,24103,24104,24105,24106,
  79400. 24107,24108,24109,24110,24111,24112,24113,24114,24115,24116,24117,24118,
  79401. 24119,24120,24121,24122,24123,24124,24125,24126,24127,24128,24129,24130,
  79402. 24131,24132,24133,24134,24135,24136,24137,24138,24139,24140,24141,24142,
  79403. 24143,24144,24145,24146,24147,24148,24149,24150,24151,24152,24153,24154,
  79404. 24155,24156,24157,24158,24159,24160,24161,24162,24163,24164,24165,24166,
  79405. 24167,24168,24169,24170,24171,24172,24173,24174,24175,24176,24177,24178,
  79406. 24179,24180,24181,24182,24183,24184,24185,24186,24187,24188,24189,24190,
  79407. 24191,24192,24193,24194,24195,24196,24197,24198,24199,24200,24201,24202,
  79408. 24203,24204,24205,24206,24207,24208,24209,24210,24211,24212,24213,24214,
  79409. 24215,24216,24217,24218,24219,24220,24221,24222,24223,24224,24225,24226,
  79410. 24227,24228,24229,24230,24231,24232,24233,24234,24235,24236,24237,24238,
  79411. 24239,24240,24241,24242,24243,24244,24245,24246,24247,24248,24249,24250,
  79412. 24251,24252,24253,24254,24255,24256,24257,24258,24259,24260,24261,24262,
  79413. 24263,24264,24265,24266,24267,24268,24269,24270,24271,24272,24273,24274,
  79414. 24275,24276,24277,24278,24279,24280,24281,24282,24283,24284,24285,24286,
  79415. 24287,24288,24289,24290,24291,24292,24293,24294,24295,24296,24297,24298,
  79416. 24299,24300,24301,24302,24303,24304,24305,24306,24307,24308,24309,24310,
  79417. 24311,24312,24313,24314,24315,24316,24317,24318,24319,24320,24321,24322,
  79418. 24323,24324,24325,24326,24327,24328,24329,24330,24331,24332,24333,24334,
  79419. 24335,24336,24337,24338,24339,24340,24341,24342,24343,24344,24345,24346,
  79420. 24347,24348,24349,24350,24351,24352,24353,24354,24355,24356,24357,24358,
  79421. 24359,24360,24361,24362,24363,24364,24365,24366,24367,24368,24369,24370,
  79422. 24371,24372,24373,24374,24375,24376,24377,24378,24379,24380,24381,24382,
  79423. 24383,24384,24385,24386,24387,24388,24389,24390,24391,24392,24393,24394,
  79424. 24395,24396,24397,24398,24399,24400,24401,24402,24403,24404,24405,24406,
  79425. 24407,24408,24409,24410,24411,24412,24413,24414,24415,24416,24417,24418,
  79426. 24419,24420,24421,24422,24423,24424,24425,24426,24427,24428,24429,24430,
  79427. 24431,24432,24433,24434,24435,24436,24437,24438,24439,24440,24441,24442,
  79428. 24443,24444,24445,24446,24447,24448,24449,24450,24451,24452,24453,24454,
  79429. 24455,24456,24457,24458,24459,24460,24461,24462,24463,24464,24465,24466,
  79430. 24467,24468,24469,24470,24471,24472,24473,24474,24475,24476,24477,24478,
  79431. 24479,24480,24481,24482,24483,24484,24485,24486,24487,24488,24489,24490,
  79432. 24491,24492,24493,24494,24495,24496,24497,24498,24499,24500,24501,24502,
  79433. 24503,24504,24505,24506,24507,24508,24509,24510,24511,24512,24513,24514,
  79434. 24515,24516,24517,24518,24519,24520,24521,24522,24523,24524,24525,24526,
  79435. 24527,24528,24529,24530,24531,24532,24533,24534,24535,24536,24537,24538,
  79436. 24539,24540,24541,24542,24543,24544,24545,24546,24547,24548,24549,24550,
  79437. 24551,24552,24553,24554,24555,24556,24557,24558,24559,24560,24561,24562,
  79438. 24563,24564,24565,24566,24567,24568,24569,24570,24571,24572,24573,24574,
  79439. 24575,24576,24577,24578,24579,24580,24581,24582,24583,24584,24585,24586,
  79440. 24587,24588,24589,24590,24591,24592,24593,24594,24595,24596,24597,24598,
  79441. 24599,24600,24601,24602,24603,24604,24605,24606,24607,24608,24609,24610,
  79442. 24611,24612,24613,24614,24615,24616,24617,24618,24619,24620,24621,24622,
  79443. 24623,24624,24625,24626,24627,24628,24629,24630,24631,24632,24633,24634,
  79444. 24635,24636,24637,24638,24639,24640,24641,24642,24643,24644,24645,24646,
  79445. 24647,24648,24649,24650,24651,24652,24653,24654,24655,24656,24657,24658,
  79446. 24659,24660,24661,24662,24663,24664,24665,24666,24667,24668,24669,24670,
  79447. 24671,24672,24673,24674,24675,24676,24677,24678,24679,24680,24681,24682,
  79448. 24683,24684,24685,24686,24687,24688,24689,24690,24691,24692,24693,24694,
  79449. 24695,24696,24697,24698,24699,24700,24701,24702,24703,24704,24705,24706,
  79450. 24707,24708,24709,24710,24711,24712,24713,24714,24715,24716,24717,24718,
  79451. 24719,24720,24721,24722,24723,24724,24725,24726,24727,24728,24729,24730,
  79452. 24731,24732,24733,24734,24735,24736,24737,24738,24739,24740,24741,24742,
  79453. 24743,24744,24745,24746,24747,24748,24749,24750,24751,24752,24753,24754,
  79454. 24755,24756,24757,24758,24759,24760,24761,24762,24763,24764,24765,24766,
  79455. 24767,24768,24769,24770,24771,24772,24773,24774,24775,24776,24777,24778,
  79456. 24779,24780,24781,24782,24783,24784,24785,24786,24787,24788,24789,24790,
  79457. 24791,24792,24793,24794,24795,24796,24797,24798,24799,24800,24801,24802,
  79458. 24803,24804,24805,24806,24807,24808,24809,24810,24811,24812,24813,24814,
  79459. 24815,24816,24817,24818,24819,24820,24821,24822,24823,24824,24825,24826,
  79460. 24827,24828,24829,24830,24831,24832,24833,24834,24835,24836,24837,24838,
  79461. 24839,24840,24841,24842,24843,24844,24845,24846,24847,24848,24849,24850,
  79462. 24851,24852,24853,24854,24855,24856,24857,24858,24859,24860,24861,24862,
  79463. 24863,24864,24865,24866,24867,24868,24869,24870,24871,24872,24873,24874,
  79464. 24875,24876,24877,24878,24879,24880,24881,24882,24883,24884,24885,24886,
  79465. 24887,24888,24889,24890,24891,24892,24893,24894,24895,24896,24897,24898,
  79466. 24899,24900,24901,24902,24903,24904,24905,24906,24907,24908,24909,24910,
  79467. 24911,24912,24913,24914,24915,24916,24917,24918,24919,24920,24921,24922,
  79468. 24923,24924,24925,24926,24927,24928,24929,24930,24931,24932,24933,24934,
  79469. 24935,24936,24937,24938,24939,24940,24941,24942,24943,24944,24945,24946,
  79470. 24947,24948,24949,24950,24951,24952,24953,24954,24955,24956,24957,24958,
  79471. 24959,24960,24961,24962,24963,24964,24965,24966,24967,24968,24969,24970,
  79472. 24971,24972,24973,24974,24975,24976,24977,24978,24979,24980,24981,24982,
  79473. 24983,24984,24985,24986,24987,24988,24989,24990,24991,24992,24993,24994,
  79474. 24995,24996,24997,24998,24999,25000,25001,25002,25003,25004,25005,25006,
  79475. 25007,25008,25009,25010,25011,25012,25013,25014,25015,25016,25017,25018,
  79476. 25019,25020,25021,25022,25023,25024,25025,25026,25027,25028,25029,25030,
  79477. 25031,25032,25033,25034,25035,25036,25037,25038,25039,25040,25041,25042,
  79478. 25043,25044,25045,25046,25047,25048,25049,25050,25051,25052,25053,25054,
  79479. 25055,25056,25057,25058,25059,25060,25061,25062,25063,25064,25065,25066,
  79480. 25067,25068,25069,25070,25071,25072,25073,25074,25075,25076,25077,25078,
  79481. 25079,25080,25081,25082,25083,25084,25085,25086,25087,25088,25089,25090,
  79482. 25091,25092,25093,25094,25095,25096,25097,25098,25099,25100,25101,25102,
  79483. 25103,25104,25105,25106,25107,25108,25109,25110,25111,25112,25113,25114,
  79484. 25115,25116,25117,25118,25119,25120,25121,25122,25123,25124,25125,25126,
  79485. 25127,25128,25129,25130,25131,25132,25133,25134,25135,25136,25137,25138,
  79486. 25139,25140,25141,25142,25143,25144,25145,25146,25147,25148,25149,25150,
  79487. 25151,25152,25153,25154,25155,25156,25157,25158,25159,25160,25161,25162,
  79488. 25163,25164,25165,25166,25167,25168,25169,25170,25171,25172,25173,25174,
  79489. 25175,25176,25177,25178,25179,25180,25181,25182,25183,25184,25185,25186,
  79490. 25187,25188,25189,25190,25191,25192,25193,25194,25195,25196,25197,25198,
  79491. 25199,25200,25201,25202,25203,25204,25205,25206,25207,25208,25209,25210,
  79492. 25211,25212,25213,25214,25215,25216,25217,25218,25219,25220,25221,25222,
  79493. 25223,25224,25225,25226,25227,25228,25229,25230,25231,25232,25233,25234,
  79494. 25235,25236,25237,25238,25239,25240,25241,25242,25243,25244,25245,25246,
  79495. 25247,25248,25249,25250,25251,25252,25253,25254,25255,25256,25257,25258,
  79496. 25259,25260,25261,25262,25263,25264,25265,25266,25267,25268,25269,25270,
  79497. 25271,25272,25273,25274,25275,25276,25277,25278,25279,25280,25281,25282,
  79498. 25283,25284,25285,25286,25287,25288,25289,25290,25291,25292,25293,25294,
  79499. 25295,25296,25297,25298,25299,25300,25301,25302,25303,25304,25305,25306,
  79500. 25307,25308,25309,25310,25311,25312,25313,25314,25315,25316,25317,25318,
  79501. 25319,25320,25321,25322,25323,25324,25325,25326,25327,25328,25329,25330,
  79502. 25331,25332,25333,25334,25335,25336,25337,25338,25339,25340,25341,25342,
  79503. 25343,25344,25345,25346,25347,25348,25349,25350,25351,25352,25353,25354,
  79504. 25355,25356,25357,25358,25359,25360,25361,25362,25363,25364,25365,25366,
  79505. 25367,25368,25369,25370,25371,25372,25373,25374,25375,25376,25377,25378,
  79506. 25379,25380,25381,25382,25383,25384,25385,25386,25387,25388,25389,25390,
  79507. 25391,25392,25393,25394,25395,25396,25397,25398,25399,25400,25401,25402,
  79508. 25403,25404,25405,25406,25407,25408,25409,25410,25411,25412,25413,25414,
  79509. 25415,25416,25417,25418,25419,25420,25421,25422,25423,25424,25425,25426,
  79510. 25427,25428,25429,25430,25431,25432,25433,25434,25435,25436,25437,25438,
  79511. 25439,25440,25441,25442,25443,25444,25445,25446,25447,25448,25449,25450,
  79512. 25451,25452,25453,25454,25455,25456,25457,25458,25459,25460,25461,25462,
  79513. 25463,25464,25465,25466,25467,25468,25469,25470,25471,25472,25473,25474,
  79514. 25475,25476,25477,25478,25479,25480,25481,25482,25483,25484,25485,25486,
  79515. 25487,25488,25489,25490,25491,25492,25493,25494,25495,25496,25497,25498,
  79516. 25499,25500,25501,25502,25503,25504,25505,25506,25507,25508,25509,25510,
  79517. 25511,25512,25513,25514,25515,25516,25517,25518,25519,25520,25521,25522,
  79518. 25523,25524,25525,25526,25527,25528,25529,25530,25531,25532,25533,25534,
  79519. 25535,25536,25537,25538,25539,25540,25541,25542,25543,25544,25545,25546,
  79520. 25547,25548,25549,25550,25551,25552,25553,25554,25555,25556,25557,25558,
  79521. 25559,25560,25561,25562,25563,25564,25565,25566,25567,25568,25569,25570,
  79522. 25571,25572,25573,25574,25575,25576,25577,25578,25579,25580,25581,25582,
  79523. 25583,25584,25585,25586,25587,25588,25589,25590,25591,25592,25593,25594,
  79524. 25595,25596,25597,25598,25599,25600,25601,25602,25603,25604,25605,25606,
  79525. 25607,25608,25609,25610,25611,25612,25613,25614,25615,25616,25617,25618,
  79526. 25619,25620,25621,25622,25623,25624,25625,25626,25627,25628,25629,25630,
  79527. 25631,25632,25633,25634,25635,25636,25637,25638,25639,25640,25641,25642,
  79528. 25643,25644,25645,25646,25647,25648,25649,25650,25651,25652,25653,25654,
  79529. 25655,25656,25657,25658,25659,25660,25661,25662,25663,25664,25665,25666,
  79530. 25667,25668,25669,25670,25671,25672,25673,25674,25675,25676,25677,25678,
  79531. 25679,25680,25681,25682,25683,25684,25685,25686,25687,25688,25689,25690,
  79532. 25691,25692,25693,25694,25695,25696,25697,25698,25699,25700,25701,25702,
  79533. 25703,25704,25705,25706,25707,25708,25709,25710,25711,25712,25713,25714,
  79534. 25715,25716,25717,25718,25719,25720,25721,25722,25723,25724,25725,25726,
  79535. 25727,25728,25729,25730,25731,25732,25733,25734,25735,25736,25737,25738,
  79536. 25739,25740,25741,25742,25743,25744,25745,25746,25747,25748,25749,25750,
  79537. 25751,25752,25753,25754,25755,25756,25757,25758,25759,25760,25761,25762,
  79538. 25763,25764,25765,25766,25767,25768,25769,25770,25771,25772,25773,25774,
  79539. 25775,25776,25777,25778,25779,25780,25781,25782,25783,25784,25785,25786,
  79540. 25787,25788,25789,25790,25791,25792,25793,25794,25795,25796,25797,25798,
  79541. 25799,25800,25801,25802,25803,25804,25805,25806,25807,25808,25809,25810,
  79542. 25811,25812,25813,25814,25815,25816,25817,25818,25819,25820,25821,25822,
  79543. 25823,25824,25825,25826,25827,25828,25829,25830,25831,25832,25833,25834,
  79544. 25835,25836,25837,25838,25839,25840,25841,25842,25843,25844,25845,25846,
  79545. 25847,25848,25849,25850,25851,25852,25853,25854,25855,25856,25857,25858,
  79546. 25859,25860,25861,25862,25863,25864,25865,25866,25867,25868,25869,25870,
  79547. 25871,25872,25873,25874,25875,25876,25877,25878,25879,25880,25881,25882,
  79548. 25883,25884,25885,25886,25887,25888,25889,25890,25891,25892,25893,25894,
  79549. 25895,25896,25897,25898,25899,25900,25901,25902,25903,25904,25905,25906,
  79550. 25907,25908,25909,25910,25911,25912,25913,25914,25915,25916,25917,25918,
  79551. 25919,25920,25921,25922,25923,25924,25925,25926,25927,25928,25929,25930,
  79552. 25931,25932,25933,25934,25935,25936,25937,25938,25939,25940,25941,25942,
  79553. 25943,25944,25945,25946,25947,25948,25949,25950,25951,25952,25953,25954,
  79554. 25955,25956,25957,25958,25959,25960,25961,25962,25963,25964,25965,25966,
  79555. 25967,25968,25969,25970,25971,25972,25973,25974,25975,25976,25977,25978,
  79556. 25979,25980,25981,25982,25983,25984,25985,25986,25987,25988,25989,25990,
  79557. 25991,25992,25993,25994,25995,25996,25997,25998,25999,26000,26001,26002,
  79558. 26003,26004,26005,26006,26007,26008,26009,26010,26011,26012,26013,26014,
  79559. 26015,26016,26017,26018,26019,26020,26021,26022,26023,26024,26025,26026,
  79560. 26027,26028,26029,26030,26031,26032,26033,26034,26035,26036,26037,26038,
  79561. 26039,26040,26041,26042,26043,26044,26045,26046,26047,26048,26049,26050,
  79562. 26051,26052,26053,26054,26055,26056,26057,26058,26059,26060,26061,26062,
  79563. 26063,26064,26065,26066,26067,26068,26069,26070,26071,26072,26073,26074,
  79564. 26075,26076,26077,26078,26079,26080,26081,26082,26083,26084,26085,26086,
  79565. 26087,26088,26089,26090,26091,26092,26093,26094,26095,26096,26097,26098,
  79566. 26099,26100,26101,26102,26103,26104,26105,26106,26107,26108,26109,26110,
  79567. 26111,26112,26113,26114,26115,26116,26117,26118,26119,26120,26121,26122,
  79568. 26123,26124,26125,26126,26127,26128,26129,26130,26131,26132,26133,26134,
  79569. 26135,26136,26137,26138,26139,26140,26141,26142,26143,26144,26145,26146,
  79570. 26147,26148,26149,26150,26151,26152,26153,26154,26155,26156,26157,26158,
  79571. 26159,26160,26161,26162,26163,26164,26165,26166,26167,26168,26169,26170,
  79572. 26171,26172,26173,26174,26175,26176,26177,26178,26179,26180,26181,26182,
  79573. 26183,26184,26185,26186,26187,26188,26189,26190,26191,26192,26193,26194,
  79574. 26195,26196,26197,26198,26199,26200,26201,26202,26203,26204,26205,26206,
  79575. 26207,26208,26209,26210,26211,26212,26213,26214,26215,26216,26217,26218,
  79576. 26219,26220,26221,26222,26223,26224,26225,26226,26227,26228,26229,26230,
  79577. 26231,26232,26233,26234,26235,26236,26237,26238,26239,26240,26241,26242,
  79578. 26243,26244,26245,26246,26247,26248,26249,26250,26251,26252,26253,26254,
  79579. 26255,26256,26257,26258,26259,26260,26261,26262,26263,26264,26265,26266,
  79580. 26267,26268,26269,26270,26271,26272,26273,26274,26275,26276,26277,26278,
  79581. 26279,26280,26281,26282,26283,26284,26285,26286,26287,26288,26289,26290,
  79582. 26291,26292,26293,26294,26295,26296,26297,26298,26299,26300,26301,26302,
  79583. 26303,26304,26305,26306,26307,26308,26309,26310,26311,26312,26313,26314,
  79584. 26315,26316,26317,26318,26319,26320,26321,26322,26323,26324,26325,26326,
  79585. 26327,26328,26329,26330,26331,26332,26333,26334,26335,26336,26337,26338,
  79586. 26339,26340,26341,26342,26343,26344,26345,26346,26347,26348,26349,26350,
  79587. 26351,26352,26353,26354,26355,26356,26357,26358,26359,26360,26361,26362,
  79588. 26363,26364,26365,26366,26367,26368,26369,26370,26371,26372,26373,26374,
  79589. 26375,26376,26377,26378,26379,26380,26381,26382,26383,26384,26385,26386,
  79590. 26387,26388,26389,26390,26391,26392,26393,26394,26395,26396,26397,26398,
  79591. 26399,26400,26401,26402,26403,26404,26405,26406,26407,26408,26409,26410,
  79592. 26411,26412,26413,26414,26415,26416,26417,26418,26419,26420,26421,26422,
  79593. 26423,26424,26425,26426,26427,26428,26429,26430,26431,26432,26433,26434,
  79594. 26435,26436,26437,26438,26439,26440,26441,26442,26443,26444,26445,26446,
  79595. 26447,26448,26449,26450,26451,26452,26453,26454,26455,26456,26457,26458,
  79596. 26459,26460,26461,26462,26463,26464,26465,26466,26467,26468,26469,26470,
  79597. 26471,26472,26473,26474,26475,26476,26477,26478,26479,26480,26481,26482,
  79598. 26483,26484,26485,26486,26487,26488,26489,26490,26491,26492,26493,26494,
  79599. 26495,26496,26497,26498,26499,26500,26501,26502,26503,26504,26505,26506,
  79600. 26507,26508,26509,26510,26511,26512,26513,26514,26515,26516,26517,26518,
  79601. 26519,26520,26521,26522,26523,26524,26525,26526,26527,26528,26529,26530,
  79602. 26531,26532,26533,26534,26535,26536,26537,26538,26539,26540,26541,26542,
  79603. 26543,26544,26545,26546,26547,26548,26549,26550,26551,26552,26553,26554,
  79604. 26555,26556,26557,26558,26559,26560,26561,26562,26563,26564,26565,26566,
  79605. 26567,26568,26569,26570,26571,26572,26573,26574,26575,26576,26577,26578,
  79606. 26579,26580,26581,26582,26583,26584,26585,26586,26587,26588,26589,26590,
  79607. 26591,26592,26593,26594,26595,26596,26597,26598,26599,26600,26601,26602,
  79608. 26603,26604,26605,26606,26607,26608,26609,26610,26611,26612,26613,26614,
  79609. 26615,26616,26617,26618,26619,26620,26621,26622,26623,26624,26625,26626,
  79610. 26627,26628,26629,26630,26631,26632,26633,26634,26635,26636,26637,26638,
  79611. 26639,26640,26641,26642,26643,26644,26645,26646,26647,26648,26649,26650,
  79612. 26651,26652,26653,26654,26655,26656,26657,26658,26659,26660,26661,26662,
  79613. 26663,26664,26665,26666,26667,26668,26669,26670,26671,26672,26673,26674,
  79614. 26675,26676,26677,26678,26679,26680,26681,26682,26683,26684,26685,26686,
  79615. 26687,26688,26689,26690,26691,26692,26693,26694,26695,26696,26697,26698,
  79616. 26699,26700,26701,26702,26703,26704,26705,26706,26707,26708,26709,26710,
  79617. 26711,26712,26713,26714,26715,26716,26717,26718,26719,26720,26721,26722,
  79618. 26723,26724,26725,26726,26727,26728,26729,26730,26731,26732,26733,26734,
  79619. 26735,26736,26737,26738,26739,26740,26741,26742,26743,26744,26745,26746,
  79620. 26747,26748,26749,26750,26751,26752,26753,26754,26755,26756,26757,26758,
  79621. 26759,26760,26761,26762,26763,26764,26765,26766,26767,26768,26769,26770,
  79622. 26771,26772,26773,26774,26775,26776,26777,26778,26779,26780,26781,26782,
  79623. 26783,26784,26785,26786,26787,26788,26789,26790,26791,26792,26793,26794,
  79624. 26795,26796,26797,26798,26799,26800,26801,26802,26803,26804,26805,26806,
  79625. 26807,26808,26809,26810,26811,26812,26813,26814,26815,26816,26817,26818,
  79626. 26819,26820,26821,26822,26823,26824,26825,26826,26827,26828,26829,26830,
  79627. 26831,26832,26833,26834,26835,26836,26837,26838,26839,26840,26841,26842,
  79628. 26843,26844,26845,26846,26847,26848,26849,26850,26851,26852,26853,26854,
  79629. 26855,26856,26857,26858,26859,26860,26861,26862,26863,26864,26865,26866,
  79630. 26867,26868,26869,26870,26871,26872,26873,26874,26875,26876,26877,26878,
  79631. 26879,26880,26881,26882,26883,26884,26885,26886,26887,26888,26889,26890,
  79632. 26891,26892,26893,26894,26895,26896,26897,26898,26899,26900,26901,26902,
  79633. 26903,26904,26905,26906,26907,26908,26909,26910,26911,26912,26913,26914,
  79634. 26915,26916,26917,26918,26919,26920,26921,26922,26923,26924,26925,26926,
  79635. 26927,26928,26929,26930,26931,26932,26933,26934,26935,26936,26937,26938,
  79636. 26939,26940,26941,26942,26943,26944,26945,26946,26947,26948,26949,26950,
  79637. 26951,26952,26953,26954,26955,26956,26957,26958,26959,26960,26961,26962,
  79638. 26963,26964,26965,26966,26967,26968,26969,26970,26971,26972,26973,26974,
  79639. 26975,26976,26977,26978,26979,26980,26981,26982,26983,26984,26985,26986,
  79640. 26987,26988,26989,26990,26991,26992,26993,26994,26995,26996,26997,26998,
  79641. 26999,27000,27001,27002,27003,27004,27005,27006,27007,27008,27009,27010,
  79642. 27011,27012,27013,27014,27015,27016,27017,27018,27019,27020,27021,27022,
  79643. 27023,27024,27025,27026,27027,27028,27029,27030,27031,27032,27033,27034,
  79644. 27035,27036,27037,27038,27039,27040,27041,27042,27043,27044,27045,27046,
  79645. 27047,27048,27049,27050,27051,27052,27053,27054,27055,27056,27057,27058,
  79646. 27059,27060,27061,27062,27063,27064,27065,27066,27067,27068,27069,27070,
  79647. 27071,27072,27073,27074,27075,27076,27077,27078,27079,27080,27081,27082,
  79648. 27083,27084,27085,27086,27087,27088,27089,27090,27091,27092,27093,27094,
  79649. 27095,27096,27097,27098,27099,27100,27101,27102,27103,27104,27105,27106,
  79650. 27107,27108,27109,27110,27111,27112,27113,27114,27115,27116,27117,27118,
  79651. 27119,27120,27121,27122,27123,27124,27125,27126,27127,27128,27129,27130,
  79652. 27131,27132,27133,27134,27135,27136,27137,27138,27139,27140,27141,27142,
  79653. 27143,27144,27145,27146,27147,27148,27149,27150,27151,27152,27153,27154,
  79654. 27155,27156,27157,27158,27159,27160,27161,27162,27163,27164,27165,27166,
  79655. 27167,27168,27169,27170,27171,27172,27173,27174,27175,27176,27177,27178,
  79656. 27179,27180,27181,27182,27183,27184,27185,27186,27187,27188,27189,27190,
  79657. 27191,27192,27193,27194,27195,27196,27197,27198,27199,27200,27201,27202,
  79658. 27203,27204,27205,27206,27207,27208,27209,27210,27211,27212,27213,27214,
  79659. 27215,27216,27217,27218,27219,27220,27221,27222,27223,27224,27225,27226,
  79660. 27227,27228,27229,27230,27231,27232,27233,27234,27235,27236,27237,27238,
  79661. 27239,27240,27241,27242,27243,27244,27245,27246,27247,27248,27249,27250,
  79662. 27251,27252,27253,27254,27255,27256,27257,27258,27259,27260,27261,27262,
  79663. 27263,27264,27265,27266,27267,27268,27269,27270,27271,27272,27273,27274,
  79664. 27275,27276,27277,27278,27279,27280,27281,27282,27283,27284,27285,27286,
  79665. 27287,27288,27289,27290,27291,27292,27293,27294,27295,27296,27297,27298,
  79666. 27299,27300,27301,27302,27303,27304,27305,27306,27307,27308,27309,27310,
  79667. 27311,27312,27313,27314,27315,27316,27317,27318,27319,27320,27321,27322,
  79668. 27323,27324,27325,27326,27327,27328,27329,27330,27331,27332,27333,27334,
  79669. 27335,27336,27337,27338,27339,27340,27341,27342,27343,27344,27345,27346,
  79670. 27347,27348,27349,27350,27351,27352,27353,27354,27355,27356,27357,27358,
  79671. 27359,27360,27361,27362,27363,27364,27365,27366,27367,27368,27369,27370,
  79672. 27371,27372,27373,27374,27375,27376,27377,27378,27379,27380,27381,27382,
  79673. 27383,27384,27385,27386,27387,27388,27389,27390,27391,27392,27393,27394,
  79674. 27395,27396,27397,27398,27399,27400,27401,27402,27403,27404,27405,27406,
  79675. 27407,27408,27409,27410,27411,27412,27413,27414,27415,27416,27417,27418,
  79676. 27419,27420,27421,27422,27423,27424,27425,27426,27427,27428,27429,27430,
  79677. 27431,27432,27433,27434,27435,27436,27437,27438,27439,27440,27441,27442,
  79678. 27443,27444,27445,27446,27447,27448,27449,27450,27451,27452,27453,27454,
  79679. 27455,27456,27457,27458,27459,27460,27461,27462,27463,27464,27465,27466,
  79680. 27467,27468,27469,27470,27471,27472,27473,27474,27475,27476,27477,27478,
  79681. 27479,27480,27481,27482,27483,27484,27485,27486,27487,27488,27489,27490,
  79682. 27491,27492,27493,27494,27495,27496,27497,27498,27499,27500,27501,27502,
  79683. 27503,27504,27505,27506,27507,27508,27509,27510,27511,27512,27513,27514,
  79684. 27515,27516,27517,27518,27519,27520,27521,27522,27523,27524,27525,27526,
  79685. 27527,27528,27529,27530,27531,27532,27533,27534,27535,27536,27537,27538,
  79686. 27539,27540,27541,27542,27543,27544,27545,27546,27547,27548,27549,27550,
  79687. 27551,27552,27553,27554,27555,27556,27557,27558,27559,27560,27561,27562,
  79688. 27563,27564,27565,27566,27567,27568,27569,27570,27571,27572,27573,27574,
  79689. 27575,27576,27577,27578,27579,27580,27581,27582,27583,27584,27585,27586,
  79690. 27587,27588,27589,27590,27591,27592,27593,27594,27595,27596,27597,27598,
  79691. 27599,27600,27601,27602,27603,27604,27605,27606,27607,27608,27609,27610,
  79692. 27611,27612,27613,27614,27615,27616,27617,27618,27619,27620,27621,27622,
  79693. 27623,27624,27625,27626,27627,27628,27629,27630,27631,27632,27633,27634,
  79694. 27635,27636,27637,27638,27639,27640,27641,27642,27643,27644,27645,27646,
  79695. 27647,27648,27649,27650,27651,27652,27653,27654,27655,27656,27657,27658,
  79696. 27659,27660,27661,27662,27663,27664,27665,27666,27667,27668,27669,27670,
  79697. 27671,27672,27673,27674,27675,27676,27677,27678,27679,27680,27681,27682,
  79698. 27683,27684,27685,27686,27687,27688,27689,27690,27691,27692,27693,27694,
  79699. 27695,27696,27697,27698,27699,27700,27701,27702,27703,27704,27705,27706,
  79700. 27707,27708,27709,27710,27711,27712,27713,27714,27715,27716,27717,27718,
  79701. 27719,27720,27721,27722,27723,27724,27725,27726,27727,27728,27729,27730,
  79702. 27731,27732,27733,27734,27735,27736,27737,27738,27739,27740,27741,27742,
  79703. 27743,27744,27745,27746,27747,27748,27749,27750,27751,27752,27753,27754,
  79704. 27755,27756,27757,27758,27759,27760,27761,27762,27763,27764,27765,27766,
  79705. 27767,27768,27769,27770,27771,27772,27773,27774,27775,27776,27777,27778,
  79706. 27779,27780,27781,27782,27783,27784,27785,27786,27787,27788,27789,27790,
  79707. 27791,27792,27793,27794,27795,27796,27797,27798,27799,27800,27801,27802,
  79708. 27803,27804,27805,27806,27807,27808,27809,27810,27811,27812,27813,27814,
  79709. 27815,27816,27817,27818,27819,27820,27821,27822,27823,27824,27825,27826,
  79710. 27827,27828,27829,27830,27831,27832,27833,27834,27835,27836,27837,27838,
  79711. 27839,27840,27841,27842,27843,27844,27845,27846,27847,27848,27849,27850,
  79712. 27851,27852,27853,27854,27855,27856,27857,27858,27859,27860,27861,27862,
  79713. 27863,27864,27865,27866,27867,27868,27869,27870,27871,27872,27873,27874,
  79714. 27875,27876,27877,27878,27879,27880,27881,27882,27883,27884,27885,27886,
  79715. 27887,27888,27889,27890,27891,27892,27893,27894,27895,27896,27897,27898,
  79716. 27899,27900,27901,27902,27903,27904,27905,27906,27907,27908,27909,27910,
  79717. 27911,27912,27913,27914,27915,27916,27917,27918,27919,27920,27921,27922,
  79718. 27923,27924,27925,27926,27927,27928,27929,27930,27931,27932,27933,27934,
  79719. 27935,27936,27937,27938,27939,27940,27941,27942,27943,27944,27945,27946,
  79720. 27947,27948,27949,27950,27951,27952,27953,27954,27955,27956,27957,27958,
  79721. 27959,27960,27961,27962,27963,27964,27965,27966,27967,27968,27969,27970,
  79722. 27971,27972,27973,27974,27975,27976,27977,27978,27979,27980,27981,27982,
  79723. 27983,27984,27985,27986,27987,27988,27989,27990,27991,27992,27993,27994,
  79724. 27995,27996,27997,27998,27999,28000,28001,28002,28003,28004,28005,28006,
  79725. 28007,28008,28009,28010,28011,28012,28013,28014,28015,28016,28017,28018,
  79726. 28019,28020,28021,28022,28023,28024,28025,28026,28027,28028,28029,28030,
  79727. 28031,28032,28033,28034,28035,28036,28037,28038,28039,28040,28041,28042,
  79728. 28043,28044,28045,28046,28047,28048,28049,28050,28051,28052,28053,28054,
  79729. 28055,28056,28057,28058,28059,28060,28061,28062,28063,28064,28065,28066,
  79730. 28067,28068,28069,28070,28071,28072,28073,28074,28075,28076,28077,28078,
  79731. 28079,28080,28081,28082,28083,28084,28085,28086,28087,28088,28089,28090,
  79732. 28091,28092,28093,28094,28095,28096,28097,28098,28099,28100,28101,28102,
  79733. 28103,28104,28105,28106,28107,28108,28109,28110,28111,28112,28113,28114,
  79734. 28115,28116,28117,28118,28119,28120,28121,28122,28123,28124,28125,28126,
  79735. 28127,28128,28129,28130,28131,28132,28133,28134,28135,28136,28137,28138,
  79736. 28139,28140,28141,28142,28143,28144,28145,28146,28147,28148,28149,28150,
  79737. 28151,28152,28153,28154,28155,28156,28157,28158,28159,28160,28161,28162,
  79738. 28163,28164,28165,28166,28167,28168,28169,28170,28171,28172,28173,28174,
  79739. 28175,28176,28177,28178,28179,28180,28181,28182,28183,28184,28185,28186,
  79740. 28187,28188,28189,28190,28191,28192,28193,28194,28195,28196,28197,28198,
  79741. 28199,28200,28201,28202,28203,28204,28205,28206,28207,28208,28209,28210,
  79742. 28211,28212,28213,28214,28215,28216,28217,28218,28219,28220,28221,28222,
  79743. 28223,28224,28225,28226,28227,28228,28229,28230,28231,28232,28233,28234,
  79744. 28235,28236,28237,28238,28239,28240,28241,28242,28243,28244,28245,28246,
  79745. 28247,28248,28249,28250,28251,28252,28253,28254,28255,28256,28257,28258,
  79746. 28259,28260,28261,28262,28263,28264,28265,28266,28267,28268,28269,28270,
  79747. 28271,28272,28273,28274,28275,28276,28277,28278,28279,28280,28281,28282,
  79748. 28283,28284,28285,28286,28287,28288,28289,28290,28291,28292,28293,28294,
  79749. 28295,28296,28297,28298,28299,28300,28301,28302,28303,28304,28305,28306,
  79750. 28307,28308,28309,28310,28311,28312,28313,28314,28315,28316,28317,28318,
  79751. 28319,28320,28321,28322,28323,28324,28325,28326,28327,28328,28329,28330,
  79752. 28331,28332,28333,28334,28335,28336,28337,28338,28339,28340,28341,28342,
  79753. 28343,28344,28345,28346,28347,28348,28349,28350,28351,28352,28353,28354,
  79754. 28355,28356,28357,28358,28359,28360,28361,28362,28363,28364,28365,28366,
  79755. 28367,28368,28369,28370,28371,28372,28373,28374,28375,28376,28377,28378,
  79756. 28379,28380,28381,28382,28383,28384,28385,28386,28387,28388,28389,28390,
  79757. 28391,28392,28393,28394,28395,28396,28397,28398,28399,28400,28401,28402,
  79758. 28403,28404,28405,28406,28407,28408,28409,28410,28411,28412,28413,28414,
  79759. 28415,28416,28417,28418,28419,28420,28421,28422,28423,28424,28425,28426,
  79760. 28427,28428,28429,28430,28431,28432,28433,28434,28435,28436,28437,28438,
  79761. 28439,28440,28441,28442,28443,28444,28445,28446,28447,28448,28449,28450,
  79762. 28451,28452,28453,28454,28455,28456,28457,28458,28459,28460,28461,28462,
  79763. 28463,28464,28465,28466,28467,28468,28469,28470,28471,28472,28473,28474,
  79764. 28475,28476,28477,28478,28479,28480,28481,28482,28483,28484,28485,28486,
  79765. 28487,28488,28489,28490,28491,28492,28493,28494,28495,28496,28497,28498,
  79766. 28499,28500,28501,28502,28503,28504,28505,28506,28507,28508,28509,28510,
  79767. 28511,28512,28513,28514,28515,28516,28517,28518,28519,28520,28521,28522,
  79768. 28523,28524,28525,28526,28527,28528,28529,28530,28531,28532,28533,28534,
  79769. 28535,28536,28537,28538,28539,28540,28541,28542,28543,28544,28545,28546,
  79770. 28547,28548,28549,28550,28551,28552,28553,28554,28555,28556,28557,28558,
  79771. 28559,28560,28561,28562,28563,28564,28565,28566,28567,28568,28569,28570,
  79772. 28571,28572,28573,28574,28575,28576,28577,28578,28579,28580,28581,28582,
  79773. 28583,28584,28585,28586,28587,28588,28589,28590,28591,28592,28593,28594,
  79774. 28595,28596,28597,28598,28599,28600,28601,28602,28603,28604,28605,28606,
  79775. 28607,28608,28609,28610,28611,28612,28613,28614,28615,28616,28617,28618,
  79776. 28619,28620,28621,28622,28623,28624,28625,28626,28627,28628,28629,28630,
  79777. 28631,28632,28633,28634,28635,28636,28637,28638,28639,28640,28641,28642,
  79778. 28643,28644,28645,28646,28647,28648,28649,28650,28651,28652,28653,28654,
  79779. 28655,28656,28657,28658,28659,28660,28661,28662,28663,28664,28665,28666,
  79780. 28667,28668,28669,28670,28671,28672,28673,28674,28675,28676,28677,28678,
  79781. 28679,28680,28681,28682,28683,28684,28685,28686,28687,28688,28689,28690,
  79782. 28691,28692,28693,28694,28695,28696,28697,28698,28699,28700,28701,28702,
  79783. 28703,28704,28705,28706,28707,28708,28709,28710,28711,28712,28713,28714,
  79784. 28715,28716,28717,28718,28719,28720,28721,28722,28723,28724,28725,28726,
  79785. 28727,28728,28729,28730,28731,28732,28733,28734,28735,28736,28737,28738,
  79786. 28739,28740,28741,28742,28743,28744,28745,28746,28747,28748,28749,28750,
  79787. 28751,28752,28753,28754,28755,28756,28757,28758,28759,28760,28761,28762,
  79788. 28763,28764,28765,28766,28767,28768,28769,28770,28771,28772,28773,28774,
  79789. 28775,28776,28777,28778,28779,28780,28781,28782,28783,28784,28785,28786,
  79790. 28787,28788,28789,28790,28791,28792,28793,28794,28795,28796,28797,28798,
  79791. 28799,28800,28801,28802,28803,28804,28805,28806,28807,28808,28809,28810,
  79792. 28811,28812,28813,28814,28815,28816,28817,28818,28819,28820,28821,28822,
  79793. 28823,28824,28825,28826,28827,28828,28829,28830,28831,28832,28833,28834,
  79794. 28835,28836,28837,28838,28839,28840,28841,28842,28843,28844,28845,28846,
  79795. 28847,28848,28849,28850,28851,28852,28853,28854,28855,28856,28857,28858,
  79796. 28859,28860,28861,28862,28863,28864,28865,28866,28867,28868,28869,28870,
  79797. 28871,28872,28873,28874,28875,28876,28877,28878,28879,28880,28881,28882,
  79798. 28883,28884,28885,28886,28887,28888,28889,28890,28891,28892,28893,28894,
  79799. 28895,28896,28897,28898,28899,28900,28901,28902,28903,28904,28905,28906,
  79800. 28907,28908,28909,28910,28911,28912,28913,28914,28915,28916,28917,28918,
  79801. 28919,28920,28921,28922,28923,28924,28925,28926,28927,28928,28929,28930,
  79802. 28931,28932,28933,28934,28935,28936,28937,28938,28939,28940,28941,28942,
  79803. 28943,28944,28945,28946,28947,28948,28949,28950,28951,28952,28953,28954,
  79804. 28955,28956,28957,28958,28959,28960,28961,28962,28963,28964,28965,28966,
  79805. 28967,28968,28969,28970,28971,28972,28973,28974,28975,28976,28977,28978,
  79806. 28979,28980,28981,28982,28983,28984,28985,28986,28987,28988,28989,28990,
  79807. 28991,28992,28993,28994,28995,28996,28997,28998,28999,29000,29001,29002,
  79808. 29003,29004,29005,29006,29007,29008,29009,29010,29011,29012,29013,29014,
  79809. 29015,29016,29017,29018,29019,29020,29021,29022,29023,29024,29025,29026,
  79810. 29027,29028,29029,29030,29031,29032,29033,29034,29035,29036,29037,29038,
  79811. 29039,29040,29041,29042,29043,29044,29045,29046,29047,29048,29049,29050,
  79812. 29051,29052,29053,29054,29055,29056,29057,29058,29059,29060,29061,29062,
  79813. 29063,29064,29065,29066,29067,29068,29069,29070,29071,29072,29073,29074,
  79814. 29075,29076,29077,29078,29079,29080,29081,29082,29083,29084,29085,29086,
  79815. 29087,29088,29089,29090,29091,29092,29093,29094,29095,29096,29097,29098,
  79816. 29099,29100,29101,29102,29103,29104,29105,29106,29107,29108,29109,29110,
  79817. 29111,29112,29113,29114,29115,29116,29117,29118,29119,29120,29121,29122,
  79818. 29123,29124,29125,29126,29127,29128,29129,29130,29131,29132,29133,29134,
  79819. 29135,29136,29137,29138,29139,29140,29141,29142,29143,29144,29145,29146,
  79820. 29147,29148,29149,29150,29151,29152,29153,29154,29155,29156,29157,29158,
  79821. 29159,29160,29161,29162,29163,29164,29165,29166,29167,29168,29169,29170,
  79822. 29171,29172,29173,29174,29175,29176,29177,29178,29179,29180,29181,29182,
  79823. 29183,29184,29185,29186,29187,29188,29189,29190,29191,29192,29193,29194,
  79824. 29195,29196,29197,29198,29199,29200,29201,29202,29203,29204,29205,29206,
  79825. 29207,29208,29209,29210,29211,29212,29213,29214,29215,29216,29217,29218,
  79826. 29219,29220,29221,29222,29223,29224,29225,29226,29227,29228,29229,29230,
  79827. 29231,29232,29233,29234,29235,29236,29237,29238,29239,29240,29241,29242,
  79828. 29243,29244,29245,29246,29247,29248,29249,29250,29251,29252,29253,29254,
  79829. 29255,29256,29257,29258,29259,29260,29261,29262,29263,29264,29265,29266,
  79830. 29267,29268,29269,29270,29271,29272,29273,29274,29275,29276,29277,29278,
  79831. 29279,29280,29281,29282,29283,29284,29285,29286,29287,29288,29289,29290,
  79832. 29291,29292,29293,29294,29295,29296,29297,29298,29299,29300,29301,29302,
  79833. 29303,29304,29305,29306,29307,29308,29309,29310,29311,29312,29313,29314,
  79834. 29315,29316,29317,29318,29319,29320,29321,29322,29323,29324,29325,29326,
  79835. 29327,29328,29329,29330,29331,29332,29333,29334,29335,29336,29337,29338,
  79836. 29339,29340,29341,29342,29343,29344,29345,29346,29347,29348,29349,29350,
  79837. 29351,29352,29353,29354,29355,29356,29357,29358,29359,29360,29361,29362,
  79838. 29363,29364,29365,29366,29367,29368,29369,29370,29371,29372,29373,29374,
  79839. 29375,29376,29377,29378,29379,29380,29381,29382,29383,29384,29385,29386,
  79840. 29387,29388,29389,29390,29391,29392,29393,29394,29395,29396,29397,29398,
  79841. 29399,29400,29401,29402,29403,29404,29405,29406,29407,29408,29409,29410,
  79842. 29411,29412,29413,29414,29415,29416,29417,29418,29419,29420,29421,29422,
  79843. 29423,29424,29425,29426,29427,29428,29429,29430,29431,29432,29433,29434,
  79844. 29435,29436,29437,29438,29439,29440,29441,29442,29443,29444,29445,29446,
  79845. 29447,29448,29449,29450,29451,29452,29453,29454,29455,29456,29457,29458,
  79846. 29459,29460,29461,29462,29463,29464,29465,29466,29467,29468,29469,29470,
  79847. 29471,29472,29473,29474,29475,29476,29477,29478,29479,29480,29481,29482,
  79848. 29483,29484,29485,29486,29487,29488,29489,29490,29491,29492,29493,29494,
  79849. 29495,29496,29497,29498,29499,29500,29501,29502,29503,29504,29505,29506,
  79850. 29507,29508,29509,29510,29511,29512,29513,29514,29515,29516,29517,29518,
  79851. 29519,29520,29521,29522,29523,29524,29525,29526,29527,29528,29529,29530,
  79852. 29531,29532,29533,29534,29535,29536,29537,29538,29539,29540,29541,29542,
  79853. 29543,29544,29545,29546,29547,29548,29549,29550,29551,29552,29553,29554,
  79854. 29555,29556,29557,29558,29559,29560,29561,29562,29563,29564,29565,29566,
  79855. 29567,29568,29569,29570,29571,29572,29573,29574,29575,29576,29577,29578,
  79856. 29579,29580,29581,29582,29583,29584,29585,29586,29587,29588,29589,29590,
  79857. 29591,29592,29593,29594,29595,29596,29597,29598,29599,29600,29601,29602,
  79858. 29603,29604,29605,29606,29607,29608,29609,29610,29611,29612,29613,29614,
  79859. 29615,29616,29617,29618,29619,29620,29621,29622,29623,29624,29625,29626,
  79860. 29627,29628,29629,29630,29631,29632,29633,29634,29635,29636,29637,29638,
  79861. 29639,29640,29641,29642,29643,29644,29645,29646,29647,29648,29649,29650,
  79862. 29651,29652,29653,29654,29655,29656,29657,29658,29659,29660,29661,29662,
  79863. 29663,29664,29665,29666,29667,29668,29669,29670,29671,29672,29673,29674,
  79864. 29675,29676,29677,29678,29679,29680,29681,29682,29683,29684,29685,29686,
  79865. 29687,29688,29689,29690,29691,29692,29693,29694,29695,29696,29697,29698,
  79866. 29699,29700,29701,29702,29703,29704,29705,29706,29707,29708,29709,29710,
  79867. 29711,29712,29713,29714,29715,29716,29717,29718,29719,29720,29721,29722,
  79868. 29723,29724,29725,29726,29727,29728,29729,29730,29731,29732,29733,29734,
  79869. 29735,29736,29737,29738,29739,29740,29741,29742,29743,29744,29745,29746,
  79870. 29747,29748,29749,29750,29751,29752,29753,29754,29755,29756,29757,29758,
  79871. 29759,29760,29761,29762,29763,29764,29765,29766,29767,29768,29769,29770,
  79872. 29771,29772,29773,29774,29775,29776,29777,29778,29779,29780,29781,29782,
  79873. 29783,29784,29785,29786,29787,29788,29789,29790,29791,29792,29793,29794,
  79874. 29795,29796,29797,29798,29799,29800,29801,29802,29803,29804,29805,29806,
  79875. 29807,29808,29809,29810,29811,29812,29813,29814,29815,29816,29817,29818,
  79876. 29819,29820,29821,29822,29823,29824,29825,29826,29827,29828,29829,29830,
  79877. 29831,29832,29833,29834,29835,29836,29837,29838,29839,29840,29841,29842,
  79878. 29843,29844,29845,29846,29847,29848,29849,29850,29851,29852,29853,29854,
  79879. 29855,29856,29857,29858,29859,29860,29861,29862,29863,29864,29865,29866,
  79880. 29867,29868,29869,29870,29871,29872,29873,29874,29875,29876,29877,29878,
  79881. 29879,29880,29881,29882,29883,29884,29885,29886,29887,29888,29889,29890,
  79882. 29891,29892,29893,29894,29895,29896,29897,29898,29899,29900,29901,29902,
  79883. 29903,29904,29905,29906,29907,29908,29909,29910,29911,29912,29913,29914,
  79884. 29915,29916,29917,29918,29919,29920,29921,29922,29923,29924,29925,29926,
  79885. 29927,29928,29929,29930,29931,29932,29933,29934,29935,29936,29937,29938,
  79886. 29939,29940,29941,29942,29943,29944,29945,29946,29947,29948,29949,29950,
  79887. 29951,29952,29953,29954,29955,29956,29957,29958,29959,29960,29961,29962,
  79888. 29963,29964,29965,29966,29967,29968,29969,29970,29971,29972,29973,29974,
  79889. 29975,29976,29977,29978,29979,29980,29981,29982,29983,29984,29985,29986,
  79890. 29987,29988,29989,29990,29991,29992,29993,29994,29995,29996,29997,29998,
  79891. 29999,30000,30001,30002,30003,30004,30005,30006,30007,30008,30009,30010,
  79892. 30011,30012,30013,30014,30015,30016,30017,30018,30019,30020,30021,30022,
  79893. 30023,30024,30025,30026,30027,30028,30029,30030,30031,30032,30033,30034,
  79894. 30035,30036,30037,30038,30039,30040,30041,30042,30043,30044,30045,30046,
  79895. 30047,30048,30049,30050,30051,30052,30053,30054,30055,30056,30057,30058,
  79896. 30059,30060,30061,30062,30063,30064,30065,30066,30067,30068,30069,30070,
  79897. 30071,30072,30073,30074,30075,30076,30077,30078,30079,30080,30081,30082,
  79898. 30083,30084,30085,30086,30087,30088,30089,30090,30091,30092,30093,30094,
  79899. 30095,30096,30097,30098,30099,30100,30101,30102,30103,30104,30105,30106,
  79900. 30107,30108,30109,30110,30111,30112,30113,30114,30115,30116,30117,30118,
  79901. 30119,30120,30121,30122,30123,30124,30125,30126,30127,30128,30129,30130,
  79902. 30131,30132,30133,30134,30135,30136,30137,30138,30139,30140,30141,30142,
  79903. 30143,30144,30145,30146,30147,30148,30149,30150,30151,30152,30153,30154,
  79904. 30155,30156,30157,30158,30159,30160,30161,30162,30163,30164,30165,30166,
  79905. 30167,30168,30169,30170,30171,30172,30173,30174,30175,30176,30177,30178,
  79906. 30179,30180,30181,30182,30183,30184,30185,30186,30187,30188,30189,30190,
  79907. 30191,30192,30193,30194,30195,30196,30197,30198,30199,30200,30201,30202,
  79908. 30203,30204,30205,30206,30207,30208,30209,30210,30211,30212,30213,30214,
  79909. 30215,30216,30217,30218,30219,30220,30221,30222,30223,30224,30225,30226,
  79910. 30227,30228,30229,30230,30231,30232,30233,30234,30235,30236,30237,30238,
  79911. 30239,30240,30241,30242,30243,30244,30245,30246,30247,30248,30249,30250,
  79912. 30251,30252,30253,30254,30255,30256,30257,30258,30259,30260,30261,30262,
  79913. 30263,30264,30265,30266,30267,30268,30269,30270,30271,30272,30273,30274,
  79914. 30275,30276,30277,30278,30279,30280,30281,30282,30283,30284,30285,30286,
  79915. 30287,30288,30289,30290,30291,30292,30293,30294,30295,30296,30297,30298,
  79916. 30299,30300,30301,30302,30303,30304,30305,30306,30307,30308,30309,30310,
  79917. 30311,30312,30313,30314,30315,30316,30317,30318,30319,30320,30321,30322,
  79918. 30323,30324,30325,30326,30327,30328,30329,30330,30331,30332,30333,30334,
  79919. 30335,30336,30337,30338,30339,30340,30341,30342,30343,30344,30345,30346,
  79920. 30347,30348,30349,30350,30351,30352,30353,30354,30355,30356,30357,30358,
  79921. 30359,30360,30361,30362,30363,30364,30365,30366,30367,30368,30369,30370,
  79922. 30371,30372,30373,30374,30375,30376,30377,30378,30379,30380,30381,30382,
  79923. 30383,30384,30385,30386,30387,30388,30389,30390,30391,30392,30393,30394,
  79924. 30395,30396,30397,30398,30399,30400,30401,30402,30403,30404,30405,30406,
  79925. 30407,30408,30409,30410,30411,30412,30413,30414,30415,30416,30417,30418,
  79926. 30419,30420,30421,30422,30423,30424,30425,30426,30427,30428,30429,30430,
  79927. 30431,30432,30433,30434,30435,30436,30437,30438,30439,30440,30441,30442,
  79928. 30443,30444,30445,30446,30447,30448,30449,30450,30451,30452,30453,30454,
  79929. 30455,30456,30457,30458,30459,30460,30461,30462,30463,30464,30465,30466,
  79930. 30467,30468,30469,30470,30471,30472,30473,30474,30475,30476,30477,30478,
  79931. 30479,30480,30481,30482,30483,30484,30485,30486,30487,30488,30489,30490,
  79932. 30491,30492,30493,30494,30495,30496,30497,30498,30499,30500,30501,30502,
  79933. 30503,30504,30505,30506,30507,30508,30509,30510,30511,30512,30513,30514,
  79934. 30515,30516,30517,30518,30519,30520,30521,30522,30523,30524,30525,30526,
  79935. 30527,30528,30529,30530,30531,30532,30533,30534,30535,30536,30537,30538,
  79936. 30539,30540,30541,30542,30543,30544,30545,30546,30547,30548,30549,30550,
  79937. 30551,30552,30553,30554,30555,30556,30557,30558,30559,30560,30561,30562,
  79938. 30563,30564,30565,30566,30567,30568,30569,30570,30571,30572,30573,30574,
  79939. 30575,30576,30577,30578,30579,30580,30581,30582,30583,30584,30585,30586,
  79940. 30587,30588,30589,30590,30591,30592,30593,30594,30595,30596,30597,30598,
  79941. 30599,30600,30601,30602,30603,30604,30605,30606,30607,30608,30609,30610,
  79942. 30611,30612,30613,30614,30615,30616,30617,30618,30619,30620,30621,30622,
  79943. 30623,30624,30625,30626,30627,30628,30629,30630,30631,30632,30633,30634,
  79944. 30635,30636,30637,30638,30639,30640,30641,30642,30643,30644,30645,30646,
  79945. 30647,30648,30649,30650,30651,30652,30653,30654,30655,30656,30657,30658,
  79946. 30659,30660,30661,30662,30663,30664,30665,30666,30667,30668,30669,30670,
  79947. 30671,30672,30673,30674,30675,30676,30677,30678,30679,30680,30681,30682,
  79948. 30683,30684,30685,30686,30687,30688,30689,30690,30691,30692,30693,30694,
  79949. 30695,30696,30697,30698,30699,30700,30701,30702,30703,30704,30705,30706,
  79950. 30707,30708,30709,30710,30711,30712,30713,30714,30715,30716,30717,30718,
  79951. 30719,30720,30721,30722,30723,30724,30725,30726,30727,30728,30729,30730,
  79952. 30731,30732,30733,30734,30735,30736,30737,30738,30739,30740,30741,30742,
  79953. 30743,30744,30745,30746,30747,30748,30749,30750,30751,30752,30753,30754,
  79954. 30755,30756,30757,30758,30759,30760,30761,30762,30763,30764,30765,30766,
  79955. 30767,30768,30769,30770,30771,30772,30773,30774,30775,30776,30777,30778,
  79956. 30779,30780,30781,30782,30783,30784,30785,30786,30787,30788,30789,30790,
  79957. 30791,30792,30793,30794,30795,30796,30797,30798,30799,30800,30801,30802,
  79958. 30803,30804,30805,30806,30807,30808,30809,30810,30811,30812,30813,30814,
  79959. 30815,30816,30817,30818,30819,30820,30821,30822,30823,30824,30825,30826,
  79960. 30827,30828,30829,30830,30831,30832,30833,30834,30835,30836,30837,30838,
  79961. 30839,30840,30841,30842,30843,30844,30845,30846,30847,30848,30849,30850,
  79962. 30851,30852,30853,30854,30855,30856,30857,30858,30859,30860,30861,30862,
  79963. 30863,30864,30865,30866,30867,30868,30869,30870,30871,30872,30873,30874,
  79964. 30875,30876,30877,30878,30879,30880,30881,30882,30883,30884,30885,30886,
  79965. 30887,30888,30889,30890,30891,30892,30893,30894,30895,30896,30897,30898,
  79966. 30899,30900,30901,30902,30903,30904,30905,30906,30907,30908,30909,30910,
  79967. 30911,30912,30913,30914,30915,30916,30917,30918,30919,30920,30921,30922,
  79968. 30923,30924,30925,30926,30927,30928,30929,30930,30931,30932,30933,30934,
  79969. 30935,30936,30937,30938,30939,30940,30941,30942,30943,30944,30945,30946,
  79970. 30947,30948,30949,30950,30951,30952,30953,30954,30955,30956,30957,30958,
  79971. 30959,30960,30961,30962,30963,30964,30965,30966,30967,30968,30969,30970,
  79972. 30971,30972,30973,30974,30975,30976,30977,30978,30979,30980,30981,30982,
  79973. 30983,30984,30985,30986,30987,30988,30989,30990,30991,30992,30993,30994,
  79974. 30995,30996,30997,30998,30999,31000,31001,31002,31003,31004,31005,31006,
  79975. 31007,31008,31009,31010,31011,31012,31013,31014,31015,31016,31017,31018,
  79976. 31019,31020,31021,31022,31023,31024,31025,31026,31027,31028,31029,31030,
  79977. 31031,31032,31033,31034,31035,31036,31037,31038,31039,31040,31041,31042,
  79978. 31043,31044,31045,31046,31047,31048,31049,31050,31051,31052,31053,31054,
  79979. 31055,31056,31057,31058,31059,31060,31061,31062,31063,31064,31065,31066,
  79980. 31067,31068,31069,31070,31071,31072,31073,31074,31075,31076,31077,31078,
  79981. 31079,31080,31081,31082,31083,31084,31085,31086,31087,31088,31089,31090,
  79982. 31091,31092,31093,31094,31095,31096,31097,31098,31099,31100,31101,31102,
  79983. 31103,31104,31105,31106,31107,31108,31109,31110,31111,31112,31113,31114,
  79984. 31115,31116,31117,31118,31119,31120,31121,31122,31123,31124,31125,31126,
  79985. 31127,31128,31129,31130,31131,31132,31133,31134,31135,31136,31137,31138,
  79986. 31139,31140,31141,31142,31143,31144,31145,31146,31147,31148,31149,31150,
  79987. 31151,31152,31153,31154,31155,31156,31157,31158,31159,31160,31161,31162,
  79988. 31163,31164,31165,31166,31167,31168,31169,31170,31171,31172,31173,31174,
  79989. 31175,31176,31177,31178,31179,31180,31181,31182,31183,31184,31185,31186,
  79990. 31187,31188,31189,31190,31191,31192,31193,31194,31195,31196,31197,31198,
  79991. 31199,31200,31201,31202,31203,31204,31205,31206,31207,31208,31209,31210,
  79992. 31211,31212,31213,31214,31215,31216,31217,31218,31219,31220,31221,31222,
  79993. 31223,31224,31225,31226,31227,31228,31229,31230,31231,31232,31233,31234,
  79994. 31235,31236,31237,31238,31239,31240,31241,31242,31243,31244,31245,31246,
  79995. 31247,31248,31249,31250,31251,31252,31253,31254,31255,31256,31257,31258,
  79996. 31259,31260,31261,31262,31263,31264,31265,31266,31267,31268,31269,31270,
  79997. 31271,31272,31273,31274,31275,31276,31277,31278,31279,31280,31281,31282,
  79998. 31283,31284,31285,31286,31287,31288,31289,31290,31291,31292,31293,31294,
  79999. 31295,31296,31297,31298,31299,31300,31301,31302,31303,31304,31305,31306,
  80000. 31307,31308,31309,31310,31311,31312,31313,31314,31315,31316,31317,31318,
  80001. 31319,31320,31321,31322,31323,31324,31325,31326,31327,31328,31329,31330,
  80002. 31331,31332,31333,31334,31335,31336,31337,31338,31339,31340,31341,31342,
  80003. 31343,31344,31345,31346,31347,31348,31349,31350,31351,31352,31353,31354,
  80004. 31355,31356,31357,31358,31359,31360,31361,31362,31363,31364,31365,31366,
  80005. 31367,31368,31369,31370,31371,31372,31373,31374,31375,31376,31377,31378,
  80006. 31379,31380,31381,31382,31383,31384,31385,31386,31387,31388,31389,31390,
  80007. 31391,31392,31393,31394,31395,31396,31397,31398,31399,31400,31401,31402,
  80008. 31403,31404,31405,31406,31407,31408,31409,31410,31411,31412,31413,31414,
  80009. 31415,31416,31417,31418,31419,31420,31421,31422,31423,31424,31425,31426,
  80010. 31427,31428,31429,31430,31431,31432,31433,31434,31435,31436,31437,31438,
  80011. 31439,31440,31441,31442,31443,31444,31445,31446,31447,31448,31449,31450,
  80012. 31451,31452,31453,31454,31455,31456,31457,31458,31459,31460,31461,31462,
  80013. 31463,31464,31465,31466,31467,31468,31469,31470,31471,31472,31473,31474,
  80014. 31475,31476,31477,31478,31479,31480,31481,31482,31483,31484,31485,31486,
  80015. 31487,31488,31489,31490,31491,31492,31493,31494,31495,31496,31497,31498,
  80016. 31499,31500,31501,31502,31503,31504,31505,31506,31507,31508,31509,31510,
  80017. 31511,31512,31513,31514,31515,31516,31517,31518,31519,31520,31521,31522,
  80018. 31523,31524,31525,31526,31527,31528,31529,31530,31531,31532,31533,31534,
  80019. 31535,31536,31537,31538,31539,31540,31541,31542,31543,31544,31545,31546,
  80020. 31547,31548,31549,31550,31551,31552,31553,31554,31555,31556,31557,31558,
  80021. 31559,31560,31561,31562,31563,31564,31565,31566,31567,31568,31569,31570,
  80022. 31571,31572,31573,31574,31575,31576,31577,31578,31579,31580,31581,31582,
  80023. 31583,31584,31585,31586,31587,31588,31589,31590,31591,31592,31593,31594,
  80024. 31595,31596,31597,31598,31599,31600,31601,31602,31603,31604,31605,31606,
  80025. 31607,31608,31609,31610,31611,31612,31613,31614,31615,31616,31617,31618,
  80026. 31619,31620,31621,31622,31623,31624,31625,31626,31627,31628,31629,31630,
  80027. 31631,31632,31633,31634,31635,31636,31637,31638,31639,31640,31641,31642,
  80028. 31643,31644,31645,31646,31647,31648,31649,31650,31651,31652,31653,31654,
  80029. 31655,31656,31657,31658,31659,31660,31661,31662,31663,31664,31665,31666,
  80030. 31667,31668,31669,31670,31671,31672,31673,31674,31675,31676,31677,31678,
  80031. 31679,31680,31681,31682,31683,31684,31685,31686,31687,31688,31689,31690,
  80032. 31691,31692,31693,31694,31695,31696,31697,31698,31699,31700,31701,31702,
  80033. 31703,31704,31705,31706,31707,31708,31709,31710,31711,31712,31713,31714,
  80034. 31715,31716,31717,31718,31719,31720,31721,31722,31723,31724,31725,31726,
  80035. 31727,31728,31729,31730,31731,31732,31733,31734,31735,31736,31737,31738,
  80036. 31739,31740,31741,31742,31743,31744,31745,31746,31747,31748,31749,31750,
  80037. 31751,31752,31753,31754,31755,31756,31757,31758,31759,31760,31761,31762,
  80038. 31763,31764,31765,31766,31767,31768,31769,31770,31771,31772,31773,31774,
  80039. 31775,31776,31777,31778,31779,31780,31781,31782,31783,31784,31785,31786,
  80040. 31787,31788,31789,31790,31791,31792,31793,31794,31795,31796,31797,31798,
  80041. 31799,31800,31801,31802,31803,31804,31805,31806,31807,31808,31809,31810,
  80042. 31811,31812,31813,31814,31815,31816,31817,31818,31819,31820,31821,31822,
  80043. 31823,31824,31825,31826,31827,31828,31829,31830,31831,31832,31833,31834,
  80044. 31835,31836,31837,31838,31839,31840,31841,31842,31843,31844,31845,31846,
  80045. 31847,31848,31849,31850,31851,31852,31853,31854,31855,31856,31857,31858,
  80046. 31859,31860,31861,31862,31863,31864,31865,31866,31867,31868,31869,31870,
  80047. 31871,31872,31873,31874,31875,31876,31877,31878,31879,31880,31881,31882,
  80048. 31883,31884,31885,31886,31887,31888,31889,31890,31891,31892,31893,31894,
  80049. 31895,31896,31897,31898,31899,31900,31901,31902,31903,31904,31905,31906,
  80050. 31907,31908,31909,31910,31911,31912,31913,31914,31915,31916,31917,31918,
  80051. 31919,31920,31921,31922,31923,31924,31925,31926,31927,31928,31929,31930,
  80052. 31931,31932,31933,31934,31935,31936,31937,31938,31939,31940,31941,31942,
  80053. 31943,31944,31945,31946,31947,31948,31949,31950,31951,31952,31953,31954,
  80054. 31955,31956,31957,31958,31959,31960,31961,31962,31963,31964,31965,31966,
  80055. 31967,31968,31969,31970,31971,31972,31973,31974,31975,31976,31977,31978,
  80056. 31979,31980,31981,31982,31983,31984,31985,31986,31987,31988,31989,31990,
  80057. 31991,31992,31993,31994,31995,31996,31997,31998,31999,32000,32001,32002,
  80058. 32003,32004,32005,32006,32007,32008,32009,32010,32011,32012,32013,32014,
  80059. 32015,32016,32017,32018,32019,32020,32021,32022,32023,32024,32025,32026,
  80060. 32027,32028,32029,32030,32031,32032,32033,32034,32035,32036,32037,32038,
  80061. 32039,32040,32041,32042,32043,32044,32045,32046,32047,32048,32049,32050,
  80062. 32051,32052,32053,32054,32055,32056,32057,32058,32059,32060,32061,32062,
  80063. 32063,32064,32065,32066,32067,32068,32069,32070,32071,32072,32073,32074,
  80064. 32075,32076,32077,32078,32079,32080,32081,32082,32083,32084,32085,32086,
  80065. 32087,32088,32089,32090,32091,32092,32093,32094,32095,32096,32097,32098,
  80066. 32099,32100,32101,32102,32103,32104,32105,32106,32107,32108,32109,32110,
  80067. 32111,32112,32113,32114,32115,32116,32117,32118,32119,32120,32121,32122,
  80068. 32123,32124,32125,32126,32127,32128,32129,32130,32131,32132,32133,32134,
  80069. 32135,32136,32137,32138,32139,32140,32141,32142,32143,32144,32145,32146,
  80070. 32147,32148,32149,32150,32151,32152,32153,32154,32155,32156,32157,32158,
  80071. 32159,32160,32161,32162,32163,32164,32165,32166,32167,32168,32169,32170,
  80072. 32171,32172,32173,32174,32175,32176,32177,32178,32179,32180,32181,32182,
  80073. 32183,32184,32185,32186,32187,32188,32189,32190,32191,32192,32193,32194,
  80074. 32195,32196,32197,32198,32199,32200,32201,32202,32203,32204,32205,32206,
  80075. 32207,32208,32209,32210,32211,32212,32213,32214,32215,32216,32217,32218,
  80076. 32219,32220,32221,32222,32223,32224,32225,32226,32227,32228,32229,32230,
  80077. 32231,32232,32233,32234,32235,32236,32237,32238,32239,32240,32241,32242,
  80078. 32243,32244,32245,32246,32247,32248,32249,32250,32251,32252,32253,32254,
  80079. 32255,32256,32257,32258,32259,32260,32261,32262,32263,32264,32265,32266,
  80080. 32267,32268,32269,32270,32271,32272,32273,32274,32275,32276,32277,32278,
  80081. 32279,32280,32281,32282,32283,32284,32285,32286,32287,32288,32289,32290,
  80082. 32291,32292,32293,32294,32295,32296,32297,32298,32299,32300,32301,32302,
  80083. 32303,32304,32305,32306,32307,32308,32309,32310,32311,32312,32313,32314,
  80084. 32315,32316,32317,32318,32319,32320,32321,32322,32323,32324,32325,32326,
  80085. 32327,32328,32329,32330,32331,32332,32333,32334,32335,32336,32337,32338,
  80086. 32339,32340,32341,32342,32343,32344,32345,32346,32347,32348,32349,32350,
  80087. 32351,32352,32353,32354,32355,32356,32357,32358,32359,32360,32361,32362,
  80088. 32363,32364,32365,32366,32367,32368,32369,32370,32371,32372,32373,32374,
  80089. 32375,32376,32377,32378,32379,32380,32381,32382,32383,32384,32385,32386,
  80090. 32387,32388,32389,32390,32391,32392,32393,32394,32395,32396,32397,32398,
  80091. 32399,32400,32401,32402,32403,32404,32405,32406,32407,32408,32409,32410,
  80092. 32411,32412,32413,32414,32415,32416,32417,32418,32419,32420,32421,32422,
  80093. 32423,32424,32425,32426,32427,32428,32429,32430,32431,32432,32433,32434,
  80094. 32435,32436,32437,32438,32439,32440,32441,32442,32443,32444,32445,32446,
  80095. 32447,32448,32449,32450,32451,32452,32453,32454,32455,32456,32457,32458,
  80096. 32459,32460,32461,32462,32463,32464,32465,32466,32467,32468,32469,32470,
  80097. 32471,32472,32473,32474,32475,32476,32477,32478,32479,32480,32481,32482,
  80098. 32483,32484,32485,32486,32487,32488,32489,32490,32491,32492,32493,32494,
  80099. 32495,32496,32497,32498,32499,32500,32501,32502,32503,32504,32505,32506,
  80100. 32507,32508,32509,32510,32511,32512,32513,32514,32515,32516,32517,32518,
  80101. 32519,32520,32521,32522,32523,32524,32525,32526,32527,32528,32529,32530,
  80102. 32531,32532,32533,32534,32535,32536,32537,32538,32539,32540,32541,32542,
  80103. 32543,32544,32545,32546,32547,32548,32549,32550,32551,32552,32553,32554,
  80104. 32555,32556,32557,32558,32559,32560,32561,32562,32563,32564,32565,32566,
  80105. 32567,32568,32569,32570,32571,32572,32573,32574,32575,32576,32577,32578,
  80106. 32579,32580,32581,32582,32583,32584,32585,32586,32587,32588,32589,32590,
  80107. 32591,32592,32593,32594,32595,32596,32597,32598,32599,32600,32601,32602,
  80108. 32603,32604,32605,32606,32607,32608,32609,32610,32611,32612,32613,32614,
  80109. 32615,32616,32617,32618,32619,32620,32621,32622,32623,32624,32625,32626,
  80110. 32627,32628,32629,32630,32631,32632,32633,32634,32635,32636,32637,32638,
  80111. 32639,32640,32641,32642,32643,32644,32645,32646,32647,32648,32649,32650,
  80112. 32651,32652,32653,32654,32655,32656,32657,32658,32659,32660,32661,32662,
  80113. 32663,32664,32665,32666,32667,32668,32669,32670,32671,32672,32673,32674,
  80114. 32675,32676,32677,32678,32679,32680,32681,32682,32683,32684,32685,32686,
  80115. 32687,32688,32689,32690,32691,32692,32693,32694,32695,32696,32697,32698,
  80116. 32699,32700,32701,32702,32703,32704,32705,32706,32707,32708,32709,32710,
  80117. 32711,32712,32713,32714,32715,32716,32717,32718,32719,32720,32721,32722,
  80118. 32723,32724,32725,32726,32727,32728,32729,32730,32731,32732,32733,32734,
  80119. 32735,32736,32737,32738,32739,32740,32741,32742,32743,32744,32745,32746,
  80120. 32747,32748,32749,32750,32751,32752,32753,32754,32755,32756,32757,32758,
  80121. 32759,32760,32761,32762,32763,32764,32765,32766,32767,32768L,32769L,32770L,
  80122. 32771L,32772L,32773L,32774L,32775L,32776L,32777L,32778L,32779L,32780L,
  80123. 32781L,32782L,32783L,32784L,32785L,32786L,32787L,32788L,32789L,32790L,
  80124. 32791L,32792L,32793L,32794L,32795L,32796L,32797L,32798L,32799L,32800L,
  80125. 32801L,32802L,32803L,32804L,32805L,32806L,32807L,32808L,32809L,32810L,
  80126. 32811L,32812L,32813L,32814L,32815L,32816L,32817L,32818L,32819L,32820L,
  80127. 32821L,32822L,32823L,32824L,32825L,32826L,32827L,32828L,32829L,32830L,
  80128. 32831L,32832L,32833L,32834L,32835L,32836L,32837L,32838L,32839L,32840L,
  80129. 32841L,32842L,32843L,32844L,32845L,32846L,32847L,32848L,32849L,32850L,
  80130. 32851L,32852L,32853L,32854L,32855L,32856L,32857L,32858L,32859L,32860L,
  80131. 32861L,32862L,32863L,32864L,32865L,32866L,32867L,32868L,32869L,32870L,
  80132. 32871L,32872L,32873L,32874L,32875L,32876L,32877L,32878L,32879L,32880L,
  80133. 32881L,32882L,32883L,32884L,32885L,32886L,32887L,32888L,32889L,32890L,
  80134. 32891L,32892L,32893L,32894L,32895L,32896L,32897L,32898L,32899L,32900L,
  80135. 32901L,32902L,32903L,32904L,32905L,32906L,32907L,32908L,32909L,32910L,
  80136. 32911L,32912L,32913L,32914L,32915L,32916L,32917L,32918L,32919L,32920L,
  80137. 32921L,32922L,32923L,32924L,32925L,32926L,32927L,32928L,32929L,32930L,
  80138. 32931L,32932L,32933L,32934L,32935L,32936L,32937L,32938L,32939L,32940L,
  80139. 32941L,32942L,32943L,32944L,32945L,32946L,32947L,32948L,32949L,32950L,
  80140. 32951L,32952L,32953L,32954L,32955L,32956L,32957L,32958L,32959L,32960L,
  80141. 32961L,32962L,32963L,32964L,32965L,32966L,32967L,32968L,32969L,32970L,
  80142. 32971L,32972L,32973L,32974L,32975L,32976L,32977L,32978L,32979L,32980L,
  80143. 32981L,32982L,32983L,32984L,32985L,32986L,32987L,32988L,32989L,32990L,
  80144. 32991L,32992L,32993L,32994L,32995L,32996L,32997L,32998L,32999L,33000L,
  80145. 33001L,33002L,33003L,33004L,33005L,33006L,33007L,33008L,33009L,33010L,
  80146. 33011L,33012L,33013L,33014L,33015L,33016L,33017L,33018L,33019L,33020L,
  80147. 33021L,33022L,33023L,33024L,33025L,33026L,33027L,33028L,33029L,33030L,
  80148. 33031L,33032L,33033L,33034L,33035L,33036L,33037L,33038L,33039L,33040L,
  80149. 33041L,33042L,33043L,33044L,33045L,33046L,33047L,33048L,33049L,33050L,
  80150. 33051L,33052L,33053L,33054L,33055L,33056L,33057L,33058L,33059L,33060L,
  80151. 33061L,33062L,33063L,33064L,33065L,33066L,33067L,33068L,33069L,33070L,
  80152. 33071L,33072L,33073L,33074L,33075L,33076L,33077L,33078L,33079L,33080L,
  80153. 33081L,33082L,33083L,33084L,33085L,33086L,33087L,33088L,33089L,33090L,
  80154. 33091L,33092L,33093L,33094L,33095L,33096L,33097L,33098L,33099L,33100L,
  80155. 33101L,33102L,33103L,33104L,33105L,33106L,33107L,33108L,33109L,33110L,
  80156. 33111L,33112L,33113L,33114L,33115L,33116L,33117L,33118L,33119L,33120L,
  80157. 33121L,33122L,33123L,33124L,33125L,33126L,33127L,33128L,33129L,33130L,
  80158. 33131L,33132L,33133L,33134L,33135L,33136L,33137L,33138L,33139L,33140L,
  80159. 33141L,33142L,33143L,33144L,33145L,33146L,33147L,33148L,33149L,33150L,
  80160. 33151L,33152L,33153L,33154L,33155L,33156L,33157L,33158L,33159L,33160L,
  80161. 33161L,33162L,33163L,33164L,33165L,33166L,33167L,33168L,33169L,33170L,
  80162. 33171L,33172L,33173L,33174L,33175L,33176L,33177L,33178L,33179L,33180L,
  80163. 33181L,33182L,33183L,33184L,33185L,33186L,33187L,33188L,33189L,33190L,
  80164. 33191L,33192L,33193L,33194L,33195L,33196L,33197L,33198L,33199L,33200L,
  80165. 33201L,33202L,33203L,33204L,33205L,33206L,33207L,33208L,33209L,33210L,
  80166. 33211L,33212L,33213L,33214L,33215L,33216L,33217L,33218L,33219L,33220L,
  80167. 33221L,33222L,33223L,33224L,33225L,33226L,33227L,33228L,33229L,33230L,
  80168. 33231L,33232L,33233L,33234L,33235L,33236L,33237L,33238L,33239L,33240L,
  80169. 33241L,33242L,33243L,33244L,33245L,33246L,33247L,33248L,33249L,33250L,
  80170. 33251L,33252L,33253L,33254L,33255L,33256L,33257L,33258L,33259L,33260L,
  80171. 33261L,33262L,33263L,33264L,33265L,33266L,33267L,33268L,33269L,33270L,
  80172. 33271L,33272L,33273L,33274L,33275L,33276L,33277L,33278L,33279L,33280L,
  80173. 33281L,33282L,33283L,33284L,33285L,33286L,33287L,33288L,33289L,33290L,
  80174. 33291L,33292L,33293L,33294L,33295L,33296L,33297L,33298L,33299L,33300L,
  80175. 33301L,33302L,33303L,33304L,33305L,33306L,33307L,33308L,33309L,33310L,
  80176. 33311L,33312L,33313L,33314L,33315L,33316L,33317L,33318L,33319L,33320L,
  80177. 33321L,33322L,33323L,33324L,33325L,33326L,33327L,33328L,33329L,33330L,
  80178. 33331L,33332L,33333L,33334L,33335L,33336L,33337L,33338L,33339L,33340L,
  80179. 33341L,33342L,33343L,33344L,33345L,33346L,33347L,33348L,33349L,33350L,
  80180. 33351L,33352L,33353L,33354L,33355L,33356L,33357L,33358L,33359L,33360L,
  80181. 33361L,33362L,33363L,33364L,33365L,33366L,33367L,33368L,33369L,33370L,
  80182. 33371L,33372L,33373L,33374L,33375L,33376L,33377L,33378L,33379L,33380L,
  80183. 33381L,33382L,33383L,33384L,33385L,33386L,33387L,33388L,33389L,33390L,
  80184. 33391L,33392L,33393L,33394L,33395L,33396L,33397L,33398L,33399L,33400L,
  80185. 33401L,33402L,33403L,33404L,33405L,33406L,33407L,33408L,33409L,33410L,
  80186. 33411L,33412L,33413L,33414L,33415L,33416L,33417L,33418L,33419L,33420L,
  80187. 33421L,33422L,33423L,33424L,33425L,33426L,33427L,33428L,33429L,33430L,
  80188. 33431L,33432L,33433L,33434L,33435L,33436L,33437L,33438L,33439L,33440L,
  80189. 33441L,33442L,33443L,33444L,33445L,33446L,33447L,33448L,33449L,33450L,
  80190. 33451L,33452L,33453L,33454L,33455L,33456L,33457L,33458L,33459L,33460L,
  80191. 33461L,33462L,33463L,33464L,33465L,33466L,33467L,33468L,33469L,33470L,
  80192. 33471L,33472L,33473L,33474L,33475L,33476L,33477L,33478L,33479L,33480L,
  80193. 33481L,33482L,33483L,33484L,33485L,33486L,33487L,33488L,33489L,33490L,
  80194. 33491L,33492L,33493L,33494L,33495L,33496L,33497L,33498L,33499L,33500L,
  80195. 33501L,33502L,33503L,33504L,33505L,33506L,33507L,33508L,33509L,33510L,
  80196. 33511L,33512L,33513L,33514L,33515L,33516L,33517L,33518L,33519L,33520L,
  80197. 33521L,33522L,33523L,33524L,33525L,33526L,33527L,33528L,33529L,33530L,
  80198. 33531L,33532L,33533L,33534L,33535L,33536L,33537L,33538L,33539L,33540L,
  80199. 33541L,33542L,33543L,33544L,33545L,33546L,33547L,33548L,33549L,33550L,
  80200. 33551L,33552L,33553L,33554L,33555L,33556L,33557L,33558L,33559L,33560L,
  80201. 33561L,33562L,33563L,33564L,33565L,33566L,33567L,33568L,33569L,33570L,
  80202. 33571L,33572L,33573L,33574L,33575L,33576L,33577L,33578L,33579L,33580L,
  80203. 33581L,33582L,33583L,33584L,33585L,33586L,33587L,33588L,33589L,33590L,
  80204. 33591L,33592L,33593L,33594L,33595L,33596L,33597L,33598L,33599L,33600L,
  80205. 33601L,33602L,33603L,33604L,33605L,33606L,33607L,33608L,33609L,33610L,
  80206. 33611L,33612L,33613L,33614L,33615L,33616L,33617L,33618L,33619L,33620L,
  80207. 33621L,33622L,33623L,33624L,33625L,33626L,33627L,33628L,33629L,33630L,
  80208. 33631L,33632L,33633L,33634L,33635L,33636L,33637L,33638L,33639L,33640L,
  80209. 33641L,33642L,33643L,33644L,33645L,33646L,33647L,33648L,33649L,33650L,
  80210. 33651L,33652L,33653L,33654L,33655L,33656L,33657L,33658L,33659L,33660L,
  80211. 33661L,33662L,33663L,33664L,33665L,33666L,33667L,33668L,33669L,33670L,
  80212. 33671L,33672L,33673L,33674L,33675L,33676L,33677L,33678L,33679L,33680L,
  80213. 33681L,33682L,33683L,33684L,33685L,33686L,33687L,33688L,33689L,33690L,
  80214. 33691L,33692L,33693L,33694L,33695L,33696L,33697L,33698L,33699L,33700L,
  80215. 33701L,33702L,33703L,33704L,33705L,33706L,33707L,33708L,33709L,33710L,
  80216. 33711L,33712L,33713L,33714L,33715L,33716L,33717L,33718L,33719L,33720L,
  80217. 33721L,33722L,33723L,33724L,33725L,33726L,33727L,33728L,33729L,33730L,
  80218. 33731L,33732L,33733L,33734L,33735L,33736L,33737L,33738L,33739L,33740L,
  80219. 33741L,33742L,33743L,33744L,33745L,33746L,33747L,33748L,33749L,33750L,
  80220. 33751L,33752L,33753L,33754L,33755L,33756L,33757L,33758L,33759L,33760L,
  80221. 33761L,33762L,33763L,33764L,33765L,33766L,33767L,33768L,33769L,33770L,
  80222. 33771L,33772L,33773L,33774L,33775L,33776L,33777L,33778L,33779L,33780L,
  80223. 33781L,33782L,33783L,33784L,33785L,33786L,33787L,33788L,33789L,33790L,
  80224. 33791L,33792L,33793L,33794L,33795L,33796L,33797L,33798L,33799L,33800L,
  80225. 33801L,33802L,33803L,33804L,33805L,33806L,33807L,33808L,33809L,33810L,
  80226. 33811L,33812L,33813L,33814L,33815L,33816L,33817L,33818L,33819L,33820L,
  80227. 33821L,33822L,33823L,33824L,33825L,33826L,33827L,33828L,33829L,33830L,
  80228. 33831L,33832L,33833L,33834L,33835L,33836L,33837L,33838L,33839L,33840L,
  80229. 33841L,33842L,33843L,33844L,33845L,33846L,33847L,33848L,33849L,33850L,
  80230. 33851L,33852L,33853L,33854L,33855L,33856L,33857L,33858L,33859L,33860L,
  80231. 33861L,33862L,33863L,33864L,33865L,33866L,33867L,33868L,33869L,33870L,
  80232. 33871L,33872L,33873L,33874L,33875L,33876L,33877L,33878L,33879L,33880L,
  80233. 33881L,33882L,33883L,33884L,33885L,33886L,33887L,33888L,33889L,33890L,
  80234. 33891L,33892L,33893L,33894L,33895L,33896L,33897L,33898L,33899L,33900L,
  80235. 33901L,33902L,33903L,33904L,33905L,33906L,33907L,33908L,33909L,33910L,
  80236. 33911L,33912L,33913L,33914L,33915L,33916L,33917L,33918L,33919L,33920L,
  80237. 33921L,33922L,33923L,33924L,33925L,33926L,33927L,33928L,33929L,33930L,
  80238. 33931L,33932L,33933L,33934L,33935L,33936L,33937L,33938L,33939L,33940L,
  80239. 33941L,33942L,33943L,33944L,33945L,33946L,33947L,33948L,33949L,33950L,
  80240. 33951L,33952L,33953L,33954L,33955L,33956L,33957L,33958L,33959L,33960L,
  80241. 33961L,33962L,33963L,33964L,33965L,33966L,33967L,33968L,33969L,33970L,
  80242. 33971L,33972L,33973L,33974L,33975L,33976L,33977L,33978L,33979L,33980L,
  80243. 33981L,33982L,33983L,33984L,33985L,33986L,33987L,33988L,33989L,33990L,
  80244. 33991L,33992L,33993L,33994L,33995L,33996L,33997L,33998L,33999L,34000L,
  80245. 34001L,34002L,34003L,34004L,34005L,34006L,34007L,34008L,34009L,34010L,
  80246. 34011L,34012L,34013L,34014L,34015L,34016L,34017L,34018L,34019L,34020L,
  80247. 34021L,34022L,34023L,34024L,34025L,34026L,34027L,34028L,34029L,34030L,
  80248. 34031L,34032L,34033L,34034L,34035L,34036L,34037L,34038L,34039L,34040L,
  80249. 34041L,34042L,34043L,34044L,34045L,34046L,34047L,34048L,34049L,34050L,
  80250. 34051L,34052L,34053L,34054L,34055L,34056L,34057L,34058L,34059L,34060L,
  80251. 34061L,34062L,34063L,34064L,34065L,34066L,34067L,34068L,34069L,34070L,
  80252. 34071L,34072L,34073L,34074L,34075L,34076L,34077L,34078L,34079L,34080L,
  80253. 34081L,34082L,34083L,34084L,34085L,34086L,34087L,34088L,34089L,34090L,
  80254. 34091L,34092L,34093L,34094L,34095L,34096L,34097L,34098L,34099L,34100L,
  80255. 34101L,34102L,34103L,34104L,34105L,34106L,34107L,34108L,34109L,34110L,
  80256. 34111L,34112L,34113L,34114L,34115L,34116L,34117L,34118L,34119L,34120L,
  80257. 34121L,34122L,34123L,34124L,34125L,34126L,34127L,34128L,34129L,34130L,
  80258. 34131L,34132L,34133L,34134L,34135L,34136L,34137L,34138L,34139L,34140L,
  80259. 34141L,34142L,34143L,34144L,34145L,34146L,34147L,34148L,34149L,34150L,
  80260. 34151L,34152L,34153L,34154L,34155L,34156L,34157L,34158L,34159L,34160L,
  80261. 34161L,34162L,34163L,34164L,34165L,34166L,34167L,34168L,34169L,34170L,
  80262. 34171L,34172L,34173L,34174L,34175L,34176L,34177L,34178L,34179L,34180L,
  80263. 34181L,34182L,34183L,34184L,34185L,34186L,34187L,34188L,34189L,34190L,
  80264. 34191L,34192L,34193L,34194L,34195L,34196L,34197L,34198L,34199L,34200L,
  80265. 34201L,34202L,34203L,34204L,34205L,34206L,34207L,34208L,34209L,34210L,
  80266. 34211L,34212L,34213L,34214L,34215L,34216L,34217L,34218L,34219L,34220L,
  80267. 34221L,34222L,34223L,34224L,34225L,34226L,34227L,34228L,34229L,34230L,
  80268. 34231L,34232L,34233L,34234L,34235L,34236L,34237L,34238L,34239L,34240L,
  80269. 34241L,34242L,34243L,34244L,34245L,34246L,34247L,34248L,34249L,34250L,
  80270. 34251L,34252L,34253L,34254L,34255L,34256L,34257L,34258L,34259L,34260L,
  80271. 34261L,34262L,34263L,34264L,34265L,34266L,34267L,34268L,34269L,34270L,
  80272. 34271L,34272L,34273L,34274L,34275L,34276L,34277L,34278L,34279L,34280L,
  80273. 34281L,34282L,34283L,34284L,34285L,34286L,34287L,34288L,34289L,34290L,
  80274. 34291L,34292L,34293L,34294L,34295L,34296L,34297L,34298L,34299L,34300L,
  80275. 34301L,34302L,34303L,34304L,34305L,34306L,34307L,34308L,34309L,34310L,
  80276. 34311L,34312L,34313L,34314L,34315L,34316L,34317L,34318L,34319L,34320L,
  80277. 34321L,34322L,34323L,34324L,34325L,34326L,34327L,34328L,34329L,34330L,
  80278. 34331L,34332L,34333L,34334L,34335L,34336L,34337L,34338L,34339L,34340L,
  80279. 34341L,34342L,34343L,34344L,34345L,34346L,34347L,34348L,34349L,34350L,
  80280. 34351L,34352L,34353L,34354L,34355L,34356L,34357L,34358L,34359L,34360L,
  80281. 34361L,34362L,34363L,34364L,34365L,34366L,34367L,34368L,34369L,34370L,
  80282. 34371L,34372L,34373L,34374L,34375L,34376L,34377L,34378L,34379L,34380L,
  80283. 34381L,34382L,34383L,34384L,34385L,34386L,34387L,34388L,34389L,34390L,
  80284. 34391L,34392L,34393L,34394L,34395L,34396L,34397L,34398L,34399L,34400L,
  80285. 34401L,34402L,34403L,34404L,34405L,34406L,34407L,34408L,34409L,34410L,
  80286. 34411L,34412L,34413L,34414L,34415L,34416L,34417L,34418L,34419L,34420L,
  80287. 34421L,34422L,34423L,34424L,34425L,34426L,34427L,34428L,34429L,34430L,
  80288. 34431L,34432L,34433L,34434L,34435L,34436L,34437L,34438L,34439L,34440L,
  80289. 34441L,34442L,34443L,34444L,34445L,34446L,34447L,34448L,34449L,34450L,
  80290. 34451L,34452L,34453L,34454L,34455L,34456L,34457L,34458L,34459L,34460L,
  80291. 34461L,34462L,34463L,34464L,34465L,34466L,34467L,34468L,34469L,34470L,
  80292. 34471L,34472L,34473L,34474L,34475L,34476L,34477L,34478L,34479L,34480L,
  80293. 34481L,34482L,34483L,34484L,34485L,34486L,34487L,34488L,34489L,34490L,
  80294. 34491L,34492L,34493L,34494L,34495L,34496L,34497L,34498L,34499L,34500L,
  80295. 34501L,34502L,34503L,34504L,34505L,34506L,34507L,34508L,34509L,34510L,
  80296. 34511L,34512L,34513L,34514L,34515L,34516L,34517L,34518L,34519L,34520L,
  80297. 34521L,34522L,34523L,34524L,34525L,34526L,34527L,34528L,34529L,34530L,
  80298. 34531L,34532L,34533L,34534L,34535L,34536L,34537L,34538L,34539L,34540L,
  80299. 34541L,34542L,34543L,34544L,34545L,34546L,34547L,34548L,34549L,34550L,
  80300. 34551L,34552L,34553L,34554L,34555L,34556L,34557L,34558L,34559L,34560L,
  80301. 34561L,34562L,34563L,34564L,34565L,34566L,34567L,34568L,34569L,34570L,
  80302. 34571L,34572L,34573L,34574L,34575L,34576L,34577L,34578L,34579L,34580L,
  80303. 34581L,34582L,34583L,34584L,34585L,34586L,34587L,34588L,34589L,34590L,
  80304. 34591L,34592L,34593L,34594L,34595L,34596L,34597L,34598L,34599L,34600L,
  80305. 34601L,34602L,34603L,34604L,34605L,34606L,34607L,34608L,34609L,34610L,
  80306. 34611L,34612L,34613L,34614L,34615L,34616L,34617L,34618L,34619L,34620L,
  80307. 34621L,34622L,34623L,34624L,34625L,34626L,34627L,34628L,34629L,34630L,
  80308. 34631L,34632L,34633L,34634L,34635L,34636L,34637L,34638L,34639L,34640L,
  80309. 34641L,34642L,34643L,34644L,34645L,34646L,34647L,34648L,34649L,34650L,
  80310. 34651L,34652L,34653L,34654L,34655L,34656L,34657L,34658L,34659L,34660L,
  80311. 34661L,34662L,34663L,34664L,34665L,34666L,34667L,34668L,34669L,34670L,
  80312. 34671L,34672L,34673L,34674L,34675L,34676L,34677L,34678L,34679L,34680L,
  80313. 34681L,34682L,34683L,34684L,34685L,34686L,34687L,34688L,34689L,34690L,
  80314. 34691L,34692L,34693L,34694L,34695L,34696L,34697L,34698L,34699L,34700L,
  80315. 34701L,34702L,34703L,34704L,34705L,34706L,34707L,34708L,34709L,34710L,
  80316. 34711L,34712L,34713L,34714L,34715L,34716L,34717L,34718L,34719L,34720L,
  80317. 34721L,34722L,34723L,34724L,34725L,34726L,34727L,34728L,34729L,34730L,
  80318. 34731L,34732L,34733L,34734L,34735L,34736L,34737L,34738L,34739L,34740L,
  80319. 34741L,34742L,34743L,34744L,34745L,34746L,34747L,34748L,34749L,34750L,
  80320. 34751L,34752L,34753L,34754L,34755L,34756L,34757L,34758L,34759L,34760L,
  80321. 34761L,34762L,34763L,34764L,34765L,34766L,34767L,34768L,34769L,34770L,
  80322. 34771L,34772L,34773L,34774L,34775L,34776L,34777L,34778L,34779L,34780L,
  80323. 34781L,34782L,34783L,34784L,34785L,34786L,34787L,34788L,34789L,34790L,
  80324. 34791L,34792L,34793L,34794L,34795L,34796L,34797L,34798L,34799L,34800L,
  80325. 34801L,34802L,34803L,34804L,34805L,34806L,34807L,34808L,34809L,34810L,
  80326. 34811L,34812L,34813L,34814L,34815L,34816L,34817L,34818L,34819L,34820L,
  80327. 34821L,34822L,34823L,34824L,34825L,34826L,34827L,34828L,34829L,34830L,
  80328. 34831L,34832L,34833L,34834L,34835L,34836L,34837L,34838L,34839L,34840L,
  80329. 34841L,34842L,34843L,34844L,34845L,34846L,34847L,34848L,34849L,34850L,
  80330. 34851L,34852L,34853L,34854L,34855L,34856L,34857L,34858L,34859L,34860L,
  80331. 34861L,34862L,34863L,34864L,34865L,34866L,34867L,34868L,34869L,34870L,
  80332. 34871L,34872L,34873L,34874L,34875L,34876L,34877L,34878L,34879L,34880L,
  80333. 34881L,34882L,34883L,34884L,34885L,34886L,34887L,34888L,34889L,34890L,
  80334. 34891L,34892L,34893L,34894L,34895L,34896L,34897L,34898L,34899L,34900L,
  80335. 34901L,34902L,34903L,34904L,34905L,34906L,34907L,34908L,34909L,34910L,
  80336. 34911L,34912L,34913L,34914L,34915L,34916L,34917L,34918L,34919L,34920L,
  80337. 34921L,34922L,34923L,34924L,34925L,34926L,34927L,34928L,34929L,34930L,
  80338. 34931L,34932L,34933L,34934L,34935L,34936L,34937L,34938L,34939L,34940L,
  80339. 34941L,34942L,34943L,34944L,34945L,34946L,34947L,34948L,34949L,34950L,
  80340. 34951L,34952L,34953L,34954L,34955L,34956L,34957L,34958L,34959L,34960L,
  80341. 34961L,34962L,34963L,34964L,34965L,34966L,34967L,34968L,34969L,34970L,
  80342. 34971L,34972L,34973L,34974L,34975L,34976L,34977L,34978L,34979L,34980L,
  80343. 34981L,34982L,34983L,34984L,34985L,34986L,34987L,34988L,34989L,34990L,
  80344. 34991L,34992L,34993L,34994L,34995L,34996L,34997L,34998L,34999L,35000L,
  80345. 35001L,35002L,35003L,35004L,35005L,35006L,35007L,35008L,35009L,35010L,
  80346. 35011L,35012L,35013L,35014L,35015L,35016L,35017L,35018L,35019L,35020L,
  80347. 35021L,35022L,35023L,35024L,35025L,35026L,35027L,35028L,35029L,35030L,
  80348. 35031L,35032L,35033L,35034L,35035L,35036L,35037L,35038L,35039L,35040L,
  80349. 35041L,35042L,35043L,35044L,35045L,35046L,35047L,35048L,35049L,35050L,
  80350. 35051L,35052L,35053L,35054L,35055L,35056L,35057L,35058L,35059L,35060L,
  80351. 35061L,35062L,35063L,35064L,35065L,35066L,35067L,35068L,35069L,35070L,
  80352. 35071L,35072L,35073L,35074L,35075L,35076L,35077L,35078L,35079L,35080L,
  80353. 35081L,35082L,35083L,35084L,35085L,35086L,35087L,35088L,35089L,35090L,
  80354. 35091L,35092L,35093L,35094L,35095L,35096L,35097L,35098L,35099L,35100L,
  80355. 35101L,35102L,35103L,35104L,35105L,35106L,35107L,35108L,35109L,35110L,
  80356. 35111L,35112L,35113L,35114L,35115L,35116L,35117L,35118L,35119L,35120L,
  80357. 35121L,35122L,35123L,35124L,35125L,35126L,35127L,35128L,35129L,35130L,
  80358. 35131L,35132L,35133L,35134L,35135L,35136L,35137L,35138L,35139L,35140L,
  80359. 35141L,35142L,35143L,35144L,35145L,35146L,35147L,35148L,35149L,35150L,
  80360. 35151L,35152L,35153L,35154L,35155L,35156L,35157L,35158L,35159L,35160L,
  80361. 35161L,35162L,35163L,35164L,35165L,35166L,35167L,35168L,35169L,35170L,
  80362. 35171L,35172L,35173L,35174L,35175L,35176L,35177L,35178L,35179L,35180L,
  80363. 35181L,35182L,35183L,35184L,35185L,35186L,35187L,35188L,35189L,35190L,
  80364. 35191L,35192L,35193L,35194L,35195L,35196L,35197L,35198L,35199L,35200L,
  80365. 35201L,35202L,35203L,35204L,35205L,35206L,35207L,35208L,35209L,35210L,
  80366. 35211L,35212L,35213L,35214L,35215L,35216L,35217L,35218L,35219L,35220L,
  80367. 35221L,35222L,35223L,35224L,35225L,35226L,35227L,35228L,35229L,35230L,
  80368. 35231L,35232L,35233L,35234L,35235L,35236L,35237L,35238L,35239L,35240L,
  80369. 35241L,35242L,35243L,35244L,35245L,35246L,35247L,35248L,35249L,35250L,
  80370. 35251L,35252L,35253L,35254L,35255L,35256L,35257L,35258L,35259L,35260L,
  80371. 35261L,35262L,35263L,35264L,35265L,35266L,35267L,35268L,35269L,35270L,
  80372. 35271L,35272L,35273L,35274L,35275L,35276L,35277L,35278L,35279L,35280L,
  80373. 35281L,35282L,35283L,35284L,35285L,35286L,35287L,35288L,35289L,35290L,
  80374. 35291L,35292L,35293L,35294L,35295L,35296L,35297L,35298L,35299L,35300L,
  80375. 35301L,35302L,35303L,35304L,35305L,35306L,35307L,35308L,35309L,35310L,
  80376. 35311L,35312L,35313L,35314L,35315L,35316L,35317L,35318L,35319L,35320L,
  80377. 35321L,35322L,35323L,35324L,35325L,35326L,35327L,35328L,35329L,35330L,
  80378. 35331L,35332L,35333L,35334L,35335L,35336L,35337L,35338L,35339L,35340L,
  80379. 35341L,35342L,35343L,35344L,35345L,35346L,35347L,35348L,35349L,35350L,
  80380. 35351L,35352L,35353L,35354L,35355L,35356L,35357L,35358L,35359L,35360L,
  80381. 35361L,35362L,35363L,35364L,35365L,35366L,35367L,35368L,35369L,35370L,
  80382. 35371L,35372L,35373L,35374L,35375L,35376L,35377L,35378L,35379L,35380L,
  80383. 35381L,35382L,35383L,35384L,35385L,35386L,35387L,35388L,35389L,35390L,
  80384. 35391L,35392L,35393L,35394L,35395L,35396L,35397L,35398L,35399L,35400L,
  80385. 35401L,35402L,35403L,35404L,35405L,35406L,35407L,35408L,35409L,35410L,
  80386. 35411L,35412L,35413L,35414L,35415L,35416L,35417L,35418L,35419L,35420L,
  80387. 35421L,35422L,35423L,35424L,35425L,35426L,35427L,35428L,35429L,35430L,
  80388. 35431L,35432L,35433L,35434L,35435L,35436L,35437L,35438L,35439L,35440L,
  80389. 35441L,35442L,35443L,35444L,35445L,35446L,35447L,35448L,35449L,35450L,
  80390. 35451L,35452L,35453L,35454L,35455L,35456L,35457L,35458L,35459L,35460L,
  80391. 35461L,35462L,35463L,35464L,35465L,35466L,35467L,35468L,35469L,35470L,
  80392. 35471L,35472L,35473L,35474L,35475L,35476L,35477L,35478L,35479L,35480L,
  80393. 35481L,35482L,35483L,35484L,35485L,35486L,35487L,35488L,35489L,35490L,
  80394. 35491L,35492L,35493L,35494L,35495L,35496L,35497L,35498L,35499L,35500L,
  80395. 35501L,35502L,35503L,35504L,35505L,35506L,35507L,35508L,35509L,35510L,
  80396. 35511L,35512L,35513L,35514L,35515L,35516L,35517L,35518L,35519L,35520L,
  80397. 35521L,35522L,35523L,35524L,35525L,35526L,35527L,35528L,35529L,35530L,
  80398. 35531L,35532L,35533L,35534L,35535L,35536L,35537L,35538L,35539L,35540L,
  80399. 35541L,35542L,35543L,35544L,35545L,35546L,35547L,35548L,35549L,35550L,
  80400. 35551L,35552L,35553L,35554L,35555L,35556L,35557L,35558L,35559L,35560L,
  80401. 35561L,35562L,35563L,35564L,35565L,35566L,35567L,35568L,35569L,35570L,
  80402. 35571L,35572L,35573L,35574L,35575L,35576L,35577L,35578L,35579L,35580L,
  80403. 35581L,35582L,35583L,35584L,35585L,35586L,35587L,35588L,35589L,35590L,
  80404. 35591L,35592L,35593L,35594L,35595L,35596L,35597L,35598L,35599L,35600L,
  80405. 35601L,35602L,35603L,35604L,35605L,35606L,35607L,35608L,35609L,35610L,
  80406. 35611L,35612L,35613L,35614L,35615L,35616L,35617L,35618L,35619L,35620L,
  80407. 35621L,35622L,35623L,35624L,35625L,35626L,35627L,35628L,35629L,35630L,
  80408. 35631L,35632L,35633L,35634L,35635L,35636L,35637L,35638L,35639L,35640L,
  80409. 35641L,35642L,35643L,35644L,35645L,35646L,35647L,35648L,35649L,35650L,
  80410. 35651L,35652L,35653L,35654L,35655L,35656L,35657L,35658L,35659L,35660L,
  80411. 35661L,35662L,35663L,35664L,35665L,35666L,35667L,35668L,35669L,35670L,
  80412. 35671L,35672L,35673L,35674L,35675L,35676L,35677L,35678L,35679L,35680L,
  80413. 35681L,35682L,35683L,35684L,35685L,35686L,35687L,35688L,35689L,35690L,
  80414. 35691L,35692L,35693L,35694L,35695L,35696L,35697L,35698L,35699L,35700L,
  80415. 35701L,35702L,35703L,35704L,35705L,35706L,35707L,35708L,35709L,35710L,
  80416. 35711L,35712L,35713L,35714L,35715L,35716L,35717L,35718L,35719L,35720L,
  80417. 35721L,35722L,35723L,35724L,35725L,35726L,35727L,35728L,35729L,35730L,
  80418. 35731L,35732L,35733L,35734L,35735L,35736L,35737L,35738L,35739L,35740L,
  80419. 35741L,35742L,35743L,35744L,35745L,35746L,35747L,35748L,35749L,35750L,
  80420. 35751L,35752L,35753L,35754L,35755L,35756L,35757L,35758L,35759L,35760L,
  80421. 35761L,35762L,35763L,35764L,35765L,35766L,35767L,35768L,35769L,35770L,
  80422. 35771L,35772L,35773L,35774L,35775L,35776L,35777L,35778L,35779L,35780L,
  80423. 35781L,35782L,35783L,35784L,35785L,35786L,35787L,35788L,35789L,35790L,
  80424. 35791L,35792L,35793L,35794L,35795L,35796L,35797L,35798L,35799L,35800L,
  80425. 35801L,35802L,35803L,35804L,35805L,35806L,35807L,35808L,35809L,35810L,
  80426. 35811L,35812L,35813L,35814L,35815L,35816L,35817L,35818L,35819L,35820L,
  80427. 35821L,35822L,35823L,35824L,35825L,35826L,35827L,35828L,35829L,35830L,
  80428. 35831L,35832L,35833L,35834L,35835L,35836L,35837L,35838L,35839L,35840L,
  80429. 35841L,35842L,35843L,35844L,35845L,35846L,35847L,35848L,35849L,35850L,
  80430. 35851L,35852L,35853L,35854L,35855L,35856L,35857L,35858L,35859L,35860L,
  80431. 35861L,35862L,35863L,35864L,35865L,35866L,35867L,35868L,35869L,35870L,
  80432. 35871L,35872L,35873L,35874L,35875L,35876L,35877L,35878L,35879L,35880L,
  80433. 35881L,35882L,35883L,35884L,35885L,35886L,35887L,35888L,35889L,35890L,
  80434. 35891L,35892L,35893L,35894L,35895L,35896L,35897L,35898L,35899L,35900L,
  80435. 35901L,35902L,35903L,35904L,35905L,35906L,35907L,35908L,35909L,35910L,
  80436. 35911L,35912L,35913L,35914L,35915L,35916L,35917L,35918L,35919L,35920L,
  80437. 35921L,35922L,35923L,35924L,35925L,35926L,35927L,35928L,35929L,35930L,
  80438. 35931L,35932L,35933L,35934L,35935L,35936L,35937L,35938L,35939L,35940L,
  80439. 35941L,35942L,35943L,35944L,35945L,35946L,35947L,35948L,35949L,35950L,
  80440. 35951L,35952L,35953L,35954L,35955L,35956L,35957L,35958L,35959L,35960L,
  80441. 35961L,35962L,35963L,35964L,35965L,35966L,35967L,35968L,35969L,35970L,
  80442. 35971L,35972L,35973L,35974L,35975L,35976L,35977L,35978L,35979L,35980L,
  80443. 35981L,35982L,35983L,35984L,35985L,35986L,35987L,35988L,35989L,35990L,
  80444. 35991L,35992L,35993L,35994L,35995L,35996L,35997L,35998L,35999L,36000L,
  80445. 36001L,36002L,36003L,36004L,36005L,36006L,36007L,36008L,36009L,36010L,
  80446. 36011L,36012L,36013L,36014L,36015L,36016L,36017L,36018L,36019L,36020L,
  80447. 36021L,36022L,36023L,36024L,36025L,36026L,36027L,36028L,36029L,36030L,
  80448. 36031L,36032L,36033L,36034L,36035L,36036L,36037L,36038L,36039L,36040L,
  80449. 36041L,36042L,36043L,36044L,36045L,36046L,36047L,36048L,36049L,36050L,
  80450. 36051L,36052L,36053L,36054L,36055L,36056L,36057L,36058L,36059L,36060L,
  80451. 36061L,36062L,36063L,36064L,36065L,36066L,36067L,36068L,36069L,36070L,
  80452. 36071L,36072L,36073L,36074L,36075L,36076L,36077L,36078L,36079L,36080L,
  80453. 36081L,36082L,36083L,36084L,36085L,36086L,36087L,36088L,36089L,36090L,
  80454. 36091L,36092L,36093L,36094L,36095L,36096L,36097L,36098L,36099L,36100L,
  80455. 36101L,36102L,36103L,36104L,36105L,36106L,36107L,36108L,36109L,36110L,
  80456. 36111L,36112L,36113L,36114L,36115L,36116L,36117L,36118L,36119L,36120L,
  80457. 36121L,36122L,36123L,36124L,36125L,36126L,36127L,36128L,36129L,36130L,
  80458. 36131L,36132L,36133L,36134L,36135L,36136L,36137L,36138L,36139L,36140L,
  80459. 36141L,36142L,36143L,36144L,36145L,36146L,36147L,36148L,36149L,36150L,
  80460. 36151L,36152L,36153L,36154L,36155L,36156L,36157L,36158L,36159L,36160L,
  80461. 36161L,36162L,36163L,36164L,36165L,36166L,36167L,36168L,36169L,36170L,
  80462. 36171L,36172L,36173L,36174L,36175L,36176L,36177L,36178L,36179L,36180L,
  80463. 36181L,36182L,36183L,36184L,36185L,36186L,36187L,36188L,36189L,36190L,
  80464. 36191L,36192L,36193L,36194L,36195L,36196L,36197L,36198L,36199L,36200L,
  80465. 36201L,36202L,36203L,36204L,36205L,36206L,36207L,36208L,36209L,36210L,
  80466. 36211L,36212L,36213L,36214L,36215L,36216L,36217L,36218L,36219L,36220L,
  80467. 36221L,36222L,36223L,36224L,36225L,36226L,36227L,36228L,36229L,36230L,
  80468. 36231L,36232L,36233L,36234L,36235L,36236L,36237L,36238L,36239L,36240L,
  80469. 36241L,36242L,36243L,36244L,36245L,36246L,36247L,36248L,36249L,36250L,
  80470. 36251L,36252L,36253L,36254L,36255L,36256L,36257L,36258L,36259L,36260L,
  80471. 36261L,36262L,36263L,36264L,36265L,36266L,36267L,36268L,36269L,36270L,
  80472. 36271L,36272L,36273L,36274L,36275L,36276L,36277L,36278L,36279L,36280L,
  80473. 36281L,36282L,36283L,36284L,36285L,36286L,36287L,36288L,36289L,36290L,
  80474. 36291L,36292L,36293L,36294L,36295L,36296L,36297L,36298L,36299L,36300L,
  80475. 36301L,36302L,36303L,36304L,36305L,36306L,36307L,36308L,36309L,36310L,
  80476. 36311L,36312L,36313L,36314L,36315L,36316L,36317L,36318L,36319L,36320L,
  80477. 36321L,36322L,36323L,36324L,36325L,36326L,36327L,36328L,36329L,36330L,
  80478. 36331L,36332L,36333L,36334L,36335L,36336L,36337L,36338L,36339L,36340L,
  80479. 36341L,36342L,36343L,36344L,36345L,36346L,36347L,36348L,36349L,36350L,
  80480. 36351L,36352L,36353L,36354L,36355L,36356L,36357L,36358L,36359L,36360L,
  80481. 36361L,36362L,36363L,36364L,36365L,36366L,36367L,36368L,36369L,36370L,
  80482. 36371L,36372L,36373L,36374L,36375L,36376L,36377L,36378L,36379L,36380L,
  80483. 36381L,36382L,36383L,36384L,36385L,36386L,36387L,36388L,36389L,36390L,
  80484. 36391L,36392L,36393L,36394L,36395L,36396L,36397L,36398L,36399L,36400L,
  80485. 36401L,36402L,36403L,36404L,36405L,36406L,36407L,36408L,36409L,36410L,
  80486. 36411L,36412L,36413L,36414L,36415L,36416L,36417L,36418L,36419L,36420L,
  80487. 36421L,36422L,36423L,36424L,36425L,36426L,36427L,36428L,36429L,36430L,
  80488. 36431L,36432L,36433L,36434L,36435L,36436L,36437L,36438L,36439L,36440L,
  80489. 36441L,36442L,36443L,36444L,36445L,36446L,36447L,36448L,36449L,36450L,
  80490. 36451L,36452L,36453L,36454L,36455L,36456L,36457L,36458L,36459L,36460L,
  80491. 36461L,36462L,36463L,36464L,36465L,36466L,36467L,36468L,36469L,36470L,
  80492. 36471L,36472L,36473L,36474L,36475L,36476L,36477L,36478L,36479L,36480L,
  80493. 36481L,36482L,36483L,36484L,36485L,36486L,36487L,36488L,36489L,36490L,
  80494. 36491L,36492L,36493L,36494L,36495L,36496L,36497L,36498L,36499L,36500L,
  80495. 36501L,36502L,36503L,36504L,36505L,36506L,36507L,36508L,36509L,36510L,
  80496. 36511L,36512L,36513L,36514L,36515L,36516L,36517L,36518L,36519L,36520L,
  80497. 36521L,36522L,36523L,36524L,36525L,36526L,36527L,36528L,36529L,36530L,
  80498. 36531L,36532L,36533L,36534L,36535L,36536L,36537L,36538L,36539L,36540L,
  80499. 36541L,36542L,36543L,36544L,36545L,36546L,36547L,36548L,36549L,36550L,
  80500. 36551L,36552L,36553L,36554L,36555L,36556L,36557L,36558L,36559L,36560L,
  80501. 36561L,36562L,36563L,36564L,36565L,36566L,36567L,36568L,36569L,36570L,
  80502. 36571L,36572L,36573L,36574L,36575L,36576L,36577L,36578L,36579L,36580L,
  80503. 36581L,36582L,36583L,36584L,36585L,36586L,36587L,36588L,36589L,36590L,
  80504. 36591L,36592L,36593L,36594L,36595L,36596L,36597L,36598L,36599L,36600L,
  80505. 36601L,36602L,36603L,36604L,36605L,36606L,36607L,36608L,36609L,36610L,
  80506. 36611L,36612L,36613L,36614L,36615L,36616L,36617L,36618L,36619L,36620L,
  80507. 36621L,36622L,36623L,36624L,36625L,36626L,36627L,36628L,36629L,36630L,
  80508. 36631L,36632L,36633L,36634L,36635L,36636L,36637L,36638L,36639L,36640L,
  80509. 36641L,36642L,36643L,36644L,36645L,36646L,36647L,36648L,36649L,36650L,
  80510. 36651L,36652L,36653L,36654L,36655L,36656L,36657L,36658L,36659L,36660L,
  80511. 36661L,36662L,36663L,36664L,36665L,36666L,36667L,36668L,36669L,36670L,
  80512. 36671L,36672L,36673L,36674L,36675L,36676L,36677L,36678L,36679L,36680L,
  80513. 36681L,36682L,36683L,36684L,36685L,36686L,36687L,36688L,36689L,36690L,
  80514. 36691L,36692L,36693L,36694L,36695L,36696L,36697L,36698L,36699L,36700L,
  80515. 36701L,36702L,36703L,36704L,36705L,36706L,36707L,36708L,36709L,36710L,
  80516. 36711L,36712L,36713L,36714L,36715L,36716L,36717L,36718L,36719L,36720L,
  80517. 36721L,36722L,36723L,36724L,36725L,36726L,36727L,36728L,36729L,36730L,
  80518. 36731L,36732L,36733L,36734L,36735L,36736L,36737L,36738L,36739L,36740L,
  80519. 36741L,36742L,36743L,36744L,36745L,36746L,36747L,36748L,36749L,36750L,
  80520. 36751L,36752L,36753L,36754L,36755L,36756L,36757L,36758L,36759L,36760L,
  80521. 36761L,36762L,36763L,36764L,36765L,36766L,36767L,36768L,36769L,36770L,
  80522. 36771L,36772L,36773L,36774L,36775L,36776L,36777L,36778L,36779L,36780L,
  80523. 36781L,36782L,36783L,36784L,36785L,36786L,36787L,36788L,36789L,36790L,
  80524. 36791L,36792L,36793L,36794L,36795L,36796L,36797L,36798L,36799L,36800L,
  80525. 36801L,36802L,36803L,36804L,36805L,36806L,36807L,36808L,36809L,36810L,
  80526. 36811L,36812L,36813L,36814L,36815L,36816L,36817L,36818L,36819L,36820L,
  80527. 36821L,36822L,36823L,36824L,36825L,36826L,36827L,36828L,36829L,36830L,
  80528. 36831L,36832L,36833L,36834L,36835L,36836L,36837L,36838L,36839L,36840L,
  80529. 36841L,36842L,36843L,36844L,36845L,36846L,36847L,36848L,36849L,36850L,
  80530. 36851L,36852L,36853L,36854L,36855L,36856L,36857L,36858L,36859L,36860L,
  80531. 36861L,36862L,36863L,36864L,36865L,36866L,36867L,36868L,36869L,36870L,
  80532. 36871L,36872L,36873L,36874L,36875L,36876L,36877L,36878L,36879L,36880L,
  80533. 36881L,36882L,36883L,36884L,36885L,36886L,36887L,36888L,36889L,36890L,
  80534. 36891L,36892L,36893L,36894L,36895L,36896L,36897L,36898L,36899L,36900L,
  80535. 36901L,36902L,36903L,36904L,36905L,36906L,36907L,36908L,36909L,36910L,
  80536. 36911L,36912L,36913L,36914L,36915L,36916L,36917L,36918L,36919L,36920L,
  80537. 36921L,36922L,36923L,36924L,36925L,36926L,36927L,36928L,36929L,36930L,
  80538. 36931L,36932L,36933L,36934L,36935L,36936L,36937L,36938L,36939L,36940L,
  80539. 36941L,36942L,36943L,36944L,36945L,36946L,36947L,36948L,36949L,36950L,
  80540. 36951L,36952L,36953L,36954L,36955L,36956L,36957L,36958L,36959L,36960L,
  80541. 36961L,36962L,36963L,36964L,36965L,36966L,36967L,36968L,36969L,36970L,
  80542. 36971L,36972L,36973L,36974L,36975L,36976L,36977L,36978L,36979L,36980L,
  80543. 36981L,36982L,36983L,36984L,36985L,36986L,36987L,36988L,36989L,36990L,
  80544. 36991L,36992L,36993L,36994L,36995L,36996L,36997L,36998L,36999L,37000L,
  80545. 37001L,37002L,37003L,37004L,37005L,37006L,37007L,37008L,37009L,37010L,
  80546. 37011L,37012L,37013L,37014L,37015L,37016L,37017L,37018L,37019L,37020L,
  80547. 37021L,37022L,37023L,37024L,37025L,37026L,37027L,37028L,37029L,37030L,
  80548. 37031L,37032L,37033L,37034L,37035L,37036L,37037L,37038L,37039L,37040L,
  80549. 37041L,37042L,37043L,37044L,37045L,37046L,37047L,37048L,37049L,37050L,
  80550. 37051L,37052L,37053L,37054L,37055L,37056L,37057L,37058L,37059L,37060L,
  80551. 37061L,37062L,37063L,37064L,37065L,37066L,37067L,37068L,37069L,37070L,
  80552. 37071L,37072L,37073L,37074L,37075L,37076L,37077L,37078L,37079L,37080L,
  80553. 37081L,37082L,37083L,37084L,37085L,37086L,37087L,37088L,37089L,37090L,
  80554. 37091L,37092L,37093L,37094L,37095L,37096L,37097L,37098L,37099L,37100L,
  80555. 37101L,37102L,37103L,37104L,37105L,37106L,37107L,37108L,37109L,37110L,
  80556. 37111L,37112L,37113L,37114L,37115L,37116L,37117L,37118L,37119L,37120L,
  80557. 37121L,37122L,37123L,37124L,37125L,37126L,37127L,37128L,37129L,37130L,
  80558. 37131L,37132L,37133L,37134L,37135L,37136L,37137L,37138L,37139L,37140L,
  80559. 37141L,37142L,37143L,37144L,37145L,37146L,37147L,37148L,37149L,37150L,
  80560. 37151L,37152L,37153L,37154L,37155L,37156L,37157L,37158L,37159L,37160L,
  80561. 37161L,37162L,37163L,37164L,37165L,37166L,37167L,37168L,37169L,37170L,
  80562. 37171L,37172L,37173L,37174L,37175L,37176L,37177L,37178L,37179L,37180L,
  80563. 37181L,37182L,37183L,37184L,37185L,37186L,37187L,37188L,37189L,37190L,
  80564. 37191L,37192L,37193L,37194L,37195L,37196L,37197L,37198L,37199L,37200L,
  80565. 37201L,37202L,37203L,37204L,37205L,37206L,37207L,37208L,37209L,37210L,
  80566. 37211L,37212L,37213L,37214L,37215L,37216L,37217L,37218L,37219L,37220L,
  80567. 37221L,37222L,37223L,37224L,37225L,37226L,37227L,37228L,37229L,37230L,
  80568. 37231L,37232L,37233L,37234L,37235L,37236L,37237L,37238L,37239L,37240L,
  80569. 37241L,37242L,37243L,37244L,37245L,37246L,37247L,37248L,37249L,37250L,
  80570. 37251L,37252L,37253L,37254L,37255L,37256L,37257L,37258L,37259L,37260L,
  80571. 37261L,37262L,37263L,37264L,37265L,37266L,37267L,37268L,37269L,37270L,
  80572. 37271L,37272L,37273L,37274L,37275L,37276L,37277L,37278L,37279L,37280L,
  80573. 37281L,37282L,37283L,37284L,37285L,37286L,37287L,37288L,37289L,37290L,
  80574. 37291L,37292L,37293L,37294L,37295L,37296L,37297L,37298L,37299L,37300L,
  80575. 37301L,37302L,37303L,37304L,37305L,37306L,37307L,37308L,37309L,37310L,
  80576. 37311L,37312L,37313L,37314L,37315L,37316L,37317L,37318L,37319L,37320L,
  80577. 37321L,37322L,37323L,37324L,37325L,37326L,37327L,37328L,37329L,37330L,
  80578. 37331L,37332L,37333L,37334L,37335L,37336L,37337L,37338L,37339L,37340L,
  80579. 37341L,37342L,37343L,37344L,37345L,37346L,37347L,37348L,37349L,37350L,
  80580. 37351L,37352L,37353L,37354L,37355L,37356L,37357L,37358L,37359L,37360L,
  80581. 37361L,37362L,37363L,37364L,37365L,37366L,37367L,37368L,37369L,37370L,
  80582. 37371L,37372L,37373L,37374L,37375L,37376L,37377L,37378L,37379L,37380L,
  80583. 37381L,37382L,37383L,37384L,37385L,37386L,37387L,37388L,37389L,37390L,
  80584. 37391L,37392L,37393L,37394L,37395L,37396L,37397L,37398L,37399L,37400L,
  80585. 37401L,37402L,37403L,37404L,37405L,37406L,37407L,37408L,37409L,37410L,
  80586. 37411L,37412L,37413L,37414L,37415L,37416L,37417L,37418L,37419L,37420L,
  80587. 37421L,37422L,37423L,37424L,37425L,37426L,37427L,37428L,37429L,37430L,
  80588. 37431L,37432L,37433L,37434L,37435L,37436L,37437L,37438L,37439L,37440L,
  80589. 37441L,37442L,37443L,37444L,37445L,37446L,37447L,37448L,37449L,37450L,
  80590. 37451L,37452L,37453L,37454L,37455L,37456L,37457L,37458L,37459L,37460L,
  80591. 37461L,37462L,37463L,37464L,37465L,37466L,37467L,37468L,37469L,37470L,
  80592. 37471L,37472L,37473L,37474L,37475L,37476L,37477L,37478L,37479L,37480L,
  80593. 37481L,37482L,37483L,37484L,37485L,37486L,37487L,37488L,37489L,37490L,
  80594. 37491L,37492L,37493L,37494L,37495L,37496L,37497L,37498L,37499L,37500L,
  80595. 37501L,37502L,37503L,37504L,37505L,37506L,37507L,37508L,37509L,37510L,
  80596. 37511L,37512L,37513L,37514L,37515L,37516L,37517L,37518L,37519L,37520L,
  80597. 37521L,37522L,37523L,37524L,37525L,37526L,37527L,37528L,37529L,37530L,
  80598. 37531L,37532L,37533L,37534L,37535L,37536L,37537L,37538L,37539L,37540L,
  80599. 37541L,37542L,37543L,37544L,37545L,37546L,37547L,37548L,37549L,37550L,
  80600. 37551L,37552L,37553L,37554L,37555L,37556L,37557L,37558L,37559L,37560L,
  80601. 37561L,37562L,37563L,37564L,37565L,37566L,37567L,37568L,37569L,37570L,
  80602. 37571L,37572L,37573L,37574L,37575L,37576L,37577L,37578L,37579L,37580L,
  80603. 37581L,37582L,37583L,37584L,37585L,37586L,37587L,37588L,37589L,37590L,
  80604. 37591L,37592L,37593L,37594L,37595L,37596L,37597L,37598L,37599L,37600L,
  80605. 37601L,37602L,37603L,37604L,37605L,37606L,37607L,37608L,37609L,37610L,
  80606. 37611L,37612L,37613L,37614L,37615L,37616L,37617L,37618L,37619L,37620L,
  80607. 37621L,37622L,37623L,37624L,37625L,37626L,37627L,37628L,37629L,37630L,
  80608. 37631L,37632L,37633L,37634L,37635L,37636L,37637L,37638L,37639L,37640L,
  80609. 37641L,37642L,37643L,37644L,37645L,37646L,37647L,37648L,37649L,37650L,
  80610. 37651L,37652L,37653L,37654L,37655L,37656L,37657L,37658L,37659L,37660L,
  80611. 37661L,37662L,37663L,37664L,37665L,37666L,37667L,37668L,37669L,37670L,
  80612. 37671L,37672L,37673L,37674L,37675L,37676L,37677L,37678L,37679L,37680L,
  80613. 37681L,37682L,37683L,37684L,37685L,37686L,37687L,37688L,37689L,37690L,
  80614. 37691L,37692L,37693L,37694L,37695L,37696L,37697L,37698L,37699L,37700L,
  80615. 37701L,37702L,37703L,37704L,37705L,37706L,37707L,37708L,37709L,37710L,
  80616. 37711L,37712L,37713L,37714L,37715L,37716L,37717L,37718L,37719L,37720L,
  80617. 37721L,37722L,37723L,37724L,37725L,37726L,37727L,37728L,37729L,37730L,
  80618. 37731L,37732L,37733L,37734L,37735L,37736L,37737L,37738L,37739L,37740L,
  80619. 37741L,37742L,37743L,37744L,37745L,37746L,37747L,37748L,37749L,37750L,
  80620. 37751L,37752L,37753L,37754L,37755L,37756L,37757L,37758L,37759L,37760L,
  80621. 37761L,37762L,37763L,37764L,37765L,37766L,37767L,37768L,37769L,37770L,
  80622. 37771L,37772L,37773L,37774L,37775L,37776L,37777L,37778L,37779L,37780L,
  80623. 37781L,37782L,37783L,37784L,37785L,37786L,37787L,37788L,37789L,37790L,
  80624. 37791L,37792L,37793L,37794L,37795L,37796L,37797L,37798L,37799L,37800L,
  80625. 37801L,37802L,37803L,37804L,37805L,37806L,37807L,37808L,37809L,37810L,
  80626. 37811L,37812L,37813L,37814L,37815L,37816L,37817L,37818L,37819L,37820L,
  80627. 37821L,37822L,37823L,37824L,37825L,37826L,37827L,37828L,37829L,37830L,
  80628. 37831L,37832L,37833L,37834L,37835L,37836L,37837L,37838L,37839L,37840L,
  80629. 37841L,37842L,37843L,37844L,37845L,37846L,37847L,37848L,37849L,37850L,
  80630. 37851L,37852L,37853L,37854L,37855L,37856L,37857L,37858L,37859L,37860L,
  80631. 37861L,37862L,37863L,37864L,37865L,37866L,37867L,37868L,37869L,37870L,
  80632. 37871L,37872L,37873L,37874L,37875L,37876L,37877L,37878L,37879L,37880L,
  80633. 37881L,37882L,37883L,37884L,37885L,37886L,37887L,37888L,37889L,37890L,
  80634. 37891L,37892L,37893L,37894L,37895L,37896L,37897L,37898L,37899L,37900L,
  80635. 37901L,37902L,37903L,37904L,37905L,37906L,37907L,37908L,37909L,37910L,
  80636. 37911L,37912L,37913L,37914L,37915L,37916L,37917L,37918L,37919L,37920L,
  80637. 37921L,37922L,37923L,37924L,37925L,37926L,37927L,37928L,37929L,37930L,
  80638. 37931L,37932L,37933L,37934L,37935L,37936L,37937L,37938L,37939L,37940L,
  80639. 37941L,37942L,37943L,37944L,37945L,37946L,37947L,37948L,37949L,37950L,
  80640. 37951L,37952L,37953L,37954L,37955L,37956L,37957L,37958L,37959L,37960L,
  80641. 37961L,37962L,37963L,37964L,37965L,37966L,37967L,37968L,37969L,37970L,
  80642. 37971L,37972L,37973L,37974L,37975L,37976L,37977L,37978L,37979L,37980L,
  80643. 37981L,37982L,37983L,37984L,37985L,37986L,37987L,37988L,37989L,37990L,
  80644. 37991L,37992L,37993L,37994L,37995L,37996L,37997L,37998L,37999L,38000L,
  80645. 38001L,38002L,38003L,38004L,38005L,38006L,38007L,38008L,38009L,38010L,
  80646. 38011L,38012L,38013L,38014L,38015L,38016L,38017L,38018L,38019L,38020L,
  80647. 38021L,38022L,38023L,38024L,38025L,38026L,38027L,38028L,38029L,38030L,
  80648. 38031L,38032L,38033L,38034L,38035L,38036L,38037L,38038L,38039L,38040L,
  80649. 38041L,38042L,38043L,38044L,38045L,38046L,38047L,38048L,38049L,38050L,
  80650. 38051L,38052L,38053L,38054L,38055L,38056L,38057L,38058L,38059L,38060L,
  80651. 38061L,38062L,38063L,38064L,38065L,38066L,38067L,38068L,38069L,38070L,
  80652. 38071L,38072L,38073L,38074L,38075L,38076L,38077L,38078L,38079L,38080L,
  80653. 38081L,38082L,38083L,38084L,38085L,38086L,38087L,38088L,38089L,38090L,
  80654. 38091L,38092L,38093L,38094L,38095L,38096L,38097L,38098L,38099L,38100L,
  80655. 38101L,38102L,38103L,38104L,38105L,38106L,38107L,38108L,38109L,38110L,
  80656. 38111L,38112L,38113L,38114L,38115L,38116L,38117L,38118L,38119L,38120L,
  80657. 38121L,38122L,38123L,38124L,38125L,38126L,38127L,38128L,38129L,38130L,
  80658. 38131L,38132L,38133L,38134L,38135L,38136L,38137L,38138L,38139L,38140L,
  80659. 38141L,38142L,38143L,38144L,38145L,38146L,38147L,38148L,38149L,38150L,
  80660. 38151L,38152L,38153L,38154L,38155L,38156L,38157L,38158L,38159L,38160L,
  80661. 38161L,38162L,38163L,38164L,38165L,38166L,38167L,38168L,38169L,38170L,
  80662. 38171L,38172L,38173L,38174L,38175L,38176L,38177L,38178L,38179L,38180L,
  80663. 38181L,38182L,38183L,38184L,38185L,38186L,38187L,38188L,38189L,38190L,
  80664. 38191L,38192L,38193L,38194L,38195L,38196L,38197L,38198L,38199L,38200L,
  80665. 38201L,38202L,38203L,38204L,38205L,38206L,38207L,38208L,38209L,38210L,
  80666. 38211L,38212L,38213L,38214L,38215L,38216L,38217L,38218L,38219L,38220L,
  80667. 38221L,38222L,38223L,38224L,38225L,38226L,38227L,38228L,38229L,38230L,
  80668. 38231L,38232L,38233L,38234L,38235L,38236L,38237L,38238L,38239L,38240L,
  80669. 38241L,38242L,38243L,38244L,38245L,38246L,38247L,38248L,38249L,38250L,
  80670. 38251L,38252L,38253L,38254L,38255L,38256L,38257L,38258L,38259L,38260L,
  80671. 38261L,38262L,38263L,38264L,38265L,38266L,38267L,38268L,38269L,38270L,
  80672. 38271L,38272L,38273L,38274L,38275L,38276L,38277L,38278L,38279L,38280L,
  80673. 38281L,38282L,38283L,38284L,38285L,38286L,38287L,38288L,38289L,38290L,
  80674. 38291L,38292L,38293L,38294L,38295L,38296L,38297L,38298L,38299L,38300L,
  80675. 38301L,38302L,38303L,38304L,38305L,38306L,38307L,38308L,38309L,38310L,
  80676. 38311L,38312L,38313L,38314L,38315L,38316L,38317L,38318L,38319L,38320L,
  80677. 38321L,38322L,38323L,38324L,38325L,38326L,38327L,38328L,38329L,38330L,
  80678. 38331L,38332L,38333L,38334L,38335L,38336L,38337L,38338L,38339L,38340L,
  80679. 38341L,38342L,38343L,38344L,38345L,38346L,38347L,38348L,38349L,38350L,
  80680. 38351L,38352L,38353L,38354L,38355L,38356L,38357L,38358L,38359L,38360L,
  80681. 38361L,38362L,38363L,38364L,38365L,38366L,38367L,38368L,38369L,38370L,
  80682. 38371L,38372L,38373L,38374L,38375L,38376L,38377L,38378L,38379L,38380L,
  80683. 38381L,38382L,38383L,38384L,38385L,38386L,38387L,38388L,38389L,38390L,
  80684. 38391L,38392L,38393L,38394L,38395L,38396L,38397L,38398L,38399L,38400L,
  80685. 38401L,38402L,38403L,38404L,38405L,38406L,38407L,38408L,38409L,38410L,
  80686. 38411L,38412L,38413L,38414L,38415L,38416L,38417L,38418L,38419L,38420L,
  80687. 38421L,38422L,38423L,38424L,38425L,38426L,38427L,38428L,38429L,38430L,
  80688. 38431L,38432L,38433L,38434L,38435L,38436L,38437L,38438L,38439L,38440L,
  80689. 38441L,38442L,38443L,38444L,38445L,38446L,38447L,38448L,38449L,38450L,
  80690. 38451L,38452L,38453L,38454L,38455L,38456L,38457L,38458L,38459L,38460L,
  80691. 38461L,38462L,38463L,38464L,38465L,38466L,38467L,38468L,38469L,38470L,
  80692. 38471L,38472L,38473L,38474L,38475L,38476L,38477L,38478L,38479L,38480L,
  80693. 38481L,38482L,38483L,38484L,38485L,38486L,38487L,38488L,38489L,38490L,
  80694. 38491L,38492L,38493L,38494L,38495L,38496L,38497L,38498L,38499L,38500L,
  80695. 38501L,38502L,38503L,38504L,38505L,38506L,38507L,38508L,38509L,38510L,
  80696. 38511L,38512L,38513L,38514L,38515L,38516L,38517L,38518L,38519L,38520L,
  80697. 38521L,38522L,38523L,38524L,38525L,38526L,38527L,38528L,38529L,38530L,
  80698. 38531L,38532L,38533L,38534L,38535L,38536L,38537L,38538L,38539L,38540L,
  80699. 38541L,38542L,38543L,38544L,38545L,38546L,38547L,38548L,38549L,38550L,
  80700. 38551L,38552L,38553L,38554L,38555L,38556L,38557L,38558L,38559L,38560L,
  80701. 38561L,38562L,38563L,38564L,38565L,38566L,38567L,38568L,38569L,38570L,
  80702. 38571L,38572L,38573L,38574L,38575L,38576L,38577L,38578L,38579L,38580L,
  80703. 38581L,38582L,38583L,38584L,38585L,38586L,38587L,38588L,38589L,38590L,
  80704. 38591L,38592L,38593L,38594L,38595L,38596L,38597L,38598L,38599L,38600L,
  80705. 38601L,38602L,38603L,38604L,38605L,38606L,38607L,38608L,38609L,38610L,
  80706. 38611L,38612L,38613L,38614L,38615L,38616L,38617L,38618L,38619L,38620L,
  80707. 38621L,38622L,38623L,38624L,38625L,38626L,38627L,38628L,38629L,38630L,
  80708. 38631L,38632L,38633L,38634L,38635L,38636L,38637L,38638L,38639L,38640L,
  80709. 38641L,38642L,38643L,38644L,38645L,38646L,38647L,38648L,38649L,38650L,
  80710. 38651L,38652L,38653L,38654L,38655L,38656L,38657L,38658L,38659L,38660L,
  80711. 38661L,38662L,38663L,38664L,38665L,38666L,38667L,38668L,38669L,38670L,
  80712. 38671L,38672L,38673L,38674L,38675L,38676L,38677L,38678L,38679L,38680L,
  80713. 38681L,38682L,38683L,38684L,38685L,38686L,38687L,38688L,38689L,38690L,
  80714. 38691L,38692L,38693L,38694L,38695L,38696L,38697L,38698L,38699L,38700L,
  80715. 38701L,38702L,38703L,38704L,38705L,38706L,38707L,38708L,38709L,38710L,
  80716. 38711L,38712L,38713L,38714L,38715L,38716L,38717L,38718L,38719L,38720L,
  80717. 38721L,38722L,38723L,38724L,38725L,38726L,38727L,38728L,38729L,38730L,
  80718. 38731L,38732L,38733L,38734L,38735L,38736L,38737L,38738L,38739L,38740L,
  80719. 38741L,38742L,38743L,38744L,38745L,38746L,38747L,38748L,38749L,38750L,
  80720. 38751L,38752L,38753L,38754L,38755L,38756L,38757L,38758L,38759L,38760L,
  80721. 38761L,38762L,38763L,38764L,38765L,38766L,38767L,38768L,38769L,38770L,
  80722. 38771L,38772L,38773L,38774L,38775L,38776L,38777L,38778L,38779L,38780L,
  80723. 38781L,38782L,38783L,38784L,38785L,38786L,38787L,38788L,38789L,38790L,
  80724. 38791L,38792L,38793L,38794L,38795L,38796L,38797L,38798L,38799L,38800L,
  80725. 38801L,38802L,38803L,38804L,38805L,38806L,38807L,38808L,38809L,38810L,
  80726. 38811L,38812L,38813L,38814L,38815L,38816L,38817L,38818L,38819L,38820L,
  80727. 38821L,38822L,38823L,38824L,38825L,38826L,38827L,38828L,38829L,38830L,
  80728. 38831L,38832L,38833L,38834L,38835L,38836L,38837L,38838L,38839L,38840L,
  80729. 38841L,38842L,38843L,38844L,38845L,38846L,38847L,38848L,38849L,38850L,
  80730. 38851L,38852L,38853L,38854L,38855L,38856L,38857L,38858L,38859L,38860L,
  80731. 38861L,38862L,38863L,38864L,38865L,38866L,38867L,38868L,38869L,38870L,
  80732. 38871L,38872L,38873L,38874L,38875L,38876L,38877L,38878L,38879L,38880L,
  80733. 38881L,38882L,38883L,38884L,38885L,38886L,38887L,38888L,38889L,38890L,
  80734. 38891L,38892L,38893L,38894L,38895L,38896L,38897L,38898L,38899L,38900L,
  80735. 38901L,38902L,38903L,38904L,38905L,38906L,38907L,38908L,38909L,38910L,
  80736. 38911L,38912L,38913L,38914L,38915L,38916L,38917L,38918L,38919L,38920L,
  80737. 38921L,38922L,38923L,38924L,38925L,38926L,38927L,38928L,38929L,38930L,
  80738. 38931L,38932L,38933L,38934L,38935L,38936L,38937L,38938L,38939L,38940L,
  80739. 38941L,38942L,38943L,38944L,38945L,38946L,38947L,38948L,38949L,38950L,
  80740. 38951L,38952L,38953L,38954L,38955L,38956L,38957L,38958L,38959L,38960L,
  80741. 38961L,38962L,38963L,38964L,38965L,38966L,38967L,38968L,38969L,38970L,
  80742. 38971L,38972L,38973L,38974L,38975L,38976L,38977L,38978L,38979L,38980L,
  80743. 38981L,38982L,38983L,38984L,38985L,38986L,38987L,38988L,38989L,38990L,
  80744. 38991L,38992L,38993L,38994L,38995L,38996L,38997L,38998L,38999L,39000L,
  80745. 39001L,39002L,39003L,39004L,39005L,39006L,39007L,39008L,39009L,39010L,
  80746. 39011L,39012L,39013L,39014L,39015L,39016L,39017L,39018L,39019L,39020L,
  80747. 39021L,39022L,39023L,39024L,39025L,39026L,39027L,39028L,39029L,39030L,
  80748. 39031L,39032L,39033L,39034L,39035L,39036L,39037L,39038L,39039L,39040L,
  80749. 39041L,39042L,39043L,39044L,39045L,39046L,39047L,39048L,39049L,39050L,
  80750. 39051L,39052L,39053L,39054L,39055L,39056L,39057L,39058L,39059L,39060L,
  80751. 39061L,39062L,39063L,39064L,39065L,39066L,39067L,39068L,39069L,39070L,
  80752. 39071L,39072L,39073L,39074L,39075L,39076L,39077L,39078L,39079L,39080L,
  80753. 39081L,39082L,39083L,39084L,39085L,39086L,39087L,39088L,39089L,39090L,
  80754. 39091L,39092L,39093L,39094L,39095L,39096L,39097L,39098L,39099L,39100L,
  80755. 39101L,39102L,39103L,39104L,39105L,39106L,39107L,39108L,39109L,39110L,
  80756. 39111L,39112L,39113L,39114L,39115L,39116L,39117L,39118L,39119L,39120L,
  80757. 39121L,39122L,39123L,39124L,39125L,39126L,39127L,39128L,39129L,39130L,
  80758. 39131L,39132L,39133L,39134L,39135L,39136L,39137L,39138L,39139L,39140L,
  80759. 39141L,39142L,39143L,39144L,39145L,39146L,39147L,39148L,39149L,39150L,
  80760. 39151L,39152L,39153L,39154L,39155L,39156L,39157L,39158L,39159L,39160L,
  80761. 39161L,39162L,39163L,39164L,39165L,39166L,39167L,39168L,39169L,39170L,
  80762. 39171L,39172L,39173L,39174L,39175L,39176L,39177L,39178L,39179L,39180L,
  80763. 39181L,39182L,39183L,39184L,39185L,39186L,39187L,39188L,39189L,39190L,
  80764. 39191L,39192L,39193L,39194L,39195L,39196L,39197L,39198L,39199L,39200L,
  80765. 39201L,39202L,39203L,39204L,39205L,39206L,39207L,39208L,39209L,39210L,
  80766. 39211L,39212L,39213L,39214L,39215L,39216L,39217L,39218L,39219L,39220L,
  80767. 39221L,39222L,39223L,39224L,39225L,39226L,39227L,39228L,39229L,39230L,
  80768. 39231L,39232L,39233L,39234L,39235L,39236L,39237L,39238L,39239L,39240L,
  80769. 39241L,39242L,39243L,39244L,39245L,39246L,39247L,39248L,39249L,39250L,
  80770. 39251L,39252L,39253L,39254L,39255L,39256L,39257L,39258L,39259L,39260L,
  80771. 39261L,39262L,39263L,39264L,39265L,39266L,39267L,39268L,39269L,39270L,
  80772. 39271L,39272L,39273L,39274L,39275L,39276L,39277L,39278L,39279L,39280L,
  80773. 39281L,39282L,39283L,39284L,39285L,39286L,39287L,39288L,39289L,39290L,
  80774. 39291L,39292L,39293L,39294L,39295L,39296L,39297L,39298L,39299L,39300L,
  80775. 39301L,39302L,39303L,39304L,39305L,39306L,39307L,39308L,39309L,39310L,
  80776. 39311L,39312L,39313L,39314L,39315L,39316L,39317L,39318L,39319L,39320L,
  80777. 39321L,39322L,39323L,39324L,39325L,39326L,39327L,39328L,39329L,39330L,
  80778. 39331L,39332L,39333L,39334L,39335L,39336L,39337L,39338L,39339L,39340L,
  80779. 39341L,39342L,39343L,39344L,39345L,39346L,39347L,39348L,39349L,39350L,
  80780. 39351L,39352L,39353L,39354L,39355L,39356L,39357L,39358L,39359L,39360L,
  80781. 39361L,39362L,39363L,39364L,39365L,39366L,39367L,39368L,39369L,39370L,
  80782. 39371L,39372L,39373L,39374L,39375L,39376L,39377L,39378L,39379L,39380L,
  80783. 39381L,39382L,39383L,39384L,39385L,39386L,39387L,39388L,39389L,39390L,
  80784. 39391L,39392L,39393L,39394L,39395L,39396L,39397L,39398L,39399L,39400L,
  80785. 39401L,39402L,39403L,39404L,39405L,39406L,39407L,39408L,39409L,39410L,
  80786. 39411L,39412L,39413L,39414L,39415L,39416L,39417L,39418L,39419L,39420L,
  80787. 39421L,39422L,39423L,39424L,39425L,39426L,39427L,39428L,39429L,39430L,
  80788. 39431L,39432L,39433L,39434L,39435L,39436L,39437L,39438L,39439L,39440L,
  80789. 39441L,39442L,39443L,39444L,39445L,39446L,39447L,39448L,39449L,39450L,
  80790. 39451L,39452L,39453L,39454L,39455L,39456L,39457L,39458L,39459L,39460L,
  80791. 39461L,39462L,39463L,39464L,39465L,39466L,39467L,39468L,39469L,39470L,
  80792. 39471L,39472L,39473L,39474L,39475L,39476L,39477L,39478L,39479L,39480L,
  80793. 39481L,39482L,39483L,39484L,39485L,39486L,39487L,39488L,39489L,39490L,
  80794. 39491L,39492L,39493L,39494L,39495L,39496L,39497L,39498L,39499L,39500L,
  80795. 39501L,39502L,39503L,39504L,39505L,39506L,39507L,39508L,39509L,39510L,
  80796. 39511L,39512L,39513L,39514L,39515L,39516L,39517L,39518L,39519L,39520L,
  80797. 39521L,39522L,39523L,39524L,39525L,39526L,39527L,39528L,39529L,39530L,
  80798. 39531L,39532L,39533L,39534L,39535L,39536L,39537L,39538L,39539L,39540L,
  80799. 39541L,39542L,39543L,39544L,39545L,39546L,39547L,39548L,39549L,39550L,
  80800. 39551L,39552L,39553L,39554L,39555L,39556L,39557L,39558L,39559L,39560L,
  80801. 39561L,39562L,39563L,39564L,39565L,39566L,39567L,39568L,39569L,39570L,
  80802. 39571L,39572L,39573L,39574L,39575L,39576L,39577L,39578L,39579L,39580L,
  80803. 39581L,39582L,39583L,39584L,39585L,39586L,39587L,39588L,39589L,39590L,
  80804. 39591L,39592L,39593L,39594L,39595L,39596L,39597L,39598L,39599L,39600L,
  80805. 39601L,39602L,39603L,39604L,39605L,39606L,39607L,39608L,39609L,39610L,
  80806. 39611L,39612L,39613L,39614L,39615L,39616L,39617L,39618L,39619L,39620L,
  80807. 39621L,39622L,39623L,39624L,39625L,39626L,39627L,39628L,39629L,39630L,
  80808. 39631L,39632L,39633L,39634L,39635L,39636L,39637L,39638L,39639L,39640L,
  80809. 39641L,39642L,39643L,39644L,39645L,39646L,39647L,39648L,39649L,39650L,
  80810. 39651L,39652L,39653L,39654L,39655L,39656L,39657L,39658L,39659L,39660L,
  80811. 39661L,39662L,39663L,39664L,39665L,39666L,39667L,39668L,39669L,39670L,
  80812. 39671L,39672L,39673L,39674L,39675L,39676L,39677L,39678L,39679L,39680L,
  80813. 39681L,39682L,39683L,39684L,39685L,39686L,39687L,39688L,39689L,39690L,
  80814. 39691L,39692L,39693L,39694L,39695L,39696L,39697L,39698L,39699L,39700L,
  80815. 39701L,39702L,39703L,39704L,39705L,39706L,39707L,39708L,39709L,39710L,
  80816. 39711L,39712L,39713L,39714L,39715L,39716L,39717L,39718L,39719L,39720L,
  80817. 39721L,39722L,39723L,39724L,39725L,39726L,39727L,39728L,39729L,39730L,
  80818. 39731L,39732L,39733L,39734L,39735L,39736L,39737L,39738L,39739L,39740L,
  80819. 39741L,39742L,39743L,39744L,39745L,39746L,39747L,39748L,39749L,39750L,
  80820. 39751L,39752L,39753L,39754L,39755L,39756L,39757L,39758L,39759L,39760L,
  80821. 39761L,39762L,39763L,39764L,39765L,39766L,39767L,39768L,39769L,39770L,
  80822. 39771L,39772L,39773L,39774L,39775L,39776L,39777L,39778L,39779L,39780L,
  80823. 39781L,39782L,39783L,39784L,39785L,39786L,39787L,39788L,39789L,39790L,
  80824. 39791L,39792L,39793L,39794L,39795L,39796L,39797L,39798L,39799L,39800L,
  80825. 39801L,39802L,39803L,39804L,39805L,39806L,39807L,39808L,39809L,39810L,
  80826. 39811L,39812L,39813L,39814L,39815L,39816L,39817L,39818L,39819L,39820L,
  80827. 39821L,39822L,39823L,39824L,39825L,39826L,39827L,39828L,39829L,39830L,
  80828. 39831L,39832L,39833L,39834L,39835L,39836L,39837L,39838L,39839L,39840L,
  80829. 39841L,39842L,39843L,39844L,39845L,39846L,39847L,39848L,39849L,39850L,
  80830. 39851L,39852L,39853L,39854L,39855L,39856L,39857L,39858L,39859L,39860L,
  80831. 39861L,39862L,39863L,39864L,39865L,39866L,39867L,39868L,39869L,39870L,
  80832. 39871L,39872L,39873L,39874L,39875L,39876L,39877L,39878L,39879L,39880L,
  80833. 39881L,39882L,39883L,39884L,39885L,39886L,39887L,39888L,39889L,39890L,
  80834. 39891L,39892L,39893L,39894L,39895L,39896L,39897L,39898L,39899L,39900L,
  80835. 39901L,39902L,39903L,39904L,39905L,39906L,39907L,39908L,39909L,39910L,
  80836. 39911L,39912L,39913L,39914L,39915L,39916L,39917L,39918L,39919L,39920L,
  80837. 39921L,39922L,39923L,39924L,39925L,39926L,39927L,39928L,39929L,39930L,
  80838. 39931L,39932L,39933L,39934L,39935L,39936L,39937L,39938L,39939L,39940L,
  80839. 39941L,39942L,39943L,39944L,39945L,39946L,39947L,39948L,39949L,39950L,
  80840. 39951L,39952L,39953L,39954L,39955L,39956L,39957L,39958L,39959L,39960L,
  80841. 39961L,39962L,39963L,39964L,39965L,39966L,39967L,39968L,39969L,39970L,
  80842. 39971L,39972L,39973L,39974L,39975L,39976L,39977L,39978L,39979L,39980L,
  80843. 39981L,39982L,39983L,39984L,39985L,39986L,39987L,39988L,39989L,39990L,
  80844. 39991L,39992L,39993L,39994L,39995L,39996L,39997L,39998L,39999L,40000L,
  80845. 40001L,40002L,40003L,40004L,40005L,40006L,40007L,40008L,40009L,40010L,
  80846. 40011L,40012L,40013L,40014L,40015L,40016L,40017L,40018L,40019L,40020L,
  80847. 40021L,40022L,40023L,40024L,40025L,40026L,40027L,40028L,40029L,40030L,
  80848. 40031L,40032L,40033L,40034L,40035L,40036L,40037L,40038L,40039L,40040L,
  80849. 40041L,40042L,40043L,40044L,40045L,40046L,40047L,40048L,40049L,40050L,
  80850. 40051L,40052L,40053L,40054L,40055L,40056L,40057L,40058L,40059L,40060L,
  80851. 40061L,40062L,40063L,40064L,40065L,40066L,40067L,40068L,40069L,40070L,
  80852. 40071L,40072L,40073L,40074L,40075L,40076L,40077L,40078L,40079L,40080L,
  80853. 40081L,40082L,40083L,40084L,40085L,40086L,40087L,40088L,40089L,40090L,
  80854. 40091L,40092L,40093L,40094L,40095L,40096L,40097L,40098L,40099L,40100L,
  80855. 40101L,40102L,40103L,40104L,40105L,40106L,40107L,40108L,40109L,40110L,
  80856. 40111L,40112L,40113L,40114L,40115L,40116L,40117L,40118L,40119L,40120L,
  80857. 40121L,40122L,40123L,40124L,40125L,40126L,40127L,40128L,40129L,40130L,
  80858. 40131L,40132L,40133L,40134L,40135L,40136L,40137L,40138L,40139L,40140L,
  80859. 40141L,40142L,40143L,40144L,40145L,40146L,40147L,40148L,40149L,40150L,
  80860. 40151L,40152L,40153L,40154L,40155L,40156L,40157L,40158L,40159L,40160L,
  80861. 40161L,40162L,40163L,40164L,40165L,40166L,40167L,40168L,40169L,40170L,
  80862. 40171L,40172L,40173L,40174L,40175L,40176L,40177L,40178L,40179L,40180L,
  80863. 40181L,40182L,40183L,40184L,40185L,40186L,40187L,40188L,40189L,40190L,
  80864. 40191L,40192L,40193L,40194L,40195L,40196L,40197L,40198L,40199L,40200L,
  80865. 40201L,40202L,40203L,40204L,40205L,40206L,40207L,40208L,40209L,40210L,
  80866. 40211L,40212L,40213L,40214L,40215L,40216L,40217L,40218L,40219L,40220L,
  80867. 40221L,40222L,40223L,40224L,40225L,40226L,40227L,40228L,40229L,40230L,
  80868. 40231L,40232L,40233L,40234L,40235L,40236L,40237L,40238L,40239L,40240L,
  80869. 40241L,40242L,40243L,40244L,40245L,40246L,40247L,40248L,40249L,40250L,
  80870. 40251L,40252L,40253L,40254L,40255L,40256L,40257L,40258L,40259L,40260L,
  80871. 40261L,40262L,40263L,40264L,40265L,40266L,40267L,40268L,40269L,40270L,
  80872. 40271L,40272L,40273L,40274L,40275L,40276L,40277L,40278L,40279L,40280L,
  80873. 40281L,40282L,40283L,40284L,40285L,40286L,40287L,40288L,40289L,40290L,
  80874. 40291L,40292L,40293L,40294L,40295L,40296L,40297L,40298L,40299L,40300L,
  80875. 40301L,40302L,40303L,40304L,40305L,40306L,40307L,40308L,40309L,40310L,
  80876. 40311L,40312L,40313L,40314L,40315L,40316L,40317L,40318L,40319L,40320L,
  80877. 40321L,40322L,40323L,40324L,40325L,40326L,40327L,40328L,40329L,40330L,
  80878. 40331L,40332L,40333L,40334L,40335L,40336L,40337L,40338L,40339L,40340L,
  80879. 40341L,40342L,40343L,40344L,40345L,40346L,40347L,40348L,40349L,40350L,
  80880. 40351L,40352L,40353L,40354L,40355L,40356L,40357L,40358L,40359L,40360L,
  80881. 40361L,40362L,40363L,40364L,40365L,40366L,40367L,40368L,40369L,40370L,
  80882. 40371L,40372L,40373L,40374L,40375L,40376L,40377L,40378L,40379L,40380L,
  80883. 40381L,40382L,40383L,40384L,40385L,40386L,40387L,40388L,40389L,40390L,
  80884. 40391L,40392L,40393L,40394L,40395L,40396L,40397L,40398L,40399L,40400L,
  80885. 40401L,40402L,40403L,40404L,40405L,40406L,40407L,40408L,40409L,40410L,
  80886. 40411L,40412L,40413L,40414L,40415L,40416L,40417L,40418L,40419L,40420L,
  80887. 40421L,40422L,40423L,40424L,40425L,40426L,40427L,40428L,40429L,40430L,
  80888. 40431L,40432L,40433L,40434L,40435L,40436L,40437L,40438L,40439L,40440L,
  80889. 40441L,40442L,40443L,40444L,40445L,40446L,40447L,40448L,40449L,40450L,
  80890. 40451L,40452L,40453L,40454L,40455L,40456L,40457L,40458L,40459L,40460L,
  80891. 40461L,40462L,40463L,40464L,40465L,40466L,40467L,40468L,40469L,40470L,
  80892. 40471L,40472L,40473L,40474L,40475L,40476L,40477L,40478L,40479L,40480L,
  80893. 40481L,40482L,40483L,40484L,40485L,40486L,40487L,40488L,40489L,40490L,
  80894. 40491L,40492L,40493L,40494L,40495L,40496L,40497L,40498L,40499L,40500L,
  80895. 40501L,40502L,40503L,40504L,40505L,40506L,40507L,40508L,40509L,40510L,
  80896. 40511L,40512L,40513L,40514L,40515L,40516L,40517L,40518L,40519L,40520L,
  80897. 40521L,40522L,40523L,40524L,40525L,40526L,40527L,40528L,40529L,40530L,
  80898. 40531L,40532L,40533L,40534L,40535L,40536L,40537L,40538L,40539L,40540L,
  80899. 40541L,40542L,40543L,40544L,40545L,40546L,40547L,40548L,40549L,40550L,
  80900. 40551L,40552L,40553L,40554L,40555L,40556L,40557L,40558L,40559L,40560L,
  80901. 40561L,40562L,40563L,40564L,40565L,40566L,40567L,40568L,40569L,40570L,
  80902. 40571L,40572L,40573L,40574L,40575L,40576L,40577L,40578L,40579L,40580L,
  80903. 40581L,40582L,40583L,40584L,40585L,40586L,40587L,40588L,40589L,40590L,
  80904. 40591L,40592L,40593L,40594L,40595L,40596L,40597L,40598L,40599L,40600L,
  80905. 40601L,40602L,40603L,40604L,40605L,40606L,40607L,40608L,40609L,40610L,
  80906. 40611L,40612L,40613L,40614L,40615L,40616L,40617L,40618L,40619L,40620L,
  80907. 40621L,40622L,40623L,40624L,40625L,40626L,40627L,40628L,40629L,40630L,
  80908. 40631L,40632L,40633L,40634L,40635L,40636L,40637L,40638L,40639L,40640L,
  80909. 40641L,40642L,40643L,40644L,40645L,40646L,40647L,40648L,40649L,40650L,
  80910. 40651L,40652L,40653L,40654L,40655L,40656L,40657L,40658L,40659L,40660L,
  80911. 40661L,40662L,40663L,40664L,40665L,40666L,40667L,40668L,40669L,40670L,
  80912. 40671L,40672L,40673L,40674L,40675L,40676L,40677L,40678L,40679L,40680L,
  80913. 40681L,40682L,40683L,40684L,40685L,40686L,40687L,40688L,40689L,40690L,
  80914. 40691L,40692L,40693L,40694L,40695L,40696L,40697L,40698L,40699L,40700L,
  80915. 40701L,40702L,40703L,40704L,40705L,40706L,40707L,40708L,40709L,40710L,
  80916. 40711L,40712L,40713L,40714L,40715L,40716L,40717L,40718L,40719L,40720L,
  80917. 40721L,40722L,40723L,40724L,40725L,40726L,40727L,40728L,40729L,40730L,
  80918. 40731L,40732L,40733L,40734L,40735L,40736L,40737L,40738L,40739L,40740L,
  80919. 40741L,40742L,40743L,40744L,40745L,40746L,40747L,40748L,40749L,40750L,
  80920. 40751L,40752L,40753L,40754L,40755L,40756L,40757L,40758L,40759L,40760L,
  80921. 40761L,40762L,40763L,40764L,40765L,40766L,40767L,40768L,40769L,40770L,
  80922. 40771L,40772L,40773L,40774L,40775L,40776L,40777L,40778L,40779L,40780L,
  80923. 40781L,40782L,40783L,40784L,40785L,40786L,40787L,40788L,40789L,40790L,
  80924. 40791L,40792L,40793L,40794L,40795L,40796L,40797L,40798L,40799L,40800L,
  80925. 40801L,40802L,40803L,40804L,40805L,40806L,40807L,40808L,40809L,40810L,
  80926. 40811L,40812L,40813L,40814L,40815L,40816L,40817L,40818L,40819L,40820L,
  80927. 40821L,40822L,40823L,40824L,40825L,40826L,40827L,40828L,40829L,40830L,
  80928. 40831L,40832L,40833L,40834L,40835L,40836L,40837L,40838L,40839L,40840L,
  80929. 40841L,40842L,40843L,40844L,40845L,40846L,40847L,40848L,40849L,40850L,
  80930. 40851L,40852L,40853L,40854L,40855L,40856L,40857L,40858L,40859L,40860L,
  80931. 40861L,40862L,40863L,40864L,40865L,40866L,40867L,40868L,40869L,40870L,
  80932. 40871L,40872L,40873L,40874L,40875L,40876L,40877L,40878L,40879L,40880L,
  80933. 40881L,40882L,40883L,40884L,40885L,40886L,40887L,40888L,40889L,40890L,
  80934. 40891L,40892L,40893L,40894L,40895L,40896L,40897L,40898L,40899L,40900L,
  80935. 40901L,40902L,40903L,40904L,40905L,40906L,40907L,40908L,40909L,40910L,
  80936. 40911L,40912L,40913L,40914L,40915L,40916L,40917L,40918L,40919L,40920L,
  80937. 40921L,40922L,40923L,40924L,40925L,40926L,40927L,40928L,40929L,40930L,
  80938. 40931L,40932L,40933L,40934L,40935L,40936L,40937L,40938L,40939L,40940L,
  80939. 40941L,40942L,40943L,40944L,40945L,40946L,40947L,40948L,40949L,40950L,
  80940. 40951L,40952L,40953L,40954L,40955L,40956L,40957L,40958L,40959L,40960L,
  80941. 40961L,40962L,40963L,40964L,40965L,40966L,40967L,40968L,40969L,40970L,
  80942. 40971L,40972L,40973L,40974L,40975L,40976L,40977L,40978L,40979L,40980L,
  80943. 40981L,40982L,40983L,40984L,40985L,40986L,40987L,40988L,40989L,40990L,
  80944. 40991L,40992L,40993L,40994L,40995L,40996L,40997L,40998L,40999L,41000L,
  80945. 41001L,41002L,41003L,41004L,41005L,41006L,41007L,41008L,41009L,41010L,
  80946. 41011L,41012L,41013L,41014L,41015L,41016L,41017L,41018L,41019L,41020L,
  80947. 41021L,41022L,41023L,41024L,41025L,41026L,41027L,41028L,41029L,41030L,
  80948. 41031L,41032L,41033L,41034L,41035L,41036L,41037L,41038L,41039L,41040L,
  80949. 41041L,41042L,41043L,41044L,41045L,41046L,41047L,41048L,41049L,41050L,
  80950. 41051L,41052L,41053L,41054L,41055L,41056L,41057L,41058L,41059L,41060L,
  80951. 41061L,41062L,41063L,41064L,41065L,41066L,41067L,41068L,41069L,41070L,
  80952. 41071L,41072L,41073L,41074L,41075L,41076L,41077L,41078L,41079L,41080L,
  80953. 41081L,41082L,41083L,41084L,41085L,41086L,41087L,41088L,41089L,41090L,
  80954. 41091L,41092L,41093L,41094L,41095L,41096L,41097L,41098L,41099L,41100L,
  80955. 41101L,41102L,41103L,41104L,41105L,41106L,41107L,41108L,41109L,41110L,
  80956. 41111L,41112L,41113L,41114L,41115L,41116L,41117L,41118L,41119L,41120L,
  80957. 41121L,41122L,41123L,41124L,41125L,41126L,41127L,41128L,41129L,41130L,
  80958. 41131L,41132L,41133L,41134L,41135L,41136L,41137L,41138L,41139L,41140L,
  80959. 41141L,41142L,41143L,41144L,41145L,41146L,41147L,41148L,41149L,41150L,
  80960. 41151L,41152L,41153L,41154L,41155L,41156L,41157L,41158L,41159L,41160L,
  80961. 41161L,41162L,41163L,41164L,41165L,41166L,41167L,41168L,41169L,41170L,
  80962. 41171L,41172L,41173L,41174L,41175L,41176L,41177L,41178L,41179L,41180L,
  80963. 41181L,41182L,41183L,41184L,41185L,41186L,41187L,41188L,41189L,41190L,
  80964. 41191L,41192L,41193L,41194L,41195L,41196L,41197L,41198L,41199L,41200L,
  80965. 41201L,41202L,41203L,41204L,41205L,41206L,41207L,41208L,41209L,41210L,
  80966. 41211L,41212L,41213L,41214L,41215L,41216L,41217L,41218L,41219L,41220L,
  80967. 41221L,41222L,41223L,41224L,41225L,41226L,41227L,41228L,41229L,41230L,
  80968. 41231L,41232L,41233L,41234L,41235L,41236L,41237L,41238L,41239L,41240L,
  80969. 41241L,41242L,41243L,41244L,41245L,41246L,41247L,41248L,41249L,41250L,
  80970. 41251L,41252L,41253L,41254L,41255L,41256L,41257L,41258L,41259L,41260L,
  80971. 41261L,41262L,41263L,41264L,41265L,41266L,41267L,41268L,41269L,41270L,
  80972. 41271L,41272L,41273L,41274L,41275L,41276L,41277L,41278L,41279L,41280L,
  80973. 41281L,41282L,41283L,41284L,41285L,41286L,41287L,41288L,41289L,41290L,
  80974. 41291L,41292L,41293L,41294L,41295L,41296L,41297L,41298L,41299L,41300L,
  80975. 41301L,41302L,41303L,41304L,41305L,41306L,41307L,41308L,41309L,41310L,
  80976. 41311L,41312L,41313L,41314L,41315L,41316L,41317L,41318L,41319L,41320L,
  80977. 41321L,41322L,41323L,41324L,41325L,41326L,41327L,41328L,41329L,41330L,
  80978. 41331L,41332L,41333L,41334L,41335L,41336L,41337L,41338L,41339L,41340L,
  80979. 41341L,41342L,41343L,41344L,41345L,41346L,41347L,41348L,41349L,41350L,
  80980. 41351L,41352L,41353L,41354L,41355L,41356L,41357L,41358L,41359L,41360L,
  80981. 41361L,41362L,41363L,41364L,41365L,41366L,41367L,41368L,41369L,41370L,
  80982. 41371L,41372L,41373L,41374L,41375L,41376L,41377L,41378L,41379L,41380L,
  80983. 41381L,41382L,41383L,41384L,41385L,41386L,41387L,41388L,41389L,41390L,
  80984. 41391L,41392L,41393L,41394L,41395L,41396L,41397L,41398L,41399L,41400L,
  80985. 41401L,41402L,41403L,41404L,41405L,41406L,41407L,41408L,41409L,41410L,
  80986. 41411L,41412L,41413L,41414L,41415L,41416L,41417L,41418L,41419L,41420L,
  80987. 41421L,41422L,41423L,41424L,41425L,41426L,41427L,41428L,41429L,41430L,
  80988. 41431L,41432L,41433L,41434L,41435L,41436L,41437L,41438L,41439L,41440L,
  80989. 41441L,41442L,41443L,41444L,41445L,41446L,41447L,41448L,41449L,41450L,
  80990. 41451L,41452L,41453L,41454L,41455L,41456L,41457L,41458L,41459L,41460L,
  80991. 41461L,41462L,41463L,41464L,41465L,41466L,41467L,41468L,41469L,41470L,
  80992. 41471L,41472L,41473L,41474L,41475L,41476L,41477L,41478L,41479L,41480L,
  80993. 41481L,41482L,41483L,41484L,41485L,41486L,41487L,41488L,41489L,41490L,
  80994. 41491L,41492L,41493L,41494L,41495L,41496L,41497L,41498L,41499L,41500L,
  80995. 41501L,41502L,41503L,41504L,41505L,41506L,41507L,41508L,41509L,41510L,
  80996. 41511L,41512L,41513L,41514L,41515L,41516L,41517L,41518L,41519L,41520L,
  80997. 41521L,41522L,41523L,41524L,41525L,41526L,41527L,41528L,41529L,41530L,
  80998. 41531L,41532L,41533L,41534L,41535L,41536L,41537L,41538L,41539L,41540L,
  80999. 41541L,41542L,41543L,41544L,41545L,41546L,41547L,41548L,41549L,41550L,
  81000. 41551L,41552L,41553L,41554L,41555L,41556L,41557L,41558L,41559L,41560L,
  81001. 41561L,41562L,41563L,41564L,41565L,41566L,41567L,41568L,41569L,41570L,
  81002. 41571L,41572L,41573L,41574L,41575L,41576L,41577L,41578L,41579L,41580L,
  81003. 41581L,41582L,41583L,41584L,41585L,41586L,41587L,41588L,41589L,41590L,
  81004. 41591L,41592L,41593L,41594L,41595L,41596L,41597L,41598L,41599L,41600L,
  81005. 41601L,41602L,41603L,41604L,41605L,41606L,41607L,41608L,41609L,41610L,
  81006. 41611L,41612L,41613L,41614L,41615L,41616L,41617L,41618L,41619L,41620L,
  81007. 41621L,41622L,41623L,41624L,41625L,41626L,41627L,41628L,41629L,41630L,
  81008. 41631L,41632L,41633L,41634L,41635L,41636L,41637L,41638L,41639L,41640L,
  81009. 41641L,41642L,41643L,41644L,41645L,41646L,41647L,41648L,41649L,41650L,
  81010. 41651L,41652L,41653L,41654L,41655L,41656L,41657L,41658L,41659L,41660L,
  81011. 41661L,41662L,41663L,41664L,41665L,41666L,41667L,41668L,41669L,41670L,
  81012. 41671L,41672L,41673L,41674L,41675L,41676L,41677L,41678L,41679L,41680L,
  81013. 41681L,41682L,41683L,41684L,41685L,41686L,41687L,41688L,41689L,41690L,
  81014. 41691L,41692L,41693L,41694L,41695L,41696L,41697L,41698L,41699L,41700L,
  81015. 41701L,41702L,41703L,41704L,41705L,41706L,41707L,41708L,41709L,41710L,
  81016. 41711L,41712L,41713L,41714L,41715L,41716L,41717L,41718L,41719L,41720L,
  81017. 41721L,41722L,41723L,41724L,41725L,41726L,41727L,41728L,41729L,41730L,
  81018. 41731L,41732L,41733L,41734L,41735L,41736L,41737L,41738L,41739L,41740L,
  81019. 41741L,41742L,41743L,41744L,41745L,41746L,41747L,41748L,41749L,41750L,
  81020. 41751L,41752L,41753L,41754L,41755L,41756L,41757L,41758L,41759L,41760L,
  81021. 41761L,41762L,41763L,41764L,41765L,41766L,41767L,41768L,41769L,41770L,
  81022. 41771L,41772L,41773L,41774L,41775L,41776L,41777L,41778L,41779L,41780L,
  81023. 41781L,41782L,41783L,41784L,41785L,41786L,41787L,41788L,41789L,41790L,
  81024. 41791L,41792L,41793L,41794L,41795L,41796L,41797L,41798L,41799L,41800L,
  81025. 41801L,41802L,41803L,41804L,41805L,41806L,41807L,41808L,41809L,41810L,
  81026. 41811L,41812L,41813L,41814L,41815L,41816L,41817L,41818L,41819L,41820L,
  81027. 41821L,41822L,41823L,41824L,41825L,41826L,41827L,41828L,41829L,41830L,
  81028. 41831L,41832L,41833L,41834L,41835L,41836L,41837L,41838L,41839L,41840L,
  81029. 41841L,41842L,41843L,41844L,41845L,41846L,41847L,41848L,41849L,41850L,
  81030. 41851L,41852L,41853L,41854L,41855L,41856L,41857L,41858L,41859L,41860L,
  81031. 41861L,41862L,41863L,41864L,41865L,41866L,41867L,41868L,41869L,41870L,
  81032. 41871L,41872L,41873L,41874L,41875L,41876L,41877L,41878L,41879L,41880L,
  81033. 41881L,41882L,41883L,41884L,41885L,41886L,41887L,41888L,41889L,41890L,
  81034. 41891L,41892L,41893L,41894L,41895L,41896L,41897L,41898L,41899L,41900L,
  81035. 41901L,41902L,41903L,41904L,41905L,41906L,41907L,41908L,41909L,41910L,
  81036. 41911L,41912L,41913L,41914L,41915L,41916L,41917L,41918L,41919L,41920L,
  81037. 41921L,41922L,41923L,41924L,41925L,41926L,41927L,41928L,41929L,41930L,
  81038. 41931L,41932L,41933L,41934L,41935L,41936L,41937L,41938L,41939L,41940L,
  81039. 41941L,41942L,41943L,41944L,41945L,41946L,41947L,41948L,41949L,41950L,
  81040. 41951L,41952L,41953L,41954L,41955L,41956L,41957L,41958L,41959L,41960L,
  81041. 41961L,41962L,41963L,41964L,41965L,41966L,41967L,41968L,41969L,41970L,
  81042. 41971L,41972L,41973L,41974L,41975L,41976L,41977L,41978L,41979L,41980L,
  81043. 41981L,41982L,41983L,41984L,41985L,41986L,41987L,41988L,41989L,41990L,
  81044. 41991L,41992L,41993L,41994L,41995L,41996L,41997L,41998L,41999L,42000L,
  81045. 42001L,42002L,42003L,42004L,42005L,42006L,42007L,42008L,42009L,42010L,
  81046. 42011L,42012L,42013L,42014L,42015L,42016L,42017L,42018L,42019L,42020L,
  81047. 42021L,42022L,42023L,42024L,42025L,42026L,42027L,42028L,42029L,42030L,
  81048. 42031L,42032L,42033L,42034L,42035L,42036L,42037L,42038L,42039L,42040L,
  81049. 42041L,42042L,42043L,42044L,42045L,42046L,42047L,42048L,42049L,42050L,
  81050. 42051L,42052L,42053L,42054L,42055L,42056L,42057L,42058L,42059L,42060L,
  81051. 42061L,42062L,42063L,42064L,42065L,42066L,42067L,42068L,42069L,42070L,
  81052. 42071L,42072L,42073L,42074L,42075L,42076L,42077L,42078L,42079L,42080L,
  81053. 42081L,42082L,42083L,42084L,42085L,42086L,42087L,42088L,42089L,42090L,
  81054. 42091L,42092L,42093L,42094L,42095L,42096L,42097L,42098L,42099L,42100L,
  81055. 42101L,42102L,42103L,42104L,42105L,42106L,42107L,42108L,42109L,42110L,
  81056. 42111L,42112L,42113L,42114L,42115L,42116L,42117L,42118L,42119L,42120L,
  81057. 42121L,42122L,42123L,42124L,42125L,42126L,42127L,42128L,42129L,42130L,
  81058. 42131L,42132L,42133L,42134L,42135L,42136L,42137L,42138L,42139L,42140L,
  81059. 42141L,42142L,42143L,42144L,42145L,42146L,42147L,42148L,42149L,42150L,
  81060. 42151L,42152L,42153L,42154L,42155L,42156L,42157L,42158L,42159L,42160L,
  81061. 42161L,42162L,42163L,42164L,42165L,42166L,42167L,42168L,42169L,42170L,
  81062. 42171L,42172L,42173L,42174L,42175L,42176L,42177L,42178L,42179L,42180L,
  81063. 42181L,42182L,42183L,42184L,42185L,42186L,42187L,42188L,42189L,42190L,
  81064. 42191L,42192L,42193L,42194L,42195L,42196L,42197L,42198L,42199L,42200L,
  81065. 42201L,42202L,42203L,42204L,42205L,42206L,42207L,42208L,42209L,42210L,
  81066. 42211L,42212L,42213L,42214L,42215L,42216L,42217L,42218L,42219L,42220L,
  81067. 42221L,42222L,42223L,42224L,42225L,42226L,42227L,42228L,42229L,42230L,
  81068. 42231L,42232L,42233L,42234L,42235L,42236L,42237L,42238L,42239L,42240L,
  81069. 42241L,42242L,42243L,42244L,42245L,42246L,42247L,42248L,42249L,42250L,
  81070. 42251L,42252L,42253L,42254L,42255L,42256L,42257L,42258L,42259L,42260L,
  81071. 42261L,42262L,42263L,42264L,42265L,42266L,42267L,42268L,42269L,42270L,
  81072. 42271L,42272L,42273L,42274L,42275L,42276L,42277L,42278L,42279L,42280L,
  81073. 42281L,42282L,42283L,42284L,42285L,42286L,42287L,42288L,42289L,42290L,
  81074. 42291L,42292L,42293L,42294L,42295L,42296L,42297L,42298L,42299L,42300L,
  81075. 42301L,42302L,42303L,42304L,42305L,42306L,42307L,42308L,42309L,42310L,
  81076. 42311L,42312L,42313L,42314L,42315L,42316L,42317L,42318L,42319L,42320L,
  81077. 42321L,42322L,42323L,42324L,42325L,42326L,42327L,42328L,42329L,42330L,
  81078. 42331L,42332L,42333L,42334L,42335L,42336L,42337L,42338L,42339L,42340L,
  81079. 42341L,42342L,42343L,42344L,42345L,42346L,42347L,42348L,42349L,42350L,
  81080. 42351L,42352L,42353L,42354L,42355L,42356L,42357L,42358L,42359L,42360L,
  81081. 42361L,42362L,42363L,42364L,42365L,42366L,42367L,42368L,42369L,42370L,
  81082. 42371L,42372L,42373L,42374L,42375L,42376L,42377L,42378L,42379L,42380L,
  81083. 42381L,42382L,42383L,42384L,42385L,42386L,42387L,42388L,42389L,42390L,
  81084. 42391L,42392L,42393L,42394L,42395L,42396L,42397L,42398L,42399L,42400L,
  81085. 42401L,42402L,42403L,42404L,42405L,42406L,42407L,42408L,42409L,42410L,
  81086. 42411L,42412L,42413L,42414L,42415L,42416L,42417L,42418L,42419L,42420L,
  81087. 42421L,42422L,42423L,42424L,42425L,42426L,42427L,42428L,42429L,42430L,
  81088. 42431L,42432L,42433L,42434L,42435L,42436L,42437L,42438L,42439L,42440L,
  81089. 42441L,42442L,42443L,42444L,42445L,42446L,42447L,42448L,42449L,42450L,
  81090. 42451L,42452L,42453L,42454L,42455L,42456L,42457L,42458L,42459L,42460L,
  81091. 42461L,42462L,42463L,42464L,42465L,42466L,42467L,42468L,42469L,42470L,
  81092. 42471L,42472L,42473L,42474L,42475L,42476L,42477L,42478L,42479L,42480L,
  81093. 42481L,42482L,42483L,42484L,42485L,42486L,42487L,42488L,42489L,42490L,
  81094. 42491L,42492L,42493L,42494L,42495L,42496L,42497L,42498L,42499L,42500L,
  81095. 42501L,42502L,42503L,42504L,42505L,42506L,42507L,42508L,42509L,42510L,
  81096. 42511L,42512L,42513L,42514L,42515L,42516L,42517L,42518L,42519L,42520L,
  81097. 42521L,42522L,42523L,42524L,42525L,42526L,42527L,42528L,42529L,42530L,
  81098. 42531L,42532L,42533L,42534L,42535L,42536L,42537L,42538L,42539L,42540L,
  81099. 42541L,42542L,42543L,42544L,42545L,42546L,42547L,42548L,42549L,42550L,
  81100. 42551L,42552L,42553L,42554L,42555L,42556L,42557L,42558L,42559L,42560L,
  81101. 42560L,42562L,42562L,42564L,42564L,42566L,42566L,42568L,42568L,42570L,
  81102. 42570L,42572L,42572L,42574L,42574L,42576L,42576L,42578L,42578L,42580L,
  81103. 42580L,42582L,42582L,42584L,42584L,42586L,42586L,42588L,42588L,42590L,
  81104. 42590L,42592L,42592L,42594L,42594L,42596L,42596L,42598L,42598L,42600L,
  81105. 42600L,42602L,42602L,42604L,42604L,42606L,42607L,42608L,42609L,42610L,
  81106. 42611L,42612L,42613L,42614L,42615L,42616L,42617L,42618L,42619L,42620L,
  81107. 42621L,42622L,42623L,42624L,42624L,42626L,42626L,42628L,42628L,42630L,
  81108. 42630L,42632L,42632L,42634L,42634L,42636L,42636L,42638L,42638L,42640L,
  81109. 42640L,42642L,42642L,42644L,42644L,42646L,42646L,42648L,42648L,42650L,
  81110. 42650L,42652L,42653L,42654L,42655L,42656L,42657L,42658L,42659L,42660L,
  81111. 42661L,42662L,42663L,42664L,42665L,42666L,42667L,42668L,42669L,42670L,
  81112. 42671L,42672L,42673L,42674L,42675L,42676L,42677L,42678L,42679L,42680L,
  81113. 42681L,42682L,42683L,42684L,42685L,42686L,42687L,42688L,42689L,42690L,
  81114. 42691L,42692L,42693L,42694L,42695L,42696L,42697L,42698L,42699L,42700L,
  81115. 42701L,42702L,42703L,42704L,42705L,42706L,42707L,42708L,42709L,42710L,
  81116. 42711L,42712L,42713L,42714L,42715L,42716L,42717L,42718L,42719L,42720L,
  81117. 42721L,42722L,42723L,42724L,42725L,42726L,42727L,42728L,42729L,42730L,
  81118. 42731L,42732L,42733L,42734L,42735L,42736L,42737L,42738L,42739L,42740L,
  81119. 42741L,42742L,42743L,42744L,42745L,42746L,42747L,42748L,42749L,42750L,
  81120. 42751L,42752L,42753L,42754L,42755L,42756L,42757L,42758L,42759L,42760L,
  81121. 42761L,42762L,42763L,42764L,42765L,42766L,42767L,42768L,42769L,42770L,
  81122. 42771L,42772L,42773L,42774L,42775L,42776L,42777L,42778L,42779L,42780L,
  81123. 42781L,42782L,42783L,42784L,42785L,42786L,42786L,42788L,42788L,42790L,
  81124. 42790L,42792L,42792L,42794L,42794L,42796L,42796L,42798L,42798L,42800L,
  81125. 42801L,42802L,42802L,42804L,42804L,42806L,42806L,42808L,42808L,42810L,
  81126. 42810L,42812L,42812L,42814L,42814L,42816L,42816L,42818L,42818L,42820L,
  81127. 42820L,42822L,42822L,42824L,42824L,42826L,42826L,42828L,42828L,42830L,
  81128. 42830L,42832L,42832L,42834L,42834L,42836L,42836L,42838L,42838L,42840L,
  81129. 42840L,42842L,42842L,42844L,42844L,42846L,42846L,42848L,42848L,42850L,
  81130. 42850L,42852L,42852L,42854L,42854L,42856L,42856L,42858L,42858L,42860L,
  81131. 42860L,42862L,42862L,42864L,42865L,42866L,42867L,42868L,42869L,42870L,
  81132. 42871L,42872L,42873L,42873L,42875L,42875L,42877L,42878L,42878L,42880L,
  81133. 42880L,42882L,42882L,42884L,42884L,42886L,42886L,42888L,42889L,42890L,
  81134. 42891L,42891L,42893L,42894L,42895L,42896L,42896L,42898L,42898L,42900L,
  81135. 42901L,42902L,42902L,42904L,42904L,42906L,42906L,42908L,42908L,42910L,
  81136. 42910L,42912L,42912L,42914L,42914L,42916L,42916L,42918L,42918L,42920L,
  81137. 42920L,42922L,42923L,42924L,42925L,42926L,42927L,42928L,42929L,42930L,
  81138. 42931L,42932L,42932L,42934L,42934L,42936L,42937L,42938L,42939L,42940L,
  81139. 42941L,42942L,42943L,42944L,42945L,42946L,42947L,42948L,42949L,42950L,
  81140. 42951L,42952L,42953L,42954L,42955L,42956L,42957L,42958L,42959L,42960L,
  81141. 42961L,42962L,42963L,42964L,42965L,42966L,42967L,42968L,42969L,42970L,
  81142. 42971L,42972L,42973L,42974L,42975L,42976L,42977L,42978L,42979L,42980L,
  81143. 42981L,42982L,42983L,42984L,42985L,42986L,42987L,42988L,42989L,42990L,
  81144. 42991L,42992L,42993L,42994L,42995L,42996L,42997L,42998L,42999L,43000L,
  81145. 43001L,43002L,43003L,43004L,43005L,43006L,43007L,43008L,43009L,43010L,
  81146. 43011L,43012L,43013L,43014L,43015L,43016L,43017L,43018L,43019L,43020L,
  81147. 43021L,43022L,43023L,43024L,43025L,43026L,43027L,43028L,43029L,43030L,
  81148. 43031L,43032L,43033L,43034L,43035L,43036L,43037L,43038L,43039L,43040L,
  81149. 43041L,43042L,43043L,43044L,43045L,43046L,43047L,43048L,43049L,43050L,
  81150. 43051L,43052L,43053L,43054L,43055L,43056L,43057L,43058L,43059L,43060L,
  81151. 43061L,43062L,43063L,43064L,43065L,43066L,43067L,43068L,43069L,43070L,
  81152. 43071L,43072L,43073L,43074L,43075L,43076L,43077L,43078L,43079L,43080L,
  81153. 43081L,43082L,43083L,43084L,43085L,43086L,43087L,43088L,43089L,43090L,
  81154. 43091L,43092L,43093L,43094L,43095L,43096L,43097L,43098L,43099L,43100L,
  81155. 43101L,43102L,43103L,43104L,43105L,43106L,43107L,43108L,43109L,43110L,
  81156. 43111L,43112L,43113L,43114L,43115L,43116L,43117L,43118L,43119L,43120L,
  81157. 43121L,43122L,43123L,43124L,43125L,43126L,43127L,43128L,43129L,43130L,
  81158. 43131L,43132L,43133L,43134L,43135L,43136L,43137L,43138L,43139L,43140L,
  81159. 43141L,43142L,43143L,43144L,43145L,43146L,43147L,43148L,43149L,43150L,
  81160. 43151L,43152L,43153L,43154L,43155L,43156L,43157L,43158L,43159L,43160L,
  81161. 43161L,43162L,43163L,43164L,43165L,43166L,43167L,43168L,43169L,43170L,
  81162. 43171L,43172L,43173L,43174L,43175L,43176L,43177L,43178L,43179L,43180L,
  81163. 43181L,43182L,43183L,43184L,43185L,43186L,43187L,43188L,43189L,43190L,
  81164. 43191L,43192L,43193L,43194L,43195L,43196L,43197L,43198L,43199L,43200L,
  81165. 43201L,43202L,43203L,43204L,43205L,43206L,43207L,43208L,43209L,43210L,
  81166. 43211L,43212L,43213L,43214L,43215L,43216L,43217L,43218L,43219L,43220L,
  81167. 43221L,43222L,43223L,43224L,43225L,43226L,43227L,43228L,43229L,43230L,
  81168. 43231L,43232L,43233L,43234L,43235L,43236L,43237L,43238L,43239L,43240L,
  81169. 43241L,43242L,43243L,43244L,43245L,43246L,43247L,43248L,43249L,43250L,
  81170. 43251L,43252L,43253L,43254L,43255L,43256L,43257L,43258L,43259L,43260L,
  81171. 43261L,43262L,43263L,43264L,43265L,43266L,43267L,43268L,43269L,43270L,
  81172. 43271L,43272L,43273L,43274L,43275L,43276L,43277L,43278L,43279L,43280L,
  81173. 43281L,43282L,43283L,43284L,43285L,43286L,43287L,43288L,43289L,43290L,
  81174. 43291L,43292L,43293L,43294L,43295L,43296L,43297L,43298L,43299L,43300L,
  81175. 43301L,43302L,43303L,43304L,43305L,43306L,43307L,43308L,43309L,43310L,
  81176. 43311L,43312L,43313L,43314L,43315L,43316L,43317L,43318L,43319L,43320L,
  81177. 43321L,43322L,43323L,43324L,43325L,43326L,43327L,43328L,43329L,43330L,
  81178. 43331L,43332L,43333L,43334L,43335L,43336L,43337L,43338L,43339L,43340L,
  81179. 43341L,43342L,43343L,43344L,43345L,43346L,43347L,43348L,43349L,43350L,
  81180. 43351L,43352L,43353L,43354L,43355L,43356L,43357L,43358L,43359L,43360L,
  81181. 43361L,43362L,43363L,43364L,43365L,43366L,43367L,43368L,43369L,43370L,
  81182. 43371L,43372L,43373L,43374L,43375L,43376L,43377L,43378L,43379L,43380L,
  81183. 43381L,43382L,43383L,43384L,43385L,43386L,43387L,43388L,43389L,43390L,
  81184. 43391L,43392L,43393L,43394L,43395L,43396L,43397L,43398L,43399L,43400L,
  81185. 43401L,43402L,43403L,43404L,43405L,43406L,43407L,43408L,43409L,43410L,
  81186. 43411L,43412L,43413L,43414L,43415L,43416L,43417L,43418L,43419L,43420L,
  81187. 43421L,43422L,43423L,43424L,43425L,43426L,43427L,43428L,43429L,43430L,
  81188. 43431L,43432L,43433L,43434L,43435L,43436L,43437L,43438L,43439L,43440L,
  81189. 43441L,43442L,43443L,43444L,43445L,43446L,43447L,43448L,43449L,43450L,
  81190. 43451L,43452L,43453L,43454L,43455L,43456L,43457L,43458L,43459L,43460L,
  81191. 43461L,43462L,43463L,43464L,43465L,43466L,43467L,43468L,43469L,43470L,
  81192. 43471L,43472L,43473L,43474L,43475L,43476L,43477L,43478L,43479L,43480L,
  81193. 43481L,43482L,43483L,43484L,43485L,43486L,43487L,43488L,43489L,43490L,
  81194. 43491L,43492L,43493L,43494L,43495L,43496L,43497L,43498L,43499L,43500L,
  81195. 43501L,43502L,43503L,43504L,43505L,43506L,43507L,43508L,43509L,43510L,
  81196. 43511L,43512L,43513L,43514L,43515L,43516L,43517L,43518L,43519L,43520L,
  81197. 43521L,43522L,43523L,43524L,43525L,43526L,43527L,43528L,43529L,43530L,
  81198. 43531L,43532L,43533L,43534L,43535L,43536L,43537L,43538L,43539L,43540L,
  81199. 43541L,43542L,43543L,43544L,43545L,43546L,43547L,43548L,43549L,43550L,
  81200. 43551L,43552L,43553L,43554L,43555L,43556L,43557L,43558L,43559L,43560L,
  81201. 43561L,43562L,43563L,43564L,43565L,43566L,43567L,43568L,43569L,43570L,
  81202. 43571L,43572L,43573L,43574L,43575L,43576L,43577L,43578L,43579L,43580L,
  81203. 43581L,43582L,43583L,43584L,43585L,43586L,43587L,43588L,43589L,43590L,
  81204. 43591L,43592L,43593L,43594L,43595L,43596L,43597L,43598L,43599L,43600L,
  81205. 43601L,43602L,43603L,43604L,43605L,43606L,43607L,43608L,43609L,43610L,
  81206. 43611L,43612L,43613L,43614L,43615L,43616L,43617L,43618L,43619L,43620L,
  81207. 43621L,43622L,43623L,43624L,43625L,43626L,43627L,43628L,43629L,43630L,
  81208. 43631L,43632L,43633L,43634L,43635L,43636L,43637L,43638L,43639L,43640L,
  81209. 43641L,43642L,43643L,43644L,43645L,43646L,43647L,43648L,43649L,43650L,
  81210. 43651L,43652L,43653L,43654L,43655L,43656L,43657L,43658L,43659L,43660L,
  81211. 43661L,43662L,43663L,43664L,43665L,43666L,43667L,43668L,43669L,43670L,
  81212. 43671L,43672L,43673L,43674L,43675L,43676L,43677L,43678L,43679L,43680L,
  81213. 43681L,43682L,43683L,43684L,43685L,43686L,43687L,43688L,43689L,43690L,
  81214. 43691L,43692L,43693L,43694L,43695L,43696L,43697L,43698L,43699L,43700L,
  81215. 43701L,43702L,43703L,43704L,43705L,43706L,43707L,43708L,43709L,43710L,
  81216. 43711L,43712L,43713L,43714L,43715L,43716L,43717L,43718L,43719L,43720L,
  81217. 43721L,43722L,43723L,43724L,43725L,43726L,43727L,43728L,43729L,43730L,
  81218. 43731L,43732L,43733L,43734L,43735L,43736L,43737L,43738L,43739L,43740L,
  81219. 43741L,43742L,43743L,43744L,43745L,43746L,43747L,43748L,43749L,43750L,
  81220. 43751L,43752L,43753L,43754L,43755L,43756L,43757L,43758L,43759L,43760L,
  81221. 43761L,43762L,43763L,43764L,43765L,43766L,43767L,43768L,43769L,43770L,
  81222. 43771L,43772L,43773L,43774L,43775L,43776L,43777L,43778L,43779L,43780L,
  81223. 43781L,43782L,43783L,43784L,43785L,43786L,43787L,43788L,43789L,43790L,
  81224. 43791L,43792L,43793L,43794L,43795L,43796L,43797L,43798L,43799L,43800L,
  81225. 43801L,43802L,43803L,43804L,43805L,43806L,43807L,43808L,43809L,43810L,
  81226. 43811L,43812L,43813L,43814L,43815L,43816L,43817L,43818L,43819L,43820L,
  81227. 43821L,43822L,43823L,43824L,43825L,43826L,43827L,43828L,43829L,43830L,
  81228. 43831L,43832L,43833L,43834L,43835L,43836L,43837L,43838L,43839L,43840L,
  81229. 43841L,43842L,43843L,43844L,43845L,43846L,43847L,43848L,43849L,43850L,
  81230. 43851L,43852L,43853L,43854L,43855L,43856L,43857L,43858L,42931L,43860L,
  81231. 43861L,43862L,43863L,43864L,43865L,43866L,43867L,43868L,43869L,43870L,
  81232. 43871L,43872L,43873L,43874L,43875L,43876L,43877L,43878L,43879L,43880L,
  81233. 43881L,43882L,43883L,43884L,43885L,43886L,43887L,5024,5025,5026,5027,5028,
  81234. 5029,5030,5031,5032,5033,5034,5035,5036,5037,5038,5039,5040,5041,5042,5043,
  81235. 5044,5045,5046,5047,5048,5049,5050,5051,5052,5053,5054,5055,5056,5057,5058,
  81236. 5059,5060,5061,5062,5063,5064,5065,5066,5067,5068,5069,5070,5071,5072,5073,
  81237. 5074,5075,5076,5077,5078,5079,5080,5081,5082,5083,5084,5085,5086,5087,5088,
  81238. 5089,5090,5091,5092,5093,5094,5095,5096,5097,5098,5099,5100,5101,5102,5103,
  81239. 43968L,43969L,43970L,43971L,43972L,43973L,43974L,43975L,43976L,43977L,
  81240. 43978L,43979L,43980L,43981L,43982L,43983L,43984L,43985L,43986L,43987L,
  81241. 43988L,43989L,43990L,43991L,43992L,43993L,43994L,43995L,43996L,43997L,
  81242. 43998L,43999L,44000L,44001L,44002L,44003L,44004L,44005L,44006L,44007L,
  81243. 44008L,44009L,44010L,44011L,44012L,44013L,44014L,44015L,44016L,44017L,
  81244. 44018L,44019L,44020L,44021L,44022L,44023L,44024L,44025L,44026L,44027L,
  81245. 44028L,44029L,44030L,44031L,44032L,44033L,44034L,44035L,44036L,44037L,
  81246. 44038L,44039L,44040L,44041L,44042L,44043L,44044L,44045L,44046L,44047L,
  81247. 44048L,44049L,44050L,44051L,44052L,44053L,44054L,44055L,44056L,44057L,
  81248. 44058L,44059L,44060L,44061L,44062L,44063L,44064L,44065L,44066L,44067L,
  81249. 44068L,44069L,44070L,44071L,44072L,44073L,44074L,44075L,44076L,44077L,
  81250. 44078L,44079L,44080L,44081L,44082L,44083L,44084L,44085L,44086L,44087L,
  81251. 44088L,44089L,44090L,44091L,44092L,44093L,44094L,44095L,44096L,44097L,
  81252. 44098L,44099L,44100L,44101L,44102L,44103L,44104L,44105L,44106L,44107L,
  81253. 44108L,44109L,44110L,44111L,44112L,44113L,44114L,44115L,44116L,44117L,
  81254. 44118L,44119L,44120L,44121L,44122L,44123L,44124L,44125L,44126L,44127L,
  81255. 44128L,44129L,44130L,44131L,44132L,44133L,44134L,44135L,44136L,44137L,
  81256. 44138L,44139L,44140L,44141L,44142L,44143L,44144L,44145L,44146L,44147L,
  81257. 44148L,44149L,44150L,44151L,44152L,44153L,44154L,44155L,44156L,44157L,
  81258. 44158L,44159L,44160L,44161L,44162L,44163L,44164L,44165L,44166L,44167L,
  81259. 44168L,44169L,44170L,44171L,44172L,44173L,44174L,44175L,44176L,44177L,
  81260. 44178L,44179L,44180L,44181L,44182L,44183L,44184L,44185L,44186L,44187L,
  81261. 44188L,44189L,44190L,44191L,44192L,44193L,44194L,44195L,44196L,44197L,
  81262. 44198L,44199L,44200L,44201L,44202L,44203L,44204L,44205L,44206L,44207L,
  81263. 44208L,44209L,44210L,44211L,44212L,44213L,44214L,44215L,44216L,44217L,
  81264. 44218L,44219L,44220L,44221L,44222L,44223L,44224L,44225L,44226L,44227L,
  81265. 44228L,44229L,44230L,44231L,44232L,44233L,44234L,44235L,44236L,44237L,
  81266. 44238L,44239L,44240L,44241L,44242L,44243L,44244L,44245L,44246L,44247L,
  81267. 44248L,44249L,44250L,44251L,44252L,44253L,44254L,44255L,44256L,44257L,
  81268. 44258L,44259L,44260L,44261L,44262L,44263L,44264L,44265L,44266L,44267L,
  81269. 44268L,44269L,44270L,44271L,44272L,44273L,44274L,44275L,44276L,44277L,
  81270. 44278L,44279L,44280L,44281L,44282L,44283L,44284L,44285L,44286L,44287L,
  81271. 44288L,44289L,44290L,44291L,44292L,44293L,44294L,44295L,44296L,44297L,
  81272. 44298L,44299L,44300L,44301L,44302L,44303L,44304L,44305L,44306L,44307L,
  81273. 44308L,44309L,44310L,44311L,44312L,44313L,44314L,44315L,44316L,44317L,
  81274. 44318L,44319L,44320L,44321L,44322L,44323L,44324L,44325L,44326L,44327L,
  81275. 44328L,44329L,44330L,44331L,44332L,44333L,44334L,44335L,44336L,44337L,
  81276. 44338L,44339L,44340L,44341L,44342L,44343L,44344L,44345L,44346L,44347L,
  81277. 44348L,44349L,44350L,44351L,44352L,44353L,44354L,44355L,44356L,44357L,
  81278. 44358L,44359L,44360L,44361L,44362L,44363L,44364L,44365L,44366L,44367L,
  81279. 44368L,44369L,44370L,44371L,44372L,44373L,44374L,44375L,44376L,44377L,
  81280. 44378L,44379L,44380L,44381L,44382L,44383L,44384L,44385L,44386L,44387L,
  81281. 44388L,44389L,44390L,44391L,44392L,44393L,44394L,44395L,44396L,44397L,
  81282. 44398L,44399L,44400L,44401L,44402L,44403L,44404L,44405L,44406L,44407L,
  81283. 44408L,44409L,44410L,44411L,44412L,44413L,44414L,44415L,44416L,44417L,
  81284. 44418L,44419L,44420L,44421L,44422L,44423L,44424L,44425L,44426L,44427L,
  81285. 44428L,44429L,44430L,44431L,44432L,44433L,44434L,44435L,44436L,44437L,
  81286. 44438L,44439L,44440L,44441L,44442L,44443L,44444L,44445L,44446L,44447L,
  81287. 44448L,44449L,44450L,44451L,44452L,44453L,44454L,44455L,44456L,44457L,
  81288. 44458L,44459L,44460L,44461L,44462L,44463L,44464L,44465L,44466L,44467L,
  81289. 44468L,44469L,44470L,44471L,44472L,44473L,44474L,44475L,44476L,44477L,
  81290. 44478L,44479L,44480L,44481L,44482L,44483L,44484L,44485L,44486L,44487L,
  81291. 44488L,44489L,44490L,44491L,44492L,44493L,44494L,44495L,44496L,44497L,
  81292. 44498L,44499L,44500L,44501L,44502L,44503L,44504L,44505L,44506L,44507L,
  81293. 44508L,44509L,44510L,44511L,44512L,44513L,44514L,44515L,44516L,44517L,
  81294. 44518L,44519L,44520L,44521L,44522L,44523L,44524L,44525L,44526L,44527L,
  81295. 44528L,44529L,44530L,44531L,44532L,44533L,44534L,44535L,44536L,44537L,
  81296. 44538L,44539L,44540L,44541L,44542L,44543L,44544L,44545L,44546L,44547L,
  81297. 44548L,44549L,44550L,44551L,44552L,44553L,44554L,44555L,44556L,44557L,
  81298. 44558L,44559L,44560L,44561L,44562L,44563L,44564L,44565L,44566L,44567L,
  81299. 44568L,44569L,44570L,44571L,44572L,44573L,44574L,44575L,44576L,44577L,
  81300. 44578L,44579L,44580L,44581L,44582L,44583L,44584L,44585L,44586L,44587L,
  81301. 44588L,44589L,44590L,44591L,44592L,44593L,44594L,44595L,44596L,44597L,
  81302. 44598L,44599L,44600L,44601L,44602L,44603L,44604L,44605L,44606L,44607L,
  81303. 44608L,44609L,44610L,44611L,44612L,44613L,44614L,44615L,44616L,44617L,
  81304. 44618L,44619L,44620L,44621L,44622L,44623L,44624L,44625L,44626L,44627L,
  81305. 44628L,44629L,44630L,44631L,44632L,44633L,44634L,44635L,44636L,44637L,
  81306. 44638L,44639L,44640L,44641L,44642L,44643L,44644L,44645L,44646L,44647L,
  81307. 44648L,44649L,44650L,44651L,44652L,44653L,44654L,44655L,44656L,44657L,
  81308. 44658L,44659L,44660L,44661L,44662L,44663L,44664L,44665L,44666L,44667L,
  81309. 44668L,44669L,44670L,44671L,44672L,44673L,44674L,44675L,44676L,44677L,
  81310. 44678L,44679L,44680L,44681L,44682L,44683L,44684L,44685L,44686L,44687L,
  81311. 44688L,44689L,44690L,44691L,44692L,44693L,44694L,44695L,44696L,44697L,
  81312. 44698L,44699L,44700L,44701L,44702L,44703L,44704L,44705L,44706L,44707L,
  81313. 44708L,44709L,44710L,44711L,44712L,44713L,44714L,44715L,44716L,44717L,
  81314. 44718L,44719L,44720L,44721L,44722L,44723L,44724L,44725L,44726L,44727L,
  81315. 44728L,44729L,44730L,44731L,44732L,44733L,44734L,44735L,44736L,44737L,
  81316. 44738L,44739L,44740L,44741L,44742L,44743L,44744L,44745L,44746L,44747L,
  81317. 44748L,44749L,44750L,44751L,44752L,44753L,44754L,44755L,44756L,44757L,
  81318. 44758L,44759L,44760L,44761L,44762L,44763L,44764L,44765L,44766L,44767L,
  81319. 44768L,44769L,44770L,44771L,44772L,44773L,44774L,44775L,44776L,44777L,
  81320. 44778L,44779L,44780L,44781L,44782L,44783L,44784L,44785L,44786L,44787L,
  81321. 44788L,44789L,44790L,44791L,44792L,44793L,44794L,44795L,44796L,44797L,
  81322. 44798L,44799L,44800L,44801L,44802L,44803L,44804L,44805L,44806L,44807L,
  81323. 44808L,44809L,44810L,44811L,44812L,44813L,44814L,44815L,44816L,44817L,
  81324. 44818L,44819L,44820L,44821L,44822L,44823L,44824L,44825L,44826L,44827L,
  81325. 44828L,44829L,44830L,44831L,44832L,44833L,44834L,44835L,44836L,44837L,
  81326. 44838L,44839L,44840L,44841L,44842L,44843L,44844L,44845L,44846L,44847L,
  81327. 44848L,44849L,44850L,44851L,44852L,44853L,44854L,44855L,44856L,44857L,
  81328. 44858L,44859L,44860L,44861L,44862L,44863L,44864L,44865L,44866L,44867L,
  81329. 44868L,44869L,44870L,44871L,44872L,44873L,44874L,44875L,44876L,44877L,
  81330. 44878L,44879L,44880L,44881L,44882L,44883L,44884L,44885L,44886L,44887L,
  81331. 44888L,44889L,44890L,44891L,44892L,44893L,44894L,44895L,44896L,44897L,
  81332. 44898L,44899L,44900L,44901L,44902L,44903L,44904L,44905L,44906L,44907L,
  81333. 44908L,44909L,44910L,44911L,44912L,44913L,44914L,44915L,44916L,44917L,
  81334. 44918L,44919L,44920L,44921L,44922L,44923L,44924L,44925L,44926L,44927L,
  81335. 44928L,44929L,44930L,44931L,44932L,44933L,44934L,44935L,44936L,44937L,
  81336. 44938L,44939L,44940L,44941L,44942L,44943L,44944L,44945L,44946L,44947L,
  81337. 44948L,44949L,44950L,44951L,44952L,44953L,44954L,44955L,44956L,44957L,
  81338. 44958L,44959L,44960L,44961L,44962L,44963L,44964L,44965L,44966L,44967L,
  81339. 44968L,44969L,44970L,44971L,44972L,44973L,44974L,44975L,44976L,44977L,
  81340. 44978L,44979L,44980L,44981L,44982L,44983L,44984L,44985L,44986L,44987L,
  81341. 44988L,44989L,44990L,44991L,44992L,44993L,44994L,44995L,44996L,44997L,
  81342. 44998L,44999L,45000L,45001L,45002L,45003L,45004L,45005L,45006L,45007L,
  81343. 45008L,45009L,45010L,45011L,45012L,45013L,45014L,45015L,45016L,45017L,
  81344. 45018L,45019L,45020L,45021L,45022L,45023L,45024L,45025L,45026L,45027L,
  81345. 45028L,45029L,45030L,45031L,45032L,45033L,45034L,45035L,45036L,45037L,
  81346. 45038L,45039L,45040L,45041L,45042L,45043L,45044L,45045L,45046L,45047L,
  81347. 45048L,45049L,45050L,45051L,45052L,45053L,45054L,45055L,45056L,45057L,
  81348. 45058L,45059L,45060L,45061L,45062L,45063L,45064L,45065L,45066L,45067L,
  81349. 45068L,45069L,45070L,45071L,45072L,45073L,45074L,45075L,45076L,45077L,
  81350. 45078L,45079L,45080L,45081L,45082L,45083L,45084L,45085L,45086L,45087L,
  81351. 45088L,45089L,45090L,45091L,45092L,45093L,45094L,45095L,45096L,45097L,
  81352. 45098L,45099L,45100L,45101L,45102L,45103L,45104L,45105L,45106L,45107L,
  81353. 45108L,45109L,45110L,45111L,45112L,45113L,45114L,45115L,45116L,45117L,
  81354. 45118L,45119L,45120L,45121L,45122L,45123L,45124L,45125L,45126L,45127L,
  81355. 45128L,45129L,45130L,45131L,45132L,45133L,45134L,45135L,45136L,45137L,
  81356. 45138L,45139L,45140L,45141L,45142L,45143L,45144L,45145L,45146L,45147L,
  81357. 45148L,45149L,45150L,45151L,45152L,45153L,45154L,45155L,45156L,45157L,
  81358. 45158L,45159L,45160L,45161L,45162L,45163L,45164L,45165L,45166L,45167L,
  81359. 45168L,45169L,45170L,45171L,45172L,45173L,45174L,45175L,45176L,45177L,
  81360. 45178L,45179L,45180L,45181L,45182L,45183L,45184L,45185L,45186L,45187L,
  81361. 45188L,45189L,45190L,45191L,45192L,45193L,45194L,45195L,45196L,45197L,
  81362. 45198L,45199L,45200L,45201L,45202L,45203L,45204L,45205L,45206L,45207L,
  81363. 45208L,45209L,45210L,45211L,45212L,45213L,45214L,45215L,45216L,45217L,
  81364. 45218L,45219L,45220L,45221L,45222L,45223L,45224L,45225L,45226L,45227L,
  81365. 45228L,45229L,45230L,45231L,45232L,45233L,45234L,45235L,45236L,45237L,
  81366. 45238L,45239L,45240L,45241L,45242L,45243L,45244L,45245L,45246L,45247L,
  81367. 45248L,45249L,45250L,45251L,45252L,45253L,45254L,45255L,45256L,45257L,
  81368. 45258L,45259L,45260L,45261L,45262L,45263L,45264L,45265L,45266L,45267L,
  81369. 45268L,45269L,45270L,45271L,45272L,45273L,45274L,45275L,45276L,45277L,
  81370. 45278L,45279L,45280L,45281L,45282L,45283L,45284L,45285L,45286L,45287L,
  81371. 45288L,45289L,45290L,45291L,45292L,45293L,45294L,45295L,45296L,45297L,
  81372. 45298L,45299L,45300L,45301L,45302L,45303L,45304L,45305L,45306L,45307L,
  81373. 45308L,45309L,45310L,45311L,45312L,45313L,45314L,45315L,45316L,45317L,
  81374. 45318L,45319L,45320L,45321L,45322L,45323L,45324L,45325L,45326L,45327L,
  81375. 45328L,45329L,45330L,45331L,45332L,45333L,45334L,45335L,45336L,45337L,
  81376. 45338L,45339L,45340L,45341L,45342L,45343L,45344L,45345L,45346L,45347L,
  81377. 45348L,45349L,45350L,45351L,45352L,45353L,45354L,45355L,45356L,45357L,
  81378. 45358L,45359L,45360L,45361L,45362L,45363L,45364L,45365L,45366L,45367L,
  81379. 45368L,45369L,45370L,45371L,45372L,45373L,45374L,45375L,45376L,45377L,
  81380. 45378L,45379L,45380L,45381L,45382L,45383L,45384L,45385L,45386L,45387L,
  81381. 45388L,45389L,45390L,45391L,45392L,45393L,45394L,45395L,45396L,45397L,
  81382. 45398L,45399L,45400L,45401L,45402L,45403L,45404L,45405L,45406L,45407L,
  81383. 45408L,45409L,45410L,45411L,45412L,45413L,45414L,45415L,45416L,45417L,
  81384. 45418L,45419L,45420L,45421L,45422L,45423L,45424L,45425L,45426L,45427L,
  81385. 45428L,45429L,45430L,45431L,45432L,45433L,45434L,45435L,45436L,45437L,
  81386. 45438L,45439L,45440L,45441L,45442L,45443L,45444L,45445L,45446L,45447L,
  81387. 45448L,45449L,45450L,45451L,45452L,45453L,45454L,45455L,45456L,45457L,
  81388. 45458L,45459L,45460L,45461L,45462L,45463L,45464L,45465L,45466L,45467L,
  81389. 45468L,45469L,45470L,45471L,45472L,45473L,45474L,45475L,45476L,45477L,
  81390. 45478L,45479L,45480L,45481L,45482L,45483L,45484L,45485L,45486L,45487L,
  81391. 45488L,45489L,45490L,45491L,45492L,45493L,45494L,45495L,45496L,45497L,
  81392. 45498L,45499L,45500L,45501L,45502L,45503L,45504L,45505L,45506L,45507L,
  81393. 45508L,45509L,45510L,45511L,45512L,45513L,45514L,45515L,45516L,45517L,
  81394. 45518L,45519L,45520L,45521L,45522L,45523L,45524L,45525L,45526L,45527L,
  81395. 45528L,45529L,45530L,45531L,45532L,45533L,45534L,45535L,45536L,45537L,
  81396. 45538L,45539L,45540L,45541L,45542L,45543L,45544L,45545L,45546L,45547L,
  81397. 45548L,45549L,45550L,45551L,45552L,45553L,45554L,45555L,45556L,45557L,
  81398. 45558L,45559L,45560L,45561L,45562L,45563L,45564L,45565L,45566L,45567L,
  81399. 45568L,45569L,45570L,45571L,45572L,45573L,45574L,45575L,45576L,45577L,
  81400. 45578L,45579L,45580L,45581L,45582L,45583L,45584L,45585L,45586L,45587L,
  81401. 45588L,45589L,45590L,45591L,45592L,45593L,45594L,45595L,45596L,45597L,
  81402. 45598L,45599L,45600L,45601L,45602L,45603L,45604L,45605L,45606L,45607L,
  81403. 45608L,45609L,45610L,45611L,45612L,45613L,45614L,45615L,45616L,45617L,
  81404. 45618L,45619L,45620L,45621L,45622L,45623L,45624L,45625L,45626L,45627L,
  81405. 45628L,45629L,45630L,45631L,45632L,45633L,45634L,45635L,45636L,45637L,
  81406. 45638L,45639L,45640L,45641L,45642L,45643L,45644L,45645L,45646L,45647L,
  81407. 45648L,45649L,45650L,45651L,45652L,45653L,45654L,45655L,45656L,45657L,
  81408. 45658L,45659L,45660L,45661L,45662L,45663L,45664L,45665L,45666L,45667L,
  81409. 45668L,45669L,45670L,45671L,45672L,45673L,45674L,45675L,45676L,45677L,
  81410. 45678L,45679L,45680L,45681L,45682L,45683L,45684L,45685L,45686L,45687L,
  81411. 45688L,45689L,45690L,45691L,45692L,45693L,45694L,45695L,45696L,45697L,
  81412. 45698L,45699L,45700L,45701L,45702L,45703L,45704L,45705L,45706L,45707L,
  81413. 45708L,45709L,45710L,45711L,45712L,45713L,45714L,45715L,45716L,45717L,
  81414. 45718L,45719L,45720L,45721L,45722L,45723L,45724L,45725L,45726L,45727L,
  81415. 45728L,45729L,45730L,45731L,45732L,45733L,45734L,45735L,45736L,45737L,
  81416. 45738L,45739L,45740L,45741L,45742L,45743L,45744L,45745L,45746L,45747L,
  81417. 45748L,45749L,45750L,45751L,45752L,45753L,45754L,45755L,45756L,45757L,
  81418. 45758L,45759L,45760L,45761L,45762L,45763L,45764L,45765L,45766L,45767L,
  81419. 45768L,45769L,45770L,45771L,45772L,45773L,45774L,45775L,45776L,45777L,
  81420. 45778L,45779L,45780L,45781L,45782L,45783L,45784L,45785L,45786L,45787L,
  81421. 45788L,45789L,45790L,45791L,45792L,45793L,45794L,45795L,45796L,45797L,
  81422. 45798L,45799L,45800L,45801L,45802L,45803L,45804L,45805L,45806L,45807L,
  81423. 45808L,45809L,45810L,45811L,45812L,45813L,45814L,45815L,45816L,45817L,
  81424. 45818L,45819L,45820L,45821L,45822L,45823L,45824L,45825L,45826L,45827L,
  81425. 45828L,45829L,45830L,45831L,45832L,45833L,45834L,45835L,45836L,45837L,
  81426. 45838L,45839L,45840L,45841L,45842L,45843L,45844L,45845L,45846L,45847L,
  81427. 45848L,45849L,45850L,45851L,45852L,45853L,45854L,45855L,45856L,45857L,
  81428. 45858L,45859L,45860L,45861L,45862L,45863L,45864L,45865L,45866L,45867L,
  81429. 45868L,45869L,45870L,45871L,45872L,45873L,45874L,45875L,45876L,45877L,
  81430. 45878L,45879L,45880L,45881L,45882L,45883L,45884L,45885L,45886L,45887L,
  81431. 45888L,45889L,45890L,45891L,45892L,45893L,45894L,45895L,45896L,45897L,
  81432. 45898L,45899L,45900L,45901L,45902L,45903L,45904L,45905L,45906L,45907L,
  81433. 45908L,45909L,45910L,45911L,45912L,45913L,45914L,45915L,45916L,45917L,
  81434. 45918L,45919L,45920L,45921L,45922L,45923L,45924L,45925L,45926L,45927L,
  81435. 45928L,45929L,45930L,45931L,45932L,45933L,45934L,45935L,45936L,45937L,
  81436. 45938L,45939L,45940L,45941L,45942L,45943L,45944L,45945L,45946L,45947L,
  81437. 45948L,45949L,45950L,45951L,45952L,45953L,45954L,45955L,45956L,45957L,
  81438. 45958L,45959L,45960L,45961L,45962L,45963L,45964L,45965L,45966L,45967L,
  81439. 45968L,45969L,45970L,45971L,45972L,45973L,45974L,45975L,45976L,45977L,
  81440. 45978L,45979L,45980L,45981L,45982L,45983L,45984L,45985L,45986L,45987L,
  81441. 45988L,45989L,45990L,45991L,45992L,45993L,45994L,45995L,45996L,45997L,
  81442. 45998L,45999L,46000L,46001L,46002L,46003L,46004L,46005L,46006L,46007L,
  81443. 46008L,46009L,46010L,46011L,46012L,46013L,46014L,46015L,46016L,46017L,
  81444. 46018L,46019L,46020L,46021L,46022L,46023L,46024L,46025L,46026L,46027L,
  81445. 46028L,46029L,46030L,46031L,46032L,46033L,46034L,46035L,46036L,46037L,
  81446. 46038L,46039L,46040L,46041L,46042L,46043L,46044L,46045L,46046L,46047L,
  81447. 46048L,46049L,46050L,46051L,46052L,46053L,46054L,46055L,46056L,46057L,
  81448. 46058L,46059L,46060L,46061L,46062L,46063L,46064L,46065L,46066L,46067L,
  81449. 46068L,46069L,46070L,46071L,46072L,46073L,46074L,46075L,46076L,46077L,
  81450. 46078L,46079L,46080L,46081L,46082L,46083L,46084L,46085L,46086L,46087L,
  81451. 46088L,46089L,46090L,46091L,46092L,46093L,46094L,46095L,46096L,46097L,
  81452. 46098L,46099L,46100L,46101L,46102L,46103L,46104L,46105L,46106L,46107L,
  81453. 46108L,46109L,46110L,46111L,46112L,46113L,46114L,46115L,46116L,46117L,
  81454. 46118L,46119L,46120L,46121L,46122L,46123L,46124L,46125L,46126L,46127L,
  81455. 46128L,46129L,46130L,46131L,46132L,46133L,46134L,46135L,46136L,46137L,
  81456. 46138L,46139L,46140L,46141L,46142L,46143L,46144L,46145L,46146L,46147L,
  81457. 46148L,46149L,46150L,46151L,46152L,46153L,46154L,46155L,46156L,46157L,
  81458. 46158L,46159L,46160L,46161L,46162L,46163L,46164L,46165L,46166L,46167L,
  81459. 46168L,46169L,46170L,46171L,46172L,46173L,46174L,46175L,46176L,46177L,
  81460. 46178L,46179L,46180L,46181L,46182L,46183L,46184L,46185L,46186L,46187L,
  81461. 46188L,46189L,46190L,46191L,46192L,46193L,46194L,46195L,46196L,46197L,
  81462. 46198L,46199L,46200L,46201L,46202L,46203L,46204L,46205L,46206L,46207L,
  81463. 46208L,46209L,46210L,46211L,46212L,46213L,46214L,46215L,46216L,46217L,
  81464. 46218L,46219L,46220L,46221L,46222L,46223L,46224L,46225L,46226L,46227L,
  81465. 46228L,46229L,46230L,46231L,46232L,46233L,46234L,46235L,46236L,46237L,
  81466. 46238L,46239L,46240L,46241L,46242L,46243L,46244L,46245L,46246L,46247L,
  81467. 46248L,46249L,46250L,46251L,46252L,46253L,46254L,46255L,46256L,46257L,
  81468. 46258L,46259L,46260L,46261L,46262L,46263L,46264L,46265L,46266L,46267L,
  81469. 46268L,46269L,46270L,46271L,46272L,46273L,46274L,46275L,46276L,46277L,
  81470. 46278L,46279L,46280L,46281L,46282L,46283L,46284L,46285L,46286L,46287L,
  81471. 46288L,46289L,46290L,46291L,46292L,46293L,46294L,46295L,46296L,46297L,
  81472. 46298L,46299L,46300L,46301L,46302L,46303L,46304L,46305L,46306L,46307L,
  81473. 46308L,46309L,46310L,46311L,46312L,46313L,46314L,46315L,46316L,46317L,
  81474. 46318L,46319L,46320L,46321L,46322L,46323L,46324L,46325L,46326L,46327L,
  81475. 46328L,46329L,46330L,46331L,46332L,46333L,46334L,46335L,46336L,46337L,
  81476. 46338L,46339L,46340L,46341L,46342L,46343L,46344L,46345L,46346L,46347L,
  81477. 46348L,46349L,46350L,46351L,46352L,46353L,46354L,46355L,46356L,46357L,
  81478. 46358L,46359L,46360L,46361L,46362L,46363L,46364L,46365L,46366L,46367L,
  81479. 46368L,46369L,46370L,46371L,46372L,46373L,46374L,46375L,46376L,46377L,
  81480. 46378L,46379L,46380L,46381L,46382L,46383L,46384L,46385L,46386L,46387L,
  81481. 46388L,46389L,46390L,46391L,46392L,46393L,46394L,46395L,46396L,46397L,
  81482. 46398L,46399L,46400L,46401L,46402L,46403L,46404L,46405L,46406L,46407L,
  81483. 46408L,46409L,46410L,46411L,46412L,46413L,46414L,46415L,46416L,46417L,
  81484. 46418L,46419L,46420L,46421L,46422L,46423L,46424L,46425L,46426L,46427L,
  81485. 46428L,46429L,46430L,46431L,46432L,46433L,46434L,46435L,46436L,46437L,
  81486. 46438L,46439L,46440L,46441L,46442L,46443L,46444L,46445L,46446L,46447L,
  81487. 46448L,46449L,46450L,46451L,46452L,46453L,46454L,46455L,46456L,46457L,
  81488. 46458L,46459L,46460L,46461L,46462L,46463L,46464L,46465L,46466L,46467L,
  81489. 46468L,46469L,46470L,46471L,46472L,46473L,46474L,46475L,46476L,46477L,
  81490. 46478L,46479L,46480L,46481L,46482L,46483L,46484L,46485L,46486L,46487L,
  81491. 46488L,46489L,46490L,46491L,46492L,46493L,46494L,46495L,46496L,46497L,
  81492. 46498L,46499L,46500L,46501L,46502L,46503L,46504L,46505L,46506L,46507L,
  81493. 46508L,46509L,46510L,46511L,46512L,46513L,46514L,46515L,46516L,46517L,
  81494. 46518L,46519L,46520L,46521L,46522L,46523L,46524L,46525L,46526L,46527L,
  81495. 46528L,46529L,46530L,46531L,46532L,46533L,46534L,46535L,46536L,46537L,
  81496. 46538L,46539L,46540L,46541L,46542L,46543L,46544L,46545L,46546L,46547L,
  81497. 46548L,46549L,46550L,46551L,46552L,46553L,46554L,46555L,46556L,46557L,
  81498. 46558L,46559L,46560L,46561L,46562L,46563L,46564L,46565L,46566L,46567L,
  81499. 46568L,46569L,46570L,46571L,46572L,46573L,46574L,46575L,46576L,46577L,
  81500. 46578L,46579L,46580L,46581L,46582L,46583L,46584L,46585L,46586L,46587L,
  81501. 46588L,46589L,46590L,46591L,46592L,46593L,46594L,46595L,46596L,46597L,
  81502. 46598L,46599L,46600L,46601L,46602L,46603L,46604L,46605L,46606L,46607L,
  81503. 46608L,46609L,46610L,46611L,46612L,46613L,46614L,46615L,46616L,46617L,
  81504. 46618L,46619L,46620L,46621L,46622L,46623L,46624L,46625L,46626L,46627L,
  81505. 46628L,46629L,46630L,46631L,46632L,46633L,46634L,46635L,46636L,46637L,
  81506. 46638L,46639L,46640L,46641L,46642L,46643L,46644L,46645L,46646L,46647L,
  81507. 46648L,46649L,46650L,46651L,46652L,46653L,46654L,46655L,46656L,46657L,
  81508. 46658L,46659L,46660L,46661L,46662L,46663L,46664L,46665L,46666L,46667L,
  81509. 46668L,46669L,46670L,46671L,46672L,46673L,46674L,46675L,46676L,46677L,
  81510. 46678L,46679L,46680L,46681L,46682L,46683L,46684L,46685L,46686L,46687L,
  81511. 46688L,46689L,46690L,46691L,46692L,46693L,46694L,46695L,46696L,46697L,
  81512. 46698L,46699L,46700L,46701L,46702L,46703L,46704L,46705L,46706L,46707L,
  81513. 46708L,46709L,46710L,46711L,46712L,46713L,46714L,46715L,46716L,46717L,
  81514. 46718L,46719L,46720L,46721L,46722L,46723L,46724L,46725L,46726L,46727L,
  81515. 46728L,46729L,46730L,46731L,46732L,46733L,46734L,46735L,46736L,46737L,
  81516. 46738L,46739L,46740L,46741L,46742L,46743L,46744L,46745L,46746L,46747L,
  81517. 46748L,46749L,46750L,46751L,46752L,46753L,46754L,46755L,46756L,46757L,
  81518. 46758L,46759L,46760L,46761L,46762L,46763L,46764L,46765L,46766L,46767L,
  81519. 46768L,46769L,46770L,46771L,46772L,46773L,46774L,46775L,46776L,46777L,
  81520. 46778L,46779L,46780L,46781L,46782L,46783L,46784L,46785L,46786L,46787L,
  81521. 46788L,46789L,46790L,46791L,46792L,46793L,46794L,46795L,46796L,46797L,
  81522. 46798L,46799L,46800L,46801L,46802L,46803L,46804L,46805L,46806L,46807L,
  81523. 46808L,46809L,46810L,46811L,46812L,46813L,46814L,46815L,46816L,46817L,
  81524. 46818L,46819L,46820L,46821L,46822L,46823L,46824L,46825L,46826L,46827L,
  81525. 46828L,46829L,46830L,46831L,46832L,46833L,46834L,46835L,46836L,46837L,
  81526. 46838L,46839L,46840L,46841L,46842L,46843L,46844L,46845L,46846L,46847L,
  81527. 46848L,46849L,46850L,46851L,46852L,46853L,46854L,46855L,46856L,46857L,
  81528. 46858L,46859L,46860L,46861L,46862L,46863L,46864L,46865L,46866L,46867L,
  81529. 46868L,46869L,46870L,46871L,46872L,46873L,46874L,46875L,46876L,46877L,
  81530. 46878L,46879L,46880L,46881L,46882L,46883L,46884L,46885L,46886L,46887L,
  81531. 46888L,46889L,46890L,46891L,46892L,46893L,46894L,46895L,46896L,46897L,
  81532. 46898L,46899L,46900L,46901L,46902L,46903L,46904L,46905L,46906L,46907L,
  81533. 46908L,46909L,46910L,46911L,46912L,46913L,46914L,46915L,46916L,46917L,
  81534. 46918L,46919L,46920L,46921L,46922L,46923L,46924L,46925L,46926L,46927L,
  81535. 46928L,46929L,46930L,46931L,46932L,46933L,46934L,46935L,46936L,46937L,
  81536. 46938L,46939L,46940L,46941L,46942L,46943L,46944L,46945L,46946L,46947L,
  81537. 46948L,46949L,46950L,46951L,46952L,46953L,46954L,46955L,46956L,46957L,
  81538. 46958L,46959L,46960L,46961L,46962L,46963L,46964L,46965L,46966L,46967L,
  81539. 46968L,46969L,46970L,46971L,46972L,46973L,46974L,46975L,46976L,46977L,
  81540. 46978L,46979L,46980L,46981L,46982L,46983L,46984L,46985L,46986L,46987L,
  81541. 46988L,46989L,46990L,46991L,46992L,46993L,46994L,46995L,46996L,46997L,
  81542. 46998L,46999L,47000L,47001L,47002L,47003L,47004L,47005L,47006L,47007L,
  81543. 47008L,47009L,47010L,47011L,47012L,47013L,47014L,47015L,47016L,47017L,
  81544. 47018L,47019L,47020L,47021L,47022L,47023L,47024L,47025L,47026L,47027L,
  81545. 47028L,47029L,47030L,47031L,47032L,47033L,47034L,47035L,47036L,47037L,
  81546. 47038L,47039L,47040L,47041L,47042L,47043L,47044L,47045L,47046L,47047L,
  81547. 47048L,47049L,47050L,47051L,47052L,47053L,47054L,47055L,47056L,47057L,
  81548. 47058L,47059L,47060L,47061L,47062L,47063L,47064L,47065L,47066L,47067L,
  81549. 47068L,47069L,47070L,47071L,47072L,47073L,47074L,47075L,47076L,47077L,
  81550. 47078L,47079L,47080L,47081L,47082L,47083L,47084L,47085L,47086L,47087L,
  81551. 47088L,47089L,47090L,47091L,47092L,47093L,47094L,47095L,47096L,47097L,
  81552. 47098L,47099L,47100L,47101L,47102L,47103L,47104L,47105L,47106L,47107L,
  81553. 47108L,47109L,47110L,47111L,47112L,47113L,47114L,47115L,47116L,47117L,
  81554. 47118L,47119L,47120L,47121L,47122L,47123L,47124L,47125L,47126L,47127L,
  81555. 47128L,47129L,47130L,47131L,47132L,47133L,47134L,47135L,47136L,47137L,
  81556. 47138L,47139L,47140L,47141L,47142L,47143L,47144L,47145L,47146L,47147L,
  81557. 47148L,47149L,47150L,47151L,47152L,47153L,47154L,47155L,47156L,47157L,
  81558. 47158L,47159L,47160L,47161L,47162L,47163L,47164L,47165L,47166L,47167L,
  81559. 47168L,47169L,47170L,47171L,47172L,47173L,47174L,47175L,47176L,47177L,
  81560. 47178L,47179L,47180L,47181L,47182L,47183L,47184L,47185L,47186L,47187L,
  81561. 47188L,47189L,47190L,47191L,47192L,47193L,47194L,47195L,47196L,47197L,
  81562. 47198L,47199L,47200L,47201L,47202L,47203L,47204L,47205L,47206L,47207L,
  81563. 47208L,47209L,47210L,47211L,47212L,47213L,47214L,47215L,47216L,47217L,
  81564. 47218L,47219L,47220L,47221L,47222L,47223L,47224L,47225L,47226L,47227L,
  81565. 47228L,47229L,47230L,47231L,47232L,47233L,47234L,47235L,47236L,47237L,
  81566. 47238L,47239L,47240L,47241L,47242L,47243L,47244L,47245L,47246L,47247L,
  81567. 47248L,47249L,47250L,47251L,47252L,47253L,47254L,47255L,47256L,47257L,
  81568. 47258L,47259L,47260L,47261L,47262L,47263L,47264L,47265L,47266L,47267L,
  81569. 47268L,47269L,47270L,47271L,47272L,47273L,47274L,47275L,47276L,47277L,
  81570. 47278L,47279L,47280L,47281L,47282L,47283L,47284L,47285L,47286L,47287L,
  81571. 47288L,47289L,47290L,47291L,47292L,47293L,47294L,47295L,47296L,47297L,
  81572. 47298L,47299L,47300L,47301L,47302L,47303L,47304L,47305L,47306L,47307L,
  81573. 47308L,47309L,47310L,47311L,47312L,47313L,47314L,47315L,47316L,47317L,
  81574. 47318L,47319L,47320L,47321L,47322L,47323L,47324L,47325L,47326L,47327L,
  81575. 47328L,47329L,47330L,47331L,47332L,47333L,47334L,47335L,47336L,47337L,
  81576. 47338L,47339L,47340L,47341L,47342L,47343L,47344L,47345L,47346L,47347L,
  81577. 47348L,47349L,47350L,47351L,47352L,47353L,47354L,47355L,47356L,47357L,
  81578. 47358L,47359L,47360L,47361L,47362L,47363L,47364L,47365L,47366L,47367L,
  81579. 47368L,47369L,47370L,47371L,47372L,47373L,47374L,47375L,47376L,47377L,
  81580. 47378L,47379L,47380L,47381L,47382L,47383L,47384L,47385L,47386L,47387L,
  81581. 47388L,47389L,47390L,47391L,47392L,47393L,47394L,47395L,47396L,47397L,
  81582. 47398L,47399L,47400L,47401L,47402L,47403L,47404L,47405L,47406L,47407L,
  81583. 47408L,47409L,47410L,47411L,47412L,47413L,47414L,47415L,47416L,47417L,
  81584. 47418L,47419L,47420L,47421L,47422L,47423L,47424L,47425L,47426L,47427L,
  81585. 47428L,47429L,47430L,47431L,47432L,47433L,47434L,47435L,47436L,47437L,
  81586. 47438L,47439L,47440L,47441L,47442L,47443L,47444L,47445L,47446L,47447L,
  81587. 47448L,47449L,47450L,47451L,47452L,47453L,47454L,47455L,47456L,47457L,
  81588. 47458L,47459L,47460L,47461L,47462L,47463L,47464L,47465L,47466L,47467L,
  81589. 47468L,47469L,47470L,47471L,47472L,47473L,47474L,47475L,47476L,47477L,
  81590. 47478L,47479L,47480L,47481L,47482L,47483L,47484L,47485L,47486L,47487L,
  81591. 47488L,47489L,47490L,47491L,47492L,47493L,47494L,47495L,47496L,47497L,
  81592. 47498L,47499L,47500L,47501L,47502L,47503L,47504L,47505L,47506L,47507L,
  81593. 47508L,47509L,47510L,47511L,47512L,47513L,47514L,47515L,47516L,47517L,
  81594. 47518L,47519L,47520L,47521L,47522L,47523L,47524L,47525L,47526L,47527L,
  81595. 47528L,47529L,47530L,47531L,47532L,47533L,47534L,47535L,47536L,47537L,
  81596. 47538L,47539L,47540L,47541L,47542L,47543L,47544L,47545L,47546L,47547L,
  81597. 47548L,47549L,47550L,47551L,47552L,47553L,47554L,47555L,47556L,47557L,
  81598. 47558L,47559L,47560L,47561L,47562L,47563L,47564L,47565L,47566L,47567L,
  81599. 47568L,47569L,47570L,47571L,47572L,47573L,47574L,47575L,47576L,47577L,
  81600. 47578L,47579L,47580L,47581L,47582L,47583L,47584L,47585L,47586L,47587L,
  81601. 47588L,47589L,47590L,47591L,47592L,47593L,47594L,47595L,47596L,47597L,
  81602. 47598L,47599L,47600L,47601L,47602L,47603L,47604L,47605L,47606L,47607L,
  81603. 47608L,47609L,47610L,47611L,47612L,47613L,47614L,47615L,47616L,47617L,
  81604. 47618L,47619L,47620L,47621L,47622L,47623L,47624L,47625L,47626L,47627L,
  81605. 47628L,47629L,47630L,47631L,47632L,47633L,47634L,47635L,47636L,47637L,
  81606. 47638L,47639L,47640L,47641L,47642L,47643L,47644L,47645L,47646L,47647L,
  81607. 47648L,47649L,47650L,47651L,47652L,47653L,47654L,47655L,47656L,47657L,
  81608. 47658L,47659L,47660L,47661L,47662L,47663L,47664L,47665L,47666L,47667L,
  81609. 47668L,47669L,47670L,47671L,47672L,47673L,47674L,47675L,47676L,47677L,
  81610. 47678L,47679L,47680L,47681L,47682L,47683L,47684L,47685L,47686L,47687L,
  81611. 47688L,47689L,47690L,47691L,47692L,47693L,47694L,47695L,47696L,47697L,
  81612. 47698L,47699L,47700L,47701L,47702L,47703L,47704L,47705L,47706L,47707L,
  81613. 47708L,47709L,47710L,47711L,47712L,47713L,47714L,47715L,47716L,47717L,
  81614. 47718L,47719L,47720L,47721L,47722L,47723L,47724L,47725L,47726L,47727L,
  81615. 47728L,47729L,47730L,47731L,47732L,47733L,47734L,47735L,47736L,47737L,
  81616. 47738L,47739L,47740L,47741L,47742L,47743L,47744L,47745L,47746L,47747L,
  81617. 47748L,47749L,47750L,47751L,47752L,47753L,47754L,47755L,47756L,47757L,
  81618. 47758L,47759L,47760L,47761L,47762L,47763L,47764L,47765L,47766L,47767L,
  81619. 47768L,47769L,47770L,47771L,47772L,47773L,47774L,47775L,47776L,47777L,
  81620. 47778L,47779L,47780L,47781L,47782L,47783L,47784L,47785L,47786L,47787L,
  81621. 47788L,47789L,47790L,47791L,47792L,47793L,47794L,47795L,47796L,47797L,
  81622. 47798L,47799L,47800L,47801L,47802L,47803L,47804L,47805L,47806L,47807L,
  81623. 47808L,47809L,47810L,47811L,47812L,47813L,47814L,47815L,47816L,47817L,
  81624. 47818L,47819L,47820L,47821L,47822L,47823L,47824L,47825L,47826L,47827L,
  81625. 47828L,47829L,47830L,47831L,47832L,47833L,47834L,47835L,47836L,47837L,
  81626. 47838L,47839L,47840L,47841L,47842L,47843L,47844L,47845L,47846L,47847L,
  81627. 47848L,47849L,47850L,47851L,47852L,47853L,47854L,47855L,47856L,47857L,
  81628. 47858L,47859L,47860L,47861L,47862L,47863L,47864L,47865L,47866L,47867L,
  81629. 47868L,47869L,47870L,47871L,47872L,47873L,47874L,47875L,47876L,47877L,
  81630. 47878L,47879L,47880L,47881L,47882L,47883L,47884L,47885L,47886L,47887L,
  81631. 47888L,47889L,47890L,47891L,47892L,47893L,47894L,47895L,47896L,47897L,
  81632. 47898L,47899L,47900L,47901L,47902L,47903L,47904L,47905L,47906L,47907L,
  81633. 47908L,47909L,47910L,47911L,47912L,47913L,47914L,47915L,47916L,47917L,
  81634. 47918L,47919L,47920L,47921L,47922L,47923L,47924L,47925L,47926L,47927L,
  81635. 47928L,47929L,47930L,47931L,47932L,47933L,47934L,47935L,47936L,47937L,
  81636. 47938L,47939L,47940L,47941L,47942L,47943L,47944L,47945L,47946L,47947L,
  81637. 47948L,47949L,47950L,47951L,47952L,47953L,47954L,47955L,47956L,47957L,
  81638. 47958L,47959L,47960L,47961L,47962L,47963L,47964L,47965L,47966L,47967L,
  81639. 47968L,47969L,47970L,47971L,47972L,47973L,47974L,47975L,47976L,47977L,
  81640. 47978L,47979L,47980L,47981L,47982L,47983L,47984L,47985L,47986L,47987L,
  81641. 47988L,47989L,47990L,47991L,47992L,47993L,47994L,47995L,47996L,47997L,
  81642. 47998L,47999L,48000L,48001L,48002L,48003L,48004L,48005L,48006L,48007L,
  81643. 48008L,48009L,48010L,48011L,48012L,48013L,48014L,48015L,48016L,48017L,
  81644. 48018L,48019L,48020L,48021L,48022L,48023L,48024L,48025L,48026L,48027L,
  81645. 48028L,48029L,48030L,48031L,48032L,48033L,48034L,48035L,48036L,48037L,
  81646. 48038L,48039L,48040L,48041L,48042L,48043L,48044L,48045L,48046L,48047L,
  81647. 48048L,48049L,48050L,48051L,48052L,48053L,48054L,48055L,48056L,48057L,
  81648. 48058L,48059L,48060L,48061L,48062L,48063L,48064L,48065L,48066L,48067L,
  81649. 48068L,48069L,48070L,48071L,48072L,48073L,48074L,48075L,48076L,48077L,
  81650. 48078L,48079L,48080L,48081L,48082L,48083L,48084L,48085L,48086L,48087L,
  81651. 48088L,48089L,48090L,48091L,48092L,48093L,48094L,48095L,48096L,48097L,
  81652. 48098L,48099L,48100L,48101L,48102L,48103L,48104L,48105L,48106L,48107L,
  81653. 48108L,48109L,48110L,48111L,48112L,48113L,48114L,48115L,48116L,48117L,
  81654. 48118L,48119L,48120L,48121L,48122L,48123L,48124L,48125L,48126L,48127L,
  81655. 48128L,48129L,48130L,48131L,48132L,48133L,48134L,48135L,48136L,48137L,
  81656. 48138L,48139L,48140L,48141L,48142L,48143L,48144L,48145L,48146L,48147L,
  81657. 48148L,48149L,48150L,48151L,48152L,48153L,48154L,48155L,48156L,48157L,
  81658. 48158L,48159L,48160L,48161L,48162L,48163L,48164L,48165L,48166L,48167L,
  81659. 48168L,48169L,48170L,48171L,48172L,48173L,48174L,48175L,48176L,48177L,
  81660. 48178L,48179L,48180L,48181L,48182L,48183L,48184L,48185L,48186L,48187L,
  81661. 48188L,48189L,48190L,48191L,48192L,48193L,48194L,48195L,48196L,48197L,
  81662. 48198L,48199L,48200L,48201L,48202L,48203L,48204L,48205L,48206L,48207L,
  81663. 48208L,48209L,48210L,48211L,48212L,48213L,48214L,48215L,48216L,48217L,
  81664. 48218L,48219L,48220L,48221L,48222L,48223L,48224L,48225L,48226L,48227L,
  81665. 48228L,48229L,48230L,48231L,48232L,48233L,48234L,48235L,48236L,48237L,
  81666. 48238L,48239L,48240L,48241L,48242L,48243L,48244L,48245L,48246L,48247L,
  81667. 48248L,48249L,48250L,48251L,48252L,48253L,48254L,48255L,48256L,48257L,
  81668. 48258L,48259L,48260L,48261L,48262L,48263L,48264L,48265L,48266L,48267L,
  81669. 48268L,48269L,48270L,48271L,48272L,48273L,48274L,48275L,48276L,48277L,
  81670. 48278L,48279L,48280L,48281L,48282L,48283L,48284L,48285L,48286L,48287L,
  81671. 48288L,48289L,48290L,48291L,48292L,48293L,48294L,48295L,48296L,48297L,
  81672. 48298L,48299L,48300L,48301L,48302L,48303L,48304L,48305L,48306L,48307L,
  81673. 48308L,48309L,48310L,48311L,48312L,48313L,48314L,48315L,48316L,48317L,
  81674. 48318L,48319L,48320L,48321L,48322L,48323L,48324L,48325L,48326L,48327L,
  81675. 48328L,48329L,48330L,48331L,48332L,48333L,48334L,48335L,48336L,48337L,
  81676. 48338L,48339L,48340L,48341L,48342L,48343L,48344L,48345L,48346L,48347L,
  81677. 48348L,48349L,48350L,48351L,48352L,48353L,48354L,48355L,48356L,48357L,
  81678. 48358L,48359L,48360L,48361L,48362L,48363L,48364L,48365L,48366L,48367L,
  81679. 48368L,48369L,48370L,48371L,48372L,48373L,48374L,48375L,48376L,48377L,
  81680. 48378L,48379L,48380L,48381L,48382L,48383L,48384L,48385L,48386L,48387L,
  81681. 48388L,48389L,48390L,48391L,48392L,48393L,48394L,48395L,48396L,48397L,
  81682. 48398L,48399L,48400L,48401L,48402L,48403L,48404L,48405L,48406L,48407L,
  81683. 48408L,48409L,48410L,48411L,48412L,48413L,48414L,48415L,48416L,48417L,
  81684. 48418L,48419L,48420L,48421L,48422L,48423L,48424L,48425L,48426L,48427L,
  81685. 48428L,48429L,48430L,48431L,48432L,48433L,48434L,48435L,48436L,48437L,
  81686. 48438L,48439L,48440L,48441L,48442L,48443L,48444L,48445L,48446L,48447L,
  81687. 48448L,48449L,48450L,48451L,48452L,48453L,48454L,48455L,48456L,48457L,
  81688. 48458L,48459L,48460L,48461L,48462L,48463L,48464L,48465L,48466L,48467L,
  81689. 48468L,48469L,48470L,48471L,48472L,48473L,48474L,48475L,48476L,48477L,
  81690. 48478L,48479L,48480L,48481L,48482L,48483L,48484L,48485L,48486L,48487L,
  81691. 48488L,48489L,48490L,48491L,48492L,48493L,48494L,48495L,48496L,48497L,
  81692. 48498L,48499L,48500L,48501L,48502L,48503L,48504L,48505L,48506L,48507L,
  81693. 48508L,48509L,48510L,48511L,48512L,48513L,48514L,48515L,48516L,48517L,
  81694. 48518L,48519L,48520L,48521L,48522L,48523L,48524L,48525L,48526L,48527L,
  81695. 48528L,48529L,48530L,48531L,48532L,48533L,48534L,48535L,48536L,48537L,
  81696. 48538L,48539L,48540L,48541L,48542L,48543L,48544L,48545L,48546L,48547L,
  81697. 48548L,48549L,48550L,48551L,48552L,48553L,48554L,48555L,48556L,48557L,
  81698. 48558L,48559L,48560L,48561L,48562L,48563L,48564L,48565L,48566L,48567L,
  81699. 48568L,48569L,48570L,48571L,48572L,48573L,48574L,48575L,48576L,48577L,
  81700. 48578L,48579L,48580L,48581L,48582L,48583L,48584L,48585L,48586L,48587L,
  81701. 48588L,48589L,48590L,48591L,48592L,48593L,48594L,48595L,48596L,48597L,
  81702. 48598L,48599L,48600L,48601L,48602L,48603L,48604L,48605L,48606L,48607L,
  81703. 48608L,48609L,48610L,48611L,48612L,48613L,48614L,48615L,48616L,48617L,
  81704. 48618L,48619L,48620L,48621L,48622L,48623L,48624L,48625L,48626L,48627L,
  81705. 48628L,48629L,48630L,48631L,48632L,48633L,48634L,48635L,48636L,48637L,
  81706. 48638L,48639L,48640L,48641L,48642L,48643L,48644L,48645L,48646L,48647L,
  81707. 48648L,48649L,48650L,48651L,48652L,48653L,48654L,48655L,48656L,48657L,
  81708. 48658L,48659L,48660L,48661L,48662L,48663L,48664L,48665L,48666L,48667L,
  81709. 48668L,48669L,48670L,48671L,48672L,48673L,48674L,48675L,48676L,48677L,
  81710. 48678L,48679L,48680L,48681L,48682L,48683L,48684L,48685L,48686L,48687L,
  81711. 48688L,48689L,48690L,48691L,48692L,48693L,48694L,48695L,48696L,48697L,
  81712. 48698L,48699L,48700L,48701L,48702L,48703L,48704L,48705L,48706L,48707L,
  81713. 48708L,48709L,48710L,48711L,48712L,48713L,48714L,48715L,48716L,48717L,
  81714. 48718L,48719L,48720L,48721L,48722L,48723L,48724L,48725L,48726L,48727L,
  81715. 48728L,48729L,48730L,48731L,48732L,48733L,48734L,48735L,48736L,48737L,
  81716. 48738L,48739L,48740L,48741L,48742L,48743L,48744L,48745L,48746L,48747L,
  81717. 48748L,48749L,48750L,48751L,48752L,48753L,48754L,48755L,48756L,48757L,
  81718. 48758L,48759L,48760L,48761L,48762L,48763L,48764L,48765L,48766L,48767L,
  81719. 48768L,48769L,48770L,48771L,48772L,48773L,48774L,48775L,48776L,48777L,
  81720. 48778L,48779L,48780L,48781L,48782L,48783L,48784L,48785L,48786L,48787L,
  81721. 48788L,48789L,48790L,48791L,48792L,48793L,48794L,48795L,48796L,48797L,
  81722. 48798L,48799L,48800L,48801L,48802L,48803L,48804L,48805L,48806L,48807L,
  81723. 48808L,48809L,48810L,48811L,48812L,48813L,48814L,48815L,48816L,48817L,
  81724. 48818L,48819L,48820L,48821L,48822L,48823L,48824L,48825L,48826L,48827L,
  81725. 48828L,48829L,48830L,48831L,48832L,48833L,48834L,48835L,48836L,48837L,
  81726. 48838L,48839L,48840L,48841L,48842L,48843L,48844L,48845L,48846L,48847L,
  81727. 48848L,48849L,48850L,48851L,48852L,48853L,48854L,48855L,48856L,48857L,
  81728. 48858L,48859L,48860L,48861L,48862L,48863L,48864L,48865L,48866L,48867L,
  81729. 48868L,48869L,48870L,48871L,48872L,48873L,48874L,48875L,48876L,48877L,
  81730. 48878L,48879L,48880L,48881L,48882L,48883L,48884L,48885L,48886L,48887L,
  81731. 48888L,48889L,48890L,48891L,48892L,48893L,48894L,48895L,48896L,48897L,
  81732. 48898L,48899L,48900L,48901L,48902L,48903L,48904L,48905L,48906L,48907L,
  81733. 48908L,48909L,48910L,48911L,48912L,48913L,48914L,48915L,48916L,48917L,
  81734. 48918L,48919L,48920L,48921L,48922L,48923L,48924L,48925L,48926L,48927L,
  81735. 48928L,48929L,48930L,48931L,48932L,48933L,48934L,48935L,48936L,48937L,
  81736. 48938L,48939L,48940L,48941L,48942L,48943L,48944L,48945L,48946L,48947L,
  81737. 48948L,48949L,48950L,48951L,48952L,48953L,48954L,48955L,48956L,48957L,
  81738. 48958L,48959L,48960L,48961L,48962L,48963L,48964L,48965L,48966L,48967L,
  81739. 48968L,48969L,48970L,48971L,48972L,48973L,48974L,48975L,48976L,48977L,
  81740. 48978L,48979L,48980L,48981L,48982L,48983L,48984L,48985L,48986L,48987L,
  81741. 48988L,48989L,48990L,48991L,48992L,48993L,48994L,48995L,48996L,48997L,
  81742. 48998L,48999L,49000L,49001L,49002L,49003L,49004L,49005L,49006L,49007L,
  81743. 49008L,49009L,49010L,49011L,49012L,49013L,49014L,49015L,49016L,49017L,
  81744. 49018L,49019L,49020L,49021L,49022L,49023L,49024L,49025L,49026L,49027L,
  81745. 49028L,49029L,49030L,49031L,49032L,49033L,49034L,49035L,49036L,49037L,
  81746. 49038L,49039L,49040L,49041L,49042L,49043L,49044L,49045L,49046L,49047L,
  81747. 49048L,49049L,49050L,49051L,49052L,49053L,49054L,49055L,49056L,49057L,
  81748. 49058L,49059L,49060L,49061L,49062L,49063L,49064L,49065L,49066L,49067L,
  81749. 49068L,49069L,49070L,49071L,49072L,49073L,49074L,49075L,49076L,49077L,
  81750. 49078L,49079L,49080L,49081L,49082L,49083L,49084L,49085L,49086L,49087L,
  81751. 49088L,49089L,49090L,49091L,49092L,49093L,49094L,49095L,49096L,49097L,
  81752. 49098L,49099L,49100L,49101L,49102L,49103L,49104L,49105L,49106L,49107L,
  81753. 49108L,49109L,49110L,49111L,49112L,49113L,49114L,49115L,49116L,49117L,
  81754. 49118L,49119L,49120L,49121L,49122L,49123L,49124L,49125L,49126L,49127L,
  81755. 49128L,49129L,49130L,49131L,49132L,49133L,49134L,49135L,49136L,49137L,
  81756. 49138L,49139L,49140L,49141L,49142L,49143L,49144L,49145L,49146L,49147L,
  81757. 49148L,49149L,49150L,49151L,49152L,49153L,49154L,49155L,49156L,49157L,
  81758. 49158L,49159L,49160L,49161L,49162L,49163L,49164L,49165L,49166L,49167L,
  81759. 49168L,49169L,49170L,49171L,49172L,49173L,49174L,49175L,49176L,49177L,
  81760. 49178L,49179L,49180L,49181L,49182L,49183L,49184L,49185L,49186L,49187L,
  81761. 49188L,49189L,49190L,49191L,49192L,49193L,49194L,49195L,49196L,49197L,
  81762. 49198L,49199L,49200L,49201L,49202L,49203L,49204L,49205L,49206L,49207L,
  81763. 49208L,49209L,49210L,49211L,49212L,49213L,49214L,49215L,49216L,49217L,
  81764. 49218L,49219L,49220L,49221L,49222L,49223L,49224L,49225L,49226L,49227L,
  81765. 49228L,49229L,49230L,49231L,49232L,49233L,49234L,49235L,49236L,49237L,
  81766. 49238L,49239L,49240L,49241L,49242L,49243L,49244L,49245L,49246L,49247L,
  81767. 49248L,49249L,49250L,49251L,49252L,49253L,49254L,49255L,49256L,49257L,
  81768. 49258L,49259L,49260L,49261L,49262L,49263L,49264L,49265L,49266L,49267L,
  81769. 49268L,49269L,49270L,49271L,49272L,49273L,49274L,49275L,49276L,49277L,
  81770. 49278L,49279L,49280L,49281L,49282L,49283L,49284L,49285L,49286L,49287L,
  81771. 49288L,49289L,49290L,49291L,49292L,49293L,49294L,49295L,49296L,49297L,
  81772. 49298L,49299L,49300L,49301L,49302L,49303L,49304L,49305L,49306L,49307L,
  81773. 49308L,49309L,49310L,49311L,49312L,49313L,49314L,49315L,49316L,49317L,
  81774. 49318L,49319L,49320L,49321L,49322L,49323L,49324L,49325L,49326L,49327L,
  81775. 49328L,49329L,49330L,49331L,49332L,49333L,49334L,49335L,49336L,49337L,
  81776. 49338L,49339L,49340L,49341L,49342L,49343L,49344L,49345L,49346L,49347L,
  81777. 49348L,49349L,49350L,49351L,49352L,49353L,49354L,49355L,49356L,49357L,
  81778. 49358L,49359L,49360L,49361L,49362L,49363L,49364L,49365L,49366L,49367L,
  81779. 49368L,49369L,49370L,49371L,49372L,49373L,49374L,49375L,49376L,49377L,
  81780. 49378L,49379L,49380L,49381L,49382L,49383L,49384L,49385L,49386L,49387L,
  81781. 49388L,49389L,49390L,49391L,49392L,49393L,49394L,49395L,49396L,49397L,
  81782. 49398L,49399L,49400L,49401L,49402L,49403L,49404L,49405L,49406L,49407L,
  81783. 49408L,49409L,49410L,49411L,49412L,49413L,49414L,49415L,49416L,49417L,
  81784. 49418L,49419L,49420L,49421L,49422L,49423L,49424L,49425L,49426L,49427L,
  81785. 49428L,49429L,49430L,49431L,49432L,49433L,49434L,49435L,49436L,49437L,
  81786. 49438L,49439L,49440L,49441L,49442L,49443L,49444L,49445L,49446L,49447L,
  81787. 49448L,49449L,49450L,49451L,49452L,49453L,49454L,49455L,49456L,49457L,
  81788. 49458L,49459L,49460L,49461L,49462L,49463L,49464L,49465L,49466L,49467L,
  81789. 49468L,49469L,49470L,49471L,49472L,49473L,49474L,49475L,49476L,49477L,
  81790. 49478L,49479L,49480L,49481L,49482L,49483L,49484L,49485L,49486L,49487L,
  81791. 49488L,49489L,49490L,49491L,49492L,49493L,49494L,49495L,49496L,49497L,
  81792. 49498L,49499L,49500L,49501L,49502L,49503L,49504L,49505L,49506L,49507L,
  81793. 49508L,49509L,49510L,49511L,49512L,49513L,49514L,49515L,49516L,49517L,
  81794. 49518L,49519L,49520L,49521L,49522L,49523L,49524L,49525L,49526L,49527L,
  81795. 49528L,49529L,49530L,49531L,49532L,49533L,49534L,49535L,49536L,49537L,
  81796. 49538L,49539L,49540L,49541L,49542L,49543L,49544L,49545L,49546L,49547L,
  81797. 49548L,49549L,49550L,49551L,49552L,49553L,49554L,49555L,49556L,49557L,
  81798. 49558L,49559L,49560L,49561L,49562L,49563L,49564L,49565L,49566L,49567L,
  81799. 49568L,49569L,49570L,49571L,49572L,49573L,49574L,49575L,49576L,49577L,
  81800. 49578L,49579L,49580L,49581L,49582L,49583L,49584L,49585L,49586L,49587L,
  81801. 49588L,49589L,49590L,49591L,49592L,49593L,49594L,49595L,49596L,49597L,
  81802. 49598L,49599L,49600L,49601L,49602L,49603L,49604L,49605L,49606L,49607L,
  81803. 49608L,49609L,49610L,49611L,49612L,49613L,49614L,49615L,49616L,49617L,
  81804. 49618L,49619L,49620L,49621L,49622L,49623L,49624L,49625L,49626L,49627L,
  81805. 49628L,49629L,49630L,49631L,49632L,49633L,49634L,49635L,49636L,49637L,
  81806. 49638L,49639L,49640L,49641L,49642L,49643L,49644L,49645L,49646L,49647L,
  81807. 49648L,49649L,49650L,49651L,49652L,49653L,49654L,49655L,49656L,49657L,
  81808. 49658L,49659L,49660L,49661L,49662L,49663L,49664L,49665L,49666L,49667L,
  81809. 49668L,49669L,49670L,49671L,49672L,49673L,49674L,49675L,49676L,49677L,
  81810. 49678L,49679L,49680L,49681L,49682L,49683L,49684L,49685L,49686L,49687L,
  81811. 49688L,49689L,49690L,49691L,49692L,49693L,49694L,49695L,49696L,49697L,
  81812. 49698L,49699L,49700L,49701L,49702L,49703L,49704L,49705L,49706L,49707L,
  81813. 49708L,49709L,49710L,49711L,49712L,49713L,49714L,49715L,49716L,49717L,
  81814. 49718L,49719L,49720L,49721L,49722L,49723L,49724L,49725L,49726L,49727L,
  81815. 49728L,49729L,49730L,49731L,49732L,49733L,49734L,49735L,49736L,49737L,
  81816. 49738L,49739L,49740L,49741L,49742L,49743L,49744L,49745L,49746L,49747L,
  81817. 49748L,49749L,49750L,49751L,49752L,49753L,49754L,49755L,49756L,49757L,
  81818. 49758L,49759L,49760L,49761L,49762L,49763L,49764L,49765L,49766L,49767L,
  81819. 49768L,49769L,49770L,49771L,49772L,49773L,49774L,49775L,49776L,49777L,
  81820. 49778L,49779L,49780L,49781L,49782L,49783L,49784L,49785L,49786L,49787L,
  81821. 49788L,49789L,49790L,49791L,49792L,49793L,49794L,49795L,49796L,49797L,
  81822. 49798L,49799L,49800L,49801L,49802L,49803L,49804L,49805L,49806L,49807L,
  81823. 49808L,49809L,49810L,49811L,49812L,49813L,49814L,49815L,49816L,49817L,
  81824. 49818L,49819L,49820L,49821L,49822L,49823L,49824L,49825L,49826L,49827L,
  81825. 49828L,49829L,49830L,49831L,49832L,49833L,49834L,49835L,49836L,49837L,
  81826. 49838L,49839L,49840L,49841L,49842L,49843L,49844L,49845L,49846L,49847L,
  81827. 49848L,49849L,49850L,49851L,49852L,49853L,49854L,49855L,49856L,49857L,
  81828. 49858L,49859L,49860L,49861L,49862L,49863L,49864L,49865L,49866L,49867L,
  81829. 49868L,49869L,49870L,49871L,49872L,49873L,49874L,49875L,49876L,49877L,
  81830. 49878L,49879L,49880L,49881L,49882L,49883L,49884L,49885L,49886L,49887L,
  81831. 49888L,49889L,49890L,49891L,49892L,49893L,49894L,49895L,49896L,49897L,
  81832. 49898L,49899L,49900L,49901L,49902L,49903L,49904L,49905L,49906L,49907L,
  81833. 49908L,49909L,49910L,49911L,49912L,49913L,49914L,49915L,49916L,49917L,
  81834. 49918L,49919L,49920L,49921L,49922L,49923L,49924L,49925L,49926L,49927L,
  81835. 49928L,49929L,49930L,49931L,49932L,49933L,49934L,49935L,49936L,49937L,
  81836. 49938L,49939L,49940L,49941L,49942L,49943L,49944L,49945L,49946L,49947L,
  81837. 49948L,49949L,49950L,49951L,49952L,49953L,49954L,49955L,49956L,49957L,
  81838. 49958L,49959L,49960L,49961L,49962L,49963L,49964L,49965L,49966L,49967L,
  81839. 49968L,49969L,49970L,49971L,49972L,49973L,49974L,49975L,49976L,49977L,
  81840. 49978L,49979L,49980L,49981L,49982L,49983L,49984L,49985L,49986L,49987L,
  81841. 49988L,49989L,49990L,49991L,49992L,49993L,49994L,49995L,49996L,49997L,
  81842. 49998L,49999L,50000L,50001L,50002L,50003L,50004L,50005L,50006L,50007L,
  81843. 50008L,50009L,50010L,50011L,50012L,50013L,50014L,50015L,50016L,50017L,
  81844. 50018L,50019L,50020L,50021L,50022L,50023L,50024L,50025L,50026L,50027L,
  81845. 50028L,50029L,50030L,50031L,50032L,50033L,50034L,50035L,50036L,50037L,
  81846. 50038L,50039L,50040L,50041L,50042L,50043L,50044L,50045L,50046L,50047L,
  81847. 50048L,50049L,50050L,50051L,50052L,50053L,50054L,50055L,50056L,50057L,
  81848. 50058L,50059L,50060L,50061L,50062L,50063L,50064L,50065L,50066L,50067L,
  81849. 50068L,50069L,50070L,50071L,50072L,50073L,50074L,50075L,50076L,50077L,
  81850. 50078L,50079L,50080L,50081L,50082L,50083L,50084L,50085L,50086L,50087L,
  81851. 50088L,50089L,50090L,50091L,50092L,50093L,50094L,50095L,50096L,50097L,
  81852. 50098L,50099L,50100L,50101L,50102L,50103L,50104L,50105L,50106L,50107L,
  81853. 50108L,50109L,50110L,50111L,50112L,50113L,50114L,50115L,50116L,50117L,
  81854. 50118L,50119L,50120L,50121L,50122L,50123L,50124L,50125L,50126L,50127L,
  81855. 50128L,50129L,50130L,50131L,50132L,50133L,50134L,50135L,50136L,50137L,
  81856. 50138L,50139L,50140L,50141L,50142L,50143L,50144L,50145L,50146L,50147L,
  81857. 50148L,50149L,50150L,50151L,50152L,50153L,50154L,50155L,50156L,50157L,
  81858. 50158L,50159L,50160L,50161L,50162L,50163L,50164L,50165L,50166L,50167L,
  81859. 50168L,50169L,50170L,50171L,50172L,50173L,50174L,50175L,50176L,50177L,
  81860. 50178L,50179L,50180L,50181L,50182L,50183L,50184L,50185L,50186L,50187L,
  81861. 50188L,50189L,50190L,50191L,50192L,50193L,50194L,50195L,50196L,50197L,
  81862. 50198L,50199L,50200L,50201L,50202L,50203L,50204L,50205L,50206L,50207L,
  81863. 50208L,50209L,50210L,50211L,50212L,50213L,50214L,50215L,50216L,50217L,
  81864. 50218L,50219L,50220L,50221L,50222L,50223L,50224L,50225L,50226L,50227L,
  81865. 50228L,50229L,50230L,50231L,50232L,50233L,50234L,50235L,50236L,50237L,
  81866. 50238L,50239L,50240L,50241L,50242L,50243L,50244L,50245L,50246L,50247L,
  81867. 50248L,50249L,50250L,50251L,50252L,50253L,50254L,50255L,50256L,50257L,
  81868. 50258L,50259L,50260L,50261L,50262L,50263L,50264L,50265L,50266L,50267L,
  81869. 50268L,50269L,50270L,50271L,50272L,50273L,50274L,50275L,50276L,50277L,
  81870. 50278L,50279L,50280L,50281L,50282L,50283L,50284L,50285L,50286L,50287L,
  81871. 50288L,50289L,50290L,50291L,50292L,50293L,50294L,50295L,50296L,50297L,
  81872. 50298L,50299L,50300L,50301L,50302L,50303L,50304L,50305L,50306L,50307L,
  81873. 50308L,50309L,50310L,50311L,50312L,50313L,50314L,50315L,50316L,50317L,
  81874. 50318L,50319L,50320L,50321L,50322L,50323L,50324L,50325L,50326L,50327L,
  81875. 50328L,50329L,50330L,50331L,50332L,50333L,50334L,50335L,50336L,50337L,
  81876. 50338L,50339L,50340L,50341L,50342L,50343L,50344L,50345L,50346L,50347L,
  81877. 50348L,50349L,50350L,50351L,50352L,50353L,50354L,50355L,50356L,50357L,
  81878. 50358L,50359L,50360L,50361L,50362L,50363L,50364L,50365L,50366L,50367L,
  81879. 50368L,50369L,50370L,50371L,50372L,50373L,50374L,50375L,50376L,50377L,
  81880. 50378L,50379L,50380L,50381L,50382L,50383L,50384L,50385L,50386L,50387L,
  81881. 50388L,50389L,50390L,50391L,50392L,50393L,50394L,50395L,50396L,50397L,
  81882. 50398L,50399L,50400L,50401L,50402L,50403L,50404L,50405L,50406L,50407L,
  81883. 50408L,50409L,50410L,50411L,50412L,50413L,50414L,50415L,50416L,50417L,
  81884. 50418L,50419L,50420L,50421L,50422L,50423L,50424L,50425L,50426L,50427L,
  81885. 50428L,50429L,50430L,50431L,50432L,50433L,50434L,50435L,50436L,50437L,
  81886. 50438L,50439L,50440L,50441L,50442L,50443L,50444L,50445L,50446L,50447L,
  81887. 50448L,50449L,50450L,50451L,50452L,50453L,50454L,50455L,50456L,50457L,
  81888. 50458L,50459L,50460L,50461L,50462L,50463L,50464L,50465L,50466L,50467L,
  81889. 50468L,50469L,50470L,50471L,50472L,50473L,50474L,50475L,50476L,50477L,
  81890. 50478L,50479L,50480L,50481L,50482L,50483L,50484L,50485L,50486L,50487L,
  81891. 50488L,50489L,50490L,50491L,50492L,50493L,50494L,50495L,50496L,50497L,
  81892. 50498L,50499L,50500L,50501L,50502L,50503L,50504L,50505L,50506L,50507L,
  81893. 50508L,50509L,50510L,50511L,50512L,50513L,50514L,50515L,50516L,50517L,
  81894. 50518L,50519L,50520L,50521L,50522L,50523L,50524L,50525L,50526L,50527L,
  81895. 50528L,50529L,50530L,50531L,50532L,50533L,50534L,50535L,50536L,50537L,
  81896. 50538L,50539L,50540L,50541L,50542L,50543L,50544L,50545L,50546L,50547L,
  81897. 50548L,50549L,50550L,50551L,50552L,50553L,50554L,50555L,50556L,50557L,
  81898. 50558L,50559L,50560L,50561L,50562L,50563L,50564L,50565L,50566L,50567L,
  81899. 50568L,50569L,50570L,50571L,50572L,50573L,50574L,50575L,50576L,50577L,
  81900. 50578L,50579L,50580L,50581L,50582L,50583L,50584L,50585L,50586L,50587L,
  81901. 50588L,50589L,50590L,50591L,50592L,50593L,50594L,50595L,50596L,50597L,
  81902. 50598L,50599L,50600L,50601L,50602L,50603L,50604L,50605L,50606L,50607L,
  81903. 50608L,50609L,50610L,50611L,50612L,50613L,50614L,50615L,50616L,50617L,
  81904. 50618L,50619L,50620L,50621L,50622L,50623L,50624L,50625L,50626L,50627L,
  81905. 50628L,50629L,50630L,50631L,50632L,50633L,50634L,50635L,50636L,50637L,
  81906. 50638L,50639L,50640L,50641L,50642L,50643L,50644L,50645L,50646L,50647L,
  81907. 50648L,50649L,50650L,50651L,50652L,50653L,50654L,50655L,50656L,50657L,
  81908. 50658L,50659L,50660L,50661L,50662L,50663L,50664L,50665L,50666L,50667L,
  81909. 50668L,50669L,50670L,50671L,50672L,50673L,50674L,50675L,50676L,50677L,
  81910. 50678L,50679L,50680L,50681L,50682L,50683L,50684L,50685L,50686L,50687L,
  81911. 50688L,50689L,50690L,50691L,50692L,50693L,50694L,50695L,50696L,50697L,
  81912. 50698L,50699L,50700L,50701L,50702L,50703L,50704L,50705L,50706L,50707L,
  81913. 50708L,50709L,50710L,50711L,50712L,50713L,50714L,50715L,50716L,50717L,
  81914. 50718L,50719L,50720L,50721L,50722L,50723L,50724L,50725L,50726L,50727L,
  81915. 50728L,50729L,50730L,50731L,50732L,50733L,50734L,50735L,50736L,50737L,
  81916. 50738L,50739L,50740L,50741L,50742L,50743L,50744L,50745L,50746L,50747L,
  81917. 50748L,50749L,50750L,50751L,50752L,50753L,50754L,50755L,50756L,50757L,
  81918. 50758L,50759L,50760L,50761L,50762L,50763L,50764L,50765L,50766L,50767L,
  81919. 50768L,50769L,50770L,50771L,50772L,50773L,50774L,50775L,50776L,50777L,
  81920. 50778L,50779L,50780L,50781L,50782L,50783L,50784L,50785L,50786L,50787L,
  81921. 50788L,50789L,50790L,50791L,50792L,50793L,50794L,50795L,50796L,50797L,
  81922. 50798L,50799L,50800L,50801L,50802L,50803L,50804L,50805L,50806L,50807L,
  81923. 50808L,50809L,50810L,50811L,50812L,50813L,50814L,50815L,50816L,50817L,
  81924. 50818L,50819L,50820L,50821L,50822L,50823L,50824L,50825L,50826L,50827L,
  81925. 50828L,50829L,50830L,50831L,50832L,50833L,50834L,50835L,50836L,50837L,
  81926. 50838L,50839L,50840L,50841L,50842L,50843L,50844L,50845L,50846L,50847L,
  81927. 50848L,50849L,50850L,50851L,50852L,50853L,50854L,50855L,50856L,50857L,
  81928. 50858L,50859L,50860L,50861L,50862L,50863L,50864L,50865L,50866L,50867L,
  81929. 50868L,50869L,50870L,50871L,50872L,50873L,50874L,50875L,50876L,50877L,
  81930. 50878L,50879L,50880L,50881L,50882L,50883L,50884L,50885L,50886L,50887L,
  81931. 50888L,50889L,50890L,50891L,50892L,50893L,50894L,50895L,50896L,50897L,
  81932. 50898L,50899L,50900L,50901L,50902L,50903L,50904L,50905L,50906L,50907L,
  81933. 50908L,50909L,50910L,50911L,50912L,50913L,50914L,50915L,50916L,50917L,
  81934. 50918L,50919L,50920L,50921L,50922L,50923L,50924L,50925L,50926L,50927L,
  81935. 50928L,50929L,50930L,50931L,50932L,50933L,50934L,50935L,50936L,50937L,
  81936. 50938L,50939L,50940L,50941L,50942L,50943L,50944L,50945L,50946L,50947L,
  81937. 50948L,50949L,50950L,50951L,50952L,50953L,50954L,50955L,50956L,50957L,
  81938. 50958L,50959L,50960L,50961L,50962L,50963L,50964L,50965L,50966L,50967L,
  81939. 50968L,50969L,50970L,50971L,50972L,50973L,50974L,50975L,50976L,50977L,
  81940. 50978L,50979L,50980L,50981L,50982L,50983L,50984L,50985L,50986L,50987L,
  81941. 50988L,50989L,50990L,50991L,50992L,50993L,50994L,50995L,50996L,50997L,
  81942. 50998L,50999L,51000L,51001L,51002L,51003L,51004L,51005L,51006L,51007L,
  81943. 51008L,51009L,51010L,51011L,51012L,51013L,51014L,51015L,51016L,51017L,
  81944. 51018L,51019L,51020L,51021L,51022L,51023L,51024L,51025L,51026L,51027L,
  81945. 51028L,51029L,51030L,51031L,51032L,51033L,51034L,51035L,51036L,51037L,
  81946. 51038L,51039L,51040L,51041L,51042L,51043L,51044L,51045L,51046L,51047L,
  81947. 51048L,51049L,51050L,51051L,51052L,51053L,51054L,51055L,51056L,51057L,
  81948. 51058L,51059L,51060L,51061L,51062L,51063L,51064L,51065L,51066L,51067L,
  81949. 51068L,51069L,51070L,51071L,51072L,51073L,51074L,51075L,51076L,51077L,
  81950. 51078L,51079L,51080L,51081L,51082L,51083L,51084L,51085L,51086L,51087L,
  81951. 51088L,51089L,51090L,51091L,51092L,51093L,51094L,51095L,51096L,51097L,
  81952. 51098L,51099L,51100L,51101L,51102L,51103L,51104L,51105L,51106L,51107L,
  81953. 51108L,51109L,51110L,51111L,51112L,51113L,51114L,51115L,51116L,51117L,
  81954. 51118L,51119L,51120L,51121L,51122L,51123L,51124L,51125L,51126L,51127L,
  81955. 51128L,51129L,51130L,51131L,51132L,51133L,51134L,51135L,51136L,51137L,
  81956. 51138L,51139L,51140L,51141L,51142L,51143L,51144L,51145L,51146L,51147L,
  81957. 51148L,51149L,51150L,51151L,51152L,51153L,51154L,51155L,51156L,51157L,
  81958. 51158L,51159L,51160L,51161L,51162L,51163L,51164L,51165L,51166L,51167L,
  81959. 51168L,51169L,51170L,51171L,51172L,51173L,51174L,51175L,51176L,51177L,
  81960. 51178L,51179L,51180L,51181L,51182L,51183L,51184L,51185L,51186L,51187L,
  81961. 51188L,51189L,51190L,51191L,51192L,51193L,51194L,51195L,51196L,51197L,
  81962. 51198L,51199L,51200L,51201L,51202L,51203L,51204L,51205L,51206L,51207L,
  81963. 51208L,51209L,51210L,51211L,51212L,51213L,51214L,51215L,51216L,51217L,
  81964. 51218L,51219L,51220L,51221L,51222L,51223L,51224L,51225L,51226L,51227L,
  81965. 51228L,51229L,51230L,51231L,51232L,51233L,51234L,51235L,51236L,51237L,
  81966. 51238L,51239L,51240L,51241L,51242L,51243L,51244L,51245L,51246L,51247L,
  81967. 51248L,51249L,51250L,51251L,51252L,51253L,51254L,51255L,51256L,51257L,
  81968. 51258L,51259L,51260L,51261L,51262L,51263L,51264L,51265L,51266L,51267L,
  81969. 51268L,51269L,51270L,51271L,51272L,51273L,51274L,51275L,51276L,51277L,
  81970. 51278L,51279L,51280L,51281L,51282L,51283L,51284L,51285L,51286L,51287L,
  81971. 51288L,51289L,51290L,51291L,51292L,51293L,51294L,51295L,51296L,51297L,
  81972. 51298L,51299L,51300L,51301L,51302L,51303L,51304L,51305L,51306L,51307L,
  81973. 51308L,51309L,51310L,51311L,51312L,51313L,51314L,51315L,51316L,51317L,
  81974. 51318L,51319L,51320L,51321L,51322L,51323L,51324L,51325L,51326L,51327L,
  81975. 51328L,51329L,51330L,51331L,51332L,51333L,51334L,51335L,51336L,51337L,
  81976. 51338L,51339L,51340L,51341L,51342L,51343L,51344L,51345L,51346L,51347L,
  81977. 51348L,51349L,51350L,51351L,51352L,51353L,51354L,51355L,51356L,51357L,
  81978. 51358L,51359L,51360L,51361L,51362L,51363L,51364L,51365L,51366L,51367L,
  81979. 51368L,51369L,51370L,51371L,51372L,51373L,51374L,51375L,51376L,51377L,
  81980. 51378L,51379L,51380L,51381L,51382L,51383L,51384L,51385L,51386L,51387L,
  81981. 51388L,51389L,51390L,51391L,51392L,51393L,51394L,51395L,51396L,51397L,
  81982. 51398L,51399L,51400L,51401L,51402L,51403L,51404L,51405L,51406L,51407L,
  81983. 51408L,51409L,51410L,51411L,51412L,51413L,51414L,51415L,51416L,51417L,
  81984. 51418L,51419L,51420L,51421L,51422L,51423L,51424L,51425L,51426L,51427L,
  81985. 51428L,51429L,51430L,51431L,51432L,51433L,51434L,51435L,51436L,51437L,
  81986. 51438L,51439L,51440L,51441L,51442L,51443L,51444L,51445L,51446L,51447L,
  81987. 51448L,51449L,51450L,51451L,51452L,51453L,51454L,51455L,51456L,51457L,
  81988. 51458L,51459L,51460L,51461L,51462L,51463L,51464L,51465L,51466L,51467L,
  81989. 51468L,51469L,51470L,51471L,51472L,51473L,51474L,51475L,51476L,51477L,
  81990. 51478L,51479L,51480L,51481L,51482L,51483L,51484L,51485L,51486L,51487L,
  81991. 51488L,51489L,51490L,51491L,51492L,51493L,51494L,51495L,51496L,51497L,
  81992. 51498L,51499L,51500L,51501L,51502L,51503L,51504L,51505L,51506L,51507L,
  81993. 51508L,51509L,51510L,51511L,51512L,51513L,51514L,51515L,51516L,51517L,
  81994. 51518L,51519L,51520L,51521L,51522L,51523L,51524L,51525L,51526L,51527L,
  81995. 51528L,51529L,51530L,51531L,51532L,51533L,51534L,51535L,51536L,51537L,
  81996. 51538L,51539L,51540L,51541L,51542L,51543L,51544L,51545L,51546L,51547L,
  81997. 51548L,51549L,51550L,51551L,51552L,51553L,51554L,51555L,51556L,51557L,
  81998. 51558L,51559L,51560L,51561L,51562L,51563L,51564L,51565L,51566L,51567L,
  81999. 51568L,51569L,51570L,51571L,51572L,51573L,51574L,51575L,51576L,51577L,
  82000. 51578L,51579L,51580L,51581L,51582L,51583L,51584L,51585L,51586L,51587L,
  82001. 51588L,51589L,51590L,51591L,51592L,51593L,51594L,51595L,51596L,51597L,
  82002. 51598L,51599L,51600L,51601L,51602L,51603L,51604L,51605L,51606L,51607L,
  82003. 51608L,51609L,51610L,51611L,51612L,51613L,51614L,51615L,51616L,51617L,
  82004. 51618L,51619L,51620L,51621L,51622L,51623L,51624L,51625L,51626L,51627L,
  82005. 51628L,51629L,51630L,51631L,51632L,51633L,51634L,51635L,51636L,51637L,
  82006. 51638L,51639L,51640L,51641L,51642L,51643L,51644L,51645L,51646L,51647L,
  82007. 51648L,51649L,51650L,51651L,51652L,51653L,51654L,51655L,51656L,51657L,
  82008. 51658L,51659L,51660L,51661L,51662L,51663L,51664L,51665L,51666L,51667L,
  82009. 51668L,51669L,51670L,51671L,51672L,51673L,51674L,51675L,51676L,51677L,
  82010. 51678L,51679L,51680L,51681L,51682L,51683L,51684L,51685L,51686L,51687L,
  82011. 51688L,51689L,51690L,51691L,51692L,51693L,51694L,51695L,51696L,51697L,
  82012. 51698L,51699L,51700L,51701L,51702L,51703L,51704L,51705L,51706L,51707L,
  82013. 51708L,51709L,51710L,51711L,51712L,51713L,51714L,51715L,51716L,51717L,
  82014. 51718L,51719L,51720L,51721L,51722L,51723L,51724L,51725L,51726L,51727L,
  82015. 51728L,51729L,51730L,51731L,51732L,51733L,51734L,51735L,51736L,51737L,
  82016. 51738L,51739L,51740L,51741L,51742L,51743L,51744L,51745L,51746L,51747L,
  82017. 51748L,51749L,51750L,51751L,51752L,51753L,51754L,51755L,51756L,51757L,
  82018. 51758L,51759L,51760L,51761L,51762L,51763L,51764L,51765L,51766L,51767L,
  82019. 51768L,51769L,51770L,51771L,51772L,51773L,51774L,51775L,51776L,51777L,
  82020. 51778L,51779L,51780L,51781L,51782L,51783L,51784L,51785L,51786L,51787L,
  82021. 51788L,51789L,51790L,51791L,51792L,51793L,51794L,51795L,51796L,51797L,
  82022. 51798L,51799L,51800L,51801L,51802L,51803L,51804L,51805L,51806L,51807L,
  82023. 51808L,51809L,51810L,51811L,51812L,51813L,51814L,51815L,51816L,51817L,
  82024. 51818L,51819L,51820L,51821L,51822L,51823L,51824L,51825L,51826L,51827L,
  82025. 51828L,51829L,51830L,51831L,51832L,51833L,51834L,51835L,51836L,51837L,
  82026. 51838L,51839L,51840L,51841L,51842L,51843L,51844L,51845L,51846L,51847L,
  82027. 51848L,51849L,51850L,51851L,51852L,51853L,51854L,51855L,51856L,51857L,
  82028. 51858L,51859L,51860L,51861L,51862L,51863L,51864L,51865L,51866L,51867L,
  82029. 51868L,51869L,51870L,51871L,51872L,51873L,51874L,51875L,51876L,51877L,
  82030. 51878L,51879L,51880L,51881L,51882L,51883L,51884L,51885L,51886L,51887L,
  82031. 51888L,51889L,51890L,51891L,51892L,51893L,51894L,51895L,51896L,51897L,
  82032. 51898L,51899L,51900L,51901L,51902L,51903L,51904L,51905L,51906L,51907L,
  82033. 51908L,51909L,51910L,51911L,51912L,51913L,51914L,51915L,51916L,51917L,
  82034. 51918L,51919L,51920L,51921L,51922L,51923L,51924L,51925L,51926L,51927L,
  82035. 51928L,51929L,51930L,51931L,51932L,51933L,51934L,51935L,51936L,51937L,
  82036. 51938L,51939L,51940L,51941L,51942L,51943L,51944L,51945L,51946L,51947L,
  82037. 51948L,51949L,51950L,51951L,51952L,51953L,51954L,51955L,51956L,51957L,
  82038. 51958L,51959L,51960L,51961L,51962L,51963L,51964L,51965L,51966L,51967L,
  82039. 51968L,51969L,51970L,51971L,51972L,51973L,51974L,51975L,51976L,51977L,
  82040. 51978L,51979L,51980L,51981L,51982L,51983L,51984L,51985L,51986L,51987L,
  82041. 51988L,51989L,51990L,51991L,51992L,51993L,51994L,51995L,51996L,51997L,
  82042. 51998L,51999L,52000L,52001L,52002L,52003L,52004L,52005L,52006L,52007L,
  82043. 52008L,52009L,52010L,52011L,52012L,52013L,52014L,52015L,52016L,52017L,
  82044. 52018L,52019L,52020L,52021L,52022L,52023L,52024L,52025L,52026L,52027L,
  82045. 52028L,52029L,52030L,52031L,52032L,52033L,52034L,52035L,52036L,52037L,
  82046. 52038L,52039L,52040L,52041L,52042L,52043L,52044L,52045L,52046L,52047L,
  82047. 52048L,52049L,52050L,52051L,52052L,52053L,52054L,52055L,52056L,52057L,
  82048. 52058L,52059L,52060L,52061L,52062L,52063L,52064L,52065L,52066L,52067L,
  82049. 52068L,52069L,52070L,52071L,52072L,52073L,52074L,52075L,52076L,52077L,
  82050. 52078L,52079L,52080L,52081L,52082L,52083L,52084L,52085L,52086L,52087L,
  82051. 52088L,52089L,52090L,52091L,52092L,52093L,52094L,52095L,52096L,52097L,
  82052. 52098L,52099L,52100L,52101L,52102L,52103L,52104L,52105L,52106L,52107L,
  82053. 52108L,52109L,52110L,52111L,52112L,52113L,52114L,52115L,52116L,52117L,
  82054. 52118L,52119L,52120L,52121L,52122L,52123L,52124L,52125L,52126L,52127L,
  82055. 52128L,52129L,52130L,52131L,52132L,52133L,52134L,52135L,52136L,52137L,
  82056. 52138L,52139L,52140L,52141L,52142L,52143L,52144L,52145L,52146L,52147L,
  82057. 52148L,52149L,52150L,52151L,52152L,52153L,52154L,52155L,52156L,52157L,
  82058. 52158L,52159L,52160L,52161L,52162L,52163L,52164L,52165L,52166L,52167L,
  82059. 52168L,52169L,52170L,52171L,52172L,52173L,52174L,52175L,52176L,52177L,
  82060. 52178L,52179L,52180L,52181L,52182L,52183L,52184L,52185L,52186L,52187L,
  82061. 52188L,52189L,52190L,52191L,52192L,52193L,52194L,52195L,52196L,52197L,
  82062. 52198L,52199L,52200L,52201L,52202L,52203L,52204L,52205L,52206L,52207L,
  82063. 52208L,52209L,52210L,52211L,52212L,52213L,52214L,52215L,52216L,52217L,
  82064. 52218L,52219L,52220L,52221L,52222L,52223L,52224L,52225L,52226L,52227L,
  82065. 52228L,52229L,52230L,52231L,52232L,52233L,52234L,52235L,52236L,52237L,
  82066. 52238L,52239L,52240L,52241L,52242L,52243L,52244L,52245L,52246L,52247L,
  82067. 52248L,52249L,52250L,52251L,52252L,52253L,52254L,52255L,52256L,52257L,
  82068. 52258L,52259L,52260L,52261L,52262L,52263L,52264L,52265L,52266L,52267L,
  82069. 52268L,52269L,52270L,52271L,52272L,52273L,52274L,52275L,52276L,52277L,
  82070. 52278L,52279L,52280L,52281L,52282L,52283L,52284L,52285L,52286L,52287L,
  82071. 52288L,52289L,52290L,52291L,52292L,52293L,52294L,52295L,52296L,52297L,
  82072. 52298L,52299L,52300L,52301L,52302L,52303L,52304L,52305L,52306L,52307L,
  82073. 52308L,52309L,52310L,52311L,52312L,52313L,52314L,52315L,52316L,52317L,
  82074. 52318L,52319L,52320L,52321L,52322L,52323L,52324L,52325L,52326L,52327L,
  82075. 52328L,52329L,52330L,52331L,52332L,52333L,52334L,52335L,52336L,52337L,
  82076. 52338L,52339L,52340L,52341L,52342L,52343L,52344L,52345L,52346L,52347L,
  82077. 52348L,52349L,52350L,52351L,52352L,52353L,52354L,52355L,52356L,52357L,
  82078. 52358L,52359L,52360L,52361L,52362L,52363L,52364L,52365L,52366L,52367L,
  82079. 52368L,52369L,52370L,52371L,52372L,52373L,52374L,52375L,52376L,52377L,
  82080. 52378L,52379L,52380L,52381L,52382L,52383L,52384L,52385L,52386L,52387L,
  82081. 52388L,52389L,52390L,52391L,52392L,52393L,52394L,52395L,52396L,52397L,
  82082. 52398L,52399L,52400L,52401L,52402L,52403L,52404L,52405L,52406L,52407L,
  82083. 52408L,52409L,52410L,52411L,52412L,52413L,52414L,52415L,52416L,52417L,
  82084. 52418L,52419L,52420L,52421L,52422L,52423L,52424L,52425L,52426L,52427L,
  82085. 52428L,52429L,52430L,52431L,52432L,52433L,52434L,52435L,52436L,52437L,
  82086. 52438L,52439L,52440L,52441L,52442L,52443L,52444L,52445L,52446L,52447L,
  82087. 52448L,52449L,52450L,52451L,52452L,52453L,52454L,52455L,52456L,52457L,
  82088. 52458L,52459L,52460L,52461L,52462L,52463L,52464L,52465L,52466L,52467L,
  82089. 52468L,52469L,52470L,52471L,52472L,52473L,52474L,52475L,52476L,52477L,
  82090. 52478L,52479L,52480L,52481L,52482L,52483L,52484L,52485L,52486L,52487L,
  82091. 52488L,52489L,52490L,52491L,52492L,52493L,52494L,52495L,52496L,52497L,
  82092. 52498L,52499L,52500L,52501L,52502L,52503L,52504L,52505L,52506L,52507L,
  82093. 52508L,52509L,52510L,52511L,52512L,52513L,52514L,52515L,52516L,52517L,
  82094. 52518L,52519L,52520L,52521L,52522L,52523L,52524L,52525L,52526L,52527L,
  82095. 52528L,52529L,52530L,52531L,52532L,52533L,52534L,52535L,52536L,52537L,
  82096. 52538L,52539L,52540L,52541L,52542L,52543L,52544L,52545L,52546L,52547L,
  82097. 52548L,52549L,52550L,52551L,52552L,52553L,52554L,52555L,52556L,52557L,
  82098. 52558L,52559L,52560L,52561L,52562L,52563L,52564L,52565L,52566L,52567L,
  82099. 52568L,52569L,52570L,52571L,52572L,52573L,52574L,52575L,52576L,52577L,
  82100. 52578L,52579L,52580L,52581L,52582L,52583L,52584L,52585L,52586L,52587L,
  82101. 52588L,52589L,52590L,52591L,52592L,52593L,52594L,52595L,52596L,52597L,
  82102. 52598L,52599L,52600L,52601L,52602L,52603L,52604L,52605L,52606L,52607L,
  82103. 52608L,52609L,52610L,52611L,52612L,52613L,52614L,52615L,52616L,52617L,
  82104. 52618L,52619L,52620L,52621L,52622L,52623L,52624L,52625L,52626L,52627L,
  82105. 52628L,52629L,52630L,52631L,52632L,52633L,52634L,52635L,52636L,52637L,
  82106. 52638L,52639L,52640L,52641L,52642L,52643L,52644L,52645L,52646L,52647L,
  82107. 52648L,52649L,52650L,52651L,52652L,52653L,52654L,52655L,52656L,52657L,
  82108. 52658L,52659L,52660L,52661L,52662L,52663L,52664L,52665L,52666L,52667L,
  82109. 52668L,52669L,52670L,52671L,52672L,52673L,52674L,52675L,52676L,52677L,
  82110. 52678L,52679L,52680L,52681L,52682L,52683L,52684L,52685L,52686L,52687L,
  82111. 52688L,52689L,52690L,52691L,52692L,52693L,52694L,52695L,52696L,52697L,
  82112. 52698L,52699L,52700L,52701L,52702L,52703L,52704L,52705L,52706L,52707L,
  82113. 52708L,52709L,52710L,52711L,52712L,52713L,52714L,52715L,52716L,52717L,
  82114. 52718L,52719L,52720L,52721L,52722L,52723L,52724L,52725L,52726L,52727L,
  82115. 52728L,52729L,52730L,52731L,52732L,52733L,52734L,52735L,52736L,52737L,
  82116. 52738L,52739L,52740L,52741L,52742L,52743L,52744L,52745L,52746L,52747L,
  82117. 52748L,52749L,52750L,52751L,52752L,52753L,52754L,52755L,52756L,52757L,
  82118. 52758L,52759L,52760L,52761L,52762L,52763L,52764L,52765L,52766L,52767L,
  82119. 52768L,52769L,52770L,52771L,52772L,52773L,52774L,52775L,52776L,52777L,
  82120. 52778L,52779L,52780L,52781L,52782L,52783L,52784L,52785L,52786L,52787L,
  82121. 52788L,52789L,52790L,52791L,52792L,52793L,52794L,52795L,52796L,52797L,
  82122. 52798L,52799L,52800L,52801L,52802L,52803L,52804L,52805L,52806L,52807L,
  82123. 52808L,52809L,52810L,52811L,52812L,52813L,52814L,52815L,52816L,52817L,
  82124. 52818L,52819L,52820L,52821L,52822L,52823L,52824L,52825L,52826L,52827L,
  82125. 52828L,52829L,52830L,52831L,52832L,52833L,52834L,52835L,52836L,52837L,
  82126. 52838L,52839L,52840L,52841L,52842L,52843L,52844L,52845L,52846L,52847L,
  82127. 52848L,52849L,52850L,52851L,52852L,52853L,52854L,52855L,52856L,52857L,
  82128. 52858L,52859L,52860L,52861L,52862L,52863L,52864L,52865L,52866L,52867L,
  82129. 52868L,52869L,52870L,52871L,52872L,52873L,52874L,52875L,52876L,52877L,
  82130. 52878L,52879L,52880L,52881L,52882L,52883L,52884L,52885L,52886L,52887L,
  82131. 52888L,52889L,52890L,52891L,52892L,52893L,52894L,52895L,52896L,52897L,
  82132. 52898L,52899L,52900L,52901L,52902L,52903L,52904L,52905L,52906L,52907L,
  82133. 52908L,52909L,52910L,52911L,52912L,52913L,52914L,52915L,52916L,52917L,
  82134. 52918L,52919L,52920L,52921L,52922L,52923L,52924L,52925L,52926L,52927L,
  82135. 52928L,52929L,52930L,52931L,52932L,52933L,52934L,52935L,52936L,52937L,
  82136. 52938L,52939L,52940L,52941L,52942L,52943L,52944L,52945L,52946L,52947L,
  82137. 52948L,52949L,52950L,52951L,52952L,52953L,52954L,52955L,52956L,52957L,
  82138. 52958L,52959L,52960L,52961L,52962L,52963L,52964L,52965L,52966L,52967L,
  82139. 52968L,52969L,52970L,52971L,52972L,52973L,52974L,52975L,52976L,52977L,
  82140. 52978L,52979L,52980L,52981L,52982L,52983L,52984L,52985L,52986L,52987L,
  82141. 52988L,52989L,52990L,52991L,52992L,52993L,52994L,52995L,52996L,52997L,
  82142. 52998L,52999L,53000L,53001L,53002L,53003L,53004L,53005L,53006L,53007L,
  82143. 53008L,53009L,53010L,53011L,53012L,53013L,53014L,53015L,53016L,53017L,
  82144. 53018L,53019L,53020L,53021L,53022L,53023L,53024L,53025L,53026L,53027L,
  82145. 53028L,53029L,53030L,53031L,53032L,53033L,53034L,53035L,53036L,53037L,
  82146. 53038L,53039L,53040L,53041L,53042L,53043L,53044L,53045L,53046L,53047L,
  82147. 53048L,53049L,53050L,53051L,53052L,53053L,53054L,53055L,53056L,53057L,
  82148. 53058L,53059L,53060L,53061L,53062L,53063L,53064L,53065L,53066L,53067L,
  82149. 53068L,53069L,53070L,53071L,53072L,53073L,53074L,53075L,53076L,53077L,
  82150. 53078L,53079L,53080L,53081L,53082L,53083L,53084L,53085L,53086L,53087L,
  82151. 53088L,53089L,53090L,53091L,53092L,53093L,53094L,53095L,53096L,53097L,
  82152. 53098L,53099L,53100L,53101L,53102L,53103L,53104L,53105L,53106L,53107L,
  82153. 53108L,53109L,53110L,53111L,53112L,53113L,53114L,53115L,53116L,53117L,
  82154. 53118L,53119L,53120L,53121L,53122L,53123L,53124L,53125L,53126L,53127L,
  82155. 53128L,53129L,53130L,53131L,53132L,53133L,53134L,53135L,53136L,53137L,
  82156. 53138L,53139L,53140L,53141L,53142L,53143L,53144L,53145L,53146L,53147L,
  82157. 53148L,53149L,53150L,53151L,53152L,53153L,53154L,53155L,53156L,53157L,
  82158. 53158L,53159L,53160L,53161L,53162L,53163L,53164L,53165L,53166L,53167L,
  82159. 53168L,53169L,53170L,53171L,53172L,53173L,53174L,53175L,53176L,53177L,
  82160. 53178L,53179L,53180L,53181L,53182L,53183L,53184L,53185L,53186L,53187L,
  82161. 53188L,53189L,53190L,53191L,53192L,53193L,53194L,53195L,53196L,53197L,
  82162. 53198L,53199L,53200L,53201L,53202L,53203L,53204L,53205L,53206L,53207L,
  82163. 53208L,53209L,53210L,53211L,53212L,53213L,53214L,53215L,53216L,53217L,
  82164. 53218L,53219L,53220L,53221L,53222L,53223L,53224L,53225L,53226L,53227L,
  82165. 53228L,53229L,53230L,53231L,53232L,53233L,53234L,53235L,53236L,53237L,
  82166. 53238L,53239L,53240L,53241L,53242L,53243L,53244L,53245L,53246L,53247L,
  82167. 53248L,53249L,53250L,53251L,53252L,53253L,53254L,53255L,53256L,53257L,
  82168. 53258L,53259L,53260L,53261L,53262L,53263L,53264L,53265L,53266L,53267L,
  82169. 53268L,53269L,53270L,53271L,53272L,53273L,53274L,53275L,53276L,53277L,
  82170. 53278L,53279L,53280L,53281L,53282L,53283L,53284L,53285L,53286L,53287L,
  82171. 53288L,53289L,53290L,53291L,53292L,53293L,53294L,53295L,53296L,53297L,
  82172. 53298L,53299L,53300L,53301L,53302L,53303L,53304L,53305L,53306L,53307L,
  82173. 53308L,53309L,53310L,53311L,53312L,53313L,53314L,53315L,53316L,53317L,
  82174. 53318L,53319L,53320L,53321L,53322L,53323L,53324L,53325L,53326L,53327L,
  82175. 53328L,53329L,53330L,53331L,53332L,53333L,53334L,53335L,53336L,53337L,
  82176. 53338L,53339L,53340L,53341L,53342L,53343L,53344L,53345L,53346L,53347L,
  82177. 53348L,53349L,53350L,53351L,53352L,53353L,53354L,53355L,53356L,53357L,
  82178. 53358L,53359L,53360L,53361L,53362L,53363L,53364L,53365L,53366L,53367L,
  82179. 53368L,53369L,53370L,53371L,53372L,53373L,53374L,53375L,53376L,53377L,
  82180. 53378L,53379L,53380L,53381L,53382L,53383L,53384L,53385L,53386L,53387L,
  82181. 53388L,53389L,53390L,53391L,53392L,53393L,53394L,53395L,53396L,53397L,
  82182. 53398L,53399L,53400L,53401L,53402L,53403L,53404L,53405L,53406L,53407L,
  82183. 53408L,53409L,53410L,53411L,53412L,53413L,53414L,53415L,53416L,53417L,
  82184. 53418L,53419L,53420L,53421L,53422L,53423L,53424L,53425L,53426L,53427L,
  82185. 53428L,53429L,53430L,53431L,53432L,53433L,53434L,53435L,53436L,53437L,
  82186. 53438L,53439L,53440L,53441L,53442L,53443L,53444L,53445L,53446L,53447L,
  82187. 53448L,53449L,53450L,53451L,53452L,53453L,53454L,53455L,53456L,53457L,
  82188. 53458L,53459L,53460L,53461L,53462L,53463L,53464L,53465L,53466L,53467L,
  82189. 53468L,53469L,53470L,53471L,53472L,53473L,53474L,53475L,53476L,53477L,
  82190. 53478L,53479L,53480L,53481L,53482L,53483L,53484L,53485L,53486L,53487L,
  82191. 53488L,53489L,53490L,53491L,53492L,53493L,53494L,53495L,53496L,53497L,
  82192. 53498L,53499L,53500L,53501L,53502L,53503L,53504L,53505L,53506L,53507L,
  82193. 53508L,53509L,53510L,53511L,53512L,53513L,53514L,53515L,53516L,53517L,
  82194. 53518L,53519L,53520L,53521L,53522L,53523L,53524L,53525L,53526L,53527L,
  82195. 53528L,53529L,53530L,53531L,53532L,53533L,53534L,53535L,53536L,53537L,
  82196. 53538L,53539L,53540L,53541L,53542L,53543L,53544L,53545L,53546L,53547L,
  82197. 53548L,53549L,53550L,53551L,53552L,53553L,53554L,53555L,53556L,53557L,
  82198. 53558L,53559L,53560L,53561L,53562L,53563L,53564L,53565L,53566L,53567L,
  82199. 53568L,53569L,53570L,53571L,53572L,53573L,53574L,53575L,53576L,53577L,
  82200. 53578L,53579L,53580L,53581L,53582L,53583L,53584L,53585L,53586L,53587L,
  82201. 53588L,53589L,53590L,53591L,53592L,53593L,53594L,53595L,53596L,53597L,
  82202. 53598L,53599L,53600L,53601L,53602L,53603L,53604L,53605L,53606L,53607L,
  82203. 53608L,53609L,53610L,53611L,53612L,53613L,53614L,53615L,53616L,53617L,
  82204. 53618L,53619L,53620L,53621L,53622L,53623L,53624L,53625L,53626L,53627L,
  82205. 53628L,53629L,53630L,53631L,53632L,53633L,53634L,53635L,53636L,53637L,
  82206. 53638L,53639L,53640L,53641L,53642L,53643L,53644L,53645L,53646L,53647L,
  82207. 53648L,53649L,53650L,53651L,53652L,53653L,53654L,53655L,53656L,53657L,
  82208. 53658L,53659L,53660L,53661L,53662L,53663L,53664L,53665L,53666L,53667L,
  82209. 53668L,53669L,53670L,53671L,53672L,53673L,53674L,53675L,53676L,53677L,
  82210. 53678L,53679L,53680L,53681L,53682L,53683L,53684L,53685L,53686L,53687L,
  82211. 53688L,53689L,53690L,53691L,53692L,53693L,53694L,53695L,53696L,53697L,
  82212. 53698L,53699L,53700L,53701L,53702L,53703L,53704L,53705L,53706L,53707L,
  82213. 53708L,53709L,53710L,53711L,53712L,53713L,53714L,53715L,53716L,53717L,
  82214. 53718L,53719L,53720L,53721L,53722L,53723L,53724L,53725L,53726L,53727L,
  82215. 53728L,53729L,53730L,53731L,53732L,53733L,53734L,53735L,53736L,53737L,
  82216. 53738L,53739L,53740L,53741L,53742L,53743L,53744L,53745L,53746L,53747L,
  82217. 53748L,53749L,53750L,53751L,53752L,53753L,53754L,53755L,53756L,53757L,
  82218. 53758L,53759L,53760L,53761L,53762L,53763L,53764L,53765L,53766L,53767L,
  82219. 53768L,53769L,53770L,53771L,53772L,53773L,53774L,53775L,53776L,53777L,
  82220. 53778L,53779L,53780L,53781L,53782L,53783L,53784L,53785L,53786L,53787L,
  82221. 53788L,53789L,53790L,53791L,53792L,53793L,53794L,53795L,53796L,53797L,
  82222. 53798L,53799L,53800L,53801L,53802L,53803L,53804L,53805L,53806L,53807L,
  82223. 53808L,53809L,53810L,53811L,53812L,53813L,53814L,53815L,53816L,53817L,
  82224. 53818L,53819L,53820L,53821L,53822L,53823L,53824L,53825L,53826L,53827L,
  82225. 53828L,53829L,53830L,53831L,53832L,53833L,53834L,53835L,53836L,53837L,
  82226. 53838L,53839L,53840L,53841L,53842L,53843L,53844L,53845L,53846L,53847L,
  82227. 53848L,53849L,53850L,53851L,53852L,53853L,53854L,53855L,53856L,53857L,
  82228. 53858L,53859L,53860L,53861L,53862L,53863L,53864L,53865L,53866L,53867L,
  82229. 53868L,53869L,53870L,53871L,53872L,53873L,53874L,53875L,53876L,53877L,
  82230. 53878L,53879L,53880L,53881L,53882L,53883L,53884L,53885L,53886L,53887L,
  82231. 53888L,53889L,53890L,53891L,53892L,53893L,53894L,53895L,53896L,53897L,
  82232. 53898L,53899L,53900L,53901L,53902L,53903L,53904L,53905L,53906L,53907L,
  82233. 53908L,53909L,53910L,53911L,53912L,53913L,53914L,53915L,53916L,53917L,
  82234. 53918L,53919L,53920L,53921L,53922L,53923L,53924L,53925L,53926L,53927L,
  82235. 53928L,53929L,53930L,53931L,53932L,53933L,53934L,53935L,53936L,53937L,
  82236. 53938L,53939L,53940L,53941L,53942L,53943L,53944L,53945L,53946L,53947L,
  82237. 53948L,53949L,53950L,53951L,53952L,53953L,53954L,53955L,53956L,53957L,
  82238. 53958L,53959L,53960L,53961L,53962L,53963L,53964L,53965L,53966L,53967L,
  82239. 53968L,53969L,53970L,53971L,53972L,53973L,53974L,53975L,53976L,53977L,
  82240. 53978L,53979L,53980L,53981L,53982L,53983L,53984L,53985L,53986L,53987L,
  82241. 53988L,53989L,53990L,53991L,53992L,53993L,53994L,53995L,53996L,53997L,
  82242. 53998L,53999L,54000L,54001L,54002L,54003L,54004L,54005L,54006L,54007L,
  82243. 54008L,54009L,54010L,54011L,54012L,54013L,54014L,54015L,54016L,54017L,
  82244. 54018L,54019L,54020L,54021L,54022L,54023L,54024L,54025L,54026L,54027L,
  82245. 54028L,54029L,54030L,54031L,54032L,54033L,54034L,54035L,54036L,54037L,
  82246. 54038L,54039L,54040L,54041L,54042L,54043L,54044L,54045L,54046L,54047L,
  82247. 54048L,54049L,54050L,54051L,54052L,54053L,54054L,54055L,54056L,54057L,
  82248. 54058L,54059L,54060L,54061L,54062L,54063L,54064L,54065L,54066L,54067L,
  82249. 54068L,54069L,54070L,54071L,54072L,54073L,54074L,54075L,54076L,54077L,
  82250. 54078L,54079L,54080L,54081L,54082L,54083L,54084L,54085L,54086L,54087L,
  82251. 54088L,54089L,54090L,54091L,54092L,54093L,54094L,54095L,54096L,54097L,
  82252. 54098L,54099L,54100L,54101L,54102L,54103L,54104L,54105L,54106L,54107L,
  82253. 54108L,54109L,54110L,54111L,54112L,54113L,54114L,54115L,54116L,54117L,
  82254. 54118L,54119L,54120L,54121L,54122L,54123L,54124L,54125L,54126L,54127L,
  82255. 54128L,54129L,54130L,54131L,54132L,54133L,54134L,54135L,54136L,54137L,
  82256. 54138L,54139L,54140L,54141L,54142L,54143L,54144L,54145L,54146L,54147L,
  82257. 54148L,54149L,54150L,54151L,54152L,54153L,54154L,54155L,54156L,54157L,
  82258. 54158L,54159L,54160L,54161L,54162L,54163L,54164L,54165L,54166L,54167L,
  82259. 54168L,54169L,54170L,54171L,54172L,54173L,54174L,54175L,54176L,54177L,
  82260. 54178L,54179L,54180L,54181L,54182L,54183L,54184L,54185L,54186L,54187L,
  82261. 54188L,54189L,54190L,54191L,54192L,54193L,54194L,54195L,54196L,54197L,
  82262. 54198L,54199L,54200L,54201L,54202L,54203L,54204L,54205L,54206L,54207L,
  82263. 54208L,54209L,54210L,54211L,54212L,54213L,54214L,54215L,54216L,54217L,
  82264. 54218L,54219L,54220L,54221L,54222L,54223L,54224L,54225L,54226L,54227L,
  82265. 54228L,54229L,54230L,54231L,54232L,54233L,54234L,54235L,54236L,54237L,
  82266. 54238L,54239L,54240L,54241L,54242L,54243L,54244L,54245L,54246L,54247L,
  82267. 54248L,54249L,54250L,54251L,54252L,54253L,54254L,54255L,54256L,54257L,
  82268. 54258L,54259L,54260L,54261L,54262L,54263L,54264L,54265L,54266L,54267L,
  82269. 54268L,54269L,54270L,54271L,54272L,54273L,54274L,54275L,54276L,54277L,
  82270. 54278L,54279L,54280L,54281L,54282L,54283L,54284L,54285L,54286L,54287L,
  82271. 54288L,54289L,54290L,54291L,54292L,54293L,54294L,54295L,54296L,54297L,
  82272. 54298L,54299L,54300L,54301L,54302L,54303L,54304L,54305L,54306L,54307L,
  82273. 54308L,54309L,54310L,54311L,54312L,54313L,54314L,54315L,54316L,54317L,
  82274. 54318L,54319L,54320L,54321L,54322L,54323L,54324L,54325L,54326L,54327L,
  82275. 54328L,54329L,54330L,54331L,54332L,54333L,54334L,54335L,54336L,54337L,
  82276. 54338L,54339L,54340L,54341L,54342L,54343L,54344L,54345L,54346L,54347L,
  82277. 54348L,54349L,54350L,54351L,54352L,54353L,54354L,54355L,54356L,54357L,
  82278. 54358L,54359L,54360L,54361L,54362L,54363L,54364L,54365L,54366L,54367L,
  82279. 54368L,54369L,54370L,54371L,54372L,54373L,54374L,54375L,54376L,54377L,
  82280. 54378L,54379L,54380L,54381L,54382L,54383L,54384L,54385L,54386L,54387L,
  82281. 54388L,54389L,54390L,54391L,54392L,54393L,54394L,54395L,54396L,54397L,
  82282. 54398L,54399L,54400L,54401L,54402L,54403L,54404L,54405L,54406L,54407L,
  82283. 54408L,54409L,54410L,54411L,54412L,54413L,54414L,54415L,54416L,54417L,
  82284. 54418L,54419L,54420L,54421L,54422L,54423L,54424L,54425L,54426L,54427L,
  82285. 54428L,54429L,54430L,54431L,54432L,54433L,54434L,54435L,54436L,54437L,
  82286. 54438L,54439L,54440L,54441L,54442L,54443L,54444L,54445L,54446L,54447L,
  82287. 54448L,54449L,54450L,54451L,54452L,54453L,54454L,54455L,54456L,54457L,
  82288. 54458L,54459L,54460L,54461L,54462L,54463L,54464L,54465L,54466L,54467L,
  82289. 54468L,54469L,54470L,54471L,54472L,54473L,54474L,54475L,54476L,54477L,
  82290. 54478L,54479L,54480L,54481L,54482L,54483L,54484L,54485L,54486L,54487L,
  82291. 54488L,54489L,54490L,54491L,54492L,54493L,54494L,54495L,54496L,54497L,
  82292. 54498L,54499L,54500L,54501L,54502L,54503L,54504L,54505L,54506L,54507L,
  82293. 54508L,54509L,54510L,54511L,54512L,54513L,54514L,54515L,54516L,54517L,
  82294. 54518L,54519L,54520L,54521L,54522L,54523L,54524L,54525L,54526L,54527L,
  82295. 54528L,54529L,54530L,54531L,54532L,54533L,54534L,54535L,54536L,54537L,
  82296. 54538L,54539L,54540L,54541L,54542L,54543L,54544L,54545L,54546L,54547L,
  82297. 54548L,54549L,54550L,54551L,54552L,54553L,54554L,54555L,54556L,54557L,
  82298. 54558L,54559L,54560L,54561L,54562L,54563L,54564L,54565L,54566L,54567L,
  82299. 54568L,54569L,54570L,54571L,54572L,54573L,54574L,54575L,54576L,54577L,
  82300. 54578L,54579L,54580L,54581L,54582L,54583L,54584L,54585L,54586L,54587L,
  82301. 54588L,54589L,54590L,54591L,54592L,54593L,54594L,54595L,54596L,54597L,
  82302. 54598L,54599L,54600L,54601L,54602L,54603L,54604L,54605L,54606L,54607L,
  82303. 54608L,54609L,54610L,54611L,54612L,54613L,54614L,54615L,54616L,54617L,
  82304. 54618L,54619L,54620L,54621L,54622L,54623L,54624L,54625L,54626L,54627L,
  82305. 54628L,54629L,54630L,54631L,54632L,54633L,54634L,54635L,54636L,54637L,
  82306. 54638L,54639L,54640L,54641L,54642L,54643L,54644L,54645L,54646L,54647L,
  82307. 54648L,54649L,54650L,54651L,54652L,54653L,54654L,54655L,54656L,54657L,
  82308. 54658L,54659L,54660L,54661L,54662L,54663L,54664L,54665L,54666L,54667L,
  82309. 54668L,54669L,54670L,54671L,54672L,54673L,54674L,54675L,54676L,54677L,
  82310. 54678L,54679L,54680L,54681L,54682L,54683L,54684L,54685L,54686L,54687L,
  82311. 54688L,54689L,54690L,54691L,54692L,54693L,54694L,54695L,54696L,54697L,
  82312. 54698L,54699L,54700L,54701L,54702L,54703L,54704L,54705L,54706L,54707L,
  82313. 54708L,54709L,54710L,54711L,54712L,54713L,54714L,54715L,54716L,54717L,
  82314. 54718L,54719L,54720L,54721L,54722L,54723L,54724L,54725L,54726L,54727L,
  82315. 54728L,54729L,54730L,54731L,54732L,54733L,54734L,54735L,54736L,54737L,
  82316. 54738L,54739L,54740L,54741L,54742L,54743L,54744L,54745L,54746L,54747L,
  82317. 54748L,54749L,54750L,54751L,54752L,54753L,54754L,54755L,54756L,54757L,
  82318. 54758L,54759L,54760L,54761L,54762L,54763L,54764L,54765L,54766L,54767L,
  82319. 54768L,54769L,54770L,54771L,54772L,54773L,54774L,54775L,54776L,54777L,
  82320. 54778L,54779L,54780L,54781L,54782L,54783L,54784L,54785L,54786L,54787L,
  82321. 54788L,54789L,54790L,54791L,54792L,54793L,54794L,54795L,54796L,54797L,
  82322. 54798L,54799L,54800L,54801L,54802L,54803L,54804L,54805L,54806L,54807L,
  82323. 54808L,54809L,54810L,54811L,54812L,54813L,54814L,54815L,54816L,54817L,
  82324. 54818L,54819L,54820L,54821L,54822L,54823L,54824L,54825L,54826L,54827L,
  82325. 54828L,54829L,54830L,54831L,54832L,54833L,54834L,54835L,54836L,54837L,
  82326. 54838L,54839L,54840L,54841L,54842L,54843L,54844L,54845L,54846L,54847L,
  82327. 54848L,54849L,54850L,54851L,54852L,54853L,54854L,54855L,54856L,54857L,
  82328. 54858L,54859L,54860L,54861L,54862L,54863L,54864L,54865L,54866L,54867L,
  82329. 54868L,54869L,54870L,54871L,54872L,54873L,54874L,54875L,54876L,54877L,
  82330. 54878L,54879L,54880L,54881L,54882L,54883L,54884L,54885L,54886L,54887L,
  82331. 54888L,54889L,54890L,54891L,54892L,54893L,54894L,54895L,54896L,54897L,
  82332. 54898L,54899L,54900L,54901L,54902L,54903L,54904L,54905L,54906L,54907L,
  82333. 54908L,54909L,54910L,54911L,54912L,54913L,54914L,54915L,54916L,54917L,
  82334. 54918L,54919L,54920L,54921L,54922L,54923L,54924L,54925L,54926L,54927L,
  82335. 54928L,54929L,54930L,54931L,54932L,54933L,54934L,54935L,54936L,54937L,
  82336. 54938L,54939L,54940L,54941L,54942L,54943L,54944L,54945L,54946L,54947L,
  82337. 54948L,54949L,54950L,54951L,54952L,54953L,54954L,54955L,54956L,54957L,
  82338. 54958L,54959L,54960L,54961L,54962L,54963L,54964L,54965L,54966L,54967L,
  82339. 54968L,54969L,54970L,54971L,54972L,54973L,54974L,54975L,54976L,54977L,
  82340. 54978L,54979L,54980L,54981L,54982L,54983L,54984L,54985L,54986L,54987L,
  82341. 54988L,54989L,54990L,54991L,54992L,54993L,54994L,54995L,54996L,54997L,
  82342. 54998L,54999L,55000L,55001L,55002L,55003L,55004L,55005L,55006L,55007L,
  82343. 55008L,55009L,55010L,55011L,55012L,55013L,55014L,55015L,55016L,55017L,
  82344. 55018L,55019L,55020L,55021L,55022L,55023L,55024L,55025L,55026L,55027L,
  82345. 55028L,55029L,55030L,55031L,55032L,55033L,55034L,55035L,55036L,55037L,
  82346. 55038L,55039L,55040L,55041L,55042L,55043L,55044L,55045L,55046L,55047L,
  82347. 55048L,55049L,55050L,55051L,55052L,55053L,55054L,55055L,55056L,55057L,
  82348. 55058L,55059L,55060L,55061L,55062L,55063L,55064L,55065L,55066L,55067L,
  82349. 55068L,55069L,55070L,55071L,55072L,55073L,55074L,55075L,55076L,55077L,
  82350. 55078L,55079L,55080L,55081L,55082L,55083L,55084L,55085L,55086L,55087L,
  82351. 55088L,55089L,55090L,55091L,55092L,55093L,55094L,55095L,55096L,55097L,
  82352. 55098L,55099L,55100L,55101L,55102L,55103L,55104L,55105L,55106L,55107L,
  82353. 55108L,55109L,55110L,55111L,55112L,55113L,55114L,55115L,55116L,55117L,
  82354. 55118L,55119L,55120L,55121L,55122L,55123L,55124L,55125L,55126L,55127L,
  82355. 55128L,55129L,55130L,55131L,55132L,55133L,55134L,55135L,55136L,55137L,
  82356. 55138L,55139L,55140L,55141L,55142L,55143L,55144L,55145L,55146L,55147L,
  82357. 55148L,55149L,55150L,55151L,55152L,55153L,55154L,55155L,55156L,55157L,
  82358. 55158L,55159L,55160L,55161L,55162L,55163L,55164L,55165L,55166L,55167L,
  82359. 55168L,55169L,55170L,55171L,55172L,55173L,55174L,55175L,55176L,55177L,
  82360. 55178L,55179L,55180L,55181L,55182L,55183L,55184L,55185L,55186L,55187L,
  82361. 55188L,55189L,55190L,55191L,55192L,55193L,55194L,55195L,55196L,55197L,
  82362. 55198L,55199L,55200L,55201L,55202L,55203L,55204L,55205L,55206L,55207L,
  82363. 55208L,55209L,55210L,55211L,55212L,55213L,55214L,55215L,55216L,55217L,
  82364. 55218L,55219L,55220L,55221L,55222L,55223L,55224L,55225L,55226L,55227L,
  82365. 55228L,55229L,55230L,55231L,55232L,55233L,55234L,55235L,55236L,55237L,
  82366. 55238L,55239L,55240L,55241L,55242L,55243L,55244L,55245L,55246L,55247L,
  82367. 55248L,55249L,55250L,55251L,55252L,55253L,55254L,55255L,55256L,55257L,
  82368. 55258L,55259L,55260L,55261L,55262L,55263L,55264L,55265L,55266L,55267L,
  82369. 55268L,55269L,55270L,55271L,55272L,55273L,55274L,55275L,55276L,55277L,
  82370. 55278L,55279L,55280L,55281L,55282L,55283L,55284L,55285L,55286L,55287L,
  82371. 55288L,55289L,55290L,55291L,55292L,55293L,55294L,55295L,55296L,55297L,
  82372. 55298L,55299L,55300L,55301L,55302L,55303L,55304L,55305L,55306L,55307L,
  82373. 55308L,55309L,55310L,55311L,55312L,55313L,55314L,55315L,55316L,55317L,
  82374. 55318L,55319L,55320L,55321L,55322L,55323L,55324L,55325L,55326L,55327L,
  82375. 55328L,55329L,55330L,55331L,55332L,55333L,55334L,55335L,55336L,55337L,
  82376. 55338L,55339L,55340L,55341L,55342L,55343L,55344L,55345L,55346L,55347L,
  82377. 55348L,55349L,55350L,55351L,55352L,55353L,55354L,55355L,55356L,55357L,
  82378. 55358L,55359L,55360L,55361L,55362L,55363L,55364L,55365L,55366L,55367L,
  82379. 55368L,55369L,55370L,55371L,55372L,55373L,55374L,55375L,55376L,55377L,
  82380. 55378L,55379L,55380L,55381L,55382L,55383L,55384L,55385L,55386L,55387L,
  82381. 55388L,55389L,55390L,55391L,55392L,55393L,55394L,55395L,55396L,55397L,
  82382. 55398L,55399L,55400L,55401L,55402L,55403L,55404L,55405L,55406L,55407L,
  82383. 55408L,55409L,55410L,55411L,55412L,55413L,55414L,55415L,55416L,55417L,
  82384. 55418L,55419L,55420L,55421L,55422L,55423L,55424L,55425L,55426L,55427L,
  82385. 55428L,55429L,55430L,55431L,55432L,55433L,55434L,55435L,55436L,55437L,
  82386. 55438L,55439L,55440L,55441L,55442L,55443L,55444L,55445L,55446L,55447L,
  82387. 55448L,55449L,55450L,55451L,55452L,55453L,55454L,55455L,55456L,55457L,
  82388. 55458L,55459L,55460L,55461L,55462L,55463L,55464L,55465L,55466L,55467L,
  82389. 55468L,55469L,55470L,55471L,55472L,55473L,55474L,55475L,55476L,55477L,
  82390. 55478L,55479L,55480L,55481L,55482L,55483L,55484L,55485L,55486L,55487L,
  82391. 55488L,55489L,55490L,55491L,55492L,55493L,55494L,55495L,55496L,55497L,
  82392. 55498L,55499L,55500L,55501L,55502L,55503L,55504L,55505L,55506L,55507L,
  82393. 55508L,55509L,55510L,55511L,55512L,55513L,55514L,55515L,55516L,55517L,
  82394. 55518L,55519L,55520L,55521L,55522L,55523L,55524L,55525L,55526L,55527L,
  82395. 55528L,55529L,55530L,55531L,55532L,55533L,55534L,55535L,55536L,55537L,
  82396. 55538L,55539L,55540L,55541L,55542L,55543L,55544L,55545L,55546L,55547L,
  82397. 55548L,55549L,55550L,55551L,55552L,55553L,55554L,55555L,55556L,55557L,
  82398. 55558L,55559L,55560L,55561L,55562L,55563L,55564L,55565L,55566L,55567L,
  82399. 55568L,55569L,55570L,55571L,55572L,55573L,55574L,55575L,55576L,55577L,
  82400. 55578L,55579L,55580L,55581L,55582L,55583L,55584L,55585L,55586L,55587L,
  82401. 55588L,55589L,55590L,55591L,55592L,55593L,55594L,55595L,55596L,55597L,
  82402. 55598L,55599L,55600L,55601L,55602L,55603L,55604L,55605L,55606L,55607L,
  82403. 55608L,55609L,55610L,55611L,55612L,55613L,55614L,55615L,55616L,55617L,
  82404. 55618L,55619L,55620L,55621L,55622L,55623L,55624L,55625L,55626L,55627L,
  82405. 55628L,55629L,55630L,55631L,55632L,55633L,55634L,55635L,55636L,55637L,
  82406. 55638L,55639L,55640L,55641L,55642L,55643L,55644L,55645L,55646L,55647L,
  82407. 55648L,55649L,55650L,55651L,55652L,55653L,55654L,55655L,55656L,55657L,
  82408. 55658L,55659L,55660L,55661L,55662L,55663L,55664L,55665L,55666L,55667L,
  82409. 55668L,55669L,55670L,55671L,55672L,55673L,55674L,55675L,55676L,55677L,
  82410. 55678L,55679L,55680L,55681L,55682L,55683L,55684L,55685L,55686L,55687L,
  82411. 55688L,55689L,55690L,55691L,55692L,55693L,55694L,55695L,55696L,55697L,
  82412. 55698L,55699L,55700L,55701L,55702L,55703L,55704L,55705L,55706L,55707L,
  82413. 55708L,55709L,55710L,55711L,55712L,55713L,55714L,55715L,55716L,55717L,
  82414. 55718L,55719L,55720L,55721L,55722L,55723L,55724L,55725L,55726L,55727L,
  82415. 55728L,55729L,55730L,55731L,55732L,55733L,55734L,55735L,55736L,55737L,
  82416. 55738L,55739L,55740L,55741L,55742L,55743L,55744L,55745L,55746L,55747L,
  82417. 55748L,55749L,55750L,55751L,55752L,55753L,55754L,55755L,55756L,55757L,
  82418. 55758L,55759L,55760L,55761L,55762L,55763L,55764L,55765L,55766L,55767L,
  82419. 55768L,55769L,55770L,55771L,55772L,55773L,55774L,55775L,55776L,55777L,
  82420. 55778L,55779L,55780L,55781L,55782L,55783L,55784L,55785L,55786L,55787L,
  82421. 55788L,55789L,55790L,55791L,55792L,55793L,55794L,55795L,55796L,55797L,
  82422. 55798L,55799L,55800L,55801L,55802L,55803L,55804L,55805L,55806L,55807L,
  82423. 55808L,55809L,55810L,55811L,55812L,55813L,55814L,55815L,55816L,55817L,
  82424. 55818L,55819L,55820L,55821L,55822L,55823L,55824L,55825L,55826L,55827L,
  82425. 55828L,55829L,55830L,55831L,55832L,55833L,55834L,55835L,55836L,55837L,
  82426. 55838L,55839L,55840L,55841L,55842L,55843L,55844L,55845L,55846L,55847L,
  82427. 55848L,55849L,55850L,55851L,55852L,55853L,55854L,55855L,55856L,55857L,
  82428. 55858L,55859L,55860L,55861L,55862L,55863L,55864L,55865L,55866L,55867L,
  82429. 55868L,55869L,55870L,55871L,55872L,55873L,55874L,55875L,55876L,55877L,
  82430. 55878L,55879L,55880L,55881L,55882L,55883L,55884L,55885L,55886L,55887L,
  82431. 55888L,55889L,55890L,55891L,55892L,55893L,55894L,55895L,55896L,55897L,
  82432. 55898L,55899L,55900L,55901L,55902L,55903L,55904L,55905L,55906L,55907L,
  82433. 55908L,55909L,55910L,55911L,55912L,55913L,55914L,55915L,55916L,55917L,
  82434. 55918L,55919L,55920L,55921L,55922L,55923L,55924L,55925L,55926L,55927L,
  82435. 55928L,55929L,55930L,55931L,55932L,55933L,55934L,55935L,55936L,55937L,
  82436. 55938L,55939L,55940L,55941L,55942L,55943L,55944L,55945L,55946L,55947L,
  82437. 55948L,55949L,55950L,55951L,55952L,55953L,55954L,55955L,55956L,55957L,
  82438. 55958L,55959L,55960L,55961L,55962L,55963L,55964L,55965L,55966L,55967L,
  82439. 55968L,55969L,55970L,55971L,55972L,55973L,55974L,55975L,55976L,55977L,
  82440. 55978L,55979L,55980L,55981L,55982L,55983L,55984L,55985L,55986L,55987L,
  82441. 55988L,55989L,55990L,55991L,55992L,55993L,55994L,55995L,55996L,55997L,
  82442. 55998L,55999L,56000L,56001L,56002L,56003L,56004L,56005L,56006L,56007L,
  82443. 56008L,56009L,56010L,56011L,56012L,56013L,56014L,56015L,56016L,56017L,
  82444. 56018L,56019L,56020L,56021L,56022L,56023L,56024L,56025L,56026L,56027L,
  82445. 56028L,56029L,56030L,56031L,56032L,56033L,56034L,56035L,56036L,56037L,
  82446. 56038L,56039L,56040L,56041L,56042L,56043L,56044L,56045L,56046L,56047L,
  82447. 56048L,56049L,56050L,56051L,56052L,56053L,56054L,56055L,56056L,56057L,
  82448. 56058L,56059L,56060L,56061L,56062L,56063L,56064L,56065L,56066L,56067L,
  82449. 56068L,56069L,56070L,56071L,56072L,56073L,56074L,56075L,56076L,56077L,
  82450. 56078L,56079L,56080L,56081L,56082L,56083L,56084L,56085L,56086L,56087L,
  82451. 56088L,56089L,56090L,56091L,56092L,56093L,56094L,56095L,56096L,56097L,
  82452. 56098L,56099L,56100L,56101L,56102L,56103L,56104L,56105L,56106L,56107L,
  82453. 56108L,56109L,56110L,56111L,56112L,56113L,56114L,56115L,56116L,56117L,
  82454. 56118L,56119L,56120L,56121L,56122L,56123L,56124L,56125L,56126L,56127L,
  82455. 56128L,56129L,56130L,56131L,56132L,56133L,56134L,56135L,56136L,56137L,
  82456. 56138L,56139L,56140L,56141L,56142L,56143L,56144L,56145L,56146L,56147L,
  82457. 56148L,56149L,56150L,56151L,56152L,56153L,56154L,56155L,56156L,56157L,
  82458. 56158L,56159L,56160L,56161L,56162L,56163L,56164L,56165L,56166L,56167L,
  82459. 56168L,56169L,56170L,56171L,56172L,56173L,56174L,56175L,56176L,56177L,
  82460. 56178L,56179L,56180L,56181L,56182L,56183L,56184L,56185L,56186L,56187L,
  82461. 56188L,56189L,56190L,56191L,56192L,56193L,56194L,56195L,56196L,56197L,
  82462. 56198L,56199L,56200L,56201L,56202L,56203L,56204L,56205L,56206L,56207L,
  82463. 56208L,56209L,56210L,56211L,56212L,56213L,56214L,56215L,56216L,56217L,
  82464. 56218L,56219L,56220L,56221L,56222L,56223L,56224L,56225L,56226L,56227L,
  82465. 56228L,56229L,56230L,56231L,56232L,56233L,56234L,56235L,56236L,56237L,
  82466. 56238L,56239L,56240L,56241L,56242L,56243L,56244L,56245L,56246L,56247L,
  82467. 56248L,56249L,56250L,56251L,56252L,56253L,56254L,56255L,56256L,56257L,
  82468. 56258L,56259L,56260L,56261L,56262L,56263L,56264L,56265L,56266L,56267L,
  82469. 56268L,56269L,56270L,56271L,56272L,56273L,56274L,56275L,56276L,56277L,
  82470. 56278L,56279L,56280L,56281L,56282L,56283L,56284L,56285L,56286L,56287L,
  82471. 56288L,56289L,56290L,56291L,56292L,56293L,56294L,56295L,56296L,56297L,
  82472. 56298L,56299L,56300L,56301L,56302L,56303L,56304L,56305L,56306L,56307L,
  82473. 56308L,56309L,56310L,56311L,56312L,56313L,56314L,56315L,56316L,56317L,
  82474. 56318L,56319L,56320L,56321L,56322L,56323L,56324L,56325L,56326L,56327L,
  82475. 56328L,56329L,56330L,56331L,56332L,56333L,56334L,56335L,56336L,56337L,
  82476. 56338L,56339L,56340L,56341L,56342L,56343L,56344L,56345L,56346L,56347L,
  82477. 56348L,56349L,56350L,56351L,56352L,56353L,56354L,56355L,56356L,56357L,
  82478. 56358L,56359L,56360L,56361L,56362L,56363L,56364L,56365L,56366L,56367L,
  82479. 56368L,56369L,56370L,56371L,56372L,56373L,56374L,56375L,56376L,56377L,
  82480. 56378L,56379L,56380L,56381L,56382L,56383L,56384L,56385L,56386L,56387L,
  82481. 56388L,56389L,56390L,56391L,56392L,56393L,56394L,56395L,56396L,56397L,
  82482. 56398L,56399L,56400L,56401L,56402L,56403L,56404L,56405L,56406L,56407L,
  82483. 56408L,56409L,56410L,56411L,56412L,56413L,56414L,56415L,56416L,56417L,
  82484. 56418L,56419L,56420L,56421L,56422L,56423L,56424L,56425L,56426L,56427L,
  82485. 56428L,56429L,56430L,56431L,56432L,56433L,56434L,56435L,56436L,56437L,
  82486. 56438L,56439L,56440L,56441L,56442L,56443L,56444L,56445L,56446L,56447L,
  82487. 56448L,56449L,56450L,56451L,56452L,56453L,56454L,56455L,56456L,56457L,
  82488. 56458L,56459L,56460L,56461L,56462L,56463L,56464L,56465L,56466L,56467L,
  82489. 56468L,56469L,56470L,56471L,56472L,56473L,56474L,56475L,56476L,56477L,
  82490. 56478L,56479L,56480L,56481L,56482L,56483L,56484L,56485L,56486L,56487L,
  82491. 56488L,56489L,56490L,56491L,56492L,56493L,56494L,56495L,56496L,56497L,
  82492. 56498L,56499L,56500L,56501L,56502L,56503L,56504L,56505L,56506L,56507L,
  82493. 56508L,56509L,56510L,56511L,56512L,56513L,56514L,56515L,56516L,56517L,
  82494. 56518L,56519L,56520L,56521L,56522L,56523L,56524L,56525L,56526L,56527L,
  82495. 56528L,56529L,56530L,56531L,56532L,56533L,56534L,56535L,56536L,56537L,
  82496. 56538L,56539L,56540L,56541L,56542L,56543L,56544L,56545L,56546L,56547L,
  82497. 56548L,56549L,56550L,56551L,56552L,56553L,56554L,56555L,56556L,56557L,
  82498. 56558L,56559L,56560L,56561L,56562L,56563L,56564L,56565L,56566L,56567L,
  82499. 56568L,56569L,56570L,56571L,56572L,56573L,56574L,56575L,56576L,56577L,
  82500. 56578L,56579L,56580L,56581L,56582L,56583L,56584L,56585L,56586L,56587L,
  82501. 56588L,56589L,56590L,56591L,56592L,56593L,56594L,56595L,56596L,56597L,
  82502. 56598L,56599L,56600L,56601L,56602L,56603L,56604L,56605L,56606L,56607L,
  82503. 56608L,56609L,56610L,56611L,56612L,56613L,56614L,56615L,56616L,56617L,
  82504. 56618L,56619L,56620L,56621L,56622L,56623L,56624L,56625L,56626L,56627L,
  82505. 56628L,56629L,56630L,56631L,56632L,56633L,56634L,56635L,56636L,56637L,
  82506. 56638L,56639L,56640L,56641L,56642L,56643L,56644L,56645L,56646L,56647L,
  82507. 56648L,56649L,56650L,56651L,56652L,56653L,56654L,56655L,56656L,56657L,
  82508. 56658L,56659L,56660L,56661L,56662L,56663L,56664L,56665L,56666L,56667L,
  82509. 56668L,56669L,56670L,56671L,56672L,56673L,56674L,56675L,56676L,56677L,
  82510. 56678L,56679L,56680L,56681L,56682L,56683L,56684L,56685L,56686L,56687L,
  82511. 56688L,56689L,56690L,56691L,56692L,56693L,56694L,56695L,56696L,56697L,
  82512. 56698L,56699L,56700L,56701L,56702L,56703L,56704L,56705L,56706L,56707L,
  82513. 56708L,56709L,56710L,56711L,56712L,56713L,56714L,56715L,56716L,56717L,
  82514. 56718L,56719L,56720L,56721L,56722L,56723L,56724L,56725L,56726L,56727L,
  82515. 56728L,56729L,56730L,56731L,56732L,56733L,56734L,56735L,56736L,56737L,
  82516. 56738L,56739L,56740L,56741L,56742L,56743L,56744L,56745L,56746L,56747L,
  82517. 56748L,56749L,56750L,56751L,56752L,56753L,56754L,56755L,56756L,56757L,
  82518. 56758L,56759L,56760L,56761L,56762L,56763L,56764L,56765L,56766L,56767L,
  82519. 56768L,56769L,56770L,56771L,56772L,56773L,56774L,56775L,56776L,56777L,
  82520. 56778L,56779L,56780L,56781L,56782L,56783L,56784L,56785L,56786L,56787L,
  82521. 56788L,56789L,56790L,56791L,56792L,56793L,56794L,56795L,56796L,56797L,
  82522. 56798L,56799L,56800L,56801L,56802L,56803L,56804L,56805L,56806L,56807L,
  82523. 56808L,56809L,56810L,56811L,56812L,56813L,56814L,56815L,56816L,56817L,
  82524. 56818L,56819L,56820L,56821L,56822L,56823L,56824L,56825L,56826L,56827L,
  82525. 56828L,56829L,56830L,56831L,56832L,56833L,56834L,56835L,56836L,56837L,
  82526. 56838L,56839L,56840L,56841L,56842L,56843L,56844L,56845L,56846L,56847L,
  82527. 56848L,56849L,56850L,56851L,56852L,56853L,56854L,56855L,56856L,56857L,
  82528. 56858L,56859L,56860L,56861L,56862L,56863L,56864L,56865L,56866L,56867L,
  82529. 56868L,56869L,56870L,56871L,56872L,56873L,56874L,56875L,56876L,56877L,
  82530. 56878L,56879L,56880L,56881L,56882L,56883L,56884L,56885L,56886L,56887L,
  82531. 56888L,56889L,56890L,56891L,56892L,56893L,56894L,56895L,56896L,56897L,
  82532. 56898L,56899L,56900L,56901L,56902L,56903L,56904L,56905L,56906L,56907L,
  82533. 56908L,56909L,56910L,56911L,56912L,56913L,56914L,56915L,56916L,56917L,
  82534. 56918L,56919L,56920L,56921L,56922L,56923L,56924L,56925L,56926L,56927L,
  82535. 56928L,56929L,56930L,56931L,56932L,56933L,56934L,56935L,56936L,56937L,
  82536. 56938L,56939L,56940L,56941L,56942L,56943L,56944L,56945L,56946L,56947L,
  82537. 56948L,56949L,56950L,56951L,56952L,56953L,56954L,56955L,56956L,56957L,
  82538. 56958L,56959L,56960L,56961L,56962L,56963L,56964L,56965L,56966L,56967L,
  82539. 56968L,56969L,56970L,56971L,56972L,56973L,56974L,56975L,56976L,56977L,
  82540. 56978L,56979L,56980L,56981L,56982L,56983L,56984L,56985L,56986L,56987L,
  82541. 56988L,56989L,56990L,56991L,56992L,56993L,56994L,56995L,56996L,56997L,
  82542. 56998L,56999L,57000L,57001L,57002L,57003L,57004L,57005L,57006L,57007L,
  82543. 57008L,57009L,57010L,57011L,57012L,57013L,57014L,57015L,57016L,57017L,
  82544. 57018L,57019L,57020L,57021L,57022L,57023L,57024L,57025L,57026L,57027L,
  82545. 57028L,57029L,57030L,57031L,57032L,57033L,57034L,57035L,57036L,57037L,
  82546. 57038L,57039L,57040L,57041L,57042L,57043L,57044L,57045L,57046L,57047L,
  82547. 57048L,57049L,57050L,57051L,57052L,57053L,57054L,57055L,57056L,57057L,
  82548. 57058L,57059L,57060L,57061L,57062L,57063L,57064L,57065L,57066L,57067L,
  82549. 57068L,57069L,57070L,57071L,57072L,57073L,57074L,57075L,57076L,57077L,
  82550. 57078L,57079L,57080L,57081L,57082L,57083L,57084L,57085L,57086L,57087L,
  82551. 57088L,57089L,57090L,57091L,57092L,57093L,57094L,57095L,57096L,57097L,
  82552. 57098L,57099L,57100L,57101L,57102L,57103L,57104L,57105L,57106L,57107L,
  82553. 57108L,57109L,57110L,57111L,57112L,57113L,57114L,57115L,57116L,57117L,
  82554. 57118L,57119L,57120L,57121L,57122L,57123L,57124L,57125L,57126L,57127L,
  82555. 57128L,57129L,57130L,57131L,57132L,57133L,57134L,57135L,57136L,57137L,
  82556. 57138L,57139L,57140L,57141L,57142L,57143L,57144L,57145L,57146L,57147L,
  82557. 57148L,57149L,57150L,57151L,57152L,57153L,57154L,57155L,57156L,57157L,
  82558. 57158L,57159L,57160L,57161L,57162L,57163L,57164L,57165L,57166L,57167L,
  82559. 57168L,57169L,57170L,57171L,57172L,57173L,57174L,57175L,57176L,57177L,
  82560. 57178L,57179L,57180L,57181L,57182L,57183L,57184L,57185L,57186L,57187L,
  82561. 57188L,57189L,57190L,57191L,57192L,57193L,57194L,57195L,57196L,57197L,
  82562. 57198L,57199L,57200L,57201L,57202L,57203L,57204L,57205L,57206L,57207L,
  82563. 57208L,57209L,57210L,57211L,57212L,57213L,57214L,57215L,57216L,57217L,
  82564. 57218L,57219L,57220L,57221L,57222L,57223L,57224L,57225L,57226L,57227L,
  82565. 57228L,57229L,57230L,57231L,57232L,57233L,57234L,57235L,57236L,57237L,
  82566. 57238L,57239L,57240L,57241L,57242L,57243L,57244L,57245L,57246L,57247L,
  82567. 57248L,57249L,57250L,57251L,57252L,57253L,57254L,57255L,57256L,57257L,
  82568. 57258L,57259L,57260L,57261L,57262L,57263L,57264L,57265L,57266L,57267L,
  82569. 57268L,57269L,57270L,57271L,57272L,57273L,57274L,57275L,57276L,57277L,
  82570. 57278L,57279L,57280L,57281L,57282L,57283L,57284L,57285L,57286L,57287L,
  82571. 57288L,57289L,57290L,57291L,57292L,57293L,57294L,57295L,57296L,57297L,
  82572. 57298L,57299L,57300L,57301L,57302L,57303L,57304L,57305L,57306L,57307L,
  82573. 57308L,57309L,57310L,57311L,57312L,57313L,57314L,57315L,57316L,57317L,
  82574. 57318L,57319L,57320L,57321L,57322L,57323L,57324L,57325L,57326L,57327L,
  82575. 57328L,57329L,57330L,57331L,57332L,57333L,57334L,57335L,57336L,57337L,
  82576. 57338L,57339L,57340L,57341L,57342L,57343L,57344L,57345L,57346L,57347L,
  82577. 57348L,57349L,57350L,57351L,57352L,57353L,57354L,57355L,57356L,57357L,
  82578. 57358L,57359L,57360L,57361L,57362L,57363L,57364L,57365L,57366L,57367L,
  82579. 57368L,57369L,57370L,57371L,57372L,57373L,57374L,57375L,57376L,57377L,
  82580. 57378L,57379L,57380L,57381L,57382L,57383L,57384L,57385L,57386L,57387L,
  82581. 57388L,57389L,57390L,57391L,57392L,57393L,57394L,57395L,57396L,57397L,
  82582. 57398L,57399L,57400L,57401L,57402L,57403L,57404L,57405L,57406L,57407L,
  82583. 57408L,57409L,57410L,57411L,57412L,57413L,57414L,57415L,57416L,57417L,
  82584. 57418L,57419L,57420L,57421L,57422L,57423L,57424L,57425L,57426L,57427L,
  82585. 57428L,57429L,57430L,57431L,57432L,57433L,57434L,57435L,57436L,57437L,
  82586. 57438L,57439L,57440L,57441L,57442L,57443L,57444L,57445L,57446L,57447L,
  82587. 57448L,57449L,57450L,57451L,57452L,57453L,57454L,57455L,57456L,57457L,
  82588. 57458L,57459L,57460L,57461L,57462L,57463L,57464L,57465L,57466L,57467L,
  82589. 57468L,57469L,57470L,57471L,57472L,57473L,57474L,57475L,57476L,57477L,
  82590. 57478L,57479L,57480L,57481L,57482L,57483L,57484L,57485L,57486L,57487L,
  82591. 57488L,57489L,57490L,57491L,57492L,57493L,57494L,57495L,57496L,57497L,
  82592. 57498L,57499L,57500L,57501L,57502L,57503L,57504L,57505L,57506L,57507L,
  82593. 57508L,57509L,57510L,57511L,57512L,57513L,57514L,57515L,57516L,57517L,
  82594. 57518L,57519L,57520L,57521L,57522L,57523L,57524L,57525L,57526L,57527L,
  82595. 57528L,57529L,57530L,57531L,57532L,57533L,57534L,57535L,57536L,57537L,
  82596. 57538L,57539L,57540L,57541L,57542L,57543L,57544L,57545L,57546L,57547L,
  82597. 57548L,57549L,57550L,57551L,57552L,57553L,57554L,57555L,57556L,57557L,
  82598. 57558L,57559L,57560L,57561L,57562L,57563L,57564L,57565L,57566L,57567L,
  82599. 57568L,57569L,57570L,57571L,57572L,57573L,57574L,57575L,57576L,57577L,
  82600. 57578L,57579L,57580L,57581L,57582L,57583L,57584L,57585L,57586L,57587L,
  82601. 57588L,57589L,57590L,57591L,57592L,57593L,57594L,57595L,57596L,57597L,
  82602. 57598L,57599L,57600L,57601L,57602L,57603L,57604L,57605L,57606L,57607L,
  82603. 57608L,57609L,57610L,57611L,57612L,57613L,57614L,57615L,57616L,57617L,
  82604. 57618L,57619L,57620L,57621L,57622L,57623L,57624L,57625L,57626L,57627L,
  82605. 57628L,57629L,57630L,57631L,57632L,57633L,57634L,57635L,57636L,57637L,
  82606. 57638L,57639L,57640L,57641L,57642L,57643L,57644L,57645L,57646L,57647L,
  82607. 57648L,57649L,57650L,57651L,57652L,57653L,57654L,57655L,57656L,57657L,
  82608. 57658L,57659L,57660L,57661L,57662L,57663L,57664L,57665L,57666L,57667L,
  82609. 57668L,57669L,57670L,57671L,57672L,57673L,57674L,57675L,57676L,57677L,
  82610. 57678L,57679L,57680L,57681L,57682L,57683L,57684L,57685L,57686L,57687L,
  82611. 57688L,57689L,57690L,57691L,57692L,57693L,57694L,57695L,57696L,57697L,
  82612. 57698L,57699L,57700L,57701L,57702L,57703L,57704L,57705L,57706L,57707L,
  82613. 57708L,57709L,57710L,57711L,57712L,57713L,57714L,57715L,57716L,57717L,
  82614. 57718L,57719L,57720L,57721L,57722L,57723L,57724L,57725L,57726L,57727L,
  82615. 57728L,57729L,57730L,57731L,57732L,57733L,57734L,57735L,57736L,57737L,
  82616. 57738L,57739L,57740L,57741L,57742L,57743L,57744L,57745L,57746L,57747L,
  82617. 57748L,57749L,57750L,57751L,57752L,57753L,57754L,57755L,57756L,57757L,
  82618. 57758L,57759L,57760L,57761L,57762L,57763L,57764L,57765L,57766L,57767L,
  82619. 57768L,57769L,57770L,57771L,57772L,57773L,57774L,57775L,57776L,57777L,
  82620. 57778L,57779L,57780L,57781L,57782L,57783L,57784L,57785L,57786L,57787L,
  82621. 57788L,57789L,57790L,57791L,57792L,57793L,57794L,57795L,57796L,57797L,
  82622. 57798L,57799L,57800L,57801L,57802L,57803L,57804L,57805L,57806L,57807L,
  82623. 57808L,57809L,57810L,57811L,57812L,57813L,57814L,57815L,57816L,57817L,
  82624. 57818L,57819L,57820L,57821L,57822L,57823L,57824L,57825L,57826L,57827L,
  82625. 57828L,57829L,57830L,57831L,57832L,57833L,57834L,57835L,57836L,57837L,
  82626. 57838L,57839L,57840L,57841L,57842L,57843L,57844L,57845L,57846L,57847L,
  82627. 57848L,57849L,57850L,57851L,57852L,57853L,57854L,57855L,57856L,57857L,
  82628. 57858L,57859L,57860L,57861L,57862L,57863L,57864L,57865L,57866L,57867L,
  82629. 57868L,57869L,57870L,57871L,57872L,57873L,57874L,57875L,57876L,57877L,
  82630. 57878L,57879L,57880L,57881L,57882L,57883L,57884L,57885L,57886L,57887L,
  82631. 57888L,57889L,57890L,57891L,57892L,57893L,57894L,57895L,57896L,57897L,
  82632. 57898L,57899L,57900L,57901L,57902L,57903L,57904L,57905L,57906L,57907L,
  82633. 57908L,57909L,57910L,57911L,57912L,57913L,57914L,57915L,57916L,57917L,
  82634. 57918L,57919L,57920L,57921L,57922L,57923L,57924L,57925L,57926L,57927L,
  82635. 57928L,57929L,57930L,57931L,57932L,57933L,57934L,57935L,57936L,57937L,
  82636. 57938L,57939L,57940L,57941L,57942L,57943L,57944L,57945L,57946L,57947L,
  82637. 57948L,57949L,57950L,57951L,57952L,57953L,57954L,57955L,57956L,57957L,
  82638. 57958L,57959L,57960L,57961L,57962L,57963L,57964L,57965L,57966L,57967L,
  82639. 57968L,57969L,57970L,57971L,57972L,57973L,57974L,57975L,57976L,57977L,
  82640. 57978L,57979L,57980L,57981L,57982L,57983L,57984L,57985L,57986L,57987L,
  82641. 57988L,57989L,57990L,57991L,57992L,57993L,57994L,57995L,57996L,57997L,
  82642. 57998L,57999L,58000L,58001L,58002L,58003L,58004L,58005L,58006L,58007L,
  82643. 58008L,58009L,58010L,58011L,58012L,58013L,58014L,58015L,58016L,58017L,
  82644. 58018L,58019L,58020L,58021L,58022L,58023L,58024L,58025L,58026L,58027L,
  82645. 58028L,58029L,58030L,58031L,58032L,58033L,58034L,58035L,58036L,58037L,
  82646. 58038L,58039L,58040L,58041L,58042L,58043L,58044L,58045L,58046L,58047L,
  82647. 58048L,58049L,58050L,58051L,58052L,58053L,58054L,58055L,58056L,58057L,
  82648. 58058L,58059L,58060L,58061L,58062L,58063L,58064L,58065L,58066L,58067L,
  82649. 58068L,58069L,58070L,58071L,58072L,58073L,58074L,58075L,58076L,58077L,
  82650. 58078L,58079L,58080L,58081L,58082L,58083L,58084L,58085L,58086L,58087L,
  82651. 58088L,58089L,58090L,58091L,58092L,58093L,58094L,58095L,58096L,58097L,
  82652. 58098L,58099L,58100L,58101L,58102L,58103L,58104L,58105L,58106L,58107L,
  82653. 58108L,58109L,58110L,58111L,58112L,58113L,58114L,58115L,58116L,58117L,
  82654. 58118L,58119L,58120L,58121L,58122L,58123L,58124L,58125L,58126L,58127L,
  82655. 58128L,58129L,58130L,58131L,58132L,58133L,58134L,58135L,58136L,58137L,
  82656. 58138L,58139L,58140L,58141L,58142L,58143L,58144L,58145L,58146L,58147L,
  82657. 58148L,58149L,58150L,58151L,58152L,58153L,58154L,58155L,58156L,58157L,
  82658. 58158L,58159L,58160L,58161L,58162L,58163L,58164L,58165L,58166L,58167L,
  82659. 58168L,58169L,58170L,58171L,58172L,58173L,58174L,58175L,58176L,58177L,
  82660. 58178L,58179L,58180L,58181L,58182L,58183L,58184L,58185L,58186L,58187L,
  82661. 58188L,58189L,58190L,58191L,58192L,58193L,58194L,58195L,58196L,58197L,
  82662. 58198L,58199L,58200L,58201L,58202L,58203L,58204L,58205L,58206L,58207L,
  82663. 58208L,58209L,58210L,58211L,58212L,58213L,58214L,58215L,58216L,58217L,
  82664. 58218L,58219L,58220L,58221L,58222L,58223L,58224L,58225L,58226L,58227L,
  82665. 58228L,58229L,58230L,58231L,58232L,58233L,58234L,58235L,58236L,58237L,
  82666. 58238L,58239L,58240L,58241L,58242L,58243L,58244L,58245L,58246L,58247L,
  82667. 58248L,58249L,58250L,58251L,58252L,58253L,58254L,58255L,58256L,58257L,
  82668. 58258L,58259L,58260L,58261L,58262L,58263L,58264L,58265L,58266L,58267L,
  82669. 58268L,58269L,58270L,58271L,58272L,58273L,58274L,58275L,58276L,58277L,
  82670. 58278L,58279L,58280L,58281L,58282L,58283L,58284L,58285L,58286L,58287L,
  82671. 58288L,58289L,58290L,58291L,58292L,58293L,58294L,58295L,58296L,58297L,
  82672. 58298L,58299L,58300L,58301L,58302L,58303L,58304L,58305L,58306L,58307L,
  82673. 58308L,58309L,58310L,58311L,58312L,58313L,58314L,58315L,58316L,58317L,
  82674. 58318L,58319L,58320L,58321L,58322L,58323L,58324L,58325L,58326L,58327L,
  82675. 58328L,58329L,58330L,58331L,58332L,58333L,58334L,58335L,58336L,58337L,
  82676. 58338L,58339L,58340L,58341L,58342L,58343L,58344L,58345L,58346L,58347L,
  82677. 58348L,58349L,58350L,58351L,58352L,58353L,58354L,58355L,58356L,58357L,
  82678. 58358L,58359L,58360L,58361L,58362L,58363L,58364L,58365L,58366L,58367L,
  82679. 58368L,58369L,58370L,58371L,58372L,58373L,58374L,58375L,58376L,58377L,
  82680. 58378L,58379L,58380L,58381L,58382L,58383L,58384L,58385L,58386L,58387L,
  82681. 58388L,58389L,58390L,58391L,58392L,58393L,58394L,58395L,58396L,58397L,
  82682. 58398L,58399L,58400L,58401L,58402L,58403L,58404L,58405L,58406L,58407L,
  82683. 58408L,58409L,58410L,58411L,58412L,58413L,58414L,58415L,58416L,58417L,
  82684. 58418L,58419L,58420L,58421L,58422L,58423L,58424L,58425L,58426L,58427L,
  82685. 58428L,58429L,58430L,58431L,58432L,58433L,58434L,58435L,58436L,58437L,
  82686. 58438L,58439L,58440L,58441L,58442L,58443L,58444L,58445L,58446L,58447L,
  82687. 58448L,58449L,58450L,58451L,58452L,58453L,58454L,58455L,58456L,58457L,
  82688. 58458L,58459L,58460L,58461L,58462L,58463L,58464L,58465L,58466L,58467L,
  82689. 58468L,58469L,58470L,58471L,58472L,58473L,58474L,58475L,58476L,58477L,
  82690. 58478L,58479L,58480L,58481L,58482L,58483L,58484L,58485L,58486L,58487L,
  82691. 58488L,58489L,58490L,58491L,58492L,58493L,58494L,58495L,58496L,58497L,
  82692. 58498L,58499L,58500L,58501L,58502L,58503L,58504L,58505L,58506L,58507L,
  82693. 58508L,58509L,58510L,58511L,58512L,58513L,58514L,58515L,58516L,58517L,
  82694. 58518L,58519L,58520L,58521L,58522L,58523L,58524L,58525L,58526L,58527L,
  82695. 58528L,58529L,58530L,58531L,58532L,58533L,58534L,58535L,58536L,58537L,
  82696. 58538L,58539L,58540L,58541L,58542L,58543L,58544L,58545L,58546L,58547L,
  82697. 58548L,58549L,58550L,58551L,58552L,58553L,58554L,58555L,58556L,58557L,
  82698. 58558L,58559L,58560L,58561L,58562L,58563L,58564L,58565L,58566L,58567L,
  82699. 58568L,58569L,58570L,58571L,58572L,58573L,58574L,58575L,58576L,58577L,
  82700. 58578L,58579L,58580L,58581L,58582L,58583L,58584L,58585L,58586L,58587L,
  82701. 58588L,58589L,58590L,58591L,58592L,58593L,58594L,58595L,58596L,58597L,
  82702. 58598L,58599L,58600L,58601L,58602L,58603L,58604L,58605L,58606L,58607L,
  82703. 58608L,58609L,58610L,58611L,58612L,58613L,58614L,58615L,58616L,58617L,
  82704. 58618L,58619L,58620L,58621L,58622L,58623L,58624L,58625L,58626L,58627L,
  82705. 58628L,58629L,58630L,58631L,58632L,58633L,58634L,58635L,58636L,58637L,
  82706. 58638L,58639L,58640L,58641L,58642L,58643L,58644L,58645L,58646L,58647L,
  82707. 58648L,58649L,58650L,58651L,58652L,58653L,58654L,58655L,58656L,58657L,
  82708. 58658L,58659L,58660L,58661L,58662L,58663L,58664L,58665L,58666L,58667L,
  82709. 58668L,58669L,58670L,58671L,58672L,58673L,58674L,58675L,58676L,58677L,
  82710. 58678L,58679L,58680L,58681L,58682L,58683L,58684L,58685L,58686L,58687L,
  82711. 58688L,58689L,58690L,58691L,58692L,58693L,58694L,58695L,58696L,58697L,
  82712. 58698L,58699L,58700L,58701L,58702L,58703L,58704L,58705L,58706L,58707L,
  82713. 58708L,58709L,58710L,58711L,58712L,58713L,58714L,58715L,58716L,58717L,
  82714. 58718L,58719L,58720L,58721L,58722L,58723L,58724L,58725L,58726L,58727L,
  82715. 58728L,58729L,58730L,58731L,58732L,58733L,58734L,58735L,58736L,58737L,
  82716. 58738L,58739L,58740L,58741L,58742L,58743L,58744L,58745L,58746L,58747L,
  82717. 58748L,58749L,58750L,58751L,58752L,58753L,58754L,58755L,58756L,58757L,
  82718. 58758L,58759L,58760L,58761L,58762L,58763L,58764L,58765L,58766L,58767L,
  82719. 58768L,58769L,58770L,58771L,58772L,58773L,58774L,58775L,58776L,58777L,
  82720. 58778L,58779L,58780L,58781L,58782L,58783L,58784L,58785L,58786L,58787L,
  82721. 58788L,58789L,58790L,58791L,58792L,58793L,58794L,58795L,58796L,58797L,
  82722. 58798L,58799L,58800L,58801L,58802L,58803L,58804L,58805L,58806L,58807L,
  82723. 58808L,58809L,58810L,58811L,58812L,58813L,58814L,58815L,58816L,58817L,
  82724. 58818L,58819L,58820L,58821L,58822L,58823L,58824L,58825L,58826L,58827L,
  82725. 58828L,58829L,58830L,58831L,58832L,58833L,58834L,58835L,58836L,58837L,
  82726. 58838L,58839L,58840L,58841L,58842L,58843L,58844L,58845L,58846L,58847L,
  82727. 58848L,58849L,58850L,58851L,58852L,58853L,58854L,58855L,58856L,58857L,
  82728. 58858L,58859L,58860L,58861L,58862L,58863L,58864L,58865L,58866L,58867L,
  82729. 58868L,58869L,58870L,58871L,58872L,58873L,58874L,58875L,58876L,58877L,
  82730. 58878L,58879L,58880L,58881L,58882L,58883L,58884L,58885L,58886L,58887L,
  82731. 58888L,58889L,58890L,58891L,58892L,58893L,58894L,58895L,58896L,58897L,
  82732. 58898L,58899L,58900L,58901L,58902L,58903L,58904L,58905L,58906L,58907L,
  82733. 58908L,58909L,58910L,58911L,58912L,58913L,58914L,58915L,58916L,58917L,
  82734. 58918L,58919L,58920L,58921L,58922L,58923L,58924L,58925L,58926L,58927L,
  82735. 58928L,58929L,58930L,58931L,58932L,58933L,58934L,58935L,58936L,58937L,
  82736. 58938L,58939L,58940L,58941L,58942L,58943L,58944L,58945L,58946L,58947L,
  82737. 58948L,58949L,58950L,58951L,58952L,58953L,58954L,58955L,58956L,58957L,
  82738. 58958L,58959L,58960L,58961L,58962L,58963L,58964L,58965L,58966L,58967L,
  82739. 58968L,58969L,58970L,58971L,58972L,58973L,58974L,58975L,58976L,58977L,
  82740. 58978L,58979L,58980L,58981L,58982L,58983L,58984L,58985L,58986L,58987L,
  82741. 58988L,58989L,58990L,58991L,58992L,58993L,58994L,58995L,58996L,58997L,
  82742. 58998L,58999L,59000L,59001L,59002L,59003L,59004L,59005L,59006L,59007L,
  82743. 59008L,59009L,59010L,59011L,59012L,59013L,59014L,59015L,59016L,59017L,
  82744. 59018L,59019L,59020L,59021L,59022L,59023L,59024L,59025L,59026L,59027L,
  82745. 59028L,59029L,59030L,59031L,59032L,59033L,59034L,59035L,59036L,59037L,
  82746. 59038L,59039L,59040L,59041L,59042L,59043L,59044L,59045L,59046L,59047L,
  82747. 59048L,59049L,59050L,59051L,59052L,59053L,59054L,59055L,59056L,59057L,
  82748. 59058L,59059L,59060L,59061L,59062L,59063L,59064L,59065L,59066L,59067L,
  82749. 59068L,59069L,59070L,59071L,59072L,59073L,59074L,59075L,59076L,59077L,
  82750. 59078L,59079L,59080L,59081L,59082L,59083L,59084L,59085L,59086L,59087L,
  82751. 59088L,59089L,59090L,59091L,59092L,59093L,59094L,59095L,59096L,59097L,
  82752. 59098L,59099L,59100L,59101L,59102L,59103L,59104L,59105L,59106L,59107L,
  82753. 59108L,59109L,59110L,59111L,59112L,59113L,59114L,59115L,59116L,59117L,
  82754. 59118L,59119L,59120L,59121L,59122L,59123L,59124L,59125L,59126L,59127L,
  82755. 59128L,59129L,59130L,59131L,59132L,59133L,59134L,59135L,59136L,59137L,
  82756. 59138L,59139L,59140L,59141L,59142L,59143L,59144L,59145L,59146L,59147L,
  82757. 59148L,59149L,59150L,59151L,59152L,59153L,59154L,59155L,59156L,59157L,
  82758. 59158L,59159L,59160L,59161L,59162L,59163L,59164L,59165L,59166L,59167L,
  82759. 59168L,59169L,59170L,59171L,59172L,59173L,59174L,59175L,59176L,59177L,
  82760. 59178L,59179L,59180L,59181L,59182L,59183L,59184L,59185L,59186L,59187L,
  82761. 59188L,59189L,59190L,59191L,59192L,59193L,59194L,59195L,59196L,59197L,
  82762. 59198L,59199L,59200L,59201L,59202L,59203L,59204L,59205L,59206L,59207L,
  82763. 59208L,59209L,59210L,59211L,59212L,59213L,59214L,59215L,59216L,59217L,
  82764. 59218L,59219L,59220L,59221L,59222L,59223L,59224L,59225L,59226L,59227L,
  82765. 59228L,59229L,59230L,59231L,59232L,59233L,59234L,59235L,59236L,59237L,
  82766. 59238L,59239L,59240L,59241L,59242L,59243L,59244L,59245L,59246L,59247L,
  82767. 59248L,59249L,59250L,59251L,59252L,59253L,59254L,59255L,59256L,59257L,
  82768. 59258L,59259L,59260L,59261L,59262L,59263L,59264L,59265L,59266L,59267L,
  82769. 59268L,59269L,59270L,59271L,59272L,59273L,59274L,59275L,59276L,59277L,
  82770. 59278L,59279L,59280L,59281L,59282L,59283L,59284L,59285L,59286L,59287L,
  82771. 59288L,59289L,59290L,59291L,59292L,59293L,59294L,59295L,59296L,59297L,
  82772. 59298L,59299L,59300L,59301L,59302L,59303L,59304L,59305L,59306L,59307L,
  82773. 59308L,59309L,59310L,59311L,59312L,59313L,59314L,59315L,59316L,59317L,
  82774. 59318L,59319L,59320L,59321L,59322L,59323L,59324L,59325L,59326L,59327L,
  82775. 59328L,59329L,59330L,59331L,59332L,59333L,59334L,59335L,59336L,59337L,
  82776. 59338L,59339L,59340L,59341L,59342L,59343L,59344L,59345L,59346L,59347L,
  82777. 59348L,59349L,59350L,59351L,59352L,59353L,59354L,59355L,59356L,59357L,
  82778. 59358L,59359L,59360L,59361L,59362L,59363L,59364L,59365L,59366L,59367L,
  82779. 59368L,59369L,59370L,59371L,59372L,59373L,59374L,59375L,59376L,59377L,
  82780. 59378L,59379L,59380L,59381L,59382L,59383L,59384L,59385L,59386L,59387L,
  82781. 59388L,59389L,59390L,59391L,59392L,59393L,59394L,59395L,59396L,59397L,
  82782. 59398L,59399L,59400L,59401L,59402L,59403L,59404L,59405L,59406L,59407L,
  82783. 59408L,59409L,59410L,59411L,59412L,59413L,59414L,59415L,59416L,59417L,
  82784. 59418L,59419L,59420L,59421L,59422L,59423L,59424L,59425L,59426L,59427L,
  82785. 59428L,59429L,59430L,59431L,59432L,59433L,59434L,59435L,59436L,59437L,
  82786. 59438L,59439L,59440L,59441L,59442L,59443L,59444L,59445L,59446L,59447L,
  82787. 59448L,59449L,59450L,59451L,59452L,59453L,59454L,59455L,59456L,59457L,
  82788. 59458L,59459L,59460L,59461L,59462L,59463L,59464L,59465L,59466L,59467L,
  82789. 59468L,59469L,59470L,59471L,59472L,59473L,59474L,59475L,59476L,59477L,
  82790. 59478L,59479L,59480L,59481L,59482L,59483L,59484L,59485L,59486L,59487L,
  82791. 59488L,59489L,59490L,59491L,59492L,59493L,59494L,59495L,59496L,59497L,
  82792. 59498L,59499L,59500L,59501L,59502L,59503L,59504L,59505L,59506L,59507L,
  82793. 59508L,59509L,59510L,59511L,59512L,59513L,59514L,59515L,59516L,59517L,
  82794. 59518L,59519L,59520L,59521L,59522L,59523L,59524L,59525L,59526L,59527L,
  82795. 59528L,59529L,59530L,59531L,59532L,59533L,59534L,59535L,59536L,59537L,
  82796. 59538L,59539L,59540L,59541L,59542L,59543L,59544L,59545L,59546L,59547L,
  82797. 59548L,59549L,59550L,59551L,59552L,59553L,59554L,59555L,59556L,59557L,
  82798. 59558L,59559L,59560L,59561L,59562L,59563L,59564L,59565L,59566L,59567L,
  82799. 59568L,59569L,59570L,59571L,59572L,59573L,59574L,59575L,59576L,59577L,
  82800. 59578L,59579L,59580L,59581L,59582L,59583L,59584L,59585L,59586L,59587L,
  82801. 59588L,59589L,59590L,59591L,59592L,59593L,59594L,59595L,59596L,59597L,
  82802. 59598L,59599L,59600L,59601L,59602L,59603L,59604L,59605L,59606L,59607L,
  82803. 59608L,59609L,59610L,59611L,59612L,59613L,59614L,59615L,59616L,59617L,
  82804. 59618L,59619L,59620L,59621L,59622L,59623L,59624L,59625L,59626L,59627L,
  82805. 59628L,59629L,59630L,59631L,59632L,59633L,59634L,59635L,59636L,59637L,
  82806. 59638L,59639L,59640L,59641L,59642L,59643L,59644L,59645L,59646L,59647L,
  82807. 59648L,59649L,59650L,59651L,59652L,59653L,59654L,59655L,59656L,59657L,
  82808. 59658L,59659L,59660L,59661L,59662L,59663L,59664L,59665L,59666L,59667L,
  82809. 59668L,59669L,59670L,59671L,59672L,59673L,59674L,59675L,59676L,59677L,
  82810. 59678L,59679L,59680L,59681L,59682L,59683L,59684L,59685L,59686L,59687L,
  82811. 59688L,59689L,59690L,59691L,59692L,59693L,59694L,59695L,59696L,59697L,
  82812. 59698L,59699L,59700L,59701L,59702L,59703L,59704L,59705L,59706L,59707L,
  82813. 59708L,59709L,59710L,59711L,59712L,59713L,59714L,59715L,59716L,59717L,
  82814. 59718L,59719L,59720L,59721L,59722L,59723L,59724L,59725L,59726L,59727L,
  82815. 59728L,59729L,59730L,59731L,59732L,59733L,59734L,59735L,59736L,59737L,
  82816. 59738L,59739L,59740L,59741L,59742L,59743L,59744L,59745L,59746L,59747L,
  82817. 59748L,59749L,59750L,59751L,59752L,59753L,59754L,59755L,59756L,59757L,
  82818. 59758L,59759L,59760L,59761L,59762L,59763L,59764L,59765L,59766L,59767L,
  82819. 59768L,59769L,59770L,59771L,59772L,59773L,59774L,59775L,59776L,59777L,
  82820. 59778L,59779L,59780L,59781L,59782L,59783L,59784L,59785L,59786L,59787L,
  82821. 59788L,59789L,59790L,59791L,59792L,59793L,59794L,59795L,59796L,59797L,
  82822. 59798L,59799L,59800L,59801L,59802L,59803L,59804L,59805L,59806L,59807L,
  82823. 59808L,59809L,59810L,59811L,59812L,59813L,59814L,59815L,59816L,59817L,
  82824. 59818L,59819L,59820L,59821L,59822L,59823L,59824L,59825L,59826L,59827L,
  82825. 59828L,59829L,59830L,59831L,59832L,59833L,59834L,59835L,59836L,59837L,
  82826. 59838L,59839L,59840L,59841L,59842L,59843L,59844L,59845L,59846L,59847L,
  82827. 59848L,59849L,59850L,59851L,59852L,59853L,59854L,59855L,59856L,59857L,
  82828. 59858L,59859L,59860L,59861L,59862L,59863L,59864L,59865L,59866L,59867L,
  82829. 59868L,59869L,59870L,59871L,59872L,59873L,59874L,59875L,59876L,59877L,
  82830. 59878L,59879L,59880L,59881L,59882L,59883L,59884L,59885L,59886L,59887L,
  82831. 59888L,59889L,59890L,59891L,59892L,59893L,59894L,59895L,59896L,59897L,
  82832. 59898L,59899L,59900L,59901L,59902L,59903L,59904L,59905L,59906L,59907L,
  82833. 59908L,59909L,59910L,59911L,59912L,59913L,59914L,59915L,59916L,59917L,
  82834. 59918L,59919L,59920L,59921L,59922L,59923L,59924L,59925L,59926L,59927L,
  82835. 59928L,59929L,59930L,59931L,59932L,59933L,59934L,59935L,59936L,59937L,
  82836. 59938L,59939L,59940L,59941L,59942L,59943L,59944L,59945L,59946L,59947L,
  82837. 59948L,59949L,59950L,59951L,59952L,59953L,59954L,59955L,59956L,59957L,
  82838. 59958L,59959L,59960L,59961L,59962L,59963L,59964L,59965L,59966L,59967L,
  82839. 59968L,59969L,59970L,59971L,59972L,59973L,59974L,59975L,59976L,59977L,
  82840. 59978L,59979L,59980L,59981L,59982L,59983L,59984L,59985L,59986L,59987L,
  82841. 59988L,59989L,59990L,59991L,59992L,59993L,59994L,59995L,59996L,59997L,
  82842. 59998L,59999L,60000L,60001L,60002L,60003L,60004L,60005L,60006L,60007L,
  82843. 60008L,60009L,60010L,60011L,60012L,60013L,60014L,60015L,60016L,60017L,
  82844. 60018L,60019L,60020L,60021L,60022L,60023L,60024L,60025L,60026L,60027L,
  82845. 60028L,60029L,60030L,60031L,60032L,60033L,60034L,60035L,60036L,60037L,
  82846. 60038L,60039L,60040L,60041L,60042L,60043L,60044L,60045L,60046L,60047L,
  82847. 60048L,60049L,60050L,60051L,60052L,60053L,60054L,60055L,60056L,60057L,
  82848. 60058L,60059L,60060L,60061L,60062L,60063L,60064L,60065L,60066L,60067L,
  82849. 60068L,60069L,60070L,60071L,60072L,60073L,60074L,60075L,60076L,60077L,
  82850. 60078L,60079L,60080L,60081L,60082L,60083L,60084L,60085L,60086L,60087L,
  82851. 60088L,60089L,60090L,60091L,60092L,60093L,60094L,60095L,60096L,60097L,
  82852. 60098L,60099L,60100L,60101L,60102L,60103L,60104L,60105L,60106L,60107L,
  82853. 60108L,60109L,60110L,60111L,60112L,60113L,60114L,60115L,60116L,60117L,
  82854. 60118L,60119L,60120L,60121L,60122L,60123L,60124L,60125L,60126L,60127L,
  82855. 60128L,60129L,60130L,60131L,60132L,60133L,60134L,60135L,60136L,60137L,
  82856. 60138L,60139L,60140L,60141L,60142L,60143L,60144L,60145L,60146L,60147L,
  82857. 60148L,60149L,60150L,60151L,60152L,60153L,60154L,60155L,60156L,60157L,
  82858. 60158L,60159L,60160L,60161L,60162L,60163L,60164L,60165L,60166L,60167L,
  82859. 60168L,60169L,60170L,60171L,60172L,60173L,60174L,60175L,60176L,60177L,
  82860. 60178L,60179L,60180L,60181L,60182L,60183L,60184L,60185L,60186L,60187L,
  82861. 60188L,60189L,60190L,60191L,60192L,60193L,60194L,60195L,60196L,60197L,
  82862. 60198L,60199L,60200L,60201L,60202L,60203L,60204L,60205L,60206L,60207L,
  82863. 60208L,60209L,60210L,60211L,60212L,60213L,60214L,60215L,60216L,60217L,
  82864. 60218L,60219L,60220L,60221L,60222L,60223L,60224L,60225L,60226L,60227L,
  82865. 60228L,60229L,60230L,60231L,60232L,60233L,60234L,60235L,60236L,60237L,
  82866. 60238L,60239L,60240L,60241L,60242L,60243L,60244L,60245L,60246L,60247L,
  82867. 60248L,60249L,60250L,60251L,60252L,60253L,60254L,60255L,60256L,60257L,
  82868. 60258L,60259L,60260L,60261L,60262L,60263L,60264L,60265L,60266L,60267L,
  82869. 60268L,60269L,60270L,60271L,60272L,60273L,60274L,60275L,60276L,60277L,
  82870. 60278L,60279L,60280L,60281L,60282L,60283L,60284L,60285L,60286L,60287L,
  82871. 60288L,60289L,60290L,60291L,60292L,60293L,60294L,60295L,60296L,60297L,
  82872. 60298L,60299L,60300L,60301L,60302L,60303L,60304L,60305L,60306L,60307L,
  82873. 60308L,60309L,60310L,60311L,60312L,60313L,60314L,60315L,60316L,60317L,
  82874. 60318L,60319L,60320L,60321L,60322L,60323L,60324L,60325L,60326L,60327L,
  82875. 60328L,60329L,60330L,60331L,60332L,60333L,60334L,60335L,60336L,60337L,
  82876. 60338L,60339L,60340L,60341L,60342L,60343L,60344L,60345L,60346L,60347L,
  82877. 60348L,60349L,60350L,60351L,60352L,60353L,60354L,60355L,60356L,60357L,
  82878. 60358L,60359L,60360L,60361L,60362L,60363L,60364L,60365L,60366L,60367L,
  82879. 60368L,60369L,60370L,60371L,60372L,60373L,60374L,60375L,60376L,60377L,
  82880. 60378L,60379L,60380L,60381L,60382L,60383L,60384L,60385L,60386L,60387L,
  82881. 60388L,60389L,60390L,60391L,60392L,60393L,60394L,60395L,60396L,60397L,
  82882. 60398L,60399L,60400L,60401L,60402L,60403L,60404L,60405L,60406L,60407L,
  82883. 60408L,60409L,60410L,60411L,60412L,60413L,60414L,60415L,60416L,60417L,
  82884. 60418L,60419L,60420L,60421L,60422L,60423L,60424L,60425L,60426L,60427L,
  82885. 60428L,60429L,60430L,60431L,60432L,60433L,60434L,60435L,60436L,60437L,
  82886. 60438L,60439L,60440L,60441L,60442L,60443L,60444L,60445L,60446L,60447L,
  82887. 60448L,60449L,60450L,60451L,60452L,60453L,60454L,60455L,60456L,60457L,
  82888. 60458L,60459L,60460L,60461L,60462L,60463L,60464L,60465L,60466L,60467L,
  82889. 60468L,60469L,60470L,60471L,60472L,60473L,60474L,60475L,60476L,60477L,
  82890. 60478L,60479L,60480L,60481L,60482L,60483L,60484L,60485L,60486L,60487L,
  82891. 60488L,60489L,60490L,60491L,60492L,60493L,60494L,60495L,60496L,60497L,
  82892. 60498L,60499L,60500L,60501L,60502L,60503L,60504L,60505L,60506L,60507L,
  82893. 60508L,60509L,60510L,60511L,60512L,60513L,60514L,60515L,60516L,60517L,
  82894. 60518L,60519L,60520L,60521L,60522L,60523L,60524L,60525L,60526L,60527L,
  82895. 60528L,60529L,60530L,60531L,60532L,60533L,60534L,60535L,60536L,60537L,
  82896. 60538L,60539L,60540L,60541L,60542L,60543L,60544L,60545L,60546L,60547L,
  82897. 60548L,60549L,60550L,60551L,60552L,60553L,60554L,60555L,60556L,60557L,
  82898. 60558L,60559L,60560L,60561L,60562L,60563L,60564L,60565L,60566L,60567L,
  82899. 60568L,60569L,60570L,60571L,60572L,60573L,60574L,60575L,60576L,60577L,
  82900. 60578L,60579L,60580L,60581L,60582L,60583L,60584L,60585L,60586L,60587L,
  82901. 60588L,60589L,60590L,60591L,60592L,60593L,60594L,60595L,60596L,60597L,
  82902. 60598L,60599L,60600L,60601L,60602L,60603L,60604L,60605L,60606L,60607L,
  82903. 60608L,60609L,60610L,60611L,60612L,60613L,60614L,60615L,60616L,60617L,
  82904. 60618L,60619L,60620L,60621L,60622L,60623L,60624L,60625L,60626L,60627L,
  82905. 60628L,60629L,60630L,60631L,60632L,60633L,60634L,60635L,60636L,60637L,
  82906. 60638L,60639L,60640L,60641L,60642L,60643L,60644L,60645L,60646L,60647L,
  82907. 60648L,60649L,60650L,60651L,60652L,60653L,60654L,60655L,60656L,60657L,
  82908. 60658L,60659L,60660L,60661L,60662L,60663L,60664L,60665L,60666L,60667L,
  82909. 60668L,60669L,60670L,60671L,60672L,60673L,60674L,60675L,60676L,60677L,
  82910. 60678L,60679L,60680L,60681L,60682L,60683L,60684L,60685L,60686L,60687L,
  82911. 60688L,60689L,60690L,60691L,60692L,60693L,60694L,60695L,60696L,60697L,
  82912. 60698L,60699L,60700L,60701L,60702L,60703L,60704L,60705L,60706L,60707L,
  82913. 60708L,60709L,60710L,60711L,60712L,60713L,60714L,60715L,60716L,60717L,
  82914. 60718L,60719L,60720L,60721L,60722L,60723L,60724L,60725L,60726L,60727L,
  82915. 60728L,60729L,60730L,60731L,60732L,60733L,60734L,60735L,60736L,60737L,
  82916. 60738L,60739L,60740L,60741L,60742L,60743L,60744L,60745L,60746L,60747L,
  82917. 60748L,60749L,60750L,60751L,60752L,60753L,60754L,60755L,60756L,60757L,
  82918. 60758L,60759L,60760L,60761L,60762L,60763L,60764L,60765L,60766L,60767L,
  82919. 60768L,60769L,60770L,60771L,60772L,60773L,60774L,60775L,60776L,60777L,
  82920. 60778L,60779L,60780L,60781L,60782L,60783L,60784L,60785L,60786L,60787L,
  82921. 60788L,60789L,60790L,60791L,60792L,60793L,60794L,60795L,60796L,60797L,
  82922. 60798L,60799L,60800L,60801L,60802L,60803L,60804L,60805L,60806L,60807L,
  82923. 60808L,60809L,60810L,60811L,60812L,60813L,60814L,60815L,60816L,60817L,
  82924. 60818L,60819L,60820L,60821L,60822L,60823L,60824L,60825L,60826L,60827L,
  82925. 60828L,60829L,60830L,60831L,60832L,60833L,60834L,60835L,60836L,60837L,
  82926. 60838L,60839L,60840L,60841L,60842L,60843L,60844L,60845L,60846L,60847L,
  82927. 60848L,60849L,60850L,60851L,60852L,60853L,60854L,60855L,60856L,60857L,
  82928. 60858L,60859L,60860L,60861L,60862L,60863L,60864L,60865L,60866L,60867L,
  82929. 60868L,60869L,60870L,60871L,60872L,60873L,60874L,60875L,60876L,60877L,
  82930. 60878L,60879L,60880L,60881L,60882L,60883L,60884L,60885L,60886L,60887L,
  82931. 60888L,60889L,60890L,60891L,60892L,60893L,60894L,60895L,60896L,60897L,
  82932. 60898L,60899L,60900L,60901L,60902L,60903L,60904L,60905L,60906L,60907L,
  82933. 60908L,60909L,60910L,60911L,60912L,60913L,60914L,60915L,60916L,60917L,
  82934. 60918L,60919L,60920L,60921L,60922L,60923L,60924L,60925L,60926L,60927L,
  82935. 60928L,60929L,60930L,60931L,60932L,60933L,60934L,60935L,60936L,60937L,
  82936. 60938L,60939L,60940L,60941L,60942L,60943L,60944L,60945L,60946L,60947L,
  82937. 60948L,60949L,60950L,60951L,60952L,60953L,60954L,60955L,60956L,60957L,
  82938. 60958L,60959L,60960L,60961L,60962L,60963L,60964L,60965L,60966L,60967L,
  82939. 60968L,60969L,60970L,60971L,60972L,60973L,60974L,60975L,60976L,60977L,
  82940. 60978L,60979L,60980L,60981L,60982L,60983L,60984L,60985L,60986L,60987L,
  82941. 60988L,60989L,60990L,60991L,60992L,60993L,60994L,60995L,60996L,60997L,
  82942. 60998L,60999L,61000L,61001L,61002L,61003L,61004L,61005L,61006L,61007L,
  82943. 61008L,61009L,61010L,61011L,61012L,61013L,61014L,61015L,61016L,61017L,
  82944. 61018L,61019L,61020L,61021L,61022L,61023L,61024L,61025L,61026L,61027L,
  82945. 61028L,61029L,61030L,61031L,61032L,61033L,61034L,61035L,61036L,61037L,
  82946. 61038L,61039L,61040L,61041L,61042L,61043L,61044L,61045L,61046L,61047L,
  82947. 61048L,61049L,61050L,61051L,61052L,61053L,61054L,61055L,61056L,61057L,
  82948. 61058L,61059L,61060L,61061L,61062L,61063L,61064L,61065L,61066L,61067L,
  82949. 61068L,61069L,61070L,61071L,61072L,61073L,61074L,61075L,61076L,61077L,
  82950. 61078L,61079L,61080L,61081L,61082L,61083L,61084L,61085L,61086L,61087L,
  82951. 61088L,61089L,61090L,61091L,61092L,61093L,61094L,61095L,61096L,61097L,
  82952. 61098L,61099L,61100L,61101L,61102L,61103L,61104L,61105L,61106L,61107L,
  82953. 61108L,61109L,61110L,61111L,61112L,61113L,61114L,61115L,61116L,61117L,
  82954. 61118L,61119L,61120L,61121L,61122L,61123L,61124L,61125L,61126L,61127L,
  82955. 61128L,61129L,61130L,61131L,61132L,61133L,61134L,61135L,61136L,61137L,
  82956. 61138L,61139L,61140L,61141L,61142L,61143L,61144L,61145L,61146L,61147L,
  82957. 61148L,61149L,61150L,61151L,61152L,61153L,61154L,61155L,61156L,61157L,
  82958. 61158L,61159L,61160L,61161L,61162L,61163L,61164L,61165L,61166L,61167L,
  82959. 61168L,61169L,61170L,61171L,61172L,61173L,61174L,61175L,61176L,61177L,
  82960. 61178L,61179L,61180L,61181L,61182L,61183L,61184L,61185L,61186L,61187L,
  82961. 61188L,61189L,61190L,61191L,61192L,61193L,61194L,61195L,61196L,61197L,
  82962. 61198L,61199L,61200L,61201L,61202L,61203L,61204L,61205L,61206L,61207L,
  82963. 61208L,61209L,61210L,61211L,61212L,61213L,61214L,61215L,61216L,61217L,
  82964. 61218L,61219L,61220L,61221L,61222L,61223L,61224L,61225L,61226L,61227L,
  82965. 61228L,61229L,61230L,61231L,61232L,61233L,61234L,61235L,61236L,61237L,
  82966. 61238L,61239L,61240L,61241L,61242L,61243L,61244L,61245L,61246L,61247L,
  82967. 61248L,61249L,61250L,61251L,61252L,61253L,61254L,61255L,61256L,61257L,
  82968. 61258L,61259L,61260L,61261L,61262L,61263L,61264L,61265L,61266L,61267L,
  82969. 61268L,61269L,61270L,61271L,61272L,61273L,61274L,61275L,61276L,61277L,
  82970. 61278L,61279L,61280L,61281L,61282L,61283L,61284L,61285L,61286L,61287L,
  82971. 61288L,61289L,61290L,61291L,61292L,61293L,61294L,61295L,61296L,61297L,
  82972. 61298L,61299L,61300L,61301L,61302L,61303L,61304L,61305L,61306L,61307L,
  82973. 61308L,61309L,61310L,61311L,61312L,61313L,61314L,61315L,61316L,61317L,
  82974. 61318L,61319L,61320L,61321L,61322L,61323L,61324L,61325L,61326L,61327L,
  82975. 61328L,61329L,61330L,61331L,61332L,61333L,61334L,61335L,61336L,61337L,
  82976. 61338L,61339L,61340L,61341L,61342L,61343L,61344L,61345L,61346L,61347L,
  82977. 61348L,61349L,61350L,61351L,61352L,61353L,61354L,61355L,61356L,61357L,
  82978. 61358L,61359L,61360L,61361L,61362L,61363L,61364L,61365L,61366L,61367L,
  82979. 61368L,61369L,61370L,61371L,61372L,61373L,61374L,61375L,61376L,61377L,
  82980. 61378L,61379L,61380L,61381L,61382L,61383L,61384L,61385L,61386L,61387L,
  82981. 61388L,61389L,61390L,61391L,61392L,61393L,61394L,61395L,61396L,61397L,
  82982. 61398L,61399L,61400L,61401L,61402L,61403L,61404L,61405L,61406L,61407L,
  82983. 61408L,61409L,61410L,61411L,61412L,61413L,61414L,61415L,61416L,61417L,
  82984. 61418L,61419L,61420L,61421L,61422L,61423L,61424L,61425L,61426L,61427L,
  82985. 61428L,61429L,61430L,61431L,61432L,61433L,61434L,61435L,61436L,61437L,
  82986. 61438L,61439L,61440L,61441L,61442L,61443L,61444L,61445L,61446L,61447L,
  82987. 61448L,61449L,61450L,61451L,61452L,61453L,61454L,61455L,61456L,61457L,
  82988. 61458L,61459L,61460L,61461L,61462L,61463L,61464L,61465L,61466L,61467L,
  82989. 61468L,61469L,61470L,61471L,61472L,61473L,61474L,61475L,61476L,61477L,
  82990. 61478L,61479L,61480L,61481L,61482L,61483L,61484L,61485L,61486L,61487L,
  82991. 61488L,61489L,61490L,61491L,61492L,61493L,61494L,61495L,61496L,61497L,
  82992. 61498L,61499L,61500L,61501L,61502L,61503L,61504L,61505L,61506L,61507L,
  82993. 61508L,61509L,61510L,61511L,61512L,61513L,61514L,61515L,61516L,61517L,
  82994. 61518L,61519L,61520L,61521L,61522L,61523L,61524L,61525L,61526L,61527L,
  82995. 61528L,61529L,61530L,61531L,61532L,61533L,61534L,61535L,61536L,61537L,
  82996. 61538L,61539L,61540L,61541L,61542L,61543L,61544L,61545L,61546L,61547L,
  82997. 61548L,61549L,61550L,61551L,61552L,61553L,61554L,61555L,61556L,61557L,
  82998. 61558L,61559L,61560L,61561L,61562L,61563L,61564L,61565L,61566L,61567L,
  82999. 61568L,61569L,61570L,61571L,61572L,61573L,61574L,61575L,61576L,61577L,
  83000. 61578L,61579L,61580L,61581L,61582L,61583L,61584L,61585L,61586L,61587L,
  83001. 61588L,61589L,61590L,61591L,61592L,61593L,61594L,61595L,61596L,61597L,
  83002. 61598L,61599L,61600L,61601L,61602L,61603L,61604L,61605L,61606L,61607L,
  83003. 61608L,61609L,61610L,61611L,61612L,61613L,61614L,61615L,61616L,61617L,
  83004. 61618L,61619L,61620L,61621L,61622L,61623L,61624L,61625L,61626L,61627L,
  83005. 61628L,61629L,61630L,61631L,61632L,61633L,61634L,61635L,61636L,61637L,
  83006. 61638L,61639L,61640L,61641L,61642L,61643L,61644L,61645L,61646L,61647L,
  83007. 61648L,61649L,61650L,61651L,61652L,61653L,61654L,61655L,61656L,61657L,
  83008. 61658L,61659L,61660L,61661L,61662L,61663L,61664L,61665L,61666L,61667L,
  83009. 61668L,61669L,61670L,61671L,61672L,61673L,61674L,61675L,61676L,61677L,
  83010. 61678L,61679L,61680L,61681L,61682L,61683L,61684L,61685L,61686L,61687L,
  83011. 61688L,61689L,61690L,61691L,61692L,61693L,61694L,61695L,61696L,61697L,
  83012. 61698L,61699L,61700L,61701L,61702L,61703L,61704L,61705L,61706L,61707L,
  83013. 61708L,61709L,61710L,61711L,61712L,61713L,61714L,61715L,61716L,61717L,
  83014. 61718L,61719L,61720L,61721L,61722L,61723L,61724L,61725L,61726L,61727L,
  83015. 61728L,61729L,61730L,61731L,61732L,61733L,61734L,61735L,61736L,61737L,
  83016. 61738L,61739L,61740L,61741L,61742L,61743L,61744L,61745L,61746L,61747L,
  83017. 61748L,61749L,61750L,61751L,61752L,61753L,61754L,61755L,61756L,61757L,
  83018. 61758L,61759L,61760L,61761L,61762L,61763L,61764L,61765L,61766L,61767L,
  83019. 61768L,61769L,61770L,61771L,61772L,61773L,61774L,61775L,61776L,61777L,
  83020. 61778L,61779L,61780L,61781L,61782L,61783L,61784L,61785L,61786L,61787L,
  83021. 61788L,61789L,61790L,61791L,61792L,61793L,61794L,61795L,61796L,61797L,
  83022. 61798L,61799L,61800L,61801L,61802L,61803L,61804L,61805L,61806L,61807L,
  83023. 61808L,61809L,61810L,61811L,61812L,61813L,61814L,61815L,61816L,61817L,
  83024. 61818L,61819L,61820L,61821L,61822L,61823L,61824L,61825L,61826L,61827L,
  83025. 61828L,61829L,61830L,61831L,61832L,61833L,61834L,61835L,61836L,61837L,
  83026. 61838L,61839L,61840L,61841L,61842L,61843L,61844L,61845L,61846L,61847L,
  83027. 61848L,61849L,61850L,61851L,61852L,61853L,61854L,61855L,61856L,61857L,
  83028. 61858L,61859L,61860L,61861L,61862L,61863L,61864L,61865L,61866L,61867L,
  83029. 61868L,61869L,61870L,61871L,61872L,61873L,61874L,61875L,61876L,61877L,
  83030. 61878L,61879L,61880L,61881L,61882L,61883L,61884L,61885L,61886L,61887L,
  83031. 61888L,61889L,61890L,61891L,61892L,61893L,61894L,61895L,61896L,61897L,
  83032. 61898L,61899L,61900L,61901L,61902L,61903L,61904L,61905L,61906L,61907L,
  83033. 61908L,61909L,61910L,61911L,61912L,61913L,61914L,61915L,61916L,61917L,
  83034. 61918L,61919L,61920L,61921L,61922L,61923L,61924L,61925L,61926L,61927L,
  83035. 61928L,61929L,61930L,61931L,61932L,61933L,61934L,61935L,61936L,61937L,
  83036. 61938L,61939L,61940L,61941L,61942L,61943L,61944L,61945L,61946L,61947L,
  83037. 61948L,61949L,61950L,61951L,61952L,61953L,61954L,61955L,61956L,61957L,
  83038. 61958L,61959L,61960L,61961L,61962L,61963L,61964L,61965L,61966L,61967L,
  83039. 61968L,61969L,61970L,61971L,61972L,61973L,61974L,61975L,61976L,61977L,
  83040. 61978L,61979L,61980L,61981L,61982L,61983L,61984L,61985L,61986L,61987L,
  83041. 61988L,61989L,61990L,61991L,61992L,61993L,61994L,61995L,61996L,61997L,
  83042. 61998L,61999L,62000L,62001L,62002L,62003L,62004L,62005L,62006L,62007L,
  83043. 62008L,62009L,62010L,62011L,62012L,62013L,62014L,62015L,62016L,62017L,
  83044. 62018L,62019L,62020L,62021L,62022L,62023L,62024L,62025L,62026L,62027L,
  83045. 62028L,62029L,62030L,62031L,62032L,62033L,62034L,62035L,62036L,62037L,
  83046. 62038L,62039L,62040L,62041L,62042L,62043L,62044L,62045L,62046L,62047L,
  83047. 62048L,62049L,62050L,62051L,62052L,62053L,62054L,62055L,62056L,62057L,
  83048. 62058L,62059L,62060L,62061L,62062L,62063L,62064L,62065L,62066L,62067L,
  83049. 62068L,62069L,62070L,62071L,62072L,62073L,62074L,62075L,62076L,62077L,
  83050. 62078L,62079L,62080L,62081L,62082L,62083L,62084L,62085L,62086L,62087L,
  83051. 62088L,62089L,62090L,62091L,62092L,62093L,62094L,62095L,62096L,62097L,
  83052. 62098L,62099L,62100L,62101L,62102L,62103L,62104L,62105L,62106L,62107L,
  83053. 62108L,62109L,62110L,62111L,62112L,62113L,62114L,62115L,62116L,62117L,
  83054. 62118L,62119L,62120L,62121L,62122L,62123L,62124L,62125L,62126L,62127L,
  83055. 62128L,62129L,62130L,62131L,62132L,62133L,62134L,62135L,62136L,62137L,
  83056. 62138L,62139L,62140L,62141L,62142L,62143L,62144L,62145L,62146L,62147L,
  83057. 62148L,62149L,62150L,62151L,62152L,62153L,62154L,62155L,62156L,62157L,
  83058. 62158L,62159L,62160L,62161L,62162L,62163L,62164L,62165L,62166L,62167L,
  83059. 62168L,62169L,62170L,62171L,62172L,62173L,62174L,62175L,62176L,62177L,
  83060. 62178L,62179L,62180L,62181L,62182L,62183L,62184L,62185L,62186L,62187L,
  83061. 62188L,62189L,62190L,62191L,62192L,62193L,62194L,62195L,62196L,62197L,
  83062. 62198L,62199L,62200L,62201L,62202L,62203L,62204L,62205L,62206L,62207L,
  83063. 62208L,62209L,62210L,62211L,62212L,62213L,62214L,62215L,62216L,62217L,
  83064. 62218L,62219L,62220L,62221L,62222L,62223L,62224L,62225L,62226L,62227L,
  83065. 62228L,62229L,62230L,62231L,62232L,62233L,62234L,62235L,62236L,62237L,
  83066. 62238L,62239L,62240L,62241L,62242L,62243L,62244L,62245L,62246L,62247L,
  83067. 62248L,62249L,62250L,62251L,62252L,62253L,62254L,62255L,62256L,62257L,
  83068. 62258L,62259L,62260L,62261L,62262L,62263L,62264L,62265L,62266L,62267L,
  83069. 62268L,62269L,62270L,62271L,62272L,62273L,62274L,62275L,62276L,62277L,
  83070. 62278L,62279L,62280L,62281L,62282L,62283L,62284L,62285L,62286L,62287L,
  83071. 62288L,62289L,62290L,62291L,62292L,62293L,62294L,62295L,62296L,62297L,
  83072. 62298L,62299L,62300L,62301L,62302L,62303L,62304L,62305L,62306L,62307L,
  83073. 62308L,62309L,62310L,62311L,62312L,62313L,62314L,62315L,62316L,62317L,
  83074. 62318L,62319L,62320L,62321L,62322L,62323L,62324L,62325L,62326L,62327L,
  83075. 62328L,62329L,62330L,62331L,62332L,62333L,62334L,62335L,62336L,62337L,
  83076. 62338L,62339L,62340L,62341L,62342L,62343L,62344L,62345L,62346L,62347L,
  83077. 62348L,62349L,62350L,62351L,62352L,62353L,62354L,62355L,62356L,62357L,
  83078. 62358L,62359L,62360L,62361L,62362L,62363L,62364L,62365L,62366L,62367L,
  83079. 62368L,62369L,62370L,62371L,62372L,62373L,62374L,62375L,62376L,62377L,
  83080. 62378L,62379L,62380L,62381L,62382L,62383L,62384L,62385L,62386L,62387L,
  83081. 62388L,62389L,62390L,62391L,62392L,62393L,62394L,62395L,62396L,62397L,
  83082. 62398L,62399L,62400L,62401L,62402L,62403L,62404L,62405L,62406L,62407L,
  83083. 62408L,62409L,62410L,62411L,62412L,62413L,62414L,62415L,62416L,62417L,
  83084. 62418L,62419L,62420L,62421L,62422L,62423L,62424L,62425L,62426L,62427L,
  83085. 62428L,62429L,62430L,62431L,62432L,62433L,62434L,62435L,62436L,62437L,
  83086. 62438L,62439L,62440L,62441L,62442L,62443L,62444L,62445L,62446L,62447L,
  83087. 62448L,62449L,62450L,62451L,62452L,62453L,62454L,62455L,62456L,62457L,
  83088. 62458L,62459L,62460L,62461L,62462L,62463L,62464L,62465L,62466L,62467L,
  83089. 62468L,62469L,62470L,62471L,62472L,62473L,62474L,62475L,62476L,62477L,
  83090. 62478L,62479L,62480L,62481L,62482L,62483L,62484L,62485L,62486L,62487L,
  83091. 62488L,62489L,62490L,62491L,62492L,62493L,62494L,62495L,62496L,62497L,
  83092. 62498L,62499L,62500L,62501L,62502L,62503L,62504L,62505L,62506L,62507L,
  83093. 62508L,62509L,62510L,62511L,62512L,62513L,62514L,62515L,62516L,62517L,
  83094. 62518L,62519L,62520L,62521L,62522L,62523L,62524L,62525L,62526L,62527L,
  83095. 62528L,62529L,62530L,62531L,62532L,62533L,62534L,62535L,62536L,62537L,
  83096. 62538L,62539L,62540L,62541L,62542L,62543L,62544L,62545L,62546L,62547L,
  83097. 62548L,62549L,62550L,62551L,62552L,62553L,62554L,62555L,62556L,62557L,
  83098. 62558L,62559L,62560L,62561L,62562L,62563L,62564L,62565L,62566L,62567L,
  83099. 62568L,62569L,62570L,62571L,62572L,62573L,62574L,62575L,62576L,62577L,
  83100. 62578L,62579L,62580L,62581L,62582L,62583L,62584L,62585L,62586L,62587L,
  83101. 62588L,62589L,62590L,62591L,62592L,62593L,62594L,62595L,62596L,62597L,
  83102. 62598L,62599L,62600L,62601L,62602L,62603L,62604L,62605L,62606L,62607L,
  83103. 62608L,62609L,62610L,62611L,62612L,62613L,62614L,62615L,62616L,62617L,
  83104. 62618L,62619L,62620L,62621L,62622L,62623L,62624L,62625L,62626L,62627L,
  83105. 62628L,62629L,62630L,62631L,62632L,62633L,62634L,62635L,62636L,62637L,
  83106. 62638L,62639L,62640L,62641L,62642L,62643L,62644L,62645L,62646L,62647L,
  83107. 62648L,62649L,62650L,62651L,62652L,62653L,62654L,62655L,62656L,62657L,
  83108. 62658L,62659L,62660L,62661L,62662L,62663L,62664L,62665L,62666L,62667L,
  83109. 62668L,62669L,62670L,62671L,62672L,62673L,62674L,62675L,62676L,62677L,
  83110. 62678L,62679L,62680L,62681L,62682L,62683L,62684L,62685L,62686L,62687L,
  83111. 62688L,62689L,62690L,62691L,62692L,62693L,62694L,62695L,62696L,62697L,
  83112. 62698L,62699L,62700L,62701L,62702L,62703L,62704L,62705L,62706L,62707L,
  83113. 62708L,62709L,62710L,62711L,62712L,62713L,62714L,62715L,62716L,62717L,
  83114. 62718L,62719L,62720L,62721L,62722L,62723L,62724L,62725L,62726L,62727L,
  83115. 62728L,62729L,62730L,62731L,62732L,62733L,62734L,62735L,62736L,62737L,
  83116. 62738L,62739L,62740L,62741L,62742L,62743L,62744L,62745L,62746L,62747L,
  83117. 62748L,62749L,62750L,62751L,62752L,62753L,62754L,62755L,62756L,62757L,
  83118. 62758L,62759L,62760L,62761L,62762L,62763L,62764L,62765L,62766L,62767L,
  83119. 62768L,62769L,62770L,62771L,62772L,62773L,62774L,62775L,62776L,62777L,
  83120. 62778L,62779L,62780L,62781L,62782L,62783L,62784L,62785L,62786L,62787L,
  83121. 62788L,62789L,62790L,62791L,62792L,62793L,62794L,62795L,62796L,62797L,
  83122. 62798L,62799L,62800L,62801L,62802L,62803L,62804L,62805L,62806L,62807L,
  83123. 62808L,62809L,62810L,62811L,62812L,62813L,62814L,62815L,62816L,62817L,
  83124. 62818L,62819L,62820L,62821L,62822L,62823L,62824L,62825L,62826L,62827L,
  83125. 62828L,62829L,62830L,62831L,62832L,62833L,62834L,62835L,62836L,62837L,
  83126. 62838L,62839L,62840L,62841L,62842L,62843L,62844L,62845L,62846L,62847L,
  83127. 62848L,62849L,62850L,62851L,62852L,62853L,62854L,62855L,62856L,62857L,
  83128. 62858L,62859L,62860L,62861L,62862L,62863L,62864L,62865L,62866L,62867L,
  83129. 62868L,62869L,62870L,62871L,62872L,62873L,62874L,62875L,62876L,62877L,
  83130. 62878L,62879L,62880L,62881L,62882L,62883L,62884L,62885L,62886L,62887L,
  83131. 62888L,62889L,62890L,62891L,62892L,62893L,62894L,62895L,62896L,62897L,
  83132. 62898L,62899L,62900L,62901L,62902L,62903L,62904L,62905L,62906L,62907L,
  83133. 62908L,62909L,62910L,62911L,62912L,62913L,62914L,62915L,62916L,62917L,
  83134. 62918L,62919L,62920L,62921L,62922L,62923L,62924L,62925L,62926L,62927L,
  83135. 62928L,62929L,62930L,62931L,62932L,62933L,62934L,62935L,62936L,62937L,
  83136. 62938L,62939L,62940L,62941L,62942L,62943L,62944L,62945L,62946L,62947L,
  83137. 62948L,62949L,62950L,62951L,62952L,62953L,62954L,62955L,62956L,62957L,
  83138. 62958L,62959L,62960L,62961L,62962L,62963L,62964L,62965L,62966L,62967L,
  83139. 62968L,62969L,62970L,62971L,62972L,62973L,62974L,62975L,62976L,62977L,
  83140. 62978L,62979L,62980L,62981L,62982L,62983L,62984L,62985L,62986L,62987L,
  83141. 62988L,62989L,62990L,62991L,62992L,62993L,62994L,62995L,62996L,62997L,
  83142. 62998L,62999L,63000L,63001L,63002L,63003L,63004L,63005L,63006L,63007L,
  83143. 63008L,63009L,63010L,63011L,63012L,63013L,63014L,63015L,63016L,63017L,
  83144. 63018L,63019L,63020L,63021L,63022L,63023L,63024L,63025L,63026L,63027L,
  83145. 63028L,63029L,63030L,63031L,63032L,63033L,63034L,63035L,63036L,63037L,
  83146. 63038L,63039L,63040L,63041L,63042L,63043L,63044L,63045L,63046L,63047L,
  83147. 63048L,63049L,63050L,63051L,63052L,63053L,63054L,63055L,63056L,63057L,
  83148. 63058L,63059L,63060L,63061L,63062L,63063L,63064L,63065L,63066L,63067L,
  83149. 63068L,63069L,63070L,63071L,63072L,63073L,63074L,63075L,63076L,63077L,
  83150. 63078L,63079L,63080L,63081L,63082L,63083L,63084L,63085L,63086L,63087L,
  83151. 63088L,63089L,63090L,63091L,63092L,63093L,63094L,63095L,63096L,63097L,
  83152. 63098L,63099L,63100L,63101L,63102L,63103L,63104L,63105L,63106L,63107L,
  83153. 63108L,63109L,63110L,63111L,63112L,63113L,63114L,63115L,63116L,63117L,
  83154. 63118L,63119L,63120L,63121L,63122L,63123L,63124L,63125L,63126L,63127L,
  83155. 63128L,63129L,63130L,63131L,63132L,63133L,63134L,63135L,63136L,63137L,
  83156. 63138L,63139L,63140L,63141L,63142L,63143L,63144L,63145L,63146L,63147L,
  83157. 63148L,63149L,63150L,63151L,63152L,63153L,63154L,63155L,63156L,63157L,
  83158. 63158L,63159L,63160L,63161L,63162L,63163L,63164L,63165L,63166L,63167L,
  83159. 63168L,63169L,63170L,63171L,63172L,63173L,63174L,63175L,63176L,63177L,
  83160. 63178L,63179L,63180L,63181L,63182L,63183L,63184L,63185L,63186L,63187L,
  83161. 63188L,63189L,63190L,63191L,63192L,63193L,63194L,63195L,63196L,63197L,
  83162. 63198L,63199L,63200L,63201L,63202L,63203L,63204L,63205L,63206L,63207L,
  83163. 63208L,63209L,63210L,63211L,63212L,63213L,63214L,63215L,63216L,63217L,
  83164. 63218L,63219L,63220L,63221L,63222L,63223L,63224L,63225L,63226L,63227L,
  83165. 63228L,63229L,63230L,63231L,63232L,63233L,63234L,63235L,63236L,63237L,
  83166. 63238L,63239L,63240L,63241L,63242L,63243L,63244L,63245L,63246L,63247L,
  83167. 63248L,63249L,63250L,63251L,63252L,63253L,63254L,63255L,63256L,63257L,
  83168. 63258L,63259L,63260L,63261L,63262L,63263L,63264L,63265L,63266L,63267L,
  83169. 63268L,63269L,63270L,63271L,63272L,63273L,63274L,63275L,63276L,63277L,
  83170. 63278L,63279L,63280L,63281L,63282L,63283L,63284L,63285L,63286L,63287L,
  83171. 63288L,63289L,63290L,63291L,63292L,63293L,63294L,63295L,63296L,63297L,
  83172. 63298L,63299L,63300L,63301L,63302L,63303L,63304L,63305L,63306L,63307L,
  83173. 63308L,63309L,63310L,63311L,63312L,63313L,63314L,63315L,63316L,63317L,
  83174. 63318L,63319L,63320L,63321L,63322L,63323L,63324L,63325L,63326L,63327L,
  83175. 63328L,63329L,63330L,63331L,63332L,63333L,63334L,63335L,63336L,63337L,
  83176. 63338L,63339L,63340L,63341L,63342L,63343L,63344L,63345L,63346L,63347L,
  83177. 63348L,63349L,63350L,63351L,63352L,63353L,63354L,63355L,63356L,63357L,
  83178. 63358L,63359L,63360L,63361L,63362L,63363L,63364L,63365L,63366L,63367L,
  83179. 63368L,63369L,63370L,63371L,63372L,63373L,63374L,63375L,63376L,63377L,
  83180. 63378L,63379L,63380L,63381L,63382L,63383L,63384L,63385L,63386L,63387L,
  83181. 63388L,63389L,63390L,63391L,63392L,63393L,63394L,63395L,63396L,63397L,
  83182. 63398L,63399L,63400L,63401L,63402L,63403L,63404L,63405L,63406L,63407L,
  83183. 63408L,63409L,63410L,63411L,63412L,63413L,63414L,63415L,63416L,63417L,
  83184. 63418L,63419L,63420L,63421L,63422L,63423L,63424L,63425L,63426L,63427L,
  83185. 63428L,63429L,63430L,63431L,63432L,63433L,63434L,63435L,63436L,63437L,
  83186. 63438L,63439L,63440L,63441L,63442L,63443L,63444L,63445L,63446L,63447L,
  83187. 63448L,63449L,63450L,63451L,63452L,63453L,63454L,63455L,63456L,63457L,
  83188. 63458L,63459L,63460L,63461L,63462L,63463L,63464L,63465L,63466L,63467L,
  83189. 63468L,63469L,63470L,63471L,63472L,63473L,63474L,63475L,63476L,63477L,
  83190. 63478L,63479L,63480L,63481L,63482L,63483L,63484L,63485L,63486L,63487L,
  83191. 63488L,63489L,63490L,63491L,63492L,63493L,63494L,63495L,63496L,63497L,
  83192. 63498L,63499L,63500L,63501L,63502L,63503L,63504L,63505L,63506L,63507L,
  83193. 63508L,63509L,63510L,63511L,63512L,63513L,63514L,63515L,63516L,63517L,
  83194. 63518L,63519L,63520L,63521L,63522L,63523L,63524L,63525L,63526L,63527L,
  83195. 63528L,63529L,63530L,63531L,63532L,63533L,63534L,63535L,63536L,63537L,
  83196. 63538L,63539L,63540L,63541L,63542L,63543L,63544L,63545L,63546L,63547L,
  83197. 63548L,63549L,63550L,63551L,63552L,63553L,63554L,63555L,63556L,63557L,
  83198. 63558L,63559L,63560L,63561L,63562L,63563L,63564L,63565L,63566L,63567L,
  83199. 63568L,63569L,63570L,63571L,63572L,63573L,63574L,63575L,63576L,63577L,
  83200. 63578L,63579L,63580L,63581L,63582L,63583L,63584L,63585L,63586L,63587L,
  83201. 63588L,63589L,63590L,63591L,63592L,63593L,63594L,63595L,63596L,63597L,
  83202. 63598L,63599L,63600L,63601L,63602L,63603L,63604L,63605L,63606L,63607L,
  83203. 63608L,63609L,63610L,63611L,63612L,63613L,63614L,63615L,63616L,63617L,
  83204. 63618L,63619L,63620L,63621L,63622L,63623L,63624L,63625L,63626L,63627L,
  83205. 63628L,63629L,63630L,63631L,63632L,63633L,63634L,63635L,63636L,63637L,
  83206. 63638L,63639L,63640L,63641L,63642L,63643L,63644L,63645L,63646L,63647L,
  83207. 63648L,63649L,63650L,63651L,63652L,63653L,63654L,63655L,63656L,63657L,
  83208. 63658L,63659L,63660L,63661L,63662L,63663L,63664L,63665L,63666L,63667L,
  83209. 63668L,63669L,63670L,63671L,63672L,63673L,63674L,63675L,63676L,63677L,
  83210. 63678L,63679L,63680L,63681L,63682L,63683L,63684L,63685L,63686L,63687L,
  83211. 63688L,63689L,63690L,63691L,63692L,63693L,63694L,63695L,63696L,63697L,
  83212. 63698L,63699L,63700L,63701L,63702L,63703L,63704L,63705L,63706L,63707L,
  83213. 63708L,63709L,63710L,63711L,63712L,63713L,63714L,63715L,63716L,63717L,
  83214. 63718L,63719L,63720L,63721L,63722L,63723L,63724L,63725L,63726L,63727L,
  83215. 63728L,63729L,63730L,63731L,63732L,63733L,63734L,63735L,63736L,63737L,
  83216. 63738L,63739L,63740L,63741L,63742L,63743L,63744L,63745L,63746L,63747L,
  83217. 63748L,63749L,63750L,63751L,63752L,63753L,63754L,63755L,63756L,63757L,
  83218. 63758L,63759L,63760L,63761L,63762L,63763L,63764L,63765L,63766L,63767L,
  83219. 63768L,63769L,63770L,63771L,63772L,63773L,63774L,63775L,63776L,63777L,
  83220. 63778L,63779L,63780L,63781L,63782L,63783L,63784L,63785L,63786L,63787L,
  83221. 63788L,63789L,63790L,63791L,63792L,63793L,63794L,63795L,63796L,63797L,
  83222. 63798L,63799L,63800L,63801L,63802L,63803L,63804L,63805L,63806L,63807L,
  83223. 63808L,63809L,63810L,63811L,63812L,63813L,63814L,63815L,63816L,63817L,
  83224. 63818L,63819L,63820L,63821L,63822L,63823L,63824L,63825L,63826L,63827L,
  83225. 63828L,63829L,63830L,63831L,63832L,63833L,63834L,63835L,63836L,63837L,
  83226. 63838L,63839L,63840L,63841L,63842L,63843L,63844L,63845L,63846L,63847L,
  83227. 63848L,63849L,63850L,63851L,63852L,63853L,63854L,63855L,63856L,63857L,
  83228. 63858L,63859L,63860L,63861L,63862L,63863L,63864L,63865L,63866L,63867L,
  83229. 63868L,63869L,63870L,63871L,63872L,63873L,63874L,63875L,63876L,63877L,
  83230. 63878L,63879L,63880L,63881L,63882L,63883L,63884L,63885L,63886L,63887L,
  83231. 63888L,63889L,63890L,63891L,63892L,63893L,63894L,63895L,63896L,63897L,
  83232. 63898L,63899L,63900L,63901L,63902L,63903L,63904L,63905L,63906L,63907L,
  83233. 63908L,63909L,63910L,63911L,63912L,63913L,63914L,63915L,63916L,63917L,
  83234. 63918L,63919L,63920L,63921L,63922L,63923L,63924L,63925L,63926L,63927L,
  83235. 63928L,63929L,63930L,63931L,63932L,63933L,63934L,63935L,63936L,63937L,
  83236. 63938L,63939L,63940L,63941L,63942L,63943L,63944L,63945L,63946L,63947L,
  83237. 63948L,63949L,63950L,63951L,63952L,63953L,63954L,63955L,63956L,63957L,
  83238. 63958L,63959L,63960L,63961L,63962L,63963L,63964L,63965L,63966L,63967L,
  83239. 63968L,63969L,63970L,63971L,63972L,63973L,63974L,63975L,63976L,63977L,
  83240. 63978L,63979L,63980L,63981L,63982L,63983L,63984L,63985L,63986L,63987L,
  83241. 63988L,63989L,63990L,63991L,63992L,63993L,63994L,63995L,63996L,63997L,
  83242. 63998L,63999L,64000L,64001L,64002L,64003L,64004L,64005L,64006L,64007L,
  83243. 64008L,64009L,64010L,64011L,64012L,64013L,64014L,64015L,64016L,64017L,
  83244. 64018L,64019L,64020L,64021L,64022L,64023L,64024L,64025L,64026L,64027L,
  83245. 64028L,64029L,64030L,64031L,64032L,64033L,64034L,64035L,64036L,64037L,
  83246. 64038L,64039L,64040L,64041L,64042L,64043L,64044L,64045L,64046L,64047L,
  83247. 64048L,64049L,64050L,64051L,64052L,64053L,64054L,64055L,64056L,64057L,
  83248. 64058L,64059L,64060L,64061L,64062L,64063L,64064L,64065L,64066L,64067L,
  83249. 64068L,64069L,64070L,64071L,64072L,64073L,64074L,64075L,64076L,64077L,
  83250. 64078L,64079L,64080L,64081L,64082L,64083L,64084L,64085L,64086L,64087L,
  83251. 64088L,64089L,64090L,64091L,64092L,64093L,64094L,64095L,64096L,64097L,
  83252. 64098L,64099L,64100L,64101L,64102L,64103L,64104L,64105L,64106L,64107L,
  83253. 64108L,64109L,64110L,64111L,64112L,64113L,64114L,64115L,64116L,64117L,
  83254. 64118L,64119L,64120L,64121L,64122L,64123L,64124L,64125L,64126L,64127L,
  83255. 64128L,64129L,64130L,64131L,64132L,64133L,64134L,64135L,64136L,64137L,
  83256. 64138L,64139L,64140L,64141L,64142L,64143L,64144L,64145L,64146L,64147L,
  83257. 64148L,64149L,64150L,64151L,64152L,64153L,64154L,64155L,64156L,64157L,
  83258. 64158L,64159L,64160L,64161L,64162L,64163L,64164L,64165L,64166L,64167L,
  83259. 64168L,64169L,64170L,64171L,64172L,64173L,64174L,64175L,64176L,64177L,
  83260. 64178L,64179L,64180L,64181L,64182L,64183L,64184L,64185L,64186L,64187L,
  83261. 64188L,64189L,64190L,64191L,64192L,64193L,64194L,64195L,64196L,64197L,
  83262. 64198L,64199L,64200L,64201L,64202L,64203L,64204L,64205L,64206L,64207L,
  83263. 64208L,64209L,64210L,64211L,64212L,64213L,64214L,64215L,64216L,64217L,
  83264. 64218L,64219L,64220L,64221L,64222L,64223L,64224L,64225L,64226L,64227L,
  83265. 64228L,64229L,64230L,64231L,64232L,64233L,64234L,64235L,64236L,64237L,
  83266. 64238L,64239L,64240L,64241L,64242L,64243L,64244L,64245L,64246L,64247L,
  83267. 64248L,64249L,64250L,64251L,64252L,64253L,64254L,64255L,64256L,64257L,
  83268. 64258L,64259L,64260L,64261L,64262L,64263L,64264L,64265L,64266L,64267L,
  83269. 64268L,64269L,64270L,64271L,64272L,64273L,64274L,64275L,64276L,64277L,
  83270. 64278L,64279L,64280L,64281L,64282L,64283L,64284L,64285L,64286L,64287L,
  83271. 64288L,64289L,64290L,64291L,64292L,64293L,64294L,64295L,64296L,64297L,
  83272. 64298L,64299L,64300L,64301L,64302L,64303L,64304L,64305L,64306L,64307L,
  83273. 64308L,64309L,64310L,64311L,64312L,64313L,64314L,64315L,64316L,64317L,
  83274. 64318L,64319L,64320L,64321L,64322L,64323L,64324L,64325L,64326L,64327L,
  83275. 64328L,64329L,64330L,64331L,64332L,64333L,64334L,64335L,64336L,64337L,
  83276. 64338L,64339L,64340L,64341L,64342L,64343L,64344L,64345L,64346L,64347L,
  83277. 64348L,64349L,64350L,64351L,64352L,64353L,64354L,64355L,64356L,64357L,
  83278. 64358L,64359L,64360L,64361L,64362L,64363L,64364L,64365L,64366L,64367L,
  83279. 64368L,64369L,64370L,64371L,64372L,64373L,64374L,64375L,64376L,64377L,
  83280. 64378L,64379L,64380L,64381L,64382L,64383L,64384L,64385L,64386L,64387L,
  83281. 64388L,64389L,64390L,64391L,64392L,64393L,64394L,64395L,64396L,64397L,
  83282. 64398L,64399L,64400L,64401L,64402L,64403L,64404L,64405L,64406L,64407L,
  83283. 64408L,64409L,64410L,64411L,64412L,64413L,64414L,64415L,64416L,64417L,
  83284. 64418L,64419L,64420L,64421L,64422L,64423L,64424L,64425L,64426L,64427L,
  83285. 64428L,64429L,64430L,64431L,64432L,64433L,64434L,64435L,64436L,64437L,
  83286. 64438L,64439L,64440L,64441L,64442L,64443L,64444L,64445L,64446L,64447L,
  83287. 64448L,64449L,64450L,64451L,64452L,64453L,64454L,64455L,64456L,64457L,
  83288. 64458L,64459L,64460L,64461L,64462L,64463L,64464L,64465L,64466L,64467L,
  83289. 64468L,64469L,64470L,64471L,64472L,64473L,64474L,64475L,64476L,64477L,
  83290. 64478L,64479L,64480L,64481L,64482L,64483L,64484L,64485L,64486L,64487L,
  83291. 64488L,64489L,64490L,64491L,64492L,64493L,64494L,64495L,64496L,64497L,
  83292. 64498L,64499L,64500L,64501L,64502L,64503L,64504L,64505L,64506L,64507L,
  83293. 64508L,64509L,64510L,64511L,64512L,64513L,64514L,64515L,64516L,64517L,
  83294. 64518L,64519L,64520L,64521L,64522L,64523L,64524L,64525L,64526L,64527L,
  83295. 64528L,64529L,64530L,64531L,64532L,64533L,64534L,64535L,64536L,64537L,
  83296. 64538L,64539L,64540L,64541L,64542L,64543L,64544L,64545L,64546L,64547L,
  83297. 64548L,64549L,64550L,64551L,64552L,64553L,64554L,64555L,64556L,64557L,
  83298. 64558L,64559L,64560L,64561L,64562L,64563L,64564L,64565L,64566L,64567L,
  83299. 64568L,64569L,64570L,64571L,64572L,64573L,64574L,64575L,64576L,64577L,
  83300. 64578L,64579L,64580L,64581L,64582L,64583L,64584L,64585L,64586L,64587L,
  83301. 64588L,64589L,64590L,64591L,64592L,64593L,64594L,64595L,64596L,64597L,
  83302. 64598L,64599L,64600L,64601L,64602L,64603L,64604L,64605L,64606L,64607L,
  83303. 64608L,64609L,64610L,64611L,64612L,64613L,64614L,64615L,64616L,64617L,
  83304. 64618L,64619L,64620L,64621L,64622L,64623L,64624L,64625L,64626L,64627L,
  83305. 64628L,64629L,64630L,64631L,64632L,64633L,64634L,64635L,64636L,64637L,
  83306. 64638L,64639L,64640L,64641L,64642L,64643L,64644L,64645L,64646L,64647L,
  83307. 64648L,64649L,64650L,64651L,64652L,64653L,64654L,64655L,64656L,64657L,
  83308. 64658L,64659L,64660L,64661L,64662L,64663L,64664L,64665L,64666L,64667L,
  83309. 64668L,64669L,64670L,64671L,64672L,64673L,64674L,64675L,64676L,64677L,
  83310. 64678L,64679L,64680L,64681L,64682L,64683L,64684L,64685L,64686L,64687L,
  83311. 64688L,64689L,64690L,64691L,64692L,64693L,64694L,64695L,64696L,64697L,
  83312. 64698L,64699L,64700L,64701L,64702L,64703L,64704L,64705L,64706L,64707L,
  83313. 64708L,64709L,64710L,64711L,64712L,64713L,64714L,64715L,64716L,64717L,
  83314. 64718L,64719L,64720L,64721L,64722L,64723L,64724L,64725L,64726L,64727L,
  83315. 64728L,64729L,64730L,64731L,64732L,64733L,64734L,64735L,64736L,64737L,
  83316. 64738L,64739L,64740L,64741L,64742L,64743L,64744L,64745L,64746L,64747L,
  83317. 64748L,64749L,64750L,64751L,64752L,64753L,64754L,64755L,64756L,64757L,
  83318. 64758L,64759L,64760L,64761L,64762L,64763L,64764L,64765L,64766L,64767L,
  83319. 64768L,64769L,64770L,64771L,64772L,64773L,64774L,64775L,64776L,64777L,
  83320. 64778L,64779L,64780L,64781L,64782L,64783L,64784L,64785L,64786L,64787L,
  83321. 64788L,64789L,64790L,64791L,64792L,64793L,64794L,64795L,64796L,64797L,
  83322. 64798L,64799L,64800L,64801L,64802L,64803L,64804L,64805L,64806L,64807L,
  83323. 64808L,64809L,64810L,64811L,64812L,64813L,64814L,64815L,64816L,64817L,
  83324. 64818L,64819L,64820L,64821L,64822L,64823L,64824L,64825L,64826L,64827L,
  83325. 64828L,64829L,64830L,64831L,64832L,64833L,64834L,64835L,64836L,64837L,
  83326. 64838L,64839L,64840L,64841L,64842L,64843L,64844L,64845L,64846L,64847L,
  83327. 64848L,64849L,64850L,64851L,64852L,64853L,64854L,64855L,64856L,64857L,
  83328. 64858L,64859L,64860L,64861L,64862L,64863L,64864L,64865L,64866L,64867L,
  83329. 64868L,64869L,64870L,64871L,64872L,64873L,64874L,64875L,64876L,64877L,
  83330. 64878L,64879L,64880L,64881L,64882L,64883L,64884L,64885L,64886L,64887L,
  83331. 64888L,64889L,64890L,64891L,64892L,64893L,64894L,64895L,64896L,64897L,
  83332. 64898L,64899L,64900L,64901L,64902L,64903L,64904L,64905L,64906L,64907L,
  83333. 64908L,64909L,64910L,64911L,64912L,64913L,64914L,64915L,64916L,64917L,
  83334. 64918L,64919L,64920L,64921L,64922L,64923L,64924L,64925L,64926L,64927L,
  83335. 64928L,64929L,64930L,64931L,64932L,64933L,64934L,64935L,64936L,64937L,
  83336. 64938L,64939L,64940L,64941L,64942L,64943L,64944L,64945L,64946L,64947L,
  83337. 64948L,64949L,64950L,64951L,64952L,64953L,64954L,64955L,64956L,64957L,
  83338. 64958L,64959L,64960L,64961L,64962L,64963L,64964L,64965L,64966L,64967L,
  83339. 64968L,64969L,64970L,64971L,64972L,64973L,64974L,64975L,64976L,64977L,
  83340. 64978L,64979L,64980L,64981L,64982L,64983L,64984L,64985L,64986L,64987L,
  83341. 64988L,64989L,64990L,64991L,64992L,64993L,64994L,64995L,64996L,64997L,
  83342. 64998L,64999L,65000L,65001L,65002L,65003L,65004L,65005L,65006L,65007L,
  83343. 65008L,65009L,65010L,65011L,65012L,65013L,65014L,65015L,65016L,65017L,
  83344. 65018L,65019L,65020L,65021L,65022L,65023L,65024L,65025L,65026L,65027L,
  83345. 65028L,65029L,65030L,65031L,65032L,65033L,65034L,65035L,65036L,65037L,
  83346. 65038L,65039L,65040L,65041L,65042L,65043L,65044L,65045L,65046L,65047L,
  83347. 65048L,65049L,65050L,65051L,65052L,65053L,65054L,65055L,65056L,65057L,
  83348. 65058L,65059L,65060L,65061L,65062L,65063L,65064L,65065L,65066L,65067L,
  83349. 65068L,65069L,65070L,65071L,65072L,65073L,65074L,65075L,65076L,65077L,
  83350. 65078L,65079L,65080L,65081L,65082L,65083L,65084L,65085L,65086L,65087L,
  83351. 65088L,65089L,65090L,65091L,65092L,65093L,65094L,65095L,65096L,65097L,
  83352. 65098L,65099L,65100L,65101L,65102L,65103L,65104L,65105L,65106L,65107L,
  83353. 65108L,65109L,65110L,65111L,65112L,65113L,65114L,65115L,65116L,65117L,
  83354. 65118L,65119L,65120L,65121L,65122L,65123L,65124L,65125L,65126L,65127L,
  83355. 65128L,65129L,65130L,65131L,65132L,65133L,65134L,65135L,65136L,65137L,
  83356. 65138L,65139L,65140L,65141L,65142L,65143L,65144L,65145L,65146L,65147L,
  83357. 65148L,65149L,65150L,65151L,65152L,65153L,65154L,65155L,65156L,65157L,
  83358. 65158L,65159L,65160L,65161L,65162L,65163L,65164L,65165L,65166L,65167L,
  83359. 65168L,65169L,65170L,65171L,65172L,65173L,65174L,65175L,65176L,65177L,
  83360. 65178L,65179L,65180L,65181L,65182L,65183L,65184L,65185L,65186L,65187L,
  83361. 65188L,65189L,65190L,65191L,65192L,65193L,65194L,65195L,65196L,65197L,
  83362. 65198L,65199L,65200L,65201L,65202L,65203L,65204L,65205L,65206L,65207L,
  83363. 65208L,65209L,65210L,65211L,65212L,65213L,65214L,65215L,65216L,65217L,
  83364. 65218L,65219L,65220L,65221L,65222L,65223L,65224L,65225L,65226L,65227L,
  83365. 65228L,65229L,65230L,65231L,65232L,65233L,65234L,65235L,65236L,65237L,
  83366. 65238L,65239L,65240L,65241L,65242L,65243L,65244L,65245L,65246L,65247L,
  83367. 65248L,65249L,65250L,65251L,65252L,65253L,65254L,65255L,65256L,65257L,
  83368. 65258L,65259L,65260L,65261L,65262L,65263L,65264L,65265L,65266L,65267L,
  83369. 65268L,65269L,65270L,65271L,65272L,65273L,65274L,65275L,65276L,65277L,
  83370. 65278L,65279L,65280L,65281L,65282L,65283L,65284L,65285L,65286L,65287L,
  83371. 65288L,65289L,65290L,65291L,65292L,65293L,65294L,65295L,65296L,65297L,
  83372. 65298L,65299L,65300L,65301L,65302L,65303L,65304L,65305L,65306L,65307L,
  83373. 65308L,65309L,65310L,65311L,65312L,65313L,65314L,65315L,65316L,65317L,
  83374. 65318L,65319L,65320L,65321L,65322L,65323L,65324L,65325L,65326L,65327L,
  83375. 65328L,65329L,65330L,65331L,65332L,65333L,65334L,65335L,65336L,65337L,
  83376. 65338L,65339L,65340L,65341L,65342L,65343L,65344L,65313L,65314L,65315L,
  83377. 65316L,65317L,65318L,65319L,65320L,65321L,65322L,65323L,65324L,65325L,
  83378. 65326L,65327L,65328L,65329L,65330L,65331L,65332L,65333L,65334L,65335L,
  83379. 65336L,65337L,65338L,65371L,65372L,65373L,65374L,65375L,65376L,65377L,
  83380. 65378L,65379L,65380L,65381L,65382L,65383L,65384L,65385L,65386L,65387L,
  83381. 65388L,65389L,65390L,65391L,65392L,65393L,65394L,65395L,65396L,65397L,
  83382. 65398L,65399L,65400L,65401L,65402L,65403L,65404L,65405L,65406L,65407L,
  83383. 65408L,65409L,65410L,65411L,65412L,65413L,65414L,65415L,65416L,65417L,
  83384. 65418L,65419L,65420L,65421L,65422L,65423L,65424L,65425L,65426L,65427L,
  83385. 65428L,65429L,65430L,65431L,65432L,65433L,65434L,65435L,65436L,65437L,
  83386. 65438L,65439L,65440L,65441L,65442L,65443L,65444L,65445L,65446L,65447L,
  83387. 65448L,65449L,65450L,65451L,65452L,65453L,65454L,65455L,65456L,65457L,
  83388. 65458L,65459L,65460L,65461L,65462L,65463L,65464L,65465L,65466L,65467L,
  83389. 65468L,65469L,65470L,65471L,65472L,65473L,65474L,65475L,65476L,65477L,
  83390. 65478L,65479L,65480L,65481L,65482L,65483L,65484L,65485L,65486L,65487L,
  83391. 65488L,65489L,65490L,65491L,65492L,65493L,65494L,65495L,65496L,65497L,
  83392. 65498L,65499L,65500L,65501L,65502L,65503L,65504L,65505L,65506L,65507L,
  83393. 65508L,65509L,65510L,65511L,65512L,65513L,65514L,65515L,65516L,65517L,
  83394. 65518L,65519L,65520L,65521L,65522L,65523L,65524L,65525L,65526L,65527L,
  83395. 65528L,65529L,65530L,65531L,65532L,65533L,65534L,65535L,
  83396. };
  83397. #endif
  83398. #if defined(DUK_USE_REGEXP_CANON_BITMAP)
  83399. /*
  83400. * Automatically generated by extract_caseconv.py, do not edit!
  83401. */
  83402. const duk_uint8_t duk_unicode_re_canon_bitmap[256] = {
  83403. 23,0,224,19,1,228,255,255,255,255,255,255,255,255,255,255,255,255,255,127,
  83404. 255,255,255,255,255,255,255,255,231,247,0,16,255,227,255,255,63,255,255,
  83405. 255,255,255,255,255,1,252,255,255,255,255,255,255,255,255,255,255,255,255,
  83406. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83407. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83408. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83409. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83410. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83411. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83412. 227,193,255,255,255,147,255,255,255,255,255,255,255,255,255,255,255,255,
  83413. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83414. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83415. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
  83416. 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,251,
  83417. };
  83418. #endif
  83419. #line 1 "duk_util_bitdecoder.c"
  83420. /*
  83421. * Bitstream decoder.
  83422. */
  83423. /* #include duk_internal.h -> already included */
  83424. /* Decode 'bits' bits from the input stream (bits must be 1...24).
  83425. * When reading past bitstream end, zeroes are shifted in. The result
  83426. * is signed to match duk_bd_decode_flagged.
  83427. */
  83428. DUK_INTERNAL duk_uint32_t duk_bd_decode(duk_bitdecoder_ctx *ctx, duk_small_int_t bits) {
  83429. duk_small_int_t shift;
  83430. duk_uint32_t mask;
  83431. duk_uint32_t tmp;
  83432. /* Note: cannot read more than 24 bits without possibly shifting top bits out.
  83433. * Fixable, but adds complexity.
  83434. */
  83435. DUK_ASSERT(bits >= 1 && bits <= 24);
  83436. while (ctx->currbits < bits) {
  83437. #if 0
  83438. DUK_DDD(DUK_DDDPRINT("decode_bits: shift more data (bits=%ld, currbits=%ld)",
  83439. (long) bits, (long) ctx->currbits));
  83440. #endif
  83441. ctx->currval <<= 8;
  83442. if (ctx->offset < ctx->length) {
  83443. /* If ctx->offset >= ctx->length, we "shift zeroes in"
  83444. * instead of croaking.
  83445. */
  83446. ctx->currval |= ctx->data[ctx->offset++];
  83447. }
  83448. ctx->currbits += 8;
  83449. }
  83450. #if 0
  83451. DUK_DDD(DUK_DDDPRINT("decode_bits: bits=%ld, currbits=%ld, currval=0x%08lx",
  83452. (long) bits, (long) ctx->currbits, (unsigned long) ctx->currval));
  83453. #endif
  83454. /* Extract 'top' bits of currval; note that the extracted bits do not need
  83455. * to be cleared, we just ignore them on next round.
  83456. */
  83457. shift = ctx->currbits - bits;
  83458. mask = (((duk_uint32_t) 1U) << bits) - 1U;
  83459. tmp = (ctx->currval >> shift) & mask;
  83460. ctx->currbits = shift; /* remaining */
  83461. #if 0
  83462. DUK_DDD(DUK_DDDPRINT("decode_bits: %ld bits -> 0x%08lx (%ld), currbits=%ld, currval=0x%08lx",
  83463. (long) bits, (unsigned long) tmp, (long) tmp, (long) ctx->currbits, (unsigned long) ctx->currval));
  83464. #endif
  83465. return tmp;
  83466. }
  83467. DUK_INTERNAL duk_small_uint_t duk_bd_decode_flag(duk_bitdecoder_ctx *ctx) {
  83468. return (duk_small_uint_t) duk_bd_decode(ctx, 1);
  83469. }
  83470. /* Decode a one-bit flag, and if set, decode a value of 'bits', otherwise return
  83471. * default value.
  83472. */
  83473. DUK_INTERNAL duk_uint32_t duk_bd_decode_flagged(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_uint32_t def_value) {
  83474. if (duk_bd_decode_flag(ctx)) {
  83475. return duk_bd_decode(ctx, bits);
  83476. } else {
  83477. return def_value;
  83478. }
  83479. }
  83480. /* Signed variant, allows negative marker value. */
  83481. DUK_INTERNAL duk_int32_t duk_bd_decode_flagged_signed(duk_bitdecoder_ctx *ctx, duk_small_int_t bits, duk_int32_t def_value) {
  83482. return (duk_int32_t) duk_bd_decode_flagged(ctx, bits, (duk_uint32_t) def_value);
  83483. }
  83484. /* Shared varint encoding. Match dukutil.py BitEncode.varuint(). */
  83485. DUK_INTERNAL duk_uint32_t duk_bd_decode_varuint(duk_bitdecoder_ctx *ctx) {
  83486. duk_small_uint_t t;
  83487. /* The bit encoding choices here are based on manual testing against
  83488. * the actual varuints generated by genbuiltins.py.
  83489. */
  83490. switch (duk_bd_decode(ctx, 2)) {
  83491. case 0:
  83492. return 0; /* [0,0] */
  83493. case 1:
  83494. return duk_bd_decode(ctx, 2) + 1; /* [1,4] */
  83495. case 2:
  83496. return duk_bd_decode(ctx, 5) + 5; /* [5,36] */
  83497. default:
  83498. t = duk_bd_decode(ctx, 7);
  83499. if (t == 0) {
  83500. return duk_bd_decode(ctx, 20);
  83501. }
  83502. return (t - 1) + 37; /* [37,163] */
  83503. }
  83504. }
  83505. /* Decode a bit packed string from a custom format used by genbuiltins.py.
  83506. * This function is here because it's used for both heap and thread inits.
  83507. * Caller must supply the output buffer whose size is NOT checked!
  83508. */
  83509. #define DUK__BITPACK_LETTER_LIMIT 26
  83510. #define DUK__BITPACK_LOOKUP1 26
  83511. #define DUK__BITPACK_LOOKUP2 27
  83512. #define DUK__BITPACK_SWITCH1 28
  83513. #define DUK__BITPACK_SWITCH 29
  83514. #define DUK__BITPACK_UNUSED1 30
  83515. #define DUK__BITPACK_EIGHTBIT 31
  83516. DUK_LOCAL const duk_uint8_t duk__bitpacked_lookup[16] = {
  83517. DUK_ASC_0, DUK_ASC_1, DUK_ASC_2, DUK_ASC_3,
  83518. DUK_ASC_4, DUK_ASC_5, DUK_ASC_6, DUK_ASC_7,
  83519. DUK_ASC_8, DUK_ASC_9, DUK_ASC_UNDERSCORE, DUK_ASC_SPACE,
  83520. 0x82, 0x80, DUK_ASC_DOUBLEQUOTE, DUK_ASC_LCURLY
  83521. };
  83522. DUK_INTERNAL duk_small_uint_t duk_bd_decode_bitpacked_string(duk_bitdecoder_ctx *bd, duk_uint8_t *out) {
  83523. duk_small_uint_t len;
  83524. duk_small_uint_t mode;
  83525. duk_small_uint_t t;
  83526. duk_small_uint_t i;
  83527. len = duk_bd_decode(bd, 5);
  83528. if (len == 31) {
  83529. len = duk_bd_decode(bd, 8); /* Support up to 256 bytes; rare. */
  83530. }
  83531. mode = 32; /* 0 = uppercase, 32 = lowercase (= 'a' - 'A') */
  83532. for (i = 0; i < len; i++) {
  83533. t = duk_bd_decode(bd, 5);
  83534. if (t < DUK__BITPACK_LETTER_LIMIT) {
  83535. t = t + DUK_ASC_UC_A + mode;
  83536. } else if (t == DUK__BITPACK_LOOKUP1) {
  83537. t = duk__bitpacked_lookup[duk_bd_decode(bd, 3)];
  83538. } else if (t == DUK__BITPACK_LOOKUP2) {
  83539. t = duk__bitpacked_lookup[8 + duk_bd_decode(bd, 3)];
  83540. } else if (t == DUK__BITPACK_SWITCH1) {
  83541. t = duk_bd_decode(bd, 5);
  83542. DUK_ASSERT_DISABLE(t >= 0); /* unsigned */
  83543. DUK_ASSERT(t <= 25);
  83544. t = t + DUK_ASC_UC_A + (mode ^ 32);
  83545. } else if (t == DUK__BITPACK_SWITCH) {
  83546. mode = mode ^ 32;
  83547. t = duk_bd_decode(bd, 5);
  83548. DUK_ASSERT_DISABLE(t >= 0);
  83549. DUK_ASSERT(t <= 25);
  83550. t = t + DUK_ASC_UC_A + mode;
  83551. } else if (t == DUK__BITPACK_EIGHTBIT) {
  83552. t = duk_bd_decode(bd, 8);
  83553. }
  83554. out[i] = (duk_uint8_t) t;
  83555. }
  83556. return len;
  83557. }
  83558. /* automatic undefs */
  83559. #undef DUK__BITPACK_EIGHTBIT
  83560. #undef DUK__BITPACK_LETTER_LIMIT
  83561. #undef DUK__BITPACK_LOOKUP1
  83562. #undef DUK__BITPACK_LOOKUP2
  83563. #undef DUK__BITPACK_SWITCH
  83564. #undef DUK__BITPACK_SWITCH1
  83565. #undef DUK__BITPACK_UNUSED1
  83566. #line 1 "duk_util_bitencoder.c"
  83567. /*
  83568. * Bitstream encoder.
  83569. */
  83570. /* #include duk_internal.h -> already included */
  83571. DUK_INTERNAL void duk_be_encode(duk_bitencoder_ctx *ctx, duk_uint32_t data, duk_small_int_t bits) {
  83572. duk_uint8_t tmp;
  83573. DUK_ASSERT(ctx != NULL);
  83574. DUK_ASSERT(ctx->currbits < 8);
  83575. /* This limitation would be fixable but adds unnecessary complexity. */
  83576. DUK_ASSERT(bits >= 1 && bits <= 24);
  83577. ctx->currval = (ctx->currval << bits) | data;
  83578. ctx->currbits += bits;
  83579. while (ctx->currbits >= 8) {
  83580. if (ctx->offset < ctx->length) {
  83581. tmp = (duk_uint8_t) ((ctx->currval >> (ctx->currbits - 8)) & 0xff);
  83582. ctx->data[ctx->offset++] = tmp;
  83583. } else {
  83584. /* If buffer has been exhausted, truncate bitstream */
  83585. ctx->truncated = 1;
  83586. }
  83587. ctx->currbits -= 8;
  83588. }
  83589. }
  83590. DUK_INTERNAL void duk_be_finish(duk_bitencoder_ctx *ctx) {
  83591. duk_small_int_t npad;
  83592. DUK_ASSERT(ctx != NULL);
  83593. DUK_ASSERT(ctx->currbits < 8);
  83594. npad = (duk_small_int_t) (8 - ctx->currbits);
  83595. if (npad > 0) {
  83596. duk_be_encode(ctx, 0, npad);
  83597. }
  83598. DUK_ASSERT(ctx->currbits == 0);
  83599. }
  83600. #line 1 "duk_util_bufwriter.c"
  83601. /*
  83602. * Fast buffer writer with slack management.
  83603. */
  83604. /* #include duk_internal.h -> already included */
  83605. /* XXX: Avoid duk_{memcmp,memmove}_unsafe() by imposing a minimum length of
  83606. * >0 for the underlying dynamic buffer.
  83607. */
  83608. /*
  83609. * Macro support functions (use only macros in calling code)
  83610. */
  83611. DUK_LOCAL void duk__bw_update_ptrs(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t curr_offset, duk_size_t new_length) {
  83612. duk_uint8_t *p;
  83613. DUK_ASSERT(thr != NULL);
  83614. DUK_ASSERT(bw_ctx != NULL);
  83615. DUK_UNREF(thr);
  83616. /* 'p' might be NULL when the underlying buffer is zero size. If so,
  83617. * the resulting pointers are not used unsafely.
  83618. */
  83619. p = (duk_uint8_t *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, bw_ctx->buf);
  83620. DUK_ASSERT(p != NULL || (DUK_HBUFFER_DYNAMIC_GET_SIZE(bw_ctx->buf) == 0 && curr_offset == 0 && new_length == 0));
  83621. bw_ctx->p = p + curr_offset;
  83622. bw_ctx->p_base = p;
  83623. bw_ctx->p_limit = p + new_length;
  83624. }
  83625. DUK_INTERNAL void duk_bw_init(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_hbuffer_dynamic *h_buf) {
  83626. DUK_ASSERT(thr != NULL);
  83627. DUK_ASSERT(bw_ctx != NULL);
  83628. DUK_ASSERT(h_buf != NULL);
  83629. bw_ctx->buf = h_buf;
  83630. duk__bw_update_ptrs(thr, bw_ctx, 0, DUK_HBUFFER_DYNAMIC_GET_SIZE(h_buf));
  83631. }
  83632. DUK_INTERNAL void duk_bw_init_pushbuf(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t buf_size) {
  83633. DUK_ASSERT(thr != NULL);
  83634. DUK_ASSERT(bw_ctx != NULL);
  83635. (void) duk_push_dynamic_buffer(thr, buf_size);
  83636. bw_ctx->buf = (duk_hbuffer_dynamic *) duk_known_hbuffer(thr, -1);
  83637. DUK_ASSERT(bw_ctx->buf != NULL);
  83638. duk__bw_update_ptrs(thr, bw_ctx, 0, buf_size);
  83639. }
  83640. /* Resize target buffer for requested size. Called by the macro only when the
  83641. * fast path test (= there is space) fails.
  83642. */
  83643. DUK_INTERNAL duk_uint8_t *duk_bw_resize(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx, duk_size_t sz) {
  83644. duk_size_t curr_off;
  83645. duk_size_t add_sz;
  83646. duk_size_t new_sz;
  83647. DUK_ASSERT(thr != NULL);
  83648. DUK_ASSERT(bw_ctx != NULL);
  83649. /* We could do this operation without caller updating bw_ctx->ptr,
  83650. * but by writing it back here we can share code better.
  83651. */
  83652. curr_off = (duk_size_t) (bw_ctx->p - bw_ctx->p_base);
  83653. add_sz = (curr_off >> DUK_BW_SLACK_SHIFT) + DUK_BW_SLACK_ADD;
  83654. new_sz = curr_off + sz + add_sz;
  83655. if (DUK_UNLIKELY(new_sz < curr_off)) {
  83656. /* overflow */
  83657. DUK_ERROR_RANGE(thr, DUK_STR_BUFFER_TOO_LONG);
  83658. DUK_WO_NORETURN(return NULL;);
  83659. }
  83660. #if 0 /* for manual torture testing: tight allocation, useful with valgrind */
  83661. new_sz = curr_off + sz;
  83662. #endif
  83663. /* This is important to ensure dynamic buffer data pointer is not
  83664. * NULL (which is possible if buffer size is zero), which in turn
  83665. * causes portability issues with e.g. memmove() and memcpy().
  83666. */
  83667. DUK_ASSERT(new_sz >= 1);
  83668. DUK_DD(DUK_DDPRINT("resize bufferwriter from %ld to %ld (add_sz=%ld)", (long) curr_off, (long) new_sz, (long) add_sz));
  83669. duk_hbuffer_resize(thr, bw_ctx->buf, new_sz);
  83670. duk__bw_update_ptrs(thr, bw_ctx, curr_off, new_sz);
  83671. return bw_ctx->p;
  83672. }
  83673. /* Make buffer compact, matching current written size. */
  83674. DUK_INTERNAL void duk_bw_compact(duk_hthread *thr, duk_bufwriter_ctx *bw_ctx) {
  83675. duk_size_t len;
  83676. DUK_ASSERT(thr != NULL);
  83677. DUK_ASSERT(bw_ctx != NULL);
  83678. DUK_UNREF(thr);
  83679. len = (duk_size_t) (bw_ctx->p - bw_ctx->p_base);
  83680. duk_hbuffer_resize(thr, bw_ctx->buf, len);
  83681. duk__bw_update_ptrs(thr, bw_ctx, len, len);
  83682. }
  83683. DUK_INTERNAL void duk_bw_write_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len) {
  83684. duk_uint8_t *p_base;
  83685. DUK_ASSERT(thr != NULL);
  83686. DUK_ASSERT(bw != NULL);
  83687. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  83688. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  83689. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  83690. DUK_UNREF(thr);
  83691. p_base = bw->p_base;
  83692. duk_memcpy_unsafe((void *) bw->p,
  83693. (const void *) (p_base + src_off),
  83694. (size_t) len);
  83695. bw->p += len;
  83696. }
  83697. DUK_INTERNAL void duk_bw_write_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t src_off, duk_size_t len) {
  83698. DUK_ASSERT(thr != NULL);
  83699. DUK_ASSERT(bw != NULL);
  83700. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  83701. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  83702. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  83703. DUK_BW_ENSURE(thr, bw, len);
  83704. duk_bw_write_raw_slice(thr, bw, src_off, len);
  83705. }
  83706. DUK_INTERNAL void duk_bw_insert_raw_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len) {
  83707. duk_uint8_t *p_base;
  83708. duk_size_t buf_sz, move_sz;
  83709. DUK_ASSERT(thr != NULL);
  83710. DUK_ASSERT(bw != NULL);
  83711. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  83712. DUK_ASSERT(buf != NULL);
  83713. DUK_UNREF(thr);
  83714. p_base = bw->p_base;
  83715. buf_sz = (duk_size_t) (bw->p - p_base); /* constrained by maximum buffer size */
  83716. move_sz = buf_sz - dst_off;
  83717. DUK_ASSERT(p_base != NULL); /* buffer size is >= 1 */
  83718. duk_memmove_unsafe((void *) (p_base + dst_off + len),
  83719. (const void *) (p_base + dst_off),
  83720. (size_t) move_sz);
  83721. duk_memcpy_unsafe((void *) (p_base + dst_off),
  83722. (const void *) buf,
  83723. (size_t) len);
  83724. bw->p += len;
  83725. }
  83726. DUK_INTERNAL void duk_bw_insert_ensure_bytes(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, const duk_uint8_t *buf, duk_size_t len) {
  83727. DUK_ASSERT(thr != NULL);
  83728. DUK_ASSERT(bw != NULL);
  83729. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  83730. DUK_ASSERT(buf != NULL);
  83731. DUK_BW_ENSURE(thr, bw, len);
  83732. duk_bw_insert_raw_bytes(thr, bw, dst_off, buf, len);
  83733. }
  83734. DUK_INTERNAL void duk_bw_insert_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len) {
  83735. duk_uint8_t *p_base;
  83736. duk_size_t buf_sz, move_sz;
  83737. DUK_ASSERT(thr != NULL);
  83738. DUK_ASSERT(bw != NULL);
  83739. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  83740. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  83741. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  83742. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  83743. DUK_UNREF(thr);
  83744. p_base = bw->p_base;
  83745. /* Don't support "straddled" source now. */
  83746. DUK_ASSERT(dst_off <= src_off || dst_off >= src_off + len);
  83747. if (dst_off <= src_off) {
  83748. /* Target is before source. Source offset is expressed as
  83749. * a "before change" offset. Account for the memmove.
  83750. */
  83751. src_off += len;
  83752. }
  83753. buf_sz = (duk_size_t) (bw->p - p_base);
  83754. move_sz = buf_sz - dst_off;
  83755. DUK_ASSERT(p_base != NULL); /* buffer size is >= 1 */
  83756. duk_memmove_unsafe((void *) (p_base + dst_off + len),
  83757. (const void *) (p_base + dst_off),
  83758. (size_t) move_sz);
  83759. duk_memcpy_unsafe((void *) (p_base + dst_off),
  83760. (const void *) (p_base + src_off),
  83761. (size_t) len);
  83762. bw->p += len;
  83763. }
  83764. DUK_INTERNAL void duk_bw_insert_ensure_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t dst_off, duk_size_t src_off, duk_size_t len) {
  83765. DUK_ASSERT(thr != NULL);
  83766. DUK_ASSERT(bw != NULL);
  83767. DUK_ASSERT(dst_off <= DUK_BW_GET_SIZE(thr, bw));
  83768. DUK_ASSERT(src_off <= DUK_BW_GET_SIZE(thr, bw));
  83769. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  83770. DUK_ASSERT(src_off + len <= DUK_BW_GET_SIZE(thr, bw));
  83771. /* Don't support "straddled" source now. */
  83772. DUK_ASSERT(dst_off <= src_off || dst_off >= src_off + len);
  83773. DUK_BW_ENSURE(thr, bw, len);
  83774. duk_bw_insert_raw_slice(thr, bw, dst_off, src_off, len);
  83775. }
  83776. DUK_INTERNAL duk_uint8_t *duk_bw_insert_raw_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len) {
  83777. duk_uint8_t *p_base, *p_dst, *p_src;
  83778. duk_size_t buf_sz, move_sz;
  83779. DUK_ASSERT(thr != NULL);
  83780. DUK_ASSERT(bw != NULL);
  83781. DUK_ASSERT(off <= DUK_BW_GET_SIZE(thr, bw));
  83782. DUK_UNREF(thr);
  83783. p_base = bw->p_base;
  83784. buf_sz = (duk_size_t) (bw->p - p_base);
  83785. move_sz = buf_sz - off;
  83786. p_dst = p_base + off + len;
  83787. p_src = p_base + off;
  83788. duk_memmove_unsafe((void *) p_dst, (const void *) p_src, (size_t) move_sz);
  83789. return p_src; /* point to start of 'reserved area' */
  83790. }
  83791. DUK_INTERNAL duk_uint8_t *duk_bw_insert_ensure_area(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len) {
  83792. DUK_ASSERT(thr != NULL);
  83793. DUK_ASSERT(bw != NULL);
  83794. DUK_ASSERT(off <= DUK_BW_GET_SIZE(thr, bw));
  83795. DUK_BW_ENSURE(thr, bw, len);
  83796. return duk_bw_insert_raw_area(thr, bw, off, len);
  83797. }
  83798. DUK_INTERNAL void duk_bw_remove_raw_slice(duk_hthread *thr, duk_bufwriter_ctx *bw, duk_size_t off, duk_size_t len) {
  83799. duk_size_t move_sz;
  83800. duk_uint8_t *p_base;
  83801. duk_uint8_t *p_src;
  83802. duk_uint8_t *p_dst;
  83803. DUK_ASSERT(thr != NULL);
  83804. DUK_ASSERT(bw != NULL);
  83805. DUK_ASSERT(off <= DUK_BW_GET_SIZE(thr, bw));
  83806. DUK_ASSERT(len <= DUK_BW_GET_SIZE(thr, bw));
  83807. DUK_ASSERT(off + len <= DUK_BW_GET_SIZE(thr, bw));
  83808. DUK_UNREF(thr);
  83809. p_base = bw->p_base;
  83810. p_dst = p_base + off;
  83811. p_src = p_dst + len;
  83812. move_sz = (duk_size_t) (bw->p - p_src);
  83813. duk_memmove_unsafe((void *) p_dst,
  83814. (const void *) p_src,
  83815. (size_t) move_sz);
  83816. bw->p -= len;
  83817. }
  83818. /*
  83819. * Macro support functions for reading/writing raw data.
  83820. *
  83821. * These are done using mempcy to ensure they're valid even for unaligned
  83822. * reads/writes on platforms where alignment counts. On x86 at least gcc
  83823. * is able to compile these into a bswap+mov. "Always inline" is used to
  83824. * ensure these macros compile to minimal code.
  83825. *
  83826. * Not really bufwriter related, but currently used together.
  83827. */
  83828. DUK_INTERNAL DUK_ALWAYS_INLINE duk_uint16_t duk_raw_read_u16_be(duk_uint8_t **p) {
  83829. union {
  83830. duk_uint8_t b[2];
  83831. duk_uint16_t x;
  83832. } u;
  83833. duk_memcpy((void *) u.b, (const void *) (*p), (size_t) 2);
  83834. u.x = DUK_NTOH16(u.x);
  83835. *p += 2;
  83836. return u.x;
  83837. }
  83838. DUK_INTERNAL DUK_ALWAYS_INLINE duk_uint32_t duk_raw_read_u32_be(duk_uint8_t **p) {
  83839. union {
  83840. duk_uint8_t b[4];
  83841. duk_uint32_t x;
  83842. } u;
  83843. duk_memcpy((void *) u.b, (const void *) (*p), (size_t) 4);
  83844. u.x = DUK_NTOH32(u.x);
  83845. *p += 4;
  83846. return u.x;
  83847. }
  83848. DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_raw_read_double_be(duk_uint8_t **p) {
  83849. duk_double_union du;
  83850. union {
  83851. duk_uint8_t b[4];
  83852. duk_uint32_t x;
  83853. } u;
  83854. duk_memcpy((void *) u.b, (const void *) (*p), (size_t) 4);
  83855. u.x = DUK_NTOH32(u.x);
  83856. du.ui[DUK_DBL_IDX_UI0] = u.x;
  83857. duk_memcpy((void *) u.b, (const void *) (*p + 4), (size_t) 4);
  83858. u.x = DUK_NTOH32(u.x);
  83859. du.ui[DUK_DBL_IDX_UI1] = u.x;
  83860. *p += 8;
  83861. return du.d;
  83862. }
  83863. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_raw_write_u16_be(duk_uint8_t **p, duk_uint16_t val) {
  83864. union {
  83865. duk_uint8_t b[2];
  83866. duk_uint16_t x;
  83867. } u;
  83868. u.x = DUK_HTON16(val);
  83869. duk_memcpy((void *) (*p), (const void *) u.b, (size_t) 2);
  83870. *p += 2;
  83871. }
  83872. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_raw_write_u32_be(duk_uint8_t **p, duk_uint32_t val) {
  83873. union {
  83874. duk_uint8_t b[4];
  83875. duk_uint32_t x;
  83876. } u;
  83877. u.x = DUK_HTON32(val);
  83878. duk_memcpy((void *) (*p), (const void *) u.b, (size_t) 4);
  83879. *p += 4;
  83880. }
  83881. DUK_INTERNAL DUK_ALWAYS_INLINE void duk_raw_write_double_be(duk_uint8_t **p, duk_double_t val) {
  83882. duk_double_union du;
  83883. union {
  83884. duk_uint8_t b[4];
  83885. duk_uint32_t x;
  83886. } u;
  83887. du.d = val;
  83888. u.x = du.ui[DUK_DBL_IDX_UI0];
  83889. u.x = DUK_HTON32(u.x);
  83890. duk_memcpy((void *) (*p), (const void *) u.b, (size_t) 4);
  83891. u.x = du.ui[DUK_DBL_IDX_UI1];
  83892. u.x = DUK_HTON32(u.x);
  83893. duk_memcpy((void *) (*p + 4), (const void *) u.b, (size_t) 4);
  83894. *p += 8;
  83895. }
  83896. #line 1 "duk_util_cast.c"
  83897. /*
  83898. * Cast helpers.
  83899. *
  83900. * C99+ coercion is challenging portability-wise because out-of-range casts
  83901. * may invoke implementation defined or even undefined behavior. See e.g.
  83902. * http://blog.frama-c.com/index.php?post/2013/10/09/Overflow-float-integer.
  83903. *
  83904. * Provide explicit cast helpers which try to avoid implementation defined
  83905. * or undefined behavior. These helpers can then be simplified in the vast
  83906. * majority of cases where the implementation defined or undefined behavior
  83907. * is not problematic.
  83908. */
  83909. /* #include duk_internal.h -> already included */
  83910. /* Portable double-to-integer cast which avoids undefined behavior and avoids
  83911. * relying on fmin(), fmax(), or other intrinsics. Out-of-range results are
  83912. * not assumed by caller, but here value is clamped, NaN converts to minval.
  83913. */
  83914. #define DUK__DOUBLE_INT_CAST1(tname,minval,maxval) do { \
  83915. if (DUK_LIKELY(x >= (duk_double_t) (minval))) { \
  83916. DUK_ASSERT(!DUK_ISNAN(x)); \
  83917. if (DUK_LIKELY(x <= (duk_double_t) (maxval))) { \
  83918. return (tname) x; \
  83919. } else { \
  83920. return (tname) (maxval); \
  83921. } \
  83922. } else { \
  83923. /* NaN or below minval. Since we don't care about the result \
  83924. * for out-of-range values, just return the minimum value for \
  83925. * both. \
  83926. */ \
  83927. return (tname) (minval); \
  83928. } \
  83929. } while (0)
  83930. /* Rely on specific NaN behavior for duk_double_{fmin,fmax}(): if either
  83931. * argument is a NaN, return the second argument. This avoids a
  83932. * NaN-to-integer cast which is undefined behavior.
  83933. */
  83934. #define DUK__DOUBLE_INT_CAST2(tname,minval,maxval) do { \
  83935. return (tname) duk_double_fmin(duk_double_fmax(x, (duk_double_t) (minval)), (duk_double_t) (maxval)); \
  83936. } while (0)
  83937. /* Another solution which doesn't need C99+ behavior for fmin() and fmax(). */
  83938. #define DUK__DOUBLE_INT_CAST3(tname,minval,maxval) do { \
  83939. if (DUK_ISNAN(x)) { \
  83940. /* 0 or any other value is fine. */ \
  83941. return (tname) 0; \
  83942. } else \
  83943. return (tname) DUK_FMIN(DUK_FMAX(x, (duk_double_t) (minval)), (duk_double_t) (maxval)); \
  83944. } \
  83945. } while (0)
  83946. /* C99+ solution: relies on specific fmin() and fmax() behavior in C99: if
  83947. * one argument is NaN but the other isn't, the non-NaN argument is returned.
  83948. * Because the limits are non-NaN values, explicit NaN check is not needed.
  83949. * This may not work on all legacy platforms, and also doesn't seem to inline
  83950. * the fmin() and fmax() calls (unless one uses -ffast-math which we don't
  83951. * support).
  83952. */
  83953. #define DUK__DOUBLE_INT_CAST4(tname,minval,maxval) do { \
  83954. return (tname) DUK_FMIN(DUK_FMAX(x, (duk_double_t) (minval)), (duk_double_t) (maxval)); \
  83955. } while (0)
  83956. DUK_INTERNAL duk_int_t duk_double_to_int_t(duk_double_t x) {
  83957. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  83958. /* Real world solution: almost any practical platform will provide
  83959. * an integer value without any guarantees what it is (which is fine).
  83960. */
  83961. return (duk_int_t) x;
  83962. #else
  83963. DUK__DOUBLE_INT_CAST1(duk_int_t, DUK_INT_MIN, DUK_INT_MAX);
  83964. #endif
  83965. }
  83966. DUK_INTERNAL duk_uint_t duk_double_to_uint_t(duk_double_t x) {
  83967. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  83968. return (duk_uint_t) x;
  83969. #else
  83970. DUK__DOUBLE_INT_CAST1(duk_uint_t, DUK_UINT_MIN, DUK_UINT_MAX);
  83971. #endif
  83972. }
  83973. DUK_INTERNAL duk_int32_t duk_double_to_int32_t(duk_double_t x) {
  83974. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  83975. return (duk_int32_t) x;
  83976. #else
  83977. DUK__DOUBLE_INT_CAST1(duk_int32_t, DUK_INT32_MIN, DUK_INT32_MAX);
  83978. #endif
  83979. }
  83980. DUK_INTERNAL duk_uint32_t duk_double_to_uint32_t(duk_double_t x) {
  83981. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  83982. return (duk_uint32_t) x;
  83983. #else
  83984. DUK__DOUBLE_INT_CAST1(duk_uint32_t, DUK_UINT32_MIN, DUK_UINT32_MAX);
  83985. #endif
  83986. }
  83987. /* Largest IEEE double that doesn't round to infinity in the default rounding
  83988. * mode. The exact midpoint between (1 - 2^(-24)) * 2^128 and 2^128 rounds to
  83989. * infinity, at least on x64. This number is one double unit below that
  83990. * midpoint. See misc/float_cast.c.
  83991. */
  83992. #define DUK__FLOAT_ROUND_LIMIT 340282356779733623858607532500980858880.0
  83993. /* Maximum IEEE float. Double-to-float conversion above this would be out of
  83994. * range and thus technically undefined behavior.
  83995. */
  83996. #define DUK__FLOAT_MAX 340282346638528859811704183484516925440.0
  83997. DUK_INTERNAL duk_float_t duk_double_to_float_t(duk_double_t x) {
  83998. /* Even a double-to-float cast is technically undefined behavior if
  83999. * the double is out-of-range. C99 Section 6.3.1.5:
  84000. *
  84001. * If the value being converted is in the range of values that can
  84002. * be represented but cannot be represented exactly, the result is
  84003. * either the nearest higher or nearest lower representable value,
  84004. * chosen in an implementation-defined manner. If the value being
  84005. * converted is outside the range of values that can be represented,
  84006. * the behavior is undefined.
  84007. */
  84008. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  84009. return (duk_float_t) x;
  84010. #else
  84011. duk_double_t t;
  84012. t = DUK_FABS(x);
  84013. DUK_ASSERT((DUK_ISNAN(x) && DUK_ISNAN(t)) ||
  84014. (!DUK_ISNAN(x) && !DUK_ISNAN(t)));
  84015. if (DUK_LIKELY(t <= DUK__FLOAT_MAX)) {
  84016. /* Standard in-range case, try to get here with a minimum
  84017. * number of checks and branches.
  84018. */
  84019. DUK_ASSERT(!DUK_ISNAN(x));
  84020. return (duk_float_t) x;
  84021. } else if (t <= DUK__FLOAT_ROUND_LIMIT) {
  84022. /* Out-of-range, but rounds to min/max float. */
  84023. DUK_ASSERT(!DUK_ISNAN(x));
  84024. if (x < 0.0) {
  84025. return (duk_float_t) -DUK__FLOAT_MAX;
  84026. } else {
  84027. return (duk_float_t) DUK__FLOAT_MAX;
  84028. }
  84029. } else if (DUK_ISNAN(x)) {
  84030. /* Assumes double NaN -> float NaN considered "in range". */
  84031. DUK_ASSERT(DUK_ISNAN(x));
  84032. return (duk_float_t) x;
  84033. } else {
  84034. /* Out-of-range, rounds to +/- Infinity. */
  84035. if (x < 0.0) {
  84036. return (duk_float_t) -DUK_DOUBLE_INFINITY;
  84037. } else {
  84038. return (duk_float_t) DUK_DOUBLE_INFINITY;
  84039. }
  84040. }
  84041. #endif
  84042. }
  84043. /* automatic undefs */
  84044. #undef DUK__DOUBLE_INT_CAST1
  84045. #undef DUK__DOUBLE_INT_CAST2
  84046. #undef DUK__DOUBLE_INT_CAST3
  84047. #undef DUK__DOUBLE_INT_CAST4
  84048. #undef DUK__FLOAT_MAX
  84049. #undef DUK__FLOAT_ROUND_LIMIT
  84050. #line 1 "duk_util_double.c"
  84051. /*
  84052. * IEEE double helpers.
  84053. */
  84054. /* #include duk_internal.h -> already included */
  84055. DUK_INTERNAL duk_bool_t duk_double_is_anyinf(duk_double_t x) {
  84056. duk_double_union du;
  84057. du.d = x;
  84058. return DUK_DBLUNION_IS_ANYINF(&du);
  84059. }
  84060. DUK_INTERNAL duk_bool_t duk_double_is_posinf(duk_double_t x) {
  84061. duk_double_union du;
  84062. du.d = x;
  84063. return DUK_DBLUNION_IS_POSINF(&du);
  84064. }
  84065. DUK_INTERNAL duk_bool_t duk_double_is_neginf(duk_double_t x) {
  84066. duk_double_union du;
  84067. du.d = x;
  84068. return DUK_DBLUNION_IS_NEGINF(&du);
  84069. }
  84070. DUK_INTERNAL duk_bool_t duk_double_is_nan(duk_double_t x) {
  84071. duk_double_union du;
  84072. du.d = x;
  84073. /* Assumes we're dealing with a Duktape internal NaN which is
  84074. * NaN normalized if duk_tval requires it.
  84075. */
  84076. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  84077. return DUK_DBLUNION_IS_NAN(&du);
  84078. }
  84079. DUK_INTERNAL duk_bool_t duk_double_is_nan_or_zero(duk_double_t x) {
  84080. duk_double_union du;
  84081. du.d = x;
  84082. /* Assumes we're dealing with a Duktape internal NaN which is
  84083. * NaN normalized if duk_tval requires it.
  84084. */
  84085. DUK_ASSERT(DUK_DBLUNION_IS_NORMALIZED(&du));
  84086. return DUK_DBLUNION_IS_NAN(&du) || DUK_DBLUNION_IS_ANYZERO(&du);
  84087. }
  84088. DUK_INTERNAL duk_bool_t duk_double_is_nan_or_inf(duk_double_t x) {
  84089. duk_double_union du;
  84090. du.d = x;
  84091. /* If exponent is 0x7FF the argument is either a NaN or an
  84092. * infinity. We don't need to check any other fields.
  84093. */
  84094. #if defined(DUK_USE_64BIT_OPS)
  84095. #if defined(DUK_USE_DOUBLE_ME)
  84096. return (du.ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x000000007ff00000)) == DUK_U64_CONSTANT(0x000000007ff00000);
  84097. #else
  84098. return (du.ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x7ff0000000000000)) == DUK_U64_CONSTANT(0x7ff0000000000000);
  84099. #endif
  84100. #else
  84101. return (du.ui[DUK_DBL_IDX_UI0] & 0x7ff00000UL) == 0x7ff00000UL;
  84102. #endif
  84103. }
  84104. DUK_INTERNAL duk_bool_t duk_double_is_nan_zero_inf(duk_double_t x) {
  84105. duk_double_union du;
  84106. #if defined(DUK_USE_64BIT_OPS)
  84107. duk_uint64_t t;
  84108. #else
  84109. duk_uint32_t t;
  84110. #endif
  84111. du.d = x;
  84112. #if defined(DUK_USE_64BIT_OPS)
  84113. #if defined(DUK_USE_DOUBLE_ME)
  84114. t = du.ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x000000007ff00000);
  84115. if (t == DUK_U64_CONSTANT(0x0000000000000000)) {
  84116. t = du.ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x0000000080000000);
  84117. return t == 0;
  84118. }
  84119. if (t == DUK_U64_CONSTANT(0x000000007ff00000)) {
  84120. return 1;
  84121. }
  84122. #else
  84123. t = du.ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x7ff0000000000000);
  84124. if (t == DUK_U64_CONSTANT(0x0000000000000000)) {
  84125. t = du.ull[DUK_DBL_IDX_ULL0] & DUK_U64_CONSTANT(0x8000000000000000);
  84126. return t == 0;
  84127. }
  84128. if (t == DUK_U64_CONSTANT(0x7ff0000000000000)) {
  84129. return 1;
  84130. }
  84131. #endif
  84132. #else
  84133. t = du.ui[DUK_DBL_IDX_UI0] & 0x7ff00000UL;
  84134. if (t == 0x00000000UL) {
  84135. return DUK_DBLUNION_IS_ANYZERO(&du);
  84136. }
  84137. if (t == 0x7ff00000UL) {
  84138. return 1;
  84139. }
  84140. #endif
  84141. return 0;
  84142. }
  84143. DUK_INTERNAL duk_small_uint_t duk_double_signbit(duk_double_t x) {
  84144. duk_double_union du;
  84145. du.d = x;
  84146. return (duk_small_uint_t) DUK_DBLUNION_GET_SIGNBIT(&du);
  84147. }
  84148. DUK_INTERNAL duk_double_t duk_double_trunc_towards_zero(duk_double_t x) {
  84149. /* XXX: optimize */
  84150. duk_small_uint_t s = duk_double_signbit(x);
  84151. x = DUK_FLOOR(DUK_FABS(x)); /* truncate towards zero */
  84152. if (s) {
  84153. x = -x;
  84154. }
  84155. return x;
  84156. }
  84157. DUK_INTERNAL duk_bool_t duk_double_same_sign(duk_double_t x, duk_double_t y) {
  84158. duk_double_union du1;
  84159. duk_double_union du2;
  84160. du1.d = x;
  84161. du2.d = y;
  84162. return (((du1.ui[DUK_DBL_IDX_UI0] ^ du2.ui[DUK_DBL_IDX_UI0]) & 0x80000000UL) == 0);
  84163. }
  84164. DUK_INTERNAL duk_double_t duk_double_fmin(duk_double_t x, duk_double_t y) {
  84165. /* Doesn't replicate fmin() behavior exactly: for fmin() if one
  84166. * argument is a NaN, the other argument should be returned.
  84167. * Duktape doesn't rely on this behavior so the replacement can
  84168. * be simplified.
  84169. */
  84170. return (x < y ? x : y);
  84171. }
  84172. DUK_INTERNAL duk_double_t duk_double_fmax(duk_double_t x, duk_double_t y) {
  84173. /* Doesn't replicate fmax() behavior exactly: for fmax() if one
  84174. * argument is a NaN, the other argument should be returned.
  84175. * Duktape doesn't rely on this behavior so the replacement can
  84176. * be simplified.
  84177. */
  84178. return (x > y ? x : y);
  84179. }
  84180. DUK_INTERNAL duk_bool_t duk_double_is_finite(duk_double_t x) {
  84181. return !duk_double_is_nan_or_inf(x);
  84182. }
  84183. DUK_INTERNAL duk_bool_t duk_double_is_integer(duk_double_t x) {
  84184. if (duk_double_is_nan_or_inf(x)) {
  84185. return 0;
  84186. } else {
  84187. return duk_js_tointeger_number(x) == x;
  84188. }
  84189. }
  84190. DUK_INTERNAL duk_bool_t duk_double_is_safe_integer(duk_double_t x) {
  84191. /* >>> 2**53-1
  84192. * 9007199254740991
  84193. */
  84194. return duk_double_is_integer(x) && DUK_FABS(x) <= 9007199254740991.0;
  84195. }
  84196. /* Check whether a duk_double_t is a whole number in the 32-bit range (reject
  84197. * negative zero), and if so, return a duk_int32_t.
  84198. * For compiler use: don't allow negative zero as it will cause trouble with
  84199. * LDINT+LDINTX, positive zero is OK.
  84200. */
  84201. DUK_INTERNAL duk_bool_t duk_is_whole_get_int32_nonegzero(duk_double_t x, duk_int32_t *ival) {
  84202. duk_int32_t t;
  84203. t = duk_double_to_int32_t(x);
  84204. if (!((duk_double_t) t == x)) {
  84205. return 0;
  84206. }
  84207. if (t == 0) {
  84208. duk_double_union du;
  84209. du.d = x;
  84210. if (DUK_DBLUNION_HAS_SIGNBIT(&du)) {
  84211. return 0;
  84212. }
  84213. }
  84214. *ival = t;
  84215. return 1;
  84216. }
  84217. /* Check whether a duk_double_t is a whole number in the 32-bit range, and if
  84218. * so, return a duk_int32_t.
  84219. */
  84220. DUK_INTERNAL duk_bool_t duk_is_whole_get_int32(duk_double_t x, duk_int32_t *ival) {
  84221. duk_int32_t t;
  84222. t = duk_double_to_int32_t(x);
  84223. if (!((duk_double_t) t == x)) {
  84224. return 0;
  84225. }
  84226. *ival = t;
  84227. return 1;
  84228. }
  84229. /* Division: division by zero is undefined behavior (and may in fact trap)
  84230. * so it needs special handling for portability.
  84231. */
  84232. DUK_INTERNAL DUK_INLINE duk_double_t duk_double_div(duk_double_t x, duk_double_t y) {
  84233. #if !defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  84234. if (DUK_UNLIKELY(y == 0.0)) {
  84235. /* In C99+ division by zero is undefined behavior so
  84236. * avoid it entirely. Hopefully the compiler is
  84237. * smart enough to avoid emitting any actual code
  84238. * because almost all practical platforms behave as
  84239. * expected.
  84240. */
  84241. if (x > 0.0) {
  84242. if (DUK_SIGNBIT(y)) {
  84243. return -DUK_DOUBLE_INFINITY;
  84244. } else {
  84245. return DUK_DOUBLE_INFINITY;
  84246. }
  84247. } else if (x < 0.0) {
  84248. if (DUK_SIGNBIT(y)) {
  84249. return DUK_DOUBLE_INFINITY;
  84250. } else {
  84251. return -DUK_DOUBLE_INFINITY;
  84252. }
  84253. } else {
  84254. /* +/- 0, NaN */
  84255. return DUK_DOUBLE_NAN;
  84256. }
  84257. }
  84258. #endif
  84259. return x / y;
  84260. }
  84261. #line 1 "duk_util_hashbytes.c"
  84262. /*
  84263. * Hash function duk_util_hashbytes().
  84264. *
  84265. * Currently, 32-bit MurmurHash2.
  84266. *
  84267. * Don't rely on specific hash values; hash function may be endianness
  84268. * dependent, for instance.
  84269. */
  84270. /* #include duk_internal.h -> already included */
  84271. #if defined(DUK_USE_STRHASH_DENSE)
  84272. /* 'magic' constants for Murmurhash2 */
  84273. #define DUK__MAGIC_M ((duk_uint32_t) 0x5bd1e995UL)
  84274. #define DUK__MAGIC_R 24
  84275. DUK_INTERNAL duk_uint32_t duk_util_hashbytes(const duk_uint8_t *data, duk_size_t len, duk_uint32_t seed) {
  84276. duk_uint32_t h = seed ^ ((duk_uint32_t) len);
  84277. while (len >= 4) {
  84278. /* Portability workaround is required for platforms without
  84279. * unaligned access. The replacement code emulates little
  84280. * endian access even on big endian architectures, which is
  84281. * OK as long as it is consistent for a build.
  84282. */
  84283. #if defined(DUK_USE_HASHBYTES_UNALIGNED_U32_ACCESS)
  84284. duk_uint32_t k = *((const duk_uint32_t *) (const void *) data);
  84285. #else
  84286. duk_uint32_t k = ((duk_uint32_t) data[0]) |
  84287. (((duk_uint32_t) data[1]) << 8) |
  84288. (((duk_uint32_t) data[2]) << 16) |
  84289. (((duk_uint32_t) data[3]) << 24);
  84290. #endif
  84291. k *= DUK__MAGIC_M;
  84292. k ^= k >> DUK__MAGIC_R;
  84293. k *= DUK__MAGIC_M;
  84294. h *= DUK__MAGIC_M;
  84295. h ^= k;
  84296. data += 4;
  84297. len -= 4;
  84298. }
  84299. switch (len) {
  84300. case 3: h ^= data[2] << 16;
  84301. case 2: h ^= data[1] << 8;
  84302. case 1: h ^= data[0];
  84303. h *= DUK__MAGIC_M;
  84304. }
  84305. h ^= h >> 13;
  84306. h *= DUK__MAGIC_M;
  84307. h ^= h >> 15;
  84308. return h;
  84309. }
  84310. #endif /* DUK_USE_STRHASH_DENSE */
  84311. /* automatic undefs */
  84312. #undef DUK__MAGIC_M
  84313. #undef DUK__MAGIC_R
  84314. #line 1 "duk_util_memory.c"
  84315. /*
  84316. * Memory utils.
  84317. */
  84318. /* #include duk_internal.h -> already included */
  84319. #if defined(DUK_USE_ALLOW_UNDEFINED_BEHAVIOR)
  84320. DUK_INTERNAL DUK_INLINE duk_small_int_t duk_memcmp_unsafe(const void *s1, const void *s2, duk_size_t len) {
  84321. DUK_ASSERT(s1 != NULL || len == 0U);
  84322. DUK_ASSERT(s2 != NULL || len == 0U);
  84323. return DUK_MEMCMP(s1, s2, (size_t) len);
  84324. }
  84325. DUK_INTERNAL DUK_INLINE duk_small_int_t duk_memcmp(const void *s1, const void *s2, duk_size_t len) {
  84326. DUK_ASSERT(s1 != NULL);
  84327. DUK_ASSERT(s2 != NULL);
  84328. return DUK_MEMCMP(s1, s2, (size_t) len);
  84329. }
  84330. #else /* DUK_USE_ALLOW_UNDEFINED_BEHAVIOR */
  84331. DUK_INTERNAL DUK_INLINE duk_small_int_t duk_memcmp_unsafe(const void *s1, const void *s2, duk_size_t len) {
  84332. DUK_ASSERT(s1 != NULL || len == 0U);
  84333. DUK_ASSERT(s2 != NULL || len == 0U);
  84334. if (DUK_UNLIKELY(len == 0U)) {
  84335. return 0;
  84336. }
  84337. DUK_ASSERT(s1 != NULL);
  84338. DUK_ASSERT(s2 != NULL);
  84339. return duk_memcmp(s1, s2, len);
  84340. }
  84341. DUK_INTERNAL DUK_INLINE duk_small_int_t duk_memcmp(const void *s1, const void *s2, duk_size_t len) {
  84342. DUK_ASSERT(s1 != NULL);
  84343. DUK_ASSERT(s2 != NULL);
  84344. return DUK_MEMCMP(s1, s2, (size_t) len);
  84345. }
  84346. #endif /* DUK_USE_ALLOW_UNDEFINED_BEHAVIOR */
  84347. #line 1 "duk_util_tinyrandom.c"
  84348. /*
  84349. * A tiny random number generator used for Math.random() and other internals.
  84350. *
  84351. * Default algorithm is xoroshiro128+: http://xoroshiro.di.unimi.it/xoroshiro128plus.c
  84352. * with SplitMix64 seed preparation: http://xorshift.di.unimi.it/splitmix64.c.
  84353. *
  84354. * Low memory targets and targets without 64-bit types use a slightly smaller
  84355. * (but slower) algorithm by Adi Shamir:
  84356. * http://www.woodmann.com/forum/archive/index.php/t-3100.html.
  84357. *
  84358. */
  84359. /* #include duk_internal.h -> already included */
  84360. #if !defined(DUK_USE_GET_RANDOM_DOUBLE)
  84361. #if defined(DUK_USE_PREFER_SIZE) || !defined(DUK_USE_64BIT_OPS)
  84362. #define DUK__RANDOM_SHAMIR3OP
  84363. #else
  84364. #define DUK__RANDOM_XOROSHIRO128PLUS
  84365. #endif
  84366. #if defined(DUK__RANDOM_SHAMIR3OP)
  84367. #define DUK__UPDATE_RND(rnd) do { \
  84368. (rnd) += ((rnd) * (rnd)) | 0x05UL; \
  84369. (rnd) = ((rnd) & 0xffffffffUL); /* if duk_uint32_t is exactly 32 bits, this is a NOP */ \
  84370. } while (0)
  84371. #define DUK__RND_BIT(rnd) ((rnd) >> 31) /* only use the highest bit */
  84372. DUK_INTERNAL void duk_util_tinyrandom_prepare_seed(duk_hthread *thr) {
  84373. DUK_UNREF(thr); /* Nothing now. */
  84374. }
  84375. DUK_INTERNAL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr) {
  84376. duk_double_t t;
  84377. duk_small_int_t n;
  84378. duk_uint32_t rnd;
  84379. rnd = thr->heap->rnd_state;
  84380. n = 53; /* enough to cover the whole mantissa */
  84381. t = 0.0;
  84382. do {
  84383. DUK__UPDATE_RND(rnd);
  84384. t += DUK__RND_BIT(rnd);
  84385. t /= 2.0;
  84386. } while (--n);
  84387. thr->heap->rnd_state = rnd;
  84388. DUK_ASSERT(t >= (duk_double_t) 0.0);
  84389. DUK_ASSERT(t < (duk_double_t) 1.0);
  84390. return t;
  84391. }
  84392. #endif /* DUK__RANDOM_SHAMIR3OP */
  84393. #if defined(DUK__RANDOM_XOROSHIRO128PLUS)
  84394. DUK_LOCAL DUK_ALWAYS_INLINE duk_uint64_t duk__rnd_splitmix64(duk_uint64_t *x) {
  84395. duk_uint64_t z;
  84396. z = (*x += DUK_U64_CONSTANT(0x9E3779B97F4A7C15));
  84397. z = (z ^ (z >> 30U)) * DUK_U64_CONSTANT(0xBF58476D1CE4E5B9);
  84398. z = (z ^ (z >> 27U)) * DUK_U64_CONSTANT(0x94D049BB133111EB);
  84399. return z ^ (z >> 31U);
  84400. }
  84401. DUK_LOCAL DUK_ALWAYS_INLINE duk_uint64_t duk__rnd_rotl(const duk_uint64_t x, duk_small_uint_t k) {
  84402. return (x << k) | (x >> (64U - k));
  84403. }
  84404. DUK_LOCAL DUK_ALWAYS_INLINE duk_uint64_t duk__xoroshiro128plus(duk_uint64_t *s) {
  84405. duk_uint64_t s0;
  84406. duk_uint64_t s1;
  84407. duk_uint64_t res;
  84408. s0 = s[0];
  84409. s1 = s[1];
  84410. res = s0 + s1;
  84411. s1 ^= s0;
  84412. s[0] = duk__rnd_rotl(s0, 55) ^ s1 ^ (s1 << 14U);
  84413. s[1] = duk__rnd_rotl(s1, 36);
  84414. return res;
  84415. }
  84416. DUK_INTERNAL void duk_util_tinyrandom_prepare_seed(duk_hthread *thr) {
  84417. duk_small_uint_t i;
  84418. duk_uint64_t x;
  84419. /* Mix both halves of the initial seed with SplitMix64. The intent
  84420. * is to ensure that very similar raw seeds (which is usually the case
  84421. * because current seed is Date.now()) result in different xoroshiro128+
  84422. * seeds.
  84423. */
  84424. x = thr->heap->rnd_state[0]; /* Only [0] is used as input here. */
  84425. for (i = 0; i < 64; i++) {
  84426. thr->heap->rnd_state[i & 0x01] = duk__rnd_splitmix64(&x); /* Keep last 2 values. */
  84427. }
  84428. }
  84429. DUK_INTERNAL duk_double_t duk_util_tinyrandom_get_double(duk_hthread *thr) {
  84430. duk_uint64_t v;
  84431. duk_double_union du;
  84432. /* For big and little endian the integer and IEEE double byte order
  84433. * is the same so a direct assignment works. For mixed endian the
  84434. * 32-bit parts must be swapped.
  84435. */
  84436. v = (DUK_U64_CONSTANT(0x3ff) << 52U) | (duk__xoroshiro128plus((duk_uint64_t *) thr->heap->rnd_state) >> 12U);
  84437. du.ull[0] = v;
  84438. #if defined(DUK_USE_DOUBLE_ME)
  84439. do {
  84440. duk_uint32_t tmp;
  84441. tmp = du.ui[0];
  84442. du.ui[0] = du.ui[1];
  84443. du.ui[1] = tmp;
  84444. } while (0);
  84445. #endif
  84446. return du.d - 1.0;
  84447. }
  84448. #endif /* DUK__RANDOM_XOROSHIRO128PLUS */
  84449. #endif /* !DUK_USE_GET_RANDOM_DOUBLE */
  84450. /* automatic undefs */
  84451. #undef DUK__RANDOM_SHAMIR3OP
  84452. #undef DUK__RANDOM_XOROSHIRO128PLUS
  84453. #undef DUK__RND_BIT
  84454. #undef DUK__UPDATE_RND